Class: AgUi::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ag_ui/server.rb,
lib/ag_ui/server/info.rb,
lib/ag_ui/server/triage.rb,
lib/ag_ui/server/sse/stream.rb,
lib/ag_ui/server/sse/event_encoder.rb,
lib/ag_ui/server/middleware/sse_stream.rb

Overview

Rack application exposing the AG-UI runtime surface the CopilotKit client expects (doc 09): /info, /agent/:id/run (SSE), /agent/:id/connect (stub), /agent/:id/stop/:threadId (ack).

The block is the run handler. It receives the rack env with env (parsed RunAgentInput) and env (StreamBuilder) and drives the run:

app = AgUi.agent(agent_id: "default") do |env|
input = env["ag_ui.input"]
env["ag_ui.stream"].open(thread_id: input.thread_id, run_id: input.run_id) do |s|
  s.run_started
  s.text_message_start(message_id: "m1")
  s.text_message_content(message_id: "m1", delta: "Hello")
  s.text_message_end(message_id: "m1")
  s.run_finished
end
end

# config.ru / Rails: mount at the CopilotKit runtimeUrl
map("/api/copilotkit") { run app }

Defined Under Namespace

Modules: Info, Middleware, SSE Classes: RecordingBuilder, Triage

Constant Summary collapse

JSON_HEADERS =
{ "content-type" => "application/json" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(agent_id: "default", description: nil, a2ui_enabled: false, info_overrides: {}, validate: true, store: RunStore::InMemory.new, &block) ⇒ Server

store: run bookkeeping for /connect replay + /stop cancellation. Defaults to the in-memory store; pass store: nil for the stateless stub behaviour (connect closes immediately, stop only acks).



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ag_ui/server.rb', line 39

def initialize(agent_id: "default", description: nil, a2ui_enabled: false,
               info_overrides: {}, validate: true,
               store: RunStore::InMemory.new, &block)
  unless block
    raise ArgumentError, "Server requires a run-handler block"
  end

  @agent_id = agent_id
  @info = Info.payload(
    agent_id: agent_id,
    description: description,
    a2ui_enabled: a2ui_enabled,
    overrides: info_overrides,
  )
  @validate = validate
  @store = store
  @handler = block
  @app = build_app
end

Instance Method Details

#call(env) ⇒ Object



59
60
61
# File 'lib/ag_ui/server.rb', line 59

def call(env)
  @app.call(env)
end