@yxw007/translate 0.0.20 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -23,12 +23,21 @@ interface BaiduEngineOption extends BaseEngineOption {
23
23
  }
24
24
  declare function baidu(options: BaiduEngineOption): Engine;
25
25
 
26
+ interface DeeplEngineOption {
27
+ key: string;
28
+ }
29
+ declare function deepl(options: DeeplEngineOption): {
30
+ name: string;
31
+ translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>): Promise<string[]>;
32
+ };
33
+
26
34
  declare const engines: {
27
35
  readonly google: typeof google;
28
36
  readonly azure: typeof azure;
29
37
  readonly amazon: typeof amazon;
30
38
  readonly baidu: typeof baidu;
31
39
  readonly deepl: typeof deepl;
40
+ readonly openai: typeof openai;
32
41
  };
33
42
 
34
43
  declare const _default$6: {
@@ -748,6 +757,7 @@ type originLanguageMapNames = {
748
757
  google: keyof typeof _default$5;
749
758
  baidu: keyof typeof _default$4;
750
759
  deepl: keyof typeof _default$3;
760
+ openai: keyof typeof _default$5;
751
761
  };
752
762
  type originLanguageMapValues = {
753
763
  amazon: ValuesOf<typeof _default$2>;
@@ -755,6 +765,7 @@ type originLanguageMapValues = {
755
765
  google: ValuesOf<typeof _default$5>;
756
766
  baidu: ValuesOf<typeof _default$4>;
757
767
  deepl: ValuesOf<typeof _default$3>;
768
+ openai: ValuesOf<typeof _default$5>;
758
769
  };
759
770
 
760
771
  declare const _default$1: {
@@ -817,6 +828,7 @@ type targetLanguageMapNames = {
817
828
  google: keyof typeof _default$5;
818
829
  baidu: keyof typeof _default$4;
819
830
  deepl: keyof typeof _default$1;
831
+ openai: keyof typeof _default$5;
820
832
  };
821
833
  type targetLanguageMapValues = {
822
834
  amazon: ValuesOf<typeof _default$2>;
@@ -824,6 +836,7 @@ type targetLanguageMapValues = {
824
836
  google: ValuesOf<typeof _default$5>;
825
837
  baidu: ValuesOf<typeof _default$4>;
826
838
  deepl: ValuesOf<typeof _default$1>;
839
+ openai: ValuesOf<typeof _default$5>;
827
840
  };
828
841
 
829
842
  type KeysOfTarget<T extends keyof targetLanguageMapNames> = targetLanguageMapNames[T];
@@ -2267,19 +2280,27 @@ declare class TranslationError extends Error {
2267
2280
  constructor(region: string, message: string);
2268
2281
  }
2269
2282
 
2270
- interface DeeplEngineOption {
2271
- key: string;
2283
+ declare const OPEN_AI_MODELS: readonly ["o1-preview", "o1-preview-2024-09-12", "o1-mini-2024-09-12", "o1-mini", "dall-e-2", "gpt-3.5-turbo", "gpt-3.5-turbo-0125", "babbage-002", "davinci-002", "dall-e-3", "text-embedding-3-large", "gpt-3.5-turbo-16k", "tts-1-hd-1106", "text-embedding-ada-002", "text-embedding-3-small", "tts-1-hd", "whisper-1", "gpt-3.5-turbo-1106", "gpt-3.5-turbo-instruct", "gpt-4o-mini-2024-07-18", "gpt-4o-mini", "tts-1", "tts-1-1106", "gpt-3.5-turbo-instruct-0914"];
2284
+ type OpenAIModel = (typeof OPEN_AI_MODELS)[number];
2285
+
2286
+ interface OpenAIEngineOption extends BaseEngineOption {
2287
+ apiKey: string;
2288
+ model: OpenAIModel;
2272
2289
  }
2273
- declare function deepl(options: DeeplEngineOption): {
2274
- name: string;
2275
- translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>): Promise<string[]>;
2276
- };
2290
+ declare function openai(options: OpenAIEngineOption): Engine;
2277
2291
 
2278
2292
  declare class Translator {
2279
2293
  private engines;
2280
2294
  private cache_time;
2281
2295
  constructor(cache_time?: number);
2296
+ /**
2297
+ * This method is obsolete, please use the addEngine method
2298
+ * @param engine {@link Engine} instance
2299
+ * @deprecated Use {@link addEngine} instead.
2300
+ */
2282
2301
  use(engine: Engine): void;
2302
+ addEngine(engine: Engine): void;
2303
+ removeEngine(engineName: string): false | undefined;
2283
2304
  translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>): Promise<string[]>;
2284
2305
  }
2285
2306
  declare const translator: Translator;
@@ -2290,6 +2311,7 @@ declare const _default: {
2290
2311
  readonly amazon: typeof amazon;
2291
2312
  readonly baidu: typeof baidu;
2292
2313
  readonly deepl: typeof deepl;
2314
+ readonly openai: typeof openai;
2293
2315
  };
2294
2316
  translator: Translator;
2295
2317
  Translator: typeof Translator;
@@ -2297,5 +2319,5 @@ declare const _default: {
2297
2319
  getLanguage: typeof getLanguage;
2298
2320
  };
2299
2321
 
2300
- export { type BaseEngineOption, Cache, type CacheRecord, type Engine, type EngineTranslateOptions, type Engines, type FromLanguage, type ToLanguage, type TranslateOptions, TranslationError, Translator, type ValuesOf, _default as default, engines, getLanguage, translator };
2322
+ export { type BaseEngineOption, Cache, type CacheRecord, type Engine, type EngineTranslateOptions, type Engines, type FromLanguage, OPEN_AI_MODELS, type OpenAIModel, type ToLanguage, type TranslateOptions, TranslationError, Translator, type ValuesOf, _default as default, engines, getLanguage, translator };
2301
2323
  //# sourceMappingURL=index.d.ts.map
@@ -1,4 +1,4 @@
1
- // translate v0.0.20 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.1.1 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -13,8 +13,34 @@ class TranslationError extends Error {
13
13
  Error.captureStackTrace(this, this.constructor);
14
14
  }
15
15
  }
16
-
17
- function google$1(options) {
16
+ const OPEN_AI_MODELS = [
17
+ "o1-preview",
18
+ "o1-preview-2024-09-12",
19
+ "o1-mini-2024-09-12",
20
+ "o1-mini",
21
+ "dall-e-2",
22
+ "gpt-3.5-turbo",
23
+ "gpt-3.5-turbo-0125",
24
+ "babbage-002",
25
+ "davinci-002",
26
+ "dall-e-3",
27
+ "text-embedding-3-large",
28
+ "gpt-3.5-turbo-16k",
29
+ "tts-1-hd-1106",
30
+ "text-embedding-ada-002",
31
+ "text-embedding-3-small",
32
+ "tts-1-hd",
33
+ "whisper-1",
34
+ "gpt-3.5-turbo-1106",
35
+ "gpt-3.5-turbo-instruct",
36
+ "gpt-4o-mini-2024-07-18",
37
+ "gpt-4o-mini",
38
+ "tts-1",
39
+ "tts-1-1106",
40
+ "gpt-3.5-turbo-instruct-0914",
41
+ ];
42
+
43
+ function google(options) {
18
44
  const base = "https://translate.googleapis.com/translate_a/single";
19
45
  return {
20
46
  name: "google",
@@ -1315,12 +1341,74 @@ function deepl$2(options) {
1315
1341
  };
1316
1342
  }
1317
1343
 
1344
+ function openai$1(options) {
1345
+ const { apiKey, model } = options;
1346
+ const name = "openai";
1347
+ const checkOptions = () => {
1348
+ if (!apiKey) {
1349
+ throw new TranslationError(name, `${name} apiKey is required`);
1350
+ }
1351
+ if (!OPEN_AI_MODELS.includes(model)) {
1352
+ throw new TranslationError(name, `${name} model=${model} is invalid`);
1353
+ }
1354
+ };
1355
+ checkOptions();
1356
+ const base = "https://api.openai.com/v1/chat/completions";
1357
+ return {
1358
+ name,
1359
+ async translate(text, opts) {
1360
+ checkOptions();
1361
+ const { from, to } = opts;
1362
+ const url = `${base}`;
1363
+ if (!Array.isArray(text)) {
1364
+ text = [text];
1365
+ }
1366
+ const prompt = {
1367
+ role: "user",
1368
+ content: `Translate the following texts from ${from} to ${to}:
1369
+ -$s$-
1370
+ ${text.join("\n")}
1371
+ -$e$-
1372
+ Translated content is between the start marker -$s$- and the end marker -$e$-, do not return the start and end markers, only the translated text is returned.
1373
+ Connect multiple text with newline character, keep the original order when return.
1374
+ `,
1375
+ };
1376
+ const res = await fetch(url, {
1377
+ method: "POST",
1378
+ headers: {
1379
+ "Content-Type": "application/json",
1380
+ Authorization: `Bearer ${apiKey}`,
1381
+ },
1382
+ body: JSON.stringify({
1383
+ model,
1384
+ messages: [{ role: "system", content: "You are a professional IT translator" }, prompt],
1385
+ max_tokens: 2000,
1386
+ }),
1387
+ });
1388
+ const bodyRes = await res.json();
1389
+ if (bodyRes.error) {
1390
+ throw new TranslationError(this.name, `Translate fail! message: ${bodyRes.error.message}`);
1391
+ }
1392
+ if (!bodyRes || !bodyRes.choices || bodyRes.choices.length === 0 || !bodyRes.choices[0]?.message?.content) {
1393
+ throw new TranslationError(this.name, "Translate fail ! translate's result is null or empty");
1394
+ }
1395
+ const content = bodyRes.choices[0].message.content;
1396
+ const translations = content
1397
+ .trim()
1398
+ .split("\n")
1399
+ .map((item) => item.trim());
1400
+ return translations;
1401
+ },
1402
+ };
1403
+ }
1404
+
1318
1405
  const engines = {
1319
- google: google$1,
1406
+ google,
1320
1407
  azure: azure$1,
1321
1408
  amazon: amazon$1,
1322
1409
  baidu: baidu$1,
1323
1410
  deepl: deepl$2,
1411
+ openai: openai$1,
1324
1412
  };
1325
1413
 
1326
1414
  class Cache {
@@ -1548,7 +1636,7 @@ var azure = {
1548
1636
  Zulu: "zu",
1549
1637
  };
1550
1638
 
1551
- var google = {
1639
+ var openai = {
1552
1640
  Abkhazian: "ab",
1553
1641
  Acehnese: "ace",
1554
1642
  "Acholi language": "ach",
@@ -2121,10 +2209,11 @@ var amazon = {
2121
2209
 
2122
2210
  const originLanguages = {
2123
2211
  azure: azure,
2124
- google: google,
2212
+ google: openai,
2125
2213
  baidu: baidu,
2126
2214
  deepl: deepl$1,
2127
2215
  amazon: amazon,
2216
+ openai: openai,
2128
2217
  };
2129
2218
 
2130
2219
  var deepl = {
@@ -2168,10 +2257,11 @@ var deepl = {
2168
2257
 
2169
2258
  const targetLanguages = {
2170
2259
  azure: azure,
2171
- google: google,
2260
+ google: openai,
2172
2261
  baidu: baidu,
2173
2262
  deepl: deepl,
2174
2263
  amazon: amazon,
2264
+ openai: openai,
2175
2265
  };
2176
2266
 
2177
2267
  function normalFromLanguage(from, engine) {
@@ -2225,13 +2315,28 @@ class Translator {
2225
2315
  this.engines = new Map();
2226
2316
  this.cache_time = cache_time;
2227
2317
  }
2318
+ /**
2319
+ * This method is obsolete, please use the addEngine method
2320
+ * @param engine {@link Engine} instance
2321
+ * @deprecated Use {@link addEngine} instead.
2322
+ */
2228
2323
  use(engine) {
2324
+ this.addEngine(engine);
2325
+ }
2326
+ addEngine(engine) {
2229
2327
  if (this.engines.has(engine.name)) {
2230
2328
  logger.warn("Engine already exists");
2231
2329
  return;
2232
2330
  }
2233
2331
  this.engines.set(engine.name, engine);
2234
2332
  }
2333
+ removeEngine(engineName) {
2334
+ if (!engineName || !this.engines.has(engineName)) {
2335
+ logger.warn("Engine name is required or not found");
2336
+ return false;
2337
+ }
2338
+ this.engines.delete(engineName);
2339
+ }
2235
2340
  async translate(text, options) {
2236
2341
  const { engine = "google", cache_time = 60 * 1000 } = options;
2237
2342
  let { from = "auto", to } = options;
@@ -2283,6 +2388,7 @@ var index = {
2283
2388
  };
2284
2389
 
2285
2390
  exports.Cache = Cache;
2391
+ exports.OPEN_AI_MODELS = OPEN_AI_MODELS;
2286
2392
  exports.TranslationError = TranslationError;
2287
2393
  exports.Translator = Translator;
2288
2394
  exports.default = index;