@rocapine/community 0.21.4 β 0.21.7
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/package.json
CHANGED
|
@@ -123,12 +123,88 @@ export function translationTimeoutMs(contentLength: number, targetCount: number)
|
|
|
123
123
|
return Math.min(90_000, 20_000 + 5 * contentLength * Math.max(1, targetCount));
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
/**
|
|
126
|
+
/** Output characters one call may produce (text length Γ targets): keeps a call
|
|
127
|
+
* inside translationTimeoutMs, whose cap a 2,000-character post in 19 locales
|
|
128
|
+
* overran on every try. */
|
|
129
|
+
const OUTPUT_CHARS_PER_CALL = 12_000;
|
|
130
|
+
|
|
131
|
+
/** Splits the targets into groups small enough for one call each. */
|
|
132
|
+
export function targetChunks(contentLength: number, targets: string[]): string[][] {
|
|
133
|
+
const size = Math.max(1, Math.floor(OUTPUT_CHARS_PER_CALL / Math.max(1, contentLength)));
|
|
134
|
+
const chunks: string[][] = [];
|
|
135
|
+
for (let i = 0; i < targets.length; i += size) chunks.push(targets.slice(i, i + size));
|
|
136
|
+
return chunks;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Folds compatibility characters (math-styled letters, full-width forms) to plain text. */
|
|
140
|
+
export function normalizeForTranslation(text: string): string {
|
|
141
|
+
return text.normalize("NFKC");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Translates into every target, in parallel calls of at most OUTPUT_CHARS_PER_CALL
|
|
145
|
+
* each. Returns null when any call fails (caller writes nothing). */
|
|
127
146
|
export async function translateItem(input: {
|
|
128
147
|
content: string;
|
|
129
148
|
options: string[];
|
|
130
149
|
targets: string[];
|
|
131
150
|
}): Promise<ParsedTranslation | null> {
|
|
151
|
+
if (input.targets.length === 0) return { sourceLocale: "", translations: {} };
|
|
152
|
+
// Styled Unicode (ππ¨π₯π, ππππβ¦) costs many tokens per letter and comes back half
|
|
153
|
+
// untranslated; NFKC folds it to plain letters.
|
|
154
|
+
input = {
|
|
155
|
+
...input,
|
|
156
|
+
content: normalizeForTranslation(input.content),
|
|
157
|
+
options: input.options.map(normalizeForTranslation),
|
|
158
|
+
};
|
|
159
|
+
// Poll labels are translated too, so they count toward each call's output.
|
|
160
|
+
const length = input.content.length + input.options.reduce((n, o) => n + o.length, 0);
|
|
161
|
+
return mergeParts(
|
|
162
|
+
await Promise.all(
|
|
163
|
+
targetChunks(length, input.targets).map((targets) =>
|
|
164
|
+
translateTargets({ ...input, targets }, translateChunk),
|
|
165
|
+
),
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** One result from parallel calls; null when any of them failed. */
|
|
171
|
+
function mergeParts(parts: (ParsedTranslation | null)[]): ParsedTranslation | null {
|
|
172
|
+
if (parts.some((p) => p === null)) return null;
|
|
173
|
+
const done = parts as ParsedTranslation[];
|
|
174
|
+
return {
|
|
175
|
+
sourceLocale: done[0].sourceLocale,
|
|
176
|
+
translations: Object.assign({}, ...done.map((p) => p.translations)),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** translateChunk's answer when OpenAI's content filter cut the response. */
|
|
181
|
+
export const FILTERED = "filtered";
|
|
182
|
+
type ChunkInput = { content: string; options: string[]; targets: string[] };
|
|
183
|
+
type ChunkCall = (input: ChunkInput) => Promise<ParsedTranslation | null | typeof FILTERED>;
|
|
184
|
+
|
|
185
|
+
/** The content filter trips at random on some locales (the Lord's Prayer in
|
|
186
|
+
* Slovak: about 1 call in 3), and one tripped locale used to sink every target
|
|
187
|
+
* of the call. A filtered call is split into one call per locale, and a single
|
|
188
|
+
* locale gets a second try. Exported for tests (`call` is injectable). */
|
|
189
|
+
export async function translateTargets(
|
|
190
|
+
input: ChunkInput,
|
|
191
|
+
call: ChunkCall,
|
|
192
|
+
): Promise<ParsedTranslation | null> {
|
|
193
|
+
const first = await call(input);
|
|
194
|
+
if (first !== FILTERED) return first;
|
|
195
|
+
if (input.targets.length === 1) {
|
|
196
|
+
const again = await call(input);
|
|
197
|
+
return again === FILTERED ? null : again;
|
|
198
|
+
}
|
|
199
|
+
return mergeParts(
|
|
200
|
+
await Promise.all(input.targets.map((t) => translateTargets({ ...input, targets: [t] }, call))),
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** One OpenAI call. Returns FILTERED when the content filter cut it, null on any other failure. */
|
|
205
|
+
async function translateChunk(
|
|
206
|
+
input: ChunkInput,
|
|
207
|
+
): Promise<ParsedTranslation | null | typeof FILTERED> {
|
|
132
208
|
const perLocale: Record<string, unknown> = {};
|
|
133
209
|
for (const l of input.targets) {
|
|
134
210
|
perLocale[l] = {
|
|
@@ -197,6 +273,16 @@ export async function translateItem(input: {
|
|
|
197
273
|
return null;
|
|
198
274
|
}
|
|
199
275
|
const data = await res.json();
|
|
276
|
+
if (data.status !== "completed") {
|
|
277
|
+
// A cut-off response (max_output_tokens, content_filterβ¦) is not parseable JSON.
|
|
278
|
+
console.error(
|
|
279
|
+
"translation incomplete",
|
|
280
|
+
data.status,
|
|
281
|
+
JSON.stringify(data.incomplete_details),
|
|
282
|
+
JSON.stringify(data.usage),
|
|
283
|
+
);
|
|
284
|
+
return data.incomplete_details?.reason === "content_filter" ? FILTERED : null;
|
|
285
|
+
}
|
|
200
286
|
// Responses API: the JSON text lives in output[].content[].text, on "message" items only
|
|
201
287
|
// ("reasoning" items have no output_text content and must never be picked). output_text is
|
|
202
288
|
// an SDK-side convenience that raw REST responses do not carry, so the fallback below is the
|