Skip to content

Commit a410bcf

Browse files
PhilPhil
authored andcommitted
Include generated docs in local circuit prompt
1 parent 0fd302e commit a410bcf

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

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

Lines changed: 26 additions & 1 deletion
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,16 @@ async function fetchFileContent(url: string): Promise<string> {
1919
}
2020
}
2121

22+
async function fetchOptionalFileContent(url: string): Promise<string> {
23+
try {
24+
const response = await fetch(url)
25+
if (!response.ok) return ""
26+
return await response.text()
27+
} catch {
28+
return ""
29+
}
30+
}
31+
2232
export const createLocalCircuitPrompt = async () => {
2333
const footprintNamesByType = getFootprintNamesByType()
2434
const footprintSizes = getFootprintSizes()
@@ -38,6 +48,10 @@ export const createLocalCircuitPrompt = async () => {
3848
"https://raw.githubusercontent.com/tscircuit/props/main/generated/COMPONENT_TYPES.md",
3949
)) || ""
4050

51+
const generatedDocs = (
52+
await fetchOptionalFileContent("https://docs.tscircuit.com/ai.txt")
53+
).trim()
54+
4155
const cleanedPropsDoc = propsDoc
4256
.split("\n")
4357
.filter((line) => !line.startsWith("#"))
@@ -53,6 +67,17 @@ YOU MUST ABIDE BY THE RULES IN THE RULES SECTION
5367
5468
Here's an overview of the tscircuit API:
5569
70+
${
71+
generatedDocs
72+
? `## Generated tscircuit docs
73+
74+
The following documentation is generated from the current tscircuit docs site:
75+
76+
${generatedDocs}
77+
`
78+
: ""
79+
}
80+
5681
<board width="10mm" height="10mm" /> // usually the root component
5782
<board outline={[{x: 0, y: 0}, {x: 10, y: 0}, {x: 10, y: 10}, {x: 0, y: 10}]} /> // custom shape instead of rectangle
5883
<led pcbX="5mm" pcbY="5mm" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { afterEach, expect, test } from "bun:test"
2+
import { createLocalCircuitPrompt } from "lib/prompt-templates/create-local-circuit-prompt"
3+
4+
const originalFetch = globalThis.fetch
5+
6+
afterEach(() => {
7+
globalThis.fetch = originalFetch
8+
})
9+
10+
test("createLocalCircuitPrompt includes generated ai docs when available", async () => {
11+
globalThis.fetch = (async (input: RequestInfo | URL) => {
12+
const url = String(input)
13+
14+
if (url === "https://docs.tscircuit.com/ai.txt") {
15+
return new Response("Use <chip /> pinLabels for IC pins.")
16+
}
17+
18+
return new Response("# Props\n\n<resistor /> props")
19+
}) as typeof fetch
20+
21+
const prompt = await createLocalCircuitPrompt()
22+
23+
expect(prompt).toContain("## Generated tscircuit docs")
24+
expect(prompt).toContain("Use <chip /> pinLabels for IC pins.")
25+
})
26+
27+
test("createLocalCircuitPrompt still builds when generated ai docs cannot be fetched", async () => {
28+
globalThis.fetch = (async (input: RequestInfo | URL) => {
29+
const url = String(input)
30+
31+
if (url === "https://docs.tscircuit.com/ai.txt") {
32+
return new Response("not found", { status: 404 })
33+
}
34+
35+
return new Response("# Props\n\n<resistor /> props")
36+
}) as typeof fetch
37+
38+
const prompt = await createLocalCircuitPrompt()
39+
40+
expect(prompt).toContain("Here's an overview of the tscircuit API")
41+
expect(prompt).not.toContain("## Generated tscircuit docs")
42+
})

0 commit comments

Comments
 (0)