@codelia/core 0.1.63 → 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 CHANGED
@@ -89,6 +89,11 @@ var OPENAI_MODELS = [
89
89
  id: "gpt-5.3-codex",
90
90
  provider: "openai"
91
91
  },
92
+ {
93
+ id: "gpt-5.3-codex-spark",
94
+ provider: "openai",
95
+ contextWindow: 128e3
96
+ },
92
97
  {
93
98
  id: "gpt-5.2-2025-12-11",
94
99
  provider: "openai"
@@ -2443,25 +2448,6 @@ var ChatAnthropic = class {
2443
2448
  var import_node_crypto2 = require("crypto");
2444
2449
  var import_openai3 = __toESM(require("openai"), 1);
2445
2450
 
2446
- // src/llm/openai/http-transport.ts
2447
- var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
2448
- const streamRequest = {
2449
- ...request,
2450
- stream: true
2451
- };
2452
- return client.responses.stream(
2453
- streamRequest,
2454
- signal || sessionIdHeader ? {
2455
- ...signal ? { signal } : {},
2456
- ...sessionIdHeader ? {
2457
- headers: {
2458
- session_id: sessionIdHeader
2459
- }
2460
- } : {}
2461
- } : void 0
2462
- ).finalResponse();
2463
- };
2464
-
2465
2451
  // src/llm/openai/response-utils.ts
2466
2452
  var isRecord2 = (value) => typeof value === "object" && value !== null;
2467
2453
  var stringifyUnknown3 = (value) => {
@@ -2503,6 +2489,107 @@ var extractOutputText = (items) => {
2503
2489
  }
2504
2490
  return texts.join("");
2505
2491
  };
2492
+ var getRecordString = (record, key) => {
2493
+ const value = record[key];
2494
+ return typeof value === "string" ? value : void 0;
2495
+ };
2496
+ var getRecordNumber = (record, key) => {
2497
+ const value = record[key];
2498
+ return typeof value === "number" ? value : void 0;
2499
+ };
2500
+ var clipDebugText = (value, maxChars = 80) => value.length <= maxChars ? value : `${value.slice(0, maxChars)}...`;
2501
+ var getResponseStreamEventDebugPayload = (event) => {
2502
+ const payload = {
2503
+ type: event.type,
2504
+ sequence_number: typeof event.sequence_number === "number" ? event.sequence_number : null
2505
+ };
2506
+ const eventRecord = event;
2507
+ const outputIndex = getRecordNumber(eventRecord, "output_index");
2508
+ if (outputIndex !== void 0) {
2509
+ payload.output_index = outputIndex;
2510
+ }
2511
+ const contentIndex = getRecordNumber(eventRecord, "content_index");
2512
+ if (contentIndex !== void 0) {
2513
+ payload.content_index = contentIndex;
2514
+ }
2515
+ switch (event.type) {
2516
+ case "response.output_text.delta":
2517
+ case "response.reasoning_text.delta":
2518
+ case "response.function_call_arguments.delta": {
2519
+ const delta = getRecordString(eventRecord, "delta") ?? "";
2520
+ payload.delta_chars = delta.length;
2521
+ payload.delta_preview = clipDebugText(delta);
2522
+ break;
2523
+ }
2524
+ case "response.output_item.added":
2525
+ case "response.output_item.done": {
2526
+ const item = eventRecord.item;
2527
+ if (isRecord2(item)) {
2528
+ payload.item_type = getRecordString(item, "type");
2529
+ payload.item_role = getRecordString(item, "role");
2530
+ }
2531
+ break;
2532
+ }
2533
+ case "response.content_part.added": {
2534
+ const part = eventRecord.part;
2535
+ if (isRecord2(part)) {
2536
+ payload.part_type = getRecordString(part, "type");
2537
+ if (part.type === "output_text") {
2538
+ const text = getRecordString(part, "text") ?? "";
2539
+ payload.delta_chars = text.length;
2540
+ payload.delta_preview = clipDebugText(text);
2541
+ }
2542
+ }
2543
+ break;
2544
+ }
2545
+ case "response.completed": {
2546
+ const response = eventRecord.response;
2547
+ if (isRecord2(response)) {
2548
+ payload.response_id = getRecordString(response, "id");
2549
+ const status = response.status;
2550
+ payload.response_status = typeof status === "string" ? status : null;
2551
+ const output = response.output;
2552
+ if (Array.isArray(output)) {
2553
+ payload.response_output_items = output.length;
2554
+ }
2555
+ const outputText = response.output_text;
2556
+ payload.response_output_text = typeof outputText === "string" ? clipDebugText(outputText) : null;
2557
+ const usage = response.usage;
2558
+ if (isRecord2(usage)) {
2559
+ payload.response_output_tokens = getRecordNumber(
2560
+ usage,
2561
+ "output_tokens"
2562
+ );
2563
+ const outputDetails = usage.output_tokens_details;
2564
+ if (isRecord2(outputDetails)) {
2565
+ payload.response_reasoning_tokens = getRecordNumber(
2566
+ outputDetails,
2567
+ "reasoning_tokens"
2568
+ );
2569
+ }
2570
+ }
2571
+ }
2572
+ break;
2573
+ }
2574
+ }
2575
+ return payload;
2576
+ };
2577
+ var getEmptyCompletionDebugPayload = (response, completion) => {
2578
+ if (completion.messages.length > 0) {
2579
+ return null;
2580
+ }
2581
+ const outputTokens = response.usage?.output_tokens;
2582
+ if (typeof outputTokens !== "number" || outputTokens <= 0) {
2583
+ return null;
2584
+ }
2585
+ return {
2586
+ id: response.id,
2587
+ status: response.status ?? null,
2588
+ output_text: typeof response.output_text === "string" ? response.output_text : null,
2589
+ output: Array.isArray(response.output) ? response.output : [],
2590
+ usage: response.usage ?? null
2591
+ };
2592
+ };
2506
2593
  var toResponseInputContent = (part) => {
2507
2594
  switch (part.type) {
2508
2595
  case "text":
@@ -2544,6 +2631,401 @@ var toFunctionCallOutput = (content) => {
2544
2631
  return content.map(toResponseInputContent);
2545
2632
  };
2546
2633
 
2634
+ // src/llm/openai/response-accumulator.ts
2635
+ var isRecord3 = (value) => typeof value === "object" && value !== null;
2636
+ var envTruthy2 = (value) => {
2637
+ if (!value) return false;
2638
+ const normalized = value.trim().toLowerCase();
2639
+ return normalized === "1" || normalized === "true";
2640
+ };
2641
+ var loggedUnhandledResponseEventTypes = /* @__PURE__ */ new Set();
2642
+ var cloneValue = (value) => {
2643
+ if (typeof structuredClone === "function") {
2644
+ return structuredClone(value);
2645
+ }
2646
+ return JSON.parse(JSON.stringify(value));
2647
+ };
2648
+ var cloneResponse = (response) => cloneValue(response);
2649
+ var cloneOutputItem = (item) => cloneValue(item);
2650
+ var ensureArrayIndex = (array, index) => {
2651
+ while (array.length <= index) {
2652
+ array.push(void 0);
2653
+ }
2654
+ };
2655
+ var OpenAiResponseAccumulator = class {
2656
+ baseResponse = null;
2657
+ terminalResponse = null;
2658
+ outputItems = [];
2659
+ observeEvent(event) {
2660
+ switch (event.type) {
2661
+ case "response.created":
2662
+ case "response.queued":
2663
+ case "response.in_progress":
2664
+ case "response.incomplete": {
2665
+ this.seedBaseResponse(event.response);
2666
+ break;
2667
+ }
2668
+ case "response.output_item.added":
2669
+ case "response.output_item.done": {
2670
+ this.setOutputItem(event.output_index, event.item);
2671
+ break;
2672
+ }
2673
+ case "response.content_part.added": {
2674
+ this.addContentPart(event.output_index, event.content_index, event.part);
2675
+ break;
2676
+ }
2677
+ case "response.output_text.delta": {
2678
+ this.appendMessageText(
2679
+ event.output_index,
2680
+ event.content_index,
2681
+ event.delta
2682
+ );
2683
+ break;
2684
+ }
2685
+ case "response.output_text.done": {
2686
+ this.setMessageText(event.output_index, event.content_index, event.text);
2687
+ break;
2688
+ }
2689
+ case "response.refusal.delta": {
2690
+ this.appendRefusalText(
2691
+ event.output_index,
2692
+ event.content_index,
2693
+ event.delta
2694
+ );
2695
+ break;
2696
+ }
2697
+ case "response.refusal.done": {
2698
+ this.setRefusalText(
2699
+ event.output_index,
2700
+ event.content_index,
2701
+ event.refusal
2702
+ );
2703
+ break;
2704
+ }
2705
+ case "response.function_call_arguments.delta": {
2706
+ this.appendFunctionCallArguments(event.output_index, event.delta);
2707
+ break;
2708
+ }
2709
+ case "response.function_call_arguments.done": {
2710
+ this.setFunctionCallArguments(
2711
+ event.output_index,
2712
+ event.name,
2713
+ event.arguments
2714
+ );
2715
+ break;
2716
+ }
2717
+ case "response.reasoning_summary_part.added":
2718
+ case "response.reasoning_summary_part.done": {
2719
+ this.setReasoningSummaryPart(
2720
+ event.output_index,
2721
+ event.summary_index,
2722
+ event.part.text
2723
+ );
2724
+ break;
2725
+ }
2726
+ case "response.reasoning_summary_text.delta": {
2727
+ this.appendReasoningSummaryText(
2728
+ event.output_index,
2729
+ event.summary_index,
2730
+ event.delta
2731
+ );
2732
+ break;
2733
+ }
2734
+ case "response.reasoning_summary_text.done": {
2735
+ this.setReasoningSummaryText(
2736
+ event.output_index,
2737
+ event.summary_index,
2738
+ event.text
2739
+ );
2740
+ break;
2741
+ }
2742
+ case "response.reasoning_text.delta": {
2743
+ this.appendReasoningText(
2744
+ event.output_index,
2745
+ event.content_index,
2746
+ event.delta
2747
+ );
2748
+ break;
2749
+ }
2750
+ case "response.reasoning_text.done": {
2751
+ this.setReasoningText(
2752
+ event.output_index,
2753
+ event.content_index,
2754
+ event.text
2755
+ );
2756
+ break;
2757
+ }
2758
+ case "response.output_text.annotation.added": {
2759
+ this.addOutputTextAnnotation(
2760
+ event.output_index,
2761
+ event.content_index,
2762
+ event.annotation_index,
2763
+ event.annotation
2764
+ );
2765
+ break;
2766
+ }
2767
+ case "response.completed": {
2768
+ this.observeTerminalResponse(event.response);
2769
+ break;
2770
+ }
2771
+ default: {
2772
+ this.logUnhandledResponseEvent(event);
2773
+ break;
2774
+ }
2775
+ }
2776
+ }
2777
+ observeTerminalResponse(response) {
2778
+ if (!this.baseResponse) {
2779
+ this.seedBaseResponse(response);
2780
+ }
2781
+ this.terminalResponse = cloneResponse(response);
2782
+ }
2783
+ buildResponse(fallbackResponse) {
2784
+ const terminal = fallbackResponse ? cloneResponse(fallbackResponse) : this.terminalResponse ? cloneResponse(this.terminalResponse) : null;
2785
+ const base = terminal ?? this.baseResponse;
2786
+ if (!base) {
2787
+ throw new Error("openai response accumulator has no response snapshot");
2788
+ }
2789
+ const merged = cloneResponse(base);
2790
+ const accumulatedOutput = this.getAccumulatedOutputItems();
2791
+ if (accumulatedOutput.length > 0) {
2792
+ merged.output = accumulatedOutput;
2793
+ const accumulatedText = extractOutputText(accumulatedOutput);
2794
+ merged.output_text = accumulatedText;
2795
+ }
2796
+ return merged;
2797
+ }
2798
+ seedBaseResponse(response) {
2799
+ this.baseResponse = cloneResponse(response);
2800
+ if (this.outputItems.length === 0 && Array.isArray(response.output)) {
2801
+ for (const [index, item] of response.output.entries()) {
2802
+ this.outputItems[index] = cloneOutputItem(item);
2803
+ }
2804
+ }
2805
+ }
2806
+ getAccumulatedOutputItems() {
2807
+ return this.outputItems.filter(
2808
+ (item) => item !== void 0
2809
+ );
2810
+ }
2811
+ setOutputItem(index, item) {
2812
+ ensureArrayIndex(this.outputItems, index);
2813
+ this.outputItems[index] = cloneOutputItem(item);
2814
+ }
2815
+ getOutputItem(index) {
2816
+ return this.outputItems[index];
2817
+ }
2818
+ addContentPart(outputIndex, contentIndex, part) {
2819
+ const output = this.getOutputItem(outputIndex);
2820
+ if (!output || !isRecord3(part)) {
2821
+ return;
2822
+ }
2823
+ if (output.type === "message" && part.type !== "reasoning_text") {
2824
+ ensureArrayIndex(output.content, contentIndex);
2825
+ output.content[contentIndex] = cloneValue(part);
2826
+ return;
2827
+ }
2828
+ if (output.type === "reasoning" && part.type === "reasoning_text") {
2829
+ const content = output.content ?? [];
2830
+ ensureArrayIndex(content, contentIndex);
2831
+ content[contentIndex] = cloneValue(part);
2832
+ output.content = content;
2833
+ }
2834
+ }
2835
+ appendMessageText(outputIndex, contentIndex, delta) {
2836
+ const part = this.ensureMessageTextPart(outputIndex, contentIndex);
2837
+ if (!part) {
2838
+ return;
2839
+ }
2840
+ part.text += delta;
2841
+ }
2842
+ setMessageText(outputIndex, contentIndex, text) {
2843
+ const part = this.ensureMessageTextPart(outputIndex, contentIndex);
2844
+ if (!part) {
2845
+ return;
2846
+ }
2847
+ part.text = text;
2848
+ }
2849
+ appendRefusalText(outputIndex, contentIndex, delta) {
2850
+ const part = this.ensureMessageRefusalPart(outputIndex, contentIndex);
2851
+ if (!part) {
2852
+ return;
2853
+ }
2854
+ part.refusal += delta;
2855
+ }
2856
+ setRefusalText(outputIndex, contentIndex, refusal) {
2857
+ const part = this.ensureMessageRefusalPart(outputIndex, contentIndex);
2858
+ if (!part) {
2859
+ return;
2860
+ }
2861
+ part.refusal = refusal;
2862
+ }
2863
+ appendFunctionCallArguments(outputIndex, delta) {
2864
+ const output = this.getOutputItem(outputIndex);
2865
+ if (!output || output.type !== "function_call") {
2866
+ return;
2867
+ }
2868
+ output.arguments += delta;
2869
+ }
2870
+ setFunctionCallArguments(outputIndex, name, argumentsText) {
2871
+ const output = this.getOutputItem(outputIndex);
2872
+ if (!output || output.type !== "function_call") {
2873
+ return;
2874
+ }
2875
+ output.name = name;
2876
+ output.arguments = argumentsText;
2877
+ }
2878
+ setReasoningSummaryPart(outputIndex, summaryIndex, text) {
2879
+ const summary = this.ensureReasoningSummaryPart(outputIndex, summaryIndex);
2880
+ if (!summary) {
2881
+ return;
2882
+ }
2883
+ summary.text = text;
2884
+ }
2885
+ appendReasoningSummaryText(outputIndex, summaryIndex, delta) {
2886
+ const summary = this.ensureReasoningSummaryPart(outputIndex, summaryIndex);
2887
+ if (!summary) {
2888
+ return;
2889
+ }
2890
+ summary.text += delta;
2891
+ }
2892
+ setReasoningSummaryText(outputIndex, summaryIndex, text) {
2893
+ const summary = this.ensureReasoningSummaryPart(outputIndex, summaryIndex);
2894
+ if (!summary) {
2895
+ return;
2896
+ }
2897
+ summary.text = text;
2898
+ }
2899
+ appendReasoningText(outputIndex, contentIndex, delta) {
2900
+ const content = this.ensureReasoningContentPart(outputIndex, contentIndex);
2901
+ if (!content) {
2902
+ return;
2903
+ }
2904
+ content.text += delta;
2905
+ }
2906
+ setReasoningText(outputIndex, contentIndex, text) {
2907
+ const content = this.ensureReasoningContentPart(outputIndex, contentIndex);
2908
+ if (!content) {
2909
+ return;
2910
+ }
2911
+ content.text = text;
2912
+ }
2913
+ addOutputTextAnnotation(outputIndex, contentIndex, annotationIndex, annotation) {
2914
+ const part = this.ensureMessageTextPart(outputIndex, contentIndex);
2915
+ if (!part) {
2916
+ return;
2917
+ }
2918
+ ensureArrayIndex(part.annotations, annotationIndex);
2919
+ part.annotations[annotationIndex] = cloneValue(annotation);
2920
+ }
2921
+ ensureMessageTextPart(outputIndex, contentIndex) {
2922
+ const output = this.getOutputItem(outputIndex);
2923
+ if (!output || output.type !== "message") {
2924
+ return void 0;
2925
+ }
2926
+ ensureArrayIndex(output.content, contentIndex);
2927
+ const current = output.content[contentIndex];
2928
+ if (!current || current.type !== "output_text") {
2929
+ output.content[contentIndex] = {
2930
+ type: "output_text",
2931
+ text: "",
2932
+ annotations: []
2933
+ };
2934
+ }
2935
+ return output.content[contentIndex];
2936
+ }
2937
+ ensureMessageRefusalPart(outputIndex, contentIndex) {
2938
+ const output = this.getOutputItem(outputIndex);
2939
+ if (!output || output.type !== "message") {
2940
+ return void 0;
2941
+ }
2942
+ ensureArrayIndex(output.content, contentIndex);
2943
+ const current = output.content[contentIndex];
2944
+ if (!current || current.type !== "refusal") {
2945
+ output.content[contentIndex] = {
2946
+ type: "refusal",
2947
+ refusal: ""
2948
+ };
2949
+ }
2950
+ return output.content[contentIndex];
2951
+ }
2952
+ ensureReasoningSummaryPart(outputIndex, summaryIndex) {
2953
+ const output = this.getOutputItem(outputIndex);
2954
+ if (!output || output.type !== "reasoning") {
2955
+ return void 0;
2956
+ }
2957
+ ensureArrayIndex(output.summary, summaryIndex);
2958
+ if (!output.summary[summaryIndex]) {
2959
+ output.summary[summaryIndex] = {
2960
+ type: "summary_text",
2961
+ text: ""
2962
+ };
2963
+ }
2964
+ return output.summary[summaryIndex];
2965
+ }
2966
+ ensureReasoningContentPart(outputIndex, contentIndex) {
2967
+ const output = this.getOutputItem(outputIndex);
2968
+ if (!output || output.type !== "reasoning") {
2969
+ return void 0;
2970
+ }
2971
+ const content = output.content ?? [];
2972
+ ensureArrayIndex(content, contentIndex);
2973
+ if (!content[contentIndex]) {
2974
+ content[contentIndex] = {
2975
+ type: "reasoning_text",
2976
+ text: ""
2977
+ };
2978
+ }
2979
+ output.content = content;
2980
+ return content[contentIndex];
2981
+ }
2982
+ logUnhandledResponseEvent(event) {
2983
+ if (!envTruthy2(process.env.CODELIA_PROVIDER_LOG) && !envTruthy2(process.env.CODELIA_DEBUG)) {
2984
+ return;
2985
+ }
2986
+ if (!event.type.startsWith("response.")) {
2987
+ return;
2988
+ }
2989
+ if (loggedUnhandledResponseEventTypes.has(event.type)) {
2990
+ return;
2991
+ }
2992
+ loggedUnhandledResponseEventTypes.add(event.type);
2993
+ const payload = getResponseStreamEventDebugPayload(event);
2994
+ console.error(
2995
+ `[openai.stream.unhandled] ${safeJsonStringify(payload)}`
2996
+ );
2997
+ }
2998
+ };
2999
+
3000
+ // src/llm/openai/http-transport.ts
3001
+ var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader, onEvent) => {
3002
+ const streamRequest = {
3003
+ ...request,
3004
+ stream: true
3005
+ };
3006
+ const stream = client.responses.stream(
3007
+ streamRequest,
3008
+ signal || sessionIdHeader ? {
3009
+ ...signal ? { signal } : {},
3010
+ ...sessionIdHeader ? {
3011
+ headers: {
3012
+ session_id: sessionIdHeader
3013
+ }
3014
+ } : {}
3015
+ } : void 0
3016
+ );
3017
+ if (typeof stream[Symbol.asyncIterator] === "function") {
3018
+ const accumulator = new OpenAiResponseAccumulator();
3019
+ for await (const event of stream) {
3020
+ accumulator.observeEvent(event);
3021
+ await onEvent?.(event);
3022
+ }
3023
+ const finalResponse = await stream.finalResponse();
3024
+ return accumulator.buildResponse(finalResponse);
3025
+ }
3026
+ return stream.finalResponse();
3027
+ };
3028
+
2547
3029
  // src/llm/openai/serializer.ts
2548
3030
  var toOpenAiOtherPart = (kind, payload) => ({
2549
3031
  type: "other",
@@ -2943,12 +3425,12 @@ var resolveTimeoutMs = (value, fallbackMs) => {
2943
3425
  }
2944
3426
  return Math.floor(value);
2945
3427
  };
2946
- var envTruthy2 = (value) => {
3428
+ var envTruthy3 = (value) => {
2947
3429
  if (!value) return false;
2948
3430
  const normalized = value.trim().toLowerCase();
2949
3431
  return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
2950
3432
  };
2951
- var WS_EVENT_TRACE_ENABLED = envTruthy2(process.env.CODELIA_DEBUG);
3433
+ var WS_EVENT_TRACE_ENABLED = envTruthy3(process.env.CODELIA_DEBUG);
2952
3434
  var WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS = 5e3;
2953
3435
  var wsEventTraceStateBySource = /* @__PURE__ */ new Map();
2954
3436
  var getWsEventTraceState = (source) => {
@@ -3070,6 +3552,7 @@ var OpenAiWsTransport = class {
3070
3552
  let settled = false;
3071
3553
  let rejectResponsePromise;
3072
3554
  const responsePromise = new Promise((resolve, reject) => {
3555
+ const accumulator = new OpenAiResponseAccumulator();
3073
3556
  let onAbort;
3074
3557
  const nativeSocket = this.getNativeSocket(ws);
3075
3558
  let removeNativeClose;
@@ -3125,7 +3608,9 @@ var OpenAiWsTransport = class {
3125
3608
  if (settled || !terminalCompletedWithoutUsage) {
3126
3609
  return;
3127
3610
  }
3128
- resolveOnce(terminalCompletedWithoutUsage);
3611
+ resolveOnce(
3612
+ accumulator.buildResponse(terminalCompletedWithoutUsage)
3613
+ );
3129
3614
  }, WS_TERMINAL_DONE_GRACE_MS);
3130
3615
  };
3131
3616
  const resetResponseTimeout = () => {
@@ -3136,7 +3621,9 @@ var OpenAiWsTransport = class {
3136
3621
  timeout = setTimeout(() => {
3137
3622
  if (settled) return;
3138
3623
  if (terminalCompletedWithoutUsage) {
3139
- resolveOnce(terminalCompletedWithoutUsage);
3624
+ resolveOnce(
3625
+ accumulator.buildResponse(terminalCompletedWithoutUsage)
3626
+ );
3140
3627
  return;
3141
3628
  }
3142
3629
  settled = true;
@@ -3151,18 +3638,21 @@ var OpenAiWsTransport = class {
3151
3638
  const resolveFromTerminalEvent = (event, type) => {
3152
3639
  const response = event?.response;
3153
3640
  if (response && typeof response === "object") {
3641
+ accumulator.observeTerminalResponse(response);
3154
3642
  if (type === "response.completed" && !this.hasUsageTotals(response)) {
3155
3643
  terminalCompletedWithoutUsage = response;
3156
3644
  scheduleDoneGraceTimer();
3157
3645
  return;
3158
3646
  }
3159
3647
  clearDoneGraceTimer();
3160
- resolveOnce(response);
3648
+ resolveOnce(accumulator.buildResponse(response));
3161
3649
  return;
3162
3650
  }
3163
3651
  if (type === "response.done" && terminalCompletedWithoutUsage) {
3164
3652
  clearDoneGraceTimer();
3165
- resolveOnce(terminalCompletedWithoutUsage);
3653
+ resolveOnce(
3654
+ accumulator.buildResponse(terminalCompletedWithoutUsage)
3655
+ );
3166
3656
  return;
3167
3657
  }
3168
3658
  rejectOnce(
@@ -3174,6 +3664,9 @@ var OpenAiWsTransport = class {
3174
3664
  if (!eventType) {
3175
3665
  return;
3176
3666
  }
3667
+ if (eventType !== "response.done") {
3668
+ accumulator.observeEvent(event);
3669
+ }
3177
3670
  if (eventType === "response.failed") {
3178
3671
  logWsEventTrace("sdk", eventType, false);
3179
3672
  onResponseFailed(event);
@@ -3792,7 +4285,7 @@ var ChatOpenAI = class {
3792
4285
  debugSeq,
3793
4286
  transportResult.transport
3794
4287
  );
3795
- return toChatInvokeCompletion2(transportResult.response, {
4288
+ const completion = toChatInvokeCompletion2(transportResult.response, {
3796
4289
  selected_model: model ?? this.model,
3797
4290
  transport: transportResult.transport,
3798
4291
  websocket_mode: this.websocketMode,
@@ -3804,6 +4297,13 @@ var ChatOpenAI = class {
3804
4297
  reasoning_applied: this.reasoningLevelMeta.applied,
3805
4298
  reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
3806
4299
  });
4300
+ await this.debugEmptyCompletionIfEnabled(
4301
+ transportResult.response,
4302
+ completion,
4303
+ debugSeq,
4304
+ transportResult.transport
4305
+ );
4306
+ return completion;
3807
4307
  }
3808
4308
  onHistoryCompacted(context) {
3809
4309
  const sessionKey = context?.sessionKey;
@@ -4006,7 +4506,8 @@ var ChatOpenAI = class {
4006
4506
  this.client,
4007
4507
  args.request,
4008
4508
  args.signal,
4009
- args.sessionIdHeader
4509
+ args.sessionIdHeader,
4510
+ this.getHttpStreamDebugObserver(args.debugSeq)
4010
4511
  );
4011
4512
  return {
4012
4513
  response,
@@ -4202,6 +4703,33 @@ var ChatOpenAI = class {
4202
4703
  }
4203
4704
  }
4204
4705
  }
4706
+ async debugEmptyCompletionIfEnabled(response, completion, seq, transport) {
4707
+ const settings = getProviderLogSettings();
4708
+ if (!settings.enabled) {
4709
+ return;
4710
+ }
4711
+ const payload = getEmptyCompletionDebugPayload(response, completion);
4712
+ if (!payload) {
4713
+ return;
4714
+ }
4715
+ const outputTokens = payload.usage?.output_tokens ?? 0;
4716
+ console.error(
4717
+ `[openai.response.empty_completion] seq=${seq} transport=${transport} websocket_mode=${this.websocketMode} id=${payload.id} status=${payload.status ?? "unknown"} output_tokens=${outputTokens} messages=0`
4718
+ );
4719
+ console.error(safeJsonStringify(payload, 2));
4720
+ }
4721
+ getHttpStreamDebugObserver(seq) {
4722
+ const settings = getProviderLogSettings();
4723
+ if (!settings.enabled) {
4724
+ return void 0;
4725
+ }
4726
+ return (event) => {
4727
+ const payload = getResponseStreamEventDebugPayload(event);
4728
+ console.error(
4729
+ `[openai.stream.event] seq=${seq} transport=http_stream websocket_mode=${this.websocketMode} ${safeJsonStringify(payload)}`
4730
+ );
4731
+ };
4732
+ }
4205
4733
  };
4206
4734
 
4207
4735
  // src/llm/openrouter/chat.ts
@@ -4209,7 +4737,7 @@ var import_node_crypto3 = require("crypto");
4209
4737
  var import_openai5 = __toESM(require("openai"), 1);
4210
4738
 
4211
4739
  // src/llm/openrouter/response-utils.ts
4212
- var isRecord3 = (value) => typeof value === "object" && value !== null;
4740
+ var isRecord4 = (value) => typeof value === "object" && value !== null;
4213
4741
  var stringifyUnknown5 = (value) => {
4214
4742
  if (value == null) return "";
4215
4743
  if (typeof value === "string") return value;
@@ -4224,7 +4752,7 @@ var formatOtherPart4 = (part) => {
4224
4752
  return payloadText ? `[other:${part.provider}/${part.kind}] ${payloadText}` : `[other:${part.provider}/${part.kind}]`;
4225
4753
  };
4226
4754
  var isResponsesApiInputContent = (value) => {
4227
- if (!isRecord3(value)) return false;
4755
+ if (!isRecord4(value)) return false;
4228
4756
  const type = value.type;
4229
4757
  if (type === "input_text") {
4230
4758
  return typeof value.text === "string";
package/dist/index.d.cts CHANGED
@@ -731,6 +731,8 @@ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$1, OpenAI
731
731
  private nextDebugInvokeSeq;
732
732
  private debugRequestIfEnabled;
733
733
  private debugResponseIfEnabled;
734
+ private debugEmptyCompletionIfEnabled;
735
+ private getHttpStreamDebugObserver;
734
736
  }
735
737
 
736
738
  declare const PROVIDER_NAME: "openrouter";