class
ion7.llm.Session
Functions
Session.new
New session.
Session:add
Append a message of an explicit role. Marks the session dirty so the engine knows to re-decode the suffix on next call.
Session:add_user
Append a user message.
Session:add_assistant
Append an assistant message. Pass `extras.thinking` and `extras.tool_calls` from the previous response so the chat template can re-render the full reasoning + tool-call envelope on the next turn (interleaved-thinking pattern).
Session:add_tool_result
Append a tool-result message tied back to a call id.
Session:set_system
Replace the system prompt. Mutating the system text invalidates the prefix cache for this session ; the `kv` layer recomputes on next prepare.
Session:all_messages
The full message list as the chat template expects it : system (when present) followed by the chronological turns.
Session:pending_messages
Just the conversational messages (no system). Same array reference Session manages internally — do not mutate.
Session:last_response
Last `Response` produced for this session, or nil if no chat ran yet. Set by `engine:chat` / `engine:stream`.
Session:format
Plain-text dump of the conversation, one `role: content` line per message. Useful when feeding history into a summariser or RAG chain.
Session:reset
Drop every message and reset KV bookkeeping. The `seq_id` stays assigned (the row is still ours) ; the `kv` layer drops the row's contents through `kv_seq_rm` at next prepare when it sees `n_past == 0`.
Session:fork
Lua-side history clone. The companion KV copy (`kv_seq_cp src dst`) happens inside `kv:fork(parent_session)` because that needs a live context. Calling `:fork` in isolation gives you a session with the same messages but an unassigned `seq_id` — useful for branching exploration where you do not need shared KV.
Session:_save_seq_snapshot
Set the snapshot + per-session n_past. Called after a successful decode. The blob is the result of `ctx:seq_snapshot(self.seq_id)`, restorable later via `ctx:seq_restore(blob, self.seq_id)` without disturbing other sessions sharing the context.
Session:_has_clean_snapshot
True when `_save_seq_snapshot` was called at least once and the session has not been mutated since.
Session:_seq_snapshot_blob
Raw snapshot blob.
Session:serialize
Plain Lua table view (no KV blob — that is per-context and not portable across runs).
Session.deserialize
Re-build a Session from a `serialize()` table. The new session has no snapshot and starts dirty so the engine will re-encode the whole history on first `engine:chat`.
Session:save
Save to disk as JSON. Goes through `ion7.vendor.json`.
Session.load
Inverse of `:save`. Returns `(session, nil)` on success or `(nil, err)` on failure.