@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.
- package/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +66 -2
- package/README.zh.md +67 -0
- package/cordis.patch.yml +14 -0
- package/icon.svg +3 -0
- package/lib/client.js +1490 -0
- package/lib/index.js +2968 -0
- package/lib/types/client/PluginSettingsCard.d.ts +255 -0
- package/lib/types/client/RangeBubble.d.ts +46 -0
- package/lib/types/client/SegmentGroup.d.ts +28 -0
- package/lib/types/client/index.d.ts +58 -0
- package/lib/types/client/locales.d.ts +90 -0
- package/lib/types/client/optional-service.d.ts +22 -0
- package/lib/types/client/settings-form.d.ts +235 -0
- package/lib/types/client/team/extension.d.ts +29 -0
- package/lib/types/client/team/sections.d.ts +38 -0
- package/lib/types/client/team/settings.d.ts +67 -0
- package/lib/types/client/team/summary.d.ts +34 -0
- package/lib/types/core/contract.d.ts +269 -0
- package/lib/types/core/hub-protocol.d.ts +175 -0
- package/lib/types/core/identity.d.ts +96 -0
- package/lib/types/core/mirror.d.ts +51 -0
- package/lib/types/core/question-report.d.ts +84 -0
- package/lib/types/core/settlement.d.ts +49 -0
- package/lib/types/core/task-record.d.ts +59 -0
- package/lib/types/core/timers.d.ts +20 -0
- package/lib/types/host/bindings.d.ts +92 -0
- package/lib/types/host/channel.d.ts +44 -0
- package/lib/types/host/credentials.d.ts +63 -0
- package/lib/types/host/extension.d.ts +57 -0
- package/lib/types/host/hub-client.d.ts +157 -0
- package/lib/types/host/questions.d.ts +105 -0
- package/lib/types/host/service.d.ts +209 -0
- package/lib/types/host/setup.d.ts +152 -0
- package/lib/types/host/tools.d.ts +19 -0
- package/lib/types/host/waterfall.d.ts +39 -0
- package/lib/types/index.d.ts +152 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +84 -3
- package/src/client/PluginSettingsCard.tsx +838 -0
- package/src/client/RangeBubble.tsx +164 -0
- package/src/client/SegmentGroup.tsx +93 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +139 -0
- package/src/client/locales.ts +168 -0
- package/src/client/optional-service.ts +30 -0
- package/src/client/settings-card.module.css +821 -0
- package/src/client/settings-form.ts +510 -0
- package/src/client/team/extension.ts +185 -0
- package/src/client/team/sections.tsx +159 -0
- package/src/client/team/settings.tsx +348 -0
- package/src/client/team/summary.ts +156 -0
- package/src/client/team/team.module.css +193 -0
- package/src/core/contract.ts +326 -0
- package/src/core/hub-protocol.ts +217 -0
- package/src/core/identity.ts +193 -0
- package/src/core/mirror.ts +88 -0
- package/src/core/question-report.ts +144 -0
- package/src/core/settlement.ts +84 -0
- package/src/core/task-record.ts +65 -0
- package/src/core/timers.ts +34 -0
- package/src/host/bindings.ts +211 -0
- package/src/host/channel.ts +97 -0
- package/src/host/credentials.ts +147 -0
- package/src/host/extension.ts +197 -0
- package/src/host/hub-client.ts +287 -0
- package/src/host/questions.ts +275 -0
- package/src/host/service.ts +1421 -0
- package/src/host/setup.ts +259 -0
- package/src/host/tools.ts +70 -0
- package/src/host/waterfall.ts +51 -0
- package/src/index.ts +452 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The team provider assembled as one task-board extension.
|
|
3
|
+
*
|
|
4
|
+
* Everything team-specific lives behind this factory: the board admits it
|
|
5
|
+
* through the cordis `taskBoard` service, hands it the capability face, and
|
|
6
|
+
* never learns what the provider does with it. The factory imports no board
|
|
7
|
+
* internals — only the same-shape contract in `../core/contract.ts` — and it
|
|
8
|
+
* probes the two optional capabilities instead of assuming them, so a board
|
|
9
|
+
* revision without follow-up or rework loses those deliveries and reports the
|
|
10
|
+
* loss rather than failing to mount.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-task-board-team/host/extension
|
|
13
|
+
*/
|
|
14
|
+
import {
|
|
15
|
+
TASK_BOARD_API_VERSION,
|
|
16
|
+
probeTaskBoardCapabilities,
|
|
17
|
+
type TaskBoardCapabilities,
|
|
18
|
+
type TaskBoardExtension,
|
|
19
|
+
type TaskBoardExtensionActionRequest,
|
|
20
|
+
type TaskBoardExtensionHost,
|
|
21
|
+
} from '../core/contract.ts'
|
|
22
|
+
import { TEAM_EXTENSION_ID } from '../core/identity.ts'
|
|
23
|
+
import { processTimers, type HostTimerFace } from '../core/timers.ts'
|
|
24
|
+
import type { SocketHandlers, SocketLike } from './channel.ts'
|
|
25
|
+
import type { BoundAgent } from './bindings.ts'
|
|
26
|
+
import type { TeamRuntimeSettings, TeamSyncService } from './service.ts'
|
|
27
|
+
import { TeamSyncService as SyncService } from './service.ts'
|
|
28
|
+
import type { TeamSetup, TeamSetupPatch } from './setup.ts'
|
|
29
|
+
import { buildTeamTools } from './tools.ts'
|
|
30
|
+
import type { HubClient } from './hub-client.ts'
|
|
31
|
+
|
|
32
|
+
export { TEAM_EXTENSION_ID }
|
|
33
|
+
|
|
34
|
+
export interface TeamExtensionOptions {
|
|
35
|
+
/** Hub REST client. */
|
|
36
|
+
hub: HubClient
|
|
37
|
+
/** Live settings of this mount. */
|
|
38
|
+
settings: () => TeamRuntimeSettings
|
|
39
|
+
/** Resolve the bound members out of the dsh-agents ledger. */
|
|
40
|
+
resolveAgents: () => Promise<{ agents: BoundAgent[]; problems: string[] }>
|
|
41
|
+
/** Open the team channel. */
|
|
42
|
+
connect: (sinceSeq: number, handlers: SocketHandlers) => Promise<SocketLike | undefined>
|
|
43
|
+
/** Ask the board to run a card; absent means this Host cannot start executions. */
|
|
44
|
+
requestRun?: (taskId: string) => void
|
|
45
|
+
/** Configuration surface the settings section drives through these actions. */
|
|
46
|
+
setup?: TeamSetup
|
|
47
|
+
/** Non-secret configuration the settings section renders, read at publish time. */
|
|
48
|
+
configuration?: () => Record<string, unknown>
|
|
49
|
+
/** Called with the live service on start and with undefined on stop. */
|
|
50
|
+
onService?: (service: TeamSyncService | undefined) => void
|
|
51
|
+
/** Volatile master switch, read at use time. Absent means enabled. */
|
|
52
|
+
enabled?: () => boolean
|
|
53
|
+
/** Whether the deployment exposes the board's action seat (the run capability). */
|
|
54
|
+
actionSeat?: () => boolean
|
|
55
|
+
timers?: HostTimerFace
|
|
56
|
+
now?: () => number
|
|
57
|
+
uuid?: () => string
|
|
58
|
+
log?: (message: string, detail?: unknown) => void
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** One identifier per run action the board's applier expects. */
|
|
62
|
+
function requestId(): string {
|
|
63
|
+
return 'team-' + Math.random().toString(36).slice(2) + '-' + Date.now().toString(36)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Build the team provider extension.
|
|
68
|
+
* @param options - deployment configuration for the integration.
|
|
69
|
+
* @returns the extension the board registers.
|
|
70
|
+
*/
|
|
71
|
+
export function createTeamExtension(options: TeamExtensionOptions): TaskBoardExtension {
|
|
72
|
+
let service: TeamSyncService | undefined
|
|
73
|
+
let capabilities: TaskBoardCapabilities = { followUp: false, rework: false, run: false }
|
|
74
|
+
const disposers: Array<() => void> = []
|
|
75
|
+
const log = options.log ?? ((message: string, detail?: unknown) => {
|
|
76
|
+
if (detail === undefined) console.error('[dsh-task-board-team] ' + message)
|
|
77
|
+
else console.error('[dsh-task-board-team] ' + message, detail)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const start = (host: TaskBoardExtensionHost): void => {
|
|
81
|
+
capabilities = probeTaskBoardCapabilities(host, options.actionSeat?.() === true)
|
|
82
|
+
// The settings section renders the configured hub, team and agent bindings,
|
|
83
|
+
// and those are not execution state: they ride the same published summary so
|
|
84
|
+
// the browser reads one document instead of opening a second surface.
|
|
85
|
+
const publishing: TaskBoardExtensionHost = options.configuration === undefined
|
|
86
|
+
? host
|
|
87
|
+
: {
|
|
88
|
+
...host,
|
|
89
|
+
publish: (summary) => { host.publish({ ...summary, configuration: options.configuration?.() ?? {} }) },
|
|
90
|
+
}
|
|
91
|
+
const live = new SyncService({
|
|
92
|
+
host: publishing,
|
|
93
|
+
hub: options.hub,
|
|
94
|
+
settings: options.settings,
|
|
95
|
+
resolveAgents: options.resolveAgents,
|
|
96
|
+
connect: options.connect,
|
|
97
|
+
capabilities,
|
|
98
|
+
timers: options.timers ?? processTimers,
|
|
99
|
+
now: options.now ?? (() => Date.now()),
|
|
100
|
+
uuid: options.uuid ?? requestId,
|
|
101
|
+
log,
|
|
102
|
+
...(options.requestRun === undefined ? {} : { requestRun: options.requestRun }),
|
|
103
|
+
})
|
|
104
|
+
service = live
|
|
105
|
+
options.onService?.(live)
|
|
106
|
+
disposers.push(host.events.onExecutionSettled(event => {
|
|
107
|
+
void live.handleExecutionSettled(event).catch(error => { log('settlement write-back failed', error) })
|
|
108
|
+
}))
|
|
109
|
+
for (const tool of buildTeamTools(live)) {
|
|
110
|
+
try {
|
|
111
|
+
disposers.push(host.registerTool(tool))
|
|
112
|
+
} catch (error) {
|
|
113
|
+
// A missing tool registry costs the comment tool, not the provider.
|
|
114
|
+
log('team_comment could not register', error)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
host.publish(live.summary() as unknown as Record<string, unknown>)
|
|
119
|
+
} catch (error) {
|
|
120
|
+
log('initial summary was refused', error)
|
|
121
|
+
}
|
|
122
|
+
void live.start().catch(error => { log('provider start failed', error) })
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
id: TEAM_EXTENSION_ID,
|
|
127
|
+
apiVersion: TASK_BOARD_API_VERSION,
|
|
128
|
+
enabled: () => options.enabled?.() ?? true,
|
|
129
|
+
start,
|
|
130
|
+
stop() {
|
|
131
|
+
for (const dispose of disposers.splice(0)) {
|
|
132
|
+
try { dispose() } catch { /* best-effort */ }
|
|
133
|
+
}
|
|
134
|
+
options.onService?.(undefined)
|
|
135
|
+
service?.stop()
|
|
136
|
+
service = undefined
|
|
137
|
+
},
|
|
138
|
+
async handleAction(request: TaskBoardExtensionActionRequest) {
|
|
139
|
+
const live = service
|
|
140
|
+
if (live === undefined) throw new Error('the team extension is not running')
|
|
141
|
+
switch (request.action) {
|
|
142
|
+
case 'set-token': {
|
|
143
|
+
const setup = options.setup
|
|
144
|
+
if (setup === undefined) throw new Error('this mount serves no configuration surface')
|
|
145
|
+
const value = typeof request.payload?.token === 'string' ? request.payload.token : ''
|
|
146
|
+
if (value.trim() === '') throw new Error('the Host Token must be a non-empty value')
|
|
147
|
+
await setup.saveToken(value)
|
|
148
|
+
return { ok: true, status: await setup.status() }
|
|
149
|
+
}
|
|
150
|
+
case 'clear-token': {
|
|
151
|
+
const setup = options.setup
|
|
152
|
+
if (setup === undefined) throw new Error('this mount serves no configuration surface')
|
|
153
|
+
await setup.clearToken()
|
|
154
|
+
return { ok: true, status: await setup.status() }
|
|
155
|
+
}
|
|
156
|
+
case 'setup-status': {
|
|
157
|
+
const setup = options.setup
|
|
158
|
+
if (setup === undefined) throw new Error('this mount serves no configuration surface')
|
|
159
|
+
return { ok: true, status: await setup.status() }
|
|
160
|
+
}
|
|
161
|
+
case 'setup-save': {
|
|
162
|
+
const setup = options.setup
|
|
163
|
+
if (setup === undefined) throw new Error('this mount serves no configuration surface')
|
|
164
|
+
const payload = request.payload ?? {}
|
|
165
|
+
const patch: TeamSetupPatch = {}
|
|
166
|
+
if (typeof payload.hubUrl === 'string') patch.hubUrl = payload.hubUrl
|
|
167
|
+
if (typeof payload.teamId === 'string') patch.teamId = payload.teamId
|
|
168
|
+
if (typeof payload.hostname === 'string') patch.hostname = payload.hostname
|
|
169
|
+
if (typeof payload.questionWaitMinutes === 'number' && Number.isFinite(payload.questionWaitMinutes)) {
|
|
170
|
+
patch.questionWaitMinutes = payload.questionWaitMinutes
|
|
171
|
+
}
|
|
172
|
+
if (Array.isArray(payload.agents)) {
|
|
173
|
+
patch.agents = payload.agents.flatMap(entry => {
|
|
174
|
+
if (typeof entry !== 'object' || entry === null) return []
|
|
175
|
+
const row = entry as Record<string, unknown>
|
|
176
|
+
if (typeof row.memberId !== 'string') return []
|
|
177
|
+
return [{ memberId: row.memberId, ...(typeof row.name === 'string' ? { name: row.name } : {}) }]
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
return { ok: true, status: await setup.save(patch) }
|
|
181
|
+
}
|
|
182
|
+
case 'refresh':
|
|
183
|
+
return { ok: true, claimed: await live.sweep() }
|
|
184
|
+
case 'comment': {
|
|
185
|
+
const text = typeof request.payload?.text === 'string' ? request.payload.text : ''
|
|
186
|
+
if (text.trim() === '') throw new Error('a comment needs non-empty text')
|
|
187
|
+
return { ok: true, ...(await live.postComment({
|
|
188
|
+
text,
|
|
189
|
+
...(request.taskId === undefined ? {} : { taskId: request.taskId }),
|
|
190
|
+
})) }
|
|
191
|
+
}
|
|
192
|
+
default:
|
|
193
|
+
throw new Error('unknown team action "' + request.action + '"')
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Host's REST client for one team-hub.
|
|
3
|
+
*
|
|
4
|
+
* Every call carries the Host Token and the protocol version, resolves its base
|
|
5
|
+
* URL and team at call time (so a settings edit reaches the next call without a
|
|
6
|
+
* remount), and turns the hub's unified error envelope into one diagnosable
|
|
7
|
+
* error carrying its code. The transport is injected: a deployment uses the
|
|
8
|
+
* global fetch, and the test suite drives a fake hub through the same seam.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-task-board-team/host/hub-client
|
|
11
|
+
*/
|
|
12
|
+
import {
|
|
13
|
+
HUB_API_PREFIX,
|
|
14
|
+
HUB_PROTOCOL_HEADER,
|
|
15
|
+
HUB_PROTOCOL_VERSION,
|
|
16
|
+
type HubActivity,
|
|
17
|
+
type HubAgentDefinition,
|
|
18
|
+
type HubAgentRecord,
|
|
19
|
+
type HubClaim,
|
|
20
|
+
type HubComment,
|
|
21
|
+
type HubIssue,
|
|
22
|
+
type HubQuestion,
|
|
23
|
+
type HubQuestionAnswer,
|
|
24
|
+
} from '../core/hub-protocol.ts'
|
|
25
|
+
import type { QuestionReportBody } from '../core/question-report.ts'
|
|
26
|
+
import type { SettleRequestBody } from '../core/settlement.ts'
|
|
27
|
+
|
|
28
|
+
/** One response as this client reads it. */
|
|
29
|
+
export interface HubResponse {
|
|
30
|
+
ok: boolean
|
|
31
|
+
status: number
|
|
32
|
+
text(): Promise<string>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The transport one deployment hands the client. */
|
|
36
|
+
export type HubFetch = (
|
|
37
|
+
url: string,
|
|
38
|
+
init: { method: string; headers: Record<string, string>; body?: string },
|
|
39
|
+
) => Promise<HubResponse>
|
|
40
|
+
|
|
41
|
+
/** One refusal the hub answered with. */
|
|
42
|
+
export class HubError extends Error {
|
|
43
|
+
/** Machine-readable code from the unified error envelope. */
|
|
44
|
+
readonly code: string
|
|
45
|
+
/** HTTP status the hub answered with. */
|
|
46
|
+
readonly status: number
|
|
47
|
+
|
|
48
|
+
constructor(code: string, message: string, status: number) {
|
|
49
|
+
super(message)
|
|
50
|
+
this.name = 'HubError'
|
|
51
|
+
this.code = code
|
|
52
|
+
this.status = status
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Read the code out of the hub's unified error envelope. */
|
|
57
|
+
function readErrorCode(body: unknown, status: number): { code: string; message: string } {
|
|
58
|
+
if (typeof body === 'object' && body !== null) {
|
|
59
|
+
const error = (body as { error?: unknown }).error
|
|
60
|
+
if (typeof error === 'object' && error !== null) {
|
|
61
|
+
const envelope = error as { code?: unknown; message?: unknown }
|
|
62
|
+
return {
|
|
63
|
+
code: typeof envelope.code === 'string' ? envelope.code : 'HTTP_' + String(status),
|
|
64
|
+
message: typeof envelope.message === 'string' ? envelope.message : 'the hub refused the request',
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { code: 'HTTP_' + String(status), message: 'the hub refused the request' }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface HubClientOptions {
|
|
72
|
+
/** Hub base URL, read per call. */
|
|
73
|
+
hubUrl: () => string
|
|
74
|
+
/** Team id, read per call. */
|
|
75
|
+
teamId: () => string
|
|
76
|
+
/** Host Token, resolved per call from the credential store. */
|
|
77
|
+
token: () => Promise<string | undefined>
|
|
78
|
+
/** Transport; the global fetch when absent. */
|
|
79
|
+
fetch?: HubFetch
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The REST surface one Host speaks to one team. */
|
|
83
|
+
export class HubClient {
|
|
84
|
+
readonly #options: HubClientOptions
|
|
85
|
+
|
|
86
|
+
constructor(options: HubClientOptions) {
|
|
87
|
+
this.#options = options
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Whether this deployment can reach a hub at all (a URL and a token exist). */
|
|
91
|
+
async configured(): Promise<boolean> {
|
|
92
|
+
return this.#base().ok
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Register this Host and synchronize its agent definitions.
|
|
97
|
+
*
|
|
98
|
+
* The body is snake_case end to end (protocol amendment A4 point 1): the hub
|
|
99
|
+
* stores exactly these keys, so a camelCase member is a field it never sees.
|
|
100
|
+
* @param body - hostname, client version and the local agent definitions.
|
|
101
|
+
* @returns the registered host id, status and the agent records the hub now holds.
|
|
102
|
+
*/
|
|
103
|
+
async register(body: {
|
|
104
|
+
hostname: string
|
|
105
|
+
client_version: string
|
|
106
|
+
agents: readonly HubAgentDefinition[]
|
|
107
|
+
}): Promise<{ host_id: string; status: string; agents?: readonly HubAgentRecord[] }> {
|
|
108
|
+
return await this.#call('POST', '/hosts/register', body) as {
|
|
109
|
+
host_id: string
|
|
110
|
+
status: string
|
|
111
|
+
agents?: readonly HubAgentRecord[]
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Send one heartbeat.
|
|
117
|
+
* @param hostId - the registered host id.
|
|
118
|
+
* @returns the hub's server time.
|
|
119
|
+
*/
|
|
120
|
+
async heartbeat(hostId: string): Promise<{ server_time?: number }> {
|
|
121
|
+
return await this.#call('POST', `/hosts/${encodeURIComponent(hostId)}/heartbeat`) as { server_time?: number }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* List the issues this Host may claim.
|
|
126
|
+
* @param hostId - the registered host id.
|
|
127
|
+
* @returns the claimable issues.
|
|
128
|
+
*/
|
|
129
|
+
async listClaimableIssues(hostId: string): Promise<readonly HubIssue[]> {
|
|
130
|
+
const query = '?assignee_host_id=' + encodeURIComponent(hostId) + '&status=' + encodeURIComponent('assigned,waiting_host')
|
|
131
|
+
const body = await this.#call('GET', '/issues' + query) as { issues?: unknown }
|
|
132
|
+
return Array.isArray(body.issues) ? body.issues as HubIssue[] : []
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Claim one issue, or re-establish the attempt already running locally.
|
|
137
|
+
* @param issueId - the issue to claim.
|
|
138
|
+
* @param credentials - the previous attempt credentials, when this is a reconnect.
|
|
139
|
+
* @returns the lease.
|
|
140
|
+
*/
|
|
141
|
+
async claim(issueId: string, credentials: { execution_attempt_id?: string; lease_token?: string } = {}): Promise<HubClaim> {
|
|
142
|
+
return await this.#call('POST', `/issues/${encodeURIComponent(issueId)}/claim`, credentials) as unknown as HubClaim
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Extend one lease.
|
|
147
|
+
* @param issueId - the claimed issue.
|
|
148
|
+
* @param leaseToken - the lease credential.
|
|
149
|
+
* @returns the new expiry.
|
|
150
|
+
*/
|
|
151
|
+
async renewLease(issueId: string, leaseToken: string): Promise<{ lease_expires_at: number }> {
|
|
152
|
+
return await this.#call('POST', `/issues/${encodeURIComponent(issueId)}/renew-lease`, { lease_token: leaseToken }) as { lease_expires_at: number }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Write one settlement back.
|
|
157
|
+
* @param issueId - the settled issue.
|
|
158
|
+
* @param body - outcome, summary and error.
|
|
159
|
+
* @returns the collaboration status the hub moved to.
|
|
160
|
+
*/
|
|
161
|
+
async settle(issueId: string, body: SettleRequestBody): Promise<{ ok: boolean; status: string }> {
|
|
162
|
+
return await this.#call('POST', `/issues/${encodeURIComponent(issueId)}/settle`, body) as { ok: boolean; status: string }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Report one decision question.
|
|
167
|
+
* @param issueId - the issue it belongs to.
|
|
168
|
+
* @param body - the question text, options and multi-select flag.
|
|
169
|
+
* @returns the stored question.
|
|
170
|
+
*/
|
|
171
|
+
async createQuestion(issueId: string, body: QuestionReportBody): Promise<HubQuestion> {
|
|
172
|
+
const response = await this.#call('POST', `/issues/${encodeURIComponent(issueId)}/questions`, body) as { question?: HubQuestion }
|
|
173
|
+
if (response.question === undefined) throw new HubError('INVALID_RESPONSE', 'the hub returned no question', 200)
|
|
174
|
+
return response.question
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Withdraw one pending question after the local asker gave up.
|
|
179
|
+
* @param questionId - the question to withdraw.
|
|
180
|
+
*/
|
|
181
|
+
async withdrawQuestion(questionId: string): Promise<void> {
|
|
182
|
+
await this.#call('POST', '/questions/' + encodeURIComponent(questionId) + '/withdraw')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Post one agent comment.
|
|
187
|
+
* @param issueId - the issue to comment on.
|
|
188
|
+
* @param body - the content and the agent identity that wrote it.
|
|
189
|
+
* @returns the stored comment.
|
|
190
|
+
*/
|
|
191
|
+
async createComment(issueId: string, body: { content: string; author_type: 'agent'; author_agent_id: string }): Promise<HubComment> {
|
|
192
|
+
const response = await this.#call('POST', `/issues/${encodeURIComponent(issueId)}/comments`, body) as { comment?: HubComment }
|
|
193
|
+
if (response.comment === undefined) throw new HubError('INVALID_RESPONSE', 'the hub returned no comment', 200)
|
|
194
|
+
return response.comment
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Read one issue's comment thread.
|
|
199
|
+
* @param issueId - the issue to read.
|
|
200
|
+
* @returns the comments, oldest first.
|
|
201
|
+
*/
|
|
202
|
+
async comments(issueId: string): Promise<readonly HubComment[]> {
|
|
203
|
+
const body = await this.#call('GET', `/issues/${encodeURIComponent(issueId)}/comments`) as { comments?: unknown }
|
|
204
|
+
return Array.isArray(body.comments) ? body.comments as HubComment[] : []
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The base URL and team, or the reason there is none. */
|
|
208
|
+
#base(): { ok: true; url: string } | { ok: false } {
|
|
209
|
+
const raw = this.#options.hubUrl().trim()
|
|
210
|
+
const team = this.#options.teamId().trim()
|
|
211
|
+
if (raw === '' || team === '') return { ok: false }
|
|
212
|
+
return { ok: true, url: raw.replace(/\/+$/, '') + HUB_API_PREFIX + '/teams/' + encodeURIComponent(team) }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async #call(method: string, path: string, body?: unknown): Promise<Record<string, unknown>> {
|
|
216
|
+
const base = this.#base()
|
|
217
|
+
if (!base.ok) throw new HubError('NOT_CONFIGURED', 'the team hub URL and team id are not configured', 0)
|
|
218
|
+
const token = await this.#options.token()
|
|
219
|
+
if (token === undefined || token === '') throw new HubError('UNAUTHORIZED', 'no Host Token is configured', 401)
|
|
220
|
+
const headers: Record<string, string> = {
|
|
221
|
+
authorization: 'Bearer ' + token,
|
|
222
|
+
[HUB_PROTOCOL_HEADER]: HUB_PROTOCOL_VERSION,
|
|
223
|
+
accept: 'application/json',
|
|
224
|
+
// The frozen samples declare it on every Host call, body or not
|
|
225
|
+
// (protocol amendment A4 point 9), so it is never conditional here.
|
|
226
|
+
'content-type': 'application/json',
|
|
227
|
+
}
|
|
228
|
+
const response = await this.#fetch()(
|
|
229
|
+
base.url + path,
|
|
230
|
+
{ method, headers, ...(body === undefined ? {} : { body: JSON.stringify(body) }) },
|
|
231
|
+
)
|
|
232
|
+
const text = await response.text()
|
|
233
|
+
let parsed: unknown
|
|
234
|
+
try {
|
|
235
|
+
parsed = text === '' ? {} : JSON.parse(text)
|
|
236
|
+
} catch {
|
|
237
|
+
parsed = undefined
|
|
238
|
+
}
|
|
239
|
+
if (!response.ok) {
|
|
240
|
+
const { code, message } = readErrorCode(parsed, response.status)
|
|
241
|
+
throw new HubError(code, message, response.status)
|
|
242
|
+
}
|
|
243
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
244
|
+
throw new HubError('INVALID_RESPONSE', 'the hub returned a body this client cannot read', response.status)
|
|
245
|
+
}
|
|
246
|
+
return parsed as Record<string, unknown>
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
#fetch(): HubFetch {
|
|
250
|
+
const injected = this.#options.fetch
|
|
251
|
+
if (injected !== undefined) return injected
|
|
252
|
+
return async (url, init) => {
|
|
253
|
+
const response = await fetch(url, init)
|
|
254
|
+
return {
|
|
255
|
+
ok: response.ok,
|
|
256
|
+
status: response.status,
|
|
257
|
+
text: async () => await response.text(),
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The activity entry one status change appends, as the browser renders it. */
|
|
264
|
+
export interface ActivityEntry {
|
|
265
|
+
id: string
|
|
266
|
+
type: string
|
|
267
|
+
text: string
|
|
268
|
+
createdAt: number
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Project one hub activity row into the shape the summary carries.
|
|
273
|
+
*
|
|
274
|
+
* The wire carries the payload as a JSON object (its `payload_json` column is a
|
|
275
|
+
* D1 storage form only), so the rendered text is this Host's own rendering of
|
|
276
|
+
* that object rather than a serialized column.
|
|
277
|
+
* @param activity - the row the hub broadcast or served.
|
|
278
|
+
* @returns the entry the browser renders.
|
|
279
|
+
*/
|
|
280
|
+
export function projectActivity(activity: HubActivity): ActivityEntry {
|
|
281
|
+
return {
|
|
282
|
+
id: activity.id,
|
|
283
|
+
type: activity.activity_type,
|
|
284
|
+
text: Object.keys(activity.payload).length === 0 ? activity.activity_type : JSON.stringify(activity.payload),
|
|
285
|
+
createdAt: activity.created_at,
|
|
286
|
+
}
|
|
287
|
+
}
|