@xiaoqiong0v0/opencode-sound-notify 1.1.1 → 1.1.6

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 CHANGED
@@ -33,6 +33,8 @@ npm install @xiaoqiong0v0/opencode-sound-notify
33
33
  "events": ["session.idle", "session.error", "permission.asked"],
34
34
  // 全局默认防抖间隔(ms),未单独配置的事件使用此值
35
35
  "defaultDebounceMs": 30000,
36
+ // 全局最小间隔(ms),所有声音播放不能低于此间隔,覆盖单独配置
37
+ "minIntervalMs": 3000,
36
38
  // 按事件单独配置防抖间隔(ms),覆盖 defaultDebounceMs
37
39
  "debounceMs": {
38
40
  "permission.asked": 5000
@@ -50,6 +52,7 @@ npm install @xiaoqiong0v0/opencode-sound-notify
50
52
  | `sounds` | `{}` | 按事件单独配置,key=事件名,value=wav 路径 |
51
53
  | `events` | `["session.idle", "session.error", "permission.asked"]` | 触发事件列表 |
52
54
  | `defaultDebounceMs` | `30000` | 全局默认防抖间隔 |
55
+ | `minIntervalMs` | `3000` | 全局最小间隔,所有声音不能小于此值,设为 0 关闭 |
53
56
  | `debounceMs` | `{}` | 按事件单独配置,key=事件名,value=防抖间隔(ms) |
54
57
  | `lang` | `"en"` | 语言设置 |
55
58
  | `enabled` | `true` | 总开关 |
@@ -73,13 +76,13 @@ npm install @xiaoqiong0v0/opencode-sound-notify
73
76
  | `session.updated` | 会话更新 |
74
77
  | `session.status` | 会话状态变更 |
75
78
 
76
- ## 测试
79
+ ## 运行时开关
77
80
 
78
- 通过 `sound_notify` 工具验证配置是否正常:
81
+ 不写配置,重启恢复。支持三种方式:
79
82
 
80
- ```
81
- sound_notify action=test
82
- ```
83
+ 1. **对话**:对 AI 说"关声音" / "mute" / "开声音" / "声音开关"
84
+ 2. **命令**:在 TUI 输入 `/sound-toggle on|off|toggle`
85
+ 3. **工具**:AI 自动调用 `sound_toggle`
83
86
 
84
87
  ## 平台支持
85
88
 
@@ -1,3 +1,4 @@
1
+ import { tool } from "@opencode-ai/plugin";
1
2
  import { spawn } from "node:child_process";
2
3
  import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
3
4
  import { join, dirname } from "node:path";
@@ -23,6 +24,8 @@ const SAMPLE_CFG = `{
23
24
  "events": ["session.idle", "session.error", "permission.asked"],
24
25
  // 全局默认防抖间隔(ms),未单独配置的事件使用此值
25
26
  "defaultDebounceMs": 30000,
27
+ // 全局最小间隔(ms),所有声音播放不能低于此间隔,覆盖单独配置
28
+ "minIntervalMs": 3000,
26
29
  // 按事件单独配置防抖间隔(ms),覆盖 defaultDebounceMs
27
30
  "debounceMs": {
28
31
  "permission.asked": 5000
@@ -34,11 +37,16 @@ const SAMPLE_CFG = `{
34
37
  }
35
38
  `;
36
39
  let LANG = "en";
40
+ let muted = false;
37
41
  const TX = {
38
42
  init_failed: { zh: "初始化失败,使用默认配置", en: "Init failed, using defaults" },
39
43
  play_error: { zh: "播放失败", en: "Playback failed" },
40
44
  unsupported_platform: { zh: "不支持的播放平台: {platform}", en: "Unsupported platform: {platform}" },
41
45
  no_sound_file: { zh: "声音文件不存在: {path}", en: "Sound file not found: {path}" },
46
+ muted_on: { zh: "声音已关闭", en: "Sound muted" },
47
+ muted_off: { zh: "声音已开启", en: "Sound unmuted" },
48
+ mute_desc: { zh: "开/关提示音", en: "Toggle notification sound" },
49
+ mute_template: { zh: "用户想要切换声音状态。调用 sound_toggle 工具。", en: "User wants to toggle sound. Call sound_toggle tool." },
42
50
  };
43
51
  const T = (key, params) => {
44
52
  const entry = TX[key] || { zh: key, en: key };
@@ -64,6 +72,7 @@ function loadCfg() {
64
72
  sound: raw.sound || "",
65
73
  sounds: raw.sounds,
66
74
  events: raw.events,
75
+ minIntervalMs: raw.minIntervalMs ?? 3000,
67
76
  defaultDebounceMs: raw.defaultDebounceMs || 30000,
68
77
  debounceMs: raw.debounceMs,
69
78
  enabled: raw.enabled !== false,
@@ -77,6 +86,7 @@ catch (e) {
77
86
  log.error(T("init_failed"), e instanceof Error ? e : Error(String(e)));
78
87
  }
79
88
  const eventTimestamps = new Map();
89
+ let globalLastPlay = 0;
80
90
  function getPlayerArgs(sound) {
81
91
  switch (process.platform) {
82
92
  case "win32":
@@ -125,25 +135,66 @@ function play(eventType) {
125
135
  });
126
136
  }
127
137
  function shouldPlay(eventType) {
138
+ if (muted)
139
+ return false;
128
140
  if (!cfg.enabled)
129
141
  return false;
130
142
  if (!cfg.events?.includes(eventType))
131
143
  return false;
132
144
  const now = Date.now();
145
+ const minMs = cfg.minIntervalMs || 0;
133
146
  const debounceMs = cfg.debounceMs?.[eventType] ?? cfg.defaultDebounceMs ?? 30000;
147
+ const effective = minMs > debounceMs ? minMs : debounceMs;
148
+ // 全局间隔检查:任何声音播放后 minIntervalMs 内都不再响应
149
+ if (minMs > 0 && now - globalLastPlay < minMs)
150
+ return false;
134
151
  const last = eventTimestamps.get(eventType) || 0;
135
- if (now - last < debounceMs)
152
+ if (now - last < effective)
136
153
  return false;
137
154
  eventTimestamps.set(eventType, now);
155
+ globalLastPlay = now;
138
156
  return true;
139
157
  }
140
158
  export const SoundNotify = async () => {
141
159
  log.loaded();
142
160
  return {
161
+ config: async (config) => {
162
+ log.hook("config", "register command sound-toggle");
163
+ try {
164
+ const commands = config.command ?? {};
165
+ commands["sound-toggle"] = {
166
+ description: T("mute_desc"),
167
+ template: T("mute_template"),
168
+ };
169
+ config.command = commands;
170
+ log.info("sound-toggle command registered");
171
+ }
172
+ catch (e) {
173
+ log.error("command registration failed", e instanceof Error ? e : Error(String(e)));
174
+ }
175
+ },
143
176
  event: async ({ event }) => {
144
177
  if (shouldPlay(event.type)) {
145
178
  play(event.type);
146
179
  }
147
180
  },
181
+ tool: {
182
+ sound_toggle: tool({
183
+ description: T("mute_desc"),
184
+ args: {
185
+ action: tool.schema.enum(["on", "off", "toggle"]).optional().describe("on=开启 off=关闭 toggle=切换"),
186
+ },
187
+ execute: async ({ action }) => {
188
+ if (action === "on")
189
+ muted = false;
190
+ else if (action === "off")
191
+ muted = true;
192
+ else
193
+ muted = !muted;
194
+ return T(muted ? "muted_on" : "muted_off");
195
+ },
196
+ }),
197
+ },
148
198
  };
149
199
  };
200
+ export default SoundNotify;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiaoqiong0v0/opencode-sound-notify",
3
- "version": "1.1.1",
3
+ "version": "1.1.6",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin: play notification sounds on session events (idle, error, permission asked).",
6
6
  "files": [
@@ -13,7 +13,7 @@
13
13
  ".": "./dist/sound-notify.js"
14
14
  },
15
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')\"",
16
+ "build": "tsc",
17
17
  "prepublishOnly": "npm run build"
18
18
  },
19
19
  "repository": {
@@ -28,8 +28,10 @@
28
28
  "dependencies": {
29
29
  "@xiaoqiong0v0/opencode-plugin-logger": "^1.0.2"
30
30
  },
31
+ "peerDependencies": {
32
+ "@opencode-ai/plugin": "^1.18.0"
33
+ },
31
34
  "devDependencies": {
32
- "@opencode-ai/plugin": "^1.18.4",
33
35
  "@types/node": "^26.1.1",
34
36
  "typescript": "^7.0.2"
35
37
  }