@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
45 lines (44 loc) • 3.94 kB
JavaScript
import * as fs from "fs";
import * as path from "path";
import { z } from "zod";
import { detectProject } from "../shared/index.js";
import { HELPER_TIMEOUT_DEFAULT_S, HELPER_TIMEOUT_MAX_S, helperFor, runHelper } from "../bridge/helper.js";
import { mcpResponse, resolveStoreForProject } from "./types.js";
export function register(server, ctx) {
server.registerTool("helper", {
title: "◆ phren · helper",
description: `Run one small, self-contained task as a headless Claude Code run (claude -p) under the project's helper account and model (phren.project.yaml helper: { account, model }, model haiku by default), in your folder (a project checkout or worktree), and return its reply. Use it for narrow work you can describe fully in one prompt (summarize a file, draft a test, look something up); the helper sees no conversation, only the prompt and the folder. It bills the helper account, not yours: Claude Code's own subagents always run under your login. Read-only unless allowEdits (Bash and the edit tools are refused); no MCP servers. Stops after timeoutSeconds (default ${HELPER_TIMEOUT_DEFAULT_S}, at most ${HELPER_TIMEOUT_MAX_S}). Refused when the project has no helper account, the account's role is not helpers, or its pool is not allowed for the project or this computer.`,
inputSchema: {
prompt: z.string().min(1).max(100_000).describe("The whole task: what to do, which files, what to return."),
project: z.string().optional().describe("Expected project; it must be the project cwd belongs to, whose helper setting applies."),
cwd: z.string().optional().describe("Absolute folder the helper runs in (your worktree); default: this server's folder."),
timeoutSeconds: z.number().int().min(1).max(HELPER_TIMEOUT_MAX_S).optional(),
allowEdits: z.boolean().optional().describe("Let the helper edit files in cwd (acceptEdits). Default false."),
},
}, async (input, extra) => {
try {
if (input.cwd !== undefined && !path.isAbsolute(input.cwd))
throw new Error("cwd is an absolute path.");
const cwd = path.resolve(input.cwd ?? process.cwd());
if (!fs.statSync(cwd, { throwIfNoEntry: false })?.isDirectory())
throw new Error(`${cwd} is not a folder.`);
// The folder decides whose rules apply: a helper only runs in a project's checkout or worktree, under that
// project's own helper and pools, so naming another project can't carry its account into this folder.
const owner = detectProject(ctx.phrenPath, cwd, ctx.profile);
if (!owner)
throw new Error(`${cwd} is not in a project's folder; a helper runs only in a project checkout or worktree.`);
if (input.project !== undefined && input.project !== owner)
throw new Error(`${cwd} belongs to project ${owner}, not ${input.project}.`);
const { phrenPath, project } = resolveStoreForProject(ctx, owner, "read");
const choice = helperFor(phrenPath, project);
const result = await runHelper(choice, { prompt: input.prompt, cwd, timeoutSeconds: input.timeoutSeconds, allowEdits: input.allowEdits, signal: extra?.signal });
const spent = result.costUSD !== undefined ? `, $${result.costUSD.toFixed(4)}` : "";
// The reply is in data.output; a failure's reason is also the error.
return mcpResponse({ ok: result.ok, data: result, ...(result.ok ? {} : { error: result.output.slice(0, 2000) }),
message: `${result.ok ? "Helper replied" : "Helper failed"} (${result.account}, ${result.model}, ${Math.round(result.durationMs / 1000)}s${spent}).` });
}
catch (error) {
return mcpResponse({ ok: false, error: error instanceof Error ? error.message : "The helper could not run." });
}
});
}