01What are conversation schemas?
A conversation schema is a set of steps. Each step asks a question, optionally checks the answer, and names the next step. Typical uses:
- Multi-step forms and onboarding flows
- Booking and reservation systems
- Customer support workflows
- Survey and questionnaire bots
- Interactive tutorials
02Basic example
A hotel booking flow. Each user answer is passed to processResponse() as a string. Structured answers are passed as JSON, so the schema can parse them:
import { ConversationSchema } from '@lov3kaizen/agentsea-core';
import { z } from 'zod';
const bookingSchema = new ConversationSchema({
name: 'hotel-booking',
description: 'Hotel reservation conversation',
startStep: 'destination',
steps: [
{
id: 'destination',
prompt: 'Where would you like to stay?',
schema: z.object({ city: z.string().min(2) }),
next: 'dates',
},
{
id: 'dates',
prompt: 'What are your check-in and check-out dates?',
schema: z.object({
checkIn: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
checkOut: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
}),
validation: (response) => {
const { checkIn, checkOut } = response as { checkIn: string; checkOut: string };
return new Date(checkOut) > new Date(checkIn);
},
errorMessage: 'Check-out must be after check-in',
next: 'guests',
},
{
id: 'guests',
prompt: 'How many guests?',
schema: z.object({
adults: z.number().min(1).max(10),
children: z.number().min(0).max(5),
}),
next: 'confirm',
},
{
id: 'confirm',
prompt: 'Please review your booking details and confirm.',
onComplete: async (response, state) => {
console.log('Booking confirmed:', state.data);
},
// no next: the conversation ends here
},
],
onComplete: async (state) => {
console.log('Conversation completed!', state);
},
});
const first = await bookingSchema.processResponse(JSON.stringify({ city: 'San Francisco' }));
console.log(first.nextPrompt); // "What are your check-in and check-out dates?"
const second = await bookingSchema.processResponse(
JSON.stringify({ checkIn: '2025-12-01', checkOut: '2025-12-05' }),
);
console.log(second.nextPrompt); // "How many guests?"Warning
validation function still runs on the stored value. Check the stored values in your own code until this is fixed.03Using with agents
ConversationManager wraps a schema and an agent. Each processMessage() call first runs the agent on the message, then passes the raw message to the schema. The agent's reply is returned as aiResponse, but it does not change the stored data.
import { Agent, AnthropicProvider, ConversationManager, ToolRegistry } from '@lov3kaizen/agentsea-core';
const agent = new Agent(
{
name: 'booking-agent',
description: 'Helps users book a hotel',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
systemPrompt: 'You are a helpful hotel booking assistant.',
},
new AnthropicProvider(),
new ToolRegistry(),
);
const manager = new ConversationManager(agent, bookingSchema, 'user-123');
// start() is synchronous
const start = manager.start();
console.log(start.prompt); // "Where would you like to stay?"
const reply = await manager.processMessage('I want to book a hotel in San Francisco');
console.log(reply.response); // the next prompt, or "Conversation complete"
console.log(reply.isComplete);
console.log(reply.aiResponse); // the agent's reply to the messagestart()processMessage(message)response, isComplete, state and aiResponse.getState(), getHistory()reset()export(), import(json)04Builder pattern
ConversationSchemaBuilder sets up the same schema with chained calls:
import { ConversationSchemaBuilder } from '@lov3kaizen/agentsea-core';
const schema = new ConversationSchemaBuilder()
.name('support-flow')
.description('Customer support conversation')
.startAt('category')
.addStep({
id: 'category',
prompt: 'What type of issue are you experiencing?',
next: (response) => {
if (String(response).includes('billing')) return 'billing';
if (String(response).includes('technical')) return 'technical';
return 'general';
},
})
.addStep({ id: 'billing', prompt: 'Please describe your billing issue.', next: 'resolve' })
.addStep({ id: 'technical', prompt: 'Please describe your technical issue.', next: 'resolve' })
.addStep({ id: 'general', prompt: 'How can I help you today?', next: 'resolve' })
.addStep({ id: 'resolve', prompt: 'Is there anything else I can help with?' })
.onComplete(async (state) => {
console.log('Support ticket created:', state.data);
})
.build();05Dynamic routing
A step's next can be a function. It receives the parsed response and the state, and returns the ID of the next step:
const schema = new ConversationSchema({
name: 'product-quiz',
startStep: 'budget',
steps: [
{
id: 'budget',
prompt: 'What is your budget?',
next: (response) => {
const budget = Number.parseInt(String(response), 10);
if (budget < 1000) return 'basic-options';
if (budget < 3000) return 'mid-options';
return 'premium-options';
},
},
{ id: 'basic-options', prompt: 'Here are our budget-friendly options...', next: 'finalize' },
{ id: 'mid-options', prompt: 'Here are our mid-range options...', next: 'finalize' },
{ id: 'premium-options', prompt: 'Here are our premium options...', next: 'finalize' },
{ id: 'finalize', prompt: 'Would you like to proceed?' },
],
});06Schema validation
A step can set a Zod schema, a custom validation function and an errorMessage. validation may be async and must return a boolean:
import { z } from 'zod';
const step = {
id: 'user-info',
prompt: 'Please provide your information.',
schema: z.object({
email: z.string().email('Invalid email format'),
age: z.number().int().min(18, 'Must be 18 or older').max(120, 'Invalid age'),
phone: z.string().regex(/^\+?[1-9]\d{1,14}$/, 'Invalid phone format'),
}),
validation: async (response) => {
const { email } = response as { email: string };
return !(await checkEmail(email)); // false when the email is already registered
},
errorMessage: 'Email already registered',
next: 'confirm',
};07State management
Read and manage the conversation state at any point:
// Current state
const state = schema.getState();
console.log(state.currentStep); // current step ID
console.log(state.data); // collected data, keyed by step ID
console.log(state.history); // conversation turns
// Formatted transcript
console.log(schema.getFormattedHistory());
// Go back to the first step
schema.reset();
// Save and restore the state as a JSON string
const exported = schema.exportState();
localStorage.setItem('conversation', exported); // browser example; use your own store on the server
const saved = localStorage.getItem('conversation');
if (saved) schema.importState(saved);08Callbacks
Run your own code at each stage:
step.onComplete(response, state)config.onComplete(state)config.onError(error, state)const schema = new ConversationSchema({
name: 'payment-flow',
startStep: 'amount',
steps: [
{
id: 'amount',
prompt: 'How much would you like to pay?',
onComplete: async (response) => {
console.log('Amount entered:', response);
},
next: 'method',
},
{
id: 'method',
prompt: 'Choose a payment method.',
onComplete: async (response) => {
await validatePaymentMethod(response);
},
next: 'process',
},
{
id: 'process',
prompt: 'Processing payment...',
onComplete: async (_response, state) => {
await processPayment(state.data.amount, state.data.method);
},
},
],
onComplete: async (state) => {
await sendReceipt(state.data);
},
onError: async (error, state) => {
console.error('Payment error:', error);
await logError(error, state);
},
});09Error handling
processResponse() returns a result object. A failed validation sets success to false, with the step's errorMessage, and the same prompt comes back. Exceptions are caught and reported the same way, and onError runs.
const result = await schema.processResponse(userInput);
if (!result.success) {
console.log(result.message); // error message to show the user
console.log(result.nextPrompt); // the prompt to ask again
} else if (result.isComplete) {
console.log('Conversation completed');
} else {
console.log(result.nextPrompt);
}10Best practices
- Clear prompts: say exactly what information each step needs.
- Helpful errors: give an
errorMessagethat tells the user how to fix the answer. - Check the data yourself: until schema checks are enforced, validate the stored values in
onCompleteor before you act on them. - Save state: export the state after each turn so a user can resume.
- Side effects in callbacks: put API calls and database writes in step or flow callbacks.
- Test every path: run each branch and each error case.
11Complete example
A restaurant reservation flow. Each answer is sent as JSON, so the step schemas parse it:
import { ConversationSchema, ConversationManager, Agent, AnthropicProvider, ToolRegistry } from '@lov3kaizen/agentsea-core';
import { z } from 'zod';
const reservationSchema = new ConversationSchema({
name: 'restaurant-reservation',
startStep: 'date',
steps: [
{
id: 'date',
prompt: 'What date would you like to reserve?',
schema: z.object({ date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/) }),
validation: (response) => {
const { date } = response as { date: string };
return new Date(date) >= new Date();
},
errorMessage: 'Please select a future date',
next: 'time',
},
{
id: 'time',
prompt: 'What time would you prefer?',
schema: z.object({
time: z.enum(['11:00', '12:00', '13:00', '18:00', '19:00', '20:00']),
}),
next: 'party-size',
},
{
id: 'party-size',
prompt: 'How many people?',
schema: z.object({ size: z.number().min(1).max(12) }),
next: 'special-requests',
},
{
id: 'special-requests',
prompt: 'Any special requests or dietary requirements?',
next: 'confirm',
},
{
id: 'confirm',
prompt: 'Please confirm your reservation.',
onComplete: async (response, state) => {
await createReservation({
date: (state.data.date as { date: string }).date,
time: (state.data.time as { time: string }).time,
size: (state.data['party-size'] as { size: number }).size,
notes: response,
});
},
},
],
onComplete: async (state) => {
await sendConfirmationEmail(state.data);
},
});
const agent = new Agent(
{
name: 'reservation-agent',
description: 'Helps with restaurant reservations',
model: 'claude-sonnet-4-6',
provider: 'anthropic',
},
new AnthropicProvider(),
new ToolRegistry(),
);
const manager = new ConversationManager(agent, reservationSchema);
async function handleMessage(userMessage: string) {
const result = await manager.processMessage(userMessage);
return result.response;
}