@codelia/core 0.1.33 → 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 +438 -221
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +438 -221
- 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,197 +2905,248 @@ 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
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2911
|
+
const responseWaiter = this.createResponsePromise({
|
|
2912
|
+
ws,
|
|
2913
|
+
signal: args.signal,
|
|
2914
|
+
closeWs
|
|
2915
|
+
});
|
|
2916
|
+
const responseCreateEvent = {
|
|
2917
|
+
type: "response.create",
|
|
2918
|
+
...args.request
|
|
2919
|
+
};
|
|
2920
|
+
if (!responseWaiter.isSettled()) {
|
|
2921
|
+
try {
|
|
2922
|
+
ws.send(responseCreateEvent);
|
|
2923
|
+
} catch (error) {
|
|
2924
|
+
responseWaiter.rejectResponsePromise(error);
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
const response = await responseWaiter.responsePromise;
|
|
2928
|
+
return { response, ws };
|
|
2929
|
+
} catch (error) {
|
|
2930
|
+
if (ownsWs) {
|
|
2931
|
+
this.closeWithoutMaskingPrimaryError(ws, "failed", error);
|
|
2932
|
+
}
|
|
2933
|
+
throw error;
|
|
2934
|
+
}
|
|
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;
|
|
2821
2995
|
}
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
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(() => {
|
|
2830
3005
|
if (settled) return;
|
|
3006
|
+
if (terminalCompletedWithoutUsage) {
|
|
3007
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3008
|
+
return;
|
|
3009
|
+
}
|
|
2831
3010
|
settled = true;
|
|
2832
3011
|
teardown();
|
|
2833
|
-
reject(
|
|
2834
|
-
};
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
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();
|
|
2843
3025
|
return;
|
|
2844
3026
|
}
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
}
|
|
2849
|
-
|
|
2850
|
-
|
|
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]);
|
|
2851
3111
|
if (!eventType) {
|
|
2852
3112
|
return;
|
|
2853
3113
|
}
|
|
2854
3114
|
if (eventType === "response.failed") {
|
|
2855
|
-
onResponseFailed(
|
|
3115
|
+
onResponseFailed(events[0]);
|
|
2856
3116
|
return;
|
|
2857
3117
|
}
|
|
2858
3118
|
if (eventType === "response.completed") {
|
|
2859
|
-
onResponseCompleted(
|
|
3119
|
+
onResponseCompleted(events[0]);
|
|
2860
3120
|
return;
|
|
2861
3121
|
}
|
|
2862
3122
|
if (eventType === "response.done") {
|
|
2863
|
-
onResponseDone(
|
|
3123
|
+
onResponseDone(events[0]);
|
|
2864
3124
|
return;
|
|
2865
3125
|
}
|
|
2866
3126
|
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
2867
3127
|
resetResponseTimeout();
|
|
2868
3128
|
}
|
|
2869
|
-
};
|
|
2870
|
-
const onResponseFailed = (event) => {
|
|
2871
|
-
const failed = event;
|
|
2872
|
-
const message = failed.response?.error?.message;
|
|
2873
|
-
const code = failed.response?.error?.code ?? void 0;
|
|
2874
|
-
rejectOnce(
|
|
2875
|
-
new WsResponseError(
|
|
2876
|
-
message || "openai websocket response failed",
|
|
2877
|
-
typeof code === "string" ? code : void 0
|
|
2878
|
-
)
|
|
2879
|
-
);
|
|
2880
|
-
};
|
|
2881
|
-
const onResponseCompleted = (event) => {
|
|
2882
|
-
resolveFromTerminalEvent(event, "response.completed");
|
|
2883
|
-
};
|
|
2884
|
-
const onResponseDone = (event) => {
|
|
2885
|
-
resolveFromTerminalEvent(event, "response.done");
|
|
2886
|
-
};
|
|
2887
|
-
const onClose = (event) => {
|
|
2888
|
-
const closeCode = this.extractWsCloseCode(event);
|
|
2889
|
-
rejectOnce(
|
|
2890
|
-
new Error(
|
|
2891
|
-
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
2892
|
-
)
|
|
2893
|
-
);
|
|
2894
|
-
};
|
|
2895
|
-
ws.on("error", onError);
|
|
2896
|
-
ws.on("event", onEvent);
|
|
2897
|
-
ws.on("response.failed", onResponseFailed);
|
|
2898
|
-
ws.on("response.completed", onResponseCompleted);
|
|
2899
|
-
ws.on("response.done", onResponseDone);
|
|
2900
|
-
ws.on("close", onClose);
|
|
2901
|
-
removeNativeClose = this.addNativeSocketListener(
|
|
2902
|
-
nativeSocket,
|
|
2903
|
-
"close",
|
|
2904
|
-
(...events) => {
|
|
2905
|
-
onClose(events[0]);
|
|
2906
|
-
}
|
|
2907
|
-
);
|
|
2908
|
-
removeNativeError = this.addNativeSocketListener(
|
|
2909
|
-
nativeSocket,
|
|
2910
|
-
"error",
|
|
2911
|
-
(error) => {
|
|
2912
|
-
onError(error);
|
|
2913
|
-
}
|
|
2914
|
-
);
|
|
2915
|
-
removeNativeMessage = this.addNativeSocketListener(
|
|
2916
|
-
nativeSocket,
|
|
2917
|
-
"message",
|
|
2918
|
-
(...events) => {
|
|
2919
|
-
const eventType = this.extractNativeResponsesEventType(events[0]);
|
|
2920
|
-
if (!eventType) {
|
|
2921
|
-
return;
|
|
2922
|
-
}
|
|
2923
|
-
if (eventType === "response.failed") {
|
|
2924
|
-
onResponseFailed(events[0]);
|
|
2925
|
-
return;
|
|
2926
|
-
}
|
|
2927
|
-
if (eventType === "response.completed") {
|
|
2928
|
-
onResponseCompleted(events[0]);
|
|
2929
|
-
return;
|
|
2930
|
-
}
|
|
2931
|
-
if (eventType === "response.done") {
|
|
2932
|
-
onResponseDone(events[0]);
|
|
2933
|
-
return;
|
|
2934
|
-
}
|
|
2935
|
-
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
2936
|
-
resetResponseTimeout();
|
|
2937
|
-
}
|
|
2938
|
-
}
|
|
2939
|
-
);
|
|
2940
|
-
if (args.signal) {
|
|
2941
|
-
onAbort = () => {
|
|
2942
|
-
rejectOnce(new Error("openai websocket request aborted"));
|
|
2943
|
-
closeWs();
|
|
2944
|
-
};
|
|
2945
|
-
args.signal.addEventListener("abort", onAbort, { once: true });
|
|
2946
|
-
if (args.signal.aborted) {
|
|
2947
|
-
onAbort();
|
|
2948
|
-
}
|
|
2949
3129
|
}
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
} catch (error) {
|
|
2960
|
-
rejectResponsePromise?.(error);
|
|
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();
|
|
2961
3139
|
}
|
|
2962
3140
|
}
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
3141
|
+
resetResponseTimeout();
|
|
3142
|
+
});
|
|
3143
|
+
return {
|
|
3144
|
+
responsePromise,
|
|
3145
|
+
isSettled: () => settled,
|
|
3146
|
+
rejectResponsePromise: (error) => {
|
|
3147
|
+
rejectResponsePromise?.(error);
|
|
2968
3148
|
}
|
|
2969
|
-
|
|
2970
|
-
}
|
|
3149
|
+
};
|
|
2971
3150
|
}
|
|
2972
3151
|
closeSafely(ws, reason) {
|
|
2973
3152
|
if (!ws) {
|
|
@@ -3135,6 +3314,9 @@ var OpenAiWsTransport = class {
|
|
|
3135
3314
|
}
|
|
3136
3315
|
return eventType !== "response.failed" && eventType !== "response.completed" && eventType !== "response.done";
|
|
3137
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
|
+
}
|
|
3138
3320
|
extractNativeResponsesEventType(message) {
|
|
3139
3321
|
const directType = this.extractResponsesEventType(message);
|
|
3140
3322
|
if (directType) {
|
|
@@ -3323,16 +3505,13 @@ var OpenAiWsTransport = class {
|
|
|
3323
3505
|
addSocketListener("error", (error) => {
|
|
3324
3506
|
settleReject(error);
|
|
3325
3507
|
});
|
|
3326
|
-
addSocketListener(
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
});
|
|
3334
|
-
}
|
|
3335
|
-
);
|
|
3508
|
+
addSocketListener("unexpected-response", (...args) => {
|
|
3509
|
+
void this.createUnexpectedResponseError(args[1]).then((error) => {
|
|
3510
|
+
settleReject(error);
|
|
3511
|
+
}).catch((error) => {
|
|
3512
|
+
settleReject(error);
|
|
3513
|
+
});
|
|
3514
|
+
});
|
|
3336
3515
|
addSocketListener("close", (code) => {
|
|
3337
3516
|
const closeCode = typeof code === "number" ? ` code=${code}` : "";
|
|
3338
3517
|
settleReject(
|
|
@@ -3371,6 +3550,7 @@ var ChatOpenAI = class {
|
|
|
3371
3550
|
model;
|
|
3372
3551
|
client;
|
|
3373
3552
|
defaultReasoningEffort;
|
|
3553
|
+
reasoningLevelMeta;
|
|
3374
3554
|
defaultTextVerbosity;
|
|
3375
3555
|
websocketMode;
|
|
3376
3556
|
websocketApiVersion;
|
|
@@ -3384,6 +3564,11 @@ var ChatOpenAI = class {
|
|
|
3384
3564
|
this.client = options.client ?? new OpenAI(options.clientOptions);
|
|
3385
3565
|
this.model = options.model ?? DEFAULT_MODEL2;
|
|
3386
3566
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT;
|
|
3567
|
+
this.reasoningLevelMeta = {
|
|
3568
|
+
requested: options.reasoningLevelRequested,
|
|
3569
|
+
applied: options.reasoningLevelApplied,
|
|
3570
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
3571
|
+
};
|
|
3387
3572
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
3388
3573
|
this.websocketMode = options.websocketMode ?? "off";
|
|
3389
3574
|
this.websocketApiVersion = options.websocketApiVersion ?? "v2";
|
|
@@ -3470,9 +3655,24 @@ var ChatOpenAI = class {
|
|
|
3470
3655
|
fallback_used: transportResult.fallbackUsed,
|
|
3471
3656
|
chain_reset: transportResult.chainReset,
|
|
3472
3657
|
ws_reconnect_count: this.wsReconnectCount,
|
|
3473
|
-
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
|
|
3474
3662
|
});
|
|
3475
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
|
+
}
|
|
3476
3676
|
async invokeWithTransport(args) {
|
|
3477
3677
|
this.evictIdleWsSessionState();
|
|
3478
3678
|
if (this.websocketMode === "off") {
|
|
@@ -3520,7 +3720,7 @@ var ChatOpenAI = class {
|
|
|
3520
3720
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3521
3721
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3522
3722
|
let wsInputMode = "full_no_previous";
|
|
3523
|
-
if (
|
|
3723
|
+
if (wsState.previousResponseId) {
|
|
3524
3724
|
if (canUsePreviousResponseId) {
|
|
3525
3725
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3526
3726
|
wsInputMode = "empty";
|
|
@@ -3582,10 +3782,12 @@ var ChatOpenAI = class {
|
|
|
3582
3782
|
} catch (error) {
|
|
3583
3783
|
let failure = error;
|
|
3584
3784
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3585
|
-
const retryWsInputMode =
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3785
|
+
const retryWsInputMode = wsState.previousResponseId ? "full_regenerated" : "full_no_previous";
|
|
3786
|
+
this.closeWsWithoutMaskingPrimaryError(
|
|
3787
|
+
wsState.ws,
|
|
3788
|
+
"retry_reset",
|
|
3789
|
+
failure
|
|
3790
|
+
);
|
|
3589
3791
|
try {
|
|
3590
3792
|
await this.debugRequestIfEnabled(
|
|
3591
3793
|
args.request,
|
|
@@ -3685,7 +3887,9 @@ var ChatOpenAI = class {
|
|
|
3685
3887
|
if (!Array.isArray(requestInput)) {
|
|
3686
3888
|
return requestInput;
|
|
3687
3889
|
}
|
|
3688
|
-
const replayItems = toResponsesInput(
|
|
3890
|
+
const replayItems = toResponsesInput(
|
|
3891
|
+
toChatInvokeCompletion2(response).messages
|
|
3892
|
+
);
|
|
3689
3893
|
if (replayItems.length === 0) {
|
|
3690
3894
|
return requestInput;
|
|
3691
3895
|
}
|
|
@@ -4211,7 +4415,7 @@ function toResponsesToolChoice2(choice) {
|
|
|
4211
4415
|
}
|
|
4212
4416
|
return { type: "function", name: choice };
|
|
4213
4417
|
}
|
|
4214
|
-
function toChatInvokeCompletion3(response) {
|
|
4418
|
+
function toChatInvokeCompletion3(response, meta) {
|
|
4215
4419
|
const usage = response.usage ? {
|
|
4216
4420
|
model: response.model,
|
|
4217
4421
|
input_tokens: response.usage.input_tokens,
|
|
@@ -4231,7 +4435,10 @@ function toChatInvokeCompletion3(response) {
|
|
|
4231
4435
|
usage,
|
|
4232
4436
|
stop_reason: response.incomplete_details?.reason ?? response.status ?? null,
|
|
4233
4437
|
provider_meta: {
|
|
4234
|
-
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 } : {}
|
|
4235
4442
|
}
|
|
4236
4443
|
};
|
|
4237
4444
|
}
|
|
@@ -4317,6 +4524,7 @@ var ChatOpenRouter = class {
|
|
|
4317
4524
|
model;
|
|
4318
4525
|
client;
|
|
4319
4526
|
defaultReasoningEffort;
|
|
4527
|
+
reasoningLevelMeta;
|
|
4320
4528
|
defaultTextVerbosity;
|
|
4321
4529
|
debugInvokeSeq = 0;
|
|
4322
4530
|
lastDebugRequestPayload = null;
|
|
@@ -4327,6 +4535,11 @@ var ChatOpenRouter = class {
|
|
|
4327
4535
|
});
|
|
4328
4536
|
this.model = options.model ?? DEFAULT_MODEL3;
|
|
4329
4537
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT2;
|
|
4538
|
+
this.reasoningLevelMeta = {
|
|
4539
|
+
requested: options.reasoningLevelRequested,
|
|
4540
|
+
applied: options.reasoningLevelApplied,
|
|
4541
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
4542
|
+
};
|
|
4330
4543
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
4331
4544
|
}
|
|
4332
4545
|
async ainvoke(input, context) {
|
|
@@ -4397,7 +4610,11 @@ var ChatOpenRouter = class {
|
|
|
4397
4610
|
} : void 0
|
|
4398
4611
|
).finalResponse();
|
|
4399
4612
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
4400
|
-
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
|
+
});
|
|
4401
4618
|
}
|
|
4402
4619
|
nextDebugInvokeSeq() {
|
|
4403
4620
|
const seq = this.debugInvokeSeq + 1;
|