Skip to main content

Complex E-Commerce Workflow

const workflow = createWorkflow({
  id: 'e-commerce-workflow',
  inputSchema: z.object({
    customerId: z.string(),
    query: z.string(),
  }),
  outputSchema: z.any(),
})
  // 1. Analyze intent
  .agent('analyzer', analyzerAgent, {
    promptTemplate: 'Analyze customer query: {{input.query}}',
  })

  // 2. Load customer data in parallel
  .parallel([
    createFunctionStep('load-profile', async (input, ctx) => {
      return await loadCustomerProfile(input.customerId);
    }),
    createFunctionStep('load-orders', async (input, ctx) => {
      return await loadCustomerOrders(input.customerId);
    }),
    createFunctionStep('search-products', async (input, ctx) => {
      return await searchProducts(ctx.stepResults.get('analyzer').text);
    }),
  ])

  // 3. Conditional: Sales vs Support
  .condition(
    'route',
    (ctx) => ctx.stepResults.get('analyzer').text.includes('buy'),
    // Sales path
    [
      createAgentStep('sales', salesAgent),
      createConnectorStep('update-crm', 'hubspot', 'contacts', 'update', {
        contactId: '{{input.customerId}}',
        lastContact: new Date().toISOString(),
      }),
    ],
    // Support path
    [
      createAgentStep('support', supportAgent),
      createConnectorStep('create-ticket', 'hubspot', 'tickets', 'create', {
        subject: '{{analyzer.text}}',
      }),
    ]
  )

  // 4. Send response
  .agent('responder', responderAgent, {
    promptTemplate: 'Create final response based on context',
  })
  .build();

const result = await workflow.execute({
  customerId: 'customer_123',
  query: 'I want to buy a laptop',
});

Next Steps