@wwwfz/dsh-agent-bridge 0.8.3 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -194
- package/THIRD_PARTY_NOTICES.md +67 -0
- package/cordis.patch.yml +4 -4
- package/package.json +40 -40
- package/src/assets.js +405 -348
- package/src/catalog.js +999 -605
- package/src/client.js +883 -4
- package/src/config.js +88 -88
- package/src/conversation-readers.js +198 -0
- package/src/conversation-store.js +583 -0
- package/src/export.js +273 -12
- package/src/index.js +282 -136
- package/src/native-transcript.js +217 -0
- package/src/project-archive.js +145 -145
- package/src/projects.js +1837 -980
- package/src/session-import.js +138 -93
- package/src/session-trace.js +121 -0
- package/src/skill-handoff.js +1327 -0
- package/src/skill-package.js +417 -0
- package/src/skill-provider.js +249 -0
- package/src/watch.js +319 -309
- package/web/Geist-OFL.txt +93 -93
- package/web/agent-icons/NOTICE.md +19 -19
- package/web/agent-icons/antigravity.svg +76 -76
- package/web/agent-icons/claude.svg +3 -3
- package/web/agent-icons/dsh.svg +58 -58
- package/web/agent-icons/licenses/copilot.txt +21 -21
- package/web/agent-icons/licenses/dsh.txt +21 -21
- package/web/agent-icons/licenses/gemini.txt +201 -201
- package/web/agent-icons/licenses/opencode.txt +21 -21
- package/web/agent-icons/licenses/qoder-notice.txt +18 -18
- package/web/agent-icons/licenses/qoder.txt +22 -22
- package/web/agent-icons/licenses/qwen.txt +202 -202
- package/web/agent-icons/qoder.svg +4 -4
- package/web/agent-icons/qwen.svg +5 -5
- package/web/agent-icons/workbuddy.svg +29 -29
- package/web/inline-process.css +22 -0
- package/web/markdown.js +225 -0
- package/web/panel.css +168 -151
- package/web/panel.html +82 -43
- package/web/panel.js +1316 -93
- package/web/project-context.js +25 -25
- package/web/reader.css +259 -0
- package/web/vendor/markdown-it-LICENSE.txt +156 -0
- package/web/vendor/markdown-it.mjs +3064 -0
package/src/session-import.js
CHANGED
|
@@ -1,93 +1,138 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
add('
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util'
|
|
4
|
+
import { saveSessionTrace } from './session-trace.js'
|
|
5
|
+
import { saveConversationSeed } from './conversation-store.js'
|
|
6
|
+
import { nativeTranscriptEvents } from './native-transcript.js'
|
|
7
|
+
|
|
8
|
+
export function nativeSessionServices(ctx) {
|
|
9
|
+
const sessions = ctx.get?.('sessions')
|
|
10
|
+
const persistence = ctx.get?.('sessionPersistence')
|
|
11
|
+
const workspaces = ctx.get?.('workspaceRegistry')
|
|
12
|
+
const projectionCache = ctx.get?.('sessionProjectionCache')
|
|
13
|
+
if (!sessions?.prepare || !persistence?.create || !workspaces?.create) return undefined
|
|
14
|
+
return { sessions, persistence, workspaces, projectionCache, attachments: ctx.get?.('attachments') }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function nativeSessionEvents(messages, title, now = Date.now()) {
|
|
18
|
+
const events = []
|
|
19
|
+
let time = messages.find(message => message.time)?.time || now
|
|
20
|
+
const add = (type, data, surfaceOp) => {
|
|
21
|
+
events.push({ seq: events.length, time, type, data, ...(surfaceOp ? { surfaceOp } : {}) })
|
|
22
|
+
}
|
|
23
|
+
const message = (role, content, source) => ({ id: randomUUID(), role, content, source })
|
|
24
|
+
let turn = 0
|
|
25
|
+
let step = 0
|
|
26
|
+
let replied = false
|
|
27
|
+
let endReason
|
|
28
|
+
const finish = () => {
|
|
29
|
+
add('step/end', { turn, step })
|
|
30
|
+
add('turn/end', { turn, reason: { kind: endReason ?? (replied ? 'completed' : 'interrupted') } })
|
|
31
|
+
}
|
|
32
|
+
for (const entry of messages) {
|
|
33
|
+
const content = [{ type: 'text', text: entry.text }]
|
|
34
|
+
if (entry.role === 'user') {
|
|
35
|
+
if (turn) finish()
|
|
36
|
+
time = Math.max(time, entry.time || 0)
|
|
37
|
+
turn++
|
|
38
|
+
step = 1
|
|
39
|
+
replied = false
|
|
40
|
+
endReason = entry.endReason
|
|
41
|
+
add('turn/start', { turn })
|
|
42
|
+
add('step/start', { turn, step })
|
|
43
|
+
if (turn === 1) {
|
|
44
|
+
// The current DSH system prompt replaces the empty first surface node on resume.
|
|
45
|
+
add('system/message', { turn, step, message: message('system', [], { kind: 'system-prompt' }) }, 'append')
|
|
46
|
+
}
|
|
47
|
+
add('user/message', message('user', content, { kind: 'user' }), 'append')
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
if (replied) {
|
|
51
|
+
add('step/end', { turn, step })
|
|
52
|
+
step++
|
|
53
|
+
}
|
|
54
|
+
time = Math.max(time, entry.time || 0)
|
|
55
|
+
if (replied) {
|
|
56
|
+
add('step/start', { turn, step })
|
|
57
|
+
}
|
|
58
|
+
add('assistant/message', {
|
|
59
|
+
turn, step, stream: [],
|
|
60
|
+
message: message('assistant', content, { kind: 'model', provider: 'agent-bridge-import', model: 'imported' }),
|
|
61
|
+
}, 'append')
|
|
62
|
+
replied = true
|
|
63
|
+
}
|
|
64
|
+
finish()
|
|
65
|
+
add('session/title', { title, messageSeqs: [], source: { kind: 'user' } })
|
|
66
|
+
return events
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function storeSeed(services, record, cwd) {
|
|
70
|
+
const { sessions, persistence } = services
|
|
71
|
+
let seed = record.seed
|
|
72
|
+
if (!seed) {
|
|
73
|
+
const { events, nativeTimeline } = record.id && record.config && record.history
|
|
74
|
+
? await nativeTranscriptEvents(record, services.attachments)
|
|
75
|
+
: { events: nativeSessionEvents(record.messages, record.title) }
|
|
76
|
+
const prepared = sessions.prepare(record.sessionId, { seed: events, meta: { cwd, createdAt: events[0].time } })
|
|
77
|
+
if (prepared.header.version !== 4) {
|
|
78
|
+
throw Object.assign(new Error('当前 DSH 的会话格式暂不支持历史迁入。'), { status: 409 })
|
|
79
|
+
}
|
|
80
|
+
seed = { header: prepared.header, events, ...(nativeTimeline ? { nativeTimeline } : {}) }
|
|
81
|
+
if (record.id && record.config) seed = await saveConversationSeed(record.config, record.id, seed)
|
|
82
|
+
}
|
|
83
|
+
const { header, events } = seed
|
|
84
|
+
const existing = await persistence.stat?.(record.sessionId)
|
|
85
|
+
const handle = existing ? await persistence.open(record.sessionId, 'write') : await persistence.create(header)
|
|
86
|
+
try {
|
|
87
|
+
let offset = 0
|
|
88
|
+
if (existing) {
|
|
89
|
+
const loaded = (await handle.read(0, events.length + 1)).events
|
|
90
|
+
if (handle.header.id !== header.id || handle.header.version !== header.version
|
|
91
|
+
|| handle.header.cwd !== header.cwd || loaded.length > events.length
|
|
92
|
+
|| !loaded.every((entry, index) => isDeepStrictEqual(JSON.parse(JSON.stringify(entry)), events[index]))) {
|
|
93
|
+
throw Object.assign(new Error('目标会话的已有内容与迁入凭据不一致。'), { status: 409 })
|
|
94
|
+
}
|
|
95
|
+
offset = loaded.length
|
|
96
|
+
}
|
|
97
|
+
if (offset < events.length) await handle.append(events.slice(offset))
|
|
98
|
+
await handle.flush()
|
|
99
|
+
return seed
|
|
100
|
+
} finally {
|
|
101
|
+
await handle.close()
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function saveNativeSession(services, record, cwd) {
|
|
106
|
+
const { workspaces, projectionCache } = services
|
|
107
|
+
const workspace = await workspaces.create(cwd)
|
|
108
|
+
const normalize = value => {
|
|
109
|
+
const normalized = path.normalize(value)
|
|
110
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized
|
|
111
|
+
}
|
|
112
|
+
if (normalize(workspace.path) !== normalize(cwd)) {
|
|
113
|
+
throw Object.assign(new Error('原工作目录已变化,请刷新项目。'), { status: 409 })
|
|
114
|
+
}
|
|
115
|
+
if (!record.stored && !workspace.sessionIds.includes(record.sessionId)) record.stored = await storeSeed(services, record, cwd)
|
|
116
|
+
record.nativeTimeline = record.stored?.nativeTimeline ?? record.seed?.nativeTimeline
|
|
117
|
+
// Save the reviewed source snapshot before attaching the new DSH session.
|
|
118
|
+
const hasTrace = Array.isArray(record.trace) && record.trace.length > 0
|
|
119
|
+
if (!record.traceStored && record.config && (hasTrace || record.sourceStats)) {
|
|
120
|
+
try {
|
|
121
|
+
const sourceStats = record.sourceStats && record.nativeTimeline
|
|
122
|
+
? { ...record.sourceStats, importedMessages: record.nativeTimeline.nativeMessages }
|
|
123
|
+
: record.sourceStats
|
|
124
|
+
const saved = await saveSessionTrace(record.config, record.sessionId, record.trace, sourceStats)
|
|
125
|
+
record.traceStored = saved.saved === true
|
|
126
|
+
} catch {
|
|
127
|
+
throw Object.assign(
|
|
128
|
+
new Error('会话历史已写入,但来源记录暂时无法保存。可重试。'),
|
|
129
|
+
{ status: 503 },
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!workspace.sessionIds.includes(record.sessionId)) {
|
|
134
|
+
projectionCache?.coldSnapshot(record.stored.header, 0, record.stored.events)
|
|
135
|
+
await workspace.attachSession(record.sessionId)
|
|
136
|
+
}
|
|
137
|
+
return { sessionId: record.sessionId }
|
|
138
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto'
|
|
2
|
+
import { mkdir, open, readFile, rename, rm, stat } from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
|
+
const sessionIdPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
6
|
+
const kinds = new Set(['summary', 'tool_call', 'tool_result'])
|
|
7
|
+
const states = new Set(['ok', 'error', 'missing', 'pending', 'truncated', 'incomplete'])
|
|
8
|
+
const maxTraceItems = 200
|
|
9
|
+
const maxTraceBytes = 96 * 1024
|
|
10
|
+
|
|
11
|
+
export function traceDirectory(config) {
|
|
12
|
+
const base = path.dirname(config.configFile ?? path.join(config.home ?? '.', 'agent-bridge.json'))
|
|
13
|
+
return path.join(base, 'agent-bridge-traces')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isSessionId(value) {
|
|
17
|
+
return typeof value === 'string' && sessionIdPattern.test(value)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function clean(value, limit) {
|
|
21
|
+
return String(value ?? '').replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, ' ').slice(0, limit)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function normalizeTrace(trace) {
|
|
25
|
+
if (!Array.isArray(trace)) return []
|
|
26
|
+
const items = []
|
|
27
|
+
for (const item of trace.slice(0, maxTraceItems)) {
|
|
28
|
+
if (!item || !kinds.has(item.kind)) continue
|
|
29
|
+
items.push({
|
|
30
|
+
id: clean(item.id, 200),
|
|
31
|
+
kind: item.kind,
|
|
32
|
+
...(item.name ? { name: clean(item.name, 120) } : {}),
|
|
33
|
+
...(item.callId ? { callId: clean(item.callId, 200) } : {}),
|
|
34
|
+
text: clean(item.text, item.kind === 'tool_result' ? 4096 : 8192),
|
|
35
|
+
status: states.has(item.status) ? item.status : 'ok',
|
|
36
|
+
...(item.truncated === true ? { truncated: true } : {}),
|
|
37
|
+
...(Number.isFinite(item.time) && item.time > 0 ? { time: item.time } : {}),
|
|
38
|
+
...(Number.isInteger(item.messageIndex) && item.messageIndex >= 0 ? { messageIndex: item.messageIndex } : {}),
|
|
39
|
+
...(Number.isSafeInteger(item.turn) && item.turn > 0 ? { turn: item.turn } : {}),
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
return items
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function countTrace(trace) {
|
|
46
|
+
const counts = { summary: 0, toolCall: 0, toolResult: 0, failed: 0, missing: 0 }
|
|
47
|
+
for (const item of trace) {
|
|
48
|
+
if (item.kind === 'summary') counts.summary++
|
|
49
|
+
else if (item.kind === 'tool_call') {
|
|
50
|
+
counts.toolCall++
|
|
51
|
+
if (item.status === 'error') counts.failed++
|
|
52
|
+
if (item.status === 'missing') counts.missing++
|
|
53
|
+
} else if (item.kind === 'tool_result') counts.toolResult++
|
|
54
|
+
}
|
|
55
|
+
return counts
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function normalizeSourceStats(value) {
|
|
59
|
+
if (!value || typeof value !== 'object') return undefined
|
|
60
|
+
const { userMessages, assistantMessages, importedMessages, partial, elapsedMs, tokens } = value
|
|
61
|
+
const nonNegative = number => Number.isSafeInteger(number) && number >= 0
|
|
62
|
+
if (![userMessages, assistantMessages, importedMessages].every(nonNegative)
|
|
63
|
+
|| userMessages === 0 || importedMessages === 0 || !Number.isSafeInteger(userMessages + assistantMessages)
|
|
64
|
+
|| importedMessages > userMessages + assistantMessages) return undefined
|
|
65
|
+
const stats = { userMessages, assistantMessages, importedMessages, partial: partial === true }
|
|
66
|
+
if (nonNegative(elapsedMs)) stats.elapsedMs = elapsedMs
|
|
67
|
+
if (nonNegative(tokens?.total)) {
|
|
68
|
+
stats.tokens = { total: tokens.total }
|
|
69
|
+
for (const name of ['input', 'output']) {
|
|
70
|
+
if (nonNegative(tokens[name]) && tokens[name] <= tokens.total) stats.tokens[name] = tokens[name]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return stats
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function traceFile(config, sessionId) {
|
|
77
|
+
if (!isSessionId(sessionId)) throw Object.assign(new Error('会话标识无效。'), { status: 400 })
|
|
78
|
+
return path.join(traceDirectory(config), `${sessionId.toLowerCase()}.json`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function saveSessionTrace(config, sessionId, trace, sourceStats) {
|
|
82
|
+
const items = normalizeTrace(trace)
|
|
83
|
+
const stats = normalizeSourceStats(sourceStats)
|
|
84
|
+
if (!items.length && !stats) return { saved: false, items: 0 }
|
|
85
|
+
const target = traceFile(config, sessionId)
|
|
86
|
+
const body = `${JSON.stringify({
|
|
87
|
+
version: 1, sessionId: sessionId.toLowerCase(), savedAt: new Date().toISOString(), trace: items,
|
|
88
|
+
...(stats ? { sourceStats: stats } : {}),
|
|
89
|
+
})}\n`
|
|
90
|
+
if (Buffer.byteLength(body) > maxTraceBytes) throw new RangeError('来源记录超过存储上限。')
|
|
91
|
+
const directory = traceDirectory(config)
|
|
92
|
+
await mkdir(directory, { recursive: true })
|
|
93
|
+
const temporary = path.join(directory, `.${path.basename(target)}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`)
|
|
94
|
+
const handle = await open(temporary, 'wx', 0o600)
|
|
95
|
+
try {
|
|
96
|
+
try {
|
|
97
|
+
await handle.writeFile(body, 'utf8')
|
|
98
|
+
await handle.sync()
|
|
99
|
+
} finally {
|
|
100
|
+
await handle.close()
|
|
101
|
+
}
|
|
102
|
+
await rename(temporary, target)
|
|
103
|
+
} catch (error) {
|
|
104
|
+
await rm(temporary, { force: true })
|
|
105
|
+
throw error
|
|
106
|
+
}
|
|
107
|
+
return { saved: true, items: items.length, file: target }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function readSessionTrace(config, sessionId) {
|
|
111
|
+
const file = traceFile(config, sessionId)
|
|
112
|
+
let info
|
|
113
|
+
try { info = await stat(file) } catch { return undefined }
|
|
114
|
+
if (!info.isFile() || info.size > maxTraceBytes) return undefined
|
|
115
|
+
let parsed
|
|
116
|
+
try { parsed = JSON.parse(await readFile(file, 'utf8')) } catch { return undefined }
|
|
117
|
+
if (parsed?.version !== 1 || parsed.sessionId !== sessionId.toLowerCase() || !Array.isArray(parsed.trace)) return undefined
|
|
118
|
+
const stats = normalizeSourceStats(parsed.sourceStats)
|
|
119
|
+
return { sessionId: parsed.sessionId, savedAt: parsed.savedAt, trace: normalizeTrace(parsed.trace),
|
|
120
|
+
...(stats ? { sourceStats: stats } : {}) }
|
|
121
|
+
}
|