@operad-io/memory 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +16 -2
- package/memory.schema.json +17 -0
- package/package.json +23 -4
- package/skills/memory/SKILL.md +84 -0
- package/src/commit.ts +279 -0
- package/src/index.ts +138 -0
- package/src/link-report.ts +224 -0
- package/src/links.ts +378 -0
- package/src/memory.ts +215 -0
- package/src/search/backend.ts +489 -0
- package/src/search/chunks.ts +86 -0
- package/src/search/command.ts +216 -0
- package/src/search/frontmatter.ts +114 -0
- package/src/search/service.ts +580 -0
- package/src/search/sources.ts +212 -0
- package/src/search/types.ts +245 -0
- package/src/tidy.ts +342 -0
- package/src/wiki.ts +619 -0
package/src/memory.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The memory plugin's settings, the wiki directory and the git repositories the
|
|
3
|
+
* bot's memory and the wiki are kept in. The wiki is a Markdown directory every
|
|
4
|
+
* bot shares; a bot's memory is `memory/` inside its own working directory.
|
|
5
|
+
* Both are used as they are: a directory that is already a git repository keeps
|
|
6
|
+
* its history and layout, and only a directory that is no repository gets one.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { homedir } from "node:os";
|
|
11
|
+
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { configPath, isRecord, prepareSettings, type ActionArgsSchema, type JsonValue } from "@operad-io/engine";
|
|
14
|
+
|
|
15
|
+
/** The plugin name. */
|
|
16
|
+
export const MEMORY_PLUGIN = "memory";
|
|
17
|
+
|
|
18
|
+
/** The step a chat calls after a reply, when the memory plugin is loaded. */
|
|
19
|
+
export const MEMORY_COMMIT_STEP = "memory/commit";
|
|
20
|
+
|
|
21
|
+
/** The `memory` skill, mounted for every bot while the plugin is enabled. */
|
|
22
|
+
export const MEMORY_SKILL_DIR = fileURLToPath(new URL("../skills/memory", import.meta.url));
|
|
23
|
+
|
|
24
|
+
/** The most of the memory index a reply carries; the tidy step keeps the file below it. */
|
|
25
|
+
export const MEMORY_INDEX_MAX = 8000;
|
|
26
|
+
|
|
27
|
+
/** The wiki directory inside the operad home when the setting is not written. */
|
|
28
|
+
export const DEFAULT_WIKI_NAME = "wiki";
|
|
29
|
+
|
|
30
|
+
/** The JSON Schema of the `memory` section (also shipped as `memory.schema.json`). */
|
|
31
|
+
export const MEMORY_SETTINGS: ActionArgsSchema = {
|
|
32
|
+
type: "object",
|
|
33
|
+
properties: {
|
|
34
|
+
wikiDir: {
|
|
35
|
+
type: "string",
|
|
36
|
+
title: "Wiki directory",
|
|
37
|
+
minLength: 1,
|
|
38
|
+
editable: true,
|
|
39
|
+
default: "wiki",
|
|
40
|
+
description:
|
|
41
|
+
"The shared knowledge base: a Markdown directory every bot reads, and the bots whose access profile allows it write. Created when missing and made a git repository when it is not one; a directory that is already a git repository is used as it is, never re-initialised or restructured. `~` is your home directory and a relative path is under the operad home. Default: `wiki` in the operad home, so ~/.operad/wiki for the usual home; a person may point it at a notes library such as an Obsidian vault.",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
additionalProperties: false,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A `memory` section that cannot be used; the message says why. */
|
|
48
|
+
export class MemoryError extends Error {
|
|
49
|
+
constructor(message: string) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.name = "MemoryError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A path as the memory plugin reads it: `~`, an absolute path, or one under the operad home. */
|
|
56
|
+
export function expandMemoryPath(path: string, home: string, userHome: string = homedir()): string {
|
|
57
|
+
if (path === "~") return userHome;
|
|
58
|
+
if (path.startsWith("~/")) return join(userHome, path.slice(2));
|
|
59
|
+
return isAbsolute(path) ? resolve(path) : resolve(home, path);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The wiki directory a setting names, with its default. */
|
|
63
|
+
export function wikiDirectory(
|
|
64
|
+
home: string,
|
|
65
|
+
settings: Readonly<Record<string, JsonValue>> = {},
|
|
66
|
+
userHome: string = homedir(),
|
|
67
|
+
): string {
|
|
68
|
+
const written = settings["wikiDir"];
|
|
69
|
+
const path = typeof written === "string" && written.length > 0 ? written : join(home, DEFAULT_WIKI_NAME);
|
|
70
|
+
return expandMemoryPath(path, home, userHome);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The `plugins.memory` entry of `config.json`: whether it is on and what it writes. */
|
|
74
|
+
function memoryEntry(home: string): { readonly enabled: boolean; readonly settings: unknown } | undefined {
|
|
75
|
+
const path = configPath(home);
|
|
76
|
+
if (!existsSync(path)) return undefined;
|
|
77
|
+
let parsed: unknown;
|
|
78
|
+
try {
|
|
79
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
80
|
+
} catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const plugins = isRecord(parsed) ? parsed.plugins : undefined;
|
|
84
|
+
const entry = isRecord(plugins) ? plugins[MEMORY_PLUGIN] : undefined;
|
|
85
|
+
// A plugin may be enabled by its module name alone.
|
|
86
|
+
if (typeof entry === "string") return entry.length > 0 ? { enabled: true, settings: undefined } : undefined;
|
|
87
|
+
if (!isRecord(entry)) return undefined;
|
|
88
|
+
return { enabled: entry.enabled !== false, settings: entry.settings };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Whether the home configures the memory plugin and has not disabled it. */
|
|
92
|
+
export function memoryEnabled(home: string): boolean {
|
|
93
|
+
return memoryEntry(home)?.enabled === true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The wiki directory the home's `config.json` names now, without a plugin
|
|
98
|
+
* context: what a bot reads to decide how the wiki is reached.
|
|
99
|
+
*/
|
|
100
|
+
export function wikiDirectoryOfHome(home: string, userHome: string = homedir()): string {
|
|
101
|
+
const section = memoryEntry(home)?.settings;
|
|
102
|
+
try {
|
|
103
|
+
const settings = prepareSettings(MEMORY_SETTINGS, section, MEMORY_PLUGIN);
|
|
104
|
+
return wikiDirectory(home, settings, userHome);
|
|
105
|
+
} catch {
|
|
106
|
+
return wikiDirectory(home, {}, userHome);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** One git run. */
|
|
111
|
+
export interface GitResult {
|
|
112
|
+
readonly ok: boolean;
|
|
113
|
+
readonly out: string;
|
|
114
|
+
readonly err: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Runs git without a shell; output is trimmed. */
|
|
118
|
+
export function memoryGit(cwd: string, args: readonly string[]): GitResult {
|
|
119
|
+
const result = Bun.spawnSync({
|
|
120
|
+
cmd: ["git", "-C", cwd, ...args],
|
|
121
|
+
stdin: "ignore",
|
|
122
|
+
stdout: "pipe",
|
|
123
|
+
stderr: "pipe",
|
|
124
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
125
|
+
timeout: 20_000,
|
|
126
|
+
killSignal: "SIGKILL",
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
ok: result.exitCode === 0,
|
|
130
|
+
out: result.stdout.toString().trim(),
|
|
131
|
+
err: result.stderr.toString().trim(),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A directory and the repository that holds it. */
|
|
136
|
+
export interface Repository {
|
|
137
|
+
/** The root of the git work tree. */
|
|
138
|
+
readonly root: string;
|
|
139
|
+
/** The directory given, which is the root when it was initialised here. */
|
|
140
|
+
readonly directory: string;
|
|
141
|
+
/** Whether this call made the repository. */
|
|
142
|
+
readonly created: boolean;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The repository a directory belongs to, made in the directory when it belongs
|
|
147
|
+
* to none. A directory inside someone else's repository is part of that
|
|
148
|
+
* repository and is used as it is: nothing is initialised again.
|
|
149
|
+
*/
|
|
150
|
+
export function ensureRepository(directory: string): Repository {
|
|
151
|
+
mkdirSync(directory, { recursive: true });
|
|
152
|
+
// git answers with a real path. Reading the directory through its real path
|
|
153
|
+
// too keeps the two comparable, so a repository that is this directory keeps
|
|
154
|
+
// the name the caller gave it.
|
|
155
|
+
const real = realpathSync(directory);
|
|
156
|
+
const found = memoryGit(real, ["rev-parse", "--show-toplevel"]);
|
|
157
|
+
if (found.ok && found.out.length > 0) {
|
|
158
|
+
const same = found.out === real;
|
|
159
|
+
return { root: same ? directory : found.out, directory: same ? directory : real, created: false };
|
|
160
|
+
}
|
|
161
|
+
const made = memoryGit(real, ["init", "--quiet"]);
|
|
162
|
+
if (!made.ok) {
|
|
163
|
+
throw new MemoryError(`git init in ${directory} failed: ${made.err || "git wrote nothing"}`);
|
|
164
|
+
}
|
|
165
|
+
return { root: directory, directory, created: true };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The wiki: created when missing, and a repository when it is not one. */
|
|
169
|
+
export function ensureWiki(
|
|
170
|
+
home: string,
|
|
171
|
+
settings: Readonly<Record<string, JsonValue>> = {},
|
|
172
|
+
): Repository & { readonly path: string } {
|
|
173
|
+
const path = wikiDirectory(home, settings);
|
|
174
|
+
return { ...ensureRepository(path), path };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The three layers of a bot's memory, made when missing; nothing is overwritten. */
|
|
178
|
+
export function ensureMemoryLayout(memoryDir: string): void {
|
|
179
|
+
mkdirSync(join(memoryDir, "log"), { recursive: true });
|
|
180
|
+
const index = join(memoryDir, "index.md");
|
|
181
|
+
if (!existsSync(index)) {
|
|
182
|
+
writeFileSync(index, "# Memory index\n\nOne line per entry. Keep it short: the detail belongs in a topic note.\n");
|
|
183
|
+
}
|
|
184
|
+
const instructions = join(memoryDir, "instructions.md");
|
|
185
|
+
if (!existsSync(instructions)) {
|
|
186
|
+
writeFileSync(instructions, "# Instructions\n\nA person's long-standing instructions to this bot.\n");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A file a bot's reply carries on its message; the chat plugin reads it and decides what to attach. */
|
|
191
|
+
export interface MemoryNote {
|
|
192
|
+
readonly name: string;
|
|
193
|
+
readonly path: string;
|
|
194
|
+
readonly maxChars?: number | undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The files a bot's reply carries as notes: its memory index, cut at
|
|
199
|
+
* {@link MEMORY_INDEX_MAX}, and the person's instructions. A file that is not
|
|
200
|
+
* there yet is left out; one created later is picked up by the next reply.
|
|
201
|
+
*/
|
|
202
|
+
export function memoryNotes(memoryDir: string): readonly MemoryNote[] {
|
|
203
|
+
const notes: MemoryNote[] = [];
|
|
204
|
+
const index = join(memoryDir, "index.md");
|
|
205
|
+
if (existsSync(index)) notes.push({ name: "memory/index.md", path: index, maxChars: MEMORY_INDEX_MAX });
|
|
206
|
+
const instructions = join(memoryDir, "instructions.md");
|
|
207
|
+
if (existsSync(instructions)) notes.push({ name: "memory/instructions.md", path: instructions });
|
|
208
|
+
return notes;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A path inside `root`, as git takes it; `.` for the root itself. */
|
|
212
|
+
export function pathIn(root: string, directory: string): string {
|
|
213
|
+
const rel = relative(root, directory);
|
|
214
|
+
return rel === "" ? "." : rel;
|
|
215
|
+
}
|