@rocapine/community 0.21.6 β 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
|
@@ -136,6 +136,11 @@ export function targetChunks(contentLength: number, targets: string[]): string[]
|
|
|
136
136
|
return chunks;
|
|
137
137
|
}
|
|
138
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
|
+
|
|
139
144
|
/** Translates into every target, in parallel calls of at most OUTPUT_CHARS_PER_CALL
|
|
140
145
|
* each. Returns null when any call fails (caller writes nothing). */
|
|
141
146
|
export async function translateItem(input: {
|
|
@@ -144,11 +149,26 @@ export async function translateItem(input: {
|
|
|
144
149
|
targets: string[];
|
|
145
150
|
}): Promise<ParsedTranslation | null> {
|
|
146
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
|
+
};
|
|
147
159
|
// Poll labels are translated too, so they count toward each call's output.
|
|
148
160
|
const length = input.content.length + input.options.reduce((n, o) => n + o.length, 0);
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
return mergeParts(
|
|
162
|
+
await Promise.all(
|
|
163
|
+
targetChunks(length, input.targets).map((targets) =>
|
|
164
|
+
translateTargets({ ...input, targets }, translateChunk),
|
|
165
|
+
),
|
|
166
|
+
),
|
|
151
167
|
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** One result from parallel calls; null when any of them failed. */
|
|
171
|
+
function mergeParts(parts: (ParsedTranslation | null)[]): ParsedTranslation | null {
|
|
152
172
|
if (parts.some((p) => p === null)) return null;
|
|
153
173
|
const done = parts as ParsedTranslation[];
|
|
154
174
|
return {
|
|
@@ -157,12 +177,34 @@ export async function translateItem(input: {
|
|
|
157
177
|
};
|
|
158
178
|
}
|
|
159
179
|
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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> {
|
|
166
208
|
const perLocale: Record<string, unknown> = {};
|
|
167
209
|
for (const l of input.targets) {
|
|
168
210
|
perLocale[l] = {
|
|
@@ -231,6 +273,16 @@ async function translateChunk(input: {
|
|
|
231
273
|
return null;
|
|
232
274
|
}
|
|
233
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
|
+
}
|
|
234
286
|
// Responses API: the JSON text lives in output[].content[].text, on "message" items only
|
|
235
287
|
// ("reasoning" items have no output_text content and must never be picked). output_text is
|
|
236
288
|
// an SDK-side convenience that raw REST responses do not carry, so the fallback below is the
|