@mastra/server 0.0.0-update-scorers-api-20250801170445 → 0.0.0-usechat-duplicate-20251016110554

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 (234) hide show
  1. package/CHANGELOG.md +4048 -0
  2. package/README.md +0 -5
  3. package/dist/chunk-2PLXW4ZX.cjs +2777 -0
  4. package/dist/chunk-2PLXW4ZX.cjs.map +1 -0
  5. package/dist/chunk-2S4IMB6E.cjs +130 -0
  6. package/dist/chunk-2S4IMB6E.cjs.map +1 -0
  7. package/dist/{chunk-X5SDJOY6.cjs → chunk-3LTO44EP.cjs} +66 -53
  8. package/dist/chunk-3LTO44EP.cjs.map +1 -0
  9. package/dist/{chunk-4US5W7PH.cjs → chunk-3SNGNFUJ.cjs} +23 -15
  10. package/dist/chunk-3SNGNFUJ.cjs.map +1 -0
  11. package/dist/{chunk-OW4FX5TS.js → chunk-4QCXUEAT.js} +10 -3
  12. package/dist/chunk-4QCXUEAT.js.map +1 -0
  13. package/dist/{chunk-SQ7KKIH3.js → chunk-67GYDFSB.js} +22 -5
  14. package/dist/chunk-67GYDFSB.js.map +1 -0
  15. package/dist/{chunk-ILCWPBYQ.cjs → chunk-6ZR275MD.cjs} +23 -5
  16. package/dist/chunk-6ZR275MD.cjs.map +1 -0
  17. package/dist/chunk-7HDOBOGG.cjs +17517 -0
  18. package/dist/chunk-7HDOBOGG.cjs.map +1 -0
  19. package/dist/{chunk-4MQU6AJN.js → chunk-ABRFV4XP.js} +4 -4
  20. package/dist/{chunk-4MQU6AJN.js.map → chunk-ABRFV4XP.js.map} +1 -1
  21. package/dist/{chunk-YS66MFDY.js → chunk-AHB4JCIQ.js} +48 -12
  22. package/dist/chunk-AHB4JCIQ.js.map +1 -0
  23. package/dist/{chunk-TXAQU2XN.js → chunk-AW5BU3RQ.js} +63 -22
  24. package/dist/chunk-AW5BU3RQ.js.map +1 -0
  25. package/dist/chunk-BATEJLED.js +124 -0
  26. package/dist/chunk-BATEJLED.js.map +1 -0
  27. package/dist/chunk-BZLZFHAT.cjs +682 -0
  28. package/dist/chunk-BZLZFHAT.cjs.map +1 -0
  29. package/dist/{chunk-BUOJL3MN.js → chunk-DB63UMTV.js} +220 -89
  30. package/dist/chunk-DB63UMTV.js.map +1 -0
  31. package/dist/{chunk-NCS2OXX6.cjs → chunk-EP3JQDPD.cjs} +4 -4
  32. package/dist/{chunk-NCS2OXX6.cjs.map → chunk-EP3JQDPD.cjs.map} +1 -1
  33. package/dist/chunk-FWO7VQDC.js +17481 -0
  34. package/dist/chunk-FWO7VQDC.js.map +1 -0
  35. package/dist/chunk-FZNS32WJ.js +46 -0
  36. package/dist/chunk-FZNS32WJ.js.map +1 -0
  37. package/dist/{chunk-4QSNRCOT.cjs → chunk-G4PUALCE.cjs} +10 -2
  38. package/dist/chunk-G4PUALCE.cjs.map +1 -0
  39. package/dist/chunk-GU4EWMZB.cjs +769 -0
  40. package/dist/chunk-GU4EWMZB.cjs.map +1 -0
  41. package/dist/{chunk-TRWPUPGD.js → chunk-HPXAM2PG.js} +109 -138
  42. package/dist/chunk-HPXAM2PG.js.map +1 -0
  43. package/dist/{chunk-JGVY3KWV.cjs → chunk-I3C4ODGV.cjs} +12 -11
  44. package/dist/chunk-I3C4ODGV.cjs.map +1 -0
  45. package/dist/chunk-JSLAYFEW.js +662 -0
  46. package/dist/chunk-JSLAYFEW.js.map +1 -0
  47. package/dist/chunk-LF2ZLOFP.js +767 -0
  48. package/dist/chunk-LF2ZLOFP.js.map +1 -0
  49. package/dist/{chunk-UVOSITKU.js → chunk-LLUOPR3J.js} +11 -10
  50. package/dist/chunk-LLUOPR3J.js.map +1 -0
  51. package/dist/chunk-LPM6BBAX.cjs +1043 -0
  52. package/dist/chunk-LPM6BBAX.cjs.map +1 -0
  53. package/dist/{chunk-S37KCGOR.cjs → chunk-MQDBNHBT.cjs} +222 -88
  54. package/dist/chunk-MQDBNHBT.cjs.map +1 -0
  55. package/dist/{chunk-G7NVCO5M.js → chunk-MYR4PVGN.js} +3 -3
  56. package/dist/{chunk-G7NVCO5M.js.map → chunk-MYR4PVGN.js.map} +1 -1
  57. package/dist/chunk-O7I5CWRX.cjs +44 -0
  58. package/dist/{chunk-Q7SFCCGT.cjs.map → chunk-O7I5CWRX.cjs.map} +1 -1
  59. package/dist/{chunk-HXIOPAHT.js → chunk-OEOGYAQC.js} +54 -42
  60. package/dist/chunk-OEOGYAQC.js.map +1 -0
  61. package/dist/chunk-OYL5TBJW.cjs +48 -0
  62. package/dist/chunk-OYL5TBJW.cjs.map +1 -0
  63. package/dist/chunk-PR4QN5HX.js +39 -0
  64. package/dist/{chunk-PZ5AY32C.js.map → chunk-PR4QN5HX.js.map} +1 -1
  65. package/dist/{chunk-HWHKM67I.js → chunk-QQXMIP6C.js} +21 -13
  66. package/dist/chunk-QQXMIP6C.js.map +1 -0
  67. package/dist/chunk-RQK4FQUD.js +1012 -0
  68. package/dist/chunk-RQK4FQUD.js.map +1 -0
  69. package/dist/chunk-SIW6CYO3.js +254 -0
  70. package/dist/chunk-SIW6CYO3.js.map +1 -0
  71. package/dist/{chunk-VIGPWTDA.cjs → chunk-SPRRK3P7.cjs} +64 -23
  72. package/dist/chunk-SPRRK3P7.cjs.map +1 -0
  73. package/dist/{chunk-GASWM5HJ.cjs → chunk-TOP25AIO.cjs} +110 -139
  74. package/dist/chunk-TOP25AIO.cjs.map +1 -0
  75. package/dist/{chunk-O4I563YW.cjs → chunk-TRGAMKHX.cjs} +50 -13
  76. package/dist/chunk-TRGAMKHX.cjs.map +1 -0
  77. package/dist/chunk-VY4ENABS.cjs +135 -0
  78. package/dist/chunk-VY4ENABS.cjs.map +1 -0
  79. package/dist/{chunk-BED2O446.cjs → chunk-XN74I6VW.cjs} +7 -7
  80. package/dist/{chunk-BED2O446.cjs.map → chunk-XN74I6VW.cjs.map} +1 -1
  81. package/dist/chunk-XVPBGW6Y.cjs +256 -0
  82. package/dist/chunk-XVPBGW6Y.cjs.map +1 -0
  83. package/dist/chunk-YWOK4F5A.js +131 -0
  84. package/dist/chunk-YWOK4F5A.js.map +1 -0
  85. package/dist/chunk-ZULZ2752.js +2774 -0
  86. package/dist/chunk-ZULZ2752.js.map +1 -0
  87. package/dist/dist-4MVGNSRL.cjs +1150 -0
  88. package/dist/dist-4MVGNSRL.cjs.map +1 -0
  89. package/dist/dist-FZYCV3VB.cjs +940 -0
  90. package/dist/dist-FZYCV3VB.cjs.map +1 -0
  91. package/dist/dist-G2BYZJOC.cjs +928 -0
  92. package/dist/dist-G2BYZJOC.cjs.map +1 -0
  93. package/dist/dist-P4MXBQ3U.cjs +16 -0
  94. package/dist/dist-P4MXBQ3U.cjs.map +1 -0
  95. package/dist/dist-PQZUVLPC.js +937 -0
  96. package/dist/dist-PQZUVLPC.js.map +1 -0
  97. package/dist/dist-R7WYX6LC.js +925 -0
  98. package/dist/dist-R7WYX6LC.js.map +1 -0
  99. package/dist/dist-RFMYFILX.cjs +764 -0
  100. package/dist/dist-RFMYFILX.cjs.map +1 -0
  101. package/dist/dist-X7XR3M3Z.js +1147 -0
  102. package/dist/dist-X7XR3M3Z.js.map +1 -0
  103. package/dist/dist-XVBSOGFK.js +761 -0
  104. package/dist/dist-XVBSOGFK.js.map +1 -0
  105. package/dist/dist-YREX2TJT.js +3 -0
  106. package/dist/dist-YREX2TJT.js.map +1 -0
  107. package/dist/index.cjs +4 -0
  108. package/dist/index.cjs.map +1 -1
  109. package/dist/index.js +3 -0
  110. package/dist/index.js.map +1 -1
  111. package/dist/server/a2a/store.cjs +25 -0
  112. package/dist/server/a2a/store.cjs.map +1 -0
  113. package/dist/server/a2a/store.d.ts +3 -3
  114. package/dist/server/a2a/store.d.ts.map +1 -1
  115. package/dist/server/a2a/store.js +23 -0
  116. package/dist/server/a2a/store.js.map +1 -0
  117. package/dist/server/a2a/tasks.d.ts +7 -7
  118. package/dist/server/a2a/tasks.d.ts.map +1 -1
  119. package/dist/server/handlers/a2a.cjs +11 -11
  120. package/dist/server/handlers/a2a.d.ts +17 -15
  121. package/dist/server/handlers/a2a.d.ts.map +1 -1
  122. package/dist/server/handlers/a2a.js +1 -1
  123. package/dist/server/handlers/agent-builder.cjs +68 -0
  124. package/dist/server/handlers/agent-builder.cjs.map +1 -0
  125. package/dist/server/handlers/agent-builder.d.ts +94 -0
  126. package/dist/server/handlers/agent-builder.d.ts.map +1 -0
  127. package/dist/server/handlers/agent-builder.js +3 -0
  128. package/dist/server/handlers/agent-builder.js.map +1 -0
  129. package/dist/server/handlers/agents.cjs +53 -9
  130. package/dist/server/handlers/agents.d.ts +156 -33
  131. package/dist/server/handlers/agents.d.ts.map +1 -1
  132. package/dist/server/handlers/agents.js +1 -1
  133. package/dist/server/handlers/legacyWorkflows.cjs +11 -11
  134. package/dist/server/handlers/legacyWorkflows.d.ts +1 -1
  135. package/dist/server/handlers/legacyWorkflows.d.ts.map +1 -1
  136. package/dist/server/handlers/legacyWorkflows.js +1 -1
  137. package/dist/server/handlers/logs.cjs +4 -4
  138. package/dist/server/handlers/logs.js +1 -1
  139. package/dist/server/handlers/memory.cjs +19 -15
  140. package/dist/server/handlers/memory.d.ts +24 -19
  141. package/dist/server/handlers/memory.d.ts.map +1 -1
  142. package/dist/server/handlers/memory.js +1 -1
  143. package/dist/server/handlers/observability.cjs +24 -0
  144. package/dist/server/handlers/observability.cjs.map +1 -0
  145. package/dist/server/handlers/observability.d.ts +49 -0
  146. package/dist/server/handlers/observability.d.ts.map +1 -0
  147. package/dist/server/handlers/observability.js +3 -0
  148. package/dist/server/handlers/observability.js.map +1 -0
  149. package/dist/server/handlers/scores.cjs +7 -7
  150. package/dist/server/handlers/scores.d.ts +110 -7
  151. package/dist/server/handlers/scores.d.ts.map +1 -1
  152. package/dist/server/handlers/scores.js +1 -1
  153. package/dist/server/handlers/telemetry.cjs +7 -3
  154. package/dist/server/handlers/telemetry.d.ts +2 -1
  155. package/dist/server/handlers/telemetry.d.ts.map +1 -1
  156. package/dist/server/handlers/telemetry.js +1 -1
  157. package/dist/server/handlers/tools.cjs +9 -5
  158. package/dist/server/handlers/tools.d.ts +5 -1
  159. package/dist/server/handlers/tools.d.ts.map +1 -1
  160. package/dist/server/handlers/tools.js +1 -1
  161. package/dist/server/handlers/utils.cjs +6 -2
  162. package/dist/server/handlers/utils.d.ts +6 -0
  163. package/dist/server/handlers/utils.d.ts.map +1 -1
  164. package/dist/server/handlers/utils.js +1 -1
  165. package/dist/server/handlers/vector.cjs +7 -7
  166. package/dist/server/handlers/vector.d.ts +1 -1
  167. package/dist/server/handlers/vector.js +1 -1
  168. package/dist/server/handlers/voice.cjs +5 -5
  169. package/dist/server/handlers/voice.d.ts +7 -5
  170. package/dist/server/handlers/voice.d.ts.map +1 -1
  171. package/dist/server/handlers/voice.js +1 -1
  172. package/dist/server/handlers/workflows.cjs +28 -16
  173. package/dist/server/handlers/workflows.d.ts +37 -34
  174. package/dist/server/handlers/workflows.d.ts.map +1 -1
  175. package/dist/server/handlers/workflows.js +1 -1
  176. package/dist/server/handlers.cjs +38 -33
  177. package/dist/server/handlers.d.ts +13 -12
  178. package/dist/server/handlers.d.ts.map +1 -1
  179. package/dist/server/handlers.js +13 -12
  180. package/dist/server/utils.d.ts +44 -0
  181. package/dist/server/utils.d.ts.map +1 -0
  182. package/dist/token-53CFGXUO.cjs +64 -0
  183. package/dist/token-53CFGXUO.cjs.map +1 -0
  184. package/dist/token-WAEKDUVY.js +62 -0
  185. package/dist/token-WAEKDUVY.js.map +1 -0
  186. package/dist/token-util-LY37VCIN.js +7 -0
  187. package/dist/token-util-LY37VCIN.js.map +1 -0
  188. package/dist/token-util-S4A355II.cjs +9 -0
  189. package/dist/token-util-S4A355II.cjs.map +1 -0
  190. package/package.json +36 -14
  191. package/dist/chunk-2XIJW7SV.cjs +0 -310
  192. package/dist/chunk-2XIJW7SV.cjs.map +0 -1
  193. package/dist/chunk-4CEZIJWJ.cjs +0 -150
  194. package/dist/chunk-4CEZIJWJ.cjs.map +0 -1
  195. package/dist/chunk-4QSNRCOT.cjs.map +0 -1
  196. package/dist/chunk-4US5W7PH.cjs.map +0 -1
  197. package/dist/chunk-7FHF55WA.js +0 -2041
  198. package/dist/chunk-7FHF55WA.js.map +0 -1
  199. package/dist/chunk-BUOJL3MN.js.map +0 -1
  200. package/dist/chunk-GASWM5HJ.cjs.map +0 -1
  201. package/dist/chunk-HWHKM67I.js.map +0 -1
  202. package/dist/chunk-HXIOPAHT.js.map +0 -1
  203. package/dist/chunk-ILCWPBYQ.cjs.map +0 -1
  204. package/dist/chunk-JGVY3KWV.cjs.map +0 -1
  205. package/dist/chunk-LRUH33B4.cjs +0 -2044
  206. package/dist/chunk-LRUH33B4.cjs.map +0 -1
  207. package/dist/chunk-O4I563YW.cjs.map +0 -1
  208. package/dist/chunk-OW4FX5TS.js.map +0 -1
  209. package/dist/chunk-PZ5AY32C.js +0 -9
  210. package/dist/chunk-Q7SFCCGT.cjs +0 -11
  211. package/dist/chunk-S37KCGOR.cjs.map +0 -1
  212. package/dist/chunk-SQ7KKIH3.js.map +0 -1
  213. package/dist/chunk-TRWPUPGD.js.map +0 -1
  214. package/dist/chunk-TXAQU2XN.js.map +0 -1
  215. package/dist/chunk-UVOSITKU.js.map +0 -1
  216. package/dist/chunk-VIGPWTDA.cjs.map +0 -1
  217. package/dist/chunk-VOY6MGKA.js +0 -301
  218. package/dist/chunk-VOY6MGKA.js.map +0 -1
  219. package/dist/chunk-X5SDJOY6.cjs.map +0 -1
  220. package/dist/chunk-Y3J45CVZ.js +0 -144
  221. package/dist/chunk-Y3J45CVZ.js.map +0 -1
  222. package/dist/chunk-YS66MFDY.js.map +0 -1
  223. package/dist/server/handlers/network.cjs +0 -24
  224. package/dist/server/handlers/network.cjs.map +0 -1
  225. package/dist/server/handlers/network.d.ts +0 -50
  226. package/dist/server/handlers/network.d.ts.map +0 -1
  227. package/dist/server/handlers/network.js +0 -3
  228. package/dist/server/handlers/network.js.map +0 -1
  229. package/dist/server/handlers/vNextNetwork.cjs +0 -220
  230. package/dist/server/handlers/vNextNetwork.cjs.map +0 -1
  231. package/dist/server/handlers/vNextNetwork.d.ts +0 -179
  232. package/dist/server/handlers/vNextNetwork.d.ts.map +0 -1
  233. package/dist/server/handlers/vNextNetwork.js +0 -213
  234. package/dist/server/handlers/vNextNetwork.js.map +0 -1
