sessions.go

Overview

sessions.go defines the SessionsAPIController, a REST API controller responsible for managing user-agent interaction sessions within the system. It provides HTTP handlers to create, retrieve, list, and delete sessions via JSON-based REST endpoints. The controller acts as an intermediary between HTTP requests and the underlying session service, handling request validation, decoding/encoding JSON payloads, invoking session operations, and formatting HTTP responses.

This file is part of the REST API Controllers set and specifically addresses session lifecycle management in the broader REST API and Web Launchers module. It links external clients with internal session persistence and event handling, enabling session state initialization, event appending, querying, and removal.


Types and Structures

SessionsAPIController

type SessionsAPIController struct {
    service session.Service
}

Functions and Methods

NewSessionsAPIController

func NewSessionsAPIController(service session.Service) *SessionsAPIController
sessService := session.NewServiceImplementation(...)
controller := NewSessionsAPIController(sessService)

CreateSessionHandler

func (c *SessionsAPIController) CreateSessionHandler(rw http.ResponseWriter, req *http.Request)

createSession (unexported helper)

func (c *SessionsAPIController) createSession(ctx context.Context, sessionID models.SessionID, createSessionRequest models.CreateSessionRequest) (models.Session, error)

DeleteSessionHandler

func (c *SessionsAPIController) DeleteSessionHandler(rw http.ResponseWriter, req *http.Request)

GetSessionHandler

func (c *SessionsAPIController) GetSessionHandler(rw http.ResponseWriter, req *http.Request)

ListSessionsHandler

func (c *SessionsAPIController) ListSessionsHandler(rw http.ResponseWriter, req *http.Request)

Important Implementation Details


Interaction with Other Components


Workflow Diagrams

SessionsAPIController Structure and Handlers

classDiagram
class SessionsAPIController {
-service: session.Service
+CreateSessionHandler()
+createSession()
+DeleteSessionHandler()
+GetSessionHandler()
+ListSessionsHandler()
}

SessionsAPIController Request Handling Flow

flowchart TD
subgraph Session API Controller
CS[CreateSessionHandler]
CSHelper[createSession]
DS[DeleteSessionHandler]
GS[GetSessionHandler]
LS[ListSessionsHandler]
end
Client_Create --> CS
CS --> CSHelper
CSHelper --> SessionService_Create[session.Service.Create]
CSHelper --> SessionService_AppendEvent[session.Service.AppendEvent]
Client_Delete --> DS
DS --> SessionService_Delete[session.Service.Delete]
Client_Get --> GS
GS --> SessionService_Get[session.Service.Get]
Client_List --> LS
LS --> SessionService_List[session.Service.List]
CSHelper --> EncodeResponse
DS --> EncodeResponse
GS --> EncodeResponse
LS --> EncodeResponse

Usage Examples

Creating a Session (HTTP POST)

POST /api/apps/{appName}/users/{userID}/sessions/{sessionID}
Content-Type: application/json

{
  "state": { ... },       // optional initial state
  "events": [ ... ]       // optional initial event history
}
HTTP/1.1 200 OK
Content-Type: application/json

{
  "appName": "...",
  "userID": "...",
  "sessionID": "...",
  "state": { ... },
  "events": [ ... ]
}

Retrieving a Session (HTTP GET)

GET /api/apps/{appName}/users/{userID}/sessions/{sessionID}
HTTP/1.1 200 OK
Content-Type: application/json

{
  "appName": "...",
  "userID": "...",
  "sessionID": "...",
  "state": { ... },
  "events": [ ... ]
}

Listing Sessions for a User (HTTP GET)

GET /api/apps/{appName}/users/{userID}/sessions
HTTP/1.1 200 OK
Content-Type: application/json

[
  { "appName": "...", "userID": "...", "sessionID": "...", "state": {...}, "events": [...] },
  { "appName": "...", "userID": "...", "sessionID": "...", "state": {...}, "events": [...] }
]

References to Related Topics


This documentation provides a detailed understanding of the sessions.go file, its role in managing session lifecycle through REST APIs, and its integration within the system's session and agent frameworks.