@linxin666/dsh-client-ui-task-board-team 0.0.0-stage → 0.4.14

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.
Files changed (74) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +66 -2
  4. package/README.zh.md +67 -0
  5. package/cordis.patch.yml +14 -0
  6. package/icon.svg +3 -0
  7. package/lib/client.js +1490 -0
  8. package/lib/index.js +2968 -0
  9. package/lib/types/client/PluginSettingsCard.d.ts +255 -0
  10. package/lib/types/client/RangeBubble.d.ts +46 -0
  11. package/lib/types/client/SegmentGroup.d.ts +28 -0
  12. package/lib/types/client/index.d.ts +58 -0
  13. package/lib/types/client/locales.d.ts +90 -0
  14. package/lib/types/client/optional-service.d.ts +22 -0
  15. package/lib/types/client/settings-form.d.ts +235 -0
  16. package/lib/types/client/team/extension.d.ts +29 -0
  17. package/lib/types/client/team/sections.d.ts +38 -0
  18. package/lib/types/client/team/settings.d.ts +67 -0
  19. package/lib/types/client/team/summary.d.ts +34 -0
  20. package/lib/types/core/contract.d.ts +269 -0
  21. package/lib/types/core/hub-protocol.d.ts +175 -0
  22. package/lib/types/core/identity.d.ts +96 -0
  23. package/lib/types/core/mirror.d.ts +51 -0
  24. package/lib/types/core/question-report.d.ts +84 -0
  25. package/lib/types/core/settlement.d.ts +49 -0
  26. package/lib/types/core/task-record.d.ts +59 -0
  27. package/lib/types/core/timers.d.ts +20 -0
  28. package/lib/types/host/bindings.d.ts +92 -0
  29. package/lib/types/host/channel.d.ts +44 -0
  30. package/lib/types/host/credentials.d.ts +63 -0
  31. package/lib/types/host/extension.d.ts +57 -0
  32. package/lib/types/host/hub-client.d.ts +157 -0
  33. package/lib/types/host/questions.d.ts +105 -0
  34. package/lib/types/host/service.d.ts +209 -0
  35. package/lib/types/host/setup.d.ts +152 -0
  36. package/lib/types/host/tools.d.ts +19 -0
  37. package/lib/types/host/waterfall.d.ts +39 -0
  38. package/lib/types/index.d.ts +152 -0
  39. package/locale/en.json +6 -0
  40. package/locale/zh.json +6 -0
  41. package/package.json +84 -3
  42. package/src/client/PluginSettingsCard.tsx +838 -0
  43. package/src/client/RangeBubble.tsx +164 -0
  44. package/src/client/SegmentGroup.tsx +93 -0
  45. package/src/client/css-modules.d.ts +5 -0
  46. package/src/client/index.ts +139 -0
  47. package/src/client/locales.ts +168 -0
  48. package/src/client/optional-service.ts +30 -0
  49. package/src/client/settings-card.module.css +821 -0
  50. package/src/client/settings-form.ts +510 -0
  51. package/src/client/team/extension.ts +185 -0
  52. package/src/client/team/sections.tsx +159 -0
  53. package/src/client/team/settings.tsx +348 -0
  54. package/src/client/team/summary.ts +156 -0
  55. package/src/client/team/team.module.css +193 -0
  56. package/src/core/contract.ts +326 -0
  57. package/src/core/hub-protocol.ts +217 -0
  58. package/src/core/identity.ts +193 -0
  59. package/src/core/mirror.ts +88 -0
  60. package/src/core/question-report.ts +144 -0
  61. package/src/core/settlement.ts +84 -0
  62. package/src/core/task-record.ts +65 -0
  63. package/src/core/timers.ts +34 -0
  64. package/src/host/bindings.ts +211 -0
  65. package/src/host/channel.ts +97 -0
  66. package/src/host/credentials.ts +147 -0
  67. package/src/host/extension.ts +197 -0
  68. package/src/host/hub-client.ts +287 -0
  69. package/src/host/questions.ts +275 -0
  70. package/src/host/service.ts +1421 -0
  71. package/src/host/setup.ts +259 -0
  72. package/src/host/tools.ts +70 -0
  73. package/src/host/waterfall.ts +51 -0
  74. package/src/index.ts +452 -0
