kernl 0.12.0 → 0.12.2

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.
Files changed (51) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +21 -0
  3. package/dist/api/resources/agents/agents.d.ts +2 -2
  4. package/dist/api/resources/agents/agents.d.ts.map +1 -1
  5. package/dist/api/resources/agents/agents.js +1 -1
  6. package/dist/index.d.ts +3 -2
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -0
  9. package/dist/kernl/index.d.ts +2 -1
  10. package/dist/kernl/index.d.ts.map +1 -1
  11. package/dist/kernl/index.js +1 -0
  12. package/dist/kernl/kernl.d.ts +4 -4
  13. package/dist/kernl/kernl.d.ts.map +1 -1
  14. package/dist/kernl/kernl.js +17 -13
  15. package/dist/kernl/registry.d.ts +46 -0
  16. package/dist/kernl/registry.d.ts.map +1 -0
  17. package/dist/kernl/registry.js +57 -0
  18. package/dist/kernl/types.d.ts +2 -2
  19. package/dist/kernl/types.d.ts.map +1 -1
  20. package/dist/storage/base.d.ts +3 -3
  21. package/dist/storage/base.d.ts.map +1 -1
  22. package/dist/storage/in-memory.d.ts +5 -5
  23. package/dist/storage/in-memory.d.ts.map +1 -1
  24. package/dist/thread/__tests__/mock.d.ts +2 -3
  25. package/dist/thread/__tests__/mock.d.ts.map +1 -1
  26. package/dist/thread/thread.d.ts +4 -0
  27. package/dist/thread/thread.d.ts.map +1 -1
  28. package/dist/thread/thread.js +43 -75
  29. package/dist/thread/types.d.ts +18 -3
  30. package/dist/thread/types.d.ts.map +1 -1
  31. package/dist/thread/utils.d.ts +7 -7
  32. package/dist/thread/utils.d.ts.map +1 -1
  33. package/dist/thread/utils.js +1 -1
  34. package/package.json +4 -6
  35. package/src/api/__tests__/threads.test.ts +3 -3
  36. package/src/api/resources/agents/agents.ts +3 -3
  37. package/src/index.ts +4 -2
  38. package/src/kernl/index.ts +3 -2
  39. package/src/kernl/kernl.ts +18 -15
  40. package/src/kernl/registry.ts +69 -0
  41. package/src/kernl/types.ts +2 -2
  42. package/src/storage/base.ts +2 -2
  43. package/src/storage/in-memory.ts +4 -4
  44. package/src/thread/__tests__/mock.ts +2 -2
  45. package/src/thread/thread.ts +47 -78
  46. package/src/thread/types.ts +49 -3
  47. package/src/thread/utils.ts +14 -7
  48. package/dist/thread/__tests__/integration.test.d.ts +0 -2
  49. package/dist/thread/__tests__/integration.test.d.ts.map +0 -1
  50. package/dist/thread/__tests__/integration.test.js +0 -320
  51. package/src/thread/__tests__/integration.test.ts +0 -434
