Skip to content
Kward Search API index

Class: Kward::RPC::TurnContext

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

Overview

Normalizes optional client turn context and renders it for model input.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize(context) ⇒ Object



7
8
9
# File 'lib/kward/rpc/turn_context.rb', line 7

def self.normalize(context)
  new.normalize(context)
end

.prompt(context) ⇒ Object



11
12
13
# File 'lib/kward/rpc/turn_context.rb', line 11

def self.prompt(context)
  new.prompt(context)
end

Instance Method Details

#normalize(context) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kward/rpc/turn_context.rb', line 15

def normalize(context)
  return nil if context.nil?
  raise ArgumentError, "turn context must be an object" unless context.is_a?(Hash)

  active_file = blank_to_nil(value(context, "activeFile"))
  open_files = array_value(context, "openFiles")
  selection = normalize_selection(value(context, "selection"))
  diagnostics = normalize_diagnostics(value(context, "diagnostics"))
  normalized = { active_file: active_file, open_files: open_files, selection: selection, diagnostics: diagnostics }.compact
  normalized.empty? ? nil : normalized
end

#prompt(context) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/kward/rpc/turn_context.rb', line 27

def prompt(context)
  lines = ["Additional client context:"]
  lines << "- Active file: #{context[:active_file]}" if context[:active_file]
  lines << "- Open files: #{context[:open_files].join(", ")}" if context[:open_files]&.any?
  append_selection(lines, context[:selection]) if context[:selection]
  append_diagnostics(lines, context[:diagnostics])
  lines.join("\n")
end