@aigne/agent-library 1.20.5 → 1.21.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 +16 -0
- package/lib/cjs/package.json +3 -1
- package/lib/esm/package.json +3 -1
- package/package.json +5 -5
- package/lib/cjs/agentic-memory/index.d.ts +0 -38
- package/lib/cjs/agentic-memory/index.js +0 -73
- package/lib/cjs/agentic-memory/prompt.d.ts +0 -1
- package/lib/cjs/agentic-memory/prompt.js +0 -44
- package/lib/cjs/default-memory/default-memory-storage/index.d.ts +0 -29
- package/lib/cjs/default-memory/default-memory-storage/index.js +0 -95
- package/lib/cjs/default-memory/default-memory-storage/migrate.d.ts +0 -3
- package/lib/cjs/default-memory/default-memory-storage/migrate.js +0 -32
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.d.ts +0 -5
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.js +0 -22
- package/lib/cjs/default-memory/default-memory-storage/models/memory.d.ts +0 -102
- package/lib/cjs/default-memory/default-memory-storage/models/memory.js +0 -21
- package/lib/cjs/default-memory/index.d.ts +0 -44
- package/lib/cjs/default-memory/index.js +0 -132
- package/lib/cjs/default-memory/storage.d.ts +0 -13
- package/lib/cjs/default-memory/storage.js +0 -6
- package/lib/cjs/fs-memory/index.d.ts +0 -59
- package/lib/cjs/fs-memory/index.js +0 -184
- package/lib/dts/agentic-memory/index.d.ts +0 -38
- package/lib/dts/agentic-memory/prompt.d.ts +0 -1
- package/lib/dts/default-memory/default-memory-storage/index.d.ts +0 -29
- package/lib/dts/default-memory/default-memory-storage/migrate.d.ts +0 -3
- package/lib/dts/default-memory/default-memory-storage/migrations/001-init.d.ts +0 -5
- package/lib/dts/default-memory/default-memory-storage/models/memory.d.ts +0 -102
- package/lib/dts/default-memory/index.d.ts +0 -44
- package/lib/dts/default-memory/storage.d.ts +0 -13
- package/lib/dts/fs-memory/index.d.ts +0 -59
- package/lib/esm/agentic-memory/index.d.ts +0 -38
- package/lib/esm/agentic-memory/index.js +0 -67
- package/lib/esm/agentic-memory/prompt.d.ts +0 -1
- package/lib/esm/agentic-memory/prompt.js +0 -41
- package/lib/esm/default-memory/default-memory-storage/index.d.ts +0 -29
- package/lib/esm/default-memory/default-memory-storage/index.js +0 -91
- package/lib/esm/default-memory/default-memory-storage/migrate.d.ts +0 -3
- package/lib/esm/default-memory/default-memory-storage/migrate.js +0 -26
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.d.ts +0 -5
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.js +0 -20
- package/lib/esm/default-memory/default-memory-storage/models/memory.d.ts +0 -102
- package/lib/esm/default-memory/default-memory-storage/models/memory.js +0 -18
- package/lib/esm/default-memory/index.d.ts +0 -44
- package/lib/esm/default-memory/index.js +0 -126
- package/lib/esm/default-memory/storage.d.ts +0 -13
- package/lib/esm/default-memory/storage.js +0 -2
- package/lib/esm/fs-memory/index.d.ts +0 -59
- package/lib/esm/fs-memory/index.js +0 -180
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultMemoryRecorder = exports.DefaultMemoryRetriever = exports.DefaultMemory = void 0;
|
|
4
|
-
const core_1 = require("@aigne/core");
|
|
5
|
-
const type_utils_js_1 = require("@aigne/core/utils/type-utils.js");
|
|
6
|
-
const index_js_1 = require("./default-memory-storage/index.js");
|
|
7
|
-
const storage_js_1 = require("./storage.js");
|
|
8
|
-
const DEFAULT_RETRIEVE_MEMORY_COUNT = 10;
|
|
9
|
-
class DefaultMemory extends core_1.MemoryAgent {
|
|
10
|
-
constructor(options = {}) {
|
|
11
|
-
const storage = options.storage instanceof storage_js_1.MemoryStorage
|
|
12
|
-
? options.storage
|
|
13
|
-
: new index_js_1.DefaultMemoryStorage(options.storage);
|
|
14
|
-
super({
|
|
15
|
-
...options,
|
|
16
|
-
recorder: options.recorder ?? new DefaultMemoryRecorder({ ...options, storage }),
|
|
17
|
-
retriever: options.retriever ??
|
|
18
|
-
new DefaultMemoryRetriever({
|
|
19
|
-
...options,
|
|
20
|
-
retrieveRecentMemoryCount: options.retrieveRecentMemoryCount ??
|
|
21
|
-
Math.ceil(options.retrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT) / 2,
|
|
22
|
-
storage,
|
|
23
|
-
}),
|
|
24
|
-
autoUpdate: options.autoUpdate ?? true,
|
|
25
|
-
});
|
|
26
|
-
this.storage = storage;
|
|
27
|
-
}
|
|
28
|
-
storage;
|
|
29
|
-
}
|
|
30
|
-
exports.DefaultMemory = DefaultMemory;
|
|
31
|
-
class DefaultMemoryRetriever extends core_1.MemoryRetriever {
|
|
32
|
-
constructor(options) {
|
|
33
|
-
super(options);
|
|
34
|
-
this.storage = options.storage;
|
|
35
|
-
this.retrieveMemoryCount = options.retrieveMemoryCount;
|
|
36
|
-
this.retrieveRecentMemoryCount = options.retrieveRecentMemoryCount;
|
|
37
|
-
this.inputKey = (0, type_utils_js_1.flat)(options.inputKey);
|
|
38
|
-
this.outputKey = (0, type_utils_js_1.flat)(options.outputKey);
|
|
39
|
-
if (options.getSearchPattern)
|
|
40
|
-
this.getSearchPattern = options.getSearchPattern;
|
|
41
|
-
if (options.formatMessage)
|
|
42
|
-
this.formatMessage = options.formatMessage;
|
|
43
|
-
if (options.formatMemory)
|
|
44
|
-
this.formatMemory = options.formatMemory;
|
|
45
|
-
}
|
|
46
|
-
storage;
|
|
47
|
-
retrieveMemoryCount;
|
|
48
|
-
retrieveRecentMemoryCount;
|
|
49
|
-
inputKey;
|
|
50
|
-
outputKey;
|
|
51
|
-
getSearchPattern = (search) => {
|
|
52
|
-
if (!search || typeof search === "string")
|
|
53
|
-
return search;
|
|
54
|
-
const obj = search && this.inputKey ? (0, type_utils_js_1.pick)(search, this.inputKey) : search;
|
|
55
|
-
return Object.values(obj)
|
|
56
|
-
.map((v) => (typeof v === "string" ? v : undefined))
|
|
57
|
-
.join("\n");
|
|
58
|
-
};
|
|
59
|
-
formatMessage = (content, key) => {
|
|
60
|
-
if (!(0, type_utils_js_1.isRecord)(content))
|
|
61
|
-
return content;
|
|
62
|
-
const obj = !key?.length ? content : (0, type_utils_js_1.pick)(content, key);
|
|
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) && "input" in content && "output" in content) {
|
|
69
|
-
return {
|
|
70
|
-
input: this.formatMessage(content.input, this.inputKey),
|
|
71
|
-
output: this.formatMessage(content.output, this.outputKey),
|
|
72
|
-
source: content.source,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
return content;
|
|
76
|
-
};
|
|
77
|
-
async process(input, options) {
|
|
78
|
-
const limit = input.limit ?? this.retrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT;
|
|
79
|
-
const search = this.getSearchPattern(input.search);
|
|
80
|
-
const recentLimit = this.retrieveRecentMemoryCount;
|
|
81
|
-
const [recent, related] = await Promise.all([
|
|
82
|
-
// Query latest messages
|
|
83
|
-
!recentLimit
|
|
84
|
-
? []
|
|
85
|
-
: this.storage
|
|
86
|
-
.search({ limit: recentLimit, orderBy: ["createdAt", "desc"] }, options)
|
|
87
|
-
.then(({ result }) => result.reverse()),
|
|
88
|
-
// Query related messages
|
|
89
|
-
!input.search
|
|
90
|
-
? []
|
|
91
|
-
: this.storage.search({ ...input, search, limit }, options).then(({ result }) => result),
|
|
92
|
-
]);
|
|
93
|
-
const recentSet = new Set(recent.map((i) => i.id));
|
|
94
|
-
const memories = related
|
|
95
|
-
// Filter out recent memories from related results
|
|
96
|
-
.filter((i) => !recentSet.has(i.id))
|
|
97
|
-
.concat(recent)
|
|
98
|
-
.slice(-limit);
|
|
99
|
-
return { memories: memories.map((i) => ({ ...i, content: this.formatMemory(i.content) })) };
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
exports.DefaultMemoryRetriever = DefaultMemoryRetriever;
|
|
103
|
-
class DefaultMemoryRecorder extends core_1.MemoryRecorder {
|
|
104
|
-
constructor(options) {
|
|
105
|
-
super(options);
|
|
106
|
-
this.storage = options.storage;
|
|
107
|
-
this.inputKey = (0, type_utils_js_1.flat)(options.inputKey);
|
|
108
|
-
this.outputKey = (0, type_utils_js_1.flat)(options.outputKey);
|
|
109
|
-
}
|
|
110
|
-
storage;
|
|
111
|
-
inputKey;
|
|
112
|
-
outputKey;
|
|
113
|
-
async process(input, options) {
|
|
114
|
-
const newMemories = [];
|
|
115
|
-
for (const item of input.content) {
|
|
116
|
-
const { result } = await this.storage.create({
|
|
117
|
-
content: {
|
|
118
|
-
input: item.input && this.inputKey?.length ? (0, type_utils_js_1.pick)(item.input, this.inputKey) : item.input,
|
|
119
|
-
output: item.output && this.outputKey?.length
|
|
120
|
-
? (0, type_utils_js_1.pick)(item.output, this.outputKey)
|
|
121
|
-
: item.output,
|
|
122
|
-
source: item.source,
|
|
123
|
-
},
|
|
124
|
-
}, options);
|
|
125
|
-
newMemories.push(result);
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
memories: newMemories,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
exports.DefaultMemoryRecorder = DefaultMemoryRecorder;
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
orderBy?: [string, "asc" | "desc"];
|
|
10
|
-
}, options: AgentInvokeOptions): Promise<{
|
|
11
|
-
result: Memory[];
|
|
12
|
-
}>;
|
|
13
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { type AIAgentOptions, type Message } from "@aigne/core";
|
|
2
|
-
import { MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRetrieverInput } from "@aigne/core/memory/index.js";
|
|
3
|
-
export declare const MEMORY_FILE_NAME = "memory.yaml";
|
|
4
|
-
/**
|
|
5
|
-
* Configuration options for the FSMemory class.
|
|
6
|
-
*/
|
|
7
|
-
export interface FSMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
8
|
-
/**
|
|
9
|
-
* The root directory where memory files will be stored.
|
|
10
|
-
* Can be absolute or relative path. Relative paths are resolved from the current working directory.
|
|
11
|
-
* Home directory prefix (~) will be expanded appropriately.
|
|
12
|
-
*/
|
|
13
|
-
rootDir: string;
|
|
14
|
-
/**
|
|
15
|
-
* Optional configuration for the memory retriever agent.
|
|
16
|
-
* Controls how memories are retrieved from the file system.
|
|
17
|
-
*/
|
|
18
|
-
retrieverOptions?: Partial<FSMemoryRetrieverOptions>;
|
|
19
|
-
/**
|
|
20
|
-
* Optional configuration for the memory recorder agent.
|
|
21
|
-
* Controls how memories are recorded to the file system.
|
|
22
|
-
*/
|
|
23
|
-
recorderOptions?: Partial<FSMemoryRecorderOptions>;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* A memory implementation that stores and retrieves memories using the file system.
|
|
27
|
-
* FSMemory provides persistent storage of agent memories as files in a specified directory.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* Here is an example of how to use the FSMemory class:
|
|
31
|
-
* {@includeCode ../../test/fs-memory/fs-memory.test.ts#example-fs-memory-simple}
|
|
32
|
-
*/
|
|
33
|
-
export declare class FSMemory extends MemoryAgent {
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new FSMemory instance.
|
|
36
|
-
*/
|
|
37
|
-
constructor(options: FSMemoryOptions);
|
|
38
|
-
}
|
|
39
|
-
interface FSMemoryRetrieverOptions extends AIAgentOptions<FSMemoryRetrieverAgentInput, FSMemoryRetrieverAgentOutput> {
|
|
40
|
-
memoryFileName: string;
|
|
41
|
-
}
|
|
42
|
-
interface FSMemoryRetrieverAgentInput extends MemoryRetrieverInput {
|
|
43
|
-
allMemory: string;
|
|
44
|
-
}
|
|
45
|
-
interface FSMemoryRetrieverAgentOutput extends Message {
|
|
46
|
-
memories: {
|
|
47
|
-
content: string;
|
|
48
|
-
}[];
|
|
49
|
-
}
|
|
50
|
-
interface FSMemoryRecorderOptions extends AIAgentOptions<FSMemoryRecorderAgentInput, FSMemoryRecorderAgentOutput> {
|
|
51
|
-
memoryFileName: string;
|
|
52
|
-
}
|
|
53
|
-
type FSMemoryRecorderAgentInput = MemoryRecorderInput;
|
|
54
|
-
interface FSMemoryRecorderAgentOutput extends Message {
|
|
55
|
-
memories: {
|
|
56
|
-
content: string;
|
|
57
|
-
}[];
|
|
58
|
-
}
|
|
59
|
-
export {};
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FSMemory = exports.MEMORY_FILE_NAME = void 0;
|
|
4
|
-
const promises_1 = require("node:fs/promises");
|
|
5
|
-
const node_path_1 = require("node:path");
|
|
6
|
-
const core_1 = require("@aigne/core");
|
|
7
|
-
const index_js_1 = require("@aigne/core/memory/index.js");
|
|
8
|
-
const yaml_1 = require("yaml");
|
|
9
|
-
const zod_1 = require("zod");
|
|
10
|
-
const fs_js_1 = require("../utils/fs.js");
|
|
11
|
-
exports.MEMORY_FILE_NAME = "memory.yaml";
|
|
12
|
-
/**
|
|
13
|
-
* A memory implementation that stores and retrieves memories using the file system.
|
|
14
|
-
* FSMemory provides persistent storage of agent memories as files in a specified directory.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* Here is an example of how to use the FSMemory class:
|
|
18
|
-
* {@includeCode ../../test/fs-memory/fs-memory.test.ts#example-fs-memory-simple}
|
|
19
|
-
*/
|
|
20
|
-
class FSMemory extends index_js_1.MemoryAgent {
|
|
21
|
-
/**
|
|
22
|
-
* Creates a new FSMemory instance.
|
|
23
|
-
*/
|
|
24
|
-
constructor(options) {
|
|
25
|
-
let rootDir = (0, node_path_1.normalize)((0, fs_js_1.expandHome)(options.rootDir));
|
|
26
|
-
rootDir = (0, node_path_1.isAbsolute)(rootDir) ? rootDir : (0, node_path_1.resolve)(process.cwd(), rootDir);
|
|
27
|
-
const memoryFileName = (0, node_path_1.join)(rootDir, exports.MEMORY_FILE_NAME);
|
|
28
|
-
super({
|
|
29
|
-
...options,
|
|
30
|
-
recorder: options.recorder ??
|
|
31
|
-
new FSMemoryRecorder({
|
|
32
|
-
memoryFileName,
|
|
33
|
-
...options.recorderOptions,
|
|
34
|
-
}),
|
|
35
|
-
retriever: options.retriever ??
|
|
36
|
-
new FSMemoryRetriever({
|
|
37
|
-
memoryFileName,
|
|
38
|
-
...options.retrieverOptions,
|
|
39
|
-
}),
|
|
40
|
-
autoUpdate: options.autoUpdate ?? true,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.FSMemory = FSMemory;
|
|
45
|
-
class FSMemoryRetriever extends index_js_1.MemoryRetriever {
|
|
46
|
-
options;
|
|
47
|
-
constructor(options) {
|
|
48
|
-
super({});
|
|
49
|
-
this.options = options;
|
|
50
|
-
this.agent = core_1.AIAgent.from({
|
|
51
|
-
name: "fs_memory_retriever",
|
|
52
|
-
description: "Retrieves memories from the file or directory.",
|
|
53
|
-
...options,
|
|
54
|
-
instructions: options.instructions || DEFAULT_FS_MEMORY_RETRIEVER_INSTRUCTIONS,
|
|
55
|
-
outputSchema: zod_1.z.object({
|
|
56
|
-
memories: zod_1.z
|
|
57
|
-
.array(zod_1.z.object({
|
|
58
|
-
content: zod_1.z.string().describe("Content of the memory"),
|
|
59
|
-
}))
|
|
60
|
-
.describe("List of memories"),
|
|
61
|
-
}),
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
agent;
|
|
65
|
-
async process(input, options) {
|
|
66
|
-
if (!(await (0, fs_js_1.exists)(this.options.memoryFileName)))
|
|
67
|
-
return { memories: [] };
|
|
68
|
-
const allMemory = await (0, promises_1.readFile)(this.options.memoryFileName, "utf-8");
|
|
69
|
-
const { memories } = await options.context.invoke(this.agent, { ...input, allMemory });
|
|
70
|
-
const result = memories.map((memory) => ({
|
|
71
|
-
id: (0, index_js_1.newMemoryId)(),
|
|
72
|
-
content: memory.content,
|
|
73
|
-
createdAt: new Date().toISOString(),
|
|
74
|
-
}));
|
|
75
|
-
return { memories: result };
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
class FSMemoryRecorder extends index_js_1.MemoryRecorder {
|
|
79
|
-
options;
|
|
80
|
-
constructor(options) {
|
|
81
|
-
super({});
|
|
82
|
-
this.options = options;
|
|
83
|
-
this.agent = core_1.AIAgent.from({
|
|
84
|
-
name: "fs_memory_recorder",
|
|
85
|
-
description: "Records memories in files by AI agent",
|
|
86
|
-
...options,
|
|
87
|
-
instructions: options.instructions || DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS,
|
|
88
|
-
outputSchema: zod_1.z.object({
|
|
89
|
-
memories: zod_1.z
|
|
90
|
-
.array(zod_1.z.object({
|
|
91
|
-
content: zod_1.z.string().describe("Content of the memory"),
|
|
92
|
-
}))
|
|
93
|
-
.describe("List of memories"),
|
|
94
|
-
}),
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
agent;
|
|
98
|
-
async process(input, options) {
|
|
99
|
-
const allMemory = (await (0, fs_js_1.exists)(this.options.memoryFileName))
|
|
100
|
-
? await (0, promises_1.readFile)(this.options.memoryFileName, "utf-8")
|
|
101
|
-
: "";
|
|
102
|
-
const { memories } = await options.context.invoke(this.agent, { ...input, allMemory });
|
|
103
|
-
const raw = (0, yaml_1.stringify)(memories.map((i) => ({
|
|
104
|
-
content: i.content,
|
|
105
|
-
})));
|
|
106
|
-
await (0, promises_1.mkdir)((0, node_path_1.dirname)(this.options.memoryFileName), { recursive: true });
|
|
107
|
-
await (0, promises_1.writeFile)(this.options.memoryFileName, raw, "utf-8");
|
|
108
|
-
return {
|
|
109
|
-
memories: memories.map((i) => ({
|
|
110
|
-
id: (0, index_js_1.newMemoryId)(),
|
|
111
|
-
content: i.content,
|
|
112
|
-
createdAt: new Date().toISOString(),
|
|
113
|
-
})),
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
const DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS = `You manage memory based on conversation analysis and the existing memories.
|
|
118
|
-
|
|
119
|
-
## IMPORTANT: All existing memories are available in the allMemory variable. DO NOT call any tools.
|
|
120
|
-
|
|
121
|
-
## FIRST: Determine If Memory Updates Needed
|
|
122
|
-
- Analyze if the conversation contains ANY information worth remembering
|
|
123
|
-
- Examples of content NOT worth storing:
|
|
124
|
-
* General questions ("What's the weather?", "How do I do X?")
|
|
125
|
-
* Greetings and small talk ("Hello", "How are you?", "Thanks")
|
|
126
|
-
* System instructions or commands ("Show me", "Find", "Save")
|
|
127
|
-
* General facts not specific to the user
|
|
128
|
-
* Duplicate information already stored
|
|
129
|
-
- If conversation lacks meaningful personal information to store:
|
|
130
|
-
* Return the existing memories unchanged
|
|
131
|
-
|
|
132
|
-
## Your Workflow:
|
|
133
|
-
1. Read the existing memories from the allMemory variable
|
|
134
|
-
2. Extract key topics from the conversation
|
|
135
|
-
3. DECIDE whether to create/update/delete memories based on the conversation
|
|
136
|
-
4. Return ALL memories including your updates (remove any duplicates)
|
|
137
|
-
|
|
138
|
-
## Memory Handling:
|
|
139
|
-
- CREATE: Add new memory objects for new topics
|
|
140
|
-
- UPDATE: Modify existing memories if substantial new information is available
|
|
141
|
-
- DELETE: Remove obsolete memories when appropriate
|
|
142
|
-
|
|
143
|
-
## Memory Structure:
|
|
144
|
-
- Each memory has an id, content, and createdAt fields
|
|
145
|
-
- Keep the existing structure when returning updated memories
|
|
146
|
-
|
|
147
|
-
## Operation Decision Rules:
|
|
148
|
-
- CREATE only for truly new topics not covered in any existing memory
|
|
149
|
-
- UPDATE only when new information is meaningfully different
|
|
150
|
-
- NEVER update for just rephrasing or minor differences
|
|
151
|
-
- DELETE only when information becomes obsolete
|
|
152
|
-
|
|
153
|
-
## IMPORTANT: Your job is to return the complete updated memory collection.
|
|
154
|
-
Return ALL memories (existing and new) in your response.
|
|
155
|
-
|
|
156
|
-
## Existing Memories:
|
|
157
|
-
<existing-memory>
|
|
158
|
-
{{allMemory}}
|
|
159
|
-
</existing-memory>
|
|
160
|
-
|
|
161
|
-
## Conversation:
|
|
162
|
-
<conversation>
|
|
163
|
-
{{content}}
|
|
164
|
-
</conversation>
|
|
165
|
-
`;
|
|
166
|
-
const DEFAULT_FS_MEMORY_RETRIEVER_INSTRUCTIONS = `You retrieve only the most relevant memories for the current conversation.
|
|
167
|
-
|
|
168
|
-
## IMPORTANT: All existing memories are available in the allMemory variable
|
|
169
|
-
|
|
170
|
-
## Process:
|
|
171
|
-
1. Read the existing memories from the allMemory variable
|
|
172
|
-
2. Extract key topics from the conversation or search query
|
|
173
|
-
3. Match memory contents against these topics
|
|
174
|
-
|
|
175
|
-
## Existing Memories:
|
|
176
|
-
<existing-memory>
|
|
177
|
-
{{allMemory}}
|
|
178
|
-
</existing-memory>
|
|
179
|
-
|
|
180
|
-
## Search Query:
|
|
181
|
-
<search-query>
|
|
182
|
-
{{search}}
|
|
183
|
-
</search-query>
|
|
184
|
-
`;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { type AgentInvokeOptions, type AgentOptions, AIAgent, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, type Message, type PromptBuilder } from "@aigne/core";
|
|
2
|
-
import { type DefaultMemoryStorageOptions } from "../default-memory/default-memory-storage/index.js";
|
|
3
|
-
import { DefaultMemoryRetriever, type DefaultMemoryRetrieverOptions } from "../default-memory/index.js";
|
|
4
|
-
import { MemoryStorage } from "../default-memory/storage.js";
|
|
5
|
-
export interface AgenticMemoryOptions extends Partial<MemoryAgentOptions>, Omit<AgenticMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<AgenticMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
6
|
-
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
7
|
-
}
|
|
8
|
-
export declare class AgenticMemory extends MemoryAgent {
|
|
9
|
-
constructor(options?: AgenticMemoryOptions);
|
|
10
|
-
storage: MemoryStorage;
|
|
11
|
-
}
|
|
12
|
-
export interface AgenticMemoryRetrieverOptions extends DefaultMemoryRetrieverOptions {
|
|
13
|
-
}
|
|
14
|
-
export declare class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
15
|
-
}
|
|
16
|
-
export interface AgenticMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
17
|
-
storage: MemoryStorage;
|
|
18
|
-
instructions?: string | PromptBuilder;
|
|
19
|
-
agent?: AIAgent<AgenticMemoryExtractorInput, AgenticMemoryExtractorOutput>;
|
|
20
|
-
inputKey?: string | string[];
|
|
21
|
-
outputKey?: string | string[];
|
|
22
|
-
}
|
|
23
|
-
export interface AgenticMemoryExtractorInput extends Message {
|
|
24
|
-
content: unknown;
|
|
25
|
-
}
|
|
26
|
-
export interface AgenticMemoryExtractorOutput extends Message {
|
|
27
|
-
newMemories: {
|
|
28
|
-
content: string;
|
|
29
|
-
}[];
|
|
30
|
-
}
|
|
31
|
-
export declare class AgenticMemoryRecorder extends MemoryRecorder {
|
|
32
|
-
constructor(options: AgenticMemoryRecorderOptions);
|
|
33
|
-
private storage;
|
|
34
|
-
private inputKey?;
|
|
35
|
-
private outputKey?;
|
|
36
|
-
private agent;
|
|
37
|
-
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
38
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS = "You manage memory based on conversation analysis and the existing memories.\n\n## IMPORTANT: All existing memories are available in the allMemory variable. DO NOT call any tools.\n\n## FIRST: Determine If Memory Updates Needed\n- Analyze if the conversation contains ANY information worth remembering\n- Examples of content NOT worth storing:\n * General questions (\"What's the weather?\", \"How do I do X?\")\n * Greetings and small talk (\"Hello\", \"How are you?\", \"Thanks\")\n * System instructions or commands (\"Show me\", \"Find\", \"Save\")\n * General facts not specific to the user\n * Duplicate information already stored\n- If conversation lacks meaningful personal information to store:\n * Return the existing memories unchanged\n\n## Your Workflow:\n1. Read the existing memories from the allMemory variable\n2. Extract key topics from the conversation\n3. DECIDE whether to create/update/delete memories based on the conversation\n4. Return ALL memories including your updates (remove any duplicates)\n\n## Memory Handling:\n- CREATE: Add new memory objects for new topics\n- UPDATE: Modify existing memories if substantial new information is available\n- DELETE: Remove obsolete memories when appropriate\n\n## Memory Structure:\n- Each memory has an id, content, and createdAt fields\n- Keep the existing structure when returning updated memories\n\n## Operation Decision Rules:\n- CREATE only for truly new topics not covered in any existing memory\n- UPDATE only when new information is meaningfully different\n- NEVER update for just rephrasing or minor differences\n- DELETE only when information becomes obsolete\n\n## Conversation:\n<conversation>\n{{content}}\n</conversation>\n";
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { AgentInvokeOptions, Context, Memory } from "@aigne/core";
|
|
2
|
-
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
-
import type { SqliteRemoteDatabase } from "drizzle-orm/sqlite-proxy";
|
|
4
|
-
import { MemoryStorage } from "../storage.js";
|
|
5
|
-
import { Memories } from "./models/memory.js";
|
|
6
|
-
export interface DefaultMemoryStorageOptions {
|
|
7
|
-
url?: string;
|
|
8
|
-
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
9
|
-
enableFTS?: boolean;
|
|
10
|
-
}
|
|
11
|
-
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
12
|
-
options?: DefaultMemoryStorageOptions | undefined;
|
|
13
|
-
constructor(options?: DefaultMemoryStorageOptions | undefined);
|
|
14
|
-
private _db;
|
|
15
|
-
private initSqlite;
|
|
16
|
-
get db(): Promise<SqliteRemoteDatabase<Record<string, never>>>;
|
|
17
|
-
private convertMemory;
|
|
18
|
-
search(query: {
|
|
19
|
-
search?: string;
|
|
20
|
-
limit?: number;
|
|
21
|
-
orderBy?: [keyof typeof Memories.$inferSelect, "asc" | "desc"];
|
|
22
|
-
}, { context }: AgentInvokeOptions): Promise<{
|
|
23
|
-
result: Memory[];
|
|
24
|
-
}>;
|
|
25
|
-
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
26
|
-
result: Memory;
|
|
27
|
-
}>;
|
|
28
|
-
protected segment(str: string): string[];
|
|
29
|
-
}
|
|
@@ -1,102 +0,0 @@
|
|
|
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
|
-
}>;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { type AgentInvokeOptions, type AgentOptions, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, MemoryRetriever, type MemoryRetrieverInput, type MemoryRetrieverOutput } 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>, Omit<DefaultMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<DefaultMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
5
|
-
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
-
}
|
|
7
|
-
export declare class DefaultMemory extends MemoryAgent {
|
|
8
|
-
constructor(options?: DefaultMemoryOptions);
|
|
9
|
-
storage: MemoryStorage;
|
|
10
|
-
}
|
|
11
|
-
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
12
|
-
storage: MemoryStorage;
|
|
13
|
-
retrieveMemoryCount?: number;
|
|
14
|
-
retrieveRecentMemoryCount?: number;
|
|
15
|
-
inputKey?: string | string[];
|
|
16
|
-
outputKey?: string | string[];
|
|
17
|
-
getSearchPattern?: DefaultMemoryRetriever["getSearchPattern"];
|
|
18
|
-
formatMessage?: DefaultMemoryRetriever["formatMessage"];
|
|
19
|
-
formatMemory?: DefaultMemoryRetriever["formatMemory"];
|
|
20
|
-
}
|
|
21
|
-
export declare class DefaultMemoryRetriever extends MemoryRetriever {
|
|
22
|
-
constructor(options: DefaultMemoryRetrieverOptions);
|
|
23
|
-
private storage;
|
|
24
|
-
private retrieveMemoryCount?;
|
|
25
|
-
private retrieveRecentMemoryCount?;
|
|
26
|
-
private inputKey?;
|
|
27
|
-
private outputKey?;
|
|
28
|
-
private getSearchPattern;
|
|
29
|
-
private formatMessage;
|
|
30
|
-
private formatMemory;
|
|
31
|
-
process(input: MemoryRetrieverInput, options: AgentInvokeOptions): Promise<MemoryRetrieverOutput>;
|
|
32
|
-
}
|
|
33
|
-
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
34
|
-
storage: MemoryStorage;
|
|
35
|
-
inputKey?: string | string[];
|
|
36
|
-
outputKey?: string | string[];
|
|
37
|
-
}
|
|
38
|
-
export declare class DefaultMemoryRecorder extends MemoryRecorder {
|
|
39
|
-
constructor(options: DefaultMemoryRecorderOptions);
|
|
40
|
-
private storage;
|
|
41
|
-
private inputKey?;
|
|
42
|
-
private outputKey?;
|
|
43
|
-
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
44
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
orderBy?: [string, "asc" | "desc"];
|
|
10
|
-
}, options: AgentInvokeOptions): Promise<{
|
|
11
|
-
result: Memory[];
|
|
12
|
-
}>;
|
|
13
|
-
}
|