@providerprotocol/ai 0.0.34 → 0.0.35

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 (133) hide show
  1. package/README.md +542 -3
  2. package/dist/anthropic/index.d.ts +2 -1
  3. package/dist/anthropic/index.js +151 -145
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/cerebras/index.d.ts +392 -0
  6. package/dist/cerebras/index.js +648 -0
  7. package/dist/cerebras/index.js.map +1 -0
  8. package/dist/chunk-3GWM5GR3.js +153 -0
  9. package/dist/chunk-3GWM5GR3.js.map +1 -0
  10. package/dist/chunk-4OGB7JZA.js +157 -0
  11. package/dist/chunk-4OGB7JZA.js.map +1 -0
  12. package/dist/chunk-7DXVRILR.js +49 -0
  13. package/dist/chunk-7DXVRILR.js.map +1 -0
  14. package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
  15. package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
  16. package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
  17. package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
  18. package/dist/chunk-AY55T37A.js.map +1 -0
  19. package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
  20. package/dist/chunk-BRP5XJ6Q.js.map +1 -0
  21. package/dist/chunk-C4JP64VW.js +298 -0
  22. package/dist/chunk-C4JP64VW.js.map +1 -0
  23. package/dist/chunk-COS4ON4G.js +111 -0
  24. package/dist/chunk-COS4ON4G.js.map +1 -0
  25. package/dist/chunk-ETBFOLQN.js +34 -0
  26. package/dist/chunk-ETBFOLQN.js.map +1 -0
  27. package/dist/chunk-HB4ZIH3T.js +31 -0
  28. package/dist/chunk-HB4ZIH3T.js.map +1 -0
  29. package/dist/chunk-I53CI6ZZ.js +142 -0
  30. package/dist/chunk-I53CI6ZZ.js.map +1 -0
  31. package/dist/chunk-IDZOVWP3.js +29 -0
  32. package/dist/chunk-IDZOVWP3.js.map +1 -0
  33. package/dist/chunk-JA3UZALR.js +88 -0
  34. package/dist/chunk-JA3UZALR.js.map +1 -0
  35. package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
  36. package/dist/chunk-N5DX5JW3.js.map +1 -0
  37. package/dist/chunk-OIEWDFQU.js +97 -0
  38. package/dist/chunk-OIEWDFQU.js.map +1 -0
  39. package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
  40. package/dist/chunk-PMK5LZ5Z.js.map +1 -0
  41. package/dist/chunk-UFFJDYCE.js +94 -0
  42. package/dist/chunk-UFFJDYCE.js.map +1 -0
  43. package/dist/chunk-VGKZIGVI.js +222 -0
  44. package/dist/chunk-VGKZIGVI.js.map +1 -0
  45. package/dist/chunk-VOEWHQUB.js +31 -0
  46. package/dist/chunk-VOEWHQUB.js.map +1 -0
  47. package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
  48. package/dist/chunk-ZI67WIQS.js +30 -0
  49. package/dist/chunk-ZI67WIQS.js.map +1 -0
  50. package/dist/{embedding-D2BYIehX.d.ts → embedding-CW6SaOOz.d.ts} +1 -1
  51. package/dist/google/index.d.ts +2 -1
  52. package/dist/google/index.js +202 -199
  53. package/dist/google/index.js.map +1 -1
  54. package/dist/groq/index.d.ts +410 -0
  55. package/dist/groq/index.js +649 -0
  56. package/dist/groq/index.js.map +1 -0
  57. package/dist/http/index.d.ts +3 -2
  58. package/dist/http/index.js +5 -4
  59. package/dist/image-stream-C0ciACM2.d.ts +11 -0
  60. package/dist/index.d.ts +8 -118
  61. package/dist/index.js +518 -767
  62. package/dist/index.js.map +1 -1
  63. package/dist/{llm-BQJZj3cD.d.ts → llm-DwbUK7un.d.ts} +12 -1632
  64. package/dist/middleware/logging/index.d.ts +76 -0
  65. package/dist/middleware/logging/index.js +74 -0
  66. package/dist/middleware/logging/index.js.map +1 -0
  67. package/dist/middleware/parsed-object/index.d.ts +45 -0
  68. package/dist/middleware/parsed-object/index.js +73 -0
  69. package/dist/middleware/parsed-object/index.js.map +1 -0
  70. package/dist/middleware/pubsub/index.d.ts +104 -0
  71. package/dist/middleware/pubsub/index.js +230 -0
  72. package/dist/middleware/pubsub/index.js.map +1 -0
  73. package/dist/middleware/pubsub/server/express/index.d.ts +52 -0
  74. package/dist/middleware/pubsub/server/express/index.js +11 -0
  75. package/dist/middleware/pubsub/server/express/index.js.map +1 -0
  76. package/dist/middleware/pubsub/server/fastify/index.d.ts +53 -0
  77. package/dist/middleware/pubsub/server/fastify/index.js +11 -0
  78. package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
  79. package/dist/middleware/pubsub/server/h3/index.d.ts +56 -0
  80. package/dist/middleware/pubsub/server/h3/index.js +11 -0
  81. package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
  82. package/dist/middleware/pubsub/server/index.d.ts +78 -0
  83. package/dist/middleware/pubsub/server/index.js +34 -0
  84. package/dist/middleware/pubsub/server/index.js.map +1 -0
  85. package/dist/middleware/pubsub/server/webapi/index.d.ts +53 -0
  86. package/dist/middleware/pubsub/server/webapi/index.js +11 -0
  87. package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
  88. package/dist/ollama/index.d.ts +2 -1
  89. package/dist/ollama/index.js +48 -45
  90. package/dist/ollama/index.js.map +1 -1
  91. package/dist/openai/index.d.ts +2 -1
  92. package/dist/openai/index.js +319 -313
  93. package/dist/openai/index.js.map +1 -1
  94. package/dist/openrouter/index.d.ts +2 -1
  95. package/dist/openrouter/index.js +379 -383
  96. package/dist/openrouter/index.js.map +1 -1
  97. package/dist/proxy/index.d.ts +10 -914
  98. package/dist/proxy/index.js +275 -1007
  99. package/dist/proxy/index.js.map +1 -1
  100. package/dist/proxy/server/express/index.d.ts +161 -0
  101. package/dist/proxy/server/express/index.js +24 -0
  102. package/dist/proxy/server/express/index.js.map +1 -0
  103. package/dist/proxy/server/fastify/index.d.ts +162 -0
  104. package/dist/proxy/server/fastify/index.js +24 -0
  105. package/dist/proxy/server/fastify/index.js.map +1 -0
  106. package/dist/proxy/server/h3/index.d.ts +189 -0
  107. package/dist/proxy/server/h3/index.js +28 -0
  108. package/dist/proxy/server/h3/index.js.map +1 -0
  109. package/dist/proxy/server/index.d.ts +151 -0
  110. package/dist/proxy/server/index.js +48 -0
  111. package/dist/proxy/server/index.js.map +1 -0
  112. package/dist/proxy/server/webapi/index.d.ts +278 -0
  113. package/dist/proxy/server/webapi/index.js +32 -0
  114. package/dist/proxy/server/webapi/index.js.map +1 -0
  115. package/dist/responses/index.d.ts +650 -0
  116. package/dist/responses/index.js +930 -0
  117. package/dist/responses/index.js.map +1 -0
  118. package/dist/{retry-8Ch-WWgX.d.ts → retry-YayV42GV.d.ts} +1 -1
  119. package/dist/stream-CecfVCPO.d.ts +1632 -0
  120. package/dist/types-C8Gciizr.d.ts +168 -0
  121. package/dist/utils/index.d.ts +53 -0
  122. package/dist/utils/index.js +7 -0
  123. package/dist/utils/index.js.map +1 -0
  124. package/dist/xai/index.d.ts +2 -1
  125. package/dist/xai/index.js +310 -310
  126. package/dist/xai/index.js.map +1 -1
  127. package/package.json +82 -4
  128. package/dist/chunk-4J6OFUKX.js.map +0 -1
  129. package/dist/chunk-ILR2D5PN.js.map +0 -1
  130. package/dist/chunk-TOJCZMVU.js.map +0 -1
  131. package/dist/chunk-WAKD3OO5.js.map +0 -1
  132. /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
  133. /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
