Class: PromptObjects::Server::App
- Inherits:
-
Object
- Object
- PromptObjects::Server::App
- 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
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
Instance Method Summary collapse
- #blocking_executor ⇒ Object
-
#broadcast(message) ⇒ Object
Broadcast a message to all connected WebSocket clients.
- #call(env) ⇒ Object
- #close ⇒ Object
-
#initialize(runtime, blocking_executor: nil) ⇒ App
constructor
A new instance of App.
-
#register_connection(handler) ⇒ Object
Register a connection handler.
-
#unregister_connection(handler) ⇒ Object
Unregister a connection handler.
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.("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
#connections ⇒ Object (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_executor ⇒ Object
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.
63 64 65 66 67 68 69 70 71 |
# File 'lib/prompt_objects/server/app.rb', line 63 def broadcast() @connections_mutex.synchronize do @connections.each do |handler| handler.() rescue StandardError => e puts "Broadcast error: #{e.}" 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 |
#close ⇒ Object
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 |