@wwwfz/dsh-agent-bridge 0.8.2 → 0.8.3

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.
@@ -1,93 +1,93 @@
1
- import { randomUUID } from 'node:crypto'
2
- import path from 'node:path'
3
-
4
- export function nativeSessionServices(ctx) {
5
- const sessions = ctx.get?.('sessions')
6
- const persistence = ctx.get?.('sessionPersistence')
7
- const workspaces = ctx.get?.('workspaceRegistry')
8
- const projectionCache = ctx.get?.('sessionProjectionCache')
9
- if (!sessions?.prepare || !persistence?.create || !workspaces?.create) return undefined
10
- return { sessions, persistence, workspaces, projectionCache }
11
- }
12
-
13
- export function nativeSessionEvents(messages, title, now = Date.now()) {
14
- const events = []
15
- let time = messages.find(message => message.time)?.time || now
16
- const add = (type, data, surfaceOp) => {
17
- events.push({ seq: events.length, time, type, data, ...(surfaceOp ? { surfaceOp } : {}) })
18
- }
19
- const message = (role, content, source) => ({ id: randomUUID(), role, content, source })
20
- let turn = 0
21
- let step = 0
22
- let replied = false
23
- const finish = () => {
24
- add('step/end', { turn, step })
25
- add('turn/end', { turn, reason: { kind: replied ? 'completed' : 'interrupted' } })
26
- }
27
- for (const entry of messages) {
28
- const content = [{ type: 'text', text: entry.text }]
29
- if (entry.role === 'user') {
30
- if (turn) finish()
31
- time = Math.max(time, entry.time || 0)
32
- turn++
33
- step = 1
34
- replied = false
35
- add('turn/start', { turn })
36
- add('step/start', { turn, step })
37
- if (turn === 1) {
38
- // The current DSH system prompt replaces the empty first surface node on resume.
39
- add('system/message', { turn, step, message: message('system', [], { kind: 'system-prompt' }) }, 'append')
40
- }
41
- add('user/message', message('user', content, { kind: 'user' }), 'append')
42
- continue
43
- }
44
- if (replied) {
45
- add('step/end', { turn, step })
46
- step++
47
- }
48
- time = Math.max(time, entry.time || 0)
49
- if (replied) {
50
- add('step/start', { turn, step })
51
- }
52
- add('assistant/message', {
53
- turn, step, stream: [],
54
- message: message('assistant', content, { kind: 'model', provider: 'agent-bridge-import', model: 'imported' }),
55
- }, 'append')
56
- replied = true
57
- }
58
- finish()
59
- add('session/title', { title, messageSeqs: [], source: { kind: 'user' } })
60
- return events
61
- }
62
-
63
- export async function saveNativeSession(services, record, cwd) {
64
- const { sessions, persistence, workspaces, projectionCache } = services
65
- const workspace = await workspaces.create(cwd)
66
- const normalize = value => {
67
- const normalized = path.normalize(value)
68
- return process.platform === 'win32' ? normalized.toLowerCase() : normalized
69
- }
70
- if (normalize(workspace.path) !== normalize(cwd)) {
71
- throw Object.assign(new Error('原工作目录已变化,请刷新项目。'), { status: 409 })
72
- }
73
- if (!record.stored) {
74
- const events = nativeSessionEvents(record.messages, record.title)
75
- const prepared = sessions.prepare(record.sessionId, { seed: events, meta: { cwd, createdAt: events[0].time } })
76
- if (prepared.header.version !== 4) {
77
- throw Object.assign(new Error('当前 DSH 的会话格式暂不支持历史迁入。'), { status: 409 })
78
- }
79
- const handle = await persistence.create(prepared.header)
80
- try {
81
- await handle.append(events)
82
- await handle.flush()
83
- record.stored = { header: prepared.header, events }
84
- } finally {
85
- await handle.close()
86
- }
87
- }
88
- if (!workspace.sessionIds.includes(record.sessionId)) {
89
- projectionCache?.coldSnapshot(record.stored.header, 0, record.stored.events)
90
- await workspace.attachSession(record.sessionId)
91
- }
92
- return { sessionId: record.sessionId }
93
- }
1
+ import { randomUUID } from 'node:crypto'
2
+ import path from 'node:path'
3
+
4
+ export function nativeSessionServices(ctx) {
5
+ const sessions = ctx.get?.('sessions')
6
+ const persistence = ctx.get?.('sessionPersistence')
7
+ const workspaces = ctx.get?.('workspaceRegistry')
8
+ const projectionCache = ctx.get?.('sessionProjectionCache')
9
+ if (!sessions?.prepare || !persistence?.create || !workspaces?.create) return undefined
10
+ return { sessions, persistence, workspaces, projectionCache }
11
+ }
12
+
13
+ export function nativeSessionEvents(messages, title, now = Date.now()) {
14
+ const events = []
15
+ let time = messages.find(message => message.time)?.time || now
16
+ const add = (type, data, surfaceOp) => {
17
+ events.push({ seq: events.length, time, type, data, ...(surfaceOp ? { surfaceOp } : {}) })
18
+ }
19
+ const message = (role, content, source) => ({ id: randomUUID(), role, content, source })
20
+ let turn = 0
21
+ let step = 0
22
+ let replied = false
23
+ const finish = () => {
24
+ add('step/end', { turn, step })
25
+ add('turn/end', { turn, reason: { kind: replied ? 'completed' : 'interrupted' } })
26
+ }
27
+ for (const entry of messages) {
28
+ const content = [{ type: 'text', text: entry.text }]
29
+ if (entry.role === 'user') {
30
+ if (turn) finish()
31
+ time = Math.max(time, entry.time || 0)
32
+ turn++
33
+ step = 1
34
+ replied = false
35
+ add('turn/start', { turn })
36
+ add('step/start', { turn, step })
37
+ if (turn === 1) {
38
+ // The current DSH system prompt replaces the empty first surface node on resume.
39
+ add('system/message', { turn, step, message: message('system', [], { kind: 'system-prompt' }) }, 'append')
40
+ }
41
+ add('user/message', message('user', content, { kind: 'user' }), 'append')
42
+ continue
43
+ }
44
+ if (replied) {
45
+ add('step/end', { turn, step })
46
+ step++
47
+ }
48
+ time = Math.max(time, entry.time || 0)
49
+ if (replied) {
50
+ add('step/start', { turn, step })
51
+ }
52
+ add('assistant/message', {
53
+ turn, step, stream: [],
54
+ message: message('assistant', content, { kind: 'model', provider: 'agent-bridge-import', model: 'imported' }),
55
+ }, 'append')
56
+ replied = true
57
+ }
58
+ finish()
59
+ add('session/title', { title, messageSeqs: [], source: { kind: 'user' } })
60
+ return events
61
+ }
62
+
63
+ export async function saveNativeSession(services, record, cwd) {
64
+ const { sessions, persistence, workspaces, projectionCache } = services
65
+ const workspace = await workspaces.create(cwd)
66
+ const normalize = value => {
67
+ const normalized = path.normalize(value)
68
+ return process.platform === 'win32' ? normalized.toLowerCase() : normalized
69
+ }
70
+ if (normalize(workspace.path) !== normalize(cwd)) {
71
+ throw Object.assign(new Error('原工作目录已变化,请刷新项目。'), { status: 409 })
72
+ }
73
+ if (!record.stored) {
74
+ const events = nativeSessionEvents(record.messages, record.title)
75
+ const prepared = sessions.prepare(record.sessionId, { seed: events, meta: { cwd, createdAt: events[0].time } })
76
+ if (prepared.header.version !== 4) {
77
+ throw Object.assign(new Error('当前 DSH 的会话格式暂不支持历史迁入。'), { status: 409 })
78
+ }
79
+ const handle = await persistence.create(prepared.header)
80
+ try {
81
+ await handle.append(events)
82
+ await handle.flush()
83
+ record.stored = { header: prepared.header, events }
84
+ } finally {
85
+ await handle.close()
86
+ }
87
+ }
88
+ if (!workspace.sessionIds.includes(record.sessionId)) {
89
+ projectionCache?.coldSnapshot(record.stored.header, 0, record.stored.events)
90
+ await workspace.attachSession(record.sessionId)
91
+ }
92
+ return { sessionId: record.sessionId }
93
+ }