kernl 0.6.1 → 0.6.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 +4 -5
- package/.turbo/turbo-check-types.log +1 -1
- package/CHANGELOG.md +36 -0
- package/dist/agent/__tests__/concurrency.test.js +1 -1
- package/dist/agent/__tests__/run.test.js +1 -1
- package/dist/{types/agent.d.ts → agent/types.d.ts} +2 -2
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent.d.ts +36 -4
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +58 -0
- package/dist/api/models/thread.d.ts +1 -1
- 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 +4 -4
- package/dist/context.d.ts.map +1 -1
- 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 +7 -18
- package/dist/kernl/kernl.d.ts.map +1 -1
- package/dist/kernl/kernl.js +29 -29
- 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 +51 -0
- package/dist/memory/memory.d.ts.map +1 -0
- package/dist/memory/memory.js +107 -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/{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/package.json +4 -2
- package/src/{types/agent.ts → agent/types.ts} +1 -1
- package/src/agent.ts +78 -2
- package/src/api/__tests__/threads.test.ts +2 -2
- package/src/api/models/thread.ts +1 -1
- 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 +6 -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} +40 -28
- 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 +143 -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 +2 -2
- package/src/{types/thread.ts → thread/types.ts} +1 -1
- package/src/thread/utils.ts +2 -2
- 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 -60
- package/dist/kernl.d.ts.map +0 -1
- package/dist/kernl.js +0 -113
- 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
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory types.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { JSONObject } from "@kernl-sdk/protocol";
|
|
6
|
+
import type { AsyncCodec } from "@kernl-sdk/shared/lib";
|
|
7
|
+
import type { IndexHandle } from "@kernl-sdk/retrieval";
|
|
8
|
+
|
|
9
|
+
import type { MemoryStore } from "./store";
|
|
10
|
+
|
|
11
|
+
// -------------------
|
|
12
|
+
// Byte types
|
|
13
|
+
// -------------------
|
|
14
|
+
|
|
15
|
+
export type TextByte = string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Image content.
|
|
19
|
+
*
|
|
20
|
+
* NOTE: Parallels @kernl-sdk/protocol's FilePart type.
|
|
21
|
+
* @see packages/protocol/src/language-model/item.ts
|
|
22
|
+
*/
|
|
23
|
+
export interface ImageByte {
|
|
24
|
+
data: Uint8Array | string; // raw bytes or base64/URI
|
|
25
|
+
mime: string; // "image/png", "image/jpeg", etc.
|
|
26
|
+
alt?: string; // alt text / description
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Audio content.
|
|
31
|
+
*/
|
|
32
|
+
export interface AudioByte {
|
|
33
|
+
data: Uint8Array | string;
|
|
34
|
+
mime: string; // "audio/wav", "audio/mp3", etc.
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Video content.
|
|
39
|
+
*/
|
|
40
|
+
export interface VideoByte {
|
|
41
|
+
data: Uint8Array | string;
|
|
42
|
+
mime: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Memory content - the smallest coherent unit of memory.
|
|
47
|
+
*
|
|
48
|
+
* May contain multiple modalities (e.g., captioned image, video with transcript).
|
|
49
|
+
* At most one of each modality type.
|
|
50
|
+
*/
|
|
51
|
+
export interface MemoryByte {
|
|
52
|
+
text?: TextByte;
|
|
53
|
+
image?: ImageByte;
|
|
54
|
+
audio?: AudioByte;
|
|
55
|
+
video?: VideoByte;
|
|
56
|
+
object?: JSONObject;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Search-ready projection of a MemoryByte.
|
|
61
|
+
*
|
|
62
|
+
* Contains canonical text plus embeddings for each modality.
|
|
63
|
+
*/
|
|
64
|
+
export interface IndexableByte {
|
|
65
|
+
text?: string; // canonical semantic text
|
|
66
|
+
tvec?: number[]; // text embedding
|
|
67
|
+
ivec?: number[]; // image embedding
|
|
68
|
+
avec?: number[]; // audio embedding
|
|
69
|
+
vvec?: number[]; // video embedding
|
|
70
|
+
metadata?: JSONObject | null; // structured payload
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Encoder that converts MemoryByte to IndexableByte with embeddings.
|
|
75
|
+
*/
|
|
76
|
+
export interface MemoryByteCodec extends AsyncCodec<MemoryByte, IndexableByte> {
|
|
77
|
+
embed(text: string): Promise<number[]>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// -------------------
|
|
81
|
+
// Config & snapshots
|
|
82
|
+
// -------------------
|
|
83
|
+
|
|
84
|
+
export interface MemoryConfig {
|
|
85
|
+
store: MemoryStore;
|
|
86
|
+
search?: IndexHandle<IndexMemoryRecord>;
|
|
87
|
+
// graph?: MemoryGraphIndex;
|
|
88
|
+
// archive?: MemoryArchiveIndex;
|
|
89
|
+
|
|
90
|
+
/** Encoder for converting MemoryByte to IndexableByte with embeddings. */
|
|
91
|
+
encoder: MemoryByteCodec;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Working memory snapshot (L1).
|
|
96
|
+
*/
|
|
97
|
+
export interface WorkingMemorySnapshot {
|
|
98
|
+
scope: MemoryScope;
|
|
99
|
+
records: MemoryRecord[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Short-term memory snapshot (L2).
|
|
104
|
+
*/
|
|
105
|
+
export interface ShortTermMemorySnapshot {
|
|
106
|
+
scope: MemoryScope;
|
|
107
|
+
records: MemoryRecord[];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Memory scope - identifies who/what this memory belongs to.
|
|
112
|
+
*/
|
|
113
|
+
export interface MemoryScope {
|
|
114
|
+
namespace?: string;
|
|
115
|
+
entityId?: string;
|
|
116
|
+
agentId?: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Memory kind - episodic (events/experiences) or semantic (facts/knowledge).
|
|
121
|
+
*/
|
|
122
|
+
export type MemoryKind = "episodic" | "semantic";
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Input for creating a new memory.
|
|
126
|
+
*/
|
|
127
|
+
export interface NewMemory {
|
|
128
|
+
id: string;
|
|
129
|
+
scope: MemoryScope;
|
|
130
|
+
kind: MemoryKind;
|
|
131
|
+
collection: string;
|
|
132
|
+
content: MemoryByte;
|
|
133
|
+
wmem?: boolean;
|
|
134
|
+
smem?: { expiresAt: number | null };
|
|
135
|
+
timestamp?: number;
|
|
136
|
+
metadata?: JSONObject | null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Simplified input for agent-scoped memory creation.
|
|
141
|
+
*
|
|
142
|
+
* Sugar over NewMemory with:
|
|
143
|
+
* - id auto-generated if not provided
|
|
144
|
+
* - scope fields flattened (namespace, entityId) - agentId is implicit
|
|
145
|
+
* - kind defaults to "semantic"
|
|
146
|
+
*/
|
|
147
|
+
export interface AgentMemoryCreate {
|
|
148
|
+
id?: string;
|
|
149
|
+
namespace?: string;
|
|
150
|
+
entityId?: string;
|
|
151
|
+
collection: string;
|
|
152
|
+
content: MemoryByte;
|
|
153
|
+
wmem?: boolean;
|
|
154
|
+
smem?: { expiresAt: number | null };
|
|
155
|
+
timestamp?: number;
|
|
156
|
+
metadata?: JSONObject | null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Base memory record fields.
|
|
161
|
+
*/
|
|
162
|
+
interface BaseMemoryRecord {
|
|
163
|
+
id: string;
|
|
164
|
+
scope: MemoryScope;
|
|
165
|
+
kind: MemoryKind;
|
|
166
|
+
collection: string;
|
|
167
|
+
content: MemoryByte;
|
|
168
|
+
wmem: boolean;
|
|
169
|
+
smem: { expiresAt: number | null };
|
|
170
|
+
timestamp: number;
|
|
171
|
+
createdAt: number;
|
|
172
|
+
updatedAt: number;
|
|
173
|
+
metadata: JSONObject | null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Episodic memory - events, experiences, conversations.
|
|
178
|
+
*/
|
|
179
|
+
export interface EpisodicMemoryRecord extends BaseMemoryRecord {
|
|
180
|
+
kind: "episodic";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Semantic memory - facts, knowledge, learned information.
|
|
185
|
+
*/
|
|
186
|
+
export interface SemanticMemoryRecord extends BaseMemoryRecord {
|
|
187
|
+
kind: "semantic";
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A persisted memory record.
|
|
192
|
+
*/
|
|
193
|
+
export type MemoryRecord = EpisodicMemoryRecord | SemanticMemoryRecord;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Update payload for a memory record.
|
|
197
|
+
*/
|
|
198
|
+
export interface MemoryRecordUpdate {
|
|
199
|
+
id: string;
|
|
200
|
+
scope?: MemoryScope;
|
|
201
|
+
collection?: string;
|
|
202
|
+
content?: MemoryByte;
|
|
203
|
+
wmem?: boolean;
|
|
204
|
+
smem?: { expiresAt: number | null };
|
|
205
|
+
timestamp?: number;
|
|
206
|
+
updatedAt?: number;
|
|
207
|
+
metadata?: JSONObject | null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Filter for listing memories.
|
|
212
|
+
*/
|
|
213
|
+
export interface MemoryFilter {
|
|
214
|
+
scope?: Partial<MemoryScope>;
|
|
215
|
+
collections?: string[];
|
|
216
|
+
wmem?: boolean;
|
|
217
|
+
smem?: boolean;
|
|
218
|
+
after?: number;
|
|
219
|
+
before?: number;
|
|
220
|
+
metadata?: JSONObject;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Options for listing memories.
|
|
225
|
+
*/
|
|
226
|
+
export interface MemoryListOptions {
|
|
227
|
+
filter?: MemoryFilter;
|
|
228
|
+
limit?: number;
|
|
229
|
+
offset?: number;
|
|
230
|
+
order?: "asc" | "desc";
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Query for semantic memory search.
|
|
235
|
+
*/
|
|
236
|
+
export interface MemorySearchQuery {
|
|
237
|
+
query: string;
|
|
238
|
+
filter?: MemoryFilter;
|
|
239
|
+
limit?: number;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Params for triggering reindexing of a memory record.
|
|
244
|
+
*/
|
|
245
|
+
export interface MemoryReindexParams {
|
|
246
|
+
id: string;
|
|
247
|
+
indexes?: ("search" | "graph" | "archive")[];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// -------------------
|
|
251
|
+
// Index types
|
|
252
|
+
// -------------------
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Flat document for search indexes.
|
|
256
|
+
*/
|
|
257
|
+
export interface IndexMemoryRecord extends IndexableByte {
|
|
258
|
+
id: string;
|
|
259
|
+
namespace: string | null;
|
|
260
|
+
entityId: string | null;
|
|
261
|
+
agentId: string | null;
|
|
262
|
+
kind: MemoryKind;
|
|
263
|
+
collection: string;
|
|
264
|
+
timestamp: number;
|
|
265
|
+
createdAt: number;
|
|
266
|
+
updatedAt: number;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Patch payload for updating an IndexMemoryRecord.
|
|
271
|
+
* Content changes require full re-index, not a patch.
|
|
272
|
+
*/
|
|
273
|
+
export interface IndexMemoryRecordPatch {
|
|
274
|
+
id: string;
|
|
275
|
+
namespace?: string | null;
|
|
276
|
+
entityId?: string | null;
|
|
277
|
+
agentId?: string | null;
|
|
278
|
+
collection?: string;
|
|
279
|
+
timestamp?: number;
|
|
280
|
+
updatedAt?: number;
|
|
281
|
+
metadata?: JSONObject | null;
|
|
282
|
+
}
|
|
@@ -5,7 +5,7 @@ import { InMemoryStorage, InMemoryThreadStore } from "../in-memory";
|
|
|
5
5
|
import { Agent } from "@/agent";
|
|
6
6
|
import { Thread } from "@/thread";
|
|
7
7
|
import type { NewThread } from "@/storage";
|
|
8
|
-
import type { ThreadEvent } from "@/types
|
|
8
|
+
import type { ThreadEvent } from "@/thread/types";
|
|
9
9
|
import { createMockModel } from "@/thread/__tests__/fixtures/mock-model";
|
|
10
10
|
|
|
11
11
|
describe("InMemoryThreadStore", () => {
|
package/src/storage/base.ts
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
* Core storage contracts.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type { AgentRegistry, ModelRegistry } from "@/types
|
|
5
|
+
import type { AgentRegistry, ModelRegistry } from "@/kernl/types";
|
|
6
6
|
import type { ThreadStore } from "./thread";
|
|
7
|
+
import type { MemoryStore } from "@/memory/store";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* The main storage interface for Kernl.
|
|
@@ -16,6 +17,11 @@ export interface KernlStorage {
|
|
|
16
17
|
*/
|
|
17
18
|
threads: ThreadStore;
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Memory store - manages memory records for agents.
|
|
22
|
+
*/
|
|
23
|
+
memories: MemoryStore;
|
|
24
|
+
|
|
19
25
|
// tasks: TaskStore;
|
|
20
26
|
// traces: TraceStore;
|
|
21
27
|
|
package/src/storage/in-memory.ts
CHANGED
|
@@ -23,17 +23,27 @@ import type {
|
|
|
23
23
|
ThreadFilter,
|
|
24
24
|
SortOrder,
|
|
25
25
|
} from "@/storage";
|
|
26
|
-
import type { ThreadEvent, ThreadState } from "@/types
|
|
27
|
-
import type { AgentRegistry, ModelRegistry } from "@/types
|
|
26
|
+
import type { ThreadEvent, ThreadState } from "@/thread/types";
|
|
27
|
+
import type { AgentRegistry, ModelRegistry } from "@/kernl/types";
|
|
28
|
+
import type {
|
|
29
|
+
MemoryStore,
|
|
30
|
+
MemoryRecord,
|
|
31
|
+
NewMemory,
|
|
32
|
+
MemoryRecordUpdate,
|
|
33
|
+
MemoryListOptions,
|
|
34
|
+
MemoryFilter,
|
|
35
|
+
} from "@/memory";
|
|
28
36
|
|
|
29
37
|
/**
|
|
30
38
|
* In-memory storage implementation.
|
|
31
39
|
*/
|
|
32
40
|
export class InMemoryStorage implements KernlStorage {
|
|
33
41
|
threads: InMemoryThreadStore;
|
|
42
|
+
memories: InMemoryMemoryStore;
|
|
34
43
|
|
|
35
44
|
constructor() {
|
|
36
45
|
this.threads = new InMemoryThreadStore();
|
|
46
|
+
this.memories = new InMemoryMemoryStore();
|
|
37
47
|
}
|
|
38
48
|
|
|
39
49
|
bind(registries: { agents: AgentRegistry; models: ModelRegistry }): void {
|
|
@@ -370,3 +380,161 @@ export class InMemoryThreadStore implements ThreadStore {
|
|
|
370
380
|
});
|
|
371
381
|
}
|
|
372
382
|
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* In-memory memory store implementation.
|
|
386
|
+
*/
|
|
387
|
+
export class InMemoryMemoryStore implements MemoryStore {
|
|
388
|
+
private memories = new Map<string, MemoryRecord>();
|
|
389
|
+
|
|
390
|
+
async get(id: string): Promise<MemoryRecord | null> {
|
|
391
|
+
return this.memories.get(id) ?? null;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async list(options?: MemoryListOptions): Promise<MemoryRecord[]> {
|
|
395
|
+
let records = Array.from(this.memories.values());
|
|
396
|
+
|
|
397
|
+
// Apply filters
|
|
398
|
+
if (options?.filter) {
|
|
399
|
+
records = this.applyFilters(records, options.filter);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Apply sorting
|
|
403
|
+
records = this.applySorting(records, options?.order);
|
|
404
|
+
|
|
405
|
+
// Apply pagination
|
|
406
|
+
if (options?.offset) records = records.slice(options.offset);
|
|
407
|
+
if (options?.limit) records = records.slice(0, options.limit);
|
|
408
|
+
|
|
409
|
+
return records;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async create(memory: NewMemory): Promise<MemoryRecord> {
|
|
413
|
+
const now = Date.now();
|
|
414
|
+
const record: MemoryRecord = {
|
|
415
|
+
id: memory.id,
|
|
416
|
+
scope: memory.scope,
|
|
417
|
+
kind: memory.kind,
|
|
418
|
+
collection: memory.collection,
|
|
419
|
+
content: memory.content,
|
|
420
|
+
wmem: memory.wmem ?? false,
|
|
421
|
+
smem: memory.smem ?? { expiresAt: null },
|
|
422
|
+
timestamp: memory.timestamp ?? now,
|
|
423
|
+
createdAt: now,
|
|
424
|
+
updatedAt: now,
|
|
425
|
+
metadata: memory.metadata ?? null,
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
this.memories.set(memory.id, record);
|
|
429
|
+
return record;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async update(id: string, patch: MemoryRecordUpdate): Promise<MemoryRecord> {
|
|
433
|
+
const record = this.memories.get(id);
|
|
434
|
+
if (!record) throw new Error(`Memory ${id} not found`);
|
|
435
|
+
|
|
436
|
+
const updated: MemoryRecord = {
|
|
437
|
+
...record,
|
|
438
|
+
...(patch.scope && { scope: patch.scope }),
|
|
439
|
+
...(patch.collection && { collection: patch.collection }),
|
|
440
|
+
...(patch.content && { content: patch.content }),
|
|
441
|
+
...(patch.wmem !== undefined && { wmem: patch.wmem }),
|
|
442
|
+
...(patch.smem && { smem: patch.smem }),
|
|
443
|
+
...(patch.timestamp && { timestamp: patch.timestamp }),
|
|
444
|
+
...(patch.metadata !== undefined && { metadata: patch.metadata }),
|
|
445
|
+
updatedAt: patch.updatedAt ?? Date.now(),
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
this.memories.set(id, updated);
|
|
449
|
+
return updated;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async delete(id: string): Promise<void> {
|
|
453
|
+
this.memories.delete(id);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async mdelete(ids: string[]): Promise<void> {
|
|
457
|
+
for (const id of ids) {
|
|
458
|
+
this.memories.delete(id);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Apply filters to memory list.
|
|
464
|
+
*/
|
|
465
|
+
private applyFilters(
|
|
466
|
+
records: MemoryRecord[],
|
|
467
|
+
filter: MemoryFilter,
|
|
468
|
+
): MemoryRecord[] {
|
|
469
|
+
return records.filter((record) => {
|
|
470
|
+
// Filter by scope
|
|
471
|
+
if (filter.scope) {
|
|
472
|
+
if (
|
|
473
|
+
filter.scope.namespace !== undefined &&
|
|
474
|
+
record.scope.namespace !== filter.scope.namespace
|
|
475
|
+
) {
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
if (
|
|
479
|
+
filter.scope.entityId !== undefined &&
|
|
480
|
+
record.scope.entityId !== filter.scope.entityId
|
|
481
|
+
) {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
if (
|
|
485
|
+
filter.scope.agentId !== undefined &&
|
|
486
|
+
record.scope.agentId !== filter.scope.agentId
|
|
487
|
+
) {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Filter by collections
|
|
493
|
+
if (filter.collections && !filter.collections.includes(record.collection)) {
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Filter by wmem
|
|
498
|
+
if (filter.wmem !== undefined && record.wmem !== filter.wmem) {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Filter by smem
|
|
503
|
+
if (filter.smem !== undefined) {
|
|
504
|
+
const hasSmem = record.smem.expiresAt !== null;
|
|
505
|
+
if (filter.smem !== hasSmem) return false;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Filter by timestamp range
|
|
509
|
+
if (filter.after !== undefined && record.timestamp <= filter.after) {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
if (filter.before !== undefined && record.timestamp >= filter.before) {
|
|
513
|
+
return false;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Filter by metadata (simple equality check for now)
|
|
517
|
+
if (filter.metadata) {
|
|
518
|
+
const recordMeta = record.metadata ?? {};
|
|
519
|
+
for (const [key, value] of Object.entries(filter.metadata)) {
|
|
520
|
+
if (recordMeta[key] !== value) return false;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
return true;
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Apply sorting to memory list.
|
|
530
|
+
*/
|
|
531
|
+
private applySorting(
|
|
532
|
+
records: MemoryRecord[],
|
|
533
|
+
order?: "asc" | "desc",
|
|
534
|
+
): MemoryRecord[] {
|
|
535
|
+
return records.sort((a, b) => {
|
|
536
|
+
const diff = a.timestamp - b.timestamp;
|
|
537
|
+
return order === "desc" ? -diff : diff;
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
}
|
package/src/storage/thread.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { tool, Toolkit } from "@/tool";
|
|
|
9
9
|
|
|
10
10
|
import { Thread } from "../thread";
|
|
11
11
|
|
|
12
|
-
import type { ThreadEvent, ThreadStreamEvent } from "@/types
|
|
12
|
+
import type { ThreadEvent, ThreadStreamEvent } from "@/thread/types";
|
|
13
13
|
import type { LanguageModelItem } from "@kernl-sdk/protocol";
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
LanguageModelResponseItem,
|
|
6
6
|
LanguageModelItem,
|
|
7
7
|
} from "@kernl-sdk/protocol";
|
|
8
|
-
import type { ThreadStreamEvent } from "@/types
|
|
8
|
+
import type { ThreadStreamEvent } from "@/thread/types";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* A mock language model that echoes the user input back as an assistant message.
|
|
@@ -11,7 +11,7 @@ import { Context } from "@/context";
|
|
|
11
11
|
import { tool, FunctionToolkit } from "@/tool";
|
|
12
12
|
import { ModelBehaviorError } from "@/lib/error";
|
|
13
13
|
|
|
14
|
-
import type { ThreadEvent } from "@/types
|
|
14
|
+
import type { ThreadEvent } from "@/thread/types";
|
|
15
15
|
import { createMockModel } from "./fixtures/mock-model";
|
|
16
16
|
|
|
17
17
|
// Helper to create user message input
|
package/src/thread/thread.ts
CHANGED
|
@@ -29,8 +29,8 @@ import type {
|
|
|
29
29
|
ThreadStreamEvent,
|
|
30
30
|
ThreadExecuteResult,
|
|
31
31
|
PerformActionsResult,
|
|
32
|
-
} from "
|
|
33
|
-
import type { AgentResponseType } from "@/types
|
|
32
|
+
} from "./types";
|
|
33
|
+
import type { AgentResponseType } from "@/agent/types";
|
|
34
34
|
|
|
35
35
|
import {
|
|
36
36
|
tevent,
|
|
@@ -15,7 +15,7 @@ import { Task } from "@/task";
|
|
|
15
15
|
import { Context } from "@/context";
|
|
16
16
|
import { Agent } from "@/agent";
|
|
17
17
|
|
|
18
|
-
import type { AgentResponseType } from "
|
|
18
|
+
import type { AgentResponseType } from "@/agent/types";
|
|
19
19
|
import type { ThreadStore } from "@/storage";
|
|
20
20
|
|
|
21
21
|
/**
|
package/src/thread/utils.ts
CHANGED
|
@@ -8,14 +8,14 @@ import { ToolCall, LanguageModelItem } from "@kernl-sdk/protocol";
|
|
|
8
8
|
import { ModelBehaviorError } from "@/lib/error";
|
|
9
9
|
|
|
10
10
|
/* types */
|
|
11
|
-
import type { AgentResponseType } from "@/types
|
|
11
|
+
import type { AgentResponseType } from "@/agent/types";
|
|
12
12
|
import type {
|
|
13
13
|
ThreadEvent,
|
|
14
14
|
ThreadEventBase,
|
|
15
15
|
ThreadStreamEvent,
|
|
16
16
|
ActionSet,
|
|
17
17
|
PublicThreadEvent,
|
|
18
|
-
} from "
|
|
18
|
+
} from "./types";
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Create a ThreadEvent from a LanguageModelItem with thread metadata.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/agent.ts","./src/context.ts","./src/guardrail.ts","./src/index.ts","./src/internal.ts","./src/kernl.ts","./src/lifecycle.ts","./src/task.ts","./src/agent/index.ts","./src/agent/__tests__/concurrency.test.ts","./src/agent/__tests__/run.test.ts","./src/api/__tests__/threads.test.ts","./src/api/models/index.ts","./src/api/models/thread.ts","./src/api/resources/threads/events.ts","./src/api/resources/threads/index.ts","./src/api/resources/threads/threads.ts","./src/api/resources/threads/types.ts","./src/api/resources/threads/utils.ts","./src/lib/env.ts","./src/lib/error.ts","./src/lib/logger.ts","./src/lib/serde/json.ts","./src/mcp/base.ts","./src/mcp/http.ts","./src/mcp/sse.ts","./src/mcp/stdio.ts","./src/mcp/types.ts","./src/mcp/utils.ts","./src/mcp/__tests__/base.test.ts","./src/mcp/__tests__/integration.test.ts","./src/mcp/__tests__/stdio.test.ts","./src/mcp/__tests__/utils.test.ts","./src/mcp/__tests__/fixtures/server.ts","./src/mcp/__tests__/fixtures/utils.ts","./src/memory/byte.ts","./src/memory/index.ts","./src/memory/indexes.ts","./src/memory/memory.ts","./src/memory/store.ts","./src/memory/types.ts","./src/storage/base.ts","./src/storage/in-memory.ts","./src/storage/index.ts","./src/storage/thread.ts","./src/storage/__tests__/in-memory.test.ts","./src/thread/index.ts","./src/thread/thread.ts","./src/thread/utils.ts","./src/thread/__tests__/integration.test.ts","./src/thread/__tests__/mock.ts","./src/thread/__tests__/namespace.test.ts","./src/thread/__tests__/thread-persistence.test.ts","./src/thread/__tests__/thread.test.ts","./src/thread/__tests__/fixtures/mock-model.ts","./src/tool/index.ts","./src/tool/tool.ts","./src/tool/toolkit.ts","./src/tool/types.ts","./src/tool/__tests__/fixtures.ts","./src/tool/__tests__/tool.test.ts","./src/tool/__tests__/toolkit.test.ts","./src/trace/processor.ts","./src/trace/traces.ts","./src/trace/utils.ts","./src/types/agent.ts","./src/types/kernl.ts","./src/types/thread.ts"],"version":"5.9.2"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cursor-page.test.d.ts","sourceRoot":"","sources":["../../../src/api/__tests__/cursor-page.test.ts"],"names":[],"mappings":""}
|