@ai-sdk/huggingface 2.0.58 → 2.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,1026 +1,835 @@
1
- // src/huggingface-provider.ts
2
- import {
3
- NoSuchModelError
4
- } from "@ai-sdk/provider";
5
- import {
6
- generateId as generateId2,
7
- loadApiKey,
8
- withoutTrailingSlash
9
- } from "@ai-sdk/provider-utils";
10
-
11
- // src/responses/huggingface-responses-language-model.ts
12
- import {
13
- APICallError
14
- } from "@ai-sdk/provider";
15
- import {
16
- combineHeaders,
17
- createEventSourceResponseHandler,
18
- createJsonResponseHandler,
19
- createProviderStreamError,
20
- generateId,
21
- parseProviderOptions,
22
- postJsonToApi,
23
- serializeModelOptions,
24
- WORKFLOW_SERIALIZE,
25
- WORKFLOW_DESERIALIZE
26
- } from "@ai-sdk/provider-utils";
27
- import { z as z3 } from "zod/v4";
28
-
29
- // src/huggingface-error.ts
30
- import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
1
+ import { APICallError, NoSuchModelError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
2
+ import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createNullLanguageModelUsage, createProviderStreamError, generateId, getTopLevelMediaType, loadApiKey, parseProviderOptions, postJsonToApi, resolveFullMediaType, serializeModelOptions, withoutTrailingSlash } from "@ai-sdk/provider-utils";
31
3
  import { z } from "zod/v4";
