@lyxa.ai/core 1.4.291 → 1.4.292

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.
@@ -4,6 +4,14 @@ export interface IAICompletionProvider {
4
4
  pollIntervalMs?: number;
5
5
  maxWaitMs?: number;
6
6
  }): Promise<TextVariantResult[]>;
7
+ generateSearchVariantsBatchOptimized(items: ItemTextInput[], model?: string, instructions?: string, options?: {
8
+ chunkSize?: number;
9
+ maxArabic?: number;
10
+ maxArabizi?: number;
11
+ maxTransliteration?: number;
12
+ pollIntervalMs?: number;
13
+ maxWaitMs?: number;
14
+ }): Promise<TextVariantResult[]>;
7
15
  }
8
16
  export interface TextVariantResult {
9
17
  itemId: string;
@@ -1 +1 @@
1
- {"version":3,"file":"common-llm-interfaces.js","sourceRoot":"/","sources":["libraries/llm/interfaces/common-llm-interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import { LLMProvider } from '../llm-provider-service';\n\nexport interface IAICompletionProvider {\n\tgenerateSearchVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]>;\n}\n\n// export interface TextVariantConfig {\n// \tisArabic?: boolean; // Arabic translation, Arabic script\n// \tisArabizi?: boolean; // Arabic word, Latin script (e.g. \"zaytoon\")\n// \tisArabicTransliteration?: boolean; // original word, spelled phonetically in Arabic script (e.g. \"أوليف\")\n// }\n\nexport interface TextVariantResult {\n\titemId: string;\n\toriginal: string;\n\tarabic?: string[];\n\tarabizi?: string[];\n\tarabicTransliteration?: string[];\n}\n\nexport interface AiConfiguration {\n\tprovider: LLMProvider;\n\tmodel?: string;\n\tinstructions?: string;\n\tprompt?: string;\n}\n\nexport interface ItemTextInput {\n\titemId: string;\n\ttext: string;\n}"]}
1
+ {"version":3,"file":"common-llm-interfaces.js","sourceRoot":"/","sources":["libraries/llm/interfaces/common-llm-interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import { LLMProvider } from '../llm-provider-service';\n\nexport interface IAICompletionProvider {\n\tgenerateSearchVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]>;\n\tgenerateSearchVariantsBatchOptimized(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: {\n\t\t\tchunkSize?: number;\n\t\t\tmaxArabic?: number;\n\t\t\tmaxArabizi?: number;\n\t\t\tmaxTransliteration?: number;\n\t\t\tpollIntervalMs?: number;\n\t\t\tmaxWaitMs?: number;\n\t\t}\n\t): Promise<TextVariantResult[]>;\n}\n\nexport interface TextVariantResult {\n\titemId: string;\n\toriginal: string;\n\tarabic?: string[];\n\tarabizi?: string[];\n\tarabicTransliteration?: string[];\n}\n\nexport interface AiConfiguration {\n\tprovider: LLMProvider;\n\tmodel?: string;\n\tinstructions?: string;\n\tprompt?: string;\n}\n\nexport interface ItemTextInput {\n\titemId: string;\n\ttext: string;\n}\n"]}
@@ -29,8 +29,8 @@ class LLMCompletionService {
29
29
  }
30
30
  async generateSearchVariants(payload, aiConfigurations) {
31
31
  const provider = await this.getProvider();
32
- console.log(`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-mini'}`);
33
- return provider.generateSearchVariantsBatch(payload, aiConfigurations.model, aiConfigurations.instructions);
32
+ console.log(`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-nano'}`);
33
+ return provider.generateSearchVariantsBatchOptimized(payload, aiConfigurations.model, aiConfigurations.instructions);
34
34
  }
35
35
  async getProvider() {
36
36
  if (!this.providerInstance) {
@@ -1 +1 @@
1
- {"version":3,"file":"llm-provider-service.js","sourceRoot":"/","sources":["libraries/llm/llm-provider-service.ts"],"names":[],"mappings":";;;AAAA,6BAAqC;AAOrC,+DAA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B,EAAE,CAAC;YAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC9C,IAAI,CAAC,OAAO,EAAE,CAAC;gBACd,MAAM,IAAA,8BAAe,EAAC,6BAA6B,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;YACrE,CAAC;YACD,IAAI,CAAC,gBAAgB,GAAG,MAAM,OAAO,EAAE,CAAC;QACzC,CAAC;QACD,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC9B,CAAC;CACD;AA3CD,oDA2CC","sourcesContent":["import { getLibraries } from '../..';\nimport {\n\tItemTextInput,\n\tAiConfiguration,\n\tTextVariantResult,\n\tIAICompletionProvider,\n} from './interfaces/common-llm-interfaces';\nimport { badRequestError } from '../../utilities/error-common';\nimport { OpenAICompletionProvider } from './providers/openai-provider';\n\nexport enum LLMProvider {\n\tOPENAI = 'openai',\n\tGEMINI = 'gemini',\n}\n\ntype ProviderFactory = () => Promise<IAICompletionProvider>;\n\nexport class LLMCompletionService {\n\tprivate providerInstance: IAICompletionProvider | null = null;\n\n\tprivate readonly factories: Record<LLMProvider, ProviderFactory> = {\n\t\t[LLMProvider.OPENAI]: async () => {\n\t\t\tconst apiKey = await getLibraries().getSecretsService().getKey('OPENAI_API_KEY');\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow badRequestError(`No API key found for provider ${LLMProvider.OPENAI}`);\n\t\t\t}\n\t\t\treturn new OpenAICompletionProvider(apiKey, 'gpt-5-mini');\n\t\t},\n\t\t[LLMProvider.GEMINI]: async () => {\n\t\t\tthrow badRequestError(`Provider ${LLMProvider.GEMINI} is not implemented yet`);\n\t\t},\n\t};\n\n\tconstructor(private readonly provider: LLMProvider) {}\n\n\tasync generateSearchVariants(\n\t\tpayload: ItemTextInput[],\n\t\taiConfigurations: AiConfiguration\n\t): Promise<TextVariantResult[]> {\n\t\tconst provider = await this.getProvider();\n\t\tconsole.log(\n\t\t\t`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-mini'}`\n\t\t);\n\t\treturn provider.generateSearchVariantsBatch(\n\t\t\tpayload,\n\t\t\taiConfigurations.model,\n\t\t\taiConfigurations.instructions\n\t\t);\n\t}\n\n\tprivate async getProvider(): Promise<IAICompletionProvider> {\n\t\tif (!this.providerInstance) {\n\t\t\tconst factory = this.factories[this.provider];\n\t\t\tif (!factory) {\n\t\t\t\tthrow badRequestError(`Unsupported LLM provider: ${this.provider}`);\n\t\t\t}\n\t\t\tthis.providerInstance = await factory();\n\t\t}\n\t\treturn this.providerInstance;\n\t}\n}"]}
1
+ {"version":3,"file":"llm-provider-service.js","sourceRoot":"/","sources":["libraries/llm/llm-provider-service.ts"],"names":[],"mappings":";;;AAAA,6BAAqC;AAOrC,+DAA+D;AAC/D,iEAAuE;AAEvE,IAAY,WAGX;AAHD,WAAY,WAAW;IACtB,gCAAiB,CAAA;IACjB,gCAAiB,CAAA;AAClB,CAAC,EAHW,WAAW,2BAAX,WAAW,QAGtB;AAID,MAAa,oBAAoB;IAgBH;IAfrB,gBAAgB,GAAiC,IAAI,CAAC;IAE7C,SAAS,GAAyC;QAClE,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,KAAK,IAAI,EAAE;YAChC,MAAM,MAAM,GAAG,MAAM,IAAA,gBAAY,GAAE,CAAC,iBAAiB,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;YACjF,IAAI,CAAC,MAAM,EAAE,CAAC;gBACb,MAAM,IAAA,8BAAe,EAAC,iCAAiC,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC;YAC9E,CAAC;YACD,OAAO,IAAI,0CAAwB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QAC3D,CAAC;QACD,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,KAAK,IAAI,EAAE;YAChC,MAAM,IAAA,8BAAe,EAAC,YAAY,WAAW,CAAC,MAAM,yBAAyB,CAAC,CAAC;QAChF,CAAC;KACD,CAAC;IAEF,YAA6B,QAAqB;QAArB,aAAQ,GAAR,QAAQ,CAAa;IAAG,CAAC;IAEtD,KAAK,CAAC,sBAAsB,CAC3B,OAAwB,EACxB,gBAAiC;QAEjC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAC1C,OAAO,CAAC,GAAG,CACV,+BAA+B,IAAI,CAAC,QAAQ,gBAAgB,gBAAgB,CAAC,KAAK,IAAI,YAAY,EAAE,CACpG,CAAC;QACF,OAAO,QAAQ,CAAC,oCAAoC,CACnD,OAAO,EACP,gBAAgB,CAAC,KAAK,EACtB,gBAAgB,CAAC,YAAY,CAC7B,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,WAAW;QACxB,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC9C,IAAI,CAAC,OAAO,EAAE,CAAC;gBACd,MAAM,IAAA,8BAAe,EAAC,6BAA6B,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;YACrE,CAAC;YACD,IAAI,CAAC,gBAAgB,GAAG,MAAM,OAAO,EAAE,CAAC;QACzC,CAAC;QACD,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC9B,CAAC;CACD;AA3CD,oDA2CC","sourcesContent":["import { getLibraries } from '../..';\nimport {\n\tItemTextInput,\n\tAiConfiguration,\n\tTextVariantResult,\n\tIAICompletionProvider,\n} from './interfaces/common-llm-interfaces';\nimport { badRequestError } from '../../utilities/error-common';\nimport { OpenAICompletionProvider } from './providers/openai-provider';\n\nexport enum LLMProvider {\n\tOPENAI = 'openai',\n\tGEMINI = 'gemini',\n}\n\ntype ProviderFactory = () => Promise<IAICompletionProvider>;\n\nexport class LLMCompletionService {\n\tprivate providerInstance: IAICompletionProvider | null = null;\n\n\tprivate readonly factories: Record<LLMProvider, ProviderFactory> = {\n\t\t[LLMProvider.OPENAI]: async () => {\n\t\t\tconst apiKey = await getLibraries().getSecretsService().getKey('OPENAI_API_KEY');\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow badRequestError(`No API key found for provider ${LLMProvider.OPENAI}`);\n\t\t\t}\n\t\t\treturn new OpenAICompletionProvider(apiKey, 'gpt-5-mini');\n\t\t},\n\t\t[LLMProvider.GEMINI]: async () => {\n\t\t\tthrow badRequestError(`Provider ${LLMProvider.GEMINI} is not implemented yet`);\n\t\t},\n\t};\n\n\tconstructor(private readonly provider: LLMProvider) {}\n\n\tasync generateSearchVariants(\n\t\tpayload: ItemTextInput[],\n\t\taiConfigurations: AiConfiguration\n\t): Promise<TextVariantResult[]> {\n\t\tconst provider = await this.getProvider();\n\t\tconsole.log(\n\t\t\t`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-nano'}`\n\t\t);\n\t\treturn provider.generateSearchVariantsBatchOptimized(\n\t\t\tpayload,\n\t\t\taiConfigurations.model,\n\t\t\taiConfigurations.instructions\n\t\t);\n\t}\n\n\tprivate async getProvider(): Promise<IAICompletionProvider> {\n\t\tif (!this.providerInstance) {\n\t\t\tconst factory = this.factories[this.provider];\n\t\t\tif (!factory) {\n\t\t\t\tthrow badRequestError(`Unsupported LLM provider: ${this.provider}`);\n\t\t\t}\n\t\t\tthis.providerInstance = await factory();\n\t\t}\n\t\treturn this.providerInstance;\n\t}\n}"]}
@@ -7,8 +7,19 @@ export declare class OpenAICompletionProvider implements IAICompletionProvider {
7
7
  pollIntervalMs?: number;
8
8
  maxWaitMs?: number;
9
9
  }): Promise<TextVariantResult[]>;
10
+ generateSearchVariantsBatchOptimized(items: ItemTextInput[], model?: string, instructions?: string, options?: {
11
+ chunkSize?: number;
12
+ maxArabic?: number;
13
+ maxArabizi?: number;
14
+ maxTransliteration?: number;
15
+ pollIntervalMs?: number;
16
+ maxWaitMs?: number;
17
+ }): Promise<TextVariantResult[]>;
10
18
  private buildVariantRequest;
19
+ private buildOptimizedInstructions;
20
+ private buildJsonSchema;
21
+ private buildOptimizedChunkSchema;
22
+ private chunkItems;
11
23
  private pollBatch;
12
24
  private readJsonlFile;
13
- private buildJsonSchema;
14
25
  }
@@ -75,10 +75,18 @@ class OpenAICompletionProvider {
75
75
  },
76
76
  }))
