@tavlo-dsh/dsh-plugin-hooks 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +29 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +5 -0
- package/index.js +793 -0
- package/lib/client.js +5 -0
- package/lib/client.js.LEGAL.txt +4 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +72 -4
- package/src/config-store.js +804 -0
- package/src/default-scripts.js +193 -0
- package/src/dsh-hook-bridge.js +999 -0
- package/src/event-hub.js +198 -0
- package/src/hook-catalog.js +359 -0
- package/src/notification-dispatcher.js +486 -0
- package/src/script-runner.js +671 -0
- package/src/sound-manager.js +403 -0
- package/src/system-notifier.js +549 -0
- package/src/third-party-providers.js +1137 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Materializes cross-platform default Node ESM hook scripts under `$DSH_HOME/hooks/scripts/default/`.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { randomBytes } from 'node:crypto'
|
|
6
|
+
import { chmod, mkdir, rename, stat, unlink, writeFile } from 'node:fs/promises'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
|
|
9
|
+
function buildNotifyScript({ header, level }) {
|
|
10
|
+
return `// ${header}
|
|
11
|
+
// Cross-platform DSH Hook script. Reads JSON payload from STDIN and outputs a directive JSON line.
|
|
12
|
+
// Return { "suppress": true } to suppress notification for matching events, or customize title/summary.
|
|
13
|
+
|
|
14
|
+
import { readFileSync } from 'node:fs'
|
|
15
|
+
|
|
16
|
+
let payload = {}
|
|
17
|
+
try {
|
|
18
|
+
const raw = readFileSync(0, 'utf8').trim()
|
|
19
|
+
if (raw) payload = JSON.parse(raw)
|
|
20
|
+
} catch {
|
|
21
|
+
// Ignore malformed STDIN
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const directive = {
|
|
25
|
+
notify: true,
|
|
26
|
+
level: ${JSON.stringify(level)},
|
|
27
|
+
hookPoint: payload.hookPoint || process.env.DSH_HOOK_POINT || '',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
process.stdout.write(JSON.stringify(directive) + '\\n')
|
|
31
|
+
`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const AUDIT_LOG_SCRIPT = `// DSH Hook Tool Audit Example Script
|
|
35
|
+
// Writes a compact, redacted JSONL audit entry under $DSH_HOME/hooks/logs/tool-audit.jsonl.
|
|
36
|
+
// Outputs { "suppress": true } by default so routine tool calls do not trigger notification spam;
|
|
37
|
+
// remove "suppress": true if you want every audited tool call to notify.
|
|
38
|
+
|
|
39
|
+
import { appendFileSync, mkdirSync, readFileSync } from 'node:fs'
|
|
40
|
+
import path from 'node:path'
|
|
41
|
+
|
|
42
|
+
let payload = {}
|
|
43
|
+
try {
|
|
44
|
+
const raw = readFileSync(0, 'utf8').trim()
|
|
45
|
+
if (raw) payload = JSON.parse(raw)
|
|
46
|
+
} catch {
|
|
47
|
+
// Ignore malformed STDIN
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const logsDir = path.resolve(import.meta.dirname, '../../logs')
|
|
51
|
+
mkdirSync(logsDir, { recursive: true })
|
|
52
|
+
|
|
53
|
+
const entry = {
|
|
54
|
+
timestamp: payload.timestamp || new Date().toISOString(),
|
|
55
|
+
eventId: payload.eventId || process.env.DSH_HOOK_EVENT_ID || null,
|
|
56
|
+
hookPoint: payload.hookPoint || process.env.DSH_HOOK_POINT || 'tool.post',
|
|
57
|
+
category: payload.category || process.env.DSH_HOOK_CATEGORY || 'UserPromptSubmit',
|
|
58
|
+
sessionId: payload.sessionId || process.env.DSH_HOOK_SESSION_ID || null,
|
|
59
|
+
projectName: payload.projectName || process.env.DSH_HOOK_PROJECT || null,
|
|
60
|
+
toolName: payload.toolName || null,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
appendFileSync(path.join(logsDir, 'tool-audit.jsonl'), JSON.stringify(entry) + '\\n', 'utf8')
|
|
64
|
+
|
|
65
|
+
process.stdout.write(JSON.stringify({
|
|
66
|
+
notify: false,
|
|
67
|
+
suppress: true,
|
|
68
|
+
audited: true,
|
|
69
|
+
summary: entry.toolName ? \`Audited tool: \${entry.toolName}\` : 'Tool audited',
|
|
70
|
+
}) + '\\n')
|
|
71
|
+
`
|
|
72
|
+
|
|
73
|
+
export const DEFAULT_SCRIPTS = Object.freeze([
|
|
74
|
+
Object.freeze({
|
|
75
|
+
id: 'notify-approval',
|
|
76
|
+
filename: 'notify-approval.mjs',
|
|
77
|
+
nameZh: '待审批通知脚本',
|
|
78
|
+
nameEn: 'Approval Notification Script',
|
|
79
|
+
enabledByDefault: true,
|
|
80
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Approval Request', level: 'approval' }),
|
|
81
|
+
}),
|
|
82
|
+
Object.freeze({
|
|
83
|
+
id: 'notify-attention',
|
|
84
|
+
filename: 'notify-attention.mjs',
|
|
85
|
+
nameZh: '需要关注通知脚本',
|
|
86
|
+
nameEn: 'Attention Notification Script',
|
|
87
|
+
enabledByDefault: true,
|
|
88
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Needs Attention', level: 'attention' }),
|
|
89
|
+
}),
|
|
90
|
+
Object.freeze({
|
|
91
|
+
id: 'notify-task-done',
|
|
92
|
+
filename: 'notify-task-done.mjs',
|
|
93
|
+
nameZh: '任务完成通知脚本',
|
|
94
|
+
nameEn: 'Task Completed Notification Script',
|
|
95
|
+
enabledByDefault: true,
|
|
96
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Task Completed', level: 'success' }),
|
|
97
|
+
}),
|
|
98
|
+
Object.freeze({
|
|
99
|
+
id: 'notify-task-failed',
|
|
100
|
+
filename: 'notify-task-failed.mjs',
|
|
101
|
+
nameZh: '任务失败通知脚本',
|
|
102
|
+
nameEn: 'Task Failed Notification Script',
|
|
103
|
+
enabledByDefault: true,
|
|
104
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Task Failed', level: 'error' }),
|
|
105
|
+
}),
|
|
106
|
+
Object.freeze({
|
|
107
|
+
id: 'notify-job-settled',
|
|
108
|
+
filename: 'notify-job-settled.mjs',
|
|
109
|
+
nameZh: '后台任务结束通知脚本',
|
|
110
|
+
nameEn: 'Background Job Settled Script',
|
|
111
|
+
enabledByDefault: false,
|
|
112
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Job Settled', level: 'info' }),
|
|
113
|
+
}),
|
|
114
|
+
Object.freeze({
|
|
115
|
+
id: 'notify-session-start',
|
|
116
|
+
filename: 'notify-session-start.mjs',
|
|
117
|
+
nameZh: '会话启动通知脚本',
|
|
118
|
+
nameEn: 'Session Start Notification Script',
|
|
119
|
+
enabledByDefault: false,
|
|
120
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Session Start', level: 'info' }),
|
|
121
|
+
}),
|
|
122
|
+
Object.freeze({
|
|
123
|
+
id: 'notify-subagent-end',
|
|
124
|
+
filename: 'notify-subagent-end.mjs',
|
|
125
|
+
nameZh: '子 Agent 完成通知脚本',
|
|
126
|
+
nameEn: 'Subagent Finished Notification Script',
|
|
127
|
+
enabledByDefault: false,
|
|
128
|
+
content: buildNotifyScript({ header: 'DSH Default Hook: Subagent Finished', level: 'success' }),
|
|
129
|
+
}),
|
|
130
|
+
Object.freeze({
|
|
131
|
+
id: 'audit-log-example',
|
|
132
|
+
filename: 'audit-log-example.mjs',
|
|
133
|
+
nameZh: '工具审计日志示例脚本',
|
|
134
|
+
nameEn: 'Tool Audit Log Example Script',
|
|
135
|
+
enabledByDefault: false,
|
|
136
|
+
content: AUDIT_LOG_SCRIPT,
|
|
137
|
+
}),
|
|
138
|
+
])
|
|
139
|
+
|
|
140
|
+
const DEFAULT_SCRIPT_MAP = new Map(DEFAULT_SCRIPTS.map((item) => [item.id, item]))
|
|
141
|
+
|
|
142
|
+
export function getDefaultScriptById(id) {
|
|
143
|
+
return typeof id === 'string' ? DEFAULT_SCRIPT_MAP.get(id) : undefined
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getDefaultScriptsDir(dataDir) {
|
|
147
|
+
if (typeof dataDir !== 'string' || !dataDir.trim()) {
|
|
148
|
+
throw new TypeError('dataDir must be a non-empty string')
|
|
149
|
+
}
|
|
150
|
+
return path.join(dataDir, 'hooks', 'scripts', 'default')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function atomicWriteText(targetPath, content) {
|
|
154
|
+
const tmpPath = `${targetPath}.tmp-${process.pid}-${randomBytes(6).toString('hex')}`
|
|
155
|
+
try {
|
|
156
|
+
await writeFile(tmpPath, content, 'utf8')
|
|
157
|
+
await rename(tmpPath, targetPath)
|
|
158
|
+
} catch (error) {
|
|
159
|
+
await unlink(tmpPath).catch(() => {})
|
|
160
|
+
throw error
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Materialize all 8 built-in `.mjs` scripts into `$DSH_HOME/hooks/scripts/default/`.
|
|
166
|
+
* @param {string} dataDir
|
|
167
|
+
* @param {{ overwrite?: boolean }} [opts]
|
|
168
|
+
*/
|
|
169
|
+
export async function ensureDefaultScripts(dataDir, { overwrite = false } = {}) {
|
|
170
|
+
const dir = getDefaultScriptsDir(dataDir)
|
|
171
|
+
await mkdir(dir, { recursive: true })
|
|
172
|
+
const materialized = []
|
|
173
|
+
for (const script of DEFAULT_SCRIPTS) {
|
|
174
|
+
const filePath = path.join(dir, script.filename)
|
|
175
|
+
let exists = false
|
|
176
|
+
if (!overwrite) {
|
|
177
|
+
try {
|
|
178
|
+
const info = await stat(filePath)
|
|
179
|
+
exists = info.isFile()
|
|
180
|
+
} catch {
|
|
181
|
+
exists = false
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (overwrite || !exists) {
|
|
185
|
+
await atomicWriteText(filePath, script.content)
|
|
186
|
+
if (process.platform !== 'win32') {
|
|
187
|
+
await chmod(filePath, 0o700).catch(() => {})
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
materialized.push(filePath)
|
|
191
|
+
}
|
|
192
|
+
return materialized
|
|
193
|
+
}
|