32
- var huggingfaceErrorDataSchema = z.object({
33
- error: z.object({
34
- message: z.string(),
35
- type: z.string().optional(),
36
- code: z.string().optional()
37
- })
38
- });
39
- var huggingfaceFailedResponseHandler = createJsonErrorResponseHandler({
40
- errorSchema: huggingfaceErrorDataSchema,
41
- errorToMessage: (data) => data.error.message
4
+ //#region src/huggingface-error.ts
5
+ const huggingfaceErrorDataSchema = z.object({ error: z.object({
6
+ message: z.string(),
7
+ type: z.string().optional(),
8
+ code: z.string().optional()
9
+ }) });
10
+ const huggingfaceFailedResponseHandler = createJsonErrorResponseHandler({
11
+ errorSchema: huggingfaceErrorDataSchema,
12
+ errorToMessage: (data) => data.error.message
42
13
  });
43
-
44
- // src/responses/convert-huggingface-responses-usage.ts
45
- import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
14
+ //#endregion
15
+ //#region src/responses/convert-huggingface-responses-usage.ts
46
16
  function convertHuggingFaceResponsesUsage(usage) {
47
- if (usage == null) {
48
- return createNullLanguageModelUsage();
49
- }
50
- const inputTokens = usage.input_tokens;
51
- const outputTokens = usage.output_tokens;
52
- const cachedTokens = usage.input_tokens_details?.cached_tokens ?? 0;
53
- const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0;
54
- return {
55
- inputTokens: {
56
- total: inputTokens,
57
- noCache: inputTokens - cachedTokens,
58
- cacheRead: cachedTokens,
59
- cacheWrite: void 0
60
- },
61
- outputTokens: {
62
- total: outputTokens,
63
- text: outputTokens - reasoningTokens,
64
- reasoning: reasoningTokens
65
- },
66
- raw: usage
67
- };
17
+ if (usage == null) return createNullLanguageModelUsage();
18
+ const inputTokens = usage.input_tokens;
19
+ const outputTokens = usage.output_tokens;
20
+ const cachedTokens = usage.input_tokens_details?.cached_tokens ?? 0;
21
+ const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0;
22
+ return {
23
+ inputTokens: {
24
+ total: inputTokens,
25
+ noCache: inputTokens - cachedTokens,
26
+ cacheRead: cachedTokens,
27
+ cacheWrite: void 0
28
+ },
29
+ outputTokens: {
30
+ total: outputTokens,
31
+ text: outputTokens - reasoningTokens,
32
+ reasoning: reasoningTokens
33
+ },
34
+ raw: usage
35
+ };
68
36
  }
69
-
70
- // src/responses/convert-to-huggingface-responses-messages.ts
71
- import {
72
- UnsupportedFunctionalityError
73
- } from "@ai-sdk/provider";
74
- import {
75
- getTopLevelMediaType,
76
- resolveFullMediaType
77
- } from "@ai-sdk/provider-utils";
78
- async function convertToHuggingFaceResponsesMessages({
79
- prompt
80
- }) {
81
- const messages = [];
82
- const warnings = [];
83
- for (const { role, content } of prompt) {
84
- switch (role) {
85
- case "system": {
86
- messages.push({ role: "system", content });
87
- break;
88
- }
89
- case "user": {
90
- messages.push({
91
- role: "user",
92
- content: content.map((part) => {
93
- switch (part.type) {
94
- case "text": {
95
- return { type: "input_text", text: part.text };
96
- }
97
- case "file": {
98
- switch (part.data.type) {
99
- case "reference": {
100
- throw new UnsupportedFunctionalityError({
101
- functionality: "file parts with provider references"
102
- });
103
- }
104
- case "text": {
105
- throw new UnsupportedFunctionalityError({
106
- functionality: "text file parts"
107
- });
108
- }
109
- case "url":
110
- case "data": {
111
- if (getTopLevelMediaType(part.mediaType) === "image") {
112
- return {
113
- type: "input_image",
114
- image_url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${part.data.data}`
115
- };
116
- } else {
117
- throw new UnsupportedFunctionalityError({
118
- functionality: `file part media type ${part.mediaType}`
119
- });
120
- }
121
- }
122
- }
123
- }
124
- default: {
125
- const _exhaustiveCheck = part;
126
- throw new Error(`Unsupported part type: ${_exhaustiveCheck}`);
127
- }
128
- }
129
- })
130
- });
131
- break;
132
- }
133
- case "assistant": {
134
- for (const part of content) {
135
- switch (part.type) {
136
- case "text": {
137
- messages.push({
138
- role: "assistant",
139
- content: [{ type: "output_text", text: part.text }]
140
- });
141
- break;
142
- }
143
- case "tool-call": {
144
- break;
145
- }
146
- case "tool-result": {
147
- break;
148
- }
149
- case "reasoning": {
150
- messages.push({
151
- role: "assistant",
152
- content: [{ type: "output_text", text: part.text }]
153
- });
154
- break;
155
- }
156
- }
157
- }
158
- break;
159
- }
160
- case "tool": {
161
- warnings.push({ type: "unsupported", feature: "tool messages" });
162
- break;
163
- }
164
- default: {
165
- const _exhaustiveCheck = role;
166
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
167
- }
168
- }
169
- }
170
- return { input: messages, warnings };
37
+ //#endregion
38
+ //#region src/responses/convert-to-huggingface-responses-messages.ts
39
+ async function convertToHuggingFaceResponsesMessages({ prompt }) {
40
+ const messages = [];
41
+ const warnings = [];
42
+ for (const { role, content } of prompt) switch (role) {
43
+ case "system":
44
+ messages.push({
45
+ role: "system",
46
+ content
47
+ });
48
+ break;
49
+ case "user":
50
+ messages.push({
51
+ role: "user",
52
+ content: content.map((part) => {
53
+ switch (part.type) {
54
+ case "text": return {
55
+ type: "input_text",
56
+ text: part.text
57
+ };
58
+ case "file": switch (part.data.type) {
59
+ case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
60
+ case "text": throw new UnsupportedFunctionalityError({ functionality: "text file parts" });
61
+ case "url":
62
+ case "data": if (getTopLevelMediaType(part.mediaType) === "image") return {
63
+ type: "input_image",
64
+ image_url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${part.data.data}`
65
+ };
66
+ else throw new UnsupportedFunctionalityError({ functionality: `file part media type ${part.mediaType}` });
67
+ }
68
+ default: throw new Error(`Unsupported part type: ${part}`);
69
+ }
70
+ })
71
+ });
72
+ break;
73
+ case "assistant":
74
+ for (const part of content) switch (part.type) {
75
+ case "text":
76
+ messages.push({
77
+ role: "assistant",
78
+ content: [{
79
+ type: "output_text",
80
+ text: part.text
81
+ }]
82
+ });
83
+ break;
84
+ case "tool-call": break;
85
+ case "tool-result": break;
86
+ case "reasoning": messages.push({
87
+ role: "assistant",
88
+ content: [{
89
+ type: "output_text",
90
+ text: part.text
91
+ }]
92
+ });
93
+ }
94
+ break;
95
+ case "tool":
96
+ warnings.push({
97
+ type: "unsupported",
98
+ feature: "tool messages"
99
+ });
100
+ break;
101
+ default: throw new Error(`Unsupported role: ${role}`);
102
+ }
103
+ return {
104
+ input: messages,
105
+ warnings
106
+ };
171
107
  }
172
-
173
- // src/responses/huggingface-responses-language-model-options.ts
174
- import { z as z2 } from "zod/v4";
175
- var huggingfaceLanguageModelResponsesOptions = z2.object({
176
- metadata: z2.record(z2.string(), z2.string()).optional(),
177
- instructions: z2.string().optional(),
178
- strictJsonSchema: z2.boolean().optional(),
179
- reasoningEffort: z2.string().optional()
108
+ //#endregion
109
+ //#region src/responses/huggingface-responses-language-model-options.ts
110
+ const huggingfaceLanguageModelResponsesOptions = z.object({
111
+ metadata: z.record(z.string(), z.string()).optional(),
112
+ instructions: z.string().optional(),
113
+ strictJsonSchema: z.boolean().optional(),
114
+ reasoningEffort: z.string().optional()
180
115
  });
181
-
182
- // src/responses/huggingface-responses-prepare-tools.ts
183
- function prepareResponsesTools({
184
- tools,
185
- toolChoice
186
- }) {
187
- tools = tools?.length ? tools : void 0;
188
- const toolWarnings = [];
189
- if (tools == null) {
190
- return { tools: void 0, toolChoice: void 0, toolWarnings };
191
- }
192
- const huggingfaceTools = [];
193
- for (const tool of tools) {
194
- switch (tool.type) {
195
- case "function":
196
- huggingfaceTools.push({
197
- type: "function",
198
- name: tool.name,
199
- description: tool.description,
200
- parameters: tool.inputSchema
201
- });
202
- break;
203
- case "provider":
204
- toolWarnings.push({
205
- type: "unsupported",
206
- feature: `provider-defined tool ${tool.id}`
207
- });
208
- break;
209
- default: {
210
- const _exhaustiveCheck = tool;
211
- throw new Error(`Unsupported tool type: ${_exhaustiveCheck}`);
212
- }
213
- }
214
- }
215
- let mappedToolChoice = void 0;
216
- if (toolChoice) {
217
- switch (toolChoice.type) {
218
- case "auto":
219
- mappedToolChoice = "auto";
220
- break;
221
- case "required":
222
- mappedToolChoice = "required";
223
- break;
224
- case "none":
225
- break;
226
- case "tool":
227
- mappedToolChoice = {
228
- type: "function",
229
- function: { name: toolChoice.toolName }
230
- };
231
- break;
232
- default: {
233
- const _exhaustiveCheck = toolChoice;
234
- throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
235
- }
236
- }
237
- }
238
- return {
239
- tools: huggingfaceTools,
240
- toolChoice: mappedToolChoice,
241
- toolWarnings
242
- };
116
+ //#endregion
117
+ //#region src/responses/huggingface-responses-prepare-tools.ts
118
+ function prepareResponsesTools({ tools, toolChoice }) {
119
+ tools = tools?.length ? tools : void 0;
120
+ const toolWarnings = [];
121
+ if (tools == null) return {
122
+ tools: void 0,
123
+ toolChoice: void 0,
124
+ toolWarnings
125
+ };
126
+ const huggingfaceTools = [];
127
+ for (const tool of tools) switch (tool.type) {
128
+ case "function":
129
+ huggingfaceTools.push({
130
+ type: "function",
131
+ name: tool.name,
132
+ description: tool.description,
133
+ parameters: tool.inputSchema
134
+ });
135
+ break;
136
+ case "provider":
137
+ toolWarnings.push({
138
+ type: "unsupported",
139
+ feature: `provider-defined tool ${tool.id}`
140
+ });
141
+ break;
142
+ default: throw new Error(`Unsupported tool type: ${tool}`);
143
+ }
144
+ let mappedToolChoice = void 0;
145
+ if (toolChoice) switch (toolChoice.type) {
146
+ case "auto":
147
+ mappedToolChoice = "auto";
148
+ break;
149
+ case "required":
150
+ mappedToolChoice = "required";
151
+ break;
152
+ case "none": break;
153
+ case "tool":
154
+ mappedToolChoice = {
155
+ type: "function",
156
+ function: { name: toolChoice.toolName }
157
+ };
158
+ break;
159
+ default: throw new Error(`Unsupported tool choice type: ${toolChoice}`);
160
+ }
161
+ return {
162
+ tools: huggingfaceTools,
163
+ toolChoice: mappedToolChoice,
164
+ toolWarnings
165
+ };
243
166
  }
244
-
245
- // src/responses/map-huggingface-responses-finish-reason.ts
167
+ //#endregion
168
+ //#region src/responses/map-huggingface-responses-finish-reason.ts
246
169
  function mapHuggingFaceResponsesFinishReason(finishReason) {
247
- switch (finishReason) {
248
- case "stop":
249
- return "stop";
250
- case "length":
251
- return "length";
252
- case "content_filter":
253
- return "content-filter";
254
- case "tool_calls":
255
- return "tool-calls";
256
- case "error":
257
- return "error";
258
- default:
259
- return "other";
260
- }
170
+ switch (finishReason) {
171
+ case "stop": return "stop";
172
+ case "length": return "length";
173
+ case "content_filter": return "content-filter";
174
+ case "tool_calls": return "tool-calls";
175
+ case "error": return "error";
176
+ default: return "other";
177
+ }
261
178
  }
262
-
263
- // src/responses/huggingface-responses-language-model.ts
264
- var HuggingFaceResponsesLanguageModel = class _HuggingFaceResponsesLanguageModel {
265
- constructor(modelId, config) {
266
- this.specificationVersion = "v4";
267
- this.supportedUrls = {
268
- "image/*": [/^https?:\/\/.*$/]
269
- };
270
- this.modelId = modelId;
271
- this.config = config;
272
- }
273
- static [WORKFLOW_SERIALIZE](model) {
274
- return serializeModelOptions({
275
- modelId: model.modelId,
276
- config: model.config
277
- });
278
- }
279
- static [WORKFLOW_DESERIALIZE](options) {
280
- return new _HuggingFaceResponsesLanguageModel(
281
- options.modelId,
282
- options.config
283
- );
284
- }
285
- get provider() {
286
- return this.config.provider;
287
- }
288
- async getArgs({
289
- maxOutputTokens,
290
- temperature,
291
- stopSequences,
292
- topP,
293
- topK,
294
- presencePenalty,
295
- frequencyPenalty,
296
- seed,
297
- prompt,
298
- providerOptions,
299
- tools,
300
- toolChoice,
301
- responseFormat
302
- }) {
303
- const warnings = [];
304
- if (topK != null) {
305
- warnings.push({ type: "unsupported", feature: "topK" });
306
- }
307
- if (seed != null) {
308
- warnings.push({ type: "unsupported", feature: "seed" });
309
- }
310
- if (presencePenalty != null) {
311
- warnings.push({ type: "unsupported", feature: "presencePenalty" });
312
- }
313
- if (frequencyPenalty != null) {
314
- warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
315
- }
316
- if (stopSequences != null) {
317
- warnings.push({ type: "unsupported", feature: "stopSequences" });
318
- }
319
- const { input, warnings: messageWarnings } = await convertToHuggingFaceResponsesMessages({
320
- prompt
321
- });
322
- warnings.push(...messageWarnings);
323
- const huggingfaceOptions = await parseProviderOptions({
324
- provider: "huggingface",
325
- providerOptions,
326
- schema: huggingfaceLanguageModelResponsesOptions
327
- });
328
- const {
329
- tools: preparedTools,
330
- toolChoice: preparedToolChoice,
331
- toolWarnings
332
- } = prepareResponsesTools({
333
- tools,
334
- toolChoice
335
- });
336
- warnings.push(...toolWarnings);
337
- const baseArgs = {
338
- model: this.modelId,
339
- input,
340
- temperature,
341
- top_p: topP,
342
- max_output_tokens: maxOutputTokens,
343
- // HuggingFace Responses API uses text.format for structured output
344
- ...responseFormat?.type === "json" && responseFormat.schema && {
345
- text: {
346
- format: {
347
- type: "json_schema",
348
- strict: huggingfaceOptions?.strictJsonSchema ?? false,
349
- name: responseFormat.name ?? "response",
350
- description: responseFormat.description,
351
- schema: responseFormat.schema
352
- }
353
- }
354
- },
355
- metadata: huggingfaceOptions?.metadata,
356
- instructions: huggingfaceOptions?.instructions,
357
- ...preparedTools && { tools: preparedTools },
358
- ...preparedToolChoice && { tool_choice: preparedToolChoice },
359
- ...huggingfaceOptions?.reasoningEffort != null && {
360
- reasoning: {
361
- ...huggingfaceOptions?.reasoningEffort != null && {
362
- effort: huggingfaceOptions.reasoningEffort
363
- }
364
- }
365
- }
366
- };
367
- return { args: baseArgs, warnings };
368
- }
369
- async doGenerate(options) {
370
- const { args, warnings } = await this.getArgs(options);
371
- const body = {
372
- ...args,
373
- stream: false
374
- };
375
- const url = this.config.url({
376
- path: "/responses",
377
- modelId: this.modelId
378
- });
379
- const {
380
- value: response,
381
- responseHeaders,
382
- rawValue: rawResponse
383
- } = await postJsonToApi({
384
- url,
385
- headers: combineHeaders(this.config.headers?.(), options.headers),
386
- body,
387
- failedResponseHandler: huggingfaceFailedResponseHandler,
388
- successfulResponseHandler: createJsonResponseHandler(
389
- huggingfaceResponsesResponseSchema
390
- ),
391
- abortSignal: options.abortSignal,
392
- fetch: this.config.fetch
393
- });
394
- if (response.error) {
395
- throw new APICallError({
396
- message: response.error.message,
397
- url,
398
- requestBodyValues: body,
399
- statusCode: 400,
400
- responseHeaders,
401
- responseBody: rawResponse,
402
- isRetryable: false
403
- });
404
- }
405
- const content = [];
406
- for (const part of response.output) {
407
- switch (part.type) {
408
- case "message": {
409
- for (const contentPart of part.content) {
410
- content.push({
411
- type: "text",
412
- text: contentPart.text,
413
- providerMetadata: {
414
- huggingface: {
415
- itemId: part.id
416
- }
417
- }
418
- });
419
- if (contentPart.annotations) {
420
- for (const annotation of contentPart.annotations) {
421
- content.push({
422
- type: "source",
423
- sourceType: "url",
424
- id: this.config.generateId?.() ?? generateId(),
425
- url: annotation.url,
426
- title: annotation.title
427
- });
428
- }
429
- }
430
- }
431
- break;
432
- }
433
- case "reasoning": {
434
- for (const contentPart of part.content) {
435
- content.push({
436
- type: "reasoning",
437
- text: contentPart.text,
438
- providerMetadata: {
439
- huggingface: {
440
- itemId: part.id
441
- }
442
- }
443
- });
444
- }
445
- break;
446
- }
447
- case "mcp_call": {
448
- content.push({
449
- type: "tool-call",
450
- toolCallId: part.id,
451
- toolName: part.name,
452
- input: part.arguments,
453
- providerExecuted: true
454
- });
455
- if (part.output) {
456
- content.push({
457
- type: "tool-result",
458
- toolCallId: part.id,
459
- toolName: part.name,
460
- result: part.output
461
- });
462
- }
463
- break;
464
- }
465
- case "mcp_list_tools": {
466
- content.push({
467
- type: "tool-call",
468
- toolCallId: part.id,
469
- toolName: "list_tools",
470
- input: JSON.stringify({ server_label: part.server_label }),
471
- providerExecuted: true
472
- });
473
- if (part.tools) {
474
- content.push({
475
- type: "tool-result",
476
- toolCallId: part.id,
477
- toolName: "list_tools",
478
- result: { tools: part.tools }
479
- });
480
- }
481
- break;
482
- }
483
- case "function_call": {
484
- content.push({
485
- type: "tool-call",
486
- toolCallId: part.call_id,
487
- toolName: part.name,
488
- input: part.arguments
489
- });
490
- if (part.output) {
491
- content.push({
492
- type: "tool-result",
493
- toolCallId: part.call_id,
494
- toolName: part.name,
495
- result: part.output
496
- });
497
- }
498
- break;
499
- }
500
- default: {
501
- break;
502
- }
503
- }
504
- }
505
- return {
506
- content,
507
- finishReason: {
508
- unified: mapHuggingFaceResponsesFinishReason(
509
- response.incomplete_details?.reason ?? "stop"
510
- ),
511
- raw: response.incomplete_details?.reason ?? void 0
512
- },
513
- usage: convertHuggingFaceResponsesUsage(response.usage),
514
- request: { body },
515
- response: {
516
- id: response.id,
517
- timestamp: new Date(response.created_at * 1e3),
518
- modelId: response.model,
519
- headers: responseHeaders,
520
- body: rawResponse
521
- },
522
- providerMetadata: {
523
- huggingface: {
524
- responseId: response.id
525
- }
526
- },
527
- warnings
528
- };
529
- }
530
- async doStream(options) {
531
- const { args, warnings } = await this.getArgs(options);
532
- const body = {
533
- ...args,
534
- stream: true
535
- };
536
- const { value: response, responseHeaders } = await postJsonToApi({
537
- url: this.config.url({
538
- path: "/responses",
539
- modelId: this.modelId
540
- }),
541
- headers: combineHeaders(this.config.headers?.(), options.headers),
542
- body,
543
- failedResponseHandler: huggingfaceFailedResponseHandler,
544
- successfulResponseHandler: createEventSourceResponseHandler(
545
- huggingfaceResponsesChunkSchema
546
- ),
547
- abortSignal: options.abortSignal,
548
- fetch: this.config.fetch
549
- });
550
- let finishReason = {
551
- unified: "other",
552
- raw: void 0
553
- };
554
- let responseId = null;
555
- let usage = void 0;
556
- return {
557
- stream: response.pipeThrough(
558
- new TransformStream({
559
- start(controller) {
560
- controller.enqueue({ type: "stream-start", warnings });
561
- },
562
- transform(chunk, controller) {
563
- if (!chunk.success) {
564
- finishReason = {
565
- unified: "error",
566
- raw: void 0
567
- };
568
- controller.enqueue({ type: "error", error: chunk.error });
569
- return;
570
- }
571
- const value = chunk.value;
572
- const streamError = createHuggingFaceResponsesStreamError(value);
573
- if (streamError != null) {
574
- finishReason = {
575
- unified: "error",
576
- raw: streamError.code != null ? String(streamError.code) : streamError.type
577
- };
578
- controller.enqueue({ type: "error", error: streamError });
579
- return;
580
- }
581
- if (isResponseCreatedChunk(value)) {
582
- responseId = value.response.id;
583
- controller.enqueue({
584
- type: "response-metadata",
585
- id: value.response.id,
586
- timestamp: new Date(value.response.created_at * 1e3),
587
- modelId: value.response.model
588
- });
589
- return;
590
- }
591
- if (isResponseOutputItemAddedChunk(value)) {
592
- if (value.item.type === "message" && value.item.role === "assistant") {
593
- controller.enqueue({
594
- type: "text-start",
595
- id: value.item.id,
596
- providerMetadata: {
597
- huggingface: {
598
- itemId: value.item.id
599
- }
600
- }
601
- });
602
- } else if (value.item.type === "function_call") {
603
- controller.enqueue({
604
- type: "tool-input-start",
605
- id: value.item.call_id,
606
- toolName: value.item.name
607
- });
608
- } else if (value.item.type === "reasoning") {
609
- controller.enqueue({
610
- type: "reasoning-start",
611
- id: value.item.id,
612
- providerMetadata: {
613
- huggingface: {
614
- itemId: value.item.id
615
- }
616
- }
617
- });
618
- }
619
- return;
620
- }
621
- if (isResponseOutputItemDoneChunk(value)) {
622
- if (value.item.type === "message" && value.item.role === "assistant") {
623
- controller.enqueue({
624
- type: "text-end",
625
- id: value.item.id
626
- });
627
- } else if (value.item.type === "function_call") {
628
- controller.enqueue({
629
- type: "tool-input-end",
630
- id: value.item.call_id
631
- });
632
- controller.enqueue({
633
- type: "tool-call",
634
- toolCallId: value.item.call_id,
635
- toolName: value.item.name,
636
- input: value.item.arguments
637
- });
638
- if (value.item.output) {
639
- controller.enqueue({
640
- type: "tool-result",
641
- toolCallId: value.item.call_id,
642
- toolName: value.item.name,
643
- result: value.item.output
644
- });
645
- }
646
- }
647
- return;
648
- }
649
- if (isResponseCompletedChunk(value)) {
650
- responseId = value.response.id;
651
- finishReason = {
652
- unified: mapHuggingFaceResponsesFinishReason(
653
- value.response.incomplete_details?.reason ?? "stop"
654
- ),
655
- raw: value.response.incomplete_details?.reason ?? void 0
656
- };
657
- if (value.response.usage) {
658
- usage = value.response.usage;
659
- }
660
- return;
661
- }
662
- if (isReasoningDeltaChunk(value)) {
663
- controller.enqueue({
664
- type: "reasoning-delta",
665
- id: value.item_id,
666
- delta: value.delta
667
- });
668
- return;
669
- }
670
- if (isReasoningEndChunk(value)) {
671
- controller.enqueue({
672
- type: "reasoning-end",
673
- id: value.item_id
674
- });
675
- return;
676
- }
677
- if (isTextDeltaChunk(value)) {
678
- controller.enqueue({
679
- type: "text-delta",
680
- id: value.item_id,
681
- delta: value.delta
682
- });
683
- }
684
- },
685
- flush(controller) {
686
- controller.enqueue({
687
- type: "finish",
688
- finishReason,
689
- usage: convertHuggingFaceResponsesUsage(usage),
690
- providerMetadata: {
691
- huggingface: {
692
- responseId
693
- }
694
- }
695
- });
696
- }
697
- })
698
- ),
699
- request: { body },
700
- response: { headers: responseHeaders }
701
- };
702
- }
179
+ //#endregion
180
+ //#region src/responses/huggingface-responses-language-model.ts
181
+ var HuggingFaceResponsesLanguageModel = class HuggingFaceResponsesLanguageModel {
182
+ static [WORKFLOW_SERIALIZE](model) {
183
+ return serializeModelOptions({
184
+ modelId: model.modelId,
185
+ config: model.config
186
+ });
187
+ }
188
+ static [WORKFLOW_DESERIALIZE](options) {
189
+ return new HuggingFaceResponsesLanguageModel(options.modelId, options.config);
190
+ }
191
+ constructor(modelId, config) {
192
+ this.specificationVersion = "v4";
193
+ this.supportedUrls = { "image/*": [/^https?:\/\/.*$/] };
194
+ this.modelId = modelId;
195
+ this.config = config;
196
+ }
197
+ get provider() {
198
+ return this.config.provider;
199
+ }
200
+ async getArgs({ maxOutputTokens, temperature, stopSequences, topP, topK, presencePenalty, frequencyPenalty, seed, prompt, providerOptions, tools, toolChoice, responseFormat }) {
201
+ const warnings = [];
202
+ if (topK != null) warnings.push({
203
+ type: "unsupported",
204
+ feature: "topK"
205
+ });
206
+ if (seed != null) warnings.push({
207
+ type: "unsupported",
208
+ feature: "seed"
209
+ });
210
+ if (presencePenalty != null) warnings.push({
211
+ type: "unsupported",
212
+ feature: "presencePenalty"
213
+ });
214
+ if (frequencyPenalty != null) warnings.push({
215
+ type: "unsupported",
216
+ feature: "frequencyPenalty"
217
+ });
218
+ if (stopSequences != null) warnings.push({
219
+ type: "unsupported",
220
+ feature: "stopSequences"
221
+ });
222
+ const { input, warnings: messageWarnings } = await convertToHuggingFaceResponsesMessages({ prompt });
223
+ warnings.push(...messageWarnings);
224
+ const huggingfaceOptions = await parseProviderOptions({
225
+ provider: "huggingface",
226
+ providerOptions,
227
+ schema: huggingfaceLanguageModelResponsesOptions
228
+ });
229
+ const { tools: preparedTools, toolChoice: preparedToolChoice, toolWarnings } = prepareResponsesTools({
230
+ tools,
231
+ toolChoice
232
+ });
233
+ warnings.push(...toolWarnings);
234
+ return {
235
+ args: {
236
+ model: this.modelId,
237
+ input,
238
+ temperature,
239
+ top_p: topP,
240
+ max_output_tokens: maxOutputTokens,
241
+ ...responseFormat?.type === "json" && responseFormat.schema && { text: { format: {
242
+ type: "json_schema",
243
+ strict: huggingfaceOptions?.strictJsonSchema ?? false,
244
+ name: responseFormat.name ?? "response",
245
+ description: responseFormat.description,
246
+ schema: responseFormat.schema
247
+ } } },
248
+ metadata: huggingfaceOptions?.metadata,
249
+ instructions: huggingfaceOptions?.instructions,
250
+ ...preparedTools && { tools: preparedTools },
251
+ ...preparedToolChoice && { tool_choice: preparedToolChoice },
252
+ ...huggingfaceOptions?.reasoningEffort != null && { reasoning: { ...huggingfaceOptions?.reasoningEffort != null && { effort: huggingfaceOptions.reasoningEffort } } }
253
+ },
254
+ warnings
255
+ };
256
+ }
257
+ async doGenerate(options) {
258
+ const { args, warnings } = await this.getArgs(options);
259
+ const body = {
260
+ ...args,
261
+ stream: false
262
+ };
263
+ const url = this.config.url({
264
+ path: "/responses",
265
+ modelId: this.modelId
266
+ });
267
+ const { value: response, responseHeaders, rawValue: rawResponse } = await postJsonToApi({
268
+ url,
269
+ headers: combineHeaders(this.config.headers?.(), options.headers),
270
+ body,
271
+ failedResponseHandler: huggingfaceFailedResponseHandler,
272
+ successfulResponseHandler: createJsonResponseHandler(huggingfaceResponsesResponseSchema),
273
+ abortSignal: options.abortSignal,
274
+ fetch: this.config.fetch
275
+ });
276
+ if (response.error) throw new APICallError({
277
+ message: response.error.message,
278
+ url,
279
+ requestBodyValues: body,
280
+ statusCode: 400,
281
+ responseHeaders,
282
+ responseBody: rawResponse,
283
+ isRetryable: false
284
+ });
285
+ const content = [];
286
+ for (const part of response.output) switch (part.type) {
287
+ case "message":
288
+ for (const contentPart of part.content) {
289
+ content.push({
290
+ type: "text",
291
+ text: contentPart.text,
292
+ providerMetadata: { huggingface: { itemId: part.id } }
293
+ });
294
+ if (contentPart.annotations) for (const annotation of contentPart.annotations) content.push({
295
+ type: "source",
296
+ sourceType: "url",
297
+ id: this.config.generateId?.() ?? generateId(),
298
+ url: annotation.url,
299
+ title: annotation.title
300
+ });
301
+ }
302
+ break;
303
+ case "reasoning":
304
+ for (const contentPart of part.content) content.push({
305
+ type: "reasoning",
306
+ text: contentPart.text,
307
+ providerMetadata: { huggingface: { itemId: part.id } }
308
+ });
309
+ break;
310
+ case "mcp_call":
311
+ content.push({
312
+ type: "tool-call",
313
+ toolCallId: part.id,
314
+ toolName: part.name,
315
+ input: part.arguments,
316
+ providerExecuted: true
317
+ });
318
+ if (part.output) content.push({
319
+ type: "tool-result",
320
+ toolCallId: part.id,
321
+ toolName: part.name,
322
+ result: part.output
323
+ });
324
+ break;
325
+ case "mcp_list_tools":
326
+ content.push({
327
+ type: "tool-call",
328
+ toolCallId: part.id,
329
+ toolName: "list_tools",
330
+ input: JSON.stringify({ server_label: part.server_label }),
331
+ providerExecuted: true
332
+ });
333
+ if (part.tools) content.push({
334
+ type: "tool-result",
335
+ toolCallId: part.id,
336
+ toolName: "list_tools",
337
+ result: { tools: part.tools }
338
+ });
339
+ break;
340
+ case "function_call":
341
+ content.push({
342
+ type: "tool-call",
343
+ toolCallId: part.call_id,
344
+ toolName: part.name,
345
+ input: part.arguments
346
+ });
347
+ if (part.output) content.push({
348
+ type: "tool-result",
349
+ toolCallId: part.call_id,
350
+ toolName: part.name,
351
+ result: part.output
352
+ });
353
+ }
354
+ return {
355
+ content,
356
+ finishReason: {
357
+ unified: mapHuggingFaceResponsesFinishReason(response.incomplete_details?.reason ?? "stop"),
358
+ raw: response.incomplete_details?.reason ?? void 0
359
+ },
360
+ usage: convertHuggingFaceResponsesUsage(response.usage),
361
+ request: { body },
362
+ response: {
363
+ id: response.id,
364
+ timestamp: /* @__PURE__ */ new Date(response.created_at * 1e3),
365
+ modelId: response.model,
366
+ headers: responseHeaders,
367
+ body: rawResponse
368
+ },
369
+ providerMetadata: { huggingface: { responseId: response.id } },
370
+ warnings
371
+ };
372
+ }
373
+ async doStream(options) {
374
+ const { args, warnings } = await this.getArgs(options);
375
+ const body = {
376
+ ...args,
377
+ stream: true
378
+ };
379
+ const { value: response, responseHeaders } = await postJsonToApi({
380
+ url: this.config.url({
381
+ path: "/responses",
382
+ modelId: this.modelId
383
+ }),
384
+ headers: combineHeaders(this.config.headers?.(), options.headers),
385
+ body,
386
+ failedResponseHandler: huggingfaceFailedResponseHandler,
387
+ successfulResponseHandler: createEventSourceResponseHandler(huggingfaceResponsesChunkSchema),
388
+ abortSignal: options.abortSignal,
389
+ fetch: this.config.fetch
390
+ });
391
+ let finishReason = {
392
+ unified: "other",
393
+ raw: void 0
394
+ };
395
+ let responseId = null;
396
+ let usage = void 0;
397
+ return {
398
+ stream: response.pipeThrough(new TransformStream({
399
+ start(controller) {
400
+ controller.enqueue({
401
+ type: "stream-start",
402
+ warnings
403
+ });
404
+ },
405
+ transform(chunk, controller) {
406
+ if (!chunk.success) {
407
+ finishReason = {
408
+ unified: "error",
409
+ raw: void 0
410
+ };
411
+ controller.enqueue({
412
+ type: "error",
413
+ error: chunk.error
414
+ });
415
+ return;
416
+ }
417
+ const value = chunk.value;
418
+ const streamError = createHuggingFaceResponsesStreamError(value);
419
+ if (streamError != null) {
420
+ finishReason = {
421
+ unified: "error",
422
+ raw: streamError.code != null ? String(streamError.code) : streamError.type
423
+ };
424
+ controller.enqueue({
425
+ type: "error",
426
+ error: streamError
427
+ });
428
+ return;
429
+ }
430
+ if (isResponseCreatedChunk(value)) {
431
+ responseId = value.response.id;
432
+ controller.enqueue({
433
+ type: "response-metadata",
434
+ id: value.response.id,
435
+ timestamp: /* @__PURE__ */ new Date(value.response.created_at * 1e3),
436
+ modelId: value.response.model
437
+ });
438
+ return;
439
+ }
440
+ if (isResponseOutputItemAddedChunk(value)) {
441
+ if (value.item.type === "message" && value.item.role === "assistant") controller.enqueue({
442
+ type: "text-start",
443
+ id: value.item.id,
444
+ providerMetadata: { huggingface: { itemId: value.item.id } }
445
+ });
446
+ else if (value.item.type === "function_call") controller.enqueue({
447
+ type: "tool-input-start",
448
+ id: value.item.call_id,
449
+ toolName: value.item.name
450
+ });
451
+ else if (value.item.type === "reasoning") controller.enqueue({
452
+ type: "reasoning-start",
453
+ id: value.item.id,
454
+ providerMetadata: { huggingface: { itemId: value.item.id } }
455
+ });
456
+ return;
457
+ }
458
+ if (isResponseOutputItemDoneChunk(value)) {
459
+ if (value.item.type === "message" && value.item.role === "assistant") controller.enqueue({
460
+ type: "text-end",
461
+ id: value.item.id
462
+ });
463
+ else if (value.item.type === "function_call") {
464
+ controller.enqueue({
465
+ type: "tool-input-end",
466
+ id: value.item.call_id
467
+ });
468
+ controller.enqueue({
469
+ type: "tool-call",
470
+ toolCallId: value.item.call_id,
471
+ toolName: value.item.name,
472
+ input: value.item.arguments
473
+ });
474
+ if (value.item.output) controller.enqueue({
475
+ type: "tool-result",
476
+ toolCallId: value.item.call_id,
477
+ toolName: value.item.name,
478
+ result: value.item.output
479
+ });
480
+ }
481
+ return;
482
+ }
483
+ if (isResponseCompletedChunk(value)) {
484
+ responseId = value.response.id;
485
+ finishReason = {
486
+ unified: mapHuggingFaceResponsesFinishReason(value.response.incomplete_details?.reason ?? "stop"),
487
+ raw: value.response.incomplete_details?.reason ?? void 0
488
+ };
489
+ if (value.response.usage) usage = value.response.usage;
490
+ return;
491
+ }
492
+ if (isReasoningDeltaChunk(value)) {
493
+ controller.enqueue({
494
+ type: "reasoning-delta",
495
+ id: value.item_id,
496
+ delta: value.delta
497
+ });
498
+ return;
499
+ }
500
+ if (isReasoningEndChunk(value)) {
501
+ controller.enqueue({
502
+ type: "reasoning-end",
503
+ id: value.item_id
504
+ });
505
+ return;
506
+ }
507
+ if (isTextDeltaChunk(value)) controller.enqueue({
508
+ type: "text-delta",
509
+ id: value.item_id,
510
+ delta: value.delta
511
+ });
512
+ },
513
+ flush(controller) {
514
+ controller.enqueue({
515
+ type: "finish",
516
+ finishReason,
517
+ usage: convertHuggingFaceResponsesUsage(usage),
518
+ providerMetadata: { huggingface: { responseId } }
519
+ });
520
+ }
521
+ })),
522
+ request: { body },
523
+ response: { headers: responseHeaders }
524
+ };
525
+ }
703
526
  };
704
527
  function createHuggingFaceResponsesStreamError(event) {
705
- const outer = asRecord(event);
706
- if (outer?.type !== "error" && outer?.type !== "response.failed") {
707
- return void 0;
708
- }
709
- const details = outer.type === "response.failed" ? asRecord(asRecord(outer.response)?.error) : asRecord(outer.error) ?? outer;
710
- if (details == null || typeof details.message !== "string") {
711
- return void 0;
712
- }
713
- const code = typeof details.code === "string" || typeof details.code === "number" ? details.code : void 0;
714
- return createProviderStreamError({
715
- message: details.message,
716
- type: outer.type,
717
- code,
718
- data: event
719
- });
528
+ const outer = asRecord(event);
529
+ if (outer?.type !== "error" && outer?.type !== "response.failed") return;
530
+ const details = outer.type === "response.failed" ? asRecord(asRecord(outer.response)?.error) : asRecord(outer.error) ?? outer;
531
+ if (details == null || typeof details.message !== "string") return;
532
+ const code = typeof details.code === "string" || typeof details.code === "number" ? details.code : void 0;
533
+ return createProviderStreamError({
534
+ message: details.message,
535
+ type: outer.type,
536
+ code,
537
+ data: event
538
+ });
720
539
  }
721
540
  function asRecord(value) {
722
- return typeof value === "object" && value != null ? value : void 0;
541
+ return typeof value === "object" && value != null ? value : void 0;
723
542
  }
724
- var huggingfaceResponsesOutputSchema = z3.discriminatedUnion("type", [
725
- z3.object({
726
- type: z3.literal("message"),
727
- id: z3.string(),
728
- role: z3.string().optional(),
729
- status: z3.string().optional(),
730
- content: z3.array(
731
- z3.object({
732
- type: z3.literal("output_text"),
733
- text: z3.string(),
734
- annotations: z3.array(z3.any()).optional()
735
- })
736
- )
737
- }),
738
- z3.object({
739
- type: z3.literal("reasoning"),
740
- id: z3.string(),
741
- status: z3.string().optional(),
742
- content: z3.array(
743
- z3.object({
744
- type: z3.literal("reasoning_text"),
745
- text: z3.string()
746
- })
747
- ),
748
- summary: z3.array(
749
- z3.object({
750
- type: z3.literal("reasoning_summary"),
751
- text: z3.string()
752
- }).optional()
753
- ).optional()
754
- }),
755
- z3.object({
756
- type: z3.literal("function_call"),
757
- id: z3.string(),
758
- call_id: z3.string(),
759
- name: z3.string(),
760
- arguments: z3.string(),
761
- output: z3.string().optional(),
762
- status: z3.string().optional()
763
- }),
764
- z3.object({
765
- type: z3.literal("mcp_call"),
766
- id: z3.string(),
767
- name: z3.string(),
768
- arguments: z3.string(),
769
- output: z3.string().optional(),
770
- status: z3.string().optional()
771
- }),
772
- z3.object({
773
- type: z3.literal("mcp_list_tools"),
774
- id: z3.string(),
775
- server_label: z3.string(),
776
- tools: z3.array(z3.any()).optional(),
777
- status: z3.string().optional()
778
- })
543
+ const huggingfaceResponsesOutputSchema = z.discriminatedUnion("type", [
544
+ z.object({
545
+ type: z.literal("message"),
546
+ id: z.string(),
547
+ role: z.string().optional(),
548
+ status: z.string().optional(),
549
+ content: z.array(z.object({
550
+ type: z.literal("output_text"),
551
+ text: z.string(),
552
+ annotations: z.array(z.any()).optional()
553
+ }))
554
+ }),
555
+ z.object({
556
+ type: z.literal("reasoning"),
557
+ id: z.string(),
558
+ status: z.string().optional(),
559
+ content: z.array(z.object({
560
+ type: z.literal("reasoning_text"),
561
+ text: z.string()
562
+ })),
563
+ summary: z.array(z.object({
564
+ type: z.literal("reasoning_summary"),
565
+ text: z.string()
566
+ }).optional()).optional()
567
+ }),
568
+ z.object({
569
+ type: z.literal("function_call"),
570
+ id: z.string(),
571
+ call_id: z.string(),
572
+ name: z.string(),
573
+ arguments: z.string(),
574
+ output: z.string().optional(),
575
+ status: z.string().optional()
576
+ }),
577
+ z.object({
578
+ type: z.literal("mcp_call"),
579
+ id: z.string(),
580
+ name: z.string(),
581
+ arguments: z.string(),
582
+ output: z.string().optional(),
583
+ status: z.string().optional()
584
+ }),
585
+ z.object({
586
+ type: z.literal("mcp_list_tools"),
587
+ id: z.string(),
588
+ server_label: z.string(),
589
+ tools: z.array(z.any()).optional(),
590
+ status: z.string().optional()
591
+ })
779
592
  ]);
780
- var huggingfaceResponsesResponseSchema = z3.object({
781
- id: z3.string(),
782
- model: z3.string(),
783
- object: z3.string(),
784
- created_at: z3.number(),
785
- status: z3.string(),
786
- error: z3.any().nullable(),
787
- instructions: z3.any().nullable(),
788
- max_output_tokens: z3.any().nullable(),
789
- metadata: z3.any().nullable(),
790
- tool_choice: z3.any(),
791
- tools: z3.array(z3.any()),
792
- temperature: z3.number(),
793
- top_p: z3.number(),
794
- incomplete_details: z3.object({
795
- reason: z3.string()
796
- }).nullable().optional(),
797
- usage: z3.object({
798
- input_tokens: z3.number(),
799
- input_tokens_details: z3.object({
800
- cached_tokens: z3.number()
801
- }).optional(),
802
- output_tokens: z3.number(),
803
- output_tokens_details: z3.object({
804
- reasoning_tokens: z3.number()
805
- }).optional(),
806
- total_tokens: z3.number()
807
- }).nullable().optional(),
808
- output: z3.array(huggingfaceResponsesOutputSchema),
809
- output_text: z3.string().nullable().optional()
593
+ const huggingfaceResponsesResponseSchema = z.object({
594
+ id: z.string(),
595
+ model: z.string(),
596
+ object: z.string(),
597
+ created_at: z.number(),
598
+ status: z.string(),
599
+ error: z.any().nullable(),
600
+ instructions: z.any().nullable(),
601
+ max_output_tokens: z.any().nullable(),
602
+ metadata: z.any().nullable(),
603
+ tool_choice: z.any(),
604
+ tools: z.array(z.any()),
605
+ temperature: z.number(),
606
+ top_p: z.number(),
607
+ incomplete_details: z.object({ reason: z.string() }).nullable().optional(),
608
+ usage: z.object({
609
+ input_tokens: z.number(),
610
+ input_tokens_details: z.object({ cached_tokens: z.number() }).optional(),
611
+ output_tokens: z.number(),
612
+ output_tokens_details: z.object({ reasoning_tokens: z.number() }).optional(),
613
+ total_tokens: z.number()
614
+ }).nullable().optional(),
615
+ output: z.array(huggingfaceResponsesOutputSchema),
616
+ output_text: z.string().nullable().optional()
810
617
  });
811
- var responseOutputItemAddedSchema = z3.object({
812
- type: z3.literal("response.output_item.added"),
813
- output_index: z3.number(),
814
- item: z3.discriminatedUnion("type", [
815
- z3.object({
816
- type: z3.literal("message"),
817
- id: z3.string(),
818
- role: z3.string().optional(),
819
- status: z3.string().optional(),
820
- content: z3.array(z3.any()).optional()
821
- }),
822
- z3.object({
823
- type: z3.literal("reasoning"),
824
- id: z3.string(),
825
- status: z3.string().optional(),
826
- content: z3.array(z3.any()).optional(),
827
- summary: z3.array(z3.any()).optional()
828
- }),
829
- z3.object({
830
- type: z3.literal("mcp_list_tools"),
831
- id: z3.string(),
832
- server_label: z3.string(),
833
- tools: z3.array(z3.any()).optional(),
834
- error: z3.string().optional()
835
- }),
836
- z3.object({
837
- type: z3.literal("mcp_call"),
838
- id: z3.string(),
839
- server_label: z3.string(),
840
- name: z3.string(),
841
- arguments: z3.string(),
842
- output: z3.string().optional(),
843
- error: z3.string().optional()
844
- }),
845
- z3.object({
846
- type: z3.literal("function_call"),
847
- id: z3.string(),
848
- call_id: z3.string(),
849
- name: z3.string(),
850
- arguments: z3.string(),
851
- output: z3.string().optional(),
852
- error: z3.string().optional()
853
- })
854
- ]),
855
- sequence_number: z3.number()
618
+ const responseOutputItemAddedSchema = z.object({
619
+ type: z.literal("response.output_item.added"),
620
+ output_index: z.number(),
621
+ item: z.discriminatedUnion("type", [
622
+ z.object({
623
+ type: z.literal("message"),
624
+ id: z.string(),
625
+ role: z.string().optional(),
626
+ status: z.string().optional(),
627
+ content: z.array(z.any()).optional()
628
+ }),
629
+ z.object({
630
+ type: z.literal("reasoning"),
631
+ id: z.string(),
632
+ status: z.string().optional(),
633
+ content: z.array(z.any()).optional(),
634
+ summary: z.array(z.any()).optional()
635
+ }),
636
+ z.object({
637
+ type: z.literal("mcp_list_tools"),
638
+ id: z.string(),
639
+ server_label: z.string(),
640
+ tools: z.array(z.any()).optional(),
641
+ error: z.string().optional()
642
+ }),
643
+ z.object({
644
+ type: z.literal("mcp_call"),
645
+ id: z.string(),
646
+ server_label: z.string(),
647
+ name: z.string(),
648
+ arguments: z.string(),
649
+ output: z.string().optional(),
650
+ error: z.string().optional()
651
+ }),
652
+ z.object({
653
+ type: z.literal("function_call"),
654
+ id: z.string(),
655
+ call_id: z.string(),
656
+ name: z.string(),
657
+ arguments: z.string(),
658
+ output: z.string().optional(),
659
+ error: z.string().optional()
660
+ })
661
+ ]),
662
+ sequence_number: z.number()
856
663
  });
857
- var responseOutputItemDoneSchema = z3.object({
858
- type: z3.literal("response.output_item.done"),
859
- output_index: z3.number(),
860
- item: z3.discriminatedUnion("type", [
861
- z3.object({
862
- type: z3.literal("message"),
863
- id: z3.string(),
864
- role: z3.string().optional(),
865
- status: z3.string().optional(),
866
- content: z3.array(z3.any()).optional()
867
- }),
868
- z3.object({
869
- type: z3.literal("mcp_list_tools"),
870
- id: z3.string(),
871
- server_label: z3.string(),
872
- tools: z3.array(z3.any()).optional(),
873
- error: z3.string().optional()
874
- }),
875
- z3.object({
876
- type: z3.literal("mcp_call"),
877
- id: z3.string(),
878
- server_label: z3.string(),
879
- name: z3.string(),
880
- arguments: z3.string(),
881
- output: z3.string().optional(),
882
- error: z3.string().optional()
883
- }),
884
- z3.object({
885
- type: z3.literal("function_call"),
886
- id: z3.string(),
887
- call_id: z3.string(),
888
- name: z3.string(),
889
- arguments: z3.string(),
890
- output: z3.string().optional(),
891
- error: z3.string().optional()
892
- }),
893
- z3.object({
894
- type: z3.literal("reasoning"),
895
- id: z3.string(),
896
- status: z3.string().optional(),
897
- content: z3.array(z3.any()).optional(),
898
- summary: z3.array(z3.any()).optional()
899
- })
900
- ]),
901
- sequence_number: z3.number()
664
+ const responseOutputItemDoneSchema = z.object({
665
+ type: z.literal("response.output_item.done"),
666
+ output_index: z.number(),
667
+ item: z.discriminatedUnion("type", [
668
+ z.object({
669
+ type: z.literal("message"),
670
+ id: z.string(),
671
+ role: z.string().optional(),
672
+ status: z.string().optional(),
673
+ content: z.array(z.any()).optional()
674
+ }),
675
+ z.object({
676
+ type: z.literal("mcp_list_tools"),
677
+ id: z.string(),
678
+ server_label: z.string(),
679
+ tools: z.array(z.any()).optional(),
680
+ error: z.string().optional()
681
+ }),
682
+ z.object({
683
+ type: z.literal("mcp_call"),
684
+ id: z.string(),
685
+ server_label: z.string(),
686
+ name: z.string(),
687
+ arguments: z.string(),
688
+ output: z.string().optional(),
689
+ error: z.string().optional()
690
+ }),
691
+ z.object({
692
+ type: z.literal("function_call"),
693
+ id: z.string(),
694
+ call_id: z.string(),
695
+ name: z.string(),
696
+ arguments: z.string(),
697
+ output: z.string().optional(),
698
+ error: z.string().optional()
699
+ }),
700
+ z.object({
701
+ type: z.literal("reasoning"),
702
+ id: z.string(),
703
+ status: z.string().optional(),
704
+ content: z.array(z.any()).optional(),
705
+ summary: z.array(z.any()).optional()
706
+ })
707
+ ]),
708
+ sequence_number: z.number()
902
709
  });
903
- var textDeltaChunkSchema = z3.object({
904
- type: z3.literal("response.output_text.delta"),
905
- item_id: z3.string(),
906
- output_index: z3.number(),
907
- content_index: z3.number(),
908
- delta: z3.string(),
909
- sequence_number: z3.number()
710
+ const textDeltaChunkSchema = z.object({
711
+ type: z.literal("response.output_text.delta"),
712
+ item_id: z.string(),
713
+ output_index: z.number(),
714
+ content_index: z.number(),
715
+ delta: z.string(),
716
+ sequence_number: z.number()
910
717
  });
911
- var reasoningTextDeltaChunkSchema = z3.object({
912
- type: z3.literal("response.reasoning_text.delta"),
913
- item_id: z3.string(),
914
- output_index: z3.number(),
915
- content_index: z3.number(),
916
- delta: z3.string(),
917
- sequence_number: z3.number()
718
+ const reasoningTextDeltaChunkSchema = z.object({
719
+ type: z.literal("response.reasoning_text.delta"),
720
+ item_id: z.string(),
721
+ output_index: z.number(),
722
+ content_index: z.number(),
723
+ delta: z.string(),
724
+ sequence_number: z.number()
918
725
  });
919
- var reasoningTextEndChunkSchema = z3.object({
920
- type: z3.literal("response.reasoning_text.done"),
921
- item_id: z3.string(),
922
- output_index: z3.number(),
923
- content_index: z3.number(),
924
- text: z3.string(),
925
- sequence_number: z3.number()
726
+ const reasoningTextEndChunkSchema = z.object({
727
+ type: z.literal("response.reasoning_text.done"),
728
+ item_id: z.string(),
729
+ output_index: z.number(),
730
+ content_index: z.number(),
731
+ text: z.string(),
732
+ sequence_number: z.number()
926
733
  });
927
- var responseCompletedChunkSchema = z3.object({
928
- type: z3.literal("response.completed"),
929
- response: huggingfaceResponsesResponseSchema,
930
- sequence_number: z3.number()
734
+ const responseCompletedChunkSchema = z.object({
735
+ type: z.literal("response.completed"),
736
+ response: huggingfaceResponsesResponseSchema,
737
+ sequence_number: z.number()
931
738
  });
932
- var responseCreatedChunkSchema = z3.object({
933
- type: z3.literal("response.created"),
934
- response: z3.object({
935
- id: z3.string(),
936
- object: z3.string(),
937
- created_at: z3.number(),
938
- status: z3.string(),
939
- model: z3.string()
940
- })
739
+ const responseCreatedChunkSchema = z.object({
740
+ type: z.literal("response.created"),
741
+ response: z.object({
742
+ id: z.string(),
743
+ object: z.string(),
744
+ created_at: z.number(),
745
+ status: z.string(),
746
+ model: z.string()
747
+ })
941
748
  });
942
- var huggingfaceResponsesChunkSchema = z3.union([
943
- responseOutputItemAddedSchema,
944
- responseOutputItemDoneSchema,
945
- reasoningTextDeltaChunkSchema,
946
- reasoningTextEndChunkSchema,
947
- textDeltaChunkSchema,
948
- responseCompletedChunkSchema,
949
- responseCreatedChunkSchema,
950
- z3.object({ type: z3.string() }).loose()
951
- // fallback for unknown chunks
749
+ const huggingfaceResponsesChunkSchema = z.union([
750
+ responseOutputItemAddedSchema,
751
+ responseOutputItemDoneSchema,
752
+ reasoningTextDeltaChunkSchema,
753
+ reasoningTextEndChunkSchema,
754
+ textDeltaChunkSchema,
755
+ responseCompletedChunkSchema,
756
+ responseCreatedChunkSchema,
757
+ z.object({ type: z.string() }).loose()
952
758
  ]);
953
759
  function isResponseOutputItemAddedChunk(chunk) {
954
- return chunk.type === "response.output_item.added";
760
+ return chunk.type === "response.output_item.added";
955
761
  }
956
762
  function isResponseOutputItemDoneChunk(chunk) {
957
- return chunk.type === "response.output_item.done";
763
+ return chunk.type === "response.output_item.done";
958
764
  }
959
765
  function isTextDeltaChunk(chunk) {
960
- return chunk.type === "response.output_text.delta";
766
+ return chunk.type === "response.output_text.delta";
961
767
  }
962
768
  function isReasoningDeltaChunk(chunk) {
963
- return chunk.type === "response.reasoning_text.delta";
769
+ return chunk.type === "response.reasoning_text.delta";
964
770
  }
965
771
  function isReasoningEndChunk(chunk) {
966
- return chunk.type === "response.reasoning_text.done";
772
+ return chunk.type === "response.reasoning_text.done";
967
773
  }
968
774
  function isResponseCompletedChunk(chunk) {
969
- return chunk.type === "response.completed";
775
+ return chunk.type === "response.completed";
970
776
  }
971
777
  function isResponseCreatedChunk(chunk) {
972
- return chunk.type === "response.created";
778
+ return chunk.type === "response.created";
973
779
  }
974
-
975
- // src/huggingface-provider.ts
780
+ //#endregion
781
+ //#region src/huggingface-provider.ts
782
+ /**
783
+ * Create a Hugging Face provider instance.
784
+ */
976
785
  function createHuggingFace(options = {}) {
977
- const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://router.huggingface.co/v1";
978
- const getHeaders = () => ({
979
- Authorization: `Bearer ${loadApiKey({
980
- apiKey: options.apiKey,
981
- environmentVariableName: "HUGGINGFACE_API_KEY",
982
- description: "Hugging Face"
983
- })}`,
984
- ...options.headers
985
- });
986
- const createResponsesModel = (modelId) => {
987
- return new HuggingFaceResponsesLanguageModel(modelId, {
988
- provider: "huggingface.responses",
989
- url: ({ path }) => `${baseURL}${path}`,
990
- headers: getHeaders,
991
- fetch: options.fetch,
992
- generateId: options.generateId ?? generateId2
993
- });
994
- };
995
- const provider = (modelId) => createResponsesModel(modelId);
996
- provider.specificationVersion = "v4";
997
- provider.languageModel = createResponsesModel;
998
- provider.responses = createResponsesModel;
999
- provider.embeddingModel = (modelId) => {
1000
- throw new NoSuchModelError({
1001
- modelId,
1002
- modelType: "embeddingModel",
1003
- message: "Hugging Face Responses API does not support text embeddings. Use the Hugging Face Inference API directly for embeddings."
1004
- });
1005
- };
1006
- provider.textEmbeddingModel = provider.embeddingModel;
1007
- provider.imageModel = (modelId) => {
1008
- throw new NoSuchModelError({
1009
- modelId,
1010
- modelType: "imageModel",
1011
- message: "Hugging Face Responses API does not support image generation. Use the Hugging Face Inference API directly for image models."
1012
- });
1013
- };
1014
- return provider;
786
+ const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://router.huggingface.co/v1";
787
+ const getHeaders = () => ({
788
+ Authorization: `Bearer ${loadApiKey({
789
+ apiKey: options.apiKey,
790
+ environmentVariableName: "HUGGINGFACE_API_KEY",
791
+ description: "Hugging Face"
792
+ })}`,
793
+ ...options.headers
794
+ });
795
+ const createResponsesModel = (modelId) => {
796
+ return new HuggingFaceResponsesLanguageModel(modelId, {
797
+ provider: "huggingface.responses",
798
+ url: ({ path }) => `${baseURL}${path}`,
799
+ headers: getHeaders,
800
+ fetch: options.fetch,
801
+ generateId: options.generateId ?? generateId
802
+ });
803
+ };
804
+ const provider = (modelId) => createResponsesModel(modelId);
805
+ provider.specificationVersion = "v4";
806
+ provider.languageModel = createResponsesModel;
807
+ provider.responses = createResponsesModel;
808
+ provider.embeddingModel = (modelId) => {
809
+ throw new NoSuchModelError({
810
+ modelId,
811
+ modelType: "embeddingModel",
812
+ message: "Hugging Face Responses API does not support text embeddings. Use the Hugging Face Inference API directly for embeddings."
813
+ });
814
+ };
815
+ provider.textEmbeddingModel = provider.embeddingModel;
816
+ provider.imageModel = (modelId) => {
817
+ throw new NoSuchModelError({
818
+ modelId,
819
+ modelType: "imageModel",
820
+ message: "Hugging Face Responses API does not support image generation. Use the Hugging Face Inference API directly for image models."
821
+ });
822
+ };
823
+ return provider;
1015
824
  }
1016
- var huggingFace = createHuggingFace();
825
+ /**
826
+ * Default Hugging Face provider instance.
827
+ */
828
+ const huggingFace = createHuggingFace();
829
+ //#endregion
830
+ //#region src/version.ts
831
+ const VERSION = "2.0.60";
832
+ //#endregion
833
+ export { VERSION, createHuggingFace, huggingFace, huggingFace as huggingface };
1017
834
 
1018
- // src/version.ts
1019
- var VERSION = true ? "2.0.58" : "0.0.0-test";
1020
- export {
1021
- VERSION,
1022
- createHuggingFace,
1023
- huggingFace,
1024
- huggingFace as huggingface
1025
- };
1026
835
  //# sourceMappingURL=index.js.map