@@ -1,27 +1,68 @@
1
+ import {
2
+ server
3
+ } from "../chunk-IDZOVWP3.js";
4
+ import {
5
+ fastify
6
+ } from "../chunk-UFFJDYCE.js";
7
+ import {
8
+ bindTools,
9
+ parseBody,
10
+ parseEmbeddingBody,
11
+ parseImageBody,
12
+ toEmbeddingJSON,
13
+ toError as toError2,
14
+ toImageJSON,
15
+ toImageSSE,
16
+ toJSON,
17
+ toSSE,
18
+ webapi
19
+ } from "../chunk-VGKZIGVI.js";
20
+ import {
21
+ express
22
+ } from "../chunk-OIEWDFQU.js";
23
+ import {
24
+ h3
25
+ } from "../chunk-4OGB7JZA.js";
26
+ import {
27
+ deserializeImageResponse,
28
+ deserializeImageStreamEvent,
29
+ deserializeMessage,
30
+ deserializeStreamEvent,
31
+ serializeEmbeddingInput,
32
+ serializeImage,
33
+ serializeMessage,
34
+ serializeStreamEvent,
35
+ serializeTurn
36
+ } from "../chunk-C4JP64VW.js";
37
+ import "../chunk-ETBFOLQN.js";
1
38
  import {
2
39
  emptyUsage
3
40
  } from "../chunk-NSE7QN3P.js";
4
- import {
5
- Image
6
- } from "../chunk-WAKD3OO5.js";
41
+ import "../chunk-N5DX5JW3.js";
7
42
  import {
8
43
  parseJsonResponse
9
- } from "../chunk-3C7O2RNO.js";
44
+ } from "../chunk-A2IM7PGT.js";
10
45
  import {
11
- AssistantMessage,
12
- ToolResultMessage,
13
- UserMessage,
14
46
  createProvider
15
- } from "../chunk-ILR2D5PN.js";
47
+ } from "../chunk-JA3UZALR.js";
48
+ import {
49
+ AssistantMessage
50
+ } from "../chunk-BRP5XJ6Q.js";
16
51
  import {
17
- ErrorCode,
18
- ModalityType,
19
- UPPError,
20
52
  doFetch,
21
53
  doStreamFetch,
22
54
  normalizeHttpError,
23
55
  toError
24
- } from "../chunk-4J6OFUKX.js";
56
+ } from "../chunk-AY55T37A.js";
57
+ import {
58
+ ErrorCode,
59
+ ModalityType,
60
+ UPPError
61
+ } from "../chunk-COS4ON4G.js";
62
+ import {
63
+ StreamEventType,
64
+ objectDelta
65
+ } from "../chunk-6S222DHN.js";
25
66
 
26
67
  // src/providers/proxy/headers.ts
