Implementation:1broseidon Ketch pi ketch tools
| Knowledge Sources | |
|---|---|
| Domains | CLI, Agent_Integration |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
The pi package extension that exposes `ketch_*` tool adapters over `ketch … --json`, translating pi tool calls into CLI invocations and projecting rich JSON results into a bounded, terminal-safe TUI render.
Description
`plugins/pi/extensions/ketch.ts` is the pi package extension manifest entry point. It defines six tool adapters — `ketch_search`, `ketch_code`, `ketch_docs`, `ketch_scrape`, `ketch_crawl`, `ketch_tag` — each backed by a real `ketch … --json` subprocess call (or, for the CLI test variant, the actual binary). The adapters map pi tool arguments onto ketch CLI flags (`query`→positional, `backend`→`-b`, `scrape`→`--scrape`, `multi`→`--multi=…`, etc.), invoke ketch, and return the parsed JSON to the model unchanged.
The render layer (see Implementation:1broseidon_Ketch_pi_render) is UI-only: it never mutates tool content or details — the model keeps the complete CLI response. Rendering uses Text (not Markdown) to keep remote content inert, and safeText strips ANSI/OSC/DCS sequences, C0/C1 controls, and invisible bidi overrides so a hostile page cannot inject terminal control sequences. callSummary bounds the call display independently of argument size; resultSummary bounds the result display independently of the model response size (max 4 rows previewed, `+N more`).
Usage
Loaded by pi when the `plugins/ketch` plugin is installed (via the root `package.json` manifest). Agents call `ketch_search`, `ketch_scrape`, etc., and see bounded, terminal-safe renderings while retaining the full JSON for reasoning.
Code Reference
Source Location
- Repository: ketch
- File: plugins/pi/extensions/ketch.ts
- Lines: 1-728
Signature
// Tool adapter shape (one per surface); the module exports the registered set.
export type Surface = "search" | "code" | "docs" | "scrape" | "crawl" | "tag";
// Each adapter: pi tool name → ketch CLI argv builder → JSON result parser.
// The renderers are provided by renderers(surface) in render.ts.
Import
// Declared in the root package.json under pi's `extensions` field.
import { renderers } from "./render";
import type { ToolRenderers } from "@earendil-works/pi-coding-agent";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| surface | Surface | Yes | One of search/code/docs/scrape/crawl/tag |
| args | object | Yes | Pi tool arguments (query, url, backend, limit, tag, …) |
| ketch binary | executable | Yes | `ketch … --json` invoked as a subprocess |
Outputs
| Name | Type | Description |
|---|---|---|
| tool result | object | Parsed ketch JSON output, returned to the model unchanged |
| render | Text lines | Bounded, terminal-safe display via renderers(surface) |
Usage Examples
Pi agent invoking ketch_search
// The agent calls ketch_search with these args; the adapter runs:
// ketch search "ripgrep recursive search" --json -l 5
// and returns the parsed JSON. The TUI shows a bounded callSummary + resultSummary.
Batch scrape with tag
// ketch_scrape { urls: [...], tag: "docs" }
// → ketch scrape <url1> <url2> --tag docs --json
// → JSON result with tagged URLs; render previews 4 pages max.