Class: Insika::Commands::CreateSession

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/commands/create_session.rb

Overview

Control command: creates a session and responds synchronously — does not create a Task. Payload { vars: {} } -> returns Session. id optional: when present, the session is born with that id (correlation by external key — e.g.: chat.id in the /v1/responses adapter; contextId in A2A). Absent -> uuid.

Instance Method Summary collapse

Constructor Details

#initialize(session_store:, event_stream:) ⇒ CreateSession

event_stream: any object with #emit(event) (tests use a spy).



13
14
15
16
# File 'lib/insika/commands/create_session.rb', line 13

def initialize(session_store:, event_stream:)
  @session_store = session_store
  @event_stream = event_stream
end

Instance Method Details

#call(command) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/insika/commands/create_session.rb', line 18

def call(command)
  # Accepts a symbol key (internal dispatch) and a string key (transport JSON)
  # — normalizes at the handler boundary.
  vars = command.payload[:vars] || command.payload["vars"] || {}
  raise Insika::ValidationError, "vars must be a Hash" unless vars.is_a?(Hash)

  # Per-chat model pin: `model`/`provider` on the payload become a
  # reserved, collision-safe slot in vars (never rendered in the prompt — the
  # Request provider skips "__"-prefixed vars). The ModelResolver reads it as
  # the highest-precedence layer (Chat > Agent > platform default).
  vars = apply_model_pin(vars.dup, command.payload)

  id = command.payload[:id] || command.payload["id"]
  session = id ? @session_store.create(id: id, vars: vars) : @session_store.create(vars: vars)
  # :session_created is from the closed catalog (origin: CreateSession handler).
  # meta without task_id/seq (control has no Task); Event#to_h does meta.compact.
  @event_stream.emit(Insika::Event.new(
                       type: :session_created,
                       data: { session_id: session.id },
                       meta: { session_id: session.id, at: Time.now.utc.iso8601 }
                     ))
  session
end