@codelia/core 0.1.64 → 0.1.65
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 +552 -29
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +552 -29
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -2390,25 +2390,6 @@ var ChatAnthropic = class {
|
|
|
2390
2390
|
import { createHash as createHash2 } from "crypto";
|
|
2391
2391
|
import OpenAI from "openai";
|
|
2392
2392
|
|
|
2393
|
-
// src/llm/openai/http-transport.ts
|
|
2394
|
-
var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
|
|
2395
|
-
const streamRequest = {
|
|
2396
|
-
...request,
|
|
2397
|
-
stream: true
|
|
2398
|
-
};
|
|
2399
|
-
return client.responses.stream(
|
|
2400
|
-
streamRequest,
|
|
2401
|
-
signal || sessionIdHeader ? {
|
|
2402
|
-
...signal ? { signal } : {},
|
|
2403
|
-
...sessionIdHeader ? {
|
|
2404
|
-
headers: {
|
|
2405
|
-
session_id: sessionIdHeader
|
|
2406
|
-
}
|
|
2407
|
-
} : {}
|
|
2408
|
-
} : void 0
|
|
2409
|
-
).finalResponse();
|
|
2410
|
-
};
|
|
2411
|
-
|
|
2412
2393
|
// src/llm/openai/response-utils.ts
|
|
2413
2394
|
var isRecord2 = (value) => typeof value === "object" && value !== null;
|
|
2414
2395
|
var stringifyUnknown3 = (value) => {
|
|
@@ -2450,6 +2431,107 @@ var extractOutputText = (items) => {
|
|
|
2450
2431
|
}
|
|
2451
2432
|
return texts.join("");
|
|
2452
2433
|
};
|
|
2434
|
+
var getRecordString = (record, key) => {
|
|
2435
|
+
const value = record[key];
|
|
2436
|
+
return typeof value === "string" ? value : void 0;
|
|
2437
|
+
};
|
|
2438
|
+
var getRecordNumber = (record, key) => {
|
|
2439
|
+
const value = record[key];
|
|
2440
|
+
return typeof value === "number" ? value : void 0;
|
|
2441
|
+
};
|
|
2442
|
+
var clipDebugText = (value, maxChars = 80) => value.length <= maxChars ? value : `${value.slice(0, maxChars)}...`;
|
|
2443
|
+
var getResponseStreamEventDebugPayload = (event) => {
|
|
2444
|
+
const payload = {
|
|
2445
|
+
type: event.type,
|
|
2446
|
+
sequence_number: typeof event.sequence_number === "number" ? event.sequence_number : null
|
|
2447
|
+
};
|
|
2448
|
+
const eventRecord = event;
|
|
2449
|
+
const outputIndex = getRecordNumber(eventRecord, "output_index");
|
|
2450
|
+
if (outputIndex !== void 0) {
|
|
2451
|
+
payload.output_index = outputIndex;
|
|
2452
|
+
}
|
|
2453
|
+
const contentIndex = getRecordNumber(eventRecord, "content_index");
|
|
2454
|
+
if (contentIndex !== void 0) {
|
|
2455
|
+
payload.content_index = contentIndex;
|
|
2456
|
+
}
|
|
2457
|
+
switch (event.type) {
|
|
2458
|
+
case "response.output_text.delta":
|
|
2459
|
+
case "response.reasoning_text.delta":
|
|
2460
|
+
case "response.function_call_arguments.delta": {
|
|
2461
|
+
const delta = getRecordString(eventRecord, "delta") ?? "";
|
|
2462
|
+
payload.delta_chars = delta.length;
|
|
2463
|
+
payload.delta_preview = clipDebugText(delta);
|
|
2464
|
+
break;
|
|
2465
|
+
}
|
|
2466
|
+
case "response.output_item.added":
|
|
2467
|
+
case "response.output_item.done": {
|
|
2468
|
+
const item = eventRecord.item;
|
|
2469
|
+
if (isRecord2(item)) {
|
|
2470
|
+
payload.item_type = getRecordString(item, "type");
|
|
2471
|
+
payload.item_role = getRecordString(item, "role");
|
|
2472
|
+
}
|
|
2473
|
+
break;
|
|
2474
|
+
}
|
|
2475
|
+
case "response.content_part.added": {
|
|
2476
|
+
const part = eventRecord.part;
|
|
2477
|
+
if (isRecord2(part)) {
|
|
2478
|
+
payload.part_type = getRecordString(part, "type");
|
|
2479
|
+
if (part.type === "output_text") {
|
|
2480
|
+
const text = getRecordString(part, "text") ?? "";
|
|
2481
|
+
payload.delta_chars = text.length;
|
|
2482
|
+
payload.delta_preview = clipDebugText(text);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
break;
|
|
2486
|
+
}
|
|
2487
|
+
case "response.completed": {
|
|
2488
|
+
const response = eventRecord.response;
|
|
2489
|
+
if (isRecord2(response)) {
|
|
2490
|
+
payload.response_id = getRecordString(response, "id");
|
|
2491
|
+
const status = response.status;
|
|
2492
|
+
payload.response_status = typeof status === "string" ? status : null;
|
|
2493
|
+
const output = response.output;
|
|
2494
|
+
if (Array.isArray(output)) {
|
|
2495
|
+
payload.response_output_items = output.length;
|
|
2496
|
+
}
|
|
2497
|
+
const outputText = response.output_text;
|
|
2498
|
+
payload.response_output_text = typeof outputText === "string" ? clipDebugText(outputText) : null;
|
|
2499
|
+
const usage = response.usage;
|
|
2500
|
+
if (isRecord2(usage)) {
|
|
2501
|
+
payload.response_output_tokens = getRecordNumber(
|
|
2502
|
+
usage,
|
|
2503
|
+
"output_tokens"
|
|
2504
|
+
);
|
|
2505
|
+
const outputDetails = usage.output_tokens_details;
|
|
2506
|
+
if (isRecord2(outputDetails)) {
|
|
2507
|
+
payload.response_reasoning_tokens = getRecordNumber(
|
|
2508
|
+
outputDetails,
|
|
2509
|
+
"reasoning_tokens"
|
|
2510
|
+
);
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
break;
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
return payload;
|
|
2518
|
+
};
|
|
2519
|
+
var getEmptyCompletionDebugPayload = (response, completion) => {
|
|
2520
|
+
if (completion.messages.length > 0) {
|
|
2521
|
+
return null;
|
|
2522
|
+
}
|
|
2523
|
+
const outputTokens = response.usage?.output_tokens;
|
|
2524
|
+
if (typeof outputTokens !== "number" || outputTokens <= 0) {
|
|
2525
|
+
return null;
|
|
2526
|
+
}
|
|
2527
|
+
return {
|
|
2528
|
+
id: response.id,
|
|
2529
|
+
status: response.status ?? null,
|
|
2530
|
+
output_text: typeof response.output_text === "string" ? response.output_text : null,
|
|
2531
|
+
output: Array.isArray(response.output) ? response.output : [],
|
|
2532
|
+
usage: response.usage ?? null
|
|
2533
|
+
};
|
|
2534
|
+
};
|
|
2453
2535
|
var toResponseInputContent = (part) => {
|
|
2454
2536
|
switch (part.type) {
|
|
2455
2537
|
case "text":
|
|
@@ -2491,6 +2573,401 @@ var toFunctionCallOutput = (content) => {
|
|
|
2491
2573
|
return content.map(toResponseInputContent);
|
|
2492
2574
|
};
|
|
2493
2575
|
|
|
2576
|
+
// src/llm/openai/response-accumulator.ts
|
|
2577
|
+
var isRecord3 = (value) => typeof value === "object" && value !== null;
|
|
2578
|
+
var envTruthy2 = (value) => {
|
|
2579
|
+
if (!value) return false;
|
|
2580
|
+
const normalized = value.trim().toLowerCase();
|
|
2581
|
+
return normalized === "1" || normalized === "true";
|
|
2582
|
+
};
|
|
2583
|
+
var loggedUnhandledResponseEventTypes = /* @__PURE__ */ new Set();
|
|
2584
|
+
var cloneValue = (value) => {
|
|
2585
|
+
if (typeof structuredClone === "function") {
|
|
2586
|
+
return structuredClone(value);
|
|
2587
|
+
}
|
|
2588
|
+
return JSON.parse(JSON.stringify(value));
|
|
2589
|
+
};
|
|
2590
|
+
var cloneResponse = (response) => cloneValue(response);
|
|
2591
|
+
var cloneOutputItem = (item) => cloneValue(item);
|
|
2592
|
+
var ensureArrayIndex = (array, index) => {
|
|
2593
|
+
while (array.length <= index) {
|
|
2594
|
+
array.push(void 0);
|
|
2595
|
+
}
|
|
2596
|
+
};
|
|
2597
|
+
var OpenAiResponseAccumulator = class {
|
|
2598
|
+
baseResponse = null;
|
|
2599
|
+
terminalResponse = null;
|
|
2600
|
+
outputItems = [];
|
|
2601
|
+
observeEvent(event) {
|
|
2602
|
+
switch (event.type) {
|
|
2603
|
+
case "response.created":
|
|
2604
|
+
case "response.queued":
|
|
2605
|
+
case "response.in_progress":
|
|
2606
|
+
case "response.incomplete": {
|
|
2607
|
+
this.seedBaseResponse(event.response);
|
|
2608
|
+
break;
|
|
2609
|
+
}
|
|
2610
|
+
case "response.output_item.added":
|
|
2611
|
+
case "response.output_item.done": {
|
|
2612
|
+
this.setOutputItem(event.output_index, event.item);
|
|
2613
|
+
break;
|
|
2614
|
+
}
|
|
2615
|
+
case "response.content_part.added": {
|
|
2616
|
+
this.addContentPart(event.output_index, event.content_index, event.part);
|
|
2617
|
+
break;
|
|
2618
|
+
}
|
|
2619
|
+
case "response.output_text.delta": {
|
|
2620
|
+
this.appendMessageText(
|
|
2621
|
+
event.output_index,
|
|
2622
|
+
event.content_index,
|
|
2623
|
+
event.delta
|
|
2624
|
+
);
|
|
2625
|
+
break;
|
|
2626
|
+
}
|
|
2627
|
+
case "response.output_text.done": {
|
|
2628
|
+
this.setMessageText(event.output_index, event.content_index, event.text);
|
|
2629
|
+
break;
|
|
2630
|
+
}
|
|
2631
|
+
case "response.refusal.delta": {
|
|
2632
|
+
this.appendRefusalText(
|
|
2633
|
+
event.output_index,
|
|
2634
|
+
event.content_index,
|
|
2635
|
+
event.delta
|
|
2636
|
+
);
|
|
2637
|
+
break;
|
|
2638
|
+
}
|
|
2639
|
+
case "response.refusal.done": {
|
|
2640
|
+
this.setRefusalText(
|
|
2641
|
+
event.output_index,
|
|
2642
|
+
event.content_index,
|
|
2643
|
+
event.refusal
|
|
2644
|
+
);
|
|
2645
|
+
break;
|
|
2646
|
+
}
|
|
2647
|
+
case "response.function_call_arguments.delta": {
|
|
2648
|
+
this.appendFunctionCallArguments(event.output_index, event.delta);
|
|
2649
|
+
break;
|
|
2650
|
+
}
|
|
2651
|
+
case "response.function_call_arguments.done": {
|
|
2652
|
+
this.setFunctionCallArguments(
|
|
2653
|
+
event.output_index,
|
|
2654
|
+
event.name,
|
|
2655
|
+
event.arguments
|
|
2656
|
+
);
|
|
2657
|
+
break;
|
|
2658
|
+
}
|
|
2659
|
+
case "response.reasoning_summary_part.added":
|
|
2660
|
+
case "response.reasoning_summary_part.done": {
|
|
2661
|
+
this.setReasoningSummaryPart(
|
|
2662
|
+
event.output_index,
|
|
2663
|
+
event.summary_index,
|
|
2664
|
+
event.part.text
|
|
2665
|
+
);
|
|
2666
|
+
break;
|
|
2667
|
+
}
|
|
2668
|
+
case "response.reasoning_summary_text.delta": {
|
|
2669
|
+
this.appendReasoningSummaryText(
|
|
2670
|
+
event.output_index,
|
|
2671
|
+
event.summary_index,
|
|
2672
|
+
event.delta
|
|
2673
|
+
);
|
|
2674
|
+
break;
|
|
2675
|
+
}
|
|
2676
|
+
case "response.reasoning_summary_text.done": {
|
|
2677
|
+
this.setReasoningSummaryText(
|
|
2678
|
+
event.output_index,
|
|
2679
|
+
event.summary_index,
|
|
2680
|
+
event.text
|
|
2681
|
+
);
|
|
2682
|
+
break;
|
|
2683
|
+
}
|
|
2684
|
+
case "response.reasoning_text.delta": {
|
|
2685
|
+
this.appendReasoningText(
|
|
2686
|
+
event.output_index,
|
|
2687
|
+
event.content_index,
|
|
2688
|
+
event.delta
|
|
2689
|
+
);
|
|
2690
|
+
break;
|
|
2691
|
+
}
|
|
2692
|
+
case "response.reasoning_text.done": {
|
|
2693
|
+
this.setReasoningText(
|
|
2694
|
+
event.output_index,
|
|
2695
|
+
event.content_index,
|
|
2696
|
+
event.text
|
|
2697
|
+
);
|
|
2698
|
+
break;
|
|
2699
|
+
}
|
|
2700
|
+
case "response.output_text.annotation.added": {
|
|
2701
|
+
this.addOutputTextAnnotation(
|
|
2702
|
+
event.output_index,
|
|
2703
|
+
event.content_index,
|
|
2704
|
+
event.annotation_index,
|
|
2705
|
+
event.annotation
|
|
2706
|
+
);
|
|
2707
|
+
break;
|
|
2708
|
+
}
|
|
2709
|
+
case "response.completed": {
|
|
2710
|
+
this.observeTerminalResponse(event.response);
|
|
2711
|
+
break;
|
|
2712
|
+
}
|
|
2713
|
+
default: {
|
|
2714
|
+
this.logUnhandledResponseEvent(event);
|
|
2715
|
+
break;
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
}
|
|
2719
|
+
observeTerminalResponse(response) {
|
|
2720
|
+
if (!this.baseResponse) {
|
|
2721
|
+
this.seedBaseResponse(response);
|
|
2722
|
+
}
|
|
2723
|
+
this.terminalResponse = cloneResponse(response);
|
|
2724
|
+
}
|
|
2725
|
+
buildResponse(fallbackResponse) {
|
|
2726
|
+
const terminal = fallbackResponse ? cloneResponse(fallbackResponse) : this.terminalResponse ? cloneResponse(this.terminalResponse) : null;
|
|
2727
|
+
const base = terminal ?? this.baseResponse;
|
|
2728
|
+
if (!base) {
|
|
2729
|
+
throw new Error("openai response accumulator has no response snapshot");
|
|
2730
|
+
}
|
|
2731
|
+
const merged = cloneResponse(base);
|
|
2732
|
+
const accumulatedOutput = this.getAccumulatedOutputItems();
|
|
2733
|
+
if (accumulatedOutput.length > 0) {
|
|
2734
|
+
merged.output = accumulatedOutput;
|
|
2735
|
+
const accumulatedText = extractOutputText(accumulatedOutput);
|
|
2736
|
+
merged.output_text = accumulatedText;
|
|
2737
|
+
}
|
|
2738
|
+
return merged;
|
|
2739
|
+
}
|
|
2740
|
+
seedBaseResponse(response) {
|
|
2741
|
+
this.baseResponse = cloneResponse(response);
|
|
2742
|
+
if (this.outputItems.length === 0 && Array.isArray(response.output)) {
|
|
2743
|
+
for (const [index, item] of response.output.entries()) {
|
|
2744
|
+
this.outputItems[index] = cloneOutputItem(item);
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
}
|
|
2748
|
+
getAccumulatedOutputItems() {
|
|
2749
|
+
return this.outputItems.filter(
|
|
2750
|
+
(item) => item !== void 0
|
|
2751
|
+
);
|
|
2752
|
+
}
|
|
2753
|
+
setOutputItem(index, item) {
|
|
2754
|
+
ensureArrayIndex(this.outputItems, index);
|
|
2755
|
+
this.outputItems[index] = cloneOutputItem(item);
|
|
2756
|
+
}
|
|
2757
|
+
getOutputItem(index) {
|
|
2758
|
+
return this.outputItems[index];
|
|
2759
|
+
}
|
|
2760
|
+
addContentPart(outputIndex, contentIndex, part) {
|
|
2761
|
+
const output = this.getOutputItem(outputIndex);
|
|
2762
|
+
if (!output || !isRecord3(part)) {
|
|
2763
|
+
return;
|
|
2764
|
+
}
|
|
2765
|
+
if (output.type === "message" && part.type !== "reasoning_text") {
|
|
2766
|
+
ensureArrayIndex(output.content, contentIndex);
|
|
2767
|
+
output.content[contentIndex] = cloneValue(part);
|
|
2768
|
+
return;
|
|
2769
|
+
}
|
|
2770
|
+
if (output.type === "reasoning" && part.type === "reasoning_text") {
|
|
2771
|
+
const content = output.content ?? [];
|
|
2772
|
+
ensureArrayIndex(content, contentIndex);
|
|
2773
|
+
content[contentIndex] = cloneValue(part);
|
|
2774
|
+
output.content = content;
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2777
|
+
appendMessageText(outputIndex, contentIndex, delta) {
|
|
2778
|
+
const part = this.ensureMessageTextPart(outputIndex, contentIndex);
|
|
2779
|
+
if (!part) {
|
|
2780
|
+
return;
|
|
2781
|
+
}
|
|
2782
|
+
part.text += delta;
|
|
2783
|
+
}
|
|
2784
|
+
setMessageText(outputIndex, contentIndex, text) {
|
|
2785
|
+
const part = this.ensureMessageTextPart(outputIndex, contentIndex);
|
|
2786
|
+
if (!part) {
|
|
2787
|
+
return;
|
|
2788
|
+
}
|
|
2789
|
+
part.text = text;
|
|
2790
|
+
}
|
|
2791
|
+
appendRefusalText(outputIndex, contentIndex, delta) {
|
|
2792
|
+
const part = this.ensureMessageRefusalPart(outputIndex, contentIndex);
|
|
2793
|
+
if (!part) {
|
|
2794
|
+
return;
|
|
2795
|
+
}
|
|
2796
|
+
part.refusal += delta;
|
|
2797
|
+
}
|
|
2798
|
+
setRefusalText(outputIndex, contentIndex, refusal) {
|
|
2799
|
+
const part = this.ensureMessageRefusalPart(outputIndex, contentIndex);
|
|
2800
|
+
if (!part) {
|
|
2801
|
+
return;
|
|
2802
|
+
}
|
|
2803
|
+
part.refusal = refusal;
|
|
2804
|
+
}
|
|
2805
|
+
appendFunctionCallArguments(outputIndex, delta) {
|
|
2806
|
+
const output = this.getOutputItem(outputIndex);
|
|
2807
|
+
if (!output || output.type !== "function_call") {
|
|
2808
|
+
return;
|
|
2809
|
+
}
|
|
2810
|
+
output.arguments += delta;
|
|
2811
|
+
}
|
|
2812
|
+
setFunctionCallArguments(outputIndex, name, argumentsText) {
|
|
2813
|
+
const output = this.getOutputItem(outputIndex);
|
|
2814
|
+
if (!output || output.type !== "function_call") {
|
|
2815
|
+
return;
|
|
2816
|
+
}
|
|
2817
|
+
output.name = name;
|
|
2818
|
+
output.arguments = argumentsText;
|
|
2819
|
+
}
|
|
2820
|
+
setReasoningSummaryPart(outputIndex, summaryIndex, text) {
|
|
2821
|
+
const summary = this.ensureReasoningSummaryPart(outputIndex, summaryIndex);
|
|
2822
|
+
if (!summary) {
|
|
2823
|
+
return;
|
|
2824
|
+
}
|
|
2825
|
+
summary.text = text;
|
|
2826
|
+
}
|
|
2827
|
+
appendReasoningSummaryText(outputIndex, summaryIndex, delta) {
|
|
2828
|
+
const summary = this.ensureReasoningSummaryPart(outputIndex, summaryIndex);
|
|
2829
|
+
if (!summary) {
|
|
2830
|
+
return;
|
|
2831
|
+
}
|
|
2832
|
+
summary.text += delta;
|
|
2833
|
+
}
|
|
2834
|
+
setReasoningSummaryText(outputIndex, summaryIndex, text) {
|
|
2835
|
+
const summary = this.ensureReasoningSummaryPart(outputIndex, summaryIndex);
|
|
2836
|
+
if (!summary) {
|
|
2837
|
+
return;
|
|
2838
|
+
}
|
|
2839
|
+
summary.text = text;
|
|
2840
|
+
}
|
|
2841
|
+
appendReasoningText(outputIndex, contentIndex, delta) {
|
|
2842
|
+
const content = this.ensureReasoningContentPart(outputIndex, contentIndex);
|
|
2843
|
+
if (!content) {
|
|
2844
|
+
return;
|
|
2845
|
+
}
|
|
2846
|
+
content.text += delta;
|
|
2847
|
+
}
|
|
2848
|
+
setReasoningText(outputIndex, contentIndex, text) {
|
|
2849
|
+
const content = this.ensureReasoningContentPart(outputIndex, contentIndex);
|
|
2850
|
+
if (!content) {
|
|
2851
|
+
return;
|
|
2852
|
+
}
|
|
2853
|
+
content.text = text;
|
|
2854
|
+
}
|
|
2855
|
+
addOutputTextAnnotation(outputIndex, contentIndex, annotationIndex, annotation) {
|
|
2856
|
+
const part = this.ensureMessageTextPart(outputIndex, contentIndex);
|
|
2857
|
+
if (!part) {
|
|
2858
|
+
return;
|
|
2859
|
+
}
|
|
2860
|
+
ensureArrayIndex(part.annotations, annotationIndex);
|
|
2861
|
+
part.annotations[annotationIndex] = cloneValue(annotation);
|
|
2862
|
+
}
|
|
2863
|
+
ensureMessageTextPart(outputIndex, contentIndex) {
|
|
2864
|
+
const output = this.getOutputItem(outputIndex);
|
|
2865
|
+
if (!output || output.type !== "message") {
|
|
2866
|
+
return void 0;
|
|
2867
|
+
}
|
|
2868
|
+
ensureArrayIndex(output.content, contentIndex);
|
|
2869
|
+
const current = output.content[contentIndex];
|
|
2870
|
+
if (!current || current.type !== "output_text") {
|
|
2871
|
+
output.content[contentIndex] = {
|
|
2872
|
+
type: "output_text",
|
|
2873
|
+
text: "",
|
|
2874
|
+
annotations: []
|
|
2875
|
+
};
|
|
2876
|
+
}
|
|
2877
|
+
return output.content[contentIndex];
|
|
2878
|
+
}
|
|
2879
|
+
ensureMessageRefusalPart(outputIndex, contentIndex) {
|
|
2880
|
+
const output = this.getOutputItem(outputIndex);
|
|
2881
|
+
if (!output || output.type !== "message") {
|
|
2882
|
+
return void 0;
|
|
2883
|
+
}
|
|
2884
|
+
ensureArrayIndex(output.content, contentIndex);
|
|
2885
|
+
const current = output.content[contentIndex];
|
|
2886
|
+
if (!current || current.type !== "refusal") {
|
|
2887
|
+
output.content[contentIndex] = {
|
|
2888
|
+
type: "refusal",
|
|
2889
|
+
refusal: ""
|
|
2890
|
+
};
|
|
2891
|
+
}
|
|
2892
|
+
return output.content[contentIndex];
|
|
2893
|
+
}
|
|
2894
|
+
ensureReasoningSummaryPart(outputIndex, summaryIndex) {
|
|
2895
|
+
const output = this.getOutputItem(outputIndex);
|
|
2896
|
+
if (!output || output.type !== "reasoning") {
|
|
2897
|
+
return void 0;
|
|
2898
|
+
}
|
|
2899
|
+
ensureArrayIndex(output.summary, summaryIndex);
|
|
2900
|
+
if (!output.summary[summaryIndex]) {
|
|
2901
|
+
output.summary[summaryIndex] = {
|
|
2902
|
+
type: "summary_text",
|
|
2903
|
+
text: ""
|
|
2904
|
+
};
|
|
2905
|
+
}
|
|
2906
|
+
return output.summary[summaryIndex];
|
|
2907
|
+
}
|
|
2908
|
+
ensureReasoningContentPart(outputIndex, contentIndex) {
|
|
2909
|
+
const output = this.getOutputItem(outputIndex);
|
|
2910
|
+
if (!output || output.type !== "reasoning") {
|
|
2911
|
+
return void 0;
|
|
2912
|
+
}
|
|
2913
|
+
const content = output.content ?? [];
|
|
2914
|
+
ensureArrayIndex(content, contentIndex);
|
|
2915
|
+
if (!content[contentIndex]) {
|
|
2916
|
+
content[contentIndex] = {
|
|
2917
|
+
type: "reasoning_text",
|
|
2918
|
+
text: ""
|
|
2919
|
+
};
|
|
2920
|
+
}
|
|
2921
|
+
output.content = content;
|
|
2922
|
+
return content[contentIndex];
|
|
2923
|
+
}
|
|
2924
|
+
logUnhandledResponseEvent(event) {
|
|
2925
|
+
if (!envTruthy2(process.env.CODELIA_PROVIDER_LOG) && !envTruthy2(process.env.CODELIA_DEBUG)) {
|
|
2926
|
+
return;
|
|
2927
|
+
}
|
|
2928
|
+
if (!event.type.startsWith("response.")) {
|
|
2929
|
+
return;
|
|
2930
|
+
}
|
|
2931
|
+
if (loggedUnhandledResponseEventTypes.has(event.type)) {
|
|
2932
|
+
return;
|
|
2933
|
+
}
|
|
2934
|
+
loggedUnhandledResponseEventTypes.add(event.type);
|
|
2935
|
+
const payload = getResponseStreamEventDebugPayload(event);
|
|
2936
|
+
console.error(
|
|
2937
|
+
`[openai.stream.unhandled] ${safeJsonStringify(payload)}`
|
|
2938
|
+
);
|
|
2939
|
+
}
|
|
2940
|
+
};
|
|
2941
|
+
|
|
2942
|
+
// src/llm/openai/http-transport.ts
|
|
2943
|
+
var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader, onEvent) => {
|
|
2944
|
+
const streamRequest = {
|
|
2945
|
+
...request,
|
|
2946
|
+
stream: true
|
|
2947
|
+
};
|
|
2948
|
+
const stream = client.responses.stream(
|
|
2949
|
+
streamRequest,
|
|
2950
|
+
signal || sessionIdHeader ? {
|
|
2951
|
+
...signal ? { signal } : {},
|
|
2952
|
+
...sessionIdHeader ? {
|
|
2953
|
+
headers: {
|
|
2954
|
+
session_id: sessionIdHeader
|
|
2955
|
+
}
|
|
2956
|
+
} : {}
|
|
2957
|
+
} : void 0
|
|
2958
|
+
);
|
|
2959
|
+
if (typeof stream[Symbol.asyncIterator] === "function") {
|
|
2960
|
+
const accumulator = new OpenAiResponseAccumulator();
|
|
2961
|
+
for await (const event of stream) {
|
|
2962
|
+
accumulator.observeEvent(event);
|
|
2963
|
+
await onEvent?.(event);
|
|
2964
|
+
}
|
|
2965
|
+
const finalResponse = await stream.finalResponse();
|
|
2966
|
+
return accumulator.buildResponse(finalResponse);
|
|
2967
|
+
}
|
|
2968
|
+
return stream.finalResponse();
|
|
2969
|
+
};
|
|
2970
|
+
|
|
2494
2971
|
// src/llm/openai/serializer.ts
|
|
2495
2972
|
var toOpenAiOtherPart = (kind, payload) => ({
|
|
2496
2973
|
type: "other",
|
|
@@ -2890,12 +3367,12 @@ var resolveTimeoutMs = (value, fallbackMs) => {
|
|
|
2890
3367
|
}
|
|
2891
3368
|
return Math.floor(value);
|
|
2892
3369
|
};
|
|
2893
|
-
var
|
|
3370
|
+
var envTruthy3 = (value) => {
|
|
2894
3371
|
if (!value) return false;
|
|
2895
3372
|
const normalized = value.trim().toLowerCase();
|
|
2896
3373
|
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
|
|
2897
3374
|
};
|
|
2898
|
-
var WS_EVENT_TRACE_ENABLED =
|
|
3375
|
+
var WS_EVENT_TRACE_ENABLED = envTruthy3(process.env.CODELIA_DEBUG);
|
|
2899
3376
|
var WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS = 5e3;
|
|
2900
3377
|
var wsEventTraceStateBySource = /* @__PURE__ */ new Map();
|
|
2901
3378
|
var getWsEventTraceState = (source) => {
|
|
@@ -3017,6 +3494,7 @@ var OpenAiWsTransport = class {
|
|
|
3017
3494
|
let settled = false;
|
|
3018
3495
|
let rejectResponsePromise;
|
|
3019
3496
|
const responsePromise = new Promise((resolve, reject) => {
|
|
3497
|
+
const accumulator = new OpenAiResponseAccumulator();
|
|
3020
3498
|
let onAbort;
|
|
3021
3499
|
const nativeSocket = this.getNativeSocket(ws);
|
|
3022
3500
|
let removeNativeClose;
|
|
@@ -3072,7 +3550,9 @@ var OpenAiWsTransport = class {
|
|
|
3072
3550
|
if (settled || !terminalCompletedWithoutUsage) {
|
|
3073
3551
|
return;
|
|
3074
3552
|
}
|
|
3075
|
-
resolveOnce(
|
|
3553
|
+
resolveOnce(
|
|
3554
|
+
accumulator.buildResponse(terminalCompletedWithoutUsage)
|
|
3555
|
+
);
|
|
3076
3556
|
}, WS_TERMINAL_DONE_GRACE_MS);
|
|
3077
3557
|
};
|
|
3078
3558
|
const resetResponseTimeout = () => {
|
|
@@ -3083,7 +3563,9 @@ var OpenAiWsTransport = class {
|
|
|
3083
3563
|
timeout = setTimeout(() => {
|
|
3084
3564
|
if (settled) return;
|
|
3085
3565
|
if (terminalCompletedWithoutUsage) {
|
|
3086
|
-
resolveOnce(
|
|
3566
|
+
resolveOnce(
|
|
3567
|
+
accumulator.buildResponse(terminalCompletedWithoutUsage)
|
|
3568
|
+
);
|
|
3087
3569
|
return;
|
|
3088
3570
|
}
|
|
3089
3571
|
settled = true;
|
|
@@ -3098,18 +3580,21 @@ var OpenAiWsTransport = class {
|
|
|
3098
3580
|
const resolveFromTerminalEvent = (event, type) => {
|
|
3099
3581
|
const response = event?.response;
|
|
3100
3582
|
if (response && typeof response === "object") {
|
|
3583
|
+
accumulator.observeTerminalResponse(response);
|
|
3101
3584
|
if (type === "response.completed" && !this.hasUsageTotals(response)) {
|
|
3102
3585
|
terminalCompletedWithoutUsage = response;
|
|
3103
3586
|
scheduleDoneGraceTimer();
|
|
3104
3587
|
return;
|
|
3105
3588
|
}
|
|
3106
3589
|
clearDoneGraceTimer();
|
|
3107
|
-
resolveOnce(response);
|
|
3590
|
+
resolveOnce(accumulator.buildResponse(response));
|
|
3108
3591
|
return;
|
|
3109
3592
|
}
|
|
3110
3593
|
if (type === "response.done" && terminalCompletedWithoutUsage) {
|
|
3111
3594
|
clearDoneGraceTimer();
|
|
3112
|
-
resolveOnce(
|
|
3595
|
+
resolveOnce(
|
|
3596
|
+
accumulator.buildResponse(terminalCompletedWithoutUsage)
|
|
3597
|
+
);
|
|
3113
3598
|
return;
|
|
3114
3599
|
}
|
|
3115
3600
|
rejectOnce(
|
|
@@ -3121,6 +3606,9 @@ var OpenAiWsTransport = class {
|
|
|
3121
3606
|
if (!eventType) {
|
|
3122
3607
|
return;
|
|
3123
3608
|
}
|
|
3609
|
+
if (eventType !== "response.done") {
|
|
3610
|
+
accumulator.observeEvent(event);
|
|
3611
|
+
}
|
|
3124
3612
|
if (eventType === "response.failed") {
|
|
3125
3613
|
logWsEventTrace("sdk", eventType, false);
|
|
3126
3614
|
onResponseFailed(event);
|
|
@@ -3739,7 +4227,7 @@ var ChatOpenAI = class {
|
|
|
3739
4227
|
debugSeq,
|
|
3740
4228
|
transportResult.transport
|
|
3741
4229
|
);
|
|
3742
|
-
|
|
4230
|
+
const completion = toChatInvokeCompletion2(transportResult.response, {
|
|
3743
4231
|
selected_model: model ?? this.model,
|
|
3744
4232
|
transport: transportResult.transport,
|
|
3745
4233
|
websocket_mode: this.websocketMode,
|
|
@@ -3751,6 +4239,13 @@ var ChatOpenAI = class {
|
|
|
3751
4239
|
reasoning_applied: this.reasoningLevelMeta.applied,
|
|
3752
4240
|
reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
|
|
3753
4241
|
});
|
|
4242
|
+
await this.debugEmptyCompletionIfEnabled(
|
|
4243
|
+
transportResult.response,
|
|
4244
|
+
completion,
|
|
4245
|
+
debugSeq,
|
|
4246
|
+
transportResult.transport
|
|
4247
|
+
);
|
|
4248
|
+
return completion;
|
|
3754
4249
|
}
|
|
3755
4250
|
onHistoryCompacted(context) {
|
|
3756
4251
|
const sessionKey = context?.sessionKey;
|
|
@@ -3953,7 +4448,8 @@ var ChatOpenAI = class {
|
|
|
3953
4448
|
this.client,
|
|
3954
4449
|
args.request,
|
|
3955
4450
|
args.signal,
|
|
3956
|
-
args.sessionIdHeader
|
|
4451
|
+
args.sessionIdHeader,
|
|
4452
|
+
this.getHttpStreamDebugObserver(args.debugSeq)
|
|
3957
4453
|
);
|
|
3958
4454
|
return {
|
|
3959
4455
|
response,
|
|
@@ -4149,6 +4645,33 @@ var ChatOpenAI = class {
|
|
|
4149
4645
|
}
|
|
4150
4646
|
}
|
|
4151
4647
|
}
|
|
4648
|
+
async debugEmptyCompletionIfEnabled(response, completion, seq, transport) {
|
|
4649
|
+
const settings = getProviderLogSettings();
|
|
4650
|
+
if (!settings.enabled) {
|
|
4651
|
+
return;
|
|
4652
|
+
}
|
|
4653
|
+
const payload = getEmptyCompletionDebugPayload(response, completion);
|
|
4654
|
+
if (!payload) {
|
|
4655
|
+
return;
|
|
4656
|
+
}
|
|
4657
|
+
const outputTokens = payload.usage?.output_tokens ?? 0;
|
|
4658
|
+
console.error(
|
|
4659
|
+
`[openai.response.empty_completion] seq=${seq} transport=${transport} websocket_mode=${this.websocketMode} id=${payload.id} status=${payload.status ?? "unknown"} output_tokens=${outputTokens} messages=0`
|
|
4660
|
+
);
|
|
4661
|
+
console.error(safeJsonStringify(payload, 2));
|
|
4662
|
+
}
|
|
4663
|
+
getHttpStreamDebugObserver(seq) {
|
|
4664
|
+
const settings = getProviderLogSettings();
|
|
4665
|
+
if (!settings.enabled) {
|
|
4666
|
+
return void 0;
|
|
4667
|
+
}
|
|
4668
|
+
return (event) => {
|
|
4669
|
+
const payload = getResponseStreamEventDebugPayload(event);
|
|
4670
|
+
console.error(
|
|
4671
|
+
`[openai.stream.event] seq=${seq} transport=http_stream websocket_mode=${this.websocketMode} ${safeJsonStringify(payload)}`
|
|
4672
|
+
);
|
|
4673
|
+
};
|
|
4674
|
+
}
|
|
4152
4675
|
};
|
|
4153
4676
|
|
|
4154
4677
|
// src/llm/openrouter/chat.ts
|
|
@@ -4156,7 +4679,7 @@ import { createHash as createHash3 } from "crypto";
|
|
|
4156
4679
|
import OpenAI2 from "openai";
|
|
4157
4680
|
|
|
4158
4681
|
// src/llm/openrouter/response-utils.ts
|
|
4159
|
-
var
|
|
4682
|
+
var isRecord4 = (value) => typeof value === "object" && value !== null;
|
|
4160
4683
|
var stringifyUnknown5 = (value) => {
|
|
4161
4684
|
if (value == null) return "";
|
|
4162
4685
|
if (typeof value === "string") return value;
|
|
@@ -4171,7 +4694,7 @@ var formatOtherPart4 = (part) => {
|
|
|
4171
4694
|
return payloadText ? `[other:${part.provider}/${part.kind}] ${payloadText}` : `[other:${part.provider}/${part.kind}]`;
|
|
4172
4695
|
};
|
|
4173
4696
|
var isResponsesApiInputContent = (value) => {
|
|
4174
|
-
if (!
|
|
4697
|
+
if (!isRecord4(value)) return false;
|
|
4175
4698
|
const type = value.type;
|
|
4176
4699
|
if (type === "input_text") {
|
|
4177
4700
|
return typeof value.text === "string";
|