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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-synapse",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"description": "A visual, non-linear conversation workspace plugin for DeepSeek Harness.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -12,8 +12,10 @@
|
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"index.js",
|
|
15
|
+
"session-bodies.js",
|
|
15
16
|
"client.js",
|
|
16
17
|
"app.js",
|
|
18
|
+
"browser-cache.js",
|
|
17
19
|
"deepseek-mark.svg",
|
|
18
20
|
"styles.css",
|
|
19
21
|
"docs",
|
|
@@ -41,7 +43,7 @@
|
|
|
41
43
|
}
|
|
42
44
|
},
|
|
43
45
|
"scripts": {
|
|
44
|
-
"build": "node --check index.js && node --check client.js && node --check app.js",
|
|
46
|
+
"build": "node --check index.js && node --check session-bodies.js && node --check client.js && node --check browser-cache.js && node --check app.js",
|
|
45
47
|
"test": "node --test test/*.test.js"
|
|
46
48
|
}
|
|
47
49
|
}
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs'
|
|
2
|
+
import { readdir, stat } from 'node:fs/promises'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import { constants, zstdDecompress, zstdDecompressSync } from 'node:zlib'
|
|
5
|
+
import { promisify } from 'node:util'
|
|
6
|
+
|
|
7
|
+
const KiB = 1024
|
|
8
|
+
export const BODY_LIMITS = Object.freeze({
|
|
9
|
+
pageMessages: 20, selectors: 40, responseBytes: 512 * KiB,
|
|
10
|
+
messageBytes: 128 * KiB, textBytes: 64 * KiB, toolFieldBytes: 8 * KiB,
|
|
11
|
+
processBytes: 64 * KiB, processEntries: 12,
|
|
12
|
+
})
|
|
13
|
+
export const LOG_LIMITS = Object.freeze({
|
|
14
|
+
compressedBytes: 8 * KiB * KiB, decodedBytes: 32 * KiB * KiB,
|
|
15
|
+
frameBytes: 8 * KiB * KiB, lineBytes: 2 * KiB * KiB,
|
|
16
|
+
cacheBytes: 8 * KiB * KiB, cacheEntries: 4, concurrency: 2, pendingReads: 16,
|
|
17
|
+
})
|
|
18
|
+
const TRUNCATED_SUFFIX = '\n——…(内容过长,其余请在 DSH 中查看)'
|
|
19
|
+
const jsonBytes = value => Buffer.byteLength(JSON.stringify(value), 'utf8')
|
|
20
|
+
// V8 slices/ropes can keep the original large JSON line alive. Retained text
|
|
21
|
+
// must own a small backing allocation, even when it looks like a short preview.
|
|
22
|
+
const ownString = value => Buffer.from(value, 'utf8').toString('utf8')
|
|
23
|
+
|
|
24
|
+
export class LogLimitError extends Error {
|
|
25
|
+
constructor(kind) { super(`会话日志超出读取上限(${kind}),请在 DSH 中查看全文`); this.name = 'LogLimitError' }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Cap serialized UTF-8 too: control characters can cost six JSON bytes each. */
|
|
29
|
+
function boundedString(value, bytes, alreadyTruncated = false) {
|
|
30
|
+
if (typeof value !== 'string') return { text: '', truncated: alreadyTruncated }
|
|
31
|
+
let end = Math.min(value.length, bytes)
|
|
32
|
+
const prefix = value.slice(0, end)
|
|
33
|
+
if (!alreadyTruncated && end === value.length && jsonBytes(prefix) <= bytes) return { text: ownString(prefix), truncated: false }
|
|
34
|
+
const budget = Math.max(2, bytes - jsonBytes(TRUNCATED_SUFFIX))
|
|
35
|
+
let low = 0
|
|
36
|
+
let high = end
|
|
37
|
+
while (low < high) {
|
|
38
|
+
const mid = Math.ceil((low + high) / 2)
|
|
39
|
+
if (jsonBytes(value.slice(0, mid)) <= budget) low = mid
|
|
40
|
+
else high = mid - 1
|
|
41
|
+
}
|
|
42
|
+
end = low
|
|
43
|
+
if (end > 0 && /[\uD800-\uDBFF]/.test(value[end - 1])) end -= 1
|
|
44
|
+
return { text: ownString(value.slice(0, end).trimEnd() + TRUNCATED_SUFFIX), truncated: true }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Walk nested tool results without recursion or building a full-size join. */
|
|
48
|
+
function boundedContent(content, bytes, textOnly = false) {
|
|
49
|
+
const parts = []
|
|
50
|
+
let chars = 0
|
|
51
|
+
let visited = 0
|
|
52
|
+
let truncated = false
|
|
53
|
+
const append = value => {
|
|
54
|
+
if (typeof value !== 'string' || value === '') return
|
|
55
|
+
const room = bytes - chars
|
|
56
|
+
if (room <= 0) { truncated = true; return }
|
|
57
|
+
const part = value.slice(0, room)
|
|
58
|
+
parts.push(part)
|
|
59
|
+
chars += part.length + 1
|
|
60
|
+
if (part.length < value.length) truncated = true
|
|
61
|
+
}
|
|
62
|
+
const stack = Array.isArray(content) ? [{ array: content, index: 0 }] : []
|
|
63
|
+
while (stack.length > 0) {
|
|
64
|
+
const frame = stack.at(-1)
|
|
65
|
+
if (frame.index >= frame.array.length) { stack.pop(); continue }
|
|
66
|
+
if (chars >= bytes || ++visited > 2048) { truncated = true; break }
|
|
67
|
+
const block = frame.array[frame.index++]
|
|
68
|
+
if (block?.type === 'text') append(block.text)
|
|
69
|
+
else if (!textOnly && block?.type === 'tool-call') { append(block.name); append(block.arguments) }
|
|
70
|
+
else if (!textOnly && block?.type === 'tool-result' && Array.isArray(block.content)) {
|
|
71
|
+
if (stack.length >= 32) truncated = true
|
|
72
|
+
else stack.push({ array: block.content, index: 0 })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return boundedString(parts.join('\n').trim(), bytes, truncated)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function boundedError(value, bytes) {
|
|
79
|
+
if (typeof value === 'string') return boundedString(value, bytes)
|
|
80
|
+
if (value === null || typeof value !== 'object') return { text: '', truncated: false }
|
|
81
|
+
const fields = [value.name, value.code, value.message].filter(field => typeof field === 'string' && field !== '')
|
|
82
|
+
return boundedString(fields.map(field => field.slice(0, bytes)).join(': '), bytes, fields.some(field => field.length > bytes))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function bodyOf(event) {
|
|
86
|
+
if (typeof event?.synapseBody === 'string') return { text: event.synapseBody, truncated: event.synapseTruncated === true }
|
|
87
|
+
switch (event?.type) {
|
|
88
|
+
case 'user/message': return boundedContent(event.data?.content, BODY_LIMITS.textBytes, true)
|
|
89
|
+
case 'assistant/message': return boundedContent(event.data?.message?.content, BODY_LIMITS.textBytes, true)
|
|
90
|
+
case 'todo/write': {
|
|
91
|
+
const todos = event.data?.todos
|
|
92
|
+
if (!Array.isArray(todos)) return { text: '', truncated: false }
|
|
93
|
+
let text = ''
|
|
94
|
+
let count = 0
|
|
95
|
+
while (count < todos.length && count < 2048 && text.length < BODY_LIMITS.textBytes) {
|
|
96
|
+
const todo = todos[count++]
|
|
97
|
+
text += `[${String(todo?.status ?? '').slice(0, 40)}] ${String(todo?.content ?? '').slice(0, BODY_LIMITS.textBytes - text.length)}\n`
|
|
98
|
+
}
|
|
99
|
+
return boundedString(text.trim(), BODY_LIMITS.textBytes, count < todos.length || text.length >= BODY_LIMITS.textBytes)
|
|
100
|
+
}
|
|
101
|
+
case 'turn/end':
|
|
102
|
+
if (event.data?.reason?.kind !== 'error') return { text: '本轮已取消', truncated: false }
|
|
103
|
+
return boundedError(event.data.reason.error ?? '本轮执行失败', BODY_LIMITS.textBytes)
|
|
104
|
+
default: return boundedError(event?.data?.error ?? event?.data?.reason ?? event?.data, BODY_LIMITS.textBytes)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Only metadata the UI uses is copied; arbitrary/deep payloads never leak. */
|
|
109
|
+
function processEntry(entry, record) {
|
|
110
|
+
const result = {
|
|
111
|
+
callId: boundedString(String(entry.callId ?? ''), 512).text,
|
|
112
|
+
name: boundedString(entry.name ?? '', 512).text,
|
|
113
|
+
status: ['pending', 'done', 'failed'].includes(entry.status) ? entry.status : undefined,
|
|
114
|
+
}
|
|
115
|
+
let truncated = record?.truncated === true
|
|
116
|
+
for (const field of ['arguments', 'result', 'error']) {
|
|
117
|
+
const value = record && Object.hasOwn(record, field) ? record[field] : entry[field]
|
|
118
|
+
const capped = field === 'error' ? boundedError(value, BODY_LIMITS.toolFieldBytes) : boundedString(value, BODY_LIMITS.toolFieldBytes)
|
|
119
|
+
result[field] = capped.text || null
|
|
120
|
+
truncated ||= capped.truncated
|
|
121
|
+
}
|
|
122
|
+
if (truncated) result.truncated = true
|
|
123
|
+
return result
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function bodySelection(messages) {
|
|
127
|
+
const seqs = new Set(messages.map(message => message.sourceSeq).filter(Number.isSafeInteger))
|
|
128
|
+
const callIds = new Set()
|
|
129
|
+
for (const message of messages) {
|
|
130
|
+
for (const entry of (Array.isArray(message.process) ? message.process : []).slice(0, BODY_LIMITS.processEntries)) callIds.add(String(entry.callId ?? ''))
|
|
131
|
+
}
|
|
132
|
+
return { seqs, callIds }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function toolId(event) {
|
|
136
|
+
return String(event.type === 'tool/call' ? event.data?.callId ?? '' : event.data?.message?.source?.callId ?? '')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function toolRecord(event) {
|
|
140
|
+
if (event.type === 'tool/call') {
|
|
141
|
+
const value = boundedString(event.data?.arguments, BODY_LIMITS.toolFieldBytes)
|
|
142
|
+
return { arguments: value.text || null, truncated: value.truncated }
|
|
143
|
+
}
|
|
144
|
+
const value = boundedContent(event.data?.message?.content, BODY_LIMITS.toolFieldBytes)
|
|
145
|
+
const error = boundedError(event.data?.error, BODY_LIMITS.toolFieldBytes)
|
|
146
|
+
return { result: value.text || null, error: error.text || null, truncated: value.truncated || error.truncated }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Compatibility helper; the HTTP caller passes only a selected page. */
|
|
150
|
+
export function bodiesFromEvents(thread, events, seqs) {
|
|
151
|
+
const wanted = seqs === null ? null : new Set(seqs)
|
|
152
|
+
const selected = (thread.messages ?? []).filter(message => wanted === null || wanted.has(message.sourceSeq))
|
|
153
|
+
const selection = bodySelection(selected)
|
|
154
|
+
const eventsBySeq = new Map()
|
|
155
|
+
const calls = new Map()
|
|
156
|
+
for (const event of events) {
|
|
157
|
+
if (selection.seqs.has(event?.seq)) eventsBySeq.set(event.seq, event)
|
|
158
|
+
if ((event?.type === 'tool/call' || event?.type === 'tool/result') && selection.callIds.has(toolId(event))) {
|
|
159
|
+
const id = toolId(event)
|
|
160
|
+
const previous = calls.get(id) ?? {}
|
|
161
|
+
const record = event.synapseTool ?? toolRecord(event)
|
|
162
|
+
calls.set(id, { ...previous, ...record, truncated: previous.truncated === true || record.truncated === true })
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const messages = {}
|
|
166
|
+
for (const message of selected) {
|
|
167
|
+
const event = eventsBySeq.get(message.sourceSeq)
|
|
168
|
+
if (event === undefined) continue
|
|
169
|
+
messages[String(message.sourceSeq)] = boundedMessage(message, bodyOf(event), calls)
|
|
170
|
+
}
|
|
171
|
+
return messages
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function boundedMessage(message, body, calls = new Map()) {
|
|
175
|
+
const process = []
|
|
176
|
+
let bytes = 2
|
|
177
|
+
const original = Array.isArray(message.process) ? message.process : []
|
|
178
|
+
for (const entry of original.slice(0, BODY_LIMITS.processEntries)) {
|
|
179
|
+
const bounded = processEntry(entry, calls.get(String(entry.callId)))
|
|
180
|
+
const size = jsonBytes(bounded) + 1
|
|
181
|
+
if (bytes + size > BODY_LIMITS.processBytes - 1024) break
|
|
182
|
+
bytes += size
|
|
183
|
+
process.push(bounded)
|
|
184
|
+
}
|
|
185
|
+
const omitted = original.length - process.length
|
|
186
|
+
return {
|
|
187
|
+
text: body.text, truncated: body.truncated, process,
|
|
188
|
+
...(omitted > 0 || process.some(entry => entry.truncated) ? { processTruncated: true, omittedProcess: omitted } : {}),
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function previewBody(message, reason) {
|
|
193
|
+
return { ...boundedMessage(message, boundedString(message.text ?? '', BODY_LIMITS.textBytes, message.truncated === true)),
|
|
194
|
+
previewOnly: true, reason }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Reserve envelope space and count the actual escaped UTF-8 response. */
|
|
198
|
+
export function bodyPage(messages, payloads, cursor, total) {
|
|
199
|
+
const response = { messages: {}, cursor, nextCursor: null, total }
|
|
200
|
+
let size = jsonBytes(response) + 1024
|
|
201
|
+
let consumed = 0
|
|
202
|
+
for (const message of messages) {
|
|
203
|
+
const key = String(message.sourceSeq)
|
|
204
|
+
const payload = payloads[key] ?? previewBody(message, 'missing')
|
|
205
|
+
const bytes = jsonBytes(key) + jsonBytes(payload) + 2
|
|
206
|
+
if (size + bytes > BODY_LIMITS.responseBytes) break
|
|
207
|
+
response.messages[key] = payload
|
|
208
|
+
size += bytes
|
|
209
|
+
consumed += 1
|
|
210
|
+
}
|
|
211
|
+
if (cursor + consumed < total) response.nextCursor = cursor + consumed
|
|
212
|
+
return response
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// DSH appends concatenated zstd frames. Parse frame lengths, since magic bytes
|
|
216
|
+
// may also occur inside an uncompressed block. Partial trailing frames are OK.
|
|
217
|
+
function* sessionFrames(buffer, limits) {
|
|
218
|
+
if (buffer.length > limits.compressedBytes) throw new LogLimitError('压缩文件')
|
|
219
|
+
let offset = 0
|
|
220
|
+
while (offset + 4 <= buffer.length) {
|
|
221
|
+
const start = offset
|
|
222
|
+
const magic = buffer.readUInt32LE(offset)
|
|
223
|
+
if ((magic >>> 4) === (0x184d2a50 >>> 4)) {
|
|
224
|
+
if (offset + 8 > buffer.length) return
|
|
225
|
+
offset += 8 + buffer.readUInt32LE(offset + 4)
|
|
226
|
+
if (offset > buffer.length) return
|
|
227
|
+
continue
|
|
228
|
+
}
|
|
229
|
+
if (magic !== 0xfd2fb528 || offset + 5 > buffer.length) return
|
|
230
|
+
const descriptor = buffer[offset + 4]
|
|
231
|
+
if (descriptor & 0x18) return // reserved/unused bits
|
|
232
|
+
const single = (descriptor & 0x20) !== 0
|
|
233
|
+
const fcsBytes = [single ? 1 : 0, 2, 4, 8][descriptor >>> 6]
|
|
234
|
+
const dictionaryBytes = [0, 1, 2, 4][descriptor & 3]
|
|
235
|
+
offset += 5
|
|
236
|
+
if (!single) {
|
|
237
|
+
if (offset >= buffer.length) return
|
|
238
|
+
const window = buffer[offset++]
|
|
239
|
+
const base = 2 ** (10 + (window >>> 3))
|
|
240
|
+
if (base + (base / 8) * (window & 7) > limits.decodedBytes) throw new LogLimitError('解压窗口')
|
|
241
|
+
}
|
|
242
|
+
offset += dictionaryBytes
|
|
243
|
+
if (offset + fcsBytes > buffer.length) return
|
|
244
|
+
if (fcsBytes > 0) {
|
|
245
|
+
const size = fcsBytes === 8 ? buffer.readBigUInt64LE(offset) : BigInt(buffer.readUIntLE(offset, fcsBytes) + (fcsBytes === 2 ? 256 : 0))
|
|
246
|
+
if (size > BigInt(limits.frameBytes)) throw new LogLimitError('单帧解压')
|
|
247
|
+
}
|
|
248
|
+
offset += fcsBytes
|
|
249
|
+
let last = false
|
|
250
|
+
while (!last) {
|
|
251
|
+
if (offset + 3 > buffer.length) return
|
|
252
|
+
const header = buffer.readUIntLE(offset, 3)
|
|
253
|
+
offset += 3
|
|
254
|
+
last = (header & 1) !== 0
|
|
255
|
+
const type = (header >>> 1) & 3
|
|
256
|
+
if (type === 3) return
|
|
257
|
+
offset += type === 1 ? 1 : header >>> 3
|
|
258
|
+
if (offset > buffer.length) return
|
|
259
|
+
}
|
|
260
|
+
if (descriptor & 4) offset += 4
|
|
261
|
+
if (offset > buffer.length) return
|
|
262
|
+
yield buffer.subarray(start, offset)
|
|
263
|
+
}
|
|
264
|
+
return offset === buffer.length
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function decodeOptions(bytes) {
|
|
268
|
+
return { maxOutputLength: bytes, params: { [constants.ZSTD_d_windowLogMax]: 25 } }
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function decodeError(error) {
|
|
272
|
+
if (error?.code === 'ERR_BUFFER_TOO_LARGE') throw new LogLimitError('解压容量')
|
|
273
|
+
throw error
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Bounded synchronous export for compatibility; production uses async zlib. */
|
|
277
|
+
export function decodeSessionLog(buffer, options = {}) {
|
|
278
|
+
if (typeof zstdDecompressSync !== 'function') return null
|
|
279
|
+
const limits = { ...LOG_LIMITS, ...options }
|
|
280
|
+
const parts = []
|
|
281
|
+
let size = 0
|
|
282
|
+
for (const frame of sessionFrames(buffer, limits)) {
|
|
283
|
+
if (size >= limits.decodedBytes) throw new LogLimitError('总解压')
|
|
284
|
+
let decoded
|
|
285
|
+
try { decoded = zstdDecompressSync(frame, decodeOptions(Math.min(limits.frameBytes, limits.decodedBytes - size))) }
|
|
286
|
+
catch (error) { if (error.code === 'ERR_BUFFER_TOO_LARGE') decodeError(error); break }
|
|
287
|
+
size += decoded.length
|
|
288
|
+
parts.push(decoded)
|
|
289
|
+
}
|
|
290
|
+
return parts.length === 0 ? null : Buffer.concat(parts, size).toString('utf8')
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const decompress = typeof zstdDecompress === 'function' ? promisify(zstdDecompress) : null
|
|
294
|
+
const SESSION_LOG_PATTERN = /^session(?:\.[\w-]+)*\.jsonl\.zstd$/
|
|
295
|
+
|
|
296
|
+
/** Retain only selected, bounded events; account cache size and gate reads. */
|
|
297
|
+
export class SessionLogReader {
|
|
298
|
+
constructor(directory, options = {}) {
|
|
299
|
+
this.directory = directory
|
|
300
|
+
this.limits = { ...LOG_LIMITS, ...options }
|
|
301
|
+
this.cache = new Map()
|
|
302
|
+
this.cacheBytes = 0
|
|
303
|
+
this.inflight = new Map()
|
|
304
|
+
this.activeReads = 0
|
|
305
|
+
this.waiters = []
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Visit raw events without retaining full bodies. Repairs use a complete,
|
|
310
|
+
* validated snapshot; a partial frame or malformed row never commits it.
|
|
311
|
+
*/
|
|
312
|
+
async scan(sessionId, visit) {
|
|
313
|
+
if (!decompress || !this.directory || typeof sessionId !== 'string' || sessionId === '') return null
|
|
314
|
+
if (this.activeReads + this.waiters.length >= this.limits.pendingReads) throw new LogLimitError('并发读取')
|
|
315
|
+
if (this.activeReads >= this.limits.concurrency) await new Promise(resolve => this.waiters.push(resolve))
|
|
316
|
+
else this.activeReads += 1
|
|
317
|
+
try {
|
|
318
|
+
const file = await this.findLog(sessionId)
|
|
319
|
+
if (file === null) return null
|
|
320
|
+
const info = await stat(file)
|
|
321
|
+
if (info.size === 0) return false
|
|
322
|
+
if (info.size > this.limits.compressedBytes) throw new LogLimitError('压缩文件')
|
|
323
|
+
const chunks = []
|
|
324
|
+
let size = 0
|
|
325
|
+
for await (const chunk of createReadStream(file, { end: info.size - 1 })) {
|
|
326
|
+
size += chunk.length
|
|
327
|
+
if (size > this.limits.compressedBytes) throw new LogLimitError('压缩文件')
|
|
328
|
+
chunks.push(chunk)
|
|
329
|
+
}
|
|
330
|
+
const compressed = Buffer.concat(chunks, size)
|
|
331
|
+
chunks.length = 0
|
|
332
|
+
let tail = Buffer.alloc(0)
|
|
333
|
+
let decodedBytes = 0
|
|
334
|
+
const consume = line => {
|
|
335
|
+
if (line.length > this.limits.lineBytes) throw new LogLimitError('单行内容')
|
|
336
|
+
if (line.length === 0) return
|
|
337
|
+
const event = JSON.parse(line.toString('utf8'))
|
|
338
|
+
if (Number.isSafeInteger(event?.seq)) visit(event)
|
|
339
|
+
}
|
|
340
|
+
const frames = sessionFrames(compressed, this.limits)
|
|
341
|
+
let next
|
|
342
|
+
while (!(next = frames.next()).done) {
|
|
343
|
+
const frame = next.value
|
|
344
|
+
if (decodedBytes >= this.limits.decodedBytes) throw new LogLimitError('总解压')
|
|
345
|
+
let decoded
|
|
346
|
+
try { decoded = await decompress(frame, decodeOptions(Math.min(this.limits.frameBytes, this.limits.decodedBytes - decodedBytes))) }
|
|
347
|
+
catch (error) { decodeError(error) }
|
|
348
|
+
decodedBytes += decoded.length
|
|
349
|
+
const data = tail.length ? Buffer.concat([tail, decoded]) : decoded
|
|
350
|
+
let start = 0
|
|
351
|
+
for (let end = data.indexOf(10); end !== -1; end = data.indexOf(10, start)) {
|
|
352
|
+
consume(data.subarray(start, end))
|
|
353
|
+
start = end + 1
|
|
354
|
+
}
|
|
355
|
+
if (data.length - start > this.limits.lineBytes) throw new LogLimitError('单行内容')
|
|
356
|
+
tail = Buffer.from(data.subarray(start))
|
|
357
|
+
}
|
|
358
|
+
if (next.value !== true || decodedBytes === 0) return false
|
|
359
|
+
consume(tail)
|
|
360
|
+
return true
|
|
361
|
+
} finally {
|
|
362
|
+
const next = this.waiters.shift()
|
|
363
|
+
if (next) next()
|
|
364
|
+
else this.activeReads -= 1
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async events(sessionId, selection = null) {
|
|
369
|
+
if (!decompress || typeof this.directory !== 'string' || this.directory === '' || typeof sessionId !== 'string' || sessionId === '') return null
|
|
370
|
+
const selector = selection === null ? '*' : JSON.stringify([[...selection.seqs].sort((a, b) => a - b), [...selection.callIds].sort()])
|
|
371
|
+
const key = JSON.stringify([sessionId, selector])
|
|
372
|
+
if (this.inflight.has(key)) return this.inflight.get(key)
|
|
373
|
+
if (this.inflight.size >= this.limits.pendingReads) throw new LogLimitError('并发读取')
|
|
374
|
+
const pending = this.read(sessionId, selection, key).finally(() => this.inflight.delete(key))
|
|
375
|
+
this.inflight.set(key, pending)
|
|
376
|
+
return pending
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async read(sessionId, selection, key) {
|
|
380
|
+
if (this.activeReads >= this.limits.concurrency) await new Promise(resolve => this.waiters.push(resolve))
|
|
381
|
+
else this.activeReads += 1
|
|
382
|
+
try {
|
|
383
|
+
const file = await this.findLog(sessionId)
|
|
384
|
+
if (file === null) { this.evict(key); return null }
|
|
385
|
+
let info
|
|
386
|
+
try { info = await stat(file) }
|
|
387
|
+
catch (error) {
|
|
388
|
+
if (error.code === 'ENOENT') { this.evict(key); return null }
|
|
389
|
+
throw error
|
|
390
|
+
}
|
|
391
|
+
const cached = this.cache.get(key)
|
|
392
|
+
if (cached?.mtimeMs === info.mtimeMs && cached?.size === info.size) {
|
|
393
|
+
this.cache.delete(key)
|
|
394
|
+
this.cache.set(key, cached)
|
|
395
|
+
return cached.events
|
|
396
|
+
}
|
|
397
|
+
this.evict(key)
|
|
398
|
+
if (info.size > this.limits.compressedBytes) throw new LogLimitError('压缩文件')
|
|
399
|
+
const chunks = []
|
|
400
|
+
let compressedBytes = 0
|
|
401
|
+
// end pins the snapshot even if a live writer appends during this read.
|
|
402
|
+
if (info.size === 0) return null
|
|
403
|
+
for await (const chunk of createReadStream(file, { end: info.size - 1 })) {
|
|
404
|
+
compressedBytes += chunk.length
|
|
405
|
+
if (compressedBytes > this.limits.compressedBytes) throw new LogLimitError('压缩文件')
|
|
406
|
+
chunks.push(chunk)
|
|
407
|
+
}
|
|
408
|
+
const compressed = Buffer.concat(chunks, compressedBytes)
|
|
409
|
+
chunks.length = 0
|
|
410
|
+
const events = []
|
|
411
|
+
let retainedBytes = Buffer.byteLength(key, 'utf8') * 3 + 128
|
|
412
|
+
if (retainedBytes > this.limits.cacheBytes) throw new LogLimitError('所选正文容量')
|
|
413
|
+
let decodedBytes = 0
|
|
414
|
+
let tail = Buffer.alloc(0)
|
|
415
|
+
const consume = line => {
|
|
416
|
+
if (line.length > this.limits.lineBytes) throw new LogLimitError('单行内容')
|
|
417
|
+
if (line.length === 0) return
|
|
418
|
+
let event
|
|
419
|
+
try { event = JSON.parse(line.toString('utf8')) } catch { return }
|
|
420
|
+
if (!Number.isSafeInteger(event?.seq)) return
|
|
421
|
+
const wantedBody = selection === null || selection.seqs.has(event.seq)
|
|
422
|
+
const wantedTool = (event.type === 'tool/call' || event.type === 'tool/result') && (selection === null || selection.callIds.has(toolId(event)))
|
|
423
|
+
if (!wantedBody && !wantedTool) return
|
|
424
|
+
const kept = { seq: event.seq, type: ownString(String(event.type ?? '').slice(0, 256)) }
|
|
425
|
+
if (wantedBody) {
|
|
426
|
+
const body = bodyOf(event)
|
|
427
|
+
kept.synapseBody = body.text
|
|
428
|
+
kept.synapseTruncated = body.truncated
|
|
429
|
+
}
|
|
430
|
+
if (wantedTool) {
|
|
431
|
+
const id = ownString(toolId(event))
|
|
432
|
+
kept.data = event.type === 'tool/call' ? { callId: id } : { message: { source: { callId: id } } }
|
|
433
|
+
kept.synapseTool = toolRecord(event)
|
|
434
|
+
}
|
|
435
|
+
// Conservative allowance for UTF-16 strings and object overhead, not
|
|
436
|
+
// just compressed bytes or an item count.
|
|
437
|
+
retainedBytes += jsonBytes(kept) * 3 + 256
|
|
438
|
+
if (retainedBytes > this.limits.cacheBytes) throw new LogLimitError('所选正文容量')
|
|
439
|
+
events.push(kept)
|
|
440
|
+
}
|
|
441
|
+
let frames = 0
|
|
442
|
+
for (const frame of sessionFrames(compressed, this.limits)) {
|
|
443
|
+
if (decodedBytes >= this.limits.decodedBytes) throw new LogLimitError('总解压')
|
|
444
|
+
let decoded
|
|
445
|
+
try { decoded = await decompress(frame, decodeOptions(Math.min(this.limits.frameBytes, this.limits.decodedBytes - decodedBytes))) }
|
|
446
|
+
catch (error) { if (error.code === 'ERR_BUFFER_TOO_LARGE') decodeError(error); break }
|
|
447
|
+
decodedBytes += decoded.length
|
|
448
|
+
frames += 1
|
|
449
|
+
const data = tail.length ? Buffer.concat([tail, decoded]) : decoded
|
|
450
|
+
let start = 0
|
|
451
|
+
for (let end = data.indexOf(10); end !== -1; end = data.indexOf(10, start)) {
|
|
452
|
+
consume(data.subarray(start, end))
|
|
453
|
+
start = end + 1
|
|
454
|
+
}
|
|
455
|
+
if (data.length - start > this.limits.lineBytes) throw new LogLimitError('单行内容')
|
|
456
|
+
tail = Buffer.from(data.subarray(start))
|
|
457
|
+
}
|
|
458
|
+
if (frames === 0) return null
|
|
459
|
+
consume(tail) // A final valid JSON line need not have a newline.
|
|
460
|
+
while (this.cache.size >= this.limits.cacheEntries || this.cacheBytes + retainedBytes > this.limits.cacheBytes) {
|
|
461
|
+
if (this.cache.size === 0) break
|
|
462
|
+
this.evict(this.cache.keys().next().value)
|
|
463
|
+
}
|
|
464
|
+
if (this.limits.cacheEntries > 0) {
|
|
465
|
+
this.cache.set(key, { events, bytes: retainedBytes, mtimeMs: info.mtimeMs, size: info.size })
|
|
466
|
+
this.cacheBytes += retainedBytes
|
|
467
|
+
}
|
|
468
|
+
return events
|
|
469
|
+
} finally {
|
|
470
|
+
const next = this.waiters.shift()
|
|
471
|
+
if (next) next()
|
|
472
|
+
else this.activeReads -= 1
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
evict(key) {
|
|
477
|
+
const entry = this.cache.get(key)
|
|
478
|
+
if (entry) this.cacheBytes -= entry.bytes
|
|
479
|
+
this.cache.delete(key)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
async findLog(sessionId) {
|
|
483
|
+
let workspaces
|
|
484
|
+
try { workspaces = await readdir(this.directory, { withFileTypes: true }) }
|
|
485
|
+
catch (error) { if (error.code === 'ENOENT') return null; throw error }
|
|
486
|
+
for (const workspace of workspaces) {
|
|
487
|
+
if (!workspace.isDirectory()) continue
|
|
488
|
+
const directory = join(this.directory, workspace.name, sessionId)
|
|
489
|
+
let files
|
|
490
|
+
try { files = await readdir(directory) }
|
|
491
|
+
catch (error) { if (error.code === 'ENOENT') continue; throw error }
|
|
492
|
+
const log = files.find(name => SESSION_LOG_PATTERN.test(name))
|
|
493
|
+
if (log !== undefined) return join(directory, log)
|
|
494
|
+
}
|
|
495
|
+
return null
|
|
496
|
+
}
|
|
497
|
+
}
|