Initial commit: Discord-Claude Gateway with event-driven agent runtime
This commit is contained in:
@@ -121,16 +121,32 @@ export class DiscordBot {
|
|||||||
|
|
||||||
private setupMessageHandler(): void {
|
private setupMessageHandler(): void {
|
||||||
this.client.on("messageCreate", (message: Message) => {
|
this.client.on("messageCreate", (message: Message) => {
|
||||||
if (shouldIgnoreMessage(message)) return;
|
console.log(`[DEBUG] Message received: "${message.content}" from ${message.author.tag} (bot: ${message.author.bot})`);
|
||||||
|
|
||||||
|
if (shouldIgnoreMessage(message)) {
|
||||||
|
console.log("[DEBUG] Ignoring bot message");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const botUser = this.client.user;
|
const botUser = this.client.user;
|
||||||
if (!botUser) return;
|
if (!botUser) {
|
||||||
|
console.log("[DEBUG] No bot user available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!message.mentions.has(botUser)) return;
|
if (!message.mentions.has(botUser)) {
|
||||||
|
console.log("[DEBUG] Message does not mention the bot");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const text = extractPromptFromMention(message.content, botUser.id);
|
const text = extractPromptFromMention(message.content, botUser.id);
|
||||||
if (!text) return;
|
console.log(`[DEBUG] Extracted prompt: "${text}"`);
|
||||||
|
if (!text) {
|
||||||
|
console.log("[DEBUG] Empty prompt after extraction, ignoring");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[DEBUG] Forwarding prompt to handler: "${text}" from channel ${message.channelId}`);
|
||||||
this.promptHandler?.({
|
this.promptHandler?.({
|
||||||
text,
|
text,
|
||||||
channelId: message.channelId,
|
channelId: message.channelId,
|
||||||
|
|||||||
@@ -92,8 +92,10 @@ export class GatewayCore {
|
|||||||
|
|
||||||
// 8. Register EventQueue processing handler
|
// 8. Register EventQueue processing handler
|
||||||
this.eventQueue.onEvent(async (event: Event) => {
|
this.eventQueue.onEvent(async (event: Event) => {
|
||||||
|
console.log(`[DEBUG] Processing event: type=${event.type}, id=${event.id}`);
|
||||||
try {
|
try {
|
||||||
const result = await this.agentRuntime.processEvent(event);
|
const result = await this.agentRuntime.processEvent(event);
|
||||||
|
console.log(`[DEBUG] Event result: responseText=${result.responseText?.length ?? 0} chars, error=${result.error ?? "none"}`);
|
||||||
|
|
||||||
if (result.responseText && result.targetChannelId) {
|
if (result.responseText && result.targetChannelId) {
|
||||||
const chunks = splitMessage(result.responseText);
|
const chunks = splitMessage(result.responseText);
|
||||||
@@ -107,7 +109,6 @@ export class GatewayCore {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error processing event:", error);
|
console.error("Error processing event:", error);
|
||||||
// Attempt to notify the channel if it's a message event
|
|
||||||
if (event.type === "message") {
|
if (event.type === "message") {
|
||||||
const payload = event.payload as MessagePayload;
|
const payload = event.payload as MessagePayload;
|
||||||
const errorMsg = formatErrorForUser(error);
|
const errorMsg = formatErrorForUser(error);
|
||||||
@@ -116,7 +117,6 @@ export class GatewayCore {
|
|||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// Decrement active query count for message events
|
|
||||||
if (event.type === "message") {
|
if (event.type === "message") {
|
||||||
this.activeQueryCount--;
|
this.activeQueryCount--;
|
||||||
}
|
}
|
||||||
@@ -125,6 +125,8 @@ export class GatewayCore {
|
|||||||
|
|
||||||
// 9. Wire DiscordBot.onPrompt() to create message events and enqueue them
|
// 9. Wire DiscordBot.onPrompt() to create message events and enqueue them
|
||||||
this.discordBot.onPrompt((prompt: Prompt) => {
|
this.discordBot.onPrompt((prompt: Prompt) => {
|
||||||
|
console.log(`[DEBUG] onPrompt called: "${prompt.text}" from channel ${prompt.channelId}`);
|
||||||
|
|
||||||
if (this.isShuttingDown) {
|
if (this.isShuttingDown) {
|
||||||
this.discordBot
|
this.discordBot
|
||||||
.sendMessage(prompt.channelId, "Gateway is shutting down. Please try again later.")
|
.sendMessage(prompt.channelId, "Gateway is shutting down. Please try again later.")
|
||||||
|
|||||||
Reference in New Issue
Block a user