-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Expand file tree
/
Copy pathagent-blueprints.ts
More file actions
63 lines (56 loc) · 2.06 KB
/
agent-blueprints.ts
File metadata and controls
63 lines (56 loc) · 2.06 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
57
58
59
60
61
62
63
import { Router } from "express";
import type { Db } from "@paperclipai/db";
import {
createAgentBlueprintSchema,
updateAgentBlueprintSchema,
} from "@paperclipai/shared";
import { validate } from "../middleware/validate.js";
import { assertBoard, assertAuthenticated } from "./authz.js";
import { notFound } from "../errors.js";
import { agentBlueprintService } from "../services/agent-blueprints.js";
export function agentBlueprintRoutes(db: Db) {
const router = Router();
const svc = agentBlueprintService(db);
// Read endpoints are open to both board users and agents (agents need to
// query blueprints when the CEO is asked to hire from a template).
router.get("/blueprints", async (req, res) => {
assertAuthenticated(req);
const search = typeof req.query.search === "string" ? req.query.search : undefined;
const role = typeof req.query.role === "string" ? req.query.role : undefined;
res.json(await svc.list({ search, role }));
});
router.get("/blueprints/:id", async (req, res) => {
assertAuthenticated(req);
const blueprint = await svc.get(req.params.id as string);
if (!blueprint) throw notFound("Blueprint not found");
res.json(blueprint);
});
router.post(
"/blueprints",
validate(createAgentBlueprintSchema),
async (req, res) => {
assertBoard(req);
const blueprint = await svc.create(req.body);
res.status(201).json(blueprint);
},
);
router.patch(
"/blueprints/:id",
validate(updateAgentBlueprintSchema),
async (req, res) => {
assertBoard(req);
const existing = await svc.get(req.params.id as string);
if (!existing) throw notFound("Blueprint not found");
const updated = await svc.update(req.params.id as string, req.body);
res.json(updated);
},
);
router.delete("/blueprints/:id", async (req, res) => {
assertBoard(req);
const existing = await svc.get(req.params.id as string);
if (!existing) throw notFound("Blueprint not found");
await svc.delete(req.params.id as string);
res.status(204).end();
});
return router;
}