@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
package/lib/index.js
ADDED
|
@@ -0,0 +1,2968 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { credentialRef, isCredentialRefName } from "@deepseek-ai/dsh-credentials";
|
|
6
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
7
|
+
//#region src/host/waterfall.ts
|
|
8
|
+
/**
|
|
9
|
+
* Install the provider's one waterfall claim.
|
|
10
|
+
*
|
|
11
|
+
* The listener is prepended so the team routing is consulted before the desktop
|
|
12
|
+
* answerer for a team card's session; every request that is not such a session's
|
|
13
|
+
* is delegated straight back down the chain, so the local surface keeps working
|
|
14
|
+
* exactly as it did before this provider existed.
|
|
15
|
+
* @param seat - the host context's event seat.
|
|
16
|
+
* @param router - resolves the question router of the live mount, if any.
|
|
17
|
+
* @returns disposer removing the subscription.
|
|
18
|
+
*/
|
|
19
|
+
function installTeamWaterfall(seat, router) {
|
|
20
|
+
return seat.on("user-questions/request", (request, next) => {
|
|
21
|
+
const live = router();
|
|
22
|
+
if (live === void 0) return next();
|
|
23
|
+
return live.request(request, next);
|
|
24
|
+
}, { prepend: true });
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/core/contract.ts
|
|
28
|
+
/** Cordis service name the board's host and browser halves each publish. */
|
|
29
|
+
const TASK_BOARD_SERVICE_NAME = "taskBoard";
|
|
30
|
+
/**
|
|
31
|
+
* Probe the optional capabilities of the capability face one board handed over.
|
|
32
|
+
* @param host - the capability face a provider was started with.
|
|
33
|
+
* @param run - whether the row found the board's action seat.
|
|
34
|
+
* @returns which optional capabilities this deployment really serves.
|
|
35
|
+
*/
|
|
36
|
+
function probeTaskBoardCapabilities(host, run) {
|
|
37
|
+
return {
|
|
38
|
+
followUp: typeof host.sessions?.followUp === "function",
|
|
39
|
+
rework: typeof host.tasks.rework === "function",
|
|
40
|
+
run
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Resolve the board's host-half face by service name.
|
|
45
|
+
* @param ctx - host context.
|
|
46
|
+
* @returns the host face, or undefined when the board is not loaded.
|
|
47
|
+
*/
|
|
48
|
+
function resolveTaskBoardHostFace(ctx) {
|
|
49
|
+
try {
|
|
50
|
+
const face = ctx.get(TASK_BOARD_SERVICE_NAME);
|
|
51
|
+
return face !== void 0 && typeof face.registerExtension === "function" ? face : void 0;
|
|
52
|
+
} catch {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolve the board's in-process action seat from the same service.
|
|
58
|
+
* @param ctx - host context.
|
|
59
|
+
* @returns the seat, or undefined when this board revision serves no applier.
|
|
60
|
+
*/
|
|
61
|
+
function resolveTaskBoardActionSeat(ctx) {
|
|
62
|
+
try {
|
|
63
|
+
const face = ctx.get(TASK_BOARD_SERVICE_NAME);
|
|
64
|
+
return face !== void 0 && typeof face.apply === "function" ? face : void 0;
|
|
65
|
+
} catch {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/core/timers.ts
|
|
71
|
+
/** The process timer face: what a real Host mount uses. */
|
|
72
|
+
const processTimers = {
|
|
73
|
+
timeout(callback, delay) {
|
|
74
|
+
const handle = setTimeout(callback, delay);
|
|
75
|
+
handle.unref?.();
|
|
76
|
+
return () => {
|
|
77
|
+
clearTimeout(handle);
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
interval(callback, delay) {
|
|
81
|
+
const handle = setInterval(callback, delay);
|
|
82
|
+
handle.unref?.();
|
|
83
|
+
return () => {
|
|
84
|
+
clearInterval(handle);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/host/bindings.ts
|
|
90
|
+
/**
|
|
91
|
+
* Which local agents this Host registers with the hub.
|
|
92
|
+
*
|
|
93
|
+
* dsh-agents owns the member definitions and writes them to
|
|
94
|
+
* `$DSH_HOME/dsh-agents/ledger.json`; it serves no cordis service for them, so
|
|
95
|
+
* this module reads that ledger file directly, read-only and tolerantly. A
|
|
96
|
+
* missing file, an unreadable one, or a document from another revision leaves
|
|
97
|
+
* the binding list empty and reports why — the Host keeps running, it simply
|
|
98
|
+
* claims nothing, which is the safe direction for a machine that would
|
|
99
|
+
* otherwise execute somebody else's work.
|
|
100
|
+
*
|
|
101
|
+
* The binding itself is a member id: the name, preset and workspace always come
|
|
102
|
+
* from the member definition, never from the browser, so a settings edit can
|
|
103
|
+
* point at a member but cannot invent one.
|
|
104
|
+
*
|
|
105
|
+
* @module dsh-task-board-team/host/bindings
|
|
106
|
+
*/
|
|
107
|
+
/** The plugin directory dsh-agents keeps under the DSH home. */
|
|
108
|
+
const AGENTS_HOME_DIRECTORY = "dsh-agents";
|
|
109
|
+
/** The ledger file dsh-agents writes its teams and members into. */
|
|
110
|
+
const AGENTS_LEDGER_FILE = "ledger.json";
|
|
111
|
+
/**
|
|
112
|
+
* Resolve the DSH home directory the way every other plugin does.
|
|
113
|
+
* @param env - process environment.
|
|
114
|
+
* @param home - platform home fallback (test seam).
|
|
115
|
+
* @returns the absolute DSH home path.
|
|
116
|
+
*/
|
|
117
|
+
function resolveDshHome(env = process.env, home = homedir()) {
|
|
118
|
+
const raw = env.DSH_HOME;
|
|
119
|
+
if (raw !== void 0 && raw.trim() !== "") {
|
|
120
|
+
const value = raw.trim();
|
|
121
|
+
if (value.startsWith("~")) return join(home, value.slice(1));
|
|
122
|
+
return value.startsWith("/") || /^[A-Za-z]:[\\/]/.test(value) ? value : join(process.cwd(), value);
|
|
123
|
+
}
|
|
124
|
+
return join(home, ".dsh");
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Resolve the path of the agents ledger file.
|
|
128
|
+
* @param env - process environment.
|
|
129
|
+
* @param home - platform home fallback (test seam).
|
|
130
|
+
* @returns the absolute ledger path.
|
|
131
|
+
*/
|
|
132
|
+
function agentsLedgerPath(env = process.env, home = homedir()) {
|
|
133
|
+
return join(resolveDshHome(env, home), AGENTS_HOME_DIRECTORY, AGENTS_LEDGER_FILE);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Read the agents ledger as a member-definition view.
|
|
137
|
+
*
|
|
138
|
+
* Anything unexpected reads as an empty binding list with a reason instead of an
|
|
139
|
+
* exception: this Host has to keep working when dsh-agents is not installed, and
|
|
140
|
+
* a half-written ledger must never turn into a claim of somebody else's work.
|
|
141
|
+
* @param read - file reader seam, defaulting to the filesystem.
|
|
142
|
+
* @returns the teams it found, plus why it found none when that is the case.
|
|
143
|
+
*/
|
|
144
|
+
async function readAgentsLedger(file, read = async (path) => await readFile(path, "utf8")) {
|
|
145
|
+
let raw;
|
|
146
|
+
try {
|
|
147
|
+
raw = await read(file);
|
|
148
|
+
} catch {
|
|
149
|
+
return {
|
|
150
|
+
teams: [],
|
|
151
|
+
problem: "the dsh-agents ledger is not readable at " + file
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
let parsed;
|
|
155
|
+
try {
|
|
156
|
+
parsed = JSON.parse(raw);
|
|
157
|
+
} catch {
|
|
158
|
+
return {
|
|
159
|
+
teams: [],
|
|
160
|
+
problem: "the dsh-agents ledger at " + file + " is not valid JSON"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return {
|
|
164
|
+
teams: [],
|
|
165
|
+
problem: "the dsh-agents ledger at " + file + " has an unknown shape"
|
|
166
|
+
};
|
|
167
|
+
const rawTeams = parsed.teams;
|
|
168
|
+
if (!Array.isArray(rawTeams)) return {
|
|
169
|
+
teams: [],
|
|
170
|
+
problem: "the dsh-agents ledger at " + file + " lists no teams"
|
|
171
|
+
};
|
|
172
|
+
const teams = [];
|
|
173
|
+
for (const entry of rawTeams) {
|
|
174
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
175
|
+
const team = entry;
|
|
176
|
+
if (typeof team.id !== "string" || typeof team.name !== "string") continue;
|
|
177
|
+
const members = [];
|
|
178
|
+
for (const rawMember of Array.isArray(team.members) ? team.members : []) {
|
|
179
|
+
if (typeof rawMember !== "object" || rawMember === null) continue;
|
|
180
|
+
const member = rawMember;
|
|
181
|
+
if (typeof member.id !== "string" || typeof member.name !== "string") continue;
|
|
182
|
+
members.push({
|
|
183
|
+
id: member.id,
|
|
184
|
+
teamId: team.id,
|
|
185
|
+
name: member.name,
|
|
186
|
+
...typeof member.preset === "string" && member.preset !== "" ? { preset: member.preset } : {},
|
|
187
|
+
...typeof member.brief === "string" && member.brief !== "" ? { brief: member.brief } : {},
|
|
188
|
+
...typeof member.model === "string" && member.model !== "" ? { model: member.model } : {},
|
|
189
|
+
...typeof member.reasoningEffort === "string" && member.reasoningEffort !== "" ? { reasoningEffort: member.reasoningEffort } : {}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
teams.push({
|
|
193
|
+
id: team.id,
|
|
194
|
+
name: team.name,
|
|
195
|
+
...typeof team.workspaceId === "string" && team.workspaceId !== "" ? { workspaceId: team.workspaceId } : {},
|
|
196
|
+
members
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return teams.length === 0 ? {
|
|
200
|
+
teams,
|
|
201
|
+
problem: "the dsh-agents ledger at " + file + " defines no team"
|
|
202
|
+
} : { teams };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Project configured bindings onto the member definitions they name.
|
|
206
|
+
*
|
|
207
|
+
* A binding whose member no longer exists is reported and skipped: the hub keeps
|
|
208
|
+
* the agent record it already has, and this Host simply stops claiming for it,
|
|
209
|
+
* which is what an operator unpublishing a member means.
|
|
210
|
+
* @param bindings - the configured member ids.
|
|
211
|
+
* @param view - the members read out of the agents ledger.
|
|
212
|
+
* @returns the resolved agents and one problem line per unusable binding.
|
|
213
|
+
*/
|
|
214
|
+
function resolveBindings(bindings, view) {
|
|
215
|
+
const agents = [];
|
|
216
|
+
const problems = [];
|
|
217
|
+
const seen = /* @__PURE__ */ new Set();
|
|
218
|
+
for (const binding of bindings) {
|
|
219
|
+
const memberId = binding.memberId.trim();
|
|
220
|
+
if (memberId === "" || seen.has(memberId)) continue;
|
|
221
|
+
seen.add(memberId);
|
|
222
|
+
const team = view.teams.find((candidate) => candidate.members.some((member) => member.id === memberId));
|
|
223
|
+
const member = team?.members.find((candidate) => candidate.id === memberId);
|
|
224
|
+
if (team === void 0 || member === void 0) {
|
|
225
|
+
problems.push("the bound member " + memberId + " is not defined in the dsh-agents ledger");
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const name = binding.name !== void 0 && binding.name.trim() !== "" ? binding.name.trim() : member.name;
|
|
229
|
+
agents.push({
|
|
230
|
+
memberId,
|
|
231
|
+
teamId: team.id,
|
|
232
|
+
teamName: team.name,
|
|
233
|
+
name,
|
|
234
|
+
...member.preset === void 0 ? {} : { preset: member.preset },
|
|
235
|
+
...member.model === void 0 ? {} : { model: member.model },
|
|
236
|
+
...member.reasoningEffort === void 0 ? {} : { reasoningEffort: member.reasoningEffort },
|
|
237
|
+
...team.workspaceId === void 0 ? {} : { workspaceId: team.workspaceId }
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
agents,
|
|
242
|
+
problems
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/host/channel.ts
|
|
247
|
+
/**
|
|
248
|
+
* Derive the team channel URL for one hub. The Host Token never travels in the
|
|
249
|
+
* URL; it rides the upgrade's Authorization header (see {@link teamSocketHeaders}).
|
|
250
|
+
* @param hubUrl - configured hub base URL (http/https or ws/wss).
|
|
251
|
+
* @param teamId - team the channel belongs to.
|
|
252
|
+
* @param sinceSeq - last event sequence this Host saw; 0 asks for a full replay.
|
|
253
|
+
* @returns the absolute WebSocket URL.
|
|
254
|
+
*/
|
|
255
|
+
function teamSocketUrl(hubUrl, teamId, sinceSeq) {
|
|
256
|
+
const socketBase = hubUrl.trim().replace(/\/+$/, "").replace(/^http(s?):\/\//i, "ws$1://");
|
|
257
|
+
const path = "/api/v1/teams/" + encodeURIComponent(teamId) + "/ws";
|
|
258
|
+
const query = "?since_seq=" + String(Math.max(0, Math.floor(sinceSeq))) + "&protocol_version=1.0.0";
|
|
259
|
+
return socketBase + path + query;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The upgrade headers the hub authenticates a Host channel with.
|
|
263
|
+
* @param token - Host Token the handshake presents.
|
|
264
|
+
* @returns the Authorization and protocol version headers.
|
|
265
|
+
*/
|
|
266
|
+
function teamSocketHeaders(token) {
|
|
267
|
+
return {
|
|
268
|
+
Authorization: "Bearer " + token,
|
|
269
|
+
"X-Team-Hub-Protocol-Version": "1.0.0"
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/** The default socket factory: the platform WebSocket, with the headers on the upgrade. */
|
|
273
|
+
const platformSocket = (url, handlers, headers) => {
|
|
274
|
+
const socket = new WebSocket(url, { headers });
|
|
275
|
+
let closed = false;
|
|
276
|
+
socket.addEventListener("message", (event) => {
|
|
277
|
+
const data = event.data;
|
|
278
|
+
if (typeof data === "string") handlers.message(data);
|
|
279
|
+
});
|
|
280
|
+
socket.addEventListener("close", (event) => {
|
|
281
|
+
if (closed) return;
|
|
282
|
+
closed = true;
|
|
283
|
+
handlers.closed(event.reason);
|
|
284
|
+
});
|
|
285
|
+
socket.addEventListener("error", () => {});
|
|
286
|
+
return {
|
|
287
|
+
send(data) {
|
|
288
|
+
if (closed) return;
|
|
289
|
+
try {
|
|
290
|
+
socket.send(data);
|
|
291
|
+
} catch {}
|
|
292
|
+
},
|
|
293
|
+
close() {
|
|
294
|
+
if (closed) return;
|
|
295
|
+
closed = true;
|
|
296
|
+
try {
|
|
297
|
+
socket.close();
|
|
298
|
+
} catch {}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/host/credentials.ts
|
|
304
|
+
/**
|
|
305
|
+
* Host-side custody of the team-hub Host Token.
|
|
306
|
+
*
|
|
307
|
+
* The token is a credential, so it lives where the harness keeps credentials —
|
|
308
|
+
* never in this extension's own settings entry, never in a board snapshot, never
|
|
309
|
+
* in a published summary and never in any agent payload. The browser sends a
|
|
310
|
+
* pasted value exactly once; the host half stores it and afterwards only reports
|
|
311
|
+
* whether one is configured, where it came from and whether it can be written.
|
|
312
|
+
*
|
|
313
|
+
* @module dsh-task-board-team/host/credentials
|
|
314
|
+
*/
|
|
315
|
+
/** Credential reference name the Host Token is stored under by default. */
|
|
316
|
+
const DEFAULT_TOKEN_REF = "DSH_TEAM_HUB_TOKEN";
|
|
317
|
+
/**
|
|
318
|
+
* Resolve the optional credential service without declaring a required
|
|
319
|
+
* injection: a deployment that serves none keeps working on the environment.
|
|
320
|
+
* @param ctx - host context.
|
|
321
|
+
* @returns the seam, or undefined.
|
|
322
|
+
*/
|
|
323
|
+
function resolveCredentialFace(ctx) {
|
|
324
|
+
try {
|
|
325
|
+
const get = ctx.get;
|
|
326
|
+
if (typeof get !== "function") return void 0;
|
|
327
|
+
const face = get.call(ctx, "credentials");
|
|
328
|
+
if (face === void 0) return void 0;
|
|
329
|
+
return typeof face.resolve === "function" && typeof face.describe === "function" && typeof face.set === "function" && typeof face.unset === "function" ? face : void 0;
|
|
330
|
+
} catch {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/** The ambient fallback: the configured reference name only. */
|
|
335
|
+
function ambientToken(env, ref) {
|
|
336
|
+
const value = env[ref];
|
|
337
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Report one reference's credential facts for a settings surface.
|
|
341
|
+
* @param ctx - host context.
|
|
342
|
+
* @param ref - the configured reference name.
|
|
343
|
+
* @param env - environment consulted for the ambient fallback.
|
|
344
|
+
* @returns the status the settings section renders.
|
|
345
|
+
*/
|
|
346
|
+
async function describeHostToken(ctx, ref, env = process.env) {
|
|
347
|
+
const face = resolveCredentialFace(ctx);
|
|
348
|
+
let info;
|
|
349
|
+
if (face !== void 0 && isCredentialRefName(ref)) try {
|
|
350
|
+
info = await face.describe(credentialRef(ref));
|
|
351
|
+
} catch {
|
|
352
|
+
info = void 0;
|
|
353
|
+
}
|
|
354
|
+
const ambient = ambientToken(env, ref);
|
|
355
|
+
if (info !== void 0 && info.configured) return {
|
|
356
|
+
configured: true,
|
|
357
|
+
writable: info.writable,
|
|
358
|
+
ref,
|
|
359
|
+
...info.source === void 0 ? {} : { source: info.source }
|
|
360
|
+
};
|
|
361
|
+
if (ambient !== void 0) return {
|
|
362
|
+
configured: true,
|
|
363
|
+
writable: info?.writable ?? false,
|
|
364
|
+
ref,
|
|
365
|
+
source: "environment"
|
|
366
|
+
};
|
|
367
|
+
return {
|
|
368
|
+
configured: false,
|
|
369
|
+
writable: info?.writable ?? face !== void 0,
|
|
370
|
+
ref
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Resolve the effective Host Token for one reference.
|
|
375
|
+
* @param ctx - host context.
|
|
376
|
+
* @param ref - the configured reference name.
|
|
377
|
+
* @param env - environment consulted for the ambient fallback.
|
|
378
|
+
* @returns the token, or undefined when none is configured.
|
|
379
|
+
*/
|
|
380
|
+
async function resolveHostToken(ctx, ref, env = process.env) {
|
|
381
|
+
if (ref.trim() === "") return void 0;
|
|
382
|
+
const face = resolveCredentialFace(ctx);
|
|
383
|
+
if (face !== void 0 && isCredentialRefName(ref)) try {
|
|
384
|
+
const resolved = await face.resolve(credentialRef(ref));
|
|
385
|
+
if (resolved?.value !== void 0 && resolved.value.trim() !== "") return resolved.value.trim();
|
|
386
|
+
} catch {}
|
|
387
|
+
return ambientToken(env, ref);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Store one Host Token in the credential seam.
|
|
391
|
+
* @param ctx - host context.
|
|
392
|
+
* @param ref - the reference to write.
|
|
393
|
+
* @param token - the secret value.
|
|
394
|
+
* @throws when no credential store is served or the store refuses the write.
|
|
395
|
+
*/
|
|
396
|
+
async function setHostToken(ctx, ref, token) {
|
|
397
|
+
const face = resolveCredentialFace(ctx);
|
|
398
|
+
if (face === void 0) throw new Error("this deployment serves no credential store; set the Host Token through the environment instead");
|
|
399
|
+
if (!isCredentialRefName(ref)) throw new Error("\"" + ref + "\" is not a valid credential reference name");
|
|
400
|
+
await face.set(credentialRef(ref), token);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Remove one stored Host Token. Absent values are a no-op.
|
|
404
|
+
* @param ctx - host context.
|
|
405
|
+
* @param ref - the reference to clear.
|
|
406
|
+
* @throws when no credential store is served.
|
|
407
|
+
*/
|
|
408
|
+
async function clearHostToken(ctx, ref) {
|
|
409
|
+
const face = resolveCredentialFace(ctx);
|
|
410
|
+
if (face === void 0) throw new Error("this deployment serves no credential store");
|
|
411
|
+
if (!isCredentialRefName(ref)) throw new Error("\"" + ref + "\" is not a valid credential reference name");
|
|
412
|
+
await face.unset(credentialRef(ref));
|
|
413
|
+
}
|
|
414
|
+
//#endregion
|
|
415
|
+
//#region src/core/identity.ts
|
|
416
|
+
/** Extension id: also the key of this provider's integration payload. */
|
|
417
|
+
const TEAM_EXTENSION_ID = "task-board-team";
|
|
418
|
+
function text(value) {
|
|
419
|
+
return typeof value === "string" && value.trim() !== "" ? value : void 0;
|
|
420
|
+
}
|
|
421
|
+
function count(value) {
|
|
422
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Read one linkage out of an opaque payload.
|
|
426
|
+
* @param payload - the stored payload, or undefined.
|
|
427
|
+
* @returns the linkage, or undefined when the payload carries no issue id.
|
|
428
|
+
*/
|
|
429
|
+
function readTeamIssueLink(payload) {
|
|
430
|
+
if (payload === void 0) return void 0;
|
|
431
|
+
const issueId = text(payload.issueId);
|
|
432
|
+
if (issueId === void 0) return void 0;
|
|
433
|
+
const link = { issueId };
|
|
434
|
+
const issueNumber = count(payload.issueNumber);
|
|
435
|
+
if (issueNumber !== void 0) link.issueNumber = issueNumber;
|
|
436
|
+
const attemptId = text(payload.attemptId);
|
|
437
|
+
if (attemptId !== void 0) link.attemptId = attemptId;
|
|
438
|
+
const leaseToken = text(payload.leaseToken);
|
|
439
|
+
if (leaseToken !== void 0) link.leaseToken = leaseToken;
|
|
440
|
+
const leaseExpiresAt = count(payload.leaseExpiresAt);
|
|
441
|
+
if (leaseExpiresAt !== void 0) link.leaseExpiresAt = leaseExpiresAt;
|
|
442
|
+
const agentId = text(payload.agentId);
|
|
443
|
+
if (agentId !== void 0) link.agentId = agentId;
|
|
444
|
+
const agentMemberId = text(payload.agentMemberId);
|
|
445
|
+
if (agentMemberId !== void 0) link.agentMemberId = agentMemberId;
|
|
446
|
+
const preset = text(payload.preset);
|
|
447
|
+
if (preset !== void 0) link.preset = preset;
|
|
448
|
+
const hubStatus = text(payload.hubStatus);
|
|
449
|
+
if (hubStatus !== void 0) link.hubStatus = hubStatus;
|
|
450
|
+
const assignee = text(payload.assignee);
|
|
451
|
+
if (assignee !== void 0) link.assignee = assignee;
|
|
452
|
+
const reviewer = text(payload.reviewer);
|
|
453
|
+
if (reviewer !== void 0) link.reviewer = reviewer;
|
|
454
|
+
const lastError = text(payload.lastError);
|
|
455
|
+
if (lastError !== void 0) link.lastError = lastError;
|
|
456
|
+
const pendingSettle = readPendingSettle(payload.pendingSettle);
|
|
457
|
+
if (pendingSettle !== void 0) link.pendingSettle = pendingSettle;
|
|
458
|
+
return link;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Read one recorded settlement out of an opaque payload.
|
|
462
|
+
* @param value - the stored member, or undefined.
|
|
463
|
+
* @returns the body to retry, or undefined when it carries no usable outcome.
|
|
464
|
+
*/
|
|
465
|
+
function readPendingSettle(value) {
|
|
466
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
467
|
+
const row = value;
|
|
468
|
+
const outcome = row.outcome === "succeeded" || row.outcome === "failed" ? row.outcome : void 0;
|
|
469
|
+
const summary = text(row.summary);
|
|
470
|
+
if (outcome === void 0 || summary === void 0) return void 0;
|
|
471
|
+
const pending = {
|
|
472
|
+
outcome,
|
|
473
|
+
summary
|
|
474
|
+
};
|
|
475
|
+
const error = text(row.error);
|
|
476
|
+
if (error !== void 0) pending.error = error;
|
|
477
|
+
const attemptId = text(row.execution_attempt_id);
|
|
478
|
+
if (attemptId !== void 0) pending.execution_attempt_id = attemptId;
|
|
479
|
+
const evidence = row.verification_evidence;
|
|
480
|
+
if (typeof evidence === "object" && evidence !== null && !Array.isArray(evidence)) pending.verification_evidence = evidence;
|
|
481
|
+
return pending;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Read the linkage off one card.
|
|
485
|
+
* @param task - the card to read.
|
|
486
|
+
* @returns its linkage, or undefined when it does not mirror an issue.
|
|
487
|
+
*/
|
|
488
|
+
function readTaskIssueLink(task) {
|
|
489
|
+
return readTeamIssueLink(task.integrations?.[TEAM_EXTENSION_ID]);
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Project a linkage back onto a payload, dropping undefined members so a merge
|
|
493
|
+
* never stores a key whose value is absent.
|
|
494
|
+
* @param link - the linkage to store.
|
|
495
|
+
* @returns the payload the board keeps under this extension's id.
|
|
496
|
+
*/
|
|
497
|
+
function toPayload(link) {
|
|
498
|
+
const payload = { issueId: link.issueId };
|
|
499
|
+
if (link.issueNumber !== void 0) payload.issueNumber = link.issueNumber;
|
|
500
|
+
if (link.attemptId !== void 0) payload.attemptId = link.attemptId;
|
|
501
|
+
if (link.leaseToken !== void 0) payload.leaseToken = link.leaseToken;
|
|
502
|
+
if (link.leaseExpiresAt !== void 0) payload.leaseExpiresAt = link.leaseExpiresAt;
|
|
503
|
+
if (link.agentId !== void 0) payload.agentId = link.agentId;
|
|
504
|
+
if (link.agentMemberId !== void 0) payload.agentMemberId = link.agentMemberId;
|
|
505
|
+
if (link.preset !== void 0) payload.preset = link.preset;
|
|
506
|
+
if (link.hubStatus !== void 0) payload.hubStatus = link.hubStatus;
|
|
507
|
+
if (link.assignee !== void 0) payload.assignee = link.assignee;
|
|
508
|
+
if (link.reviewer !== void 0) payload.reviewer = link.reviewer;
|
|
509
|
+
if (link.lastError !== void 0) payload.lastError = link.lastError;
|
|
510
|
+
if (link.pendingSettle !== void 0) payload.pendingSettle = { ...link.pendingSettle };
|
|
511
|
+
return payload;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Find the card that already mirrors one issue.
|
|
515
|
+
* @param linked - every card this extension linked.
|
|
516
|
+
* @param issueId - the issue to look for.
|
|
517
|
+
* @returns the linked task, or undefined when the issue has no card yet.
|
|
518
|
+
*/
|
|
519
|
+
function findLinkedTask(linked, issueId) {
|
|
520
|
+
for (const entry of linked) if (readTeamIssueLink(entry.payload)?.issueId === issueId) return entry.task;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Find the card whose execution owns one session.
|
|
524
|
+
*
|
|
525
|
+
* Questions and mentions arrive addressed by SESSION, and the only session a
|
|
526
|
+
* team question may be routed for is one a mirror card opened — that is what
|
|
527
|
+
* keeps this provider out of every other conversation on the machine.
|
|
528
|
+
* @param linked - every card this extension linked.
|
|
529
|
+
* @param sessionId - the session that asked, or that will receive the message.
|
|
530
|
+
* @returns the linked task, or undefined when this session is not a team card's.
|
|
531
|
+
*/
|
|
532
|
+
function findTaskBySession(linked, sessionId) {
|
|
533
|
+
for (const entry of linked) {
|
|
534
|
+
if (readTeamIssueLink(entry.payload) === void 0) continue;
|
|
535
|
+
if (entry.task.executions.some((execution) => execution.sessionId === sessionId)) return entry.task;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
//#endregion
|
|
539
|
+
//#region src/core/hub-protocol.ts
|
|
540
|
+
/**
|
|
541
|
+
* Wire vocabulary of the team-hub protocol v1, as the Host half reads it.
|
|
542
|
+
*
|
|
543
|
+
* The protocol specification (`team-hub/PROTOCOL.md`) is the single source of
|
|
544
|
+
* truth; this module restates only what a Host uses and the operational
|
|
545
|
+
* constants section 13 fixes, so a drift in either direction is visible in one
|
|
546
|
+
* place. Nothing here interprets collaboration semantics: the hub is
|
|
547
|
+
* authoritative over issues, comments, questions and activity, and the Host is
|
|
548
|
+
* authoritative over execution.
|
|
549
|
+
*
|
|
550
|
+
* @module dsh-task-board-team/core/hub-protocol
|
|
551
|
+
*/
|
|
552
|
+
/** Protocol version sent on every REST call and on the WebSocket handshake. */
|
|
553
|
+
const HUB_PROTOCOL_VERSION = "1.0.0";
|
|
554
|
+
/** Header carrying {@link HUB_PROTOCOL_VERSION}. */
|
|
555
|
+
const HUB_PROTOCOL_HEADER = "X-Team-Hub-Protocol-Version";
|
|
556
|
+
/** Heartbeat cadence the hub expects from an online Host (section 13). */
|
|
557
|
+
const HEARTBEAT_INTERVAL_MS = 15e3;
|
|
558
|
+
/** Lease renewal cadence while an execution is active (section 13). */
|
|
559
|
+
const LEASE_RENEW_INTERVAL_MS = 2e4;
|
|
560
|
+
/** Execution summary character cap, aligned with the board's own limit. */
|
|
561
|
+
const EXECUTION_SUMMARY_LIMIT = 4e3;
|
|
562
|
+
/** Cadence the claim sweep is coalesced to (protocol amendment A4 point 6). */
|
|
563
|
+
const SWEEP_INTERVAL_MS = 5e3;
|
|
564
|
+
/** Backoff the reconnect loop starts at after a dropped WebSocket. */
|
|
565
|
+
const RECONNECT_BASE_MS = 1e3;
|
|
566
|
+
/** Upper bound of the reconnect backoff; a longer outage never waits longer. */
|
|
567
|
+
const RECONNECT_MAX_MS = 3e4;
|
|
568
|
+
/** Client version this Host reports at registration. */
|
|
569
|
+
const HOST_CLIENT_VERSION = "0.4.12";
|
|
570
|
+
/**
|
|
571
|
+
* Read one server frame, tolerating anything the hub may add.
|
|
572
|
+
* @param raw - one decoded frame.
|
|
573
|
+
* @returns the frame, or undefined when it carries no recognizable shape.
|
|
574
|
+
*/
|
|
575
|
+
function readServerFrame(raw) {
|
|
576
|
+
if (typeof raw !== "object" || raw === null) return void 0;
|
|
577
|
+
const frame = raw;
|
|
578
|
+
switch (frame.t) {
|
|
579
|
+
case "pong": return {
|
|
580
|
+
t: "pong",
|
|
581
|
+
ts: typeof frame.ts === "number" ? frame.ts : 0
|
|
582
|
+
};
|
|
583
|
+
case "event": {
|
|
584
|
+
if (typeof frame.seq !== "number" || typeof frame.event !== "string") return void 0;
|
|
585
|
+
const payload = typeof frame.payload === "object" && frame.payload !== null ? frame.payload : {};
|
|
586
|
+
return {
|
|
587
|
+
t: "event",
|
|
588
|
+
seq: frame.seq,
|
|
589
|
+
event: frame.event,
|
|
590
|
+
payload,
|
|
591
|
+
ts: typeof frame.ts === "number" ? frame.ts : 0
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
case "resync_required": return {
|
|
595
|
+
t: "resync_required",
|
|
596
|
+
reason: typeof frame.reason === "string" ? frame.reason : "",
|
|
597
|
+
latest_seq: typeof frame.latest_seq === "number" ? frame.latest_seq : 0
|
|
598
|
+
};
|
|
599
|
+
default: return;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/** Longest issue body carried into a card description. */
|
|
603
|
+
const MIRROR_DESCRIPTION_LIMIT = 32e3;
|
|
604
|
+
function clip$1(text, limit) {
|
|
605
|
+
const trimmed = text.trim();
|
|
606
|
+
return trimmed.length <= limit ? trimmed : trimmed.slice(0, limit);
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Build the card a claimed issue becomes.
|
|
610
|
+
* @param issue - the claimed issue.
|
|
611
|
+
* @param agent - the member it was claimed for.
|
|
612
|
+
* @returns the draft the board creates the card from.
|
|
613
|
+
*/
|
|
614
|
+
function buildMirrorDraft(issue, agent) {
|
|
615
|
+
const title = clip$1(`#${String(issue.issue_number)} ${issue.title}`, 256);
|
|
616
|
+
const body = clip$1(issue.description, MIRROR_DESCRIPTION_LIMIT);
|
|
617
|
+
const lines = [
|
|
618
|
+
`Team issue ${issue.id} (issue #${String(issue.issue_number)}) claimed for agent ${agent.name}.`,
|
|
619
|
+
"",
|
|
620
|
+
issue.title.trim()
|
|
621
|
+
];
|
|
622
|
+
if (body !== "") lines.push("", body);
|
|
623
|
+
lines.push("", "Work on this task in this workspace. Report progress and ask for decisions with the team_comment tool;", "a question asked through ask_user_question is routed to the team and answered by its reviewer.", "Answering a rework request in the same session keeps the review thread on the hub intact.");
|
|
624
|
+
return {
|
|
625
|
+
title,
|
|
626
|
+
description: body === "" ? `Team issue #${String(issue.issue_number)} mirror card.` : body,
|
|
627
|
+
prompt: lines.join("\n"),
|
|
628
|
+
...agent.workspaceId === void 0 ? {} : { workspaceId: agent.workspaceId },
|
|
629
|
+
...agent.preset === void 0 ? {} : { mode: agent.preset },
|
|
630
|
+
...agent.model === void 0 ? {} : { model: agent.model },
|
|
631
|
+
...agent.reasoningEffort === void 0 ? {} : { reasoningEffort: agent.reasoningEffort }
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/core/settlement.ts
|
|
636
|
+
/**
|
|
637
|
+
* The settlement one finished execution reports to the hub.
|
|
638
|
+
*
|
|
639
|
+
* The board owns the execution verdict and the hub owns the collaboration
|
|
640
|
+
* status, so this module is the whole mapping between them: a passed automated
|
|
641
|
+
* acceptance becomes `succeeded` (the hub moves the issue to `in_review`), and
|
|
642
|
+
* every other outcome — including a cancelled run — becomes `failed`, because a
|
|
643
|
+
* card the Host stopped never earned a human review. The summary is clipped to
|
|
644
|
+
* the character cap the protocol fixes (section 13), which mirrors the board's
|
|
645
|
+
* own execution-summary limit.
|
|
646
|
+
*
|
|
647
|
+
* @module dsh-task-board-team/core/settlement
|
|
648
|
+
*/
|
|
649
|
+
/**
|
|
650
|
+
* Map one execution outcome onto the hub's settlement vocabulary.
|
|
651
|
+
* @param outcome - the board's own verdict.
|
|
652
|
+
* @returns `succeeded` only for a passed run.
|
|
653
|
+
*/
|
|
654
|
+
function settleOutcome(outcome) {
|
|
655
|
+
return outcome === "succeeded" ? "succeeded" : "failed";
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Clip one summary to the protocol's character cap.
|
|
659
|
+
* @param text - the text to clip.
|
|
660
|
+
* @returns text of at most {@link EXECUTION_SUMMARY_LIMIT} characters.
|
|
661
|
+
*/
|
|
662
|
+
function clipSummary(text) {
|
|
663
|
+
if (text.length <= 4e3) return text;
|
|
664
|
+
return text.slice(0, EXECUTION_SUMMARY_LIMIT - 3) + "...";
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Build the settlement body for one settled execution.
|
|
668
|
+
*
|
|
669
|
+
* The summary names the run and what it produced, so a reviewer reading the hub
|
|
670
|
+
* sees which attempt this was without opening the Host: the card title, the
|
|
671
|
+
* attempt id and, on a failure, the recorded reason.
|
|
672
|
+
* @param input - the settlement event and the card it belongs to.
|
|
673
|
+
* @returns the body to post.
|
|
674
|
+
*/
|
|
675
|
+
function buildSettleRequest(input) {
|
|
676
|
+
const outcome = settleOutcome(input.outcome);
|
|
677
|
+
const lines = [`${input.task.title} — ${outcome === "succeeded" ? "automated acceptance passed" : "automated acceptance did not pass"}`, `attempt: ${input.executionId}`];
|
|
678
|
+
if (input.error !== void 0 && input.error.trim() !== "") lines.push(`reason: ${input.error.trim()}`);
|
|
679
|
+
const body = {
|
|
680
|
+
outcome,
|
|
681
|
+
summary: clipSummary(lines.join("\n"))
|
|
682
|
+
};
|
|
683
|
+
if (input.error !== void 0 && input.error.trim() !== "") body.error = clipSummary(input.error.trim());
|
|
684
|
+
if (input.attemptId !== void 0) body.execution_attempt_id = input.attemptId;
|
|
685
|
+
body.verification_evidence = {
|
|
686
|
+
execution_id: input.executionId,
|
|
687
|
+
outcome
|
|
688
|
+
};
|
|
689
|
+
return body;
|
|
690
|
+
}
|
|
691
|
+
//#endregion
|
|
692
|
+
//#region src/host/hub-client.ts
|
|
693
|
+
/**
|
|
694
|
+
* The Host's REST client for one team-hub.
|
|
695
|
+
*
|
|
696
|
+
* Every call carries the Host Token and the protocol version, resolves its base
|
|
697
|
+
* URL and team at call time (so a settings edit reaches the next call without a
|
|
698
|
+
* remount), and turns the hub's unified error envelope into one diagnosable
|
|
699
|
+
* error carrying its code. The transport is injected: a deployment uses the
|
|
700
|
+
* global fetch, and the test suite drives a fake hub through the same seam.
|
|
701
|
+
*
|
|
702
|
+
* @module dsh-task-board-team/host/hub-client
|
|
703
|
+
*/
|
|
704
|
+
/** One refusal the hub answered with. */
|
|
705
|
+
var HubError = class extends Error {
|
|
706
|
+
/** Machine-readable code from the unified error envelope. */
|
|
707
|
+
code;
|
|
708
|
+
/** HTTP status the hub answered with. */
|
|
709
|
+
status;
|
|
710
|
+
constructor(code, message, status) {
|
|
711
|
+
super(message);
|
|
712
|
+
this.name = "HubError";
|
|
713
|
+
this.code = code;
|
|
714
|
+
this.status = status;
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
/** Read the code out of the hub's unified error envelope. */
|
|
718
|
+
function readErrorCode(body, status) {
|
|
719
|
+
if (typeof body === "object" && body !== null) {
|
|
720
|
+
const error = body.error;
|
|
721
|
+
if (typeof error === "object" && error !== null) {
|
|
722
|
+
const envelope = error;
|
|
723
|
+
return {
|
|
724
|
+
code: typeof envelope.code === "string" ? envelope.code : "HTTP_" + String(status),
|
|
725
|
+
message: typeof envelope.message === "string" ? envelope.message : "the hub refused the request"
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return {
|
|
730
|
+
code: "HTTP_" + String(status),
|
|
731
|
+
message: "the hub refused the request"
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
/** The REST surface one Host speaks to one team. */
|
|
735
|
+
var HubClient = class {
|
|
736
|
+
#options;
|
|
737
|
+
constructor(options) {
|
|
738
|
+
this.#options = options;
|
|
739
|
+
}
|
|
740
|
+
/** Whether this deployment can reach a hub at all (a URL and a token exist). */
|
|
741
|
+
async configured() {
|
|
742
|
+
return this.#base().ok;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Register this Host and synchronize its agent definitions.
|
|
746
|
+
*
|
|
747
|
+
* The body is snake_case end to end (protocol amendment A4 point 1): the hub
|
|
748
|
+
* stores exactly these keys, so a camelCase member is a field it never sees.
|
|
749
|
+
* @param body - hostname, client version and the local agent definitions.
|
|
750
|
+
* @returns the registered host id, status and the agent records the hub now holds.
|
|
751
|
+
*/
|
|
752
|
+
async register(body) {
|
|
753
|
+
return await this.#call("POST", "/hosts/register", body);
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Send one heartbeat.
|
|
757
|
+
* @param hostId - the registered host id.
|
|
758
|
+
* @returns the hub's server time.
|
|
759
|
+
*/
|
|
760
|
+
async heartbeat(hostId) {
|
|
761
|
+
return await this.#call("POST", `/hosts/${encodeURIComponent(hostId)}/heartbeat`);
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* List the issues this Host may claim.
|
|
765
|
+
* @param hostId - the registered host id.
|
|
766
|
+
* @returns the claimable issues.
|
|
767
|
+
*/
|
|
768
|
+
async listClaimableIssues(hostId) {
|
|
769
|
+
const query = "?assignee_host_id=" + encodeURIComponent(hostId) + "&status=assigned%2Cwaiting_host";
|
|
770
|
+
const body = await this.#call("GET", "/issues" + query);
|
|
771
|
+
return Array.isArray(body.issues) ? body.issues : [];
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Claim one issue, or re-establish the attempt already running locally.
|
|
775
|
+
* @param issueId - the issue to claim.
|
|
776
|
+
* @param credentials - the previous attempt credentials, when this is a reconnect.
|
|
777
|
+
* @returns the lease.
|
|
778
|
+
*/
|
|
779
|
+
async claim(issueId, credentials = {}) {
|
|
780
|
+
return await this.#call("POST", `/issues/${encodeURIComponent(issueId)}/claim`, credentials);
|
|
781
|
+
}
|
|
782
|
+
/**
|
|
783
|
+
* Extend one lease.
|
|
784
|
+
* @param issueId - the claimed issue.
|
|
785
|
+
* @param leaseToken - the lease credential.
|
|
786
|
+
* @returns the new expiry.
|
|
787
|
+
*/
|
|
788
|
+
async renewLease(issueId, leaseToken) {
|
|
789
|
+
return await this.#call("POST", `/issues/${encodeURIComponent(issueId)}/renew-lease`, { lease_token: leaseToken });
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Write one settlement back.
|
|
793
|
+
* @param issueId - the settled issue.
|
|
794
|
+
* @param body - outcome, summary and error.
|
|
795
|
+
* @returns the collaboration status the hub moved to.
|
|
796
|
+
*/
|
|
797
|
+
async settle(issueId, body) {
|
|
798
|
+
return await this.#call("POST", `/issues/${encodeURIComponent(issueId)}/settle`, body);
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Report one decision question.
|
|
802
|
+
* @param issueId - the issue it belongs to.
|
|
803
|
+
* @param body - the question text, options and multi-select flag.
|
|
804
|
+
* @returns the stored question.
|
|
805
|
+
*/
|
|
806
|
+
async createQuestion(issueId, body) {
|
|
807
|
+
const response = await this.#call("POST", `/issues/${encodeURIComponent(issueId)}/questions`, body);
|
|
808
|
+
if (response.question === void 0) throw new HubError("INVALID_RESPONSE", "the hub returned no question", 200);
|
|
809
|
+
return response.question;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Withdraw one pending question after the local asker gave up.
|
|
813
|
+
* @param questionId - the question to withdraw.
|
|
814
|
+
*/
|
|
815
|
+
async withdrawQuestion(questionId) {
|
|
816
|
+
await this.#call("POST", "/questions/" + encodeURIComponent(questionId) + "/withdraw");
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Post one agent comment.
|
|
820
|
+
* @param issueId - the issue to comment on.
|
|
821
|
+
* @param body - the content and the agent identity that wrote it.
|
|
822
|
+
* @returns the stored comment.
|
|
823
|
+
*/
|
|
824
|
+
async createComment(issueId, body) {
|
|
825
|
+
const response = await this.#call("POST", `/issues/${encodeURIComponent(issueId)}/comments`, body);
|
|
826
|
+
if (response.comment === void 0) throw new HubError("INVALID_RESPONSE", "the hub returned no comment", 200);
|
|
827
|
+
return response.comment;
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Read one issue's comment thread.
|
|
831
|
+
* @param issueId - the issue to read.
|
|
832
|
+
* @returns the comments, oldest first.
|
|
833
|
+
*/
|
|
834
|
+
async comments(issueId) {
|
|
835
|
+
const body = await this.#call("GET", `/issues/${encodeURIComponent(issueId)}/comments`);
|
|
836
|
+
return Array.isArray(body.comments) ? body.comments : [];
|
|
837
|
+
}
|
|
838
|
+
/** The base URL and team, or the reason there is none. */
|
|
839
|
+
#base() {
|
|
840
|
+
const raw = this.#options.hubUrl().trim();
|
|
841
|
+
const team = this.#options.teamId().trim();
|
|
842
|
+
if (raw === "" || team === "") return { ok: false };
|
|
843
|
+
return {
|
|
844
|
+
ok: true,
|
|
845
|
+
url: raw.replace(/\/+$/, "") + "/api/v1/teams/" + encodeURIComponent(team)
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
async #call(method, path, body) {
|
|
849
|
+
const base = this.#base();
|
|
850
|
+
if (!base.ok) throw new HubError("NOT_CONFIGURED", "the team hub URL and team id are not configured", 0);
|
|
851
|
+
const token = await this.#options.token();
|
|
852
|
+
if (token === void 0 || token === "") throw new HubError("UNAUTHORIZED", "no Host Token is configured", 401);
|
|
853
|
+
const headers = {
|
|
854
|
+
authorization: "Bearer " + token,
|
|
855
|
+
[HUB_PROTOCOL_HEADER]: HUB_PROTOCOL_VERSION,
|
|
856
|
+
accept: "application/json",
|
|
857
|
+
"content-type": "application/json"
|
|
858
|
+
};
|
|
859
|
+
const response = await this.#fetch()(base.url + path, {
|
|
860
|
+
method,
|
|
861
|
+
headers,
|
|
862
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
863
|
+
});
|
|
864
|
+
const text = await response.text();
|
|
865
|
+
let parsed;
|
|
866
|
+
try {
|
|
867
|
+
parsed = text === "" ? {} : JSON.parse(text);
|
|
868
|
+
} catch {
|
|
869
|
+
parsed = void 0;
|
|
870
|
+
}
|
|
871
|
+
if (!response.ok) {
|
|
872
|
+
const { code, message } = readErrorCode(parsed, response.status);
|
|
873
|
+
throw new HubError(code, message, response.status);
|
|
874
|
+
}
|
|
875
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new HubError("INVALID_RESPONSE", "the hub returned a body this client cannot read", response.status);
|
|
876
|
+
return parsed;
|
|
877
|
+
}
|
|
878
|
+
#fetch() {
|
|
879
|
+
const injected = this.#options.fetch;
|
|
880
|
+
if (injected !== void 0) return injected;
|
|
881
|
+
return async (url, init) => {
|
|
882
|
+
const response = await fetch(url, init);
|
|
883
|
+
return {
|
|
884
|
+
ok: response.ok,
|
|
885
|
+
status: response.status,
|
|
886
|
+
text: async () => await response.text()
|
|
887
|
+
};
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
//#endregion
|
|
892
|
+
//#region src/core/question-report.ts
|
|
893
|
+
/**
|
|
894
|
+
* Build the hub question one Host question request reports.
|
|
895
|
+
* @param item - the first question of the batch.
|
|
896
|
+
* @returns the report body.
|
|
897
|
+
*/
|
|
898
|
+
function buildQuestionReport(item) {
|
|
899
|
+
const text = item.header === void 0 || item.header.trim() === "" ? item.question : `${item.header.trim()}: ${item.question}`;
|
|
900
|
+
const options = (item.options ?? []).filter((option) => option.label.trim() !== "").map((option) => ({
|
|
901
|
+
label: option.label,
|
|
902
|
+
...option.description === void 0 || option.description.trim() === "" ? {} : { description: option.description }
|
|
903
|
+
}));
|
|
904
|
+
return {
|
|
905
|
+
question_text: text,
|
|
906
|
+
...options.length === 0 ? {} : { options },
|
|
907
|
+
...item.multiSelect === true ? { multi_select: true } : {}
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* Map one team answer back onto the question the Host asked.
|
|
912
|
+
* @param item - the reported question item (whose id the answer addresses).
|
|
913
|
+
* @param answer - the hub's answer payload: the selected labels and optional custom text.
|
|
914
|
+
* @returns the answer batch the Host settles its ask with.
|
|
915
|
+
*/
|
|
916
|
+
function answerToHost(item, answer) {
|
|
917
|
+
const selected = [...answer.selected];
|
|
918
|
+
const custom = answer.custom !== void 0 && answer.custom.trim() !== "" ? answer.custom : void 0;
|
|
919
|
+
return { answers: [{
|
|
920
|
+
id: item.id,
|
|
921
|
+
selected,
|
|
922
|
+
...custom === void 0 ? {} : { custom }
|
|
923
|
+
}] };
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* The answer batch that ends a question nobody answered.
|
|
927
|
+
*
|
|
928
|
+
* The protocol resolves an expired question as unanswered so the agent can
|
|
929
|
+
* proceed on its own assumptions or pause; the Host's answer vocabulary has no
|
|
930
|
+
* "expired" outcome, so the ask settles with an empty batch — a defined answer
|
|
931
|
+
* that selected nothing — instead of a fabricated choice a human never made.
|
|
932
|
+
* @returns the empty answer batch.
|
|
933
|
+
*/
|
|
934
|
+
function unansweredAnswer() {
|
|
935
|
+
return { answers: [] };
|
|
936
|
+
}
|
|
937
|
+
/** Whether a question request still fits the hub's question schema. */
|
|
938
|
+
function isReportable(item) {
|
|
939
|
+
return item.question.trim() !== "" && item.question.length <= 4e3;
|
|
940
|
+
}
|
|
941
|
+
//#endregion
|
|
942
|
+
//#region src/host/questions.ts
|
|
943
|
+
/**
|
|
944
|
+
* The provider's claims on the Host's process-wide question waterfall.
|
|
945
|
+
*/
|
|
946
|
+
var QuestionRouter = class {
|
|
947
|
+
#options;
|
|
948
|
+
#pending = /* @__PURE__ */ new Map();
|
|
949
|
+
constructor(options) {
|
|
950
|
+
this.#options = options;
|
|
951
|
+
}
|
|
952
|
+
/** Every question this Host is still waiting on, oldest claim first. */
|
|
953
|
+
pending() {
|
|
954
|
+
return [...this.#pending.values()].map((entry) => ({
|
|
955
|
+
id: entry.id,
|
|
956
|
+
taskId: entry.taskId,
|
|
957
|
+
issueId: entry.issueId,
|
|
958
|
+
question: entry.question,
|
|
959
|
+
...entry.hubQuestionId === void 0 ? {} : { hubQuestionId: entry.hubQuestionId },
|
|
960
|
+
askedAt: entry.askedAt
|
|
961
|
+
}));
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* The waterfall body: claim one question that belongs to a team card, and
|
|
965
|
+
* delegate every other request to the next answerer.
|
|
966
|
+
* @param request - the pending question batch.
|
|
967
|
+
* @param next - the rest of the answerer chain.
|
|
968
|
+
* @returns the team's answer, or whatever the next answerer says.
|
|
969
|
+
*/
|
|
970
|
+
async request(request, next) {
|
|
971
|
+
const sessionId = request.agent?.id;
|
|
972
|
+
if (sessionId === void 0) return await next();
|
|
973
|
+
const task = findTaskBySession(this.#options.linked(), sessionId);
|
|
974
|
+
if (task === void 0) return await next();
|
|
975
|
+
const link = readTaskIssueLink(task);
|
|
976
|
+
if (link === void 0) return await next();
|
|
977
|
+
const item = request.questions[0];
|
|
978
|
+
if (item === void 0 || !isReportable(item)) return await next();
|
|
979
|
+
const id = this.#options.uuid();
|
|
980
|
+
let settle = () => void 0;
|
|
981
|
+
const answer = new Promise((resolve) => {
|
|
982
|
+
settle = resolve;
|
|
983
|
+
});
|
|
984
|
+
let questionId;
|
|
985
|
+
try {
|
|
986
|
+
questionId = (await this.#options.hub.createQuestion(link.issueId, buildQuestionReport(item))).id;
|
|
987
|
+
} catch (error) {
|
|
988
|
+
this.#options.log?.("question report failed", error);
|
|
989
|
+
return await next();
|
|
990
|
+
}
|
|
991
|
+
const entry = {
|
|
992
|
+
id,
|
|
993
|
+
taskId: task.id,
|
|
994
|
+
issueId: link.issueId,
|
|
995
|
+
question: item.question,
|
|
996
|
+
hubQuestionId: questionId,
|
|
997
|
+
askedAt: this.#options.now(),
|
|
998
|
+
item,
|
|
999
|
+
settle
|
|
1000
|
+
};
|
|
1001
|
+
this.#pending.set(id, entry);
|
|
1002
|
+
this.#options.onChange?.();
|
|
1003
|
+
const waitMs = this.#options.fallbackWaitMs?.();
|
|
1004
|
+
const cancelWait = waitMs === void 0 || waitMs <= 0 ? () => {} : this.#options.timers.timeout(() => {
|
|
1005
|
+
this.#expireLocally(id);
|
|
1006
|
+
}, waitMs);
|
|
1007
|
+
const abort = () => {
|
|
1008
|
+
this.#withdraw(id);
|
|
1009
|
+
};
|
|
1010
|
+
request.signal?.addEventListener("abort", abort, { once: true });
|
|
1011
|
+
const settled = await answer;
|
|
1012
|
+
cancelWait();
|
|
1013
|
+
request.signal?.removeEventListener("abort", abort);
|
|
1014
|
+
return settled ?? await next();
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Settle one question with a team answer.
|
|
1018
|
+
* @param payload - the hub's `question.answered` payload.
|
|
1019
|
+
* @returns whether this Host was waiting on that question.
|
|
1020
|
+
*/
|
|
1021
|
+
answered(payload) {
|
|
1022
|
+
const questionId = typeof payload.question_id === "string" ? payload.question_id : void 0;
|
|
1023
|
+
if (questionId === void 0) return false;
|
|
1024
|
+
const entry = this.#byHubId(questionId);
|
|
1025
|
+
if (entry === void 0) return false;
|
|
1026
|
+
const answer = readAnswerPayload(payload.answer);
|
|
1027
|
+
this.#settle(entry.id, answerToHost(entry.item, answer));
|
|
1028
|
+
return true;
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* End one question as unanswered, the way the hub's expiry does.
|
|
1032
|
+
* @param payload - the hub's `question.expired` payload.
|
|
1033
|
+
* @returns whether this Host was waiting on that question.
|
|
1034
|
+
*/
|
|
1035
|
+
expired(payload) {
|
|
1036
|
+
const questionId = typeof payload.question_id === "string" ? payload.question_id : void 0;
|
|
1037
|
+
if (questionId === void 0) return false;
|
|
1038
|
+
const entry = this.#byHubId(questionId);
|
|
1039
|
+
if (entry === void 0) return false;
|
|
1040
|
+
this.#settle(entry.id, unansweredAnswer());
|
|
1041
|
+
return true;
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Drop one question the hub reported withdrawn.
|
|
1045
|
+
* @param payload - the hub's `question.withdrawn` payload.
|
|
1046
|
+
* @returns whether this Host was waiting on that question.
|
|
1047
|
+
*/
|
|
1048
|
+
withdrawn(payload) {
|
|
1049
|
+
const questionId = typeof payload.question_id === "string" ? payload.question_id : void 0;
|
|
1050
|
+
if (questionId === void 0) return false;
|
|
1051
|
+
const entry = this.#byHubId(questionId);
|
|
1052
|
+
if (entry === void 0) return false;
|
|
1053
|
+
this.#settle(entry.id, unansweredAnswer());
|
|
1054
|
+
return true;
|
|
1055
|
+
}
|
|
1056
|
+
/** Drop every claim, delegating each to the next answerer. */
|
|
1057
|
+
dispose() {
|
|
1058
|
+
for (const id of [...this.#pending.keys()]) this.#settle(id, void 0);
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* The local ask was cancelled: tell the hub, then let the next answerer take it.
|
|
1062
|
+
*/
|
|
1063
|
+
async #withdraw(id) {
|
|
1064
|
+
const entry = this.#pending.get(id);
|
|
1065
|
+
if (entry === void 0) return;
|
|
1066
|
+
if (entry.hubQuestionId !== void 0) try {
|
|
1067
|
+
await this.#options.hub.withdrawQuestion(entry.hubQuestionId);
|
|
1068
|
+
} catch (error) {
|
|
1069
|
+
if (!(error instanceof HubError) || error.code !== "QUESTION_NOT_PENDING") this.#options.log?.("question withdraw failed", error);
|
|
1070
|
+
}
|
|
1071
|
+
this.#settle(id, void 0);
|
|
1072
|
+
}
|
|
1073
|
+
/** The local wait window ended: withdraw on the hub and return to the machine. */
|
|
1074
|
+
async #expireLocally(id) {
|
|
1075
|
+
await this.#withdraw(id);
|
|
1076
|
+
}
|
|
1077
|
+
#byHubId(questionId) {
|
|
1078
|
+
for (const entry of this.#pending.values()) if (entry.hubQuestionId === questionId) return entry;
|
|
1079
|
+
}
|
|
1080
|
+
#settle(id, answer) {
|
|
1081
|
+
const entry = this.#pending.get(id);
|
|
1082
|
+
if (entry === void 0) return false;
|
|
1083
|
+
this.#pending.delete(id);
|
|
1084
|
+
entry.settle(answer);
|
|
1085
|
+
this.#options.onChange?.();
|
|
1086
|
+
return true;
|
|
1087
|
+
}
|
|
1088
|
+
};
|
|
1089
|
+
/**
|
|
1090
|
+
* Read the hub's answer member into the shape the mapping needs.
|
|
1091
|
+
*
|
|
1092
|
+
* Protocol amendment A2 fixes this shape as `{ selected: string[], custom?: string }`
|
|
1093
|
+
* (or null while a question is still unanswered), so a null answer reads as an
|
|
1094
|
+
* empty selection instead of being mistaken for a custom-only reply.
|
|
1095
|
+
*/
|
|
1096
|
+
function readAnswerPayload(raw) {
|
|
1097
|
+
if (typeof raw !== "object" || raw === null) return { selected: [] };
|
|
1098
|
+
const answer = raw;
|
|
1099
|
+
const selected = Array.isArray(answer.selected) ? answer.selected.filter((value) => typeof value === "string") : [];
|
|
1100
|
+
const custom = typeof answer.custom === "string" ? answer.custom : void 0;
|
|
1101
|
+
return {
|
|
1102
|
+
selected,
|
|
1103
|
+
...custom === void 0 ? {} : { custom }
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
//#endregion
|
|
1107
|
+
//#region src/host/service.ts
|
|
1108
|
+
/**
|
|
1109
|
+
* The provider's hub synchronization: registration, heartbeat, claiming, leases
|
|
1110
|
+
* and write-back.
|
|
1111
|
+
*
|
|
1112
|
+
* The division of authority runs through every method here. The hub decides
|
|
1113
|
+
* which issues exist, who owns them and what collaboration status they carry;
|
|
1114
|
+
* this Host decides whether it may take one, how it runs it locally and what the
|
|
1115
|
+
* run produced. A claim is the only moment the two meet: the hub hands over a
|
|
1116
|
+
* lease and an attempt id, this Host materializes the issue as a board card and
|
|
1117
|
+
* reports back when that card's execution settles.
|
|
1118
|
+
*
|
|
1119
|
+
* Reconnection is the interesting path, so it is written as one: the attempt
|
|
1120
|
+
* credentials live on the card's own payload, and a re-claim presents them. A
|
|
1121
|
+
* hub that recognizes them re-establishes the lease and creates nothing new, and
|
|
1122
|
+
* this Host in turn never creates a second card for an issue it already mirrors
|
|
1123
|
+
* and never starts a second execution for an attempt it already ran.
|
|
1124
|
+
*
|
|
1125
|
+
* @module dsh-task-board-team/host/service
|
|
1126
|
+
*/
|
|
1127
|
+
/** The problem line published when the board serves no draft preset. */
|
|
1128
|
+
const DRAFT_PIN_PROBLEM = "this board revision cannot pin an agent preset, model or reasoning effort on a new card, so a claimed issue becomes a mirror card that is left for a human to start";
|
|
1129
|
+
/** The problem line published while this Host has no workspace to run a card in. */
|
|
1130
|
+
const WORKSPACE_PROBLEM = "no run workspace is bound to this Host, so no team issue is claimed: a mirror card without a workspace cannot be started, and the hub would keep its issue running";
|
|
1131
|
+
/** Longest comment or activity text one summary carries. */
|
|
1132
|
+
const VIEW_TEXT_LIMIT = 2e3;
|
|
1133
|
+
function clip(text) {
|
|
1134
|
+
const trimmed = text.trim();
|
|
1135
|
+
return trimmed.length <= VIEW_TEXT_LIMIT ? trimmed : trimmed.slice(0, VIEW_TEXT_LIMIT - 3) + "...";
|
|
1136
|
+
}
|
|
1137
|
+
/** The synchronization service of one mounted extension. */
|
|
1138
|
+
var TeamSyncService = class {
|
|
1139
|
+
#options;
|
|
1140
|
+
#questions;
|
|
1141
|
+
#leases = /* @__PURE__ */ new Map();
|
|
1142
|
+
#cards = /* @__PURE__ */ new Map();
|
|
1143
|
+
/** Hub agent records by their hub id, learned at registration. */
|
|
1144
|
+
#hubAgents = /* @__PURE__ */ new Map();
|
|
1145
|
+
#agents = [];
|
|
1146
|
+
#problems = [];
|
|
1147
|
+
#hostId;
|
|
1148
|
+
#seq = 0;
|
|
1149
|
+
#socket;
|
|
1150
|
+
#stopHeartbeat;
|
|
1151
|
+
#stopReconnect;
|
|
1152
|
+
#started = false;
|
|
1153
|
+
#connection = "idle";
|
|
1154
|
+
#lastError;
|
|
1155
|
+
#lastHeartbeatAt;
|
|
1156
|
+
#reconnects = 0;
|
|
1157
|
+
#reconnectAttempt = 0;
|
|
1158
|
+
#starting;
|
|
1159
|
+
#presetPin = "unknown";
|
|
1160
|
+
/** Instant of the last sweep, so a burst of events coalesces into one pass. */
|
|
1161
|
+
#lastSweepAt;
|
|
1162
|
+
/** A sweep scheduled for the next cadence boundary, while one is pending. */
|
|
1163
|
+
#pendingSweep;
|
|
1164
|
+
/** Issues with a claim in flight: one claim per issue at a time, whoever asks. */
|
|
1165
|
+
#claiming = /* @__PURE__ */ new Set();
|
|
1166
|
+
/** Agent names the hub reported another Host already owns, by name. */
|
|
1167
|
+
#agentConflicts = /* @__PURE__ */ new Map();
|
|
1168
|
+
/** Issues whose owed settlement the current pass already retried. */
|
|
1169
|
+
#settledThisPass = /* @__PURE__ */ new Set();
|
|
1170
|
+
constructor(options) {
|
|
1171
|
+
this.#options = options;
|
|
1172
|
+
this.#questions = new QuestionRouter({
|
|
1173
|
+
hub: options.hub,
|
|
1174
|
+
host: options.host,
|
|
1175
|
+
linked: () => this.#linked(),
|
|
1176
|
+
timers: options.timers,
|
|
1177
|
+
now: options.now,
|
|
1178
|
+
uuid: options.uuid,
|
|
1179
|
+
onChange: () => {
|
|
1180
|
+
this.#publish();
|
|
1181
|
+
},
|
|
1182
|
+
fallbackWaitMs: () => {
|
|
1183
|
+
const minutes = options.settings().questionWaitMinutes ?? 0;
|
|
1184
|
+
return minutes > 0 ? minutes * 6e4 : void 0;
|
|
1185
|
+
},
|
|
1186
|
+
...options.log === void 0 ? {} : { log: options.log }
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
/** Republish the summary, e.g. after a configuration write changed what it shows. */
|
|
1190
|
+
publishNow() {
|
|
1191
|
+
this.#publish();
|
|
1192
|
+
}
|
|
1193
|
+
/** The question router this service routes the Host waterfall through. */
|
|
1194
|
+
get questions() {
|
|
1195
|
+
return this.#questions;
|
|
1196
|
+
}
|
|
1197
|
+
/** Start registration, heartbeat, the channel and the first claim sweep. */
|
|
1198
|
+
async start() {
|
|
1199
|
+
if (this.#started) return;
|
|
1200
|
+
this.#started = true;
|
|
1201
|
+
this.#starting = this.#bootstrap();
|
|
1202
|
+
await this.#starting;
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Resolve once this mount finished registering, or immediately when it has not
|
|
1206
|
+
* started at all.
|
|
1207
|
+
*
|
|
1208
|
+
* A sweep requested before registration completes must wait for the host id
|
|
1209
|
+
* instead of silently claiming nothing, which is what a caller racing the
|
|
1210
|
+
* bootstrap would otherwise get.
|
|
1211
|
+
* @returns a promise resolved when the bootstrap settles.
|
|
1212
|
+
*/
|
|
1213
|
+
async whenStarted() {
|
|
1214
|
+
await this.#starting;
|
|
1215
|
+
}
|
|
1216
|
+
/** Release every timer, the socket and every pending question claim. */
|
|
1217
|
+
stop() {
|
|
1218
|
+
this.#started = false;
|
|
1219
|
+
this.#stopHeartbeat?.();
|
|
1220
|
+
this.#stopHeartbeat = void 0;
|
|
1221
|
+
this.#stopReconnect?.();
|
|
1222
|
+
this.#stopReconnect = void 0;
|
|
1223
|
+
this.#pendingSweep?.();
|
|
1224
|
+
this.#pendingSweep = void 0;
|
|
1225
|
+
this.#socket?.close();
|
|
1226
|
+
this.#socket = void 0;
|
|
1227
|
+
for (const cancel of this.#leases.values()) cancel();
|
|
1228
|
+
this.#leases.clear();
|
|
1229
|
+
this.#questions.dispose();
|
|
1230
|
+
this.#connection = "idle";
|
|
1231
|
+
}
|
|
1232
|
+
/** The summary the browser renders, built fresh from the live state. */
|
|
1233
|
+
summary() {
|
|
1234
|
+
const cards = {};
|
|
1235
|
+
for (const [taskId, view] of this.#cards) cards[taskId] = { ...view };
|
|
1236
|
+
for (const entry of this.#linked()) {
|
|
1237
|
+
const link = readTaskIssueLink(entry.task);
|
|
1238
|
+
if (link === void 0) continue;
|
|
1239
|
+
const existing = cards[entry.task.id];
|
|
1240
|
+
cards[entry.task.id] = {
|
|
1241
|
+
issueId: link.issueId,
|
|
1242
|
+
...link.issueNumber === void 0 ? {} : { issueNumber: link.issueNumber },
|
|
1243
|
+
...link.hubStatus === void 0 ? {} : { hubStatus: link.hubStatus },
|
|
1244
|
+
...link.assignee === void 0 ? {} : { assignee: link.assignee },
|
|
1245
|
+
...link.reviewer === void 0 ? {} : { reviewer: link.reviewer },
|
|
1246
|
+
comments: existing?.comments ?? [],
|
|
1247
|
+
activity: existing?.activity ?? [],
|
|
1248
|
+
...link.lastError === void 0 ? {} : { lastError: link.lastError },
|
|
1249
|
+
...existing?.agentName === void 0 ? {} : { agentName: existing.agentName }
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
const agents = this.#agents.map((agent) => {
|
|
1253
|
+
const hubAgentId = this.#hubAgentIdOf(agent.memberId);
|
|
1254
|
+
return {
|
|
1255
|
+
memberId: agent.memberId,
|
|
1256
|
+
name: agent.name,
|
|
1257
|
+
...agent.preset === void 0 ? {} : { preset: agent.preset },
|
|
1258
|
+
...hubAgentId === void 0 ? {} : { hubAgentId }
|
|
1259
|
+
};
|
|
1260
|
+
});
|
|
1261
|
+
return {
|
|
1262
|
+
connection: {
|
|
1263
|
+
state: this.#connection,
|
|
1264
|
+
...this.#hostId === void 0 ? {} : { hostId: this.#hostId },
|
|
1265
|
+
...this.#lastHeartbeatAt === void 0 ? {} : { lastHeartbeatAt: this.#lastHeartbeatAt },
|
|
1266
|
+
reconnects: this.#reconnects,
|
|
1267
|
+
...this.#lastError === void 0 ? {} : { lastError: this.#lastError }
|
|
1268
|
+
},
|
|
1269
|
+
capabilities: this.#options.capabilities,
|
|
1270
|
+
agents,
|
|
1271
|
+
problems: [...this.#problems, ...this.#conflictProblems()],
|
|
1272
|
+
presetPin: this.#presetPin,
|
|
1273
|
+
cards,
|
|
1274
|
+
pendingQuestions: [...this.#questions.pending()]
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Report one settled execution back to the hub.
|
|
1279
|
+
* @param event - the board's settlement notification.
|
|
1280
|
+
*/
|
|
1281
|
+
async handleExecutionSettled(event) {
|
|
1282
|
+
const task = this.#options.host.tasks.get(event.taskId);
|
|
1283
|
+
if (task === void 0) return;
|
|
1284
|
+
const link = readTaskIssueLink(task);
|
|
1285
|
+
if (link === void 0) return;
|
|
1286
|
+
this.#releaseLease(task.id);
|
|
1287
|
+
const attemptId = link.attemptId;
|
|
1288
|
+
if (attemptId === void 0) {
|
|
1289
|
+
this.#recordError(task.id, /* @__PURE__ */ new Error("this card carries no execution attempt, so its outcome cannot be attributed and was not written back"));
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
const body = buildSettleRequest({
|
|
1293
|
+
task,
|
|
1294
|
+
executionId: event.executionId,
|
|
1295
|
+
outcome: event.outcome,
|
|
1296
|
+
...event.error === void 0 ? {} : { error: event.error },
|
|
1297
|
+
attemptId
|
|
1298
|
+
});
|
|
1299
|
+
this.#writeLink(task.id, { pendingSettle: body });
|
|
1300
|
+
await this.#writeSettlement(task.id, link.issueId, body);
|
|
1301
|
+
this.#publish();
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* Send one recorded settlement and, once the hub takes it, release everything
|
|
1305
|
+
* the hub now owns.
|
|
1306
|
+
*
|
|
1307
|
+
* The attempt and its lease belong to a finished run: settling is the last
|
|
1308
|
+
* thing this Host does with them, so a later claim of the same issue is fresh
|
|
1309
|
+
* instead of a reconnect that would ask the hub to re-establish a dead attempt.
|
|
1310
|
+
* @param taskId - the card the settlement belongs to.
|
|
1311
|
+
* @param issueId - the issue it settles.
|
|
1312
|
+
* @param body - the settlement body, as the card recorded it.
|
|
1313
|
+
*/
|
|
1314
|
+
async #writeSettlement(taskId, issueId, body) {
|
|
1315
|
+
const attemptId = body.execution_attempt_id;
|
|
1316
|
+
if (attemptId === void 0) {
|
|
1317
|
+
this.#writeLink(taskId, { pendingSettle: void 0 });
|
|
1318
|
+
this.#recordError(taskId, /* @__PURE__ */ new Error("a settlement without an execution attempt is never sent to the hub"));
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
try {
|
|
1322
|
+
const settled = await this.#options.hub.settle(issueId, body);
|
|
1323
|
+
this.#writeLink(taskId, {
|
|
1324
|
+
pendingSettle: void 0,
|
|
1325
|
+
attemptId: void 0,
|
|
1326
|
+
leaseToken: void 0,
|
|
1327
|
+
hubStatus: readStatus(settled.status)
|
|
1328
|
+
});
|
|
1329
|
+
} catch (error) {
|
|
1330
|
+
if (!isSettlementFinal(error)) {
|
|
1331
|
+
this.#recordError(taskId, error);
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
const card = this.#options.host.tasks.get(taskId);
|
|
1335
|
+
const current = card === void 0 ? void 0 : readTaskIssueLink(card);
|
|
1336
|
+
if (current?.attemptId !== void 0 && current.attemptId !== attemptId) {
|
|
1337
|
+
this.#writeLink(taskId, { pendingSettle: void 0 });
|
|
1338
|
+
this.#options.log?.("dropped the settlement of a replaced attempt for " + issueId, error);
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
this.#writeLink(taskId, {
|
|
1342
|
+
pendingSettle: void 0,
|
|
1343
|
+
attemptId: void 0,
|
|
1344
|
+
leaseToken: void 0
|
|
1345
|
+
});
|
|
1346
|
+
this.#recordError(taskId, /* @__PURE__ */ new Error("the hub refused this settlement for good (" + describe(error) + "); it was dropped instead of retried, so start the card again to report a new outcome"));
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
/**
|
|
1350
|
+
* Retry every settlement the hub has not accepted yet.
|
|
1351
|
+
*
|
|
1352
|
+
* A refusal is not permission to decide the outcome locally: the retry presents
|
|
1353
|
+
* the same body and the hub's own gates still decide whether it lands. A
|
|
1354
|
+
* settlement an expired attempt made impossible was already dropped by
|
|
1355
|
+
* {@link TeamSyncService.#writeSettlement}, so only a retryable refusal is
|
|
1356
|
+
* carried into this pass.
|
|
1357
|
+
*/
|
|
1358
|
+
async #retryPendingSettlements() {
|
|
1359
|
+
for (const entry of this.#linked()) {
|
|
1360
|
+
const link = readTeamIssueLink(entry.payload);
|
|
1361
|
+
const pending = link?.pendingSettle;
|
|
1362
|
+
if (link === void 0 || pending === void 0) continue;
|
|
1363
|
+
this.#settledThisPass.add(link.issueId);
|
|
1364
|
+
await this.#writeSettlement(entry.task.id, link.issueId, pending);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Claim every issue the hub offers this Host, for the agents bound to it.
|
|
1369
|
+
* @returns how many issues this pass turned into a new card.
|
|
1370
|
+
*/
|
|
1371
|
+
async sweep() {
|
|
1372
|
+
if (!this.#started) return 0;
|
|
1373
|
+
await this.whenStarted();
|
|
1374
|
+
return await this.#runSweep();
|
|
1375
|
+
}
|
|
1376
|
+
/**
|
|
1377
|
+
* Ask for a sweep, coalesced to at most one pass per cadence window.
|
|
1378
|
+
*
|
|
1379
|
+
* A burst of channel events (a reviewer rejecting, a status moving, an issue
|
|
1380
|
+
* appearing) asks for the same work, and the hub counts Host requests per
|
|
1381
|
+
* minute (protocol amendment A4 point 6), so the asks collapse into one pass:
|
|
1382
|
+
* the current window's pass or the single pass scheduled at its boundary.
|
|
1383
|
+
*/
|
|
1384
|
+
requestSweep() {
|
|
1385
|
+
if (!this.#started) return;
|
|
1386
|
+
if (this.#pendingSweep !== void 0) return;
|
|
1387
|
+
const now = this.#options.now();
|
|
1388
|
+
const last = this.#lastSweepAt;
|
|
1389
|
+
if (last === void 0 || now - last >= 5e3) {
|
|
1390
|
+
this.#runSweep();
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
this.#pendingSweep = this.#options.timers.timeout(() => {
|
|
1394
|
+
this.#pendingSweep = void 0;
|
|
1395
|
+
this.#runSweep();
|
|
1396
|
+
}, SWEEP_INTERVAL_MS - (now - last));
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* One claim sweep over what the hub currently offers this Host.
|
|
1400
|
+
* @returns how many issues this pass turned into a new card.
|
|
1401
|
+
*/
|
|
1402
|
+
async #runSweep() {
|
|
1403
|
+
if (this.#hostId === void 0) return 0;
|
|
1404
|
+
this.#lastSweepAt = this.#options.now();
|
|
1405
|
+
this.#settledThisPass.clear();
|
|
1406
|
+
await this.#retryPendingSettlements();
|
|
1407
|
+
if (this.#agents.length > 0 && !this.#agents.some((agent) => this.#workspaceFor(agent) !== "")) {
|
|
1408
|
+
this.#raiseProblem(WORKSPACE_PROBLEM);
|
|
1409
|
+
this.#publish();
|
|
1410
|
+
return 0;
|
|
1411
|
+
}
|
|
1412
|
+
this.#dropProblem(WORKSPACE_PROBLEM);
|
|
1413
|
+
let issues;
|
|
1414
|
+
try {
|
|
1415
|
+
issues = await this.#options.hub.listClaimableIssues(this.#hostId);
|
|
1416
|
+
} catch (error) {
|
|
1417
|
+
this.#lastError = describe(error);
|
|
1418
|
+
this.#publish();
|
|
1419
|
+
return 0;
|
|
1420
|
+
}
|
|
1421
|
+
let claimed = 0;
|
|
1422
|
+
for (const issue of issues) {
|
|
1423
|
+
const agent = this.#agentFor(issue);
|
|
1424
|
+
if (agent === void 0) continue;
|
|
1425
|
+
if (this.#workspaceFor(agent) === "") continue;
|
|
1426
|
+
try {
|
|
1427
|
+
if (await this.#claimIssue(issue, agent)) claimed += 1;
|
|
1428
|
+
} catch (error) {
|
|
1429
|
+
this.#lastError = describe(error);
|
|
1430
|
+
this.#options.log?.("claim failed for " + issue.id, error);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
this.#publish();
|
|
1434
|
+
return claimed;
|
|
1435
|
+
}
|
|
1436
|
+
/** The agents whose hub records were synchronized at registration. */
|
|
1437
|
+
boundAgents() {
|
|
1438
|
+
return this.#agents;
|
|
1439
|
+
}
|
|
1440
|
+
async #bootstrap() {
|
|
1441
|
+
const resolved = await this.#options.resolveAgents();
|
|
1442
|
+
const runnable = resolved.agents.filter((agent) => agent.preset !== void 0 && agent.preset !== "");
|
|
1443
|
+
const skipped = resolved.agents.filter((agent) => agent.preset === void 0 || agent.preset === "").map((agent) => agent.name + " is not registered: its dsh-agents definition pins no agent preset, so this Host would have to run it under the deployment default");
|
|
1444
|
+
this.#agents = runnable;
|
|
1445
|
+
this.#problems = [...resolved.problems, ...skipped];
|
|
1446
|
+
if (this.#agents.length === 0) {
|
|
1447
|
+
this.#problems = [...this.#problems, "no dsh-agents member is bound to this Host, so no issue is claimed"];
|
|
1448
|
+
this.#publish();
|
|
1449
|
+
return;
|
|
1450
|
+
}
|
|
1451
|
+
await this.#register();
|
|
1452
|
+
if (this.#hostId === void 0) {
|
|
1453
|
+
this.#publish();
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
this.#startHeartbeat();
|
|
1457
|
+
await this.#openChannel();
|
|
1458
|
+
await this.#runSweep();
|
|
1459
|
+
}
|
|
1460
|
+
async #register() {
|
|
1461
|
+
this.#connection = "connecting";
|
|
1462
|
+
const definitions = this.#agents.map((agent) => ({
|
|
1463
|
+
name: agent.name,
|
|
1464
|
+
preset_id: agent.preset ?? "",
|
|
1465
|
+
...agent.model === void 0 ? {} : { model: agent.model },
|
|
1466
|
+
...agent.reasoningEffort === void 0 ? {} : { reasoning_effort: agent.reasoningEffort }
|
|
1467
|
+
}));
|
|
1468
|
+
try {
|
|
1469
|
+
const registered = await this.#options.hub.register({
|
|
1470
|
+
hostname: this.#options.settings().hostname,
|
|
1471
|
+
client_version: HOST_CLIENT_VERSION,
|
|
1472
|
+
agents: definitions
|
|
1473
|
+
});
|
|
1474
|
+
this.#hostId = registered.host_id;
|
|
1475
|
+
this.#connection = "online";
|
|
1476
|
+
this.#lastError = void 0;
|
|
1477
|
+
this.#hubAgents.clear();
|
|
1478
|
+
for (const record of readAgentRecords(registered)) {
|
|
1479
|
+
const local = this.#agents.find((agent) => agent.name === record.name);
|
|
1480
|
+
if (local !== void 0) this.#hubAgents.set(record.id, local);
|
|
1481
|
+
}
|
|
1482
|
+
this.#agentConflicts.clear();
|
|
1483
|
+
for (const conflict of readAgentConflicts(registered)) this.#agentConflicts.set(conflict.name, conflict.reason);
|
|
1484
|
+
} catch (error) {
|
|
1485
|
+
this.#hostId = void 0;
|
|
1486
|
+
this.#connection = "error";
|
|
1487
|
+
this.#lastError = describe(error);
|
|
1488
|
+
this.#options.log?.("host registration failed", error);
|
|
1489
|
+
}
|
|
1490
|
+
this.#publish();
|
|
1491
|
+
}
|
|
1492
|
+
#startHeartbeat() {
|
|
1493
|
+
this.#stopHeartbeat?.();
|
|
1494
|
+
const beat = () => {
|
|
1495
|
+
const hostId = this.#hostId;
|
|
1496
|
+
if (hostId === void 0) return;
|
|
1497
|
+
this.#options.hub.heartbeat(hostId).then(() => {
|
|
1498
|
+
this.#lastHeartbeatAt = this.#options.now();
|
|
1499
|
+
if (this.#connection === "reconnecting") this.#connection = "online";
|
|
1500
|
+
this.#publish();
|
|
1501
|
+
}).catch((error) => {
|
|
1502
|
+
this.#lastError = describe(error);
|
|
1503
|
+
this.#options.log?.("heartbeat failed", error);
|
|
1504
|
+
this.#publish();
|
|
1505
|
+
});
|
|
1506
|
+
};
|
|
1507
|
+
this.#stopHeartbeat = this.#options.timers.interval(beat, HEARTBEAT_INTERVAL_MS);
|
|
1508
|
+
beat();
|
|
1509
|
+
}
|
|
1510
|
+
async #openChannel() {
|
|
1511
|
+
this.#stopReconnect?.();
|
|
1512
|
+
this.#stopReconnect = void 0;
|
|
1513
|
+
let socket;
|
|
1514
|
+
try {
|
|
1515
|
+
socket = await this.#options.connect(this.#seq, {
|
|
1516
|
+
message: (data) => {
|
|
1517
|
+
this.#onFrame(data);
|
|
1518
|
+
},
|
|
1519
|
+
closed: (reason) => {
|
|
1520
|
+
this.#onClosed(reason);
|
|
1521
|
+
}
|
|
1522
|
+
});
|
|
1523
|
+
} catch (error) {
|
|
1524
|
+
this.#options.log?.("team channel could not open", error);
|
|
1525
|
+
socket = void 0;
|
|
1526
|
+
}
|
|
1527
|
+
if (socket === void 0) {
|
|
1528
|
+
this.#lastError = "the team channel is unavailable; reconnecting";
|
|
1529
|
+
this.#scheduleReconnect();
|
|
1530
|
+
this.#publish();
|
|
1531
|
+
return;
|
|
1532
|
+
}
|
|
1533
|
+
this.#socket = socket;
|
|
1534
|
+
this.#connection = "online";
|
|
1535
|
+
this.#reconnectAttempt = 0;
|
|
1536
|
+
this.#publish();
|
|
1537
|
+
}
|
|
1538
|
+
#scheduleReconnect() {
|
|
1539
|
+
this.#stopReconnect?.();
|
|
1540
|
+
this.#connection = "reconnecting";
|
|
1541
|
+
this.#reconnects += 1;
|
|
1542
|
+
const delay = Math.min(RECONNECT_BASE_MS * 2 ** this.#reconnectAttempt, RECONNECT_MAX_MS);
|
|
1543
|
+
this.#reconnectAttempt += 1;
|
|
1544
|
+
this.#stopReconnect = this.#options.timers.timeout(() => {
|
|
1545
|
+
this.#stopReconnect = void 0;
|
|
1546
|
+
this.#register().then(async () => {
|
|
1547
|
+
await this.#openChannel();
|
|
1548
|
+
await this.#runSweep();
|
|
1549
|
+
}).catch((error) => {
|
|
1550
|
+
this.#options.log?.("reconnect failed", error);
|
|
1551
|
+
this.#scheduleReconnect();
|
|
1552
|
+
});
|
|
1553
|
+
}, delay);
|
|
1554
|
+
}
|
|
1555
|
+
#onClosed(reason) {
|
|
1556
|
+
if (!this.#started) return;
|
|
1557
|
+
this.#socket = void 0;
|
|
1558
|
+
if (reason !== void 0 && reason !== "") this.#lastError = reason;
|
|
1559
|
+
this.#publish();
|
|
1560
|
+
this.#scheduleReconnect();
|
|
1561
|
+
}
|
|
1562
|
+
#onFrame(data) {
|
|
1563
|
+
let parsed;
|
|
1564
|
+
try {
|
|
1565
|
+
parsed = JSON.parse(data);
|
|
1566
|
+
} catch {
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
const frame = readServerFrame(parsed);
|
|
1570
|
+
if (frame === void 0) return;
|
|
1571
|
+
if (frame.t === "pong") return;
|
|
1572
|
+
if (frame.t === "resync_required") {
|
|
1573
|
+
this.#seq = 0;
|
|
1574
|
+
this.requestSweep();
|
|
1575
|
+
return;
|
|
1576
|
+
}
|
|
1577
|
+
this.#onEvent(frame);
|
|
1578
|
+
}
|
|
1579
|
+
#onEvent(frame) {
|
|
1580
|
+
if (frame.seq <= this.#seq && this.#seq !== 0) return;
|
|
1581
|
+
this.#seq = frame.seq;
|
|
1582
|
+
const payload = frame.payload;
|
|
1583
|
+
switch (frame.event) {
|
|
1584
|
+
case "question.answered":
|
|
1585
|
+
this.#questions.answered(payload);
|
|
1586
|
+
return;
|
|
1587
|
+
case "question.expired":
|
|
1588
|
+
this.#questions.expired(payload);
|
|
1589
|
+
return;
|
|
1590
|
+
case "question.withdrawn":
|
|
1591
|
+
this.#questions.withdrawn(payload);
|
|
1592
|
+
return;
|
|
1593
|
+
case "comment.created":
|
|
1594
|
+
this.#recordComment(payload);
|
|
1595
|
+
return;
|
|
1596
|
+
case "comment.mentioned":
|
|
1597
|
+
this.#deliverMention(payload);
|
|
1598
|
+
return;
|
|
1599
|
+
case "issue.rework":
|
|
1600
|
+
this.#rework(payload);
|
|
1601
|
+
return;
|
|
1602
|
+
case "issue.status_changed":
|
|
1603
|
+
this.#recordStatus(payload);
|
|
1604
|
+
return;
|
|
1605
|
+
case "issue.created":
|
|
1606
|
+
case "issue.updated":
|
|
1607
|
+
this.requestSweep();
|
|
1608
|
+
return;
|
|
1609
|
+
default: return;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Claim one issue for one bound agent out of a sweep.
|
|
1614
|
+
* @returns whether this call created a card.
|
|
1615
|
+
*/
|
|
1616
|
+
async #claimIssue(issue, agent) {
|
|
1617
|
+
return await this.#claimExclusively(issue.id, () => this.#claimOnce({
|
|
1618
|
+
issueId: issue.id,
|
|
1619
|
+
issue,
|
|
1620
|
+
agent
|
|
1621
|
+
}));
|
|
1622
|
+
}
|
|
1623
|
+
/**
|
|
1624
|
+
* Run one claim while no other claim for the same issue is in flight.
|
|
1625
|
+
*
|
|
1626
|
+
* Protocol amendment A4 point 5 makes claims on one issue strictly single
|
|
1627
|
+
* flight, and both paths that claim — a sweep and an `issue.rework` event, which
|
|
1628
|
+
* the hub sends back to back with `issue.updated` — enter here: the second
|
|
1629
|
+
* caller finds the issue already being claimed and has nothing to do, instead of
|
|
1630
|
+
* racing a claim the hub would refuse and recording that refusal as a failure.
|
|
1631
|
+
* @param issueId - the issue being claimed.
|
|
1632
|
+
* @param claim - the claim to run while the issue is held.
|
|
1633
|
+
* @returns whatever the claim answered; false when one is already in flight.
|
|
1634
|
+
*/
|
|
1635
|
+
async #claimExclusively(issueId, claim) {
|
|
1636
|
+
if (this.#claiming.has(issueId)) return false;
|
|
1637
|
+
this.#claiming.add(issueId);
|
|
1638
|
+
try {
|
|
1639
|
+
return await claim();
|
|
1640
|
+
} finally {
|
|
1641
|
+
this.#claiming.delete(issueId);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Claim one issue and act on what the claim grants.
|
|
1646
|
+
*
|
|
1647
|
+
* The card is the one this Host already mirrors, when it mirrors one: a claim
|
|
1648
|
+
* never creates a second card for one issue. A card whose run finished but
|
|
1649
|
+
* whose outcome the hub has not accepted is never claimed at all: the claim
|
|
1650
|
+
* would mint an attempt nothing here executes, and the retry of the old body
|
|
1651
|
+
* would then be refused against that attempt, losing what the run produced.
|
|
1652
|
+
* Only a pending rework may claim such a card, because the reviewer's
|
|
1653
|
+
* rejection IS the newer round.
|
|
1654
|
+
*
|
|
1655
|
+
* What runs afterwards follows from the claim itself — a pending rework always
|
|
1656
|
+
* runs, a reconnect runs nothing, and a card with nothing running here is
|
|
1657
|
+
* started again (a human retry moved the issue back to `assigned`, and nothing
|
|
1658
|
+
* else would ever start that card).
|
|
1659
|
+
* @param input - the issue, the agent it belongs to, and any rework the caller knows.
|
|
1660
|
+
* @returns whether this call created a card.
|
|
1661
|
+
*/
|
|
1662
|
+
async #claimOnce(input) {
|
|
1663
|
+
const existing = findLinkedTask(this.#linked(), input.issueId);
|
|
1664
|
+
const previous = existing === void 0 ? void 0 : readTaskIssueLink(existing);
|
|
1665
|
+
const agent = input.agent;
|
|
1666
|
+
const issue = input.issue;
|
|
1667
|
+
if (existing === void 0 && (agent === void 0 || issue === void 0)) return false;
|
|
1668
|
+
const instructions = input.rework ?? (typeof issue?.rework_instructions === "string" ? issue.rework_instructions.trim() : "");
|
|
1669
|
+
if (existing !== void 0 && previous?.pendingSettle !== void 0 && instructions === "") {
|
|
1670
|
+
if (!this.#settledThisPass.has(input.issueId)) {
|
|
1671
|
+
this.#settledThisPass.add(input.issueId);
|
|
1672
|
+
await this.#writeSettlement(existing.id, input.issueId, previous.pendingSettle);
|
|
1673
|
+
}
|
|
1674
|
+
if (this.#pendingSettleOf(existing.id) !== void 0) return false;
|
|
1675
|
+
}
|
|
1676
|
+
const claim = await this.#claimFromHub(input.issueId, existing, previous);
|
|
1677
|
+
if (claim === void 0) return false;
|
|
1678
|
+
if (existing !== void 0) {
|
|
1679
|
+
this.#writeLink(existing.id, {
|
|
1680
|
+
attemptId: claim.execution_attempt_id,
|
|
1681
|
+
leaseToken: claim.lease_token,
|
|
1682
|
+
leaseExpiresAt: claim.lease_expires_at,
|
|
1683
|
+
hubStatus: "running"
|
|
1684
|
+
});
|
|
1685
|
+
this.#ensureLease(existing, input.issueId, claim.lease_token);
|
|
1686
|
+
if (previous?.pendingSettle !== void 0 && previous.pendingSettle.execution_attempt_id !== claim.execution_attempt_id) this.#writeLink(existing.id, { pendingSettle: void 0 });
|
|
1687
|
+
if (this.#localExecutionRunning(existing)) return false;
|
|
1688
|
+
if (instructions !== "") {
|
|
1689
|
+
this.#applyRework(existing.id, instructions);
|
|
1690
|
+
return false;
|
|
1691
|
+
}
|
|
1692
|
+
this.#requestRun(existing.id);
|
|
1693
|
+
return false;
|
|
1694
|
+
}
|
|
1695
|
+
if (agent === void 0 || issue === void 0) return false;
|
|
1696
|
+
const workspaceId = this.#workspaceFor(agent);
|
|
1697
|
+
const mirror = {
|
|
1698
|
+
agentId: this.#hubAgentIdOf(agent.memberId) ?? agent.memberId,
|
|
1699
|
+
memberId: agent.memberId,
|
|
1700
|
+
name: agent.name,
|
|
1701
|
+
...agent.preset === void 0 ? {} : { preset: agent.preset },
|
|
1702
|
+
...agent.model === void 0 ? {} : { model: agent.model },
|
|
1703
|
+
...agent.reasoningEffort === void 0 ? {} : { reasoningEffort: agent.reasoningEffort },
|
|
1704
|
+
...workspaceId === "" ? {} : { workspaceId }
|
|
1705
|
+
};
|
|
1706
|
+
const draft = buildMirrorDraft(issue, mirror);
|
|
1707
|
+
const payload = toPayload({
|
|
1708
|
+
issueId: issue.id,
|
|
1709
|
+
issueNumber: issue.issue_number,
|
|
1710
|
+
attemptId: claim.execution_attempt_id,
|
|
1711
|
+
leaseToken: claim.lease_token,
|
|
1712
|
+
leaseExpiresAt: claim.lease_expires_at,
|
|
1713
|
+
agentId: mirror.agentId,
|
|
1714
|
+
agentMemberId: agent.memberId,
|
|
1715
|
+
...agent.preset === void 0 ? {} : { preset: agent.preset },
|
|
1716
|
+
hubStatus: "running"
|
|
1717
|
+
});
|
|
1718
|
+
let task;
|
|
1719
|
+
try {
|
|
1720
|
+
task = this.#options.host.tasks.create(draft, { payload });
|
|
1721
|
+
} catch (error) {
|
|
1722
|
+
this.#options.log?.("mirror card refused for " + issue.id, error);
|
|
1723
|
+
this.#lastError = describe(error);
|
|
1724
|
+
return false;
|
|
1725
|
+
}
|
|
1726
|
+
this.#cards.set(task.id, {
|
|
1727
|
+
issueId: issue.id,
|
|
1728
|
+
issueNumber: issue.issue_number,
|
|
1729
|
+
hubStatus: "running",
|
|
1730
|
+
agentName: agent.name,
|
|
1731
|
+
comments: [],
|
|
1732
|
+
activity: []
|
|
1733
|
+
});
|
|
1734
|
+
this.#ensureLease(task, issue.id, claim.lease_token);
|
|
1735
|
+
if (!this.#verifyDraftPin(task.id, agent)) {
|
|
1736
|
+
this.#recordError(task.id, /* @__PURE__ */ new Error("this board revision cannot pin an agent preset on a new card, so the mirror card was created without " + agent.name + "'s own preset and was not started; start it by hand once the board supports draft targets"));
|
|
1737
|
+
return true;
|
|
1738
|
+
}
|
|
1739
|
+
this.#requestRun(task.id);
|
|
1740
|
+
return true;
|
|
1741
|
+
}
|
|
1742
|
+
/**
|
|
1743
|
+
* The settlement one card still owes the hub, read back from the card itself.
|
|
1744
|
+
* @param taskId - the card to read.
|
|
1745
|
+
* @returns the pending settlement, or undefined when the hub took it.
|
|
1746
|
+
*/
|
|
1747
|
+
#pendingSettleOf(taskId) {
|
|
1748
|
+
const task = this.#options.host.tasks.get(taskId);
|
|
1749
|
+
if (task === void 0) return void 0;
|
|
1750
|
+
return readTaskIssueLink(task)?.pendingSettle;
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Ask the hub for one issue, presenting this Host's previous attempt only on a
|
|
1754
|
+
* reconnect.
|
|
1755
|
+
*
|
|
1756
|
+
* A reconnect is the one moment the old credentials are still true: the run is
|
|
1757
|
+
* executing here and this Host still holds the lease, so the hub re-establishes
|
|
1758
|
+
* that attempt instead of minting a new one (protocol section 5). Every other
|
|
1759
|
+
* claim is fresh and the hub mints the attempt id itself, so presenting one it
|
|
1760
|
+
* never granted would only ask it to adopt this Host's guess.
|
|
1761
|
+
* @param issueId - the issue to claim.
|
|
1762
|
+
* @param task - the card that mirrors it, when it has one.
|
|
1763
|
+
* @param link - that card's stored linkage.
|
|
1764
|
+
* @returns the lease the hub granted, or undefined when another attempt holds the issue.
|
|
1765
|
+
*/
|
|
1766
|
+
async #claimFromHub(issueId, task, link) {
|
|
1767
|
+
try {
|
|
1768
|
+
return await this.#options.hub.claim(issueId, this.#reconnectCredentials(task, link));
|
|
1769
|
+
} catch (error) {
|
|
1770
|
+
if (isClaimHeldElsewhere(error)) return void 0;
|
|
1771
|
+
throw error;
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
/**
|
|
1775
|
+
* The credentials one claim presents, which is non-empty only on a reconnect.
|
|
1776
|
+
* @param task - the card that mirrors the issue, when it has one.
|
|
1777
|
+
* @param link - that card's stored linkage.
|
|
1778
|
+
* @returns the attempt and lease to present, or nothing for a fresh claim.
|
|
1779
|
+
*/
|
|
1780
|
+
#reconnectCredentials(task, link) {
|
|
1781
|
+
if (task === void 0 || link === void 0) return {};
|
|
1782
|
+
if (link.attemptId === void 0 || link.leaseToken === void 0) return {};
|
|
1783
|
+
if (!this.#localExecutionRunning(task)) return {};
|
|
1784
|
+
return {
|
|
1785
|
+
execution_attempt_id: link.attemptId,
|
|
1786
|
+
lease_token: link.leaseToken
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
/**
|
|
1790
|
+
* Whether this Host still runs one of the card's executions.
|
|
1791
|
+
*
|
|
1792
|
+
* The board closes an execution by stamping its end instant, so an execution
|
|
1793
|
+
* with no end is what "running here" means: the card's column can read
|
|
1794
|
+
* `running` for a declaration a human made, and that starts nothing.
|
|
1795
|
+
* @param task - the card to read.
|
|
1796
|
+
* @returns whether one of its executions is still open.
|
|
1797
|
+
*/
|
|
1798
|
+
#localExecutionRunning(task) {
|
|
1799
|
+
return task.executions.some((execution) => execution.endedAt === void 0);
|
|
1800
|
+
}
|
|
1801
|
+
/**
|
|
1802
|
+
* Check that the created card really carries the member's execution targets.
|
|
1803
|
+
* @param taskId - the card just created.
|
|
1804
|
+
* @param agent - the member it was created for.
|
|
1805
|
+
* @returns whether the board kept the pinned preset (no preset means nothing to check).
|
|
1806
|
+
*/
|
|
1807
|
+
#verifyDraftPin(taskId, agent) {
|
|
1808
|
+
if (agent.preset === void 0 || agent.preset === "") {
|
|
1809
|
+
this.#presetPin = "supported";
|
|
1810
|
+
return true;
|
|
1811
|
+
}
|
|
1812
|
+
const created = this.#options.host.tasks.get(taskId);
|
|
1813
|
+
if (created !== void 0 && created.mode === agent.preset) {
|
|
1814
|
+
this.#presetPin = "supported";
|
|
1815
|
+
return true;
|
|
1816
|
+
}
|
|
1817
|
+
this.#presetPin = "missing";
|
|
1818
|
+
this.#raiseProblem(DRAFT_PIN_PROBLEM);
|
|
1819
|
+
return false;
|
|
1820
|
+
}
|
|
1821
|
+
/**
|
|
1822
|
+
* Post one agent comment on the issue a card mirrors.
|
|
1823
|
+
*
|
|
1824
|
+
* The agent identity is the hub record this Host registered for the member the
|
|
1825
|
+
* card was claimed for: the hub only accepts agent comments from the token
|
|
1826
|
+
* holder that owns that agent, so the id comes from the payload instead of
|
|
1827
|
+
* from whatever the model named.
|
|
1828
|
+
* @param input - the card or issue to comment on, and the markdown body.
|
|
1829
|
+
* @returns the stored comment's id.
|
|
1830
|
+
*/
|
|
1831
|
+
async postComment(input) {
|
|
1832
|
+
const linked = this.#linked();
|
|
1833
|
+
const task = input.taskId === void 0 ? input.issueId === void 0 ? void 0 : findLinkedTask(linked, input.issueId) : this.#options.host.tasks.get(input.taskId);
|
|
1834
|
+
if (task === void 0) throw new Error("no team card matches this comment");
|
|
1835
|
+
const link = readTaskIssueLink(task);
|
|
1836
|
+
if (link === void 0) throw new Error("that card does not mirror a team issue");
|
|
1837
|
+
const agentId = link.agentId ?? this.#hubAgentIdOf(link.agentMemberId ?? "");
|
|
1838
|
+
if (agentId === void 0) throw new Error("no hub agent record is bound to that card yet");
|
|
1839
|
+
const comment = await this.#options.hub.createComment(link.issueId, {
|
|
1840
|
+
content: input.text,
|
|
1841
|
+
author_type: "agent",
|
|
1842
|
+
author_agent_id: agentId
|
|
1843
|
+
});
|
|
1844
|
+
this.#recordComment({ comment });
|
|
1845
|
+
return {
|
|
1846
|
+
issueId: link.issueId,
|
|
1847
|
+
commentId: comment.id
|
|
1848
|
+
};
|
|
1849
|
+
}
|
|
1850
|
+
#requestRun(taskId) {
|
|
1851
|
+
const run = this.#options.requestRun;
|
|
1852
|
+
if (run === void 0) {
|
|
1853
|
+
this.#recordError(taskId, /* @__PURE__ */ new Error("this board revision serves no way to start an execution; open the card and run it from the board"));
|
|
1854
|
+
return;
|
|
1855
|
+
}
|
|
1856
|
+
try {
|
|
1857
|
+
run(taskId);
|
|
1858
|
+
} catch (error) {
|
|
1859
|
+
this.#recordError(taskId, error);
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
#ensureLease(task, issueId, leaseToken) {
|
|
1863
|
+
this.#releaseLease(task.id);
|
|
1864
|
+
const renew = () => {
|
|
1865
|
+
this.#options.hub.renewLease(issueId, leaseToken).then((renewed) => {
|
|
1866
|
+
this.#writeLink(task.id, { leaseExpiresAt: renewed.lease_expires_at });
|
|
1867
|
+
}).catch((error) => {
|
|
1868
|
+
if (error instanceof HubError && error.code === "LEASE_EXPIRED") {
|
|
1869
|
+
this.#releaseLease(task.id);
|
|
1870
|
+
this.#recordError(task.id, error);
|
|
1871
|
+
return;
|
|
1872
|
+
}
|
|
1873
|
+
this.#options.log?.("lease renewal failed for " + issueId, error);
|
|
1874
|
+
});
|
|
1875
|
+
};
|
|
1876
|
+
this.#leases.set(task.id, this.#options.timers.interval(renew, LEASE_RENEW_INTERVAL_MS));
|
|
1877
|
+
renew();
|
|
1878
|
+
}
|
|
1879
|
+
#releaseLease(taskId) {
|
|
1880
|
+
const cancel = this.#leases.get(taskId);
|
|
1881
|
+
if (cancel === void 0) return;
|
|
1882
|
+
cancel();
|
|
1883
|
+
this.#leases.delete(taskId);
|
|
1884
|
+
}
|
|
1885
|
+
async #deliverMention(payload) {
|
|
1886
|
+
const issueId = typeof payload.issue_id === "string" ? payload.issue_id : void 0;
|
|
1887
|
+
const content = typeof payload.content === "string" ? payload.content : "";
|
|
1888
|
+
if (issueId === void 0 || content.trim() === "") return;
|
|
1889
|
+
const author = typeof payload.author === "string" && payload.author !== "" ? payload.author : "a team member";
|
|
1890
|
+
const task = findLinkedTask(this.#linked(), issueId);
|
|
1891
|
+
if (task === void 0) return;
|
|
1892
|
+
this.#pushActivity(task.id, issueId, "comment.mentioned", clip(author + ": " + content));
|
|
1893
|
+
const sessions = this.#options.host.sessions;
|
|
1894
|
+
const deliver = sessions?.followUp;
|
|
1895
|
+
if (sessions === void 0 || deliver === void 0) {
|
|
1896
|
+
this.#recordError(task.id, /* @__PURE__ */ new Error("this board revision serves no follow-up delivery, so a mention could not reach the session"));
|
|
1897
|
+
return;
|
|
1898
|
+
}
|
|
1899
|
+
try {
|
|
1900
|
+
await deliver.call(sessions, task.id, clip(author + " mentioned this task: " + content));
|
|
1901
|
+
} catch (error) {
|
|
1902
|
+
this.#recordError(task.id, error);
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
async #rework(payload) {
|
|
1906
|
+
const issueId = typeof payload.issue_id === "string" ? payload.issue_id : void 0;
|
|
1907
|
+
if (issueId === void 0) return;
|
|
1908
|
+
const instructions = typeof payload.instructions === "string" ? payload.instructions.trim() : "";
|
|
1909
|
+
const task = findLinkedTask(this.#linked(), issueId);
|
|
1910
|
+
if (task === void 0) {
|
|
1911
|
+
this.#options.log?.("rework requested for an issue this Host does not mirror: " + issueId);
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
if (instructions === "") {
|
|
1915
|
+
this.#recordError(task.id, /* @__PURE__ */ new Error("the rework request carried no instructions, so nothing was re-run"));
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1918
|
+
try {
|
|
1919
|
+
await this.#claimExclusively(issueId, () => this.#claimOnce({
|
|
1920
|
+
issueId,
|
|
1921
|
+
rework: instructions
|
|
1922
|
+
}));
|
|
1923
|
+
} catch (error) {
|
|
1924
|
+
this.#recordError(task.id, error);
|
|
1925
|
+
return;
|
|
1926
|
+
}
|
|
1927
|
+
this.#publish();
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* Apply review instructions to one already-mirrored card through the board's
|
|
1931
|
+
* rework capability, or report the gap.
|
|
1932
|
+
* @param taskId - the card the reviewer sent back.
|
|
1933
|
+
* @param instructions - what the reviewer asked for.
|
|
1934
|
+
*/
|
|
1935
|
+
#applyRework(taskId, instructions) {
|
|
1936
|
+
const rework = this.#options.host.tasks.rework;
|
|
1937
|
+
if (rework === void 0) {
|
|
1938
|
+
this.#recordError(taskId, /* @__PURE__ */ new Error("this board revision serves no rework, so the review instructions could not be applied"));
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
try {
|
|
1942
|
+
rework.call(this.#options.host.tasks, taskId, instructions, "team-hub");
|
|
1943
|
+
} catch (error) {
|
|
1944
|
+
this.#recordError(taskId, error);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
#recordComment(payload) {
|
|
1948
|
+
const comment = payload.comment;
|
|
1949
|
+
if (typeof comment !== "object" || comment === null) return;
|
|
1950
|
+
const row = comment;
|
|
1951
|
+
if (typeof row.issue_id !== "string") return;
|
|
1952
|
+
const task = findLinkedTask(this.#linked(), row.issue_id);
|
|
1953
|
+
if (task === void 0) return;
|
|
1954
|
+
const view = this.#view(task.id, row.issue_id);
|
|
1955
|
+
const author = this.#authorLabel(row);
|
|
1956
|
+
const entry = {
|
|
1957
|
+
id: typeof row.id === "string" ? row.id : this.#options.uuid(),
|
|
1958
|
+
author,
|
|
1959
|
+
content: clip(typeof row.content === "string" ? row.content : ""),
|
|
1960
|
+
createdAt: typeof row.created_at === "number" ? row.created_at : this.#options.now()
|
|
1961
|
+
};
|
|
1962
|
+
view.comments = [...view.comments, entry].slice(-20);
|
|
1963
|
+
this.#pushActivity(task.id, row.issue_id, "comment.created", entry.content);
|
|
1964
|
+
this.#publish();
|
|
1965
|
+
}
|
|
1966
|
+
/**
|
|
1967
|
+
* Render one comment author the way a reader recognizes them.
|
|
1968
|
+
*
|
|
1969
|
+
* An agent author resolves to the member name this Host registered, and a human
|
|
1970
|
+
* author to the display name the card already knows; a name this Host cannot
|
|
1971
|
+
* resolve reads as "team member" rather than as a raw `mem_...` identifier,
|
|
1972
|
+
* which tells a reader nothing about who is speaking.
|
|
1973
|
+
* @param comment - the comment row the hub served.
|
|
1974
|
+
* @returns the label the detail section renders.
|
|
1975
|
+
*/
|
|
1976
|
+
#authorLabel(comment) {
|
|
1977
|
+
const agentId = typeof comment.author_agent_id === "string" ? comment.author_agent_id : void 0;
|
|
1978
|
+
if (agentId !== void 0) {
|
|
1979
|
+
const agent = this.#hubAgents.get(agentId);
|
|
1980
|
+
if (agent !== void 0) return agent.name;
|
|
1981
|
+
return "team agent";
|
|
1982
|
+
}
|
|
1983
|
+
const memberId = typeof comment.author_member_id === "string" ? comment.author_member_id : void 0;
|
|
1984
|
+
if (memberId !== void 0) for (const view of this.#cards.values()) {
|
|
1985
|
+
if (view.reviewerMemberId === memberId && view.reviewer !== void 0) return view.reviewer;
|
|
1986
|
+
if (view.assigneeMemberId === memberId && view.assignee !== void 0) return view.assignee;
|
|
1987
|
+
}
|
|
1988
|
+
return "team member";
|
|
1989
|
+
}
|
|
1990
|
+
#recordStatus(payload) {
|
|
1991
|
+
const issueId = typeof payload.issue_id === "string" ? payload.issue_id : void 0;
|
|
1992
|
+
const status = readStatus(payload.new_status);
|
|
1993
|
+
if (issueId === void 0 || status === void 0) return;
|
|
1994
|
+
const task = findLinkedTask(this.#linked(), issueId);
|
|
1995
|
+
if (task === void 0) return;
|
|
1996
|
+
const view = this.#view(task.id, issueId);
|
|
1997
|
+
view.hubStatus = status;
|
|
1998
|
+
this.#pushActivity(task.id, issueId, "issue.status_changed", status);
|
|
1999
|
+
this.#writeLink(task.id, { hubStatus: status });
|
|
2000
|
+
if (status === "assigned" || status === "waiting_host") this.requestSweep();
|
|
2001
|
+
if (status === "done" || status === "canceled" || status === "failed") this.#releaseLease(task.id);
|
|
2002
|
+
this.#publish();
|
|
2003
|
+
}
|
|
2004
|
+
#pushActivity(taskId, issueId, type, text) {
|
|
2005
|
+
const view = this.#view(taskId, issueId);
|
|
2006
|
+
view.activity = [...view.activity, {
|
|
2007
|
+
id: this.#options.uuid(),
|
|
2008
|
+
type,
|
|
2009
|
+
text: clip(text),
|
|
2010
|
+
createdAt: this.#options.now()
|
|
2011
|
+
}].slice(-20);
|
|
2012
|
+
}
|
|
2013
|
+
#view(taskId, issueId) {
|
|
2014
|
+
const existing = this.#cards.get(taskId);
|
|
2015
|
+
if (existing !== void 0) {
|
|
2016
|
+
existing.issueId = issueId;
|
|
2017
|
+
return existing;
|
|
2018
|
+
}
|
|
2019
|
+
const created = {
|
|
2020
|
+
issueId,
|
|
2021
|
+
comments: [],
|
|
2022
|
+
activity: []
|
|
2023
|
+
};
|
|
2024
|
+
this.#cards.set(taskId, created);
|
|
2025
|
+
return created;
|
|
2026
|
+
}
|
|
2027
|
+
#recordError(taskId, error) {
|
|
2028
|
+
const message = describe(error);
|
|
2029
|
+
this.#lastError = message;
|
|
2030
|
+
const view = this.#cards.get(taskId);
|
|
2031
|
+
if (view !== void 0) view.lastError = message;
|
|
2032
|
+
if (this.#options.host.tasks.get(taskId) !== void 0) this.#writeLink(taskId, { lastError: message });
|
|
2033
|
+
this.#options.log?.(message);
|
|
2034
|
+
this.#publish();
|
|
2035
|
+
}
|
|
2036
|
+
#writeLink(taskId, patch) {
|
|
2037
|
+
const task = this.#options.host.tasks.get(taskId);
|
|
2038
|
+
if (task === void 0) return;
|
|
2039
|
+
const current = readTaskIssueLink(task);
|
|
2040
|
+
if (current === void 0) return;
|
|
2041
|
+
const merged = {
|
|
2042
|
+
...current,
|
|
2043
|
+
...patch
|
|
2044
|
+
};
|
|
2045
|
+
const payload = toPayload(merged);
|
|
2046
|
+
for (const key of Object.keys(patch)) if (patch[key] === void 0) payload[key] = void 0;
|
|
2047
|
+
try {
|
|
2048
|
+
this.#options.host.integration.write(taskId, payload);
|
|
2049
|
+
} catch (error) {
|
|
2050
|
+
this.#options.log?.("integration write failed for " + taskId, error);
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
const view = this.#cards.get(taskId);
|
|
2054
|
+
if (view !== void 0 && merged.hubStatus !== void 0) view.hubStatus = merged.hubStatus;
|
|
2055
|
+
}
|
|
2056
|
+
#linked() {
|
|
2057
|
+
try {
|
|
2058
|
+
return this.#options.host.tasks.linked();
|
|
2059
|
+
} catch {
|
|
2060
|
+
return [];
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
/**
|
|
2064
|
+
* The workspace a mirror card claimed for one member runs in.
|
|
2065
|
+
*
|
|
2066
|
+
* The mount's own setting wins, then the workspace the member's dsh-agents team
|
|
2067
|
+
* pins. An empty answer means the board could not start a card created for this
|
|
2068
|
+
* member, which is the state a claim must never enter.
|
|
2069
|
+
* @param agent - the member the issue would be claimed for.
|
|
2070
|
+
* @returns the workspace id, or an empty string when neither source names one.
|
|
2071
|
+
*/
|
|
2072
|
+
#workspaceFor(agent) {
|
|
2073
|
+
const configured = (this.#options.settings().workspaceId ?? "").trim();
|
|
2074
|
+
if (configured !== "") return configured;
|
|
2075
|
+
return (agent.workspaceId ?? "").trim();
|
|
2076
|
+
}
|
|
2077
|
+
#agentFor(issue) {
|
|
2078
|
+
const hubAgentId = issue.assignee_agent_id;
|
|
2079
|
+
if (hubAgentId === void 0) return void 0;
|
|
2080
|
+
return this.#hubAgents.get(hubAgentId);
|
|
2081
|
+
}
|
|
2082
|
+
#hubAgentIdOf(memberId) {
|
|
2083
|
+
for (const [hubId, agent] of this.#hubAgents) if (agent.memberId === memberId) return hubId;
|
|
2084
|
+
}
|
|
2085
|
+
/**
|
|
2086
|
+
* The problems the hub's registration answer leaves behind, one per clash.
|
|
2087
|
+
* @returns the visible lines the settings section renders.
|
|
2088
|
+
*/
|
|
2089
|
+
#conflictProblems() {
|
|
2090
|
+
return [...this.#agentConflicts].map(([name, reason]) => name + " is not registered: " + reason);
|
|
2091
|
+
}
|
|
2092
|
+
/**
|
|
2093
|
+
* Add one operator-visible problem line, once.
|
|
2094
|
+
*
|
|
2095
|
+
* A sweep repeats every few seconds, so appending the same line each pass would
|
|
2096
|
+
* bury every other problem under copies of itself.
|
|
2097
|
+
* @param line - the line to publish while its cause holds.
|
|
2098
|
+
*/
|
|
2099
|
+
#raiseProblem(line) {
|
|
2100
|
+
if (this.#problems.includes(line)) return;
|
|
2101
|
+
this.#problems = [...this.#problems, line];
|
|
2102
|
+
}
|
|
2103
|
+
/**
|
|
2104
|
+
* Drop one problem line whose cause is gone.
|
|
2105
|
+
* @param line - the line to stop publishing.
|
|
2106
|
+
*/
|
|
2107
|
+
#dropProblem(line) {
|
|
2108
|
+
if (!this.#problems.includes(line)) return;
|
|
2109
|
+
this.#problems = this.#problems.filter((problem) => problem !== line);
|
|
2110
|
+
}
|
|
2111
|
+
#publish() {
|
|
2112
|
+
try {
|
|
2113
|
+
this.#options.host.publish(this.summary());
|
|
2114
|
+
} catch (error) {
|
|
2115
|
+
this.#options.log?.("summary publish failed", error);
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
};
|
|
2119
|
+
/** Read one status string into the collaboration vocabulary. */
|
|
2120
|
+
function readStatus(raw) {
|
|
2121
|
+
switch (raw) {
|
|
2122
|
+
case "todo":
|
|
2123
|
+
case "assigned":
|
|
2124
|
+
case "waiting_host":
|
|
2125
|
+
case "running":
|
|
2126
|
+
case "in_review":
|
|
2127
|
+
case "done":
|
|
2128
|
+
case "canceled":
|
|
2129
|
+
case "failed": return raw;
|
|
2130
|
+
default: return;
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
/** Read the agent records one registration answered with. */
|
|
2134
|
+
function readAgentRecords(registered) {
|
|
2135
|
+
if (typeof registered !== "object" || registered === null) return [];
|
|
2136
|
+
const agents = registered.agents;
|
|
2137
|
+
if (!Array.isArray(agents)) return [];
|
|
2138
|
+
const records = [];
|
|
2139
|
+
for (const entry of agents) {
|
|
2140
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
2141
|
+
const record = entry;
|
|
2142
|
+
if (typeof record.id === "string" && typeof record.name === "string") records.push({
|
|
2143
|
+
id: record.id,
|
|
2144
|
+
name: record.name
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
return records;
|
|
2148
|
+
}
|
|
2149
|
+
/** Read the agent names one registration answer reported as already owned elsewhere. */
|
|
2150
|
+
function readAgentConflicts(answer) {
|
|
2151
|
+
if (typeof answer !== "object" || answer === null) return [];
|
|
2152
|
+
const conflicts = answer.conflicts;
|
|
2153
|
+
if (!Array.isArray(conflicts)) return [];
|
|
2154
|
+
const rows = [];
|
|
2155
|
+
for (const entry of conflicts) {
|
|
2156
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
2157
|
+
const row = entry;
|
|
2158
|
+
if (typeof row.name !== "string" || row.name === "") continue;
|
|
2159
|
+
rows.push({
|
|
2160
|
+
name: row.name,
|
|
2161
|
+
reason: typeof row.reason === "string" ? row.reason : "the hub reported a conflict"
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
return rows;
|
|
2165
|
+
}
|
|
2166
|
+
/**
|
|
2167
|
+
* Whether one settlement refusal means the hub will never accept that body.
|
|
2168
|
+
*
|
|
2169
|
+
* Two families are final. `412 LEASE_EXPIRED` says the issue no longer carries the
|
|
2170
|
+
* attempt the write-back presents — a rework, a retry or another attempt replaced
|
|
2171
|
+
* it — so the same body can never match again. Every other deterministic 4xx says
|
|
2172
|
+
* the request itself is refused, which a later identical request does not change;
|
|
2173
|
+
* `408` and `429` are the two that mean "not now" rather than "not ever", so those
|
|
2174
|
+
* stay retryable along with transport failures and 5xx.
|
|
2175
|
+
* @param error - the refusal the settlement answered with.
|
|
2176
|
+
* @returns whether this settlement can never land.
|
|
2177
|
+
*/
|
|
2178
|
+
function isSettlementFinal(error) {
|
|
2179
|
+
if (!(error instanceof HubError)) return false;
|
|
2180
|
+
if (error.code === "LEASE_EXPIRED") return true;
|
|
2181
|
+
if (error.status < 400 || error.status >= 500) return false;
|
|
2182
|
+
if (error.status === 401 || error.code === "HTTP_" + String(error.status)) return false;
|
|
2183
|
+
return error.status !== 408 && error.status !== 429;
|
|
2184
|
+
}
|
|
2185
|
+
/**
|
|
2186
|
+
* Whether one claim refusal means another attempt is already executing the issue.
|
|
2187
|
+
*
|
|
2188
|
+
* Two callers claiming the same issue is a race the hub resolves, not a fault:
|
|
2189
|
+
* the loser is told the lease belongs to another attempt (409 `LEASE_HELD_BY_OTHER`,
|
|
2190
|
+
* or the bare 409 of a claim that just lost). Recording that as a failure would
|
|
2191
|
+
* put a false error on a card that is running exactly as it should.
|
|
2192
|
+
* @param error - the refusal the claim answered with.
|
|
2193
|
+
* @returns whether this refusal is a lost race.
|
|
2194
|
+
*/
|
|
2195
|
+
function isClaimHeldElsewhere(error) {
|
|
2196
|
+
return error instanceof HubError && (error.status === 409 || error.code === "LEASE_HELD_BY_OTHER");
|
|
2197
|
+
}
|
|
2198
|
+
/** One error as the summary and the log carry it. */
|
|
2199
|
+
function describe(error) {
|
|
2200
|
+
if (error instanceof HubError) return error.code + ": " + error.message;
|
|
2201
|
+
if (error instanceof Error) return error.message;
|
|
2202
|
+
return String(error);
|
|
2203
|
+
}
|
|
2204
|
+
//#endregion
|
|
2205
|
+
//#region src/host/tools.ts
|
|
2206
|
+
/**
|
|
2207
|
+
* The one model-visible tool this extension contributes: `team_comment`.
|
|
2208
|
+
*
|
|
2209
|
+
* Comments are the agent's half of the collaboration surface — the reviewer
|
|
2210
|
+
* reads them on the issue, and a mention in one is delivered into the session.
|
|
2211
|
+
* The tool registers through the board's capability face, so it lives exactly as
|
|
2212
|
+
* long as the board runs this extension, and it accepts no shell text, path or
|
|
2213
|
+
* permission: a comment is prose and nothing else.
|
|
2214
|
+
*
|
|
2215
|
+
* @module dsh-task-board-team/host/tools
|
|
2216
|
+
*/
|
|
2217
|
+
function renderJson(_args, value) {
|
|
2218
|
+
return [{
|
|
2219
|
+
type: "text",
|
|
2220
|
+
text: JSON.stringify(value, null, 2)
|
|
2221
|
+
}];
|
|
2222
|
+
}
|
|
2223
|
+
function json(value) {
|
|
2224
|
+
return value;
|
|
2225
|
+
}
|
|
2226
|
+
/** Byte length of one comment body, as the hub measures its own cap. */
|
|
2227
|
+
function byteLength(text) {
|
|
2228
|
+
return new TextEncoder().encode(text).length;
|
|
2229
|
+
}
|
|
2230
|
+
/**
|
|
2231
|
+
* Build the comment tool.
|
|
2232
|
+
* @param service - the running sync service.
|
|
2233
|
+
* @returns the tool definition the board registers.
|
|
2234
|
+
*/
|
|
2235
|
+
function buildTeamTools(service) {
|
|
2236
|
+
return [defineTool({
|
|
2237
|
+
name: "team_comment",
|
|
2238
|
+
description: "Post one comment on the team issue behind a task board card. Use it to report progress, ask a question that does not need to block, or explain a decision to the human reviewer. Accepts taskId (the card you are working on) or issueId. The body is Markdown, must stay under the byte cap and must not carry secrets.",
|
|
2239
|
+
parameters: {
|
|
2240
|
+
taskId: {
|
|
2241
|
+
type: "string",
|
|
2242
|
+
description: "Task board card id that mirrors a team issue."
|
|
2243
|
+
},
|
|
2244
|
+
issueId: {
|
|
2245
|
+
type: "string",
|
|
2246
|
+
description: "Team issue id, when the card id is not known."
|
|
2247
|
+
},
|
|
2248
|
+
text: {
|
|
2249
|
+
type: "string",
|
|
2250
|
+
required: true,
|
|
2251
|
+
description: "Markdown comment body. Must be non-empty and under the byte cap."
|
|
2252
|
+
}
|
|
2253
|
+
},
|
|
2254
|
+
output: {
|
|
2255
|
+
schema: { type: "json" },
|
|
2256
|
+
render: renderJson
|
|
2257
|
+
},
|
|
2258
|
+
async execute(args) {
|
|
2259
|
+
const text = typeof args.text === "string" ? args.text : "";
|
|
2260
|
+
if (text.trim() === "") return json({
|
|
2261
|
+
ok: false,
|
|
2262
|
+
code: "text-required",
|
|
2263
|
+
message: "text must be a non-empty comment body"
|
|
2264
|
+
});
|
|
2265
|
+
if (byteLength(text) > 32768) return json({
|
|
2266
|
+
ok: false,
|
|
2267
|
+
code: "text-too-long",
|
|
2268
|
+
message: "the comment body exceeds the byte cap"
|
|
2269
|
+
});
|
|
2270
|
+
const taskId = typeof args.taskId === "string" && args.taskId !== "" ? args.taskId : void 0;
|
|
2271
|
+
const issueId = typeof args.issueId === "string" && args.issueId !== "" ? args.issueId : void 0;
|
|
2272
|
+
if (taskId === void 0 && issueId === void 0) return json({
|
|
2273
|
+
ok: false,
|
|
2274
|
+
code: "target-required",
|
|
2275
|
+
message: "name the card (taskId) or the issue (issueId) to comment on"
|
|
2276
|
+
});
|
|
2277
|
+
try {
|
|
2278
|
+
return json({
|
|
2279
|
+
ok: true,
|
|
2280
|
+
...await service.postComment({
|
|
2281
|
+
text,
|
|
2282
|
+
...taskId === void 0 ? {} : { taskId },
|
|
2283
|
+
...issueId === void 0 ? {} : { issueId }
|
|
2284
|
+
})
|
|
2285
|
+
});
|
|
2286
|
+
} catch (error) {
|
|
2287
|
+
return json({
|
|
2288
|
+
ok: false,
|
|
2289
|
+
code: "comment-refused",
|
|
2290
|
+
message: error instanceof Error ? error.message : String(error)
|
|
2291
|
+
});
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
})];
|
|
2295
|
+
}
|
|
2296
|
+
//#endregion
|
|
2297
|
+
//#region src/host/extension.ts
|
|
2298
|
+
/**
|
|
2299
|
+
* The team provider assembled as one task-board extension.
|
|
2300
|
+
*
|
|
2301
|
+
* Everything team-specific lives behind this factory: the board admits it
|
|
2302
|
+
* through the cordis `taskBoard` service, hands it the capability face, and
|
|
2303
|
+
* never learns what the provider does with it. The factory imports no board
|
|
2304
|
+
* internals — only the same-shape contract in `../core/contract.ts` — and it
|
|
2305
|
+
* probes the two optional capabilities instead of assuming them, so a board
|
|
2306
|
+
* revision without follow-up or rework loses those deliveries and reports the
|
|
2307
|
+
* loss rather than failing to mount.
|
|
2308
|
+
*
|
|
2309
|
+
* @module dsh-task-board-team/host/extension
|
|
2310
|
+
*/
|
|
2311
|
+
/** One identifier per run action the board's applier expects. */
|
|
2312
|
+
function requestId() {
|
|
2313
|
+
return "team-" + Math.random().toString(36).slice(2) + "-" + Date.now().toString(36);
|
|
2314
|
+
}
|
|
2315
|
+
/**
|
|
2316
|
+
* Build the team provider extension.
|
|
2317
|
+
* @param options - deployment configuration for the integration.
|
|
2318
|
+
* @returns the extension the board registers.
|
|
2319
|
+
*/
|
|
2320
|
+
function createTeamExtension(options) {
|
|
2321
|
+
let service;
|
|
2322
|
+
let capabilities = {
|
|
2323
|
+
followUp: false,
|
|
2324
|
+
rework: false,
|
|
2325
|
+
run: false
|
|
2326
|
+
};
|
|
2327
|
+
const disposers = [];
|
|
2328
|
+
const log = options.log ?? ((message, detail) => {
|
|
2329
|
+
if (detail === void 0) console.error("[dsh-task-board-team] " + message);
|
|
2330
|
+
else console.error("[dsh-task-board-team] " + message, detail);
|
|
2331
|
+
});
|
|
2332
|
+
const start = (host) => {
|
|
2333
|
+
capabilities = probeTaskBoardCapabilities(host, options.actionSeat?.() === true);
|
|
2334
|
+
const live = new TeamSyncService({
|
|
2335
|
+
host: options.configuration === void 0 ? host : {
|
|
2336
|
+
...host,
|
|
2337
|
+
publish: (summary) => {
|
|
2338
|
+
host.publish({
|
|
2339
|
+
...summary,
|
|
2340
|
+
configuration: options.configuration?.() ?? {}
|
|
2341
|
+
});
|
|
2342
|
+
}
|
|
2343
|
+
},
|
|
2344
|
+
hub: options.hub,
|
|
2345
|
+
settings: options.settings,
|
|
2346
|
+
resolveAgents: options.resolveAgents,
|
|
2347
|
+
connect: options.connect,
|
|
2348
|
+
capabilities,
|
|
2349
|
+
timers: options.timers ?? processTimers,
|
|
2350
|
+
now: options.now ?? (() => Date.now()),
|
|
2351
|
+
uuid: options.uuid ?? requestId,
|
|
2352
|
+
log,
|
|
2353
|
+
...options.requestRun === void 0 ? {} : { requestRun: options.requestRun }
|
|
2354
|
+
});
|
|
2355
|
+
service = live;
|
|
2356
|
+
options.onService?.(live);
|
|
2357
|
+
disposers.push(host.events.onExecutionSettled((event) => {
|
|
2358
|
+
live.handleExecutionSettled(event).catch((error) => {
|
|
2359
|
+
log("settlement write-back failed", error);
|
|
2360
|
+
});
|
|
2361
|
+
}));
|
|
2362
|
+
for (const tool of buildTeamTools(live)) try {
|
|
2363
|
+
disposers.push(host.registerTool(tool));
|
|
2364
|
+
} catch (error) {
|
|
2365
|
+
log("team_comment could not register", error);
|
|
2366
|
+
}
|
|
2367
|
+
try {
|
|
2368
|
+
host.publish(live.summary());
|
|
2369
|
+
} catch (error) {
|
|
2370
|
+
log("initial summary was refused", error);
|
|
2371
|
+
}
|
|
2372
|
+
live.start().catch((error) => {
|
|
2373
|
+
log("provider start failed", error);
|
|
2374
|
+
});
|
|
2375
|
+
};
|
|
2376
|
+
return {
|
|
2377
|
+
id: TEAM_EXTENSION_ID,
|
|
2378
|
+
apiVersion: 1,
|
|
2379
|
+
enabled: () => options.enabled?.() ?? true,
|
|
2380
|
+
start,
|
|
2381
|
+
stop() {
|
|
2382
|
+
for (const dispose of disposers.splice(0)) try {
|
|
2383
|
+
dispose();
|
|
2384
|
+
} catch {}
|
|
2385
|
+
options.onService?.(void 0);
|
|
2386
|
+
service?.stop();
|
|
2387
|
+
service = void 0;
|
|
2388
|
+
},
|
|
2389
|
+
async handleAction(request) {
|
|
2390
|
+
const live = service;
|
|
2391
|
+
if (live === void 0) throw new Error("the team extension is not running");
|
|
2392
|
+
switch (request.action) {
|
|
2393
|
+
case "set-token": {
|
|
2394
|
+
const setup = options.setup;
|
|
2395
|
+
if (setup === void 0) throw new Error("this mount serves no configuration surface");
|
|
2396
|
+
const value = typeof request.payload?.token === "string" ? request.payload.token : "";
|
|
2397
|
+
if (value.trim() === "") throw new Error("the Host Token must be a non-empty value");
|
|
2398
|
+
await setup.saveToken(value);
|
|
2399
|
+
return {
|
|
2400
|
+
ok: true,
|
|
2401
|
+
status: await setup.status()
|
|
2402
|
+
};
|
|
2403
|
+
}
|
|
2404
|
+
case "clear-token": {
|
|
2405
|
+
const setup = options.setup;
|
|
2406
|
+
if (setup === void 0) throw new Error("this mount serves no configuration surface");
|
|
2407
|
+
await setup.clearToken();
|
|
2408
|
+
return {
|
|
2409
|
+
ok: true,
|
|
2410
|
+
status: await setup.status()
|
|
2411
|
+
};
|
|
2412
|
+
}
|
|
2413
|
+
case "setup-status": {
|
|
2414
|
+
const setup = options.setup;
|
|
2415
|
+
if (setup === void 0) throw new Error("this mount serves no configuration surface");
|
|
2416
|
+
return {
|
|
2417
|
+
ok: true,
|
|
2418
|
+
status: await setup.status()
|
|
2419
|
+
};
|
|
2420
|
+
}
|
|
2421
|
+
case "setup-save": {
|
|
2422
|
+
const setup = options.setup;
|
|
2423
|
+
if (setup === void 0) throw new Error("this mount serves no configuration surface");
|
|
2424
|
+
const payload = request.payload ?? {};
|
|
2425
|
+
const patch = {};
|
|
2426
|
+
if (typeof payload.hubUrl === "string") patch.hubUrl = payload.hubUrl;
|
|
2427
|
+
if (typeof payload.teamId === "string") patch.teamId = payload.teamId;
|
|
2428
|
+
if (typeof payload.hostname === "string") patch.hostname = payload.hostname;
|
|
2429
|
+
if (typeof payload.questionWaitMinutes === "number" && Number.isFinite(payload.questionWaitMinutes)) patch.questionWaitMinutes = payload.questionWaitMinutes;
|
|
2430
|
+
if (Array.isArray(payload.agents)) patch.agents = payload.agents.flatMap((entry) => {
|
|
2431
|
+
if (typeof entry !== "object" || entry === null) return [];
|
|
2432
|
+
const row = entry;
|
|
2433
|
+
if (typeof row.memberId !== "string") return [];
|
|
2434
|
+
return [{
|
|
2435
|
+
memberId: row.memberId,
|
|
2436
|
+
...typeof row.name === "string" ? { name: row.name } : {}
|
|
2437
|
+
}];
|
|
2438
|
+
});
|
|
2439
|
+
return {
|
|
2440
|
+
ok: true,
|
|
2441
|
+
status: await setup.save(patch)
|
|
2442
|
+
};
|
|
2443
|
+
}
|
|
2444
|
+
case "refresh": return {
|
|
2445
|
+
ok: true,
|
|
2446
|
+
claimed: await live.sweep()
|
|
2447
|
+
};
|
|
2448
|
+
case "comment": {
|
|
2449
|
+
const text = typeof request.payload?.text === "string" ? request.payload.text : "";
|
|
2450
|
+
if (text.trim() === "") throw new Error("a comment needs non-empty text");
|
|
2451
|
+
return {
|
|
2452
|
+
ok: true,
|
|
2453
|
+
...await live.postComment({
|
|
2454
|
+
text,
|
|
2455
|
+
...request.taskId === void 0 ? {} : { taskId: request.taskId }
|
|
2456
|
+
})
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
default: throw new Error("unknown team action \"" + request.action + "\"");
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
};
|
|
2463
|
+
}
|
|
2464
|
+
//#endregion
|
|
2465
|
+
//#region src/host/setup.ts
|
|
2466
|
+
/** Settings entry ids this package's rows carry, most specific first. */
|
|
2467
|
+
const TEAM_ENTRY_IDS = [
|
|
2468
|
+
"web-ui-task-board-team",
|
|
2469
|
+
"ui-task-board-team",
|
|
2470
|
+
"task-board-team"
|
|
2471
|
+
];
|
|
2472
|
+
/**
|
|
2473
|
+
* Resolve the optional settings service.
|
|
2474
|
+
* @param ctx - host context.
|
|
2475
|
+
* @returns the surface, or undefined when this deployment serves none.
|
|
2476
|
+
*/
|
|
2477
|
+
function resolveSettingsFace(ctx) {
|
|
2478
|
+
try {
|
|
2479
|
+
const get = ctx.get;
|
|
2480
|
+
if (typeof get !== "function") return void 0;
|
|
2481
|
+
const face = get.call(ctx, "settings");
|
|
2482
|
+
if (face === void 0) return void 0;
|
|
2483
|
+
return typeof face.describe === "function" && typeof face.mutate === "function" ? face : void 0;
|
|
2484
|
+
} catch {
|
|
2485
|
+
return;
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
/**
|
|
2489
|
+
* Find this extension's own entry in the settings surface.
|
|
2490
|
+
* @param face - the settings surface.
|
|
2491
|
+
* @returns the entry id and its current revision, or undefined.
|
|
2492
|
+
*/
|
|
2493
|
+
function findTeamSettingsEntry(face) {
|
|
2494
|
+
let descriptors;
|
|
2495
|
+
try {
|
|
2496
|
+
descriptors = face.describe({ redactSecrets: true });
|
|
2497
|
+
} catch {
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
for (const wanted of TEAM_ENTRY_IDS) {
|
|
2501
|
+
const hit = descriptors.find((descriptor) => descriptor.ns === wanted);
|
|
2502
|
+
if (hit !== void 0) return {
|
|
2503
|
+
ns: wanted,
|
|
2504
|
+
revision: typeof hit.revision === "number" ? hit.revision : 0
|
|
2505
|
+
};
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
/** The configuration surface one mount owns. */
|
|
2509
|
+
var TeamSetup = class {
|
|
2510
|
+
#options;
|
|
2511
|
+
/** Last known credential facts; refreshed on mount and after every write. */
|
|
2512
|
+
#token;
|
|
2513
|
+
/** Called after any write so the live mount can republish its summary. */
|
|
2514
|
+
onWrite;
|
|
2515
|
+
constructor(options) {
|
|
2516
|
+
this.#options = options;
|
|
2517
|
+
}
|
|
2518
|
+
/** Whether a Host Token is currently configured, as of the last refresh. */
|
|
2519
|
+
tokenConfigured() {
|
|
2520
|
+
return this.#token?.configured === true;
|
|
2521
|
+
}
|
|
2522
|
+
/** Re-read the credential facts without exposing the value. */
|
|
2523
|
+
async refreshToken() {
|
|
2524
|
+
const current = this.#options.current();
|
|
2525
|
+
this.#token = await describeHostToken(this.#options.ctx, current.tokenRef);
|
|
2526
|
+
}
|
|
2527
|
+
/**
|
|
2528
|
+
* Store one pasted Host Token.
|
|
2529
|
+
*
|
|
2530
|
+
* This is the only path a token value travels: the browser sends it once, the
|
|
2531
|
+
* host half stores it in the credential seam, and nothing ever reads it back
|
|
2532
|
+
* out to a client.
|
|
2533
|
+
* @param value - the token the operator pasted.
|
|
2534
|
+
*/
|
|
2535
|
+
async saveToken(value) {
|
|
2536
|
+
const trimmed = value.trim();
|
|
2537
|
+
if (trimmed === "") throw new Error("the Host Token must be a non-empty value");
|
|
2538
|
+
const current = this.#options.current();
|
|
2539
|
+
await setHostToken(this.#options.ctx, current.tokenRef, trimmed);
|
|
2540
|
+
await this.refreshToken();
|
|
2541
|
+
this.onWrite?.();
|
|
2542
|
+
}
|
|
2543
|
+
/** Remove the stored Host Token. */
|
|
2544
|
+
async clearToken() {
|
|
2545
|
+
const current = this.#options.current();
|
|
2546
|
+
await clearHostToken(this.#options.ctx, current.tokenRef);
|
|
2547
|
+
await this.refreshToken();
|
|
2548
|
+
this.onWrite?.();
|
|
2549
|
+
}
|
|
2550
|
+
/** Read the current configuration status. */
|
|
2551
|
+
async status() {
|
|
2552
|
+
const current = this.#options.current();
|
|
2553
|
+
const token = await describeHostToken(this.#options.ctx, current.tokenRef);
|
|
2554
|
+
this.#token = token;
|
|
2555
|
+
return {
|
|
2556
|
+
hubUrl: current.hubUrl,
|
|
2557
|
+
teamId: current.teamId,
|
|
2558
|
+
agents: current.agents.map((binding) => ({ ...binding })),
|
|
2559
|
+
hostname: current.hostname,
|
|
2560
|
+
questionWaitMinutes: current.questionWaitMinutes ?? 0,
|
|
2561
|
+
token,
|
|
2562
|
+
writable: resolveSettingsFace(this.#options.ctx)?.writable !== false,
|
|
2563
|
+
...this.#options.provider() === void 0 ? {} : { provider: this.#options.provider() }
|
|
2564
|
+
};
|
|
2565
|
+
}
|
|
2566
|
+
/**
|
|
2567
|
+
* Store the hub URL, the team id, the member bindings or the hostname.
|
|
2568
|
+
*
|
|
2569
|
+
* Every field is written in one batch against the revision just read, so a
|
|
2570
|
+
* hub URL and the team it belongs to cannot be stored split by a concurrent
|
|
2571
|
+
* edit, and the Loader commits the result into the running mount without a
|
|
2572
|
+
* restart.
|
|
2573
|
+
* @param patch - the fields to replace.
|
|
2574
|
+
* @returns the status after the write.
|
|
2575
|
+
*/
|
|
2576
|
+
async save(patch) {
|
|
2577
|
+
const operations = [];
|
|
2578
|
+
if (patch.hubUrl !== void 0) operations.push({
|
|
2579
|
+
op: "set",
|
|
2580
|
+
path: ["hubUrl"],
|
|
2581
|
+
value: normalizeHubUrl(patch.hubUrl)
|
|
2582
|
+
});
|
|
2583
|
+
if (patch.teamId !== void 0) operations.push({
|
|
2584
|
+
op: "set",
|
|
2585
|
+
path: ["teamId"],
|
|
2586
|
+
value: patch.teamId.trim()
|
|
2587
|
+
});
|
|
2588
|
+
if (patch.hostname !== void 0) operations.push({
|
|
2589
|
+
op: "set",
|
|
2590
|
+
path: ["hostname"],
|
|
2591
|
+
value: patch.hostname.trim()
|
|
2592
|
+
});
|
|
2593
|
+
if (patch.agents !== void 0) operations.push({
|
|
2594
|
+
op: "set",
|
|
2595
|
+
path: ["agents"],
|
|
2596
|
+
value: normalizeBindings(patch.agents)
|
|
2597
|
+
});
|
|
2598
|
+
if (patch.questionWaitMinutes !== void 0) operations.push({
|
|
2599
|
+
op: "set",
|
|
2600
|
+
path: ["questionWaitMinutes"],
|
|
2601
|
+
value: Math.max(0, Math.trunc(patch.questionWaitMinutes))
|
|
2602
|
+
});
|
|
2603
|
+
if (operations.length > 0) {
|
|
2604
|
+
const face = resolveSettingsFace(this.#options.ctx);
|
|
2605
|
+
if (face === void 0) throw new Error("this deployment serves no settings surface, so the team settings cannot be written");
|
|
2606
|
+
if (face.writable === false) throw new Error("this deployment serves configuration read-only");
|
|
2607
|
+
const entry = findTeamSettingsEntry(face);
|
|
2608
|
+
if (entry === void 0) throw new Error("this deployment exposes no settings form for the team extension");
|
|
2609
|
+
await face.mutate(entry.ns, operations, entry.revision);
|
|
2610
|
+
}
|
|
2611
|
+
this.#options.reload?.();
|
|
2612
|
+
this.onWrite?.();
|
|
2613
|
+
return await this.status();
|
|
2614
|
+
}
|
|
2615
|
+
};
|
|
2616
|
+
/**
|
|
2617
|
+
* Normalize one hub URL to an absolute http(s) base without a trailing slash.
|
|
2618
|
+
* @param raw - the value the operator typed.
|
|
2619
|
+
* @returns the normalized URL.
|
|
2620
|
+
* @throws when the value is not a usable absolute URL.
|
|
2621
|
+
*/
|
|
2622
|
+
function normalizeHubUrl(raw) {
|
|
2623
|
+
const trimmed = raw.trim().replace(/\/+$/, "");
|
|
2624
|
+
if (trimmed === "") return "";
|
|
2625
|
+
let url;
|
|
2626
|
+
try {
|
|
2627
|
+
url = new URL(trimmed);
|
|
2628
|
+
} catch {
|
|
2629
|
+
throw new Error("the hub URL must be an absolute http(s) URL");
|
|
2630
|
+
}
|
|
2631
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") throw new Error("the hub URL must be an absolute http(s) URL");
|
|
2632
|
+
return trimmed;
|
|
2633
|
+
}
|
|
2634
|
+
/**
|
|
2635
|
+
* Keep only bindings that name a member, dropping duplicates.
|
|
2636
|
+
* @param raw - the bindings one caller proposed.
|
|
2637
|
+
* @returns the normalized list.
|
|
2638
|
+
*/
|
|
2639
|
+
function normalizeBindings(raw) {
|
|
2640
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2641
|
+
const result = [];
|
|
2642
|
+
for (const binding of raw) {
|
|
2643
|
+
const memberId = typeof binding?.memberId === "string" ? binding.memberId.trim() : "";
|
|
2644
|
+
if (memberId === "" || seen.has(memberId)) continue;
|
|
2645
|
+
seen.add(memberId);
|
|
2646
|
+
const name = typeof binding.name === "string" ? binding.name.trim() : "";
|
|
2647
|
+
result.push({
|
|
2648
|
+
memberId,
|
|
2649
|
+
...name === "" ? {} : { name }
|
|
2650
|
+
});
|
|
2651
|
+
}
|
|
2652
|
+
return result;
|
|
2653
|
+
}
|
|
2654
|
+
//#endregion
|
|
2655
|
+
//#region src/index.ts
|
|
2656
|
+
/** npm identity shared by every install source of this package. */
|
|
2657
|
+
const PACKAGE_NAME = "@linxin666/dsh-client-ui-task-board-team";
|
|
2658
|
+
/** Order of this extension's announcement section, just after the board's own. */
|
|
2659
|
+
const SECTION_ORDER = 260;
|
|
2660
|
+
/** The tool this extension registers, named in its announcement. */
|
|
2661
|
+
const TEAM_TOOL_NAME = "team_comment";
|
|
2662
|
+
/** Model-facing announcement: what the extension does and the words that name it. */
|
|
2663
|
+
const TEAM_GUIDANCE = "本机已安装 dsh-task-board-team 扩展(DSH Web GUI 任务看板的团队协作提供方):把云端 team-hub 上指派给本机 dsh-agents 成员的 issue 领取为看板镜像卡并在本机执行,执行结算写回 hub,执行会话里的 ask_user_question 会转给团队审阅人回答;另注册 team_comment 工具供模型发表评论,评论里 @ 到本机 Agent 时会作为后续消息投递进会话。工具权限审批一律只在本机处理,绝不进入 hub。用户提到「团队任务 / team-hub / 领取团队 issue / 给团队留言」时即指本扩展,请据此协作。";
|
|
2664
|
+
/** The single-instance guard key; two install sources must not double register. */
|
|
2665
|
+
const MOUNTED_KEY = Symbol.for("dsh-task-board-team:mounted");
|
|
2666
|
+
/** Schema default of the announcement switch. */
|
|
2667
|
+
const DEFAULT_ANNOUNCE_TO_AGENT = false;
|
|
2668
|
+
/** Schema default of the Host Token reference. */
|
|
2669
|
+
const DEFAULT_HOST_TOKEN_REF = DEFAULT_TOKEN_REF;
|
|
2670
|
+
const AgentBindingSchema = z.object({
|
|
2671
|
+
memberId: z.string(),
|
|
2672
|
+
name: z.string().default("")
|
|
2673
|
+
});
|
|
2674
|
+
const Config = z.object({
|
|
2675
|
+
enabled: z.boolean().default(true).volatile(),
|
|
2676
|
+
announceToAgent: z.boolean().default(false).volatile(),
|
|
2677
|
+
hubUrl: z.string().default("").volatile(),
|
|
2678
|
+
teamId: z.string().default("").volatile(),
|
|
2679
|
+
workspaceId: z.string().default("").volatile(),
|
|
2680
|
+
hostTokenRef: z.string().default(DEFAULT_TOKEN_REF).volatile(),
|
|
2681
|
+
agents: z.array(AgentBindingSchema).default([]).volatile(),
|
|
2682
|
+
hostname: z.string().default("").volatile(),
|
|
2683
|
+
questionWaitMinutes: z.number().default(0).volatile()
|
|
2684
|
+
});
|
|
2685
|
+
/**
|
|
2686
|
+
* Read one config field's live value.
|
|
2687
|
+
* @param field - the field as the Loader handed it.
|
|
2688
|
+
* @param fallback - value when the field is absent.
|
|
2689
|
+
* @returns the effective value.
|
|
2690
|
+
*/
|
|
2691
|
+
function readConfigField(field, fallback) {
|
|
2692
|
+
if (field === void 0) return fallback;
|
|
2693
|
+
if (typeof field === "object" && field !== null && typeof field.get === "function") return field.get();
|
|
2694
|
+
return field;
|
|
2695
|
+
}
|
|
2696
|
+
/**
|
|
2697
|
+
* Resolve the effective settings of one mount.
|
|
2698
|
+
* @param config - the row's config as the Host handed it.
|
|
2699
|
+
* @param hostname - platform hostname fallback.
|
|
2700
|
+
* @returns the settings the registration consumes.
|
|
2701
|
+
*/
|
|
2702
|
+
function resolveProviderSettings(config, hostname = "") {
|
|
2703
|
+
const bindings = readConfigField(config?.agents, []).map((entry) => ({
|
|
2704
|
+
memberId: typeof entry?.memberId === "string" ? entry.memberId : "",
|
|
2705
|
+
...typeof entry?.name === "string" && entry.name !== "" ? { name: entry.name } : {}
|
|
2706
|
+
}));
|
|
2707
|
+
const configured = readConfigField(config?.hostname, "").trim();
|
|
2708
|
+
return {
|
|
2709
|
+
enabled: readConfigField(config?.enabled, true),
|
|
2710
|
+
announceToAgent: readConfigField(config?.announceToAgent, false),
|
|
2711
|
+
hubUrl: readConfigField(config?.hubUrl, "").trim(),
|
|
2712
|
+
teamId: readConfigField(config?.teamId, "").trim(),
|
|
2713
|
+
workspaceId: readConfigField(config?.workspaceId, "").trim(),
|
|
2714
|
+
hostTokenRef: readConfigField(config?.hostTokenRef, DEFAULT_TOKEN_REF).trim(),
|
|
2715
|
+
agents: normalizeBindings(bindings),
|
|
2716
|
+
hostname: configured === "" ? hostname : configured,
|
|
2717
|
+
questionWaitMinutes: Math.max(0, Math.trunc(readConfigField(config?.questionWaitMinutes, 0)))
|
|
2718
|
+
};
|
|
2719
|
+
}
|
|
2720
|
+
/**
|
|
2721
|
+
* Resolve an optional service by name.
|
|
2722
|
+
* @param ctx - host context.
|
|
2723
|
+
* @param name - service name.
|
|
2724
|
+
* @returns the service, or undefined when this deployment serves none.
|
|
2725
|
+
*/
|
|
2726
|
+
function resolveOptional(ctx, name) {
|
|
2727
|
+
try {
|
|
2728
|
+
const get = ctx.get;
|
|
2729
|
+
if (typeof get !== "function") return void 0;
|
|
2730
|
+
return get.call(ctx, name);
|
|
2731
|
+
} catch {
|
|
2732
|
+
return;
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
/**
|
|
2736
|
+
* Resolve the announcement text, or undefined when the tool it names is not
|
|
2737
|
+
* reachable: guidance for a tool the session cannot see only wastes context.
|
|
2738
|
+
* @param ctx - host context.
|
|
2739
|
+
* @returns the text to publish, or undefined.
|
|
2740
|
+
*/
|
|
2741
|
+
function resolveAnnouncement(ctx) {
|
|
2742
|
+
const tools = resolveOptional(ctx, "tools");
|
|
2743
|
+
if (tools === void 0 || typeof tools.get !== "function") return void 0;
|
|
2744
|
+
try {
|
|
2745
|
+
return tools.get("team_comment") === void 0 ? void 0 : TEAM_GUIDANCE;
|
|
2746
|
+
} catch {
|
|
2747
|
+
return;
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Mount the plugin once per Host process, whichever install source loaded it.
|
|
2752
|
+
* @param key - the process-wide guard key.
|
|
2753
|
+
* @param apply - the real entry.
|
|
2754
|
+
* @returns the guarded entry.
|
|
2755
|
+
*/
|
|
2756
|
+
function mountOnce(key, apply) {
|
|
2757
|
+
return ((ctx, config) => {
|
|
2758
|
+
const global = globalThis;
|
|
2759
|
+
if (global[key] === true) return;
|
|
2760
|
+
global[key] = true;
|
|
2761
|
+
try {
|
|
2762
|
+
apply(ctx, config);
|
|
2763
|
+
} catch (error) {
|
|
2764
|
+
global[key] = false;
|
|
2765
|
+
throw error;
|
|
2766
|
+
}
|
|
2767
|
+
});
|
|
2768
|
+
}
|
|
2769
|
+
/**
|
|
2770
|
+
* Resolve the local agents the configured bindings name.
|
|
2771
|
+
* @param settings - live settings of this mount.
|
|
2772
|
+
* @returns the resolved agents and one problem line per unusable binding.
|
|
2773
|
+
*/
|
|
2774
|
+
async function resolveBoundAgents(settings) {
|
|
2775
|
+
const view = await readAgentsLedger(agentsLedgerPath());
|
|
2776
|
+
const resolved = resolveBindings(settings.agents, view);
|
|
2777
|
+
return {
|
|
2778
|
+
agents: resolved.agents,
|
|
2779
|
+
problems: [...view.problem === void 0 ? [] : [view.problem], ...resolved.problems]
|
|
2780
|
+
};
|
|
2781
|
+
}
|
|
2782
|
+
const apply = mountOnce(MOUNTED_KEY, applyImpl);
|
|
2783
|
+
/**
|
|
2784
|
+
* Activate the extension's host half.
|
|
2785
|
+
*
|
|
2786
|
+
* The provider is admitted through the board's own registration service, so it
|
|
2787
|
+
* follows the board's master switch as well as this extension's. Registration
|
|
2788
|
+
* lives behind a cordis dependency scope (ctx.inject) because the Host loads
|
|
2789
|
+
* plugin rows in an order this package does not own: the board's service may
|
|
2790
|
+
* genuinely not exist yet when this row activates, and the scope mounts when it
|
|
2791
|
+
* appears and unloads when it is withdrawn.
|
|
2792
|
+
* @param ctx - the plugin context.
|
|
2793
|
+
* @param config - resolved plugin config.
|
|
2794
|
+
*/
|
|
2795
|
+
function applyImpl(ctx, config) {
|
|
2796
|
+
const fallbackHostname = platformHostname();
|
|
2797
|
+
const settings = () => resolveProviderSettings(config, fallbackHostname);
|
|
2798
|
+
let injection;
|
|
2799
|
+
let mounted;
|
|
2800
|
+
let liveService;
|
|
2801
|
+
let disposeSection;
|
|
2802
|
+
let announceLive = false;
|
|
2803
|
+
let disposed = false;
|
|
2804
|
+
const provider = () => liveService?.summary();
|
|
2805
|
+
const setup = new TeamSetup({
|
|
2806
|
+
ctx,
|
|
2807
|
+
current: () => {
|
|
2808
|
+
const current = settings();
|
|
2809
|
+
return {
|
|
2810
|
+
hubUrl: current.hubUrl,
|
|
2811
|
+
teamId: current.teamId,
|
|
2812
|
+
agents: current.agents,
|
|
2813
|
+
hostname: current.hostname,
|
|
2814
|
+
tokenRef: current.hostTokenRef,
|
|
2815
|
+
questionWaitMinutes: current.questionWaitMinutes
|
|
2816
|
+
};
|
|
2817
|
+
},
|
|
2818
|
+
provider,
|
|
2819
|
+
reload: () => {
|
|
2820
|
+
requestSync();
|
|
2821
|
+
}
|
|
2822
|
+
});
|
|
2823
|
+
/** The socket factory: the token is resolved per connection, so a rotation works. */
|
|
2824
|
+
const connect = async (sinceSeq, handlers) => {
|
|
2825
|
+
const current = settings();
|
|
2826
|
+
if (current.hubUrl === "" || current.teamId === "") return void 0;
|
|
2827
|
+
const token = await resolveHostToken(ctx, current.hostTokenRef);
|
|
2828
|
+
if (token === void 0) return void 0;
|
|
2829
|
+
return platformSocket(teamSocketUrl(current.hubUrl, current.teamId, sinceSeq), handlers, teamSocketHeaders(token));
|
|
2830
|
+
};
|
|
2831
|
+
/** Ask the board to run one card through its own action applier. */
|
|
2832
|
+
const requestRun = (taskId) => {
|
|
2833
|
+
const seat = resolveTaskBoardActionSeat(ctx);
|
|
2834
|
+
if (seat === void 0) throw new Error("this board revision serves no action applier, so a claimed card cannot be started");
|
|
2835
|
+
seat.apply("team-run-" + taskId + "-" + String(Date.now()), {
|
|
2836
|
+
kind: "run",
|
|
2837
|
+
taskId
|
|
2838
|
+
}, "team-hub");
|
|
2839
|
+
};
|
|
2840
|
+
setup.onWrite = () => {
|
|
2841
|
+
liveService?.publishNow();
|
|
2842
|
+
};
|
|
2843
|
+
const teardownProvider = () => {
|
|
2844
|
+
const current = injection;
|
|
2845
|
+
injection = void 0;
|
|
2846
|
+
mounted = void 0;
|
|
2847
|
+
if (current !== void 0) current.dispose();
|
|
2848
|
+
};
|
|
2849
|
+
const sync = async () => {
|
|
2850
|
+
if (disposed) return;
|
|
2851
|
+
const next = settings();
|
|
2852
|
+
const wantAnnounce = next.enabled && next.announceToAgent;
|
|
2853
|
+
if (wantAnnounce !== announceLive) {
|
|
2854
|
+
announceLive = wantAnnounce;
|
|
2855
|
+
try {
|
|
2856
|
+
disposeSection?.();
|
|
2857
|
+
} catch {}
|
|
2858
|
+
disposeSection = void 0;
|
|
2859
|
+
if (wantAnnounce) {
|
|
2860
|
+
const systemPrompt = resolveOptional(ctx, "systemPrompt");
|
|
2861
|
+
const text = resolveAnnouncement(ctx);
|
|
2862
|
+
if (systemPrompt !== void 0 && text !== void 0) try {
|
|
2863
|
+
disposeSection = systemPrompt.section({
|
|
2864
|
+
name: "plugin:task-board-team",
|
|
2865
|
+
order: SECTION_ORDER,
|
|
2866
|
+
text
|
|
2867
|
+
});
|
|
2868
|
+
} catch {}
|
|
2869
|
+
}
|
|
2870
|
+
}
|
|
2871
|
+
if (!next.enabled) {
|
|
2872
|
+
teardownProvider();
|
|
2873
|
+
return;
|
|
2874
|
+
}
|
|
2875
|
+
const desired = JSON.stringify({
|
|
2876
|
+
hubUrl: next.hubUrl,
|
|
2877
|
+
teamId: next.teamId,
|
|
2878
|
+
hostTokenRef: next.hostTokenRef,
|
|
2879
|
+
agents: next.agents,
|
|
2880
|
+
hostname: next.hostname
|
|
2881
|
+
});
|
|
2882
|
+
if (desired === mounted) return;
|
|
2883
|
+
teardownProvider();
|
|
2884
|
+
mounted = desired;
|
|
2885
|
+
injection = ctx.inject(["taskBoard"], (scope) => {
|
|
2886
|
+
scope.effect(() => {
|
|
2887
|
+
const face = resolveTaskBoardHostFace(scope);
|
|
2888
|
+
if (face === void 0) {
|
|
2889
|
+
console.error("[dsh-task-board-team] the taskBoard service does not answer the registration contract");
|
|
2890
|
+
return () => {};
|
|
2891
|
+
}
|
|
2892
|
+
setup.refreshToken().then(() => {
|
|
2893
|
+
liveService?.publishNow();
|
|
2894
|
+
}).catch(() => {});
|
|
2895
|
+
const dispose = face.registerExtension(createTeamExtension({
|
|
2896
|
+
hub: new HubClient({
|
|
2897
|
+
hubUrl: () => settings().hubUrl,
|
|
2898
|
+
teamId: () => settings().teamId,
|
|
2899
|
+
token: async () => await resolveHostToken(ctx, settings().hostTokenRef)
|
|
2900
|
+
}),
|
|
2901
|
+
settings: () => ({
|
|
2902
|
+
enabled: settings().enabled,
|
|
2903
|
+
hostname: settings().hostname,
|
|
2904
|
+
bindings: settings().agents,
|
|
2905
|
+
workspaceId: settings().workspaceId,
|
|
2906
|
+
questionWaitMinutes: settings().questionWaitMinutes
|
|
2907
|
+
}),
|
|
2908
|
+
resolveAgents: async () => await resolveBoundAgents(settings()),
|
|
2909
|
+
connect,
|
|
2910
|
+
requestRun,
|
|
2911
|
+
actionSeat: () => resolveTaskBoardActionSeat(scope) !== void 0,
|
|
2912
|
+
configuration: () => ({
|
|
2913
|
+
hubUrl: settings().hubUrl,
|
|
2914
|
+
teamId: settings().teamId,
|
|
2915
|
+
hostname: settings().hostname,
|
|
2916
|
+
tokenConfigured: setup.tokenConfigured(),
|
|
2917
|
+
questionWaitMinutes: settings().questionWaitMinutes
|
|
2918
|
+
}),
|
|
2919
|
+
timers: processTimers,
|
|
2920
|
+
enabled: () => settings().enabled,
|
|
2921
|
+
setup,
|
|
2922
|
+
onService: (service) => {
|
|
2923
|
+
liveService = service;
|
|
2924
|
+
},
|
|
2925
|
+
log: (message, detail) => {
|
|
2926
|
+
if (detail === void 0) console.error("[dsh-task-board-team] " + message);
|
|
2927
|
+
else console.error("[dsh-task-board-team] " + message, detail);
|
|
2928
|
+
}
|
|
2929
|
+
}));
|
|
2930
|
+
return () => {
|
|
2931
|
+
liveService = void 0;
|
|
2932
|
+
dispose();
|
|
2933
|
+
};
|
|
2934
|
+
}, "task-board-team: provider registration");
|
|
2935
|
+
});
|
|
2936
|
+
};
|
|
2937
|
+
/** Serialize sync requests: a config write and a volatile commit can arrive together. */
|
|
2938
|
+
let chain = Promise.resolve();
|
|
2939
|
+
const requestSync = () => {
|
|
2940
|
+
chain = chain.then(() => sync()).catch((error) => {
|
|
2941
|
+
console.error("[dsh-task-board-team] sync failed", error);
|
|
2942
|
+
});
|
|
2943
|
+
};
|
|
2944
|
+
ctx.on("loader/volatile-update", () => {
|
|
2945
|
+
requestSync();
|
|
2946
|
+
});
|
|
2947
|
+
ctx.on("credentials/reference-updated", () => {
|
|
2948
|
+
requestSync();
|
|
2949
|
+
});
|
|
2950
|
+
installTeamWaterfall(ctx, () => liveService?.questions);
|
|
2951
|
+
ctx.effect(() => {
|
|
2952
|
+
requestSync();
|
|
2953
|
+
return () => {
|
|
2954
|
+
disposed = true;
|
|
2955
|
+
teardownProvider();
|
|
2956
|
+
try {
|
|
2957
|
+
disposeSection?.();
|
|
2958
|
+
} catch {}
|
|
2959
|
+
disposeSection = void 0;
|
|
2960
|
+
};
|
|
2961
|
+
}, "task-board-team: provider lifecycle");
|
|
2962
|
+
}
|
|
2963
|
+
/** The platform hostname this Host registers under. */
|
|
2964
|
+
function platformHostname() {
|
|
2965
|
+
return process.env.HOSTNAME ?? process.env.COMPUTERNAME ?? "dsh-host";
|
|
2966
|
+
}
|
|
2967
|
+
//#endregion
|
|
2968
|
+
export { Config, DEFAULT_ANNOUNCE_TO_AGENT, DEFAULT_HOST_TOKEN_REF, PACKAGE_NAME, TEAM_GUIDANCE, TEAM_TOOL_NAME, apply, mountOnce, readConfigField, resolveAnnouncement, resolveProviderSettings };
|