@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
253 lines (252 loc) • 11.9 kB
JavaScript
import { z } from "zod";
import { listAcross, ForgeError, IssueListError, parseRepo, providerFor, repoForProject } from "../forge/issues.js";
import { listAllProjects } from "../store-routing.js";
import { BridgeError } from "./protocol.js";
/**
* The phone's forge issue routes (docs/phren-hook.md#issues-from-the-phone):
* a list and an item read, and the five writes. Every route is owner-only; the
* writes also go through owner-proof.ts. All forge work lives in ../forge/.
*/
/** A list body is trimmed to this many characters; /v1/issues/item has the whole body. */
export const LIST_BODY_LIMIT = 2_000;
/** The phone drops off Tailscale on big bursts, so one list answer stays under this. */
export const ISSUES_RESPONSE_LIMIT = 256 * 1024;
const LIST_CHECKLIST_BYTES = 16 * 1024;
const LIST_ERRORS_BYTES = 32 * 1024;
const DEFAULT_REPO_TTL_MS = 5 * 60_000;
const issueNumber = z.number().int().min(1).max(2_147_483_647);
const repoRef = z.string().trim().min(3).max(300);
const issueText = z.string().max(100_000);
export const issueListQuerySchema = z.object({
repos: z.array(repoRef).max(64).optional(),
state: z.enum(["open", "closed", "all"]).optional(),
assignee: z.string().trim().min(1).max(200).optional(),
label: z.string().trim().min(1).max(200).optional(),
search: z.string().trim().min(1).max(300).optional(),
limit: z.number().int().min(1).max(200).optional(),
}).strict();
export const issueItemQuerySchema = z.object({ repo: repoRef, number: issueNumber }).strict();
/** The bodies mirror the MCP issue tools (../forge/issues.ts types). */
export const issueCreateSchema = z.object({
repo: repoRef,
title: z.string().trim().min(1).max(1_000),
body: issueText.optional(),
checklist: z.array(z.string().trim().min(1).max(1_000)).max(200).optional(),
labels: z.array(z.string().trim().min(1).max(100)).max(50).optional(),
}).strict();
export const issueEditSchema = z.object({
repo: repoRef,
number: issueNumber,
title: z.string().trim().min(1).max(1_000).optional(),
body: issueText.optional(),
checklist: z.array(z.object({ text: z.string().trim().min(1).max(1_000), done: z.boolean() }).strict()).max(200).optional(),
addLabels: z.array(z.string().trim().min(1).max(100)).max(50).optional(),
removeLabels: z.array(z.string().trim().min(1).max(100)).max(50).optional(),
state: z.enum(["open", "closed"]).optional(),
}).strict();
export const issueCommentSchema = z.object({ repo: repoRef, number: issueNumber, body: z.string().trim().min(1).max(100_000) }).strict();
/** `computer` defaults to the Hook's own computer name. */
export const issueClaimSchema = z.object({ repo: repoRef, number: issueNumber, computer: z.string().trim().min(1).max(253).optional(), release: z.boolean().optional() }).strict();
export const issueCloseSchema = z.object({ repo: repoRef, number: issueNumber, comment: z.string().trim().min(1).max(100_000).optional() }).strict();
/** Forge failures reach the phone as stable HTTP statuses and `forge-<code>` codes. */
const FORGE_STATUS = {
auth: 502, "not-found": 404, forbidden: 403, "rate-limited": 429, unavailable: 503, invalid: 400, conflict: 409,
};
function forgeBridgeError(error) {
if (!(error instanceof ForgeError))
throw error;
const code = `forge-${error.code}`;
if (error instanceof IssueListError) {
const { errors, truncated } = boundedErrors(error.errors);
const message = error.message.slice(0, 500);
throw new BridgeError(FORGE_STATUS[error.code], message, {
code, issues: [], errors, truncated: truncated || message !== error.message,
});
}
throw new BridgeError(FORGE_STATUS[error.code], error.message, { code });
}
async function mapped(run) {
try {
return await run();
}
catch (error) {
return forgeBridgeError(error);
}
}
const forgeDeps = {
list: query => listAcross(query),
get: ref => providerFor(ref.repo).get(ref),
create: input => providerFor(input.repo).create(input),
edit: (ref, edit) => providerFor(ref.repo).edit(ref, edit),
comment: (ref, body) => providerFor(ref.repo).comment(ref, body),
claim: (ref, computer, release) => providerFor(ref.repo).claim(ref, computer, release),
close: (ref, comment) => providerFor(ref.repo).close(ref, comment),
projects: async (store) => listAllProjects(store).map(entry => `${entry.store.name}/${entry.projectName}`),
repoFor: (project, store) => repoForProject(project, store),
now: Date.now,
};
let deps = forgeDeps;
let defaultRepos;
/** Tests swap the forge for a fake; the default repo cache goes with it. */
export function setIssueRouteDeps(overrides) {
const previous = deps;
deps = { ...forgeDeps, ...overrides };
defaultRepos = undefined;
return () => { deps = previous; defaultRepos = undefined; };
}
export function resetIssueRouteCache() { defaultRepos = undefined; }
/** Put the real forge back, for tests. */
export function resetIssueRouteDeps() { deps = forgeDeps; defaultRepos = undefined; }
/**
* Every store project whose repository resolves, cached for five minutes: the
* phone never names the repos, so this walk runs once per burst. Four projects
* at a time, never one `git config` per project all at once.
*/
export async function defaultIssueRepos(store) {
const now = deps.now();
if (defaultRepos?.store === store && now - defaultRepos.at < DEFAULT_REPO_TTL_MS)
return defaultRepos.repos;
const projects = await deps.projects(store);
const found = new Array(projects.length);
let next = 0;
await Promise.all(Array.from({ length: Math.min(4, projects.length) }, async () => {
while (next < projects.length) {
const index = next++;
found[index] = await deps.repoFor(projects[index], store).catch(() => undefined);
}
}));
const repos = [...new Set(found.filter((repo) => Boolean(repo)))];
defaultRepos = { store, at: now, repos };
return repos;
}
function jsonBytes(value) {
return Buffer.byteLength(JSON.stringify(value));
}
/** Count brackets and separators against the same UTF-8 budget as each entry. */
function takeWithin(entries, budget) {
const kept = [];
let bytes = 2;
for (const entry of entries) {
const size = jsonBytes(entry);
if (size + 2 > budget)
continue;
const separator = kept.length ? 1 : 0;
if (bytes + size + separator > budget)
break;
kept.push(entry);
bytes += size + separator;
}
return kept;
}
function boundedErrors(failures) {
let clipped = false;
function* previews() {
for (const { repo, code, error } of failures) {
clipped ||= repo.length > 300 || error.length > 500;
yield { repo: repo.slice(0, 300), code, error: error.slice(0, 500) };
}
}
const errors = takeWithin(previews(), LIST_ERRORS_BYTES);
return { errors, truncated: clipped || errors.length < failures.length };
}
/** Full bodies and checklists are available on the item route. */
function listIssue(issue) {
let clipped = false;
function* previews() {
for (const { text, done } of issue.checklist) {
clipped ||= text.length > 1_000;
yield { text: text.slice(0, 1_000), done };
}
}
const checklist = takeWithin(previews(), LIST_CHECKLIST_BYTES);
const body = issue.body.slice(0, LIST_BODY_LIMIT);
const truncated = body !== issue.body || clipped || checklist.length < issue.checklist.length;
return { ...issue, body, checklist, ...(truncated ? { truncated: true } : {}) };
}
function boundedList(result) {
const { errors, truncated } = boundedErrors(result.errors);
// The empty issues array already contributes two bytes to the envelope.
const budget = ISSUES_RESPONSE_LIMIT - jsonBytes({ issues: [], errors, truncated: false }) + 2;
function* rows() {
for (const issue of result.issues)
yield listIssue(issue);
}
const issues = takeWithin(rows(), budget);
return { issues, errors, truncated: truncated || issues.length < result.issues.length };
}
function reposFrom(url) {
return url.searchParams.getAll("repos")
.flatMap(value => value.split(","))
.map(value => value.trim())
.filter(Boolean);
}
/** A query value, with an empty one read as absent. */
function param(url, name) {
const value = url.searchParams.get(name);
return value === null || value === "" ? undefined : value;
}
function numberParam(value) {
if (value === undefined)
return undefined;
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : value;
}
/** `GET /v1/issues`: every named repo, or every store project's repo. */
export async function issueListRoute(url, store) {
const repos = reposFrom(url);
const state = param(url, "state"), assignee = param(url, "assignee"), label = param(url, "label");
const search = param(url, "search"), limit = numberParam(param(url, "limit"));
const query = issueListQuerySchema.parse({
...(repos.length ? { repos } : {}),
...(state === undefined ? {} : { state }),
...(assignee === undefined ? {} : { assignee }),
...(label === undefined ? {} : { label }),
...(search === undefined ? {} : { search }),
...(limit === undefined ? {} : { limit }),
});
const resolved = query.repos?.length ? query : { ...query, repos: await defaultIssueRepos(store) };
return boundedList(await mapped(() => deps.list(resolved)));
}
/** `GET /v1/issues/item`: one issue with its full body. */
export async function issueItemRoute(url) {
const query = issueItemQuerySchema.parse({ repo: param(url, "repo"), number: numberParam(param(url, "number")) });
const issue = await mapped(() => deps.get({ repo: parseRepo(query.repo), number: query.number }));
return { issue };
}
/** `POST /v1/issues/{create,edit,comment,claim,close}` */
export async function issueWriteRoute(pathname, data, computer) {
switch (pathname) {
case "/v1/issues/create": {
const input = issueCreateSchema.parse(data);
const issue = await mapped(() => deps.create({
repo: parseRepo(input.repo), title: input.title,
...(input.body === undefined ? {} : { body: input.body }),
...(input.checklist === undefined ? {} : { checklist: input.checklist }),
...(input.labels === undefined ? {} : { labels: input.labels }),
}));
return { ok: true, issue };
}
case "/v1/issues/edit": {
const input = issueEditSchema.parse(data);
const { repo, number, ...edit } = input;
const issue = await mapped(() => deps.edit({ repo: parseRepo(repo), number }, edit));
return { ok: true, issue };
}
case "/v1/issues/comment": {
const input = issueCommentSchema.parse(data);
const url = await mapped(() => deps.comment({ repo: parseRepo(input.repo), number: input.number }, input.body));
return { ok: true, url: url.url };
}
case "/v1/issues/claim": {
const input = issueClaimSchema.parse(data);
const issue = await mapped(() => deps.claim({ repo: parseRepo(input.repo), number: input.number }, input.computer ?? computer, input.release));
return { ok: true, issue };
}
case "/v1/issues/close": {
const input = issueCloseSchema.parse(data);
const issue = await mapped(() => deps.close({ repo: parseRepo(input.repo), number: input.number }, input.comment));
return { ok: true, issue };
}
default: throw new BridgeError(404, "Unknown Phren Hook route.");
}
}