import { createFlow, START, END } from "@waniwani/sdk/mcp";
import { z } from "zod";
import { crm, enrichment } from "./services";
export const leadGenFunnel = createFlow({
id: "lead_generation",
title: "Lead Generation",
description: "Use when a visitor expresses interest and we need to capture them as a lead. Collects email, role, and use case, then pushes to CRM.",
state: {
email: z.string().describe("Work email, used to identify the lead"),
role: z.string().describe("Role at company (e.g. 'PM', 'Engineering Manager')"),
useCase: z.string().describe("What they want to build or solve"),
company: z.string().optional().describe("Company name, enriched from email domain"),
leadId: z.string().optional().describe("CRM lead ID, set after push"),
},
})
.addNode({
id: "ask_email",
label: "Ask for email",
run: ({ interrupt }) =>
interrupt({
email: {
question: "What's your work email?",
validate: (email: string) => {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
throw new Error("Please share a valid work email.");
}
},
},
}),
})
.addNode({
id: "ask_role_and_use_case",
label: "Ask role + use case",
run: ({ interrupt }) =>
interrupt({
role: { question: "What's your role?" },
useCase: {
question: "What are you hoping to build?",
suggestions: [
"Customer support",
"Lead capture / sales",
"Internal tools",
"Something else",
],
},
}),
})
.addNode({
id: "lookup_company",
label: "Enrich from email",
hideFromFunnel: true,
run: async ({ state }) => {
const enriched = await enrichment.lookupByEmail(state.email);
return { company: enriched?.company };
},
})
.addNode({
id: "push_lead",
label: "Push to CRM",
run: async ({ state, waniwani }) => {
const lead = await crm.createLead({
email: state.email,
role: state.role,
useCase: state.useCase,
company: state.company,
});
waniwani?.identify(state.email!);
waniwani?.track.leadQualified({
externalId: lead.id,
email: state.email,
source: "mcp_chat",
});
return { leadId: lead.id };
},
})
.addEdge(START, "ask_email")
.addEdge("ask_email", "ask_role_and_use_case")
.addEdge("ask_role_and_use_case", "lookup_company")
.addEdge("lookup_company", "push_lead")
.addEdge("push_lead", END)
.compile();