Session<State> constructor

Session<State>(
  1. SessionState initialState, {
  2. SchemanticType<State>? stateSchema,
})

Builds a session from initialState, assigning a sessionId if absent.

State is held internally as the raw JSON map (mirroring the JS plain object) so mutations round-trip cleanly through SessionState's (de)serialization regardless of the generated setter behavior.

When a stateSchema is provided, getCustom / updateCustom parse and serialize the custom state through it; otherwise they operate on raw JSON.

Implementation

Session(SessionState initialState, {this._stateSchema})
  : sessionId = initialState.sessionId ?? generateUuidV4() {
  // Deep-clone so we never alias (or mutate) the caller's object: the session
  // owns its state, and a handler mutating it must not reach back into the
  // caller's / chat's state. A shallow `Map.from` would leave nested
  // structures (messages, custom, artifacts) shared by reference.
  _json = _deepClone(initialState.toJson()) as Map<String, dynamic>;
  _json['sessionId'] = sessionId;
}