From de7df11fcfa0379bbf70d48276386886ffa08440 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Thu, 10 Sep 2026 15:56:07 -0700 Subject: [PATCH] feat(playground): migrate client to React --- playground/deploy/start.sh | 1 + playground/index.html | 51 +----- playground/package-lock.json | 63 ++++++- playground/package.json | 5 + playground/server.mjs | 4 +- playground/src/code-editors.tsx | 147 ++++++++++++++++ playground/src/dom.ts | 24 --- playground/src/file-tree-panel.tsx | 43 +++++ playground/src/file-tree.ts | 7 +- playground/src/main.ts | 4 - playground/src/main.tsx | 22 +++ playground/src/markdown-editor.tsx | 61 +++++++ playground/src/persistence.ts | 14 +- playground/src/playground-app.ts | 262 ----------------------------- playground/src/playground-app.tsx | 229 +++++++++++++++++++++++++ playground/src/style.css | 86 +++++++++- playground/tsconfig.json | 1 + 17 files changed, 667 insertions(+), 357 deletions(-) create mode 100644 playground/src/code-editors.tsx delete mode 100644 playground/src/dom.ts create mode 100644 playground/src/file-tree-panel.tsx delete mode 100644 playground/src/main.ts create mode 100644 playground/src/main.tsx create mode 100644 playground/src/markdown-editor.tsx delete mode 100644 playground/src/playground-app.ts create mode 100644 playground/src/playground-app.tsx diff --git a/playground/deploy/start.sh b/playground/deploy/start.sh index 330544f21c4..e8c43055ed7 100644 --- a/playground/deploy/start.sh +++ b/playground/deploy/start.sh @@ -48,6 +48,7 @@ runuser -u playground -- env -i \ NODE_ENV=production \ HOST=0.0.0.0 \ PORT=8080 \ + PLAYGROUND_USERNAME="${OPENCODE_SERVER_USERNAME:-opencode}" \ PLAYGROUND_PASSWORD="${OPENCODE_SERVER_PASSWORD}" \ PLAYGROUND_REPOSITORY_DIR="${repository_directory}" \ PLAYGROUND_WORKSPACE_DIR="${workspace_directory}" \ diff --git a/playground/index.html b/playground/index.html index 9262b39f779..2da41ac0ee6 100644 --- a/playground/index.html +++ b/playground/index.html @@ -7,54 +7,7 @@ LiteLLM Rust Playground -
-
-
- LOCAL PROOF -

LiteLLM Rust Playground