27
68
  function mergeHeaders(requestHeaders, defaultHeaders) {
@@ -36,97 +77,145 @@ function mergeHeaders(requestHeaders, defaultHeaders) {
36
77
  return headers;
37
78
  }
38
79
 
39
- // src/providers/proxy/serialization.ts
40
- function serializeMessage(m) {
41
- const base = {
42
- id: m.id,
43
- type: m.type,
44
- content: [],
45
- metadata: m.metadata,
46
- timestamp: m.timestamp.toISOString()
80
+ // src/providers/proxy/llm.ts
81
+ var PROXY_CAPABILITIES = {
82
+ streaming: true,
83
+ tools: true,
84
+ structuredOutput: true,
85
+ imageInput: true,
86
+ documentInput: true,
87
+ videoInput: true,
88
+ audioInput: true
89
+ };
90
+ function serializeRequest(request, modelId) {
91
+ return {
92
+ model: modelId,
93
+ messages: request.messages.map(serializeMessage),
94
+ system: request.system,
95
+ params: request.params,
96
+ tools: request.tools?.map((t) => ({
97
+ name: t.name,
98
+ description: t.description,
99
+ parameters: t.parameters,
100
+ metadata: t.metadata
101
+ })),
102
+ structure: request.structure
47
103
  };
48
- if (m instanceof UserMessage) {
49
- base.content = m.content;
50
- } else if (m instanceof AssistantMessage) {
51
- base.content = m.content;
52
- base.toolCalls = m.toolCalls;
53
- } else if (m instanceof ToolResultMessage) {
54
- base.results = m.results;
104
+ }
105
+ function mapCompletionStopReason(reason) {
106
+ switch (reason) {
107
+ case "stop":
108
+ return "end_turn";
109
+ case "length":
110
+ return "max_tokens";
111
+ case "tool_calls":
112
+ return "tool_use";
113
+ case "content_filter":
114
+ return "content_filter";
115
+ default:
116
+ return "end_turn";
55
117
  }
56
- return base;
57
118
  }
58
- function deserializeMessage(json) {
59
- const options = {
60
- id: json.id,
61
- metadata: json.metadata
62
- };
63
- switch (json.type) {
64
- case "user":
65
- return new UserMessage(json.content, options);
66
- case "assistant":
67
- return new AssistantMessage(
68
- json.content,
69
- json.toolCalls,
70
- options
71
- );
72
- case "tool_result":
73
- return new ToolResultMessage(json.results ?? [], options);
119
+ function mapAnthropicStopReason(reason) {
120
+ switch (reason) {
121
+ case "tool_use":
122
+ return "tool_use";
123
+ case "max_tokens":
124
+ return "max_tokens";
125
+ case "end_turn":
126
+ return "end_turn";
127
+ case "stop_sequence":
128
+ return "end_turn";
74
129
  default:
75
- throw new UPPError(
76
- `Unknown message type: ${json.type}`,
77
- ErrorCode.InvalidResponse,
78
- "proxy",
79
- ModalityType.LLM
80
- );
130
+ return "end_turn";
81
131
  }
82
132
  }
83
- function serializeTurn(turn) {
84
- return {
85
- messages: turn.messages.map(serializeMessage),
86
- toolExecutions: turn.toolExecutions,
87
- usage: turn.usage,
88
- cycles: turn.cycles,
89
- data: turn.data
90
- };
133
+ function mapGoogleStopReason(reason) {
134
+ switch (reason) {
135
+ case "STOP":
136
+ return "end_turn";
137
+ case "MAX_TOKENS":
138
+ return "max_tokens";
139
+ case "SAFETY":
140
+ return "content_filter";
141
+ case "RECITATION":
142
+ return "content_filter";
143
+ case "OTHER":
144
+ return "end_turn";
145
+ default:
146
+ return "end_turn";
147
+ }
91
148
  }
92
- function serializeStreamEvent(event) {
93
- const delta = event.delta;
94
- if (delta.data instanceof Uint8Array) {
95
- const { data, ...rest } = delta;
96
- const bytes = Array.from(data);
97
- const base64 = btoa(bytes.map((b) => String.fromCharCode(b)).join(""));
98
- return {
99
- type: event.type,
100
- index: event.index,
101
- delta: { ...rest, data: base64 }
102
- };
149
+ function mapOllamaStopReason(reason) {
150
+ if (reason === "length") {
151
+ return "max_tokens";
103
152
  }
104
- return event;
153
+ if (reason === "stop") {
154
+ return "end_turn";
155
+ }
156
+ return "end_turn";
105
157
  }
106
- function deserializeStreamEvent(event) {
107
- const delta = event.delta;
108
- if (typeof delta.data === "string") {
109
- const binaryString = atob(delta.data);
110
- const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0));
111
- return {
112
- type: event.type,
113
- index: event.index,
114
- delta: { ...delta, data: bytes }
115
- };
158
+ function deriveStopReason(message) {
159
+ if (!message) {
160
+ return "end_turn";
161
+ }
162
+ if (message.toolCalls && message.toolCalls.length > 0) {
163
+ return "tool_use";
164
+ }
165
+ const metadata = message.metadata;
166
+ const openaiMeta = metadata?.openai;
167
+ if (openaiMeta?.status) {
168
+ if (openaiMeta.status === "failed") {
169
+ return "error";
170
+ }
171
+ if (openaiMeta.status === "completed") {
172
+ return "end_turn";
173
+ }
174
+ }
175
+ if (openaiMeta?.finish_reason) {
176
+ return mapCompletionStopReason(openaiMeta.finish_reason);
177
+ }
178
+ const openrouterMeta = metadata?.openrouter;
179
+ if (openrouterMeta?.finish_reason) {
180
+ return mapCompletionStopReason(openrouterMeta.finish_reason);
181
+ }
182
+ const xaiMeta = metadata?.xai;
183
+ if (xaiMeta?.status) {
184
+ if (xaiMeta.status === "failed") {
185
+ return "error";
186
+ }
187
+ if (xaiMeta.status === "completed") {
188
+ return "end_turn";
189
+ }
190
+ }
191
+ if (xaiMeta?.finish_reason) {
192
+ return mapCompletionStopReason(xaiMeta.finish_reason);
193
+ }
194
+ const anthropicMeta = metadata?.anthropic;
195
+ if (anthropicMeta?.stop_reason) {
196
+ return mapAnthropicStopReason(anthropicMeta.stop_reason);
197
+ }
198
+ const googleMeta = metadata?.google;
199
+ if (googleMeta?.finishReason) {
200
+ return mapGoogleStopReason(googleMeta.finishReason);
116
201
  }
117
- return event;
202
+ const ollamaMeta = metadata?.ollama;
203
+ if (ollamaMeta?.done_reason) {
204
+ return mapOllamaStopReason(ollamaMeta.done_reason);
205
+ }
206
+ return "end_turn";
207
+ }
208
+ function turnJSONToLLMResponse(data) {
209
+ const messages = data.messages.map(deserializeMessage);
210
+ const lastAssistant = messages.filter((m) => m.type === "assistant").pop();
211
+ const stopReason = deriveStopReason(lastAssistant);
212
+ return {
213
+ message: lastAssistant ?? new AssistantMessage(""),
214
+ usage: data.usage ?? emptyUsage(),
215
+ stopReason,
216
+ data: data.data
217
+ };
118
218
  }
119
-
120
- // src/providers/proxy/llm.ts
121
- var PROXY_CAPABILITIES = {
122
- streaming: true,
123
- tools: true,
124
- structuredOutput: true,
125
- imageInput: true,
126
- documentInput: true,
127
- videoInput: true,
128
- audioInput: true
129
- };
130
219
  function createLLMHandler(options) {
131
220
  const { endpoint, headers: defaultHeaders = {} } = options;
132
221
  let providerRef = null;
@@ -245,6 +334,12 @@ function createLLMHandler(options) {
245
334
  } else {
246
335
  const event = deserializeStreamEvent(parsed);
247
336
  yield event;
337
+ if (request.structure && event.type === StreamEventType.TextDelta) {
338
+ yield objectDelta(event.delta.text ?? "", event.index);
339
+ }
340
+ if (request.structure && event.type === StreamEventType.ToolCallDelta && event.delta.argumentsJson) {
341
+ yield objectDelta(event.delta.argumentsJson, event.index);
342
+ }
248
343
  }
249
344
  } catch {
250
345
  }
@@ -271,6 +366,12 @@ function createLLMHandler(options) {
271
366
  } else {
272
367
  const event = deserializeStreamEvent(parsed);
273
368
  yield event;
369
+ if (request.structure && event.type === StreamEventType.TextDelta) {
370
+ yield objectDelta(event.delta.text ?? "", event.index);
371
+ }
372
+ if (request.structure && event.type === StreamEventType.ToolCallDelta && event.delta.argumentsJson) {
373
+ yield objectDelta(event.delta.argumentsJson, event.index);
374
+ }
274
375
  }
275
376
  } catch {
276
377
  }
@@ -300,396 +401,11 @@ function createLLMHandler(options) {
300
401
  }
301
402
  };
302
403
  }
303
- function serializeRequest(request, modelId) {
304
- return {
305
- model: modelId,
306
- messages: request.messages.map(serializeMessage),
307
- system: request.system,
308
- params: request.params,
309
- tools: request.tools?.map((t) => ({
310
- name: t.name,
311
- description: t.description,
312
- parameters: t.parameters,
313
- metadata: t.metadata
314
- })),
315
- structure: request.structure
316
- };
317
- }
318
- function turnJSONToLLMResponse(data) {
319
- const messages = data.messages.map(deserializeMessage);
320
- const lastAssistant = messages.filter((m) => m.type === "assistant").pop();
321
- const stopReason = deriveStopReason(lastAssistant);
322
- return {
323
- message: lastAssistant ?? new AssistantMessage(""),
324
- usage: data.usage ?? emptyUsage(),
325
- stopReason,
326
- data: data.data
327
- };
328
- }
329
- function deriveStopReason(message) {
330
- if (!message) {
331
- return "end_turn";
332
- }
333
- if (message.toolCalls && message.toolCalls.length > 0) {
334
- return "tool_use";
335
- }
336
- const metadata = message.metadata;
337
- const openaiMeta = metadata?.openai;
338
- if (openaiMeta?.status) {
339
- if (openaiMeta.status === "failed") {
340
- return "error";
341
- }
342
- if (openaiMeta.status === "completed") {
343
- return "end_turn";
344
- }
345
- }
346
- if (openaiMeta?.finish_reason) {
347
- return mapCompletionStopReason(openaiMeta.finish_reason);
348
- }
349
- const openrouterMeta = metadata?.openrouter;
350
- if (openrouterMeta?.finish_reason) {
351
- return mapCompletionStopReason(openrouterMeta.finish_reason);
352
- }
353
- const xaiMeta = metadata?.xai;
354
- if (xaiMeta?.status) {
355
- if (xaiMeta.status === "failed") {
356
- return "error";
357
- }
358
- if (xaiMeta.status === "completed") {
359
- return "end_turn";
360
- }
361
- }
362
- if (xaiMeta?.finish_reason) {
363
- return mapCompletionStopReason(xaiMeta.finish_reason);
364
- }
365
- const anthropicMeta = metadata?.anthropic;
366
- if (anthropicMeta?.stop_reason) {
367
- return mapAnthropicStopReason(anthropicMeta.stop_reason);
368
- }
369
- const googleMeta = metadata?.google;
370
- if (googleMeta?.finishReason) {
371
- return mapGoogleStopReason(googleMeta.finishReason);
372
- }
373
- const ollamaMeta = metadata?.ollama;
374
- if (ollamaMeta?.done_reason) {
375
- return mapOllamaStopReason(ollamaMeta.done_reason);
376
- }
377
- return "end_turn";
378
- }
379
- function mapCompletionStopReason(reason) {
380
- switch (reason) {
381
- case "stop":
382
- return "end_turn";
383
- case "length":
384
- return "max_tokens";
385
- case "tool_calls":
386
- return "tool_use";
387
- case "content_filter":
388
- return "content_filter";
389
- default:
390
- return "end_turn";
391
- }
392
- }
393
- function mapAnthropicStopReason(reason) {
394
- switch (reason) {
395
- case "tool_use":
396
- return "tool_use";
397
- case "max_tokens":
398
- return "max_tokens";
399
- case "end_turn":
400
- return "end_turn";
401
- case "stop_sequence":
402
- return "end_turn";
403
- default:
404
- return "end_turn";
405
- }
406
- }
407
- function mapGoogleStopReason(reason) {
408
- switch (reason) {
409
- case "STOP":
410
- return "end_turn";
411
- case "MAX_TOKENS":
412
- return "max_tokens";
413
- case "SAFETY":
414
- return "content_filter";
415
- case "RECITATION":
416
- return "content_filter";
417
- case "OTHER":
418
- return "end_turn";
419
- default:
420
- return "end_turn";
421
- }
422
- }
423
- function mapOllamaStopReason(reason) {
424
- if (reason === "length") {
425
- return "max_tokens";
426
- }
427
- if (reason === "stop") {
428
- return "end_turn";
429
- }
430
- return "end_turn";
431
- }
432
-
433
- // src/providers/proxy/serialization.media.ts
434
- function serializeEmbeddingInput(input) {
435
- if (typeof input === "string") {
436
- return input;
437
- }
438
- if (input.type === "text") {
439
- return { type: "text", text: input.text };
440
- }
441
- if (input.type === "image") {
442
- const source = serializeUnknownImageSource(input.source, input.mimeType);
443
- return { type: "image", source, mimeType: input.mimeType };
444
- }
445
- throw new UPPError(
446
- "Unsupported embedding input type",
447
- ErrorCode.InvalidRequest,
448
- "proxy",
449
- ModalityType.Embedding
450
- );
451
- }
452
- function deserializeEmbeddingInput(input) {
453
- if (typeof input === "string") {
454
- return input;
455
- }
456
- if (input.type === "text") {
457
- return { type: "text", text: input.text };
458
- }
459
- if (input.type === "image") {
460
- return {
461
- type: "image",
462
- mimeType: input.mimeType,
463
- source: deserializeImageSource(input.source)
464
- };
465
- }
466
- throw new UPPError(
467
- "Unsupported embedding input type",
468
- ErrorCode.InvalidResponse,
469
- "proxy",
470
- ModalityType.Embedding
471
- );
472
- }
473
- function serializeImage(image) {
474
- const block = image.toBlock();
475
- return {
476
- source: serializeImageSource(block.source),
477
- mimeType: block.mimeType,
478
- width: block.width,
479
- height: block.height
480
- };
481
- }
482
- function deserializeImage(image) {
483
- const block = {
484
- type: "image",
485
- source: deserializeImageSource(image.source),
486
- mimeType: image.mimeType,
487
- width: image.width,
488
- height: image.height
489
- };
490
- return Image.fromBlock(block);
491
- }
492
- function serializeGeneratedImage(image) {
493
- return {
494
- image: serializeImage(image.image),
495
- metadata: image.metadata
496
- };
497
- }
498
- function deserializeGeneratedImage(image) {
499
- return {
500
- image: deserializeImage(image.image),
501
- metadata: image.metadata
502
- };
503
- }
504
- function serializeImageResult(result) {
505
- return {
506
- images: result.images.map(serializeGeneratedImage),
507
- metadata: result.metadata,
508
- usage: result.usage
509
- };
510
- }
511
- function deserializeImageResponse(response) {
512
- if (!response || typeof response !== "object" || !Array.isArray(response.images)) {
513
- throw new UPPError(
514
- "Invalid image response",
515
- ErrorCode.InvalidResponse,
516
- "proxy",
517
- ModalityType.Image
518
- );
519
- }
520
- return {
521
- images: response.images.map(deserializeGeneratedImage),
522
- metadata: response.metadata,
523
- usage: response.usage
524
- };
525
- }
526
- function serializeImageStreamEvent(event) {
527
- if (event.type === "preview") {
528
- return {
529
- type: "preview",
530
- index: event.index,
531
- image: serializeImage(event.image),
532
- metadata: event.metadata
533
- };
534
- }
535
- return {
536
- type: "complete",
537
- index: event.index,
538
- image: serializeGeneratedImage(event.image)
539
- };
540
- }
541
- function deserializeImageStreamEvent(event) {
542
- if (event.type === "preview") {
543
- return {
544
- type: "preview",
545
- index: event.index,
546
- image: deserializeImage(event.image),
547
- metadata: event.metadata
548
- };
549
- }
550
- return {
551
- type: "complete",
552
- index: event.index,
553
- image: deserializeGeneratedImage(event.image)
554
- };
555
- }
556
- function serializeImageSource(source) {
557
- if (source.type === "base64") {
558
- return { type: "base64", data: source.data };
559
- }
560
- if (source.type === "url") {
561
- return { type: "url", url: source.url };
562
- }
563
- if (typeof source.data === "string") {
564
- return { type: "base64", data: source.data };
565
- }
566
- if (source.data instanceof Uint8Array) {
567
- return { type: "base64", data: bytesToBase64(source.data) };
568
- }
569
- return { type: "base64", data: bytesToBase64(Uint8Array.from(source.data)) };
570
- }
571
- function serializeUnknownImageSource(source, mimeType) {
572
- if (source instanceof Image) {
573
- return serializeImage(source).source;
574
- }
575
- if (isImageSource(source)) {
576
- return serializeImageSource(source);
577
- }
578
- if (typeof source === "string") {
579
- return { type: "base64", data: source };
580
- }
581
- throw new UPPError(
582
- `Unsupported image source for ${mimeType}`,
583
- ErrorCode.InvalidRequest,
584
- "proxy",
585
- ModalityType.Embedding
586
- );
587
- }
588
- function deserializeImageSource(source) {
589
- if (source.type === "base64") {
590
- return { type: "base64", data: source.data };
591
- }
592
- if (source.type === "url") {
593
- return { type: "url", url: source.url };
594
- }
595
- return { type: "bytes", data: coerceBytes(source.data) };
596
- }
597
- function isImageSource(value) {
598
- if (!value || typeof value !== "object") {
599
- return false;
600
- }
601
- const source = value;
602
- if (source.type === "base64") {
603
- return typeof source.data === "string";
604
- }
605
- if (source.type === "url") {
606
- return typeof source.url === "string";
607
- }
608
- if (source.type === "bytes") {
609
- return source.data instanceof Uint8Array || Array.isArray(source.data) || typeof source.data === "string";
610
- }
611
- return false;
612
- }
613
- function coerceBytes(data) {
614
- if (typeof data === "string") {
615
- return base64ToBytes(data);
616
- }
617
- return Uint8Array.from(data);
618
- }
619
- function bytesToBase64(bytes) {
620
- const binary = Array.from(bytes).map((b) => String.fromCharCode(b)).join("");
621
- return btoa(binary);
622
- }
623
- function base64ToBytes(base64) {
624
- const binaryString = atob(base64);
625
- return Uint8Array.from(binaryString, (c) => c.charCodeAt(0));
626
- }
627
404
 
628
405
  // src/providers/proxy/embedding.ts
629
406
  var DEFAULT_MAX_BATCH_SIZE = Number.MAX_SAFE_INTEGER;
630
- var DEFAULT_MAX_INPUT_LENGTH = Number.MAX_SAFE_INTEGER;
631
- var DEFAULT_DIMENSIONS = 0;
632
- function createEmbeddingHandler(options) {
633
- const { endpoint, headers: defaultHeaders = {} } = options;
634
- let providerRef = null;
635
- return {
636
- supportedInputs: ["text", "image"],
637
- _setProvider(provider) {
638
- providerRef = provider;
639
- },
640
- bind(modelId) {
641
- const provider = providerRef;
642
- if (!provider) {
643
- throw new UPPError(
644
- "Provider reference not set. Handler must be used with createProvider().",
645
- ErrorCode.InvalidRequest,
646
- "proxy",
647
- ModalityType.Embedding
648
- );
649
- }
650
- const model = {
651
- modelId,
652
- maxBatchSize: DEFAULT_MAX_BATCH_SIZE,
653
- maxInputLength: DEFAULT_MAX_INPUT_LENGTH,
654
- dimensions: DEFAULT_DIMENSIONS,
655
- get provider() {
656
- return provider;
657
- },
658
- async embed(request) {
659
- const body = {
660
- model: modelId,
661
- inputs: request.inputs.map(serializeEmbeddingInput),
662
- params: request.params
663
- };
664
- const headers = mergeHeaders(request.config.headers, defaultHeaders);
665
- const response = await doFetch(
666
- endpoint,
667
- {
668
- method: "POST",
669
- headers: {
670
- ...headers,
671
- "Content-Type": "application/json",
672
- Accept: "application/json"
673
- },
674
- body: JSON.stringify(body),
675
- signal: request.signal
676
- },
677
- request.config,
678
- "proxy",
679
- "embedding"
680
- );
681
- const data = await parseJsonResponse(
682
- response,
683
- "proxy",
684
- "embedding"
685
- );
686
- return normalizeEmbeddingResponse(data);
687
- }
688
- };
689
- return model;
690
- }
691
- };
692
- }
407
+ var DEFAULT_MAX_INPUT_LENGTH = Number.MAX_SAFE_INTEGER;
408
+ var DEFAULT_DIMENSIONS = 0;
693
409
  function normalizeEmbeddingResponse(data) {
694
410
  if (!data || typeof data !== "object" || !Array.isArray(data.embeddings)) {
695
411
  throw new UPPError(
@@ -733,17 +449,11 @@ function normalizeEmbeddingResponse(data) {
733
449
  metadata: data.metadata
734
450
  };
735
451
  }
736
-
737
- // src/providers/proxy/image.ts
738
- var PROXY_IMAGE_CAPABILITIES = {
739
- generate: true,
740
- streaming: true,
741
- edit: true
742
- };
743
- function createImageHandler(options) {
452
+ function createEmbeddingHandler(options) {
744
453
  const { endpoint, headers: defaultHeaders = {} } = options;
745
454
  let providerRef = null;
746
455
  return {
456
+ supportedInputs: ["text", "image"],
747
457
  _setProvider(provider) {
748
458
  providerRef = provider;
749
459
  },
@@ -754,32 +464,59 @@ function createImageHandler(options) {
754
464
  "Provider reference not set. Handler must be used with createProvider().",
755
465
  ErrorCode.InvalidRequest,
756
466
  "proxy",
757
- ModalityType.Image
467
+ ModalityType.Embedding
758
468
  );
759
469
  }
760
470
  const model = {
761
471
  modelId,
762
- capabilities: PROXY_IMAGE_CAPABILITIES,
472
+ maxBatchSize: DEFAULT_MAX_BATCH_SIZE,
473
+ maxInputLength: DEFAULT_MAX_INPUT_LENGTH,
474
+ dimensions: DEFAULT_DIMENSIONS,
763
475
  get provider() {
764
476
  return provider;
765
477
  },
766
- async generate(request) {
767
- const body = buildImageRequestBody(modelId, request);
768
- return executeImageRequest(endpoint, body, request, defaultHeaders);
769
- },
770
- async edit(request) {
771
- const body = buildImageEditRequestBody(modelId, request);
772
- return executeImageRequest(endpoint, body, request, defaultHeaders);
478
+ async embed(request) {
479
+ const body = {
480
+ model: modelId,
481
+ inputs: request.inputs.map(serializeEmbeddingInput),
482
+ params: request.params
483
+ };
484
+ const headers = mergeHeaders(request.config.headers, defaultHeaders);
485
+ const response = await doFetch(
486
+ endpoint,
487
+ {
488
+ method: "POST",
489
+ headers: {
490
+ ...headers,
491
+ "Content-Type": "application/json",
492
+ Accept: "application/json"
493
+ },
494
+ body: JSON.stringify(body),
495
+ signal: request.signal
496
+ },
497
+ request.config,
498
+ "proxy",
499
+ "embedding"
500
+ );
501
+ const data = await parseJsonResponse(
502
+ response,
503
+ "proxy",
504
+ "embedding"
505
+ );
506
+ return normalizeEmbeddingResponse(data);
773
507
  }
774
508
  };
775
- model.stream = function stream(request) {
776
- const body = buildImageRequestBody(modelId, request);
777
- return executeImageStream(endpoint, body, request, defaultHeaders);
778
- };
779
509
  return model;
780
510
  }
781
511
  };
782
512
  }
513
+
514
+ // src/providers/proxy/image.ts
515
+ var PROXY_IMAGE_CAPABILITIES = {
516
+ generate: true,
517
+ streaming: true,
518
+ edit: true
519
+ };
783
520
  function buildImageRequestBody(modelId, request) {
784
521
  return {
785
522
  model: modelId,
@@ -796,6 +533,9 @@ function buildImageEditRequestBody(modelId, request) {
796
533
  mask: request.mask ? serializeImage(request.mask) : void 0
797
534
  };
798
535
  }
536
+ function isImageResponsePayload(payload) {
537
+ return !!payload && typeof payload === "object" && "images" in payload && Array.isArray(payload.images);
538
+ }
799
539
  async function executeImageRequest(endpoint, body, request, defaultHeaders) {
800
540
  const headers = mergeHeaders(request.config.headers, defaultHeaders);
801
541
  const response = await doFetch(
@@ -941,518 +681,46 @@ function executeImageStream(endpoint, body, request, defaultHeaders) {
941
681
  response: responsePromise
942
682
  };
943
683
  }
944
- function isImageResponsePayload(payload) {
945
- return !!payload && typeof payload === "object" && "images" in payload && Array.isArray(payload.images);
946
- }
947
-
948
- // src/providers/proxy/server/image-stream.ts
949
- function resolveImageResult(stream) {
950
- if ("result" in stream) {
951
- return stream.result;
952
- }
953
- return stream.response;
954
- }
955
-
956
- // src/providers/proxy/server/express.ts
957
- function sendJSON(turn, res) {
958
- res.setHeader("Content-Type", "application/json");
959
- res.json(serializeTurn(turn));
960
- }
961
- function sendEmbeddingJSON(result, res) {
962
- res.setHeader("Content-Type", "application/json");
963
- res.json(result);
964
- }
965
- function sendImageJSON(result, res) {
966
- res.setHeader("Content-Type", "application/json");
967
- res.json(serializeImageResult(result));
968
- }
969
- function streamSSE(stream, res) {
970
- res.setHeader("Content-Type", "text/event-stream");
971
- res.setHeader("Cache-Control", "no-cache");
972
- res.setHeader("Connection", "keep-alive");
973
- (async () => {
974
- try {
975
- for await (const event of stream) {
976
- const serialized = serializeStreamEvent(event);
977
- res.write(`data: ${JSON.stringify(serialized)}
978
-
979
- `);
980
- }
981
- const turn = await stream.turn;
982
- res.write(`data: ${JSON.stringify(serializeTurn(turn))}
983
-
984
- `);
985
- res.write("data: [DONE]\n\n");
986
- } catch (error) {
987
- const message = error instanceof Error ? error.message : String(error);
988
- res.write(`data: ${JSON.stringify({ error: message })}
989
-
990
- `);
991
- } finally {
992
- res.end();
993
- }
994
- })();
995
- }
996
- function streamImageSSE(stream, res) {
997
- res.setHeader("Content-Type", "text/event-stream");
998
- res.setHeader("Cache-Control", "no-cache");
999
- res.setHeader("Connection", "keep-alive");
1000
- (async () => {
1001
- try {
1002
- for await (const event of stream) {
1003
- const serialized = serializeImageStreamEvent(event);
1004
- res.write(`data: ${JSON.stringify(serialized)}
1005
-
1006
- `);
1007
- }
1008
- const result = await resolveImageResult(stream);
1009
- res.write(`data: ${JSON.stringify(serializeImageResult(result))}
1010
-
1011
- `);
1012
- res.write("data: [DONE]\n\n");
1013
- } catch (error) {
1014
- const message = error instanceof Error ? error.message : String(error);
1015
- res.write(`data: ${JSON.stringify({ error: message })}
1016
-
1017
- `);
1018
- } finally {
1019
- res.end();
1020
- }
1021
- })();
1022
- }
1023
- function sendError(message, status, res) {
1024
- res.status(status).json({ error: message });
1025
- }
1026
- var express = {
1027
- sendJSON,
1028
- sendEmbeddingJSON,
1029
- sendImageJSON,
1030
- streamSSE,
1031
- streamImageSSE,
1032
- sendError
1033
- };
1034
-
1035
- // src/providers/proxy/server/fastify.ts
1036
- function sendJSON2(turn, reply) {
1037
- return reply.header("Content-Type", "application/json").send(serializeTurn(turn));
1038
- }
1039
- function sendEmbeddingJSON2(result, reply) {
1040
- return reply.header("Content-Type", "application/json").send(result);
1041
- }
1042
- function sendImageJSON2(result, reply) {
1043
- return reply.header("Content-Type", "application/json").send(serializeImageResult(result));
1044
- }
1045
- function streamSSE2(stream, reply) {
1046
- reply.header("Content-Type", "text/event-stream").header("Cache-Control", "no-cache").header("Connection", "keep-alive");
1047
- const raw = reply.raw;
1048
- (async () => {
1049
- try {
1050
- for await (const event of stream) {
1051
- const serialized = serializeStreamEvent(event);
1052
- raw.write(`data: ${JSON.stringify(serialized)}
1053
-
1054
- `);
1055
- }
1056
- const turn = await stream.turn;
1057
- raw.write(`data: ${JSON.stringify(serializeTurn(turn))}
1058
-
1059
- `);
1060
- raw.write("data: [DONE]\n\n");
1061
- } catch (error) {
1062
- const message = error instanceof Error ? error.message : String(error);
1063
- raw.write(`data: ${JSON.stringify({ error: message })}
1064
-
1065
- `);
1066
- } finally {
1067
- raw.end();
1068
- }
1069
- })();
1070
- return reply;
1071
- }
1072
- function streamImageSSE2(stream, reply) {
1073
- reply.header("Content-Type", "text/event-stream").header("Cache-Control", "no-cache").header("Connection", "keep-alive");
1074
- const raw = reply.raw;
1075
- (async () => {
1076
- try {
1077
- for await (const event of stream) {
1078
- const serialized = serializeImageStreamEvent(event);
1079
- raw.write(`data: ${JSON.stringify(serialized)}
1080
-
1081
- `);
1082
- }
1083
- const result = await resolveImageResult(stream);
1084
- raw.write(`data: ${JSON.stringify(serializeImageResult(result))}
1085
-
1086
- `);
1087
- raw.write("data: [DONE]\n\n");
1088
- } catch (error) {
1089
- const message = error instanceof Error ? error.message : String(error);
1090
- raw.write(`data: ${JSON.stringify({ error: message })}
1091
-
1092
- `);
1093
- } finally {
1094
- raw.end();
1095
- }
1096
- })();
1097
- return reply;
1098
- }
1099
- function sendError2(message, status, reply) {
1100
- return reply.status(status).send({ error: message });
1101
- }
1102
- var fastify = {
1103
- sendJSON: sendJSON2,
1104
- sendEmbeddingJSON: sendEmbeddingJSON2,
1105
- sendImageJSON: sendImageJSON2,
1106
- streamSSE: streamSSE2,
1107
- streamImageSSE: streamImageSSE2,
1108
- sendError: sendError2
1109
- };
1110
-
1111
- // src/providers/proxy/server/h3.ts
1112
- function sendJSON3(turn, event) {
1113
- event.node.res.setHeader("Content-Type", "application/json");
1114
- return serializeTurn(turn);
1115
- }
1116
- function sendEmbeddingJSON3(result, event) {
1117
- event.node.res.setHeader("Content-Type", "application/json");
1118
- return result;
1119
- }
1120
- function sendImageJSON3(result, event) {
1121
- event.node.res.setHeader("Content-Type", "application/json");
1122
- return serializeImageResult(result);
1123
- }
1124
- function streamSSE3(stream, event) {
1125
- const res = event.node.res;
1126
- res.setHeader("Content-Type", "text/event-stream");
1127
- res.setHeader("Cache-Control", "no-cache");
1128
- res.setHeader("Connection", "keep-alive");
1129
- (async () => {
1130
- try {
1131
- for await (const evt of stream) {
1132
- const serialized = serializeStreamEvent(evt);
1133
- res.write(`data: ${JSON.stringify(serialized)}
1134
-
1135
- `);
1136
- }
1137
- const turn = await stream.turn;
1138
- res.write(`data: ${JSON.stringify(serializeTurn(turn))}
1139
-
1140
- `);
1141
- res.write("data: [DONE]\n\n");
1142
- } catch (error) {
1143
- const message = error instanceof Error ? error.message : String(error);
1144
- res.write(`data: ${JSON.stringify({ error: message })}
1145
-
1146
- `);
1147
- } finally {
1148
- res.end();
1149
- }
1150
- })();
1151
- }
1152
- function streamImageSSE3(stream, event) {
1153
- const res = event.node.res;
1154
- res.setHeader("Content-Type", "text/event-stream");
1155
- res.setHeader("Cache-Control", "no-cache");
1156
- res.setHeader("Connection", "keep-alive");
1157
- (async () => {
1158
- try {
1159
- for await (const evt of stream) {
1160
- const serialized = serializeImageStreamEvent(evt);
1161
- res.write(`data: ${JSON.stringify(serialized)}
1162
-
1163
- `);
1164
- }
1165
- const result = await resolveImageResult(stream);
1166
- res.write(`data: ${JSON.stringify(serializeImageResult(result))}
1167
-
1168
- `);
1169
- res.write("data: [DONE]\n\n");
1170
- } catch (error) {
1171
- const message = error instanceof Error ? error.message : String(error);
1172
- res.write(`data: ${JSON.stringify({ error: message })}
1173
-
1174
- `);
1175
- } finally {
1176
- res.end();
1177
- }
1178
- })();
1179
- }
1180
- function createSSEStream(stream) {
1181
- const encoder = new TextEncoder();
1182
- return new ReadableStream({
1183
- async start(controller) {
1184
- try {
1185
- for await (const event of stream) {
1186
- const serialized = serializeStreamEvent(event);
1187
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(serialized)}
1188
-
1189
- `));
1190
- }
1191
- const turn = await stream.turn;
1192
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(serializeTurn(turn))}
1193
-
1194
- `));
1195
- controller.enqueue(encoder.encode("data: [DONE]\n\n"));
1196
- } catch (error) {
1197
- const message = error instanceof Error ? error.message : String(error);
1198
- controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: message })}
1199
-
1200
- `));
1201
- } finally {
1202
- controller.close();
1203
- }
1204
- }
1205
- });
1206
- }
1207
- function createImageSSEStream(stream) {
1208
- const encoder = new TextEncoder();
1209
- return new ReadableStream({
1210
- async start(controller) {
1211
- try {
1212
- for await (const event of stream) {
1213
- const serialized = serializeImageStreamEvent(event);
1214
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(serialized)}
1215
-
1216
- `));
1217
- }
1218
- const result = await resolveImageResult(stream);
1219
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(serializeImageResult(result))}
1220
-
1221
- `));
1222
- controller.enqueue(encoder.encode("data: [DONE]\n\n"));
1223
- } catch (error) {
1224
- const message = error instanceof Error ? error.message : String(error);
1225
- controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: message })}
1226
-
1227
- `));
1228
- } finally {
1229
- controller.close();
1230
- }
1231
- }
1232
- });
1233
- }
1234
- function sendError3(message, status, event) {
1235
- return { error: message, statusCode: status };
1236
- }
1237
- var h3 = {
1238
- sendJSON: sendJSON3,
1239
- sendEmbeddingJSON: sendEmbeddingJSON3,
1240
- sendImageJSON: sendImageJSON3,
1241
- streamSSE: streamSSE3,
1242
- streamImageSSE: streamImageSSE3,
1243
- createSSEStream,
1244
- createImageSSEStream,
1245
- sendError: sendError3
1246
- };
1247
-
1248
- // src/providers/proxy/server/webapi.ts
1249
- function parseBody(body) {
1250
- if (!body || typeof body !== "object") {
1251
- throw new Error("Request body must be an object");
1252
- }
1253
- const data = body;
1254
- if (!Array.isArray(data.messages)) {
1255
- throw new Error("Request body must have a messages array");
1256
- }
1257
- for (const message of data.messages) {
1258
- if (!message || typeof message !== "object") {
1259
- throw new Error("Each message must be an object");
1260
- }
1261
- const msg = message;
1262
- if (typeof msg.id !== "string") {
1263
- throw new Error("Each message must have a string id");
1264
- }
1265
- if (typeof msg.type !== "string") {
1266
- throw new Error("Each message must have a string type");
1267
- }
1268
- if (typeof msg.timestamp !== "string") {
1269
- throw new Error("Each message must have a string timestamp");
1270
- }
1271
- if ((msg.type === "user" || msg.type === "assistant") && !Array.isArray(msg.content)) {
1272
- throw new Error("User and assistant messages must have a content array");
1273
- }
1274
- }
1275
- return {
1276
- messages: data.messages.map(deserializeMessage),
1277
- system: data.system,
1278
- params: data.params,
1279
- model: typeof data.model === "string" ? data.model : void 0,
1280
- tools: data.tools,
1281
- structure: data.structure
1282
- };
1283
- }
1284
- function parseEmbeddingBody(body) {
1285
- if (!body || typeof body !== "object") {
1286
- throw new Error("Request body must be an object");
1287
- }
1288
- const data = body;
1289
- if (!Array.isArray(data.inputs)) {
1290
- throw new Error("Request body must have an inputs array");
1291
- }
1292
- const inputs = data.inputs.map(
1293
- (input) => deserializeEmbeddingInput(input)
1294
- );
1295
- return {
1296
- inputs,
1297
- params: data.params,
1298
- model: typeof data.model === "string" ? data.model : void 0
1299
- };
1300
- }
1301
- function parseImageBody(body) {
1302
- if (!body || typeof body !== "object") {
1303
- throw new Error("Request body must be an object");
1304
- }
1305
- const data = body;
1306
- const promptValue = data.prompt;
1307
- let prompt;
1308
- if (typeof promptValue === "string") {
1309
- prompt = promptValue;
1310
- } else if (promptValue && typeof promptValue === "object") {
1311
- const promptObj = promptValue;
1312
- if (typeof promptObj.prompt === "string") {
1313
- prompt = promptObj.prompt;
1314
- }
1315
- }
1316
- if (!prompt) {
1317
- throw new Error("Request body must have a prompt string");
1318
- }
1319
- const image = data.image ? deserializeImage(data.image) : void 0;
1320
- const mask = data.mask ? deserializeImage(data.mask) : void 0;
684
+ function createImageHandler(options) {
685
+ const { endpoint, headers: defaultHeaders = {} } = options;
686
+ let providerRef = null;
1321
687
  return {
1322
- prompt,
1323
- params: data.params,
1324
- model: typeof data.model === "string" ? data.model : void 0,
1325
- image,
1326
- mask
1327
- };
1328
- }
1329
- function toJSON(turn) {
1330
- return new Response(JSON.stringify(serializeTurn(turn)), {
1331
- headers: { "Content-Type": "application/json" }
1332
- });
1333
- }
1334
- function toEmbeddingJSON(result) {
1335
- return new Response(JSON.stringify(result), {
1336
- headers: { "Content-Type": "application/json" }
1337
- });
1338
- }
1339
- function toImageJSON(result) {
1340
- return new Response(JSON.stringify(serializeImageResult(result)), {
1341
- headers: { "Content-Type": "application/json" }
1342
- });
1343
- }
1344
- function toSSE(stream) {
1345
- const encoder = new TextEncoder();
1346
- const readable = new ReadableStream({
1347
- async start(controller) {
1348
- try {
1349
- for await (const event of stream) {
1350
- const serialized = serializeStreamEvent(event);
1351
- const data = `data: ${JSON.stringify(serialized)}
1352
-
1353
- `;
1354
- controller.enqueue(encoder.encode(data));
1355
- }
1356
- const turn = await stream.turn;
1357
- const turnData = serializeTurn(turn);
1358
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(turnData)}
1359
-
1360
- `));
1361
- controller.enqueue(encoder.encode("data: [DONE]\n\n"));
1362
- controller.close();
1363
- } catch (error) {
1364
- const errorMsg = error instanceof Error ? error.message : String(error);
1365
- controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: errorMsg })}
1366
-
1367
- `));
1368
- controller.close();
688
+ _setProvider(provider) {
689
+ providerRef = provider;
690
+ },
691
+ bind(modelId) {
692
+ const provider = providerRef;
693
+ if (!provider) {
694
+ throw new UPPError(
695
+ "Provider reference not set. Handler must be used with createProvider().",
696
+ ErrorCode.InvalidRequest,
697
+ "proxy",
698
+ ModalityType.Image
699
+ );
1369
700
  }
1370
- }
1371
- });
1372
- return new Response(readable, {
1373
- headers: {
1374
- "Content-Type": "text/event-stream",
1375
- "Cache-Control": "no-cache",
1376
- Connection: "keep-alive"
1377
- }
1378
- });
1379
- }
1380
- function toImageSSE(stream) {
1381
- const encoder = new TextEncoder();
1382
- const readable = new ReadableStream({
1383
- async start(controller) {
1384
- try {
1385
- for await (const event of stream) {
1386
- const serialized = serializeImageStreamEvent(event);
1387
- const data = `data: ${JSON.stringify(serialized)}
1388
-
1389
- `;
1390
- controller.enqueue(encoder.encode(data));
701
+ const model = {
702
+ modelId,
703
+ capabilities: PROXY_IMAGE_CAPABILITIES,
704
+ get provider() {
705
+ return provider;
706
+ },
707
+ async generate(request) {
708
+ const body = buildImageRequestBody(modelId, request);
709
+ return executeImageRequest(endpoint, body, request, defaultHeaders);
710
+ },
711
+ async edit(request) {
712
+ const body = buildImageEditRequestBody(modelId, request);
713
+ return executeImageRequest(endpoint, body, request, defaultHeaders);
1391
714
  }
1392
- const result = await resolveImageResult(stream);
1393
- const resultData = serializeImageResult(result);
1394
- controller.enqueue(encoder.encode(`data: ${JSON.stringify(resultData)}
1395
-
1396
- `));
1397
- controller.enqueue(encoder.encode("data: [DONE]\n\n"));
1398
- controller.close();
1399
- } catch (error) {
1400
- const errorMsg = error instanceof Error ? error.message : String(error);
1401
- controller.enqueue(encoder.encode(`data: ${JSON.stringify({ error: errorMsg })}
1402
-
1403
- `));
1404
- controller.close();
1405
- }
1406
- }
1407
- });
1408
- return new Response(readable, {
1409
- headers: {
1410
- "Content-Type": "text/event-stream",
1411
- "Cache-Control": "no-cache",
1412
- Connection: "keep-alive"
1413
- }
1414
- });
1415
- }
1416
- function toError2(message, status = 500) {
1417
- return new Response(JSON.stringify({ error: message }), {
1418
- status,
1419
- headers: { "Content-Type": "application/json" }
1420
- });
1421
- }
1422
- function bindTools(schemas, implementations) {
1423
- if (!schemas) return [];
1424
- return schemas.map((schema) => {
1425
- const run = implementations[schema.name];
1426
- if (!run) {
1427
- throw new Error(`No implementation for tool: ${schema.name}`);
715
+ };
716
+ model.stream = function stream(request) {
717
+ const body = buildImageRequestBody(modelId, request);
718
+ return executeImageStream(endpoint, body, request, defaultHeaders);
719
+ };
720
+ return model;
1428
721
  }
1429
- return { ...schema, run };
1430
- });
722
+ };
1431
723
  }
1432
- var webapi = {
1433
- parseBody,
1434
- parseEmbeddingBody,
1435
- parseImageBody,
1436
- toJSON,
1437
- toEmbeddingJSON,
1438
- toImageJSON,
1439
- toSSE,
1440
- toImageSSE,
1441
- toError: toError2,
1442
- bindTools
1443
- };
1444
-
1445
- // src/providers/proxy/server/index.ts
1446
- var server = {
1447
- /** Web API adapter (Bun, Deno, Next.js, Workers) */
1448
- webapi,
1449
- /** Express/Connect adapter */
1450
- express,
1451
- /** Fastify adapter */
1452
- fastify,
1453
- /** H3/Nitro/Nuxt adapter */
1454
- h3
1455
- };
1456
724
 
1457
725
  // src/providers/proxy/index.ts
1458
726
  function proxy(options) {