@aigne/agentic-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 +17 -0
- package/LICENSE.md +93 -0
- package/README.md +128 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +17 -0
- package/lib/cjs/memory.d.ts +36 -0
- package/lib/cjs/memory.js +71 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/prompt.d.ts +1 -0
- package/lib/cjs/prompt.js +44 -0
- package/lib/dts/index.d.ts +1 -0
- package/lib/dts/memory.d.ts +36 -0
- package/lib/dts/prompt.d.ts +1 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/memory.d.ts +36 -0
- package/lib/esm/memory.js +65 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/prompt.d.ts +1 -0
- package/lib/esm/prompt.js +41 -0
- package/package.json +47 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 (2025-07-15)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **memory:** support did space memory adapter ([#229](https://github.com/AIGNE-io/aigne-framework/issues/229)) ([6f69b64](https://github.com/AIGNE-io/aigne-framework/commit/6f69b64e98b963db9d6ab5357306b445385eaa68))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @aigne/core bumped to 1.34.0
|
|
16
|
+
* @aigne/default-memory bumped to 1.0.0
|
|
17
|
+
* @aigne/openai bumped to 0.10.0
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Elastic License 2.0
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor’s trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
70
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
71
|
+
legal claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that
|
|
84
|
+
organization. **control** means ownership of substantially all the assets of an
|
|
85
|
+
entity, or the power to direct its management and policies by vote, contract, or
|
|
86
|
+
otherwise. Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# @aigne/agentic-memory
|
|
2
|
+
|
|
3
|
+
[](https://star-history.com/#AIGNE-io/aigne-framework)
|
|
4
|
+
[](https://github.com/AIGNE-io/aigne-framework/issues)
|
|
5
|
+
[](https://codecov.io/gh/AIGNE-io/aigne-framework)
|
|
6
|
+
[](https://www.npmjs.com/package/@aigne/agentic-memory)
|
|
7
|
+
[](https://github.com/AIGNE-io/aigne-framework/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
Agentic memory system component for [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework), providing AI-powered memory management capabilities.
|
|
10
|
+
|
|
11
|
+
## Introduction
|
|
12
|
+
|
|
13
|
+
`@aigne/agentic-memory` is an intelligent memory system component of [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework), providing AI agent-based memory management functionality. This component uses intelligent agents to handle memory recording, retrieval, and management, providing smarter memory capabilities for AI applications.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
* **Intelligent Memory Management**: Uses AI agents to manage memory recording and retrieval processes
|
|
18
|
+
* **Automatic Memory Updates**: Supports automatic memory update mechanisms
|
|
19
|
+
* **Storage Adaptation**: Compatible with multiple storage backends, including default SQLite storage
|
|
20
|
+
* **Memory Filtering**: Intelligent filtering and organization of memory content
|
|
21
|
+
* **TypeScript Support**: Complete type definitions providing an excellent development experience
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
### Using npm
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @aigne/agentic-memory
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Using yarn
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
yarn add @aigne/agentic-memory
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Using pnpm
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pnpm add @aigne/agentic-memory
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Basic Usage
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { AIAgent, AIGNE } from "@aigne/core";
|
|
47
|
+
import { AgenticMemory } from "@aigne/agentic-memory";
|
|
48
|
+
import { OpenAIChatModel } from "@aigne/openai";
|
|
49
|
+
|
|
50
|
+
// Create AI model instance
|
|
51
|
+
const model = new OpenAIChatModel({
|
|
52
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
53
|
+
model: "gpt-4-turbo",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Create intelligent memory system
|
|
57
|
+
const memory = new AgenticMemory({
|
|
58
|
+
storage: {
|
|
59
|
+
url: "file:memory.db",
|
|
60
|
+
},
|
|
61
|
+
autoUpdate: true,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Create AI agent with intelligent memory
|
|
65
|
+
const agent = AIAgent.from({
|
|
66
|
+
name: "SmartAssistant",
|
|
67
|
+
instructions: "You are a helpful assistant with intelligent memory capabilities.",
|
|
68
|
+
memory: memory,
|
|
69
|
+
inputKey: "message",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Use AIGNE execution engine
|
|
73
|
+
const aigne = new AIGNE({ model });
|
|
74
|
+
|
|
75
|
+
// Invoke agent
|
|
76
|
+
const userAgent = await aigne.invoke(agent);
|
|
77
|
+
|
|
78
|
+
// Send message
|
|
79
|
+
const response = await userAgent.invoke({
|
|
80
|
+
message: "Remember that I like to drink coffee in the morning",
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
console.log(response.message);
|
|
84
|
+
|
|
85
|
+
// Query memory later
|
|
86
|
+
const response2 = await userAgent.invoke({
|
|
87
|
+
message: "What do you know about my morning routine?",
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
console.log(response2.message);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Advanced Configuration
|
|
94
|
+
|
|
95
|
+
### Custom Storage Options
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { AgenticMemory } from "@aigne/agentic-memory";
|
|
99
|
+
import { DefaultMemoryStorage } from "@aigne/default-memory";
|
|
100
|
+
|
|
101
|
+
const customStorage = new DefaultMemoryStorage({
|
|
102
|
+
url: "file:custom-memory.db",
|
|
103
|
+
// Other storage configurations
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const memory = new AgenticMemory({
|
|
107
|
+
storage: customStorage,
|
|
108
|
+
autoUpdate: true,
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Configure Memory Agent Options
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const memory = new AgenticMemory({
|
|
116
|
+
storage: {
|
|
117
|
+
url: "file:memory.db",
|
|
118
|
+
},
|
|
119
|
+
// Custom memory recorder options
|
|
120
|
+
name: "CustomMemoryAgent",
|
|
121
|
+
instructions: "Custom memory management instructions",
|
|
122
|
+
// Other agent configurations
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
Elastic-2.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./memory.js";
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./memory.js"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, AIAgent, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, type Message, type PromptBuilder } from "@aigne/core";
|
|
2
|
+
import { DefaultMemoryRetriever, type DefaultMemoryRetrieverOptions, type DefaultMemoryStorageOptions, MemoryStorage } from "@aigne/default-memory";
|
|
3
|
+
export interface AgenticMemoryOptions extends Partial<MemoryAgentOptions>, Omit<AgenticMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<AgenticMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
4
|
+
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
5
|
+
}
|
|
6
|
+
export declare class AgenticMemory extends MemoryAgent {
|
|
7
|
+
constructor(options?: AgenticMemoryOptions);
|
|
8
|
+
storage: MemoryStorage;
|
|
9
|
+
}
|
|
10
|
+
export interface AgenticMemoryRetrieverOptions extends DefaultMemoryRetrieverOptions {
|
|
11
|
+
}
|
|
12
|
+
export declare class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
13
|
+
}
|
|
14
|
+
export interface AgenticMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
15
|
+
storage: MemoryStorage;
|
|
16
|
+
instructions?: string | PromptBuilder;
|
|
17
|
+
agent?: AIAgent<AgenticMemoryExtractorInput, AgenticMemoryExtractorOutput>;
|
|
18
|
+
inputKey?: string | string[];
|
|
19
|
+
outputKey?: string | string[];
|
|
20
|
+
}
|
|
21
|
+
export interface AgenticMemoryExtractorInput extends Message {
|
|
22
|
+
content: unknown;
|
|
23
|
+
}
|
|
24
|
+
export interface AgenticMemoryExtractorOutput extends Message {
|
|
25
|
+
newMemories: {
|
|
26
|
+
content: string;
|
|
27
|
+
}[];
|
|
28
|
+
}
|
|
29
|
+
export declare class AgenticMemoryRecorder extends MemoryRecorder {
|
|
30
|
+
constructor(options: AgenticMemoryRecorderOptions);
|
|
31
|
+
private storage;
|
|
32
|
+
private inputKey?;
|
|
33
|
+
private outputKey?;
|
|
34
|
+
private agent;
|
|
35
|
+
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgenticMemoryRecorder = exports.AgenticMemoryRetriever = exports.AgenticMemory = void 0;
|
|
4
|
+
const core_1 = require("@aigne/core");
|
|
5
|
+
const type_utils_js_1 = require("@aigne/core/utils/type-utils.js");
|
|
6
|
+
const default_memory_1 = require("@aigne/default-memory");
|
|
7
|
+
const zod_1 = require("zod");
|
|
8
|
+
const prompt_js_1 = require("./prompt.js");
|
|
9
|
+
class AgenticMemory extends core_1.MemoryAgent {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
const storage = options.storage instanceof default_memory_1.MemoryStorage
|
|
12
|
+
? options.storage
|
|
13
|
+
: new default_memory_1.DefaultMemoryStorage(options.storage);
|
|
14
|
+
super({
|
|
15
|
+
...options,
|
|
16
|
+
recorder: options.recorder ?? new AgenticMemoryRecorder({ ...options, storage }),
|
|
17
|
+
retriever: options.retriever ?? new AgenticMemoryRetriever({ ...options, storage }),
|
|
18
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
19
|
+
});
|
|
20
|
+
this.storage = storage;
|
|
21
|
+
}
|
|
22
|
+
storage;
|
|
23
|
+
}
|
|
24
|
+
exports.AgenticMemory = AgenticMemory;
|
|
25
|
+
class AgenticMemoryRetriever extends default_memory_1.DefaultMemoryRetriever {
|
|
26
|
+
}
|
|
27
|
+
exports.AgenticMemoryRetriever = AgenticMemoryRetriever;
|
|
28
|
+
class AgenticMemoryRecorder extends core_1.MemoryRecorder {
|
|
29
|
+
constructor(options) {
|
|
30
|
+
super(options);
|
|
31
|
+
this.storage = options.storage;
|
|
32
|
+
this.inputKey = (0, type_utils_js_1.flat)(options.inputKey);
|
|
33
|
+
this.outputKey = (0, type_utils_js_1.flat)(options.outputKey);
|
|
34
|
+
this.agent =
|
|
35
|
+
options.agent ??
|
|
36
|
+
core_1.AIAgent.from({
|
|
37
|
+
name: "agentic_memory_extractor",
|
|
38
|
+
description: "Records memories in files by AI agent",
|
|
39
|
+
instructions: options.instructions || prompt_js_1.DEFAULT_AGENTIC_MEMORY_RECORDER_INSTRUCTIONS,
|
|
40
|
+
outputSchema: zod_1.z.object({
|
|
41
|
+
newMemories: zod_1.z
|
|
42
|
+
.array(zod_1.z.object({
|
|
43
|
+
content: zod_1.z.string().describe("Content of the memory"),
|
|
44
|
+
}))
|
|
45
|
+
.describe("Newly created memories"),
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
storage;
|
|
50
|
+
inputKey;
|
|
51
|
+
outputKey;
|
|
52
|
+
agent;
|
|
53
|
+
async process(input, options) {
|
|
54
|
+
const agenticMemories = await options.context.invoke(this.agent, {
|
|
55
|
+
content: input.content.map((item) => ({
|
|
56
|
+
input: item.input && this.inputKey?.length ? (0, type_utils_js_1.pick)(item.input, this.inputKey) : item.input,
|
|
57
|
+
output: item.output && this.outputKey?.length ? (0, type_utils_js_1.pick)(item.output, this.outputKey) : item.output,
|
|
58
|
+
source: item.source,
|
|
59
|
+
})),
|
|
60
|
+
});
|
|
61
|
+
const newMemories = [];
|
|
62
|
+
for (const item of agenticMemories.newMemories) {
|
|
63
|
+
const { result } = await this.storage.create({ content: item.content }, options);
|
|
64
|
+
newMemories.push(result);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
memories: newMemories,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.AgenticMemoryRecorder = AgenticMemoryRecorder;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_AGENTIC_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";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_AGENTIC_MEMORY_RECORDER_INSTRUCTIONS = void 0;
|
|
4
|
+
exports.DEFAULT_AGENTIC_MEMORY_RECORDER_INSTRUCTIONS = `You manage memory based on conversation analysis and the existing memories.
|
|
5
|
+
|
|
6
|
+
## IMPORTANT: All existing memories are available in the allMemory variable. DO NOT call any tools.
|
|
7
|
+
|
|
8
|
+
## FIRST: Determine If Memory Updates Needed
|
|
9
|
+
- Analyze if the conversation contains ANY information worth remembering
|
|
10
|
+
- Examples of content NOT worth storing:
|
|
11
|
+
* General questions ("What's the weather?", "How do I do X?")
|
|
12
|
+
* Greetings and small talk ("Hello", "How are you?", "Thanks")
|
|
13
|
+
* System instructions or commands ("Show me", "Find", "Save")
|
|
14
|
+
* General facts not specific to the user
|
|
15
|
+
* Duplicate information already stored
|
|
16
|
+
- If conversation lacks meaningful personal information to store:
|
|
17
|
+
* Return the existing memories unchanged
|
|
18
|
+
|
|
19
|
+
## Your Workflow:
|
|
20
|
+
1. Read the existing memories from the allMemory variable
|
|
21
|
+
2. Extract key topics from the conversation
|
|
22
|
+
3. DECIDE whether to create/update/delete memories based on the conversation
|
|
23
|
+
4. Return ALL memories including your updates (remove any duplicates)
|
|
24
|
+
|
|
25
|
+
## Memory Handling:
|
|
26
|
+
- CREATE: Add new memory objects for new topics
|
|
27
|
+
- UPDATE: Modify existing memories if substantial new information is available
|
|
28
|
+
- DELETE: Remove obsolete memories when appropriate
|
|
29
|
+
|
|
30
|
+
## Memory Structure:
|
|
31
|
+
- Each memory has an id, content, and createdAt fields
|
|
32
|
+
- Keep the existing structure when returning updated memories
|
|
33
|
+
|
|
34
|
+
## Operation Decision Rules:
|
|
35
|
+
- CREATE only for truly new topics not covered in any existing memory
|
|
36
|
+
- UPDATE only when new information is meaningfully different
|
|
37
|
+
- NEVER update for just rephrasing or minor differences
|
|
38
|
+
- DELETE only when information becomes obsolete
|
|
39
|
+
|
|
40
|
+
## Conversation:
|
|
41
|
+
<conversation>
|
|
42
|
+
{{content}}
|
|
43
|
+
</conversation>
|
|
44
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./memory.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, AIAgent, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, type Message, type PromptBuilder } from "@aigne/core";
|
|
2
|
+
import { DefaultMemoryRetriever, type DefaultMemoryRetrieverOptions, type DefaultMemoryStorageOptions, MemoryStorage } from "@aigne/default-memory";
|
|
3
|
+
export interface AgenticMemoryOptions extends Partial<MemoryAgentOptions>, Omit<AgenticMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<AgenticMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
4
|
+
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
5
|
+
}
|
|
6
|
+
export declare class AgenticMemory extends MemoryAgent {
|
|
7
|
+
constructor(options?: AgenticMemoryOptions);
|
|
8
|
+
storage: MemoryStorage;
|
|
9
|
+
}
|
|
10
|
+
export interface AgenticMemoryRetrieverOptions extends DefaultMemoryRetrieverOptions {
|
|
11
|
+
}
|
|
12
|
+
export declare class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
13
|
+
}
|
|
14
|
+
export interface AgenticMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
15
|
+
storage: MemoryStorage;
|
|
16
|
+
instructions?: string | PromptBuilder;
|
|
17
|
+
agent?: AIAgent<AgenticMemoryExtractorInput, AgenticMemoryExtractorOutput>;
|
|
18
|
+
inputKey?: string | string[];
|
|
19
|
+
outputKey?: string | string[];
|
|
20
|
+
}
|
|
21
|
+
export interface AgenticMemoryExtractorInput extends Message {
|
|
22
|
+
content: unknown;
|
|
23
|
+
}
|
|
24
|
+
export interface AgenticMemoryExtractorOutput extends Message {
|
|
25
|
+
newMemories: {
|
|
26
|
+
content: string;
|
|
27
|
+
}[];
|
|
28
|
+
}
|
|
29
|
+
export declare class AgenticMemoryRecorder extends MemoryRecorder {
|
|
30
|
+
constructor(options: AgenticMemoryRecorderOptions);
|
|
31
|
+
private storage;
|
|
32
|
+
private inputKey?;
|
|
33
|
+
private outputKey?;
|
|
34
|
+
private agent;
|
|
35
|
+
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_AGENTIC_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";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./memory.js";
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./memory.js";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type AgentInvokeOptions, type AgentOptions, AIAgent, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, type Message, type PromptBuilder } from "@aigne/core";
|
|
2
|
+
import { DefaultMemoryRetriever, type DefaultMemoryRetrieverOptions, type DefaultMemoryStorageOptions, MemoryStorage } from "@aigne/default-memory";
|
|
3
|
+
export interface AgenticMemoryOptions extends Partial<MemoryAgentOptions>, Omit<AgenticMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<AgenticMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
4
|
+
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
5
|
+
}
|
|
6
|
+
export declare class AgenticMemory extends MemoryAgent {
|
|
7
|
+
constructor(options?: AgenticMemoryOptions);
|
|
8
|
+
storage: MemoryStorage;
|
|
9
|
+
}
|
|
10
|
+
export interface AgenticMemoryRetrieverOptions extends DefaultMemoryRetrieverOptions {
|
|
11
|
+
}
|
|
12
|
+
export declare class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
13
|
+
}
|
|
14
|
+
export interface AgenticMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
15
|
+
storage: MemoryStorage;
|
|
16
|
+
instructions?: string | PromptBuilder;
|
|
17
|
+
agent?: AIAgent<AgenticMemoryExtractorInput, AgenticMemoryExtractorOutput>;
|
|
18
|
+
inputKey?: string | string[];
|
|
19
|
+
outputKey?: string | string[];
|
|
20
|
+
}
|
|
21
|
+
export interface AgenticMemoryExtractorInput extends Message {
|
|
22
|
+
content: unknown;
|
|
23
|
+
}
|
|
24
|
+
export interface AgenticMemoryExtractorOutput extends Message {
|
|
25
|
+
newMemories: {
|
|
26
|
+
content: string;
|
|
27
|
+
}[];
|
|
28
|
+
}
|
|
29
|
+
export declare class AgenticMemoryRecorder extends MemoryRecorder {
|
|
30
|
+
constructor(options: AgenticMemoryRecorderOptions);
|
|
31
|
+
private storage;
|
|
32
|
+
private inputKey?;
|
|
33
|
+
private outputKey?;
|
|
34
|
+
private agent;
|
|
35
|
+
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { AIAgent, MemoryAgent, MemoryRecorder, } from "@aigne/core";
|
|
2
|
+
import { flat, pick } from "@aigne/core/utils/type-utils.js";
|
|
3
|
+
import { DefaultMemoryRetriever, DefaultMemoryStorage, MemoryStorage, } from "@aigne/default-memory";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { DEFAULT_AGENTIC_MEMORY_RECORDER_INSTRUCTIONS } from "./prompt.js";
|
|
6
|
+
export class AgenticMemory 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 AgenticMemoryRecorder({ ...options, storage }),
|
|
14
|
+
retriever: options.retriever ?? new AgenticMemoryRetriever({ ...options, storage }),
|
|
15
|
+
autoUpdate: options.autoUpdate ?? true,
|
|
16
|
+
});
|
|
17
|
+
this.storage = storage;
|
|
18
|
+
}
|
|
19
|
+
storage;
|
|
20
|
+
}
|
|
21
|
+
export class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
22
|
+
}
|
|
23
|
+
export class AgenticMemoryRecorder extends MemoryRecorder {
|
|
24
|
+
constructor(options) {
|
|
25
|
+
super(options);
|
|
26
|
+
this.storage = options.storage;
|
|
27
|
+
this.inputKey = flat(options.inputKey);
|
|
28
|
+
this.outputKey = flat(options.outputKey);
|
|
29
|
+
this.agent =
|
|
30
|
+
options.agent ??
|
|
31
|
+
AIAgent.from({
|
|
32
|
+
name: "agentic_memory_extractor",
|
|
33
|
+
description: "Records memories in files by AI agent",
|
|
34
|
+
instructions: options.instructions || DEFAULT_AGENTIC_MEMORY_RECORDER_INSTRUCTIONS,
|
|
35
|
+
outputSchema: z.object({
|
|
36
|
+
newMemories: z
|
|
37
|
+
.array(z.object({
|
|
38
|
+
content: z.string().describe("Content of the memory"),
|
|
39
|
+
}))
|
|
40
|
+
.describe("Newly created memories"),
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
storage;
|
|
45
|
+
inputKey;
|
|
46
|
+
outputKey;
|
|
47
|
+
agent;
|
|
48
|
+
async process(input, options) {
|
|
49
|
+
const agenticMemories = await options.context.invoke(this.agent, {
|
|
50
|
+
content: input.content.map((item) => ({
|
|
51
|
+
input: item.input && this.inputKey?.length ? pick(item.input, this.inputKey) : item.input,
|
|
52
|
+
output: item.output && this.outputKey?.length ? pick(item.output, this.outputKey) : item.output,
|
|
53
|
+
source: item.source,
|
|
54
|
+
})),
|
|
55
|
+
});
|
|
56
|
+
const newMemories = [];
|
|
57
|
+
for (const item of agenticMemories.newMemories) {
|
|
58
|
+
const { result } = await this.storage.create({ content: item.content }, options);
|
|
59
|
+
newMemories.push(result);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
memories: newMemories,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_AGENTIC_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";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const DEFAULT_AGENTIC_MEMORY_RECORDER_INSTRUCTIONS = `You manage memory based on conversation analysis and the existing memories.
|
|
2
|
+
|
|
3
|
+
## IMPORTANT: All existing memories are available in the allMemory variable. DO NOT call any tools.
|
|
4
|
+
|
|
5
|
+
## FIRST: Determine If Memory Updates Needed
|
|
6
|
+
- Analyze if the conversation contains ANY information worth remembering
|
|
7
|
+
- Examples of content NOT worth storing:
|
|
8
|
+
* General questions ("What's the weather?", "How do I do X?")
|
|
9
|
+
* Greetings and small talk ("Hello", "How are you?", "Thanks")
|
|
10
|
+
* System instructions or commands ("Show me", "Find", "Save")
|
|
11
|
+
* General facts not specific to the user
|
|
12
|
+
* Duplicate information already stored
|
|
13
|
+
- If conversation lacks meaningful personal information to store:
|
|
14
|
+
* Return the existing memories unchanged
|
|
15
|
+
|
|
16
|
+
## Your Workflow:
|
|
17
|
+
1. Read the existing memories from the allMemory variable
|
|
18
|
+
2. Extract key topics from the conversation
|
|
19
|
+
3. DECIDE whether to create/update/delete memories based on the conversation
|
|
20
|
+
4. Return ALL memories including your updates (remove any duplicates)
|
|
21
|
+
|
|
22
|
+
## Memory Handling:
|
|
23
|
+
- CREATE: Add new memory objects for new topics
|
|
24
|
+
- UPDATE: Modify existing memories if substantial new information is available
|
|
25
|
+
- DELETE: Remove obsolete memories when appropriate
|
|
26
|
+
|
|
27
|
+
## Memory Structure:
|
|
28
|
+
- Each memory has an id, content, and createdAt fields
|
|
29
|
+
- Keep the existing structure when returning updated memories
|
|
30
|
+
|
|
31
|
+
## Operation Decision Rules:
|
|
32
|
+
- CREATE only for truly new topics not covered in any existing memory
|
|
33
|
+
- UPDATE only when new information is meaningfully different
|
|
34
|
+
- NEVER update for just rephrasing or minor differences
|
|
35
|
+
- DELETE only when information becomes obsolete
|
|
36
|
+
|
|
37
|
+
## Conversation:
|
|
38
|
+
<conversation>
|
|
39
|
+
{{content}}
|
|
40
|
+
</conversation>
|
|
41
|
+
`;
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/agentic-memory",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Agentic 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/agentic-memory",
|
|
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
|
+
"zod": "^3.25.67",
|
|
29
|
+
"@aigne/core": "^1.34.0",
|
|
30
|
+
"@aigne/default-memory": "^1.0.0",
|
|
31
|
+
"@aigne/openai": "^0.10.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/bun": "^1.2.18",
|
|
35
|
+
"npm-run-all": "^4.1.5",
|
|
36
|
+
"rimraf": "^6.0.1",
|
|
37
|
+
"typescript": "^5.8.3"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"lint": "tsc --noEmit",
|
|
41
|
+
"build": "tsc --build scripts/tsconfig.build.json",
|
|
42
|
+
"clean": "rimraf lib test/coverage",
|
|
43
|
+
"test": "bun test",
|
|
44
|
+
"test:coverage": "bun test --coverage --coverage-reporter=lcov --coverage-reporter=text",
|
|
45
|
+
"postbuild": "node ../../scripts/post-build-lib.mjs"
|
|
46
|
+
}
|
|
47
|
+
}
|