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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +34 -0
- package/dist/agent/base.d.ts +5 -1
- package/dist/agent/base.d.ts.map +1 -1
- package/dist/agent/base.js +8 -0
- package/dist/agent.d.ts +1 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +15 -2
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +2 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/kernl/kernl.d.ts +1 -2
- package/dist/kernl/kernl.d.ts.map +1 -1
- package/dist/kernl/kernl.js +61 -1
- package/dist/lib/env.d.ts.map +1 -1
- package/dist/lib/env.js +1 -1
- package/dist/lifecycle/__tests__/hooks.test.d.ts +2 -0
- package/dist/lifecycle/__tests__/hooks.test.d.ts.map +1 -0
- package/dist/lifecycle/__tests__/hooks.test.js +553 -0
- package/dist/lifecycle.d.ts +222 -120
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +5 -23
- package/dist/memory/memory.js +1 -1
- package/dist/realtime/index.d.ts +1 -1
- package/dist/realtime/index.d.ts.map +1 -1
- package/dist/realtime/index.js +1 -1
- package/dist/realtime/session.d.ts +31 -22
- package/dist/realtime/session.d.ts.map +1 -1
- package/dist/realtime/session.js +64 -55
- package/dist/realtime/transport.d.ts +45 -0
- package/dist/realtime/transport.d.ts.map +1 -0
- package/dist/realtime/transport.js +32 -0
- package/dist/realtime/types.d.ts +8 -2
- package/dist/realtime/types.d.ts.map +1 -1
- package/dist/thread/__tests__/thread.test.js +2 -2
- package/dist/thread/thread.d.ts +2 -2
- package/dist/thread/thread.d.ts.map +1 -1
- package/dist/thread/thread.js +75 -8
- package/dist/thread/types.d.ts +1 -1
- package/dist/thread/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/agent/base.ts +13 -1
- package/src/agent.ts +17 -3
- package/src/context.ts +2 -0
- package/src/index.ts +10 -1
- package/src/kernl/kernl.ts +67 -3
- package/src/lib/env.ts +3 -1
- package/src/lifecycle/__tests__/hooks.test.ts +668 -0
- package/src/lifecycle.ts +289 -163
- package/src/memory/memory.ts +1 -1
- package/src/realtime/index.ts +1 -1
- package/src/realtime/session.ts +88 -64
- package/src/realtime/transport.ts +64 -0
- package/src/realtime/types.ts +10 -2
- package/src/thread/__tests__/thread.test.ts +2 -2
- package/src/thread/thread.ts +88 -10
- package/src/thread/types.ts +1 -1
- package/dist/realtime/channel.d.ts +0 -30
- package/dist/realtime/channel.d.ts.map +0 -1
- package/dist/realtime/channel.js +0 -1
- package/src/realtime/channel.ts +0 -32
package/src/lifecycle.ts
CHANGED
|
@@ -1,181 +1,307 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Emitter } from "@kernl-sdk/shared";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
import { TextOutput } from "@/thread/types";
|
|
12
|
+
// --- Thread Events ---
|
|
10
13
|
|
|
11
|
-
|
|
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
|
-
*
|
|
44
|
+
* Emitted when a thread stops execution.
|
|
15
45
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
106
|
-
* of the agent.
|
|
95
|
+
* Emitted when a model call starts.
|
|
107
96
|
*/
|
|
108
|
-
export
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
*
|
|
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
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
*
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
*
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
|
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> {}
|
package/src/memory/memory.ts
CHANGED