@danielzfliu/memory 2.0.0 → 2.2.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 +9 -351
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +35 -0
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +314 -44
- package/dist/server.js.map +1 -1
- package/dist/store.d.ts +6 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +43 -14
- package/dist/store.js.map +1 -1
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/README.md +9 -0
- package/docs/configuration.md +55 -0
- package/docs/getting-started.md +71 -0
- package/docs/mcp-server.md +61 -0
- package/docs/npm-package.md +100 -0
- package/docs/rest-api.md +455 -0
- package/package.json +3 -1
package/dist/types.d.ts
CHANGED
|
@@ -4,12 +4,15 @@ export interface Piece {
|
|
|
4
4
|
title?: string;
|
|
5
5
|
tags: string[];
|
|
6
6
|
}
|
|
7
|
+
export type RequestLoggingMode = "off" | "metadata" | "body";
|
|
7
8
|
export interface MemoryConfig {
|
|
8
9
|
chromaUrl?: string;
|
|
9
10
|
ollamaUrl?: string;
|
|
10
11
|
embeddingModel?: string;
|
|
11
12
|
generationModel?: string;
|
|
12
13
|
collectionName?: string;
|
|
14
|
+
requestLogging?: RequestLoggingMode;
|
|
15
|
+
corsOrigins?: string[];
|
|
13
16
|
}
|
|
14
17
|
export interface QueryOptions {
|
|
15
18
|
tags?: string[];
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,KAAK;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,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
1
|
+
# Documentation
|
|
2
|
+
|
|
3
|
+
Use this index to find the authoritative guide for each topic:
|
|
4
|
+
|
|
5
|
+
- [Getting Started](./getting-started.md): local clone setup, Ollama, ChromaDB, and how to choose a mode
|
|
6
|
+
- [MCP Server](./mcp-server.md): stdio usage and available MCP tools
|
|
7
|
+
- [npm Package](./npm-package.md): programmatic usage and public exports
|
|
8
|
+
- [REST API](./rest-api.md): HTTP server startup and endpoint contract
|
|
9
|
+
- [Configuration](./configuration.md): runtime options and environment variable overrides
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Configuration
|
|
2
|
+
|
|
3
|
+
All `MemoryConfig` fields are optional. Defaults are applied automatically.
|
|
4
|
+
|
|
5
|
+
| Option | Default | Description |
|
|
6
|
+
|--------|---------|-------------|
|
|
7
|
+
| `chromaUrl` | `http://localhost:8000` | ChromaDB server URL |
|
|
8
|
+
| `ollamaUrl` | `http://localhost:11434` | Ollama server URL |
|
|
9
|
+
| `embeddingModel` | `nomic-embed-text-v2-moe:latest` | Ollama model for embeddings |
|
|
10
|
+
| `generationModel` | `gemma3:latest` | Ollama model for RAG generation |
|
|
11
|
+
| `collectionName` | `pieces` | Default ChromaDB collection name |
|
|
12
|
+
| `requestLogging` | `off` | REST request logging mode: `off`, `metadata`, or `body` |
|
|
13
|
+
| `corsOrigins` | `[]` | Optional allowlist of browser origins permitted to receive CORS headers |
|
|
14
|
+
|
|
15
|
+
`generationModel` is used by `createServer` and `MemoryMcpServer`. When constructing `RagPipeline` directly, pass the model name to its constructor.
|
|
16
|
+
|
|
17
|
+
`requestLogging: "metadata"` logs method, URL, status, duration, and collection when present.
|
|
18
|
+
|
|
19
|
+
`requestLogging: "body"` additionally logs the JSON request body. This should stay opt-in because queries and stored content may be sensitive.
|
|
20
|
+
|
|
21
|
+
`corsOrigins` enables CORS only for the listed origins. When empty, the REST API does not emit CORS headers.
|
|
22
|
+
|
|
23
|
+
For safety, keep `corsOrigins` explicit and avoid permissive wildcards for non-local use.
|
|
24
|
+
|
|
25
|
+
## Environment variable overrides
|
|
26
|
+
|
|
27
|
+
Runtime configuration can be overridden with either `MEMORY_*` or non-prefixed environment variable names:
|
|
28
|
+
|
|
29
|
+
| Config field | Supported environment variables |
|
|
30
|
+
|-------------|----------------------------------|
|
|
31
|
+
| `chromaUrl` | `MEMORY_CHROMA_URL`, `CHROMA_URL` |
|
|
32
|
+
| `ollamaUrl` | `MEMORY_OLLAMA_URL`, `OLLAMA_URL` |
|
|
33
|
+
| `embeddingModel` | `MEMORY_EMBEDDING_MODEL`, `EMBEDDING_MODEL` |
|
|
34
|
+
| `generationModel` | `MEMORY_GENERATION_MODEL`, `GENERATION_MODEL` |
|
|
35
|
+
| `collectionName` | `MEMORY_COLLECTION_NAME`, `COLLECTION_NAME` |
|
|
36
|
+
| `requestLogging` | `MEMORY_REQUEST_LOGGING`, `REQUEST_LOGGING` |
|
|
37
|
+
| `corsOrigins` | `MEMORY_CORS_ORIGINS`, `CORS_ORIGINS` |
|
|
38
|
+
|
|
39
|
+
For `requestLogging`, supported values are `off`, `metadata`, and `body`.
|
|
40
|
+
|
|
41
|
+
For `corsOrigins`, provide a comma-separated list such as `http://localhost:5173,https://notes.example.com`.
|
|
42
|
+
|
|
43
|
+
## Example
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { createServer } from "@danielzfliu/memory";
|
|
47
|
+
|
|
48
|
+
const app = createServer({
|
|
49
|
+
chromaUrl: process.env.MEMORY_CHROMA_URL,
|
|
50
|
+
ollamaUrl: process.env.MEMORY_OLLAMA_URL,
|
|
51
|
+
collectionName: "agent-alice",
|
|
52
|
+
requestLogging: "metadata",
|
|
53
|
+
corsOrigins: ["http://localhost:5173"],
|
|
54
|
+
});
|
|
55
|
+
```
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
## Prerequisites
|
|
4
|
+
|
|
5
|
+
- **Node.js** `>= 18`
|
|
6
|
+
- **Ollama** running locally
|
|
7
|
+
- **ChromaDB** server running locally
|
|
8
|
+
|
|
9
|
+
## Clone and install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
git clone https://github.com/DanielZFLiu/memory.git
|
|
13
|
+
cd memory
|
|
14
|
+
npm install
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Set up Ollama
|
|
18
|
+
|
|
19
|
+
Install Ollama from [ollama.com](https://ollama.com), then pull the default models:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
ollama pull nomic-embed-text-v2-moe:latest
|
|
23
|
+
ollama pull gemma3:latest
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Start Ollama:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm run ollama # start Ollama on port 11434
|
|
30
|
+
npm run ollama:port -- 11435 # start Ollama on a custom port
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Set up ChromaDB
|
|
34
|
+
|
|
35
|
+
### Option 1: Docker
|
|
36
|
+
|
|
37
|
+
The repository includes `docker-compose.yml` for running ChromaDB and persisting data in `./chroma/`.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm run docker:up
|
|
41
|
+
npm run docker:logs
|
|
42
|
+
npm run docker:down
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
This exposes ChromaDB on `http://localhost:8000`.
|
|
46
|
+
|
|
47
|
+
### Option 2: pip
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install chromadb
|
|
51
|
+
chroma run --port 8000
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
You may need to add Python's Scripts directory to your `PATH` after installation.
|
|
55
|
+
|
|
56
|
+
### Option 3: npm helper script
|
|
57
|
+
|
|
58
|
+
If `chroma` is already available on your machine, you can also run:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm run db # start ChromaDB on port 8000
|
|
62
|
+
npm run db:port -- 8001 # start ChromaDB on a custom port
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Choose a usage mode
|
|
66
|
+
|
|
67
|
+
- [MCP Server](./mcp-server.md)
|
|
68
|
+
- [npm Package](./npm-package.md)
|
|
69
|
+
- [REST API](./rest-api.md)
|
|
70
|
+
|
|
71
|
+
See [Configuration](./configuration.md) for runtime options and supported environment variables.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# MCP Server
|
|
2
|
+
|
|
3
|
+
Use Memory as a standalone Model Context Protocol server over stdio.
|
|
4
|
+
|
|
5
|
+
After completing [Getting Started](./getting-started.md), run the MCP server from a local clone with:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm run build
|
|
9
|
+
node ./dist/main.js
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Memory MCP communicates over stdio, so it does not open an HTTP port.
|
|
13
|
+
|
|
14
|
+
## Run via `npx`
|
|
15
|
+
|
|
16
|
+
In an MCP client configuration, you can run the published package directly:
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
{
|
|
20
|
+
"mcpServers": {
|
|
21
|
+
"memory": {
|
|
22
|
+
"command": "npx",
|
|
23
|
+
"args": ["-y", "@danielzfliu/memory"]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
If you are running from a local clone instead:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"mcpServers": {
|
|
34
|
+
"memory": {
|
|
35
|
+
"command": "node",
|
|
36
|
+
"args": ["c:/path/to/memory/dist/main.js"]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Available tools
|
|
43
|
+
|
|
44
|
+
| Tool | Description |
|
|
45
|
+
|------|-------------|
|
|
46
|
+
| `add_piece` | Add a new piece with optional title, tags, and collection |
|
|
47
|
+
| `get_piece` | Retrieve a piece by id |
|
|
48
|
+
| `update_piece` | Update piece content, title, and/or tags; `title: null` clears the title |
|
|
49
|
+
| `delete_piece` | Delete a piece by id |
|
|
50
|
+
| `query_pieces` | Semantic search over stored pieces; supports optional hybrid search |
|
|
51
|
+
| `rag_query` | Retrieve relevant pieces and generate a grounded answer with citations |
|
|
52
|
+
| `list_collections` | List all collection names in the memory store |
|
|
53
|
+
| `delete_collection` | Delete an entire collection and all its pieces |
|
|
54
|
+
|
|
55
|
+
All piece-level tools accept an optional `collection` parameter to target a specific collection instead of the default collection.
|
|
56
|
+
|
|
57
|
+
There is no separate `create_collection` MCP tool. Collections are created implicitly the first time an agent writes to a new collection.
|
|
58
|
+
|
|
59
|
+
## Configuration
|
|
60
|
+
|
|
61
|
+
The MCP server uses the same runtime configuration as the library and REST API. See [Configuration](./configuration.md).
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# npm Package
|
|
2
|
+
|
|
3
|
+
Install Memory into your own Node.js or TypeScript project:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @danielzfliu/memory
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Programmatic usage
|
|
10
|
+
|
|
11
|
+
The examples below are written in TypeScript.
|
|
12
|
+
|
|
13
|
+
### Use `PieceStore` and `RagPipeline`
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { PieceStore, RagPipeline, MemoryConfig } from "@danielzfliu/memory";
|
|
17
|
+
|
|
18
|
+
async function main() {
|
|
19
|
+
const config: MemoryConfig = {
|
|
20
|
+
chromaUrl: "http://localhost:8000",
|
|
21
|
+
ollamaUrl: "http://localhost:11434",
|
|
22
|
+
embeddingModel: "nomic-embed-text-v2-moe:latest",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const store = new PieceStore(config);
|
|
26
|
+
await store.init();
|
|
27
|
+
|
|
28
|
+
await store.addPiece(
|
|
29
|
+
"TypeScript is a typed superset of JavaScript.",
|
|
30
|
+
["typescript", "programming"],
|
|
31
|
+
"TypeScript overview",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const results = await store.queryPieces("typed languages", { topK: 5 });
|
|
35
|
+
console.log("results", results);
|
|
36
|
+
|
|
37
|
+
const filtered = await store.queryPieces("typed languages", {
|
|
38
|
+
tags: ["typescript"],
|
|
39
|
+
topK: 5,
|
|
40
|
+
});
|
|
41
|
+
console.log("filtered", filtered);
|
|
42
|
+
|
|
43
|
+
const hybrid = await store.queryPieces("typed languages", {
|
|
44
|
+
topK: 5,
|
|
45
|
+
useHybridSearch: true,
|
|
46
|
+
});
|
|
47
|
+
console.log("hybrid", hybrid);
|
|
48
|
+
|
|
49
|
+
const rag = new RagPipeline(store, config.ollamaUrl!, "gemma3:latest");
|
|
50
|
+
const answer = await rag.query("What is TypeScript?", {
|
|
51
|
+
tags: ["programming"],
|
|
52
|
+
});
|
|
53
|
+
console.log("answer", answer);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
main().catch((err) => {
|
|
57
|
+
console.error(err);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Embed the REST API in your own Express app
|
|
63
|
+
|
|
64
|
+
`createServer` returns a configured Express app you can mount or extend:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { createServer } from "@danielzfliu/memory";
|
|
68
|
+
|
|
69
|
+
const app = createServer({
|
|
70
|
+
chromaUrl: "http://localhost:8000",
|
|
71
|
+
ollamaUrl: "http://localhost:11434",
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
app.listen(4000, () => console.log("Running on :4000"));
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`createServer` exposes the full REST surface.
|
|
78
|
+
|
|
79
|
+
If you are building your own app or dashboard directly on the library, `PieceStore` also exposes lower-level collection and listing helpers such as `createCollection`, `listCollections`, `listPieces`, and `listTags`.
|
|
80
|
+
|
|
81
|
+
## Public exports
|
|
82
|
+
|
|
83
|
+
| Export | Description |
|
|
84
|
+
|--------|-------------|
|
|
85
|
+
| `PieceStore` | CRUD and semantic search over tagged text pieces |
|
|
86
|
+
| `RagPipeline` | Retrieve-then-generate pipeline using `PieceStore` and Ollama |
|
|
87
|
+
| `EmbeddingClient` | Low-level Ollama embedding wrapper |
|
|
88
|
+
| `MemoryMcpServer` | MCP server class for stdio transport |
|
|
89
|
+
| `createServer` | Express app factory with REST endpoints pre-configured |
|
|
90
|
+
| `MemoryConfig` | Configuration interface |
|
|
91
|
+
| `DEFAULT_MEMORY_CONFIG` | Default resolved configuration values |
|
|
92
|
+
| `RequestLoggingMode` | REST request logging mode: `off`, `metadata`, or `body` |
|
|
93
|
+
| `Piece` | `{ id, content, title?, tags }` |
|
|
94
|
+
| `QueryOptions` | `{ tags?, topK?, useHybridSearch? }` |
|
|
95
|
+
| `QueryResult` | `{ piece, score }` |
|
|
96
|
+
| `RagResult` | `{ answer, sources }` |
|
|
97
|
+
|
|
98
|
+
## Configuration
|
|
99
|
+
|
|
100
|
+
See [Configuration](./configuration.md) for defaults and environment variable overrides.
|