@aigne/agent-library 1.16.0 → 1.17.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 +30 -0
- package/lib/cjs/default-memory/default-memory-storage/index.js +9 -10
- package/lib/cjs/default-memory/index.d.ts +17 -1
- package/lib/cjs/default-memory/index.js +46 -16
- package/lib/dts/default-memory/index.d.ts +17 -1
- package/lib/esm/default-memory/default-memory-storage/index.js +9 -10
- package/lib/esm/default-memory/index.d.ts +17 -1
- package/lib/esm/default-memory/index.js +46 -16
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -13,6 +13,36 @@
|
|
|
13
13
|
* @aigne/core bumped to 1.22.0
|
|
14
14
|
* @aigne/openai bumped to 0.3.4
|
|
15
15
|
|
|
16
|
+
## [1.17.0](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.16.1...agent-library-v1.17.0) (2025-06-26)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* **transport:** support invoke server side chat model ([#182](https://github.com/AIGNE-io/aigne-framework/issues/182)) ([f81a1bf](https://github.com/AIGNE-io/aigne-framework/commit/f81a1bf883abda1845ccee09b270e5f583e287ab))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Dependencies
|
|
25
|
+
|
|
26
|
+
* The following workspace dependencies were updated
|
|
27
|
+
* dependencies
|
|
28
|
+
* @aigne/core bumped to 1.24.0
|
|
29
|
+
* @aigne/openai bumped to 0.4.0
|
|
30
|
+
|
|
31
|
+
## [1.16.1](https://github.com/AIGNE-io/aigne-framework/compare/agent-library-v1.16.0...agent-library-v1.16.1) (2025-06-25)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Bug Fixes
|
|
35
|
+
|
|
36
|
+
* **core:** pass input/output to MemoryAgent directily ([#178](https://github.com/AIGNE-io/aigne-framework/issues/178)) ([3b20e33](https://github.com/AIGNE-io/aigne-framework/commit/3b20e33f1eefc81ac1e009b1afff14fca46644b1))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Dependencies
|
|
40
|
+
|
|
41
|
+
* The following workspace dependencies were updated
|
|
42
|
+
* dependencies
|
|
43
|
+
* @aigne/core bumped to 1.23.1
|
|
44
|
+
* @aigne/openai bumped to 0.3.6
|
|
45
|
+
|
|
16
46
|
## [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
47
|
|
|
18
48
|
|
|
@@ -39,19 +39,14 @@ class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
|
|
|
39
39
|
const db = await this.db;
|
|
40
40
|
const memories = this.options?.enableFTS && query.search
|
|
41
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
|
-
})
|
|
42
|
+
.select()
|
|
49
43
|
.from(memory_js_1.Memories)
|
|
50
44
|
.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}`)
|
|
45
|
+
.where((0, drizzle_orm_1.sql) `Memories_fts MATCH ${drizzle_orm_1.sql.param(this.segment(query.search).join(" OR "))}`)
|
|
52
46
|
.orderBy((0, drizzle_orm_1.sql) `bm25(Memories_fts)`)
|
|
53
47
|
.limit(limit)
|
|
54
48
|
.execute()
|
|
49
|
+
.then((rows) => rows.map((row) => row.Memories))
|
|
55
50
|
: await db
|
|
56
51
|
.select()
|
|
57
52
|
.from(memory_js_1.Memories)
|
|
@@ -81,14 +76,18 @@ class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
|
|
|
81
76
|
this.options?.enableFTS &&
|
|
82
77
|
db.run((0, drizzle_orm_1.sql) `\
|
|
83
78
|
insert into Memories_fts (id, content)
|
|
84
|
-
values (${id}, ${this.segment((0, yaml_1.stringify)(memory.content)).join(" ")})`),
|
|
79
|
+
values (${drizzle_orm_1.sql.param(id)}, ${drizzle_orm_1.sql.param(this.segment((0, yaml_1.stringify)(memory.content)).join(" "))})`),
|
|
85
80
|
]);
|
|
86
81
|
if (!result)
|
|
87
82
|
throw new Error("Failed to create memory");
|
|
88
83
|
return { result: this.convertMemory(result) };
|
|
89
84
|
}
|
|
90
85
|
segment(str) {
|
|
91
|
-
return Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str))
|
|
86
|
+
return (Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str))
|
|
87
|
+
.map((i) => i.segment)
|
|
88
|
+
// Remove non-alphanumeric characters and trim whitespace
|
|
89
|
+
.map((i) => i.replace(/[^\p{L}\p{N}\s]/gu, "").trim())
|
|
90
|
+
.filter(Boolean));
|
|
92
91
|
}
|
|
93
92
|
}
|
|
94
93
|
exports.DefaultMemoryStorage = DefaultMemoryStorage;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, MemoryRetriever, 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
6
|
recorderOptions?: Omit<DefaultMemoryRecorderOptions, "storage">;
|
|
7
7
|
retrieverOptions?: Omit<DefaultMemoryRetrieverOptions, "storage">;
|
|
8
|
+
messageKey?: string | string[];
|
|
8
9
|
}
|
|
9
10
|
export declare class DefaultMemory extends MemoryAgent {
|
|
10
11
|
constructor(options?: DefaultMemoryOptions);
|
|
@@ -13,8 +14,23 @@ export declare class DefaultMemory extends MemoryAgent {
|
|
|
13
14
|
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
14
15
|
storage: MemoryStorage;
|
|
15
16
|
defaultRetrieveMemoryCount?: number;
|
|
17
|
+
retrieveFromMessageKey?: string | string[];
|
|
18
|
+
getSearchPattern?: DefaultMemoryRetriever["getSearchPattern"];
|
|
19
|
+
formatMessage?: DefaultMemoryRetriever["formatMessage"];
|
|
20
|
+
formatMemory?: DefaultMemoryRetriever["formatMemory"];
|
|
21
|
+
}
|
|
22
|
+
declare class DefaultMemoryRetriever extends MemoryRetriever {
|
|
23
|
+
constructor(options: DefaultMemoryRetrieverOptions);
|
|
24
|
+
private storage;
|
|
25
|
+
private defaultRetrieveMemoryCount?;
|
|
26
|
+
private retrieveFromMessageKey?;
|
|
27
|
+
private getSearchPattern;
|
|
28
|
+
private formatMessage;
|
|
29
|
+
private formatMemory;
|
|
30
|
+
process(input: MemoryRetrieverInput, options: AgentInvokeOptions): Promise<MemoryRetrieverOutput>;
|
|
16
31
|
}
|
|
17
32
|
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
18
33
|
storage: MemoryStorage;
|
|
19
34
|
rememberFromMessageKey?: string | string[];
|
|
20
35
|
}
|
|
36
|
+
export {};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DefaultMemory = void 0;
|
|
4
4
|
const core_1 = require("@aigne/core");
|
|
5
|
+
const type_utils_js_1 = require("@aigne/core/utils/type-utils.js");
|
|
5
6
|
const index_js_1 = require("./default-memory-storage/index.js");
|
|
6
7
|
const storage_js_1 = require("./storage.js");
|
|
7
8
|
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
@@ -15,11 +16,13 @@ class DefaultMemory extends core_1.MemoryAgent {
|
|
|
15
16
|
recorder: options.recorder ??
|
|
16
17
|
new DefaultMemoryRecorder({
|
|
17
18
|
...options.recorderOptions,
|
|
19
|
+
rememberFromMessageKey: options.recorderOptions?.rememberFromMessageKey ?? options.messageKey,
|
|
18
20
|
storage,
|
|
19
21
|
}),
|
|
20
22
|
retriever: options.retriever ??
|
|
21
23
|
new DefaultMemoryRetriever({
|
|
22
24
|
...options.retrieverOptions,
|
|
25
|
+
retrieveFromMessageKey: options.retrieverOptions?.retrieveFromMessageKey ?? options.messageKey,
|
|
23
26
|
storage,
|
|
24
27
|
}),
|
|
25
28
|
autoUpdate: options.autoUpdate ?? true,
|
|
@@ -34,15 +37,50 @@ class DefaultMemoryRetriever extends core_1.MemoryRetriever {
|
|
|
34
37
|
super(options);
|
|
35
38
|
this.storage = options.storage;
|
|
36
39
|
this.defaultRetrieveMemoryCount = options.defaultRetrieveMemoryCount;
|
|
40
|
+
this.retrieveFromMessageKey = options.retrieveFromMessageKey;
|
|
41
|
+
if (options.getSearchPattern)
|
|
42
|
+
this.getSearchPattern = options.getSearchPattern;
|
|
43
|
+
if (options.formatMessage)
|
|
44
|
+
this.formatMessage = options.formatMessage;
|
|
45
|
+
if (options.formatMemory)
|
|
46
|
+
this.formatMemory = options.formatMemory;
|
|
37
47
|
}
|
|
38
48
|
storage;
|
|
39
49
|
defaultRetrieveMemoryCount;
|
|
50
|
+
retrieveFromMessageKey;
|
|
51
|
+
getSearchPattern = (search) => {
|
|
52
|
+
if (!search || typeof search === "string")
|
|
53
|
+
return search;
|
|
54
|
+
const obj = search && this.retrieveFromMessageKey ? (0, type_utils_js_1.pick)(search, this.retrieveFromMessageKey) : search;
|
|
55
|
+
return Object.values(obj)
|
|
56
|
+
.map((v) => (typeof v === "string" ? v : undefined))
|
|
57
|
+
.join("\n");
|
|
58
|
+
};
|
|
59
|
+
formatMessage = (content) => {
|
|
60
|
+
if (!this.retrieveFromMessageKey || !(0, type_utils_js_1.isRecord)(content))
|
|
61
|
+
return content;
|
|
62
|
+
const obj = (0, type_utils_js_1.pick)(content, this.retrieveFromMessageKey);
|
|
63
|
+
return Object.values(obj)
|
|
64
|
+
.map((v) => (typeof v === "string" ? v : undefined))
|
|
65
|
+
.join("\n");
|
|
66
|
+
};
|
|
67
|
+
formatMemory = (content) => {
|
|
68
|
+
if (!(0, type_utils_js_1.isRecord)(content))
|
|
69
|
+
return content;
|
|
70
|
+
if ("input" in content || "output" in content) {
|
|
71
|
+
return {
|
|
72
|
+
user: this.formatMessage(content.input),
|
|
73
|
+
agent: this.formatMessage(content.output),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
40
77
|
async process(input, options) {
|
|
41
78
|
const { result } = await this.storage.search({
|
|
42
79
|
...input,
|
|
80
|
+
search: this.getSearchPattern(input.search),
|
|
43
81
|
limit: input.limit ?? this.defaultRetrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT,
|
|
44
82
|
}, options);
|
|
45
|
-
return { memories: result };
|
|
83
|
+
return { memories: result.map((i) => ({ ...i, content: this.formatMemory(i.content) })) };
|
|
46
84
|
}
|
|
47
85
|
}
|
|
48
86
|
class DefaultMemoryRecorder extends core_1.MemoryRecorder {
|
|
@@ -58,8 +96,13 @@ class DefaultMemoryRecorder extends core_1.MemoryRecorder {
|
|
|
58
96
|
for (const item of input.content) {
|
|
59
97
|
const { result } = await this.storage.create({
|
|
60
98
|
content: {
|
|
61
|
-
|
|
62
|
-
|
|
99
|
+
input: item.input && this.rememberFromMessageKey
|
|
100
|
+
? (0, type_utils_js_1.pick)(item.input, this.rememberFromMessageKey)
|
|
101
|
+
: item.input,
|
|
102
|
+
output: item.output && this.rememberFromMessageKey
|
|
103
|
+
? (0, type_utils_js_1.pick)(item.output, this.rememberFromMessageKey)
|
|
104
|
+
: item.output,
|
|
105
|
+
source: item.source,
|
|
63
106
|
},
|
|
64
107
|
}, options);
|
|
65
108
|
newMemories.push(result);
|
|
@@ -68,17 +111,4 @@ class DefaultMemoryRecorder extends core_1.MemoryRecorder {
|
|
|
68
111
|
memories: newMemories,
|
|
69
112
|
};
|
|
70
113
|
}
|
|
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
|
-
}
|
|
84
114
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, MemoryRetriever, 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
6
|
recorderOptions?: Omit<DefaultMemoryRecorderOptions, "storage">;
|
|
7
7
|
retrieverOptions?: Omit<DefaultMemoryRetrieverOptions, "storage">;
|
|
8
|
+
messageKey?: string | string[];
|
|
8
9
|
}
|
|
9
10
|
export declare class DefaultMemory extends MemoryAgent {
|
|
10
11
|
constructor(options?: DefaultMemoryOptions);
|
|
@@ -13,8 +14,23 @@ export declare class DefaultMemory extends MemoryAgent {
|
|
|
13
14
|
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
14
15
|
storage: MemoryStorage;
|
|
15
16
|
defaultRetrieveMemoryCount?: number;
|
|
17
|
+
retrieveFromMessageKey?: string | string[];
|
|
18
|
+
getSearchPattern?: DefaultMemoryRetriever["getSearchPattern"];
|
|
19
|
+
formatMessage?: DefaultMemoryRetriever["formatMessage"];
|
|
20
|
+
formatMemory?: DefaultMemoryRetriever["formatMemory"];
|
|
21
|
+
}
|
|
22
|
+
declare class DefaultMemoryRetriever extends MemoryRetriever {
|
|
23
|
+
constructor(options: DefaultMemoryRetrieverOptions);
|
|
24
|
+
private storage;
|
|
25
|
+
private defaultRetrieveMemoryCount?;
|
|
26
|
+
private retrieveFromMessageKey?;
|
|
27
|
+
private getSearchPattern;
|
|
28
|
+
private formatMessage;
|
|
29
|
+
private formatMemory;
|
|
30
|
+
process(input: MemoryRetrieverInput, options: AgentInvokeOptions): Promise<MemoryRetrieverOutput>;
|
|
16
31
|
}
|
|
17
32
|
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
18
33
|
storage: MemoryStorage;
|
|
19
34
|
rememberFromMessageKey?: string | string[];
|
|
20
35
|
}
|
|
36
|
+
export {};
|
|
@@ -36,19 +36,14 @@ export class DefaultMemoryStorage extends MemoryStorage {
|
|
|
36
36
|
const db = await this.db;
|
|
37
37
|
const memories = this.options?.enableFTS && query.search
|
|
38
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
|
-
})
|
|
39
|
+
.select()
|
|
46
40
|
.from(Memories)
|
|
47
41
|
.innerJoin(sql `Memories_fts`, sql `Memories_fts.id = ${Memories.id}`)
|
|
48
|
-
.where(sql `Memories_fts MATCH ${query.search}`)
|
|
42
|
+
.where(sql `Memories_fts MATCH ${sql.param(this.segment(query.search).join(" OR "))}`)
|
|
49
43
|
.orderBy(sql `bm25(Memories_fts)`)
|
|
50
44
|
.limit(limit)
|
|
51
45
|
.execute()
|
|
46
|
+
.then((rows) => rows.map((row) => row.Memories))
|
|
52
47
|
: await db
|
|
53
48
|
.select()
|
|
54
49
|
.from(Memories)
|
|
@@ -78,13 +73,17 @@ export class DefaultMemoryStorage extends MemoryStorage {
|
|
|
78
73
|
this.options?.enableFTS &&
|
|
79
74
|
db.run(sql `\
|
|
80
75
|
insert into Memories_fts (id, content)
|
|
81
|
-
values (${id}, ${this.segment(stringify(memory.content)).join(" ")})`),
|
|
76
|
+
values (${sql.param(id)}, ${sql.param(this.segment(stringify(memory.content)).join(" "))})`),
|
|
82
77
|
]);
|
|
83
78
|
if (!result)
|
|
84
79
|
throw new Error("Failed to create memory");
|
|
85
80
|
return { result: this.convertMemory(result) };
|
|
86
81
|
}
|
|
87
82
|
segment(str) {
|
|
88
|
-
return Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str))
|
|
83
|
+
return (Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str))
|
|
84
|
+
.map((i) => i.segment)
|
|
85
|
+
// Remove non-alphanumeric characters and trim whitespace
|
|
86
|
+
.map((i) => i.replace(/[^\p{L}\p{N}\s]/gu, "").trim())
|
|
87
|
+
.filter(Boolean));
|
|
89
88
|
}
|
|
90
89
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRecorderOutput, MemoryRetriever, 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
6
|
recorderOptions?: Omit<DefaultMemoryRecorderOptions, "storage">;
|
|
7
7
|
retrieverOptions?: Omit<DefaultMemoryRetrieverOptions, "storage">;
|
|
8
|
+
messageKey?: string | string[];
|
|
8
9
|
}
|
|
9
10
|
export declare class DefaultMemory extends MemoryAgent {
|
|
10
11
|
constructor(options?: DefaultMemoryOptions);
|
|
@@ -13,8 +14,23 @@ export declare class DefaultMemory extends MemoryAgent {
|
|
|
13
14
|
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
14
15
|
storage: MemoryStorage;
|
|
15
16
|
defaultRetrieveMemoryCount?: number;
|
|
17
|
+
retrieveFromMessageKey?: string | string[];
|
|
18
|
+
getSearchPattern?: DefaultMemoryRetriever["getSearchPattern"];
|
|
19
|
+
formatMessage?: DefaultMemoryRetriever["formatMessage"];
|
|
20
|
+
formatMemory?: DefaultMemoryRetriever["formatMemory"];
|
|
21
|
+
}
|
|
22
|
+
declare class DefaultMemoryRetriever extends MemoryRetriever {
|
|
23
|
+
constructor(options: DefaultMemoryRetrieverOptions);
|
|
24
|
+
private storage;
|
|
25
|
+
private defaultRetrieveMemoryCount?;
|
|
26
|
+
private retrieveFromMessageKey?;
|
|
27
|
+
private getSearchPattern;
|
|
28
|
+
private formatMessage;
|
|
29
|
+
private formatMemory;
|
|
30
|
+
process(input: MemoryRetrieverInput, options: AgentInvokeOptions): Promise<MemoryRetrieverOutput>;
|
|
16
31
|
}
|
|
17
32
|
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
18
33
|
storage: MemoryStorage;
|
|
19
34
|
rememberFromMessageKey?: string | string[];
|
|
20
35
|
}
|
|
36
|
+
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MemoryAgent, MemoryRecorder, MemoryRetriever, } from "@aigne/core";
|
|
2
|
+
import { isRecord, pick } from "@aigne/core/utils/type-utils.js";
|
|
2
3
|
import { DefaultMemoryStorage, } from "./default-memory-storage/index.js";
|
|
3
4
|
import { MemoryStorage } from "./storage.js";
|
|
4
5
|
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
@@ -12,11 +13,13 @@ export class DefaultMemory extends MemoryAgent {
|
|
|
12
13
|
recorder: options.recorder ??
|
|
13
14
|
new DefaultMemoryRecorder({
|
|
14
15
|
...options.recorderOptions,
|
|
16
|
+
rememberFromMessageKey: options.recorderOptions?.rememberFromMessageKey ?? options.messageKey,
|
|
15
17
|
storage,
|
|
16
18
|
}),
|
|
17
19
|
retriever: options.retriever ??
|
|
18
20
|
new DefaultMemoryRetriever({
|
|
19
21
|
...options.retrieverOptions,
|
|
22
|
+
retrieveFromMessageKey: options.retrieverOptions?.retrieveFromMessageKey ?? options.messageKey,
|
|
20
23
|
storage,
|
|
21
24
|
}),
|
|
22
25
|
autoUpdate: options.autoUpdate ?? true,
|
|
@@ -30,15 +33,50 @@ class DefaultMemoryRetriever extends MemoryRetriever {
|
|
|
30
33
|
super(options);
|
|
31
34
|
this.storage = options.storage;
|
|
32
35
|
this.defaultRetrieveMemoryCount = options.defaultRetrieveMemoryCount;
|
|
36
|
+
this.retrieveFromMessageKey = options.retrieveFromMessageKey;
|
|
37
|
+
if (options.getSearchPattern)
|
|
38
|
+
this.getSearchPattern = options.getSearchPattern;
|
|
39
|
+
if (options.formatMessage)
|
|
40
|
+
this.formatMessage = options.formatMessage;
|
|
41
|
+
if (options.formatMemory)
|
|
42
|
+
this.formatMemory = options.formatMemory;
|
|
33
43
|
}
|
|
34
44
|
storage;
|
|
35
45
|
defaultRetrieveMemoryCount;
|
|
46
|
+
retrieveFromMessageKey;
|
|
47
|
+
getSearchPattern = (search) => {
|
|
48
|
+
if (!search || typeof search === "string")
|
|
49
|
+
return search;
|
|
50
|
+
const obj = search && this.retrieveFromMessageKey ? pick(search, this.retrieveFromMessageKey) : search;
|
|
51
|
+
return Object.values(obj)
|
|
52
|
+
.map((v) => (typeof v === "string" ? v : undefined))
|
|
53
|
+
.join("\n");
|
|
54
|
+
};
|
|
55
|
+
formatMessage = (content) => {
|
|
56
|
+
if (!this.retrieveFromMessageKey || !isRecord(content))
|
|
57
|
+
return content;
|
|
58
|
+
const obj = pick(content, this.retrieveFromMessageKey);
|
|
59
|
+
return Object.values(obj)
|
|
60
|
+
.map((v) => (typeof v === "string" ? v : undefined))
|
|
61
|
+
.join("\n");
|
|
62
|
+
};
|
|
63
|
+
formatMemory = (content) => {
|
|
64
|
+
if (!isRecord(content))
|
|
65
|
+
return content;
|
|
66
|
+
if ("input" in content || "output" in content) {
|
|
67
|
+
return {
|
|
68
|
+
user: this.formatMessage(content.input),
|
|
69
|
+
agent: this.formatMessage(content.output),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
};
|
|
36
73
|
async process(input, options) {
|
|
37
74
|
const { result } = await this.storage.search({
|
|
38
75
|
...input,
|
|
76
|
+
search: this.getSearchPattern(input.search),
|
|
39
77
|
limit: input.limit ?? this.defaultRetrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT,
|
|
40
78
|
}, options);
|
|
41
|
-
return { memories: result };
|
|
79
|
+
return { memories: result.map((i) => ({ ...i, content: this.formatMemory(i.content) })) };
|
|
42
80
|
}
|
|
43
81
|
}
|
|
44
82
|
class DefaultMemoryRecorder extends MemoryRecorder {
|
|
@@ -54,8 +92,13 @@ class DefaultMemoryRecorder extends MemoryRecorder {
|
|
|
54
92
|
for (const item of input.content) {
|
|
55
93
|
const { result } = await this.storage.create({
|
|
56
94
|
content: {
|
|
57
|
-
|
|
58
|
-
|
|
95
|
+
input: item.input && this.rememberFromMessageKey
|
|
96
|
+
? pick(item.input, this.rememberFromMessageKey)
|
|
97
|
+
: item.input,
|
|
98
|
+
output: item.output && this.rememberFromMessageKey
|
|
99
|
+
? pick(item.output, this.rememberFromMessageKey)
|
|
100
|
+
: item.output,
|
|
101
|
+
source: item.source,
|
|
59
102
|
},
|
|
60
103
|
}, options);
|
|
61
104
|
newMemories.push(result);
|
|
@@ -64,17 +107,4 @@ class DefaultMemoryRecorder extends MemoryRecorder {
|
|
|
64
107
|
memories: newMemories,
|
|
65
108
|
};
|
|
66
109
|
}
|
|
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
|
-
}
|
|
80
110
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigne/agent-library",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "Collection of agent libraries for AIGNE framework",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"uuid": "^11.1.0",
|
|
48
48
|
"yaml": "^2.7.1",
|
|
49
49
|
"zod": "^3.24.4",
|
|
50
|
-
"@aigne/core": "^1.
|
|
51
|
-
"@aigne/openai": "^0.
|
|
50
|
+
"@aigne/core": "^1.24.0",
|
|
51
|
+
"@aigne/openai": "^0.4.0",
|
|
52
52
|
"@aigne/sqlite": "^0.1.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|