Class: AgUi::Middleware::ToolRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/ag_ui/middleware/tool_router.rb

Overview

Brute-style turn middleware implementing both halves of the AG-UI tool model (doc 03).

Way in: advertises the run's client tool definitions (RunAgentInput.tools) plus the app's SERVER tool definitions on env (idempotent — the loop re-enters this middleware every iteration).

Way out, per tool call the model made:

- every call emits TOOL_CALL_START / ARGS / END
- a SERVER tool ({name:, description:, parameters:, handler:})
executes inline: its result is appended as a :tool message
(Loop::ToolResult re-invokes the stack so the model continues
the same run) and emitted as TOOL_CALL_RESULT — the Node
runtime's exact shape
- a CLIENT tool sets env[:should_exit]: the run ends with plain
RUN_FINISHED and the browser executes it (multi-run model)
- mixed turns: server tools still execute, but any client call
ends the run

Instance Method Summary collapse

Constructor Details

#initialize(app, tools: nil, server_tools: nil) ⇒ ToolRouter

Returns a new instance of ToolRouter.



30
31
32
33
34
# File 'lib/ag_ui/middleware/tool_router.rb', line 30

def initialize(app, tools: nil, server_tools: nil)
  @app = app
  @client_tools = tools || []
  @server_tools = (server_tools || []).to_h { |t| [t[:name].to_s, t] }
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ag_ui/middleware/tool_router.rb', line 36

def call(env)
  advertise(env)

  @app.call(env)

  last = env[:messages].last
  if last.respond_to?(:tool_call?) && last.tool_call?
    route(env, last.tool_calls)
  end

  env
end