refactor: use one playground guide

This commit is contained in:
Yujong Lee 2026-09-10 15:00:36 -07:00
parent 903e4c216d
commit e9227d0f8c
6 changed files with 52 additions and 70 deletions

View file

@ -80,24 +80,16 @@ const readExamples = async () => {
const id = entry.name;
const directory = path.join(examplesDirectory, id);
const sourcePaths = ['Cargo.toml', ...await listRustFiles(directory)];
const [guideSource, files] = await Promise.all([
readFile(path.join(directory, 'GUIDE.md'), 'utf8'),
Promise.all(sourcePaths.map(async filePath => ({
languageId: filePath.endsWith('.rs') ? 'rust' : 'toml',
path: filePath,
source: await readFile(path.join(directory, filePath), 'utf8'),
target: path.posix.join('examples', id, filePath),
uri: pathToFileURL(path.join(directory, filePath)).href,
}))),
]);
const files = await Promise.all(sourcePaths.map(async filePath => ({
languageId: filePath.endsWith('.rs') ? 'rust' : 'toml',
path: filePath,
source: await readFile(path.join(directory, filePath), 'utf8'),
target: path.posix.join('examples', id, filePath),
uri: pathToFileURL(path.join(directory, filePath)).href,
})));
return {
directory,
files,
guide: {
path: 'GUIDE.md',
source: guideSource,
target: path.posix.join('examples', id, 'GUIDE.md'),
},
id,
title: titleFromId(id),
};
@ -112,10 +104,15 @@ const vite = await createViteServer({
const server = http.createServer(async (request, response) => {
if (request.method === 'GET' && request.url === '/api/info') {
const [examples, revision] = await Promise.all([readExamples(), gitRevision()]);
const [examples, guideSource, revision] = await Promise.all([
readExamples(),
readFile(path.join(workspaceDirectory, 'GUIDE.md'), 'utf8'),
gitRevision(),
]);
sendJson(response, 200, {
revision,
examples: examples.map(({ directory: _directory, ...example }) => example),
guide: { path: 'GUIDE.md', source: guideSource, target: 'GUIDE.md' },
rootUri: pathToFileURL(workspaceDirectory).href,
});
return;

View file

@ -33,13 +33,13 @@ type PlaygroundGuide = {
type PlaygroundExample = {
files: PlaygroundFile[];
guide: PlaygroundGuide;
id: string;
title: string;
};
type PlaygroundInfo = {
examples: PlaygroundExample[];
guide: PlaygroundGuide;
rootUri: string;
revision: string;
};
@ -263,8 +263,6 @@ const main = async () => {
const fileByUri = new Map(allFiles.map(entry => [entry.file.uri, entry]));
const views = new Map<string, EditorView>();
const containers = new Map<string, HTMLDivElement>();
const markdownViews = new Map<string, EditorView>();
const markdownContainers = new Map<string, HTMLDivElement>();
const exampleButtons = new Map<string, HTMLButtonElement>();
const fileButtons = new Map<string, HTMLButtonElement>();
let activeExampleId = initialExample.id;
@ -314,10 +312,6 @@ const main = async () => {
exampleButtons.forEach((button, candidate) => {
button.dataset.active = String(candidate === example.id);
});
markdownContainers.forEach((container, candidate) => {
container.hidden = candidate !== example.id;
});
guidePath.textContent = `${example.id}://${example.guide.path}`;
renderFileTree(example);
if (selectDefaultFile) {
const current = fileByUri.get(activeUri);
@ -426,33 +420,25 @@ const main = async () => {
views.set(file.uri, view);
});
info.examples.forEach(example => {
const container = document.createElement('div');
container.className = 'markdown-container';
container.hidden = example.id !== activeExampleId;
markdownEditorParent.append(container);
markdownContainers.set(example.id, container);
const view = new EditorView({
doc: storedFiles[example.guide.target] ?? example.guide.source,
extensions: [
basicSetup,
markdown(),
oneDark,
playgroundLinkDecorations,
markdownFileNavigation(openFileTarget),
EditorView.lineWrapping,
EditorView.updateListener.of(update => {
if (update.docChanged) {
saveStoredFile(example.guide.target, update.state.doc.toString());
}
}),
],
parent: container,
});
markdownViews.set(example.id, view);
const markdownView = new EditorView({
doc: storedFiles[info.guide.target] ?? info.guide.source,
extensions: [
basicSetup,
markdown(),
oneDark,
playgroundLinkDecorations,
markdownFileNavigation(openFileTarget),
EditorView.lineWrapping,
EditorView.updateListener.of(update => {
if (update.docChanged) {
saveStoredFile(info.guide.target, update.state.doc.toString());
}
}),
],
parent: markdownEditorParent,
});
guidePath.textContent = info.guide.path;
selectExample(activeExampleId);
lspStatus.textContent = 'rust-analyzer connected';
lspStatus.dataset.state = 'ready';
@ -490,9 +476,8 @@ const main = async () => {
const view = views.get(file.uri);
view?.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: file.source } });
});
const markdownView = markdownViews.get(example.id);
markdownView?.dispatch({
changes: { from: 0, to: markdownView.state.doc.length, insert: example.guide.source },
markdownView.dispatch({
changes: { from: 0, to: markdownView.state.doc.length, insert: info.guide.source },
});
});
};

View file

@ -204,7 +204,6 @@ h1 {
#editors,
#markdown-editor,
.markdown-container,
.editor-container,
.cm-editor {
height: 100%;

View file

@ -0,0 +1,19 @@
# LiteLLM Rust examples
Each example is a standalone binary Cargo package with its own `main` function. A link's URI scheme selects the example crate; the rest is a path relative to that crate.
## Proc macro
- [Open the entry point](proc-macro://src/main.rs#L1)
- [Open the derived data model](proc-macro://src/proof.rs#L3)
- [Open its Cargo manifest](proc-macro://Cargo.toml#L1)
`CompilerProof` derives Serde's `Serialize` and `Deserialize` procedural macros and references `litellm_core::Error`.
## In-repo dependency
- [Open the main function](error-type://src/main.rs#L3)
- [Open the report module](error-type://src/report.rs#L1)
- [Open its Cargo manifest](error-type://Cargo.toml#L1)
This example prints the fully qualified name of `litellm_core::Error`. No model or network request is involved.

View file

@ -1,9 +0,0 @@
# In-repo dependency app
This standalone binary package imports a type directly from LiteLLM's Rust crate.
- [Open the main function](error-type://src/main.rs#L3)
- [Open the report module](error-type://src/report.rs#L1)
- [Open its Cargo manifest](error-type://Cargo.toml#L1)
Run the example to print the fully qualified name of `litellm_core::Error`. No model or network request is involved.

View file

@ -1,9 +0,0 @@
# Proc macro app
This example is a standalone binary Cargo package with its own `main` function.
- [Open the entry point](proc-macro://src/main.rs#L1)
- [Open the derived data model](proc-macro://src/proof.rs#L3)
- [Open its Cargo manifest](proc-macro://Cargo.toml#L1)
`CompilerProof` derives Serde's `Serialize` and `Deserialize` procedural macros. The app also references `litellm_core::Error` from the in-repo LiteLLM Rust crate.