@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.js
CHANGED
|
@@ -200,6 +200,10 @@ var ANTHROPIC_MODELS = [
|
|
|
200
200
|
id: "claude-opus-4-5-20251201",
|
|
201
201
|
provider: "anthropic"
|
|
202
202
|
},
|
|
203
|
+
{
|
|
204
|
+
id: "claude-sonnet-4-6",
|
|
205
|
+
provider: "anthropic"
|
|
206
|
+
},
|
|
203
207
|
{
|
|
204
208
|
id: "claude-sonnet-4-5-20250929",
|
|
205
209
|
provider: "anthropic"
|
|
@@ -324,10 +328,12 @@ var DEFAULT_MODEL_REGISTRY = createModelRegistry([
|
|
|
324
328
|
]);
|
|
325
329
|
|
|
326
330
|
// src/services/compaction/service.ts
|
|
327
|
-
var DEFAULT_THRESHOLD_RATIO = 0.
|
|
331
|
+
var DEFAULT_THRESHOLD_RATIO = 0.85;
|
|
328
332
|
var DEFAULT_RETAIN_LAST_TURNS = 1;
|
|
329
333
|
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.";
|
|
330
334
|
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.";
|
|
335
|
+
var COMPACTION_RETAIN_PREFIX = "[codelia.compaction.retain]\n";
|
|
336
|
+
var COMPACTION_SUMMARY_PREFIX = "[codelia.compaction.summary]\n";
|
|
331
337
|
var CompactionService = class _CompactionService {
|
|
332
338
|
config;
|
|
333
339
|
modelRegistry;
|
|
@@ -363,6 +369,13 @@ var CompactionService = class _CompactionService {
|
|
|
363
369
|
};
|
|
364
370
|
}
|
|
365
371
|
const preparedMessages = this.prepareMessagesForSummary(messages);
|
|
372
|
+
if (preparedMessages.length === 0) {
|
|
373
|
+
return {
|
|
374
|
+
compacted: false,
|
|
375
|
+
compactedMessages: messages,
|
|
376
|
+
usage: null
|
|
377
|
+
};
|
|
378
|
+
}
|
|
366
379
|
const prompt = this.buildCompactionPrompt();
|
|
367
380
|
const interruptMessage = {
|
|
368
381
|
role: "user",
|
|
@@ -414,7 +427,10 @@ var CompactionService = class _CompactionService {
|
|
|
414
427
|
}
|
|
415
428
|
prepareMessagesForSummary(messages) {
|
|
416
429
|
if (messages.length === 0) return messages;
|
|
417
|
-
const prepared = messages.map((message) => ({ ...message }));
|
|
430
|
+
const prepared = messages.filter((message) => !isCompactionMemoryMessage(message)).map((message) => ({ ...message }));
|
|
431
|
+
if (prepared.length === 0) {
|
|
432
|
+
return prepared;
|
|
433
|
+
}
|
|
418
434
|
const last = prepared[prepared.length - 1];
|
|
419
435
|
if (last.role === "assistant" && last.tool_calls?.length) {
|
|
420
436
|
if (last.content) {
|
|
@@ -472,26 +488,73 @@ var CompactionService = class _CompactionService {
|
|
|
472
488
|
nonSystemMessages.push(message);
|
|
473
489
|
}
|
|
474
490
|
}
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
491
|
+
const existingMemory = this.collectCompactionMemory(nonSystemMessages);
|
|
492
|
+
const mergedRetain = mergeCompactionMemory(
|
|
493
|
+
existingMemory.retainEntries,
|
|
494
|
+
retain
|
|
495
|
+
);
|
|
496
|
+
const mergedSummary = summary.trim() || existingMemory.summaryEntries[existingMemory.summaryEntries.length - 1] || "";
|
|
497
|
+
const conversationMessages = nonSystemMessages.filter(
|
|
498
|
+
(message) => !isCompactionMemoryMessage(message)
|
|
499
|
+
);
|
|
500
|
+
const tail = this.normalizeRetainedTail(
|
|
501
|
+
this.getLastTurns(conversationMessages, this.config.retainLastTurns)
|
|
478
502
|
);
|
|
479
503
|
const compacted = [...systemMessages];
|
|
480
|
-
if (
|
|
504
|
+
if (mergedRetain) {
|
|
481
505
|
compacted.push({
|
|
482
506
|
role: "user",
|
|
483
|
-
content:
|
|
507
|
+
content: `${COMPACTION_RETAIN_PREFIX}${mergedRetain}`
|
|
484
508
|
});
|
|
485
509
|
}
|
|
486
|
-
if (
|
|
510
|
+
if (mergedSummary) {
|
|
487
511
|
compacted.push({
|
|
488
512
|
role: "user",
|
|
489
|
-
content:
|
|
513
|
+
content: `${COMPACTION_SUMMARY_PREFIX}${mergedSummary}`
|
|
490
514
|
});
|
|
491
515
|
}
|
|
492
516
|
compacted.push(...tail);
|
|
493
517
|
return compacted;
|
|
494
518
|
}
|
|
519
|
+
collectCompactionMemory(messages) {
|
|
520
|
+
const retainEntries = [];
|
|
521
|
+
const summaryEntries = [];
|
|
522
|
+
for (const message of messages) {
|
|
523
|
+
const parsed = parseCompactionMemory(message);
|
|
524
|
+
if (!parsed) {
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
if (parsed.kind === "retain") {
|
|
528
|
+
retainEntries.push(parsed.body);
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
summaryEntries.push(parsed.body);
|
|
532
|
+
}
|
|
533
|
+
return { retainEntries, summaryEntries };
|
|
534
|
+
}
|
|
535
|
+
normalizeRetainedTail(messages) {
|
|
536
|
+
const normalized = [];
|
|
537
|
+
for (const message of messages) {
|
|
538
|
+
if (message.role === "user") {
|
|
539
|
+
normalized.push(message);
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
if (message.role !== "assistant") {
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
const assistant = {
|
|
546
|
+
role: "assistant",
|
|
547
|
+
content: message.content,
|
|
548
|
+
...message.name ? { name: message.name } : {},
|
|
549
|
+
...message.refusal ? { refusal: message.refusal } : {}
|
|
550
|
+
};
|
|
551
|
+
if (assistant.content == null && assistant.refusal == null) {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
normalized.push(assistant);
|
|
555
|
+
}
|
|
556
|
+
return normalized;
|
|
557
|
+
}
|
|
495
558
|
getLastTurns(messages, retainLastTurns) {
|
|
496
559
|
if (retainLastTurns <= 0) return [];
|
|
497
560
|
let remaining = retainLastTurns;
|
|
@@ -582,6 +645,39 @@ var extractTag = (text, tag) => {
|
|
|
582
645
|
const match = text.match(regex);
|
|
583
646
|
return match?.[1]?.trim() ?? "";
|
|
584
647
|
};
|
|
648
|
+
var parseCompactionMemory = (message) => {
|
|
649
|
+
if (message.role !== "user" || typeof message.content !== "string") {
|
|
650
|
+
return null;
|
|
651
|
+
}
|
|
652
|
+
if (message.content.startsWith(COMPACTION_RETAIN_PREFIX)) {
|
|
653
|
+
return {
|
|
654
|
+
kind: "retain",
|
|
655
|
+
body: message.content.slice(COMPACTION_RETAIN_PREFIX.length).trim()
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
if (message.content.startsWith(COMPACTION_SUMMARY_PREFIX)) {
|
|
659
|
+
return {
|
|
660
|
+
kind: "summary",
|
|
661
|
+
body: message.content.slice(COMPACTION_SUMMARY_PREFIX.length).trim()
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
return null;
|
|
665
|
+
};
|
|
666
|
+
var isCompactionMemoryMessage = (message) => parseCompactionMemory(message) !== null;
|
|
667
|
+
var mergeCompactionMemory = (existing, next) => {
|
|
668
|
+
const ordered = [...existing, next];
|
|
669
|
+
const seen = /* @__PURE__ */ new Set();
|
|
670
|
+
const merged = [];
|
|
671
|
+
for (const entry of ordered) {
|
|
672
|
+
const normalized = entry.trim();
|
|
673
|
+
if (!normalized || seen.has(normalized)) {
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
seen.add(normalized);
|
|
677
|
+
merged.push(normalized);
|
|
678
|
+
}
|
|
679
|
+
return merged.join("\n\n");
|
|
680
|
+
};
|
|
585
681
|
var stripSnapshotSuffix = (modelId) => modelId.replace(/-[0-9]{4}-[0-9]{2}-[0-9]{2}$/, "");
|
|
586
682
|
var parseQualifiedModelId = (modelId) => {
|
|
587
683
|
const sep = modelId.indexOf("/");
|
|
@@ -1018,8 +1114,7 @@ var Agent = class {
|
|
|
1018
1114
|
store: this.services.toolOutputCacheStore ?? null
|
|
1019
1115
|
});
|
|
1020
1116
|
this.usageService = new TokenUsageService({
|
|
1021
|
-
enabled: options.enableUsageTracking ?? true
|
|
1022
|
-
thresholdRatio: 0.5
|
|
1117
|
+
enabled: options.enableUsageTracking ?? true
|
|
1023
1118
|
});
|
|
1024
1119
|
this.canExecuteTool = options.canExecuteTool;
|
|
1025
1120
|
}
|
|
@@ -1031,11 +1126,15 @@ var Agent = class {
|
|
|
1031
1126
|
if (!usage) {
|
|
1032
1127
|
return null;
|
|
1033
1128
|
}
|
|
1034
|
-
const
|
|
1035
|
-
const modelSpec = resolveModelWithQualifiedFallback2(
|
|
1129
|
+
const usageModelSpec = usage.model ? resolveModelWithQualifiedFallback2(
|
|
1036
1130
|
this.modelRegistry,
|
|
1037
1131
|
this.llm.provider,
|
|
1038
|
-
|
|
1132
|
+
usage.model
|
|
1133
|
+
) : void 0;
|
|
1134
|
+
const modelSpec = usageModelSpec ?? resolveModelWithQualifiedFallback2(
|
|
1135
|
+
this.modelRegistry,
|
|
1136
|
+
this.llm.provider,
|
|
1137
|
+
this.llm.model
|
|
1039
1138
|
);
|
|
1040
1139
|
const contextLimit = modelSpec?.maxInputTokens ?? modelSpec?.contextWindow ?? null;
|
|
1041
1140
|
if (!contextLimit || contextLimit <= 0) {
|
|
@@ -1082,6 +1181,9 @@ var Agent = class {
|
|
|
1082
1181
|
if (compacted && compactedMessages) {
|
|
1083
1182
|
this.history.replaceViewMessages(compactedMessages);
|
|
1084
1183
|
}
|
|
1184
|
+
if (compacted || options.force) {
|
|
1185
|
+
await this.llm.onHistoryCompacted?.(invokeContext);
|
|
1186
|
+
}
|
|
1085
1187
|
const completeEvent = {
|
|
1086
1188
|
type: "compaction_complete",
|
|
1087
1189
|
timestamp: Date.now(),
|
|
@@ -1999,7 +2101,7 @@ var toUsage = (response) => {
|
|
|
1999
2101
|
total_tokens: inputTokens + outputTokens
|
|
2000
2102
|
};
|
|
2001
2103
|
};
|
|
2002
|
-
var toChatInvokeCompletion = (response) => {
|
|
2104
|
+
var toChatInvokeCompletion = (response, meta) => {
|
|
2003
2105
|
const blocks = response.content ?? [];
|
|
2004
2106
|
const messages = [];
|
|
2005
2107
|
for (const block of blocks) {
|
|
@@ -2069,7 +2171,11 @@ var toChatInvokeCompletion = (response) => {
|
|
|
2069
2171
|
provider_meta: {
|
|
2070
2172
|
response_id: response.id,
|
|
2071
2173
|
model: response.model,
|
|
2072
|
-
raw_output_text: stringifyUnknown2(extractText(blocks))
|
|
2174
|
+
raw_output_text: stringifyUnknown2(extractText(blocks)),
|
|
2175
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
2176
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
2177
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {},
|
|
2178
|
+
...typeof meta?.reasoning_budget_preset === "string" ? { reasoning_budget_preset: meta.reasoning_budget_preset } : {}
|
|
2073
2179
|
}
|
|
2074
2180
|
};
|
|
2075
2181
|
};
|
|
@@ -2106,12 +2212,21 @@ var ChatAnthropic = class {
|
|
|
2106
2212
|
model;
|
|
2107
2213
|
client;
|
|
2108
2214
|
defaultMaxTokens;
|
|
2215
|
+
defaultInvokeOptions;
|
|
2216
|
+
reasoningLevelMeta;
|
|
2109
2217
|
debugInvokeSeq = 0;
|
|
2110
2218
|
lastDebugRequestPayload = null;
|
|
2111
2219
|
constructor(options = {}) {
|
|
2112
2220
|
this.client = options.client ?? new Anthropic(options.clientOptions);
|
|
2113
2221
|
this.model = options.model ?? DEFAULT_MODEL;
|
|
2114
2222
|
this.defaultMaxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
2223
|
+
this.defaultInvokeOptions = { ...options.invokeOptions ?? {} };
|
|
2224
|
+
this.reasoningLevelMeta = {
|
|
2225
|
+
requested: options.reasoningLevelRequested,
|
|
2226
|
+
applied: options.reasoningLevelApplied,
|
|
2227
|
+
fallbackApplied: options.reasoningFallbackApplied,
|
|
2228
|
+
budgetPreset: options.reasoningBudgetPreset
|
|
2229
|
+
};
|
|
2115
2230
|
}
|
|
2116
2231
|
async ainvoke(input, _context) {
|
|
2117
2232
|
const {
|
|
@@ -2126,7 +2241,11 @@ var ChatAnthropic = class {
|
|
|
2126
2241
|
const enableTools = toolChoice !== "none";
|
|
2127
2242
|
const tools = enableTools ? toAnthropicTools(toolDefs) : void 0;
|
|
2128
2243
|
const tool_choice = enableTools ? toAnthropicToolChoice(toolChoice) : void 0;
|
|
2129
|
-
const
|
|
2244
|
+
const resolvedOptions = {
|
|
2245
|
+
...this.defaultInvokeOptions,
|
|
2246
|
+
...options ?? {}
|
|
2247
|
+
};
|
|
2248
|
+
const { max_tokens, ...rest } = resolvedOptions;
|
|
2130
2249
|
const request = {
|
|
2131
2250
|
model: model ?? this.model,
|
|
2132
2251
|
max_tokens: max_tokens ?? this.defaultMaxTokens,
|
|
@@ -2148,7 +2267,12 @@ var ChatAnthropic = class {
|
|
|
2148
2267
|
} : void 0
|
|
2149
2268
|
);
|
|
2150
2269
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
2151
|
-
return toChatInvokeCompletion(response
|
|
2270
|
+
return toChatInvokeCompletion(response, {
|
|
2271
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
2272
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
2273
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied,
|
|
2274
|
+
reasoning_budget_preset: this.reasoningLevelMeta.budgetPreset
|
|
2275
|
+
});
|
|
2152
2276
|
}
|
|
2153
2277
|
nextDebugInvokeSeq() {
|
|
2154
2278
|
const seq = this.debugInvokeSeq + 1;
|
|
@@ -2236,6 +2360,25 @@ var ChatAnthropic = class {
|
|
|
2236
2360
|
import { createHash as createHash2 } from "crypto";
|
|
2237
2361
|
import OpenAI from "openai";
|
|
2238
2362
|
|
|
2363
|
+
// src/llm/openai/http-transport.ts
|
|
2364
|
+
var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
2365
|
+
const streamRequest = {
|
|
2366
|
+
...request,
|
|
2367
|
+
stream: true
|
|
2368
|
+
};
|
|
2369
|
+
return client.responses.stream(
|
|
2370
|
+
streamRequest,
|
|
2371
|
+
signal || sessionIdHeader ? {
|
|
2372
|
+
...signal ? { signal } : {},
|
|
2373
|
+
...sessionIdHeader ? {
|
|
2374
|
+
headers: {
|
|
2375
|
+
session_id: sessionIdHeader
|
|
2376
|
+
}
|
|
2377
|
+
} : {}
|
|
2378
|
+
} : void 0
|
|
2379
|
+
).finalResponse();
|
|
2380
|
+
};
|
|
2381
|
+
|
|
2239
2382
|
// src/llm/openai/response-utils.ts
|
|
2240
2383
|
var isRecord2 = (value) => typeof value === "object" && value !== null;
|
|
2241
2384
|
var stringifyUnknown3 = (value) => {
|
|
@@ -2625,7 +2768,10 @@ function toChatInvokeCompletion2(response, meta) {
|
|
|
2625
2768
|
...typeof meta?.fallback_used === "boolean" ? { fallback_used: meta.fallback_used } : {},
|
|
2626
2769
|
...typeof meta?.chain_reset === "boolean" ? { chain_reset: meta.chain_reset } : {},
|
|
2627
2770
|
...typeof meta?.ws_reconnect_count === "number" ? { ws_reconnect_count: meta.ws_reconnect_count } : {},
|
|
2628
|
-
...typeof meta?.ws_input_mode === "string" ? { ws_input_mode: meta.ws_input_mode } : {}
|
|
2771
|
+
...typeof meta?.ws_input_mode === "string" ? { ws_input_mode: meta.ws_input_mode } : {},
|
|
2772
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
2773
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
2774
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {}
|
|
2629
2775
|
}
|
|
2630
2776
|
};
|
|
2631
2777
|
}
|
|
@@ -2697,29 +2843,11 @@ function toOpenAiHostedSearchTool(tool) {
|
|
|
2697
2843
|
};
|
|
2698
2844
|
}
|
|
2699
2845
|
|
|
2700
|
-
// src/llm/openai/http-transport.ts
|
|
2701
|
-
var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
2702
|
-
const streamRequest = {
|
|
2703
|
-
...request,
|
|
2704
|
-
stream: true
|
|
2705
|
-
};
|
|
2706
|
-
return client.responses.stream(
|
|
2707
|
-
streamRequest,
|
|
2708
|
-
signal || sessionIdHeader ? {
|
|
2709
|
-
...signal ? { signal } : {},
|
|
2710
|
-
...sessionIdHeader ? {
|
|
2711
|
-
headers: {
|
|
2712
|
-
session_id: sessionIdHeader
|
|
2713
|
-
}
|
|
2714
|
-
} : {}
|
|
2715
|
-
} : void 0
|
|
2716
|
-
).finalResponse();
|
|
2717
|
-
};
|
|
2718
|
-
|
|
2719
2846
|
// src/llm/openai/websocket-transport.ts
|
|
2720
2847
|
import { ResponsesWS } from "openai/resources/responses/ws";
|
|
2721
2848
|
var WS_CONNECT_TIMEOUT_MS = 2e4;
|
|
2722
2849
|
var WS_RESPONSE_IDLE_TIMEOUT_MS = 3e5;
|
|
2850
|
+
var WS_TERMINAL_DONE_GRACE_MS = 120;
|
|
2723
2851
|
var WS_UNEXPECTED_RESPONSE_BODY_TIMEOUT_MS = 250;
|
|
2724
2852
|
var WS_UNEXPECTED_RESPONSE_BODY_MAX_CHARS = 2e3;
|
|
2725
2853
|
var OPENAI_BETA_HEADER = "OpenAI-Beta";
|
|
@@ -2777,140 +2905,26 @@ var OpenAiWsTransport = class {
|
|
|
2777
2905
|
}
|
|
2778
2906
|
try {
|
|
2779
2907
|
await this.waitForWsOpen(ws, args.signal);
|
|
2780
|
-
let settled = false;
|
|
2781
|
-
let rejectResponsePromise;
|
|
2782
2908
|
const closeWs = () => {
|
|
2783
2909
|
this.closeWithoutMaskingPrimaryError(ws, "done");
|
|
2784
2910
|
};
|
|
2785
|
-
const
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
let removeNativeError;
|
|
2790
|
-
let removeNativeMessage;
|
|
2791
|
-
let timeout;
|
|
2792
|
-
const resetResponseTimeout = () => {
|
|
2793
|
-
if (settled) return;
|
|
2794
|
-
if (timeout) {
|
|
2795
|
-
clearTimeout(timeout);
|
|
2796
|
-
}
|
|
2797
|
-
timeout = setTimeout(() => {
|
|
2798
|
-
if (settled) return;
|
|
2799
|
-
settled = true;
|
|
2800
|
-
teardown();
|
|
2801
|
-
reject(new Error("openai websocket response timeout"));
|
|
2802
|
-
}, this.wsResponseIdleTimeoutMs);
|
|
2803
|
-
};
|
|
2804
|
-
const teardown = () => {
|
|
2805
|
-
if (ws.off) {
|
|
2806
|
-
ws.off("error", onError);
|
|
2807
|
-
ws.off("event", onEvent);
|
|
2808
|
-
ws.off("response.failed", onResponseFailed);
|
|
2809
|
-
ws.off("response.completed", onResponseCompleted);
|
|
2810
|
-
ws.off("close", onClose);
|
|
2811
|
-
}
|
|
2812
|
-
if (timeout) {
|
|
2813
|
-
clearTimeout(timeout);
|
|
2814
|
-
}
|
|
2815
|
-
removeNativeClose?.();
|
|
2816
|
-
removeNativeError?.();
|
|
2817
|
-
removeNativeMessage?.();
|
|
2818
|
-
if (args.signal && onAbort) {
|
|
2819
|
-
args.signal.removeEventListener("abort", onAbort);
|
|
2820
|
-
}
|
|
2821
|
-
};
|
|
2822
|
-
const resolveOnce = (response2) => {
|
|
2823
|
-
if (settled) return;
|
|
2824
|
-
settled = true;
|
|
2825
|
-
teardown();
|
|
2826
|
-
resolve(response2);
|
|
2827
|
-
};
|
|
2828
|
-
const rejectOnce = (error) => {
|
|
2829
|
-
if (settled) return;
|
|
2830
|
-
settled = true;
|
|
2831
|
-
teardown();
|
|
2832
|
-
reject(error);
|
|
2833
|
-
};
|
|
2834
|
-
rejectResponsePromise = rejectOnce;
|
|
2835
|
-
const onError = (error) => {
|
|
2836
|
-
rejectOnce(error);
|
|
2837
|
-
};
|
|
2838
|
-
const onEvent = () => {
|
|
2839
|
-
resetResponseTimeout();
|
|
2840
|
-
};
|
|
2841
|
-
const onResponseFailed = (event) => {
|
|
2842
|
-
const failed = event;
|
|
2843
|
-
const message = failed.response?.error?.message;
|
|
2844
|
-
const code = failed.response?.error?.code ?? void 0;
|
|
2845
|
-
rejectOnce(
|
|
2846
|
-
new WsResponseError(
|
|
2847
|
-
message || "openai websocket response failed",
|
|
2848
|
-
typeof code === "string" ? code : void 0
|
|
2849
|
-
)
|
|
2850
|
-
);
|
|
2851
|
-
};
|
|
2852
|
-
const onResponseCompleted = (event) => {
|
|
2853
|
-
const completed = event;
|
|
2854
|
-
resolveOnce(completed.response);
|
|
2855
|
-
};
|
|
2856
|
-
const onClose = (event) => {
|
|
2857
|
-
const closeCode = this.extractWsCloseCode(event);
|
|
2858
|
-
rejectOnce(
|
|
2859
|
-
new Error(
|
|
2860
|
-
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
2861
|
-
)
|
|
2862
|
-
);
|
|
2863
|
-
};
|
|
2864
|
-
ws.on("error", onError);
|
|
2865
|
-
ws.on("event", onEvent);
|
|
2866
|
-
ws.on("response.failed", onResponseFailed);
|
|
2867
|
-
ws.on("response.completed", onResponseCompleted);
|
|
2868
|
-
ws.on("close", onClose);
|
|
2869
|
-
removeNativeClose = this.addNativeSocketListener(
|
|
2870
|
-
nativeSocket,
|
|
2871
|
-
"close",
|
|
2872
|
-
(...events) => {
|
|
2873
|
-
onClose(events[0]);
|
|
2874
|
-
}
|
|
2875
|
-
);
|
|
2876
|
-
removeNativeError = this.addNativeSocketListener(
|
|
2877
|
-
nativeSocket,
|
|
2878
|
-
"error",
|
|
2879
|
-
(error) => {
|
|
2880
|
-
onError(error);
|
|
2881
|
-
}
|
|
2882
|
-
);
|
|
2883
|
-
removeNativeMessage = this.addNativeSocketListener(
|
|
2884
|
-
nativeSocket,
|
|
2885
|
-
"message",
|
|
2886
|
-
() => {
|
|
2887
|
-
resetResponseTimeout();
|
|
2888
|
-
}
|
|
2889
|
-
);
|
|
2890
|
-
if (args.signal) {
|
|
2891
|
-
onAbort = () => {
|
|
2892
|
-
rejectOnce(new Error("openai websocket request aborted"));
|
|
2893
|
-
closeWs();
|
|
2894
|
-
};
|
|
2895
|
-
args.signal.addEventListener("abort", onAbort, { once: true });
|
|
2896
|
-
if (args.signal.aborted) {
|
|
2897
|
-
onAbort();
|
|
2898
|
-
}
|
|
2899
|
-
}
|
|
2900
|
-
resetResponseTimeout();
|
|
2911
|
+
const responseWaiter = this.createResponsePromise({
|
|
2912
|
+
ws,
|
|
2913
|
+
signal: args.signal,
|
|
2914
|
+
closeWs
|
|
2901
2915
|
});
|
|
2902
2916
|
const responseCreateEvent = {
|
|
2903
2917
|
type: "response.create",
|
|
2904
2918
|
...args.request
|
|
2905
2919
|
};
|
|
2906
|
-
if (!
|
|
2920
|
+
if (!responseWaiter.isSettled()) {
|
|
2907
2921
|
try {
|
|
2908
2922
|
ws.send(responseCreateEvent);
|
|
2909
2923
|
} catch (error) {
|
|
2910
|
-
rejectResponsePromise
|
|
2924
|
+
responseWaiter.rejectResponsePromise(error);
|
|
2911
2925
|
}
|
|
2912
2926
|
}
|
|
2913
|
-
const response = await responsePromise;
|
|
2927
|
+
const response = await responseWaiter.responsePromise;
|
|
2914
2928
|
return { response, ws };
|
|
2915
2929
|
} catch (error) {
|
|
2916
2930
|
if (ownsWs) {
|
|
@@ -2919,6 +2933,221 @@ var OpenAiWsTransport = class {
|
|
|
2919
2933
|
throw error;
|
|
2920
2934
|
}
|
|
2921
2935
|
}
|
|
2936
|
+
createResponsePromise(args) {
|
|
2937
|
+
const { ws, signal, closeWs } = args;
|
|
2938
|
+
let settled = false;
|
|
2939
|
+
let rejectResponsePromise;
|
|
2940
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
2941
|
+
let onAbort;
|
|
2942
|
+
const nativeSocket = this.getNativeSocket(ws);
|
|
2943
|
+
let removeNativeClose;
|
|
2944
|
+
let removeNativeError;
|
|
2945
|
+
let removeNativeMessage;
|
|
2946
|
+
let timeout;
|
|
2947
|
+
let terminalDoneGraceTimer;
|
|
2948
|
+
let terminalCompletedWithoutUsage = null;
|
|
2949
|
+
const clearDoneGraceTimer = () => {
|
|
2950
|
+
if (terminalDoneGraceTimer) {
|
|
2951
|
+
clearTimeout(terminalDoneGraceTimer);
|
|
2952
|
+
terminalDoneGraceTimer = void 0;
|
|
2953
|
+
}
|
|
2954
|
+
};
|
|
2955
|
+
const teardown = () => {
|
|
2956
|
+
if (ws.off) {
|
|
2957
|
+
ws.off("error", onError);
|
|
2958
|
+
ws.off("event", onEvent);
|
|
2959
|
+
ws.off("response.failed", onResponseFailed);
|
|
2960
|
+
ws.off("response.completed", onResponseCompleted);
|
|
2961
|
+
ws.off("response.done", onResponseDone);
|
|
2962
|
+
ws.off("close", onClose);
|
|
2963
|
+
}
|
|
2964
|
+
if (timeout) {
|
|
2965
|
+
clearTimeout(timeout);
|
|
2966
|
+
}
|
|
2967
|
+
clearDoneGraceTimer();
|
|
2968
|
+
removeNativeClose?.();
|
|
2969
|
+
removeNativeError?.();
|
|
2970
|
+
removeNativeMessage?.();
|
|
2971
|
+
if (signal && onAbort) {
|
|
2972
|
+
signal.removeEventListener("abort", onAbort);
|
|
2973
|
+
}
|
|
2974
|
+
};
|
|
2975
|
+
const resolveOnce = (response) => {
|
|
2976
|
+
if (settled) return;
|
|
2977
|
+
settled = true;
|
|
2978
|
+
teardown();
|
|
2979
|
+
resolve(response);
|
|
2980
|
+
};
|
|
2981
|
+
const rejectOnce = (error) => {
|
|
2982
|
+
if (settled) return;
|
|
2983
|
+
settled = true;
|
|
2984
|
+
teardown();
|
|
2985
|
+
reject(error);
|
|
2986
|
+
};
|
|
2987
|
+
const scheduleDoneGraceTimer = () => {
|
|
2988
|
+
if (terminalDoneGraceTimer || settled) {
|
|
2989
|
+
return;
|
|
2990
|
+
}
|
|
2991
|
+
terminalDoneGraceTimer = setTimeout(() => {
|
|
2992
|
+
terminalDoneGraceTimer = void 0;
|
|
2993
|
+
if (settled || !terminalCompletedWithoutUsage) {
|
|
2994
|
+
return;
|
|
2995
|
+
}
|
|
2996
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
2997
|
+
}, WS_TERMINAL_DONE_GRACE_MS);
|
|
2998
|
+
};
|
|
2999
|
+
const resetResponseTimeout = () => {
|
|
3000
|
+
if (settled) return;
|
|
3001
|
+
if (timeout) {
|
|
3002
|
+
clearTimeout(timeout);
|
|
3003
|
+
}
|
|
3004
|
+
timeout = setTimeout(() => {
|
|
3005
|
+
if (settled) return;
|
|
3006
|
+
if (terminalCompletedWithoutUsage) {
|
|
3007
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3008
|
+
return;
|
|
3009
|
+
}
|
|
3010
|
+
settled = true;
|
|
3011
|
+
teardown();
|
|
3012
|
+
reject(new Error("openai websocket response timeout"));
|
|
3013
|
+
}, this.wsResponseIdleTimeoutMs);
|
|
3014
|
+
};
|
|
3015
|
+
rejectResponsePromise = rejectOnce;
|
|
3016
|
+
const onError = (error) => {
|
|
3017
|
+
rejectOnce(error);
|
|
3018
|
+
};
|
|
3019
|
+
const resolveFromTerminalEvent = (event, type) => {
|
|
3020
|
+
const response = event?.response;
|
|
3021
|
+
if (response && typeof response === "object") {
|
|
3022
|
+
if (type === "response.completed" && !this.hasUsageTotals(response)) {
|
|
3023
|
+
terminalCompletedWithoutUsage = response;
|
|
3024
|
+
scheduleDoneGraceTimer();
|
|
3025
|
+
return;
|
|
3026
|
+
}
|
|
3027
|
+
clearDoneGraceTimer();
|
|
3028
|
+
resolveOnce(response);
|
|
3029
|
+
return;
|
|
3030
|
+
}
|
|
3031
|
+
if (type === "response.done" && terminalCompletedWithoutUsage) {
|
|
3032
|
+
clearDoneGraceTimer();
|
|
3033
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3034
|
+
return;
|
|
3035
|
+
}
|
|
3036
|
+
rejectOnce(
|
|
3037
|
+
new Error(`openai websocket ${type} missing response payload`)
|
|
3038
|
+
);
|
|
3039
|
+
};
|
|
3040
|
+
const onEvent = (event) => {
|
|
3041
|
+
const eventType = this.extractResponsesEventType(event);
|
|
3042
|
+
if (!eventType) {
|
|
3043
|
+
return;
|
|
3044
|
+
}
|
|
3045
|
+
if (eventType === "response.failed") {
|
|
3046
|
+
onResponseFailed(event);
|
|
3047
|
+
return;
|
|
3048
|
+
}
|
|
3049
|
+
if (eventType === "response.completed") {
|
|
3050
|
+
onResponseCompleted(event);
|
|
3051
|
+
return;
|
|
3052
|
+
}
|
|
3053
|
+
if (eventType === "response.done") {
|
|
3054
|
+
onResponseDone(event);
|
|
3055
|
+
return;
|
|
3056
|
+
}
|
|
3057
|
+
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
3058
|
+
resetResponseTimeout();
|
|
3059
|
+
}
|
|
3060
|
+
};
|
|
3061
|
+
const onResponseFailed = (event) => {
|
|
3062
|
+
const failed = event;
|
|
3063
|
+
const message = failed.response?.error?.message;
|
|
3064
|
+
const code = failed.response?.error?.code ?? void 0;
|
|
3065
|
+
rejectOnce(
|
|
3066
|
+
new WsResponseError(
|
|
3067
|
+
message || "openai websocket response failed",
|
|
3068
|
+
typeof code === "string" ? code : void 0
|
|
3069
|
+
)
|
|
3070
|
+
);
|
|
3071
|
+
};
|
|
3072
|
+
const onResponseCompleted = (event) => {
|
|
3073
|
+
resolveFromTerminalEvent(event, "response.completed");
|
|
3074
|
+
};
|
|
3075
|
+
const onResponseDone = (event) => {
|
|
3076
|
+
resolveFromTerminalEvent(event, "response.done");
|
|
3077
|
+
};
|
|
3078
|
+
const onClose = (event) => {
|
|
3079
|
+
const closeCode = this.extractWsCloseCode(event);
|
|
3080
|
+
rejectOnce(
|
|
3081
|
+
new Error(
|
|
3082
|
+
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
3083
|
+
)
|
|
3084
|
+
);
|
|
3085
|
+
};
|
|
3086
|
+
ws.on("error", onError);
|
|
3087
|
+
ws.on("event", onEvent);
|
|
3088
|
+
ws.on("response.failed", onResponseFailed);
|
|
3089
|
+
ws.on("response.completed", onResponseCompleted);
|
|
3090
|
+
ws.on("response.done", onResponseDone);
|
|
3091
|
+
ws.on("close", onClose);
|
|
3092
|
+
removeNativeClose = this.addNativeSocketListener(
|
|
3093
|
+
nativeSocket,
|
|
3094
|
+
"close",
|
|
3095
|
+
(...events) => {
|
|
3096
|
+
onClose(events[0]);
|
|
3097
|
+
}
|
|
3098
|
+
);
|
|
3099
|
+
removeNativeError = this.addNativeSocketListener(
|
|
3100
|
+
nativeSocket,
|
|
3101
|
+
"error",
|
|
3102
|
+
(error) => {
|
|
3103
|
+
onError(error);
|
|
3104
|
+
}
|
|
3105
|
+
);
|
|
3106
|
+
removeNativeMessage = this.addNativeSocketListener(
|
|
3107
|
+
nativeSocket,
|
|
3108
|
+
"message",
|
|
3109
|
+
(...events) => {
|
|
3110
|
+
const eventType = this.extractNativeResponsesEventType(events[0]);
|
|
3111
|
+
if (!eventType) {
|
|
3112
|
+
return;
|
|
3113
|
+
}
|
|
3114
|
+
if (eventType === "response.failed") {
|
|
3115
|
+
onResponseFailed(events[0]);
|
|
3116
|
+
return;
|
|
3117
|
+
}
|
|
3118
|
+
if (eventType === "response.completed") {
|
|
3119
|
+
onResponseCompleted(events[0]);
|
|
3120
|
+
return;
|
|
3121
|
+
}
|
|
3122
|
+
if (eventType === "response.done") {
|
|
3123
|
+
onResponseDone(events[0]);
|
|
3124
|
+
return;
|
|
3125
|
+
}
|
|
3126
|
+
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
3127
|
+
resetResponseTimeout();
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
);
|
|
3131
|
+
if (signal) {
|
|
3132
|
+
onAbort = () => {
|
|
3133
|
+
rejectOnce(new Error("openai websocket request aborted"));
|
|
3134
|
+
closeWs();
|
|
3135
|
+
};
|
|
3136
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
3137
|
+
if (signal.aborted) {
|
|
3138
|
+
onAbort();
|
|
3139
|
+
}
|
|
3140
|
+
}
|
|
3141
|
+
resetResponseTimeout();
|
|
3142
|
+
});
|
|
3143
|
+
return {
|
|
3144
|
+
responsePromise,
|
|
3145
|
+
isSettled: () => settled,
|
|
3146
|
+
rejectResponsePromise: (error) => {
|
|
3147
|
+
rejectResponsePromise?.(error);
|
|
3148
|
+
}
|
|
3149
|
+
};
|
|
3150
|
+
}
|
|
2922
3151
|
closeSafely(ws, reason) {
|
|
2923
3152
|
if (!ws) {
|
|
2924
3153
|
return;
|
|
@@ -3072,6 +3301,38 @@ var OpenAiWsTransport = class {
|
|
|
3072
3301
|
const message = maybeError.message.toLowerCase();
|
|
3073
3302
|
return message.includes("not open") || message.includes("already closed") || message.includes("was closed before") || message.includes("readystate") || message.includes("closing") || message.includes("closed");
|
|
3074
3303
|
}
|
|
3304
|
+
extractResponsesEventType(event) {
|
|
3305
|
+
if (!event || typeof event !== "object") {
|
|
3306
|
+
return void 0;
|
|
3307
|
+
}
|
|
3308
|
+
const maybeEvent = event;
|
|
3309
|
+
return typeof maybeEvent.type === "string" ? maybeEvent.type : void 0;
|
|
3310
|
+
}
|
|
3311
|
+
shouldExtendResponseIdleTimeout(eventType) {
|
|
3312
|
+
if (!eventType.startsWith("response.")) {
|
|
3313
|
+
return false;
|
|
3314
|
+
}
|
|
3315
|
+
return eventType !== "response.failed" && eventType !== "response.completed" && eventType !== "response.done";
|
|
3316
|
+
}
|
|
3317
|
+
hasUsageTotals(response) {
|
|
3318
|
+
return typeof response.usage?.input_tokens === "number" && typeof response.usage?.output_tokens === "number" && typeof response.usage?.total_tokens === "number";
|
|
3319
|
+
}
|
|
3320
|
+
extractNativeResponsesEventType(message) {
|
|
3321
|
+
const directType = this.extractResponsesEventType(message);
|
|
3322
|
+
if (directType) {
|
|
3323
|
+
return directType;
|
|
3324
|
+
}
|
|
3325
|
+
const payloadText = this.toUnexpectedResponseChunkText(message).trim();
|
|
3326
|
+
if (!payloadText) {
|
|
3327
|
+
return void 0;
|
|
3328
|
+
}
|
|
3329
|
+
try {
|
|
3330
|
+
const parsed = JSON.parse(payloadText);
|
|
3331
|
+
return this.extractResponsesEventType(parsed);
|
|
3332
|
+
} catch {
|
|
3333
|
+
return void 0;
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3075
3336
|
formatUnexpectedResponseHeaders(headers) {
|
|
3076
3337
|
if (!headers || typeof headers !== "object") {
|
|
3077
3338
|
return void 0;
|
|
@@ -3244,16 +3505,13 @@ var OpenAiWsTransport = class {
|
|
|
3244
3505
|
addSocketListener("error", (error) => {
|
|
3245
3506
|
settleReject(error);
|
|
3246
3507
|
});
|
|
3247
|
-
addSocketListener(
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
});
|
|
3255
|
-
}
|
|
3256
|
-
);
|
|
3508
|
+
addSocketListener("unexpected-response", (...args) => {
|
|
3509
|
+
void this.createUnexpectedResponseError(args[1]).then((error) => {
|
|
3510
|
+
settleReject(error);
|
|
3511
|
+
}).catch((error) => {
|
|
3512
|
+
settleReject(error);
|
|
3513
|
+
});
|
|
3514
|
+
});
|
|
3257
3515
|
addSocketListener("close", (code) => {
|
|
3258
3516
|
const closeCode = typeof code === "number" ? ` code=${code}` : "";
|
|
3259
3517
|
settleReject(
|
|
@@ -3292,6 +3550,7 @@ var ChatOpenAI = class {
|
|
|
3292
3550
|
model;
|
|
3293
3551
|
client;
|
|
3294
3552
|
defaultReasoningEffort;
|
|
3553
|
+
reasoningLevelMeta;
|
|
3295
3554
|
defaultTextVerbosity;
|
|
3296
3555
|
websocketMode;
|
|
3297
3556
|
websocketApiVersion;
|
|
@@ -3305,6 +3564,11 @@ var ChatOpenAI = class {
|
|
|
3305
3564
|
this.client = options.client ?? new OpenAI(options.clientOptions);
|
|
3306
3565
|
this.model = options.model ?? DEFAULT_MODEL2;
|
|
3307
3566
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT;
|
|
3567
|
+
this.reasoningLevelMeta = {
|
|
3568
|
+
requested: options.reasoningLevelRequested,
|
|
3569
|
+
applied: options.reasoningLevelApplied,
|
|
3570
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
3571
|
+
};
|
|
3308
3572
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
3309
3573
|
this.websocketMode = options.websocketMode ?? "off";
|
|
3310
3574
|
this.websocketApiVersion = options.websocketApiVersion ?? "v2";
|
|
@@ -3391,9 +3655,24 @@ var ChatOpenAI = class {
|
|
|
3391
3655
|
fallback_used: transportResult.fallbackUsed,
|
|
3392
3656
|
chain_reset: transportResult.chainReset,
|
|
3393
3657
|
ws_reconnect_count: this.wsReconnectCount,
|
|
3394
|
-
ws_input_mode: transportResult.wsInputMode
|
|
3658
|
+
ws_input_mode: transportResult.wsInputMode,
|
|
3659
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
3660
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
3661
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
3395
3662
|
});
|
|
3396
3663
|
}
|
|
3664
|
+
onHistoryCompacted(context) {
|
|
3665
|
+
const sessionKey = context?.sessionKey;
|
|
3666
|
+
if (sessionKey) {
|
|
3667
|
+
this.clearWsSessionState(sessionKey, "history_compacted");
|
|
3668
|
+
this.wsDisabledUntilBySessionKey.delete(sessionKey);
|
|
3669
|
+
return;
|
|
3670
|
+
}
|
|
3671
|
+
for (const key of Array.from(this.wsStateBySessionKey.keys())) {
|
|
3672
|
+
this.clearWsSessionState(key, "history_compacted");
|
|
3673
|
+
}
|
|
3674
|
+
this.wsDisabledUntilBySessionKey.clear();
|
|
3675
|
+
}
|
|
3397
3676
|
async invokeWithTransport(args) {
|
|
3398
3677
|
this.evictIdleWsSessionState();
|
|
3399
3678
|
if (this.websocketMode === "off") {
|
|
@@ -3441,7 +3720,7 @@ var ChatOpenAI = class {
|
|
|
3441
3720
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3442
3721
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3443
3722
|
let wsInputMode = "full_no_previous";
|
|
3444
|
-
if (
|
|
3723
|
+
if (wsState.previousResponseId) {
|
|
3445
3724
|
if (canUsePreviousResponseId) {
|
|
3446
3725
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3447
3726
|
wsInputMode = "empty";
|
|
@@ -3503,10 +3782,12 @@ var ChatOpenAI = class {
|
|
|
3503
3782
|
} catch (error) {
|
|
3504
3783
|
let failure = error;
|
|
3505
3784
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3506
|
-
const retryWsInputMode =
|
|
3507
|
-
|
|
3508
|
-
|
|
3509
|
-
|
|
3785
|
+
const retryWsInputMode = wsState.previousResponseId ? "full_regenerated" : "full_no_previous";
|
|
3786
|
+
this.closeWsWithoutMaskingPrimaryError(
|
|
3787
|
+
wsState.ws,
|
|
3788
|
+
"retry_reset",
|
|
3789
|
+
failure
|
|
3790
|
+
);
|
|
3510
3791
|
try {
|
|
3511
3792
|
await this.debugRequestIfEnabled(
|
|
3512
3793
|
args.request,
|
|
@@ -3606,7 +3887,9 @@ var ChatOpenAI = class {
|
|
|
3606
3887
|
if (!Array.isArray(requestInput)) {
|
|
3607
3888
|
return requestInput;
|
|
3608
3889
|
}
|
|
3609
|
-
const replayItems = toResponsesInput(
|
|
3890
|
+
const replayItems = toResponsesInput(
|
|
3891
|
+
toChatInvokeCompletion2(response).messages
|
|
3892
|
+
);
|
|
3610
3893
|
if (replayItems.length === 0) {
|
|
3611
3894
|
return requestInput;
|
|
3612
3895
|
}
|
|
@@ -4132,7 +4415,7 @@ function toResponsesToolChoice2(choice) {
|
|
|
4132
4415
|
}
|
|
4133
4416
|
return { type: "function", name: choice };
|
|
4134
4417
|
}
|
|
4135
|
-
function toChatInvokeCompletion3(response) {
|
|
4418
|
+
function toChatInvokeCompletion3(response, meta) {
|
|
4136
4419
|
const usage = response.usage ? {
|
|
4137
4420
|
model: response.model,
|
|
4138
4421
|
input_tokens: response.usage.input_tokens,
|
|
@@ -4152,7 +4435,10 @@ function toChatInvokeCompletion3(response) {
|
|
|
4152
4435
|
usage,
|
|
4153
4436
|
stop_reason: response.incomplete_details?.reason ?? response.status ?? null,
|
|
4154
4437
|
provider_meta: {
|
|
4155
|
-
response_id: response.id
|
|
4438
|
+
response_id: response.id,
|
|
4439
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
4440
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
4441
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {}
|
|
4156
4442
|
}
|
|
4157
4443
|
};
|
|
4158
4444
|
}
|
|
@@ -4238,6 +4524,7 @@ var ChatOpenRouter = class {
|
|
|
4238
4524
|
model;
|
|
4239
4525
|
client;
|
|
4240
4526
|
defaultReasoningEffort;
|
|
4527
|
+
reasoningLevelMeta;
|
|
4241
4528
|
defaultTextVerbosity;
|
|
4242
4529
|
debugInvokeSeq = 0;
|
|
4243
4530
|
lastDebugRequestPayload = null;
|
|
@@ -4248,6 +4535,11 @@ var ChatOpenRouter = class {
|
|
|
4248
4535
|
});
|
|
4249
4536
|
this.model = options.model ?? DEFAULT_MODEL3;
|
|
4250
4537
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT2;
|
|
4538
|
+
this.reasoningLevelMeta = {
|
|
4539
|
+
requested: options.reasoningLevelRequested,
|
|
4540
|
+
applied: options.reasoningLevelApplied,
|
|
4541
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
4542
|
+
};
|
|
4251
4543
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
4252
4544
|
}
|
|
4253
4545
|
async ainvoke(input, context) {
|
|
@@ -4318,7 +4610,11 @@ var ChatOpenRouter = class {
|
|
|
4318
4610
|
} : void 0
|
|
4319
4611
|
).finalResponse();
|
|
4320
4612
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
4321
|
-
return toChatInvokeCompletion3(response
|
|
4613
|
+
return toChatInvokeCompletion3(response, {
|
|
4614
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
4615
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
4616
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
4617
|
+
});
|
|
4322
4618
|
}
|
|
4323
4619
|
nextDebugInvokeSeq() {
|
|
4324
4620
|
const seq = this.debugInvokeSeq + 1;
|