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

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 (65) 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 +1117 -0
  8. package/lib/index.js +2968 -0
  9. package/lib/types/client/index.d.ts +58 -0
  10. package/lib/types/client/locales.d.ts +83 -0
  11. package/lib/types/client/optional-service.d.ts +22 -0
  12. package/lib/types/client/team/extension.d.ts +29 -0
  13. package/lib/types/client/team/sections.d.ts +38 -0
  14. package/lib/types/client/team/settings.d.ts +41 -0
  15. package/lib/types/client/team/summary.d.ts +34 -0
  16. package/lib/types/core/contract.d.ts +269 -0
  17. package/lib/types/core/hub-protocol.d.ts +175 -0
  18. package/lib/types/core/identity.d.ts +96 -0
  19. package/lib/types/core/mirror.d.ts +51 -0
  20. package/lib/types/core/question-report.d.ts +84 -0
  21. package/lib/types/core/settlement.d.ts +49 -0
  22. package/lib/types/core/task-record.d.ts +59 -0
  23. package/lib/types/core/timers.d.ts +20 -0
  24. package/lib/types/host/bindings.d.ts +92 -0
  25. package/lib/types/host/channel.d.ts +44 -0
  26. package/lib/types/host/credentials.d.ts +63 -0
  27. package/lib/types/host/extension.d.ts +57 -0
  28. package/lib/types/host/hub-client.d.ts +157 -0
  29. package/lib/types/host/questions.d.ts +105 -0
  30. package/lib/types/host/service.d.ts +209 -0
  31. package/lib/types/host/setup.d.ts +152 -0
  32. package/lib/types/host/tools.d.ts +19 -0
  33. package/lib/types/host/waterfall.d.ts +39 -0
  34. package/lib/types/index.d.ts +152 -0
  35. package/locale/en.json +6 -0
  36. package/locale/zh.json +6 -0
  37. package/package.json +83 -3
  38. package/src/client/css-modules.d.ts +5 -0
  39. package/src/client/index.ts +139 -0
  40. package/src/client/locales.ts +154 -0
  41. package/src/client/optional-service.ts +30 -0
  42. package/src/client/team/extension.ts +185 -0
  43. package/src/client/team/sections.tsx +159 -0
  44. package/src/client/team/settings.tsx +242 -0
  45. package/src/client/team/summary.ts +156 -0
  46. package/src/client/team/team.module.css +122 -0
  47. package/src/core/contract.ts +326 -0
  48. package/src/core/hub-protocol.ts +217 -0
  49. package/src/core/identity.ts +193 -0
  50. package/src/core/mirror.ts +88 -0
  51. package/src/core/question-report.ts +144 -0
  52. package/src/core/settlement.ts +84 -0
  53. package/src/core/task-record.ts +65 -0
  54. package/src/core/timers.ts +34 -0
  55. package/src/host/bindings.ts +211 -0
  56. package/src/host/channel.ts +97 -0
  57. package/src/host/credentials.ts +147 -0
  58. package/src/host/extension.ts +197 -0
  59. package/src/host/hub-client.ts +287 -0
  60. package/src/host/questions.ts +275 -0
  61. package/src/host/service.ts +1421 -0
  62. package/src/host/setup.ts +259 -0
  63. package/src/host/tools.ts +70 -0
  64. package/src/host/waterfall.ts +51 -0
  65. package/src/index.ts +452 -0
