const { conversationData: c = {}, recognizedObject: r = {} } = this; class Router { constructor() { r.router = { logs: [] }; if (c.router) { r.router.logs.push("Router Already Initialized"); } else { r.router.logs.push("Initializing Router"); c.router = { agents: [], nextAction: "Next", }; } } setNextAction(action) { c.router.nextAction = action; } getNextAction() { return c.router.nextAction; } hasNext() { return c.router.agents.some((agent) => agent.state === "submitted"); } next() { return c.router.agents.find((agent) => agent.state === "submitted"); } getActiveAgent() { return c.router.agents.find((agent) => agent.state === "active"); } log(message) { if (typeof message === "string" || message instanceof String) { r.router.logs.push("" + message); } else { r.router.logs.push(JSON.stringify(message)); } } async assignAgents() { const intents = [ { name: "Invoice_Agent", description: "Answers Questions about Invoices and Orders", priority: 1, }, { name: "Device_Agent", description: "Answers Questions about Devices, Models and Setup", priority: 1, }, ]; const { data } = await request.post("/test", { modelId: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", body: { max_tokens: 1024, anthropic_version: "bedrock-2023-05-31", system: [ { type: "text", text: `You are a helpful assistant, designed to classify user utterances and return all matched intents based on their descriptions.\nClassify the following user utterance and return **all matched intents** from the list below.\nIf the input does not match any intents, respond with "UNABLE TO CLASSIFY".\nThis is the list of available intents and their descriptions to match against:\n\n${intents .map((intent) => `- ${intent.name}: ${intent.description}`) .join( "\n" )}\n\nReturn the matched intents and the part of the input that matched each intent on their own line. For example:\n\nIntent A: partial utterance\nIntent B: partial utterance`, }, ], messages: [ { role: "user", content: [ { type: "text", text: recognizedObject.input, }, ], }, ], temperature: 1.0, }, credentials: { accessKeyId: "AKIAXSFOCMMUBXR67SUK", secretAccessKey: "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", }, }); r.router.logs.push({ agentClassification: data }); recognizedObject.classifier = data; const tool = data?.content?.[0]?.text; if (tool.includes("UNABLE TO CLASSIFY")) { r.router.logs.push("No intent matched"); } else { const list = tool.split("\n").map((line) => line.trim()); const results = list.map((line) => { const [intent, utterance] = line.split(":").map((part) => part.trim()); return { intent, utterance }; }); results.sort((a, b) => { const priorityA = intents.find((intent) => intent.name === a.intent)?.priority || 0; const priorityB = intents.find((intent) => intent.name === b.intent)?.priority || 0; return priorityA - priorityB; }); c.router.agents = results.map((agent) => ({ id: agent.intent, input: agent.utterance, state: "submitted", })); } } setState(id, state) { const agent = c.router.agents.find((agent) => agent.id === id); if (agent) { agent.state = state; r.router.logs.push(`router.setState: agent=${JSON.stringify(agent)}`); } } } return new Router();