refactor(whatsapp): reuse login tool implementation
This commit is contained in:
parent
da9e0b658d
commit
e793e3873f
@ -1,71 +1 @@
|
|||||||
import { Type } from "@sinclair/typebox";
|
export { createWhatsAppLoginTool as createRuntimeWhatsAppLoginTool } from "../../../extensions/whatsapp/src/agent-tools-login.js";
|
||||||
import type { ChannelAgentTool } from "openclaw/plugin-sdk/channel-runtime";
|
|
||||||
|
|
||||||
export function createRuntimeWhatsAppLoginTool(): ChannelAgentTool {
|
|
||||||
return {
|
|
||||||
label: "WhatsApp Login",
|
|
||||||
name: "whatsapp_login",
|
|
||||||
ownerOnly: true,
|
|
||||||
description: "Generate a WhatsApp QR code for linking, or wait for the scan to complete.",
|
|
||||||
parameters: Type.Object({
|
|
||||||
action: Type.Unsafe<"start" | "wait">({
|
|
||||||
type: "string",
|
|
||||||
enum: ["start", "wait"],
|
|
||||||
}),
|
|
||||||
timeoutMs: Type.Optional(Type.Number()),
|
|
||||||
force: Type.Optional(Type.Boolean()),
|
|
||||||
}),
|
|
||||||
execute: async (_toolCallId, args) => {
|
|
||||||
const { startWebLoginWithQr, waitForWebLogin } =
|
|
||||||
await import("../../../extensions/whatsapp/src/login-qr.js");
|
|
||||||
const action = (args as { action?: string })?.action ?? "start";
|
|
||||||
if (action === "wait") {
|
|
||||||
const result = await waitForWebLogin({
|
|
||||||
timeoutMs:
|
|
||||||
typeof (args as { timeoutMs?: unknown }).timeoutMs === "number"
|
|
||||||
? (args as { timeoutMs?: number }).timeoutMs
|
|
||||||
: undefined,
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
content: [{ type: "text", text: result.message }],
|
|
||||||
details: { connected: result.connected },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await startWebLoginWithQr({
|
|
||||||
timeoutMs:
|
|
||||||
typeof (args as { timeoutMs?: unknown }).timeoutMs === "number"
|
|
||||||
? (args as { timeoutMs?: number }).timeoutMs
|
|
||||||
: undefined,
|
|
||||||
force:
|
|
||||||
typeof (args as { force?: unknown }).force === "boolean"
|
|
||||||
? (args as { force?: boolean }).force
|
|
||||||
: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result.qrDataUrl) {
|
|
||||||
return {
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: result.message,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
details: { qr: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const text = [
|
|
||||||
result.message,
|
|
||||||
"",
|
|
||||||
"Open WhatsApp -> Linked Devices and scan:",
|
|
||||||
"",
|
|
||||||
``,
|
|
||||||
].join("\n");
|
|
||||||
return {
|
|
||||||
content: [{ type: "text", text }],
|
|
||||||
details: { qr: true },
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user