Repository navigation
refactor(server): read skill and workspace previews through one function - #255
Conversation
Two functions decided whether a file is text, binary or too large, and returned different shapes for the same answer: `readSkillFile` in a route file returned `kind`, and `WorkspaceService.readFile` returned `state` plus a `previewType` that only ever restated it. `AGENTS.md` asks route handlers to stay thin and call services, which the skill reader did not. `readFilePreview` in `src/server/services/filePreview.ts` is now the only place that classification happens. It takes the caller's size limit, whether an oversized file is refused or truncated, and whether an image is inlined, so the skill page keeps refusing above 100 KB and the Files tab keeps reading the first 1 MB. Both readers delegate and drop their duplicated checks. One result shape, `state: 'text' | 'image' | 'binary' | 'too_large' | 'missing' | 'error'`, reaches the server types, the desktop types (`types/skill.ts`, `api/sessions.ts`, `stores/workspacePanelStore.ts`, `WorkspacePanel.tsx`) and `SkillDetail`. `previewType` is gone: `state === 'image'` says it. The behaviour difference the issue asked to see: a skill file that cannot be read — it vanished between the directory walk and the read — used to throw out of `readSkillFile` and be logged by the caller as a read failure. It is now skipped through the shared `missing` / `error` state, which is the answer the Files tab already gave for the same file. Refs GoDiao#247
GoDiao
left a comment
There was a problem hiding this comment.
Thanks, this is a clean refactor. The shared reader sits in the right place, both callers keep their limits, path containment still runs before the read, and previewType is gone everywhere. A few things to tidy before it goes in.
Should fix
1. An unreadable skill file is now skipped without a log. On main, a failed read threw out of readSkillFile, the catch at src/server/api/skills.ts:196 logged [Skills] Failed to read skill file, and the file was skipped. Now readFilePreview returns state: 'error', readSkillFile returns null (line 134), and buildFileTree hits if (!file) continue (line 191). Nothing reads preview.error. The file was skipped before and is skipped now, so the one real change is that the log is gone, which AGENTS.md's "Don't swallow errors" rule doesn't allow. Please log fullPath and preview.error when the state is error (missing can stay quiet), and add a test that an unreadable file is skipped.
2. WorkspacePanel.tsx still checks 'ok' on the file path. A file preview can no longer be 'ok'; only diff results are.
- Line 594:
state === 'text' || state === 'ok'. The'ok'can't match, because a diff in stateokis handled at line 573 first. - Line 556: the
'ok'inisFileContentonly makescontentMenunon-empty for a diff entry, and the diff branch doesn't usecontentMenu.
Please drop 'ok' from both. While you're there, the copy-contents condition at line 773 can be entry?.content !== undefined, since only a text result carries content.
3. The desktop app still has two state unions. SkillFileState in desktop/src/types/skill.ts:24 is a hand-written subset of FilePreviewState in desktop/src/api/sessions.ts:245. Since this issue is about having one shape, please derive it instead, for example Exclude<FilePreviewState, 'missing' | 'error'>.
4. A design doc still names the old field. docs/features/ui-refresh/visual-refresh.zh.md:39 says the server marks each skill file with kind (text, image, binary, too_large). Please update it to state.
5. Two branches of readFilePreview aren't covered by the tests. I checked by changing the code and running bun test file-preview workspace-service skills:
- Changing both
stat.size > maxBytesto>=leaves all 64 tests passing, so the exact-limit boundary isn't tested. Please add a file of exactlymaxBytes; it should come back astextwithtruncated: falsein both modes. - Replacing the
errormessage with a constant also leaves all 64 passing, so theerrorstate isn't tested at all. Please add a case for it.
Notes
- When
statitself fails with something other than ENOENT/ENOTDIR (e.g. EACCES), the Files tab error now starts with "Failed to read workspace file" instead of "Failed to stat workspace path". Nothing matches on the text, so this is fine. - The PR body's list of updated files leaves out
src/server/api/skills.ts, which also changes its response field fromkindtostate. - No CHANGELOG entry is needed for this one; we'll add a line when it lands.
Log an unreadable skill file with its path before skipping it, drop the last 'ok' comparisons from the workspace panel, derive SkillFileState from FilePreviewState on both sides, name the state in the visual refresh document, and cover the exact-limit and error branches.
|
All five points are in 1. The log. 2. That change moved 10 fixtures: 3. The type. 4. Docs. 5. The two branches. Exact limit: a file of exactly PR body lists |
GoDiao
left a comment
There was a problem hiding this comment.
Thanks @iruzen-dono, and welcome to DreamCoder! I should have said that on #250. All five points are in: the skipped skill file is logged again, the 'ok' checks are gone from the file path, SkillFileState is derived from FilePreviewState, the doc says state, and the limit and error cases are tested. I checked that the new skills test fails without the log and the limit test fails with >=, and ran the server and desktop suites for the touched files here. CI is green.
We'll add the CHANGELOG line on main after merging.
We'll be opening more features and issues soon, and you're very welcome to keep contributing. If you find DreamCoder useful, a star on the repo would be much appreciated.
Closes #247
What changed
src/server/services/filePreview.tsis now the only place a file is classified as text, image, binary, too large, missing or unreadable.readFilePreview(path, { maxBytes, oversize, inlineImage })takes the caller's limit and what to do past it, so the two callers keep their own behaviour: the skill page keeps refusing above 100 KB (oversize: 'refuse'), the Files tab keeps reading its first 1 MB (oversize: 'truncate'). An image is not read at all unless the caller asks for the data URL, which is why the skill page still never loads image bytes for a page it renders on demand.readSkillFileandWorkspaceService.readFileboth delegate to it and lose their duplicated checks.readSkillFileis out of the route file's body count too — it only decides frontmatter and entry-ness now, which is what the route is for.One result shape
previewTypeis gone from the API and everywhere it was read:state === 'image'already says it, and keeping both was the duplication this issue is about. Updated:src/server/api/skills.ts,src/server/services/workspaceService.ts,desktop/src/types/skill.ts,desktop/src/api/sessions.ts,desktop/src/stores/workspacePanelStore.ts,desktop/src/components/workspace/WorkspacePanel.tsxanddesktop/src/components/skills/SkillDetail.tsx.SkillFileStateisExclude<FilePreviewState, 'missing' | 'error'>on both sides, so the four states a page can render are derived from the six a preview can report.The store keeps its own
kind: 'file' | 'diff'discriminator for the panel, which is a different axis — which is why the unified field isstateand notkind, as the issue's list of names allowed.The behaviour difference, as the issue asked
One, and it is a file that cannot be read at all:
readSkillFileused to log the failure and drop the file. It now returnsmissingorerrorthrough the shared function, and the walk skips it — loggingfullPathandpreview.erroron theerrorstate so the reason survives, whilemissingstays quiet.Everything else is preserved, including the two size limits, the zero-byte binary rule, the image short-circuit before the size check,
truncated/readBytes, and thelanguageeach state reports.Checks, with the numbers
bun teston the four server files this PR owns → 155 pass, 0 fail, 1 skipped (the unreadable-file case, skipped on Windows wherechmoddoes not restrict)cd desktop && bun run lint→ exit 0 (tsc, tsc electron, oxlint)cd desktop && bun run test -- --run→ 2858 pass, 5 fail; those same 5 fail by name on a stashed tree, inpages.test,LocalSlashCommandPanel,MessageListandUpdateChecker, none of which this PR touchesbun test(whole server suite) → 2625 pass, 16 fail; the same failures reproduce onHEAD~1(189 pass / 6 fail on the six affected files, on both commits), all Windows symlink fixtures and 5 s timeouts.New spec:
src/server/__tests__/file-preview.test.ts, eight cases — text, a zero byte, an oversized file in both modes, a file of exactly the limit in both modes, a missing file, a pathstatrefuses with something other than absence, and an image with and without the inline request.Out of scope
No visible change on either page. The workspace panel's
kind: 'file' | 'diff'and the tree/status results keep their ownstatevocabulary, which is a different object from a file preview.