dsh-seal-scripts 0.5.0 → 0.5.2

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.
@@ -47,9 +47,9 @@ async function cachedDigest(dir) {
47
47
  export async function buildState(deps, dshHome, snapshot = null) {
48
48
  const options = readOptionsSync(dshHome)
49
49
  const persistence = deps.persistence
50
- if (persistence === undefined) throw new Error('会话持久化不可用')
50
+ if (persistence === undefined) throw new Error('session persistence is unavailable')
51
51
  const root = persistence.config?.root
52
- if (typeof root !== 'string' || root === '') throw new Error('会话持久化没有存储根目录')
52
+ if (typeof root !== 'string' || root === '') throw new Error('session persistence has no storage root')
53
53
  // 子代理会话不列:它们的 cwd 与主会话相同,混进来只会是一堆同名行。
54
54
  const headers = (await persistence.list())
55
55
  .map((row) => row.header ?? row)
@@ -58,7 +58,7 @@ export async function buildState(deps, dshHome, snapshot = null) {
58
58
 
59
59
  const byCwd = new Map()
60
60
  for (const header of headers) {
61
- const cwd = typeof header.cwd === 'string' ? header.cwd : '(无工作区)'
61
+ const cwd = typeof header.cwd === 'string' ? header.cwd : '(no workspace)'
62
62
  if (!byCwd.has(cwd)) byCwd.set(cwd, [])
63
63
  byCwd.get(cwd).push(header)
64
64
  }
@@ -75,7 +75,7 @@ export async function buildState(deps, dshHome, snapshot = null) {
75
75
  const digestCode = digest === null ? 'no-log' : (digest.ok === false ? digest.code : '')
76
76
  sessions.push({
77
77
  id: header.id,
78
- title: title || (digestCode === 'no-zstd' || digestCode === 'unreadable' ? shortId(header.id) : '空会话'),
78
+ title: title || (digestCode === 'no-zstd' || digestCode === 'unreadable' ? shortId(header.id) : 'empty session'),
79
79
  hasTitle: title !== '',
80
80
  digestCode,
81
81
  userMessages: digest?.userMessages ?? 0,
@@ -88,7 +88,7 @@ export async function buildState(deps, dshHome, snapshot = null) {
88
88
 
89
89
  // 只从载入文件算:以前这里为每份回顾整份 readFile,只为算几个界面从不显示的字段。
90
90
  let memories = []
91
- if (typeof cwd === 'string' && cwd !== '(无工作区)') {
91
+ if (typeof cwd === 'string' && cwd !== '(no workspace)') {
92
92
  const dir = memoryDir(dshHome, cwd)
93
93
  const data = await readAssembly(dir, cwd)
94
94
  const now = new Date()
@@ -112,7 +112,7 @@ export async function buildState(deps, dshHome, snapshot = null) {
112
112
  .sort((a, b) => String(b.compactedAt).localeCompare(String(a.compactedAt)))
113
113
  }
114
114
  // health:拆回顾最近一次成功/失败(F6)。没有记录就是 null,界面不显示那一行。
115
- const health = typeof cwd === 'string' && cwd !== '(无工作区)' ? healthOf(dshHome, cwd) : null
115
+ const health = typeof cwd === 'string' && cwd !== '(no workspace)' ? healthOf(dshHome, cwd) : null
116
116
  workspaces.push({ cwd, sessions, memories, health })
117
117
  }
118
118
  workspaces.sort((a, b) => a.cwd.localeCompare(b.cwd))
@@ -155,7 +155,7 @@ export function routesSessions(route, { dshHome, getDeps, waitForSplit, canLifec
155
155
  if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
156
156
  const body = JSON.parse(await readBody(req) || '{}')
157
157
  if (typeof body.cwd !== 'string' || body.cwd === '') {
158
- respond(res, 400, { error: { code: 'invalid-body', message: '需要 cwd' } }); return
158
+ respond(res, 400, { error: { code: 'invalid-body', message: 'cwd is required' } }); return
159
159
  }
160
160
  // 选项每一次现读:设置页改完立刻重新取,框里的内容与下一轮注入是同一套装配。
161
161
  const options = readOptionsSync(dshHome)
@@ -181,7 +181,7 @@ export function routesSessions(route, { dshHome, getDeps, waitForSplit, canLifec
181
181
  if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
182
182
  const body = JSON.parse(await readBody(req) || '{}')
183
183
  if (canLifecycle === false) {
184
- respond(res, 503, { error: { code: 'host-capability', message: '本宿主缺 agents.create/resume,「删除并回顾」已关闭' } })
184
+ respond(res, 503, { error: { code: 'host-capability', message: 'this host lacks agents.create/resume; "Delete + review" is off' } })
185
185
  return
186
186
  }
187
187
  try {
@@ -8,10 +8,17 @@ import { readOptionsSync } from '../../core/settings.js'
8
8
  import { readLocaleSync } from '../../core/locale.js'
9
9
  import { catalogText, toolSequenceRoot, runsDir } from '../../core/schema.js'
10
10
  import { exportToolSequence, importToolSequence } from '../../core/tool-transfer.js'
11
+ import { clearToolCaches } from '../../core/tool-cache.js'
11
12
  import { readBody, readBodyToFile, respond } from '../http-util.js'
12
13
 
13
14
  const PREFIX = '/api/session-seal-scripts.'
14
15
 
16
+ /**
17
+ * 正在进行的导入条数(0.5.1)。导入会把包临时落在 _runs/ 里,
18
+ * 「清除缓存」看到有人在做就先跳过这些临时包——两边都在同一个宿主进程里,数得准。
19
+ */
20
+ let importsInFlight = 0
21
+
15
22
  /** @param {(path:string, handler:Function) => void} route @param {{dshHome:string, onOptionsChanged?:Function}} deps */
16
23
  export function routesTools(route, { dshHome, onOptionsChanged }) {
17
24
  // 「自创工具」页(0.3.1):工具组清单要注入的那一段原文。
@@ -41,7 +48,7 @@ export function routesTools(route, { dshHome, onOptionsChanged }) {
41
48
  if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
42
49
  const raw = await readBody(req)
43
50
  let body
44
- try { body = JSON.parse(raw || '{}') } catch { respond(res, 400, { error: { code: 'bad-json', message: '请求体不是 JSON' } }); return }
51
+ try { body = JSON.parse(raw || '{}') } catch { respond(res, 400, { error: { code: 'bad-json', message: 'request body is not JSON' } }); return }
45
52
  let packed
46
53
  try {
47
54
  packed = await exportToolSequence({ dshHome, dir: body && body.cwd })
@@ -84,18 +91,19 @@ export function routesTools(route, { dshHome, onOptionsChanged }) {
84
91
  let buffer
85
92
  let tmpFile = ''
86
93
  let bytes = 0
94
+ importsInFlight += 1
87
95
  try {
88
96
  if (ctype.includes('application/json')) {
89
97
  const raw = await readBody(req)
90
98
  let body
91
99
  try { body = JSON.parse(raw || '{}') } catch {
92
- respond(res, 400, { error: { code: 'bad-json', message: '请求体不是 JSON' } })
100
+ respond(res, 400, { error: { code: 'bad-json', message: 'request body is not JSON' } })
93
101
  return
94
102
  }
95
103
  const data = typeof body.data === 'string' ? body.data : ''
96
- if (data === '') { respond(res, 400, { error: { code: 'missing-file', message: '没有收到包内容' } }); return }
97
- try { buffer = Buffer.from(data, 'base64') } catch { respond(res, 400, { error: { code: 'bad-base64', message: '包内容不是合法的 base64' } }); return }
98
- if (buffer.length === 0) { respond(res, 400, { error: { code: 'empty-file', message: '包是空的' } }); return }
104
+ if (data === '') { respond(res, 400, { error: { code: 'missing-file', message: 'no package content received' } }); return }
105
+ try { buffer = Buffer.from(data, 'base64') } catch { respond(res, 400, { error: { code: 'bad-base64', message: 'package content is not valid base64' } }); return }
106
+ if (buffer.length === 0) { respond(res, 400, { error: { code: 'empty-file', message: 'the package is empty' } }); return }
99
107
  bytes = buffer.length
100
108
  if (body.dryRun === true) dryRun = true
101
109
  if (typeof body.name === 'string' && body.name !== '') name = body.name
@@ -107,7 +115,7 @@ export function routesTools(route, { dshHome, onOptionsChanged }) {
107
115
  const written = await readBodyToFile(req, tmpFile)
108
116
  bytes = written.bytes
109
117
  if (bytes === 0) {
110
- respond(res, 400, { error: { code: 'empty-file', message: '包是空的' } })
118
+ respond(res, 400, { error: { code: 'empty-file', message: 'the package is empty' } })
111
119
  return
112
120
  }
113
121
  }
@@ -134,7 +142,34 @@ export function routesTools(route, { dshHome, onOptionsChanged }) {
134
142
  skipped: result.skipped
135
143
  })
136
144
  } finally {
145
+ importsInFlight -= 1
137
146
  if (tmpFile !== '') await fsp.rm(tmpFile, { force: true }).catch(() => {})
138
147
  }
139
148
  })
149
+
150
+ /**
151
+ * 清除缓存(0.5.1):「自创工具」页最右边那颗按钮。
152
+ *
153
+ * 只删两类东西:<id>.bak-<时间戳> 回滚版本、_runs/ 里的运行日志。
154
+ * 工具目录与 _index.json 不动——清完工具照常跑,索引不用重建。
155
+ * 单个条目删不掉不算整轮失败:照样回 200,把 failed 交给界面点名。
156
+ */
157
+ route(PREFIX + 'toolSequence/clear-cache', async (req, res) => {
158
+ if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
159
+ let result
160
+ try {
161
+ result = await clearToolCaches({ dshHome, keepImportTmp: importsInFlight > 0 })
162
+ } catch (error) {
163
+ respond(res, 500, { error: { code: 'clear-failed', message: error?.message ?? String(error) } })
164
+ return
165
+ }
166
+ respond(res, 200, {
167
+ ok: result.failed.length === 0,
168
+ path: toolSequenceRoot(dshHome),
169
+ backups: result.backups,
170
+ runs: result.runs,
171
+ total: result.total,
172
+ failed: result.failed
173
+ })
174
+ })
140
175
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-seal-scripts",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "index.js",