ion7-llm / session

class

ion7.llm.Session

id integer Process-unique session id.
seq_id integer? KV row in the shared context.
system string? System prompt (single string).
messages table[] Canonical `{role, content, ...}` array.
n_past integer Per-session next-decode position.
_seq_snapshot string? Last `ctx:seq_snapshot(seq_id)` blob.
_dirty boolean Set when `messages` mutated since last decode.
_msg_kv_ends integer[] KV-end-position per message (for eviction).
_last_response ion7.llm.Response? Set by `engine:chat` after each call.

Functions

Session.new

New session.

Session.new(opts)
optstable?
→ ion7.llm.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(role, content, extras)
rolestring`"user" | "assistant" | "system" | "tool"`
contentstring
extrastable?Pass-through fields (`thinking`,
→ ion7.llm.Sessionself

Session:add_user

Append a user message.

Session:add_user(text)

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_assistant(text, extras)

Session:add_tool_result

Append a tool-result message tied back to a call id.

Session:add_tool_result(tool_call_id, content)
tool_call_idstring
contentstring|tableStringified result, or a table the

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:set_system(text)
textstring

Session:all_messages

The full message list as the chat template expects it : system (when present) followed by the chronological turns.

Session:all_messages()
→ table[]

Session:pending_messages

Just the conversational messages (no system). Same array reference Session manages internally — do not mutate.

Session:pending_messages()
→ table[]

Session:last_response

Last `Response` produced for this session, or nil if no chat ran yet. Set by `engine:chat` / `engine:stream`.

Session:last_response()
→ ion7.llm.Response?

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:format(msgs)
msgstable?Defaults to `self.messages` (excludes system).
→ string

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:reset()
→ ion7.llm.Sessionself

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:fork()
→ ion7.llm.Session

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:_save_seq_snapshot(blob, n_past)
blobstring
n_pastinteger

Session:_has_clean_snapshot

True when `_save_seq_snapshot` was called at least once and the session has not been mutated since.

Session:_has_clean_snapshot()
→ boolean

Session:_seq_snapshot_blob

Raw snapshot blob.

Session:_seq_snapshot_blob()
→ string?

Session:serialize

Plain Lua table view (no KV blob — that is per-context and not portable across runs).

Session:serialize()
→ table

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.deserialize(t)
ttable
→ ion7.llm.Session

Session:save

Save to disk as JSON. Goes through `ion7.vendor.json`.

Session:save(path)
pathstring
→ booleanok
→ string?err

Session.load

Inverse of `:save`. Returns `(session, nil)` on success or `(nil, err)` on failure.

Session.load(path)
pathstring
→ ion7.llm.Session?
→ string?