@ai-sdk/huggingface 1.0.0-beta.5 → 1.0.0-beta.51

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
@@ -49,6 +49,45 @@ var huggingfaceFailedResponseHandler = (0, import_provider_utils.createJsonError
49
49
  errorToMessage: (data) => data.error.message
50
50
  });
51
51
 
52
+ // src/responses/convert-huggingface-responses-usage.ts
53
+ function convertHuggingFaceResponsesUsage(usage) {
54
+ var _a, _b, _c, _d;
55
+ if (usage == null) {
56
+ return {
57
+ inputTokens: {
58
+ total: void 0,
59
+ noCache: void 0,
60
+ cacheRead: void 0,
61
+ cacheWrite: void 0
62
+ },
63
+ outputTokens: {
64
+ total: void 0,
65
+ text: void 0,
66
+ reasoning: void 0
67
+ },
68
+ raw: void 0
69
+ };
70
+ }
71
+ const inputTokens = usage.input_tokens;
72
+ const outputTokens = usage.output_tokens;
73
+ const cachedTokens = (_b = (_a = usage.input_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
74
+ const reasoningTokens = (_d = (_c = usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
75
+ return {
76
+ inputTokens: {
77
+ total: inputTokens,
78
+ noCache: inputTokens - cachedTokens,
79
+ cacheRead: cachedTokens,
80
+ cacheWrite: void 0
81
+ },
82
+ outputTokens: {
83
+ total: outputTokens,
84
+ text: outputTokens - reasoningTokens,
85
+ reasoning: reasoningTokens
86
+ },
87
+ raw: usage
88
+ };
89
+ }
90
+
52
91
  // src/responses/convert-to-huggingface-responses-messages.ts
53
92
  var import_provider = require("@ai-sdk/provider");
54
93
  async function convertToHuggingFaceResponsesMessages({
@@ -120,10 +159,7 @@ async function convertToHuggingFaceResponsesMessages({
120
159
  break;
121
160
  }
122
161
  case "tool": {
123
- warnings.push({
124
- type: "unsupported-setting",
125
- setting: "tool messages"
126
- });
162
+ warnings.push({ type: "unsupported", feature: "tool messages" });
127
163
  break;
128
164
  }
129
165
  default: {
@@ -174,10 +210,10 @@ function prepareResponsesTools({
174
210
  parameters: tool.inputSchema
175
211
  });
176
212
  break;
177
- case "provider-defined":
213
+ case "provider":
178
214
  toolWarnings.push({
179
- type: "unsupported-tool",
180
- tool
215
+ type: "unsupported",
216
+ feature: `provider-defined tool ${tool.id}`
181
217
  });
182
218
  break;
183
219
  default: {
@@ -247,25 +283,19 @@ var HuggingFaceResponsesLanguageModel = class {
247
283
  var _a, _b;
248
284
  const warnings = [];
249
285
  if (topK != null) {
250
- warnings.push({ type: "unsupported-setting", setting: "topK" });
286
+ warnings.push({ type: "unsupported", feature: "topK" });
251
287
  }
252
288
  if (seed != null) {
253
- warnings.push({ type: "unsupported-setting", setting: "seed" });
289
+ warnings.push({ type: "unsupported", feature: "seed" });
254
290
  }
255
291
  if (presencePenalty != null) {
256
- warnings.push({
257
- type: "unsupported-setting",
258
- setting: "presencePenalty"
259
- });
292
+ warnings.push({ type: "unsupported", feature: "presencePenalty" });
260
293
  }
261
294
  if (frequencyPenalty != null) {
262
- warnings.push({
263
- type: "unsupported-setting",
264
- setting: "frequencyPenalty"
265
- });
295
+ warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
266
296
  }
267
297
  if (stopSequences != null) {
268
- warnings.push({ type: "unsupported-setting", setting: "stopSequences" });
298
+ warnings.push({ type: "unsupported", feature: "stopSequences" });
269
299
  }
270
300
  const { input, warnings: messageWarnings } = await convertToHuggingFaceResponsesMessages({
271
301
  prompt
@@ -306,12 +336,19 @@ var HuggingFaceResponsesLanguageModel = class {
306
336
  metadata: huggingfaceOptions == null ? void 0 : huggingfaceOptions.metadata,
307
337
  instructions: huggingfaceOptions == null ? void 0 : huggingfaceOptions.instructions,
308
338
  ...preparedTools && { tools: preparedTools },
309
- ...preparedToolChoice && { tool_choice: preparedToolChoice }
339
+ ...preparedToolChoice && { tool_choice: preparedToolChoice },
340
+ ...(huggingfaceOptions == null ? void 0 : huggingfaceOptions.reasoningEffort) != null && {
341
+ reasoning: {
342
+ ...(huggingfaceOptions == null ? void 0 : huggingfaceOptions.reasoningEffort) != null && {
343
+ effort: huggingfaceOptions.reasoningEffort
344
+ }
345
+ }
346
+ }
310
347
  };
311
348
  return { args: baseArgs, warnings };
312
349
  }
313
350
  async doGenerate(options) {
314
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
351
+ var _a, _b, _c, _d, _e;
315
352
  const { args, warnings } = await this.getArgs(options);
316
353
  const body = {
317
354
  ...args,
@@ -375,6 +412,20 @@ var HuggingFaceResponsesLanguageModel = class {
375
412
  }
376
413
  break;
377
414
  }
415
+ case "reasoning": {
416
+ for (const contentPart of part.content) {
417
+ content.push({
418
+ type: "reasoning",
419
+ text: contentPart.text,
420
+ providerMetadata: {
421
+ huggingface: {
422
+ itemId: part.id
423
+ }
424
+ }
425
+ });
426
+ }
427
+ break;
428
+ }
378
429
  case "mcp_call": {
379
430
  content.push({
380
431
  type: "tool-call",
@@ -388,8 +439,7 @@ var HuggingFaceResponsesLanguageModel = class {
388
439
  type: "tool-result",
389
440
  toolCallId: part.id,
390
441
  toolName: part.name,
391
- result: part.output,
392
- providerExecuted: true
442
+ result: part.output
393
443
  });
394
444
  }
395
445
  break;
@@ -407,8 +457,7 @@ var HuggingFaceResponsesLanguageModel = class {
407
457
  type: "tool-result",
408
458
  toolCallId: part.id,
409
459
  toolName: "list_tools",
410
- result: { tools: part.tools },
411
- providerExecuted: true
460
+ result: { tools: part.tools }
412
461
  });
413
462
  }
414
463
  break;
@@ -440,11 +489,7 @@ var HuggingFaceResponsesLanguageModel = class {
440
489
  finishReason: mapHuggingFaceResponsesFinishReason(
441
490
  (_e = (_d = response.incomplete_details) == null ? void 0 : _d.reason) != null ? _e : "stop"
442
491
  ),
443
- usage: {
444
- inputTokens: (_g = (_f = response.usage) == null ? void 0 : _f.input_tokens) != null ? _g : 0,
445
- outputTokens: (_i = (_h = response.usage) == null ? void 0 : _h.output_tokens) != null ? _i : 0,
446
- 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)
447
- },
492
+ usage: convertHuggingFaceResponsesUsage(response.usage),
448
493
  request: { body },
449
494
  response: {
450
495
  id: response.id,
@@ -483,11 +528,7 @@ var HuggingFaceResponsesLanguageModel = class {
483
528
  });
484
529
  let finishReason = "unknown";
485
530
  let responseId = null;
486
- const usage = {
487
- inputTokens: void 0,
488
- outputTokens: void 0,
489
- totalTokens: void 0
490
- };
531
+ let usage = void 0;
491
532
  return {
492
533
  stream: response.pipeThrough(
493
534
  new TransformStream({
@@ -495,7 +536,7 @@ var HuggingFaceResponsesLanguageModel = class {
495
536
  controller.enqueue({ type: "stream-start", warnings });
496
537
  },
497
538
  transform(chunk, controller) {
498
- var _a, _b, _c;
539
+ var _a, _b;
499
540
  if (!chunk.success) {
500
541
  finishReason = "error";
501
542
  controller.enqueue({ type: "error", error: chunk.error });
@@ -529,6 +570,16 @@ var HuggingFaceResponsesLanguageModel = class {
529
570
  id: value.item.call_id,
530
571
  toolName: value.item.name
531
572
  });
573
+ } else if (value.item.type === "reasoning") {
574
+ controller.enqueue({
575
+ type: "reasoning-start",
576
+ id: value.item.id,
577
+ providerMetadata: {
578
+ huggingface: {
579
+ itemId: value.item.id
580
+ }
581
+ }
582
+ });
532
583
  }
533
584
  return;
534
585
  }
@@ -566,12 +617,25 @@ var HuggingFaceResponsesLanguageModel = class {
566
617
  (_b = (_a = value.response.incomplete_details) == null ? void 0 : _a.reason) != null ? _b : "stop"
567
618
  );
568
619
  if (value.response.usage) {
569
- usage.inputTokens = value.response.usage.input_tokens;
570
- usage.outputTokens = value.response.usage.output_tokens;
571
- usage.totalTokens = (_c = value.response.usage.total_tokens) != null ? _c : value.response.usage.input_tokens + value.response.usage.output_tokens;
620
+ usage = value.response.usage;
572
621
  }
573
622
  return;
574
623
  }
624
+ if (isReasoningDeltaChunk(value)) {
625
+ controller.enqueue({
626
+ type: "reasoning-delta",
627
+ id: value.item_id,
628
+ delta: value.delta
629
+ });
630
+ return;
631
+ }
632
+ if (isReasoningEndChunk(value)) {
633
+ controller.enqueue({
634
+ type: "reasoning-end",
635
+ id: value.item_id
636
+ });
637
+ return;
638
+ }
575
639
  if (isTextDeltaChunk(value)) {
576
640
  controller.enqueue({
577
641
  type: "text-delta",
@@ -585,7 +649,7 @@ var HuggingFaceResponsesLanguageModel = class {
585
649
  controller.enqueue({
586
650
  type: "finish",
587
651
  finishReason,
588
- usage,
652
+ usage: convertHuggingFaceResponsesUsage(usage),
589
653
  providerMetadata: {
590
654
  huggingface: {
591
655
  responseId
@@ -603,8 +667,65 @@ var HuggingFaceResponsesLanguageModel = class {
603
667
  var huggingfaceResponsesProviderOptionsSchema = import_v42.z.object({
604
668
  metadata: import_v42.z.record(import_v42.z.string(), import_v42.z.string()).optional(),
605
669
  instructions: import_v42.z.string().optional(),
606
- strictJsonSchema: import_v42.z.boolean().optional()
670
+ strictJsonSchema: import_v42.z.boolean().optional(),
671
+ reasoningEffort: import_v42.z.string().optional()
607
672
  });
673
+ var huggingfaceResponsesOutputSchema = import_v42.z.discriminatedUnion("type", [
674
+ import_v42.z.object({
675
+ type: import_v42.z.literal("message"),
676
+ id: import_v42.z.string(),
677
+ role: import_v42.z.string().optional(),
678
+ status: import_v42.z.string().optional(),
679
+ content: import_v42.z.array(
680
+ import_v42.z.object({
681
+ type: import_v42.z.literal("output_text"),
682
+ text: import_v42.z.string(),
683
+ annotations: import_v42.z.array(import_v42.z.any()).optional()
684
+ })
685
+ )
686
+ }),
687
+ import_v42.z.object({
688
+ type: import_v42.z.literal("reasoning"),
689
+ id: import_v42.z.string(),
690
+ status: import_v42.z.string().optional(),
691
+ content: import_v42.z.array(
692
+ import_v42.z.object({
693
+ type: import_v42.z.literal("reasoning_text"),
694
+ text: import_v42.z.string()
695
+ })
696
+ ),
697
+ summary: import_v42.z.array(
698
+ import_v42.z.object({
699
+ type: import_v42.z.literal("reasoning_summary"),
700
+ text: import_v42.z.string()
701
+ }).optional()
702
+ ).optional()
703
+ }),
704
+ import_v42.z.object({
705
+ type: import_v42.z.literal("function_call"),
706
+ id: import_v42.z.string(),
707
+ call_id: import_v42.z.string(),
708
+ name: import_v42.z.string(),
709
+ arguments: import_v42.z.string(),
710
+ output: import_v42.z.string().optional(),
711
+ status: import_v42.z.string().optional()
712
+ }),
713
+ import_v42.z.object({
714
+ type: import_v42.z.literal("mcp_call"),
715
+ id: import_v42.z.string(),
716
+ name: import_v42.z.string(),
717
+ arguments: import_v42.z.string(),
718
+ output: import_v42.z.string().optional(),
719
+ status: import_v42.z.string().optional()
720
+ }),
721
+ import_v42.z.object({
722
+ type: import_v42.z.literal("mcp_list_tools"),
723
+ id: import_v42.z.string(),
724
+ server_label: import_v42.z.string(),
725
+ tools: import_v42.z.array(import_v42.z.any()).optional(),
726
+ status: import_v42.z.string().optional()
727
+ })
728
+ ]);
608
729
  var huggingfaceResponsesResponseSchema = import_v42.z.object({
609
730
  id: import_v42.z.string(),
610
731
  model: import_v42.z.string(),
@@ -633,7 +754,7 @@ var huggingfaceResponsesResponseSchema = import_v42.z.object({
633
754
  }).optional(),
634
755
  total_tokens: import_v42.z.number()
635
756
  }).nullable().optional(),
636
- output: import_v42.z.array(import_v42.z.any()),
757
+ output: import_v42.z.array(huggingfaceResponsesOutputSchema),
637
758
  output_text: import_v42.z.string().nullable().optional()
638
759
  });
639
760
  var responseOutputItemAddedSchema = import_v42.z.object({
@@ -647,6 +768,13 @@ var responseOutputItemAddedSchema = import_v42.z.object({
647
768
  status: import_v42.z.string().optional(),
648
769
  content: import_v42.z.array(import_v42.z.any()).optional()
649
770
  }),
771
+ import_v42.z.object({
772
+ type: import_v42.z.literal("reasoning"),
773
+ id: import_v42.z.string(),
774
+ status: import_v42.z.string().optional(),
775
+ content: import_v42.z.array(import_v42.z.any()).optional(),
776
+ summary: import_v42.z.array(import_v42.z.any()).optional()
777
+ }),
650
778
  import_v42.z.object({
651
779
  type: import_v42.z.literal("mcp_list_tools"),
652
780
  id: import_v42.z.string(),
@@ -710,6 +838,13 @@ var responseOutputItemDoneSchema = import_v42.z.object({
710
838
  arguments: import_v42.z.string(),
711
839
  output: import_v42.z.string().optional(),
712
840
  error: import_v42.z.string().optional()
841
+ }),
842
+ import_v42.z.object({
843
+ type: import_v42.z.literal("reasoning"),
844
+ id: import_v42.z.string(),
845
+ status: import_v42.z.string().optional(),
846
+ content: import_v42.z.array(import_v42.z.any()).optional(),
847
+ summary: import_v42.z.array(import_v42.z.any()).optional()
713
848
  })
714
849
  ]),
715
850
  sequence_number: import_v42.z.number()
@@ -722,6 +857,22 @@ var textDeltaChunkSchema = import_v42.z.object({
722
857
  delta: import_v42.z.string(),
723
858
  sequence_number: import_v42.z.number()
724
859
  });
860
+ var reasoningTextDeltaChunkSchema = import_v42.z.object({
861
+ type: import_v42.z.literal("response.reasoning_text.delta"),
862
+ item_id: import_v42.z.string(),
863
+ output_index: import_v42.z.number(),
864
+ content_index: import_v42.z.number(),
865
+ delta: import_v42.z.string(),
866
+ sequence_number: import_v42.z.number()
867
+ });
868
+ var reasoningTextEndChunkSchema = import_v42.z.object({
869
+ type: import_v42.z.literal("response.reasoning_text.done"),
870
+ item_id: import_v42.z.string(),
871
+ output_index: import_v42.z.number(),
872
+ content_index: import_v42.z.number(),
873
+ text: import_v42.z.string(),
874
+ sequence_number: import_v42.z.number()
875
+ });
725
876
  var responseCompletedChunkSchema = import_v42.z.object({
726
877
  type: import_v42.z.literal("response.completed"),
727
878
  response: huggingfaceResponsesResponseSchema,
@@ -740,6 +891,8 @@ var responseCreatedChunkSchema = import_v42.z.object({
740
891
  var huggingfaceResponsesChunkSchema = import_v42.z.union([
741
892
  responseOutputItemAddedSchema,
742
893
  responseOutputItemDoneSchema,
894
+ reasoningTextDeltaChunkSchema,
895
+ reasoningTextEndChunkSchema,
743
896
  textDeltaChunkSchema,
744
897
  responseCompletedChunkSchema,
745
898
  responseCreatedChunkSchema,
@@ -755,6 +908,12 @@ function isResponseOutputItemDoneChunk(chunk) {
755
908
  function isTextDeltaChunk(chunk) {
756
909
  return chunk.type === "response.output_text.delta";
757
910
  }
911
+ function isReasoningDeltaChunk(chunk) {
912
+ return chunk.type === "response.reasoning_text.delta";
913
+ }
914
+ function isReasoningEndChunk(chunk) {
915
+ return chunk.type === "response.reasoning_text.done";
916
+ }
758
917
  function isResponseCompletedChunk(chunk) {
759
918
  return chunk.type === "response.completed";
760
919
  }
@@ -785,15 +944,17 @@ function createHuggingFace(options = {}) {
785
944
  });
786
945
  };
787
946
  const provider = (modelId) => createResponsesModel(modelId);
947
+ provider.specificationVersion = "v3";
788
948
  provider.languageModel = createResponsesModel;
789
949
  provider.responses = createResponsesModel;
790
- provider.textEmbeddingModel = (modelId) => {
950
+ provider.embeddingModel = (modelId) => {
791
951
  throw new import_provider3.NoSuchModelError({
792
952
  modelId,
793
- modelType: "textEmbeddingModel",
953
+ modelType: "embeddingModel",
794
954
  message: "Hugging Face Responses API does not support text embeddings. Use the Hugging Face Inference API directly for embeddings."
795
955
  });
796
956
  };
957
+ provider.textEmbeddingModel = provider.embeddingModel;
797
958
  provider.imageModel = (modelId) => {
798
959
  throw new import_provider3.NoSuchModelError({
799
960
  modelId,