@quecue/dev-notify 0.1.0-alpha.1 → 0.1.0-alpha.3

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.
@@ -0,0 +1,189 @@
1
+ import { readFile, mkdir, open, rename, unlink } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { randomBytes, createHash } from 'node:crypto';
4
+ import { setTimeout as delay } from 'node:timers/promises';
5
+ import { ConfigError, decodeUtf8, loadConfig, ready } from './config.js';
6
+ import { replyHooksReady } from './claude-code.js';
7
+ export const TTL = 24 * 60 * 60 * 1000;
8
+ export const digest = (text) => createHash('sha256').update(text).digest('hex');
9
+ export const botKey = (config) => digest(config.telegram.token);
10
+ export const targetEpoch = (target) => digest(JSON.stringify([target.session, target.pid, target.birth, target.tty]));
11
+ export const policyPath = (config) => config + '.reply.json';
12
+ export const replyDirectory = (config) => config + '.reply-data';
13
+ export async function readJson(file) {
14
+ try {
15
+ const bytes = await readFile(file);
16
+ if (bytes.length > 16 * 1024 * 1024)
17
+ throw new Error();
18
+ return JSON.parse(decodeUtf8(bytes));
19
+ }
20
+ catch (error) {
21
+ if (error.code === 'ENOENT')
22
+ return null;
23
+ throw new ConfigError('无法读取回复配置或状态;未覆盖、未启用远程输入');
24
+ }
25
+ }
26
+ export async function atomicJson(file, value) {
27
+ await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
28
+ const temporary = path.join(path.dirname(file), `.reply-${randomBytes(12).toString('hex')}.tmp`);
29
+ const handle = await open(temporary, 'wx', 0o600);
30
+ try {
31
+ try {
32
+ await handle.writeFile(JSON.stringify(value) + '\n', 'utf8');
33
+ await handle.sync();
34
+ }
35
+ finally {
36
+ await handle.close();
37
+ }
38
+ await rename(temporary, file);
39
+ }
40
+ finally {
41
+ await unlink(temporary).catch(error => { if (error.code !== 'ENOENT')
42
+ throw error; });
43
+ }
44
+ }
45
+ export async function locked(file, work) {
46
+ await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
47
+ let handle;
48
+ for (let attempt = 0; attempt < 100; attempt++) {
49
+ try {
50
+ handle = await open(file, 'wx', 0o600);
51
+ break;
52
+ }
53
+ catch (error) {
54
+ if (error.code !== 'EEXIST')
55
+ throw new ConfigError('无法获取回复状态写入锁');
56
+ if (attempt === 99)
57
+ throw new ConfigError('回复状态锁繁忙;确认进程已退出后才能处理残留锁');
58
+ await delay(40);
59
+ }
60
+ }
61
+ try {
62
+ return await work();
63
+ }
64
+ finally {
65
+ await handle?.close();
66
+ await unlink(file);
67
+ }
68
+ }
69
+ export async function accessReplies(file, env = process.env) {
70
+ const policy = await readJson(policyPath(file));
71
+ if (policy === null || policy.enabled === false)
72
+ return null;
73
+ if (policy.version !== 1 || policy.enabled !== true || typeof policy.bot !== 'string' || !/^[a-f0-9]{64}$/.test(policy.bot)
74
+ || typeof policy.generation !== 'string' || !/^[a-f0-9]{32}$/.test(policy.generation)
75
+ || !Array.isArray(policy.users) || !policy.users.length || policy.users.some((id) => typeof id !== 'string' || !/^[1-9][0-9]*$/.test(id))) {
76
+ throw new ConfigError('回复授权配置无效,拒绝远程输入');
77
+ }
78
+ if (!await replyHooksReady(file, { env }))
79
+ throw new ConfigError('回复所需 hooks 缺失、异步时序不安全或已禁用;请先重新运行 integration claude-code install 并在 /hooks 确认加载');
80
+ const config = await loadConfig(file, true, env);
81
+ if (!ready(config, 'telegram') || botKey(config) !== policy.bot)
82
+ throw new ConfigError('Telegram Bot 已变更或不可用,请重新运行 reply setup 授权');
83
+ const users = policy.users.filter((id) => config.telegram.chatIds.includes(id));
84
+ if (!users.length)
85
+ throw new ConfigError('回复授权用户不在当前 Telegram 接收名单中,拒绝远程输入');
86
+ const effective = { ...policy, users };
87
+ return { file, config, policy: effective, identity: digest(JSON.stringify([policy.bot, policy.generation, users, policy.permissions === true])) };
88
+ }
89
+ export async function stateTransaction(access, work) {
90
+ const directory = replyDirectory(access.file);
91
+ return locked(path.join(directory, 'state.lock'), async () => {
92
+ const policy = await readJson(policyPath(access.file));
93
+ if (!policy?.enabled || policy.bot !== access.policy.bot || policy.generation !== access.policy.generation
94
+ || (policy.permissions === true) !== (access.policy.permissions === true))
95
+ throw new ConfigError('回复授权已撤销或变化,拒绝写入旧状态');
96
+ let state = await readJson(path.join(directory, 'state.json'));
97
+ if (!state || state.identity !== access.identity)
98
+ state = { version: 1, identity: access.identity, offset: 0, sessions: {}, entries: {}, pending: {}, requests: {}, calls: {} };
99
+ const record = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
100
+ state.requests ??= {};
101
+ state.calls ??= {};
102
+ if (state.version !== 1 || !Number.isSafeInteger(state.offset) || state.offset < 0
103
+ || ![state.sessions, state.entries, state.pending, state.requests, state.calls].every(record))
104
+ throw new ConfigError('回复状态结构无效,拒绝远程输入');
105
+ const now = Date.now();
106
+ for (const section of [state.sessions, state.entries, state.pending, state.requests, state.calls])
107
+ for (const [key, value] of Object.entries(section)) {
108
+ if (!record(value) || typeof value.updated !== 'number' || !Number.isFinite(value.updated)
109
+ || value.updated > now + 60000 || now - value.updated > TTL)
110
+ delete section[key];
111
+ }
112
+ for (const [key, value] of Object.entries(state.sessions)) {
113
+ if (value.session !== key || !Number.isSafeInteger(value.pid) || value.pid <= 0 || typeof value.birth !== 'string'
114
+ || typeof value.tty !== 'string' || typeof value.working !== 'boolean')
115
+ delete state.sessions[key];
116
+ }
117
+ for (const [key, value] of Object.entries(state.entries)) {
118
+ if (typeof value.session !== 'string' || typeof value.epoch !== 'string')
119
+ delete state.entries[key];
120
+ }
121
+ for (const [key, value] of Object.entries(state.pending)) {
122
+ if (typeof value.epoch !== 'string' || !/^[1-9][0-9]*$/.test(value.chat) || typeof value.id !== 'string'
123
+ || !['queued', 'submitted', 'unknown'].includes(value.phase))
124
+ delete state.pending[key];
125
+ }
126
+ for (const [key, value] of Object.entries(state.requests)) {
127
+ if (value.id !== key || !['question', 'permission'].includes(value.kind) || !record(value.input)
128
+ || !Array.isArray(value.questions) || !record(value.answers) || !Array.isArray(value.selected)
129
+ || !Number.isFinite(value.expires))
130
+ delete state.requests[key];
131
+ }
132
+ const result = await work(state);
133
+ await atomicJson(path.join(directory, 'state.json'), state);
134
+ return result;
135
+ });
136
+ }
137
+ export async function configureReplies(file, prompts, env = process.env) {
138
+ if (!await replyHooksReady(file, { env }))
139
+ throw new ConfigError('请先运行 integration claude-code install 安装绑定此通知配置的本版完整 hooks,再配置回复');
140
+ const config = await loadConfig(file, true, env);
141
+ if (!ready(config, 'telegram'))
142
+ throw new ConfigError('回复 Claude 需要先配置 Telegram Bot 和私聊接收人');
143
+ const available = config.telegram.chatIds.filter(id => /^[1-9][0-9]*$/.test(id));
144
+ if (!available.length)
145
+ throw new ConfigError('远程回复只允许 Telegram 私聊用户,不支持群聊');
146
+ const before = await readJson(policyPath(file));
147
+ prompts.out('远程回复等同向本机 Claude Code 终端输入并提交内容;不会自动批准工具权限,但可提交 Claude 命令。只授权可信用户。');
148
+ prompts.out('飞书按钮跳转 Telegram;不直接接收飞书消息。需要独占 Bot 的 getUpdates:不得与旧 Python 桥接或另一台机器共用同一个 Bot。');
149
+ prompts.out('Windows 使用系统 PowerShell / 控制台 API;macOS 使用 Ghostty 或 tmux,需要相应系统权限。不依赖 Python。');
150
+ prompts.out('支持范围不含 VS Code 集成终端、WSL、SSH;不确定目标或会话已切换时拒绝输入。');
151
+ prompts.out('通知接收人不会自动获得输入权限。当前可选择的私聊 chat_id:' + available.join('、'));
152
+ const text = await prompts.text('输入本次明确授权的私聊 chat_id(逗号分隔,留空取消):');
153
+ if (!text.trim())
154
+ return false;
155
+ const users = [...new Set(text.split(',').map(id => id.trim()))];
156
+ if (users.some(id => !available.includes(id)))
157
+ throw new ConfigError('只能授权当前通知名单中的明确私聊用户');
158
+ prompts.out('将授权以下用户远程输入:' + users.join('、'));
159
+ prompts.out('结构化问题将展示真实选项并允许自定义回答;普通回复只在本轮结束后输入。问题/选项及可选的工具请求参数可能含敏感信息,将发送到授权私聊和已配置的飞书渠道。');
160
+ const permissions = /^(y|yes)$/i.test(await prompts.text('是否另外允许这些用户逐次点击批准/拒绝工具权限?不修改永久规则。[y/N] '));
161
+ if (!/^(y|yes)$/i.test(await prompts.text('确认启用远程回复并接受上述权限范围?[y/N] ')))
162
+ return false;
163
+ await locked(policyPath(file) + '.lock', async () => {
164
+ if (JSON.stringify(before) !== JSON.stringify(await readJson(policyPath(file))))
165
+ throw new ConfigError('回复授权已被其他进程修改,请重试');
166
+ const effective = await loadConfig(file, true, env);
167
+ if (botKey(effective) !== botKey(config) || users.some(id => !effective.telegram.chatIds.includes(id)))
168
+ throw new ConfigError('通知配置在授权期间已变化,请重试');
169
+ await atomicJson(policyPath(file), { version: 1, enabled: true, bot: botKey(config), users, permissions, generation: randomBytes(16).toString('hex') });
170
+ });
171
+ prompts.out('回复授权已保存;旧消息映射不继承。新回复需从启用后的任务完成通知进入。');
172
+ return true;
173
+ }
174
+ export async function disableReplies(file, prompts) {
175
+ const before = await readJson(policyPath(file));
176
+ if (!before?.enabled) {
177
+ prompts.out('远程回复未启用。');
178
+ return true;
179
+ }
180
+ if (!/^(y|yes)$/i.test(await prompts.text('确认禁用远程回复?通知和凭据保持不变。[y/N] ')))
181
+ return false;
182
+ await locked(policyPath(file) + '.lock', async () => {
183
+ if (JSON.stringify(before) !== JSON.stringify(await readJson(policyPath(file))))
184
+ throw new ConfigError('回复授权已变化,请重试');
185
+ await atomicJson(policyPath(file), { ...before, enabled: false });
186
+ });
187
+ prompts.out('远程回复已禁用;正在进行中的注入不能撤回,桥接下次检查授权时退出。');
188
+ return true;
189
+ }
@@ -0,0 +1,19 @@
1
+ import { requestJson } from './channels.js';
2
+ import { ConfigError } from './config.js';
3
+ export function telegramApi(token, request = requestJson) {
4
+ return async (method, payload) => {
5
+ if (!['sendMessage', 'editMessageReplyMarkup', 'answerCallbackQuery', 'getMe', 'getUpdates', 'getWebhookInfo'].includes(method))
6
+ throw new ConfigError('不支持的 Telegram 桥接方法');
7
+ const response = await request(`https://api.telegram.org/bot${token}/${method}`, payload);
8
+ if (response.ok !== true) {
9
+ if (response.error_code === 409)
10
+ throw new ConfigError('Telegram 轮询冲突;检查旧 Python 桥接、其他机器或 webhook,桥接不会抢占或删除 webhook');
11
+ throw new ConfigError('Telegram 桥接请求失败;不输出服务端正文或秘密');
12
+ }
13
+ return response.result;
14
+ };
15
+ }
16
+ export async function replyHint(api, chat, title, content, replyTo, markup) {
17
+ return api('sendMessage', { chat_id: chat, text: title + '\n\n' + content,
18
+ ...(replyTo ? { reply_parameters: { message_id: replyTo } } : {}), ...(markup ? { reply_markup: markup } : {}) });
19
+ }
package/dist/setup.js CHANGED
@@ -130,7 +130,7 @@ export async function wizard(file, options = {}) {
130
130
  const { out } = prompts;
131
131
  out(`配置路径:${file}`);
132
132
  out('密钥隐藏输入并存入 Windows Credential Manager / macOS Keychain;JSON 只保存引用。');
133
- out('不安装 hooks,不启用远程控制;只有额外同意测试时才发送消息。');
133
+ out('本步骤仅配置渠道;保存后可另行确认启用 Claude Code 通知,不启用远程控制。只有额外同意测试时才发送测试消息。');
134
134
  if (before && !yes(await prompts.text('已有配置,继续编辑?[y/N] ')))
135
135
  return false;
136
136
  const selection = await prompts.text('选择渠道:1 Telegram,2 飞书,3 两者;其他输入取消:');
package/package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "name": "@quecue/dev-notify",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.3",
4
4
  "description": "跨平台开发任务通知工具,支持 Telegram 与飞书",
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/QueCue/dev-notify.git"
9
+ },
10
+ "homepage": "https://github.com/QueCue/dev-notify#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/QueCue/dev-notify/issues"
13
+ },
6
14
  "type": "module",
7
15
  "engines": {
8
16
  "node": ">=22.0.0"
@@ -14,6 +22,7 @@
14
22
  "dist/*.js",
15
23
  "README.md",
16
24
  "SECURITY.md",
25
+ "CHANGELOG.md",
17
26
  "LICENSE"
18
27
  ],
19
28
  "scripts": {