Files
apes-sidebar/src/config.ts
T
Menghuan1918 d4ca9eb6fb feat(transfer): 流式传输与原生打包,JSONL 预览/编辑与大文件分页预览
宿主半:
- /sidebar/file 与 /sidebar/html 改为 createReadStream + pipeline(新增
  file-stream.ts),删除 mediaLimit;流开始后失败由 writeJson 的
  headersSent 守卫降级为 destroy,不再抛 ERR_HTTP_HEADERS_SENT
- 上传本就是流式的,只删掉客户端 MAX_UPLOAD_BYTES 预检与宿主 limit 逐块检查
- 删除 readLimit / mediaLimit / uploadLimit 三个配置键,新增共享常量模块
  editor-limits.ts(MAX_EDITABLE_BYTES = 5 MiB、PAGE_BYTES、写入体护栏)
- fs.read 重塑为 text | binary | too-large 三态,不再有「截断文本」这个
  既不可信又不可保存的中间态;新增 fs.readPage 字节窗口分页(按行对齐、
  按 path+size+mtime 缓存首次探测到的编码)
- 删除 JSON 版 fs.write,改为 /sidebar/write raw 路由(文档即请求体),
  拆掉 MAX_BODY_BYTES=1MiB 这道保存墙;编码与行尾还原逻辑原样搬到
  fs-operations.writeWorkspaceText
- 打包改原生命令:新增 archive-tools.ts(zip → tar 探测、公共祖先定位、
  argv 规划、stdio 重定向),archive-route 改为后台写临时文件 + 流式下发,
  删除手写 ZIP writer src/zip.ts;新增 unavailable 错误码

客户端半:
- 新增 text-pages.ts(字节偏移分页游标,显式翻页)、jsonl.ts(逐行记录
  解析与摘要)、lang-jsonl.ts(每行独立的 JSON tokenizer,替代会为每条记录
  产生错误节点的 json() 语法)、LargeFileView.tsx(记录卡片 / 日志行两种
  只读分页视图)
- 新增 jsonl viewer 描述符(预览 = 记录列表,编辑 = 可保存的 CodeMirror)
- FileViewerProps 用 size/largeFile 取代 truncated;超限文件改为只读分页
  预览并可下载,不再提供编辑器
- tags.invalid 由纯白改为红色波浪线(浅色主题下原本白底白字不可见)
- zip 下载改回普通 <a download> 直链,不再把整包放进浏览器内存
2026-10-11 22:25:25 +08:00

145 lines
6.5 KiB
TypeScript

