dsh-repo-watch 0.1.2 → 0.2.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 CHANGED
@@ -42,15 +42,15 @@
42
42
  推的是**一条报数**,不是内容倾泻——在手机上两秒能看完,要不要看细节由你决定:
43
43
 
44
44
  ```
45
- 🔔 有 4 条新消息
45
+ 有 4 条新消息(来源:GitHub)
46
46
 
47
47
  · 需要你回复 4 条 — 有人回复了你提的 issue/PR、@ 了你,或请你 review
48
48
 
49
- 回复「看」查看全部,「看1」只看需要你回复的。
50
- 最近一条:awesome-dsh-plugin/awesome-dsh-plugin#15938
49
+ 回复「查看」查看全部明细;「查看 1」只看需要你回复的。
50
+ 最近一条:awesome-dsh-plugin/awesome-dsh-plugin#15938 Add: two IM channel plugins…(PR)
51
51
  ```
52
52
 
53
- 回「看」之后,会话里给出带原话的明细(同一条只在聊天里展开,不再二次推送):
53
+ 回「查看」之后,会话里给出带原话的明细(同一条只在聊天里展开,不再二次推送):
54
54
 
55
55
  ```
56
56
  共 4 条,显示 4 条:
@@ -68,9 +68,20 @@
68
68
  - [x] 占位发布(0.0.1)
69
69
  - [x] 轮询 + 水位 + 状态机(0.1.0)
70
70
  - [x] 通过飞书/微信通道推送
71
- - [x] 中文报数式推送 + 「看」展开明细(0.1.2)
71
+ - [x] 中文报数式推送 + 「查看」展开明细(0.1.2)
72
+ - [x] 「其他动静」也会留一份,供 `quiet` 摘要查看(0.2.0)
73
+ - [x] `snooze` 生效:标记「稍后再说」的条目在到期前不再提醒(0.2.0)
74
+ - [x] 插件图标与本地化文案(0.2.0)
72
75
  - [ ] `deep` 摘要(分类 + 优先级 + 相似历史)
73
- - [ ] 会话内快捷指令(`/watch done #646`)
76
+ - [ ] 多账号 / 组织仓库
77
+
78
+ ## 三种摘要
79
+
80
+ | 摘要 | 内容 | 怎么触发 |
81
+ |---|---|---|
82
+ | `waiting` | 需要你回复或处理的条目——报数推送里说的就是它 | 回「查看」 |
83
+ | `quiet` | **不需要你动手、但确实发生了**的事:别人在你订阅的 thread 里讨论、机器人提交等,留 30 天 | 让我「看看其他动静」 |
84
+ | `deep` | 在 `waiting` 基础上带状态、提醒次数等内部字段 | 排查时用 |
74
85
 
75
86
  ## 许可
76
87
 
