-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
519 lines (463 loc) · 18 KB
/
Copy pathindex.ts
File metadata and controls
519 lines (463 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import type { Plugin } from "@opencode-ai/plugin"
import { tool } from "@opencode-ai/plugin"
import * as fs from "node:fs/promises"
import * as path from "node:path"
const getDateStr = (): string => {
const d = new Date()
const year = d.getFullYear()
const month = String(d.getMonth() + 1).padStart(2, "0")
const day = String(d.getDate()).padStart(2, "0")
return `${year}-${month}-${day}`
}
type VaultPaths = {
vault: string
raw: string
pages: string
daily: string
assets: string
}
type VaultStats = {
totalPages: number
totalDailyLogs: number
deprecatedPages: number
}
type EchoesState = {
version: number
pluginVersion: string
initialized: boolean
session: {
started: boolean
saved: boolean
lastStart: string | null
lastSave: string | null
}
stats: VaultStats
}
const STATE_FILENAME = ".opencode/echoes-state.json"
const getPluginVersion = async (): Promise<string> => {
try {
const pkgPath = path.join(path.dirname(new URL(import.meta.url).pathname), "package.json")
const pkg = JSON.parse(await fs.readFile(pkgPath, "utf-8"))
return pkg.version || "0.0.0"
} catch {
return "0.0.0"
}
}
const defaultState = (): EchoesState => ({
version: 1,
pluginVersion: "0.0.0",
initialized: false,
session: {
started: false,
saved: false,
lastStart: null,
lastSave: null,
},
stats: {
totalPages: 0,
totalDailyLogs: 0,
deprecatedPages: 0,
},
})
const readState = async (directory: string): Promise<EchoesState> => {
try {
const raw = await fs.readFile(path.join(directory, STATE_FILENAME), "utf-8")
return JSON.parse(raw) as EchoesState
} catch {
return defaultState()
}
}
const writeState = async (directory: string, state: EchoesState): Promise<void> => {
const filePath = path.join(directory, STATE_FILENAME)
await fs.mkdir(path.dirname(filePath), { recursive: true })
await fs.writeFile(filePath, JSON.stringify(state, null, 2))
}
const resolveVaultPaths = (directory: string): VaultPaths => {
const vault = path.join(directory, "EchoesVault")
return {
vault,
raw: path.join(vault, "raw"),
pages: path.join(vault, "pages"),
daily: path.join(vault, "daily"),
assets: path.join(vault, "assets"),
}
}
const ensureVaultDirs = async (paths: VaultPaths): Promise<void> => {
await fs.mkdir(paths.raw, { recursive: true })
await fs.mkdir(paths.pages, { recursive: true })
await fs.mkdir(paths.daily, { recursive: true })
await fs.mkdir(paths.assets, { recursive: true })
}
const collectStats = async (vaultPaths: VaultPaths): Promise<VaultStats> => {
let totalPages = 0
let totalDailyLogs = 0
let deprecatedPages = 0
try {
const pageFiles = (await fs.readdir(vaultPaths.pages)).filter((f) => f.endsWith(".md"))
totalPages = pageFiles.length
for (const file of pageFiles) {
const content = await fs.readFile(path.join(vaultPaths.pages, file), "utf-8")
if (content.includes("DEPRECATED")) {
deprecatedPages++
}
}
} catch { /* pages dir may not exist yet */ }
try {
const dailyFiles = (await fs.readdir(vaultPaths.daily)).filter((f) => f.endsWith(".md"))
totalDailyLogs = dailyFiles.length
} catch { /* daily dir may not exist yet */ }
return { totalPages, totalDailyLogs, deprecatedPages }
}
const updateStats = async (directory: string, vaultPaths: VaultPaths): Promise<void> => {
const st = await readState(directory)
st.stats = await collectStats(vaultPaths)
await writeState(directory, st)
}
const DEFAULT_INDEX = `# EchoesVault Index
Welcome to the EchoesVault knowledge base.
This index tracks all structured pages in the vault.
`
const sanitizeFilename = (name: string): string => {
const cleaned = name.replace(/\.\./g, "").replace(/[\/\\]/g, "")
return cleaned || "untitled"
}
// Normalizes any user/LLM-supplied page name to a safe `*.md` filename,
// ensuring we never produce duplicates like `foo.md.md`.
const toPageFilename = (name: string): string => {
const safe = sanitizeFilename(name)
return safe.endsWith(".md") ? safe : `${safe}.md`
}
const toPageSlug = (filename: string): string => filename.replace(/\.md$/, "")
const ensureCommands = async (directory: string, commands: Record<string, string>): Promise<void> => {
const cmdDir = path.join(directory, ".opencode", "commands")
await fs.mkdir(cmdDir, { recursive: true })
for (const [name, content] of Object.entries(commands)) {
const cmdFile = path.join(cmdDir, name)
try {
await fs.access(cmdFile)
} catch {
await fs.writeFile(cmdFile, content)
}
}
}
const ensureSkills = async (directory: string, skills: Record<string, string>): Promise<void> => {
for (const [name, content] of Object.entries(skills)) {
const skillDir = path.join(directory, ".opencode", "skills", name)
const skillFile = path.join(skillDir, "SKILL.md")
await fs.mkdir(skillDir, { recursive: true })
try {
await fs.access(skillFile)
} catch {
await fs.writeFile(skillFile, content)
}
}
}
const parseCommandFrontmatter = (cmd: string): { template: string; description?: string; agent?: string } => {
const match = cmd.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/)
if (!match) return { template: cmd }
const frontmatter: Record<string, string> = {}
for (const line of match[1].split("\n")) {
const [key, ...rest] = line.split(":")
if (key && rest.length) frontmatter[key.trim()] = rest.join(":").trim()
}
return { template: match[2], description: frontmatter.description, agent: frontmatter.agent }
}
const statusActions = {
"echoes-init": "init",
"echoes-start": "start",
"echoes-end": "end",
} as const
type StatusAction = (typeof statusActions)[keyof typeof statusActions]
const OpenCodeEchoes: Plugin = async ({ directory }) => {
const pluginDir = path.dirname(new URL(import.meta.url).pathname)
const readPromptFile = async (relativePath: string): Promise<string> => {
return await fs.readFile(path.join(pluginDir, "prompts", relativePath), "utf-8")
}
const ECHOES_INIT = await readPromptFile("commands/echoes-init.md")
const ECHOES_START = await readPromptFile("commands/echoes-start.md")
const ECHOES_END = await readPromptFile("commands/echoes-end.md")
const ECHOES_STATUS = await readPromptFile("commands/echoes-status.md")
const APPEND_TO_DAILY_LOG = await readPromptFile("skills/echoes-append-to-daily-log.md")
const SEARCH_VAULT_PAGES = await readPromptFile("skills/echoes-search-vault-pages.md")
const CREATE_OR_UPDATE_PAGE = await readPromptFile("skills/echoes-create-or-update-page.md")
const commands: Record<string, string> = {
"echoes-init.md": ECHOES_INIT,
"echoes-start.md": ECHOES_START,
"echoes-end.md": ECHOES_END,
"echoes-status.md": ECHOES_STATUS,
}
const skills: Record<string, string> = {
"echoes-append-to-daily-log": APPEND_TO_DAILY_LOG,
"echoes-search-vault-pages": SEARCH_VAULT_PAGES,
"echoes-create-or-update-page": CREATE_OR_UPDATE_PAGE,
}
const paths = resolveVaultPaths(directory)
const indexFile = path.join(paths.vault, "index.md")
await ensureVaultDirs(paths)
try {
await fs.access(indexFile)
} catch {
await fs.writeFile(indexFile, DEFAULT_INDEX)
}
await ensureCommands(directory, commands)
await ensureSkills(directory, skills)
// Sidebar status can change only through the matching user-invoked command.
const authorizedStatusActions = new Map<string, StatusAction>()
const state = await readState(directory)
state.pluginVersion = await getPluginVersion()
state.stats = await collectStats(paths)
await writeState(directory, state)
return {
config: async (input) => {
input.command = input.command || {}
for (const [name, cmd] of Object.entries(commands)) {
const { template, description, agent } = parseCommandFrontmatter(cmd)
input.command[name.replace(".md", "")] = { template, description, agent }
}
},
"command.execute.before": async (input) => {
const command = input.command.replace(/^\//, "") as keyof typeof statusActions
const action = statusActions[command]
if (action) {
authorizedStatusActions.set(input.sessionID, action)
}
},
tool: {
commit_memory_to_echoes_vault: tool({
description:
"Save final session memory to EchoesVault. This succeeds only after the user explicitly runs /echoes-end; never call it merely because a task is complete.",
args: {
dailySummary: tool.schema
.string()
.describe(
"Detailed summary of the current session: what was accomplished, bugs discovered, where you stopped, and what remains to be done."
),
newPages: tool.schema
.array(
tool.schema.object({
filename: tool.schema
.string()
.describe("Filename without .md extension (e.g. 'architecture-decisions')"),
content: tool.schema
.string()
.describe("Full markdown content of the knowledge base page"),
})
)
.optional()
.describe("Array of new knowledge base pages to create in EchoesVault/pages/"),
indexAppends: tool.schema
.array(tool.schema.string())
.optional()
.describe("Lines to append to the end of EchoesVault/index.md"),
indexUpdates: tool.schema
.array(
tool.schema.object({
oldLine: tool.schema
.string()
.describe("The exact line to find and replace in the index"),
newLine: tool.schema
.string()
.describe("The replacement line"),
})
)
.optional()
.describe("Lines to find and replace in place within EchoesVault/index.md"),
},
async execute(args, ctx) {
if (authorizedStatusActions.get(ctx.sessionID) !== "end") {
return [
"Final memory was not saved.",
"Only an explicit user /echoes-end command can save final memory and set the status to Memory Saved.",
].join("\n")
}
const today = getDateStr()
await ensureVaultDirs(paths)
const dailyFile = path.join(paths.daily, `${today}.md`)
const timestamp = new Date().toISOString()
const header = `## Session — ${timestamp}\n\n`
await fs.appendFile(dailyFile, header + args.dailySummary + "\n\n")
let pagesCreated = 0
if (args.newPages && args.newPages.length > 0) {
for (const page of args.newPages) {
const fileName = toPageFilename(page.filename)
const pageFile = path.join(paths.pages, fileName)
await fs.writeFile(pageFile, page.content.trim() + "\n")
pagesCreated++
}
}
const idxFile = path.join(paths.vault, "index.md")
let indexContent = ""
try {
indexContent = await fs.readFile(idxFile, "utf-8")
} catch {
indexContent = DEFAULT_INDEX
}
if (args.indexUpdates && args.indexUpdates.length > 0) {
for (const upd of args.indexUpdates) {
if (indexContent.includes(upd.oldLine)) {
indexContent = indexContent.replaceAll(upd.oldLine, upd.newLine)
}
}
}
if (args.indexAppends && args.indexAppends.length > 0) {
const toAppend = args.indexAppends.join("\n")
indexContent = indexContent.trimEnd() + "\n" + toAppend + "\n"
}
await fs.writeFile(idxFile, indexContent)
const st = await readState(directory)
st.session.saved = true
st.session.lastSave = new Date().toISOString()
st.stats = await collectStats(paths)
await writeState(directory, st)
authorizedStatusActions.delete(ctx.sessionID)
return [
`✅ Memory committed to EchoesVault.`,
`- Daily log: EchoesVault/daily/${today}.md`,
`- Pages created: ${pagesCreated}`,
`- Index: updated`,
].join("\n")
},
}),
echoes_append_to_daily_log: tool({
description:
"Append an intermediate technical note or decision to today's daily log without ending the session.",
args: {
logEntry: tool.schema
.string()
.describe(
"Markdown-formatted bullet points to append. Do not include date/time — the system adds a timestamp automatically."
),
},
async execute(args, _ctx) {
const today = getDateStr()
await ensureVaultDirs(paths)
const dailyFile = path.join(paths.daily, `${today}.md`)
const timestamp = new Date().toISOString()
const entry = `### Scratchpad — ${timestamp}\n\n${args.logEntry}\n\n`
await fs.appendFile(dailyFile, entry)
await updateStats(directory, paths)
return `✅ Scratchpad note saved to EchoesVault/daily/${today}.md`
},
}),
echoes_search_vault_pages: tool({
description:
"Search the EchoesVault pages/ directory for specific concepts, keywords, or implementation details.",
args: {
query: tool.schema
.string()
.describe("Specific keyword or short phrase to search for across the pages/ directory."),
},
async execute(args, _ctx) {
await ensureVaultDirs(paths)
const results: string[] = []
try {
const files = (await fs.readdir(paths.pages)).filter((f) =>
f.endsWith(".md")
)
for (const file of files) {
const content = await fs.readFile(
path.join(paths.pages, file),
"utf-8"
)
const lines = content.split("\n")
for (let i = 0; i < lines.length; i++) {
if (
lines[i].toLowerCase().includes(args.query.toLowerCase())
) {
results.push(
`${file}:${i + 1}: ${lines[i].trim().slice(0, 200)}`
)
}
}
}
} catch {
return "_No pages found in EchoesVault/pages/_"
}
if (results.length === 0) {
return `No results found for "${args.query}" in EchoesVault/pages/.`
}
return results.join("\n")
},
}),
echoes_create_or_update_page: tool({
description:
"Atomically create a new markdown page or update an existing one in EchoesVault/pages/, automatically updating the index if the file is new.",
args: {
filename: tool.schema
.string()
.describe("Exact filename without paths (e.g. 'auth-architecture.md')."),
content: tool.schema
.string()
.describe("Full markdown content of the page, starting with YAML frontmatter."),
indexDescription: tool.schema
.string()
.optional()
.describe("One-sentence description for the index. Required for new files. Format: '- [[filename]]: description'."),
},
async execute(args, _ctx) {
await ensureVaultDirs(paths)
const fileName = toPageFilename(args.filename)
const pageFile = path.join(paths.pages, fileName)
const existed = await fs.access(pageFile).then(() => true).catch(() => false)
await fs.writeFile(pageFile, args.content.trim() + "\n")
if (!existed && args.indexDescription) {
const idxFile = path.join(paths.vault, "index.md")
let indexContent = ""
try {
indexContent = await fs.readFile(idxFile, "utf-8")
} catch {
indexContent = DEFAULT_INDEX
}
const link = `[[${toPageSlug(fileName)}]]`
if (!indexContent.includes(link)) {
indexContent = indexContent.trimEnd() + "\n" + args.indexDescription + "\n"
await fs.writeFile(idxFile, indexContent)
}
}
await updateStats(directory, paths)
const action = existed ? "updated" : "created"
const parts = [`✅ Page ${action}: EchoesVault/pages/${fileName}`]
if (!existed && args.indexDescription) {
parts.push(`📑 Index: synced`)
}
return parts.join("\n")
},
}),
echoes_activate_vault: tool({
description:
"Mark the EchoesVault as activated. This succeeds only after the user explicitly runs /echoes-init.",
args: {},
async execute(_args, ctx) {
if (authorizedStatusActions.get(ctx.sessionID) !== "init") {
return "Vault status was not changed. Only an explicit user /echoes-init command can activate EchoesVault."
}
const st = await readState(directory)
st.initialized = true
st.stats = await collectStats(paths)
await writeState(directory, st)
authorizedStatusActions.delete(ctx.sessionID)
return "EchoesVault activated."
},
}),
echoes_start_session: tool({
description:
"Mark the current EchoesVault session as started. This succeeds only after the user explicitly runs /echoes-start.",
args: {},
async execute(_args, ctx) {
if (authorizedStatusActions.get(ctx.sessionID) !== "start") {
return "Vault status was not changed. Only an explicit user /echoes-start command can start an EchoesVault session."
}
const st = await readState(directory)
st.session.started = true
st.session.saved = false
st.session.lastStart = new Date().toISOString()
await writeState(directory, st)
authorizedStatusActions.delete(ctx.sessionID)
return "EchoesVault session started."
},
}),
},
}
}
export default { server: OpenCodeEchoes }
export { OpenCodeEchoes }