diff --git a/lib/prompt-templates/create-local-circuit-prompt.ts b/lib/prompt-templates/create-local-circuit-prompt.ts index a93f11f..fabd1e6 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,24 @@ async function fetchFileContent(url: string): Promise { } } +const generatedDocsUrl = "https://docs.tscircuit.com/ai.txt" +let generatedDocsPromise: Promise | undefined + +export function clearGeneratedDocsCacheForTests() { + generatedDocsPromise = undefined +} + +async function fetchGeneratedDocs(): Promise { + generatedDocsPromise ??= fetch(generatedDocsUrl) + .then((response) => { + if (!response.ok) return "" + return response.text() + }) + .catch(() => "") + + return generatedDocsPromise +} + export const createLocalCircuitPrompt = async () => { const footprintNamesByType = getFootprintNamesByType() const footprintSizes = getFootprintSizes() @@ -33,10 +51,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", - )) || "" + ), + fetchGeneratedDocs(), + ]) const cleanedPropsDoc = propsDoc .split("\n") @@ -51,6 +71,8 @@ YOU MUST ABIDE BY THE RULES IN THE RULES SECTION ## tscircuit API overview +${generatedDocs ? `Generated tscircuit documentation:\n\n${generatedDocs}\n\n` : ""} + Here's an overview of the tscircuit API: // usually the root component diff --git a/tests/prompt-templates/create-local-circuit-prompt.test.ts b/tests/prompt-templates/create-local-circuit-prompt.test.ts new file mode 100644 index 0000000..6c3ba92 --- /dev/null +++ b/tests/prompt-templates/create-local-circuit-prompt.test.ts @@ -0,0 +1,63 @@ +import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test" +import { + clearGeneratedDocsCacheForTests, + createLocalCircuitPrompt, +} from "../../lib/prompt-templates/create-local-circuit-prompt" + +const originalFetch = globalThis.fetch + +beforeEach(() => { + clearGeneratedDocsCacheForTests() +}) + +afterEach(() => { + globalThis.fetch = originalFetch + clearGeneratedDocsCacheForTests() +}) + +function mockDocsFetch(generatedDocsResponse: Response | Error) { + const fetchMock = mock(async (url: string | URL | Request) => { + const requestUrl = String(url) + + if (requestUrl === "https://docs.tscircuit.com/ai.txt") { + if (generatedDocsResponse instanceof Error) { + throw generatedDocsResponse + } + + return generatedDocsResponse + } + + if ( + requestUrl === + "https://raw.githubusercontent.com/tscircuit/props/main/generated/COMPONENT_TYPES.md" + ) { + return new Response("# Components\n\n") + } + + throw new Error(`Unexpected fetch URL: ${requestUrl}`) + }) + + globalThis.fetch = fetchMock as unknown as typeof fetch + + return fetchMock +} + +describe("createLocalCircuitPrompt", () => { + it("includes the generated tscircuit docs when they are available", async () => { + mockDocsFetch(new Response("Generated docs content")) + + const prompt = await createLocalCircuitPrompt() + + expect(prompt).toContain("Generated tscircuit documentation:") + expect(prompt).toContain("Generated docs content") + }) + + it("keeps creating the prompt when generated docs cannot be fetched", async () => { + mockDocsFetch(new Error("docs unavailable")) + + const prompt = await createLocalCircuitPrompt() + + expect(prompt).toContain("Here's an overview of the tscircuit API:") + expect(prompt).not.toContain("Generated tscircuit documentation:") + }) +}) 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..a027594 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" +const itWithOpenAi = process.env.OPENAI_API_KEY ? it : it.skip + describe("generateRandomPrompts", () => { - it("should return an array of prompts", async () => { + itWithOpenAi("should return an array of prompts", async () => { const prompts = await generateRandomPrompts(3) expect(Array.isArray(prompts)).toBe(true)