Skip to content
Open
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
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ sudo pacman -S libnotify # Arch

For sounds, you need one of: `paplay`, `aplay`, `mpv`, or `ffplay`

Linux notifications also include default urgency levels for better priority handling:
- `error` -> critical
- `permission` / `question` / `interrupted` -> normal
- `complete` / `subagent_complete` -> normal
- `user_cancelled` -> low

Linux notifications also use event-specific themed icons, richer metadata (`app-name`, categories, stack tags), and actions on supported daemons (`Open OpenCode`, `Copy message`). Grouping is threaded by session so each active session updates in place.

**Windows**: Works out of the box. But heads up:
- Only `.wav` files work (not mp3)
- Use full paths like `C:/Users/You/sounds/alert.wav` not `~/`
Expand Down Expand Up @@ -74,12 +82,12 @@ Create `~/.config/opencode/opencode-notifier.json` with the defaults:
"user_cancelled": { "sound": false, "notification": false }
},
"messages": {
"permission": "Session needs permission: {sessionTitle}",
"complete": "Session has finished: {sessionTitle}",
"subagent_complete": "Subagent task completed: {sessionTitle}",
"error": "Session encountered an error: {sessionTitle}",
"question": "Session has a question: {sessionTitle}",
"user_cancelled": "Session was cancelled by user: {sessionTitle}"
"permission": "Permission needed: {sessionTitle}",
"complete": "Task complete: {sessionTitle}",
"subagent_complete": "Subagent complete: {sessionTitle}",
"error": "Error: {sessionTitle}",
"question": "Question: {sessionTitle}",
"user_cancelled": "Cancelled: {sessionTitle}"
},
"sounds": {
"permission": null,
Expand Down Expand Up @@ -162,12 +170,12 @@ Customize the notification text:
```json
{
"messages": {
"permission": "Session needs permission: {sessionTitle}",
"complete": "Session has finished: {sessionTitle}",
"subagent_complete": "Subagent task completed: {sessionTitle}",
"error": "Session encountered an error: {sessionTitle}",
"question": "Session has a question: {sessionTitle}",
"user_cancelled": "Session was cancelled by user: {sessionTitle}"
"permission": "Permission needed: {sessionTitle}",
"complete": "Task complete: {sessionTitle}",
"subagent_complete": "Subagent complete: {sessionTitle}",
"error": "Error: {sessionTitle}",
"question": "Question: {sessionTitle}",
"user_cancelled": "Cancelled: {sessionTitle}"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("Config", () => {

expect(isEventSoundEnabled(config, "user_cancelled")).toBe(false)
expect(isEventNotificationEnabled(config, "user_cancelled")).toBe(false)
expect(config.messages.user_cancelled).toBe("Session was cancelled by user: {sessionTitle}")
expect(config.messages.user_cancelled).toBe("Cancelled: {sessionTitle}")
})

test("loadConfig parses user_cancelled event config from file", async () => {
Expand Down
14 changes: 7 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ const DEFAULT_CONFIG: NotifierConfig = {
user_cancelled: { sound: false, notification: false },
},
messages: {
permission: "Session needs permission: {sessionTitle}",
complete: "Session has finished: {sessionTitle}",
subagent_complete: "Subagent task completed: {sessionTitle}",
error: "Session encountered an error: {sessionTitle}",
question: "Session has a question: {sessionTitle}",
interrupted: "Session was interrupted: {sessionTitle}",
user_cancelled: "Session was cancelled by user: {sessionTitle}",
permission: "Permission needed: {sessionTitle}",
complete: "Task complete: {sessionTitle}",
subagent_complete: "Subagent complete: {sessionTitle}",
error: "Error: {sessionTitle}",
question: "Question: {sessionTitle}",
interrupted: "Interrupted: {sessionTitle}",
user_cancelled: "Cancelled: {sessionTitle}",
},
sounds: {
permission: null,
Expand Down
44 changes: 37 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,30 @@ setInterval(() => {
}
}, 5 * 60 * 1000)

function getNotificationTitle(config: NotifierConfig, projectName: string | null): string {
if (config.showProjectName && projectName) {
return `OpenCode (${projectName})`
function getEventTitle(eventType: EventType): string {
switch (eventType) {
case "permission":
return "Permission Needed"
case "complete":
return "Task Complete"
case "subagent_complete":
return "Subagent Complete"
case "error":
return "Error"
case "question":
return "Question"
case "interrupted":
return "Interrupted"
case "user_cancelled":
return "Cancelled"
default:
return "Update"
}
return "OpenCode"
}

function getNotificationTitle(config: NotifierConfig, projectName: string | null, eventType: EventType): string {
const baseTitle = config.showProjectName && projectName ? `OpenCode (${projectName})` : "OpenCode"
return `${baseTitle} - ${getEventTitle(eventType)}`
}

function formatTimestamp(): string {
Expand All @@ -96,6 +115,16 @@ function formatTimestamp(): string {
return `${h}:${m}:${s}`
}

function formatNotificationMessage(message: string, timestamp: string, turn: number): string {
const parts: string[] = []
if (message.trim().length > 0) {
parts.push(message.trim())
}
parts.push(`at ${timestamp}`)
parts.push(`#${turn}`)
return parts.join(" • ")
}

async function handleEvent(
config: NotifierConfig,
eventType: EventType,
Expand All @@ -114,17 +143,18 @@ async function handleEvent(
const turn = incrementTurnCount()

const rawMessage = getMessage(config, eventType)
const message = interpolateMessage(rawMessage, {
const interpolatedMessage = interpolateMessage(rawMessage, {
sessionTitle: config.showSessionTitle ? sessionTitle : null,
projectName,
timestamp,
turn,
})
const message = formatNotificationMessage(interpolatedMessage, timestamp, turn)

if (isEventNotificationEnabled(config, eventType)) {
const title = getNotificationTitle(config, projectName)
const title = getNotificationTitle(config, projectName, eventType)
const iconPath = getIconPath(config)
promises.push(sendNotification(title, message, config.timeout, iconPath, config.notificationSystem, config.linux.grouping))
promises.push(sendNotification(title, message, config.timeout, iconPath, config.notificationSystem, config.linux.grouping, eventType, sessionID))
}

if (isEventSoundEnabled(config, eventType)) {
Expand Down
Loading
Loading