dsh-pdf-workbench 1.4.1 → 1.5.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 +29 -7
- package/client.js +4106 -469
- package/docs/LAYOUT.md +56 -0
- package/docs/RELEASE.md +8 -0
- package/docs/TESTING.md +16 -0
- package/docs/ci.md +18 -13
- package/docs/copy/templates.md +4 -4
- package/docs/decisions.md +119 -0
- package/docs/features.md +14 -8
- package/docs/requirements/2026-11-25-v1.5/346/224/271/350/277/233/351/234/200/346/261/202.md +622 -0
- package/docs/roles/{release-manager.md → ci.md} +37 -47
- package/docs/roles/programmer.md +45 -59
- package/docs/roles/requirements.md +68 -0
- package/docs/todo.md +590 -32
- package/index.js +337 -1
- package/package.json +1 -1
- package/src/host/backup.js +172 -33
- package/src/host/book-wizard.js +454 -0
- package/src/host/digest.js +4 -0
- package/src/host/engine.js +88 -0
- package/src/host/exports.js +159 -0
- package/src/host/http.js +28 -1
- package/src/host/imports.js +650 -0
- package/src/host/index-source.js +117 -11
- package/src/host/library-export.js +162 -0
- package/src/host/pdf-source.js +6 -2
- package/src/host/routes.js +1020 -120
- package/src/host/shortcut.js +238 -0
- package/src/host/storage.js +421 -0
- package/src/host/translate-blocks.js +49 -45
- package/src/host/translate-job.js +37 -10
- package/src/host/translate.js +19 -45
- package/src/host/translation-cache.js +209 -0
- package/src/host/typst-install.js +132 -0
- package/src/host/zip.js +486 -4
- package/tools/latex_to_typst.py +1353 -0
- package/tools/md2overlay.py +147 -152
- package/tools/md2overlay_typst.py +312 -0
- package/tools/md2typst.py +818 -0
- package/tools/overlay_layout.py +332 -0
- package/tools/pdf_blocks.py +82 -10
- package/tools/realign_overlay.py +4 -1
- package/tools/rebuild_overlay.py +3 -1
- package/tools/split_pdf.py +158 -0
- package/tools/typst_runtime.py +98 -0
- package/vendor/katex/LICENSE +21 -0
- package/vendor/katex/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/vendor/katex/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/vendor/katex/katex.min.css +1 -0
- package/vendor/katex/katex.min.js +1 -0
- package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +9 -1
- package/docs/boss-handover.md +0 -84
- package/docs/roles/auditor.md +0 -123
- package/docs/roles/boss.md +0 -162
- package/docs/roles/code-reviewer.md +0 -132
- package/docs/roles/feature-inventory.md +0 -119
- package/docs/roles/feedback-collector.md +0 -105
- package/docs/roles/tester.md +0 -140
- package/docs/roles/ui-copywriter.md +0 -122
- package/src/host/book-import.js +0 -98
package/index.js
CHANGED
|
@@ -15,8 +15,11 @@
|
|
|
15
15
|
* @module dsh-pdf-workbench
|
|
16
16
|
*/
|
|
17
17
|
import { basename, dirname, join } from 'node:path'
|
|
18
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
19
|
+
import { randomUUID } from 'node:crypto'
|
|
18
20
|
import { fileURLToPath } from 'node:url'
|
|
19
21
|
import { resolveConfig } from './src/host/config.js'
|
|
22
|
+
import { resolveTypeset } from './src/host/engine.js'
|
|
20
23
|
import { collectTranscript, saveDigest, summarize } from './src/host/digest.js'
|
|
21
24
|
import { LibrariesStore } from './src/host/libraries.js'
|
|
22
25
|
import { LinksStore } from './src/host/links.js'
|
|
@@ -26,10 +29,21 @@ import { ShelfGroups } from './src/host/shelf-groups.js'
|
|
|
26
29
|
import { PREFIX, createHandler } from './src/host/routes.js'
|
|
27
30
|
import { installToolkit } from './src/host/toolkit.js'
|
|
28
31
|
import { purgeArchivedBlanks } from './src/host/purge.js'
|
|
32
|
+
import { migrateTranslationCache } from './src/host/translation-cache.js'
|
|
29
33
|
|
|
30
34
|
/** Cordis plugin name. */
|
|
31
35
|
export const name = 'pdfdesk'
|
|
32
36
|
|
|
37
|
+
/**
|
|
38
|
+
* One error's message, whatever shape the throw took.
|
|
39
|
+
* @param {unknown} error - the thrown value.
|
|
40
|
+
* @returns {string} a readable message.
|
|
41
|
+
*/
|
|
42
|
+
function messageOf(error) {
|
|
43
|
+
if (error !== null && error !== undefined && typeof error.message === 'string') return error.message
|
|
44
|
+
return String(error)
|
|
45
|
+
}
|
|
46
|
+
|
|
33
47
|
/** The browser carrier and the trust fence guarding its routes. */
|
|
34
48
|
export const inject = ['webServer', 'connection']
|
|
35
49
|
|
|
@@ -40,6 +54,7 @@ export const inject = ['webServer', 'connection']
|
|
|
40
54
|
* @returns {void}
|
|
41
55
|
*/
|
|
42
56
|
export function apply(ctx, rawConfig) {
|
|
57
|
+
const startedAt = Date.now()
|
|
43
58
|
const config = resolveConfig(rawConfig)
|
|
44
59
|
const connection = ctx.get('connection')
|
|
45
60
|
const pending = new PendingStore(config.pendingPath)
|
|
@@ -55,6 +70,24 @@ export function apply(ctx, rawConfig) {
|
|
|
55
70
|
name: basename(config.libraryDir),
|
|
56
71
|
})
|
|
57
72
|
|
|
73
|
+
// The per-page translation cache moved from this data folder into each
|
|
74
|
+
// library (`<libraryDir>/.pdfdesk/translation/`), so two libraries cannot
|
|
75
|
+
// share a same-named book's pages. The move starts once at mount and the
|
|
76
|
+
// routes that touch the cache await it (DEC-0014 §5); a book whose owner is
|
|
77
|
+
// ambiguous stays in the old folder and is reported instead of guessed.
|
|
78
|
+
const translationMigration = (async () => {
|
|
79
|
+
try {
|
|
80
|
+
const listed = await libraries.list()
|
|
81
|
+
const entries = listed.items.length === 0 ? [libraries.fallbackEntry()] : listed.items
|
|
82
|
+
const report = await migrateTranslationCache({ dataDir: config.dataDir, libraries: entries })
|
|
83
|
+
for (const warning of report.warnings) ctx.logger?.warn?.('pdfdesk: translation cache migration: %s', warning)
|
|
84
|
+
return report
|
|
85
|
+
} catch (error) {
|
|
86
|
+
ctx.logger?.warn?.('pdfdesk: translation cache migration failed: %s', String(error))
|
|
87
|
+
return { moved: [], skipped: [], warnings: [String(error)] }
|
|
88
|
+
}
|
|
89
|
+
})()
|
|
90
|
+
|
|
58
91
|
/**
|
|
59
92
|
* Read one session's context pressure, live when it is running and from the
|
|
60
93
|
* last recorded snapshot when it is not.
|
|
@@ -84,6 +117,274 @@ export function apply(ctx, rawConfig) {
|
|
|
84
117
|
return stored === null ? null : { tokens: stored.tokens, window: stored.window, ratio: stored.tokens / stored.window, source: 'stored' }
|
|
85
118
|
}
|
|
86
119
|
|
|
120
|
+
/**
|
|
121
|
+
* The models this deployment can route, for the settings page. Read-only: it
|
|
122
|
+
* never selects a model and never writes the default, so opening settings can
|
|
123
|
+
* never change which model a session uses.
|
|
124
|
+
* @returns {Promise<{ providers: Array<object>, default: object | null, failures: Array<object> }>} the catalog.
|
|
125
|
+
*/
|
|
126
|
+
async function aiCatalog() {
|
|
127
|
+
const llm = ctx.get('llm')
|
|
128
|
+
const defaultModel = ctx.get('agentDefaultModel')
|
|
129
|
+
const providers = []
|
|
130
|
+
const failures = []
|
|
131
|
+
if (llm !== undefined && llm !== null) {
|
|
132
|
+
let listed = []
|
|
133
|
+
try {
|
|
134
|
+
listed = typeof llm.listProviders === 'function' ? llm.listProviders() : []
|
|
135
|
+
} catch (error) {
|
|
136
|
+
failures.push({ id: '', name: '', message: messageOf(error) })
|
|
137
|
+
}
|
|
138
|
+
for (const provider of listed) {
|
|
139
|
+
const id = String(provider.id)
|
|
140
|
+
const name = String(provider.name === undefined || provider.name === null ? id : provider.name)
|
|
141
|
+
try {
|
|
142
|
+
const rows = await llm.listModels(id)
|
|
143
|
+
providers.push({
|
|
144
|
+
id,
|
|
145
|
+
name,
|
|
146
|
+
models: rows.map(row => ({
|
|
147
|
+
id: String(row.id),
|
|
148
|
+
name: String(row.name === undefined || row.name === null ? row.id : row.name),
|
|
149
|
+
modalities: Array.isArray(row.inputModalities) ? row.inputModalities.map(String) : ['text'],
|
|
150
|
+
})),
|
|
151
|
+
})
|
|
152
|
+
} catch (error) {
|
|
153
|
+
failures.push({ id, name, message: messageOf(error) })
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const selection = defaultModel === undefined || defaultModel === null || typeof defaultModel.currentSelection !== 'function'
|
|
158
|
+
? null
|
|
159
|
+
: defaultModel.currentSelection()
|
|
160
|
+
return {
|
|
161
|
+
providers,
|
|
162
|
+
default: selection === null || selection === undefined
|
|
163
|
+
? null
|
|
164
|
+
: { provider: String(selection.provider), model: String(selection.model) },
|
|
165
|
+
failures,
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The model selection a stored choice asks for: `follow` reads the deployment
|
|
171
|
+
* default (live), `pinned` uses what the user picked. Read-only either way.
|
|
172
|
+
* @param {{ mode?: string, provider?: string, model?: string } | null | undefined} choice - the client's stored choice.
|
|
173
|
+
* @returns {{ provider: string, model: string } | null} the selection, or null when none is available.
|
|
174
|
+
*/
|
|
175
|
+
function resolveChoice(choice) {
|
|
176
|
+
if (choice !== null && choice !== undefined && choice.mode === 'pinned') {
|
|
177
|
+
const provider = String(choice.provider ?? '')
|
|
178
|
+
const model = String(choice.model ?? '')
|
|
179
|
+
return provider === '' || model === '' ? null : { provider, model }
|
|
180
|
+
}
|
|
181
|
+
const defaultModel = ctx.get('agentDefaultModel')
|
|
182
|
+
const selection = defaultModel === undefined || defaultModel === null || typeof defaultModel.currentSelection !== 'function'
|
|
183
|
+
? null
|
|
184
|
+
: defaultModel.currentSelection()
|
|
185
|
+
return selection === null || selection === undefined
|
|
186
|
+
? null
|
|
187
|
+
: { provider: String(selection.provider), model: String(selection.model) }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The directory one session should live in: the requested one, the chosen
|
|
192
|
+
* Workspace, or the active library.
|
|
193
|
+
* @param {{ cwd?: string, workspaceId?: string }} request - the client request.
|
|
194
|
+
* @returns {string} an absolute directory.
|
|
195
|
+
*/
|
|
196
|
+
function sessionCwd(request) {
|
|
197
|
+
if (typeof request.cwd === 'string' && request.cwd !== '') return request.cwd
|
|
198
|
+
const registry = ctx.get('workspaceRegistry')
|
|
199
|
+
if (typeof request.workspaceId === 'string' && request.workspaceId !== ''
|
|
200
|
+
&& registry !== undefined && registry !== null && typeof registry.get === 'function') {
|
|
201
|
+
const workspace = registry.get(request.workspaceId)
|
|
202
|
+
if (workspace !== undefined && workspace !== null && typeof workspace.path === 'string') return workspace.path
|
|
203
|
+
}
|
|
204
|
+
return config.libraryDir
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Create one reader session with an explicit model. The model travels as
|
|
209
|
+
* creation options only: this never writes the deployment default, so other
|
|
210
|
+
* conversations keep the model the user chose for them.
|
|
211
|
+
* @param {{ cwd?: string, workspaceId?: string, sessionId?: string, choice?: object }} request - the client request.
|
|
212
|
+
* @returns {Promise<{ sessionId: string, selection: { provider: string, model: string } }>} the created session.
|
|
213
|
+
*/
|
|
214
|
+
async function aiSession(request) {
|
|
215
|
+
const agents = ctx.get('agents')
|
|
216
|
+
if (agents === undefined || agents === null || typeof agents.create !== 'function') {
|
|
217
|
+
throw Object.assign(new Error('宿主没有会话服务'), { code: 'no-agents' })
|
|
218
|
+
}
|
|
219
|
+
const choice = request === null || request === undefined ? null : request.choice
|
|
220
|
+
const selection = resolveChoice(choice)
|
|
221
|
+
if (selection === null) throw Object.assign(new Error('没有可用的模型选择'), { code: 'no-selection' })
|
|
222
|
+
const sessionId = typeof request.sessionId === 'string' && request.sessionId !== '' ? request.sessionId : `session-${randomUUID()}`
|
|
223
|
+
await agents.create({ sessionId, meta: { cwd: sessionCwd(request) }, agentOptions: selection })
|
|
224
|
+
return { sessionId, selection }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* One-click isolation probe: create a throwaway session with the chosen model
|
|
229
|
+
* and confirm the deployment default did not move. The session is archived and
|
|
230
|
+
* disposed, so it never becomes a visible conversation.
|
|
231
|
+
* @param {{ choice?: object }} request - the client request.
|
|
232
|
+
* @returns {Promise<{ sessionId: string, requested: object, before: object | null, after: object | null, same: boolean | null, note: string }>} the probe result.
|
|
233
|
+
*/
|
|
234
|
+
async function aiProbe(request) {
|
|
235
|
+
const agents = ctx.get('agents')
|
|
236
|
+
if (agents === undefined || agents === null || typeof agents.create !== 'function') {
|
|
237
|
+
throw Object.assign(new Error('宿主没有会话服务'), { code: 'no-agents' })
|
|
238
|
+
}
|
|
239
|
+
const requested = resolveChoice(request === null || request === undefined ? null : request.choice)
|
|
240
|
+
if (requested === null) throw Object.assign(new Error('没有可用的模型选择'), { code: 'no-selection' })
|
|
241
|
+
const before = resolveChoice(null)
|
|
242
|
+
const sessionId = `session-${randomUUID()}`
|
|
243
|
+
let handle = null
|
|
244
|
+
try {
|
|
245
|
+
handle = await agents.create({ sessionId, meta: { cwd: config.libraryDir }, agentOptions: requested })
|
|
246
|
+
const after = resolveChoice(null)
|
|
247
|
+
const registry = ctx.get('workspaceRegistry')
|
|
248
|
+
if (registry !== undefined && registry !== null && typeof registry.archiveSession === 'function') {
|
|
249
|
+
try {
|
|
250
|
+
await registry.archiveSession(sessionId, { stopActivity: true })
|
|
251
|
+
} catch {
|
|
252
|
+
// The probe result is still valid without the archive.
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
sessionId,
|
|
257
|
+
requested,
|
|
258
|
+
before,
|
|
259
|
+
after,
|
|
260
|
+
same: before === null || after === null ? null : before.provider === after.provider && before.model === after.model,
|
|
261
|
+
note: '会话在第一次发言前不写请求头,所以这里验证的是“创建成功 + 全局默认未变”。',
|
|
262
|
+
}
|
|
263
|
+
} finally {
|
|
264
|
+
if (handle !== null && handle !== undefined && typeof handle.dispose === 'function') {
|
|
265
|
+
try {
|
|
266
|
+
await handle.dispose()
|
|
267
|
+
} catch {
|
|
268
|
+
// Already gone.
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The Host model channel one translation call uses. Read-only towards the
|
|
276
|
+
* deployment default: the model travels as a call argument and is never
|
|
277
|
+
* selected, so translating a chapter cannot change what other conversations
|
|
278
|
+
* use. Page images are stored once per page and referenced by the request,
|
|
279
|
+
* which is the only shape `llm.stream` accepts an image in.
|
|
280
|
+
* @param {{ mode?: string, provider?: string, model?: string } | null | undefined} choice - the client's stored choice.
|
|
281
|
+
* @returns {Promise<{ label: string, provider: string, model: string, image: Function, chat: Function }>} the channel.
|
|
282
|
+
*/
|
|
283
|
+
async function aiTranslate(choice) {
|
|
284
|
+
const llm = ctx.get('llm')
|
|
285
|
+
const attachments = ctx.get('attachments')
|
|
286
|
+
if (llm === undefined || llm === null || typeof llm.stream !== 'function'
|
|
287
|
+
|| attachments === undefined || attachments === null || typeof attachments.saveImage !== 'function') {
|
|
288
|
+
throw Object.assign(new Error('宿主没有可用的模型或附件服务'), { code: 'no-llm' })
|
|
289
|
+
}
|
|
290
|
+
const selection = resolveChoice(choice)
|
|
291
|
+
if (selection === null) {
|
|
292
|
+
throw Object.assign(new Error('没有可用的翻译模型:请在 设置 → AI 模型 为「整章翻译」选一个模型'), { code: 'no-model' })
|
|
293
|
+
}
|
|
294
|
+
const label = `${selection.provider} / ${selection.model}`
|
|
295
|
+
// OpenCode requires an `x-opencode-session` header for every request and DSH
|
|
296
|
+
// fills it from `sessionId`; a background translation is not an Agent Session,
|
|
297
|
+
// so it carries one stable per-run routing key instead. Without it the
|
|
298
|
+
// provider answers 400 MissingSessionID.
|
|
299
|
+
const routingId = `session-pdfdesk-${randomUUID()}`
|
|
300
|
+
let info = null
|
|
301
|
+
try {
|
|
302
|
+
info = await llm.resolveModelInfo(selection.provider, selection.model)
|
|
303
|
+
} catch (error) {
|
|
304
|
+
throw Object.assign(new Error(`翻译模型 ${label} 不可用:${messageOf(error)}`), { code: 'model-missing' })
|
|
305
|
+
}
|
|
306
|
+
const modalities = info !== null && info !== undefined && Array.isArray(info.inputModalities)
|
|
307
|
+
? info.inputModalities.map(String)
|
|
308
|
+
: ['text']
|
|
309
|
+
if (!modalities.includes('image')) {
|
|
310
|
+
throw Object.assign(new Error(`翻译模型 ${label} 不能读图,请在 设置 → AI 模型 换一个支持图片的模型`), { code: 'model-cannot-read-image' })
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
label,
|
|
314
|
+
provider: selection.provider,
|
|
315
|
+
model: selection.model,
|
|
316
|
+
/**
|
|
317
|
+
* Store one page image so a request can reference it.
|
|
318
|
+
* @param {Uint8Array | Buffer} bytes - PNG/JPEG/WebP bytes.
|
|
319
|
+
* @param {string} [mediaType] - declared type, default PNG.
|
|
320
|
+
* @returns {Promise<object>} the durable image reference.
|
|
321
|
+
*/
|
|
322
|
+
async image(bytes, mediaType) {
|
|
323
|
+
const data = bytes instanceof Uint8Array ? bytes : Uint8Array.from(bytes)
|
|
324
|
+
try {
|
|
325
|
+
return await attachments.saveImage({
|
|
326
|
+
data,
|
|
327
|
+
mediaType: typeof mediaType === 'string' && mediaType !== '' ? mediaType : 'image/png',
|
|
328
|
+
name: 'pdfdesk-page',
|
|
329
|
+
})
|
|
330
|
+
} catch (error) {
|
|
331
|
+
throw new Error(`页图保存失败(${label}):${messageOf(error)}`)
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
/**
|
|
335
|
+
* One model call carrying one page image and one prompt.
|
|
336
|
+
* @param {{ prompt: string, image: object, signal?: AbortSignal, onUsage?: Function }} request - the call.
|
|
337
|
+
* @returns {Promise<string>} the concatenated text blocks.
|
|
338
|
+
*/
|
|
339
|
+
async chat(request) {
|
|
340
|
+
const messages = [{
|
|
341
|
+
role: 'user',
|
|
342
|
+
content: [
|
|
343
|
+
{ type: 'text', text: String(request.prompt) },
|
|
344
|
+
{ type: 'image', attachment: request.image },
|
|
345
|
+
],
|
|
346
|
+
}]
|
|
347
|
+
let text = ''
|
|
348
|
+
let usage = null
|
|
349
|
+
try {
|
|
350
|
+
for await (const chunk of llm.stream({
|
|
351
|
+
provider: selection.provider,
|
|
352
|
+
model: selection.model,
|
|
353
|
+
messages,
|
|
354
|
+
temperature: 0.2,
|
|
355
|
+
signal: request.signal,
|
|
356
|
+
sessionId: routingId,
|
|
357
|
+
})) {
|
|
358
|
+
if (chunk === null || chunk === undefined || typeof chunk !== 'object') continue
|
|
359
|
+
if (chunk.type === 'text-delta' && typeof chunk.text === 'string') {
|
|
360
|
+
text += chunk.text
|
|
361
|
+
continue
|
|
362
|
+
}
|
|
363
|
+
if (chunk.type === 'usage' && chunk.usage !== null && chunk.usage !== undefined && typeof chunk.usage === 'object') {
|
|
364
|
+
usage = chunk.usage
|
|
365
|
+
continue
|
|
366
|
+
}
|
|
367
|
+
if (chunk.type !== 'finish') continue
|
|
368
|
+
const reason = chunk.reason === null || chunk.reason === undefined ? {} : chunk.reason
|
|
369
|
+
const kind = String(reason.kind ?? '')
|
|
370
|
+
if (kind === 'error' || kind === 'aborted') {
|
|
371
|
+
const failure = reason.failure === null || reason.failure === undefined ? {} : reason.failure
|
|
372
|
+
throw new Error(String(failure.message ?? '模型调用中断'))
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
} catch (error) {
|
|
376
|
+
throw Object.assign(new Error(`翻译调用失败(${label}):${messageOf(error)}`), { code: 'llm-failed' })
|
|
377
|
+
}
|
|
378
|
+
if (usage !== null && typeof request.onUsage === 'function') {
|
|
379
|
+
const input = Number(usage.inputTokens) || 0
|
|
380
|
+
const output = Number(usage.outputTokens) || 0
|
|
381
|
+
request.onUsage({ input, output, total: Number(usage.totalTokens) || input + output })
|
|
382
|
+
}
|
|
383
|
+
return text
|
|
384
|
+
},
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
87
388
|
/**
|
|
88
389
|
* Summarize one chapter's conversations into an outline file.
|
|
89
390
|
* @param {{ book: object, chapter: object, sessionIds: string[], mode: string }} request - what to digest.
|
|
@@ -115,6 +416,7 @@ export function apply(ctx, rawConfig) {
|
|
|
115
416
|
return { text, path, sessions: collected.used.length, dropped: collected.dropped }
|
|
116
417
|
}
|
|
117
418
|
|
|
419
|
+
const typeset = resolveTypeset(config.toolsDir)
|
|
118
420
|
const handler = createHandler(config, {
|
|
119
421
|
// Every route sits behind DSH's own Host/Origin and browser-login fence.
|
|
120
422
|
// A website open in the user's browser cannot call the workbench API, even
|
|
@@ -123,7 +425,17 @@ export function apply(ctx, rawConfig) {
|
|
|
123
425
|
if (connection === undefined || connection === null || typeof connection.requestRejection !== 'function') return undefined
|
|
124
426
|
return connection.requestRejection(request)
|
|
125
427
|
},
|
|
126
|
-
md2texScript:
|
|
428
|
+
md2texScript: typeset.readingScript,
|
|
429
|
+
overlayScript: typeset.overlayScript,
|
|
430
|
+
overlayName: typeset.overlayName,
|
|
431
|
+
typstEnv: typeset.env,
|
|
432
|
+
// Engine provenance for the settings row and the on-demand installer.
|
|
433
|
+
typeset: {
|
|
434
|
+
engine: typeset.engine,
|
|
435
|
+
typst: typeset.typst,
|
|
436
|
+
version: typeset.typstVersion,
|
|
437
|
+
source: typeset.typstSource,
|
|
438
|
+
},
|
|
127
439
|
// Session services the base-session route drives (stage 7.6).
|
|
128
440
|
sessionServices: () => ({
|
|
129
441
|
agents: ctx.get('agents'),
|
|
@@ -134,7 +446,14 @@ export function apply(ctx, rawConfig) {
|
|
|
134
446
|
links,
|
|
135
447
|
libraries,
|
|
136
448
|
shelves,
|
|
449
|
+
// Routes that read or write a chapter's translation cache await the move
|
|
450
|
+
// from the old global folder before doing so.
|
|
451
|
+
translationMigration: () => translationMigration,
|
|
137
452
|
contextPressure,
|
|
453
|
+
aiCatalog,
|
|
454
|
+
aiSession,
|
|
455
|
+
aiProbe,
|
|
456
|
+
aiTranslate,
|
|
138
457
|
digest: digestChapter,
|
|
139
458
|
installToolkit: libraryDir => installToolkit(config.toolsDir, libraryDir),
|
|
140
459
|
/**
|
|
@@ -223,4 +542,21 @@ export function apply(ctx, rawConfig) {
|
|
|
223
542
|
await pending.remove(session.id)
|
|
224
543
|
}).catch(() => {})
|
|
225
544
|
}), 'pdfdesk: pending naming and context pressure')
|
|
545
|
+
|
|
546
|
+
// Start-up baseline (T-08): one record per DSH start, so the cost the plugin
|
|
547
|
+
// adds is measurable without a profiler. A failed write must never block start.
|
|
548
|
+
try {
|
|
549
|
+
const file = join(config.dataDir, 'startup.json')
|
|
550
|
+
let list = []
|
|
551
|
+
try {
|
|
552
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
553
|
+
if (Array.isArray(parsed)) list = parsed
|
|
554
|
+
} catch {
|
|
555
|
+
// first run, or the file was removed
|
|
556
|
+
}
|
|
557
|
+
list.push({ at: Date.now(), applyMs: Date.now() - startedAt, engine: typeset.engine })
|
|
558
|
+
writeFileSync(file, JSON.stringify(list.slice(-30), null, 1))
|
|
559
|
+
} catch {
|
|
560
|
+
// measurement only
|
|
561
|
+
}
|
|
226
562
|
}
|
package/package.json
CHANGED