@aigne/default-memory 1.0.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 +18 -0
- package/LICENSE.md +93 -0
- package/README.md +167 -0
- package/lib/cjs/default-memory-storage/index.d.ts +29 -0
- package/lib/cjs/default-memory-storage/index.js +95 -0
- package/lib/cjs/default-memory-storage/migrate.d.ts +3 -0
- package/lib/cjs/default-memory-storage/migrate.js +32 -0
- package/lib/cjs/default-memory-storage/migrations/001-init.d.ts +5 -0
- package/lib/cjs/default-memory-storage/migrations/001-init.js +22 -0
- package/lib/cjs/default-memory-storage/models/memory.d.ts +102 -0
- package/lib/cjs/default-memory-storage/models/memory.js +21 -0
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.js +19 -0
- package/lib/cjs/memory.d.ts +44 -0
- package/lib/cjs/memory.js +137 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/storage.d.ts +13 -0
- package/lib/cjs/storage.js +6 -0
- package/lib/dts/default-memory-storage/index.d.ts +29 -0
- package/lib/dts/default-memory-storage/migrate.d.ts +3 -0
- package/lib/dts/default-memory-storage/migrations/001-init.d.ts +5 -0
- package/lib/dts/default-memory-storage/models/memory.d.ts +102 -0
- package/lib/dts/index.d.ts +3 -0
- package/lib/dts/memory.d.ts +44 -0
- package/lib/dts/storage.d.ts +13 -0
- package/lib/esm/default-memory-storage/index.d.ts +29 -0
- package/lib/esm/default-memory-storage/index.js +91 -0
- package/lib/esm/default-memory-storage/migrate.d.ts +3 -0
- package/lib/esm/default-memory-storage/migrate.js +26 -0
- package/lib/esm/default-memory-storage/migrations/001-init.d.ts +5 -0
- package/lib/esm/default-memory-storage/migrations/001-init.js +20 -0
- package/lib/esm/default-memory-storage/models/memory.d.ts +102 -0
- package/lib/esm/default-memory-storage/models/memory.js +18 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/memory.d.ts +44 -0
- package/lib/esm/memory.js +131 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/storage.d.ts +13 -0
- package/lib/esm/storage.js +2 -0
- package/package.json +50 -0
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { MemoryAgent, MemoryRecorder, MemoryRetriever, } from "@aigne/core";
|
|
2
|
+
import { flat, isRecord, pick } from "@aigne/core/utils/type-utils.js";
|
|
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
|
+
const storage = options.storage instanceof MemoryStorage
|
|
9
|
+
? options.storage
|
|
10
|
+
: new DefaultMemoryStorage(options.storage);
|
|
11
|
+
super({
|
|
12
|
+
...options,
|
|
13
|
+
recorder: options.recorder ?? new DefaultMemoryRecorder({ ...options, storage }),
|
|
14
|
+
retriever: options.retriever ??
|
|
15
|
+
new DefaultMemoryRetriever({
|
|
16
|
+
...options,
|
|
17
|
+
retrieveRecentMemoryCount: options.retrieveRecentMemoryCount ??
|
|
18
|
+
Math.ceil(options.retrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT) / 2,
|
|
19
|
+
storage,
|
|
20
|
+
}),
|
|
21
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
22
|
+
});
|
|
23
|
+
this.storage = storage;
|
|
24
|
+
}
|
|
25
|
+
storage;
|
|
26
|
+
}
|
|
27
|
+
export class DefaultMemoryRetriever extends MemoryRetriever {
|
|
28
|
+
constructor(options) {
|
|
29
|
+
super(options);
|
|
30
|
+
this.storage = options.storage;
|
|
31
|
+
this.retrieveMemoryCount = options.retrieveMemoryCount;
|
|
32
|
+
this.retrieveRecentMemoryCount = options.retrieveRecentMemoryCount;
|
|
33
|
+
this.inputKey = flat(options.inputKey);
|
|
34
|
+
this.outputKey = flat(options.outputKey);
|
|
35
|
+
if (options.getSearchPattern)
|
|
36
|
+
this.getSearchPattern = options.getSearchPattern;
|
|
37
|
+
if (options.formatMessage)
|
|
38
|
+
this.formatMessage = options.formatMessage;
|
|
39
|
+
if (options.formatMemory)
|
|
40
|
+
this.formatMemory = options.formatMemory;
|
|
41
|
+
}
|
|
42
|
+
storage;
|
|
43
|
+
retrieveMemoryCount;
|
|
44
|
+
retrieveRecentMemoryCount;
|
|
45
|
+
inputKey;
|
|
46
|
+
outputKey;
|
|
47
|
+
getSearchPattern = (search) => {
|
|
48
|
+
if (!search || typeof search === "string")
|
|
49
|
+
return search;
|
|
50
|
+
const obj = search && this.inputKey ? pick(search, this.inputKey) : search;
|
|
51
|
+
return Object.values(obj)
|
|
52
|
+
.map((v) => (typeof v === "string" ? v : undefined))
|
|
53
|
+
.join("\n");
|
|
54
|
+
};
|
|
55
|
+
formatMessage = (content, key) => {
|
|
56
|
+
if (!isRecord(content))
|
|
57
|
+
return content;
|
|
58
|
+
const obj = !key?.length ? content : pick(content, key);
|
|
59
|
+
return Object.values(obj)
|
|
60
|
+
.map((v) => (typeof v === "string" ? v : undefined))
|
|
61
|
+
.join("\n");
|
|
62
|
+
};
|
|
63
|
+
formatMemory = (content) => {
|
|
64
|
+
if (isRecord(content) && "input" in content && "output" in content) {
|
|
65
|
+
return {
|
|
66
|
+
input: this.formatMessage(content.input, this.inputKey),
|
|
67
|
+
output: this.formatMessage(content.output, this.outputKey),
|
|
68
|
+
source: content.source,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return content;
|
|
72
|
+
};
|
|
73
|
+
async process(input, options) {
|
|
74
|
+
const limit = input.limit ?? this.retrieveMemoryCount ?? DEFAULT_RETRIEVE_MEMORY_COUNT;
|
|
75
|
+
const search = this.getSearchPattern(input.search);
|
|
76
|
+
const recentLimit = this.retrieveRecentMemoryCount;
|
|
77
|
+
const [recent, related] = await Promise.all([
|
|
78
|
+
// Query latest messages
|
|
79
|
+
!recentLimit
|
|
80
|
+
? []
|
|
81
|
+
: this.storage
|
|
82
|
+
.search({ limit: recentLimit, orderBy: ["createdAt", "desc"] }, options)
|
|
83
|
+
.then(({ result }) => result.reverse()),
|
|
84
|
+
// Query related messages
|
|
85
|
+
!input.search
|
|
86
|
+
? []
|
|
87
|
+
: this.storage.search({ ...input, search, limit }, options).then(({ result }) => result),
|
|
88
|
+
]);
|
|
89
|
+
const recentSet = new Set(recent.map((i) => i.id));
|
|
90
|
+
const memories = related
|
|
91
|
+
// Filter out recent memories from related results
|
|
92
|
+
.filter((i) => !recentSet.has(i.id))
|
|
93
|
+
.concat(recent)
|
|
94
|
+
.slice(-limit);
|
|
95
|
+
return {
|
|
96
|
+
memories: memories.map((i) => ({
|
|
97
|
+
...i,
|
|
98
|
+
content: this.formatMemory(i.content),
|
|
99
|
+
})),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export class DefaultMemoryRecorder extends MemoryRecorder {
|
|
104
|
+
constructor(options) {
|
|
105
|
+
super(options);
|
|
106
|
+
this.storage = options.storage;
|
|
107
|
+
this.inputKey = flat(options.inputKey);
|
|
108
|
+
this.outputKey = 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 ? pick(item.input, this.inputKey) : item.input,
|
|
119
|
+
output: item.output && this.outputKey?.length
|
|
120
|
+
? 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
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/default-memory",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Default memory for AIGNE framework",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
|
|
9
|
+
"homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/memory/default",
|
|
10
|
+
"license": "Elastic-2.0",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/AIGNE-io/aigne-framework"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib/cjs",
|
|
17
|
+
"lib/dts",
|
|
18
|
+
"lib/esm",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"README.md",
|
|
21
|
+
"CHANGELOG.md"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./lib/cjs/index.js",
|
|
25
|
+
"module": "./lib/esm/index.js",
|
|
26
|
+
"types": "./lib/dts/index.d.ts",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"drizzle-orm": "^0.44.2",
|
|
29
|
+
"uuid": "^11.1.0",
|
|
30
|
+
"yaml": "^2.8.0",
|
|
31
|
+
"zod": "^3.25.67",
|
|
32
|
+
"@aigne/core": "^1.34.0",
|
|
33
|
+
"@aigne/sqlite": "^0.4.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/bun": "^1.2.18",
|
|
37
|
+
"npm-run-all": "^4.1.5",
|
|
38
|
+
"rimraf": "^6.0.1",
|
|
39
|
+
"typescript": "^5.8.3",
|
|
40
|
+
"@aigne/openai": "^0.10.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"lint": "tsc --noEmit",
|
|
44
|
+
"build": "tsc --build scripts/tsconfig.build.json",
|
|
45
|
+
"clean": "rimraf lib test/coverage",
|
|
46
|
+
"test": "bun test",
|
|
47
|
+
"test:coverage": "bun test --coverage --coverage-reporter=lcov --coverage-reporter=text",
|
|
48
|
+
"postbuild": "node ../../scripts/post-build-lib.mjs"
|
|
49
|
+
}
|
|
50
|
+
}
|