@bowenqt/qiniu-ai-sdk 0.3.1 → 0.5.0
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/README.md +194 -15
- package/dist/adapter/convert-prompt.d.ts +7 -0
- package/dist/adapter/convert-prompt.d.ts.map +1 -0
- package/dist/adapter/convert-prompt.js +207 -0
- package/dist/adapter/convert-prompt.js.map +1 -0
- package/dist/adapter/convert-prompt.mjs +201 -0
- package/dist/adapter/index.d.ts +4 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/index.js.map +1 -0
- package/dist/adapter/index.mjs +3 -0
- package/dist/adapter/provider.d.ts +14 -0
- package/dist/adapter/provider.d.ts.map +1 -0
- package/dist/adapter/provider.js +36 -0
- package/dist/adapter/provider.js.map +1 -0
- package/dist/adapter/provider.mjs +32 -0
- package/dist/adapter/qiniu-chat-model.d.ts +27 -0
- package/dist/adapter/qiniu-chat-model.d.ts.map +1 -0
- package/dist/adapter/qiniu-chat-model.js +207 -0
- package/dist/adapter/qiniu-chat-model.js.map +1 -0
- package/dist/adapter/qiniu-chat-model.mjs +203 -0
- package/dist/adapter/types.d.ts +2 -0
- package/dist/adapter/types.d.ts.map +1 -0
- package/dist/adapter/types.js +3 -0
- package/dist/adapter/types.js.map +1 -0
- package/dist/adapter/types.mjs +2 -0
- package/dist/adapter/utils.d.ts +2 -0
- package/dist/adapter/utils.d.ts.map +1 -0
- package/dist/adapter/utils.js +26 -0
- package/dist/adapter/utils.js.map +1 -0
- package/dist/adapter/utils.mjs +23 -0
- package/dist/client.d.ts +9 -9
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +18 -18
- package/dist/client.js.map +1 -1
- package/dist/client.mjs +9 -9
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/models.d.ts +115 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +150 -0
- package/dist/models.js.map +1 -0
- package/dist/models.mjs +147 -0
- package/dist/modules/account/index.d.ts +54 -71
- package/dist/modules/account/index.d.ts.map +1 -1
- package/dist/modules/account/index.js +110 -43
- package/dist/modules/account/index.js.map +1 -1
- package/dist/modules/account/index.mjs +110 -43
- package/dist/modules/asr/index.d.ts.map +1 -1
- package/dist/modules/asr/index.js +16 -6
- package/dist/modules/asr/index.js.map +1 -1
- package/dist/modules/asr/index.mjs +16 -6
- package/dist/modules/image/index.d.ts +55 -1
- package/dist/modules/image/index.d.ts.map +1 -1
- package/dist/modules/image/index.js +21 -0
- package/dist/modules/image/index.js.map +1 -1
- package/dist/modules/image/index.mjs +21 -0
- package/dist/modules/ocr/index.d.ts +4 -0
- package/dist/modules/ocr/index.d.ts.map +1 -1
- package/dist/modules/ocr/index.js +3 -2
- package/dist/modules/ocr/index.js.map +1 -1
- package/dist/modules/ocr/index.mjs +3 -2
- package/dist/modules/tts/index.d.ts +1 -0
- package/dist/modules/tts/index.d.ts.map +1 -1
- package/dist/modules/tts/index.js +172 -50
- package/dist/modules/tts/index.js.map +1 -1
- package/dist/modules/tts/index.mjs +139 -50
- package/dist/modules/video/index.d.ts +176 -6
- package/dist/modules/video/index.d.ts.map +1 -1
- package/dist/modules/video/index.js +325 -5
- package/dist/modules/video/index.js.map +1 -1
- package/dist/modules/video/index.mjs +325 -5
- package/package.json +15 -1
package/README.md
CHANGED
|
@@ -72,6 +72,32 @@ for await (const chunk of stream) {
|
|
|
72
72
|
}
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
+
## Vercel AI SDK Adapter
|
|
76
|
+
|
|
77
|
+
Use the adapter to integrate with the Vercel AI SDK (`streamText`, `generateText`).
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { createQiniu } from '@bowenqt/qiniu-ai-sdk/adapter';
|
|
81
|
+
import { streamText } from 'ai';
|
|
82
|
+
|
|
83
|
+
const qiniu = createQiniu({
|
|
84
|
+
apiKey: process.env.QINIU_API_KEY || process.env.OPENAI_API_KEY,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const { textStream } = await streamText({
|
|
88
|
+
model: qiniu.languageModel('gemini-2.5-flash'),
|
|
89
|
+
prompt: 'Introduce Qiniu Cloud in one sentence.',
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
for await (const text of textStream) {
|
|
93
|
+
process.stdout.write(text);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Notes:
|
|
98
|
+
- If you already have a `QiniuAI` client, pass it via `createQiniu({ client })`.
|
|
99
|
+
- You can override `baseUrl` using `createQiniu({ baseUrl })` or `QINIU_BASE_URL`.
|
|
100
|
+
|
|
75
101
|
## API Reference
|
|
76
102
|
|
|
77
103
|
### Client Initialization
|
|
@@ -223,22 +249,189 @@ for await (const chunk of stream) {
|
|
|
223
249
|
#### `client.image`
|
|
224
250
|
|
|
225
251
|
- `create(params: ImageGenerationRequest): Promise<{ task_id: string }>`
|
|
252
|
+
- `edit(params: ImageEditRequest): Promise<ImageEditResponse>`
|
|
226
253
|
- `get(taskId: string): Promise<ImageTaskResponse>`
|
|
227
254
|
- `waitForCompletion(taskId: string, options?: WaitOptions): Promise<ImageTaskResponse>`
|
|
228
255
|
|
|
256
|
+
**Image Edit (Kling/Gemini):**
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
// Kling multi-image edit
|
|
260
|
+
const editTask = await client.image.edit({
|
|
261
|
+
model: 'kling-v1',
|
|
262
|
+
prompt: 'Make it watercolor style',
|
|
263
|
+
image_reference: 'subject',
|
|
264
|
+
subject_image_list: [{ image: 'https://example.com/subject.jpg', image_type: 'subject' }],
|
|
265
|
+
scene_image: { image: 'https://example.com/scene.jpg', image_type: 'scene' },
|
|
266
|
+
style_image: { image: 'https://example.com/style.jpg', image_type: 'style' },
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// Gemini edit
|
|
270
|
+
const geminiEdit = await client.image.edit({
|
|
271
|
+
model: 'gemini-3.0-pro-image-preview',
|
|
272
|
+
prompt: 'Add a sunset sky',
|
|
273
|
+
image_url: 'https://example.com/input.png',
|
|
274
|
+
image_config: { aspect_ratio: '16:9', image_size: '2K' },
|
|
275
|
+
mask: 'base64-mask-data',
|
|
276
|
+
});
|
|
277
|
+
```
|
|
278
|
+
|
|
229
279
|
#### `client.video`
|
|
230
280
|
|
|
231
281
|
- `create(params: VideoGenerationRequest): Promise<{ id: string }>`
|
|
232
282
|
- `get(id: string): Promise<VideoTaskResponse>`
|
|
283
|
+
- `remix(id: string, params: VideoRemixRequest): Promise<{ id: string }>`
|
|
233
284
|
- `waitForCompletion(id: string, options?: WaitOptions): Promise<VideoTaskResponse>`
|
|
234
285
|
|
|
286
|
+
**First & Last Frame Video Generation:**
|
|
287
|
+
|
|
288
|
+
The SDK provides a unified `frames` parameter that works across all models (Kling, Veo):
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
// Kling first/last frame (multi-frame generation)
|
|
292
|
+
const klingTask = await client.video.create({
|
|
293
|
+
model: 'kling-video-o1',
|
|
294
|
+
prompt: '视频连贯在一起',
|
|
295
|
+
frames: {
|
|
296
|
+
first: { url: 'https://example.com/start.jpg' },
|
|
297
|
+
last: { url: 'https://example.com/end.jpg' }
|
|
298
|
+
},
|
|
299
|
+
size: '1920x1080',
|
|
300
|
+
mode: 'pro'
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// Veo first/last frame
|
|
304
|
+
const veoTask = await client.video.create({
|
|
305
|
+
model: 'veo-2.0-generate-001',
|
|
306
|
+
prompt: 'A cat jumping from chair to table',
|
|
307
|
+
frames: {
|
|
308
|
+
first: { url: 'https://example.com/cat-chair.jpg' },
|
|
309
|
+
last: { url: 'https://example.com/cat-table.jpg' }
|
|
310
|
+
},
|
|
311
|
+
generate_audio: true,
|
|
312
|
+
resolution: '720p',
|
|
313
|
+
seed: 12345,
|
|
314
|
+
sample_count: 1
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// Wait for completion (works with both Kling and Veo)
|
|
318
|
+
const result = await client.video.waitForCompletion(veoTask.id);
|
|
319
|
+
console.log(result.task_result?.videos[0].url);
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
**Video Reference Generation (Kling):**
|
|
323
|
+
|
|
324
|
+
```typescript
|
|
325
|
+
const task = await client.video.create({
|
|
326
|
+
model: 'kling-video-o1',
|
|
327
|
+
prompt: '融合视频风格生成新内容',
|
|
328
|
+
video_list: [{
|
|
329
|
+
video_url: 'https://example.com/reference.mp4',
|
|
330
|
+
refer_type: 'base',
|
|
331
|
+
keep_original_sound: 'yes'
|
|
332
|
+
}]
|
|
333
|
+
});
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
**Kling Native Parameters:**
|
|
337
|
+
|
|
338
|
+
For backward compatibility, you can also use Kling's native parameters directly:
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
// Using image_list directly (kling-video-o1)
|
|
342
|
+
const task = await client.video.create({
|
|
343
|
+
model: 'kling-video-o1',
|
|
344
|
+
prompt: '...',
|
|
345
|
+
image_list: [
|
|
346
|
+
{ image: 'https://...', type: 'first_frame' },
|
|
347
|
+
{ image: 'https://...', type: 'end_frame' }
|
|
348
|
+
]
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// Using image_tail (kling-v2-5-turbo)
|
|
352
|
+
const task = await client.video.create({
|
|
353
|
+
model: 'kling-v2-5-turbo',
|
|
354
|
+
prompt: '...',
|
|
355
|
+
input_reference: 'https://example.com/start.jpg',
|
|
356
|
+
image_tail: 'https://example.com/end.jpg'
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
**Video Remix (Sora):**
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
const remixTask = await client.video.remix('videos-123...', {
|
|
364
|
+
prompt: 'Make it cinematic',
|
|
365
|
+
});
|
|
366
|
+
console.log(remixTask.id);
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### `client.ocr`
|
|
370
|
+
|
|
371
|
+
- `detect(params: OcrRequest): Promise<OcrResponse>`
|
|
372
|
+
|
|
373
|
+
**OCR Example:**
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
const ocrResult = await client.ocr.detect({
|
|
377
|
+
url: 'https://static.qiniu.com/ai-inference/example-resources/ocr-example.png',
|
|
378
|
+
});
|
|
379
|
+
console.log(ocrResult.text);
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
#### `client.asr`
|
|
383
|
+
|
|
384
|
+
- `transcribe(params: AsrRequest): Promise<AsrResponse>`
|
|
385
|
+
|
|
386
|
+
**ASR Example:**
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
const asrResult = await client.asr.transcribe({
|
|
390
|
+
audio: {
|
|
391
|
+
format: 'mp3',
|
|
392
|
+
url: 'https://static.qiniu.com/ai-inference/example-resources/example.mp3',
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
console.log(asrResult.text);
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
#### `client.account`
|
|
399
|
+
|
|
400
|
+
- `usage(params: UsageQuery): Promise<UsageResponse>`
|
|
401
|
+
|
|
402
|
+
**Account Usage Example (API Key):**
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
const usage = await client.account.usage({
|
|
406
|
+
granularity: 'day',
|
|
407
|
+
start: '2024-01-01T00:00:00+08:00',
|
|
408
|
+
end: '2024-01-31T23:59:59+08:00',
|
|
409
|
+
});
|
|
410
|
+
console.log(usage.data.length);
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
**Account Usage Example (AK/SK):**
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
const usage = await client.account.usage({
|
|
417
|
+
granularity: 'day',
|
|
418
|
+
start: '2024-01-01T00:00:00+08:00',
|
|
419
|
+
end: '2024-01-31T23:59:59+08:00',
|
|
420
|
+
auth: {
|
|
421
|
+
accessKey: 'your-ak',
|
|
422
|
+
secretKey: 'your-sk',
|
|
423
|
+
},
|
|
424
|
+
});
|
|
425
|
+
console.log(usage.data.length);
|
|
426
|
+
```
|
|
427
|
+
|
|
235
428
|
#### `client.sys`
|
|
236
429
|
|
|
237
430
|
- `search(params: WebSearchRequest): Promise<WebSearchResult[]>`
|
|
238
431
|
|
|
239
432
|
### Advanced Usage: Generic API Access
|
|
240
433
|
|
|
241
|
-
For features not yet fully wrapped in modules
|
|
434
|
+
For features not yet fully wrapped in modules, use the generic `post` and `get` methods.
|
|
242
435
|
|
|
243
436
|
**OCR (Optical Character Recognition):**
|
|
244
437
|
|
|
@@ -263,20 +456,6 @@ const res = await client.post<any>('/voice/tts', {
|
|
|
263
456
|
});
|
|
264
457
|
```
|
|
265
458
|
|
|
266
|
-
**Veo Video Generation (First & Last Frame):**
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
// Veo models require a specific structure for first/last frame
|
|
270
|
-
const veoTask = await client.post<{ id: string }>('/v1/videos/generations', {
|
|
271
|
-
model: 'veo-2.0-generate-001',
|
|
272
|
-
instances: [{
|
|
273
|
-
prompt: "A video of a cat jumping",
|
|
274
|
-
image: { gcsUri: "gs://..." }, // First frame
|
|
275
|
-
lastFrame: { gcsUri: "gs://..." } // Last frame
|
|
276
|
-
}]
|
|
277
|
-
});
|
|
278
|
-
```
|
|
279
|
-
|
|
280
459
|
### Wait Options
|
|
281
460
|
|
|
282
461
|
For `waitForCompletion` methods:
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ChatCompletionRequest, ChatMessage } from '../lib/types';
|
|
2
|
+
import type { LanguageModelV2FinishReason, LanguageModelV2FunctionTool, LanguageModelV2Prompt, LanguageModelV2ProviderDefinedTool, LanguageModelV2ToolChoice } from './types';
|
|
3
|
+
export declare function convertPromptToMessages(prompt: LanguageModelV2Prompt): ChatMessage[];
|
|
4
|
+
export declare function mapFinishReason(reason?: string | null): LanguageModelV2FinishReason;
|
|
5
|
+
export declare function convertTools(tools?: Array<LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool>): ChatCompletionRequest['tools'] | undefined;
|
|
6
|
+
export declare function convertToolChoice(choice?: LanguageModelV2ToolChoice): ChatCompletionRequest['tool_choice'] | undefined;
|
|
7
|
+
//# sourceMappingURL=convert-prompt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convert-prompt.d.ts","sourceRoot":"","sources":["../../src/adapter/convert-prompt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,WAAW,EAAyB,MAAM,cAAc,CAAC;AAC9F,OAAO,KAAK,EAER,2BAA2B,EAC3B,2BAA2B,EAE3B,qBAAqB,EACrB,kCAAkC,EAGlC,yBAAyB,EAG5B,MAAM,SAAS,CAAC;AAEjB,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,qBAAqB,GAAG,WAAW,EAAE,CA4BpF;AAED,wBAAgB,eAAe,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,2BAA2B,CAanF;AAED,wBAAgB,YAAY,CACxB,KAAK,CAAC,EAAE,KAAK,CAAC,2BAA2B,GAAG,kCAAkC,CAAC,GAChF,qBAAqB,CAAC,OAAO,CAAC,GAAG,SAAS,CAmB5C;AAED,wBAAgB,iBAAiB,CAAC,MAAM,CAAC,EAAE,yBAAyB,GAAG,qBAAqB,CAAC,aAAa,CAAC,GAAG,SAAS,CAsBtH"}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertPromptToMessages = convertPromptToMessages;
|
|
4
|
+
exports.mapFinishReason = mapFinishReason;
|
|
5
|
+
exports.convertTools = convertTools;
|
|
6
|
+
exports.convertToolChoice = convertToolChoice;
|
|
7
|
+
function convertPromptToMessages(prompt) {
|
|
8
|
+
const messages = [];
|
|
9
|
+
for (const message of prompt) {
|
|
10
|
+
if (message.role === 'system') {
|
|
11
|
+
messages.push({ role: 'system', content: message.content });
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (message.role === 'tool') {
|
|
15
|
+
messages.push(...convertToolResultMessages(message));
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const { content, toolCalls } = convertAssistantOrUserMessage(message);
|
|
19
|
+
const normalized = {
|
|
20
|
+
role: message.role,
|
|
21
|
+
content,
|
|
22
|
+
};
|
|
23
|
+
if (toolCalls.length > 0) {
|
|
24
|
+
normalized.tool_calls = toolCalls;
|
|
25
|
+
}
|
|
26
|
+
messages.push(normalized);
|
|
27
|
+
}
|
|
28
|
+
return messages;
|
|
29
|
+
}
|
|
30
|
+
function mapFinishReason(reason) {
|
|
31
|
+
switch (reason) {
|
|
32
|
+
case 'stop':
|
|
33
|
+
return 'stop';
|
|
34
|
+
case 'length':
|
|
35
|
+
return 'length';
|
|
36
|
+
case 'tool_calls':
|
|
37
|
+
return 'tool-calls';
|
|
38
|
+
case 'content_filter':
|
|
39
|
+
return 'content-filter';
|
|
40
|
+
default:
|
|
41
|
+
return 'unknown';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function convertTools(tools) {
|
|
45
|
+
if (!tools || tools.length === 0) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
const functionTools = tools.filter((tool) => tool.type === 'function');
|
|
49
|
+
if (functionTools.length === 0) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
return functionTools.map((tool) => ({
|
|
53
|
+
type: 'function',
|
|
54
|
+
function: {
|
|
55
|
+
name: tool.name,
|
|
56
|
+
description: tool.description,
|
|
57
|
+
parameters: tool.parameters,
|
|
58
|
+
},
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
function convertToolChoice(choice) {
|
|
62
|
+
if (!choice) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
if (choice.type === 'auto') {
|
|
66
|
+
return 'auto';
|
|
67
|
+
}
|
|
68
|
+
if (choice.type === 'none') {
|
|
69
|
+
return 'none';
|
|
70
|
+
}
|
|
71
|
+
if (choice.type === 'required') {
|
|
72
|
+
return 'auto';
|
|
73
|
+
}
|
|
74
|
+
if (choice.type === 'tool') {
|
|
75
|
+
return { type: 'function', function: { name: choice.toolName } };
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
function convertAssistantOrUserMessage(message) {
|
|
80
|
+
const parts = [];
|
|
81
|
+
const toolCalls = [];
|
|
82
|
+
for (const part of message.content) {
|
|
83
|
+
if (part.type === 'text') {
|
|
84
|
+
parts.push({ type: 'text', text: part.text });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (part.type === 'reasoning') {
|
|
88
|
+
parts.push({ type: 'text', text: part.text });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (part.type === 'file') {
|
|
92
|
+
const converted = convertFilePart(part);
|
|
93
|
+
if (converted) {
|
|
94
|
+
parts.push(converted);
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (part.type === 'tool-call') {
|
|
99
|
+
toolCalls.push(convertToolCall(part));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const content = mergeTextParts(parts);
|
|
103
|
+
return { content, toolCalls };
|
|
104
|
+
}
|
|
105
|
+
function convertToolResultMessages(message) {
|
|
106
|
+
const toolMessages = [];
|
|
107
|
+
for (const part of message.content) {
|
|
108
|
+
if (part.type !== 'tool-result') {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const content = stringifyToolResult(part);
|
|
112
|
+
toolMessages.push({
|
|
113
|
+
role: 'tool',
|
|
114
|
+
content,
|
|
115
|
+
name: part.toolName,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return toolMessages;
|
|
119
|
+
}
|
|
120
|
+
function convertFilePart(part) {
|
|
121
|
+
const mediaType = part.mediaType.toLowerCase();
|
|
122
|
+
if (!mediaType.startsWith('image/')) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const url = resolveFileDataUrl(part);
|
|
126
|
+
if (!url) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
type: 'image_url',
|
|
131
|
+
image_url: {
|
|
132
|
+
url,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function resolveFileDataUrl(part) {
|
|
137
|
+
const data = part.data;
|
|
138
|
+
if (data instanceof URL) {
|
|
139
|
+
return data.toString();
|
|
140
|
+
}
|
|
141
|
+
if (typeof data === 'string') {
|
|
142
|
+
if (/^https?:/i.test(data) || /^data:/i.test(data)) {
|
|
143
|
+
return data;
|
|
144
|
+
}
|
|
145
|
+
return `data:${part.mediaType};base64,${data}`;
|
|
146
|
+
}
|
|
147
|
+
if (data instanceof Uint8Array) {
|
|
148
|
+
const base64 = encodeBase64(data);
|
|
149
|
+
return `data:${part.mediaType};base64,${base64}`;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
function encodeBase64(data) {
|
|
154
|
+
if (typeof Buffer !== 'undefined') {
|
|
155
|
+
return Buffer.from(data).toString('base64');
|
|
156
|
+
}
|
|
157
|
+
let binary = '';
|
|
158
|
+
for (const byte of data) {
|
|
159
|
+
binary += String.fromCharCode(byte);
|
|
160
|
+
}
|
|
161
|
+
const btoaFn = globalThis.btoa;
|
|
162
|
+
if (btoaFn) {
|
|
163
|
+
return btoaFn(binary);
|
|
164
|
+
}
|
|
165
|
+
throw new Error('Base64 encoding is not available in this environment.');
|
|
166
|
+
}
|
|
167
|
+
function convertToolCall(part) {
|
|
168
|
+
return {
|
|
169
|
+
id: part.toolCallId,
|
|
170
|
+
type: 'function',
|
|
171
|
+
function: {
|
|
172
|
+
name: part.toolName,
|
|
173
|
+
arguments: JSON.stringify(part.args ?? {}),
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function stringifyToolResult(part) {
|
|
178
|
+
if (Array.isArray(part.content) && part.content.length > 0) {
|
|
179
|
+
return part.content
|
|
180
|
+
.map((contentPart) => {
|
|
181
|
+
if (contentPart.type === 'text') {
|
|
182
|
+
return contentPart.text;
|
|
183
|
+
}
|
|
184
|
+
if (contentPart.type === 'image') {
|
|
185
|
+
const mediaType = contentPart.mediaType || 'image/png';
|
|
186
|
+
return `data:${mediaType};base64,${contentPart.data}`;
|
|
187
|
+
}
|
|
188
|
+
return '';
|
|
189
|
+
})
|
|
190
|
+
.join('');
|
|
191
|
+
}
|
|
192
|
+
if (part.result !== undefined) {
|
|
193
|
+
return JSON.stringify(part.result);
|
|
194
|
+
}
|
|
195
|
+
return '';
|
|
196
|
+
}
|
|
197
|
+
function mergeTextParts(parts) {
|
|
198
|
+
if (parts.length === 0) {
|
|
199
|
+
return '';
|
|
200
|
+
}
|
|
201
|
+
const hasNonText = parts.some((part) => part.type !== 'text');
|
|
202
|
+
if (hasNonText) {
|
|
203
|
+
return parts;
|
|
204
|
+
}
|
|
205
|
+
return parts.map((part) => (part.type === 'text' ? part.text ?? '' : '')).join('');
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=convert-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convert-prompt.js","sourceRoot":"","sources":["../../src/adapter/convert-prompt.ts"],"names":[],"mappings":";;AAeA,0DA4BC;AAED,0CAaC;AAED,oCAqBC;AAED,8CAsBC;AA1FD,SAAgB,uBAAuB,CAAC,MAA6B;IACjE,MAAM,QAAQ,GAAkB,EAAE,CAAC;IAEnC,KAAK,MAAM,OAAO,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC5B,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO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C;AAED,SAAS,cAAc,CAAC,KAAoB;IACxC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,EAAE,CAAC;IACd,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC;IAC9D,IAAI,UAAU,EAAE,CAAC;QACb,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACvF,CAAC"}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
export function convertPromptToMessages(prompt) {
|
|
2
|
+
const messages = [];
|
|
3
|
+
for (const message of prompt) {
|
|
4
|
+
if (message.role === 'system') {
|
|
5
|
+
messages.push({ role: 'system', content: message.content });
|
|
6
|
+
continue;
|
|
7
|
+
}
|
|
8
|
+
if (message.role === 'tool') {
|
|
9
|
+
messages.push(...convertToolResultMessages(message));
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
const { content, toolCalls } = convertAssistantOrUserMessage(message);
|
|
13
|
+
const normalized = {
|
|
14
|
+
role: message.role,
|
|
15
|
+
content,
|
|
16
|
+
};
|
|
17
|
+
if (toolCalls.length > 0) {
|
|
18
|
+
normalized.tool_calls = toolCalls;
|
|
19
|
+
}
|
|
20
|
+
messages.push(normalized);
|
|
21
|
+
}
|
|
22
|
+
return messages;
|
|
23
|
+
}
|
|
24
|
+
export function mapFinishReason(reason) {
|
|
25
|
+
switch (reason) {
|
|
26
|
+
case 'stop':
|
|
27
|
+
return 'stop';
|
|
28
|
+
case 'length':
|
|
29
|
+
return 'length';
|
|
30
|
+
case 'tool_calls':
|
|
31
|
+
return 'tool-calls';
|
|
32
|
+
case 'content_filter':
|
|
33
|
+
return 'content-filter';
|
|
34
|
+
default:
|
|
35
|
+
return 'unknown';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function convertTools(tools) {
|
|
39
|
+
if (!tools || tools.length === 0) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
const functionTools = tools.filter((tool) => tool.type === 'function');
|
|
43
|
+
if (functionTools.length === 0) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return functionTools.map((tool) => ({
|
|
47
|
+
type: 'function',
|
|
48
|
+
function: {
|
|
49
|
+
name: tool.name,
|
|
50
|
+
description: tool.description,
|
|
51
|
+
parameters: tool.parameters,
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
export function convertToolChoice(choice) {
|
|
56
|
+
if (!choice) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
if (choice.type === 'auto') {
|
|
60
|
+
return 'auto';
|
|
61
|
+
}
|
|
62
|
+
if (choice.type === 'none') {
|
|
63
|
+
return 'none';
|
|
64
|
+
}
|
|
65
|
+
if (choice.type === 'required') {
|
|
66
|
+
return 'auto';
|
|
67
|
+
}
|
|
68
|
+
if (choice.type === 'tool') {
|
|
69
|
+
return { type: 'function', function: { name: choice.toolName } };
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
function convertAssistantOrUserMessage(message) {
|
|
74
|
+
const parts = [];
|
|
75
|
+
const toolCalls = [];
|
|
76
|
+
for (const part of message.content) {
|
|
77
|
+
if (part.type === 'text') {
|
|
78
|
+
parts.push({ type: 'text', text: part.text });
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (part.type === 'reasoning') {
|
|
82
|
+
parts.push({ type: 'text', text: part.text });
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (part.type === 'file') {
|
|
86
|
+
const converted = convertFilePart(part);
|
|
87
|
+
if (converted) {
|
|
88
|
+
parts.push(converted);
|
|
89
|
+
}
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (part.type === 'tool-call') {
|
|
93
|
+
toolCalls.push(convertToolCall(part));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const content = mergeTextParts(parts);
|
|
97
|
+
return { content, toolCalls };
|
|
98
|
+
}
|
|
99
|
+
function convertToolResultMessages(message) {
|
|
100
|
+
const toolMessages = [];
|
|
101
|
+
for (const part of message.content) {
|
|
102
|
+
if (part.type !== 'tool-result') {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const content = stringifyToolResult(part);
|
|
106
|
+
toolMessages.push({
|
|
107
|
+
role: 'tool',
|
|
108
|
+
content,
|
|
109
|
+
name: part.toolName,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return toolMessages;
|
|
113
|
+
}
|
|
114
|
+
function convertFilePart(part) {
|
|
115
|
+
const mediaType = part.mediaType.toLowerCase();
|
|
116
|
+
if (!mediaType.startsWith('image/')) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
const url = resolveFileDataUrl(part);
|
|
120
|
+
if (!url) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
type: 'image_url',
|
|
125
|
+
image_url: {
|
|
126
|
+
url,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function resolveFileDataUrl(part) {
|
|
131
|
+
const data = part.data;
|
|
132
|
+
if (data instanceof URL) {
|
|
133
|
+
return data.toString();
|
|
134
|
+
}
|
|
135
|
+
if (typeof data === 'string') {
|
|
136
|
+
if (/^https?:/i.test(data) || /^data:/i.test(data)) {
|
|
137
|
+
return data;
|
|
138
|
+
}
|
|
139
|
+
return `data:${part.mediaType};base64,${data}`;
|
|
140
|
+
}
|
|
141
|
+
if (data instanceof Uint8Array) {
|
|
142
|
+
const base64 = encodeBase64(data);
|
|
143
|
+
return `data:${part.mediaType};base64,${base64}`;
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
function encodeBase64(data) {
|
|
148
|
+
if (typeof Buffer !== 'undefined') {
|
|
149
|
+
return Buffer.from(data).toString('base64');
|
|
150
|
+
}
|
|
151
|
+
let binary = '';
|
|
152
|
+
for (const byte of data) {
|
|
153
|
+
binary += String.fromCharCode(byte);
|
|
154
|
+
}
|
|
155
|
+
const btoaFn = globalThis.btoa;
|
|
156
|
+
if (btoaFn) {
|
|
157
|
+
return btoaFn(binary);
|
|
158
|
+
}
|
|
159
|
+
throw new Error('Base64 encoding is not available in this environment.');
|
|
160
|
+
}
|
|
161
|
+
function convertToolCall(part) {
|
|
162
|
+
return {
|
|
163
|
+
id: part.toolCallId,
|
|
164
|
+
type: 'function',
|
|
165
|
+
function: {
|
|
166
|
+
name: part.toolName,
|
|
167
|
+
arguments: JSON.stringify(part.args ?? {}),
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function stringifyToolResult(part) {
|
|
172
|
+
if (Array.isArray(part.content) && part.content.length > 0) {
|
|
173
|
+
return part.content
|
|
174
|
+
.map((contentPart) => {
|
|
175
|
+
if (contentPart.type === 'text') {
|
|
176
|
+
return contentPart.text;
|
|
177
|
+
}
|
|
178
|
+
if (contentPart.type === 'image') {
|
|
179
|
+
const mediaType = contentPart.mediaType || 'image/png';
|
|
180
|
+
return `data:${mediaType};base64,${contentPart.data}`;
|
|
181
|
+
}
|
|
182
|
+
return '';
|
|
183
|
+
})
|
|
184
|
+
.join('');
|
|
185
|
+
}
|
|
186
|
+
if (part.result !== undefined) {
|
|
187
|
+
return JSON.stringify(part.result);
|
|
188
|
+
}
|
|
189
|
+
return '';
|
|
190
|
+
}
|
|
191
|
+
function mergeTextParts(parts) {
|
|
192
|
+
if (parts.length === 0) {
|
|
193
|
+
return '';
|
|
194
|
+
}
|
|
195
|
+
const hasNonText = parts.some((part) => part.type !== 'text');
|
|
196
|
+
if (hasNonText) {
|
|
197
|
+
return parts;
|
|
198
|
+
}
|
|
199
|
+
return parts.map((part) => (part.type === 'text' ? part.text ?? '' : '')).join('');
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=convert-prompt.js.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createQiniu, qiniu } from './provider';
|
|
2
|
+
export { QiniuChatModel } from './qiniu-chat-model';
|
|
3
|
+
export type { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2CallWarning, LanguageModelV2Content, LanguageModelV2File, LanguageModelV2FilePart, LanguageModelV2FinishReason, LanguageModelV2FunctionTool, LanguageModelV2Message, LanguageModelV2Prompt, LanguageModelV2ProviderDefinedTool, LanguageModelV2Reasoning, LanguageModelV2ReasoningPart, LanguageModelV2ResponseMetadata, LanguageModelV2StreamPart, LanguageModelV2Text, LanguageModelV2TextPart, LanguageModelV2ToolCall, LanguageModelV2ToolCallDelta, LanguageModelV2ToolCallPart, LanguageModelV2ToolChoice, LanguageModelV2ToolResultPart, LanguageModelV2Usage, } from './types';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapter/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,YAAY,EACR,eAAe,EACf,0BAA0B,EAC1B,0BAA0B,EAC1B,sBAAsB,EACtB,mBAAmB,EACnB,uBAAuB,EACvB,2BAA2B,EAC3B,2BAA2B,EAC3B,sBAAsB,EACtB,qBAAqB,EACrB,kCAAkC,EAClC,wBAAwB,EACxB,4BAA4B,EAC5B,+BAA+B,EAC/B,yBAAyB,EACzB,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,4BAA4B,EAC5B,2BAA2B,EAC3B,yBAAyB,EACzB,6BAA6B,EAC7B,oBAAoB,GACvB,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QiniuChatModel = exports.qiniu = exports.createQiniu = void 0;
|
|
4
|
+
var provider_1 = require("./provider");
|
|
5
|
+
Object.defineProperty(exports, "createQiniu", { enumerable: true, get: function () { return provider_1.createQiniu; } });
|
|
6
|
+
Object.defineProperty(exports, "qiniu", { enumerable: true, get: function () { return provider_1.qiniu; } });
|
|
7
|
+
var qiniu_chat_model_1 = require("./qiniu-chat-model");
|
|
8
|
+
Object.defineProperty(exports, "QiniuChatModel", { enumerable: true, get: function () { return qiniu_chat_model_1.QiniuChatModel; } });
|
|
9
|
+
//# sourceMappingURL=index.js.map
|