Class: Nanobot::Agent::ContextBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/nanobot/agent/context.rb

Overview

ContextBuilder assembles the system prompt and message context for LLM calls

Constant Summary collapse

BOOTSTRAP_FILES =
%w[AGENTS.md SOUL.md USER.md TOOLS.md IDENTITY.md].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace, logger: nil) ⇒ ContextBuilder



17
18
19
20
21
# File 'lib/nanobot/agent/context.rb', line 17

def initialize(workspace, logger: nil)
  @workspace = Pathname.new(workspace).expand_path
  @memory_store = MemoryStore.new(@workspace)
  @logger = logger || Logger.new(IO::NULL)
end

Instance Attribute Details

#memory_store ⇒ Object (readonly)

Returns the value of attribute memory_store.



13
14
15
# File 'lib/nanobot/agent/context.rb', line 13

def memory_store
  @memory_store
end

#workspace ⇒ Object (readonly)

Returns the value of attribute workspace.



13
14
15
# File 'lib/nanobot/agent/context.rb', line 13

def workspace
  @workspace
end

Instance Method Details

#add_tool_result(messages, tool_call_id, tool_name, result) ⇒ Array<Hash>

Add a tool result to messages



91
92
93
94
95
96
97
98
99
# File 'lib/nanobot/agent/context.rb', line 91

def add_tool_result(messages, tool_call_id, tool_name, result)
  messages << {
    role: 'tool',
    tool_call_id: tool_call_id,
    name: tool_name,
    content: result
  }
  messages
end

#build_messages(current_message:, history: [], channel: nil, chat_id: nil) ⇒ Array<Hash>

Build messages array for LLM



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nanobot/agent/context.rb', line 60

def build_messages(current_message:, history: [], channel: nil, chat_id: nil)
  messages = []

  # System prompt
  system_prompt = build_system_prompt
  messages << { role: 'system', content: system_prompt }

  # Add history
  history.each do |msg|
    messages << msg
  end
  @logger.debug "History: #{history.length} messages"

  # Add channel context as a separate system message if provided
  if channel && chat_id
    messages << { role: 'system', content: "Current channel: #{channel}, Chat ID: #{chat_id}" }
  end

  # Add current message
  messages << { role: 'user', content: current_message }
  @logger.debug "Built #{messages.length} messages for LLM"

  messages
end

#build_system_prompt ⇒ String

Build the complete system prompt



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanobot/agent/context.rb', line 25

def build_system_prompt
  parts = []

  # Runtime information
  parts << build_runtime_info

  # Bootstrap files
  BOOTSTRAP_FILES.each do |filename|
    content = read_bootstrap_file(filename)
    if content
      parts << content
      @logger.debug "Loaded bootstrap file: #{filename} (#{content.length} chars)"
    else
      @logger.debug "Bootstrap file not found or empty: #{filename}"
    end
  end

  # Memory context
  memory_context = @memory_store.get_memory_context
  if memory_context
    parts << "# Memory\n\n#{memory_context}"
    @logger.debug "Loaded memory context (#{memory_context.length} chars)"
  end

  prompt = parts.join("\n\n---\n\n")
  @logger.debug "System prompt assembled: #{prompt.length} chars total"
  prompt
end