Copy
import { createWorkflow, Agent, openai } from '@runflow-ai/sdk';
import { z } from 'zod';
// Qualification agent
const qualifierAgent = new Agent({
name: 'Lead Qualifier',
instructions: 'Analyze lead data and assign a score (1-10) with reasoning.',
model: openai('gpt-4o-mini'),
});
// Sales copy agent
const copywriterAgent = new Agent({
name: 'Sales Copywriter',
instructions: 'Write a personalized email for the lead based on their profile.',
model: openai('gpt-4o'),
});
// Complete workflow
const salesWorkflow = createWorkflow({
id: 'lead-to-deal',
inputSchema: z.object({
leadEmail: z.string().email(),
leadName: z.string(),
company: z.string(),
notes: z.string(),
}),
outputSchema: z.any(),
})
// Step 1: Qualify the lead
.agent('qualify', qualifierAgent, {
promptTemplate: `Analyze this lead:
Name: {{input.leadName}}
Company: {{input.company}}
Notes: {{input.notes}}
Provide score (1-10) and reasoning.`,
})
// Step 2: Conditional - Only proceed if score >= 7
.condition(
'check-score',
(ctx) => {
const score = parseInt(ctx.stepResults.get('qualify').text.match(/\d+/)?.[0] || '0');
return score >= 7;
},
// High quality lead path
[
// Create contact in HubSpot
{
id: 'create-contact',
type: 'connector',
config: {
connector: 'hubspot',
resource: 'contacts',
action: 'create',
parameters: {
email: '{{input.leadEmail}}',
firstname: '{{input.leadName}}',
company: '{{input.company}}',
lifecyclestage: 'lead',
},
},
},
// Generate personalized email
{
id: 'write-email',
type: 'agent',
config: {
agent: copywriterAgent,
promptTemplate: `Write a personalized sales email for:
Name: {{input.leadName}}
Company: {{input.company}}
Qualification: {{qualify.text}}`,
},
},
],
// Low quality lead path
[
{
id: 'log-rejected',
type: 'function',
config: {
execute: async (input, ctx) => {
console.log(`Lead ${input.leadName} rejected - Low score`);
return { rejected: true };
},
},
},
]
)
.build();
// Execute workflow
const result = await salesWorkflow.execute({
leadEmail: 'john@techcorp.com',
leadName: 'John Smith',
company: 'TechCorp Inc',
notes: 'Interested in enterprise plan. Budget: $10k/month. Timeline: Q1 2025.',
});
console.log('Workflow completed:', result);
Features
- ✅ Lead Qualification: AI analyzes and scores leads
- ✅ Conditional Logic: Only processes high-quality leads
- ✅ CRM Integration: Creates contacts automatically
- ✅ Personalized Emails: Generates custom sales copy