perf(pi): reuse tau rpc for command auto-replies
This commit is contained in:
@@ -4,6 +4,7 @@ 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";
|
||||
|
||||
@@ -115,4 +116,33 @@ describe("agent buildArgs + parseOutput helpers", () => {
|
||||
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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,7 +12,16 @@ import type { AgentMeta, AgentSpec } from "./types.js";
|
||||
function toMeta(parsed?: ClaudeJsonParseResult): AgentMeta | undefined {
|
||||
if (!parsed?.parsed) return undefined;
|
||||
const summary = summarizeClaudeMetadata(parsed.parsed);
|
||||
return summary ? { extra: { summary } } : undefined;
|
||||
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 = {
|
||||
|
||||
50
src/agents/gemini.ts
Normal file
50
src/agents/gemini.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import path from "node:path";
|
||||
|
||||
import type { AgentMeta, AgentSpec } from "./types.js";
|
||||
|
||||
const GEMINI_BIN = "gemini";
|
||||
export const GEMINI_IDENTITY_PREFIX =
|
||||
"You are Gemini responding for warelay. 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): { text?: string; meta?: AgentMeta } {
|
||||
const trimmed = raw.trim();
|
||||
return { text: trimmed || undefined, meta: undefined };
|
||||
}
|
||||
|
||||
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,5 +1,6 @@
|
||||
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";
|
||||
@@ -7,6 +8,7 @@ import type { AgentKind, AgentSpec } from "./types.js";
|
||||
const specs: Record<AgentKind, AgentSpec> = {
|
||||
claude: claudeSpec,
|
||||
codex: codexSpec,
|
||||
gemini: geminiSpec,
|
||||
opencode: opencodeSpec,
|
||||
pi: piSpec,
|
||||
};
|
||||
|
||||
@@ -47,7 +47,11 @@ function parsePiJson(raw: string): AgentParseResult {
|
||||
|
||||
export const piSpec: AgentSpec = {
|
||||
kind: "pi",
|
||||
isInvocation: (argv) => argv.length > 0 && path.basename(argv[0]) === "pi",
|
||||
isInvocation: (argv) => {
|
||||
if (argv.length === 0) return false;
|
||||
const base = path.basename(argv[0]).replace(/\.(m?js)$/i, "");
|
||||
return base === "pi" || base === "tau";
|
||||
},
|
||||
buildArgs: (ctx) => {
|
||||
const argv = [...ctx.argv];
|
||||
// Non-interactive print + JSON
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
export type AgentKind = "claude" | "opencode" | "pi" | "codex";
|
||||
export type AgentKind = "claude" | "opencode" | "pi" | "codex" | "gemini";
|
||||
|
||||
export type AgentMeta = {
|
||||
model?: string;
|
||||
provider?: string;
|
||||
stopReason?: string;
|
||||
sessionId?: string;
|
||||
usage?: {
|
||||
input?: number;
|
||||
output?: number;
|
||||
|
||||
Reference in New Issue
Block a user