PhoenixAI.Store.Adapters.ETS (PhoenixAI.Store v0.1.0)

Copy Markdown View Source

In-memory ETS adapter for PhoenixAI.Store.Adapter.

All functions receive an opts keyword list containing a :table key with the ETS table reference (typically owned by a TableOwner GenServer).

Storage Layout

  • Conversations: {{:conversation, id}, %Conversation{}}
  • Messages: {{:message, conversation_id, message_id}, %Message{}}
  • Facts: {{:fact, user_id, key}, %Fact{}}
  • Profiles: {{:profile, user_id}, %Profile{}}

Summary

Functions

Appends a message to a conversation in ETS. Returns {:error, :not_found} if the conversation does not exist.

Checks whether a conversation with the given ID exists in the ETS table.

Counts conversations in ETS matching the given filters.

Counts events in ETS matching the given filters.

Counts all facts for a user in ETS via a full match scan.

Deletes a conversation and all its messages and cost records from ETS.

Deletes a fact by {user_id, key} from ETS. Always returns :ok.

Deletes a user profile from ETS. Always returns :ok.

Returns all cost records for a conversation from ETS, sorted by recorded_at ascending.

Returns all facts for a user from ETS, sorted by inserted_at ascending.

Returns all messages for a conversation from ETS, sorted by inserted_at ascending.

Returns all conversations from ETS matching the given filters, sorted by inserted_at descending.

Returns a paginated, filtered list of events from ETS with an opaque cursor for the next page.

Loads a conversation by ID from ETS and eagerly populates its messages.

Loads a user profile from ETS. Returns {:error, :not_found} if absent.

Appends an event to the ETS table keyed on {inserted_at, id} for stable ordering.

Inserts or updates a conversation in the ETS table, preserving inserted_at on upsert.

Inserts a cost record into ETS keyed on {conversation_id, record_id}.

Upserts a fact in ETS keyed on {user_id, key}, preserving inserted_at on update.

Upserts a user profile in ETS keyed on user_id, preserving inserted_at on update.

Sums token_count across all messages for a conversation stored in ETS.

Sums total_cost across all cost records matching the given filters in ETS.

Sums token_count across all messages in all conversations belonging to a user in ETS.

Functions

add_message(conversation_id, message, opts)

Appends a message to a conversation in ETS. Returns {:error, :not_found} if the conversation does not exist.

conversation_exists?(id, opts)

Checks whether a conversation with the given ID exists in the ETS table.

count_conversations(filters, opts)

Counts conversations in ETS matching the given filters.

Note: O(n) — materializes the full filtered list then counts. Use the Ecto adapter for production workloads requiring efficient counts.

count_events(filters, opts)

Counts events in ETS matching the given filters.

count_facts(user_id, opts)

Counts all facts for a user in ETS via a full match scan.

delete_conversation(id, opts)

Deletes a conversation and all its messages and cost records from ETS.

delete_fact(user_id, key, opts)

Deletes a fact by {user_id, key} from ETS. Always returns :ok.

delete_profile(user_id, opts)

Deletes a user profile from ETS. Always returns :ok.

get_cost_records(conversation_id, opts)

Returns all cost records for a conversation from ETS, sorted by recorded_at ascending.

get_facts(user_id, opts)

Returns all facts for a user from ETS, sorted by inserted_at ascending.

get_messages(conversation_id, opts)

Returns all messages for a conversation from ETS, sorted by inserted_at ascending.

list_conversations(filters, opts)

Returns all conversations from ETS matching the given filters, sorted by inserted_at descending.

list_events(filters, opts)

Returns a paginated, filtered list of events from ETS with an opaque cursor for the next page.

load_conversation(id, opts)

Loads a conversation by ID from ETS and eagerly populates its messages.

load_profile(user_id, opts)

Loads a user profile from ETS. Returns {:error, :not_found} if absent.

log_event(event, opts)

Appends an event to the ETS table keyed on {inserted_at, id} for stable ordering.

save_conversation(conversation, opts)

Inserts or updates a conversation in the ETS table, preserving inserted_at on upsert.

save_cost_record(record, opts)

Inserts a cost record into ETS keyed on {conversation_id, record_id}.

save_fact(fact, opts)

Upserts a fact in ETS keyed on {user_id, key}, preserving inserted_at on update.

save_profile(profile, opts)

Upserts a user profile in ETS keyed on user_id, preserving inserted_at on update.

sum_conversation_tokens(conversation_id, opts)

Sums token_count across all messages for a conversation stored in ETS.

sum_cost(filters, opts)

Sums total_cost across all cost records matching the given filters in ETS.

sum_user_tokens(user_id, opts)

Sums token_count across all messages in all conversations belonging to a user in ETS.