@@ -0,0 +1,275 @@
1
+ /**
2
+ * Decision questions asked by a team card's session, routed through the hub.
3
+ *
4
+ * The Host's question waterfall is shared by every answerer on the machine, so
5
+ * this module claims exactly one case and delegates everything else: a request
6
+ * whose `agent.id` — the session id, since the event carries no session field —
7
+ * is the session of a card this extension mirrors. Every other conversation on
8
+ * the machine keeps its own answerer, and the local surface stays available for
9
+ * a claimed question because the claim holds the ask while the hub's reviewers
10
+ * answer it.
11
+ *
12
+ * Tool permission approvals are NOT part of this module and have no listener at
13
+ * all: `approval/request` belongs to the local machine's security domain
14
+ * (`team-hub/PROTOCOL.md` section 6), so this provider never puts one on the
15
+ * wire.
16
+ *
17
+ * @module dsh-task-board-team/host/questions
18
+ */
19
+ import type { HubClient } from './hub-client.ts'
20
+ import { HubError } from './hub-client.ts'
21
+ import {
22
+ answerToHost,
23
+ buildQuestionReport,
24
+ isReportable,
25
+ unansweredAnswer,
26
+ type QuestionAnswer,
27
+ type QuestionItem,
28
+ type QuestionRequestShape,
29
+ } from '../core/question-report.ts'
30
+ import { findLinkedTask, findTaskBySession, readTaskIssueLink } from '../core/identity.ts'
31
+ import type { TaskBoardExtensionHost, TaskBoardLinkedTask } from '../core/contract.ts'
32
+ import type { HostTimerFace } from '../core/timers.ts'
33
+
34
+ /**
35
+ * Default local fallback wait, in milliseconds.
36
+ *
37
+ * Zero means "no local cap": the claim holds the ask until the hub settles it
38
+ * (answered, expired or withdrawn), which is what the acceptance loop needs — a
39
+ * member answering from a lock-screen push may take far longer than any fixed
40
+ * window. A deployment that wants the local surface back sooner turns the
41
+ * fallback on and names its own number of minutes.
42
+ */
43
+ export const DEFAULT_QUESTION_WAIT_MS = 0
44
+
45
+ /** One question this Host reported and is still waiting on. */
46
+ export interface PendingQuestionView {
47
+ /** Local claim id. */
48
+ id: string
49
+ /** Card the asking session belongs to. */
50
+ taskId: string
51
+ /** Issue the question belongs to. */
52
+ issueId: string
53
+ /** The question text, as the browser renders it. */
54
+ question: string
55
+ /** Hub question id, once the hub stored it. */
56
+ hubQuestionId?: string
57
+ /** Instant the question was reported (ms epoch). */
58
+ askedAt: number
59
+ }
60
+
61
+ interface PendingQuestion extends PendingQuestionView {
62
+ item: QuestionItem
63
+ settle(answer: QuestionAnswer | undefined): void
64
+ }
65
+
66
+ export interface QuestionRouterOptions {
67
+ hub: HubClient
68
+ host: TaskBoardExtensionHost
69
+ /** Every card this extension mirrors; read live so a deleted card is not answered. */
70
+ linked: () => readonly TaskBoardLinkedTask[]
71
+ timers: HostTimerFace
72
+ now: () => number
73
+ /** Mint the local claim id. */
74
+ uuid: () => string
75
+ /** Called whenever the pending set changes, so the summary can republish. */
76
+ onChange?: () => void
77
+ /**
78
+ * Local fallback wait for one claim, read per question.
79
+ *
80
+ * Absent or zero means the claim waits for the hub's own terminal state; a
81
+ * positive number withdraws the question after that long and hands the ask back
82
+ * to the next answerer.
83
+ */
84
+ fallbackWaitMs?: () => number | undefined
85
+ log?: (message: string, detail?: unknown) => void
86
+ }
87
+
88
+ /**
89
+ * The provider's claims on the Host's process-wide question waterfall.
90
+ */
91
+ export class QuestionRouter {
92
+ readonly #options: QuestionRouterOptions
93
+ readonly #pending = new Map<string, PendingQuestion>()
94
+
95
+ constructor(options: QuestionRouterOptions) {
96
+ this.#options = options
97
+ }
98
+
99
+ /** Every question this Host is still waiting on, oldest claim first. */
100
+ pending(): readonly PendingQuestionView[] {
101
+ return [...this.#pending.values()].map(entry => ({
102
+ id: entry.id,
103
+ taskId: entry.taskId,
104
+ issueId: entry.issueId,
105
+ question: entry.question,
106
+ ...(entry.hubQuestionId === undefined ? {} : { hubQuestionId: entry.hubQuestionId }),
107
+ askedAt: entry.askedAt,
108
+ }))
109
+ }
110
+
111
+ /**
112
+ * The waterfall body: claim one question that belongs to a team card, and
113
+ * delegate every other request to the next answerer.
114
+ * @param request - the pending question batch.
115
+ * @param next - the rest of the answerer chain.
116
+ * @returns the team's answer, or whatever the next answerer says.
117
+ */
118
+ async request(request: QuestionRequestShape, next: () => Promise<QuestionAnswer>): Promise<QuestionAnswer> {
119
+ const sessionId = request.agent?.id
120
+ if (sessionId === undefined) return await next()
121
+ // The claim is scoped to a mirrored card's own session: a question asked in
122
+ // any other conversation on this machine is none of this provider's business.
123
+ const task = findTaskBySession(this.#options.linked(), sessionId)
124
+ if (task === undefined) return await next()
125
+ const link = readTaskIssueLink(task)
126
+ if (link === undefined) return await next()
127
+ const item = request.questions[0]
128
+ if (item === undefined || !isReportable(item)) return await next()
129
+
130
+ const id = this.#options.uuid()
131
+ let settle: (answer: QuestionAnswer | undefined) => void = () => undefined
132
+ const answer = new Promise<QuestionAnswer | undefined>(resolve => { settle = resolve })
133
+ let questionId: string
134
+ try {
135
+ const question = await this.#options.hub.createQuestion(link.issueId, buildQuestionReport(item))
136
+ questionId = question.id
137
+ } catch (error) {
138
+ // A hub that cannot take the question must not take the agent with it: the
139
+ // local surface answers, exactly as it did before this provider existed.
140
+ this.#options.log?.('question report failed', error)
141
+ return await next()
142
+ }
143
+
144
+ const entry: PendingQuestion = {
145
+ id,
146
+ taskId: task.id,
147
+ issueId: link.issueId,
148
+ question: item.question,
149
+ hubQuestionId: questionId,
150
+ askedAt: this.#options.now(),
151
+ item,
152
+ settle,
153
+ }
154
+ this.#pending.set(id, entry)
155
+ this.#options.onChange?.()
156
+
157
+ const waitMs = this.#options.fallbackWaitMs?.()
158
+ const cancelWait = waitMs === undefined || waitMs <= 0
159
+ ? (): void => {}
160
+ : this.#options.timers.timeout(() => { void this.#expireLocally(id) }, waitMs)
161
+ const abort = (): void => { void this.#withdraw(id) }
162
+ request.signal?.addEventListener('abort', abort, { once: true })
163
+
164
+ const settled = await answer
165
+ cancelWait()
166
+ request.signal?.removeEventListener('abort', abort)
167
+ return settled ?? await next()
168
+ }
169
+
170
+ /**
171
+ * Settle one question with a team answer.
172
+ * @param payload - the hub's `question.answered` payload.
173
+ * @returns whether this Host was waiting on that question.
174
+ */
175
+ answered(payload: Record<string, unknown>): boolean {
176
+ const questionId = typeof payload.question_id === 'string' ? payload.question_id : undefined
177
+ if (questionId === undefined) return false
178
+ const entry = this.#byHubId(questionId)
179
+ if (entry === undefined) return false
180
+ const answer = readAnswerPayload(payload.answer)
181
+ this.#settle(entry.id, answerToHost(entry.item, answer))
182
+ return true
183
+ }
184
+
185
+ /**
186
+ * End one question as unanswered, the way the hub's expiry does.
187
+ * @param payload - the hub's `question.expired` payload.
188
+ * @returns whether this Host was waiting on that question.
189
+ */
190
+ expired(payload: Record<string, unknown>): boolean {
191
+ const questionId = typeof payload.question_id === 'string' ? payload.question_id : undefined
192
+ if (questionId === undefined) return false
193
+ const entry = this.#byHubId(questionId)
194
+ if (entry === undefined) return false
195
+ this.#settle(entry.id, unansweredAnswer())
196
+ return true
197
+ }
198
+
199
+ /**
200
+ * Drop one question the hub reported withdrawn.
201
+ * @param payload - the hub's `question.withdrawn` payload.
202
+ * @returns whether this Host was waiting on that question.
203
+ */
204
+ withdrawn(payload: Record<string, unknown>): boolean {
205
+ const questionId = typeof payload.question_id === 'string' ? payload.question_id : undefined
206
+ if (questionId === undefined) return false
207
+ const entry = this.#byHubId(questionId)
208
+ if (entry === undefined) return false
209
+ this.#settle(entry.id, unansweredAnswer())
210
+ return true
211
+ }
212
+
213
+ /** Drop every claim, delegating each to the next answerer. */
214
+ dispose(): void {
215
+ for (const id of [...this.#pending.keys()]) this.#settle(id, undefined)
216
+ }
217
+
218
+ /**
219
+ * The local ask was cancelled: tell the hub, then let the next answerer take it.
220
+ */
221
+ async #withdraw(id: string): Promise<void> {
222
+ const entry = this.#pending.get(id)
223
+ if (entry === undefined) return
224
+ if (entry.hubQuestionId !== undefined) {
225
+ try {
226
+ await this.#options.hub.withdrawQuestion(entry.hubQuestionId)
227
+ } catch (error) {
228
+ // A question the hub already closed needs no second close; a hub that
229
+ // is down costs the dismissal, not the agent's turn.
230
+ if (!(error instanceof HubError) || error.code !== 'QUESTION_NOT_PENDING') {
231
+ this.#options.log?.('question withdraw failed', error)
232
+ }
233
+ }
234
+ }
235
+ this.#settle(id, undefined)
236
+ }
237
+
238
+ /** The local wait window ended: withdraw on the hub and return to the machine. */
239
+ async #expireLocally(id: string): Promise<void> {
240
+ await this.#withdraw(id)
241
+ }
242
+
243
+ #byHubId(questionId: string): PendingQuestion | undefined {
244
+ for (const entry of this.#pending.values()) {
245
+ if (entry.hubQuestionId === questionId) return entry
246
+ }
247
+ return undefined
248
+ }
249
+
250
+ #settle(id: string, answer: QuestionAnswer | undefined): boolean {
251
+ const entry = this.#pending.get(id)
252
+ if (entry === undefined) return false
253
+ this.#pending.delete(id)
254
+ entry.settle(answer)
255
+ this.#options.onChange?.()
256
+ return true
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Read the hub's answer member into the shape the mapping needs.
262
+ *
263
+ * Protocol amendment A2 fixes this shape as `{ selected: string[], custom?: string }`
264
+ * (or null while a question is still unanswered), so a null answer reads as an
265
+ * empty selection instead of being mistaken for a custom-only reply.
266
+ */
267
+ function readAnswerPayload(raw: unknown): { selected: readonly string[]; custom?: string } {
268
+ if (typeof raw !== 'object' || raw === null) return { selected: [] }
269
+ const answer = raw as Record<string, unknown>
270
+ const selected = Array.isArray(answer.selected)
271
+ ? answer.selected.filter((value): value is string => typeof value === 'string')
272
+ : []
273
+ const custom = typeof answer.custom === 'string' ? answer.custom : undefined
274
+ return { selected, ...(custom === undefined ? {} : { custom }) }
275
+ }