@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.d.ts 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";
package/dist/index.js CHANGED
@@ -31,6 +31,11 @@ var OPENAI_MODELS = [
31
31
  id: "gpt-5.3-codex",
32
32
  provider: "openai"
33
33
  },
34
+ {
35
+ id: "gpt-5.3-codex-spark",
36
+ provider: "openai",
37
+ contextWindow: 128e3
38
+ },
34
39
  {
35
40
  id: "gpt-5.2-2025-12-11",
36
41
  provider: "openai"
@@ -2385,25 +2390,6 @@ var ChatAnthropic = class {
2385
2390
  import { createHash as createHash2 } from "crypto";
2386
2391
  import OpenAI from "openai";
2387
2392
 
2388
- // src/llm/openai/http-transport.ts
2389
- var invokeOpenAiHttp = async (client, request, signal, sessionIdHeader) => {
2390
- const streamRequest = {
2391
- ...request,
2392
- stream: true
2393
- };
2394
- return client.responses.stream(
2395
- streamRequest,
2396
- signal || sessionIdHeader ? {
2397
- ...signal ? { signal } : {},
2398
- ...sessionIdHeader ? {
2399
- headers: {
2400
- session_id: sessionIdHeader
2401
- }
2402
- } : {}
2403
- } : void 0
2404
- ).finalResponse();
2405
- };
2406
-
2407
2393
  // src/llm/openai/response-utils.ts
2408
2394
  var isRecord2 = (value) => typeof value === "object" && value !== null;
2409
2395
  var stringifyUnknown3 = (value) => {
@@ -2445,6 +2431,107 @@ var extractOutputText = (items) => {
2445
2431
  }
2446
2432
  return texts.join("");
2447
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
+ };
2448
2535
  var toResponseInputContent = (part) => {
2449
2536
  switch (part.type) {
2450
2537
  case "text":
@@ -2486,6 +2573,401 @@ var toFunctionCallOutput = (content) => {
2486
2573
  return content.map(toResponseInputContent);
2487
2574
  };
2488
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
+
2489
2971
  // src/llm/openai/serializer.ts
2490
2972
  var toOpenAiOtherPart = (kind, payload) => ({
2491
2973
  type: "other",
@@ -2885,12 +3367,12 @@ var resolveTimeoutMs = (value, fallbackMs) => {
2885
3367
  }
2886
3368
  return Math.floor(value);
2887
3369
  };
2888
- var envTruthy2 = (value) => {
3370
+ var envTruthy3 = (value) => {
2889
3371
  if (!value) return false;
2890
3372
  const normalized = value.trim().toLowerCase();
2891
3373
  return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
2892
3374
  };
2893
- var WS_EVENT_TRACE_ENABLED = envTruthy2(process.env.CODELIA_DEBUG);
3375
+ var WS_EVENT_TRACE_ENABLED = envTruthy3(process.env.CODELIA_DEBUG);
2894
3376
  var WS_EVENT_TRACE_PROGRESS_LOG_INTERVAL_MS = 5e3;
2895
3377
  var wsEventTraceStateBySource = /* @__PURE__ */ new Map();
2896
3378
  var getWsEventTraceState = (source) => {
@@ -3012,6 +3494,7 @@ var OpenAiWsTransport = class {
3012
3494
  let settled = false;
3013
3495
  let rejectResponsePromise;
3014
3496
  const responsePromise = new Promise((resolve, reject) => {
3497
+ const accumulator = new OpenAiResponseAccumulator();
3015
3498
  let onAbort;
3016
3499
  const nativeSocket = this.getNativeSocket(ws);
3017
3500
  let removeNativeClose;
@@ -3067,7 +3550,9 @@ var OpenAiWsTransport = class {
3067
3550
  if (settled || !terminalCompletedWithoutUsage) {
3068
3551
  return;
3069
3552
  }
3070
- resolveOnce(terminalCompletedWithoutUsage);
3553
+ resolveOnce(
3554
+ accumulator.buildResponse(terminalCompletedWithoutUsage)
3555
+ );
3071
3556
  }, WS_TERMINAL_DONE_GRACE_MS);
3072
3557
  };
3073
3558
  const resetResponseTimeout = () => {
@@ -3078,7 +3563,9 @@ var OpenAiWsTransport = class {
3078
3563
  timeout = setTimeout(() => {
3079
3564
  if (settled) return;
3080
3565
  if (terminalCompletedWithoutUsage) {
3081
- resolveOnce(terminalCompletedWithoutUsage);
3566
+ resolveOnce(
3567
+ accumulator.buildResponse(terminalCompletedWithoutUsage)
3568
+ );
3082
3569
  return;
3083
3570
  }
3084
3571
  settled = true;
@@ -3093,18 +3580,21 @@ var OpenAiWsTransport = class {
3093
3580
  const resolveFromTerminalEvent = (event, type) => {
3094
3581
  const response = event?.response;
3095
3582
  if (response && typeof response === "object") {
3583
+ accumulator.observeTerminalResponse(response);
3096
3584
  if (type === "response.completed" && !this.hasUsageTotals(response)) {
3097
3585
  terminalCompletedWithoutUsage = response;
3098
3586
  scheduleDoneGraceTimer();
3099
3587
  return;
3100
3588
  }
3101
3589
  clearDoneGraceTimer();
3102
- resolveOnce(response);
3590
+ resolveOnce(accumulator.buildResponse(response));
3103
3591
  return;
3104
3592
  }
3105
3593
  if (type === "response.done" && terminalCompletedWithoutUsage) {
3106
3594
  clearDoneGraceTimer();
3107
- resolveOnce(terminalCompletedWithoutUsage);
3595
+ resolveOnce(
3596
+ accumulator.buildResponse(terminalCompletedWithoutUsage)
3597
+ );
3108
3598
  return;
3109
3599
  }
3110
3600
  rejectOnce(
@@ -3116,6 +3606,9 @@ var OpenAiWsTransport = class {
3116
3606
  if (!eventType) {
3117
3607
  return;
3118
3608
  }
3609
+ if (eventType !== "response.done") {
3610
+ accumulator.observeEvent(event);
3611
+ }
3119
3612
  if (eventType === "response.failed") {
3120
3613
  logWsEventTrace("sdk", eventType, false);
3121
3614
  onResponseFailed(event);
@@ -3734,7 +4227,7 @@ var ChatOpenAI = class {
3734
4227
  debugSeq,
3735
4228
  transportResult.transport
3736
4229
  );
3737
- return toChatInvokeCompletion2(transportResult.response, {
4230
+ const completion = toChatInvokeCompletion2(transportResult.response, {
3738
4231
  selected_model: model ?? this.model,
3739
4232
  transport: transportResult.transport,
3740
4233
  websocket_mode: this.websocketMode,
@@ -3746,6 +4239,13 @@ var ChatOpenAI = class {
3746
4239
  reasoning_applied: this.reasoningLevelMeta.applied,
3747
4240
  reasoning_fallback: this.reasoningLevelMeta.fallbackApplied
3748
4241
  });
4242
+ await this.debugEmptyCompletionIfEnabled(
4243
+ transportResult.response,
4244
+ completion,
4245
+ debugSeq,
4246
+ transportResult.transport
4247
+ );
4248
+ return completion;
3749
4249
  }
3750
4250
  onHistoryCompacted(context) {
3751
4251
  const sessionKey = context?.sessionKey;
@@ -3948,7 +4448,8 @@ var ChatOpenAI = class {
3948
4448
  this.client,
3949
4449
  args.request,
3950
4450
  args.signal,
3951
- args.sessionIdHeader
4451
+ args.sessionIdHeader,
4452
+ this.getHttpStreamDebugObserver(args.debugSeq)
3952
4453
  );
3953
4454
  return {
3954
4455
  response,
@@ -4144,6 +4645,33 @@ var ChatOpenAI = class {
4144
4645
  }
4145
4646
  }
4146
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
+ }
4147
4675
  };
4148
4676
 
4149
4677
  // src/llm/openrouter/chat.ts
@@ -4151,7 +4679,7 @@ import { createHash as createHash3 } from "crypto";
4151
4679
  import OpenAI2 from "openai";
4152
4680
 
4153
4681
  // src/llm/openrouter/response-utils.ts
4154
- var isRecord3 = (value) => typeof value === "object" && value !== null;
4682
+ var isRecord4 = (value) => typeof value === "object" && value !== null;
4155
4683
  var stringifyUnknown5 = (value) => {
4156
4684
  if (value == null) return "";
4157
4685
  if (typeof value === "string") return value;
@@ -4166,7 +4694,7 @@ var formatOtherPart4 = (part) => {
4166
4694
  return payloadText ? `[other:${part.provider}/${part.kind}] ${payloadText}` : `[other:${part.provider}/${part.kind}]`;
4167
4695
  };
4168
4696
  var isResponsesApiInputContent = (value) => {
4169
- if (!isRecord3(value)) return false;
4697
+ if (!isRecord4(value)) return false;
4170
4698
  const type = value.type;
4171
4699
  if (type === "input_text") {
4172
4700
  return typeof value.text === "string";