GeezerVison is showing github.com — feat: support configuring a default_agent across all API/user surface… · sashankh/opencode@8f6c884
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: github/action.yml +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | description: "Model to use" | 10 | 10 | required: true | 11 | 11 |
| | 12 | + agent: | | 13 | + description: "Agent to use. Must be a primary agent. Falls back to default_agent from config or 'build' if not found." | | 14 | + required: false | | 15 | + | 12 | 16 | share: | 13 | 17 | description: "Share the opencode session (defaults to true for public repos)" | 14 | 18 | required: false | | 62 | 66 | run: opencode github run | 63 | 67 | env: | 64 | 68 | MODEL: ${{ inputs.model }} | | 69 | + AGENT: ${{ inputs.agent }} | 65 | 70 | SHARE: ${{ inputs.share }} | 66 | 71 | PROMPT: ${{ inputs.prompt }} | 67 | 72 | USE_GITHUB_TOKEN: ${{ inputs.use_github_token }} | |
Collapse file Copy file name to clipboardExpand all lines: github/index.ts +27-1Lines changed: 27 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -318,6 +318,10 @@ function useEnvRunUrl() { | 318 | 318 | return `/${repo.owner}/${repo.repo}/actions/runs/${runId}` | 319 | 319 | } | 320 | 320 |
| | 321 | +function useEnvAgent() { | | 322 | + return process.env["AGENT"] || undefined | | 323 | +} | | 324 | + | 321 | 325 | function useEnvShare() { | 322 | 326 | const value = process.env["SHARE"] | 323 | 327 | if (!value) return undefined | @@ -578,16 +582,38 @@ async function summarize(response: string) { | 578 | 582 | } | 579 | 583 | } | 580 | 584 |
| | 585 | +async function resolveAgent(): Promise<string | undefined> { | | 586 | + const envAgent = useEnvAgent() | | 587 | + if (!envAgent) return undefined | | 588 | + | | 589 | + // Validate the agent exists and is a primary agent | | 590 | + const agents = await client.agent.list<true>() | | 591 | + const agent = agents.data?.find((a) => a.name === envAgent) | | 592 | + | | 593 | + if (!agent) { | | 594 | + console.warn(`agent "${envAgent}" not found. Falling back to default agent`) | | 595 | + return undefined | | 596 | + } | | 597 | + | | 598 | + if (agent.mode === "subagent") { | | 599 | + console.warn(`agent "${envAgent}" is a subagent, not a primary agent. Falling back to default agent`) | | 600 | + return undefined | | 601 | + } | | 602 | + | | 603 | + return envAgent | | 604 | +} | | 605 | + | 581 | 606 | async function chat(text: string, files: PromptFiles = []) { | 582 | 607 | console.log("Sending message to opencode...") | 583 | 608 | const { providerID, modelID } = useEnvModel() | | 609 | + const agent = await resolveAgent() | 584 | 610 |
| 585 | 611 | const chat = await client.session.chat<true>({ | 586 | 612 | path: session, | 587 | 613 | body: { | 588 | 614 | providerID, | 589 | 615 | modelID, | 590 | | - agent: "build", | | 616 | + agent, | 591 | 617 | parts: [ | 592 | 618 | { | 593 | 619 | type: "text", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/agent.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,7 @@ import { Log } from "../util/log" | 22 | 22 | import { ACPSessionManager } from "./session" | 23 | 23 | import type { ACPConfig, ACPSessionState } from "./types" | 24 | 24 | import { Provider } from "../provider/provider" | | 25 | +import { Agent as AgentModule } from "../agent/agent" | 25 | 26 | import { Installation } from "@/installation" | 26 | 27 | import { MessageV2 } from "@/session/message-v2" | 27 | 28 | import { Config } from "@/config/config" | @@ -705,7 +706,8 @@ export namespace ACP { | 705 | 706 | description: agent.description, | 706 | 707 | })) | 707 | 708 |
| 708 | | - const currentModeId = availableModes.find((m) => m.name === "build")?.id ?? availableModes[0].id | | 709 | + const defaultAgentName = await AgentModule.defaultAgent() | | 710 | + const currentModeId = availableModes.find((m) => m.name === defaultAgentName)?.id ?? availableModes[0].id | 709 | 711 |
| 710 | 712 | const mcpServers: Record<string, Config.Mcp> = {} | 711 | 713 | for (const server of params.mcpServers) { | @@ -807,7 +809,7 @@ export namespace ACP { | 807 | 809 | if (!current) { | 808 | 810 | this.sessionManager.setModel(session.id, model) | 809 | 811 | } | 810 | | - const agent = session.modeId ?? "build" | | 812 | + const agent = session.modeId ?? (await AgentModule.defaultAgent()) | 811 | 813 |
| 812 | 814 | const parts: Array< | 813 | 815 | { type: "text"; text: string } | { type: "file"; url: string; filename: string; mime: string } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/agent/agent.ts +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,9 @@ import { generateObject, type ModelMessage } from "ai" | 5 | 5 | import { SystemPrompt } from "../session/system" | 6 | 6 | import { Instance } from "../project/instance" | 7 | 7 | import { mergeDeep } from "remeda" | | 8 | +import { Log } from "../util/log" | | 9 | + | | 10 | +const log = Log.create({ service: "agent" }) | 8 | 11 |
| 9 | 12 | import PROMPT_GENERATE from "./generate.txt" | 10 | 13 | import PROMPT_COMPACTION from "./prompt/compaction.txt" | @@ -20,6 +23,7 @@ export namespace Agent { | 20 | 23 | mode: z.enum(["subagent", "primary", "all"]), | 21 | 24 | native: z.boolean().optional(), | 22 | 25 | hidden: z.boolean().optional(), | | 26 | + default: z.boolean().optional(), | 23 | 27 | topP: z.number().optional(), | 24 | 28 | temperature: z.number().optional(), | 25 | 29 | color: z.string().optional(), | @@ -245,6 +249,19 @@ export namespace Agent { | 245 | 249 | item.permission = mergeAgentPermissions(cfg.permission ?? {}, permission ?? {}) | 246 | 250 | } | 247 | 251 | } | | 252 | + | | 253 | + // Mark the default agent | | 254 | + const defaultName = cfg.default_agent ?? "build" | | 255 | + const defaultCandidate = result[defaultName] | | 256 | + if (defaultCandidate && defaultCandidate.mode !== "subagent") { | | 257 | + defaultCandidate.default = true | | 258 | + } else { | | 259 | + // Fall back to "build" if configured default is invalid | | 260 | + if (result["build"]) { | | 261 | + result["build"].default = true | | 262 | + } | | 263 | + } | | 264 | + | 248 | 265 | return result | 249 | 266 | }) | 250 | 267 |
| @@ -256,6 +273,12 @@ export namespace Agent { | 256 | 273 | return state().then((x) => Object.values(x)) | 257 | 274 | } | 258 | 275 |
| | 276 | + export async function defaultAgent(): Promise<string> { | | 277 | + const agents = await state() | | 278 | + const defaultCandidate = Object.values(agents).find((a) => a.default) | | 279 | + return defaultCandidate?.name ?? "build" | | 280 | + } | | 281 | + | 259 | 282 | export async function generate(input: { description: string; model?: { providerID: string; modelID: string } }) { | 260 | 283 | const cfg = await Config.get() | 261 | 284 | const defaultModel = input.model ?? (await Provider.defaultModel()) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/github.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -762,7 +762,7 @@ export const GithubRunCommand = cmd({ | 762 | 762 | providerID, | 763 | 763 | modelID, | 764 | 764 | }, | 765 | | - agent: "build", | | 765 | + // agent is omitted - server will use default_agent from config or fall back to "build" | 766 | 766 | parts: [ | 767 | 767 | { | 768 | 768 | id: Identifier.ascending("part"), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/run.ts +26-2Lines changed: 26 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ import { select } from "@clack/prompts" | 10 | 10 | import { createOpencodeClient, type OpencodeClient } from "@opencode-ai/sdk/v2" | 11 | 11 | import { Server } from "../../server/server" | 12 | 12 | import { Provider } from "../../provider/provider" | | 13 | +import { Agent } from "../../agent/agent" | 13 | 14 |
| 14 | 15 | const TOOL: Record<string, [string, string]> = { | 15 | 16 | todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD], | @@ -223,10 +224,33 @@ export const RunCommand = cmd({ | 223 | 224 | } | 224 | 225 | })() | 225 | 226 |
| | 227 | + // Validate agent if specified | | 228 | + const resolvedAgent = await (async () => { | | 229 | + if (!args.agent) return undefined | | 230 | + const agent = await Agent.get(args.agent) | | 231 | + if (!agent) { | | 232 | + UI.println( | | 233 | + UI.Style.TEXT_WARNING_BOLD + "!", | | 234 | + UI.Style.TEXT_NORMAL, | | 235 | + `agent "${args.agent}" not found. Falling back to default agent`, | | 236 | + ) | | 237 | + return undefined | | 238 | + } | | 239 | + if (agent.mode === "subagent") { | | 240 | + UI.println( | | 241 | + UI.Style.TEXT_WARNING_BOLD + "!", | | 242 | + UI.Style.TEXT_NORMAL, | | 243 | + `agent "${args.agent}" is a subagent, not a primary agent. Falling back to default agent`, | | 244 | + ) | | 245 | + return undefined | | 246 | + } | | 247 | + return args.agent | | 248 | + })() | | 249 | + | 226 | 250 | if (args.command) { | 227 | 251 | await sdk.session.command({ | 228 | 252 | sessionID, | 229 | | - agent: args.agent || "build", | | 253 | + agent: resolvedAgent, | 230 | 254 | model: args.model, | 231 | 255 | command: args.command, | 232 | 256 | arguments: message, | @@ -235,7 +259,7 @@ export const RunCommand = cmd({ | 235 | 259 | const modelParam = args.model ? Provider.parseModel(args.model) : undefined | 236 | 260 | await sdk.session.prompt({ | 237 | 261 | sessionID, | 238 | | - agent: args.agent || "build", | | 262 | + agent: resolvedAgent, | 239 | 263 | model: modelParam, | 240 | 264 | parts: [...fileParts, { type: "text", text: message }], | 241 | 265 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/context/local.tsx +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,7 +56,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | 56 | 56 | const [agentStore, setAgentStore] = createStore<{ | 57 | 57 | current: string | 58 | 58 | }>({ | 59 | | - current: agents()[0].name, | | 59 | + current: agents().find((x) => x.default)?.name ?? agents()[0].name, | 60 | 60 | }) | 61 | 61 | const { theme } = useTheme() | 62 | 62 | const colors = createMemo(() => [ | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -666,6 +666,12 @@ export namespace Config { | 666 | 666 | .string() | 667 | 667 | .describe("Small model to use for tasks like title generation in the format of provider/model") | 668 | 668 | .optional(), | | 669 | + default_agent: z | | 670 | + .string() | | 671 | + .optional() | | 672 | + .describe( | | 673 | + "Default agent to use when none is specified. Must be a primary agent. Falls back to 'build' if not set or if the specified agent is invalid.", | | 674 | + ), | 669 | 675 | username: z | 670 | 676 | .string() | 671 | 677 | .optional() | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/server.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1060,11 +1060,11 @@ export namespace Server { | 1060 | 1060 | const sessionID = c.req.valid("param").sessionID | 1061 | 1061 | const body = c.req.valid("json") | 1062 | 1062 | const msgs = await Session.messages({ sessionID }) | 1063 | | - let currentAgent = "build" | | 1063 | + let currentAgent = await Agent.defaultAgent() | 1064 | 1064 | for (let i = msgs.length - 1; i >= 0; i--) { | 1065 | 1065 | const info = msgs[i].info | 1066 | 1066 | if (info.role === "user") { | 1067 | | - currentAgent = info.agent || "build" | | 1067 | + currentAgent = info.agent || (await Agent.defaultAgent()) | 1068 | 1068 | break | 1069 | 1069 | } | 1070 | 1070 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -715,7 +715,7 @@ export namespace SessionPrompt { | 715 | 715 | } | 716 | 716 |
| 717 | 717 | async function createUserMessage(input: PromptInput) { | 718 | | - const agent = await Agent.get(input.agent ?? "build") | | 718 | + const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent())) | 719 | 719 | const info: MessageV2.Info = { | 720 | 720 | id: input.messageID ?? Identifier.ascending("message"), | 721 | 721 | role: "user", | @@ -1282,7 +1282,7 @@ export namespace SessionPrompt { | 1282 | 1282 | export async function command(input: CommandInput) { | 1283 | 1283 | log.info("command", input) | 1284 | 1284 | const command = await Command.get(input.command) | 1285 | | - const agentName = command.agent ?? input.agent ?? "build" | | 1285 | + const agentName = command.agent ?? input.agent ?? (await Agent.defaultAgent()) | 1286 | 1286 |
| 1287 | 1287 | const raw = input.arguments.match(argsRegex) ?? [] | 1288 | 1288 | const args = raw.map((arg) => arg.replace(quoteTrimRegex, "")) | @@ -1425,7 +1425,7 @@ export namespace SessionPrompt { | 1425 | 1425 | time: { | 1426 | 1426 | created: Date.now(), | 1427 | 1427 | }, | 1428 | | - agent: input.message.info.role === "user" ? input.message.info.agent : "build", | | 1428 | + agent: input.message.info.role === "user" ? input.message.info.agent : await Agent.defaultAgent(), | 1429 | 1429 | model: { | 1430 | 1430 | providerID: input.providerID, | 1431 | 1431 | modelID: input.modelID, | |
|