package/icon.svg ADDED
@@ -0,0 +1,19 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Repository watchtower">
2
+ <defs>
3
+ <linearGradient id="tower" x1="0" y1="0" x2="1" y2="1">
4
+ <stop offset="0" stop-color="#2f6f4f"/>
5
+ <stop offset="1" stop-color="#1d4a34"/>
6
+ </linearGradient>
7
+ </defs>
8
+ <rect x="4" y="4" width="56" height="56" rx="14" fill="url(#tower)"/>
9
+ <!-- watchtower roof -->
10
+ <path d="M32 14 L44 24 H20 Z" fill="#e8f2ec"/>
11
+ <!-- tower body -->
12
+ <rect x="24" y="24" width="16" height="20" rx="2" fill="#e8f2ec"/>
13
+ <!-- watch window -->
14
+ <circle cx="32" cy="32" r="4.5" fill="#2f6f4f"/>
15
+ <!-- base -->
16
+ <rect x="18" y="44" width="28" height="5" rx="2.5" fill="#e8f2ec"/>
17
+ <!-- the one lit dot: someone is waiting -->
18
+ <circle cx="46" cy="18" r="4.5" fill="#f2b134"/>
19
+ </svg>
package/index.mjs CHANGED
@@ -134,14 +134,18 @@ function shortDay(at) {
134
134
  * It is a roll-call, not a dump. Listing every thread with its excerpt felt wrong
135
135
  * in practice: a push is read on a phone in two seconds, and four paragraphs of
136
136
  * somebody else's comment is more than that moment can absorb. So the message says
137
- * how many there are, split by what they want from you, and how to ask for the
138
- * detail — which is one reply away, in the same chat.
137
+ * how many there are, where they come from, split by what they want from you, and
138
+ * how to ask for the detail — which is one reply away, in the same chat.
139
+ *
140
+ * Plain text by design: no emoji, no decoration. The wording says 查看 rather than
141
+ * 看 because a one-character prompt reads like a typo, even though short replies
142
+ * are exactly what the digest accepts.
139
143
  */
140
144
  function composeWaitingMessage(entries) {
141
145
  const buckets = { reply: [], action: [], other: [] }
142
146
  for (const entry of entries) buckets[bucketOf(entry.reason)].push(entry)
143
147
  const total = entries.length
144
- const head = total === 1 ? '🔔 有 1 条新消息' : `🔔 有 ${total} 条新消息`
148
+ const head = `有 ${total} 条新消息(来源:GitHub)`
145
149
  const lines = [head, '']
146
150
  for (const key of ['reply', 'action', 'other']) {
147
151
  const count = buckets[key].length
@@ -149,17 +153,44 @@ function composeWaitingMessage(entries) {
149
153
  lines.push(`· ${bucketText(key)} ${count} 条 — ${bucketHint(key)}`)
150
154
  }
151
155
  lines.push('')
152
- const hints = []
153
- if (buckets.reply.length) hints.push('「看1」只看需要你回复的')
154
- if (buckets.action.length) hints.push('「看2」只看需要你处理的')
155
- lines.push(`回复「看」查看全部${hints.length ? `,${hints.join(',')}` : ''}。`)
156
+ const hints = ['回复「查看」查看全部明细']
157
+ if (buckets.reply.length) hints.push('「查看 1」只看需要你回复的')
158
+ if (buckets.action.length) hints.push('「查看 2」只看需要你处理的')
159
+ lines.push(`${hints.join(';')}。`)
156
160
  const newest = entries
157
161
  .slice()
158
162
  .sort((a, b) => Number(b.lastActivityAt ?? 0) - Number(a.lastActivityAt ?? 0))[0]
159
- if (newest) lines.push(`最近一条:${newest.key}`)
163
+ if (newest) {
164
+ const kind = String(newest.thread?.kind ?? newest.kind ?? '')
165
+ const kindText = kind === 'pr' ? '(PR)' : kind === 'issue' ? '(issue)' : ''
166
+ const title = newest.title ? ` ${newest.title}` : ''
167
+ lines.push(`最近一条:${newest.key}${title}${kindText}`)
168
+ }
160
169
  return lines.join('\n')
161
170
  }
162
171
 
172
+ /**
173
+ * Fold newly noticed activity into the stored list.
174
+ *
175
+ * One entry per thread (the newest wins), bounded, and pruned by age: this list
176
+ * exists so "what else moved" can be answered, not as an archive.
177
+ */
178
+ function mergeActivity(previous, noticed, now) {
179
+ const byKey = new Map()
180
+ for (const item of Array.isArray(previous) ? previous : []) {
181
+ if (item?.key) byKey.set(item.key, item)
182
+ }
183
+ for (const item of noticed) {
184
+ const existing = byKey.get(item.key)
185
+ if (!existing || Number(item.at ?? 0) >= Number(existing.at ?? 0)) byKey.set(item.key, item)
186
+ }
187
+ const keepMs = 30 * 24 * 60 * 60 * 1000
188
+ return [...byKey.values()]
189
+ .filter((item) => now - Number(item.at ?? 0) <= keepMs)
190
+ .sort((a, b) => Number(b.at ?? 0) - Number(a.at ?? 0))
191
+ .slice(0, 200)
192
+ }
193
+
163
194
  /**
164
195
  * One poll: notifications (someone acted toward me) + repo scan (someone opened
165
196
  * something of mine). Both paths feed the same judgement and the same state file.
@@ -234,14 +265,31 @@ async function pollOnce(ctx, config) {
234
265
  }
235
266
 
236
267
  const fresh = []
268
+ const noticed = []
237
269
  for (const candidate of candidates) {
238
- if (!candidate.verdict.pending) continue
270
+ if (!candidate.verdict.pending) {
271
+ // Activity that does not need an answer is still activity: it is what the
272
+ // "quiet" digest is for, and throwing it away would make that digest empty.
273
+ if (!/^(i-spoke-last|already-read|closed|mine)$/.test(candidate.verdict.reason)) {
274
+ noticed.push({
275
+ key: candidate.key,
276
+ title: candidate.title,
277
+ actor: candidate.actor,
278
+ text: candidate.snippet,
279
+ reason: candidate.verdict.reason,
280
+ at: candidate.lastActivityAt,
281
+ })
282
+ }
283
+ continue
284
+ }
239
285
  const previous = state.threads[candidate.key]
240
286
  const notify = shouldNotify({
241
287
  previous: previous?.state,
242
288
  pending: true,
243
289
  lastActivityAt: candidate.lastActivityAt,
244
290
  stateSince: Number(previous?.stateSince ?? 0),
291
+ snoozeUntil: Number(previous?.snoozeUntil ?? 0),
292
+ now,
245
293
  })
246
294
  upsertThread(state, candidate.key, {
247
295
  kind: candidate.thread.kind,
@@ -255,6 +303,7 @@ async function pollOnce(ctx, config) {
255
303
  if (notify) fresh.push({ ...candidate, reason: candidate.verdict.reason })
256
304
  }
257
305
 
306
+ state.activity = mergeActivity(state.activity, noticed, now)
258
307
  state.watermark = now
259
308
  pruneThreads(state, now)
260
309
  saveState(config ?? {}, state)
@@ -350,8 +399,9 @@ const digestTool = (config) => ({
350
399
  name: 'repo_watch_digest',
351
400
  description:
352
401
  'List the threads the watchtower is tracking, with who spoke last and what they said. '
353
- + 'Call this whenever the user asks to see the details after a watchtower push — a reply of '
354
- + '"看" / "详情" / "show me" means bucket "all"; "看1" means bucket "reply"; "看2" means "action". '
402
+ + 'Call this whenever the user asks to see the detail after a watchtower push: "查看", "详情", '
403
+ + '"展开", "看", "列表" all mean bucket "all"; "查看 1" / "看1" means bucket "reply"; '
404
+ + '"查看 2" / "看2" means "action". Users reply briefly, so match on the intent, not on an exact word. '
355
405
  + 'kind="waiting" (default) returns what needs the user, "quiet" returns the rest, "deep" adds '
356
406
  + 'the stored reason and last actor for each thread.',
357
407
  parameters: {
@@ -377,6 +427,22 @@ const digestTool = (config) => ({
377
427
  const limit = Math.max(1, Number(args?.limit ?? 20))
378
428
  const wanted = String(args?.kind ?? 'waiting')
379
429
  const bucket = String(args?.bucket ?? 'all')
430
+
431
+ // `quiet` answers "what else moved", which is activity, not threads: those
432
+ // entries did not become pending, so they are not in the state machine.
433
+ if (wanted === 'quiet') {
434
+ const items = (Array.isArray(state.activity) ? state.activity : []).slice(0, limit)
435
+ if (items.length === 0) return { ok: true, text: '最近没有其他动静。' }
436
+ const lines = [`最近 ${items.length} 条其他动静:`]
437
+ items.forEach((item, index) => {
438
+ lines.push('')
439
+ lines.push(`${index + 1}. ${item.key}${item.title ? ` ${item.title}` : ''}`)
440
+ lines.push(` ${reasonText(item.reason)}${item.actor ? ` · ${item.actor}` : ''}${item.at ? `(${shortDay(item.at)})` : ''}`)
441
+ if (item.text) lines.push(` ${item.text}`)
442
+ })
443
+ return { ok: true, text: lines.join('\n') }
444
+ }
445
+
380
446
  const rows = Object.entries(state.threads)
381
447
  .filter(([, thread]) => (wanted === 'waiting' ? thread.state === 'pending' : thread.state !== 'pending'))
382
448
  .filter(([, thread]) => bucket === 'all' || bucketOf(thread.reason) === bucket)
@@ -468,4 +534,4 @@ const checkTool = (ctx, config) => ({
468
534
  })
469
535
 
470
536
  /** Exposed for the smoke test: wiring a delay keeps timers testable. */
471
- export const _internals = { pollOnce, pushText, runtime, delay, composeWaitingMessage }
537
+ export const _internals = { pollOnce, pushText, runtime, delay, composeWaitingMessage, mergeActivity }
package/lib/state.mjs CHANGED
@@ -19,9 +19,9 @@ export const MAX_THREADS = 2000
19
19
  export const statePath = (config = {}) =>
20
20
  String(config.stateFile ?? '').trim() || join(dshHome(), 'repo-watch.json')
21
21
 
22
- /** @returns {{ version: number, watermark: number, threads: Record<string, object> }} */
22
+ /** @returns {{ version: number, watermark: number, threads: Record<string, object>, activity: object[] }} */
23
23
  export function emptyState() {
24
- return { version: STATE_VERSION, watermark: 0, threads: {} }
24
+ return { version: STATE_VERSION, watermark: 0, threads: {}, activity: [] }
25
25
  }
26
26
 
27
27
  export function loadState(config = {}) {
@@ -32,6 +32,7 @@ export function loadState(config = {}) {
32
32
  version: STATE_VERSION,
33
33
  watermark: Number(parsed.watermark ?? 0),
34
34
  threads: parsed.threads && typeof parsed.threads === 'object' ? parsed.threads : {},
35
+ activity: Array.isArray(parsed.activity) ? parsed.activity : [],
35
36
  }
36
37
  } catch {
37
38
  return emptyState()
package/lib/triage.mjs CHANGED
@@ -172,10 +172,14 @@ export function judgeRepoItem({ issue, me }) {
172
172
  * @param {boolean} input.pending the fresh judgement
173
173
  * @param {number} [input.lastActivityAt] activity that triggered this judgement
174
174
  * @param {number} [input.stateSince] when the stored state was entered
175
+ * @param {number} [input.snoozeUntil] silence this thread until then
176
+ * @param {number} [input.now] clock, so the snooze rule is testable
175
177
  * @returns {boolean}
176
178
  */
177
- export function shouldNotify({ previous, pending, lastActivityAt = 0, stateSince = 0 }) {
179
+ export function shouldNotify({ previous, pending, lastActivityAt = 0, stateSince = 0, snoozeUntil = 0, now = Date.now() }) {
178
180
  if (!pending) return false
181
+ // A snoozed thread stays pending — it still needs an answer, just not now.
182
+ if (Number(snoozeUntil) > Number(now)) return false
179
183
  if (previous === undefined) return true
180
184
  if (previous === 'done') return Number(lastActivityAt) > Number(stateSince)
181
185
  return false
package/locale/en.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Repository watchtower",
4
+ "description": "Watches your GitHub repositories in the background and speaks up only when someone is actually waiting on you: replies to your threads, mentions, review requests, and issues opened in your repos."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "仓库守望",
4
+ "description": "在后台盯着你的 GitHub 仓库,只在真的有人等你时才说一句:你提的 issue/PR 有人回复、有人 @ 你、请你 review,或者有人给你的仓库开了 issue。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-repo-watch",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "description": "DSH repository watchtower: it watches your GitHub repos in the background and only speaks up when someone is actually waiting on you.",
6
6
  "license": "MIT",
@@ -11,12 +11,16 @@
11
11
  "main": "index.mjs",
12
12
  "exports": {
13
13
  ".": "./index.mjs",
14
+ "./locale/*.json": "./locale/*.json",
15
+ "./icon": "./icon.svg",
14
16
  "./package.json": "./package.json"
15
17
  },
16
18
  "files": [
17
19
  "index.mjs",
18
20
  "lib/*.mjs",
19
21
  "cordis.patch.yml",
22
+ "locale/*.json",
23
+ "icon.svg",
20
24
  "test/*.mjs",
21
25
  "README.md",
22
26
  "LICENSE"
@@ -32,6 +36,6 @@
32
36
  "node": ">=20.18.1"
33
37
  },
34
38
  "scripts": {
35
- "test": "node --test test/triage.test.mjs"
39
+ "test": "node --test test/triage.test.mjs test/message.test.mjs"
36
40
  }
37
41
  }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * The push message is the whole product surface for most users, so its wording is
3
+ * pinned here: no emoji, an explicit source, a readable prompt, and the type of the
4
+ * newest thread spelled out.
5
+ */
6
+ import test from 'node:test'
7
+ import assert from 'node:assert/strict'
8
+
9
+ import { _internals } from '../index.mjs'
10
+
11
+ const { composeWaitingMessage, mergeActivity } = _internals
12
+
13
+ const entry = (over = {}) => ({
14
+ key: 'owner/repo#1',
15
+ title: 'Some title',
16
+ thread: { kind: 'issue' },
17
+ reason: 'someone-replied-to-my-thread',
18
+ actor: 'them',
19
+ snippet: 'hello',
20
+ lastActivityAt: 1_700_000_000_000,
21
+ url: 'https://github.com/owner/repo/issues/1',
22
+ ...over,
23
+ })
24
+
25
+ test('the roll-call carries no emoji', () => {
26
+ const text = composeWaitingMessage([entry(), entry({ key: 'owner/repo#2' })])
27
+ assert.doesNotMatch(text, /\p{Extended_Pictographic}/u)
28
+ })
29
+
30
+ test('the header counts the messages and names the source', () => {
31
+ const text = composeWaitingMessage([entry(), entry({ key: 'owner/repo#2' })])
32
+ assert.match(text, /^有 2 条新消息(来源:GitHub)$/m)
33
+
34
+ const single = composeWaitingMessage([entry()])
35
+ assert.match(single, /^有 1 条新消息(来源:GitHub)$/m)
36
+ })
37
+
38
+ test('the prompt asks for 查看 while still accepting a short reply', () => {
39
+ const text = composeWaitingMessage([entry()])
40
+ assert.match(text, /回复「查看」查看全部明细/)
41
+ // 该字单独出现时不算提示,避免让人以为只能回一个字
42
+ assert.doesNotMatch(text, /回复「看」/)
43
+ })
44
+
45
+ test('bucket lines explain what each group wants', () => {
46
+ const text = composeWaitingMessage([
47
+ entry(),
48
+ entry({ key: 'owner/repo#2', reason: 'external-issue' }),
49
+ ])
50
+ assert.match(text, /需要你回复 1 条/)
51
+ assert.match(text, /需要你处理 1 条/)
52
+ assert.match(text, /「查看 1」/)
53
+ assert.match(text, /「查看 2」/)
54
+ })
55
+
56
+ test('the newest line names the thread type', () => {
57
+ const issueText = composeWaitingMessage([entry()])
58
+ assert.match(issueText, /最近一条:owner\/repo#1 Some title(issue)/)
59
+
60
+ const prText = composeWaitingMessage([entry({ thread: { kind: 'pr' } })])
61
+ assert.match(prText, /最近一条:owner\/repo#1 Some title(PR)/)
62
+ })
63
+
64
+ test('the newest line is the most recent thread', () => {
65
+ const text = composeWaitingMessage([
66
+ entry({ key: 'owner/repo#old', lastActivityAt: 1_000 }),
67
+ entry({ key: 'owner/repo#new', lastActivityAt: 9_000 }),
68
+ ])
69
+ assert.match(text, /最近一条:owner\/repo#new/)
70
+ })
71
+
72
+ test('quiet activity keeps one entry per thread, newest wins', () => {
73
+ const now = 10_000_000
74
+ const merged = mergeActivity(
75
+ [{ key: 'a#1', at: now - 100, title: 'old' }, { key: 'b#2', at: now - 200, title: 'b' }],
76
+ [{ key: 'a#1', at: now - 10, title: 'new' }, { key: 'c#3', at: now - 50, title: 'c' }],
77
+ now,
78
+ )
79
+ assert.equal(merged.length, 3)
80
+ assert.equal(merged[0].key, 'a#1', 'newest first')
81
+ assert.equal(merged[0].title, 'new', 'newest wins for the same thread')
82
+ assert.deepEqual(merged.map((item) => item.key), ['a#1', 'c#3', 'b#2'])
83
+ })
84
+
85
+ test('quiet activity forgets what is older than a month', () => {
86
+ const now = 10_000_000_000
87
+ const merged = mergeActivity(
88
+ [{ key: 'stale#1', at: now - 40 * 24 * 3600_000 }],
89
+ [{ key: 'fresh#2', at: now - 1000 }],
90
+ now,
91
+ )
92
+ assert.deepEqual(merged.map((item) => item.key), ['fresh#2'])
93
+ })
@@ -83,6 +83,23 @@ test('announce only when a thread enters pending or reopens after done', () => {
83
83
  assert.equal(shouldNotify({ previous: 'pending', pending: false }), false)
84
84
  })
85
85
 
86
+ test('a snoozed thread stays pending but stays quiet', () => {
87
+ const now = 1_000_000
88
+ // 首次出现也按 snooze 静默:snooze 的语义是"先别告诉我"
89
+ assert.equal(shouldNotify({ previous: undefined, pending: true, snoozeUntil: now + 60_000, now }), false)
90
+ assert.equal(shouldNotify({ previous: 'done', pending: true, lastActivityAt: 20, stateSince: 10, snoozeUntil: now + 60_000, now }), false)
91
+ // 到期后照常提醒
92
+ assert.equal(shouldNotify({ previous: undefined, pending: true, snoozeUntil: now - 1, now }), true)
93
+ })
94
+
95
+ test('a done thread still reopens when new activity arrives after snooze', () => {
96
+ const now = 1_000_000
97
+ assert.equal(
98
+ shouldNotify({ previous: 'done', pending: true, lastActivityAt: 90, stateSince: 10, snoozeUntil: now - 1, now }),
99
+ true,
100
+ )
101
+ })
102
+
86
103
  test('quiet hours wrap around midnight', () => {
87
104
  assert.equal(inQuietHours(23, '23-8'), true)
88
105
  assert.equal(inQuietHours(3, '23-8'), true)