@langchain/openai 0.6.16 → 1.0.0-alpha.2

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.
Files changed (163) hide show
  1. package/README.md +8 -8
  2. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  3. package/dist/azure/chat_models.cjs +661 -808
  4. package/dist/azure/chat_models.cjs.map +1 -0
  5. package/dist/azure/chat_models.d.cts +449 -0
  6. package/dist/azure/chat_models.d.cts.map +1 -0
  7. package/dist/azure/chat_models.d.ts +32 -28
  8. package/dist/azure/chat_models.d.ts.map +1 -0
  9. package/dist/azure/chat_models.js +660 -804
  10. package/dist/azure/chat_models.js.map +1 -0
  11. package/dist/azure/embeddings.cjs +81 -130
  12. package/dist/azure/embeddings.cjs.map +1 -0
  13. package/dist/azure/embeddings.d.cts +27 -0
  14. package/dist/azure/embeddings.d.cts.map +1 -0
  15. package/dist/azure/embeddings.d.ts +25 -20
  16. package/dist/azure/embeddings.d.ts.map +1 -0
  17. package/dist/azure/embeddings.js +80 -126
  18. package/dist/azure/embeddings.js.map +1 -0
  19. package/dist/azure/llms.cjs +109 -168
  20. package/dist/azure/llms.cjs.map +1 -0
  21. package/dist/azure/llms.d.cts +33 -0
  22. package/dist/azure/llms.d.cts.map +1 -0
  23. package/dist/azure/llms.d.ts +31 -25
  24. package/dist/azure/llms.d.ts.map +1 -0
  25. package/dist/azure/llms.js +108 -164
  26. package/dist/azure/llms.js.map +1 -0
  27. package/dist/chat_models.cjs +1959 -2789
  28. package/dist/chat_models.cjs.map +1 -0
  29. package/dist/chat_models.d.cts +931 -0
  30. package/dist/chat_models.d.cts.map +1 -0
  31. package/dist/chat_models.d.ts +367 -351
  32. package/dist/chat_models.d.ts.map +1 -0
  33. package/dist/chat_models.js +1954 -2779
  34. package/dist/chat_models.js.map +1 -0
  35. package/dist/embeddings.cjs +140 -206
  36. package/dist/embeddings.cjs.map +1 -0
  37. package/dist/embeddings.d.cts +120 -0
  38. package/dist/embeddings.d.cts.map +1 -0
  39. package/dist/embeddings.d.ts +93 -86
  40. package/dist/embeddings.d.ts.map +1 -0
  41. package/dist/embeddings.js +138 -201
  42. package/dist/embeddings.js.map +1 -0
  43. package/dist/index.cjs +47 -34
  44. package/dist/index.d.cts +16 -0
  45. package/dist/index.d.ts +16 -13
  46. package/dist/index.js +17 -13
  47. package/dist/langchain-core/dist/types/_internal.d.cts +11 -0
  48. package/dist/langchain-core/dist/types/_internal.d.cts.map +1 -0
  49. package/dist/langchain-core/dist/types/_internal.d.ts +11 -0
  50. package/dist/langchain-core/dist/types/_internal.d.ts.map +1 -0
  51. package/dist/langchain-core/dist/utils/stream.d.cts +26 -0
  52. package/dist/langchain-core/dist/utils/stream.d.cts.map +1 -0
  53. package/dist/langchain-core/dist/utils/stream.d.ts +26 -0
  54. package/dist/langchain-core/dist/utils/stream.d.ts.map +1 -0
  55. package/dist/llms.cjs +312 -468
  56. package/dist/llms.cjs.map +1 -0
  57. package/dist/llms.d.cts +123 -0
  58. package/dist/llms.d.cts.map +1 -0
  59. package/dist/llms.d.ts +93 -87
  60. package/dist/llms.d.ts.map +1 -0
  61. package/dist/llms.js +309 -462
  62. package/dist/llms.js.map +1 -0
  63. package/dist/tools/custom.cjs +25 -26
  64. package/dist/tools/custom.cjs.map +1 -0
  65. package/dist/tools/custom.d.cts +10 -0
  66. package/dist/tools/custom.d.cts.map +1 -0
  67. package/dist/tools/custom.d.ts +8 -3
  68. package/dist/tools/custom.d.ts.map +1 -0
  69. package/dist/tools/custom.js +24 -23
  70. package/dist/tools/custom.js.map +1 -0
  71. package/dist/tools/dalle.cjs +104 -194
  72. package/dist/tools/dalle.cjs.map +1 -0
  73. package/dist/tools/dalle.d.cts +123 -0
  74. package/dist/tools/dalle.d.cts.map +1 -0
  75. package/dist/tools/dalle.d.ts +111 -104
  76. package/dist/tools/dalle.d.ts.map +1 -0
  77. package/dist/tools/dalle.js +102 -189
  78. package/dist/tools/dalle.js.map +1 -0
  79. package/dist/tools/index.cjs +1 -17
  80. package/dist/tools/index.js +1 -1
  81. package/dist/types.d.cts +263 -0
  82. package/dist/types.d.cts.map +1 -0
  83. package/dist/types.d.ts +244 -246
  84. package/dist/types.d.ts.map +1 -0
  85. package/dist/utils/azure.cjs +86 -47
  86. package/dist/utils/azure.cjs.map +1 -0
  87. package/dist/utils/azure.d.cts +76 -0
  88. package/dist/utils/azure.d.cts.map +1 -0
  89. package/dist/utils/azure.d.ts +51 -9
  90. package/dist/utils/azure.d.ts.map +1 -0
  91. package/dist/utils/azure.js +85 -45
  92. package/dist/utils/azure.js.map +1 -0
  93. package/dist/utils/client.cjs +25 -0
  94. package/dist/utils/client.cjs.map +1 -0
  95. package/dist/utils/client.d.cts +5 -0
  96. package/dist/utils/client.d.cts.map +1 -0
  97. package/dist/utils/client.d.ts +5 -0
  98. package/dist/utils/client.d.ts.map +1 -0
  99. package/dist/utils/client.js +24 -0
  100. package/dist/utils/client.js.map +1 -0
  101. package/dist/utils/errors.cjs +9 -8
  102. package/dist/utils/errors.cjs.map +1 -0
  103. package/dist/utils/errors.js +9 -6
  104. package/dist/utils/errors.js.map +1 -0
  105. package/dist/utils/message_inputs.cjs +139 -0
  106. package/dist/utils/message_inputs.cjs.map +1 -0
  107. package/dist/utils/message_inputs.d.cts +9 -0
  108. package/dist/utils/message_inputs.d.cts.map +1 -0
  109. package/dist/utils/message_inputs.d.ts +9 -0
  110. package/dist/utils/message_inputs.d.ts.map +1 -0
  111. package/dist/utils/message_inputs.js +137 -0
  112. package/dist/utils/message_inputs.js.map +1 -0
  113. package/dist/utils/misc.cjs +32 -0
  114. package/dist/utils/misc.cjs.map +1 -0
  115. package/dist/utils/misc.d.cts +9 -0
  116. package/dist/utils/misc.d.cts.map +1 -0
  117. package/dist/utils/misc.d.ts +9 -0
  118. package/dist/utils/misc.d.ts.map +1 -0
  119. package/dist/utils/misc.js +29 -0
  120. package/dist/utils/misc.js.map +1 -0
  121. package/dist/utils/output.cjs +106 -45
  122. package/dist/utils/output.cjs.map +1 -0
  123. package/dist/utils/output.js +104 -43
  124. package/dist/utils/output.js.map +1 -0
  125. package/dist/utils/prompts.cjs +39 -38
  126. package/dist/utils/prompts.cjs.map +1 -0
  127. package/dist/utils/prompts.d.cts +42 -0
  128. package/dist/utils/prompts.d.cts.map +1 -0
  129. package/dist/utils/prompts.d.ts +10 -4
  130. package/dist/utils/prompts.d.ts.map +1 -0
  131. package/dist/utils/prompts.js +40 -36
  132. package/dist/utils/prompts.js.map +1 -0
  133. package/dist/utils/standard.cjs +315 -0
  134. package/dist/utils/standard.cjs.map +1 -0
  135. package/dist/utils/standard.js +313 -0
  136. package/dist/utils/standard.js.map +1 -0
  137. package/dist/utils/tools.cjs +150 -127
  138. package/dist/utils/tools.cjs.map +1 -0
  139. package/dist/utils/tools.d.cts +13 -0
  140. package/dist/utils/tools.d.cts.map +1 -0
  141. package/dist/utils/tools.d.ts +12 -42
  142. package/dist/utils/tools.d.ts.map +1 -0
  143. package/dist/utils/tools.js +146 -123
  144. package/dist/utils/tools.js.map +1 -0
  145. package/package.json +51 -60
  146. package/dist/tools/index.d.ts +0 -1
  147. package/dist/types.cjs +0 -2
  148. package/dist/types.js +0 -1
  149. package/dist/utils/errors.d.ts +0 -2
  150. package/dist/utils/headers.cjs +0 -39
  151. package/dist/utils/headers.d.ts +0 -8
  152. package/dist/utils/headers.js +0 -35
  153. package/dist/utils/openai-format-fndef.cjs +0 -80
  154. package/dist/utils/openai-format-fndef.d.ts +0 -44
  155. package/dist/utils/openai-format-fndef.js +0 -77
  156. package/dist/utils/openai.cjs +0 -127
  157. package/dist/utils/openai.d.ts +0 -15
  158. package/dist/utils/openai.js +0 -119
  159. package/dist/utils/output.d.ts +0 -11
  160. package/index.cjs +0 -1
  161. package/index.d.cts +0 -1
  162. package/index.d.ts +0 -1
  163. package/index.js +0 -1
