ion7-core / context.state

module

context.state

Functions

M.save_state

Save the full context state to a file. The optional `tokens` cdata is embedded so a downstream `load_state` can recover the original prompt alongside the KV cache.

M.save_state(self, path, tokens, n_tokens)
pathstringOutput file path.
tokenscdata?Optional `llama_token[]` to embed.
n_tokensinteger?Length of `tokens`.
→ boolean

M.load_state

Restore the full context state from a file written by `save_state`. When `token_buf` is provided we also recover the embedded prompt token sequence ; the Lua-side `n_past` mirror is updated to match the restored fill position.

M.load_state(self, path, token_buf, token_capacity)
pathstring
token_bufcdata?Buffer to receive the embedded tokens.
token_capacityinteger?Capacity of `token_buf`.
→ booleanok
→ integern_tokens_restored

M.snapshot

Serialise the full state into a Lua string (zero file I/O). Returns nil if `llama_state_get_size` reports 0 bytes.

M.snapshot(self)
→ string|nil

M.restore

Restore state from a `snapshot` blob. Does NOT update the Lua-side `n_past` mirror (the snapshot does not carry it) — call `Context:set_n_past(n)` after if you need to resume position-aware decoding.

M.restore(self, blob)
blobstring
→ boolean

M.seq_state_size

Byte size of the state for a single sequence — useful to pre-size caller-side buffers before `seq_snapshot`.

M.seq_state_size(self, seq_id)
seq_idinteger?Default 0.
→ integer

M.seq_save_state

Save a single sequence's KV state to a file.

M.seq_save_state(self, path, seq_id)
pathstring
seq_idinteger?Default 0.
→ boolean

M.seq_load_state

Load a sequence's KV state from a file into `dst_seq_id`.

M.seq_load_state(self, path, dst_seq_id)
pathstring
dst_seq_idinteger?Default 0.
→ boolean

M.seq_snapshot

Serialise a single sequence's KV state into a Lua string.

M.seq_snapshot(self, seq_id)
seq_idinteger?Default 0.
→ string|nil

M.seq_restore

Restore a sequence's KV state from a `seq_snapshot` blob.

M.seq_restore(self, blob, dst_seq_id)
blobstring
dst_seq_idinteger?Default 0.
→ integerBytes consumed (0 on failure).