+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
178 lines
5.5 KiB
TypeScript
178 lines
5.5 KiB
TypeScript
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import path from "path"
|
|
import { InstanceState } from "@/effect/instance-state"
|
|
import { EffectBridge } from "@/effect/bridge"
|
|
import type { InstanceContext } from "@/project/instance-context"
|
|
import { Effect, Layer, Context, Schema } from "effect"
|
|
import { Config } from "@/config/config"
|
|
import { MCP } from "../mcp"
|
|
import { Skill } from "../skill"
|
|
import PROMPT_INITIALIZE from "./template/initialize.txt"
|
|
import PROMPT_REVIEW from "./template/review.txt"
|
|
import { LegacyEvent } from "@opencode-ai/schema/legacy-event"
|
|
|
|
type State = {
|
|
commands: Record<string, Info>
|
|
}
|
|
|
|
export const Event = {
|
|
Executed: LegacyEvent.CommandExecuted,
|
|
}
|
|
|
|
export const Info = Schema.Struct({
|
|
name: Schema.String,
|
|
description: Schema.optional(Schema.String),
|
|
agent: Schema.optional(Schema.String),
|
|
model: Schema.optional(Schema.String),
|
|
source: Schema.optional(Schema.Literals(["command", "mcp", "skill"])),
|
|
// Some command templates are lazy promises from MCP prompt resolution.
|
|
template: Schema.Unknown,
|
|
subtask: Schema.optional(Schema.Boolean),
|
|
hints: Schema.Array(Schema.String),
|
|
}).annotate({ identifier: "Command" })
|
|
|
|
export type Info = Omit<Schema.Schema.Type<typeof Info>, "template"> & { template: Promise<string> | string }
|
|
|
|
export function hints(template: string) {
|
|
const result: string[] = []
|
|
const numbered = template.match(/\$\d+/g)
|
|
if (numbered) {
|
|
for (const match of [...new Set(numbered)].sort()) result.push(match)
|
|
}
|
|
if (template.includes("$ARGUMENTS")) result.push("$ARGUMENTS")
|
|
return result
|
|
}
|
|
|
|
export const Default = {
|
|
INIT: "init",
|
|
REVIEW: "review",
|
|
} as const
|
|
|
|
export interface Interface {
|
|
readonly get: (name: string) => Effect.Effect<Info | undefined>
|
|
readonly list: () => Effect.Effect<Info[]>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/Command") {}
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const config = yield* Config.Service
|
|
const mcp = yield* MCP.Service
|
|
const skill = yield* Skill.Service
|
|
|
|
const init = Effect.fn("Command.state")(function* (ctx: InstanceContext) {
|
|
const cfg = yield* config.get()
|
|
const bridge = yield* EffectBridge.make()
|
|
const commands: Record<string, Info> = {}
|
|
|
|
commands[Default.INIT] = {
|
|
name: Default.INIT,
|
|
description: "guided AGENTS.md setup",
|
|
source: "command",
|
|
get template() {
|
|
return PROMPT_INITIALIZE.replace("${path}", ctx.worktree)
|
|
},
|
|
hints: hints(PROMPT_INITIALIZE),
|
|
}
|
|
commands[Default.REVIEW] = {
|
|
name: Default.REVIEW,
|
|
description: "review changes [commit|branch|pr], defaults to uncommitted",
|
|
source: "command",
|
|
get template() {
|
|
return PROMPT_REVIEW.replace("${path}", ctx.worktree)
|
|
},
|
|
subtask: true,
|
|
hints: hints(PROMPT_REVIEW),
|
|
}
|
|
|
|
for (const [name, command] of Object.entries(cfg.command ?? {})) {
|
|
commands[name] = {
|
|
name,
|
|
agent: command.agent,
|
|
model: command.model,
|
|
description: command.description,
|
|
source: "command",
|
|
get template() {
|
|
return command.template
|
|
},
|
|
subtask: command.subtask,
|
|
hints: hints(command.template),
|
|
}
|
|
}
|
|
|
|
for (const [name, prompt] of Object.entries(yield* mcp.prompts())) {
|
|
commands[name] = {
|
|
name,
|
|
source: "mcp",
|
|
description: prompt.description,
|
|
get template() {
|
|
return bridge.promise(
|
|
mcp
|
|
.getPrompt(
|
|
prompt.client,
|
|
prompt.name,
|
|
prompt.arguments
|
|
? Object.fromEntries(prompt.arguments.map((argument, i) => [argument.name, `$${i + 1}`]))
|
|
: {},
|
|
)
|
|
.pipe(
|
|
Effect.map(
|
|
(template) =>
|
|
template?.messages
|
|
.map((message) => (message.content.type === "text" ? message.content.text : ""))
|
|
.join("\n") || "",
|
|
),
|
|
),
|
|
)
|
|
},
|
|
hints: prompt.arguments?.map((_, i) => `$${i + 1}`) ?? [],
|
|
}
|
|
}
|
|
|
|
for (const item of yield* skill.all()) {
|
|
if (commands[item.name]) continue
|
|
const dir = item.location === "<built-in>" ? undefined : path.dirname(item.location)
|
|
commands[item.name] = {
|
|
name: item.name,
|
|
description: item.description,
|
|
source: "skill",
|
|
get template() {
|
|
if (!dir) return item.content
|
|
return [
|
|
item.content,
|
|
"",
|
|
`Base directory for this skill: ${dir}`,
|
|
"Relative paths in this skill (e.g., scripts/, references/) are relative to this base directory.",
|
|
].join("\n")
|
|
},
|
|
hints: [],
|
|
}
|
|
}
|
|
|
|
return {
|
|
commands,
|
|
}
|
|
})
|
|
|
|
const state = yield* InstanceState.make<State>((ctx) => init(ctx))
|
|
|
|
const get = Effect.fn("Command.get")(function* (name: string) {
|
|
const s = yield* InstanceState.get(state)
|
|
return s.commands[name]
|
|
})
|
|
|
|
const list = Effect.fn("Command.list")(function* () {
|
|
const s = yield* InstanceState.get(state)
|
|
return Object.values(s.commands)
|
|
})
|
|
|
|
return Service.of({ get, list })
|
|
}),
|
|
)
|
|
|
|
export const node = LayerNode.make({ service: Service, layer: layer, deps: [Config.node, MCP.node, Skill.node] })
|
|
|
|
export * as Command from "."
|