chore: make pi-only rpc with fixed sessions
This commit is contained in:
@@ -1,62 +1,39 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { CLAUDE_IDENTITY_PREFIX } from "../auto-reply/claude.js";
|
||||
import { OPENCODE_IDENTITY_PREFIX } from "../auto-reply/opencode.js";
|
||||
import { claudeSpec } from "./claude.js";
|
||||
import { codexSpec } from "./codex.js";
|
||||
import { GEMINI_IDENTITY_PREFIX, geminiSpec } from "./gemini.js";
|
||||
import { opencodeSpec } from "./opencode.js";
|
||||
import { piSpec } from "./pi.js";
|
||||
|
||||
describe("agent buildArgs + parseOutput helpers", () => {
|
||||
it("claudeSpec injects flags and identity once", () => {
|
||||
const argv = ["claude", "hi"];
|
||||
const built = claudeSpec.buildArgs({
|
||||
describe("pi agent helpers", () => {
|
||||
it("buildArgs injects print/format flags and identity once", () => {
|
||||
const argv = ["pi", "hi"];
|
||||
const built = piSpec.buildArgs({
|
||||
argv,
|
||||
bodyIndex: 1,
|
||||
isNewSession: true,
|
||||
sessionId: "sess",
|
||||
sendSystemOnce: false,
|
||||
systemSent: false,
|
||||
identityPrefix: undefined,
|
||||
identityPrefix: "IDENT",
|
||||
format: "json",
|
||||
});
|
||||
expect(built).toContain("--output-format");
|
||||
expect(built).toContain("json");
|
||||
expect(built).toContain("-p");
|
||||
expect(built.at(-1)).toContain(CLAUDE_IDENTITY_PREFIX);
|
||||
expect(built).toContain("--mode");
|
||||
expect(built).toContain("json");
|
||||
expect(built.at(-1)).toContain("IDENT");
|
||||
|
||||
const builtNoIdentity = claudeSpec.buildArgs({
|
||||
const builtNoIdentity = piSpec.buildArgs({
|
||||
argv,
|
||||
bodyIndex: 1,
|
||||
isNewSession: false,
|
||||
sessionId: "sess",
|
||||
sendSystemOnce: true,
|
||||
systemSent: true,
|
||||
identityPrefix: undefined,
|
||||
identityPrefix: "IDENT",
|
||||
format: "json",
|
||||
});
|
||||
expect(builtNoIdentity.at(-1)).not.toContain(CLAUDE_IDENTITY_PREFIX);
|
||||
expect(builtNoIdentity.at(-1)).toBe("hi");
|
||||
});
|
||||
|
||||
it("opencodeSpec adds format flag and identity prefix when needed", () => {
|
||||
const argv = ["opencode", "body"];
|
||||
const built = opencodeSpec.buildArgs({
|
||||
argv,
|
||||
bodyIndex: 1,
|
||||
isNewSession: true,
|
||||
sessionId: "sess",
|
||||
sendSystemOnce: false,
|
||||
systemSent: false,
|
||||
identityPrefix: undefined,
|
||||
format: "json",
|
||||
});
|
||||
expect(built).toContain("--format");
|
||||
expect(built).toContain("json");
|
||||
expect(built.at(-1)).toContain(OPENCODE_IDENTITY_PREFIX);
|
||||
});
|
||||
|
||||
it("piSpec parses final assistant message and preserves usage meta", () => {
|
||||
it("parses final assistant message and preserves usage meta", () => {
|
||||
const stdout = [
|
||||
'{"type":"message_start","message":{"role":"assistant"}}',
|
||||
'{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"hello world"}],"usage":{"input":10,"output":5},"model":"pi-1","provider":"inflection","stopReason":"end"}}',
|
||||
@@ -80,83 +57,4 @@ describe("agent buildArgs + parseOutput helpers", () => {
|
||||
expect(tool?.toolName).toBe("bash");
|
||||
expect(tool?.meta).toBe("ls -la");
|
||||
});
|
||||
|
||||
it("codexSpec parses agent_message and aggregates usage", () => {
|
||||
const stdout = [
|
||||
'{"type":"item.completed","item":{"type":"agent_message","text":"hi there"}}',
|
||||
'{"type":"turn.completed","usage":{"input_tokens":50,"output_tokens":10,"cached_input_tokens":5}}',
|
||||
].join("\n");
|
||||
const parsed = codexSpec.parseOutput(stdout);
|
||||
expect(parsed.texts?.[0]).toBe("hi there");
|
||||
const usage = parsed.meta?.usage as {
|
||||
input?: number;
|
||||
output?: number;
|
||||
cacheRead?: number;
|
||||
total?: number;
|
||||
};
|
||||
expect(usage?.input).toBe(50);
|
||||
expect(usage?.output).toBe(10);
|
||||
expect(usage?.cacheRead).toBe(5);
|
||||
expect(usage?.total).toBe(65);
|
||||
});
|
||||
|
||||
it("opencodeSpec parses streamed events and summarizes meta", () => {
|
||||
const stdout = [
|
||||
'{"type":"step_start","timestamp":0}',
|
||||
'{"type":"text","part":{"text":"hi"}}',
|
||||
'{"type":"step_finish","timestamp":1200,"part":{"cost":0.002,"tokens":{"input":100,"output":20}}}',
|
||||
].join("\n");
|
||||
const parsed = opencodeSpec.parseOutput(stdout);
|
||||
expect(parsed.texts?.[0]).toBe("hi");
|
||||
expect(parsed.meta?.extra?.summary).toContain("duration=1200ms");
|
||||
expect(parsed.meta?.extra?.summary).toContain("cost=$0.0020");
|
||||
expect(parsed.meta?.extra?.summary).toContain("tokens=100+20");
|
||||
});
|
||||
|
||||
it("codexSpec buildArgs enforces exec/json/sandbox defaults", () => {
|
||||
const argv = ["codex", "hello world"];
|
||||
const built = codexSpec.buildArgs({
|
||||
argv,
|
||||
bodyIndex: 1,
|
||||
isNewSession: true,
|
||||
sessionId: "sess",
|
||||
sendSystemOnce: false,
|
||||
systemSent: false,
|
||||
identityPrefix: undefined,
|
||||
format: "json",
|
||||
});
|
||||
expect(built[1]).toBe("exec");
|
||||
expect(built).toContain("--json");
|
||||
expect(built).toContain("--skip-git-repo-check");
|
||||
expect(built).toContain("read-only");
|
||||
});
|
||||
|
||||
it("geminiSpec prepends identity unless already sent", () => {
|
||||
const argv = ["gemini", "hi"];
|
||||
const built = geminiSpec.buildArgs({
|
||||
argv,
|
||||
bodyIndex: 1,
|
||||
isNewSession: true,
|
||||
sessionId: "sess",
|
||||
sendSystemOnce: false,
|
||||
systemSent: false,
|
||||
identityPrefix: undefined,
|
||||
format: "json",
|
||||
});
|
||||
expect(built.at(-1)).toContain(GEMINI_IDENTITY_PREFIX);
|
||||
|
||||
const builtOnce = geminiSpec.buildArgs({
|
||||
argv,
|
||||
bodyIndex: 1,
|
||||
isNewSession: false,
|
||||
sessionId: "sess",
|
||||
sendSystemOnce: true,
|
||||
systemSent: true,
|
||||
identityPrefix: undefined,
|
||||
format: "json",
|
||||
});
|
||||
expect(builtOnce.at(-1)).toBe("hi");
|
||||
expect(builtOnce).toContain("--output-format");
|
||||
expect(builtOnce).toContain("json");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
import path from "node:path";
|
||||
|
||||
import {
|
||||
CLAUDE_BIN,
|
||||
CLAUDE_IDENTITY_PREFIX,
|
||||
type ClaudeJsonParseResult,
|
||||
parseClaudeJson,
|
||||
summarizeClaudeMetadata,
|
||||
} from "../auto-reply/claude.js";
|
||||
import type { AgentMeta, AgentSpec } from "./types.js";
|
||||
|
||||
function toMeta(parsed?: ClaudeJsonParseResult): AgentMeta | undefined {
|
||||
if (!parsed?.parsed) return undefined;
|
||||
const summary = summarizeClaudeMetadata(parsed.parsed);
|
||||
const sessionId =
|
||||
parsed.parsed &&
|
||||
typeof parsed.parsed === "object" &&
|
||||
typeof (parsed.parsed as { session_id?: unknown }).session_id === "string"
|
||||
? (parsed.parsed as { session_id: string }).session_id
|
||||
: undefined;
|
||||
const meta: AgentMeta = {};
|
||||
if (sessionId) meta.sessionId = sessionId;
|
||||
if (summary) meta.extra = { summary };
|
||||
return Object.keys(meta).length ? meta : undefined;
|
||||
}
|
||||
|
||||
export const claudeSpec: AgentSpec = {
|
||||
kind: "claude",
|
||||
isInvocation: (argv) =>
|
||||
argv.length > 0 && path.basename(argv[0]) === CLAUDE_BIN,
|
||||
buildArgs: (ctx) => {
|
||||
// Split around the body so we can inject flags without losing the body
|
||||
// position. This keeps templated prompts intact even when we add flags.
|
||||
const argv = [...ctx.argv];
|
||||
const body = argv[ctx.bodyIndex] ?? "";
|
||||
const beforeBody = argv.slice(0, ctx.bodyIndex);
|
||||
const afterBody = argv.slice(ctx.bodyIndex + 1);
|
||||
|
||||
const wantsOutputFormat = typeof ctx.format === "string";
|
||||
if (wantsOutputFormat) {
|
||||
const hasOutputFormat = argv.some(
|
||||
(part) =>
|
||||
part === "--output-format" || part.startsWith("--output-format="),
|
||||
);
|
||||
if (!hasOutputFormat) {
|
||||
const outputFormat = ctx.format ?? "json";
|
||||
beforeBody.push("--output-format", outputFormat);
|
||||
}
|
||||
}
|
||||
|
||||
const hasPrintFlag = argv.some(
|
||||
(part) => part === "-p" || part === "--print",
|
||||
);
|
||||
if (!hasPrintFlag) {
|
||||
beforeBody.push("-p");
|
||||
}
|
||||
|
||||
const shouldPrependIdentity = !(ctx.sendSystemOnce && ctx.systemSent);
|
||||
const bodyWithIdentity =
|
||||
shouldPrependIdentity && body
|
||||
? [ctx.identityPrefix ?? CLAUDE_IDENTITY_PREFIX, body]
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
: body;
|
||||
|
||||
return [...beforeBody, bodyWithIdentity, ...afterBody];
|
||||
},
|
||||
parseOutput: (rawStdout) => {
|
||||
const parsed = parseClaudeJson(rawStdout);
|
||||
const text = parsed?.text ?? rawStdout.trim();
|
||||
return {
|
||||
texts: text ? [text.trim()] : undefined,
|
||||
meta: toMeta(parsed),
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -1,80 +0,0 @@
|
||||
import path from "node:path";
|
||||
|
||||
import type { AgentMeta, AgentParseResult, AgentSpec } from "./types.js";
|
||||
|
||||
function parseCodexJson(raw: string): AgentParseResult {
|
||||
const lines = raw.split(/\n+/).filter((l) => l.trim().startsWith("{"));
|
||||
const texts: string[] = [];
|
||||
let meta: AgentMeta | undefined;
|
||||
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const ev = JSON.parse(line) as {
|
||||
type?: string;
|
||||
item?: { type?: string; text?: string };
|
||||
usage?: unknown;
|
||||
};
|
||||
// Codex streams multiple events; capture the last agent_message text and
|
||||
// the final turn usage for cost/telemetry.
|
||||
if (
|
||||
ev.type === "item.completed" &&
|
||||
ev.item?.type === "agent_message" &&
|
||||
typeof ev.item.text === "string"
|
||||
) {
|
||||
texts.push(ev.item.text);
|
||||
}
|
||||
if (
|
||||
ev.type === "turn.completed" &&
|
||||
ev.usage &&
|
||||
typeof ev.usage === "object"
|
||||
) {
|
||||
const u = ev.usage as {
|
||||
input_tokens?: number;
|
||||
cached_input_tokens?: number;
|
||||
output_tokens?: number;
|
||||
};
|
||||
meta = {
|
||||
usage: {
|
||||
input: u.input_tokens,
|
||||
output: u.output_tokens,
|
||||
cacheRead: u.cached_input_tokens,
|
||||
total:
|
||||
(u.input_tokens ?? 0) +
|
||||
(u.output_tokens ?? 0) +
|
||||
(u.cached_input_tokens ?? 0),
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
const finalTexts = texts.length ? texts.map((t) => t.trim()) : undefined;
|
||||
return { texts: finalTexts, meta };
|
||||
}
|
||||
|
||||
export const codexSpec: AgentSpec = {
|
||||
kind: "codex",
|
||||
isInvocation: (argv) => argv.length > 0 && path.basename(argv[0]) === "codex",
|
||||
buildArgs: (ctx) => {
|
||||
const argv = [...ctx.argv];
|
||||
const hasExec = argv.length > 0 && argv[1] === "exec";
|
||||
if (!hasExec) {
|
||||
argv.splice(1, 0, "exec");
|
||||
}
|
||||
// Ensure JSON output
|
||||
if (!argv.includes("--json")) {
|
||||
argv.splice(argv.length - 1, 0, "--json");
|
||||
}
|
||||
// Safety defaults
|
||||
if (!argv.includes("--skip-git-repo-check")) {
|
||||
argv.splice(argv.length - 1, 0, "--skip-git-repo-check");
|
||||
}
|
||||
if (!argv.some((p) => p === "--sandbox" || p.startsWith("--sandbox="))) {
|
||||
argv.splice(argv.length - 1, 0, "--sandbox", "read-only");
|
||||
}
|
||||
return argv;
|
||||
},
|
||||
parseOutput: parseCodexJson,
|
||||
};
|
||||
@@ -1,54 +0,0 @@
|
||||
import path from "node:path";
|
||||
|
||||
import type { AgentParseResult, AgentSpec } from "./types.js";
|
||||
|
||||
const GEMINI_BIN = "gemini";
|
||||
export const GEMINI_IDENTITY_PREFIX =
|
||||
"You are Gemini responding for clawdis. Keep WhatsApp replies concise (<1500 chars). If the prompt contains media paths or a Transcript block, use them. If this was a heartbeat probe and nothing needs attention, reply with exactly HEARTBEAT_OK.";
|
||||
|
||||
// Gemini CLI currently prints plain text; --output json is flaky across versions, so we
|
||||
// keep parsing minimal and let MEDIA token stripping happen later in the pipeline.
|
||||
function parseGeminiOutput(raw: string): AgentParseResult {
|
||||
const trimmed = raw.trim();
|
||||
const text = trimmed || undefined;
|
||||
return {
|
||||
texts: text ? [text] : undefined,
|
||||
meta: undefined,
|
||||
} satisfies AgentParseResult;
|
||||
}
|
||||
|
||||
export const geminiSpec: AgentSpec = {
|
||||
kind: "gemini",
|
||||
isInvocation: (argv) =>
|
||||
argv.length > 0 && path.basename(argv[0]) === GEMINI_BIN,
|
||||
buildArgs: (ctx) => {
|
||||
const argv = [...ctx.argv];
|
||||
const body = argv[ctx.bodyIndex] ?? "";
|
||||
const beforeBody = argv.slice(0, ctx.bodyIndex);
|
||||
const afterBody = argv.slice(ctx.bodyIndex + 1);
|
||||
|
||||
if (ctx.format) {
|
||||
const hasOutput =
|
||||
beforeBody.some(
|
||||
(p) => p === "--output-format" || p.startsWith("--output-format="),
|
||||
) ||
|
||||
afterBody.some(
|
||||
(p) => p === "--output-format" || p.startsWith("--output-format="),
|
||||
);
|
||||
if (!hasOutput) {
|
||||
beforeBody.push("--output-format", ctx.format);
|
||||
}
|
||||
}
|
||||
|
||||
const shouldPrependIdentity = !(ctx.sendSystemOnce && ctx.systemSent);
|
||||
const bodyWithIdentity =
|
||||
shouldPrependIdentity && body
|
||||
? [ctx.identityPrefix ?? GEMINI_IDENTITY_PREFIX, body]
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
: body;
|
||||
|
||||
return [...beforeBody, bodyWithIdentity, ...afterBody];
|
||||
},
|
||||
parseOutput: parseGeminiOutput,
|
||||
};
|
||||
@@ -1,15 +1,7 @@
|
||||
import { claudeSpec } from "./claude.js";
|
||||
import { codexSpec } from "./codex.js";
|
||||
import { geminiSpec } from "./gemini.js";
|
||||
import { opencodeSpec } from "./opencode.js";
|
||||
import { piSpec } from "./pi.js";
|
||||
import type { AgentKind, AgentSpec } from "./types.js";
|
||||
|
||||
const specs: Record<AgentKind, AgentSpec> = {
|
||||
claude: claudeSpec,
|
||||
codex: codexSpec,
|
||||
gemini: geminiSpec,
|
||||
opencode: opencodeSpec,
|
||||
pi: piSpec,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import path from "node:path";
|
||||
|
||||
import {
|
||||
OPENCODE_BIN,
|
||||
OPENCODE_IDENTITY_PREFIX,
|
||||
parseOpencodeJson,
|
||||
summarizeOpencodeMetadata,
|
||||
} from "../auto-reply/opencode.js";
|
||||
import type { AgentMeta, AgentSpec } from "./types.js";
|
||||
|
||||
function toMeta(
|
||||
parsed: ReturnType<typeof parseOpencodeJson>,
|
||||
): AgentMeta | undefined {
|
||||
const summary = summarizeOpencodeMetadata(parsed.meta);
|
||||
return summary ? { extra: { summary } } : undefined;
|
||||
}
|
||||
|
||||
export const opencodeSpec: AgentSpec = {
|
||||
kind: "opencode",
|
||||
isInvocation: (argv) =>
|
||||
argv.length > 0 && path.basename(argv[0]) === OPENCODE_BIN,
|
||||
buildArgs: (ctx) => {
|
||||
// Split around the body so we can insert flags without losing the prompt.
|
||||
const argv = [...ctx.argv];
|
||||
const body = argv[ctx.bodyIndex] ?? "";
|
||||
const beforeBody = argv.slice(0, ctx.bodyIndex);
|
||||
const afterBody = argv.slice(ctx.bodyIndex + 1);
|
||||
const wantsJson = ctx.format === "json";
|
||||
|
||||
// Ensure format json for parsing
|
||||
if (wantsJson) {
|
||||
const hasFormat = [...beforeBody, body, ...afterBody].some(
|
||||
(part) => part === "--format" || part.startsWith("--format="),
|
||||
);
|
||||
if (!hasFormat) {
|
||||
beforeBody.push("--format", "json");
|
||||
}
|
||||
}
|
||||
|
||||
// Session args default to --session
|
||||
// Identity prefix
|
||||
// Opencode streams text tokens; we still seed an identity so the agent
|
||||
// keeps context on first turn.
|
||||
const shouldPrependIdentity = !(ctx.sendSystemOnce && ctx.systemSent);
|
||||
const bodyWithIdentity =
|
||||
shouldPrependIdentity && body
|
||||
? [ctx.identityPrefix ?? OPENCODE_IDENTITY_PREFIX, body]
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
: body;
|
||||
|
||||
return [...beforeBody, bodyWithIdentity, ...afterBody];
|
||||
},
|
||||
parseOutput: (rawStdout) => {
|
||||
const parsed = parseOpencodeJson(rawStdout);
|
||||
const text = parsed.text ?? rawStdout.trim();
|
||||
return {
|
||||
texts: text ? [text.trim()] : undefined,
|
||||
meta: toMeta(parsed),
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -145,22 +145,25 @@ export const piSpec: AgentSpec = {
|
||||
},
|
||||
buildArgs: (ctx) => {
|
||||
const argv = [...ctx.argv];
|
||||
let bodyPos = ctx.bodyIndex;
|
||||
// Non-interactive print + JSON
|
||||
if (!argv.includes("-p") && !argv.includes("--print")) {
|
||||
argv.splice(argv.length - 1, 0, "-p");
|
||||
argv.splice(bodyPos, 0, "-p");
|
||||
bodyPos += 1;
|
||||
}
|
||||
if (
|
||||
ctx.format === "json" &&
|
||||
!argv.includes("--mode") &&
|
||||
!argv.some((a) => a === "--mode")
|
||||
) {
|
||||
argv.splice(argv.length - 1, 0, "--mode", "json");
|
||||
argv.splice(bodyPos, 0, "--mode", "json");
|
||||
bodyPos += 2;
|
||||
}
|
||||
// Session defaults
|
||||
// Identity prefix optional; Pi usually doesn't need it, but allow injection
|
||||
if (!(ctx.sendSystemOnce && ctx.systemSent) && argv[ctx.bodyIndex]) {
|
||||
const existingBody = argv[ctx.bodyIndex];
|
||||
argv[ctx.bodyIndex] = [ctx.identityPrefix, existingBody]
|
||||
if (!(ctx.sendSystemOnce && ctx.systemSent) && argv[bodyPos]) {
|
||||
const existingBody = argv[bodyPos];
|
||||
argv[bodyPos] = [ctx.identityPrefix, existingBody]
|
||||
.filter(Boolean)
|
||||
.join("\n\n");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export type AgentKind = "claude" | "opencode" | "pi" | "codex" | "gemini";
|
||||
export type AgentKind = "pi";
|
||||
|
||||
export type AgentMeta = {
|
||||
model?: string;
|
||||
|
||||
Reference in New Issue
Block a user