Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(web-studio): optimize large JSON previews (#3644)

yufengd59bdebb2765
diff --git a/web-studio/src/i18n/locales/en.ts b/web-studio/src/i18n/locales/en.ts
index d30f5d68f..eb45114cf 100644
--- a/web-studio/src/i18n/locales/en.ts
+++ b/web-studio/src/i18n/locales/en.ts
@@ -851,9 +851,12 @@ const en = {
edit: 'Edit',
emptyFile: '(empty file)',
emptyPrompt: 'Select a file to preview it here',
+ formattingJson: 'Formatting JSON...',
imageFailed: 'Image failed to load.',
imageLoading: 'Loading image...',
largeFileSkipped: 'This file is large and was not loaded automatically.',
+ jsonFormatFailed: 'JSON formatting failed. Showing the raw content.',
+ loadFile: 'Load anyway',
loadingContent: 'Reading content...',
loadingEditor: 'Loading editor...',
markdownPreview: 'Preview',
diff --git a/web-studio/src/i18n/locales/zh-CN.ts b/web-studio/src/i18n/locales/zh-CN.ts
index b1504ea16..9b4a10f1c 100644
--- a/web-studio/src/i18n/locales/zh-CN.ts
+++ b/web-studio/src/i18n/locales/zh-CN.ts
@@ -821,9 +821,12 @@ const zhCN = {
edit: '编辑',
emptyFile: '(空文件)',
emptyPrompt: '选择文件后在这里预览',
+ formattingJson: '正在格式化 JSON...',
imageFailed: '图片加载失败。',
imageLoading: '正在加载图片...',
largeFileSkipped: '文件较大,默认不自动加载。',
+ jsonFormatFailed: 'JSON 格式化失败,已显示原始内容。',
+ loadFile: '仍然加载',
loadingContent: '正在读取内容...',
loadingEditor: '加载编辑器...',
markdownPreview: '预览',
diff --git a/web-studio/src/routes/resources/-components/code-editor.tsx b/web-studio/src/routes/resources/-components/code-editor.tsx
index 7b4469da6..4f78dd538 100644
--- a/web-studio/src/routes/resources/-components/code-editor.tsx
+++ b/web-studio/src/routes/resources/-components/code-editor.tsx
@@ -28,7 +28,7 @@ import {
closeBrackets,
closeBracketsKeymap,
} from '@codemirror/autocomplete'
-import { oneDark } from '@codemirror/theme-one-dark'
+import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark'
const languageLoaders: Partial<Record<string, () => Promise<LanguageSupport>>> =
{
@@ -99,10 +99,25 @@ interface CodeEditorProps {
initialContent: string
filename: string
isDark?: boolean
+ readOnly?: boolean
+ enableLanguageSupport?: boolean
+ lineWrapping?: boolean
+ appearance?: 'editor' | 'plain'
}
export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
- function CodeEditor({ initialContent, filename, isDark = false }, ref) {
+ function CodeEditor(
+ {
+ initialContent,
+ filename,
+ isDark = false,
+ readOnly = false,
+ enableLanguageSupport = true,
+ lineWrapping = false,
+ appearance = 'editor',
+ },
+ ref,
+ ) {
const containerRef = useRef<HTMLDivElement>(null)
const viewRef = useRef<EditorView | null>(null)
@@ -118,41 +133,91 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
const setup = async () => {
const extensions = [
lineNumbers(),
- highlightActiveLineGutter(),
- highlightActiveLine(),
drawSelection(),
- history(),
- foldGutter(),
- indentOnInput(),
- bracketMatching(),
- closeBrackets(),
- autocompletion(),
- highlightSelectionMatches(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
- keymap.of([
- ...closeBracketsKeymap,
- ...defaultKeymap,
- ...searchKeymap,
- ...historyKeymap,
- indentWithTab,
- ]),
+ EditorState.readOnly.of(readOnly),
+ EditorView.editable.of(!readOnly),
EditorView.theme({
- '&': { height: '100%' },
- '.cm-scroller': { overflow: 'auto' },
+ '&': {
+ height: '100%',
+ backgroundColor:
+ appearance === 'plain'
+ ? 'var(--background)'
+ : 'color-mix(in oklch, var(--muted) 28%, var(--background))',
+ color: 'var(--foreground)',
+ },
+ '&.cm-focused': { outline: 'none' },
+ '.cm-scroller': {
+ overflow: 'auto',
+ backgroundColor: 'transparent',
+ },
'.cm-content': {
+ caretColor: 'var(--primary)',
fontFamily:
'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace',
fontSize: '13px',
},
- '.cm-gutters': { fontSize: '13px' },
+ '.cm-gutters': {
+ backgroundColor:
+ appearance === 'plain'
+ ? 'var(--background)'
+ : 'color-mix(in oklch, var(--muted) 44%, var(--background))',
+ borderRight:
+ appearance === 'plain' ? 'none' : '1px solid var(--border)',
+ color: 'var(--muted-foreground)',
+ fontSize: '13px',
+ },
+ '.cm-lineNumbers .cm-gutterElement': {
+ paddingLeft: '8px',
+ paddingRight: '10px',
+ },
+ '.cm-cursor, .cm-dropCursor': {
+ borderLeftColor: 'var(--primary)',
+ },
+ '&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection':
+ {
+ backgroundColor:
+ 'color-mix(in oklch, var(--primary) 24%, transparent)',
+ },
+ '.cm-activeLine, .cm-activeLineGutter': {
+ backgroundColor:
+ 'color-mix(in oklch, var(--primary) 8%, transparent)',
+ },
}),
]
+ if (readOnly) {
+ extensions.push(keymap.of(searchKeymap))
+ } else {
+ extensions.push(
+ highlightActiveLineGutter(),
+ highlightActiveLine(),
+ history(),
+ foldGutter(),
+ indentOnInput(),
+ bracketMatching(),
+ closeBrackets(),
+ autocompletion(),
+ highlightSelectionMatches(),
+ keymap.of([
+ ...closeBracketsKeymap,
+ ...defaultKeymap,
+ ...searchKeymap,
+ ...historyKeymap,
+ indentWithTab,
+ ]),
+ )
+ }
+
+ if (lineWrapping) {
+ extensions.push(EditorView.lineWrapping)
+ }
+
if (isDark) {
- extensions.push(oneDark)
+ extensions.push(syntaxHighlighting(oneDarkHighlightStyle))
}
- const lang = detectLanguage(filename)
+ const lang = enableLanguageSupport ? detectLanguage(filename) : null
if (lang && languageLoaders[lang]) {
try {
const langSupport = await languageLoaders[lang]()
@@ -184,12 +249,24 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
viewRef.current?.destroy()
viewRef.current = null
}
- }, [filename, isDark, initialContent])
+ }, [
+ enableLanguageSupport,
+ appearance,
+ filename,
+ isDark,
+ initialContent,
+ lineWrapping,
+ readOnly,
+ ])
return (
<div
ref={containerRef}
- className="h-full min-h-0 overflow-hidden rounded-md border"
+ className={
+ appearance === 'plain'
+ ? 'h-full min-h-0 overflow-hidden bg-background'
+ : 'h-full min-h-0 overflow-hidden rounded-md border shadow-sm'
+ }
/>
)
},
diff --git a/web-studio/src/routes/resources/-components/file-preview.tsx b/web-studio/src/routes/resources/-components/file-preview.tsx
index 5aa08de90..dba314d7c 100644
--- a/web-studio/src/routes/resources/-components/file-preview.tsx
+++ b/web-studio/src/routes/resources/-components/file-preview.tsx
@@ -19,8 +19,10 @@ import { formatSize, normalizeReadContent } from '../-lib/normalize'
import { fetchDirectoryLevelContent, saveFileContent } from '../-lib/api'
import {
useVikingFilePreview,
+ useVikingFsStat,
useInvalidateVikingFs,
} from '../-hooks/viking-fm'
+import { useJsonFormat } from '../-hooks/use-json-format'
import type { VikingFsEntry } from '../-types/viking-fm'
import type { CodeEditorHandle } from './code-editor'
@@ -134,6 +136,7 @@ const DIRECTORY_LEVEL_META: Array<{
]
const JSONL_MESSAGE_PREVIEW_LIMIT = 720
+const LARGE_FILE_PREVIEW_BYTES = 2 * 1024 * 1024
const JSONL_TOOLCALL_STORAGE_KEY = 'openviking.playground.jsonlToolCall'
const COLLAPSE_SYMBOL = '▾'
const EXPAND_SYMBOL = '▸'
@@ -1024,25 +1027,52 @@ export function FilePreview({
showCloseButton = true,
}: FilePreviewProps) {
const { t } = useTranslation('resources')
+ const isJsonPath = Boolean(
+ file && !file.isDir && file.name.toLowerCase().endsWith('.json'),
+ )
+ const needsMetadata = Boolean(
+ isJsonPath && file && (file.sizeBytes === null || !file.modTime),
+ )
+ const statQuery = useVikingFsStat(needsMetadata ? file?.uri : undefined)
+ const resolvedFile = useMemo(() => {
+ if (!file || !statQuery.data || statQuery.data.uri !== file.uri) {
+ return file
+ }
+ return {
+ ...file,
+ ...statQuery.data,
+ }
+ }, [file, statQuery.data])
+ const isJsonFile = isJsonPath
const previewQuery = useVikingFilePreview(
- file,
+ resolvedFile,
{
- maxAutoReadBytes: 2 * 1024 * 1024,
+ maxAutoReadBytes: LARGE_FILE_PREVIEW_BYTES,
defaultReadLimit: -1,
+ requireKnownSize: isJsonFile,
},
{
raw: true,
},
)
const preview = previewQuery.preview
+ const metadataLoading = needsMetadata && statQuery.isPending
+ const hasPreviewContent =
+ Boolean(preview?.shouldAutoRead) || previewQuery.isContentLoaded
+ const previewLoading = metadataLoading || previewQuery.isLoading
const displayContent = useMemo(
() => memoryFieldsDisplayContent(preview?.content || ''),
[preview?.content],
)
+ const jsonFormat = useJsonFormat(
+ displayContent,
+ isJsonFile && hasPreviewContent,
+ )
const directoryPreview = useDirectoryPreview(file)
const [markdownMode, setMarkdownMode] = useState<'preview' | 'source'>(
'preview',
)
+ const [jsonMode, setJsonMode] = useState<'preview' | 'source'>('preview')
const [activeDirectoryLevels, setActiveDirectoryLevels] = useState<
Set<DirectoryLevelId>
>(new Set(['abstract', 'overview']))
@@ -1061,6 +1091,7 @@ export function FilePreview({
useEffect(() => {
setMarkdownMode('preview')
+ setJsonMode('preview')
setEditing(false)
setActiveDirectoryLevels(new Set(['abstract', 'overview']))
}, [file?.uri])
@@ -1082,7 +1113,7 @@ export function FilePreview({
const [highlightedCodeHtml, setHighlightedCodeHtml] = useState('')
const needsHighlight =
- preview?.fileType === 'code' ||
+ (preview?.fileType === 'code' && !isJsonFile) ||
(preview?.fileType === 'markdown' && markdownMode === 'source')
useEffect(() => {
@@ -1208,11 +1239,18 @@ export function FilePreview({
activeDirectoryLevels.has(level.id),
)
const showHeader = !(hideDirectoryHeader && file.isDir)
+ const previewFile = resolvedFile || file
+ const showJsonPreview =
+ !editing &&
+ !previewLoading &&
+ preview?.fileType === 'code' &&
+ hasPreviewContent &&
+ isJsonFile
return (
<div className="flex h-full min-h-0 flex-col overflow-hidden">
{showHeader ? (
- <div className="flex min-h-14 items-center justify-between border-b px-4">
+ <div className="flex min-h-14 shrink-0 items-center justify-between border-b px-4">
<div className="flex min-w-0 items-center gap-2">
<div className="min-w-0">
<div className="truncate text-sm font-medium leading-5">
@@ -1220,8 +1258,8 @@ export function FilePreview({
</div>
{!file.isDir ? (
<div className="text-xs leading-5 text-muted-foreground">
- {formatSize(file.sizeBytes ?? file.size)} ·{' '}
- {file.modTime || '-'}
+ {formatSize(previewFile.sizeBytes ?? previewFile.size)} ·{' '}
+ {previewFile.modTime || '-'}
</div>
) : null}
</div>
@@ -1277,7 +1315,7 @@ export function FilePreview({
) : null}
{editing && preview?.content != null ? (
- <div className="h-full min-h-0 p-2">
+ <div className="min-h-0 flex-1 p-2">
<Suspense
fallback={
<div className="flex h-full items-center justify-center text-sm text-muted-foreground">
@@ -1294,8 +1332,80 @@ export function FilePreview({
/>
</Suspense>
</div>
+ ) : showJsonPreview ? (
+ <div className="min-h-0 flex-1 p-2">
+ <div className="flex h-full min-h-0 flex-col">
+ <div className="mb-2 inline-flex shrink-0 self-start overflow-hidden rounded-md border">
+ <button
+ type="button"
+ className={`px-3 py-1.5 text-xs ${
+ jsonMode === 'preview'
+ ? 'bg-muted font-medium text-foreground'
+ : 'text-muted-foreground hover:bg-muted/60'
+ }`}
+ onClick={() => setJsonMode('preview')}
+ >
+ {t('filePreview.markdownPreview')}
+ </button>
+ <button
+ type="button"
+ className={`px-3 py-1.5 text-xs ${
+ jsonMode === 'source'
+ ? 'bg-muted font-medium text-foreground'
+ : 'text-muted-foreground hover:bg-muted/60'
+ }`}
+ onClick={() => setJsonMode('source')}
+ >
+ {t('filePreview.markdownSource')}
+ </button>
+ </div>
+
+ {jsonMode === 'preview' && jsonFormat.isFormatting ? (
+ <div className="flex min-h-0 flex-1 items-center justify-center text-sm text-muted-foreground">
+ <Loader2 className="mr-2 size-4 animate-spin" />
+ {t('filePreview.formattingJson')}
+ </div>
+ ) : (
+ <div className="flex min-h-0 flex-1 flex-col gap-2">
+ {jsonMode === 'preview' && jsonFormat.error ? (
+ <div className="shrink-0 text-xs text-destructive">
+ {t('filePreview.jsonFormatFailed')}
+ </div>
+ ) : null}
+ <div className="min-h-0 flex-1">
+ <Suspense
+ fallback={
+ <div className="flex h-full items-center justify-center text-sm text-muted-foreground">
+ <Loader2 className="mr-2 size-4 animate-spin" />
+ {t('filePreview.loadingEditor')}
+ </div>
+ }
+ >
+ <LazyCodeEditor
+ initialContent={
+ jsonMode === 'preview'
+ ? jsonFormat.content
+ : displayContent
+ }
+ filename={previewFile.name}
+ isDark={isDark}
+ readOnly
+ appearance={jsonMode === 'preview' ? 'plain' : 'editor'}
+ enableLanguageSupport={
+ (previewFile.sizeBytes ?? 0) <= LARGE_FILE_PREVIEW_BYTES
+ }
+ lineWrapping={
+ jsonMode === 'source' || Boolean(jsonFormat.error)
+ }
+ />
+ </Suspense>
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
) : (
- <ScrollArea className="h-full min-h-0">
+ <ScrollArea className="min-h-0 flex-1">
<div className="mx-auto min-h-full w-full max-w-5xl p-4">
{isMarkdown && !editing ? (
<div className="mb-3 inline-flex overflow-hidden rounded-md border">
@@ -1437,27 +1547,42 @@ export function FilePreview({
)
) : null}
- {previewQuery.isLoading && preview?.fileType !== 'image' ? (
+ {previewLoading && preview?.fileType !== 'image' ? (
<div className="text-sm text-muted-foreground">
{t('filePreview.loadingContent')}
</div>
) : null}
- {!previewQuery.isLoading &&
+ {!previewLoading &&
preview &&
!file.isDir &&
preview.fileType !== 'image' &&
- !preview.shouldAutoRead ? (
- <div className="text-sm text-muted-foreground">
- {preview.reason === 'binary'
- ? t('filePreview.unsupportedBinary')
- : t('filePreview.largeFileSkipped')}
+ !hasPreviewContent ? (
+ <div className="space-y-3 text-sm text-muted-foreground">
+ <div>
+ {preview.reason === 'binary'
+ ? t('filePreview.unsupportedBinary')
+ : t('filePreview.largeFileSkipped')}
+ </div>
+ {previewQuery.canLoadContent && isJsonFile ? (
+ <Button
+ size="sm"
+ variant="outline"
+ disabled={previewQuery.isFetching}
+ onClick={() => void previewQuery.refetch()}
+ >
+ {previewQuery.isFetching ? (
+ <Loader2 className="mr-2 size-4 animate-spin" />
+ ) : null}
+ {t('filePreview.loadFile')}
+ </Button>
+ ) : null}
</div>
) : null}
- {!previewQuery.isLoading &&
+ {!previewLoading &&
preview?.fileType === 'markdown' &&
- preview.shouldAutoRead &&
+ hasPreviewContent &&
markdownMode === 'preview' ? (
<article className="prose prose-sm max-w-none break-words dark:prose-invert dark:prose-pre:bg-muted-foreground/20">
<ReactMarkdown
@@ -1496,9 +1621,9 @@ export function FilePreview({
</article>
) : null}
- {!previewQuery.isLoading &&
+ {!previewLoading &&
preview?.fileType === 'markdown' &&
- preview.shouldAutoRead &&
+ hasPreviewContent &&
markdownMode === 'source' ? (
<pre className="overflow-auto rounded-md border bg-muted/20 p-3 text-xs leading-6">
<code
@@ -1512,9 +1637,10 @@ export function FilePreview({
</pre>
) : null}
- {!previewQuery.isLoading &&
+ {!previewLoading &&
preview?.fileType === 'code' &&
- preview.shouldAutoRead ? (
+ hasPreviewContent &&
+ !isJsonFile ? (
<pre className="overflow-auto rounded-md border bg-muted/20 p-3 text-xs leading-6">
<code
className="hljs block"
@@ -1527,19 +1653,19 @@ export function FilePreview({
</pre>
) : null}
- {!previewQuery.isLoading &&
+ {!previewLoading &&
preview?.fileType === 'jsonl' &&
- preview.shouldAutoRead ? (
+ hasPreviewContent ? (
<JsonlPreview content={displayContent || ''} />
) : null}
- {!previewQuery.isLoading &&
+ {!previewLoading &&
preview &&
preview.fileType !== 'image' &&
preview.fileType !== 'markdown' &&
preview.fileType !== 'jsonl' &&
preview.fileType !== 'code' &&
- preview.shouldAutoRead ? (
+ hasPreviewContent ? (
<pre className="whitespace-pre-wrap break-words text-xs leading-6">
{displayContent || emptyFileText}
</pre>
diff --git a/web-studio/src/routes/resources/-hooks/use-json-format.ts b/web-studio/src/routes/resources/-hooks/use-json-format.ts
new file mode 100644
index 000000000..07e288e79
--- /dev/null
+++ b/web-studio/src/routes/resources/-hooks/use-json-format.ts
@@ -0,0 +1,61 @@
+import { useEffect, useState } from 'react'
+
+import JsonFormatWorker from '../-lib/json-format.worker?worker'
+import type { JsonFormatRequest, JsonFormatResponse } from '../-lib/json-format'
+
+interface JsonFormatState extends JsonFormatResponse {
+ source: string
+}
+
+const EMPTY_STATE: JsonFormatState = {
+ source: '',
+ content: '',
+ error: null,
+}
+
+export function useJsonFormat(content: string, enabled: boolean) {
+ const [state, setState] = useState<JsonFormatState>(EMPTY_STATE)
+
+ useEffect(() => {
+ if (!enabled || !content) {
+ return
+ }
+
+ const worker = new JsonFormatWorker()
+ let active = true
+
+ worker.onmessage = (event: MessageEvent<JsonFormatResponse>) => {
+ if (!active) return
+ setState({
+ source: content,
+ content: event.data.content,
+ error: event.data.error,
+ })
+ worker.terminate()
+ }
+ worker.onerror = (event) => {
+ if (!active) return
+ setState({
+ source: content,
+ content,
+ error: event.message,
+ })
+ worker.terminate()
+ }
+
+ const request: JsonFormatRequest = { content }
+ worker.postMessage(request)
+
+ return () => {
+ active = false
+ worker.terminate()
+ }
+ }, [content, enabled])
+
+ const isCurrent = state.source === content
+ return {
+ content: isCurrent ? state.content : content,
+ error: isCurrent ? state.error : null,
+ isFormatting: enabled && Boolean(content) && !isCurrent,
+ }
+}
diff --git a/web-studio/src/routes/resources/-hooks/viking-fm.test.tsx b/web-studio/src/routes/resources/-hooks/viking-fm.test.tsx
new file mode 100644
index 000000000..47807fa42
--- /dev/null
+++ b/web-studio/src/routes/resources/-hooks/viking-fm.test.tsx
@@ -0,0 +1,82 @@
+// @vitest-environment jsdom
+
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { act, renderHook, waitFor } from '@testing-library/react'
+import type { PropsWithChildren } from 'react'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { fetchFileContent } from '../-lib/api'
+import type { VikingFsEntry } from '../-types/viking-fm'
+import { useVikingFilePreview } from './viking-fm'
+
+vi.mock('../-lib/api', () => {
+ return {
+ fetchFileContent: vi.fn(),
+ fetchFind: vi.fn(),
+ fetchFindAllTypes: vi.fn(),
+ fetchFsList: vi.fn(),
+ fetchFsStat: vi.fn(),
+ fetchFsTree: vi.fn(),
+ }
+})
+
+const largeJson: VikingFsEntry = {
+ uri: 'viking://resources/large.json',
+ name: 'large.json',
+ isDir: false,
+ size: '3 MiB',
+ sizeBytes: 3 * 1024 * 1024,
+ modTime: '2026-07-31 10:00',
+ modTimestamp: null,
+ abstract: '',
+ overview: '',
+}
+
+describe('useVikingFilePreview', () => {
+ beforeEach(() => {
+ vi.mocked(fetchFileContent).mockReset()
+ })
+
+ it('exposes explicitly loaded content for a large file', async () => {
+ vi.mocked(fetchFileContent).mockResolvedValue({
+ uri: largeJson.uri,
+ content: '{"loaded":true}',
+ offset: 0,
+ limit: -1,
+ truncated: false,
+ })
+ const queryClient = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ })
+ const wrapper = ({ children }: PropsWithChildren) => (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ )
+ const { result } = renderHook(
+ () =>
+ useVikingFilePreview(
+ largeJson,
+ {
+ maxAutoReadBytes: 2 * 1024 * 1024,
+ defaultReadLimit: -1,
+ requireKnownSize: true,
+ },
+ { raw: true },
+ ),
+ { wrapper },
+ )
+
+ expect(result.current.preview?.shouldAutoRead).toBe(false)
+ expect(result.current.canLoadContent).toBe(true)
+ expect(fetchFileContent).not.toHaveBeenCalled()
+
+ await act(async () => {
+ await result.current.refetch()
+ })
+
+ await waitFor(() => {
+ expect(result.current.isContentLoaded).toBe(true)
+ })
+ expect(result.current.preview?.content).toBe('{"loaded":true}')
+ expect(fetchFileContent).toHaveBeenCalledOnce()
+ })
+})
diff --git a/web-studio/src/routes/resources/-hooks/viking-fm.ts b/web-studio/src/routes/resources/-hooks/viking-fm.ts
index 62bc5d3c2..aa042b2de 100644
--- a/web-studio/src/routes/resources/-hooks/viking-fm.ts
+++ b/web-studio/src/routes/resources/-hooks/viking-fm.ts
@@ -90,6 +90,7 @@ export function useVikingFilePreview(
) {
const maxAutoReadBytes = policy.maxAutoReadBytes ?? 2 * 1024 * 1024
const defaultReadLimit = policy.defaultReadLimit ?? 500
+ const requireKnownSize = policy.requireKnownSize ?? false
const effectiveReadOptions = useMemo(
() => ({
offset: readOptions.offset ?? 0,
@@ -102,9 +103,9 @@ export function useVikingFilePreview(
const autoRead = useMemo(
() =>
entry
- ? shouldAutoRead(entry, maxAutoReadBytes)
+ ? shouldAutoRead(entry, maxAutoReadBytes, requireKnownSize)
: { shouldRead: false as const },
- [entry, maxAutoReadBytes],
+ [entry, maxAutoReadBytes, requireKnownSize],
)
const readQuery = useQuery({
@@ -131,10 +132,10 @@ export function useVikingFilePreview(
fileType,
shouldAutoRead: false,
reason: autoRead.reason,
- content: '',
- offset: effectiveReadOptions.offset,
- limit: effectiveReadOptions.limit,
- truncated: true,
+ content: readQuery.data?.content || '',
+ offset: readQuery.data?.offset ?? effectiveReadOptions.offset,
+ limit: readQuery.data?.limit ?? effectiveReadOptions.limit,
+ truncated: readQuery.data?.truncated ?? true,
}
}
@@ -152,7 +153,9 @@ export function useVikingFilePreview(
return {
...readQuery,
preview,
- canLoadContent: Boolean(entry) && autoRead.shouldRead,
+ isContentLoaded: readQuery.data !== undefined,
+ canLoadContent:
+ Boolean(entry) && !autoRead.shouldRead && autoRead.reason !== 'binary',
}
}
diff --git a/web-studio/src/routes/resources/-lib/json-format.test.ts b/web-studio/src/routes/resources/-lib/json-format.test.ts
new file mode 100644
index 000000000..c013d0642
--- /dev/null
+++ b/web-studio/src/routes/resources/-lib/json-format.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from 'vitest'
+
+import { formatJsonContent } from './json-format'
+
+describe('formatJsonContent', () => {
+ it('adds indentation and line breaks to compact JSON', () => {
+ expect(formatJsonContent('{"items":[{"id":1},{"id":2}]}')).toBe(
+ [
+ '{',
+ ' "items": [',
+ ' {',
+ ' "id": 1',
+ ' },',
+ ' {',
+ ' "id": 2',
+ ' }',
+ ' ]',
+ '}',
+ ].join('\n'),
+ )
+ })
+
+ it('throws for invalid JSON', () => {
+ expect(() => formatJsonContent('{"invalid"}')).toThrow()
+ })
+})
diff --git a/web-studio/src/routes/resources/-lib/json-format.ts b/web-studio/src/routes/resources/-lib/json-format.ts
new file mode 100644
index 000000000..60606becc
--- /dev/null
+++ b/web-studio/src/routes/resources/-lib/json-format.ts
@@ -0,0 +1,12 @@
+export interface JsonFormatRequest {
+ content: string
+}
+
+export interface JsonFormatResponse {
+ content: string
+ error: string | null
+}
+
+export function formatJsonContent(content: string): string {
+ return JSON.stringify(JSON.parse(content), null, 2)
+}
diff --git a/web-studio/src/routes/resources/-lib/json-format.worker.ts b/web-studio/src/routes/resources/-lib/json-format.worker.ts
new file mode 100644
index 000000000..964392a3d
--- /dev/null
+++ b/web-studio/src/routes/resources/-lib/json-format.worker.ts
@@ -0,0 +1,21 @@
+import { formatJsonContent } from './json-format'
+import type { JsonFormatRequest, JsonFormatResponse } from './json-format'
+
+const workerScope = self as unknown as {
+ onmessage: ((event: MessageEvent<JsonFormatRequest>) => void) | null
+ postMessage: (message: JsonFormatResponse) => void
+}
+
+workerScope.onmessage = (event) => {
+ try {
+ workerScope.postMessage({
+ content: formatJsonContent(event.data.content),
+ error: null,
+ })
+ } catch (error) {
+ workerScope.postMessage({
+ content: event.data.content,
+ error: error instanceof Error ? error.message : String(error),
+ })
+ }
+}
diff --git a/web-studio/src/routes/resources/-lib/normalize.test.ts b/web-studio/src/routes/resources/-lib/normalize.test.ts
new file mode 100644
index 000000000..cf273547a
--- /dev/null
+++ b/web-studio/src/routes/resources/-lib/normalize.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it } from 'vitest'
+
+import { shouldAutoRead } from './normalize'
+
+const jsonEntry = {
+ isDir: false,
+ uri: 'viking://resources/example.json',
+ sizeBytes: null,
+}
+
+describe('shouldAutoRead', () => {
+ it('waits for JSON size metadata when required', () => {
+ expect(shouldAutoRead(jsonEntry, 2 * 1024 * 1024, true)).toEqual({
+ shouldRead: false,
+ reason: 'unknown-size',
+ })
+ })
+
+ it('automatically reads a known small JSON file', () => {
+ expect(
+ shouldAutoRead({ ...jsonEntry, sizeBytes: 1024 }, 2 * 1024 * 1024, true),
+ ).toEqual({ shouldRead: true })
+ })
+
+ it('requires an explicit load for a known large JSON file', () => {
+ expect(
+ shouldAutoRead(
+ { ...jsonEntry, sizeBytes: 3 * 1024 * 1024 },
+ 2 * 1024 * 1024,
+ true,
+ ),
+ ).toEqual({ shouldRead: false, reason: 'too-large' })
+ })
+})
diff --git a/web-studio/src/routes/resources/-lib/normalize.ts b/web-studio/src/routes/resources/-lib/normalize.ts
index 92d1c0ddb..de839bfa7 100644
--- a/web-studio/src/routes/resources/-lib/normalize.ts
+++ b/web-studio/src/routes/resources/-lib/normalize.ts
@@ -285,9 +285,10 @@ export function isLikelyBinary(uri: string): boolean {
export function shouldAutoRead(
entry: Pick<VikingFsEntry, 'isDir' | 'uri' | 'sizeBytes'>,
maxAutoReadBytes = 2 * 1024 * 1024,
+ requireKnownSize = false,
): {
shouldRead: boolean
- reason?: 'binary' | 'too-large'
+ reason?: 'binary' | 'too-large' | 'unknown-size'
} {
if (entry.isDir) {
return { shouldRead: false }
@@ -298,6 +299,10 @@ export function shouldAutoRead(
return { shouldRead: false, reason: 'binary' }
}
+ if (requireKnownSize && entry.sizeBytes === null) {
+ return { shouldRead: false, reason: 'unknown-size' }
+ }
+
if (entry.sizeBytes !== null && entry.sizeBytes > maxAutoReadBytes) {
return { shouldRead: false, reason: 'too-large' }
}
diff --git a/web-studio/src/routes/resources/-types/viking-fm.ts b/web-studio/src/routes/resources/-types/viking-fm.ts
index b16483995..a3ec817fa 100644
--- a/web-studio/src/routes/resources/-types/viking-fm.ts
+++ b/web-studio/src/routes/resources/-types/viking-fm.ts
@@ -67,13 +67,14 @@ export interface VikingReadResult {
export interface VikingPreviewPolicy {
maxAutoReadBytes?: number
defaultReadLimit?: number
+ requireKnownSize?: boolean
}
export interface VikingPreviewResult {
entry: VikingFsEntry
fileType: VikingFileType
shouldAutoRead: boolean
- reason?: 'binary' | 'too-large'
+ reason?: 'binary' | 'too-large' | 'unknown-size'
content: string
offset: number
limit: number