-
-
- Connecting to rust-analyzer - Diagnostics pending - - -
-
- -
- - -
-
- src/main.rs - -
-
-
- -
-
- -
-
- OUTPUT - -
-
Press Run to compile against the cloned LiteLLM repository.
-
-
-
- +
+ diff --git a/playground/package-lock.json b/playground/package-lock.json index 77cc3408447..d4109d988d9 100644 --- a/playground/package-lock.json +++ b/playground/package-lock.json @@ -12,11 +12,16 @@ "@codemirror/theme-one-dark": "^6.1.3", "@codemirror/view": "^6.43.11", "@pierre/trees": "^1.0.0-beta.6", + "@tanstack/react-query": "^5.102.8", "codemirror": "^6.0.2", + "react": "^19.3.0", + "react-dom": "^19.3.0", "vite": "^7.1.5", "ws": "^8.18.3" }, "devDependencies": { + "@types/react": "^19.3.0", + "@types/react-dom": "^19.3.0", "typescript": "^5.9.2" } }, @@ -1127,12 +1132,58 @@ "win32" ] }, + "node_modules/@tanstack/query-core": { + "version": "5.102.8", + "resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.102.8.tgz", + "integrity": "sha512-ZNjkJ33CqvPNec/6lZBnHqLc3EVGPZ9ySLhYahU9TcuRFdmwXewuj0c4hwSWcGHqEUwcSrKeZ+oGcvPBqXcQcg==", + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + } + }, + "node_modules/@tanstack/react-query": { + "version": "5.102.8", + "resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.102.8.tgz", + "integrity": "sha512-TYBea4OuXWD7MhaSHq069TWbFe7rcwWN6kzT7JF0OKi1K6c1gTv2IzD6A6ExJsCMozdkqBWeuIUZmu4KQg0O5A==", + "license": "MIT", + "dependencies": { + "@tanstack/query-core": "5.102.8" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + }, + "peerDependencies": { + "react": "^18 || ^19" + } + }, "node_modules/@types/estree": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz", "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==", "license": "MIT" }, + "node_modules/@types/react": { + "version": "19.3.0", + "resolved": "https://registry.npmjs.org/@types/react/-/react-19.3.0.tgz", + "integrity": "sha512-N0rFCuH9YoxG9/m61l9MfpJKfmLOVU0em7ipIz6TRgSSkvReLB9vL85GB+yr8Bs5leqpvg96JSwF4ZS1s4viQg==", + "dev": true, + "license": "MIT", + "dependencies": { + "csstype": "^3.2.2" + } + }, + "node_modules/@types/react-dom": { + "version": "19.3.0", + "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-19.3.0.tgz", + "integrity": "sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "@types/react": "^19.3.0" + } + }, "node_modules/codemirror": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/codemirror/-/codemirror-6.0.2.tgz", @@ -1154,6 +1205,13 @@ "integrity": "sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA==", "license": "MIT" }, + "node_modules/csstype": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz", + "integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==", + "dev": true, + "license": "MIT" + }, "node_modules/esbuild": { "version": "0.28.2", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", @@ -1326,7 +1384,6 @@ "resolved": "https://registry.npmjs.org/react/-/react-19.3.0.tgz", "integrity": "sha512-E8LUcbtBWt20bbl2YoHfx4ZDBdxVTfOKtCZn9cDSJ4l6/nuoApcpIBcj47t2wZoVX8g2ZHuMHbiShgCR1T5Sog==", "license": "MIT", - "peer": true, "engines": { "node": ">=0.10.0" } @@ -1336,7 +1393,6 @@ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.3.0.tgz", "integrity": "sha512-JDk8dgif51OjFoDE70+OT9ICyYr+69HlmihNwp1+Nsfbna3t5sIiCa9ZJktDmQ4/1b/rn26hIAR2uYXDMr5r0Q==", "license": "MIT", - "peer": true, "dependencies": { "scheduler": "^0.28.0" }, @@ -1393,8 +1449,7 @@ "version": "0.28.0", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.28.0.tgz", "integrity": "sha512-juorfCmIkIw8tT+p5BXSm6PJjQF/ycEYmKyzURCIt/RaZIhL+PulbQ9Yu2z1HdOJDdqDTlxA1+xKBmHXJsczAw==", - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/source-map-js": { "version": "1.2.1", diff --git a/playground/package.json b/playground/package.json index c776a6fc3ec..1f1402be084 100644 --- a/playground/package.json +++ b/playground/package.json @@ -13,11 +13,16 @@ "@codemirror/theme-one-dark": "^6.1.3", "@codemirror/view": "^6.43.11", "@pierre/trees": "^1.0.0-beta.6", + "@tanstack/react-query": "^5.102.8", "codemirror": "^6.0.2", + "react": "^19.3.0", + "react-dom": "^19.3.0", "vite": "^7.1.5", "ws": "^8.18.3" }, "devDependencies": { + "@types/react": "^19.3.0", + "@types/react-dom": "^19.3.0", "typescript": "^5.9.2" } } diff --git a/playground/server.mjs b/playground/server.mjs index ff8bde3c27a..67f6274c390 100644 --- a/playground/server.mjs +++ b/playground/server.mjs @@ -17,14 +17,16 @@ const host = process.env.HOST ?? '127.0.0.1'; const isProduction = process.env.NODE_ENV === 'production'; const staticDirectory = path.join(playgroundDirectory, 'dist'); const playgroundPassword = process.env.PLAYGROUND_PASSWORD; +const playgroundUsername = process.env.PLAYGROUND_USERNAME ?? 'opencode'; const commandEnvironment = { ...process.env }; delete commandEnvironment.PLAYGROUND_PASSWORD; +delete commandEnvironment.PLAYGROUND_USERNAME; const isAuthorized = request => { if (!playgroundPassword) { return true; } - const expected = Buffer.from(`opencode:${playgroundPassword}`); + const expected = Buffer.from(`${playgroundUsername}:${playgroundPassword}`); const authorization = request.headers.authorization ?? ''; const actual = authorization.startsWith('Basic ') ? Buffer.from(authorization.slice('Basic '.length), 'base64') diff --git a/playground/src/code-editors.tsx b/playground/src/code-editors.tsx new file mode 100644 index 00000000000..aa2958b6ad0 --- /dev/null +++ b/playground/src/code-editors.tsx @@ -0,0 +1,147 @@ +import { rust } from '@codemirror/lang-rust'; +import { languageServerExtensions, LSPClient } from '@codemirror/lsp-client'; +import { oneDark } from '@codemirror/theme-one-dark'; +import { EditorView, basicSetup } from 'codemirror'; +import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'; + +import { commandClickDefinition, connectTransport, PlaygroundWorkspace } from './lsp'; +import type { PlaygroundExample, PlaygroundInfo, StoredFiles } from './types'; + +export type CodeEditorsHandle = { + getFiles: (example: PlaygroundExample) => StoredFiles; + goToLine: (uri: string, requestedLine: number) => void; + reset: (example: PlaygroundExample) => void; +}; + +type CodeEditorsProps = { + activeUri: string; + info: PlaygroundInfo; + onConnectionChange: (state: 'connecting' | 'ready' | 'error', message?: string) => void; + onDiagnostics: (message: string, hasDiagnostics: boolean) => void; + onOpenFile: (uri: string) => boolean; + onSave: (target: string, source: string) => void; +}; + +export const CodeEditors = forwardRef(function CodeEditors( + { activeUri, info, onConnectionChange, onDiagnostics, onOpenFile, onSave }, + handle, +) { + const containers = useRef(new Map()); + const views = useRef(new Map()); + const callbacks = useRef({ onConnectionChange, onDiagnostics, onOpenFile, onSave }); + callbacks.current = { onConnectionChange, onDiagnostics, onOpenFile, onSave }; + + useImperativeHandle(handle, () => ({ + getFiles: example => Object.fromEntries( + example.files.map(file => [file.path, views.current.get(file.uri)?.state.doc.toString() ?? file.source]), + ), + goToLine: (uri, requestedLine) => { + const view = views.current.get(uri); + if (!view) { + return; + } + const line = view.state.doc.line(Math.min(Math.max(requestedLine, 1), view.state.doc.lines)); + view.dispatch({ + selection: { anchor: line.from }, + effects: EditorView.scrollIntoView(line.from, { y: 'center' }), + }); + view.focus(); + }, + reset: example => example.files.forEach(file => { + const view = views.current.get(file.uri); + view?.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: file.source } }); + }), + }), []); + + useEffect(() => { + let disposed = false; + const setup = async () => { + callbacks.current.onConnectionChange('connecting'); + const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'; + const transport = await connectTransport(`${protocol}//${location.host}/lsp`, () => { + if (!disposed) { + callbacks.current.onConnectionChange('error', 'rust-analyzer disconnected'); + } + }); + const client = new LSPClient({ + rootUri: info.rootUri, + timeout: 15_000, + workspace: lspClient => new PlaygroundWorkspace(lspClient, uri => { + callbacks.current.onOpenFile(uri); + return views.current.get(uri) ?? null; + }), + extensions: languageServerExtensions(), + notificationHandlers: { + 'textDocument/publishDiagnostics': (_client, params) => { + const diagnostics = Array.isArray(params?.diagnostics) ? params.diagnostics : []; + const filename = String(params?.uri ?? '').split('/').at(-1) ?? 'file'; + callbacks.current.onDiagnostics( + `${diagnostics.length} diagnostic${diagnostics.length === 1 ? '' : 's'} in ${filename}`, + diagnostics.length > 0, + ); + return false; + }, + }, + }).connect(transport); + await client.initializing; + if (disposed) { + return; + } + info.examples.flatMap(example => example.files).forEach(file => { + const parent = containers.current.get(file.uri); + if (!parent) { + return; + } + const languageExtensions = file.languageId === 'rust' + ? [rust(), client.plugin(file.uri, 'rust'), commandClickDefinition] + : []; + const view = new EditorView({ + doc: file.source, + extensions: [ + basicSetup, + oneDark, + languageExtensions, + EditorView.lineWrapping, + EditorView.updateListener.of(update => { + if (update.docChanged) { + callbacks.current.onSave(file.target, update.state.doc.toString()); + } + }), + ], + parent, + }); + views.current.set(file.uri, view); + }); + callbacks.current.onConnectionChange('ready'); + }; + + setup().catch(error => { + const message = error instanceof Error ? error.message : 'Unable to start rust-analyzer'; + callbacks.current.onConnectionChange('error', message); + }); + return () => { + disposed = true; + views.current.forEach(view => view.destroy()); + views.current.clear(); + }; + }, [info]); + + return ( +
+ {info.examples.flatMap(example => example.files).map(file => ( + + ); +}); diff --git a/playground/src/dom.ts b/playground/src/dom.ts deleted file mode 100644 index f944b0b7c73..00000000000 --- a/playground/src/dom.ts +++ /dev/null @@ -1,24 +0,0 @@ -const requireElement = (id: string): T => { - const element = document.getElementById(id); - if (!element) { - throw new Error(`Missing element: ${id}`); - } - return element as T; -}; - -export const elements = { - activePath: requireElement('active-path'), - diagnosticStatus: requireElement('diagnostic-status'), - editorsParent: requireElement('editors'), - exampleNav: requireElement('example-nav'), - fileNav: requireElement('file-nav'), - fileTreeTitle: requireElement('file-tree-title'), - guidePath: requireElement('guide-path'), - lspStatus: requireElement('lsp-status'), - markdownEditorParent: requireElement('markdown-editor'), - output: requireElement('output'), - resetButton: requireElement('reset-button'), - revision: requireElement('revision'), - runButton: requireElement('run-button'), - runStatus: requireElement('run-status'), -}; diff --git a/playground/src/file-tree-panel.tsx b/playground/src/file-tree-panel.tsx new file mode 100644 index 00000000000..c9921776b43 --- /dev/null +++ b/playground/src/file-tree-panel.tsx @@ -0,0 +1,43 @@ +import { useEffect, useRef } from 'react'; + +import { ExampleFileTree } from './file-tree'; +import type { PlaygroundExample } from './types'; + +type FileTreePanelProps = { + activePath?: string; + example: PlaygroundExample; + onOpenFile: (uri: string) => boolean; +}; + +export const FileTreePanel = ({ activePath, example, onOpenFile }: FileTreePanelProps) => { + const container = useRef(null); + const tree = useRef(null); + const openFile = useRef(onOpenFile); + openFile.current = onOpenFile; + + useEffect(() => { + if (!container.current) { + return; + } + const instance = new ExampleFileTree(container.current, uri => { openFile.current(uri); }); + instance.render(example, activePath); + tree.current = instance; + return () => { + instance.cleanUp(); + tree.current = null; + }; + }, [example]); + + useEffect(() => { + if (activePath) { + tree.current?.select(activePath); + } + }, [activePath]); + + return ( + <> +

FILES · {example.id}://

+