-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.ts
More file actions
267 lines (245 loc) · 9.6 KB
/
Copy pathindex.ts
File metadata and controls
267 lines (245 loc) · 9.6 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
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"
import { fileURLToPath } from "node:url"
import { resolveEchoesWorkspace, runEchoes } from "./runtime.ts"
const PLUGIN_ROOT = path.dirname(fileURLToPath(import.meta.url))
const parseCommandFrontmatter = (
command: string,
): { template: string; description?: string; agent?: string } => {
const match = command.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/)
if (!match) return { template: command }
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 displayOutput = (output: string): string => output.trimEnd()
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, worktree }) => {
const readPromptFile = async (relativePath: string): Promise<string> =>
await fs.readFile(path.join(PLUGIN_ROOT, "prompts", relativePath), "utf-8")
const commands: Record<string, string> = {
"echoes-init.md": await readPromptFile("commands/echoes-init.md"),
"echoes-start.md": await readPromptFile("commands/echoes-start.md"),
"echoes-end.md": await readPromptFile("commands/echoes-end.md"),
"echoes-status.md": await readPromptFile("commands/echoes-status.md"),
}
// Lifecycle tools are one-shot capabilities granted only by the matching explicit slash command.
const authorizedStatusActions = new Map<string, StatusAction>()
const workspaceFor = (contextDirectory?: string, contextWorktree?: string): string =>
resolveEchoesWorkspace(contextDirectory, contextWorktree, directory, worktree)
const requireAuthorization = (sessionID: string, expected: StatusAction): void => {
if (authorizedStatusActions.get(sessionID) !== expected) {
throw new Error(
`EchoesVault ${expected} requires the explicit /echoes-${expected} command from the user.`,
)
}
}
return {
config: async (input) => {
input.command ||= {}
for (const [name, command] of Object.entries(commands)) {
const { template, description, agent } = parseCommandFrontmatter(command)
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: {
echoes_activate_vault: tool({
description:
"Initialize or explicitly migrate EchoesVault through the shared portable runtime. Available only after /echoes-init.",
args: {},
async execute(_args, ctx) {
requireAuthorization(ctx.sessionID, "init")
const output = displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "init", {
signal: ctx.abort,
}),
)
authorizedStatusActions.delete(ctx.sessionID)
return output
},
}),
echoes_start_session: tool({
description:
"Restore the generated index and three most recent EchoesVault session entries. Available only after /echoes-start.",
args: {},
async execute(_args, ctx) {
requireAuthorization(ctx.sessionID, "start")
const output = displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "start", {
args: ["--recent", "3"],
signal: ctx.abort,
}),
)
authorizedStatusActions.delete(ctx.sessionID)
return output
},
}),
echoes_vault_status: tool({
description:
"Inspect EchoesVault protocol, storage, metadata, index, Git readiness, conflicts, and scale without modifying files.",
args: {},
async execute(_args, ctx) {
return displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "status", {
args: ["--format", "card"],
signal: ctx.abort,
}),
)
},
}),
commit_memory_to_echoes_vault: tool({
description:
"Finalize an EchoesVault session with one daily summary and optional curated pages. Available only after explicit /echoes-end.",
args: {
dailySummary: tool.schema
.string()
.min(1)
.describe("Dense final outcomes, unresolved blockers, and next steps; never a transcript."),
pages: tool.schema
.array(
tool.schema.object({
filename: tool.schema.string().min(1).describe("Safe page filename ending in .md."),
content: tool.schema
.string()
.min(1)
.describe("Complete page with type, stack, status, and summary frontmatter."),
expectedSha256: tool.schema
.string()
.optional()
.describe("Fresh current hash, required when replacing an existing page."),
}),
)
.optional(),
},
async execute(args, ctx) {
requireAuthorization(ctx.sessionID, "end")
const output = displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "end", {
args: ["--confirm-explicit-user-end", "--payload", "-"],
payload: {
dailySummary: args.dailySummary,
pages: args.pages ?? [],
},
signal: ctx.abort,
}),
)
authorizedStatusActions.delete(ctx.sessionID)
return output
},
}),
echoes_append_to_daily_log: tool({
description:
"Write one intermediate technical milestone to a unique EchoesVault daily entry without ending the session.",
args: {
logEntry: tool.schema
.string()
.min(1)
.describe("Concise Markdown facts, decisions, blockers, or next steps."),
},
async execute(args, ctx) {
return displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "append", {
args: ["--payload", "-"],
payload: { entry: args.logEntry },
signal: ctx.abort,
}),
)
},
}),
echoes_search_vault_pages: tool({
description:
"Search EchoesVault knowledge pages with a narrow keyword or phrase before reading a relevant page.",
args: {
query: tool.schema.string().min(1).describe("Specific keyword or short phrase."),
limit: tool.schema.number().int().min(1).max(500).optional(),
},
async execute(args, ctx) {
const runtimeArgs = [args.query]
if (args.limit !== undefined) runtimeArgs.push("--limit", String(args.limit))
return displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "search", {
args: runtimeArgs,
signal: ctx.abort,
}),
)
},
}),
echoes_hash_vault_page: tool({
description:
"Calculate the current SHA-256 of an EchoesVault page before replacing that existing page.",
args: {
filename: tool.schema.string().min(1).describe("Existing page filename."),
},
async execute(args, ctx) {
return displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "hash", {
args: [args.filename],
signal: ctx.abort,
}),
)
},
}),
echoes_create_or_update_page: tool({
description:
"Create a validated EchoesVault page or replace one using its fresh expected SHA-256; the runtime regenerates the index.",
args: {
filename: tool.schema.string().min(1).describe("Exact page filename without paths."),
content: tool.schema
.string()
.min(1)
.describe("Complete Markdown page with all required frontmatter."),
expectedSha256: tool.schema
.string()
.optional()
.describe("Fresh current hash, required when replacing an existing page."),
},
async execute(args, ctx) {
return displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "upsert", {
args: ["--payload", "-"],
payload: {
filename: args.filename,
content: args.content,
...(args.expectedSha256 ? { expectedSha256: args.expectedSha256 } : {}),
},
signal: ctx.abort,
}),
)
},
}),
echoes_hydrate_vault: tool({
description:
"Rebuild only ignored local EchoesVault index/state files for an already initialized checkout.",
args: {},
async execute(_args, ctx) {
return displayOutput(
await runEchoes(workspaceFor(ctx.directory, ctx.worktree), "hydrate", {
signal: ctx.abort,
}),
)
},
}),
},
}
}
export default { server: OpenCodeEchoes }
export { OpenCodeEchoes }