@wwwfz/dsh-agent-bridge 0.8.3 → 0.9.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 +198 -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 +906 -605
- package/src/client.js +623 -310
- package/src/config.js +88 -88
- package/src/export.js +241 -12
- package/src/index.js +160 -136
- package/src/project-archive.js +145 -145
- package/src/projects.js +1189 -980
- package/src/session-import.js +107 -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 +309 -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/markdown.js +225 -0
- package/web/panel.css +607 -502
- package/web/panel.html +277 -246
- package/web/panel.js +2334 -1559
- package/web/project-context.js +25 -25
- 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,107 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
let
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
add('
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
add('
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
step
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
await handle.
|
|
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 { saveSessionTrace } from './session-trace.js'
|
|
4
|
+
|
|
5
|
+
export function nativeSessionServices(ctx) {
|
|
6
|
+
const sessions = ctx.get?.('sessions')
|
|
7
|
+
const persistence = ctx.get?.('sessionPersistence')
|
|
8
|
+
const workspaces = ctx.get?.('workspaceRegistry')
|
|
9
|
+
const projectionCache = ctx.get?.('sessionProjectionCache')
|
|
10
|
+
if (!sessions?.prepare || !persistence?.create || !workspaces?.create) return undefined
|
|
11
|
+
return { sessions, persistence, workspaces, projectionCache }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function nativeSessionEvents(messages, title, now = Date.now()) {
|
|
15
|
+
const events = []
|
|
16
|
+
let time = messages.find(message => message.time)?.time || now
|
|
17
|
+
const add = (type, data, surfaceOp) => {
|
|
18
|
+
events.push({ seq: events.length, time, type, data, ...(surfaceOp ? { surfaceOp } : {}) })
|
|
19
|
+
}
|
|
20
|
+
const message = (role, content, source) => ({ id: randomUUID(), role, content, source })
|
|
21
|
+
let turn = 0
|
|
22
|
+
let step = 0
|
|
23
|
+
let replied = false
|
|
24
|
+
const finish = () => {
|
|
25
|
+
add('step/end', { turn, step })
|
|
26
|
+
add('turn/end', { turn, reason: { kind: replied ? 'completed' : 'interrupted' } })
|
|
27
|
+
}
|
|
28
|
+
for (const entry of messages) {
|
|
29
|
+
const content = [{ type: 'text', text: entry.text }]
|
|
30
|
+
if (entry.role === 'user') {
|
|
31
|
+
if (turn) finish()
|
|
32
|
+
time = Math.max(time, entry.time || 0)
|
|
33
|
+
turn++
|
|
34
|
+
step = 1
|
|
35
|
+
replied = false
|
|
36
|
+
add('turn/start', { turn })
|
|
37
|
+
add('step/start', { turn, step })
|
|
38
|
+
if (turn === 1) {
|
|
39
|
+
// The current DSH system prompt replaces the empty first surface node on resume.
|
|
40
|
+
add('system/message', { turn, step, message: message('system', [], { kind: 'system-prompt' }) }, 'append')
|
|
41
|
+
}
|
|
42
|
+
add('user/message', message('user', content, { kind: 'user' }), 'append')
|
|
43
|
+
continue
|
|
44
|
+
}
|
|
45
|
+
if (replied) {
|
|
46
|
+
add('step/end', { turn, step })
|
|
47
|
+
step++
|
|
48
|
+
}
|
|
49
|
+
time = Math.max(time, entry.time || 0)
|
|
50
|
+
if (replied) {
|
|
51
|
+
add('step/start', { turn, step })
|
|
52
|
+
}
|
|
53
|
+
add('assistant/message', {
|
|
54
|
+
turn, step, stream: [],
|
|
55
|
+
message: message('assistant', content, { kind: 'model', provider: 'agent-bridge-import', model: 'imported' }),
|
|
56
|
+
}, 'append')
|
|
57
|
+
replied = true
|
|
58
|
+
}
|
|
59
|
+
finish()
|
|
60
|
+
add('session/title', { title, messageSeqs: [], source: { kind: 'user' } })
|
|
61
|
+
return events
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function saveNativeSession(services, record, cwd) {
|
|
65
|
+
const { sessions, persistence, workspaces, projectionCache } = services
|
|
66
|
+
const workspace = await workspaces.create(cwd)
|
|
67
|
+
const normalize = value => {
|
|
68
|
+
const normalized = path.normalize(value)
|
|
69
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized
|
|
70
|
+
}
|
|
71
|
+
if (normalize(workspace.path) !== normalize(cwd)) {
|
|
72
|
+
throw Object.assign(new Error('原工作目录已变化,请刷新项目。'), { status: 409 })
|
|
73
|
+
}
|
|
74
|
+
if (!record.stored) {
|
|
75
|
+
const 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
|
+
const handle = await persistence.create(prepared.header)
|
|
81
|
+
try {
|
|
82
|
+
await handle.append(events)
|
|
83
|
+
await handle.flush()
|
|
84
|
+
record.stored = { header: prepared.header, events }
|
|
85
|
+
} finally {
|
|
86
|
+
await handle.close()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Save the reviewed source snapshot before attaching the new DSH session.
|
|
90
|
+
const hasTrace = Array.isArray(record.trace) && record.trace.length > 0
|
|
91
|
+
if (!record.traceStored && record.config && (hasTrace || record.sourceStats)) {
|
|
92
|
+
try {
|
|
93
|
+
const saved = await saveSessionTrace(record.config, record.sessionId, record.trace, record.sourceStats)
|
|
94
|
+
record.traceStored = saved.saved === true
|
|
95
|
+
} catch {
|
|
96
|
+
throw Object.assign(
|
|
97
|
+
new Error('会话历史已写入,但来源记录暂时无法保存。可重试。'),
|
|
98
|
+
{ status: 503 },
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!workspace.sessionIds.includes(record.sessionId)) {
|
|
103
|
+
projectionCache?.coldSnapshot(record.stored.header, 0, record.stored.events)
|
|
104
|
+
await workspace.attachSession(record.sessionId)
|
|
105
|
+
}
|
|
106
|
+
return { sessionId: record.sessionId }
|
|
107
|
+
}
|
|
@@ -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'])
|
|
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
|
+
}
|