mnesis.models.message¶
message
¶
Core message and part data models for Mnesis.
FinishReason
¶
Bases: StrEnum
Why the LLM stopped generating.
String enum so result.finish_reason == "stop" comparisons work
without importing the enum.
Important — FinishReason.ERROR: when finish_reason is
FinishReason.ERROR (or "error"), the LLM call failed.
TurnResult.text contains an error description, not a model response.
Always check finish_reason before processing text.
TextPart
¶
Bases: BaseModel
A plain text segment of a message.
ReasoningPart
¶
Bases: BaseModel
Chain-of-thought reasoning text (e.g. Claude extended thinking, o1 reasoning).
ToolStatus
¶
Bases: BaseModel
Mutable lifecycle status of a tool call. Updated in-place via update_part_status().
ToolPart
¶
CompactionMarkerPart
¶
Bases: BaseModel
Metadata marker embedded in a compaction summary message.
Records which messages were compacted and which escalation level succeeded.
StepStartPart
¶
Bases: BaseModel
Marker for the start of an agentic step within a turn.
StepFinishPart
¶
Bases: BaseModel
Marker for the end of an agentic step, including token usage.
PatchPart
¶
Bases: BaseModel
A git-format unified diff representing file changes made during a turn.
FileRefPart
¶
Bases: BaseModel
A content-addressed reference to a large file stored outside the context window.
When the LargeFileHandler intercepts a file that exceeds the inline threshold,
it stores it externally and replaces the content with this reference object.
The ContextBuilder renders this as a structured [FILE: ...] block.
content_id
instance-attribute
¶
SHA-256 hex digest of the file content. Used as cache key.
file_type
instance-attribute
¶
Detected MIME type or language identifier (e.g. 'python', 'application/json').
exploration_summary
instance-attribute
¶
Deterministic structural description of the file (classes, functions, keys, etc.).
TokenUsage
¶
Bases: BaseModel
Token counts for a single LLM response or cumulative session usage.
effective_total
¶
Return total, computing from parts when the explicit total is zero.
MessageError
¶
Bases: BaseModel
Structured error attached to an assistant message when the turn fails.
Message
¶
Bases: BaseModel
A single message stored in the ImmutableStore.
Messages are append-only — the only mutable fields are token usage and finish_reason (updated after streaming completes), and part status fields (managed via update_part_status).
created_at
class-attribute
instance-attribute
¶
Unix millisecond timestamp.
is_summary
class-attribute
instance-attribute
¶
True if this message is a compaction summary produced by the CompactionEngine.
mode
class-attribute
instance-attribute
¶
'compaction' for summary messages, None for regular turns.
MessageWithParts
¶
ContextBudget
¶
Bases: BaseModel
Token budget for assembling the context window on a single turn.
TurnResult
¶
Bases: BaseModel
The result of a single MnesisSession.send() call.
Contains the assistant's text response, token usage, finish reason, and indicators for compaction and doom loop detection.
Important — finish_reason == "error": When finish_reason is
"error", the LLM call failed and text contains an error description,
not an LLM response. Do not treat the text as a model reply in this case.
Always check finish_reason before processing text.
finish_reason
instance-attribute
¶
Why the LLM stopped generating.
Type is FinishReason | str: known values are the FinishReason enum
members; providers may return additional string values.
FinishReason.STOP/"stop"— natural end of response.FinishReason.MAX_TOKENS/"max_tokens"— output token limit reached.FinishReason.LENGTH/"length"— alias used by some providers.FinishReason.TOOL_CALLS/"tool_calls"— model requested tool execution.FinishReason.ERROR/"error"— the LLM call failed;textcontains the error description, not a model response. Do not pass this text to the model as if it were a real reply.
cost
instance-attribute
¶
Estimated USD cost of this turn. Always 0.0 — not yet implemented.
CompactionResult
¶
Bases: BaseModel
The result of a compaction run.
Records which escalation level succeeded, how many messages were compressed, the token savings achieved, and how many tool outputs were pruned.
level_used
instance-attribute
¶
1 = selective LLM, 2 = aggressive LLM, 3 = deterministic fallback.
pruned_tool_outputs
class-attribute
instance-attribute
¶
Number of tool output parts tombstoned by the ToolOutputPruner during this run.
pruned_tokens
class-attribute
instance-attribute
¶
Tokens reclaimed by pruning tool outputs during this run.
PruneResult
¶
Bases: BaseModel
The result of a ToolOutputPruner pass.
RecordResult
¶
Bases: BaseModel
The result of a MnesisSession.record() call.
Records the IDs of the persisted user and assistant messages so callers can reference them later (e.g. for event subscriptions or debugging).