kernl 0.10.0 → 0.11.1

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/src/agent/base.ts CHANGED
@@ -4,7 +4,7 @@ import type { Context, UnknownContext } from "@/context";
4
4
  import type { Tool, BaseToolkit } from "@/tool";
5
5
  import { memory } from "@/tool";
6
6
  import { MisconfiguredError } from "@/lib/error";
7
- import { AgentHooks } from "@/lifecycle";
7
+ import { AgentHooks, type AgentHookEvents } from "@/lifecycle";
8
8
  import type { Kernl } from "@/kernl";
9
9
  import type {
10
10
  AgentMemoryCreate,
@@ -101,6 +101,18 @@ export abstract class BaseAgent<
101
101
  }
102
102
  }
103
103
 
104
+ /**
105
+ * Emit a lifecycle event to agent and kernl listeners.
106
+ */
107
+ emit<K extends keyof AgentHookEvents<TContext, TOutput>>(
108
+ event: K,
109
+ ...args: AgentHookEvents<TContext, TOutput>[K]
110
+ ): boolean {
111
+ const result = super.emit(event, ...args);
112
+ this.kernl?.emit(event, ...(args as any));
113
+ return result;
114
+ }
115
+
104
116
  /**
105
117
  * Get a specific tool by ID from systools and toolkits.
106
118
  */
package/src/agent.ts CHANGED
@@ -13,7 +13,7 @@ import type {
13
13
  RThreadHistoryParams,
14
14
  RThreadUpdateParams,
15
15
  } from "@/api/resources/threads/types";
