-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
56 lines (49 loc) · 1.75 KB
/
Copy pathapp.ts
File metadata and controls
56 lines (49 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { OpenAPIHono } from "@hono/zod-openapi";
import { cors } from "hono/cors";
import { jsonError } from "@/lib/http-error";
import { operationsRoute } from "@/routes/operations";
import { projectRoute } from "@/routes/project";
import { projectSessionsRoute } from "@/routes/project-sessions";
import { projectRuntimeManager } from "@/services/project-manager";
export const app = new OpenAPIHono();
// CORS
app.use(
"/*",
cors({
origin: process.env.CORS_ORIGIN?.split(",") ?? ["http://localhost:3000"],
allowMethods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
allowHeaders: ["Content-Type", "X-Agents-Playground-Token"],
maxAge: 86400,
}),
);
app.use("/api/*", async (context, next) => {
const expected = process.env.AGENTS_PLAYGROUND_TOKEN?.trim();
const protectsVersionRead =
context.req.path === "/api/project/versioning" || context.req.path.startsWith("/api/project/versions");
const requiresToken = protectsVersionRead || !["GET", "HEAD", "OPTIONS"].includes(context.req.method);
if (expected && requiresToken && context.req.header("X-Agents-Playground-Token") !== expected) {
return context.json({ error: { message: "Invalid Playground access token." } }, 403);
}
await next();
});
// Routes
app.route("/api", projectRoute);
app.route("/api", projectSessionsRoute);
app.route("/api", operationsRoute);
// OpenAPI document
app.doc("/openapi.json", {
openapi: "3.0.0",
info: {
title: "Managed Agents Workbench API",
version: "1.0.0",
},
});
// Health check
app.get("/health", (c) =>
c.json({
status: "ok",
project: { id: projectRuntimeManager.projectId, config_path: projectRuntimeManager.configPath },
}),
);
// Centralized error formatting: routes throw, this maps to { error: { message } }.
app.onError((error) => jsonError(error));