@ghilteras/opencode-agent-memory 0.3.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/LICENSE +9 -0
- package/README.md +180 -0
- package/package.json +42 -0
- package/src/embeddings.ts +46 -0
- package/src/frontmatter.ts +47 -0
- package/src/journal.test.ts +384 -0
- package/src/journal.ts +358 -0
- package/src/letta.ts +47 -0
- package/src/memory.test.ts +29 -0
- package/src/memory.ts +300 -0
- package/src/plugin.ts +80 -0
- package/src/prompt.test.ts +158 -0
- package/src/prompt.ts +58 -0
- package/src/tools.ts +200 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Josh Thomas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# opencode-agent-memory
|
|
2
|
+
|
|
3
|
+
> **Fork notice**: This is the maintained fork `@ghilteras/opencode-agent-memory` v0.3.0 of
|
|
4
|
+
> [opencode-agent-memory](https://github.com/joshuadavidthomas/opencode-agent-memory)
|
|
5
|
+
> (joshuadavidthomas, MIT). This release adopts the "proper timestamps without cache
|
|
6
|
+
> busting" design (per-block `modified_at` frontmatter, remove the volatile
|
|
7
|
+
> `memory_metadata` block, append memory XML at the end of the system prompt) derived
|
|
8
|
+
> from the Annakan/draxxris fork plan. Upstream PR #20 (freeze metadata timestamps) is
|
|
9
|
+
> superseded by the stronger per-block design and remains open upstream.
|
|
10
|
+
|
|
11
|
+
[Letta](https://letta.com)-style editable [memory blocks](https://docs.letta.com/guides/agents/memory-blocks/) for [OpenCode](https://opencode.ai).
|
|
12
|
+
|
|
13
|
+
## Experimental
|
|
14
|
+
|
|
15
|
+
This plugin is experimental. The core idea - giving the agent persistent, self-editable memory blocks - is adapted from [Letta](https://github.com/letta-ai/letta). Specifially, the plugin follows Letta's [shared memory blocks](https://docs.letta.com/tutorials/shared-memory-blocks) pattern - the markdown files on disk are shared state that every OpenCode session can read and write.
|
|
16
|
+
|
|
17
|
+
Think of it as AGENTS.md with a harness. OpenCode supports [rules](https://opencode.ai/docs/rules/) via `AGENTS.md` and custom instruction files - this plugin is similar in spirit, but adds structure (scoped blocks with metadata and size limits), dedicated tools for memory operations, and prompting that encourages the agent to actively maintain its own memory. The content is similar; the scaffolding around it is what's different.
|
|
18
|
+
|
|
19
|
+
For background on the memory concept, see Letta's docs on [memory](https://docs.letta.com/guides/agents/memory/) and [memory blocks](https://docs.letta.com/guides/agents/memory-blocks/).
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- **Persistent memory** - Information survives across sessions and context compaction
|
|
24
|
+
- **Shared across sessions** - Global blocks shared across all projects, project blocks shared across sessions in that codebase
|
|
25
|
+
- **Self-editing** - The agent can read and modify its own memory with dedicated tools
|
|
26
|
+
- **System prompt injection** - Memory blocks appear in the system prompt, always in-context
|
|
27
|
+
- **Journal** - Append-only entries with semantic search for capturing insights, decisions, and discoveries across sessions
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
- [OpenCode](https://opencode.ai/) v1.0.115 or later
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Add to your OpenCode config (`~/.config/opencode/opencode.json`):
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"plugin": ["opencode-agent-memory"]
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Restart OpenCode and you're ready to go.
|
|
44
|
+
|
|
45
|
+
Optionally, pin to a specific version for stability:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"plugin": ["opencode-agent-memory@0.2.0"]
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
OpenCode fetches unpinned plugins from npm on each startup; pinned versions are cached and require a manual version bump to update.
|
|
54
|
+
|
|
55
|
+
### Local Development
|
|
56
|
+
|
|
57
|
+
If you want to customize or contribute:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
git clone https://github.com/joshuadavidthomas/opencode-agent-memory ~/.config/opencode/opencode-agent-memory
|
|
61
|
+
mkdir -p ~/.config/opencode/plugin
|
|
62
|
+
ln -sf ~/.config/opencode/opencode-agent-memory/src/plugin.ts ~/.config/opencode/plugin/memory.ts
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
### Memory Tools
|
|
68
|
+
|
|
69
|
+
The plugin gives the agent 3 tools for managing memory:
|
|
70
|
+
|
|
71
|
+
| Tool | Description |
|
|
72
|
+
|------|-------------|
|
|
73
|
+
| `memory_list` | List available memory blocks (labels, descriptions, sizes) |
|
|
74
|
+
| `memory_set` | Create or update a memory block (full overwrite) |
|
|
75
|
+
| `memory_replace` | Replace a substring within a memory block |
|
|
76
|
+
|
|
77
|
+
You interact with memory by editing the markdown files directly or asking the agent to update its memory.
|
|
78
|
+
|
|
79
|
+
### Journal Tools
|
|
80
|
+
|
|
81
|
+
When the journal is enabled, the agent gets 3 additional tools:
|
|
82
|
+
|
|
83
|
+
| Tool | Description |
|
|
84
|
+
|------|-------------|
|
|
85
|
+
| `journal_write` | Write a new journal entry with title, body, and optional tags |
|
|
86
|
+
| `journal_search` | Search entries semantically, filter by project or tags, with pagination |
|
|
87
|
+
| `journal_read` | Read a specific journal entry by ID |
|
|
88
|
+
|
|
89
|
+
Journal entries are append-only markdown files with YAML frontmatter, stored in `~/.config/opencode/journal/`. Each entry records which project, model, provider, agent, and session it was written from. Semantic search uses local embeddings ([all-MiniLM-L6-v2](https://huggingface.co/Xenova/all-MiniLM-L6-v2)) - no data leaves your machine.
|
|
90
|
+
|
|
91
|
+
### Default Blocks
|
|
92
|
+
|
|
93
|
+
Three blocks are seeded on first run:
|
|
94
|
+
|
|
95
|
+
| Block | Scope | Purpose |
|
|
96
|
+
|-------|-------|---------|
|
|
97
|
+
| `persona` | global | How the agent should behave and respond |
|
|
98
|
+
| `human` | global | Details about you (preferences, habits, constraints) |
|
|
99
|
+
| `project` | project | Codebase-specific knowledge (commands, architecture, conventions) |
|
|
100
|
+
|
|
101
|
+
These are just starting points. Create whatever blocks make sense for your workflow - `debugging-notes`, `api-preferences`, `learned-patterns`, etc.
|
|
102
|
+
|
|
103
|
+
### Memory Locations
|
|
104
|
+
|
|
105
|
+
- **Global blocks**: `~/.config/opencode/memory/*.md`
|
|
106
|
+
- **Project blocks**: `.opencode/memory/*.md` (auto-gitignored)
|
|
107
|
+
|
|
108
|
+
### Block Format
|
|
109
|
+
|
|
110
|
+
Each block is a markdown file with YAML frontmatter:
|
|
111
|
+
|
|
112
|
+
| Field | Type | Default | Description |
|
|
113
|
+
|-------|------|---------|-------------|
|
|
114
|
+
| `label` | string | filename | Unique identifier for the block |
|
|
115
|
+
| `description` | string | generic | Tells the agent how to use this block |
|
|
116
|
+
| `limit` | integer | 5000 | Maximum characters allowed |
|
|
117
|
+
| `read_only` | boolean | false | Prevent agent from modifying |
|
|
118
|
+
|
|
119
|
+
All fields have defaults for graceful degradation, but `description` is essential - without it, the agent gets a generic fallback and won't know how to use the block effectively. See Letta's docs on [the importance of the description field](https://docs.letta.com/guides/agents/memory-blocks/#the-importance-of-the-description-field).
|
|
120
|
+
|
|
121
|
+
### Journal Configuration
|
|
122
|
+
|
|
123
|
+
The journal is opt-in. Enable it in `~/.config/opencode/agent-memory.json`:
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
{
|
|
127
|
+
"journal": {
|
|
128
|
+
"enabled": true
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
You can optionally suggest tags to guide the agent's classification:
|
|
134
|
+
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"journal": {
|
|
138
|
+
"enabled": true,
|
|
139
|
+
"tags": [
|
|
140
|
+
{ "name": "perf", "description": "Performance optimization work" },
|
|
141
|
+
{ "name": "debugging", "description": "Debugging sessions and findings" }
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Tags are free-form strings - the agent can use any tag, not just the suggested ones. Suggested tags appear in the system prompt to provide guidance.
|
|
148
|
+
|
|
149
|
+
## Inspiration
|
|
150
|
+
|
|
151
|
+
The memory architecture and philosophical framing are adapted from [Letta](https://github.com/letta-ai/letta) (formerly MemGPT), a framework for building LLM agents with editable long-term memory.
|
|
152
|
+
|
|
153
|
+
Also worth exploring: [private-journal-mcp](https://github.com/obra/private-journal-mcp) by Jesse Vincent, which gives Claude a private journaling capability to process feelings and thoughts. His [blog post](https://blog.fsck.com/2025/05/28/dear-diary-the-user-asked-me-if-im-alive/) about it explores similar territory around AI self-reflection and persistent inner experience.
|
|
154
|
+
|
|
155
|
+
## Contributing
|
|
156
|
+
|
|
157
|
+
Contributions are welcome! Here's how to set up for development:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
git clone https://github.com/joshuadavidthomas/opencode-agent-memory
|
|
161
|
+
cd opencode-agent-memory
|
|
162
|
+
bun install
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Then symlink the plugin to your OpenCode config:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
mkdir -p ~/.config/opencode/plugin
|
|
169
|
+
ln -sf "$(pwd)/src/plugin.ts" ~/.config/opencode/plugin/memory.ts
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
opencode-agent-memory is licensed under the MIT license. See the [`LICENSE`](LICENSE) file for more information.
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
opencode-agent-memory is not built by, or affiliated with, the OpenCode team.
|
|
179
|
+
|
|
180
|
+
OpenCode is ©2025 Anomaly.
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ghilteras/opencode-agent-memory",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Letta-style editable memory blocks + journal for OpenCode (maintained fork of opencode-agent-memory).",
|
|
5
|
+
"author": "Angelo Pantano <ghilteras@protonmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Ghilteras/opencode-agent-memory.git"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"main": "src/plugin.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"files": [
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"typecheck": "tsc --noEmit"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@huggingface/transformers": "^3.8.1",
|
|
25
|
+
"@opencode-ai/plugin": "^1.0.115",
|
|
26
|
+
"js-yaml": "^4.1.0",
|
|
27
|
+
"zod": "^4.1.13"
|
|
28
|
+
},
|
|
29
|
+
"overrides": {
|
|
30
|
+
"onnxruntime-node": "1.20.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@tsconfig/bun": "^1.0.10",
|
|
34
|
+
"@types/bun": "^1.3.3",
|
|
35
|
+
"@types/js-yaml": "^4.0.9",
|
|
36
|
+
"@types/node": "^24.10.1",
|
|
37
|
+
"typescript": "^5.9.3"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"bun": ">=1.0.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const MODEL_NAME = "Xenova/all-MiniLM-L6-v2";
|
|
2
|
+
const MODEL_DTYPE = "q8";
|
|
3
|
+
|
|
4
|
+
let pipelinePromise: Promise<any> | undefined;
|
|
5
|
+
|
|
6
|
+
async function getPipeline() {
|
|
7
|
+
if (!pipelinePromise) {
|
|
8
|
+
pipelinePromise = (async () => {
|
|
9
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
10
|
+
return pipeline("feature-extraction", MODEL_NAME, {
|
|
11
|
+
dtype: MODEL_DTYPE,
|
|
12
|
+
});
|
|
13
|
+
})();
|
|
14
|
+
}
|
|
15
|
+
return pipelinePromise;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function generateEmbedding(text: string): Promise<number[]> {
|
|
19
|
+
const pipe = await getPipeline();
|
|
20
|
+
const output = await pipe(text, { pooling: "mean", normalize: true });
|
|
21
|
+
return Array.from(output.data as Float32Array) as number[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function cosineSimilarity(a: number[], b: number[]): number {
|
|
25
|
+
if (a.length !== b.length) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Embedding dimension mismatch: ${a.length} vs ${b.length}`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let dotProduct = 0;
|
|
32
|
+
let normA = 0;
|
|
33
|
+
let normB = 0;
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < a.length; i++) {
|
|
36
|
+
const ai = a[i]!;
|
|
37
|
+
const bi = b[i]!;
|
|
38
|
+
dotProduct += ai * bi;
|
|
39
|
+
normA += ai * ai;
|
|
40
|
+
normB += bi * bi;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const denominator = Math.sqrt(normA) * Math.sqrt(normB);
|
|
44
|
+
if (denominator === 0) return 0;
|
|
45
|
+
return dotProduct / denominator;
|
|
46
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
|
|
6
|
+
export function splitFrontmatter(text: string): {
|
|
7
|
+
frontmatterText: string | undefined;
|
|
8
|
+
body: string;
|
|
9
|
+
} {
|
|
10
|
+
if (!text.startsWith("---\n")) {
|
|
11
|
+
return { frontmatterText: undefined, body: text };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const endIndex = text.indexOf("\n---\n", 4);
|
|
15
|
+
if (endIndex === -1) {
|
|
16
|
+
return { frontmatterText: undefined, body: text };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const frontmatterText = text.slice(4, endIndex);
|
|
20
|
+
const body = text.slice(endIndex + "\n---\n".length);
|
|
21
|
+
return { frontmatterText, body };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function buildFrontmatterDocument(
|
|
25
|
+
frontmatter: Record<string, unknown>,
|
|
26
|
+
body: string,
|
|
27
|
+
): string {
|
|
28
|
+
const frontmatterYaml = yaml.dump(frontmatter, {
|
|
29
|
+
lineWidth: 120,
|
|
30
|
+
noRefs: true,
|
|
31
|
+
sortKeys: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return `---\n${frontmatterYaml}---\n${body.trim()}\n`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function atomicWriteFile(
|
|
38
|
+
filePath: string,
|
|
39
|
+
content: string,
|
|
40
|
+
): Promise<void> {
|
|
41
|
+
const tempPath = path.join(
|
|
42
|
+
path.dirname(filePath),
|
|
43
|
+
`.${path.basename(filePath)}.tmp`,
|
|
44
|
+
);
|
|
45
|
+
await fs.writeFile(tempPath, content, "utf-8");
|
|
46
|
+
await fs.rename(tempPath, filePath);
|
|
47
|
+
}
|