@gonzih/cc-discord 0.2.8 → 0.2.10

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/bot.d.ts CHANGED
@@ -66,6 +66,12 @@ export declare class CcDiscordBot {
66
66
  private sendToChannel;
67
67
  /** Send to a channel by ID — used by notifier callbacks. */
68
68
  sendToChannelById(channelId: string, text: string): Promise<void>;
69
+ /**
70
+ * Send text to a channel by ID, scanning for absolute file paths and attaching them.
71
+ * Used exclusively for Claude coordinator output (meta-agent flush).
72
+ * Falls back to plain text send if no valid files are found.
73
+ */
74
+ sendWithFileDetection(channelId: string, text: string): Promise<void>;
69
75
  private isAllowed;
70
76
  private handleMessage;
71
77
  private handleVoice;
package/dist/bot.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * One ClaudeProcess per channel (or channel:thread) — sessions are isolated per channel.
4
4
  */
5
5
  import { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder, EmbedBuilder, AttachmentBuilder, Events, ChannelType, } from "discord.js";
6
- import { existsSync, createWriteStream, mkdirSync } from "fs";
6
+ import { existsSync, createWriteStream, mkdirSync, statSync } from "fs";
7
7
  import { resolve, basename, join } from "path";
8
8
  import https from "https";
9
9
  import http from "http";
@@ -301,6 +301,70 @@ export class CcDiscordBot {
301
301
  }
302
302
  await this.sendToChannel(channel, text);
303
303
  }
