Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion packages/opencode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export namespace Agent {
.optional(),
prompt: z.string().optional(),
tools: z.record(z.string(), z.boolean()),
subagents: z.record(z.string(), z.boolean()),
options: z.record(z.string(), z.any()),
})
.meta({
Expand Down Expand Up @@ -113,6 +114,7 @@ export namespace Agent {
todowrite: false,
...defaultTools,
},
subagents: {},
options: {},
permission: agentPermission,
mode: "subagent",
Expand All @@ -121,6 +123,7 @@ export namespace Agent {
build: {
name: "build",
tools: { ...defaultTools },
subagents: {},
options: {},
permission: agentPermission,
mode: "primary",
Expand All @@ -133,6 +136,7 @@ export namespace Agent {
tools: {
...defaultTools,
},
subagents: {},
mode: "primary",
builtIn: true,
},
Expand All @@ -150,9 +154,23 @@ export namespace Agent {
permission: agentPermission,
options: {},
tools: {},
subagents: {},
builtIn: false,
}
const { name, model, prompt, tools, description, temperature, top_p, mode, color, permission, ...extra } = value
const {
name,
model,
prompt,
tools,
subagents,
description,
temperature,
top_p,
mode,
permission,
color,
...extra
} = value
item.options = {
...item.options,
...extra,
Expand All @@ -168,6 +186,11 @@ export namespace Agent {
...defaultTools,
...item.tools,
}
if (subagents)
item.subagents = {
...item.subagents,
...subagents,
}
if (description) item.description = description
if (temperature != undefined) item.temperature = temperature
if (top_p != undefined) item.topP = top_p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { createMemo, createResource, createEffect, onMount, For, Show } from "so
import { createStore } from "solid-js/store"
import { useSDK } from "@tui/context/sdk"
import { useSync } from "@tui/context/sync"
import { useLocal } from "@tui/context/local"
import { useTheme, selectedForeground } from "@tui/context/theme"
import { SplitBorder } from "@tui/component/border"
import { useCommandDialog } from "@tui/component/dialog-command"
import { Locale } from "@/util/locale"
import { Wildcard } from "@/util/wildcard"
import type { PromptInfo } from "./history"

export type AutocompleteRef = {
Expand Down Expand Up @@ -39,6 +41,7 @@ export function Autocomplete(props: {
}) {
const sdk = useSDK()
const sync = useSync()
const local = useLocal()
const command = useCommandDialog()
const { theme } = useTheme()

Expand Down Expand Up @@ -152,9 +155,11 @@ export function Autocomplete(props: {
)

const agents = createMemo(() => {
const agents = sync.data.agent
return agents
const current = local.agent.current() as { subagents?: Record<string, boolean> }
const subagents = current.subagents ?? {}
return sync.data.agent
.filter((agent) => !agent.builtIn && agent.mode !== "primary")
.filter((agent) => Wildcard.all(agent.name, subagents) !== false)
.map(
(agent): AutocompleteOption => ({
display: "@" + agent.name,
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export namespace Config {
top_p: z.number().optional(),
prompt: z.string().optional(),
tools: z.record(z.string(), z.boolean()).optional(),
subagents: z.record(z.string(), z.boolean()).optional(),
disable: z.boolean().optional(),
description: z.string().optional().describe("Description of when to use the agent"),
mode: z.enum(["subagent", "primary", "all"]).optional(),
Expand Down
19 changes: 18 additions & 1 deletion packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { SessionSummary } from "./summary"
import { NamedError } from "@opencode-ai/util/error"
import { fn } from "@/util/fn"
import { SessionProcessor } from "./processor"
import { TaskTool } from "@/tool/task"
import { TaskTool, filterSubagents, TASK_DESCRIPTION } from "@/tool/task"
import { SessionStatus } from "./status"
import { Token } from "@/util/token"

Expand Down Expand Up @@ -804,6 +804,23 @@ export namespace SessionPrompt {
}
tools[key] = item
}

// Regenerate task tool description with filtered subagents
if (tools.task) {
const all = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
const filtered = filterSubagents(all, input.agent.subagents)
const description = TASK_DESCRIPTION.replace(
"{agents}",
filtered
.map((a) => `- ${a.name}: ${a.description ?? "This subagent should only be called manually by the user."}`)
.join("\n"),
)
tools.task = {
...tools.task,
description,
}
}

return tools
}

Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/tool/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { Agent } from "../agent/agent"
import { SessionPrompt } from "../session/prompt"
import { iife } from "@/util/iife"
import { defer } from "@/util/defer"
import { Wildcard } from "@/util/wildcard"

export { DESCRIPTION as TASK_DESCRIPTION }

export function filterSubagents(agents: Agent.Info[], subagents: Record<string, boolean>) {
return agents.filter((a) => Wildcard.all(a.name, subagents) !== false)
}

export const TaskTool = Tool.define("task", async () => {
const agents = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
Expand All @@ -29,6 +36,9 @@ export const TaskTool = Tool.define("task", async () => {
async execute(params, ctx) {
const agent = await Agent.get(params.subagent_type)
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
const calling = await Agent.get(ctx.agent)
if (calling && Wildcard.all(params.subagent_type, calling.subagents) === false)
throw new Error(`Agent '${params.subagent_type}' is not available to ${ctx.agent}`)
const session = await iife(async () => {
if (params.session_id) {
const found = await Session.get(params.session_id).catch(() => {})
Expand Down
93 changes: 93 additions & 0 deletions packages/opencode/test/subagents-filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, test, expect } from "bun:test"
import type { Agent } from "../src/agent/agent"
import { filterSubagents } from "../src/tool/task"
import { Wildcard } from "../src/util/wildcard"

describe("filterSubagents", () => {
const mockAgents = [
{ name: "general", mode: "subagent" },
{ name: "code-reviewer", mode: "subagent" },
{ name: "orchestrator-fast", mode: "subagent" },
{ name: "orchestrator-slow", mode: "subagent" },
] as Agent.Info[]

test("returns all agents when subagents config is empty", () => {
const result = filterSubagents(mockAgents, {})
expect(result).toHaveLength(4)
expect(result.map((a) => a.name)).toEqual(["general", "code-reviewer", "orchestrator-fast", "orchestrator-slow"])
})

test("excludes agents with explicit false", () => {
const result = filterSubagents(mockAgents, { "code-reviewer": false })
expect(result).toHaveLength(3)
expect(result.map((a) => a.name)).toEqual(["general", "orchestrator-fast", "orchestrator-slow"])
})

test("includes agents with explicit true", () => {
const result = filterSubagents(mockAgents, {
"code-reviewer": true,
general: false,
})
expect(result).toHaveLength(3)
expect(result.map((a) => a.name)).toEqual(["code-reviewer", "orchestrator-fast", "orchestrator-slow"])
})

test("supports wildcard patterns to exclude", () => {
const result = filterSubagents(mockAgents, { "orchestrator-*": false })
expect(result).toHaveLength(2)
expect(result.map((a) => a.name)).toEqual(["general", "code-reviewer"])
})

test("supports wildcard patterns to include with specific exclusion", () => {
const result = filterSubagents(mockAgents, {
"*": true,
"orchestrator-fast": false,
})
expect(result).toHaveLength(3)
expect(result.map((a) => a.name)).toEqual(["general", "code-reviewer", "orchestrator-slow"])
})

test("longer pattern takes precedence", () => {
const result = filterSubagents(mockAgents, {
"orchestrator-*": false,
"orchestrator-fast": true,
})
expect(result).toHaveLength(3)
expect(result.map((a) => a.name)).toEqual(["general", "code-reviewer", "orchestrator-fast"])
})
})

describe("Wildcard.all for subagents", () => {
test("returns undefined when no match", () => {
expect(Wildcard.all("code-reviewer", {})).toBeUndefined()
})

test("returns false for explicit false", () => {
expect(Wildcard.all("code-reviewer", { "code-reviewer": false })).toBe(false)
})

test("returns true for explicit true", () => {
expect(Wildcard.all("code-reviewer", { "code-reviewer": true })).toBe(true)
})

test("matches wildcard patterns", () => {
expect(Wildcard.all("orchestrator-fast", { "orchestrator-*": false })).toBe(false)
expect(Wildcard.all("orchestrator-slow", { "orchestrator-*": false })).toBe(false)
expect(Wildcard.all("general", { "orchestrator-*": false })).toBeUndefined()
})

test("longer pattern takes precedence over shorter", () => {
expect(
Wildcard.all("orchestrator-fast", {
"orchestrator-*": false,
"orchestrator-fast": true,
}),
).toBe(true)
expect(
Wildcard.all("orchestrator-slow", {
"orchestrator-*": false,
"orchestrator-fast": true,
}),
).toBe(false)
})
})
9 changes: 9 additions & 0 deletions packages/sdk/js/src/gen/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,9 @@ export type AgentConfig = {
tools?: {
[key: string]: boolean
}
subagents?: {
[key: string]: boolean
}
disable?: boolean
/**
* Description of when to use the agent
Expand All @@ -929,6 +932,9 @@ export type AgentConfig = {
| unknown
| string
| number
| {
[key: string]: boolean
}
| {
[key: string]: boolean
}
Expand Down Expand Up @@ -1492,6 +1498,9 @@ export type Agent = {
tools: {
[key: string]: boolean
}
subagents: {
[key: string]: boolean
}
options: {
[key: string]: unknown
}
Expand Down
70 changes: 70 additions & 0 deletions packages/web/src/content/docs/agents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,76 @@ You can also use wildcards to control multiple tools at once. For example, to di

---

### Subagents

Control which subagents this agent can invoke via the Task tool with the `subagents` config.

By default, all subagents are available. Set a subagent to `false` to hide it from this agent.

```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"agent": {
"build": {
"subagents": {
"code-reviewer": false
}
}
}
}
```

You can also use wildcards to control multiple subagents at once:

```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"agent": {
"build": {
"subagents": {
"orchestrator-*": false
}
}
}
}
```

Longer patterns take precedence over shorter ones. This allows you to exclude a group while keeping specific exceptions:

```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"agent": {
"build": {
"subagents": {
"orchestrator-*": false,
"orchestrator-fast": true
}
}
}
}
```

You can also configure subagents in Markdown agents:

```markdown title="~/.config/opencode/agent/focused.md"
---
description: Agent with limited subagent access
mode: primary
subagents:
general: true
orchestrator-*: false
---

You are a focused agent with limited subagent access.
```

:::note
Filtered subagents will not appear in the Task tool description and cannot be invoked.
:::

---

### Permissions

You can configure permissions to manage what actions an agent can take. Currently, the permissions for the `edit`, `bash`, and `webfetch` tools can be configured to:
Expand Down