Build a Vue Chat UI for Long-Running AI Agents with Graceful Aborts

Building a chat interface in Vue is easy. Building a chat interface for an autonomous AI agent that can run for three minutes, write files, call external APIs, generate sub-tasks, and occasionally hallucinate an infinite loop is an entirely different engineering challenge.

If you have ever shipped an LLM-powered feature to production, you have likely seen this exact failure mode: a user clicks "Run Agent," the LLM enters a recursive tool-calling loop, tokens start burning at $0.03 a second, and your Vue UI locks up tight. The user mashes the browser back button, refreshes the page, and your backend is left orphaned, burning server resources and hitting rate limits because there was no clean way to send an abort signal.

Streaming text from a Large Language Model is a solved problem. Server-Sent Events (SSE) or WebSockets make it trivial to pipe tokens into a reactive ref. But giving users granular control over a stateful AI agent—specifically the ability to Stop, Retry, and Resume mid-stream—requires careful choreography between your Vue 3 composition functions, your HTTP transport layer, and your agent's state machine.

In this deep dive, we are going to build a production-grade Vue AI agent control interface. We will look past the basic fetch wrapper and tackle the messy realities of stream abortion, optimistic state recovery, idempotent retry mechanisms, and state rehydration after a network drop.


📋 Table of Contents


The Anatomy of an AI Agent Stream in Vue

Most tutorials show you how to read a ReadableStream using response.body.getReader(). While that works for a simple chatbot answering trivia, an agent interface deals with structured events.

An agent stream doesn't just send text tokens; it sends JSON payloads indicating tool execution starts, tool outputs, intermediate thoughts, errors, and final completions.

[User Prompt] 
    ↓
[Vue Frontend] --(POST /api/agent)--► [Backend Orchestrator]
                                            │
◄──(SSE: token)─────────────────────────────┤ (Thinking...)
◄──(SSE: tool_start: "search_db")──────────┤
◄──(SSE: tool_output: "{...}")──────────────┤
◄──(SSE: token)─────────────────────────────┤ (Synthesizing...)

When a user hits Stop, you aren't just canceling a network request. You are sending an out-of-band cancellation signal to the orchestrator so it can release database locks, terminate active child processes, and persist the agent's exact state to disk so it can be Resumed later.

Let's look at how to implement this cleanly in Vue 3 using Composition API.


1. Graceful Abort Architecture Using AbortController

Scenario: Your user triggers a complex research agent. Halfway through compiling a 50-page report, the agent gets stuck in a loop querying a slow vector database. The user clicks the Stop button, but the UI keeps updating because the browser is still processing buffered chunks from the TCP stream.

Why it matters: Calling abort() on a standard fetch request cancels the network pipe, but it leaves the backend server completely unaware that the client has disconnected. For AI agents, this means LLM API calls keep firing and vector DB queries keep running until completion, costing you money and resources.

Solution: Implement a dual-layer cancellation pattern. The frontend uses an AbortController to sever the connection immediately, while simultaneously firing a lightweight, high-priority beacon or DELETE request to instruct the backend agent orchestrator to kill the worker process.

Code:

// useAgentStream.ts
import { ref, shallowRef } from 'vue'

export function useAgentStream() {
  const isRunning = ref(false)
  const messages = shallowRef<Array<any>>([])
  let abortController: AbortController | null = null

  const startAgent = async (prompt: string, agentId: string) => {
    isRunning.value = true
    abortController = new AbortController()

    try {
      const response = await fetch(`/api/agents/${agentId}/run`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ prompt }),
        signal: abortController.signal
      })

      if (!response.body) throw new Error('Readable stream not supported')

      const reader = response.body.getReader()
      const decoder = new TextDecoder()

      while (true) {
        const { value, done } = await reader.read()
        if (done) break

        const chunk = decoder.decode(value, { stream: true })
        processStreamChunk(chunk)
      }
    } catch (err: any) {
      if (err.name === 'AbortError') {
        console.log('Agent stream aborted by user.')
      } else {
        console.error('Agent execution error:', err)
      }
    } finally {
      isRunning.value = false
      abortController = null
    }
  }

  const stopAgent = async (agentId: string) => {
    if (!abortController) return

    // 1. Cut the local network stream immediately
    abortController.abort()

    // 2. Inform the backend to terminate the worker process
    try {
      await fetch(`/api/agents/${agentId}/abort`, { method: 'POST' })
    } catch (err) {
      console.error('Failed to notify backend of abortion:', err)
    } finally {
      isRunning.value = false
    }
  }

  const processStreamChunk = (chunk: string) => {
    // Parse SSE lines and update reactive state
    const lines = chunk.split('\n\n')
    for (const line of lines) {
      if (line.startsWith('data: ')) {
        const data = JSON.parse(line.replace('data: ', ''))
        messages.value = [...messages.value, data]
      }
    }
  }

  return { isRunning, messages, startAgent, stopAgent }
}

