Class: SmartAgent::AgentContext

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

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ AgentContext



64
65
66
# File 'lib/smart_agent/agent.rb', line 64

def initialize(agent)
  @agent = agent
end

Instance Method Details

#call_tools(result) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/smart_agent/agent.rb', line 135

def call_tools(result)
  @agent.processor(:tool).call({ :status => :start }) if @agent.processor(:tool)
  SmartAgent.logger.info("call tools: " + result.to_s)
  results = []
  result.call_tools.each do |tool|
    tool_call_id = tool["id"]
    tool_name = tool["function"]["name"].to_sym
    params = JSON.parse(tool["function"]["arguments"])
    if Tool.find_tool(tool_name)
      @agent.processor(:tool).call({ :content => "ToolName is `#{tool_name}`\n" }) if @agent.processor(:tool)
      @agent.processor(:tool).call({ :content => "params is `#{params}`\n" }) if @agent.processor(:tool)
      tool_result = Tool.find_tool(tool_name).call(params)

      SmartAgent.prompt_engine.history_messages << { "role" => "assistant", "content" => "", "tool_calls" => [tool] } #result.response.dig("choices", 0, "message")
      SmartAgent.prompt_engine.history_messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => tool_result.to_s.force_encoding("UTF-8") }
      results << tool_result
    end
    if server_name = MCPClient.find_server_by_tool_name(tool_name)
      @agent.processor(:tool).call({ :content => "MCP Server is `#{server_name}`, ToolName is `#{tool_name}`\n" }) if @agent.processor(:tool)
      @agent.processor(:tool).call({ :content => "params is `#{params}`\n" }) if @agent.processor(:tool)
      tool_result = MCPClient.new(server_name).call(tool_name, params)
      SmartAgent.prompt_engine.history_messages << { "role" => "assistant", "content" => "", "tool_calls" => [tool] } # result.response.dig("choices", 0, "message")
      SmartAgent.prompt_engine.history_messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => tool_result.to_s }
      results << tool_result
    end
    @agent.processor(:tool).call({ :content => " ... done\n" }) if @agent.processor(:tool)
  end
  @agent.processor(:tool).call({ :status => :end }) if @agent.processor(:tool)
  return results
end

#call_worker(name, params, with_tools: true, with_history: false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/smart_agent/agent.rb', line 68

def call_worker(name, params, with_tools: true, with_history: false)
  SmartAgent.logger.info ("Call Worker name is: #{name}")
  if with_tools
    simple_tools = []
    if @agent.tools
      simple_tools = @agent.tools.map { |tool_name| Tool.find_tool(tool_name).to_json }
    end
    if @agent.servers
      mcp_tools = @agent.servers.map { |mcp_name| MCPClient.new(mcp_name).to_json }
      mcp_tools.each do |tools|
        tools["tools"].each do |tool|
          simple_tools << tool
        end
      end
    end
    params[:tools] = simple_tools
  end
  params[:with_history] = with_history
  ret = nil
  if @agent.on_event
    full_result = {}
    tool_calls = []
    result = SmartAgent.prompt_engine.call_worker_by_stream(name, params) do |chunk, _bytesize|
      if full_result.empty?
        full_result["id"] = chunk["id"]
        full_result["object"] = chunk["object"]
        full_result["created"] = chunk["created"]
        full_result["model"] = chunk["model"]
        full_result["choices"] = [{
          "index" => 0,
          "message" => {
            "role" => "assistant",
            "content" => "",
            "reasoning_content" => "",
            "tool_calls" => [],
          },
        }]
        full_result["usage"] = chunk["usage"]
        full_result["system_fingerprint"] = chunk["system_fingerprint"]
      end
      if chunk.dig("choices", 0, "delta", "reasoning_content")
        full_result["choices"][0]["message"]["reasoning_content"] += chunk.dig("choices", 0, "delta", "reasoning_content")
        @agent.processor(:reasoning).call(chunk) if @agent.processor(:reasoning)
      end
      if chunk.dig("choices", 0, "delta", "content")
        full_result["choices"][0]["message"]["content"] += chunk.dig("choices", 0, "delta", "content")
        @agent.processor(:content).call(chunk) if @agent.processor(:content)
      end
      if chunk_tool_calls = chunk.dig("choices", 0, "delta", "tool_calls")
        chunk_tool_calls.each do |tool_call|
          if tool_calls.size > tool_call["index"]
            tool_calls[tool_call["index"]]["function"]["arguments"] += tool_call["function"]["arguments"]
          else
            tool_calls << tool_call
          end
        end
      end
    end
    full_result["choices"][0]["message"]["tool_calls"] = tool_calls
    result = full_result
  else
    result = SmartAgent.prompt_engine.call_worker(name, params)
  end
  ret = Result.new(result)
  return ret
end

#paramsObject



166
167
168
# File 'lib/smart_agent/agent.rb', line 166

def params
  @params ||= {}
end