diff --git a/packages/opencode/src/cli/cmd/agent.ts b/packages/opencode/src/cli/cmd/agent.ts index a774c6d026b..dcdbdcf10bb 100644 --- a/packages/opencode/src/cli/cmd/agent.ts +++ b/packages/opencode/src/cli/cmd/agent.ts @@ -11,7 +11,58 @@ import { EOL } from "os" const AgentCreateCommand = cmd({ command: "create", describe: "create a new agent", - async handler() { + builder: (yargs) => + yargs + .option("path", { + type: "string", + describe: "directory path to generate the agent file", + }) + .option("description", { + type: "string", + describe: "what the agent should do", + }) + .option("mode", { + type: "string", + describe: "agent mode", + choices: ["all", "primary", "subagent"] as const, + }), + async handler(args) { + const cliPath = args.path as string | undefined + const cliDescription = args.description as string | undefined + const cliMode = args.mode as "all" | "primary" | "subagent" | undefined + + const isNonInteractive = cliPath && cliDescription && cliMode + + if (isNonInteractive) { + await Instance.provide({ + directory: process.cwd(), + async fn() { + const generated = await Agent.generate({ description: cliDescription }).catch((error) => { + console.error(`Error: LLM failed to generate agent: ${error.message}`) + process.exit(1) + }) + + const frontmatter: any = { + description: generated.whenToUse, + mode: cliMode, + } + + const content = matter.stringify(generated.systemPrompt, frontmatter) + const agentDir = path.join(cliPath, "agent") + const filePath = path.join(agentDir, `${generated.identifier}.md`) + + const file = Bun.file(filePath) + if (await file.exists()) { + console.error(`Error: Agent file already exists: ${filePath}`) + process.exit(1) + } + + await Bun.write(filePath, content) + console.log(filePath) + }, + }) + return + } await Instance.provide({ directory: process.cwd(), async fn() {