@zenning/openai 1.4.5 → 1.4.6

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.js CHANGED
@@ -2629,37 +2629,58 @@ var OpenAIResponsesLanguageModel = class {
2629
2629
  }
2630
2630
  }
2631
2631
  const reasoningSummary = (_b = (_a15 = response.output.find((item) => item.type === "reasoning")) == null ? void 0 : _a15.summary) != null ? _b : null;
2632
- console.log(JSON.stringify({
2632
+ const allAnnotations = outputTextElements.flatMap((content) => content.annotations);
2633
+ console.log("\u{1F4CB} Processing annotations in doGenerate:", JSON.stringify({
2633
2634
  msg: "ai-sdk: content annotations",
2634
- annotations: outputTextElements.flatMap((content) => content.annotations)
2635
- }));
2635
+ count: allAnnotations.length,
2636
+ annotations: allAnnotations
2637
+ }, null, 2));
2636
2638
  return {
2637
2639
  text: outputTextElements.map((content) => content.text).join("\n"),
2638
2640
  sources: outputTextElements.flatMap(
2639
2641
  (content) => content.annotations.map((annotation) => {
2640
- var _a16, _b2, _c2, _d2, _e2, _f2, _g2, _h, _i;
2641
- if (annotation.type === "url_citation") {
2642
- return {
2643
- sourceType: "url",
2644
- id: (_c2 = (_b2 = (_a16 = this.config).generateId) == null ? void 0 : _b2.call(_a16)) != null ? _c2 : (0, import_provider_utils9.generateId)(),
2645
- url: annotation.url,
2646
- title: annotation.title
2647
- };
2648
- } else if (annotation.type === "file_citation") {
2649
- return {
2650
- sourceType: "document",
2651
- id: (_f2 = (_e2 = (_d2 = this.config).generateId) == null ? void 0 : _e2.call(_d2)) != null ? _f2 : (0, import_provider_utils9.generateId)(),
2652
- mediaType: "text/plain",
2653
- title: annotation.quote || annotation.filename || "Document",
2654
- filename: annotation.filename,
2655
- quote: annotation.quote
2656
- };
2657
- } else {
2642
+ var _a16, _b2, _c2, _d2, _e2, _f2, _g2, _h, _i, _j, _k, _l;
2643
+ console.log("\u{1F517} Processing annotation for source:", JSON.stringify(annotation, null, 2));
2644
+ try {
2645
+ if (annotation.type === "url_citation") {
2646
+ const urlSource = {
2647
+ sourceType: "url",
2648
+ id: (_c2 = (_b2 = (_a16 = this.config).generateId) == null ? void 0 : _b2.call(_a16)) != null ? _c2 : (0, import_provider_utils9.generateId)(),
2649
+ url: annotation.url,
2650
+ title: annotation.title
2651
+ };
2652
+ console.log("\u2705 Created URL source:", JSON.stringify(urlSource, null, 2));
2653
+ return urlSource;
2654
+ } else if (annotation.type === "file_citation") {
2655
+ const documentSource = {
2656
+ sourceType: "document",
2657
+ id: (_f2 = (_e2 = (_d2 = this.config).generateId) == null ? void 0 : _e2.call(_d2)) != null ? _f2 : (0, import_provider_utils9.generateId)(),
2658
+ mediaType: "text/plain",
2659
+ title: annotation.quote || annotation.filename || "Document",
2660
+ filename: annotation.filename,
2661
+ quote: annotation.quote
2662
+ };
2663
+ console.log("\u{1F4C4} Created document source:", JSON.stringify(documentSource, null, 2));
2664
+ return documentSource;
2665
+ } else {
2666
+ console.log("\u26A0\uFE0F Unknown annotation type in doGenerate:", annotation.type);
2667
+ return {
2668
+ sourceType: "url",
2669
+ id: (_i = (_h = (_g2 = this.config).generateId) == null ? void 0 : _h.call(_g2)) != null ? _i : (0, import_provider_utils9.generateId)(),
2670
+ url: "",
2671
+ title: "Unknown Source"
2672
+ };
2673
+ }
2674
+ } catch (error) {
2675
+ console.error("\u274C Error creating source in doGenerate:", {
2676
+ annotation,
2677
+ error: error instanceof Error ? error.message : String(error)
2678
+ });
2658
2679
  return {
2659
2680
  sourceType: "url",
2660
- id: (_i = (_h = (_g2 = this.config).generateId) == null ? void 0 : _h.call(_g2)) != null ? _i : (0, import_provider_utils9.generateId)(),
2681
+ id: (_l = (_k = (_j = this.config).generateId) == null ? void 0 : _k.call(_j)) != null ? _l : (0, import_provider_utils9.generateId)(),
2661
2682
  url: "",
2662
- title: "Unknown Source"
2683
+ title: "Error Source"
2663
2684
  };
2664
2685
  }
2665
2686
  })
@@ -2704,24 +2725,40 @@ var OpenAIResponsesLanguageModel = class {
2704
2725
  };
2705
2726
  }
2706
2727
  async doStream(options) {
2728
+ console.log("\u{1F680} Starting doStream with options:", JSON.stringify({
2729
+ modelId: this.modelId,
2730
+ hasAbortSignal: !!options.abortSignal
2731
+ }, null, 2));
2707
2732
  const { args: body, warnings } = this.getArgs(options);
2708
- const { responseHeaders, value: response } = await (0, import_provider_utils9.postJsonToApi)({
2709
- url: this.config.url({
2710
- path: "/responses",
2711
- modelId: this.modelId
2712
- }),
2713
- headers: (0, import_provider_utils9.combineHeaders)(this.config.headers(), options.headers),
2714
- body: {
2715
- ...body,
2716
- stream: true
2717
- },
2718
- failedResponseHandler: openaiFailedResponseHandler,
2719
- successfulResponseHandler: (0, import_provider_utils9.createEventSourceResponseHandler)(
2720
- openaiResponsesChunkSchema
2721
- ),
2722
- abortSignal: options.abortSignal,
2723
- fetch: this.config.fetch
2724
- });
2733
+ console.log("\u{1F4E4} Request body:", JSON.stringify(body, null, 2));
2734
+ let response;
2735
+ let responseHeaders;
2736
+ try {
2737
+ console.log("\u{1F4E1} Making API request...");
2738
+ const result = await (0, import_provider_utils9.postJsonToApi)({
2739
+ url: this.config.url({
2740
+ path: "/responses",
2741
+ modelId: this.modelId
2742
+ }),
2743
+ headers: (0, import_provider_utils9.combineHeaders)(this.config.headers(), options.headers),
2744
+ body: {
2745
+ ...body,
2746
+ stream: true
2747
+ },
2748
+ failedResponseHandler: openaiFailedResponseHandler,
2749
+ successfulResponseHandler: (0, import_provider_utils9.createEventSourceResponseHandler)(
2750
+ openaiResponsesChunkSchema
2751
+ ),
2752
+ abortSignal: options.abortSignal,
2753
+ fetch: this.config.fetch
2754
+ });
2755
+ response = result.value;
2756
+ responseHeaders = result.responseHeaders;
2757
+ console.log("\u2705 API request successful, starting stream processing");
2758
+ } catch (error) {
2759
+ console.error("\u274C API request failed:", error);
2760
+ throw error;
2761
+ }
2725
2762
  const self = this;
2726
2763
  let finishReason = "unknown";
2727
2764
  let promptTokens = NaN;
@@ -2736,173 +2773,213 @@ var OpenAIResponsesLanguageModel = class {
2736
2773
  new TransformStream({
2737
2774
  transform(chunk, controller) {
2738
2775
  var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2739
- if (!chunk.success) {
2740
- finishReason = "error";
2741
- controller.enqueue({ type: "error", error: chunk.error });
2742
- return;
2743
- }
2744
- const value = chunk.value;
2745
- if (isResponseOutputItemAddedChunk(value)) {
2746
- if (value.item.type === "function_call") {
2747
- ongoingToolCalls[value.output_index] = {
2748
- toolName: value.item.name,
2749
- toolCallId: value.item.call_id
2750
- };
2751
- controller.enqueue({
2752
- type: "tool-call-delta",
2753
- toolCallType: "function",
2754
- toolCallId: value.item.call_id,
2755
- toolName: value.item.name,
2756
- argsTextDelta: value.item.arguments
2757
- });
2758
- } else if (value.item.type === "web_search_call") {
2759
- ongoingToolCalls[value.output_index] = {
2760
- toolName: "web_search_preview",
2761
- toolCallId: value.item.id
2762
- };
2776
+ try {
2777
+ console.log("\u{1F4E6} Processing chunk:", JSON.stringify(chunk, null, 2));
2778
+ if (!chunk.success) {
2779
+ console.error("\u274C Chunk parsing failed:", chunk.error);
2780
+ finishReason = "error";
2781
+ controller.enqueue({ type: "error", error: chunk.error });
2782
+ return;
2783
+ }
2784
+ const value = chunk.value;
2785
+ console.log("\u{1F4E5} Chunk value type:", value.type);
2786
+ if (isResponseOutputItemAddedChunk(value)) {
2787
+ console.log("\u{1F4DD} Output item added:", JSON.stringify(value, null, 2));
2788
+ if (value.item.type === "function_call") {
2789
+ ongoingToolCalls[value.output_index] = {
2790
+ toolName: value.item.name,
2791
+ toolCallId: value.item.call_id
2792
+ };
2793
+ controller.enqueue({
2794
+ type: "tool-call-delta",
2795
+ toolCallType: "function",
2796
+ toolCallId: value.item.call_id,
2797
+ toolName: value.item.name,
2798
+ argsTextDelta: value.item.arguments
2799
+ });
2800
+ } else if (value.item.type === "web_search_call") {
2801
+ ongoingToolCalls[value.output_index] = {
2802
+ toolName: "web_search_preview",
2803
+ toolCallId: value.item.id
2804
+ };
2805
+ controller.enqueue({
2806
+ type: "tool-call-delta",
2807
+ toolCallType: "function",
2808
+ toolCallId: value.item.id,
2809
+ toolName: "web_search_preview",
2810
+ argsTextDelta: JSON.stringify({ action: value.item.action })
2811
+ });
2812
+ } else if (value.item.type === "computer_call") {
2813
+ ongoingToolCalls[value.output_index] = {
2814
+ toolName: "computer_use",
2815
+ toolCallId: value.item.id
2816
+ };
2817
+ controller.enqueue({
2818
+ type: "tool-call-delta",
2819
+ toolCallType: "function",
2820
+ toolCallId: value.item.id,
2821
+ toolName: "computer_use",
2822
+ argsTextDelta: ""
2823
+ });
2824
+ } else if (value.item.type === "file_search_call") {
2825
+ ongoingToolCalls[value.output_index] = {
2826
+ toolName: "file_search",
2827
+ toolCallId: value.item.id
2828
+ };
2829
+ controller.enqueue({
2830
+ type: "tool-call-delta",
2831
+ toolCallType: "function",
2832
+ toolCallId: value.item.id,
2833
+ toolName: "file_search",
2834
+ argsTextDelta: ""
2835
+ });
2836
+ }
2837
+ } else if (isResponseFunctionCallArgumentsDeltaChunk(value)) {
2838
+ console.log("\u{1F527} Function call arguments delta:", JSON.stringify(value, null, 2));
2839
+ const toolCall = ongoingToolCalls[value.output_index];
2840
+ if (toolCall != null) {
2841
+ controller.enqueue({
2842
+ type: "tool-call-delta",
2843
+ toolCallType: "function",
2844
+ toolCallId: toolCall.toolCallId,
2845
+ toolName: toolCall.toolName,
2846
+ argsTextDelta: value.delta
2847
+ });
2848
+ }
2849
+ } else if (isResponseCreatedChunk(value)) {
2850
+ console.log("\u{1F680} Response created:", JSON.stringify(value, null, 2));
2851
+ responseId = value.response.id;
2763
2852
  controller.enqueue({
2764
- type: "tool-call-delta",
2765
- toolCallType: "function",
2766
- toolCallId: value.item.id,
2767
- toolName: "web_search_preview",
2768
- argsTextDelta: JSON.stringify({ action: value.item.action })
2853
+ type: "response-metadata",
2854
+ id: value.response.id,
2855
+ timestamp: new Date(value.response.created_at * 1e3),
2856
+ modelId: value.response.model
2769
2857
  });
2770
- } else if (value.item.type === "computer_call") {
2771
- ongoingToolCalls[value.output_index] = {
2772
- toolName: "computer_use",
2773
- toolCallId: value.item.id
2774
- };
2858
+ } else if (isTextDeltaChunk(value)) {
2859
+ console.log("\u{1F4DD} Text delta chunk:", JSON.stringify(value, null, 2));
2775
2860
  controller.enqueue({
2776
- type: "tool-call-delta",
2777
- toolCallType: "function",
2778
- toolCallId: value.item.id,
2779
- toolName: "computer_use",
2780
- argsTextDelta: ""
2861
+ type: "text-delta",
2862
+ textDelta: value.delta
2781
2863
  });
2782
- } else if (value.item.type === "file_search_call") {
2783
- ongoingToolCalls[value.output_index] = {
2784
- toolName: "file_search",
2785
- toolCallId: value.item.id
2786
- };
2864
+ } else if (isResponseReasoningSummaryTextDeltaChunk(value)) {
2865
+ console.log("\u{1F9E0} Reasoning summary delta:", JSON.stringify(value, null, 2));
2787
2866
  controller.enqueue({
2788
- type: "tool-call-delta",
2789
- toolCallType: "function",
2790
- toolCallId: value.item.id,
2791
- toolName: "file_search",
2792
- argsTextDelta: ""
2867
+ type: "reasoning",
2868
+ textDelta: value.delta
2793
2869
  });
2794
- }
2795
- } else if (isResponseFunctionCallArgumentsDeltaChunk(value)) {
2796
- const toolCall = ongoingToolCalls[value.output_index];
2797
- if (toolCall != null) {
2798
- controller.enqueue({
2799
- type: "tool-call-delta",
2800
- toolCallType: "function",
2801
- toolCallId: toolCall.toolCallId,
2802
- toolName: toolCall.toolName,
2803
- argsTextDelta: value.delta
2870
+ } else if (isResponseOutputItemDoneChunk(value)) {
2871
+ console.log("\u2705 Output item done:", JSON.stringify(value, null, 2));
2872
+ if (value.item.type === "function_call") {
2873
+ ongoingToolCalls[value.output_index] = void 0;
2874
+ hasToolCalls = true;
2875
+ controller.enqueue({
2876
+ type: "tool-call",
2877
+ toolCallType: "function",
2878
+ toolCallId: value.item.call_id,
2879
+ toolName: value.item.name,
2880
+ args: value.item.arguments
2881
+ });
2882
+ } else if (value.item.type === "web_search_call") {
2883
+ ongoingToolCalls[value.output_index] = void 0;
2884
+ hasToolCalls = true;
2885
+ controller.enqueue({
2886
+ type: "tool-call",
2887
+ toolCallType: "function",
2888
+ toolCallId: value.item.id,
2889
+ toolName: "web_search_preview",
2890
+ args: JSON.stringify({ action: value.item.action })
2891
+ });
2892
+ } else if (value.item.type === "computer_call") {
2893
+ ongoingToolCalls[value.output_index] = void 0;
2894
+ hasToolCalls = true;
2895
+ controller.enqueue({
2896
+ type: "tool-call",
2897
+ toolCallType: "function",
2898
+ toolCallId: value.item.id,
2899
+ toolName: "computer_use",
2900
+ args: ""
2901
+ });
2902
+ } else if (value.item.type === "file_search_call") {
2903
+ ongoingToolCalls[value.output_index] = void 0;
2904
+ hasToolCalls = true;
2905
+ controller.enqueue({
2906
+ type: "tool-call",
2907
+ toolCallType: "function",
2908
+ toolCallId: value.item.id,
2909
+ toolName: "file_search",
2910
+ args: JSON.stringify({
2911
+ queries: value.item.queries,
2912
+ results: value.item.results
2913
+ })
2914
+ });
2915
+ }
2916
+ } else if (isResponseFinishedChunk(value)) {
2917
+ console.log("\u{1F3C1} Response finished:", JSON.stringify(value, null, 2));
2918
+ finishReason = mapOpenAIResponseFinishReason({
2919
+ finishReason: (_a15 = value.response.incomplete_details) == null ? void 0 : _a15.reason,
2920
+ hasToolCalls
2804
2921
  });
2922
+ promptTokens = value.response.usage.input_tokens;
2923
+ completionTokens = value.response.usage.output_tokens;
2924
+ cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
2925
+ reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
2926
+ } else if (isResponseAnnotationAddedChunk(value)) {
2927
+ console.log("\u{1F50D} Processing annotation chunk:", JSON.stringify({
2928
+ type: value.type,
2929
+ annotation: value.annotation
2930
+ }, null, 2));
2931
+ try {
2932
+ if (value.annotation.type === "url_citation") {
2933
+ const urlSource = {
2934
+ sourceType: "url",
2935
+ id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : (0, import_provider_utils9.generateId)(),
2936
+ url: value.annotation.url,
2937
+ title: value.annotation.title
2938
+ };
2939
+ console.log("\u2705 Creating URL source:", JSON.stringify(urlSource, null, 2));
2940
+ controller.enqueue({
2941
+ type: "source",
2942
+ source: urlSource
2943
+ });
2944
+ console.log("\u2705 URL source enqueued successfully");
2945
+ } else if (value.annotation.type === "file_citation") {
2946
+ const documentSource = {
2947
+ sourceType: "document",
2948
+ id: (_k = (_j = (_i = self.config).generateId) == null ? void 0 : _j.call(_i)) != null ? _k : (0, import_provider_utils9.generateId)(),
2949
+ mediaType: "text/plain",
2950
+ title: value.annotation.quote || value.annotation.filename || "Document",
2951
+ filename: value.annotation.filename,
2952
+ quote: value.annotation.quote
2953
+ };
2954
+ console.log("\u{1F4C4} Creating document source:", JSON.stringify(documentSource, null, 2));
2955
+ controller.enqueue({
2956
+ type: "source",
2957
+ source: documentSource
2958
+ });
2959
+ console.log("\u2705 Document source enqueued successfully");
2960
+ } else {
2961
+ console.log("\u26A0\uFE0F Unknown annotation type:", value.annotation.type);
2962
+ }
2963
+ } catch (error) {
2964
+ console.error("\u274C Error processing annotation:", {
2965
+ annotation: value.annotation,
2966
+ error: error instanceof Error ? error.message : String(error),
2967
+ stack: error instanceof Error ? error.stack : void 0
2968
+ });
2969
+ }
2970
+ } else {
2971
+ console.log("\u2753 Unhandled chunk type:", value.type, JSON.stringify(value, null, 2));
2805
2972
  }
2806
- } else if (isResponseCreatedChunk(value)) {
2807
- responseId = value.response.id;
2808
- controller.enqueue({
2809
- type: "response-metadata",
2810
- id: value.response.id,
2811
- timestamp: new Date(value.response.created_at * 1e3),
2812
- modelId: value.response.model
2813
- });
2814
- } else if (isTextDeltaChunk(value)) {
2815
- controller.enqueue({
2816
- type: "text-delta",
2817
- textDelta: value.delta
2973
+ } catch (error) {
2974
+ console.error("\u{1F4A5} FATAL ERROR in chunk processing:", {
2975
+ error: error instanceof Error ? error.message : String(error),
2976
+ stack: error instanceof Error ? error.stack : void 0
2818
2977
  });
2819
- } else if (isResponseReasoningSummaryTextDeltaChunk(value)) {
2978
+ finishReason = "error";
2820
2979
  controller.enqueue({
2821
- type: "reasoning",
2822
- textDelta: value.delta
2980
+ type: "error",
2981
+ error: error instanceof Error ? error : new Error(String(error))
2823
2982
  });
2824
- } else if (isResponseOutputItemDoneChunk(value)) {
2825
- if (value.item.type === "function_call") {
2826
- ongoingToolCalls[value.output_index] = void 0;
2827
- hasToolCalls = true;
2828
- controller.enqueue({
2829
- type: "tool-call",
2830
- toolCallType: "function",
2831
- toolCallId: value.item.call_id,
2832
- toolName: value.item.name,
2833
- args: value.item.arguments
2834
- });
2835
- } else if (value.item.type === "web_search_call") {
2836
- ongoingToolCalls[value.output_index] = void 0;
2837
- hasToolCalls = true;
2838
- controller.enqueue({
2839
- type: "tool-call",
2840
- toolCallType: "function",
2841
- toolCallId: value.item.id,
2842
- toolName: "web_search_preview",
2843
- args: JSON.stringify({ action: value.item.action })
2844
- });
2845
- } else if (value.item.type === "computer_call") {
2846
- ongoingToolCalls[value.output_index] = void 0;
2847
- hasToolCalls = true;
2848
- controller.enqueue({
2849
- type: "tool-call",
2850
- toolCallType: "function",
2851
- toolCallId: value.item.id,
2852
- toolName: "computer_use",
2853
- args: ""
2854
- });
2855
- } else if (value.item.type === "file_search_call") {
2856
- ongoingToolCalls[value.output_index] = void 0;
2857
- hasToolCalls = true;
2858
- controller.enqueue({
2859
- type: "tool-call",
2860
- toolCallType: "function",
2861
- toolCallId: value.item.id,
2862
- toolName: "file_search",
2863
- args: JSON.stringify({
2864
- queries: value.item.queries,
2865
- results: value.item.results
2866
- })
2867
- });
2868
- }
2869
- } else if (isResponseFinishedChunk(value)) {
2870
- finishReason = mapOpenAIResponseFinishReason({
2871
- finishReason: (_a15 = value.response.incomplete_details) == null ? void 0 : _a15.reason,
2872
- hasToolCalls
2873
- });
2874
- promptTokens = value.response.usage.input_tokens;
2875
- completionTokens = value.response.usage.output_tokens;
2876
- cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
2877
- reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
2878
- } else if (isResponseAnnotationAddedChunk(value)) {
2879
- console.log(JSON.stringify({
2880
- msg: "ai-sdk: source (stream)",
2881
- source: value.annotation
2882
- }));
2883
- if (value.annotation.type === "url_citation") {
2884
- controller.enqueue({
2885
- type: "source",
2886
- source: {
2887
- sourceType: "url",
2888
- id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : (0, import_provider_utils9.generateId)(),
2889
- url: value.annotation.url,
2890
- title: value.annotation.title
2891
- }
2892
- });
2893
- } else if (value.annotation.type === "file_citation") {
2894
- controller.enqueue({
2895
- type: "source",
2896
- source: {
2897
- sourceType: "document",
2898
- id: (_k = (_j = (_i = self.config).generateId) == null ? void 0 : _j.call(_i)) != null ? _k : (0, import_provider_utils9.generateId)(),
2899
- mediaType: "text/plain",
2900
- title: value.annotation.quote || value.annotation.filename || "Document",
2901
- filename: value.annotation.filename,
2902
- quote: value.annotation.quote
2903
- }
2904
- });
2905
- }
2906
2983
  }
2907
2984
  },
2908
2985
  flush(controller) {