repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/command-adapter.ts

Raw
File outline
import type { RpcClient, RpcResponse } from "./rpc";

export type RpcSender = Pick<RpcClient, "send">;

export type PromptOptions = {
	digest?: () => string;
	clearDigest?: () => void;
};

export function sendPrompt(
	rpc: RpcSender,
	text: string,
	options: PromptOptions = {},
): Promise<RpcResponse> {
	const digest = options.digest?.() ?? "";
	if (!digest) return rpc.send({ type: "prompt", message: text });
	options.clearDigest?.();
	return rpc.send({
		type: "prompt",
		message: `${digest}\n\nUser prompt:\n${text}`,
	});
}

export function runSlashCommand(
	rpc: RpcSender,
	command: string,
	args = "",
): Promise<RpcResponse> {
	const suffix = args.trim() ? ` ${args.trim()}` : "";
	return sendPrompt(rpc, `/${command}${suffix}`);
}

export function runBash(
	rpc: RpcSender,
	command: string,
	excludeFromContext: boolean,
): Promise<RpcResponse> {
	return rpc.send({ type: "bash", command, excludeFromContext });
}

export function cycleModel(rpc: RpcSender): Promise<RpcResponse> {
	return rpc.send({ type: "cycle_model" });
}

export function cycleThinking(rpc: RpcSender): Promise<RpcResponse> {
	return rpc.send({ type: "cycle_thinking_level" });
}

export function compactSession(rpc: RpcSender): Promise<RpcResponse> {
	return rpc.send({ type: "compact" });
}

export function abortCurrent(rpc: RpcSender): Promise<RpcResponse> {
	return rpc.send({ type: "abort" });
}