From 09349683b9251ccaa10e1bfea2b8d63261b8e7e8 Mon Sep 17 00:00:00 2001 From: haocyan0723-code Date: Sun, 24 May 2026 18:17:43 +0800 Subject: [PATCH 1/2] Include generated docs in local circuit prompt --- .../create-local-circuit-prompt.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/prompt-templates/create-local-circuit-prompt.ts b/lib/prompt-templates/create-local-circuit-prompt.ts index a93f11f..c3b5645 100644 --- a/lib/prompt-templates/create-local-circuit-prompt.ts +++ b/lib/prompt-templates/create-local-circuit-prompt.ts @@ -19,6 +19,24 @@ async function fetchFileContent(url: string): Promise { } } +let generatedDocsCache: string | null = null + +export const __resetGeneratedDocsCacheForTests = () => { + generatedDocsCache = null +} + +async function getGeneratedDocsContent(): Promise { + if (generatedDocsCache !== null) return generatedDocsCache + + try { + generatedDocsCache = await fetchFileContent("https://docs.tscircuit.com/ai.txt") + } catch { + generatedDocsCache = "" + } + + return generatedDocsCache +} + export const createLocalCircuitPrompt = async () => { const footprintNamesByType = getFootprintNamesByType() const footprintSizes = getFootprintSizes() @@ -43,6 +61,7 @@ export const createLocalCircuitPrompt = async () => { .filter((line) => !line.startsWith("#")) .join("\n") .replace(/\n\n+/g, "\n\n") + const generatedDocsContent = await getGeneratedDocsContent() return ` You are an expert in electronic circuit design and tscircuit, and your job is to create a circuit board in tscircuit with the user-provided description. @@ -53,6 +72,8 @@ YOU MUST ABIDE BY THE RULES IN THE RULES SECTION Here's an overview of the tscircuit API: +${generatedDocsContent ? `## Auto-generated tscircuit docs\n\n${generatedDocsContent}` : ""} + // usually the root component // custom shape instead of rectangle From 276c2bf423e20f8a52ee1a13146ec70374c10ea7 Mon Sep 17 00:00:00 2001 From: haocyan0723-code Date: Sun, 24 May 2026 18:18:07 +0800 Subject: [PATCH 2/2] Add generated docs prompt tests --- tests/create-local-circuit-prompt.test.ts | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/create-local-circuit-prompt.test.ts diff --git a/tests/create-local-circuit-prompt.test.ts b/tests/create-local-circuit-prompt.test.ts new file mode 100644 index 0000000..87dad06 --- /dev/null +++ b/tests/create-local-circuit-prompt.test.ts @@ -0,0 +1,42 @@ +import { afterEach, expect, test } from "bun:test" +import { + __resetGeneratedDocsCacheForTests, + createLocalCircuitPrompt, +} from "../lib/prompt-templates/create-local-circuit-prompt" + +const originalFetch = globalThis.fetch + +afterEach(() => { + globalThis.fetch = originalFetch + __resetGeneratedDocsCacheForTests() +}) + +test("includes generated docs when ai.txt is available", async () => { + globalThis.fetch = async (url) => + new Response( + url.toString().includes("ai.txt") + ? "Generated docs content" + : "# Component Types\n\ncomponent props content", + ) + + const prompt = await createLocalCircuitPrompt() + + expect(prompt).toContain("## Auto-generated tscircuit docs") + expect(prompt).toContain("Generated docs content") + expect(prompt).toContain("component props content") +}) + +test("keeps prompt creation working when generated docs fail", async () => { + globalThis.fetch = async (url) => { + if (url.toString().includes("ai.txt")) { + throw new Error("docs unavailable") + } + + return new Response("# Component Types\n\ncomponent props content") + } + + const prompt = await createLocalCircuitPrompt() + + expect(prompt).not.toContain("## Auto-generated tscircuit docs") + expect(prompt).toContain("component props content") +})