Skip to content
Kward Search API index

Class: Kward::Compaction::Preparation

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/compactor.rb

Overview

Compaction support object used by conversation summarization.

Instance Method Summary collapse

Constructor Details

#initialize(conversation:, settings: Settings.new, estimator: TokenEstimator.new, cut_point_finder: CutPointFinder.new(estimator: estimator), file_operation_tracker: FileOperationTracker.new) ⇒ Preparation

Creates an object for conversation compaction.



335
336
337
338
339
340
341
# File 'lib/kward/compactor.rb', line 335

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

#callObject

Raises:



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/kward/compactor.rb', line 343

def call
  branch_entries = entry_messages(@conversation.messages)
  raise NothingToCompact, "Nothing to compact" if branch_entries.empty?
  raise AlreadyCompacted, "Already compacted" if compaction_entry?(branch_entries.last)

  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.messages_to_summarize.empty? && cut.turn_prefix_messages.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.messages_to_summarize + cut.turn_prefix_messages
  file_ops = @file_operation_tracker.call(summarized_for_file_ops, previous_details: compaction_details(previous_entry))
  preserved_skill_messages = activated_skill_messages(summarized_for_file_ops)
  kept_messages = preserved_skill_messages + Array(cut.preserved_messages) + (branch_entries[cut.first_kept_index..] || [])

  PreparationResult.new(
    first_kept_entry_id: first_kept_entry_id,
    messages_to_summarize: cut.messages_to_summarize,
    kept_messages: kept_messages,
    turn_prefix_messages: cut.turn_prefix_messages,
    split_turn: cut.split_turn,
    tokens_before: @estimator.context_tokens(@conversation.context_messages),
    previous_summary: previous_entry ? compaction_summary(previous_entry) : nil,
    file_ops: file_ops,
    settings: @settings
  )
end