-
Notifications
You must be signed in to change notification settings - Fork 44
Include generated docs in local circuit prompt #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DevvoZA
wants to merge
2
commits into
tscircuit:main
Choose a base branch
from
DevvoZA:codex/generated-docs-prompt-45
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
tests/prompt-templates/create-local-circuit-prompt.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test" | ||
| import { | ||
| createLocalCircuitPrompt, | ||
| resetGeneratedTscircuitDocsCacheForTests, | ||
| setGeneratedTscircuitDocsTimeoutForTests, | ||
| } from "../../lib/prompt-templates/create-local-circuit-prompt" | ||
|
|
||
| const originalFetch = globalThis.fetch | ||
|
|
||
| const propsDoc = `# Component Types | ||
|
|
||
| <resistor resistance="1k" footprint="0603" /> | ||
| ` | ||
|
|
||
| function mockFetch(handler: (url: string) => Response | Promise<Response>) { | ||
| globalThis.fetch = (async (input) => { | ||
| const url = | ||
| typeof input === "string" | ||
| ? input | ||
| : input instanceof URL | ||
| ? input.toString() | ||
| : input.url | ||
| return handler(url) | ||
| }) as typeof fetch | ||
| } | ||
|
|
||
| const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
|
||
| beforeEach(() => { | ||
| resetGeneratedTscircuitDocsCacheForTests() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch | ||
| resetGeneratedTscircuitDocsCacheForTests() | ||
| }) | ||
|
|
||
| describe("createLocalCircuitPrompt", () => { | ||
| test("includes auto-generated docs before the handwritten API overview", async () => { | ||
| mockFetch((url) => { | ||
| if (url.includes("COMPONENT_TYPES.md")) { | ||
| return new Response(propsDoc) | ||
| } | ||
| if (url.includes("docs.tscircuit.com/ai.txt")) { | ||
| return new Response("Generated docs: prefer <chip /> pinLabels.") | ||
| } | ||
| return new Response("not found", { status: 404 }) | ||
| }) | ||
|
|
||
| const prompt = await createLocalCircuitPrompt() | ||
|
|
||
| expect(prompt).toContain("## Auto-generated tscircuit documentation") | ||
| expect(prompt).toContain("Generated docs: prefer <chip /> pinLabels.") | ||
| expect( | ||
| prompt.indexOf("## Auto-generated tscircuit documentation"), | ||
| ).toBeLessThan(prompt.indexOf("## tscircuit API overview")) | ||
| }) | ||
|
|
||
| test("keeps prompt creation working when generated docs are unavailable", async () => { | ||
| mockFetch((url) => { | ||
| if (url.includes("COMPONENT_TYPES.md")) { | ||
| return new Response(propsDoc) | ||
| } | ||
| if (url.includes("docs.tscircuit.com/ai.txt")) { | ||
| return new Response("temporarily unavailable", { status: 503 }) | ||
| } | ||
| return new Response("not found", { status: 404 }) | ||
| }) | ||
|
|
||
| const prompt = await createLocalCircuitPrompt() | ||
|
|
||
| expect(prompt).not.toContain("## Auto-generated tscircuit documentation") | ||
| expect(prompt).toContain("## tscircuit API overview") | ||
| expect(prompt).toContain("<resistor resistance") | ||
| }) | ||
|
|
||
| test("caches successful generated docs while still fetching current props docs", async () => { | ||
| const calls: string[] = [] | ||
|
|
||
| mockFetch((url) => { | ||
| calls.push(url) | ||
| if (url.includes("COMPONENT_TYPES.md")) { | ||
| return new Response(propsDoc) | ||
| } | ||
| if (url.includes("docs.tscircuit.com/ai.txt")) { | ||
| return new Response("Generated docs cached once.") | ||
| } | ||
| return new Response("not found", { status: 404 }) | ||
| }) | ||
|
|
||
| await createLocalCircuitPrompt() | ||
| await createLocalCircuitPrompt() | ||
|
|
||
| expect( | ||
| calls.filter((url) => url.includes("docs.tscircuit.com/ai.txt")).length, | ||
| ).toBe(1) | ||
| expect( | ||
| calls.filter((url) => url.includes("COMPONENT_TYPES.md")).length, | ||
| ).toBe(2) | ||
| }) | ||
|
|
||
| test("times out and caches slow optional generated docs", async () => { | ||
| const calls: string[] = [] | ||
| setGeneratedTscircuitDocsTimeoutForTests(1) | ||
|
|
||
| mockFetch((url) => { | ||
| calls.push(url) | ||
| if (url.includes("COMPONENT_TYPES.md")) { | ||
| return new Response(propsDoc) | ||
| } | ||
| if (url.includes("docs.tscircuit.com/ai.txt")) { | ||
| return wait(100).then( | ||
| () => new Response("Generated docs arrived too late."), | ||
| ) | ||
| } | ||
| return new Response("not found", { status: 404 }) | ||
| }) | ||
|
|
||
| const prompt = await createLocalCircuitPrompt() | ||
| const promptWithCachedTimeout = await createLocalCircuitPrompt() | ||
|
|
||
| expect(prompt).not.toContain("Generated docs arrived too late.") | ||
| expect(promptWithCachedTimeout).toContain("## tscircuit API overview") | ||
| expect( | ||
| calls.filter((url) => url.includes("docs.tscircuit.com/ai.txt")).length, | ||
| ).toBe(1) | ||
| expect( | ||
| calls.filter((url) => url.includes("COMPONENT_TYPES.md")).length, | ||
| ).toBe(2) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 testIfOpenAiApiKey = process.env.OPENAI_API_KEY ? test : test.skip | ||
|
|
||
| testIfOpenAiApiKey( | ||
| "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) | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.