@aigne/afs-history 1.3.0-beta → 1.74.0-beta

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.
Files changed (109) hide show
  1. package/dist/index.cjs +285 -0
  2. package/dist/index.d.cts +48 -0
  3. package/dist/index.d.cts.map +1 -0
  4. package/dist/index.d.mts +48 -0
  5. package/dist/index.d.mts.map +1 -0
  6. package/dist/index.mjs +284 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/dist/storage/index.cjs +64 -0
  9. package/dist/storage/index.d.cts +25 -0
  10. package/dist/storage/index.d.cts.map +1 -0
  11. package/dist/storage/index.d.mts +25 -0
  12. package/dist/storage/index.d.mts.map +1 -0
  13. package/dist/storage/index.mjs +64 -0
  14. package/dist/storage/index.mjs.map +1 -0
  15. package/dist/storage/migrate.cjs +34 -0
  16. package/dist/storage/migrate.mjs +35 -0
  17. package/dist/storage/migrate.mjs.map +1 -0
  18. package/dist/storage/migrations/001-init.cjs +25 -0
  19. package/{lib/esm/storage/migrations/001-init.js → dist/storage/migrations/001-init.mjs} +11 -7
  20. package/dist/storage/migrations/001-init.mjs.map +1 -0
  21. package/dist/storage/migrations/002-add-agent-id.cjs +13 -0
  22. package/dist/storage/migrations/002-add-agent-id.mjs +14 -0
  23. package/dist/storage/migrations/002-add-agent-id.mjs.map +1 -0
  24. package/dist/storage/migrations/003-add-compact-table.cjs +23 -0
  25. package/{lib/esm/storage/migrations/003-add-compact-table.js → dist/storage/migrations/003-add-compact-table.mjs} +11 -7
  26. package/dist/storage/migrations/003-add-compact-table.mjs.map +1 -0
  27. package/dist/storage/migrations/004-add-memory-table.cjs +23 -0
  28. package/{lib/esm/storage/migrations/004-add-memory-table.js → dist/storage/migrations/004-add-memory-table.mjs} +11 -7
  29. package/dist/storage/migrations/004-add-memory-table.mjs.map +1 -0
  30. package/dist/storage/migrations/005-add-indexes.cjs +30 -0
  31. package/dist/storage/migrations/005-add-indexes.mjs +31 -0
  32. package/dist/storage/migrations/005-add-indexes.mjs.map +1 -0
  33. package/dist/storage/models/compact.cjs +20 -0
  34. package/dist/storage/models/compact.mjs +20 -0
  35. package/dist/storage/models/compact.mjs.map +1 -0
  36. package/dist/storage/models/entries.cjs +22 -0
  37. package/dist/storage/models/entries.mjs +22 -0
  38. package/dist/storage/models/entries.mjs.map +1 -0
  39. package/dist/storage/models/memory.cjs +20 -0
  40. package/dist/storage/models/memory.mjs +20 -0
  41. package/dist/storage/models/memory.mjs.map +1 -0
  42. package/dist/storage/type.cjs +8 -0
  43. package/dist/storage/type.d.cts +55 -0
  44. package/dist/storage/type.d.cts.map +1 -0
  45. package/dist/storage/type.d.mts +55 -0
  46. package/dist/storage/type.d.mts.map +1 -0
  47. package/dist/storage/type.mjs +8 -0
  48. package/dist/storage/type.mjs.map +1 -0
  49. package/package.json +32 -41
  50. package/CHANGELOG.md +0 -390
  51. package/lib/cjs/index.d.ts +0 -41
  52. package/lib/cjs/index.js +0 -274
  53. package/lib/cjs/package.json +0 -3
  54. package/lib/cjs/storage/index.d.ts +0 -21
  55. package/lib/cjs/storage/index.js +0 -118
  56. package/lib/cjs/storage/migrate.d.ts +0 -3
  57. package/lib/cjs/storage/migrate.js +0 -41
  58. package/lib/cjs/storage/migrations/001-init.d.ts +0 -2
  59. package/lib/cjs/storage/migrations/001-init.js +0 -25
  60. package/lib/cjs/storage/migrations/002-add-agent-id.d.ts +0 -2
  61. package/lib/cjs/storage/migrations/002-add-agent-id.js +0 -13
  62. package/lib/cjs/storage/migrations/003-add-compact-table.d.ts +0 -2
  63. package/lib/cjs/storage/migrations/003-add-compact-table.js +0 -23
  64. package/lib/cjs/storage/migrations/004-add-memory-table.d.ts +0 -2
  65. package/lib/cjs/storage/migrations/004-add-memory-table.js +0 -23
  66. package/lib/cjs/storage/migrations/005-add-indexes.d.ts +0 -2
  67. package/lib/cjs/storage/migrations/005-add-indexes.js +0 -31
  68. package/lib/cjs/storage/models/compact.d.ts +0 -183
  69. package/lib/cjs/storage/models/compact.js +0 -27
  70. package/lib/cjs/storage/models/entries.d.ts +0 -218
  71. package/lib/cjs/storage/models/entries.js +0 -29
  72. package/lib/cjs/storage/models/memory.d.ts +0 -182
  73. package/lib/cjs/storage/models/memory.js +0 -27
  74. package/lib/cjs/storage/type.d.ts +0 -52
  75. package/lib/cjs/storage/type.js +0 -11
  76. package/lib/dts/index.d.ts +0 -41
  77. package/lib/dts/storage/index.d.ts +0 -21
  78. package/lib/dts/storage/migrate.d.ts +0 -3
  79. package/lib/dts/storage/migrations/001-init.d.ts +0 -2
  80. package/lib/dts/storage/migrations/002-add-agent-id.d.ts +0 -2
  81. package/lib/dts/storage/migrations/003-add-compact-table.d.ts +0 -2
  82. package/lib/dts/storage/migrations/004-add-memory-table.d.ts +0 -2
  83. package/lib/dts/storage/migrations/005-add-indexes.d.ts +0 -2
  84. package/lib/dts/storage/models/compact.d.ts +0 -183
  85. package/lib/dts/storage/models/entries.d.ts +0 -218
  86. package/lib/dts/storage/models/memory.d.ts +0 -182
  87. package/lib/dts/storage/type.d.ts +0 -52
  88. package/lib/esm/index.d.ts +0 -41
  89. package/lib/esm/index.js +0 -256
  90. package/lib/esm/package.json +0 -3
  91. package/lib/esm/storage/index.d.ts +0 -21
  92. package/lib/esm/storage/index.js +0 -100
  93. package/lib/esm/storage/migrate.d.ts +0 -3
  94. package/lib/esm/storage/migrate.js +0 -38
  95. package/lib/esm/storage/migrations/001-init.d.ts +0 -2
  96. package/lib/esm/storage/migrations/002-add-agent-id.d.ts +0 -2
  97. package/lib/esm/storage/migrations/002-add-agent-id.js +0 -10
  98. package/lib/esm/storage/migrations/003-add-compact-table.d.ts +0 -2
  99. package/lib/esm/storage/migrations/004-add-memory-table.d.ts +0 -2
  100. package/lib/esm/storage/migrations/005-add-indexes.d.ts +0 -2
  101. package/lib/esm/storage/migrations/005-add-indexes.js +0 -28
  102. package/lib/esm/storage/models/compact.d.ts +0 -183
  103. package/lib/esm/storage/models/compact.js +0 -22
  104. package/lib/esm/storage/models/entries.d.ts +0 -218
  105. package/lib/esm/storage/models/entries.js +0 -24
  106. package/lib/esm/storage/models/memory.d.ts +0 -182
  107. package/lib/esm/storage/models/memory.js +0 -22
  108. package/lib/esm/storage/type.d.ts +0 -52
  109. package/lib/esm/storage/type.js +0 -8
