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