kernl 0.9.1 → 0.11.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.
Files changed (64) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +34 -0
  3. package/dist/agent/base.d.ts +5 -1
  4. package/dist/agent/base.d.ts.map +1 -1
  5. package/dist/agent/base.js +8 -0
  6. package/dist/agent.d.ts +1 -1
  7. package/dist/agent.d.ts.map +1 -1
  8. package/dist/agent.js +15 -2
  9. package/dist/context.d.ts +2 -0
  10. package/dist/context.d.ts.map +1 -1
  11. package/dist/context.js +2 -0
  12. package/dist/index.d.ts +2 -1
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +2 -1
  15. package/dist/kernl/kernl.d.ts +1 -2
  16. package/dist/kernl/kernl.d.ts.map +1 -1
  17. package/dist/kernl/kernl.js +61 -1
  18. package/dist/lib/env.d.ts.map +1 -1
  19. package/dist/lib/env.js +1 -1
  20. package/dist/lifecycle/__tests__/hooks.test.d.ts +2 -0
  21. package/dist/lifecycle/__tests__/hooks.test.d.ts.map +1 -0
  22. package/dist/lifecycle/__tests__/hooks.test.js +553 -0
  23. package/dist/lifecycle.d.ts +222 -120
  24. package/dist/lifecycle.d.ts.map +1 -1
  25. package/dist/lifecycle.js +5 -23
  26. package/dist/memory/memory.js +1 -1
  27. package/dist/realtime/index.d.ts +1 -1
  28. package/dist/realtime/index.d.ts.map +1 -1
  29. package/dist/realtime/index.js +1 -1
  30. package/dist/realtime/session.d.ts +31 -22
  31. package/dist/realtime/session.d.ts.map +1 -1
  32. package/dist/realtime/session.js +64 -55
  33. package/dist/realtime/transport.d.ts +45 -0
  34. package/dist/realtime/transport.d.ts.map +1 -0
  35. package/dist/realtime/transport.js +32 -0
  36. package/dist/realtime/types.d.ts +8 -2
  37. package/dist/realtime/types.d.ts.map +1 -1
  38. package/dist/thread/__tests__/thread.test.js +2 -2
  39. package/dist/thread/thread.d.ts +2 -2
  40. package/dist/thread/thread.d.ts.map +1 -1
  41. package/dist/thread/thread.js +75 -8
  42. package/dist/thread/types.d.ts +1 -1
  43. package/dist/thread/types.d.ts.map +1 -1
  44. package/package.json +4 -4
  45. package/src/agent/base.ts +13 -1
  46. package/src/agent.ts +17 -3
  47. package/src/context.ts +2 -0
  48. package/src/index.ts +10 -1
  49. package/src/kernl/kernl.ts +67 -3
  50. package/src/lib/env.ts +3 -1
  51. package/src/lifecycle/__tests__/hooks.test.ts +668 -0
  52. package/src/lifecycle.ts +289 -163
  53. package/src/memory/memory.ts +1 -1
  54. package/src/realtime/index.ts +1 -1
  55. package/src/realtime/session.ts +88 -64
  56. package/src/realtime/transport.ts +64 -0
  57. package/src/realtime/types.ts +10 -2
  58. package/src/thread/__tests__/thread.test.ts +2 -2
  59. package/src/thread/thread.ts +88 -10
  60. package/src/thread/types.ts +1 -1
  61. package/dist/realtime/channel.d.ts +0 -30
  62. package/dist/realtime/channel.d.ts.map +0 -1
  63. package/dist/realtime/channel.js +0 -1
  64. package/src/realtime/channel.ts +0 -32
package/src/lifecycle.ts CHANGED
@@ -1,181 +1,307 @@
1
- import { EventEmitter } from "node:events";
1
+ import { Emitter } from "@kernl-sdk/shared";
2
2
 
