@lyxa.ai/core 1.4.289 → 1.4.291
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/dist/libraries/llm/interfaces/common-llm-interfaces.d.ts +1 -3
- package/dist/libraries/llm/interfaces/common-llm-interfaces.js.map +1 -1
- package/dist/libraries/llm/llm-provider-service.d.ts +1 -1
- package/dist/libraries/llm/llm-provider-service.js +3 -6
- package/dist/libraries/llm/llm-provider-service.js.map +1 -1
- package/dist/libraries/llm/providers/openai-provider.d.ts +1 -12
- package/dist/libraries/llm/providers/openai-provider.js +1 -105
- package/dist/libraries/llm/providers/openai-provider.js.map +1 -1
- package/dist/libraries/trpc/middlewares/auth.d.ts +1 -1
- package/dist/libraries/trpc/middlewares/createRoleProtectedProcedure.d.ts +1 -1
- package/dist/libraries/trpc/middlewares/phone-verified.d.ts +1 -1
- package/dist/libraries/trpc/middlewares/publicUserDecoder.d.ts +1 -1
- package/dist/types/README.md +1 -1
- package/dist/types/package.json +1 -1
- package/dist/types/utilities/validation/common-validation.d.ts +12 -12
- package/package.json +1 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { LLMProvider } from '../llm-provider-service';
|
|
2
2
|
export interface IAICompletionProvider {
|
|
3
|
-
|
|
4
|
-
concurrency?: number;
|
|
3
|
+
generateSearchVariantsBatch(items: ItemTextInput[], model?: string, instructions?: string, options?: {
|
|
5
4
|
pollIntervalMs?: number;
|
|
6
5
|
maxWaitMs?: number;
|
|
7
6
|
}): Promise<TextVariantResult[]>;
|
|
@@ -18,7 +17,6 @@ export interface AiConfiguration {
|
|
|
18
17
|
model?: string;
|
|
19
18
|
instructions?: string;
|
|
20
19
|
prompt?: string;
|
|
21
|
-
concurrency?: number;
|
|
22
20
|
}
|
|
23
21
|
export interface ItemTextInput {
|
|
24
22
|
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\
|
|
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}"]}
|
|
@@ -8,6 +8,6 @@ export declare class LLMCompletionService {
|
|
|
8
8
|
private providerInstance;
|
|
9
9
|
private readonly factories;
|
|
10
10
|
constructor(provider: LLMProvider);
|
|
11
|
-
|
|
11
|
+
generateSearchVariants(payload: ItemTextInput[], aiConfigurations: AiConfiguration): Promise<TextVariantResult[]>;
|
|
12
12
|
private getProvider;
|
|
13
13
|
}
|
|
@@ -27,19 +27,16 @@ class LLMCompletionService {
|
|
|
27
27
|
constructor(provider) {
|
|
28
28
|
this.provider = provider;
|
|
29
29
|
}
|
|
30
|
-
async
|
|
30
|
+
async generateSearchVariants(payload, aiConfigurations) {
|
|
31
31
|
const provider = await this.getProvider();
|
|
32
32
|
console.log(`You are using llm provider: ${this.provider} with model: ${aiConfigurations.model ?? 'gpt-5-mini'}`);
|
|
33
|
-
|
|
34
|
-
concurrency: aiConfigurations.concurrency ?? 10,
|
|
35
|
-
};
|
|
36
|
-
return provider.generateSearchVariants(payload, aiConfigurations.model, aiConfigurations.instructions, options);
|
|
33
|
+
return provider.generateSearchVariantsBatch(payload, aiConfigurations.model, aiConfigurations.instructions);
|
|
37
34
|
}
|
|
38
35
|
async getProvider() {
|
|
39
36
|
if (!this.providerInstance) {
|
|
40
37
|
const factory = this.factories[this.provider];
|
|
41
38
|
if (!factory) {
|
|
42
|
-
throw (0, error_common_1.badRequestError)(`Unsupported
|
|
39
|
+
throw (0, error_common_1.badRequestError)(`Unsupported LLM provider: ${this.provider}`);
|
|
43
40
|
}
|
|
44
41
|
this.providerInstance = await factory();
|
|
45
42
|
}
|
|
@@ -1 +1 @@
|
|
|
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,YA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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,2BAA2B,CAC1C,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-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}"]}
|
|
@@ -3,18 +3,7 @@ export declare class OpenAICompletionProvider implements IAICompletionProvider {
|
|
|
3
3
|
private readonly client;
|
|
4
4
|
private readonly model;
|
|
5
5
|
constructor(apiKey: string | undefined, model?: string);
|
|
6
|
-
|
|
7
|
-
batchThreshold?: number;
|
|
8
|
-
concurrency?: number;
|
|
9
|
-
maxRetries?: number;
|
|
10
|
-
pollIntervalMs?: number;
|
|
11
|
-
maxWaitMs?: number;
|
|
12
|
-
}): Promise<TextVariantResult[]>;
|
|
13
|
-
generatePromptVariantsRealtime(items: ItemTextInput[], model?: string, instructions?: string, options?: {
|
|
14
|
-
concurrency?: number;
|
|
15
|
-
maxRetries?: number;
|
|
16
|
-
}): Promise<TextVariantResult[]>;
|
|
17
|
-
generatePromptVariantsBatch(items: ItemTextInput[], model?: string, instructions?: string, options?: {
|
|
6
|
+
generateSearchVariantsBatch(items: ItemTextInput[], model?: string, instructions?: string, options?: {
|
|
18
7
|
pollIntervalMs?: number;
|
|
19
8
|
maxWaitMs?: number;
|
|
20
9
|
}): Promise<TextVariantResult[]>;
|
|
@@ -45,111 +45,7 @@ class OpenAICompletionProvider {
|
|
|
45
45
|
this.client = new openai_1.default({ apiKey });
|
|
46
46
|
this.model = model;
|
|
47
47
|
}
|
|
48
|
-
async
|
|
49
|
-
const batchThreshold = options?.batchThreshold ?? 1000;
|
|
50
|
-
const mode = items.length >= batchThreshold ? 'batch' : 'realtime';
|
|
51
|
-
console.log('generatePromptVariants: dispatching', {
|
|
52
|
-
itemCount: items.length,
|
|
53
|
-
batchThreshold,
|
|
54
|
-
mode,
|
|
55
|
-
});
|
|
56
|
-
if (mode === 'batch') {
|
|
57
|
-
console.log('Started batch processing generatePromptVariantsBatch');
|
|
58
|
-
return this.generatePromptVariantsBatch(items, model, instructions, {
|
|
59
|
-
pollIntervalMs: options?.pollIntervalMs,
|
|
60
|
-
maxWaitMs: options?.maxWaitMs,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
console.log('Started realtime processing generatePromptVariantsRealtime');
|
|
64
|
-
return this.generatePromptVariantsRealtime(items, model, instructions, {
|
|
65
|
-
concurrency: options?.concurrency,
|
|
66
|
-
maxRetries: options?.maxRetries,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
async generatePromptVariantsRealtime(items, model, instructions, options) {
|
|
70
|
-
const concurrency = options?.concurrency ?? 10;
|
|
71
|
-
const maxRetries = options?.maxRetries ?? 3;
|
|
72
|
-
try {
|
|
73
|
-
if (items.length === 0)
|
|
74
|
-
return [];
|
|
75
|
-
const { fields, instructions: defaultInstructions } = this.buildVariantRequest();
|
|
76
|
-
const finalInstructions = instructions ?? defaultInstructions;
|
|
77
|
-
const finalModel = model ?? this.model;
|
|
78
|
-
const schema = this.buildJsonSchema(fields);
|
|
79
|
-
console.log('generatePromptVariantsRealtime: starting', {
|
|
80
|
-
itemCount: items.length,
|
|
81
|
-
finalModel,
|
|
82
|
-
concurrency,
|
|
83
|
-
});
|
|
84
|
-
const runOne = async ({ itemId, text }, attempt = 1) => {
|
|
85
|
-
try {
|
|
86
|
-
const response = await this.client.responses.create({
|
|
87
|
-
model: finalModel,
|
|
88
|
-
instructions: finalInstructions,
|
|
89
|
-
input: text,
|
|
90
|
-
text: {
|
|
91
|
-
format: {
|
|
92
|
-
type: 'json_schema',
|
|
93
|
-
name: 'text_variants',
|
|
94
|
-
schema,
|
|
95
|
-
strict: true,
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
const outputText = response.output_text ??
|
|
100
|
-
response.output
|
|
101
|
-
?.find((o) => o.type === 'message')
|
|
102
|
-
?.content?.find((c) => c.type === 'output_text')?.text;
|
|
103
|
-
if (!outputText) {
|
|
104
|
-
throw new Error('empty output_text in response');
|
|
105
|
-
}
|
|
106
|
-
let parsed;
|
|
107
|
-
try {
|
|
108
|
-
parsed = JSON.parse(outputText);
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
console.error(`generatePromptVariantsRealtime: failed to parse output for itemId=${itemId}`, outputText);
|
|
112
|
-
return { itemId, original: text };
|
|
113
|
-
}
|
|
114
|
-
return { itemId, original: text, ...parsed };
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
const status = error?.status ?? error?.response?.status;
|
|
118
|
-
const isRetryable = status === 429 || status === 500 || status === 503;
|
|
119
|
-
if (isRetryable && attempt <= maxRetries) {
|
|
120
|
-
const backoffMs = 1000 * 2 ** attempt;
|
|
121
|
-
console.warn(`generatePromptVariantsRealtime: retryable error (status=${status}) for itemId=${itemId}, attempt ${attempt}/${maxRetries}, retrying in ${backoffMs}ms`);
|
|
122
|
-
await new Promise(resolve => setTimeout(resolve, backoffMs));
|
|
123
|
-
return runOne({ itemId, text }, attempt + 1);
|
|
124
|
-
}
|
|
125
|
-
console.error(`generatePromptVariantsRealtime: failed for itemId=${itemId} after ${attempt} attempt(s)`, error);
|
|
126
|
-
return { itemId, original: text };
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
const results = [];
|
|
130
|
-
for (let i = 0; i < items.length; i += concurrency) {
|
|
131
|
-
const chunk = items.slice(i, i + concurrency);
|
|
132
|
-
const chunkResults = await Promise.all(chunk.map(item => runOne(item)));
|
|
133
|
-
results.push(...chunkResults);
|
|
134
|
-
console.log(`generatePromptVariantsRealtime: progress ${Math.min(i + concurrency, items.length)}/${items.length}`);
|
|
135
|
-
}
|
|
136
|
-
const failedCount = results.filter(r => Object.keys(r).length <= 2).length;
|
|
137
|
-
console.log('generatePromptVariantsRealtime: done', {
|
|
138
|
-
total: items.length,
|
|
139
|
-
failed: failedCount,
|
|
140
|
-
});
|
|
141
|
-
return results;
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
console.error('generatePromptVariantsRealtime failed', {
|
|
145
|
-
error,
|
|
146
|
-
itemCount: items.length,
|
|
147
|
-
model,
|
|
148
|
-
});
|
|
149
|
-
throw error;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
async generatePromptVariantsBatch(items, model, instructions, options) {
|
|
48
|
+
async generateSearchVariantsBatch(items, model, instructions, options) {
|
|
153
49
|
try {
|
|
154
50
|
const { fields, instructions: defaultInstructions } = this.buildVariantRequest();
|
|
155
51
|
const finalInstructions = instructions ?? defaultInstructions;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-provider.js","sourceRo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AAC,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;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;AArWD,4DAqWC","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// ── Public dispatcher ───────────────────────────────────────────\n\t// Use this as the default entry point. It picks realtime (fast,\n\t// synchronous fan-out) vs batch (cheap, unbounded latency) based\n\t// on item count so callers don't have to decide manually.\n\n\tasync generateSearchVariants(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: {\n\t\t\tbatchThreshold?: number; // item count at/above which we switch to Batch API\n\t\t\tconcurrency?: number; // realtime fan-out concurrency\n\t\t\tmaxRetries?: number; // realtime retry count on 429/500/503\n\t\t\tpollIntervalMs?: number; // batch poll interval\n\t\t\tmaxWaitMs?: number; // batch max wait\n\t\t}\n\t): Promise<TextVariantResult[]> {\n\t\tconst batchThreshold = options?.batchThreshold ?? 1000;\n\t\tconst mode = items.length >= batchThreshold ? 'batch' : 'realtime';\n\n\t\tconsole.log('generatePromptVariants: dispatching', {\n\t\t\titemCount: items.length,\n\t\t\tbatchThreshold,\n\t\t\tmode,\n\t\t});\n\n\t\tif (mode === 'batch') {\n\t\t\tconsole.log('Started batch processing generatePromptVariantsBatch');\n\t\t\treturn this.generatePromptVariantsBatch(items, model, instructions, {\n\t\t\t\tpollIntervalMs: options?.pollIntervalMs,\n\t\t\t\tmaxWaitMs: options?.maxWaitMs,\n\t\t\t});\n\t\t}\n\n\t\tconsole.log('Started realtime processing generatePromptVariantsRealtime');\n\t\treturn this.generatePromptVariantsRealtime(items, model, instructions, {\n\t\t\tconcurrency: options?.concurrency,\n\t\t\tmaxRetries: options?.maxRetries,\n\t\t});\n\t}\n\n\t// ── Realtime path (fast) ────────────────────────────────────────\n\t// Direct synchronous calls, fanned out with bounded concurrency.\n\t// Use this whenever you need the result back in the same request/\n\t// job — seconds to low minutes for hundreds of items, not hours.\n\n\tasync generatePromptVariantsRealtime(\n\t\titems: ItemTextInput[],\n\t\tmodel?: string,\n\t\tinstructions?: string,\n\t\toptions?: { concurrency?: number; maxRetries?: number }\n\t): Promise<TextVariantResult[]> {\n\t\tconst concurrency = options?.concurrency ?? 10;\n\t\tconst maxRetries = options?.maxRetries ?? 3;\n\n\t\ttry {\n\t\t\tif (items.length === 0) return [];\n\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\t\t\tconst schema = this.buildJsonSchema(fields);\n\n\t\t\tconsole.log('generatePromptVariantsRealtime: starting', {\n\t\t\t\titemCount: items.length,\n\t\t\t\tfinalModel,\n\t\t\t\tconcurrency,\n\t\t\t});\n\n\t\t\tconst runOne = async ({ itemId, text }: ItemTextInput, attempt = 1): Promise<TextVariantResult> => {\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await this.client.responses.create({\n\t\t\t\t\t\tmodel: finalModel,\n\t\t\t\t\t\tinstructions: finalInstructions,\n\t\t\t\t\t\tinput: text,\n\t\t\t\t\t\ttext: {\n\t\t\t\t\t\t\tformat: {\n\t\t\t\t\t\t\t\ttype: 'json_schema',\n\t\t\t\t\t\t\t\tname: 'text_variants',\n\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\tstrict: true,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\n\t\t\t\t\tconst outputText =\n\t\t\t\t\t\t(response as any).output_text ??\n\t\t\t\t\t\t(response as any).output\n\t\t\t\t\t\t\t?.find((o: any) => o.type === 'message')\n\t\t\t\t\t\t\t?.content?.find((c: any) => c.type === 'output_text')?.text;\n\n\t\t\t\t\tif (!outputText) {\n\t\t\t\t\t\tthrow new Error('empty output_text in response');\n\t\t\t\t\t}\n\n\t\t\t\t\tlet parsed: Record<string, unknown>;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tparsed = JSON.parse(outputText);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`generatePromptVariantsRealtime: failed to parse output for itemId=${itemId}`,\n\t\t\t\t\t\t\toutputText\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn { itemId, original: text };\n\t\t\t\t\t}\n\n\t\t\t\t\treturn { itemId, original: text, ...parsed };\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\tconst status = error?.status ?? error?.response?.status;\n\t\t\t\t\tconst isRetryable = status === 429 || status === 500 || status === 503;\n\n\t\t\t\t\tif (isRetryable && attempt <= maxRetries) {\n\t\t\t\t\t\tconst backoffMs = 1000 * 2 ** attempt; // 2s, 4s, 8s...\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t`generatePromptVariantsRealtime: retryable error (status=${status}) for itemId=${itemId}, attempt ${attempt}/${maxRetries}, retrying in ${backoffMs}ms`\n\t\t\t\t\t\t);\n\t\t\t\t\t\tawait new Promise(resolve => setTimeout(resolve, backoffMs));\n\t\t\t\t\t\treturn runOne({ itemId, text }, attempt + 1);\n\t\t\t\t\t}\n\n\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t`generatePromptVariantsRealtime: failed for itemId=${itemId} after ${attempt} attempt(s)`,\n\t\t\t\t\t\terror\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\n\t\t\tconst results: TextVariantResult[] = [];\n\t\t\tfor (let i = 0; i < items.length; i += concurrency) {\n\t\t\t\tconst chunk = items.slice(i, i + concurrency);\n\t\t\t\tconst chunkResults = await Promise.all(chunk.map(item => runOne(item)));\n\t\t\t\tresults.push(...chunkResults);\n\t\t\t\tconsole.log(\n\t\t\t\t\t`generatePromptVariantsRealtime: progress ${Math.min(i + concurrency, items.length)}/${items.length}`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst failedCount = results.filter(r => Object.keys(r).length <= 2).length;\n\t\t\tconsole.log('generatePromptVariantsRealtime: done', {\n\t\t\t\ttotal: items.length,\n\t\t\t\tfailed: failedCount,\n\t\t\t});\n\n\t\t\treturn results;\n\t\t} catch (error) {\n\t\t\tconsole.error('generatePromptVariantsRealtime failed', {\n\t\t\t\terror,\n\t\t\t\titemCount: items.length,\n\t\t\t\tmodel,\n\t\t\t});\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t// ── Batch path (cheap, slow, unbounded latency) ─────────────────\n\t// Only use directly for large (1000+), non-urgent jobs kicked off\n\t// from a background worker/cron — not from a request/response path.\n\tasync generatePromptVariantsBatch(\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\t\t\t\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});\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);\t\t\t\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\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,IAA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C,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;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"]}
|
|
@@ -5,9 +5,9 @@ interface AuthOptions {
|
|
|
5
5
|
autoRefresh?: boolean;
|
|
6
6
|
}
|
|
7
7
|
export declare function createAuthenticatedProcedure(options: AuthOptions): import("@trpc/server").TRPCProcedureBuilder<import("../context").LyxaHTTPContext, object, {
|
|
8
|
+
req: import("http").IncomingMessage;
|
|
8
9
|
res: import("http").ServerResponse<import("http").IncomingMessage>;
|
|
9
10
|
tokenType: TokenType | undefined;
|
|
10
|
-
req: import("http").IncomingMessage;
|
|
11
11
|
requestId: string | undefined;
|
|
12
12
|
entity: import("../context").EntityContext | undefined;
|
|
13
13
|
usedRefreshToken: boolean | undefined;
|
|
@@ -4,9 +4,9 @@ interface RoleProtectedOptions {
|
|
|
4
4
|
allowedRoles: string[];
|
|
5
5
|
}
|
|
6
6
|
export declare function createRoleProtectedProcedure(options: RoleProtectedOptions): import("@trpc/server").TRPCProcedureBuilder<import("../context").LyxaHTTPContext, object, {
|
|
7
|
+
req: import("http").IncomingMessage;
|
|
7
8
|
res: import("http").ServerResponse<import("http").IncomingMessage>;
|
|
8
9
|
tokenType: import("../../auth").TokenType | undefined;
|
|
9
|
-
req: import("http").IncomingMessage;
|
|
10
10
|
requestId: string | undefined;
|
|
11
11
|
entity: import("../context").EntityContext | undefined;
|
|
12
12
|
usedRefreshToken: boolean | undefined;
|
|
@@ -3,9 +3,9 @@ interface PhoneVerifiedOptions {
|
|
|
3
3
|
entityTypes: AuthEntityType[];
|
|
4
4
|
}
|
|
5
5
|
export declare function createPhoneVerifiedProcedure(options: PhoneVerifiedOptions): import("@trpc/server").TRPCProcedureBuilder<import("../context").LyxaHTTPContext, object, {
|
|
6
|
+
req: import("http").IncomingMessage;
|
|
6
7
|
res: import("http").ServerResponse<import("http").IncomingMessage>;
|
|
7
8
|
tokenType: import("../../auth").TokenType | undefined;
|
|
8
|
-
req: import("http").IncomingMessage;
|
|
9
9
|
requestId: string | undefined;
|
|
10
10
|
entity: import("../context").EntityContext | undefined;
|
|
11
11
|
usedRefreshToken: boolean | undefined;
|
|
@@ -5,9 +5,9 @@ interface AuthOptions {
|
|
|
5
5
|
autoRefresh?: boolean;
|
|
6
6
|
}
|
|
7
7
|
export declare function publicUserDecoder(options: AuthOptions): import("@trpc/server").TRPCProcedureBuilder<import("../context").LyxaHTTPContext, object, {
|
|
8
|
+
req: import("http").IncomingMessage;
|
|
8
9
|
res: import("http").ServerResponse<import("http").IncomingMessage>;
|
|
9
10
|
tokenType: TokenType | undefined;
|
|
10
|
-
req: import("http").IncomingMessage;
|
|
11
11
|
requestId: string | undefined;
|
|
12
12
|
entity: import("../context").EntityContext | undefined;
|
|
13
13
|
usedRefreshToken: boolean | undefined;
|
package/dist/types/README.md
CHANGED
package/dist/types/package.json
CHANGED
|
@@ -26,8 +26,8 @@ export declare const FilterSchema: z.ZodOptional<z.ZodObject<{
|
|
|
26
26
|
searchFields?: string[] | undefined;
|
|
27
27
|
} | undefined;
|
|
28
28
|
sort?: Record<string, 1 | -1> | undefined;
|
|
29
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
30
29
|
populate?: any;
|
|
30
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
31
31
|
query?: Record<string, any> | undefined;
|
|
32
32
|
}, {
|
|
33
33
|
search?: {
|
|
@@ -36,8 +36,8 @@ export declare const FilterSchema: z.ZodOptional<z.ZodObject<{
|
|
|
36
36
|
} | undefined;
|
|
37
37
|
sort?: Record<string, 1 | -1> | undefined;
|
|
38
38
|
size?: number | undefined;
|
|
39
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
40
39
|
populate?: any;
|
|
40
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
41
41
|
query?: Record<string, any> | undefined;
|
|
42
42
|
page?: number | undefined;
|
|
43
43
|
}>>;
|
|
@@ -736,12 +736,12 @@ export declare const GetByIdInputSchema: z.ZodObject<{
|
|
|
736
736
|
populate: z.ZodOptional<z.ZodUnion<[z.ZodType<any, z.ZodTypeDef, any>, z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">]>>;
|
|
737
737
|
}, "strip", z.ZodTypeAny, {
|
|
738
738
|
_id: import("mongoose").Types.ObjectId;
|
|
739
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
740
739
|
populate?: any;
|
|
740
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
741
741
|
}, {
|
|
742
742
|
_id: string | import("mongoose").Types.ObjectId;
|
|
743
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
744
743
|
populate?: any;
|
|
744
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
745
745
|
}>;
|
|
746
746
|
export type GetByIdInputDTO = DTO<typeof GetByIdInputSchema>;
|
|
747
747
|
export declare const GetProductByIdInputSchema: z.ZodObject<{
|
|
@@ -754,12 +754,12 @@ export declare const GetProductByIdInputSchema: z.ZodObject<{
|
|
|
754
754
|
_id: import("mongoose").Types.ObjectId;
|
|
755
755
|
withAttributeHiddenItems: boolean;
|
|
756
756
|
withAddonsHiddenItems: boolean;
|
|
757
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
758
757
|
populate?: any;
|
|
758
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
759
759
|
}, {
|
|
760
760
|
_id: string | import("mongoose").Types.ObjectId;
|
|
761
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
762
761
|
populate?: any;
|
|
762
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
763
763
|
withAttributeHiddenItems?: boolean | undefined;
|
|
764
764
|
withAddonsHiddenItems?: boolean | undefined;
|
|
765
765
|
}>;
|
|
@@ -773,15 +773,15 @@ export declare const GetOrderByIdInputSchema: z.ZodObject<{
|
|
|
773
773
|
userOrderCompletionScope: z.ZodOptional<z.ZodNativeEnum<typeof UserOrderCompletionScope>>;
|
|
774
774
|
}, "strip", z.ZodTypeAny, {
|
|
775
775
|
_id: import("mongoose").Types.ObjectId;
|
|
776
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
777
776
|
populate?: any;
|
|
777
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
778
778
|
groupByCategories?: boolean | undefined;
|
|
779
779
|
getParentCategory?: boolean | undefined;
|
|
780
780
|
userOrderCompletionScope?: UserOrderCompletionScope | undefined;
|
|
781
781
|
}, {
|
|
782
782
|
_id: string | import("mongoose").Types.ObjectId;
|
|
783
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
784
783
|
populate?: any;
|
|
784
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
785
785
|
groupByCategories?: boolean | undefined;
|
|
786
786
|
getParentCategory?: boolean | undefined;
|
|
787
787
|
userOrderCompletionScope?: UserOrderCompletionScope | undefined;
|
|
@@ -791,11 +791,11 @@ export declare const GetByTokenInputSchema: z.ZodOptional<z.ZodObject<{
|
|
|
791
791
|
select: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodLiteral<0>, z.ZodLiteral<1>]>>>;
|
|
792
792
|
populate: z.ZodOptional<z.ZodUnion<[z.ZodType<any, z.ZodTypeDef, any>, z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">]>>;
|
|
793
793
|
}, "strip", z.ZodTypeAny, {
|
|
794
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
795
794
|
populate?: any;
|
|
796
|
-
}, {
|
|
797
795
|
select?: Record<string, 0 | 1> | undefined;
|
|
796
|
+
}, {
|
|
798
797
|
populate?: any;
|
|
798
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
799
799
|
}>>;
|
|
800
800
|
export type GetByTokenInputDTO = DTO<typeof GetByTokenInputSchema>;
|
|
801
801
|
export declare const GetOneQuerySchema: z.ZodObject<{
|
|
@@ -803,12 +803,12 @@ export declare const GetOneQuerySchema: z.ZodObject<{
|
|
|
803
803
|
select: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodLiteral<0>, z.ZodLiteral<1>]>>>;
|
|
804
804
|
populate: z.ZodOptional<z.ZodUnion<[z.ZodType<any, z.ZodTypeDef, any>, z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">]>>;
|
|
805
805
|
}, "strip", z.ZodTypeAny, {
|
|
806
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
807
806
|
populate?: any;
|
|
807
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
808
808
|
query?: Record<string, any> | undefined;
|
|
809
809
|
}, {
|
|
810
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
811
810
|
populate?: any;
|
|
811
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
812
812
|
query?: Record<string, any> | undefined;
|
|
813
813
|
}>;
|
|
814
814
|
export type GetOneQueryDTO = DTO<typeof GetOneQuerySchema>;
|