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