@codelia/core 0.1.33 → 0.1.35
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 +497 -222
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +497 -222
- 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";
|
|
@@ -2731,6 +2859,54 @@ var resolveTimeoutMs = (value, fallbackMs) => {
|
|
|
2731
2859
|
}
|
|
2732
2860
|
return Math.floor(value);
|
|
2733
2861
|
};
|
|
2862
|
+
var envTruthy2 = (value) => {
|
|
2863
|
+
if (!value) return false;
|
|
2864
|
+
const normalized = value.trim().toLowerCase();
|
|
2865
|
+
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
|
|
2866
|
+
};
|
|
2867
|
+
var WS_EVENT_TRACE_ENABLED = envTruthy2(process.env.CODELIA_DEBUG);
|
|
2868
|
+
var WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS = 5e3;
|
|
2869
|
+
var wsEventTraceStateBySource = /* @__PURE__ */ new Map();
|
|
2870
|
+
var getWsEventTraceState = (source) => {
|
|
2871
|
+
const existing = wsEventTraceStateBySource.get(source);
|
|
2872
|
+
if (existing) {
|
|
2873
|
+
return existing;
|
|
2874
|
+
}
|
|
2875
|
+
const created = {
|
|
2876
|
+
lastLoggedAt: 0,
|
|
2877
|
+
suppressed: 0,
|
|
2878
|
+
lastEventType: ""
|
|
2879
|
+
};
|
|
2880
|
+
wsEventTraceStateBySource.set(source, created);
|
|
2881
|
+
return created;
|
|
2882
|
+
};
|
|
2883
|
+
var logWsEventTrace = (source, eventType, resetResponseTimeout) => {
|
|
2884
|
+
if (!WS_EVENT_TRACE_ENABLED) {
|
|
2885
|
+
return;
|
|
2886
|
+
}
|
|
2887
|
+
const state = getWsEventTraceState(source);
|
|
2888
|
+
const now = Date.now();
|
|
2889
|
+
if (resetResponseTimeout) {
|
|
2890
|
+
state.lastEventType = eventType;
|
|
2891
|
+
if (state.lastLoggedAt > 0 && now - state.lastLoggedAt < WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS) {
|
|
2892
|
+
state.suppressed += 1;
|
|
2893
|
+
return;
|
|
2894
|
+
}
|
|
2895
|
+
const suppressedSuffix2 = state.suppressed > 0 ? ` suppressed=${state.suppressed}` : "";
|
|
2896
|
+
state.suppressed = 0;
|
|
2897
|
+
state.lastLoggedAt = now;
|
|
2898
|
+
console.error(
|
|
2899
|
+
`[openai.ws.debug] ts=${new Date(now).toISOString()} source=${source} event_type=${eventType} reset_response_timeout=1${suppressedSuffix2}`
|
|
2900
|
+
);
|
|
2901
|
+
return;
|
|
2902
|
+
}
|
|
2903
|
+
const suppressedSuffix = state.suppressed > 0 ? ` suppressed=${state.suppressed} last_progress_event_type=${state.lastEventType}` : "";
|
|
2904
|
+
state.suppressed = 0;
|
|
2905
|
+
state.lastLoggedAt = now;
|
|
2906
|
+
console.error(
|
|
2907
|
+
`[openai.ws.debug] ts=${new Date(now).toISOString()} source=${source} event_type=${eventType} reset_response_timeout=0${suppressedSuffix}`
|
|
2908
|
+
);
|
|
2909
|
+
};
|
|
2734
2910
|
var WsResponseError = class extends Error {
|
|
2735
2911
|
constructor(message, code) {
|
|
2736
2912
|
super(message);
|
|
@@ -2777,197 +2953,258 @@ var OpenAiWsTransport = class {
|
|
|
2777
2953
|
}
|
|
2778
2954
|
try {
|
|
2779
2955
|
await this.waitForWsOpen(ws, args.signal);
|
|
2780
|
-
let settled = false;
|
|
2781
|
-
let rejectResponsePromise;
|
|
2782
2956
|
const closeWs = () => {
|
|
2783
2957
|
this.closeWithoutMaskingPrimaryError(ws, "done");
|
|
2784
2958
|
};
|
|
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
|
-
|
|
2959
|
+
const responseWaiter = this.createResponsePromise({
|
|
2960
|
+
ws,
|
|
2961
|
+
signal: args.signal,
|
|
2962
|
+
closeWs
|
|
2963
|
+
});
|
|
2964
|
+
const responseCreateEvent = {
|
|
2965
|
+
type: "response.create",
|
|
2966
|
+
...args.request
|
|
2967
|
+
};
|
|
2968
|
+
if (!responseWaiter.isSettled()) {
|
|
2969
|
+
try {
|
|
2970
|
+
ws.send(responseCreateEvent);
|
|
2971
|
+
} catch (error) {
|
|
2972
|
+
responseWaiter.rejectResponsePromise(error);
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
const response = await responseWaiter.responsePromise;
|
|
2976
|
+
return { response, ws };
|
|
2977
|
+
} catch (error) {
|
|
2978
|
+
if (ownsWs) {
|
|
2979
|
+
this.closeWithoutMaskingPrimaryError(ws, "failed", error);
|
|
2980
|
+
}
|
|
2981
|
+
throw error;
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
createResponsePromise(args) {
|
|
2985
|
+
const { ws, signal, closeWs } = args;
|
|
2986
|
+
let settled = false;
|
|
2987
|
+
let rejectResponsePromise;
|
|
2988
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
2989
|
+
let onAbort;
|
|
2990
|
+
const nativeSocket = this.getNativeSocket(ws);
|
|
2991
|
+
let removeNativeClose;
|
|
2992
|
+
let removeNativeError;
|
|
2993
|
+
let removeNativeMessage;
|
|
2994
|
+
let timeout;
|
|
2995
|
+
let terminalDoneGraceTimer;
|
|
2996
|
+
let terminalCompletedWithoutUsage = null;
|
|
2997
|
+
const clearDoneGraceTimer = () => {
|
|
2998
|
+
if (terminalDoneGraceTimer) {
|
|
2999
|
+
clearTimeout(terminalDoneGraceTimer);
|
|
3000
|
+
terminalDoneGraceTimer = void 0;
|
|
3001
|
+
}
|
|
3002
|
+
};
|
|
3003
|
+
const teardown = () => {
|
|
3004
|
+
if (ws.off) {
|
|
3005
|
+
ws.off("error", onError);
|
|
3006
|
+
ws.off("event", onEvent);
|
|
3007
|
+
ws.off("response.failed", onResponseFailed);
|
|
3008
|
+
ws.off("response.completed", onResponseCompleted);
|
|
3009
|
+
ws.off("response.done", onResponseDone);
|
|
3010
|
+
ws.off("close", onClose);
|
|
3011
|
+
}
|
|
3012
|
+
if (timeout) {
|
|
3013
|
+
clearTimeout(timeout);
|
|
3014
|
+
}
|
|
3015
|
+
clearDoneGraceTimer();
|
|
3016
|
+
removeNativeClose?.();
|
|
3017
|
+
removeNativeError?.();
|
|
3018
|
+
removeNativeMessage?.();
|
|
3019
|
+
if (signal && onAbort) {
|
|
3020
|
+
signal.removeEventListener("abort", onAbort);
|
|
3021
|
+
}
|
|
3022
|
+
};
|
|
3023
|
+
const resolveOnce = (response) => {
|
|
3024
|
+
if (settled) return;
|
|
3025
|
+
settled = true;
|
|
3026
|
+
teardown();
|
|
3027
|
+
resolve(response);
|
|
3028
|
+
};
|
|
3029
|
+
const rejectOnce = (error) => {
|
|
3030
|
+
if (settled) return;
|
|
3031
|
+
settled = true;
|
|
3032
|
+
teardown();
|
|
3033
|
+
reject(error);
|
|
3034
|
+
};
|
|
3035
|
+
const scheduleDoneGraceTimer = () => {
|
|
3036
|
+
if (terminalDoneGraceTimer || settled) {
|
|
3037
|
+
return;
|
|
3038
|
+
}
|
|
3039
|
+
terminalDoneGraceTimer = setTimeout(() => {
|
|
3040
|
+
terminalDoneGraceTimer = void 0;
|
|
3041
|
+
if (settled || !terminalCompletedWithoutUsage) {
|
|
3042
|
+
return;
|
|
2821
3043
|
}
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
3044
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3045
|
+
}, WS_TERMINAL_DONE_GRACE_MS);
|
|
3046
|
+
};
|
|
3047
|
+
const resetResponseTimeout = () => {
|
|
3048
|
+
if (settled) return;
|
|
3049
|
+
if (timeout) {
|
|
3050
|
+
clearTimeout(timeout);
|
|
3051
|
+
}
|
|
3052
|
+
timeout = setTimeout(() => {
|
|
2830
3053
|
if (settled) return;
|
|
3054
|
+
if (terminalCompletedWithoutUsage) {
|
|
3055
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3056
|
+
return;
|
|
3057
|
+
}
|
|
2831
3058
|
settled = true;
|
|
2832
3059
|
teardown();
|
|
2833
|
-
reject(
|
|
2834
|
-
};
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
3060
|
+
reject(new Error("openai websocket response timeout"));
|
|
3061
|
+
}, this.wsResponseIdleTimeoutMs);
|
|
3062
|
+
};
|
|
3063
|
+
rejectResponsePromise = rejectOnce;
|
|
3064
|
+
const onError = (error) => {
|
|
3065
|
+
rejectOnce(error);
|
|
3066
|
+
};
|
|
3067
|
+
const resolveFromTerminalEvent = (event, type) => {
|
|
3068
|
+
const response = event?.response;
|
|
3069
|
+
if (response && typeof response === "object") {
|
|
3070
|
+
if (type === "response.completed" && !this.hasUsageTotals(response)) {
|
|
3071
|
+
terminalCompletedWithoutUsage = response;
|
|
3072
|
+
scheduleDoneGraceTimer();
|
|
2843
3073
|
return;
|
|
2844
3074
|
}
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
}
|
|
2849
|
-
|
|
2850
|
-
|
|
3075
|
+
clearDoneGraceTimer();
|
|
3076
|
+
resolveOnce(response);
|
|
3077
|
+
return;
|
|
3078
|
+
}
|
|
3079
|
+
if (type === "response.done" && terminalCompletedWithoutUsage) {
|
|
3080
|
+
clearDoneGraceTimer();
|
|
3081
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3082
|
+
return;
|
|
3083
|
+
}
|
|
3084
|
+
rejectOnce(
|
|
3085
|
+
new Error(`openai websocket ${type} missing response payload`)
|
|
3086
|
+
);
|
|
3087
|
+
};
|
|
3088
|
+
const onEvent = (event) => {
|
|
3089
|
+
const eventType = this.extractResponsesEventType(event);
|
|
3090
|
+
if (!eventType) {
|
|
3091
|
+
return;
|
|
3092
|
+
}
|
|
3093
|
+
if (eventType === "response.failed") {
|
|
3094
|
+
logWsEventTrace("sdk", eventType, false);
|
|
3095
|
+
onResponseFailed(event);
|
|
3096
|
+
return;
|
|
3097
|
+
}
|
|
3098
|
+
if (eventType === "response.completed") {
|
|
3099
|
+
logWsEventTrace("sdk", eventType, false);
|
|
3100
|
+
onResponseCompleted(event);
|
|
3101
|
+
return;
|
|
3102
|
+
}
|
|
3103
|
+
if (eventType === "response.done") {
|
|
3104
|
+
logWsEventTrace("sdk", eventType, false);
|
|
3105
|
+
onResponseDone(event);
|
|
3106
|
+
return;
|
|
3107
|
+
}
|
|
3108
|
+
const shouldReset = this.shouldExtendResponseIdleTimeout(eventType);
|
|
3109
|
+
logWsEventTrace("sdk", eventType, shouldReset);
|
|
3110
|
+
if (shouldReset) {
|
|
3111
|
+
resetResponseTimeout();
|
|
3112
|
+
}
|
|
3113
|
+
};
|
|
3114
|
+
const onResponseFailed = (event) => {
|
|
3115
|
+
const failed = event;
|
|
3116
|
+
const message = failed.response?.error?.message;
|
|
3117
|
+
const code = failed.response?.error?.code ?? void 0;
|
|
3118
|
+
rejectOnce(
|
|
3119
|
+
new WsResponseError(
|
|
3120
|
+
message || "openai websocket response failed",
|
|
3121
|
+
typeof code === "string" ? code : void 0
|
|
3122
|
+
)
|
|
3123
|
+
);
|
|
3124
|
+
};
|
|
3125
|
+
const onResponseCompleted = (event) => {
|
|
3126
|
+
resolveFromTerminalEvent(event, "response.completed");
|
|
3127
|
+
};
|
|
3128
|
+
const onResponseDone = (event) => {
|
|
3129
|
+
resolveFromTerminalEvent(event, "response.done");
|
|
3130
|
+
};
|
|
3131
|
+
const onClose = (event) => {
|
|
3132
|
+
const closeCode = this.extractWsCloseCode(event);
|
|
3133
|
+
rejectOnce(
|
|
3134
|
+
new Error(
|
|
3135
|
+
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
3136
|
+
)
|
|
3137
|
+
);
|
|
3138
|
+
};
|
|
3139
|
+
ws.on("error", onError);
|
|
3140
|
+
ws.on("event", onEvent);
|
|
3141
|
+
ws.on("response.failed", onResponseFailed);
|
|
3142
|
+
ws.on("response.completed", onResponseCompleted);
|
|
3143
|
+
ws.on("response.done", onResponseDone);
|
|
3144
|
+
ws.on("close", onClose);
|
|
3145
|
+
removeNativeClose = this.addNativeSocketListener(
|
|
3146
|
+
nativeSocket,
|
|
3147
|
+
"close",
|
|
3148
|
+
(...events) => {
|
|
3149
|
+
onClose(events[0]);
|
|
3150
|
+
}
|
|
3151
|
+
);
|
|
3152
|
+
removeNativeError = this.addNativeSocketListener(
|
|
3153
|
+
nativeSocket,
|
|
3154
|
+
"error",
|
|
3155
|
+
(error) => {
|
|
3156
|
+
onError(error);
|
|
3157
|
+
}
|
|
3158
|
+
);
|
|
3159
|
+
removeNativeMessage = this.addNativeSocketListener(
|
|
3160
|
+
nativeSocket,
|
|
3161
|
+
"message",
|
|
3162
|
+
(...events) => {
|
|
3163
|
+
const eventType = this.extractNativeResponsesEventType(events[0]);
|
|
2851
3164
|
if (!eventType) {
|
|
2852
3165
|
return;
|
|
2853
3166
|
}
|
|
2854
3167
|
if (eventType === "response.failed") {
|
|
2855
|
-
|
|
3168
|
+
logWsEventTrace("native", eventType, false);
|
|
3169
|
+
onResponseFailed(events[0]);
|
|
2856
3170
|
return;
|
|
2857
3171
|
}
|
|
2858
3172
|
if (eventType === "response.completed") {
|
|
2859
|
-
|
|
3173
|
+
logWsEventTrace("native", eventType, false);
|
|
3174
|
+
onResponseCompleted(events[0]);
|
|
2860
3175
|
return;
|
|
2861
3176
|
}
|
|
2862
3177
|
if (eventType === "response.done") {
|
|
2863
|
-
|
|
3178
|
+
logWsEventTrace("native", eventType, false);
|
|
3179
|
+
onResponseDone(events[0]);
|
|
2864
3180
|
return;
|
|
2865
3181
|
}
|
|
2866
|
-
|
|
3182
|
+
const shouldReset = this.shouldExtendResponseIdleTimeout(eventType);
|
|
3183
|
+
logWsEventTrace("native", eventType, shouldReset);
|
|
3184
|
+
if (shouldReset) {
|
|
2867
3185
|
resetResponseTimeout();
|
|
2868
3186
|
}
|
|
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
3187
|
}
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
} catch (error) {
|
|
2960
|
-
rejectResponsePromise?.(error);
|
|
3188
|
+
);
|
|
3189
|
+
if (signal) {
|
|
3190
|
+
onAbort = () => {
|
|
3191
|
+
rejectOnce(new Error("openai websocket request aborted"));
|
|
3192
|
+
closeWs();
|
|
3193
|
+
};
|
|
3194
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
3195
|
+
if (signal.aborted) {
|
|
3196
|
+
onAbort();
|
|
2961
3197
|
}
|
|
2962
3198
|
}
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
3199
|
+
resetResponseTimeout();
|
|
3200
|
+
});
|
|
3201
|
+
return {
|
|
3202
|
+
responsePromise,
|
|
3203
|
+
isSettled: () => settled,
|
|
3204
|
+
rejectResponsePromise: (error) => {
|
|
3205
|
+
rejectResponsePromise?.(error);
|
|
2968
3206
|
}
|
|
2969
|
-
|
|
2970
|
-
}
|
|
3207
|
+
};
|
|
2971
3208
|
}
|
|
2972
3209
|
closeSafely(ws, reason) {
|
|
2973
3210
|
if (!ws) {
|
|
@@ -3135,6 +3372,9 @@ var OpenAiWsTransport = class {
|
|
|
3135
3372
|
}
|
|
3136
3373
|
return eventType !== "response.failed" && eventType !== "response.completed" && eventType !== "response.done";
|
|
3137
3374
|
}
|
|
3375
|
+
hasUsageTotals(response) {
|
|
3376
|
+
return typeof response.usage?.input_tokens === "number" && typeof response.usage?.output_tokens === "number" && typeof response.usage?.total_tokens === "number";
|
|
3377
|
+
}
|
|
3138
3378
|
extractNativeResponsesEventType(message) {
|
|
3139
3379
|
const directType = this.extractResponsesEventType(message);
|
|
3140
3380
|
if (directType) {
|
|
@@ -3323,16 +3563,13 @@ var OpenAiWsTransport = class {
|
|
|
3323
3563
|
addSocketListener("error", (error) => {
|
|
3324
3564
|
settleReject(error);
|
|
3325
3565
|
});
|
|
3326
|
-
addSocketListener(
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
});
|
|
3334
|
-
}
|
|
3335
|
-
);
|
|
3566
|
+
addSocketListener("unexpected-response", (...args) => {
|
|
3567
|
+
void this.createUnexpectedResponseError(args[1]).then((error) => {
|
|
3568
|
+
settleReject(error);
|
|
3569
|
+
}).catch((error) => {
|
|
3570
|
+
settleReject(error);
|
|
3571
|
+
});
|
|
3572
|
+
});
|
|
3336
3573
|
addSocketListener("close", (code) => {
|
|
3337
3574
|
const closeCode = typeof code === "number" ? ` code=${code}` : "";
|
|
3338
3575
|
settleReject(
|
|
@@ -3371,6 +3608,7 @@ var ChatOpenAI = class {
|
|
|
3371
3608
|
model;
|
|
3372
3609
|
client;
|
|
3373
3610
|
defaultReasoningEffort;
|
|
3611
|
+
reasoningLevelMeta;
|
|
3374
3612
|
defaultTextVerbosity;
|
|
3375
3613
|
websocketMode;
|
|
3376
3614
|
websocketApiVersion;
|
|
@@ -3384,6 +3622,11 @@ var ChatOpenAI = class {
|
|
|
3384
3622
|
this.client = options.client ?? new OpenAI(options.clientOptions);
|
|
3385
3623
|
this.model = options.model ?? DEFAULT_MODEL2;
|
|
3386
3624
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT;
|
|
3625
|
+
this.reasoningLevelMeta = {
|
|
3626
|
+
requested: options.reasoningLevelRequested,
|
|
3627
|
+
applied: options.reasoningLevelApplied,
|
|
3628
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
3629
|
+
};
|
|
3387
3630
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
3388
3631
|
this.websocketMode = options.websocketMode ?? "off";
|
|
3389
3632
|
this.websocketApiVersion = options.websocketApiVersion ?? "v2";
|
|
@@ -3470,9 +3713,24 @@ var ChatOpenAI = class {
|
|
|
3470
3713
|
fallback_used: transportResult.fallbackUsed,
|
|
3471
3714
|
chain_reset: transportResult.chainReset,
|
|
3472
3715
|
ws_reconnect_count: this.wsReconnectCount,
|
|
3473
|
-
ws_input_mode: transportResult.wsInputMode
|
|
3716
|
+
ws_input_mode: transportResult.wsInputMode,
|
|
3717
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
3718
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
3719
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
3474
3720
|
});
|
|
3475
3721
|
}
|
|
3722
|
+
onHistoryCompacted(context) {
|
|
3723
|
+
const sessionKey = context?.sessionKey;
|
|
3724
|
+
if (sessionKey) {
|
|
3725
|
+
this.clearWsSessionState(sessionKey, "history_compacted");
|
|
3726
|
+
this.wsDisabledUntilBySessionKey.delete(sessionKey);
|
|
3727
|
+
return;
|
|
3728
|
+
}
|
|
3729
|
+
for (const key of Array.from(this.wsStateBySessionKey.keys())) {
|
|
3730
|
+
this.clearWsSessionState(key, "history_compacted");
|
|
3731
|
+
}
|
|
3732
|
+
this.wsDisabledUntilBySessionKey.clear();
|
|
3733
|
+
}
|
|
3476
3734
|
async invokeWithTransport(args) {
|
|
3477
3735
|
this.evictIdleWsSessionState();
|
|
3478
3736
|
if (this.websocketMode === "off") {
|
|
@@ -3520,7 +3778,7 @@ var ChatOpenAI = class {
|
|
|
3520
3778
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3521
3779
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3522
3780
|
let wsInputMode = "full_no_previous";
|
|
3523
|
-
if (
|
|
3781
|
+
if (wsState.previousResponseId) {
|
|
3524
3782
|
if (canUsePreviousResponseId) {
|
|
3525
3783
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3526
3784
|
wsInputMode = "empty";
|
|
@@ -3582,10 +3840,12 @@ var ChatOpenAI = class {
|
|
|
3582
3840
|
} catch (error) {
|
|
3583
3841
|
let failure = error;
|
|
3584
3842
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3585
|
-
const retryWsInputMode =
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3843
|
+
const retryWsInputMode = wsState.previousResponseId ? "full_regenerated" : "full_no_previous";
|
|
3844
|
+
this.closeWsWithoutMaskingPrimaryError(
|
|
3845
|
+
wsState.ws,
|
|
3846
|
+
"retry_reset",
|
|
3847
|
+
failure
|
|
3848
|
+
);
|
|
3589
3849
|
try {
|
|
3590
3850
|
await this.debugRequestIfEnabled(
|
|
3591
3851
|
args.request,
|
|
@@ -3685,7 +3945,9 @@ var ChatOpenAI = class {
|
|
|
3685
3945
|
if (!Array.isArray(requestInput)) {
|
|
3686
3946
|
return requestInput;
|
|
3687
3947
|
}
|
|
3688
|
-
const replayItems = toResponsesInput(
|
|
3948
|
+
const replayItems = toResponsesInput(
|
|
3949
|
+
toChatInvokeCompletion2(response).messages
|
|
3950
|
+
);
|
|
3689
3951
|
if (replayItems.length === 0) {
|
|
3690
3952
|
return requestInput;
|
|
3691
3953
|
}
|
|
@@ -4211,7 +4473,7 @@ function toResponsesToolChoice2(choice) {
|
|
|
4211
4473
|
}
|
|
4212
4474
|
return { type: "function", name: choice };
|
|
4213
4475
|
}
|
|
4214
|
-
function toChatInvokeCompletion3(response) {
|
|
4476
|
+
function toChatInvokeCompletion3(response, meta) {
|
|
4215
4477
|
const usage = response.usage ? {
|
|
4216
4478
|
model: response.model,
|
|
4217
4479
|
input_tokens: response.usage.input_tokens,
|
|
@@ -4231,7 +4493,10 @@ function toChatInvokeCompletion3(response) {
|
|
|
4231
4493
|
usage,
|
|
4232
4494
|
stop_reason: response.incomplete_details?.reason ?? response.status ?? null,
|
|
4233
4495
|
provider_meta: {
|
|
4234
|
-
response_id: response.id
|
|
4496
|
+
response_id: response.id,
|
|
4497
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
4498
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
4499
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {}
|
|
4235
4500
|
}
|
|
4236
4501
|
};
|
|
4237
4502
|
}
|
|
@@ -4317,6 +4582,7 @@ var ChatOpenRouter = class {
|
|
|
4317
4582
|
model;
|
|
4318
4583
|
client;
|
|
4319
4584
|
defaultReasoningEffort;
|
|
4585
|
+
reasoningLevelMeta;
|
|
4320
4586
|
defaultTextVerbosity;
|
|
4321
4587
|
debugInvokeSeq = 0;
|
|
4322
4588
|
lastDebugRequestPayload = null;
|
|
@@ -4327,6 +4593,11 @@ var ChatOpenRouter = class {
|
|
|
4327
4593
|
});
|
|
4328
4594
|
this.model = options.model ?? DEFAULT_MODEL3;
|
|
4329
4595
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT2;
|
|
4596
|
+
this.reasoningLevelMeta = {
|
|
4597
|
+
requested: options.reasoningLevelRequested,
|
|
4598
|
+
applied: options.reasoningLevelApplied,
|
|
4599
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
4600
|
+
};
|
|
4330
4601
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
4331
4602
|
}
|
|
4332
4603
|
async ainvoke(input, context) {
|
|
@@ -4397,7 +4668,11 @@ var ChatOpenRouter = class {
|
|
|
4397
4668
|
} : void 0
|
|
4398
4669
|
).finalResponse();
|
|
4399
4670
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
4400
|
-
return toChatInvokeCompletion3(response
|
|
4671
|
+
return toChatInvokeCompletion3(response, {
|
|
4672
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
4673
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
4674
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
4675
|
+
});
|
|
4401
4676
|
}
|
|
4402
4677
|
nextDebugInvokeSeq() {
|
|
4403
4678
|
const seq = this.debugInvokeSeq + 1;
|