dsh-tiddlywiki 0.33.2 → 0.33.4
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/docs/CHANGELOG.md +172 -199
- package/docs/development.md +1 -1
- package/lib/client.js +6 -6
- package/lib/index.js +474 -189
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/src/client/i18n-frame.ts +4 -2
- package/src/client/i18n.ts +9 -2
- package/src/client/knowledge-fab.ts +7 -4
- package/src/client/note-widget-save.ts +4 -1
- package/src/client/note-widget-scope.ts +16 -1
- package/src/client/note-widget-tags.ts +15 -12
- package/src/client/note-widget.ts +1 -1
- package/src/client/settings-page-catalog.ts +7 -2
- package/src/client/settings-page-runtime.ts +7 -6
- package/src/client/settings-page-wikis.ts +3 -4
- package/src/client/sidebar-entry.ts +4 -0
- package/src/client/status-cache.ts +4 -1
- package/src/client/tid-parse.ts +88 -3
- package/src/client/tid-preview.ts +18 -7
- package/src/client/tool-views-shell.ts +4 -4
- package/src/client/tw-frame-api.ts +11 -4
- package/src/client/tw-frame-hash.ts +7 -1
- package/src/client/tw-frame.ts +11 -4
- package/src/host/admin-routes-info.ts +62 -16
- package/src/host/admin-routes-seeds.ts +22 -12
- package/src/host/admin-routes.ts +29 -13
- package/src/host/clip-bridge-limits.ts +12 -3
- package/src/host/clip-bridge-payload.ts +15 -4
- package/src/host/clip-bridge.ts +20 -3
- package/src/host/command-note.ts +6 -3
- package/src/host/config.ts +7 -2
- package/src/host/git.ts +6 -2
- package/src/host/mutation-lock.ts +22 -4
- package/src/host/routes-session-agent.ts +5 -1
- package/src/host/routes-session-summary.ts +23 -3
- package/src/host/routes-tw-proxy.ts +5 -0
- package/src/host/routes.ts +15 -3
- package/src/host/session-summary-scan.ts +12 -3
- package/src/host/tools-attach.ts +24 -4
- package/src/host/tools-git.ts +1 -1
- package/src/host/tools-notes-lifecycle.ts +53 -22
- package/src/host/tools-notes-structure.ts +11 -6
- package/src/host/tools-notes-write.ts +18 -7
- package/src/host/tools-support.ts +61 -12
- package/src/host/tools.ts +6 -3
- package/src/host/tw-api.ts +5 -1
- package/src/host/wechat-publish.ts +6 -2
- package/src/host/wiki-farm.ts +9 -3
- package/src/host/wiki-instance.ts +14 -0
- package/src/host/wiki.ts +8 -0
- package/src/index-routes.ts +52 -7
- package/src/index-wikis.ts +2 -2
- package/src/index.ts +23 -2
|
@@ -21,11 +21,18 @@ import { join } from 'node:path'
|
|
|
21
21
|
import { drainThenStop } from './seeds.ts'
|
|
22
22
|
import { GitFace } from './git.ts'
|
|
23
23
|
import { maskConfigSecrets } from './admin-secrets.ts'
|
|
24
|
+
import { buildWriteTiddler } from './write-policy.ts'
|
|
24
25
|
import { bundledCatalog, normalizeThemes, pinLanguageTiddler, readActiveThemeName, readWikiInfo, scanWikiRuntimePlugins, writeWikiInfo, type WikiInfo } from './admin-catalog.ts'
|
|
25
26
|
import type { AdminDeps } from './admin-routes.ts'
|
|
26
27
|
|
|
27
|
-
/** 这一组真正用到的 deps 成员(不是整个 AdminDeps)。
|
|
28
|
-
|
|
28
|
+
/** 这一组真正用到的 deps 成员(不是整个 AdminDeps)。
|
|
29
|
+
*
|
|
30
|
+
* v0.33.4:加上 `mutationLock` —— `handleInfo` 也会 `drainThenStop` + 重启子进程
|
|
31
|
+
* (改插件/主题/语言),它是**第二组**会停子进程的 admin 路由,却一直没接共享锁,
|
|
32
|
+
* 于是能和 `/sync`、`/admin/restart` 同时停同一个 child(一方排干还没落地,另一方
|
|
33
|
+
* 就把 child 杀掉 ⇒ 排队写入丢失)。
|
|
34
|
+
*/
|
|
35
|
+
export type AdminInfoRouteDeps = Pick<AdminDeps, 'config' | 'getClient' | 'getWikiPath' | 'server' | 'targetProblem' | 'twRoot' | 'mutationLock'>
|
|
29
36
|
|
|
30
37
|
export function createAdminInfoRoutes(
|
|
31
38
|
deps: AdminInfoRouteDeps,
|
|
@@ -170,27 +177,51 @@ export function createAdminInfoRoutes(
|
|
|
170
177
|
// same lists re-sent) used to rewrite tiddlywiki.info + restart TW — a
|
|
171
178
|
// visible TW interruption for a request that asked for nothing.
|
|
172
179
|
const changed = JSON.stringify(info) !== beforeInfo
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
180
|
+
// v0.33.4: this restart is one of the two admin paths that stop the TW
|
|
181
|
+
// child, so it takes the SHARED mutation lock like `/admin/restart` and
|
|
182
|
+
// the seed run do. `changed === false` never touches the lock.
|
|
183
|
+
const lock = deps.mutationLock
|
|
184
|
+
let lockTaken = false
|
|
185
|
+
if (changed && lock !== undefined) {
|
|
186
|
+
if (!lock.begin('admin-info')) {
|
|
187
|
+
json(res, { ok: false, error: `另一个重启/同步正在进行中(${lock.current() ?? '?'}),请稍候` }, 429)
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
lockTaken = true
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
if (changed) {
|
|
194
|
+
await writeWikiInfo(wikiPath, info)
|
|
195
|
+
// v0.24.1: plugins/themes/languages change → restart, but drain first.
|
|
196
|
+
// This path predates the "restarting over unflushed writes loses them"
|
|
197
|
+
// insight that the seed path below already documents (rule #1).
|
|
198
|
+
await drainThenStop({
|
|
199
|
+
client: deps.getClient(req),
|
|
200
|
+
tiddlersDir: join(deps.getWikiPath(req), 'tiddlers'),
|
|
201
|
+
stop: async () => { await deps.server(req)?.restart() },
|
|
202
|
+
log: (message) => console.warn('[dsh-tiddlywiki]', message),
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
} finally {
|
|
206
|
+
if (lockTaken) lock?.end('admin-info')
|
|
184
207
|
}
|
|
185
208
|
// Activate the chosen theme tiddler (mirrors TW's own Control Panel).
|
|
186
209
|
if (Array.isArray(body.themes) && activatedTheme !== undefined) {
|
|
187
210
|
const client = deps.getClient(req)
|
|
188
211
|
if (client !== undefined) {
|
|
212
|
+
// Ironclad rule #2 (v0.33.4): `$:/theme` is an EXISTING tiddler, so a
|
|
213
|
+
// hand-built body silently dropped its custom fields and forced
|
|
214
|
+
// `tags: []`. Read it first and go through the shared write policy —
|
|
215
|
+
// exactly what `$:/language` was fixed to do in v0.30.30.
|
|
216
|
+
const existingTheme = await client.get('$:/theme')
|
|
217
|
+
const { tiddler: themeTiddler } = buildWriteTiddler('$:/theme', `$:/themes/${activatedTheme}`, { existing: existingTheme })
|
|
189
218
|
await client
|
|
190
|
-
.put(
|
|
219
|
+
.put(themeTiddler)
|
|
191
220
|
.catch((err) => { console.warn('[dsh-tiddlywiki] activating theme failed:', err) })
|
|
192
221
|
}
|
|
193
222
|
}
|
|
223
|
+
// Set when the uiLanguage hint could not be persisted (v0.33.4).
|
|
224
|
+
let languageHintError: string | undefined
|
|
194
225
|
// After a languages change, pin the active language tiddler: first
|
|
195
226
|
// enabled language, or en-GB when none is enabled. Only when the request
|
|
196
227
|
// actually carried a languages array (plugins/themes restarts skip this).
|
|
@@ -211,11 +242,26 @@ export function createAdminInfoRoutes(
|
|
|
211
242
|
// a language the user just disabled here.
|
|
212
243
|
const hint = langs.length > 0 ? langs[0] : ''
|
|
213
244
|
if ((deps.config(req).get().uiLanguage ?? '') !== hint) {
|
|
214
|
-
|
|
245
|
+
// v0.33.4: do NOT swallow this. The whole point of the write is that
|
|
246
|
+
// the next dsh-web restart must not re-enable the language the user
|
|
247
|
+
// just turned off; `ConfigUnreadableError` silently discarded made
|
|
248
|
+
// that benefit vanish with the response still saying ok:true. Report
|
|
249
|
+
// it, keep the request successful (the language pin already landed).
|
|
250
|
+
try {
|
|
251
|
+
await deps.config(req).set(client, { uiLanguage: hint })
|
|
252
|
+
} catch (err) {
|
|
253
|
+
languageHintError = `语言提示没写进配置(下次 dsh web 启动可能又启用「${hint === '' ? '默认语言' : hint}」):${err instanceof Error ? err.message : String(err)}`
|
|
254
|
+
console.warn('[dsh-tiddlywiki]', languageHintError)
|
|
255
|
+
}
|
|
215
256
|
}
|
|
216
257
|
}
|
|
217
258
|
}
|
|
218
|
-
json(res, {
|
|
259
|
+
json(res, {
|
|
260
|
+
ok: true,
|
|
261
|
+
changed,
|
|
262
|
+
info: { plugins: info.plugins, themes: info.themes, languages: info.languages ?? [] },
|
|
263
|
+
...(languageHintError === undefined ? {} : { warning: languageHintError }),
|
|
264
|
+
})
|
|
219
265
|
} catch (err) {
|
|
220
266
|
json(res, { ok: false, error: err instanceof Error ? err.message : String(err) }, 500)
|
|
221
267
|
}
|
|
@@ -44,7 +44,7 @@ export function createAdminSeedRoutes(
|
|
|
44
44
|
json(res, { ok: false, error: notRunning(req) }, 503)
|
|
45
45
|
return
|
|
46
46
|
}
|
|
47
|
-
const items = await deps.seeds.checkAll(client)
|
|
47
|
+
const items = await deps.seeds.checkAll(client, req)
|
|
48
48
|
json(res, { ok: true, items })
|
|
49
49
|
} catch (err) {
|
|
50
50
|
json(res, { ok: false, error: err instanceof Error ? err.message : String(err) }, 500)
|
|
@@ -68,7 +68,7 @@ export function createAdminSeedRoutes(
|
|
|
68
68
|
return
|
|
69
69
|
}
|
|
70
70
|
const seedStartedAt = Date.now()
|
|
71
|
-
const results = await deps.seeds.run(client, id, force)
|
|
71
|
+
const results = await deps.seeds.run(client, id, force, req)
|
|
72
72
|
// A seeded SERVER-route plugin (render) only loads at TW BOOT, and TW's
|
|
73
73
|
// syncer flushes REST writes on a ~250ms timer. Without the flush-wait +
|
|
74
74
|
// restart the settings page reported「已重新初始化」while /render kept
|
|
@@ -81,13 +81,22 @@ export function createAdminSeedRoutes(
|
|
|
81
81
|
// v0.30.12:这一段会 drain + 重启 TW 子进程,所以必须拿**共享**的变更锁
|
|
82
82
|
// (否则设置页「初始化」与 `/sync`、`/admin/restart` 会同时停子进程)。
|
|
83
83
|
// 拿不到就如实报冲突,而不是排队硬上。
|
|
84
|
+
// v0.33.4:`end()` 只在真的拿到锁时才调,并且带上自己的 label —— 以前
|
|
85
|
+
// `seedLock?.end()` 在**每条**响应上都执行(最常见的「重新初始化但没改任何
|
|
86
|
+
// 东西」路径根本没 begin),于是会把此刻正持锁的 `/sync` 释放掉,让
|
|
87
|
+
// `/admin/restart` 拿到锁、两条路径同时排干 + 停同一个子进程。
|
|
84
88
|
const seedLock = deps.mutationLock
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
const needsRestart = needsRestartAfterSeeds(results)
|
|
90
|
+
let lockTaken = false
|
|
91
|
+
if (needsRestart && seedLock !== undefined) {
|
|
92
|
+
if (!seedLock.begin('admin-seeds')) {
|
|
93
|
+
json(res, { ok: false, error: `另一个重启/同步正在进行中(${seedLock.current() ?? '?'}),请稍候` }, 429)
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
lockTaken = true
|
|
88
97
|
}
|
|
89
|
-
|
|
90
|
-
|
|
98
|
+
try {
|
|
99
|
+
if (needsRestart) {
|
|
91
100
|
const flushed = await waitForFileWrite(join(deps.getWikiPath(req), 'tiddlers', RENDER_PLUGIN_FILE), 8_000, 150, seedStartedAt)
|
|
92
101
|
if (!flushed) console.warn('[dsh-tiddlywiki] seeded render plugin file not seen on disk before restart')
|
|
93
102
|
// Drain the rest of the syncer queue too: force-all writes every seed,
|
|
@@ -102,13 +111,14 @@ export function createAdminSeedRoutes(
|
|
|
102
111
|
})
|
|
103
112
|
if (!drained) console.warn('[dsh-tiddlywiki] seed writes may not have been flushed before restart')
|
|
104
113
|
restarted = true
|
|
105
|
-
} catch (err) {
|
|
106
|
-
restartError = err instanceof Error ? err.message : String(err)
|
|
107
|
-
console.warn('[dsh-tiddlywiki] restart after seeding failed:', restartError)
|
|
108
114
|
}
|
|
115
|
+
} catch (err) {
|
|
116
|
+
restartError = err instanceof Error ? err.message : String(err)
|
|
117
|
+
console.warn('[dsh-tiddlywiki] restart after seeding failed:', restartError)
|
|
118
|
+
} finally {
|
|
119
|
+
if (lockTaken) seedLock?.end('admin-seeds')
|
|
109
120
|
}
|
|
110
121
|
const ok = results.every((r) => r.ok)
|
|
111
|
-
seedLock?.end()
|
|
112
122
|
json(res, { ok, results, restarted, ...(restartError !== undefined ? { restartError } : {}) }, ok ? 200 : 400)
|
|
113
123
|
} catch (err) {
|
|
114
124
|
json(res, { ok: false, error: err instanceof Error ? err.message : String(err) }, 500)
|
|
@@ -131,7 +141,7 @@ export function createAdminSeedRoutes(
|
|
|
131
141
|
json(res, { ok: false, error: notRunning(req) }, 503)
|
|
132
142
|
return
|
|
133
143
|
}
|
|
134
|
-
const results = await deps.seeds.remove(client, id)
|
|
144
|
+
const results = await deps.seeds.remove(client, id, req)
|
|
135
145
|
const ok = results.every((r) => r.ok)
|
|
136
146
|
json(res, { ok, results }, ok ? 200 : 400)
|
|
137
147
|
} catch (err) {
|
package/src/host/admin-routes.ts
CHANGED
|
@@ -104,10 +104,17 @@ export interface AdminDeps {
|
|
|
104
104
|
* content hashes (v0.22.0): the page shows 「有更新」 and refuses to imply a
|
|
105
105
|
* local edit was checked for when the marker predates hashes.
|
|
106
106
|
*/
|
|
107
|
-
checkAll: (client: TiddlyWebClient) => Promise<Array<{ id: string; title: string; description: string; present: boolean; removable: boolean; detail?: string; updateAvailable?: boolean; userModified?: boolean; legacyMarker?: boolean }>>
|
|
108
|
-
|
|
107
|
+
checkAll: (client: TiddlyWebClient, req?: IncomingMessage) => Promise<Array<{ id: string; title: string; description: string; present: boolean; removable: boolean; detail?: string; updateAvailable?: boolean; userModified?: boolean; legacyMarker?: boolean }>>
|
|
108
|
+
/**
|
|
109
|
+
* v0.33.4: carries the REQUEST so the wiring can hand the seed context the
|
|
110
|
+
* TARGET wiki's same-origin proxy base (the `tw-web-host` seed writes it).
|
|
111
|
+
* This path used to omit it, so「重新初始化」on a multi-wiki install rewrote
|
|
112
|
+
* `$:/config/tiddlyweb/host` to the bare legacy path and that wiki's embedded
|
|
113
|
+
* editor then proxied every API call to the DEFAULT wiki.
|
|
114
|
+
*/
|
|
115
|
+
run: (client: TiddlyWebClient, id: string | undefined, force: boolean, req?: IncomingMessage) => Promise<Array<{ id: string; ok: boolean; wrote: boolean; detail?: string; error?: string }>>
|
|
109
116
|
/** 反初始化: remove one (or all) optional seed's seeded tiddlers + markers. */
|
|
110
|
-
remove: (client: TiddlyWebClient, id: string | undefined) => Promise<Array<{ id: string; ok: boolean; wrote: boolean; detail?: string; error?: string }>>
|
|
117
|
+
remove: (client: TiddlyWebClient, id: string | undefined, req?: IncomingMessage) => Promise<Array<{ id: string; ok: boolean; wrote: boolean; detail?: string; error?: string }>>
|
|
111
118
|
}
|
|
112
119
|
}
|
|
113
120
|
|
|
@@ -205,6 +212,14 @@ export function registerAdminRoutes(ctx: { webServer: WebServerFace }, deps: Adm
|
|
|
205
212
|
// that restarted nothing while the settings page scoped to that wiki
|
|
206
213
|
// believed it had.
|
|
207
214
|
if (refuseStoppedTarget(req, res)) return
|
|
215
|
+
// v0.33.4: no child behind this request ⇒ nothing to restart. The drain
|
|
216
|
+
// helper short-circuits on `client: undefined` and `server(req)?.restart()`
|
|
217
|
+
// swallows itself, so this route answered `{ok:true,drained:true}` while
|
|
218
|
+
// restarting nothing — the same fake success `/restart` used to return.
|
|
219
|
+
if (deps.server(req) === undefined) {
|
|
220
|
+
json(res, { ok: false, error: notRunning(req) }, 503)
|
|
221
|
+
return
|
|
222
|
+
}
|
|
208
223
|
// v0.30.12: take the SHARED mutation lock. Before this, the settings page
|
|
209
224
|
// could press 「重启」 while a `/sync` was draining the same queue (or while
|
|
210
225
|
// another restart was in flight) — the two operations both stop the TW child.
|
|
@@ -214,17 +229,18 @@ export function registerAdminRoutes(ctx: { webServer: WebServerFace }, deps: Adm
|
|
|
214
229
|
return
|
|
215
230
|
}
|
|
216
231
|
try {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
232
|
+
// v0.24.1: drain the syncer queue first (ironclad rule #1) — this route
|
|
233
|
+
// used to kill the child with writes still queued, losing them silently.
|
|
234
|
+
const drained = await drainThenStop({
|
|
235
|
+
client: deps.getClient(req),
|
|
236
|
+
tiddlersDir: join(deps.getWikiPath(req), 'tiddlers'),
|
|
237
|
+
stop: async () => { await deps.server(req)?.restart() },
|
|
238
|
+
log: (message) => console.warn('[dsh-tiddlywiki]', message),
|
|
239
|
+
})
|
|
240
|
+
json(res, { ok: true, status: deps.server(req)?.status().status, drained })
|
|
226
241
|
} finally {
|
|
227
|
-
lock
|
|
242
|
+
// v0.33.4: labelled release — a lock we never took cannot be freed here.
|
|
243
|
+
lock?.end('admin-restart')
|
|
228
244
|
}
|
|
229
245
|
} catch (err) {
|
|
230
246
|
json(res, { ok: false, error: err instanceof Error ? err.message : String(err) }, 500)
|
|
@@ -7,10 +7,19 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @module dsh-tiddlywiki/host/clip-bridge-limits
|
|
9
9
|
*/
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Cap on a single /clip request body (bytes).
|
|
12
|
+
*
|
|
13
|
+
* The text cap below is derived from THIS number, not chosen independently
|
|
14
|
+
* (v0.33.4): the old pair (200 000 characters vs 256 KiB) was self-contradictory
|
|
15
|
+
* — CJK text is 3 bytes per character in UTF-8, so ~600 KB was "allowed" by the
|
|
16
|
+
* character cap and killed by the body cap with「请求体不是合法的 JSON(或过大)」.
|
|
17
|
+
* 60 000 characters stays under 256 KiB even if every character is 3 bytes
|
|
18
|
+
* (180 KB) plus JSON escaping and the url/title/tag fields.
|
|
19
|
+
*/
|
|
13
20
|
export const MAX_CLIP_BODY_BYTES = 256 * 1024
|
|
21
|
+
/** Cap on the clipped text (characters) — plenty for a page selection. */
|
|
22
|
+
export const MAX_CLIP_TEXT_LENGTH = 60_000
|
|
14
23
|
/** Cap on one downloaded image (bytes) — 15 MB keeps the wiki sane. */
|
|
15
24
|
export const MAX_IMAGE_BYTES = 15 * 1024 * 1024
|
|
16
25
|
/** Max images stored per clip (the picker already caps the page list). */
|
|
@@ -41,13 +41,24 @@ export async function resolveClipTitle(
|
|
|
41
41
|
if (!(await exists(candidate))) return candidate
|
|
42
42
|
}
|
|
43
43
|
const stamp = new Date(now()).toISOString().slice(0, 23).replace(/[T:]/g, '-')
|
|
44
|
-
let candidate = `${base}(${stamp})`
|
|
45
44
|
// 同一毫秒内再撞(连点、或书签被跑了两遍)时继续加序号 —— 每一轮都真的问一次
|
|
46
45
|
// `exists`,绝不因为「时间戳不同了」就假设名字空着。
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
//
|
|
47
|
+
// v0.33.4: the old loop assigned the NEXT candidate AFTER the probe, so when it
|
|
48
|
+
// ran out of rounds it returned a name that had never been checked — the one
|
|
49
|
+
// thing this loop exists to guarantee. Probe each candidate BEFORE returning it.
|
|
50
|
+
for (let i = 0; i < 100; i++) {
|
|
51
|
+
const candidate = i === 0 ? `${base}(${stamp})` : `${base}(${stamp}-${i})`
|
|
52
|
+
if (!(await exists(candidate))) return candidate
|
|
53
|
+
}
|
|
54
|
+
// 100 collisions on one millisecond stamp should be impossible; if it ever
|
|
55
|
+
// happens, a random suffix is the only name that cannot be predicted — probed
|
|
56
|
+
// like every other candidate.
|
|
57
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
58
|
+
const candidate = `${base}(${stamp}-${Math.random().toString(36).slice(2, 8)})`
|
|
59
|
+
if (!(await exists(candidate))) return candidate
|
|
49
60
|
}
|
|
50
|
-
|
|
61
|
+
throw new Error('剪藏标题生成失败:同一毫秒内连续 200 次撞名,请稍后重试')
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
/** Build the markdown tiddler for ONE TEXT-ONLY clip (no images). */
|
package/src/host/clip-bridge.ts
CHANGED
|
@@ -46,7 +46,7 @@ import { createServer, type IncomingMessage, type Server, type ServerResponse }
|
|
|
46
46
|
import type { AddressInfo } from 'node:net'
|
|
47
47
|
import type { Tiddler } from './tw-api.ts'
|
|
48
48
|
import { readBody, safeTokenEqual } from './http.ts'
|
|
49
|
-
import { MAX_CLIP_BODY_BYTES, MAX_IMAGE_BYTES } from './clip-bridge-limits.ts'
|
|
49
|
+
import { MAX_CLIP_BODY_BYTES, MAX_CLIP_TEXT_LENGTH, MAX_IMAGE_BYTES } from './clip-bridge-limits.ts'
|
|
50
50
|
import { assertPublicImageUrl } from './clip-bridge-ssrf.ts'
|
|
51
51
|
|
|
52
52
|
// The v0.30.0 split must be INVISIBLE to callers: `src/index.ts` re-exports
|
|
@@ -331,10 +331,27 @@ export class ClipBridge {
|
|
|
331
331
|
}
|
|
332
332
|
|
|
333
333
|
let body: unknown
|
|
334
|
+
// v0.33.4: split「too large」from「not JSON」. The old single catch told a user
|
|
335
|
+
// whose selection was simply too big that their bookmarket sent invalid JSON,
|
|
336
|
+
// which points at the wrong thing entirely. `readBody` rejects with exactly
|
|
337
|
+
// `body too large` (host/http.ts).
|
|
338
|
+
let rawBody: string
|
|
334
339
|
try {
|
|
335
|
-
|
|
340
|
+
rawBody = await readBody(req, MAX_CLIP_BODY_BYTES)
|
|
341
|
+
} catch (err) {
|
|
342
|
+
const tooLarge = err instanceof Error && /body too large/.test(err.message)
|
|
343
|
+
this.respond(res, {
|
|
344
|
+
ok: false,
|
|
345
|
+
error: tooLarge
|
|
346
|
+
? `请求体超过 ${Math.floor(MAX_CLIP_BODY_BYTES / 1024)}KB 上限(正文最多 ${MAX_CLIP_TEXT_LENGTH} 字符);请减少选中内容后重试`
|
|
347
|
+
: '请求体读取失败,请重试',
|
|
348
|
+
}, tooLarge ? 413 : 400)
|
|
349
|
+
return
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
body = JSON.parse(rawBody) as unknown
|
|
336
353
|
} catch {
|
|
337
|
-
this.respond(res, { ok: false, error: '请求体不是合法的 JSON
|
|
354
|
+
this.respond(res, { ok: false, error: '请求体不是合法的 JSON' }, 400)
|
|
338
355
|
return
|
|
339
356
|
}
|
|
340
357
|
const parsed = parseClipPayload(body)
|
package/src/host/command-note.ts
CHANGED
|
@@ -43,7 +43,7 @@ export interface NoteCommandDeps {
|
|
|
43
43
|
workspaceName?: (sessionId: string) => string | undefined
|
|
44
44
|
workspaceMarkEnabled?: () => boolean
|
|
45
45
|
/** Debounced auto-commit ping (the same one the write tools fire). */
|
|
46
|
-
autoCommit: () => void
|
|
46
|
+
autoCommit: (sessionId?: string) => void
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/** The command's stable identity (also what `/help` lists). */
|
|
@@ -110,7 +110,10 @@ export function registerNoteCommand(commands: CommandRegistryFace, deps: NoteCom
|
|
|
110
110
|
text: `「${parsed.title}」已经存在(${scope.label ?? '当前知识库'})。要改请用编辑器,或让 Agent 用 tiddlywiki_get + tiddlywiki_put 按并发令牌更新;换标题再加一次也可以。`,
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
|
|
113
|
+
// 默认开启(与 put/append 同一口径:只有显式 `=== false` 才关)。旧写法是
|
|
114
|
+
// `=== true`,于是 deps 未注入(headless / 宿主换实现)时 `/note` 写出的笔记
|
|
115
|
+
// 静默缺 `ws/<项目>` 标记与 `workspace` 字段,而同一篇笔记用 put 写就有。
|
|
116
|
+
const mark = deps.workspaceMarkEnabled?.() !== false && sessionId !== undefined
|
|
114
117
|
? workspaceMarkFromCwd(deps.workspaceName?.(sessionId))
|
|
115
118
|
: undefined
|
|
116
119
|
const fields: Record<string, unknown> = {}
|
|
@@ -123,7 +126,7 @@ export function registerNoteCommand(commands: CommandRegistryFace, deps: NoteCom
|
|
|
123
126
|
// `agent-written` —— 那个标签的含义是"Agent 写的,人可以放心改"。
|
|
124
127
|
const { tiddler } = buildWriteTiddler(parsed.title, parsed.body, { tags, fields, agentTag: false })
|
|
125
128
|
await scope.client.put(tiddler)
|
|
126
|
-
deps.autoCommit()
|
|
129
|
+
deps.autoCommit(sessionId)
|
|
127
130
|
const where = scope.label !== undefined ? `(${scope.label})` : ''
|
|
128
131
|
return { kind: 'success', text: `已写入「${parsed.title}」${where}` }
|
|
129
132
|
},
|
package/src/host/config.ts
CHANGED
|
@@ -380,8 +380,13 @@ export class ConfigStore {
|
|
|
380
380
|
*/
|
|
381
381
|
async load(client: TiddlyWebClient | undefined): Promise<void> {
|
|
382
382
|
if (client === undefined) {
|
|
383
|
-
|
|
384
|
-
|
|
383
|
+
// v0.33.4: a TRUE no-op, as the docblock above always claimed. The old
|
|
384
|
+
// body cleared `overrides` while leaving `loaded = true`, and `set()` only
|
|
385
|
+
// refuses to write while `loaded` is false — so the next `set()` merged a
|
|
386
|
+
// patch into an EMPTY cache and persisted a config tiddler containing just
|
|
387
|
+
// that patch, silently dropping every other user setting (the accident
|
|
388
|
+
// `loaded` exists to prevent). Keeping the cache also matches how a
|
|
389
|
+
// transient read failure is handled two branches below.
|
|
385
390
|
return
|
|
386
391
|
}
|
|
387
392
|
let tiddler
|
package/src/host/git.ts
CHANGED
|
@@ -339,8 +339,12 @@ export class GitFace {
|
|
|
339
339
|
}
|
|
340
340
|
}
|
|
341
341
|
const conflictFiles = await this.unmergedFiles(dir)
|
|
342
|
-
// Only abort
|
|
343
|
-
//
|
|
342
|
+
// Only abort a rebase WE started (none was in progress before this pull).
|
|
343
|
+
// v0.33.4: the old comment described an OR条件 ("…or one was already there
|
|
344
|
+
// but the failure produced unmerged paths of ours") that was never
|
|
345
|
+
// implemented — and should NOT be: when a rebase predates us, `git pull`
|
|
346
|
+
// refuses up front and leaves no new conflict, so aborting would throw away
|
|
347
|
+
// the user's own in-progress rebase.
|
|
344
348
|
const rebaseNow = await this.hasRebaseInProgress(dir)
|
|
345
349
|
const ourRebase = rebaseNow && !preexistingRebase
|
|
346
350
|
if (ourRebase) {
|
|
@@ -18,7 +18,16 @@
|
|
|
18
18
|
*
|
|
19
19
|
* SEMANTICS: `begin()` returns false when something else holds it (the caller
|
|
20
20
|
* answers 409 with the holder's label, so a user sees 「同步进行中」 instead of a
|
|
21
|
-
* mystery failure). `end()`
|
|
21
|
+
* mystery failure). `end(label)` releases ONLY when `label` is the current
|
|
22
|
+
* holder, so "always call it in a `finally`" is safe: a handler whose `begin()`
|
|
23
|
+
* never ran (or failed) cannot hand someone else's lock to the next caller.
|
|
24
|
+
*
|
|
25
|
+
* ⚠️ v0.33.4 — `end()` used to clear the holder unconditionally, and
|
|
26
|
+
* `admin-routes-seeds` calls it on EVERY response. The common path there
|
|
27
|
+
* (「重新初始化」 that rewrote nothing ⇒ no restart ⇒ `begin()` never ran) then
|
|
28
|
+
* freed a `/sync` lock mid-flight, letting `/admin/restart` start a second
|
|
29
|
+
* drain + child stop on the same queue — the exact write-loss overlap v0.30.12
|
|
30
|
+
* was built to prevent. Hence the label check (and a guard case for it).
|
|
22
31
|
*
|
|
23
32
|
* @module dsh-tiddlywiki/host/mutation-lock
|
|
24
33
|
*/
|
|
@@ -29,8 +38,12 @@ export interface MutationLock {
|
|
|
29
38
|
* mutation is in flight; `current()` then names it.
|
|
30
39
|
*/
|
|
31
40
|
begin: (label: string) => boolean
|
|
32
|
-
/**
|
|
33
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Release it. Pass the SAME label the `begin()` was called with: a mismatched
|
|
43
|
+
* label is ignored, so releasing is safe in a `finally` even when `begin()`
|
|
44
|
+
* returned false (or was never reached).
|
|
45
|
+
*/
|
|
46
|
+
end: (label?: string) => void
|
|
34
47
|
/** The label of the mutation currently in flight, if any. */
|
|
35
48
|
current: () => string | undefined
|
|
36
49
|
}
|
|
@@ -43,7 +56,12 @@ export function createMutationLock(): MutationLock {
|
|
|
43
56
|
inFlight = label
|
|
44
57
|
return true
|
|
45
58
|
},
|
|
46
|
-
end: (): void => {
|
|
59
|
+
end: (label?: string): void => {
|
|
60
|
+
// `label === undefined` keeps the pre-v0.33.4 call shape working for a
|
|
61
|
+
// harness that holds the only lock it knows about. When a label IS given
|
|
62
|
+
// it must match the holder, which is what makes `finally` safe: a
|
|
63
|
+
// handler whose `begin()` never ran cannot free someone else's lock.
|
|
64
|
+
if (label !== undefined && inFlight !== label) return
|
|
47
65
|
inFlight = undefined
|
|
48
66
|
},
|
|
49
67
|
current: (): string | undefined => inFlight,
|
|
@@ -198,7 +198,11 @@ export function createAgentHandlers(deps: AgentRouteDeps): AgentRouteHandlers {
|
|
|
198
198
|
AbortSignal.timeout(20_000),
|
|
199
199
|
)
|
|
200
200
|
// A refusal (session gone / busy) must not be reported as success.
|
|
201
|
-
|
|
201
|
+
// v0.33.4: the declared contract is `Promise<{accepted: boolean}>`, so
|
|
202
|
+
// `accepted !== undefined` was a永真 condition — and a host that resolved
|
|
203
|
+
// `undefined` fell through to `{ok:true}` while nothing was queued. Only an
|
|
204
|
+
// explicit `true` proves acceptance.
|
|
205
|
+
if (accepted?.accepted !== true) {
|
|
202
206
|
json(res, { ok: false, error: '会话未接受该消息(可能已结束或正忙)', requestId, sessionId }, 409)
|
|
203
207
|
return
|
|
204
208
|
}
|
|
@@ -37,6 +37,25 @@ export function createSessionSummaryHandler(
|
|
|
37
37
|
*/
|
|
38
38
|
const SUMMARY_REUSE_MS = 3_000
|
|
39
39
|
const summaryInFlight = new Map<string, { at: number; value: Promise<SessionSummaryResult> }>()
|
|
40
|
+
/**
|
|
41
|
+
* Identity of the TW client a cached run was started for (v0.33.4).
|
|
42
|
+
*
|
|
43
|
+
* `session` alone is NOT a valid cache key: `client` is the TARGETED wiki's
|
|
44
|
+
* client, so within the 3s reuse window a request scoped to another wiki (the
|
|
45
|
+
* session's scope changed, or `?wiki=` differs) reused the previous wiki's
|
|
46
|
+
* summary — titles, tags and wikitext of the wrong knowledge base. A WeakMap
|
|
47
|
+
* gives each client instance a stable number without touching its API.
|
|
48
|
+
*/
|
|
49
|
+
const clientIds = new WeakMap<TiddlyWebClient, number>()
|
|
50
|
+
let nextClientId = 1
|
|
51
|
+
const clientIdOf = (client: TiddlyWebClient): number => {
|
|
52
|
+
const known = clientIds.get(client)
|
|
53
|
+
if (known !== undefined) return known
|
|
54
|
+
const id = nextClientId
|
|
55
|
+
nextClientId += 1
|
|
56
|
+
clientIds.set(client, id)
|
|
57
|
+
return id
|
|
58
|
+
}
|
|
40
59
|
/**
|
|
41
60
|
* POST /dsh-tiddlywiki/session/summary — 生成当前会话的 wiki 汇总页(「知识库」
|
|
42
61
|
* Tab 的后端)。body `{ session: <会话ID> }`;后端用 sessionQuery 读本会话(含
|
|
@@ -75,15 +94,16 @@ export function createSessionSummaryHandler(
|
|
|
75
94
|
json(res, { ok: false, error: 'session query service unavailable' }, 503)
|
|
76
95
|
return
|
|
77
96
|
}
|
|
78
|
-
const
|
|
97
|
+
const cacheKey = `${session}|${clientIdOf(client)}`
|
|
98
|
+
const cachedSummary = summaryInFlight.get(cacheKey)
|
|
79
99
|
let pendingSummary: Promise<SessionSummaryResult>
|
|
80
100
|
if (cachedSummary !== undefined && Date.now() - cachedSummary.at < SUMMARY_REUSE_MS) {
|
|
81
101
|
pendingSummary = cachedSummary.value
|
|
82
102
|
} else {
|
|
83
103
|
pendingSummary = writeSessionSummary(client, sq, session)
|
|
84
|
-
summaryInFlight.set(
|
|
104
|
+
summaryInFlight.set(cacheKey, { at: Date.now(), value: pendingSummary })
|
|
85
105
|
pendingSummary.catch(() => {
|
|
86
|
-
if (summaryInFlight.get(
|
|
106
|
+
if (summaryInFlight.get(cacheKey)?.value === pendingSummary) summaryInFlight.delete(cacheKey)
|
|
87
107
|
})
|
|
88
108
|
}
|
|
89
109
|
const result = await pendingSummary
|
|
@@ -228,6 +228,11 @@ export function createTwProxyRoutes(deps: TwProxyRouteDeps): TwProxyRouteHandler
|
|
|
228
228
|
if (method === 'PUT' || method === 'DELETE' || method === 'POST') headers['x-requested-with'] = 'TiddlyWiki'
|
|
229
229
|
const init: RequestInit = { method, headers, signal: AbortSignal.timeout(15_000) }
|
|
230
230
|
if (method === 'PUT' || method === 'POST') init.body = await readBody(req, MAX_PROXY_BODY_BYTES)
|
|
231
|
+
// v0.33.4: drain a body the upstream fetch does not consume (a DELETE with
|
|
232
|
+
// a body, chiefly). The sibling `/tw` proxy has always done this; without it
|
|
233
|
+
// the request never drains and the client sits on a half-open socket until
|
|
234
|
+
// the 15s timeout.
|
|
235
|
+
else if (req.readableEnded === false && (req.headers['content-length'] !== undefined || req.headers['transfer-encoding'] !== undefined)) req.resume()
|
|
231
236
|
const upstream = await fetch(`${server.url}${rest}${url.search}`, init)
|
|
232
237
|
const data = await upstream.text()
|
|
233
238
|
res.writeHead(upstream.status, {
|
package/src/host/routes.ts
CHANGED
|
@@ -349,7 +349,9 @@ export function registerRoutes(ctx: { webServer: WebServerFace }, deps: RouteDep
|
|
|
349
349
|
// single surface).
|
|
350
350
|
const mutationLock = deps.mutationLock ?? createMutationLock()
|
|
351
351
|
const beginMutation = (label: string): boolean => mutationLock.begin(label)
|
|
352
|
-
|
|
352
|
+
// v0.33.4: release with the SAME label, so a caller whose `begin()` never ran
|
|
353
|
+
// cannot free a lock someone else holds (see host/mutation-lock.ts).
|
|
354
|
+
const endMutation = (label: string): void => mutationLock.end(label)
|
|
353
355
|
|
|
354
356
|
/**
|
|
355
357
|
* Absolute twin of `twProxy` for embedders whose own document is not on
|
|
@@ -505,6 +507,16 @@ export function registerRoutes(ctx: { webServer: WebServerFace }, deps: RouteDep
|
|
|
505
507
|
// Sync is a repository/folder operation on the target wiki's path: without
|
|
506
508
|
// this check a stopped `?wiki=` would pull+commit+push the DEFAULT repo.
|
|
507
509
|
if (refuseStoppedTarget(req, res)) return
|
|
510
|
+
// v0.33.4: no child behind this request ⇒ there is nothing to restart.
|
|
511
|
+
// `drainThenStop` treats `client: undefined` as "nothing queued" and the
|
|
512
|
+
// optional `server(req)?.restart()` swallows itself, so this used to
|
|
513
|
+
// answer `{ok:true,drained:true,status:undefined}` — a FAKE success that
|
|
514
|
+
// restarted nothing while the caller believed it had (same class as the
|
|
515
|
+
// `?wiki=<registered but stopped>` fake success fixed in v0.29.0).
|
|
516
|
+
if (deps.server(req) === undefined) {
|
|
517
|
+
json(res, { ok: false, error: notRunning(req) }, 503)
|
|
518
|
+
return
|
|
519
|
+
}
|
|
508
520
|
if (!beginMutation('restart')) {
|
|
509
521
|
json(res, { ok: false, error: '另一个重启/同步正在进行中,请稍候' }, 429)
|
|
510
522
|
return
|
|
@@ -520,7 +532,7 @@ export function registerRoutes(ctx: { webServer: WebServerFace }, deps: RouteDep
|
|
|
520
532
|
stop: async () => { await deps.server(req)?.restart() },
|
|
521
533
|
log: (message) => console.warn('[dsh-tiddlywiki]', message) })
|
|
522
534
|
} finally {
|
|
523
|
-
endMutation()
|
|
535
|
+
endMutation('restart')
|
|
524
536
|
}
|
|
525
537
|
json(res, { ok: true, status: deps.server(req)?.status().status, drained })
|
|
526
538
|
} catch (err) {
|
|
@@ -616,7 +628,7 @@ export function registerRoutes(ctx: { webServer: WebServerFace }, deps: RouteDep
|
|
|
616
628
|
} catch (err) {
|
|
617
629
|
json(res, { ok: false, error: err instanceof Error ? err.message : String(err) }, errorStatus(err))
|
|
618
630
|
} finally {
|
|
619
|
-
endMutation()
|
|
631
|
+
endMutation('sync')
|
|
620
632
|
}
|
|
621
633
|
}
|
|
622
634
|
|
|
@@ -32,6 +32,10 @@ const NOTE_TOOL_NAMES = new Set([
|
|
|
32
32
|
'tiddlywiki_put',
|
|
33
33
|
'tiddlywiki_batch_put',
|
|
34
34
|
'tiddlywiki_append',
|
|
35
|
+
// v0.33.4: `replace` is the「读全文 → 改几处 → 整篇回写」mainstay, and it was
|
|
36
|
+
// missing from this set — a session that edited notes ONLY through `replace`
|
|
37
|
+
// was reported as「暂时没有产生、读取…任何笔记」.
|
|
38
|
+
'tiddlywiki_replace',
|
|
35
39
|
'tiddlywiki_attach',
|
|
36
40
|
'tiddlywiki_rename',
|
|
37
41
|
'tiddlywiki_get',
|
|
@@ -64,7 +68,7 @@ export function isPlausibleTitle(title: string): boolean {
|
|
|
64
68
|
if (title.length === 0 || title.length > 300) return false
|
|
65
69
|
if (title.trim().length === 0) return false
|
|
66
70
|
if (/[\u0000-\u001f\u007f]/.test(title)) return false // 控制字符 / 换行
|
|
67
|
-
if (/[\]
|
|
71
|
+
if (/[\]|[`#]/.test(title)) return false // 会破坏 [[…]] 链接语法(`[` 同样要挡)
|
|
68
72
|
if (title.startsWith('…') || title === '标题') return false // 占位 / 示例写法
|
|
69
73
|
return true
|
|
70
74
|
}
|
|
@@ -103,12 +107,17 @@ export function trimTrailingClosers(raw: string): string {
|
|
|
103
107
|
}
|
|
104
108
|
}
|
|
105
109
|
|
|
106
|
-
/** 扫助手回复文本里的 `/dsh-tiddlywiki/tw
|
|
110
|
+
/** 扫助手回复文本里的 `/dsh-tiddlywiki/tw/[<库 id>/]#标题` 引用链接 → 读取。 */
|
|
107
111
|
export function scanRefs(text: string, time: number, subagent: boolean, collected: Collected): void {
|
|
108
112
|
// 只认到下一个 `#` / 空白为止(`#` 之后再出现 `#` 是 URL 片段分隔,标题里的 `#`
|
|
109
113
|
// 按链接约定必须编码);`)` / `]` 不再一律截断——它们是合法标题字符,只有落在
|
|
110
114
|
// 末尾且无配对开启符时才当收尾符削掉(见 trimTrailingClosers)。
|
|
111
|
-
|
|
115
|
+
//
|
|
116
|
+
// ⚠️ v0.33.4:**多库**时注入的提示词要求写成 `/tw/<库 id>/#标题`(prompt.ts 按
|
|
117
|
+
// 作用域拼 `linkBase`)。旧正则只认裸 `/tw/#标题`,于是在多库安装里助手回复的
|
|
118
|
+
// **每一条**笔记引用都扫不到,「知识库」Tab 的读取列表系统性为空 —— 而且看不出
|
|
119
|
+
// 是漏扫。库 id 的形状与 `seeds-registry.ts` 的 `isOurProxyBase` 保持一致。
|
|
120
|
+
const re = /\/dsh-tiddlywiki\/tw\/(?:[a-z0-9][a-z0-9._-]{0,63}\/)?#([^#\s]+)/g
|
|
112
121
|
let m: RegExpExecArray | null
|
|
113
122
|
while ((m = re.exec(text)) !== null) {
|
|
114
123
|
const raw = trimTrailingClosers(m[1] ?? '')
|