dsh-task-cat 0.0.0-stage → 1.0.0
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/README.md +96 -2
- package/client.js +1486 -0
- package/cordis.patch.yml +3 -0
- package/desktop.js +559 -0
- package/icon.svg +15 -0
- package/index.js +561 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/overlay/TaskPetOverlay.swift +1566 -0
- package/package.json +50 -4
package/index.js
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-task-pet — host half.
|
|
3
|
+
*
|
|
4
|
+
* Registers one session-projection unit, `taskPet`, that folds each Session's
|
|
5
|
+
* committed log into the compact progress facts a floating pet needs: the open
|
|
6
|
+
* turn/step, the current todo list, the active goal, the last tool, and how the
|
|
7
|
+
* previous turn ended.
|
|
8
|
+
*
|
|
9
|
+
* The unit is a normal `summary` projection, so every Session list row carries
|
|
10
|
+
* its view (`projectionValues.taskPet`), and live sessions also receive per-event
|
|
11
|
+
* change frames. The Client half renders the pet and jumps into the conversation
|
|
12
|
+
* that needs attention.
|
|
13
|
+
*
|
|
14
|
+
* This half also owns the desktop overlay: the page reports its roster over one
|
|
15
|
+
* guarded loopback route, a native helper draws the screen-wide pet, and the
|
|
16
|
+
* helper's click intents come back to the page through a small polled queue. See
|
|
17
|
+
* `./desktop.js` for why that surface lives outside the window.
|
|
18
|
+
*
|
|
19
|
+
* The projection registry only requires `.parse()` on `stateSchema` and
|
|
20
|
+
* `viewSchema`; this bundle ships no zod dependency, so the two validators below
|
|
21
|
+
* are hand-written normalisers. They never reject an object: a stale or partially
|
|
22
|
+
* written checkpoint is repaired in place instead of breaking the event drive.
|
|
23
|
+
*
|
|
24
|
+
* @module dsh-task-pet
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { createDesktopPet } from './desktop.js';
|
|
28
|
+
|
|
29
|
+
/** Todo statuses the `todo_write` tool records. */
|
|
30
|
+
const TODO_STATUSES = new Set(['pending', 'in_progress', 'completed']);
|
|
31
|
+
|
|
32
|
+
/** Turn endings the pet reports as a failure rather than a clean finish. */
|
|
33
|
+
const ENDING_KINDS = new Set(['completed', 'blocked', 'aborted', 'error', 'max-tokens']);
|
|
34
|
+
|
|
35
|
+
/** Goal phases `@deepseek-ai/dsh-goal` records. */
|
|
36
|
+
const GOAL_PHASES = new Set(['active', 'paused', 'blocked', 'complete']);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether one value is a plain object.
|
|
40
|
+
* @param value - candidate value.
|
|
41
|
+
* @returns true for a non-null, non-array object.
|
|
42
|
+
*/
|
|
43
|
+
function isRecord(value) {
|
|
44
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Coerce one counter to a non-negative integer.
|
|
49
|
+
* @param value - candidate value.
|
|
50
|
+
* @param fallback - value used when the candidate is unusable.
|
|
51
|
+
* @returns a safe non-negative integer.
|
|
52
|
+
*/
|
|
53
|
+
function countOf(value, fallback = 0) {
|
|
54
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.floor(value) : fallback;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Coerce an optional timestamp.
|
|
59
|
+
* @param value - candidate value.
|
|
60
|
+
* @returns a non-negative number, or null when absent.
|
|
61
|
+
*/
|
|
62
|
+
function timeOf(value) {
|
|
63
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Normalise one todo list from a checkpoint row.
|
|
68
|
+
* @param value - candidate list.
|
|
69
|
+
* @returns a clean list, or null when the value is not a list.
|
|
70
|
+
*/
|
|
71
|
+
function normalizeTodos(value) {
|
|
72
|
+
if (!Array.isArray(value)) return null;
|
|
73
|
+
const todos = [];
|
|
74
|
+
for (const entry of value) {
|
|
75
|
+
if (!isRecord(entry) || typeof entry.content !== 'string') continue;
|
|
76
|
+
const status = TODO_STATUSES.has(entry.status) ? entry.status : 'pending';
|
|
77
|
+
todos.push({ content: entry.content, status });
|
|
78
|
+
}
|
|
79
|
+
return todos;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Normalise the active goal from a checkpoint row.
|
|
84
|
+
* @param value - candidate goal.
|
|
85
|
+
* @returns `{ phase, objective }`, or null.
|
|
86
|
+
*/
|
|
87
|
+
function normalizeGoal(value) {
|
|
88
|
+
if (!isRecord(value) || typeof value.objective !== 'string') return null;
|
|
89
|
+
return {
|
|
90
|
+
objective: value.objective,
|
|
91
|
+
phase: GOAL_PHASES.has(value.phase) ? value.phase : 'active',
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Build the projection's initial state.
|
|
97
|
+
* @returns a fresh fold state.
|
|
98
|
+
*/
|
|
99
|
+
function initialState() {
|
|
100
|
+
return {
|
|
101
|
+
turn: 0,
|
|
102
|
+
step: 0,
|
|
103
|
+
openTurn: false,
|
|
104
|
+
startedAt: null,
|
|
105
|
+
updatedAt: 0,
|
|
106
|
+
ending: null,
|
|
107
|
+
todos: null,
|
|
108
|
+
goal: null,
|
|
109
|
+
tools: 0,
|
|
110
|
+
lastTool: null,
|
|
111
|
+
lastToolError: false,
|
|
112
|
+
promptAt: 0,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Validate and repair one stored fold state.
|
|
118
|
+
* @param value - candidate state from a checkpoint row or `init`.
|
|
119
|
+
* @returns a usable state object.
|
|
120
|
+
*/
|
|
121
|
+
function normalizeState(value) {
|
|
122
|
+
if (!isRecord(value)) return initialState();
|
|
123
|
+
return {
|
|
124
|
+
turn: countOf(value.turn),
|
|
125
|
+
step: countOf(value.step),
|
|
126
|
+
openTurn: value.openTurn === true,
|
|
127
|
+
startedAt: timeOf(value.startedAt),
|
|
128
|
+
updatedAt: countOf(value.updatedAt),
|
|
129
|
+
ending: ENDING_KINDS.has(value.ending) ? value.ending : null,
|
|
130
|
+
todos: normalizeTodos(value.todos),
|
|
131
|
+
goal: normalizeGoal(value.goal),
|
|
132
|
+
tools: countOf(value.tools),
|
|
133
|
+
lastTool: typeof value.lastTool === 'string' ? value.lastTool : null,
|
|
134
|
+
lastToolError: value.lastToolError === true,
|
|
135
|
+
promptAt: countOf(value.promptAt),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Fold one committed Session event into the pet state.
|
|
141
|
+
* @param state - state before the event.
|
|
142
|
+
* @param event - the committed Session event.
|
|
143
|
+
* @returns the same state reference when the event is unrelated.
|
|
144
|
+
*/
|
|
145
|
+
function applyEvent(state, event) {
|
|
146
|
+
switch (event.type) {
|
|
147
|
+
case 'turn/start': {
|
|
148
|
+
const turn = countOf(event.data?.turn, state.turn);
|
|
149
|
+
return {
|
|
150
|
+
...state,
|
|
151
|
+
turn,
|
|
152
|
+
step: 0,
|
|
153
|
+
openTurn: true,
|
|
154
|
+
startedAt: event.time,
|
|
155
|
+
updatedAt: event.time,
|
|
156
|
+
ending: null,
|
|
157
|
+
tools: 0,
|
|
158
|
+
lastTool: null,
|
|
159
|
+
lastToolError: false,
|
|
160
|
+
// A new turn starts with a fresh list; the todo unit resets on the same event.
|
|
161
|
+
todos: null,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
case 'step/start': {
|
|
165
|
+
const step = countOf(event.data?.step, state.step);
|
|
166
|
+
if (step === state.step) return state;
|
|
167
|
+
return { ...state, step, updatedAt: event.time };
|
|
168
|
+
}
|
|
169
|
+
case 'turn/end': {
|
|
170
|
+
const kind = ENDING_KINDS.has(event.data?.reason?.kind) ? event.data.reason.kind : 'completed';
|
|
171
|
+
return {
|
|
172
|
+
...state,
|
|
173
|
+
openTurn: false,
|
|
174
|
+
tools: 0,
|
|
175
|
+
ending: kind,
|
|
176
|
+
updatedAt: event.time,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
case 'todo/write': {
|
|
180
|
+
const todos = normalizeTodos(event.data?.todos);
|
|
181
|
+
return { ...state, todos, updatedAt: event.time };
|
|
182
|
+
}
|
|
183
|
+
case 'goal/change': {
|
|
184
|
+
const goal = normalizeGoal(event.data?.goal);
|
|
185
|
+
return { ...state, goal, updatedAt: event.time };
|
|
186
|
+
}
|
|
187
|
+
case 'tool/call': {
|
|
188
|
+
return {
|
|
189
|
+
...state,
|
|
190
|
+
tools: state.tools + 1,
|
|
191
|
+
lastTool: typeof event.data?.name === 'string' ? event.data.name : state.lastTool,
|
|
192
|
+
lastToolError: false,
|
|
193
|
+
updatedAt: event.time,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
case 'tool/result': {
|
|
197
|
+
return {
|
|
198
|
+
...state,
|
|
199
|
+
tools: Math.max(0, state.tools - 1),
|
|
200
|
+
lastToolError: event.data?.message?.isError === true,
|
|
201
|
+
updatedAt: event.time,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
case 'user/message': {
|
|
205
|
+
if (event.data?.source?.kind !== 'user') return state;
|
|
206
|
+
return { ...state, promptAt: event.time, updatedAt: event.time };
|
|
207
|
+
}
|
|
208
|
+
default: return state;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Repair one client-visible pet view.
|
|
214
|
+
* @param value - candidate wire value.
|
|
215
|
+
* @returns a view with every field present and well-typed.
|
|
216
|
+
*/
|
|
217
|
+
function normalizeView(value) {
|
|
218
|
+
const view = isRecord(value) ? value : {};
|
|
219
|
+
return {
|
|
220
|
+
turn: countOf(view.turn),
|
|
221
|
+
step: countOf(view.step),
|
|
222
|
+
active: view.active === true,
|
|
223
|
+
startedAt: timeOf(view.startedAt),
|
|
224
|
+
updatedAt: countOf(view.updatedAt),
|
|
225
|
+
ending: ENDING_KINDS.has(view.ending) ? view.ending : null,
|
|
226
|
+
todos: isRecord(view.todos) && countOf(view.todos.total) > 0
|
|
227
|
+
? {
|
|
228
|
+
total: countOf(view.todos.total),
|
|
229
|
+
completed: countOf(view.todos.completed),
|
|
230
|
+
inProgress: countOf(view.todos.inProgress),
|
|
231
|
+
pending: countOf(view.todos.pending),
|
|
232
|
+
current: typeof view.todos.current === 'string' ? view.todos.current : null,
|
|
233
|
+
next: typeof view.todos.next === 'string' ? view.todos.next : null,
|
|
234
|
+
}
|
|
235
|
+
: null,
|
|
236
|
+
goal: isRecord(view.goal) && typeof view.goal.objective === 'string'
|
|
237
|
+
? { objective: view.goal.objective, phase: GOAL_PHASES.has(view.goal.phase) ? view.goal.phase : 'active' }
|
|
238
|
+
: null,
|
|
239
|
+
lastTool: typeof view.lastTool === 'string' ? view.lastTool : null,
|
|
240
|
+
lastToolError: view.lastToolError === true,
|
|
241
|
+
promptAt: countOf(view.promptAt),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Project the fold state into the client-visible pet view.
|
|
247
|
+
* @param state - current fold state.
|
|
248
|
+
* @returns the compact wire value.
|
|
249
|
+
*/
|
|
250
|
+
function viewOf(state) {
|
|
251
|
+
let todos = null;
|
|
252
|
+
if (Array.isArray(state.todos) && state.todos.length > 0) {
|
|
253
|
+
let completed = 0;
|
|
254
|
+
let inProgress = 0;
|
|
255
|
+
let pending = 0;
|
|
256
|
+
let current = null;
|
|
257
|
+
let next = null;
|
|
258
|
+
for (const todo of state.todos) {
|
|
259
|
+
if (todo.status === 'completed') completed += 1;
|
|
260
|
+
else if (todo.status === 'in_progress') {
|
|
261
|
+
inProgress += 1;
|
|
262
|
+
if (current === null) current = todo.content;
|
|
263
|
+
} else {
|
|
264
|
+
pending += 1;
|
|
265
|
+
if (next === null) next = todo.content;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
todos = {
|
|
269
|
+
total: state.todos.length,
|
|
270
|
+
completed,
|
|
271
|
+
inProgress,
|
|
272
|
+
pending,
|
|
273
|
+
current,
|
|
274
|
+
next,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
turn: state.turn,
|
|
279
|
+
step: state.step,
|
|
280
|
+
active: state.openTurn,
|
|
281
|
+
startedAt: state.startedAt,
|
|
282
|
+
updatedAt: state.updatedAt,
|
|
283
|
+
ending: state.ending,
|
|
284
|
+
todos,
|
|
285
|
+
goal: state.goal === null ? null : { objective: state.goal.objective, phase: state.goal.phase },
|
|
286
|
+
lastTool: state.lastTool,
|
|
287
|
+
lastToolError: state.lastToolError,
|
|
288
|
+
promptAt: state.promptAt,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A permissive schema face: the registry only calls `parse`.
|
|
294
|
+
* @param normalize - value repair function.
|
|
295
|
+
* @returns the schema object.
|
|
296
|
+
*/
|
|
297
|
+
function schemaOf(normalize) {
|
|
298
|
+
return {
|
|
299
|
+
parse(value) {
|
|
300
|
+
return normalize(value);
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** One unit key of our own; sharing another key would merge fold semantics. */
|
|
306
|
+
const PROJECTION_KEY = 'taskPet';
|
|
307
|
+
|
|
308
|
+
/** Guarded loopback route the page uses to drive the desktop overlay. */
|
|
309
|
+
const OVERLAY_PATH = '/dsh-task-pet/overlay';
|
|
310
|
+
|
|
311
|
+
/** Largest state report one page may push. */
|
|
312
|
+
const OVERLAY_BODY_LIMIT = 32768;
|
|
313
|
+
|
|
314
|
+
/** How many click intents are held for a page that has not polled yet. */
|
|
315
|
+
const INTENT_QUEUE_LIMIT = 16;
|
|
316
|
+
|
|
317
|
+
/** How long one poll waits for a click before answering empty. */
|
|
318
|
+
const OVERLAY_POLL_MS = 20000;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Inside the projection set a caller can ask for: they are the only kinds the
|
|
322
|
+
* roster and the overlay understand.
|
|
323
|
+
*/
|
|
324
|
+
const KINDS = new Set(['running', 'attention', 'progress', 'failed', 'finished', 'idle']);
|
|
325
|
+
|
|
326
|
+
/** Shape of a language tag the helper may adopt. */
|
|
327
|
+
const LOCALE_TAG = /^[a-z]{2,3}(?:[-_][A-Za-z0-9]{2,8})?$/;
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* One guarded body read. `OVERLAY_BODY_LIMIT` keeps a hostile page from parking
|
|
331
|
+
* data in memory, and a non-JSON body is a client bug, not a server fault.
|
|
332
|
+
* @param request - incoming node request.
|
|
333
|
+
* @returns the parsed object.
|
|
334
|
+
*/
|
|
335
|
+
async function readBody(request) {
|
|
336
|
+
const chunks = [];
|
|
337
|
+
let size = 0;
|
|
338
|
+
for await (const chunk of request) {
|
|
339
|
+
size += chunk.length;
|
|
340
|
+
if (size > OVERLAY_BODY_LIMIT) throw new Error('request body too large');
|
|
341
|
+
chunks.push(chunk);
|
|
342
|
+
}
|
|
343
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
344
|
+
if (text.trim() === '') return {};
|
|
345
|
+
const parsed = JSON.parse(text);
|
|
346
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) throw new Error('body must be an object');
|
|
347
|
+
return parsed;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Send one JSON response.
|
|
352
|
+
* @param response - node response.
|
|
353
|
+
* @param status - HTTP status code.
|
|
354
|
+
* @param value - JSON-serializable payload.
|
|
355
|
+
*/
|
|
356
|
+
function reply(response, status, value) {
|
|
357
|
+
response.writeHead(status, {
|
|
358
|
+
'cache-control': 'no-store',
|
|
359
|
+
'content-type': 'application/json; charset=utf-8',
|
|
360
|
+
});
|
|
361
|
+
response.end(JSON.stringify(value));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Whether this request may drive the desktop overlay: loopback only, no
|
|
366
|
+
* forwarding header, and inside the Harness connection fence (Host/Origin plus
|
|
367
|
+
* the browser authentication cookie).
|
|
368
|
+
* @param ctx - Host context carrying `connection`.
|
|
369
|
+
* @param request - incoming node request.
|
|
370
|
+
* @returns true when the caller is the local page.
|
|
371
|
+
*/
|
|
372
|
+
function admitted(ctx, request) {
|
|
373
|
+
const address = request.socket.remoteAddress;
|
|
374
|
+
if (address !== '127.0.0.1' && address !== '::1' && address !== '::ffff:127.0.0.1') return false;
|
|
375
|
+
if (request.headers.forwarded !== undefined) return false;
|
|
376
|
+
if (request.headers['x-forwarded-for'] !== undefined) return false;
|
|
377
|
+
if (request.headers['x-real-ip'] !== undefined) return false;
|
|
378
|
+
const connection = ctx.get('connection');
|
|
379
|
+
return connection === undefined || connection.requestRejection(request) === undefined;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Reduce one page report to exactly the facts the overlay renders. The page is
|
|
384
|
+
* this bundle's own Client half, but a route is a trust boundary: nothing is
|
|
385
|
+
* forwarded that has not been re-typed here.
|
|
386
|
+
* @param body - parsed report.
|
|
387
|
+
* @returns the overlay state, or undefined when it is not a state report.
|
|
388
|
+
*/
|
|
389
|
+
function overlayState(body) {
|
|
390
|
+
const mood = new Set(['working', 'alert', 'idle', 'sleeping']);
|
|
391
|
+
const count = (value) => (Number.isFinite(value) && value > 0 ? Math.min(999, Math.round(value)) : 0);
|
|
392
|
+
const whole = (value) => (Number.isFinite(value) ? Math.max(0, Math.min(9999, Math.round(value))) : 0);
|
|
393
|
+
const sessions = [];
|
|
394
|
+
if (Array.isArray(body.sessions)) {
|
|
395
|
+
for (const row of body.sessions.slice(0, 40)) {
|
|
396
|
+
if (typeof row !== 'object' || row === null) continue;
|
|
397
|
+
if (typeof row.id !== 'string' || typeof row.title !== 'string') continue;
|
|
398
|
+
if (typeof row.kind !== 'string' || !KINDS.has(row.kind)) continue;
|
|
399
|
+
sessions.push({
|
|
400
|
+
id: row.id.slice(0, 128),
|
|
401
|
+
title: row.title.slice(0, 200),
|
|
402
|
+
kind: row.kind,
|
|
403
|
+
// A badge the page already localized, so the card never invents copy.
|
|
404
|
+
currentLabel: typeof row.currentLabel === 'string' ? row.currentLabel.slice(0, 40) : '',
|
|
405
|
+
active: row.active === true,
|
|
406
|
+
// The page composes the one-line status (elapsed, todos, current tool)
|
|
407
|
+
// with the dictionary it already loaded, so both surfaces say the same
|
|
408
|
+
// thing in the same words.
|
|
409
|
+
meta: typeof row.meta === 'string' ? row.meta.slice(0, 120) : '',
|
|
410
|
+
done: whole(row.done),
|
|
411
|
+
total: whole(row.total),
|
|
412
|
+
current: row.current === true,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
mood: typeof body.mood === 'string' && mood.has(body.mood) ? body.mood : 'idle',
|
|
418
|
+
badge: count(body.badge),
|
|
419
|
+
attention: count(body.attention),
|
|
420
|
+
running: count(body.running),
|
|
421
|
+
summary: typeof body.summary === 'string' ? body.summary.slice(0, 240) : '',
|
|
422
|
+
// The helper's own menus (open/hide/more) follow the app, not the shell's
|
|
423
|
+
// `LANG`, which is often `C.UTF-8` on macOS.
|
|
424
|
+
locale: typeof body.locale === 'string' && LOCALE_TAG.test(body.locale) ? body.locale.slice(0, 16) : '',
|
|
425
|
+
// null means "the page could not tell"; the helper then keeps whatever it had.
|
|
426
|
+
light: typeof body.light === 'boolean' ? body.light : null,
|
|
427
|
+
sessions,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Host plugin body: register the `taskPet` session projection and serve the
|
|
433
|
+
* desktop overlay's small control surface.
|
|
434
|
+
* @param ctx - Host context carrying the projection registry and the web server.
|
|
435
|
+
*/
|
|
436
|
+
export async function apply(ctx) {
|
|
437
|
+
await ctx.sessionProjections.register({
|
|
438
|
+
key: PROJECTION_KEY,
|
|
439
|
+
stateVersion: 1,
|
|
440
|
+
stateSchema: schemaOf(normalizeState),
|
|
441
|
+
init: () => initialState(),
|
|
442
|
+
apply: applyEvent,
|
|
443
|
+
wire: {
|
|
444
|
+
viewSchema: schemaOf(normalizeView),
|
|
445
|
+
view: viewOf,
|
|
446
|
+
// Every Session list row carries the pet's progress block, so the Client
|
|
447
|
+
// needs no per-session request to show background progress.
|
|
448
|
+
summary: true,
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
const pet = createDesktopPet(ctx);
|
|
453
|
+
/** Clicks the helper reported while no page was polling. */
|
|
454
|
+
let intents = [];
|
|
455
|
+
/** Pages waiting for the next click; a long poll keeps the loop idle. */
|
|
456
|
+
const waiters = new Set();
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Hand out queued intents. Called when one arrives and when a poll starts, so
|
|
460
|
+
* a page that subscribes late still receives what the helper reported.
|
|
461
|
+
* @returns the intents that were pending.
|
|
462
|
+
*/
|
|
463
|
+
function takeIntents() {
|
|
464
|
+
const delivered = intents;
|
|
465
|
+
intents = [];
|
|
466
|
+
return delivered;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
pet.subscribe((event, data) => {
|
|
470
|
+
let next;
|
|
471
|
+
if (event === 'jump' && typeof data === 'object' && data !== null && typeof data.id === 'string') {
|
|
472
|
+
next = { type: 'jump', id: data.id };
|
|
473
|
+
} else if (event === 'activate') {
|
|
474
|
+
next = { type: 'activate' };
|
|
475
|
+
} else if (event === 'status') {
|
|
476
|
+
// A surface change (detach, crash, restart) must reach the page at once:
|
|
477
|
+
// the long poll may otherwise hold it for its whole budget.
|
|
478
|
+
for (const wake of [...waiters]) wake();
|
|
479
|
+
return;
|
|
480
|
+
} else {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
intents = [...intents, next].slice(-INTENT_QUEUE_LIMIT);
|
|
484
|
+
for (const wake of [...waiters]) wake();
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
ctx.effect(() => {
|
|
488
|
+
void pet.start();
|
|
489
|
+
return () => {
|
|
490
|
+
void pet.stop();
|
|
491
|
+
};
|
|
492
|
+
}, 'dsh-task-pet: desktop overlay');
|
|
493
|
+
|
|
494
|
+
// `inject` waits for the carrier instead of sampling it once: a Host that is
|
|
495
|
+
// still booting when this plugin loads must not end up with a pet but no route.
|
|
496
|
+
ctx.inject(['webServer'], (child) => {
|
|
497
|
+
child.effect(() => child.webServer.register({
|
|
498
|
+
kind: 'exact',
|
|
499
|
+
path: OVERLAY_PATH,
|
|
500
|
+
handler(request, response) {
|
|
501
|
+
if (!admitted(child, request)) {
|
|
502
|
+
reply(response, 403, { ok: false, error: 'the desktop overlay is limited to the local app' });
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (request.method === 'GET') {
|
|
506
|
+
// Long poll: answer at once when something is queued, otherwise hold
|
|
507
|
+
// the request until a click lands or the wait budget runs out. The
|
|
508
|
+
// page then reconnects, so the loop costs one request per idle window
|
|
509
|
+
// instead of one per second.
|
|
510
|
+
const finish = () => {
|
|
511
|
+
clearTimeout(timer);
|
|
512
|
+
waiters.delete(wake);
|
|
513
|
+
if (response.writableEnded) return;
|
|
514
|
+
reply(response, 200, { ok: true, ...pet.status(), intents: takeIntents() });
|
|
515
|
+
};
|
|
516
|
+
const wake = () => { finish(); };
|
|
517
|
+
const timer = setTimeout(finish, OVERLAY_POLL_MS);
|
|
518
|
+
waiters.add(wake);
|
|
519
|
+
response.on('close', () => {
|
|
520
|
+
clearTimeout(timer);
|
|
521
|
+
waiters.delete(wake);
|
|
522
|
+
});
|
|
523
|
+
if (intents.length > 0) finish();
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (request.method !== 'POST') {
|
|
527
|
+
reply(response, 405, { ok: false, error: 'method not allowed' });
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
void (async () => {
|
|
531
|
+
try {
|
|
532
|
+
const body = await readBody(request);
|
|
533
|
+
if (body.action === 'enable' || body.action === 'disable') {
|
|
534
|
+
await pet.setEnabled(body.action === 'enable');
|
|
535
|
+
reply(response, 200, { ok: true, ...pet.status() });
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (body.state !== undefined) {
|
|
539
|
+
pet.push(overlayState(body.state));
|
|
540
|
+
reply(response, 200, { ok: true, ...pet.status() });
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
reply(response, 400, { ok: false, error: 'expected action or state' });
|
|
544
|
+
} catch (error) {
|
|
545
|
+
reply(response, 400, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
546
|
+
}
|
|
547
|
+
})();
|
|
548
|
+
},
|
|
549
|
+
}), 'dsh-task-pet: overlay route');
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export const name = 'dsh-task-pet';
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Only the projection table is a hard dependency. The HTTP carrier and the
|
|
557
|
+
* connection fence are read through `ctx.get` because a Host composition
|
|
558
|
+
* without a browser still gets the pet's projection, and waiting for a service
|
|
559
|
+
* that will never load would park this plugin forever.
|
|
560
|
+
*/
|
|
561
|
+
export const inject = ['sessionProjections'];
|
package/locale/en.json
ADDED