/**
* Serializable configuration and defaults for the sidebar host half. Loader
* schema validation normally fills defaults; {@link resolveSidebarConfig}
* applies the same defaults for direct callers that bypass the Loader.
*
* Schemastery comes from DSH, not from the public `schemastery` package, and
* that is load-bearing rather than stylistic: only DSH's build WRAPS a
* `meta.volatile` field in a cosmokit `Volatile` reference when it parses the
* config. The Loader's volatile-commit path walks those references
* (`volatileEntries` / `updateVolatile`), so a schema built with the public
* package produces plain values, leaves the loader nothing to commit, and
* **silently drops every live preference write** — the write reports success
* and the effective value never changes.
* @module dsh-better-sidebar/config
*/
import z from '@deepseek-ai/schemastery'
import {
EXPLORER_EXCLUDE_DEFAULTS,
type SidebarPrefs,
} from './prefs-shared.ts'
export {
EXPLORER_EXCLUDE_DEFAULTS,
SIDEBAR_PREFS_DEFAULTS,
SIDEBAR_PREFS_NS,
type SidebarPrefs,
} from './prefs-shared.ts'
/** Tunable sidebar host limits (every field optional; defaults fill in). */
export interface SidebarConfig {
/** Explorer row bound of one level. */
listLimit?: number
}
/**
* Schemastery schema for the deployment-provided host limits.
*
* The three transfer/read caps this schema used to carry (`readLimit` /
* `mediaLimit` / `uploadLimit`) are gone: upload, download and the HTML
* preview route all stream now, so there is nothing left for a byte cap to
* protect and a large file is simply a large file. What replaced `readLimit`
* is {@link MAX_EDITABLE_BYTES} — not a transfer cap but the size above which
* a text file gets a read-only paged preview instead of an editor, because
* CodeMirror has to hold the whole document in the browser.
*
* Old profiles that still write the retired keys are harmless: schemastery
* tolerates unknown keys (the same property that keeps a legacy
* `workspaceFence` field in an old profile from being fatal).
*/
const LimitsSchema = z.object({
listLimit: z.number().step(1).min(1).default(1000),
})
/** Fully defaulted sidebar host settings. */
export interface ResolvedSidebarConfig {
listLimit: number
}
/**
* Apply direct-call defaults after Loader schema validation has normally run.
*
* @param config - Deployment-provided sidebar host settings.
* @returns Complete settings consumed by the host half.
*/
export function resolveSidebarConfig(config: SidebarConfig | undefined): ResolvedSidebarConfig {
return {
listLimit: config?.listLimit ?? 1000,
}
}
// ── User-facing "Side card" preferences ─────────────────────────────────────
/**
* Schemastery schema for the user-facing preferences (validated by the
* settings service).
*
* Asserted as `z<SidebarPrefs>` (the interface lives in `prefs-shared.ts`; the
* explicit annotation is also what keeps the emitted declaration portable).
* Unknown keys are TOLERATED by this schema and pass through untouched, which
* is what keeps a legacy `workspaceFence: true` — or the retired title-bar
* compatibility keys (`titleBarScheme` / `titleBarPresetId` /
* `titleBarCompat` / `titleBarStripPx`, removed with the whole strip
* mechanism) — in an old profile harmless instead of fatal.
*/
export const PrefsSchema: z<SidebarPrefs> = z.object({
autoOpenSubagent: z.boolean().default(true),
autoOpenJobs: z.boolean().default(true),
tasksViewMode: z.union([z.const('graph'), z.const('tree')]).default('graph'),
mobileNoAutoOpen: z.boolean().default(true),
mobileDefaultTree: z.boolean().default(true),
agentOpenTools: z.boolean().default(false),
editorExplorer: z.boolean().default(false),
editorGitGutter: z.boolean().default(true),
explorerExclude: z.array(z.string()).default([...EXPLORER_EXCLUDE_DEFAULTS]),
customCss: z.string(),
htmlViewerNoSandbox: z.boolean().default(false),
htmlViewerDefaultUnsafe: z.boolean().default(false),
// Per-feature enable switches are OPEN maps (any tab/viewer id, built-in or
// external): an absent key means enabled, so old documents resolve to {}
// (everything on) with no migration. Non-boolean values fail validation.
tabsEnabled: z.dict(z.boolean()).default({}),
viewersEnabled: z.dict(z.boolean()).default({}),
// Plugin-owned settings blobs (v0.12.0+) are an OPEN nested map: any
// descriptor id may carry any JSON-serializable values. This is the
// "settings seam" opening — without it the seam would drop third-party
// keys as unknown schema fields.
pluginSettings: z.dict(z.dict(z.any())).default({}),
})
// ── The Loader row schema ───────────────────────────────────────────────────
//
// DSH 0.1.7 replaced the registrable settings namespace with a forms service
// over the profile's own entries: a form is addressed by the row's Loader
// entry id and read from `entry.fiber.runtime.Config`, i.e. THIS export.
// There is no longer anywhere else for the user preferences to live, so they
// are merged into the row schema beside the deployment limits.
//
// Every preference field is volatile, and BOTH halves of that matter:
// - the Loader's `equalExceptVolatile` ignores volatile fields, so a
// preference edit is recognised as volatile-only and takes the live-commit
// path (`loader/volatile-update`) instead of remounting the plugin, while
// the ordinary limit fields above keep the remount semantics they had;
// - DSH's schemastery WRAPS a volatile field in a `Volatile` reference while
// parsing, which is the thing that path commits into. Marking a field
// volatile on the public schemastery build sets the same `meta` flag but
// produces plain values, and the commit then silently has nothing to do —
// which is why this module imports schemastery from DSH (see the header).
//
// `.volatile()` RETURNS A COPY (`extra()` does), so the marked schemas have to
// be collected — calling it for its side effect leaves every field non-volatile
// and the settings service then reports "no volatile fields" for the row.
const volatilePrefs = Object.fromEntries(
Object.entries(PrefsSchema.dict ?? {}).map(([key, field]) => [key, field.volatile()]),
)
/**
* Config schema of this plugin's Loader row: deployment limits plus the live
* user preferences.
*/
export const Config = z.object({
...LimitsSchema.dict,
...volatilePrefs,
}) as z<SidebarConfig & SidebarPrefs>