@240xu/dsh-message-ops 0.10.8 → 0.11.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 +17 -1
- package/package.json +1 -1
- package/src/client.js +646 -1
- package/src/index.js +182 -6
- package/src/session-file.js +14 -0
- package/src/session-trash.js +402 -0
- package/test/composer-refill.test.js +2 -2
- package/test/ops-core.test.js +6 -1
- package/test/ops-search.test.js +104 -0
- package/test/session-trash.test.js +179 -0
package/test/ops-core.test.js
CHANGED
|
@@ -174,7 +174,7 @@ test('apply:无 webServer、无 tools 时走 inject 等待,不抛错', () =>
|
|
|
174
174
|
assert.equal(ctx.registrations.length, 0)
|
|
175
175
|
})
|
|
176
176
|
|
|
177
|
-
test('apply:webServer 存在时注册全部
|
|
177
|
+
test('apply:webServer 存在时注册全部 10 条路由;tools 缺失走 inject 等待', () => {
|
|
178
178
|
const ctx = mockCtx({ webServer: { register: (route) => ctx.registrations.push(route) } })
|
|
179
179
|
assert.doesNotThrow(() => apply(ctx))
|
|
180
180
|
const paths = ctx.registrations.map((r) => r.path).sort()
|
|
@@ -183,6 +183,11 @@ test('apply:webServer 存在时注册全部 5 条路由;tools 缺失走 inje
|
|
|
183
183
|
'/api/message-ops/messages',
|
|
184
184
|
'/api/message-ops/restore',
|
|
185
185
|
'/api/message-ops/revert',
|
|
186
|
+
'/api/message-ops/search',
|
|
187
|
+
'/api/message-ops/session/delete',
|
|
188
|
+
'/api/message-ops/session/purge',
|
|
189
|
+
'/api/message-ops/session/restore',
|
|
190
|
+
'/api/message-ops/session/trash',
|
|
186
191
|
'/api/message-ops/text',
|
|
187
192
|
], '0.9.0:branch/export 路由已移除(改用官方 session/fork 与 /api/session.export)')
|
|
188
193
|
// tools 缺失 → 容错等待而非 fatal
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compact payload + per-session search contract (TDD).
|
|
3
|
+
*
|
|
4
|
+
* The panel must not download fullText for every row (190 KB for 543 rows),
|
|
5
|
+
* but search must still match text beyond the 160-char list snippet.
|
|
6
|
+
*/
|
|
7
|
+
import test from 'node:test'
|
|
8
|
+
import assert from 'node:assert/strict'
|
|
9
|
+
import fs from 'node:fs'
|
|
10
|
+
import os from 'node:os'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
|
|
13
|
+
import { _internals } from '../src/index.js'
|
|
14
|
+
import { encodeSessionFile } from '../src/session-file.js'
|
|
15
|
+
|
|
16
|
+
const SID = 'session-33333333-3333-4333-8333-333333333333'
|
|
17
|
+
|
|
18
|
+
function setup(t) {
|
|
19
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'mops-search-'))
|
|
20
|
+
const prev = process.env.DSH_HOME
|
|
21
|
+
process.env.DSH_HOME = home
|
|
22
|
+
const dir = path.join(home, 'sessions', '--tmp-project--', SID)
|
|
23
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
24
|
+
const header = { type: 'session', id: SID, version: 4, createdAt: 1790000000000 }
|
|
25
|
+
const events = [
|
|
26
|
+
{
|
|
27
|
+
type: 'user/message', seq: 1, time: 1,
|
|
28
|
+
data: { id: 'u1', source: { kind: 'user' }, message: { role: 'user', content: [{ type: 'text', text: 'hello world' }] } },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: 'assistant/message', seq: 2, time: 2,
|
|
32
|
+
data: { id: 'a1', message: { role: 'assistant', content: [{ type: 'text', text: 'A'.repeat(300) }, { type: 'text', text: 'needle-inside the long answer' }] } },
|
|
33
|
+
},
|
|
34
|
+
{ type: 'tool/call', seq: 3, time: 3, data: { name: 'web_search', callId: 'c1' } },
|
|
35
|
+
{
|
|
36
|
+
type: 'system/message', seq: 4, time: 4,
|
|
37
|
+
surfaceOp: { op: 'replace', startSeq: 2, endSeq: 3 },
|
|
38
|
+
data: { id: 'm1', message: { role: 'system', content: [] } },
|
|
39
|
+
},
|
|
40
|
+
]
|
|
41
|
+
fs.writeFileSync(path.join(dir, 'session.v4.jsonl.zstd'), encodeSessionFile(header, events))
|
|
42
|
+
|
|
43
|
+
t.after(() => {
|
|
44
|
+
if (prev === undefined) delete process.env.DSH_HOME
|
|
45
|
+
else process.env.DSH_HOME = prev
|
|
46
|
+
fs.rmSync(home, { recursive: true, force: true })
|
|
47
|
+
})
|
|
48
|
+
return { ctx: { get: () => undefined } }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
test('compact view drops heavy fields but keeps visibility state and counts', async (t) => {
|
|
52
|
+
const { ctx } = setup(t)
|
|
53
|
+
const full = await _internals.opsList(ctx, SID)
|
|
54
|
+
const compact = await _internals.opsList(ctx, SID, { compact: true })
|
|
55
|
+
|
|
56
|
+
assert.equal(full.ok, true)
|
|
57
|
+
assert.equal(compact.ok, true)
|
|
58
|
+
assert.equal(compact.messages.length, 4)
|
|
59
|
+
assert.equal(typeof full.messages[0].fullText, 'string')
|
|
60
|
+
assert.equal(compact.messages[0].fullText, undefined, 'compact must not carry fullText')
|
|
61
|
+
assert.equal(compact.messages[0].turn, undefined, 'compact must not carry turn')
|
|
62
|
+
assert.equal(compact.messages[0].step, undefined, 'compact must not carry step')
|
|
63
|
+
assert.equal(compact.messages[0].range, undefined, 'compact must not carry range')
|
|
64
|
+
assert.equal(compact.messages[0].sourceKind, undefined, 'compact must not carry sourceKind')
|
|
65
|
+
assert.equal(compact.messages[0].id, undefined, 'compact must not carry message ids')
|
|
66
|
+
assert.equal(compact.messages[0].type, undefined, 'compact must not carry event type')
|
|
67
|
+
assert.equal(compact.messages[0].marker, undefined, 'compact must not carry per-row marker flags')
|
|
68
|
+
assert.equal(compact.messages[0].restoresSeq, undefined, 'compact must not carry per-row restore pointers')
|
|
69
|
+
assert.equal(compact.messages[1].shadowed, true)
|
|
70
|
+
assert.equal(compact.messages[2].shadowed, true)
|
|
71
|
+
assert.equal(compact.shadowedCount, 2)
|
|
72
|
+
assert.equal(compact.markerCount, 1)
|
|
73
|
+
assert.equal(compact.visibleCount, 2)
|
|
74
|
+
assert.equal(compact.session.confirmTitle, SID, 'untitled sessions confirm by session id')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('search matches full text beyond the 160-char snippet', async (t) => {
|
|
78
|
+
const { ctx } = setup(t)
|
|
79
|
+
const result = await _internals.opsSearch(ctx, SID, 'needle-inside')
|
|
80
|
+
assert.equal(result.ok, true)
|
|
81
|
+
assert.equal(result.total, 1)
|
|
82
|
+
assert.equal(result.results[0].seq, 2)
|
|
83
|
+
assert.equal(result.results[0].role, 'assistant')
|
|
84
|
+
assert.equal(result.results[0].shadowed, true)
|
|
85
|
+
assert.ok(result.results[0].snippet.includes('needle-inside'))
|
|
86
|
+
assert.ok(result.results[0].snippet.length <= 160)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('search supports role filtering and limit metadata', async (t) => {
|
|
90
|
+
const { ctx } = setup(t)
|
|
91
|
+
const assistant = await _internals.opsSearch(ctx, SID, 'needle', { role: 'assistant' })
|
|
92
|
+
const user = await _internals.opsSearch(ctx, SID, 'needle', { role: 'user' })
|
|
93
|
+
assert.equal(assistant.total, 1)
|
|
94
|
+
assert.equal(user.total, 0)
|
|
95
|
+
const limited = await _internals.opsSearch(ctx, SID, 'A', { limit: 1 })
|
|
96
|
+
assert.equal(limited.results.length, 1)
|
|
97
|
+
assert.equal(limited.truncated, true)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('search rejects empty and overlong queries', async (t) => {
|
|
101
|
+
const { ctx } = setup(t)
|
|
102
|
+
await assert.rejects(() => _internals.opsSearch(ctx, SID, ' '), (e) => e.status === 400)
|
|
103
|
+
await assert.rejects(() => _internals.opsSearch(ctx, SID, 'x'.repeat(501)), (e) => e.status === 400)
|
|
104
|
+
})
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session trash contract (TDD).
|
|
3
|
+
*
|
|
4
|
+
* The trash is a reversible move: the session directory is renamed into
|
|
5
|
+
* $DSH_HOME/trash/session-ops/<trashId>/data/... and the projection/workspace
|
|
6
|
+
* metadata is captured in a manifest before being removed from the live
|
|
7
|
+
* domains. Restore must put all of it back; purge must be explicit.
|
|
8
|
+
*/
|
|
9
|
+
import test from 'node:test'
|
|
10
|
+
import assert from 'node:assert/strict'
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import os from 'node:os'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
deleteSessionToTrash,
|
|
17
|
+
listTrash,
|
|
18
|
+
restoreTrash,
|
|
19
|
+
purgeTrash,
|
|
20
|
+
trashRoot,
|
|
21
|
+
} from '../src/session-trash.js'
|
|
22
|
+
|
|
23
|
+
const SID = 'session-11111111-1111-4111-8111-111111111111'
|
|
24
|
+
const OTHER = 'session-22222222-2222-4222-8222-222222222222'
|
|
25
|
+
|
|
26
|
+
function makeCtx({ running = false } = {}) {
|
|
27
|
+
const proj = new Map([[SID, {
|
|
28
|
+
identity: { createdAt: 1790000000000 },
|
|
29
|
+
rows: { title: { val: '会话标题' } },
|
|
30
|
+
}]])
|
|
31
|
+
const workspaces = {
|
|
32
|
+
w1: { path: '/tmp/project', title: '项目', sessionIds: [SID, OTHER] },
|
|
33
|
+
w2: { path: '/tmp/other', title: 'Other', sessionIds: [OTHER] },
|
|
34
|
+
}
|
|
35
|
+
const global = {
|
|
36
|
+
initialized: true,
|
|
37
|
+
archivedSessionIds: [SID],
|
|
38
|
+
pinnedSessionIds: [SID],
|
|
39
|
+
}
|
|
40
|
+
const projTable = {
|
|
41
|
+
get: (k) => proj.get(k),
|
|
42
|
+
delete: async (k) => { proj.delete(k) },
|
|
43
|
+
put: async (k, v) => { proj.set(k, v) },
|
|
44
|
+
entries: () => proj.entries(),
|
|
45
|
+
}
|
|
46
|
+
const wsTable = {
|
|
47
|
+
entries: () => Object.entries(workspaces),
|
|
48
|
+
put: async (k, v) => { workspaces[k] = v },
|
|
49
|
+
}
|
|
50
|
+
const sd = {
|
|
51
|
+
get(name) {
|
|
52
|
+
if (name === 'session_projcache') return { table: () => projTable }
|
|
53
|
+
if (name === 'workspace') return { table: () => wsTable, global: { get: () => global, set: async (v) => Object.assign(global, v) } }
|
|
54
|
+
return undefined
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
const ctx = {
|
|
58
|
+
get(name) {
|
|
59
|
+
if (name === 'storageDomain') return sd
|
|
60
|
+
if (name === 'agents') return { get: () => (running ? { status: 'running' } : undefined) }
|
|
61
|
+
if (name === 'sessions') return { get: () => undefined, flush: async () => {} }
|
|
62
|
+
return undefined
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
return { ctx, proj, workspaces, global }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function setup(t) {
|
|
69
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'mops-trash-'))
|
|
70
|
+
const prev = process.env.DSH_HOME
|
|
71
|
+
process.env.DSH_HOME = home
|
|
72
|
+
const dir = path.join(home, 'sessions', '--tmp-project--', SID)
|
|
73
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
74
|
+
fs.writeFileSync(path.join(dir, 'session.v4.jsonl.zstd'), 'fake-frame')
|
|
75
|
+
const env = makeCtx()
|
|
76
|
+
t.after(() => {
|
|
77
|
+
if (prev === undefined) delete process.env.DSH_HOME
|
|
78
|
+
else process.env.DSH_HOME = prev
|
|
79
|
+
fs.rmSync(home, { recursive: true, force: true })
|
|
80
|
+
})
|
|
81
|
+
return { home, dir, ...env }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
test('delete moves the session directory into trash and strips metadata', async (t) => {
|
|
85
|
+
const { ctx, dir, proj, workspaces, global } = setup(t)
|
|
86
|
+
const result = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
87
|
+
|
|
88
|
+
assert.equal(result.ok, true)
|
|
89
|
+
assert.match(result.trashId, /^\d+-[0-9a-f]{8}$/)
|
|
90
|
+
assert.equal(fs.existsSync(dir), false, 'source session dir must be moved away')
|
|
91
|
+
assert.equal(proj.has(SID), false, 'projection row must be removed')
|
|
92
|
+
assert.deepEqual(workspaces.w1.sessionIds, [OTHER], 'workspace membership must be stripped')
|
|
93
|
+
assert.deepEqual(global.archivedSessionIds, [], 'archived marker must be stripped')
|
|
94
|
+
assert.deepEqual(global.pinnedSessionIds, [], 'pinned marker must be stripped')
|
|
95
|
+
|
|
96
|
+
const manifestPath = path.join(trashRoot(), result.trashId, 'manifest.json')
|
|
97
|
+
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
|
|
98
|
+
assert.equal(manifest.state, 'trashed')
|
|
99
|
+
assert.equal(manifest.sessionId, SID)
|
|
100
|
+
assert.equal(manifest.title, '会话标题')
|
|
101
|
+
assert.ok(manifest.projRow, 'projection row must be captured for restore')
|
|
102
|
+
assert.deepEqual(manifest.workspaceIds, ['w1'])
|
|
103
|
+
assert.equal(manifest.archived, true)
|
|
104
|
+
assert.equal(manifest.pinned, true)
|
|
105
|
+
assert.ok(fs.existsSync(path.join(trashRoot(), result.trashId, 'data', '--tmp-project--', SID, 'session.v4.jsonl.zstd')))
|
|
106
|
+
|
|
107
|
+
const entries = await listTrash(ctx)
|
|
108
|
+
assert.equal(entries.length, 1)
|
|
109
|
+
assert.equal(entries[0].trashId, result.trashId)
|
|
110
|
+
assert.equal(entries[0].title, '会话标题')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('restore puts the directory and all metadata back', async (t) => {
|
|
114
|
+
const { ctx, dir, proj, workspaces, global } = setup(t)
|
|
115
|
+
const { trashId } = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
116
|
+
|
|
117
|
+
await restoreTrash(ctx, trashId)
|
|
118
|
+
|
|
119
|
+
assert.equal(fs.existsSync(path.join(dir, 'session.v4.jsonl.zstd')), true)
|
|
120
|
+
assert.equal(proj.has(SID), true)
|
|
121
|
+
assert.equal(proj.get(SID).rows.title.val, '会话标题')
|
|
122
|
+
assert.ok(workspaces.w1.sessionIds.includes(SID))
|
|
123
|
+
assert.ok(global.archivedSessionIds.includes(SID))
|
|
124
|
+
assert.ok(global.pinnedSessionIds.includes(SID))
|
|
125
|
+
assert.deepEqual(await listTrash(ctx), [])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('delete refuses a running session', async (t) => {
|
|
129
|
+
const { home } = setup(t)
|
|
130
|
+
const { ctx } = makeCtx({ running: true })
|
|
131
|
+
await assert.rejects(
|
|
132
|
+
() => deleteSessionToTrash(ctx, SID, '会话标题'),
|
|
133
|
+
(e) => e.status === 409 && /running/i.test(e.message),
|
|
134
|
+
)
|
|
135
|
+
assert.equal(fs.existsSync(path.join(home, 'sessions', '--tmp-project--', SID)), true)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('delete requires the exact title', async (t) => {
|
|
139
|
+
const { ctx, dir } = setup(t)
|
|
140
|
+
await assert.rejects(
|
|
141
|
+
() => deleteSessionToTrash(ctx, SID, 'wrong title'),
|
|
142
|
+
(e) => e.status === 400 && /title/i.test(e.message),
|
|
143
|
+
)
|
|
144
|
+
assert.equal(fs.existsSync(dir), true)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('purge permanently removes a trash entry', async (t) => {
|
|
148
|
+
const { ctx } = setup(t)
|
|
149
|
+
const { trashId } = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
150
|
+
await purgeTrash(ctx, trashId)
|
|
151
|
+
assert.equal(fs.existsSync(path.join(trashRoot(), trashId)), false)
|
|
152
|
+
assert.deepEqual(await listTrash(ctx), [])
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('expired trash entries can be swept without touching fresh ones', async (t) => {
|
|
156
|
+
const { ctx } = setup(t)
|
|
157
|
+
const old = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
158
|
+
const manifestPath = path.join(trashRoot(), old.trashId, 'manifest.json')
|
|
159
|
+
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
|
|
160
|
+
manifest.expiresAt = Date.now() - 1000
|
|
161
|
+
fs.writeFileSync(manifestPath, JSON.stringify(manifest))
|
|
162
|
+
|
|
163
|
+
const entries = await listTrash(ctx, { purgeExpired: true })
|
|
164
|
+
assert.deepEqual(entries, [])
|
|
165
|
+
assert.equal(fs.existsSync(path.join(trashRoot(), old.trashId)), false)
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('restore refuses to overwrite an existing session directory', async (t) => {
|
|
169
|
+
const { ctx, dir } = setup(t)
|
|
170
|
+
const { trashId } = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
171
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
172
|
+
fs.writeFileSync(path.join(dir, 'session.v4.jsonl.zstd'), 'new-frame')
|
|
173
|
+
|
|
174
|
+
await assert.rejects(
|
|
175
|
+
() => restoreTrash(ctx, trashId),
|
|
176
|
+
(e) => e.status === 409 && /exists/i.test(e.message),
|
|
177
|
+
)
|
|
178
|
+
assert.equal(fs.readFileSync(path.join(dir, 'session.v4.jsonl.zstd'), 'utf8'), 'new-frame')
|
|
179
|
+
})
|