@@ -0,0 +1,925 @@
1
+ import { withoutTrailingSlash, loadApiKey, createJsonErrorResponseHandler, postJsonToApi, createJsonResponseHandler, combineHeaders, generateId, createEventSourceResponseHandler, InvalidResponseDataError, isParsableJson, UnsupportedFunctionalityError, convertUint8ArrayToBase64, NoSuchModelError } from './chunk-RQK4FQUD.js';
2
+ import { z } from 'zod';
3
+
4
+ function getOpenAIMetadata(message) {
5
+ var _a, _b;
6
+ return (_b = (_a = message == null ? void 0 : message.providerMetadata) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
7
+ }
8
+ function convertToOpenAICompatibleChatMessages(prompt) {
9
+ const messages = [];
10
+ for (const { role, content, ...message } of prompt) {
11
+ const metadata = getOpenAIMetadata({ ...message });
12
+ switch (role) {
13
+ case "system": {
14
+ messages.push({ role: "system", content, ...metadata });
15
+ break;
16
+ }
17
+ case "user": {
18
+ if (content.length === 1 && content[0].type === "text") {
19
+ messages.push({
20
+ role: "user",
21
+ content: content[0].text,
22
+ ...getOpenAIMetadata(content[0])
23
+ });
24
+ break;
25
+ }
26
+ messages.push({
27
+ role: "user",
28
+ content: content.map((part) => {
29
+ var _a;
30
+ const partMetadata = getOpenAIMetadata(part);
31
+ switch (part.type) {
32
+ case "text": {
33
+ return { type: "text", text: part.text, ...partMetadata };
34
+ }
35
+ case "image": {
36
+ return {
37
+ type: "image_url",
38
+ image_url: {
39
+ url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
40
+ },
41
+ ...partMetadata
42
+ };
43
+ }
44
+ case "file": {
45
+ throw new UnsupportedFunctionalityError({
46
+ functionality: "File content parts in user messages"
47
+ });
48
+ }
49
+ }
50
+ }),
51
+ ...metadata
52
+ });
53
+ break;
54
+ }
55
+ case "assistant": {
56
+ let text = "";
57
+ const toolCalls = [];
58
+ for (const part of content) {
59
+ const partMetadata = getOpenAIMetadata(part);
60
+ switch (part.type) {
61
+ case "text": {
62
+ text += part.text;
63
+ break;
64
+ }
65
+ case "tool-call": {
66
+ toolCalls.push({
67
+ id: part.toolCallId,
68
+ type: "function",
69
+ function: {
70
+ name: part.toolName,
71
+ arguments: JSON.stringify(part.args)
72
+ },
73
+ ...partMetadata
74
+ });
75
+ break;
76
+ }
77
+ }
78
+ }
79
+ messages.push({
80
+ role: "assistant",
81
+ content: text,
82
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
83
+ ...metadata
84
+ });
85
+ break;
86
+ }
87
+ case "tool": {
88
+ for (const toolResponse of content) {
89
+ const toolResponseMetadata = getOpenAIMetadata(toolResponse);
90
+ messages.push({
91
+ role: "tool",
92
+ tool_call_id: toolResponse.toolCallId,
93
+ content: JSON.stringify(toolResponse.result),
94
+ ...toolResponseMetadata
95
+ });
96
+ }
97
+ break;
98
+ }
99
+ default: {
100
+ const _exhaustiveCheck = role;
101
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
102
+ }
103
+ }
104
+ }
105
+ return messages;
106
+ }
107
+ function getResponseMetadata({
108
+ id,
109
+ model,
110
+ created
111
+ }) {
112
+ return {
113
+ id: id != null ? id : void 0,
114
+ modelId: model != null ? model : void 0,
115
+ timestamp: created != null ? new Date(created * 1e3) : void 0
116
+ };
117
+ }
118
+ function mapOpenAICompatibleFinishReason(finishReason) {
119
+ switch (finishReason) {
120
+ case "stop":
121
+ return "stop";
122
+ case "length":
123
+ return "length";
124
+ case "content_filter":
125
+ return "content-filter";
126
+ case "function_call":
127
+ case "tool_calls":
128
+ return "tool-calls";
129
+ default:
130
+ return "unknown";
131
+ }
132
+ }
133
+ var openaiCompatibleErrorDataSchema = z.object({
134
+ error: z.object({
135
+ message: z.string(),
136
+ // The additional information below is handled loosely to support
137
+ // OpenAI-compatible providers that have slightly different error
138
+ // responses:
139
+ type: z.string().nullish(),
140
+ param: z.any().nullish(),
141
+ code: z.union([z.string(), z.number()]).nullish()
142
+ })
143
+ });
144
+ var defaultOpenAICompatibleErrorStructure = {
145
+ errorSchema: openaiCompatibleErrorDataSchema,
146
+ errorToMessage: (data) => data.error.message
147
+ };
148
+ function prepareTools({
149
+ mode,
150
+ structuredOutputs
151
+ }) {
152
+ var _a;
153
+ const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
154
+ const toolWarnings = [];
155
+ if (tools == null) {
156
+ return { tools: void 0, tool_choice: void 0, toolWarnings };
157
+ }
158
+ const toolChoice = mode.toolChoice;
159
+ const openaiCompatTools = [];
160
+ for (const tool of tools) {
161
+ if (tool.type === "provider-defined") {
162
+ toolWarnings.push({ type: "unsupported-tool", tool });
163
+ } else {
164
+ openaiCompatTools.push({
165
+ type: "function",
166
+ function: {
167
+ name: tool.name,
168
+ description: tool.description,
169
+ parameters: tool.parameters
170
+ }
171
+ });
172
+ }
173
+ }
174
+ if (toolChoice == null) {
175
+ return { tools: openaiCompatTools, tool_choice: void 0, toolWarnings };
176
+ }
177
+ const type = toolChoice.type;
178
+ switch (type) {
179
+ case "auto":
180
+ case "none":
181
+ case "required":
182
+ return { tools: openaiCompatTools, tool_choice: type, toolWarnings };
183
+ case "tool":
184
+ return {
185
+ tools: openaiCompatTools,
186
+ tool_choice: {
187
+ type: "function",
188
+ function: {
189
+ name: toolChoice.toolName
190
+ }
191
+ },
192
+ toolWarnings
193
+ };
194
+ default: {
195
+ const _exhaustiveCheck = type;
196
+ throw new UnsupportedFunctionalityError({
197
+ functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
198
+ });
199
+ }
200
+ }
201
+ }
202
+ var OpenAICompatibleChatLanguageModel = class {
203
+ // type inferred via constructor
204
+ constructor(modelId, settings, config) {
205
+ this.specificationVersion = "v1";
206
+ var _a, _b;
207
+ this.modelId = modelId;
208
+ this.settings = settings;
209
+ this.config = config;
210
+ const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
211
+ this.chunkSchema = createOpenAICompatibleChatChunkSchema(
212
+ errorStructure.errorSchema
213
+ );
214
+ this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
215
+ this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
216
+ }
217
+ get defaultObjectGenerationMode() {
218
+ return this.config.defaultObjectGenerationMode;
219
+ }
220
+ get provider() {
221
+ return this.config.provider;
222
+ }
223
+ get providerOptionsName() {
224
+ return this.config.provider.split(".")[0].trim();
225
+ }
226
+ getArgs({
227
+ mode,
228
+ prompt,
229
+ maxTokens,
230
+ temperature,
231
+ topP,
232
+ topK,
233
+ frequencyPenalty,
234
+ presencePenalty,
235
+ providerMetadata,
236
+ stopSequences,
237
+ responseFormat,
238
+ seed
239
+ }) {
240
+ var _a, _b, _c, _d, _e;
241
+ const type = mode.type;
242
+ const warnings = [];
243
+ if (topK != null) {
244
+ warnings.push({
245
+ type: "unsupported-setting",
246
+ setting: "topK"
247
+ });
248
+ }
249
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
250
+ warnings.push({
251
+ type: "unsupported-setting",
252
+ setting: "responseFormat",
253
+ details: "JSON response format schema is only supported with structuredOutputs"
254
+ });
255
+ }
256
+ const baseArgs = {
257
+ // model id:
258
+ model: this.modelId,
259
+ // model specific settings:
260
+ user: this.settings.user,
261
+ // standardized settings:
262
+ max_tokens: maxTokens,
263
+ temperature,
264
+ top_p: topP,
265
+ frequency_penalty: frequencyPenalty,
266
+ presence_penalty: presencePenalty,
267
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
268
+ type: "json_schema",
269
+ json_schema: {
270
+ schema: responseFormat.schema,
271
+ name: (_a = responseFormat.name) != null ? _a : "response",
272
+ description: responseFormat.description
273
+ }
274
+ } : { type: "json_object" } : void 0,
275
+ stop: stopSequences,
276
+ seed,
277
+ ...providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName],
278
+ reasoning_effort: (_d = (_b = providerMetadata == null ? void 0 : providerMetadata[this.providerOptionsName]) == null ? void 0 : _b.reasoningEffort) != null ? _d : (_c = providerMetadata == null ? void 0 : providerMetadata["openai-compatible"]) == null ? void 0 : _c.reasoningEffort,
279
+ // messages:
280
+ messages: convertToOpenAICompatibleChatMessages(prompt)
281
+ };
282
+ switch (type) {
283
+ case "regular": {
284
+ const { tools, tool_choice, toolWarnings } = prepareTools({
285
+ mode,
286
+ structuredOutputs: this.supportsStructuredOutputs
287
+ });
288
+ return {
289
+ args: { ...baseArgs, tools, tool_choice },
290
+ warnings: [...warnings, ...toolWarnings]
291
+ };
292
+ }
293
+ case "object-json": {
294
+ return {
295
+ args: {
296
+ ...baseArgs,
297
+ response_format: this.supportsStructuredOutputs === true && mode.schema != null ? {
298
+ type: "json_schema",
299
+ json_schema: {
300
+ schema: mode.schema,
301
+ name: (_e = mode.name) != null ? _e : "response",
302
+ description: mode.description
303
+ }
304
+ } : { type: "json_object" }
305
+ },
306
+ warnings
307
+ };
308
+ }
309
+ case "object-tool": {
310
+ return {
311
+ args: {
312
+ ...baseArgs,
313
+ tool_choice: {
314
+ type: "function",
315
+ function: { name: mode.tool.name }
316
+ },
317
+ tools: [
318
+ {
319
+ type: "function",
320
+ function: {
321
+ name: mode.tool.name,
322
+ description: mode.tool.description,
323
+ parameters: mode.tool.parameters
324
+ }
325
+ }
326
+ ]
327
+ },
328
+ warnings
329
+ };
330
+ }
331
+ default: {
332
+ const _exhaustiveCheck = type;
333
+ throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
334
+ }
335
+ }
336
+ }
337
+ async doGenerate(options) {
338
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
339
+ const { args, warnings } = this.getArgs({ ...options });
340
+ const body = JSON.stringify(args);
341
+ const {
342
+ responseHeaders,
343
+ value: responseBody,
344
+ rawValue: rawResponse
345
+ } = await postJsonToApi({
346
+ url: this.config.url({
347
+ path: "/chat/completions",
348
+ modelId: this.modelId
349
+ }),
350
+ headers: combineHeaders(this.config.headers(), options.headers),
351
+ body: args,
352
+ failedResponseHandler: this.failedResponseHandler,
353
+ successfulResponseHandler: createJsonResponseHandler(
354
+ OpenAICompatibleChatResponseSchema
355
+ ),
356
+ abortSignal: options.abortSignal,
357
+ fetch: this.config.fetch
358
+ });
359
+ const { messages: rawPrompt, ...rawSettings } = args;
360
+ const choice = responseBody.choices[0];
361
+ const providerMetadata = {
362
+ [this.providerOptionsName]: {},
363
+ ...(_b = (_a = this.config.metadataExtractor) == null ? void 0 : _a.extractMetadata) == null ? void 0 : _b.call(_a, {
364
+ parsedBody: rawResponse
365
+ })
366
+ };
367
+ const completionTokenDetails = (_c = responseBody.usage) == null ? void 0 : _c.completion_tokens_details;
368
+ const promptTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.prompt_tokens_details;
369
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
370
+ providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
371
+ }
372
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
373
+ providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
374
+ }
375
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
376
+ providerMetadata[this.providerOptionsName].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
377
+ }
378
+ if ((promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null) {
379
+ providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
380
+ }
381
+ return {
382
+ text: (_e = choice.message.content) != null ? _e : void 0,
383
+ reasoning: (_f = choice.message.reasoning_content) != null ? _f : void 0,
384
+ toolCalls: (_g = choice.message.tool_calls) == null ? void 0 : _g.map((toolCall) => {
385
+ var _a2;
386
+ return {
387
+ toolCallType: "function",
388
+ toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
389
+ toolName: toolCall.function.name,
390
+ args: toolCall.function.arguments
391
+ };
392
+ }),
393
+ finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
394
+ usage: {
395
+ promptTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.prompt_tokens) != null ? _i : NaN,
396
+ completionTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.completion_tokens) != null ? _k : NaN
397
+ },
398
+ providerMetadata,
399
+ rawCall: { rawPrompt, rawSettings },
400
+ rawResponse: { headers: responseHeaders, body: rawResponse },
401
+ response: getResponseMetadata(responseBody),
402
+ warnings,
403
+ request: { body }
404
+ };
405
+ }
406
+ async doStream(options) {
407
+ var _a;
408
+ if (this.settings.simulateStreaming) {
409
+ const result = await this.doGenerate(options);
410
+ const simulatedStream = new ReadableStream({
411
+ start(controller) {
412
+ controller.enqueue({ type: "response-metadata", ...result.response });
413
+ if (result.reasoning) {
414
+ if (Array.isArray(result.reasoning)) {
415
+ for (const part of result.reasoning) {
416
+ if (part.type === "text") {
417
+ controller.enqueue({
418
+ type: "reasoning",
419
+ textDelta: part.text
420
+ });
421
+ }
422
+ }
423
+ } else {
424
+ controller.enqueue({
425
+ type: "reasoning",
426
+ textDelta: result.reasoning
427
+ });
428
+ }
429
+ }
430
+ if (result.text) {
431
+ controller.enqueue({
432
+ type: "text-delta",
433
+ textDelta: result.text
434
+ });
435
+ }
436
+ if (result.toolCalls) {
437
+ for (const toolCall of result.toolCalls) {
438
+ controller.enqueue({
439
+ type: "tool-call",
440
+ ...toolCall
441
+ });
442
+ }
443
+ }
444
+ controller.enqueue({
445
+ type: "finish",
446
+ finishReason: result.finishReason,
447
+ usage: result.usage,
448
+ logprobs: result.logprobs,
449
+ providerMetadata: result.providerMetadata
450
+ });
451
+ controller.close();
452
+ }
453
+ });
454
+ return {
455
+ stream: simulatedStream,
456
+ rawCall: result.rawCall,
457
+ rawResponse: result.rawResponse,
458
+ warnings: result.warnings
459
+ };
460
+ }
461
+ const { args, warnings } = this.getArgs({ ...options });
462
+ const body = {
463
+ ...args,
464
+ stream: true,
465
+ // only include stream_options when in strict compatibility mode:
466
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
467
+ };
468
+ const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
469
+ const { responseHeaders, value: response } = await postJsonToApi({
470
+ url: this.config.url({
471
+ path: "/chat/completions",
472
+ modelId: this.modelId
473
+ }),
474
+ headers: combineHeaders(this.config.headers(), options.headers),
475
+ body,
476
+ failedResponseHandler: this.failedResponseHandler,
477
+ successfulResponseHandler: createEventSourceResponseHandler(
478
+ this.chunkSchema
479
+ ),
480
+ abortSignal: options.abortSignal,
481
+ fetch: this.config.fetch
482
+ });
483
+ const { messages: rawPrompt, ...rawSettings } = args;
484
+ const toolCalls = [];
485
+ let finishReason = "unknown";
486
+ let usage = {
487
+ completionTokens: void 0,
488
+ completionTokensDetails: {
489
+ reasoningTokens: void 0,
490
+ acceptedPredictionTokens: void 0,
491
+ rejectedPredictionTokens: void 0
492
+ },
493
+ promptTokens: void 0,
494
+ promptTokensDetails: {
495
+ cachedTokens: void 0
496
+ }
497
+ };
498
+ let isFirstChunk = true;
499
+ let providerOptionsName = this.providerOptionsName;
500
+ return {
501
+ stream: response.pipeThrough(
502
+ new TransformStream({
503
+ // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
504
+ transform(chunk, controller) {
505
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
506
+ if (!chunk.success) {
507
+ finishReason = "error";
508
+ controller.enqueue({ type: "error", error: chunk.error });
509
+ return;
510
+ }
511
+ const value = chunk.value;
512
+ metadataExtractor == null ? void 0 : metadataExtractor.processChunk(chunk.rawValue);
513
+ if ("error" in value) {
514
+ finishReason = "error";
515
+ controller.enqueue({ type: "error", error: value.error.message });
516
+ return;
517
+ }
518
+ if (isFirstChunk) {
519
+ isFirstChunk = false;
520
+ controller.enqueue({
521
+ type: "response-metadata",
522
+ ...getResponseMetadata(value)
523
+ });
524
+ }
525
+ if (value.usage != null) {
526
+ const {
527
+ prompt_tokens,
528
+ completion_tokens,
529
+ prompt_tokens_details,
530
+ completion_tokens_details
531
+ } = value.usage;
532
+ usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
533
+ usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
534
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
535
+ usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
536
+ }
537
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
538
+ usage.completionTokensDetails.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
539
+ }
540
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
541
+ usage.completionTokensDetails.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
542
+ }
543
+ if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
544
+ usage.promptTokensDetails.cachedTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
545
+ }
546
+ }
547
+ const choice = value.choices[0];
548
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
549
+ finishReason = mapOpenAICompatibleFinishReason(
550
+ choice.finish_reason
551
+ );
552
+ }
553
+ if ((choice == null ? void 0 : choice.delta) == null) {
554
+ return;
555
+ }
556
+ const delta = choice.delta;
557
+ if (delta.reasoning_content != null) {
558
+ controller.enqueue({
559
+ type: "reasoning",
560
+ textDelta: delta.reasoning_content
561
+ });
562
+ }
563
+ if (delta.content != null) {
564
+ controller.enqueue({
565
+ type: "text-delta",
566
+ textDelta: delta.content
567
+ });
568
+ }
569
+ if (delta.tool_calls != null) {
570
+ for (const toolCallDelta of delta.tool_calls) {
571
+ const index = toolCallDelta.index;
572
+ if (toolCalls[index] == null) {
573
+ if (toolCallDelta.type !== "function") {
574
+ throw new InvalidResponseDataError({
575
+ data: toolCallDelta,
576
+ message: `Expected 'function' type.`
577
+ });
578
+ }
579
+ if (toolCallDelta.id == null) {
580
+ throw new InvalidResponseDataError({
581
+ data: toolCallDelta,
582
+ message: `Expected 'id' to be a string.`
583
+ });
584
+ }
585
+ if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
586
+ throw new InvalidResponseDataError({
587
+ data: toolCallDelta,
588
+ message: `Expected 'function.name' to be a string.`
589
+ });
590
+ }
591
+ toolCalls[index] = {
592
+ id: toolCallDelta.id,
593
+ type: "function",
594
+ function: {
595
+ name: toolCallDelta.function.name,
596
+ arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
597
+ },
598
+ hasFinished: false
599
+ };
600
+ const toolCall2 = toolCalls[index];
601
+ if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
602
+ if (toolCall2.function.arguments.length > 0) {
603
+ controller.enqueue({
604
+ type: "tool-call-delta",
605
+ toolCallType: "function",
606
+ toolCallId: toolCall2.id,
607
+ toolName: toolCall2.function.name,
608
+ argsTextDelta: toolCall2.function.arguments
609
+ });
610
+ }
611
+ if (isParsableJson(toolCall2.function.arguments)) {
612
+ controller.enqueue({
613
+ type: "tool-call",
614
+ toolCallType: "function",
615
+ toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
616
+ toolName: toolCall2.function.name,
617
+ args: toolCall2.function.arguments
618
+ });
619
+ toolCall2.hasFinished = true;
620
+ }
621
+ }
622
+ continue;
623
+ }
624
+ const toolCall = toolCalls[index];
625
+ if (toolCall.hasFinished) {
626
+ continue;
627
+ }
628
+ if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
629
+ toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
630
+ }
631
+ controller.enqueue({
632
+ type: "tool-call-delta",
633
+ toolCallType: "function",
634
+ toolCallId: toolCall.id,
635
+ toolName: toolCall.function.name,
636
+ argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
637
+ });
638
+ if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
639
+ controller.enqueue({
640
+ type: "tool-call",
641
+ toolCallType: "function",
642
+ toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
643
+ toolName: toolCall.function.name,
644
+ args: toolCall.function.arguments
645
+ });
646
+ toolCall.hasFinished = true;
647
+ }
648
+ }
649
+ }
650
+ },
651
+ flush(controller) {
652
+ var _a2, _b;
653
+ const providerMetadata = {
654
+ [providerOptionsName]: {},
655
+ ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
656
+ };
657
+ if (usage.completionTokensDetails.reasoningTokens != null) {
658
+ providerMetadata[providerOptionsName].reasoningTokens = usage.completionTokensDetails.reasoningTokens;
659
+ }
660
+ if (usage.completionTokensDetails.acceptedPredictionTokens != null) {
661
+ providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.completionTokensDetails.acceptedPredictionTokens;
662
+ }
663
+ if (usage.completionTokensDetails.rejectedPredictionTokens != null) {
664
+ providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.completionTokensDetails.rejectedPredictionTokens;
665
+ }
666
+ if (usage.promptTokensDetails.cachedTokens != null) {
667
+ providerMetadata[providerOptionsName].cachedPromptTokens = usage.promptTokensDetails.cachedTokens;
668
+ }
669
+ controller.enqueue({
670
+ type: "finish",
671
+ finishReason,
672
+ usage: {
673
+ promptTokens: (_a2 = usage.promptTokens) != null ? _a2 : NaN,
674
+ completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
675
+ },
676
+ providerMetadata
677
+ });
678
+ }
679
+ })
680
+ ),
681
+ rawCall: { rawPrompt, rawSettings },
682
+ rawResponse: { headers: responseHeaders },
683
+ warnings,
684
+ request: { body: JSON.stringify(body) }
685
+ };
686
+ }
687
+ };
688
+ var openaiCompatibleTokenUsageSchema = z.object({
689
+ prompt_tokens: z.number().nullish(),
690
+ completion_tokens: z.number().nullish(),
691
+ prompt_tokens_details: z.object({
692
+ cached_tokens: z.number().nullish()
693
+ }).nullish(),
694
+ completion_tokens_details: z.object({
695
+ reasoning_tokens: z.number().nullish(),
696
+ accepted_prediction_tokens: z.number().nullish(),
697
+ rejected_prediction_tokens: z.number().nullish()
698
+ }).nullish()
699
+ }).nullish();
700
+ var OpenAICompatibleChatResponseSchema = z.object({
701
+ id: z.string().nullish(),
702
+ created: z.number().nullish(),
703
+ model: z.string().nullish(),
704
+ choices: z.array(
705
+ z.object({
706
+ message: z.object({
707
+ role: z.literal("assistant").nullish(),
708
+ content: z.string().nullish(),
709
+ reasoning_content: z.string().nullish(),
710
+ tool_calls: z.array(
711
+ z.object({
712
+ id: z.string().nullish(),
713
+ type: z.literal("function"),
714
+ function: z.object({
715
+ name: z.string(),
716
+ arguments: z.string()
717
+ })
718
+ })
719
+ ).nullish()
720
+ }),
721
+ finish_reason: z.string().nullish()
722
+ })
723
+ ),
724
+ usage: openaiCompatibleTokenUsageSchema
725
+ });
726
+ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z.union([
727
+ z.object({
728
+ id: z.string().nullish(),
729
+ created: z.number().nullish(),
730
+ model: z.string().nullish(),
731
+ choices: z.array(
732
+ z.object({
733
+ delta: z.object({
734
+ role: z.enum(["assistant"]).nullish(),
735
+ content: z.string().nullish(),
736
+ reasoning_content: z.string().nullish(),
737
+ tool_calls: z.array(
738
+ z.object({
739
+ index: z.number().optional(),
740
+ id: z.string().nullish(),
741
+ type: z.literal("function").nullish(),
742
+ function: z.object({
743
+ name: z.string().nullish(),
744
+ arguments: z.string().nullish()
745
+ })
746
+ })
747
+ ).nullish()
748
+ }).nullish(),
749
+ finish_reason: z.string().nullish()
750
+ })
751
+ ),
752
+ usage: openaiCompatibleTokenUsageSchema
753
+ }),
754
+ errorSchema
755
+ ]);
756
+ z.object({
757
+ id: z.string().nullish(),
758
+ created: z.number().nullish(),
759
+ model: z.string().nullish(),
760
+ choices: z.array(
761
+ z.object({
762
+ text: z.string(),
763
+ finish_reason: z.string()
764
+ })
765
+ ),
766
+ usage: z.object({
767
+ prompt_tokens: z.number(),
768
+ completion_tokens: z.number()
769
+ }).nullish()
770
+ });
771
+ z.object({
772
+ data: z.array(z.object({ embedding: z.array(z.number()) })),
773
+ usage: z.object({ prompt_tokens: z.number() }).nullish()
774
+ });
775
+ var OpenAICompatibleImageModel = class {
776
+ constructor(modelId, settings, config) {
777
+ this.modelId = modelId;
778
+ this.settings = settings;
779
+ this.config = config;
780
+ this.specificationVersion = "v1";
781
+ }
782
+ get maxImagesPerCall() {
783
+ var _a;
784
+ return (_a = this.settings.maxImagesPerCall) != null ? _a : 10;
785
+ }
786
+ get provider() {
787
+ return this.config.provider;
788
+ }
789
+ async doGenerate({
790
+ prompt,
791
+ n,
792
+ size,
793
+ aspectRatio,
794
+ seed,
795
+ providerOptions,
796
+ headers,
797
+ abortSignal
798
+ }) {
799
+ var _a, _b, _c, _d, _e;
800
+ const warnings = [];
801
+ if (aspectRatio != null) {
802
+ warnings.push({
803
+ type: "unsupported-setting",
804
+ setting: "aspectRatio",
805
+ details: "This model does not support aspect ratio. Use `size` instead."
806
+ });
807
+ }
808
+ if (seed != null) {
809
+ warnings.push({ type: "unsupported-setting", setting: "seed" });
810
+ }
811
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
812
+ const { value: response, responseHeaders } = await postJsonToApi({
813
+ url: this.config.url({
814
+ path: "/images/generations",
815
+ modelId: this.modelId
816
+ }),
817
+ headers: combineHeaders(this.config.headers(), headers),
818
+ body: {
819
+ model: this.modelId,
820
+ prompt,
821
+ n,
822
+ size,
823
+ ...(_d = providerOptions.openai) != null ? _d : {},
824
+ response_format: "b64_json",
825
+ ...this.settings.user ? { user: this.settings.user } : {}
826
+ },
827
+ failedResponseHandler: createJsonErrorResponseHandler(
828
+ (_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
829
+ ),
830
+ successfulResponseHandler: createJsonResponseHandler(
831
+ openaiCompatibleImageResponseSchema
832
+ ),
833
+ abortSignal,
834
+ fetch: this.config.fetch
835
+ });
836
+ return {
837
+ images: response.data.map((item) => item.b64_json),
838
+ warnings,
839
+ response: {
840
+ timestamp: currentDate,
841
+ modelId: this.modelId,
842
+ headers: responseHeaders
843
+ }
844
+ };
845
+ }
846
+ };
847
+ var openaiCompatibleImageResponseSchema = z.object({
848
+ data: z.array(z.object({ b64_json: z.string() }))
849
+ });
850
+ function supportsStructuredOutputs(modelId) {
851
+ return [
852
+ "grok-3",
853
+ "grok-3-beta",
854
+ "grok-3-latest",
855
+ "grok-3-fast",
856
+ "grok-3-fast-beta",
857
+ "grok-3-fast-latest",
858
+ "grok-3-mini",
859
+ "grok-3-mini-beta",
860
+ "grok-3-mini-latest",
861
+ "grok-3-mini-fast",
862
+ "grok-3-mini-fast-beta",
863
+ "grok-3-mini-fast-latest",
864
+ "grok-2-1212",
865
+ "grok-2-vision-1212"
866
+ ].includes(modelId);
867
+ }
868
+ var xaiErrorSchema = z.object({
869
+ code: z.string(),
870
+ error: z.string()
871
+ });
872
+ var xaiErrorStructure = {
873
+ errorSchema: xaiErrorSchema,
874
+ errorToMessage: (data) => data.error
875
+ };
876
+ function createXai(options = {}) {
877
+ var _a;
878
+ const baseURL = withoutTrailingSlash(
879
+ (_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
880
+ );
881
+ const getHeaders = () => ({
882
+ Authorization: `Bearer ${loadApiKey({
883
+ apiKey: options.apiKey,
884
+ environmentVariableName: "XAI_API_KEY",
885
+ description: "xAI API key"
886
+ })}`,
887
+ ...options.headers
888
+ });
889
+ const createLanguageModel = (modelId, settings = {}) => {
890
+ const structuredOutputs = supportsStructuredOutputs(modelId);
891
+ return new OpenAICompatibleChatLanguageModel(modelId, settings, {
892
+ provider: "xai.chat",
893
+ url: ({ path }) => `${baseURL}${path}`,
894
+ headers: getHeaders,
895
+ fetch: options.fetch,
896
+ defaultObjectGenerationMode: structuredOutputs ? "json" : "tool",
897
+ errorStructure: xaiErrorStructure,
898
+ supportsStructuredOutputs: structuredOutputs,
899
+ includeUsage: true
900
+ });
901
+ };
902
+ const createImageModel = (modelId, settings = {}) => {
903
+ return new OpenAICompatibleImageModel(modelId, settings, {
904
+ provider: "xai.image",
905
+ url: ({ path }) => `${baseURL}${path}`,
906
+ headers: getHeaders,
907
+ fetch: options.fetch,
908
+ errorStructure: xaiErrorStructure
909
+ });
910
+ };
911
+ const provider = (modelId, settings) => createLanguageModel(modelId, settings);
912
+ provider.languageModel = createLanguageModel;
913
+ provider.chat = createLanguageModel;
914
+ provider.textEmbeddingModel = (modelId) => {
915
+ throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
916
+ };
917
+ provider.imageModel = createImageModel;
918
+ provider.image = createImageModel;
919
+ return provider;
920
+ }
921
+ var xai = createXai();
922
+
923
+ export { createXai, xai };
924
+ //# sourceMappingURL=dist-R7WYX6LC.js.map
925
+ //# sourceMappingURL=dist-R7WYX6LC.js.map