@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.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,197 +2962,248 @@ 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
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2968
|
+
const responseWaiter = this.createResponsePromise({
|
|
2969
|
+
ws,
|
|
2970
|
+
signal: args.signal,
|
|
2971
|
+
closeWs
|
|
2972
|
+
});
|
|
2973
|
+
const responseCreateEvent = {
|
|
2974
|
+
type: "response.create",
|
|
2975
|
+
...args.request
|
|
2976
|
+
};
|
|
2977
|
+
if (!responseWaiter.isSettled()) {
|
|
2978
|
+
try {
|
|
2979
|
+
ws.send(responseCreateEvent);
|
|
2980
|
+
} catch (error) {
|
|
2981
|
+
responseWaiter.rejectResponsePromise(error);
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
const response = await responseWaiter.responsePromise;
|
|
2985
|
+
return { response, ws };
|
|
2986
|
+
} catch (error) {
|
|
2987
|
+
if (ownsWs) {
|
|
2988
|
+
this.closeWithoutMaskingPrimaryError(ws, "failed", error);
|
|
2989
|
+
}
|
|
2990
|
+
throw error;
|
|
2991
|
+
}
|
|
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;
|
|
2878
3052
|
}
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
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(() => {
|
|
2887
3062
|
if (settled) return;
|
|
3063
|
+
if (terminalCompletedWithoutUsage) {
|
|
3064
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3065
|
+
return;
|
|
3066
|
+
}
|
|
2888
3067
|
settled = true;
|
|
2889
3068
|
teardown();
|
|
2890
|
-
reject(
|
|
2891
|
-
};
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
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();
|
|
2900
3082
|
return;
|
|
2901
3083
|
}
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
}
|
|
2906
|
-
|
|
2907
|
-
|
|
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]);
|
|
2908
3168
|
if (!eventType) {
|
|
2909
3169
|
return;
|
|
2910
3170
|
}
|
|
2911
3171
|
if (eventType === "response.failed") {
|
|
2912
|
-
onResponseFailed(
|
|
3172
|
+
onResponseFailed(events[0]);
|
|
2913
3173
|
return;
|
|
2914
3174
|
}
|
|
2915
3175
|
if (eventType === "response.completed") {
|
|
2916
|
-
onResponseCompleted(
|
|
3176
|
+
onResponseCompleted(events[0]);
|
|
2917
3177
|
return;
|
|
2918
3178
|
}
|
|
2919
3179
|
if (eventType === "response.done") {
|
|
2920
|
-
onResponseDone(
|
|
3180
|
+
onResponseDone(events[0]);
|
|
2921
3181
|
return;
|
|
2922
3182
|
}
|
|
2923
3183
|
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
2924
3184
|
resetResponseTimeout();
|
|
2925
3185
|
}
|
|
2926
|
-
};
|
|
2927
|
-
const onResponseFailed = (event) => {
|
|
2928
|
-
const failed = event;
|
|
2929
|
-
const message = failed.response?.error?.message;
|
|
2930
|
-
const code = failed.response?.error?.code ?? void 0;
|
|
2931
|
-
rejectOnce(
|
|
2932
|
-
new WsResponseError(
|
|
2933
|
-
message || "openai websocket response failed",
|
|
2934
|
-
typeof code === "string" ? code : void 0
|
|
2935
|
-
)
|
|
2936
|
-
);
|
|
2937
|
-
};
|
|
2938
|
-
const onResponseCompleted = (event) => {
|
|
2939
|
-
resolveFromTerminalEvent(event, "response.completed");
|
|
2940
|
-
};
|
|
2941
|
-
const onResponseDone = (event) => {
|
|
2942
|
-
resolveFromTerminalEvent(event, "response.done");
|
|
2943
|
-
};
|
|
2944
|
-
const onClose = (event) => {
|
|
2945
|
-
const closeCode = this.extractWsCloseCode(event);
|
|
2946
|
-
rejectOnce(
|
|
2947
|
-
new Error(
|
|
2948
|
-
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
2949
|
-
)
|
|
2950
|
-
);
|
|
2951
|
-
};
|
|
2952
|
-
ws.on("error", onError);
|
|
2953
|
-
ws.on("event", onEvent);
|
|
2954
|
-
ws.on("response.failed", onResponseFailed);
|
|
2955
|
-
ws.on("response.completed", onResponseCompleted);
|
|
2956
|
-
ws.on("response.done", onResponseDone);
|
|
2957
|
-
ws.on("close", onClose);
|
|
2958
|
-
removeNativeClose = this.addNativeSocketListener(
|
|
2959
|
-
nativeSocket,
|
|
2960
|
-
"close",
|
|
2961
|
-
(...events) => {
|
|
2962
|
-
onClose(events[0]);
|
|
2963
|
-
}
|
|
2964
|
-
);
|
|
2965
|
-
removeNativeError = this.addNativeSocketListener(
|
|
2966
|
-
nativeSocket,
|
|
2967
|
-
"error",
|
|
2968
|
-
(error) => {
|
|
2969
|
-
onError(error);
|
|
2970
|
-
}
|
|
2971
|
-
);
|
|
2972
|
-
removeNativeMessage = this.addNativeSocketListener(
|
|
2973
|
-
nativeSocket,
|
|
2974
|
-
"message",
|
|
2975
|
-
(...events) => {
|
|
2976
|
-
const eventType = this.extractNativeResponsesEventType(events[0]);
|
|
2977
|
-
if (!eventType) {
|
|
2978
|
-
return;
|
|
2979
|
-
}
|
|
2980
|
-
if (eventType === "response.failed") {
|
|
2981
|
-
onResponseFailed(events[0]);
|
|
2982
|
-
return;
|
|
2983
|
-
}
|
|
2984
|
-
if (eventType === "response.completed") {
|
|
2985
|
-
onResponseCompleted(events[0]);
|
|
2986
|
-
return;
|
|
2987
|
-
}
|
|
2988
|
-
if (eventType === "response.done") {
|
|
2989
|
-
onResponseDone(events[0]);
|
|
2990
|
-
return;
|
|
2991
|
-
}
|
|
2992
|
-
if (this.shouldExtendResponseIdleTimeout(eventType)) {
|
|
2993
|
-
resetResponseTimeout();
|
|
2994
|
-
}
|
|
2995
|
-
}
|
|
2996
|
-
);
|
|
2997
|
-
if (args.signal) {
|
|
2998
|
-
onAbort = () => {
|
|
2999
|
-
rejectOnce(new Error("openai websocket request aborted"));
|
|
3000
|
-
closeWs();
|
|
3001
|
-
};
|
|
3002
|
-
args.signal.addEventListener("abort", onAbort, { once: true });
|
|
3003
|
-
if (args.signal.aborted) {
|
|
3004
|
-
onAbort();
|
|
3005
|
-
}
|
|
3006
3186
|
}
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
} catch (error) {
|
|
3017
|
-
rejectResponsePromise?.(error);
|
|
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();
|
|
3018
3196
|
}
|
|
3019
3197
|
}
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3198
|
+
resetResponseTimeout();
|
|
3199
|
+
});
|
|
3200
|
+
return {
|
|
3201
|
+
responsePromise,
|
|
3202
|
+
isSettled: () => settled,
|
|
3203
|
+
rejectResponsePromise: (error) => {
|
|
3204
|
+
rejectResponsePromise?.(error);
|
|
3025
3205
|
}
|
|
3026
|
-
|
|
3027
|
-
}
|
|
3206
|
+
};
|
|
3028
3207
|
}
|
|
3029
3208
|
closeSafely(ws, reason) {
|
|
3030
3209
|
if (!ws) {
|
|
@@ -3192,6 +3371,9 @@ var OpenAiWsTransport = class {
|
|
|
3192
3371
|
}
|
|
3193
3372
|
return eventType !== "response.failed" && eventType !== "response.completed" && eventType !== "response.done";
|
|
3194
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
|
+
}
|
|
3195
3377
|
extractNativeResponsesEventType(message) {
|
|
3196
3378
|
const directType = this.extractResponsesEventType(message);
|
|
3197
3379
|
if (directType) {
|
|
@@ -3380,16 +3562,13 @@ var OpenAiWsTransport = class {
|
|
|
3380
3562
|
addSocketListener("error", (error) => {
|
|
3381
3563
|
settleReject(error);
|
|
3382
3564
|
});
|
|
3383
|
-
addSocketListener(
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
});
|
|
3391
|
-
}
|
|
3392
|
-
);
|
|
3565
|
+
addSocketListener("unexpected-response", (...args) => {
|
|
3566
|
+
void this.createUnexpectedResponseError(args[1]).then((error) => {
|
|
3567
|
+
settleReject(error);
|
|
3568
|
+
}).catch((error) => {
|
|
3569
|
+
settleReject(error);
|
|
3570
|
+
});
|
|
3571
|
+
});
|
|
3393
3572
|
addSocketListener("close", (code) => {
|
|
3394
3573
|
const closeCode = typeof code === "number" ? ` code=${code}` : "";
|
|
3395
3574
|
settleReject(
|
|
@@ -3428,6 +3607,7 @@ var ChatOpenAI = class {
|
|
|
3428
3607
|
model;
|
|
3429
3608
|
client;
|
|
3430
3609
|
defaultReasoningEffort;
|
|
3610
|
+
reasoningLevelMeta;
|
|
3431
3611
|
defaultTextVerbosity;
|
|
3432
3612
|
websocketMode;
|
|
3433
3613
|
websocketApiVersion;
|
|
@@ -3441,6 +3621,11 @@ var ChatOpenAI = class {
|
|
|
3441
3621
|
this.client = options.client ?? new import_openai3.default(options.clientOptions);
|
|
3442
3622
|
this.model = options.model ?? DEFAULT_MODEL2;
|
|
3443
3623
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT;
|
|
3624
|
+
this.reasoningLevelMeta = {
|
|
3625
|
+
requested: options.reasoningLevelRequested,
|
|
3626
|
+
applied: options.reasoningLevelApplied,
|
|
3627
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
3628
|
+
};
|
|
3444
3629
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
3445
3630
|
this.websocketMode = options.websocketMode ?? "off";
|
|
3446
3631
|
this.websocketApiVersion = options.websocketApiVersion ?? "v2";
|
|
@@ -3527,9 +3712,24 @@ var ChatOpenAI = class {
|
|
|
3527
3712
|
fallback_used: transportResult.fallbackUsed,
|
|
3528
3713
|
chain_reset: transportResult.chainReset,
|
|
3529
3714
|
ws_reconnect_count: this.wsReconnectCount,
|
|
3530
|
-
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
|
|
3531
3719
|
});
|
|
3532
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
|
+
}
|
|
3533
3733
|
async invokeWithTransport(args) {
|
|
3534
3734
|
this.evictIdleWsSessionState();
|
|
3535
3735
|
if (this.websocketMode === "off") {
|
|
@@ -3577,7 +3777,7 @@ var ChatOpenAI = class {
|
|
|
3577
3777
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3578
3778
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3579
3779
|
let wsInputMode = "full_no_previous";
|
|
3580
|
-
if (
|
|
3780
|
+
if (wsState.previousResponseId) {
|
|
3581
3781
|
if (canUsePreviousResponseId) {
|
|
3582
3782
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3583
3783
|
wsInputMode = "empty";
|
|
@@ -3639,10 +3839,12 @@ var ChatOpenAI = class {
|
|
|
3639
3839
|
} catch (error) {
|
|
3640
3840
|
let failure = error;
|
|
3641
3841
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3642
|
-
const retryWsInputMode =
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3842
|
+
const retryWsInputMode = wsState.previousResponseId ? "full_regenerated" : "full_no_previous";
|
|
3843
|
+
this.closeWsWithoutMaskingPrimaryError(
|
|
3844
|
+
wsState.ws,
|
|
3845
|
+
"retry_reset",
|
|
3846
|
+
failure
|
|
3847
|
+
);
|
|
3646
3848
|
try {
|
|
3647
3849
|
await this.debugRequestIfEnabled(
|
|
3648
3850
|
args.request,
|
|
@@ -3742,7 +3944,9 @@ var ChatOpenAI = class {
|
|
|
3742
3944
|
if (!Array.isArray(requestInput)) {
|
|
3743
3945
|
return requestInput;
|
|
3744
3946
|
}
|
|
3745
|
-
const replayItems = toResponsesInput(
|
|
3947
|
+
const replayItems = toResponsesInput(
|
|
3948
|
+
toChatInvokeCompletion2(response).messages
|
|
3949
|
+
);
|
|
3746
3950
|
if (replayItems.length === 0) {
|
|
3747
3951
|
return requestInput;
|
|
3748
3952
|
}
|
|
@@ -4268,7 +4472,7 @@ function toResponsesToolChoice2(choice) {
|
|
|
4268
4472
|
}
|
|
4269
4473
|
return { type: "function", name: choice };
|
|
4270
4474
|
}
|
|
4271
|
-
function toChatInvokeCompletion3(response) {
|
|
4475
|
+
function toChatInvokeCompletion3(response, meta) {
|
|
4272
4476
|
const usage = response.usage ? {
|
|
4273
4477
|
model: response.model,
|
|
4274
4478
|
input_tokens: response.usage.input_tokens,
|
|
@@ -4288,7 +4492,10 @@ function toChatInvokeCompletion3(response) {
|
|
|
4288
4492
|
usage,
|
|
4289
4493
|
stop_reason: response.incomplete_details?.reason ?? response.status ?? null,
|
|
4290
4494
|
provider_meta: {
|
|
4291
|
-
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 } : {}
|
|
4292
4499
|
}
|
|
4293
4500
|
};
|
|
4294
4501
|
}
|
|
@@ -4374,6 +4581,7 @@ var ChatOpenRouter = class {
|
|
|
4374
4581
|
model;
|
|
4375
4582
|
client;
|
|
4376
4583
|
defaultReasoningEffort;
|
|
4584
|
+
reasoningLevelMeta;
|
|
4377
4585
|
defaultTextVerbosity;
|
|
4378
4586
|
debugInvokeSeq = 0;
|
|
4379
4587
|
lastDebugRequestPayload = null;
|
|
@@ -4384,6 +4592,11 @@ var ChatOpenRouter = class {
|
|
|
4384
4592
|
});
|
|
4385
4593
|
this.model = options.model ?? DEFAULT_MODEL3;
|
|
4386
4594
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT2;
|
|
4595
|
+
this.reasoningLevelMeta = {
|
|
4596
|
+
requested: options.reasoningLevelRequested,
|
|
4597
|
+
applied: options.reasoningLevelApplied,
|
|
4598
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
4599
|
+
};
|
|
4387
4600
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
4388
4601
|
}
|
|
4389
4602
|
async ainvoke(input, context) {
|
|
@@ -4454,7 +4667,11 @@ var ChatOpenRouter = class {
|
|
|
4454
4667
|
} : void 0
|
|
4455
4668
|
).finalResponse();
|
|
4456
4669
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
4457
|
-
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
|
+
});
|
|
4458
4675
|
}
|
|
4459
4676
|
nextDebugInvokeSeq() {
|
|
4460
4677
|
const seq = this.debugInvokeSeq + 1;
|