Tiptap Editor 3.0 Beta is out. Start here

AI Agent lifecycle

The AI Agent lifecycle starts when provider.run() is called, and ends when the AI Agent has completed the task, needs user review, or encounters an error.

Lifecycle steps

  1. Initialization: The lifecycle begins when you call provider.run() after adding a user message.
  2. Processing: The AI Agent calls the AI model. The AI responds with a message and a tool call.
  3. Tool execution: The AI Agent calls the tool to read or modify the document.
  4. Loop: The AI Agent calls the AI again and executes the next tool, until a termination condition is met.
  5. Completion: The lifecycle ends when the AI Agent has completed the task, needs user review, or encounters an error.

Controlling the lifecycle

You can control the AI Agent lifecycle using these methods:

// Start the lifecycle
provider.run()

// Stop the current lifecycle
provider.stop()

By default, the AI Agent doesn't automatically run after adding a user message. You need to explicitly call run():

// Add a message and run the AI Agent
provider.addUserMessage('Format this document with proper headings')
provider.run()

Handling completion

Listen to the runFinished event to be notified when the lifecycle is completed.

// Subscribe to the runFinished event
provider.on('runFinished', (context) => {
  console.log('AI Agent lifecycle completed')
  showCompletionNotification('The AI Agent has completed the task')
})

Statuses

During its lifecycle, the AI Agent transitions through different statuses:

  1. idleloading: When run() is called
  2. loadingreviewingToolCall: When a tool call requires user review
  3. loadingloadingError: When an error occurs
  4. loadingidle: When one of these conditions is met:
    • A final tool call is executed. A final tool call is one that has the isFinal property set to true in the tool call handler object.
    • The LLM does not respond with a tool call.
    • provider.stop() is called
    • Another lifecycle is started with provider.run(), before the current lifecycle has finished.

Running AI Agents concurrently

In its current version, the AI Agent extension does not support running multiple AI Agents within the same editor. If a lifecycle is already in progress when provider.run() is called, the current lifecycle is interrupted before the new lifecycle starts.

To run multiple AI Agents at the same time, instantiate multiple editors and AI Agent providers.