Why this works: By decoupling the local stream termination (abortController.abort()) from the remote process termination (/api/agents/:id/abort), the UI responds instantly (< 10ms) without waiting for HTTP round-trips over a congested network.

💡 Practical note: Always wrap your stream parsing logic in a try/catch block looking specifically for err.name === 'AbortError'. If you treat manual abortions as unexpected errors, your error boundary components will flash red error banners every time a user hits stop.


2. Idempotent Retries Without Duplicate Tool Execution

Scenario: Your AI agent is executing a multi-step workflow. Step 1: Write code. Step 2: Run tests. Step 3: Deploy to staging. Halfway through Step 2, a transient gateway timeout (504 Gateway Time-out) drops the connection. The user clicks Retry.

Why it matters: If your retry logic simply replays the entire prompt history to the LLM without tracking state, the model might decide to re-run Step 1 and Step 3 from scratch, resulting in duplicate infrastructure deployments or redundant database mutations.

Solution: Assign a unique Execution Session ID and Step Checkpoint Hash to every interaction. When a retry is triggered, the frontend sends the last known valid state checkpoint, instructing the backend agent framework to skip already-completed tool executions and resume directly from the point of failure.

Code:

// AgentChatInterface.vue
<script setup lang="ts">
import { ref } from 'vue'
import { useAgentStream } from './useAgentStream'

const props = defineProps<{ agentId: string }>()
const { isRunning, messages, startAgent, stopAgent } = useAgentStream()

const lastCheckpointId = ref<string | null>(null)
const hasFailed = ref(false)

const handleRun = async (prompt: string) => {
  hasFailed.value = false
  try {
    await startAgent(prompt, props.agentId)
  } catch {
    hasFailed.value = true
  }
}

const handleRetry = async () => {
  if (!lastCheckpointId.value) return
  
  hasFailed.value = false
  // Send the checkpoint token so the backend bypasses completed tool calls
  try {
    await fetch(`/api/agents/${props.agentId}/retry`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ checkpointId: lastCheckpointId.value })
    })
  } catch (err) {
    hasFailed.value = true
    console.error('Retry failed:', err)
  }
}
</script>

Why this works: The backend agent framework maintains a transaction log of completed tool outputs. By passing the checkpointId, you prevent the agent from re-executing expensive or destructive side-effects like sending emails or writing production records.


3. State Rehydration and Resume After a Connection Drop

Scenario: A user closes their laptop or switches Wi-Fi networks while an agent is running. When they reopen the Vue application, the stream is dead, but the backend agent is still running in a background container processing data.

Why it matters: If your frontend state lives purely inside a volatile Vue ref, page refreshes destroy your connection context. Users hate losing their place in long-running operations.

Solution: On component mount, query the backend for any active agent sessions associated with the user. If an active session exists, re-establish an SSE connection starting from the last acknowledged sequence number (Last-Event-ID).

Code:

// useAgentRehydration.ts
import { onMounted, ref } from 'vue'

