Skip to content

Commit

Permalink
remove entryPoint for now, verify code can compile, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthoffner committed Dec 9, 2023
1 parent 576dbc3 commit 8d893d8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
7 changes: 2 additions & 5 deletions esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ describe('JavaScript and TypeScript Compiler Tests', () => {
it('should compile JavaScript code correctly', async () => {
const [javaScriptCompiler] = createJavaScriptCompiler();
const result = await javaScriptCompiler({
rawCode: `1 + 1`,
entryPoint: 'index.js'
rawCode: `1 + 1`
});
expect(result).toBe("Compilation failed: The \"wasmURL\" option only works in the browser");
});

it('should compile TypeScript code correctly', async () => {
const [javaScriptCompiler] = createJavaScriptCompiler();
const result = await javaScriptCompiler({
rawCode: `let num: number = 1; num + 1;`,
entryPoint: 'index.ts'
rawCode: `let num: number = 1; num + 1;`
});
expect(result).toBe("Compilation failed: The \"wasmURL\" option only works in the browser");
});
Expand All @@ -25,7 +23,6 @@ describe('JavaScript and TypeScript Compiler Tests', () => {
const [javaScriptCompiler] = createJavaScriptCompiler();
const result = await javaScriptCompiler({
rawCode: `const element = <div>Hello World</div>; element.type;`,
entryPoint: 'index.jsx'
});
expect(result).toBe("Compilation failed: The \"wasmURL\" option only works in the browser");
});
Expand Down
17 changes: 8 additions & 9 deletions esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,33 @@ export const unpkgPathPlugin = (): esbuild.Plugin => {
};

// Define the version of esbuild-wasm to use
const ESBUILD_WASM_VERSION = "0.14.42";
const ESBUILD_WASM_VERSION = "0.14.54";

function createJavaScriptCompiler() {
const paramsSchema = z.object({
rawCode: z.string(),
entryPoint: z.string(),
rawCode: z.string()
});
const name = "javaScriptCompiler";
const description = "Compiles JavaScript, TypeScript, or JSX code within a chat interface using esbuild-wasm. Accepts raw code and an entry point as input, and provides compiled output, facilitating real-time code execution and analysis in a chatbot environment.";
const description = "Compiles JavaScript, TypeScript, or JSX code within a chat interface using esbuild-wasm. Accepts raw code as input, and provides compiled output, facilitating real-time code execution and analysis in a chatbot environment.";

const execute = async (params: z.infer<typeof paramsSchema>) => {
const { rawCode, entryPoint } = params;
const { rawCode } = params;

try {
// Initialize esbuild-wasm if it hasn't been initialized
await esbuild.initialize({
worker: true,
wasmURL: `https://unpkg.com/esbuild-wasm@${ESBUILD_WASM_VERSION}/esbuild.wasm`,
wasmURL: `https://unpkg.com/esbuild-wasm@${ESBUILD_WASM_VERSION}/esbuild.wasm`
});

// Compile the code using esbuild-wasm
const result = await esbuild.build({
entryPoints: [entryPoint],
entryPoints: ["index.js"],
bundle: true,
write: false,
minify: true,
outdir: "/",
plugins: [unpkgPathPlugin(), unpkgFetchPlugin(rawCode, entryPoint)],
plugins: [unpkgPathPlugin(), unpkgFetchPlugin(rawCode, "index.js")],
metafile: true,
allowOverwrite: true
});
Expand All @@ -156,7 +155,7 @@ function createJavaScriptCompiler() {
if (error instanceof Error) {
return `Compilation failed: ${error.message}`;
}
return "Compilation failed with unknown error";
return `Compilation failed with unknown error: ${error}`;
}
};

Expand Down
8 changes: 2 additions & 6 deletions example/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ export const runtime = "edge";
const functions: ChatCompletionFunctions[] = [
{
name: 'javaScriptCompiler',
description: 'Compiles JavaScript, TypeScript, or JSX code within a chat interface using esbuild-wasm. Accepts raw code and an entry point as input, and provides compiled output, facilitating real-time code execution and analysis in a chatbot environment.',
description: 'Compiles JavaScript, TypeScript, or JSX code within a chat interface using esbuild-wasm. Accepts raw code as input, and provides compiled output, facilitating real-time code execution and analysis in a chatbot environment.',
parameters: {
type: 'object',
properties: {
rawCode: {
type: 'string',
description: 'The raw code to be compiled. This can include JavaScript, TypeScript, or JSX code.'
},
entryPoint: {
type: 'string',
description: 'The entry point filename for the compilation process. This is typically the main file of the code snippet.'
}
},
required: ['rawCode', 'entryPoint']
required: ['rawCode']
}
}
];
Expand Down
22 changes: 7 additions & 15 deletions example/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import { createJavaScriptCompiler } from "../../dist/esm/esbuild";
const [compiler] = createJavaScriptCompiler();

const examples = [
"Convert this JSON object to a TypeScript interface.",
"Create a React functional component with a useState hook.",
"Write a JavaScript function to sort an array of numbers in ascending order.",
"Show me how to fetch data from an API using async/await in JavaScript.",
"Demonstrate a simple example of JSX conditional rendering.",
"Compile and run a TypeScript file that creates an interactive to-do list with add, remove, and toggle completion functionalities."
];

export default function Chat() {
Expand All @@ -37,12 +33,13 @@ export default function Chat() {
parsedFunctionCallArguments = JSON.parse(args);
}

console.log("functionCallHandler");
console.log(functionCall, chatMessages);

let result;

result = JSON.stringify(compiler(parsedFunctionCallArguments.code));
const compiledResult = compiler({ rawCode: parsedFunctionCallArguments.rawCode });
console.log(compiledResult);
result = JSON.stringify(compiledResult);

console.log(result);

return {
messages: [
Expand Down Expand Up @@ -109,7 +106,7 @@ export default function Chat() {
),
},
function: {
avatar: <div>function</div>,
avatar: <div></div>,
bgColor: "bg-gray-200",
avatarColor: "bg-blue-500",
dialogComponent: (message: Message) => (
Expand All @@ -123,11 +120,6 @@ export default function Chat() {

return (
<main className="flex flex-col items-center justify-between pb-40">
<div className="absolute top-5 hidden w-full justify-between px-5 sm:flex">
<div className="rounded-lg p-2 text-2xl transition-colors duration-200 hover:bg-stone-100 sm:bottom-auto">
🤖
</div>
</div>
{messages.length > 0 ? (
messages.map((message, i) => {
return (
Expand Down

0 comments on commit 8d893d8

Please sign in to comment.