77
77
  .join('\n');
78
+ console.log({ jsonl });
78
79
  const inputFile = await this.client.files.create({
79
80
  file: await (0, openai_1.toFile)(Buffer.from(jsonl, 'utf-8'), 'batch-input.jsonl'),
80
81
  purpose: 'batch',
81
82
  });
83
+ console.log({ inputFile });
84
+ const openAiBatchApiPayload = {
85
+ input_file_id: inputFile.id,
86
+ endpoint: '/v1/responses',
87
+ completion_window: '24h',
88
+ };
89
+ console.log({ openAiBatchApiPayload });
82
90
  let batch = await this.client.batches.create({
83
91
  input_file_id: inputFile.id,
84
92
  endpoint: '/v1/responses',
@@ -120,6 +128,104 @@ class OpenAICompletionProvider {
120
128
  throw error;
121
129
  }
122
130
  }
131
+ async generateSearchVariantsBatchOptimized(items, model, instructions, options) {
132
+ const finalModel = model ?? this.model;
133
+ const chunkSize = options?.chunkSize ?? 200;
134
+ const maxArabic = options?.maxArabic ?? 1;
135
+ const maxArabizi = options?.maxArabizi ?? 2;
136
+ const maxTransliteration = options?.maxTransliteration ?? 1;
137
+ try {
138
+ const finalInstructions = instructions ??
139
+ this.buildOptimizedInstructions({
140
+ maxArabic,
141
+ maxArabizi,
142
+ maxTransliteration,
143
+ });
144
+ const chunkSchema = this.buildOptimizedChunkSchema({ maxArabic, maxArabizi, maxTransliteration });
145
+ const chunks = this.chunkItems(items, chunkSize);
146
+ if (chunks.length > 50_000) {
147
+ throw new Error(`Too many chunks (${chunks.length}) for a single batch file — increase chunkSize or split into multiple batch submissions.`);
148
+ }
149
+ console.log(`generateSearchVariantsBatchOptimized: model=${finalModel} items=${items.length} chunkSize=${chunkSize} chunks=${chunks.length}`);
150
+ const jsonl = chunks
151
+ .map((chunkItems, i) => JSON.stringify({
152
+ custom_id: `chunk-${i}`,
153
+ method: 'POST',
154
+ url: '/v1/responses',
155
+ body: {
156
+ model: finalModel,
157
+ instructions: finalInstructions,
158
+ input: JSON.stringify(chunkItems.map(({ itemId, text }) => ({ id: itemId, text }))),
159
+ text: {
160
+ format: {
161
+ type: 'json_schema',
162
+ name: 'text_variants_batch',
163
+ schema: chunkSchema,
164
+ strict: true,
165
+ },
166
+ },
167
+ },
168
+ }))
169
+ .join('\n');
170
+ const inputFile = await this.client.files.create({
171
+ file: await (0, openai_1.toFile)(Buffer.from(jsonl, 'utf-8'), 'batch-input-optimized.jsonl'),
172
+ purpose: 'batch',
173
+ });
174
+ let batch = await this.client.batches.create({
175
+ input_file_id: inputFile.id,
176
+ endpoint: '/v1/responses',
177
+ completion_window: '24h',
178
+ });
179
+ batch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);
180
+ if (batch.status !== 'completed') {
181
+ throw new Error(`Batch ${batch.id} ended with status "${batch.status}" (${batch.request_counts?.failed ?? 0} failed)`);
182
+ }
183
+ const outputByChunk = await this.readJsonlFile(batch.output_file_id);
184
+ const errorsByChunk = await this.readJsonlFile(batch.error_file_id);
185
+ const resultsById = new Map();
186
+ for (const [chunkId, record] of outputByChunk.entries()) {
187
+ const body = record?.response?.body;
188
+ const outputText = body?.output_text ??
189
+ body?.output
190
+ ?.find((o) => o.type === 'message')
191
+ ?.content?.find((c) => c.type === 'output_text')?.text;
192
+ if (!outputText) {
193
+ const err = errorsByChunk.get(chunkId);
194
+ console.error(`generateSearchVariantsBatchOptimized: no output for ${chunkId}`, err ?? record?.error);
195
+ continue;
196
+ }
197
+ try {
198
+ const parsed = JSON.parse(outputText);
199
+ for (const r of parsed.results) {
200
+ resultsById.set(r.id, {
201
+ arabic: r.ar,
202
+ arabizi: r.az,
203
+ arabicTransliteration: r.tr,
204
+ });
205
+ }
206
+ }
207
+ catch {
208
+ console.error(`generateSearchVariantsBatchOptimized: failed to parse output for ${chunkId}`, outputText);
209
+ }
210
+ }
211
+ return items.map(({ itemId, text }) => {
212
+ const r = resultsById.get(itemId);
213
+ if (!r) {
214
+ console.error(`generateSearchVariantsBatchOptimized: no result for itemId=${itemId}`);
215
+ return { itemId, original: text };
216
+ }
217
+ return { itemId, original: text, ...r };
218
+ });
219
+ }
220
+ catch (error) {
221
+ console.error('generateSearchVariantsBatchOptimized failed', {
222
+ error,
223
+ itemCount: items.length,
224
+ model: finalModel,
225
+ });
226
+ throw error;
227
+ }
228
+ }
123
229
  buildVariantRequest() {
124
230
  const fieldDescriptions = {
125
231
  arabic: 'The Arabic translation of the word/phrase, written in Arabic script (e.g. "زيتون" for "olive"). Provide 1-3 common variants if applicable.',
@@ -135,6 +241,67 @@ class OpenAICompletionProvider {
135
241
  ].join('\n');
136
242
  return { fields, instructions };
137
243
  }
244
+ buildOptimizedInstructions(caps) {
245
+ return [
246
+ 'You generate search-name variants for a food delivery app product/shop name.',
247
+ 'You will receive a JSON array of items, each with "id" and "text".',
248
+ 'For EACH item, return an object with:',
249
+ `- id: the exact id provided (unchanged)`,
250
+ `- ar: Arabic translation(s) in Arabic script, max ${caps.maxArabic}.`,
251
+ `- az: Arabizi spelling(s) (Latin script/numerals, e.g. "zaytoon"), max ${caps.maxArabizi}.`,
252
+ `- tr: Arabic-script transliteration(s) of the original word (not translated), max ${caps.maxTransliteration}.`,
253
+ 'Return one result per input item, same order, no items skipped or merged.',
254
+ 'Values must contain only the term itself — no parenthetical notes, no commentary.',
255
+ 'Return strict JSON matching the schema. No extra commentary, no extra fields.',
256
+ ].join('\n');
257
+ }
258
+ buildJsonSchema(fields) {
259
+ const properties = {};
260
+ for (const field of fields) {
261
+ properties[field] = { type: 'array', items: { type: 'string' } };
262
+ }
263
+ return {
264
+ type: 'object',
265
+ properties,
266
+ required: fields,
267
+ additionalProperties: false,
268
+ };
269
+ }
270
+ buildOptimizedChunkSchema(caps) {
271
+ return {
272
+ type: 'object',
273
+ properties: {
274
+ results: {
275
+ type: 'array',
276
+ items: {
277
+ type: 'object',
278
+ properties: {
279
+ id: { type: 'string' },
280
+ ar: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: caps.maxArabic },
281
+ az: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: caps.maxArabizi },
282
+ tr: {
283
+ type: 'array',
284
+ items: { type: 'string' },
285
+ minItems: 1,
286
+ maxItems: caps.maxTransliteration,
287
+ },
288
+ },
289
+ required: ['id', 'ar', 'az', 'tr'],
290
+ additionalProperties: false,
291
+ },
292
+ },
293
+ },
294
+ required: ['results'],
295
+ additionalProperties: false,
296
+ };
297
+ }
298
+ chunkItems(items, size) {
299
+ const out = [];
300
+ for (let i = 0; i < items.length; i += size) {
301
+ out.push(items.slice(i, i + size));
302
+ }
303
+ return out;
304
+ }
138
305
  async pollBatch(batchId, intervalMs = 5000, maxWaitMs = 24 * 60 * 60 * 1000) {
139
306
  const terminal = new Set(['completed', 'failed', 'expired', 'cancelled']);
140
307
  const start = Date.now();
@@ -161,18 +328,6 @@ class OpenAICompletionProvider {
161
328
  }
162
329
  return result;
163
330
  }
