@xiaoqiong0v0/opencode-sound-notify 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -0
- package/assets/default.wav +0 -0
- package/dist/sound-notify.js +131 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# opencode-sound-notify
|
|
2
|
+
|
|
3
|
+
OpenCode 插件:在会话事件触发时播放提示音,让用户无需盯着屏幕也能感知状态变化。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @xiaoqiong0v0/opencode-sound-notify
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
在 `opencode.json` 中添加:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"plugin": ["@xiaoqiong0v0/opencode-sound-notify"]
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 配置
|
|
20
|
+
|
|
21
|
+
首次加载自动生成配置文件 `~/.config/opencode/sound-notify.jsonc`:
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
{
|
|
25
|
+
// 提示音文件路径,留空使用内置 default.wav
|
|
26
|
+
"sound": "",
|
|
27
|
+
// 触发提示音的事件列表
|
|
28
|
+
"events": ["session.idle", "session.error", "permission.asked"],
|
|
29
|
+
// 全局默认防抖间隔(ms),未单独配置的事件使用此值
|
|
30
|
+
"defaultDebounceMs": 30000,
|
|
31
|
+
// 按事件单独配置防抖间隔(ms),覆盖 defaultDebounceMs
|
|
32
|
+
"debounceMs": {
|
|
33
|
+
"session.idle": 10000,
|
|
34
|
+
"session.error": 5000
|
|
35
|
+
},
|
|
36
|
+
// 语言: "zh" 或 "en"
|
|
37
|
+
"lang": "en",
|
|
38
|
+
// 是否启用
|
|
39
|
+
"enabled": true
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
| 字段 | 默认值 | 说明 |
|
|
44
|
+
|------|--------|------|
|
|
45
|
+
| `sound` | 内置 `assets/default.wav` | 自定义 .wav 路径 |
|
|
46
|
+
| `events` | `["session.idle", "session.error", "permission.asked"]` | 触发事件列表 |
|
|
47
|
+
| `defaultDebounceMs` | `30000` | 全局默认防抖间隔 |
|
|
48
|
+
| `debounceMs` | `{}` | 按事件单独配置,key=事件名,value=防抖间隔(ms) |
|
|
49
|
+
| `lang` | `"en"` | 语言设置 |
|
|
50
|
+
| `enabled` | `true` | 总开关 |
|
|
51
|
+
|
|
52
|
+
## 支持的事件
|
|
53
|
+
|
|
54
|
+
| 事件 | 说明 |
|
|
55
|
+
|------|------|
|
|
56
|
+
| `session.idle` | 会话空闲(任务完成) |
|
|
57
|
+
| `session.error` | 会话出错 |
|
|
58
|
+
| `permission.asked` | 权限询问 |
|
|
59
|
+
| `session.created` | 新会话创建 |
|
|
60
|
+
| `session.updated` | 会话更新 |
|
|
61
|
+
| `session.status` | 会话状态变更 |
|
|
62
|
+
|
|
63
|
+
## 测试
|
|
64
|
+
|
|
65
|
+
通过 `sound_notify` 工具验证配置是否正常:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
sound_notify action=test
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 平台支持
|
|
72
|
+
|
|
73
|
+
| 平台 | 播放方式 |
|
|
74
|
+
|------|----------|
|
|
75
|
+
| Windows | PowerShell SoundPlayer |
|
|
76
|
+
| macOS | `afplay` |
|
|
77
|
+
| Linux | `paplay` (PulseAudio) / `aplay` (ALSA) |
|
|
Binary file
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { join, dirname } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import createLogger from "@xiaoqiong0v0/opencode-plugin-logger";
|
|
6
|
+
const CONFIG_DIR = process.env.HOME || process.env.USERPROFILE || "";
|
|
7
|
+
const CONFIG_PATH = join(CONFIG_DIR, ".config/opencode", "sound-notify.jsonc");
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = dirname(__filename);
|
|
10
|
+
const ASSET_DIR = join(__dirname, "..", "assets");
|
|
11
|
+
const DEFAULT_SOUND = join(ASSET_DIR, "default.wav");
|
|
12
|
+
const log = createLogger("sound-notify");
|
|
13
|
+
const SAMPLE_CFG = `{
|
|
14
|
+
// 提示音文件路径,留空使用内置 default.wav
|
|
15
|
+
"sound": "",
|
|
16
|
+
// 触发提示音的事件列表
|
|
17
|
+
"events": ["session.idle", "session.error", "permission.asked"],
|
|
18
|
+
// 全局默认防抖间隔(ms),未单独配置的事件使用此值
|
|
19
|
+
"defaultDebounceMs": 30000,
|
|
20
|
+
// 按事件单独配置防抖间隔(ms),覆盖 defaultDebounceMs
|
|
21
|
+
"debounceMs": {
|
|
22
|
+
"permission.asked": 5000
|
|
23
|
+
},
|
|
24
|
+
// 语言: "zh" 或 "en"
|
|
25
|
+
"lang": "en",
|
|
26
|
+
// 是否启用
|
|
27
|
+
"enabled": true
|
|
28
|
+
}
|
|
29
|
+
`;
|
|
30
|
+
let LANG = "en";
|
|
31
|
+
const TX = {
|
|
32
|
+
init_failed: { zh: "初始化失败,使用默认配置", en: "Init failed, using defaults" },
|
|
33
|
+
play_error: { zh: "播放失败", en: "Playback failed" },
|
|
34
|
+
unsupported_platform: { zh: "不支持的播放平台: {platform}", en: "Unsupported platform: {platform}" },
|
|
35
|
+
no_sound_file: { zh: "声音文件不存在: {path}", en: "Sound file not found: {path}" },
|
|
36
|
+
};
|
|
37
|
+
const T = (key, params) => {
|
|
38
|
+
const entry = TX[key] || { zh: key, en: key };
|
|
39
|
+
const t = LANG === "zh" ? entry.zh : entry.en;
|
|
40
|
+
if (!params)
|
|
41
|
+
return t;
|
|
42
|
+
return Object.entries(params).reduce((s, [k, v]) => s.replace(`{${k}}`, v), t);
|
|
43
|
+
};
|
|
44
|
+
let cfg = {};
|
|
45
|
+
let soundPath = DEFAULT_SOUND;
|
|
46
|
+
function readJsonc(path) {
|
|
47
|
+
const raw = readFileSync(path, "utf-8").replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
48
|
+
return JSON.parse(raw);
|
|
49
|
+
}
|
|
50
|
+
function loadCfg() {
|
|
51
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
52
|
+
const dir = dirname(CONFIG_PATH);
|
|
53
|
+
if (!existsSync(dir))
|
|
54
|
+
mkdirSync(dir, { recursive: true });
|
|
55
|
+
writeFileSync(CONFIG_PATH, SAMPLE_CFG, "utf-8");
|
|
56
|
+
}
|
|
57
|
+
const raw = readJsonc(CONFIG_PATH);
|
|
58
|
+
cfg = {
|
|
59
|
+
sound: raw.sound || "",
|
|
60
|
+
events: raw.events,
|
|
61
|
+
defaultDebounceMs: raw.defaultDebounceMs || 30000,
|
|
62
|
+
debounceMs: raw.debounceMs,
|
|
63
|
+
enabled: raw.enabled !== false,
|
|
64
|
+
};
|
|
65
|
+
soundPath = cfg.sound && existsSync(cfg.sound) ? cfg.sound : DEFAULT_SOUND;
|
|
66
|
+
LANG = raw.lang === "zh" ? "zh" : "en";
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
loadCfg();
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
log.error(T("init_failed"), e instanceof Error ? e : Error(String(e)));
|
|
73
|
+
}
|
|
74
|
+
const eventTimestamps = new Map();
|
|
75
|
+
function getPlayerArgs(sound) {
|
|
76
|
+
switch (process.platform) {
|
|
77
|
+
case "win32":
|
|
78
|
+
return ["powershell", "-NoProfile", "-Command", `(New-Object System.Media.SoundPlayer '${sound}').PlaySync()`];
|
|
79
|
+
case "darwin":
|
|
80
|
+
return ["afplay", sound];
|
|
81
|
+
case "linux": {
|
|
82
|
+
if (existsSync("/usr/bin/paplay"))
|
|
83
|
+
return ["paplay", sound];
|
|
84
|
+
if (existsSync("/usr/bin/aplay"))
|
|
85
|
+
return ["aplay", sound];
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
default:
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function play() {
|
|
93
|
+
if (!existsSync(soundPath)) {
|
|
94
|
+
log.error(T("no_sound_file", { path: soundPath }));
|
|
95
|
+
return Promise.resolve(false);
|
|
96
|
+
}
|
|
97
|
+
const args = getPlayerArgs(soundPath);
|
|
98
|
+
if (!args) {
|
|
99
|
+
log.error(T("unsupported_platform", { platform: process.platform }));
|
|
100
|
+
return Promise.resolve(false);
|
|
101
|
+
}
|
|
102
|
+
const [cmd, ...cmdArgs] = args;
|
|
103
|
+
const ps = spawn(cmd, cmdArgs, { stdio: "ignore", windowsHide: true });
|
|
104
|
+
return new Promise((resolve) => {
|
|
105
|
+
ps.on("error", (err) => { log.error(T("play_error"), err); resolve(false); });
|
|
106
|
+
ps.on("close", (code) => { resolve(code === 0); });
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function shouldPlay(eventType) {
|
|
110
|
+
if (!cfg.enabled)
|
|
111
|
+
return false;
|
|
112
|
+
if (!cfg.events?.includes(eventType))
|
|
113
|
+
return false;
|
|
114
|
+
const now = Date.now();
|
|
115
|
+
const debounceMs = cfg.debounceMs?.[eventType] ?? cfg.defaultDebounceMs ?? 30000;
|
|
116
|
+
const last = eventTimestamps.get(eventType) || 0;
|
|
117
|
+
if (now - last < debounceMs)
|
|
118
|
+
return false;
|
|
119
|
+
eventTimestamps.set(eventType, now);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
export const SoundNotify = async () => {
|
|
123
|
+
log.loaded();
|
|
124
|
+
return {
|
|
125
|
+
event: async ({ event }) => {
|
|
126
|
+
if (shouldPlay(event.type)) {
|
|
127
|
+
play();
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xiaoqiong0v0/opencode-sound-notify",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "OpenCode plugin: play notification sounds on session events (idle, error, permission asked).",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"assets",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/sound-notify.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./dist/sound-notify.js"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc && node -e \"const fs=require('fs'),path=require('path'); const copyDir=(s,d)=>{fs.mkdirSync(d,{recursive:true});fs.readdirSync(s).forEach(f=>{const ss=path.join(s,f),dd=path.join(d,f);fs.statSync(ss).isDirectory()?copyDir(ss,dd):fs.copyFileSync(ss,dd)})}; fs.mkdirSync('.opencode/plugins',{recursive:true}); fs.copyFileSync('dist/sound-notify.js', '.opencode/plugins/sound-notify.js'); copyDir('assets','.opencode/assets')\"",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/xiaoqiong0v0/opencode-sound-notify.git"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/xiaoqiong0v0/opencode-sound-notify#readme",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/xiaoqiong0v0/opencode-sound-notify/issues"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@xiaoqiong0v0/opencode-plugin-logger": "^1.0.2"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@opencode-ai/plugin": "^1.18.4",
|
|
33
|
+
"@types/node": "^26.1.1",
|
|
34
|
+
"typescript": "^7.0.2"
|
|
35
|
+
}
|
|
36
|
+
}
|