dsh-spec-collab 0.1.6 → 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.
@@ -0,0 +1,138 @@
1
+ import type { ParticipantRole, RequirementView, ReviewKind } from '../protocol.ts'
2
+
3
+ export type TaskView = 'review' | 'patches' | 'ready'
4
+ export type WorkflowCommand =
5
+ | { kind: 'open'; view: TaskView }
6
+ | { kind: 'bind-workspace' }
7
+ | { kind: 'request-review'; reviewKind: ReviewKind }
8
+ | { kind: 'advance' }
9
+ | { kind: 'confirm'; role: ParticipantRole }
10
+ | { kind: 'generate-ready' }
11
+ | { kind: 'none' }
12
+
13
+ export interface WorkflowState {
14
+ eyebrow: string
15
+ title: string
16
+ detail: string
17
+ actionLabel: string
18
+ command: WorkflowCommand
19
+ disabled?: boolean
20
+ disabledReason?: string
21
+ }
22
+
23
+ const reviewIsOpen = (status: RequirementView['reviewItems'][number]['status']): boolean => !['resolved', 'invalidated', 'non-blocking-verify'].includes(status)
24
+
25
+ export function workflowState(requirement: RequirementView, participantRole: ParticipantRole): WorkflowState {
26
+ const currentReviews = requirement.reviewItems.filter(item => item.commit === requirement.currentCommit)
27
+ const blockers = currentReviews.filter(item => item.severity === 'blocking' && reviewIsOpen(item.status))
28
+ const productBlockers = blockers.filter(item => item.status !== 'joint-review')
29
+ const unanswered = blockers.filter(item => item.status === 'open')
30
+ const currentPatches = requirement.patches.filter(item => item.baseCommit === requirement.currentCommit && item.status === 'pending')
31
+ const currentRuns = requirement.aiRuns.filter(run => run.commit === requirement.currentCommit)
32
+ const activeRun = currentRuns.slice().reverse().find(run => ['queued', 'running'].includes(run.status))
33
+ const completed = (kind: ReviewKind): boolean => currentRuns.some(run => run.kind === kind && run.status === 'completed')
34
+ const productConfirmed = requirement.confirmations.some(item => item.commit === requirement.currentCommit && item.role === 'product' && item.scope === 'version' && item.status === 'active')
35
+ const engineeringConfirmed = requirement.confirmations.some(item => item.commit === requirement.currentCommit && item.role === 'engineering' && item.scope === 'version' && item.status === 'active')
36
+ const failedChecks = requirement.readiness.filter(check => !check.passed)
37
+
38
+ if (!requirement.workspaceId) return {
39
+ eyebrow: '开始前', title: '关联需求所在的项目', detail: 'AI 会从这个项目读取代码、规范和团队知识,关联后不能中途切换。',
40
+ actionLabel: '选择项目', command: { kind: 'bind-workspace' },
41
+ }
42
+
43
+ if (activeRun) return {
44
+ eyebrow: 'AI 正在工作', title: activeRun.kind === 'engineering-precheck' ? '正在从研发视角检查需求' : '正在整理需求中的问题',
45
+ detail: '结果会自动出现在这里,可以先继续编辑正文。', actionLabel: '分析中', command: { kind: 'none' }, disabled: true,
46
+ }
47
+
48
+ if (requirement.stage === 'product-review') {
49
+ if (!completed('product-first')) return {
50
+ eyebrow: '需求澄清', title: '先让 AI 找出需求缺口', detail: 'AI 会检查目标、范围、验收标准、证据和潜在风险。',
51
+ actionLabel: '开始需求检查', command: { kind: 'request-review', reviewKind: 'product-first' },
52
+ }
53
+ if (unanswered.length > 0) return {
54
+ eyebrow: '需要你的判断', title: `回答 ${unanswered.length} 个关键问题`, detail: '先处理会影响范围、规则或验收结果的问题,其余内容可以稍后核对。',
55
+ actionLabel: '继续回答', command: { kind: 'open', view: 'review' },
56
+ }
57
+ if (currentPatches.length > 0) return {
58
+ eyebrow: 'AI 已整理改动', title: `审核 ${currentPatches.length} 份正文修改`, detail: '确认改动符合你的判断后,再写入正式需求版本。',
59
+ actionLabel: '查看改动', command: { kind: 'open', view: 'patches' },
60
+ }
61
+ if (productBlockers.length > 0 || !completed('product-second')) return {
62
+ eyebrow: '答案已收集', title: '让 AI 把结论整理进正文', detail: 'AI 会复核你的回答,并生成可逐项审核的正文修改。',
63
+ actionLabel: '整理正文改动', command: { kind: 'request-review', reviewKind: 'product-second' },
64
+ }
65
+ return {
66
+ eyebrow: '产品澄清完成', title: '进入产品确认', detail: '当前版本没有产品侧阻塞问题,下一步由产品负责人确认内容。',
67
+ actionLabel: '进入产品确认', command: { kind: 'advance' },
68
+ }
69
+ }
70
+
71
+ if (requirement.stage === 'product-confirmation') {
72
+ if (!productConfirmed) return {
73
+ eyebrow: '产品确认', title: '确认当前需求版本', detail: participantRole === 'product' ? '确认目标、范围、业务规则和验收标准准确反映了产品决策。' : '需要一位产品角色确认当前版本后才能继续。',
74
+ actionLabel: participantRole === 'product' ? '确认当前版本' : '等待产品确认', command: participantRole === 'product' ? { kind: 'confirm', role: 'product' } : { kind: 'none' },
75
+ ...(participantRole === 'product' ? {} : { disabled: true, disabledReason: '当前身份是研发,需由产品角色完成确认。' }),
76
+ }
77
+ return {
78
+ eyebrow: '产品已确认', title: '邀请研发共同检查', detail: '产品结论已经锁定到当前版本,可以进入研发可行性和测试约束检查。',
79
+ actionLabel: '进入产研共审', command: { kind: 'advance' },
80
+ }
81
+ }
82
+
83
+ if (requirement.stage === 'joint-review') {
84
+ if (!completed('engineering-precheck')) return {
85
+ eyebrow: '产研共审', title: '从研发视角检查可实施性', detail: '重点检查现有实现、影响范围、兼容性和测试落点。',
86
+ actionLabel: '开始研发检查', command: { kind: 'request-review', reviewKind: 'engineering-precheck' },
87
+ }
88
+ if (unanswered.length > 0) return {
89
+ eyebrow: '研发发现待确认项', title: `处理 ${unanswered.length} 个阻塞问题`, detail: '这些问题会影响实现或验收,需要在交付研发前明确。',
90
+ actionLabel: '继续处理', command: { kind: 'open', view: 'review' },
91
+ }
92
+ if (currentPatches.length > 0) return {
93
+ eyebrow: '研发建议了改动', title: `审核 ${currentPatches.length} 份正文修改`, detail: '确认实现约束和测试条件已准确写回需求。',
94
+ actionLabel: '查看改动', command: { kind: 'open', view: 'patches' },
95
+ }
96
+ if (blockers.length > 0) return {
97
+ eyebrow: '仍有待收敛内容', title: '复核已回复的问题', detail: '已有回复尚未消除阻塞,请重新检查或让 AI 根据结论更新正文。',
98
+ actionLabel: '查看问题', command: { kind: 'open', view: 'review' },
99
+ }
100
+ if (failedChecks.length > 0) return {
101
+ eyebrow: '交付条件未齐', title: `补齐 ${failedChecks.length} 项需求内容`, detail: '完成条件会指出正文缺少的目标、范围、验收标准或测试约束。',
102
+ actionLabel: '查看缺少内容', command: { kind: 'open', view: 'ready' },
103
+ }
104
+ if (!productConfirmed) return {
105
+ eyebrow: '等待角色确认', title: '请产品确认当前版本', detail: participantRole === 'product' ? '正文已通过检查,确认它准确反映最终产品结论。' : '需要产品角色确认同一个当前版本。',
106
+ actionLabel: participantRole === 'product' ? '以产品身份确认' : '等待产品确认', command: participantRole === 'product' ? { kind: 'confirm', role: 'product' } : { kind: 'none' },
107
+ ...(participantRole === 'product' ? {} : { disabled: true, disabledReason: '当前身份是研发,需由产品角色完成确认。' }),
108
+ }
109
+ if (!engineeringConfirmed) return {
110
+ eyebrow: '最后一步确认', title: '请研发确认当前版本', detail: participantRole === 'engineering' ? '确认实现边界、风险和测试约束已经足够明确。' : '需要研发角色确认同一个当前版本。',
111
+ actionLabel: participantRole === 'engineering' ? '以研发身份确认' : '等待研发确认', command: participantRole === 'engineering' ? { kind: 'confirm', role: 'engineering' } : { kind: 'none' },
112
+ ...(participantRole === 'engineering' ? {} : { disabled: true, disabledReason: '当前身份是产品,需由研发角色完成确认。' }),
113
+ }
114
+ return {
115
+ eyebrow: '可以交付研发', title: '生成就绪需求', detail: '产品和研发已确认同一个版本,所有完成条件均已通过。',
116
+ actionLabel: '生成就绪需求', command: { kind: 'generate-ready' },
117
+ }
118
+ }
119
+
120
+ return {
121
+ eyebrow: '需求已就绪', title: '这份需求可以进入研发', detail: '正文、确认人、关键决策和待验证项已固定在同一个版本中。',
122
+ actionLabel: '查看交付内容', command: { kind: 'open', view: 'ready' },
123
+ }
124
+ }
125
+
126
+ export function friendlyActionError(error: string): string {
127
+ const messages: Array<[string, string]> = [
128
+ ['product blocking review items remain', '还有阻塞问题未处理,请先完成待澄清事项。'],
129
+ ['product second review is required', '需要先让 AI 根据回复整理并复核正文。'],
130
+ ['product must confirm the current version', '需要产品角色确认当前版本后才能进入产研共审。'],
131
+ ['readiness gate is not satisfied', '需求内容还没有满足全部交付条件,请查看缺少内容。'],
132
+ ['current version needs product and engineering confirmation', '产品和研发需要分别确认同一个当前版本。'],
133
+ ['review workspace is required', '请选择需求所在的项目。'],
134
+ ['human participant is required', '这项操作需要由真实参与者完成。'],
135
+ ['role is required', '当前身份角色不能执行这项确认。'],
136
+ ]
137
+ return messages.find(([source]) => error.includes(source))?.[1] ?? error
138
+ }