164
- buildJsonSchema(fields) {
165
- const properties = {};
166
- for (const field of fields) {
167
- properties[field] = { type: 'array', items: { type: 'string' } };
168
- }
169
- return {
170
- type: 'object',
171
- properties,
172
- required: fields,
173
- additionalProperties: false,
174
- };
175
- }
176
331
  }
177
332
  exports.OpenAICompletionProvider = OpenAICompletionProvider;
178
333
  //# sourceMappingURL=openai-provider.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"openai-provider.js","sourceRoot":"/","sources":["libraries/llm/providers/openai-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAwC;AAGxC,MAAa,wBAAwB;IACnB,MAAM,CAAS;IACf,KAAK,CAAS;IAE/B,YAAY,MAA0B,EAAE,QAAgB,YAAY;QACnE,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;QACrF,CAAC;QACD,IAA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K,CAAC,aAAa,CAAC,MAAiC;QAC5D,MAAM,MAAM,GAAG,IAAI,GAAG,EAAe,CAAC;QACtC,IAAI,CAAC,MAAM;YAAE,OAAO,MAAM,CAAC;QAE3B,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC7D,MAAM,IAAI,GAAG,MAAM,YAAY,CAAC,IAAI,EAAE,CAAC;QAEvC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YACrD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAEO,eAAe,CAAC,MAAmC;QAC1D,MAAM,UAAU,GAAwB,EAAE,CAAC;QAC3C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,CAAC;QAClE,CAAC;QAED,OAAO;YACN,IAAI,EAAE,QAAQ;YACd,UAAU;YACV,QAAQ,EAAE,MAAM;YAChB,oBAAoB,EAAE,KAAK;SAC3B,CAAC;IACH,CAAC;CACD;AAzMD,4DAyMC","sourcesContent":["import OpenAI, { toFile } from 'openai';\nimport { IAICompletionProvider, ItemTextInput, TextVariantResult } from '../interfaces/common-llm-interfaces';\n\nexport class OpenAICompletionProvider implements IAICompletionProvider {\n\tprivate readonly client: OpenAI;\n\tprivate readonly model: string;\n\n\tconstructor(apiKey: string | undefined, model: string = 'gpt-5-mini') {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error('OPENAI_API_KEY is required to construct OpenAICompletionProvider');\n\t\t}\n\t\tthis.client = new OpenAI({ apiKey });\n\t\tthis.model = model;\n\t}\n\n\t// ── Batch path (cheap, slow, unbounded latency) ─────────────────\n\t// Use for large, non-urgent jobs kicked off from a background\n\t// worker/cron — not from a request/response path.\n\tasync generateSearchVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]> {\n\t\ttry {\n\t\t\tconst { fields, instructions: defaultInstructions } = this.buildVariantRequest();\n\t\t\tconst finalInstructions = instructions ?? defaultInstructions;\n\t\t\tconst finalModel = model ?? this.model;\n\n\t\t\tconsole.log(`You are using llm provider model: ${finalModel}`);\n\n\t\t\tconst schema = this.buildJsonSchema(fields);\n\n\t\t\tif (items.length > 50_000) {\n\t\t\t\tthrow new Error('Batch API supports max 50,000 requests per batch — chunk your items first.');\n\t\t\t}\n\n\t\t\t// 1. Build JSONL\n\t\t\tconst jsonl = items\n\t\t\t\t.map(({ itemId, text }) =>\n\t\t\t\t\tJSON.stringify({\n\t\t\t\t\t\tcustom_id: itemId,\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\turl: '/v1/responses',\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\t\tinput: text,\n\t\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\t\tname: 'text_variants',\n\t\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t.join('\\n');\n\n\t\t\t// 2. Upload input file\n\t\t\tconst inputFile = await this.client.files.create({\n\t\t\t\tfile: await toFile(Buffer.from(jsonl, 'utf-8'), 'batch-input.jsonl'),\n\t\t\t\tpurpose: 'batch',\n\t\t\t});\n\n\t\t\t// 3. Create batch job\n\t\t\tlet batch = await this.client.batches.create({\n\t\t\t\tinput_file_id: inputFile.id,\n\t\t\t\tendpoint: '/v1/responses',\n\t\t\t\tcompletion_window: '24h',\n\t\t\t});\n\n\t\t\t// 4. Poll until done\n\t\t\tbatch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);\n\n\t\t\tif (batch.status !== 'completed') {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Batch ${batch.id} ended with status \"${batch.status}\" (${batch.request_counts?.failed ?? 0} failed)`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 5. Parse output\n\t\t\tconst outputById = await this.readJsonlFile(batch.output_file_id);\n\t\t\tconst errorsById = await this.readJsonlFile(batch.error_file_id);\n\n\t\t\t// 6. Merge results\n\t\t\treturn items.map(({ itemId, text }) => {\n\t\t\t\tconst record = outputById.get(itemId);\n\t\t\t\tconst body = record?.response?.body;\n\n\t\t\t\tconst outputText =\n\t\t\t\t\tbody?.output_text ??\n\t\t\t\t\tbody?.output\n\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\tif (!outputText) {\n\t\t\t\t\tconst err = errorsById.get(itemId);\n\t\t\t\t\tconsole.error(`generatePromptVariantsBatch: no output for itemId=${itemId}`, err ?? record?.error);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\treturn { itemId, original: text, ...JSON.parse(outputText) };\n\t\t\t\t} catch {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generatePromptVariantsBatch: failed to parse output for itemId=${itemId}`,\n\t\t\t\t\t\toutputText\n\t\t\t\t\t);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error('generatePromptVariantsBatch failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel,\n\t\t\t});\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Helpers ──────────────────────────────────────────────────────\n\tprivate buildVariantRequest(): {\n\t\tfields: (keyof TextVariantResult)[];\n\t\tinstructions: string;\n\t} {\n\t\ttype GeneratedField = Exclude<keyof TextVariantResult, 'original' | 'itemId'>;\n\n\t\tconst fieldDescriptions: Record<GeneratedField, string> = {\n\t\t\tarabic:\n\t\t\t\t'The Arabic translation of the word/phrase, written in Arabic script (e.g. \"زيتون\" for \"olive\"). Provide 1-3 common variants if applicable.',\n\t\t\tarabizi:\n\t\t\t\t'The Arabic word\\'s meaning, written phonetically in Latin script/numerals — i.e. Arabizi (e.g. \"zaytoon\"). Provide 1-3 common spelling variants.',\n\t\t\tarabicTransliteration:\n\t\t\t\t'The original word spelled phonetically in Arabic script, NOT translated (e.g. \"أوليف\" for \"olive\"). Useful when there is no native Arabic equivalent.',\n\t\t};\n\n\t\tconst fields = Object.keys(fieldDescriptions) as GeneratedField[];\n\n\t\tconst instructions = [\n\t\t\t'You generate search-name variants for a food delivery app product/shop name.',\n\t\t\t'For the given input text, return ONLY the following fields as arrays of strings:',\n\t\t\t...fields.map(f => `- ${f}: ${fieldDescriptions[f]}`),\n\t\t\t'Return strict JSON matching the schema. No extra commentary, no extra fields.',\n\t\t].join('\\n');\n\n\t\treturn { fields, instructions };\n\t}\n\n\tprivate async pollBatch(\n\t\tbatchId: string,\n\t\tintervalMs = 5000,\n\t\tmaxWaitMs = 24 * 60 * 60 * 1000\n\t): Promise<OpenAI.Batches.Batch> {\n\t\tconst terminal = new Set(['completed', 'failed', 'expired', 'cancelled']);\n\t\tconst start = Date.now();\n\n\t\twhile (true) {\n\t\t\tconst batch = await this.client.batches.retrieve(batchId);\n\t\t\tconsole.log(\n\t\t\t\t`pollBatch: ${batchId} status=${batch.status} elapsed=${Math.round((Date.now() - start) / 1000)}s`\n\t\t\t);\n\t\t\tif (terminal.has(batch.status)) return batch;\n\n\t\t\tif (Date.now() - start > maxWaitMs) {\n\t\t\t\tthrow new Error(`Batch ${batchId} did not finish within ${maxWaitMs}ms (status: ${batch.status})`);\n\t\t\t}\n\n\t\t\tawait new Promise(resolve => setTimeout(resolve, intervalMs));\n\t\t}\n\t}\n\n\tprivate async readJsonlFile(fileId: string | null | undefined): Promise<Map<string, any>> {\n\t\tconst result = new Map<string, any>();\n\t\tif (!fileId) return result;\n\n\t\tconst fileResponse = await this.client.files.content(fileId);\n\t\tconst text = await fileResponse.text();\n\n\t\tfor (const line of text.split('\\n').filter(Boolean)) {\n\t\t\tconst record = JSON.parse(line);\n\t\t\tresult.set(record.custom_id, record);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate buildJsonSchema(fields: (keyof TextVariantResult)[]) {\n\t\tconst properties: Record<string, any> = {};\n\t\tfor (const field of fields) {\n\t\t\tproperties[field] = { type: 'array', items: { type: 'string' } };\n\t\t}\n\n\t\treturn {\n\t\t\ttype: 'object',\n\t\t\tproperties,\n\t\t\trequired: fields,\n\t\t\tadditionalProperties: false,\n\t\t};\n\t}\n}\n"]}
