Class: PromptNavigator::PromptExecution

Inherits:
ApplicationRecord show all
Defined in:
app/models/prompt_navigator/prompt_execution.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_set!(ids) ⇒ Object

Bulk-delete a set of PromptExecutions, tolerating the self-referential ‘previous_id` foreign key by first nulling intra-set links. Callers (e.g. a host’s Chat#destroy flow) pass the ids of orphaned executions after their owning records (Messages) have been destroyed.

Raises ActiveRecord::InvalidForeignKey if any PE in the set is still referenced from outside the set (e.g. another chat’s branch); callers decide whether to rescue and leave the orphans in place.



24
25
26
27
28
29
30
# File 'app/models/prompt_navigator/prompt_execution.rb', line 24

def self.delete_set!(ids)
  ids = Array(ids).compact
  return if ids.empty?

  where(id: ids).update_all(previous_id: nil)
  where(id: ids).delete_all
end

Instance Method Details

#build_context(limit: nil) ⇒ Object

Builds a context array from the direct lineage for summarization. Each entry contains { prompt:, response: } from ancestor PromptExecutions. Optionally limit to the most recent N ancestors.



10
11
12
13
14
# File 'app/models/prompt_navigator/prompt_execution.rb', line 10

def build_context(limit: nil)
  ancestors(limit: limit).map do
    { prompt: it.prompt, response: it.response }
  end
end