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