mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:41:22 +00:00
gateway: drop ipc and simplify cli
This commit is contained in:
@@ -54,7 +54,6 @@ export type HealthSummary = {
|
||||
age: number | null;
|
||||
}>;
|
||||
};
|
||||
ipc: { path: string; exists: boolean };
|
||||
};
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 10_000;
|
||||
@@ -209,9 +208,6 @@ export async function getHealthSnapshot(
|
||||
age: s.updatedAt ? Date.now() - s.updatedAt : null,
|
||||
}));
|
||||
|
||||
const ipcPath = path.join(process.env.HOME ?? "", ".clawdis", "clawdis.sock");
|
||||
const ipcExists = Boolean(ipcPath) && fs.existsSync(ipcPath);
|
||||
|
||||
const start = Date.now();
|
||||
const cappedTimeout = Math.max(1000, timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
||||
const connect = linked ? await probeWebConnect(cappedTimeout) : undefined;
|
||||
@@ -235,7 +231,6 @@ export async function getHealthSnapshot(
|
||||
count: sessions.length,
|
||||
recent,
|
||||
},
|
||||
ipc: { path: ipcPath, exists: ipcExists },
|
||||
};
|
||||
|
||||
return summary;
|
||||
@@ -300,11 +295,6 @@ export async function healthCommand(
|
||||
);
|
||||
}
|
||||
}
|
||||
runtime.log(
|
||||
info(
|
||||
`IPC socket: ${summary.ipc.exists ? "present" : "missing"} (${summary.ipc.path})`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (fatal) {
|
||||
|
||||
@@ -4,9 +4,10 @@ import type { CliDeps } from "../cli/deps.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { sendCommand } from "./send.js";
|
||||
|
||||
const sendViaIpcMock = vi.fn().mockResolvedValue(null);
|
||||
vi.mock("../web/ipc.js", () => ({
|
||||
sendViaIpc: (...args: unknown[]) => sendViaIpcMock(...args),
|
||||
const callGatewayMock = vi.fn();
|
||||
vi.mock("../gateway/call.js", () => ({
|
||||
callGateway: (...args: unknown[]) => callGatewayMock(...args),
|
||||
randomIdempotencyKey: () => "idem-1",
|
||||
}));
|
||||
|
||||
const originalTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
@@ -48,8 +49,8 @@ describe("sendCommand", () => {
|
||||
expect(deps.sendMessageWhatsApp).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses IPC when available", async () => {
|
||||
sendViaIpcMock.mockResolvedValueOnce({ success: true, messageId: "ipc1" });
|
||||
it("sends via gateway", async () => {
|
||||
callGatewayMock.mockResolvedValueOnce({ messageId: "g1" });
|
||||
const deps = makeDeps();
|
||||
await sendCommand(
|
||||
{
|
||||
@@ -59,25 +60,8 @@ describe("sendCommand", () => {
|
||||
deps,
|
||||
runtime,
|
||||
);
|
||||
expect(deps.sendMessageWhatsApp).not.toHaveBeenCalled();
|
||||
expect(runtime.log).toHaveBeenCalledWith(expect.stringContaining("ipc1"));
|
||||
});
|
||||
|
||||
it("falls back to direct send when IPC fails", async () => {
|
||||
sendViaIpcMock.mockResolvedValueOnce({ success: false, error: "nope" });
|
||||
const deps = makeDeps({
|
||||
sendMessageWhatsApp: vi.fn().mockResolvedValue({ messageId: "direct1" }),
|
||||
});
|
||||
await sendCommand(
|
||||
{
|
||||
to: "+1",
|
||||
message: "hi",
|
||||
media: "pic.jpg",
|
||||
},
|
||||
deps,
|
||||
runtime,
|
||||
);
|
||||
expect(deps.sendMessageWhatsApp).toHaveBeenCalled();
|
||||
expect(callGatewayMock).toHaveBeenCalled();
|
||||
expect(runtime.log).toHaveBeenCalledWith(expect.stringContaining("g1"));
|
||||
});
|
||||
|
||||
it("routes to telegram provider", async () => {
|
||||
@@ -100,10 +84,8 @@ describe("sendCommand", () => {
|
||||
});
|
||||
|
||||
it("emits json output", async () => {
|
||||
sendViaIpcMock.mockResolvedValueOnce(null);
|
||||
const deps = makeDeps({
|
||||
sendMessageWhatsApp: vi.fn().mockResolvedValue({ messageId: "direct2" }),
|
||||
});
|
||||
callGatewayMock.mockResolvedValueOnce({ messageId: "direct2" });
|
||||
const deps = makeDeps();
|
||||
await sendCommand(
|
||||
{
|
||||
to: "+1",
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { CliDeps } from "../cli/deps.js";
|
||||
import { info, success } from "../globals.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { sendViaIpc } from "../web/ipc.js";
|
||||
import { callGateway, randomIdempotencyKey } from "../gateway/call.js";
|
||||
import { startGatewayServer } from "../gateway/server.js";
|
||||
|
||||
export async function sendCommand(
|
||||
opts: {
|
||||
@@ -11,6 +12,7 @@ export async function sendCommand(
|
||||
json?: boolean;
|
||||
dryRun?: boolean;
|
||||
media?: string;
|
||||
spawnGateway?: boolean;
|
||||
},
|
||||
deps: CliDeps,
|
||||
runtime: RuntimeEnv,
|
||||
@@ -53,56 +55,44 @@ export async function sendCommand(
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to send via IPC to running gateway first (avoids Signal session corruption)
|
||||
const ipcResult = await sendViaIpc(opts.to, opts.message, opts.media);
|
||||
if (ipcResult) {
|
||||
if (ipcResult.success) {
|
||||
runtime.log(
|
||||
success(`✅ Sent via gateway IPC. Message ID: ${ipcResult.messageId}`),
|
||||
);
|
||||
if (opts.json) {
|
||||
runtime.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
provider: "web",
|
||||
via: "ipc",
|
||||
to: opts.to,
|
||||
messageId: ipcResult.messageId,
|
||||
mediaUrl: opts.media ?? null,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// IPC failed but gateway is running - warn and fall back
|
||||
runtime.log(
|
||||
info(
|
||||
`IPC send failed (${ipcResult.error}), falling back to direct connection`,
|
||||
),
|
||||
);
|
||||
// Always send via gateway over WS to avoid multi-session corruption.
|
||||
const sendViaGateway = async () =>
|
||||
callGateway<{
|
||||
messageId: string;
|
||||
}>({
|
||||
url: "ws://127.0.0.1:18789",
|
||||
method: "send",
|
||||
params: {
|
||||
to: opts.to,
|
||||
message: opts.message,
|
||||
mediaUrl: opts.media,
|
||||
idempotencyKey: randomIdempotencyKey(),
|
||||
},
|
||||
timeoutMs: 10_000,
|
||||
clientName: "cli",
|
||||
mode: "cli",
|
||||
});
|
||||
|
||||
let result: { messageId: string } | undefined;
|
||||
try {
|
||||
result = await sendViaGateway();
|
||||
} catch (err) {
|
||||
if (!opts.spawnGateway) throw err;
|
||||
await startGatewayServer(18789);
|
||||
result = await sendViaGateway();
|
||||
}
|
||||
|
||||
// Fall back to direct connection (creates new Baileys socket)
|
||||
const res = await deps
|
||||
.sendMessageWhatsApp(opts.to, opts.message, {
|
||||
verbose: false,
|
||||
mediaUrl: opts.media,
|
||||
})
|
||||
.catch((err) => {
|
||||
runtime.error(`❌ Web send failed: ${String(err)}`);
|
||||
throw err;
|
||||
});
|
||||
runtime.log(
|
||||
success(`✅ Sent via gateway. Message ID: ${result.messageId ?? "unknown"}`),
|
||||
);
|
||||
if (opts.json) {
|
||||
runtime.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
provider: "web",
|
||||
via: "direct",
|
||||
via: "gateway",
|
||||
to: opts.to,
|
||||
messageId: res.messageId,
|
||||
messageId: result.messageId,
|
||||
mediaUrl: opts.media ?? null,
|
||||
},
|
||||
null,
|
||||
|
||||
Reference in New Issue
Block a user