Skip to content

Commit a0d6b92

Browse files
committed
Core: Code refactoring
1 parent f3586f9 commit a0d6b92

18 files changed

Lines changed: 643 additions & 144 deletions

File tree

.github/workflows/npm-sdk.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ jobs:
1818
- name: Install dependencies
1919
run: bun install
2020

21-
- name: Build SDK
22-
run: bun run sdk
23-
2421
- name: Publish to npm
25-
run: bun publish --cwd ./packages/exec0-sdk
22+
run: bun publish --cwd ./packages/exec0-node
2623
env:
2724
NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }}

apps/api/src/handlers/index.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

apps/api/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Scalar } from "@scalar/hono-api-reference";
22
import { Hono } from "hono";
33
import { openAPIRouteHandler } from "hono-openapi";
4-
import executeRoutes from "@/routes";
4+
import {routes} from "@/routes";
55

66
export { Sandbox } from "@cloudflare/sandbox";
77

@@ -11,7 +11,7 @@ app.get("/", (c) => {
1111
return c.text("Welcome to Exec0!!");
1212
});
1313

14-
app.route("/api/v1/execute", executeRoutes);
14+
app.route("/api/v1", routes);
1515

1616
app.get(
1717
"/v1/openapi.json",

apps/api/src/lib/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

apps/api/src/routes/index.ts

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,8 @@
11
import { Hono } from "hono";
2-
import { describeRoute, resolver, validator as zValidator } from "hono-openapi";
3-
import { createExecuteHandler } from "@/handlers";
4-
import { executeRequestSchema, executeResponseSchema } from "@/schemas";
2+
import { runRouter } from "./run";
53

64
const app = new Hono();
75

8-
app.post(
9-
"/python",
10-
describeRoute({
11-
operationId: "executePython",
12-
description: "Execute Python code",
13-
responses: {
14-
200: {
15-
description: "Successful execution",
16-
content: {
17-
"application/json": {
18-
schema: resolver(executeResponseSchema),
19-
},
20-
},
21-
},
22-
},
23-
}),
24-
zValidator("json", executeRequestSchema),
25-
createExecuteHandler("python"),
26-
);
6+
app.route("/run", runRouter)
277

28-
app.post(
29-
"/javascript",
30-
describeRoute({
31-
operationId: "executeJavaScript",
32-
description: "Execute JavaScript code",
33-
responses: {
34-
200: {
35-
description: "Successful execution",
36-
content: {
37-
"application/json": {
38-
schema: resolver(executeResponseSchema),
39-
},
40-
},
41-
},
42-
},
43-
}),
44-
zValidator("json", executeRequestSchema),
45-
createExecuteHandler("javascript"),
46-
);
47-
48-
app.post(
49-
"/typescript",
50-
describeRoute({
51-
operationId: "executeTypeScript",
52-
description: "Execute TypeScript code",
53-
responses: {
54-
200: {
55-
description: "Successful execution",
56-
content: {
57-
"application/json": {
58-
schema: resolver(executeResponseSchema),
59-
},
60-
},
61-
},
62-
},
63-
}),
64-
zValidator("json", executeRequestSchema),
65-
createExecuteHandler("typescript"),
66-
);
67-
68-
export default app;
8+
export { app as routes}

apps/api/src/routes/run/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Hono } from "hono";
2+
import { runPythonRouter } from "./python";
3+
import { runJavascriptRouter } from "./javascript";
4+
import { runTypescriptRouter } from "./typescript";
5+
6+
const app = new Hono();
7+
8+
app.route("/python", runPythonRouter)
9+
app.route("/javascript", runJavascriptRouter)
10+
app.route("/typescript", runTypescriptRouter)
11+
12+
export { app as runRouter}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Hono } from "hono";
2+
import { describeRoute, resolver, validator as zValidator } from "hono-openapi";
3+
import {
4+
errorResponseSchema,
5+
runRequestSchema,
6+
runResponseSchema,
7+
} from "@/schemas";
8+
import { runCode } from "@/services/runcode";
9+
10+
const app = new Hono();
11+
12+
app.post(
13+
"/",
14+
describeRoute({
15+
operationId: "runJavascript",
16+
description: "Run Javascript code",
17+
responses: {
18+
200: {
19+
description: "Successful execution",
20+
content: {
21+
"application/json": {
22+
schema: resolver(runResponseSchema),
23+
},
24+
},
25+
},
26+
400: {
27+
description: "Execution error",
28+
content: {
29+
"application/json": {
30+
schema: resolver(errorResponseSchema),
31+
},
32+
},
33+
},
34+
},
35+
}),
36+
zValidator("json", runRequestSchema),
37+
async (c) => {
38+
const env = c.env as { Sandbox: string };
39+
const body = c.req.valid("json");
40+
41+
const result = await runCode("javascript", body.code, env.Sandbox);
42+
return c.json(result, result.success ? 200 : 400);
43+
},
44+
);
45+
46+
export { app as runJavascriptRouter };

