fix(auto-reply): prefer Pi RPC by default
parent
79818f73c0
commit
0780859a4d
|
|
@ -611,7 +611,9 @@ export async function runCommandReply(
|
||||||
streamedAny = true;
|
streamedAny = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const preferRpc = process.env.CLAWDIS_USE_PI_RPC === "1";
|
// Default to RPC (it is testable/offline and avoids spawning long-lived CLI processes).
|
||||||
|
// Set `CLAWDIS_USE_PI_RPC=0` to force the JSON fallback path.
|
||||||
|
const preferRpc = process.env.CLAWDIS_USE_PI_RPC !== "0";
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
const runId = params.runId ?? crypto.randomUUID();
|
const runId = params.runId ?? crypto.randomUUID();
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ describe("directive parsing", () => {
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
inbound: {
|
inbound: {
|
||||||
|
allowFrom: ["*"],
|
||||||
reply: {
|
reply: {
|
||||||
mode: "command",
|
mode: "command",
|
||||||
command: ["pi", "{{Body}}"],
|
command: ["pi", "{{Body}}"],
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ vi.mock("../web/session.js", () => webMocks);
|
||||||
|
|
||||||
const baseCfg = {
|
const baseCfg = {
|
||||||
inbound: {
|
inbound: {
|
||||||
|
allowFrom: ["*"],
|
||||||
reply: {
|
reply: {
|
||||||
mode: "command" as const,
|
mode: "command" as const,
|
||||||
command: ["echo", "{{Body}}"],
|
command: ["echo", "{{Body}}"],
|
||||||
|
|
@ -88,6 +89,7 @@ describe("trigger handling", () => {
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
inbound: {
|
inbound: {
|
||||||
|
allowFrom: ["*"],
|
||||||
reply: {
|
reply: {
|
||||||
mode: "command",
|
mode: "command",
|
||||||
command: ["echo", "{{Body}}"],
|
command: ["echo", "{{Body}}"],
|
||||||
|
|
@ -104,6 +106,8 @@ describe("trigger handling", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("ignores think directives that only appear in the context wrapper", async () => {
|
it("ignores think directives that only appear in the context wrapper", async () => {
|
||||||
|
const prevPreferRpc = process.env.CLAWDIS_USE_PI_RPC;
|
||||||
|
process.env.CLAWDIS_USE_PI_RPC = "1";
|
||||||
const rpcMock = vi.spyOn(tauRpc, "runPiRpc").mockResolvedValue({
|
const rpcMock = vi.spyOn(tauRpc, "runPiRpc").mockResolvedValue({
|
||||||
stdout:
|
stdout:
|
||||||
'{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"ok"}]}}',
|
'{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"ok"}]}}',
|
||||||
|
|
@ -135,9 +139,17 @@ describe("trigger handling", () => {
|
||||||
const prompt = rpcMock.mock.calls[0]?.[0]?.prompt ?? "";
|
const prompt = rpcMock.mock.calls[0]?.[0]?.prompt ?? "";
|
||||||
expect(prompt).toContain("Give me the status");
|
expect(prompt).toContain("Give me the status");
|
||||||
expect(prompt).not.toContain("/thinking high");
|
expect(prompt).not.toContain("/thinking high");
|
||||||
|
|
||||||
|
if (prevPreferRpc === undefined) {
|
||||||
|
delete process.env.CLAWDIS_USE_PI_RPC;
|
||||||
|
} else {
|
||||||
|
process.env.CLAWDIS_USE_PI_RPC = prevPreferRpc;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not emit directive acks for heartbeats with /think", async () => {
|
it("does not emit directive acks for heartbeats with /think", async () => {
|
||||||
|
const prevPreferRpc = process.env.CLAWDIS_USE_PI_RPC;
|
||||||
|
process.env.CLAWDIS_USE_PI_RPC = "1";
|
||||||
const rpcMock = vi.spyOn(tauRpc, "runPiRpc").mockResolvedValue({
|
const rpcMock = vi.spyOn(tauRpc, "runPiRpc").mockResolvedValue({
|
||||||
stdout:
|
stdout:
|
||||||
'{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"ok"}]}}',
|
'{"type":"message_end","message":{"role":"assistant","content":[{"type":"text","text":"ok"}]}}',
|
||||||
|
|
@ -170,5 +182,11 @@ describe("trigger handling", () => {
|
||||||
expect(text).toBe("ok");
|
expect(text).toBe("ok");
|
||||||
expect(text).not.toMatch(/Thinking level set/i);
|
expect(text).not.toMatch(/Thinking level set/i);
|
||||||
expect(rpcMock).toHaveBeenCalledOnce();
|
expect(rpcMock).toHaveBeenCalledOnce();
|
||||||
|
|
||||||
|
if (prevPreferRpc === undefined) {
|
||||||
|
delete process.env.CLAWDIS_USE_PI_RPC;
|
||||||
|
} else {
|
||||||
|
process.env.CLAWDIS_USE_PI_RPC = prevPreferRpc;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ import {
|
||||||
listPortListeners,
|
listPortListeners,
|
||||||
type PortProcess,
|
type PortProcess,
|
||||||
parseLsofOutput,
|
parseLsofOutput,
|
||||||
} from "./program.js";
|
} from "./ports.js";
|
||||||
|
|
||||||
describe("gateway --force helpers", () => {
|
describe("gateway --force helpers", () => {
|
||||||
let originalKill: typeof process.kill;
|
let originalKill: typeof process.kill;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import type { HealthSummary } from "./health.js";
|
||||||
import { healthCommand } from "./health.js";
|
import { healthCommand } from "./health.js";
|
||||||
|
|
||||||
const runtime = {
|
const runtime = {
|
||||||
|
|
@ -8,110 +9,58 @@ const runtime = {
|
||||||
exit: vi.fn(),
|
exit: vi.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
vi.mock("../config/config.js", () => ({
|
const callGatewayMock = vi.fn();
|
||||||
loadConfig: () => ({ web: {}, inbound: {} }),
|
vi.mock("../gateway/call.js", () => ({
|
||||||
}));
|
callGateway: (...args: unknown[]) => callGatewayMock(...args),
|
||||||
|
|
||||||
vi.mock("../config/sessions.js", () => ({
|
|
||||||
resolveStorePath: vi.fn(() => "/tmp/sessions.json"),
|
|
||||||
loadSessionStore: vi.fn(() => ({
|
|
||||||
"+1555": { updatedAt: Date.now() - 60_000 },
|
|
||||||
})),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const waitForWaConnection = vi.fn();
|
|
||||||
const webAuthExists = vi.fn();
|
|
||||||
const fetchMock = vi.fn();
|
|
||||||
|
|
||||||
vi.stubGlobal("fetch", fetchMock);
|
|
||||||
|
|
||||||
vi.mock("../web/session.js", () => ({
|
|
||||||
createWaSocket: vi.fn(async () => ({
|
|
||||||
ws: { close: vi.fn() },
|
|
||||||
ev: { on: vi.fn() },
|
|
||||||
})),
|
|
||||||
waitForWaConnection: (...args: unknown[]) => waitForWaConnection(...args),
|
|
||||||
webAuthExists: (...args: unknown[]) => webAuthExists(...args),
|
|
||||||
getStatusCode: vi.fn(() => 440),
|
|
||||||
getWebAuthAgeMs: () => 5000,
|
|
||||||
logWebSelfId: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../web/reconnect.js", () => ({
|
|
||||||
resolveHeartbeatSeconds: () => 60,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe("healthCommand", () => {
|
describe("healthCommand", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
fetchMock.mockReset();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("outputs JSON when linked and connect succeeds", async () => {
|
it("outputs JSON from gateway", async () => {
|
||||||
webAuthExists.mockResolvedValue(true);
|
const snapshot: HealthSummary = {
|
||||||
waitForWaConnection.mockResolvedValue(undefined);
|
ts: Date.now(),
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "123:abc";
|
durationMs: 5,
|
||||||
fetchMock
|
web: {
|
||||||
.mockResolvedValueOnce({
|
linked: true,
|
||||||
ok: true,
|
authAgeMs: 5000,
|
||||||
status: 200,
|
connect: { ok: true, elapsedMs: 10 },
|
||||||
json: async () => ({ ok: true, result: { id: 1, username: "bot" } }),
|
},
|
||||||
})
|
telegram: { configured: true, probe: { ok: true, elapsedMs: 1 } },
|
||||||
.mockResolvedValueOnce({
|
heartbeatSeconds: 60,
|
||||||
ok: true,
|
sessions: {
|
||||||
status: 200,
|
path: "/tmp/sessions.json",
|
||||||
json: async () => ({ ok: true, result: { url: "https://hook" } }),
|
count: 1,
|
||||||
});
|
recent: [{ key: "+1555", updatedAt: Date.now(), age: 0 }],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
callGatewayMock.mockResolvedValueOnce(snapshot);
|
||||||
|
|
||||||
await healthCommand({ json: true, timeoutMs: 5000 }, runtime as never);
|
await healthCommand({ json: true, timeoutMs: 5000 }, runtime as never);
|
||||||
|
|
||||||
expect(runtime.exit).not.toHaveBeenCalled();
|
expect(runtime.exit).not.toHaveBeenCalled();
|
||||||
const logged = runtime.log.mock.calls[0][0] as string;
|
const logged = runtime.log.mock.calls[0]?.[0] as string;
|
||||||
const parsed = JSON.parse(logged);
|
const parsed = JSON.parse(logged) as HealthSummary;
|
||||||
expect(parsed.web.linked).toBe(true);
|
expect(parsed.web.linked).toBe(true);
|
||||||
expect(parsed.web.connect.ok).toBe(true);
|
|
||||||
expect(parsed.telegram.configured).toBe(true);
|
expect(parsed.telegram.configured).toBe(true);
|
||||||
expect(parsed.telegram.probe.ok).toBe(true);
|
|
||||||
expect(parsed.sessions.count).toBe(1);
|
expect(parsed.sessions.count).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("exits non-zero when not linked", async () => {
|
it("prints text summary when not json", async () => {
|
||||||
webAuthExists.mockResolvedValue(false);
|
callGatewayMock.mockResolvedValueOnce({
|
||||||
await healthCommand({ json: true }, runtime as never);
|
ts: Date.now(),
|
||||||
expect(runtime.exit).toHaveBeenCalledWith(1);
|
durationMs: 5,
|
||||||
});
|
web: { linked: false, authAgeMs: null },
|
||||||
|
telegram: { configured: false },
|
||||||
|
heartbeatSeconds: 60,
|
||||||
|
sessions: { path: "/tmp/sessions.json", count: 0, recent: [] },
|
||||||
|
} satisfies HealthSummary);
|
||||||
|
|
||||||
it("exits non-zero when connect fails", async () => {
|
await healthCommand({ json: false }, runtime as never);
|
||||||
webAuthExists.mockResolvedValue(true);
|
|
||||||
waitForWaConnection.mockRejectedValueOnce({ output: { statusCode: 440 } });
|
|
||||||
|
|
||||||
await healthCommand({ json: true }, runtime as never);
|
expect(runtime.exit).not.toHaveBeenCalled();
|
||||||
|
expect(runtime.log).toHaveBeenCalled();
|
||||||
expect(runtime.exit).toHaveBeenCalledWith(1);
|
|
||||||
const logged = runtime.log.mock.calls[0][0] as string;
|
|
||||||
const parsed = JSON.parse(logged);
|
|
||||||
expect(parsed.web.connect.ok).toBe(false);
|
|
||||||
expect(parsed.web.connect.status).toBe(440);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("exits non-zero when telegram probe fails", async () => {
|
|
||||||
webAuthExists.mockResolvedValue(true);
|
|
||||||
waitForWaConnection.mockResolvedValue(undefined);
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "123:abc";
|
|
||||||
fetchMock.mockResolvedValue({
|
|
||||||
ok: false,
|
|
||||||
status: 401,
|
|
||||||
json: async () => ({ ok: false, description: "unauthorized" }),
|
|
||||||
});
|
|
||||||
|
|
||||||
await healthCommand({ json: true }, runtime as never);
|
|
||||||
|
|
||||||
expect(runtime.exit).toHaveBeenCalledWith(1);
|
|
||||||
const logged = runtime.log.mock.calls[0][0] as string;
|
|
||||||
const parsed = JSON.parse(logged);
|
|
||||||
expect(parsed.telegram.configured).toBe(true);
|
|
||||||
expect(parsed.telegram.probe.ok).toBe(false);
|
|
||||||
expect(parsed.telegram.probe.status).toBe(401);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,7 @@
|
||||||
import type { AnyMessageContent } from "@whiskeysockets/baileys";
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||||
|
import { setActiveWebListener } from "./active-listener.js";
|
||||||
vi.mock("./session.js", () => {
|
|
||||||
const { EventEmitter } = require("node:events");
|
|
||||||
const ev = new EventEmitter();
|
|
||||||
const sock = {
|
|
||||||
ev,
|
|
||||||
ws: { close: vi.fn() },
|
|
||||||
sendPresenceUpdate: vi.fn().mockResolvedValue(undefined),
|
|
||||||
sendMessage: vi.fn().mockResolvedValue({ key: { id: "msg123" } }),
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
createWaSocket: vi.fn().mockResolvedValue(sock),
|
|
||||||
waitForWaConnection: vi.fn().mockResolvedValue(undefined),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const loadWebMediaMock = vi.fn();
|
const loadWebMediaMock = vi.fn();
|
||||||
vi.mock("./media.js", () => ({
|
vi.mock("./media.js", () => ({
|
||||||
|
|
@ -25,23 +10,34 @@ vi.mock("./media.js", () => ({
|
||||||
|
|
||||||
import { sendMessageWhatsApp } from "./outbound.js";
|
import { sendMessageWhatsApp } from "./outbound.js";
|
||||||
|
|
||||||
const { createWaSocket } = await import("./session.js");
|
|
||||||
|
|
||||||
describe("web outbound", () => {
|
describe("web outbound", () => {
|
||||||
|
const sendComposingTo = vi.fn(async () => {});
|
||||||
|
const sendMessage = vi.fn(async () => ({ messageId: "msg123" }));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
setActiveWebListener({ sendComposingTo, sendMessage });
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
resetLogger();
|
resetLogger();
|
||||||
setLoggerOverride(null);
|
setLoggerOverride(null);
|
||||||
|
setActiveWebListener(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sends message via web and closes socket", async () => {
|
it("sends message via active listener", async () => {
|
||||||
await sendMessageWhatsApp("+1555", "hi", { verbose: false });
|
const result = await sendMessageWhatsApp("+1555", "hi", { verbose: false });
|
||||||
const sock = await createWaSocket();
|
expect(result).toEqual({
|
||||||
expect(sock.sendMessage).toHaveBeenCalled();
|
messageId: "msg123",
|
||||||
expect(sock.ws.close).toHaveBeenCalled();
|
toJid: "1555@s.whatsapp.net",
|
||||||
|
});
|
||||||
|
expect(sendComposingTo).toHaveBeenCalledWith("+1555");
|
||||||
|
expect(sendMessage).toHaveBeenCalledWith(
|
||||||
|
"+1555",
|
||||||
|
"hi",
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("maps audio to PTT with opus mime when ogg", async () => {
|
it("maps audio to PTT with opus mime when ogg", async () => {
|
||||||
|
|
@ -55,16 +51,12 @@ describe("web outbound", () => {
|
||||||
verbose: false,
|
verbose: false,
|
||||||
mediaUrl: "/tmp/voice.ogg",
|
mediaUrl: "/tmp/voice.ogg",
|
||||||
});
|
});
|
||||||
const sock = await createWaSocket();
|
expect(sendMessage).toHaveBeenLastCalledWith(
|
||||||
const [, payload] = sock.sendMessage.mock.calls.at(-1) as [
|
"+1555",
|
||||||
string,
|
"voice note",
|
||||||
AnyMessageContent,
|
buf,
|
||||||
];
|
"audio/ogg; codecs=opus",
|
||||||
expect(payload).toMatchObject({
|
);
|
||||||
audio: buf,
|
|
||||||
ptt: true,
|
|
||||||
mimetype: "audio/ogg; codecs=opus",
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("maps video with caption", async () => {
|
it("maps video with caption", async () => {
|
||||||
|
|
@ -78,16 +70,12 @@ describe("web outbound", () => {
|
||||||
verbose: false,
|
verbose: false,
|
||||||
mediaUrl: "/tmp/video.mp4",
|
mediaUrl: "/tmp/video.mp4",
|
||||||
});
|
});
|
||||||
const sock = await createWaSocket();
|
expect(sendMessage).toHaveBeenLastCalledWith(
|
||||||
const [, payload] = sock.sendMessage.mock.calls.at(-1) as [
|
"+1555",
|
||||||
string,
|
"clip",
|
||||||
AnyMessageContent,
|
buf,
|
||||||
];
|
"video/mp4",
|
||||||
expect(payload).toMatchObject({
|
);
|
||||||
video: buf,
|
|
||||||
caption: "clip",
|
|
||||||
mimetype: "video/mp4",
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("maps image with caption", async () => {
|
it("maps image with caption", async () => {
|
||||||
|
|
@ -101,16 +89,12 @@ describe("web outbound", () => {
|
||||||
verbose: false,
|
verbose: false,
|
||||||
mediaUrl: "/tmp/pic.jpg",
|
mediaUrl: "/tmp/pic.jpg",
|
||||||
});
|
});
|
||||||
const sock = await createWaSocket();
|
expect(sendMessage).toHaveBeenLastCalledWith(
|
||||||
const [, payload] = sock.sendMessage.mock.calls.at(-1) as [
|
"+1555",
|
||||||
string,
|
"pic",
|
||||||
AnyMessageContent,
|
buf,
|
||||||
];
|
"image/jpeg",
|
||||||
expect(payload).toMatchObject({
|
);
|
||||||
image: buf,
|
|
||||||
caption: "pic",
|
|
||||||
mimetype: "image/jpeg",
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("maps other kinds to document with filename", async () => {
|
it("maps other kinds to document with filename", async () => {
|
||||||
|
|
@ -125,16 +109,11 @@ describe("web outbound", () => {
|
||||||
verbose: false,
|
verbose: false,
|
||||||
mediaUrl: "/tmp/file.pdf",
|
mediaUrl: "/tmp/file.pdf",
|
||||||
});
|
});
|
||||||
const sock = await createWaSocket();
|
expect(sendMessage).toHaveBeenLastCalledWith(
|
||||||
const [, payload] = sock.sendMessage.mock.calls.at(-1) as [
|
"+1555",
|
||||||
string,
|
"doc",
|
||||||
AnyMessageContent,
|
buf,
|
||||||
];
|
"application/pdf",
|
||||||
expect(payload).toMatchObject({
|
);
|
||||||
document: buf,
|
|
||||||
fileName: "file.pdf",
|
|
||||||
caption: "doc",
|
|
||||||
mimetype: "application/pdf",
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue