@aigne/agent-library 1.14.0 → 1.16.0
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 +34 -23
- package/lib/cjs/default-memory/default-memory-storage/index.d.ts +3 -0
- package/lib/cjs/default-memory/default-memory-storage/index.js +44 -15
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.js +6 -0
- package/lib/cjs/default-memory/index.d.ts +11 -1
- package/lib/cjs/default-memory/index.js +51 -27
- package/lib/cjs/fs-memory/index.js +6 -8
- package/lib/cjs/orchestrator/index.d.ts +1 -0
- package/lib/cjs/orchestrator/index.js +1 -0
- package/lib/dts/default-memory/default-memory-storage/index.d.ts +3 -0
- package/lib/dts/default-memory/index.d.ts +11 -1
- package/lib/dts/orchestrator/index.d.ts +1 -0
- package/lib/esm/default-memory/default-memory-storage/index.d.ts +3 -0
- package/lib/esm/default-memory/default-memory-storage/index.js +45 -16
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.js +6 -0
- package/lib/esm/default-memory/index.d.ts +11 -1
- package/lib/esm/default-memory/index.js +51 -24
- package/lib/esm/fs-memory/index.js +6 -8
- package/lib/esm/orchestrator/index.d.ts +1 -0
- package/lib/esm/orchestrator/index.js +1 -0
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
|
-
## [1.
|
|
1
|
+
## [1.15.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.14.0...agent-library-v1.15.0) (2025-06-24)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Features
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* support observability for cli and blocklet ([#155](https://github.com/AIGNE-io/aigne-framework/issues/155)) ([5baa705](https://github.com/AIGNE-io/aigne-framework/commit/5baa705a33cfdba1efc5ccbe18674c27513ca97d))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Dependencies
|
|
10
|
+
|
|
11
|
+
* The following workspace dependencies were updated
|
|
12
|
+
* dependencies
|
|
13
|
+
* @aigne/core bumped to 1.22.0
|
|
14
|
+
* @aigne/openai bumped to 0.3.4
|
|
15
|
+
|
|
16
|
+
## [1.16.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.15.0...agent-library-v1.16.0) (2025-06-25)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* support remember custom fields from message ([#174](https://github.com/AIGNE-io/aigne-framework/issues/174)) ([664069d](https://github.com/AIGNE-io/aigne-framework/commit/664069d343137f69d0c103b2b5eff545ab0051fb))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Dependencies
|
|
25
|
+
|
|
26
|
+
* The following workspace dependencies were updated
|
|
27
|
+
* dependencies
|
|
28
|
+
* @aigne/core bumped to 1.23.0
|
|
29
|
+
* @aigne/openai bumped to 0.3.5
|
|
7
30
|
|
|
8
31
|
## [1.14.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.13.2...agent-library-v1.14.0) (2025-06-20)
|
|
9
32
|
|
|
@@ -142,6 +165,15 @@
|
|
|
142
165
|
* @aigne/openai bumped to 0.2.1
|
|
143
166
|
* @aigne/sqlite bumped to 0.1.0
|
|
144
167
|
|
|
168
|
+
|
|
169
|
+
## [1.11.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.10.0...agent-library-v1.11.0) (2025-05-27)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
### Features
|
|
173
|
+
|
|
174
|
+
* add schema transform ([#35](https://github.com/AIGNE-io/aigne-framework/issues/35)) ([c7d9a2c](https://github.com/AIGNE-io/aigne-framework/commit/c7d9a2c9fcab8d384d4198db5ff6ba4603846cdf))
|
|
175
|
+
|
|
176
|
+
|
|
145
177
|
## [1.10.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.9.0...agent-library-v1.10.0) (2025-05-25)
|
|
146
178
|
|
|
147
179
|
|
|
@@ -280,24 +312,3 @@
|
|
|
280
312
|
### Bug Fixes
|
|
281
313
|
|
|
282
314
|
* rename @aigne/core-next to @aigne/core ([3a81009](https://github.com/AIGNE-io/aigne-framework/commit/3a8100962c81813217b687ae28e8de604419c622))
|
|
283
|
-
|
|
284
|
-
## 1.1.0-beta.17 (2025-3-18)
|
|
285
|
-
|
|
286
|
-
- chore: add support for esm module
|
|
287
|
-
|
|
288
|
-
## 1.1.0-beta.16 (2025-3-18)
|
|
289
|
-
|
|
290
|
-
- chore: add puppeteer in linux need docker_container
|
|
291
|
-
|
|
292
|
-
## 1.1.0-beta.15 (2025-3-18)
|
|
293
|
-
|
|
294
|
-
- chore: make coverage report as text to terminal
|
|
295
|
-
- chore: update contributing docs
|
|
296
|
-
|
|
297
|
-
## 1.1.0-beta.14 (2025-3-18)
|
|
298
|
-
|
|
299
|
-
- chore(example): add code-execution example
|
|
300
|
-
|
|
301
|
-
## 1.1.0-beta.13 (2025-3-18)
|
|
302
|
-
|
|
303
|
-
- feat: add OrchestratorAgent in agent library
|
|
@@ -5,6 +5,7 @@ import { MemoryStorage } from "../storage.js";
|
|
|
5
5
|
export interface DefaultMemoryStorageOptions {
|
|
6
6
|
url?: string;
|
|
7
7
|
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
8
|
+
enableFTS?: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
10
11
|
options?: DefaultMemoryStorageOptions | undefined;
|
|
@@ -14,6 +15,7 @@ export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
|
14
15
|
get db(): Promise<SqliteRemoteDatabase<Record<string, never>>>;
|
|
15
16
|
private convertMemory;
|
|
16
17
|
search(query: {
|
|
18
|
+
search?: string;
|
|
17
19
|
limit?: number;
|
|
18
20
|
}, { context }: AgentInvokeOptions): Promise<{
|
|
19
21
|
result: Memory[];
|
|
@@ -21,4 +23,5 @@ export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
|
21
23
|
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
22
24
|
result: Memory;
|
|
23
25
|
}>;
|
|
26
|
+
protected segment(str: string): string[];
|
|
24
27
|
}
|
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.DefaultMemoryStorage = void 0;
|
|
4
4
|
const sqlite_1 = require("@aigne/sqlite");
|
|
5
5
|
const drizzle_orm_1 = require("drizzle-orm");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const yaml_1 = require("yaml");
|
|
6
8
|
const storage_js_1 = require("../storage.js");
|
|
7
9
|
const migrate_js_1 = require("./migrate.js");
|
|
8
10
|
const memory_js_1 = require("./models/memory.js");
|
|
@@ -35,13 +37,28 @@ class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
|
|
|
35
37
|
const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
|
|
36
38
|
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
37
39
|
const db = await this.db;
|
|
38
|
-
const memories =
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
const memories = this.options?.enableFTS && query.search
|
|
41
|
+
? await db
|
|
42
|
+
.select({
|
|
43
|
+
id: memory_js_1.Memories.id,
|
|
44
|
+
sessionId: memory_js_1.Memories.sessionId,
|
|
45
|
+
content: memory_js_1.Memories.content,
|
|
46
|
+
createdAt: memory_js_1.Memories.createdAt,
|
|
47
|
+
updatedAt: memory_js_1.Memories.updatedAt,
|
|
48
|
+
})
|
|
49
|
+
.from(memory_js_1.Memories)
|
|
50
|
+
.innerJoin((0, drizzle_orm_1.sql) `Memories_fts`, (0, drizzle_orm_1.sql) `Memories_fts.id = ${memory_js_1.Memories.id}`)
|
|
51
|
+
.where((0, drizzle_orm_1.sql) `Memories_fts MATCH ${query.search}`)
|
|
52
|
+
.orderBy((0, drizzle_orm_1.sql) `bm25(Memories_fts)`)
|
|
53
|
+
.limit(limit)
|
|
54
|
+
.execute()
|
|
55
|
+
: await db
|
|
56
|
+
.select()
|
|
57
|
+
.from(memory_js_1.Memories)
|
|
58
|
+
.where(sessionId ? (0, drizzle_orm_1.eq)(memory_js_1.Memories.sessionId, sessionId) : (0, drizzle_orm_1.isNull)(memory_js_1.Memories.sessionId))
|
|
59
|
+
.orderBy((0, drizzle_orm_1.desc)(memory_js_1.Memories.id))
|
|
60
|
+
.limit(limit)
|
|
61
|
+
.execute();
|
|
45
62
|
return {
|
|
46
63
|
result: memories.reverse().map(this.convertMemory),
|
|
47
64
|
};
|
|
@@ -49,17 +66,29 @@ class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
|
|
|
49
66
|
async create(memory, { context }) {
|
|
50
67
|
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
51
68
|
const db = await this.db;
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
const id = (0, uuid_1.v7)();
|
|
70
|
+
const [[result]] = await Promise.all([
|
|
71
|
+
db
|
|
72
|
+
.insert(memory_js_1.Memories)
|
|
73
|
+
.values({
|
|
74
|
+
...memory,
|
|
75
|
+
id,
|
|
76
|
+
content: memory.content,
|
|
77
|
+
sessionId,
|
|
78
|
+
})
|
|
79
|
+
.returning()
|
|
80
|
+
.execute(),
|
|
81
|
+
this.options?.enableFTS &&
|
|
82
|
+
db.run((0, drizzle_orm_1.sql) `\
|
|
83
|
+
insert into Memories_fts (id, content)
|
|
84
|
+
values (${id}, ${this.segment((0, yaml_1.stringify)(memory.content)).join(" ")})`),
|
|
85
|
+
]);
|
|
60
86
|
if (!result)
|
|
61
87
|
throw new Error("Failed to create memory");
|
|
62
88
|
return { result: this.convertMemory(result) };
|
|
63
89
|
}
|
|
90
|
+
segment(str) {
|
|
91
|
+
return Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str)).map((i) => i.segment);
|
|
92
|
+
}
|
|
64
93
|
}
|
|
65
94
|
exports.DefaultMemoryStorage = DefaultMemoryStorage;
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import { MemoryAgent, type MemoryAgentOptions } from "@aigne/core";
|
|
1
|
+
import { type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
2
2
|
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
3
|
import { MemoryStorage } from "./storage.js";
|
|
4
4
|
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
5
5
|
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
+
recorderOptions?: Omit<DefaultMemoryRecorderOptions, "storage">;
|
|
7
|
+
retrieverOptions?: Omit<DefaultMemoryRetrieverOptions, "storage">;
|
|
6
8
|
}
|
|
7
9
|
export declare class DefaultMemory extends MemoryAgent {
|
|
8
10
|
constructor(options?: DefaultMemoryOptions);
|
|
9
11
|
storage: MemoryStorage;
|
|
10
12
|
}
|
|
13
|
+
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
14
|
+
storage: MemoryStorage;
|
|
15
|
+
defaultRetrieveMemoryCount?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
18
|
+
storage: MemoryStorage;
|
|
19
|
+
rememberFromMessageKey?: string | string[];
|
|
20
|
+
}
|
|
@@ -1,60 +1,84 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.DefaultMemory = void 0;
|
|
7
4
|
const core_1 = require("@aigne/core");
|
|
8
|
-
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
9
5
|
const index_js_1 = require("./default-memory-storage/index.js");
|
|
10
6
|
const storage_js_1 = require("./storage.js");
|
|
11
7
|
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
12
8
|
class DefaultMemory extends core_1.MemoryAgent {
|
|
13
9
|
constructor(options = {}) {
|
|
10
|
+
const storage = options.storage instanceof storage_js_1.MemoryStorage
|
|
11
|
+
? options.storage
|
|
12
|
+
: new index_js_1.DefaultMemoryStorage(options.storage);
|
|
14
13
|
super({
|
|
15
14
|
...options,
|
|
15
|
+
recorder: options.recorder ??
|
|
16
|
+
new DefaultMemoryRecorder({
|
|
17
|
+
...options.recorderOptions,
|
|
18
|
+
storage,
|
|
19
|
+
}),
|
|
20
|
+
retriever: options.retriever ??
|
|
21
|
+
new DefaultMemoryRetriever({
|
|
22
|
+
...options.retrieverOptions,
|
|
23
|
+
storage,
|
|
24
|
+
}),
|
|
16
25
|
autoUpdate: options.autoUpdate ?? true,
|
|
17
26
|
});
|
|
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);
|
|
27
|
+
this.storage = storage;
|
|
26
28
|
}
|
|
27
29
|
storage;
|
|
28
30
|
}
|
|
29
31
|
exports.DefaultMemory = DefaultMemory;
|
|
30
32
|
class DefaultMemoryRetriever extends core_1.MemoryRetriever {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
33
|
+
constructor(options) {
|
|
34
|
+
super(options);
|
|
35
|
+
this.storage = options.storage;
|
|
36
|
+
this.defaultRetrieveMemoryCount = options.defaultRetrieveMemoryCount;
|
|
35
37
|
}
|
|
38
|
+
storage;
|
|
39
|
+
defaultRetrieveMemoryCount;
|
|
36
40
|
async process(input, options) {
|
|
37
|
-
const { result } = await this.
|
|
41
|
+
const { result } = await this.storage.search({
|
|
42
|
+
...input,
|
|
43
|
+
limit: input.limit ?? this.defaultRetrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT,
|
|
44
|
+
}, options);
|
|
38
45
|
return { memories: result };
|
|
39
46
|
}
|
|
40
47
|
}
|
|
41
48
|
class DefaultMemoryRecorder extends core_1.MemoryRecorder {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.
|
|
49
|
+
constructor(options) {
|
|
50
|
+
super(options);
|
|
51
|
+
this.storage = options.storage;
|
|
52
|
+
this.rememberFromMessageKey = options.rememberFromMessageKey;
|
|
46
53
|
}
|
|
54
|
+
storage;
|
|
55
|
+
rememberFromMessageKey;
|
|
47
56
|
async process(input, options) {
|
|
48
57
|
const newMemories = [];
|
|
49
|
-
for (const
|
|
50
|
-
const { result
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
for (const item of input.content) {
|
|
59
|
+
const { result } = await this.storage.create({
|
|
60
|
+
content: {
|
|
61
|
+
...item,
|
|
62
|
+
content: this.processMemoryInput(item.content),
|
|
63
|
+
},
|
|
64
|
+
}, options);
|
|
65
|
+
newMemories.push(result);
|
|
55
66
|
}
|
|
56
67
|
return {
|
|
57
68
|
memories: newMemories,
|
|
58
69
|
};
|
|
59
70
|
}
|
|
71
|
+
processMemoryInput(content) {
|
|
72
|
+
const keys = Array.isArray(this.rememberFromMessageKey)
|
|
73
|
+
? this.rememberFromMessageKey
|
|
74
|
+
: this.rememberFromMessageKey
|
|
75
|
+
? [this.rememberFromMessageKey]
|
|
76
|
+
: [];
|
|
77
|
+
if (!keys.length)
|
|
78
|
+
return content;
|
|
79
|
+
return Object.entries(content)
|
|
80
|
+
.filter(([key]) => keys.includes(key))
|
|
81
|
+
.map(([_, value]) => (typeof value === "string" ? value : JSON.stringify(value)))
|
|
82
|
+
.join("\n");
|
|
83
|
+
}
|
|
60
84
|
}
|
|
@@ -27,20 +27,18 @@ class FSMemory extends index_js_1.MemoryAgent {
|
|
|
27
27
|
const memoryFileName = (0, node_path_1.join)(rootDir, exports.MEMORY_FILE_NAME);
|
|
28
28
|
super({
|
|
29
29
|
...options,
|
|
30
|
-
|
|
31
|
-
});
|
|
32
|
-
this.recorder =
|
|
33
|
-
options.recorder ??
|
|
30
|
+
recorder: options.recorder ??
|
|
34
31
|
new FSMemoryRecorder({
|
|
35
32
|
memoryFileName,
|
|
36
33
|
...options.recorderOptions,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
options.retriever ??
|
|
34
|
+
}),
|
|
35
|
+
retriever: options.retriever ??
|
|
40
36
|
new FSMemoryRetriever({
|
|
41
37
|
memoryFileName,
|
|
42
38
|
...options.retrieverOptions,
|
|
43
|
-
})
|
|
39
|
+
}),
|
|
40
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
41
|
+
});
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
exports.FSMemory = FSMemory;
|
|
@@ -62,6 +62,7 @@ export interface OrchestratorAgentOptions<I extends Message = Message, O extends
|
|
|
62
62
|
* - Synthesizes final result through completer
|
|
63
63
|
*/
|
|
64
64
|
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
65
|
+
tag: string;
|
|
65
66
|
/**
|
|
66
67
|
* Factory method to create an OrchestratorAgent instance
|
|
67
68
|
* @param options - Configuration options for the Orchestrator Agent
|
|
@@ -51,6 +51,7 @@ __exportStar(require("./orchestrator-prompts.js"), exports);
|
|
|
51
51
|
* - Synthesizes final result through completer
|
|
52
52
|
*/
|
|
53
53
|
class OrchestratorAgent extends core_1.Agent {
|
|
54
|
+
tag = "OrchestratorAgent";
|
|
54
55
|
/**
|
|
55
56
|
* Factory method to create an OrchestratorAgent instance
|
|
56
57
|
* @param options - Configuration options for the Orchestrator Agent
|
|
@@ -5,6 +5,7 @@ import { MemoryStorage } from "../storage.js";
|
|
|
5
5
|
export interface DefaultMemoryStorageOptions {
|
|
6
6
|
url?: string;
|
|
7
7
|
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
8
|
+
enableFTS?: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
10
11
|
options?: DefaultMemoryStorageOptions | undefined;
|
|
@@ -14,6 +15,7 @@ export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
|
14
15
|
get db(): Promise<SqliteRemoteDatabase<Record<string, never>>>;
|
|
15
16
|
private convertMemory;
|
|
16
17
|
search(query: {
|
|
18
|
+
search?: string;
|
|
17
19
|
limit?: number;
|
|
18
20
|
}, { context }: AgentInvokeOptions): Promise<{
|
|
19
21
|
result: Memory[];
|
|
@@ -21,4 +23,5 @@ export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
|
21
23
|
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
22
24
|
result: Memory;
|
|
23
25
|
}>;
|
|
26
|
+
protected segment(str: string): string[];
|
|
24
27
|
}
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import { MemoryAgent, type MemoryAgentOptions } from "@aigne/core";
|
|
1
|
+
import { type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
2
2
|
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
3
|
import { MemoryStorage } from "./storage.js";
|
|
4
4
|
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
5
5
|
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
+
recorderOptions?: Omit<DefaultMemoryRecorderOptions, "storage">;
|
|
7
|
+
retrieverOptions?: Omit<DefaultMemoryRetrieverOptions, "storage">;
|
|
6
8
|
}
|
|
7
9
|
export declare class DefaultMemory extends MemoryAgent {
|
|
8
10
|
constructor(options?: DefaultMemoryOptions);
|
|
9
11
|
storage: MemoryStorage;
|
|
10
12
|
}
|
|
13
|
+
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
14
|
+
storage: MemoryStorage;
|
|
15
|
+
defaultRetrieveMemoryCount?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
18
|
+
storage: MemoryStorage;
|
|
19
|
+
rememberFromMessageKey?: string | string[];
|
|
20
|
+
}
|
|
@@ -62,6 +62,7 @@ export interface OrchestratorAgentOptions<I extends Message = Message, O extends
|
|
|
62
62
|
* - Synthesizes final result through completer
|
|
63
63
|
*/
|
|
64
64
|
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
65
|
+
tag: string;
|
|
65
66
|
/**
|
|
66
67
|
* Factory method to create an OrchestratorAgent instance
|
|
67
68
|
* @param options - Configuration options for the Orchestrator Agent
|
|
@@ -5,6 +5,7 @@ import { MemoryStorage } from "../storage.js";
|
|
|
5
5
|
export interface DefaultMemoryStorageOptions {
|
|
6
6
|
url?: string;
|
|
7
7
|
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
8
|
+
enableFTS?: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
10
11
|
options?: DefaultMemoryStorageOptions | undefined;
|
|
@@ -14,6 +15,7 @@ export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
|
14
15
|
get db(): Promise<SqliteRemoteDatabase<Record<string, never>>>;
|
|
15
16
|
private convertMemory;
|
|
16
17
|
search(query: {
|
|
18
|
+
search?: string;
|
|
17
19
|
limit?: number;
|
|
18
20
|
}, { context }: AgentInvokeOptions): Promise<{
|
|
19
21
|
result: Memory[];
|
|
@@ -21,4 +23,5 @@ export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
|
21
23
|
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
22
24
|
result: Memory;
|
|
23
25
|
}>;
|
|
26
|
+
protected segment(str: string): string[];
|
|
24
27
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { initDatabase } from "@aigne/sqlite";
|
|
2
|
-
import { desc, eq, isNull } from "drizzle-orm";
|
|
2
|
+
import { desc, eq, isNull, sql } from "drizzle-orm";
|
|
3
|
+
import { v7 } from "uuid";
|
|
4
|
+
import { stringify } from "yaml";
|
|
3
5
|
import { MemoryStorage } from "../storage.js";
|
|
4
6
|
import { migrate } from "./migrate.js";
|
|
5
7
|
import { Memories } from "./models/memory.js";
|
|
@@ -32,13 +34,28 @@ export class DefaultMemoryStorage extends MemoryStorage {
|
|
|
32
34
|
const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
|
|
33
35
|
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
34
36
|
const db = await this.db;
|
|
35
|
-
const memories =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const memories = this.options?.enableFTS && query.search
|
|
38
|
+
? await db
|
|
39
|
+
.select({
|
|
40
|
+
id: Memories.id,
|
|
41
|
+
sessionId: Memories.sessionId,
|
|
42
|
+
content: Memories.content,
|
|
43
|
+
createdAt: Memories.createdAt,
|
|
44
|
+
updatedAt: Memories.updatedAt,
|
|
45
|
+
})
|
|
46
|
+
.from(Memories)
|
|
47
|
+
.innerJoin(sql `Memories_fts`, sql `Memories_fts.id = ${Memories.id}`)
|
|
48
|
+
.where(sql `Memories_fts MATCH ${query.search}`)
|
|
49
|
+
.orderBy(sql `bm25(Memories_fts)`)
|
|
50
|
+
.limit(limit)
|
|
51
|
+
.execute()
|
|
52
|
+
: await db
|
|
53
|
+
.select()
|
|
54
|
+
.from(Memories)
|
|
55
|
+
.where(sessionId ? eq(Memories.sessionId, sessionId) : isNull(Memories.sessionId))
|
|
56
|
+
.orderBy(desc(Memories.id))
|
|
57
|
+
.limit(limit)
|
|
58
|
+
.execute();
|
|
42
59
|
return {
|
|
43
60
|
result: memories.reverse().map(this.convertMemory),
|
|
44
61
|
};
|
|
@@ -46,16 +63,28 @@ export class DefaultMemoryStorage extends MemoryStorage {
|
|
|
46
63
|
async create(memory, { context }) {
|
|
47
64
|
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
48
65
|
const db = await this.db;
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
66
|
+
const id = v7();
|
|
67
|
+
const [[result]] = await Promise.all([
|
|
68
|
+
db
|
|
69
|
+
.insert(Memories)
|
|
70
|
+
.values({
|
|
71
|
+
...memory,
|
|
72
|
+
id,
|
|
73
|
+
content: memory.content,
|
|
74
|
+
sessionId,
|
|
75
|
+
})
|
|
76
|
+
.returning()
|
|
77
|
+
.execute(),
|
|
78
|
+
this.options?.enableFTS &&
|
|
79
|
+
db.run(sql `\
|
|
80
|
+
insert into Memories_fts (id, content)
|
|
81
|
+
values (${id}, ${this.segment(stringify(memory.content)).join(" ")})`),
|
|
82
|
+
]);
|
|
57
83
|
if (!result)
|
|
58
84
|
throw new Error("Failed to create memory");
|
|
59
85
|
return { result: this.convertMemory(result) };
|
|
60
86
|
}
|
|
87
|
+
segment(str) {
|
|
88
|
+
return Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str)).map((i) => i.segment);
|
|
89
|
+
}
|
|
61
90
|
}
|
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import { MemoryAgent, type MemoryAgentOptions } from "@aigne/core";
|
|
1
|
+
import { type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
2
2
|
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
3
|
import { MemoryStorage } from "./storage.js";
|
|
4
4
|
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
5
5
|
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
+
recorderOptions?: Omit<DefaultMemoryRecorderOptions, "storage">;
|
|
7
|
+
retrieverOptions?: Omit<DefaultMemoryRetrieverOptions, "storage">;
|
|
6
8
|
}
|
|
7
9
|
export declare class DefaultMemory extends MemoryAgent {
|
|
8
10
|
constructor(options?: DefaultMemoryOptions);
|
|
9
11
|
storage: MemoryStorage;
|
|
10
12
|
}
|
|
13
|
+
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
14
|
+
storage: MemoryStorage;
|
|
15
|
+
defaultRetrieveMemoryCount?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
18
|
+
storage: MemoryStorage;
|
|
19
|
+
rememberFromMessageKey?: string | string[];
|
|
20
|
+
}
|
|
@@ -1,53 +1,80 @@
|
|
|
1
1
|
import { MemoryAgent, MemoryRecorder, MemoryRetriever, } from "@aigne/core";
|
|
2
|
-
import equal from "fast-deep-equal";
|
|
3
2
|
import { DefaultMemoryStorage, } from "./default-memory-storage/index.js";
|
|
4
3
|
import { MemoryStorage } from "./storage.js";
|
|
5
4
|
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
6
5
|
export class DefaultMemory extends MemoryAgent {
|
|
7
6
|
constructor(options = {}) {
|
|
7
|
+
const storage = options.storage instanceof MemoryStorage
|
|
8
|
+
? options.storage
|
|
9
|
+
: new DefaultMemoryStorage(options.storage);
|
|
8
10
|
super({
|
|
9
11
|
...options,
|
|
12
|
+
recorder: options.recorder ??
|
|
13
|
+
new DefaultMemoryRecorder({
|
|
14
|
+
...options.recorderOptions,
|
|
15
|
+
storage,
|
|
16
|
+
}),
|
|
17
|
+
retriever: options.retriever ??
|
|
18
|
+
new DefaultMemoryRetriever({
|
|
19
|
+
...options.retrieverOptions,
|
|
20
|
+
storage,
|
|
21
|
+
}),
|
|
10
22
|
autoUpdate: options.autoUpdate ?? true,
|
|
11
23
|
});
|
|
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);
|
|
24
|
+
this.storage = storage;
|
|
20
25
|
}
|
|
21
26
|
storage;
|
|
22
27
|
}
|
|
23
28
|
class DefaultMemoryRetriever extends MemoryRetriever {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.
|
|
29
|
+
constructor(options) {
|
|
30
|
+
super(options);
|
|
31
|
+
this.storage = options.storage;
|
|
32
|
+
this.defaultRetrieveMemoryCount = options.defaultRetrieveMemoryCount;
|
|
28
33
|
}
|
|
34
|
+
storage;
|
|
35
|
+
defaultRetrieveMemoryCount;
|
|
29
36
|
async process(input, options) {
|
|
30
|
-
const { result } = await this.
|
|
37
|
+
const { result } = await this.storage.search({
|
|
38
|
+
...input,
|
|
39
|
+
limit: input.limit ?? this.defaultRetrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT,
|
|
40
|
+
}, options);
|
|
31
41
|
return { memories: result };
|
|
32
42
|
}
|
|
33
43
|
}
|
|
34
44
|
class DefaultMemoryRecorder extends MemoryRecorder {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.
|
|
45
|
+
constructor(options) {
|
|
46
|
+
super(options);
|
|
47
|
+
this.storage = options.storage;
|
|
48
|
+
this.rememberFromMessageKey = options.rememberFromMessageKey;
|
|
39
49
|
}
|
|
50
|
+
storage;
|
|
51
|
+
rememberFromMessageKey;
|
|
40
52
|
async process(input, options) {
|
|
41
53
|
const newMemories = [];
|
|
42
|
-
for (const
|
|
43
|
-
const { result
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
for (const item of input.content) {
|
|
55
|
+
const { result } = await this.storage.create({
|
|
56
|
+
content: {
|
|
57
|
+
...item,
|
|
58
|
+
content: this.processMemoryInput(item.content),
|
|
59
|
+
},
|
|
60
|
+
}, options);
|
|
61
|
+
newMemories.push(result);
|
|
48
62
|
}
|
|
49
63
|
return {
|
|
50
64
|
memories: newMemories,
|
|
51
65
|
};
|
|
52
66
|
}
|
|
67
|
+
processMemoryInput(content) {
|
|
68
|
+
const keys = Array.isArray(this.rememberFromMessageKey)
|
|
69
|
+
? this.rememberFromMessageKey
|
|
70
|
+
: this.rememberFromMessageKey
|
|
71
|
+
? [this.rememberFromMessageKey]
|
|
72
|
+
: [];
|
|
73
|
+
if (!keys.length)
|
|
74
|
+
return content;
|
|
75
|
+
return Object.entries(content)
|
|
76
|
+
.filter(([key]) => keys.includes(key))
|
|
77
|
+
.map(([_, value]) => (typeof value === "string" ? value : JSON.stringify(value)))
|
|
78
|
+
.join("\n");
|
|
79
|
+
}
|
|
53
80
|
}
|
|
@@ -24,20 +24,18 @@ export class FSMemory extends MemoryAgent {
|
|
|
24
24
|
const memoryFileName = join(rootDir, MEMORY_FILE_NAME);
|
|
25
25
|
super({
|
|
26
26
|
...options,
|
|
27
|
-
|
|
28
|
-
});
|
|
29
|
-
this.recorder =
|
|
30
|
-
options.recorder ??
|
|
27
|
+
recorder: options.recorder ??
|
|
31
28
|
new FSMemoryRecorder({
|
|
32
29
|
memoryFileName,
|
|
33
30
|
...options.recorderOptions,
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
options.retriever ??
|
|
31
|
+
}),
|
|
32
|
+
retriever: options.retriever ??
|
|
37
33
|
new FSMemoryRetriever({
|
|
38
34
|
memoryFileName,
|
|
39
35
|
...options.retrieverOptions,
|
|
40
|
-
})
|
|
36
|
+
}),
|
|
37
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
38
|
+
});
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
class FSMemoryRetriever extends MemoryRetriever {
|
|
@@ -62,6 +62,7 @@ export interface OrchestratorAgentOptions<I extends Message = Message, O extends
|
|
|
62
62
|
* - Synthesizes final result through completer
|
|
63
63
|
*/
|
|
64
64
|
export declare class OrchestratorAgent<I extends Message = Message, O extends Message = Message> extends Agent<I, O> {
|
|
65
|
+
tag: string;
|
|
65
66
|
/**
|
|
66
67
|
* Factory method to create an OrchestratorAgent instance
|
|
67
68
|
* @param options - Configuration options for the Orchestrator Agent
|
|
@@ -31,6 +31,7 @@ export * from "./orchestrator-prompts.js";
|
|
|
31
31
|
* - Synthesizes final result through completer
|
|
32
32
|
*/
|
|
33
33
|
export class OrchestratorAgent extends Agent {
|
|
34
|
+
tag = "OrchestratorAgent";
|
|
34
35
|
/**
|
|
35
36
|
* Factory method to create an OrchestratorAgent instance
|
|
36
37
|
* @param options - Configuration options for the Orchestrator Agent
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigne/agent-library",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"description": "Collection of agent libraries for AIGNE framework",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
"uuid": "^11.1.0",
|
|
48
48
|
"yaml": "^2.7.1",
|
|
49
49
|
"zod": "^3.24.4",
|
|
50
|
-
"@aigne/core": "^1.
|
|
51
|
-
"@aigne/
|
|
52
|
-
"@aigne/
|
|
50
|
+
"@aigne/core": "^1.23.0",
|
|
51
|
+
"@aigne/openai": "^0.3.5",
|
|
52
|
+
"@aigne/sqlite": "^0.1.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/bun": "^1.2.12",
|