From c6b4c0f030a9e9f1f759f247c7e71615f4e00146 Mon Sep 17 00:00:00 2001 From: shootingallday Date: Thu, 21 May 2026 00:04:37 -0400 Subject: [PATCH] Include generated docs in local prompt --- .../create-local-circuit-prompt.ts | 24 +++++- tests/create-local-circuit-prompt.test.ts | 45 +++++++++++ tests/tscircuitCoder.test.ts | 77 ++++++++++--------- tests/utils/generate-random-prompts.test.ts | 6 +- 4 files changed, 110 insertions(+), 42 deletions(-) create mode 100644 tests/create-local-circuit-prompt.test.ts diff --git a/lib/prompt-templates/create-local-circuit-prompt.ts b/lib/prompt-templates/create-local-circuit-prompt.ts index a93f11f..e963000 100644 --- a/lib/prompt-templates/create-local-circuit-prompt.ts +++ b/lib/prompt-templates/create-local-circuit-prompt.ts @@ -1,7 +1,7 @@ import { + fp, getFootprintNamesByType, getFootprintSizes, - fp, } from "@tscircuit/footprinter" async function fetchFileContent(url: string): Promise { @@ -19,6 +19,18 @@ async function fetchFileContent(url: string): Promise { } } +async function fetchOptionalFileContent(url: string): Promise { + try { + const response = await fetch(url) + if (!response.ok) { + return "" + } + return await response.text() + } catch { + return "" + } +} + export const createLocalCircuitPrompt = async () => { const footprintNamesByType = getFootprintNamesByType() const footprintSizes = getFootprintSizes() @@ -33,10 +45,12 @@ export const createLocalCircuitPrompt = async () => { "", ) - const propsDoc = - (await fetchFileContent( + const [propsDoc, generatedDocs] = await Promise.all([ + fetchFileContent( "https://raw.githubusercontent.com/tscircuit/props/main/generated/COMPONENT_TYPES.md", - )) || "" + ), + fetchOptionalFileContent("https://docs.tscircuit.com/ai.txt"), + ]) const cleanedPropsDoc = propsDoc .split("\n") @@ -53,6 +67,8 @@ YOU MUST ABIDE BY THE RULES IN THE RULES SECTION Here's an overview of the tscircuit API: +${generatedDocs ? `## Generated tscircuit docs\n\n${generatedDocs}\n` : ""} + // usually the root component // custom shape instead of rectangle diff --git a/tests/create-local-circuit-prompt.test.ts b/tests/create-local-circuit-prompt.test.ts new file mode 100644 index 0000000..a29baff --- /dev/null +++ b/tests/create-local-circuit-prompt.test.ts @@ -0,0 +1,45 @@ +import { afterEach, expect, test } from "bun:test" +import { createLocalCircuitPrompt } from "lib/prompt-templates/create-local-circuit-prompt" + +const originalFetch = globalThis.fetch + +afterEach(() => { + globalThis.fetch = originalFetch +}) + +test("createLocalCircuitPrompt includes generated docs when available", async () => { + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = String(input) + if (url === "https://docs.tscircuit.com/ai.txt") { + return new Response("GENERATED_TSCIRCUIT_DOCS") + } + if (url.includes("COMPONENT_TYPES.md")) { + return new Response("# Props\n docs") + } + throw new Error(`Unexpected fetch: ${url}`) + }) as typeof fetch + + const prompt = await createLocalCircuitPrompt() + + expect(prompt).toContain("## Generated tscircuit docs") + expect(prompt).toContain("GENERATED_TSCIRCUIT_DOCS") + expect(prompt).toContain(" docs") +}) + +test("createLocalCircuitPrompt still works when generated docs are unavailable", async () => { + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = String(input) + if (url === "https://docs.tscircuit.com/ai.txt") { + return new Response("missing", { status: 404, statusText: "Not Found" }) + } + if (url.includes("COMPONENT_TYPES.md")) { + return new Response("# Props\n docs") + } + throw new Error(`Unexpected fetch: ${url}`) + }) as typeof fetch + + const prompt = await createLocalCircuitPrompt() + + expect(prompt).not.toContain("## Generated tscircuit docs") + expect(prompt).toContain(" docs") +}) diff --git a/tests/tscircuitCoder.test.ts b/tests/tscircuitCoder.test.ts index d66c022..598e0cc 100644 --- a/tests/tscircuitCoder.test.ts +++ b/tests/tscircuitCoder.test.ts @@ -1,39 +1,44 @@ -import { createTscircuitCoder } from "lib/tscircuit-coder/tscircuitCoder" import { expect, test } from "bun:test" +import { createTscircuitCoder } from "lib/tscircuit-coder/tscircuitCoder" import { getPrimarySourceCodeFromVfs } from "lib/utils/get-primary-source-code-from-vfs" -test("TscircuitCoder submitPrompt streams and updates vfs", async () => { - const streamedChunks: string[] = [] - let vfsUpdated = false - const tscircuitCoder = createTscircuitCoder() - tscircuitCoder.on("streamedChunk", (chunk: string) => { - streamedChunks.push(chunk) - }) - tscircuitCoder.on("vfsChanged", () => { - vfsUpdated = true - }) - - await tscircuitCoder.submitPrompt({ - prompt: "create bridge rectifier circuit", - }) - - await tscircuitCoder.submitPrompt({ - prompt: "add a transistor component", - }) - - let codeWithTransistor = getPrimarySourceCodeFromVfs(tscircuitCoder.vfs) - expect(codeWithTransistor).toInclude("transistor") - - await tscircuitCoder.submitPrompt({ - prompt: "add a tssop20 chip", - }) - - let codeWithChip = getPrimarySourceCodeFromVfs(tscircuitCoder.vfs) - expect(codeWithChip).toInclude("tssop20") - expect(codeWithChip).toInclude("transistor") - - expect(streamedChunks.length).toBeGreaterThan(0) - const vfsKeys = Object.keys(tscircuitCoder.vfs) - expect(vfsKeys.length).toBeGreaterThan(0) - expect(vfsUpdated).toBe(true) -}) +const testWithOpenAi = process.env.OPENAI_API_KEY ? test : test.skip + +testWithOpenAi( + "TscircuitCoder submitPrompt streams and updates vfs", + async () => { + const streamedChunks: string[] = [] + let vfsUpdated = false + const tscircuitCoder = createTscircuitCoder() + tscircuitCoder.on("streamedChunk", (chunk: string) => { + streamedChunks.push(chunk) + }) + tscircuitCoder.on("vfsChanged", () => { + vfsUpdated = true + }) + + await tscircuitCoder.submitPrompt({ + prompt: "create bridge rectifier circuit", + }) + + await tscircuitCoder.submitPrompt({ + prompt: "add a transistor component", + }) + + const codeWithTransistor = getPrimarySourceCodeFromVfs(tscircuitCoder.vfs) + expect(codeWithTransistor).toInclude("transistor") + + await tscircuitCoder.submitPrompt({ + prompt: "add a tssop20 chip", + }) + + const codeWithChip = getPrimarySourceCodeFromVfs(tscircuitCoder.vfs) + expect(codeWithChip).toInclude("tssop20") + expect(codeWithChip).toInclude("transistor") + + expect(streamedChunks.length).toBeGreaterThan(0) + const vfsKeys = Object.keys(tscircuitCoder.vfs) + expect(vfsKeys.length).toBeGreaterThan(0) + expect(vfsUpdated).toBe(true) + }, +) diff --git a/tests/utils/generate-random-prompts.test.ts b/tests/utils/generate-random-prompts.test.ts index 41a061c..16fea23 100644 --- a/tests/utils/generate-random-prompts.test.ts +++ b/tests/utils/generate-random-prompts.test.ts @@ -1,8 +1,10 @@ -import { describe, it, expect } from "bun:test" +import { describe, expect, it } from "bun:test" import { generateRandomPrompts } from "../../lib/utils/generate-random-prompts" describe("generateRandomPrompts", () => { - it("should return an array of prompts", async () => { + const itWithOpenAi = process.env.OPENAI_API_KEY ? it : it.skip + + itWithOpenAi("should return an array of prompts", async () => { const prompts = await generateRandomPrompts(3) expect(Array.isArray(prompts)).toBe(true)