glotto 3.0.0 → 3.1.0
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/README.md +112 -47
- package/esm/cli.js +136 -32
- package/esm/deno.d.ts +5 -0
- package/esm/deno.js +18 -6
- package/esm/src/config.d.ts +4 -0
- package/esm/src/config.d.ts.map +1 -0
- package/esm/src/config.js +95 -0
- package/esm/src/contants.d.ts +4 -0
- package/esm/src/contants.d.ts.map +1 -1
- package/esm/src/contants.js +14 -5
- package/esm/src/diff.d.ts +4 -0
- package/esm/src/diff.d.ts.map +1 -0
- package/esm/src/diff.js +53 -0
- package/esm/src/file.d.ts +4 -3
- package/esm/src/file.d.ts.map +1 -1
- package/esm/src/file.js +23 -5
- package/esm/src/providers/anthropic.d.ts +2 -2
- package/esm/src/providers/anthropic.d.ts.map +1 -1
- package/esm/src/providers/anthropic.js +9 -1
- package/esm/src/providers/gemini.d.ts +2 -2
- package/esm/src/providers/gemini.d.ts.map +1 -1
- package/esm/src/providers/gemini.js +10 -2
- package/esm/src/providers/openai.d.ts +2 -2
- package/esm/src/providers/openai.d.ts.map +1 -1
- package/esm/src/providers/openai.js +9 -1
- package/esm/src/translator.d.ts +14 -7
- package/esm/src/translator.d.ts.map +1 -1
- package/esm/src/translator.js +161 -77
- package/esm/src/types.d.ts +31 -3
- package/esm/src/types.d.ts.map +1 -1
- package/esm/src/utilites.d.ts +3 -4
- package/esm/src/utilites.d.ts.map +1 -1
- package/esm/src/utilites.js +40 -16
- package/package.json +20 -6
- package/schema/glotto.schema.json +87 -0
- package/script/cli.js +134 -30
- package/script/deno.d.ts +5 -0
- package/script/deno.js +18 -6
- package/script/src/config.d.ts +4 -0
- package/script/src/config.d.ts.map +1 -0
- package/script/src/config.js +132 -0
- package/script/src/contants.d.ts +4 -0
- package/script/src/contants.d.ts.map +1 -1
- package/script/src/contants.js +15 -6
- package/script/src/diff.d.ts +4 -0
- package/script/src/diff.d.ts.map +1 -0
- package/script/src/diff.js +57 -0
- package/script/src/file.d.ts +4 -3
- package/script/src/file.d.ts.map +1 -1
- package/script/src/file.js +28 -10
- package/script/src/providers/anthropic.d.ts +2 -2
- package/script/src/providers/anthropic.d.ts.map +1 -1
- package/script/src/providers/anthropic.js +9 -1
- package/script/src/providers/gemini.d.ts +2 -2
- package/script/src/providers/gemini.d.ts.map +1 -1
- package/script/src/providers/gemini.js +10 -2
- package/script/src/providers/openai.d.ts +2 -2
- package/script/src/providers/openai.d.ts.map +1 -1
- package/script/src/providers/openai.js +9 -1
- package/script/src/translator.d.ts +14 -7
- package/script/src/translator.d.ts.map +1 -1
- package/script/src/translator.js +168 -83
- package/script/src/types.d.ts +31 -3
- package/script/src/types.d.ts.map +1 -1
- package/script/src/utilites.d.ts +3 -4
- package/script/src/utilites.d.ts.map +1 -1
- package/script/src/utilites.js +43 -20
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.loadConfig = loadConfig;
|
|
37
|
+
exports.applyConfig = applyConfig;
|
|
38
|
+
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
39
|
+
const mod_js_1 = require("../deps/jsr.io/@std/path/1.1.4/mod.js");
|
|
40
|
+
const contants_js_1 = require("./contants.js");
|
|
41
|
+
async function readConfigFile(path) {
|
|
42
|
+
let raw;
|
|
43
|
+
try {
|
|
44
|
+
raw = await dntShim.Deno.readTextFile(path);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof dntShim.Deno.errors.NotFound) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
if (raw.trim() === '') {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
const parsed = JSON.parse(raw);
|
|
56
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
57
|
+
throw new Error(`Config file must contain a JSON object: ${path}`);
|
|
58
|
+
}
|
|
59
|
+
return parsed;
|
|
60
|
+
}
|
|
61
|
+
async function loadConfig(explicitPath) {
|
|
62
|
+
if (explicitPath) {
|
|
63
|
+
const config = await readConfigFile(explicitPath);
|
|
64
|
+
if (config === null) {
|
|
65
|
+
throw new Error(`Config file not found: ${explicitPath}`);
|
|
66
|
+
}
|
|
67
|
+
return config;
|
|
68
|
+
}
|
|
69
|
+
const defaultPath = (0, mod_js_1.join)(dntShim.Deno.cwd(), contants_js_1.CONFIG_FILE_NAME);
|
|
70
|
+
const config = await readConfigFile(defaultPath);
|
|
71
|
+
if (config === null) {
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
return config;
|
|
75
|
+
}
|
|
76
|
+
function joinList(value) {
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
if (Array.isArray(value)) {
|
|
81
|
+
return value.join(',');
|
|
82
|
+
}
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
function rawArgsHas(rawArgs, flag) {
|
|
86
|
+
const long = `--${flag}`;
|
|
87
|
+
const longEq = `--${flag}=`;
|
|
88
|
+
return rawArgs.some((a) => a === long || a.startsWith(longEq));
|
|
89
|
+
}
|
|
90
|
+
function applyConfig(cli, config, rawArgs) {
|
|
91
|
+
const merged = { ...cli };
|
|
92
|
+
if (merged.key === undefined && config.key !== undefined) {
|
|
93
|
+
merged.key = config.key;
|
|
94
|
+
}
|
|
95
|
+
if (merged.provider === undefined && config.provider !== undefined) {
|
|
96
|
+
merged.provider = config.provider;
|
|
97
|
+
}
|
|
98
|
+
if (merged.model === undefined && config.model !== undefined) {
|
|
99
|
+
merged.model = config.model;
|
|
100
|
+
}
|
|
101
|
+
if (merged.input === undefined && config.input !== undefined) {
|
|
102
|
+
merged.input = config.input;
|
|
103
|
+
}
|
|
104
|
+
if (merged.output === undefined && config.output !== undefined) {
|
|
105
|
+
merged.output = joinList(config.output);
|
|
106
|
+
}
|
|
107
|
+
if (merged.from === undefined && config.from !== undefined) {
|
|
108
|
+
merged.from = config.from;
|
|
109
|
+
}
|
|
110
|
+
if (merged.to === undefined && config.to !== undefined) {
|
|
111
|
+
merged.to = joinList(config.to);
|
|
112
|
+
}
|
|
113
|
+
if (merged.url === undefined && config.url !== undefined) {
|
|
114
|
+
merged.url = config.url;
|
|
115
|
+
}
|
|
116
|
+
if (merged['max-batch-size'] === undefined && config.maxBatchSize !== undefined) {
|
|
117
|
+
merged['max-batch-size'] = String(config.maxBatchSize);
|
|
118
|
+
}
|
|
119
|
+
if (!rawArgsHas(rawArgs, 'no-limit') && config.noLimit !== undefined) {
|
|
120
|
+
merged['no-limit'] = config.noLimit;
|
|
121
|
+
}
|
|
122
|
+
if (!rawArgsHas(rawArgs, 'no-timeout') && config.noTimeout !== undefined) {
|
|
123
|
+
merged['no-timeout'] = config.noTimeout;
|
|
124
|
+
}
|
|
125
|
+
if (!rawArgsHas(rawArgs, 'stats') && config.stats !== undefined) {
|
|
126
|
+
merged.stats = config.stats;
|
|
127
|
+
}
|
|
128
|
+
if (!rawArgsHas(rawArgs, 'incremental') && config.incremental !== undefined) {
|
|
129
|
+
merged.incremental = config.incremental;
|
|
130
|
+
}
|
|
131
|
+
return merged;
|
|
132
|
+
}
|
package/script/src/contants.d.ts
CHANGED
|
@@ -5,5 +5,9 @@ export declare const DEFAULT_MAX_BATCH_BYTES: 12000;
|
|
|
5
5
|
export declare const MAX_RETRIES: 3;
|
|
6
6
|
export declare const BASE_RETRY_DELAY_MS: 2000;
|
|
7
7
|
export declare const INTER_BATCH_DELAY_MS: 1500;
|
|
8
|
+
export declare const INTER_LEAF_DELAY_MS: 200;
|
|
9
|
+
export declare const PER_LEAF_FALLBACK_RATIO: 0.5;
|
|
10
|
+
export declare const GITHUB_REPO_URL: "https://github.com/ibodev1/glotto";
|
|
11
|
+
export declare const CONFIG_FILE_NAME: "glotto.config.json";
|
|
8
12
|
export declare const HELP_TEXT: string;
|
|
9
13
|
//# sourceMappingURL=contants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contants.d.ts","sourceRoot":"","sources":["../../src/src/contants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,eAAO,MAAM,gBAAgB,EAAE,QAAmB,CAAC;AAEnD,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAInD,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAG,KAAe,CAAC;AAEvD,eAAO,MAAM,WAAW,EAAG,CAAU,CAAC;AAEtC,eAAO,MAAM,mBAAmB,EAAG,IAAc,CAAC;AAElD,eAAO,MAAM,oBAAoB,EAAG,IAAc,CAAC;AAEnD,eAAO,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"contants.d.ts","sourceRoot":"","sources":["../../src/src/contants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,eAAO,MAAM,gBAAgB,EAAE,QAAmB,CAAC;AAEnD,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAInD,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAG,KAAe,CAAC;AAEvD,eAAO,MAAM,WAAW,EAAG,CAAU,CAAC;AAEtC,eAAO,MAAM,mBAAmB,EAAG,IAAc,CAAC;AAElD,eAAO,MAAM,oBAAoB,EAAG,IAAc,CAAC;AAEnD,eAAO,MAAM,mBAAmB,EAAG,GAAY,CAAC;AAEhD,eAAO,MAAM,uBAAuB,EAAG,GAAY,CAAC;AAEpD,eAAO,MAAM,eAAe,EAAG,mCAA4C,CAAC;AAE5E,eAAO,MAAM,gBAAgB,EAAG,oBAA6B,CAAC;AAE9D,eAAO,MAAM,SAAS,QAqCrB,CAAC"}
|
package/script/src/contants.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HELP_TEXT = exports.INTER_BATCH_DELAY_MS = exports.BASE_RETRY_DELAY_MS = exports.MAX_RETRIES = exports.DEFAULT_MAX_BATCH_BYTES = exports.DEFAULT_MODELS = exports.DEFAULT_PROVIDER = void 0;
|
|
4
|
-
exports.DEFAULT_PROVIDER = '
|
|
3
|
+
exports.HELP_TEXT = exports.CONFIG_FILE_NAME = exports.GITHUB_REPO_URL = exports.PER_LEAF_FALLBACK_RATIO = exports.INTER_LEAF_DELAY_MS = exports.INTER_BATCH_DELAY_MS = exports.BASE_RETRY_DELAY_MS = exports.MAX_RETRIES = exports.DEFAULT_MAX_BATCH_BYTES = exports.DEFAULT_MODELS = exports.DEFAULT_PROVIDER = void 0;
|
|
4
|
+
exports.DEFAULT_PROVIDER = 'openai';
|
|
5
5
|
exports.DEFAULT_MODELS = {
|
|
6
6
|
gemini: 'gemini-2.5-flash',
|
|
7
7
|
openai: 'gpt-4.1-mini',
|
|
@@ -11,6 +11,10 @@ exports.DEFAULT_MAX_BATCH_BYTES = 12_000;
|
|
|
11
11
|
exports.MAX_RETRIES = 3;
|
|
12
12
|
exports.BASE_RETRY_DELAY_MS = 2_000;
|
|
13
13
|
exports.INTER_BATCH_DELAY_MS = 1_500;
|
|
14
|
+
exports.INTER_LEAF_DELAY_MS = 200;
|
|
15
|
+
exports.PER_LEAF_FALLBACK_RATIO = 0.5;
|
|
16
|
+
exports.GITHUB_REPO_URL = 'https://github.com/ibodev1/glotto';
|
|
17
|
+
exports.CONFIG_FILE_NAME = 'glotto.config.json';
|
|
14
18
|
exports.HELP_TEXT = `
|
|
15
19
|
Glotto AI Translator
|
|
16
20
|
-------------------
|
|
@@ -26,10 +30,13 @@ Options:
|
|
|
26
30
|
-p, --provider AI translation provider to use (default: ${exports.DEFAULT_PROVIDER})
|
|
27
31
|
-m, --model Model name for the selected provider (optional)
|
|
28
32
|
-i, --input Path to source JSON file (required)
|
|
29
|
-
-o, --output
|
|
33
|
+
-o, --output Target JSON file path. Comma-separated for multi-target (required)
|
|
30
34
|
-f, --from Source language (required)
|
|
31
|
-
-t, --to Target language (required)
|
|
35
|
+
-t, --to Target language. Comma-separated for multi-target (required)
|
|
32
36
|
--url Custom base URL for OpenAI/Anthropic (optional)
|
|
37
|
+
--config Path to a glotto.config.json (default: ./glotto.config.json if present)
|
|
38
|
+
--stats Print AI usage stats (input/output tokens, calls) at the end
|
|
39
|
+
--incremental Translate only missing/empty keys when the target file already exists
|
|
33
40
|
--no-limit Disable rate limit delay between batches
|
|
34
41
|
--no-timeout Disable request timeout (wait indefinitely for AI response)
|
|
35
42
|
--max-batch-size Maximum source size per batch, in KB (default: ${exports.DEFAULT_MAX_BATCH_BYTES / 1024} KB)
|
|
@@ -39,8 +46,10 @@ Options:
|
|
|
39
46
|
Examples:
|
|
40
47
|
glotto --key {{key}} --input=en.json --output=tr.json --from=english --to=turkish
|
|
41
48
|
glotto --key {{key}} -i en.json -o tr.json -f english -t turkish -p gemini
|
|
42
|
-
glotto --key {{key}} -i en.json -
|
|
43
|
-
glotto --key {{key}} -i en.json -o tr.json -f english -t turkish
|
|
49
|
+
glotto --key {{key}} -i en.json -f english -t "turkish,french" -o "tr.json,fr.json"
|
|
50
|
+
glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --incremental
|
|
51
|
+
glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --stats
|
|
52
|
+
glotto --key {{key}} --config ./glotto.config.json
|
|
44
53
|
glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --no-limit --no-timeout
|
|
45
54
|
glotto --key {{key}} -i en.json -o tr.json -f english -t turkish --max-batch-size 8
|
|
46
55
|
`;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { JsonValue, Leaf } from './types.js';
|
|
2
|
+
export declare function findMissingLeaves(sourceLeaves: Leaf[], existingTarget: JsonValue): Leaf[];
|
|
3
|
+
export declare function mergeTranslations(existingTarget: JsonValue, sourceLeaves: Leaf[], translations: Map<number, string>): JsonValue;
|
|
4
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../src/src/diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAQ,MAAM,YAAY,CAAC;AAyBxD,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,SAAS,GAAG,IAAI,EAAE,CAYzF;AASD,wBAAgB,iBAAiB,CAC/B,cAAc,EAAE,SAAS,EACzB,YAAY,EAAE,IAAI,EAAE,EACpB,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAChC,SAAS,CAYX"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findMissingLeaves = findMissingLeaves;
|
|
4
|
+
exports.mergeTranslations = mergeTranslations;
|
|
5
|
+
const translator_js_1 = require("./translator.js");
|
|
6
|
+
function getPath(root, path) {
|
|
7
|
+
let node = root;
|
|
8
|
+
for (const key of path) {
|
|
9
|
+
if (node === null || typeof node !== 'object') {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const container = node;
|
|
13
|
+
if (!(key in container)) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
node = container[key];
|
|
17
|
+
}
|
|
18
|
+
return node;
|
|
19
|
+
}
|
|
20
|
+
function isPresentTranslation(value) {
|
|
21
|
+
if (typeof value !== 'string') {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return value.trim().length > 0;
|
|
25
|
+
}
|
|
26
|
+
function findMissingLeaves(sourceLeaves, existingTarget) {
|
|
27
|
+
const missing = [];
|
|
28
|
+
for (const leaf of sourceLeaves) {
|
|
29
|
+
if (!leaf.translatable) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const existingValue = getPath(existingTarget, leaf.path);
|
|
33
|
+
if (!isPresentTranslation(existingValue)) {
|
|
34
|
+
missing.push(leaf);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return missing;
|
|
38
|
+
}
|
|
39
|
+
function deepClone(value) {
|
|
40
|
+
if (value === null || typeof value !== 'object') {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
return JSON.parse(JSON.stringify(value));
|
|
44
|
+
}
|
|
45
|
+
function mergeTranslations(existingTarget, sourceLeaves, translations) {
|
|
46
|
+
const clone = deepClone(existingTarget);
|
|
47
|
+
for (const leaf of sourceLeaves) {
|
|
48
|
+
if (!translations.has(leaf.id)) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (leaf.path.length === 0) {
|
|
52
|
+
return translations.get(leaf.id);
|
|
53
|
+
}
|
|
54
|
+
(0, translator_js_1.setPath)(clone, leaf.path, translations.get(leaf.id));
|
|
55
|
+
}
|
|
56
|
+
return clone;
|
|
57
|
+
}
|
package/script/src/file.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { JsonValue } from './types.js';
|
|
2
|
-
export declare
|
|
3
|
-
export declare
|
|
4
|
-
export declare
|
|
2
|
+
export declare function resolvePath(...paths: string[]): string;
|
|
3
|
+
export declare function getImportJson<T = JsonValue>(input: string): Promise<T>;
|
|
4
|
+
export declare function writeOutput(outputPath: string, content: string): Promise<void>;
|
|
5
|
+
export declare function readJsonIfExists<T = JsonValue>(path: string): Promise<T | null>;
|
|
5
6
|
//# sourceMappingURL=file.d.ts.map
|
package/script/src/file.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/src/file.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,
|
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/src/file.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,wBAAgB,WAAW,CAAC,GAAG,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAEtD;AAED,wBAAsB,aAAa,CAAC,CAAC,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAO5E;AAED,wBAAsB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEpF;AAED,wBAAsB,gBAAgB,CAAC,CAAC,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAcrF"}
|
package/script/src/file.js
CHANGED
|
@@ -33,21 +33,39 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.
|
|
36
|
+
exports.resolvePath = resolvePath;
|
|
37
|
+
exports.getImportJson = getImportJson;
|
|
38
|
+
exports.writeOutput = writeOutput;
|
|
39
|
+
exports.readJsonIfExists = readJsonIfExists;
|
|
37
40
|
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
38
41
|
const mod_js_1 = require("../deps/jsr.io/@std/path/1.1.4/mod.js");
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
function resolvePath(...paths) {
|
|
43
|
+
return (0, mod_js_1.join)(dntShim.Deno.cwd(), ...paths);
|
|
44
|
+
}
|
|
45
|
+
async function getImportJson(input) {
|
|
46
|
+
const filePath = resolvePath(input);
|
|
43
47
|
const fileContent = await dntShim.Deno.readTextFile(filePath);
|
|
44
48
|
if (typeof fileContent !== 'string' || fileContent.trim() === '') {
|
|
45
49
|
throw new Error('No Content!');
|
|
46
50
|
}
|
|
47
51
|
return JSON.parse(fileContent);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const writeOutput = async (outputPath, content) => {
|
|
52
|
+
}
|
|
53
|
+
async function writeOutput(outputPath, content) {
|
|
51
54
|
await dntShim.Deno.writeTextFile(outputPath, content, { create: true });
|
|
52
|
-
}
|
|
53
|
-
|
|
55
|
+
}
|
|
56
|
+
async function readJsonIfExists(path) {
|
|
57
|
+
let raw;
|
|
58
|
+
try {
|
|
59
|
+
raw = await dntShim.Deno.readTextFile(path);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error instanceof dntShim.Deno.errors.NotFound) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
if (raw.trim() === '') {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return JSON.parse(raw);
|
|
71
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { TextTranslator, TranslateOptions } from '../types.js';
|
|
1
|
+
import type { TextTranslator, TranslateOptions, TranslateResult } from '../types.js';
|
|
2
2
|
declare class AnthropicModel implements TextTranslator {
|
|
3
3
|
private client;
|
|
4
4
|
private model;
|
|
5
5
|
constructor(key: string, baseUrl?: string, modelName?: string, options?: TranslateOptions);
|
|
6
|
-
translate(prompt: string): Promise<
|
|
6
|
+
translate(prompt: string): Promise<TranslateResult>;
|
|
7
7
|
}
|
|
8
8
|
export default AnthropicModel;
|
|
9
9
|
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/src/providers/anthropic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,aAAa,CAAC;AAGrG,cAAM,cAAe,YAAW,cAAc;IAC5C,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,KAAK,CAAS;gBAGpB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,GAAE,gBAAuD;IAU5D,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAoB1D;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -33,10 +33,18 @@ class AnthropicModel {
|
|
|
33
33
|
temperature: 0.2,
|
|
34
34
|
messages: [{ role: 'user', content: prompt }],
|
|
35
35
|
});
|
|
36
|
-
|
|
36
|
+
const text = message.content
|
|
37
37
|
.filter((part) => part.type === 'text')
|
|
38
38
|
.map((part) => part.text)
|
|
39
39
|
.join('');
|
|
40
|
+
let usage;
|
|
41
|
+
if (message.usage) {
|
|
42
|
+
usage = {
|
|
43
|
+
inputTokens: message.usage.input_tokens ?? 0,
|
|
44
|
+
outputTokens: message.usage.output_tokens ?? 0,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return { text, usage };
|
|
40
48
|
}
|
|
41
49
|
}
|
|
42
50
|
exports.default = AnthropicModel;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { TextTranslator, TranslateOptions } from '../types.js';
|
|
1
|
+
import type { TextTranslator, TranslateOptions, TranslateResult } from '../types.js';
|
|
2
2
|
declare class Gemini implements TextTranslator {
|
|
3
3
|
private genAI;
|
|
4
4
|
private model;
|
|
5
5
|
constructor(key: string, modelName?: string, options?: TranslateOptions);
|
|
6
|
-
translate(prompt: string): Promise<
|
|
6
|
+
translate(prompt: string): Promise<TranslateResult>;
|
|
7
7
|
}
|
|
8
8
|
export default Gemini;
|
|
9
9
|
//# sourceMappingURL=gemini.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gemini.d.ts","sourceRoot":"","sources":["../../../src/src/providers/gemini.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"gemini.d.ts","sourceRoot":"","sources":["../../../src/src/providers/gemini.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,aAAa,CAAC;AAGrG,cAAM,MAAO,YAAW,cAAc;IACpC,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,KAAK,CAAS;gBAGpB,GAAG,EAAE,MAAM,EACX,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,GAAE,gBAAuD;IAS5D,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAmB1D;AAED,eAAe,MAAM,CAAC"}
|
|
@@ -23,7 +23,7 @@ class Gemini {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
async translate(prompt) {
|
|
26
|
-
const
|
|
26
|
+
const response = await this.genAI.models.generateContent({
|
|
27
27
|
model: this.model,
|
|
28
28
|
contents: prompt,
|
|
29
29
|
config: {
|
|
@@ -31,7 +31,15 @@ class Gemini {
|
|
|
31
31
|
temperature: 0.2,
|
|
32
32
|
},
|
|
33
33
|
});
|
|
34
|
-
|
|
34
|
+
const text = response.text ?? '';
|
|
35
|
+
let usage;
|
|
36
|
+
if (response.usageMetadata) {
|
|
37
|
+
usage = {
|
|
38
|
+
inputTokens: response.usageMetadata.promptTokenCount ?? 0,
|
|
39
|
+
outputTokens: response.usageMetadata.candidatesTokenCount ?? 0,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return { text, usage };
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
45
|
exports.default = Gemini;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { TextTranslator, TranslateOptions } from '../types.js';
|
|
1
|
+
import type { TextTranslator, TranslateOptions, TranslateResult } from '../types.js';
|
|
2
2
|
declare class OpenAIModel implements TextTranslator {
|
|
3
3
|
private client;
|
|
4
4
|
private model;
|
|
5
5
|
constructor(key: string, baseUrl?: string, modelName?: string, options?: TranslateOptions);
|
|
6
|
-
translate(prompt: string): Promise<
|
|
6
|
+
translate(prompt: string): Promise<TranslateResult>;
|
|
7
7
|
}
|
|
8
8
|
export default OpenAIModel;
|
|
9
9
|
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/src/providers/openai.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/src/providers/openai.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAkB,MAAM,aAAa,CAAC;AAGrG,cAAM,WAAY,YAAW,cAAc;IACzC,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAS;gBAGpB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,GAAE,gBAAuD;IAU5D,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAgB1D;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -32,7 +32,15 @@ class OpenAIModel {
|
|
|
32
32
|
messages: [{ role: 'user', content: prompt }],
|
|
33
33
|
temperature: 0.2,
|
|
34
34
|
});
|
|
35
|
-
|
|
35
|
+
const text = completion.choices[0]?.message?.content ?? '';
|
|
36
|
+
let usage;
|
|
37
|
+
if (completion.usage) {
|
|
38
|
+
usage = {
|
|
39
|
+
inputTokens: completion.usage.prompt_tokens ?? 0,
|
|
40
|
+
outputTokens: completion.usage.completion_tokens ?? 0,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return { text, usage };
|
|
36
44
|
}
|
|
37
45
|
}
|
|
38
46
|
exports.default = OpenAIModel;
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import type { Batch, JsonValue, Leaf, TextTranslator, TranslateOptions } from './types.js';
|
|
2
|
-
export declare
|
|
3
|
-
export declare
|
|
4
|
-
export declare
|
|
5
|
-
export declare
|
|
6
|
-
export declare
|
|
7
|
-
export declare
|
|
1
|
+
import type { Batch, JsonValue, Leaf, Path, TextTranslator, TranslateOptions, TranslateUsage } from './types.js';
|
|
2
|
+
export declare function extractLeaves(data: JsonValue): Leaf[];
|
|
3
|
+
export declare function groupIntoBatches(leaves: Leaf[], maxBytes: number): Batch[];
|
|
4
|
+
export declare function buildBatchPrompt(from: string, to: string, leaves: Leaf[]): string;
|
|
5
|
+
export declare function buildSinglePrompt(from: string, to: string, value: string): string;
|
|
6
|
+
export declare function decodeResponse(text: string): Map<number, string>;
|
|
7
|
+
export declare function setPath(root: JsonValue, path: Path, value: JsonValue): void;
|
|
8
|
+
export declare function reconstruct(allLeaves: Leaf[], translations: Map<number, string>): JsonValue;
|
|
9
|
+
export type RunBatchesResult = {
|
|
10
|
+
translations: Map<number, string>;
|
|
11
|
+
usage: TranslateUsage;
|
|
12
|
+
calls: number;
|
|
13
|
+
};
|
|
14
|
+
export declare function runBatches(batches: Batch[], translator: TextTranslator, from: string, to: string, options: TranslateOptions): Promise<RunBatchesResult>;
|
|
8
15
|
//# sourceMappingURL=translator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translator.d.ts","sourceRoot":"","sources":["../../src/src/translator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,
|
|
1
|
+
{"version":3,"file":"translator.d.ts","sourceRoot":"","sources":["../../src/src/translator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAWjH,wBAAgB,aAAa,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,EAAE,CAmCrD;AAgBD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,KAAK,EAAE,CA0B1E;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAkBjF;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAMjF;AAQD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAWhE;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAY3E;AAED,wBAAgB,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAe3F;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,KAAK,EAAE,cAAc,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AA6IF,wBAAsB,UAAU,CAC9B,OAAO,EAAE,KAAK,EAAE,EAChB,UAAU,EAAE,cAAc,EAC1B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,gBAAgB,CAAC,CAwC3B"}
|