16
- import type { Context, UnknownContext } from "./context";
16
+ import { Context, type UnknownContext } from "./context";
17
17
  import {
18
18
  InputGuardrail,
19
19
  OutputGuardrail,
@@ -102,10 +102,14 @@ export class Agent<
102
102
 
103
103
  // create new thread if not found in storage or no tid provided
104
104
  if (!thread) {
105
+ const ctx = options?.context
106
+ ? new Context<TContext>(options?.namespace ?? "kernl", options.context)
107
+ : undefined;
108
+
105
109
  thread = new Thread({
106
110
  agent: this,
107
111
  input: items,
108
- context: options?.context,
112
+ context: ctx,
109
113
  model: options?.model,
110
114
  task: options?.task,
111
115
  tid: options?.threadId,
@@ -116,6 +120,9 @@ export class Agent<
116
120
  }
117
121
 
118
122
  // resume existing thread from storage
123
+ if (options?.context) {
124
+ thread.context.context = options.context;
125
+ }
119
126
  thread.append(...items);
120
127
  return this.kernl.schedule(thread);
121
128
  }
@@ -163,10 +170,14 @@ export class Agent<
163
170
 
164
171
  // create new thread if not found in storage or no tid provided
165
172
  if (!thread) {
173
+ const ctx = options?.context
174
+ ? new Context<TContext>(options?.namespace ?? "kernl", options.context)
175
+ : undefined;
176
+
166
177
  thread = new Thread({
167
178
  agent: this,
168
179
  input: items,
169
- context: options?.context,
180
+ context: ctx,
170
181
  model: options?.model,
171
182
  task: options?.task,
172
183
  tid: options?.threadId,
@@ -178,6 +189,9 @@ export class Agent<
178
189
  }
179
190
 
180
191
  // resume existing thread from storage
192
+ if (options?.context) {
193
+ thread.context.context = options.context;
194
+ }
181
195
  thread.append(...items);
182
196
  yield* this.kernl.scheduleStream(thread);
183
197
  }
package/src/context.ts CHANGED
@@ -23,6 +23,8 @@ export class Context<TContext = UnknownContext> {
23
23
  /**
24
24
  * The agent executing this context.
25
25
  * Set by the thread during execution.
26
+ *
27
+ * NOTE: Primarily used by system tools (e.g., memory) that need agent access.
26
28
  */
27
29
  agent?: Agent<TContext, any>;
28
30
 
package/src/index.ts CHANGED
@@ -8,6 +8,18 @@ export type {
8
8
  export { Agent } from "./agent";
9
9
  export { Context } from "./context";
10
10
 
11
+ // --- lifecycle hooks ---
12
+
13
+ export type {
14
+ LifecycleEvent,
15
+ ThreadStartEvent,
16
+ ThreadStopEvent,
17
+ ModelCallStartEvent,
18
+ ModelCallEndEvent,
19
+ ToolCallStartEvent,
20
+ ToolCallEndEvent,
21
+ } from "./lifecycle";
22
+
11
23
  // --- realtime ---
12
24
 
13
25
  export { RealtimeAgent, RealtimeSession, WebSocketTransport } from "./realtime";
@@ -2,7 +2,6 @@ import type { LanguageModel } from "@kernl-sdk/protocol";
2
2
  import { resolveEmbeddingModel } from "@kernl-sdk/retrieval";
3
3
 
4
4
  import { BaseAgent } from "@/agent/base";
5
- import { UnknownContext } from "@/context";
6
5
  import { KernlHooks } from "@/lifecycle";
7
6
  import type { Thread } from "@/thread";
8
7
  import type { ResolvedAgentResponse } from "@/guardrail";
@@ -28,7 +27,7 @@ import type { KernlOptions, MemoryOptions, StorageOptions } from "./types";
28
27
  * Orchestrates agent execution, including guardrails, tool calls, session persistence, and
29
28
  * tracing.
30
29
  */
31
- export class Kernl extends KernlHooks<UnknownContext, AgentOutputType> {
30
+ export class Kernl extends KernlHooks {
32
31
  private readonly _agents: Map<string, BaseAgent> = new Map();
33
32
  private readonly _models: Map<string, LanguageModel> = new Map();
34
33
 
@@ -103,8 +102,42 @@ export class Kernl extends KernlHooks<UnknownContext, AgentOutputType> {
103
102
  thread: Thread<TContext, TOutput>,
104
103
  ): Promise<ThreadExecuteResult<ResolvedAgentResponse<TOutput>>> {
105
104
  this.athreads.set(thread.tid, thread);
105
+
106
+ this.emit("thread.start", {
107
+ kind: "thread.start",
108
+ threadId: thread.tid,
109
+ agentId: thread.agent.id,
110
+ namespace: thread.namespace,
111
+ context: thread.context,
112
+ });
113
+
106
114
  try {
107
- return await thread.execute();
115
+ const result = await thread.execute();
116
+
117
+ this.emit("thread.stop", {
118
+ kind: "thread.stop",
119
+ threadId: thread.tid,
120
+ agentId: thread.agent.id,
121
+ namespace: thread.namespace,
122
+ context: thread.context,
123
+ state: thread.state,
124
+ outcome: "success",
125
+ result: result.response,
126
+ });
127
+
128
+ return result;
129
+ } catch (err) {
130
+ this.emit("thread.stop", {
131
+ kind: "thread.stop",
132
+ threadId: thread.tid,
133
+ agentId: thread.agent.id,
134
+ namespace: thread.namespace,
135
+ context: thread.context,
136
+ state: thread.state,
137
+ outcome: "error",
138
+ error: err instanceof Error ? err.message : String(err),
139
+ });
140
+ throw err;
108
141
  } finally {
109
142
  this.athreads.delete(thread.tid);
110
143
  }
@@ -135,8 +168,39 @@ export class Kernl extends KernlHooks<UnknownContext, AgentOutputType> {
135
168
  thread: Thread<TContext, TOutput>,
136
169
  ): AsyncIterable<ThreadStreamEvent> {
137
170
  this.athreads.set(thread.tid, thread);
171
+
172
+ this.emit("thread.start", {
173
+ kind: "thread.start",
174
+ threadId: thread.tid,
175
+ agentId: thread.agent.id,
176
+ namespace: thread.namespace,
177
+ context: thread.context,
178
+ });
179
+
138
180
  try {
139
181
  yield* thread.stream();
182
+
183
+ this.emit("thread.stop", {
184
+ kind: "thread.stop",
185
+ threadId: thread.tid,
186
+ agentId: thread.agent.id,
187
+ namespace: thread.namespace,
188
+ context: thread.context,
189
+ state: thread.state,
190
+ outcome: "success",
191
+ });
192
+ } catch (err) {
193
+ this.emit("thread.stop", {
194
+ kind: "thread.stop",
195
+ threadId: thread.tid,
196
+ agentId: thread.agent.id,
197
+ namespace: thread.namespace,
198
+ context: thread.context,
199
+ state: thread.state,
200
+ outcome: "error",
201
+ error: err instanceof Error ? err.message : String(err),
202
+ });
203
+ throw err;
140
204
  } finally {
141
205
  this.athreads.delete(thread.tid);
142
206
  }