@ai-sdk/huggingface 2.0.0-beta.3 → 2.0.0-beta.31

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