@@ -0,0 +1,326 @@
1
+ /**
2
+ * Same-shape restatement of the task board's external-provider contract.
3
+ *
4
+ * The board owns this contract (`packages/dsh-task-board/src/core/extension.ts`)
5
+ * and this package may not import the board's internals, so every type and
6
+ * constant the extension consumes is restated here with an identical shape and
7
+ * resolved at runtime through the shared cordis service name. Nothing in this
8
+ * module is a value import of a board package: the declarations are erased and
9
+ * the service lookup is a plain property read.
10
+ *
11
+ * Two capabilities ride the contract as OPTIONAL members (the API version stays
12
+ * 1): `sessions.followUp` and `tasks.rework`. A provider probes for them and
13
+ * degrades visibly where a deployment serves neither, which is what
14
+ * {@link probeTaskBoardCapabilities} reports.
15
+ *
16
+ * @module dsh-task-board-team/core/contract
17
+ */
18
+ import type { ToolDefinition } from '@deepseek-ai/dsh-tools'
19
+ import type { ExecutionOutcome, TaskRecord, TaskStatus } from './task-record.ts'
20
+
21
+ /** Cordis service name the board's host and browser halves each publish. */
22
+ export const TASK_BOARD_SERVICE_NAME = 'taskBoard'
23
+
24
+ /** Contract version this extension was built against. */
25
+ export const TASK_BOARD_API_VERSION = 1
26
+
27
+ /** Child seat a provider registers its task-detail section into. */
28
+ export const TASK_BOARD_DETAIL_SECTION = 'task-board.detail.section'
29
+ /** Child seat a provider registers its settings section into. */
30
+ export const TASK_BOARD_SETTINGS_SECTION = 'task-board.settings.section'
31
+ /** Child seat a provider registers its card decoration into. */
32
+ export const TASK_BOARD_CARD_DECORATION = 'task-board.card.decoration'
33
+
34
+ /** Largest free text one provider call pushes into an execution, in characters. */
35
+ export const TASK_BOARD_MESSAGE_MAX_LENGTH = 4_000
36
+
37
+ /** One opaque provider payload the board stores without interpreting. */
38
+ export type TaskBoardExtensionPayload = Record<string, unknown>
39
+
40
+ /** One action the browser half dispatches over the board's same-origin channel. */
41
+ export interface TaskBoardExtensionActionRequest {
42
+ /** Id of the extension the action belongs to. */
43
+ extensionId: string
44
+ /** Provider-owned action name, routed back to this extension's handleAction. */
45
+ action: string
46
+ /** Task the action targets, when it targets one. */
47
+ taskId?: string
48
+ /** Opaque provider payload. */
49
+ payload?: TaskBoardExtensionPayload
50
+ }
51
+
52
+ /** Deliver one extension action through the board's same-origin action channel. */
53
+ export type TaskBoardExtensionDispatch = (request: TaskBoardExtensionActionRequest) => Promise<boolean>
54
+
55
+ /** Draft one provider creates a task from. */
56
+ export interface TaskBoardTaskDraft {
57
+ title: string
58
+ description: string
59
+ prompt: string
60
+ /** Column the created card opens in; absent means the board default. */
61
+ status?: TaskStatus
62
+ /** Parent card id, when the provider materializes a subtask. */
63
+ parentId?: string
64
+ /** Workspace the execution must run in; absent leaves the board's own rules. */
65
+ workspaceId?: string
66
+ /**
67
+ * Agent preset the created card's execution must be composed from.
68
+ *
69
+ * Optional in the contract like the two capability members, so a provider
70
+ * passes it and then CHECKS it survived: a board revision that serves no draft
71
+ * preset drops the field silently, and a card that fell back to the deployment
72
+ * default must not be started as if the member's own preset had been pinned.
73
+ */
74
+ mode?: string
75
+ /** Pinned model for the created card's execution. */
76
+ model?: string
77
+ /** Reasoning effort for that model route. */
78
+ reasoningEffort?: string
79
+ }
80
+
81
+ /** Options of {@link TaskBoardTasksFace.create}. */
82
+ export interface TaskBoardCreateOptions {
83
+ /** Opaque payload stored under the creating extension's id. */
84
+ payload?: TaskBoardExtensionPayload
85
+ /** Create the card off-board: it exists in the ledger but is never shown. */
86
+ hidden?: boolean
87
+ }
88
+
89
+ /** One task linked to an extension, with that extension's own payload. */
90
+ export interface TaskBoardLinkedTask {
91
+ task: TaskRecord
92
+ payload: TaskBoardExtensionPayload
93
+ }
94
+
95
+ /** Task reads and writes the board grants an extension. */
96
+ export interface TaskBoardTasksFace {
97
+ /** Every task in the ledger, most recent mutation last. */
98
+ list(): readonly TaskRecord[]
99
+ /** One task by id, or undefined. */
100
+ get(taskId: string): TaskRecord | undefined
101
+ /** Create a task through the board's own creation gate. Throws on refusal. */
102
+ create(draft: TaskBoardTaskDraft, options?: TaskBoardCreateOptions): TaskRecord
103
+ /** Patch a task's content through the board's content gate. Throws when frozen. */
104
+ patchContent(taskId: string, patch: { title?: string; description?: string; prompt?: string }): void
105
+ /** Move a task through the board's status gates. Throws on refusal. */
106
+ setStatus(taskId: string, status: TaskStatus, initiator?: string): void
107
+ /** Every task carrying this extension's payload. */
108
+ linked(): readonly TaskBoardLinkedTask[]
109
+ /**
110
+ * Re-run an executed card in its ORIGINAL session with rework instructions.
111
+ *
112
+ * Optional in the contract: a board built against the earlier shape serves
113
+ * none, so a provider probes before using it.
114
+ */
115
+ rework?(taskId: string, instructions: string, initiator?: string): void
116
+ }
117
+
118
+ /** Execution-session face: delivery into a run the board already has. */
119
+ export interface TaskBoardSessionsFace {
120
+ /** Queue one follow-up user message into the card's current or latest session. */
121
+ followUp(taskId: string, text: string): Promise<void>
122
+ }
123
+
124
+ /** Storage face for one extension's own payload on a task. */
125
+ export interface TaskBoardIntegrationFace {
126
+ /** Read this extension's payload, or undefined when the task carries none. */
127
+ read(taskId: string): TaskBoardExtensionPayload | undefined
128
+ /** Shallow-merge this extension's payload into the task. */
129
+ write(taskId: string, payload: TaskBoardExtensionPayload): void
130
+ }
131
+
132
+ /** Status-change notification. */
133
+ export interface TaskBoardStatusChangedEvent {
134
+ taskId: string
135
+ status: TaskStatus
136
+ previous: TaskStatus
137
+ initiator?: string
138
+ }
139
+
140
+ /** Execution-settlement notification. */
141
+ export interface TaskBoardExecutionSettledEvent {
142
+ taskId: string
143
+ executionId: string
144
+ outcome: ExecutionOutcome
145
+ error?: string
146
+ }
147
+
148
+ /** Task-deletion notification. */
149
+ export interface TaskBoardTaskDeletedEvent {
150
+ taskId: string
151
+ }
152
+
153
+ /** Event subscriptions an extension registers for; every callback runs isolated. */
154
+ export interface TaskBoardEventFace {
155
+ onStatusChanged(callback: (event: TaskBoardStatusChangedEvent) => void): () => void
156
+ onExecutionSettled(callback: (event: TaskBoardExecutionSettledEvent) => void): () => void
157
+ onTaskDeleted(callback: (event: TaskBoardTaskDeletedEvent) => void): () => void
158
+ }
159
+
160
+ /** Host capability face handed to one extension while it is started. */
161
+ export interface TaskBoardExtensionHost {
162
+ tasks: TaskBoardTasksFace
163
+ /** Delivery into a card's execution session; optional member of the contract. */
164
+ sessions?: TaskBoardSessionsFace
165
+ integration: TaskBoardIntegrationFace
166
+ events: TaskBoardEventFace
167
+ /** Publish this extension's read-only summary into the board snapshot. */
168
+ publish(summary: TaskBoardExtensionPayload): void
169
+ /** Register one model-visible tool; the returned disposer unregisters it. */
170
+ registerTool(definition: ToolDefinition): () => void
171
+ }
172
+
173
+ /** One external provider contributed to the task board. */
174
+ export interface TaskBoardExtension {
175
+ /** Stable extension id; also the key of this extension's integration payload. */
176
+ readonly id: string
177
+ /** Contract version this extension was built against. */
178
+ readonly apiVersion: number
179
+ /** Volatile per-extension switch; absent means enabled. Read at use time. */
180
+ enabled?(): boolean
181
+ /** Acquire the capability face and start any provider-side work. */
182
+ start?(host: TaskBoardExtensionHost): void
183
+ /** Release provider-side work. Never clears stored data. */
184
+ stop?(): void
185
+ /** Route one dispatched action; the returned value goes back to the dispatcher. */
186
+ handleAction?(request: TaskBoardExtensionActionRequest): unknown | Promise<unknown>
187
+ }
188
+
189
+ /** Which optional capabilities the board serving this deployment really has. */
190
+ export interface TaskBoardCapabilities {
191
+ /** The board can deliver a follow-up message into an execution session. */
192
+ followUp: boolean
193
+ /** The board can re-run an executed card in its own session. */
194
+ rework: boolean
195
+ /** The host process exposes the board's own action surface (see {@link TaskBoardActionSeat}). */
196
+ run: boolean
197
+ }
198
+
199
+ /**
200
+ * The board's in-process action seat.
201
+ *
202
+ * The provider contract covers everything this extension does EXCEPT starting an
203
+ * execution: the capability face has create, patch, move, follow-up and rework,
204
+ * and no member that runs a card. The board's host face does carry the action
205
+ * applier the browser and the remote bridge drive it through, so the row probes
206
+ * for it and records the capability as absent when a deployment serves none —
207
+ * the mirror card is still created and the settings card says this Host cannot
208
+ * start it.
209
+ */
210
+ export interface TaskBoardActionSeat {
211
+ /** Apply one board action through every gate the board already has. */
212
+ apply(requestId: string, action: Record<string, unknown>, initiator?: string): unknown
213
+ }
214
+
215
+ /**
216
+ * Probe the optional capabilities of the capability face one board handed over.
217
+ * @param host - the capability face a provider was started with.
218
+ * @param run - whether the row found the board's action seat.
219
+ * @returns which optional capabilities this deployment really serves.
220
+ */
221
+ export function probeTaskBoardCapabilities(host: TaskBoardExtensionHost, run: boolean): TaskBoardCapabilities {
222
+ return {
223
+ followUp: typeof host.sessions?.followUp === 'function',
224
+ rework: typeof host.tasks.rework === 'function',
225
+ run,
226
+ }
227
+ }
228
+
229
+ /** Host-half service face published under {@link TASK_BOARD_SERVICE_NAME}. */
230
+ export interface TaskBoardHostFace {
231
+ /** Admit one provider extension; the returned disposer releases it. */
232
+ registerExtension(extension: TaskBoardExtension): () => void
233
+ /** Whether the board currently runs a given extension. */
234
+ isExtensionEnabled(extensionId: string): boolean
235
+ /** The board's in-process action seat, when this board revision serves one. */
236
+ apply?(requestId: string, action: Record<string, unknown>, initiator?: string): unknown
237
+ }
238
+
239
+ /** The narrow context seat the resolvers need. */
240
+ export interface ContextSeat {
241
+ get(name: string): unknown
242
+ }
243
+
244
+ /**
245
+ * Resolve the board's host-half face by service name.
246
+ * @param ctx - host context.
247
+ * @returns the host face, or undefined when the board is not loaded.
248
+ */
249
+ export function resolveTaskBoardHostFace(ctx: ContextSeat): TaskBoardHostFace | undefined {
250
+ try {
251
+ const face = ctx.get(TASK_BOARD_SERVICE_NAME) as TaskBoardHostFace | undefined
252
+ return face !== undefined && typeof (face as { registerExtension?: unknown }).registerExtension === 'function'
253
+ ? face
254
+ : undefined
255
+ } catch {
256
+ return undefined
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Resolve the board's in-process action seat from the same service.
262
+ * @param ctx - host context.
263
+ * @returns the seat, or undefined when this board revision serves no applier.
264
+ */
265
+ export function resolveTaskBoardActionSeat(ctx: ContextSeat): TaskBoardActionSeat | undefined {
266
+ try {
267
+ const face = ctx.get(TASK_BOARD_SERVICE_NAME) as TaskBoardActionSeat | undefined
268
+ return face !== undefined && typeof (face as { apply?: unknown }).apply === 'function' ? face : undefined
269
+ } catch {
270
+ return undefined
271
+ }
272
+ }
273
+ /** Owner props of the task-detail seat. */
274
+ export interface TaskBoardDetailSectionProps {
275
+ task: TaskRecord
276
+ dispatch: TaskBoardExtensionDispatch
277
+ }
278
+
279
+ /** Owner props of the settings seat. */
280
+ export interface TaskBoardSettingsSectionProps {
281
+ dispatch: TaskBoardExtensionDispatch
282
+ }
283
+
284
+ /** Owner props of the card-decoration seat. */
285
+ export interface TaskBoardCardDecorationProps {
286
+ task: TaskRecord
287
+ }
288
+
289
+ /** Board mirror an extension's browser half observes. */
290
+ export interface TaskBoardClientMirror {
291
+ /** Whether the board's master switch currently runs the extension faces. */
292
+ enabled: boolean
293
+ /** Tasks the board currently holds. */
294
+ tasks: readonly TaskRecord[]
295
+ /** Published extension summaries, keyed by extension id. */
296
+ extensions: Readonly<Record<string, unknown>>
297
+ }
298
+
299
+ /** Whether a task should be shown on the board; false hides it. */
300
+ export type TaskBoardVisibilityPredicate = (task: TaskRecord) => boolean
301
+
302
+ /** Client capability face the board's browser half provides. */
303
+ export interface TaskBoardClientFace {
304
+ /** Deliver one action through the board's same-origin action channel. */
305
+ dispatch(request: TaskBoardExtensionActionRequest): Promise<boolean>
306
+ /** Register one card visibility predicate; the returned disposer removes it. */
307
+ registerVisibility(predicate: TaskBoardVisibilityPredicate): () => void
308
+ /** Subscribe to the board mirror. */
309
+ subscribe(callback: (mirror: TaskBoardClientMirror) => void): () => void
310
+ /** Read the current board mirror. */
311
+ snapshot(): TaskBoardClientMirror
312
+ }
313
+
314
+ /**
315
+ * Resolve the board's browser-half client face by service name.
316
+ * @param ctx - browser context.
317
+ * @returns the client face, or undefined when the board is not loaded.
318
+ */
319
+ export function resolveTaskBoardClientFace(ctx: ContextSeat): TaskBoardClientFace | undefined {
320
+ try {
321
+ const face = ctx.get(TASK_BOARD_SERVICE_NAME) as TaskBoardClientFace | undefined
322
+ return face !== undefined && typeof (face as { dispatch?: unknown }).dispatch === 'function' ? face : undefined
323
+ } catch {
324
+ return undefined
325
+ }
326
+ }
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Wire vocabulary of the team-hub protocol v1, as the Host half reads it.
3
+ *
4
+ * The protocol specification (`team-hub/PROTOCOL.md`) is the single source of
5
+ * truth; this module restates only what a Host uses and the operational
6
+ * constants section 13 fixes, so a drift in either direction is visible in one
7
+ * place. Nothing here interprets collaboration semantics: the hub is
8
+ * authoritative over issues, comments, questions and activity, and the Host is
9
+ * authoritative over execution.
10
+ *
11
+ * @module dsh-task-board-team/core/hub-protocol
12
+ */
13
+
14
+ /** Protocol version sent on every REST call and on the WebSocket handshake. */
15
+ export const HUB_PROTOCOL_VERSION = '1.0.0'
16
+
17
+ /** Header carrying {@link HUB_PROTOCOL_VERSION}. */
18
+ export const HUB_PROTOCOL_HEADER = 'X-Team-Hub-Protocol-Version'
19
+
20
+ /** Prefix of every REST path this Host speaks. */
21
+ export const HUB_API_PREFIX = '/api/v1'
22
+
23
+ /** Heartbeat cadence the hub expects from an online Host (section 13). */
24
+ export const HEARTBEAT_INTERVAL_MS = 15_000
25
+
26
+ /** Lease lifetime the hub grants on a claim (section 13). */
27
+ export const LEASE_DURATION_MS = 60_000
28
+
29
+ /** Lease renewal cadence while an execution is active (section 13). */
30
+ export const LEASE_RENEW_INTERVAL_MS = 20_000
31
+
32
+ /** Question lifetime before the hub expires it (section 13). */
33
+ export const QUESTION_TTL_MS = 86_400_000
34
+
35
+ /** Execution summary character cap, aligned with the board's own limit. */
36
+ export const EXECUTION_SUMMARY_LIMIT = 4_000
37
+
38
+ /** Comment text byte cap (section 11). */
39
+ export const COMMENT_LIMIT_BYTES = 32_768
40
+
41
+ /** Cadence the claim sweep is coalesced to (protocol amendment A4 point 6). */
42
+ export const SWEEP_INTERVAL_MS = 5_000
43
+
44
+ /** Backoff the reconnect loop starts at after a dropped WebSocket. */
45
+ export const RECONNECT_BASE_MS = 1_000
46
+
47
+ /** Upper bound of the reconnect backoff; a longer outage never waits longer. */
48
+ export const RECONNECT_MAX_MS = 30_000
49
+
50
+ /** Client version this Host reports at registration. */
51
+ export const HOST_CLIENT_VERSION = '0.4.12'
52
+
53
+ /** Collaboration status of one issue, owned by the hub. */
54
+ export type HubIssueStatus =
55
+ | 'todo'
56
+ | 'assigned'
57
+ | 'waiting_host'
58
+ | 'running'
59
+ | 'in_review'
60
+ | 'done'
61
+ | 'canceled'
62
+ | 'failed'
63
+
64
+ /**
65
+ * One Agent definition this Host registers with the hub.
66
+ *
67
+ * Protocol amendment A4 fixes the registration body as snake_case and requires a
68
+ * non-empty `preset_id`: a member whose definition pins no preset is not
69
+ * reported at all, because the hub would otherwise store an agent this Host can
70
+ * only run under the deployment's default preset.
71
+ */
72
+ export interface HubAgentDefinition {
73
+ /** Name the hub resolves `@` mentions by. */
74
+ name: string
75
+ /** Agent preset id the member definition pins; never empty. */
76
+ preset_id: string
77
+ /** Pinned model of that member, when it pins one. */
78
+ model?: string
79
+ /** Reasoning effort bound to that model, when the member pins one. */
80
+ reasoning_effort?: string
81
+ }
82
+
83
+ /** One Agent record the hub answered registration with. */
84
+ export interface HubAgentRecord {
85
+ id: string
86
+ name: string
87
+ }
88
+
89
+ /** One issue, as the hub serves it. */
90
+ export interface HubIssue {
91
+ id: string
92
+ issue_number: number
93
+ title: string
94
+ description: string
95
+ status: HubIssueStatus
96
+ assignee_type?: 'member' | 'agent'
97
+ assignee_member_id?: string
98
+ assignee_agent_id?: string
99
+ reviewer_member_id?: string
100
+ executing_host_id?: string
101
+ lease_expires_at?: number
102
+ /** Attempt the hub currently holds for this issue; null when it holds none. */
103
+ execution_attempt_id?: string | null
104
+ execution_summary?: string
105
+ /**
106
+ * Rework instructions waiting to be applied (protocol amendment A4 point 5).
107
+ *
108
+ * Non-empty instructions on the issue mean "a review sent this back": whichever
109
+ * path claims the card next must apply them to the existing mirror card.
110
+ */
111
+ rework_instructions?: string | null
112
+ failure_error?: string | null
113
+ updated_at?: number
114
+ }
115
+
116
+ /** What a successful claim hands back. */
117
+ export interface HubClaim {
118
+ lease_token: string
119
+ lease_expires_at: number
120
+ execution_attempt_id: string
121
+ }
122
+
123
+ /** One selectable answer, as the hub exposes it on the wire. */
124
+ export interface HubQuestionOption {
125
+ label: string
126
+ description?: string
127
+ }
128
+
129
+ /**
130
+ * One decision question, in the wire shape (protocol amendment A2).
131
+ *
132
+ * The D1 columns `options_json` and `answer_json` are storage forms only: over
133
+ * the wire both are parsed values, so this client never sees a serialized string
134
+ * and never produces one.
135
+ */
136
+ export interface HubQuestion {
137
+ id: string
138
+ issue_id: string
139
+ /** Question prompt text. */
140
+ text: string
141
+ options: readonly HubQuestionOption[] | null
142
+ multi_select: boolean
143
+ status: 'pending' | 'answered' | 'dismissed' | 'expired'
144
+ answer: HubQuestionAnswer | null
145
+ answered_at: number | null
146
+ }
147
+
148
+ /** One comment on an issue. */
149
+ export interface HubComment {
150
+ id: string
151
+ issue_id: string
152
+ author_type: 'human' | 'agent'
153
+ author_member_id?: string
154
+ author_agent_id?: string
155
+ content: string
156
+ created_at: number
157
+ }
158
+
159
+ /**
160
+ * One entry of the hub's append-only activity stream, in the wire shape: the
161
+ * payload is a JSON object here, and `payload_json` exists only inside D1.
162
+ */
163
+ export interface HubActivity {
164
+ id: string
165
+ issue_id: string
166
+ actor_type: 'member' | 'agent' | 'host' | 'system'
167
+ actor_id: string | null
168
+ activity_type: string
169
+ payload: Record<string, unknown>
170
+ created_at: number
171
+ }
172
+
173
+ /** The answer one human gives to one question (protocol amendment A2). */
174
+ export interface HubQuestionAnswer {
175
+ selected: readonly string[]
176
+ custom?: string
177
+ }
178
+
179
+ /** One client frame the Host sends on the team channel. */
180
+ export type HubClientFrame =
181
+ | { t: 'ping'; ts: number }
182
+ | { t: 'resume'; since_seq: number }
183
+
184
+ /** One server frame the Host reads from the team channel. */
185
+ export type HubServerFrame =
186
+ | { t: 'pong'; ts: number }
187
+ | { t: 'event'; seq: number; event: string; payload: Record<string, unknown>; ts: number }
188
+ | { t: 'resync_required'; reason: string; latest_seq: number }
189
+
190
+ /**
191
+ * Read one server frame, tolerating anything the hub may add.
192
+ * @param raw - one decoded frame.
193
+ * @returns the frame, or undefined when it carries no recognizable shape.
194
+ */
195
+ export function readServerFrame(raw: unknown): HubServerFrame | undefined {
196
+ if (typeof raw !== 'object' || raw === null) return undefined
197
+ const frame = raw as Record<string, unknown>
198
+ switch (frame.t) {
199
+ case 'pong':
200
+ return { t: 'pong', ts: typeof frame.ts === 'number' ? frame.ts : 0 }
201
+ case 'event': {
202
+ if (typeof frame.seq !== 'number' || typeof frame.event !== 'string') return undefined
203
+ const payload = typeof frame.payload === 'object' && frame.payload !== null
204
+ ? frame.payload as Record<string, unknown>
205
+ : {}
206
+ return { t: 'event', seq: frame.seq, event: frame.event, payload, ts: typeof frame.ts === 'number' ? frame.ts : 0 }
207
+ }
208
+ case 'resync_required':
209
+ return {
210
+ t: 'resync_required',
211
+ reason: typeof frame.reason === 'string' ? frame.reason : '',
212
+ latest_seq: typeof frame.latest_seq === 'number' ? frame.latest_seq : 0,
213
+ }
214
+ default:
215
+ return undefined
216
+ }
217
+ }