@botcord/daemon 0.2.45 → 0.2.47

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.
@@ -601,10 +601,22 @@ function normalizeAssistantText(text) {
601
601
  if (!finalMatch && selected.trimStart().toLowerCase().startsWith("<think")) {
602
602
  return "";
603
603
  }
604
- return selected
604
+ return stripLeadingBoundaryResidue(selected
605
605
  .replace(/<think[^>]*>[\s\S]*?<\/think>/gi, "")
606
606
  .replace(/<\/?final>/gi, "")
607
- .trim();
607
+ .trim());
608
+ }
609
+ function stripLeadingBoundaryResidue(text) {
610
+ if (!text.startsWith("<"))
611
+ return text;
612
+ // Keep real HTML/XML-ish tags and common comparison operators. A lone
613
+ // leading "<" before normal prose can be left behind when ACP streams a
614
+ // structural marker boundary separately from the final assistant text.
615
+ if (/^<\/?[A-Za-z][A-Za-z0-9:-]*(?:\s|>|\/>)/.test(text))
616
+ return text;
617
+ if (/^<(?:\s|=|<)/.test(text))
618
+ return text;
619
+ return text.slice(1).trimStart();
608
620
  }
609
621
  function createAssistantTextFilter() {
610
622
  let pending = "";
package/dist/turn-text.js CHANGED
@@ -5,15 +5,18 @@ const GROUP_HINT = '[In group chats, do NOT reply unless you are explicitly ment
5
5
  const DIRECT_HINT = '[If the conversation has naturally concluded or no response is needed, ' +
6
6
  'reply with exactly "NO_REPLY" and nothing else.]';
7
7
  /**
8
- * Reminder appended to every wrapped (non-owner-chat) inbound message. The
9
- * dispatcher discards `result.text` for any room that is not `rm_oc_*`, so
10
- * the agent must call the `botcord_send` tool (or the `botcord send` CLI
11
- * via Bash) to actually deliver a reply. Plain assistant text in those
12
- * rooms is logged and dropped.
8
+ * Reminder appended to wrapped BotCord network rooms that are not owner-chat.
9
+ * The dispatcher discards `result.text` for those rooms, so the agent must
10
+ * call the `botcord_send` tool (or the `botcord send` CLI via Bash) to
11
+ * actually deliver a reply. Plain assistant text in those rooms is logged
12
+ * and dropped.
13
13
  */
14
14
  const NON_OWNER_REPLY_HINT = "[This room is NOT owner-chat. Plain text output WILL NOT be sent. " +
15
15
  "To reply, call the `botcord_send` tool, or run " +
16
16
  '`botcord send --room <room_id> --text "..."` via Bash.]';
17
+ const THIRD_PARTY_REPLY_HINT = "[This is a third-party gateway chat. Reply normally in your final assistant " +
18
+ "message; BotCord daemon will deliver that text through the same channel. " +
19
+ "No extra send tool is required for this chat.]";
17
20
  /**
18
21
  * Read the BotCord envelope type from a raw inbound message. Returns
19
22
  * `undefined` when the message didn't come from the BotCord channel or the
@@ -28,6 +31,15 @@ function readEnvelopeType(raw) {
28
31
  const t = env.type;
29
32
  return typeof t === "string" ? t : undefined;
30
33
  }
34
+ function isThirdPartyConversation(conversationId) {
35
+ return (conversationId.startsWith("telegram:") ||
36
+ conversationId.startsWith("wechat:"));
37
+ }
38
+ function replyDeliveryHint(msg) {
39
+ return isThirdPartyConversation(msg.conversation.id)
40
+ ? THIRD_PARTY_REPLY_HINT
41
+ : NON_OWNER_REPLY_HINT;
42
+ }
31
43
  /**
32
44
  * Read the `raw.batch` array emitted by the BotCord channel when inbox
33
45
  * drain groups multiple messages for the same `(room, topic)`. Returns the
@@ -127,7 +139,7 @@ export function composeBotCordUserTurn(msg) {
127
139
  "",
128
140
  hint,
129
141
  "",
130
- NON_OWNER_REPLY_HINT,
142
+ replyDeliveryHint(msg),
131
143
  ];
132
144
  if (contactRequestHint) {
133
145
  lines.push("", contactRequestHint);
@@ -178,7 +190,7 @@ function composeBatchedTurn(msg, batch) {
178
190
  "",
179
191
  hint,
180
192
  "",
181
- NON_OWNER_REPLY_HINT,
193
+ replyDeliveryHint(msg),
182
194
  ];
183
195
  if (contactRequestSenders.length > 0) {
184
196
  // Dedup + list — multiple distinct senders show as "A, B".
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botcord/daemon",
3
- "version": "0.2.45",
3
+ "version": "0.2.47",
4
4
  "description": "BotCord local daemon — bridges Hub inbox push to local Claude Code / Codex / Gemini CLIs",
5
5
  "type": "module",
6
6
  "bin": {
@@ -265,6 +265,88 @@ describe("OpenclawAcpAdapter.run", () => {
265
265
  );
266
266
  });
267
267
 
268
+ it("strips a lone leading ACP boundary marker from final prompt text", async () => {
269
+ const child = new FakeChild();
270
+ const adapter = new OpenclawAcpAdapter({ spawnFn: makeSpawn(child) });
271
+ const gateway: ResolvedOpenclawGateway = {
272
+ name: "local",
273
+ url: "ws://127.0.0.1:1",
274
+ openclawAgent: "main",
275
+ };
276
+
277
+ child.stdin.on("data", (chunk: Buffer) => {
278
+ for (const line of chunk.toString("utf8").split("\n").filter(Boolean)) {
279
+ const frame = JSON.parse(line);
280
+ if (frame.method === "initialize") {
281
+ child.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { protocolVersion: 1 } }) + "\n");
282
+ } else if (frame.method === "session/new") {
283
+ child.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { sessionId: "sid-boundary" } }) + "\n");
284
+ } else if (frame.method === "session/prompt") {
285
+ child.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { text: "<你好!终于可以正常交流了。" } }) + "\n");
286
+ }
287
+ }
288
+ });
289
+
290
+ const res = await adapter.run({
291
+ text: "hi",
292
+ sessionId: null,
293
+ cwd: "/tmp",
294
+ accountId: "ag_alice",
295
+ signal: new AbortController().signal,
296
+ trustLevel: "owner",
297
+ gateway,
298
+ });
299
+
300
+ expect(res.text).toBe("你好!终于可以正常交流了。");
301
+ });
302
+
303
+ it("strips a lone leading ACP boundary marker from streamed fallback text", async () => {
304
+ const child = new FakeChild();
305
+ const adapter = new OpenclawAcpAdapter({ spawnFn: makeSpawn(child) });
306
+ const gateway: ResolvedOpenclawGateway = {
307
+ name: "local",
308
+ url: "ws://127.0.0.1:1",
309
+ openclawAgent: "main",
310
+ };
311
+
312
+ child.stdin.on("data", (chunk: Buffer) => {
313
+ for (const line of chunk.toString("utf8").split("\n").filter(Boolean)) {
314
+ const frame = JSON.parse(line);
315
+ if (frame.method === "initialize") {
316
+ child.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { protocolVersion: 1 } }) + "\n");
317
+ } else if (frame.method === "session/new") {
318
+ child.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { sessionId: "sid-stream-boundary" } }) + "\n");
319
+ } else if (frame.method === "session/prompt") {
320
+ for (const text of ["<", "好!终于可以正常交流了。"]) {
321
+ child.stdout.write(
322
+ JSON.stringify({
323
+ jsonrpc: "2.0",
324
+ method: "session/update",
325
+ params: {
326
+ sessionId: "sid-stream-boundary",
327
+ update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text } },
328
+ },
329
+ }) + "\n",
330
+ );
331
+ }
332
+ child.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { stopReason: "end_turn" } }) + "\n");
333
+ }
334
+ }
335
+ });
336
+
337
+ const res = await adapter.run({
338
+ text: "hi",
339
+ sessionId: null,
340
+ cwd: "/tmp",
341
+ accountId: "ag_alice",
342
+ signal: new AbortController().signal,
343
+ trustLevel: "owner",
344
+ gateway,
345
+ });
346
+
347
+ expect(res.text).toBe("好!终于可以正常交流了。");
348
+ });
349
+
268
350
  it("respawns the pooled child when gateway.url or gateway.token changes under the same name", async () => {
269
351
  function newChild(): FakeChild {
270
352
  const c = new FakeChild();
@@ -68,6 +68,43 @@ describe("composeBotCordUserTurn", () => {
68
68
  expect(out).not.toContain("do NOT reply unless");
69
69
  });
70
70
 
71
+ it("keeps the botcord_send delivery hint for non-owner BotCord rooms", () => {
72
+ const out = composeBotCordUserTurn(
73
+ makeMessage({
74
+ conversation: { id: "rm_dm_xxx", kind: "direct" },
75
+ sender: { id: "ag_peer", kind: "agent" },
76
+ }),
77
+ );
78
+ expect(out).toContain("Plain text output WILL NOT be sent");
79
+ expect(out).toContain("botcord_send");
80
+ });
81
+
82
+ it("does not tell Telegram chats to use botcord_send", () => {
83
+ const out = composeBotCordUserTurn(
84
+ makeMessage({
85
+ channel: "gw_telegram_123",
86
+ conversation: { id: "telegram:user:7904063707", kind: "direct" },
87
+ sender: { id: "telegram:user:7904063707", name: "danny_aaas", kind: "user" },
88
+ }),
89
+ );
90
+ expect(out).toContain("third-party gateway chat");
91
+ expect(out).toContain("Reply normally in your final assistant message");
92
+ expect(out).not.toContain("Plain text output WILL NOT be sent");
93
+ expect(out).not.toContain("botcord_send");
94
+ });
95
+
96
+ it("does not tell WeChat chats to use botcord_send", () => {
97
+ const out = composeBotCordUserTurn(
98
+ makeMessage({
99
+ channel: "gw_wechat_123",
100
+ conversation: { id: "wechat:user:wxl_alice", kind: "direct" },
101
+ sender: { id: "wechat:user:wxl_alice", name: "Alice", kind: "user" },
102
+ }),
103
+ );
104
+ expect(out).toContain("third-party gateway chat");
105
+ expect(out).not.toContain("botcord_send");
106
+ });
107
+
71
108
  it("passes owner-chat messages through verbatim (no wrapper, no hint)", () => {
72
109
  const out = composeBotCordUserTurn(
73
110
  makeMessage({
@@ -709,10 +709,20 @@ function normalizeAssistantText(text: string | undefined): string {
709
709
  if (!finalMatch && selected.trimStart().toLowerCase().startsWith("<think")) {
710
710
  return "";
711
711
  }
712
- return selected
712
+ return stripLeadingBoundaryResidue(selected
713
713
  .replace(/<think[^>]*>[\s\S]*?<\/think>/gi, "")
714
714
  .replace(/<\/?final>/gi, "")
715
- .trim();
715
+ .trim());
716
+ }
717
+
718
+ function stripLeadingBoundaryResidue(text: string): string {
719
+ if (!text.startsWith("<")) return text;
720
+ // Keep real HTML/XML-ish tags and common comparison operators. A lone
721
+ // leading "<" before normal prose can be left behind when ACP streams a
722
+ // structural marker boundary separately from the final assistant text.
723
+ if (/^<\/?[A-Za-z][A-Za-z0-9:-]*(?:\s|>|\/>)/.test(text)) return text;
724
+ if (/^<(?:\s|=|<)/.test(text)) return text;
725
+ return text.slice(1).trimStart();
716
726
  }
717
727
 
718
728
  function createAssistantTextFilter(): {
package/src/turn-text.ts CHANGED
@@ -35,16 +35,20 @@ const DIRECT_HINT =
35
35
  'reply with exactly "NO_REPLY" and nothing else.]';
36
36
 
37
37
  /**
38
- * Reminder appended to every wrapped (non-owner-chat) inbound message. The
39
- * dispatcher discards `result.text` for any room that is not `rm_oc_*`, so
40
- * the agent must call the `botcord_send` tool (or the `botcord send` CLI
41
- * via Bash) to actually deliver a reply. Plain assistant text in those
42
- * rooms is logged and dropped.
38
+ * Reminder appended to wrapped BotCord network rooms that are not owner-chat.
39
+ * The dispatcher discards `result.text` for those rooms, so the agent must
40
+ * call the `botcord_send` tool (or the `botcord send` CLI via Bash) to
41
+ * actually deliver a reply. Plain assistant text in those rooms is logged
42
+ * and dropped.
43
43
  */
44
44
  const NON_OWNER_REPLY_HINT =
45
45
  "[This room is NOT owner-chat. Plain text output WILL NOT be sent. " +
46
46
  "To reply, call the `botcord_send` tool, or run " +
47
47
  '`botcord send --room <room_id> --text "..."` via Bash.]';
48
+ const THIRD_PARTY_REPLY_HINT =
49
+ "[This is a third-party gateway chat. Reply normally in your final assistant " +
50
+ "message; BotCord daemon will deliver that text through the same channel. " +
51
+ "No extra send tool is required for this chat.]";
48
52
 
49
53
  /**
50
54
  * Read the BotCord envelope type from a raw inbound message. Returns
@@ -59,6 +63,19 @@ function readEnvelopeType(raw: unknown): string | undefined {
59
63
  return typeof t === "string" ? t : undefined;
60
64
  }
61
65
 
66
+ function isThirdPartyConversation(conversationId: string): boolean {
67
+ return (
68
+ conversationId.startsWith("telegram:") ||
69
+ conversationId.startsWith("wechat:")
70
+ );
71
+ }
72
+
73
+ function replyDeliveryHint(msg: GatewayInboundMessage): string {
74
+ return isThirdPartyConversation(msg.conversation.id)
75
+ ? THIRD_PARTY_REPLY_HINT
76
+ : NON_OWNER_REPLY_HINT;
77
+ }
78
+
62
79
  /** Minimal shape of one batched inbound entry. Matches the BotCord channel
63
80
  * `BatchedInboxRaw.batch[]` elements but expressed structurally so the
64
81
  * composer doesn't import channel internals. */
@@ -182,7 +199,7 @@ export function composeBotCordUserTurn(msg: GatewayInboundMessage): string {
182
199
  "",
183
200
  hint,
184
201
  "",
185
- NON_OWNER_REPLY_HINT,
202
+ replyDeliveryHint(msg),
186
203
  ];
187
204
  if (contactRequestHint) {
188
205
  lines.push("", contactRequestHint);
@@ -243,7 +260,7 @@ function composeBatchedTurn(
243
260
  "",
244
261
  hint,
245
262
  "",
246
- NON_OWNER_REPLY_HINT,
263
+ replyDeliveryHint(msg),
247
264
  ];
248
265
 
249
266
  if (contactRequestSenders.length > 0) {