@gonzih/cc-discord 0.2.9 → 0.2.11
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 +4 -0
- package/dist/bot.js +32 -6
- package/dist/notifier.js +6 -5
- package/package.json +1 -1
package/dist/bot.d.ts
CHANGED
|
@@ -54,6 +54,10 @@ export declare class CcDiscordBot {
|
|
|
54
54
|
loadChannelMappings(): Promise<void>;
|
|
55
55
|
/** Typing intervals for meta-agent routed channels — keyed by channelId. */
|
|
56
56
|
private metaAgentTypingTimers;
|
|
57
|
+
/** Per-channel debounce state for guild channel → meta-agent routing. */
|
|
58
|
+
private messageBuffer;
|
|
59
|
+
/** Debounce window in ms — multiple messages within this window are coalesced into one prompt. */
|
|
60
|
+
private static readonly MESSAGE_DEBOUNCE_MS;
|
|
57
61
|
/** Start (or reset) the typing indicator for a meta-agent–routed channel. */
|
|
58
62
|
private startMetaAgentTyping;
|
|
59
63
|
/** Stop the typing indicator for a meta-agent–routed channel. Called by the notifier on flush. */
|
package/dist/bot.js
CHANGED
|
@@ -227,6 +227,10 @@ export class CcDiscordBot {
|
|
|
227
227
|
}
|
|
228
228
|
/** Typing intervals for meta-agent routed channels — keyed by channelId. */
|
|
229
229
|
metaAgentTypingTimers = new Map();
|
|
230
|
+
/** Per-channel debounce state for guild channel → meta-agent routing. */
|
|
231
|
+
messageBuffer = new Map();
|
|
232
|
+
/** Debounce window in ms — multiple messages within this window are coalesced into one prompt. */
|
|
233
|
+
static MESSAGE_DEBOUNCE_MS = 2500;
|
|
230
234
|
/** Start (or reset) the typing indicator for a meta-agent–routed channel. */
|
|
231
235
|
startMetaAgentTyping(channelId, channel) {
|
|
232
236
|
this.stopMetaAgentTyping(channelId);
|
|
@@ -443,6 +447,7 @@ export class CcDiscordBot {
|
|
|
443
447
|
const loopState = this.loopManager.getState(parentId);
|
|
444
448
|
if (loopState && loopState.threadId === effectiveChannelId) {
|
|
445
449
|
const username = msg.member?.displayName ?? msg.author.username;
|
|
450
|
+
this.startMetaAgentTyping(effectiveChannelId, msg.channel);
|
|
446
451
|
try {
|
|
447
452
|
await routeToMetaAgent(loopState.namespace, stampPrompt(text, username, msg.createdAt), this.redis);
|
|
448
453
|
}
|
|
@@ -465,20 +470,41 @@ export class CcDiscordBot {
|
|
|
465
470
|
this.writeChatMessage("user", "discord", text, effectiveChannelId, mappedNs.namespace);
|
|
466
471
|
this.opts.registerRoutedChannelId?.(mappedNs.namespace, effectiveChannelId);
|
|
467
472
|
this.persistChannelMapping(effectiveChannelId, mappedNs.namespace, mappedNs.repoUrl);
|
|
468
|
-
|
|
469
|
-
// Detect goal messages → create a thread for loop tracking
|
|
473
|
+
// Detect goal messages → create a thread for loop tracking (before debounce so thread is created promptly)
|
|
470
474
|
if (this.loopManager && msg.guild && !msg.channel.isThread()) {
|
|
471
475
|
if (isGoalMessage(text)) {
|
|
472
476
|
await this.createLoopThread(msg, effectiveChannelId, mappedNs.namespace, text);
|
|
473
477
|
}
|
|
474
478
|
}
|
|
475
479
|
const username = msg.member?.displayName ?? msg.author.username;
|
|
476
|
-
|
|
477
|
-
|
|
480
|
+
const stamped = stampPrompt(text, username, msg.createdAt);
|
|
481
|
+
// Capture for use inside async callback (this.redis is guaranteed non-null by the outer guard)
|
|
482
|
+
const redis = this.redis;
|
|
483
|
+
const nsName = mappedNs.namespace;
|
|
484
|
+
const sendChannel = msg.channel;
|
|
485
|
+
// Debounce: buffer messages arriving within MESSAGE_DEBOUNCE_MS and route once
|
|
486
|
+
const existing = this.messageBuffer.get(effectiveChannelId);
|
|
487
|
+
if (existing) {
|
|
488
|
+
// Another message already queued — append and reset timer
|
|
489
|
+
clearTimeout(existing.timer);
|
|
490
|
+
existing.messages.push(stamped);
|
|
478
491
|
}
|
|
479
|
-
|
|
480
|
-
|
|
492
|
+
else {
|
|
493
|
+
// First message in the window — start typing indicator immediately so user sees feedback
|
|
494
|
+
this.startMetaAgentTyping(effectiveChannelId, msg.channel);
|
|
495
|
+
this.messageBuffer.set(effectiveChannelId, { timer: undefined, messages: [stamped] });
|
|
481
496
|
}
|
|
497
|
+
const buf = this.messageBuffer.get(effectiveChannelId);
|
|
498
|
+
buf.timer = setTimeout(async () => {
|
|
499
|
+
this.messageBuffer.delete(effectiveChannelId);
|
|
500
|
+
const combined = buf.messages.join('\n\n');
|
|
501
|
+
try {
|
|
502
|
+
await routeToMetaAgent(nsName, combined, redis);
|
|
503
|
+
}
|
|
504
|
+
catch (err) {
|
|
505
|
+
await sendChannel.send(`Failed to route to ${nsName}: ${err.message}`).catch(() => { });
|
|
506
|
+
}
|
|
507
|
+
}, CcDiscordBot.MESSAGE_DEBOUNCE_MS);
|
|
482
508
|
return;
|
|
483
509
|
}
|
|
484
510
|
// Unknown guild channel — reject rather than silently start a local session with wrong context
|
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.
|
|
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;
|
|
@@ -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)
|
|
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)
|
|
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);
|