103 lines
No EOL
3.6 KiB
JavaScript
103 lines
No EOL
3.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.routeMessage = routeMessage;
|
|
const runner_1 = require("./agent/runner");
|
|
const DISCORD_MAX_LENGTH = 2000;
|
|
/**
|
|
* Splits a message at line breaks so each chunk stays under the Discord
|
|
* character limit. If a single line exceeds the limit, it is split at
|
|
* the character boundary as a last resort.
|
|
*/
|
|
function splitMessage(text) {
|
|
if (text.length <= DISCORD_MAX_LENGTH) {
|
|
return [text];
|
|
}
|
|
const chunks = [];
|
|
const lines = text.split("\n");
|
|
let current = "";
|
|
for (const line of lines) {
|
|
// If adding this line would exceed the limit, flush current chunk
|
|
if (current.length + line.length + 1 > DISCORD_MAX_LENGTH) {
|
|
if (current.length > 0) {
|
|
chunks.push(current);
|
|
current = "";
|
|
}
|
|
// Handle lines that are themselves too long
|
|
if (line.length > DISCORD_MAX_LENGTH) {
|
|
let remaining = line;
|
|
while (remaining.length > DISCORD_MAX_LENGTH) {
|
|
chunks.push(remaining.slice(0, DISCORD_MAX_LENGTH));
|
|
remaining = remaining.slice(DISCORD_MAX_LENGTH);
|
|
}
|
|
if (remaining.length > 0) {
|
|
current = remaining;
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
current += (current.length > 0 ? "\n" : "") + line;
|
|
}
|
|
if (current.length > 0) {
|
|
chunks.push(current);
|
|
}
|
|
return chunks;
|
|
}
|
|
/**
|
|
* Routes a Discord message to the correct agent based on channel_id.
|
|
* Returns true if the message was handled, false if the channel is not
|
|
* mapped to any agent.
|
|
*/
|
|
async function routeMessage(message, db, config, managementChannelId) {
|
|
const channelId = message.channelId;
|
|
// Ignore messages in the management channel (commands only)
|
|
if (channelId === managementChannelId) {
|
|
return false;
|
|
}
|
|
// Look up workspace for this channel
|
|
const workspace = db.getWorkspaceByChannelId(channelId);
|
|
if (!workspace) {
|
|
return false;
|
|
}
|
|
const channel = message.channel;
|
|
// Show typing indicator while the agent works
|
|
const typingInterval = setInterval(() => {
|
|
channel.sendTyping().catch(() => { });
|
|
}, 5000);
|
|
// Send immediately as well
|
|
await channel.sendTyping().catch(() => { });
|
|
try {
|
|
// Load recent conversation history for context
|
|
const history = db.getConversationHistory(workspace.id, 30);
|
|
// Run the agent
|
|
const response = await (0, runner_1.runAgent)({
|
|
workspacePath: workspace.workspace_path,
|
|
channelName: channel.name,
|
|
userMessage: message.content,
|
|
conversationHistory: history,
|
|
});
|
|
// Store user message
|
|
db.addConversation(workspace.id, message.id, "user", message.content, message.author.username);
|
|
// Split and send response
|
|
const chunks = splitMessage(response);
|
|
let firstMsgId = null;
|
|
for (const chunk of chunks) {
|
|
const sent = await channel.send(chunk);
|
|
if (!firstMsgId) {
|
|
firstMsgId = sent.id;
|
|
}
|
|
}
|
|
// Store assistant response (use first message id for dedup)
|
|
if (firstMsgId) {
|
|
db.addConversation(workspace.id, firstMsgId, "assistant", response, null);
|
|
}
|
|
}
|
|
catch (error) {
|
|
const msg = error instanceof Error ? error.message : "Unbekannter Fehler";
|
|
await channel.send(`Fehler bei der Verarbeitung: ${msg}`).catch(() => { });
|
|
}
|
|
finally {
|
|
clearInterval(typingInterval);
|
|
}
|
|
return true;
|
|
}
|
|
//# sourceMappingURL=router.js.map
|