https://www.boundaryml.com logo
Code reads like this ```ts function buildFlow() { ...
# general
b
Code reads like this
Copy code
ts
function buildFlow() {
  const flow = new RestateFlow({ input: 'User Message' })

  // 1. Route: does the AI need tools, or can it respond directly?
  const route = flow.add<{ action: 'respond' | 'tool_call' | 'clarify' }>('Agentic\nRouter', services.ai, 'route')

  // 2. Tool call path — loops back to router until it says 'respond'
  const executeTool = flow.add('Execute\nTool', services.ai, 'executeTool')
    .dependsOn(route)
    .if('Tool Call', r => r.get(route).action === 'tool_call')
    .loop(route)

  // 3. Generate AI response — runs when router says 'respond'
  const generate = flow.add('LLM\nStream Tokens', services.ai, 'generate')
    .dependsOn(route)
    .if('Respond', r => r.get(route).action === 'respond')

  // 4. Persist final message to conversation VO
  const commit = flow.add('Commit\nMessage', objects.conversation, 'commitMessage')
    .dependsOn(generate)

  // 5. Human sends next message — or ends the conversation
  const nextMessage = flow.promise<{ text: string } | { end: true }>('User\nMessage', 'nextMessage')
    .dependsOn(commit)
    .loop(route)

  return {
    flow,
    nodes: { route, executeTool, generate, commit, nextMessage },
  }
}