utils.go

Overview

This file provides utility functions for managing and manipulating session state deltas within the system. It handles the segregation and merging of state information categorized by different scopes: application-wide (app), user-specific (user), and session-specific (session). The main purpose is to facilitate clean separation and reassembly of these states when processing client-server interactions or internal session management.

The file defines two core functions:

These utilities are essential for managing session state in a structured manner, ensuring that state updates and retrievals conform to the expected scope boundaries.


Constants


Functions

ExtractStateDeltas

func ExtractStateDeltas(delta map[string]any) (
	appStateDelta, userStateDelta, sessionStateDelta map[string]any,
)

Description

Splits a single combined state delta map (delta) into three separate maps corresponding to application, user, and session states. It uses key prefixes to determine which map each key-value pair belongs to:

Temporary keys (starting with temp:) are ignored and excluded from all output maps.

Parameters

Returns

Usage Example

delta := map[string]any{
	"app:theme": "dark",
	"user:locale": "en-US",
	"session_id": "abc123",
	"temp:cache": 42,
}

appState, userState, sessionState := ExtractStateDeltas(delta)

// appState => {"theme": "dark"}
// userState => {"locale": "en-US"}
// sessionState => {"session_id": "abc123"}

Implementation Details


MergeStates

func MergeStates(appState, userState, sessionState map[string]any) map[string]any

Description

Combines three separate state maps (appState, userState, sessionState) into a single map suitable for client responses or unified state representation. It prefixes keys from the application and user states with their respective prefixes (app: and user:) to maintain their identity in the merged map. Session state keys remain unchanged.

Parameters

Returns

Usage Example

appState := map[string]any{"theme": "dark"}
userState := map[string]any{"locale": "en-US"}
sessionState := map[string]any{"session_id": "abc123"}

merged := MergeStates(appState, userState, sessionState)

// merged => {
//    "app:theme": "dark",
//    "user:locale": "en-US",
//    "session_id": "abc123",
// }

Implementation Details


Interaction with Other Parts of the System

This file is part of the session state management utilities within the broader Session Management topic. It supports the breakdown and reassembly of state deltas which are central to:

The state maps manipulated here are commonly used in workflows where states must be scoped properly—application-wide, user-specific, or per-session—to maintain consistency and separation of concerns.


Diagram: Function Workflow and Relationships

flowchart TD
A[Input: Combined delta map] -->|ExtractStateDeltas| B1[appStateDelta map]
A -->|ExtractStateDeltas| B2[userStateDelta map]
A -->|ExtractStateDeltas| B3[sessionStateDelta map]
B1 -->|MergeStates| C[Merged map]
B2 -->|MergeStates| C
B3 -->|MergeStates| C
style A fill:none,stroke:none
style B1 fill:none,stroke:none
style B2 fill:none,stroke:none
style B3 fill:none,stroke:none
style C fill:none,stroke:none

This flowchart illustrates the primary data flow:

  1. ExtractStateDeltas takes a combined delta and splits it into three maps by prefix.

  2. MergeStates takes the three separated maps and recombines them into a single map with prefixes restored.


Notes on Algorithms and Efficiency


This file forms a fundamental building block for state handling in the session management subsystem, enabling clean separation and unified representation of multi-scoped state data. It directly supports workflows that require clear boundaries between app, user, and session level data for agents and services.