@@ -0,0 +1,315 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_misc = require('./misc.cjs');
3
+ const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
4
+
5
+ //#region src/utils/standard.ts
6
+ function _convertToChatCompletionsData(block) {
7
+ if (block.type === "image") {
8
+ if (block.url) return {
9
+ type: "image_url",
10
+ image_url: { url: block.url }
11
+ };
12
+ else if (block.data) return {
13
+ type: "image_url",
14
+ image_url: { url: `data:${block.mimeType};base64,${block.data}` }
15
+ };
16
+ }
17
+ if (block.type === "audio") {
18
+ if (block.data) {
19
+ const format = require_misc.iife(() => {
20
+ const [, format$1] = block.mimeType.split("/");
21
+ if (format$1 === "wav" || format$1 === "mp3") return format$1;
22
+ return "wav";
23
+ });
24
+ return {
25
+ type: "input_audio",
26
+ input_audio: {
27
+ data: block.data.toString(),
28
+ format
29
+ }
30
+ };
31
+ }
32
+ }
33
+ if (block.type === "file") {
34
+ if (block.data) return {
35
+ type: "file",
36
+ file: { file_data: block.data.toString() }
37
+ };
38
+ if (block.fileId) return {
39
+ type: "file",
40
+ file: { file_id: block.fileId }
41
+ };
42
+ }
43
+ return void 0;
44
+ }
45
+ function _convertToCompletionsMessageFromV1(message, model) {
46
+ let role = require_misc.messageToOpenAIRole(message);
47
+ if (role === "system" && require_misc.isReasoningModel(model)) role = "developer";
48
+ if (role === "developer") return {
49
+ role: "developer",
50
+ content: message.contentBlocks.filter((block) => block.type === "text")
51
+ };
52
+ else if (role === "system") return {
53
+ role: "system",
54
+ content: message.contentBlocks.filter((block) => block.type === "text")
55
+ };
56
+ else if (role === "assistant") return {
57
+ role: "assistant",
58
+ content: message.contentBlocks.filter((block) => block.type === "text")
59
+ };
60
+ else if (role === "tool" && __langchain_core_messages.ToolMessage.isInstance(message)) return {
61
+ role: "tool",
62
+ tool_call_id: message.tool_call_id,
63
+ content: message.contentBlocks.filter((block) => block.type === "text")
64
+ };
65
+ else if (role === "function") return {
66
+ role: "function",
67
+ name: message.name ?? "",
68
+ content: message.contentBlocks.filter((block) => block.type === "text").join("")
69
+ };
70
+ function* iterateUserContent(blocks) {
71
+ for (const block of blocks) {
72
+ if (block.type === "text") yield {
73
+ type: "text",
74
+ text: block.text
75
+ };
76
+ const data = _convertToChatCompletionsData(block);
77
+ if (data) yield data;
78
+ }
79
+ }
80
+ return {
81
+ role: "user",
82
+ content: Array.from(iterateUserContent(message.contentBlocks))
83
+ };
84
+ }
85
+ function _convertToResponsesMessageFromV1(message) {
86
+ const isResponsesMessage = (0, __langchain_core_messages.isAIMessage)(message) && message.response_metadata?.model_provider === "openai";
87
+ function* iterateItems() {
88
+ const messageRole = require_misc.iife(() => {
89
+ try {
90
+ const role = require_misc.messageToOpenAIRole(message);
91
+ if (role === "system" || role === "developer" || role === "assistant" || role === "user") return role;
92
+ return "assistant";
93
+ } catch {
94
+ return "assistant";
95
+ }
96
+ });
97
+ let currentMessage = void 0;
98
+ const functionCallIdsWithBlocks = /* @__PURE__ */ new Set();
99
+ const serverFunctionCallIdsWithBlocks = /* @__PURE__ */ new Set();
100
+ const pendingFunctionChunks = /* @__PURE__ */ new Map();
101
+ const pendingServerFunctionChunks = /* @__PURE__ */ new Map();
102
+ function* flushMessage() {
103
+ if (!currentMessage) return;
104
+ const content = currentMessage.content;
105
+ if (typeof content === "string" && content.length > 0 || Array.isArray(content) && content.length > 0) yield currentMessage;
106
+ currentMessage = void 0;
107
+ }
108
+ const pushMessageContent = (content) => {
109
+ if (!currentMessage) currentMessage = {
110
+ type: "message",
111
+ role: messageRole,
112
+ content: []
113
+ };
114
+ if (typeof currentMessage.content === "string") currentMessage.content = currentMessage.content.length > 0 ? [{
115
+ type: "input_text",
116
+ text: currentMessage.content
117
+ }, ...content] : [...content];
118
+ else currentMessage.content.push(...content);
119
+ };
120
+ const toJsonString = (value) => {
121
+ if (typeof value === "string") return value;
122
+ try {
123
+ return JSON.stringify(value ?? {});
124
+ } catch {
125
+ return "{}";
126
+ }
127
+ };
128
+ const resolveImageItem = (block) => {
129
+ const detail = require_misc.iife(() => {
130
+ const raw = block.metadata?.detail;
131
+ if (raw === "low" || raw === "high" || raw === "auto") return raw;
132
+ return "auto";
133
+ });
134
+ if (block.fileId) return {
135
+ type: "input_image",
136
+ detail,
137
+ file_id: block.fileId
138
+ };
139
+ if (block.url) return {
140
+ type: "input_image",
141
+ detail,
142
+ image_url: block.url
143
+ };
144
+ if (block.data) {
145
+ const base64Data = typeof block.data === "string" ? block.data : Buffer.from(block.data).toString("base64");
146
+ const mimeType = block.mimeType ?? "image/png";
147
+ return {
148
+ type: "input_image",
149
+ detail,
150
+ image_url: `data:${mimeType};base64,${base64Data}`
151
+ };
152
+ }
153
+ return void 0;
154
+ };
155
+ const resolveFileItem = (block) => {
156
+ const filename = block.metadata?.filename ?? block.metadata?.name ?? block.metadata?.title;
157
+ if (block.fileId && typeof filename === "string") return {
158
+ type: "input_file",
159
+ file_id: block.fileId,
160
+ ...filename ? { filename } : {}
161
+ };
162
+ if (block.url && typeof filename === "string") return {
163
+ type: "input_file",
164
+ file_url: block.url,
165
+ ...filename ? { filename } : {}
166
+ };
167
+ if (block.data && typeof filename === "string") {
168
+ const encoded = typeof block.data === "string" ? block.data : Buffer.from(block.data).toString("base64");
169
+ const mimeType = block.mimeType ?? "application/octet-stream";
170
+ return {
171
+ type: "input_file",
172
+ file_data: `data:${mimeType};base64,${encoded}`,
173
+ ...filename ? { filename } : {}
174
+ };
175
+ }
176
+ return void 0;
177
+ };
178
+ const convertReasoningBlock = (block) => {
179
+ const summaryEntries = require_misc.iife(() => {
180
+ if (Array.isArray(block.summary)) {
181
+ const candidate = block.summary;
182
+ const mapped = candidate?.map((item) => item?.text).filter((text) => typeof text === "string") ?? [];
183
+ if (mapped.length > 0) return mapped;
184
+ }
185
+ return block.reasoning ? [block.reasoning] : [];
186
+ });
187
+ const summary = summaryEntries.length > 0 ? summaryEntries.map((text) => ({
188
+ type: "summary_text",
189
+ text
190
+ })) : [{
191
+ type: "summary_text",
192
+ text: ""
193
+ }];
194
+ const reasoningItem = {
195
+ type: "reasoning",
196
+ id: block.id ?? "",
197
+ summary
198
+ };
199
+ if (block.reasoning) reasoningItem.content = [{
200
+ type: "reasoning_text",
201
+ text: block.reasoning
202
+ }];
203
+ return reasoningItem;
204
+ };
205
+ const convertFunctionCall = (block) => ({
206
+ type: "function_call",
207
+ name: block.name ?? "",
208
+ call_id: block.id ?? "",
209
+ arguments: toJsonString(block.args)
210
+ });
211
+ const convertFunctionCallOutput = (block) => {
212
+ const output = toJsonString(block.output);
213
+ const status = block.status === "success" ? "completed" : block.status === "error" ? "incomplete" : void 0;
214
+ return {
215
+ type: "function_call_output",
216
+ call_id: block.toolCallId ?? "",
217
+ output,
218
+ ...status ? { status } : {}
219
+ };
220
+ };
221
+ for (const block of message.contentBlocks) if (block.type === "text") pushMessageContent([{
222
+ type: "input_text",
223
+ text: block.text
224
+ }]);
225
+ else if (block.type === "invalid_tool_call") {} else if (block.type === "reasoning") {
226
+ yield* flushMessage();
227
+ yield convertReasoningBlock(block);
228
+ } else if (block.type === "tool_call") {
229
+ yield* flushMessage();
230
+ const id = block.id ?? "";
231
+ if (id) {
232
+ functionCallIdsWithBlocks.add(id);
233
+ pendingFunctionChunks.delete(id);
234
+ }
235
+ yield convertFunctionCall(block);
236
+ } else if (block.type === "tool_call_chunk") {
237
+ if (block.id) {
238
+ const existing = pendingFunctionChunks.get(block.id) ?? {
239
+ name: block.name,
240
+ args: []
241
+ };
242
+ if (block.name) existing.name = block.name;
243
+ if (block.args) existing.args.push(block.args);
244
+ pendingFunctionChunks.set(block.id, existing);
245
+ }
246
+ } else if (block.type === "server_tool_call") {
247
+ yield* flushMessage();
248
+ const id = block.id ?? "";
249
+ if (id) {
250
+ serverFunctionCallIdsWithBlocks.add(id);
251
+ pendingServerFunctionChunks.delete(id);
252
+ }
253
+ yield convertFunctionCall(block);
254
+ } else if (block.type === "server_tool_call_chunk") {
255
+ if (block.id) {
256
+ const existing = pendingServerFunctionChunks.get(block.id) ?? {
257
+ name: block.name,
258
+ args: []
259
+ };
260
+ if (block.name) existing.name = block.name;
261
+ if (block.args) existing.args.push(block.args);
262
+ pendingServerFunctionChunks.set(block.id, existing);
263
+ }
264
+ } else if (block.type === "server_tool_call_result") {
265
+ yield* flushMessage();
266
+ yield convertFunctionCallOutput(block);
267
+ } else if (block.type === "audio") {} else if (block.type === "file") {
268
+ const fileItem = resolveFileItem(block);
269
+ if (fileItem) pushMessageContent([fileItem]);
270
+ } else if (block.type === "image") {
271
+ const imageItem = resolveImageItem(block);
272
+ if (imageItem) pushMessageContent([imageItem]);
273
+ } else if (block.type === "video") {
274
+ const videoItem = resolveFileItem(block);
275
+ if (videoItem) pushMessageContent([videoItem]);
276
+ } else if (block.type === "text-plain") {
277
+ if (block.text) pushMessageContent([{
278
+ type: "input_text",
279
+ text: block.text
280
+ }]);
281
+ } else if (block.type === "non_standard" && isResponsesMessage) {
282
+ yield* flushMessage();
283
+ yield block.value;
284
+ }
285
+ yield* flushMessage();
286
+ for (const [id, chunk] of pendingFunctionChunks) {
287
+ if (!id || functionCallIdsWithBlocks.has(id)) continue;
288
+ const args = chunk.args.join("");
289
+ if (!chunk.name && !args) continue;
290
+ yield {
291
+ type: "function_call",
292
+ call_id: id,
293
+ name: chunk.name ?? "",
294
+ arguments: args
295
+ };
296
+ }
297
+ for (const [id, chunk] of pendingServerFunctionChunks) {
298
+ if (!id || serverFunctionCallIdsWithBlocks.has(id)) continue;
299
+ const args = chunk.args.join("");
300
+ if (!chunk.name && !args) continue;
301
+ yield {
302
+ type: "function_call",
303
+ call_id: id,
304
+ name: chunk.name ?? "",
305
+ arguments: args
306
+ };
307
+ }
308
+ }
309
+ return Array.from(iterateItems());
310
+ }
311
+
312
+ //#endregion
313
+ exports._convertToCompletionsMessageFromV1 = _convertToCompletionsMessageFromV1;
314
+ exports._convertToResponsesMessageFromV1 = _convertToResponsesMessageFromV1;
315
+ //# sourceMappingURL=standard.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standard.cjs","names":["block: ContentBlock.Standard","iife","format","message: BaseMessage","model?: string","messageToOpenAIRole","isReasoningModel","ToolMessage","blocks: ContentBlock.Standard[]","currentMessage: OpenAIClient.Responses.EasyInputMessage | undefined","pushMessageContent: (\n content: OpenAIClient.Responses.ResponseInputMessageContentList\n ) => void","value: unknown","block: ContentBlock.Multimodal.Image","block: ContentBlock.Multimodal.File | ContentBlock.Multimodal.Video","block: ContentBlock.Reasoning","reasoningItem: OpenAIClient.Responses.ResponseReasoningItem","block: ContentBlock.Tools.ToolCall | ContentBlock.Tools.ServerToolCall","block: ContentBlock.Tools.ServerToolCallResult"],"sources":["../../src/utils/standard.ts"],"sourcesContent":["import type { OpenAI as OpenAIClient } from \"openai\";\nimport {\n isAIMessage,\n BaseMessage,\n ToolMessage,\n type ContentBlock,\n} from \"@langchain/core/messages\";\nimport { iife, isReasoningModel, messageToOpenAIRole } from \"./misc.js\";\n\nexport type ResponsesInputItem = OpenAIClient.Responses.ResponseInputItem;\n\ntype ChatCompletionContentData =\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartImage\n | OpenAIClient.Chat.Completions.ChatCompletionContentPartInputAudio\n | OpenAIClient.Chat.Completions.ChatCompletionContentPart.File;\n\nfunction _convertToChatCompletionsData(\n block: ContentBlock.Standard\n): ChatCompletionContentData | undefined {\n if (block.type === \"image\") {\n if (block.url) {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n },\n };\n } else if (block.data) {\n return {\n type: \"image_url\",\n image_url: {\n url: `data:${block.mimeType};base64,${block.data}`,\n },\n };\n }\n }\n if (block.type === \"audio\") {\n if (block.data) {\n const format = iife(() => {\n const [, format] = block.mimeType.split(\"/\");\n if (format === \"wav\" || format === \"mp3\") {\n return format;\n }\n return \"wav\";\n });\n return {\n type: \"input_audio\",\n input_audio: {\n data: block.data.toString(),\n format,\n },\n };\n }\n }\n if (block.type === \"file\") {\n if (block.data) {\n return {\n type: \"file\",\n file: {\n file_data: block.data.toString(),\n },\n };\n }\n if (block.fileId) {\n return {\n type: \"file\",\n file: {\n file_id: block.fileId,\n },\n };\n }\n }\n return undefined;\n}\n\nexport function _convertToCompletionsMessageFromV1(\n message: BaseMessage,\n model?: string\n): OpenAIClient.Chat.Completions.ChatCompletionMessageParam {\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n if (role === \"developer\") {\n return {\n role: \"developer\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"system\") {\n return {\n role: \"system\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"assistant\") {\n return {\n role: \"assistant\",\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"tool\" && ToolMessage.isInstance(message)) {\n return {\n role: \"tool\",\n tool_call_id: message.tool_call_id,\n content: message.contentBlocks.filter((block) => block.type === \"text\"),\n };\n } else if (role === \"function\") {\n return {\n role: \"function\",\n name: message.name ?? \"\",\n content: message.contentBlocks\n .filter((block) => block.type === \"text\")\n .join(\"\"),\n };\n }\n // Default to user message handling\n function* iterateUserContent(blocks: ContentBlock.Standard[]) {\n for (const block of blocks) {\n if (block.type === \"text\") {\n yield {\n type: \"text\" as const,\n text: block.text,\n };\n }\n const data = _convertToChatCompletionsData(block);\n if (data) {\n yield data;\n }\n }\n }\n return {\n role: \"user\",\n content: Array.from(iterateUserContent(message.contentBlocks)),\n };\n}\n\nexport function _convertToResponsesMessageFromV1(message: BaseMessage) {\n const isResponsesMessage =\n isAIMessage(message) &&\n message.response_metadata?.model_provider === \"openai\";\n\n function* iterateItems(): Generator<OpenAIClient.Responses.ResponseInputItem> {\n const messageRole = iife(() => {\n try {\n const role = messageToOpenAIRole(message);\n if (\n role === \"system\" ||\n role === \"developer\" ||\n role === \"assistant\" ||\n role === \"user\"\n ) {\n return role;\n }\n return \"assistant\";\n } catch {\n return \"assistant\";\n }\n });\n\n let currentMessage: OpenAIClient.Responses.EasyInputMessage | undefined =\n undefined;\n\n const functionCallIdsWithBlocks = new Set<string>();\n const serverFunctionCallIdsWithBlocks = new Set<string>();\n\n const pendingFunctionChunks = new Map<\n string,\n { name?: string; args: string[] }\n >();\n const pendingServerFunctionChunks = new Map<\n string,\n { name?: string; args: string[] }\n >();\n\n function* flushMessage() {\n if (!currentMessage) return;\n const content = currentMessage.content;\n if (\n (typeof content === \"string\" && content.length > 0) ||\n (Array.isArray(content) && content.length > 0)\n ) {\n yield currentMessage;\n }\n currentMessage = undefined;\n }\n\n const pushMessageContent: (\n content: OpenAIClient.Responses.ResponseInputMessageContentList\n ) => void = (content) => {\n if (!currentMessage) {\n currentMessage = {\n type: \"message\",\n role: messageRole,\n content: [],\n };\n }\n if (typeof currentMessage.content === \"string\") {\n currentMessage.content =\n currentMessage.content.length > 0\n ? [{ type: \"input_text\", text: currentMessage.content }, ...content]\n : [...content];\n } else {\n currentMessage.content.push(...content);\n }\n };\n\n const toJsonString = (value: unknown) => {\n if (typeof value === \"string\") {\n return value;\n }\n try {\n return JSON.stringify(value ?? {});\n } catch {\n return \"{}\";\n }\n };\n\n const resolveImageItem = (\n block: ContentBlock.Multimodal.Image\n ): OpenAIClient.Responses.ResponseInputImage | undefined => {\n const detail = iife(() => {\n const raw = block.metadata?.detail;\n if (raw === \"low\" || raw === \"high\" || raw === \"auto\") {\n return raw;\n }\n return \"auto\";\n });\n if (block.fileId) {\n return {\n type: \"input_image\",\n detail,\n file_id: block.fileId,\n };\n }\n if (block.url) {\n return {\n type: \"input_image\",\n detail,\n image_url: block.url,\n };\n }\n if (block.data) {\n const base64Data =\n typeof block.data === \"string\"\n ? block.data\n : Buffer.from(block.data).toString(\"base64\");\n const mimeType = block.mimeType ?? \"image/png\";\n return {\n type: \"input_image\",\n detail,\n image_url: `data:${mimeType};base64,${base64Data}`,\n };\n }\n return undefined;\n };\n\n const resolveFileItem = (\n block: ContentBlock.Multimodal.File | ContentBlock.Multimodal.Video\n ): OpenAIClient.Responses.ResponseInputFile | undefined => {\n const filename =\n block.metadata?.filename ??\n block.metadata?.name ??\n block.metadata?.title;\n if (block.fileId && typeof filename === \"string\") {\n return {\n type: \"input_file\",\n file_id: block.fileId,\n ...(filename ? { filename } : {}),\n };\n }\n if (block.url && typeof filename === \"string\") {\n return {\n type: \"input_file\",\n file_url: block.url,\n ...(filename ? { filename } : {}),\n };\n }\n if (block.data && typeof filename === \"string\") {\n const encoded =\n typeof block.data === \"string\"\n ? block.data\n : Buffer.from(block.data).toString(\"base64\");\n const mimeType = block.mimeType ?? \"application/octet-stream\";\n return {\n type: \"input_file\",\n file_data: `data:${mimeType};base64,${encoded}`,\n ...(filename ? { filename } : {}),\n };\n }\n return undefined;\n };\n\n const convertReasoningBlock = (\n block: ContentBlock.Reasoning\n ): OpenAIClient.Responses.ResponseReasoningItem => {\n const summaryEntries = iife(() => {\n if (Array.isArray(block.summary)) {\n const candidate = block.summary;\n const mapped =\n candidate\n ?.map((item) => item?.text)\n .filter((text): text is string => typeof text === \"string\") ?? [];\n if (mapped.length > 0) {\n return mapped;\n }\n }\n return block.reasoning ? [block.reasoning] : [];\n });\n\n const summary =\n summaryEntries.length > 0\n ? summaryEntries.map((text) => ({\n type: \"summary_text\" as const,\n text,\n }))\n : [{ type: \"summary_text\" as const, text: \"\" }];\n\n const reasoningItem: OpenAIClient.Responses.ResponseReasoningItem = {\n type: \"reasoning\",\n id: block.id ?? \"\",\n summary,\n };\n\n if (block.reasoning) {\n reasoningItem.content = [\n {\n type: \"reasoning_text\" as const,\n text: block.reasoning,\n },\n ];\n }\n return reasoningItem;\n };\n\n const convertFunctionCall = (\n block: ContentBlock.Tools.ToolCall | ContentBlock.Tools.ServerToolCall\n ): OpenAIClient.Responses.ResponseFunctionToolCall => ({\n type: \"function_call\",\n name: block.name ?? \"\",\n call_id: block.id ?? \"\",\n arguments: toJsonString(block.args),\n });\n\n const convertFunctionCallOutput = (\n block: ContentBlock.Tools.ServerToolCallResult\n ): OpenAIClient.Responses.ResponseInputItem.FunctionCallOutput => {\n const output = toJsonString(block.output);\n const status =\n block.status === \"success\"\n ? \"completed\"\n : block.status === \"error\"\n ? \"incomplete\"\n : undefined;\n return {\n type: \"function_call_output\",\n call_id: block.toolCallId ?? \"\",\n output,\n ...(status ? { status } : {}),\n };\n };\n\n for (const block of message.contentBlocks) {\n if (block.type === \"text\") {\n pushMessageContent([{ type: \"input_text\", text: block.text }]);\n } else if (block.type === \"invalid_tool_call\") {\n // no-op\n } else if (block.type === \"reasoning\") {\n yield* flushMessage();\n yield convertReasoningBlock(\n block as ContentBlock.Standard & { type: \"reasoning\" }\n );\n } else if (block.type === \"tool_call\") {\n yield* flushMessage();\n const id = block.id ?? \"\";\n if (id) {\n functionCallIdsWithBlocks.add(id);\n pendingFunctionChunks.delete(id);\n }\n yield convertFunctionCall(\n block as ContentBlock.Standard & { type: \"tool_call\" }\n );\n } else if (block.type === \"tool_call_chunk\") {\n if (block.id) {\n const existing = pendingFunctionChunks.get(block.id) ?? {\n name: block.name,\n args: [],\n };\n if (block.name) existing.name = block.name;\n if (block.args) existing.args.push(block.args);\n pendingFunctionChunks.set(block.id, existing);\n }\n } else if (block.type === \"server_tool_call\") {\n yield* flushMessage();\n const id = block.id ?? \"\";\n if (id) {\n serverFunctionCallIdsWithBlocks.add(id);\n pendingServerFunctionChunks.delete(id);\n }\n yield convertFunctionCall(block);\n } else if (block.type === \"server_tool_call_chunk\") {\n if (block.id) {\n const existing = pendingServerFunctionChunks.get(block.id) ?? {\n name: block.name,\n args: [],\n };\n if (block.name) existing.name = block.name;\n if (block.args) existing.args.push(block.args);\n pendingServerFunctionChunks.set(block.id, existing);\n }\n } else if (block.type === \"server_tool_call_result\") {\n yield* flushMessage();\n yield convertFunctionCallOutput(block);\n } else if (block.type === \"audio\") {\n // no-op\n } else if (block.type === \"file\") {\n const fileItem = resolveFileItem(block);\n if (fileItem) {\n pushMessageContent([fileItem]);\n }\n } else if (block.type === \"image\") {\n const imageItem = resolveImageItem(block);\n if (imageItem) {\n pushMessageContent([imageItem]);\n }\n } else if (block.type === \"video\") {\n const videoItem = resolveFileItem(block);\n if (videoItem) {\n pushMessageContent([videoItem]);\n }\n } else if (block.type === \"text-plain\") {\n if (block.text) {\n pushMessageContent([\n {\n type: \"input_text\",\n text: block.text,\n },\n ]);\n }\n } else if (block.type === \"non_standard\" && isResponsesMessage) {\n yield* flushMessage();\n yield block.value as ResponsesInputItem;\n }\n }\n yield* flushMessage();\n\n for (const [id, chunk] of pendingFunctionChunks) {\n if (!id || functionCallIdsWithBlocks.has(id)) continue;\n const args = chunk.args.join(\"\");\n if (!chunk.name && !args) continue;\n yield {\n type: \"function_call\",\n call_id: id,\n name: chunk.name ?? \"\",\n arguments: args,\n };\n }\n\n for (const [id, chunk] of pendingServerFunctionChunks) {\n if (!id || serverFunctionCallIdsWithBlocks.has(id)) continue;\n const args = chunk.args.join(\"\");\n if (!chunk.name && !args) continue;\n yield {\n type: \"function_call\",\n call_id: id,\n name: chunk.name ?? \"\",\n arguments: args,\n };\n }\n }\n return Array.from(iterateItems());\n}\n"],"mappings":";;;;;AAgBA,SAAS,8BACPA,OACuC;AACvC,KAAI,MAAM,SAAS,SACjB;MAAI,MAAM,IACR,QAAO;GACL,MAAM;GACN,WAAW,EACT,KAAK,MAAM,IACZ;EACF;WACQ,MAAM,KACf,QAAO;GACL,MAAM;GACN,WAAW,EACT,KAAK,CAAC,KAAK,EAAE,MAAM,SAAS,QAAQ,EAAE,MAAM,MAAM,CACnD;EACF;CACF;AAEH,KAAI,MAAM,SAAS,SACjB;MAAI,MAAM,MAAM;GACd,MAAM,SAASC,kBAAK,MAAM;IACxB,MAAM,GAAGC,SAAO,GAAG,MAAM,SAAS,MAAM,IAAI;AAC5C,QAAIA,aAAW,SAASA,aAAW,MACjC,QAAOA;AAET,WAAO;GACR,EAAC;AACF,UAAO;IACL,MAAM;IACN,aAAa;KACX,MAAM,MAAM,KAAK,UAAU;KAC3B;IACD;GACF;EACF;;AAEH,KAAI,MAAM,SAAS,QAAQ;AACzB,MAAI,MAAM,KACR,QAAO;GACL,MAAM;GACN,MAAM,EACJ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AkB,oBAAoB;GAC9D,OAAO,cAAc;GACrB,MAAM,MAAM;EACb;EAEH,OAAO,cAAc;AAErB,OAAK,MAAM,CAAC,IAAI,MAAM,IAAI,uBAAuB;AAC/C,OAAI,CAAC,MAAM,0BAA0B,IAAI,GAAG,CAAE;GAC9C,MAAM,OAAO,MAAM,KAAK,KAAK,GAAG;AAChC,OAAI,CAAC,MAAM,QAAQ,CAAC,KAAM;GAC1B,MAAM;IACJ,MAAM;IACN,SAAS;IACT,MAAM,MAAM,QAAQ;IACpB,WAAW;GACZ;EACF;AAED,OAAK,MAAM,CAAC,IAAI,MAAM,IAAI,6BAA6B;AACrD,OAAI,CAAC,MAAM,gCAAgC,IAAI,GAAG,CAAE;GACpD,MAAM,OAAO,MAAM,KAAK,KAAK,GAAG;AAChC,OAAI,CAAC,MAAM,QAAQ,CAAC,KAAM;GAC1B,MAAM;IACJ,MAAM;IACN,SAAS;IACT,MAAM,MAAM,QAAQ;IACpB,WAAW;GACZ;EACF;CACF;AACD,QAAO,MAAM,KAAK,cAAc,CAAC;AAClC"}
@@ -0,0 +1,313 @@
1
+ import { iife, isReasoningModel, messageToOpenAIRole } from "./misc.js";
2
+ import { ToolMessage, isAIMessage } from "@langchain/core/messages";
3
+
4
+ //#region src/utils/standard.ts
5
+ function _convertToChatCompletionsData(block) {
6
+ if (block.type === "image") {
7
+ if (block.url) return {
8
+ type: "image_url",
9
+ image_url: { url: block.url }
10
+ };
11
+ else if (block.data) return {
12
+ type: "image_url",
13
+ image_url: { url: `data:${block.mimeType};base64,${block.data}` }
14
+ };
15
+ }
16
+ if (block.type === "audio") {
17
+ if (block.data) {
18
+ const format = iife(() => {
19
+ const [, format$1] = block.mimeType.split("/");
20
+ if (format$1 === "wav" || format$1 === "mp3") return format$1;
21
+ return "wav";
22
+ });
23
+ return {
24
+ type: "input_audio",
25
+ input_audio: {
26
+ data: block.data.toString(),
27
+ format
28
+ }
29
+ };
30
+ }
31
+ }
32
+ if (block.type === "file") {
33
+ if (block.data) return {
34
+ type: "file",
35
+ file: { file_data: block.data.toString() }
36
+ };
37
+ if (block.fileId) return {
38
+ type: "file",
39
+ file: { file_id: block.fileId }
40
+ };
41
+ }
42
+ return void 0;
43
+ }
44
+ function _convertToCompletionsMessageFromV1(message, model) {
45
+ let role = messageToOpenAIRole(message);
46
+ if (role === "system" && isReasoningModel(model)) role = "developer";
47
+ if (role === "developer") return {
48
+ role: "developer",
49
+ content: message.contentBlocks.filter((block) => block.type === "text")
50
+ };
51
+ else if (role === "system") return {
52
+ role: "system",
53
+ content: message.contentBlocks.filter((block) => block.type === "text")
54
+ };
55
+ else if (role === "assistant") return {
56
+ role: "assistant",
57
+ content: message.contentBlocks.filter((block) => block.type === "text")
58
+ };
59
+ else if (role === "tool" && ToolMessage.isInstance(message)) return {
60
+ role: "tool",
61
+ tool_call_id: message.tool_call_id,
62
+ content: message.contentBlocks.filter((block) => block.type === "text")
63
+ };
64
+ else if (role === "function") return {
65
+ role: "function",
66
+ name: message.name ?? "",
67
+ content: message.contentBlocks.filter((block) => block.type === "text").join("")
68
+ };
69
+ function* iterateUserContent(blocks) {
70
+ for (const block of blocks) {
71
+ if (block.type === "text") yield {
72
+ type: "text",
73
+ text: block.text
74
+ };
75
+ const data = _convertToChatCompletionsData(block);
76
+ if (data) yield data;
77
+ }
78
+ }
79
+ return {
80
+ role: "user",
81
+ content: Array.from(iterateUserContent(message.contentBlocks))
82
+ };
83
+ }
84
+ function _convertToResponsesMessageFromV1(message) {
85
+ const isResponsesMessage = isAIMessage(message) && message.response_metadata?.model_provider === "openai";
86
+ function* iterateItems() {
87
+ const messageRole = iife(() => {
88
+ try {
89
+ const role = messageToOpenAIRole(message);
90
+ if (role === "system" || role === "developer" || role === "assistant" || role === "user") return role;
91
+ return "assistant";
92
+ } catch {
93
+ return "assistant";
94
+ }
95
+ });
96
+ let currentMessage = void 0;
97
+ const functionCallIdsWithBlocks = /* @__PURE__ */ new Set();
98
+ const serverFunctionCallIdsWithBlocks = /* @__PURE__ */ new Set();
99
+ const pendingFunctionChunks = /* @__PURE__ */ new Map();
100
+ const pendingServerFunctionChunks = /* @__PURE__ */ new Map();
101
+ function* flushMessage() {
102
+ if (!currentMessage) return;
103
+ const content = currentMessage.content;
104
+ if (typeof content === "string" && content.length > 0 || Array.isArray(content) && content.length > 0) yield currentMessage;
105
+ currentMessage = void 0;
106
+ }
107
+ const pushMessageContent = (content) => {
108
+ if (!currentMessage) currentMessage = {
109
+ type: "message",
110
+ role: messageRole,
111
+ content: []
112
+ };
113
+ if (typeof currentMessage.content === "string") currentMessage.content = currentMessage.content.length > 0 ? [{
114
+ type: "input_text",
115
+ text: currentMessage.content
116
+ }, ...content] : [...content];
117
+ else currentMessage.content.push(...content);
118
+ };
119
+ const toJsonString = (value) => {
120
+ if (typeof value === "string") return value;
121
+ try {
122
+ return JSON.stringify(value ?? {});
123
+ } catch {
124
+ return "{}";
125
+ }
126
+ };
127
+ const resolveImageItem = (block) => {
128
+ const detail = iife(() => {
129
+ const raw = block.metadata?.detail;
130
+ if (raw === "low" || raw === "high" || raw === "auto") return raw;
131
+ return "auto";
132
+ });
133
+ if (block.fileId) return {
134
+ type: "input_image",
135
+ detail,
136
+ file_id: block.fileId
137
+ };
138
+ if (block.url) return {
139
+ type: "input_image",
140
+ detail,
141
+ image_url: block.url
142
+ };
143
+ if (block.data) {
144
+ const base64Data = typeof block.data === "string" ? block.data : Buffer.from(block.data).toString("base64");
145
+ const mimeType = block.mimeType ?? "image/png";
146
+ return {
147
+ type: "input_image",
148
+ detail,
149
+ image_url: `data:${mimeType};base64,${base64Data}`
150
+ };
151
+ }
152
+ return void 0;
153
+ };
154
+ const resolveFileItem = (block) => {
155
+ const filename = block.metadata?.filename ?? block.metadata?.name ?? block.metadata?.title;
156
+ if (block.fileId && typeof filename === "string") return {
157
+ type: "input_file",
158
+ file_id: block.fileId,
159
+ ...filename ? { filename } : {}
160
+ };
161
+ if (block.url && typeof filename === "string") return {
162
+ type: "input_file",
163
+ file_url: block.url,
164
+ ...filename ? { filename } : {}
165
+ };
166
+ if (block.data && typeof filename === "string") {
167
+ const encoded = typeof block.data === "string" ? block.data : Buffer.from(block.data).toString("base64");
168
+ const mimeType = block.mimeType ?? "application/octet-stream";
169
+ return {
170
+ type: "input_file",
171
+ file_data: `data:${mimeType};base64,${encoded}`,
172
+ ...filename ? { filename } : {}
173
+ };
174
+ }
175
+ return void 0;
176
+ };
177
+ const convertReasoningBlock = (block) => {
178
+ const summaryEntries = iife(() => {
179
+ if (Array.isArray(block.summary)) {
180
+ const candidate = block.summary;
181
+ const mapped = candidate?.map((item) => item?.text).filter((text) => typeof text === "string") ?? [];
182
+ if (mapped.length > 0) return mapped;
183
+ }
184
+ return block.reasoning ? [block.reasoning] : [];
185
+ });
186
+ const summary = summaryEntries.length > 0 ? summaryEntries.map((text) => ({
187
+ type: "summary_text",
188
+ text
189
+ })) : [{
190
+ type: "summary_text",
191
+ text: ""
192
+ }];
193
+ const reasoningItem = {
194
+ type: "reasoning",
195
+ id: block.id ?? "",
196
+ summary
197
+ };
198
+ if (block.reasoning) reasoningItem.content = [{
199
+ type: "reasoning_text",
200
+ text: block.reasoning
201
+ }];
202
+ return reasoningItem;
203
+ };
204
+ const convertFunctionCall = (block) => ({
205
+ type: "function_call",
206
+ name: block.name ?? "",
207
+ call_id: block.id ?? "",
208
+ arguments: toJsonString(block.args)
209
+ });
210
+ const convertFunctionCallOutput = (block) => {
211
+ const output = toJsonString(block.output);
212
+ const status = block.status === "success" ? "completed" : block.status === "error" ? "incomplete" : void 0;
213
+ return {
214
+ type: "function_call_output",
215
+ call_id: block.toolCallId ?? "",
216
+ output,
217
+ ...status ? { status } : {}
218
+ };
219
+ };
220
+ for (const block of message.contentBlocks) if (block.type === "text") pushMessageContent([{
221
+ type: "input_text",
222
+ text: block.text
223
+ }]);
224
+ else if (block.type === "invalid_tool_call") {} else if (block.type === "reasoning") {
225
+ yield* flushMessage();
226
+ yield convertReasoningBlock(block);
227
+ } else if (block.type === "tool_call") {
228
+ yield* flushMessage();
229
+ const id = block.id ?? "";
230
+ if (id) {
231
+ functionCallIdsWithBlocks.add(id);
232
+ pendingFunctionChunks.delete(id);
233
+ }
234
+ yield convertFunctionCall(block);
235
+ } else if (block.type === "tool_call_chunk") {
236
+ if (block.id) {
237
+ const existing = pendingFunctionChunks.get(block.id) ?? {
238
+ name: block.name,
239
+ args: []
240
+ };
241
+ if (block.name) existing.name = block.name;
242
+ if (block.args) existing.args.push(block.args);
243
+ pendingFunctionChunks.set(block.id, existing);
244
+ }
245
+ } else if (block.type === "server_tool_call") {
246
+ yield* flushMessage();
247
+ const id = block.id ?? "";
248
+ if (id) {
249
+ serverFunctionCallIdsWithBlocks.add(id);
250
+ pendingServerFunctionChunks.delete(id);
251
+ }
252
+ yield convertFunctionCall(block);
253
+ } else if (block.type === "server_tool_call_chunk") {
254
+ if (block.id) {
255
+ const existing = pendingServerFunctionChunks.get(block.id) ?? {
256
+ name: block.name,
257
+ args: []
258
+ };
259
+ if (block.name) existing.name = block.name;
260
+ if (block.args) existing.args.push(block.args);
261
+ pendingServerFunctionChunks.set(block.id, existing);
262
+ }
263
+ } else if (block.type === "server_tool_call_result") {
264
+ yield* flushMessage();
265
+ yield convertFunctionCallOutput(block);
266
+ } else if (block.type === "audio") {} else if (block.type === "file") {
267
+ const fileItem = resolveFileItem(block);
268
+ if (fileItem) pushMessageContent([fileItem]);
269
+ } else if (block.type === "image") {
270
+ const imageItem = resolveImageItem(block);
271
+ if (imageItem) pushMessageContent([imageItem]);
272
+ } else if (block.type === "video") {
273
+ const videoItem = resolveFileItem(block);
274
+ if (videoItem) pushMessageContent([videoItem]);
275
+ } else if (block.type === "text-plain") {
276
+ if (block.text) pushMessageContent([{
277
+ type: "input_text",
278
+ text: block.text
279
+ }]);
280
+ } else if (block.type === "non_standard" && isResponsesMessage) {
281
+ yield* flushMessage();
282
+ yield block.value;
283
+ }
284
+ yield* flushMessage();
285
+ for (const [id, chunk] of pendingFunctionChunks) {
286
+ if (!id || functionCallIdsWithBlocks.has(id)) continue;
287
+ const args = chunk.args.join("");
288
+ if (!chunk.name && !args) continue;
289
+ yield {
290
+ type: "function_call",
291
+ call_id: id,
292
+ name: chunk.name ?? "",
293
+ arguments: args
294
+ };
295
+ }
296
+ for (const [id, chunk] of pendingServerFunctionChunks) {
297
+ if (!id || serverFunctionCallIdsWithBlocks.has(id)) continue;
298
+ const args = chunk.args.join("");
299
+ if (!chunk.name && !args) continue;
300
+ yield {
301
+ type: "function_call",
302
+ call_id: id,
303
+ name: chunk.name ?? "",
304
+ arguments: args
305
+ };
306
+ }
307
+ }
308
+ return Array.from(iterateItems());
309
+ }
310
+
311
+ //#endregion
312
+ export { _convertToCompletionsMessageFromV1, _convertToResponsesMessageFromV1 };
313
+ //# sourceMappingURL=standard.js.map