refactor(background-notification): remove chat.message handler
- Remove unused chat message notification handler - Remove formatDuration and formatNotifications helpers - Simplify to event-only handling - Remove chat.message hook call from main plugin Background task notifications now rely on event-based system only. 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,3 +27,4 @@ yarn.lock
|
||||
.env.local
|
||||
test-injection/
|
||||
notepad.md
|
||||
oauth-success.html
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { BackgroundManager, BackgroundTask } from "../../features/background-agent"
|
||||
import type { BackgroundManager } from "../../features/background-agent"
|
||||
|
||||
interface Event {
|
||||
type: string
|
||||
@@ -9,94 +9,13 @@ interface EventInput {
|
||||
event: Event
|
||||
}
|
||||
|
||||
interface ChatMessageInput {
|
||||
sessionID: string
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
interface ChatMessageOutput {
|
||||
parts: Array<{ type: string; text?: string; [key: string]: unknown }>
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
function formatDuration(start: Date, end?: Date): string {
|
||||
const duration = (end ?? new Date()).getTime() - start.getTime()
|
||||
const seconds = Math.floor(duration / 1000)
|
||||
const minutes = Math.floor(seconds / 60)
|
||||
const hours = Math.floor(minutes / 60)
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${minutes % 60}m ${seconds % 60}s`
|
||||
} else if (minutes > 0) {
|
||||
return `${minutes}m ${seconds % 60}s`
|
||||
} else {
|
||||
return `${seconds}s`
|
||||
}
|
||||
}
|
||||
|
||||
function formatNotifications(tasks: BackgroundTask[]): string {
|
||||
if (tasks.length === 0) {
|
||||
return ""
|
||||
}
|
||||
|
||||
if (tasks.length > 1) {
|
||||
let message = `**Background Tasks Complete (${tasks.length})**\n\n`
|
||||
|
||||
for (const task of tasks) {
|
||||
const duration = formatDuration(task.startedAt, task.completedAt)
|
||||
const toolCalls = task.progress?.toolCalls ?? 0
|
||||
|
||||
message += `• **${task.id}** - ${task.description}\n`
|
||||
message += ` Duration: ${duration} | Tool calls: ${toolCalls}\n\n`
|
||||
}
|
||||
|
||||
message += `Use \`background_output\` tool to retrieve results.`
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
const task = tasks[0]
|
||||
const duration = formatDuration(task.startedAt, task.completedAt)
|
||||
const toolCalls = task.progress?.toolCalls ?? 0
|
||||
|
||||
return `**Background Task Complete**
|
||||
|
||||
**Task ID:** ${task.id}
|
||||
**Description:** ${task.description}
|
||||
**Duration:** ${duration}
|
||||
**Tool calls:** ${toolCalls}
|
||||
|
||||
Use \`background_output\` tool with task_id="${task.id}" to retrieve the full result.`
|
||||
}
|
||||
|
||||
export function createBackgroundNotificationHook(manager: BackgroundManager) {
|
||||
const eventHandler = async ({ event }: EventInput) => {
|
||||
manager.handleEvent(event)
|
||||
}
|
||||
|
||||
const chatMessageHandler = async (
|
||||
input: ChatMessageInput,
|
||||
output: ChatMessageOutput
|
||||
) => {
|
||||
const notifications = manager.getPendingNotifications(input.sessionID)
|
||||
|
||||
if (notifications.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const message = formatNotifications(notifications)
|
||||
|
||||
output.parts.unshift({
|
||||
type: "text",
|
||||
text: message,
|
||||
})
|
||||
|
||||
manager.clearNotifications(input.sessionID)
|
||||
}
|
||||
|
||||
return {
|
||||
event: eventHandler,
|
||||
"chat.message": chatMessageHandler,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -180,7 +180,6 @@ const OhMyOpenCodePlugin: Plugin = async (ctx) => {
|
||||
|
||||
"chat.message": async (input, output) => {
|
||||
await claudeCodeHooks["chat.message"]?.(input, output);
|
||||
await backgroundNotificationHook["chat.message"](input, output);
|
||||
},
|
||||
|
||||
config: async (config) => {
|
||||
|
||||
Reference in New Issue
Block a user