repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/tools/notify.ts

Raw
import { tool } from "@opencode-ai/plugin";
import notifier from "node-notifier";
import { homedir } from "node:os";
import path from "node:path";

type Urgency = "low" | "normal" | "critical";

interface NotificationOptions {
  title: string;
  message: string;
  sound: boolean;
  timeout: number;
  urgency: Urgency;
  wait: boolean;
}

/**
 * Validate notification options
 */
export function validateOptions(options: NotificationOptions): string | null {
  if (options.title.length > 256 || options.message.length > 256) {
    return "Title and message must be 256 characters or less";
  }

  if (options.timeout < 0 || options.timeout > 86400) {
    return "Timeout must be between 0 and 86400 seconds";
  }

  return null;
}

/**
 * Get the config directory, respecting OPENCODE_CONFIG_DIR environment variable
 */
export function getConfigDir(): string {
  return process.env.OPENCODE_CONFIG_DIR ||
    path.join(homedir(), ".config", "opencode");
}

/**
 * Send notification via node-notifier
 */
function sendViaNodeNotifier(options: NotificationOptions): Promise<boolean> {
  return new Promise((resolve) => {
    const notifierOptions: Record<string, unknown> = {
      title: options.title,
      message: options.message,
      sound: options.sound,
      wait: options.wait,
      timeout: options.timeout,
      icon: path.join(getConfigDir(), "notify-icon.png"),
    };

    // node-notifier uses the same urgency values
    if (options.urgency !== "normal") {
      notifierOptions.urgency = options.urgency;
    }

    notifier.notify(notifierOptions, (err: Error | null) => {
      if (err) {
        console.error("node-notifier error:", err.message);
        resolve(false);
      } else {
        resolve(true);
      }
    });
  });
}

/**
 * Send a system notification using node-notifier
 */
async function sendNotification(options: NotificationOptions): Promise<string> {
  const success = await sendViaNodeNotifier(options);

  if (success) {
    return `Notification sent: ${options.title} - ${options.message}`;
  }

  return `Failed to send notification: node-notifier failed. Ensure node-notifier is installed and a notification daemon is running.`;
}

export const send = tool({
  description:
    "Send a system notification to the user using node-notifier package.",
  args: {
    title: tool.schema
      .string()
      .describe("Notification title - the header text of the notification"),
    message: tool.schema
      .string()
      .describe(
        "Notification body - the main content text of the notification",
      ),
    sound: tool.schema
      .boolean()
      .default(true)
      .describe("Whether to play a notification sound"),
    timeout: tool.schema
      .number()
      .default(60)
      .describe("Auto-dismiss timeout in seconds (0 to disable)"),
    urgency: tool.schema
      .enum(["low", "normal", "critical"])
      .default("normal")
      .describe("Urgency/priority level of the notification"),
    wait: tool.schema
      .boolean()
      .default(false)
      .describe("Wait for user interaction before dismissing"),
  },
  async execute(args) {
    try {
      const options: NotificationOptions = {
        title: args.title,
        message: args.message,
        sound: args.sound ?? true,
        timeout: args.timeout ?? 60,
        urgency: args.urgency ?? "normal",
        wait: args.wait ?? false,
      };

      // Validate inputs
      const validationError = validateOptions(options);
      if (validationError) {
        return `Validation error: ${validationError}`;
      }

      return await sendNotification(options);
    } catch (e) {
      const errorMsg = e instanceof Error ? e.message : String(e);
      return `Notification error: ${errorMsg}`;
    }
  },
});