StatefulSingleUseAIAgent

abstract class StatefulSingleUseAIAgent<Input, Output, TContext : AIAgentContext>(val strategy: AIAgentStrategy<Input, Output, TContext>, logger: KLogger, agentId: String? = null) : AIAgent<Input, Output> (source)

Abstract base class representing a single-use AI agent with state.

This AI agent is designed to execute a specific long-running strategy only once, and provides API to monitor and manage it's state.

It maintains internal states including its running status, whether it was started, its result (if available), and the root context associated with its execution. The class enforces safe state transitions and provides thread-safe operations via a mutex.

Parameters

Input

the type of the input accepted by the agent.

Output

the type of the output produced by the agent.

TContext

the type of the context used during the agent's execution, extending AIAgentContext.

Inheritors

Constructors

Link copied to clipboard
constructor(strategy: AIAgentStrategy<Input, Output, TContext>, logger: KLogger, agentId: String? = null)

Properties

Link copied to clipboard

The configuration for the AI agent.

Link copied to clipboard
override val id: String

A unique identifier represented as a string. This identifier is lazily initialized. If a value is present in agentId, it will use that; otherwise, it will generate a random UUID string.

Link copied to clipboard

Represents the pipeline used by the AI agent for processing tasks or data.

Link copied to clipboard

the execution strategy defining how the agent processes input and produces output.

Functions

Link copied to clipboard
inline fun <Input, Output> AIAgent<Input, Output>.asTool(agentName: String, agentDescription: String, inputDescription: String? = null, inputSerializer: KSerializer<Input> = serializer(), outputSerializer: KSerializer<Output> = serializer(), json: Json = Json.Default): Tool<AIAgentTool.AgentToolArgs, AIAgentTool.AgentToolResult>

Converts the current AI agent into a tool to allow using it in other agents as a tool.

Link copied to clipboard
suspend override fun close()

Closes the AI Agent and performs necessary cleanup operations.

Link copied to clipboard
open override fun <Feature : Any> feature(key: AIAgentStorageKey<Feature>): Feature?

Retrieves a feature associated with the given key from the current context.

Link copied to clipboard

Retrieves a specific agent feature of type Feature from the AI agent's storage. If the requested feature is not found, throws an IllegalStateException.

Link copied to clipboard
suspend override fun getState(): AIAgent.Companion.State<Output>

Retrieves the current state of the AI agent during its lifecycle.

Link copied to clipboard
suspend fun AIAgent<*, *>.isFinished(): Boolean

Checks whether the AI agent has reached a finished state.

Link copied to clipboard
suspend fun AIAgent<*, *>.isRunning(): Boolean

Checks whether the AI agent is currently in a running state.

Link copied to clipboard
abstract suspend fun prepareContext(agentInput: Input, runId: String): TContext

Prepares and initializes the agent context required to handle the given input and run ID.

Link copied to clipboard
open suspend fun result(): Output

Retrieves the result of the operation if the current state is State.Finished. Throws an IllegalStateException if the operation is not in a finished state.

Link copied to clipboard
suspend override fun run(agentInput: Input): Output

Executes the agent's main functionality, coordinating with various components such as pipelines and strategies. Ensures the agent is run in a thread-safe manner using locking mechanisms.