Repository navigation
Expand file tree
/
Copy pathcommand.ts
More file actions
119 lines (113 loc) · 4.59 KB
/
Copy pathcommand.ts
File metadata and controls
119 lines (113 loc) · 4.59 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
export * as CommandPlugin from "./command.js"
import { define } from "@opencode/plugin/effect/plugin"
import { Effect, Stream } from "effect"
import { Bus } from "../bus.js"
import { Command } from "../command.js"
import { Location } from "../location.js"
import { Mcp } from "../mcp/index.js"
import PROMPT_INITIALIZE from "./command/initialize.txt"
import PROMPT_REVIEW from "./command/review.txt"
export const Plugin = define({
id: "opencode.command",
effect: Effect.fn(function* (ctx) {
const location = yield* Location.Service
const mcp = yield* Mcp.Service
const bus = yield* Bus.Service
const loaded = { prompts: [] as Mcp.Prompt[] }
yield* bus.subscribe(Mcp.PromptsChanged).pipe(
Stream.runForEach(() =>
mcp.prompts().pipe(
Effect.tap((prompts) => Effect.sync(() => (loaded.prompts = prompts))),
Effect.andThen(ctx.command.reload()),
),
),
Effect.forkScoped({ startImmediately: true }),
)
loaded.prompts = yield* mcp.prompts()
yield* ctx.command.transform((editor) => {
for (const [name, description, template] of [
["init", "guided AGENTS.md setup", PROMPT_INITIALIZE],
["review", "review changes [commit|branch|pr], defaults to uncommitted", PROMPT_REVIEW],
] as const) {
editor.add({
name,
description,
execute: (input) =>
ctx.session
.prompt({
...input.prompt,
sessionID: input.sessionID,
text: append(template.replace("${path}", location.project.directory), input.prompt.text),
delivery: input.delivery,
})
.pipe(Effect.asVoid),
})
}
for (const prompt of loaded.prompts) {
const name = mcpCommandName(prompt.server, prompt.name)
const declared = prompt.arguments ?? []
editor.add({
name,
description: prompt.description,
execute: (input) =>
Effect.gen(function* () {
const args = Command.parseArguments(input.prompt.text)
const missing = declared.filter((argument, index) => argument.required && args[index] === undefined)
if (missing.length > 0)
return yield* Effect.fail(
new Error(
`/${name} needs ${missing.map((argument) => argument.name).join(", ")}. Usage: /${name} ${declared
.map((argument) => (argument.required ? `<${argument.name}>` : `[${argument.name}]`))
.join(" ")}`,
),
)
const result = yield* mcp
.prompt({
server: prompt.server,
name: prompt.name,
// Optional arguments the user left out are omitted, not sent as empty strings.
args: Object.fromEntries(
declared.flatMap((argument, index) => {
const value = args[index]
return value === undefined ? [] : [[argument.name, value]]
}),
),
})
.pipe(
Effect.mapError(
(error) => new Error(`MCP prompt ${prompt.server}:${prompt.name} failed: ${error.message}`),
),
)
if (!result) return yield* Effect.fail(new Error(`MCP prompt not found: ${prompt.server}:${prompt.name}`))
yield* ctx.session.prompt({
...input.prompt,
sessionID: input.sessionID,
text: result.messages
.map((message) => promptMessageText(message.content))
.join("\n")
.trim(),
delivery: input.delivery,
})
}).pipe(Effect.asVoid),
})
}
})
}),
})
function append(template: string, input: string) {
const value = input.trim()
if (template.includes("$ARGUMENTS")) return template.replaceAll("$ARGUMENTS", () => value)
return [template, value].filter(Boolean).join("\n\n")
}
function promptMessageText(content: Mcp.PromptResult["messages"][number]["content"]) {
if (content.type === "text") return content.text
// An embedded text resource is part of the prompt; binary resources and media have no text to send.
if (content.type === "resource" && "text" in content.resource) return content.resource.text
return ""
}
function mcpCommandName(server: string, prompt: string) {
return `${sanitize(server)}:${sanitize(prompt)}`
}
function sanitize(value: string) {
return value.replace(/[^a-zA-Z0-9_-]/g, "_")
}