project-brain 0.44.0 → 0.45.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/README.md +79 -11
- package/package.json +6 -6
- package/templates/claude-tui/activity-pane/hooks/feat-agents.tsx +179 -0
- package/templates/claude-tui/activity-pane/hooks/feat-blocked.tsx +232 -0
- package/templates/claude-tui/activity-pane/hooks/feat-ci.tsx +400 -0
- package/templates/claude-tui/activity-pane/hooks/feat-context.tsx +283 -0
- package/templates/claude-tui/activity-pane/hooks/feat-notify.tsx +145 -0
- package/templates/claude-tui/activity-pane/hooks/feat-progress.tsx +413 -0
- package/templates/claude-tui/activity-pane/hooks/feat-running.tsx +528 -0
- package/templates/claude-tui/activity-pane/hooks/feat-savings.tsx +533 -0
- package/templates/claude-tui/activity-pane/hooks/register.tsx +336 -177
- package/templates/claude-tui/activity-pane/types/index.d.ts +83 -0
- package/templates/claude-tui/context-gate/.claude-plugin/plugin.json +7 -0
- package/templates/claude-tui/context-gate/hooks/hooks.json +1 -0
- package/templates/claude-tui/context-gate/hooks/register.tsx +355 -0
- package/templates/claude-tui/context-gate/types/index.d.ts +28 -0
- package/templates/claude-tui/skill-listing/.claude-plugin/plugin.json +7 -0
- package/templates/claude-tui/skill-listing/hooks/hooks.json +1 -0
- package/templates/claude-tui/skill-listing/hooks/register.tsx +300 -0
- package/templates/claude-tui/skill-listing/types/index.d.ts +22 -0
- package/templates/model-routing.md +10 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import type { Hook } from 'claude-code'
|
|
2
|
+
import { atom, read, update } from 'claude-code'
|
|
3
|
+
import type { Register } from 'claude-code'
|
|
4
|
+
|
|
5
|
+
import type { Api, Row, Span } from './register'
|
|
6
|
+
import { clean, fit, ICONS, span } from './register'
|
|
7
|
+
|
|
8
|
+
/** One task line of an ODD task doc. */
|
|
9
|
+
export type ProgressTask = { id: string; title: string; done: boolean; route?: string; commit?: string }
|
|
10
|
+
|
|
11
|
+
/** What the progress card draws, parsed from `odd/tasks/<feature>.md`. */
|
|
12
|
+
export type ProgressDoc = {
|
|
13
|
+
feature: string
|
|
14
|
+
path: string
|
|
15
|
+
title: string
|
|
16
|
+
objective?: string
|
|
17
|
+
tasks: ProgressTask[]
|
|
18
|
+
done: number
|
|
19
|
+
total: number
|
|
20
|
+
current?: ProgressTask
|
|
21
|
+
pending: string[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The stored card: the doc it came from (path + mtime, to skip re-parsing) and the parse, absent when nothing shows. */
|
|
25
|
+
export type ProgressState = { path: string; mtimeMs: number; doc?: ProgressDoc }
|
|
26
|
+
|
|
27
|
+
export type ProgressPart = { doc?: ProgressDoc }
|
|
28
|
+
|
|
29
|
+
const CAP_BYTES = 256 * 1024
|
|
30
|
+
const MAX_TASKS = 200
|
|
31
|
+
const MAX_PENDING = 5
|
|
32
|
+
const TEXT_MAX = 240
|
|
33
|
+
const BAR_CELLS = 10
|
|
34
|
+
const RUN_TIMEOUT_MS = 3000
|
|
35
|
+
|
|
36
|
+
const progressState = atom({ plugin: 'project-brain-activity', key: 'progress' } as const, { path: '', mtimeMs: 0 } as ProgressState, { shape: 'progress-1' })
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------- parser
|
|
39
|
+
|
|
40
|
+
const TASK_HEADING = /^(#{2,3})\s+(?:tasks|tareas|checklist|work units|units|unidades)\b/i
|
|
41
|
+
const LOG_HEADING = /^(#{2,3})\s+(?:log|progress|progreso|registro|bit[aá]cora)\b/i
|
|
42
|
+
const OBJECTIVE_HEADING = /^#{2,3}\s+(?:objective|objetivo)\b/i
|
|
43
|
+
const OBJECTIVE_LINE = /^\**(?:objective|objetivo)\**\s*:\**\s*(.+)$/i
|
|
44
|
+
const CHECKBOX = /^(\s*)[-*+]\s+\[([ xX~-])\]\s+(.*)$/
|
|
45
|
+
const BULLET_ID = /^(\s*)[-*+]\s+(.*)$/
|
|
46
|
+
const ID = /^\**([A-Z][A-Za-z]{0,3}-?\d+(?:\.\d+)?[a-z]?)\**(?=[\s:.)—–-]|$)/
|
|
47
|
+
const COMMIT = /(?:commit\s+`?|`)([0-9a-f]{7,40})\b/i
|
|
48
|
+
const ROUTE = /\b(?:route|ruta)\s*:?\s*\**\s*(inline|delegated|delegada|delegado)/i
|
|
49
|
+
const ROUTE_PAREN = /\((inline|delegated|delegada|delegado)\b/i
|
|
50
|
+
const DONE_STATUS = /^\W*(?:done|hecho|hecha|completed?|completad[oa]|ok|x)\b|^[✓✔✅]/i
|
|
51
|
+
const DROPPED_STATUS = /^\W*(?:dropped|descartad[oa]|cancel)/i
|
|
52
|
+
const DONE_WORD = /\b(?:done|hecho|hecha|completad[oa])\b|✅/gi
|
|
53
|
+
const NEGATED = /\b(?:not(?:\s+yet)?|no|pending|todo|sin|falta(?:n)?)\s+(?:\w+\s+)?$/i
|
|
54
|
+
const TRAILING_SHAS = /\s+((?:[0-9a-f]{7,12})(?:\s*,\s*[0-9a-f]{7,12})*)\s*[.;]?\s*$/i
|
|
55
|
+
|
|
56
|
+
/** A plain line counts as done when one of its done words carries no negation right before it. */
|
|
57
|
+
export const saysDone = (text: string): boolean =>
|
|
58
|
+
[...text.matchAll(DONE_WORD)].some(match => !NEGATED.test(text.slice(0, match.index)))
|
|
59
|
+
|
|
60
|
+
/** The SHAs a done line ends with, such as `T1 (S1-S3) c64a223, 78e3bfd.` */
|
|
61
|
+
const trailingShas = (text: string): { shas: string[]; at: number } | undefined => {
|
|
62
|
+
const match = TRAILING_SHAS.exec(text)
|
|
63
|
+
if (!match) return undefined
|
|
64
|
+
const shas = (match[1] ?? '').split(/\s*,\s*/).filter(one => /\d/.test(one))
|
|
65
|
+
return shas.length > 0 ? { shas, at: match.index } : undefined
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Skipped, pending or not-run checks, Spanish and English. */
|
|
69
|
+
const PENDING = new RegExp(
|
|
70
|
+
[
|
|
71
|
+
String.raw`\bno\s+(?:se\s+)?(?:corri[oó]|corrieron|(?:ejecut|verific|prob)(?:ad[oa]s?|[oó]|aron)\b)`,
|
|
72
|
+
String.raw`\bsin\s+(?:probar|verificar|correr|ejecutar)`,
|
|
73
|
+
String.raw`\bpendiente`,
|
|
74
|
+
String.raw`\bomitid[oa]s?\b`,
|
|
75
|
+
String.raw`\bfalta(?:n)?\s+(?:probar|verificar|correr)`,
|
|
76
|
+
String.raw`\bnot\s+(?:yet\s+)?(?:run|ran|executed|verified|tested|checked)\b`,
|
|
77
|
+
String.raw`\bdid(?:\s+not|n['’]t)\s+run\b`,
|
|
78
|
+
String.raw`\b(?:untested|unverified)\b`,
|
|
79
|
+
String.raw`\b(?:tests?|checks?|specs?|e2e|suites?|verifications?|smoke)\s+(?:were\s+|was\s+)?skipped\b`,
|
|
80
|
+
String.raw`\bskipped\s+(?:the\s+)?(?:tests?|checks?|specs?|e2e|suites?|verification|smoke)\b`,
|
|
81
|
+
String.raw`\bpending\b(?!-)`,
|
|
82
|
+
].join('|'),
|
|
83
|
+
'i',
|
|
84
|
+
)
|
|
85
|
+
const NOT_PENDING = /\b(?:nothing|nada|ningun[oa]?|no|0)\s+(?:\w+\s+)?(?:pending|pendientes?)\b/i
|
|
86
|
+
|
|
87
|
+
const oneLine = (raw: string): string => clean(raw).replace(/\s+/g, ' ').trim()
|
|
88
|
+
const clip = (raw: string): string => {
|
|
89
|
+
const line = oneLine(raw)
|
|
90
|
+
return line.length <= TEXT_MAX ? line : `${line.slice(0, TEXT_MAX - 1)}…`
|
|
91
|
+
}
|
|
92
|
+
const unbold = (raw: string): string => raw.replace(/\*\*/g, '')
|
|
93
|
+
|
|
94
|
+
type Section = { lines: string[] }
|
|
95
|
+
|
|
96
|
+
/** The lines under the first heading matching `heading`, up to the next heading of the same or higher level. */
|
|
97
|
+
const sectionOf = (lines: string[], heading: RegExp, last = false): Section | undefined => {
|
|
98
|
+
let found: Section | undefined
|
|
99
|
+
for (let index = 0; index < lines.length; index++) {
|
|
100
|
+
const match = heading.exec(lines[index] ?? '')
|
|
101
|
+
if (!match) continue
|
|
102
|
+
const level = (match[1] ?? '##').length
|
|
103
|
+
const body: string[] = []
|
|
104
|
+
for (let next = index + 1; next < lines.length; next++) {
|
|
105
|
+
const line = lines[next] ?? ''
|
|
106
|
+
const head = /^(#{1,6})\s/.exec(line)
|
|
107
|
+
if (head && (head[1] ?? '').length <= level) break
|
|
108
|
+
body.push(line)
|
|
109
|
+
}
|
|
110
|
+
found = { lines: body }
|
|
111
|
+
if (!last) return found
|
|
112
|
+
}
|
|
113
|
+
return found
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const taskFrom = (body: string, index: number, done: boolean): ProgressTask => {
|
|
117
|
+
const text = unbold(body).trim()
|
|
118
|
+
const idMatch = ID.exec(text)
|
|
119
|
+
const id = idMatch?.[1] ?? `#${index + 1}`
|
|
120
|
+
const tagged = COMMIT.exec(text)?.[1]
|
|
121
|
+
const bare = done && tagged === undefined ? trailingShas(text) : undefined
|
|
122
|
+
const described = bare === undefined ? text : text.slice(0, bare.at)
|
|
123
|
+
const rest = idMatch ? described.slice(idMatch[0].length) : described
|
|
124
|
+
const title = clip(rest.replace(/^[\s:.)—–-]+/, '').replace(/\s*(?:[.;]\s*)?\b(?:route|ruta)\s*:.*$/i, '')) || id
|
|
125
|
+
const route = ROUTE.exec(text)?.[1] ?? ROUTE_PAREN.exec(text)?.[1]
|
|
126
|
+
const commit = tagged ?? bare?.shas[0]
|
|
127
|
+
const task: ProgressTask = { id, title, done }
|
|
128
|
+
if (route !== undefined) task.route = route.toLowerCase().startsWith('deleg') ? 'delegated' : 'inline'
|
|
129
|
+
if (commit !== undefined && /\d/.test(commit)) task.commit = commit
|
|
130
|
+
return task
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const cellsOf = (line: string): string[] => line.trim().replace(/^\|/, '').replace(/\|$/, '').split('|').map(one => one.trim())
|
|
134
|
+
|
|
135
|
+
const tableTasks = (lines: string[]): ProgressTask[] | undefined => {
|
|
136
|
+
const table = lines.filter(line => line.trim().startsWith('|'))
|
|
137
|
+
if (table.length < 3) return undefined
|
|
138
|
+
const header = cellsOf(table[0] ?? '').map(one => unbold(one).toLowerCase())
|
|
139
|
+
const column = (names: RegExp): number => header.findIndex(one => names.test(one))
|
|
140
|
+
const idAt = column(/^(?:id|#|task|tarea)$/)
|
|
141
|
+
const statusAt = column(/^(?:status|estado)$/)
|
|
142
|
+
if (statusAt < 0) return undefined
|
|
143
|
+
const titleAt = column(/^(?:unit|unidad|title|t[ií]tulo|task|tarea|descripci[oó]n|description|what|qu[eé])$/)
|
|
144
|
+
const routeAt = column(/^(?:route|ruta)$/)
|
|
145
|
+
const commitAt = column(/^commit$/)
|
|
146
|
+
const tasks: ProgressTask[] = []
|
|
147
|
+
for (const line of table.slice(1)) {
|
|
148
|
+
if (/^\|?\s*:?-{2,}/.test(line.trim())) continue
|
|
149
|
+
const cells = cellsOf(line)
|
|
150
|
+
const status = cells[statusAt] ?? ''
|
|
151
|
+
if (DROPPED_STATUS.test(status)) continue
|
|
152
|
+
const id = unbold(cells[idAt >= 0 ? idAt : 0] ?? '').trim() || `#${tasks.length + 1}`
|
|
153
|
+
const fallbackAt = idAt >= 0 ? idAt + 1 : 1
|
|
154
|
+
const titleCell = titleAt >= 0 && titleAt !== idAt ? titleAt : [statusAt, routeAt, commitAt].includes(fallbackAt) ? -1 : fallbackAt
|
|
155
|
+
const title = (titleCell >= 0 ? clip(unbold(cells[titleCell] ?? '')) : '') || clip(id)
|
|
156
|
+
const task: ProgressTask = { id: clip(id), title, done: DONE_STATUS.test(status) }
|
|
157
|
+
const route = routeAt >= 0 ? cells[routeAt] ?? '' : ''
|
|
158
|
+
if (/deleg/i.test(route)) task.route = 'delegated'
|
|
159
|
+
else if (/inline/i.test(route)) task.route = 'inline'
|
|
160
|
+
const commit = commitAt >= 0 ? /\b([0-9a-f]{7,40})\b/.exec(cells[commitAt] ?? '')?.[1] : undefined
|
|
161
|
+
if (commit !== undefined && /\d/.test(commit)) task.commit = commit
|
|
162
|
+
tasks.push(task)
|
|
163
|
+
}
|
|
164
|
+
return tasks.length > 0 ? tasks : undefined
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const checkboxTasks = (lines: string[]): ProgressTask[] | undefined => {
|
|
168
|
+
const boxes = lines.map(line => CHECKBOX.exec(line)).filter((one): one is RegExpExecArray => one !== null)
|
|
169
|
+
if (boxes.length === 0) return undefined
|
|
170
|
+
const indent = Math.min(...boxes.map(one => (one[1] ?? '').length))
|
|
171
|
+
const tasks: ProgressTask[] = []
|
|
172
|
+
for (const box of boxes) {
|
|
173
|
+
if ((box[1] ?? '').length !== indent) continue
|
|
174
|
+
const mark = box[2] ?? ' '
|
|
175
|
+
if (mark === '-') continue
|
|
176
|
+
tasks.push(taskFrom(box[3] ?? '', tasks.length, mark === 'x' || mark === 'X'))
|
|
177
|
+
}
|
|
178
|
+
return tasks
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Plain `- T1 …` bullets with no checkbox: done only when the line says so. */
|
|
182
|
+
const bulletTasks = (lines: string[]): ProgressTask[] | undefined => {
|
|
183
|
+
const tasks: ProgressTask[] = []
|
|
184
|
+
for (const line of lines) {
|
|
185
|
+
const bullet = BULLET_ID.exec(line)
|
|
186
|
+
if (!bullet || (bullet[1] ?? '').length > 0) continue
|
|
187
|
+
if (!ID.test(unbold(bullet[2] ?? ''))) continue
|
|
188
|
+
tasks.push(taskFrom(bullet[2] ?? '', tasks.length, saysDone(bullet[2] ?? '')))
|
|
189
|
+
}
|
|
190
|
+
return tasks.length > 0 ? tasks : undefined
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const objectiveOf = (lines: string[]): string | undefined => {
|
|
194
|
+
for (const line of lines.slice(0, 12)) {
|
|
195
|
+
const match = OBJECTIVE_LINE.exec(line.trim())
|
|
196
|
+
if (match?.[1]) return clip(match[1])
|
|
197
|
+
}
|
|
198
|
+
const section = sectionOf(lines, OBJECTIVE_HEADING)
|
|
199
|
+
const first = section?.lines.find(line => line.trim() !== '')
|
|
200
|
+
return first === undefined ? undefined : clip(first)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const LOG_PREFIX = /^(?:[A-Z]\d+\s*)?\(?\d{4}-\d{2}-\d{2}\)?:?/
|
|
204
|
+
const EXCERPT_BEFORE = 40
|
|
205
|
+
const EXCERPT_AFTER = 110
|
|
206
|
+
|
|
207
|
+
/** A long log line as its L#/date prefix plus the words around the pending marker, so the red line says what did not run. */
|
|
208
|
+
export const excerptOf = (line: string): string => {
|
|
209
|
+
const flat = oneLine(line)
|
|
210
|
+
const match = PENDING.exec(flat)
|
|
211
|
+
if (match === null || flat.length <= EXCERPT_BEFORE + EXCERPT_AFTER) return clip(flat)
|
|
212
|
+
const prefix = LOG_PREFIX.exec(flat)?.[0] ?? ''
|
|
213
|
+
const from = Math.max(prefix.length, match.index - EXCERPT_BEFORE)
|
|
214
|
+
const space = flat.indexOf(' ', from)
|
|
215
|
+
const start = from > prefix.length && space >= 0 && space < match.index ? space + 1 : from
|
|
216
|
+
const end = Math.min(flat.length, match.index + EXCERPT_AFTER)
|
|
217
|
+
const body = `${start > prefix.length ? '…' : ''}${flat.slice(start, end).trim()}${end < flat.length ? '…' : ''}`
|
|
218
|
+
return clip(prefix === '' ? body : `${prefix} ${body}`)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const pendingOf = (lines: string[]): string[] => {
|
|
222
|
+
const section = sectionOf(lines, LOG_HEADING, true)
|
|
223
|
+
if (!section) return []
|
|
224
|
+
return section.lines
|
|
225
|
+
.map(line => line.replace(/^\s*(?:[-*+]|\d+\.)\s+/, '').trim())
|
|
226
|
+
.filter(line => line !== '' && PENDING.test(line) && !NOT_PENDING.test(line))
|
|
227
|
+
.slice(-MAX_PENDING)
|
|
228
|
+
.map(excerptOf)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Parses an ODD task doc; undefined when it has no tasks section or no tasks. */
|
|
232
|
+
export const parseTaskDoc = (text: string, path: string): ProgressDoc | undefined => {
|
|
233
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n')
|
|
234
|
+
const section = sectionOf(lines, TASK_HEADING)
|
|
235
|
+
if (!section) return undefined
|
|
236
|
+
const tasks = (tableTasks(section.lines) ?? checkboxTasks(section.lines) ?? bulletTasks(section.lines) ?? []).slice(0, MAX_TASKS)
|
|
237
|
+
if (tasks.length === 0) return undefined
|
|
238
|
+
const feature = clip((path.split('/').pop() ?? path).replace(/\.md$/i, ''))
|
|
239
|
+
const heading = lines.find(line => /^#\s/.test(line))
|
|
240
|
+
const done = tasks.filter(one => one.done).length
|
|
241
|
+
const doc: ProgressDoc = {
|
|
242
|
+
feature,
|
|
243
|
+
path: clip(path),
|
|
244
|
+
title: heading === undefined ? feature : clip(heading.replace(/^#\s+/, '')),
|
|
245
|
+
tasks,
|
|
246
|
+
done,
|
|
247
|
+
total: tasks.length,
|
|
248
|
+
pending: pendingOf(lines),
|
|
249
|
+
}
|
|
250
|
+
const objective = objectiveOf(lines)
|
|
251
|
+
if (objective !== undefined) doc.objective = objective
|
|
252
|
+
const current = tasks.find(one => !one.done)
|
|
253
|
+
if (current !== undefined) doc.current = current
|
|
254
|
+
return doc
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ---------------------------------------------------------------- drawing
|
|
258
|
+
|
|
259
|
+
export const progressBar = (done: number, total: number, cells = BAR_CELLS): string => {
|
|
260
|
+
const filled = total <= 0 ? 0 : Math.min(cells, Math.round((done / total) * cells))
|
|
261
|
+
return '▰'.repeat(filled) + '▱'.repeat(cells - filled)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export const rowsProgress = (
|
|
265
|
+
part: ProgressPart,
|
|
266
|
+
ctx: { width: number; accent: string; isOpen: (key: string, byDefault: boolean) => boolean },
|
|
267
|
+
): Row[] => {
|
|
268
|
+
const doc = part.doc
|
|
269
|
+
if (doc === undefined) return []
|
|
270
|
+
const width = Math.max(10, ctx.width)
|
|
271
|
+
const key = 'progress-card'
|
|
272
|
+
const open = ctx.isOpen(key, true)
|
|
273
|
+
const label = `Progreso · ${doc.feature} · ${doc.done}/${doc.total}`
|
|
274
|
+
const rows: Row[] = [
|
|
275
|
+
{
|
|
276
|
+
key,
|
|
277
|
+
cells: [{ spans: [span(fit(`${open ? '▼' : '▶'} ${label}`, width), { color: ctx.accent, bold: true })], action: { kind: 'toggle', key }, label, key: `btn-${key}` }],
|
|
278
|
+
},
|
|
279
|
+
]
|
|
280
|
+
if (!open) return rows
|
|
281
|
+
const isComplete = doc.done === doc.total
|
|
282
|
+
const percent = doc.total === 0 ? 0 : Math.round((doc.done / doc.total) * 100)
|
|
283
|
+
rows.push({
|
|
284
|
+
key: 'progress-bar',
|
|
285
|
+
cells: [{ spans: [span(' '), span(progressBar(doc.done, doc.total), { color: isComplete ? 'success' : ctx.accent }), span(fit(` ${percent}%`, Math.max(1, width - 2 - BAR_CELLS)), { dim: true })] }],
|
|
286
|
+
})
|
|
287
|
+
if (doc.current !== undefined) {
|
|
288
|
+
const route = doc.current.route === undefined ? '' : ` · ${doc.current.route === 'delegated' ? 'delegada' : 'inline'}`
|
|
289
|
+
rows.push({
|
|
290
|
+
key: 'progress-current',
|
|
291
|
+
cells: [{ spans: [span(` ${ICONS.running} `, { color: 'warning' }), span(fit(`Ahora: ${doc.current.id} ${doc.current.title}${route}`, Math.max(1, width - 4)))] }],
|
|
292
|
+
})
|
|
293
|
+
} else {
|
|
294
|
+
rows.push({ key: 'progress-current', cells: [{ spans: [span(` ${ICONS.ok} `, { color: 'success' }), span(fit('Todas las tareas están hechas', Math.max(1, width - 4)), { color: 'success' })] }] })
|
|
295
|
+
}
|
|
296
|
+
if (doc.pending.length > 0) {
|
|
297
|
+
rows.push({ key: 'progress-pending-head', cells: [{ spans: [span(fit(` Verificaciones sin correr (${doc.pending.length})`, width), { color: 'error', bold: true })] }] })
|
|
298
|
+
doc.pending.forEach((line, index) => {
|
|
299
|
+
rows.push({ key: `progress-pending-${index}`, cells: [{ spans: [span(` ${ICONS.error} `, { color: 'error' }), span(fit(line, Math.max(1, width - 4)), { color: 'error' })] }] })
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
return rows
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export const footerProgress = (part: ProgressPart): Span[] => {
|
|
306
|
+
const doc = part.doc
|
|
307
|
+
if (doc === undefined) return []
|
|
308
|
+
return [span(`T ${doc.done}/${doc.total}`, { color: doc.done === doc.total ? 'success' : doc.pending.length > 0 ? 'error' : 'subtle' })]
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ---------------------------------------------------------------- loading
|
|
312
|
+
|
|
313
|
+
type RunText = (argv: readonly string[], cwd: string) => Promise<string | undefined>
|
|
314
|
+
|
|
315
|
+
/** The `odd/tasks` folders to look in: the session's checkout, and the main checkout when it runs inside a worktree. */
|
|
316
|
+
export const taskDirsOf = async (run: RunText, cwd: string): Promise<string[]> => {
|
|
317
|
+
const top = (await run(['git', 'rev-parse', '--show-toplevel'], cwd))?.trim()
|
|
318
|
+
const common = (await run(['git', 'rev-parse', '--path-format=absolute', '--git-common-dir'], cwd))?.trim()
|
|
319
|
+
const main = common !== undefined && /\/\.git\/?$/.test(common) ? common.replace(/\/\.git\/?$/, '') : undefined
|
|
320
|
+
const roots = [top || cwd, main].filter((one): one is string => one !== undefined && one !== '')
|
|
321
|
+
return [...new Set(roots)].map(root => `${root.replace(/\/$/, '')}/odd/tasks`)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export const isTaskDocPath = (path: string): boolean => /(?:^|\/)odd\/tasks\/[^/]+\.md$/i.test(path)
|
|
325
|
+
|
|
326
|
+
let loading = false
|
|
327
|
+
let loadAgain = false
|
|
328
|
+
|
|
329
|
+
async function loadProgress($: Api): Promise<void> {
|
|
330
|
+
const run: RunText = async (argv, cwd) => {
|
|
331
|
+
const result = await $.process.run(argv, { cwd, timeoutMs: RUN_TIMEOUT_MS }).catch(() => undefined)
|
|
332
|
+
return result?.exitCode === 0 ? result.stdout : undefined
|
|
333
|
+
}
|
|
334
|
+
const cwd = (await $.session.cwd().catch(() => undefined)) ?? (await $.session.root().catch(() => undefined))
|
|
335
|
+
if (cwd === undefined) return
|
|
336
|
+
const dirs = await taskDirsOf(run, cwd)
|
|
337
|
+
let newest: { path: string; mtimeMs: number } | undefined
|
|
338
|
+
for (const dir of dirs) {
|
|
339
|
+
const found = await $.fs.list(dir).catch(() => [])
|
|
340
|
+
for (const entry of found) {
|
|
341
|
+
if (entry.kind !== 'file' || !/\.md$/i.test(entry.name)) continue
|
|
342
|
+
if (newest === undefined || entry.mtimeMs > newest.mtimeMs) newest = { path: `${dir}/${entry.name}`, mtimeMs: entry.mtimeMs }
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (newest === undefined) {
|
|
346
|
+
await update($, progressState, () => ({ path: '', mtimeMs: 0 }))
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
const stat = await $.fs.stat(newest.path).catch(() => undefined)
|
|
350
|
+
if (stat === undefined || stat.kind !== 'file') return
|
|
351
|
+
const stored = await read($, progressState)
|
|
352
|
+
if (stored.path === newest.path && stored.mtimeMs === stat.mtimeMs) return
|
|
353
|
+
const chosen = newest.path
|
|
354
|
+
if (stat.size > CAP_BYTES) {
|
|
355
|
+
await update($, progressState, () => ({ path: chosen, mtimeMs: stat.mtimeMs }))
|
|
356
|
+
return
|
|
357
|
+
}
|
|
358
|
+
const text = await $.fs.read(chosen).catch(() => undefined)
|
|
359
|
+
if (text === undefined) return
|
|
360
|
+
const doc = parseTaskDoc(text, chosen)
|
|
361
|
+
await update($, progressState, () => (doc === undefined ? { path: chosen, mtimeMs: stat.mtimeMs } : { path: chosen, mtimeMs: stat.mtimeMs, doc }))
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async function refreshProgress($: Api): Promise<void> {
|
|
365
|
+
if (loading) {
|
|
366
|
+
loadAgain = true
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
loading = true
|
|
370
|
+
try {
|
|
371
|
+
do {
|
|
372
|
+
loadAgain = false
|
|
373
|
+
await loadProgress($).catch(() => undefined)
|
|
374
|
+
} while (loadAgain)
|
|
375
|
+
} finally {
|
|
376
|
+
loading = false
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export async function readProgress($: Api): Promise<ProgressPart> {
|
|
381
|
+
const stored = await read($, progressState).catch(() => undefined)
|
|
382
|
+
return stored?.doc === undefined ? {} : { doc: stored.doc }
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export const registerProgress = (on: Parameters<Register>[0]): void => {
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export async function progressSessionStart($: Api, e: Parameters<Hook<'session.start'>>[1], next: Parameters<Hook<'session.start'>>[2]): Promise<Awaited<ReturnType<Hook<'session.start'>>>> {
|
|
391
|
+
const result = await next(e)
|
|
392
|
+
void refreshProgress($ as Api).catch(() => undefined)
|
|
393
|
+
return result
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export async function progressTurnComplete($: Api, e: Parameters<Hook<'turn.complete'>>[1], next: Parameters<Hook<'turn.complete'>>[2]): Promise<Awaited<ReturnType<Hook<'turn.complete'>>>> {
|
|
397
|
+
const result = await next(e)
|
|
398
|
+
void refreshProgress($ as Api).catch(() => undefined)
|
|
399
|
+
return result
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export async function progressToolCall($: Api, e: Parameters<Hook<'tool.call'>>[1], next: Parameters<Hook<'tool.call'>>[2]): Promise<Awaited<ReturnType<Hook<'tool.call'>>>> {
|
|
403
|
+
const result = await next(e)
|
|
404
|
+
try {
|
|
405
|
+
if (e.tool === 'Write' || e.tool === 'Edit') {
|
|
406
|
+
const path = (e as unknown as { file_path?: unknown }).file_path
|
|
407
|
+
if (typeof path === 'string' && isTaskDocPath(path)) void refreshProgress($ as Api).catch(() => undefined)
|
|
408
|
+
}
|
|
409
|
+
} catch {
|
|
410
|
+
// A failed refresh only leaves the card as it was.
|
|
411
|
+
}
|
|
412
|
+
return result
|
|
413
|
+
}
|