@ai-sdk/huggingface 0.0.1

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