dsh-synapse 0.5.6 → 0.5.8
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 +60 -7
- package/app.js +915 -244
- package/browser-cache.js +286 -0
- package/client.js +5 -2
- package/docs/architecture.md +12 -2
- package/docs/en/README.md +32 -12
- package/docs/zh-CN/README.md +31 -11
- package/index.js +310 -264
- package/package.json +4 -2
- package/session-bodies.js +497 -0
- package/styles.css +82 -43
package/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { copyFile, mkdir, readFile,
|
|
1
|
+
import { copyFile, mkdir, readFile, rename, stat, unlink, writeFile } from 'node:fs/promises'
|
|
2
2
|
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { constants as fsConstants } from 'node:fs'
|
|
3
4
|
import { dirname, join } from 'node:path'
|
|
4
|
-
import {
|
|
5
|
+
import { BODY_LIMITS, LogLimitError, SessionLogReader, bodiesFromEvents, bodyPage, bodySelection, previewBody } from './session-bodies.js'
|
|
6
|
+
export { SessionLogReader, bodiesFromEvents, decodeSessionLog } from './session-bodies.js'
|
|
5
7
|
|
|
6
8
|
export const name = 'synapse'
|
|
7
9
|
export const inject = ['webServer', 'sessions']
|
|
@@ -16,22 +18,13 @@ const MAX_NOTE_LENGTH = 4_000
|
|
|
16
18
|
// grow to megabytes (issue #42). Bodies are streamed back on demand by
|
|
17
19
|
// `/synapse/api/threads/:id/full`.
|
|
18
20
|
const EXCERPT_CHARS = 280
|
|
19
|
-
// On-demand bodies are capped so a single enormous tool result cannot stall the
|
|
20
|
-
// browser while its card is rendered.
|
|
21
|
-
const MAX_BODY_CHARS = 200_000
|
|
22
21
|
const PROJECTION_TRUNCATED_SUFFIX = '\n——…(详情查看全文)'
|
|
23
|
-
const BODY_TRUNCATED_SUFFIX = '\n——…(正文过长,其余请在 DSH 中查看)'
|
|
24
22
|
const TOPIC_COLORS = ['#0f766e', '#2563eb', '#be123c', '#7c3aed', '#b45309']
|
|
25
23
|
// The harness writes `user/message` rows of its own (runtime snapshots, skill
|
|
26
24
|
// catalogues). A canvas card must stand for a turn a person actually typed, so
|
|
27
25
|
// an injected row is recognised by its structured `source.kind` instead of by
|
|
28
26
|
// guessing at the prose (issue #31 / #23).
|
|
29
|
-
const
|
|
30
|
-
// Stored projections keep no raw event to re-read, so the one-time cleanup
|
|
31
|
-
// recognises a legacy catalogue card by the markers only that injection emits:
|
|
32
|
-
// the `<system-reminder>` opener plus the `<available_skills>` index it wraps.
|
|
33
|
-
const SKILL_CATALOG_OPENER = '<system-reminder>'
|
|
34
|
-
const SKILL_CATALOG_MARKER = '<available_skills>'
|
|
27
|
+
const PROJECTION_VERSION = 2
|
|
35
28
|
const LOCK_STALE_MS = 60_000
|
|
36
29
|
// Deferred (event-projection) writes coalesce into one save per window, so a
|
|
37
30
|
// burst of session events costs a single full-state write instead of one per
|
|
@@ -64,11 +57,27 @@ export class WorkspaceStore {
|
|
|
64
57
|
return structuredClone(workspace)
|
|
65
58
|
}
|
|
66
59
|
|
|
60
|
+
/** DSH groups by session membership, independently of Synapse's cwd groups. */
|
|
61
|
+
async threadsForSessions(sessionIds) {
|
|
62
|
+
if (!Array.isArray(sessionIds) || sessionIds.some(id => typeof id !== 'string' || id.trim() === '')) throw new InputError('sessionIds 必须是非空字符串数组')
|
|
63
|
+
await this.ready
|
|
64
|
+
const requested = new Set(sessionIds)
|
|
65
|
+
const threads = []
|
|
66
|
+
for (const workspace of this.state.workspaces) {
|
|
67
|
+
for (const thread of workspace.threads) {
|
|
68
|
+
if (requested.has(thread.dshSessionId)) threads.push(thread)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Sessions without a projection simply contribute no matches.
|
|
72
|
+
// Clone only matches, so unrelated workspaces never enter the response.
|
|
73
|
+
return structuredClone(threads)
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
/**
|
|
68
77
|
* Read-only lookup used by on-demand body loading: returns the live thread
|
|
69
78
|
* (no clone) together with its workspace, or null when the id is unknown.
|
|
70
79
|
*/
|
|
71
|
-
async
|
|
80
|
+
async readThread(threadId) {
|
|
72
81
|
await this.ready
|
|
73
82
|
for (const workspace of this.state.workspaces) {
|
|
74
83
|
for (const thread of workspace.threads ?? []) {
|
|
@@ -150,11 +159,21 @@ export class WorkspaceStore {
|
|
|
150
159
|
if (!Array.isArray(removedSessionIds) || removedSessionIds.some(item => typeof item !== 'string')) throw new InputError('removedSessionIds 必须是字符串数组')
|
|
151
160
|
const blankIds = new Set(sessions.filter(item => item?.blank === true && typeof item.id === 'string').map(item => item.id))
|
|
152
161
|
const removedIds = new Set(removedSessionIds)
|
|
162
|
+
let changed = false
|
|
153
163
|
for (const workspace of this.state.workspaces) {
|
|
154
164
|
if (workspace.kind !== 'dsh') continue
|
|
155
|
-
|
|
165
|
+
if (blankIds.size === 0 && removedIds.size === 0) continue
|
|
166
|
+
const threads = workspace.threads.filter(thread => !blankIds.has(thread.dshSessionId) && !removedIds.has(thread.dshSessionId))
|
|
167
|
+
if (threads.length === workspace.threads.length) continue
|
|
168
|
+
workspace.threads = threads
|
|
169
|
+
workspace.updatedAt = new Date().toISOString()
|
|
170
|
+
changed = true
|
|
171
|
+
}
|
|
172
|
+
const workspaces = this.state.workspaces.filter(workspace => workspace.kind !== 'dsh' || workspace.threads.length > 0)
|
|
173
|
+
if (workspaces.length !== this.state.workspaces.length) {
|
|
174
|
+
this.state.workspaces = workspaces
|
|
175
|
+
changed = true
|
|
156
176
|
}
|
|
157
|
-
this.state.workspaces = this.state.workspaces.filter(workspace => workspace.kind !== 'dsh' || workspace.threads.length > 0)
|
|
158
177
|
for (const item of sessions) {
|
|
159
178
|
if (typeof item?.id !== 'string' || item.id === '' || typeof item.cwd !== 'string' || item.cwd === '') continue
|
|
160
179
|
if (item.blank === true) continue
|
|
@@ -162,15 +181,12 @@ export class WorkspaceStore {
|
|
|
162
181
|
// must not recreate a session that the user deliberately archived.
|
|
163
182
|
if (this.state.hiddenSessionIds.includes(item.id)) continue
|
|
164
183
|
const workspace = this.dshWorkspace(item.cwd, 'DSH 任务')
|
|
165
|
-
const session = { id: item.id, header: {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
thread.title = item.title.slice(0, MAX_TITLE_LENGTH)
|
|
169
|
-
thread.dshSessionTitle = thread.title
|
|
170
|
-
}
|
|
184
|
+
const session = { id: item.id, header: { cwd: item.cwd, parentSession: typeof item.parentId === 'string' ? item.parentId : undefined }, title: typeof item.title === 'string' ? item.title : undefined }
|
|
185
|
+
const result = this.dshThread(workspace, session)
|
|
186
|
+
changed = result.changed || changed
|
|
171
187
|
}
|
|
172
|
-
return this.
|
|
173
|
-
}, { deferred: true })
|
|
188
|
+
return { changed, value: this.state.workspaces.map(workspace => this.summary(workspace)) }
|
|
189
|
+
}, { deferred: true, trackChanges: true })
|
|
174
190
|
}
|
|
175
191
|
|
|
176
192
|
async addMessage(threadId, text) {
|
|
@@ -228,43 +244,147 @@ export class WorkspaceStore {
|
|
|
228
244
|
}
|
|
229
245
|
|
|
230
246
|
/** Replay one live DSH session into the dedicated projection workspace. */
|
|
231
|
-
async projectSession(session, replayFrom = 0, workspaceTitle = 'DSH 任务') {
|
|
232
|
-
return this.mutate(() => {
|
|
233
|
-
if (this.state.hiddenSessionIds.includes(session.id)) return null
|
|
247
|
+
async projectSession(session, replayFrom = 0, workspaceTitle = 'DSH 任务', { returnThread = true } = {}) {
|
|
248
|
+
return this.mutate(async () => {
|
|
249
|
+
if (this.state.hiddenSessionIds.includes(session.id)) return { changed: false, value: null }
|
|
234
250
|
const workspace = this.dshWorkspace(sessionCwd(session), workspaceTitle)
|
|
235
|
-
const
|
|
251
|
+
const result = this.dshThread(workspace, session)
|
|
252
|
+
const { thread } = result
|
|
253
|
+
let changed = result.changed
|
|
236
254
|
// `snapshotEvents()` replaces the removed `session.events` getter. It is
|
|
237
255
|
// marked deprecated because DSH is moving complete logs out of memory, but
|
|
238
256
|
// rebuilding this canvas from history needs the full sequence and the
|
|
239
257
|
// reader is the only complete-log access a plugin still has.
|
|
240
|
-
|
|
241
|
-
|
|
258
|
+
const events = session.snapshotEvents()
|
|
259
|
+
if (thread.projectionVersion !== PROJECTION_VERSION) {
|
|
260
|
+
const repair = this.projectionRepair(workspace, thread, replayFrom)
|
|
261
|
+
for (const event of events) repair.visit(event)
|
|
262
|
+
changed = await repair.commit() || changed
|
|
263
|
+
} else {
|
|
264
|
+
const seqs = new Set(thread.messages.map(message => message.sourceSeq))
|
|
265
|
+
for (const event of events) {
|
|
266
|
+
if (event.seq >= replayFrom && isProjectionEvent(event)) changed = this.projectEventInto(workspace, thread, event, seqs) || changed
|
|
267
|
+
}
|
|
242
268
|
}
|
|
243
|
-
return structuredClone(thread)
|
|
244
|
-
}, { deferred: true })
|
|
269
|
+
return { changed, value: changed && returnThread ? structuredClone(thread) : null }
|
|
270
|
+
}, { deferred: true, trackChanges: true })
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Repair closed sessions one at a time, retaining previews rather than logs. */
|
|
274
|
+
async repairStoredSessions(reader, onError = error => { throw error }) {
|
|
275
|
+
await this.ready
|
|
276
|
+
const ids = this.state.workspaces.flatMap(workspace => workspace.threads
|
|
277
|
+
.filter(thread => typeof thread.dshSessionId === 'string' && thread.projectionVersion !== PROJECTION_VERSION)
|
|
278
|
+
.map(thread => thread.id))
|
|
279
|
+
for (const id of ids) {
|
|
280
|
+
try { await this.mutate(async () => {
|
|
281
|
+
const located = await this.readThread(id)
|
|
282
|
+
if (located === null || located.thread.projectionVersion === PROJECTION_VERSION) return { changed: false, value: null }
|
|
283
|
+
const { workspace, thread } = located
|
|
284
|
+
// A legacy fork may not have saved its durable cut. Its earliest
|
|
285
|
+
// indexed seq is a conservative boundary; do not add inherited turns.
|
|
286
|
+
const legacyCut = thread.parentId || thread.sourceParentSessionId
|
|
287
|
+
? thread.messages.reduce((min, message) => Number.isSafeInteger(message.sourceSeq) ? Math.min(min, message.sourceSeq) : min, Infinity)
|
|
288
|
+
: 0
|
|
289
|
+
const repair = this.projectionRepair(workspace, thread, thread.sourceSeedLength ?? (Number.isFinite(legacyCut) ? legacyCut : 0))
|
|
290
|
+
// No commit if the log is absent, corrupt, partial or beyond the limits.
|
|
291
|
+
const complete = await reader.scan(thread.dshSessionId, repair.visit)
|
|
292
|
+
const changed = complete === true && await repair.commit()
|
|
293
|
+
return { changed, value: null }
|
|
294
|
+
}, { deferred: true, trackChanges: true }) }
|
|
295
|
+
catch (error) { onError(error) }
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Stage a repair off-state. Only exact seq matches may replace/remove old
|
|
301
|
+
* messages; local notes and rows absent from the available log survive.
|
|
302
|
+
*/
|
|
303
|
+
projectionRepair(workspace, thread, replayFrom) {
|
|
304
|
+
const oldBySeq = new Map(thread.messages.filter(message => Number.isSafeInteger(message.sourceSeq)).map(message => [message.sourceSeq, message]))
|
|
305
|
+
const seen = new Set()
|
|
306
|
+
const callIds = new Set()
|
|
307
|
+
const seqs = new Set()
|
|
308
|
+
const draft = { ...thread, messages: [], pendingProcess: [] }
|
|
309
|
+
const draftWorkspace = { updatedAt: workspace.updatedAt }
|
|
310
|
+
const visit = event => {
|
|
311
|
+
if (!Number.isSafeInteger(event?.seq)) return
|
|
312
|
+
seen.add(event.seq)
|
|
313
|
+
if (event.seq < replayFrom || event.type === 'session/title') return
|
|
314
|
+
if (event.type === 'tool/call' || event.type === 'tool/result') {
|
|
315
|
+
const id = toolCallId(event)
|
|
316
|
+
if (id !== '') callIds.add(id)
|
|
317
|
+
}
|
|
318
|
+
if (!isProjectionEvent(event)) return
|
|
319
|
+
const previousLength = draft.messages.length
|
|
320
|
+
this.projectEventInto(draftWorkspace, draft, event, seqs)
|
|
321
|
+
if (draft.messages.length > previousLength) {
|
|
322
|
+
const message = draft.messages.at(-1)
|
|
323
|
+
const old = oldBySeq.get(message.sourceSeq)
|
|
324
|
+
if (old) draft.messages[draft.messages.length - 1] = { ...old, ...message, id: old.id }
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const commit = async () => {
|
|
328
|
+
const projected = new Map(draft.messages.map(message => [message.sourceSeq, message]))
|
|
329
|
+
// Preserve tool entries whose original events are not in this snapshot.
|
|
330
|
+
for (const old of thread.messages) {
|
|
331
|
+
const target = projected.get(old.sourceSeq)
|
|
332
|
+
if (target && Array.isArray(old.process)) {
|
|
333
|
+
target.process ??= []
|
|
334
|
+
target.process.push(...old.process.filter(entry => !callIds.has(String(entry.callId ?? ''))))
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const ordered = draft.messages.map(message => ({ message, order: message.sourceSeq, local: false }))
|
|
338
|
+
let anchor = -1
|
|
339
|
+
for (const message of thread.messages) {
|
|
340
|
+
if (Number.isSafeInteger(message.sourceSeq)) {
|
|
341
|
+
anchor = message.sourceSeq
|
|
342
|
+
if (!seen.has(message.sourceSeq)) ordered.push({ message, order: anchor, local: false })
|
|
343
|
+
} else ordered.push({ message, order: anchor, local: true })
|
|
344
|
+
}
|
|
345
|
+
ordered.sort((a, b) => a.order - b.order || Number(a.local) - Number(b.local))
|
|
346
|
+
const messages = ordered.map(entry => entry.message)
|
|
347
|
+
const pendingProcess = [...draft.pendingProcess, ...(thread.pendingProcess ?? []).filter(entry => !callIds.has(String(entry.callId ?? '')))]
|
|
348
|
+
const complete = [...oldBySeq.keys()].every(seq => seen.has(seq))
|
|
349
|
+
const changed = complete || messages.length !== thread.messages.length
|
|
350
|
+
|| messages.some((message, index) => !sameProjection(message, thread.messages[index]))
|
|
351
|
+
|| !sameProcess(pendingProcess, thread.pendingProcess ?? [])
|
|
352
|
+
if (!changed) return false
|
|
353
|
+
// Keep the original canvas, once, before the first provenance repair.
|
|
354
|
+
try { await copyFile(this.dataFile, `${this.dataFile}.projection-v2-backup`, fsConstants.COPYFILE_EXCL) }
|
|
355
|
+
catch (error) { if (error.code !== 'EEXIST') throw error }
|
|
356
|
+
thread.messages = messages
|
|
357
|
+
thread.pendingProcess = pendingProcess
|
|
358
|
+
if (complete) thread.projectionVersion = PROJECTION_VERSION
|
|
359
|
+
thread.updatedAt = new Date().toISOString()
|
|
360
|
+
workspace.updatedAt = thread.updatedAt
|
|
361
|
+
return true
|
|
362
|
+
}
|
|
363
|
+
return { visit, commit }
|
|
245
364
|
}
|
|
246
365
|
|
|
247
366
|
/** Project one committed DSH session event. Repeated sequence numbers are ignored. */
|
|
248
367
|
async projectEvent(session, event, workspaceTitle = 'DSH 任务') {
|
|
249
|
-
return this.
|
|
250
|
-
if (this.state.hiddenSessionIds.includes(session.id)) return null
|
|
251
|
-
const workspace = this.dshWorkspace(sessionCwd(session), workspaceTitle)
|
|
252
|
-
const thread = this.dshThread(workspace, session)
|
|
253
|
-
this.projectEventInto(workspace, thread, event)
|
|
254
|
-
return structuredClone(thread)
|
|
255
|
-
}, { deferred: true })
|
|
368
|
+
return this.projectEvents(session, [event], workspaceTitle)
|
|
256
369
|
}
|
|
257
370
|
|
|
258
|
-
/**
|
|
259
|
-
async projectEvents(session, events, workspaceTitle = 'DSH 任务') {
|
|
260
|
-
|
|
371
|
+
/** No-op batches return null, without cloning the thread or scheduling a save. */
|
|
372
|
+
async projectEvents(session, events, workspaceTitle = 'DSH 任务', { returnThread = true } = {}) {
|
|
373
|
+
const relevant = events.filter(isProjectionEvent)
|
|
374
|
+
if (relevant.length === 0) return null
|
|
261
375
|
return this.mutate(() => {
|
|
262
|
-
if (this.state.hiddenSessionIds.includes(session.id)) return null
|
|
263
|
-
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
376
|
+
if (this.state.hiddenSessionIds.includes(session.id)) return { changed: false, value: null }
|
|
377
|
+
let workspace = this.state.workspaces.find(item => item.kind === 'dsh' && item.cwd === sessionCwd(session))
|
|
378
|
+
const existing = workspace?.threads.find(thread => thread.dshSessionId === session.id)
|
|
379
|
+
// An empty/injected message must not manufacture a new canvas node.
|
|
380
|
+
if (existing === undefined && !relevant.some(event => event.type === 'session/title' || event.type === 'tool/call' || event.type === 'tool/result' || projectableEvent(event) !== null)) return { changed: false, value: null }
|
|
381
|
+
workspace ??= this.dshWorkspace(sessionCwd(session), workspaceTitle)
|
|
382
|
+
const result = this.dshThread(workspace, session)
|
|
383
|
+
const { thread } = result
|
|
384
|
+
let changed = result.changed
|
|
385
|
+
for (const event of relevant) changed = this.projectEventInto(workspace, thread, event) || changed
|
|
386
|
+
return { changed, value: changed && returnThread ? structuredClone(thread) : null }
|
|
387
|
+
}, { deferred: true, trackChanges: true })
|
|
268
388
|
}
|
|
269
389
|
|
|
270
390
|
async load() {
|
|
@@ -295,13 +415,15 @@ export class WorkspaceStore {
|
|
|
295
415
|
}
|
|
296
416
|
}
|
|
297
417
|
|
|
298
|
-
async mutate(action, { deferred = false } = {}) {
|
|
418
|
+
async mutate(action, { deferred = false, trackChanges = false } = {}) {
|
|
299
419
|
await this.ready
|
|
300
420
|
const task = this.serial.then(async () => {
|
|
301
|
-
const result = action()
|
|
302
|
-
if (
|
|
303
|
-
|
|
304
|
-
|
|
421
|
+
const result = await action()
|
|
422
|
+
if (!trackChanges || result.changed) {
|
|
423
|
+
if (deferred) this.markDirty()
|
|
424
|
+
else await this.save()
|
|
425
|
+
}
|
|
426
|
+
return trackChanges ? result.value : result
|
|
305
427
|
})
|
|
306
428
|
this.serial = task.catch(() => undefined)
|
|
307
429
|
return task
|
|
@@ -426,16 +548,27 @@ export class WorkspaceStore {
|
|
|
426
548
|
dshThread(workspace, session) {
|
|
427
549
|
let thread = workspace.threads.find(item => item.dshSessionId === session.id)
|
|
428
550
|
if (thread !== undefined) {
|
|
551
|
+
let changed = false
|
|
429
552
|
if (typeof session.title === 'string' && session.title.trim() !== '') {
|
|
430
553
|
const title = session.title.slice(0, MAX_TITLE_LENGTH)
|
|
431
|
-
thread.title
|
|
432
|
-
|
|
554
|
+
if (thread.title !== title || thread.dshSessionTitle !== title) {
|
|
555
|
+
thread.title = title
|
|
556
|
+
thread.dshSessionTitle = title
|
|
557
|
+
changed = true
|
|
558
|
+
}
|
|
433
559
|
}
|
|
434
560
|
// `inheritedEventCount` is DSH's durable fork cut, the successor of the
|
|
435
561
|
// removed `header.seedLength`. It survives restore, unlike `firstLiveSeq`.
|
|
436
562
|
const seedLength = session.inheritedEventCount
|
|
437
|
-
if (Number.isSafeInteger(seedLength) && seedLength >= 0
|
|
438
|
-
|
|
563
|
+
if (Number.isSafeInteger(seedLength) && seedLength >= 0 && thread.sourceSeedLength !== seedLength) {
|
|
564
|
+
thread.sourceSeedLength = seedLength
|
|
565
|
+
changed = true
|
|
566
|
+
}
|
|
567
|
+
if (changed) {
|
|
568
|
+
thread.updatedAt = new Date().toISOString()
|
|
569
|
+
workspace.updatedAt = thread.updatedAt
|
|
570
|
+
}
|
|
571
|
+
return { thread, changed }
|
|
439
572
|
}
|
|
440
573
|
const parentSessionId = typeof session.header?.parentSession === 'string' ? session.header.parentSession : null
|
|
441
574
|
const parent = parentSessionId === null ? undefined : workspace.threads.find(item => item.dshSessionId === parentSessionId)
|
|
@@ -458,6 +591,7 @@ export class WorkspaceStore {
|
|
|
458
591
|
updatedAt: now,
|
|
459
592
|
messages: [],
|
|
460
593
|
pendingProcess: [],
|
|
594
|
+
projectionVersion: PROJECTION_VERSION,
|
|
461
595
|
}
|
|
462
596
|
workspace.threads.push(thread)
|
|
463
597
|
// A child may arrive before its parent during startup replay. Repair that
|
|
@@ -466,24 +600,28 @@ export class WorkspaceStore {
|
|
|
466
600
|
if (child.sourceParentSessionId === session.id && child.parentId === null) child.parentId = thread.id
|
|
467
601
|
}
|
|
468
602
|
workspace.updatedAt = now
|
|
469
|
-
return thread
|
|
603
|
+
return { thread, changed: true }
|
|
470
604
|
}
|
|
471
605
|
|
|
472
|
-
projectEventInto(workspace, thread, event) {
|
|
606
|
+
projectEventInto(workspace, thread, event, seqs = null) {
|
|
473
607
|
if (event.type === 'session/title' && typeof event.data?.title === 'string') {
|
|
474
|
-
|
|
475
|
-
thread.
|
|
608
|
+
const title = event.data.title.slice(0, MAX_TITLE_LENGTH)
|
|
609
|
+
if (thread.title === title && thread.dshSessionTitle === title) return false
|
|
610
|
+
thread.title = title
|
|
611
|
+
thread.dshSessionTitle = title
|
|
476
612
|
thread.updatedAt = new Date(event.time).toISOString()
|
|
477
613
|
workspace.updatedAt = thread.updatedAt
|
|
478
|
-
return
|
|
614
|
+
return true
|
|
479
615
|
}
|
|
480
616
|
if (event.type === 'tool/call' || event.type === 'tool/result') {
|
|
481
|
-
this.foldToolProcess(thread, event)
|
|
617
|
+
if (!this.foldToolProcess(thread, event)) return false
|
|
482
618
|
workspace.updatedAt = thread.updatedAt
|
|
483
|
-
return
|
|
619
|
+
return true
|
|
484
620
|
}
|
|
621
|
+
// Check identity before reading a possibly enormous repeated message body.
|
|
622
|
+
if (seqs ? seqs.has(event.seq) : thread.messages.some(message => message.sourceSeq === event.seq)) return false
|
|
485
623
|
const projection = projectableEvent(event)
|
|
486
|
-
if (projection === null
|
|
624
|
+
if (projection === null) return false
|
|
487
625
|
const at = new Date(event.time).toISOString()
|
|
488
626
|
const message = {
|
|
489
627
|
id: randomUUID(),
|
|
@@ -493,18 +631,21 @@ export class WorkspaceStore {
|
|
|
493
631
|
kind: projection.kind,
|
|
494
632
|
sourceSeq: event.seq,
|
|
495
633
|
at,
|
|
634
|
+
...(projection.kind === 'user' && typeof event.data?.source?.kind === 'string' ? { sourceKind: event.data.source.kind } : {}),
|
|
496
635
|
...(projection.kind === 'assistant' || projection.kind === 'error'
|
|
497
636
|
? { turn: event.data?.turn, step: event.data?.step, process: [] }
|
|
498
637
|
: {}),
|
|
499
638
|
}
|
|
500
639
|
this.attachPendingProcess(thread, message)
|
|
501
640
|
thread.messages.push(message)
|
|
641
|
+
seqs?.add(event.seq)
|
|
502
642
|
thread.updatedAt = at
|
|
503
643
|
workspace.updatedAt = at
|
|
504
644
|
if (thread.dshSessionTitle === null && projection.kind === 'user') {
|
|
505
645
|
thread.title = titleFromText(projection.text)
|
|
506
646
|
thread.dshSessionTitle = thread.title
|
|
507
647
|
}
|
|
648
|
+
return true
|
|
508
649
|
}
|
|
509
650
|
|
|
510
651
|
/**
|
|
@@ -514,14 +655,19 @@ export class WorkspaceStore {
|
|
|
514
655
|
* assistant/error message, retain it on the thread until that turn appears.
|
|
515
656
|
*/
|
|
516
657
|
foldToolProcess(thread, event) {
|
|
517
|
-
const at = new Date(event.time).toISOString()
|
|
518
658
|
const data = event.data ?? {}
|
|
519
|
-
const
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
659
|
+
const callId = toolCallId(event)
|
|
660
|
+
if (callId === '') return false
|
|
661
|
+
let target
|
|
662
|
+
for (let index = thread.messages.length - 1; index >= 0; index -= 1) {
|
|
663
|
+
const message = thread.messages[index]
|
|
664
|
+
if ((message.kind === 'assistant' || message.kind === 'error')
|
|
665
|
+
&& (message.turn === data.turn && message.step === data.step || message.turn === undefined && message.step === undefined)) {
|
|
666
|
+
target = message
|
|
667
|
+
break
|
|
668
|
+
}
|
|
669
|
+
}
|
|
523
670
|
const process = target === undefined ? (thread.pendingProcess ??= []) : (target.process ??= [])
|
|
524
|
-
const callId = String(event.type === 'tool/call' ? data.callId : data.message?.source?.callId ?? '')
|
|
525
671
|
const entry = process.find(item => item.callId === callId)
|
|
526
672
|
// Process records keep only identity and status: tool arguments and results
|
|
527
673
|
// are the bulk of the old file (issue #42) and are reloaded on demand.
|
|
@@ -529,6 +675,7 @@ export class WorkspaceStore {
|
|
|
529
675
|
if (entry === undefined) {
|
|
530
676
|
process.push({ callId, turn: data.turn, step: data.step, name: data.name, status: 'pending' })
|
|
531
677
|
} else {
|
|
678
|
+
if (entry.name === data.name) return false
|
|
532
679
|
entry.name = data.name
|
|
533
680
|
}
|
|
534
681
|
} else {
|
|
@@ -536,11 +683,15 @@ export class WorkspaceStore {
|
|
|
536
683
|
if (entry === undefined) {
|
|
537
684
|
process.push({ callId, turn: data.turn, step: data.step, name: '工具调用', status: failed ? 'failed' : 'done' })
|
|
538
685
|
} else {
|
|
539
|
-
|
|
540
|
-
|
|
686
|
+
const status = failed ? 'failed' : 'done'
|
|
687
|
+
const name = typeof entry.name === 'string' && entry.name !== '' ? entry.name : '工具调用'
|
|
688
|
+
if (entry.status === status && entry.name === name) return false
|
|
689
|
+
entry.status = status
|
|
690
|
+
entry.name = name
|
|
541
691
|
}
|
|
542
692
|
}
|
|
543
|
-
thread.updatedAt =
|
|
693
|
+
thread.updatedAt = new Date(event.time).toISOString()
|
|
694
|
+
return true
|
|
544
695
|
}
|
|
545
696
|
|
|
546
697
|
attachPendingProcess(thread, message) {
|
|
@@ -585,7 +736,7 @@ function normalizeState(value) {
|
|
|
585
736
|
...workspace,
|
|
586
737
|
threads: Array.isArray(workspace.threads) ? workspace.threads.map(thread => {
|
|
587
738
|
if (Array.isArray(thread.messages)) {
|
|
588
|
-
const messages = thread.messages.filter(message => !
|
|
739
|
+
const messages = thread.messages.filter(message => !(message.kind === 'user' && isInjectedUserSource(message.source ?? { kind: message.sourceKind })))
|
|
589
740
|
if (messages.length !== thread.messages.length) migrated = true
|
|
590
741
|
return { ...thread, messages }
|
|
591
742
|
}
|
|
@@ -733,6 +884,19 @@ function requiredText(value, maxLength, field) {
|
|
|
733
884
|
return text
|
|
734
885
|
}
|
|
735
886
|
|
|
887
|
+
function toolCallId(event) {
|
|
888
|
+
const id = event.type === 'tool/call' ? event.data?.callId : event.data?.message?.source?.callId
|
|
889
|
+
return id === null || id === undefined ? '' : String(id)
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/** Cheap intake filter; error kinds stay aligned with the body projection. */
|
|
893
|
+
function isProjectionEvent(event) {
|
|
894
|
+
if (event?.type === 'session/title') return typeof event.data?.title === 'string'
|
|
895
|
+
if (event?.type === 'tool/call' || event?.type === 'tool/result') return toolCallId(event) !== ''
|
|
896
|
+
if (event?.type === 'user/message' && isInjectedUserSource(event.data?.source)) return false
|
|
897
|
+
return eventKind(event) !== null
|
|
898
|
+
}
|
|
899
|
+
|
|
736
900
|
/** Event → canvas message kind, or null when the event is not a message. */
|
|
737
901
|
function eventKind(event) {
|
|
738
902
|
switch (event?.type) {
|
|
@@ -774,142 +938,11 @@ function projectableEvent(event) {
|
|
|
774
938
|
const kind = eventKind(event)
|
|
775
939
|
if (kind === null) return null
|
|
776
940
|
const text = eventBody(event)
|
|
777
|
-
if (kind === 'user' && (isInjectedUserSource(event.data?.source)
|
|
941
|
+
if (kind === 'user' && (isInjectedUserSource(event.data?.source)
|
|
942
|
+
|| typeof event.data?.source?.kind !== 'string' && isRuntimeContextText(text))) return null
|
|
778
943
|
return noteProjection(kind, text)
|
|
779
944
|
}
|
|
780
945
|
|
|
781
|
-
/**
|
|
782
|
-
* Full bodies for the requested stored messages, assembled from one session's
|
|
783
|
-
* event log. Exported so the on-demand loader is testable without a Host.
|
|
784
|
-
* `seqs === null` returns every projected message of the thread.
|
|
785
|
-
*/
|
|
786
|
-
export function bodiesFromEvents(thread, events, seqs) {
|
|
787
|
-
const wanted = seqs === null ? null : new Set(seqs)
|
|
788
|
-
const messages = {}
|
|
789
|
-
const eventsBySeq = new Map(events.map(event => [event?.seq, event]))
|
|
790
|
-
const calls = new Map()
|
|
791
|
-
for (const event of events) {
|
|
792
|
-
if (event?.type !== 'tool/call' && event?.type !== 'tool/result') continue
|
|
793
|
-
const callId = String(event.type === 'tool/call' ? event.data?.callId : event.data?.message?.source?.callId ?? '')
|
|
794
|
-
if (callId === '') continue
|
|
795
|
-
const record = calls.get(callId) ?? {}
|
|
796
|
-
if (event.type === 'tool/call') {
|
|
797
|
-
record.arguments = typeof event.data?.arguments === 'string' ? event.data.arguments : null
|
|
798
|
-
} else {
|
|
799
|
-
record.result = contentText(event.data?.message?.content) || null
|
|
800
|
-
record.error = errorText(event.data?.error)
|
|
801
|
-
}
|
|
802
|
-
calls.set(callId, record)
|
|
803
|
-
}
|
|
804
|
-
for (const message of thread.messages ?? []) {
|
|
805
|
-
if (wanted !== null && !wanted.has(message.sourceSeq)) continue
|
|
806
|
-
const event = eventsBySeq.get(message.sourceSeq)
|
|
807
|
-
if (event === undefined) continue
|
|
808
|
-
const body = eventBody(event).trim()
|
|
809
|
-
const capped = body.length > MAX_BODY_CHARS ? `${body.slice(0, MAX_BODY_CHARS)}${BODY_TRUNCATED_SUFFIX}` : body
|
|
810
|
-
const process = Array.isArray(message.process)
|
|
811
|
-
? message.process.map(entry => ({ ...entry, ...(calls.get(String(entry.callId)) ?? {}) }))
|
|
812
|
-
: []
|
|
813
|
-
messages[String(message.sourceSeq)] = { text: capped, truncated: capped.length < body.length, process }
|
|
814
|
-
}
|
|
815
|
-
return messages
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
// ---------------------------------------------------------------------------
|
|
819
|
-
// Session logs on disk
|
|
820
|
-
//
|
|
821
|
-
// `ctx.sessions` only exposes sessions this process currently holds, which is
|
|
822
|
-
// one or two out of a workspace's history. Everything else is read from the DSH
|
|
823
|
-
// session log: a run of concatenated zstd frames holding JSONL events.
|
|
824
|
-
// ---------------------------------------------------------------------------
|
|
825
|
-
|
|
826
|
-
const SESSION_LOG_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd])
|
|
827
|
-
const SESSION_LOG_PATTERN = /^session(?:\.[\w-]+)*\.jsonl\.zstd$/
|
|
828
|
-
|
|
829
|
-
/** Decode the concatenated zstd frames of one session log into JSONL text. */
|
|
830
|
-
export function decodeSessionLog(buffer) {
|
|
831
|
-
if (typeof zstdDecompressSync !== 'function') return null
|
|
832
|
-
const offsets = []
|
|
833
|
-
let index = buffer.indexOf(SESSION_LOG_MAGIC, 0)
|
|
834
|
-
while (index !== -1) {
|
|
835
|
-
offsets.push(index)
|
|
836
|
-
index = buffer.indexOf(SESSION_LOG_MAGIC, index + 4)
|
|
837
|
-
}
|
|
838
|
-
if (offsets.length === 0) return null
|
|
839
|
-
const parts = []
|
|
840
|
-
for (let i = 0; i < offsets.length; i += 1) {
|
|
841
|
-
const end = i + 1 < offsets.length ? offsets[i + 1] : buffer.length
|
|
842
|
-
try {
|
|
843
|
-
parts.push(zstdDecompressSync(buffer.subarray(offsets[i], end)))
|
|
844
|
-
} catch {
|
|
845
|
-
// A log that is being appended to can end mid-frame; decode what is whole.
|
|
846
|
-
break
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
if (parts.length === 0) return null
|
|
850
|
-
return Buffer.concat(parts).toString('utf8')
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
/** Reads session events from `$DSH_HOME/sessions`, caching by file mtime. */
|
|
854
|
-
export class SessionLogReader {
|
|
855
|
-
constructor(directory) {
|
|
856
|
-
this.directory = directory
|
|
857
|
-
this.cache = new Map()
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
/** Events of one session, or null when its log is missing or unreadable. */
|
|
861
|
-
async events(sessionId) {
|
|
862
|
-
if (typeof this.directory !== 'string' || this.directory === '' || typeof sessionId !== 'string' || sessionId === '') return null
|
|
863
|
-
const file = await this.findLog(sessionId)
|
|
864
|
-
if (file === null) {
|
|
865
|
-
this.cache.delete(sessionId)
|
|
866
|
-
return null
|
|
867
|
-
}
|
|
868
|
-
const info = await stat(file).catch(() => null)
|
|
869
|
-
if (info === null) return null
|
|
870
|
-
const cached = this.cache.get(sessionId)
|
|
871
|
-
if (cached !== undefined && cached.mtimeMs === info.mtimeMs && cached.size === info.size) return cached.events
|
|
872
|
-
const text = decodeSessionLog(await readFile(file))
|
|
873
|
-
if (text === null) return null
|
|
874
|
-
const events = []
|
|
875
|
-
for (const line of text.split('\n')) {
|
|
876
|
-
if (line.trim() === '') continue
|
|
877
|
-
try {
|
|
878
|
-
const event = JSON.parse(line)
|
|
879
|
-
if (typeof event?.seq === 'number') events.push(event)
|
|
880
|
-
} catch {
|
|
881
|
-
// Trailing partial line while the session is still being written.
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
// Keep a few decoded logs; one screenful of cards touches few threads.
|
|
885
|
-
if (this.cache.size >= 4) this.cache.delete(this.cache.keys().next().value)
|
|
886
|
-
this.cache.set(sessionId, { mtimeMs: info.mtimeMs, size: info.size, events })
|
|
887
|
-
return events
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
async findLog(sessionId) {
|
|
891
|
-
let workspaces
|
|
892
|
-
try {
|
|
893
|
-
workspaces = await readdir(this.directory, { withFileTypes: true })
|
|
894
|
-
} catch {
|
|
895
|
-
return null
|
|
896
|
-
}
|
|
897
|
-
for (const workspace of workspaces) {
|
|
898
|
-
if (!workspace.isDirectory()) continue
|
|
899
|
-
const sessionDirectory = join(this.directory, workspace.name, sessionId)
|
|
900
|
-
let files
|
|
901
|
-
try {
|
|
902
|
-
files = await readdir(sessionDirectory)
|
|
903
|
-
} catch {
|
|
904
|
-
continue
|
|
905
|
-
}
|
|
906
|
-
const log = files.find(name => SESSION_LOG_PATTERN.test(name))
|
|
907
|
-
if (log !== undefined) return join(sessionDirectory, log)
|
|
908
|
-
}
|
|
909
|
-
return null
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
|
|
913
946
|
function errorText(value) {
|
|
914
947
|
if (typeof value === 'string') return value.trim() || null
|
|
915
948
|
if (value === null || value === undefined || typeof value !== 'object') return null
|
|
@@ -922,56 +955,43 @@ function errorText(value) {
|
|
|
922
955
|
|
|
923
956
|
function noteProjection(kind, text) {
|
|
924
957
|
const normalized = text.trim()
|
|
925
|
-
|
|
958
|
+
// Empty assistant steps still own tools; they never create a user turn.
|
|
959
|
+
if (normalized === '' && kind !== 'assistant') return null
|
|
926
960
|
const excerpt = excerptOf(normalized)
|
|
927
961
|
return { kind, text: excerpt, length: normalized.length, truncated: excerpt.length < normalized.length }
|
|
928
962
|
}
|
|
929
963
|
|
|
930
964
|
/** Short preview kept in the workspace file, with an ellipsis when cut. */
|
|
931
965
|
function excerptOf(text) {
|
|
932
|
-
|
|
933
|
-
return
|
|
966
|
+
const excerpt = text.length <= EXCERPT_CHARS ? text : `${text.slice(0, EXCERPT_CHARS).trimEnd()}…`
|
|
967
|
+
return Buffer.from(excerpt, 'utf8').toString('utf8')
|
|
934
968
|
}
|
|
935
969
|
|
|
936
970
|
function isRuntimeContextText(text) {
|
|
937
971
|
return typeof text === 'string' && text.trimStart().startsWith('Current runtime context. This snapshot supersedes earlier runtime-context snapshots.')
|
|
938
972
|
}
|
|
939
973
|
|
|
940
|
-
function isRuntimeContextMessage(message) {
|
|
941
|
-
return message?.kind === 'user' && isRuntimeContextText(message.text)
|
|
942
|
-
}
|
|
943
|
-
|
|
944
974
|
/** A harness-authored turn, identified by its structured provenance. */
|
|
945
975
|
function isInjectedUserSource(source) {
|
|
946
|
-
return typeof source?.kind === 'string' &&
|
|
976
|
+
return typeof source?.kind === 'string' && source.kind !== 'user'
|
|
947
977
|
}
|
|
948
978
|
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
function isSkillCatalogText(text) {
|
|
954
|
-
if (typeof text !== 'string') return false
|
|
955
|
-
const head = text.trimStart()
|
|
956
|
-
return head.startsWith(SKILL_CATALOG_OPENER) && head.includes(SKILL_CATALOG_MARKER)
|
|
979
|
+
function contentText(content) {
|
|
980
|
+
if (!Array.isArray(content)) return ''
|
|
981
|
+
return content.filter(block => block?.type === 'text').map(block => block.text)
|
|
982
|
+
.filter(value => typeof value === 'string' && value.trim() !== '').join('\n')
|
|
957
983
|
}
|
|
958
984
|
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
return message?.kind === 'user' && (isRuntimeContextMessage(message) || isSkillCatalogText(message.text))
|
|
985
|
+
function sameProcess(a, b) {
|
|
986
|
+
return a.length === b.length && a.every((entry, index) => {
|
|
987
|
+
const other = b[index]
|
|
988
|
+
return other && ['callId', 'name', 'status', 'turn', 'step'].every(key => entry[key] === other[key])
|
|
989
|
+
})
|
|
965
990
|
}
|
|
966
991
|
|
|
967
|
-
function
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
if (block?.type === 'text') return [block.text]
|
|
971
|
-
if (block?.type === 'tool-call') return [block.name, block.arguments]
|
|
972
|
-
if (block?.type === 'tool-result') return contentText(block.content)
|
|
973
|
-
return []
|
|
974
|
-
}).filter(value => typeof value === 'string' && value.trim() !== '').join('\n')
|
|
992
|
+
function sameProjection(a, b) {
|
|
993
|
+
return b && ['id', 'text', 'length', 'truncated', 'kind', 'sourceSeq', 'sourceKind', 'at', 'turn', 'step'].every(key => a[key] === b[key])
|
|
994
|
+
&& sameProcess(a.process ?? [], b.process ?? [])
|
|
975
995
|
}
|
|
976
996
|
|
|
977
997
|
function titleFromText(text) {
|
|
@@ -1014,7 +1034,7 @@ function sendFile(res, contentType, body) {
|
|
|
1014
1034
|
}
|
|
1015
1035
|
|
|
1016
1036
|
function page() {
|
|
1017
|
-
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Synapse for DSH</title><link rel="stylesheet" href="
|
|
1037
|
+
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><base href="./"><title>Synapse for DSH</title><link rel="stylesheet" href="styles.css"></head><body><div id="app"></div><script src="browser-cache.js"></script><script src="app.js"></script></body></html>`
|
|
1018
1038
|
}
|
|
1019
1039
|
|
|
1020
1040
|
/** Mount Synapse routes on the existing DSH Web Server. */
|
|
@@ -1038,13 +1058,14 @@ export function apply(ctx, config) {
|
|
|
1038
1058
|
// history through the parent node, so only project the child's live tail:
|
|
1039
1059
|
// `inheritedEventCount` is 0 without a fork and the durable cut with one.
|
|
1040
1060
|
const replayFrom = session.inheritedEventCount ?? 0
|
|
1041
|
-
void store.projectSession(session, replayFrom, projectionWorkspaceTitle).catch(reportProjectionFailure)
|
|
1061
|
+
void store.projectSession(session, replayFrom, projectionWorkspaceTitle, { returnThread: false }).catch(reportProjectionFailure)
|
|
1042
1062
|
}
|
|
1043
1063
|
// Buffer live events per session and flush them in one write per microtask,
|
|
1044
1064
|
// so a burst of turn events coalesces into a single save instead of N.
|
|
1045
1065
|
const projectionQueue = []
|
|
1046
1066
|
let projectionScheduled = false
|
|
1047
1067
|
const enqueueProjection = (session, event) => {
|
|
1068
|
+
if (!isProjectionEvent(event)) return
|
|
1048
1069
|
projectionQueue.push({ session, event })
|
|
1049
1070
|
if (projectionScheduled) return
|
|
1050
1071
|
projectionScheduled = true
|
|
@@ -1057,8 +1078,8 @@ export function apply(ctx, config) {
|
|
|
1057
1078
|
if (entry === undefined) bySession.set(item.session.id, [item.session, [item.event]])
|
|
1058
1079
|
else entry[1].push(item.event)
|
|
1059
1080
|
}
|
|
1060
|
-
for (const [
|
|
1061
|
-
void store.projectEvents(session, events, projectionWorkspaceTitle).catch(reportProjectionFailure)
|
|
1081
|
+
for (const [session, events] of bySession.values()) {
|
|
1082
|
+
void store.projectEvents(session, events, projectionWorkspaceTitle, { returnThread: false }).catch(reportProjectionFailure)
|
|
1062
1083
|
}
|
|
1063
1084
|
})
|
|
1064
1085
|
}
|
|
@@ -1066,30 +1087,39 @@ export function apply(ctx, config) {
|
|
|
1066
1087
|
ctx.on('session/created', replaySession)
|
|
1067
1088
|
ctx.on('session/event', enqueueProjection)
|
|
1068
1089
|
for (const session of ctx.sessions.list()) replaySession(session)
|
|
1090
|
+
void store.repairStoredSessions(sessionLog, reportProjectionFailure).catch(reportProjectionFailure)
|
|
1069
1091
|
}
|
|
1070
|
-
//
|
|
1071
|
-
//
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
const threadBodies = async (threadId, seqs) => {
|
|
1075
|
-
const located = await store.locateThread(threadId)
|
|
1092
|
+
// Both the canvas and detail view select a bounded page. Closed sessions
|
|
1093
|
+
// retain only that page's messages and tools, never the whole decoded log.
|
|
1094
|
+
const threadBodies = async (threadId, { seqs, cursor, limit }) => {
|
|
1095
|
+
const located = await store.readThread(threadId)
|
|
1076
1096
|
if (located === null) throw new NotFoundError('会话不存在')
|
|
1077
1097
|
const { thread } = located
|
|
1078
1098
|
const wanted = seqs === null ? null : new Set(seqs)
|
|
1099
|
+
const candidates = (thread.messages ?? []).filter(message => Number.isSafeInteger(message.sourceSeq) && (wanted === null || wanted.has(message.sourceSeq)))
|
|
1100
|
+
const selected = candidates.slice(cursor, cursor + limit)
|
|
1101
|
+
let payloads = {}
|
|
1102
|
+
let unavailable = false
|
|
1103
|
+
let warning
|
|
1079
1104
|
if (thread.dshSessionId === null) {
|
|
1080
|
-
const
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1105
|
+
for (const message of selected) payloads[String(message.sourceSeq)] = { ...previewBody(message, 'local'), previewOnly: false, local: true }
|
|
1106
|
+
} else if (selected.length > 0) {
|
|
1107
|
+
const session = ctx.sessions.list().find(candidate => candidate.id === thread.dshSessionId)
|
|
1108
|
+
let events
|
|
1109
|
+
try {
|
|
1110
|
+
events = session !== undefined ? session.snapshotEvents() : await sessionLog.events(thread.dshSessionId, bodySelection(selected))
|
|
1111
|
+
} catch (error) {
|
|
1112
|
+
if (!(error instanceof LogLimitError)) throw error
|
|
1113
|
+
warning = error.message
|
|
1114
|
+
for (const message of selected) payloads[String(message.sourceSeq)] = previewBody(message, 'limit')
|
|
1115
|
+
}
|
|
1116
|
+
if (events) payloads = bodiesFromEvents({ messages: selected }, events, null)
|
|
1117
|
+
else if (!warning) {
|
|
1118
|
+
unavailable = true
|
|
1119
|
+
for (const message of selected) payloads[String(message.sourceSeq)] = previewBody(message, 'unavailable')
|
|
1084
1120
|
}
|
|
1085
|
-
return { messages }
|
|
1086
1121
|
}
|
|
1087
|
-
|
|
1088
|
-
if (session !== undefined) return { messages: bodiesFromEvents(thread, session.snapshotEvents(), seqs) }
|
|
1089
|
-
// Closed session: read its log from disk, which is where the body lives.
|
|
1090
|
-
const events = await sessionLog.events(thread.dshSessionId)
|
|
1091
|
-
if (events === null) return { messages: {}, unavailable: true }
|
|
1092
|
-
return { messages: bodiesFromEvents(thread, events, seqs) }
|
|
1122
|
+
return { ...bodyPage(selected, payloads, cursor, candidates.length), ...(unavailable ? { unavailable: true } : {}), ...(warning ? { warning } : {}) }
|
|
1093
1123
|
}
|
|
1094
1124
|
// The DSH /api browser-trust fence does not cover /synapse routes, so this
|
|
1095
1125
|
// handler checks the Host header itself: localhost is allowed by default and
|
|
@@ -1106,6 +1136,7 @@ export function apply(ctx, config) {
|
|
|
1106
1136
|
if (req.method === 'GET') return sendJson(res, 200, { workspaces: await store.list() })
|
|
1107
1137
|
if (req.method === 'POST') return sendJson(res, 201, { workspace: await store.create((await readJson(req)).title) })
|
|
1108
1138
|
}
|
|
1139
|
+
if (path === '/synapse/api/threads/query' && req.method === 'POST') return sendJson(res, 200, { threads: await store.threadsForSessions((await readJson(req))?.sessionIds) })
|
|
1109
1140
|
const workspace = /^\/synapse\/api\/workspaces\/([0-9a-f-]+)$/i.exec(path)
|
|
1110
1141
|
if (workspace !== null) {
|
|
1111
1142
|
if (req.method === 'GET') return sendJson(res, 200, { workspace: await store.get(workspace[1]) })
|
|
@@ -1118,11 +1149,25 @@ export function apply(ctx, config) {
|
|
|
1118
1149
|
if (messages !== null && req.method === 'POST') return sendJson(res, 201, { thread: await store.addMessage(messages[1], (await readJson(req)).text) })
|
|
1119
1150
|
const full = /^\/synapse\/api\/threads\/([0-9a-f-]+)\/full$/i.exec(path)
|
|
1120
1151
|
if (full !== null && req.method === 'GET') {
|
|
1121
|
-
const
|
|
1122
|
-
const
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1152
|
+
const params = new URL(req.url ?? '/', 'http://dsh.local').searchParams
|
|
1153
|
+
const integer = (name, fallback, maximum) => {
|
|
1154
|
+
const raw = params.get(name)
|
|
1155
|
+
if (raw === null) return fallback
|
|
1156
|
+
if (!/^\d+$/.test(raw) || !Number.isSafeInteger(Number(raw)) || Number(raw) > maximum) throw new InputError(`${name} 超出范围`)
|
|
1157
|
+
return Number(raw)
|
|
1158
|
+
}
|
|
1159
|
+
const raw = params.get('seqs')
|
|
1160
|
+
let seqs = null
|
|
1161
|
+
if (raw !== null) {
|
|
1162
|
+
if (raw.length > 800) throw new InputError('seqs 过长')
|
|
1163
|
+
const values = raw.split(',')
|
|
1164
|
+
if (values.length > BODY_LIMITS.selectors || values.some(value => !/^\d+$/.test(value) || !Number.isSafeInteger(Number(value)))) throw new InputError('seqs 必须是至多 40 个非负整数')
|
|
1165
|
+
seqs = [...new Set(values.map(Number))]
|
|
1166
|
+
}
|
|
1167
|
+
const cursor = integer('cursor', 0, Number.MAX_SAFE_INTEGER)
|
|
1168
|
+
const limit = integer('limit', BODY_LIMITS.pageMessages, BODY_LIMITS.pageMessages)
|
|
1169
|
+
if (limit === 0) throw new InputError('limit 必须大于 0')
|
|
1170
|
+
return sendJson(res, 200, await threadBodies(full[1], { seqs, cursor, limit }))
|
|
1126
1171
|
}
|
|
1127
1172
|
const thread = /^\/synapse\/api\/threads\/([0-9a-f-]+)$/i.exec(path)
|
|
1128
1173
|
if (thread !== null && req.method === 'PATCH') return sendJson(res, 200, { thread: await store.updateThread(thread[1], await readJson(req)) })
|
|
@@ -1139,9 +1184,10 @@ export function apply(ctx, config) {
|
|
|
1139
1184
|
return sendJson(res, 500, { error: unreadable ? error.message : 'Synapse 数据暂时不可用' })
|
|
1140
1185
|
}
|
|
1141
1186
|
}
|
|
1142
|
-
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse', handler: (_req, res) => { res.writeHead(302, { location: '
|
|
1187
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse', handler: (_req, res) => { res.writeHead(302, { location: './synapse/' }); res.end() } }), 'synapse: redirect')
|
|
1143
1188
|
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse/', handler: (_req, res) => { sendFile(res, 'text/html; charset=utf-8', page()) } }), 'synapse: page')
|
|
1144
1189
|
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse/app.js', handler: async (_req, res) => { sendFile(res, 'text/javascript; charset=utf-8', await readFile(new URL('./app.js', import.meta.url), 'utf8')) } }), 'synapse: app')
|
|
1190
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse/browser-cache.js', handler: async (_req, res) => { sendFile(res, 'text/javascript; charset=utf-8', await readFile(new URL('./browser-cache.js', import.meta.url), 'utf8')) } }), 'synapse: browser cache')
|
|
1145
1191
|
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse/styles.css', handler: async (_req, res) => { sendFile(res, 'text/css; charset=utf-8', await readFile(new URL('./styles.css', import.meta.url), 'utf8')) } }), 'synapse: styles')
|
|
1146
1192
|
ctx.effect(() => ctx.webServer.register({ kind: 'exact', path: '/synapse/deepseek-mark.svg', handler: async (_req, res) => { sendFile(res, 'image/svg+xml', await readFile(new URL('./deepseek-mark.svg', import.meta.url), 'utf8')) } }), 'synapse: DeepSeek mark')
|
|
1147
1193
|
ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: '/synapse/api', handler: api }), 'synapse: api')
|