@@ -1,320 +0,0 @@
1
- import { describe, it, expect, beforeAll } from "vitest";
2
- import { z } from "zod";
3
- import { openai } from "@ai-sdk/openai";
4
- import { AISDKLanguageModel } from "@kernl-sdk/ai";
5
- import "@kernl-sdk/ai/openai"; // (TMP)
6
- import { Agent } from "../../agent.js";
7
- import { Kernl } from "../../kernl/index.js";
8
- import { tool, Toolkit } from "../../tool/index.js";
9
- import { Thread } from "../thread.js";
10
- /**
11
- * Integration tests for Thread streaming with real AI SDK providers.
12
- *
13
- * These tests require an OPENAI_API_KEY environment variable to be set.
14
- * They will be skipped if the API key is not available.
15
- *
16
- * Run with: OPENAI_API_KEY=your-key pnpm test:run
17
- */
18
- const SKIP_INTEGRATION_TESTS = !process.env.OPENAI_API_KEY;
19
- describe.skipIf(SKIP_INTEGRATION_TESTS)("Thread streaming integration", () => {
20
- let kernl;
21
- let model;
22
- beforeAll(() => {
23
- kernl = new Kernl();
24
- model = new AISDKLanguageModel(openai("gpt-4.1"));
25
- });
26
- describe("stream()", () => {
27
- it("should yield both delta events and complete items", async () => {
28
- const agent = new Agent({
29
- id: "test-stream",
30
- name: "Test Stream Agent",
31
- instructions: "You are a helpful assistant.",
32
- model,
33
- });
34
- const input = [
35
- {
36
- kind: "message",
37
- id: "msg-1",
38
- role: "user",
39
- content: [
40
- { kind: "text", text: "Say 'Hello World' and nothing else." },
41
- ],
42
- },
43
- ];
44
- const thread = new Thread({ agent, input });
45
- const events = [];
46
- for await (const event of thread.stream()) {
47
- events.push(event);
48
- }
49
- expect(events.length).toBeGreaterThan(0);
50
- // Should have text-delta events (for streaming UX)
51
- const textDeltas = events.filter((e) => e.kind === "text.delta");
52
- expect(textDeltas.length).toBeGreaterThan(0);
53
- // Should have text-start event
54
- const textStarts = events.filter((e) => e.kind === "text.start");
55
- expect(textStarts.length).toBeGreaterThan(0);
56
- // Should have text-end event
57
- const textEnds = events.filter((e) => e.kind === "text.end");
58
- expect(textEnds.length).toBeGreaterThan(0);
59
- // Should have complete Message item (for history)
60
- const messages = events.filter((e) => e.kind === "message");
61
- expect(messages.length).toBeGreaterThan(0);
62
- const assistantMessage = messages.find((m) => m.role === "assistant");
63
- expect(assistantMessage).toBeDefined();
64
- expect(assistantMessage.content).toBeDefined();
65
- expect(assistantMessage.content.length).toBeGreaterThan(0);
66
- // Message should have accumulated text from all deltas
67
- const textContent = assistantMessage.content.find((c) => c.kind === "text");
68
- expect(textContent).toBeDefined();
69
- expect(textContent.text).toBeDefined();
70
- expect(textContent.text.length).toBeGreaterThan(0);
71
- // Verify accumulated text matches concatenated deltas
72
- const accumulatedFromDeltas = textDeltas.map((d) => d.text).join("");
73
- expect(textContent.text).toBe(accumulatedFromDeltas);
74
- // Should have finish event
75
- const finishEvents = events.filter((e) => e.kind === "finish");
76
- expect(finishEvents.length).toBe(1);
77
- }, 30000);
78
- it("should filter deltas from history but include complete items", async () => {
79
- const agent = new Agent({
80
- id: "test-history",
81
- name: "Test History Agent",
82
- instructions: "You are a helpful assistant.",
83
- model,
84
- });
85
- const input = [
86
- {
87
- kind: "message",
88
- id: "msg-1",
89
- role: "user",
90
- content: [{ kind: "text", text: "Count to 3" }],
91
- },
92
- ];
93
- const thread = new Thread({ agent, input });
94
- const streamEvents = [];
95
- for await (const event of thread.stream()) {
96
- streamEvents.push(event);
97
- }
98
- // Access private history via type assertion for testing
99
- const history = thread.history;
100
- // History should only contain complete items (message, reasoning, tool-call, tool-result)
101
- // TypeScript already enforces this via ThreadEvent type, but let's verify at runtime
102
- for (const event of history) {
103
- expect(["message", "reasoning", "tool-call", "tool-result"]).toContain(event.kind);
104
- }
105
- // Stream events should include deltas (but history should not)
106
- const streamDeltas = streamEvents.filter((e) => e.kind === "text.delta" ||
107
- e.kind === "text.start" ||
108
- e.kind === "text.end");
109
- expect(streamDeltas.length).toBeGreaterThan(0);
110
- // History should contain the input message (with ThreadEvent headers added)
111
- expect(history[0]).toMatchObject({
112
- kind: "message",
113
- role: "user",
114
- content: [{ kind: "text", text: "Count to 3" }],
115
- });
116
- // History should contain complete Message items
117
- const historyMessages = history.filter((e) => e.kind === "message");
118
- expect(historyMessages.length).toBeGreaterThan(1); // input + assistant response
119
- // Verify assistant message has complete text (not deltas)
120
- const assistantMessage = historyMessages.find((m) => m.role === "assistant");
121
- expect(assistantMessage).toBeDefined();
122
- const textContent = assistantMessage.content.find((c) => c.kind === "text");
123
- expect(textContent.text).toBeTruthy();
124
- expect(textContent.text.length).toBeGreaterThan(0);
125
- }, 30000);
126
- it("should work with tool calls", async () => {
127
- const addTool = tool({
128
- id: "add",
129
- name: "add",
130
- description: "Add two numbers together",
131
- parameters: z.object({
132
- a: z.number().describe("The first number"),
133
- b: z.number().describe("The second number"),
134
- }),
135
- execute: async (ctx, { a, b }) => {
136
- return a + b;
137
- },
138
- });
139
- const toolkit = new Toolkit({
140
- id: "math",
141
- tools: [addTool],
142
- });
143
- const agent = new Agent({
144
- id: "test-tools",
145
- name: "Test Tools Agent",
146
- instructions: "You are a helpful assistant that can do math.",
147
- model,
148
- toolkits: [toolkit],
149
- });
150
- const input = [
151
- {
152
- kind: "message",
153
- id: "msg-1",
154
- role: "user",
155
- content: [{ kind: "text", text: "What is 25 + 17?" }],
156
- },
157
- ];
158
- const thread = new Thread({ agent, input });
159
- const events = [];
160
- for await (const event of thread.stream()) {
161
- events.push(event);
162
- }
163
- expect(events.length).toBeGreaterThan(0);
164
- // Should have tool calls
165
- const toolCalls = events.filter((e) => e.kind === "tool.call");
166
- expect(toolCalls.length).toBeGreaterThan(0);
167
- // Verify tool was called with correct parameters
168
- const addToolCall = toolCalls.find((tc) => tc.toolId === "add");
169
- expect(addToolCall).toBeDefined();
170
- expect(JSON.parse(addToolCall.arguments)).toEqual({ a: 25, b: 17 });
171
- // Should have tool results
172
- const toolResults = events.filter((e) => e.kind === "tool.result");
173
- expect(toolResults.length).toBeGreaterThan(0);
174
- // Verify tool result is correct
175
- const addToolResult = toolResults.find((tr) => tr.callId === addToolCall.callId);
176
- expect(addToolResult).toBeDefined();
177
- expect(addToolResult.result).toBe(42);
178
- // History should contain tool calls and results
179
- const history = thread.history;
180
- const historyToolCalls = history.filter((e) => e.kind === "tool.call");
181
- const historyToolResults = history.filter((e) => e.kind === "tool.result");
182
- expect(historyToolCalls.length).toBe(toolCalls.length);
183
- expect(historyToolResults.length).toBe(toolResults.length);
184
- // Verify the assistant's final response references the correct answer
185
- const messages = events.filter((e) => e.kind === "message");
186
- const assistantMessage = messages.find((m) => m.role === "assistant");
187
- expect(assistantMessage).toBeDefined();
188
- const textContent = assistantMessage.content.find((c) => c.kind === "text");
189
- expect(textContent).toBeDefined();
190
- expect(textContent.text).toContain("42");
191
- }, 30000);
192
- it("should properly encode tool results with matching callIds for multi-turn", async () => {
193
- const multiplyTool = tool({
194
- id: "multiply",
195
- name: "multiply",
196
- description: "Multiply two numbers",
197
- parameters: z.object({
198
- a: z.number().describe("First number"),
199
- b: z.number().describe("Second number"),
200
- }),
201
- execute: async (ctx, { a, b }) => {
202
- return a * b;
203
- },
204
- });
205
- const toolkit = new Toolkit({
206
- id: "math",
207
- tools: [multiplyTool],
208
- });
209
- const agent = new Agent({
210
- id: "test-multi-turn",
211
- name: "Test Multi-Turn Agent",
212
- instructions: "You are a helpful assistant that can do math.",
213
- model,
214
- toolkits: [toolkit],
215
- });
216
- const input = [
217
- {
218
- kind: "message",
219
- id: "msg-1",
220
- role: "user",
221
- content: [{ kind: "text", text: "What is 7 times 6?" }],
222
- },
223
- ];
224
- const thread = new Thread({ agent, input });
225
- const events = [];
226
- // Collect all events from the stream
227
- for await (const event of thread.stream()) {
228
- events.push(event);
229
- }
230
- // Find the tool call and result
231
- const toolCalls = events.filter((e) => e.kind === "tool.call");
232
- const toolResults = events.filter((e) => e.kind === "tool.result");
233
- expect(toolCalls.length).toBeGreaterThan(0);
234
- expect(toolResults.length).toBeGreaterThan(0);
235
- const multiplyCall = toolCalls[0];
236
- const multiplyResult = toolResults[0];
237
- // Verify callId matches between tool call and result
238
- expect(multiplyCall.callId).toBe(multiplyResult.callId);
239
- expect(multiplyCall.toolId).toBe("multiply");
240
- expect(multiplyResult.toolId).toBe("multiply");
241
- // Verify the tool result has the correct structure
242
- expect(multiplyResult.callId).toBeDefined();
243
- expect(typeof multiplyResult.callId).toBe("string");
244
- expect(multiplyResult.callId.length).toBeGreaterThan(0);
245
- // Verify history contains both with matching callIds
246
- const history = thread.history;
247
- const historyToolCall = history.find((e) => e.kind === "tool.call" && e.toolId === "multiply");
248
- const historyToolResult = history.find((e) => e.kind === "tool.result" && e.toolId === "multiply");
249
- expect(historyToolCall).toBeDefined();
250
- expect(historyToolResult).toBeDefined();
251
- expect(historyToolCall.callId).toBe(historyToolResult.callId);
252
- // Verify final response uses the tool result
253
- const messages = events.filter((e) => e.kind === "message");
254
- const assistantMessage = messages.find((m) => m.role === "assistant");
255
- expect(assistantMessage).toBeDefined();
256
- const textContent = assistantMessage.content.find((c) => c.kind === "text");
257
- expect(textContent).toBeDefined();
258
- expect(textContent.text).toContain("42");
259
- }, 30000);
260
- });
261
- describe("execute()", () => {
262
- it("should consume stream and return final response", async () => {
263
- const agent = new Agent({
264
- id: "test-blocking",
265
- name: "Test Blocking Agent",
266
- instructions: "You are a helpful assistant.",
267
- model,
268
- });
269
- const input = [
270
- {
271
- kind: "message",
272
- id: "msg-1",
273
- role: "user",
274
- content: [{ kind: "text", text: "Say 'Testing' and nothing else." }],
275
- },
276
- ];
277
- const thread = new Thread({ agent, input });
278
- const result = await thread.execute();
279
- // Should have a response
280
- expect(result.response).toBeDefined();
281
- expect(typeof result.response).toBe("string");
282
- expect(result.response.length).toBeGreaterThan(0);
283
- // Should have final state
284
- expect(result.state).toBe("stopped");
285
- }, 30000);
286
- it("should validate structured output in blocking mode", async () => {
287
- const PersonSchema = z.object({
288
- name: z.string(),
289
- age: z.number(),
290
- });
291
- const agent = new Agent({
292
- id: "test-structured",
293
- name: "Test Structured Agent",
294
- instructions: "You are a helpful assistant. Return JSON with name and age fields.",
295
- model,
296
- output: PersonSchema,
297
- });
298
- const input = [
299
- {
300
- kind: "message",
301
- id: "msg-1",
302
- role: "user",
303
- content: [
304
- {
305
- kind: "text",
306
- text: 'Return a JSON object with name "Alice" and age 30',
307
- },
308
- ],
309
- },
310
- ];
311
- const thread = new Thread({ agent, input });
312
- const result = await thread.execute();
313
- // Response should be validated and parsed
314
- expect(result.response).toBeDefined();
315
- expect(typeof result.response).toBe("object");
316
- expect(result.response.name).toBeTruthy();
317
- expect(typeof result.response.age).toBe("number");
318
- }, 30000);
319
- });
320
- });