@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/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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/sqlite bumped to 0.4.0
|
|
17
|
+
* devDependencies
|
|
18
|
+
* @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,167 @@
|
|
|
1
|
+
# @aigne/default-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/default-memory)
|
|
7
|
+
[](https://github.com/AIGNE-io/aigne-framework/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
Default memory system component for [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework), providing SQLite-based memory storage capabilities.
|
|
10
|
+
|
|
11
|
+
## Introduction
|
|
12
|
+
|
|
13
|
+
`@aigne/default-memory` is the default memory system component of [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework), providing SQLite database-based memory storage and retrieval functionality. This component implements standard memory management interfaces and is an ideal choice for building AI applications.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
* **SQLite Storage**: Uses SQLite database for reliable memory persistence
|
|
18
|
+
* **Memory Retrieval**: Supports similarity-based memory retrieval
|
|
19
|
+
* **Automatic Memory Updates**: Supports automatic memory update mechanisms
|
|
20
|
+
* **Memory Statistics**: Provides storage and retrieval statistics for memories
|
|
21
|
+
* **Flexible Configuration**: Supports custom memory retrieval count and related configurations
|
|
22
|
+
* **TypeScript Support**: Complete type definitions providing an excellent development experience
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
### Using npm
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @aigne/default-memory
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Using yarn
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
yarn add @aigne/default-memory
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Using pnpm
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pnpm add @aigne/default-memory
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Basic Usage
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { AIAgent, AIGNE } from "@aigne/core";
|
|
48
|
+
import { DefaultMemory } from "@aigne/default-memory";
|
|
49
|
+
import { OpenAIChatModel } from "@aigne/openai";
|
|
50
|
+
|
|
51
|
+
// Create AI model instance
|
|
52
|
+
const model = new OpenAIChatModel({
|
|
53
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
54
|
+
model: "gpt-4-turbo",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Create default memory system
|
|
58
|
+
const memory = new DefaultMemory({
|
|
59
|
+
storage: {
|
|
60
|
+
url: "file:memory.db",
|
|
61
|
+
},
|
|
62
|
+
retrieveMemoryCount: 10,
|
|
63
|
+
retrieveRecentMemoryCount: 5,
|
|
64
|
+
autoUpdate: true,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Create AI agent with memory
|
|
68
|
+
const agent = AIAgent.from({
|
|
69
|
+
name: "Assistant",
|
|
70
|
+
instructions: "You are a helpful assistant with memory capabilities.",
|
|
71
|
+
memory: memory,
|
|
72
|
+
inputKey: "message",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Use AIGNE execution engine
|
|
76
|
+
const aigne = new AIGNE({ model });
|
|
77
|
+
|
|
78
|
+
// Invoke agent
|
|
79
|
+
const userAgent = await aigne.invoke(agent);
|
|
80
|
+
|
|
81
|
+
// Send message
|
|
82
|
+
const response = await userAgent.invoke({
|
|
83
|
+
message: "My name is John and I work as a software developer",
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
console.log(response.message);
|
|
87
|
+
|
|
88
|
+
// Query memory later
|
|
89
|
+
const response2 = await userAgent.invoke({
|
|
90
|
+
message: "What do you remember about me?",
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log(response2.message);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Advanced Configuration
|
|
97
|
+
|
|
98
|
+
### Custom Storage Configuration
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { DefaultMemory, DefaultMemoryStorage } from "@aigne/default-memory";
|
|
102
|
+
|
|
103
|
+
// Use custom storage configuration
|
|
104
|
+
const memory = new DefaultMemory({
|
|
105
|
+
storage: {
|
|
106
|
+
url: "file:custom-memory.db",
|
|
107
|
+
// Other storage configurations
|
|
108
|
+
},
|
|
109
|
+
retrieveMemoryCount: 20,
|
|
110
|
+
retrieveRecentMemoryCount: 10,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Or use custom storage instance
|
|
114
|
+
const customStorage = new DefaultMemoryStorage({
|
|
115
|
+
url: "file:custom-memory.db",
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const memory2 = new DefaultMemory({
|
|
119
|
+
storage: customStorage,
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Configure Memory Retrieval Options
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const memory = new DefaultMemory({
|
|
127
|
+
storage: {
|
|
128
|
+
url: "file:memory.db",
|
|
129
|
+
},
|
|
130
|
+
// Set total number of memories to retrieve
|
|
131
|
+
retrieveMemoryCount: 15,
|
|
132
|
+
// Set number of recent memories to retrieve
|
|
133
|
+
retrieveRecentMemoryCount: 8,
|
|
134
|
+
// Enable auto update
|
|
135
|
+
autoUpdate: true,
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Using with Multiple Agents
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { DefaultMemory } from "@aigne/default-memory";
|
|
143
|
+
|
|
144
|
+
// Create shared memory system
|
|
145
|
+
const sharedMemory = new DefaultMemory({
|
|
146
|
+
storage: {
|
|
147
|
+
url: "file:shared-memory.db",
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Multiple agents can share the same memory system
|
|
152
|
+
const agent1 = AIAgent.from({
|
|
153
|
+
name: "Agent1",
|
|
154
|
+
instructions: "You are assistant 1",
|
|
155
|
+
memory: sharedMemory,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const agent2 = AIAgent.from({
|
|
159
|
+
name: "Agent2",
|
|
160
|
+
instructions: "You are assistant 2",
|
|
161
|
+
memory: sharedMemory,
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
Elastic-2.0
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultMemoryStorage = void 0;
|
|
4
|
+
const sqlite_1 = require("@aigne/sqlite");
|
|
5
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const yaml_1 = require("yaml");
|
|
8
|
+
const storage_js_1 = require("../storage.js");
|
|
9
|
+
const migrate_js_1 = require("./migrate.js");
|
|
10
|
+
const memory_js_1 = require("./models/memory.js");
|
|
11
|
+
const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
12
|
+
class DefaultMemoryStorage extends storage_js_1.MemoryStorage {
|
|
13
|
+
options;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super();
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
_db;
|
|
19
|
+
async initSqlite() {
|
|
20
|
+
const db = (await (0, sqlite_1.initDatabase)({ url: this.options?.url }));
|
|
21
|
+
await (0, migrate_js_1.migrate)(db);
|
|
22
|
+
return db;
|
|
23
|
+
}
|
|
24
|
+
get db() {
|
|
25
|
+
this._db ??= this.initSqlite();
|
|
26
|
+
return this._db;
|
|
27
|
+
}
|
|
28
|
+
convertMemory(m) {
|
|
29
|
+
return {
|
|
30
|
+
id: m.id,
|
|
31
|
+
sessionId: m.sessionId,
|
|
32
|
+
content: m.content,
|
|
33
|
+
createdAt: m.createdAt.toISOString(),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async search(query, { context }) {
|
|
37
|
+
const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
|
|
38
|
+
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
39
|
+
const db = await this.db;
|
|
40
|
+
const memories = this.options?.enableFTS && query.search
|
|
41
|
+
? await db
|
|
42
|
+
.select()
|
|
43
|
+
.from(memory_js_1.Memories)
|
|
44
|
+
.innerJoin((0, drizzle_orm_1.sql) `Memories_fts`, (0, drizzle_orm_1.sql) `Memories_fts.id = ${memory_js_1.Memories.id}`)
|
|
45
|
+
.where((0, drizzle_orm_1.sql) `Memories_fts MATCH ${drizzle_orm_1.sql.param(this.segment(query.search).join(" OR "))}`)
|
|
46
|
+
.orderBy((0, drizzle_orm_1.sql) `bm25(Memories_fts)`)
|
|
47
|
+
.limit(limit)
|
|
48
|
+
.execute()
|
|
49
|
+
.then((rows) => rows.map((row) => row.Memories))
|
|
50
|
+
: await db
|
|
51
|
+
.select()
|
|
52
|
+
.from(memory_js_1.Memories)
|
|
53
|
+
.where(sessionId ? (0, drizzle_orm_1.eq)(memory_js_1.Memories.sessionId, sessionId) : (0, drizzle_orm_1.isNull)(memory_js_1.Memories.sessionId))
|
|
54
|
+
.orderBy(query.orderBy
|
|
55
|
+
? (query.orderBy[1] === "asc" ? drizzle_orm_1.asc : drizzle_orm_1.desc)(drizzle_orm_1.sql.identifier(query.orderBy[0]))
|
|
56
|
+
: (0, drizzle_orm_1.desc)(memory_js_1.Memories.id))
|
|
57
|
+
.limit(limit)
|
|
58
|
+
.execute();
|
|
59
|
+
return {
|
|
60
|
+
result: memories.map(this.convertMemory),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async create(memory, { context }) {
|
|
64
|
+
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
65
|
+
const db = await this.db;
|
|
66
|
+
const id = (0, uuid_1.v7)();
|
|
67
|
+
const [[result]] = await Promise.all([
|
|
68
|
+
db
|
|
69
|
+
.insert(memory_js_1.Memories)
|
|
70
|
+
.values({
|
|
71
|
+
...memory,
|
|
72
|
+
id,
|
|
73
|
+
content: memory.content,
|
|
74
|
+
sessionId,
|
|
75
|
+
})
|
|
76
|
+
.returning()
|
|
77
|
+
.execute(),
|
|
78
|
+
this.options?.enableFTS &&
|
|
79
|
+
db.run((0, drizzle_orm_1.sql) `\
|
|
80
|
+
insert into Memories_fts (id, content)
|
|
81
|
+
values (${drizzle_orm_1.sql.param(id)}, ${drizzle_orm_1.sql.param(this.segment((0, yaml_1.stringify)(memory.content)).join(" "))})`),
|
|
82
|
+
]);
|
|
83
|
+
if (!result)
|
|
84
|
+
throw new Error("Failed to create memory");
|
|
85
|
+
return { result: this.convertMemory(result) };
|
|
86
|
+
}
|
|
87
|
+
segment(str) {
|
|
88
|
+
return (Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str))
|
|
89
|
+
.map((i) => i.segment)
|
|
90
|
+
// Remove non-alphanumeric characters and trim whitespace
|
|
91
|
+
.map((i) => i.replace(/[^\p{L}\p{N}\s]/gu, "").trim())
|
|
92
|
+
.filter(Boolean));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.DefaultMemoryStorage = DefaultMemoryStorage;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.migrate = migrate;
|
|
7
|
+
const sql_1 = require("drizzle-orm/sql");
|
|
8
|
+
const _001_init_js_1 = __importDefault(require("./migrations/001-init.js"));
|
|
9
|
+
async function migrate(db) {
|
|
10
|
+
const migrations = [_001_init_js_1.default];
|
|
11
|
+
const migrationsTable = "__drizzle_migrations";
|
|
12
|
+
const migrationTableCreate = (0, sql_1.sql) `
|
|
13
|
+
CREATE TABLE IF NOT EXISTS ${sql_1.sql.identifier(migrationsTable)} (
|
|
14
|
+
id SERIAL PRIMARY KEY,
|
|
15
|
+
hash text NOT NULL
|
|
16
|
+
)
|
|
17
|
+
`;
|
|
18
|
+
await db.run(migrationTableCreate).execute();
|
|
19
|
+
const dbMigrations = await db
|
|
20
|
+
.values((0, sql_1.sql) `SELECT id, hash FROM ${sql_1.sql.identifier(migrationsTable)} ORDER BY id DESC LIMIT 1`)
|
|
21
|
+
.execute();
|
|
22
|
+
const lastDbMigration = dbMigrations[0];
|
|
23
|
+
const queriesToRun = [];
|
|
24
|
+
for (const migration of migrations) {
|
|
25
|
+
if (!lastDbMigration || lastDbMigration[1] < migration.hash) {
|
|
26
|
+
queriesToRun.push(...migration.sql, `INSERT INTO \`${migrationsTable}\` ("hash") VALUES('${migration.hash}')`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const query of queriesToRun) {
|
|
30
|
+
await db.run(query).execute();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
hash: "001-init",
|
|
5
|
+
sql: [
|
|
6
|
+
`\
|
|
7
|
+
CREATE TABLE "Memories" (
|
|
8
|
+
"id" TEXT NOT NULL PRIMARY KEY,
|
|
9
|
+
"createdAt" DATETIME NOT NULL,
|
|
10
|
+
"updatedAt" DATETIME NOT NULL,
|
|
11
|
+
"sessionId" TEXT,
|
|
12
|
+
"content" JSON NOT NULL
|
|
13
|
+
)
|
|
14
|
+
`,
|
|
15
|
+
`\
|
|
16
|
+
CREATE VIRTUAL TABLE "Memories_fts" USING fts5(
|
|
17
|
+
id UNINDEXED,
|
|
18
|
+
content
|
|
19
|
+
)
|
|
20
|
+
`,
|
|
21
|
+
],
|
|
22
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
}>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Memories = void 0;
|
|
4
|
+
const type_js_1 = require("@aigne/sqlite/type.js");
|
|
5
|
+
const sqlite_core_1 = require("drizzle-orm/sqlite-core");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
exports.Memories = (0, sqlite_core_1.sqliteTable)("Memories", {
|
|
8
|
+
id: (0, sqlite_core_1.text)("id")
|
|
9
|
+
.notNull()
|
|
10
|
+
.primaryKey()
|
|
11
|
+
.$defaultFn(() => (0, uuid_1.v7)()),
|
|
12
|
+
createdAt: (0, type_js_1.datetime)("createdAt")
|
|
13
|
+
.notNull()
|
|
14
|
+
.$defaultFn(() => new Date()),
|
|
15
|
+
updatedAt: (0, type_js_1.datetime)("updatedAt")
|
|
16
|
+
.notNull()
|
|
17
|
+
.$defaultFn(() => new Date())
|
|
18
|
+
.$onUpdateFn(() => new Date()),
|
|
19
|
+
sessionId: (0, sqlite_core_1.text)("sessionId"),
|
|
20
|
+
content: (0, type_js_1.json)("content").notNull(),
|
|
21
|
+
});
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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("./default-memory-storage/index.js"), exports);
|
|
18
|
+
__exportStar(require("./memory.js"), exports);
|
|
19
|
+
__exportStar(require("./storage.js"), exports);
|
|
@@ -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
|
+
}
|