wagent 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +60 -6
- package/dist/utils/logger.js +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12,9 +12,42 @@ const MAX_HISTORY = 30;
|
|
|
12
12
|
const processedMessages = new Set();
|
|
13
13
|
const agentSentMessages = new Set();
|
|
14
14
|
const typingUntil = new Map();
|
|
15
|
+
const originalConsole = {
|
|
16
|
+
log: console.log.bind(console),
|
|
17
|
+
warn: console.warn.bind(console),
|
|
18
|
+
error: console.error.bind(console),
|
|
19
|
+
};
|
|
20
|
+
function shouldSuppressConsole(args) {
|
|
21
|
+
if (process.argv.includes("--debug"))
|
|
22
|
+
return false;
|
|
23
|
+
const text = args.map((arg) => (typeof arg === "string" ? arg : "")).join(" ");
|
|
24
|
+
return (text.startsWith("Closing session:") ||
|
|
25
|
+
text.includes("Decrypted message with closed session") ||
|
|
26
|
+
text.includes("stream errored out") ||
|
|
27
|
+
text.includes("no name present") ||
|
|
28
|
+
text.includes("blocked on missing key"));
|
|
29
|
+
}
|
|
30
|
+
console.log = (...args) => {
|
|
31
|
+
if (!shouldSuppressConsole(args))
|
|
32
|
+
originalConsole.log(...args);
|
|
33
|
+
};
|
|
34
|
+
console.warn = (...args) => {
|
|
35
|
+
if (!shouldSuppressConsole(args))
|
|
36
|
+
originalConsole.warn(...args);
|
|
37
|
+
};
|
|
38
|
+
console.error = (...args) => {
|
|
39
|
+
if (!shouldSuppressConsole(args))
|
|
40
|
+
originalConsole.error(...args);
|
|
41
|
+
};
|
|
15
42
|
function getOwnJid(adapter) {
|
|
16
43
|
return adapter.getMyJid();
|
|
17
44
|
}
|
|
45
|
+
function toUserJid(jid) {
|
|
46
|
+
if (!jid || !jid.includes("@"))
|
|
47
|
+
return jid;
|
|
48
|
+
const [user, domain] = jid.split("@");
|
|
49
|
+
return `${user.split(":")[0]}@${domain}`;
|
|
50
|
+
}
|
|
18
51
|
function getContactName(adapter, jid) {
|
|
19
52
|
return adapter.getContacts(jid).then((c) => {
|
|
20
53
|
const match = c.find((x) => x.jid === jid);
|
|
@@ -677,6 +710,19 @@ function messageCanReachAgent(config, event, isSelf, text) {
|
|
|
677
710
|
function isConfirmation(text) {
|
|
678
711
|
return /\b(confirm|confirmed|approve|approved|yes|ship it|looks good)\b/i.test(text);
|
|
679
712
|
}
|
|
713
|
+
function isReadyForPromptDraft(text, notes) {
|
|
714
|
+
const total = notes.join(" ").length;
|
|
715
|
+
return /\b(enough|draft|write.*prompt|make.*prompt|done|that's it|that is it)\b/i.test(text) || notes.length >= 3 || total >= 500;
|
|
716
|
+
}
|
|
717
|
+
function nextBootstrapQuestion(notes) {
|
|
718
|
+
const questions = [
|
|
719
|
+
"Got it. Who are you to me, and what should I call you?",
|
|
720
|
+
"What should my purpose be?",
|
|
721
|
+
"Who am I allowed to read or reply to?",
|
|
722
|
+
"What vibe should I use when I talk?",
|
|
723
|
+
];
|
|
724
|
+
return questions[Math.min(Math.max(notes.length - 1, 0), questions.length - 1)];
|
|
725
|
+
}
|
|
680
726
|
async function sendTracked(adapter, to, text) {
|
|
681
727
|
const res = await adapter.sendMessage(to, { type: "text", text });
|
|
682
728
|
rememberAgentMessage(res.messageId);
|
|
@@ -702,6 +748,11 @@ async function handleBootstrapMessage(adapter, config, chatId, text) {
|
|
|
702
748
|
return config;
|
|
703
749
|
}
|
|
704
750
|
config.agent.bootstrapNotes.push(text);
|
|
751
|
+
if (!isReadyForPromptDraft(text, config.agent.bootstrapNotes)) {
|
|
752
|
+
saveConfig(config);
|
|
753
|
+
await sendTracked(adapter, chatId, nextBootstrapQuestion(config.agent.bootstrapNotes));
|
|
754
|
+
return config;
|
|
755
|
+
}
|
|
705
756
|
const draft = await generateWithFallback(config, {
|
|
706
757
|
system: "Draft a concise first-person system prompt for a WhatsApp agent from the user's setup notes. Include identity, purpose, vibe, read/reply boundaries, and tool behavior. Return only the prompt.",
|
|
707
758
|
messages: [{ role: "user", content: config.agent.bootstrapNotes.join("\n\n") }],
|
|
@@ -794,6 +845,7 @@ async function main() {
|
|
|
794
845
|
instanceId = instances[0].id;
|
|
795
846
|
}
|
|
796
847
|
let myJid = null;
|
|
848
|
+
let myUserJid = null;
|
|
797
849
|
instanceManager.onAnyEvent((event, instId, payload) => {
|
|
798
850
|
if (event === "connection.changed" && instId === instanceId) {
|
|
799
851
|
const conn = payload;
|
|
@@ -804,6 +856,7 @@ async function main() {
|
|
|
804
856
|
if (conn.status === "open") {
|
|
805
857
|
const adapter = instanceManager.getAdapter(instId);
|
|
806
858
|
myJid = getOwnJid(adapter);
|
|
859
|
+
myUserJid = toUserJid(myJid);
|
|
807
860
|
console.log(`\nWhatsApp connected as ${myJid}`);
|
|
808
861
|
}
|
|
809
862
|
}
|
|
@@ -812,7 +865,8 @@ async function main() {
|
|
|
812
865
|
const adapter = instanceManager.getAdapter(instanceId);
|
|
813
866
|
for (let i = 0; i < 60; i++) {
|
|
814
867
|
myJid = getOwnJid(adapter);
|
|
815
|
-
|
|
868
|
+
myUserJid = toUserJid(myJid);
|
|
869
|
+
if (myJid && myUserJid)
|
|
816
870
|
break;
|
|
817
871
|
await new Promise((r) => setTimeout(r, 1000));
|
|
818
872
|
}
|
|
@@ -822,7 +876,7 @@ async function main() {
|
|
|
822
876
|
}
|
|
823
877
|
config = await ensureAiConfig(config);
|
|
824
878
|
const tools = buildTools(adapter);
|
|
825
|
-
config = await startBootstrap(adapter, config, myJid);
|
|
879
|
+
config = await startBootstrap(adapter, config, myUserJid ?? myJid);
|
|
826
880
|
console.log("Agent ready. Message yourself to configure or command it.");
|
|
827
881
|
instanceManager.onAnyEvent(async (event, instId, payload) => {
|
|
828
882
|
if (event === "presence.updated" && instId === instanceId) {
|
|
@@ -850,11 +904,11 @@ async function main() {
|
|
|
850
904
|
for (const k of toDelete)
|
|
851
905
|
processedMessages.delete(k);
|
|
852
906
|
}
|
|
853
|
-
const
|
|
854
|
-
if (!messageCanReachAgent(config, msg,
|
|
907
|
+
const isOwnerSelfChat = Boolean(msg.message.isFromMe && myUserJid && msg.chatId === myUserJid);
|
|
908
|
+
if (!messageCanReachAgent(config, msg, isOwnerSelfChat, text))
|
|
855
909
|
return;
|
|
856
910
|
await waitForTypingToStop(msg.chatId);
|
|
857
|
-
if (config.agent.mode === "bootstrap" &&
|
|
911
|
+
if (config.agent.mode === "bootstrap" && isOwnerSelfChat) {
|
|
858
912
|
config = await handleBootstrapMessage(adapter, config, msg.chatId, text);
|
|
859
913
|
return;
|
|
860
914
|
}
|
|
@@ -862,7 +916,7 @@ async function main() {
|
|
|
862
916
|
const chatName = msg.chatId.endsWith("@g.us")
|
|
863
917
|
? await getGroupName(adapter, msg.chatId)
|
|
864
918
|
: senderName;
|
|
865
|
-
processMessage(adapter, config, instanceId, msg, text,
|
|
919
|
+
processMessage(adapter, config, instanceId, msg, text, isOwnerSelfChat, senderName, chatName, tools)
|
|
866
920
|
.catch((err) => logger.error({ err }, "processMessage error"));
|
|
867
921
|
}
|
|
868
922
|
});
|
package/dist/utils/logger.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// WA MCP — Structured Logger with Request Tracing
|
|
3
3
|
// ============================================================
|
|
4
4
|
import pino from "pino";
|
|
5
|
-
import { SERVER_NAME
|
|
5
|
+
import { SERVER_NAME } from "../constants.js";
|
|
6
6
|
const logLevel = process.argv.includes("--debug")
|
|
7
7
|
? "debug"
|
|
8
8
|
: process.argv.includes("--verbose")
|
|
9
9
|
? "info"
|
|
10
|
-
:
|
|
10
|
+
: "silent";
|
|
11
11
|
const isStdio = process.env.WA_TRANSPORT === "stdio";
|
|
12
12
|
export const logger = pino({
|
|
13
13
|
name: SERVER_NAME,
|