Skip to content

Commit 334087b

Browse files
committed
Include generated docs in local circuit prompt
1 parent 0fd302e commit 334087b

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

lib/prompt-templates/create-local-circuit-prompt.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2+
fp,
23
getFootprintNamesByType,
34
getFootprintSizes,
4-
fp,
55
} from "@tscircuit/footprinter"
66

77
async function fetchFileContent(url: string): Promise<string> {
@@ -19,6 +19,24 @@ async function fetchFileContent(url: string): Promise<string> {
1919
}
2020
}
2121

22+
const generatedDocsUrl = "https://docs.tscircuit.com/ai.txt"
23+
let generatedDocsPromise: Promise<string> | undefined
24+
25+
export function clearGeneratedDocsCacheForTests() {
26+
generatedDocsPromise = undefined
27+
}
28+
29+
async function fetchGeneratedDocs(): Promise<string> {
30+
generatedDocsPromise ??= fetch(generatedDocsUrl)
31+
.then((response) => {
32+
if (!response.ok) return ""
33+
return response.text()
34+
})
35+
.catch(() => "")
36+
37+
return generatedDocsPromise
38+
}
39+
2240
export const createLocalCircuitPrompt = async () => {
2341
const footprintNamesByType = getFootprintNamesByType()
2442
const footprintSizes = getFootprintSizes()
@@ -33,10 +51,12 @@ export const createLocalCircuitPrompt = async () => {
3351
"",
3452
)
3553

36-
const propsDoc =
37-
(await fetchFileContent(
54+
const [propsDoc, generatedDocs] = await Promise.all([
55+
fetchFileContent(
3856
"https://raw.githubusercontent.com/tscircuit/props/main/generated/COMPONENT_TYPES.md",
39-
)) || ""
57+
),
58+
fetchGeneratedDocs(),
59+
])
4060

4161
const cleanedPropsDoc = propsDoc
4262
.split("\n")
@@ -51,6 +71,8 @@ YOU MUST ABIDE BY THE RULES IN THE RULES SECTION
5171
5272
## tscircuit API overview
5373
74+
${generatedDocs ? `Generated tscircuit documentation:\n\n${generatedDocs}\n\n` : ""}
75+
5476
Here's an overview of the tscircuit API:
5577
5678
<board width="10mm" height="10mm" /> // usually the root component
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { afterEach, describe, expect, it, mock } from "bun:test"
2+
import {
3+
clearGeneratedDocsCacheForTests,
4+
createLocalCircuitPrompt,
5+
} from "../../lib/prompt-templates/create-local-circuit-prompt"
6+
7+
const originalFetch = globalThis.fetch
8+
9+
afterEach(() => {
10+
globalThis.fetch = originalFetch
11+
clearGeneratedDocsCacheForTests()
12+
})
13+
14+
function mockDocsFetch(generatedDocsResponse: Response | Error) {
15+
const fetchMock = mock(async (url: string | URL | Request) => {
16+
const requestUrl = String(url)
17+
18+
if (requestUrl === "https://docs.tscircuit.com/ai.txt") {
19+
if (generatedDocsResponse instanceof Error) {
20+
throw generatedDocsResponse
21+
}
22+
23+
return generatedDocsResponse
24+
}
25+
26+
if (
27+
requestUrl ===
28+
"https://raw.githubusercontent.com/tscircuit/props/main/generated/COMPONENT_TYPES.md"
29+
) {
30+
return new Response("# Components\n\n<resistor />")
31+
}
32+
33+
throw new Error(`Unexpected fetch URL: ${requestUrl}`)
34+
})
35+
36+
globalThis.fetch = fetchMock as unknown as typeof fetch
37+
38+
return fetchMock
39+
}
40+
41+
describe("createLocalCircuitPrompt", () => {
42+
it("includes the generated tscircuit docs when they are available", async () => {
43+
mockDocsFetch(new Response("Generated docs content"))
44+
45+
const prompt = await createLocalCircuitPrompt()
46+
47+
expect(prompt).toContain("Generated tscircuit documentation:")
48+
expect(prompt).toContain("Generated docs content")
49+
})
50+
51+
it("keeps creating the prompt when generated docs cannot be fetched", async () => {
52+
mockDocsFetch(new Error("docs unavailable"))
53+
54+
const prompt = await createLocalCircuitPrompt()
55+
56+
expect(prompt).toContain("Here's an overview of the tscircuit API:")
57+
expect(prompt).not.toContain("Generated tscircuit documentation:")
58+
})
59+
})

0 commit comments

Comments
 (0)