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
- Initialization: The lifecycle begins when you call
provider.run()
after adding a user message. - Processing: The AI Agent calls the AI model. The AI responds with a message and a tool call.
- Tool execution: The AI Agent calls the tool to read or modify the document.
- Loop: The AI Agent calls the AI again and executes the next tool, until a termination condition is met.
- 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:
idle
→loading
: Whenrun()
is calledloading
→reviewingToolCall
: When a tool call requires user reviewloading
→loadingError
: When an error occursloading
→idle
: 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 totrue
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.
- A final tool call is executed. A final tool call is one that has the
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.