@gonzih/cc-discord 0.2.7 → 0.2.9
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 +6 -0
- package/dist/bot.js +66 -2
- package/dist/notifier.d.ts +2 -1
- package/dist/notifier.js +12 -9
- package/package.json +1 -1
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;
|
|
@@ -1198,7 +1262,7 @@ export class CcDiscordBot {
|
|
|
1198
1262
|
return;
|
|
1199
1263
|
try {
|
|
1200
1264
|
session.claude.sendPrompt(stampPrompt(text));
|
|
1201
|
-
this.writeChatMessage("user", "cc-
|
|
1265
|
+
this.writeChatMessage("user", "cc-discord", text, channelId);
|
|
1202
1266
|
}
|
|
1203
1267
|
catch (err) {
|
|
1204
1268
|
console.error(`[forwardNotification:${channelId}] failed:`, err.message);
|
package/dist/notifier.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { type EvalReport } from "./loop-manager.js";
|
|
|
15
15
|
import type { CcDiscordBot } from "./bot.js";
|
|
16
16
|
export interface ChatMessage {
|
|
17
17
|
id: string;
|
|
18
|
-
source: "discord" | "ui" | "claude" | "cc-tg";
|
|
18
|
+
source: "discord" | "ui" | "claude" | "cc-tg" | "cc-discord";
|
|
19
19
|
role: "user" | "assistant" | "tool";
|
|
20
20
|
content: string;
|
|
21
21
|
timestamp: string;
|
|
@@ -24,6 +24,7 @@ export interface ChatMessage {
|
|
|
24
24
|
export interface ParsedNotification {
|
|
25
25
|
text: string;
|
|
26
26
|
chatId?: number;
|
|
27
|
+
isCron: boolean;
|
|
27
28
|
/** Populated when the notification JSON contains an `eval_report` object. */
|
|
28
29
|
evalReport?: EvalReport;
|
|
29
30
|
}
|
package/dist/notifier.js
CHANGED
|
@@ -44,6 +44,7 @@ export function parseNotification(raw) {
|
|
|
44
44
|
let model;
|
|
45
45
|
let cost;
|
|
46
46
|
let chatId;
|
|
47
|
+
let isCron = false;
|
|
47
48
|
try {
|
|
48
49
|
const parsed = JSON.parse(raw);
|
|
49
50
|
// routing: absent/empty → all transports; non-empty → only listed transports
|
|
@@ -58,18 +59,20 @@ export function parseNotification(raw) {
|
|
|
58
59
|
cost = parsed.cost;
|
|
59
60
|
if (typeof parsed.chat_id === "number" && parsed.chat_id !== 0)
|
|
60
61
|
chatId = parsed.chat_id;
|
|
62
|
+
if (typeof parsed.is_cron === "boolean")
|
|
63
|
+
isCron = parsed.is_cron;
|
|
61
64
|
}
|
|
62
65
|
catch {
|
|
63
|
-
return { text };
|
|
66
|
+
return { text, isCron };
|
|
64
67
|
}
|
|
65
68
|
// Parse eval_report if present — this field is non-standard and not in NotificationPayload type
|
|
66
69
|
const evalReport = parseEvalReport(raw);
|
|
67
70
|
if (!driver)
|
|
68
|
-
return { text, chatId, evalReport: evalReport ?? undefined };
|
|
71
|
+
return { text, chatId, isCron, evalReport: evalReport ?? undefined };
|
|
69
72
|
const shortModel = shortenModelName(model ?? "", driver);
|
|
70
73
|
const badge = shortModel ? `${driver}:${shortModel}` : driver;
|
|
71
74
|
const costStr = cost != null ? ` cost: $${cost.toFixed(3)}` : "";
|
|
72
|
-
return { text: `${text}\n[${badge}]${costStr}`, chatId, evalReport: evalReport ?? undefined };
|
|
75
|
+
return { text: `${text}\n[${badge}]${costStr}`, chatId, isCron, evalReport: evalReport ?? undefined };
|
|
73
76
|
}
|
|
74
77
|
/**
|
|
75
78
|
* Write a message to the chat log in Redis.
|
|
@@ -211,8 +214,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
|
|
|
211
214
|
const deliverTo = bot.getLoopThreadId(targetChannelId) ?? targetChannelId;
|
|
212
215
|
const chunks = splitLongMessage(text);
|
|
213
216
|
for (const chunk of chunks) {
|
|
214
|
-
bot.
|
|
215
|
-
log("warn", `meta-agent flush
|
|
217
|
+
bot.sendWithFileDetection(deliverTo, chunk).catch((err) => {
|
|
218
|
+
log("warn", `meta-agent flush sendWithFileDetection failed (ns=${ns}):`, err.message);
|
|
216
219
|
});
|
|
217
220
|
}
|
|
218
221
|
}
|
|
@@ -298,8 +301,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
|
|
|
298
301
|
bot.sendToChannelById(destChannelId, notification.text).catch((err) => {
|
|
299
302
|
log("warn", `notify list send failed (ns=${ns}):`, err.message);
|
|
300
303
|
});
|
|
301
|
-
if (
|
|
302
|
-
|
|
304
|
+
if (!notification.isCron && handleUserMessage) {
|
|
305
|
+
handleUserMessage(mainChannelId, notification.text);
|
|
303
306
|
}
|
|
304
307
|
}
|
|
305
308
|
if (remaining > 0) {
|
|
@@ -358,8 +361,8 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
|
|
|
358
361
|
bot.sendToChannelById(deliverTo, notification.text).catch((err) => {
|
|
359
362
|
log("warn", `notify send failed (ns=${ns}):`, err.message);
|
|
360
363
|
});
|
|
361
|
-
if (
|
|
362
|
-
|
|
364
|
+
if (!notification.isCron && handleUserMessage) {
|
|
365
|
+
handleUserMessage(mainChannelId, notification.text);
|
|
363
366
|
}
|
|
364
367
|
}
|
|
365
368
|
else {
|