GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add CLI arguments to agent create command for scripting (#5157) · bpd1069/opencode@b205779

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/opencode/src
    • agent
      • agent.ts
    • cli/cmd
      • agent.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/agent/agent.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/agent/agent.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export namespace Agent {
252252
},
253253
},
254254
temperature: 0.3,
255-
prompt: [
255+
messages: [
256256
...system.map(
257257
(item): ModelMessage => ({
258258
role: "system",
Collapse file

‎packages/opencode/src/cli/cmd/agent.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/agent.ts
+170-86Lines changed: 170 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,131 +4,215 @@ import { UI } from "../ui"
44
import { Global } from "../../global"
55
import { Agent } from "../../agent/agent"
66
import path from "path"
7+
import fs from "fs/promises"
78
import matter from "gray-matter"
89
import { Instance } from "../../project/instance"
910
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+
]
1028

1129
const AgentCreateCommand = cmd({
1230
command: "create",
1331
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) {
1552
await Instance.provide({
1653
directory: process.cwd(),
1754
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+
2067
const project = Instance.project
2168

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+
)
4198
}
4299

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+
}
49113

114+
// Generate agent
50115
const spinner = prompts.spinner()
51-
52116
spinner.start("Generating agent configuration...")
53-
const generated = await Agent.generate({ description: query }).catch((error) => {
117+
const generated = await Agent.generate({ description }).catch((error) => {
54118
spinner.stop(`LLM failed to generate agent: ${error.message}`, 1)
119+
if (isFullyNonInteractive) process.exit(1)
55120
throw new UI.CancelledError()
56121
})
57122
spinner.stop(`Agent ${generated.identifier} generated`)
58123

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+
}
105140

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
106172
const tools: Record<string, boolean> = {}
107-
for (const tool of availableTools) {
173+
for (const tool of AVAILABLE_TOOLS) {
108174
if (!selectedTools.includes(tool)) {
109175
tools[tool] = false
110176
}
111177
}
112178

113-
const frontmatter: any = {
179+
// Build frontmatter
180+
const frontmatter: {
181+
description: string
182+
mode: AgentMode
183+
tools?: Record<string, boolean>
184+
} = {
114185
description: generated.whenToUse,
115-
mode: modeResult,
186+
mode,
116187
}
117188
if (Object.keys(tools).length > 0) {
118189
frontmatter.tools = tools
119190
}
120191

192+
// Write file
121193
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+
}
127207

128208
await Bun.write(filePath, content)
129209

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+
}
132216
},
133217
})
134218
},

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.