The Agent Runtime Service provides a comprehensive API for:
- Agent Management: Creating, terminating, and managing agent actors
- Service Access: Accessing configured AI services (ModelService, ProviderService, PolicyService)
- State Management: Managing agent state and activity processing
- Message Handling: Sending activities and subscribing to runtime events
The AgentRuntimeServiceApi interface defines all available operations:
interface AgentRuntimeServiceApi {
// Agent Management
readonly create: <S>(id: AgentRuntimeId, initialState: S) => Effect.Effect<RuntimeInterface<S>, AgentRuntimeError>
readonly terminate: (id: AgentRuntimeId) => Effect.Effect<void, AgentRuntimeNotFoundError>
readonly send: (id: AgentRuntimeId, activity: AgentActivity) => Effect.Effect<void, AgentRuntimeNotFoundError>
readonly getState: (id: AgentRuntimeId) => Effect.Effect<AgentRuntimeState<unknown>, AgentRuntimeNotFoundError>
readonly subscribe: (id: AgentRuntimeId) => Stream.Stream<AgentActivity, AgentRuntimeNotFoundError>
// Service Access
readonly getModelService: () => Effect.Effect<ModelServiceApi, never>
readonly getProviderService: () => Effect.Effect<ProviderServiceApi, never>
readonly getPolicyService: () => Effect.Effect<PolicyServiceApi, never>
}Creates a new agent runtime with initial state and workflow processing.
create<S>(
id: AgentRuntimeId,
initialState: S
): Effect.Effect<RuntimeInterface<S>, AgentRuntimeError>Parameters:
id: Unique identifier for the agent runtimeinitialState: Initial state of generic type S
Returns:
RuntimeInterface<S>containing:id: Runtime identifiersend: Method to send activitiesgetState: Method to get current statesubscribe: Method to subscribe to activities
Errors:
AgentRuntimeError: If runtime with same ID already exists
Example:
const runtime = yield* agentRuntimeService.create("my-agent", { count: 0 });
console.log(runtime.id); // "my-agent"Gracefully terminates an agent runtime and cleans up resources.
terminate(id: AgentRuntimeId): Effect.Effect<void, AgentRuntimeNotFoundError>Parameters:
id: Runtime identifier to terminate
Returns:
voidon successful termination
Errors:
AgentRuntimeNotFoundError: If runtime doesn't exist
Example:
yield* agentRuntimeService.terminate("my-agent");Sends an activity to a runtime for processing.
send(
id: AgentRuntimeId,
activity: AgentActivity
): Effect.Effect<void, AgentRuntimeNotFoundError>Parameters:
id: Target runtime identifieractivity: Activity record to process
Returns:
voidwhen activity is queued
Errors:
AgentRuntimeNotFoundError: If runtime doesn't exist
Example:
yield* agentRuntimeService.send("my-agent", {
id: "task-1",
agentRuntimeId: "my-agent",
timestamp: Date.now(),
type: AgentRecordType.COMMAND,
payload: { action: "increment" },
metadata: {},
sequence: 1
});Retrieves the current state of a runtime.
getState(id: AgentRuntimeId): Effect.Effect<AgentRuntimeState<unknown>, AgentRuntimeNotFoundError>Parameters:
id: Runtime identifier
Returns:
AgentRuntimeState<unknown>containing current state and status
Errors:
AgentRuntimeNotFoundError: If runtime doesn't exist
Example:
const state = yield* agentRuntimeService.getState("my-agent");
console.log(state.status); // "IDLE" | "PROCESSING" | "ERROR" | "TERMINATED"Creates a subscription to runtime activity events.
subscribe(id: AgentRuntimeId): Stream.Stream<AgentActivity, AgentRuntimeNotFoundError>Parameters:
id: Runtime identifier
Returns:
StreamofAgentActivityevents
Errors:
AgentRuntimeNotFoundError: If runtime doesn't exist
Example:
const stream = agentRuntimeService.subscribe("my-agent");
yield* Stream.runForeach(stream, (activity) =>
Effect.log(`Received activity: ${activity.type}`)
);Returns the configured ModelService instance.
getModelService(): Effect.Effect<ModelServiceApi, never>Returns:
ModelServiceApifor model operations
Example:
const modelService = yield* agentRuntimeService.getModelService();
const isValid = yield* modelService.validateModel("gpt-4o");Returns the configured ProviderService instance.
getProviderService(): Effect.Effect<ProviderServiceApi, never>Returns:
ProviderServiceApifor provider operations
Example:
const providerService = yield* agentRuntimeService.getProviderService();
const client = yield* providerService.createClient("openai");Returns the configured PolicyService instance.
getPolicyService(): Effect.Effect<PolicyServiceApi, never>Returns:
PolicyServiceApifor policy operations
Example:
const policyService = yield* agentRuntimeService.getPolicyService();
const result = yield* policyService.checkPolicy({
auth: { userId: "user1" },
requestedModel: "gpt-4o",
operationType: "text:generate"
});Represents the current state of an agent runtime.
interface AgentRuntimeState<S> {
readonly id: AgentRuntimeId
readonly state: S
readonly status: AgentRuntimeStatus
readonly lastUpdated: number
readonly processing?: {
readonly processed: number
readonly failures: number
readonly avgProcessingTime: number
readonly lastError?: unknown
}
}Base interface for all activities sent to agents.
interface AgentActivity {
readonly id: string
readonly agentRuntimeId: AgentRuntimeId
readonly timestamp: number
readonly type: AgentRecordType
readonly payload: unknown
readonly metadata: Record<string, unknown>
readonly sequence: number
}Enumeration of possible runtime states.
enum AgentRuntimeStatus {
IDLE = "IDLE",
PROCESSING = "PROCESSING",
ERROR = "ERROR",
TERMINATED = "TERMINATED"
}Types of activities that can be sent to agents.
enum AgentRecordType {
COMMAND = "COMMAND",
STATE_CHANGE = "STATE_CHANGE"
}Base error for all runtime operations.
class AgentRuntimeError extends Data.TaggedError("AgentRuntimeError")<{
readonly agentRuntimeId: AgentRuntimeId
readonly description: string
readonly module: string
readonly method: string
readonly cause?: unknown
}> {}Error when trying to access a non-existent runtime.
class AgentRuntimeNotFoundError extends AgentRuntimeError {
readonly _tag = "AgentRuntimeNotFoundError"
}Error during activity processing.
class AgentRuntimeProcessingError extends AgentRuntimeError {
readonly _tag = "AgentRuntimeProcessingError"
}Error when trying to operate on a terminated runtime.
class AgentRuntimeTerminatedError extends AgentRuntimeError {
readonly _tag = "AgentRuntimeTerminatedError"
}const completeExample = Effect.gen(function* () {
// Get the agent runtime service
const agentRuntime = yield* AgentRuntimeService;
// Get AI services
const modelService = yield* agentRuntime.getModelService();
const policyService = yield* agentRuntime.getPolicyService();
// Validate model before creating agent
const modelValid = yield* modelService.validateModel("gpt-4o");
if (!modelValid) {
return yield* Effect.fail(new Error("Invalid model"));
}
// Check policy
const policyResult = yield* policyService.checkPolicy({
auth: { userId: "user1" },
requestedModel: "gpt-4o",
operationType: "text:generate"
});
if (!policyResult.allowed) {
return yield* Effect.fail(new Error("Policy denied"));
}
// Create agent runtime
const runtime = yield* agentRuntime.create("my-agent", {
userId: "user1",
model: "gpt-4o",
conversationHistory: []
});
// Subscribe to activities
const subscription = agentRuntime.subscribe("my-agent");
yield* Stream.runForeach(subscription, (activity) =>
Effect.log(`Activity: ${activity.type}`)
).pipe(Effect.fork);
// Send command
yield* agentRuntime.send("my-agent", {
id: "cmd-1",
agentRuntimeId: "my-agent",
timestamp: Date.now(),
type: AgentRecordType.COMMAND,
payload: {
action: "generate_text",
prompt: "Hello world"
},
metadata: {},
sequence: 1
});
// Get final state
const finalState = yield* agentRuntime.getState("my-agent");
yield* Effect.log(`Final status: ${finalState.status}`);
// Cleanup
yield* agentRuntime.terminate("my-agent");
});
// Run with proper dependencies
const program = Effect.provide(
completeExample,
Layer.mergeAll(
ConfigurationService.Default,
ModelService.Default,
ProviderService.Default,
PolicyService.Default,
AgentRuntimeService.Default,
NodeFileSystem.layer
)
);The AgentRuntimeService automatically loads configurations for all dependent services:
- ModelService: Loads from
master-config.agents.modelsConfigPath - ProviderService: Loads from
master-config.agents.providersConfigPath - PolicyService: Loads from
master-config.agents.policiesConfigPath
This provides a single entry point for accessing all configured AI services while maintaining clean separation of concerns.