runtime.go

Overview

This file implements the RuntimeAPIController, the primary REST API controller responsible for executing AI agents within user sessions. It handles HTTP requests to run agents synchronously or stream their output via Server-Sent Events (SSE). The controller validates session existence, loads the specified agent, configures the agent runner, invokes the agent with the provided user message, and returns the resulting session events to clients.

The controller serves as the gateway between external API clients and the internal agent execution framework, coordinating interactions with the session service, artifact service, and agent loader components.

This file falls under the REST API Controllers subtopic, which covers HTTP handlers that bridge REST API requests to underlying agent and session services.


Types and Functions

RuntimeAPIController

type RuntimeAPIController struct {
	sessionService  session.Service
	artifactService artifact.Service
	agentLoader     agent.Loader
}

NewRuntimeAPIController

func NewRuntimeAPIController(sessionService session.Service, agentLoader agent.Loader, artifactService artifact.Service) *RuntimeAPIController

RunHandler

func (c *RuntimeAPIController) RunHandler(rw http.ResponseWriter, req *http.Request) error

RunSSEHandler

func (c *RuntimeAPIController) RunSSEHandler(rw http.ResponseWriter, req *http.Request) error

runAgent (private method)

func (c *RuntimeAPIController) runAgent(ctx context.Context, runAgentRequest models.RunAgentRequest) ([]*session.Event, error)

flashEvent (private function)

func flashEvent(flusher http.Flusher, rw http.ResponseWriter, event session.Event) error

validateSessionExists (private method)

func (c *RuntimeAPIController) validateSessionExists(ctx context.Context, appName, userID, sessionID string) error

getRunner (private method)

func (c *RuntimeAPIController) getRunner(req models.RunAgentRequest) (*runner.Runner, *agent.RunConfig, error)

decodeRequestBody (private function)

func decodeRequestBody(req *http.Request) (decodedReq models.RunAgentRequest, err error)

Important Implementation Details


Interaction with Other System Components


Usage Examples

Running an Agent Synchronously (Non-Streaming)

A client POSTs to /run with JSON body:

{
  "appName": "chatbot",
  "userId": "user123",
  "sessionId": "sess456",
  "newMessage": {
    "text": "Hello, agent!"
  },
  "streaming": false
}

The RunHandler will:

Running an Agent with Streaming (SSE)

A client POSTs to /run_sse with "streaming": true in the body. The server responds with an SSE stream:

data: {"eventType":"message", "content": "..."}
data: {"eventType":"update", "content": "..."}
...

Events are flushed incrementally as the agent produces them, enabling real-time client updates.


Visual Diagram: RuntimeAPIController Structure and Workflow

classDiagram
class RuntimeAPIController {
-sessionService: session.Service
-artifactService: artifact.Service
-agentLoader: agent.Loader
+RunHandler()
+RunSSEHandler()
-runAgent()
-validateSessionExists()
-getRunner()
}
RuntimeAPIController --> session.Service
RuntimeAPIController --> artifact.Service
RuntimeAPIController --> agent.Loader
RuntimeAPIController ..> runner.Runner : creates and uses

Visual Diagram: Agent Execution Flow in RuntimeAPIController

flowchart TD
Client[HTTP Client]
ReqDecode[Decode JSON Request]
ValidateSession[Validate Session Exists]
LoadAgent[Load Agent by AppName]
NewRunner[Create Runner]
RunAgent[Run Agent]
CollectEvents[Collect Session Events]
EventConvert[Convert to API Events]
Response[Send JSON or SSE Response]
Client --> ReqDecode --> ValidateSession --> LoadAgent --> NewRunner --> RunAgent --> CollectEvents --> EventConvert --> Response

References to Related Topics


This file provides the essential HTTP bridging code for running AI agents with session context, supporting both batch and streaming interaction modes, and ensuring robust validation and error handling.