mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
p2 rewritten by the Chord v2 authoring rules (decision "App authoring rules confirmed by the p2 audit"), in four parts merged together: - core: session lifecycle (relocate, clone, fork, archive) and search rebuild are persisted jobs on a new server store; Sessions v6 with client-chosen ids and visible operations; Retry answers with the worker's real outcome; workers push branch, subagent and status state to the server; roots take the method's ctx. - services: models, envs, ports, login, mcp, logs, sharing and voice follow external state (self.follow, followNative) instead of copy loops; methods answer after their state commits; presence from host.references. - chat: chat mode, artifacts and prompt resources follow Durable and native state; shell commands are Chord jobs (shellId is the requestId); status pushed before send and stop answer; race-free draft claim. - ui: optimistic updates on web (useOptimistic) and terminal (TuiScope.optimistic, extended in Chord with a reducer and a pending flag), pending states for long work, visible rollback, load waterfalls removed; merged with the one composer and immediate first send (ccff9876e) and the folder + button (5a31a6d5f). docs/optimistic-ui.md describes the pattern.
50 lines
2.9 KiB
TypeScript
50 lines
2.9 KiB
TypeScript
// The terminals plugin's worker facet: the agent's `terminal` tool, calling the server's `AgentTerminals` as this
|
|
// session's worker, at the calling conversation's location. It follows the code mode switch as MCP tools do: for
|
|
// scripts while code mode is on, loaded with `tool_search` while it is off.
|
|
|
|
import type { Extension, ToolExecutionApi } from "@earendil-works/pi-durable";
|
|
import { type Context, type Ctx, contribute, defineFacet, expectVariant } from "@p2/app/v1";
|
|
import { ExtensionSwitches } from "@p2/app/v1/node";
|
|
import { Extensions, harnessOf, workerOf } from "@p2/app/v1/worker";
|
|
import { AgentTerminals } from "./contract.ts";
|
|
import { createTerminalExtension, TERMINAL_EXTENSION, type TerminalBackend, type TerminalReach } from "./tool.ts";
|
|
|
|
/** The code mode plugin's extension: its switch is on while the `codemode` setting is, absent without the plugin. */
|
|
const CODEMODE_EXTENSION = "codemode";
|
|
|
|
export default defineFacet((host) => {
|
|
contribute(host, "p2.terminal-tools", (self) => {
|
|
const harness = harnessOf(self);
|
|
const worker = workerOf(self);
|
|
const terminals = async (ctx: Ctx) => expectVariant(await ctx.get(AgentTerminals, {}), "worker");
|
|
/** One call of the server's object, as this worker, cancelled with `context`. */
|
|
const via = <T>(
|
|
call: (server: Awaited<ReturnType<typeof terminals>>, ctx: Ctx) => Promise<T>,
|
|
context: Context,
|
|
): Promise<T> => worker().root(async (ctx) => call(await terminals(ctx), ctx), context);
|
|
const backend: TerminalBackend = {
|
|
list: async (context) => (await via((server, ctx) => server.list({}, ctx), context)).windows,
|
|
open: (args, context) => via((server, ctx) => server.open(args, ctx), context),
|
|
send: (args, context) => via((server, ctx) => server.send(args, ctx), context),
|
|
cursor: (target, context) => via((server, ctx) => server.cursor({ target }, ctx), context),
|
|
read: (args, context) => via((server, ctx) => server.read(args, ctx), context),
|
|
capture: (args, context) => via((server, ctx) => server.capture(args, ctx), context),
|
|
close: (args, context) => via((server, ctx) => server.close(args, ctx), context),
|
|
};
|
|
const locate = (api: ToolExecutionApi, context: Context) =>
|
|
harness().environments.locate(api.conversationId, context, api);
|
|
const offer = (reach: TerminalReach) => ({
|
|
[TERMINAL_EXTENSION]: createTerminalExtension(reach, backend, locate),
|
|
});
|
|
let offered: TerminalReach = "codemode";
|
|
const extension = self.writable<Readonly<Record<string, Extension>>>(offer(offered));
|
|
self.contribute(Extensions, extension);
|
|
// Code mode switched off or not loaded: scripts cannot reach the tool, so `tool_search` loads it.
|
|
self.on(self.registry(ExtensionSwitches), ({ value }) => {
|
|
const reach: TerminalReach = value.get(CODEMODE_EXTENSION) === true ? "codemode" : "tool_search";
|
|
if (reach === offered) return;
|
|
offered = reach;
|
|
extension.set(offer(reach));
|
|
});
|
|
});
|
|
});
|