3
- import { Agent } from "./agent";
4
- import { Context, UnknownContext } from "./context";
5
- import { Tool } from "./tool";
6
- import type { ToolCall } from "@kernl-sdk/protocol";
3
+ import type {
4
+ LanguageModelUsage,
5
+ LanguageModelFinishReason,
6
+ LanguageModelRequestSettings,
7
+ ToolCallState,
8
+ } from "@kernl-sdk/protocol";
9
+ import type { Context } from "@/context";
10
+ import type { ThreadState } from "@/thread/types";
7
11
 
8
- import { AgentOutputType } from "@/agent/types";
9
- import { TextOutput } from "@/thread/types";
12
+ // --- Thread Events ---
10
13
 
11
- export type EventEmitterEvents = Record<string, any[]>;
14
+ /**
15
+ * Emitted when a thread starts execution.
16
+ */
17
+ export interface ThreadStartEvent<TContext = unknown> {
18
+ readonly kind: "thread.start";
19
+
20
+ /**
21
+ * The thread ID.
22
+ */
23
+ threadId: string;
24
+
25
+ /**
26
+ * The agent executing this thread.
27
+ */
28
+ agentId: string;
29
+
30
+ /**
31
+ * The namespace of the thread.
32
+ */
33
+ namespace: string;
34
+
35
+ /**
36
+ * The context for this execution.
37
+ *
38
+ * NOTE: Includes `context.agent` reference for tools - may be optimized in future.
39
+ */
40
+ context: Context<TContext>;
41
+ }
12
42
 
13
43
  /**
14
- * Generic typed event emitter that wraps Node's EventEmitter with type safety
44
+ * Emitted when a thread stops execution.
15
45
  */
