kernl 0.6.2 → 0.7.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/.turbo/turbo-check-types.log +1 -1
- package/CHANGELOG.md +44 -0
- package/dist/agent/__tests__/concurrency.test.js +1 -1
- package/dist/agent/__tests__/run.test.js +1 -1
- package/dist/agent/__tests__/systools.test.d.ts +2 -0
- package/dist/agent/__tests__/systools.test.d.ts.map +1 -0
- package/dist/agent/__tests__/systools.test.js +121 -0
- package/dist/{types/agent.d.ts → agent/types.d.ts} +19 -2
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent.d.ts +48 -6
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +100 -13
- package/dist/api/models/thread.d.ts +1 -1
- package/dist/api/resources/agents/agents.d.ts +38 -0
- package/dist/api/resources/agents/agents.d.ts.map +1 -0
- package/dist/api/resources/agents/agents.js +44 -0
- package/dist/api/resources/agents/index.d.ts +2 -0
- package/dist/api/resources/agents/index.d.ts.map +1 -0
- package/dist/api/resources/agents/index.js +1 -0
- package/dist/api/resources/threads/threads.d.ts +1 -1
- package/dist/api/resources/threads/threads.d.ts.map +1 -1
- package/dist/api/resources/threads/threads.js +1 -1
- package/dist/api/resources/threads/types.d.ts +2 -2
- package/dist/api/resources/threads/types.d.ts.map +1 -1
- package/dist/context.d.ts +10 -4
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +5 -0
- package/dist/guardrail.d.ts +2 -2
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/internal.d.ts +2 -2
- package/dist/internal.js +1 -1
- package/dist/kernl/index.d.ts +1 -1
- package/dist/kernl/index.d.ts.map +1 -1
- package/dist/kernl/index.js +0 -1
- package/dist/kernl/kernl.d.ts +11 -20
- package/dist/kernl/kernl.d.ts.map +1 -1
- package/dist/kernl/kernl.js +37 -34
- package/dist/kernl/types.d.ts +91 -0
- package/dist/kernl/types.d.ts.map +1 -0
- package/dist/lib/error.d.ts +2 -2
- package/dist/lifecycle.d.ts +2 -2
- package/dist/memory/codec.d.ts +32 -0
- package/dist/memory/codec.d.ts.map +1 -0
- package/dist/memory/codec.js +97 -0
- package/dist/memory/codecs/domain.d.ts +34 -0
- package/dist/memory/codecs/domain.d.ts.map +1 -0
- package/dist/memory/codecs/domain.js +99 -0
- package/dist/memory/codecs/identity.d.ts +12 -0
- package/dist/memory/codecs/identity.d.ts.map +1 -0
- package/dist/memory/codecs/identity.js +17 -0
- package/dist/memory/codecs/index.d.ts +31 -0
- package/dist/memory/codecs/index.d.ts.map +1 -0
- package/dist/memory/codecs/index.js +39 -0
- package/dist/memory/codecs/tpuf.d.ts +38 -0
- package/dist/memory/codecs/tpuf.d.ts.map +1 -0
- package/dist/memory/codecs/tpuf.js +90 -0
- package/dist/memory/encoder.d.ts +29 -0
- package/dist/memory/encoder.d.ts.map +1 -0
- package/dist/memory/encoder.js +45 -0
- package/dist/memory/handle.d.ts +89 -0
- package/dist/memory/handle.d.ts.map +1 -0
- package/dist/memory/handle.js +128 -0
- package/dist/memory/index.d.ts +12 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/indexes.d.ts +91 -0
- package/dist/memory/indexes.d.ts.map +1 -0
- package/dist/memory/indexes.js +7 -0
- package/dist/memory/memory.d.ts +55 -0
- package/dist/memory/memory.d.ts.map +1 -0
- package/dist/memory/memory.js +113 -0
- package/dist/memory/schema.d.ts +41 -0
- package/dist/memory/schema.d.ts.map +1 -0
- package/dist/memory/schema.js +112 -0
- package/dist/memory/store.d.ts +36 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +4 -0
- package/dist/memory/types.d.ts +250 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +4 -0
- package/dist/storage/base.d.ts +6 -1
- package/dist/storage/base.d.ts.map +1 -1
- package/dist/storage/in-memory.d.ts +24 -2
- package/dist/storage/in-memory.d.ts.map +1 -1
- package/dist/storage/in-memory.js +131 -0
- package/dist/storage/thread.d.ts +1 -1
- package/dist/thread/__tests__/integration.test.js +1 -1
- package/dist/thread/__tests__/mock.d.ts +1 -1
- package/dist/thread/__tests__/namespace.test.js +1 -1
- package/dist/thread/__tests__/thread.test.js +1 -1
- package/dist/thread/thread.d.ts +2 -2
- package/dist/thread/thread.d.ts.map +1 -1
- package/dist/thread/thread.js +3 -1
- package/dist/{types/thread.d.ts → thread/types.d.ts} +2 -2
- package/dist/thread/types.d.ts.map +1 -0
- package/dist/thread/utils.d.ts +2 -2
- package/dist/thread/utils.d.ts.map +1 -1
- package/dist/tool/index.d.ts +1 -0
- package/dist/tool/index.d.ts.map +1 -1
- package/dist/tool/index.js +2 -0
- package/dist/tool/sys/index.d.ts +7 -0
- package/dist/tool/sys/index.d.ts.map +1 -0
- package/dist/tool/sys/index.js +6 -0
- package/dist/tool/sys/memory.d.ts +14 -0
- package/dist/tool/sys/memory.d.ts.map +1 -0
- package/dist/tool/sys/memory.js +103 -0
- package/dist/tool/tool.d.ts +1 -1
- package/dist/tool/tool.d.ts.map +1 -1
- package/dist/tool/tool.js +2 -2
- package/package.json +4 -2
- package/src/agent/__tests__/systools.test.ts +146 -0
- package/src/{types/agent.ts → agent/types.ts} +22 -1
- package/src/agent.ts +144 -36
- package/src/api/__tests__/threads.test.ts +2 -2
- package/src/api/models/thread.ts +1 -1
- package/src/api/resources/agents/agents.ts +56 -0
- package/src/api/resources/agents/index.ts +1 -0
- package/src/api/resources/threads/events.ts +1 -1
- package/src/api/resources/threads/threads.ts +2 -2
- package/src/api/resources/threads/types.ts +2 -2
- package/src/context.ts +14 -136
- package/src/guardrail.ts +2 -2
- package/src/index.ts +35 -6
- package/src/internal.ts +2 -2
- package/src/kernl/index.ts +8 -0
- package/src/{kernl.ts → kernl/kernl.ts} +50 -10
- package/src/kernl/types.ts +106 -0
- package/src/lib/error.ts +2 -2
- package/src/lifecycle.ts +2 -2
- package/src/memory/codecs/domain.ts +115 -0
- package/src/memory/codecs/identity.ts +28 -0
- package/src/memory/codecs/index.ts +61 -0
- package/src/memory/codecs/tpuf.ts +115 -0
- package/src/memory/encoder.ts +56 -0
- package/src/memory/handle.ts +189 -0
- package/src/memory/index.ts +49 -0
- package/src/memory/indexes.ts +108 -0
- package/src/memory/memory.ts +151 -0
- package/src/memory/schema.ts +142 -0
- package/src/memory/store.ts +47 -0
- package/src/memory/types.ts +282 -0
- package/src/storage/__tests__/in-memory.test.ts +1 -1
- package/src/storage/base.ts +7 -1
- package/src/storage/in-memory.ts +170 -2
- package/src/storage/thread.ts +1 -1
- package/src/thread/__tests__/integration.test.ts +1 -1
- package/src/thread/__tests__/mock.ts +1 -1
- package/src/thread/__tests__/thread.test.ts +1 -1
- package/src/thread/thread.ts +5 -3
- package/src/{types/thread.ts → thread/types.ts} +1 -1
- package/src/thread/utils.ts +2 -2
- package/src/tool/index.ts +3 -0
- package/src/tool/sys/index.ts +7 -0
- package/src/tool/sys/memory.ts +120 -0
- package/src/tool/tool.ts +8 -4
- package/tsconfig.tsbuildinfo +1 -0
- package/dist/api/__tests__/cursor-page.test.d.ts +0 -2
- package/dist/api/__tests__/cursor-page.test.d.ts.map +0 -1
- package/dist/api/__tests__/cursor-page.test.js +0 -414
- package/dist/api/__tests__/offset-page.test.d.ts +0 -2
- package/dist/api/__tests__/offset-page.test.d.ts.map +0 -1
- package/dist/api/__tests__/offset-page.test.js +0 -510
- package/dist/api/pagination/base.d.ts +0 -48
- package/dist/api/pagination/base.d.ts.map +0 -1
- package/dist/api/pagination/base.js +0 -45
- package/dist/api/pagination/cursor.d.ts +0 -44
- package/dist/api/pagination/cursor.d.ts.map +0 -1
- package/dist/api/pagination/cursor.js +0 -52
- package/dist/api/pagination/offset.d.ts +0 -42
- package/dist/api/pagination/offset.d.ts.map +0 -1
- package/dist/api/pagination/offset.js +0 -55
- package/dist/kernl/threads.d.ts +0 -110
- package/dist/kernl/threads.d.ts.map +0 -1
- package/dist/kernl/threads.js +0 -126
- package/dist/kernl.d.ts +0 -51
- package/dist/kernl.d.ts.map +0 -1
- package/dist/kernl.js +0 -91
- package/dist/types/agent.d.ts.map +0 -1
- package/dist/types/kernl.d.ts +0 -42
- package/dist/types/kernl.d.ts.map +0 -1
- package/dist/types/thread.d.ts.map +0 -1
- package/src/api/__tests__/cursor-page.test.ts +0 -512
- package/src/api/__tests__/offset-page.test.ts +0 -624
- package/src/api/pagination/base.ts +0 -79
- package/src/api/pagination/cursor.ts +0 -86
- package/src/api/pagination/offset.ts +0 -89
- package/src/types/kernl.ts +0 -51
- /package/dist/{types/agent.js → agent/types.js} +0 -0
- /package/dist/{types/kernl.js → kernl/types.js} +0 -0
- /package/dist/{types/thread.js → thread/types.js} +0 -0
package/src/api/models/thread.ts
CHANGED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Agent } from "@/agent";
|
|
2
|
+
import type { AgentResponseType } from "@/agent/types";
|
|
3
|
+
import type { UnknownContext } from "@/context";
|
|
4
|
+
import type { TextResponse } from "@/thread/types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Agents resource.
|
|
8
|
+
*
|
|
9
|
+
* Thin facade over the in-process agent registry, returning live Agent instances.
|
|
10
|
+
*
|
|
11
|
+
* Note: agents are code, not persisted data; this is process-local.
|
|
12
|
+
*/
|
|
13
|
+
export class RAgents {
|
|
14
|
+
constructor(private readonly registry: Map<string, Agent>) {}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get a live Agent instance by id.
|
|
18
|
+
*
|
|
19
|
+
* Callers are expected to know the concrete TContext/TResponse types
|
|
20
|
+
* for their own agents and can specify them via generics.
|
|
21
|
+
*/
|
|
22
|
+
get<
|
|
23
|
+
TContext = UnknownContext,
|
|
24
|
+
TResponse extends AgentResponseType = TextResponse,
|
|
25
|
+
>(id: string): Agent<TContext, TResponse> | undefined {
|
|
26
|
+
const agent = this.registry.get(id);
|
|
27
|
+
return agent as Agent<TContext, TResponse> | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check if an agent with the given id is registered.
|
|
32
|
+
*/
|
|
33
|
+
has(id: string): boolean {
|
|
34
|
+
return this.registry.has(id);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* List all registered agents as live instances.
|
|
39
|
+
*
|
|
40
|
+
* Since this is a heterogeneous collection, we expose the widest safe
|
|
41
|
+
* type parameters here.
|
|
42
|
+
*/
|
|
43
|
+
list(): Agent<UnknownContext, AgentResponseType>[] {
|
|
44
|
+
return Array.from(this.registry.values()) as Agent<
|
|
45
|
+
UnknownContext,
|
|
46
|
+
AgentResponseType
|
|
47
|
+
>[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Unregister an agent at runtime.
|
|
52
|
+
*/
|
|
53
|
+
unregister(id: string): boolean {
|
|
54
|
+
return this.registry.delete(id);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RAgents } from "./agents";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ThreadStore } from "@/storage";
|
|
2
2
|
import { isPublicEvent } from "@/thread/utils";
|
|
3
3
|
import type { MThreadEvent } from "@/api/models";
|
|
4
|
-
import type { ThreadEvent } from "@/types
|
|
4
|
+
import type { ThreadEvent } from "@/thread/types";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Events subresource for threads.
|
|
@@ -3,14 +3,14 @@ import type {
|
|
|
3
3
|
ThreadListOptions,
|
|
4
4
|
ThreadHistoryOptions,
|
|
5
5
|
} from "@/storage";
|
|
6
|
-
import type { ThreadEvent } from "@/types
|
|
6
|
+
import type { ThreadEvent } from "@/thread/types";
|
|
7
7
|
import { isPublicEvent } from "@/thread/utils";
|
|
8
8
|
import { Context } from "@/context";
|
|
9
9
|
import { randomID } from "@kernl-sdk/shared/lib";
|
|
10
10
|
import { RUNNING } from "@kernl-sdk/protocol";
|
|
11
11
|
|
|
12
12
|
import type { MThread, MThreadEvent } from "@/api/models";
|
|
13
|
-
import { CursorPage, type CursorPageResponse } from "
|
|
13
|
+
import { CursorPage, type CursorPageResponse } from "@kernl-sdk/shared";
|
|
14
14
|
|
|
15
15
|
import { RThreadEvents } from "./events";
|
|
16
16
|
import type {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ThreadState } from "@/types
|
|
1
|
+
import type { ThreadState } from "@/thread/types";
|
|
2
2
|
import type { SortOrder } from "@/storage";
|
|
3
|
-
import type { CursorPageParams } from "
|
|
3
|
+
import type { CursorPageParams } from "@kernl-sdk/shared";
|
|
4
4
|
|
|
5
5
|
export interface RThreadHistoryParams {
|
|
6
6
|
/**
|
package/src/context.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { Agent } from "@/agent";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
4
|
+
* Context that is being passed around as part of the session is unknown
|
|
3
5
|
*/
|
|
4
|
-
export type
|
|
6
|
+
export type UnknownContext = unknown;
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* A propagation mechanism which carries execution-scoped values across API boundaries and between logically associated
|
|
@@ -12,12 +14,18 @@ export class Context<TContext = UnknownContext> {
|
|
|
12
14
|
* The namespace that this context belongs to.
|
|
13
15
|
*/
|
|
14
16
|
namespace: string;
|
|
15
|
-
|
|
17
|
+
|
|
16
18
|
/**
|
|
17
19
|
* The inner context object.
|
|
18
20
|
*/
|
|
19
21
|
context: TContext;
|
|
20
22
|
|
|
23
|
+
/**
|
|
24
|
+
* The agent executing this context.
|
|
25
|
+
* Set by the thread during execution.
|
|
26
|
+
*/
|
|
27
|
+
agent?: Agent<TContext, any>;
|
|
28
|
+
|
|
21
29
|
// ----------------------
|
|
22
30
|
// TEMPORARY: Tool approval tracking until actions system is refined
|
|
23
31
|
// ----------------------
|
|
@@ -64,10 +72,7 @@ export class Context<TContext = UnknownContext> {
|
|
|
64
72
|
// */
|
|
65
73
|
// #approvals: Map<string, ApprovalRecord>;
|
|
66
74
|
|
|
67
|
-
constructor(
|
|
68
|
-
namespace: string = "kernl",
|
|
69
|
-
context: TContext = {} as TContext,
|
|
70
|
-
) {
|
|
75
|
+
constructor(namespace: string = "kernl", context: TContext = {} as TContext) {
|
|
71
76
|
this.namespace = namespace;
|
|
72
77
|
this.context = context;
|
|
73
78
|
this.approvals = new Map();
|
|
@@ -139,136 +144,9 @@ export class Context<TContext = UnknownContext> {
|
|
|
139
144
|
// approvals: Object.fromEntries(this.#approvals.entries()),
|
|
140
145
|
};
|
|
141
146
|
}
|
|
142
|
-
|
|
143
|
-
// /**
|
|
144
|
-
// * Rebuild the approvals map from a serialized state.
|
|
145
|
-
// * @internal
|
|
146
|
-
// *
|
|
147
|
-
// * @param approvals - The approvals map to rebuild.
|
|
148
|
-
// */
|
|
149
|
-
// _rebuildApprovals(approvals: Record<string, ApprovalRecord>) {
|
|
150
|
-
// this.#approvals = new Map(Object.entries(approvals));
|
|
151
|
-
// }
|
|
152
|
-
|
|
153
|
-
// /**
|
|
154
|
-
// * Check if a tool call has been approved.
|
|
155
|
-
// *
|
|
156
|
-
// * @param approval - Details about the tool call being evaluated.
|
|
157
|
-
// * @returns `true` if the tool call has been approved, `false` if blocked and `undefined` if not yet approved or rejected.
|
|
158
|
-
// */
|
|
159
|
-
// isToolApproved(approval: { toolName: string; callId: string }) {
|
|
160
|
-
// const { toolName, callId } = approval;
|
|
161
|
-
// const approvalEntry = this.#approvals.get(toolName);
|
|
162
|
-
// if (approvalEntry?.approved === true && approvalEntry.rejected === true) {
|
|
163
|
-
// logger.warn(
|
|
164
|
-
// "Tool is permanently approved and rejected at the same time. Approval takes precedence",
|
|
165
|
-
// );
|
|
166
|
-
// return true;
|
|
167
|
-
// }
|
|
168
|
-
|
|
169
|
-
// if (approvalEntry?.approved === true) {
|
|
170
|
-
// return true;
|
|
171
|
-
// }
|
|
172
|
-
|
|
173
|
-
// if (approvalEntry?.rejected === true) {
|
|
174
|
-
// return false;
|
|
175
|
-
// }
|
|
176
|
-
|
|
177
|
-
// const individualCallApproval = Array.isArray(approvalEntry?.approved)
|
|
178
|
-
// ? approvalEntry.approved.includes(callId)
|
|
179
|
-
// : false;
|
|
180
|
-
// const individualCallRejection = Array.isArray(approvalEntry?.rejected)
|
|
181
|
-
// ? approvalEntry.rejected.includes(callId)
|
|
182
|
-
// : false;
|
|
183
|
-
|
|
184
|
-
// if (individualCallApproval && individualCallRejection) {
|
|
185
|
-
// logger.warn(
|
|
186
|
-
// `Tool call ${callId} is both approved and rejected at the same time. Approval takes precedence`,
|
|
187
|
-
// );
|
|
188
|
-
// return true;
|
|
189
|
-
// }
|
|
190
|
-
|
|
191
|
-
// if (individualCallApproval) {
|
|
192
|
-
// return true;
|
|
193
|
-
// }
|
|
194
|
-
|
|
195
|
-
// if (individualCallRejection) {
|
|
196
|
-
// return false;
|
|
197
|
-
// }
|
|
198
|
-
|
|
199
|
-
// return undefined;
|
|
200
|
-
// }
|
|
201
|
-
|
|
202
|
-
// /**
|
|
203
|
-
// * Approve a tool call.
|
|
204
|
-
// *
|
|
205
|
-
// * @param approvalItem - The tool approval item to approve.
|
|
206
|
-
// * @param options - Additional approval behavior options.
|
|
207
|
-
// */
|
|
208
|
-
// approveTool(
|
|
209
|
-
// approvalItem: RunToolApprovalItem,
|
|
210
|
-
// { alwaysApprove = false }: { alwaysApprove?: boolean } = {},
|
|
211
|
-
// ) {
|
|
212
|
-
// const toolName = approvalItem.rawItem.name;
|
|
213
|
-
// if (alwaysApprove) {
|
|
214
|
-
// this.#approvals.set(toolName, {
|
|
215
|
-
// approved: true,
|
|
216
|
-
// rejected: [],
|
|
217
|
-
// });
|
|
218
|
-
// return;
|
|
219
|
-
// }
|
|
220
|
-
|
|
221
|
-
// const approvalEntry = this.#approvals.get(toolName) ?? {
|
|
222
|
-
// approved: [],
|
|
223
|
-
// rejected: [],
|
|
224
|
-
// };
|
|
225
|
-
// if (Array.isArray(approvalEntry.approved)) {
|
|
226
|
-
// // function tool has call_id, hosted tool call has id
|
|
227
|
-
// const callId =
|
|
228
|
-
// "callId" in approvalItem.rawItem
|
|
229
|
-
// ? approvalItem.rawItem.callId // function tools
|
|
230
|
-
// : approvalItem.rawItem.id!; // hosted tools
|
|
231
|
-
// approvalEntry.approved.push(callId);
|
|
232
|
-
// }
|
|
233
|
-
// this.#approvals.set(toolName, approvalEntry);
|
|
234
|
-
// }
|
|
235
|
-
|
|
236
|
-
// /**
|
|
237
|
-
// * Reject a tool call.
|
|
238
|
-
// *
|
|
239
|
-
// * @param approvalItem - The tool approval item to reject.
|
|
240
|
-
// */
|
|
241
|
-
// rejectTool(
|
|
242
|
-
// approvalItem: RunToolApprovalItem,
|
|
243
|
-
// { alwaysReject = false }: { alwaysReject?: boolean } = {},
|
|
244
|
-
// ) {
|
|
245
|
-
// const toolName = approvalItem.rawItem.name;
|
|
246
|
-
// if (alwaysReject) {
|
|
247
|
-
// this.#approvals.set(toolName, {
|
|
248
|
-
// approved: false,
|
|
249
|
-
// rejected: true,
|
|
250
|
-
// });
|
|
251
|
-
// return;
|
|
252
|
-
// }
|
|
253
|
-
|
|
254
|
-
// const approvalEntry = this.#approvals.get(toolName) ?? {
|
|
255
|
-
// approved: [] as string[],
|
|
256
|
-
// rejected: [] as string[],
|
|
257
|
-
// };
|
|
258
|
-
|
|
259
|
-
// if (Array.isArray(approvalEntry.rejected)) {
|
|
260
|
-
// // function tool has call_id, hosted tool call has id
|
|
261
|
-
// const callId =
|
|
262
|
-
// "callId" in approvalItem.rawItem
|
|
263
|
-
// ? approvalItem.rawItem.callId // function tools
|
|
264
|
-
// : approvalItem.rawItem.id!; // hosted tools
|
|
265
|
-
// approvalEntry.rejected.push(callId);
|
|
266
|
-
// }
|
|
267
|
-
// this.#approvals.set(toolName, approvalEntry);
|
|
268
|
-
// }
|
|
269
147
|
}
|
|
270
148
|
|
|
271
149
|
/**
|
|
272
|
-
*
|
|
150
|
+
* Status of a tool call approval.
|
|
273
151
|
*/
|
|
274
|
-
export type
|
|
152
|
+
export type ApprovalStatus = "approved" | "rejected" | "pending";
|
package/src/guardrail.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { LanguageModelResponse } from "@kernl-sdk/protocol";
|
|
|
4
4
|
|
|
5
5
|
import { Agent } from "./agent";
|
|
6
6
|
import { Context, UnknownContext } from "./context";
|
|
7
|
-
import type { AgentResponseType } from "
|
|
8
|
-
import type { TextResponse, ThreadEvent } from "
|
|
7
|
+
import type { AgentResponseType } from "@/agent/types";
|
|
8
|
+
import type { TextResponse, ThreadEvent } from "@/thread/types";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Resolves the agent output type based on the response type.
|
package/src/index.ts
CHANGED
|
@@ -4,24 +4,25 @@ export type {
|
|
|
4
4
|
StorageOptions,
|
|
5
5
|
AgentRegistry,
|
|
6
6
|
ModelRegistry,
|
|
7
|
-
} from "./
|
|
7
|
+
} from "./kernl";
|
|
8
8
|
export { Agent } from "./agent";
|
|
9
9
|
export { Context } from "./context";
|
|
10
10
|
|
|
11
11
|
// --- tools --
|
|
12
|
+
|
|
12
13
|
export { tool, Toolkit, FunctionToolkit, MCPToolkit } from "./tool";
|
|
13
14
|
export { MCPServerSSE } from "./mcp/sse";
|
|
14
15
|
export { MCPServerStdio } from "./mcp/stdio";
|
|
15
16
|
export { MCPServerStreamableHttp } from "./mcp/http";
|
|
16
17
|
|
|
17
|
-
// ---
|
|
18
|
+
// --- threads ---
|
|
19
|
+
|
|
18
20
|
export type {
|
|
19
21
|
MThread as Thread,
|
|
20
22
|
MThreadEvent as ThreadEvent,
|
|
21
23
|
MThreadEventBase as ThreadEventBase,
|
|
22
24
|
} from "./api/models";
|
|
23
25
|
|
|
24
|
-
// --- thread resource types ---
|
|
25
26
|
export type {
|
|
26
27
|
RThreadsListParams as ThreadsListParams,
|
|
27
28
|
RThreadGetOptions as ThreadGetOptions,
|
|
@@ -30,14 +31,14 @@ export type {
|
|
|
30
31
|
RThreadUpdateParams as ThreadUpdateParams,
|
|
31
32
|
} from "./api/resources/threads";
|
|
32
33
|
|
|
33
|
-
// --- thread state enums ---
|
|
34
34
|
export {
|
|
35
35
|
THREAD_STATES,
|
|
36
36
|
type ThreadState,
|
|
37
37
|
type PublicThreadEvent,
|
|
38
|
-
} from "./types
|
|
38
|
+
} from "./thread/types";
|
|
39
|
+
|
|
40
|
+
// --- storage ---
|
|
39
41
|
|
|
40
|
-
// --- storage types ---
|
|
41
42
|
export type {
|
|
42
43
|
ThreadStore,
|
|
43
44
|
NewThread,
|
|
@@ -50,3 +51,31 @@ export type {
|
|
|
50
51
|
KernlStorage,
|
|
51
52
|
Transaction,
|
|
52
53
|
} from "./storage";
|
|
54
|
+
|
|
55
|
+
// --- memory ---
|
|
56
|
+
export { Memory, MemoryByteEncoder } from "./memory";
|
|
57
|
+
export type {
|
|
58
|
+
MemoryStore,
|
|
59
|
+
MemoryScope,
|
|
60
|
+
NewMemory,
|
|
61
|
+
MemoryConfig,
|
|
62
|
+
MemoryReindexParams,
|
|
63
|
+
MemoryRecord,
|
|
64
|
+
MemoryKind,
|
|
65
|
+
MemoryRecordUpdate,
|
|
66
|
+
MemoryFilter,
|
|
67
|
+
MemoryListOptions,
|
|
68
|
+
MemorySearchQuery,
|
|
69
|
+
WorkingMemorySnapshot,
|
|
70
|
+
ShortTermMemorySnapshot,
|
|
71
|
+
MemoryIndexBase,
|
|
72
|
+
MemorySearchIndex,
|
|
73
|
+
MemoryGraphIndex,
|
|
74
|
+
MemoryArchiveIndex,
|
|
75
|
+
GraphTraversalQuery,
|
|
76
|
+
GraphTraversalResult,
|
|
77
|
+
ArchiveQuery,
|
|
78
|
+
ArchiveResult,
|
|
79
|
+
MemoryByte,
|
|
80
|
+
MemoryByteCodec,
|
|
81
|
+
} from "./memory";
|
package/src/internal.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { LanguageModel } from "@kernl-sdk/protocol";
|
|
2
|
+
import { resolveEmbeddingModel } from "@kernl-sdk/retrieval";
|
|
2
3
|
|
|
3
4
|
import { Agent } from "@/agent";
|
|
4
5
|
import { UnknownContext } from "@/context";
|
|
@@ -7,10 +8,18 @@ import type { Thread } from "@/thread";
|
|
|
7
8
|
import type { ResolvedAgentResponse } from "@/guardrail";
|
|
8
9
|
import { InMemoryStorage, type KernlStorage } from "@/storage";
|
|
9
10
|
import { RThreads } from "@/api/resources/threads";
|
|
11
|
+
import { RAgents } from "@/api/resources/agents";
|
|
12
|
+
import {
|
|
13
|
+
Memory,
|
|
14
|
+
MemoryByteEncoder,
|
|
15
|
+
MemoryIndexHandle,
|
|
16
|
+
buildMemoryIndexSchema,
|
|
17
|
+
} from "@/memory";
|
|
10
18
|
|
|
11
|
-
import type {
|
|
12
|
-
import type {
|
|
13
|
-
|
|
19
|
+
import type { ThreadExecuteResult, ThreadStreamEvent } from "@/thread/types";
|
|
20
|
+
import type { AgentResponseType } from "@/agent/types";
|
|
21
|
+
|
|
22
|
+
import type { KernlOptions } from "./types";
|
|
14
23
|
|
|
15
24
|
/**
|
|
16
25
|
* The kernl - manages agent processes, scheduling, and task lifecycle.
|
|
@@ -19,35 +28,66 @@ import type { AgentResponseType } from "@/types/agent";
|
|
|
19
28
|
* tracing.
|
|
20
29
|
*/
|
|
21
30
|
export class Kernl extends KernlHooks<UnknownContext, AgentResponseType> {
|
|
22
|
-
private
|
|
23
|
-
private
|
|
31
|
+
private readonly _agents: Map<string, Agent> = new Map();
|
|
32
|
+
private readonly _models: Map<string, LanguageModel> = new Map();
|
|
24
33
|
|
|
25
34
|
readonly storage: KernlStorage;
|
|
26
35
|
athreads: Map<string, Thread<any, any>> = new Map(); /* active threads */
|
|
27
36
|
|
|
28
37
|
// --- public API ---
|
|
29
|
-
readonly threads: RThreads;
|
|
38
|
+
readonly threads: RThreads;
|
|
39
|
+
readonly agents: RAgents;
|
|
40
|
+
readonly memories: Memory;
|
|
30
41
|
|
|
31
42
|
constructor(options: KernlOptions = {}) {
|
|
32
43
|
super();
|
|
33
44
|
this.storage = options.storage?.db ?? new InMemoryStorage();
|
|
34
|
-
this.storage.bind({ agents: this.
|
|
45
|
+
this.storage.bind({ agents: this._agents, models: this._models });
|
|
35
46
|
this.threads = new RThreads(this.storage.threads);
|
|
47
|
+
this.agents = new RAgents(this._agents);
|
|
48
|
+
|
|
49
|
+
// initialize memory
|
|
50
|
+
const embeddingModel =
|
|
51
|
+
options.memory?.embeddingModel ?? "openai/text-embedding-3-small";
|
|
52
|
+
const embedder =
|
|
53
|
+
typeof embeddingModel === "string"
|
|
54
|
+
? resolveEmbeddingModel<string>(embeddingModel)
|
|
55
|
+
: embeddingModel;
|
|
56
|
+
const encoder = new MemoryByteEncoder(embedder);
|
|
57
|
+
|
|
58
|
+
const vector = options.storage?.vector;
|
|
59
|
+
const indexId = options.memory?.indexId ?? "memories_sindex";
|
|
60
|
+
const dimensions = options.memory?.dimensions ?? 1536;
|
|
61
|
+
const providerOptions = options.memory?.indexProviderOptions ?? { schema: "kernl" };
|
|
62
|
+
|
|
63
|
+
this.memories = new Memory({
|
|
64
|
+
store: this.storage.memories,
|
|
65
|
+
search:
|
|
66
|
+
vector !== undefined
|
|
67
|
+
? new MemoryIndexHandle({
|
|
68
|
+
index: vector,
|
|
69
|
+
indexId,
|
|
70
|
+
schema: buildMemoryIndexSchema({ dimensions }),
|
|
71
|
+
providerOptions,
|
|
72
|
+
})
|
|
73
|
+
: undefined,
|
|
74
|
+
encoder,
|
|
75
|
+
});
|
|
36
76
|
}
|
|
37
77
|
|
|
38
78
|
/**
|
|
39
79
|
* Registers a new agent with the kernl instance.
|
|
40
80
|
*/
|
|
41
81
|
register(agent: Agent): void {
|
|
42
|
-
this.
|
|
82
|
+
this._agents.set(agent.id, agent);
|
|
43
83
|
agent.bind(this);
|
|
44
84
|
|
|
45
85
|
// (TODO): implement exhaustive model registry in protocol/ package
|
|
46
86
|
//
|
|
47
87
|
// auto-populate model registry for storage hydration
|
|
48
88
|
const key = `${agent.model.provider}/${agent.model.modelId}`;
|
|
49
|
-
if (!this.
|
|
50
|
-
this.
|
|
89
|
+
if (!this._models.has(key)) {
|
|
90
|
+
this._models.set(key, agent.model);
|
|
51
91
|
}
|
|
52
92
|
}
|
|
53
93
|
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { LanguageModel, EmbeddingModel } from "@kernl-sdk/protocol";
|
|
2
|
+
import { SearchIndex } from "@kernl-sdk/retrieval";
|
|
3
|
+
|
|
4
|
+
import { Agent } from "@/agent";
|
|
5
|
+
import { KernlStorage } from "@/storage";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Storage configuration for Kernl.
|
|
9
|
+
*/
|
|
10
|
+
export interface StorageOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Relational database storage (threads, tasks, traces).
|
|
13
|
+
*/
|
|
14
|
+
db?: KernlStorage;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Vector search index for semantic memory search.
|
|
18
|
+
* Supports pgvector, Turbopuffer, etc.
|
|
19
|
+
*/
|
|
20
|
+
vector?: SearchIndex;
|
|
21
|
+
|
|
22
|
+
// Future storage layers (deferred):
|
|
23
|
+
// blob?: BlobStore;
|
|
24
|
+
// lake?: DataLake;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Memory system configuration.
|
|
29
|
+
*/
|
|
30
|
+
export interface MemoryOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Embedding model for memory encoding.
|
|
33
|
+
*
|
|
34
|
+
* Can be:
|
|
35
|
+
* - A string like "openai/text-embedding-3-small" (resolved via provider registry)
|
|
36
|
+
* - An EmbeddingModel instance
|
|
37
|
+
*
|
|
38
|
+
* @default "openai/text-embedding-3-small"
|
|
39
|
+
*/
|
|
40
|
+
embeddingModel?: string | EmbeddingModel<string>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Logical index ID used by the search backend.
|
|
44
|
+
* - For pgvector: becomes the table name (with schema from indexProviderOptions)
|
|
45
|
+
* - For Turbopuffer: becomes the namespace
|
|
46
|
+
* @default "kernl_memories_index"
|
|
47
|
+
*/
|
|
48
|
+
indexId?: string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Backend-specific options passed to SearchIndex.createIndex().
|
|
52
|
+
* - For pgvector: { schema?: string } controls schema (default: "kernl")
|
|
53
|
+
* - For Turbopuffer: not used
|
|
54
|
+
*/
|
|
55
|
+
indexProviderOptions?: Record<string, unknown>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Vector dimensions for embeddings.
|
|
59
|
+
* Only needed if embedding model doesn't provide this automatically.
|
|
60
|
+
* @default 1536
|
|
61
|
+
*/
|
|
62
|
+
dimensions?: number;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Similarity metric for vector search.
|
|
66
|
+
* @default "cosine"
|
|
67
|
+
*/
|
|
68
|
+
similarity?: "cosine" | "euclidean" | "dot_product";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Configuration options for creating a Kernl instance.
|
|
73
|
+
*/
|
|
74
|
+
export interface KernlOptions {
|
|
75
|
+
/**
|
|
76
|
+
* Storage configuration for persisting threads, tasks, and traces.
|
|
77
|
+
*/
|
|
78
|
+
storage?: StorageOptions;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Memory system configuration.
|
|
82
|
+
*/
|
|
83
|
+
memory?: MemoryOptions;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Agent registry interface.
|
|
88
|
+
*
|
|
89
|
+
* Satisfied by Map<string, Agent>.
|
|
90
|
+
*/
|
|
91
|
+
export interface AgentRegistry {
|
|
92
|
+
get(id: string): Agent<any> | undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Model registry interface.
|
|
97
|
+
*
|
|
98
|
+
* Satisfied by Map<string, LanguageModel>.
|
|
99
|
+
* Key format: "provider/modelId"
|
|
100
|
+
*
|
|
101
|
+
* TODO: Create an exhaustive model registry in the protocol package
|
|
102
|
+
* with all supported models and their metadata.
|
|
103
|
+
*/
|
|
104
|
+
export interface ModelRegistry {
|
|
105
|
+
get(key: string): LanguageModel | undefined;
|
|
106
|
+
}
|
package/src/lib/error.ts
CHANGED
|
@@ -9,8 +9,8 @@ import { randomID } from "@kernl-sdk/shared/lib";
|
|
|
9
9
|
// import { SerializedThread } from "@/lib/serde/thread";
|
|
10
10
|
type SerializedThread = any;
|
|
11
11
|
|
|
12
|
-
import { AgentResponseType } from "@/types
|
|
13
|
-
import { TextResponse } from "@/types
|
|
12
|
+
import { AgentResponseType } from "@/agent/types";
|
|
13
|
+
import { TextResponse } from "@/thread/types";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Abstract base class for all `kernl` errors
|
package/src/lifecycle.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { Context, UnknownContext } from "./context";
|
|
|
5
5
|
import { Tool } from "./tool";
|
|
6
6
|
import type { ToolCall } from "@kernl-sdk/protocol";
|
|
7
7
|
|
|
8
|
-
import { AgentResponseType } from "
|
|
9
|
-
import { TextResponse } from "
|
|
8
|
+
import { AgentResponseType } from "@/agent/types";
|
|
9
|
+
import { TextResponse } from "@/thread/types";
|
|
10
10
|
|
|
11
11
|
export type EventEmitterEvents = Record<string, any[]>;
|
|
12
12
|
|