@aigne/example-afs-memory 0.10.70-beta

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.
@@ -0,0 +1,44 @@
1
+ # Change the name of this file to .env.local and fill in the following values
2
+
3
+ # Uncomment the lines below to enable debug logging
4
+ # DEBUG="aigne:*"
5
+
6
+ # Use different Models
7
+
8
+ # OpenAI
9
+ MODEL="openai:gpt-4.1"
10
+ OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
11
+
12
+ # Anthropic claude
13
+ # MODEL="anthropic:claude-3-7-sonnet-latest"
14
+ # ANTHROPIC_API_KEY=""
15
+
16
+ # Gemini
17
+ # MODEL="gemini:gemini-2.0-flash"
18
+ # GEMINI_API_KEY=""
19
+
20
+ # Bedrock nova
21
+ # MODEL=bedrock:us.amazon.nova-premier-v1:0
22
+ # AWS_ACCESS_KEY_ID=""
23
+ # AWS_SECRET_ACCESS_KEY=""
24
+ # AWS_REGION=us-west-2
25
+
26
+ # DeepSeek
27
+ # MODEL="deepseek:deepseek-chat"
28
+ # DEEPSEEK_API_KEY=""
29
+
30
+ # OpenRouter
31
+ # MODEL="openrouter:openai/gpt-4o"
32
+ # OPEN_ROUTER_API_KEY=""
33
+
34
+ # xAI
35
+ # MODEL="xai:grok-2-latest"
36
+ # XAI_API_KEY=""
37
+
38
+ # Ollama
39
+ # MODEL="ollama:llama3.2"
40
+ # OLLAMA_DEFAULT_BASE_URL="http://localhost:11434/v1";
41
+
42
+
43
+ # Setup proxy if needed
44
+ # HTTPS_PROXY=http://localhost:7890
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,242 @@
1
+ # Memory Example
2
+
3
+ <p align="center">
4
+ <picture>
5
+ <source srcset="https://raw.githubusercontent.com/AIGNE-io/aigne-framework/main/logo-dark.svg" media="(prefers-color-scheme: dark)">
6
+ <source srcset="https://raw.githubusercontent.com/AIGNE-io/aigne-framework/main/logo.svg" media="(prefers-color-scheme: light)">
7
+ <img src="https://raw.githubusercontent.com/AIGNE-io/aigne-framework/main/logo.svg" alt="AIGNE Logo" width="400" />
8
+ </picture>
9
+ </p>
10
+
11
+ This example demonstrates how to create a chatbot with **long-term memory** using the [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework). The chatbot can remember user information across conversations and recall details shared in previous interactions.
12
+
13
+ The example uses two powerful AFS (Agentic File System) modules:
14
+ - **AFSHistory**: Automatically records all conversations for context
15
+ - **UserProfileMemory**: Intelligently extracts and stores user information (name, interests, location, etc.)
16
+
17
+ **Agentic File System (AFS)** is a virtual file system abstraction that provides AI agents with unified access to various storage backends. For comprehensive documentation, see [AFS Documentation](../../afs/README.md).
18
+
19
+ ## Prerequisites
20
+
21
+ * [Node.js](https://nodejs.org) (>=20.0) and npm installed on your machine
22
+ * An [OpenAI API key](https://platform.openai.com/api-keys) for interacting with OpenAI's services
23
+ * Optional dependencies (if running the example from source code):
24
+ * [Pnpm](https://pnpm.io) for package management
25
+ * [Bun](https://bun.sh) for running unit tests & examples
26
+
27
+ ## Quick Start (No Installation Required)
28
+
29
+ ```bash
30
+ export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # Set your OpenAI API key
31
+
32
+ # First conversation - introduce yourself
33
+ npx -y @aigne/example-afs-memory --input "I'm Bob, and I like blue color"
34
+ # Response: Nice to meet you, Bob — I've saved that your favorite color is blue...
35
+
36
+ # Second conversation - the bot remembers you!
37
+ npx -y @aigne/example-afs-memory --input "Tell me all info about me you known"
38
+ # Response: Here's what I currently have stored about you:
39
+ # * Name: Bob
40
+ # * Interests / favorite color: blue
41
+
42
+ # Run in interactive chat mode
43
+ npx -y @aigne/example-afs-memory --chat
44
+ ```
45
+
46
+ ## Installation
47
+
48
+ ### Clone the Repository
49
+
50
+ ```bash
51
+ git clone https://github.com/AIGNE-io/aigne-framework
52
+ ```
53
+
54
+ ### Install Dependencies
55
+
56
+ ```bash
57
+ cd aigne-framework/examples/afs-memory
58
+
59
+ pnpm install
60
+ ```
61
+
62
+ ### Setup Environment Variables
63
+
64
+ Setup your OpenAI API key in the `.env.local` file:
65
+
66
+ ```bash
67
+ OPENAI_API_KEY="" # Set your OpenAI API key here
68
+ ```
69
+
70
+ #### Using Different Models
71
+
72
+ You can use different AI models by setting the `MODEL` environment variable along with the corresponding API key. The framework supports multiple providers:
73
+
74
+ * **OpenAI**: `MODEL="openai:gpt-4.1"` with `OPENAI_API_KEY`
75
+ * **Anthropic**: `MODEL="anthropic:claude-3-7-sonnet-latest"` with `ANTHROPIC_API_KEY`
76
+ * **Google Gemini**: `MODEL="gemini:gemini-2.0-flash"` with `GEMINI_API_KEY`
77
+ * **AWS Bedrock**: `MODEL="bedrock:us.amazon.nova-premier-v1:0"` with AWS credentials
78
+ * **DeepSeek**: `MODEL="deepseek:deepseek-chat"` with `DEEPSEEK_API_KEY`
79
+ * **OpenRouter**: `MODEL="openrouter:openai/gpt-4o"` with `OPEN_ROUTER_API_KEY`
80
+ * **xAI**: `MODEL="xai:grok-2-latest"` with `XAI_API_KEY`
81
+ * **Ollama**: `MODEL="ollama:llama3.2"` with `OLLAMA_DEFAULT_BASE_URL`
82
+
83
+ For detailed configuration examples, please refer to the `.env.local.example` file in this directory.
84
+
85
+ ### Run the Example
86
+
87
+ ```bash
88
+ # Run in interactive mode
89
+ pnpm start
90
+
91
+ # Run with a single message
92
+ pnpm start --input "I'm Bob, and I like blue color"
93
+ ```
94
+
95
+ ### Example Conversation Flow
96
+
97
+ ```bash
98
+ # First conversation - introduce yourself
99
+ $ pnpm start --input "I'm Bob, and I like blue color"
100
+ Response: Nice to meet you, Bob — I've saved that your favorite color is blue.
101
+ If you'd like, I can remember more about you (location, hobbies, projects, etc.)...
102
+
103
+ # Second conversation - the bot remembers you!
104
+ $ pnpm start --input "Tell me all info about me you known"
105
+ Response: Here's what I currently have stored about you:
106
+ * Name: Bob
107
+ * Interests / favorite color: blue
108
+
109
+ Would you like to add or update anything (location, hobbies, projects, family, etc.)?
110
+ ```
111
+
112
+ ## How Memory Works
113
+
114
+ This example uses two complementary memory modules that work together to provide intelligent, personalized conversations:
115
+
116
+ ### 1. AFSHistory Module - Conversation Context
117
+
118
+ **Purpose**: Records every conversation turn to provide recent context.
119
+
120
+ **How it works**:
121
+ - Automatically saves each user message and AI response pair
122
+ - Stores conversations with timestamps and unique IDs
123
+ - Enables the AI to reference recent conversations
124
+
125
+ **Example**:
126
+ ```bash
127
+ # First conversation
128
+ $ pnpm start --input "I'm Bob, and I like blue color"
129
+ Response: Nice to meet you, Bob — I've saved that your favorite color is blue...
130
+
131
+ # The conversation is automatically saved to /history/{uuid}
132
+ ```
133
+
134
+ ### 2. UserProfileMemory Module - User Information Extraction
135
+
136
+ **Purpose**: Intelligently extracts and stores structured user information from conversations.
137
+
138
+ **How it works**:
139
+ 1. **Listens** to conversation history events
140
+ 2. **Analyzes** each conversation using AI to identify user information
141
+ 3. **Extracts** relevant details (name, interests, location, family, projects, etc.)
142
+ 4. **Stores** in a structured JSON profile at `/user-profile-memory`
143
+ 5. **Updates** incrementally using JSON Patch operations
144
+
145
+ **What it remembers**:
146
+ - Name and personal details
147
+ - Location (country, city, address)
148
+ - Interests and hobbies
149
+ - Family members and relationships
150
+ - Projects and work
151
+ - Languages spoken
152
+ - Birthday and other personal info
153
+
154
+ **Example**:
155
+ ```bash
156
+ # After Bob introduces himself, the profile is automatically created:
157
+ {
158
+ "name": [{ "name": "Bob" }],
159
+ "interests": [{ "content": "blue color" }]
160
+ }
161
+
162
+ # In the next conversation, the bot can recall this information:
163
+ $ pnpm start --input "Tell me all info about me you known"
164
+ Response: Here's what I currently have stored about you:
165
+ * Name: Bob
166
+ * Interests / favorite color: blue
167
+ ```
168
+
169
+ ### 3. Memory Injection - How the AI Uses Memory
170
+
171
+ When you send a message, the system automatically:
172
+
173
+ **Step 1: Inject User Profile into System Prompt**
174
+ ```text
175
+ You are a friendly chatbot
176
+
177
+ <related-memories>
178
+ User Profile Memory: This contains structured information about the user...
179
+
180
+ - name:
181
+ - name: Bob
182
+ interests:
183
+ - content: blue color
184
+ </related-memories>
185
+ ```
186
+
187
+ **Step 2: Add Recent Conversation History**
188
+ ```json
189
+ [
190
+ {
191
+ "role": "system",
192
+ "content": "You are a friendly chatbot... [profile injected here]"
193
+ },
194
+ {
195
+ "role": "user",
196
+ "content": "I'm Bob and I like blue color"
197
+ },
198
+ {
199
+ "role": "assistant",
200
+ "content": "Nice to meet you, Bob..."
201
+ },
202
+ {
203
+ "role": "user",
204
+ "content": "Tell me all info about me you known"
205
+ }
206
+ ]
207
+ ```
208
+
209
+ **Step 3: AI Generates Personalized Response**
210
+
211
+ The AI can now:
212
+ - Address the user by name
213
+ - Reference their interests
214
+ - Provide personalized recommendations
215
+ - Maintain context across sessions
216
+
217
+ ### Key Design Benefits
218
+
219
+ 1. **Automatic**: No manual profile management needed
220
+ 2. **Intelligent**: AI determines what's important to remember
221
+ 3. **Incremental**: Profile updates gradually over time
222
+ 4. **Persistent**: Memory survives across multiple conversations
223
+ 5. **Structured**: Information is organized in a consistent format
224
+ 6. **Privacy-Aware**: All data stored locally in SQLite
225
+
226
+ ## Related Examples
227
+
228
+ - [AFS LocalFS Example](../afs-local-fs/README.md) - File system access with AI agents
229
+ - [AFS MCP Server Example](../afs-mcp-server/README.md) - Mount MCP servers as AFS modules
230
+
231
+ ## Related Packages
232
+
233
+ - [@aigne/afs](../../afs/README.md) - AFS core package
234
+ - [@aigne/afs-user-profile-memory](../../afs/user-profile-memory/README.md) - User profile memory module
235
+
236
+ ## TypeScript Support
237
+
238
+ This package includes full TypeScript type definitions.
239
+
240
+ ## License
241
+
242
+ [MIT](../../LICENSE.md)
package/index.test.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { expect, test } from "bun:test";
2
+ import { runExampleTest } from "@aigne/test-utils/run-example-test.js";
3
+
4
+ test(
5
+ "should successfully run the chatbot",
6
+ async () => {
7
+ const { status } = await runExampleTest();
8
+ expect(status).toBe(0);
9
+ },
10
+ { timeout: 600000 },
11
+ );
package/index.ts ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env npx -y bun
2
+
3
+ import { AFS } from "@aigne/afs";
4
+ import { AFSHistory } from "@aigne/afs-history";
5
+ import { UserProfileMemory } from "@aigne/afs-user-profile-memory";
6
+ import { loadAIGNEWithCmdOptions, runWithAIGNE } from "@aigne/cli/utils/run-with-aigne.js";
7
+ import { AIAgent } from "@aigne/core";
8
+
9
+ const aigne = await loadAIGNEWithCmdOptions();
10
+
11
+ const sharedStorage = { url: "file:./memory.sqlite3" };
12
+
13
+ const afs = new AFS()
14
+ .mount(new AFSHistory({ storage: sharedStorage }))
15
+ .mount(new UserProfileMemory({ storage: sharedStorage, context: aigne.newContext() }));
16
+
17
+ const agent = AIAgent.from({
18
+ instructions: "You are a friendly chatbot",
19
+ inputKey: "message",
20
+ afs,
21
+ });
22
+
23
+ await runWithAIGNE(agent, {
24
+ aigne,
25
+ chatLoopOptions: {
26
+ welcome: "Hello! I'm a chatbot with memory. Try asking me a question!",
27
+ },
28
+ });
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@aigne/example-afs-memory",
3
+ "version": "0.10.70-beta",
4
+ "description": "A demonstration of using AIGNE Framework with memory-based AFS modules",
5
+ "author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
6
+ "homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/afs-memory",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/AIGNE-io/aigne-framework"
11
+ },
12
+ "bin": "index.ts",
13
+ "files": [
14
+ ".env.local.example",
15
+ "*.ts",
16
+ "README.md"
17
+ ],
18
+ "dependencies": {
19
+ "@aigne/afs": "^1.2.0-beta",
20
+ "@aigne/afs-history": "^1.0.0",
21
+ "@aigne/afs-user-profile-memory": "^1.1.0-beta",
22
+ "@aigne/cli": "^1.55.0-beta",
23
+ "@aigne/core": "^1.68.0-beta"
24
+ },
25
+ "devDependencies": {
26
+ "@types/bun": "^1.2.22",
27
+ "@aigne/test-utils": "^0.5.60-beta"
28
+ },
29
+ "scripts": {
30
+ "start": "bun run index.ts",
31
+ "lint": "tsc --noEmit",
32
+ "test:llm": "bun test index.test.ts"
33
+ }
34
+ }