@t0ken.ai/memoryx-sdk 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/README.md +135 -0
- package/dist/client.d.ts +31 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +289 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +123 -0
- package/dist/index.js.map +1 -0
- package/dist/queue.d.ts +33 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +309 -0
- package/dist/queue.js.map +1 -0
- package/dist/storage.d.ts +62 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +317 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# @t0ken.ai/memoryx-sdk
|
|
2
|
+
|
|
3
|
+
MemoryX Node.js SDK - 让 AI Agents 轻松拥有持久记忆
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @t0ken.ai/memoryx-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 快速开始
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { MemoryXSDK, PRESET } from '@t0ken/memoryx-sdk';
|
|
15
|
+
|
|
16
|
+
// 使用预设模式
|
|
17
|
+
const sdk = new MemoryXSDK({ preset: 'batch' });
|
|
18
|
+
|
|
19
|
+
// 添加记忆
|
|
20
|
+
await sdk.addMemory("用户喜欢用Python做数据分析");
|
|
21
|
+
|
|
22
|
+
// 搜索记忆
|
|
23
|
+
const results = await sdk.search("数据分析");
|
|
24
|
+
console.log(results.data);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 发送策略
|
|
28
|
+
|
|
29
|
+
SDK 支持灵活的发送策略配置:
|
|
30
|
+
|
|
31
|
+
### 预设模式
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
// 实时模式 - 立即发送
|
|
35
|
+
const sdk = new MemoryXSDK({ preset: 'realtime' });
|
|
36
|
+
|
|
37
|
+
// 批量模式 - 50条或5秒
|
|
38
|
+
const sdk = new MemoryXSDK({ preset: 'batch' });
|
|
39
|
+
|
|
40
|
+
// 会话模式 - 2轮或30分钟
|
|
41
|
+
const sdk = new MemoryXSDK({ preset: 'conversation' });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 自定义策略
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const sdk = new MemoryXSDK({
|
|
48
|
+
strategy: {
|
|
49
|
+
rounds: 3, // 3轮对话后发送
|
|
50
|
+
batchSize: 100, // 或100条消息
|
|
51
|
+
intervalMs: 60000 // 或1分钟
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 完全自定义
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const sdk = new MemoryXSDK({
|
|
60
|
+
strategy: {
|
|
61
|
+
customTrigger: (stats) => {
|
|
62
|
+
// 自定义逻辑
|
|
63
|
+
return stats.rounds >= 2 || stats.totalTokens >= 4000;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API
|
|
70
|
+
|
|
71
|
+
### 记忆操作
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// 添加记忆
|
|
75
|
+
await sdk.addMemory(content, metadata?);
|
|
76
|
+
|
|
77
|
+
// 搜索记忆
|
|
78
|
+
const results = await sdk.search(query, limit?);
|
|
79
|
+
|
|
80
|
+
// 列出记忆
|
|
81
|
+
const memories = await sdk.list(limit?, offset?);
|
|
82
|
+
|
|
83
|
+
// 删除记忆
|
|
84
|
+
await sdk.delete(memoryId);
|
|
85
|
+
|
|
86
|
+
// 获取配额
|
|
87
|
+
const quota = await sdk.getQuota();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 会话操作
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
// 添加对话消息
|
|
94
|
+
await sdk.addMessage('user', '你好');
|
|
95
|
+
await sdk.addMessage('assistant', '你好!有什么可以帮助你的?');
|
|
96
|
+
|
|
97
|
+
// 开始新会话
|
|
98
|
+
sdk.startNewConversation();
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 队列操作
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// 手动刷新队列
|
|
105
|
+
await sdk.flush();
|
|
106
|
+
|
|
107
|
+
// 获取队列状态
|
|
108
|
+
const stats = await sdk.getQueueStats();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## 策略参数
|
|
112
|
+
|
|
113
|
+
| 参数 | 类型 | 说明 |
|
|
114
|
+
|------|------|------|
|
|
115
|
+
| `rounds` | number | N轮对话后发送 |
|
|
116
|
+
| `batchSize` | number | N条消息后发送 |
|
|
117
|
+
| `intervalMs` | number | N毫秒后发送 |
|
|
118
|
+
| `maxTokens` | number | N tokens后发送 |
|
|
119
|
+
| `customTrigger` | function | 自定义触发函数 |
|
|
120
|
+
|
|
121
|
+
## 队列状态
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
interface QueueStats {
|
|
125
|
+
messageCount: number; // 当前消息数
|
|
126
|
+
rounds: number; // 当前轮数
|
|
127
|
+
totalTokens: number; // 总tokens
|
|
128
|
+
oldestMessageAge: number; // 最早消息年龄
|
|
129
|
+
conversationId: string; // 会话ID
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { StoredConfig, AccountInfo, AddResponse, SearchResponse, SearchResult, Memory, Message } from "./types";
|
|
2
|
+
export declare class APIClient {
|
|
3
|
+
private apiKey;
|
|
4
|
+
private apiBaseUrl;
|
|
5
|
+
private projectId;
|
|
6
|
+
private userId;
|
|
7
|
+
constructor(config?: Partial<StoredConfig>);
|
|
8
|
+
getApiKey(): string | null;
|
|
9
|
+
getProjectId(): string;
|
|
10
|
+
getUserId(): string | null;
|
|
11
|
+
getApiBaseUrl(): string;
|
|
12
|
+
getConfig(): StoredConfig;
|
|
13
|
+
setApiKey(key: string): void;
|
|
14
|
+
setProjectId(id: string): void;
|
|
15
|
+
setUserId(id: string): void;
|
|
16
|
+
getMachineFingerprint(): string;
|
|
17
|
+
autoRegister(agentType?: string, agentName?: string): Promise<AccountInfo>;
|
|
18
|
+
sendMemories(memories: Memory[]): Promise<AddResponse>;
|
|
19
|
+
sendConversation(conversationId: string, messages: Message[]): Promise<AddResponse>;
|
|
20
|
+
search(query: string, limit?: number): Promise<SearchResponse>;
|
|
21
|
+
list(limit?: number, offset?: number): Promise<{
|
|
22
|
+
success: boolean;
|
|
23
|
+
data: SearchResult[];
|
|
24
|
+
total: number;
|
|
25
|
+
}>;
|
|
26
|
+
delete(memoryId: string): Promise<{
|
|
27
|
+
success: boolean;
|
|
28
|
+
}>;
|
|
29
|
+
getQuota(): Promise<Record<string, any>>;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAIhH,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,SAAS,CAAqB;IACtC,OAAO,CAAC,MAAM,CAAuB;gBAEzB,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|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.APIClient = void 0;
|
|
37
|
+
const crypto = __importStar(require("crypto"));
|
|
38
|
+
const os = __importStar(require("os"));
|
|
39
|
+
const DEFAULT_API_BASE = "https://t0ken.ai/api";
|
|
40
|
+
class APIClient {
|
|
41
|
+
constructor(config) {
|
|
42
|
+
this.apiKey = null;
|
|
43
|
+
this.projectId = "default";
|
|
44
|
+
this.userId = null;
|
|
45
|
+
this.apiKey = config?.apiKey || null;
|
|
46
|
+
this.apiBaseUrl = config?.apiBaseUrl || DEFAULT_API_BASE;
|
|
47
|
+
this.projectId = config?.projectId || "default";
|
|
48
|
+
this.userId = config?.userId || null;
|
|
49
|
+
}
|
|
50
|
+
getApiKey() {
|
|
51
|
+
return this.apiKey;
|
|
52
|
+
}
|
|
53
|
+
getProjectId() {
|
|
54
|
+
return this.projectId;
|
|
55
|
+
}
|
|
56
|
+
getUserId() {
|
|
57
|
+
return this.userId;
|
|
58
|
+
}
|
|
59
|
+
getApiBaseUrl() {
|
|
60
|
+
return this.apiBaseUrl;
|
|
61
|
+
}
|
|
62
|
+
getConfig() {
|
|
63
|
+
return {
|
|
64
|
+
apiKey: this.apiKey,
|
|
65
|
+
projectId: this.projectId,
|
|
66
|
+
userId: this.userId,
|
|
67
|
+
apiBaseUrl: this.apiBaseUrl
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
setApiKey(key) {
|
|
71
|
+
this.apiKey = key;
|
|
72
|
+
}
|
|
73
|
+
setProjectId(id) {
|
|
74
|
+
this.projectId = id;
|
|
75
|
+
}
|
|
76
|
+
setUserId(id) {
|
|
77
|
+
this.userId = id;
|
|
78
|
+
}
|
|
79
|
+
getMachineFingerprint() {
|
|
80
|
+
const components = [
|
|
81
|
+
os.hostname(),
|
|
82
|
+
os.platform(),
|
|
83
|
+
os.arch(),
|
|
84
|
+
os.cpus()[0]?.model || "unknown",
|
|
85
|
+
os.totalmem()
|
|
86
|
+
];
|
|
87
|
+
const raw = components.join("|");
|
|
88
|
+
return crypto.createHash("sha256").update(raw).digest("hex").slice(0, 32);
|
|
89
|
+
}
|
|
90
|
+
async autoRegister(agentType = "nodejs_sdk", agentName) {
|
|
91
|
+
const fingerprint = this.getMachineFingerprint();
|
|
92
|
+
const response = await fetch(`${this.apiBaseUrl}/agents/auto-register`, {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: { "Content-Type": "application/json" },
|
|
95
|
+
body: JSON.stringify({
|
|
96
|
+
machine_fingerprint: fingerprint,
|
|
97
|
+
agent_type: agentType,
|
|
98
|
+
agent_name: agentName || os.hostname(),
|
|
99
|
+
platform: os.platform(),
|
|
100
|
+
platform_version: os.release()
|
|
101
|
+
})
|
|
102
|
+
});
|
|
103
|
+
if (!response.ok) {
|
|
104
|
+
const errorData = await response.json().catch(() => ({}));
|
|
105
|
+
throw new Error(`Auto-register failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
106
|
+
}
|
|
107
|
+
const data = await response.json();
|
|
108
|
+
this.apiKey = data.api_key;
|
|
109
|
+
this.projectId = String(data.project_id);
|
|
110
|
+
this.userId = data.agent_id;
|
|
111
|
+
return {
|
|
112
|
+
agent_id: data.agent_id,
|
|
113
|
+
api_key: data.api_key,
|
|
114
|
+
project_id: String(data.project_id)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
async sendMemories(memories) {
|
|
118
|
+
if (!this.apiKey) {
|
|
119
|
+
throw new Error("Not authenticated. Call autoRegister() first.");
|
|
120
|
+
}
|
|
121
|
+
if (memories.length === 0) {
|
|
122
|
+
return { success: true };
|
|
123
|
+
}
|
|
124
|
+
const endpoint = memories.length === 1
|
|
125
|
+
? `${this.apiBaseUrl}/v1/memories`
|
|
126
|
+
: `${this.apiBaseUrl}/v1/memories/batch`;
|
|
127
|
+
const body = memories.length === 1
|
|
128
|
+
? { content: memories[0].content, project_id: this.projectId, metadata: memories[0].metadata || {} }
|
|
129
|
+
: { memories, project_id: this.projectId };
|
|
130
|
+
const response = await fetch(endpoint, {
|
|
131
|
+
method: "POST",
|
|
132
|
+
headers: {
|
|
133
|
+
"Content-Type": "application/json",
|
|
134
|
+
"X-API-Key": this.apiKey
|
|
135
|
+
},
|
|
136
|
+
body: JSON.stringify(body)
|
|
137
|
+
});
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
const errorData = await response.json().catch(() => ({}));
|
|
140
|
+
throw new Error(`Send memories failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
141
|
+
}
|
|
142
|
+
const data = await response.json();
|
|
143
|
+
return {
|
|
144
|
+
success: true,
|
|
145
|
+
task_id: data.task_id,
|
|
146
|
+
status: data.status
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async sendConversation(conversationId, messages) {
|
|
150
|
+
if (!this.apiKey) {
|
|
151
|
+
throw new Error("Not authenticated. Call autoRegister() first.");
|
|
152
|
+
}
|
|
153
|
+
if (messages.length === 0) {
|
|
154
|
+
return { success: true };
|
|
155
|
+
}
|
|
156
|
+
const response = await fetch(`${this.apiBaseUrl}/conversations/flush`, {
|
|
157
|
+
method: "POST",
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "application/json",
|
|
160
|
+
"X-API-Key": this.apiKey
|
|
161
|
+
},
|
|
162
|
+
body: JSON.stringify({
|
|
163
|
+
conversation_id: conversationId,
|
|
164
|
+
messages: messages.map(m => ({
|
|
165
|
+
role: m.role,
|
|
166
|
+
content: m.content,
|
|
167
|
+
timestamp: m.timestamp || Date.now(),
|
|
168
|
+
tokens: m.tokens || 0
|
|
169
|
+
}))
|
|
170
|
+
})
|
|
171
|
+
});
|
|
172
|
+
if (!response.ok) {
|
|
173
|
+
const errorData = await response.json().catch(() => ({}));
|
|
174
|
+
throw new Error(`Send conversation failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
175
|
+
}
|
|
176
|
+
const data = await response.json();
|
|
177
|
+
return {
|
|
178
|
+
success: true,
|
|
179
|
+
task_id: data.task_id,
|
|
180
|
+
status: data.status
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
async search(query, limit = 10) {
|
|
184
|
+
if (!this.apiKey) {
|
|
185
|
+
throw new Error("Not authenticated. Call autoRegister() first.");
|
|
186
|
+
}
|
|
187
|
+
const response = await fetch(`${this.apiBaseUrl}/v1/memories/search`, {
|
|
188
|
+
method: "POST",
|
|
189
|
+
headers: {
|
|
190
|
+
"Content-Type": "application/json",
|
|
191
|
+
"X-API-Key": this.apiKey
|
|
192
|
+
},
|
|
193
|
+
body: JSON.stringify({
|
|
194
|
+
query,
|
|
195
|
+
project_id: this.projectId,
|
|
196
|
+
limit
|
|
197
|
+
})
|
|
198
|
+
});
|
|
199
|
+
if (!response.ok) {
|
|
200
|
+
const errorData = await response.json().catch(() => ({}));
|
|
201
|
+
throw new Error(`Search failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
202
|
+
}
|
|
203
|
+
const data = await response.json();
|
|
204
|
+
return {
|
|
205
|
+
success: true,
|
|
206
|
+
data: (data.data || []).map((m) => ({
|
|
207
|
+
id: m.id,
|
|
208
|
+
content: m.memory || m.content,
|
|
209
|
+
category: m.category || "other",
|
|
210
|
+
score: m.score || 0.5
|
|
211
|
+
})),
|
|
212
|
+
related_memories: (data.related_memories || []).map((m) => ({
|
|
213
|
+
id: m.id,
|
|
214
|
+
content: m.memory || m.content,
|
|
215
|
+
category: m.category || "other",
|
|
216
|
+
score: m.score || 0
|
|
217
|
+
})),
|
|
218
|
+
remaining_quota: data.remaining_quota
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
async list(limit = 50, offset = 0) {
|
|
222
|
+
if (!this.apiKey) {
|
|
223
|
+
throw new Error("Not authenticated. Call autoRegister() first.");
|
|
224
|
+
}
|
|
225
|
+
const params = new URLSearchParams({
|
|
226
|
+
limit: String(limit),
|
|
227
|
+
offset: String(offset),
|
|
228
|
+
project_id: this.projectId
|
|
229
|
+
});
|
|
230
|
+
const response = await fetch(`${this.apiBaseUrl}/v1/memories/list?${params}`, {
|
|
231
|
+
method: "GET",
|
|
232
|
+
headers: {
|
|
233
|
+
"Content-Type": "application/json",
|
|
234
|
+
"X-API-Key": this.apiKey
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
if (!response.ok) {
|
|
238
|
+
const errorData = await response.json().catch(() => ({}));
|
|
239
|
+
throw new Error(`List failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
240
|
+
}
|
|
241
|
+
const data = await response.json();
|
|
242
|
+
return {
|
|
243
|
+
success: true,
|
|
244
|
+
data: (data.data || []).map((m) => ({
|
|
245
|
+
id: m.id,
|
|
246
|
+
content: m.memory || m.content,
|
|
247
|
+
category: m.category || "other",
|
|
248
|
+
score: 0
|
|
249
|
+
})),
|
|
250
|
+
total: data.total || 0
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
async delete(memoryId) {
|
|
254
|
+
if (!this.apiKey) {
|
|
255
|
+
throw new Error("Not authenticated. Call autoRegister() first.");
|
|
256
|
+
}
|
|
257
|
+
const response = await fetch(`${this.apiBaseUrl}/v1/memories/${memoryId}`, {
|
|
258
|
+
method: "DELETE",
|
|
259
|
+
headers: {
|
|
260
|
+
"Content-Type": "application/json",
|
|
261
|
+
"X-API-Key": this.apiKey
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
if (!response.ok) {
|
|
265
|
+
const errorData = await response.json().catch(() => ({}));
|
|
266
|
+
throw new Error(`Delete failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
267
|
+
}
|
|
268
|
+
return { success: true };
|
|
269
|
+
}
|
|
270
|
+
async getQuota() {
|
|
271
|
+
if (!this.apiKey) {
|
|
272
|
+
throw new Error("Not authenticated. Call autoRegister() first.");
|
|
273
|
+
}
|
|
274
|
+
const response = await fetch(`${this.apiBaseUrl}/v1/quota`, {
|
|
275
|
+
method: "GET",
|
|
276
|
+
headers: {
|
|
277
|
+
"Content-Type": "application/json",
|
|
278
|
+
"X-API-Key": this.apiKey
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
if (!response.ok) {
|
|
282
|
+
const errorData = await response.json().catch(() => ({}));
|
|
283
|
+
throw new Error(`Get quota failed: ${response.status} ${JSON.stringify(errorData)}`);
|
|
284
|
+
}
|
|
285
|
+
return response.json();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
exports.APIClient = APIClient;
|
|
289
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAiC;AACjC,uCAAyB;AAGzB,MAAM,gBAAgB,GAAG,sBAAsB,CAAC;AAEhD,MAAa,SAAS;IAMpB,YAAY,MAA8B;QALlC,WAAM,GAAkB,IAAI,CAAC;QAE7B,cAAS,GAAW,SAAS,CAAC;QAC9B,WAAM,GAAkB,IAAI,CAAC;QAGnC,IAAI,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,IAAI,IAAI,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,MAAM,EAAE,UAAU,IAAI,gBAAgB,CAAC;QACzD,IAAI,CAAC,SAAS,GAAG,MAAM,EAAE,SAAS,IAAI,SAAS,CAAC;QAChD,IAAI,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,IAAI,IAAI,CAAC;IACvC,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,YAAY;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,SAAS;QACP,OAAO;YACL,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,UAAU,EAAE,IAAI,CAAC,UAAU;SAC5B,CAAC;IACJ,CAAC;IAED,SAAS,CAAC,GAAW;QACnB,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;IACpB,CAAC;IAED,YAAY,CAAC,EAAU;QACrB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACtB,CA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kBAAkB;gBAClC,WAAW,EAAE,IAAI,CAAC,MAAM;aACzB;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,SAAS,GAAQ,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAC/D,MAAM,IAAI,KAAK,CAAC,qBAAqB,QAAQ,CAAC,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QACvF,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,EAAkC,CAAC;IACzD,CAAC;CACF;AAtSD,8BAsSC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MemoryXSDKOptions, FlushStrategy, QueueStats, Memory, Message, SearchResponse, AccountInfo, PresetMode } from "./types";
|
|
2
|
+
import { countTokens, setDebug as setQueueDebug } from "./queue";
|
|
3
|
+
export { countTokens, setQueueDebug as setDebug };
|
|
4
|
+
export type { FlushStrategy, QueueStats, Memory, Message, SearchResponse, AccountInfo, MemoryXSDKOptions, PresetMode };
|
|
5
|
+
export declare class MemoryXSDK {
|
|
6
|
+
private client;
|
|
7
|
+
private queue;
|
|
8
|
+
private initialized;
|
|
9
|
+
private options;
|
|
10
|
+
constructor(options?: MemoryXSDKOptions);
|
|
11
|
+
init(): Promise<void>;
|
|
12
|
+
private saveConfig;
|
|
13
|
+
autoRegister(agentType?: string, agentName?: string): Promise<AccountInfo>;
|
|
14
|
+
addMemory(content: string, metadata?: Record<string, any>): Promise<number>;
|
|
15
|
+
search(query: string, limit?: number): Promise<SearchResponse>;
|
|
16
|
+
list(limit?: number, offset?: number): Promise<{
|
|
17
|
+
success: boolean;
|
|
18
|
+
data: any[];
|
|
19
|
+
total: number;
|
|
20
|
+
}>;
|
|
21
|
+
delete(memoryId: string): Promise<{
|
|
22
|
+
success: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
getQuota(): Promise<Record<string, any>>;
|
|
25
|
+
addMessage(role: 'user' | 'assistant', content: string): Promise<number>;
|
|
26
|
+
startNewConversation(): void;
|
|
27
|
+
flush(): Promise<void>;
|
|
28
|
+
getQueueStats(): Promise<QueueStats>;
|
|
29
|
+
getMemoryQueueLength(): Promise<number>;
|
|
30
|
+
getConversationQueueLength(): Promise<number>;
|
|
31
|
+
setStrategy(strategy: FlushStrategy): void;
|
|
32
|
+
destroy(): void;
|
|
33
|
+
getApiKey(): string | null;
|
|
34
|
+
getProjectId(): string;
|
|
35
|
+
getUserId(): string | null;
|
|
36
|
+
}
|
|
37
|
+
export declare function connectMemory(options?: MemoryXSDKOptions): MemoryXSDK;
|
|
38
|
+
export declare const PRESET: Record<PresetMode, FlushStrategy>;
|
|
39
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAW,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1I,OAAO,EAAgB,WAAW,EAAE,QAAQ,IAAI,aAAa,EAAE,MAAM,SAAS,CAAC;AAG/E,OAAO,EAAE,WAAW,EAAE,aAAa,IAAI,QAAQ,EAAE,CAAC;AAClD,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,iBAAiB,EAAE,UAAU,EAAE,CAAC;AAEvH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,WAAW,CAAkB;IACrC,OAAO,CAAC,OAAO,CAAoB;gBAEvB,OAAO,GAAE,iBAAsB;IAoBrC,IAAI,IAAI,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AO,GAAE,iBAAsB,GAAG,UAAU,CAEzE;AAED,eAAO,MAAM,MAAM,mCAAU,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PRESET = exports.MemoryXSDK = exports.setDebug = exports.countTokens = void 0;
|
|
4
|
+
exports.connectMemory = connectMemory;
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
const client_1 = require("./client");
|
|
7
|
+
const queue_1 = require("./queue");
|
|
8
|
+
Object.defineProperty(exports, "countTokens", { enumerable: true, get: function () { return queue_1.countTokens; } });
|
|
9
|
+
Object.defineProperty(exports, "setDebug", { enumerable: true, get: function () { return queue_1.setDebug; } });
|
|
10
|
+
const storage_1 = require("./storage");
|
|
11
|
+
class MemoryXSDK {
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
this.initialized = false;
|
|
14
|
+
this.options = options;
|
|
15
|
+
if (options.storageDir) {
|
|
16
|
+
(0, storage_1.setStorageDir)(options.storageDir);
|
|
17
|
+
}
|
|
18
|
+
this.client = new client_1.APIClient({
|
|
19
|
+
apiKey: options.apiKey || null,
|
|
20
|
+
apiBaseUrl: options.apiUrl,
|
|
21
|
+
projectId: options.projectId
|
|
22
|
+
});
|
|
23
|
+
const strategy = options.preset
|
|
24
|
+
? types_1.PRESETS[options.preset]
|
|
25
|
+
: options.strategy || {};
|
|
26
|
+
this.queue = new queue_1.QueueManager(this.client, strategy);
|
|
27
|
+
}
|
|
28
|
+
async init() {
|
|
29
|
+
if (this.initialized)
|
|
30
|
+
return;
|
|
31
|
+
const stored = await storage_1.SQLiteStorage.loadConfig();
|
|
32
|
+
if (stored) {
|
|
33
|
+
if (!this.options.apiKey && stored.apiKey) {
|
|
34
|
+
this.client.setApiKey(stored.apiKey);
|
|
35
|
+
}
|
|
36
|
+
if (!this.options.projectId && stored.projectId) {
|
|
37
|
+
this.client.setProjectId(stored.projectId);
|
|
38
|
+
}
|
|
39
|
+
if (stored.userId) {
|
|
40
|
+
this.client.setUserId(stored.userId);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!this.client.getApiKey() && this.options.autoRegister !== false) {
|
|
44
|
+
await this.autoRegister();
|
|
45
|
+
}
|
|
46
|
+
this.queue.startTimers();
|
|
47
|
+
this.initialized = true;
|
|
48
|
+
}
|
|
49
|
+
async saveConfig() {
|
|
50
|
+
await storage_1.SQLiteStorage.saveConfig(this.client.getConfig());
|
|
51
|
+
}
|
|
52
|
+
async autoRegister(agentType, agentName) {
|
|
53
|
+
const info = await this.client.autoRegister(agentType || this.options.agentType || "nodejs_sdk", agentName);
|
|
54
|
+
await this.saveConfig();
|
|
55
|
+
return info;
|
|
56
|
+
}
|
|
57
|
+
// Memory operations
|
|
58
|
+
async addMemory(content, metadata = {}) {
|
|
59
|
+
await this.init();
|
|
60
|
+
return this.queue.addMemory(content, metadata);
|
|
61
|
+
}
|
|
62
|
+
async search(query, limit = 10) {
|
|
63
|
+
await this.init();
|
|
64
|
+
return this.client.search(query, limit);
|
|
65
|
+
}
|
|
66
|
+
async list(limit = 50, offset = 0) {
|
|
67
|
+
await this.init();
|
|
68
|
+
return this.client.list(limit, offset);
|
|
69
|
+
}
|
|
70
|
+
async delete(memoryId) {
|
|
71
|
+
await this.init();
|
|
72
|
+
return this.client.delete(memoryId);
|
|
73
|
+
}
|
|
74
|
+
async getQuota() {
|
|
75
|
+
await this.init();
|
|
76
|
+
return this.client.getQuota();
|
|
77
|
+
}
|
|
78
|
+
// Conversation operations
|
|
79
|
+
async addMessage(role, content) {
|
|
80
|
+
await this.init();
|
|
81
|
+
return this.queue.addMessage(role, content);
|
|
82
|
+
}
|
|
83
|
+
startNewConversation() {
|
|
84
|
+
this.queue.startNewConversation();
|
|
85
|
+
}
|
|
86
|
+
// Queue operations
|
|
87
|
+
async flush() {
|
|
88
|
+
await this.init();
|
|
89
|
+
return this.queue.flush();
|
|
90
|
+
}
|
|
91
|
+
async getQueueStats() {
|
|
92
|
+
return this.queue.getQueueStats();
|
|
93
|
+
}
|
|
94
|
+
async getMemoryQueueLength() {
|
|
95
|
+
return this.queue.getMemoryQueueLength();
|
|
96
|
+
}
|
|
97
|
+
async getConversationQueueLength() {
|
|
98
|
+
return this.queue.getConversationQueueLength();
|
|
99
|
+
}
|
|
100
|
+
setStrategy(strategy) {
|
|
101
|
+
this.queue.setStrategy(strategy);
|
|
102
|
+
}
|
|
103
|
+
// Cleanup
|
|
104
|
+
destroy() {
|
|
105
|
+
this.queue.stopTimers();
|
|
106
|
+
}
|
|
107
|
+
// Getters
|
|
108
|
+
getApiKey() {
|
|
109
|
+
return this.client.getApiKey();
|
|
110
|
+
}
|
|
111
|
+
getProjectId() {
|
|
112
|
+
return this.client.getProjectId();
|
|
113
|
+
}
|
|
114
|
+
getUserId() {
|
|
115
|
+
return this.client.getUserId();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.MemoryXSDK = MemoryXSDK;
|
|
119
|
+
function connectMemory(options = {}) {
|
|
120
|
+
return new MemoryXSDK(options);
|
|
121
|
+
}
|
|
122
|
+
exports.PRESET = types_1.PRESETS;
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAoJA,sCAEC;AAtJD,mCAA0I;AAC1I,qCAAqC;AACrC,mCAA+E;AAGtE,4FAHc,mBAAW,OAGd;AAAmB,yFAHS,gBAAa,OAGd;AAF/C,uCAAyD;AAKzD,MAAa,UAAU;IAMrB,YAAY,UAA6B,EAAE;QAHnC,gBAAW,GAAY,KAAK,CAAC;QAInC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,IAAA,uBAAa,EAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACpC,CAAC;QA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