304
+ /**
305
+ * Send text to a channel by ID, scanning for absolute file paths and attaching them.
306
+ * Used exclusively for Claude coordinator output (meta-agent flush).
307
+ * Falls back to plain text send if no valid files are found.
308
+ */
309
+ async sendWithFileDetection(channelId, text) {
310
+ const channel = await this.getChannel(channelId);
311
+ if (!channel) {
312
+ console.warn(`[bot] sendWithFileDetection: channel ${channelId} not found`);
313
+ return;
314
+ }
315
+ // Extract absolute paths from text — handle bare paths and backtick-wrapped paths
316
+ const rawMatches = text.match(/`(\/[^`\s]+)`|\/[^\s`'"]+/g) ?? [];
317
+ const candidates = rawMatches.map((m) => m.replace(/^`|`$/g, ""));
318
+ const MAX_SIZE = 8 * 1024 * 1024;
319
+ const validPaths = [];
320
+ const seen = new Set();
321
+ for (const candidate of candidates) {
322
+ // Strip trailing punctuation that may have been caught by the regex
323
+ const p = candidate.replace(/[.,;:!?)]+$/, "");
324
+ if (seen.has(p))
325
+ continue;
326
+ seen.add(p);
327
+ try {
328
+ if (existsSync(p)) {
329
+ const st = statSync(p);
330
+ if (st.isFile() && st.size < MAX_SIZE) {
331
+ validPaths.push(p);
332
+ }
333
+ }
334
+ }
335
+ catch {
336
+ // ignore stat errors
337
+ }
338
+ }
339
+ if (validPaths.length > 0) {
340
+ const formatted = formatForDiscord(text);
341
+ const chunks = splitLongMessage(formatted);
342
+ // Send first chunk with file attachments, remaining chunks as plain text
343
+ const files = validPaths.map((p) => ({ attachment: p, name: basename(p) }));
344
+ try {
345
+ await channel.send({ content: chunks[0] || undefined, files });
346
+ }
347
+ catch (err) {
348
+ console.warn(`[bot] sendWithFileDetection attach failed:`, err.message);
349
+ // Fall back to plain text for the first chunk
350
+ if (chunks[0]?.trim()) {
351
+ await channel.send(chunks[0]).catch((e) => {
352
+ console.error("[bot] sendWithFileDetection fallback failed:", e.message);
353
+ });
354
+ }
355
+ }
356
+ for (const chunk of chunks.slice(1)) {
357
+ if (!chunk.trim())
358
+ continue;
359
+ await channel.send(chunk).catch((e) => {
360
+ console.error("[bot] send failed:", e.message);
361
+ });
362
+ }
363
+ }
364
+ else {
365
+ await this.sendToChannel(channel, text);
366
+ }
367
+ }
304
368
  isAllowed(userId) {
305
369
  if (!this.opts.allowedUserIds?.length)
306
370
  return true;
@@ -379,6 +443,7 @@ export class CcDiscordBot {
379
443
  const loopState = this.loopManager.getState(parentId);
380
444
  if (loopState && loopState.threadId === effectiveChannelId) {
381
445
  const username = msg.member?.displayName ?? msg.author.username;
446
+ this.startMetaAgentTyping(effectiveChannelId, msg.channel);
382
447
  try {
383
448
  await routeToMetaAgent(loopState.namespace, stampPrompt(text, username, msg.createdAt), this.redis);
384
449
  }
package/dist/notifier.js CHANGED
@@ -203,7 +203,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
203
203
  // Buffer for meta-agent streaming output — debounced before sending to Discord
204
204
  const metaAgentBuffers = new Map();
205
205
  function flushMetaAgentBuffer(ns, targetChannelId) {
206
- bot.stopMetaAgentTyping(targetChannelId);
206
+ const loopThreadId = bot.getLoopThreadId(targetChannelId);
207
+ bot.stopMetaAgentTyping(loopThreadId ?? targetChannelId);
207
208
  const buf = metaAgentBuffers.get(ns);
208
209
  if (!buf || !buf.text.trim())
209
210
  return;
@@ -214,8 +215,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
214
215
  const deliverTo = bot.getLoopThreadId(targetChannelId) ?? targetChannelId;
215
216
  const chunks = splitLongMessage(text);
216
217
  for (const chunk of chunks) {
217
- bot.sendToChannelById(deliverTo, chunk).catch((err) => {
218
- log("warn", `meta-agent flush sendToChannelById failed (ns=${ns}):`, err.message);
218
+ bot.sendWithFileDetection(deliverTo, chunk).catch((err) => {
219
+ log("warn", `meta-agent flush sendWithFileDetection failed (ns=${ns}):`, err.message);
219
220
  });
220
221
  }
221
222
  }
@@ -290,8 +291,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
290
291
  const mainChannelId = ns === namespace
291
292
  ? (resolveNotifyChannel(notification.chatId, notifyChannelId, getActiveChannelId, reverseSnowflakeLookup, ns, getChannelIdForNamespace) ?? targetChannelId)
292
293
  : targetChannelId;
293
- // If a loop is active for this channel, route to its thread
294
- const destChannelId = bot.getLoopThreadId(mainChannelId) ?? mainChannelId;
294
+ // If a loop is active for this channel, route to its thread (skip for cron notifications)
295
+ const destChannelId = (!notification.isCron && bot.getLoopThreadId(mainChannelId)) ? bot.getLoopThreadId(mainChannelId) : mainChannelId;
295
296
  // When an eval report is embedded, post a structured embed to the thread
296
297
  if (notification.evalReport) {
297
298
  bot.postEvalEmbed(mainChannelId, notification.evalReport).catch((err) => {
@@ -351,8 +352,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
351
352
  : routedChannelIds.get(ns);
352
353
  }
353
354
  if (mainChannelId != null) {
354
- // If a loop is active, route notification text to the thread
355
- const deliverTo = bot.getLoopThreadId(mainChannelId) ?? mainChannelId;
355
+ // If a loop is active, route notification text to the thread (skip for cron notifications)
356
+ const deliverTo = (!notification.isCron && bot.getLoopThreadId(mainChannelId)) ? bot.getLoopThreadId(mainChannelId) : mainChannelId;
356
357
  if (notification.evalReport) {
357
358
  bot.postEvalEmbed(mainChannelId, notification.evalReport).catch((err) => {
358
359
  log("warn", `postEvalEmbed failed (ns=${ns}):`, err.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gonzih/cc-discord",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "Claude Code Discord bot — chat with Claude Code via Discord",
5
5
  "type": "module",
6
6
  "bin": {