@kernl-sdk/ai 0.3.5 → 0.4.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +44 -0
- package/dist/__tests__/integration.test.js +16 -16
- package/dist/__tests__/language-model.test.js +10 -10
- package/dist/convert/__tests__/message.test.js +7 -7
- package/dist/convert/__tests__/settings.test.js +1 -1
- package/dist/convert/__tests__/stream.test.js +19 -19
- package/dist/convert/__tests__/tools.test.js +1 -1
- package/dist/convert/__tests__/ui-message.test.js +44 -44
- package/dist/convert/__tests__/ui-stream.test.js +42 -42
- package/dist/convert/message.js +2 -2
- package/dist/convert/response.js +2 -2
- package/dist/convert/stream.js +13 -13
- package/dist/convert/ui-message.js +5 -5
- package/dist/convert/ui-stream.js +13 -13
- package/dist/language-model.js +7 -7
- package/package.json +3 -3
- package/src/__tests__/integration.test.ts +16 -16
- package/src/__tests__/language-model.test.ts +10 -10
- package/src/convert/__tests__/message.test.ts +7 -7
- package/src/convert/__tests__/settings.test.ts +1 -1
- package/src/convert/__tests__/stream.test.ts +19 -19
- package/src/convert/__tests__/tools.test.ts +1 -1
- package/src/convert/__tests__/ui-message.test.ts +44 -44
- package/src/convert/__tests__/ui-stream.test.ts +42 -42
- package/src/convert/message.ts +2 -2
- package/src/convert/response.ts +2 -2
- package/src/convert/stream.ts +13 -13
- package/src/convert/ui-message.ts +7 -7
- package/src/convert/ui-stream.ts +13 -13
- package/src/language-model.ts +7 -7
|
@@ -9,7 +9,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
9
9
|
describe("encode - text events", () => {
|
|
10
10
|
it("should encode text-start event", () => {
|
|
11
11
|
const event: LanguageModelStreamEvent = {
|
|
12
|
-
kind: "text
|
|
12
|
+
kind: "text.start",
|
|
13
13
|
id: "text-1",
|
|
14
14
|
};
|
|
15
15
|
|
|
@@ -23,7 +23,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
23
23
|
|
|
24
24
|
it("should encode text-delta event", () => {
|
|
25
25
|
const event: LanguageModelStreamEvent = {
|
|
26
|
-
kind: "text
|
|
26
|
+
kind: "text.delta",
|
|
27
27
|
id: "text-1",
|
|
28
28
|
text: "Hello world",
|
|
29
29
|
};
|
|
@@ -39,7 +39,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
39
39
|
|
|
40
40
|
it("should encode text-end event", () => {
|
|
41
41
|
const event: LanguageModelStreamEvent = {
|
|
42
|
-
kind: "text
|
|
42
|
+
kind: "text.end",
|
|
43
43
|
id: "text-1",
|
|
44
44
|
};
|
|
45
45
|
|
|
@@ -55,7 +55,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
55
55
|
describe("encode - reasoning events", () => {
|
|
56
56
|
it("should encode reasoning-start event", () => {
|
|
57
57
|
const event: LanguageModelStreamEvent = {
|
|
58
|
-
kind: "reasoning
|
|
58
|
+
kind: "reasoning.start",
|
|
59
59
|
id: "reason-1",
|
|
60
60
|
};
|
|
61
61
|
|
|
@@ -69,7 +69,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
69
69
|
|
|
70
70
|
it("should encode reasoning-delta event", () => {
|
|
71
71
|
const event: LanguageModelStreamEvent = {
|
|
72
|
-
kind: "reasoning
|
|
72
|
+
kind: "reasoning.delta",
|
|
73
73
|
id: "reason-1",
|
|
74
74
|
text: "thinking step by step",
|
|
75
75
|
};
|
|
@@ -85,7 +85,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
85
85
|
|
|
86
86
|
it("should encode reasoning-end event", () => {
|
|
87
87
|
const event: LanguageModelStreamEvent = {
|
|
88
|
-
kind: "reasoning
|
|
88
|
+
kind: "reasoning.end",
|
|
89
89
|
id: "reason-1",
|
|
90
90
|
};
|
|
91
91
|
|
|
@@ -101,9 +101,9 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
101
101
|
describe("encode - tool input events", () => {
|
|
102
102
|
it("should encode tool-input-start event", () => {
|
|
103
103
|
const event: LanguageModelStreamEvent = {
|
|
104
|
-
kind: "tool
|
|
104
|
+
kind: "tool.input.start",
|
|
105
105
|
id: "tool-1",
|
|
106
|
-
|
|
106
|
+
toolId: "calculator",
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
const result = STREAM_UI_PART.encode(event);
|
|
@@ -111,15 +111,15 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
111
111
|
expect(result).toEqual({
|
|
112
112
|
type: "tool-input-start",
|
|
113
113
|
toolCallId: "tool-1",
|
|
114
|
-
|
|
114
|
+
toolId: "calculator",
|
|
115
115
|
});
|
|
116
116
|
});
|
|
117
117
|
|
|
118
118
|
it("should encode tool-input-start event with title", () => {
|
|
119
119
|
const event: LanguageModelStreamEvent = {
|
|
120
|
-
kind: "tool
|
|
120
|
+
kind: "tool.input.start",
|
|
121
121
|
id: "tool-1",
|
|
122
|
-
|
|
122
|
+
toolId: "calculator",
|
|
123
123
|
title: "Calculate sum",
|
|
124
124
|
};
|
|
125
125
|
|
|
@@ -128,14 +128,14 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
128
128
|
expect(result).toEqual({
|
|
129
129
|
type: "tool-input-start",
|
|
130
130
|
toolCallId: "tool-1",
|
|
131
|
-
|
|
131
|
+
toolId: "calculator",
|
|
132
132
|
title: "Calculate sum",
|
|
133
133
|
});
|
|
134
134
|
});
|
|
135
135
|
|
|
136
136
|
it("should encode tool-input-delta event", () => {
|
|
137
137
|
const event: LanguageModelStreamEvent = {
|
|
138
|
-
kind: "tool
|
|
138
|
+
kind: "tool.input.delta",
|
|
139
139
|
id: "tool-1",
|
|
140
140
|
delta: '{"a": 1',
|
|
141
141
|
};
|
|
@@ -151,7 +151,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
151
151
|
|
|
152
152
|
it("should return null for tool-input-end event", () => {
|
|
153
153
|
const event: LanguageModelStreamEvent = {
|
|
154
|
-
kind: "tool
|
|
154
|
+
kind: "tool.input.end",
|
|
155
155
|
id: "tool-1",
|
|
156
156
|
};
|
|
157
157
|
|
|
@@ -164,7 +164,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
164
164
|
describe("encode - tool call and result events", () => {
|
|
165
165
|
it("should encode tool-call as tool-input-available", () => {
|
|
166
166
|
const event: LanguageModelStreamEvent = {
|
|
167
|
-
kind: "tool
|
|
167
|
+
kind: "tool.call",
|
|
168
168
|
callId: "call-123",
|
|
169
169
|
toolId: "calculator",
|
|
170
170
|
state: COMPLETED,
|
|
@@ -176,14 +176,14 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
176
176
|
expect(result).toEqual({
|
|
177
177
|
type: "tool-input-available",
|
|
178
178
|
toolCallId: "call-123",
|
|
179
|
-
|
|
179
|
+
toolId: "calculator",
|
|
180
180
|
input: { a: 5, b: 3 },
|
|
181
181
|
});
|
|
182
182
|
});
|
|
183
183
|
|
|
184
184
|
it("should handle tool-call with empty arguments string", () => {
|
|
185
185
|
const event: LanguageModelStreamEvent = {
|
|
186
|
-
kind: "tool
|
|
186
|
+
kind: "tool.call",
|
|
187
187
|
callId: "call-empty",
|
|
188
188
|
toolId: "list_issues",
|
|
189
189
|
state: IN_PROGRESS,
|
|
@@ -195,14 +195,14 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
195
195
|
expect(result).toEqual({
|
|
196
196
|
type: "tool-input-available",
|
|
197
197
|
toolCallId: "call-empty",
|
|
198
|
-
|
|
198
|
+
toolId: "list_issues",
|
|
199
199
|
input: {},
|
|
200
200
|
});
|
|
201
201
|
});
|
|
202
202
|
|
|
203
203
|
it("should encode successful tool-result as tool-output-available", () => {
|
|
204
204
|
const event: LanguageModelStreamEvent = {
|
|
205
|
-
kind: "tool
|
|
205
|
+
kind: "tool.result",
|
|
206
206
|
callId: "call-123",
|
|
207
207
|
toolId: "calculator",
|
|
208
208
|
state: COMPLETED,
|
|
@@ -221,7 +221,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
221
221
|
|
|
222
222
|
it("should encode failed tool-result as tool-output-error", () => {
|
|
223
223
|
const event: LanguageModelStreamEvent = {
|
|
224
|
-
kind: "tool
|
|
224
|
+
kind: "tool.result",
|
|
225
225
|
callId: "call-123",
|
|
226
226
|
toolId: "calculator",
|
|
227
227
|
state: FAILED,
|
|
@@ -240,7 +240,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
240
240
|
|
|
241
241
|
it("should handle failed tool-result with null error", () => {
|
|
242
242
|
const event: LanguageModelStreamEvent = {
|
|
243
|
-
kind: "tool
|
|
243
|
+
kind: "tool.result",
|
|
244
244
|
callId: "call-123",
|
|
245
245
|
toolId: "calculator",
|
|
246
246
|
state: FAILED,
|
|
@@ -261,7 +261,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
261
261
|
describe("encode - stream control events", () => {
|
|
262
262
|
it("should encode stream-start as start", () => {
|
|
263
263
|
const event: LanguageModelStreamEvent = {
|
|
264
|
-
kind: "stream
|
|
264
|
+
kind: "stream.start",
|
|
265
265
|
};
|
|
266
266
|
|
|
267
267
|
const result = STREAM_UI_PART.encode(event);
|
|
@@ -356,7 +356,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
356
356
|
describe("encode - providerMetadata omission", () => {
|
|
357
357
|
it("should omit providerMetadata from text events", () => {
|
|
358
358
|
const event: LanguageModelStreamEvent = {
|
|
359
|
-
kind: "text
|
|
359
|
+
kind: "text.delta",
|
|
360
360
|
id: "text-1",
|
|
361
361
|
text: "Hello",
|
|
362
362
|
providerMetadata: {
|
|
@@ -376,7 +376,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
376
376
|
|
|
377
377
|
it("should omit providerMetadata from tool calls", () => {
|
|
378
378
|
const event: LanguageModelStreamEvent = {
|
|
379
|
-
kind: "tool
|
|
379
|
+
kind: "tool.call",
|
|
380
380
|
callId: "call-123",
|
|
381
381
|
toolId: "calculator",
|
|
382
382
|
state: COMPLETED,
|
|
@@ -391,7 +391,7 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
391
391
|
expect(result).toEqual({
|
|
392
392
|
type: "tool-input-available",
|
|
393
393
|
toolCallId: "call-123",
|
|
394
|
-
|
|
394
|
+
toolId: "calculator",
|
|
395
395
|
input: { x: 1 },
|
|
396
396
|
});
|
|
397
397
|
expect(result).not.toHaveProperty("providerMetadata");
|
|
@@ -416,11 +416,11 @@ describe("STREAM_UI_PART codec", () => {
|
|
|
416
416
|
describe("toUIMessageStream", () => {
|
|
417
417
|
it("should convert async iterable to readable stream", async () => {
|
|
418
418
|
const events: LanguageModelStreamEvent[] = [
|
|
419
|
-
{ kind: "stream
|
|
420
|
-
{ kind: "text
|
|
421
|
-
{ kind: "text
|
|
422
|
-
{ kind: "text
|
|
423
|
-
{ kind: "text
|
|
419
|
+
{ kind: "stream.start" },
|
|
420
|
+
{ kind: "text.start", id: "text-1" },
|
|
421
|
+
{ kind: "text.delta", id: "text-1", text: "Hello" },
|
|
422
|
+
{ kind: "text.delta", id: "text-1", text: " world" },
|
|
423
|
+
{ kind: "text.end", id: "text-1" },
|
|
424
424
|
{ kind: "finish", finishReason: "stop", usage: { inputTokens: 10, outputTokens: 5, totalTokens: 15 } },
|
|
425
425
|
];
|
|
426
426
|
|
|
@@ -453,11 +453,11 @@ describe("toUIMessageStream", () => {
|
|
|
453
453
|
|
|
454
454
|
it("should filter out null events", async () => {
|
|
455
455
|
const events: LanguageModelStreamEvent[] = [
|
|
456
|
-
{ kind: "text
|
|
457
|
-
{ kind: "text
|
|
458
|
-
{ kind: "tool
|
|
456
|
+
{ kind: "text.start", id: "text-1" },
|
|
457
|
+
{ kind: "text.delta", id: "text-1", text: "Hello" },
|
|
458
|
+
{ kind: "tool.input.end", id: "tool-1" }, // Should be filtered (returns null)
|
|
459
459
|
{ kind: "raw", rawValue: {} }, // Should be filtered (returns null)
|
|
460
|
-
{ kind: "text
|
|
460
|
+
{ kind: "text.end", id: "text-1" },
|
|
461
461
|
];
|
|
462
462
|
|
|
463
463
|
async function* generateEvents() {
|
|
@@ -485,11 +485,11 @@ describe("toUIMessageStream", () => {
|
|
|
485
485
|
|
|
486
486
|
it("should handle tool calls and results", async () => {
|
|
487
487
|
const events: LanguageModelStreamEvent[] = [
|
|
488
|
-
{ kind: "tool
|
|
489
|
-
{ kind: "tool
|
|
490
|
-
{ kind: "tool
|
|
491
|
-
{ kind: "tool
|
|
492
|
-
{ kind: "tool
|
|
488
|
+
{ kind: "tool.input.start", id: "tool-1", toolId: "calculator" },
|
|
489
|
+
{ kind: "tool.input.delta", id: "tool-1", delta: '{"x":' },
|
|
490
|
+
{ kind: "tool.input.delta", id: "tool-1", delta: '5}' },
|
|
491
|
+
{ kind: "tool.call", callId: "tool-1", toolId: "calculator", state: COMPLETED, arguments: '{"x":5}' },
|
|
492
|
+
{ kind: "tool.result", callId: "tool-1", toolId: "calculator", state: COMPLETED, result: 25, error: null },
|
|
493
493
|
];
|
|
494
494
|
|
|
495
495
|
async function* generateEvents() {
|
|
@@ -509,17 +509,17 @@ describe("toUIMessageStream", () => {
|
|
|
509
509
|
}
|
|
510
510
|
|
|
511
511
|
expect(chunks).toEqual([
|
|
512
|
-
{ type: "tool-input-start", toolCallId: "tool-1",
|
|
512
|
+
{ type: "tool-input-start", toolCallId: "tool-1", toolId: "calculator" },
|
|
513
513
|
{ type: "tool-input-delta", toolCallId: "tool-1", inputTextDelta: '{"x":' },
|
|
514
514
|
{ type: "tool-input-delta", toolCallId: "tool-1", inputTextDelta: '5}' },
|
|
515
|
-
{ type: "tool-input-available", toolCallId: "tool-1",
|
|
515
|
+
{ type: "tool-input-available", toolCallId: "tool-1", toolId: "calculator", input: { x: 5 } },
|
|
516
516
|
{ type: "tool-output-available", toolCallId: "tool-1", output: 25 },
|
|
517
517
|
]);
|
|
518
518
|
});
|
|
519
519
|
|
|
520
520
|
it("should handle errors in stream", async () => {
|
|
521
521
|
const events: LanguageModelStreamEvent[] = [
|
|
522
|
-
{ kind: "text
|
|
522
|
+
{ kind: "text.start", id: "text-1" },
|
|
523
523
|
{ kind: "error", error: new Error("Network timeout") },
|
|
524
524
|
];
|
|
525
525
|
|
package/src/convert/message.ts
CHANGED
|
@@ -106,7 +106,7 @@ export const MESSAGE: Codec<LanguageModelItem, LanguageModelV3Message> = {
|
|
|
106
106
|
};
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
case "tool
|
|
109
|
+
case "tool.call": {
|
|
110
110
|
return {
|
|
111
111
|
role: "assistant",
|
|
112
112
|
content: [
|
|
@@ -121,7 +121,7 @@ export const MESSAGE: Codec<LanguageModelItem, LanguageModelV3Message> = {
|
|
|
121
121
|
};
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
case "tool
|
|
124
|
+
case "tool.result": {
|
|
125
125
|
return {
|
|
126
126
|
role: "tool",
|
|
127
127
|
content: [
|
package/src/convert/response.ts
CHANGED
|
@@ -68,7 +68,7 @@ export const MODEL_RESPONSE: Codec<LanguageModelResponse, AISdkGenerateResult> =
|
|
|
68
68
|
|
|
69
69
|
case "tool-call":
|
|
70
70
|
content.push({
|
|
71
|
-
kind: "tool
|
|
71
|
+
kind: "tool.call",
|
|
72
72
|
callId: item.toolCallId,
|
|
73
73
|
toolId: item.toolName,
|
|
74
74
|
state: IN_PROGRESS,
|
|
@@ -79,7 +79,7 @@ export const MODEL_RESPONSE: Codec<LanguageModelResponse, AISdkGenerateResult> =
|
|
|
79
79
|
|
|
80
80
|
case "tool-result":
|
|
81
81
|
content.push({
|
|
82
|
-
kind: "tool
|
|
82
|
+
kind: "tool.result",
|
|
83
83
|
callId: item.toolCallId,
|
|
84
84
|
toolId: item.toolName,
|
|
85
85
|
state: item.isError ? FAILED : COMPLETED,
|
package/src/convert/stream.ts
CHANGED
|
@@ -49,14 +49,14 @@ export const STREAM_PART: Codec<
|
|
|
49
49
|
switch (part.type) {
|
|
50
50
|
case "text-start":
|
|
51
51
|
return {
|
|
52
|
-
kind: "text
|
|
52
|
+
kind: "text.start",
|
|
53
53
|
id: part.id,
|
|
54
54
|
providerMetadata: part.providerMetadata,
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
case "text-delta":
|
|
58
58
|
return {
|
|
59
|
-
kind: "text
|
|
59
|
+
kind: "text.delta",
|
|
60
60
|
id: part.id,
|
|
61
61
|
text: part.delta,
|
|
62
62
|
providerMetadata: part.providerMetadata,
|
|
@@ -64,21 +64,21 @@ export const STREAM_PART: Codec<
|
|
|
64
64
|
|
|
65
65
|
case "text-end":
|
|
66
66
|
return {
|
|
67
|
-
kind: "text
|
|
67
|
+
kind: "text.end",
|
|
68
68
|
id: part.id,
|
|
69
69
|
providerMetadata: part.providerMetadata,
|
|
70
70
|
};
|
|
71
71
|
|
|
72
72
|
case "reasoning-start":
|
|
73
73
|
return {
|
|
74
|
-
kind: "reasoning
|
|
74
|
+
kind: "reasoning.start",
|
|
75
75
|
id: part.id,
|
|
76
76
|
providerMetadata: part.providerMetadata,
|
|
77
77
|
};
|
|
78
78
|
|
|
79
79
|
case "reasoning-delta":
|
|
80
80
|
return {
|
|
81
|
-
kind: "reasoning
|
|
81
|
+
kind: "reasoning.delta",
|
|
82
82
|
id: part.id,
|
|
83
83
|
text: part.delta,
|
|
84
84
|
providerMetadata: part.providerMetadata,
|
|
@@ -86,23 +86,23 @@ export const STREAM_PART: Codec<
|
|
|
86
86
|
|
|
87
87
|
case "reasoning-end":
|
|
88
88
|
return {
|
|
89
|
-
kind: "reasoning
|
|
89
|
+
kind: "reasoning.end",
|
|
90
90
|
id: part.id,
|
|
91
91
|
providerMetadata: part.providerMetadata,
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
case "tool-input-start":
|
|
95
95
|
return {
|
|
96
|
-
kind: "tool
|
|
96
|
+
kind: "tool.input.start",
|
|
97
97
|
id: part.id,
|
|
98
|
-
|
|
98
|
+
toolId: part.toolName,
|
|
99
99
|
title: part.title,
|
|
100
100
|
providerMetadata: part.providerMetadata,
|
|
101
101
|
};
|
|
102
102
|
|
|
103
103
|
case "tool-input-delta":
|
|
104
104
|
return {
|
|
105
|
-
kind: "tool
|
|
105
|
+
kind: "tool.input.delta",
|
|
106
106
|
id: part.id,
|
|
107
107
|
delta: part.delta,
|
|
108
108
|
providerMetadata: part.providerMetadata,
|
|
@@ -110,14 +110,14 @@ export const STREAM_PART: Codec<
|
|
|
110
110
|
|
|
111
111
|
case "tool-input-end":
|
|
112
112
|
return {
|
|
113
|
-
kind: "tool
|
|
113
|
+
kind: "tool.input.end",
|
|
114
114
|
id: part.id,
|
|
115
115
|
providerMetadata: part.providerMetadata,
|
|
116
116
|
};
|
|
117
117
|
|
|
118
118
|
case "tool-call":
|
|
119
119
|
return {
|
|
120
|
-
kind: "tool
|
|
120
|
+
kind: "tool.call",
|
|
121
121
|
callId: part.toolCallId,
|
|
122
122
|
toolId: part.toolName,
|
|
123
123
|
state: IN_PROGRESS,
|
|
@@ -128,7 +128,7 @@ export const STREAM_PART: Codec<
|
|
|
128
128
|
case "tool-result":
|
|
129
129
|
// provider-defined tools can stream tool results
|
|
130
130
|
return {
|
|
131
|
-
kind: "tool
|
|
131
|
+
kind: "tool.result",
|
|
132
132
|
callId: part.toolCallId,
|
|
133
133
|
toolId: part.toolName,
|
|
134
134
|
state: part.isError ? FAILED : COMPLETED,
|
|
@@ -139,7 +139,7 @@ export const STREAM_PART: Codec<
|
|
|
139
139
|
|
|
140
140
|
case "stream-start":
|
|
141
141
|
return {
|
|
142
|
-
kind: "stream
|
|
142
|
+
kind: "stream.start",
|
|
143
143
|
warnings: part.warnings.map(WARNING.decode),
|
|
144
144
|
};
|
|
145
145
|
|
|
@@ -170,7 +170,7 @@ const TOOL_UI_PART: Codec<
|
|
|
170
170
|
switch (part.state) {
|
|
171
171
|
case "input-available": {
|
|
172
172
|
const call: ToolCall = {
|
|
173
|
-
kind: "tool
|
|
173
|
+
kind: "tool.call",
|
|
174
174
|
callId,
|
|
175
175
|
toolId,
|
|
176
176
|
state: IN_PROGRESS,
|
|
@@ -182,7 +182,7 @@ const TOOL_UI_PART: Codec<
|
|
|
182
182
|
|
|
183
183
|
case "output-available": {
|
|
184
184
|
const result: ToolResult = {
|
|
185
|
-
kind: "tool
|
|
185
|
+
kind: "tool.result",
|
|
186
186
|
callId,
|
|
187
187
|
toolId,
|
|
188
188
|
state: COMPLETED,
|
|
@@ -195,7 +195,7 @@ const TOOL_UI_PART: Codec<
|
|
|
195
195
|
|
|
196
196
|
case "output-error": {
|
|
197
197
|
const result: ToolResult = {
|
|
198
|
-
kind: "tool
|
|
198
|
+
kind: "tool.result",
|
|
199
199
|
callId,
|
|
200
200
|
toolId,
|
|
201
201
|
state: FAILED,
|
|
@@ -291,8 +291,8 @@ export function historyToUIMessages(items: LanguageModelItem[]): UIMessage[] {
|
|
|
291
291
|
const toolMap = new Map<
|
|
292
292
|
string,
|
|
293
293
|
{
|
|
294
|
-
call?: Extract<LanguageModelItem, { kind: "tool
|
|
295
|
-
result?: Extract<LanguageModelItem, { kind: "tool
|
|
294
|
+
call?: Extract<LanguageModelItem, { kind: "tool.call" }>;
|
|
295
|
+
result?: Extract<LanguageModelItem, { kind: "tool.result" }>;
|
|
296
296
|
}
|
|
297
297
|
>();
|
|
298
298
|
const reasoningParts: Extract<
|
|
@@ -303,10 +303,10 @@ export function historyToUIMessages(items: LanguageModelItem[]): UIMessage[] {
|
|
|
303
303
|
while (j < items.length && items[j].kind !== "message") {
|
|
304
304
|
const next = items[j];
|
|
305
305
|
|
|
306
|
-
if (next.kind === "tool
|
|
306
|
+
if (next.kind === "tool.call") {
|
|
307
307
|
const existing = toolMap.get(next.callId) || {};
|
|
308
308
|
toolMap.set(next.callId, { ...existing, call: next });
|
|
309
|
-
} else if (next.kind === "tool
|
|
309
|
+
} else if (next.kind === "tool.result") {
|
|
310
310
|
const existing = toolMap.get(next.callId) || {};
|
|
311
311
|
toolMap.set(next.callId, { ...existing, result: next });
|
|
312
312
|
} else if (next.kind === "reasoning") {
|
package/src/convert/ui-stream.ts
CHANGED
|
@@ -49,60 +49,60 @@ export const STREAM_UI_PART: Codec<
|
|
|
49
49
|
> = {
|
|
50
50
|
encode: (event: LanguageModelStreamEvent): UIMessageChunk | null => {
|
|
51
51
|
switch (event.kind) {
|
|
52
|
-
case "text
|
|
52
|
+
case "text.start":
|
|
53
53
|
return {
|
|
54
54
|
type: "text-start",
|
|
55
55
|
id: event.id,
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
case "text
|
|
58
|
+
case "text.delta":
|
|
59
59
|
return {
|
|
60
60
|
type: "text-delta",
|
|
61
61
|
id: event.id,
|
|
62
62
|
delta: event.text,
|
|
63
63
|
};
|
|
64
64
|
|
|
65
|
-
case "text
|
|
65
|
+
case "text.end":
|
|
66
66
|
return {
|
|
67
67
|
type: "text-end",
|
|
68
68
|
id: event.id,
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
-
case "reasoning
|
|
71
|
+
case "reasoning.start":
|
|
72
72
|
return {
|
|
73
73
|
type: "reasoning-start",
|
|
74
74
|
id: event.id,
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
case "reasoning
|
|
77
|
+
case "reasoning.delta":
|
|
78
78
|
return {
|
|
79
79
|
type: "reasoning-delta",
|
|
80
80
|
id: event.id,
|
|
81
81
|
delta: event.text,
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
case "reasoning
|
|
84
|
+
case "reasoning.end":
|
|
85
85
|
return {
|
|
86
86
|
type: "reasoning-end",
|
|
87
87
|
id: event.id,
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
-
case "tool
|
|
90
|
+
case "tool.input.start":
|
|
91
91
|
return {
|
|
92
92
|
type: "tool-input-start",
|
|
93
93
|
toolCallId: event.id,
|
|
94
|
-
toolName: event.
|
|
94
|
+
toolName: event.toolId,
|
|
95
95
|
...(event.title != null ? { title: event.title } : {}),
|
|
96
96
|
};
|
|
97
97
|
|
|
98
|
-
case "tool
|
|
98
|
+
case "tool.input.delta":
|
|
99
99
|
return {
|
|
100
100
|
type: "tool-input-delta",
|
|
101
101
|
toolCallId: event.id,
|
|
102
102
|
inputTextDelta: event.delta,
|
|
103
103
|
};
|
|
104
104
|
|
|
105
|
-
case "tool
|
|
105
|
+
case "tool.call":
|
|
106
106
|
return {
|
|
107
107
|
type: "tool-input-available",
|
|
108
108
|
toolCallId: event.callId,
|
|
@@ -110,7 +110,7 @@ export const STREAM_UI_PART: Codec<
|
|
|
110
110
|
input: JSON.parse(event.arguments),
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
case "tool
|
|
113
|
+
case "tool.result":
|
|
114
114
|
// Convert tool result to tool-output-available or tool-output-error
|
|
115
115
|
if (event.state === FAILED) {
|
|
116
116
|
return {
|
|
@@ -126,7 +126,7 @@ export const STREAM_UI_PART: Codec<
|
|
|
126
126
|
output: event.result,
|
|
127
127
|
};
|
|
128
128
|
|
|
129
|
-
case "stream
|
|
129
|
+
case "stream.start":
|
|
130
130
|
return {
|
|
131
131
|
type: "start",
|
|
132
132
|
};
|
|
@@ -151,7 +151,7 @@ export const STREAM_UI_PART: Codec<
|
|
|
151
151
|
case "message":
|
|
152
152
|
case "reasoning":
|
|
153
153
|
// - events without direct UI equivalents -
|
|
154
|
-
case "tool
|
|
154
|
+
case "tool.input.end":
|
|
155
155
|
case "raw":
|
|
156
156
|
return null;
|
|
157
157
|
|
package/src/language-model.ts
CHANGED
|
@@ -74,13 +74,13 @@ export class AISDKLanguageModel implements LanguageModel {
|
|
|
74
74
|
|
|
75
75
|
for await (const event of convertStream(stream.stream)) {
|
|
76
76
|
switch (event.kind) {
|
|
77
|
-
case "text
|
|
77
|
+
case "text.start": {
|
|
78
78
|
tbuf[event.id] = "";
|
|
79
79
|
yield event;
|
|
80
80
|
break;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
case "text
|
|
83
|
+
case "text.delta": {
|
|
84
84
|
if (tbuf[event.id] !== undefined) {
|
|
85
85
|
tbuf[event.id] += event.text;
|
|
86
86
|
}
|
|
@@ -88,7 +88,7 @@ export class AISDKLanguageModel implements LanguageModel {
|
|
|
88
88
|
break;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
case "text
|
|
91
|
+
case "text.end": {
|
|
92
92
|
const text = tbuf[event.id];
|
|
93
93
|
if (text !== undefined) {
|
|
94
94
|
yield message({
|
|
@@ -102,13 +102,13 @@ export class AISDKLanguageModel implements LanguageModel {
|
|
|
102
102
|
break;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
case "reasoning
|
|
105
|
+
case "reasoning.start": {
|
|
106
106
|
rbuf[event.id] = "";
|
|
107
107
|
yield event;
|
|
108
108
|
break;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
case "reasoning
|
|
111
|
+
case "reasoning.delta": {
|
|
112
112
|
if (rbuf[event.id] !== undefined) {
|
|
113
113
|
rbuf[event.id] += event.text;
|
|
114
114
|
}
|
|
@@ -116,7 +116,7 @@ export class AISDKLanguageModel implements LanguageModel {
|
|
|
116
116
|
break;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
case "reasoning
|
|
119
|
+
case "reasoning.end": {
|
|
120
120
|
const text = rbuf[event.id];
|
|
121
121
|
if (text !== undefined) {
|
|
122
122
|
yield reasoning({
|
|
@@ -130,7 +130,7 @@ export class AISDKLanguageModel implements LanguageModel {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
default:
|
|
133
|
-
// all other events (tool
|
|
133
|
+
// all other events (tool.call, tool.result, finish, etc.) pass through
|
|
134
134
|
yield event;
|
|
135
135
|
break;
|
|
136
136
|
}
|