wagent 1.0.1 → 1.0.2

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 CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import "./quiet-console.js";
2
3
  import { InstanceManager } from "./services/instance-manager.js";
3
4
  import { createChildLogger } from "./utils/logger.js";
4
5
  import { generateText, tool } from "ai";
@@ -12,33 +13,6 @@ const MAX_HISTORY = 30;
12
13
  const processedMessages = new Set();
13
14
  const agentSentMessages = new Set();
14
15
  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
- };
42
16
  function getOwnJid(adapter) {
43
17
  return adapter.getMyJid();
44
18
  }
@@ -179,7 +153,7 @@ function buildTools(adapter) {
179
153
  execute: async ({ to, text, quotedMessageId }, { experimental_context }) => {
180
154
  const ctx = experimental_context;
181
155
  const target = await resolveTarget(adapter, to, ctx);
182
- const content = { type: "text", text };
156
+ const content = { type: "text", text: withAgentPrefix(text) };
183
157
  if (quotedMessageId)
184
158
  content.quotedMessageId = quotedMessageId;
185
159
  await delayBeforeReply(ctx.config);
@@ -710,9 +684,15 @@ function messageCanReachAgent(config, event, isSelf, text) {
710
684
  function isConfirmation(text) {
711
685
  return /\b(confirm|confirmed|approve|approved|yes|ship it|looks good)\b/i.test(text);
712
686
  }
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;
687
+ function isPromptDraftRequest(text) {
688
+ return /\b(enough|draft|write.*prompt|make.*prompt|system prompt|ready|done|that's it|that is it)\b/i.test(text);
689
+ }
690
+ function maybeExtractSayCommand(text) {
691
+ const match = text.match(/^\s*say\s+["']?(.+?)["']?\s*$/i);
692
+ return match?.[1]?.trim() ?? null;
693
+ }
694
+ function withAgentPrefix(text) {
695
+ return text.trim().toLowerCase().startsWith("wagent:") ? text : `wagent: ${text}`;
716
696
  }
717
697
  function nextBootstrapQuestion(notes) {
718
698
  const questions = [
@@ -724,14 +704,14 @@ function nextBootstrapQuestion(notes) {
724
704
  return questions[Math.min(Math.max(notes.length - 1, 0), questions.length - 1)];
725
705
  }
726
706
  async function sendTracked(adapter, to, text) {
727
- const res = await adapter.sendMessage(to, { type: "text", text });
707
+ const res = await adapter.sendMessage(to, { type: "text", text: withAgentPrefix(text) });
728
708
  rememberAgentMessage(res.messageId);
729
709
  }
730
710
  async function startBootstrap(adapter, config, myJid) {
731
711
  if (config.agent.bootstrapStarted || config.agent.mode !== "bootstrap")
732
712
  return config;
733
713
  await sendTracked(adapter, myJid, "I'm online. I don't know who I am yet.");
734
- await sendTracked(adapter, myJid, "Tell me my purpose, your relationship to me, my vibe, and who I can read or reply to.");
714
+ await sendTracked(adapter, myJid, "Tell me my purpose, what to call you, my vibe, and who I can read/reply to. Say 'draft prompt' when ready.");
735
715
  await sendTracked(adapter, myJid, "I can message contacts, manage WhatsApp tools, remember context, compact memory, and enforce read/reply permissions.");
736
716
  config.agent.bootstrapStarted = true;
737
717
  saveConfig(config);
@@ -747,8 +727,20 @@ async function handleBootstrapMessage(adapter, config, chatId, text) {
747
727
  await sendTracked(adapter, chatId, "Confirmed. I compacted setup context and will follow this system prompt from now on.");
748
728
  return config;
749
729
  }
730
+ if (config.agent.pendingSystemPrompt && !isConfirmation(text)) {
731
+ config.agent.pendingSystemPrompt = undefined;
732
+ config.agent.bootstrapNotes.push(`Feedback on rejected draft: ${text}`);
733
+ saveConfig(config);
734
+ await sendTracked(adapter, chatId, "Okay, I won't use that draft. Tell me what to change, or say 'draft prompt' when ready.");
735
+ return config;
736
+ }
737
+ const say = maybeExtractSayCommand(text);
738
+ if (say) {
739
+ await sendTracked(adapter, chatId, say);
740
+ return config;
741
+ }
750
742
  config.agent.bootstrapNotes.push(text);
751
- if (!isReadyForPromptDraft(text, config.agent.bootstrapNotes)) {
743
+ if (!isPromptDraftRequest(text)) {
752
744
  saveConfig(config);
753
745
  await sendTracked(adapter, chatId, nextBootstrapQuestion(config.agent.bootstrapNotes));
754
746
  return config;
@@ -794,7 +786,7 @@ async function processMessage(adapter, config, instId, event, text, isSelf, send
794
786
  if (responseText) {
795
787
  await adapter.sendPresence(chatId, "composing");
796
788
  await delayBeforeReply(config);
797
- const content = { type: "text", text: responseText };
789
+ const content = { type: "text", text: withAgentPrefix(responseText) };
798
790
  const res = await adapter.sendMessage(chatId, content);
799
791
  rememberAgentMessage(res.messageId);
800
792
  await adapter.sendPresence(chatId, "paused");
@@ -802,7 +794,7 @@ async function processMessage(adapter, config, instId, event, text, isSelf, send
802
794
  }
803
795
  else if (allToolCalls.length > 0) {
804
796
  logger.info({ chatId, toolCount: allToolCalls.length }, "Tools executed silently");
805
- const res = await adapter.sendMessage(chatId, { type: "text", text: "Done." });
797
+ const res = await adapter.sendMessage(chatId, { type: "text", text: withAgentPrefix("Done.") });
806
798
  rememberAgentMessage(res.messageId);
807
799
  }
808
800
  }
@@ -810,7 +802,7 @@ async function processMessage(adapter, config, instId, event, text, isSelf, send
810
802
  const msg = err instanceof Error ? err.message : String(err);
811
803
  logger.error({ err, chatId }, "Agent error");
812
804
  try {
813
- const fallback = "Sorry, I hit an error processing that. Please try again.";
805
+ const fallback = withAgentPrefix("Sorry, I hit an error processing that. Please try again.");
814
806
  const res = await adapter.sendMessage(chatId, { type: "text", text: fallback });
815
807
  rememberAgentMessage(res.messageId);
816
808
  }
@@ -0,0 +1,31 @@
1
+ const originalConsole = {
2
+ log: console.log.bind(console),
3
+ warn: console.warn.bind(console),
4
+ error: console.error.bind(console),
5
+ };
6
+ function shouldSuppressConsole(args) {
7
+ if (process.argv.includes("--debug"))
8
+ return false;
9
+ const text = args.map((arg) => (typeof arg === "string" ? arg : "")).join(" ");
10
+ return (text.startsWith("Closing session:") ||
11
+ text.includes("Decrypted message with closed session") ||
12
+ text.includes("stream errored out") ||
13
+ text.includes("no name present") ||
14
+ text.includes("blocked on missing key") ||
15
+ text.includes("failed to find key") ||
16
+ text.includes("transaction failed") ||
17
+ text.includes("failed to decrypt message"));
18
+ }
19
+ console.log = (...args) => {
20
+ if (!shouldSuppressConsole(args))
21
+ originalConsole.log(...args);
22
+ };
23
+ console.warn = (...args) => {
24
+ if (!shouldSuppressConsole(args))
25
+ originalConsole.warn(...args);
26
+ };
27
+ console.error = (...args) => {
28
+ if (!shouldSuppressConsole(args))
29
+ originalConsole.error(...args);
30
+ };
31
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wagent",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "WhatsApp Agent — AI-powered WhatsApp assistant using WAMCP + Groq",
5
5
  "type": "module",
6
6
  "bin": {