package/dist/index.cjs ADDED
@@ -0,0 +1,285 @@
1
+ const require_type = require('./storage/type.cjs');
2
+ const require_storage_index = require('./storage/index.cjs');
3
+ let _aigne_afs = require("@aigne/afs");
4
+ let _aigne_afs_utils_zod = require("@aigne/afs/utils/zod");
5
+ let _aigne_uuid = require("@aigne/uuid");
6
+ let radix3 = require("radix3");
7
+ let ufo = require("ufo");
8
+ let zod = require("zod");
9
+
10
+ //#region src/index.ts
11
+ const afsStorageOptionsSchema = (0, _aigne_afs_utils_zod.camelize)(zod.z.object({ url: (0, _aigne_afs_utils_zod.optionalize)(zod.z.string().describe("Database URL for storage")) }));
12
+ const afsHistoryOptionsSchema = (0, _aigne_afs_utils_zod.camelize)(zod.z.object({
13
+ storage: (0, _aigne_afs_utils_zod.optionalize)(zod.z.union([zod.z.custom((v) => require_type.isAFSStorage(v)), afsStorageOptionsSchema])),
14
+ accessMode: _aigne_afs.accessModeSchema
15
+ }));
16
+ var AFSHistory = class AFSHistory {
17
+ static schema() {
18
+ return afsHistoryOptionsSchema;
19
+ }
20
+ static async load({ parsed }) {
21
+ return new AFSHistory(await AFSHistory.schema().parseAsync(parsed));
22
+ }
23
+ constructor(options) {
24
+ (0, _aigne_afs_utils_zod.zodParse)((0, _aigne_afs_utils_zod.optionalize)(afsHistoryOptionsSchema), options);
25
+ this.storage = require_type.isAFSStorage(options?.storage) ? options.storage : new require_storage_index.AFSStorageSQLite(this, options?.storage);
26
+ this.accessMode = options?.accessMode ?? "readwrite";
27
+ }
28
+ name = "history";
29
+ accessMode;
30
+ storage;
31
+ afs;
32
+ router = (0, radix3.createRouter)({ routes: {
33
+ "/by-session": {
34
+ action: "list",
35
+ type: "history",
36
+ scope: "session"
37
+ },
38
+ "/by-session/:sessionId": {
39
+ action: "list",
40
+ type: "history",
41
+ scope: "session"
42
+ },
43
+ "/by-session/:sessionId/new": {
44
+ action: "create",
45
+ type: "history",
46
+ scope: "session"
47
+ },
48
+ "/by-session/:sessionId/@metadata/compact": {
49
+ action: "list",
50
+ type: "compact",
51
+ scope: "session"
52
+ },
53
+ "/by-session/:sessionId/@metadata/compact/new": {
54
+ action: "create",
55
+ type: "compact",
56
+ scope: "session"
57
+ },
58
+ "/by-session/:sessionId/@metadata/compact/:compactId": {
59
+ action: "read",
60
+ type: "compact",
61
+ scope: "session"
62
+ },
63
+ "/by-session/:sessionId/@metadata/memory": {
64
+ action: "list",
65
+ type: "memory",
66
+ scope: "session"
67
+ },
68
+ "/by-session/:sessionId/@metadata/memory/new": {
69
+ action: "create",
70
+ type: "memory",
71
+ scope: "session"
72
+ },
73
+ "/by-session/:sessionId/@metadata/memory/:memoryId": {
74
+ action: "read",
75
+ type: "memory",
76
+ scope: "session"
77
+ },
78
+ "/by-session/:sessionId/:entryId": {
79
+ action: "read",
80
+ type: "history",
81
+ scope: "session"
82
+ },
83
+ "/by-user": {
84
+ action: "list",
85
+ type: "history",
86
+ scope: "user"
87
+ },
88
+ "/by-user/:userId": {
89
+ action: "list",
90
+ type: "history",
91
+ scope: "user"
92
+ },
93
+ "/by-user/:userId/new": {
94
+ action: "create",
95
+ type: "history",
96
+ scope: "user"
97
+ },
98
+ "/by-user/:userId/@metadata/compact": {
99
+ action: "list",
100
+ type: "compact",
101
+ scope: "user"
102
+ },
103
+ "/by-user/:userId/@metadata/compact/new": {
104
+ action: "create",
105
+ type: "compact",
106
+ scope: "user"
107
+ },
108
+ "/by-user/:userId/@metadata/compact/:compactId": {
109
+ action: "read",
110
+ type: "compact",
111
+ scope: "user"
112
+ },
113
+ "/by-user/:userId/@metadata/memory": {
114
+ action: "list",
115
+ type: "memory",
116
+ scope: "user"
117
+ },
118
+ "/by-user/:userId/@metadata/memory/new": {
119
+ action: "create",
120
+ type: "memory",
121
+ scope: "user"
122
+ },
123
+ "/by-user/:userId/@metadata/memory/:memoryId": {
124
+ action: "read",
125
+ type: "memory",
126
+ scope: "user"
127
+ },
128
+ "/by-user/:userId/:entryId": {
129
+ action: "read",
130
+ type: "history",
131
+ scope: "user"
132
+ },
133
+ "/by-agent": {
134
+ action: "list",
135
+ type: "history",
136
+ scope: "agent"
137
+ },
138
+ "/by-agent/:agentId": {
139
+ action: "list",
140
+ type: "history",
141
+ scope: "agent"
142
+ },
143
+ "/by-agent/:agentId/new": {
144
+ action: "create",
145
+ type: "history",
146
+ scope: "agent"
147
+ },
148
+ "/by-agent/:agentId/@metadata/compact": {
149
+ action: "list",
150
+ type: "compact",
151
+ scope: "agent"
152
+ },
153
+ "/by-agent/:agentId/@metadata/compact/new": {
154
+ action: "create",
155
+ type: "compact",
156
+ scope: "agent"
157
+ },
158
+ "/by-agent/:agentId/@metadata/compact/:compactId": {
159
+ action: "read",
160
+ type: "compact",
161
+ scope: "agent"
162
+ },
163
+ "/by-agent/:agentId/:entryId": {
164
+ action: "read",
165
+ type: "history",
166
+ scope: "agent"
167
+ }
168
+ } });
169
+ rootEntries = [
170
+ {
171
+ id: "by-session",
172
+ path: "/by-session",
173
+ description: "Retrieve history entries by session ID."
174
+ },
175
+ {
176
+ id: "by-user",
177
+ path: "/by-user",
178
+ description: "Retrieve history entries by user ID."
179
+ },
180
+ {
181
+ id: "by-agent",
182
+ path: "/by-agent",
183
+ description: "Retrieve history entries by agent ID."
184
+ }
185
+ ];
186
+ onMount(afs) {
187
+ this.afs = afs;
188
+ }
189
+ async list(path, options) {
190
+ if (path === "/") return { data: this.rootEntries };
191
+ const match = this.router.lookup(path);
192
+ if (!match || match.action !== "list") return { data: [] };
193
+ const { type, scope } = match;
194
+ const mergedFilter = {
195
+ ...options?.filter,
196
+ ...match.params
197
+ };
198
+ const result = await this.storage.list({
199
+ ...options,
200
+ type,
201
+ scope,
202
+ filter: mergedFilter
203
+ });
204
+ return {
205
+ ...result,
206
+ data: result.data.map((entry) => ({
207
+ ...entry,
208
+ path: this.normalizePath(entry, scope, type)
209
+ }))
210
+ };
211
+ }
212
+ async read(path, options) {
213
+ const match = this.router.lookup(path);
214
+ if (!match || match.action !== "read") return {};
215
+ const { type, scope } = match;
216
+ const entryId = match.params?.entryId ?? match.params?.compactId ?? match.params?.memoryId;
217
+ if (!entryId) throw new Error(`Entry ID is required in the path to read ${type}.`);
218
+ const mergedFilter = {
219
+ ...options?.filter,
220
+ ...match.params
221
+ };
222
+ const data = await this.storage.read(entryId, {
223
+ type,
224
+ scope,
225
+ filter: mergedFilter
226
+ });
227
+ return { data: data && {
228
+ ...data,
229
+ path: this.normalizePath(data, scope, type)
230
+ } };
231
+ }
232
+ async write(path, content, options) {
233
+ const id = (0, _aigne_uuid.v7)();
234
+ const match = this.router.lookup(path);
235
+ if (!match || match.action !== "create") throw new Error("Can only write to paths with 'new' suffix: /by-{scope}/{scopeId}/new or /by-{scope}/{scopeId}/@metadata/{type}/new");
236
+ const { type, scope } = match;
237
+ const scopeIdField = `${scope}Id`;
238
+ if (!match.params?.[scopeIdField]) throw new Error(`${scopeIdField} is required in the path to create ${type} entry.`);
239
+ const entry = await this.storage.create({
240
+ ...match.params,
241
+ ...content,
242
+ id,
243
+ path: (0, ufo.joinURL)("/", type, id)
244
+ }, {
245
+ type,
246
+ scope
247
+ });
248
+ if (type === "history") this.afs?.emit("historyCreated", { entry }, options);
249
+ return { data: {
250
+ ...entry,
251
+ path: this.normalizePath(entry, scope, type)
252
+ } };
253
+ }
254
+ async delete(path, _options) {
255
+ const match = this.router.lookup(path);
256
+ if (!match || match.action !== "read") throw new Error(`Cannot delete: path not found or not a valid entry path`);
257
+ const { type, scope } = match;
258
+ const entryId = match.params?.entryId ?? match.params?.compactId ?? match.params?.memoryId;
259
+ if (!entryId) throw new Error(`Entry ID is required in the path to delete ${type}.`);
260
+ const result = await this.storage.delete(entryId, {
261
+ type,
262
+ scope,
263
+ filter: match.params
264
+ });
265
+ if (result.deletedCount === 0) return { message: `No ${type} entry found with id ${entryId}` };
266
+ return { message: `Deleted ${result.deletedCount} ${type} entry` };
267
+ }
268
+ normalizePath(entry, scope, entryType) {
269
+ const scopeId = {
270
+ session: entry.sessionId,
271
+ user: entry.userId,
272
+ agent: entry.agentId
273
+ }[scope];
274
+ if (!scopeId) throw new Error(`Cannot reset path for entry without ${scope} info.`);
275
+ const prefix = `by-${scope}`;
276
+ if (entryType === "compact") return (0, ufo.joinURL)("/", prefix, scopeId, "@metadata/compact", entry.id);
277
+ if (entryType === "memory") return (0, ufo.joinURL)("/", prefix, scopeId, "@metadata/memory", entry.id);
278
+ return (0, ufo.joinURL)("/", prefix, scopeId, entry.id);
279
+ }
280
+ };
281
+
282
+ //#endregion
283
+ exports.AFSHistory = AFSHistory;
284
+ exports.AFSStorageSQLite = require_storage_index.AFSStorageSQLite;
285
+ exports.isAFSStorage = require_type.isAFSStorage;
@@ -0,0 +1,48 @@
1
+ import { AFSStorage, AFSStorageCreateOptions, AFSStorageCreatePayload, AFSStorageDeleteOptions, AFSStorageListOptions, AFSStorageMigrations, AFSStorageReadOptions, AFSStorageTypeOptions, EntryType, Scope, isAFSStorage } from "./storage/type.cjs";
2
+ import { AFSStorageSQLite, AFSStorageSQLiteOptions } from "./storage/index.cjs";
3
+ import { AFSAccessMode, AFSDeleteOptions, AFSDeleteResult, AFSListOptions, AFSListResult, AFSModule, AFSModuleLoadParams, AFSReadOptions, AFSReadResult, AFSRoot, AFSWriteEntryPayload, AFSWriteOptions, AFSWriteResult } from "@aigne/afs";
4
+ import { z } from "zod";
5
+
6
+ //#region src/index.d.ts
7
+ interface AFSHistoryOptions {
8
+ storage?: AFSStorage | AFSStorageSQLiteOptions;
9
+ /**
10
+ * Access mode for this module.
11
+ * @default "readwrite"
12
+ */
13
+ accessMode?: AFSAccessMode;
14
+ }
15
+ declare class AFSHistory implements AFSModule {
16
+ static schema(): z.ZodEffects<z.ZodObject<{
17
+ storage: z.ZodType<any, z.ZodTypeDef, any>;
18
+ accessMode: z.ZodOptional<z.ZodEnum<["readonly", "readwrite"]>>;
19
+ }, "strip", z.ZodTypeAny, {
20
+ storage?: any;
21
+ accessMode?: "readonly" | "readwrite" | undefined;
22
+ }, {
23
+ storage?: any;
24
+ accessMode?: "readonly" | "readwrite" | undefined;
25
+ }>, {
26
+ storage?: any;
27
+ accessMode?: "readonly" | "readwrite" | undefined;
28
+ }, any>;
29
+ static load({
30
+ parsed
31
+ }: AFSModuleLoadParams): Promise<AFSHistory>;
32
+ constructor(options?: AFSHistoryOptions);
33
+ readonly name: string;
34
+ readonly accessMode: AFSAccessMode;
35
+ private storage;
36
+ private afs?;
37
+ private router;
38
+ private rootEntries;
39
+ onMount(afs: AFSRoot): void;
40
+ list(path: string, options?: AFSListOptions): Promise<AFSListResult>;
41
+ read(path: string, options?: AFSReadOptions): Promise<AFSReadResult>;
42
+ write(path: string, content: AFSWriteEntryPayload, options: AFSWriteOptions): Promise<AFSWriteResult>;
43
+ delete(path: string, _options?: AFSDeleteOptions): Promise<AFSDeleteResult>;
44
+ private normalizePath;
45
+ }
46
+ //#endregion
47
+ export { AFSHistory, AFSHistoryOptions, AFSStorage, AFSStorageCreateOptions, AFSStorageCreatePayload, AFSStorageDeleteOptions, AFSStorageListOptions, AFSStorageMigrations, AFSStorageReadOptions, AFSStorageSQLite, AFSStorageSQLiteOptions, AFSStorageTypeOptions, EntryType, Scope, isAFSStorage };
48
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;UAkCiB,iBAAA;EAAA,OAAA,GACL,UAAA,GAAa,uBAAA;EAAA;;;AAqBzB;EArByB,UAAA,GAKV,aAAA;AAAA;AAAA,cAgBF,UAAA,YAAsB,SAAA;EAAA,OAAA,OAAA,GACpB,CAAA,CAAA,UAAA,CAAA,CAAA,CAAA,SAAA;IAAA,OAAA;;;;;;;;;;;;;;KAIiB,mBAAA,GAAmB,OAAA,CAAA,UAAA;EAAA,YAAA,OAAA,GAK3B,iBAAA;EAAA,SAAA,IAAA;EAAA,SAAA,UAAA,EAWD,aAAA;EAAA,QAAA,OAAA;EAAA,QAAA,GAAA;EAAA,QAAA,MAAA;EAAA,QAAA,WAAA;EAAA,QAAA,GAAA,EAwGR,OAAA;EAAA,KAAA,IAAA,UAAA,OAAA,GAIsB,cAAA,GAAiB,OAAA,CAAQ,aAAA;EAAA,KAAA,IAAA,UAAA,OAAA,GA8BzB,cAAA,GAAiB,OAAA,CAAQ,aAAA;EAAA,MAAA,IAAA,UAAA,OAAA,EA+BjD,oBAAA,EAAA,OAAA,EACA,eAAA,GACR,OAAA,CAAQ,cAAA;EAAA,OAAA,IAAA,UAAA,QAAA,GA0C2B,gBAAA,GAAmB,OAAA,CAAQ,eAAA;EAAA,QAAA,aAAA;AAAA"}
@@ -0,0 +1,48 @@
1
+ import { AFSStorage, AFSStorageCreateOptions, AFSStorageCreatePayload, AFSStorageDeleteOptions, AFSStorageListOptions, AFSStorageMigrations, AFSStorageReadOptions, AFSStorageTypeOptions, EntryType, Scope, isAFSStorage } from "./storage/type.mjs";
2
+ import { AFSStorageSQLite, AFSStorageSQLiteOptions } from "./storage/index.mjs";
3
+ import { AFSAccessMode, AFSDeleteOptions, AFSDeleteResult, AFSListOptions, AFSListResult, AFSModule, AFSModuleLoadParams, AFSReadOptions, AFSReadResult, AFSRoot, AFSWriteEntryPayload, AFSWriteOptions, AFSWriteResult } from "@aigne/afs";
4
+ import { z } from "zod";
5
+
6
+ //#region src/index.d.ts
7
+ interface AFSHistoryOptions {
8
+ storage?: AFSStorage | AFSStorageSQLiteOptions;
9
+ /**
10
+ * Access mode for this module.
11
+ * @default "readwrite"
12
+ */
13
+ accessMode?: AFSAccessMode;
14
+ }
15
+ declare class AFSHistory implements AFSModule {
16
+ static schema(): z.ZodEffects<z.ZodObject<{
17
+ storage: z.ZodType<any, z.ZodTypeDef, any>;
18
+ accessMode: z.ZodOptional<z.ZodEnum<["readonly", "readwrite"]>>;
19
+ }, "strip", z.ZodTypeAny, {
20
+ storage?: any;
21
+ accessMode?: "readonly" | "readwrite" | undefined;
22
+ }, {
23
+ storage?: any;
24
+ accessMode?: "readonly" | "readwrite" | undefined;
25
+ }>, {
26
+ storage?: any;
27
+ accessMode?: "readonly" | "readwrite" | undefined;
28
+ }, any>;
29
+ static load({
30
+ parsed
31
+ }: AFSModuleLoadParams): Promise<AFSHistory>;
32
+ constructor(options?: AFSHistoryOptions);
33
+ readonly name: string;
34
+ readonly accessMode: AFSAccessMode;
35
+ private storage;
36
+ private afs?;
37
+ private router;
38
+ private rootEntries;
39
+ onMount(afs: AFSRoot): void;
40
+ list(path: string, options?: AFSListOptions): Promise<AFSListResult>;
41
+ read(path: string, options?: AFSReadOptions): Promise<AFSReadResult>;
42
+ write(path: string, content: AFSWriteEntryPayload, options: AFSWriteOptions): Promise<AFSWriteResult>;
43
+ delete(path: string, _options?: AFSDeleteOptions): Promise<AFSDeleteResult>;
44
+ private normalizePath;
45
+ }
46
+ //#endregion
47
+ export { AFSHistory, AFSHistoryOptions, AFSStorage, AFSStorageCreateOptions, AFSStorageCreatePayload, AFSStorageDeleteOptions, AFSStorageListOptions, AFSStorageMigrations, AFSStorageReadOptions, AFSStorageSQLite, AFSStorageSQLiteOptions, AFSStorageTypeOptions, EntryType, Scope, isAFSStorage };
48
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;UAkCiB,iBAAA;EAAA,OAAA,GACL,UAAA,GAAa,uBAAA;EAAA;;;AAqBzB;EArByB,UAAA,GAKV,aAAA;AAAA;AAAA,cAgBF,UAAA,YAAsB,SAAA;EAAA,OAAA,OAAA,GACpB,CAAA,CAAA,UAAA,CAAA,CAAA,CAAA,SAAA;IAAA,OAAA;;;;;;;;;;;;;;KAIiB,mBAAA,GAAmB,OAAA,CAAA,UAAA;EAAA,YAAA,OAAA,GAK3B,iBAAA;EAAA,SAAA,IAAA;EAAA,SAAA,UAAA,EAWD,aAAA;EAAA,QAAA,OAAA;EAAA,QAAA,GAAA;EAAA,QAAA,MAAA;EAAA,QAAA,WAAA;EAAA,QAAA,GAAA,EAwGR,OAAA;EAAA,KAAA,IAAA,UAAA,OAAA,GAIsB,cAAA,GAAiB,OAAA,CAAQ,aAAA;EAAA,KAAA,IAAA,UAAA,OAAA,GA8BzB,cAAA,GAAiB,OAAA,CAAQ,aAAA;EAAA,MAAA,IAAA,UAAA,OAAA,EA+BjD,oBAAA,EAAA,OAAA,EACA,eAAA,GACR,OAAA,CAAQ,cAAA;EAAA,OAAA,IAAA,UAAA,QAAA,GA0C2B,gBAAA,GAAmB,OAAA,CAAQ,eAAA;EAAA,QAAA,aAAA;AAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,284 @@
1
+ import { isAFSStorage } from "./storage/type.mjs";
2
+ import { AFSStorageSQLite } from "./storage/index.mjs";
3
+ import { accessModeSchema } from "@aigne/afs";
4
+ import { camelize, optionalize, zodParse } from "@aigne/afs/utils/zod";
5
+ import { v7 } from "@aigne/uuid";
6
+ import { createRouter } from "radix3";
7
+ import { joinURL } from "ufo";
8
+ import { z } from "zod";
9
+
10
+ //#region src/index.ts
11
+ const afsStorageOptionsSchema = camelize(z.object({ url: optionalize(z.string().describe("Database URL for storage")) }));
12
+ const afsHistoryOptionsSchema = camelize(z.object({
13
+ storage: optionalize(z.union([z.custom((v) => isAFSStorage(v)), afsStorageOptionsSchema])),
14
+ accessMode: accessModeSchema
15
+ }));
16
+ var AFSHistory = class AFSHistory {
17
+ static schema() {
18
+ return afsHistoryOptionsSchema;
19
+ }
20
+ static async load({ parsed }) {
21
+ return new AFSHistory(await AFSHistory.schema().parseAsync(parsed));
22
+ }
23
+ constructor(options) {
24
+ zodParse(optionalize(afsHistoryOptionsSchema), options);
25
+ this.storage = isAFSStorage(options?.storage) ? options.storage : new AFSStorageSQLite(this, options?.storage);
26
+ this.accessMode = options?.accessMode ?? "readwrite";
27
+ }
28
+ name = "history";
29
+ accessMode;
30
+ storage;
31
+ afs;
32
+ router = createRouter({ routes: {
33
+ "/by-session": {
34
+ action: "list",
35
+ type: "history",
36
+ scope: "session"
37
+ },
38
+ "/by-session/:sessionId": {
39
+ action: "list",
40
+ type: "history",
41
+ scope: "session"
42
+ },
43
+ "/by-session/:sessionId/new": {
44
+ action: "create",
45
+ type: "history",
46
+ scope: "session"
47
+ },
48
+ "/by-session/:sessionId/@metadata/compact": {
49
+ action: "list",
50
+ type: "compact",
51
+ scope: "session"
52
+ },
53
+ "/by-session/:sessionId/@metadata/compact/new": {
54
+ action: "create",
55
+ type: "compact",
56
+ scope: "session"
57
+ },
58
+ "/by-session/:sessionId/@metadata/compact/:compactId": {
59
+ action: "read",
60
+ type: "compact",
61
+ scope: "session"
62
+ },
63
+ "/by-session/:sessionId/@metadata/memory": {
64
+ action: "list",
65
+ type: "memory",
66
+ scope: "session"
67
+ },
68
+ "/by-session/:sessionId/@metadata/memory/new": {
69
+ action: "create",
70
+ type: "memory",
71
+ scope: "session"
72
+ },
73
+ "/by-session/:sessionId/@metadata/memory/:memoryId": {
74
+ action: "read",
75
+ type: "memory",
76
+ scope: "session"
77
+ },
78
+ "/by-session/:sessionId/:entryId": {
79
+ action: "read",
80
+ type: "history",
81
+ scope: "session"
82
+ },
83
+ "/by-user": {
84
+ action: "list",
85
+ type: "history",
86
+ scope: "user"
87
+ },
88
+ "/by-user/:userId": {
89
+ action: "list",
90
+ type: "history",
91
+ scope: "user"
92
+ },
93
+ "/by-user/:userId/new": {
94
+ action: "create",
95
+ type: "history",
96
+ scope: "user"
97
+ },
98
+ "/by-user/:userId/@metadata/compact": {
99
+ action: "list",
100
+ type: "compact",
101
+ scope: "user"
102
+ },
103
+ "/by-user/:userId/@metadata/compact/new": {
104
+ action: "create",
105
+ type: "compact",
106
+ scope: "user"
107
+ },
108
+ "/by-user/:userId/@metadata/compact/:compactId": {
109
+ action: "read",
110
+ type: "compact",
111
+ scope: "user"
112
+ },
113
+ "/by-user/:userId/@metadata/memory": {
114
+ action: "list",
115
+ type: "memory",
116
+ scope: "user"
117
+ },
118
+ "/by-user/:userId/@metadata/memory/new": {
119
+ action: "create",
120
+ type: "memory",
121
+ scope: "user"
122
+ },
123
+ "/by-user/:userId/@metadata/memory/:memoryId": {
124
+ action: "read",
125
+ type: "memory",
126
+ scope: "user"
127
+ },
128
+ "/by-user/:userId/:entryId": {
129
+ action: "read",
130
+ type: "history",
131
+ scope: "user"
132
+ },
133
+ "/by-agent": {
134
+ action: "list",
135
+ type: "history",
136
+ scope: "agent"
137
+ },
138
+ "/by-agent/:agentId": {
139
+ action: "list",
140
+ type: "history",
141
+ scope: "agent"
142
+ },
143
+ "/by-agent/:agentId/new": {
144
+ action: "create",
145
+ type: "history",
146
+ scope: "agent"
147
+ },
148
+ "/by-agent/:agentId/@metadata/compact": {
149
+ action: "list",
150
+ type: "compact",
151
+ scope: "agent"
152
+ },
153
+ "/by-agent/:agentId/@metadata/compact/new": {
154
+ action: "create",
155
+ type: "compact",
156
+ scope: "agent"
157
+ },
158
+ "/by-agent/:agentId/@metadata/compact/:compactId": {
159
+ action: "read",
160
+ type: "compact",
161
+ scope: "agent"
162
+ },
163
+ "/by-agent/:agentId/:entryId": {
164
+ action: "read",
165
+ type: "history",
166
+ scope: "agent"
167
+ }
168
+ } });
169
+ rootEntries = [
170
+ {
171
+ id: "by-session",
172
+ path: "/by-session",
173
+ description: "Retrieve history entries by session ID."
174
+ },
175
+ {
176
+ id: "by-user",
177
+ path: "/by-user",
178
+ description: "Retrieve history entries by user ID."
179
+ },
180
+ {
181
+ id: "by-agent",
182
+ path: "/by-agent",
183
+ description: "Retrieve history entries by agent ID."
184
+ }
185
+ ];
186
+ onMount(afs) {
187
+ this.afs = afs;
188
+ }
189
+ async list(path, options) {
190
+ if (path === "/") return { data: this.rootEntries };
191
+ const match = this.router.lookup(path);
192
+ if (!match || match.action !== "list") return { data: [] };
193
+ const { type, scope } = match;
194
+ const mergedFilter = {
195
+ ...options?.filter,
196
+ ...match.params
197
+ };
198
+ const result = await this.storage.list({
199
+ ...options,
200
+ type,
201
+ scope,
202
+ filter: mergedFilter
203
+ });
204
+ return {
205
+ ...result,
206
+ data: result.data.map((entry) => ({
207
+ ...entry,
208
+ path: this.normalizePath(entry, scope, type)
209
+ }))
210
+ };
211
+ }
212
+ async read(path, options) {
213
+ const match = this.router.lookup(path);
214
+ if (!match || match.action !== "read") return {};
215
+ const { type, scope } = match;
216
+ const entryId = match.params?.entryId ?? match.params?.compactId ?? match.params?.memoryId;
217
+ if (!entryId) throw new Error(`Entry ID is required in the path to read ${type}.`);
218
+ const mergedFilter = {
219
+ ...options?.filter,
220
+ ...match.params
221
+ };
222
+ const data = await this.storage.read(entryId, {
223
+ type,
224
+ scope,
225
+ filter: mergedFilter
226
+ });
227
+ return { data: data && {
228
+ ...data,
229
+ path: this.normalizePath(data, scope, type)
230
+ } };
231
+ }
232
+ async write(path, content, options) {
233
+ const id = v7();
234
+ const match = this.router.lookup(path);
235
+ if (!match || match.action !== "create") throw new Error("Can only write to paths with 'new' suffix: /by-{scope}/{scopeId}/new or /by-{scope}/{scopeId}/@metadata/{type}/new");
236
+ const { type, scope } = match;
237
+ const scopeIdField = `${scope}Id`;
238
+ if (!match.params?.[scopeIdField]) throw new Error(`${scopeIdField} is required in the path to create ${type} entry.`);
239
+ const entry = await this.storage.create({
240
+ ...match.params,
241
+ ...content,
242
+ id,
243
+ path: joinURL("/", type, id)
244
+ }, {
245
+ type,
246
+ scope
247
+ });
248
+ if (type === "history") this.afs?.emit("historyCreated", { entry }, options);
249
+ return { data: {
250
+ ...entry,
251
+ path: this.normalizePath(entry, scope, type)
252
+ } };
253
+ }
254
+ async delete(path, _options) {
255
+ const match = this.router.lookup(path);
256
+ if (!match || match.action !== "read") throw new Error(`Cannot delete: path not found or not a valid entry path`);
257
+ const { type, scope } = match;
258
+ const entryId = match.params?.entryId ?? match.params?.compactId ?? match.params?.memoryId;
259
+ if (!entryId) throw new Error(`Entry ID is required in the path to delete ${type}.`);
260
+ const result = await this.storage.delete(entryId, {
261
+ type,
262
+ scope,
263
+ filter: match.params
264
+ });
265
+ if (result.deletedCount === 0) return { message: `No ${type} entry found with id ${entryId}` };
266
+ return { message: `Deleted ${result.deletedCount} ${type} entry` };
267
+ }
268
+ normalizePath(entry, scope, entryType) {
269
+ const scopeId = {
270
+ session: entry.sessionId,
271
+ user: entry.userId,
272
+ agent: entry.agentId
273
+ }[scope];
274
+ if (!scopeId) throw new Error(`Cannot reset path for entry without ${scope} info.`);
275
+ const prefix = `by-${scope}`;
276
+ if (entryType === "compact") return joinURL("/", prefix, scopeId, "@metadata/compact", entry.id);
277
+ if (entryType === "memory") return joinURL("/", prefix, scopeId, "@metadata/memory", entry.id);
278
+ return joinURL("/", prefix, scopeId, entry.id);
279
+ }
280
+ };
281
+
282
+ //#endregion
283
+ export { AFSHistory, AFSStorageSQLite, isAFSStorage };
284
+ //# sourceMappingURL=index.mjs.map