Skip to content
Merged
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
20 changes: 9 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ jobs:
with:
bun-version: latest

- name: Install dependencies
run: bun install

- name: Lint
run: bun run lint
- uses: actions/setup-node@v4
with:
node-version: "24"
registry-url: "https://registry.npmjs.org"

- name: Build
run: bun run build
- name: Prevent duplicate publish
run: |
if npm view opencode-notification@$(node -p "require('./package.json').version") version; then
echo "Version already published"
exit 1
fi

- uses: actions/setup-node@v4
with:
node-version: "24"
registry-url: "https://registry.npmjs.org"
- name: Install dependencies
run: bun install

- name: Lint
run: bun run lint

- name: Publish to npm
run: npm publish --access public
Expand Down
5 changes: 4 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import path from "node:path";
import type { Plugin } from "@opencode-ai/plugin";
import { createNotificationScheduler } from "@/notification-scheduler";
import { loadConfig, ConfigError } from "@/config/loader";
import { createResolvedConfig } from "@/config/resolver";

export const SimpleNotificationPlugin: Plugin = async ({ client }) => {
const scheduler = createNotificationScheduler();
const config = await loadConfig().catch(async (error) => {
const message =
error instanceof ConfigError
? `Notification plugin config error: ${error.message}`
: `Notification plugin failed to load: ${error instanceof Error ? error.message : String(error)}`;

await client.tui.showToast({
body: { message, variant: "error" },
});

throw error;
});

const resolvedConfig = createResolvedConfig(config);
const scheduler = createNotificationScheduler(resolvedConfig);
// Tracks sessions where assistant has responded since last user message
const activeSessions = new Set<string>();

Expand All @@ -22,7 +38,7 @@ export const SimpleNotificationPlugin: Plugin = async ({ client }) => {
.get({ path: { id: sessionId } })
.then((details) => details.data?.title)
.catch(() => undefined);
scheduler.schedule(sessionId, "Response ready", title ?? sessionId);
scheduler.schedule(sessionId, "Response ready", title ?? sessionId, "session.idle");
}
activeSessions.delete(sessionId);
break;
Expand All @@ -37,7 +53,7 @@ export const SimpleNotificationPlugin: Plugin = async ({ client }) => {
.catch(() => undefined)
: (event.properties.error?.data.message as string);
if (sessionId) {
scheduler.schedule(sessionId, "Session error", message ?? sessionId);
scheduler.schedule(sessionId, "Session error", message ?? sessionId, "session.error");
}
break;
}
Expand All @@ -58,6 +74,7 @@ export const SimpleNotificationPlugin: Plugin = async ({ client }) => {
sessionId,
"Permission Asked",
`${session?.title} in ${projectName} needs permission`,
"permission.asked",
);
break;
}
Expand All @@ -78,6 +95,7 @@ export const SimpleNotificationPlugin: Plugin = async ({ client }) => {
sessionId,
"Question",
`${session?.title} in ${projectName} has a question`,
"question.asked",
);
break;
}
Expand All @@ -95,8 +113,6 @@ export const SimpleNotificationPlugin: Plugin = async ({ client }) => {
case "message.updated": {
const info = event.properties.info;
if (info.role === "user") {
// Only cancel for real user messages, not automatic system messages
// System messages have 'agent' or 'model' fields
const infoAny = info;
const isAutomaticMessage = infoAny.agent || infoAny.model;
if (!isAutomaticMessage) {
Expand All @@ -119,7 +135,6 @@ export const SimpleNotificationPlugin: Plugin = async ({ client }) => {

case "tui.prompt.append":
case "tui.command.execute":
// No sessionID in these events, can't cancel reliably
break;
}
},
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"url": "git+https://github.com/IdrisGit/opencode-notification.git"
},
"files": [
"dist"
"dist",
"schema"
],
"type": "module",
"main": "dist/index.js",
Expand All @@ -28,14 +29,17 @@
}
},
"scripts": {
"build": "bun run clean && bun build index.ts --outdir dist --target bun --format esm && bunx tsc -p tsconfig.build.json --emitDeclarationOnly",
"prepack": "bun run build",
"build": "bun run clean && bun run generate-schema && bun build index.ts --outdir dist --target bun --format esm && bunx tsc -p tsconfig.build.json --emitDeclarationOnly",
"generate-schema": "bun scripts/generate-schema.ts",
"clean": "rm -rf dist",
"lint": "oxlint .",
"fmt": "oxfmt",
"fmt:check": "oxfmt --check"
},
"dependencies": {
"@opencode-ai/plugin": "^1.2.16"
"@opencode-ai/plugin": "^1.2.16",
"zod": "^4.3.6"
},
"devDependencies": {
"@types/bun": "latest",
Expand Down
93 changes: 93 additions & 0 deletions schema/oc-notification.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"$id": "https://unpkg.com/opencode-notification@0.0.3/schema/oc-notification.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"delay": {
"default": 15,
"description": "Default notification delay in seconds",
"type": "number"
},
"enabled": {
"default": true,
"description": "Master switch to enable/disable all notifications",
"type": "boolean"
},
"response_ready": {
"default": {
"enabled": true
},
"description": "Notification when AI response is ready",
"type": "object",
"properties": {
"enabled": {
"default": true,
"description": "Enable notifications for this event type",
"type": "boolean"
},
"delay": {
"description": "Optional override for notification delay in seconds",
"type": "number"
}
}
},
"error": {
"default": {
"enabled": true
},
"description": "Notification on session error",
"type": "object",
"properties": {
"enabled": {
"default": true,
"description": "Enable notifications for this event type",
"type": "boolean"
},
"delay": {
"description": "Optional override for notification delay in seconds",
"type": "number"
}
}
},
"permission_asked": {
"default": {
"enabled": true
},
"description": "Notification when permission is requested",
"type": "object",
"properties": {
"enabled": {
"default": true,
"description": "Enable notifications for this event type",
"type": "boolean"
},
"delay": {
"description": "Optional override for notification delay in seconds",
"type": "number"
}
}
},
"question_asked": {
"default": {
"enabled": true
},
"description": "Notification when a question is asked",
"type": "object",
"properties": {
"enabled": {
"default": true,
"description": "Enable notifications for this event type",
"type": "boolean"
},
"delay": {
"description": "Optional override for notification delay in seconds",
"type": "number"
}
}
},
"$schema": {
"type": "string",
"description": "JSON Schema URL for editor autocomplete and validation"
}
}
}
32 changes: 32 additions & 0 deletions scripts/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as z from "zod";
import { CONFIG_FILE_NAME } from "@/config/constants";
import { ConfigSchema } from "@/config/schema";

// Read package.json to get version for schema $id
const packageJsonPath = `${import.meta.dir}/../package.json`;
const packageJson = await Bun.file(packageJsonPath).json();
const version = packageJson.version;

const jsonSchema = z.toJSONSchema(ConfigSchema, {
io: "input",
target: "draft-2020-12",
unrepresentable: "throw",
});
const rootSchema = jsonSchema as z.core.JSONSchema.ObjectSchema;
rootSchema.properties ??= {};
rootSchema.properties.$schema = {
type: "string",
description: "JSON Schema URL for editor autocomplete and validation",
};

// Add $id with versioned URL for npm/unpkg CDN
// This allows users to reference specific schema versions
const schemaWithId = {
$id: `https://unpkg.com/opencode-notification@${version}/schema/${CONFIG_FILE_NAME}`,
...jsonSchema,
};

const outputPath = `${import.meta.dir}/../schema/${CONFIG_FILE_NAME}`;
await Bun.write(outputPath, JSON.stringify(schemaWithId, null, 2));

console.log(`Generated JSON schema: schema/${CONFIG_FILE_NAME} (version ${version})`);
1 change: 1 addition & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CONFIG_FILE_NAME = "oc-notification.json";
Loading
Loading