@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.
- 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 +1117 -0
- package/lib/index.js +2968 -0
- package/lib/types/client/index.d.ts +58 -0
- package/lib/types/client/locales.d.ts +83 -0
- package/lib/types/client/optional-service.d.ts +22 -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 +41 -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 +83 -3
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +139 -0
- package/src/client/locales.ts +154 -0
- package/src/client/optional-service.ts +30 -0
- package/src/client/team/extension.ts +185 -0
- package/src/client/team/sections.tsx +159 -0
- package/src/client/team/settings.tsx +242 -0
- package/src/client/team/summary.ts +156 -0
- package/src/client/team/team.module.css +122 -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,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which local agents this Host registers with the hub.
|
|
3
|
+
*
|
|
4
|
+
* dsh-agents owns the member definitions and writes them to
|
|
5
|
+
* `$DSH_HOME/dsh-agents/ledger.json`; it serves no cordis service for them, so
|
|
6
|
+
* this module reads that ledger file directly, read-only and tolerantly. A
|
|
7
|
+
* missing file, an unreadable one, or a document from another revision leaves
|
|
8
|
+
* the binding list empty and reports why — the Host keeps running, it simply
|
|
9
|
+
* claims nothing, which is the safe direction for a machine that would
|
|
10
|
+
* otherwise execute somebody else's work.
|
|
11
|
+
*
|
|
12
|
+
* The binding itself is a member id: the name, preset and workspace always come
|
|
13
|
+
* from the member definition, never from the browser, so a settings edit can
|
|
14
|
+
* point at a member but cannot invent one.
|
|
15
|
+
*
|
|
16
|
+
* @module dsh-task-board-team/host/bindings
|
|
17
|
+
*/
|
|
18
|
+
import { readFile } from 'node:fs/promises'
|
|
19
|
+
import { homedir } from 'node:os'
|
|
20
|
+
import { join } from 'node:path'
|
|
21
|
+
|
|
22
|
+
/** The plugin directory dsh-agents keeps under the DSH home. */
|
|
23
|
+
export const AGENTS_HOME_DIRECTORY = 'dsh-agents'
|
|
24
|
+
|
|
25
|
+
/** The ledger file dsh-agents writes its teams and members into. */
|
|
26
|
+
export const AGENTS_LEDGER_FILE = 'ledger.json'
|
|
27
|
+
|
|
28
|
+
/** One member definition, as the agents ledger stores it. */
|
|
29
|
+
export interface AgentsMemberDefinition {
|
|
30
|
+
id: string
|
|
31
|
+
teamId: string
|
|
32
|
+
name: string
|
|
33
|
+
preset?: string
|
|
34
|
+
permission?: 'read-only' | 'workspace-write' | 'danger-full-access'
|
|
35
|
+
model?: string
|
|
36
|
+
reasoningEffort?: string
|
|
37
|
+
brief?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** One team in the agents ledger. */
|
|
41
|
+
export interface AgentsTeamDefinition {
|
|
42
|
+
id: string
|
|
43
|
+
name: string
|
|
44
|
+
workspaceId?: string
|
|
45
|
+
members: AgentsMemberDefinition[]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What this module reads out of the agents ledger. */
|
|
49
|
+
export interface AgentsLedgerView {
|
|
50
|
+
teams: AgentsTeamDefinition[]
|
|
51
|
+
/** Why the ledger could not be read, when it could not. */
|
|
52
|
+
problem?: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One member this Host binds as a team Agent. */
|
|
56
|
+
export interface BoundAgent {
|
|
57
|
+
/** dsh-agents member id. */
|
|
58
|
+
memberId: string
|
|
59
|
+
/** Team the member belongs to. */
|
|
60
|
+
teamId: string
|
|
61
|
+
/** Team name, for the operator. */
|
|
62
|
+
teamName: string
|
|
63
|
+
/** Name the hub resolves mentions by. */
|
|
64
|
+
name: string
|
|
65
|
+
/** Agent preset the member definition pins. */
|
|
66
|
+
preset?: string
|
|
67
|
+
/** Model the member definition pins. */
|
|
68
|
+
model?: string
|
|
69
|
+
/** Reasoning effort the member definition pins for that model. */
|
|
70
|
+
reasoningEffort?: string
|
|
71
|
+
/** Workspace the member's team pins, so the mirror card runs where the member does. */
|
|
72
|
+
workspaceId?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One configured binding, as the settings entry stores it. */
|
|
76
|
+
export interface TeamAgentBinding {
|
|
77
|
+
/** dsh-agents member id to bind. */
|
|
78
|
+
memberId: string
|
|
79
|
+
/** Name the hub resolves mentions by; empty falls back to the member's own name. */
|
|
80
|
+
name?: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Resolve the DSH home directory the way every other plugin does.
|
|
85
|
+
* @param env - process environment.
|
|
86
|
+
* @param home - platform home fallback (test seam).
|
|
87
|
+
* @returns the absolute DSH home path.
|
|
88
|
+
*/
|
|
89
|
+
export function resolveDshHome(env: NodeJS.ProcessEnv = process.env, home: string = homedir()): string {
|
|
90
|
+
const raw = env.DSH_HOME
|
|
91
|
+
if (raw !== undefined && raw.trim() !== '') {
|
|
92
|
+
const value = raw.trim()
|
|
93
|
+
if (value.startsWith('~')) return join(home, value.slice(1))
|
|
94
|
+
return value.startsWith('/') || /^[A-Za-z]:[\\/]/.test(value) ? value : join(process.cwd(), value)
|
|
95
|
+
}
|
|
96
|
+
return join(home, '.dsh')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Resolve the path of the agents ledger file.
|
|
101
|
+
* @param env - process environment.
|
|
102
|
+
* @param home - platform home fallback (test seam).
|
|
103
|
+
* @returns the absolute ledger path.
|
|
104
|
+
*/
|
|
105
|
+
export function agentsLedgerPath(env: NodeJS.ProcessEnv = process.env, home: string = homedir()): string {
|
|
106
|
+
return join(resolveDshHome(env, home), AGENTS_HOME_DIRECTORY, AGENTS_LEDGER_FILE)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Read the agents ledger as a member-definition view.
|
|
111
|
+
*
|
|
112
|
+
* Anything unexpected reads as an empty binding list with a reason instead of an
|
|
113
|
+
* exception: this Host has to keep working when dsh-agents is not installed, and
|
|
114
|
+
* a half-written ledger must never turn into a claim of somebody else's work.
|
|
115
|
+
* @param read - file reader seam, defaulting to the filesystem.
|
|
116
|
+
* @returns the teams it found, plus why it found none when that is the case.
|
|
117
|
+
*/
|
|
118
|
+
export async function readAgentsLedger(
|
|
119
|
+
file: string,
|
|
120
|
+
read: (path: string) => Promise<string> = async path => await readFile(path, 'utf8'),
|
|
121
|
+
): Promise<AgentsLedgerView> {
|
|
122
|
+
let raw: string
|
|
123
|
+
try {
|
|
124
|
+
raw = await read(file)
|
|
125
|
+
} catch {
|
|
126
|
+
return { teams: [], problem: 'the dsh-agents ledger is not readable at ' + file }
|
|
127
|
+
}
|
|
128
|
+
let parsed: unknown
|
|
129
|
+
try {
|
|
130
|
+
parsed = JSON.parse(raw)
|
|
131
|
+
} catch {
|
|
132
|
+
return { teams: [], problem: 'the dsh-agents ledger at ' + file + ' is not valid JSON' }
|
|
133
|
+
}
|
|
134
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
135
|
+
return { teams: [], problem: 'the dsh-agents ledger at ' + file + ' has an unknown shape' }
|
|
136
|
+
}
|
|
137
|
+
const rawTeams = (parsed as { teams?: unknown }).teams
|
|
138
|
+
if (!Array.isArray(rawTeams)) return { teams: [], problem: 'the dsh-agents ledger at ' + file + ' lists no teams' }
|
|
139
|
+
const teams: AgentsTeamDefinition[] = []
|
|
140
|
+
for (const entry of rawTeams) {
|
|
141
|
+
if (typeof entry !== 'object' || entry === null) continue
|
|
142
|
+
const team = entry as Record<string, unknown>
|
|
143
|
+
if (typeof team.id !== 'string' || typeof team.name !== 'string') continue
|
|
144
|
+
const members: AgentsMemberDefinition[] = []
|
|
145
|
+
for (const rawMember of Array.isArray(team.members) ? team.members : []) {
|
|
146
|
+
if (typeof rawMember !== 'object' || rawMember === null) continue
|
|
147
|
+
const member = rawMember as Record<string, unknown>
|
|
148
|
+
if (typeof member.id !== 'string' || typeof member.name !== 'string') continue
|
|
149
|
+
members.push({
|
|
150
|
+
id: member.id,
|
|
151
|
+
teamId: team.id,
|
|
152
|
+
name: member.name,
|
|
153
|
+
...(typeof member.preset === 'string' && member.preset !== '' ? { preset: member.preset } : {}),
|
|
154
|
+
...(typeof member.brief === 'string' && member.brief !== '' ? { brief: member.brief } : {}),
|
|
155
|
+
...(typeof member.model === 'string' && member.model !== '' ? { model: member.model } : {}),
|
|
156
|
+
...(typeof member.reasoningEffort === 'string' && member.reasoningEffort !== '' ? { reasoningEffort: member.reasoningEffort } : {}),
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
teams.push({
|
|
160
|
+
id: team.id,
|
|
161
|
+
name: team.name,
|
|
162
|
+
...(typeof team.workspaceId === 'string' && team.workspaceId !== '' ? { workspaceId: team.workspaceId } : {}),
|
|
163
|
+
members,
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
return teams.length === 0
|
|
167
|
+
? { teams, problem: 'the dsh-agents ledger at ' + file + ' defines no team' }
|
|
168
|
+
: { teams }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Project configured bindings onto the member definitions they name.
|
|
173
|
+
*
|
|
174
|
+
* A binding whose member no longer exists is reported and skipped: the hub keeps
|
|
175
|
+
* the agent record it already has, and this Host simply stops claiming for it,
|
|
176
|
+
* which is what an operator unpublishing a member means.
|
|
177
|
+
* @param bindings - the configured member ids.
|
|
178
|
+
* @param view - the members read out of the agents ledger.
|
|
179
|
+
* @returns the resolved agents and one problem line per unusable binding.
|
|
180
|
+
*/
|
|
181
|
+
export function resolveBindings(
|
|
182
|
+
bindings: readonly TeamAgentBinding[],
|
|
183
|
+
view: AgentsLedgerView,
|
|
184
|
+
): { agents: BoundAgent[]; problems: string[] } {
|
|
185
|
+
const agents: BoundAgent[] = []
|
|
186
|
+
const problems: string[] = []
|
|
187
|
+
const seen = new Set<string>()
|
|
188
|
+
for (const binding of bindings) {
|
|
189
|
+
const memberId = binding.memberId.trim()
|
|
190
|
+
if (memberId === '' || seen.has(memberId)) continue
|
|
191
|
+
seen.add(memberId)
|
|
192
|
+
const team = view.teams.find(candidate => candidate.members.some(member => member.id === memberId))
|
|
193
|
+
const member = team?.members.find(candidate => candidate.id === memberId)
|
|
194
|
+
if (team === undefined || member === undefined) {
|
|
195
|
+
problems.push('the bound member ' + memberId + ' is not defined in the dsh-agents ledger')
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
const name = binding.name !== undefined && binding.name.trim() !== '' ? binding.name.trim() : member.name
|
|
199
|
+
agents.push({
|
|
200
|
+
memberId,
|
|
201
|
+
teamId: team.id,
|
|
202
|
+
teamName: team.name,
|
|
203
|
+
name,
|
|
204
|
+
...(member.preset === undefined ? {} : { preset: member.preset }),
|
|
205
|
+
...(member.model === undefined ? {} : { model: member.model }),
|
|
206
|
+
...(member.reasoningEffort === undefined ? {} : { reasoningEffort: member.reasoningEffort }),
|
|
207
|
+
...(team.workspaceId === undefined ? {} : { workspaceId: team.workspaceId }),
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
return { agents, problems }
|
|
211
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The team channel: the WebSocket the hub pushes events over.
|
|
3
|
+
*
|
|
4
|
+
* Both halves of the transport live here rather than in the sync service: the
|
|
5
|
+
* URL is derived from the configured hub (http becomes ws), and one socket is
|
|
6
|
+
* a thin wrapper over whatever the deployment's WebSocket constructor is. The
|
|
7
|
+
* reconnect policy stays with the service, because only it knows whether a
|
|
8
|
+
* dropped connection is a lease-ending outage or a quiet restart.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-task-board-team/host/channel
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** One open socket, as the service drives it. */
|
|
14
|
+
export interface SocketLike {
|
|
15
|
+
/** Send one text frame. */
|
|
16
|
+
send(data: string): void
|
|
17
|
+
/** Close the connection; a later close event is not reported again. */
|
|
18
|
+
close(): void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What one socket reports back. */
|
|
22
|
+
export interface SocketHandlers {
|
|
23
|
+
/** One decoded text frame. */
|
|
24
|
+
message(data: string): void
|
|
25
|
+
/** The connection ended; the service decides whether to reconnect. */
|
|
26
|
+
closed(reason?: string): void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Open one socket at `url`, presenting `headers` on the upgrade, and wire its handlers. */
|
|
30
|
+
export type ConnectSocket = (url: string, handlers: SocketHandlers, headers: Record<string, string>) => SocketLike
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Derive the team channel URL for one hub. The Host Token never travels in the
|
|
34
|
+
* URL; it rides the upgrade's Authorization header (see {@link teamSocketHeaders}).
|
|
35
|
+
* @param hubUrl - configured hub base URL (http/https or ws/wss).
|
|
36
|
+
* @param teamId - team the channel belongs to.
|
|
37
|
+
* @param sinceSeq - last event sequence this Host saw; 0 asks for a full replay.
|
|
38
|
+
* @returns the absolute WebSocket URL.
|
|
39
|
+
*/
|
|
40
|
+
export function teamSocketUrl(hubUrl: string, teamId: string, sinceSeq: number): string {
|
|
41
|
+
const base = hubUrl.trim().replace(/\/+$/, '')
|
|
42
|
+
const socketBase = base.replace(/^http(s?):\/\//i, 'ws$1://')
|
|
43
|
+
const path = '/api/v1/teams/' + encodeURIComponent(teamId) + '/ws'
|
|
44
|
+
const query = '?since_seq=' + String(Math.max(0, Math.floor(sinceSeq)))
|
|
45
|
+
+ '&protocol_version=' + encodeURIComponent('1.0.0')
|
|
46
|
+
return socketBase + path + query
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The upgrade headers the hub authenticates a Host channel with.
|
|
51
|
+
* @param token - Host Token the handshake presents.
|
|
52
|
+
* @returns the Authorization and protocol version headers.
|
|
53
|
+
*/
|
|
54
|
+
export function teamSocketHeaders(token: string): Record<string, string> {
|
|
55
|
+
return { Authorization: 'Bearer ' + token, 'X-Team-Hub-Protocol-Version': '1.0.0' }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The platform constructor with the init form the Host runtime accepts (headers on the upgrade). */
|
|
59
|
+
type HeaderedWebSocket = new (url: string, init: { headers: Record<string, string> }) => WebSocket
|
|
60
|
+
|
|
61
|
+
/** The default socket factory: the platform WebSocket, with the headers on the upgrade. */
|
|
62
|
+
export const platformSocket: ConnectSocket = (url, handlers, headers) => {
|
|
63
|
+
const socket = new (WebSocket as unknown as HeaderedWebSocket)(url, { headers })
|
|
64
|
+
let closed = false
|
|
65
|
+
socket.addEventListener('message', event => {
|
|
66
|
+
const data = (event as { data?: unknown }).data
|
|
67
|
+
if (typeof data === 'string') handlers.message(data)
|
|
68
|
+
})
|
|
69
|
+
socket.addEventListener('close', event => {
|
|
70
|
+
if (closed) return
|
|
71
|
+
closed = true
|
|
72
|
+
handlers.closed((event as { reason?: string }).reason)
|
|
73
|
+
})
|
|
74
|
+
socket.addEventListener('error', () => {
|
|
75
|
+
// A transport error is always followed by a close event on this platform;
|
|
76
|
+
// reporting it here as well would schedule two reconnects for one failure.
|
|
77
|
+
})
|
|
78
|
+
return {
|
|
79
|
+
send(data) {
|
|
80
|
+
if (closed) return
|
|
81
|
+
try {
|
|
82
|
+
socket.send(data)
|
|
83
|
+
} catch {
|
|
84
|
+
// A send racing a close is not a decision this layer makes.
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
close() {
|
|
88
|
+
if (closed) return
|
|
89
|
+
closed = true
|
|
90
|
+
try {
|
|
91
|
+
socket.close()
|
|
92
|
+
} catch {
|
|
93
|
+
// Already gone.
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-side custody of the team-hub Host Token.
|
|
3
|
+
*
|
|
4
|
+
* The token is a credential, so it lives where the harness keeps credentials —
|
|
5
|
+
* never in this extension's own settings entry, never in a board snapshot, never
|
|
6
|
+
* in a published summary and never in any agent payload. The browser sends a
|
|
7
|
+
* pasted value exactly once; the host half stores it and afterwards only reports
|
|
8
|
+
* whether one is configured, where it came from and whether it can be written.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-task-board-team/host/credentials
|
|
11
|
+
*/
|
|
12
|
+
import { credentialRef, isCredentialRefName } from '@deepseek-ai/dsh-credentials'
|
|
13
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
14
|
+
|
|
15
|
+
/** Credential reference name the Host Token is stored under by default. */
|
|
16
|
+
export const DEFAULT_TOKEN_REF = 'DSH_TEAM_HUB_TOKEN'
|
|
17
|
+
|
|
18
|
+
/** Credential facts one reference reports; no slot carries a value. */
|
|
19
|
+
interface CredentialInfo {
|
|
20
|
+
configured: boolean
|
|
21
|
+
source?: string
|
|
22
|
+
writable: boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The slice of the credential seam this extension uses. */
|
|
26
|
+
export interface CredentialFace {
|
|
27
|
+
resolve(ref: unknown): Promise<{ value: string; source?: string } | undefined>
|
|
28
|
+
describe(ref: unknown): Promise<CredentialInfo>
|
|
29
|
+
set(ref: unknown, value: string): Promise<void>
|
|
30
|
+
unset(ref: unknown): Promise<void>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resolve the optional credential service without declaring a required
|
|
35
|
+
* injection: a deployment that serves none keeps working on the environment.
|
|
36
|
+
* @param ctx - host context.
|
|
37
|
+
* @returns the seam, or undefined.
|
|
38
|
+
*/
|
|
39
|
+
export function resolveCredentialFace(ctx: Context): CredentialFace | undefined {
|
|
40
|
+
try {
|
|
41
|
+
const get = (ctx as { get?: (name: string) => unknown }).get
|
|
42
|
+
if (typeof get !== 'function') return undefined
|
|
43
|
+
const face = get.call(ctx, 'credentials') as CredentialFace | undefined
|
|
44
|
+
if (face === undefined) return undefined
|
|
45
|
+
const usable = typeof face.resolve === 'function'
|
|
46
|
+
&& typeof face.describe === 'function'
|
|
47
|
+
&& typeof face.set === 'function'
|
|
48
|
+
&& typeof face.unset === 'function'
|
|
49
|
+
return usable ? face : undefined
|
|
50
|
+
} catch {
|
|
51
|
+
return undefined
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The ambient fallback: the configured reference name only. */
|
|
56
|
+
function ambientToken(env: Record<string, string | undefined>, ref: string): string | undefined {
|
|
57
|
+
const value = env[ref]
|
|
58
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Report one reference's credential facts for a settings surface.
|
|
63
|
+
* @param ctx - host context.
|
|
64
|
+
* @param ref - the configured reference name.
|
|
65
|
+
* @param env - environment consulted for the ambient fallback.
|
|
66
|
+
* @returns the status the settings section renders.
|
|
67
|
+
*/
|
|
68
|
+
export async function describeHostToken(
|
|
69
|
+
ctx: Context,
|
|
70
|
+
ref: string,
|
|
71
|
+
env: Record<string, string | undefined> = process.env,
|
|
72
|
+
): Promise<{ configured: boolean; writable: boolean; source?: string; ref: string }> {
|
|
73
|
+
const face = resolveCredentialFace(ctx)
|
|
74
|
+
let info: CredentialInfo | undefined
|
|
75
|
+
if (face !== undefined && isCredentialRefName(ref)) {
|
|
76
|
+
try {
|
|
77
|
+
info = await face.describe(credentialRef(ref))
|
|
78
|
+
} catch {
|
|
79
|
+
info = undefined
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const ambient = ambientToken(env, ref)
|
|
83
|
+
if (info !== undefined && info.configured) {
|
|
84
|
+
return {
|
|
85
|
+
configured: true,
|
|
86
|
+
writable: info.writable,
|
|
87
|
+
ref,
|
|
88
|
+
...(info.source === undefined ? {} : { source: info.source }),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (ambient !== undefined) {
|
|
92
|
+
return { configured: true, writable: info?.writable ?? false, ref, source: 'environment' }
|
|
93
|
+
}
|
|
94
|
+
return { configured: false, writable: info?.writable ?? face !== undefined, ref }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Resolve the effective Host Token for one reference.
|
|
99
|
+
* @param ctx - host context.
|
|
100
|
+
* @param ref - the configured reference name.
|
|
101
|
+
* @param env - environment consulted for the ambient fallback.
|
|
102
|
+
* @returns the token, or undefined when none is configured.
|
|
103
|
+
*/
|
|
104
|
+
export async function resolveHostToken(
|
|
105
|
+
ctx: Context,
|
|
106
|
+
ref: string,
|
|
107
|
+
env: Record<string, string | undefined> = process.env,
|
|
108
|
+
): Promise<string | undefined> {
|
|
109
|
+
if (ref.trim() === '') return undefined
|
|
110
|
+
const face = resolveCredentialFace(ctx)
|
|
111
|
+
if (face !== undefined && isCredentialRefName(ref)) {
|
|
112
|
+
try {
|
|
113
|
+
const resolved = await face.resolve(credentialRef(ref))
|
|
114
|
+
if (resolved?.value !== undefined && resolved.value.trim() !== '') return resolved.value.trim()
|
|
115
|
+
} catch {
|
|
116
|
+
// An unreadable store falls through to the environment.
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return ambientToken(env, ref)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Store one Host Token in the credential seam.
|
|
124
|
+
* @param ctx - host context.
|
|
125
|
+
* @param ref - the reference to write.
|
|
126
|
+
* @param token - the secret value.
|
|
127
|
+
* @throws when no credential store is served or the store refuses the write.
|
|
128
|
+
*/
|
|
129
|
+
export async function setHostToken(ctx: Context, ref: string, token: string): Promise<void> {
|
|
130
|
+
const face = resolveCredentialFace(ctx)
|
|
131
|
+
if (face === undefined) throw new Error('this deployment serves no credential store; set the Host Token through the environment instead')
|
|
132
|
+
if (!isCredentialRefName(ref)) throw new Error('"' + ref + '" is not a valid credential reference name')
|
|
133
|
+
await face.set(credentialRef(ref), token)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Remove one stored Host Token. Absent values are a no-op.
|
|
138
|
+
* @param ctx - host context.
|
|
139
|
+
* @param ref - the reference to clear.
|
|
140
|
+
* @throws when no credential store is served.
|
|
141
|
+
*/
|
|
142
|
+
export async function clearHostToken(ctx: Context, ref: string): Promise<void> {
|
|
143
|
+
const face = resolveCredentialFace(ctx)
|
|
144
|
+
if (face === undefined) throw new Error('this deployment serves no credential store')
|
|
145
|
+
if (!isCredentialRefName(ref)) throw new Error('"' + ref + '" is not a valid credential reference name')
|
|
146
|
+
await face.unset(credentialRef(ref))
|
|
147
|
+
}
|
|
@@ -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
|
+
}
|