|
| 1 | +import { |
| 2 | + createDb, |
| 3 | + deleteUserSkill, |
| 4 | + listUserSkills, |
| 5 | + type UserSkillRecord, |
| 6 | + upsertUserSkill, |
| 7 | + withUserContext, |
| 8 | +} from "@cheatcode/db"; |
| 9 | +import { APIError } from "@cheatcode/observability"; |
| 10 | +import { |
| 11 | + CreateUserSkillSchema, |
| 12 | + type UserId, |
| 13 | + UserSkillSchema, |
| 14 | + UserSkillsResponseSchema, |
| 15 | +} from "@cheatcode/types"; |
| 16 | +import { z } from "zod"; |
| 17 | +import type { GatewayEnv } from "./index"; |
| 18 | + |
| 19 | +const IdParamSchema = z.string().uuid(); |
| 20 | +const DEFAULT_SKILL_CATEGORY = "Builder & Apps"; |
| 21 | + |
| 22 | +function skillSummary(record: UserSkillRecord): unknown { |
| 23 | + return UserSkillSchema.parse({ |
| 24 | + category: record.category, |
| 25 | + createdAt: record.createdAt.toISOString(), |
| 26 | + description: record.description, |
| 27 | + id: record.id, |
| 28 | + name: record.name, |
| 29 | + tags: record.tags, |
| 30 | + updatedAt: record.updatedAt.toISOString(), |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +/** `GET /v1/skills` — the caller's custom skills (body-less summaries). */ |
| 35 | +export async function listUserSkillsRoute( |
| 36 | + env: GatewayEnv, |
| 37 | + ctx: ExecutionContext, |
| 38 | + userId: UserId, |
| 39 | +): Promise<Response> { |
| 40 | + const { db, close } = createDb(env.HYPERDRIVE); |
| 41 | + try { |
| 42 | + const rows = await withUserContext(db, userId, (tx) => listUserSkills(tx, userId)); |
| 43 | + return Response.json(UserSkillsResponseSchema.parse({ skills: rows.map(skillSummary) })); |
| 44 | + } finally { |
| 45 | + ctx.waitUntil(close()); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * `POST /v1/skills` — create or update (by name) a custom skill. Used by the agent's |
| 51 | + * `skill_create` tool path and the manual creation form. |
| 52 | + */ |
| 53 | +export async function createUserSkillRoute( |
| 54 | + env: GatewayEnv, |
| 55 | + ctx: ExecutionContext, |
| 56 | + request: Request, |
| 57 | + userId: UserId, |
| 58 | +): Promise<Response> { |
| 59 | + const parsed = CreateUserSkillSchema.safeParse(await request.json()); |
| 60 | + if (!parsed.success) { |
| 61 | + throw new APIError(400, "invalid_request_body", "Invalid skill payload", { |
| 62 | + details: { issues: parsed.error.issues.map((issue) => issue.message) }, |
| 63 | + retriable: false, |
| 64 | + }); |
| 65 | + } |
| 66 | + const input = parsed.data; |
| 67 | + const { db, close } = createDb(env.HYPERDRIVE); |
| 68 | + try { |
| 69 | + const record = await withUserContext(db, userId, (tx) => |
| 70 | + upsertUserSkill(tx, { |
| 71 | + body: input.body, |
| 72 | + category: input.category ?? DEFAULT_SKILL_CATEGORY, |
| 73 | + description: input.description, |
| 74 | + name: input.name, |
| 75 | + tags: input.tags ?? [], |
| 76 | + userId, |
| 77 | + }), |
| 78 | + ); |
| 79 | + return Response.json(skillSummary(record), { status: 201 }); |
| 80 | + } finally { |
| 81 | + ctx.waitUntil(close()); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** `DELETE /v1/skills/:id` — soft-delete a custom skill the caller owns. */ |
| 86 | +export async function deleteUserSkillRoute( |
| 87 | + env: GatewayEnv, |
| 88 | + ctx: ExecutionContext, |
| 89 | + userId: UserId, |
| 90 | + skillId: string, |
| 91 | +): Promise<Response> { |
| 92 | + const parsed = IdParamSchema.safeParse(skillId); |
| 93 | + if (!parsed.success) { |
| 94 | + throw new APIError(400, "invalid_path_param", "Invalid skill id", { retriable: false }); |
| 95 | + } |
| 96 | + const { db, close } = createDb(env.HYPERDRIVE); |
| 97 | + try { |
| 98 | + const deleted = await withUserContext(db, userId, (tx) => |
| 99 | + deleteUserSkill(tx, userId, parsed.data), |
| 100 | + ); |
| 101 | + if (!deleted) { |
| 102 | + throw new APIError(404, "not_found_skill", "Skill not found", { retriable: false }); |
| 103 | + } |
| 104 | + return new Response(null, { status: 204 }); |
| 105 | + } finally { |
| 106 | + ctx.waitUntil(close()); |
| 107 | + } |
| 108 | +} |
0 commit comments