@ljoukov/llm 4.0.7 → 4.0.9
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/README.md +2 -2
- package/dist/index.cjs +293 -151
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +293 -151
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -329,6 +329,34 @@ function estimateCallCostUsd({
|
|
|
329
329
|
import os2 from "os";
|
|
330
330
|
import { TextDecoder } from "util";
|
|
331
331
|
|
|
332
|
+
// src/utils/runtimeSingleton.ts
|
|
333
|
+
var runtimeSingletonStoreKey = /* @__PURE__ */ Symbol.for("@ljoukov/llm.runtimeSingletonStore");
|
|
334
|
+
function getRuntimeSingletonStore() {
|
|
335
|
+
const globalObject = globalThis;
|
|
336
|
+
const existingStore = globalObject[runtimeSingletonStoreKey];
|
|
337
|
+
if (existingStore) {
|
|
338
|
+
return existingStore;
|
|
339
|
+
}
|
|
340
|
+
const store = /* @__PURE__ */ new Map();
|
|
341
|
+
Object.defineProperty(globalObject, runtimeSingletonStoreKey, {
|
|
342
|
+
value: store,
|
|
343
|
+
enumerable: false,
|
|
344
|
+
configurable: false,
|
|
345
|
+
writable: false
|
|
346
|
+
});
|
|
347
|
+
return store;
|
|
348
|
+
}
|
|
349
|
+
function getRuntimeSingleton(key, create) {
|
|
350
|
+
const store = getRuntimeSingletonStore();
|
|
351
|
+
const existingValue = store.get(key);
|
|
352
|
+
if (existingValue !== void 0) {
|
|
353
|
+
return existingValue;
|
|
354
|
+
}
|
|
355
|
+
const createdValue = create();
|
|
356
|
+
store.set(key, createdValue);
|
|
357
|
+
return createdValue;
|
|
358
|
+
}
|
|
359
|
+
|
|
332
360
|
// src/openai/chatgpt-auth.ts
|
|
333
361
|
import { Buffer as Buffer2 } from "buffer";
|
|
334
362
|
import fs2 from "fs";
|
|
@@ -339,14 +367,16 @@ import { z } from "zod";
|
|
|
339
367
|
// src/utils/env.ts
|
|
340
368
|
import fs from "fs";
|
|
341
369
|
import path from "path";
|
|
342
|
-
var
|
|
370
|
+
var envState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.envState"), () => ({
|
|
371
|
+
envLoaded: false
|
|
372
|
+
}));
|
|
343
373
|
function loadLocalEnv() {
|
|
344
|
-
if (envLoaded) {
|
|
374
|
+
if (envState.envLoaded) {
|
|
345
375
|
return;
|
|
346
376
|
}
|
|
347
377
|
const envPath = path.join(process.cwd(), ".env.local");
|
|
348
378
|
loadEnvFromFile(envPath, { override: false });
|
|
349
|
-
envLoaded = true;
|
|
379
|
+
envState.envLoaded = true;
|
|
350
380
|
}
|
|
351
381
|
function loadEnvFromFile(filePath, { override = false } = {}) {
|
|
352
382
|
let content;
|
|
@@ -432,8 +462,10 @@ var ExchangeResponseSchema = z.object({
|
|
|
432
462
|
expires_in: z.union([z.number(), z.string()]),
|
|
433
463
|
id_token: z.string().optional()
|
|
434
464
|
});
|
|
435
|
-
var
|
|
436
|
-
|
|
465
|
+
var chatGptAuthState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.chatGptAuthState"), () => ({
|
|
466
|
+
cachedProfile: null,
|
|
467
|
+
refreshPromise: null
|
|
468
|
+
}));
|
|
437
469
|
async function fetchChatGptAuthProfileFromTokenProvider(options) {
|
|
438
470
|
const base = options.baseUrl.replace(/\/+$/u, "");
|
|
439
471
|
const store = options.store?.trim() ? options.store.trim() : "kv";
|
|
@@ -534,13 +566,13 @@ async function getChatGptAuthProfile() {
|
|
|
534
566
|
const tokenProviderUrl = process.env[CHATGPT_AUTH_TOKEN_PROVIDER_URL_ENV];
|
|
535
567
|
const tokenProviderKey = process.env[CHATGPT_AUTH_TOKEN_PROVIDER_API_KEY_ENV] ?? process.env[CHATGPT_AUTH_API_KEY_ENV];
|
|
536
568
|
if (tokenProviderUrl && tokenProviderUrl.trim().length > 0 && tokenProviderKey && tokenProviderKey.trim().length > 0) {
|
|
537
|
-
if (cachedProfile && !isExpired(cachedProfile)) {
|
|
538
|
-
return cachedProfile;
|
|
569
|
+
if (chatGptAuthState.cachedProfile && !isExpired(chatGptAuthState.cachedProfile)) {
|
|
570
|
+
return chatGptAuthState.cachedProfile;
|
|
539
571
|
}
|
|
540
|
-
if (refreshPromise) {
|
|
541
|
-
return refreshPromise;
|
|
572
|
+
if (chatGptAuthState.refreshPromise) {
|
|
573
|
+
return chatGptAuthState.refreshPromise;
|
|
542
574
|
}
|
|
543
|
-
refreshPromise = (async () => {
|
|
575
|
+
chatGptAuthState.refreshPromise = (async () => {
|
|
544
576
|
try {
|
|
545
577
|
const store = process.env[CHATGPT_AUTH_TOKEN_PROVIDER_STORE_ENV];
|
|
546
578
|
const profile = await fetchChatGptAuthProfileFromTokenProvider({
|
|
@@ -548,31 +580,31 @@ async function getChatGptAuthProfile() {
|
|
|
548
580
|
apiKey: tokenProviderKey,
|
|
549
581
|
store: store ?? void 0
|
|
550
582
|
});
|
|
551
|
-
cachedProfile = profile;
|
|
583
|
+
chatGptAuthState.cachedProfile = profile;
|
|
552
584
|
return profile;
|
|
553
585
|
} finally {
|
|
554
|
-
refreshPromise = null;
|
|
586
|
+
chatGptAuthState.refreshPromise = null;
|
|
555
587
|
}
|
|
556
588
|
})();
|
|
557
|
-
return refreshPromise;
|
|
589
|
+
return chatGptAuthState.refreshPromise;
|
|
558
590
|
}
|
|
559
|
-
if (cachedProfile && !isExpired(cachedProfile)) {
|
|
560
|
-
return cachedProfile;
|
|
591
|
+
if (chatGptAuthState.cachedProfile && !isExpired(chatGptAuthState.cachedProfile)) {
|
|
592
|
+
return chatGptAuthState.cachedProfile;
|
|
561
593
|
}
|
|
562
|
-
if (refreshPromise) {
|
|
563
|
-
return refreshPromise;
|
|
594
|
+
if (chatGptAuthState.refreshPromise) {
|
|
595
|
+
return chatGptAuthState.refreshPromise;
|
|
564
596
|
}
|
|
565
|
-
refreshPromise = (async () => {
|
|
597
|
+
chatGptAuthState.refreshPromise = (async () => {
|
|
566
598
|
try {
|
|
567
|
-
const baseProfile = cachedProfile ?? loadAuthProfileFromCodexStore();
|
|
599
|
+
const baseProfile = chatGptAuthState.cachedProfile ?? loadAuthProfileFromCodexStore();
|
|
568
600
|
const profile = isExpired(baseProfile) ? await refreshAndPersistCodexProfile(baseProfile) : baseProfile;
|
|
569
|
-
cachedProfile = profile;
|
|
601
|
+
chatGptAuthState.cachedProfile = profile;
|
|
570
602
|
return profile;
|
|
571
603
|
} finally {
|
|
572
|
-
refreshPromise = null;
|
|
604
|
+
chatGptAuthState.refreshPromise = null;
|
|
573
605
|
}
|
|
574
606
|
})();
|
|
575
|
-
return refreshPromise;
|
|
607
|
+
return chatGptAuthState.refreshPromise;
|
|
576
608
|
}
|
|
577
609
|
function resolveCodexHome() {
|
|
578
610
|
const codexHome = process.env.CODEX_HOME;
|
|
@@ -1304,8 +1336,10 @@ function createAbortError(reason) {
|
|
|
1304
1336
|
// src/openai/chatgpt-codex.ts
|
|
1305
1337
|
var CHATGPT_CODEX_ENDPOINT = "https://chatgpt.com/backend-api/codex/responses";
|
|
1306
1338
|
var CHATGPT_RESPONSES_EXPERIMENTAL_HEADER = "responses=experimental";
|
|
1307
|
-
var
|
|
1308
|
-
|
|
1339
|
+
var chatGptCodexState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.chatGptCodexState"), () => ({
|
|
1340
|
+
cachedResponsesWebSocketMode: null,
|
|
1341
|
+
chatGptResponsesWebSocketDisabled: false
|
|
1342
|
+
}));
|
|
1309
1343
|
async function streamChatGptCodexResponse(options) {
|
|
1310
1344
|
const { access, accountId } = await getChatGptAuthProfile();
|
|
1311
1345
|
const mode = resolveChatGptResponsesWebSocketMode();
|
|
@@ -1331,7 +1365,7 @@ async function streamChatGptCodexResponse(options) {
|
|
|
1331
1365
|
}
|
|
1332
1366
|
};
|
|
1333
1367
|
};
|
|
1334
|
-
if (mode === "off" || chatGptResponsesWebSocketDisabled) {
|
|
1368
|
+
if (mode === "off" || chatGptCodexState.chatGptResponsesWebSocketDisabled) {
|
|
1335
1369
|
return fallbackStreamFactory();
|
|
1336
1370
|
}
|
|
1337
1371
|
const websocketHeaders = buildChatGptCodexHeaders({
|
|
@@ -1350,7 +1384,7 @@ async function streamChatGptCodexResponse(options) {
|
|
|
1350
1384
|
}),
|
|
1351
1385
|
createFallbackStream: fallbackStreamFactory,
|
|
1352
1386
|
onWebSocketFallback: () => {
|
|
1353
|
-
chatGptResponsesWebSocketDisabled = true;
|
|
1387
|
+
chatGptCodexState.chatGptResponsesWebSocketDisabled = true;
|
|
1354
1388
|
}
|
|
1355
1389
|
});
|
|
1356
1390
|
}
|
|
@@ -1380,14 +1414,14 @@ async function streamChatGptCodexResponseSse(options) {
|
|
|
1380
1414
|
return parseEventStream(body);
|
|
1381
1415
|
}
|
|
1382
1416
|
function resolveChatGptResponsesWebSocketMode() {
|
|
1383
|
-
if (cachedResponsesWebSocketMode) {
|
|
1384
|
-
return cachedResponsesWebSocketMode;
|
|
1417
|
+
if (chatGptCodexState.cachedResponsesWebSocketMode) {
|
|
1418
|
+
return chatGptCodexState.cachedResponsesWebSocketMode;
|
|
1385
1419
|
}
|
|
1386
|
-
cachedResponsesWebSocketMode = resolveResponsesWebSocketMode(
|
|
1420
|
+
chatGptCodexState.cachedResponsesWebSocketMode = resolveResponsesWebSocketMode(
|
|
1387
1421
|
process.env.CHATGPT_RESPONSES_WEBSOCKET_MODE ?? process.env.OPENAI_RESPONSES_WEBSOCKET_MODE,
|
|
1388
1422
|
"auto"
|
|
1389
1423
|
);
|
|
1390
|
-
return cachedResponsesWebSocketMode;
|
|
1424
|
+
return chatGptCodexState.cachedResponsesWebSocketMode;
|
|
1391
1425
|
}
|
|
1392
1426
|
function buildChatGptCodexHeaders(options) {
|
|
1393
1427
|
const openAiBeta = options.useWebSocket ? mergeOpenAiBetaHeader(
|
|
@@ -1425,8 +1459,8 @@ async function collectChatGptCodexResponse(options) {
|
|
|
1425
1459
|
}
|
|
1426
1460
|
});
|
|
1427
1461
|
} catch (error) {
|
|
1428
|
-
if (!sawAnyDelta && !retriedViaSseFallback && shouldRetryViaSseFallback(error) && !chatGptResponsesWebSocketDisabled) {
|
|
1429
|
-
chatGptResponsesWebSocketDisabled = true;
|
|
1462
|
+
if (!sawAnyDelta && !retriedViaSseFallback && shouldRetryViaSseFallback(error) && !chatGptCodexState.chatGptResponsesWebSocketDisabled) {
|
|
1463
|
+
chatGptCodexState.chatGptResponsesWebSocketDisabled = true;
|
|
1430
1464
|
retriedViaSseFallback = true;
|
|
1431
1465
|
continue;
|
|
1432
1466
|
}
|
|
@@ -1662,7 +1696,12 @@ var MODEL_CONCURRENCY_PROVIDERS = [
|
|
|
1662
1696
|
"google",
|
|
1663
1697
|
"fireworks"
|
|
1664
1698
|
];
|
|
1665
|
-
var
|
|
1699
|
+
var modelConcurrencyState = getRuntimeSingleton(
|
|
1700
|
+
/* @__PURE__ */ Symbol.for("@ljoukov/llm.modelConcurrencyState"),
|
|
1701
|
+
() => ({
|
|
1702
|
+
configuredModelConcurrency: normalizeModelConcurrencyConfig({})
|
|
1703
|
+
})
|
|
1704
|
+
);
|
|
1666
1705
|
function clampModelConcurrencyCap(value) {
|
|
1667
1706
|
if (!Number.isFinite(value)) {
|
|
1668
1707
|
return DEFAULT_MODEL_CONCURRENCY_CAP;
|
|
@@ -1736,14 +1775,14 @@ function resolveDefaultProviderCap(provider, modelId) {
|
|
|
1736
1775
|
return DEFAULT_FIREWORKS_MODEL_CONCURRENCY_CAP;
|
|
1737
1776
|
}
|
|
1738
1777
|
function configureModelConcurrency(config = {}) {
|
|
1739
|
-
configuredModelConcurrency = normalizeModelConcurrencyConfig(config);
|
|
1778
|
+
modelConcurrencyState.configuredModelConcurrency = normalizeModelConcurrencyConfig(config);
|
|
1740
1779
|
}
|
|
1741
1780
|
function resetModelConcurrencyConfig() {
|
|
1742
|
-
configuredModelConcurrency = normalizeModelConcurrencyConfig({});
|
|
1781
|
+
modelConcurrencyState.configuredModelConcurrency = normalizeModelConcurrencyConfig({});
|
|
1743
1782
|
}
|
|
1744
1783
|
function resolveModelConcurrencyCap(options) {
|
|
1745
1784
|
const modelId = options.modelId ? normalizeModelIdForConfig(options.modelId) : void 0;
|
|
1746
|
-
const config = options.config ? normalizeModelConcurrencyConfig(options.config) : configuredModelConcurrency;
|
|
1785
|
+
const config = options.config ? normalizeModelConcurrencyConfig(options.config) : modelConcurrencyState.configuredModelConcurrency;
|
|
1747
1786
|
const providerModelCap = modelId ? config.providerModelCaps[options.provider].get(modelId) : void 0;
|
|
1748
1787
|
if (providerModelCap !== void 0) {
|
|
1749
1788
|
return providerModelCap;
|
|
@@ -1977,32 +2016,37 @@ import OpenAI from "openai";
|
|
|
1977
2016
|
import { Agent, fetch as undiciFetch } from "undici";
|
|
1978
2017
|
var DEFAULT_FIREWORKS_BASE_URL = "https://api.fireworks.ai/inference/v1";
|
|
1979
2018
|
var DEFAULT_FIREWORKS_TIMEOUT_MS = 15 * 6e4;
|
|
1980
|
-
var
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
2019
|
+
var fireworksClientState = getRuntimeSingleton(
|
|
2020
|
+
/* @__PURE__ */ Symbol.for("@ljoukov/llm.fireworksClientState"),
|
|
2021
|
+
() => ({
|
|
2022
|
+
cachedClient: null,
|
|
2023
|
+
cachedFetch: null,
|
|
2024
|
+
cachedBaseUrl: null,
|
|
2025
|
+
cachedApiKey: null,
|
|
2026
|
+
cachedTimeoutMs: null
|
|
2027
|
+
})
|
|
2028
|
+
);
|
|
1985
2029
|
function resolveTimeoutMs() {
|
|
1986
|
-
if (cachedTimeoutMs !== null) {
|
|
1987
|
-
return cachedTimeoutMs;
|
|
2030
|
+
if (fireworksClientState.cachedTimeoutMs !== null) {
|
|
2031
|
+
return fireworksClientState.cachedTimeoutMs;
|
|
1988
2032
|
}
|
|
1989
2033
|
const raw = process.env.FIREWORKS_TIMEOUT_MS;
|
|
1990
2034
|
const parsed = raw ? Number(raw) : Number.NaN;
|
|
1991
|
-
cachedTimeoutMs = Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_FIREWORKS_TIMEOUT_MS;
|
|
1992
|
-
return cachedTimeoutMs;
|
|
2035
|
+
fireworksClientState.cachedTimeoutMs = Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_FIREWORKS_TIMEOUT_MS;
|
|
2036
|
+
return fireworksClientState.cachedTimeoutMs;
|
|
1993
2037
|
}
|
|
1994
2038
|
function resolveBaseUrl() {
|
|
1995
|
-
if (cachedBaseUrl !== null) {
|
|
1996
|
-
return cachedBaseUrl;
|
|
2039
|
+
if (fireworksClientState.cachedBaseUrl !== null) {
|
|
2040
|
+
return fireworksClientState.cachedBaseUrl;
|
|
1997
2041
|
}
|
|
1998
2042
|
loadLocalEnv();
|
|
1999
2043
|
const raw = process.env.FIREWORKS_BASE_URL?.trim();
|
|
2000
|
-
cachedBaseUrl = raw && raw.length > 0 ? raw : DEFAULT_FIREWORKS_BASE_URL;
|
|
2001
|
-
return cachedBaseUrl;
|
|
2044
|
+
fireworksClientState.cachedBaseUrl = raw && raw.length > 0 ? raw : DEFAULT_FIREWORKS_BASE_URL;
|
|
2045
|
+
return fireworksClientState.cachedBaseUrl;
|
|
2002
2046
|
}
|
|
2003
2047
|
function resolveApiKey() {
|
|
2004
|
-
if (cachedApiKey !== null) {
|
|
2005
|
-
return cachedApiKey;
|
|
2048
|
+
if (fireworksClientState.cachedApiKey !== null) {
|
|
2049
|
+
return fireworksClientState.cachedApiKey;
|
|
2006
2050
|
}
|
|
2007
2051
|
loadLocalEnv();
|
|
2008
2052
|
const raw = process.env.FIREWORKS_TOKEN ?? process.env.FIREWORKS_API_KEY;
|
|
@@ -2012,46 +2056,51 @@ function resolveApiKey() {
|
|
|
2012
2056
|
"FIREWORKS_TOKEN (or FIREWORKS_API_KEY) must be provided to access Fireworks APIs."
|
|
2013
2057
|
);
|
|
2014
2058
|
}
|
|
2015
|
-
cachedApiKey = token;
|
|
2016
|
-
return cachedApiKey;
|
|
2059
|
+
fireworksClientState.cachedApiKey = token;
|
|
2060
|
+
return fireworksClientState.cachedApiKey;
|
|
2017
2061
|
}
|
|
2018
2062
|
function getFireworksFetch() {
|
|
2019
|
-
if (cachedFetch) {
|
|
2020
|
-
return cachedFetch;
|
|
2063
|
+
if (fireworksClientState.cachedFetch) {
|
|
2064
|
+
return fireworksClientState.cachedFetch;
|
|
2021
2065
|
}
|
|
2022
2066
|
const timeoutMs = resolveTimeoutMs();
|
|
2023
2067
|
const dispatcher = new Agent({
|
|
2024
2068
|
bodyTimeout: timeoutMs,
|
|
2025
2069
|
headersTimeout: timeoutMs
|
|
2026
2070
|
});
|
|
2027
|
-
cachedFetch = ((input, init) => {
|
|
2071
|
+
fireworksClientState.cachedFetch = ((input, init) => {
|
|
2028
2072
|
return undiciFetch(input, {
|
|
2029
2073
|
...init ?? {},
|
|
2030
2074
|
dispatcher
|
|
2031
2075
|
});
|
|
2032
2076
|
});
|
|
2033
|
-
return cachedFetch;
|
|
2077
|
+
return fireworksClientState.cachedFetch;
|
|
2034
2078
|
}
|
|
2035
2079
|
function getFireworksClient() {
|
|
2036
|
-
if (cachedClient) {
|
|
2037
|
-
return cachedClient;
|
|
2080
|
+
if (fireworksClientState.cachedClient) {
|
|
2081
|
+
return fireworksClientState.cachedClient;
|
|
2038
2082
|
}
|
|
2039
|
-
cachedClient = new OpenAI({
|
|
2083
|
+
fireworksClientState.cachedClient = new OpenAI({
|
|
2040
2084
|
apiKey: resolveApiKey(),
|
|
2041
2085
|
baseURL: resolveBaseUrl(),
|
|
2042
2086
|
timeout: resolveTimeoutMs(),
|
|
2043
2087
|
fetch: getFireworksFetch()
|
|
2044
2088
|
});
|
|
2045
|
-
return cachedClient;
|
|
2089
|
+
return fireworksClientState.cachedClient;
|
|
2046
2090
|
}
|
|
2047
2091
|
|
|
2048
2092
|
// src/fireworks/calls.ts
|
|
2049
2093
|
var DEFAULT_SCHEDULER_KEY = "__default__";
|
|
2050
|
-
var
|
|
2094
|
+
var fireworksCallState = getRuntimeSingleton(
|
|
2095
|
+
/* @__PURE__ */ Symbol.for("@ljoukov/llm.fireworksCallState"),
|
|
2096
|
+
() => ({
|
|
2097
|
+
schedulerByModel: /* @__PURE__ */ new Map()
|
|
2098
|
+
})
|
|
2099
|
+
);
|
|
2051
2100
|
function getSchedulerForModel(modelId) {
|
|
2052
2101
|
const normalizedModelId = modelId?.trim();
|
|
2053
2102
|
const schedulerKey = normalizedModelId && normalizedModelId.length > 0 ? normalizedModelId : DEFAULT_SCHEDULER_KEY;
|
|
2054
|
-
const existing = schedulerByModel.get(schedulerKey);
|
|
2103
|
+
const existing = fireworksCallState.schedulerByModel.get(schedulerKey);
|
|
2055
2104
|
if (existing) {
|
|
2056
2105
|
return existing;
|
|
2057
2106
|
}
|
|
@@ -2063,7 +2112,7 @@ function getSchedulerForModel(modelId) {
|
|
|
2063
2112
|
minIntervalBetweenStartMs: 200,
|
|
2064
2113
|
startJitterMs: 200
|
|
2065
2114
|
});
|
|
2066
|
-
schedulerByModel.set(schedulerKey, created);
|
|
2115
|
+
fireworksCallState.schedulerByModel.set(schedulerKey, created);
|
|
2067
2116
|
return created;
|
|
2068
2117
|
}
|
|
2069
2118
|
async function runFireworksCall(fn, modelId, runOptions) {
|
|
@@ -2110,7 +2159,10 @@ var ServiceAccountSchema = z2.object({
|
|
|
2110
2159
|
privateKey: private_key.replace(/\\n/g, "\n"),
|
|
2111
2160
|
tokenUri: token_uri
|
|
2112
2161
|
}));
|
|
2113
|
-
var
|
|
2162
|
+
var googleAuthState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.googleAuthState"), () => ({
|
|
2163
|
+
cachedServiceAccount: null,
|
|
2164
|
+
authClientCache: /* @__PURE__ */ new Map()
|
|
2165
|
+
}));
|
|
2114
2166
|
function parseGoogleServiceAccount(input) {
|
|
2115
2167
|
let parsed;
|
|
2116
2168
|
try {
|
|
@@ -2121,16 +2173,16 @@ function parseGoogleServiceAccount(input) {
|
|
|
2121
2173
|
return ServiceAccountSchema.parse(parsed);
|
|
2122
2174
|
}
|
|
2123
2175
|
function getGoogleServiceAccount() {
|
|
2124
|
-
if (cachedServiceAccount) {
|
|
2125
|
-
return cachedServiceAccount;
|
|
2176
|
+
if (googleAuthState.cachedServiceAccount) {
|
|
2177
|
+
return googleAuthState.cachedServiceAccount;
|
|
2126
2178
|
}
|
|
2127
2179
|
loadLocalEnv();
|
|
2128
2180
|
const raw = process.env.GOOGLE_SERVICE_ACCOUNT_JSON;
|
|
2129
2181
|
if (!raw || raw.trim().length === 0) {
|
|
2130
2182
|
throw new Error("GOOGLE_SERVICE_ACCOUNT_JSON must be provided for Google APIs access.");
|
|
2131
2183
|
}
|
|
2132
|
-
cachedServiceAccount = parseGoogleServiceAccount(raw);
|
|
2133
|
-
return cachedServiceAccount;
|
|
2184
|
+
googleAuthState.cachedServiceAccount = parseGoogleServiceAccount(raw);
|
|
2185
|
+
return googleAuthState.cachedServiceAccount;
|
|
2134
2186
|
}
|
|
2135
2187
|
function normaliseScopes(scopes) {
|
|
2136
2188
|
if (!scopes) {
|
|
@@ -2182,8 +2234,10 @@ function isGeminiImageModelId(value) {
|
|
|
2182
2234
|
}
|
|
2183
2235
|
var CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
|
|
2184
2236
|
var DEFAULT_VERTEX_LOCATION = "global";
|
|
2185
|
-
var
|
|
2186
|
-
|
|
2237
|
+
var geminiClientState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.geminiClientState"), () => ({
|
|
2238
|
+
geminiConfiguration: {},
|
|
2239
|
+
clientPromise: void 0
|
|
2240
|
+
}));
|
|
2187
2241
|
function normaliseConfigValue(value) {
|
|
2188
2242
|
if (value === void 0 || value === null) {
|
|
2189
2243
|
return void 0;
|
|
@@ -2194,14 +2248,14 @@ function normaliseConfigValue(value) {
|
|
|
2194
2248
|
function configureGemini(options = {}) {
|
|
2195
2249
|
const nextProjectId = normaliseConfigValue(options.projectId);
|
|
2196
2250
|
const nextLocation = normaliseConfigValue(options.location);
|
|
2197
|
-
geminiConfiguration = {
|
|
2198
|
-
projectId: nextProjectId !== void 0 ? nextProjectId : geminiConfiguration.projectId,
|
|
2199
|
-
location: nextLocation !== void 0 ? nextLocation : geminiConfiguration.location
|
|
2251
|
+
geminiClientState.geminiConfiguration = {
|
|
2252
|
+
projectId: nextProjectId !== void 0 ? nextProjectId : geminiClientState.geminiConfiguration.projectId,
|
|
2253
|
+
location: nextLocation !== void 0 ? nextLocation : geminiClientState.geminiConfiguration.location
|
|
2200
2254
|
};
|
|
2201
|
-
clientPromise = void 0;
|
|
2255
|
+
geminiClientState.clientPromise = void 0;
|
|
2202
2256
|
}
|
|
2203
2257
|
function resolveProjectId() {
|
|
2204
|
-
const override = geminiConfiguration.projectId;
|
|
2258
|
+
const override = geminiClientState.geminiConfiguration.projectId;
|
|
2205
2259
|
if (override) {
|
|
2206
2260
|
return override;
|
|
2207
2261
|
}
|
|
@@ -2209,15 +2263,15 @@ function resolveProjectId() {
|
|
|
2209
2263
|
return serviceAccount.projectId;
|
|
2210
2264
|
}
|
|
2211
2265
|
function resolveLocation() {
|
|
2212
|
-
const override = geminiConfiguration.location;
|
|
2266
|
+
const override = geminiClientState.geminiConfiguration.location;
|
|
2213
2267
|
if (override) {
|
|
2214
2268
|
return override;
|
|
2215
2269
|
}
|
|
2216
2270
|
return DEFAULT_VERTEX_LOCATION;
|
|
2217
2271
|
}
|
|
2218
2272
|
async function getGeminiClient() {
|
|
2219
|
-
if (!clientPromise) {
|
|
2220
|
-
clientPromise = Promise.resolve().then(() => {
|
|
2273
|
+
if (!geminiClientState.clientPromise) {
|
|
2274
|
+
geminiClientState.clientPromise = Promise.resolve().then(() => {
|
|
2221
2275
|
const projectId = resolveProjectId();
|
|
2222
2276
|
const location = resolveLocation();
|
|
2223
2277
|
const googleAuthOptions = getGoogleAuthOptions(CLOUD_PLATFORM_SCOPE);
|
|
@@ -2229,7 +2283,7 @@ async function getGeminiClient() {
|
|
|
2229
2283
|
});
|
|
2230
2284
|
});
|
|
2231
2285
|
}
|
|
2232
|
-
return clientPromise;
|
|
2286
|
+
return geminiClientState.clientPromise;
|
|
2233
2287
|
}
|
|
2234
2288
|
|
|
2235
2289
|
// src/google/calls.ts
|
|
@@ -2425,11 +2479,13 @@ function retryDelayMs(attempt) {
|
|
|
2425
2479
|
return base + jitter;
|
|
2426
2480
|
}
|
|
2427
2481
|
var DEFAULT_SCHEDULER_KEY2 = "__default__";
|
|
2428
|
-
var
|
|
2482
|
+
var googleCallState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.googleCallState"), () => ({
|
|
2483
|
+
schedulerByModel: /* @__PURE__ */ new Map()
|
|
2484
|
+
}));
|
|
2429
2485
|
function getSchedulerForModel2(modelId) {
|
|
2430
2486
|
const normalizedModelId = modelId?.trim();
|
|
2431
2487
|
const schedulerKey = normalizedModelId && normalizedModelId.length > 0 ? normalizedModelId : DEFAULT_SCHEDULER_KEY2;
|
|
2432
|
-
const existing =
|
|
2488
|
+
const existing = googleCallState.schedulerByModel.get(schedulerKey);
|
|
2433
2489
|
if (existing) {
|
|
2434
2490
|
return existing;
|
|
2435
2491
|
}
|
|
@@ -2452,7 +2508,7 @@ function getSchedulerForModel2(modelId) {
|
|
|
2452
2508
|
}
|
|
2453
2509
|
}
|
|
2454
2510
|
});
|
|
2455
|
-
|
|
2511
|
+
googleCallState.schedulerByModel.set(schedulerKey, created);
|
|
2456
2512
|
return created;
|
|
2457
2513
|
}
|
|
2458
2514
|
async function runGeminiCall(fn, modelId, runOptions) {
|
|
@@ -2462,53 +2518,55 @@ async function runGeminiCall(fn, modelId, runOptions) {
|
|
|
2462
2518
|
// src/openai/client.ts
|
|
2463
2519
|
import OpenAI2 from "openai";
|
|
2464
2520
|
import { Agent as Agent2, fetch as undiciFetch2 } from "undici";
|
|
2465
|
-
var
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2521
|
+
var openAiClientState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.openAiClientState"), () => ({
|
|
2522
|
+
cachedApiKey: null,
|
|
2523
|
+
cachedClient: null,
|
|
2524
|
+
cachedFetch: null,
|
|
2525
|
+
cachedTimeoutMs: null,
|
|
2526
|
+
openAiResponsesWebSocketMode: null,
|
|
2527
|
+
openAiResponsesWebSocketDisabled: false
|
|
2528
|
+
}));
|
|
2471
2529
|
var DEFAULT_OPENAI_TIMEOUT_MS = 15 * 6e4;
|
|
2472
2530
|
function resolveOpenAiTimeoutMs() {
|
|
2473
|
-
if (
|
|
2474
|
-
return
|
|
2531
|
+
if (openAiClientState.cachedTimeoutMs !== null) {
|
|
2532
|
+
return openAiClientState.cachedTimeoutMs;
|
|
2475
2533
|
}
|
|
2476
2534
|
const raw = process.env.OPENAI_STREAM_TIMEOUT_MS ?? process.env.OPENAI_TIMEOUT_MS;
|
|
2477
2535
|
const parsed = raw ? Number(raw) : Number.NaN;
|
|
2478
|
-
|
|
2479
|
-
return
|
|
2536
|
+
openAiClientState.cachedTimeoutMs = Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_OPENAI_TIMEOUT_MS;
|
|
2537
|
+
return openAiClientState.cachedTimeoutMs;
|
|
2480
2538
|
}
|
|
2481
2539
|
function getOpenAiFetch() {
|
|
2482
|
-
if (
|
|
2483
|
-
return
|
|
2540
|
+
if (openAiClientState.cachedFetch) {
|
|
2541
|
+
return openAiClientState.cachedFetch;
|
|
2484
2542
|
}
|
|
2485
2543
|
const timeoutMs = resolveOpenAiTimeoutMs();
|
|
2486
2544
|
const dispatcher = new Agent2({
|
|
2487
2545
|
bodyTimeout: timeoutMs,
|
|
2488
2546
|
headersTimeout: timeoutMs
|
|
2489
2547
|
});
|
|
2490
|
-
|
|
2548
|
+
openAiClientState.cachedFetch = ((input, init) => {
|
|
2491
2549
|
return undiciFetch2(input, {
|
|
2492
2550
|
...init ?? {},
|
|
2493
2551
|
dispatcher
|
|
2494
2552
|
});
|
|
2495
2553
|
});
|
|
2496
|
-
return
|
|
2554
|
+
return openAiClientState.cachedFetch;
|
|
2497
2555
|
}
|
|
2498
2556
|
function resolveOpenAiBaseUrl() {
|
|
2499
2557
|
loadLocalEnv();
|
|
2500
2558
|
return process.env.OPENAI_BASE_URL?.trim() || "https://api.openai.com/v1";
|
|
2501
2559
|
}
|
|
2502
2560
|
function resolveOpenAiResponsesWebSocketMode() {
|
|
2503
|
-
if (openAiResponsesWebSocketMode) {
|
|
2504
|
-
return openAiResponsesWebSocketMode;
|
|
2561
|
+
if (openAiClientState.openAiResponsesWebSocketMode) {
|
|
2562
|
+
return openAiClientState.openAiResponsesWebSocketMode;
|
|
2505
2563
|
}
|
|
2506
2564
|
loadLocalEnv();
|
|
2507
|
-
openAiResponsesWebSocketMode = resolveResponsesWebSocketMode(
|
|
2565
|
+
openAiClientState.openAiResponsesWebSocketMode = resolveResponsesWebSocketMode(
|
|
2508
2566
|
process.env.OPENAI_RESPONSES_WEBSOCKET_MODE,
|
|
2509
2567
|
"auto"
|
|
2510
2568
|
);
|
|
2511
|
-
return openAiResponsesWebSocketMode;
|
|
2569
|
+
return openAiClientState.openAiResponsesWebSocketMode;
|
|
2512
2570
|
}
|
|
2513
2571
|
function wrapFallbackStream(stream) {
|
|
2514
2572
|
return {
|
|
@@ -2561,7 +2619,7 @@ function installResponsesWebSocketTransport(client, apiKey) {
|
|
|
2561
2619
|
responsesApi.stream = (request, options) => {
|
|
2562
2620
|
const mode = resolveOpenAiResponsesWebSocketMode();
|
|
2563
2621
|
const fallbackStreamFactory = () => wrapFallbackStream(originalStream(request, options));
|
|
2564
|
-
if (mode === "off" || openAiResponsesWebSocketDisabled) {
|
|
2622
|
+
if (mode === "off" || openAiClientState.openAiResponsesWebSocketDisabled) {
|
|
2565
2623
|
return fallbackStreamFactory();
|
|
2566
2624
|
}
|
|
2567
2625
|
const signal = options && typeof options === "object" ? options.signal ?? void 0 : void 0;
|
|
@@ -2580,15 +2638,15 @@ function installResponsesWebSocketTransport(client, apiKey) {
|
|
|
2580
2638
|
createFallbackStream: fallbackStreamFactory,
|
|
2581
2639
|
onWebSocketFallback: (error) => {
|
|
2582
2640
|
if (isResponsesWebSocketUnsupportedError(error)) {
|
|
2583
|
-
openAiResponsesWebSocketDisabled = true;
|
|
2641
|
+
openAiClientState.openAiResponsesWebSocketDisabled = true;
|
|
2584
2642
|
}
|
|
2585
2643
|
}
|
|
2586
2644
|
});
|
|
2587
2645
|
};
|
|
2588
2646
|
}
|
|
2589
2647
|
function getOpenAiApiKey() {
|
|
2590
|
-
if (
|
|
2591
|
-
return
|
|
2648
|
+
if (openAiClientState.cachedApiKey !== null) {
|
|
2649
|
+
return openAiClientState.cachedApiKey;
|
|
2592
2650
|
}
|
|
2593
2651
|
loadLocalEnv();
|
|
2594
2652
|
const raw = process.env.OPENAI_API_KEY;
|
|
@@ -2596,33 +2654,35 @@ function getOpenAiApiKey() {
|
|
|
2596
2654
|
if (!value) {
|
|
2597
2655
|
throw new Error("OPENAI_API_KEY must be provided to access OpenAI APIs.");
|
|
2598
2656
|
}
|
|
2599
|
-
|
|
2600
|
-
return
|
|
2657
|
+
openAiClientState.cachedApiKey = value;
|
|
2658
|
+
return openAiClientState.cachedApiKey;
|
|
2601
2659
|
}
|
|
2602
2660
|
function getOpenAiClient() {
|
|
2603
|
-
if (
|
|
2604
|
-
return
|
|
2661
|
+
if (openAiClientState.cachedClient) {
|
|
2662
|
+
return openAiClientState.cachedClient;
|
|
2605
2663
|
}
|
|
2606
2664
|
loadLocalEnv();
|
|
2607
2665
|
const apiKey = getOpenAiApiKey();
|
|
2608
2666
|
const timeoutMs = resolveOpenAiTimeoutMs();
|
|
2609
|
-
|
|
2667
|
+
openAiClientState.cachedClient = new OpenAI2({
|
|
2610
2668
|
apiKey,
|
|
2611
2669
|
fetch: getOpenAiFetch(),
|
|
2612
2670
|
timeout: timeoutMs
|
|
2613
2671
|
});
|
|
2614
|
-
installResponsesWebSocketTransport(
|
|
2615
|
-
return
|
|
2672
|
+
installResponsesWebSocketTransport(openAiClientState.cachedClient, apiKey);
|
|
2673
|
+
return openAiClientState.cachedClient;
|
|
2616
2674
|
}
|
|
2617
2675
|
|
|
2618
2676
|
// src/openai/calls.ts
|
|
2619
2677
|
var DEFAULT_OPENAI_REASONING_EFFORT = "medium";
|
|
2620
2678
|
var DEFAULT_SCHEDULER_KEY3 = "__default__";
|
|
2621
|
-
var
|
|
2679
|
+
var openAiCallState = getRuntimeSingleton(/* @__PURE__ */ Symbol.for("@ljoukov/llm.openAiCallState"), () => ({
|
|
2680
|
+
schedulerByModel: /* @__PURE__ */ new Map()
|
|
2681
|
+
}));
|
|
2622
2682
|
function getSchedulerForModel3(modelId) {
|
|
2623
2683
|
const normalizedModelId = modelId?.trim();
|
|
2624
2684
|
const schedulerKey = normalizedModelId && normalizedModelId.length > 0 ? normalizedModelId : DEFAULT_SCHEDULER_KEY3;
|
|
2625
|
-
const existing =
|
|
2685
|
+
const existing = openAiCallState.schedulerByModel.get(schedulerKey);
|
|
2626
2686
|
if (existing) {
|
|
2627
2687
|
return existing;
|
|
2628
2688
|
}
|
|
@@ -2634,7 +2694,7 @@ function getSchedulerForModel3(modelId) {
|
|
|
2634
2694
|
minIntervalBetweenStartMs: 200,
|
|
2635
2695
|
startJitterMs: 200
|
|
2636
2696
|
});
|
|
2637
|
-
|
|
2697
|
+
openAiCallState.schedulerByModel.set(schedulerKey, created);
|
|
2638
2698
|
return created;
|
|
2639
2699
|
}
|
|
2640
2700
|
async function runOpenAiCall(fn, modelId, runOptions) {
|
|
@@ -2709,6 +2769,33 @@ function ensureTrailingNewline(value) {
|
|
|
2709
2769
|
function hasNonEmptyText(value) {
|
|
2710
2770
|
return typeof value === "string" && value.length > 0;
|
|
2711
2771
|
}
|
|
2772
|
+
function hasLogArtifactValue(value) {
|
|
2773
|
+
if (value === null || value === void 0) {
|
|
2774
|
+
return false;
|
|
2775
|
+
}
|
|
2776
|
+
if (typeof value === "string") {
|
|
2777
|
+
return value.length > 0;
|
|
2778
|
+
}
|
|
2779
|
+
if (Array.isArray(value)) {
|
|
2780
|
+
return value.length > 0;
|
|
2781
|
+
}
|
|
2782
|
+
if (typeof value === "object") {
|
|
2783
|
+
return Object.keys(value).length > 0;
|
|
2784
|
+
}
|
|
2785
|
+
return true;
|
|
2786
|
+
}
|
|
2787
|
+
function serialiseJsonArtifact(value) {
|
|
2788
|
+
if (!hasLogArtifactValue(value)) {
|
|
2789
|
+
return void 0;
|
|
2790
|
+
}
|
|
2791
|
+
try {
|
|
2792
|
+
return `${JSON.stringify(sanitiseLogValue(value), null, 2)}
|
|
2793
|
+
`;
|
|
2794
|
+
} catch {
|
|
2795
|
+
return `${JSON.stringify(String(value), null, 2)}
|
|
2796
|
+
`;
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2712
2799
|
function redactDataUrlPayload(value) {
|
|
2713
2800
|
if (!value.toLowerCase().startsWith("data:")) {
|
|
2714
2801
|
return value;
|
|
@@ -2989,7 +3076,9 @@ var AgentLoggingSessionImpl = class {
|
|
|
2989
3076
|
const responsePath = path3.join(baseDir, "response.txt");
|
|
2990
3077
|
const thoughtsPath = path3.join(baseDir, "thoughts.txt");
|
|
2991
3078
|
const toolCallPath = path3.join(baseDir, "tool_call.txt");
|
|
3079
|
+
const toolCallJsonPath = path3.join(baseDir, "tool_call.json");
|
|
2992
3080
|
const toolCallResponsePath = path3.join(baseDir, "tool_call_response.txt");
|
|
3081
|
+
const toolCallResponseJsonPath = path3.join(baseDir, "tool_call_response.json");
|
|
2993
3082
|
const errorPath = path3.join(baseDir, "error.txt");
|
|
2994
3083
|
const responseMetadataPath = path3.join(baseDir, "response.metadata.json");
|
|
2995
3084
|
let chain = this.ensureReady.then(async () => {
|
|
@@ -3020,6 +3109,10 @@ var AgentLoggingSessionImpl = class {
|
|
|
3020
3109
|
"utf8"
|
|
3021
3110
|
);
|
|
3022
3111
|
}
|
|
3112
|
+
const toolCallResponseJson = serialiseJsonArtifact(input.toolCallResponsePayload);
|
|
3113
|
+
if (toolCallResponseJson) {
|
|
3114
|
+
await writeFile(toolCallResponseJsonPath, toolCallResponseJson, "utf8");
|
|
3115
|
+
}
|
|
3023
3116
|
}).catch(() => void 0);
|
|
3024
3117
|
this.track(chain);
|
|
3025
3118
|
let closed = false;
|
|
@@ -3058,6 +3151,10 @@ var AgentLoggingSessionImpl = class {
|
|
|
3058
3151
|
if (hasNonEmptyText(options?.toolCallText)) {
|
|
3059
3152
|
await writeFile(toolCallPath, ensureTrailingNewline(options.toolCallText), "utf8");
|
|
3060
3153
|
}
|
|
3154
|
+
const toolCallJson = serialiseJsonArtifact(options?.toolCallPayload);
|
|
3155
|
+
if (toolCallJson) {
|
|
3156
|
+
await writeFile(toolCallJsonPath, toolCallJson, "utf8");
|
|
3157
|
+
}
|
|
3061
3158
|
await this.writeAttachments(baseDir, options?.attachments);
|
|
3062
3159
|
const payload = {
|
|
3063
3160
|
capturedAt: toIsoNow(),
|
|
@@ -3087,6 +3184,10 @@ var AgentLoggingSessionImpl = class {
|
|
|
3087
3184
|
if (hasNonEmptyText(options?.toolCallText)) {
|
|
3088
3185
|
await writeFile(toolCallPath, ensureTrailingNewline(options.toolCallText), "utf8");
|
|
3089
3186
|
}
|
|
3187
|
+
const toolCallJson = serialiseJsonArtifact(options?.toolCallPayload);
|
|
3188
|
+
if (toolCallJson) {
|
|
3189
|
+
await writeFile(toolCallJsonPath, toolCallJson, "utf8");
|
|
3190
|
+
}
|
|
3090
3191
|
await this.writeAttachments(baseDir, options?.attachments);
|
|
3091
3192
|
await writeFile(errorPath, ensureTrailingNewline(toErrorMessage(error)), "utf8");
|
|
3092
3193
|
const payload = {
|
|
@@ -3120,7 +3221,10 @@ var AgentLoggingSessionImpl = class {
|
|
|
3120
3221
|
}
|
|
3121
3222
|
}
|
|
3122
3223
|
};
|
|
3123
|
-
var loggingSessionStorage =
|
|
3224
|
+
var loggingSessionStorage = getRuntimeSingleton(
|
|
3225
|
+
/* @__PURE__ */ Symbol.for("@ljoukov/llm.agentLogging.sessionStorage"),
|
|
3226
|
+
() => new AsyncLocalStorage()
|
|
3227
|
+
);
|
|
3124
3228
|
function createAgentLoggingSession(config) {
|
|
3125
3229
|
return new AgentLoggingSessionImpl(config);
|
|
3126
3230
|
}
|
|
@@ -3135,7 +3239,10 @@ function getCurrentAgentLoggingSession() {
|
|
|
3135
3239
|
}
|
|
3136
3240
|
|
|
3137
3241
|
// src/llm.ts
|
|
3138
|
-
var toolCallContextStorage =
|
|
3242
|
+
var toolCallContextStorage = getRuntimeSingleton(
|
|
3243
|
+
/* @__PURE__ */ Symbol.for("@ljoukov/llm.toolCallContextStorage"),
|
|
3244
|
+
() => new AsyncLocalStorage2()
|
|
3245
|
+
);
|
|
3139
3246
|
function getCurrentToolCallContext() {
|
|
3140
3247
|
return toolCallContextStorage.getStore() ?? null;
|
|
3141
3248
|
}
|
|
@@ -5058,6 +5165,10 @@ function collectLoggedAttachmentsFromGeminiParts(parts, prefix) {
|
|
|
5058
5165
|
return collectLoggedAttachmentsFromLlmParts(convertGooglePartsToLlmParts(parts), prefix);
|
|
5059
5166
|
}
|
|
5060
5167
|
function extractToolCallResponseTextFromOpenAiInput(input) {
|
|
5168
|
+
const responses = extractToolCallResponsePayloadFromOpenAiInput(input);
|
|
5169
|
+
return serialiseLogArtifactText(responses);
|
|
5170
|
+
}
|
|
5171
|
+
function extractToolCallResponsePayloadFromOpenAiInput(input) {
|
|
5061
5172
|
if (!Array.isArray(input)) {
|
|
5062
5173
|
return void 0;
|
|
5063
5174
|
}
|
|
@@ -5074,9 +5185,13 @@ function extractToolCallResponseTextFromOpenAiInput(input) {
|
|
|
5074
5185
|
}
|
|
5075
5186
|
];
|
|
5076
5187
|
});
|
|
5077
|
-
return
|
|
5188
|
+
return responses.length > 0 ? responses : void 0;
|
|
5078
5189
|
}
|
|
5079
5190
|
function extractToolCallResponseTextFromFireworksMessages(messages) {
|
|
5191
|
+
const responses = extractToolCallResponsePayloadFromFireworksMessages(messages);
|
|
5192
|
+
return serialiseLogArtifactText(responses);
|
|
5193
|
+
}
|
|
5194
|
+
function extractToolCallResponsePayloadFromFireworksMessages(messages) {
|
|
5080
5195
|
if (!Array.isArray(messages)) {
|
|
5081
5196
|
return void 0;
|
|
5082
5197
|
}
|
|
@@ -5091,9 +5206,13 @@ function extractToolCallResponseTextFromFireworksMessages(messages) {
|
|
|
5091
5206
|
}
|
|
5092
5207
|
];
|
|
5093
5208
|
});
|
|
5094
|
-
return
|
|
5209
|
+
return responses.length > 0 ? responses : void 0;
|
|
5095
5210
|
}
|
|
5096
5211
|
function extractToolCallResponseTextFromGeminiContents(contents) {
|
|
5212
|
+
const responses = extractToolCallResponsePayloadFromGeminiContents(contents);
|
|
5213
|
+
return serialiseLogArtifactText(responses);
|
|
5214
|
+
}
|
|
5215
|
+
function extractToolCallResponsePayloadFromGeminiContents(contents) {
|
|
5097
5216
|
if (!Array.isArray(contents)) {
|
|
5098
5217
|
return void 0;
|
|
5099
5218
|
}
|
|
@@ -5116,40 +5235,36 @@ function extractToolCallResponseTextFromGeminiContents(contents) {
|
|
|
5116
5235
|
}
|
|
5117
5236
|
}
|
|
5118
5237
|
}
|
|
5119
|
-
return
|
|
5238
|
+
return responses.length > 0 ? responses : void 0;
|
|
5120
5239
|
}
|
|
5121
|
-
function
|
|
5122
|
-
return
|
|
5123
|
-
|
|
5124
|
-
if (call.kind === "custom") {
|
|
5125
|
-
return {
|
|
5126
|
-
kind: call.kind,
|
|
5127
|
-
name: call.name,
|
|
5128
|
-
callId: call.callId,
|
|
5129
|
-
itemId: call.itemId,
|
|
5130
|
-
input: call.input
|
|
5131
|
-
};
|
|
5132
|
-
}
|
|
5133
|
-
const { value, error } = parseOpenAiToolArguments(call.arguments);
|
|
5240
|
+
function toLoggedOpenAiStyleToolCalls(calls) {
|
|
5241
|
+
return calls.map((call) => {
|
|
5242
|
+
if (call.kind === "custom") {
|
|
5134
5243
|
return {
|
|
5135
5244
|
kind: call.kind,
|
|
5136
5245
|
name: call.name,
|
|
5137
5246
|
callId: call.callId,
|
|
5138
5247
|
itemId: call.itemId,
|
|
5139
|
-
|
|
5140
|
-
...error ? { parseError: error, rawArguments: call.arguments } : {}
|
|
5248
|
+
input: call.input
|
|
5141
5249
|
};
|
|
5142
|
-
}
|
|
5143
|
-
|
|
5250
|
+
}
|
|
5251
|
+
const { value, error } = parseOpenAiToolArguments(call.arguments);
|
|
5252
|
+
return {
|
|
5253
|
+
kind: call.kind,
|
|
5254
|
+
name: call.name,
|
|
5255
|
+
callId: call.callId,
|
|
5256
|
+
itemId: call.itemId,
|
|
5257
|
+
arguments: value,
|
|
5258
|
+
...error ? { parseError: error, rawArguments: call.arguments } : {}
|
|
5259
|
+
};
|
|
5260
|
+
});
|
|
5144
5261
|
}
|
|
5145
|
-
function
|
|
5146
|
-
return
|
|
5147
|
-
|
|
5148
|
-
|
|
5149
|
-
|
|
5150
|
-
|
|
5151
|
-
}))
|
|
5152
|
-
);
|
|
5262
|
+
function toLoggedGeminiToolCalls(calls) {
|
|
5263
|
+
return calls.map((call) => ({
|
|
5264
|
+
name: call.name ?? "unknown",
|
|
5265
|
+
callId: typeof call.id === "string" ? call.id : void 0,
|
|
5266
|
+
arguments: sanitiseLogValue(call.args ?? {})
|
|
5267
|
+
}));
|
|
5153
5268
|
}
|
|
5154
5269
|
function startLlmCallLoggerFromContents(options) {
|
|
5155
5270
|
const session = getCurrentAgentLoggingSession();
|
|
@@ -5229,6 +5344,13 @@ function startLlmCallLoggerFromPayload(options) {
|
|
|
5229
5344
|
) : extractToolCallResponseTextFromGeminiContents(
|
|
5230
5345
|
options.requestPayload.contents
|
|
5231
5346
|
);
|
|
5347
|
+
const toolCallResponsePayload = options.provider === "openai" || options.provider === "chatgpt" ? extractToolCallResponsePayloadFromOpenAiInput(
|
|
5348
|
+
options.requestPayload.input
|
|
5349
|
+
) : options.provider === "fireworks" ? extractToolCallResponsePayloadFromFireworksMessages(
|
|
5350
|
+
options.requestPayload.messages
|
|
5351
|
+
) : extractToolCallResponsePayloadFromGeminiContents(
|
|
5352
|
+
options.requestPayload.contents
|
|
5353
|
+
);
|
|
5232
5354
|
return session.startLlmCall({
|
|
5233
5355
|
provider: options.provider,
|
|
5234
5356
|
modelId: options.modelId,
|
|
@@ -5238,7 +5360,8 @@ function startLlmCallLoggerFromPayload(options) {
|
|
|
5238
5360
|
...getCurrentToolCallContext() ? { toolContext: getCurrentToolCallContext() } : {}
|
|
5239
5361
|
},
|
|
5240
5362
|
attachments,
|
|
5241
|
-
toolCallResponseText
|
|
5363
|
+
toolCallResponseText,
|
|
5364
|
+
toolCallResponsePayload
|
|
5242
5365
|
});
|
|
5243
5366
|
}
|
|
5244
5367
|
async function runTextCall(params) {
|
|
@@ -5803,7 +5926,10 @@ var DEFAULT_TOOL_LOOP_MAX_STEPS = 8;
|
|
|
5803
5926
|
function resolveToolLoopContents(input) {
|
|
5804
5927
|
return resolveTextContents(input);
|
|
5805
5928
|
}
|
|
5806
|
-
var toolLoopSteeringInternals =
|
|
5929
|
+
var toolLoopSteeringInternals = getRuntimeSingleton(
|
|
5930
|
+
/* @__PURE__ */ Symbol.for("@ljoukov/llm.toolLoopSteeringInternals"),
|
|
5931
|
+
() => /* @__PURE__ */ new WeakMap()
|
|
5932
|
+
);
|
|
5807
5933
|
function createToolLoopSteeringChannel() {
|
|
5808
5934
|
const pending = [];
|
|
5809
5935
|
let closed = false;
|
|
@@ -6066,6 +6192,7 @@ async function runToolLoop(request) {
|
|
|
6066
6192
|
let responseText = "";
|
|
6067
6193
|
let reasoningSummary = "";
|
|
6068
6194
|
let stepToolCallText;
|
|
6195
|
+
let stepToolCallPayload;
|
|
6069
6196
|
const stepRequestPayload = {
|
|
6070
6197
|
model: providerInfo.model,
|
|
6071
6198
|
input,
|
|
@@ -6175,7 +6302,7 @@ async function runToolLoop(request) {
|
|
|
6175
6302
|
emitEvent({ type: "usage", usage: usageTokens, costUsd: stepCostUsd, modelVersion });
|
|
6176
6303
|
}
|
|
6177
6304
|
const responseToolCalls = extractOpenAiToolCalls(finalResponse.output);
|
|
6178
|
-
|
|
6305
|
+
stepToolCallPayload = toLoggedOpenAiStyleToolCalls(
|
|
6179
6306
|
responseToolCalls.map(
|
|
6180
6307
|
(call) => call.kind === "custom" ? {
|
|
6181
6308
|
kind: call.kind,
|
|
@@ -6192,6 +6319,7 @@ async function runToolLoop(request) {
|
|
|
6192
6319
|
}
|
|
6193
6320
|
)
|
|
6194
6321
|
);
|
|
6322
|
+
stepToolCallText = serialiseLogArtifactText(stepToolCallPayload);
|
|
6195
6323
|
const stepToolCalls = [];
|
|
6196
6324
|
if (responseToolCalls.length === 0) {
|
|
6197
6325
|
const steeringInput2 = steeringInternal?.drainPendingContents() ?? [];
|
|
@@ -6357,6 +6485,7 @@ async function runToolLoop(request) {
|
|
|
6357
6485
|
stepCallLogger?.complete({
|
|
6358
6486
|
responseText,
|
|
6359
6487
|
toolCallText: stepToolCallText,
|
|
6488
|
+
toolCallPayload: stepToolCallPayload,
|
|
6360
6489
|
metadata: {
|
|
6361
6490
|
provider: "openai",
|
|
6362
6491
|
model: request.model,
|
|
@@ -6377,6 +6506,7 @@ async function runToolLoop(request) {
|
|
|
6377
6506
|
stepCallLogger?.fail(error, {
|
|
6378
6507
|
responseText,
|
|
6379
6508
|
toolCallText: stepToolCallText,
|
|
6509
|
+
toolCallPayload: stepToolCallPayload,
|
|
6380
6510
|
metadata: {
|
|
6381
6511
|
provider: "openai",
|
|
6382
6512
|
model: request.model,
|
|
@@ -6411,6 +6541,7 @@ async function runToolLoop(request) {
|
|
|
6411
6541
|
let responseText = "";
|
|
6412
6542
|
let reasoningSummaryText = "";
|
|
6413
6543
|
let stepToolCallText;
|
|
6544
|
+
let stepToolCallPayload;
|
|
6414
6545
|
const markFirstModelEvent = () => {
|
|
6415
6546
|
if (firstModelEventAtMs === void 0) {
|
|
6416
6547
|
firstModelEventAtMs = Date.now();
|
|
@@ -6479,7 +6610,7 @@ async function runToolLoop(request) {
|
|
|
6479
6610
|
stepCallLogger?.appendResponseDelta(responseText);
|
|
6480
6611
|
}
|
|
6481
6612
|
const responseToolCalls = response.toolCalls ?? [];
|
|
6482
|
-
|
|
6613
|
+
stepToolCallPayload = toLoggedOpenAiStyleToolCalls(
|
|
6483
6614
|
responseToolCalls.map(
|
|
6484
6615
|
(call) => call.kind === "custom" ? {
|
|
6485
6616
|
kind: call.kind,
|
|
@@ -6496,6 +6627,7 @@ async function runToolLoop(request) {
|
|
|
6496
6627
|
}
|
|
6497
6628
|
)
|
|
6498
6629
|
);
|
|
6630
|
+
stepToolCallText = serialiseLogArtifactText(stepToolCallPayload);
|
|
6499
6631
|
if (responseToolCalls.length === 0) {
|
|
6500
6632
|
const steeringInput2 = steeringInternal?.drainPendingContents() ?? [];
|
|
6501
6633
|
const steeringItems2 = steeringInput2.length > 0 ? toChatGptInput(steeringInput2).input : [];
|
|
@@ -6668,6 +6800,7 @@ async function runToolLoop(request) {
|
|
|
6668
6800
|
stepCallLogger?.complete({
|
|
6669
6801
|
responseText,
|
|
6670
6802
|
toolCallText: stepToolCallText,
|
|
6803
|
+
toolCallPayload: stepToolCallPayload,
|
|
6671
6804
|
metadata: {
|
|
6672
6805
|
provider: "chatgpt",
|
|
6673
6806
|
model: request.model,
|
|
@@ -6686,6 +6819,7 @@ async function runToolLoop(request) {
|
|
|
6686
6819
|
stepCallLogger?.fail(error, {
|
|
6687
6820
|
responseText,
|
|
6688
6821
|
toolCallText: stepToolCallText,
|
|
6822
|
+
toolCallPayload: stepToolCallPayload,
|
|
6689
6823
|
metadata: {
|
|
6690
6824
|
provider: "chatgpt",
|
|
6691
6825
|
model: request.model,
|
|
@@ -6716,6 +6850,7 @@ async function runToolLoop(request) {
|
|
|
6716
6850
|
let responseText = "";
|
|
6717
6851
|
let blocked = false;
|
|
6718
6852
|
let stepToolCallText;
|
|
6853
|
+
let stepToolCallPayload;
|
|
6719
6854
|
const stepRequestPayload = {
|
|
6720
6855
|
model: providerInfo.model,
|
|
6721
6856
|
messages,
|
|
@@ -6780,7 +6915,7 @@ async function runToolLoop(request) {
|
|
|
6780
6915
|
});
|
|
6781
6916
|
}
|
|
6782
6917
|
const responseToolCalls = extractFireworksToolCalls(message);
|
|
6783
|
-
|
|
6918
|
+
stepToolCallPayload = toLoggedOpenAiStyleToolCalls(
|
|
6784
6919
|
responseToolCalls.map((call) => ({
|
|
6785
6920
|
kind: "function",
|
|
6786
6921
|
name: call.name,
|
|
@@ -6788,6 +6923,7 @@ async function runToolLoop(request) {
|
|
|
6788
6923
|
callId: call.id
|
|
6789
6924
|
}))
|
|
6790
6925
|
);
|
|
6926
|
+
stepToolCallText = serialiseLogArtifactText(stepToolCallPayload);
|
|
6791
6927
|
if (responseToolCalls.length === 0) {
|
|
6792
6928
|
const steeringInput2 = steeringInternal?.drainPendingContents() ?? [];
|
|
6793
6929
|
const steeringMessages = steeringInput2.length > 0 ? toFireworksMessages(steeringInput2) : [];
|
|
@@ -6940,6 +7076,7 @@ async function runToolLoop(request) {
|
|
|
6940
7076
|
stepCallLogger?.complete({
|
|
6941
7077
|
responseText,
|
|
6942
7078
|
toolCallText: stepToolCallText,
|
|
7079
|
+
toolCallPayload: stepToolCallPayload,
|
|
6943
7080
|
metadata: {
|
|
6944
7081
|
provider: "fireworks",
|
|
6945
7082
|
model: request.model,
|
|
@@ -6968,6 +7105,7 @@ async function runToolLoop(request) {
|
|
|
6968
7105
|
stepCallLogger?.fail(error, {
|
|
6969
7106
|
responseText,
|
|
6970
7107
|
toolCallText: stepToolCallText,
|
|
7108
|
+
toolCallPayload: stepToolCallPayload,
|
|
6971
7109
|
metadata: {
|
|
6972
7110
|
provider: "fireworks",
|
|
6973
7111
|
model: request.model,
|
|
@@ -6996,6 +7134,7 @@ async function runToolLoop(request) {
|
|
|
6996
7134
|
let responseText = "";
|
|
6997
7135
|
let thoughtsText = "";
|
|
6998
7136
|
let stepToolCallText;
|
|
7137
|
+
let stepToolCallPayload;
|
|
6999
7138
|
const markFirstModelEvent = () => {
|
|
7000
7139
|
if (firstModelEventAtMs === void 0) {
|
|
7001
7140
|
firstModelEventAtMs = Date.now();
|
|
@@ -7126,7 +7265,8 @@ async function runToolLoop(request) {
|
|
|
7126
7265
|
responseImages: 0
|
|
7127
7266
|
});
|
|
7128
7267
|
totalCostUsd += stepCostUsd;
|
|
7129
|
-
|
|
7268
|
+
stepToolCallPayload = toLoggedGeminiToolCalls(response.functionCalls);
|
|
7269
|
+
stepToolCallText = serialiseLogArtifactText(stepToolCallPayload);
|
|
7130
7270
|
if (response.functionCalls.length === 0) {
|
|
7131
7271
|
const steeringInput2 = steeringInternal?.drainPendingContents() ?? [];
|
|
7132
7272
|
finalText = responseText;
|
|
@@ -7295,6 +7435,7 @@ async function runToolLoop(request) {
|
|
|
7295
7435
|
responseText,
|
|
7296
7436
|
attachments: responseOutputAttachments,
|
|
7297
7437
|
toolCallText: stepToolCallText,
|
|
7438
|
+
toolCallPayload: stepToolCallPayload,
|
|
7298
7439
|
metadata: {
|
|
7299
7440
|
provider: "gemini",
|
|
7300
7441
|
model: request.model,
|
|
@@ -7317,6 +7458,7 @@ async function runToolLoop(request) {
|
|
|
7317
7458
|
stepCallLogger?.fail(error, {
|
|
7318
7459
|
responseText,
|
|
7319
7460
|
toolCallText: stepToolCallText,
|
|
7461
|
+
toolCallPayload: stepToolCallPayload,
|
|
7320
7462
|
metadata: {
|
|
7321
7463
|
provider: "gemini",
|
|
7322
7464
|
model: request.model,
|