16
- class TypedEventEmitter<
17
- EventTypes extends EventEmitterEvents = Record<string, any[]>,
18
- > extends EventEmitter {
19
- // Overload for typed events
20
- on<K extends keyof EventTypes>(
21
- event: K,
22
- listener: (...args: EventTypes[K]) => void,
23
- ): this;
24
- // Fallback for compatibility with parent
25
- on(event: string | symbol, listener: (...args: any[]) => void): this;
26
- on(event: any, listener: any): this {
27
- return super.on(event, listener);
28
- }
29
-
30
- // Overload for typed events
31
- off<K extends keyof EventTypes>(
32
- event: K,
33
- listener: (...args: EventTypes[K]) => void,
34
- ): this;
35
- // Fallback for compatibility with parent
36
- off(event: string | symbol, listener: (...args: any[]) => void): this;
37
- off(event: any, listener: any): this {
38
- return super.off(event, listener);
39
- }
40
-
41
- // Overload for typed events
42
- emit<K extends keyof EventTypes>(event: K, ...args: EventTypes[K]): boolean;
43
- // Fallback for compatibility with parent
44
- emit(event: string | symbol, ...args: any[]): boolean;
45
- emit(event: any, ...args: any[]): boolean {
46
- return super.emit(event, ...args);
47
- }
48
-
49
- // Overload for typed events
50
- once<K extends keyof EventTypes>(
51
- event: K,
52
- listener: (...args: EventTypes[K]) => void,
53
- ): this;
54
- // Fallback for compatibility with parent
55
- once(event: string | symbol, listener: (...args: any[]) => void): this;
56
- once(event: any, listener: any): this {
57
- return super.once(event, listener);
58
- }
46
+ export interface ThreadStopEvent<TContext = unknown, TOutput = unknown> {
47
+ readonly kind: "thread.stop";
48
+
49
+ /**
50
+ * The thread ID.
51
+ */
52
+ threadId: string;
53
+
54
+ /**
55
+ * The agent that executed this thread.
56
+ */
57
+ agentId: string;
58
+
59
+ /**
60
+ * The namespace of the thread.
61
+ */
62
+ namespace: string;
63
+
64
+ /**
65
+ * The context for this execution.
66
+ *
67
+ * NOTE: Includes `context.agent` reference for tools - may be optimized in future.
68
+ */
69
+ context: Context<TContext>;
70
+
71
+ /**
72
+ * Final state of the thread.
73
+ */
74
+ state: ThreadState;
75
+
76
+ /**
77
+ * The outcome of the execution.
78
+ */
79
+ outcome: "success" | "error" | "cancelled";
80
+
81
+ /**
82
+ * The result if outcome is "success".
83
+ */
84
+ result?: TOutput;
85
+
86
+ /**
87
+ * Error message if outcome is "error".
88
+ */
89
+ error?: string;
59
90
  }
60
91
 
61
- export type AgentHookEvents<
62
- TContext = UnknownContext,
63
- TOutput extends AgentOutputType = TextOutput,
64
- > = {
65
- /**
66
- * @param context - The context of the run
67
- */
68
- agent_start: [context: Context<TContext>, agent: Agent<TContext, TOutput>];
69
- /**
70
- * @param context - The context of the run
71
- * @param output - The output of the agent
72
- */
73
- agent_end: [context: Context<TContext>, output: string];
74
- // /**
75
- // * @param context - The context of the run
76
- // * @param agent - The agent that is handing off
77
- // * @param nextAgent - The next agent to run
78
- // */
79
- // agent_handoff: [context: Context<TContext>, nextAgent: Agent<any, any>];
80
- /**
81
- * @param context - The context of the run
82
- * @param agent - The agent that is starting a tool
83
- * @param tool - The tool that is starting
84
- */
85
- agent_tool_start: [
86
- context: Context<TContext>,
87
- tool: Tool<any>,
88
- details: { toolCall: ToolCall },
89
- ];
90
- /**
91
- * @param context - The context of the run
92
- * @param agent - The agent that is ending a tool
93
- * @param tool - The tool that is ending
94
- * @param result - The result of the tool
95
- */
96
- agent_tool_end: [
97
- context: Context<TContext>,
98
- tool: Tool<any>,
99
- result: string,
100
- details: { toolCall: ToolCall },
101
- ];
102
- };
92
+ // --- Model Events ---
103
93
 
104
94
  /**
105
- * Event emitter that every Agent instance inherits from and that emits events for the lifecycle
106
- * of the agent.
95
+ * Emitted when a model call starts.
107
96
  */
108
- export class AgentHooks<
109
- TContext = UnknownContext,
110
- TOutput extends AgentOutputType = TextOutput,
111
- > extends TypedEventEmitter<AgentHookEvents<TContext, TOutput>> {}
97
+ export interface ModelCallStartEvent<TContext = unknown> {
98
+ readonly kind: "model.call.start";
99
+
100
+ /**
101
+ * The model provider.
102
+ */
103
+ provider: string;
104
+
105
+ /**
106
+ * The model ID.
107
+ */
108
+ modelId: string;
109
+
110
+ /**
111
+ * Request settings passed to the model.
112
+ */
113
+ settings: LanguageModelRequestSettings;
114
+
115
+ /**
116
+ * Thread ID if called within a thread context.
117
+ */
118
+ threadId?: string;
119
+
120
+ /**
121
+ * Agent ID if called within an agent context.
122
+ */
123
+ agentId?: string;
124
+
125
+ /**
126
+ * Execution context if available.
127
+ *
128
+ * NOTE: Includes `context.agent` reference for tools - may be optimized in future.
129
+ */
130
+ context?: Context<TContext>;
131
+ }
132
+
133
+ /**
134
+ * Emitted when a model call ends.
135
+ */
136
+ export interface ModelCallEndEvent<TContext = unknown> {
137
+ readonly kind: "model.call.end";
138
+
139
+ /**
140
+ * The model provider.
141
+ */
142
+ provider: string;
143
+
144
+ /**
145
+ * The model ID.
146
+ */
147
+ modelId: string;
148
+
149
+ /**
150
+ * Reason the model stopped generating.
151
+ */
152
+ finishReason: LanguageModelFinishReason;
153
+
154
+ /**
155
+ * Token usage for this call.
156
+ */
157
+ usage?: LanguageModelUsage;
158
+
159
+ /**
160
+ * Thread ID if called within a thread context.
161
+ */
162
+ threadId?: string;
163
+
164
+ /**
165
+ * Agent ID if called within an agent context.
166
+ */
167
+ agentId?: string;
168
+
169
+ /**
170
+ * Execution context if available.
171
+ *
172
+ * NOTE: Includes `context.agent` reference for tools - may be optimized in future.
173
+ */
174
+ context?: Context<TContext>;
175
+ }
176
+
177
+ // --- Tool Events ---
112
178
 
113
179
  /**
114
- * Events emitted by the kernl during execution.
115
- *
116
- * Unlike AgentHookEvents (which are emitted by individual agents with implicit context),
117
- * KernlHookEvents explicitly include the agent reference in all events since it needs to
118
- * coordinate multiple agents and listeners need to know which agent triggered each event.
180
+ * Emitted when a tool call starts.
119
181
  */
120
- export type KernlHookEvents<
121
- TContext = UnknownContext,
122
- TOutput extends AgentOutputType = TextOutput,
123
- > = {
124
- /**
125
- * @param context - The context of the run
126
- * @param agent - The agent that is starting
127
- */
128
- agent_start: [context: Context<TContext>, agent: Agent<TContext, TOutput>];
129
- /**
130
- * @param context - The context of the run
131
- * @param agent - The agent that is ending
132
- * @param output - The output of the agent
133
- */
134
- agent_end: [
135
- context: Context<TContext>,
136
- agent: Agent<TContext, TOutput>,
137
- output: string,
138
- ];
139
- /**
140
- * @param context - The context of the run
141
- * @param fromAgent - The agent that is handing off
142
- * @param toAgent - The next agent to run
143
- */
144
- agent_handoff: [
145
- context: Context<TContext>,
146
- fromAgent: Agent<any, any>,
147
- toAgent: Agent<any, any>,
148
- ];
149
- /**
150
- * @param context - The context of the run
151
- * @param agent - The agent that is starting a tool
152
- * @param tool - The tool that is starting
153
- */
154
- agent_tool_start: [
155
- context: Context<TContext>,
156
- agent: Agent<TContext, TOutput>,
157
- tool: Tool,
158
- details: { toolCall: ToolCall },
159
- ];
160
- /**
161
- * @param context - The context of the run
162
- * @param agent - The agent that is ending a tool
163
- * @param tool - The tool that is ending
164
- * @param result - The result of the tool
165
- */
166
- agent_tool_end: [
167
- context: Context<TContext>,
168
- agent: Agent<TContext, TOutput>,
169
- tool: Tool,
170
- result: string,
171
- details: { toolCall: ToolCall },
172
- ];
182
+ export interface ToolCallStartEvent<TContext = unknown> {
183
+ readonly kind: "tool.call.start";
184
+
185
+ /**
186
+ * The thread ID.
187
+ */
188
+ threadId: string;
189
+
190
+ /**
191
+ * The agent executing this tool.
192
+ */
193
+ agentId: string;
194
+
195
+ /**
196
+ * The context for this execution.
197
+ *
198
+ * NOTE: Includes `context.agent` reference for tools - may be optimized in future.
199
+ */
200
+ context: Context<TContext>;
201
+
202
+ /**
203
+ * The tool being called.
204
+ */
205
+ toolId: string;
206
+
207
+ /**
208
+ * Unique identifier for this call.
209
+ */
210
+ callId: string;
211
+
212
+ /**
213
+ * Arguments passed to the tool (parsed JSON).
214
+ */
215
+ args: Record<string, unknown>;
216
+ }
217
+
218
+ /**
219
+ * Emitted when a tool call ends.
220
+ */
221
+ export interface ToolCallEndEvent<TContext = unknown> {
222
+ readonly kind: "tool.call.end";
223
+
224
+ /**
225
+ * The thread ID.
226
+ */
227
+ threadId: string;
228
+
229
+ /**
230
+ * The agent that executed this tool.
231
+ */
232
+ agentId: string;
233
+
234
+ /**
235
+ * The context for this execution.
236
+ *
237
+ * NOTE: Includes `context.agent` reference for tools - may be optimized in future.
238
+ */
239
+ context: Context<TContext>;
240
+
241
+ /**
242
+ * The tool that was called.
243
+ */
244
+ toolId: string;
245
+
246
+ /**
247
+ * Unique identifier for this call.
248
+ */
249
+ callId: string;
250
+
251
+ /**
252
+ * Final state of the tool call.
253
+ */
254
+ state: ToolCallState;
255
+
256
+ /**
257
+ * Result if state is "completed".
258
+ */
259
+ result?: string;
260
+
261
+ /**
262
+ * Error message if state is "failed", null if successful.
263
+ */
264
+ error: string | null;
265
+ }
266
+
267
+ // --- Union ---
268
+
269
+ export type LifecycleEvent<TContext = unknown, TOutput = unknown> =
270
+ | ThreadStartEvent<TContext>
271
+ | ThreadStopEvent<TContext, TOutput>
272
+ | ModelCallStartEvent<TContext>
273
+ | ModelCallEndEvent<TContext>
274
+ | ToolCallStartEvent<TContext>
275
+ | ToolCallEndEvent<TContext>;
276
+
277
+ // --- Event Maps ---
278
+
279
+ /**
280
+ * Event map for agent-level lifecycle hooks (typed).
281
+ */
282
+ export type AgentHookEvents<TContext = unknown, TOutput = unknown> = {
283
+ "thread.start": [event: ThreadStartEvent<TContext>];
284
+ "thread.stop": [event: ThreadStopEvent<TContext, TOutput>];
285
+ "model.call.start": [event: ModelCallStartEvent<TContext>];
286
+ "model.call.end": [event: ModelCallEndEvent<TContext>];
287
+ "tool.call.start": [event: ToolCallStartEvent<TContext>];
288
+ "tool.call.end": [event: ToolCallEndEvent<TContext>];
173
289
  };
174
290
 
175
291
  /**
176
- * Event emitter that the kernl uses to emit events for the lifecycle of every agent run.
292
+ * Event map for Kernl-level lifecycle hooks (untyped).
293
+ */
294
+ export type KernlHookEvents = AgentHookEvents<unknown, unknown>;
295
+
296
+ /**
297
+ * Event emitter for agent-level lifecycle events.
298
+ */
299
+ export class AgentHooks<
300
+ TContext = unknown,
301
+ TOutput = unknown,
302
+ > extends Emitter<AgentHookEvents<TContext, TOutput>> {}
303
+
304
+ /**
305
+ * Event emitter for Kernl-level lifecycle events.
177
306
  */
178
- export class KernlHooks<
179
- TContext = UnknownContext,
180
- TOutput extends AgentOutputType = TextOutput,
181
- > extends TypedEventEmitter<KernlHookEvents<TContext, TOutput>> {}
307
+ export class KernlHooks extends Emitter<KernlHookEvents> {}
@@ -97,7 +97,7 @@ export class Memory {
97
97
  return this._search.query({
98
98
  query: [{ text: q.query, tvec: tvec ?? undefined }],
99
99
  filter: q.filter ? MEMORY_FILTER.encode(q.filter) : undefined,
100
- topK: q.limit ?? 20,
100
+ limit: q.limit ?? 20,
101
101
  });
102
102
  }
103
103
 
@@ -1,4 +1,4 @@
1
1
  export * from "./types";
2
- export * from "./channel";
3
2
  export * from "./agent";
4
3
  export * from "./session";
4
+ export * from "./transport";