@@ -4,131 +4,215 @@ import { UI } from "../ui" |
4 | 4 | import { Global } from "../../global" |
5 | 5 | import { Agent } from "../../agent/agent" |
6 | 6 | import path from "path" |
| 7 | +import fs from "fs/promises" |
7 | 8 | import matter from "gray-matter" |
8 | 9 | import { Instance } from "../../project/instance" |
9 | 10 | import { EOL } from "os" |
| 11 | +import type { Argv } from "yargs" |
| 12 | + |
| 13 | +type AgentMode = "all" | "primary" | "subagent" |
| 14 | + |
| 15 | +const AVAILABLE_TOOLS = [ |
| 16 | + "bash", |
| 17 | + "read", |
| 18 | + "write", |
| 19 | + "edit", |
| 20 | + "list", |
| 21 | + "glob", |
| 22 | + "grep", |
| 23 | + "webfetch", |
| 24 | + "task", |
| 25 | + "todowrite", |
| 26 | + "todoread", |
| 27 | +] |
10 | 28 |
|
11 | 29 | const AgentCreateCommand = cmd({ |
12 | 30 | command: "create", |
13 | 31 | describe: "create a new agent", |
14 | | - async handler() { |
| 32 | + builder: (yargs: Argv) => |
| 33 | + yargs |
| 34 | + .option("path", { |
| 35 | + type: "string", |
| 36 | + describe: "directory path to generate the agent file", |
| 37 | + }) |
| 38 | + .option("description", { |
| 39 | + type: "string", |
| 40 | + describe: "what the agent should do", |
| 41 | + }) |
| 42 | + .option("mode", { |
| 43 | + type: "string", |
| 44 | + describe: "agent mode", |
| 45 | + choices: ["all", "primary", "subagent"] as const, |
| 46 | + }) |
| 47 | + .option("tools", { |
| 48 | + type: "string", |
| 49 | + describe: `comma-separated list of tools to enable (default: all). Available: "${AVAILABLE_TOOLS.join(", ")}"`, |
| 50 | + }), |
| 51 | + async handler(args) { |
15 | 52 | await Instance.provide({ |
16 | 53 | directory: process.cwd(), |
17 | 54 | async fn() { |
18 | | - UI.empty() |
19 | | - prompts.intro("Create agent") |
| 55 | + const cliPath = args.path |
| 56 | + const cliDescription = args.description |
| 57 | + const cliMode = args.mode as AgentMode | undefined |
| 58 | + const cliTools = args.tools |
| 59 | + |
| 60 | + const isFullyNonInteractive = cliPath && cliDescription && cliMode && cliTools !== undefined |
| 61 | + |
| 62 | + if (!isFullyNonInteractive) { |
| 63 | + UI.empty() |
| 64 | + prompts.intro("Create agent") |
| 65 | + } |
| 66 | + |
20 | 67 | const project = Instance.project |
21 | 68 |
|
22 | | - let scope: "global" | "project" = "global" |
23 | | - if (project.vcs === "git") { |
24 | | - const scopeResult = await prompts.select({ |
25 | | - message: "Location", |
26 | | - options: [ |
27 | | - { |
28 | | - label: "Current project", |
29 | | - value: "project" as const, |
30 | | - hint: Instance.worktree, |
31 | | - }, |
32 | | - { |
33 | | - label: "Global", |
34 | | - value: "global" as const, |
35 | | - hint: Global.Path.config, |
36 | | - }, |
37 | | - ], |
38 | | - }) |
39 | | - if (prompts.isCancel(scopeResult)) throw new UI.CancelledError() |
40 | | - scope = scopeResult |
| 69 | + // Determine scope/path |
| 70 | + let targetPath: string |
| 71 | + if (cliPath) { |
| 72 | + targetPath = path.join(cliPath, "agent") |
| 73 | + } else { |
| 74 | + let scope: "global" | "project" = "global" |
| 75 | + if (project.vcs === "git") { |
| 76 | + const scopeResult = await prompts.select({ |
| 77 | + message: "Location", |
| 78 | + options: [ |
| 79 | + { |
| 80 | + label: "Current project", |
| 81 | + value: "project" as const, |
| 82 | + hint: Instance.worktree, |
| 83 | + }, |
| 84 | + { |
| 85 | + label: "Global", |
| 86 | + value: "global" as const, |
| 87 | + hint: Global.Path.config, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }) |
| 91 | + if (prompts.isCancel(scopeResult)) throw new UI.CancelledError() |
| 92 | + scope = scopeResult |
| 93 | + } |
| 94 | + targetPath = path.join( |
| 95 | + scope === "global" ? Global.Path.config : path.join(Instance.worktree, ".opencode"), |
| 96 | + "agent", |
| 97 | + ) |
41 | 98 | } |
42 | 99 |
|
43 | | - const query = await prompts.text({ |
44 | | - message: "Description", |
45 | | - placeholder: "What should this agent do?", |
46 | | - validate: (x) => (x && x.length > 0 ? undefined : "Required"), |
47 | | - }) |
48 | | - if (prompts.isCancel(query)) throw new UI.CancelledError() |
| 100 | + // Get description |
| 101 | + let description: string |
| 102 | + if (cliDescription) { |
| 103 | + description = cliDescription |
| 104 | + } else { |
| 105 | + const query = await prompts.text({ |
| 106 | + message: "Description", |
| 107 | + placeholder: "What should this agent do?", |
| 108 | + validate: (x) => (x && x.length > 0 ? undefined : "Required"), |
| 109 | + }) |
| 110 | + if (prompts.isCancel(query)) throw new UI.CancelledError() |
| 111 | + description = query |
| 112 | + } |
49 | 113 |
|
| 114 | + // Generate agent |
50 | 115 | const spinner = prompts.spinner() |
51 | | - |
52 | 116 | spinner.start("Generating agent configuration...") |
53 | | - const generated = await Agent.generate({ description: query }).catch((error) => { |
| 117 | + const generated = await Agent.generate({ description }).catch((error) => { |
54 | 118 | spinner.stop(`LLM failed to generate agent: ${error.message}`, 1) |
| 119 | + if (isFullyNonInteractive) process.exit(1) |
55 | 120 | throw new UI.CancelledError() |
56 | 121 | }) |
57 | 122 | spinner.stop(`Agent ${generated.identifier} generated`) |
58 | 123 |
|
59 | | - const availableTools = [ |
60 | | - "bash", |
61 | | - "read", |
62 | | - "write", |
63 | | - "edit", |
64 | | - "list", |
65 | | - "glob", |
66 | | - "grep", |
67 | | - "webfetch", |
68 | | - "task", |
69 | | - "todowrite", |
70 | | - "todoread", |
71 | | - ] |
72 | | - |
73 | | - const selectedTools = await prompts.multiselect({ |
74 | | - message: "Select tools to enable", |
75 | | - options: availableTools.map((tool) => ({ |
76 | | - label: tool, |
77 | | - value: tool, |
78 | | - })), |
79 | | - initialValues: availableTools, |
80 | | - }) |
81 | | - if (prompts.isCancel(selectedTools)) throw new UI.CancelledError() |
82 | | - |
83 | | - const modeResult = await prompts.select({ |
84 | | - message: "Agent mode", |
85 | | - options: [ |
86 | | - { |
87 | | - label: "All", |
88 | | - value: "all" as const, |
89 | | - hint: "Can function in both primary and subagent roles", |
90 | | - }, |
91 | | - { |
92 | | - label: "Primary", |
93 | | - value: "primary" as const, |
94 | | - hint: "Acts as a primary/main agent", |
95 | | - }, |
96 | | - { |
97 | | - label: "Subagent", |
98 | | - value: "subagent" as const, |
99 | | - hint: "Can be used as a subagent by other agents", |
100 | | - }, |
101 | | - ], |
102 | | - initialValue: "all", |
103 | | - }) |
104 | | - if (prompts.isCancel(modeResult)) throw new UI.CancelledError() |
| 124 | + // Select tools |
| 125 | + let selectedTools: string[] |
| 126 | + if (cliTools !== undefined) { |
| 127 | + selectedTools = cliTools ? cliTools.split(",").map((t) => t.trim()) : AVAILABLE_TOOLS |
| 128 | + } else { |
| 129 | + const result = await prompts.multiselect({ |
| 130 | + message: "Select tools to enable", |
| 131 | + options: AVAILABLE_TOOLS.map((tool) => ({ |
| 132 | + label: tool, |
| 133 | + value: tool, |
| 134 | + })), |
| 135 | + initialValues: AVAILABLE_TOOLS, |
| 136 | + }) |
| 137 | + if (prompts.isCancel(result)) throw new UI.CancelledError() |
| 138 | + selectedTools = result |
| 139 | + } |
105 | 140 |
|
| 141 | + // Get mode |
| 142 | + let mode: AgentMode |
| 143 | + if (cliMode) { |
| 144 | + mode = cliMode |
| 145 | + } else { |
| 146 | + const modeResult = await prompts.select({ |
| 147 | + message: "Agent mode", |
| 148 | + options: [ |
| 149 | + { |
| 150 | + label: "All", |
| 151 | + value: "all" as const, |
| 152 | + hint: "Can function in both primary and subagent roles", |
| 153 | + }, |
| 154 | + { |
| 155 | + label: "Primary", |
| 156 | + value: "primary" as const, |
| 157 | + hint: "Acts as a primary/main agent", |
| 158 | + }, |
| 159 | + { |
| 160 | + label: "Subagent", |
| 161 | + value: "subagent" as const, |
| 162 | + hint: "Can be used as a subagent by other agents", |
| 163 | + }, |
| 164 | + ], |
| 165 | + initialValue: "all" as const, |
| 166 | + }) |
| 167 | + if (prompts.isCancel(modeResult)) throw new UI.CancelledError() |
| 168 | + mode = modeResult |
| 169 | + } |
| 170 | + |
| 171 | + // Build tools config |
106 | 172 | const tools: Record<string, boolean> = {} |
107 | | - for (const tool of availableTools) { |
| 173 | + for (const tool of AVAILABLE_TOOLS) { |
108 | 174 | if (!selectedTools.includes(tool)) { |
109 | 175 | tools[tool] = false |
110 | 176 | } |
111 | 177 | } |
112 | 178 |
|
113 | | - const frontmatter: any = { |
| 179 | + // Build frontmatter |
| 180 | + const frontmatter: { |
| 181 | + description: string |
| 182 | + mode: AgentMode |
| 183 | + tools?: Record<string, boolean> |
| 184 | + } = { |
114 | 185 | description: generated.whenToUse, |
115 | | - mode: modeResult, |
| 186 | + mode, |
116 | 187 | } |
117 | 188 | if (Object.keys(tools).length > 0) { |
118 | 189 | frontmatter.tools = tools |
119 | 190 | } |
120 | 191 |
|
| 192 | + // Write file |
121 | 193 | const content = matter.stringify(generated.systemPrompt, frontmatter) |
122 | | - const filePath = path.join( |
123 | | - scope === "global" ? Global.Path.config : path.join(Instance.worktree, ".opencode"), |
124 | | - `agent`, |
125 | | - `${generated.identifier}.md`, |
126 | | - ) |
| 194 | + const filePath = path.join(targetPath, `${generated.identifier}.md`) |
| 195 | + |
| 196 | + await fs.mkdir(targetPath, { recursive: true }) |
| 197 | + |
| 198 | + const file = Bun.file(filePath) |
| 199 | + if (await file.exists()) { |
| 200 | + if (isFullyNonInteractive) { |
| 201 | + console.error(`Error: Agent file already exists: ${filePath}`) |
| 202 | + process.exit(1) |
| 203 | + } |
| 204 | + prompts.log.error(`Agent file already exists: ${filePath}`) |
| 205 | + throw new UI.CancelledError() |
| 206 | + } |
127 | 207 |
|
128 | 208 | await Bun.write(filePath, content) |
129 | 209 |
|
130 | | - prompts.log.success(`Agent created: ${filePath}`) |
131 | | - prompts.outro("Done") |
| 210 | + if (isFullyNonInteractive) { |
| 211 | + console.log(filePath) |
| 212 | + } else { |
| 213 | + prompts.log.success(`Agent created: ${filePath}`) |
| 214 | + prompts.outro("Done") |
| 215 | + } |
132 | 216 | }, |
133 | 217 | }) |
134 | 218 | }, |
|