@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,139 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_misc = require('./misc.cjs');
3
+ const require_standard = require('./standard.cjs');
4
+ const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
5
+ const __langchain_core_output_parsers_openai_tools = require_rolldown_runtime.__toESM(require("@langchain/core/output_parsers/openai_tools"));
6
+
7
+ //#region src/utils/message_inputs.ts
8
+ const completionsApiContentBlockConverter = {
9
+ providerName: "ChatOpenAI",
10
+ fromStandardTextBlock(block) {
11
+ return {
12
+ type: "text",
13
+ text: block.text
14
+ };
15
+ },
16
+ fromStandardImageBlock(block) {
17
+ if (block.source_type === "url") return {
18
+ type: "image_url",
19
+ image_url: {
20
+ url: block.url,
21
+ ...block.metadata?.detail ? { detail: block.metadata.detail } : {}
22
+ }
23
+ };
24
+ if (block.source_type === "base64") {
25
+ const url = `data:${block.mime_type ?? ""};base64,${block.data}`;
26
+ return {
27
+ type: "image_url",
28
+ image_url: {
29
+ url,
30
+ ...block.metadata?.detail ? { detail: block.metadata.detail } : {}
31
+ }
32
+ };
33
+ }
34
+ throw new Error(`Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
35
+ },
36
+ fromStandardAudioBlock(block) {
37
+ if (block.source_type === "url") {
38
+ const data = (0, __langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
39
+ if (!data) throw new Error(`URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
40
+ const rawMimeType = data.mime_type || block.mime_type || "";
41
+ let mimeType;
42
+ try {
43
+ mimeType = (0, __langchain_core_messages.parseMimeType)(rawMimeType);
44
+ } catch {
45
+ throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
46
+ }
47
+ if (mimeType.type !== "audio" || mimeType.subtype !== "wav" && mimeType.subtype !== "mp3") throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
48
+ return {
49
+ type: "input_audio",
50
+ input_audio: {
51
+ format: mimeType.subtype,
52
+ data: data.data
53
+ }
54
+ };
55
+ }
56
+ if (block.source_type === "base64") {
57
+ let mimeType;
58
+ try {
59
+ mimeType = (0, __langchain_core_messages.parseMimeType)(block.mime_type ?? "");
60
+ } catch {
61
+ throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
62
+ }
63
+ if (mimeType.type !== "audio" || mimeType.subtype !== "wav" && mimeType.subtype !== "mp3") throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
64
+ return {
65
+ type: "input_audio",
66
+ input_audio: {
67
+ format: mimeType.subtype,
68
+ data: block.data
69
+ }
70
+ };
71
+ }
72
+ throw new Error(`Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
73
+ },
74
+ fromStandardFileBlock(block) {
75
+ if (block.source_type === "url") {
76
+ const data = (0, __langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
77
+ if (!data) throw new Error(`URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
78
+ return {
79
+ type: "file",
80
+ file: {
81
+ file_data: block.url,
82
+ ...block.metadata?.filename || block.metadata?.name ? { filename: block.metadata?.filename || block.metadata?.name } : {}
83
+ }
84
+ };
85
+ }
86
+ if (block.source_type === "base64") return {
87
+ type: "file",
88
+ file: {
89
+ file_data: `data:${block.mime_type ?? ""};base64,${block.data}`,
90
+ ...block.metadata?.filename || block.metadata?.name || block.metadata?.title ? { filename: block.metadata?.filename || block.metadata?.name || block.metadata?.title } : {}
91
+ }
92
+ };
93
+ if (block.source_type === "id") return {
94
+ type: "file",
95
+ file: { file_id: block.id }
96
+ };
97
+ throw new Error(`File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
98
+ }
99
+ };
100
+ function _convertMessagesToOpenAIParams(messages, model) {
101
+ return messages.flatMap((message) => {
102
+ if ("output_version" in message.response_metadata && message.response_metadata?.output_version === "v1") return require_standard._convertToCompletionsMessageFromV1(message);
103
+ let role = require_misc.messageToOpenAIRole(message);
104
+ if (role === "system" && require_misc.isReasoningModel(model)) role = "developer";
105
+ const content = typeof message.content === "string" ? message.content : message.content.map((m) => {
106
+ if ((0, __langchain_core_messages.isDataContentBlock)(m)) return (0, __langchain_core_messages.convertToProviderContentBlock)(m, completionsApiContentBlockConverter);
107
+ return m;
108
+ });
109
+ const completionParam = {
110
+ role,
111
+ content
112
+ };
113
+ if (message.name != null) completionParam.name = message.name;
114
+ if (message.additional_kwargs.function_call != null) {
115
+ completionParam.function_call = message.additional_kwargs.function_call;
116
+ completionParam.content = "";
117
+ }
118
+ if ((0, __langchain_core_messages.isAIMessage)(message) && !!message.tool_calls?.length) {
119
+ completionParam.tool_calls = message.tool_calls.map(__langchain_core_output_parsers_openai_tools.convertLangChainToolCallToOpenAI);
120
+ completionParam.content = "";
121
+ } else {
122
+ if (message.additional_kwargs.tool_calls != null) completionParam.tool_calls = message.additional_kwargs.tool_calls;
123
+ if (message.tool_call_id != null) completionParam.tool_call_id = message.tool_call_id;
124
+ }
125
+ if (message.additional_kwargs.audio && typeof message.additional_kwargs.audio === "object" && "id" in message.additional_kwargs.audio) {
126
+ const audioMessage = {
127
+ role: "assistant",
128
+ audio: { id: message.additional_kwargs.audio.id }
129
+ };
130
+ return [completionParam, audioMessage];
131
+ }
132
+ return completionParam;
133
+ });
134
+ }
135
+
136
+ //#endregion
137
+ exports._convertMessagesToOpenAIParams = _convertMessagesToOpenAIParams;
138
+ exports.completionsApiContentBlockConverter = completionsApiContentBlockConverter;
139
+ //# sourceMappingURL=message_inputs.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message_inputs.cjs","names":["completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>","mimeType: { type: string; subtype: string }","messages: BaseMessage[]","model?: string","_convertToCompletionsMessageFromV1","messageToOpenAIRole","isReasoningModel","completionParam: Record<string, any>","convertLangChainToolCallToOpenAI"],"sources":["../../src/utils/message_inputs.ts"],"sourcesContent":["import {\n BaseMessage,\n convertToProviderContentBlock,\n isAIMessage,\n isDataContentBlock,\n parseBase64DataUrl,\n parseMimeType,\n StandardContentBlockConverter,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { convertLangChainToolCallToOpenAI } from \"@langchain/core/output_parsers/openai_tools\";\nimport type { OpenAI as OpenAIClient } from \"openai\";\nimport type {\n ChatCompletionContentPartText,\n ChatCompletionContentPartImage,\n ChatCompletionContentPartInputAudio,\n ChatCompletionContentPart,\n} from \"openai/resources/chat/completions\";\nimport { _convertToCompletionsMessageFromV1 } from \"./standard.js\";\nimport { isReasoningModel, messageToOpenAIRole } from \"./misc.js\";\n\nexport const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}> = {\n providerName: \"ChatOpenAI\",\n\n fromStandardTextBlock(block): ChatCompletionContentPartText {\n return { type: \"text\", text: block.text };\n },\n\n fromStandardImageBlock(block): ChatCompletionContentPartImage {\n if (block.source_type === \"url\") {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n const url = `data:${block.mime_type ?? \"\"};base64,${block.data}`;\n return {\n type: \"image_url\",\n image_url: {\n url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n throw new Error(\n `Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardAudioBlock(block): ChatCompletionContentPartInputAudio {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (!data) {\n throw new Error(\n `URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n const rawMimeType = data.mime_type || block.mime_type || \"\";\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(rawMimeType);\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: data.data,\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(block.mime_type ?? \"\");\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: block.data,\n },\n };\n }\n\n throw new Error(\n `Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardFileBlock(block): ChatCompletionContentPart.File {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (!data) {\n throw new Error(\n `URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n return {\n type: \"file\",\n file: {\n file_data: block.url, // formatted as base64 data URL\n ...(block.metadata?.filename || block.metadata?.name\n ? {\n filename: (block.metadata?.filename ||\n block.metadata?.name) as string,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n return {\n type: \"file\",\n file: {\n file_data: `data:${block.mime_type ?? \"\"};base64,${block.data}`,\n ...(block.metadata?.filename ||\n block.metadata?.name ||\n block.metadata?.title\n ? {\n filename: (block.metadata?.filename ||\n block.metadata?.name ||\n block.metadata?.title) as string,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"id\") {\n return {\n type: \"file\",\n file: {\n file_id: block.id,\n },\n };\n }\n\n throw new Error(\n `File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n};\n\nexport function _convertMessagesToOpenAIParams(\n messages: BaseMessage[],\n model?: string\n): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[] {\n return messages.flatMap((message) => {\n if (\n \"output_version\" in message.response_metadata &&\n message.response_metadata?.output_version === \"v1\"\n ) {\n return _convertToCompletionsMessageFromV1(message);\n }\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n\n const content =\n typeof message.content === \"string\"\n ? message.content\n : message.content.map((m) => {\n if (isDataContentBlock(m)) {\n return convertToProviderContentBlock(\n m,\n completionsApiContentBlockConverter\n );\n }\n return m;\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const completionParam: Record<string, any> = {\n role,\n content,\n };\n if (message.name != null) {\n completionParam.name = message.name;\n }\n if (message.additional_kwargs.function_call != null) {\n completionParam.function_call = message.additional_kwargs.function_call;\n completionParam.content = \"\";\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n completionParam.tool_calls = message.tool_calls.map(\n convertLangChainToolCallToOpenAI\n );\n completionParam.content = \"\";\n } else {\n if (message.additional_kwargs.tool_calls != null) {\n completionParam.tool_calls = message.additional_kwargs.tool_calls;\n }\n if ((message as ToolMessage).tool_call_id != null) {\n completionParam.tool_call_id = (message as ToolMessage).tool_call_id;\n }\n }\n\n if (\n message.additional_kwargs.audio &&\n typeof message.additional_kwargs.audio === \"object\" &&\n \"id\" in message.additional_kwargs.audio\n ) {\n const audioMessage = {\n role: \"assistant\",\n audio: {\n id: message.additional_kwargs.audio.id,\n },\n };\n return [\n completionParam,\n audioMessage,\n ] as OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];\n }\n\n return completionParam as OpenAIClient.Chat.Completions.ChatCompletionMessageParam;\n });\n}\n"],"mappings":";;;;;;;AAqBA,MAAaA,sCAKR;CACH,cAAc;CAEd,sBAAsB,OAAsC;AAC1D,SAAO;GAAE,MAAM;GAAQ,MAAM,MAAM;EAAM;CAC1C;CAED,uBAAuB,OAAuC;AAC5D,MAAI,MAAM,gBAAgB,MACxB,QAAO;GACL,MAAM;GACN,WAAW;IACT,KAAK,MAAM;IACX,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,OAAmC,IAC5D,CAAE;GACP;EACF;AAGH,MAAI,MAAM,gBAAgB,UAAU;GAClC,MAAM,MAAM,CAAC,KAAK,EAAE,MAAM,aAAa,GAAG,QAAQ,EAAE,MAAM,MAAM;AAChE,UAAO;IACL,MAAM;IACN,WAAW;KACT;KACA,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,OAAmC,IAC5D,CAAE;IACP;GACF;EACF;AAED,QAAM,IAAI,MACR,CAAC,sCAAsC,EAAE,MAAM,YAAY,iCAAiC,CAAC;CAEhG;CAED,uBAAuB,OAA4C;AACjE,MAAI,MAAM,gBAAgB,OAAO;GAC/B,MAAM,yDAA0B,EAAE,SAAS,MAAM,IAAK,EAAC;AACvD,OAAI,CAAC,KACH,OAAM,IAAI,MACR,CAAC,kCAAkC,EAAE,MAAM,YAAY,+CAA+C,CAAC;GAI3G,MAAM,cAAc,KAAK,aAAa,MAAM,aAAa;GACzD,IAAIC;AAEJ,OAAI;IACF,wDAAyB,YAAY;GACtC,QAAO;AACN,UAAM,IAAI,MACR,CAAC,8BAA8B,EAAE,MAAM,YAAY,8CAA8C,CAAC;GAErG;AAED,OACE,SAAS,SAAS,WACjB,SAAS,YAAY,SAAS,SAAS,YAAY,MAEpD,OAAM,IAAI,MACR,CAAC,8BAA8B,EAAE,MAAM,YAAY,8CAA8C,CAAC;AAItG,UAAO;IACL,MAAM;IACN,aAAa;KACX,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1,9 @@
1
+ import { OpenAI as OpenAI$1 } from "openai";
2
+ import { BaseMessage } from "@langchain/core/messages";
3
+
4
+ //#region src/utils/message_inputs.d.ts
5
+
6
+ declare function _convertMessagesToOpenAIParams(messages: BaseMessage[], model?: string): OpenAI$1.Chat.Completions.ChatCompletionMessageParam[];
7
+ //#endregion
8
+ export { _convertMessagesToOpenAIParams };
9
+ //# sourceMappingURL=message_inputs.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message_inputs.d.cts","names":["BaseMessage","StandardContentBlockConverter","OpenAI","OpenAIClient","ChatCompletionContentPartText","ChatCompletionContentPartImage","ChatCompletionContentPartInputAudio","ChatCompletionContentPart","completionsApiContentBlockConverter","File","_convertMessagesToOpenAIParams","Chat","Completions","ChatCompletionMessageParam"],"sources":["../../src/utils/message_inputs.d.ts"],"sourcesContent":["import { BaseMessage, StandardContentBlockConverter } from \"@langchain/core/messages\";\nimport type { OpenAI as OpenAIClient } from \"openai\";\nimport type { ChatCompletionContentPartText, ChatCompletionContentPartImage, ChatCompletionContentPartInputAudio, ChatCompletionContentPart } from \"openai/resources/chat/completions\";\nexport declare const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>;\nexport declare function _convertMessagesToOpenAIParams(messages: BaseMessage[], model?: string): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];\n"],"mappings":";;;;;iBASwBU,8BAAAA,WAAyCV,gCAAgCG,QAAAA,CAAaQ,IAAAA,CAAKC,WAAAA,CAAYC"}
@@ -0,0 +1,9 @@
1
+ import { OpenAI as OpenAI$1 } from "openai";
2
+ import { BaseMessage } from "@langchain/core/messages";
3
+
4
+ //#region src/utils/message_inputs.d.ts
5
+
6
+ declare function _convertMessagesToOpenAIParams(messages: BaseMessage[], model?: string): OpenAI$1.Chat.Completions.ChatCompletionMessageParam[];
7
+ //#endregion
8
+ export { _convertMessagesToOpenAIParams };
9
+ //# sourceMappingURL=message_inputs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message_inputs.d.ts","names":["BaseMessage","StandardContentBlockConverter","OpenAI","OpenAIClient","ChatCompletionContentPartText","ChatCompletionContentPartImage","ChatCompletionContentPartInputAudio","ChatCompletionContentPart","completionsApiContentBlockConverter","File","_convertMessagesToOpenAIParams","Chat","Completions","ChatCompletionMessageParam"],"sources":["../../src/utils/message_inputs.d.ts"],"sourcesContent":["import { BaseMessage, StandardContentBlockConverter } from \"@langchain/core/messages\";\nimport type { OpenAI as OpenAIClient } from \"openai\";\nimport type { ChatCompletionContentPartText, ChatCompletionContentPartImage, ChatCompletionContentPartInputAudio, ChatCompletionContentPart } from \"openai/resources/chat/completions\";\nexport declare const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>;\nexport declare function _convertMessagesToOpenAIParams(messages: BaseMessage[], model?: string): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];\n"],"mappings":";;;;;iBASwBU,8BAAAA,WAAyCV,gCAAgCG,QAAAA,CAAaQ,IAAAA,CAAKC,WAAAA,CAAYC"}
@@ -0,0 +1,137 @@
1
+ import { isReasoningModel, messageToOpenAIRole } from "./misc.js";
2
+ import { _convertToCompletionsMessageFromV1 } from "./standard.js";
3
+ import { convertToProviderContentBlock, isAIMessage, isDataContentBlock, parseBase64DataUrl, parseMimeType } from "@langchain/core/messages";
4
+ import { convertLangChainToolCallToOpenAI } from "@langchain/core/output_parsers/openai_tools";
5
+
6
+ //#region src/utils/message_inputs.ts
7
+ const completionsApiContentBlockConverter = {
8
+ providerName: "ChatOpenAI",
9
+ fromStandardTextBlock(block) {
10
+ return {
11
+ type: "text",
12
+ text: block.text
13
+ };
14
+ },
15
+ fromStandardImageBlock(block) {
16
+ if (block.source_type === "url") return {
17
+ type: "image_url",
18
+ image_url: {
19
+ url: block.url,
20
+ ...block.metadata?.detail ? { detail: block.metadata.detail } : {}
21
+ }
22
+ };
23
+ if (block.source_type === "base64") {
24
+ const url = `data:${block.mime_type ?? ""};base64,${block.data}`;
25
+ return {
26
+ type: "image_url",
27
+ image_url: {
28
+ url,
29
+ ...block.metadata?.detail ? { detail: block.metadata.detail } : {}
30
+ }
31
+ };
32
+ }
33
+ throw new Error(`Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
34
+ },
35
+ fromStandardAudioBlock(block) {
36
+ if (block.source_type === "url") {
37
+ const data = parseBase64DataUrl({ dataUrl: block.url });
38
+ if (!data) throw new Error(`URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
39
+ const rawMimeType = data.mime_type || block.mime_type || "";
40
+ let mimeType;
41
+ try {
42
+ mimeType = parseMimeType(rawMimeType);
43
+ } catch {
44
+ throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
45
+ }
46
+ if (mimeType.type !== "audio" || mimeType.subtype !== "wav" && mimeType.subtype !== "mp3") throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
47
+ return {
48
+ type: "input_audio",
49
+ input_audio: {
50
+ format: mimeType.subtype,
51
+ data: data.data
52
+ }
53
+ };
54
+ }
55
+ if (block.source_type === "base64") {
56
+ let mimeType;
57
+ try {
58
+ mimeType = parseMimeType(block.mime_type ?? "");
59
+ } catch {
60
+ throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
61
+ }
62
+ if (mimeType.type !== "audio" || mimeType.subtype !== "wav" && mimeType.subtype !== "mp3") throw new Error(`Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`);
63
+ return {
64
+ type: "input_audio",
65
+ input_audio: {
66
+ format: mimeType.subtype,
67
+ data: block.data
68
+ }
69
+ };
70
+ }
71
+ throw new Error(`Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
72
+ },
73
+ fromStandardFileBlock(block) {
74
+ if (block.source_type === "url") {
75
+ const data = parseBase64DataUrl({ dataUrl: block.url });
76
+ if (!data) throw new Error(`URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`);
77
+ return {
78
+ type: "file",
79
+ file: {
80
+ file_data: block.url,
81
+ ...block.metadata?.filename || block.metadata?.name ? { filename: block.metadata?.filename || block.metadata?.name } : {}
82
+ }
83
+ };
84
+ }
85
+ if (block.source_type === "base64") return {
86
+ type: "file",
87
+ file: {
88
+ file_data: `data:${block.mime_type ?? ""};base64,${block.data}`,
89
+ ...block.metadata?.filename || block.metadata?.name || block.metadata?.title ? { filename: block.metadata?.filename || block.metadata?.name || block.metadata?.title } : {}
90
+ }
91
+ };
92
+ if (block.source_type === "id") return {
93
+ type: "file",
94
+ file: { file_id: block.id }
95
+ };
96
+ throw new Error(`File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`);
97
+ }
98
+ };
99
+ function _convertMessagesToOpenAIParams(messages, model) {
100
+ return messages.flatMap((message) => {
101
+ if ("output_version" in message.response_metadata && message.response_metadata?.output_version === "v1") return _convertToCompletionsMessageFromV1(message);
102
+ let role = messageToOpenAIRole(message);
103
+ if (role === "system" && isReasoningModel(model)) role = "developer";
104
+ const content = typeof message.content === "string" ? message.content : message.content.map((m) => {
105
+ if (isDataContentBlock(m)) return convertToProviderContentBlock(m, completionsApiContentBlockConverter);
106
+ return m;
107
+ });
108
+ const completionParam = {
109
+ role,
110
+ content
111
+ };
112
+ if (message.name != null) completionParam.name = message.name;
113
+ if (message.additional_kwargs.function_call != null) {
114
+ completionParam.function_call = message.additional_kwargs.function_call;
115
+ completionParam.content = "";
116
+ }
117
+ if (isAIMessage(message) && !!message.tool_calls?.length) {
118
+ completionParam.tool_calls = message.tool_calls.map(convertLangChainToolCallToOpenAI);
119
+ completionParam.content = "";
120
+ } else {
121
+ if (message.additional_kwargs.tool_calls != null) completionParam.tool_calls = message.additional_kwargs.tool_calls;
122
+ if (message.tool_call_id != null) completionParam.tool_call_id = message.tool_call_id;
123
+ }
124
+ if (message.additional_kwargs.audio && typeof message.additional_kwargs.audio === "object" && "id" in message.additional_kwargs.audio) {
125
+ const audioMessage = {
126
+ role: "assistant",
127
+ audio: { id: message.additional_kwargs.audio.id }
128
+ };
129
+ return [completionParam, audioMessage];
130
+ }
131
+ return completionParam;
132
+ });
133
+ }
134
+
135
+ //#endregion
136
+ export { _convertMessagesToOpenAIParams, completionsApiContentBlockConverter };
137
+ //# sourceMappingURL=message_inputs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message_inputs.js","names":["completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}>","mimeType: { type: string; subtype: string }","messages: BaseMessage[]","model?: string","completionParam: Record<string, any>"],"sources":["../../src/utils/message_inputs.ts"],"sourcesContent":["import {\n BaseMessage,\n convertToProviderContentBlock,\n isAIMessage,\n isDataContentBlock,\n parseBase64DataUrl,\n parseMimeType,\n StandardContentBlockConverter,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { convertLangChainToolCallToOpenAI } from \"@langchain/core/output_parsers/openai_tools\";\nimport type { OpenAI as OpenAIClient } from \"openai\";\nimport type {\n ChatCompletionContentPartText,\n ChatCompletionContentPartImage,\n ChatCompletionContentPartInputAudio,\n ChatCompletionContentPart,\n} from \"openai/resources/chat/completions\";\nimport { _convertToCompletionsMessageFromV1 } from \"./standard.js\";\nimport { isReasoningModel, messageToOpenAIRole } from \"./misc.js\";\n\nexport const completionsApiContentBlockConverter: StandardContentBlockConverter<{\n text: ChatCompletionContentPartText;\n image: ChatCompletionContentPartImage;\n audio: ChatCompletionContentPartInputAudio;\n file: ChatCompletionContentPart.File;\n}> = {\n providerName: \"ChatOpenAI\",\n\n fromStandardTextBlock(block): ChatCompletionContentPartText {\n return { type: \"text\", text: block.text };\n },\n\n fromStandardImageBlock(block): ChatCompletionContentPartImage {\n if (block.source_type === \"url\") {\n return {\n type: \"image_url\",\n image_url: {\n url: block.url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n const url = `data:${block.mime_type ?? \"\"};base64,${block.data}`;\n return {\n type: \"image_url\",\n image_url: {\n url,\n ...(block.metadata?.detail\n ? { detail: block.metadata.detail as \"auto\" | \"low\" | \"high\" }\n : {}),\n },\n };\n }\n\n throw new Error(\n `Image content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardAudioBlock(block): ChatCompletionContentPartInputAudio {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (!data) {\n throw new Error(\n `URL audio blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n const rawMimeType = data.mime_type || block.mime_type || \"\";\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(rawMimeType);\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: data.data,\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n let mimeType: { type: string; subtype: string };\n\n try {\n mimeType = parseMimeType(block.mime_type ?? \"\");\n } catch {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n if (\n mimeType.type !== \"audio\" ||\n (mimeType.subtype !== \"wav\" && mimeType.subtype !== \"mp3\")\n ) {\n throw new Error(\n `Audio blocks with source_type ${block.source_type} must have mime type of audio/wav or audio/mp3`\n );\n }\n\n return {\n type: \"input_audio\",\n input_audio: {\n format: mimeType.subtype,\n data: block.data,\n },\n };\n }\n\n throw new Error(\n `Audio content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n\n fromStandardFileBlock(block): ChatCompletionContentPart.File {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (!data) {\n throw new Error(\n `URL file blocks with source_type ${block.source_type} must be formatted as a data URL for ChatOpenAI`\n );\n }\n\n return {\n type: \"file\",\n file: {\n file_data: block.url, // formatted as base64 data URL\n ...(block.metadata?.filename || block.metadata?.name\n ? {\n filename: (block.metadata?.filename ||\n block.metadata?.name) as string,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"base64\") {\n return {\n type: \"file\",\n file: {\n file_data: `data:${block.mime_type ?? \"\"};base64,${block.data}`,\n ...(block.metadata?.filename ||\n block.metadata?.name ||\n block.metadata?.title\n ? {\n filename: (block.metadata?.filename ||\n block.metadata?.name ||\n block.metadata?.title) as string,\n }\n : {}),\n },\n };\n }\n\n if (block.source_type === \"id\") {\n return {\n type: \"file\",\n file: {\n file_id: block.id,\n },\n };\n }\n\n throw new Error(\n `File content blocks with source_type ${block.source_type} are not supported for ChatOpenAI`\n );\n },\n};\n\nexport function _convertMessagesToOpenAIParams(\n messages: BaseMessage[],\n model?: string\n): OpenAIClient.Chat.Completions.ChatCompletionMessageParam[] {\n return messages.flatMap((message) => {\n if (\n \"output_version\" in message.response_metadata &&\n message.response_metadata?.output_version === \"v1\"\n ) {\n return _convertToCompletionsMessageFromV1(message);\n }\n let role = messageToOpenAIRole(message);\n if (role === \"system\" && isReasoningModel(model)) {\n role = \"developer\";\n }\n\n const content =\n typeof message.content === \"string\"\n ? message.content\n : message.content.map((m) => {\n if (isDataContentBlock(m)) {\n return convertToProviderContentBlock(\n m,\n completionsApiContentBlockConverter\n );\n }\n return m;\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const completionParam: Record<string, any> = {\n role,\n content,\n };\n if (message.name != null) {\n completionParam.name = message.name;\n }\n if (message.additional_kwargs.function_call != null) {\n completionParam.function_call = message.additional_kwargs.function_call;\n completionParam.content = \"\";\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n completionParam.tool_calls = message.tool_calls.map(\n convertLangChainToolCallToOpenAI\n );\n completionParam.content = \"\";\n } else {\n if (message.additional_kwargs.tool_calls != null) {\n completionParam.tool_calls = message.additional_kwargs.tool_calls;\n }\n if ((message as ToolMessage).tool_call_id != null) {\n completionParam.tool_call_id = (message as ToolMessage).tool_call_id;\n }\n }\n\n if (\n message.additional_kwargs.audio &&\n typeof message.additional_kwargs.audio === \"object\" &&\n \"id\" in message.additional_kwargs.audio\n ) {\n const audioMessage = {\n role: \"assistant\",\n audio: {\n id: message.additional_kwargs.audio.id,\n },\n };\n return [\n completionParam,\n audioMessage,\n ] as OpenAIClient.Chat.Completions.ChatCompletionMessageParam[];\n }\n\n return completionParam as OpenAIClient.Chat.Completions.ChatCompletionMessageParam;\n });\n}\n"],"mappings":";;;;;;AAqBA,MAAaA,sCAKR;CACH,cAAc;CAEd,sBAAsB,OAAsC;AAC1D,SAAO;GAAE,MAAM;GAAQ,MAAM,MAAM;EAAM;CAC1C;CAED,uBAAuB,OAAuC;AAC5D,MAAI,MAAM,gBAAgB,MACxB,QAAO;GACL,MAAM;GACN,WAAW;IACT,KAAK,MAAM;IACX,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,OAAmC,IAC5D,CAAE;GACP;EACF;AAGH,MAAI,MAAM,gBAAgB,UAAU;GAClC,MAAM,MAAM,CAAC,KAAK,EAAE,MAAM,aAAa,GAAG,QAAQ,EAAE,MAAM,MAAM;AAChE,UAAO;IACL,MAAM;IACN,WAAW;KACT;KACA,GAAI,MAAM,UAAU,SAChB,EAAE,QAAQ,MAAM,SAAS,OAAmC,IAC5D,CAAE;IACP;GACF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QAAQ;AAEjC,MAAI,QAAQ,kBAAkB,iBAAiB,MAAM;GACnD,gBAAgB,gBAAgB,QAAQ,kBAAkB;GAC1D,gBAAgB,UAAU;EAC3B;AACD,MAAI,YAAY,QAAQ,IAAI,CAAC,CAAC,QAAQ,YAAY,QAAQ;GACxD,gBAAgB,aAAa,QAAQ,WAAW,IAC9C,iCACD;GACD,gBAAgB,UAAU;EAC3B,OAAM;AACL,OAAI,QAAQ,kBAAkB,cAAc,MAC1C,gBAAgB,aAAa,QAAQ,kBAAkB;AAEzD,OAAK,QAAwB,gBAAgB,MAC3C,gBAAgB,eAAgB,QAAwB;EAE3D;AAED,MACE,QAAQ,kBAAkB,SAC1B,OAAO,QAAQ,kBAAkB,UAAU,YAC3C,QAAQ,QAAQ,kBAAkB,OAClC;GACA,MAAM,eAAe;IACnB,MAAM;IACN,OAAO,EACL,IAAI,QAAQ,kBAAkB,MAAM,GACrC;GACF;AACD,UAAO,CACL,iBACA,YACD;EACF;AAED,SAAO;CACR,EAAC;AACH"}
@@ -0,0 +1,32 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
3
+
4
+ //#region src/utils/misc.ts
5
+ const iife = (fn) => fn();
6
+ function isReasoningModel(model) {
7
+ return model && /^o\d/.test(model);
8
+ }
9
+ function extractGenericMessageCustomRole(message) {
10
+ if (message.role !== "system" && message.role !== "developer" && message.role !== "assistant" && message.role !== "user" && message.role !== "function" && message.role !== "tool") console.warn(`Unknown message role: ${message.role}`);
11
+ return message.role;
12
+ }
13
+ function messageToOpenAIRole(message) {
14
+ const type = message._getType();
15
+ switch (type) {
16
+ case "system": return "system";
17
+ case "ai": return "assistant";
18
+ case "human": return "user";
19
+ case "function": return "function";
20
+ case "tool": return "tool";
21
+ case "generic":
22
+ if (!__langchain_core_messages.ChatMessage.isInstance(message)) throw new Error("Invalid generic chat message");
23
+ return extractGenericMessageCustomRole(message);
24
+ default: throw new Error(`Unknown message type: ${type}`);
25
+ }
26
+ }
27
+
28
+ //#endregion
29
+ exports.iife = iife;
30
+ exports.isReasoningModel = isReasoningModel;
31
+ exports.messageToOpenAIRole = messageToOpenAIRole;
32
+ //# sourceMappingURL=misc.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.cjs","names":["fn: () => T","model?: string","message: ChatMessage","message: BaseMessage","ChatMessage"],"sources":["../../src/utils/misc.ts"],"sourcesContent":["import type { OpenAI as OpenAIClient } from \"openai\";\nimport { BaseMessage, ChatMessage } from \"@langchain/core/messages\";\n\nexport const iife = <T>(fn: () => T) => fn();\n\nexport function isReasoningModel(model?: string) {\n return model && /^o\\d/.test(model);\n}\n\nexport function extractGenericMessageCustomRole(message: ChatMessage) {\n if (\n message.role !== \"system\" &&\n message.role !== \"developer\" &&\n message.role !== \"assistant\" &&\n message.role !== \"user\" &&\n message.role !== \"function\" &&\n message.role !== \"tool\"\n ) {\n console.warn(`Unknown message role: ${message.role}`);\n }\n\n return message.role as OpenAIClient.ChatCompletionRole;\n}\n\nexport function messageToOpenAIRole(\n message: BaseMessage\n): OpenAIClient.ChatCompletionRole {\n const type = message._getType();\n switch (type) {\n case \"system\":\n return \"system\";\n case \"ai\":\n return \"assistant\";\n case \"human\":\n return \"user\";\n case \"function\":\n return \"function\";\n case \"tool\":\n return \"tool\";\n case \"generic\": {\n if (!ChatMessage.isInstance(message))\n throw new Error(\"Invalid generic chat message\");\n return extractGenericMessageCustomRole(message);\n }\n default:\n throw new Error(`Unknown message type: ${type}`);\n }\n}\n"],"mappings":";;;;AAGA,MAAa,OAAO,CAAIA,OAAgB,IAAI;AAE5C,SAAgB,iBAAiBC,OAAgB;AAC/C,QAAO,SAAS,OAAO,KAAK,MAAM;AACnC;AAED,SAAgB,gCAAgCC,SAAsB;AACpE,KACE,QAAQ,SAAS,YACjB,QAAQ,SAAS,eACjB,QAAQ,SAAS,eACjB,QAAQ,SAAS,UACjB,QAAQ,SAAS,cACjB,QAAQ,SAAS,QAEjB,QAAQ,KAAK,CAAC,sBAAsB,EAAE,QAAQ,MAAM,CAAC;AAGvD,QAAO,QAAQ;AAChB;AAED,SAAgB,oBACdC,SACiC;CACjC,MAAM,OAAO,QAAQ,UAAU;AAC/B,SAAQ,MAAR;EACE,KAAK,SACH,QAAO;EACT,KAAK,KACH,QAAO;EACT,KAAK,QACH,QAAO;EACT,KAAK,WACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK;AACH,OAAI,CAACC,sCAAY,WAAW,QAAQ,CAClC,OAAM,IAAI,MAAM;AAClB,UAAO,gCAAgC,QAAQ;EAEjD,QACE,OAAM,IAAI,MAAM,CAAC,sBAAsB,EAAE,MAAM;CAClD;AACF"}
@@ -0,0 +1,9 @@
1
+ import { OpenAI as OpenAI$1 } from "openai";
2
+ import { BaseMessage } from "@langchain/core/messages";
3
+
4
+ //#region src/utils/misc.d.ts
5
+
6
+ declare function messageToOpenAIRole(message: BaseMessage): OpenAI$1.ChatCompletionRole;
7
+ //#endregion
8
+ export { messageToOpenAIRole };
9
+ //# sourceMappingURL=misc.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.d.cts","names":["OpenAI","OpenAIClient","BaseMessage","ChatMessage","iife","T","isReasoningModel","extractGenericMessageCustomRole","ChatCompletionRole","messageToOpenAIRole"],"sources":["../../src/utils/misc.d.ts"],"sourcesContent":["import type { OpenAI as OpenAIClient } from \"openai\";\nimport { BaseMessage, ChatMessage } from \"@langchain/core/messages\";\nexport declare const iife: <T>(fn: () => T) => T;\nexport declare function isReasoningModel(model?: string): \"\" | boolean | undefined;\nexport declare function extractGenericMessageCustomRole(message: ChatMessage): OpenAIClient.ChatCompletionRole;\nexport declare function messageToOpenAIRole(message: BaseMessage): OpenAIClient.ChatCompletionRole;\n"],"mappings":";;;;;AAK2C,iBAAnBS,mBAAAA,CAAmB,OAAA,EAAUP,WAAV,CAAA,EAAwBD,QAAAA,CAAaO,kBAArC"}
@@ -0,0 +1,9 @@
1
+ import { OpenAI as OpenAI$1 } from "openai";
2
+ import { BaseMessage, ChatMessage } from "@langchain/core/messages";
3
+
4
+ //#region src/utils/misc.d.ts
5
+
6
+ declare function messageToOpenAIRole(message: BaseMessage): OpenAI$1.ChatCompletionRole;
7
+ //#endregion
8
+ export { messageToOpenAIRole };
9
+ //# sourceMappingURL=misc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.d.ts","names":["OpenAI","OpenAIClient","BaseMessage","ChatMessage","iife","T","isReasoningModel","extractGenericMessageCustomRole","ChatCompletionRole","messageToOpenAIRole"],"sources":["../../src/utils/misc.d.ts"],"sourcesContent":["import type { OpenAI as OpenAIClient } from \"openai\";\nimport { BaseMessage, ChatMessage } from \"@langchain/core/messages\";\nexport declare const iife: <T>(fn: () => T) => T;\nexport declare function isReasoningModel(model?: string): \"\" | boolean | undefined;\nexport declare function extractGenericMessageCustomRole(message: ChatMessage): OpenAIClient.ChatCompletionRole;\nexport declare function messageToOpenAIRole(message: BaseMessage): OpenAIClient.ChatCompletionRole;\n"],"mappings":";;;;;AAK2C,iBAAnBS,mBAAAA,CAAmB,OAAA,EAAUP,WAAV,CAAA,EAAwBD,QAAAA,CAAaO,kBAArC"}
@@ -0,0 +1,29 @@
1
+ import { ChatMessage } from "@langchain/core/messages";
2
+
3
+ //#region src/utils/misc.ts
4
+ const iife = (fn) => fn();
5
+ function isReasoningModel(model) {
6
+ return model && /^o\d/.test(model);
7
+ }
8
+ function extractGenericMessageCustomRole(message) {
9
+ if (message.role !== "system" && message.role !== "developer" && message.role !== "assistant" && message.role !== "user" && message.role !== "function" && message.role !== "tool") console.warn(`Unknown message role: ${message.role}`);
10
+ return message.role;
11
+ }
12
+ function messageToOpenAIRole(message) {
13
+ const type = message._getType();
14
+ switch (type) {
15
+ case "system": return "system";
16
+ case "ai": return "assistant";
17
+ case "human": return "user";
18
+ case "function": return "function";
19
+ case "tool": return "tool";
20
+ case "generic":
21
+ if (!ChatMessage.isInstance(message)) throw new Error("Invalid generic chat message");
22
+ return extractGenericMessageCustomRole(message);
23
+ default: throw new Error(`Unknown message type: ${type}`);
24
+ }
25
+ }
26
+
27
+ //#endregion
28
+ export { iife, isReasoningModel, messageToOpenAIRole };
29
+ //# sourceMappingURL=misc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"misc.js","names":["fn: () => T","model?: string","message: ChatMessage","message: BaseMessage"],"sources":["../../src/utils/misc.ts"],"sourcesContent":["import type { OpenAI as OpenAIClient } from \"openai\";\nimport { BaseMessage, ChatMessage } from \"@langchain/core/messages\";\n\nexport const iife = <T>(fn: () => T) => fn();\n\nexport function isReasoningModel(model?: string) {\n return model && /^o\\d/.test(model);\n}\n\nexport function extractGenericMessageCustomRole(message: ChatMessage) {\n if (\n message.role !== \"system\" &&\n message.role !== \"developer\" &&\n message.role !== \"assistant\" &&\n message.role !== \"user\" &&\n message.role !== \"function\" &&\n message.role !== \"tool\"\n ) {\n console.warn(`Unknown message role: ${message.role}`);\n }\n\n return message.role as OpenAIClient.ChatCompletionRole;\n}\n\nexport function messageToOpenAIRole(\n message: BaseMessage\n): OpenAIClient.ChatCompletionRole {\n const type = message._getType();\n switch (type) {\n case \"system\":\n return \"system\";\n case \"ai\":\n return \"assistant\";\n case \"human\":\n return \"user\";\n case \"function\":\n return \"function\";\n case \"tool\":\n return \"tool\";\n case \"generic\": {\n if (!ChatMessage.isInstance(message))\n throw new Error(\"Invalid generic chat message\");\n return extractGenericMessageCustomRole(message);\n }\n default:\n throw new Error(`Unknown message type: ${type}`);\n }\n}\n"],"mappings":";;;AAGA,MAAa,OAAO,CAAIA,OAAgB,IAAI;AAE5C,SAAgB,iBAAiBC,OAAgB;AAC/C,QAAO,SAAS,OAAO,KAAK,MAAM;AACnC;AAED,SAAgB,gCAAgCC,SAAsB;AACpE,KACE,QAAQ,SAAS,YACjB,QAAQ,SAAS,eACjB,QAAQ,SAAS,eACjB,QAAQ,SAAS,UACjB,QAAQ,SAAS,cACjB,QAAQ,SAAS,QAEjB,QAAQ,KAAK,CAAC,sBAAsB,EAAE,QAAQ,MAAM,CAAC;AAGvD,QAAO,QAAQ;AAChB;AAED,SAAgB,oBACdC,SACiC;CACjC,MAAM,OAAO,QAAQ,UAAU;AAC/B,SAAQ,MAAR;EACE,KAAK,SACH,QAAO;EACT,KAAK,KACH,QAAO;EACT,KAAK,QACH,QAAO;EACT,KAAK,WACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK;AACH,OAAI,CAAC,YAAY,WAAW,QAAQ,CAClC,OAAM,IAAI,MAAM;AAClB,UAAO,gCAAgC,QAAQ;EAEjD,QACE,OAAM,IAAI,MAAM,CAAC,sBAAsB,EAAE,MAAM;CAClD;AACF"}
@@ -1,50 +1,111 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleMultiModalOutput = handleMultiModalOutput;
4
- exports._convertOpenAIResponsesUsageToLangChainUsage = _convertOpenAIResponsesUsageToLangChainUsage;
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const __langchain_core_utils_types = require_rolldown_runtime.__toESM(require("@langchain/core/utils/types"));
3
+ const zod_v4_core = require_rolldown_runtime.__toESM(require("zod/v4/core"));
4
+ const openai_helpers_zod = require_rolldown_runtime.__toESM(require("openai/helpers/zod"));
5
+
6
+ //#region src/utils/output.ts
7
+ const SUPPORTED_METHODS = [
8
+ "jsonSchema",
9
+ "functionCalling",
10
+ "jsonMode"
11
+ ];
12
+ /**
13
+ * Get the structured output method for a given model. By default, it uses
14
+ * `jsonSchema` if the model supports it, otherwise it uses `functionCalling`.
15
+ *
16
+ * @throws if the method is invalid, e.g. is not a string or invalid method is provided.
17
+ * @param model - The model name.
18
+ * @param config - The structured output method options.
19
+ * @returns The structured output method.
20
+ */
21
+ function getStructuredOutputMethod(model, method) {
22
+ /**
23
+ * If a method is provided, validate it.
24
+ */
25
+ if (typeof method !== "undefined" && !SUPPORTED_METHODS.includes(method)) throw new Error(`Invalid method: ${method}. Supported methods are: ${SUPPORTED_METHODS.join(", ")}`);
26
+ const hasSupportForJsonSchema = !model.startsWith("gpt-3") && !model.startsWith("gpt-4-") && model !== "gpt-4";
27
+ /**
28
+ * If the model supports JSON Schema, use it by default.
29
+ */
30
+ if (hasSupportForJsonSchema && !method) return "jsonSchema";
31
+ if (!hasSupportForJsonSchema && method === "jsonSchema") throw new Error(`JSON Schema is not supported for model "${model}". Please use a different method, e.g. "functionCalling" or "jsonMode".`);
32
+ /**
33
+ * If the model does not support JSON Schema, use function calling by default.
34
+ */
35
+ return method ?? "functionCalling";
36
+ }
37
+ function makeParseableResponseFormat(response_format, parser) {
38
+ const obj = { ...response_format };
39
+ Object.defineProperties(obj, {
40
+ $brand: {
41
+ value: "auto-parseable-response-format",
42
+ enumerable: false
43
+ },
44
+ $parseRaw: {
45
+ value: parser,
46
+ enumerable: false
47
+ }
48
+ });
49
+ return obj;
50
+ }
51
+ function interopZodResponseFormat(zodSchema, name, props) {
52
+ if ((0, __langchain_core_utils_types.isZodSchemaV3)(zodSchema)) return (0, openai_helpers_zod.zodResponseFormat)(zodSchema, name, props);
53
+ if ((0, __langchain_core_utils_types.isZodSchemaV4)(zodSchema)) return makeParseableResponseFormat({
54
+ type: "json_schema",
55
+ json_schema: {
56
+ ...props,
57
+ name,
58
+ strict: true,
59
+ schema: (0, zod_v4_core.toJSONSchema)(zodSchema, {
60
+ cycles: "ref",
61
+ reused: "ref",
62
+ override(ctx) {
63
+ ctx.jsonSchema.title = name;
64
+ }
65
+ })
66
+ }
67
+ }, (content) => (0, zod_v4_core.parse)(zodSchema, JSON.parse(content)));
68
+ throw new Error("Unsupported schema response format");
69
+ }
5
70
  /**
6
- * Handle multi modal response content.
7
- *
8
- * @param content The content of the message.
9
- * @param messages The messages of the response.
10
- * @returns The new content of the message.
11
- */
71
+ * Handle multi modal response content.
72
+ *
73
+ * @param content The content of the message.
74
+ * @param messages The messages of the response.
75
+ * @returns The new content of the message.
76
+ */
12
77
  function handleMultiModalOutput(content, messages) {
13
- /**
14
- * Handle OpenRouter image responses
15
- * @see https://openrouter.ai/docs/features/multimodal/image-generation#api-usage
16
- */
17
- if (messages &&
18
- typeof messages === "object" &&
19
- "images" in messages &&
20
- Array.isArray(messages.images)) {
21
- const images = messages.images
22
- .filter((image) => typeof image?.image_url?.url === "string")
23
- .map((image) => ({
24
- type: "image",
25
- url: image.image_url.url,
26
- source_type: "url",
27
- }));
28
- return [{ type: "text", text: content, source_type: "text" }, ...images];
29
- }
30
- return content;
78
+ /**
79
+ * Handle OpenRouter image responses
80
+ * @see https://openrouter.ai/docs/features/multimodal/image-generation#api-usage
81
+ */
82
+ if (messages && typeof messages === "object" && "images" in messages && Array.isArray(messages.images)) {
83
+ const images = messages.images.filter((image) => typeof image?.image_url?.url === "string").map((image) => ({
84
+ type: "image",
85
+ url: image.image_url.url
86
+ }));
87
+ return [{
88
+ type: "text",
89
+ text: content
90
+ }, ...images];
91
+ }
92
+ return content;
31
93
  }
32
94
  function _convertOpenAIResponsesUsageToLangChainUsage(usage) {
33
- const inputTokenDetails = {
34
- ...(usage?.input_tokens_details?.cached_tokens != null && {
35
- cache_read: usage?.input_tokens_details?.cached_tokens,
36
- }),
37
- };
38
- const outputTokenDetails = {
39
- ...(usage?.output_tokens_details?.reasoning_tokens != null && {
40
- reasoning: usage?.output_tokens_details?.reasoning_tokens,
41
- }),
42
- };
43
- return {
44
- input_tokens: usage?.input_tokens ?? 0,
45
- output_tokens: usage?.output_tokens ?? 0,
46
- total_tokens: usage?.total_tokens ?? 0,
47
- input_token_details: inputTokenDetails,
48
- output_token_details: outputTokenDetails,
49
- };
95
+ const inputTokenDetails = { ...usage?.input_tokens_details?.cached_tokens != null && { cache_read: usage?.input_tokens_details?.cached_tokens } };
96
+ const outputTokenDetails = { ...usage?.output_tokens_details?.reasoning_tokens != null && { reasoning: usage?.output_tokens_details?.reasoning_tokens } };
97
+ return {
98
+ input_tokens: usage?.input_tokens ?? 0,
99
+ output_tokens: usage?.output_tokens ?? 0,
100
+ total_tokens: usage?.total_tokens ?? 0,
101
+ input_token_details: inputTokenDetails,
102
+ output_token_details: outputTokenDetails
103
+ };
50
104
  }
105
+
106
+ //#endregion
107
+ exports._convertOpenAIResponsesUsageToLangChainUsage = _convertOpenAIResponsesUsageToLangChainUsage;
108
+ exports.getStructuredOutputMethod = getStructuredOutputMethod;
109
+ exports.handleMultiModalOutput = handleMultiModalOutput;
110
+ exports.interopZodResponseFormat = interopZodResponseFormat;
111
+ //# sourceMappingURL=output.cjs.map