Class: SmartAgent::AgentContext

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

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ AgentContext

Returns a new instance of 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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/smart_agent/agent.rb', line 103

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_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}`" }) if @agent.processor(:tool)
      results << Tool.new(tool_name).call(params)
    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}`" }) if @agent.processor(:tool)
      results << MCPClient.new(server_name).call(tool_name, params)
    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, result: nil, with_tools: true) ⇒ 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
# File 'lib/smart_agent/agent.rb', line 68

def call_worker(name, params, result: nil, with_tools: true)
  if result
    params[:result] = result
  end
  if with_tools
    if @agent.tools
      simple_tools = @agent.tools.map { |tool_name| Tool.new(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
  ret = nil
  if @agent.on_event && with_tools == false
    SmartAgent.prompt_engine.call_worker_by_stream(name, params) do |chunk, _bytesize|
      if chunk.dig("choices", 0, "delta", "reasoning_content")
        @agent.processor(:reasoning).call(chunk) if @agent.processor(:reasoning)
      end
      if chunk.dig("choices", 0, "delta", "content")
        @agent.processor(:content).call(chunk) if @agent.processor(:content)
      end
    end
  else
    result = SmartAgent.prompt_engine.call_worker(name, params)
    ret = Result.new(result)
    return ret
  end
end

#paramsObject



124
125
126
# File 'lib/smart_agent/agent.rb', line 124

def params
  @params ||= {}
end