Class: Kward::Compaction::Preparation
- Inherits:
-
Object
- Object
- Kward::Compaction::Preparation
- Defined in:
- lib/kward/compactor.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(conversation:, settings: Settings.new, estimator: TokenEstimator.new, cut_point_finder: CutPointFinder.new(estimator: estimator), file_operation_tracker: FileOperationTracker.new) ⇒ Preparation
constructor
A new instance of Preparation.
Constructor Details
#initialize(conversation:, settings: Settings.new, estimator: TokenEstimator.new, cut_point_finder: CutPointFinder.new(estimator: estimator), file_operation_tracker: FileOperationTracker.new) ⇒ Preparation
Returns a new instance of Preparation.
500 501 502 503 504 505 506 |
# File 'lib/kward/compactor.rb', line 500 def initialize(conversation:, settings: Settings.new, estimator: TokenEstimator.new, cut_point_finder: CutPointFinder.new(estimator: estimator), file_operation_tracker: FileOperationTracker.new) @conversation = conversation @settings = settings @estimator = estimator @cut_point_finder = cut_point_finder @file_operation_tracker = file_operation_tracker end |
Instance Method Details
#call ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/kward/compactor.rb', line 508 def call branch_entries = (@conversation.) raise NothingToCompact, "Nothing to compact" if branch_entries.empty? raise AlreadyCompacted, "Already compacted" if compaction_entry?(branch_entries.last) || already_compacted? previous_index = latest_previous_compaction_index(branch_entries) previous_entry = previous_index ? branch_entries[previous_index] : nil boundary_start = boundary_start_index(branch_entries, previous_index, previous_entry) raise NothingToCompact, "Nothing to compact" if boundary_start >= branch_entries.length cut = @cut_point_finder.find(entries: branch_entries, start_index: boundary_start, keep_recent_tokens: @settings.keep_recent_tokens) raise NothingToCompact, "Nothing to compact" unless cut raise NothingToCompact, "Nothing to compact" if cut..empty? && cut..empty? first_kept_index = cut.preserved_start_index || cut.first_kept_index first_kept_entry_id = entry_id(branch_entries[first_kept_index], first_kept_index) summarized_for_file_ops = cut. + cut. file_ops = @file_operation_tracker.call(summarized_for_file_ops, previous_details: compaction_details(previous_entry)) = Array(cut.) + (branch_entries[cut.first_kept_index..] || []) PreparationResult.new( first_kept_entry_id: first_kept_entry_id, messages_to_summarize: cut., kept_messages: , turn_prefix_messages: cut., split_turn: cut.split_turn, tokens_before: @estimator.context_tokens(@conversation.), previous_summary: previous_entry ? compaction_summary(previous_entry) : nil, file_ops: file_ops, settings: @settings ) end |