kernl 0.6.3 → 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/CHANGELOG.md +14 -0
- 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/agent/types.d.ts +17 -0
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent.d.ts +14 -4
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +42 -13
- 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/context.d.ts +6 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +5 -0
- package/dist/kernl/kernl.d.ts +4 -2
- package/dist/kernl/kernl.d.ts.map +1 -1
- package/dist/kernl/kernl.js +11 -8
- package/dist/memory/memory.d.ts +5 -1
- package/dist/memory/memory.d.ts.map +1 -1
- package/dist/memory/memory.js +6 -0
- package/dist/thread/thread.d.ts.map +1 -1
- package/dist/thread/thread.js +3 -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 +1 -1
- package/src/agent/__tests__/systools.test.ts +146 -0
- package/src/agent/types.ts +21 -0
- package/src/agent.ts +70 -38
- package/src/api/resources/agents/agents.ts +56 -0
- package/src/api/resources/agents/index.ts +1 -0
- package/src/context.ts +8 -0
- package/src/kernl/kernl.ts +11 -8
- package/src/memory/memory.ts +8 -0
- package/src/thread/thread.ts +3 -1
- 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/dist/memory/memory.js
CHANGED
|
@@ -71,6 +71,12 @@ export class Memory {
|
|
|
71
71
|
topK: q.limit ?? 20,
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* List memories matching the filter.
|
|
76
|
+
*/
|
|
77
|
+
async list(options) {
|
|
78
|
+
return this.store.list(options);
|
|
79
|
+
}
|
|
74
80
|
/**
|
|
75
81
|
* Repair indexing for a memory without modifying the DB row.
|
|
76
82
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../../src/thread/thread.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAKzD,OAAO,EAML,aAAa,EAGd,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EAEV,WAAW,EACX,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAUvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,MAAM,CACjB,QAAQ,GAAG,OAAO,EAClB,SAAS,SAAS,iBAAiB,G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AI,aAAa,CAAC,iBAAiB,CAAC;IAuBjD;;;;;OAKG;YACY,QAAQ;IAgEvB;;;;;OAKG;YACY,IAAI;IA8BnB;;;;;OAKG;YACW,UAAU;IAuCxB;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,KAAK,EAAE,gBAAgB,EAAE,GAAG,WAAW,EAAE;IAiBnD;;OAEG;IACH,MAAM;IAQN;;OAEG;YACW,cAAc;IAyC5B;;;;OAIG;YACW,YAAY;IA4C1B;;OAEG;YACW,mBAAmB;CA2ClC"}
|
package/dist/thread/thread.js
CHANGED
|
@@ -70,6 +70,7 @@ export class Thread {
|
|
|
70
70
|
this.agent = options.agent;
|
|
71
71
|
this.context =
|
|
72
72
|
options.context ?? new Context(this.namespace, {});
|
|
73
|
+
this.context.agent = options.agent;
|
|
73
74
|
this.parent = options.task ?? null;
|
|
74
75
|
this.model = options.model ?? options.agent.model;
|
|
75
76
|
this.storage = options.storage;
|
|
@@ -262,7 +263,7 @@ export class Thread {
|
|
|
262
263
|
state: this.state,
|
|
263
264
|
tick: this._tick,
|
|
264
265
|
context: this.context,
|
|
265
|
-
metadata
|
|
266
|
+
// no metadata - not owned by checkpoint
|
|
266
267
|
});
|
|
267
268
|
}
|
|
268
269
|
/**
|
|
@@ -351,6 +352,7 @@ export class Thread {
|
|
|
351
352
|
// (TMP) - passing the approval status through the context until actions system
|
|
352
353
|
// is refined
|
|
353
354
|
const ctx = new Context(this.namespace, this.context.context);
|
|
355
|
+
ctx.agent = this.agent;
|
|
354
356
|
ctx.approve(call.callId); // mark this call as approved
|
|
355
357
|
const res = await tool.invoke(ctx, call.arguments, call.callId);
|
|
356
358
|
return {
|
package/dist/tool/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { BaseTool, FunctionTool, HostedTool, tool } from "./tool.js";
|
|
2
2
|
export { BaseToolkit, Toolkit, FunctionToolkit, MCPToolkit } from "./toolkit.js";
|
|
3
3
|
export type { Tool, ToolResult, FunctionToolkitConfig, MCPToolkitConfig, ToolkitFilter, ToolkitFilterContext, } from "./types.js";
|
|
4
|
+
export { memory } from "./sys/index.js";
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/tool/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tool/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC9E,YAAY,EACV,IAAI,EACJ,UAAU,EACV,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,EACb,oBAAoB,GACrB,MAAM,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tool/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC9E,YAAY,EACV,IAAI,EACJ,UAAU,EACV,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,EACb,oBAAoB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC"}
|
package/dist/tool/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tool/sys/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory system toolkit.
|
|
3
|
+
*
|
|
4
|
+
* Provides tools for agents to store and retrieve memories.
|
|
5
|
+
* Enabled via `memory: true` in agent config.
|
|
6
|
+
*/
|
|
7
|
+
import { Toolkit } from "../toolkit.js";
|
|
8
|
+
/**
|
|
9
|
+
* Memory system toolkit.
|
|
10
|
+
*
|
|
11
|
+
* Provides memories.search, memories.create, and memories.list tools.
|
|
12
|
+
*/
|
|
13
|
+
export declare const memory: Toolkit<unknown>;
|
|
14
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../src/tool/sys/memory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAmGrC;;;;GAIG;AACH,eAAO,MAAM,MAAM,kBAIjB,CAAC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory system toolkit.
|
|
3
|
+
*
|
|
4
|
+
* Provides tools for agents to store and retrieve memories.
|
|
5
|
+
* Enabled via `memory: true` in agent config.
|
|
6
|
+
*/
|
|
7
|
+
import assert from "assert";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { tool } from "../tool.js";
|
|
10
|
+
import { Toolkit } from "../toolkit.js";
|
|
11
|
+
// --- Tools ---
|
|
12
|
+
/**
|
|
13
|
+
* Search memories for relevant information using natural language.
|
|
14
|
+
*/
|
|
15
|
+
const search = tool({
|
|
16
|
+
id: "memories.search",
|
|
17
|
+
description: "Search your memories. " +
|
|
18
|
+
"Use this to recall facts, preferences, or context you've previously stored.",
|
|
19
|
+
parameters: z.object({
|
|
20
|
+
query: z.string().describe("Natural language search query"),
|
|
21
|
+
limit: z
|
|
22
|
+
.number()
|
|
23
|
+
.int()
|
|
24
|
+
.positive()
|
|
25
|
+
.optional()
|
|
26
|
+
.describe("Max results (default: 10)"),
|
|
27
|
+
}),
|
|
28
|
+
execute: async (ctx, { query, limit }) => {
|
|
29
|
+
assert(ctx.agent, "ctx.agent required for memory tools");
|
|
30
|
+
const mems = await ctx.agent.memories.search({
|
|
31
|
+
query,
|
|
32
|
+
limit: limit ?? 10,
|
|
33
|
+
});
|
|
34
|
+
return mems.map((h) => ({
|
|
35
|
+
id: h.document?.id,
|
|
36
|
+
text: h.document?.text,
|
|
37
|
+
score: h.score,
|
|
38
|
+
}));
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Store a new memory to persist across conversations.
|
|
43
|
+
*/
|
|
44
|
+
const create = tool({
|
|
45
|
+
id: "memories.create",
|
|
46
|
+
description: "Store a new memory. Use this to remember important facts, user preferences, " +
|
|
47
|
+
"or context that should persist across conversations.",
|
|
48
|
+
parameters: z.object({
|
|
49
|
+
content: z.string().describe("Text content to remember"),
|
|
50
|
+
collection: z
|
|
51
|
+
.string()
|
|
52
|
+
.optional()
|
|
53
|
+
.describe("Category for organizing memories (default: 'facts')"),
|
|
54
|
+
}),
|
|
55
|
+
execute: async (ctx, { content, collection }) => {
|
|
56
|
+
assert(ctx.agent, "ctx.agent required for memory tools");
|
|
57
|
+
const mem = await ctx.agent.memories.create({
|
|
58
|
+
collection: collection ?? "facts",
|
|
59
|
+
content: { text: content },
|
|
60
|
+
});
|
|
61
|
+
return { id: mem.id, stored: true };
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* List stored memories, optionally filtered by collection.
|
|
66
|
+
*/
|
|
67
|
+
const list = tool({
|
|
68
|
+
id: "memories.list",
|
|
69
|
+
description: "List your stored memories. Use this to see what you've remembered, " +
|
|
70
|
+
"optionally filtered by collection.",
|
|
71
|
+
parameters: z.object({
|
|
72
|
+
collection: z.string().optional().describe("Filter by collection name"),
|
|
73
|
+
limit: z
|
|
74
|
+
.number()
|
|
75
|
+
.int()
|
|
76
|
+
.positive()
|
|
77
|
+
.optional()
|
|
78
|
+
.describe("Max results (default: 20)"),
|
|
79
|
+
}),
|
|
80
|
+
execute: async (ctx, { collection, limit }) => {
|
|
81
|
+
assert(ctx.agent, "ctx.agent required for memory tools");
|
|
82
|
+
const mems = await ctx.agent.memories.list({
|
|
83
|
+
collection,
|
|
84
|
+
limit: limit ?? 20,
|
|
85
|
+
});
|
|
86
|
+
return mems.map((r) => ({
|
|
87
|
+
id: r.id,
|
|
88
|
+
collection: r.collection,
|
|
89
|
+
text: r.content.text,
|
|
90
|
+
}));
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
// --- Toolkit ---
|
|
94
|
+
/**
|
|
95
|
+
* Memory system toolkit.
|
|
96
|
+
*
|
|
97
|
+
* Provides memories.search, memories.create, and memories.list tools.
|
|
98
|
+
*/
|
|
99
|
+
export const memory = new Toolkit({
|
|
100
|
+
id: "sys.memory",
|
|
101
|
+
description: "Tools for storing and retrieving agent memories",
|
|
102
|
+
tools: [list, create, search],
|
|
103
|
+
});
|
package/dist/tool/tool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, UnknownContext } from "../context.js";
|
|
2
|
-
import type
|
|
2
|
+
import { type LanguageModelTool } from "@kernl-sdk/protocol";
|
|
3
3
|
import type { ToolConfig, ToolApprovalFunction, ToolEnabledFunction, ToolErrorFunction, ToolInputParameters, ToolResult } from "./types.js";
|
|
4
4
|
/**
|
|
5
5
|
* Exposes a function to the agent as a tool to be called
|
package/dist/tool/tool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/tool/tool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/tool/tool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAKpD,OAAO,EAIL,KAAK,iBAAiB,EACvB,MAAM,qBAAqB,CA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;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAInC;;OAEG;IACH,SAAS,IAAI,iBAAiB;CAQ/B"}
|
package/dist/tool/tool.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { ModelBehaviorError } from "../lib/error.js";
|
|
3
3
|
import { logger } from "../lib/logger.js";
|
|
4
|
+
import { FAILED, COMPLETED, INTERRUPTIBLE, } from "@kernl-sdk/protocol";
|
|
4
5
|
import { json } from "@kernl-sdk/shared/lib";
|
|
5
|
-
import { FAILED, COMPLETED, INTERRUPTIBLE } from "@kernl-sdk/protocol";
|
|
6
6
|
/**
|
|
7
7
|
* Exposes a function to the agent as a tool to be called
|
|
8
8
|
*
|
|
@@ -122,7 +122,7 @@ export class FunctionTool extends BaseTool {
|
|
|
122
122
|
description: this.description,
|
|
123
123
|
parameters: z.toJSONSchema(this.parameters ?? z.object({}), {
|
|
124
124
|
target: "draft-7",
|
|
125
|
-
}), //
|
|
125
|
+
}), // use empty object if no parameters (matches AI SDK)
|
|
126
126
|
};
|
|
127
127
|
}
|
|
128
128
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Agent } from "@/agent";
|
|
3
|
+
import { Kernl } from "@/kernl";
|
|
4
|
+
import { Context } from "@/context";
|
|
5
|
+
import { createMockModel } from "@/thread/__tests__/fixtures/mock-model";
|
|
6
|
+
import { message } from "@kernl-sdk/protocol";
|
|
7
|
+
|
|
8
|
+
describe("Agent systools", () => {
|
|
9
|
+
const model = createMockModel(async () => ({
|
|
10
|
+
content: [message({ role: "assistant", text: "Done" })],
|
|
11
|
+
finishReason: "stop",
|
|
12
|
+
usage: { inputTokens: 2, outputTokens: 2, totalTokens: 4 },
|
|
13
|
+
warnings: [],
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
describe("memory toolkit", () => {
|
|
17
|
+
it("adds memory toolkit when memory.enabled is true", () => {
|
|
18
|
+
const agent = new Agent({
|
|
19
|
+
id: "test-agent",
|
|
20
|
+
name: "Test",
|
|
21
|
+
instructions: "Test",
|
|
22
|
+
model,
|
|
23
|
+
memory: { enabled: true },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const kernl = new Kernl();
|
|
27
|
+
kernl.register(agent);
|
|
28
|
+
|
|
29
|
+
expect(agent.systools.length).toBe(1);
|
|
30
|
+
expect(agent.systools[0].id).toBe("sys.memory");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("has no systools when memory not configured", () => {
|
|
34
|
+
const agent = new Agent({
|
|
35
|
+
id: "test-agent",
|
|
36
|
+
name: "Test",
|
|
37
|
+
instructions: "Test",
|
|
38
|
+
model,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const kernl = new Kernl();
|
|
42
|
+
kernl.register(agent);
|
|
43
|
+
|
|
44
|
+
expect(agent.systools.length).toBe(0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("has no systools when memory.enabled is false", () => {
|
|
48
|
+
const agent = new Agent({
|
|
49
|
+
id: "test-agent",
|
|
50
|
+
name: "Test",
|
|
51
|
+
instructions: "Test",
|
|
52
|
+
model,
|
|
53
|
+
memory: { enabled: false },
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const kernl = new Kernl();
|
|
57
|
+
kernl.register(agent);
|
|
58
|
+
|
|
59
|
+
expect(agent.systools.length).toBe(0);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("can retrieve memory tools via agent.tool()", () => {
|
|
63
|
+
const agent = new Agent({
|
|
64
|
+
id: "test-agent",
|
|
65
|
+
name: "Test",
|
|
66
|
+
instructions: "Test",
|
|
67
|
+
model,
|
|
68
|
+
memory: { enabled: true },
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const kernl = new Kernl();
|
|
72
|
+
kernl.register(agent);
|
|
73
|
+
|
|
74
|
+
expect(agent.tool("memories.search")).toBeDefined();
|
|
75
|
+
expect(agent.tool("memories.create")).toBeDefined();
|
|
76
|
+
expect(agent.tool("memories.list")).toBeDefined();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("includes memory tools in agent.tools() output", async () => {
|
|
80
|
+
const agent = new Agent({
|
|
81
|
+
id: "test-agent",
|
|
82
|
+
name: "Test",
|
|
83
|
+
instructions: "Test",
|
|
84
|
+
model,
|
|
85
|
+
memory: { enabled: true },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const kernl = new Kernl();
|
|
89
|
+
kernl.register(agent);
|
|
90
|
+
|
|
91
|
+
const ctx = new Context("test");
|
|
92
|
+
const tools = await agent.tools(ctx);
|
|
93
|
+
const ids = tools.map((t) => t.id);
|
|
94
|
+
|
|
95
|
+
expect(ids).toContain("memories.search");
|
|
96
|
+
expect(ids).toContain("memories.create");
|
|
97
|
+
expect(ids).toContain("memories.list");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("systools appear before user toolkits in tools() output", async () => {
|
|
101
|
+
const agent = new Agent({
|
|
102
|
+
id: "test-agent",
|
|
103
|
+
name: "Test",
|
|
104
|
+
instructions: "Test",
|
|
105
|
+
model,
|
|
106
|
+
memory: { enabled: true },
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const kernl = new Kernl();
|
|
110
|
+
kernl.register(agent);
|
|
111
|
+
|
|
112
|
+
const ctx = new Context("test");
|
|
113
|
+
const tools = await agent.tools(ctx);
|
|
114
|
+
|
|
115
|
+
// Memory tools should be first (from systools)
|
|
116
|
+
expect(tools[0].id).toBe("memories.search");
|
|
117
|
+
expect(tools[1].id).toBe("memories.create");
|
|
118
|
+
expect(tools[2].id).toBe("memories.list");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("memory config defaults", () => {
|
|
123
|
+
it("defaults memory to { enabled: false }", () => {
|
|
124
|
+
const agent = new Agent({
|
|
125
|
+
id: "test-agent",
|
|
126
|
+
name: "Test",
|
|
127
|
+
instructions: "Test",
|
|
128
|
+
model,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(agent.memory).toEqual({ enabled: false });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("preserves memory config when provided", () => {
|
|
135
|
+
const agent = new Agent({
|
|
136
|
+
id: "test-agent",
|
|
137
|
+
name: "Test",
|
|
138
|
+
instructions: "Test",
|
|
139
|
+
model,
|
|
140
|
+
memory: { enabled: true },
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
expect(agent.memory).toEqual({ enabled: true });
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
package/src/agent/types.ts
CHANGED
|
@@ -23,6 +23,9 @@ export interface AgentConfig<
|
|
|
23
23
|
/* The name of the agent (defaults to ID if not provided) */
|
|
24
24
|
name: string;
|
|
25
25
|
|
|
26
|
+
/* A brief description of the agent's purpose */
|
|
27
|
+
description?: string;
|
|
28
|
+
|
|
26
29
|
/**
|
|
27
30
|
* The instructions for the agent. Will be used as the "system prompt" when this agent is
|
|
28
31
|
* invoked. Describes what the agent should do, and how it responds.
|
|
@@ -85,6 +88,14 @@ export interface AgentConfig<
|
|
|
85
88
|
*/
|
|
86
89
|
toolkits?: BaseToolkit<TContext>[];
|
|
87
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Memory configuration for this agent.
|
|
93
|
+
* Enables memory system tools (memories.search, memories.create, memories.list).
|
|
94
|
+
*
|
|
95
|
+
* Requires kernl to be configured with memory storage.
|
|
96
|
+
*/
|
|
97
|
+
memory?: AgentMemoryConfig;
|
|
98
|
+
|
|
88
99
|
/**
|
|
89
100
|
* A list of checks that run in parallel to the agent's execution on the input + output for the agent,
|
|
90
101
|
* depending on the configuration.
|
|
@@ -146,3 +157,13 @@ export interface AgentGuardrails<
|
|
|
146
157
|
* 'text' is a special type that indicates the output will be a string.
|
|
147
158
|
*/
|
|
148
159
|
export type AgentResponseType = TextResponse | ZodType;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Memory configuration for an agent.
|
|
163
|
+
*/
|
|
164
|
+
export interface AgentMemoryConfig {
|
|
165
|
+
/**
|
|
166
|
+
* Enable memory system tools for this agent.
|
|
167
|
+
*/
|
|
168
|
+
enabled: boolean;
|
|
169
|
+
}
|
package/src/agent.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
RThreadUpdateParams,
|
|
16
16
|
} from "@/api/resources/threads/types";
|
|
17
17
|
import type { Context, UnknownContext } from "./context";
|
|
18
|
-
import { Tool } from "./tool";
|
|
18
|
+
import { Tool, memory } from "./tool";
|
|
19
19
|
import { BaseToolkit } from "./tool/toolkit";
|
|
20
20
|
import {
|
|
21
21
|
InputGuardrail,
|
|
@@ -23,17 +23,27 @@ import {
|
|
|
23
23
|
type ResolvedAgentResponse,
|
|
24
24
|
} from "./guardrail";
|
|
25
25
|
import { AgentHooks } from "./lifecycle";
|
|
26
|
+
import type {
|
|
27
|
+
AgentMemoryCreate,
|
|
28
|
+
MemoryListOptions,
|
|
29
|
+
MemorySearchQuery,
|
|
30
|
+
} from "./memory";
|
|
26
31
|
|
|
32
|
+
import { randomID } from "@kernl-sdk/shared/lib";
|
|
27
33
|
import { MisconfiguredError, RuntimeError } from "./lib/error";
|
|
28
|
-
|
|
34
|
+
|
|
35
|
+
/* types */
|
|
36
|
+
import type {
|
|
37
|
+
AgentConfig,
|
|
38
|
+
AgentMemoryConfig,
|
|
39
|
+
AgentResponseType,
|
|
40
|
+
} from "./agent/types";
|
|
29
41
|
import type {
|
|
30
42
|
TextResponse,
|
|
31
43
|
ThreadExecuteOptions,
|
|
32
44
|
ThreadExecuteResult,
|
|
33
45
|
ThreadStreamEvent,
|
|
34
|
-
} from "
|
|
35
|
-
import type { AgentMemoryCreate, MemorySearchQuery } from "./memory";
|
|
36
|
-
import { randomID } from "@kernl-sdk/shared/lib";
|
|
46
|
+
} from "./thread/types";
|
|
37
47
|
|
|
38
48
|
export class Agent<
|
|
39
49
|
TContext = UnknownContext,
|
|
@@ -46,12 +56,15 @@ export class Agent<
|
|
|
46
56
|
|
|
47
57
|
id: string;
|
|
48
58
|
name: string;
|
|
59
|
+
description?: string;
|
|
49
60
|
instructions: (context: Context<TContext>) => Promise<string> | string;
|
|
50
61
|
|
|
51
62
|
model: LanguageModel;
|
|
52
63
|
modelSettings: LanguageModelRequestSettings;
|
|
53
|
-
toolkits: BaseToolkit<TContext>[];
|
|
54
64
|
// actions: ActionSet; /* TODO */
|
|
65
|
+
toolkits: BaseToolkit<TContext>[];
|
|
66
|
+
systools: BaseToolkit<TContext>[];
|
|
67
|
+
memory: AgentMemoryConfig;
|
|
55
68
|
|
|
56
69
|
guardrails: {
|
|
57
70
|
input: InputGuardrail[];
|
|
@@ -59,10 +72,9 @@ export class Agent<
|
|
|
59
72
|
};
|
|
60
73
|
responseType: TResponse = "text" as TResponse;
|
|
61
74
|
resetToolChoice: boolean;
|
|
62
|
-
// toolUseBehavior: ToolUseBehavior; (TODO)
|
|
63
75
|
|
|
64
76
|
// --- (TODO) ---
|
|
65
|
-
//
|
|
77
|
+
// toolUseBehavior: ToolUseBehavior;
|
|
66
78
|
// handoffs: (Agent<any, TResponse> | Handoff<any, TResponse>)[];
|
|
67
79
|
// ----------
|
|
68
80
|
|
|
@@ -81,6 +93,7 @@ export class Agent<
|
|
|
81
93
|
}
|
|
82
94
|
this.id = config.id;
|
|
83
95
|
this.name = config.name;
|
|
96
|
+
this.description = config.description;
|
|
84
97
|
this.instructions =
|
|
85
98
|
typeof config.instructions === "function"
|
|
86
99
|
? config.instructions
|
|
@@ -89,6 +102,9 @@ export class Agent<
|
|
|
89
102
|
this.modelSettings = config.modelSettings ?? {};
|
|
90
103
|
|
|
91
104
|
this.toolkits = config.toolkits ?? [];
|
|
105
|
+
this.systools = [];
|
|
106
|
+
this.memory = config.memory ?? { enabled: false };
|
|
107
|
+
|
|
92
108
|
for (const toolkit of this.toolkits) {
|
|
93
109
|
toolkit.bind(this);
|
|
94
110
|
}
|
|
@@ -99,21 +115,6 @@ export class Agent<
|
|
|
99
115
|
}
|
|
100
116
|
this.resetToolChoice = config.resetToolChoice ?? true;
|
|
101
117
|
// this.toolUseBehavior = config.toolUseBehavior ?? "run_llm_again";
|
|
102
|
-
|
|
103
|
-
// this.handoffDescription = config.handoffDescription ?? "";
|
|
104
|
-
// this.handoffs = config.handoffs ?? [];
|
|
105
|
-
|
|
106
|
-
// --- Runtime warning for handoff response type compatibility ---
|
|
107
|
-
// if (config.handoffresponseTypeWarningEnabled) {
|
|
108
|
-
// ...
|
|
109
|
-
// if (responseTypes.size > 1) {
|
|
110
|
-
// logger.warn(
|
|
111
|
-
// `[Agent] Warning: Handoff agents have different response types: ${Array.from(responseTypes).join(", ")}.
|
|
112
|
-
// You can make it type-safe by using Agent.create({ ... }) method instead.`,
|
|
113
|
-
// );
|
|
114
|
-
// }
|
|
115
|
-
// }
|
|
116
|
-
// }
|
|
117
118
|
}
|
|
118
119
|
|
|
119
120
|
/**
|
|
@@ -121,6 +122,14 @@ export class Agent<
|
|
|
121
122
|
*/
|
|
122
123
|
bind(kernl: Kernl): void {
|
|
123
124
|
this.kernl = kernl;
|
|
125
|
+
|
|
126
|
+
// initialize system toolkits
|
|
127
|
+
if (this.memory.enabled) {
|
|
128
|
+
// safety: system tools only rely on ctx.agent, not ctx.context
|
|
129
|
+
const toolkit = memory as unknown as BaseToolkit<TContext>;
|
|
130
|
+
this.systools.push(toolkit);
|
|
131
|
+
toolkit.bind(this);
|
|
132
|
+
}
|
|
124
133
|
}
|
|
125
134
|
|
|
126
135
|
/**
|
|
@@ -247,12 +256,18 @@ export class Agent<
|
|
|
247
256
|
/**
|
|
248
257
|
* @internal
|
|
249
258
|
*
|
|
250
|
-
* Get a specific tool by ID from
|
|
259
|
+
* Get a specific tool by ID from systools and toolkits.
|
|
251
260
|
*
|
|
252
261
|
* @param id The tool ID to look up
|
|
253
262
|
* @returns The tool if found, undefined otherwise
|
|
254
263
|
*/
|
|
255
264
|
tool(id: string): Tool<TContext> | undefined {
|
|
265
|
+
// Check systools first
|
|
266
|
+
for (const toolkit of this.systools) {
|
|
267
|
+
const tool = toolkit.get(id);
|
|
268
|
+
if (tool) return tool;
|
|
269
|
+
}
|
|
270
|
+
// Then user toolkits
|
|
256
271
|
for (const toolkit of this.toolkits) {
|
|
257
272
|
const tool = toolkit.get(id);
|
|
258
273
|
if (tool) return tool;
|
|
@@ -263,7 +278,7 @@ export class Agent<
|
|
|
263
278
|
/**
|
|
264
279
|
* @internal
|
|
265
280
|
*
|
|
266
|
-
* Get all tools available from
|
|
281
|
+
* Get all tools available from systools and toolkits for the given context.
|
|
267
282
|
* Checks for duplicate tool IDs across toolkits and throws an error if found.
|
|
268
283
|
*
|
|
269
284
|
* (TODO): Consider returning toolkits alongside tools so we can serialize them
|
|
@@ -274,24 +289,22 @@ export class Agent<
|
|
|
274
289
|
* @throws {MisconfiguredError} If duplicate tool IDs are found across toolkits
|
|
275
290
|
*/
|
|
276
291
|
async tools(context: Context<TContext>): Promise<Tool<TContext>[]> {
|
|
277
|
-
const
|
|
278
|
-
const toolIds = new Set<string>();
|
|
292
|
+
const all: Tool<TContext>[] = [];
|
|
279
293
|
|
|
280
|
-
for (const toolkit of this.toolkits) {
|
|
281
|
-
|
|
294
|
+
for (const toolkit of [...this.systools, ...this.toolkits]) {
|
|
295
|
+
all.push(...(await toolkit.list(context)));
|
|
296
|
+
}
|
|
282
297
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
throw new MisconfiguredError(
|
|
286
|
-
`Duplicate tool IDs found across toolkits: ${duplicates.join(", ")}`,
|
|
287
|
-
);
|
|
288
|
-
}
|
|
298
|
+
const ids = all.map((t) => t.id);
|
|
299
|
+
const duplicates = ids.filter((id, i) => ids.indexOf(id) !== i);
|
|
289
300
|
|
|
290
|
-
|
|
291
|
-
|
|
301
|
+
if (duplicates.length > 0) {
|
|
302
|
+
throw new MisconfiguredError(
|
|
303
|
+
`Duplicate tool IDs found: ${[...new Set(duplicates)].join(", ")}`,
|
|
304
|
+
);
|
|
292
305
|
}
|
|
293
306
|
|
|
294
|
-
return
|
|
307
|
+
return all;
|
|
295
308
|
}
|
|
296
309
|
|
|
297
310
|
/**
|
|
@@ -359,6 +372,24 @@ export class Agent<
|
|
|
359
372
|
const kmem = this.kernl.memories;
|
|
360
373
|
|
|
361
374
|
return {
|
|
375
|
+
/**
|
|
376
|
+
* List memories scoped to this agent.
|
|
377
|
+
*/
|
|
378
|
+
list: (
|
|
379
|
+
params?: Omit<MemoryListOptions, "filter"> & {
|
|
380
|
+
collection?: string;
|
|
381
|
+
limit?: number;
|
|
382
|
+
// (TODO): we might want to add the filter back here
|
|
383
|
+
},
|
|
384
|
+
) =>
|
|
385
|
+
kmem.list({
|
|
386
|
+
filter: {
|
|
387
|
+
scope: { agentId },
|
|
388
|
+
collections: params?.collection ? [params.collection] : undefined,
|
|
389
|
+
},
|
|
390
|
+
limit: params?.limit,
|
|
391
|
+
}),
|
|
392
|
+
|
|
362
393
|
/**
|
|
363
394
|
* Create a new memory scoped to this agent.
|
|
364
395
|
*/
|
|
@@ -384,6 +415,7 @@ export class Agent<
|
|
|
384
415
|
*/
|
|
385
416
|
search: (
|
|
386
417
|
params: Omit<MemorySearchQuery, "filter"> & {
|
|
418
|
+
// (TODO): is this correct?
|
|
387
419
|
filter?: Omit<NonNullable<MemorySearchQuery["filter"]>, "scope"> & {
|
|
388
420
|
scope?: Omit<
|
|
389
421
|
NonNullable<NonNullable<MemorySearchQuery["filter"]>["scope"]>,
|