zz_

app/cli/lib/remoteTools.ts

zzmq.dev@gmail.com/zz / app / cli / lib / remoteTools.ts @ 31943d52 Download
Ladder Summary
  • file This module provides utilities for invoking a worker’s server‑side tools over the HTTP API. It exports **splitSlug** (parses an “owner/repo” slug into its two parts), **callRemoteTool** (sends a POST request to `/api/repos/:owner/:repo/tools/:name` with optional anonymous access), **remoteCallTool** (extracts the `repo` argument from the tool payload and forwards the call via `callRemoteTool`), and **remoteExecuteTool** (a thin wrapper that calls `remoteCallTool` using a `ToolDef` definition).
 // Calls a single tool by name against a deployed Worker's MaintainerDO,
 // via the generic /api/repos/:owner/:repo/tools/:name passthrough
 // (app/worker/src/webApp.ts's handleApiCallTool). This is the one
 // mechanism "remote zz work" is built on: the exact same tool code
 // (agent/tools.ts's ALL_TOOLS) runs server-side either way, so there's
 // no separate remote reimplementation of any protocol logic to drift
 // out of sync with the local path.
 import { apiFetch } from "./api.js";
 import type { ToolDef } from "../../agent/tools.js";
 
 export function splitSlug(slug: string): [string, string] {
  const idx = slug.lastIndexOf("/");
  if (idx === -1) throw new Error(`expected "owner/repo-name", got "${slug}"`);
  return [slug.slice(0, idx), slug.slice(idx + 1)];
 }
 
 export async function callRemoteTool(repoSlug: string, name: string, args: Record<string, unknown>): Promise<unknown> {
  const [owner, repo] = splitSlug(repoSlug);
  const { result } = await apiFetch<{ result: unknown }>("POST", `/api/repos/${owner}/${repo}/tools/${name}`, args);
  return result;
 }
 
 // A generic ProtocolCall/executeTool implementation, repo-agnostic --
 // every tool's own arguments already carry `repo` (the model always
 // supplies it; scriptedWork.ts's own call sites always do too), so
 // these just forward whichever repo the call actually names instead
 // of needing one supplied separately. Drop-in for agent/loop.ts's
 // `executeTool` option and agent/scriptedWork.ts's `callTool` option.
 export async function remoteCallTool(name: string, args: Record<string, unknown>): Promise<unknown> {
  const repo = args.repo as string | undefined;
  if (!repo) throw new Error(`tool "${name}" call is missing a "repo" argument`);
  return callRemoteTool(repo, name, args);
 }
 
 export async function remoteExecuteTool(tool: ToolDef, args: Record<string, unknown>): Promise<unknown> {
  return remoteCallTool(tool.name, args);
 }