llm-exe 2.3.1 → 2.3.2
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/index.d.mts +139 -14
- package/dist/index.d.ts +139 -14
- package/dist/index.js +932 -826
- package/dist/index.mjs +930 -826
- package/package.json +3 -1
package/dist/index.mjs
CHANGED
|
@@ -125,6 +125,10 @@ var BaseExecutor = class {
|
|
|
125
125
|
*/
|
|
126
126
|
__publicField(this, "hooks");
|
|
127
127
|
__publicField(this, "allowedHooks", [hookOnComplete, hookOnError, hookOnSuccess]);
|
|
128
|
+
/**
|
|
129
|
+
* @property maxHooksPerEvent - Maximum number of hooks allowed per event
|
|
130
|
+
*/
|
|
131
|
+
__publicField(this, "maxHooksPerEvent", 100);
|
|
128
132
|
this.id = uuidv4();
|
|
129
133
|
this.type = type;
|
|
130
134
|
this.name = name;
|
|
@@ -230,6 +234,11 @@ var BaseExecutor = class {
|
|
|
230
234
|
const _hooks = Array.isArray(hookInput) ? hookInput : [hookInput];
|
|
231
235
|
for (const hook of _hooks) {
|
|
232
236
|
if (hook && typeof hook === "function" && !this.hooks[hookKey].find((h) => h === hook)) {
|
|
237
|
+
if (this.hooks[hookKey].length >= this.maxHooksPerEvent) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
`Maximum number of hooks (${this.maxHooksPerEvent}) reached for event "${String(hookKey)}". Consider removing unused hooks or increasing the limit.`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
233
242
|
this.hooks[hookKey].push(hook);
|
|
234
243
|
}
|
|
235
244
|
}
|
|
@@ -257,10 +266,18 @@ var BaseExecutor = class {
|
|
|
257
266
|
}
|
|
258
267
|
once(eventName, fn) {
|
|
259
268
|
if (typeof fn !== "function") return this;
|
|
269
|
+
if (this.hooks[eventName] && this.hooks[eventName].length >= this.maxHooksPerEvent) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
`Maximum number of hooks (${this.maxHooksPerEvent}) reached for event "${String(eventName)}". Consider removing unused hooks or increasing the limit.`
|
|
272
|
+
);
|
|
273
|
+
}
|
|
260
274
|
const onceWrapper = (...args) => {
|
|
261
275
|
fn(...args);
|
|
262
276
|
this.off(eventName, onceWrapper);
|
|
263
277
|
};
|
|
278
|
+
if (!this.hooks[eventName]) {
|
|
279
|
+
this.hooks[eventName] = [];
|
|
280
|
+
}
|
|
264
281
|
this.hooks[eventName].push(onceWrapper);
|
|
265
282
|
return this;
|
|
266
283
|
}
|
|
@@ -271,6 +288,40 @@ var BaseExecutor = class {
|
|
|
271
288
|
getTraceId() {
|
|
272
289
|
return this.traceId;
|
|
273
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Clear all hooks for a specific event or all events
|
|
293
|
+
* Useful for preventing memory leaks in long-running processes
|
|
294
|
+
* @param eventName - The event name to clear hooks for
|
|
295
|
+
*/
|
|
296
|
+
clearHooks(eventName) {
|
|
297
|
+
if (eventName) {
|
|
298
|
+
if (this.hooks[eventName]) {
|
|
299
|
+
this.hooks[eventName] = [];
|
|
300
|
+
}
|
|
301
|
+
} else {
|
|
302
|
+
for (const key of this.allowedHooks) {
|
|
303
|
+
if (this.hooks[key]) {
|
|
304
|
+
this.hooks[key] = [];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Get the count of hooks for monitoring memory usage
|
|
312
|
+
* @param eventName - Optional event name to get count for
|
|
313
|
+
* @returns Hook count for specific event or all events
|
|
314
|
+
*/
|
|
315
|
+
getHookCount(eventName) {
|
|
316
|
+
if (eventName) {
|
|
317
|
+
return this.hooks[eventName]?.length || 0;
|
|
318
|
+
}
|
|
319
|
+
const counts = {};
|
|
320
|
+
for (const key of this.allowedHooks) {
|
|
321
|
+
counts[key] = this.hooks[key]?.length || 0;
|
|
322
|
+
}
|
|
323
|
+
return counts;
|
|
324
|
+
}
|
|
274
325
|
};
|
|
275
326
|
|
|
276
327
|
// src/utils/modules/inferFunctionName.ts
|
|
@@ -2057,6 +2108,63 @@ function getEnvironmentVariable(name) {
|
|
|
2057
2108
|
}
|
|
2058
2109
|
}
|
|
2059
2110
|
|
|
2111
|
+
// src/llm/output/_util.ts
|
|
2112
|
+
function formatOptions(response, handler) {
|
|
2113
|
+
const out = [];
|
|
2114
|
+
for (const item of response) {
|
|
2115
|
+
const result = handler(item);
|
|
2116
|
+
if (result) {
|
|
2117
|
+
out.push([result]);
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
return out;
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
// src/llm/output/openai.ts
|
|
2124
|
+
function formatResult(result) {
|
|
2125
|
+
const out = [];
|
|
2126
|
+
if (typeof result?.message?.content === "string") {
|
|
2127
|
+
out.push({
|
|
2128
|
+
type: "text",
|
|
2129
|
+
text: result.message.content
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
2132
|
+
if (result?.message?.tool_calls) {
|
|
2133
|
+
for (const call of result.message.tool_calls) {
|
|
2134
|
+
out.push({
|
|
2135
|
+
functionId: call.id,
|
|
2136
|
+
type: "function_use",
|
|
2137
|
+
name: call.function.name,
|
|
2138
|
+
input: maybeParseJSON(call.function.arguments)
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
return out;
|
|
2143
|
+
}
|
|
2144
|
+
function OutputOpenAIChat(result, _config) {
|
|
2145
|
+
const id = result.id;
|
|
2146
|
+
const name = result.model || _config?.options.model?.default || "openai.unknown";
|
|
2147
|
+
const created = result.created;
|
|
2148
|
+
const [_content, ..._options] = result?.choices || [];
|
|
2149
|
+
const stopReason = _content?.finish_reason;
|
|
2150
|
+
const content = formatResult(_content);
|
|
2151
|
+
const options = formatOptions(_options, formatResult);
|
|
2152
|
+
const usage = {
|
|
2153
|
+
output_tokens: result?.usage?.completion_tokens,
|
|
2154
|
+
input_tokens: result?.usage?.prompt_tokens,
|
|
2155
|
+
total_tokens: result?.usage?.total_tokens
|
|
2156
|
+
};
|
|
2157
|
+
return {
|
|
2158
|
+
id,
|
|
2159
|
+
name,
|
|
2160
|
+
created,
|
|
2161
|
+
usage,
|
|
2162
|
+
stopReason,
|
|
2163
|
+
content,
|
|
2164
|
+
options
|
|
2165
|
+
};
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2060
2168
|
// src/llm/config/openai/promptSanitizeMessageCallback.ts
|
|
2061
2169
|
function openaiPromptMessageCallback(_message) {
|
|
2062
2170
|
let message = { ..._message };
|
|
@@ -2090,38 +2198,127 @@ function openaiPromptSanitize(_messages, _inputBodyObj, _outputObj) {
|
|
|
2090
2198
|
return _messages.map(openaiPromptMessageCallback);
|
|
2091
2199
|
}
|
|
2092
2200
|
|
|
2093
|
-
// src/llm/
|
|
2094
|
-
var
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2201
|
+
// src/llm/output/_utils/cleanJsonSchemaFor.ts
|
|
2202
|
+
var providerFieldExclusions = {
|
|
2203
|
+
"openai.chat": ["default"],
|
|
2204
|
+
// fields to exclude for openai.chat, xai, deepseek
|
|
2205
|
+
"anthropic.chat": [],
|
|
2206
|
+
"google.chat": ["additionalProperties"]
|
|
2207
|
+
};
|
|
2208
|
+
function cleanJsonSchemaFor(schema = {}, provider) {
|
|
2209
|
+
const clone = deepClone(schema);
|
|
2210
|
+
if (Object.keys(clone).length === 0) {
|
|
2211
|
+
return {
|
|
2212
|
+
type: "object",
|
|
2213
|
+
properties: {},
|
|
2214
|
+
required: []
|
|
2215
|
+
};
|
|
2216
|
+
}
|
|
2217
|
+
const exclusions = providerFieldExclusions[provider] || [];
|
|
2218
|
+
function removeDisallowedFields(obj) {
|
|
2219
|
+
if (Array.isArray(obj)) {
|
|
2220
|
+
return obj.map(removeDisallowedFields);
|
|
2221
|
+
} else if (typeof obj === "object" && obj !== null) {
|
|
2222
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
2223
|
+
if (!exclusions.includes(key)) {
|
|
2224
|
+
acc[key] = removeDisallowedFields(obj[key]);
|
|
2225
|
+
}
|
|
2226
|
+
return acc;
|
|
2227
|
+
}, {});
|
|
2104
2228
|
}
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2229
|
+
return obj;
|
|
2230
|
+
}
|
|
2231
|
+
return removeDisallowedFields(clone);
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
// src/llm/config/openai/compatible.ts
|
|
2235
|
+
function createOpenAiCompatibleConfiguration(overrides) {
|
|
2236
|
+
const [apiKeyPropertyKey, apiKeyPropertyValue] = overrides.apiKeyMapping;
|
|
2237
|
+
const config = {
|
|
2238
|
+
key: overrides.key,
|
|
2239
|
+
provider: overrides.provider,
|
|
2240
|
+
endpoint: overrides.endpoint,
|
|
2241
|
+
options: {
|
|
2242
|
+
prompt: {},
|
|
2243
|
+
effort: {},
|
|
2244
|
+
topP: {},
|
|
2245
|
+
useJson: {},
|
|
2246
|
+
[apiKeyPropertyKey]: {
|
|
2247
|
+
default: getEnvironmentVariable(apiKeyPropertyValue)
|
|
2248
|
+
}
|
|
2112
2249
|
},
|
|
2113
|
-
|
|
2114
|
-
|
|
2250
|
+
method: "POST",
|
|
2251
|
+
headers: `{"Authorization":"Bearer {{${apiKeyPropertyKey}}}", "Content-Type": "application/json" }`,
|
|
2252
|
+
mapBody: {
|
|
2253
|
+
prompt: {
|
|
2254
|
+
key: "messages",
|
|
2255
|
+
transform: openaiPromptSanitize
|
|
2256
|
+
},
|
|
2257
|
+
model: {
|
|
2258
|
+
key: "model"
|
|
2259
|
+
},
|
|
2260
|
+
topP: {
|
|
2261
|
+
key: "top_p"
|
|
2262
|
+
},
|
|
2263
|
+
useJson: {
|
|
2264
|
+
key: "response_format.type",
|
|
2265
|
+
transform: (v) => v ? "json_object" : "text"
|
|
2266
|
+
},
|
|
2267
|
+
effort: {
|
|
2268
|
+
key: "reasoning_effort",
|
|
2269
|
+
transform: (v, _s) => {
|
|
2270
|
+
if (
|
|
2271
|
+
// only supported reasoning models
|
|
2272
|
+
["gpt-5"].includes(_s.model) && typeof v === "string" && ["minimal", "low", "medium", "high"].includes(v)
|
|
2273
|
+
) {
|
|
2274
|
+
return v;
|
|
2275
|
+
}
|
|
2276
|
+
return void 0;
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2115
2279
|
},
|
|
2116
|
-
|
|
2117
|
-
|
|
2280
|
+
mapOptions: {
|
|
2281
|
+
jsonSchema: (schema, options, currentInput) => ({
|
|
2282
|
+
response_format: {
|
|
2283
|
+
...currentInput?.response_format || {},
|
|
2284
|
+
type: "json_schema",
|
|
2285
|
+
json_schema: {
|
|
2286
|
+
name: "output",
|
|
2287
|
+
strict: !!options?.functionCallStrictInput,
|
|
2288
|
+
schema: cleanJsonSchemaFor(schema, "openai.chat")
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
}),
|
|
2292
|
+
functionCall: (call) => {
|
|
2293
|
+
if (call === "any") return { tool_choice: "required" };
|
|
2294
|
+
if (call === "none") return { tool_choice: "none" };
|
|
2295
|
+
if (call === "auto") return { tool_choice: "auto" };
|
|
2296
|
+
return { tool_choice: call };
|
|
2297
|
+
},
|
|
2298
|
+
functions: (functions, options) => ({
|
|
2299
|
+
tools: functions.map((f) => ({
|
|
2300
|
+
type: "function",
|
|
2301
|
+
function: {
|
|
2302
|
+
name: f.name,
|
|
2303
|
+
description: f.description,
|
|
2304
|
+
parameters: cleanJsonSchemaFor(f.parameters, "openai.chat"),
|
|
2305
|
+
strict: !!options?.functionCallStrictInput
|
|
2306
|
+
}
|
|
2307
|
+
}))
|
|
2308
|
+
})
|
|
2118
2309
|
},
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2310
|
+
transformResponse: overrides.transformResponse ?? OutputOpenAIChat
|
|
2311
|
+
};
|
|
2312
|
+
return config;
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
// src/llm/config/openai/index.ts
|
|
2316
|
+
var openAiChatV1 = createOpenAiCompatibleConfiguration({
|
|
2317
|
+
key: "openai.chat.v1",
|
|
2318
|
+
provider: "openai.chat",
|
|
2319
|
+
endpoint: `https://api.openai.com/v1/chat/completions`,
|
|
2320
|
+
apiKeyMapping: ["openAiApiKey", "OPENAI_API_KEY"]
|
|
2321
|
+
});
|
|
2125
2322
|
var openAiChatMockV1 = {
|
|
2126
2323
|
key: "openai.chat-mock.v1",
|
|
2127
2324
|
provider: "openai.chat-mock",
|
|
@@ -2148,9 +2345,10 @@ var openAiChatMockV1 = {
|
|
|
2148
2345
|
},
|
|
2149
2346
|
useJson: {
|
|
2150
2347
|
key: "response_format.type",
|
|
2151
|
-
|
|
2348
|
+
transform: (v) => v ? "json_object" : "text"
|
|
2152
2349
|
}
|
|
2153
|
-
}
|
|
2350
|
+
},
|
|
2351
|
+
transformResponse: OutputOpenAIChat
|
|
2154
2352
|
};
|
|
2155
2353
|
var openai = {
|
|
2156
2354
|
"openai.chat.v1": openAiChatV1,
|
|
@@ -2228,8 +2426,75 @@ function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
|
|
|
2228
2426
|
].map(anthropicPromptMessageCallback);
|
|
2229
2427
|
}
|
|
2230
2428
|
|
|
2429
|
+
// src/llm/output/claude.ts
|
|
2430
|
+
function formatResult2(response) {
|
|
2431
|
+
const content = response?.content || [];
|
|
2432
|
+
const out = [];
|
|
2433
|
+
for (let i = 0; i < content.length; i++) {
|
|
2434
|
+
const result = content[i];
|
|
2435
|
+
if (result.type === "text") {
|
|
2436
|
+
out.push({
|
|
2437
|
+
type: "text",
|
|
2438
|
+
text: result.text
|
|
2439
|
+
});
|
|
2440
|
+
} else if (result.type === "tool_use") {
|
|
2441
|
+
out.push({
|
|
2442
|
+
functionId: result.id,
|
|
2443
|
+
type: "function_use",
|
|
2444
|
+
name: result.name,
|
|
2445
|
+
input: result.input
|
|
2446
|
+
});
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
return out;
|
|
2450
|
+
}
|
|
2451
|
+
function OutputAnthropicClaude3Chat(result, _config) {
|
|
2452
|
+
const id = result.id;
|
|
2453
|
+
const name = result.model || _config?.options.model?.default || "anthropic.unknown";
|
|
2454
|
+
const stopReason = result.stop_reason;
|
|
2455
|
+
const content = formatResult2(result);
|
|
2456
|
+
const usage = {
|
|
2457
|
+
input_tokens: result?.usage?.input_tokens,
|
|
2458
|
+
output_tokens: result?.usage?.output_tokens,
|
|
2459
|
+
total_tokens: result?.usage?.input_tokens + result?.usage?.output_tokens
|
|
2460
|
+
};
|
|
2461
|
+
return {
|
|
2462
|
+
id,
|
|
2463
|
+
name,
|
|
2464
|
+
created: Date.now(),
|
|
2465
|
+
usage,
|
|
2466
|
+
stopReason,
|
|
2467
|
+
content
|
|
2468
|
+
};
|
|
2469
|
+
}
|
|
2470
|
+
|
|
2471
|
+
// src/llm/output/llama.ts
|
|
2472
|
+
function OutputMetaLlama3Chat(result, _config) {
|
|
2473
|
+
const id = uuidv4();
|
|
2474
|
+
const name = _config?.options?.model?.default || "meta";
|
|
2475
|
+
const created = (/* @__PURE__ */ new Date()).getTime();
|
|
2476
|
+
const stopReason = result.stop_reason;
|
|
2477
|
+
const content = [
|
|
2478
|
+
{ type: "text", text: result.generation }
|
|
2479
|
+
];
|
|
2480
|
+
const usage = {
|
|
2481
|
+
output_tokens: result?.generation_token_count,
|
|
2482
|
+
input_tokens: result?.prompt_token_count,
|
|
2483
|
+
total_tokens: result?.generation_token_count + result?.prompt_token_count
|
|
2484
|
+
};
|
|
2485
|
+
return {
|
|
2486
|
+
id,
|
|
2487
|
+
name,
|
|
2488
|
+
created,
|
|
2489
|
+
usage,
|
|
2490
|
+
stopReason,
|
|
2491
|
+
content,
|
|
2492
|
+
options: []
|
|
2493
|
+
};
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2231
2496
|
// src/llm/config/bedrock/index.ts
|
|
2232
|
-
var
|
|
2497
|
+
var ANTHROPIC_BEDROCK_VERSION = "bedrock-2023-05-31";
|
|
2233
2498
|
var amazonAnthropicChatV1 = {
|
|
2234
2499
|
key: "amazon:anthropic.chat.v1",
|
|
2235
2500
|
provider: "amazon:anthropic.chat",
|
|
@@ -2250,7 +2515,7 @@ var amazonAnthropicChatV1 = {
|
|
|
2250
2515
|
mapBody: {
|
|
2251
2516
|
prompt: {
|
|
2252
2517
|
key: "messages",
|
|
2253
|
-
|
|
2518
|
+
transform: anthropicPromptSanitize
|
|
2254
2519
|
},
|
|
2255
2520
|
topP: {
|
|
2256
2521
|
key: "top_p"
|
|
@@ -2261,9 +2526,26 @@ var amazonAnthropicChatV1 = {
|
|
|
2261
2526
|
},
|
|
2262
2527
|
anthropic_version: {
|
|
2263
2528
|
key: "anthropic_version",
|
|
2264
|
-
default:
|
|
2529
|
+
default: ANTHROPIC_BEDROCK_VERSION
|
|
2265
2530
|
}
|
|
2266
|
-
}
|
|
2531
|
+
},
|
|
2532
|
+
mapOptions: {
|
|
2533
|
+
functionCall: (call, _options) => {
|
|
2534
|
+
if (call === "none") return { _clearFunctions: true };
|
|
2535
|
+
if (call === "auto" || call === "any") {
|
|
2536
|
+
return { tool_choice: { type: call } };
|
|
2537
|
+
}
|
|
2538
|
+
return { tool_choice: call };
|
|
2539
|
+
},
|
|
2540
|
+
functions: (functions) => ({
|
|
2541
|
+
tools: functions.map((f) => ({
|
|
2542
|
+
name: f.name,
|
|
2543
|
+
description: f.description,
|
|
2544
|
+
input_schema: cleanJsonSchemaFor(f.parameters, "anthropic.chat")
|
|
2545
|
+
}))
|
|
2546
|
+
})
|
|
2547
|
+
},
|
|
2548
|
+
transformResponse: OutputAnthropicClaude3Chat
|
|
2267
2549
|
};
|
|
2268
2550
|
var amazonMetaChatV1 = {
|
|
2269
2551
|
key: "amazon:meta.chat.v1",
|
|
@@ -2285,7 +2567,7 @@ var amazonMetaChatV1 = {
|
|
|
2285
2567
|
mapBody: {
|
|
2286
2568
|
prompt: {
|
|
2287
2569
|
key: "prompt",
|
|
2288
|
-
|
|
2570
|
+
transform: (messages) => {
|
|
2289
2571
|
if (typeof messages === "string") {
|
|
2290
2572
|
return messages;
|
|
2291
2573
|
} else {
|
|
@@ -2305,7 +2587,8 @@ var amazonMetaChatV1 = {
|
|
|
2305
2587
|
key: "max_gen_len",
|
|
2306
2588
|
default: 2048
|
|
2307
2589
|
}
|
|
2308
|
-
}
|
|
2590
|
+
},
|
|
2591
|
+
transformResponse: OutputMetaLlama3Chat
|
|
2309
2592
|
};
|
|
2310
2593
|
var bedrock = {
|
|
2311
2594
|
"amazon:anthropic.chat.v1": amazonAnthropicChatV1,
|
|
@@ -2344,24 +2627,65 @@ var anthropicChatV1 = {
|
|
|
2344
2627
|
},
|
|
2345
2628
|
prompt: {
|
|
2346
2629
|
key: "messages",
|
|
2347
|
-
|
|
2630
|
+
transform: anthropicPromptSanitize
|
|
2631
|
+
},
|
|
2632
|
+
temperature: {
|
|
2633
|
+
key: "temperature"
|
|
2634
|
+
},
|
|
2635
|
+
topP: {
|
|
2636
|
+
key: "top_p"
|
|
2637
|
+
},
|
|
2638
|
+
topK: {
|
|
2639
|
+
key: "top_k"
|
|
2640
|
+
},
|
|
2641
|
+
stopSequences: {
|
|
2642
|
+
key: "stop_sequences"
|
|
2643
|
+
},
|
|
2644
|
+
stream: {
|
|
2645
|
+
key: "stream"
|
|
2646
|
+
},
|
|
2647
|
+
metadata: {
|
|
2648
|
+
key: "metadata"
|
|
2649
|
+
},
|
|
2650
|
+
serviceTier: {
|
|
2651
|
+
key: "service_tier"
|
|
2652
|
+
// Map camelCase to snake_case
|
|
2348
2653
|
}
|
|
2349
|
-
}
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2654
|
+
},
|
|
2655
|
+
mapOptions: {
|
|
2656
|
+
functionCall: (call, _options) => {
|
|
2657
|
+
if (call === "none") return { _clearFunctions: true };
|
|
2658
|
+
if (call === "auto" || call === "any") {
|
|
2659
|
+
return { tool_choice: { type: call } };
|
|
2660
|
+
}
|
|
2661
|
+
return { tool_choice: call };
|
|
2662
|
+
},
|
|
2663
|
+
functions: (functions) => ({
|
|
2664
|
+
tools: functions.map((f) => ({
|
|
2665
|
+
name: f.name,
|
|
2666
|
+
description: f.description,
|
|
2667
|
+
input_schema: cleanJsonSchemaFor(f.parameters, "anthropic.chat")
|
|
2668
|
+
}))
|
|
2669
|
+
})
|
|
2670
|
+
},
|
|
2671
|
+
transformResponse: OutputAnthropicClaude3Chat
|
|
2672
|
+
};
|
|
2673
|
+
var anthropic = {
|
|
2674
|
+
"anthropic.chat.v1": anthropicChatV1,
|
|
2675
|
+
// Claude 4 models (latest generation)
|
|
2676
|
+
"anthropic.claude-sonnet-4": withDefaultModel(
|
|
2358
2677
|
anthropicChatV1,
|
|
2359
2678
|
"claude-sonnet-4-0"
|
|
2360
2679
|
),
|
|
2680
|
+
"anthropic.claude-opus-4": withDefaultModel(
|
|
2681
|
+
anthropicChatV1,
|
|
2682
|
+
"claude-opus-4-0"
|
|
2683
|
+
),
|
|
2361
2684
|
"anthropic.claude-3-7-sonnet": withDefaultModel(
|
|
2362
2685
|
anthropicChatV1,
|
|
2363
|
-
"claude-3-7-sonnet-
|
|
2686
|
+
"claude-3-7-sonnet-20250219"
|
|
2364
2687
|
),
|
|
2688
|
+
// Claude 3.5 models
|
|
2365
2689
|
"anthropic.claude-3-5-sonnet": withDefaultModel(
|
|
2366
2690
|
anthropicChatV1,
|
|
2367
2691
|
"claude-3-5-sonnet-latest"
|
|
@@ -2373,42 +2697,21 @@ var anthropic = {
|
|
|
2373
2697
|
// Deprecated
|
|
2374
2698
|
"anthropic.claude-3-opus": withDefaultModel(
|
|
2375
2699
|
anthropicChatV1,
|
|
2376
|
-
"claude-3-opus-
|
|
2700
|
+
"claude-3-opus-20240229"
|
|
2701
|
+
),
|
|
2702
|
+
"anthropic.claude-3-haiku": withDefaultModel(
|
|
2703
|
+
anthropicChatV1,
|
|
2704
|
+
"claude-3-haiku-20240307"
|
|
2377
2705
|
)
|
|
2378
2706
|
};
|
|
2379
2707
|
|
|
2380
2708
|
// src/llm/config/x/index.ts
|
|
2381
|
-
var xaiChatV1 = {
|
|
2709
|
+
var xaiChatV1 = createOpenAiCompatibleConfiguration({
|
|
2382
2710
|
key: "xai.chat.v1",
|
|
2383
2711
|
provider: "xai.chat",
|
|
2384
2712
|
endpoint: `https://api.x.ai/v1/chat/completions`,
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
topP: {},
|
|
2388
|
-
useJson: {},
|
|
2389
|
-
xAiApiKey: {
|
|
2390
|
-
default: getEnvironmentVariable("XAI_API_KEY")
|
|
2391
|
-
}
|
|
2392
|
-
},
|
|
2393
|
-
method: "POST",
|
|
2394
|
-
headers: `{"Authorization":"Bearer {{xAiApiKey}}", "Content-Type": "application/json" }`,
|
|
2395
|
-
mapBody: {
|
|
2396
|
-
prompt: {
|
|
2397
|
-
key: "messages",
|
|
2398
|
-
sanitize: openaiPromptSanitize
|
|
2399
|
-
},
|
|
2400
|
-
model: {
|
|
2401
|
-
key: "model"
|
|
2402
|
-
},
|
|
2403
|
-
topP: {
|
|
2404
|
-
key: "top_p"
|
|
2405
|
-
},
|
|
2406
|
-
useJson: {
|
|
2407
|
-
key: "response_format.type",
|
|
2408
|
-
sanitize: (v) => v ? "json_object" : "text"
|
|
2409
|
-
}
|
|
2410
|
-
}
|
|
2411
|
-
};
|
|
2713
|
+
apiKeyMapping: ["xAiApiKey", "XAI_API_KEY"]
|
|
2714
|
+
});
|
|
2412
2715
|
var xai = {
|
|
2413
2716
|
"xai.chat.v1": xaiChatV1,
|
|
2414
2717
|
"xai.grok-2": withDefaultModel(xaiChatV1, "grok-2-latest"),
|
|
@@ -2416,6 +2719,78 @@ var xai = {
|
|
|
2416
2719
|
"xai.grok-4": withDefaultModel(xaiChatV1, "grok-4")
|
|
2417
2720
|
};
|
|
2418
2721
|
|
|
2722
|
+
// src/llm/output/_utils/combineJsonl.ts
|
|
2723
|
+
function combineJsonl(jsonl) {
|
|
2724
|
+
const lines = jsonl.trim().split("\n").map((line) => line.trim()).filter((line) => line.length > 0).map((line) => {
|
|
2725
|
+
try {
|
|
2726
|
+
return JSON.parse(line);
|
|
2727
|
+
} catch (e) {
|
|
2728
|
+
throw new Error(`Invalid JSON: ${line}`);
|
|
2729
|
+
}
|
|
2730
|
+
});
|
|
2731
|
+
if (lines.length === 0) {
|
|
2732
|
+
throw new Error("No JSON lines provided.");
|
|
2733
|
+
}
|
|
2734
|
+
lines.sort(
|
|
2735
|
+
(a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
|
|
2736
|
+
);
|
|
2737
|
+
let combinedContent = lines.map((line) => line?.message?.content ?? "").join("");
|
|
2738
|
+
combinedContent = combinedContent.replace(/\\u003c/g, "<").replace(/\\u003e/g, ">");
|
|
2739
|
+
const finalLine = lines.find((line) => line.done === true);
|
|
2740
|
+
if (!finalLine) {
|
|
2741
|
+
throw new Error("No line found where done = true.");
|
|
2742
|
+
}
|
|
2743
|
+
const result = {
|
|
2744
|
+
model: finalLine.model,
|
|
2745
|
+
created_at: finalLine.created_at,
|
|
2746
|
+
message: {
|
|
2747
|
+
role: finalLine?.message?.role || "assistant",
|
|
2748
|
+
content: combinedContent
|
|
2749
|
+
},
|
|
2750
|
+
done_reason: finalLine.done_reason,
|
|
2751
|
+
done: finalLine.done,
|
|
2752
|
+
total_duration: finalLine.total_duration,
|
|
2753
|
+
load_duration: finalLine.load_duration,
|
|
2754
|
+
prompt_eval_count: finalLine.prompt_eval_count,
|
|
2755
|
+
prompt_eval_duration: finalLine.prompt_eval_duration,
|
|
2756
|
+
eval_count: finalLine.eval_count,
|
|
2757
|
+
eval_duration: finalLine.eval_duration
|
|
2758
|
+
};
|
|
2759
|
+
const content = {
|
|
2760
|
+
type: "text",
|
|
2761
|
+
text: combinedContent
|
|
2762
|
+
};
|
|
2763
|
+
return {
|
|
2764
|
+
lines,
|
|
2765
|
+
result,
|
|
2766
|
+
content
|
|
2767
|
+
};
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
// src/llm/output/ollama.ts
|
|
2771
|
+
function OutputOllamaChat(result, _config) {
|
|
2772
|
+
const combined = combineJsonl(result);
|
|
2773
|
+
const id = `${combined.result.model}.${combined.result.created_at}`;
|
|
2774
|
+
const name = combined.result.model || _config?.options.model?.default || "ollama.unknown";
|
|
2775
|
+
const created = (/* @__PURE__ */ new Date(`${combined.result.created_at}`)).getTime();
|
|
2776
|
+
const stopReason = `${combined?.result?.done_reason || "stop"}`;
|
|
2777
|
+
const content = [combined.content];
|
|
2778
|
+
const usage = {
|
|
2779
|
+
output_tokens: 0,
|
|
2780
|
+
input_tokens: 0,
|
|
2781
|
+
total_tokens: 0
|
|
2782
|
+
};
|
|
2783
|
+
return {
|
|
2784
|
+
id,
|
|
2785
|
+
name,
|
|
2786
|
+
created,
|
|
2787
|
+
usage,
|
|
2788
|
+
stopReason,
|
|
2789
|
+
content,
|
|
2790
|
+
options: []
|
|
2791
|
+
};
|
|
2792
|
+
}
|
|
2793
|
+
|
|
2419
2794
|
// src/llm/config/ollama/index.ts
|
|
2420
2795
|
var ollamaChatV1 = {
|
|
2421
2796
|
key: "ollama.chat.v1",
|
|
@@ -2429,7 +2804,7 @@ var ollamaChatV1 = {
|
|
|
2429
2804
|
mapBody: {
|
|
2430
2805
|
prompt: {
|
|
2431
2806
|
key: "messages",
|
|
2432
|
-
|
|
2807
|
+
transform: (v) => {
|
|
2433
2808
|
if (typeof v === "string") {
|
|
2434
2809
|
return [{ role: "user", content: v }];
|
|
2435
2810
|
}
|
|
@@ -2439,7 +2814,8 @@ var ollamaChatV1 = {
|
|
|
2439
2814
|
model: {
|
|
2440
2815
|
key: "model"
|
|
2441
2816
|
}
|
|
2442
|
-
}
|
|
2817
|
+
},
|
|
2818
|
+
transformResponse: OutputOllamaChat
|
|
2443
2819
|
};
|
|
2444
2820
|
var ollama = {
|
|
2445
2821
|
"ollama.chat.v1": ollamaChatV1,
|
|
@@ -2481,490 +2857,60 @@ function googleGeminiPromptMessageCallback(_message) {
|
|
|
2481
2857
|
const toolsArr = Array.isArray(function_call) ? function_call : [function_call];
|
|
2482
2858
|
role = "model";
|
|
2483
2859
|
parts.push(
|
|
2484
|
-
...toolsArr.map((call) => {
|
|
2485
|
-
const { name, arguments: input } = call;
|
|
2486
|
-
return {
|
|
2487
|
-
functionCall: {
|
|
2488
|
-
name,
|
|
2489
|
-
args: maybeParseJSON(input)
|
|
2490
|
-
}
|
|
2491
|
-
};
|
|
2492
|
-
})
|
|
2493
|
-
);
|
|
2494
|
-
delete message.function_call;
|
|
2495
|
-
}
|
|
2496
|
-
return {
|
|
2497
|
-
role,
|
|
2498
|
-
parts
|
|
2499
|
-
};
|
|
2500
|
-
}
|
|
2501
|
-
|
|
2502
|
-
// src/llm/config/google/promptSanitize.ts
|
|
2503
|
-
function googleGeminiPromptSanitize(_messages, _inputBodyObj, _outputObj) {
|
|
2504
|
-
if (typeof _messages === "string") {
|
|
2505
|
-
return [{ role: "user", parts: [{ text: _messages }] }];
|
|
2506
|
-
}
|
|
2507
|
-
if (Array.isArray(_messages)) {
|
|
2508
|
-
if (_messages.length === 0) {
|
|
2509
|
-
throw new Error("Empty messages array");
|
|
2510
|
-
}
|
|
2511
|
-
if (_messages.length === 1 && _messages[0].role === "system") {
|
|
2512
|
-
return [{ role: "user", parts: [{ text: _messages[0].content }] }];
|
|
2513
|
-
}
|
|
2514
|
-
const hasSystemInstruction = _messages.some(
|
|
2515
|
-
(message) => message.role === "system"
|
|
2516
|
-
);
|
|
2517
|
-
if (hasSystemInstruction) {
|
|
2518
|
-
const theSystemInstructions = _messages.filter(
|
|
2519
|
-
(message) => message.role === "system"
|
|
2520
|
-
);
|
|
2521
|
-
const withoutSystemInstructions = _messages.filter(
|
|
2522
|
-
(message) => message.role !== "system"
|
|
2523
|
-
);
|
|
2524
|
-
_outputObj.system_instruction = {
|
|
2525
|
-
parts: theSystemInstructions.map((message) => ({
|
|
2526
|
-
text: message.content
|
|
2527
|
-
}))
|
|
2528
|
-
};
|
|
2529
|
-
return withoutSystemInstructions.map(googleGeminiPromptMessageCallback);
|
|
2530
|
-
}
|
|
2531
|
-
return _messages.map(googleGeminiPromptMessageCallback);
|
|
2532
|
-
}
|
|
2533
|
-
throw new Error("Invalid messages format");
|
|
2534
|
-
}
|
|
2535
|
-
|
|
2536
|
-
// src/llm/config/google/index.ts
|
|
2537
|
-
var googleGeminiChatV1 = {
|
|
2538
|
-
key: "google.chat.v1",
|
|
2539
|
-
provider: "google.chat",
|
|
2540
|
-
endpoint: `https://generativelanguage.googleapis.com/v1beta/models/{{model}}:generateContent?key={{geminiApiKey}}`,
|
|
2541
|
-
options: {
|
|
2542
|
-
prompt: {},
|
|
2543
|
-
// topP: {},
|
|
2544
|
-
geminiApiKey: {
|
|
2545
|
-
default: getEnvironmentVariable("GEMINI_API_KEY")
|
|
2546
|
-
}
|
|
2547
|
-
},
|
|
2548
|
-
method: "POST",
|
|
2549
|
-
headers: `{"Content-Type": "application/json" }`,
|
|
2550
|
-
mapBody: {
|
|
2551
|
-
prompt: {
|
|
2552
|
-
key: "contents",
|
|
2553
|
-
sanitize: googleGeminiPromptSanitize
|
|
2554
|
-
}
|
|
2555
|
-
// topP: {
|
|
2556
|
-
// key: "top_p",
|
|
2557
|
-
// }
|
|
2558
|
-
}
|
|
2559
|
-
};
|
|
2560
|
-
var google = {
|
|
2561
|
-
"google.chat.v1": googleGeminiChatV1,
|
|
2562
|
-
"google.gemini-2.0-flash": withDefaultModel(
|
|
2563
|
-
googleGeminiChatV1,
|
|
2564
|
-
"gemini-2.0-flash"
|
|
2565
|
-
),
|
|
2566
|
-
"google.gemini-2.0-flash-lite": withDefaultModel(
|
|
2567
|
-
googleGeminiChatV1,
|
|
2568
|
-
"gemini-2.0-flash-lite"
|
|
2569
|
-
),
|
|
2570
|
-
"google.gemini-2.5-flash": withDefaultModel(
|
|
2571
|
-
googleGeminiChatV1,
|
|
2572
|
-
"gemini-2.5-flash"
|
|
2573
|
-
),
|
|
2574
|
-
"google.gemini-2.5-flash-lite": withDefaultModel(
|
|
2575
|
-
googleGeminiChatV1,
|
|
2576
|
-
"gemini-2.5-flash-lite"
|
|
2577
|
-
),
|
|
2578
|
-
"google.gemini-1.5-pro": withDefaultModel(
|
|
2579
|
-
googleGeminiChatV1,
|
|
2580
|
-
"gemini-1.5-pro"
|
|
2581
|
-
),
|
|
2582
|
-
"google.gemini-2.5-pro": withDefaultModel(
|
|
2583
|
-
googleGeminiChatV1,
|
|
2584
|
-
"gemini-2.5-pro"
|
|
2585
|
-
)
|
|
2586
|
-
};
|
|
2587
|
-
|
|
2588
|
-
// src/llm/config/deepseek/index.ts
|
|
2589
|
-
var deepseekChatV1 = {
|
|
2590
|
-
key: "deepseek.chat.v1",
|
|
2591
|
-
provider: "deepseek.chat",
|
|
2592
|
-
endpoint: `https://api.deepseek.com/v1/chat/completions`,
|
|
2593
|
-
options: {
|
|
2594
|
-
prompt: {},
|
|
2595
|
-
topP: {},
|
|
2596
|
-
useJson: {},
|
|
2597
|
-
deepseekApiKey: {
|
|
2598
|
-
default: getEnvironmentVariable("DEEPSEEK_API_KEY")
|
|
2599
|
-
}
|
|
2600
|
-
},
|
|
2601
|
-
method: "POST",
|
|
2602
|
-
headers: `{"Authorization":"Bearer {{deepseekApiKey}}", "Content-Type": "application/json" }`,
|
|
2603
|
-
mapBody: {
|
|
2604
|
-
prompt: {
|
|
2605
|
-
key: "messages",
|
|
2606
|
-
sanitize: openaiPromptSanitize
|
|
2607
|
-
},
|
|
2608
|
-
model: {
|
|
2609
|
-
key: "model"
|
|
2610
|
-
},
|
|
2611
|
-
topP: {
|
|
2612
|
-
key: "top_p"
|
|
2613
|
-
},
|
|
2614
|
-
useJson: {
|
|
2615
|
-
key: "response_format.type",
|
|
2616
|
-
sanitize: (v) => v ? "json_object" : "text"
|
|
2617
|
-
}
|
|
2618
|
-
}
|
|
2619
|
-
};
|
|
2620
|
-
var deepseek = {
|
|
2621
|
-
"deepseek.chat.v1": deepseekChatV1,
|
|
2622
|
-
"deepseek.chat": withDefaultModel(deepseekChatV1, "deepseek-chat")
|
|
2623
|
-
};
|
|
2624
|
-
|
|
2625
|
-
// src/llm/config.ts
|
|
2626
|
-
var configs = {
|
|
2627
|
-
...openai,
|
|
2628
|
-
...anthropic,
|
|
2629
|
-
...bedrock,
|
|
2630
|
-
...xai,
|
|
2631
|
-
...ollama,
|
|
2632
|
-
...google,
|
|
2633
|
-
...deepseek
|
|
2634
|
-
};
|
|
2635
|
-
function getLlmConfig(provider) {
|
|
2636
|
-
if (!provider) {
|
|
2637
|
-
throw new LlmExeError(`Missing provider`, "unknown", {
|
|
2638
|
-
error: "Missing provider",
|
|
2639
|
-
resolution: "Provide a valid provider"
|
|
2640
|
-
});
|
|
2641
|
-
}
|
|
2642
|
-
const pick2 = configs[provider];
|
|
2643
|
-
if (pick2) {
|
|
2644
|
-
return pick2;
|
|
2645
|
-
}
|
|
2646
|
-
throw new LlmExeError(`Invalid provider: ${provider}`, "unknown", {
|
|
2647
|
-
provider,
|
|
2648
|
-
error: `Invalid provider: ${provider}`,
|
|
2649
|
-
resolution: "Provide a valid provider"
|
|
2650
|
-
});
|
|
2651
|
-
}
|
|
2652
|
-
|
|
2653
|
-
// src/llm/output/_utils/getResultContent.ts
|
|
2654
|
-
function getResultContent(result, index) {
|
|
2655
|
-
if (typeof index === "number" && index > 0) {
|
|
2656
|
-
const arr = result?.options || [];
|
|
2657
|
-
const val = arr[index];
|
|
2658
|
-
return val ? val : [];
|
|
2659
|
-
}
|
|
2660
|
-
return [...result.content];
|
|
2661
|
-
}
|
|
2662
|
-
|
|
2663
|
-
// src/llm/output/_utils/getResultText.ts
|
|
2664
|
-
function getResultText(result, index) {
|
|
2665
|
-
if (typeof index === "number" && index > 0) {
|
|
2666
|
-
const arr = result?.options || [];
|
|
2667
|
-
const val = arr[index];
|
|
2668
|
-
return isOutputResultContentText(val?.[0]) ? val[0]?.text : "";
|
|
2669
|
-
}
|
|
2670
|
-
return isOutputResultContentText(result?.content?.[0]) ? result.content[0]?.text : "";
|
|
2671
|
-
}
|
|
2672
|
-
|
|
2673
|
-
// src/llm/output/base.ts
|
|
2674
|
-
function BaseLlmOutput2(result) {
|
|
2675
|
-
const __result = Object.freeze({
|
|
2676
|
-
id: result.id || uuidv4(),
|
|
2677
|
-
name: result.name,
|
|
2678
|
-
usage: result.usage,
|
|
2679
|
-
stopReason: result.stopReason,
|
|
2680
|
-
options: [...result?.options || []],
|
|
2681
|
-
content: [...result.content],
|
|
2682
|
-
created: result?.created || (/* @__PURE__ */ new Date()).getTime()
|
|
2683
|
-
});
|
|
2684
|
-
function getResult() {
|
|
2685
|
-
return {
|
|
2686
|
-
id: __result.id,
|
|
2687
|
-
name: __result.name,
|
|
2688
|
-
created: __result.created,
|
|
2689
|
-
usage: __result.usage,
|
|
2690
|
-
options: __result.options,
|
|
2691
|
-
content: __result.content,
|
|
2692
|
-
stopReason: __result.stopReason
|
|
2693
|
-
};
|
|
2694
|
-
}
|
|
2695
|
-
return {
|
|
2696
|
-
getResultContent: (index) => getResultContent(__result, index),
|
|
2697
|
-
getResultText: (index) => getResultText(__result, index),
|
|
2698
|
-
getResult
|
|
2699
|
-
};
|
|
2700
|
-
}
|
|
2701
|
-
|
|
2702
|
-
// src/llm/output/_util.ts
|
|
2703
|
-
function normalizeFunctionCall(input, provider) {
|
|
2704
|
-
if (input === "any") {
|
|
2705
|
-
if (provider === "openai") {
|
|
2706
|
-
return "required";
|
|
2707
|
-
}
|
|
2708
|
-
}
|
|
2709
|
-
return input;
|
|
2710
|
-
}
|
|
2711
|
-
function formatOptions(response, handler) {
|
|
2712
|
-
const out = [];
|
|
2713
|
-
for (const item of response) {
|
|
2714
|
-
const result = handler(item);
|
|
2715
|
-
if (result) {
|
|
2716
|
-
out.push([result]);
|
|
2717
|
-
}
|
|
2718
|
-
}
|
|
2719
|
-
return out;
|
|
2720
|
-
}
|
|
2721
|
-
|
|
2722
|
-
// src/llm/output/openai.ts
|
|
2723
|
-
function formatResult(result) {
|
|
2724
|
-
const out = [];
|
|
2725
|
-
if (typeof result?.message?.content === "string") {
|
|
2726
|
-
out.push({
|
|
2727
|
-
type: "text",
|
|
2728
|
-
text: result.message.content
|
|
2729
|
-
});
|
|
2730
|
-
}
|
|
2731
|
-
if (result?.message?.tool_calls) {
|
|
2732
|
-
for (const call of result.message.tool_calls) {
|
|
2733
|
-
out.push({
|
|
2734
|
-
functionId: call.id,
|
|
2735
|
-
type: "function_use",
|
|
2736
|
-
name: call.function.name,
|
|
2737
|
-
input: maybeParseJSON(call.function.arguments)
|
|
2738
|
-
});
|
|
2739
|
-
}
|
|
2740
|
-
}
|
|
2741
|
-
return out;
|
|
2742
|
-
}
|
|
2743
|
-
function OutputOpenAIChat(result, _config) {
|
|
2744
|
-
const id = result.id;
|
|
2745
|
-
const name = result.model || _config?.model || "openai.unknown";
|
|
2746
|
-
const created = result.created;
|
|
2747
|
-
const [_content, ..._options] = result?.choices || [];
|
|
2748
|
-
const stopReason = _content?.finish_reason;
|
|
2749
|
-
const content = formatResult(_content);
|
|
2750
|
-
const options = formatOptions(_options, formatResult);
|
|
2751
|
-
const usage = {
|
|
2752
|
-
output_tokens: result?.usage?.completion_tokens,
|
|
2753
|
-
input_tokens: result?.usage?.prompt_tokens,
|
|
2754
|
-
total_tokens: result?.usage?.total_tokens
|
|
2755
|
-
};
|
|
2756
|
-
return BaseLlmOutput2({
|
|
2757
|
-
id,
|
|
2758
|
-
name,
|
|
2759
|
-
created,
|
|
2760
|
-
usage,
|
|
2761
|
-
stopReason,
|
|
2762
|
-
content,
|
|
2763
|
-
options
|
|
2764
|
-
});
|
|
2765
|
-
}
|
|
2766
|
-
|
|
2767
|
-
// src/llm/output/claude.ts
|
|
2768
|
-
function formatResult2(response) {
|
|
2769
|
-
const content = response?.content || [];
|
|
2770
|
-
const out = [];
|
|
2771
|
-
for (let i = 0; i < content.length; i++) {
|
|
2772
|
-
const result = content[i];
|
|
2773
|
-
if (result.type === "text") {
|
|
2774
|
-
out.push({
|
|
2775
|
-
type: "text",
|
|
2776
|
-
text: result.text
|
|
2777
|
-
});
|
|
2778
|
-
} else if (result.type === "tool_use") {
|
|
2779
|
-
out.push({
|
|
2780
|
-
functionId: result.id,
|
|
2781
|
-
type: "function_use",
|
|
2782
|
-
name: result.name,
|
|
2783
|
-
input: result.input
|
|
2784
|
-
});
|
|
2785
|
-
}
|
|
2786
|
-
}
|
|
2787
|
-
return out;
|
|
2788
|
-
}
|
|
2789
|
-
function OutputAnthropicClaude3Chat(result, _config) {
|
|
2790
|
-
const id = result.id;
|
|
2791
|
-
const name = result.model || _config?.model || "anthropic.unknown";
|
|
2792
|
-
const stopReason = result.stop_reason;
|
|
2793
|
-
const content = formatResult2(result);
|
|
2794
|
-
const usage = {
|
|
2795
|
-
input_tokens: result?.usage?.input_tokens,
|
|
2796
|
-
output_tokens: result?.usage?.output_tokens,
|
|
2797
|
-
total_tokens: result?.usage?.input_tokens + result?.usage?.input_tokens
|
|
2798
|
-
};
|
|
2799
|
-
return BaseLlmOutput2({
|
|
2800
|
-
id,
|
|
2801
|
-
name,
|
|
2802
|
-
usage,
|
|
2803
|
-
stopReason,
|
|
2804
|
-
content
|
|
2805
|
-
});
|
|
2806
|
-
}
|
|
2807
|
-
|
|
2808
|
-
// src/llm/output/llama.ts
|
|
2809
|
-
function OutputMetaLlama3Chat(result, _config) {
|
|
2810
|
-
const name = _config?.model || "meta";
|
|
2811
|
-
const stopReason = result.stop_reason;
|
|
2812
|
-
const content = [
|
|
2813
|
-
{ type: "text", text: result.generation }
|
|
2814
|
-
];
|
|
2815
|
-
const usage = {
|
|
2816
|
-
output_tokens: result?.generation_token_count,
|
|
2817
|
-
input_tokens: result?.prompt_token_count,
|
|
2818
|
-
total_tokens: result?.generation_token_count + result?.prompt_token_count
|
|
2819
|
-
};
|
|
2820
|
-
return BaseLlmOutput2({
|
|
2821
|
-
name,
|
|
2822
|
-
usage,
|
|
2823
|
-
stopReason,
|
|
2824
|
-
content
|
|
2825
|
-
});
|
|
2826
|
-
}
|
|
2827
|
-
|
|
2828
|
-
// src/llm/output/default.ts
|
|
2829
|
-
function OutputDefault(result, _config) {
|
|
2830
|
-
const name = _config.model || "unknown";
|
|
2831
|
-
const stopReason = result?.stopReason || "stop";
|
|
2832
|
-
const content = [];
|
|
2833
|
-
if (result?.text) {
|
|
2834
|
-
content.push({ type: "text", text: result.text });
|
|
2835
|
-
}
|
|
2836
|
-
const usage = {
|
|
2837
|
-
output_tokens: result?.output_tokens || 0,
|
|
2838
|
-
input_tokens: result?.input_tokens || 0,
|
|
2839
|
-
total_tokens: (result?.input_tokens || 0) + (result?.output_tokens || 0)
|
|
2840
|
-
};
|
|
2841
|
-
return BaseLlmOutput2({
|
|
2842
|
-
name,
|
|
2843
|
-
usage,
|
|
2844
|
-
stopReason,
|
|
2845
|
-
content
|
|
2846
|
-
});
|
|
2847
|
-
}
|
|
2848
|
-
|
|
2849
|
-
// src/llm/output/xai.ts
|
|
2850
|
-
function formatResult3(result) {
|
|
2851
|
-
const out = [];
|
|
2852
|
-
if (typeof result?.message?.content === "string") {
|
|
2853
|
-
out.push({
|
|
2854
|
-
type: "text",
|
|
2855
|
-
text: result.message.content
|
|
2856
|
-
});
|
|
2857
|
-
}
|
|
2858
|
-
if (result?.message?.tool_calls) {
|
|
2859
|
-
for (const call of result.message.tool_calls) {
|
|
2860
|
-
out.push({
|
|
2861
|
-
functionId: call.id,
|
|
2862
|
-
type: "function_use",
|
|
2863
|
-
name: call.function.name,
|
|
2864
|
-
input: maybeParseJSON(call.function.arguments)
|
|
2865
|
-
});
|
|
2866
|
-
}
|
|
2867
|
-
}
|
|
2868
|
-
return out;
|
|
2869
|
-
}
|
|
2870
|
-
function OutputXAIChat(result, _config) {
|
|
2871
|
-
const id = result.id;
|
|
2872
|
-
const name = result.model || _config?.model || "openai.unknown";
|
|
2873
|
-
const created = result.created;
|
|
2874
|
-
const [_content, ..._options] = result?.choices || [];
|
|
2875
|
-
const stopReason = _content?.finish_reason;
|
|
2876
|
-
const content = formatResult3(_content);
|
|
2877
|
-
const options = formatOptions(_options, formatResult3);
|
|
2878
|
-
const usage = {
|
|
2879
|
-
output_tokens: result?.usage?.completion_tokens,
|
|
2880
|
-
input_tokens: result?.usage?.prompt_tokens,
|
|
2881
|
-
total_tokens: result?.usage?.total_tokens
|
|
2882
|
-
};
|
|
2883
|
-
return BaseLlmOutput2({
|
|
2884
|
-
id,
|
|
2885
|
-
name,
|
|
2886
|
-
created,
|
|
2887
|
-
usage,
|
|
2888
|
-
stopReason,
|
|
2889
|
-
content,
|
|
2890
|
-
options
|
|
2891
|
-
});
|
|
2892
|
-
}
|
|
2893
|
-
|
|
2894
|
-
// src/llm/output/_utils/combineJsonl.ts
|
|
2895
|
-
function combineJsonl(jsonl) {
|
|
2896
|
-
const lines = jsonl.trim().split("\n").map((line) => line.trim()).filter((line) => line.length > 0).map((line) => {
|
|
2897
|
-
try {
|
|
2898
|
-
return JSON.parse(line);
|
|
2899
|
-
} catch (e) {
|
|
2900
|
-
throw new Error(`Invalid JSON: ${line}`);
|
|
2901
|
-
}
|
|
2902
|
-
});
|
|
2903
|
-
if (lines.length === 0) {
|
|
2904
|
-
throw new Error("No JSON lines provided.");
|
|
2905
|
-
}
|
|
2906
|
-
lines.sort(
|
|
2907
|
-
(a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
|
|
2908
|
-
);
|
|
2909
|
-
let combinedContent = lines.map((line) => line?.message?.content ?? "").join("");
|
|
2910
|
-
combinedContent = combinedContent.replace(/\\u003c/g, "<").replace(/\\u003e/g, ">");
|
|
2911
|
-
const finalLine = lines.find((line) => line.done === true);
|
|
2912
|
-
if (!finalLine) {
|
|
2913
|
-
throw new Error("No line found where done = true.");
|
|
2914
|
-
}
|
|
2915
|
-
const result = {
|
|
2916
|
-
model: finalLine.model,
|
|
2917
|
-
created_at: finalLine.created_at,
|
|
2918
|
-
message: {
|
|
2919
|
-
role: finalLine?.message?.role || "assistant",
|
|
2920
|
-
content: combinedContent
|
|
2921
|
-
},
|
|
2922
|
-
done_reason: finalLine.done_reason,
|
|
2923
|
-
done: finalLine.done,
|
|
2924
|
-
total_duration: finalLine.total_duration,
|
|
2925
|
-
load_duration: finalLine.load_duration,
|
|
2926
|
-
prompt_eval_count: finalLine.prompt_eval_count,
|
|
2927
|
-
prompt_eval_duration: finalLine.prompt_eval_duration,
|
|
2928
|
-
eval_count: finalLine.eval_count,
|
|
2929
|
-
eval_duration: finalLine.eval_duration
|
|
2930
|
-
};
|
|
2931
|
-
const content = {
|
|
2932
|
-
type: "text",
|
|
2933
|
-
text: combinedContent
|
|
2934
|
-
};
|
|
2860
|
+
...toolsArr.map((call) => {
|
|
2861
|
+
const { name, arguments: input } = call;
|
|
2862
|
+
return {
|
|
2863
|
+
functionCall: {
|
|
2864
|
+
name,
|
|
2865
|
+
args: maybeParseJSON(input)
|
|
2866
|
+
}
|
|
2867
|
+
};
|
|
2868
|
+
})
|
|
2869
|
+
);
|
|
2870
|
+
delete message.function_call;
|
|
2871
|
+
}
|
|
2935
2872
|
return {
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
content
|
|
2873
|
+
role,
|
|
2874
|
+
parts
|
|
2939
2875
|
};
|
|
2940
2876
|
}
|
|
2941
2877
|
|
|
2942
|
-
// src/llm/
|
|
2943
|
-
function
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2878
|
+
// src/llm/config/google/promptSanitize.ts
|
|
2879
|
+
function googleGeminiPromptSanitize(_messages, _inputBodyObj, _outputObj) {
|
|
2880
|
+
if (typeof _messages === "string") {
|
|
2881
|
+
return [{ role: "user", parts: [{ text: _messages }] }];
|
|
2882
|
+
}
|
|
2883
|
+
if (Array.isArray(_messages)) {
|
|
2884
|
+
if (_messages.length === 0) {
|
|
2885
|
+
throw new Error("Empty messages array");
|
|
2886
|
+
}
|
|
2887
|
+
if (_messages.length === 1 && _messages[0].role === "system") {
|
|
2888
|
+
return [{ role: "user", parts: [{ text: _messages[0].content }] }];
|
|
2889
|
+
}
|
|
2890
|
+
const hasSystemInstruction = _messages.some(
|
|
2891
|
+
(message) => message.role === "system"
|
|
2892
|
+
);
|
|
2893
|
+
if (hasSystemInstruction) {
|
|
2894
|
+
const theSystemInstructions = _messages.filter(
|
|
2895
|
+
(message) => message.role === "system"
|
|
2896
|
+
);
|
|
2897
|
+
const withoutSystemInstructions = _messages.filter(
|
|
2898
|
+
(message) => message.role !== "system"
|
|
2899
|
+
);
|
|
2900
|
+
_outputObj.system_instruction = {
|
|
2901
|
+
parts: theSystemInstructions.map((message) => ({
|
|
2902
|
+
text: message.content
|
|
2903
|
+
}))
|
|
2904
|
+
};
|
|
2905
|
+
return withoutSystemInstructions.map(googleGeminiPromptMessageCallback);
|
|
2906
|
+
}
|
|
2907
|
+
return _messages.map(googleGeminiPromptMessageCallback);
|
|
2908
|
+
}
|
|
2909
|
+
throw new Error("Invalid messages format");
|
|
2964
2910
|
}
|
|
2965
2911
|
|
|
2966
2912
|
// src/llm/output/google.gemini/formatResult.ts
|
|
2967
|
-
function
|
|
2913
|
+
function formatResult3(result, id) {
|
|
2968
2914
|
const { parts = [] } = result?.content || {};
|
|
2969
2915
|
const out = [];
|
|
2970
2916
|
for (let i = 0; i < parts.length; i++) {
|
|
@@ -2989,18 +2935,18 @@ function formatResult4(result, id) {
|
|
|
2989
2935
|
// src/llm/output/google.gemini/index.ts
|
|
2990
2936
|
function OutputGoogleGeminiChat(result, _config) {
|
|
2991
2937
|
const id = result.responseId;
|
|
2992
|
-
const name = result.modelVersion || _config?.model || "gemini";
|
|
2938
|
+
const name = result.modelVersion || _config?.options.model?.default || "gemini";
|
|
2993
2939
|
const created = (/* @__PURE__ */ new Date()).getTime();
|
|
2994
2940
|
const [_content, ..._options] = result?.candidates || [];
|
|
2995
2941
|
const stopReason = _content?.finishReason?.toLowerCase();
|
|
2996
|
-
const content =
|
|
2997
|
-
const options = formatOptions(_options,
|
|
2942
|
+
const content = formatResult3(_content, id);
|
|
2943
|
+
const options = formatOptions(_options, formatResult3);
|
|
2998
2944
|
const usage = {
|
|
2999
2945
|
output_tokens: result?.usageMetadata?.candidatesTokenCount,
|
|
3000
2946
|
input_tokens: result?.usageMetadata?.promptTokenCount,
|
|
3001
2947
|
total_tokens: result?.usageMetadata?.totalTokenCount
|
|
3002
2948
|
};
|
|
3003
|
-
return
|
|
2949
|
+
return {
|
|
3004
2950
|
id,
|
|
3005
2951
|
name,
|
|
3006
2952
|
created,
|
|
@@ -3008,83 +2954,152 @@ function OutputGoogleGeminiChat(result, _config) {
|
|
|
3008
2954
|
stopReason,
|
|
3009
2955
|
content,
|
|
3010
2956
|
options
|
|
3011
|
-
}
|
|
2957
|
+
};
|
|
3012
2958
|
}
|
|
3013
2959
|
|
|
3014
|
-
// src/llm/
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
2960
|
+
// src/llm/config/google/index.ts
|
|
2961
|
+
var googleGeminiChatV1 = {
|
|
2962
|
+
key: "google.chat.v1",
|
|
2963
|
+
provider: "google.chat",
|
|
2964
|
+
endpoint: `https://generativelanguage.googleapis.com/v1beta/models/{{model}}:generateContent?key={{geminiApiKey}}`,
|
|
2965
|
+
options: {
|
|
2966
|
+
effort: {},
|
|
2967
|
+
prompt: {},
|
|
2968
|
+
// topP: {},
|
|
2969
|
+
geminiApiKey: {
|
|
2970
|
+
default: getEnvironmentVariable("GEMINI_API_KEY")
|
|
2971
|
+
}
|
|
2972
|
+
},
|
|
2973
|
+
method: "POST",
|
|
2974
|
+
headers: `{"Content-Type": "application/json" }`,
|
|
2975
|
+
mapBody: {
|
|
2976
|
+
prompt: {
|
|
2977
|
+
key: "contents",
|
|
2978
|
+
transform: googleGeminiPromptSanitize
|
|
2979
|
+
},
|
|
2980
|
+
effort: {
|
|
2981
|
+
key: "config.thinkingConfig.thinkingBudget",
|
|
2982
|
+
transform: (v, _s) => {
|
|
2983
|
+
if (
|
|
2984
|
+
// only supported reasoning models
|
|
2985
|
+
["gemini-2.5-pro", "gemini-2.5-flash", "gemini-2.5-light"].includes(
|
|
2986
|
+
_s.model
|
|
2987
|
+
) && typeof v === "string" && ["minimal", "low", "medium", "high"].includes(v)
|
|
2988
|
+
) {
|
|
2989
|
+
if (v === "low" || v === "minimal") {
|
|
2990
|
+
return 1024;
|
|
2991
|
+
} else if (v === "medium") {
|
|
2992
|
+
return 8192;
|
|
2993
|
+
} else if (v === "high") {
|
|
2994
|
+
return 24576;
|
|
2995
|
+
}
|
|
2996
|
+
}
|
|
2997
|
+
return void 0;
|
|
2998
|
+
}
|
|
2999
|
+
}
|
|
3000
|
+
},
|
|
3001
|
+
mapOptions: {
|
|
3002
|
+
functionCall: (call) => ({
|
|
3003
|
+
toolConfig: {
|
|
3004
|
+
functionCallingConfig: {
|
|
3005
|
+
mode: call === "any" ? "any" : call === "none" ? "none" : "auto"
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
}),
|
|
3009
|
+
functions: (functions) => ({
|
|
3010
|
+
tools: [
|
|
3011
|
+
{
|
|
3012
|
+
functionDeclarations: functions.map((f) => ({
|
|
3013
|
+
name: f.name,
|
|
3014
|
+
description: f.description,
|
|
3015
|
+
parameters: cleanJsonSchemaFor(f.parameters, "google.chat")
|
|
3016
|
+
}))
|
|
3017
|
+
}
|
|
3018
|
+
]
|
|
3019
|
+
})
|
|
3020
|
+
},
|
|
3021
|
+
transformResponse: OutputGoogleGeminiChat
|
|
3022
|
+
};
|
|
3023
|
+
var google = {
|
|
3024
|
+
"google.chat.v1": googleGeminiChatV1,
|
|
3025
|
+
"google.gemini-2.0-flash": withDefaultModel(
|
|
3026
|
+
googleGeminiChatV1,
|
|
3027
|
+
"gemini-2.0-flash"
|
|
3028
|
+
),
|
|
3029
|
+
"google.gemini-2.0-flash-lite": withDefaultModel(
|
|
3030
|
+
googleGeminiChatV1,
|
|
3031
|
+
"gemini-2.0-flash-lite"
|
|
3032
|
+
),
|
|
3033
|
+
"google.gemini-2.5-flash": withDefaultModel(
|
|
3034
|
+
googleGeminiChatV1,
|
|
3035
|
+
"gemini-2.5-flash"
|
|
3036
|
+
),
|
|
3037
|
+
"google.gemini-2.5-flash-lite": withDefaultModel(
|
|
3038
|
+
googleGeminiChatV1,
|
|
3039
|
+
"gemini-2.5-flash-lite"
|
|
3040
|
+
),
|
|
3041
|
+
"google.gemini-1.5-pro": withDefaultModel(
|
|
3042
|
+
googleGeminiChatV1,
|
|
3043
|
+
"gemini-1.5-pro"
|
|
3044
|
+
),
|
|
3045
|
+
"google.gemini-2.5-pro": withDefaultModel(
|
|
3046
|
+
googleGeminiChatV1,
|
|
3047
|
+
"gemini-2.5-pro"
|
|
3048
|
+
)
|
|
3049
|
+
};
|
|
3050
|
+
|
|
3051
|
+
// src/llm/config/deepseek/index.ts
|
|
3052
|
+
var deepseekChatV1 = createOpenAiCompatibleConfiguration({
|
|
3053
|
+
key: "deepseek.chat.v1",
|
|
3054
|
+
provider: "deepseek.chat",
|
|
3055
|
+
endpoint: `https://api.deepseek.com/v1/chat/completions`,
|
|
3056
|
+
apiKeyMapping: ["deepseekApiKey", "DEEPSEEK_API_KEY"]
|
|
3057
|
+
});
|
|
3058
|
+
var deepseek = {
|
|
3059
|
+
"deepseek.chat.v1": deepseekChatV1,
|
|
3060
|
+
"deepseek.chat": withDefaultModel(deepseekChatV1, "deepseek-chat")
|
|
3061
|
+
};
|
|
3062
|
+
|
|
3063
|
+
// src/llm/config.ts
|
|
3064
|
+
var configs = {
|
|
3065
|
+
...openai,
|
|
3066
|
+
...anthropic,
|
|
3067
|
+
...bedrock,
|
|
3068
|
+
...xai,
|
|
3069
|
+
...ollama,
|
|
3070
|
+
...google,
|
|
3071
|
+
...deepseek
|
|
3072
|
+
};
|
|
3073
|
+
function getLlmConfig(provider) {
|
|
3074
|
+
if (!provider) {
|
|
3075
|
+
throw new LlmExeError(`Missing provider`, "unknown", {
|
|
3076
|
+
error: "Missing provider",
|
|
3077
|
+
resolution: "Provide a valid provider"
|
|
3021
3078
|
});
|
|
3022
3079
|
}
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
functionId: call.id,
|
|
3027
|
-
type: "function_use",
|
|
3028
|
-
name: call.function.name,
|
|
3029
|
-
input: maybeParseJSON(call.function.arguments)
|
|
3030
|
-
});
|
|
3031
|
-
}
|
|
3080
|
+
const pick2 = configs[provider];
|
|
3081
|
+
if (pick2) {
|
|
3082
|
+
return pick2;
|
|
3032
3083
|
}
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
const name = result.model || _config?.model || "deepseek.unknown";
|
|
3038
|
-
const created = result.created;
|
|
3039
|
-
const [_content, ..._options] = result?.choices || [];
|
|
3040
|
-
const stopReason = _content?.finish_reason;
|
|
3041
|
-
const content = formatResult5(_content);
|
|
3042
|
-
const options = formatOptions(_options, formatResult5);
|
|
3043
|
-
const usage = {
|
|
3044
|
-
output_tokens: result?.usage?.completion_tokens,
|
|
3045
|
-
input_tokens: result?.usage?.prompt_tokens,
|
|
3046
|
-
total_tokens: result?.usage?.total_tokens
|
|
3047
|
-
};
|
|
3048
|
-
return BaseLlmOutput2({
|
|
3049
|
-
id,
|
|
3050
|
-
name,
|
|
3051
|
-
created,
|
|
3052
|
-
usage,
|
|
3053
|
-
stopReason,
|
|
3054
|
-
content,
|
|
3055
|
-
options
|
|
3084
|
+
throw new LlmExeError(`Invalid provider: ${provider}`, "unknown", {
|
|
3085
|
+
provider,
|
|
3086
|
+
error: `Invalid provider: ${provider}`,
|
|
3087
|
+
resolution: "Provide a valid provider"
|
|
3056
3088
|
});
|
|
3057
3089
|
}
|
|
3058
3090
|
|
|
3059
|
-
// src/
|
|
3060
|
-
function
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
return OutputMetaLlama3Chat(response, config);
|
|
3070
|
-
// case "amazon:nova.chat.v1":
|
|
3071
|
-
// return OutputDefault(response, config);
|
|
3072
|
-
case "xai.chat.v1":
|
|
3073
|
-
return OutputXAIChat(response, config);
|
|
3074
|
-
case "ollama.chat.v1":
|
|
3075
|
-
return OutputOllamaChat(response, config);
|
|
3076
|
-
case "google.chat.v1":
|
|
3077
|
-
return OutputGoogleGeminiChat(response, config);
|
|
3078
|
-
case "deepseek.chat.v1":
|
|
3079
|
-
return OutputDeepSeekChat(response, config);
|
|
3080
|
-
// use oai for now
|
|
3081
|
-
default: {
|
|
3082
|
-
if (config?.key?.startsWith("custom:")) {
|
|
3083
|
-
return OutputDefault(response, config);
|
|
3084
|
-
}
|
|
3085
|
-
throw new Error("Unsupported provider");
|
|
3091
|
+
// src/utils/modules/maskApiKeysInDebug.ts
|
|
3092
|
+
function maskApiKeys(log) {
|
|
3093
|
+
return log.replace(
|
|
3094
|
+
/\b(Bearer\s+[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+|Bearer\s+[A-Za-z0-9\-_]{20,}|sk-[A-Za-z0-9]{20,}|AKIA[A-Z0-9]{16}|[A-Za-z0-9]{32,})\b/g,
|
|
3095
|
+
(match) => {
|
|
3096
|
+
if (match.length <= 8) return match;
|
|
3097
|
+
const prefix = match.substring(0, 4);
|
|
3098
|
+
const suffix = match.substring(match.length - 4);
|
|
3099
|
+
const maskLength = match.length - 8;
|
|
3100
|
+
return `${prefix}${"*".repeat(maskLength)}${suffix}`;
|
|
3086
3101
|
}
|
|
3087
|
-
|
|
3102
|
+
);
|
|
3088
3103
|
}
|
|
3089
3104
|
|
|
3090
3105
|
// src/utils/modules/debug.ts
|
|
@@ -3110,7 +3125,15 @@ function debug(...args) {
|
|
|
3110
3125
|
logs.push(arg.toString());
|
|
3111
3126
|
} else {
|
|
3112
3127
|
try {
|
|
3113
|
-
|
|
3128
|
+
let shouldMask = false;
|
|
3129
|
+
if (arg.headers && arg.headers.Authorization) {
|
|
3130
|
+
shouldMask = true;
|
|
3131
|
+
}
|
|
3132
|
+
let str = JSON.stringify(arg, null, 2);
|
|
3133
|
+
if (shouldMask) {
|
|
3134
|
+
str = maskApiKeys(str);
|
|
3135
|
+
}
|
|
3136
|
+
logs.push(str);
|
|
3114
3137
|
} catch (error) {
|
|
3115
3138
|
console.error("Error parsing object:", error);
|
|
3116
3139
|
}
|
|
@@ -3158,7 +3181,8 @@ async function apiRequest(url, options) {
|
|
|
3158
3181
|
}
|
|
3159
3182
|
throw new Error(message);
|
|
3160
3183
|
}
|
|
3161
|
-
|
|
3184
|
+
const contentType = response.headers.get("content-type");
|
|
3185
|
+
if (contentType?.includes("application/json")) {
|
|
3162
3186
|
const responseData = await response.json();
|
|
3163
3187
|
return responseData;
|
|
3164
3188
|
} else {
|
|
@@ -3175,20 +3199,26 @@ async function apiRequest(url, options) {
|
|
|
3175
3199
|
function convertDotNotation(obj) {
|
|
3176
3200
|
const result = {};
|
|
3177
3201
|
for (const key in obj) {
|
|
3178
|
-
if (
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3202
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && !key.includes(".")) {
|
|
3203
|
+
result[key] = obj[key];
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
for (const key in obj) {
|
|
3207
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && key.includes(".")) {
|
|
3208
|
+
const keys = key.split(".");
|
|
3209
|
+
let currentLevel = result;
|
|
3210
|
+
for (let i = 0; i < keys.length; i++) {
|
|
3211
|
+
const currentKey = keys[i];
|
|
3212
|
+
if (i === keys.length - 1) {
|
|
3213
|
+
currentLevel[currentKey] = obj[key];
|
|
3214
|
+
} else {
|
|
3215
|
+
if (!(currentKey in currentLevel)) {
|
|
3216
|
+
currentLevel[currentKey] = {};
|
|
3217
|
+
} else if (typeof currentLevel[currentKey] !== "object" || currentLevel[currentKey] === null || Array.isArray(currentLevel[currentKey])) {
|
|
3218
|
+
currentLevel[currentKey] = {};
|
|
3188
3219
|
}
|
|
3220
|
+
currentLevel = currentLevel[currentKey];
|
|
3189
3221
|
}
|
|
3190
|
-
} else {
|
|
3191
|
-
result[key] = obj[key];
|
|
3192
3222
|
}
|
|
3193
3223
|
}
|
|
3194
3224
|
}
|
|
@@ -3205,8 +3235,8 @@ function mapBody(template, body) {
|
|
|
3205
3235
|
const { key: providerSpecificKey, default: defaultValue } = providerSpecificSettings;
|
|
3206
3236
|
if (providerSpecificKey) {
|
|
3207
3237
|
let valueForThisKey = body[genericInputKey];
|
|
3208
|
-
if (providerSpecificSettings.
|
|
3209
|
-
valueForThisKey = providerSpecificSettings.
|
|
3238
|
+
if (providerSpecificSettings.transform && typeof providerSpecificSettings.transform === "function") {
|
|
3239
|
+
valueForThisKey = providerSpecificSettings.transform(
|
|
3210
3240
|
valueForThisKey,
|
|
3211
3241
|
Object.freeze({ ...body }),
|
|
3212
3242
|
output
|
|
@@ -3230,28 +3260,70 @@ import { Sha256 } from "@aws-crypto/sha256-js";
|
|
|
3230
3260
|
|
|
3231
3261
|
// src/utils/modules/runWithTemporaryEnv.ts
|
|
3232
3262
|
async function runWithTemporaryEnv(env, handler) {
|
|
3233
|
-
const
|
|
3263
|
+
const modifiedKeys = [];
|
|
3264
|
+
const originalValues = {};
|
|
3265
|
+
const envBefore = { ...process.env };
|
|
3234
3266
|
try {
|
|
3235
|
-
|
|
3267
|
+
try {
|
|
3268
|
+
env();
|
|
3269
|
+
} catch (envError) {
|
|
3270
|
+
const envAfter2 = process.env;
|
|
3271
|
+
for (const key in envAfter2) {
|
|
3272
|
+
if (envBefore[key] !== envAfter2[key]) {
|
|
3273
|
+
modifiedKeys.push(key);
|
|
3274
|
+
originalValues[key] = envBefore[key];
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3277
|
+
for (const key in envBefore) {
|
|
3278
|
+
if (!(key in envAfter2) && !modifiedKeys.includes(key)) {
|
|
3279
|
+
modifiedKeys.push(key);
|
|
3280
|
+
originalValues[key] = envBefore[key];
|
|
3281
|
+
}
|
|
3282
|
+
}
|
|
3283
|
+
throw envError;
|
|
3284
|
+
}
|
|
3285
|
+
const envAfter = process.env;
|
|
3286
|
+
for (const key in envAfter) {
|
|
3287
|
+
if (envBefore[key] !== envAfter[key]) {
|
|
3288
|
+
modifiedKeys.push(key);
|
|
3289
|
+
originalValues[key] = envBefore[key];
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
for (const key in envBefore) {
|
|
3293
|
+
if (!(key in envAfter) && !modifiedKeys.includes(key)) {
|
|
3294
|
+
modifiedKeys.push(key);
|
|
3295
|
+
originalValues[key] = envBefore[key];
|
|
3296
|
+
}
|
|
3297
|
+
}
|
|
3236
3298
|
const value = await handler();
|
|
3237
3299
|
return value;
|
|
3238
3300
|
} finally {
|
|
3239
|
-
|
|
3301
|
+
for (const key of modifiedKeys) {
|
|
3302
|
+
const originalValue = originalValues[key];
|
|
3303
|
+
if (originalValue === void 0) {
|
|
3304
|
+
delete process.env[key];
|
|
3305
|
+
} else {
|
|
3306
|
+
process.env[key] = originalValue;
|
|
3307
|
+
}
|
|
3308
|
+
}
|
|
3240
3309
|
}
|
|
3241
3310
|
}
|
|
3242
3311
|
|
|
3243
3312
|
// src/utils/modules/getAwsAuthorizationHeaders.ts
|
|
3244
3313
|
async function getAwsAuthorizationHeaders(req, props) {
|
|
3314
|
+
if (!props.url || !props.regionName) {
|
|
3315
|
+
throw new Error("URL and region name are required for AWS authorization");
|
|
3316
|
+
}
|
|
3245
3317
|
const providerChain = fromNodeProviderChain();
|
|
3246
3318
|
const credentials = await runWithTemporaryEnv(
|
|
3247
3319
|
() => {
|
|
3248
|
-
if (props.awsAccessKey) {
|
|
3320
|
+
if (props.awsAccessKey && typeof props.awsAccessKey === "string") {
|
|
3249
3321
|
process.env["AWS_ACCESS_KEY_ID"] = props.awsAccessKey;
|
|
3250
3322
|
}
|
|
3251
|
-
if (props.awsSecretKey) {
|
|
3323
|
+
if (props.awsSecretKey && typeof props.awsSecretKey === "string") {
|
|
3252
3324
|
process.env["AWS_SECRET_ACCESS_KEY"] = props.awsSecretKey;
|
|
3253
3325
|
}
|
|
3254
|
-
if (props.awsSessionToken) {
|
|
3326
|
+
if (props.awsSessionToken && typeof props.awsSessionToken === "string") {
|
|
3255
3327
|
process.env["AWS_SESSION_TOKEN"] = props.awsSessionToken;
|
|
3256
3328
|
}
|
|
3257
3329
|
},
|
|
@@ -3281,8 +3353,21 @@ async function getAwsAuthorizationHeaders(req, props) {
|
|
|
3281
3353
|
// src/llm/_utils.parseHeaders.ts
|
|
3282
3354
|
async function parseHeaders(config, replacements, payload) {
|
|
3283
3355
|
const replace = replaceTemplateStringSimple(config.headers, replacements);
|
|
3284
|
-
|
|
3285
|
-
|
|
3356
|
+
let parsedHeaders = {};
|
|
3357
|
+
if (replace) {
|
|
3358
|
+
try {
|
|
3359
|
+
parsedHeaders = JSON.parse(replace);
|
|
3360
|
+
if (typeof parsedHeaders !== "object" || parsedHeaders === null || Array.isArray(parsedHeaders)) {
|
|
3361
|
+
throw new Error("Headers must be a JSON object");
|
|
3362
|
+
}
|
|
3363
|
+
} catch (error) {
|
|
3364
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
3365
|
+
throw new Error(
|
|
3366
|
+
`Failed to parse headers configuration: ${errorMessage}. Headers template: "${config.headers}". After replacement: "${replace}"`
|
|
3367
|
+
);
|
|
3368
|
+
}
|
|
3369
|
+
}
|
|
3370
|
+
const headers = Object.assign({}, payload.headers, parsedHeaders);
|
|
3286
3371
|
if (config.provider.startsWith("amazon:") || config.provider.startsWith("amazon.")) {
|
|
3287
3372
|
const url = payload.url;
|
|
3288
3373
|
return getAwsAuthorizationHeaders(
|
|
@@ -3303,122 +3388,129 @@ async function parseHeaders(config, replacements, payload) {
|
|
|
3303
3388
|
}
|
|
3304
3389
|
}
|
|
3305
3390
|
|
|
3306
|
-
// src/llm/output/
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
};
|
|
3313
|
-
|
|
3314
|
-
const
|
|
3315
|
-
|
|
3391
|
+
// src/llm/output/default.ts
|
|
3392
|
+
function OutputDefault(result, _config) {
|
|
3393
|
+
const name = _config?.options.model?.default || "unknown";
|
|
3394
|
+
const stopReason = result?.stopReason || "stop";
|
|
3395
|
+
const content = [];
|
|
3396
|
+
if (result?.text) {
|
|
3397
|
+
content.push({ type: "text", text: result.text });
|
|
3398
|
+
}
|
|
3399
|
+
const usage = {
|
|
3400
|
+
output_tokens: result?.output_tokens || 0,
|
|
3401
|
+
input_tokens: result?.input_tokens || 0,
|
|
3402
|
+
total_tokens: (result?.input_tokens || 0) + (result?.output_tokens || 0)
|
|
3403
|
+
};
|
|
3404
|
+
return {
|
|
3405
|
+
name,
|
|
3406
|
+
usage,
|
|
3407
|
+
stopReason,
|
|
3408
|
+
content
|
|
3409
|
+
};
|
|
3410
|
+
}
|
|
3411
|
+
|
|
3412
|
+
// src/llm/output/_utils/getResultContent.ts
|
|
3413
|
+
function getResultContent(result, index) {
|
|
3414
|
+
if (typeof index === "number" && index > 0) {
|
|
3415
|
+
const arr = result?.options || [];
|
|
3416
|
+
const val = arr[index];
|
|
3417
|
+
return val ? val : [];
|
|
3418
|
+
}
|
|
3419
|
+
return [...result.content];
|
|
3420
|
+
}
|
|
3421
|
+
|
|
3422
|
+
// src/llm/output/_utils/getResultText.ts
|
|
3423
|
+
function getResultText(result, index) {
|
|
3424
|
+
if (typeof index === "number" && index > 0) {
|
|
3425
|
+
const arr = result?.options || [];
|
|
3426
|
+
const val = arr[index];
|
|
3427
|
+
return isOutputResultContentText(val?.[0]) ? val[0]?.text : "";
|
|
3428
|
+
}
|
|
3429
|
+
return isOutputResultContentText(result?.content?.[0]) ? result.content[0]?.text : "";
|
|
3430
|
+
}
|
|
3431
|
+
|
|
3432
|
+
// src/llm/output/base.ts
|
|
3433
|
+
function BaseLlmOutput(result) {
|
|
3434
|
+
const __result = Object.freeze({
|
|
3435
|
+
id: result.id || uuidv4(),
|
|
3436
|
+
name: result.name,
|
|
3437
|
+
usage: result.usage,
|
|
3438
|
+
stopReason: result.stopReason,
|
|
3439
|
+
options: [...result?.options || []],
|
|
3440
|
+
content: [...result.content],
|
|
3441
|
+
created: result?.created || (/* @__PURE__ */ new Date()).getTime()
|
|
3442
|
+
});
|
|
3443
|
+
function getResult() {
|
|
3316
3444
|
return {
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3445
|
+
id: __result.id,
|
|
3446
|
+
name: __result.name,
|
|
3447
|
+
created: __result.created,
|
|
3448
|
+
usage: __result.usage,
|
|
3449
|
+
options: __result.options,
|
|
3450
|
+
content: __result.content,
|
|
3451
|
+
stopReason: __result.stopReason
|
|
3320
3452
|
};
|
|
3321
3453
|
}
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3454
|
+
return {
|
|
3455
|
+
getResultContent: (index) => getResultContent(__result, index),
|
|
3456
|
+
getResultText: (index) => getResultText(__result, index),
|
|
3457
|
+
getResult
|
|
3458
|
+
};
|
|
3459
|
+
}
|
|
3460
|
+
|
|
3461
|
+
// src/llm/_utils.mapOptions.ts
|
|
3462
|
+
function mapOptions(input, options, config) {
|
|
3463
|
+
if (!config?.mapOptions || !options) {
|
|
3464
|
+
return input;
|
|
3465
|
+
}
|
|
3466
|
+
let result = { ...input };
|
|
3467
|
+
let processedOptions = options;
|
|
3468
|
+
if (options.functionCall && config.mapOptions.functionCall) {
|
|
3469
|
+
const mapping = config.mapOptions.functionCall(
|
|
3470
|
+
options.functionCall,
|
|
3471
|
+
options,
|
|
3472
|
+
result,
|
|
3473
|
+
config
|
|
3474
|
+
);
|
|
3475
|
+
if (mapping._clearFunctions) {
|
|
3476
|
+
processedOptions = { ...options, functions: [] };
|
|
3477
|
+
const { _clearFunctions, ...rest } = mapping;
|
|
3478
|
+
result = { ...result, ...rest };
|
|
3479
|
+
} else {
|
|
3480
|
+
result = { ...result, ...mapping };
|
|
3333
3481
|
}
|
|
3334
|
-
return obj;
|
|
3335
3482
|
}
|
|
3336
|
-
|
|
3483
|
+
if (processedOptions.functions?.length && config.mapOptions.functions) {
|
|
3484
|
+
result = {
|
|
3485
|
+
...result,
|
|
3486
|
+
...config.mapOptions.functions(
|
|
3487
|
+
processedOptions.functions,
|
|
3488
|
+
processedOptions,
|
|
3489
|
+
result,
|
|
3490
|
+
config
|
|
3491
|
+
)
|
|
3492
|
+
};
|
|
3493
|
+
}
|
|
3494
|
+
if (options.jsonSchema && config.mapOptions.jsonSchema) {
|
|
3495
|
+
result = {
|
|
3496
|
+
...result,
|
|
3497
|
+
...config.mapOptions.jsonSchema(options.jsonSchema, options, result, config)
|
|
3498
|
+
};
|
|
3499
|
+
}
|
|
3500
|
+
return result;
|
|
3337
3501
|
}
|
|
3338
3502
|
|
|
3339
3503
|
// src/llm/llm.call.ts
|
|
3340
3504
|
async function useLlm_call(state, messages, _options) {
|
|
3341
3505
|
const config = getLlmConfig(state.key);
|
|
3342
|
-
const
|
|
3343
|
-
const input = mapBody(
|
|
3506
|
+
const transformBody = mapBody(
|
|
3344
3507
|
config.mapBody,
|
|
3345
3508
|
Object.assign({}, state, {
|
|
3346
3509
|
prompt: messages
|
|
3347
3510
|
})
|
|
3348
3511
|
);
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
const curr = input["response_format"] || {};
|
|
3352
|
-
input["response_format"] = Object.assign(curr, {
|
|
3353
|
-
type: "json_schema",
|
|
3354
|
-
json_schema: {
|
|
3355
|
-
name: "output",
|
|
3356
|
-
strict: !!functionCallStrictInput,
|
|
3357
|
-
schema: cleanJsonSchemaFor(_options?.jsonSchema, "openai.chat")
|
|
3358
|
-
}
|
|
3359
|
-
});
|
|
3360
|
-
}
|
|
3361
|
-
}
|
|
3362
|
-
if (_options && _options?.functionCall) {
|
|
3363
|
-
if (state.provider.startsWith("anthropic")) {
|
|
3364
|
-
if (_options?.functionCall === "none") {
|
|
3365
|
-
_options.functions = [];
|
|
3366
|
-
} else if (_options?.functionCall === "auto" || _options?.functionCall === "any") {
|
|
3367
|
-
input["tool_choice"] = { type: _options?.functionCall };
|
|
3368
|
-
} else {
|
|
3369
|
-
input["tool_choice"] = _options?.functionCall;
|
|
3370
|
-
}
|
|
3371
|
-
} else if (state.provider.startsWith("openai") || state.provider.startsWith("deepseek") || state.provider.startsWith("xai")) {
|
|
3372
|
-
input["tool_choice"] = normalizeFunctionCall(
|
|
3373
|
-
_options?.functionCall,
|
|
3374
|
-
"openai"
|
|
3375
|
-
);
|
|
3376
|
-
} else if (state.provider.startsWith("google")) {
|
|
3377
|
-
input["toolConfig"] = {
|
|
3378
|
-
functionCallingConfig: {
|
|
3379
|
-
mode: normalizeFunctionCall(_options?.functionCall, "google")
|
|
3380
|
-
}
|
|
3381
|
-
};
|
|
3382
|
-
}
|
|
3383
|
-
}
|
|
3384
|
-
if (_options && _options?.functions?.length) {
|
|
3385
|
-
if (state.provider.startsWith("anthropic")) {
|
|
3386
|
-
input["tools"] = _options.functions.map((f) => ({
|
|
3387
|
-
name: f.name,
|
|
3388
|
-
description: f.description,
|
|
3389
|
-
input_schema: cleanJsonSchemaFor(f.parameters, "anthropic.chat")
|
|
3390
|
-
}));
|
|
3391
|
-
} else if (state.provider.startsWith("openai") || state.provider.startsWith("deepseek") || state.provider.startsWith("xai")) {
|
|
3392
|
-
input["tools"] = _options.functions.map((f) => {
|
|
3393
|
-
const props = {
|
|
3394
|
-
name: f?.name,
|
|
3395
|
-
description: f?.description,
|
|
3396
|
-
parameters: f?.parameters
|
|
3397
|
-
};
|
|
3398
|
-
return {
|
|
3399
|
-
type: "function",
|
|
3400
|
-
function: Object.assign(
|
|
3401
|
-
props,
|
|
3402
|
-
{
|
|
3403
|
-
parameters: cleanJsonSchemaFor(props.parameters, "openai.chat")
|
|
3404
|
-
},
|
|
3405
|
-
{ strict: functionCallStrictInput }
|
|
3406
|
-
)
|
|
3407
|
-
};
|
|
3408
|
-
});
|
|
3409
|
-
} else if (state.provider.startsWith("google")) {
|
|
3410
|
-
input["tools"] = [
|
|
3411
|
-
{
|
|
3412
|
-
functionDeclarations: _options.functions.map((f) => ({
|
|
3413
|
-
name: f.name,
|
|
3414
|
-
description: f.description,
|
|
3415
|
-
parameters: cleanJsonSchemaFor(f.parameters, "google.chat")
|
|
3416
|
-
}))
|
|
3417
|
-
}
|
|
3418
|
-
];
|
|
3419
|
-
}
|
|
3420
|
-
}
|
|
3421
|
-
const body = JSON.stringify(input);
|
|
3512
|
+
const applyOptions = mapOptions(transformBody, _options, config);
|
|
3513
|
+
const body = JSON.stringify(applyOptions);
|
|
3422
3514
|
const url = replaceTemplateStringSimple(config.endpoint, state);
|
|
3423
3515
|
const headers = await parseHeaders(config, state, {
|
|
3424
3516
|
url,
|
|
@@ -3445,7 +3537,9 @@ async function useLlm_call(state, messages, _options) {
|
|
|
3445
3537
|
body,
|
|
3446
3538
|
headers
|
|
3447
3539
|
});
|
|
3448
|
-
|
|
3540
|
+
const { transformResponse = OutputDefault } = config;
|
|
3541
|
+
const normalized = transformResponse(response, config);
|
|
3542
|
+
return BaseLlmOutput(normalized);
|
|
3449
3543
|
}
|
|
3450
3544
|
|
|
3451
3545
|
// src/llm/_utils.stateFromOptions.ts
|
|
@@ -3588,6 +3682,80 @@ function useLlm(provider, options = {}) {
|
|
|
3588
3682
|
const config = getLlmConfig(provider);
|
|
3589
3683
|
return apiRequestWrapper(config, options, useLlm_call);
|
|
3590
3684
|
}
|
|
3685
|
+
function useLlmConfiguration(config) {
|
|
3686
|
+
return (options = {}) => apiRequestWrapper(config, options, useLlm_call);
|
|
3687
|
+
}
|
|
3688
|
+
|
|
3689
|
+
// src/embedding/output/BaseEmbeddingOutput.ts
|
|
3690
|
+
function BaseEmbeddingOutput(result) {
|
|
3691
|
+
const __result = Object.freeze({
|
|
3692
|
+
id: result.id || uuidv4(),
|
|
3693
|
+
model: result.model,
|
|
3694
|
+
usage: result.usage,
|
|
3695
|
+
embedding: [...result?.embedding || []],
|
|
3696
|
+
created: result?.created || (/* @__PURE__ */ new Date()).getTime()
|
|
3697
|
+
});
|
|
3698
|
+
function getResult() {
|
|
3699
|
+
return {
|
|
3700
|
+
id: __result.id,
|
|
3701
|
+
model: __result.model,
|
|
3702
|
+
created: __result.created,
|
|
3703
|
+
usage: __result.usage,
|
|
3704
|
+
embedding: __result.embedding
|
|
3705
|
+
};
|
|
3706
|
+
}
|
|
3707
|
+
function getEmbedding(index) {
|
|
3708
|
+
if (index && index > 0) {
|
|
3709
|
+
const arr = __result?.embedding;
|
|
3710
|
+
const val = arr[index];
|
|
3711
|
+
return val ? val : [];
|
|
3712
|
+
}
|
|
3713
|
+
return __result.embedding[0];
|
|
3714
|
+
}
|
|
3715
|
+
return {
|
|
3716
|
+
getEmbedding,
|
|
3717
|
+
getResult
|
|
3718
|
+
};
|
|
3719
|
+
}
|
|
3720
|
+
|
|
3721
|
+
// src/embedding/output/OpenAiEmbedding.ts
|
|
3722
|
+
function OpenAiEmbedding(result, config) {
|
|
3723
|
+
const __result = deepClone(result);
|
|
3724
|
+
const model = __result.model || config.model || "openai.unknown";
|
|
3725
|
+
const created = (/* @__PURE__ */ new Date()).getTime();
|
|
3726
|
+
const results = result?.data || [];
|
|
3727
|
+
const embedding = results.map((a) => a.embedding);
|
|
3728
|
+
const usage = {
|
|
3729
|
+
output_tokens: 0,
|
|
3730
|
+
input_tokens: result?.usage?.prompt_tokens,
|
|
3731
|
+
total_tokens: result?.usage?.total_tokens
|
|
3732
|
+
};
|
|
3733
|
+
return BaseEmbeddingOutput({
|
|
3734
|
+
model,
|
|
3735
|
+
created,
|
|
3736
|
+
usage,
|
|
3737
|
+
embedding
|
|
3738
|
+
});
|
|
3739
|
+
}
|
|
3740
|
+
|
|
3741
|
+
// src/embedding/output/AmazonTitan.ts
|
|
3742
|
+
function AmazonTitanEmbedding(result, config) {
|
|
3743
|
+
const __result = deepClone(result);
|
|
3744
|
+
const model = config.model || "amazon.unknown";
|
|
3745
|
+
const created = (/* @__PURE__ */ new Date()).getTime();
|
|
3746
|
+
const embedding = [__result.embedding];
|
|
3747
|
+
const usage = {
|
|
3748
|
+
output_tokens: 0,
|
|
3749
|
+
input_tokens: __result.inputTextTokenCount,
|
|
3750
|
+
total_tokens: __result.inputTextTokenCount
|
|
3751
|
+
};
|
|
3752
|
+
return BaseEmbeddingOutput({
|
|
3753
|
+
model,
|
|
3754
|
+
created,
|
|
3755
|
+
usage,
|
|
3756
|
+
embedding
|
|
3757
|
+
});
|
|
3758
|
+
}
|
|
3591
3759
|
|
|
3592
3760
|
// src/embedding/config.ts
|
|
3593
3761
|
var embeddingConfigs = {
|
|
@@ -3620,7 +3788,8 @@ var embeddingConfigs = {
|
|
|
3620
3788
|
encodingFormat: {
|
|
3621
3789
|
key: "encoding_format"
|
|
3622
3790
|
}
|
|
3623
|
-
}
|
|
3791
|
+
},
|
|
3792
|
+
transformResponse: OpenAiEmbedding
|
|
3624
3793
|
},
|
|
3625
3794
|
"amazon.embedding.v1": {
|
|
3626
3795
|
key: "amazon.embedding.v1",
|
|
@@ -3647,7 +3816,8 @@ var embeddingConfigs = {
|
|
|
3647
3816
|
dimensions: {
|
|
3648
3817
|
key: "dimensions"
|
|
3649
3818
|
}
|
|
3650
|
-
}
|
|
3819
|
+
},
|
|
3820
|
+
transformResponse: AmazonTitanEmbedding
|
|
3651
3821
|
}
|
|
3652
3822
|
};
|
|
3653
3823
|
function getEmbeddingConfig(provider) {
|
|
@@ -3661,77 +3831,6 @@ function getEmbeddingConfig(provider) {
|
|
|
3661
3831
|
throw new Error(`Invalid provider: ${provider}`);
|
|
3662
3832
|
}
|
|
3663
3833
|
|
|
3664
|
-
// src/embedding/output/BaseEmbeddingOutput.ts
|
|
3665
|
-
function BaseEmbeddingOutput(result) {
|
|
3666
|
-
const __result = Object.freeze({
|
|
3667
|
-
id: result.id || uuidv4(),
|
|
3668
|
-
model: result.model,
|
|
3669
|
-
usage: result.usage,
|
|
3670
|
-
embedding: [...result?.embedding || []],
|
|
3671
|
-
created: result?.created || (/* @__PURE__ */ new Date()).getTime()
|
|
3672
|
-
});
|
|
3673
|
-
function getResult() {
|
|
3674
|
-
return {
|
|
3675
|
-
id: __result.id,
|
|
3676
|
-
model: __result.model,
|
|
3677
|
-
created: __result.created,
|
|
3678
|
-
usage: __result.usage,
|
|
3679
|
-
embedding: __result.embedding
|
|
3680
|
-
};
|
|
3681
|
-
}
|
|
3682
|
-
function getEmbedding(index) {
|
|
3683
|
-
if (index && index > 0) {
|
|
3684
|
-
const arr = __result?.embedding;
|
|
3685
|
-
const val = arr[index];
|
|
3686
|
-
return val ? val : [];
|
|
3687
|
-
}
|
|
3688
|
-
return __result.embedding[0];
|
|
3689
|
-
}
|
|
3690
|
-
return {
|
|
3691
|
-
getEmbedding,
|
|
3692
|
-
getResult
|
|
3693
|
-
};
|
|
3694
|
-
}
|
|
3695
|
-
|
|
3696
|
-
// src/embedding/output/AmazonTitan.ts
|
|
3697
|
-
function AmazonTitanEmbedding(result, config) {
|
|
3698
|
-
const __result = deepClone(result);
|
|
3699
|
-
const model = config.model || "amazon.unknown";
|
|
3700
|
-
const created = (/* @__PURE__ */ new Date()).getTime();
|
|
3701
|
-
const embedding = [__result.embedding];
|
|
3702
|
-
const usage = {
|
|
3703
|
-
output_tokens: 0,
|
|
3704
|
-
input_tokens: __result.inputTextTokenCount,
|
|
3705
|
-
total_tokens: __result.inputTextTokenCount
|
|
3706
|
-
};
|
|
3707
|
-
return BaseEmbeddingOutput({
|
|
3708
|
-
model,
|
|
3709
|
-
created,
|
|
3710
|
-
usage,
|
|
3711
|
-
embedding
|
|
3712
|
-
});
|
|
3713
|
-
}
|
|
3714
|
-
|
|
3715
|
-
// src/embedding/output/OpenAiEmbedding.ts
|
|
3716
|
-
function OpenAiEmbedding(result, config) {
|
|
3717
|
-
const __result = deepClone(result);
|
|
3718
|
-
const model = __result.model || config.model || "openai.unknown";
|
|
3719
|
-
const created = (/* @__PURE__ */ new Date()).getTime();
|
|
3720
|
-
const results = result?.data || [];
|
|
3721
|
-
const embedding = results.map((a) => a.embedding);
|
|
3722
|
-
const usage = {
|
|
3723
|
-
output_tokens: 0,
|
|
3724
|
-
input_tokens: result?.usage?.prompt_tokens,
|
|
3725
|
-
total_tokens: result?.usage?.total_tokens
|
|
3726
|
-
};
|
|
3727
|
-
return BaseEmbeddingOutput({
|
|
3728
|
-
model,
|
|
3729
|
-
created,
|
|
3730
|
-
usage,
|
|
3731
|
-
embedding
|
|
3732
|
-
});
|
|
3733
|
-
}
|
|
3734
|
-
|
|
3735
3834
|
// src/embedding/output/getEmbeddingOutputParser.ts
|
|
3736
3835
|
function getEmbeddingOutputParser(config, response) {
|
|
3737
3836
|
switch (config.key) {
|
|
@@ -4749,6 +4848,9 @@ var Dialogue = class extends BaseStateItem {
|
|
|
4749
4848
|
* @returns this for chaining
|
|
4750
4849
|
*/
|
|
4751
4850
|
addFromOutput(output) {
|
|
4851
|
+
if (!output || typeof output !== "object") {
|
|
4852
|
+
return this;
|
|
4853
|
+
}
|
|
4752
4854
|
const result = "getResult" in output ? output.getResult() : output;
|
|
4753
4855
|
if (!result || typeof result !== "object") {
|
|
4754
4856
|
return this;
|
|
@@ -4884,6 +4986,7 @@ export {
|
|
|
4884
4986
|
createEmbedding,
|
|
4885
4987
|
createLlmExecutor,
|
|
4886
4988
|
createLlmFunctionExecutor,
|
|
4989
|
+
createOpenAiCompatibleConfiguration,
|
|
4887
4990
|
createParser,
|
|
4888
4991
|
createPrompt,
|
|
4889
4992
|
createState,
|
|
@@ -4894,5 +4997,6 @@ export {
|
|
|
4894
4997
|
registerPartials,
|
|
4895
4998
|
useExecutors,
|
|
4896
4999
|
useLlm,
|
|
5000
|
+
useLlmConfiguration,
|
|
4897
5001
|
utils_exports as utils
|
|
4898
5002
|
};
|