Skip to content

Commit b00b823

Browse files
committed
fix(background-task): gracefully handle agent not found errors
When an invalid or unregistered agent is passed to background_task or call_omo_agent, OpenCode crashes with "TypeError: undefined is not an object (evaluating 'agent.name')". This fix: - Validates agent parameter is not empty before launching - Catches prompt errors and returns friendly error message - Notifies parent session when background task fails - Improves error message to guide user on resolution Fixes #37 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
1 parent 53d8cf1 commit b00b823

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/features/background-agent/manager.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export class BackgroundManager {
3939
}
4040

4141
async launch(input: LaunchInput): Promise<BackgroundTask> {
42+
if (!input.agent || input.agent.trim() === "") {
43+
throw new Error("Agent parameter is required")
44+
}
45+
4246
const createResult = await this.client.session.create({
4347
body: {
4448
parentID: input.parentSessionID,
@@ -71,7 +75,7 @@ export class BackgroundManager {
7175
this.tasks.set(task.id, task)
7276
this.startPolling()
7377

74-
log("[background-agent] Launching task:", { taskId: task.id, sessionID })
78+
log("[background-agent] Launching task:", { taskId: task.id, sessionID, agent: input.agent })
7579

7680
this.client.session.promptAsync({
7781
path: { id: sessionID },
@@ -90,8 +94,15 @@ export class BackgroundManager {
9094
const existingTask = this.findBySession(sessionID)
9195
if (existingTask) {
9296
existingTask.status = "error"
93-
existingTask.error = String(error)
97+
const errorMessage = error instanceof Error ? error.message : String(error)
98+
if (errorMessage.includes("agent.name") || errorMessage.includes("undefined")) {
99+
existingTask.error = `Agent "${input.agent}" not found. Make sure the agent is registered in your opencode.json or provided by a plugin.`
100+
} else {
101+
existingTask.error = errorMessage
102+
}
94103
existingTask.completedAt = new Date()
104+
this.markForNotification(existingTask)
105+
this.notifyParentSession(existingTask)
95106
}
96107
})
97108

src/tools/background-task/tools.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ export function createBackgroundTask(manager: BackgroundManager) {
2626
args: {
2727
description: tool.schema.string().describe("Short task description (shown in status)"),
2828
prompt: tool.schema.string().describe("Full detailed prompt for the agent"),
29-
agent: tool.schema.string().describe("Agent type to use (any agent allowed)"),
29+
agent: tool.schema.string().describe("Agent type to use (any registered agent)"),
3030
},
3131
async execute(args: BackgroundTaskArgs, toolContext) {
32+
if (!args.agent || args.agent.trim() === "") {
33+
return `❌ Agent parameter is required. Please specify which agent to use (e.g., "explore", "librarian", "build", etc.)`
34+
}
35+
3236
try {
3337
const task = await manager.launch({
3438
description: args.description,
3539
prompt: args.prompt,
36-
agent: args.agent,
40+
agent: args.agent.trim(),
3741
parentSessionID: toolContext.sessionID,
3842
parentMessageID: toolContext.messageID,
3943
})

src/tools/call-omo-agent/tools.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,26 @@ async function executeSync(
114114
log(`[call_omo_agent] Sending prompt to session ${sessionID}`)
115115
log(`[call_omo_agent] Prompt text:`, args.prompt.substring(0, 100))
116116

117-
await ctx.client.session.prompt({
118-
path: { id: sessionID },
119-
body: {
120-
agent: args.subagent_type,
121-
tools: {
122-
task: false,
123-
call_omo_agent: false,
117+
try {
118+
await ctx.client.session.prompt({
119+
path: { id: sessionID },
120+
body: {
121+
agent: args.subagent_type,
122+
tools: {
123+
task: false,
124+
call_omo_agent: false,
125+
},
126+
parts: [{ type: "text", text: args.prompt }],
124127
},
125-
parts: [{ type: "text", text: args.prompt }],
126-
},
127-
})
128+
})
129+
} catch (error) {
130+
const errorMessage = error instanceof Error ? error.message : String(error)
131+
log(`[call_omo_agent] Prompt error:`, errorMessage)
132+
if (errorMessage.includes("agent.name") || errorMessage.includes("undefined")) {
133+
return `Error: Agent "${args.subagent_type}" not found. Make sure the agent is registered in your opencode.json or provided by a plugin.\n\n<task_metadata>\nsession_id: ${sessionID}\n</task_metadata>`
134+
}
135+
return `Error: Failed to send prompt: ${errorMessage}\n\n<task_metadata>\nsession_id: ${sessionID}\n</task_metadata>`
136+
}
128137

129138
log(`[call_omo_agent] Prompt sent, fetching messages...`)
130139

0 commit comments

Comments
 (0)