@genesislcap/ai-assistant 15.90.1-GENC-1829.2 → 15.90.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/chat-driver.cjs +155 -900
- package/dist/chat-driver.cjs.map +4 -4
- package/dist/chat-driver.mjs +155 -892
- package/dist/chat-driver.mjs.map +4 -4
- package/dist/custom-elements.json +0 -80
- package/dist/dts/chat-driver-node.d.ts +2 -2
- package/dist/dts/chat-driver-node.d.ts.map +1 -1
- package/dist/esm/chat-driver-node.js +3 -6
- package/package.json +17 -17
package/dist/chat-driver.cjs
CHANGED
|
@@ -43,17 +43,11 @@ __export(chat_driver_node_exports, {
|
|
|
43
43
|
MALFORMED_CALL_NUDGE: () => MALFORMED_CALL_NUDGE,
|
|
44
44
|
MutableAIProviderRegistry: () => MutableAIProviderRegistry,
|
|
45
45
|
NOOP_ACTIVITY_BUS: () => NOOP_ACTIVITY_BUS,
|
|
46
|
-
OPENAI_CACHE_WRITE_12H_MULTIPLIER: () => OPENAI_CACHE_WRITE_12H_MULTIPLIER,
|
|
47
|
-
OPENAI_CACHE_WRITE_30M_MULTIPLIER: () => OPENAI_CACHE_WRITE_30M_MULTIPLIER,
|
|
48
|
-
OPENAI_LONG_CONTEXT_THRESHOLD: () => OPENAI_LONG_CONTEXT_THRESHOLD,
|
|
49
|
-
OpenAIProvider: () => OpenAIProvider,
|
|
50
|
-
OpenAITransport: () => OpenAITransport,
|
|
51
46
|
OrchestratingDriver: () => OrchestratingDriver,
|
|
52
47
|
PROVIDER_REFUSED_CODE: () => PROVIDER_REFUSED_CODE,
|
|
53
48
|
ProviderRefusedError: () => ProviderRefusedError,
|
|
54
49
|
SUPPORTED_ANTHROPIC_MODEL_IDS: () => SUPPORTED_ANTHROPIC_MODEL_IDS,
|
|
55
50
|
SUPPORTED_GEMINI_MODEL_IDS: () => SUPPORTED_GEMINI_MODEL_IDS,
|
|
56
|
-
SUPPORTED_OPENAI_MODEL_IDS: () => SUPPORTED_OPENAI_MODEL_IDS,
|
|
57
51
|
UNROUTABLE_INSTRUCTION: () => UNROUTABLE_INSTRUCTION,
|
|
58
52
|
UNROUTABLE_PLACEHOLDER: () => UNROUTABLE_PLACEHOLDER,
|
|
59
53
|
addUsage: () => addUsage,
|
|
@@ -82,8 +76,6 @@ __export(chat_driver_node_exports, {
|
|
|
82
76
|
isTerminalAIServiceRefusal: () => isTerminalAIServiceRefusal,
|
|
83
77
|
lifetimeDebugLog: () => lifetimeDebugLog,
|
|
84
78
|
messageUsage: () => messageUsage,
|
|
85
|
-
openaiRatesFor: () => openaiRatesFor,
|
|
86
|
-
openaiTokenCost: () => openaiTokenCost,
|
|
87
79
|
resolveAITiers: () => resolveAITiers,
|
|
88
80
|
restoreMachine: () => restoreMachine,
|
|
89
81
|
strictFallbackAgent: () => strictFallbackAgent,
|
|
@@ -1258,6 +1250,155 @@ var GeminiProvider = class {
|
|
|
1258
1250
|
}
|
|
1259
1251
|
};
|
|
1260
1252
|
|
|
1253
|
+
// ../../foundation-ai/dist/esm/transports/budget-exhausted-error.js
|
|
1254
|
+
var BUDGET_EXCEEDED_CODE = "BUDGET_EXCEEDED";
|
|
1255
|
+
var VENDOR_LABELS = Object.freeze({
|
|
1256
|
+
anthropic: "Anthropic",
|
|
1257
|
+
gemini: "Gemini",
|
|
1258
|
+
openai: "OpenAI",
|
|
1259
|
+
chrome: "Chrome"
|
|
1260
|
+
});
|
|
1261
|
+
var BUDGETED_VENDORS = Object.freeze([
|
|
1262
|
+
"anthropic",
|
|
1263
|
+
"gemini"
|
|
1264
|
+
]);
|
|
1265
|
+
var VENDOR_TYPE_BY_LABEL = new Map(Object.entries(VENDOR_LABELS).map(([type, label]) => [
|
|
1266
|
+
label.toLowerCase(),
|
|
1267
|
+
type
|
|
1268
|
+
]));
|
|
1269
|
+
function vendorTypeOfLabel(label) {
|
|
1270
|
+
if (typeof label !== "string")
|
|
1271
|
+
return void 0;
|
|
1272
|
+
return VENDOR_TYPE_BY_LABEL.get(label.trim().toLowerCase());
|
|
1273
|
+
}
|
|
1274
|
+
var BUDGET_EXCEEDED_STATUS = 402;
|
|
1275
|
+
var DEFAULT_BUDGET_EXHAUSTED_MESSAGE = "You've reached your AI usage limit. Contact your administrator to raise it.";
|
|
1276
|
+
var BudgetExhaustedError = class extends Error {
|
|
1277
|
+
constructor(vendorLabel, budgetUsd, spentUsd, detail, extra) {
|
|
1278
|
+
super(`${vendorLabel} request refused: the AI usage budget is exhausted` + (budgetUsd != null ? ` (spent ${spentUsd != null ? `$${spentUsd}` : "an unknown amount"} of a $${budgetUsd} budget)` : "") + (detail ? `: ${detail}` : "") + ". Retrying will not clear this \u2014 the budget must be raised.");
|
|
1279
|
+
this.vendorLabel = vendorLabel;
|
|
1280
|
+
this.budgetUsd = budgetUsd;
|
|
1281
|
+
this.spentUsd = spentUsd;
|
|
1282
|
+
this.detail = detail;
|
|
1283
|
+
this.name = "BudgetExhaustedError";
|
|
1284
|
+
this.otherVendorAvailable = extra === null || extra === void 0 ? void 0 : extra.otherVendorAvailable;
|
|
1285
|
+
this.serverVendor = extra === null || extra === void 0 ? void 0 : extra.serverVendor;
|
|
1286
|
+
}
|
|
1287
|
+
};
|
|
1288
|
+
var isBudgetRejection = (status, code) => status === BUDGET_EXCEEDED_STATUS || code === BUDGET_EXCEEDED_CODE;
|
|
1289
|
+
var parseJsonOrUndefined = (text) => {
|
|
1290
|
+
try {
|
|
1291
|
+
return JSON.parse(text);
|
|
1292
|
+
} catch (_a) {
|
|
1293
|
+
return void 0;
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
var codeOf = (payload) => {
|
|
1297
|
+
const code = payload === null || payload === void 0 ? void 0 : payload.code;
|
|
1298
|
+
return typeof code === "string" ? code : void 0;
|
|
1299
|
+
};
|
|
1300
|
+
var numberOrUndefined = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
1301
|
+
var booleanOrUndefined = (value) => typeof value === "boolean" ? value : void 0;
|
|
1302
|
+
var stringOrUndefined = (value) => typeof value === "string" && value.trim() !== "" ? value : void 0;
|
|
1303
|
+
function budgetExhaustedFrom(vendorLabel, payload, fallbackDetail) {
|
|
1304
|
+
var _a, _b, _c, _d;
|
|
1305
|
+
const body = typeof payload === "object" && payload !== null ? payload : {};
|
|
1306
|
+
const details = typeof body.details === "object" && body.details !== null ? body.details : {};
|
|
1307
|
+
const detail = typeof body.error === "string" ? body.error : fallbackDetail;
|
|
1308
|
+
return new BudgetExhaustedError(vendorLabel, (_a = numberOrUndefined(body.budgetUsd)) !== null && _a !== void 0 ? _a : numberOrUndefined(details.budgetUsd), (_b = numberOrUndefined(body.spentUsd)) !== null && _b !== void 0 ? _b : numberOrUndefined(details.spentUsd), detail, {
|
|
1309
|
+
otherVendorAvailable: (_c = booleanOrUndefined(body.otherVendorAvailable)) !== null && _c !== void 0 ? _c : booleanOrUndefined(details.otherVendorAvailable),
|
|
1310
|
+
serverVendor: (_d = stringOrUndefined(body.vendor)) !== null && _d !== void 0 ? _d : stringOrUndefined(details.vendor)
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
// ../../foundation-ai/dist/esm/transports/provider-refused.js
|
|
1315
|
+
var PROVIDER_REFUSED_CODE = "PROVIDER_REFUSED";
|
|
1316
|
+
var DEFAULT_PROVIDER_REFUSED_MESSAGE = "AI requests are currently being refused by the provider. This isn't related to your account or your usage and needs no action from you \u2014 please contact your support team so it can be restored.";
|
|
1317
|
+
var ProviderRefusedError = class extends Error {
|
|
1318
|
+
constructor(vendorLabel, kind, upstreamStatus, upstreamType, detail) {
|
|
1319
|
+
super(`${vendorLabel} refused the request (${kind})` + (upstreamStatus != null ? ` \u2014 HTTP ${upstreamStatus}` : "") + (upstreamType ? ` ${upstreamType}` : "") + (detail ? `: ${detail}` : "") + ". Retrying will not clear this \u2014 the provider account must be fixed.");
|
|
1320
|
+
this.vendorLabel = vendorLabel;
|
|
1321
|
+
this.kind = kind;
|
|
1322
|
+
this.upstreamStatus = upstreamStatus;
|
|
1323
|
+
this.upstreamType = upstreamType;
|
|
1324
|
+
this.detail = detail;
|
|
1325
|
+
this.name = "ProviderRefusedError";
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
var ANTHROPIC_SPEND_PHRASES = Object.freeze([
|
|
1329
|
+
/credit balance/i,
|
|
1330
|
+
/usage limit/i,
|
|
1331
|
+
/spend limit/i,
|
|
1332
|
+
/insufficient (credit|funds|balance)/i
|
|
1333
|
+
]);
|
|
1334
|
+
var ANTHROPIC_AUTH_TYPES = Object.freeze([
|
|
1335
|
+
"authentication_error",
|
|
1336
|
+
"permission_error"
|
|
1337
|
+
]);
|
|
1338
|
+
function anthropicErrorBody(payload) {
|
|
1339
|
+
var _a;
|
|
1340
|
+
const outer = typeof payload === "object" && payload !== null ? payload : {};
|
|
1341
|
+
for (const candidate of [
|
|
1342
|
+
outer.error,
|
|
1343
|
+
(_a = outer.details) === null || _a === void 0 ? void 0 : _a.error
|
|
1344
|
+
]) {
|
|
1345
|
+
if (typeof candidate === "object" && candidate !== null) {
|
|
1346
|
+
const { type, message } = candidate;
|
|
1347
|
+
return {
|
|
1348
|
+
type: typeof type === "string" ? type : void 0,
|
|
1349
|
+
message: typeof message === "string" ? message : void 0
|
|
1350
|
+
};
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
return void 0;
|
|
1354
|
+
}
|
|
1355
|
+
function statedKind(payload) {
|
|
1356
|
+
const outer = typeof payload === "object" && payload !== null ? payload : {};
|
|
1357
|
+
const nested = typeof outer.details === "object" && outer.details !== null ? outer.details : {};
|
|
1358
|
+
for (const candidate of [outer.kind, nested.kind]) {
|
|
1359
|
+
if (candidate === "auth" || candidate === "spend")
|
|
1360
|
+
return candidate;
|
|
1361
|
+
}
|
|
1362
|
+
return void 0;
|
|
1363
|
+
}
|
|
1364
|
+
function kindFromEnvelopeType(payload) {
|
|
1365
|
+
const body = anthropicErrorBody(payload);
|
|
1366
|
+
if (!(body === null || body === void 0 ? void 0 : body.type))
|
|
1367
|
+
return void 0;
|
|
1368
|
+
if (ANTHROPIC_AUTH_TYPES.includes(body.type))
|
|
1369
|
+
return "auth";
|
|
1370
|
+
if (body.type === "billing_error")
|
|
1371
|
+
return "spend";
|
|
1372
|
+
return void 0;
|
|
1373
|
+
}
|
|
1374
|
+
function providerRefusalOf(vendor, code, payload) {
|
|
1375
|
+
var _a, _b;
|
|
1376
|
+
if (code === PROVIDER_REFUSED_CODE) {
|
|
1377
|
+
return (_b = (_a = statedKind(payload)) !== null && _a !== void 0 ? _a : kindFromEnvelopeType(payload)) !== null && _b !== void 0 ? _b : "spend";
|
|
1378
|
+
}
|
|
1379
|
+
if (vendor !== "anthropic")
|
|
1380
|
+
return void 0;
|
|
1381
|
+
const structural = kindFromEnvelopeType(payload);
|
|
1382
|
+
if (structural)
|
|
1383
|
+
return structural;
|
|
1384
|
+
const body = anthropicErrorBody(payload);
|
|
1385
|
+
if ((body === null || body === void 0 ? void 0 : body.type) === "invalid_request_error" && body.message) {
|
|
1386
|
+
const message = body.message;
|
|
1387
|
+
if (ANTHROPIC_SPEND_PHRASES.some((phrase) => phrase.test(message)))
|
|
1388
|
+
return "spend";
|
|
1389
|
+
}
|
|
1390
|
+
return void 0;
|
|
1391
|
+
}
|
|
1392
|
+
function providerRefusedFrom(vendorLabel, kind, upstreamStatus, payload, fallbackDetail) {
|
|
1393
|
+
var _a;
|
|
1394
|
+
const body = anthropicErrorBody(payload);
|
|
1395
|
+
return new ProviderRefusedError(vendorLabel, kind, upstreamStatus, body === null || body === void 0 ? void 0 : body.type, (_a = body === null || body === void 0 ? void 0 : body.message) !== null && _a !== void 0 ? _a : fallbackDetail);
|
|
1396
|
+
}
|
|
1397
|
+
function vendorOfTransportLabel(vendorLabel) {
|
|
1398
|
+
const entry = Object.entries(VENDOR_LABELS).find(([, label]) => label === vendorLabel);
|
|
1399
|
+
return entry === null || entry === void 0 ? void 0 : entry[0];
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1261
1402
|
// ../../foundation-ai/dist/esm/types/config.types.js
|
|
1262
1403
|
var SUPPORTED_GEMINI_MODEL_IDS = [
|
|
1263
1404
|
"gemini-2.5-pro",
|
|
@@ -1277,20 +1418,11 @@ var SUPPORTED_ANTHROPIC_MODEL_IDS = [
|
|
|
1277
1418
|
"claude-sonnet-4-6",
|
|
1278
1419
|
"claude-haiku-4-5-20251001"
|
|
1279
1420
|
];
|
|
1280
|
-
var SUPPORTED_OPENAI_MODEL_IDS = [
|
|
1281
|
-
"gpt-6-astra",
|
|
1282
|
-
"gpt-6.1-sol",
|
|
1283
|
-
"gpt-6.1-terra",
|
|
1284
|
-
"gpt-6-sol",
|
|
1285
|
-
"gpt-6-luna"
|
|
1286
|
-
];
|
|
1287
1421
|
function vendorOfModel(modelId) {
|
|
1288
1422
|
if (SUPPORTED_ANTHROPIC_MODEL_IDS.includes(modelId))
|
|
1289
1423
|
return "anthropic";
|
|
1290
1424
|
if (SUPPORTED_GEMINI_MODEL_IDS.includes(modelId))
|
|
1291
1425
|
return "gemini";
|
|
1292
|
-
if (SUPPORTED_OPENAI_MODEL_IDS.includes(modelId))
|
|
1293
|
-
return "openai";
|
|
1294
1426
|
return void 0;
|
|
1295
1427
|
}
|
|
1296
1428
|
|
|
@@ -1439,52 +1571,6 @@ function geminiTokenCost(model, usage, at = /* @__PURE__ */ new Date()) {
|
|
|
1439
1571
|
}
|
|
1440
1572
|
};
|
|
1441
1573
|
}
|
|
1442
|
-
var OPENAI_PRICING = {
|
|
1443
|
-
"gpt-6-astra": { promptPerMillion: 10, cachedPerMillion: 1, candidatePerMillion: 50 },
|
|
1444
|
-
"gpt-6.1-sol": { promptPerMillion: 2, cachedPerMillion: 0.1, candidatePerMillion: 10 },
|
|
1445
|
-
"gpt-6.1-terra": { promptPerMillion: 1, cachedPerMillion: 0.05, candidatePerMillion: 5 },
|
|
1446
|
-
"gpt-6-sol": { promptPerMillion: 2, cachedPerMillion: 0.2, candidatePerMillion: 10 },
|
|
1447
|
-
"gpt-6-luna": { promptPerMillion: 0.1, cachedPerMillion: 0.01, candidatePerMillion: 0.5 }
|
|
1448
|
-
};
|
|
1449
|
-
var OPENAI_LONG_CONTEXT_THRESHOLD = 272e3;
|
|
1450
|
-
var OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER = 2;
|
|
1451
|
-
var OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER = 1.5;
|
|
1452
|
-
var OPENAI_CACHE_WRITE_30M_MULTIPLIER = 1.25;
|
|
1453
|
-
var OPENAI_CACHE_WRITE_12H_MULTIPLIER = 2.5;
|
|
1454
|
-
function openaiRatesFor(model, promptTokens) {
|
|
1455
|
-
const rate = OPENAI_PRICING[model];
|
|
1456
|
-
if (promptTokens <= OPENAI_LONG_CONTEXT_THRESHOLD) {
|
|
1457
|
-
return rate;
|
|
1458
|
-
}
|
|
1459
|
-
return {
|
|
1460
|
-
promptPerMillion: rate.promptPerMillion * OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER,
|
|
1461
|
-
cachedPerMillion: rate.cachedPerMillion * OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER,
|
|
1462
|
-
candidatePerMillion: rate.candidatePerMillion * OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER
|
|
1463
|
-
};
|
|
1464
|
-
}
|
|
1465
|
-
function openaiTokenCost(model, usage) {
|
|
1466
|
-
const { promptPerMillion, cachedPerMillion, candidatePerMillion } = openaiRatesFor(model, usage.inputTokens);
|
|
1467
|
-
const m = TOKENS_PER_MILLION;
|
|
1468
|
-
const uncachedTokens = Math.max(0, usage.inputTokens - usage.cacheReadTokens - usage.cacheWriteTokens);
|
|
1469
|
-
const write12hTokens = Math.min(usage.cacheWrite12hTokens, usage.cacheWriteTokens);
|
|
1470
|
-
const write30mTokens = Math.max(0, usage.cacheWriteTokens - write12hTokens);
|
|
1471
|
-
const promptCost = uncachedTokens / m * promptPerMillion;
|
|
1472
|
-
const cacheReadCost = usage.cacheReadTokens / m * cachedPerMillion;
|
|
1473
|
-
const cacheWriteCost = write30mTokens / m * promptPerMillion * OPENAI_CACHE_WRITE_30M_MULTIPLIER + write12hTokens / m * promptPerMillion * OPENAI_CACHE_WRITE_12H_MULTIPLIER;
|
|
1474
|
-
const candidateCost = usage.outputTokens / m * candidatePerMillion;
|
|
1475
|
-
const cacheReadFull = usage.cacheReadTokens / m * promptPerMillion;
|
|
1476
|
-
const cacheWriteFull = usage.cacheWriteTokens / m * promptPerMillion;
|
|
1477
|
-
return {
|
|
1478
|
-
costUsd: promptCost + cacheReadCost + cacheWriteCost + candidateCost,
|
|
1479
|
-
savedUsd: cacheReadFull - cacheReadCost + (cacheWriteFull - cacheWriteCost),
|
|
1480
|
-
breakdown: {
|
|
1481
|
-
promptUsd: promptCost,
|
|
1482
|
-
cacheReadUsd: cacheReadCost,
|
|
1483
|
-
cacheWriteUsd: cacheWriteCost,
|
|
1484
|
-
candidateUsd: candidateCost
|
|
1485
|
-
}
|
|
1486
|
-
};
|
|
1487
|
-
}
|
|
1488
1574
|
|
|
1489
1575
|
// ../../foundation-ai/dist/esm/utils/tool-schema.js
|
|
1490
1576
|
var SCHEMA_MAP_KEYS = ["properties", "patternProperties", "$defs", "definitions"];
|
|
@@ -1667,135 +1753,6 @@ function toGeminiSchema(parameters) {
|
|
|
1667
1753
|
});
|
|
1668
1754
|
return translated;
|
|
1669
1755
|
}
|
|
1670
|
-
var OPENAI_UNDROPPABLE_KEYWORDS = ["allOf", "not", "if", "then", "else", "oneOf"];
|
|
1671
|
-
function withNull(node) {
|
|
1672
|
-
if (typeof node.type === "string") {
|
|
1673
|
-
const out = Object.assign(Object.assign({}, node), { type: node.type === "null" ? "null" : [node.type, "null"] });
|
|
1674
|
-
if (Array.isArray(node.enum) && !node.enum.includes(null))
|
|
1675
|
-
out.enum = [...node.enum, null];
|
|
1676
|
-
return out;
|
|
1677
|
-
}
|
|
1678
|
-
if (Array.isArray(node.type)) {
|
|
1679
|
-
if (node.type.includes("null"))
|
|
1680
|
-
return node;
|
|
1681
|
-
const out = Object.assign(Object.assign({}, node), { type: [...node.type, "null"] });
|
|
1682
|
-
if (Array.isArray(node.enum) && !node.enum.includes(null))
|
|
1683
|
-
out.enum = [...node.enum, null];
|
|
1684
|
-
return out;
|
|
1685
|
-
}
|
|
1686
|
-
if (Array.isArray(node.anyOf)) {
|
|
1687
|
-
return node.anyOf.some((b) => isPlainObject2(b) && b.type === "null") ? node : Object.assign(Object.assign({}, node), { anyOf: [...node.anyOf, { type: "null" }] });
|
|
1688
|
-
}
|
|
1689
|
-
return { anyOf: [node, { type: "null" }] };
|
|
1690
|
-
}
|
|
1691
|
-
function toOpenAIStrictSchema(parameters) {
|
|
1692
|
-
const madeNullable = [];
|
|
1693
|
-
const overridden = [];
|
|
1694
|
-
const undroppable = [];
|
|
1695
|
-
const schema = mapSchema(parameters, (node, path) => {
|
|
1696
|
-
for (const keyword of OPENAI_UNDROPPABLE_KEYWORDS) {
|
|
1697
|
-
if (keyword in node)
|
|
1698
|
-
undroppable.push(`${path}/${keyword}`);
|
|
1699
|
-
}
|
|
1700
|
-
const isObjectNode = node.type === "object" || Array.isArray(node.type) && node.type.includes("object") || isPlainObject2(node.properties);
|
|
1701
|
-
if (!isObjectNode)
|
|
1702
|
-
return node;
|
|
1703
|
-
if (node.additionalProperties !== false) {
|
|
1704
|
-
if (node.additionalProperties !== void 0)
|
|
1705
|
-
overridden.push(`${path}/additionalProperties`);
|
|
1706
|
-
node.additionalProperties = false;
|
|
1707
|
-
}
|
|
1708
|
-
if (isPlainObject2(node.properties)) {
|
|
1709
|
-
const names = Object.keys(node.properties);
|
|
1710
|
-
const required = new Set(Array.isArray(node.required) ? node.required : []);
|
|
1711
|
-
const properties = node.properties;
|
|
1712
|
-
for (const name of names) {
|
|
1713
|
-
if (required.has(name))
|
|
1714
|
-
continue;
|
|
1715
|
-
properties[name] = isPlainObject2(properties[name]) ? withNull(properties[name]) : { type: "null" };
|
|
1716
|
-
madeNullable.push(`${path}/properties/${name}`);
|
|
1717
|
-
}
|
|
1718
|
-
node.required = names;
|
|
1719
|
-
}
|
|
1720
|
-
return node;
|
|
1721
|
-
});
|
|
1722
|
-
return { schema, madeNullable, overridden, undroppable };
|
|
1723
|
-
}
|
|
1724
|
-
function enforceOpenAIToolSchema(parameters, toolName) {
|
|
1725
|
-
const { schema, madeNullable, overridden, undroppable } = toOpenAIStrictSchema(parameters);
|
|
1726
|
-
if (madeNullable.length) {
|
|
1727
|
-
logger.debug(`OpenAITransport: tool "${toolName}" is strict, which has no optional properties; these are now required and nullable, so an absent value arrives as null: ${madeNullable.join(", ")}`);
|
|
1728
|
-
}
|
|
1729
|
-
if (overridden.length) {
|
|
1730
|
-
logger.warn(`OpenAITransport: tool "${toolName}" is strict, which requires closed objects; \`additionalProperties\` was set to false at: ${overridden.join(", ")}.`);
|
|
1731
|
-
}
|
|
1732
|
-
if (undroppable.length) {
|
|
1733
|
-
logger.warn(`OpenAITransport: tool "${toolName}" uses schema keywords strict mode cannot express (${undroppable.join(", ")}). They are left in place, so the request will be rejected. Rewrite them or drop enforceSchema for this tool.`);
|
|
1734
|
-
}
|
|
1735
|
-
return schema;
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
// ../../foundation-ai/dist/esm/transports/budget-exhausted-error.js
|
|
1739
|
-
var BUDGET_EXCEEDED_CODE = "BUDGET_EXCEEDED";
|
|
1740
|
-
var VENDOR_LABELS = Object.freeze({
|
|
1741
|
-
anthropic: "Anthropic",
|
|
1742
|
-
gemini: "Gemini",
|
|
1743
|
-
openai: "OpenAI",
|
|
1744
|
-
chrome: "Chrome"
|
|
1745
|
-
});
|
|
1746
|
-
var BUDGETED_VENDORS = Object.freeze([
|
|
1747
|
-
"anthropic",
|
|
1748
|
-
"gemini",
|
|
1749
|
-
"openai"
|
|
1750
|
-
]);
|
|
1751
|
-
var VENDOR_TYPE_BY_LABEL = new Map(Object.entries(VENDOR_LABELS).map(([type, label]) => [
|
|
1752
|
-
label.toLowerCase(),
|
|
1753
|
-
type
|
|
1754
|
-
]));
|
|
1755
|
-
function vendorTypeOfLabel(label) {
|
|
1756
|
-
if (typeof label !== "string")
|
|
1757
|
-
return void 0;
|
|
1758
|
-
return VENDOR_TYPE_BY_LABEL.get(label.trim().toLowerCase());
|
|
1759
|
-
}
|
|
1760
|
-
var BUDGET_EXCEEDED_STATUS = 402;
|
|
1761
|
-
var DEFAULT_BUDGET_EXHAUSTED_MESSAGE = "You've reached your AI usage limit. Contact your administrator to raise it.";
|
|
1762
|
-
var BudgetExhaustedError = class extends Error {
|
|
1763
|
-
constructor(vendorLabel, budgetUsd, spentUsd, detail, extra) {
|
|
1764
|
-
super(`${vendorLabel} request refused: the AI usage budget is exhausted` + (budgetUsd != null ? ` (spent ${spentUsd != null ? `$${spentUsd}` : "an unknown amount"} of a $${budgetUsd} budget)` : "") + (detail ? `: ${detail}` : "") + ". Retrying will not clear this \u2014 the budget must be raised.");
|
|
1765
|
-
this.vendorLabel = vendorLabel;
|
|
1766
|
-
this.budgetUsd = budgetUsd;
|
|
1767
|
-
this.spentUsd = spentUsd;
|
|
1768
|
-
this.detail = detail;
|
|
1769
|
-
this.name = "BudgetExhaustedError";
|
|
1770
|
-
this.otherVendorAvailable = extra === null || extra === void 0 ? void 0 : extra.otherVendorAvailable;
|
|
1771
|
-
this.serverVendor = extra === null || extra === void 0 ? void 0 : extra.serverVendor;
|
|
1772
|
-
}
|
|
1773
|
-
};
|
|
1774
|
-
var isBudgetRejection = (status, code) => status === BUDGET_EXCEEDED_STATUS || code === BUDGET_EXCEEDED_CODE;
|
|
1775
|
-
var parseJsonOrUndefined = (text) => {
|
|
1776
|
-
try {
|
|
1777
|
-
return JSON.parse(text);
|
|
1778
|
-
} catch (_a) {
|
|
1779
|
-
return void 0;
|
|
1780
|
-
}
|
|
1781
|
-
};
|
|
1782
|
-
var codeOf = (payload) => {
|
|
1783
|
-
const code = payload === null || payload === void 0 ? void 0 : payload.code;
|
|
1784
|
-
return typeof code === "string" ? code : void 0;
|
|
1785
|
-
};
|
|
1786
|
-
var numberOrUndefined = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
1787
|
-
var booleanOrUndefined = (value) => typeof value === "boolean" ? value : void 0;
|
|
1788
|
-
var stringOrUndefined = (value) => typeof value === "string" && value.trim() !== "" ? value : void 0;
|
|
1789
|
-
function budgetExhaustedFrom(vendorLabel, payload, fallbackDetail) {
|
|
1790
|
-
var _a, _b, _c, _d;
|
|
1791
|
-
const body = typeof payload === "object" && payload !== null ? payload : {};
|
|
1792
|
-
const details = typeof body.details === "object" && body.details !== null ? body.details : {};
|
|
1793
|
-
const detail = typeof body.error === "string" ? body.error : fallbackDetail;
|
|
1794
|
-
return new BudgetExhaustedError(vendorLabel, (_a = numberOrUndefined(body.budgetUsd)) !== null && _a !== void 0 ? _a : numberOrUndefined(details.budgetUsd), (_b = numberOrUndefined(body.spentUsd)) !== null && _b !== void 0 ? _b : numberOrUndefined(details.spentUsd), detail, {
|
|
1795
|
-
otherVendorAvailable: (_c = booleanOrUndefined(body.otherVendorAvailable)) !== null && _c !== void 0 ? _c : booleanOrUndefined(details.otherVendorAvailable),
|
|
1796
|
-
serverVendor: (_d = stringOrUndefined(body.vendor)) !== null && _d !== void 0 ? _d : stringOrUndefined(details.vendor)
|
|
1797
|
-
});
|
|
1798
|
-
}
|
|
1799
1756
|
|
|
1800
1757
|
// ../../foundation-ai/dist/esm/utils/abort-reason.js
|
|
1801
1758
|
function authoritativeAbortReason(error, timeoutSignal, callerSignal) {
|
|
@@ -1930,8 +1887,6 @@ var CONTEXT_OVERFLOW_PHRASES = Object.freeze([
|
|
|
1930
1887
|
/input token count .* exceeds/i,
|
|
1931
1888
|
/maximum context length/i,
|
|
1932
1889
|
/context[_ ]length[_ ]exceeded/i,
|
|
1933
|
-
// OpenAI Responses: `Your input exceeds the context window of this model.`
|
|
1934
|
-
/exceeds the context window/i,
|
|
1935
1890
|
/too many (input )?tokens/i
|
|
1936
1891
|
]);
|
|
1937
1892
|
var TOKEN_FIGURES = /(\d[\d,]{2,})\D{0,40}?(?:>|exceeds)\D{0,80}?(\d[\d,]{2,})/i;
|
|
@@ -2050,94 +2005,6 @@ function readFramedBody(stream, onChunk, vendorLabel, stallTimeout) {
|
|
|
2050
2005
|
});
|
|
2051
2006
|
}
|
|
2052
2007
|
|
|
2053
|
-
// ../../foundation-ai/dist/esm/transports/provider-refused.js
|
|
2054
|
-
var PROVIDER_REFUSED_CODE = "PROVIDER_REFUSED";
|
|
2055
|
-
var DEFAULT_PROVIDER_REFUSED_MESSAGE = "AI requests are currently being refused by the provider. This isn't related to your account or your usage and needs no action from you \u2014 please contact your support team so it can be restored.";
|
|
2056
|
-
var ProviderRefusedError = class extends Error {
|
|
2057
|
-
constructor(vendorLabel, kind, upstreamStatus, upstreamType, detail) {
|
|
2058
|
-
super(`${vendorLabel} refused the request (${kind})` + (upstreamStatus != null ? ` \u2014 HTTP ${upstreamStatus}` : "") + (upstreamType ? ` ${upstreamType}` : "") + (detail ? `: ${detail}` : "") + ". Retrying will not clear this \u2014 the provider account must be fixed.");
|
|
2059
|
-
this.vendorLabel = vendorLabel;
|
|
2060
|
-
this.kind = kind;
|
|
2061
|
-
this.upstreamStatus = upstreamStatus;
|
|
2062
|
-
this.upstreamType = upstreamType;
|
|
2063
|
-
this.detail = detail;
|
|
2064
|
-
this.name = "ProviderRefusedError";
|
|
2065
|
-
}
|
|
2066
|
-
};
|
|
2067
|
-
var ANTHROPIC_SPEND_PHRASES = Object.freeze([
|
|
2068
|
-
/credit balance/i,
|
|
2069
|
-
/usage limit/i,
|
|
2070
|
-
/spend limit/i,
|
|
2071
|
-
/insufficient (credit|funds|balance)/i
|
|
2072
|
-
]);
|
|
2073
|
-
var ANTHROPIC_AUTH_TYPES = Object.freeze([
|
|
2074
|
-
"authentication_error",
|
|
2075
|
-
"permission_error"
|
|
2076
|
-
]);
|
|
2077
|
-
function anthropicErrorBody(payload) {
|
|
2078
|
-
var _a;
|
|
2079
|
-
const outer = typeof payload === "object" && payload !== null ? payload : {};
|
|
2080
|
-
for (const candidate of [
|
|
2081
|
-
outer.error,
|
|
2082
|
-
(_a = outer.details) === null || _a === void 0 ? void 0 : _a.error
|
|
2083
|
-
]) {
|
|
2084
|
-
if (typeof candidate === "object" && candidate !== null) {
|
|
2085
|
-
const { type, message } = candidate;
|
|
2086
|
-
return {
|
|
2087
|
-
type: typeof type === "string" ? type : void 0,
|
|
2088
|
-
message: typeof message === "string" ? message : void 0
|
|
2089
|
-
};
|
|
2090
|
-
}
|
|
2091
|
-
}
|
|
2092
|
-
return void 0;
|
|
2093
|
-
}
|
|
2094
|
-
function statedKind(payload) {
|
|
2095
|
-
const outer = typeof payload === "object" && payload !== null ? payload : {};
|
|
2096
|
-
const nested = typeof outer.details === "object" && outer.details !== null ? outer.details : {};
|
|
2097
|
-
for (const candidate of [outer.kind, nested.kind]) {
|
|
2098
|
-
if (candidate === "auth" || candidate === "spend")
|
|
2099
|
-
return candidate;
|
|
2100
|
-
}
|
|
2101
|
-
return void 0;
|
|
2102
|
-
}
|
|
2103
|
-
function kindFromEnvelopeType(payload) {
|
|
2104
|
-
const body = anthropicErrorBody(payload);
|
|
2105
|
-
if (!(body === null || body === void 0 ? void 0 : body.type))
|
|
2106
|
-
return void 0;
|
|
2107
|
-
if (ANTHROPIC_AUTH_TYPES.includes(body.type))
|
|
2108
|
-
return "auth";
|
|
2109
|
-
if (body.type === "billing_error")
|
|
2110
|
-
return "spend";
|
|
2111
|
-
return void 0;
|
|
2112
|
-
}
|
|
2113
|
-
function providerRefusalOf(vendor, code, payload) {
|
|
2114
|
-
var _a, _b;
|
|
2115
|
-
if (code === PROVIDER_REFUSED_CODE) {
|
|
2116
|
-
return (_b = (_a = statedKind(payload)) !== null && _a !== void 0 ? _a : kindFromEnvelopeType(payload)) !== null && _b !== void 0 ? _b : "spend";
|
|
2117
|
-
}
|
|
2118
|
-
if (vendor !== "anthropic")
|
|
2119
|
-
return void 0;
|
|
2120
|
-
const structural = kindFromEnvelopeType(payload);
|
|
2121
|
-
if (structural)
|
|
2122
|
-
return structural;
|
|
2123
|
-
const body = anthropicErrorBody(payload);
|
|
2124
|
-
if ((body === null || body === void 0 ? void 0 : body.type) === "invalid_request_error" && body.message) {
|
|
2125
|
-
const message = body.message;
|
|
2126
|
-
if (ANTHROPIC_SPEND_PHRASES.some((phrase) => phrase.test(message)))
|
|
2127
|
-
return "spend";
|
|
2128
|
-
}
|
|
2129
|
-
return void 0;
|
|
2130
|
-
}
|
|
2131
|
-
function providerRefusedFrom(vendorLabel, kind, upstreamStatus, payload, fallbackDetail) {
|
|
2132
|
-
var _a;
|
|
2133
|
-
const body = anthropicErrorBody(payload);
|
|
2134
|
-
return new ProviderRefusedError(vendorLabel, kind, upstreamStatus, body === null || body === void 0 ? void 0 : body.type, (_a = body === null || body === void 0 ? void 0 : body.message) !== null && _a !== void 0 ? _a : fallbackDetail);
|
|
2135
|
-
}
|
|
2136
|
-
function vendorOfTransportLabel(vendorLabel) {
|
|
2137
|
-
const entry = Object.entries(VENDOR_LABELS).find(([, label]) => label === vendorLabel);
|
|
2138
|
-
return entry === null || entry === void 0 ? void 0 : entry[0];
|
|
2139
|
-
}
|
|
2140
|
-
|
|
2141
2008
|
// ../../foundation-ai/dist/esm/transports/retry-hints.js
|
|
2142
2009
|
var MAX_SERVER_REQUESTED_BACKOFF_MS = 6e4;
|
|
2143
2010
|
var MAX_TOTAL_SERVER_REQUESTED_BACKOFF_MS = 12e4;
|
|
@@ -3065,610 +2932,6 @@ AnthropicTransport.RETRYABLE_STATUSES = [
|
|
|
3065
2932
|
];
|
|
3066
2933
|
AnthropicTransport.BACKOFF_BASE_MS = 1e3;
|
|
3067
2934
|
|
|
3068
|
-
// ../../foundation-ai/dist/esm/transports/openai-transport.js
|
|
3069
|
-
var OPENAI_DIRECT_URL = "https://api.openai.com/v1/responses";
|
|
3070
|
-
var DEFAULT_MODEL2 = "gpt-6-luna";
|
|
3071
|
-
var DEFAULT_TIMEOUT2 = 3e5;
|
|
3072
|
-
var DEFAULT_STALL_TIMEOUT2 = 3e4;
|
|
3073
|
-
var OPENAI_CONTEXT_LIMITS = {
|
|
3074
|
-
"gpt-6-astra": 922e3,
|
|
3075
|
-
"gpt-6.1-sol": 105e4,
|
|
3076
|
-
"gpt-6.1-terra": 105e4,
|
|
3077
|
-
"gpt-6-sol": 105e4,
|
|
3078
|
-
"gpt-6-luna": 105e4
|
|
3079
|
-
};
|
|
3080
|
-
var OPENAI_DEFAULT_TEMPERATURE = 1;
|
|
3081
|
-
var OPENAI_MAX_TEMPERATURE = 2;
|
|
3082
|
-
var OPENAI_EFFORT_NONE_TIERS = ["gpt-6.1-terra", "gpt-6-luna"];
|
|
3083
|
-
var OPENAI_EFFORT = {
|
|
3084
|
-
minimal: "low",
|
|
3085
|
-
low: "low",
|
|
3086
|
-
medium: "medium",
|
|
3087
|
-
high: "high",
|
|
3088
|
-
max: "max"
|
|
3089
|
-
};
|
|
3090
|
-
function openaiEffortFor(model, policy) {
|
|
3091
|
-
if (policy === void 0 || policy === "auto")
|
|
3092
|
-
return void 0;
|
|
3093
|
-
if (policy === "off")
|
|
3094
|
-
return OPENAI_EFFORT_NONE_TIERS.includes(model) ? "none" : "low";
|
|
3095
|
-
return OPENAI_EFFORT[policy];
|
|
3096
|
-
}
|
|
3097
|
-
function toOpenAIToolChoice(choice) {
|
|
3098
|
-
if (!choice || choice === "auto")
|
|
3099
|
-
return void 0;
|
|
3100
|
-
if (choice === "required" || choice === "none")
|
|
3101
|
-
return choice;
|
|
3102
|
-
return { type: "function", name: choice.tool };
|
|
3103
|
-
}
|
|
3104
|
-
function toOpenAITool(tool) {
|
|
3105
|
-
const parameters = tool.enforceSchema ? enforceOpenAIToolSchema(tool.parameters, tool.name) : tool.parameters;
|
|
3106
|
-
return {
|
|
3107
|
-
type: "function",
|
|
3108
|
-
name: tool.name,
|
|
3109
|
-
description: tool.description,
|
|
3110
|
-
parameters,
|
|
3111
|
-
strict: !!tool.enforceSchema
|
|
3112
|
-
};
|
|
3113
|
-
}
|
|
3114
|
-
function assertSupportedOpenAIModel(model) {
|
|
3115
|
-
if (!SUPPORTED_OPENAI_MODEL_IDS.includes(model)) {
|
|
3116
|
-
throw new Error(`OpenAITransport: unsupported model "${model}". Use one of: ${SUPPORTED_OPENAI_MODEL_IDS.join(", ")}.`);
|
|
3117
|
-
}
|
|
3118
|
-
}
|
|
3119
|
-
var OPENAI_MODEL_WARNINGS = {
|
|
3120
|
-
"gpt-6-astra": "OpenAITransport: using gpt-6-astra \u2014 flagship model at $10/$50 per MTok; reserve for tasks where the cheaper tiers are insufficient.",
|
|
3121
|
-
"gpt-6.1-sol": 'OpenAITransport: using gpt-6.1-sol \u2014 materially higher cost than terra/luna, and it cannot turn reasoning off (thinkingPolicy "off" runs at effort "low").',
|
|
3122
|
-
"gpt-6-sol": "OpenAITransport: using gpt-6-sol \u2014 superseded by gpt-6.1-sol at the same price, and it cannot turn reasoning off."
|
|
3123
|
-
};
|
|
3124
|
-
var OPENAI_PROVIDER_KEY = "openai";
|
|
3125
|
-
var CACHE_BREAKPOINT = Object.freeze({ mode: "explicit" });
|
|
3126
|
-
function imagePart(att) {
|
|
3127
|
-
const part = {
|
|
3128
|
-
type: "input_image",
|
|
3129
|
-
image_url: `data:${normalizeImageMime(att.mimeType)};base64,${stripImageDataUrl(att.data)}`
|
|
3130
|
-
};
|
|
3131
|
-
if (att.detail)
|
|
3132
|
-
part.detail = att.detail === "medium" ? "auto" : att.detail;
|
|
3133
|
-
return part;
|
|
3134
|
-
}
|
|
3135
|
-
function userContentParts(text, atts) {
|
|
3136
|
-
const parts = [];
|
|
3137
|
-
for (const att of atts) {
|
|
3138
|
-
if (att.kind === "image")
|
|
3139
|
-
parts.push(imagePart(att));
|
|
3140
|
-
}
|
|
3141
|
-
if (text)
|
|
3142
|
-
parts.push({ type: "input_text", text });
|
|
3143
|
-
for (const att of atts) {
|
|
3144
|
-
if (att.kind !== "image")
|
|
3145
|
-
parts.push({ type: "input_text", text: `[File: ${att.name}]
|
|
3146
|
-
${att.content}` });
|
|
3147
|
-
}
|
|
3148
|
-
return parts;
|
|
3149
|
-
}
|
|
3150
|
-
function turnStateOf(msg) {
|
|
3151
|
-
var _a, _b, _c, _d, _e;
|
|
3152
|
-
return (_b = (_a = msg.providerMetadata) === null || _a === void 0 ? void 0 : _a[OPENAI_PROVIDER_KEY]) !== null && _b !== void 0 ? _b : (_e = (_d = (_c = msg.toolCalls) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.providerMetadata) === null || _e === void 0 ? void 0 : _e[OPENAI_PROVIDER_KEY];
|
|
3153
|
-
}
|
|
3154
|
-
function replayableReasoning(item) {
|
|
3155
|
-
const { status: _status } = item, rest = __rest(item, ["status"]);
|
|
3156
|
-
return rest;
|
|
3157
|
-
}
|
|
3158
|
-
var OpenAITransport = class _OpenAITransport {
|
|
3159
|
-
constructor(config = {}) {
|
|
3160
|
-
var _a, _b, _c, _d;
|
|
3161
|
-
this.lifetimeCostUsd = 0;
|
|
3162
|
-
this.lifetimeSavingsUsd = 0;
|
|
3163
|
-
this.warnedThinkingClamped = false;
|
|
3164
|
-
const model = (_a = config.model) !== null && _a !== void 0 ? _a : DEFAULT_MODEL2;
|
|
3165
|
-
assertSupportedOpenAIModel(model);
|
|
3166
|
-
this.model = model;
|
|
3167
|
-
const warning = OPENAI_MODEL_WARNINGS[model];
|
|
3168
|
-
if (warning) {
|
|
3169
|
-
logger.warn(warning);
|
|
3170
|
-
}
|
|
3171
|
-
this.timeout = (_b = config.timeout) !== null && _b !== void 0 ? _b : DEFAULT_TIMEOUT2;
|
|
3172
|
-
this.stallTimeout = (_c = config.stallTimeout) !== null && _c !== void 0 ? _c : DEFAULT_STALL_TIMEOUT2;
|
|
3173
|
-
this.apiKey = config.apiKey;
|
|
3174
|
-
this.serverEndpoint = config.serverEndpoint;
|
|
3175
|
-
this.maxTokens = config.maxTokens;
|
|
3176
|
-
this.reasoningSummary = (_d = config.reasoningSummary) !== null && _d !== void 0 ? _d : "none";
|
|
3177
|
-
}
|
|
3178
|
-
getConfig() {
|
|
3179
|
-
return { provider: "openai", model: this.model, contextLimit: OPENAI_CONTEXT_LIMITS[this.model] };
|
|
3180
|
-
}
|
|
3181
|
-
getLifetimeCost() {
|
|
3182
|
-
return this.lifetimeCostUsd;
|
|
3183
|
-
}
|
|
3184
|
-
getLifetimeSavings() {
|
|
3185
|
-
return this.lifetimeSavingsUsd;
|
|
3186
|
-
}
|
|
3187
|
-
resetLifetimeCost() {
|
|
3188
|
-
this.lifetimeCostUsd = 0;
|
|
3189
|
-
this.lifetimeSavingsUsd = 0;
|
|
3190
|
-
}
|
|
3191
|
-
// ── AITransport (structured prompt) ────────────────────────────────────
|
|
3192
|
-
sendStructuredPrompt(options) {
|
|
3193
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
3194
|
-
const { systemPrompt, userPrompt, responseSchema } = options;
|
|
3195
|
-
const message = yield this.sendChatMessage([], userPrompt, {
|
|
3196
|
-
systemPrompt,
|
|
3197
|
-
responseSchema
|
|
3198
|
-
});
|
|
3199
|
-
return message.content;
|
|
3200
|
-
});
|
|
3201
|
-
}
|
|
3202
|
-
// ── ChatTransport (multi-turn chat) ────────────────────────────────────
|
|
3203
|
-
sendChatMessage(history, userMessage, options) {
|
|
3204
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
3205
|
-
const body = this.buildRequestBody(history, userMessage, options);
|
|
3206
|
-
const response = yield this.post(body, options === null || options === void 0 ? void 0 : options.signal);
|
|
3207
|
-
return this.fromResponse(response, body.max_output_tokens);
|
|
3208
|
-
});
|
|
3209
|
-
}
|
|
3210
|
-
/**
|
|
3211
|
-
* The Responses request body for a turn. Public for tests and for hosts that want to inspect the
|
|
3212
|
-
* exact wire shape; not part of the stable contract.
|
|
3213
|
-
*
|
|
3214
|
-
* @internal
|
|
3215
|
-
*/
|
|
3216
|
-
buildRequestBody(history, userMessage, options) {
|
|
3217
|
-
var _a, _b;
|
|
3218
|
-
const scope = (options === null || options === void 0 ? void 0 : options.cachePolicy) && options.cachePolicy.scope !== "default" ? options.cachePolicy.scope : void 0;
|
|
3219
|
-
const input = [];
|
|
3220
|
-
if (options === null || options === void 0 ? void 0 : options.systemPrompt) {
|
|
3221
|
-
const part = { type: "input_text", text: options.systemPrompt };
|
|
3222
|
-
if (scope)
|
|
3223
|
-
part.prompt_cache_breakpoint = CACHE_BREAKPOINT;
|
|
3224
|
-
input.push({ type: "message", role: "developer", content: [part] });
|
|
3225
|
-
}
|
|
3226
|
-
input.push(...this.toInputItems(history, userMessage, options === null || options === void 0 ? void 0 : options.attachments));
|
|
3227
|
-
if (scope === "history")
|
|
3228
|
-
this.markHistoryBreakpoint(input);
|
|
3229
|
-
if (options === null || options === void 0 ? void 0 : options.tailContext) {
|
|
3230
|
-
input.push({
|
|
3231
|
-
type: "message",
|
|
3232
|
-
role: "user",
|
|
3233
|
-
content: [{ type: "input_text", text: options.tailContext }]
|
|
3234
|
-
});
|
|
3235
|
-
}
|
|
3236
|
-
const body = {
|
|
3237
|
-
model: this.model,
|
|
3238
|
-
input,
|
|
3239
|
-
store: false,
|
|
3240
|
-
// Encrypted reasoning is what makes a stateless tool loop keep its chain of thought.
|
|
3241
|
-
include: ["reasoning.encrypted_content"]
|
|
3242
|
-
};
|
|
3243
|
-
if (scope) {
|
|
3244
|
-
body.prompt_cache_options = { mode: "explicit", ttl: "30m" };
|
|
3245
|
-
}
|
|
3246
|
-
if ((_a = options === null || options === void 0 ? void 0 : options.tools) === null || _a === void 0 ? void 0 : _a.length) {
|
|
3247
|
-
body.tools = options.tools.map(toOpenAITool);
|
|
3248
|
-
const toolChoice = toOpenAIToolChoice(options.toolChoice);
|
|
3249
|
-
if (toolChoice)
|
|
3250
|
-
body.tool_choice = toolChoice;
|
|
3251
|
-
}
|
|
3252
|
-
this.warnIfThinkingClamped(options === null || options === void 0 ? void 0 : options.thinkingPolicy);
|
|
3253
|
-
const effort = openaiEffortFor(this.model, options === null || options === void 0 ? void 0 : options.thinkingPolicy);
|
|
3254
|
-
if (effort) {
|
|
3255
|
-
body.reasoning = effort === "none" || this.reasoningSummary === "none" ? { effort } : { effort, summary: this.reasoningSummary };
|
|
3256
|
-
} else if (this.reasoningSummary !== "none") {
|
|
3257
|
-
body.reasoning = { summary: this.reasoningSummary };
|
|
3258
|
-
}
|
|
3259
|
-
if ((options === null || options === void 0 ? void 0 : options.temperature) != null && effort === "none") {
|
|
3260
|
-
body.temperature = scaleTemperature(options.temperature, {
|
|
3261
|
-
defaultTemp: OPENAI_DEFAULT_TEMPERATURE,
|
|
3262
|
-
maxTemp: OPENAI_MAX_TEMPERATURE
|
|
3263
|
-
});
|
|
3264
|
-
}
|
|
3265
|
-
const maxTokens = (_b = options === null || options === void 0 ? void 0 : options.maxTokens) !== null && _b !== void 0 ? _b : this.maxTokens;
|
|
3266
|
-
if (maxTokens != null)
|
|
3267
|
-
body.max_output_tokens = maxTokens;
|
|
3268
|
-
if (options === null || options === void 0 ? void 0 : options.responseSchema) {
|
|
3269
|
-
body.text = {
|
|
3270
|
-
format: {
|
|
3271
|
-
type: "json_schema",
|
|
3272
|
-
name: "response",
|
|
3273
|
-
schema: toOpenAIStrictSchema(options.responseSchema).schema,
|
|
3274
|
-
strict: true
|
|
3275
|
-
}
|
|
3276
|
-
};
|
|
3277
|
-
}
|
|
3278
|
-
return body;
|
|
3279
|
-
}
|
|
3280
|
-
// ── Private helpers ────────────────────────────────────────────────────
|
|
3281
|
-
warnIfThinkingClamped(policy) {
|
|
3282
|
-
if (this.warnedThinkingClamped)
|
|
3283
|
-
return;
|
|
3284
|
-
if (policy === "off" && !OPENAI_EFFORT_NONE_TIERS.includes(this.model)) {
|
|
3285
|
-
this.warnedThinkingClamped = true;
|
|
3286
|
-
logger.warn(`OpenAITransport: thinkingPolicy 'off' sent as effort 'low' \u2014 ${this.model} does not accept effort 'none'. Reasoning tokens are billed as output; use gpt-6.1-terra or gpt-6-luna if you need them gone.`);
|
|
3287
|
-
} else if (policy === "minimal") {
|
|
3288
|
-
this.warnedThinkingClamped = true;
|
|
3289
|
-
logger.warn(`OpenAITransport: thinkingPolicy 'minimal' sent as effort 'low' \u2014 OpenAI has no 'minimal' effort.`);
|
|
3290
|
-
}
|
|
3291
|
-
}
|
|
3292
|
-
toInputItems(history, userMessage, attachments) {
|
|
3293
|
-
var _a, _b, _c, _d, _e;
|
|
3294
|
-
const items = [];
|
|
3295
|
-
const replayedReasoning = /* @__PURE__ */ new Set();
|
|
3296
|
-
for (const msg of history) {
|
|
3297
|
-
if (msg.role === "system" || msg.role === "system-event" || msg.role === "synthetic-user" || msg.role === "compacted-summary" || msg.category === "reasoning" || msg.category === "narration" || // Driver-authored prose no model produced — see ChatMessage.excludeFromProvider.
|
|
3298
|
-
msg.excludeFromProvider || msg.thinking)
|
|
3299
|
-
continue;
|
|
3300
|
-
if (msg.toolResult) {
|
|
3301
|
-
items.push(this.toFunctionCallOutput(msg));
|
|
3302
|
-
continue;
|
|
3303
|
-
}
|
|
3304
|
-
if (msg.role === "user") {
|
|
3305
|
-
const content2 = userContentParts(msg.content, (_a = msg.attachments) !== null && _a !== void 0 ? _a : []);
|
|
3306
|
-
if (content2.length)
|
|
3307
|
-
items.push({ type: "message", role: "user", content: content2 });
|
|
3308
|
-
continue;
|
|
3309
|
-
}
|
|
3310
|
-
const state = turnStateOf(msg);
|
|
3311
|
-
if ((state === null || state === void 0 ? void 0 : state.model) === this.model) {
|
|
3312
|
-
for (const r of (_b = state.reasoning) !== null && _b !== void 0 ? _b : []) {
|
|
3313
|
-
const key = (_c = r.id) !== null && _c !== void 0 ? _c : r.encrypted_content;
|
|
3314
|
-
if (replayedReasoning.has(key))
|
|
3315
|
-
continue;
|
|
3316
|
-
replayedReasoning.add(key);
|
|
3317
|
-
items.push(replayableReasoning(r));
|
|
3318
|
-
}
|
|
3319
|
-
}
|
|
3320
|
-
if (msg.content) {
|
|
3321
|
-
const item = {
|
|
3322
|
-
type: "message",
|
|
3323
|
-
role: "assistant",
|
|
3324
|
-
content: [{ type: "output_text", text: msg.content }]
|
|
3325
|
-
};
|
|
3326
|
-
if (state === null || state === void 0 ? void 0 : state.phase)
|
|
3327
|
-
item.phase = state.phase;
|
|
3328
|
-
items.push(item);
|
|
3329
|
-
}
|
|
3330
|
-
for (const tc of (_d = msg.toolCalls) !== null && _d !== void 0 ? _d : []) {
|
|
3331
|
-
items.push({
|
|
3332
|
-
type: "function_call",
|
|
3333
|
-
call_id: tc.id,
|
|
3334
|
-
name: tc.name,
|
|
3335
|
-
arguments: JSON.stringify((_e = tc.args) !== null && _e !== void 0 ? _e : {})
|
|
3336
|
-
});
|
|
3337
|
-
}
|
|
3338
|
-
}
|
|
3339
|
-
const content = userContentParts(userMessage, attachments !== null && attachments !== void 0 ? attachments : []);
|
|
3340
|
-
if (content.length)
|
|
3341
|
-
items.push({ type: "message", role: "user", content });
|
|
3342
|
-
return items;
|
|
3343
|
-
}
|
|
3344
|
-
/**
|
|
3345
|
-
* A tool result. Plain string output when there is only text, so a turn without images keeps the
|
|
3346
|
-
* simplest wire shape; an array when the tool returned images (or when a cache breakpoint has to
|
|
3347
|
-
* be placed on it later — see `markHistoryBreakpoint`).
|
|
3348
|
-
*/
|
|
3349
|
-
toFunctionCallOutput(msg) {
|
|
3350
|
-
var _a;
|
|
3351
|
-
const result = msg.toolResult;
|
|
3352
|
-
const images = (_a = result.attachments) !== null && _a !== void 0 ? _a : [];
|
|
3353
|
-
if (!images.length) {
|
|
3354
|
-
return { type: "function_call_output", call_id: result.toolCallId, output: result.content };
|
|
3355
|
-
}
|
|
3356
|
-
return {
|
|
3357
|
-
type: "function_call_output",
|
|
3358
|
-
call_id: result.toolCallId,
|
|
3359
|
-
output: [{ type: "input_text", text: result.content }, ...images.map(imagePart)]
|
|
3360
|
-
};
|
|
3361
|
-
}
|
|
3362
|
-
/**
|
|
3363
|
-
* Put the history-scope breakpoint on the last stored item that can carry one: a user message's
|
|
3364
|
-
* text part or a tool result. Assistant items cannot carry one, and a trailing assistant item is
|
|
3365
|
-
* covered by the next request's breakpoint anyway.
|
|
3366
|
-
*/
|
|
3367
|
-
markHistoryBreakpoint(input) {
|
|
3368
|
-
for (let i = input.length - 1; i >= 0; i--) {
|
|
3369
|
-
const item = input[i];
|
|
3370
|
-
if (item.type === "function_call_output") {
|
|
3371
|
-
const output = typeof item.output === "string" ? [{ type: "input_text", text: item.output }] : [...item.output];
|
|
3372
|
-
const textIndex = output.findIndex((p) => p.type === "input_text");
|
|
3373
|
-
if (textIndex < 0)
|
|
3374
|
-
continue;
|
|
3375
|
-
output[textIndex] = Object.assign(Object.assign({}, output[textIndex]), { prompt_cache_breakpoint: CACHE_BREAKPOINT });
|
|
3376
|
-
input[i] = Object.assign(Object.assign({}, item), { output });
|
|
3377
|
-
return;
|
|
3378
|
-
}
|
|
3379
|
-
if (item.type === "message" && item.role !== "assistant") {
|
|
3380
|
-
const content = [...item.content];
|
|
3381
|
-
let textIndex = -1;
|
|
3382
|
-
for (let j = content.length - 1; j >= 0; j--) {
|
|
3383
|
-
if (content[j].type === "input_text") {
|
|
3384
|
-
textIndex = j;
|
|
3385
|
-
break;
|
|
3386
|
-
}
|
|
3387
|
-
}
|
|
3388
|
-
if (textIndex < 0)
|
|
3389
|
-
continue;
|
|
3390
|
-
content[textIndex] = Object.assign(Object.assign({}, content[textIndex]), { prompt_cache_breakpoint: CACHE_BREAKPOINT });
|
|
3391
|
-
input[i] = Object.assign(Object.assign({}, item), { content });
|
|
3392
|
-
return;
|
|
3393
|
-
}
|
|
3394
|
-
}
|
|
3395
|
-
}
|
|
3396
|
-
usageFromResponse(usage) {
|
|
3397
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
3398
|
-
if (!usage)
|
|
3399
|
-
return {};
|
|
3400
|
-
const u = usage;
|
|
3401
|
-
const cacheReadTokens = (_b = (_a = u.input_tokens_details) === null || _a === void 0 ? void 0 : _a.cached_tokens) !== null && _b !== void 0 ? _b : 0;
|
|
3402
|
-
const cacheWriteTokens = (_c = u.cache_write_tokens) !== null && _c !== void 0 ? _c : 0;
|
|
3403
|
-
const { costUsd, savedUsd } = openaiTokenCost(this.model, {
|
|
3404
|
-
inputTokens: (_d = u.input_tokens) !== null && _d !== void 0 ? _d : 0,
|
|
3405
|
-
outputTokens: (_e = u.output_tokens) !== null && _e !== void 0 ? _e : 0,
|
|
3406
|
-
cacheReadTokens,
|
|
3407
|
-
cacheWriteTokens,
|
|
3408
|
-
cacheWrite12hTokens: (_g = (_f = u.cache_write_tokens_details) === null || _f === void 0 ? void 0 : _f.ttl_12h_tokens) !== null && _g !== void 0 ? _g : 0
|
|
3409
|
-
});
|
|
3410
|
-
this.lifetimeCostUsd += costUsd;
|
|
3411
|
-
this.lifetimeSavingsUsd += savedUsd;
|
|
3412
|
-
const dp = _OpenAITransport.COST_DECIMAL_PLACES;
|
|
3413
|
-
console.log(`--- OpenAI Token Usage (${this.model}) ---`);
|
|
3414
|
-
console.log(`Input Tokens: ${(_h = u.input_tokens) !== null && _h !== void 0 ? _h : 0} (${cacheReadTokens} cached, ${cacheWriteTokens} written)`);
|
|
3415
|
-
console.log(`Output Tokens: ${(_j = u.output_tokens) !== null && _j !== void 0 ? _j : 0} (incl. ${(_l = (_k = u.output_tokens_details) === null || _k === void 0 ? void 0 : _k.reasoning_tokens) !== null && _l !== void 0 ? _l : 0} reasoning)`);
|
|
3416
|
-
console.log(`Total Cost: $${costUsd.toFixed(dp)}`);
|
|
3417
|
-
console.log(`Cache Saved: $${savedUsd.toFixed(dp)} (lifetime $${this.lifetimeSavingsUsd.toFixed(dp)})`);
|
|
3418
|
-
console.log(`Lifetime Cost: $${this.lifetimeCostUsd.toFixed(dp)}`);
|
|
3419
|
-
console.log("--------------------------");
|
|
3420
|
-
return {
|
|
3421
|
-
// Responses' input_tokens already INCLUDES cached and written tokens, matching what
|
|
3422
|
-
// ChatMessage.inputTokens means on Gemini (the whole prompt).
|
|
3423
|
-
inputTokens: u.input_tokens,
|
|
3424
|
-
// Reasoning tokens are already inside output_tokens — no folding needed (unlike Gemini).
|
|
3425
|
-
outputTokens: u.output_tokens,
|
|
3426
|
-
thoughtsTokens: (_m = u.output_tokens_details) === null || _m === void 0 ? void 0 : _m.reasoning_tokens,
|
|
3427
|
-
cacheReadTokens: (_o = u.input_tokens_details) === null || _o === void 0 ? void 0 : _o.cached_tokens,
|
|
3428
|
-
cacheWriteTokens: u.cache_write_tokens,
|
|
3429
|
-
cost: costUsd
|
|
3430
|
-
};
|
|
3431
|
-
}
|
|
3432
|
-
fromResponse(response, effectiveMaxTokens) {
|
|
3433
|
-
var _a, _b, _c, _d, _e, _f;
|
|
3434
|
-
const usage = this.usageFromResponse(response.usage);
|
|
3435
|
-
const output = (_a = response.output) !== null && _a !== void 0 ? _a : [];
|
|
3436
|
-
const reasoning = [];
|
|
3437
|
-
const summaryParts = [];
|
|
3438
|
-
const textParts = [];
|
|
3439
|
-
const refusals = [];
|
|
3440
|
-
const toolCalls = [];
|
|
3441
|
-
let phase;
|
|
3442
|
-
for (const item of output) {
|
|
3443
|
-
if (item.type === "reasoning") {
|
|
3444
|
-
reasoning.push(item);
|
|
3445
|
-
for (const s of (_b = item.summary) !== null && _b !== void 0 ? _b : []) {
|
|
3446
|
-
if (s.text)
|
|
3447
|
-
summaryParts.push(s.text);
|
|
3448
|
-
}
|
|
3449
|
-
} else if (item.type === "message") {
|
|
3450
|
-
if (typeof item.phase === "string" && phase !== "final_answer")
|
|
3451
|
-
phase = item.phase;
|
|
3452
|
-
for (const part of (_c = item.content) !== null && _c !== void 0 ? _c : []) {
|
|
3453
|
-
if (part.type === "output_text" && typeof part.text === "string")
|
|
3454
|
-
textParts.push(part.text);
|
|
3455
|
-
if (part.type === "refusal" && typeof part.refusal === "string")
|
|
3456
|
-
refusals.push(part.refusal);
|
|
3457
|
-
}
|
|
3458
|
-
} else if (item.type === "function_call") {
|
|
3459
|
-
toolCalls.push({
|
|
3460
|
-
id: String((_e = (_d = item.call_id) !== null && _d !== void 0 ? _d : item.id) !== null && _e !== void 0 ? _e : crypto.randomUUID()),
|
|
3461
|
-
name: String(item.name),
|
|
3462
|
-
args: parseArguments(item.arguments)
|
|
3463
|
-
});
|
|
3464
|
-
}
|
|
3465
|
-
}
|
|
3466
|
-
const state = { model: this.model };
|
|
3467
|
-
if (reasoning.length)
|
|
3468
|
-
state.reasoning = reasoning;
|
|
3469
|
-
if (phase)
|
|
3470
|
-
state.phase = phase;
|
|
3471
|
-
const providerMetadata = { [OPENAI_PROVIDER_KEY]: state };
|
|
3472
|
-
if (toolCalls.length)
|
|
3473
|
-
toolCalls[0].providerMetadata = providerMetadata;
|
|
3474
|
-
const narration = textParts.join("");
|
|
3475
|
-
const summary = summaryParts.join("\n\n");
|
|
3476
|
-
const base = Object.assign({ role: "assistant", content: narration, reasoning: summary || void 0, providerMetadata }, toolCalls.length ? { toolCalls } : {});
|
|
3477
|
-
const status = response.status;
|
|
3478
|
-
const incompleteReason = (_f = response.incomplete_details) === null || _f === void 0 ? void 0 : _f.reason;
|
|
3479
|
-
const finishReason = refusals.length && !narration && !toolCalls.length ? "refusal" : status === "incomplete" ? incompleteReason !== null && incompleteReason !== void 0 ? incompleteReason : "incomplete" : status;
|
|
3480
|
-
if (finishReason === "refusal") {
|
|
3481
|
-
base.content = refusals.join("\n");
|
|
3482
|
-
}
|
|
3483
|
-
if (status === "incomplete" && incompleteReason === "max_output_tokens") {
|
|
3484
|
-
if (toolCalls.length || !narration.trim()) {
|
|
3485
|
-
throw new ResponseTruncatedError(this.model, effectiveMaxTokens, usage.outputTokens, toolCalls.map((tc) => tc.name));
|
|
3486
|
-
}
|
|
3487
|
-
}
|
|
3488
|
-
if (!toolCalls.length && !narration && !summary && finishReason !== "refusal") {
|
|
3489
|
-
logger.warn("OpenAITransport: blank response", {
|
|
3490
|
-
model: this.model,
|
|
3491
|
-
status,
|
|
3492
|
-
incompleteReason,
|
|
3493
|
-
outputTypes: output.map((o) => o.type),
|
|
3494
|
-
usage: response.usage
|
|
3495
|
-
});
|
|
3496
|
-
}
|
|
3497
|
-
if (usage.inputTokens != null)
|
|
3498
|
-
base.inputTokens = usage.inputTokens;
|
|
3499
|
-
if (usage.outputTokens != null)
|
|
3500
|
-
base.outputTokens = usage.outputTokens;
|
|
3501
|
-
if (usage.cacheReadTokens != null)
|
|
3502
|
-
base.cacheReadTokens = usage.cacheReadTokens;
|
|
3503
|
-
if (usage.cacheWriteTokens != null)
|
|
3504
|
-
base.cacheWriteTokens = usage.cacheWriteTokens;
|
|
3505
|
-
if (usage.cost != null)
|
|
3506
|
-
base.cost = usage.cost;
|
|
3507
|
-
base.model = this.model;
|
|
3508
|
-
if (finishReason != null || usage.thoughtsTokens != null) {
|
|
3509
|
-
base.responseMeta = {
|
|
3510
|
-
finishReason,
|
|
3511
|
-
thoughtsTokens: usage.thoughtsTokens,
|
|
3512
|
-
parts: { functionCall: toolCalls.length, thought: summaryParts.length, text: textParts.length }
|
|
3513
|
-
};
|
|
3514
|
-
}
|
|
3515
|
-
return base;
|
|
3516
|
-
}
|
|
3517
|
-
buildEndpoint(body) {
|
|
3518
|
-
if (this.apiKey) {
|
|
3519
|
-
return {
|
|
3520
|
-
url: OPENAI_DIRECT_URL,
|
|
3521
|
-
payload: body,
|
|
3522
|
-
headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}` },
|
|
3523
|
-
credentials: "omit"
|
|
3524
|
-
};
|
|
3525
|
-
}
|
|
3526
|
-
if (this.serverEndpoint) {
|
|
3527
|
-
return {
|
|
3528
|
-
url: this.serverEndpoint,
|
|
3529
|
-
payload: body,
|
|
3530
|
-
// Opt in to the proxy's NDJSON heartbeat framing.
|
|
3531
|
-
headers: { "Content-Type": "application/json", Accept: "application/x-ndjson" },
|
|
3532
|
-
credentials: "include"
|
|
3533
|
-
};
|
|
3534
|
-
}
|
|
3535
|
-
throw new Error("OpenAITransport: no API key or server endpoint configured. Pass apiKey or serverEndpoint in OpenAIAIConfig.");
|
|
3536
|
-
}
|
|
3537
|
-
post(body, signal) {
|
|
3538
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
3539
|
-
const { url, payload, headers, credentials } = this.buildEndpoint(body);
|
|
3540
|
-
return yield postWithRetry({
|
|
3541
|
-
url,
|
|
3542
|
-
headers,
|
|
3543
|
-
body: payload,
|
|
3544
|
-
credentials,
|
|
3545
|
-
vendorLabel: VENDOR_LABELS.openai,
|
|
3546
|
-
retryableStatuses: _OpenAITransport.RETRYABLE_STATUSES,
|
|
3547
|
-
timeout: this.timeout,
|
|
3548
|
-
stallTimeout: this.stallTimeout,
|
|
3549
|
-
backoffBaseMs: _OpenAITransport.BACKOFF_BASE_MS,
|
|
3550
|
-
signal
|
|
3551
|
-
});
|
|
3552
|
-
});
|
|
3553
|
-
}
|
|
3554
|
-
};
|
|
3555
|
-
OpenAITransport.COST_DECIMAL_PLACES = 6;
|
|
3556
|
-
OpenAITransport.RETRYABLE_STATUSES = [429, 500, 502, 503];
|
|
3557
|
-
OpenAITransport.BACKOFF_BASE_MS = 1e3;
|
|
3558
|
-
function parseArguments(raw) {
|
|
3559
|
-
if (raw && typeof raw === "object")
|
|
3560
|
-
return raw;
|
|
3561
|
-
if (typeof raw !== "string" || !raw.trim())
|
|
3562
|
-
return {};
|
|
3563
|
-
try {
|
|
3564
|
-
const parsed = JSON.parse(raw);
|
|
3565
|
-
return parsed && typeof parsed === "object" ? parsed : {};
|
|
3566
|
-
} catch (_a) {
|
|
3567
|
-
logger.warn("OpenAITransport: function_call arguments are not valid JSON", { raw });
|
|
3568
|
-
return {};
|
|
3569
|
-
}
|
|
3570
|
-
}
|
|
3571
|
-
|
|
3572
|
-
// ../../foundation-ai/dist/esm/transports/server-openai-transport.js
|
|
3573
|
-
var AI_SERVER_PATH = "/gwf/ai-service/chat-completions";
|
|
3574
|
-
var DEFAULT_MODEL3 = "gpt-4o-mini";
|
|
3575
|
-
var DEFAULT_TIMEOUT3 = 3e4;
|
|
3576
|
-
var OPENAI_CONTEXT_LIMITS2 = {
|
|
3577
|
-
"gpt-4o": 128e3,
|
|
3578
|
-
"gpt-4o-mini": 128e3,
|
|
3579
|
-
"gpt-4-turbo": 128e3
|
|
3580
|
-
};
|
|
3581
|
-
var ServerOpenAITransport = class {
|
|
3582
|
-
constructor(config = {}) {
|
|
3583
|
-
var _a, _b;
|
|
3584
|
-
this.model = (_a = config.model) !== null && _a !== void 0 ? _a : DEFAULT_MODEL3;
|
|
3585
|
-
this.timeout = (_b = config.timeout) !== null && _b !== void 0 ? _b : DEFAULT_TIMEOUT3;
|
|
3586
|
-
}
|
|
3587
|
-
getConfig() {
|
|
3588
|
-
return {
|
|
3589
|
-
provider: "openai",
|
|
3590
|
-
model: this.model,
|
|
3591
|
-
contextLimit: OPENAI_CONTEXT_LIMITS2[this.model]
|
|
3592
|
-
};
|
|
3593
|
-
}
|
|
3594
|
-
sendStructuredPrompt(options) {
|
|
3595
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
3596
|
-
var _a, _b;
|
|
3597
|
-
const { systemPrompt, userPrompt, responseSchema } = options;
|
|
3598
|
-
const controller = new AbortController();
|
|
3599
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
3600
|
-
const response = yield fetch(AI_SERVER_PATH, {
|
|
3601
|
-
method: "POST",
|
|
3602
|
-
headers: {
|
|
3603
|
-
"Content-Type": "application/json"
|
|
3604
|
-
},
|
|
3605
|
-
body: JSON.stringify({
|
|
3606
|
-
provider: "openai",
|
|
3607
|
-
model: this.model,
|
|
3608
|
-
systemPrompt,
|
|
3609
|
-
userPrompt,
|
|
3610
|
-
responseSchema
|
|
3611
|
-
}),
|
|
3612
|
-
signal: controller.signal,
|
|
3613
|
-
credentials: "include"
|
|
3614
|
-
});
|
|
3615
|
-
clearTimeout(timeoutId);
|
|
3616
|
-
if (!response.ok) {
|
|
3617
|
-
const err = yield response.text();
|
|
3618
|
-
const parsed = parseJsonOrUndefined(err);
|
|
3619
|
-
const refusal = providerRefusalOf("openai", codeOf(parsed), parsed);
|
|
3620
|
-
if (refusal) {
|
|
3621
|
-
throw providerRefusedFrom(VENDOR_LABELS.openai, refusal, response.status, parsed, err || void 0);
|
|
3622
|
-
}
|
|
3623
|
-
if (isBudgetRejection(response.status, codeOf(parsed))) {
|
|
3624
|
-
throw budgetExhaustedFrom(VENDOR_LABELS.openai, parsed, err || void 0);
|
|
3625
|
-
}
|
|
3626
|
-
throw new Error(`AI proxy error ${response.status}: ${err}`);
|
|
3627
|
-
}
|
|
3628
|
-
const data = yield response.json();
|
|
3629
|
-
return (_b = (_a = data === null || data === void 0 ? void 0 : data.content) !== null && _a !== void 0 ? _a : data === null || data === void 0 ? void 0 : data.CONTENT) !== null && _b !== void 0 ? _b : "";
|
|
3630
|
-
});
|
|
3631
|
-
}
|
|
3632
|
-
};
|
|
3633
|
-
|
|
3634
|
-
// ../../foundation-ai/dist/esm/providers/openai-provider.js
|
|
3635
|
-
var OpenAIProvider = class {
|
|
3636
|
-
constructor(config, transport) {
|
|
3637
|
-
this.transport = transport;
|
|
3638
|
-
this.criteriaInstructions = config.criteriaInstructions;
|
|
3639
|
-
if (transport instanceof OpenAITransport) {
|
|
3640
|
-
this.prompt = (message, options) => __awaiter(this, void 0, void 0, function* () {
|
|
3641
|
-
return (yield transport.sendChatMessage([], message, options)).content;
|
|
3642
|
-
});
|
|
3643
|
-
this.chat = (history, userMessage, options) => transport.sendChatMessage(history, userMessage, options);
|
|
3644
|
-
}
|
|
3645
|
-
}
|
|
3646
|
-
getStatus() {
|
|
3647
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
3648
|
-
const config = this.transport instanceof OpenAITransport || this.transport instanceof ServerOpenAITransport ? this.transport.getConfig() : { provider: "openai", model: "gpt-4o-mini", contextLimit: void 0 };
|
|
3649
|
-
return { provider: "openai", model: config.model, contextLimit: config.contextLimit };
|
|
3650
|
-
});
|
|
3651
|
-
}
|
|
3652
|
-
interpretCriteria(input, context) {
|
|
3653
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
3654
|
-
var _a;
|
|
3655
|
-
const fields = (_a = context === null || context === void 0 ? void 0 : context.fields) !== null && _a !== void 0 ? _a : [];
|
|
3656
|
-
const systemPrompt = buildCriteriaSystemPrompt(fields, this.criteriaInstructions);
|
|
3657
|
-
const userPrompt = buildCriteriaUserPrompt(input);
|
|
3658
|
-
try {
|
|
3659
|
-
const raw = yield this.transport.sendStructuredPrompt({
|
|
3660
|
-
systemPrompt,
|
|
3661
|
-
userPrompt,
|
|
3662
|
-
responseSchema: CRITERIA_JSON_SCHEMA
|
|
3663
|
-
});
|
|
3664
|
-
return parseCriteriaResponse(raw);
|
|
3665
|
-
} catch (e) {
|
|
3666
|
-
logger.error("OpenAIProvider interpretCriteria failed:", e);
|
|
3667
|
-
}
|
|
3668
|
-
});
|
|
3669
|
-
}
|
|
3670
|
-
};
|
|
3671
|
-
|
|
3672
2935
|
// ../../foundation-ai/dist/esm/transports/gemini-malformed-call.js
|
|
3673
2936
|
var MALFORMED_CALL_ANCHOR = "default_api.";
|
|
3674
2937
|
var PRINT_WRAPPER_RE = /print\(\s*$/;
|
|
@@ -3877,9 +3140,9 @@ function repairMalformedFunctionCall(finishMessage, offeredToolNames) {
|
|
|
3877
3140
|
|
|
3878
3141
|
// ../../foundation-ai/dist/esm/transports/gemini-transport.js
|
|
3879
3142
|
var GEMINI_DIRECT_URL = (model) => `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
|
|
3880
|
-
var
|
|
3881
|
-
var
|
|
3882
|
-
var
|
|
3143
|
+
var DEFAULT_MODEL2 = "gemini-2.5-flash-lite";
|
|
3144
|
+
var DEFAULT_TIMEOUT2 = 3e5;
|
|
3145
|
+
var DEFAULT_STALL_TIMEOUT2 = 3e4;
|
|
3883
3146
|
var GEMINI_CONTEXT_LIMITS = {
|
|
3884
3147
|
"gemini-2.5-pro": 1048576,
|
|
3885
3148
|
"gemini-2.5-flash": 1048576,
|
|
@@ -4065,15 +3328,15 @@ var GeminiTransport = class _GeminiTransport {
|
|
|
4065
3328
|
this.lifetimeCostUsd = 0;
|
|
4066
3329
|
this.lifetimeSavingsUsd = 0;
|
|
4067
3330
|
this.warnedThinkingClamped = false;
|
|
4068
|
-
const model = (_a = config.model) !== null && _a !== void 0 ? _a :
|
|
3331
|
+
const model = (_a = config.model) !== null && _a !== void 0 ? _a : DEFAULT_MODEL2;
|
|
4069
3332
|
assertSupportedGeminiModel(model);
|
|
4070
3333
|
this.model = model;
|
|
4071
3334
|
const warning = GEMINI_MODEL_WARNINGS[model];
|
|
4072
3335
|
if (warning) {
|
|
4073
3336
|
logger.warn(warning);
|
|
4074
3337
|
}
|
|
4075
|
-
this.timeout = (_b = config.timeout) !== null && _b !== void 0 ? _b :
|
|
4076
|
-
this.stallTimeout = (_c = config.stallTimeout) !== null && _c !== void 0 ? _c :
|
|
3338
|
+
this.timeout = (_b = config.timeout) !== null && _b !== void 0 ? _b : DEFAULT_TIMEOUT2;
|
|
3339
|
+
this.stallTimeout = (_c = config.stallTimeout) !== null && _c !== void 0 ? _c : DEFAULT_STALL_TIMEOUT2;
|
|
4077
3340
|
this.apiKey = config.apiKey;
|
|
4078
3341
|
this.serverEndpoint = config.serverEndpoint;
|
|
4079
3342
|
}
|
|
@@ -13615,17 +12878,11 @@ function collectRows(messages, depth, subAgentOf) {
|
|
|
13615
12878
|
MALFORMED_CALL_NUDGE,
|
|
13616
12879
|
MutableAIProviderRegistry,
|
|
13617
12880
|
NOOP_ACTIVITY_BUS,
|
|
13618
|
-
OPENAI_CACHE_WRITE_12H_MULTIPLIER,
|
|
13619
|
-
OPENAI_CACHE_WRITE_30M_MULTIPLIER,
|
|
13620
|
-
OPENAI_LONG_CONTEXT_THRESHOLD,
|
|
13621
|
-
OpenAIProvider,
|
|
13622
|
-
OpenAITransport,
|
|
13623
12881
|
OrchestratingDriver,
|
|
13624
12882
|
PROVIDER_REFUSED_CODE,
|
|
13625
12883
|
ProviderRefusedError,
|
|
13626
12884
|
SUPPORTED_ANTHROPIC_MODEL_IDS,
|
|
13627
12885
|
SUPPORTED_GEMINI_MODEL_IDS,
|
|
13628
|
-
SUPPORTED_OPENAI_MODEL_IDS,
|
|
13629
12886
|
UNROUTABLE_INSTRUCTION,
|
|
13630
12887
|
UNROUTABLE_PLACEHOLDER,
|
|
13631
12888
|
addUsage,
|
|
@@ -13654,8 +12911,6 @@ function collectRows(messages, depth, subAgentOf) {
|
|
|
13654
12911
|
isTerminalAIServiceRefusal,
|
|
13655
12912
|
lifetimeDebugLog,
|
|
13656
12913
|
messageUsage,
|
|
13657
|
-
openaiRatesFor,
|
|
13658
|
-
openaiTokenCost,
|
|
13659
12914
|
resolveAITiers,
|
|
13660
12915
|
restoreMachine,
|
|
13661
12916
|
strictFallbackAgent,
|