dsh-bark-push 0.2.1 → 0.3.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/CHANGELOG.md +17 -0
- package/README.md +49 -12
- package/cordis.patch.yml +12 -0
- package/docs/browser-notifications-design.md +691 -0
- package/docs/settings-0.3.0.png +0 -0
- package/lib/browser-notify.js +396 -0
- package/lib/client.js +255 -11
- package/lib/config.js +51 -0
- package/lib/events.js +21 -5
- package/lib/http.js +303 -65
- package/lib/index.js +25 -5
- package/lib/state.js +51 -7
- package/lib/ui.js +864 -185
- package/package.json +6 -4
package/lib/http.js
CHANGED
|
@@ -23,6 +23,7 @@ export const ROUTES = Object.freeze({
|
|
|
23
23
|
toggle: '/api/bark-push/toggle',
|
|
24
24
|
test: '/api/bark-push/test',
|
|
25
25
|
testChannel: '/api/bark-push/test-channel',
|
|
26
|
+
browserEvents: '/api/bark-push/browser-events',
|
|
26
27
|
});
|
|
27
28
|
|
|
28
29
|
/** Maximum accepted JSON request body. */
|
|
@@ -87,6 +88,17 @@ function muteTarget(kind, id) {
|
|
|
87
88
|
return { kind, id: trimmed };
|
|
88
89
|
}
|
|
89
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Normalize one optional mute channel. Unknown values are rejected rather than
|
|
93
|
+
* silently writing to the wrong mute set.
|
|
94
|
+
* @param {unknown} value - raw channel.
|
|
95
|
+
* @returns {'push'|'browser'|null} normalized channel.
|
|
96
|
+
*/
|
|
97
|
+
function muteChannel(value) {
|
|
98
|
+
if (value === undefined || value === null) return 'push';
|
|
99
|
+
return value === 'push' || value === 'browser' ? value : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
90
102
|
/**
|
|
91
103
|
* Read one optional Cordis service without an inject requirement.
|
|
92
104
|
* @param {object} ctx - host context.
|
|
@@ -105,102 +117,310 @@ function serviceOf(ctx, key) {
|
|
|
105
117
|
}
|
|
106
118
|
}
|
|
107
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Record a best-effort catalog read failure. The management page must still
|
|
122
|
+
* render with whatever source is available.
|
|
123
|
+
* @param {object} ctx - host context.
|
|
124
|
+
* @param {string} message - diagnostic line.
|
|
125
|
+
* @returns {void}
|
|
126
|
+
*/
|
|
127
|
+
function warnCatalog(ctx, message) {
|
|
128
|
+
try {
|
|
129
|
+
ctx?.logger?.warn?.(message);
|
|
130
|
+
} catch {
|
|
131
|
+
// Diagnostics never break the catalog.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Normalize one Session summary from the Host's Session controller or the raw
|
|
137
|
+
* Session query corpus into the small row shape this page consumes.
|
|
138
|
+
* @param {unknown} item - summary-like object.
|
|
139
|
+
* @param {object} [fallbackHeader] - explicit header when the item is a Session.
|
|
140
|
+
* @returns {object|null} normalized row, or null without an id.
|
|
141
|
+
*/
|
|
142
|
+
function normalizeSessionRow(item, fallbackHeader) {
|
|
143
|
+
if (item === null || typeof item !== 'object') return null;
|
|
144
|
+
const header = fallbackHeader ?? (item.header !== null && typeof item.header === 'object' ? item.header : {});
|
|
145
|
+
const id = typeof item.sessionId === 'string'
|
|
146
|
+
? item.sessionId
|
|
147
|
+
: typeof item.id === 'string'
|
|
148
|
+
? item.id
|
|
149
|
+
: typeof header.id === 'string'
|
|
150
|
+
? header.id
|
|
151
|
+
: '';
|
|
152
|
+
if (id.length === 0) return null;
|
|
153
|
+
const projections = item.projections !== null && typeof item.projections === 'object' ? item.projections : undefined;
|
|
154
|
+
const values = projections && projections.values !== null && typeof projections.values === 'object' ? projections.values : undefined;
|
|
155
|
+
const projectedTitle = typeof values?.title === 'string' ? values.title : '';
|
|
156
|
+
const directTitle = typeof item.title === 'string' ? item.title : '';
|
|
157
|
+
const updatedAt = Number.isFinite(item.updatedAt)
|
|
158
|
+
? item.updatedAt
|
|
159
|
+
: Number.isFinite(header.createdAt)
|
|
160
|
+
? header.createdAt
|
|
161
|
+
: 0;
|
|
162
|
+
return {
|
|
163
|
+
id,
|
|
164
|
+
title: projectedTitle || directTitle,
|
|
165
|
+
updatedAt,
|
|
166
|
+
blank: item.blank === true,
|
|
167
|
+
origin: typeof item.origin === 'string' ? item.origin : typeof header.origin === 'string' ? header.origin : null,
|
|
168
|
+
cwd: typeof item.cwd === 'string' ? item.cwd : typeof header.cwd === 'string' ? header.cwd : '',
|
|
169
|
+
running: item.running === true,
|
|
170
|
+
agentAvailable: item.agentAvailable !== false,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Read Session summaries through the low-level query service and its durable
|
|
176
|
+
* projection cache. This is the fallback when the Web Session controller is
|
|
177
|
+
* absent; it mirrors the fields `ApiSessionList` builds for the sidebar.
|
|
178
|
+
* @param {object} ctx - host context.
|
|
179
|
+
* @param {AbortSignal} [signal] - request cancellation.
|
|
180
|
+
* @returns {Promise<Array<object>|null>} normalized rows, or null when unavailable.
|
|
181
|
+
*/
|
|
182
|
+
async function listViaSessionQuery(ctx, signal) {
|
|
183
|
+
const query = serviceOf(ctx, 'sessionQuery');
|
|
184
|
+
if (typeof query?.listSessions !== 'function') return null;
|
|
185
|
+
let records;
|
|
186
|
+
try {
|
|
187
|
+
records = await query.listSessions(signal);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
warnCatalog(ctx, `bark-push: 会话查询失败:${error?.message ?? String(error)}`);
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
if (!Array.isArray(records)) return null;
|
|
193
|
+
const cache = serviceOf(ctx, 'sessionProjectionCache');
|
|
194
|
+
const sessionsService = serviceOf(ctx, 'sessions');
|
|
195
|
+
const titles = serviceOf(ctx, 'sessionTitle');
|
|
196
|
+
const rows = [];
|
|
197
|
+
for (const record of records) {
|
|
198
|
+
const header = record?.header !== null && typeof record?.header === 'object' ? record.header : {};
|
|
199
|
+
let snapshot;
|
|
200
|
+
try {
|
|
201
|
+
snapshot = cache?.cachedSnapshot?.(header);
|
|
202
|
+
} catch {
|
|
203
|
+
snapshot = undefined;
|
|
204
|
+
}
|
|
205
|
+
let title = typeof snapshot?.values?.title === 'string' ? snapshot.values.title : '';
|
|
206
|
+
const live = typeof sessionsService?.get === 'function' ? sessionsService.get(header.id) : undefined;
|
|
207
|
+
if (title.length === 0 && live !== undefined) {
|
|
208
|
+
try {
|
|
209
|
+
title = titles?.get?.(live)?.title ?? '';
|
|
210
|
+
} catch {
|
|
211
|
+
title = '';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const row = normalizeSessionRow({
|
|
215
|
+
sessionId: header.id,
|
|
216
|
+
title,
|
|
217
|
+
updatedAt: header.createdAt,
|
|
218
|
+
blank: snapshot?.values?.sessionListMetadata?.blank === true,
|
|
219
|
+
origin: header.origin,
|
|
220
|
+
cwd: header.cwd,
|
|
221
|
+
running: false,
|
|
222
|
+
agentAvailable: live !== undefined,
|
|
223
|
+
projections: snapshot === undefined ? undefined : { values: snapshot.values },
|
|
224
|
+
});
|
|
225
|
+
if (row !== null) rows.push(row);
|
|
226
|
+
}
|
|
227
|
+
return rows;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Last-resort live Session enumeration. It cannot see cold sessions, but it
|
|
232
|
+
* still applies the sidebar's subagent/blank filters in buildCatalog.
|
|
233
|
+
* @param {object} ctx - host context.
|
|
234
|
+
* @returns {Array<object>} normalized rows.
|
|
235
|
+
*/
|
|
236
|
+
function listLiveSessions(ctx) {
|
|
237
|
+
const sessionsService = serviceOf(ctx, 'sessions');
|
|
238
|
+
const live = typeof sessionsService?.list === 'function' ? sessionsService.list() : [];
|
|
239
|
+
const titles = serviceOf(ctx, 'sessionTitle');
|
|
240
|
+
const rows = [];
|
|
241
|
+
for (const session of live) {
|
|
242
|
+
let title = '';
|
|
243
|
+
try {
|
|
244
|
+
title = titles?.get?.(session)?.title ?? session?.title ?? '';
|
|
245
|
+
} catch {
|
|
246
|
+
title = '';
|
|
247
|
+
}
|
|
248
|
+
const row = normalizeSessionRow(
|
|
249
|
+
{
|
|
250
|
+
sessionId: session?.header?.id ?? session?.id,
|
|
251
|
+
title,
|
|
252
|
+
updatedAt: session?.header?.createdAt,
|
|
253
|
+
origin: session?.header?.origin,
|
|
254
|
+
cwd: session?.header?.cwd,
|
|
255
|
+
running: false,
|
|
256
|
+
agentAvailable: true,
|
|
257
|
+
},
|
|
258
|
+
session?.header,
|
|
259
|
+
);
|
|
260
|
+
if (row !== null) rows.push(row);
|
|
261
|
+
}
|
|
262
|
+
return rows;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Read the canonical Session list. Prefer `sessionController.list`, the same
|
|
267
|
+
* visible summary feed the Web sidebar consumes (including cold titles and
|
|
268
|
+
* blank/origin metadata); fall back to the raw query engine, then to live
|
|
269
|
+
* Sessions only.
|
|
270
|
+
* @param {object} ctx - host context.
|
|
271
|
+
* @param {AbortSignal} [signal] - request cancellation.
|
|
272
|
+
* @returns {Promise<Array<object>>} normalized summaries.
|
|
273
|
+
*/
|
|
274
|
+
async function collectSessionSummaries(ctx, signal) {
|
|
275
|
+
const controller = serviceOf(ctx, 'sessionController');
|
|
276
|
+
if (typeof controller?.list === 'function') {
|
|
277
|
+
try {
|
|
278
|
+
const value = await controller.list({}, signal);
|
|
279
|
+
const items = Array.isArray(value?.items) ? value.items : Array.isArray(value) ? value : null;
|
|
280
|
+
if (items !== null) return items.map((item) => normalizeSessionRow(item)).filter((row) => row !== null);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
warnCatalog(ctx, `bark-push: 会话列表读取失败,改用底层查询:${error?.message ?? String(error)}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const queried = await listViaSessionQuery(ctx, signal);
|
|
286
|
+
if (queried !== null) return queried;
|
|
287
|
+
return listLiveSessions(ctx);
|
|
288
|
+
}
|
|
289
|
+
|
|
108
290
|
/**
|
|
109
291
|
* Build the catalog shown on the management page: every registered workspace
|
|
110
|
-
* and the
|
|
292
|
+
* and the Sessions visible in DSH's own sidebar, with current mute flags.
|
|
293
|
+
*
|
|
294
|
+
* Visibility follows `dsh-client-ui-workspace`: subagent Sessions, blank
|
|
295
|
+
* Sessions, and archived Sessions (the default archived filter) never appear.
|
|
296
|
+
* Muted Sessions that are now hidden remain as rows so their mute can still
|
|
297
|
+
* be cleared.
|
|
111
298
|
* @param {object} ctx - host context.
|
|
112
299
|
* @param {object} snapshot - state snapshot.
|
|
113
|
-
* @
|
|
300
|
+
* @param {AbortSignal} [signal] - request cancellation.
|
|
301
|
+
* @returns {Promise<{ workspaces: Array<object>, sessions: Array<object>, sessionWorkspace: Record<string, string> }>} catalog.
|
|
114
302
|
*/
|
|
115
|
-
function buildCatalog(ctx, snapshot) {
|
|
303
|
+
export async function buildCatalog(ctx, snapshot, signal) {
|
|
116
304
|
const mutedWorkspaces = new Set(snapshot.mutedWorkspaces);
|
|
117
305
|
const mutedSessions = new Set(snapshot.mutedSessions);
|
|
306
|
+
const allMutedSessions = new Set([
|
|
307
|
+
...mutedSessions,
|
|
308
|
+
...(Array.isArray(snapshot.browserMutedSessions) ? snapshot.browserMutedSessions : []),
|
|
309
|
+
]);
|
|
118
310
|
const workspaces = [];
|
|
119
311
|
/** Session id -> workspace id for every Session the registry knows. */
|
|
120
312
|
const workspaceBySession = new Map();
|
|
121
|
-
/** Workspace id ->
|
|
122
|
-
const
|
|
313
|
+
/** Workspace id -> display title. */
|
|
314
|
+
const workspaceTitleById = new Map();
|
|
315
|
+
let archivedSessionIds = new Set();
|
|
316
|
+
let pinnedSessionIds = [];
|
|
123
317
|
try {
|
|
124
318
|
const registry = serviceOf(ctx, 'workspaceRegistry');
|
|
125
319
|
const list = typeof registry?.list === 'function' ? registry.list() : [];
|
|
126
320
|
for (const workspace of list) {
|
|
127
321
|
const id = String(workspace.id);
|
|
128
322
|
const path = typeof workspace.path === 'string' ? workspace.path : '';
|
|
323
|
+
const title = typeof workspace.title === 'string' && workspace.title.length > 0 ? workspace.title : id;
|
|
129
324
|
workspaces.push({
|
|
130
325
|
id,
|
|
131
|
-
title
|
|
326
|
+
title,
|
|
132
327
|
path,
|
|
133
328
|
muted: mutedWorkspaces.has(id),
|
|
134
329
|
});
|
|
135
|
-
|
|
330
|
+
workspaceTitleById.set(id, title);
|
|
136
331
|
for (const sessionId of Array.isArray(workspace.sessionIds) ? workspace.sessionIds : []) {
|
|
137
332
|
workspaceBySession.set(String(sessionId), id);
|
|
138
333
|
}
|
|
139
334
|
}
|
|
335
|
+
if (Array.isArray(registry?.archivedSessionIds)) {
|
|
336
|
+
archivedSessionIds = new Set(registry.archivedSessionIds.map(String));
|
|
337
|
+
}
|
|
338
|
+
if (Array.isArray(registry?.pinnedSessionIds)) {
|
|
339
|
+
pinnedSessionIds = registry.pinnedSessionIds.map(String);
|
|
340
|
+
}
|
|
140
341
|
} catch {
|
|
141
342
|
// A missing or broken registry only means the page shows no projects.
|
|
142
343
|
}
|
|
143
344
|
|
|
144
|
-
|
|
145
|
-
const seen = new Set();
|
|
146
|
-
const liveRows = [];
|
|
345
|
+
let summaries = [];
|
|
147
346
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const id = String(session.header?.id ?? session.id ?? '');
|
|
152
|
-
if (id.length === 0) continue;
|
|
153
|
-
let title = '';
|
|
154
|
-
try {
|
|
155
|
-
title = serviceOf(ctx, 'sessionTitle')?.get?.(session)?.title ?? '';
|
|
156
|
-
} catch {
|
|
157
|
-
title = '';
|
|
158
|
-
}
|
|
159
|
-
liveRows.push({
|
|
160
|
-
id,
|
|
161
|
-
title: typeof title === 'string' ? title : '',
|
|
162
|
-
cwd: typeof session.header?.cwd === 'string' ? session.header.cwd : '',
|
|
163
|
-
workspaceId: workspaceBySession.get(id) ?? null,
|
|
164
|
-
origin: session.header?.origin ?? null,
|
|
165
|
-
createdAt: Number.isFinite(session.header?.createdAt) ? session.header.createdAt : 0,
|
|
166
|
-
muted: mutedSessions.has(id),
|
|
167
|
-
offline: false,
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
} catch {
|
|
171
|
-
// Live Session enumeration is optional.
|
|
347
|
+
summaries = await collectSessionSummaries(ctx, signal);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
warnCatalog(ctx, `bark-push: 会话目录构建失败:${error?.message ?? String(error)}`);
|
|
172
350
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
351
|
+
|
|
352
|
+
// The controller already carries titles, but a live title can be newer than
|
|
353
|
+
// the cached projection; fill any gap before rendering.
|
|
354
|
+
const sessionsService = serviceOf(ctx, 'sessions');
|
|
355
|
+
const titlesService = serviceOf(ctx, 'sessionTitle');
|
|
356
|
+
for (const row of summaries) {
|
|
357
|
+
if (row.title.length > 0) continue;
|
|
358
|
+
const live = typeof sessionsService?.get === 'function' ? sessionsService.get(row.id) : undefined;
|
|
359
|
+
if (live === undefined) continue;
|
|
360
|
+
try {
|
|
361
|
+
const title = titlesService?.get?.(live)?.title;
|
|
362
|
+
if (typeof title === 'string' && title.length > 0) row.title = title;
|
|
363
|
+
} catch {
|
|
364
|
+
// A title failure leaves the row unnamed.
|
|
365
|
+
}
|
|
177
366
|
}
|
|
178
367
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
368
|
+
const summaryById = new Map(summaries.map((row) => [row.id, row]));
|
|
369
|
+
const visible = summaries.filter((row) => row.blank !== true && row.origin !== 'subagent' && !archivedSessionIds.has(row.id));
|
|
370
|
+
const pinRank = new Map(pinnedSessionIds.map((id, index) => [id, index]));
|
|
371
|
+
visible.sort((left, right) => {
|
|
372
|
+
const leftPinned = pinRank.has(left.id);
|
|
373
|
+
const rightPinned = pinRank.has(right.id);
|
|
374
|
+
if (leftPinned && rightPinned) return pinRank.get(left.id) - pinRank.get(right.id);
|
|
375
|
+
if (leftPinned !== rightPinned) return leftPinned ? -1 : 1;
|
|
376
|
+
if (left.updatedAt !== right.updatedAt) return right.updatedAt - left.updatedAt;
|
|
377
|
+
return left.id.localeCompare(right.id);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
const sessions = [];
|
|
381
|
+
const seen = new Set();
|
|
382
|
+
for (const row of visible) {
|
|
383
|
+
const workspaceId = workspaceBySession.get(row.id) ?? null;
|
|
184
384
|
sessions.push({
|
|
185
|
-
id:
|
|
186
|
-
title:
|
|
187
|
-
cwd:
|
|
385
|
+
id: row.id,
|
|
386
|
+
title: row.title,
|
|
387
|
+
cwd: row.cwd,
|
|
188
388
|
workspaceId,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
389
|
+
workspaceTitle: workspaceId === null ? '' : workspaceTitleById.get(workspaceId) ?? workspaceId,
|
|
390
|
+
origin: row.origin,
|
|
391
|
+
createdAt: row.updatedAt,
|
|
392
|
+
updatedAt: row.updatedAt,
|
|
393
|
+
running: row.running,
|
|
394
|
+
muted: mutedSessions.has(row.id),
|
|
395
|
+
archived: false,
|
|
396
|
+
orphaned: false,
|
|
193
397
|
});
|
|
194
|
-
seen.add(
|
|
195
|
-
if (sessions.length >= 500) break;
|
|
398
|
+
seen.add(row.id);
|
|
196
399
|
}
|
|
197
400
|
|
|
198
|
-
|
|
401
|
+
// A mute outlives the row it was set on: keep archived, subagent, blank, or
|
|
402
|
+
// deleted Sessions visible here so the user can still clear that mute.
|
|
403
|
+
for (const id of allMutedSessions) {
|
|
199
404
|
if (seen.has(id)) continue;
|
|
200
|
-
|
|
405
|
+
const summary = summaryById.get(id);
|
|
406
|
+
const workspaceId = workspaceBySession.get(id) ?? null;
|
|
407
|
+
sessions.push({
|
|
408
|
+
id,
|
|
409
|
+
title: summary?.title ?? '',
|
|
410
|
+
cwd: summary?.cwd ?? '',
|
|
411
|
+
workspaceId,
|
|
412
|
+
workspaceTitle: workspaceId === null ? '' : workspaceTitleById.get(workspaceId) ?? workspaceId,
|
|
413
|
+
origin: summary?.origin ?? null,
|
|
414
|
+
createdAt: summary?.updatedAt ?? 0,
|
|
415
|
+
updatedAt: summary?.updatedAt ?? 0,
|
|
416
|
+
running: false,
|
|
417
|
+
muted: mutedSessions.has(id),
|
|
418
|
+
archived: archivedSessionIds.has(id),
|
|
419
|
+
orphaned: true,
|
|
420
|
+
});
|
|
201
421
|
seen.add(id);
|
|
202
|
-
if (sessions.length >= 500) break;
|
|
203
422
|
}
|
|
423
|
+
|
|
204
424
|
return { workspaces, sessions, sessionWorkspace: Object.fromEntries(workspaceBySession) };
|
|
205
425
|
}
|
|
206
426
|
|
|
@@ -233,7 +453,12 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
233
453
|
disposers.push(dispose);
|
|
234
454
|
};
|
|
235
455
|
|
|
236
|
-
|
|
456
|
+
/**
|
|
457
|
+
* Build the complete management payload.
|
|
458
|
+
* @param {Request} [request] - current request, used for cancellation.
|
|
459
|
+
* @returns {Promise<object>} payload.
|
|
460
|
+
*/
|
|
461
|
+
const statePayload = async (request) => {
|
|
237
462
|
const config = runtime.getConfig();
|
|
238
463
|
const snapshot = runtime.getState();
|
|
239
464
|
return {
|
|
@@ -244,8 +469,10 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
244
469
|
mute: {
|
|
245
470
|
workspaces: snapshot.mutedWorkspaces,
|
|
246
471
|
sessions: snapshot.mutedSessions,
|
|
472
|
+
browserWorkspaces: snapshot.browserMutedWorkspaces ?? [],
|
|
473
|
+
browserSessions: snapshot.browserMutedSessions ?? [],
|
|
247
474
|
},
|
|
248
|
-
catalog: buildCatalog(host, snapshot),
|
|
475
|
+
catalog: await buildCatalog(host, snapshot, request?.signal),
|
|
249
476
|
lastTest: snapshot.lastTest,
|
|
250
477
|
lastDelivery: runtime.lastDelivery(),
|
|
251
478
|
warnings: runtime.warnings(),
|
|
@@ -266,7 +493,7 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
266
493
|
});
|
|
267
494
|
});
|
|
268
495
|
|
|
269
|
-
register(ROUTES.state, ['GET'], async () => json(statePayload()));
|
|
496
|
+
register(ROUTES.state, ['GET'], async (request) => json(await statePayload(request)));
|
|
270
497
|
|
|
271
498
|
register(ROUTES.config, ['POST'], async (request) => {
|
|
272
499
|
if (!writeAllowed(request)) return json({ ok: false, error: 'forbidden' }, 403);
|
|
@@ -278,7 +505,7 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
278
505
|
}
|
|
279
506
|
try {
|
|
280
507
|
await runtime.updateConfig(body);
|
|
281
|
-
return json(statePayload());
|
|
508
|
+
return json(await statePayload(request));
|
|
282
509
|
} catch (error) {
|
|
283
510
|
return json({ ok: false, error: error?.message ?? '保存失败' }, 400);
|
|
284
511
|
}
|
|
@@ -294,10 +521,12 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
294
521
|
}
|
|
295
522
|
const target = muteTarget(body.kind, body.id);
|
|
296
523
|
if (target === null) return json({ ok: false, error: '无效的静音目标' }, 400);
|
|
524
|
+
const channel = muteChannel(body.channel);
|
|
525
|
+
if (channel === null) return json({ ok: false, error: '无效的静音通道' }, 400);
|
|
297
526
|
if (typeof body.muted !== 'boolean') return json({ ok: false, error: 'muted 必须是布尔值' }, 400);
|
|
298
527
|
try {
|
|
299
|
-
await runtime.setMute(target.kind, target.id, body.muted);
|
|
300
|
-
return json(statePayload());
|
|
528
|
+
await runtime.setMute(target.kind, target.id, body.muted, channel);
|
|
529
|
+
return json(await statePayload(request));
|
|
301
530
|
} catch (error) {
|
|
302
531
|
return json({ ok: false, error: error?.message ?? '保存失败' }, 500);
|
|
303
532
|
}
|
|
@@ -313,19 +542,28 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
313
542
|
}
|
|
314
543
|
const target = muteTarget(body.kind, body.id);
|
|
315
544
|
if (target === null) return json({ ok: false, error: '无效的静音目标' }, 400);
|
|
545
|
+
const channel = muteChannel(body.channel);
|
|
546
|
+
if (channel === null) return json({ ok: false, error: '无效的静音通道' }, 400);
|
|
316
547
|
try {
|
|
317
|
-
const result = await runtime.toggleMute(target.kind, target.id);
|
|
318
|
-
return json({ ...statePayload(), muted: result.muted });
|
|
548
|
+
const result = await runtime.toggleMute(target.kind, target.id, channel);
|
|
549
|
+
return json({ ...(await statePayload(request)), muted: result.muted });
|
|
319
550
|
} catch (error) {
|
|
320
551
|
return json({ ok: false, error: error?.message ?? '保存失败' }, 500);
|
|
321
552
|
}
|
|
322
553
|
});
|
|
323
554
|
|
|
555
|
+
register(ROUTES.browserEvents, ['GET'], async (request) => {
|
|
556
|
+
if (typeof runtime.browserNotify?.subscribe !== 'function') {
|
|
557
|
+
return json({ ok: false, error: '浏览器通知通道不可用' }, 501);
|
|
558
|
+
}
|
|
559
|
+
return runtime.browserNotify.subscribe(request);
|
|
560
|
+
});
|
|
561
|
+
|
|
324
562
|
register(ROUTES.test, ['POST'], async (request) => {
|
|
325
563
|
if (!writeAllowed(request)) return json({ ok: false, error: 'forbidden' }, 403);
|
|
326
564
|
try {
|
|
327
565
|
const result = await runtime.test();
|
|
328
|
-
return json({ ok: result.ok, result, state: statePayload() });
|
|
566
|
+
return json({ ok: result.ok, result, state: await statePayload(request) });
|
|
329
567
|
} catch (error) {
|
|
330
568
|
return json({ ok: false, error: error?.message ?? '测试推送失败' }, 500);
|
|
331
569
|
}
|
|
@@ -347,7 +585,7 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
347
585
|
}
|
|
348
586
|
try {
|
|
349
587
|
const result = await runtime.testChannel(body.id.trim());
|
|
350
|
-
return json({ ok: result.ok, result, state: statePayload() });
|
|
588
|
+
return json({ ok: result.ok, result, state: await statePayload(request) });
|
|
351
589
|
} catch (error) {
|
|
352
590
|
return json({ ok: false, error: error?.message ?? '渠道测试失败' }, 500);
|
|
353
591
|
}
|
package/lib/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import { readFileSync } from 'node:fs';
|
|
|
17
17
|
|
|
18
18
|
import { mergeConfig, normalizeConfig, objectOf, publicConfig } from './config.js';
|
|
19
19
|
import { BarkClient } from './bark.js';
|
|
20
|
+
import { BrowserNotifyHub } from './browser-notify.js';
|
|
20
21
|
import { ChannelSender } from './channels.js';
|
|
21
22
|
import { installSessionBridge, installQuestionBridge } from './events.js';
|
|
22
23
|
import { installHttpRoutes, nextConfigOverlay } from './http.js';
|
|
@@ -117,22 +118,38 @@ export function apply(ctx, config) {
|
|
|
117
118
|
logger,
|
|
118
119
|
version: packageVersion(),
|
|
119
120
|
});
|
|
121
|
+
const browserNotify = new BrowserNotifyHub({
|
|
122
|
+
getConfig,
|
|
123
|
+
state,
|
|
124
|
+
notifier,
|
|
125
|
+
getSession: (sessionId) => {
|
|
126
|
+
try {
|
|
127
|
+
const sessions = typeof ctx.get === 'function' ? ctx.get('sessions') : ctx.sessions;
|
|
128
|
+
return sessions?.get?.(sessionId);
|
|
129
|
+
} catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
logger,
|
|
134
|
+
});
|
|
120
135
|
|
|
121
136
|
/** @type {Array<() => void>} */
|
|
122
137
|
const disposers = [];
|
|
123
138
|
disposers.push(
|
|
124
139
|
installSessionBridge(ctx, {
|
|
125
140
|
notifier,
|
|
141
|
+
browserNotifier: browserNotify,
|
|
126
142
|
getConfig,
|
|
127
143
|
logger,
|
|
128
144
|
}),
|
|
129
145
|
);
|
|
130
|
-
disposers.push(installQuestionBridge(ctx, { notifier, logger }));
|
|
146
|
+
disposers.push(installQuestionBridge(ctx, { notifier, browserNotifier: browserNotify, logger }));
|
|
131
147
|
|
|
132
148
|
const runtime = {
|
|
133
149
|
version: packageVersion(),
|
|
134
150
|
getConfig,
|
|
135
151
|
getState: () => state.snapshot(),
|
|
152
|
+
browserNotify,
|
|
136
153
|
warnings: () => [...new Set(warnings)],
|
|
137
154
|
lastDelivery: () => notifier.lastDelivery,
|
|
138
155
|
/**
|
|
@@ -165,19 +182,21 @@ export function apply(ctx, config) {
|
|
|
165
182
|
* @param {'workspace'|'session'} kind - mute level.
|
|
166
183
|
* @param {string} id - target id.
|
|
167
184
|
* @param {boolean} muted - desired state.
|
|
185
|
+
* @param {'push'|'browser'} [channel] - notification channel.
|
|
168
186
|
* @returns {Promise<void>} completion.
|
|
169
187
|
*/
|
|
170
|
-
async setMute(kind, id, muted) {
|
|
171
|
-
await state.setMuted(kind, id, muted);
|
|
188
|
+
async setMute(kind, id, muted, channel = 'push') {
|
|
189
|
+
await state.setMuted(kind, id, muted, channel);
|
|
172
190
|
},
|
|
173
191
|
/**
|
|
174
192
|
* Toggle one mute flag.
|
|
175
193
|
* @param {'workspace'|'session'} kind - mute level.
|
|
176
194
|
* @param {string} id - target id.
|
|
195
|
+
* @param {'push'|'browser'} [channel] - notification channel.
|
|
177
196
|
* @returns {Promise<{ muted: boolean }>} new value.
|
|
178
197
|
*/
|
|
179
|
-
async toggleMute(kind, id) {
|
|
180
|
-
const result = await state.toggleMuted(kind, id);
|
|
198
|
+
async toggleMute(kind, id, channel = 'push') {
|
|
199
|
+
const result = await state.toggleMuted(kind, id, channel);
|
|
181
200
|
return { muted: result.muted };
|
|
182
201
|
},
|
|
183
202
|
/** @returns {Promise<object>} sanitized test outcome. */
|
|
@@ -203,6 +222,7 @@ export function apply(ctx, config) {
|
|
|
203
222
|
() => () => {
|
|
204
223
|
abort.abort();
|
|
205
224
|
queue.dispose();
|
|
225
|
+
browserNotify.dispose();
|
|
206
226
|
for (const dispose of disposers) {
|
|
207
227
|
try {
|
|
208
228
|
dispose();
|