@rocapine/community 0.21.4 → 0.21.6

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/community",
3
- "version": "0.21.4",
3
+ "version": "0.21.6",
4
4
  "description": "CLI to install, upgrade and adopt the Rocapine Community SDK's Supabase backend (migrations, Edge Functions) into a project.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -123,11 +123,45 @@ 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
- /** One OpenAI call. Returns null on any failure (caller writes nothing). */
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
+ /** Translates into every target, in parallel calls of at most OUTPUT_CHARS_PER_CALL
140
+ * each. Returns null when any call fails (caller writes nothing). */
127
141
  export async function translateItem(input: {
128
142
  content: string;
129
143
  options: string[];
130
144
  targets: string[];
145
+ }): Promise<ParsedTranslation | null> {
146
+ if (input.targets.length === 0) return { sourceLocale: "", translations: {} };
147
+ // Poll labels are translated too, so they count toward each call's output.
148
+ const length = input.content.length + input.options.reduce((n, o) => n + o.length, 0);
149
+ const parts = await Promise.all(
150
+ targetChunks(length, input.targets).map((targets) => translateChunk({ ...input, targets })),
151
+ );
152
+ if (parts.some((p) => p === null)) return null;
153
+ const done = parts as ParsedTranslation[];
154
+ return {
155
+ sourceLocale: done[0].sourceLocale,
156
+ translations: Object.assign({}, ...done.map((p) => p.translations)),
157
+ };
158
+ }
159
+
160
+ /** One OpenAI call. Returns null on any failure. */
161
+ async function translateChunk(input: {
162
+ content: string;
163
+ options: string[];
164
+ targets: string[];
131
165
  }): Promise<ParsedTranslation | null> {
132
166
  const perLocale: Record<string, unknown> = {};
133
167
  for (const l of input.targets) {