import { Nango } from "@nangohq/node";
// Initialize the Nango client
const nango = new Nango({ secretKey: process.env.NANGO_SECRET_KEY! });
export async function runLLMAgent(modelClient: any) {
const userId = "user1";
const integrationId = "hubspot";
// Step 1: Ensure the user is authorized
console.log("🔒 Checking Hubspot authorization...");
let connectionId = (await nango.listConnections({ userId })).connections[0]?.connection_id;
// Step 2: If the user is not authorized, redirect to the auth flow.
if (!connectionId) {
console.log("User not authorized, starting Hubspot auth flow...");
const session = await nango.createConnectSession({
allowed_integrations: [integrationId],
end_user: { id: userId },
}); // Handle API auth via Nango.
// Redirect the user to a page to authorize Hubspot.
// Alternatively, you can embed Nango's Frontend SDK in your app.
console.log(`Please authorize Hubspot here: ${session.data.connect_link}`);
const connection = await nango.waitForConnection(integrationId, userId);
connectionId = connection?.connection_id;
if (!connectionId) throw new Error("Auth flow failed");
}
// Step 3: Send a prompt to your model.
console.log("🤖 Agent running...");
const response = await modelClient.generate({
model: "your-llm-model-id",
input: "Get the current Hubspot user info using the who_am_i tool.",
tools: [
{
name: "who_am_i",
description: "Fetch the current Hubspot user profile.",
parameters: { type: "object", properties: {} },
},
],
});
// Check if the model requested a tool call
const toolCall = response?.toolCalls?.[0] || response?.tool_call || null;
if (toolCall?.name === "who_am_i") {
console.log("🧩 Model decided to call the 'who_am_i' tool...");
try {
// Step 4: Execute the requested tool with Nango.
const userInfo = await nango.triggerAction("hubspot", connectionId, "whoami");
console.log("✅ Retrieved Hubspot user info:", userInfo);
} catch (error: any) {
console.error("❌ Nango API error:", error.response?.data?.error || error);
}
} else {
// Handle plain text or other model responses
const textOutput = response?.text || response?.output_text || JSON.stringify(response);
console.log("🗣️ Model response:", textOutput);
}
}