export function useAgentRehydration(agentId: string) {
  const activeSession = ref<any>(null)
  const isRecovering = ref(false)

  const checkActiveSession = async () => {
    isRecovering.value = true
    try {
      const res = await fetch(`/api/agents/${agentId}/active-session`)
      if (!res.ok) return
      
      const session = await res.json()
      if (session && session.status === 'running') {
        activeSession.value = session
        reconnectStream(session.id, session.lastSequenceId)
      }
    } finally {
      isRecovering.value = false
    }
  }

  const reconnectStream = (sessionId: string, lastSeqId: number) => {
    const eventSource = new EventSource(`/api/agents/stream/${sessionId}?lastEventId=${lastSeqId}`)
    
    eventSource.onmessage = (event) => {
      const data = JSON.parse(event.data)
      // Append to reactive state
      if (data.status === 'completed' || data.status === 'failed') {
        eventSource.close()
      }
    }

    eventSource.onerror = () => {
      console.warn('Reconnection stream dropped. Retrying in 3s...')
      eventSource.close()
      setTimeout(() => reconnectStream(sessionId, lastSeqId), 3000)
    }
  }

  onMounted(() => {
    checkActiveSession()
  })

  return { activeSession, isRecovering }
}

Why this works: Using native EventSource with the Last-Event-ID header allows standard HTTP infrastructure (like proxies and load balancers) to replay missed SSE frames automatically without custom protocol design.


4. Managing Asynchronous Tool-Call Interruption States

Scenario: Your AI agent hits a tool call that requires human confirmation—for example, "Delete production database table users?". The agent pauses execution and waits for user input. Your Vue UI needs to render a dangerous confirmation box, but the agent's internal state machine is frozen waiting for an HTTP POST response.

Why it matters: Standard chat interfaces assume messages are just text. Agent interfaces are state machines with distinct blocking states: IDLE, RUNNING, WAITING_FOR_INPUT, PAUSED, and ERROR. If your UI doesn't explicitly map to these states, users won't know why the agent stopped talking.

Solution: Define a strict finite state machine (FSM) type definition in TypeScript and bind your Vue component classes directly to agent interruption states.

┌───────┐      Start       ┌─────────┐      Tool Request      ┌──────────────────┐
│ IDLE  │ ───────────────► │ RUNNING │ ─────────────────────► │ WAITING_FOR_USER │
└───────┘                  └─────────┘                        └──────────────────┘
    ▲                           │                                       │
    │                           │ Stop                                  │ Approve / Reject
    │                           ▼                                       ▼
    │                      ┌─────────┐                         ┌──────────────────┐
    └───────────────────── │ STOPPED │                         │ RESUMING...      │
                           └─────────┘                         └──────────────────┘

Code:

// types/agent.ts
export type AgentState = 
  | 'IDLE' 
  | 'RUNNING' 
  | 'WAITING_FOR_INPUT' 
  | 'PAUSED' 
  | 'ERROR' 
  | 'COMPLETED';

export interface AgentInterruptionPayload {
  toolName: string;
  arguments: Record<string, any>;
  reason: string;
}
<!-- AgentControlPanel.vue -->
<script setup lang="ts">
import { computed, ref } from 'vue'
import type { AgentState, AgentInterruptionPayload } from './types/agent'

const currentState = ref<AgentState>('IDLE')
const interruptionData = ref<AgentInterruptionPayload | null>(null)

const isActionable = computed(() => currentState.value === 'WAITING_FOR_INPUT')

const handleUserDecision = async (approved: boolean) => {
  currentState.value = 'RUNNING'
  await fetch(`/api/agents/resolve-interruption`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ approved })
  })
}
</script>

<template>
  <div class="control-panel">
    <div v-if="isActionable" class="warning-banner bg-amber-500/10 border border-amber-500 p-4 rounded-lg">
      <h3 class="text-amber-400 font-bold">⚠️ Agent Requires Approval</h3>
      <p>The agent wants to execute: <code>{{ interruptionData?.toolName }}</code></p>
      <div class="flex gap-2 mt-3">
        <button @click="handleUserDecision(true)" class="bg-red-600 px-4 py-2 rounded text-white">Approve & Continue</button>
        <button @click="handleUserDecision(false)" class="bg-gray-700 px-4 py-2 rounded text-white">Reject</button>
      </div>
    </div>
  </div>
</template>

Why this works: Explicitly typing the agent's lifecycle states prevents edge cases where a user can click "Stop" or "Retry" while the agent is suspended waiting for tool authorization.


5. Optimistic UI Updates and Rollback Strategies

Scenario: A user types a prompt and hits enter. They want the message to appear in the chat stream instantly. However, if the backend rejects the prompt due to rate limits or policy violations, the optimistic message needs to vanish cleanly without leaving ghost elements in the Vue DOM.

