import { createFlow, START, END } from "@waniwani/sdk/mcp";
import { z } from "zod";
import { crm } from "./crm";
export const salesFunnel = createFlow({
id: "sales_funnel",
title: "Sales Funnel",
description: `Use when a visitor wants to learn more about the product, evaluate it for their team, or buy. Qualify intent first, then capture the right lead information for the stage.`,
state: {
intent: z.enum(["learn", "evaluate", "buy"]).describe("Stage of buyer journey"),
email: z.string().describe("Work email"),
company: z.string().optional().describe("Company name"),
role: z.string().optional().describe("Role at company"),
teamSize: z.string().optional().describe("Team size (e.g. '1-10', '11-50', '50+')"),
leadId: z.string().optional().describe("CRM lead ID (set after push)"),
},
})
.addNode({
id: "qualify_intent",
label: "Qualify intent",
run: ({ interrupt }) =>
interrupt({
intent: {
question: "What brings you here today?",
suggestions: [
"Just learning about the product",
"Evaluating for my team",
"Ready to buy",
],
},
}),
})
.addNode({
id: "capture_email_only",
label: "Capture email",
run: ({ interrupt }) =>
interrupt({ email: { question: "What's your work email?" } }),
})
.addNode({
id: "capture_company_role",
label: "Capture company + role",
run: ({ interrupt }) =>
interrupt({
email: { question: "Work email?" },
company: { question: "Which company?" },
role: { question: "What's your role?" },
}),
})
.addNode({
id: "capture_full_lead",
label: "Capture full lead",
run: ({ interrupt }) =>
interrupt({
email: { question: "Work email?" },
company: { question: "Which company?" },
role: { question: "What's your role?" },
teamSize: { question: "How big is your team?" },
}),
})
.addNode({
id: "push_to_crm",
label: "Push to CRM",
run: async ({ state }) => {
const lead = await crm.createLead({
email: state.email,
company: state.company,
role: state.role,
teamSize: state.teamSize,
stage: state.intent,
});
return { leadId: lead.id };
},
})
.addEdge(START, "qualify_intent")
.addConditionalEdge("qualify_intent", (state) => {
if (state.intent === "learn") return "capture_email_only";
if (state.intent === "evaluate") return "capture_company_role";
return "capture_full_lead";
})
.addEdge("capture_email_only", "push_to_crm")
.addEdge("capture_company_role", "push_to_crm")
.addEdge("capture_full_lead", "push_to_crm")
.addEdge("push_to_crm", END)
.compile();