feat: embed pi agent runtime
This commit is contained in:
@@ -10,7 +10,12 @@ import {
|
||||
type MockInstance,
|
||||
vi,
|
||||
} from "vitest";
|
||||
import * as commandReply from "../auto-reply/command-reply.js";
|
||||
|
||||
vi.mock("../agents/pi-embedded.js", () => ({
|
||||
runEmbeddedPiAgent: vi.fn(),
|
||||
}));
|
||||
|
||||
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";
|
||||
import type { ClawdisConfig } from "../config/config.js";
|
||||
import * as configModule from "../config/config.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
@@ -24,151 +29,153 @@ const runtime: RuntimeEnv = {
|
||||
}),
|
||||
};
|
||||
|
||||
const runReplySpy = vi.spyOn(commandReply, "runCommandReply");
|
||||
const configSpy = vi.spyOn(configModule, "loadConfig");
|
||||
|
||||
function makeStorePath() {
|
||||
return path.join(
|
||||
os.tmpdir(),
|
||||
`clawdis-agent-test-${Date.now()}-${Math.random()}.json`,
|
||||
);
|
||||
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
|
||||
const base = fs.mkdtempSync(path.join(os.tmpdir(), "clawdis-agent-"));
|
||||
const previousHome = process.env.HOME;
|
||||
process.env.HOME = base;
|
||||
try {
|
||||
return await fn(base);
|
||||
} finally {
|
||||
process.env.HOME = previousHome;
|
||||
fs.rmSync(base, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function mockConfig(
|
||||
home: string,
|
||||
storePath: string,
|
||||
replyOverrides?: Partial<NonNullable<ClawdisConfig["inbound"]>["reply"]>,
|
||||
inboundOverrides?: Partial<NonNullable<ClawdisConfig["inbound"]>>,
|
||||
) {
|
||||
configSpy.mockReturnValue({
|
||||
inbound: {
|
||||
reply: {
|
||||
mode: "command",
|
||||
command: ["echo", "{{Body}}"],
|
||||
session: {
|
||||
store: storePath,
|
||||
sendSystemOnce: false,
|
||||
},
|
||||
...replyOverrides,
|
||||
},
|
||||
workspace: path.join(home, "clawd"),
|
||||
agent: { provider: "anthropic", model: "claude-opus-4-5" },
|
||||
session: { store: storePath, mainKey: "main" },
|
||||
...inboundOverrides,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
runReplySpy.mockResolvedValue({
|
||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
||||
payloads: [{ text: "ok" }],
|
||||
meta: { durationMs: 5 },
|
||||
meta: {
|
||||
durationMs: 5,
|
||||
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
describe("agentCommand", () => {
|
||||
it("creates a session entry when deriving from --to", async () => {
|
||||
const store = makeStorePath();
|
||||
mockConfig(store);
|
||||
await withTempHome(async (home) => {
|
||||
const store = path.join(home, "sessions.json");
|
||||
mockConfig(home, store);
|
||||
|
||||
await agentCommand({ message: "hello", to: "+1555" }, runtime);
|
||||
await agentCommand({ message: "hello", to: "+1555" }, runtime);
|
||||
|
||||
const saved = JSON.parse(fs.readFileSync(store, "utf-8")) as Record<
|
||||
string,
|
||||
{ sessionId: string }
|
||||
>;
|
||||
const entry = Object.values(saved)[0];
|
||||
expect(entry.sessionId).toBeTruthy();
|
||||
const saved = JSON.parse(fs.readFileSync(store, "utf-8")) as Record<
|
||||
string,
|
||||
{ sessionId: string }
|
||||
>;
|
||||
const entry = Object.values(saved)[0];
|
||||
expect(entry.sessionId).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
it("persists thinking and verbose overrides", async () => {
|
||||
const store = makeStorePath();
|
||||
mockConfig(store);
|
||||
await withTempHome(async (home) => {
|
||||
const store = path.join(home, "sessions.json");
|
||||
mockConfig(home, store);
|
||||
|
||||
await agentCommand(
|
||||
{ message: "hi", to: "+1222", thinking: "high", verbose: "on" },
|
||||
runtime,
|
||||
);
|
||||
await agentCommand(
|
||||
{ message: "hi", to: "+1222", thinking: "high", verbose: "on" },
|
||||
runtime,
|
||||
);
|
||||
|
||||
const saved = JSON.parse(fs.readFileSync(store, "utf-8")) as Record<
|
||||
string,
|
||||
{ thinkingLevel?: string; verboseLevel?: string }
|
||||
>;
|
||||
const entry = Object.values(saved)[0];
|
||||
expect(entry.thinkingLevel).toBe("high");
|
||||
expect(entry.verboseLevel).toBe("on");
|
||||
const saved = JSON.parse(fs.readFileSync(store, "utf-8")) as Record<
|
||||
string,
|
||||
{ thinkingLevel?: string; verboseLevel?: string }
|
||||
>;
|
||||
const entry = Object.values(saved)[0];
|
||||
expect(entry.thinkingLevel).toBe("high");
|
||||
expect(entry.verboseLevel).toBe("on");
|
||||
|
||||
const callArgs = runReplySpy.mock.calls.at(-1)?.[0];
|
||||
expect(callArgs?.thinkLevel).toBe("high");
|
||||
expect(callArgs?.verboseLevel).toBe("on");
|
||||
const callArgs = vi.mocked(runEmbeddedPiAgent).mock.calls.at(-1)?.[0];
|
||||
expect(callArgs?.thinkLevel).toBe("high");
|
||||
expect(callArgs?.verboseLevel).toBe("on");
|
||||
});
|
||||
});
|
||||
|
||||
it("resumes when session-id is provided", async () => {
|
||||
const store = makeStorePath();
|
||||
fs.mkdirSync(path.dirname(store), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
store,
|
||||
JSON.stringify(
|
||||
{
|
||||
foo: {
|
||||
sessionId: "session-123",
|
||||
updatedAt: Date.now(),
|
||||
systemSent: true,
|
||||
await withTempHome(async (home) => {
|
||||
const store = path.join(home, "sessions.json");
|
||||
fs.mkdirSync(path.dirname(store), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
store,
|
||||
JSON.stringify(
|
||||
{
|
||||
foo: {
|
||||
sessionId: "session-123",
|
||||
updatedAt: Date.now(),
|
||||
systemSent: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
mockConfig(store);
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
mockConfig(home, store);
|
||||
|
||||
await agentCommand(
|
||||
{ message: "resume me", sessionId: "session-123" },
|
||||
runtime,
|
||||
);
|
||||
await agentCommand(
|
||||
{ message: "resume me", sessionId: "session-123" },
|
||||
runtime,
|
||||
);
|
||||
|
||||
const callArgs = runReplySpy.mock.calls.at(-1)?.[0];
|
||||
expect(callArgs?.isNewSession).toBe(false);
|
||||
expect(callArgs?.templatingCtx.SessionId).toBe("session-123");
|
||||
const callArgs = vi.mocked(runEmbeddedPiAgent).mock.calls.at(-1)?.[0];
|
||||
expect(callArgs?.sessionId).toBe("session-123");
|
||||
});
|
||||
});
|
||||
|
||||
it("prints JSON payload when requested", async () => {
|
||||
runReplySpy.mockResolvedValue({
|
||||
payloads: [{ text: "json-reply", mediaUrl: "http://x.test/a.jpg" }],
|
||||
meta: { durationMs: 42 },
|
||||
await withTempHome(async (home) => {
|
||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
||||
payloads: [{ text: "json-reply", mediaUrl: "http://x.test/a.jpg" }],
|
||||
meta: {
|
||||
durationMs: 42,
|
||||
agentMeta: { sessionId: "s", provider: "p", model: "m" },
|
||||
},
|
||||
});
|
||||
const store = path.join(home, "sessions.json");
|
||||
mockConfig(home, store);
|
||||
|
||||
await agentCommand({ message: "hi", to: "+1999", json: true }, runtime);
|
||||
|
||||
const logged = (runtime.log as MockInstance).mock.calls.at(
|
||||
-1,
|
||||
)?.[0] as string;
|
||||
const parsed = JSON.parse(logged) as {
|
||||
payloads: Array<{ text: string; mediaUrl?: string | null }>;
|
||||
meta: { durationMs: number };
|
||||
};
|
||||
expect(parsed.payloads[0].text).toBe("json-reply");
|
||||
expect(parsed.payloads[0].mediaUrl).toBe("http://x.test/a.jpg");
|
||||
expect(parsed.meta.durationMs).toBe(42);
|
||||
});
|
||||
const store = makeStorePath();
|
||||
mockConfig(store);
|
||||
|
||||
await agentCommand({ message: "hi", to: "+1999", json: true }, runtime);
|
||||
|
||||
const logged = (runtime.log as MockInstance).mock.calls.at(
|
||||
-1,
|
||||
)?.[0] as string;
|
||||
const parsed = JSON.parse(logged) as {
|
||||
payloads: Array<{ text: string; mediaUrl?: string }>;
|
||||
meta: { durationMs: number };
|
||||
};
|
||||
expect(parsed.payloads[0].text).toBe("json-reply");
|
||||
expect(parsed.payloads[0].mediaUrl).toBe("http://x.test/a.jpg");
|
||||
expect(parsed.meta.durationMs).toBe(42);
|
||||
});
|
||||
|
||||
it("builds command body without WhatsApp wrappers", async () => {
|
||||
const store = makeStorePath();
|
||||
mockConfig(store, {
|
||||
mode: "command",
|
||||
command: ["echo", "{{Body}}"],
|
||||
session: {
|
||||
store,
|
||||
sendSystemOnce: false,
|
||||
sessionIntro: "Intro {{SessionId}}",
|
||||
},
|
||||
bodyPrefix: "[pfx] ",
|
||||
it("passes the message through as the agent prompt", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
const store = path.join(home, "sessions.json");
|
||||
mockConfig(home, store);
|
||||
|
||||
await agentCommand({ message: "ping", to: "+1333" }, runtime);
|
||||
|
||||
const callArgs = vi.mocked(runEmbeddedPiAgent).mock.calls.at(-1)?.[0];
|
||||
expect(callArgs?.prompt).toBe("ping");
|
||||
});
|
||||
|
||||
await agentCommand({ message: "ping", to: "+1333" }, runtime);
|
||||
|
||||
const callArgs = runReplySpy.mock.calls.at(-1)?.[0];
|
||||
const body = callArgs?.templatingCtx.Body as string;
|
||||
expect(body.startsWith("Intro")).toBe(true);
|
||||
expect(body).toContain("[pfx] ping");
|
||||
expect(body).not.toContain("WhatsApp");
|
||||
expect(body).not.toContain("MEDIA:");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user