Why it matters: Naive implementations push messages directly to the global messages array before the network request resolves. If the request fails, cleaning up that state often leads to messy array mutation bugs or UI flickering.

Solution: Use a temporary, distinct optimistic wrapper object that is either promoted to an official message upon success or discarded upon error.

Code:

const addMessageOptimistically = async (text: string) => {
  const tempId = `opt-${Date.now()}`
  const optimisticMessage = {
    id: tempId,
    role: 'user',
    content: text,
    status: 'pending' // Visual indicator: spinning loader or opacity-50
  }

  messages.value = [...messages.value, optimisticMessage]

  try {
    const res = await fetch('/api/chat', {
      method: 'POST',
      body: JSON.stringify({ prompt: text, tempId })
    })
    
    if (!res.ok) throw new Error('Failed to send')

    const serverData = await res.json()
    
    // Promote optimistic message to confirmed server state
    messages.value = messages.value.map(msg => 
      msg.id === tempId ? { ...serverData, status: 'confirmed' } : msg
    )
  } catch (err) {
    // Rollback: Remove the optimistic message entirely
    messages.value = messages.value.filter(msg => msg.id !== tempId)
    triggerErrorToast('Message failed to send. Please retry.')
  }
}

Why this works: Marking temporary items with a explicit status: 'pending' property allows your CSS transitions to animate the entry smoothly while giving you a precise handle for garbage collection if the request fails.


6. Handling Token Buffering and UI Rendering Jank

Scenario: Your AI agent streams 50 tokens per second. If your Vue reactivity system triggers a DOM re-render on every single token arrival, the browser main thread chokes, scroll anchoring breaks, and the UI becomes completely unresponsive.

Why it matters: Vue's reactivity is exceptionally fast, but updating the DOM 50 times a second creates massive layout thrashing, especially when rendering markdown blocks inside chat bubbles.

Solution: Implement a requestAnimationFrame-based batching buffer that flushes incoming token chunks to the reactive ref at a steady 60fps (roughly every 16ms).

Code:

// useBufferedStream.ts
import { ref } from 'vue'

export function useBufferedStream() {
  const displayedContent = ref('')
  let buffer = ''
  let animationFrameId: number | null = null

  const flushBuffer = () => {
    if (buffer.length > 0) {
      displayedContent.value += buffer
      buffer = ''
    }
    animationFrameId = requestAnimationFrame(flushBuffer)
  }

  const appendToStream = (incomingText: string) => {
    buffer += incomingText
    if (!animationFrameId) {
      animationFrameId = requestAnimationFrame(flushBuffer)
    }
  }

  const stopBuffering = () => {
    if (animationFrameId) {
      cancelAnimationFrame(animationFrameId)
      animationFrameId = null
    }
    // Flush remainder
    if (buffer.length > 0) {
      displayedContent.value += buffer
      buffer = ''
    }
  }

  return { displayedContent, appendToStream, stopBuffering }
}

Why this works: Instead of slamming Vue's reactive proxy with hundreds of updates per second, you accumulate chunks in memory and let requestAnimationFrame batch them into a single render tick. This keeps your CPU utilization low and your chat interface buttery smooth.


Production Architecture: Putting It All Together

When designing a robust agent interface, managing controls isn't just about writing event listeners—it's about building a predictable lifecycle around asynchronous distributed systems.

Here is how the components interact in a production deployment:

Control Action Frontend Behavior Transport Layer Backend Orchestrator
Stop Aborts AbortController, flips UI state to STOPPED POST /api/agents/:id/abort Signals background worker to kill active LLM / tool streams
Retry Dispatches last known checkpoint ID, clears error state POST /api/agents/:id/retry Restores session state from database, resumes execution graph
Resume Re-establishes SSE subscription with Last-Event-ID GET /api/agents/stream/:id Replays unacknowledged event buffer from Redis stream

By combining granular AbortController management, checkpoint-based retries, buffered stream rendering, and strict state machine typing, you transform a fragile wrapper around an LLM API into a reliable application that users can trust with complex, long-running workflows.

Post a Comment

Previous Post Next Post