import { z } from "zod"; import { forbidden } from "../../errors"; import { limitWrite } from "../../limits"; import { hasPermission } from "../../repos"; import { scopedRepoPermission, TOKEN_AREAS } from "../../token-scope"; import type { TokenArea, TokenPermissions } from "../../token-scope"; import type { Permission } from "~/lib/types"; import { mintFineGrainedToken } from "../grants"; import { defineTool, repoArgs, scopedRepo, ToolError } from "./base"; const MAX_TOKEN_DAYS = 7; const DAY_MS = 24 * 60 * 60 * 1000; const CONFIRM_KEY = "confirm_token"; function needed(area: TokenArea, access: "read" | "write"): Permission { if (area === "admin") return access === "write" ? "admin" : "read"; return access; } const createTokenTool = defineTool({ name: "create_repo_token", title: "Create scoped repository token", description: `Mint a fine-grained token for one repository (e.g. for a sub-agent or CI), valid up to ${MAX_TOKEN_DAYS} days. It can never exceed this grant's own access; clients that support elicitation are asked to confirm.`, input: z.object({ ...repoArgs, name: z.string().trim().min(1).max(80), permissions: z.object(Object.fromEntries(TOKEN_AREAS.map((area) => [area, z.enum(["read", "write"]).optional()]))), expires_in_days: z.number().int().min(1).max(MAX_TOKEN_DAYS).default(1), }), write: true, async run({ caller, inputResponses }, args) { await limitWrite(caller.viewer); const { repo } = await scopedRepo(caller, args.owner, args.repo, "contents", "read"); const permissions: TokenPermissions = {}; for (const area of TOKEN_AREAS) { const access = args.permissions[area]; if (access !== "read" && access !== "write") continue; if (!hasPermission(await scopedRepoPermission(repo, caller.credential, area), needed(area, access))) { throw forbidden(`This grant cannot delegate ${area}:${access}`); } permissions[area] = access; } if (Object.keys(permissions).length === 0) throw new ToolError("Request at least one permission"); if (caller.clientCapabilities.elicitation && caller.era === "modern") { const answer = inputResponses?.[CONFIRM_KEY]; if (!answer) { return { inputRequired: { [CONFIRM_KEY]: { method: "elicitation/create", params: { mode: "form", message: `Create token "${args.name}" for ${repo.owner_name}/${repo.name} with ${Object.entries( permissions, ) .map(([area, access]) => `${area}:${access}`) .join(", ")} for ${args.expires_in_days} day(s)?`, requestedSchema: { type: "object", properties: { approve: { type: "boolean", title: "Approve" } }, required: ["approve"], }, }, }, }, }; } const approved = typeof answer === "object" && answer !== null && Reflect.get(answer, "action") === "accept" && Reflect.get(Reflect.get(answer, "content") ?? {}, "approve") === true; if (!approved) throw new ToolError("Token creation was declined", true); } const minted = await mintFineGrainedToken({ userId: caller.viewer.id, name: `${args.name} (via ${caller.agent.name})`, scopes: "fine-grained", grant: { permissions, allRepos: false, repoIds: [repo.id], agent: caller.agent.name }, ttlMs: args.expires_in_days * DAY_MS, clientId: caller.agent.clientId, }); return { structured: { token: minted.token, id: minted.id, repo: `${repo.owner_name}/${repo.name}`, permissions, expires_at: minted.expiresAt, }, repoId: repo.id, }; }, }); const deploySiteTool = defineTool({ name: "deploy_site", title: "Deploy site", description: "Deploy a repository's site from a ref. Not yet available on this forge; the call reports that.", input: z.object({ ...repoArgs, ref: z.string().max(250).optional() }), write: true, async run({ caller }, args) { await scopedRepo(caller, args.owner, args.repo, "contents", "write"); throw new ToolError("Sites are not available on this forge yet; push to the Pages branch to publish instead"); }, }); export const accessTools = [createTokenTool, deploySiteTool];