1
+ {"version":3,"file":"openai-provider.js","sourceRoot":"/","sources":["libraries/llm/providers/openai-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAwC;AAGxC,MAAa,wBAAwB;IACnB,MAAM,CAAS;IACf,KAAK,CAAS;IAE/B,YAAY,MAA0B,EAAE,QAAgB,YAAY;QACnE,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;QACrF,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,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,CAClG,CAAC;YACF,IAAI,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC;gBAAE,OAAO,KAAK,CAAC;YAE7C,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,SAAS,EAAE,CAAC;gBACpC,MAAM,IAAI,KAAK,CAAC,SAAS,OAAO,0BAA0B,SAAS,eAAe,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;YACpG,CAAC;YAED,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC;QAC/D,CAAC;IACF,CAAC;IAEO,KAAK,CAAC,aAAa,CAAC,MAAiC;QAC5D,MAAM,MAAM,GAAG,IAAI,GAAG,EAAe,CAAC;QACtC,IAAI,CAAC,MAAM;YAAE,OAAO,MAAM,CAAC;QAE3B,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC7D,MAAM,IAAI,GAAG,MAAM,YAAY,CAAC,IAAI,EAAE,CAAC;QAEvC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YACrD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;CACD;AA1bD,4DA0bC","sourcesContent":["import OpenAI, { toFile } from 'openai';\nimport { IAICompletionProvider, ItemTextInput, TextVariantResult } from '../interfaces/common-llm-interfaces';\n\nexport class OpenAICompletionProvider implements IAICompletionProvider {\n\tprivate readonly client: OpenAI;\n\tprivate readonly model: string;\n\n\tconstructor(apiKey: string | undefined, model: string = 'gpt-5-mini') {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error('OPENAI_API_KEY is required to construct OpenAICompletionProvider');\n\t\t}\n\t\tthis.client = new OpenAI({ apiKey });\n\t\tthis.model = model;\n\t}\n\n\t// ── Batch path (cheap, slow, unbounded latency) ─────────────────\n\t// Use for large, non-urgent jobs kicked off from a background\n\t// worker/cron — not from a request/response path.\n\t// NOTE: this is the ORIGINAL, non-optimized version (1 item per\n\t// line, verbose keys, unbounded array lengths, instructions\n\t// repeated per item). Kept as-is for comparison/rollback.\n\t// Prefer generateSearchVariantsBatchOptimized for production/cost-sensitive runs.\n\tasync generateSearchVariantsBatch(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { pollIntervalMs?: number; maxWaitMs?: number }\n\t): Promise<TextVariantResult[]> {\n\t\ttry {\n\t\t\tconst { fields, instructions: defaultInstructions } = this.buildVariantRequest();\n\t\t\tconst finalInstructions = instructions ?? defaultInstructions;\n\t\t\tconst finalModel = model ?? this.model;\n\n\t\t\tconsole.log(`You are using llm provider model: ${finalModel}`);\n\n\t\t\tconst schema = this.buildJsonSchema(fields);\n\n\t\t\tif (items.length > 50_000) {\n\t\t\t\tthrow new Error('Batch API supports max 50,000 requests per batch — chunk your items first.');\n\t\t\t}\n\n\t\t\t// 1. Build JSONL\n\t\t\tconst jsonl = items\n\t\t\t\t.map(({ itemId, text }) =>\n\t\t\t\t\tJSON.stringify({\n\t\t\t\t\t\tcustom_id: itemId,\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\turl: '/v1/responses',\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\t\tinput: text,\n\t\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\t\tname: 'text_variants',\n\t\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t.join('\\n');\n\t\t\tconsole.log({ jsonl });\n\t\t\t// 2. Upload input file\n\t\t\tconst inputFile = await this.client.files.create({\n\t\t\t\tfile: await toFile(Buffer.from(jsonl, 'utf-8'), 'batch-input.jsonl'),\n\t\t\t\tpurpose: 'batch',\n\t\t\t});\n\t\t\tconsole.log({ inputFile });\n\t\t\t// 3. Create batch job\n\t\t\tconst openAiBatchApiPayload = {\n\t\t\t\tinput_file_id: inputFile.id,\n\t\t\t\tendpoint: '/v1/responses',\n\t\t\t\tcompletion_window: '24h',\n\t\t\t};\n\t\t\tconsole.log({ openAiBatchApiPayload });\n\t\t\tlet batch = await this.client.batches.create({\n\t\t\t\tinput_file_id: inputFile.id,\n\t\t\t\tendpoint: '/v1/responses',\n\t\t\t\tcompletion_window: '24h',\n\t\t\t});\n\n\t\t\t// 4. Poll until done\n\t\t\tbatch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);\n\n\t\t\tif (batch.status !== 'completed') {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Batch ${batch.id} ended with status \"${batch.status}\" (${batch.request_counts?.failed ?? 0} failed)`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 5. Parse output\n\t\t\tconst outputById = await this.readJsonlFile(batch.output_file_id);\n\t\t\tconst errorsById = await this.readJsonlFile(batch.error_file_id);\n\n\t\t\t// 6. Merge results\n\t\t\treturn items.map(({ itemId, text }) => {\n\t\t\t\tconst record = outputById.get(itemId);\n\t\t\t\tconst body = record?.response?.body;\n\n\t\t\t\tconst outputText =\n\t\t\t\t\tbody?.output_text ??\n\t\t\t\t\tbody?.output\n\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\tif (!outputText) {\n\t\t\t\t\tconst err = errorsById.get(itemId);\n\t\t\t\t\tconsole.error(`generatePromptVariantsBatch: no output for itemId=${itemId}`, err ?? record?.error);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\treturn { itemId, original: text, ...JSON.parse(outputText) };\n\t\t\t\t} catch {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generatePromptVariantsBatch: failed to parse output for itemId=${itemId}`,\n\t\t\t\t\t\toutputText\n\t\t\t\t\t);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error('generatePromptVariantsBatch failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel,\n\t\t\t});\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Cost-optimized batch path ────────────────────────────────────\n\t// Implements everything discussed:\n\t// 1. Chunking — groups items (default 200/chunk) into one line per\n\t// chunk, so `instructions` is billed once per chunk instead of\n\t// once per item (biggest single cost lever).\n\t// 2. Short output keys — model emits {id, ar, az, tr} instead of\n\t// {itemId, arabic, arabizi, arabicTransliteration}; mapped back\n\t// to full field names locally, at zero token cost.\n\t// 3. Capped array lengths — schema enforces maxItems (not just a\n\t// prose suggestion), so the model can't drift to longer arrays.\n\t// 4. No echoed fields — model never re-emits itemId/original text;\n\t// both are re-attached locally from the input you already have.\n\tasync generateSearchVariantsBatchOptimized(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: {\n\t\t\tchunkSize?: number; // items per chunk/line — tune after testing token usage\n\t\t\tmaxArabic?: number; // cap on `arabic` variants (set to 1 for a single string-like result)\n\t\t\tmaxArabizi?: number; // cap on `arabizi` variants\n\t\t\tmaxTransliteration?: number; // cap on `arabicTransliteration` variants\n\t\t\tpollIntervalMs?: number;\n\t\t\tmaxWaitMs?: number;\n\t\t}\n\t): Promise<TextVariantResult[]> {\n\t\tconst finalModel = model ?? this.model;\n\t\tconst chunkSize = options?.chunkSize ?? 200;\n\t\tconst maxArabic = options?.maxArabic ?? 1;\n\t\tconst maxArabizi = options?.maxArabizi ?? 2;\n\t\tconst maxTransliteration = options?.maxTransliteration ?? 1;\n\n\t\ttry {\n\t\t\tconst finalInstructions =\n\t\t\t\tinstructions ??\n\t\t\t\tthis.buildOptimizedInstructions({\n\t\t\t\t\tmaxArabic,\n\t\t\t\t\tmaxArabizi,\n\t\t\t\t\tmaxTransliteration,\n\t\t\t\t});\n\n\t\t\tconst chunkSchema = this.buildOptimizedChunkSchema({ maxArabic, maxArabizi, maxTransliteration });\n\n\t\t\tconst chunks = this.chunkItems(items, chunkSize);\n\n\t\t\tif (chunks.length > 50_000) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Too many chunks (${chunks.length}) for a single batch file — increase chunkSize or split into multiple batch submissions.`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconsole.log(\n\t\t\t\t`generateSearchVariantsBatchOptimized: model=${finalModel} items=${items.length} chunkSize=${chunkSize} chunks=${chunks.length}`\n\t\t\t);\n\n\t\t\t// 1. Build JSONL — ONE line per chunk, not per item\n\t\t\tconst jsonl = chunks\n\t\t\t\t.map((chunkItems, i) =>\n\t\t\t\t\tJSON.stringify({\n\t\t\t\t\t\tcustom_id: `chunk-${i}`,\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\turl: '/v1/responses',\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\t\t// Only send what the model needs to generate from: id + text.\n\t\t\t\t\t\t\t// No other fields, no restating of instructions per item.\n\t\t\t\t\t\t\tinput: JSON.stringify(chunkItems.map(({ itemId, text }) => ({ id: itemId, text }))),\n\t\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\t\tname: 'text_variants_batch',\n\t\t\t\t\t\t\t\t\tschema: chunkSchema,\n\t\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t\t.join('\\n');\n\n\t\t\t// 2. Upload input file\n\t\t\tconst inputFile = await this.client.files.create({\n\t\t\t\tfile: await toFile(Buffer.from(jsonl, 'utf-8'), 'batch-input-optimized.jsonl'),\n\t\t\t\tpurpose: 'batch',\n\t\t\t});\n\n\t\t\t// 3. Create batch job\n\t\t\tlet batch = await this.client.batches.create({\n\t\t\t\tinput_file_id: inputFile.id,\n\t\t\t\tendpoint: '/v1/responses',\n\t\t\t\tcompletion_window: '24h',\n\t\t\t});\n\n\t\t\t// 4. Poll until done\n\t\t\tbatch = await this.pollBatch(batch.id, options?.pollIntervalMs, options?.maxWaitMs);\n\n\t\t\tif (batch.status !== 'completed') {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Batch ${batch.id} ended with status \"${batch.status}\" (${batch.request_counts?.failed ?? 0} failed)`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 5. Parse output — keyed by chunk custom_id, each containing an array of per-item results\n\t\t\tconst outputByChunk = await this.readJsonlFile(batch.output_file_id);\n\t\t\tconst errorsByChunk = await this.readJsonlFile(batch.error_file_id);\n\n\t\t\t// 6. Flatten chunk results into a per-itemId map, expanding short keys back to full field names\n\t\t\tconst resultsById = new Map<string, Partial<TextVariantResult>>();\n\n\t\t\tfor (const [chunkId, record] of outputByChunk.entries()) {\n\t\t\t\tconst body = record?.response?.body;\n\t\t\t\tconst outputText =\n\t\t\t\t\tbody?.output_text ??\n\t\t\t\t\tbody?.output\n\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\tif (!outputText) {\n\t\t\t\t\tconst err = errorsByChunk.get(chunkId);\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generateSearchVariantsBatchOptimized: no output for ${chunkId}`,\n\t\t\t\t\t\terr ?? record?.error\n\t\t\t\t\t);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst parsed = JSON.parse(outputText) as {\n\t\t\t\t\t\tresults: Array<{ id: string; ar: string[]; az: string[]; tr: string[] }>;\n\t\t\t\t\t};\n\t\t\t\t\tfor (const r of parsed.results) {\n\t\t\t\t\t\tresultsById.set(r.id, {\n\t\t\t\t\t\t\tarabic: r.ar,\n\t\t\t\t\t\t\tarabizi: r.az,\n\t\t\t\t\t\t\tarabicTransliteration: r.tr,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generateSearchVariantsBatchOptimized: failed to parse output for ${chunkId}`,\n\t\t\t\t\t\toutputText\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 7. Merge back with original items, preserving order and filling gaps\n\t\t\treturn items.map(({ itemId, text }) => {\n\t\t\t\tconst r = resultsById.get(itemId);\n\t\t\t\tif (!r) {\n\t\t\t\t\tconsole.error(`generateSearchVariantsBatchOptimized: no result for itemId=${itemId}`);\n\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t}\n\t\t\t\treturn { itemId, original: text, ...r } as TextVariantResult;\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error('generateSearchVariantsBatchOptimized failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel: finalModel,\n\t\t\t});\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Helpers ──────────────────────────────────────────────────────\n\tprivate buildVariantRequest(): {\n\t\tfields: (keyof TextVariantResult)[];\n\t\tinstructions: string;\n\t} {\n\t\ttype GeneratedField = Exclude<keyof TextVariantResult, 'original' | 'itemId'>;\n\n\t\tconst fieldDescriptions: Record<GeneratedField, string> = {\n\t\t\tarabic:\n\t\t\t\t'The Arabic translation of the word/phrase, written in Arabic script (e.g. \"زيتون\" for \"olive\"). Provide 1-3 common variants if applicable.',\n\t\t\tarabizi:\n\t\t\t\t'The Arabic word\\'s meaning, written phonetically in Latin script/numerals — i.e. Arabizi (e.g. \"zaytoon\"). Provide 1-3 common spelling variants.',\n\t\t\tarabicTransliteration:\n\t\t\t\t'The original word spelled phonetically in Arabic script, NOT translated (e.g. \"أوليف\" for \"olive\"). Useful when there is no native Arabic equivalent.',\n\t\t};\n\n\t\tconst fields = Object.keys(fieldDescriptions) as GeneratedField[];\n\n\t\tconst instructions = [\n\t\t\t'You generate search-name variants for a food delivery app product/shop name.',\n\t\t\t'For the given input text, return ONLY the following fields as arrays of strings:',\n\t\t\t...fields.map(f => `- ${f}: ${fieldDescriptions[f]}`),\n\t\t\t'Return strict JSON matching the schema. No extra commentary, no extra fields.',\n\t\t].join('\\n');\n\n\t\treturn { fields, instructions };\n\t}\n\n\t// Compact instructions for the optimized path: short-key output format,\n\t// explicit array caps stated in prose (schema also enforces them),\n\t// and an explicit \"no commentary inside values\" rule to stop the model\n\t// padding fields with parenthetical notes.\n\tprivate buildOptimizedInstructions(caps: {\n\t\tmaxArabic: number;\n\t\tmaxArabizi: number;\n\t\tmaxTransliteration: number;\n\t}): string {\n\t\treturn [\n\t\t\t'You generate search-name variants for a food delivery app product/shop name.',\n\t\t\t'You will receive a JSON array of items, each with \"id\" and \"text\".',\n\t\t\t'For EACH item, return an object with:',\n\t\t\t`- id: the exact id provided (unchanged)`,\n\t\t\t`- ar: Arabic translation(s) in Arabic script, max ${caps.maxArabic}.`,\n\t\t\t`- az: Arabizi spelling(s) (Latin script/numerals, e.g. \"zaytoon\"), max ${caps.maxArabizi}.`,\n\t\t\t`- tr: Arabic-script transliteration(s) of the original word (not translated), max ${caps.maxTransliteration}.`,\n\t\t\t'Return one result per input item, same order, no items skipped or merged.',\n\t\t\t'Values must contain only the term itself — no parenthetical notes, no commentary.',\n\t\t\t'Return strict JSON matching the schema. No extra commentary, no extra fields.',\n\t\t].join('\\n');\n\t}\n\n\t// Schema for the ORIGINAL per-item path (verbose keys, unbounded arrays).\n\tprivate buildJsonSchema(fields: (keyof TextVariantResult)[]) {\n\t\tconst properties: Record<string, any> = {};\n\t\tfor (const field of fields) {\n\t\t\tproperties[field] = { type: 'array', items: { type: 'string' } };\n\t\t}\n\n\t\treturn {\n\t\t\ttype: 'object',\n\t\t\tproperties,\n\t\t\trequired: fields,\n\t\t\tadditionalProperties: false,\n\t\t};\n\t}\n\n\t// Schema for the OPTIMIZED chunk path: wraps a `results` array (one\n\t// entry per item in the chunk), short keys, and hard maxItems caps.\n\tprivate buildOptimizedChunkSchema(caps: {\n\t\tmaxArabic: number;\n\t\tmaxArabizi: number;\n\t\tmaxTransliteration: number;\n\t}) {\n\t\treturn {\n\t\t\ttype: 'object',\n\t\t\tproperties: {\n\t\t\t\tresults: {\n\t\t\t\t\ttype: 'array',\n\t\t\t\t\titems: {\n\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tid: { type: 'string' },\n\t\t\t\t\t\t\tar: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: caps.maxArabic },\n\t\t\t\t\t\t\taz: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: caps.maxArabizi },\n\t\t\t\t\t\t\ttr: {\n\t\t\t\t\t\t\t\ttype: 'array',\n\t\t\t\t\t\t\t\titems: { type: 'string' },\n\t\t\t\t\t\t\t\tminItems: 1,\n\t\t\t\t\t\t\t\tmaxItems: caps.maxTransliteration,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: ['id', 'ar', 'az', 'tr'],\n\t\t\t\t\t\tadditionalProperties: false,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\trequired: ['results'],\n\t\t\tadditionalProperties: false,\n\t\t};\n\t}\n\n\tprivate chunkItems<T>(items: T[], size: number): T[][] {\n\t\tconst out: T[][] = [];\n\t\tfor (let i = 0; i < items.length; i += size) {\n\t\t\tout.push(items.slice(i, i + size));\n\t\t}\n\t\treturn out;\n\t}\n\n\tprivate async pollBatch(\n\t\tbatchId: string,\n\t\tintervalMs = 5000,\n\t\tmaxWaitMs = 24 * 60 * 60 * 1000\n\t): Promise<OpenAI.Batches.Batch> {\n\t\tconst terminal = new Set(['completed', 'failed', 'expired', 'cancelled']);\n\t\tconst start = Date.now();\n\n\t\twhile (true) {\n\t\t\tconst batch = await this.client.batches.retrieve(batchId);\n\t\t\tconsole.log(\n\t\t\t\t`pollBatch: ${batchId} status=${batch.status} elapsed=${Math.round((Date.now() - start) / 1000)}s`\n\t\t\t);\n\t\t\tif (terminal.has(batch.status)) return batch;\n\n\t\t\tif (Date.now() - start > maxWaitMs) {\n\t\t\t\tthrow new Error(`Batch ${batchId} did not finish within ${maxWaitMs}ms (status: ${batch.status})`);\n\t\t\t}\n\n\t\t\tawait new Promise(resolve => setTimeout(resolve, intervalMs));\n\t\t}\n\t}\n\n\tprivate async readJsonlFile(fileId: string | null | undefined): Promise<Map<string, any>> {\n\t\tconst result = new Map<string, any>();\n\t\tif (!fileId) return result;\n\n\t\tconst fileResponse = await this.client.files.content(fileId);\n\t\tconst text = await fileResponse.text();\n\n\t\tfor (const line of text.split('\\n').filter(Boolean)) {\n\t\t\tconst record = JSON.parse(line);\n\t\t\tresult.set(record.custom_id, record);\n\t\t}\n\n\t\treturn result;\n\t}\n}\n"]}
@@ -22,7 +22,7 @@ Perfect for sharing types between frontend and backend applications.
22
22
 
23
23
  ## Version
24
24
 
25
- Version: 1.4.291
25
+ Version: 1.4.292
26
26
 
27
27
  ## Dependencies
28
28
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/types",
3
- "version": "1.4.291",
3
+ "version": "1.4.292",
4
4
  "description": "Lyxa type definitions and validation schemas for both frontend and backend",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/core",
3
- "version": "1.4.291",
3
+ "version": "1.4.292",
4
4
  "description": "The Core system of the Lyxa services.",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",