kernl 0.2.1 → 0.6.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/.turbo/turbo-build.log +5 -4
- package/.turbo/turbo-check-types.log +4 -0
- package/CHANGELOG.md +147 -0
- package/LICENSE +1 -1
- package/dist/agent/__tests__/concurrency.test.d.ts +2 -0
- package/dist/agent/__tests__/concurrency.test.d.ts.map +1 -0
- package/dist/agent/__tests__/concurrency.test.js +152 -0
- package/dist/agent/__tests__/run.test.d.ts +2 -0
- package/dist/agent/__tests__/run.test.d.ts.map +1 -0
- package/dist/agent/__tests__/run.test.js +357 -0
- package/dist/agent/index.d.ts +1 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent.d.ts +35 -12
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +102 -15
- package/dist/api/__tests__/cursor-page.test.d.ts +2 -0
- package/dist/api/__tests__/cursor-page.test.d.ts.map +1 -0
- package/dist/api/__tests__/cursor-page.test.js +414 -0
- package/dist/api/__tests__/offset-page.test.d.ts +2 -0
- package/dist/api/__tests__/offset-page.test.d.ts.map +1 -0
- package/dist/api/__tests__/offset-page.test.js +510 -0
- package/dist/api/__tests__/threads.test.d.ts +2 -0
- package/dist/api/__tests__/threads.test.d.ts.map +1 -0
- package/dist/api/__tests__/threads.test.js +338 -0
- package/dist/api/models/index.d.ts +2 -0
- package/dist/api/models/index.d.ts.map +1 -0
- package/dist/api/models/thread.d.ts +120 -0
- package/dist/api/models/thread.d.ts.map +1 -0
- package/dist/api/pagination/base.d.ts +48 -0
- package/dist/api/pagination/base.d.ts.map +1 -0
- package/dist/api/pagination/base.js +45 -0
- package/dist/api/pagination/cursor.d.ts +44 -0
- package/dist/api/pagination/cursor.d.ts.map +1 -0
- package/dist/api/pagination/cursor.js +52 -0
- package/dist/api/pagination/offset.d.ts +42 -0
- package/dist/api/pagination/offset.d.ts.map +1 -0
- package/dist/api/pagination/offset.js +55 -0
- package/dist/api/resources/threads/events.d.ts +21 -0
- package/dist/api/resources/threads/events.d.ts.map +1 -0
- package/dist/api/resources/threads/events.js +24 -0
- package/dist/api/resources/threads/index.d.ts +4 -0
- package/dist/api/resources/threads/index.d.ts.map +1 -0
- package/dist/api/resources/threads/index.js +2 -0
- package/dist/api/resources/threads/threads.d.ts +57 -0
- package/dist/api/resources/threads/threads.d.ts.map +1 -0
- package/dist/api/resources/threads/threads.js +199 -0
- package/dist/api/resources/threads/types.d.ts +123 -0
- package/dist/api/resources/threads/types.d.ts.map +1 -0
- package/dist/api/resources/threads/utils.d.ts +18 -0
- package/dist/api/resources/threads/utils.d.ts.map +1 -0
- package/dist/api/resources/threads/utils.js +78 -0
- package/dist/context.d.ts +5 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +6 -1
- package/dist/guardrail.d.ts +4 -4
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -3
- package/dist/internal.d.ts +4 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +2 -0
- package/dist/kernl/index.d.ts +3 -0
- package/dist/kernl/index.d.ts.map +1 -0
- package/dist/kernl/index.js +2 -0
- package/dist/kernl/kernl.d.ts +64 -0
- package/dist/kernl/kernl.d.ts.map +1 -0
- package/dist/kernl/kernl.js +116 -0
- package/dist/kernl/threads.d.ts +110 -0
- package/dist/kernl/threads.d.ts.map +1 -0
- package/dist/kernl/threads.js +126 -0
- package/dist/kernl.d.ts +27 -11
- package/dist/kernl.d.ts.map +1 -1
- package/dist/kernl.js +74 -11
- package/dist/lib/env.d.ts +3 -3
- package/dist/lib/env.js +1 -1
- package/dist/lib/error.d.ts +3 -3
- package/dist/lib/logger.js +1 -1
- package/dist/lifecycle.d.ts +5 -5
- package/dist/mcp/__tests__/base.test.js +2 -2
- package/dist/mcp/__tests__/fixtures/utils.d.ts +1 -1
- package/dist/mcp/__tests__/fixtures/utils.js +1 -1
- package/dist/mcp/__tests__/integration.test.js +16 -16
- package/dist/mcp/__tests__/stdio.test.js +2 -2
- package/dist/mcp/__tests__/utils.test.js +8 -8
- package/dist/mcp/base.d.ts +2 -2
- package/dist/mcp/http.d.ts +3 -3
- package/dist/mcp/http.d.ts.map +1 -1
- package/dist/mcp/http.js +11 -11
- package/dist/mcp/sse.d.ts +3 -3
- package/dist/mcp/sse.d.ts.map +1 -1
- package/dist/mcp/sse.js +9 -9
- package/dist/mcp/stdio.d.ts +2 -2
- package/dist/mcp/stdio.js +2 -2
- package/dist/mcp/types.d.ts +3 -3
- package/dist/mcp/utils.d.ts +4 -4
- package/dist/mcp/utils.d.ts.map +1 -1
- package/dist/mcp/utils.js +5 -6
- package/dist/storage/__tests__/in-memory.test.d.ts +2 -0
- package/dist/storage/__tests__/in-memory.test.d.ts.map +1 -0
- package/dist/storage/__tests__/in-memory.test.js +455 -0
- package/dist/storage/base.d.ts +64 -0
- package/dist/storage/base.d.ts.map +1 -0
- package/dist/storage/base.js +4 -0
- package/dist/storage/in-memory.d.ts +62 -0
- package/dist/storage/in-memory.d.ts.map +1 -0
- package/dist/storage/in-memory.js +283 -0
- package/dist/storage/index.d.ts +10 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +7 -0
- package/dist/storage/thread.d.ts +123 -0
- package/dist/storage/thread.d.ts.map +1 -0
- package/dist/storage/thread.js +4 -0
- package/dist/task.d.ts +8 -6
- package/dist/task.d.ts.map +1 -1
- package/dist/task.js +10 -8
- package/dist/thread/__tests__/fixtures/mock-model.d.ts +1 -2
- package/dist/thread/__tests__/fixtures/mock-model.d.ts.map +1 -1
- package/dist/thread/__tests__/integration.test.js +10 -10
- package/dist/thread/__tests__/mock.d.ts +1 -1
- package/dist/thread/__tests__/namespace.test.d.ts +2 -0
- package/dist/thread/__tests__/namespace.test.d.ts.map +1 -0
- package/dist/thread/__tests__/namespace.test.js +131 -0
- package/dist/thread/__tests__/thread-persistence.test.d.ts +2 -0
- package/dist/thread/__tests__/thread-persistence.test.d.ts.map +1 -0
- package/dist/thread/__tests__/thread-persistence.test.js +351 -0
- package/dist/thread/__tests__/thread.test.js +55 -57
- package/dist/thread/index.d.ts +1 -1
- package/dist/thread/index.js +1 -1
- package/dist/thread/thread.d.ts +74 -22
- package/dist/thread/thread.d.ts.map +1 -1
- package/dist/thread/thread.js +212 -74
- package/dist/thread/utils.d.ts +38 -10
- package/dist/thread/utils.d.ts.map +1 -1
- package/dist/thread/utils.js +53 -9
- package/dist/tool/__tests__/fixtures.d.ts +8 -8
- package/dist/tool/__tests__/fixtures.js +3 -3
- package/dist/tool/__tests__/tool.test.js +2 -2
- package/dist/tool/__tests__/toolkit.test.js +17 -14
- package/dist/tool/index.d.ts +3 -3
- package/dist/tool/index.js +2 -2
- package/dist/tool/tool.d.ts +2 -2
- package/dist/tool/tool.js +5 -5
- package/dist/tool/toolkit.d.ts +4 -4
- package/dist/tool/toolkit.js +1 -1
- package/dist/tool/types.d.ts +4 -4
- package/dist/trace/traces.js +2 -2
- package/dist/types/agent.d.ts +4 -4
- package/dist/types/kernl.d.ts +42 -0
- package/dist/types/kernl.d.ts.map +1 -0
- package/dist/types/thread.d.ts +110 -24
- package/dist/types/thread.d.ts.map +1 -1
- package/dist/types/thread.js +12 -0
- package/package.json +12 -8
- package/src/agent/__tests__/concurrency.test.ts +194 -0
- package/src/agent/__tests__/run.test.ts +441 -0
- package/src/agent/index.ts +0 -0
- package/src/agent.ts +139 -24
- package/src/api/__tests__/cursor-page.test.ts +512 -0
- package/src/api/__tests__/offset-page.test.ts +624 -0
- package/src/api/__tests__/threads.test.ts +415 -0
- package/src/api/models/index.ts +6 -0
- package/src/api/models/thread.ts +138 -0
- package/src/api/pagination/base.ts +79 -0
- package/src/api/pagination/cursor.ts +86 -0
- package/src/api/pagination/offset.ts +89 -0
- package/src/api/resources/threads/events.ts +26 -0
- package/src/api/resources/threads/index.ts +9 -0
- package/src/api/resources/threads/threads.ts +256 -0
- package/src/api/resources/threads/types.ts +143 -0
- package/src/api/resources/threads/utils.ts +104 -0
- package/src/context.ts +10 -1
- package/src/index.ts +49 -1
- package/src/internal.ts +15 -0
- package/src/kernl.ts +86 -17
- package/src/mcp/__tests__/integration.test.ts +8 -9
- package/src/mcp/__tests__/utils.test.ts +6 -6
- package/src/mcp/http.ts +9 -9
- package/src/mcp/sse.ts +7 -7
- package/src/mcp/utils.ts +6 -5
- package/src/storage/__tests__/in-memory.test.ts +534 -0
- package/src/storage/base.ts +77 -0
- package/src/storage/in-memory.ts +372 -0
- package/src/storage/index.ts +21 -0
- package/src/storage/thread.ts +141 -0
- package/src/task.ts +12 -10
- package/src/thread/__tests__/fixtures/mock-model.ts +2 -4
- package/src/thread/__tests__/integration.test.ts +13 -12
- package/src/thread/__tests__/namespace.test.ts +158 -0
- package/src/thread/__tests__/thread-persistence.test.ts +367 -0
- package/src/thread/__tests__/thread.test.ts +52 -54
- package/src/thread/thread.ts +247 -96
- package/src/thread/utils.ts +76 -13
- package/src/tool/__tests__/fixtures.ts +1 -1
- package/src/tool/__tests__/toolkit.test.ts +15 -12
- package/src/tool/tool.ts +3 -3
- package/src/types/kernl.ts +51 -0
- package/src/types/thread.ts +139 -25
- package/vitest.config.ts +1 -0
- package/dist/env.d.ts +0 -45
- package/dist/env.d.ts.map +0 -1
- package/dist/env.js +0 -31
- package/dist/error.d.ts +0 -1
- package/dist/error.d.ts.map +0 -1
- package/dist/kernel.d.ts +0 -7
- package/dist/kernel.d.ts.map +0 -1
- package/dist/kernel.js +0 -7
- package/dist/lib/serde/__tests__/codec.test.d.ts +0 -2
- package/dist/lib/serde/__tests__/codec.test.d.ts.map +0 -1
- package/dist/lib/serde/__tests__/codec.test.js +0 -75
- package/dist/lib/serde/codec.d.ts +0 -12
- package/dist/lib/serde/codec.d.ts.map +0 -1
- package/dist/lib/serde/codec.js +0 -54
- package/dist/lib/serde/thread.d.ts +0 -1
- package/dist/lib/serde/thread.d.ts.map +0 -1
- package/dist/lib/serde/thread.js +0 -172
- package/dist/lib/serde/tool.d.ts +0 -36
- package/dist/lib/serde/tool.d.ts.map +0 -1
- package/dist/lib/utils.d.ts +0 -19
- package/dist/lib/utils.d.ts.map +0 -1
- package/dist/lib/utils.js +0 -41
- package/dist/logger.d.ts +0 -36
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -43
- package/dist/mcp/__tests__/fixtures/echo-server.d.ts +0 -3
- package/dist/mcp/__tests__/fixtures/echo-server.d.ts.map +0 -1
- package/dist/mcp/__tests__/fixtures/echo-server.js +0 -92
- package/dist/mcp/__tests__/fixtures/math-server.d.ts +0 -3
- package/dist/mcp/__tests__/fixtures/math-server.d.ts.map +0 -1
- package/dist/mcp/__tests__/fixtures/math-server.js +0 -98
- package/dist/mcp/__tests__/fixtures/test-server.d.ts +0 -3
- package/dist/mcp/__tests__/fixtures/test-server.d.ts.map +0 -1
- package/dist/mcp/__tests__/fixtures/test-server.js +0 -163
- package/dist/mcp/__tests__/test-utils.d.ts +0 -17
- package/dist/mcp/__tests__/test-utils.d.ts.map +0 -1
- package/dist/mcp/__tests__/test-utils.js +0 -42
- package/dist/mcp/node.d.ts +0 -60
- package/dist/mcp/node.d.ts.map +0 -1
- package/dist/mcp/node.js +0 -297
- package/dist/model.d.ts +0 -175
- package/dist/model.d.ts.map +0 -1
- package/dist/providers/ai.d.ts +0 -1
- package/dist/providers/ai.d.ts.map +0 -1
- package/dist/providers/ai.js +0 -1
- package/dist/providers/default.d.ts +0 -16
- package/dist/providers/default.d.ts.map +0 -1
- package/dist/providers/default.js +0 -17
- package/dist/providers/registry.d.ts +0 -1
- package/dist/providers/registry.d.ts.map +0 -1
- package/dist/providers/registry.js +0 -1
- package/dist/sched/scheduler.d.ts +0 -20
- package/dist/sched/scheduler.d.ts.map +0 -1
- package/dist/sched/task.d.ts +0 -92
- package/dist/sched/task.d.ts.map +0 -1
- package/dist/sched/task.js +0 -102
- package/dist/serde/__tests__/codec.test.d.ts +0 -2
- package/dist/serde/__tests__/codec.test.d.ts.map +0 -1
- package/dist/serde/__tests__/codec.test.js +0 -75
- package/dist/serde/codec.d.ts +0 -12
- package/dist/serde/codec.d.ts.map +0 -1
- package/dist/serde/codec.js +0 -54
- package/dist/serde/json.d.ts +0 -8
- package/dist/serde/json.d.ts.map +0 -1
- package/dist/serde/json.js +0 -13
- package/dist/serde/thread.d.ts +0 -687
- package/dist/serde/thread.d.ts.map +0 -1
- package/dist/serde/thread.js +0 -158
- package/dist/serde/tool.d.ts +0 -36
- package/dist/serde/tool.d.ts.map +0 -1
- package/dist/session.d.ts +0 -1
- package/dist/session.d.ts.map +0 -1
- package/dist/session.js +0 -1
- package/dist/thread/__tests__/stream.test.d.ts +0 -2
- package/dist/thread/__tests__/stream.test.d.ts.map +0 -1
- package/dist/thread/__tests__/stream.test.js +0 -244
- package/dist/tool/mcp.d.ts +0 -75
- package/dist/tool/mcp.d.ts.map +0 -1
- package/dist/tool/mcp.js +0 -111
- package/dist/tools.d.ts +0 -362
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -220
- package/dist/types/proto.d.ts +0 -1551
- package/dist/types/proto.d.ts.map +0 -1
- package/dist/types/proto.js +0 -531
- package/dist/usage.d.ts +0 -43
- package/dist/usage.d.ts.map +0 -1
- package/dist/usage.js +0 -61
- package/src/lib/serde/thread.ts +0 -188
- /package/dist/{error.js → agent/index.js} +0 -0
- /package/dist/{lib/serde/tool.js → api/models/index.js} +0 -0
- /package/dist/{model.js → api/models/thread.js} +0 -0
- /package/dist/{sched/scheduler.js → api/resources/threads/types.js} +0 -0
- /package/dist/{serde/tool.js → types/kernl.js} +0 -0
package/src/agent.ts
CHANGED
|
@@ -1,27 +1,37 @@
|
|
|
1
1
|
import {
|
|
2
|
+
message,
|
|
2
3
|
LanguageModel,
|
|
4
|
+
LanguageModelItem,
|
|
3
5
|
LanguageModelRequestSettings,
|
|
4
|
-
message,
|
|
5
6
|
} from "@kernl-sdk/protocol";
|
|
6
7
|
|
|
8
|
+
import { Thread } from "./thread";
|
|
9
|
+
import type { Kernl } from "./kernl";
|
|
10
|
+
import type {
|
|
11
|
+
RThreadsListParams,
|
|
12
|
+
RThreadCreateParams,
|
|
13
|
+
RThreadGetOptions,
|
|
14
|
+
RThreadHistoryParams,
|
|
15
|
+
RThreadUpdateParams,
|
|
16
|
+
} from "@/api/resources/threads/types";
|
|
7
17
|
import type { Context, UnknownContext } from "./context";
|
|
8
|
-
import { InputGuardrail, OutputGuardrail } from "./guardrail";
|
|
9
|
-
import { AgentHooks } from "./lifecycle";
|
|
10
|
-
import { BaseToolkit } from "./tool/toolkit";
|
|
11
18
|
import { Tool } from "./tool";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
import { BaseToolkit } from "./tool/toolkit";
|
|
20
|
+
import {
|
|
21
|
+
InputGuardrail,
|
|
22
|
+
OutputGuardrail,
|
|
23
|
+
type ResolvedAgentResponse,
|
|
24
|
+
} from "./guardrail";
|
|
25
|
+
import { AgentHooks } from "./lifecycle";
|
|
15
26
|
|
|
27
|
+
import { MisconfiguredError, RuntimeError } from "./lib/error";
|
|
16
28
|
import type { AgentConfig, AgentResponseType } from "./types/agent";
|
|
17
29
|
import type {
|
|
18
30
|
TextResponse,
|
|
19
|
-
|
|
31
|
+
ThreadExecuteOptions,
|
|
20
32
|
ThreadExecuteResult,
|
|
21
33
|
ThreadStreamEvent,
|
|
22
34
|
} from "./types/thread";
|
|
23
|
-
import type { Kernl } from "./kernl";
|
|
24
|
-
import type { ResolvedAgentResponse } from "./guardrail";
|
|
25
35
|
|
|
26
36
|
export class Agent<
|
|
27
37
|
TContext = UnknownContext,
|
|
@@ -113,10 +123,13 @@ export class Agent<
|
|
|
113
123
|
|
|
114
124
|
/**
|
|
115
125
|
* Blocking execution - spawns or resumes thread and waits for completion
|
|
126
|
+
*
|
|
127
|
+
* @throws {RuntimeError} If the specified thread is already running (concurrent execution not allowed)
|
|
128
|
+
* @throws {MisconfiguredError} If the agent is not bound to a kernl instance
|
|
116
129
|
*/
|
|
117
130
|
async run(
|
|
118
|
-
|
|
119
|
-
options?:
|
|
131
|
+
input: string | LanguageModelItem[],
|
|
132
|
+
options?: ThreadExecuteOptions<TContext>,
|
|
120
133
|
): Promise<ThreadExecuteResult<ResolvedAgentResponse<TResponse>>> {
|
|
121
134
|
if (!this.kernl) {
|
|
122
135
|
throw new MisconfiguredError(
|
|
@@ -124,17 +137,46 @@ export class Agent<
|
|
|
124
137
|
);
|
|
125
138
|
}
|
|
126
139
|
|
|
127
|
-
const
|
|
140
|
+
const items =
|
|
141
|
+
typeof input === "string"
|
|
142
|
+
? [message({ role: "user", text: input })]
|
|
143
|
+
: input;
|
|
128
144
|
const tid = options?.threadId;
|
|
129
145
|
|
|
130
|
-
|
|
131
|
-
|
|
146
|
+
let thread: Thread<TContext, TResponse> | null = null;
|
|
147
|
+
|
|
148
|
+
if (tid) {
|
|
149
|
+
// no concurrent execution of same thread - correctness contract
|
|
150
|
+
// TODO: race condition - need to check again after async storage.get()
|
|
151
|
+
if (this.kernl.athreads.has(tid)) {
|
|
152
|
+
throw new RuntimeError(`Thread ${tid} is already running.`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// try to resume from storage if available
|
|
156
|
+
if (this.kernl.storage?.threads) {
|
|
157
|
+
thread = (await this.kernl.storage.threads.get(tid, {
|
|
158
|
+
history: true,
|
|
159
|
+
})) as Thread<TContext, TResponse> | null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// create new thread if not found in storage or no tid provided
|
|
132
164
|
if (!thread) {
|
|
133
|
-
thread = new Thread(
|
|
165
|
+
thread = new Thread({
|
|
166
|
+
agent: this,
|
|
167
|
+
input: items,
|
|
168
|
+
context: options?.context,
|
|
169
|
+
model: options?.model,
|
|
170
|
+
task: options?.task,
|
|
171
|
+
tid: options?.threadId,
|
|
172
|
+
namespace: options?.namespace,
|
|
173
|
+
storage: this.kernl.storage?.threads,
|
|
174
|
+
});
|
|
134
175
|
return this.kernl.spawn(thread);
|
|
135
176
|
}
|
|
136
177
|
|
|
137
|
-
thread
|
|
178
|
+
// resume existing thread from storage
|
|
179
|
+
thread.append(...items);
|
|
138
180
|
return this.kernl.schedule(thread);
|
|
139
181
|
}
|
|
140
182
|
|
|
@@ -142,10 +184,13 @@ export class Agent<
|
|
|
142
184
|
* Streaming execution - spawns or resumes thread and returns async iterator
|
|
143
185
|
*
|
|
144
186
|
* NOTE: streaming probably won't make sense in scheduling contexts so spawnStream etc. won't make sense
|
|
187
|
+
*
|
|
188
|
+
* @throws {RuntimeError} If the specified thread is already running (concurrent execution not allowed)
|
|
189
|
+
* @throws {MisconfiguredError} If the agent is not bound to a kernl instance
|
|
145
190
|
*/
|
|
146
191
|
async *stream(
|
|
147
|
-
|
|
148
|
-
options?:
|
|
192
|
+
input: string | LanguageModelItem[],
|
|
193
|
+
options?: ThreadExecuteOptions<TContext>,
|
|
149
194
|
): AsyncIterable<ThreadStreamEvent> {
|
|
150
195
|
if (!this.kernl) {
|
|
151
196
|
throw new MisconfiguredError(
|
|
@@ -153,22 +198,53 @@ export class Agent<
|
|
|
153
198
|
);
|
|
154
199
|
}
|
|
155
200
|
|
|
156
|
-
const
|
|
201
|
+
const items =
|
|
202
|
+
typeof input === "string"
|
|
203
|
+
? [message({ role: "user", text: input })]
|
|
204
|
+
: input;
|
|
157
205
|
const tid = options?.threadId;
|
|
158
206
|
|
|
159
|
-
|
|
160
|
-
|
|
207
|
+
let thread: Thread<TContext, TResponse> | null = null;
|
|
208
|
+
|
|
209
|
+
if (tid) {
|
|
210
|
+
// no concurrent execution of same thread - correctness contract
|
|
211
|
+
// TODO: race condition - need to check again after async storage.get()
|
|
212
|
+
if (this.kernl.athreads.has(tid)) {
|
|
213
|
+
throw new RuntimeError(`Thread ${tid} is already running.`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// try to resume from storage if available
|
|
217
|
+
if (this.kernl.storage?.threads) {
|
|
218
|
+
thread = (await this.kernl.storage.threads.get(tid, {
|
|
219
|
+
history: true,
|
|
220
|
+
})) as Thread<TContext, TResponse> | null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// create new thread if not found in storage or no tid provided
|
|
161
225
|
if (!thread) {
|
|
162
|
-
thread = new Thread(
|
|
226
|
+
thread = new Thread({
|
|
227
|
+
agent: this,
|
|
228
|
+
input: items,
|
|
229
|
+
context: options?.context,
|
|
230
|
+
model: options?.model,
|
|
231
|
+
task: options?.task,
|
|
232
|
+
tid: options?.threadId,
|
|
233
|
+
namespace: options?.namespace,
|
|
234
|
+
storage: this.kernl.storage?.threads,
|
|
235
|
+
});
|
|
163
236
|
yield* this.kernl.spawnStream(thread);
|
|
164
237
|
return;
|
|
165
238
|
}
|
|
166
239
|
|
|
167
|
-
thread
|
|
240
|
+
// resume existing thread from storage
|
|
241
|
+
thread.append(...items);
|
|
168
242
|
yield* this.kernl.scheduleStream(thread);
|
|
169
243
|
}
|
|
170
244
|
|
|
171
245
|
/**
|
|
246
|
+
* @internal
|
|
247
|
+
*
|
|
172
248
|
* Get a specific tool by ID from all toolkits.
|
|
173
249
|
*
|
|
174
250
|
* @param id The tool ID to look up
|
|
@@ -183,6 +259,8 @@ export class Agent<
|
|
|
183
259
|
}
|
|
184
260
|
|
|
185
261
|
/**
|
|
262
|
+
* @internal
|
|
263
|
+
*
|
|
186
264
|
* Get all tools available from all toolkits for the given context.
|
|
187
265
|
* Checks for duplicate tool IDs across toolkits and throws an error if found.
|
|
188
266
|
*
|
|
@@ -213,4 +291,41 @@ export class Agent<
|
|
|
213
291
|
|
|
214
292
|
return allTools;
|
|
215
293
|
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Thread management scoped to this agent.
|
|
297
|
+
*
|
|
298
|
+
* Convenience wrapper around kernl.threads that automatically filters to this agent's threads.
|
|
299
|
+
*/
|
|
300
|
+
get threads() {
|
|
301
|
+
if (!this.kernl) {
|
|
302
|
+
throw new MisconfiguredError(
|
|
303
|
+
`Agent ${this.id} not bound to kernl. Call kernl.register(agent) first.`,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const agentId = this.id;
|
|
308
|
+
const kthreads = this.kernl.threads;
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
get: (tid: string, options?: RThreadGetOptions) =>
|
|
312
|
+
kthreads.get(tid, options),
|
|
313
|
+
list: (params: Omit<RThreadsListParams, "agentId"> = {}) =>
|
|
314
|
+
kthreads.list({ ...params, agentId }),
|
|
315
|
+
delete: (tid: string) => kthreads.delete(tid),
|
|
316
|
+
history: (tid: string, params?: RThreadHistoryParams) =>
|
|
317
|
+
kthreads.history(tid, params),
|
|
318
|
+
create: (params: Omit<RThreadCreateParams, "agentId" | "model">) =>
|
|
319
|
+
kthreads.create({
|
|
320
|
+
...params,
|
|
321
|
+
agentId,
|
|
322
|
+
model: {
|
|
323
|
+
provider: this.model.provider,
|
|
324
|
+
modelId: this.model.modelId,
|
|
325
|
+
},
|
|
326
|
+
}),
|
|
327
|
+
update: (tid: string, patch: RThreadUpdateParams) =>
|
|
328
|
+
kthreads.update(tid, patch),
|
|
329
|
+
};
|
|
330
|
+
}
|
|
216
331
|
}
|