apps/api/src/routes/run/python.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Hono } from "hono";
2+
import { describeRoute, resolver, validator as zValidator } from "hono-openapi";
3+
import {
4+
errorResponseSchema,
5+
runRequestSchema,
6+
runResponseSchema,
7+
} from "@/schemas";
8+
import { runCode } from "@/services/runcode";
9+
10+
const app = new Hono();
11+
12+
app.post(
13+
"/",
14+
describeRoute({
15+
operationId: "runPython",
16+
description: "Run Python code",
17+
responses: {
18+
200: {
19+
description: "Successful execution",
20+
content: {
21+
"application/json": {
22+
schema: resolver(runResponseSchema),
23+
example: {
24+
success: true,
25+
output: "Hello, World!\n",
26+
},
27+
},
28+
},
29+
},
30+
400: {
31+
description: "Execution error",
32+
content: {
33+
"application/json": {
34+
schema: resolver(errorResponseSchema),
35+
example: {
36+
success: false,
37+
error: "Syntax error",
38+
},
39+
},
40+
},
41+
},
42+
},
43+
}),
44+
zValidator("json", runRequestSchema),
45+
async (c) => {
46+
const env = c.env as { Sandbox: string };
47+
const body = c.req.valid("json");
48+
49+
const result = await runCode("python", body.code, env.Sandbox);
50+
return c.json(result, result.success ? 200 : 400);
51+
},
52+
);
53+
54+
export { app as runPythonRouter };
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Hono } from "hono";
2+
import { describeRoute, resolver, validator as zValidator } from "hono-openapi";
3+
import {
4+
errorResponseSchema,
5+
runRequestSchema,
6+
runResponseSchema,
7+
} from "@/schemas";
8+
import { runCode } from "@/services/runcode";
9+
10+
const app = new Hono();
11+
12+
app.post(
13+
"/",
14+
describeRoute({
15+
operationId: "runTypescript",
16+
description: "Run Typescript code",
17+
responses: {
18+
200: {
19+
description: "Successful execution",
20+
content: {
21+
"application/json": {
22+
schema: resolver(runResponseSchema),
23+
example: {
24+
code: 'console.log("Hello, World!")',
25+
},
26+
},
27+
},
28+
},
29+
400: {
30+
description: "Execution error",
31+
content: {
32+
"application/json": {
33+
schema: resolver(errorResponseSchema),
34+
},
35+
},
36+
},
37+
},
38+
}),
39+
zValidator("json", runRequestSchema),
40+
async (c) => {
41+
const env = c.env as { Sandbox: string };
42+
const body = c.req.valid("json");
43+
44+
const result = await runCode("typescript", body.code, env.Sandbox);
45+
return c.json(result, result.success ? 200 : 400);
46+
},
47+
);
48+
49+
export { app as runTypescriptRouter };

apps/api/src/schemas/index.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
import { z } from "zod";
1+
import z from "zod";
22

3-
export const executeRequestSchema = z.object({
4-
code: z.string().min(1, "Code cannot be empty"),
5-
timeout: z.number().int().positive().optional().default(5000),
3+
export const runRequestSchema = z.object({
4+
code: z
5+
.string()
6+
.min(1, "Code cannot be empty")
7+
.describe("The code snippet to be executed"),
68
});
79

8-
export const executeResponseSchema = z.object({
9-
output: z.string(),
10-
error: z.string().optional(),
11-
executionTime: z.number(),
10+
export const outputSchema = z.object({
11+
text: z.string().optional(),
12+
html: z.string().optional(),
13+
png: z.string().optional(),
14+
jpeg: z.string().optional(),
15+
svg: z.string().optional(),
16+
latex: z.string().optional(),
17+
markdown: z.string().optional(),
18+
json: z.any().optional(),
19+
chart: z.any().optional(),
20+
data: z.any().optional(),
21+
});
22+
23+
export const runResponseSchema = z.object({
24+
success: z.boolean(),
25+
output: outputSchema.optional(),
26+
error: z.string().nullable().optional(),
27+
});
28+
29+
export const errorResponseSchema = z.object({
30+
success: z.boolean(),
31+
error: z.string(),
1232
});

0 commit comments

Comments
 (0)