@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.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";
|
|
@@ -2788,6 +2916,54 @@ var resolveTimeoutMs = (value, fallbackMs) => {
|
|
|
2788
2916
|
}
|
|
2789
2917
|
return Math.floor(value);
|
|
2790
2918
|
};
|
|
2919
|
+
var envTruthy2 = (value) => {
|
|
2920
|
+
if (!value) return false;
|
|
2921
|
+
const normalized = value.trim().toLowerCase();
|
|
2922
|
+
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
|
|
2923
|
+
};
|
|
2924
|
+
var WS_EVENT_TRACE_ENABLED = envTruthy2(process.env.CODELIA_DEBUG);
|
|
2925
|
+
var WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS = 5e3;
|
|
2926
|
+
var wsEventTraceStateBySource = /* @__PURE__ */ new Map();
|
|
2927
|
+
var getWsEventTraceState = (source) => {
|
|
2928
|
+
const existing = wsEventTraceStateBySource.get(source);
|
|
2929
|
+
if (existing) {
|
|
2930
|
+
return existing;
|
|
2931
|
+
}
|
|
2932
|
+
const created = {
|
|
2933
|
+
lastLoggedAt: 0,
|
|
2934
|
+
suppressed: 0,
|
|
2935
|
+
lastEventType: ""
|
|
2936
|
+
};
|
|
2937
|
+
wsEventTraceStateBySource.set(source, created);
|
|
2938
|
+
return created;
|
|
2939
|
+
};
|
|
2940
|
+
var logWsEventTrace = (source, eventType, resetResponseTimeout) => {
|
|
2941
|
+
if (!WS_EVENT_TRACE_ENABLED) {
|
|
2942
|
+
return;
|
|
2943
|
+
}
|
|
2944
|
+
const state = getWsEventTraceState(source);
|
|
2945
|
+
const now = Date.now();
|
|
2946
|
+
if (resetResponseTimeout) {
|
|
2947
|
+
state.lastEventType = eventType;
|
|
2948
|
+
if (state.lastLoggedAt > 0 && now - state.lastLoggedAt < WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS) {
|
|
2949
|
+
state.suppressed += 1;
|
|
2950
|
+
return;
|
|
2951
|
+
}
|
|
2952
|
+
const suppressedSuffix2 = state.suppressed > 0 ? ` suppressed=${state.suppressed}` : "";
|
|
2953
|
+
state.suppressed = 0;
|
|
2954
|
+
state.lastLoggedAt = now;
|
|
2955
|
+
console.error(
|
|
2956
|
+
`[openai.ws.debug] ts=${new Date(now).toISOString()} source=${source} event_type=${eventType} reset_response_timeout=1${suppressedSuffix2}`
|
|
2957
|
+
);
|
|
2958
|
+
return;
|
|
2959
|
+
}
|
|
2960
|
+
const suppressedSuffix = state.suppressed > 0 ? ` suppressed=${state.suppressed} last_progress_event_type=${state.lastEventType}` : "";
|
|
2961
|
+
state.suppressed = 0;
|
|
2962
|
+
state.lastLoggedAt = now;
|
|
2963
|
+
console.error(
|
|
2964
|
+
`[openai.ws.debug] ts=${new Date(now).toISOString()} source=${source} event_type=${eventType} reset_response_timeout=0${suppressedSuffix}`
|
|
2965
|
+
);
|
|
2966
|
+
};
|
|
2791
2967
|
var WsResponseError = class extends Error {
|
|
2792
2968
|
constructor(message, code) {
|
|
2793
2969
|
super(message);
|
|
@@ -2834,197 +3010,258 @@ var OpenAiWsTransport = class {
|
|
|
2834
3010
|
}
|
|
2835
3011
|
try {
|
|
2836
3012
|
await this.waitForWsOpen(ws, args.signal);
|
|
2837
|
-
let settled = false;
|
|
2838
|
-
let rejectResponsePromise;
|
|
2839
3013
|
const closeWs = () => {
|
|
2840
3014
|
this.closeWithoutMaskingPrimaryError(ws, "done");
|
|
2841
3015
|
};
|
|
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
|
-
|
|
3016
|
+
const responseWaiter = this.createResponsePromise({
|
|
3017
|
+
ws,
|
|
3018
|
+
signal: args.signal,
|
|
3019
|
+
closeWs
|
|
3020
|
+
});
|
|
3021
|
+
const responseCreateEvent = {
|
|
3022
|
+
type: "response.create",
|
|
3023
|
+
...args.request
|
|
3024
|
+
};
|
|
3025
|
+
if (!responseWaiter.isSettled()) {
|
|
3026
|
+
try {
|
|
3027
|
+
ws.send(responseCreateEvent);
|
|
3028
|
+
} catch (error) {
|
|
3029
|
+
responseWaiter.rejectResponsePromise(error);
|
|
3030
|
+
}
|
|
3031
|
+
}
|
|
3032
|
+
const response = await responseWaiter.responsePromise;
|
|
3033
|
+
return { response, ws };
|
|
3034
|
+
} catch (error) {
|
|
3035
|
+
if (ownsWs) {
|
|
3036
|
+
this.closeWithoutMaskingPrimaryError(ws, "failed", error);
|
|
3037
|
+
}
|
|
3038
|
+
throw error;
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
createResponsePromise(args) {
|
|
3042
|
+
const { ws, signal, closeWs } = args;
|
|
3043
|
+
let settled = false;
|
|
3044
|
+
let rejectResponsePromise;
|
|
3045
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
3046
|
+
let onAbort;
|
|
3047
|
+
const nativeSocket = this.getNativeSocket(ws);
|
|
3048
|
+
let removeNativeClose;
|
|
3049
|
+
let removeNativeError;
|
|
3050
|
+
let removeNativeMessage;
|
|
3051
|
+
let timeout;
|
|
3052
|
+
let terminalDoneGraceTimer;
|
|
3053
|
+
let terminalCompletedWithoutUsage = null;
|
|
3054
|
+
const clearDoneGraceTimer = () => {
|
|
3055
|
+
if (terminalDoneGraceTimer) {
|
|
3056
|
+
clearTimeout(terminalDoneGraceTimer);
|
|
3057
|
+
terminalDoneGraceTimer = void 0;
|
|
3058
|
+
}
|
|
3059
|
+
};
|
|
3060
|
+
const teardown = () => {
|
|
3061
|
+
if (ws.off) {
|
|
3062
|
+
ws.off("error", onError);
|
|
3063
|
+
ws.off("event", onEvent);
|
|
3064
|
+
ws.off("response.failed", onResponseFailed);
|
|
3065
|
+
ws.off("response.completed", onResponseCompleted);
|
|
3066
|
+
ws.off("response.done", onResponseDone);
|
|
3067
|
+
ws.off("close", onClose);
|
|
3068
|
+
}
|
|
3069
|
+
if (timeout) {
|
|
3070
|
+
clearTimeout(timeout);
|
|
3071
|
+
}
|
|
3072
|
+
clearDoneGraceTimer();
|
|
3073
|
+
removeNativeClose?.();
|
|
3074
|
+
removeNativeError?.();
|
|
3075
|
+
removeNativeMessage?.();
|
|
3076
|
+
if (signal && onAbort) {
|
|
3077
|
+
signal.removeEventListener("abort", onAbort);
|
|
3078
|
+
}
|
|
3079
|
+
};
|
|
3080
|
+
const resolveOnce = (response) => {
|
|
3081
|
+
if (settled) return;
|
|
3082
|
+
settled = true;
|
|
3083
|
+
teardown();
|
|
3084
|
+
resolve(response);
|
|
3085
|
+
};
|
|
3086
|
+
const rejectOnce = (error) => {
|
|
3087
|
+
if (settled) return;
|
|
3088
|
+
settled = true;
|
|
3089
|
+
teardown();
|
|
3090
|
+
reject(error);
|
|
3091
|
+
};
|
|
3092
|
+
const scheduleDoneGraceTimer = () => {
|
|
3093
|
+
if (terminalDoneGraceTimer || settled) {
|
|
3094
|
+
return;
|
|
3095
|
+
}
|
|
3096
|
+
terminalDoneGraceTimer = setTimeout(() => {
|
|
3097
|
+
terminalDoneGraceTimer = void 0;
|
|
3098
|
+
if (settled || !terminalCompletedWithoutUsage) {
|
|
3099
|
+
return;
|
|
2878
3100
|
}
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
3101
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3102
|
+
}, WS_TERMINAL_DONE_GRACE_MS);
|
|
3103
|
+
};
|
|
3104
|
+
const resetResponseTimeout = () => {
|
|
3105
|
+
if (settled) return;
|
|
3106
|
+
if (timeout) {
|
|
3107
|
+
clearTimeout(timeout);
|
|
3108
|
+
}
|
|
3109
|
+
timeout = setTimeout(() => {
|
|
2887
3110
|
if (settled) return;
|
|
3111
|
+
if (terminalCompletedWithoutUsage) {
|
|
3112
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3113
|
+
return;
|
|
3114
|
+
}
|
|
2888
3115
|
settled = true;
|
|
2889
3116
|
teardown();
|
|
2890
|
-
reject(
|
|
2891
|
-
};
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
3117
|
+
reject(new Error("openai websocket response timeout"));
|
|
3118
|
+
}, this.wsResponseIdleTimeoutMs);
|
|
3119
|
+
};
|
|
3120
|
+
rejectResponsePromise = rejectOnce;
|
|
3121
|
+
const onError = (error) => {
|
|
3122
|
+
rejectOnce(error);
|
|
3123
|
+
};
|
|
3124
|
+
const resolveFromTerminalEvent = (event, type) => {
|
|
3125
|
+
const response = event?.response;
|
|
3126
|
+
if (response && typeof response === "object") {
|
|
3127
|
+
if (type === "response.completed" && !this.hasUsageTotals(response)) {
|
|
3128
|
+
terminalCompletedWithoutUsage = response;
|
|
3129
|
+
scheduleDoneGraceTimer();
|
|
2900
3130
|
return;
|
|
2901
3131
|
}
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
}
|
|
2906
|
-
|
|
2907
|
-
|
|
3132
|
+
clearDoneGraceTimer();
|
|
3133
|
+
resolveOnce(response);
|
|
3134
|
+
return;
|
|
3135
|
+
}
|
|
3136
|
+
if (type === "response.done" && terminalCompletedWithoutUsage) {
|
|
3137
|
+
clearDoneGraceTimer();
|
|
3138
|
+
resolveOnce(terminalCompletedWithoutUsage);
|
|
3139
|
+
return;
|
|
3140
|
+
}
|
|
3141
|
+
rejectOnce(
|
|
3142
|
+
new Error(`openai websocket ${type} missing response payload`)
|
|
3143
|
+
);
|
|
3144
|
+
};
|
|
3145
|
+
const onEvent = (event) => {
|
|
3146
|
+
const eventType = this.extractResponsesEventType(event);
|
|
3147
|
+
if (!eventType) {
|
|
3148
|
+
return;
|
|
3149
|
+
}
|
|
3150
|
+
if (eventType === "response.failed") {
|
|
3151
|
+
logWsEventTrace("sdk", eventType, false);
|
|
3152
|
+
onResponseFailed(event);
|
|
3153
|
+
return;
|
|
3154
|
+
}
|
|
3155
|
+
if (eventType === "response.completed") {
|
|
3156
|
+
logWsEventTrace("sdk", eventType, false);
|
|
3157
|
+
onResponseCompleted(event);
|
|
3158
|
+
return;
|
|
3159
|
+
}
|
|
3160
|
+
if (eventType === "response.done") {
|
|
3161
|
+
logWsEventTrace("sdk", eventType, false);
|
|
3162
|
+
onResponseDone(event);
|
|
3163
|
+
return;
|
|
3164
|
+
}
|
|
3165
|
+
const shouldReset = this.shouldExtendResponseIdleTimeout(eventType);
|
|
3166
|
+
logWsEventTrace("sdk", eventType, shouldReset);
|
|
3167
|
+
if (shouldReset) {
|
|
3168
|
+
resetResponseTimeout();
|
|
3169
|
+
}
|
|
3170
|
+
};
|
|
3171
|
+
const onResponseFailed = (event) => {
|
|
3172
|
+
const failed = event;
|
|
3173
|
+
const message = failed.response?.error?.message;
|
|
3174
|
+
const code = failed.response?.error?.code ?? void 0;
|
|
3175
|
+
rejectOnce(
|
|
3176
|
+
new WsResponseError(
|
|
3177
|
+
message || "openai websocket response failed",
|
|
3178
|
+
typeof code === "string" ? code : void 0
|
|
3179
|
+
)
|
|
3180
|
+
);
|
|
3181
|
+
};
|
|
3182
|
+
const onResponseCompleted = (event) => {
|
|
3183
|
+
resolveFromTerminalEvent(event, "response.completed");
|
|
3184
|
+
};
|
|
3185
|
+
const onResponseDone = (event) => {
|
|
3186
|
+
resolveFromTerminalEvent(event, "response.done");
|
|
3187
|
+
};
|
|
3188
|
+
const onClose = (event) => {
|
|
3189
|
+
const closeCode = this.extractWsCloseCode(event);
|
|
3190
|
+
rejectOnce(
|
|
3191
|
+
new Error(
|
|
3192
|
+
`openai websocket closed before response${closeCode ? ` code=${closeCode}` : ""}`
|
|
3193
|
+
)
|
|
3194
|
+
);
|
|
3195
|
+
};
|
|
3196
|
+
ws.on("error", onError);
|
|
3197
|
+
ws.on("event", onEvent);
|
|
3198
|
+
ws.on("response.failed", onResponseFailed);
|
|
3199
|
+
ws.on("response.completed", onResponseCompleted);
|
|
3200
|
+
ws.on("response.done", onResponseDone);
|
|
3201
|
+
ws.on("close", onClose);
|
|
3202
|
+
removeNativeClose = this.addNativeSocketListener(
|
|
3203
|
+
nativeSocket,
|
|
3204
|
+
"close",
|
|
3205
|
+
(...events) => {
|
|
3206
|
+
onClose(events[0]);
|
|
3207
|
+
}
|
|
3208
|
+
);
|
|
3209
|
+
removeNativeError = this.addNativeSocketListener(
|
|
3210
|
+
nativeSocket,
|
|
3211
|
+
"error",
|
|
3212
|
+
(error) => {
|
|
3213
|
+
onError(error);
|
|
3214
|
+
}
|
|
3215
|
+
);
|
|
3216
|
+
removeNativeMessage = this.addNativeSocketListener(
|
|
3217
|
+
nativeSocket,
|
|
3218
|
+
"message",
|
|
3219
|
+
(...events) => {
|
|
3220
|
+
const eventType = this.extractNativeResponsesEventType(events[0]);
|
|
2908
3221
|
if (!eventType) {
|
|
2909
3222
|
return;
|
|
2910
3223
|
}
|
|
2911
3224
|
if (eventType === "response.failed") {
|
|
2912
|
-
|
|
3225
|
+
logWsEventTrace("native", eventType, false);
|
|
3226
|
+
onResponseFailed(events[0]);
|
|
2913
3227
|
return;
|
|
2914
3228
|
}
|
|
2915
3229
|
if (eventType === "response.completed") {
|
|
2916
|
-
|
|
3230
|
+
logWsEventTrace("native", eventType, false);
|
|
3231
|
+
onResponseCompleted(events[0]);
|
|
2917
3232
|
return;
|
|
2918
3233
|
}
|
|
2919
3234
|
if (eventType === "response.done") {
|
|
2920
|
-
|
|
3235
|
+
logWsEventTrace("native", eventType, false);
|
|
3236
|
+
onResponseDone(events[0]);
|
|
2921
3237
|
return;
|
|
2922
3238
|
}
|
|
2923
|
-
|
|
3239
|
+
const shouldReset = this.shouldExtendResponseIdleTimeout(eventType);
|
|
3240
|
+
logWsEventTrace("native", eventType, shouldReset);
|
|
3241
|
+
if (shouldReset) {
|
|
2924
3242
|
resetResponseTimeout();
|
|
2925
3243
|
}
|
|
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
3244
|
}
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
} catch (error) {
|
|
3017
|
-
rejectResponsePromise?.(error);
|
|
3245
|
+
);
|
|
3246
|
+
if (signal) {
|
|
3247
|
+
onAbort = () => {
|
|
3248
|
+
rejectOnce(new Error("openai websocket request aborted"));
|
|
3249
|
+
closeWs();
|
|
3250
|
+
};
|
|
3251
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
3252
|
+
if (signal.aborted) {
|
|
3253
|
+
onAbort();
|
|
3018
3254
|
}
|
|
3019
3255
|
}
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3256
|
+
resetResponseTimeout();
|
|
3257
|
+
});
|
|
3258
|
+
return {
|
|
3259
|
+
responsePromise,
|
|
3260
|
+
isSettled: () => settled,
|
|
3261
|
+
rejectResponsePromise: (error) => {
|
|
3262
|
+
rejectResponsePromise?.(error);
|
|
3025
3263
|
}
|
|
3026
|
-
|
|
3027
|
-
}
|
|
3264
|
+
};
|
|
3028
3265
|
}
|
|
3029
3266
|
closeSafely(ws, reason) {
|
|
3030
3267
|
if (!ws) {
|
|
@@ -3192,6 +3429,9 @@ var OpenAiWsTransport = class {
|
|
|
3192
3429
|
}
|
|
3193
3430
|
return eventType !== "response.failed" && eventType !== "response.completed" && eventType !== "response.done";
|
|
3194
3431
|
}
|
|
3432
|
+
hasUsageTotals(response) {
|
|
3433
|
+
return typeof response.usage?.input_tokens === "number" && typeof response.usage?.output_tokens === "number" && typeof response.usage?.total_tokens === "number";
|
|
3434
|
+
}
|
|
3195
3435
|
extractNativeResponsesEventType(message) {
|
|
3196
3436
|
const directType = this.extractResponsesEventType(message);
|
|
3197
3437
|
if (directType) {
|
|
@@ -3380,16 +3620,13 @@ var OpenAiWsTransport = class {
|
|
|
3380
3620
|
addSocketListener("error", (error) => {
|
|
3381
3621
|
settleReject(error);
|
|
3382
3622
|
});
|
|
3383
|
-
addSocketListener(
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
});
|
|
3391
|
-
}
|
|
3392
|
-
);
|
|
3623
|
+
addSocketListener("unexpected-response", (...args) => {
|
|
3624
|
+
void this.createUnexpectedResponseError(args[1]).then((error) => {
|
|
3625
|
+
settleReject(error);
|
|
3626
|
+
}).catch((error) => {
|
|
3627
|
+
settleReject(error);
|
|
3628
|
+
});
|
|
3629
|
+
});
|
|
3393
3630
|
addSocketListener("close", (code) => {
|
|
3394
3631
|
const closeCode = typeof code === "number" ? ` code=${code}` : "";
|
|
3395
3632
|
settleReject(
|
|
@@ -3428,6 +3665,7 @@ var ChatOpenAI = class {
|
|
|
3428
3665
|
model;
|
|
3429
3666
|
client;
|
|
3430
3667
|
defaultReasoningEffort;
|
|
3668
|
+
reasoningLevelMeta;
|
|
3431
3669
|
defaultTextVerbosity;
|
|
3432
3670
|
websocketMode;
|
|
3433
3671
|
websocketApiVersion;
|
|
@@ -3441,6 +3679,11 @@ var ChatOpenAI = class {
|
|
|
3441
3679
|
this.client = options.client ?? new import_openai3.default(options.clientOptions);
|
|
3442
3680
|
this.model = options.model ?? DEFAULT_MODEL2;
|
|
3443
3681
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT;
|
|
3682
|
+
this.reasoningLevelMeta = {
|
|
3683
|
+
requested: options.reasoningLevelRequested,
|
|
3684
|
+
applied: options.reasoningLevelApplied,
|
|
3685
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
3686
|
+
};
|
|
3444
3687
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
3445
3688
|
this.websocketMode = options.websocketMode ?? "off";
|
|
3446
3689
|
this.websocketApiVersion = options.websocketApiVersion ?? "v2";
|
|
@@ -3527,9 +3770,24 @@ var ChatOpenAI = class {
|
|
|
3527
3770
|
fallback_used: transportResult.fallbackUsed,
|
|
3528
3771
|
chain_reset: transportResult.chainReset,
|
|
3529
3772
|
ws_reconnect_count: this.wsReconnectCount,
|
|
3530
|
-
ws_input_mode: transportResult.wsInputMode
|
|
3773
|
+
ws_input_mode: transportResult.wsInputMode,
|
|
3774
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
3775
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
3776
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
3531
3777
|
});
|
|
3532
3778
|
}
|
|
3779
|
+
onHistoryCompacted(context) {
|
|
3780
|
+
const sessionKey = context?.sessionKey;
|
|
3781
|
+
if (sessionKey) {
|
|
3782
|
+
this.clearWsSessionState(sessionKey, "history_compacted");
|
|
3783
|
+
this.wsDisabledUntilBySessionKey.delete(sessionKey);
|
|
3784
|
+
return;
|
|
3785
|
+
}
|
|
3786
|
+
for (const key of Array.from(this.wsStateBySessionKey.keys())) {
|
|
3787
|
+
this.clearWsSessionState(key, "history_compacted");
|
|
3788
|
+
}
|
|
3789
|
+
this.wsDisabledUntilBySessionKey.clear();
|
|
3790
|
+
}
|
|
3533
3791
|
async invokeWithTransport(args) {
|
|
3534
3792
|
this.evictIdleWsSessionState();
|
|
3535
3793
|
if (this.websocketMode === "off") {
|
|
@@ -3577,7 +3835,7 @@ var ChatOpenAI = class {
|
|
|
3577
3835
|
const canUsePreviousResponseId = this.websocketApiVersion === "v2" && Boolean(wsState.previousResponseId) && !shouldResetChain && incrementalInput !== null;
|
|
3578
3836
|
const chainReset = shouldResetChain || Boolean(wsState.previousResponseId) && !canUsePreviousResponseId;
|
|
3579
3837
|
let wsInputMode = "full_no_previous";
|
|
3580
|
-
if (
|
|
3838
|
+
if (wsState.previousResponseId) {
|
|
3581
3839
|
if (canUsePreviousResponseId) {
|
|
3582
3840
|
if (Array.isArray(incrementalInput) && incrementalInput.length === 0) {
|
|
3583
3841
|
wsInputMode = "empty";
|
|
@@ -3639,10 +3897,12 @@ var ChatOpenAI = class {
|
|
|
3639
3897
|
} catch (error) {
|
|
3640
3898
|
let failure = error;
|
|
3641
3899
|
if (this.websocketMode === "on" && this.isWsReconnectRetryableError(failure)) {
|
|
3642
|
-
const retryWsInputMode =
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3900
|
+
const retryWsInputMode = wsState.previousResponseId ? "full_regenerated" : "full_no_previous";
|
|
3901
|
+
this.closeWsWithoutMaskingPrimaryError(
|
|
3902
|
+
wsState.ws,
|
|
3903
|
+
"retry_reset",
|
|
3904
|
+
failure
|
|
3905
|
+
);
|
|
3646
3906
|
try {
|
|
3647
3907
|
await this.debugRequestIfEnabled(
|
|
3648
3908
|
args.request,
|
|
@@ -3742,7 +4002,9 @@ var ChatOpenAI = class {
|
|
|
3742
4002
|
if (!Array.isArray(requestInput)) {
|
|
3743
4003
|
return requestInput;
|
|
3744
4004
|
}
|
|
3745
|
-
const replayItems = toResponsesInput(
|
|
4005
|
+
const replayItems = toResponsesInput(
|
|
4006
|
+
toChatInvokeCompletion2(response).messages
|
|
4007
|
+
);
|
|
3746
4008
|
if (replayItems.length === 0) {
|
|
3747
4009
|
return requestInput;
|
|
3748
4010
|
}
|
|
@@ -4268,7 +4530,7 @@ function toResponsesToolChoice2(choice) {
|
|
|
4268
4530
|
}
|
|
4269
4531
|
return { type: "function", name: choice };
|
|
4270
4532
|
}
|
|
4271
|
-
function toChatInvokeCompletion3(response) {
|
|
4533
|
+
function toChatInvokeCompletion3(response, meta) {
|
|
4272
4534
|
const usage = response.usage ? {
|
|
4273
4535
|
model: response.model,
|
|
4274
4536
|
input_tokens: response.usage.input_tokens,
|
|
@@ -4288,7 +4550,10 @@ function toChatInvokeCompletion3(response) {
|
|
|
4288
4550
|
usage,
|
|
4289
4551
|
stop_reason: response.incomplete_details?.reason ?? response.status ?? null,
|
|
4290
4552
|
provider_meta: {
|
|
4291
|
-
response_id: response.id
|
|
4553
|
+
response_id: response.id,
|
|
4554
|
+
...typeof meta?.reasoning_requested === "string" ? { reasoning_requested: meta.reasoning_requested } : {},
|
|
4555
|
+
...typeof meta?.reasoning_applied === "string" ? { reasoning_applied: meta.reasoning_applied } : {},
|
|
4556
|
+
...typeof meta?.reasoning_fallback === "boolean" ? { reasoning_fallback: meta.reasoning_fallback } : {}
|
|
4292
4557
|
}
|
|
4293
4558
|
};
|
|
4294
4559
|
}
|
|
@@ -4374,6 +4639,7 @@ var ChatOpenRouter = class {
|
|
|
4374
4639
|
model;
|
|
4375
4640
|
client;
|
|
4376
4641
|
defaultReasoningEffort;
|
|
4642
|
+
reasoningLevelMeta;
|
|
4377
4643
|
defaultTextVerbosity;
|
|
4378
4644
|
debugInvokeSeq = 0;
|
|
4379
4645
|
lastDebugRequestPayload = null;
|
|
@@ -4384,6 +4650,11 @@ var ChatOpenRouter = class {
|
|
|
4384
4650
|
});
|
|
4385
4651
|
this.model = options.model ?? DEFAULT_MODEL3;
|
|
4386
4652
|
this.defaultReasoningEffort = options.reasoningEffort ?? DEFAULT_REASONING_EFFORT2;
|
|
4653
|
+
this.reasoningLevelMeta = {
|
|
4654
|
+
requested: options.reasoningLevelRequested,
|
|
4655
|
+
applied: options.reasoningLevelApplied,
|
|
4656
|
+
fallbackApplied: options.reasoningFallbackApplied
|
|
4657
|
+
};
|
|
4387
4658
|
this.defaultTextVerbosity = options.textVerbosity;
|
|
4388
4659
|
}
|
|
4389
4660
|
async ainvoke(input, context) {
|
|
@@ -4454,7 +4725,11 @@ var ChatOpenRouter = class {
|
|
|
4454
4725
|
} : void 0
|
|
4455
4726
|
).finalResponse();
|
|
4456
4727
|
await this.debugResponseIfEnabled(response, debugSeq);
|
|
4457
|
-
return toChatInvokeCompletion3(response
|
|
4728
|
+
return toChatInvokeCompletion3(response, {
|
|
4729
|
+
reasoning_requested: this.reasoningLevelMeta.requested,
|
|
4730
|
+
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
4731
|
+
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
4732
|
+
});
|
|
4458
4733
|
}
|
|
4459
4734
|
nextDebugInvokeSeq() {
|
|
4460
4735
|
const seq = this.debugInvokeSeq + 1;
|