@codelia/core 0.1.32 → 0.1.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +475 -179
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +475 -179
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -257,6 +257,10 @@ var ANTHROPIC_MODELS = [
|
|
|
257
257
|
id: "claude-opus-4-5-20251201",
|
|
258
258
|
provider: "anthropic"
|
|
259
259
|
},
|
|
260
|
+
{
|
|
261
|
+
id: "claude-sonnet-4-6",
|
|
262
|
+
provider: "anthropic"
|
|
263
|
+
},
|
|
260
264
|
{
|
|
261
265
|
id: "claude-sonnet-4-5-20250929",
|
|
262
266
|
provider: "anthropic"
|
|
@@ -381,10 +385,12 @@ var DEFAULT_MODEL_REGISTRY = createModelRegistry([
|
|
|
381
385
|
]);
|
|
382
386
|
|
|
383
387
|
// src/services/compaction/service.ts
|
|
384
|
-
var DEFAULT_THRESHOLD_RATIO = 0.
|
|
388
|
+
var DEFAULT_THRESHOLD_RATIO = 0.85;
|
|
385
389
|
var DEFAULT_RETAIN_LAST_TURNS = 1;
|
|
386
390
|
var DEFAULT_SUMMARY_PROMPT = "Summarize the conversation so it can be continued later. Focus on decisions, results, constraints, and next steps. Keep it concise and factual.";
|
|
387
391
|
var DEFAULT_RETAIN_PROMPT = "List concrete details that must be preserved verbatim. Include tool output refs, file paths, identifiers, commands, TODOs, and any critical decisions.";
|
|
392
|
+
var COMPACTION_RETAIN_PREFIX = "[codelia.compaction.retain]\n";
|
|
393
|
+
var COMPACTION_SUMMARY_PREFIX = "[codelia.compaction.summary]\n";
|
|
388
394
|
var CompactionService = class _CompactionService {
|
|
389
395
|
config;
|
|
390
396
|
modelRegistry;
|
|
@@ -420,6 +426,13 @@ var CompactionService = class _CompactionService {
|
|
|
420
426
|
};
|
|
421
427
|
}
|
|
422
428
|
const preparedMessages = this.prepareMessagesForSummary(messages);
|
|
429
|
+
if (preparedMessages.length === 0) {
|
|
430
|
+
return {
|
|
431
|
+
compacted: false,
|
|
432
|
+
compactedMessages: messages,
|
|
433
|
+
usage: null
|
|
434
|
+
};
|
|
435
|
+
}
|
|
423
436
|
const prompt = this.buildCompactionPrompt();
|
|
424
437
|
const interruptMessage = {
|
|
425
438
|
role: "user",
|
|
@@ -471,7 +484,10 @@ var CompactionService = class _CompactionService {
|
|
|
471
484
|
}
|
|
472
485
|
prepareMessagesForSummary(messages) {
|
|
473
486
|
if (messages.length === 0) return messages;
|
|
474
|
-
const prepared = messages.map((message) => ({ ...message }));
|
|
487
|
+
const prepared = messages.filter((message) => !isCompactionMemoryMessage(message)).map((message) => ({ ...message }));
|
|
488
|
+
if (prepared.length === 0) {
|
|
489
|
+
return prepared;
|
|
490
|
+
}
|
|
475
491
|
const last = prepared[prepared.length - 1];
|
|
476
492
|
if (last.role === "assistant" && last.tool_calls?.length) {
|
|
477
493
|
if (last.content) {
|
|
@@ -529,26 +545,73 @@ var CompactionService = class _CompactionService {
|
|
|
529
545
|
nonSystemMessages.push(message);
|
|
530
546
|
}
|
|
531
547
|
}
|
|
532
|
-
const
|
|
533
|
-
|
|
534
|
-
|
|
548
|
+
const existingMemory = this.collectCompactionMemory(nonSystemMessages);
|
|
549
|
+
const mergedRetain = mergeCompactionMemory(
|
|
550
|
+
existingMemory.retainEntries,
|
|
551
|
+
retain
|
|
552
|
+
);
|
|
553
|
+
const mergedSummary = summary.trim() || existingMemory.summaryEntries[existingMemory.summaryEntries.length - 1] || "";
|
|
554
|
+
const conversationMessages = nonSystemMessages.filter(
|
|
555
|
+
(message) => !isCompactionMemoryMessage(message)
|
|
556
|
+
);
|
|
557
|
+
const tail = this.normalizeRetainedTail(
|
|
558
|
+
this.getLastTurns(conversationMessages, this.config.retainLastTurns)
|
|
535
559
|
);
|
|
536
560
|
const compacted = [...systemMessages];
|
|
537
|
-
if (
|
|
561
|
+
if (mergedRetain) {
|
|
538
562
|
compacted.push({
|
|
539
563
|
role: "user",
|
|
540
|
-
content:
|
|
564
|
+
content: `${COMPACTION_RETAIN_PREFIX}${mergedRetain}`
|
|
541
565
|
});
|
|
542
566
|
}
|
|
543
|
-
if (
|
|
567
|
+
if (mergedSummary) {
|
|
544
568
|
compacted.push({
|
|
545
569
|
role: "user",
|
|
546
|
-
content:
|
|
570
|
+
content: `${COMPACTION_SUMMARY_PREFIX}${mergedSummary}`
|
|
547
571
|
});
|
|
548
572
|
}
|
|
549
573
|
compacted.push(...tail);
|
|
550
574
|
return compacted;
|
|
551
575
|
}
|
|
576
|
+
collectCompactionMemory(messages) {
|
|
577
|
+
const retainEntries = [];
|
|
578
|
+
const summaryEntries = [];
|
|
579
|
+
for (const message of messages) {
|
|
580
|
+
const parsed = parseCompactionMemory(message);
|
|
581
|
+
if (!parsed) {
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (parsed.kind === "retain") {
|
|
585
|
+
retainEntries.push(parsed.body);
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
summaryEntries.push(parsed.body);
|
|
589
|
+
}
|
|
590
|
+
return { retainEntries, summaryEntries };
|
|
591
|
+
}
|
|
592
|
+
normalizeRetainedTail(messages) {
|
|
593
|
+
const normalized = [];
|
|
594
|
+
for (const message of messages) {
|
|
595
|
+
if (message.role === "user") {
|
|
596
|
+
normalized.push(message);
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
if (message.role !== "assistant") {
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
const assistant = {
|
|
603
|
+
role: "assistant",
|
|
604
|
+
content: message.content,
|
|
605
|
+
...message.name ? { name: message.name } : {},
|
|
606
|
+
...message.refusal ? { refusal: message.refusal } : {}
|
|
607
|
+
};
|
|
608
|
+
if (assistant.content == null && assistant.refusal == null) {
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
normalized.push(assistant);
|
|
612
|
+
}
|
|
613
|
+
return normalized;
|
|
614
|
+
}
|
|
552
615
|
getLastTurns(messages, retainLastTurns) {
|
|
553
616
|
if (retainLastTurns <= 0) return [];
|
|
554
617
|
let remaining = retainLastTurns;
|
|
@@ -639,6 +702,39 @@ var extractTag = (text, tag) => {
|
|
|
639
702
|
const match = text.match(regex);
|
|
640
703
|
return match?.[1]?.trim() ?? "";
|
|
641
704
|
};
|
|
705
|
+
var parseCompactionMemory = (message) => {
|
|
706
|
+
if (message.role !== "user" || typeof message.content !== "string") {
|
|
707
|
+
return null;
|
|
708
|
+
}
|
|
709
|
+
if (message.content.startsWith(COMPACTION_RETAIN_PREFIX)) {
|
|
710
|
+
return {
|
|
711
|
+
kind: "retain",
|
|
712
|
+
body: message.content.slice(COMPACTION_RETAIN_PREFIX.length).trim()
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
if (message.content.startsWith(COMPACTION_SUMMARY_PREFIX)) {
|
|
716
|
+
return {
|
|
717
|
+
kind: "summary",
|
|
718
|
+
body: message.content.slice(COMPACTION_SUMMARY_PREFIX.length).trim()
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
return null;
|
|
722
|
+
};
|
|
723
|
+
var isCompactionMemoryMessage = (message) => parseCompactionMemory(message) !== null;
|
|
724
|
+
var mergeCompactionMemory = (existing, next) => {
|
|
725
|
+
const ordered = [...existing, next];
|
|
726
|
+
const seen = /* @__PURE__ */ new Set();
|
|
727
|
+
const merged = [];
|
|
728
|
+
for (const entry of ordered) {
|
|
729
|
+
const normalized = entry.trim();
|
|
730
|
+
if (!normalized || seen.has(normalized)) {
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
seen.add(normalized);
|
|
734
|
+
merged.push(normalized);
|
|
735
|
+
}
|
|
736
|
+
return merged.join("\n\n");
|
|
737
|
+
};
|
|
642
738
|
var stripSnapshotSuffix = (modelId) => modelId.replace(/-[0-9]{4}-[0-9]{2}-[0-9]{2}$/, "");
|
|
643
739
|
var parseQualifiedModelId = (modelId) => {
|
|
644
740
|
const sep = modelId.indexOf("/");
|
|
@@ -1075,8 +1171,7 @@ var Agent = class {
|
|
|
1075
1171
|
store: this.services.toolOutputCacheStore ?? null
|
|
1076
1172
|
});
|
|
1077
1173
|
this.usageService = new TokenUsageService({
|
|
1078
|
-
enabled: options.enableUsageTracking ?? true
|
|
1079
|
-
thresholdRatio: 0.5
|
|
1174
|
+
enabled: options.enableUsageTracking ?? true
|
|
1080
1175
|
});
|
|
1081
1176
|
this.canExecuteTool = options.canExecuteTool;
|
|
1082
1177
|
}
|
|
@@ -1088,11 +1183,15 @@ var Agent = class {
|
|
|
1088
1183
|
if (!usage) {
|
|
1089
1184
|
return null;
|
|
1090
1185
|
}
|
|
1091
|
-
const
|
|
1092
|
-
const modelSpec = resolveModelWithQualifiedFallback2(
|
|
1186
|
+
const usageModelSpec = usage.model ? resolveModelWithQualifiedFallback2(
|
|
1093
1187
|
this.modelRegistry,
|
|
1094
1188
|
this.llm.provider,
|
|
1095
|
-
|
|
1189
|
+
usage.model
|
|
1190
|
+
) : void 0;
|
|
1191
|
+
const modelSpec = usageModelSpec ?? resolveModelWithQualifiedFallback2(
|
|
1192
|
+
this.modelRegistry,
|
|
1193
|
+
this.llm.provider,
|
|
1194
|
+
this.llm.model
|
|
1096
1195
|
);
|
|
1097
1196
|
const contextLimit = modelSpec?.maxInputTokens ?? modelSpec?.contextWindow ?? null;
|
|
1098
1197
|
if (!contextLimit || contextLimit <= 0) {
|
|
@@ -1139,6 +1238,9 @@ var Agent = class {
|
|
|
1139
1238
|
if (compacted && compactedMessages) {
|
|
1140
1239
|
this.history.replaceViewMessages(compactedMessages);
|
|
1141
1240
|
}
|
|
1241
|
+
if (compacted || options.force) {
|
|
1242
|
+
await this.llm.onHistoryCompacted?.(invokeContext);
|
|
1243
|
+
}
|
|
1142
1244
|
const completeEvent = {
|
|
1143
1245
|
type: "compaction_complete",
|
|
1144
1246
|
timestamp: Date.now(),
|
|
@@ -2056,7 +2158,7 @@ var toUsage = (response) => {
|
|
|
2056
2158
|
total_tokens: inputTokens + outputTokens
|
|
2057
2159
|
};
|
|
2058
2160
|
};
|
|
2059
|
-
var toChatInvokeCompletion = (response) => {
|
|
2161
|
+
var toChatInvokeCompletion = (response, meta) => {
|
|
2060
2162
|
const blocks = response.content ?? [];
|
|
2061
2163
|
const messages = [];
|
|
2062
2164
|
for (const block of blocks) {
|
|
@@ -2126,7 +2228,11 @@ var toChatInvokeCompletion = (response) => {
|
|
|
2126
2228
|
provider_meta: {
|
|
2127
2229
|
response_id: response.id,
|
|
2128
2230
|
model: response.model,
|
|
2129
|
-
raw_output_text: stringifyUnknown2(extractText(blocks))
|
|
2231
|
+
raw_output_text: stringifyUnknown2(extractText(blocks)),
|
|
2232
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
2233
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
2234
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {},
|
|
2235
|
+
...typeof meta?.reasoning_budget_preset === "string" ? { reasoning_budget_preset: meta.reasoning_budget_preset } : {}
|
|
2130
2236
|
}
|
|
2131
2237
|
};
|
|
2132
2238
|
};
|
|
@@ -2163,12 +2269,21 @@ var ChatAnthropic = class {
|
|
|
2163
2269
|
model;
|
|
2164
2270
|
client;
|
|
2165
2271
|
defaultMaxTokens;
|
|
2272
|
+
defaultInvokeOptions;
|
|
2273
|
+
reasoningLevelMeta;
|
|
2166
2274
|
debugInvokeSeq = 0;
|
|
2167
2275
|
lastDebugRequestPayload = null;
|
|
2168
2276
|
constructor(options = {}) {
|
|
2169
2277
|
this.client = options.client ?? new import_sdk.default(options.clientOptions);
|
|
2170
2278
|
this.model = options.model ?? DEFAULT_MODEL;
|
|
2171
2279
|
this.defaultMaxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
2280
|
+
this.defaultInvokeOptions = { ...options.invokeOptions ?? {} };
|
|
2281
|
+
this.reasoningLevelMeta = {
|
|
2282
|
+
requested: options.reasoningLevelRequested,
|
|
2283
|
+
applied: options.reasoningLevelApplied,
|
|
2284
|
+
fallbackApplied: options.reasoningFallbackApplied,
|
|
2285
|
+
budgetPreset: options.reasoningBudgetPreset
|
|
2286
|
+
};
|
|
2172
2287
|
}
|
|
2173
2288
|
async ainvoke(input, _context) {
|
|
2174
2289
|
const {
|
|
@@ -2183,7 +2298,11 @@ var ChatAnthropic = class {
|
|
|
2183
2298
|
const enableTools = toolChoice !== "none";
|
|
2184
2299
|
const tools = enableTools ? toAnthropicTools(toolDefs) : void 0;
|
|
2185
2300
|
const tool_choice = enableTools ? toAnthropicToolChoice(toolChoice) : void 0;
|
|
2186
|
-
const
|
|
2301
|
+
const resolvedOptions = {
|
|
2302
|
+
...this.defaultInvokeOptions,
|
|
2303
|
+
...options ?? {}
|
|
2304
|
+
};
|
|
2305
|
+
const { max_tokens, ...rest } = resolvedOptions;
|
|
2187
2306
|
const request = {
|
|
2188
2307
|
model: model ?? this.model,
|
|
2189
2308
|
max_tokens: max_tokens ?? this.defaultMaxTokens,
|
|
@@ -2205,7 +2324,12 @@ var ChatAnthropic = class {
|
|
|
2205
2324
|
} : void 0
|
|
2206
2325
|
);
|
|
2207
2326
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
2208
|
-
return toChatInvokeCompletion(response
|
|
2327
|
+
return toChatInvokeCompletion(response, {
|
|
2328
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
2329
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
2330
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied,
|
|
2331
|
+
reasoning_budget_preset: this.reasoningLevelMeta.budgetPreset
|
|
2332
|
+
});
|
|
2209
2333
|
}
|
|
2210
2334
|
nextDebugInvokeSeq() {
|
|
2211
2335
|
const seq = this.debugInvokeSeq + 1;
|
|
@@ -2293,6 +2417,25 @@ var ChatAnthropic = class {
|
|
|
2293
2417
|
var import_node_crypto2 = require("crypto");
|
|
2294
2418
|
var import_openai3 = __toESM(require("openai"), 1);
|
|
2295
2419
|
|
|
2420
|
+
// src/llm/openai/http-transport.ts
|
|
2421
|
+
var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
2422
|
+
const streamRequest = {
|
|
2423
|
+
...request,
|
|
2424
|
+
stream: true
|
|
2425
|
+
};
|
|
2426
|
+
return client.responses.stream(
|
|
2427
|
+
streamRequest,
|
|
2428
|
+
signal || sessionIdHeader ? {
|
|
2429
|
+
...signal ? { signal } : {},
|
|
2430
|
+
...sessionIdHeader ? {
|
|
2431
|
+
headers: {
|
|
2432
|
+
session_id: sessionIdHeader
|
|
2433
|
+
}
|
|
2434
|
+
} : {}
|
|
2435
|
+
} : void 0
|
|
2436
|
+
).finalResponse();
|
|
2437
|
+
};
|
|
2438
|
+
|
|
2296
2439
|
// src/llm/openai/response-utils.ts
|
|
2297
2440
|
var isRecord2 = (value) => typeof value === "object" && value !== null;
|
|
2298
2441
|
var stringifyUnknown3 = (value) => {
|
|
@@ -2682,7 +2825,10 @@ function toChatInvokeCompletion2(response, meta) {
|
|
|
2682
2825
|
...typeof meta?.fallback_used === "boolean" ? { fallback_used: meta.fallback_used } : {},
|
|
2683
2826
|
...typeof meta?.chain_reset === "boolean" ? { chain_reset: meta.chain_reset } : {},
|
|
2684
2827
|
...typeof meta?.ws_reconnect_count === "number" ? { ws_reconnect_count: meta.ws_reconnect_count } : {},
|
|
2685
|
-
...typeof meta?.ws_input_mode === "string" ? { ws_input_mode: meta.ws_input_mode } : {}
|
|
2828
|
+
...typeof meta?.ws_input_mode === "string" ? { ws_input_mode: meta.ws_input_mode } : {},
|
|
2829
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
2830
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
2831
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {}
|
|
2686
2832
|
}
|
|
2687
2833
|
};
|
|
2688
2834
|
}
|
|
@@ -2754,29 +2900,11 @@ function toOpenAiHostedSearchTool(tool) {
|
|
|
2754
2900
|
};
|
|
2755
2901
|
}
|
|
2756
2902
|
|
|
2757
|
-
// src/llm/openai/http-transport.ts
|
|
2758
|
-
var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
2759
|
-
const streamRequest = {
|
|
2760
|
-
...request,
|
|
2761
|
-
stream: true
|
|
2762
|
-
};
|
|
2763
|
-
return client.responses.stream(
|
|
2764
|
-
streamRequest,
|
|
2765
|
-
signal || sessionIdHeader ? {
|
|
2766
|
-
...signal ? { signal } : {},
|
|
2767
|
-
...sessionIdHeader ? {
|
|
2768
|
-
headers: {
|
|
2769
|
-
session_id: sessionIdHeader
|
|
2770
|
-
}
|
|
2771
|
-
} : {}
|
|
2772
|
-
} : void 0
|
|
2773
|
-
).finalResponse();
|
|
2774
|
-
};
|
|
2775
|
-
|
|
2776
2903
|
// src/llm/openai/websocket-transport.ts
|
|
2777
2904
|
var import_ws = require("openai/resources/responses/ws");
|
|
2778
2905
|
var WS_CONNECT_TIMEOUT_MS = 2e4;
|
|
2779
2906
|
var WS_RESPONSE_IDLE_TIMEOUT_MS = 3e5;
|
|
2907
|
+
var WS_TERMINAL_DONE_GRACE_MS = 120;
|
|
2780
2908
|
var WS_UNEXPECTED_RESPONSE_BODY_TIMEOUT_MS = 250;
|
|
2781
2909
|
var WS_UNEXPECTED_RESPONSE_BODY_MAX_CHARS = 2e3;
|
|
2782
2910
|
var OPENAI_BETA_HEADER = "OpenAI-Beta";
|
|
@@ -2834,140 +2962,26 @@ var OpenAiWsTransport = class {
|
|
|
2834
2962
|
}
|
|
2835
2963
|
try {
|
|
2836
2964
|
await this.waitForWsOpen(ws, args.signal);
|
|
2837
|
-
let settled = false;
|
|
2838
|
-
let rejectResponsePromise;
|
|
2839
2965
|
const closeWs = () => {
|
|
2840
2966
|
this.closeWithoutMaskingPrimaryError(ws, "done");
|
|
2841
2967
|
};
|
|
2842
|
-
const
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
let removeNativeError;
|
|
2847
|
-
let removeNativeMessage;
|
|
2848
|
-
let timeout;
|
|
2849
|
-
const resetResponseTimeout = () => {
|
|
2850
|
-
if (settled) return;
|
|
2851
|
-
if (timeout) {
|
|
2852
|
-
clearTimeout(timeout);
|
|
2853
|
-
}
|
|
2854
|
-
timeout = setTimeout(() => {
|
|
2855
|
-
if (settled) return;
|
|
2856
|
-
settled = true;
|
|
2857
|
-
teardown();
|
|
2858
|
-
reject(new Error("openai websocket response timeout"));
|
|
2859
|
-
}, this.wsResponseIdleTimeoutMs);
|
|
2860
|
-
};
|
|
2861
|
-
const teardown = () => {
|
|
2862
|
-
if (ws.off) {
|
|
2863
|
-
ws.off("error", onError);
|
|
2864
|
-
ws.off("event", onEvent);
|
|
2865
|
-
ws.off("response.failed", onResponseFailed);
|
|
2866
|
-
ws.off("response.completed", onResponseCompleted);
|
|
2867
|
-
ws.off("close", onClose);
|
|
2868
|
-
}
|
|
2869
|
-
if (timeout) {
|
|
2870
|
-
clearTimeout(timeout);
|
|
2871
|
-
}
|
|
2872
|
-
removeNativeClose?.();
|
|
2873
|
-
removeNativeError?.();
|
|
2874
|
-
removeNativeMessage?.();
|
|
2875
|
-
if (args.signal && onAbort) {
|
|
2876
|
-
args.signal.removeEventListener("abort", onAbort);
|
|
2877
|
-
}
|
|
2878
|
-
};
|
|
2879
|
-
const resolveOnce = (response2) => {
|
|
2880
|
-
if (settled) return;
|
|
2881
|
-
settled = true;
|
|
2882
|
-
teardown();
|
|
2883
|
-
resolve(response2);
|
|
2884
|
-
};
|
|
2885
|
-
const rejectOnce = (error) => {
|
|
2886
|
-
if (settled) return;
|
|
2887
|
-
settled = true;
|
|
2888
|
-
teardown();
|
|
2889
|
-
reject(error);
|
|
2890
|
-
};
|
|
2891
|
-
rejectResponsePromise = rejectOnce;
|
|
2892
|
-
const onError = (error) => {
|
|
2893
|
-
rejectOnce(error);
|
|
2894
|
-
};
|
|
2895
|
-
const onEvent = () => {
|
|
2896
|
-
resetResponseTimeout();
|
|
2897
|
-
};
|
|
2898
|
-
const onResponseFailed = (event) => {
|
|
2899
|
-
const failed = event;
|
|
2900
|
-
const message = failed.response?.error?.message;
|
|
2901
|
-
const code = failed.response?.error?.code ?? void 0;
|
|
2902
|
-
rejectOnce(
|
|
2903
|
-
new WsResponseError(
|
|
2904
|
-
message || "openai websocket response failed",
|
|
2905
|
-
typeof code === "string" ? code : void 0
|
|
2906
|
-
)
|
|
2907
|
-
);
|
|
2908
|
-
};
|
|
2909
|
-
const onResponseCompleted = (event) => {
|
|
2910
|
-
const completed = event;
|
|
2911
|
-
resolveOnce(completed.response);
|
|
2912
|
-
};
|
|
2913
|
-
const onClose = (event) => {
|
|
2914
|
-
const closeCode = this.extractWsCloseCode(event);
|
|
2915
|
-
rejectOnce(
|
|
2916
|
-
new Error(
|
|
2917
|
-
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
2918
|
-
)
|
|
2919
|
-
);
|
|
2920
|
-
};
|
|
2921
|
-
ws.on("error", onError);
|
|
2922
|
-
ws.on("event", onEvent);
|
|
2923
|
-
ws.on("response.failed", onResponseFailed);
|
|
2924
|
-
ws.on("response.completed", onResponseCompleted);
|
|
2925
|
-
ws.on("close", onClose);
|
|
2926
|
-
removeNativeClose = this.addNativeSocketListener(
|
|
2927
|
-
nativeSocket,
|
|
2928
|
-
"close",
|
|
2929
|
-
(...events) => {
|
|
2930
|
-
onClose(events[0]);
|
|
2931
|
-
}
|
|
2932
|
-
);
|
|
2933
|
-
removeNativeError = this.addNativeSocketListener(
|
|
2934
|
-
nativeSocket,
|
|
2935
|
-
"error",
|
|
2936
|
-
(error) => {
|
|
2937
|
-
onError(error);
|
|
2938
|
-
}
|
|
2939
|
-
);
|
|
2940
|
-
removeNativeMessage = this.addNativeSocketListener(
|
|
2941
|
-
nativeSocket,
|
|
2942
|
-
"message",
|
|
2943
|
-
() => {
|
|
2944
|
-
resetResponseTimeout();
|
|
2945
|
-
}
|
|
2946
|
-
);
|
|
2947
|
-
if (args.signal) {
|
|
2948
|
-
onAbort = () => {
|
|
2949
|
-
rejectOnce(new Error("openai websocket request aborted"));
|
|
2950
|
-
closeWs();
|
|
2951
|
-
};
|
|
2952
|
-
args.signal.addEventListener("abort", onAbort, { once: true });
|
|
2953
|
-
if (args.signal.aborted) {
|
|
2954
|
-
onAbort();
|
|
2955
|
-
}
|
|
2956
|
-
}
|
|
2957
|
-
resetResponseTimeout();
|
|
2968
|
+
const responseWaiter = this.createResponsePromise({
|
|
2969
|
+
ws,
|
|
2970
|
+
signal: args.signal,
|
|
2971
|
+
closeWs
|
|
2958
2972
|
});
|
|
2959
2973
|
const responseCreateEvent = {
|
|
2960
2974
|
type: "response.create",
|
|
2961
2975
|
...args.request
|
|
2962
2976
|
};
|
|
2963
|
-
if (!
|
|
2977
|
+
if (!responseWaiter.isSettled()) {
|
|
2964
2978
|
try {
|
|
2965
2979
|
ws.send(responseCreateEvent);
|
|
2966
2980
|
} catch (error) {
|
|
2967
|
-
rejectResponsePromise
|
|
2981
|
+
responseWaiter.rejectResponsePromise(error);
|
|
2968
2982
|
}
|
|
2969
2983
|
}
|
|
2970
|
-
const response = await responsePromise;
|
|
2984
|
+
const response = await responseWaiter.responsePromise;
|
|
2971
2985
|
return { response, ws };
|
|
2972
2986
|
} catch (error) {
|
|
2973
2987
|
if (ownsWs) {
|
|
@@ -2976,6 +2990,221 @@ var OpenAiWsTransport = class {
|
|
|
2976
2990
|
throw error;
|
|
2977
2991
|
}
|
|
2978
2992
|
}
|
|
2993
|
+
createResponsePromise(args) {
|
|
2994
|
+
const { ws, signal, closeWs } = args;
|
|
2995
|
+
let settled = false;
|
|
2996
|
+
let rejectResponsePromise;
|
|
2997
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
2998
|
+
let onAbort;
|
|
2999
|
+
const nativeSocket = this.getNativeSocket(ws);
|
|
3000
|
+
let removeNativeClose;
|
|
3001
|
+
let removeNativeError;
|
|
3002
|
+
let removeNativeMessage;
|
|
3003
|
+
let timeout;
|
|
3004
|
+
let terminalDoneGraceTimer;
|
|
3005
|
+
let terminalCompletedWithoutUsage = null;
|
|
3006
|
+
const clearDoneGraceTimer = () => {
|
|
3007
|
+
if (terminalDoneGraceTimer) {
|
|
3008
|
+
clearTimeout(terminalDoneGraceTimer);
|
|
3009
|
+
terminalDoneGraceTimer = void 0;
|
|
3010
|
+
}
|
|
3011
|
+
};
|
|
3012
|
+
const teardown = () => {
|
|
3013
|
+
if (ws.off) {
|
|
3014
|
+
ws.off("error", onError);
|
|
3015
|
+
ws.off("event", onEvent);
|
|
3016
|
+
ws.off("response.failed", onResponseFailed);
|
|
3017
|
+
ws.off("response.completed", onResponseCompleted);
|
|
3018
|
+
ws.off("response.done", onResponseDone);
|
|
3019
|
+
ws.off("close", onClose);
|
|
3020
|
+
}
|
|
3021
|
+
if (timeout) {
|
|
3022
|
+
clearTimeout(timeout);
|
|
3023
|
+
}
|
|
3024
|
+
clearDoneGraceTimer();
|
|
3025
|
+
removeNativeClose?.();
|
|
3026
|
+
removeNativeError?.();
|
|
3027
|
+
removeNativeMessage?.();
|
|
3028
|
+
if (signal && onAbort) {
|
|
3029
|
+
signal.removeEventListener("abort", onAbort);
|
|
3030
|
+
}
|
|
3031
|
+
};
|
|
3032
|
+
const resolveOnce = (response) => {
|
|
3033
|
+
if (settled) return;
|
|
3034
|
+
settled = true;
|
|
3035
|
+
teardown();
|
|
3036
|
+
resolve(response);
|
|
3037
|
+
};
|
|
3038
|
+
const rejectOnce = (error) => {
|
|
3039
|
+
if (settled) return;
|
|
3040
|
+
settled = true;
|
|
3041
|
+
teardown();
|
|
3042
|
+
reject(error);
|
|
3043
|
+
};
|
|
3044
|
+
const scheduleDoneGraceTimer = () => {
|
|
3045
|
+
if (terminalDoneGraceTimer || settled) {
|
|
3046
|
+
return;
|
|
3047
|
+
}
|
|
3048
|
+
terminalDoneGraceTimer = setTimeout(() => {
|
|
3049
|
+
terminalDoneGraceTimer = void 0;
|
|
3050
|
+
if (settled || !terminalCompletedWithoutUsage) {
|
|
3051
|
+
return;
|
|
3052
|
+
}
|
|
3053
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3054
|
+
}, WS_TERMINAL_DONE_GRACE_MS);
|
|
3055
|
+
};
|
|
3056
|
+
const resetResponseTimeout = () => {
|
|
3057
|
+
if (settled) return;
|
|
3058
|
+
if (timeout) {
|
|
3059
|
+
clearTimeout(timeout);
|
|
3060
|
+
}
|
|
3061
|
+
timeout = setTimeout(() => {
|
|
3062
|
+
if (settled) return;
|
|
3063
|
+
if (terminalCompletedWithoutUsage) {
|
|
3064
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3065
|
+
return;
|
|
3066
|
+
}
|
|
3067
|
+
settled = true;
|
|
3068
|
+
teardown();
|
|
3069
|
+
reject(new Error("openai websocket response timeout"));
|
|
3070
|
+
}, this.wsResponseIdleTimeoutMs);
|
|
3071
|
+
};
|
|
3072
|
+
rejectResponsePromise = rejectOnce;
|
|
3073
|
+
const onError = (error) => {
|
|
3074
|
+
rejectOnce(error);
|
|
3075
|
+
};
|
|
3076
|
+
const resolveFromTerminalEvent = (event, type) => {
|
|
3077
|
+
const response = event?.response;
|
|
3078
|
+
if (response && typeof response === "object") {
|
|
3079
|
+
if (type === "response.completed" && !this.hasUsageTotals(response)) {
|
|
3080
|
+
terminalCompletedWithoutUsage = response;
|
|
3081
|
+
scheduleDoneGraceTimer();
|
|
3082
|
+
return;
|
|
3083
|
+
}
|
|
3084
|
+
clearDoneGraceTimer();
|
|
3085
|
+
resolveOnce(response);
|
|
3086
|
+
return;
|
|
3087
|
+
}
|
|
3088
|
+
if (type === "response.done" && terminalCompletedWithoutUsage) {
|
|
3089
|
+
clearDoneGraceTimer();
|
|
3090
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3091
|
+
return;
|
|
3092
|
+
}
|
|
3093
|
+
rejectOnce(
|
|
3094
|
+
new Error(`openai websocket ${type} missing response payload`)
|
|
3095
|
+
);
|
|
3096
|
+
};
|
|
3097
|
+
const onEvent = (event) => {
|
|
3098
|
+
const eventType = this.extractResponsesEventType(event);
|
|
3099
|
+
if (!eventType) {
|
|
3100
|
+
return;
|
|
3101
|
+
}
|
|
3102
|
+
if (eventType === "response.failed") {
|
|
3103
|
+
onResponseFailed(event);
|
|
3104
|
+
return;
|
|
3105
|
+
}
|
|
3106
|
+
if (eventType === "response.completed") {
|
|
3107
|
+
onResponseCompleted(event);
|
|
3108
|
+
return;
|
|
3109
|
+
}
|
|
3110
|
+
if (eventType === "response.done") {
|
|
3111
|
+
onResponseDone(event);
|
|
3112
|
+
return;
|
|
3113
|
+
}
|
|
3114
|
+
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
3115
|
+
resetResponseTimeout();
|
|
3116
|
+
}
|
|
3117
|
+
};
|
|
3118
|
+
const onResponseFailed = (event) => {
|
|
3119
|
+
const failed = event;
|
|
3120
|
+
const message = failed.response?.error?.message;
|
|
3121
|
+
const code = failed.response?.error?.code ?? void 0;
|
|
3122
|
+
rejectOnce(
|
|
3123
|
+
new WsResponseError(
|
|
3124
|
+
message || "openai websocket response failed",
|
|
3125
|
+
typeof code === "string" ? code : void 0
|
|
3126
|
+
)
|
|
3127
|
+
);
|
|
3128
|
+
};
|
|
3129
|
+
const onResponseCompleted = (event) => {
|
|
3130
|
+
resolveFromTerminalEvent(event, "response.completed");
|
|
3131
|
+
};
|
|
3132
|
+
const onResponseDone = (event) => {
|
|
3133
|
+
resolveFromTerminalEvent(event, "response.done");
|
|
3134
|
+
};
|
|
3135
|
+
const onClose = (event) => {
|
|
3136
|
+
const closeCode = this.extractWsCloseCode(event);
|
|
3137
|
+
rejectOnce(
|
|
3138
|
+
new Error(
|
|
3139
|
+
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
3140
|
+
)
|
|
3141
|
+
);
|
|
3142
|
+
};
|
|
3143
|
+
ws.on("error", onError);
|
|
3144
|
+
ws.on("event", onEvent);
|
|
3145
|
+
ws.on("response.failed", onResponseFailed);
|
|
3146
|
+
ws.on("response.completed", onResponseCompleted);
|
|
3147
|
+
ws.on("response.done", onResponseDone);
|
|
3148
|
+
ws.on("close", onClose);
|
|
3149
|
+
removeNativeClose = this.addNativeSocketListener(
|
|
3150
|
+
nativeSocket,
|
|
3151
|
+
"close",
|
|
3152
|
+
(...events) => {
|
|
3153
|
+
onClose(events[0]);
|
|
3154
|
+
}
|
|
3155
|
+
);
|
|
3156
|
+
removeNativeError = this.addNativeSocketListener(
|
|
3157
|
+
nativeSocket,
|
|
3158
|
+
"error",
|
|
3159
|
+
(error) => {
|
|
3160
|
+
onError(error);
|
|
3161
|
+
}
|
|
3162
|
+
);
|
|
3163
|
+
removeNativeMessage = this.addNativeSocketListener(
|
|
3164
|
+
nativeSocket,
|
|
3165
|
+
"message",
|
|
3166
|
+
(...events) => {
|
|
3167
|
+
const eventType = this.extractNativeResponsesEventType(events[0]);
|
|
3168
|
+
if (!eventType) {
|
|
3169
|
+
return;
|
|
3170
|
+
}
|
|
3171
|
+
if (eventType === "response.failed") {
|
|
3172
|
+
onResponseFailed(events[0]);
|
|
3173
|
+
return;
|
|
3174
|
+
}
|
|
3175
|
+
if (eventType === "response.completed") {
|
|
3176
|
+
onResponseCompleted(events[0]);
|
|
3177
|
+
return;
|
|
3178
|
+
}
|
|
3179
|
+
if (eventType === "response.done") {
|
|
3180
|
+
onResponseDone(events[0]);
|
|
3181
|
+
return;
|
|
3182
|
+
}
|
|
3183
|
+
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
3184
|
+
resetResponseTimeout();
|
|
3185
|
+
}
|
|
3186
|
+
}
|
|
3187
|
+
);
|
|
3188
|
+
if (signal) {
|
|
3189
|
+
onAbort = () => {
|
|
3190
|
+
rejectOnce(new Error("openai websocket request aborted"));
|
|
3191
|
+
closeWs();
|
|
3192
|
+
};
|
|
3193
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
3194
|
+
if (signal.aborted) {
|
|
3195
|
+
onAbort();
|
|
3196
|
+
}
|
|
3197
|
+
}
|
|
3198
|
+
resetResponseTimeout();
|
|
3199
|
+
});
|
|
3200
|
+
return {
|
|
3201
|
+
responsePromise,
|
|
3202
|
+
isSettled: () => settled,
|
|
3203
|
+
rejectResponsePromise: (error) => {
|
|
3204
|
+
rejectResponsePromise?.(error);
|
|
3205
|
+
}
|
|
3206
|
+
};
|
|
3207
|
+
}
|
|
2979
3208
|
closeSafely(ws, reason) {
|
|
2980
3209
|
if (!ws) {
|
|
2981
3210
|
return;
|
|
@@ -3129,6 +3358,38 @@ var OpenAiWsTransport = class {
|
|
|
3129
3358
|
const message = maybeError.message.toLowerCase();
|
|
3130
3359
|
return message.includes("not open") || message.includes("already closed") || message.includes("was closed before") || message.includes("readystate") || message.includes("closing") || message.includes("closed");
|
|
3131
3360
|
}
|
|
3361
|
+
extractResponsesEventType(event) {
|
|
3362
|
+
if (!event || typeof event !== "object") {
|
|
3363
|
+
return void 0;
|
|
3364
|
+
}
|
|
3365
|
+
const maybeEvent = event;
|
|
3366
|
+
return typeof maybeEvent.type === "string" ? maybeEvent.type : void 0;
|
|
3367
|
+
}
|
|
3368
|
+
shouldExtendResponseIdleTimeout(eventType) {
|
|
3369
|
+
if (!eventType.startsWith("response.")) {
|
|
3370
|
+
return false;
|
|
3371
|
+
}
|
|
3372
|
+
return eventType !== "response.failed" && eventType !== "response.completed" && eventType !== "response.done";
|
|
3373
|
+
}
|
|
3374
|
+
hasUsageTotals(response) {
|
|
3375
|
+
return typeof response.usage?.input_tokens === "number" && typeof response.usage?.output_tokens === "number" && typeof response.usage?.total_tokens === "number";
|
|
3376
|
+
}
|
|
3377
|
+
extractNativeResponsesEventType(message) {
|
|
3378
|
+
const directType = this.extractResponsesEventType(message);
|
|
3379
|
+
if (directType) {
|
|
3380
|
+
return directType;
|
|
3381
|
+
}
|
|
3382
|
+
const payloadText = this.toUnexpectedResponseChunkText(message).trim();
|
|
3383
|
+
if (!payloadText) {
|
|
3384
|
+
return void 0;
|
|
3385
|
+
}
|
|
3386
|
+
try {
|
|
3387
|
+
const parsed = JSON.parse(payloadText);
|
|
3388
|
+
return this.extractResponsesEventType(parsed);
|
|
3389
|
+
} catch {
|
|
3390
|
+
return void 0;
|
|
3391
|
+
}
|
|
3392
|
+
}
|
|
3132
3393
|
formatUnexpectedResponseHeaders(headers) {
|
|
3133
3394
|
if (!headers || typeof headers !== "object") {
|
|
3134
3395
|
return void 0;
|
|
@@ -3301,16 +3562,13 @@ var OpenAiWsTransport = class {
|
|
|
3301
3562
|
addSocketListener("error", (error) => {
|
|
3302
3563
|
settleReject(error);
|
|
3303
3564
|
});
|
|
3304
|
-
addSocketListener(
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
});
|
|
3312
|
-
}
|
|
3313
|
-
);
|
|
3565
|
+
addSocketListener("unexpected-response", (...args) => {
|
|
3566
|
+
void this.createUnexpectedResponseError(args[1]).then((error) => {
|
|
3567
|
+
settleReject(error);
|
|
3568
|
+
}).catch((error) => {
|
|
3569
|
+
settleReject(error);
|
|
3570
|
+
});
|
|
3571
|
+
});
|
|
3314
3572
|
addSocketListener("close", (code) => {
|
|
3315
3573
|
const closeCode = typeof code === "number" ? ` code=${code}` : "";
|
|
3316
3574
|
settleReject(
|
|
@@ -3349,6 +3607,7 @@ var ChatOpenAI = class {
|
|
|
3349
3607
|
model;
|
|
3350
3608
|
client;
|
|
3351
3609
|
defaultReasoningEffort;
|
|
3610
|
+
reasoningLevelMeta;
|
|
3352
3611
|
defaultTextVerbosity;
|
|
3353
3612
|
websocketMode;
|
|
3354
3613
|
websocketApiVersion;
|
|
@@ -3362,6 +3621,11 @@ var ChatOpenAI = class {
|
|
|
3362
3621
|
this.client = options.client ?? new import_openai3.default(options.clientOptions);
|
|
3363
3622
|
this.model = options.model ?? DEFAULT_MODEL2;
|
|
3364
3623
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT;
|
|
3624
|
+
this.reasoningLevelMeta = {
|
|
3625
|
+
requested: options.reasoningLevelRequested,
|
|
3626
|
+
applied: options.reasoningLevelApplied,
|
|
3627
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
3628
|
+
};
|
|
3365
3629
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
3366
3630
|
this.websocketMode = options.websocketMode ?? "off";
|
|
3367
3631
|
this.websocketApiVersion = options.websocketApiVersion ?? "v2";
|
|
@@ -3448,9 +3712,24 @@ var ChatOpenAI = class {
|
|
|
3448
3712
|
fallback_used: transportResult.fallbackUsed,
|
|
3449
3713
|
chain_reset: transportResult.chainReset,
|
|
3450
3714
|
ws_reconnect_count: this.wsReconnectCount,
|
|
3451
|
-
ws_input_mode: transportResult.wsInputMode
|
|
3715
|
+
ws_input_mode: transportResult.wsInputMode,
|
|
3716
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
3717
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
3718
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
3452
3719
|
});
|
|
3453
3720
|
}
|
|
3721
|
+
onHistoryCompacted(context) {
|
|
3722
|
+
const sessionKey = context?.sessionKey;
|
|
3723
|
+
if (sessionKey) {
|
|
3724
|
+
this.clearWsSessionState(sessionKey, "history_compacted");
|
|
3725
|
+
this.wsDisabledUntilBySessionKey.delete(sessionKey);
|
|
3726
|
+
return;
|
|
3727
|
+
}
|
|
3728
|
+
for (const key of Array.from(this.wsStateBySessionKey.keys())) {
|
|
3729
|
+
this.clearWsSessionState(key, "history_compacted");
|
|
3730
|
+
}
|
|
3731
|
+
this.wsDisabledUntilBySessionKey.clear();
|
|
3732
|
+
}
|
|
3454
3733
|
async invokeWithTransport(args) {
|
|
3455
3734
|
this.evictIdleWsSessionState();
|
|
3456
3735
|
if (this.websocketMode === "off") {
|
|
@@ -3498,7 +3777,7 @@ var ChatOpenAI = class {
|
|
|
3498
3777
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3499
3778
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3500
3779
|
let wsInputMode = "full_no_previous";
|
|
3501
|
-
if (
|
|
3780
|
+
if (wsState.previousResponseId) {
|
|
3502
3781
|
if (canUsePreviousResponseId) {
|
|
3503
3782
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3504
3783
|
wsInputMode = "empty";
|
|
@@ -3560,10 +3839,12 @@ var ChatOpenAI = class {
|
|
|
3560
3839
|
} catch (error) {
|
|
3561
3840
|
let failure = error;
|
|
3562
3841
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3563
|
-
const retryWsInputMode =
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3842
|
+
const retryWsInputMode = wsState.previousResponseId ? "full_regenerated" : "full_no_previous";
|
|
3843
|
+
this.closeWsWithoutMaskingPrimaryError(
|
|
3844
|
+
wsState.ws,
|
|
3845
|
+
"retry_reset",
|
|
3846
|
+
failure
|
|
3847
|
+
);
|
|
3567
3848
|
try {
|
|
3568
3849
|
await this.debugRequestIfEnabled(
|
|
3569
3850
|
args.request,
|
|
@@ -3663,7 +3944,9 @@ var ChatOpenAI = class {
|
|
|
3663
3944
|
if (!Array.isArray(requestInput)) {
|
|
3664
3945
|
return requestInput;
|
|
3665
3946
|
}
|
|
3666
|
-
const replayItems = toResponsesInput(
|
|
3947
|
+
const replayItems = toResponsesInput(
|
|
3948
|
+
toChatInvokeCompletion2(response).messages
|
|
3949
|
+
);
|
|
3667
3950
|
if (replayItems.length === 0) {
|
|
3668
3951
|
return requestInput;
|
|
3669
3952
|
}
|
|
@@ -4189,7 +4472,7 @@ function toResponsesToolChoice2(choice) {
|
|
|
4189
4472
|
}
|
|
4190
4473
|
return { type: "function", name: choice };
|
|
4191
4474
|
}
|
|
4192
|
-
function toChatInvokeCompletion3(response) {
|
|
4475
|
+
function toChatInvokeCompletion3(response, meta) {
|
|
4193
4476
|
const usage = response.usage ? {
|
|
4194
4477
|
model: response.model,
|
|
4195
4478
|
input_tokens: response.usage.input_tokens,
|
|
@@ -4209,7 +4492,10 @@ function toChatInvokeCompletion3(response) {
|
|
|
4209
4492
|
usage,
|
|
4210
4493
|
stop_reason: response.incomplete_details?.reason ?? response.status ?? null,
|
|
4211
4494
|
provider_meta: {
|
|
4212
|
-
response_id: response.id
|
|
4495
|
+
response_id: response.id,
|
|
4496
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
4497
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
4498
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {}
|
|
4213
4499
|
}
|
|
4214
4500
|
};
|
|
4215
4501
|
}
|
|
@@ -4295,6 +4581,7 @@ var ChatOpenRouter = class {
|
|
|
4295
4581
|
model;
|
|
4296
4582
|
client;
|
|
4297
4583
|
defaultReasoningEffort;
|
|
4584
|
+
reasoningLevelMeta;
|
|
4298
4585
|
defaultTextVerbosity;
|
|
4299
4586
|
debugInvokeSeq = 0;
|
|
4300
4587
|
lastDebugRequestPayload = null;
|
|
@@ -4305,6 +4592,11 @@ var ChatOpenRouter = class {
|
|
|
4305
4592
|
});
|
|
4306
4593
|
this.model = options.model ?? DEFAULT_MODEL3;
|
|
4307
4594
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT2;
|
|
4595
|
+
this.reasoningLevelMeta = {
|
|
4596
|
+
requested: options.reasoningLevelRequested,
|
|
4597
|
+
applied: options.reasoningLevelApplied,
|
|
4598
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
4599
|
+
};
|
|
4308
4600
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
4309
4601
|
}
|
|
4310
4602
|
async ainvoke(input, context) {
|
|
@@ -4375,7 +4667,11 @@ var ChatOpenRouter = class {
|
|
|
4375
4667
|
} : void 0
|
|
4376
4668
|
).finalResponse();
|
|
4377
4669
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
4378
|
-
return toChatInvokeCompletion3(response
|
|
4670
|
+
return toChatInvokeCompletion3(response, {
|
|
4671
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
4672
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
4673
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
4674
|
+
});
|
|
4379
4675
|
}
|
|
4380
4676
|
nextDebugInvokeSeq() {
|
|
4381
4677
|
const seq = this.debugInvokeSeq + 1;
|