core.history
History tracking strategies for FFAI interactions.
History tracking strategies for FFAI interactions.
- class ConversationHistory[source]
Bases:
objectAPI-facing message history for provider SDK calls.
Unlike
PermanentHistory, turns here carry no timestamps and the history is intended to be cleared (e.g. when starting a new conversation context). Theget_turns()output is structured for direct injection into provider message APIs.- add_turn_assistant(content)[source]
Append an assistant turn to the history.
- Parameters:
content (str) – The assistant’s response text.
- Return type:
None
- class HistoryRecorder(context, permanent_history, ordered_history, memory=None, memory_persist=False, memory_persist_path=None)[source]
Bases:
objectRecords interactions to all 5 history stores in a single operation.
Owns the raw
historyandclean_historylists and coordinates writes toPermanentHistory,OrderedPromptHistory, andResponseContext. Optionally embeds each recorded Q+A pair into aMemoryinstance on a fire-and-forget background thread.Callers invoke
record()instead of manually writing to 5 separate stores.- Parameters:
context (ResponseContext) – The ResponseContext for prompt_attr_history recording.
permanent_history (PermanentHistory) – The PermanentHistory for chronological turns.
ordered_history (OrderedPromptHistory) – The OrderedPromptHistory for named interactions.
memory (Memory | None) – Optional Memory instance. When provided, each successful
record()call submits an embedding task to a dedicated single-worker thread pool. Failed embeds are logged atWARNINGand dropped; they never propagate to the caller.memory_persist (bool) – If
True, persist the memory store to Parquet after each successful embed. Requiresmemory_persist_pathto be set.memory_persist_path (str | None) – Fully-qualified file path for the Parquet persistence file. Ignored when
memory_persistisFalseormemoryisNone. Typically computed byFFAI.__init__()asf"{config.memory.persist_dir}/{config.memory.collection_name}.parquet".
- record(prompt, response, model, prompt_name=None, history=None, status='success', resolved_prompt=None, usage=None, metadata=None)[source]
Record an interaction to all 5 history stores.
When a
Memoryinstance is configured andstatus == "success", the Q+A pair is embedded on a fire-and-forget background thread. Metadata is derived fromprompt_namewhen not explicitly passed.- Parameters:
prompt (str) – The resolved prompt text.
response (Any) – The cleaned response.
model (str) – Model identifier used.
prompt_name (str | None) – Logical name for the prompt.
history (list[str] | None) – List of prompt names this call depends on.
status (str) – Execution status (“success”, “skipped”, “failed”). Only
"success"triggers embedding.resolved_prompt (str | None) – The fully interpolated prompt sent to the model.
usage (Any) – Token usage from the API call.
metadata (dict[str, Any] | None) – Optional caller metadata. When
None, derived fromprompt_nameas{"prompt_name": prompt_name}(or{}ifprompt_nameis alsoNone).
- Return type:
None
- class OrderedPromptHistory[source]
Bases:
objectManages ordered prompt-response history with named references.
This class provides: - Named prompt storage for declarative context assembly - Sequential ordering of interactions - Query capabilities by prompt name, model, etc. - History chain tracking for dependency resolution
- prompt_dict
OrderedDict mapping prompt names to interaction lists.
- get_effective_prompt_name(prompt_name)[source]
Get the effective prompt name from various input types.
- add_interaction(model, prompt, response, prompt_name=None, history=None)[source]
Add a new interaction to the history, storing cleaned versions of prompt and response.
- Parameters:
model (str) – The model used for the interaction
prompt (str) – The prompt text
response (str) – The response text
prompt_name (str | None) – Optional name/key for the prompt. If None, uses prompt text as key
history (list[str] | None) – Optional list of prompt names that form the history chain for this interaction
- Returns:
The created Interaction object
- Return type:
- get_interactions_by_prompt_name(prompt_name)[source]
Get all interactions for a specific prompt name.
- Parameters:
prompt_name (str)
- Return type:
- get_latest_interaction_by_prompt_name(prompt_name)[source]
Get the most recent interaction for a specific prompt name.
- Parameters:
prompt_name (str)
- Return type:
Interaction | None
- get_interactions_by_model_and_prompt_name(model, prompt_name)[source]
Get all interactions for a specific model and prompt name combination.
- Parameters:
- Return type:
- merge_histories(other)[source]
Merge another OrderedPromptHistory into this one.
- Parameters:
other (OrderedPromptHistory) – Another OrderedPromptHistory instance to merge
- Return type:
None
- get_interaction_by_prompt(prompt)[source]
Get an interaction by its exact prompt text.
Useful when prompt was used as the prompt_name.
- Parameters:
prompt (str)
- Return type:
Interaction | None
- class PermanentHistory[source]
Bases:
objectAppend-only chronological turn history with timestamps.
Each turn stores a
role("user"or"assistant"), structuredcontent, a per-turntimestamp, and optionalmetadata. Consecutive user turns are coalesced by appending content unless the caller passes non-Nonemetadata, in which case a new turn is always created (so distinctprompt_namemetadata is never silently merged).- add_turn_assistant(content, metadata=None)[source]
Append an assistant turn with the current timestamp.
- add_turn_user(content, metadata=None)[source]
Append a user turn, coalescing with the previous user turn when adjacent.
Coalescing only happens when metadata is
None. If metadata is provided, a new turn is always created so distinctprompt_namemetadata is never silently merged into the previous user turn.
Classes
- class ConversationHistory[source]
Bases:
objectAPI-facing message history for provider SDK calls.
Unlike
PermanentHistory, turns here carry no timestamps and the history is intended to be cleared (e.g. when starting a new conversation context). Theget_turns()output is structured for direct injection into provider message APIs.- add_turn_assistant(content)[source]
Append an assistant turn to the history.
- Parameters:
content (str) – The assistant’s response text.
- Return type:
None
- class HistoryRecorder(context, permanent_history, ordered_history, memory=None, memory_persist=False, memory_persist_path=None)[source]
Bases:
objectRecords interactions to all 5 history stores in a single operation.
Owns the raw
historyandclean_historylists and coordinates writes toPermanentHistory,OrderedPromptHistory, andResponseContext. Optionally embeds each recorded Q+A pair into aMemoryinstance on a fire-and-forget background thread.Callers invoke
record()instead of manually writing to 5 separate stores.- Parameters:
context (ResponseContext) – The ResponseContext for prompt_attr_history recording.
permanent_history (PermanentHistory) – The PermanentHistory for chronological turns.
ordered_history (OrderedPromptHistory) – The OrderedPromptHistory for named interactions.
memory (Memory | None) – Optional Memory instance. When provided, each successful
record()call submits an embedding task to a dedicated single-worker thread pool. Failed embeds are logged atWARNINGand dropped; they never propagate to the caller.memory_persist (bool) – If
True, persist the memory store to Parquet after each successful embed. Requiresmemory_persist_pathto be set.memory_persist_path (str | None) – Fully-qualified file path for the Parquet persistence file. Ignored when
memory_persistisFalseormemoryisNone. Typically computed byFFAI.__init__()asf"{config.memory.persist_dir}/{config.memory.collection_name}.parquet".
- record(prompt, response, model, prompt_name=None, history=None, status='success', resolved_prompt=None, usage=None, metadata=None)[source]
Record an interaction to all 5 history stores.
When a
Memoryinstance is configured andstatus == "success", the Q+A pair is embedded on a fire-and-forget background thread. Metadata is derived fromprompt_namewhen not explicitly passed.- Parameters:
prompt (str) – The resolved prompt text.
response (Any) – The cleaned response.
model (str) – Model identifier used.
prompt_name (str | None) – Logical name for the prompt.
history (list[str] | None) – List of prompt names this call depends on.
status (str) – Execution status (“success”, “skipped”, “failed”). Only
"success"triggers embedding.resolved_prompt (str | None) – The fully interpolated prompt sent to the model.
usage (Any) – Token usage from the API call.
metadata (dict[str, Any] | None) – Optional caller metadata. When
None, derived fromprompt_nameas{"prompt_name": prompt_name}(or{}ifprompt_nameis alsoNone).
- Return type:
None
- class OrderedPromptHistory[source]
Bases:
objectManages ordered prompt-response history with named references.
This class provides: - Named prompt storage for declarative context assembly - Sequential ordering of interactions - Query capabilities by prompt name, model, etc. - History chain tracking for dependency resolution
- prompt_dict
OrderedDict mapping prompt names to interaction lists.
- get_effective_prompt_name(prompt_name)[source]
Get the effective prompt name from various input types.
- add_interaction(model, prompt, response, prompt_name=None, history=None)[source]
Add a new interaction to the history, storing cleaned versions of prompt and response.
- Parameters:
model (str) – The model used for the interaction
prompt (str) – The prompt text
response (str) – The response text
prompt_name (str | None) – Optional name/key for the prompt. If None, uses prompt text as key
history (list[str] | None) – Optional list of prompt names that form the history chain for this interaction
- Returns:
The created Interaction object
- Return type:
- get_interactions_by_prompt_name(prompt_name)[source]
Get all interactions for a specific prompt name.
- Parameters:
prompt_name (str)
- Return type:
- get_latest_interaction_by_prompt_name(prompt_name)[source]
Get the most recent interaction for a specific prompt name.
- Parameters:
prompt_name (str)
- Return type:
Interaction | None
- get_interactions_by_model_and_prompt_name(model, prompt_name)[source]
Get all interactions for a specific model and prompt name combination.
- Parameters:
- Return type:
- merge_histories(other)[source]
Merge another OrderedPromptHistory into this one.
- Parameters:
other (OrderedPromptHistory) – Another OrderedPromptHistory instance to merge
- Return type:
None
- get_interaction_by_prompt(prompt)[source]
Get an interaction by its exact prompt text.
Useful when prompt was used as the prompt_name.
- Parameters:
prompt (str)
- Return type:
Interaction | None
- class PermanentHistory[source]
Bases:
objectAppend-only chronological turn history with timestamps.
Each turn stores a
role("user"or"assistant"), structuredcontent, a per-turntimestamp, and optionalmetadata. Consecutive user turns are coalesced by appending content unless the caller passes non-Nonemetadata, in which case a new turn is always created (so distinctprompt_namemetadata is never silently merged).- add_turn_assistant(content, metadata=None)[source]
Append an assistant turn with the current timestamp.
- add_turn_user(content, metadata=None)[source]
Append a user turn, coalescing with the previous user turn when adjacent.
Coalescing only happens when metadata is
None. If metadata is provided, a new turn is always created so distinctprompt_namemetadata is never silently merged into the previous user turn.