-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
101 lines (83 loc) · 3.4 KB
/
Copy pathextension.js
File metadata and controls
101 lines (83 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const axios = require('axios');
const INTENTO_API_URL = 'https://api.inten.to';
const getApiKey = () => {
const apiKey = vscode.workspace.getConfiguration('intento').get('apiKey');
if (!apiKey) {
throw new Error('Inten.to API key is not set. Please configure it in Settings.');
}
return apiKey;
};
const getTargetLanguage = () => {
const targetLanguage = vscode.workspace.getConfiguration('intento').get('targetLanguage');
if (!targetLanguage) {
throw new Error('Target language is not set. Please configure it in Settings.');
}
return targetLanguage;
};
async function getTranslation(text, apiKey, targetLanguage) {
const headers = { 'apikey': apiKey, 'Content-Type': 'application/json' };
const submitResponse = await axios.post(`${INTENTO_API_URL}/ai/text/translate`, {
context: { text: [text], to: targetLanguage, from: '' },
service: { routing: 'best_it', async: true }
}, { headers });
const operationId = submitResponse.data.id;
if (!operationId) {
throw new Error('Failed to submit translation job to Inten.to.');
}
const maxRetries = 12;
for (let i = 0; i < maxRetries; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
const resultResponse = await axios.get(`${INTENTO_API_URL}/operations/${operationId}`, { headers });
const result = resultResponse.data;
if (result.done) {
if (result.error) {
throw new Error(`Inten.to API Error: ${JSON.stringify(result.error)}`);
}
const translatedText = result.response?.[0]?.results?.[0];
if (!translatedText) {
throw new Error('Could not parse translated text from the API response.');
}
return translatedText;
}
}
throw new Error('Translation job timed out. The server took too long to respond.');
}
function activate(context) {
const translateCommand = vscode.commands.registerCommand('intento.translate', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor || editor.selection.isEmpty) {
vscode.window.showWarningMessage('No text selected for translation.');
return;
}
const selectedText = editor.document.getText(editor.selection);
try {
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Translating with Inten.to...",
cancellable: false
}, async () => {
const apiKey = getApiKey();
const targetLanguage = getTargetLanguage();
const translatedText = await getTranslation(selectedText, apiKey, targetLanguage);
await vscode.env.clipboard.writeText(translatedText);
vscode.window.showInformationMessage(`Translated & Copied: ${translatedText}`);
});
} catch (error) {
vscode.window.showErrorMessage(error.message);
console.error(error);
}
});
context.subscriptions.push(translateCommand);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
// below is for testing
getApiKey,
getTargetLanguage,
getTranslation
};