Class: PromptObjects::Server::App

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/server/app.rb

Overview

Main Rack application for the PromptObjects web server. Routes requests to WebSocket handler, API, or static assets.

Constant Summary collapse

STATIC_EXTENSIONS =
%w[.html .js .css .png .svg .ico .woff .woff2 .map .json].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime, blocking_executor: nil) ⇒ App

Returns a new instance of App.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prompt_objects/server/app.rb', line 16

def initialize(runtime, blocking_executor: nil)
  @runtime = runtime
  @blocking_executor = blocking_executor
  @api = API::Routes.new(runtime)
  @public_path = File.expand_path("public", __dir__)
  @connections = []
  @connections_mutex = Mutex.new

  # Broadcast PO-to-PO delegation events to all connected clients.
  # This makes delegated POs visible in the UI (canvas + chat).
  @runtime.on_delegation_event = ->(event_type, payload) {
    broadcast(
      type: "po_delegation_#{event_type}",
      payload: payload
    )
  }

end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



14
15
16
# File 'lib/prompt_objects/server/app.rb', line 14

def connections
  @connections
end

Instance Method Details

#blocking_executorObject



35
36
37
38
39
# File 'lib/prompt_objects/server/app.rb', line 35

def blocking_executor
  @blocking_executor ||= Execution::BlockingExecutor.new(
    size: Integer(ENV.fetch("PROMPT_OBJECTS_BLOCKING_WORKERS", 4))
  )
end

#broadcast(message) ⇒ Object

Broadcast a message to all connected WebSocket clients.

Parameters:

  • message (Hash)

    Message to broadcast



63
64
65
66
67
68
69
70
71
# File 'lib/prompt_objects/server/app.rb', line 63

def broadcast(message)
  @connections_mutex.synchronize do
    @connections.each do |handler|
      handler.send_message(message)
    rescue StandardError => e
      puts "Broadcast error: #{e.message}" if ENV["DEBUG"]
    end
  end
end

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/prompt_objects/server/app.rb', line 45

def call(env)
  request_path = env["PATH_INFO"]

  if websocket_request?(env)
    handle_websocket(env)
  elsif request_path.start_with?("/api/")
    @api.call(env)
  elsif request_path.start_with?("/artifacts/")
    serve_artifact_viewer(request_path)
  elsif static_asset?(request_path)
    serve_static(request_path)
  else
    serve_index
  end
end

#closeObject



41
42
43
# File 'lib/prompt_objects/server/app.rb', line 41

def close
  @blocking_executor&.close
end

#register_connection(handler) ⇒ Object

Register a connection handler.



74
75
76
77
78
# File 'lib/prompt_objects/server/app.rb', line 74

def register_connection(handler)
  @connections_mutex.synchronize do
    @connections << handler
  end
end

#unregister_connection(handler) ⇒ Object

Unregister a connection handler.



81
82
83
84
85
# File 'lib/prompt_objects/server/app.rb', line 81

def unregister_connection(handler)
  @connections_mutex.synchronize do
    @connections.delete(handler)
  end
end