73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import type { Plugin } from "@opencode-ai/plugin"
|
|
|
|
/**
|
|
* 通知 Plugin
|
|
* 在会话完成或发生错误时发送系统通知
|
|
*/
|
|
export const NotificationPlugin: Plugin = async ({ project, client, $ }) => {
|
|
// 检测操作系统
|
|
const platform = process.platform
|
|
|
|
/**
|
|
* 发送系统通知
|
|
* @param title 通知标题
|
|
* @param message 通知内容
|
|
* @param isError 是否为错误通知
|
|
*/
|
|
const sendNotification = async (title: string, message: string, isError: boolean = false) => {
|
|
try {
|
|
if (platform === "darwin") {
|
|
// macOS - 使用 osascript
|
|
const sound = isError ? ' sound name "Basso"' : ''
|
|
await $`osascript -e 'display notification "${message}" with title "${title}"${sound}'`
|
|
} else if (platform === "linux") {
|
|
// Linux - 使用 notify-send
|
|
const urgency = isError ? "-u critical" : ""
|
|
await $`notify-send "${title}" "${message}" ${urgency}`
|
|
} else if (platform === "win32") {
|
|
// Windows - 使用 PowerShell
|
|
const script = `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('${message}', '${title}')`
|
|
await $`powershell -Command "& {${script}}"`
|
|
}
|
|
|
|
// 记录日志
|
|
await client.app.log({
|
|
service: "notification",
|
|
level: "info",
|
|
message: `已发送通知: ${title} - ${message}`,
|
|
})
|
|
} catch (error) {
|
|
// 如果通知发送失败,记录错误但不中断流程
|
|
await client.app.log({
|
|
service: "notification",
|
|
level: "error",
|
|
message: `发送通知失败: ${error}`,
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
event: async ({ event }) => {
|
|
const projectName = project?.name || "OpenCode"
|
|
|
|
// 会话完成时发送通知
|
|
if (event.type === "session.idle") {
|
|
await sendNotification(
|
|
projectName,
|
|
"会话已完成!",
|
|
false
|
|
)
|
|
}
|
|
|
|
// 会话错误时发送通知
|
|
if (event.type === "session.error") {
|
|
await sendNotification(
|
|
projectName,
|
|
"会话发生错误!",
|
|
true
|
|
)
|
|
}
|
|
},
|
|
}
|
|
}
|