kernl 0.12.1 → 0.12.3
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 +22 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +10 -4
- package/dist/api/resources/agents/agents.d.ts +2 -2
- package/dist/api/resources/agents/agents.d.ts.map +1 -1
- package/dist/api/resources/agents/agents.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/kernl/index.d.ts +2 -1
- package/dist/kernl/index.d.ts.map +1 -1
- package/dist/kernl/index.js +1 -0
- package/dist/kernl/kernl.d.ts +4 -4
- package/dist/kernl/kernl.d.ts.map +1 -1
- package/dist/kernl/kernl.js +17 -13
- package/dist/kernl/registry.d.ts +46 -0
- package/dist/kernl/registry.d.ts.map +1 -0
- package/dist/kernl/registry.js +57 -0
- package/dist/kernl/types.d.ts +2 -2
- package/dist/kernl/types.d.ts.map +1 -1
- package/dist/lib/env.d.ts +2 -2
- package/dist/storage/base.d.ts +3 -3
- package/dist/storage/base.d.ts.map +1 -1
- package/dist/storage/in-memory.d.ts +5 -5
- package/dist/storage/in-memory.d.ts.map +1 -1
- package/dist/thread/__tests__/mock.d.ts.map +1 -1
- package/dist/thread/__tests__/mock.js +13 -4
- package/dist/thread/thread.js +7 -7
- package/package.json +3 -3
- package/src/agent.ts +13 -7
- package/src/api/__tests__/threads.test.ts +3 -3
- package/src/api/resources/agents/agents.ts +3 -3
- package/src/index.ts +3 -2
- package/src/kernl/index.ts +3 -2
- package/src/kernl/kernl.ts +18 -15
- package/src/kernl/registry.ts +69 -0
- package/src/kernl/types.ts +2 -2
- package/src/storage/base.ts +2 -2
- package/src/storage/in-memory.ts +4 -4
- package/src/thread/__tests__/mock.ts +13 -4
- package/src/thread/thread.ts +7 -7
package/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @kernl/core
|
|
2
2
|
|
|
3
|
+
## 0.12.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 884e513: Align with @ai-sdk/provider v3 stable release
|
|
8
|
+
- Update LanguageModelUsage to nested structure (inputTokens.total, outputTokens.total, etc.)
|
|
9
|
+
- Update LanguageModelFinishReason to object with unified and raw properties
|
|
10
|
+
- Rename LanguageModelWarning to SharedWarning with updated structure
|
|
11
|
+
- Update tool type from "provider-defined" to "provider"
|
|
12
|
+
- Bump @ai-sdk peer dependencies from beta to stable (^3.0.3)
|
|
13
|
+
|
|
14
|
+
- 0576a77: Fix model not being updated when resuming thread from storage
|
|
15
|
+
- Updated dependencies [884e513]
|
|
16
|
+
- @kernl-sdk/protocol@0.5.1
|
|
17
|
+
- @kernl-sdk/retrieval@0.1.10
|
|
18
|
+
|
|
19
|
+
## 0.12.2
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- 11cf6fd: Add ModelRegistry and AgentRegistry classes to fix hydration issues when threads use model overrides. Models are now auto-registered in spawn/schedule methods.
|
|
24
|
+
|
|
3
25
|
## 0.12.1
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,aAAa,EACb,iBAAiB,EACjB,4BAA4B,EAC7B,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EACV,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AACzD,OAAO,EACL,cAAc,EACd,eAAe,EACf,KAAK,qBAAqB,EAC3B,MAAM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EACnC,OAAO,CAAC,EAAE,oBAAoB,CAAC,QAAQ,CAAC,GACvC,aAAa,CAAC,iBAAiB,CAAC;IA6DnC;;;;OAIG;IACH,IAAI,OAAO;mBAWI,MAAM,YAAY,iBAAiB;wBAE/B,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC;sBAEpC,MAAM;uBACL,MAAM,WAAW,oBAAoB;yBAEnC,IAAI,CAAC,mBAAmB,EAAE,SAAS,GAAG,OAAO,CAAC;sBASjD,MAAM,SAAS,mBAAmB;MAGnD;CACF"}
|
package/dist/agent.js
CHANGED
|
@@ -27,14 +27,11 @@ export class Agent extends BaseAgent {
|
|
|
27
27
|
* @throws {MisconfiguredError} If the agent is not bound to a kernl instance
|
|
28
28
|
*/
|
|
29
29
|
async run(input, options) {
|
|
30
|
+
let thread = null;
|
|
30
31
|
if (!this.kernl) {
|
|
31
32
|
throw new MisconfiguredError(`Agent ${this.id} not bound to kernl. Call kernl.register(agent) first.`);
|
|
32
33
|
}
|
|
33
|
-
const items = typeof input === "string"
|
|
34
|
-
? [message({ role: "user", text: input })]
|
|
35
|
-
: input;
|
|
36
34
|
const tid = options?.threadId;
|
|
37
|
-
let thread = null;
|
|
38
35
|
if (tid) {
|
|
39
36
|
// no concurrent execution of same thread - correctness contract
|
|
40
37
|
// TODO: race condition - need to check again after async storage.get()
|
|
@@ -48,6 +45,9 @@ export class Agent extends BaseAgent {
|
|
|
48
45
|
}));
|
|
49
46
|
}
|
|
50
47
|
}
|
|
48
|
+
const items = typeof input === "string"
|
|
49
|
+
? [message({ role: "user", text: input })]
|
|
50
|
+
: input;
|
|
51
51
|
// create new thread if not found in storage or no tid provided
|
|
52
52
|
if (!thread) {
|
|
53
53
|
const ctx = options?.context
|
|
@@ -69,6 +69,9 @@ export class Agent extends BaseAgent {
|
|
|
69
69
|
if (options?.context) {
|
|
70
70
|
thread.context.context = options.context;
|
|
71
71
|
}
|
|
72
|
+
if (options?.model) {
|
|
73
|
+
thread.model = options.model;
|
|
74
|
+
}
|
|
72
75
|
thread.append(...items);
|
|
73
76
|
return this.kernl.schedule(thread);
|
|
74
77
|
}
|
|
@@ -124,6 +127,9 @@ export class Agent extends BaseAgent {
|
|
|
124
127
|
if (options?.context) {
|
|
125
128
|
thread.context.context = options.context;
|
|
126
129
|
}
|
|
130
|
+
if (options?.model) {
|
|
131
|
+
thread.model = options.model;
|
|
132
|
+
}
|
|
127
133
|
thread.append(...items);
|
|
128
134
|
yield* this.kernl.scheduleStream(thread);
|
|
129
135
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Agent } from "../../../agent.js";
|
|
2
|
-
import type { BaseAgent } from "../../../agent/base.js";
|
|
3
2
|
import type { AgentOutputType } from "../../../agent/types.js";
|
|
4
3
|
import type { UnknownContext } from "../../../context.js";
|
|
4
|
+
import type { AgentRegistry } from "../../../kernl/registry.js";
|
|
5
5
|
import type { TextOutput } from "../../../thread/types.js";
|
|
6
6
|
/**
|
|
7
7
|
* Agents resource.
|
|
@@ -16,7 +16,7 @@ import type { TextOutput } from "../../../thread/types.js";
|
|
|
16
16
|
*/
|
|
17
17
|
export declare class RAgents {
|
|
18
18
|
private readonly registry;
|
|
19
|
-
constructor(registry:
|
|
19
|
+
constructor(registry: AgentRegistry);
|
|
20
20
|
/**
|
|
21
21
|
* Get a live Agent instance by id.
|
|
22
22
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agents.d.ts","sourceRoot":"","sources":["../../../../src/api/resources/agents/agents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"agents.d.ts","sourceRoot":"","sources":["../../../../src/api/resources/agents/agents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD;;;;;;;;;;GAUG;AACH,qBAAa,OAAO;IACN,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,aAAa;IAEpD;;;;;;;OAOG;IACH,GAAG,CACD,QAAQ,GAAG,cAAc,EACzB,OAAO,SAAS,eAAe,GAAG,UAAU,EAC5C,EAAE,EAAE,MAAM,GAAG,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,SAAS;IAQnD;;OAEG;IACH,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAIxB;;OAEG;IACH,IAAI,IAAI,KAAK,CAAC,cAAc,EAAE,eAAe,CAAC,EAAE;IAMhD;;OAEG;IACH,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;CAGhC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { Kernl } from "./kernl/index.js";
|
|
2
|
-
export type { KernlOptions, StorageOptions,
|
|
2
|
+
export type { KernlOptions, StorageOptions, IAgentRegistry, IModelRegistry, } from "./kernl/index.js";
|
|
3
|
+
export { AgentRegistry, ModelRegistry } from "./kernl/index.js";
|
|
3
4
|
export { Agent } from "./agent.js";
|
|
4
5
|
export { Context } from "./context.js";
|
|
5
6
|
export type { LifecycleEvent, ThreadStartEvent, ThreadStopEvent, ModelCallStartEvent, ModelCallEndEvent, ToolCallStartEvent, ToolCallEndEvent, } from "./lifecycle.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,YAAY,EACV,YAAY,E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K,iBAAiB,GACvB,MAAM,gBAAgB,CAAC;AAIxB,YAAY,EACV,WAAW,EACX,SAAS,EACT,YAAY,EACZ,YAAY,EACZ,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,SAAS,EACT,YAAY,EACZ,WAAW,GACZ,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AACrD,YAAY,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,YAAY,EACZ,mBAAmB,EACnB,YAAY,EACZ,UAAU,EACV,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,qBAAqB,EACrB,uBAAuB,EACvB,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,UAAU,EACV,eAAe,GAChB,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/kernl/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { Kernl } from "./kernl.js";
|
|
2
|
-
export
|
|
2
|
+
export { AgentRegistry, ModelRegistry } from "./registry.js";
|
|
3
|
+
export type { KernlOptions, StorageOptions, MemoryOptions, IAgentRegistry, IModelRegistry, } from "./types.js";
|
|
3
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/kernl/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,YAAY,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/kernl/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC1D,YAAY,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,cAAc,EACd,cAAc,GACf,MAAM,SAAS,CAAC"}
|
package/dist/kernl/index.js
CHANGED
package/dist/kernl/kernl.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { KernlHooks } from "../lifecycle.js";
|
|
|
3
3
|
import type { Thread } from "../thread/index.js";
|
|
4
4
|
import type { ResolvedAgentResponse } from "../guardrail.js";
|
|
5
5
|
import { type KernlStorage } from "../storage/index.js";
|
|
6
|
+
import { Memory } from "../memory/index.js";
|
|
6
7
|
import { RThreads } from "../api/resources/threads/index.js";
|
|
7
8
|
import { RAgents } from "../api/resources/agents/index.js";
|
|
8
|
-
import { Memory } from "../memory/index.js";
|
|
9
9
|
import type { ThreadExecuteResult, ThreadStreamEvent } from "../thread/types.js";
|
|
10
10
|
import type { AgentOutputType } from "../agent/types.js";
|
|
11
11
|
import type { KernlOptions } from "./types.js";
|
|
@@ -18,10 +18,10 @@ import type { KernlOptions } from "./types.js";
|
|
|
18
18
|
export declare class Kernl extends KernlHooks {
|
|
19
19
|
private readonly _agents;
|
|
20
20
|
private readonly _models;
|
|
21
|
-
readonly storage: KernlStorage;
|
|
22
|
-
athreads: Map<string, Thread<any, any>>;
|
|
23
21
|
private readonly _memopts;
|
|
24
22
|
private readonly _storopts;
|
|
23
|
+
readonly storage: KernlStorage;
|
|
24
|
+
athreads: Map<string, Thread<any, any>>;
|
|
25
25
|
private warnings;
|
|
26
26
|
readonly threads: RThreads;
|
|
27
27
|
readonly agents: RAgents;
|
|
@@ -58,6 +58,6 @@ export declare class Kernl extends KernlHooks {
|
|
|
58
58
|
*
|
|
59
59
|
* Initialize the memory system based on the storage + memory configuration.
|
|
60
60
|
*/
|
|
61
|
-
private
|
|
61
|
+
private initmem;
|
|
62
62
|
}
|
|
63
63
|
//# sourceMappingURL=kernl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kernl.d.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ingModel } from "@kernl-sdk/retrieval";
|
|
2
2
|
import { KernlHooks } from "../lifecycle.js";
|
|
3
3
|
import { InMemoryStorage } from "../storage/index.js";
|
|
4
|
-
import { RThreads } from "../api/resources/threads/index.js";
|
|
5
|
-
import { RAgents } from "../api/resources/agents/index.js";
|
|
6
4
|
import { Memory, MemoryByteEncoder, MemoryIndexHandle, buildMemoryIndexSchema, } from "../memory/index.js";
|
|
7
5
|
import { logger } from "../lib/logger.js";
|
|
6
|
+
import { RThreads } from "../api/resources/threads/index.js";
|
|
7
|
+
import { RAgents } from "../api/resources/agents/index.js";
|
|
8
|
+
import { AgentRegistry, ModelRegistry } from "./registry.js";
|
|
8
9
|
/**
|
|
9
10
|
* The kernl - manages agent processes, scheduling, and task lifecycle.
|
|
10
11
|
*
|
|
@@ -12,12 +13,12 @@ import { logger } from "../lib/logger.js";
|
|
|
12
13
|
* tracing.
|
|
13
14
|
*/
|
|
14
15
|
export class Kernl extends KernlHooks {
|
|
15
|
-
_agents
|
|
16
|
-
_models
|
|
17
|
-
storage;
|
|
18
|
-
athreads = new Map(); /* active threads */
|
|
16
|
+
_agents;
|
|
17
|
+
_models;
|
|
19
18
|
_memopts;
|
|
20
19
|
_storopts;
|
|
20
|
+
storage;
|
|
21
|
+
athreads = new Map(); /* active threads */
|
|
21
22
|
warnings = {
|
|
22
23
|
embedding: false, // "Embeddings are not configured. If you want memories to auto-embed text content..."
|
|
23
24
|
vector: false, // "No vector storage configured. The memories.search() function will not be..."
|
|
@@ -30,17 +31,19 @@ export class Kernl extends KernlHooks {
|
|
|
30
31
|
super();
|
|
31
32
|
this._memopts = options.memory;
|
|
32
33
|
this._storopts = options.storage;
|
|
34
|
+
this._agents = new AgentRegistry();
|
|
35
|
+
this._models = new ModelRegistry();
|
|
33
36
|
this.storage = options.storage?.db ?? new InMemoryStorage();
|
|
34
37
|
this.storage.bind({ agents: this._agents, models: this._models });
|
|
35
38
|
this.threads = new RThreads(this.storage.threads);
|
|
36
39
|
this.agents = new RAgents(this._agents);
|
|
37
|
-
this.memories = this.
|
|
40
|
+
this.memories = this.initmem();
|
|
38
41
|
}
|
|
39
42
|
/**
|
|
40
43
|
* Registers a new agent with the kernl instance.
|
|
41
44
|
*/
|
|
42
45
|
register(agent) {
|
|
43
|
-
this._agents.
|
|
46
|
+
this._agents.register(agent);
|
|
44
47
|
agent.bind(this);
|
|
45
48
|
// memory config warnings (log once)
|
|
46
49
|
if (agent.memory.enabled) {
|
|
@@ -57,16 +60,14 @@ export class Kernl extends KernlHooks {
|
|
|
57
60
|
}
|
|
58
61
|
// auto-populate model registry for storage hydration (llm agents only - for now)
|
|
59
62
|
if (agent.kind === "llm") {
|
|
60
|
-
|
|
61
|
-
if (!this._models.has(key)) {
|
|
62
|
-
this._models.set(key, agent.model);
|
|
63
|
-
}
|
|
63
|
+
this._models.register(agent.model);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
67
|
* Spawn a new thread - blocking execution
|
|
68
68
|
*/
|
|
69
69
|
async spawn(thread) {
|
|
70
|
+
this._models.register(thread.model);
|
|
70
71
|
this.athreads.set(thread.tid, thread);
|
|
71
72
|
try {
|
|
72
73
|
return await thread.execute();
|
|
@@ -81,6 +82,7 @@ export class Kernl extends KernlHooks {
|
|
|
81
82
|
* NOTE: just blocks for now
|
|
82
83
|
*/
|
|
83
84
|
async schedule(thread) {
|
|
85
|
+
this._models.register(thread.model);
|
|
84
86
|
this.athreads.set(thread.tid, thread);
|
|
85
87
|
try {
|
|
86
88
|
return await thread.execute();
|
|
@@ -95,6 +97,7 @@ export class Kernl extends KernlHooks {
|
|
|
95
97
|
* Spawn a new thread - streaming execution
|
|
96
98
|
*/
|
|
97
99
|
async *spawnStream(thread) {
|
|
100
|
+
this._models.register(thread.model);
|
|
98
101
|
this.athreads.set(thread.tid, thread);
|
|
99
102
|
try {
|
|
100
103
|
yield* thread.stream();
|
|
@@ -109,6 +112,7 @@ export class Kernl extends KernlHooks {
|
|
|
109
112
|
* Schedule an existing thread - streaming execution
|
|
110
113
|
*/
|
|
111
114
|
async *scheduleStream(thread) {
|
|
115
|
+
this._models.register(thread.model);
|
|
112
116
|
this.athreads.set(thread.tid, thread);
|
|
113
117
|
try {
|
|
114
118
|
yield* thread.stream();
|
|
@@ -123,7 +127,7 @@ export class Kernl extends KernlHooks {
|
|
|
123
127
|
*
|
|
124
128
|
* Initialize the memory system based on the storage + memory configuration.
|
|
125
129
|
*/
|
|
126
|
-
|
|
130
|
+
initmem() {
|
|
127
131
|
const embeddingModel = this._memopts?.embedding;
|
|
128
132
|
const embedder = embeddingModel
|
|
129
133
|
? typeof embeddingModel === "string"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { LanguageModel } from "@kernl-sdk/protocol";
|
|
2
|
+
import type { BaseAgent } from "../agent/base.js";
|
|
3
|
+
import type { IAgentRegistry, IModelRegistry } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Registry for language models used by threads.
|
|
6
|
+
*
|
|
7
|
+
* Models are keyed by "{provider}/{modelId}" and must be registered before
|
|
8
|
+
* storage can hydrate threads that reference them.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ModelRegistry implements IModelRegistry {
|
|
11
|
+
private readonly models;
|
|
12
|
+
/**
|
|
13
|
+
* Register a model instance. Idempotent - only adds if not already present.
|
|
14
|
+
*/
|
|
15
|
+
register(model: LanguageModel): void;
|
|
16
|
+
/**
|
|
17
|
+
* Get a model by its composite key ("{provider}/{modelId}").
|
|
18
|
+
*/
|
|
19
|
+
get(key: string): LanguageModel | undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Registry for agents.
|
|
23
|
+
*
|
|
24
|
+
* Agents are keyed by their id and must be registered before threads can
|
|
25
|
+
* reference them.
|
|
26
|
+
*/
|
|
27
|
+
export declare class AgentRegistry implements IAgentRegistry {
|
|
28
|
+
private readonly agents;
|
|
29
|
+
/**
|
|
30
|
+
* Register an agent instance. Replaces existing agent with same id.
|
|
31
|
+
*/
|
|
32
|
+
register(agent: BaseAgent<any>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Get an agent by its id.
|
|
35
|
+
*/
|
|
36
|
+
get(id: string): BaseAgent<any> | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Unregister an agent by id.
|
|
39
|
+
*/
|
|
40
|
+
unregister(id: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* List all registered agents.
|
|
43
|
+
*/
|
|
44
|
+
values(): IterableIterator<BaseAgent<any>>;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/kernl/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9D;;;;;GAKG;AACH,qBAAa,aAAc,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyC;IAEhE;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI;IAOpC;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;CAG5C;AAED;;;;;GAKG;AACH,qBAAa,aAAc,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0C;IAEjE;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI;IAIrC;;OAEG;IACH,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,GAAG,SAAS;IAI3C;;OAEG;IACH,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI/B;;OAEG;IACH,MAAM,IAAI,gBAAgB,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;CAG3C"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registry for language models used by threads.
|
|
3
|
+
*
|
|
4
|
+
* Models are keyed by "{provider}/{modelId}" and must be registered before
|
|
5
|
+
* storage can hydrate threads that reference them.
|
|
6
|
+
*/
|
|
7
|
+
export class ModelRegistry {
|
|
8
|
+
models = new Map();
|
|
9
|
+
/**
|
|
10
|
+
* Register a model instance. Idempotent - only adds if not already present.
|
|
11
|
+
*/
|
|
12
|
+
register(model) {
|
|
13
|
+
const key = `${model.provider}/${model.modelId}`;
|
|
14
|
+
if (!this.models.has(key)) {
|
|
15
|
+
this.models.set(key, model);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Get a model by its composite key ("{provider}/{modelId}").
|
|
20
|
+
*/
|
|
21
|
+
get(key) {
|
|
22
|
+
return this.models.get(key);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Registry for agents.
|
|
27
|
+
*
|
|
28
|
+
* Agents are keyed by their id and must be registered before threads can
|
|
29
|
+
* reference them.
|
|
30
|
+
*/
|
|
31
|
+
export class AgentRegistry {
|
|
32
|
+
agents = new Map();
|
|
33
|
+
/**
|
|
34
|
+
* Register an agent instance. Replaces existing agent with same id.
|
|
35
|
+
*/
|
|
36
|
+
register(agent) {
|
|
37
|
+
this.agents.set(agent.id, agent);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get an agent by its id.
|
|
41
|
+
*/
|
|
42
|
+
get(id) {
|
|
43
|
+
return this.agents.get(id);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Unregister an agent by id.
|
|
47
|
+
*/
|
|
48
|
+
unregister(id) {
|
|
49
|
+
return this.agents.delete(id);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* List all registered agents.
|
|
53
|
+
*/
|
|
54
|
+
values() {
|
|
55
|
+
return this.agents.values();
|
|
56
|
+
}
|
|
57
|
+
}
|
package/dist/kernl/types.d.ts
CHANGED
|
@@ -74,7 +74,7 @@ export interface KernlOptions {
|
|
|
74
74
|
*
|
|
75
75
|
* Satisfied by Map<string, BaseAgent>.
|
|
76
76
|
*/
|
|
77
|
-
export interface
|
|
77
|
+
export interface IAgentRegistry {
|
|
78
78
|
get(id: string): BaseAgent<any> | undefined;
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
@@ -86,7 +86,7 @@ export interface AgentRegistry {
|
|
|
86
86
|
* TODO: Create an exhaustive model registry in the protocol package
|
|
87
87
|
* with all supported models and their metadata.
|
|
88
88
|
*/
|
|
89
|
-
export interface
|
|
89
|
+
export interface IModelRegistry {
|
|
90
90
|
get(key: string): LanguageModel | undefined;
|
|
91
91
|
}
|
|
92
92
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/kernl/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,EAAE,CAAC,EAAE,YAAY,CAAC;IAElB;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CAKtB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IAE5C;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;OAIG;IACH,oBAAoB,CAAC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,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB;;OAEG;IACH,MAAM,CAAC,EAAE,aAAa,CAAC;CACxB;AAED;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;CAC7C;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,cAAc;IAC7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAAC;CAC7C"}
|
package/dist/lib/env.d.ts
CHANGED
|
@@ -7,11 +7,11 @@ import { z } from "zod";
|
|
|
7
7
|
*/
|
|
8
8
|
declare const envSchema: z.ZodObject<{
|
|
9
9
|
LOG_LEVEL: z.ZodDefault<z.ZodEnum<{
|
|
10
|
-
error: "error";
|
|
11
10
|
trace: "trace";
|
|
12
11
|
debug: "debug";
|
|
13
12
|
info: "info";
|
|
14
13
|
warn: "warn";
|
|
14
|
+
error: "error";
|
|
15
15
|
fatal: "fatal";
|
|
16
16
|
}>>;
|
|
17
17
|
KERNL_LOG_MODEL_DATA: z.ZodPipe<z.ZodOptional<z.ZodEnum<{
|
|
@@ -31,7 +31,7 @@ declare const envSchema: z.ZodObject<{
|
|
|
31
31
|
* console.log(env.LOG_LEVEL);
|
|
32
32
|
*/
|
|
33
33
|
export declare const env: {
|
|
34
|
-
LOG_LEVEL: "
|
|
34
|
+
LOG_LEVEL: "trace" | "debug" | "info" | "warn" | "error" | "fatal";
|
|
35
35
|
KERNL_LOG_MODEL_DATA: boolean;
|
|
36
36
|
KERNL_LOG_TOOL_DATA: boolean;
|
|
37
37
|
};
|
package/dist/storage/base.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Core storage contracts.
|
|
3
3
|
*/
|
|
4
|
-
import type {
|
|
4
|
+
import type { IAgentRegistry, IModelRegistry } from "../kernl/types.js";
|
|
5
5
|
import type { ThreadStore } from "./thread.js";
|
|
6
6
|
import type { MemoryStore } from "../memory/store.js";
|
|
7
7
|
/**
|
|
@@ -24,8 +24,8 @@ export interface KernlStorage {
|
|
|
24
24
|
* Called by Kernl after construction to wire up agent/model lookups.
|
|
25
25
|
*/
|
|
26
26
|
bind(registries: {
|
|
27
|
-
agents:
|
|
28
|
-
models:
|
|
27
|
+
agents: IAgentRegistry;
|
|
28
|
+
models: IModelRegistry;
|
|
29
29
|
}): void;
|
|
30
30
|
/**
|
|
31
31
|
* Execute a function within a transaction.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/storage/base.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/storage/base.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,OAAO,EAAE,WAAW,CAAC;IAErB;;OAEG;IACH,QAAQ,EAAE,WAAW,CAAC;IAKtB;;;;OAIG;IACH,IAAI,CAAC,UAAU,EAAE;QAAE,MAAM,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,cAAc,CAAA;KAAE,GAAG,IAAI,CAAC;IAE3E;;;;;OAKG;IACH,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAEhE;;;;OAIG;IACH,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtB;;OAEG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;OAEG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,OAAO,EAAE,WAAW,CAAC;IAMrB;;OAEG;IACH,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;OAEG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B"}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { Thread } from "../thread/index.js";
|
|
9
9
|
import type { KernlStorage, Transaction, ThreadStore, NewThread, ThreadUpdate, ThreadInclude, ThreadListOptions, ThreadHistoryOptions } from "../storage/index.js";
|
|
10
10
|
import type { ThreadEvent } from "../thread/types.js";
|
|
11
|
-
import type {
|
|
11
|
+
import type { IAgentRegistry, IModelRegistry } from "../kernl/types.js";
|
|
12
12
|
import type { MemoryStore, MemoryRecord, NewMemory, MemoryRecordUpdate, MemoryListOptions } from "../memory/index.js";
|
|
13
13
|
/**
|
|
14
14
|
* In-memory storage implementation.
|
|
@@ -18,8 +18,8 @@ export declare class InMemoryStorage implements KernlStorage {
|
|
|
18
18
|
memories: InMemoryMemoryStore;
|
|
19
19
|
constructor();
|
|
20
20
|
bind(registries: {
|
|
21
|
-
agents:
|
|
22
|
-
models:
|
|
21
|
+
agents: IAgentRegistry;
|
|
22
|
+
models: IModelRegistry;
|
|
23
23
|
}): void;
|
|
24
24
|
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
25
25
|
init(): Promise<void>;
|
|
@@ -34,8 +34,8 @@ export declare class InMemoryThreadStore implements ThreadStore {
|
|
|
34
34
|
private events;
|
|
35
35
|
private registries;
|
|
36
36
|
bind(registries: {
|
|
37
|
-
agents:
|
|
38
|
-
models:
|
|
37
|
+
agents: IAgentRegistry;
|
|
38
|
+
models: IModelRegistry;
|
|
39
39
|
}): void;
|
|
40
40
|
get(tid: string, include?: ThreadInclude): Promise<Thread | null>;
|
|
41
41
|
list(options?: ThreadListOptions): Promise<Thread[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.d.ts","sourceRoot":"","sources":["../../src/storage/in-memory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAKlC,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,WAAW,EACX,SAAS,EACT,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,oBAAoB,EAGrB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"in-memory.d.ts","sourceRoot":"","sources":["../../src/storage/in-memory.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAKlC,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,WAAW,EACX,SAAS,EACT,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,oBAAoB,EAGrB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpE,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,SAAS,EACT,kBAAkB,EAClB,iBAAiB,EAElB,MAAM,UAAU,CAAC;AAElB;;GAEG;AACH,qBAAa,eAAgB,YAAW,YAAY;IAClD,OAAO,EAAE,mBAAmB,CAAC;IAC7B,QAAQ,EAAE,mBAAmB,CAAC;;IAO9B,IAAI,CAAC,UAAU,EAAE;QAAE,MAAM,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,cAAc,CAAA;KAAE,GAAG,IAAI;IAIpE,WAAW,CAAC,C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,YAAY;CA6BrB;AAED;;GAEG;AACH,qBAAa,mBAAoB,YAAW,WAAW;IACrD,OAAO,CAAC,QAAQ,CAAmC;IAE7C,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAI7C,IAAI,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;IAkB1D,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC;IAoBhD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;IAoBpE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIjC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAM3C;;OAEG;IACH,OAAO,CAAC,YAAY;IAmEpB;;OAEG;IACH,OAAO,CAAC,YAAY;CASrB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../../src/thread/__tests__/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EAGrB,wBAAwB,EACzB,MAAM,qBAAqB,CAAC;AAE7B;;;;;GAKG;AACH,qBAAa,iBAAkB,YAAW,aAAa;IACrD,QAAQ,CAAC,IAAI,EAAG,KAAK,CAAU;IAC/B,QAAQ,CAAC,QAAQ,UAAU;IAC3B,QAAQ,CAAC,OAAO,gBAAgB;IAE1B,QAAQ,CACZ,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../../src/thread/__tests__/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EAGrB,wBAAwB,EACzB,MAAM,qBAAqB,CAAC;AAE7B;;;;;GAKG;AACH,qBAAa,iBAAkB,YAAW,aAAa;IACrD,QAAQ,CAAC,IAAI,EAAG,KAAK,CAAU;IAC/B,QAAQ,CAAC,QAAQ,UAAU;IAC3B,QAAQ,CAAC,OAAO,gBAAgB;IAE1B,QAAQ,CACZ,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,qBAAqB,CAAC;IAyC1B,MAAM,CACX,OAAO,EAAE,oBAAoB,GAC5B,aAAa,CAAC,wBAAwB,CAAC;IAK1C;;OAEG;IACH,OAAO,CAAC,eAAe;IAgBvB;;OAEG;IACH,OAAO,CAAC,WAAW;IAKnB;;OAEG;IACH,OAAO,CAAC,UAAU;CAGnB"}
|
|
@@ -26,13 +26,22 @@ export class MockLanguageModel {
|
|
|
26
26
|
},
|
|
27
27
|
];
|
|
28
28
|
// Return with fake usage stats
|
|
29
|
+
const tokens = this.countTokens(userText);
|
|
29
30
|
return {
|
|
30
31
|
content,
|
|
31
|
-
finishReason: "stop",
|
|
32
|
+
finishReason: { unified: "stop", raw: "stop" },
|
|
32
33
|
usage: {
|
|
33
|
-
inputTokens:
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
inputTokens: {
|
|
35
|
+
total: tokens,
|
|
36
|
+
noCache: tokens,
|
|
37
|
+
cacheRead: undefined,
|
|
38
|
+
cacheWrite: undefined,
|
|
39
|
+
},
|
|
40
|
+
outputTokens: {
|
|
41
|
+
total: tokens,
|
|
42
|
+
text: tokens,
|
|
43
|
+
reasoning: undefined,
|
|
44
|
+
},
|
|
36
45
|
},
|
|
37
46
|
warnings: [],
|
|
38
47
|
};
|
package/dist/thread/thread.js
CHANGED
|
@@ -155,13 +155,13 @@ export class Thread {
|
|
|
155
155
|
for await (const e of this.tick()) {
|
|
156
156
|
if (e.kind === "error") {
|
|
157
157
|
err = e.error;
|
|
158
|
-
logger.error(
|
|
158
|
+
logger.error(err.message); // (TODO): onError callback in options
|
|
159
159
|
}
|
|
160
160
|
// complete items get persisted with seq, deltas are ephemeral
|
|
161
161
|
if (notDelta(e)) {
|
|
162
|
-
const [
|
|
163
|
-
events.push(
|
|
164
|
-
yield
|
|
162
|
+
const [eseq] = this.append(e);
|
|
163
|
+
events.push(eseq);
|
|
164
|
+
yield eseq;
|
|
165
165
|
}
|
|
166
166
|
else {
|
|
167
167
|
yield e;
|
|
@@ -187,8 +187,8 @@ export class Thread {
|
|
|
187
187
|
const { actions, pendingApprovals } = await this.performActions(intentions);
|
|
188
188
|
// append + yield action events (sequenced)
|
|
189
189
|
for (const a of actions) {
|
|
190
|
-
const [
|
|
191
|
-
yield
|
|
190
|
+
const [eseq] = this.append(a);
|
|
191
|
+
yield eseq;
|
|
192
192
|
}
|
|
193
193
|
await this.checkpoint(); /* c3: tick complete */
|
|
194
194
|
if (pendingApprovals.length > 0) {
|
|
@@ -216,7 +216,7 @@ export class Thread {
|
|
|
216
216
|
const req = await this.prepareModelRequest(this.history);
|
|
217
217
|
this.emit("model.call.start", { settings: req.settings ?? {} });
|
|
218
218
|
let usage;
|
|
219
|
-
let finishReason = "
|
|
219
|
+
let finishReason = { unified: "other", raw: undefined };
|
|
220
220
|
try {
|
|
221
221
|
if (this.model.stream) {
|
|
222
222
|
for await (const event of this.model.stream(req)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kernl",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.3",
|
|
4
4
|
"description": "A modern AI agent framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kernl",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@modelcontextprotocol/sdk": "^1.20.2",
|
|
36
36
|
"yaml": "^2.8.2",
|
|
37
|
-
"@kernl-sdk/protocol": "0.5.
|
|
38
|
-
"@kernl-sdk/retrieval": "0.1.
|
|
37
|
+
"@kernl-sdk/protocol": "0.5.1",
|
|
38
|
+
"@kernl-sdk/retrieval": "0.1.10",
|
|
39
39
|
"@kernl-sdk/shared": "^0.4.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
package/src/agent.ts
CHANGED
|
@@ -71,20 +71,15 @@ export class Agent<
|
|
|
71
71
|
input: string | LanguageModelItem[],
|
|
72
72
|
options?: ThreadExecuteOptions<TContext>,
|
|
73
73
|
): Promise<ThreadExecuteResult<ResolvedAgentResponse<TOutput>>> {
|
|
74
|
+
let thread: Thread<TContext, TOutput> | null = null;
|
|
75
|
+
|
|
74
76
|
if (!this.kernl) {
|
|
75
77
|
throw new MisconfiguredError(
|
|
76
78
|
`Agent ${this.id} not bound to kernl. Call kernl.register(agent) first.`,
|
|
77
79
|
);
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
const items =
|
|
81
|
-
typeof input === "string"
|
|
82
|
-
? [message({ role: "user", text: input })]
|
|
83
|
-
: input;
|
|
84
82
|
const tid = options?.threadId;
|
|
85
|
-
|
|
86
|
-
let thread: Thread<TContext, TOutput> | null = null;
|
|
87
|
-
|
|
88
83
|
if (tid) {
|
|
89
84
|
// no concurrent execution of same thread - correctness contract
|
|
90
85
|
// TODO: race condition - need to check again after async storage.get()
|
|
@@ -100,6 +95,11 @@ export class Agent<
|
|
|
100
95
|
}
|
|
101
96
|
}
|
|
102
97
|
|
|
98
|
+
const items =
|
|
99
|
+
typeof input === "string"
|
|
100
|
+
? [message({ role: "user", text: input })]
|
|
101
|
+
: input;
|
|
102
|
+
|
|
103
103
|
// create new thread if not found in storage or no tid provided
|
|
104
104
|
if (!thread) {
|
|
105
105
|
const ctx = options?.context
|
|
@@ -123,6 +123,9 @@ export class Agent<
|
|
|
123
123
|
if (options?.context) {
|
|
124
124
|
thread.context.context = options.context;
|
|
125
125
|
}
|
|
126
|
+
if (options?.model) {
|
|
127
|
+
thread.model = options.model;
|
|
128
|
+
}
|
|
126
129
|
thread.append(...items);
|
|
127
130
|
return this.kernl.schedule(thread);
|
|
128
131
|
}
|
|
@@ -192,6 +195,9 @@ export class Agent<
|
|
|
192
195
|
if (options?.context) {
|
|
193
196
|
thread.context.context = options.context;
|
|
194
197
|
}
|
|
198
|
+
if (options?.model) {
|
|
199
|
+
thread.model = options.model;
|
|
200
|
+
}
|
|
195
201
|
thread.append(...items);
|
|
196
202
|
yield* this.kernl.scheduleStream(thread);
|
|
197
203
|
}
|
|
@@ -4,7 +4,7 @@ import { message, RUNNING } from "@kernl-sdk/protocol";
|
|
|
4
4
|
|
|
5
5
|
import { Agent } from "@/agent";
|
|
6
6
|
import { InMemoryThreadStore } from "@/storage/in-memory";
|
|
7
|
-
import type {
|
|
7
|
+
import type { IAgentRegistry, IModelRegistry } from "@/kernl/types";
|
|
8
8
|
import type { NewThread } from "@/storage";
|
|
9
9
|
import { tevent } from "@/thread/utils";
|
|
10
10
|
import type { ThreadEvent } from "@/thread/types";
|
|
@@ -14,8 +14,8 @@ import type { MThread } from "@/api/models";
|
|
|
14
14
|
function createTestStore() {
|
|
15
15
|
const store = new InMemoryThreadStore();
|
|
16
16
|
|
|
17
|
-
const agents:
|
|
18
|
-
const models:
|
|
17
|
+
const agents: IAgentRegistry & Map<string, Agent> = new Map();
|
|
18
|
+
const models: IModelRegistry & Map<string, any> = new Map();
|
|
19
19
|
|
|
20
20
|
const model = {
|
|
21
21
|
spec: "1.0" as const,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Agent } from "@/agent";
|
|
2
|
-
import type { BaseAgent } from "@/agent/base";
|
|
3
2
|
import type { AgentOutputType } from "@/agent/types";
|
|
4
3
|
import type { UnknownContext } from "@/context";
|
|
4
|
+
import type { AgentRegistry } from "@/kernl/registry";
|
|
5
5
|
import type { TextOutput } from "@/thread/types";
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -16,7 +16,7 @@ import type { TextOutput } from "@/thread/types";
|
|
|
16
16
|
* to realtime agents is needed, add a separate `kernl.realtimeAgents` resource.
|
|
17
17
|
*/
|
|
18
18
|
export class RAgents {
|
|
19
|
-
constructor(private readonly registry:
|
|
19
|
+
constructor(private readonly registry: AgentRegistry) {}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Get a live Agent instance by id.
|
|
@@ -57,6 +57,6 @@ export class RAgents {
|
|
|
57
57
|
* Unregister an agent at runtime.
|
|
58
58
|
*/
|
|
59
59
|
unregister(id: string): boolean {
|
|
60
|
-
return this.registry.
|
|
60
|
+
return this.registry.unregister(id);
|
|
61
61
|
}
|
|
62
62
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,9 +2,10 @@ export { Kernl } from "./kernl";
|
|
|
2
2
|
export type {
|
|
3
3
|
KernlOptions,
|
|
4
4
|
StorageOptions,
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
IAgentRegistry,
|
|
6
|
+
IModelRegistry,
|
|
7
7
|
} from "./kernl";
|
|
8
|
+
export { AgentRegistry, ModelRegistry } from "./kernl";
|
|
8
9
|
export { Agent } from "./agent";
|
|
9
10
|
export { Context } from "./context";
|
|
10
11
|
|
package/src/kernl/index.ts
CHANGED
package/src/kernl/kernl.ts
CHANGED
|
@@ -6,8 +6,6 @@ import { KernlHooks } from "@/lifecycle";
|
|
|
6
6
|
import type { Thread } from "@/thread";
|
|
7
7
|
import type { ResolvedAgentResponse } from "@/guardrail";
|
|
8
8
|
import { InMemoryStorage, type KernlStorage } from "@/storage";
|
|
9
|
-
import { RThreads } from "@/api/resources/threads";
|
|
10
|
-
import { RAgents } from "@/api/resources/agents";
|
|
11
9
|
import {
|
|
12
10
|
Memory,
|
|
13
11
|
MemoryByteEncoder,
|
|
@@ -16,10 +14,13 @@ import {
|
|
|
16
14
|
} from "@/memory";
|
|
17
15
|
|
|
18
16
|
import { logger } from "@/lib/logger";
|
|
19
|
-
|
|
17
|
+
import { RThreads } from "@/api/resources/threads";
|
|
18
|
+
import { RAgents } from "@/api/resources/agents";
|
|
20
19
|
import type { ThreadExecuteResult, ThreadStreamEvent } from "@/thread/types";
|
|
21
20
|
import type { AgentOutputType } from "@/agent/types";
|
|
21
|
+
|
|
22
22
|
import type { KernlOptions, MemoryOptions, StorageOptions } from "./types";
|
|
23
|
+
import { AgentRegistry, ModelRegistry } from "./registry";
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* The kernl - manages agent processes, scheduling, and task lifecycle.
|
|
@@ -28,15 +29,14 @@ import type { KernlOptions, MemoryOptions, StorageOptions } from "./types";
|
|
|
28
29
|
* tracing.
|
|
29
30
|
*/
|
|
30
31
|
export class Kernl extends KernlHooks {
|
|
31
|
-
private readonly _agents:
|
|
32
|
-
private readonly _models:
|
|
32
|
+
private readonly _agents: AgentRegistry;
|
|
33
|
+
private readonly _models: ModelRegistry;
|
|
34
|
+
private readonly _memopts: MemoryOptions | undefined;
|
|
35
|
+
private readonly _storopts: StorageOptions | undefined;
|
|
33
36
|
|
|
34
37
|
readonly storage: KernlStorage;
|
|
35
38
|
athreads: Map<string, Thread<any, any>> = new Map(); /* active threads */
|
|
36
39
|
|
|
37
|
-
private readonly _memopts: MemoryOptions | undefined;
|
|
38
|
-
private readonly _storopts: StorageOptions | undefined;
|
|
39
|
-
|
|
40
40
|
private warnings = {
|
|
41
41
|
embedding: false, // "Embeddings are not configured. If you want memories to auto-embed text content..."
|
|
42
42
|
vector: false, // "No vector storage configured. The memories.search() function will not be..."
|
|
@@ -53,18 +53,20 @@ export class Kernl extends KernlHooks {
|
|
|
53
53
|
this._memopts = options.memory;
|
|
54
54
|
this._storopts = options.storage;
|
|
55
55
|
|
|
56
|
+
this._agents = new AgentRegistry();
|
|
57
|
+
this._models = new ModelRegistry();
|
|
56
58
|
this.storage = options.storage?.db ?? new InMemoryStorage();
|
|
57
59
|
this.storage.bind({ agents: this._agents, models: this._models });
|
|
58
60
|
this.threads = new RThreads(this.storage.threads);
|
|
59
61
|
this.agents = new RAgents(this._agents);
|
|
60
|
-
this.memories = this.
|
|
62
|
+
this.memories = this.initmem();
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
/**
|
|
64
66
|
* Registers a new agent with the kernl instance.
|
|
65
67
|
*/
|
|
66
68
|
register(agent: BaseAgent<any>): void {
|
|
67
|
-
this._agents.
|
|
69
|
+
this._agents.register(agent);
|
|
68
70
|
agent.bind(this);
|
|
69
71
|
|
|
70
72
|
// memory config warnings (log once)
|
|
@@ -88,10 +90,7 @@ export class Kernl extends KernlHooks {
|
|
|
88
90
|
|
|
89
91
|
// auto-populate model registry for storage hydration (llm agents only - for now)
|
|
90
92
|
if (agent.kind === "llm") {
|
|
91
|
-
|
|
92
|
-
if (!this._models.has(key)) {
|
|
93
|
-
this._models.set(key, agent.model as LanguageModel);
|
|
94
|
-
}
|
|
93
|
+
this._models.register(agent.model as LanguageModel);
|
|
95
94
|
}
|
|
96
95
|
}
|
|
97
96
|
|
|
@@ -101,6 +100,7 @@ export class Kernl extends KernlHooks {
|
|
|
101
100
|
async spawn<TContext, TOutput extends AgentOutputType>(
|
|
102
101
|
thread: Thread<TContext, TOutput>,
|
|
103
102
|
): Promise<ThreadExecuteResult<ResolvedAgentResponse<TOutput>>> {
|
|
103
|
+
this._models.register(thread.model);
|
|
104
104
|
this.athreads.set(thread.tid, thread);
|
|
105
105
|
try {
|
|
106
106
|
return await thread.execute();
|
|
@@ -117,6 +117,7 @@ export class Kernl extends KernlHooks {
|
|
|
117
117
|
async schedule<TContext, TOutput extends AgentOutputType>(
|
|
118
118
|
thread: Thread<TContext, TOutput>,
|
|
119
119
|
): Promise<ThreadExecuteResult<ResolvedAgentResponse<TOutput>>> {
|
|
120
|
+
this._models.register(thread.model);
|
|
120
121
|
this.athreads.set(thread.tid, thread);
|
|
121
122
|
try {
|
|
122
123
|
return await thread.execute();
|
|
@@ -133,6 +134,7 @@ export class Kernl extends KernlHooks {
|
|
|
133
134
|
async *spawnStream<TContext, TOutput extends AgentOutputType>(
|
|
134
135
|
thread: Thread<TContext, TOutput>,
|
|
135
136
|
): AsyncIterable<ThreadStreamEvent> {
|
|
137
|
+
this._models.register(thread.model);
|
|
136
138
|
this.athreads.set(thread.tid, thread);
|
|
137
139
|
try {
|
|
138
140
|
yield* thread.stream();
|
|
@@ -149,6 +151,7 @@ export class Kernl extends KernlHooks {
|
|
|
149
151
|
async *scheduleStream<TContext, TOutput extends AgentOutputType>(
|
|
150
152
|
thread: Thread<TContext, TOutput>,
|
|
151
153
|
): AsyncIterable<ThreadStreamEvent> {
|
|
154
|
+
this._models.register(thread.model);
|
|
152
155
|
this.athreads.set(thread.tid, thread);
|
|
153
156
|
try {
|
|
154
157
|
yield* thread.stream();
|
|
@@ -164,7 +167,7 @@ export class Kernl extends KernlHooks {
|
|
|
164
167
|
*
|
|
165
168
|
* Initialize the memory system based on the storage + memory configuration.
|
|
166
169
|
*/
|
|
167
|
-
private
|
|
170
|
+
private initmem(): Memory {
|
|
168
171
|
const embeddingModel = this._memopts?.embedding;
|
|
169
172
|
const embedder = embeddingModel
|
|
170
173
|
? typeof embeddingModel === "string"
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { LanguageModel } from "@kernl-sdk/protocol";
|
|
2
|
+
|
|
3
|
+
import type { BaseAgent } from "@/agent/base";
|
|
4
|
+
import type { IAgentRegistry, IModelRegistry } from "./types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Registry for language models used by threads.
|
|
8
|
+
*
|
|
9
|
+
* Models are keyed by "{provider}/{modelId}" and must be registered before
|
|
10
|
+
* storage can hydrate threads that reference them.
|
|
11
|
+
*/
|
|
12
|
+
export class ModelRegistry implements IModelRegistry {
|
|
13
|
+
private readonly models: Map<string, LanguageModel> = new Map();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Register a model instance. Idempotent - only adds if not already present.
|
|
17
|
+
*/
|
|
18
|
+
register(model: LanguageModel): void {
|
|
19
|
+
const key = `${model.provider}/${model.modelId}`;
|
|
20
|
+
if (!this.models.has(key)) {
|
|
21
|
+
this.models.set(key, model);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get a model by its composite key ("{provider}/{modelId}").
|
|
27
|
+
*/
|
|
28
|
+
get(key: string): LanguageModel | undefined {
|
|
29
|
+
return this.models.get(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Registry for agents.
|
|
35
|
+
*
|
|
36
|
+
* Agents are keyed by their id and must be registered before threads can
|
|
37
|
+
* reference them.
|
|
38
|
+
*/
|
|
39
|
+
export class AgentRegistry implements IAgentRegistry {
|
|
40
|
+
private readonly agents: Map<string, BaseAgent<any>> = new Map();
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Register an agent instance. Replaces existing agent with same id.
|
|
44
|
+
*/
|
|
45
|
+
register(agent: BaseAgent<any>): void {
|
|
46
|
+
this.agents.set(agent.id, agent);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get an agent by its id.
|
|
51
|
+
*/
|
|
52
|
+
get(id: string): BaseAgent<any> | undefined {
|
|
53
|
+
return this.agents.get(id);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Unregister an agent by id.
|
|
58
|
+
*/
|
|
59
|
+
unregister(id: string): boolean {
|
|
60
|
+
return this.agents.delete(id);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* List all registered agents.
|
|
65
|
+
*/
|
|
66
|
+
values(): IterableIterator<BaseAgent<any>> {
|
|
67
|
+
return this.agents.values();
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/kernl/types.ts
CHANGED
|
@@ -89,7 +89,7 @@ export interface KernlOptions {
|
|
|
89
89
|
*
|
|
90
90
|
* Satisfied by Map<string, BaseAgent>.
|
|
91
91
|
*/
|
|
92
|
-
export interface
|
|
92
|
+
export interface IAgentRegistry {
|
|
93
93
|
get(id: string): BaseAgent<any> | undefined;
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -102,6 +102,6 @@ export interface AgentRegistry {
|
|
|
102
102
|
* TODO: Create an exhaustive model registry in the protocol package
|
|
103
103
|
* with all supported models and their metadata.
|
|
104
104
|
*/
|
|
105
|
-
export interface
|
|
105
|
+
export interface IModelRegistry {
|
|
106
106
|
get(key: string): LanguageModel | undefined;
|
|
107
107
|
}
|
package/src/storage/base.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Core storage contracts.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type {
|
|
5
|
+
import type { IAgentRegistry, IModelRegistry } from "@/kernl/types";
|
|
6
6
|
import type { ThreadStore } from "./thread";
|
|
7
7
|
import type { MemoryStore } from "@/memory/store";
|
|
8
8
|
|
|
@@ -30,7 +30,7 @@ export interface KernlStorage {
|
|
|
30
30
|
*
|
|
31
31
|
* Called by Kernl after construction to wire up agent/model lookups.
|
|
32
32
|
*/
|
|
33
|
-
bind(registries: { agents:
|
|
33
|
+
bind(registries: { agents: IAgentRegistry; models: IModelRegistry }): void;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Execute a function within a transaction.
|
package/src/storage/in-memory.ts
CHANGED
|
@@ -25,7 +25,7 @@ import type {
|
|
|
25
25
|
SortOrder,
|
|
26
26
|
} from "@/storage";
|
|
27
27
|
import type { ThreadEvent, ThreadState } from "@/thread/types";
|
|
28
|
-
import type {
|
|
28
|
+
import type { IAgentRegistry, IModelRegistry } from "@/kernl/types";
|
|
29
29
|
import type {
|
|
30
30
|
MemoryStore,
|
|
31
31
|
MemoryRecord,
|
|
@@ -47,7 +47,7 @@ export class InMemoryStorage implements KernlStorage {
|
|
|
47
47
|
this.memories = new InMemoryMemoryStore();
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
bind(registries: { agents:
|
|
50
|
+
bind(registries: { agents: IAgentRegistry; models: IModelRegistry }): void {
|
|
51
51
|
this.threads.bind(registries);
|
|
52
52
|
}
|
|
53
53
|
|
|
@@ -93,10 +93,10 @@ interface ThreadData {
|
|
|
93
93
|
export class InMemoryThreadStore implements ThreadStore {
|
|
94
94
|
private threads = new Map<string, ThreadData>();
|
|
95
95
|
private events = new Map<string, ThreadEvent[]>(); // tid -> events
|
|
96
|
-
private registries: { agents:
|
|
96
|
+
private registries: { agents: IAgentRegistry; models: IModelRegistry } | null =
|
|
97
97
|
null;
|
|
98
98
|
|
|
99
|
-
bind(registries: { agents:
|
|
99
|
+
bind(registries: { agents: IAgentRegistry; models: IModelRegistry }): void {
|
|
100
100
|
this.registries = registries;
|
|
101
101
|
}
|
|
102
102
|
|
|
@@ -40,13 +40,22 @@ export class MockLanguageModel implements LanguageModel {
|
|
|
40
40
|
];
|
|
41
41
|
|
|
42
42
|
// Return with fake usage stats
|
|
43
|
+
const tokens = this.countTokens(userText);
|
|
43
44
|
return {
|
|
44
45
|
content,
|
|
45
|
-
finishReason: "stop",
|
|
46
|
+
finishReason: { unified: "stop", raw: "stop" },
|
|
46
47
|
usage: {
|
|
47
|
-
inputTokens:
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
inputTokens: {
|
|
49
|
+
total: tokens,
|
|
50
|
+
noCache: tokens,
|
|
51
|
+
cacheRead: undefined,
|
|
52
|
+
cacheWrite: undefined,
|
|
53
|
+
},
|
|
54
|
+
outputTokens: {
|
|
55
|
+
total: tokens,
|
|
56
|
+
text: tokens,
|
|
57
|
+
reasoning: undefined,
|
|
58
|
+
},
|
|
50
59
|
},
|
|
51
60
|
warnings: [],
|
|
52
61
|
};
|
package/src/thread/thread.ts
CHANGED
|
@@ -216,13 +216,13 @@ export class Thread<
|
|
|
216
216
|
for await (const e of this.tick()) {
|
|
217
217
|
if (e.kind === "error") {
|
|
218
218
|
err = e.error;
|
|
219
|
-
logger.error(
|
|
219
|
+
logger.error(err.message); // (TODO): onError callback in options
|
|
220
220
|
}
|
|
221
221
|
// complete items get persisted with seq, deltas are ephemeral
|
|
222
222
|
if (notDelta(e)) {
|
|
223
|
-
const [
|
|
224
|
-
events.push(
|
|
225
|
-
yield
|
|
223
|
+
const [eseq] = this.append(e);
|
|
224
|
+
events.push(eseq);
|
|
225
|
+
yield eseq;
|
|
226
226
|
} else {
|
|
227
227
|
yield e;
|
|
228
228
|
}
|
|
@@ -253,8 +253,8 @@ export class Thread<
|
|
|
253
253
|
|
|
254
254
|
// append + yield action events (sequenced)
|
|
255
255
|
for (const a of actions) {
|
|
256
|
-
const [
|
|
257
|
-
yield
|
|
256
|
+
const [eseq] = this.append(a);
|
|
257
|
+
yield eseq;
|
|
258
258
|
}
|
|
259
259
|
|
|
260
260
|
await this.checkpoint(); /* c3: tick complete */
|
|
@@ -289,7 +289,7 @@ export class Thread<
|
|
|
289
289
|
this.emit("model.call.start", { settings: req.settings ?? {} });
|
|
290
290
|
|
|
291
291
|
let usage: LanguageModelUsage | undefined;
|
|
292
|
-
let finishReason: LanguageModelFinishReason = "
|
|
292
|
+
let finishReason: LanguageModelFinishReason = { unified: "other", raw: undefined };
|
|
293
293
|
|
|
294
294
|
try {
|
|
295
295
|
if (this.model.stream) {
|