AiAgent class final

Runs an automatic tool-calling loop on top of any AiStreamClient.

Instead of manually handling AiToolCall events, feed the agent a list of AiTools and it manages the full round trip:

  1. Stream a completion
  2. If the model calls tools → execute them, append results, loop
  3. When the model responds with text → done
final agent = AiAgent(
  client: OpenAiStreamClient(baseUrl: '...', apiKey: '...'),
  tools: [getWeatherTool, searchTool],
  maxIterations: 5,
);

final result = await agent.run(
  [AiChatMessage.user('What is the weather in Lahore?')],
  model: 'gpt-4o',
);
print(result.text);

Constructors

AiAgent({required AiStreamClient client, List<AiTool> tools = const [], int maxIterations = 8, void onEvent(Object event)?})
const

Properties

client → AiStreamClient
The streaming client to use for completions.
final
hashCode → int
The hash code for this object.
no setterinherited
maxIterations → int
Maximum tool-calling iterations before giving up (safety bound).
final
onEvent → void Function(Object event)?
Optional callback for every streaming event (for live UI rendering while the agent works).
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
tools → List<AiTool>
Tools the model may invoke.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
run(List<AiChatMessage> messages, {required String model, double? temperature, int? maxTokens}) → Future<AiAgentResult>
Runs the agent: streams a completion, executes any tool calls, feeds results back, and repeats until the model produces a final text response or maxIterations is hit.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited