fix(background-agent): force TUI update when notifying parent session

- Use `promptAsync` instead of `prompt` to avoid session state conflicts
- Use `tui.showToast` for immediate visible feedback
- Hack: Trigger `tui.submitPrompt` after message injection to force TUI refresh and trigger AI response
- Update `BackgroundManager` to accept `PluginInput` for directory access
This commit is contained in:
YeonGyu-Kim
2025-12-11 18:13:02 +09:00
parent b422e2f94f
commit 9986841f9b
2 changed files with 34 additions and 8 deletions

View File

@@ -28,12 +28,14 @@ export class BackgroundManager {
private tasks: Map<string, BackgroundTask> private tasks: Map<string, BackgroundTask>
private notifications: Map<string, BackgroundTask[]> private notifications: Map<string, BackgroundTask[]>
private client: OpencodeClient private client: OpencodeClient
private directory: string
private pollingInterval?: Timer private pollingInterval?: Timer
constructor(client: OpencodeClient) { constructor(ctx: PluginInput) {
this.tasks = new Map() this.tasks = new Map()
this.notifications = new Map() this.notifications = new Map()
this.client = client this.client = ctx.client
this.directory = ctx.directory
} }
async launch(input: LaunchInput): Promise<BackgroundTask> { async launch(input: LaunchInput): Promise<BackgroundTask> {
@@ -214,19 +216,43 @@ export class BackgroundManager {
const duration = this.formatDuration(task.startedAt, task.completedAt) const duration = this.formatDuration(task.startedAt, task.completedAt)
const toolCalls = task.progress?.toolCalls ?? 0 const toolCalls = task.progress?.toolCalls ?? 0
const message = `Background task "${task.description}" completed in ${duration} with ${toolCalls} tool calls. Use background_result tool with taskId="${task.id}" to get the full result.` const message = `[BACKGROUND TASK COMPLETED] Task "${task.description}" finished in ${duration} with ${toolCalls} tool calls. Use background_result tool with taskId="${task.id}" to retrieve the result.`
log("[background-agent] Sending message to parent session:", task.parentSessionID) log("[background-agent] Sending async message to parent:", task.parentSessionID)
this.client.session.prompt({ // eslint-disable-next-line @typescript-eslint/no-explicit-any
const tuiClient = this.client as any
if (tuiClient.tui?.showToast) {
tuiClient.tui.showToast({
body: {
title: "Background Task Completed",
message: `Task "${task.description}" finished.`,
variant: "success",
duration: 5000,
},
}).catch(() => {})
}
this.client.session.promptAsync({
path: { id: task.parentSessionID }, path: { id: task.parentSessionID },
body: { body: {
parts: [{ type: "text", text: message }], parts: [{ type: "text", text: message }],
}, },
}).then((result) => { }).then((result) => {
log("[background-agent] Message sent, response:", result.data ? "success" : result.error) log("[background-agent] promptAsync result:", { error: result.error, response: result.response?.status })
setTimeout(() => {
if (tuiClient.tui?.submitPrompt) {
log("[background-agent] Triggering submitPrompt to force TUI update")
tuiClient.tui.submitPrompt({
query: { directory: this.directory }
}).catch((err: unknown) => {
log("[background-agent] submitPrompt failed:", String(err))
})
}
}, 100)
}).catch((error) => { }).catch((error) => {
log("[background-agent] Failed to send message:", error) log("[background-agent] promptAsync exception:", String(error))
}) })
} }

View File

@@ -164,7 +164,7 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
updateTerminalTitle({ sessionId: "main" }); updateTerminalTitle({ sessionId: "main" });
const backgroundManager = new BackgroundManager(ctx.client); const backgroundManager = new BackgroundManager(ctx);
const backgroundNotificationHook = createBackgroundNotificationHook(backgroundManager); const backgroundNotificationHook = createBackgroundNotificationHook(backgroundManager);
const backgroundTools = createBackgroundTools(backgroundManager, ctx.client); const backgroundTools = createBackgroundTools(backgroundManager, ctx.client);