@aigne/agent-library 1.11.0 → 1.12.1
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 +26 -0
- package/lib/cjs/default-memory/default-memory-storage/index.d.ts +23 -0
- package/lib/cjs/default-memory/default-memory-storage/index.js +65 -0
- package/lib/cjs/default-memory/default-memory-storage/migrate.d.ts +3 -0
- package/lib/cjs/default-memory/default-memory-storage/migrate.js +32 -0
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.d.ts +5 -0
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.js +16 -0
- package/lib/cjs/default-memory/default-memory-storage/models/memory.d.ts +102 -0
- package/lib/cjs/default-memory/default-memory-storage/models/memory.js +21 -0
- package/lib/cjs/default-memory/index.d.ts +10 -0
- package/lib/cjs/default-memory/index.js +60 -0
- package/lib/cjs/default-memory/storage.d.ts +12 -0
- package/lib/cjs/default-memory/storage.js +6 -0
- package/lib/cjs/fs-memory/index.js +1 -1
- package/lib/cjs/utils/fs.d.ts +2 -0
- package/lib/cjs/utils/fs.js +25 -0
- package/lib/dts/default-memory/default-memory-storage/index.d.ts +23 -0
- package/lib/dts/default-memory/default-memory-storage/migrate.d.ts +3 -0
- package/lib/dts/default-memory/default-memory-storage/migrations/001-init.d.ts +5 -0
- package/lib/dts/default-memory/default-memory-storage/models/memory.d.ts +102 -0
- package/lib/dts/default-memory/index.d.ts +10 -0
- package/lib/dts/default-memory/storage.d.ts +12 -0
- package/lib/dts/utils/fs.d.ts +2 -0
- package/lib/esm/default-memory/default-memory-storage/index.d.ts +23 -0
- package/lib/esm/default-memory/default-memory-storage/index.js +61 -0
- package/lib/esm/default-memory/default-memory-storage/migrate.d.ts +3 -0
- package/lib/esm/default-memory/default-memory-storage/migrate.js +26 -0
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.d.ts +5 -0
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.js +14 -0
- package/lib/esm/default-memory/default-memory-storage/models/memory.d.ts +102 -0
- package/lib/esm/default-memory/default-memory-storage/models/memory.js +18 -0
- package/lib/esm/default-memory/index.d.ts +10 -0
- package/lib/esm/default-memory/index.js +53 -0
- package/lib/esm/default-memory/storage.d.ts +12 -0
- package/lib/esm/default-memory/storage.js +2 -0
- package/lib/esm/fs-memory/index.js +1 -1
- package/lib/esm/utils/fs.d.ts +2 -0
- package/lib/esm/utils/fs.js +21 -0
- package/package.json +8 -5
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,32 @@
|
|
|
5
5
|
|
|
6
6
|
* add schema transform ([#35](https://github.com/AIGNE-io/aigne-framework/issues/35)) ([c7d9a2c](https://github.com/AIGNE-io/aigne-framework/commit/c7d9a2c9fcab8d384d4198db5ff6ba4603846cdf))
|
|
7
7
|
|
|
8
|
+
## [1.12.1](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.12.0...agent-library-v1.12.1) (2025-05-30)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @aigne/core bumped to 1.18.1
|
|
16
|
+
* @aigne/openai bumped to 0.2.2
|
|
17
|
+
|
|
18
|
+
## [1.12.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.11.0...agent-library-v1.12.0) (2025-05-29)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
* add memory agents support for client agent ([#139](https://github.com/AIGNE-io/aigne-framework/issues/139)) ([57044fa](https://github.com/AIGNE-io/aigne-framework/commit/57044fa87b8abcba395cd05f941d6d312ab65764))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Dependencies
|
|
27
|
+
|
|
28
|
+
* The following workspace dependencies were updated
|
|
29
|
+
* dependencies
|
|
30
|
+
* @aigne/core bumped to 1.18.0
|
|
31
|
+
* @aigne/openai bumped to 0.2.1
|
|
32
|
+
* @aigne/sqlite bumped to 0.1.0
|
|
33
|
+
|
|
8
34
|
## [1.10.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.9.0...agent-library-v1.10.0) (2025-05-25)
|
|
9
35
|
|
|
10
36
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, Context, Memory } from "@aigne/core";
|
|
2
|
+
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
+
import { MemoryStorage } from "../storage.js";
|
|
4
|
+
export interface DefaultMemoryStorageOptions {
|
|
5
|
+
url?: string;
|
|
6
|
+
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
7
|
+
}
|
|
8
|
+
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
9
|
+
options?: DefaultMemoryStorageOptions | undefined;
|
|
10
|
+
constructor(options?: DefaultMemoryStorageOptions | undefined);
|
|
11
|
+
private _db;
|
|
12
|
+
private initSqlite;
|
|
13
|
+
get db(): Promise<import("drizzle-orm/libsql/driver-core.js").LibSQLDatabase<Record<string, never>> | import("drizzle-orm/sqlite-proxy/driver.js").SqliteRemoteDatabase<Record<string, never>>>;
|
|
14
|
+
private convertMemory;
|
|
15
|
+
search(query: {
|
|
16
|
+
limit?: number;
|
|
17
|
+
}, { context }: AgentInvokeOptions): Promise<{
|
|
18
|
+
result: Memory[];
|
|
19
|
+
}>;
|
|
20
|
+
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
21
|
+
result: Memory;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultMemoryStorage = void 0;
|
|
4
|
+
const sqlite_1 = require("@aigne/sqlite");
|
|
5
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
6
|
+
const storage_js_1 = require("../storage.js");
|
|
7
|
+
const migrate_js_1 = require("./migrate.js");
|
|
8
|
+
const memory_js_1 = require("./models/memory.js");
|
|
9
|
+
const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
10
|
+
class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
|
|
11
|
+
options;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super();
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
_db;
|
|
17
|
+
async initSqlite() {
|
|
18
|
+
const db = await (0, sqlite_1.initDatabase)({ url: this.options?.url });
|
|
19
|
+
await (0, migrate_js_1.migrate)(db);
|
|
20
|
+
return db;
|
|
21
|
+
}
|
|
22
|
+
get db() {
|
|
23
|
+
this._db ??= this.initSqlite();
|
|
24
|
+
return this._db;
|
|
25
|
+
}
|
|
26
|
+
convertMemory(m) {
|
|
27
|
+
return {
|
|
28
|
+
id: m.id,
|
|
29
|
+
sessionId: m.sessionId,
|
|
30
|
+
content: m.content,
|
|
31
|
+
createdAt: m.createdAt.toISOString(),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async search(query, { context }) {
|
|
35
|
+
const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
|
|
36
|
+
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
37
|
+
const db = await this.db;
|
|
38
|
+
const memories = await db
|
|
39
|
+
.select()
|
|
40
|
+
.from(memory_js_1.Memories)
|
|
41
|
+
.where(sessionId ? (0, drizzle_orm_1.eq)(memory_js_1.Memories.sessionId, sessionId) : (0, drizzle_orm_1.isNull)(memory_js_1.Memories.sessionId))
|
|
42
|
+
.orderBy((0, drizzle_orm_1.desc)(memory_js_1.Memories.id))
|
|
43
|
+
.limit(limit)
|
|
44
|
+
.execute();
|
|
45
|
+
return {
|
|
46
|
+
result: memories.reverse().map(this.convertMemory),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async create(memory, { context }) {
|
|
50
|
+
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
51
|
+
const db = await this.db;
|
|
52
|
+
const [result] = await db
|
|
53
|
+
.insert(memory_js_1.Memories)
|
|
54
|
+
.values({
|
|
55
|
+
...memory,
|
|
56
|
+
sessionId,
|
|
57
|
+
})
|
|
58
|
+
.returning()
|
|
59
|
+
.execute();
|
|
60
|
+
if (!result)
|
|
61
|
+
throw new Error("Failed to create memory");
|
|
62
|
+
return { result: this.convertMemory(result) };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.DefaultMemoryStorage = DefaultMemoryStorage;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.migrate = migrate;
|
|
7
|
+
const sql_1 = require("drizzle-orm/sql");
|
|
8
|
+
const _001_init_js_1 = __importDefault(require("./migrations/001-init.js"));
|
|
9
|
+
async function migrate(db) {
|
|
10
|
+
const migrations = [_001_init_js_1.default];
|
|
11
|
+
const migrationsTable = "__drizzle_migrations";
|
|
12
|
+
const migrationTableCreate = (0, sql_1.sql) `
|
|
13
|
+
CREATE TABLE IF NOT EXISTS ${sql_1.sql.identifier(migrationsTable)} (
|
|
14
|
+
id SERIAL PRIMARY KEY,
|
|
15
|
+
hash text NOT NULL
|
|
16
|
+
)
|
|
17
|
+
`;
|
|
18
|
+
await db.run(migrationTableCreate).execute();
|
|
19
|
+
const dbMigrations = await db
|
|
20
|
+
.values((0, sql_1.sql) `SELECT id, hash FROM ${sql_1.sql.identifier(migrationsTable)} ORDER BY id DESC LIMIT 1`)
|
|
21
|
+
.execute();
|
|
22
|
+
const lastDbMigration = dbMigrations[0];
|
|
23
|
+
const queriesToRun = [];
|
|
24
|
+
for (const migration of migrations) {
|
|
25
|
+
if (!lastDbMigration || lastDbMigration[1] < migration.hash) {
|
|
26
|
+
queriesToRun.push(...migration.sql, `INSERT INTO \`${migrationsTable}\` ("hash") VALUES('${migration.hash}')`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const query of queriesToRun) {
|
|
30
|
+
await db.run(query).execute();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
hash: "001-init",
|
|
5
|
+
sql: [
|
|
6
|
+
`\
|
|
7
|
+
CREATE TABLE "Memories" (
|
|
8
|
+
"id" TEXT NOT NULL PRIMARY KEY,
|
|
9
|
+
"createdAt" DATETIME NOT NULL,
|
|
10
|
+
"updatedAt" DATETIME NOT NULL,
|
|
11
|
+
"sessionId" TEXT,
|
|
12
|
+
"content" JSON NOT NULL
|
|
13
|
+
)
|
|
14
|
+
`,
|
|
15
|
+
],
|
|
16
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export declare const Memories: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
2
|
+
name: "Memories";
|
|
3
|
+
schema: undefined;
|
|
4
|
+
columns: {
|
|
5
|
+
id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
6
|
+
name: "id";
|
|
7
|
+
tableName: "Memories";
|
|
8
|
+
dataType: "string";
|
|
9
|
+
columnType: "SQLiteText";
|
|
10
|
+
data: string;
|
|
11
|
+
driverParam: string;
|
|
12
|
+
notNull: true;
|
|
13
|
+
hasDefault: true;
|
|
14
|
+
isPrimaryKey: true;
|
|
15
|
+
isAutoincrement: false;
|
|
16
|
+
hasRuntimeDefault: true;
|
|
17
|
+
enumValues: [string, ...string[]];
|
|
18
|
+
baseColumn: never;
|
|
19
|
+
identity: undefined;
|
|
20
|
+
generated: undefined;
|
|
21
|
+
}, {}, {
|
|
22
|
+
length: number | undefined;
|
|
23
|
+
}>;
|
|
24
|
+
createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
25
|
+
name: "createdAt";
|
|
26
|
+
tableName: "Memories";
|
|
27
|
+
dataType: "custom";
|
|
28
|
+
columnType: "SQLiteCustomColumn";
|
|
29
|
+
data: Date;
|
|
30
|
+
driverParam: string;
|
|
31
|
+
notNull: true;
|
|
32
|
+
hasDefault: true;
|
|
33
|
+
isPrimaryKey: false;
|
|
34
|
+
isAutoincrement: false;
|
|
35
|
+
hasRuntimeDefault: true;
|
|
36
|
+
enumValues: undefined;
|
|
37
|
+
baseColumn: never;
|
|
38
|
+
identity: undefined;
|
|
39
|
+
generated: undefined;
|
|
40
|
+
}, {}, {
|
|
41
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
42
|
+
}>;
|
|
43
|
+
updatedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
44
|
+
name: "updatedAt";
|
|
45
|
+
tableName: "Memories";
|
|
46
|
+
dataType: "custom";
|
|
47
|
+
columnType: "SQLiteCustomColumn";
|
|
48
|
+
data: Date;
|
|
49
|
+
driverParam: string;
|
|
50
|
+
notNull: true;
|
|
51
|
+
hasDefault: true;
|
|
52
|
+
isPrimaryKey: false;
|
|
53
|
+
isAutoincrement: false;
|
|
54
|
+
hasRuntimeDefault: true;
|
|
55
|
+
enumValues: undefined;
|
|
56
|
+
baseColumn: never;
|
|
57
|
+
identity: undefined;
|
|
58
|
+
generated: undefined;
|
|
59
|
+
}, {}, {
|
|
60
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
61
|
+
}>;
|
|
62
|
+
sessionId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
63
|
+
name: "sessionId";
|
|
64
|
+
tableName: "Memories";
|
|
65
|
+
dataType: "string";
|
|
66
|
+
columnType: "SQLiteText";
|
|
67
|
+
data: string;
|
|
68
|
+
driverParam: string;
|
|
69
|
+
notNull: false;
|
|
70
|
+
hasDefault: false;
|
|
71
|
+
isPrimaryKey: false;
|
|
72
|
+
isAutoincrement: false;
|
|
73
|
+
hasRuntimeDefault: false;
|
|
74
|
+
enumValues: [string, ...string[]];
|
|
75
|
+
baseColumn: never;
|
|
76
|
+
identity: undefined;
|
|
77
|
+
generated: undefined;
|
|
78
|
+
}, {}, {
|
|
79
|
+
length: number | undefined;
|
|
80
|
+
}>;
|
|
81
|
+
content: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
82
|
+
name: "content";
|
|
83
|
+
tableName: "Memories";
|
|
84
|
+
dataType: "custom";
|
|
85
|
+
columnType: "SQLiteCustomColumn";
|
|
86
|
+
data: unknown;
|
|
87
|
+
driverParam: string;
|
|
88
|
+
notNull: true;
|
|
89
|
+
hasDefault: false;
|
|
90
|
+
isPrimaryKey: false;
|
|
91
|
+
isAutoincrement: false;
|
|
92
|
+
hasRuntimeDefault: false;
|
|
93
|
+
enumValues: undefined;
|
|
94
|
+
baseColumn: never;
|
|
95
|
+
identity: undefined;
|
|
96
|
+
generated: undefined;
|
|
97
|
+
}, {}, {
|
|
98
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
99
|
+
}>;
|
|
100
|
+
};
|
|
101
|
+
dialect: "sqlite";
|
|
102
|
+
}>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Memories = void 0;
|
|
4
|
+
const type_js_1 = require("@aigne/sqlite/type.js");
|
|
5
|
+
const sqlite_core_1 = require("drizzle-orm/sqlite-core");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
exports.Memories = (0, sqlite_core_1.sqliteTable)("Memories", {
|
|
8
|
+
id: (0, sqlite_core_1.text)("id")
|
|
9
|
+
.notNull()
|
|
10
|
+
.primaryKey()
|
|
11
|
+
.$defaultFn(() => (0, uuid_1.v7)()),
|
|
12
|
+
createdAt: (0, type_js_1.datetime)("createdAt")
|
|
13
|
+
.notNull()
|
|
14
|
+
.$defaultFn(() => new Date()),
|
|
15
|
+
updatedAt: (0, type_js_1.datetime)("updatedAt")
|
|
16
|
+
.notNull()
|
|
17
|
+
.$defaultFn(() => new Date())
|
|
18
|
+
.$onUpdateFn(() => new Date()),
|
|
19
|
+
sessionId: (0, sqlite_core_1.text)("sessionId"),
|
|
20
|
+
content: (0, type_js_1.json)("content").notNull(),
|
|
21
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MemoryAgent, type MemoryAgentOptions } from "@aigne/core";
|
|
2
|
+
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
|
+
import { MemoryStorage } from "./storage.js";
|
|
4
|
+
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
5
|
+
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
+
}
|
|
7
|
+
export declare class DefaultMemory extends MemoryAgent {
|
|
8
|
+
constructor(options?: DefaultMemoryOptions);
|
|
9
|
+
storage: MemoryStorage;
|
|
10
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DefaultMemory = void 0;
|
|
7
|
+
const core_1 = require("@aigne/core");
|
|
8
|
+
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
9
|
+
const index_js_1 = require("./default-memory-storage/index.js");
|
|
10
|
+
const storage_js_1 = require("./storage.js");
|
|
11
|
+
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
12
|
+
class DefaultMemory extends core_1.MemoryAgent {
|
|
13
|
+
constructor(options = {}) {
|
|
14
|
+
super({
|
|
15
|
+
...options,
|
|
16
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
17
|
+
});
|
|
18
|
+
this.storage =
|
|
19
|
+
options.storage instanceof storage_js_1.MemoryStorage
|
|
20
|
+
? options.storage
|
|
21
|
+
: new index_js_1.DefaultMemoryStorage(options.storage);
|
|
22
|
+
if (!this.recorder)
|
|
23
|
+
this.recorder = new DefaultMemoryRecorder(this);
|
|
24
|
+
if (!this.retriever)
|
|
25
|
+
this.retriever = new DefaultMemoryRetriever(this);
|
|
26
|
+
}
|
|
27
|
+
storage;
|
|
28
|
+
}
|
|
29
|
+
exports.DefaultMemory = DefaultMemory;
|
|
30
|
+
class DefaultMemoryRetriever extends core_1.MemoryRetriever {
|
|
31
|
+
memory;
|
|
32
|
+
constructor(memory) {
|
|
33
|
+
super({});
|
|
34
|
+
this.memory = memory;
|
|
35
|
+
}
|
|
36
|
+
async process(input, options) {
|
|
37
|
+
const { result } = await this.memory.storage.search({ ...input, limit: input.limit ?? DEFAULT_RETRIEVE_MEMORY_COUNT }, options);
|
|
38
|
+
return { memories: result };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
class DefaultMemoryRecorder extends core_1.MemoryRecorder {
|
|
42
|
+
memory;
|
|
43
|
+
constructor(memory) {
|
|
44
|
+
super({});
|
|
45
|
+
this.memory = memory;
|
|
46
|
+
}
|
|
47
|
+
async process(input, options) {
|
|
48
|
+
const newMemories = [];
|
|
49
|
+
for (const content of input.content) {
|
|
50
|
+
const { result: [last], } = await this.memory.storage.search({ limit: 1 }, options);
|
|
51
|
+
if (!(0, fast_deep_equal_1.default)(last?.content, content)) {
|
|
52
|
+
const { result } = await this.memory.storage.create({ content }, options);
|
|
53
|
+
newMemories.push(result);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
memories: newMemories,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, Memory } from "@aigne/core";
|
|
2
|
+
export declare abstract class MemoryStorage {
|
|
3
|
+
abstract create(memory: Pick<Memory, "content">, options: AgentInvokeOptions): Promise<{
|
|
4
|
+
result: Memory;
|
|
5
|
+
}>;
|
|
6
|
+
abstract search(query: {
|
|
7
|
+
search?: string;
|
|
8
|
+
limit?: number;
|
|
9
|
+
}, options: AgentInvokeOptions): Promise<{
|
|
10
|
+
result: Memory[];
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
@@ -5,9 +5,9 @@ const promises_1 = require("node:fs/promises");
|
|
|
5
5
|
const node_path_1 = require("node:path");
|
|
6
6
|
const core_1 = require("@aigne/core");
|
|
7
7
|
const index_js_1 = require("@aigne/core/memory/index.js");
|
|
8
|
-
const fs_js_1 = require("@aigne/core/utils/fs.js");
|
|
9
8
|
const yaml_1 = require("yaml");
|
|
10
9
|
const zod_1 = require("zod");
|
|
10
|
+
const fs_js_1 = require("../utils/fs.js");
|
|
11
11
|
exports.MEMORY_FILE_NAME = "memory.yaml";
|
|
12
12
|
/**
|
|
13
13
|
* A memory implementation that stores and retrieves memories using the file system.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exists = exists;
|
|
4
|
+
exports.expandHome = expandHome;
|
|
5
|
+
const promises_1 = require("node:fs/promises");
|
|
6
|
+
const node_os_1 = require("node:os");
|
|
7
|
+
const node_path_1 = require("node:path");
|
|
8
|
+
async function exists(path) {
|
|
9
|
+
try {
|
|
10
|
+
await (0, promises_1.stat)(path);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
catch (e) {
|
|
14
|
+
if (e.code === "ENOENT") {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
throw e;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function expandHome(filepath) {
|
|
21
|
+
if (filepath.startsWith("~/") || filepath === "~") {
|
|
22
|
+
return (0, node_path_1.join)((0, node_os_1.homedir)(), filepath.slice(1));
|
|
23
|
+
}
|
|
24
|
+
return filepath;
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, Context, Memory } from "@aigne/core";
|
|
2
|
+
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
+
import { MemoryStorage } from "../storage.js";
|
|
4
|
+
export interface DefaultMemoryStorageOptions {
|
|
5
|
+
url?: string;
|
|
6
|
+
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
7
|
+
}
|
|
8
|
+
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
9
|
+
options?: DefaultMemoryStorageOptions | undefined;
|
|
10
|
+
constructor(options?: DefaultMemoryStorageOptions | undefined);
|
|
11
|
+
private _db;
|
|
12
|
+
private initSqlite;
|
|
13
|
+
get db(): Promise<import("drizzle-orm/libsql").LibSQLDatabase<Record<string, never>>>;
|
|
14
|
+
private convertMemory;
|
|
15
|
+
search(query: {
|
|
16
|
+
limit?: number;
|
|
17
|
+
}, { context }: AgentInvokeOptions): Promise<{
|
|
18
|
+
result: Memory[];
|
|
19
|
+
}>;
|
|
20
|
+
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
21
|
+
result: Memory;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export declare const Memories: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
2
|
+
name: "Memories";
|
|
3
|
+
schema: undefined;
|
|
4
|
+
columns: {
|
|
5
|
+
id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
6
|
+
name: "id";
|
|
7
|
+
tableName: "Memories";
|
|
8
|
+
dataType: "string";
|
|
9
|
+
columnType: "SQLiteText";
|
|
10
|
+
data: string;
|
|
11
|
+
driverParam: string;
|
|
12
|
+
notNull: true;
|
|
13
|
+
hasDefault: true;
|
|
14
|
+
isPrimaryKey: true;
|
|
15
|
+
isAutoincrement: false;
|
|
16
|
+
hasRuntimeDefault: true;
|
|
17
|
+
enumValues: [string, ...string[]];
|
|
18
|
+
baseColumn: never;
|
|
19
|
+
identity: undefined;
|
|
20
|
+
generated: undefined;
|
|
21
|
+
}, {}, {
|
|
22
|
+
length: number | undefined;
|
|
23
|
+
}>;
|
|
24
|
+
createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
25
|
+
name: "createdAt";
|
|
26
|
+
tableName: "Memories";
|
|
27
|
+
dataType: "custom";
|
|
28
|
+
columnType: "SQLiteCustomColumn";
|
|
29
|
+
data: Date;
|
|
30
|
+
driverParam: string;
|
|
31
|
+
notNull: true;
|
|
32
|
+
hasDefault: true;
|
|
33
|
+
isPrimaryKey: false;
|
|
34
|
+
isAutoincrement: false;
|
|
35
|
+
hasRuntimeDefault: true;
|
|
36
|
+
enumValues: undefined;
|
|
37
|
+
baseColumn: never;
|
|
38
|
+
identity: undefined;
|
|
39
|
+
generated: undefined;
|
|
40
|
+
}, {}, {
|
|
41
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
42
|
+
}>;
|
|
43
|
+
updatedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
44
|
+
name: "updatedAt";
|
|
45
|
+
tableName: "Memories";
|
|
46
|
+
dataType: "custom";
|
|
47
|
+
columnType: "SQLiteCustomColumn";
|
|
48
|
+
data: Date;
|
|
49
|
+
driverParam: string;
|
|
50
|
+
notNull: true;
|
|
51
|
+
hasDefault: true;
|
|
52
|
+
isPrimaryKey: false;
|
|
53
|
+
isAutoincrement: false;
|
|
54
|
+
hasRuntimeDefault: true;
|
|
55
|
+
enumValues: undefined;
|
|
56
|
+
baseColumn: never;
|
|
57
|
+
identity: undefined;
|
|
58
|
+
generated: undefined;
|
|
59
|
+
}, {}, {
|
|
60
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
61
|
+
}>;
|
|
62
|
+
sessionId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
63
|
+
name: "sessionId";
|
|
64
|
+
tableName: "Memories";
|
|
65
|
+
dataType: "string";
|
|
66
|
+
columnType: "SQLiteText";
|
|
67
|
+
data: string;
|
|
68
|
+
driverParam: string;
|
|
69
|
+
notNull: false;
|
|
70
|
+
hasDefault: false;
|
|
71
|
+
isPrimaryKey: false;
|
|
72
|
+
isAutoincrement: false;
|
|
73
|
+
hasRuntimeDefault: false;
|
|
74
|
+
enumValues: [string, ...string[]];
|
|
75
|
+
baseColumn: never;
|
|
76
|
+
identity: undefined;
|
|
77
|
+
generated: undefined;
|
|
78
|
+
}, {}, {
|
|
79
|
+
length: number | undefined;
|
|
80
|
+
}>;
|
|
81
|
+
content: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
82
|
+
name: "content";
|
|
83
|
+
tableName: "Memories";
|
|
84
|
+
dataType: "custom";
|
|
85
|
+
columnType: "SQLiteCustomColumn";
|
|
86
|
+
data: unknown;
|
|
87
|
+
driverParam: string;
|
|
88
|
+
notNull: true;
|
|
89
|
+
hasDefault: false;
|
|
90
|
+
isPrimaryKey: false;
|
|
91
|
+
isAutoincrement: false;
|
|
92
|
+
hasRuntimeDefault: false;
|
|
93
|
+
enumValues: undefined;
|
|
94
|
+
baseColumn: never;
|
|
95
|
+
identity: undefined;
|
|
96
|
+
generated: undefined;
|
|
97
|
+
}, {}, {
|
|
98
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
99
|
+
}>;
|
|
100
|
+
};
|
|
101
|
+
dialect: "sqlite";
|
|
102
|
+
}>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MemoryAgent, type MemoryAgentOptions } from "@aigne/core";
|
|
2
|
+
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
|
+
import { MemoryStorage } from "./storage.js";
|
|
4
|
+
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
5
|
+
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
+
}
|
|
7
|
+
export declare class DefaultMemory extends MemoryAgent {
|
|
8
|
+
constructor(options?: DefaultMemoryOptions);
|
|
9
|
+
storage: MemoryStorage;
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, Memory } from "@aigne/core";
|
|
2
|
+
export declare abstract class MemoryStorage {
|
|
3
|
+
abstract create(memory: Pick<Memory, "content">, options: AgentInvokeOptions): Promise<{
|
|
4
|
+
result: Memory;
|
|
5
|
+
}>;
|
|
6
|
+
abstract search(query: {
|
|
7
|
+
search?: string;
|
|
8
|
+
limit?: number;
|
|
9
|
+
}, options: AgentInvokeOptions): Promise<{
|
|
10
|
+
result: Memory[];
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, Context, Memory } from "@aigne/core";
|
|
2
|
+
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
+
import { MemoryStorage } from "../storage.js";
|
|
4
|
+
export interface DefaultMemoryStorageOptions {
|
|
5
|
+
url?: string;
|
|
6
|
+
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
7
|
+
}
|
|
8
|
+
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
9
|
+
options?: DefaultMemoryStorageOptions | undefined;
|
|
10
|
+
constructor(options?: DefaultMemoryStorageOptions | undefined);
|
|
11
|
+
private _db;
|
|
12
|
+
private initSqlite;
|
|
13
|
+
get db(): Promise<import("drizzle-orm/libsql").LibSQLDatabase<Record<string, never>>>;
|
|
14
|
+
private convertMemory;
|
|
15
|
+
search(query: {
|
|
16
|
+
limit?: number;
|
|
17
|
+
}, { context }: AgentInvokeOptions): Promise<{
|
|
18
|
+
result: Memory[];
|
|
19
|
+
}>;
|
|
20
|
+
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
21
|
+
result: Memory;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { initDatabase } from "@aigne/sqlite";
|
|
2
|
+
import { desc, eq, isNull } from "drizzle-orm";
|
|
3
|
+
import { MemoryStorage } from "../storage.js";
|
|
4
|
+
import { migrate } from "./migrate.js";
|
|
5
|
+
import { Memories } from "./models/memory.js";
|
|
6
|
+
const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
7
|
+
export class DefaultMemoryStorage extends MemoryStorage {
|
|
8
|
+
options;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super();
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
_db;
|
|
14
|
+
async initSqlite() {
|
|
15
|
+
const db = await initDatabase({ url: this.options?.url });
|
|
16
|
+
await migrate(db);
|
|
17
|
+
return db;
|
|
18
|
+
}
|
|
19
|
+
get db() {
|
|
20
|
+
this._db ??= this.initSqlite();
|
|
21
|
+
return this._db;
|
|
22
|
+
}
|
|
23
|
+
convertMemory(m) {
|
|
24
|
+
return {
|
|
25
|
+
id: m.id,
|
|
26
|
+
sessionId: m.sessionId,
|
|
27
|
+
content: m.content,
|
|
28
|
+
createdAt: m.createdAt.toISOString(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async search(query, { context }) {
|
|
32
|
+
const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
|
|
33
|
+
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
34
|
+
const db = await this.db;
|
|
35
|
+
const memories = await db
|
|
36
|
+
.select()
|
|
37
|
+
.from(Memories)
|
|
38
|
+
.where(sessionId ? eq(Memories.sessionId, sessionId) : isNull(Memories.sessionId))
|
|
39
|
+
.orderBy(desc(Memories.id))
|
|
40
|
+
.limit(limit)
|
|
41
|
+
.execute();
|
|
42
|
+
return {
|
|
43
|
+
result: memories.reverse().map(this.convertMemory),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async create(memory, { context }) {
|
|
47
|
+
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
48
|
+
const db = await this.db;
|
|
49
|
+
const [result] = await db
|
|
50
|
+
.insert(Memories)
|
|
51
|
+
.values({
|
|
52
|
+
...memory,
|
|
53
|
+
sessionId,
|
|
54
|
+
})
|
|
55
|
+
.returning()
|
|
56
|
+
.execute();
|
|
57
|
+
if (!result)
|
|
58
|
+
throw new Error("Failed to create memory");
|
|
59
|
+
return { result: this.convertMemory(result) };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm/sql";
|
|
2
|
+
import init from "./migrations/001-init.js";
|
|
3
|
+
export async function migrate(db) {
|
|
4
|
+
const migrations = [init];
|
|
5
|
+
const migrationsTable = "__drizzle_migrations";
|
|
6
|
+
const migrationTableCreate = sql `
|
|
7
|
+
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
|
|
8
|
+
id SERIAL PRIMARY KEY,
|
|
9
|
+
hash text NOT NULL
|
|
10
|
+
)
|
|
11
|
+
`;
|
|
12
|
+
await db.run(migrationTableCreate).execute();
|
|
13
|
+
const dbMigrations = await db
|
|
14
|
+
.values(sql `SELECT id, hash FROM ${sql.identifier(migrationsTable)} ORDER BY id DESC LIMIT 1`)
|
|
15
|
+
.execute();
|
|
16
|
+
const lastDbMigration = dbMigrations[0];
|
|
17
|
+
const queriesToRun = [];
|
|
18
|
+
for (const migration of migrations) {
|
|
19
|
+
if (!lastDbMigration || lastDbMigration[1] < migration.hash) {
|
|
20
|
+
queriesToRun.push(...migration.sql, `INSERT INTO \`${migrationsTable}\` ("hash") VALUES('${migration.hash}')`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
for (const query of queriesToRun) {
|
|
24
|
+
await db.run(query).execute();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export declare const Memories: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
2
|
+
name: "Memories";
|
|
3
|
+
schema: undefined;
|
|
4
|
+
columns: {
|
|
5
|
+
id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
6
|
+
name: "id";
|
|
7
|
+
tableName: "Memories";
|
|
8
|
+
dataType: "string";
|
|
9
|
+
columnType: "SQLiteText";
|
|
10
|
+
data: string;
|
|
11
|
+
driverParam: string;
|
|
12
|
+
notNull: true;
|
|
13
|
+
hasDefault: true;
|
|
14
|
+
isPrimaryKey: true;
|
|
15
|
+
isAutoincrement: false;
|
|
16
|
+
hasRuntimeDefault: true;
|
|
17
|
+
enumValues: [string, ...string[]];
|
|
18
|
+
baseColumn: never;
|
|
19
|
+
identity: undefined;
|
|
20
|
+
generated: undefined;
|
|
21
|
+
}, {}, {
|
|
22
|
+
length: number | undefined;
|
|
23
|
+
}>;
|
|
24
|
+
createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
25
|
+
name: "createdAt";
|
|
26
|
+
tableName: "Memories";
|
|
27
|
+
dataType: "custom";
|
|
28
|
+
columnType: "SQLiteCustomColumn";
|
|
29
|
+
data: Date;
|
|
30
|
+
driverParam: string;
|
|
31
|
+
notNull: true;
|
|
32
|
+
hasDefault: true;
|
|
33
|
+
isPrimaryKey: false;
|
|
34
|
+
isAutoincrement: false;
|
|
35
|
+
hasRuntimeDefault: true;
|
|
36
|
+
enumValues: undefined;
|
|
37
|
+
baseColumn: never;
|
|
38
|
+
identity: undefined;
|
|
39
|
+
generated: undefined;
|
|
40
|
+
}, {}, {
|
|
41
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
42
|
+
}>;
|
|
43
|
+
updatedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
44
|
+
name: "updatedAt";
|
|
45
|
+
tableName: "Memories";
|
|
46
|
+
dataType: "custom";
|
|
47
|
+
columnType: "SQLiteCustomColumn";
|
|
48
|
+
data: Date;
|
|
49
|
+
driverParam: string;
|
|
50
|
+
notNull: true;
|
|
51
|
+
hasDefault: true;
|
|
52
|
+
isPrimaryKey: false;
|
|
53
|
+
isAutoincrement: false;
|
|
54
|
+
hasRuntimeDefault: true;
|
|
55
|
+
enumValues: undefined;
|
|
56
|
+
baseColumn: never;
|
|
57
|
+
identity: undefined;
|
|
58
|
+
generated: undefined;
|
|
59
|
+
}, {}, {
|
|
60
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
61
|
+
}>;
|
|
62
|
+
sessionId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
63
|
+
name: "sessionId";
|
|
64
|
+
tableName: "Memories";
|
|
65
|
+
dataType: "string";
|
|
66
|
+
columnType: "SQLiteText";
|
|
67
|
+
data: string;
|
|
68
|
+
driverParam: string;
|
|
69
|
+
notNull: false;
|
|
70
|
+
hasDefault: false;
|
|
71
|
+
isPrimaryKey: false;
|
|
72
|
+
isAutoincrement: false;
|
|
73
|
+
hasRuntimeDefault: false;
|
|
74
|
+
enumValues: [string, ...string[]];
|
|
75
|
+
baseColumn: never;
|
|
76
|
+
identity: undefined;
|
|
77
|
+
generated: undefined;
|
|
78
|
+
}, {}, {
|
|
79
|
+
length: number | undefined;
|
|
80
|
+
}>;
|
|
81
|
+
content: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
82
|
+
name: "content";
|
|
83
|
+
tableName: "Memories";
|
|
84
|
+
dataType: "custom";
|
|
85
|
+
columnType: "SQLiteCustomColumn";
|
|
86
|
+
data: unknown;
|
|
87
|
+
driverParam: string;
|
|
88
|
+
notNull: true;
|
|
89
|
+
hasDefault: false;
|
|
90
|
+
isPrimaryKey: false;
|
|
91
|
+
isAutoincrement: false;
|
|
92
|
+
hasRuntimeDefault: false;
|
|
93
|
+
enumValues: undefined;
|
|
94
|
+
baseColumn: never;
|
|
95
|
+
identity: undefined;
|
|
96
|
+
generated: undefined;
|
|
97
|
+
}, {}, {
|
|
98
|
+
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
99
|
+
}>;
|
|
100
|
+
};
|
|
101
|
+
dialect: "sqlite";
|
|
102
|
+
}>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { datetime, json } from "@aigne/sqlite/type.js";
|
|
2
|
+
import { sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
3
|
+
import { v7 } from "uuid";
|
|
4
|
+
export const Memories = sqliteTable("Memories", {
|
|
5
|
+
id: text("id")
|
|
6
|
+
.notNull()
|
|
7
|
+
.primaryKey()
|
|
8
|
+
.$defaultFn(() => v7()),
|
|
9
|
+
createdAt: datetime("createdAt")
|
|
10
|
+
.notNull()
|
|
11
|
+
.$defaultFn(() => new Date()),
|
|
12
|
+
updatedAt: datetime("updatedAt")
|
|
13
|
+
.notNull()
|
|
14
|
+
.$defaultFn(() => new Date())
|
|
15
|
+
.$onUpdateFn(() => new Date()),
|
|
16
|
+
sessionId: text("sessionId"),
|
|
17
|
+
content: json("content").notNull(),
|
|
18
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MemoryAgent, type MemoryAgentOptions } from "@aigne/core";
|
|
2
|
+
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
|
+
import { MemoryStorage } from "./storage.js";
|
|
4
|
+
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
5
|
+
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
+
}
|
|
7
|
+
export declare class DefaultMemory extends MemoryAgent {
|
|
8
|
+
constructor(options?: DefaultMemoryOptions);
|
|
9
|
+
storage: MemoryStorage;
|
|
10
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { MemoryAgent, MemoryRecorder, MemoryRetriever, } from "@aigne/core";
|
|
2
|
+
import equal from "fast-deep-equal";
|
|
3
|
+
import { DefaultMemoryStorage, } from "./default-memory-storage/index.js";
|
|
4
|
+
import { MemoryStorage } from "./storage.js";
|
|
5
|
+
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
6
|
+
export class DefaultMemory extends MemoryAgent {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
super({
|
|
9
|
+
...options,
|
|
10
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
11
|
+
});
|
|
12
|
+
this.storage =
|
|
13
|
+
options.storage instanceof MemoryStorage
|
|
14
|
+
? options.storage
|
|
15
|
+
: new DefaultMemoryStorage(options.storage);
|
|
16
|
+
if (!this.recorder)
|
|
17
|
+
this.recorder = new DefaultMemoryRecorder(this);
|
|
18
|
+
if (!this.retriever)
|
|
19
|
+
this.retriever = new DefaultMemoryRetriever(this);
|
|
20
|
+
}
|
|
21
|
+
storage;
|
|
22
|
+
}
|
|
23
|
+
class DefaultMemoryRetriever extends MemoryRetriever {
|
|
24
|
+
memory;
|
|
25
|
+
constructor(memory) {
|
|
26
|
+
super({});
|
|
27
|
+
this.memory = memory;
|
|
28
|
+
}
|
|
29
|
+
async process(input, options) {
|
|
30
|
+
const { result } = await this.memory.storage.search({ ...input, limit: input.limit ?? DEFAULT_RETRIEVE_MEMORY_COUNT }, options);
|
|
31
|
+
return { memories: result };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class DefaultMemoryRecorder extends MemoryRecorder {
|
|
35
|
+
memory;
|
|
36
|
+
constructor(memory) {
|
|
37
|
+
super({});
|
|
38
|
+
this.memory = memory;
|
|
39
|
+
}
|
|
40
|
+
async process(input, options) {
|
|
41
|
+
const newMemories = [];
|
|
42
|
+
for (const content of input.content) {
|
|
43
|
+
const { result: [last], } = await this.memory.storage.search({ limit: 1 }, options);
|
|
44
|
+
if (!equal(last?.content, content)) {
|
|
45
|
+
const { result } = await this.memory.storage.create({ content }, options);
|
|
46
|
+
newMemories.push(result);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
memories: newMemories,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AgentInvokeOptions, Memory } from "@aigne/core";
|
|
2
|
+
export declare abstract class MemoryStorage {
|
|
3
|
+
abstract create(memory: Pick<Memory, "content">, options: AgentInvokeOptions): Promise<{
|
|
4
|
+
result: Memory;
|
|
5
|
+
}>;
|
|
6
|
+
abstract search(query: {
|
|
7
|
+
search?: string;
|
|
8
|
+
limit?: number;
|
|
9
|
+
}, options: AgentInvokeOptions): Promise<{
|
|
10
|
+
result: Memory[];
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
@@ -2,9 +2,9 @@ import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
|
2
2
|
import { dirname, isAbsolute, join, normalize, resolve } from "node:path";
|
|
3
3
|
import { AIAgent } from "@aigne/core";
|
|
4
4
|
import { MemoryAgent, MemoryRecorder, MemoryRetriever, newMemoryId, } from "@aigne/core/memory/index.js";
|
|
5
|
-
import { exists, expandHome } from "@aigne/core/utils/fs.js";
|
|
6
5
|
import { stringify } from "yaml";
|
|
7
6
|
import { z } from "zod";
|
|
7
|
+
import { exists, expandHome } from "../utils/fs.js";
|
|
8
8
|
export const MEMORY_FILE_NAME = "memory.yaml";
|
|
9
9
|
/**
|
|
10
10
|
* A memory implementation that stores and retrieves memories using the file system.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
export async function exists(path) {
|
|
5
|
+
try {
|
|
6
|
+
await stat(path);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch (e) {
|
|
10
|
+
if (e.code === "ENOENT") {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
throw e;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function expandHome(filepath) {
|
|
17
|
+
if (filepath.startsWith("~/") || filepath === "~") {
|
|
18
|
+
return join(homedir(), filepath.slice(1));
|
|
19
|
+
}
|
|
20
|
+
return filepath;
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigne/agent-library",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.1",
|
|
4
4
|
"description": "Collection of agent libraries for AIGNE framework",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -39,18 +39,21 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
+
"drizzle-orm": "^0.43.1",
|
|
43
|
+
"fastq": "^1.19.1",
|
|
42
44
|
"jsonata": "^2.0.6",
|
|
43
45
|
"jsonschema": "^1.5.0",
|
|
44
46
|
"to-json-schema": "^0.2.5",
|
|
45
|
-
"
|
|
47
|
+
"uuid": "^11.1.0",
|
|
46
48
|
"yaml": "^2.7.1",
|
|
47
49
|
"zod": "^3.24.4",
|
|
48
|
-
"@aigne/core": "^1.
|
|
49
|
-
"@aigne/
|
|
50
|
+
"@aigne/core": "^1.18.1",
|
|
51
|
+
"@aigne/sqlite": "^0.1.0",
|
|
52
|
+
"@aigne/openai": "^0.2.2"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
52
|
-
"@types/to-json-schema": "^0.2.4",
|
|
53
55
|
"@types/bun": "^1.2.12",
|
|
56
|
+
"@types/to-json-schema": "^0.2.4",
|
|
54
57
|
"npm-run-all": "^4.1.5",
|
|
55
58
|
"rimraf": "^6.0.1",
|
|
56
59
|
"typescript": "^5.8.3"
|