main
 1import { loadConfig } from "./config.js";
 2import { XmppClient } from "./xmpp/client.js";
 3import { SessionManager } from "./agent/session.js";
 4import { AgentRunner } from "./agent/runner.js";
 5import { createToolRegistry } from "./tools/index.js";
 6import { bareJid } from "./xmpp/types.js";
 7
 8async function main(): Promise<void> {
 9  console.log("Daneel - XMPP Research Bot");
10  console.log("==========================\n");
11
12  // Load configuration
13  const config = loadConfig();
14  console.log(`XMPP JID: ${config.xmpp.jid}`);
15  console.log(`Owner JID: ${config.xmpp.ownerJid}`);
16  console.log(`Default model: ${config.llm.defaultModel.provider}/${config.llm.defaultModel.model}`);
17  console.log(`Data directory: ${config.paths.dataDir}`);
18
19  // Initialize components
20  const sessionManager = new SessionManager(config.paths.dataDir);
21  const toolRegistry = createToolRegistry(config);
22
23  // Map of JID -> AgentRunner
24  const agents = new Map<string, AgentRunner>();
25
26  function getOrCreateAgent(jid: string): AgentRunner {
27    const bare = bareJid(jid);
28    let agent = agents.get(bare);
29    if (!agent) {
30      agent = new AgentRunner(bare, config, sessionManager, toolRegistry);
31      agents.set(bare, agent);
32    }
33    return agent;
34  }
35
36  // Create XMPP client
37  const xmpp = new XmppClient(config);
38
39  // Set up message handler
40  xmpp.onMessage(async (message) => {
41    console.log(`[${new Date().toISOString()}] Message from ${bareJid(message.from)}: ${message.body.slice(0, 50)}...`);
42
43    const agent = getOrCreateAgent(message.from);
44    const response = await agent.processMessage(message.body);
45
46    await xmpp.sendMessage(message.from, response);
47    console.log(`[${new Date().toISOString()}] Response sent (${response.length} chars)`);
48  });
49
50  // Graceful shutdown
51  const shutdown = async (signal: string): Promise<void> => {
52    console.log(`\nReceived ${signal}, shutting down...`);
53    await xmpp.stop();
54    process.exit(0);
55  };
56
57  process.on("SIGINT", () => shutdown("SIGINT"));
58  process.on("SIGTERM", () => shutdown("SIGTERM"));
59
60  // Start XMPP client
61  console.log("\nConnecting to XMPP server...");
62  await xmpp.start();
63  console.log("Bot is running. Press Ctrl+C to stop.\n");
64}
65
66main().catch((err) => {
67  console.error("Fatal error:", err);
68  process.exit(1);
69});