@langchain/openai 1.0.0-alpha.1 → 1.0.0-alpha.3
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/LICENSE +6 -6
- package/dist/chat_models.cjs +21 -11
- package/dist/chat_models.cjs.map +1 -1
- package/dist/chat_models.d.cts +10 -7
- package/dist/chat_models.d.cts.map +1 -1
- package/dist/chat_models.d.ts +9 -6
- package/dist/chat_models.d.ts.map +1 -1
- package/dist/chat_models.js +22 -12
- package/dist/chat_models.js.map +1 -1
- package/dist/embeddings.cjs +4 -0
- package/dist/embeddings.cjs.map +1 -1
- package/dist/embeddings.d.cts +8 -3
- package/dist/embeddings.d.cts.map +1 -1
- package/dist/embeddings.d.ts +8 -3
- package/dist/embeddings.d.ts.map +1 -1
- package/dist/embeddings.js +4 -0
- package/dist/embeddings.js.map +1 -1
- package/dist/llms.cjs.map +1 -1
- package/dist/llms.js.map +1 -1
- package/dist/tools/custom.cjs.map +1 -1
- package/dist/tools/custom.js.map +1 -1
- package/dist/tools/dalle.cjs.map +1 -1
- package/dist/tools/dalle.js.map +1 -1
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/message_inputs.cjs +1 -1
- package/dist/utils/message_inputs.cjs.map +1 -1
- package/dist/utils/message_inputs.d.cts.map +1 -1
- package/dist/utils/message_inputs.d.ts.map +1 -1
- package/dist/utils/message_inputs.js +1 -1
- package/dist/utils/message_inputs.js.map +1 -1
- package/dist/utils/misc.cjs +7 -2
- package/dist/utils/misc.cjs.map +1 -1
- package/dist/utils/misc.d.cts.map +1 -1
- package/dist/utils/misc.d.ts.map +1 -1
- package/dist/utils/misc.js +6 -1
- package/dist/utils/misc.js.map +1 -1
- package/dist/utils/output.cjs +37 -0
- package/dist/utils/output.cjs.map +1 -1
- package/dist/utils/output.js +36 -1
- package/dist/utils/output.js.map +1 -1
- package/dist/utils/prompts.cjs.map +1 -1
- package/dist/utils/prompts.d.cts.map +1 -1
- package/dist/utils/prompts.d.ts.map +1 -1
- package/dist/utils/prompts.js.map +1 -1
- package/dist/utils/standard.cjs +217 -15
- package/dist/utils/standard.cjs.map +1 -1
- package/dist/utils/standard.js +218 -16
- package/dist/utils/standard.js.map +1 -1
- package/dist/utils/tools.cjs.map +1 -1
- package/dist/utils/tools.d.cts.map +1 -1
- package/dist/utils/tools.d.ts.map +1 -1
- package/dist/utils/tools.js.map +1 -1
- package/package.json +30 -33
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompts.js","names":["formattedPrompt: BasePromptValue"],"sources":["../../src/utils/prompts.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"prompts.js","names":["formattedPrompt: BasePromptValue"],"sources":["../../src/utils/prompts.ts"],"sourcesContent":["import type { BasePromptValue } from \"@langchain/core/prompt_values\";\nimport type { OpenAI } from \"openai\";\n\nimport { _convertMessagesToOpenAIParams } from \"./message_inputs.js\";\n\n/**\n * Convert a formatted LangChain prompt (e.g. pulled from the hub) into\n * a format expected by OpenAI's JS SDK.\n *\n * Requires the \"@langchain/openai\" package to be installed in addition\n * to the OpenAI SDK.\n *\n * @example\n * ```ts\n * import { convertPromptToOpenAI } from \"langsmith/utils/hub/openai\";\n * import { pull } from \"langchain/hub\";\n *\n * import OpenAI from 'openai';\n *\n * const prompt = await pull(\"jacob/joke-generator\");\n * const formattedPrompt = await prompt.invoke({\n * topic: \"cats\",\n * });\n *\n * const { messages } = convertPromptToOpenAI(formattedPrompt);\n *\n * const openAIClient = new OpenAI();\n *\n * const openaiResponse = await openAIClient.chat.completions.create({\n * model: \"gpt-4o-mini\",\n * messages,\n * });\n * ```\n * @param formattedPrompt\n * @returns A partial OpenAI payload.\n */\nexport function convertPromptToOpenAI(formattedPrompt: BasePromptValue): {\n messages: OpenAI.Chat.ChatCompletionMessageParam[];\n} {\n const messages = formattedPrompt.toChatMessages();\n return {\n messages: _convertMessagesToOpenAIParams(\n messages\n ) as OpenAI.Chat.ChatCompletionMessageParam[],\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,sBAAsBA,iBAEpC;CACA,MAAM,WAAW,gBAAgB,gBAAgB;AACjD,QAAO,EACL,UAAU,+BACR,SACD,CACF;AACF"}
|
package/dist/utils/standard.cjs
CHANGED
|
@@ -57,7 +57,7 @@ function _convertToCompletionsMessageFromV1(message, model) {
|
|
|
57
57
|
role: "assistant",
|
|
58
58
|
content: message.contentBlocks.filter((block) => block.type === "text")
|
|
59
59
|
};
|
|
60
|
-
else if (role === "tool" &&
|
|
60
|
+
else if (role === "tool" && __langchain_core_messages.ToolMessage.isInstance(message)) return {
|
|
61
61
|
role: "tool",
|
|
62
62
|
tool_call_id: message.tool_call_id,
|
|
63
63
|
content: message.contentBlocks.filter((block) => block.type === "text")
|
|
@@ -83,28 +83,230 @@ function _convertToCompletionsMessageFromV1(message, model) {
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
function _convertToResponsesMessageFromV1(message) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
+
}
|
|
91
127
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
+
})) : [{
|
|
96
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",
|
|
97
201
|
text: block.reasoning
|
|
98
|
-
}]
|
|
202
|
+
}];
|
|
203
|
+
return reasoningItem;
|
|
99
204
|
};
|
|
100
|
-
|
|
205
|
+
const convertFunctionCall = (block) => ({
|
|
101
206
|
type: "function_call",
|
|
102
|
-
name: block.name,
|
|
207
|
+
name: block.name ?? "",
|
|
103
208
|
call_id: block.id ?? "",
|
|
104
|
-
arguments:
|
|
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
|
+
};
|
|
105
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
|
+
}
|
|
106
308
|
}
|
|
107
|
-
return Array.from(
|
|
309
|
+
return Array.from(iterateItems());
|
|
108
310
|
}
|
|
109
311
|
|
|
110
312
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standard.cjs","names":["block: ContentBlock.Standard","iife","format","message: AIMessage","model?: string","messageToOpenAIRole","isReasoningModel","blocks: ContentBlock.Standard[]"],"sources":["../../src/utils/standard.ts"],"sourcesContent":["import type { OpenAI as OpenAIClient } from \"openai\";\nimport {\n isToolMessage,\n type AIMessage,\n type ContentBlock,\n} from \"@langchain/core/messages\";\nimport { iife, isReasoningModel, messageToOpenAIRole } from \"./misc.js\";\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: AIMessage,\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\" && isToolMessage(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: AIMessage) {\n function* iterateContent(): Generator<OpenAIClient.Responses.ResponseInputItem> {\n for (const block of message.contentBlocks) {\n if (block.type === \"text\") {\n yield {\n type: \"message\" as const,\n role: \"assistant\",\n content: block.text,\n };\n } else if (block.type === \"reasoning\") {\n yield {\n id: block.id ?? \"\",\n type: \"reasoning\" as const,\n summary: [\n {\n type: \"summary_text\",\n text: block.reasoning,\n },\n ],\n };\n } else if (block.type === \"tool_call\") {\n yield {\n type: \"function_call\" as const,\n name: block.name,\n call_id: block.id ?? \"\",\n arguments: JSON.stringify(block.args),\n };\n }\n }\n }\n return Array.from(iterateContent());\n}\n"],"mappings":";;;;;AAaA,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,WAAW,MAAM,KAAK,UAAU,CACjC;EACF;AAEH,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N,SAAS,MAAM;EAChB;WACQ,MAAM,SAAS,aACxB,MAAM;GACJ,IAAI,MAAM,MAAM;GAChB,MAAM;GACN,SAAS,CACP;IACE,MAAM;IACN,MAAM,MAAM;GACb,CACF;EACF;WACQ,MAAM,SAAS,aACxB,MAAM;GACJ,MAAM;GACN,MAAM,MAAM;GACZ,SAAS,MAAM,MAAM;GACrB,WAAW,KAAK,UAAU,MAAM,KAAK;EACtC;CAGN;AACD,QAAO,MAAM,KAAK,gBAAgB,CAAC;AACpC"}
|
|
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,EAA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gBAAgB,MAAM;AACvC,OAAI,UACF,mBAAmB,CAAC,QAAS,EAAC;EAEjC,WAAU,MAAM,SAAS,SAAS;GACjC,MAAM,YAAY,iBAAiB,MAAM;AACzC,OAAI,WACF,mBAAmB,CAAC,SAAU,EAAC;EAElC,WAAU,MAAM,SAAS,SAAS;GACjC,MAAM,YAAY,gBAAgB,MAAM;AACxC,OAAI,WACF,mBAAmB,CAAC,SAAU,EAAC;EAElC,WAAU,MAAM,SAAS,cACxB;OAAI,MAAM,MACR,mBAAmB,CACjB;IACE,MAAM;IACN,MAAM,MAAM;GACb,CACF,EAAC;EACH,WACQ,MAAM,SAAS,kBAAkB,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"}
|
package/dist/utils/standard.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { iife, isReasoningModel, messageToOpenAIRole } from "./misc.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ToolMessage, isAIMessage } from "@langchain/core/messages";
|
|
3
3
|
|
|
4
4
|
//#region src/utils/standard.ts
|
|
5
5
|
function _convertToChatCompletionsData(block) {
|
|
@@ -56,7 +56,7 @@ function _convertToCompletionsMessageFromV1(message, model) {
|
|
|
56
56
|
role: "assistant",
|
|
57
57
|
content: message.contentBlocks.filter((block) => block.type === "text")
|
|
58
58
|
};
|
|
59
|
-
else if (role === "tool" &&
|
|
59
|
+
else if (role === "tool" && ToolMessage.isInstance(message)) return {
|
|
60
60
|
role: "tool",
|
|
61
61
|
tool_call_id: message.tool_call_id,
|
|
62
62
|
content: message.contentBlocks.filter((block) => block.type === "text")
|
|
@@ -82,28 +82,230 @@ function _convertToCompletionsMessageFromV1(message, model) {
|
|
|
82
82
|
};
|
|
83
83
|
}
|
|
84
84
|
function _convertToResponsesMessageFromV1(message) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
+
}
|
|
90
126
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
})) : [{
|
|
95
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",
|
|
96
200
|
text: block.reasoning
|
|
97
|
-
}]
|
|
201
|
+
}];
|
|
202
|
+
return reasoningItem;
|
|
98
203
|
};
|
|
99
|
-
|
|
204
|
+
const convertFunctionCall = (block) => ({
|
|
100
205
|
type: "function_call",
|
|
101
|
-
name: block.name,
|
|
206
|
+
name: block.name ?? "",
|
|
102
207
|
call_id: block.id ?? "",
|
|
103
|
-
arguments:
|
|
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
|
+
};
|
|
104
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
|
+
}
|
|
105
307
|
}
|
|
106
|
-
return Array.from(
|
|
308
|
+
return Array.from(iterateItems());
|
|
107
309
|
}
|
|
108
310
|
|
|
109
311
|
//#endregion
|