-
Notifications
You must be signed in to change notification settings - Fork 1
feat: LLM context file builder #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * Vite plugin for generating llms.txt | ||
| * | ||
| * Generates an llms.txt file at build time that describes the Malloy models | ||
| * and schema to help LLMs understand the data explorer site. | ||
| * | ||
| * In dev mode, serves llms.txt dynamically via middleware. | ||
| */ | ||
|
|
||
| import type { Plugin, ResolvedConfig, ViteDevServer } from "vite"; | ||
| import * as fs from "node:fs/promises"; | ||
| import * as path from "node:path"; | ||
| import { | ||
| extractModelsSchema, | ||
| getDataFiles, | ||
| getNotebooks, | ||
| generateLlmsTxtContent, | ||
| } from "../src/llms-txt"; | ||
|
|
||
| export interface LlmsTxtPluginOptions { | ||
| siteTitle?: string; | ||
| modelsDir?: string; | ||
| } | ||
|
|
||
| export default function llmsTxtPlugin( | ||
| options: LlmsTxtPluginOptions = {}, | ||
| ): Plugin { | ||
| const { siteTitle = "Malloy Data Explorer", modelsDir = "models" } = options; | ||
|
|
||
| let config: ResolvedConfig; | ||
|
|
||
| async function generateContent(): Promise<string> { | ||
| const modelsDirPath = path.join(config.root, modelsDir); | ||
|
|
||
| const [models, dataFiles, notebooks] = await Promise.all([ | ||
| extractModelsSchema(modelsDirPath), | ||
| getDataFiles(modelsDirPath), | ||
| getNotebooks(modelsDirPath), | ||
| ]); | ||
|
|
||
| return generateLlmsTxtContent({ | ||
| siteTitle, | ||
| basePath: config.base, | ||
| models, | ||
| dataFiles, | ||
| notebooks, | ||
| }); | ||
| } | ||
|
|
||
| return { | ||
| name: "vite-plugin-llms-txt", | ||
|
|
||
| configResolved(resolvedConfig) { | ||
| config = resolvedConfig; | ||
| }, | ||
|
|
||
| // DEV MODE: Serve llms.txt dynamically | ||
| configureServer(server: ViteDevServer) { | ||
| server.middlewares.use((req, res, next) => { | ||
| if (req.url === "/llms.txt") { | ||
| void (async () => { | ||
| try { | ||
| // Regenerate on each request in dev mode for hot reloading | ||
| const content = await generateContent(); | ||
| res.setHeader("Content-Type", "text/plain; charset=utf-8"); | ||
| res.end(content); | ||
| } catch (error) { | ||
| console.error("[llms.txt] Error generating content:", error); | ||
| res.statusCode = 500; | ||
| res.end( | ||
| `Error generating llms.txt: ${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| } | ||
| })(); | ||
| return; | ||
| } | ||
| next(); | ||
| }); | ||
| }, | ||
|
|
||
| // BUILD MODE: Generate file after bundle | ||
| async closeBundle() { | ||
| if (process.env["VITEST"] || process.env["NODE_ENV"] === "test") { | ||
| return; | ||
| } | ||
| if (config.command !== "build") return; | ||
|
|
||
| try { | ||
| const content = await generateContent(); | ||
|
|
||
| const outputPath = path.join( | ||
| config.root, | ||
| config.build.outDir, | ||
| "llms.txt", | ||
| ); | ||
| await fs.writeFile(outputPath, content, "utf-8"); | ||
|
|
||
| console.log(`[llms.txt] Generated ${outputPath}`); | ||
| } catch (error) { | ||
| console.error("[llms.txt] Error generating file:", error); | ||
| throw error; | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The void IIFE pattern
void (async () => { ... })()is used to handle the async operation in the middleware. While this works, there's a subtle issue: if an error is thrown after the response headers are sent but beforeres.end()is called, the response might be left hanging. Consider adding error handling around the entire async block to ensure the response is always properly closed, or use a safer pattern like awaiting the promise and catching errors at the top level.