memoryos-api 0.1.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/BUILD_AND_PUBLISH.md +115 -0
- package/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +122 -0
- package/package.json +30 -0
- package/src/index.ts +171 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Building and Publishing the MemoryOS Node.js SDK
|
|
2
|
+
|
|
3
|
+
## Prerequisites
|
|
4
|
+
|
|
5
|
+
1. Node.js 16+ and npm
|
|
6
|
+
2. npm account (https://www.npmjs.com)
|
|
7
|
+
3. Logged in to npm: `npm login`
|
|
8
|
+
|
|
9
|
+
## Installation of Build Tools
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install -g npm
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Building the Package
|
|
16
|
+
|
|
17
|
+
From the `sdk-node` directory:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm run build
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
This compiles TypeScript to JavaScript in the `dist/` directory and generates type definitions.
|
|
24
|
+
|
|
25
|
+
## Publishing to npm
|
|
26
|
+
|
|
27
|
+
### Option 1: Using npm CLI (Recommended)
|
|
28
|
+
|
|
29
|
+
First, ensure you're logged in:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm login
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then publish:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm publish
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Option 2: Using npm Token
|
|
42
|
+
|
|
43
|
+
Create or use an existing npm access token from https://www.npmjs.com/settings/tokens
|
|
44
|
+
|
|
45
|
+
Set the token:
|
|
46
|
+
```bash
|
|
47
|
+
npm config set //registry.npmjs.org/:_authToken=your-npm-token
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Then publish:
|
|
51
|
+
```bash
|
|
52
|
+
npm publish
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Verifying the Upload
|
|
56
|
+
|
|
57
|
+
After publishing, verify your package is available:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install memoryos
|
|
61
|
+
node -e "const MemoryOS = require('memoryos'); console.log('Success!');"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Or with TypeScript:
|
|
65
|
+
```bash
|
|
66
|
+
npm install memoryos
|
|
67
|
+
npx ts-node -e "import MemoryOS from 'memoryos'; console.log('Success!');"
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Version Updates
|
|
71
|
+
|
|
72
|
+
To release a new version:
|
|
73
|
+
|
|
74
|
+
1. Update version in `package.json`
|
|
75
|
+
2. Run `npm run build`
|
|
76
|
+
3. Run `npm publish`
|
|
77
|
+
|
|
78
|
+
## Testing Before Publishing
|
|
79
|
+
|
|
80
|
+
Test the package locally:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Install in development mode
|
|
84
|
+
npm install
|
|
85
|
+
|
|
86
|
+
# Run tests
|
|
87
|
+
npm test
|
|
88
|
+
|
|
89
|
+
# Build
|
|
90
|
+
npm run build
|
|
91
|
+
|
|
92
|
+
# Test the built package
|
|
93
|
+
npm pack
|
|
94
|
+
npm install ./memoryos-0.1.0.tgz
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Scoped Packages (Optional)
|
|
98
|
+
|
|
99
|
+
If you want to publish under a scope (e.g., `@memoryos/sdk`):
|
|
100
|
+
|
|
101
|
+
1. Update `name` in `package.json`: `"@memoryos/sdk"`
|
|
102
|
+
2. Run `npm publish --access public`
|
|
103
|
+
|
|
104
|
+
## Troubleshooting
|
|
105
|
+
|
|
106
|
+
- **"You must be logged in"**: Run `npm login` first
|
|
107
|
+
- **"Package name already exists"**: Use a different name or scope
|
|
108
|
+
- **"Version already exists"**: Update the version in package.json
|
|
109
|
+
- **"Invalid package"**: Check that all required fields are in package.json
|
|
110
|
+
|
|
111
|
+
## Resources
|
|
112
|
+
|
|
113
|
+
- npm Documentation: https://docs.npmjs.com/
|
|
114
|
+
- npm Publishing Guide: https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry
|
|
115
|
+
- TypeScript Publishing: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MemoryOS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# MemoryOS Node.js SDK
|
|
2
|
+
|
|
3
|
+
Persistent memory for AI agents. Store, search, and retrieve memories with semantic understanding.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install memoryos
|
|
9
|
+
# or
|
|
10
|
+
yarn add memoryos
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import MemoryOS from 'memoryos';
|
|
17
|
+
|
|
18
|
+
// Initialize client
|
|
19
|
+
const client = new MemoryOS('your-api-key');
|
|
20
|
+
|
|
21
|
+
// Store a memory
|
|
22
|
+
const memoryId = await client.store(
|
|
23
|
+
'User prefers concise responses',
|
|
24
|
+
{ user_id: '123' }
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// Search memories (semantic search)
|
|
28
|
+
const results = await client.search('user preferences', 5);
|
|
29
|
+
results.forEach(memory => {
|
|
30
|
+
console.log(`Similarity: ${memory.similarity}`);
|
|
31
|
+
console.log(`Content: ${memory.content}`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// List all memories
|
|
35
|
+
const allMemories = await client.list({ limit: 20 });
|
|
36
|
+
|
|
37
|
+
// Get a specific memory
|
|
38
|
+
const memory = await client.get(memoryId);
|
|
39
|
+
|
|
40
|
+
// Delete a memory
|
|
41
|
+
await client.delete(memoryId);
|
|
42
|
+
|
|
43
|
+
// Get statistics
|
|
44
|
+
const stats = await client.stats();
|
|
45
|
+
console.log(`Total memories: ${stats.totalMemories}`);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Integration with LangChain
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import MemoryOS from 'memoryos';
|
|
52
|
+
|
|
53
|
+
const client = new MemoryOS('your-api-key');
|
|
54
|
+
|
|
55
|
+
// Store conversation context
|
|
56
|
+
async function rememberConversation(userInput: string, response: string) {
|
|
57
|
+
await client.store(
|
|
58
|
+
`User: ${userInput}\nAssistant: ${response}`,
|
|
59
|
+
{ type: 'conversation' }
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Retrieve relevant context
|
|
64
|
+
async function getContext(query: string) {
|
|
65
|
+
const results = await client.search(query, 5);
|
|
66
|
+
return results.map(m => m.content);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Integration with AutoGPT
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import MemoryOS from 'memoryos';
|
|
74
|
+
|
|
75
|
+
class MemoryManager {
|
|
76
|
+
private client: MemoryOS;
|
|
77
|
+
|
|
78
|
+
constructor(apiKey: string) {
|
|
79
|
+
this.client = new MemoryOS(apiKey);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async remember(fact: string, category: string) {
|
|
83
|
+
await this.client.store(fact, { category });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async recall(query: string, limit = 5) {
|
|
87
|
+
return this.client.search(query, limit);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async forget(memoryId: number) {
|
|
91
|
+
return this.client.delete(memoryId);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Usage
|
|
96
|
+
const manager = new MemoryManager('your-api-key');
|
|
97
|
+
await manager.remember('User is in New York', 'location');
|
|
98
|
+
const memories = await manager.recall('where is the user');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## API Reference
|
|
102
|
+
|
|
103
|
+
### MemoryOS(apiKey, baseUrl?)
|
|
104
|
+
|
|
105
|
+
Initialize the MemoryOS client.
|
|
106
|
+
|
|
107
|
+
**Parameters:**
|
|
108
|
+
- `apiKey` (string): Your MemoryOS API key
|
|
109
|
+
- `baseUrl` (string, optional): Base URL of the MemoryOS API
|
|
110
|
+
|
|
111
|
+
### store(content, metadata?)
|
|
112
|
+
|
|
113
|
+
Store a new memory.
|
|
114
|
+
|
|
115
|
+
**Parameters:**
|
|
116
|
+
- `content` (string): Memory content (max 10,000 characters)
|
|
117
|
+
- `metadata` (object, optional): Custom metadata
|
|
118
|
+
|
|
119
|
+
**Returns:** Promise<number> - Memory ID
|
|
120
|
+
|
|
121
|
+
### search(query, limit?)
|
|
122
|
+
|
|
123
|
+
Search memories using semantic search.
|
|
124
|
+
|
|
125
|
+
**Parameters:**
|
|
126
|
+
- `query` (string): Search query
|
|
127
|
+
- `limit` (number, optional): Max results (default: 10, max: 100)
|
|
128
|
+
|
|
129
|
+
**Returns:** Promise<Memory[]> - List of matching memories
|
|
130
|
+
|
|
131
|
+
### list(options?)
|
|
132
|
+
|
|
133
|
+
List all memories with pagination.
|
|
134
|
+
|
|
135
|
+
**Parameters:**
|
|
136
|
+
- `options.limit` (number, optional): Results per page (default: 20, max: 100)
|
|
137
|
+
- `options.offset` (number, optional): Number to skip (default: 0)
|
|
138
|
+
|
|
139
|
+
**Returns:** Promise<Memory[]> - List of memories
|
|
140
|
+
|
|
141
|
+
### get(memoryId)
|
|
142
|
+
|
|
143
|
+
Get a specific memory by ID.
|
|
144
|
+
|
|
145
|
+
**Parameters:**
|
|
146
|
+
- `memoryId` (number): Memory ID
|
|
147
|
+
|
|
148
|
+
**Returns:** Promise<Memory> - Memory object
|
|
149
|
+
|
|
150
|
+
### delete(memoryId)
|
|
151
|
+
|
|
152
|
+
Delete a memory.
|
|
153
|
+
|
|
154
|
+
**Parameters:**
|
|
155
|
+
- `memoryId` (number): Memory ID
|
|
156
|
+
|
|
157
|
+
**Returns:** Promise<boolean> - True if successful
|
|
158
|
+
|
|
159
|
+
### stats()
|
|
160
|
+
|
|
161
|
+
Get memory statistics.
|
|
162
|
+
|
|
163
|
+
**Returns:** Promise<MemoryStats> - Statistics object
|
|
164
|
+
|
|
165
|
+
## Error Handling
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { MemoryOSError } from 'memoryos';
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
await client.store('Some memory');
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (error instanceof MemoryOSError) {
|
|
174
|
+
console.error(`Error: ${error.message}`);
|
|
175
|
+
console.error(`Status: ${error.statusCode}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## TypeScript Support
|
|
181
|
+
|
|
182
|
+
Full TypeScript support included. Type definitions are automatically available:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import MemoryOS, { Memory, MemoryStats } from 'memoryos';
|
|
186
|
+
|
|
187
|
+
const client = new MemoryOS('your-api-key');
|
|
188
|
+
const memories: Memory[] = await client.list();
|
|
189
|
+
const stats: MemoryStats = await client.stats();
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Getting Your API Key
|
|
193
|
+
|
|
194
|
+
1. Sign up at https://billiondna-vaiyukai.manus.space
|
|
195
|
+
2. Go to your dashboard
|
|
196
|
+
3. Create a new API key in the "API Keys" section
|
|
197
|
+
4. Use it in your code
|
|
198
|
+
|
|
199
|
+
## Support
|
|
200
|
+
|
|
201
|
+
For issues and questions, visit https://billiondna-vaiyukai.manus.space/docs
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemoryOS Node.js/TypeScript SDK
|
|
3
|
+
* Persistent memory for AI agents
|
|
4
|
+
*/
|
|
5
|
+
export interface Memory {
|
|
6
|
+
id: number;
|
|
7
|
+
content: string;
|
|
8
|
+
metadata?: Record<string, any>;
|
|
9
|
+
createdAt?: string;
|
|
10
|
+
updatedAt?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface MemoryStats {
|
|
13
|
+
totalMemories: number;
|
|
14
|
+
oldestMemory?: string;
|
|
15
|
+
newestMemory?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare class MemoryOSError extends Error {
|
|
18
|
+
statusCode?: number | undefined;
|
|
19
|
+
constructor(message: string, statusCode?: number | undefined);
|
|
20
|
+
}
|
|
21
|
+
export declare class MemoryOS {
|
|
22
|
+
private apiKey;
|
|
23
|
+
private baseUrl;
|
|
24
|
+
private headers;
|
|
25
|
+
/**
|
|
26
|
+
* Initialize MemoryOS client
|
|
27
|
+
* @param apiKey Your MemoryOS API key
|
|
28
|
+
* @param baseUrl Base URL of the MemoryOS API (default: production)
|
|
29
|
+
*/
|
|
30
|
+
constructor(apiKey: string, baseUrl?: string);
|
|
31
|
+
/**
|
|
32
|
+
* Store a new memory
|
|
33
|
+
* @param content The memory content (up to 10,000 characters)
|
|
34
|
+
* @param metadata Optional metadata object
|
|
35
|
+
* @returns Memory ID of the stored memory
|
|
36
|
+
*/
|
|
37
|
+
store(content: string, metadata?: Record<string, any>): Promise<number>;
|
|
38
|
+
/**
|
|
39
|
+
* Search for memories
|
|
40
|
+
* @param query Search query (semantic search)
|
|
41
|
+
* @param limit Maximum number of results (default: 10, max: 100)
|
|
42
|
+
* @returns List of matching memories
|
|
43
|
+
*/
|
|
44
|
+
search(query: string, limit?: number): Promise<Memory[]>;
|
|
45
|
+
/**
|
|
46
|
+
* List all memories with pagination
|
|
47
|
+
* @param limit Number of memories to return (default: 20, max: 100)
|
|
48
|
+
* @param offset Number of memories to skip (default: 0)
|
|
49
|
+
* @returns List of memories
|
|
50
|
+
*/
|
|
51
|
+
list(limit?: number, offset?: number): Promise<Memory[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Get a specific memory by ID
|
|
54
|
+
* @param memoryId ID of the memory to retrieve
|
|
55
|
+
* @returns The memory object
|
|
56
|
+
*/
|
|
57
|
+
get(memoryId: number): Promise<Memory>;
|
|
58
|
+
/**
|
|
59
|
+
* Delete a memory
|
|
60
|
+
* @param memoryId ID of the memory to delete
|
|
61
|
+
* @returns True if deletion was successful
|
|
62
|
+
*/
|
|
63
|
+
delete(memoryId: number): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Get memory statistics
|
|
66
|
+
* @returns Memory statistics
|
|
67
|
+
*/
|
|
68
|
+
stats(): Promise<MemoryStats>;
|
|
69
|
+
private request;
|
|
70
|
+
}
|
|
71
|
+
export default MemoryOS;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemoryOS Node.js/TypeScript SDK
|
|
3
|
+
* Persistent memory for AI agents
|
|
4
|
+
*/
|
|
5
|
+
export class MemoryOSError extends Error {
|
|
6
|
+
constructor(message, statusCode) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this.name = "MemoryOSError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class MemoryOS {
|
|
13
|
+
/**
|
|
14
|
+
* Initialize MemoryOS client
|
|
15
|
+
* @param apiKey Your MemoryOS API key
|
|
16
|
+
* @param baseUrl Base URL of the MemoryOS API (default: production)
|
|
17
|
+
*/
|
|
18
|
+
constructor(apiKey, baseUrl = "https://billiondna-vaiyukai.manus.space") {
|
|
19
|
+
this.apiKey = apiKey;
|
|
20
|
+
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
21
|
+
this.headers = {
|
|
22
|
+
Authorization: `Bearer ${apiKey}`,
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Store a new memory
|
|
28
|
+
* @param content The memory content (up to 10,000 characters)
|
|
29
|
+
* @param metadata Optional metadata object
|
|
30
|
+
* @returns Memory ID of the stored memory
|
|
31
|
+
*/
|
|
32
|
+
async store(content, metadata) {
|
|
33
|
+
const response = await this.request("POST", "/api/memories/store", {
|
|
34
|
+
content,
|
|
35
|
+
metadata,
|
|
36
|
+
});
|
|
37
|
+
return response.id;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Search for memories
|
|
41
|
+
* @param query Search query (semantic search)
|
|
42
|
+
* @param limit Maximum number of results (default: 10, max: 100)
|
|
43
|
+
* @returns List of matching memories
|
|
44
|
+
*/
|
|
45
|
+
async search(query, limit = 10) {
|
|
46
|
+
const params = new URLSearchParams({
|
|
47
|
+
query,
|
|
48
|
+
limit: Math.min(limit, 100).toString(),
|
|
49
|
+
});
|
|
50
|
+
return this.request("GET", `/api/memories/search?${params}`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* List all memories with pagination
|
|
54
|
+
* @param limit Number of memories to return (default: 20, max: 100)
|
|
55
|
+
* @param offset Number of memories to skip (default: 0)
|
|
56
|
+
* @returns List of memories
|
|
57
|
+
*/
|
|
58
|
+
async list(limit = 20, offset = 0) {
|
|
59
|
+
const params = new URLSearchParams({
|
|
60
|
+
limit: Math.min(limit, 100).toString(),
|
|
61
|
+
offset: offset.toString(),
|
|
62
|
+
});
|
|
63
|
+
return this.request("GET", `/api/memories/list?${params}`);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get a specific memory by ID
|
|
67
|
+
* @param memoryId ID of the memory to retrieve
|
|
68
|
+
* @returns The memory object
|
|
69
|
+
*/
|
|
70
|
+
async get(memoryId) {
|
|
71
|
+
return this.request("GET", `/api/memories/${memoryId}`);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Delete a memory
|
|
75
|
+
* @param memoryId ID of the memory to delete
|
|
76
|
+
* @returns True if deletion was successful
|
|
77
|
+
*/
|
|
78
|
+
async delete(memoryId) {
|
|
79
|
+
await this.request("DELETE", `/api/memories/${memoryId}`);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get memory statistics
|
|
84
|
+
* @returns Memory statistics
|
|
85
|
+
*/
|
|
86
|
+
async stats() {
|
|
87
|
+
return this.request("GET", "/api/memories/stats");
|
|
88
|
+
}
|
|
89
|
+
async request(method, path, body) {
|
|
90
|
+
const url = `${this.baseUrl}${path}`;
|
|
91
|
+
const options = {
|
|
92
|
+
method,
|
|
93
|
+
headers: this.headers,
|
|
94
|
+
};
|
|
95
|
+
if (body) {
|
|
96
|
+
options.body = JSON.stringify(body);
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const response = await fetch(url, options);
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
let message = response.statusText;
|
|
102
|
+
try {
|
|
103
|
+
const errorData = await response.json();
|
|
104
|
+
message = errorData.message || message;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
// Use statusText if JSON parsing fails
|
|
108
|
+
}
|
|
109
|
+
throw new MemoryOSError(`API Error (${response.status}): ${message}`, response.status);
|
|
110
|
+
}
|
|
111
|
+
return response.json();
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
if (error instanceof MemoryOSError) {
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
118
|
+
throw new MemoryOSError(`Request failed: ${message}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export default MemoryOS;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "memoryos-api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js/TypeScript SDK for MemoryOS - Persistent memory for AI agents",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "npm run build",
|
|
10
|
+
"test": "jest"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"memory",
|
|
14
|
+
"ai",
|
|
15
|
+
"agents",
|
|
16
|
+
"persistent",
|
|
17
|
+
"storage",
|
|
18
|
+
"api"
|
|
19
|
+
],
|
|
20
|
+
"author": "MemoryOS",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/memoryos/sdk-node"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^20.0.0",
|
|
28
|
+
"typescript": "^5.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemoryOS Node.js/TypeScript SDK
|
|
3
|
+
* Persistent memory for AI agents
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface Memory {
|
|
7
|
+
id: number;
|
|
8
|
+
content: string;
|
|
9
|
+
metadata?: Record<string, any>;
|
|
10
|
+
createdAt?: string;
|
|
11
|
+
updatedAt?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface MemoryStats {
|
|
15
|
+
totalMemories: number;
|
|
16
|
+
oldestMemory?: string;
|
|
17
|
+
newestMemory?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class MemoryOSError extends Error {
|
|
21
|
+
constructor(
|
|
22
|
+
message: string,
|
|
23
|
+
public statusCode?: number
|
|
24
|
+
) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "MemoryOSError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class MemoryOS {
|
|
31
|
+
private apiKey: string;
|
|
32
|
+
private baseUrl: string;
|
|
33
|
+
private headers: Record<string, string>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Initialize MemoryOS client
|
|
37
|
+
* @param apiKey Your MemoryOS API key
|
|
38
|
+
* @param baseUrl Base URL of the MemoryOS API (default: production)
|
|
39
|
+
*/
|
|
40
|
+
constructor(
|
|
41
|
+
apiKey: string,
|
|
42
|
+
baseUrl: string = "https://billiondna-vaiyukai.manus.space"
|
|
43
|
+
) {
|
|
44
|
+
this.apiKey = apiKey;
|
|
45
|
+
this.baseUrl = baseUrl.replace(/\/$/, "");
|
|
46
|
+
this.headers = {
|
|
47
|
+
Authorization: `Bearer ${apiKey}`,
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Store a new memory
|
|
54
|
+
* @param content The memory content (up to 10,000 characters)
|
|
55
|
+
* @param metadata Optional metadata object
|
|
56
|
+
* @returns Memory ID of the stored memory
|
|
57
|
+
*/
|
|
58
|
+
async store(
|
|
59
|
+
content: string,
|
|
60
|
+
metadata?: Record<string, any>
|
|
61
|
+
): Promise<number> {
|
|
62
|
+
const response = await this.request("POST", "/api/memories/store", {
|
|
63
|
+
content,
|
|
64
|
+
metadata,
|
|
65
|
+
});
|
|
66
|
+
return response.id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Search for memories
|
|
71
|
+
* @param query Search query (semantic search)
|
|
72
|
+
* @param limit Maximum number of results (default: 10, max: 100)
|
|
73
|
+
* @returns List of matching memories
|
|
74
|
+
*/
|
|
75
|
+
async search(query: string, limit: number = 10): Promise<Memory[]> {
|
|
76
|
+
const params = new URLSearchParams({
|
|
77
|
+
query,
|
|
78
|
+
limit: Math.min(limit, 100).toString(),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return this.request("GET", `/api/memories/search?${params}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* List all memories with pagination
|
|
86
|
+
* @param limit Number of memories to return (default: 20, max: 100)
|
|
87
|
+
* @param offset Number of memories to skip (default: 0)
|
|
88
|
+
* @returns List of memories
|
|
89
|
+
*/
|
|
90
|
+
async list(limit: number = 20, offset: number = 0): Promise<Memory[]> {
|
|
91
|
+
const params = new URLSearchParams({
|
|
92
|
+
limit: Math.min(limit, 100).toString(),
|
|
93
|
+
offset: offset.toString(),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return this.request("GET", `/api/memories/list?${params}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get a specific memory by ID
|
|
101
|
+
* @param memoryId ID of the memory to retrieve
|
|
102
|
+
* @returns The memory object
|
|
103
|
+
*/
|
|
104
|
+
async get(memoryId: number): Promise<Memory> {
|
|
105
|
+
return this.request("GET", `/api/memories/${memoryId}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Delete a memory
|
|
110
|
+
* @param memoryId ID of the memory to delete
|
|
111
|
+
* @returns True if deletion was successful
|
|
112
|
+
*/
|
|
113
|
+
async delete(memoryId: number): Promise<boolean> {
|
|
114
|
+
await this.request("DELETE", `/api/memories/${memoryId}`);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get memory statistics
|
|
120
|
+
* @returns Memory statistics
|
|
121
|
+
*/
|
|
122
|
+
async stats(): Promise<MemoryStats> {
|
|
123
|
+
return this.request("GET", "/api/memories/stats");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private async request(
|
|
127
|
+
method: string,
|
|
128
|
+
path: string,
|
|
129
|
+
body?: any
|
|
130
|
+
): Promise<any> {
|
|
131
|
+
const url = `${this.baseUrl}${path}`;
|
|
132
|
+
|
|
133
|
+
const options: RequestInit = {
|
|
134
|
+
method,
|
|
135
|
+
headers: this.headers,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
if (body) {
|
|
139
|
+
options.body = JSON.stringify(body);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const response = await fetch(url, options);
|
|
144
|
+
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
let message = response.statusText;
|
|
147
|
+
try {
|
|
148
|
+
const errorData = await response.json();
|
|
149
|
+
message = (errorData as any).message || message;
|
|
150
|
+
} catch {
|
|
151
|
+
// Use statusText if JSON parsing fails
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
throw new MemoryOSError(
|
|
155
|
+
`API Error (${response.status}): ${message}`,
|
|
156
|
+
response.status
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return response.json();
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (error instanceof MemoryOSError) {
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
166
|
+
throw new MemoryOSError(`Request failed: ${message}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export default MemoryOS;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"moduleResolution": "node"
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
18
|
+
}
|