Skip to content

Commit

Permalink
pull snippet from registry function
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 8, 2024
1 parent fcc3e1b commit aa89474
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"noExplicitAny": "off"
},
"style": {
"noParameterAssign": "off",
"noNonNullAssertion": "off",
"useNumberNamespace": "off",
"useFilenamingConvention": {
Expand Down
6 changes: 6 additions & 0 deletions lib/code-runner/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./code-runner-context"
export * from "./get-imports-from-code"
export * from "./run-prompt"
export * from "./safe-evaluate-code"
export * from "./transpile-code"
export * from "./pull-snippet-import"
26 changes: 26 additions & 0 deletions lib/code-runner/pull-snippet-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Get the compiled js for a snippet
*/
export const pullSnippetCompiledJs = async (
importName: string,
opts: {
registryUrl?: string
} = {},
) => {
opts.registryUrl ??= "https://registry-api.tscircuit.com"
if (importName.startsWith("@tsci/")) {
// @tsci/foo.bar -> foo/bar
importName = importName.split("@tsci/")[1].replace(/\./, "/")
}
if (importName.startsWith("@")) {
importName = importName.slice(1)
}

const [owner_name, unscoped_name] = importName.split("/")

const { snippet } = await fetch(
`${opts.registryUrl}/snippets/get?owner_name=${owner_name}&unscoped_name=${unscoped_name}`,
).then((res) => res.json())

return snippet.compiled_js
}
27 changes: 10 additions & 17 deletions lib/code-runner/transpile-code.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
// if (!code) return ""
// if (isStreaming) return ""
// try {
// const result = Babel.transform(code, {
// presets: ["react", "typescript"],
// plugins: ["transform-modules-commonjs"],
// filename: "virtual.tsx",
// })
// return result.code || ""
// } catch (error: any) {
// console.error("Babel compilation error:", error)
// return `Error: ${error.message}`
// }


import * as Babel from "@babel/standalone"

export const safeTranspileCode = (code: string): { success: true, transpiledCode: string, error: undefined } | { success: false, error: string, transpiledCode: undefined } => {
export const safeTranspileCode = (
code: string,
):
| { success: true; transpiledCode: string; error: undefined }
| { success: false; error: string; transpiledCode: undefined } => {
if (!code) return { success: true, transpiledCode: "", error: undefined }
try {
const result = Babel.transform(code, {
presets: ["react", "typescript"],
plugins: ["transform-modules-commonjs"],
filename: "index.tsx",
})
return { success: true, transpiledCode: result.code || "", error: undefined }
return {
success: true,
transpiledCode: result.code || "",
error: undefined,
}
} catch (error: any) {
console.error("Babel compilation error:", error)
return { success: false, error: error.message, transpiledCode: undefined }
Expand Down
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { createCircuitBoard1Template } from "prompt-templates/create-circuit-board1"

// TODO eventually this will be it's own export
export * from "./code-runner"

0 comments on commit aa89474

Please sign in to comment.