@aigne/afs-history 1.1.3 → 1.2.0-beta.10
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/CHANGELOG.md +135 -0
- package/lib/cjs/index.d.ts +38 -13
- package/lib/cjs/index.js +244 -23
- package/lib/cjs/storage/index.d.ts +11 -7
- package/lib/cjs/storage/index.js +69 -23
- package/lib/cjs/storage/migrate.d.ts +1 -1
- package/lib/cjs/storage/migrate.js +15 -5
- package/lib/cjs/storage/migrations/002-add-agent-id.d.ts +2 -0
- package/lib/cjs/storage/migrations/002-add-agent-id.js +13 -0
- package/lib/cjs/storage/migrations/003-add-compact-table.d.ts +2 -0
- package/lib/cjs/storage/migrations/003-add-compact-table.js +23 -0
- package/lib/cjs/storage/migrations/004-add-memory-table.d.ts +2 -0
- package/lib/cjs/storage/migrations/004-add-memory-table.js +23 -0
- package/lib/cjs/storage/migrations/005-add-indexes.d.ts +2 -0
- package/lib/cjs/storage/migrations/005-add-indexes.js +31 -0
- package/lib/cjs/storage/models/compact.d.ts +183 -0
- package/lib/cjs/storage/models/compact.js +27 -0
- package/lib/cjs/storage/models/entries.d.ts +19 -0
- package/lib/cjs/storage/models/entries.js +1 -0
- package/lib/cjs/storage/models/memory.d.ts +182 -0
- package/lib/cjs/storage/models/memory.js +27 -0
- package/lib/cjs/storage/type.d.ts +33 -5
- package/lib/dts/index.d.ts +38 -13
- package/lib/dts/storage/index.d.ts +11 -7
- package/lib/dts/storage/migrate.d.ts +1 -1
- package/lib/dts/storage/migrations/002-add-agent-id.d.ts +2 -0
- package/lib/dts/storage/migrations/003-add-compact-table.d.ts +2 -0
- package/lib/dts/storage/migrations/004-add-memory-table.d.ts +2 -0
- package/lib/dts/storage/migrations/005-add-indexes.d.ts +2 -0
- package/lib/dts/storage/models/compact.d.ts +183 -0
- package/lib/dts/storage/models/entries.d.ts +19 -0
- package/lib/dts/storage/models/memory.d.ts +182 -0
- package/lib/dts/storage/type.d.ts +33 -5
- package/lib/esm/index.d.ts +38 -13
- package/lib/esm/index.js +244 -23
- package/lib/esm/storage/index.d.ts +11 -7
- package/lib/esm/storage/index.js +70 -24
- package/lib/esm/storage/migrate.d.ts +1 -1
- package/lib/esm/storage/migrate.js +15 -5
- package/lib/esm/storage/migrations/002-add-agent-id.d.ts +2 -0
- package/lib/esm/storage/migrations/002-add-agent-id.js +10 -0
- package/lib/esm/storage/migrations/003-add-compact-table.d.ts +2 -0
- package/lib/esm/storage/migrations/003-add-compact-table.js +20 -0
- package/lib/esm/storage/migrations/004-add-memory-table.d.ts +2 -0
- package/lib/esm/storage/migrations/004-add-memory-table.js +20 -0
- package/lib/esm/storage/migrations/005-add-indexes.d.ts +2 -0
- package/lib/esm/storage/migrations/005-add-indexes.js +28 -0
- package/lib/esm/storage/models/compact.d.ts +183 -0
- package/lib/esm/storage/models/compact.js +22 -0
- package/lib/esm/storage/models/entries.d.ts +19 -0
- package/lib/esm/storage/models/entries.js +1 -0
- package/lib/esm/storage/models/memory.d.ts +182 -0
- package/lib/esm/storage/models/memory.js +22 -0
- package/lib/esm/storage/type.d.ts +33 -5
- package/package.json +5 -3
package/lib/esm/index.js
CHANGED
|
@@ -1,42 +1,263 @@
|
|
|
1
|
+
import { accessModeSchema, } from "@aigne/afs";
|
|
1
2
|
import { v7 } from "@aigne/uuid";
|
|
3
|
+
import { createRouter } from "radix3";
|
|
2
4
|
import { joinURL } from "ufo";
|
|
5
|
+
import { z } from "zod";
|
|
3
6
|
import { SharedAFSStorage, } from "./storage/index.js";
|
|
4
7
|
export * from "./storage/index.js";
|
|
8
|
+
const sharedAFSStorageOptionsSchema = z.object({
|
|
9
|
+
url: z.string().describe("Database URL for storage").optional(),
|
|
10
|
+
});
|
|
11
|
+
const afsHistoryOptionsSchema = preprocessSchema((v) => {
|
|
12
|
+
if (!v || typeof v !== "object") {
|
|
13
|
+
return v;
|
|
14
|
+
}
|
|
15
|
+
return { ...v, accessMode: v.accessMode || v.access_mode };
|
|
16
|
+
}, z.object({
|
|
17
|
+
storage: sharedAFSStorageOptionsSchema.optional(),
|
|
18
|
+
accessMode: accessModeSchema,
|
|
19
|
+
}));
|
|
5
20
|
export class AFSHistory {
|
|
21
|
+
static schema() {
|
|
22
|
+
return afsHistoryOptionsSchema;
|
|
23
|
+
}
|
|
24
|
+
static async load({ parsed }) {
|
|
25
|
+
const valid = await AFSHistory.schema().parseAsync(parsed);
|
|
26
|
+
return new AFSHistory(valid);
|
|
27
|
+
}
|
|
6
28
|
constructor(options) {
|
|
7
29
|
this.storage =
|
|
8
30
|
options?.storage instanceof SharedAFSStorage
|
|
9
31
|
? options.storage.withModule(this)
|
|
10
32
|
: new SharedAFSStorage(options?.storage).withModule(this);
|
|
33
|
+
this.accessMode = options?.accessMode ?? "readwrite";
|
|
11
34
|
}
|
|
12
|
-
storage;
|
|
13
35
|
name = "history";
|
|
36
|
+
accessMode;
|
|
37
|
+
storage;
|
|
38
|
+
afs;
|
|
39
|
+
router = createRouter({
|
|
40
|
+
routes: {
|
|
41
|
+
"/by-session": { action: "list", type: "history", scope: "session" },
|
|
42
|
+
"/by-session/:sessionId": { action: "list", type: "history", scope: "session" },
|
|
43
|
+
"/by-session/:sessionId/new": { action: "create", type: "history", scope: "session" },
|
|
44
|
+
"/by-session/:sessionId/@metadata/compact": {
|
|
45
|
+
action: "list",
|
|
46
|
+
type: "compact",
|
|
47
|
+
scope: "session",
|
|
48
|
+
},
|
|
49
|
+
"/by-session/:sessionId/@metadata/compact/new": {
|
|
50
|
+
action: "create",
|
|
51
|
+
type: "compact",
|
|
52
|
+
scope: "session",
|
|
53
|
+
},
|
|
54
|
+
"/by-session/:sessionId/@metadata/compact/:compactId": {
|
|
55
|
+
action: "read",
|
|
56
|
+
type: "compact",
|
|
57
|
+
scope: "session",
|
|
58
|
+
},
|
|
59
|
+
"/by-session/:sessionId/@metadata/memory": {
|
|
60
|
+
action: "list",
|
|
61
|
+
type: "memory",
|
|
62
|
+
scope: "session",
|
|
63
|
+
},
|
|
64
|
+
"/by-session/:sessionId/@metadata/memory/new": {
|
|
65
|
+
action: "create",
|
|
66
|
+
type: "memory",
|
|
67
|
+
scope: "session",
|
|
68
|
+
},
|
|
69
|
+
"/by-session/:sessionId/@metadata/memory/:memoryId": {
|
|
70
|
+
action: "read",
|
|
71
|
+
type: "memory",
|
|
72
|
+
scope: "session",
|
|
73
|
+
},
|
|
74
|
+
"/by-session/:sessionId/:entryId": { action: "read", type: "history", scope: "session" },
|
|
75
|
+
"/by-user": { action: "list", type: "history", scope: "user" },
|
|
76
|
+
"/by-user/:userId": { action: "list", type: "history", scope: "user" },
|
|
77
|
+
"/by-user/:userId/new": { action: "create", type: "history", scope: "user" },
|
|
78
|
+
"/by-user/:userId/@metadata/compact": { action: "list", type: "compact", scope: "user" },
|
|
79
|
+
"/by-user/:userId/@metadata/compact/new": {
|
|
80
|
+
action: "create",
|
|
81
|
+
type: "compact",
|
|
82
|
+
scope: "user",
|
|
83
|
+
},
|
|
84
|
+
"/by-user/:userId/@metadata/compact/:compactId": {
|
|
85
|
+
action: "read",
|
|
86
|
+
type: "compact",
|
|
87
|
+
scope: "user",
|
|
88
|
+
},
|
|
89
|
+
"/by-user/:userId/@metadata/memory": { action: "list", type: "memory", scope: "user" },
|
|
90
|
+
"/by-user/:userId/@metadata/memory/new": { action: "create", type: "memory", scope: "user" },
|
|
91
|
+
"/by-user/:userId/@metadata/memory/:memoryId": {
|
|
92
|
+
action: "read",
|
|
93
|
+
type: "memory",
|
|
94
|
+
scope: "user",
|
|
95
|
+
},
|
|
96
|
+
"/by-user/:userId/:entryId": { action: "read", type: "history", scope: "user" },
|
|
97
|
+
"/by-agent": { action: "list", type: "history", scope: "agent" },
|
|
98
|
+
"/by-agent/:agentId": { action: "list", type: "history", scope: "agent" },
|
|
99
|
+
"/by-agent/:agentId/new": { action: "create", type: "history", scope: "agent" },
|
|
100
|
+
"/by-agent/:agentId/@metadata/compact": { action: "list", type: "compact", scope: "agent" },
|
|
101
|
+
"/by-agent/:agentId/@metadata/compact/new": {
|
|
102
|
+
action: "create",
|
|
103
|
+
type: "compact",
|
|
104
|
+
scope: "agent",
|
|
105
|
+
},
|
|
106
|
+
"/by-agent/:agentId/@metadata/compact/:compactId": {
|
|
107
|
+
action: "read",
|
|
108
|
+
type: "compact",
|
|
109
|
+
scope: "agent",
|
|
110
|
+
},
|
|
111
|
+
"/by-agent/:agentId/:entryId": { action: "read", type: "history", scope: "agent" },
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
rootEntries = [
|
|
115
|
+
{
|
|
116
|
+
id: "by-session",
|
|
117
|
+
path: "/by-session",
|
|
118
|
+
description: "Retrieve history entries by session ID.",
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: "by-user",
|
|
122
|
+
path: "/by-user",
|
|
123
|
+
description: "Retrieve history entries by user ID.",
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
id: "by-agent",
|
|
127
|
+
path: "/by-agent",
|
|
128
|
+
description: "Retrieve history entries by agent ID.",
|
|
129
|
+
},
|
|
130
|
+
];
|
|
14
131
|
onMount(afs) {
|
|
15
|
-
afs
|
|
16
|
-
this.storage
|
|
17
|
-
.create({
|
|
18
|
-
path: joinURL("/", v7()),
|
|
19
|
-
content: { input, output },
|
|
20
|
-
})
|
|
21
|
-
.then((entry) => {
|
|
22
|
-
afs.emit("historyCreated", { entry });
|
|
23
|
-
})
|
|
24
|
-
.catch((error) => {
|
|
25
|
-
console.error("Failed to store history entry", error);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
132
|
+
this.afs = afs;
|
|
28
133
|
}
|
|
29
134
|
async list(path, options) {
|
|
30
|
-
if (path
|
|
31
|
-
return {
|
|
32
|
-
|
|
135
|
+
if (path === "/")
|
|
136
|
+
return { data: this.rootEntries };
|
|
137
|
+
const match = this.router.lookup(path);
|
|
138
|
+
if (!match || match.action !== "list") {
|
|
139
|
+
return { data: [] };
|
|
140
|
+
}
|
|
141
|
+
const { type, scope } = match;
|
|
142
|
+
const mergedFilter = {
|
|
143
|
+
...options?.filter,
|
|
144
|
+
...match.params,
|
|
145
|
+
};
|
|
146
|
+
const result = await this.storage.list({
|
|
147
|
+
...options,
|
|
148
|
+
type,
|
|
149
|
+
scope,
|
|
150
|
+
filter: mergedFilter,
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
...result,
|
|
154
|
+
data: result.data.map((entry) => ({
|
|
155
|
+
...entry,
|
|
156
|
+
path: this.normalizePath(entry, scope, type),
|
|
157
|
+
})),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
async read(path, options) {
|
|
161
|
+
const match = this.router.lookup(path);
|
|
162
|
+
if (!match || match.action !== "read")
|
|
163
|
+
return {};
|
|
164
|
+
const { type, scope } = match;
|
|
165
|
+
const entryId = match.params?.entryId ?? match.params?.compactId ?? match.params?.memoryId;
|
|
166
|
+
if (!entryId)
|
|
167
|
+
throw new Error(`Entry ID is required in the path to read ${type}.`);
|
|
168
|
+
const mergedFilter = {
|
|
169
|
+
...options?.filter,
|
|
170
|
+
...match.params,
|
|
171
|
+
};
|
|
172
|
+
const data = await this.storage.read(entryId, {
|
|
173
|
+
type,
|
|
174
|
+
scope,
|
|
175
|
+
filter: mergedFilter,
|
|
176
|
+
});
|
|
177
|
+
// Add virtual path prefix for entries
|
|
178
|
+
return {
|
|
179
|
+
data: data && {
|
|
180
|
+
...data,
|
|
181
|
+
path: this.normalizePath(data, scope, type),
|
|
182
|
+
},
|
|
183
|
+
};
|
|
33
184
|
}
|
|
34
|
-
async
|
|
35
|
-
const
|
|
36
|
-
|
|
185
|
+
async write(path, content, options) {
|
|
186
|
+
const id = v7();
|
|
187
|
+
const match = this.router.lookup(path);
|
|
188
|
+
if (!match || match.action !== "create") {
|
|
189
|
+
throw new Error("Can only write to paths with 'new' suffix: /by-{scope}/{scopeId}/new or /by-{scope}/{scopeId}/@metadata/{type}/new");
|
|
190
|
+
}
|
|
191
|
+
const { type, scope } = match;
|
|
192
|
+
// Validate that scope ID is provided in path params
|
|
193
|
+
const scopeIdField = `${scope}Id`;
|
|
194
|
+
const scopeIdValue = match.params?.[scopeIdField];
|
|
195
|
+
if (!scopeIdValue) {
|
|
196
|
+
throw new Error(`${scopeIdField} is required in the path to create ${type} entry.`);
|
|
197
|
+
}
|
|
198
|
+
const entry = await this.storage.create({
|
|
199
|
+
...match.params,
|
|
200
|
+
...content,
|
|
201
|
+
id,
|
|
202
|
+
path: joinURL("/", type, id),
|
|
203
|
+
}, { type, scope });
|
|
204
|
+
// Emit event for history entries
|
|
205
|
+
if (type === "history") {
|
|
206
|
+
this.afs?.emit("historyCreated", { entry }, options);
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
data: {
|
|
210
|
+
...entry,
|
|
211
|
+
path: this.normalizePath(entry, scope, type),
|
|
212
|
+
},
|
|
213
|
+
};
|
|
37
214
|
}
|
|
38
|
-
async
|
|
39
|
-
const
|
|
40
|
-
|
|
215
|
+
async delete(path, _options) {
|
|
216
|
+
const match = this.router.lookup(path);
|
|
217
|
+
if (!match || match.action !== "read") {
|
|
218
|
+
throw new Error(`Cannot delete: path not found or not a valid entry path`);
|
|
219
|
+
}
|
|
220
|
+
const { type, scope } = match;
|
|
221
|
+
const entryId = match.params?.entryId ?? match.params?.compactId ?? match.params?.memoryId;
|
|
222
|
+
if (!entryId)
|
|
223
|
+
throw new Error(`Entry ID is required in the path to delete ${type}.`);
|
|
224
|
+
const result = await this.storage.delete(entryId, {
|
|
225
|
+
type,
|
|
226
|
+
scope,
|
|
227
|
+
filter: match.params,
|
|
228
|
+
});
|
|
229
|
+
if (result.deletedCount === 0) {
|
|
230
|
+
return {
|
|
231
|
+
message: `No ${type} entry found with id ${entryId}`,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
message: `Deleted ${result.deletedCount} ${type} entry`,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
normalizePath(entry, scope, entryType) {
|
|
239
|
+
const scopeIdMap = {
|
|
240
|
+
session: entry.sessionId,
|
|
241
|
+
user: entry.userId,
|
|
242
|
+
agent: entry.agentId,
|
|
243
|
+
};
|
|
244
|
+
const scopeId = scopeIdMap[scope];
|
|
245
|
+
if (!scopeId) {
|
|
246
|
+
throw new Error(`Cannot reset path for entry without ${scope} info.`);
|
|
247
|
+
}
|
|
248
|
+
const prefix = `by-${scope}`;
|
|
249
|
+
// Build path based on entry type
|
|
250
|
+
if (entryType === "compact") {
|
|
251
|
+
return joinURL("/", prefix, scopeId, "@metadata/compact", entry.id);
|
|
252
|
+
}
|
|
253
|
+
if (entryType === "memory") {
|
|
254
|
+
return joinURL("/", prefix, scopeId, "@metadata/memory", entry.id);
|
|
255
|
+
}
|
|
256
|
+
// Default: history entry
|
|
257
|
+
return joinURL("/", prefix, scopeId, entry.id);
|
|
41
258
|
}
|
|
42
259
|
}
|
|
260
|
+
const _typeCheck = AFSHistory;
|
|
261
|
+
function preprocessSchema(fn, schema) {
|
|
262
|
+
return z.preprocess(fn, schema);
|
|
263
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { AFSEntry, AFSModule } from "@aigne/afs";
|
|
2
2
|
import { initDatabase } from "@aigne/sqlite";
|
|
3
|
-
import {
|
|
4
|
-
import type { AFSStorage, AFSStorageCreatePayload, AFSStorageListOptions } from "./type.js";
|
|
3
|
+
import type { AFSStorage, AFSStorageCreateOptions, AFSStorageCreatePayload, AFSStorageDeleteOptions, AFSStorageListOptions, AFSStorageReadOptions } from "./type.js";
|
|
5
4
|
export * from "./type.js";
|
|
6
5
|
export interface SharedAFSStorageOptions {
|
|
7
6
|
url?: string;
|
|
@@ -17,11 +16,16 @@ export declare class SharedAFSStorage {
|
|
|
17
16
|
}
|
|
18
17
|
export declare class AFSStorageWithModule implements AFSStorage {
|
|
19
18
|
private db;
|
|
20
|
-
private
|
|
21
|
-
constructor(db: ReturnType<typeof initDatabase>,
|
|
19
|
+
private module;
|
|
20
|
+
constructor(db: ReturnType<typeof initDatabase>, module: AFSModule);
|
|
21
|
+
private _tables?;
|
|
22
|
+
private get tables();
|
|
22
23
|
list(options?: AFSStorageListOptions): Promise<{
|
|
23
|
-
|
|
24
|
+
data: AFSEntry[];
|
|
25
|
+
}>;
|
|
26
|
+
read(id: string, options?: AFSStorageReadOptions): Promise<AFSEntry | undefined>;
|
|
27
|
+
create(entry: AFSStorageCreatePayload, options?: AFSStorageCreateOptions): Promise<AFSEntry>;
|
|
28
|
+
delete(id: string, options?: AFSStorageDeleteOptions): Promise<{
|
|
29
|
+
deletedCount: number;
|
|
24
30
|
}>;
|
|
25
|
-
read(path: string): Promise<AFSEntry | undefined>;
|
|
26
|
-
create(entry: AFSStorageCreatePayload): Promise<AFSEntry>;
|
|
27
31
|
}
|
package/lib/esm/storage/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { and, asc, desc, eq, initDatabase, sql } from "@aigne/sqlite";
|
|
1
|
+
import { and, asc, desc, eq, gt, initDatabase, lt, sql } from "@aigne/sqlite";
|
|
2
2
|
import { migrate } from "./migrate.js";
|
|
3
|
+
import { compactTable } from "./models/compact.js";
|
|
3
4
|
import { entriesTable } from "./models/entries.js";
|
|
5
|
+
import { memoryTable } from "./models/memory.js";
|
|
4
6
|
export * from "./type.js";
|
|
5
7
|
const DEFAULT_AFS_STORAGE_LIST_LIMIT = 10;
|
|
6
8
|
export class SharedAFSStorage {
|
|
@@ -14,55 +16,99 @@ export class SharedAFSStorage {
|
|
|
14
16
|
return this._db;
|
|
15
17
|
}
|
|
16
18
|
withModule(module) {
|
|
17
|
-
return new AFSStorageWithModule(this.db,
|
|
19
|
+
return new AFSStorageWithModule(this.db, module);
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
export class AFSStorageWithModule {
|
|
21
23
|
db;
|
|
22
|
-
|
|
23
|
-
constructor(db,
|
|
24
|
+
module;
|
|
25
|
+
constructor(db, module) {
|
|
24
26
|
this.db = db;
|
|
25
|
-
this.
|
|
27
|
+
this.module = module;
|
|
28
|
+
}
|
|
29
|
+
_tables;
|
|
30
|
+
get tables() {
|
|
31
|
+
if (!this._tables) {
|
|
32
|
+
this._tables = this.db.then((db) => migrate(db, this.module).then(() => ({
|
|
33
|
+
db,
|
|
34
|
+
entries: entriesTable(this.module),
|
|
35
|
+
compact: compactTable(this.module),
|
|
36
|
+
memory: memoryTable(this.module),
|
|
37
|
+
})));
|
|
38
|
+
}
|
|
39
|
+
return this._tables;
|
|
26
40
|
}
|
|
27
41
|
async list(options = {}) {
|
|
28
|
-
const { filter, limit = DEFAULT_AFS_STORAGE_LIST_LIMIT } = options;
|
|
29
|
-
const db = await this.
|
|
30
|
-
|
|
31
|
-
const
|
|
42
|
+
const { type = "history", scope, filter, limit = DEFAULT_AFS_STORAGE_LIST_LIMIT } = options;
|
|
43
|
+
const { db, entries, compact, memory } = await this.tables;
|
|
44
|
+
// Select table based on type
|
|
45
|
+
const table = type === "compact" ? compact : type === "memory" ? memory : entries;
|
|
46
|
+
const data = await db
|
|
32
47
|
.select()
|
|
33
48
|
.from(table)
|
|
34
|
-
.where(and(
|
|
35
|
-
|
|
49
|
+
.where(and(
|
|
50
|
+
// Add scope filter for compact/memory tables
|
|
51
|
+
scope && type !== "history"
|
|
52
|
+
? eq(sql `json_extract(${table.metadata}, '$.scope')`, scope)
|
|
53
|
+
: undefined, filter?.agentId ? eq(table.agentId, filter.agentId) : undefined, filter?.userId ? eq(table.userId, filter.userId) : undefined, filter?.sessionId ? eq(table.sessionId, filter.sessionId) : undefined, filter?.before ? lt(table.createdAt, new Date(filter.before)) : undefined, filter?.after ? gt(table.createdAt, new Date(filter.after)) : undefined))
|
|
54
|
+
.orderBy(...(options.orderBy ?? [["createdAt", "desc"]]).map((item) => (item[1] === "asc" ? asc : desc)(sql.identifier(item[0]))))
|
|
36
55
|
.limit(limit)
|
|
37
56
|
.execute();
|
|
38
|
-
return {
|
|
57
|
+
return { data };
|
|
39
58
|
}
|
|
40
|
-
async read(
|
|
41
|
-
const
|
|
42
|
-
const
|
|
59
|
+
async read(id, options) {
|
|
60
|
+
const { type = "history", scope, filter } = options ?? {};
|
|
61
|
+
const { db, entries, compact, memory } = await this.tables;
|
|
62
|
+
// Select table based on type
|
|
63
|
+
const table = type === "compact" ? compact : type === "memory" ? memory : entries;
|
|
43
64
|
return db
|
|
44
65
|
.select()
|
|
45
66
|
.from(table)
|
|
46
|
-
.where(eq(table.
|
|
67
|
+
.where(and(eq(table.id, id),
|
|
68
|
+
// Add scope filter for compact/memory tables
|
|
69
|
+
scope && type !== "history"
|
|
70
|
+
? eq(sql `json_extract(${table.metadata}, '$.scope')`, scope)
|
|
71
|
+
: undefined, filter?.agentId ? eq(table.agentId, filter.agentId) : undefined, filter?.userId ? eq(table.userId, filter.userId) : undefined, filter?.sessionId ? eq(table.sessionId, filter.sessionId) : undefined))
|
|
47
72
|
.limit(1)
|
|
48
73
|
.execute()
|
|
49
|
-
.then((
|
|
74
|
+
.then((rows) => rows.at(0));
|
|
50
75
|
}
|
|
51
|
-
async create(entry) {
|
|
52
|
-
const
|
|
53
|
-
const
|
|
76
|
+
async create(entry, options) {
|
|
77
|
+
const { type = "history", scope } = options ?? {};
|
|
78
|
+
const { db, entries, compact, memory } = await this.tables;
|
|
79
|
+
// Select table based on type
|
|
80
|
+
const table = type === "compact" ? compact : type === "memory" ? memory : entries;
|
|
81
|
+
// Prepare entry data - only add scope to metadata for compact/memory types
|
|
82
|
+
const entryData = { ...entry, metadata: { ...entry.metadata, scope } };
|
|
83
|
+
// Try update first, then insert if not found (upsert)
|
|
54
84
|
let result = await db
|
|
55
85
|
.update(table)
|
|
56
|
-
.set(
|
|
57
|
-
.where(eq(table.
|
|
86
|
+
.set(entryData)
|
|
87
|
+
.where(eq(table.id, entry.id))
|
|
58
88
|
.returning()
|
|
59
89
|
.execute();
|
|
60
90
|
if (!result.length) {
|
|
61
|
-
result = await db.insert(table).values(
|
|
91
|
+
result = await db.insert(table).values(entryData).returning().execute();
|
|
62
92
|
}
|
|
63
93
|
const [res] = result;
|
|
64
94
|
if (!res)
|
|
65
|
-
throw new Error(
|
|
95
|
+
throw new Error(`Failed to create ${type} entry, no result`);
|
|
66
96
|
return res;
|
|
67
97
|
}
|
|
98
|
+
async delete(id, options) {
|
|
99
|
+
const { type = "history", scope, filter } = options ?? {};
|
|
100
|
+
const { db, entries, compact, memory } = await this.tables;
|
|
101
|
+
// Select table based on type
|
|
102
|
+
const table = type === "compact" ? compact : type === "memory" ? memory : entries;
|
|
103
|
+
const result = await db
|
|
104
|
+
.delete(table)
|
|
105
|
+
.where(and(eq(table.id, id),
|
|
106
|
+
// Add scope filter for compact/memory tables
|
|
107
|
+
scope && type !== "history"
|
|
108
|
+
? eq(sql `json_extract(${table.metadata}, '$.scope')`, scope)
|
|
109
|
+
: undefined, filter?.agentId ? eq(table.agentId, filter.agentId) : undefined, filter?.userId ? eq(table.userId, filter.userId) : undefined, filter?.sessionId ? eq(table.sessionId, filter.sessionId) : undefined))
|
|
110
|
+
.returning()
|
|
111
|
+
.execute();
|
|
112
|
+
return { deletedCount: result.length };
|
|
113
|
+
}
|
|
68
114
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { AFSModule } from "@aigne/afs";
|
|
2
2
|
import { type initDatabase } from "@aigne/sqlite";
|
|
3
|
-
export declare function migrate(db: ReturnType<typeof initDatabase
|
|
3
|
+
export declare function migrate(db: Awaited<ReturnType<typeof initDatabase>>, module: AFSModule): Promise<void>;
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { sql } from "@aigne/sqlite";
|
|
2
2
|
import { v7 } from "@aigne/uuid";
|
|
3
3
|
import { init } from "./migrations/001-init.js";
|
|
4
|
+
import { addAgentId } from "./migrations/002-add-agent-id.js";
|
|
5
|
+
import { addCompactTable } from "./migrations/003-add-compact-table.js";
|
|
6
|
+
import { addMemoryTable } from "./migrations/004-add-memory-table.js";
|
|
7
|
+
import { addIndexes } from "./migrations/005-add-indexes.js";
|
|
4
8
|
export async function migrate(db, module) {
|
|
5
|
-
const migrations = [
|
|
9
|
+
const migrations = [
|
|
10
|
+
init,
|
|
11
|
+
addAgentId,
|
|
12
|
+
addCompactTable,
|
|
13
|
+
addMemoryTable,
|
|
14
|
+
addIndexes,
|
|
15
|
+
];
|
|
6
16
|
const migrationsTable = "__drizzle_migrations";
|
|
7
17
|
const migrationTableCreate = sql `
|
|
8
18
|
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
|
|
@@ -11,18 +21,18 @@ export async function migrate(db, module) {
|
|
|
11
21
|
"hash" TEXT NOT NULL
|
|
12
22
|
)
|
|
13
23
|
`;
|
|
14
|
-
await
|
|
15
|
-
const dbMigrations = await
|
|
24
|
+
await db.run(migrationTableCreate).execute();
|
|
25
|
+
const dbMigrations = await db
|
|
16
26
|
.values(sql `SELECT "id", "moduleId", "hash" FROM ${sql.identifier(migrationsTable)} WHERE "moduleId" = ${sql.param(module.name)} ORDER BY id DESC LIMIT 1`)
|
|
17
27
|
.execute();
|
|
18
28
|
const lastDbMigration = dbMigrations[0];
|
|
19
29
|
const queriesToRun = [];
|
|
20
30
|
for (const migration of migrations) {
|
|
21
|
-
if (!lastDbMigration || lastDbMigration[
|
|
31
|
+
if (!lastDbMigration || lastDbMigration[2] < migration.hash) {
|
|
22
32
|
queriesToRun.push(...migration.sql(module), sql `INSERT INTO ${sql.identifier(migrationsTable)} ("id", "moduleId", "hash") VALUES(${sql.param(v7())}, ${sql.param(module.name)}, ${sql.param(migration.hash)})`);
|
|
23
33
|
}
|
|
24
34
|
}
|
|
25
35
|
for (const query of queriesToRun) {
|
|
26
|
-
await
|
|
36
|
+
await db.run(query).execute();
|
|
27
37
|
}
|
|
28
38
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { sql } from "@aigne/sqlite";
|
|
2
|
+
import { entriesTableName } from "../models/entries.js";
|
|
3
|
+
export const addAgentId = {
|
|
4
|
+
hash: "002-add-agent-id",
|
|
5
|
+
sql: (module) => [
|
|
6
|
+
sql `\
|
|
7
|
+
ALTER TABLE ${sql.identifier(entriesTableName(module))} ADD COLUMN "agentId" TEXT;
|
|
8
|
+
`,
|
|
9
|
+
],
|
|
10
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { sql } from "@aigne/sqlite";
|
|
2
|
+
import { compactTableName } from "../models/compact.js";
|
|
3
|
+
export const addCompactTable = {
|
|
4
|
+
hash: "003-add-compact-table",
|
|
5
|
+
sql: (module) => [
|
|
6
|
+
sql `\
|
|
7
|
+
CREATE TABLE IF NOT EXISTS ${sql.identifier(compactTableName(module))} (
|
|
8
|
+
"id" TEXT NOT NULL PRIMARY KEY,
|
|
9
|
+
"createdAt" DATETIME NOT NULL,
|
|
10
|
+
"updatedAt" DATETIME NOT NULL,
|
|
11
|
+
"path" TEXT NOT NULL,
|
|
12
|
+
"sessionId" TEXT,
|
|
13
|
+
"userId" TEXT,
|
|
14
|
+
"agentId" TEXT,
|
|
15
|
+
"metadata" JSON,
|
|
16
|
+
"content" JSON
|
|
17
|
+
)
|
|
18
|
+
`,
|
|
19
|
+
],
|
|
20
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { sql } from "@aigne/sqlite";
|
|
2
|
+
import { memoryTableName } from "../models/memory.js";
|
|
3
|
+
export const addMemoryTable = {
|
|
4
|
+
hash: "004-add-memory-table",
|
|
5
|
+
sql: (module) => [
|
|
6
|
+
sql `\
|
|
7
|
+
CREATE TABLE IF NOT EXISTS ${sql.identifier(memoryTableName(module))} (
|
|
8
|
+
"id" TEXT NOT NULL PRIMARY KEY,
|
|
9
|
+
"createdAt" DATETIME NOT NULL,
|
|
10
|
+
"updatedAt" DATETIME NOT NULL,
|
|
11
|
+
"path" TEXT NOT NULL,
|
|
12
|
+
"sessionId" TEXT,
|
|
13
|
+
"userId" TEXT,
|
|
14
|
+
"agentId" TEXT,
|
|
15
|
+
"metadata" JSON,
|
|
16
|
+
"content" JSON
|
|
17
|
+
)
|
|
18
|
+
`,
|
|
19
|
+
],
|
|
20
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { sql } from "@aigne/sqlite";
|
|
2
|
+
import { compactTableName } from "../models/compact.js";
|
|
3
|
+
import { entriesTableName } from "../models/entries.js";
|
|
4
|
+
import { memoryTableName } from "../models/memory.js";
|
|
5
|
+
export const addIndexes = {
|
|
6
|
+
hash: "005-add-indexes",
|
|
7
|
+
sql: (module) => {
|
|
8
|
+
const entriesTable = entriesTableName(module);
|
|
9
|
+
const memoryTable = memoryTableName(module);
|
|
10
|
+
const compactTable = compactTableName(module);
|
|
11
|
+
return [
|
|
12
|
+
// Entries table indexes
|
|
13
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_entries_session_created" ON ${sql.identifier(entriesTable)} ("sessionId", "createdAt" DESC)`,
|
|
14
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_entries_user_created" ON ${sql.identifier(entriesTable)} ("userId", "createdAt" DESC)`,
|
|
15
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_entries_agent_created" ON ${sql.identifier(entriesTable)} ("agentId", "createdAt" DESC)`,
|
|
16
|
+
// Memory table indexes
|
|
17
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_memory_session_created" ON ${sql.identifier(memoryTable)} ("sessionId", "createdAt" DESC)`,
|
|
18
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_memory_user_created" ON ${sql.identifier(memoryTable)} ("userId", "createdAt" DESC)`,
|
|
19
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_memory_agent_created" ON ${sql.identifier(memoryTable)} ("agentId", "createdAt" DESC)`,
|
|
20
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_memory_scope" ON ${sql.identifier(memoryTable)} (json_extract("metadata", '$.scope'))`,
|
|
21
|
+
// Compact table indexes
|
|
22
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_compact_session_created" ON ${sql.identifier(compactTable)} ("sessionId", "createdAt" DESC)`,
|
|
23
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_compact_user_created" ON ${sql.identifier(compactTable)} ("userId", "createdAt" DESC)`,
|
|
24
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_compact_agent_created" ON ${sql.identifier(compactTable)} ("agentId", "createdAt" DESC)`,
|
|
25
|
+
sql `CREATE INDEX IF NOT EXISTS "idx_compact_scope" ON ${sql.identifier(compactTable)} (json_extract("metadata", '$.scope'))`,
|
|
26
|
+
];
|
|
27
|
+
},
|
|
28
|
+
};
|