Skip to content
Closed
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
8 changes: 7 additions & 1 deletion src/features/background-agent/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,13 @@ export class BackgroundManager {
body: {
agent: prevMessage?.agent,
model: modelField,
parts: [{ type: "text", text: message }],
parts: [
{
type: "text",
text: message,
metadata: { origin: "background-notification" },
},
],
},
query: { directory: this.directory },
})
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/claude-code-hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { cacheToolInput, getToolInput } from "./tool-input-cache"
import { recordToolUse, recordToolResult, getTranscriptPath, recordUserMessage } from "./transcript"
import type { PluginConfig } from "./types"
import { log, isHookDisabled } from "../../shared"
import { hasPartOrigin, log, isHookDisabled } from "../../shared"
import { injectHookMessage } from "../../features/hook-message-injector"
import { detectKeywordsWithType, removeCodeBlocks } from "../keyword-detector"

Expand Down Expand Up @@ -138,7 +138,9 @@ export function createClaudeCodeHooksHook(ctx: PluginInput, config: PluginConfig
return
}

const detectedKeywords = detectKeywordsWithType(removeCodeBlocks(prompt), input.agent)
const detectedKeywords = hasPartOrigin(textParts, "background-notification")
? []
: detectKeywordsWithType(removeCodeBlocks(prompt), input.agent)
const keywordMessages = detectedKeywords.map((k) => k.message)

if (keywordMessages.length > 0) {
Expand Down
71 changes: 71 additions & 0 deletions src/hooks/keyword-detector/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, test } from "bun:test"

import { createKeywordDetectorHook } from "./index"

describe("keyword-detector hook", () => {
function createMockInput() {
const toastCalls: Array<{ title: string; message: string }> = []

const ctx = {
client: {
tui: {
showToast: async (opts: any) => {
toastCalls.push({
title: opts.body.title,
message: opts.body.message,
})
return {}
},
},
},
} as any

return { ctx, toastCalls }
}

test("skips keyword detection for background notifications", async () => {
// #given
const { ctx, toastCalls } = createMockInput()
const hook = createKeywordDetectorHook(ctx)
const output = {
message: {} as Record<string, unknown>,
parts: [
{
type: "text",
text: "ultrawork",
metadata: { origin: "background-notification" },
},
],
}

// #when
await hook["chat.message"](
{ sessionID: "ses-1", agent: "sisyphus" },
output,
)

// #then
expect(output.message.variant).toBeUndefined()
expect(toastCalls).toHaveLength(0)
})

test("applies ultrawork behavior for regular user messages", async () => {
// #given
const { ctx, toastCalls } = createMockInput()
const hook = createKeywordDetectorHook(ctx)
const output = {
message: {} as Record<string, unknown>,
parts: [{ type: "text", text: "ultrawork" }],
}

// #when
await hook["chat.message"](
{ sessionID: "ses-2", agent: "sisyphus" },
output,
)

// #then
expect(output.message.variant).toBe("max")
expect(toastCalls).toHaveLength(1)
})
})
6 changes: 5 additions & 1 deletion src/hooks/keyword-detector/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PluginInput } from "@opencode-ai/plugin"
import { detectKeywordsWithType, extractPromptText, removeCodeBlocks } from "./detector"
import { log } from "../../shared"
import { hasPartOrigin, log } from "../../shared"

export * from "./detector"
export * from "./constants"
Expand All @@ -20,6 +20,10 @@ export function createKeywordDetectorHook(ctx: PluginInput) {
parts: Array<{ type: string; text?: string; [key: string]: unknown }>
}
): Promise<void> => {
if (hasPartOrigin(output.parts, "background-notification")) {
return
}

const promptText = extractPromptText(output.parts)
const detectedKeywords = detectKeywordsWithType(removeCodeBlocks(promptText), input.agent)

Expand Down
1 change: 1 addition & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from "./migration"
export * from "./opencode-config-dir"
export * from "./opencode-version"
export * from "./permission-compat"
export * from "./message-origin"
30 changes: 30 additions & 0 deletions src/shared/message-origin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, test } from "bun:test"

import { hasPartOrigin } from "./message-origin"

describe("hasPartOrigin", () => {
test("returns true when a part matches origin", () => {
// #given
const parts = [
{ metadata: { origin: "background-notification" } },
{ metadata: { origin: "user" } },
]

// #when
const result = hasPartOrigin(parts, "background-notification")

// #then
expect(result).toBe(true)
})

test("returns false when no part matches origin", () => {
// #given
const parts = [{ metadata: { origin: "user" } }, {}]

// #when
const result = hasPartOrigin(parts, "background-notification")

// #then
expect(result).toBe(false)
})
})
11 changes: 11 additions & 0 deletions src/shared/message-origin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type PartWithMetadata = Record<string, unknown> & {
metadata?: Record<string, unknown>
}

export function hasPartOrigin(parts: PartWithMetadata[], origin: string): boolean {
return parts.some((part) => {
const metadata = part.metadata
if (!metadata) return false
return metadata.origin === origin
})
}