Class: SolidLoop::Middlewares::NetworkCalling
- Inherits:
-
Object
- Object
- SolidLoop::Middlewares::NetworkCalling
- Defined in:
- app/services/solid_loop/middlewares/network_calling.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ NetworkCalling
constructor
A new instance of NetworkCalling.
Constructor Details
#initialize(app) ⇒ NetworkCalling
Returns a new instance of NetworkCalling.
6 7 8 |
# File 'app/services/solid_loop/middlewares/network_calling.rb', line 6 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/services/solid_loop/middlewares/network_calling.rb', line 10 def call(env) adapter_class = SolidLoop::Adapters::Native # Determine the dialect from the agent dialect_name = env.agent.llm_dialect_name.to_s dialect = case dialect_name when "gemini" then SolidLoop::Dialects::Gemini.new when "anthropic" then SolidLoop::Dialects::Anthropic.new else SolidLoop::Dialects::OpenAi.new end adapter = adapter_class.new( base_url: env.model_config[:base_url], api_token: env.model_config[:api_token], model_name: env.model_config[:llm_model_name], payload: env.payload, streaming: !!env.agent.streaming?, reasoning_strategies: env.agent.reasoning_strategies, dialect: dialect, fallback_messages_text: env., read_timeout: env.model_config[:read_timeout], cancellation_check: -> { env.loop_active_fresh? } ) if env.agent.streaming? # Born hidden: a mid-stream partial is not canonical # history. The ONLY path that flips it visible is the canonical LLM # commit txn in ResponseParsing (status: success + is_hidden: false). # Reclaim/pause/stop/error keep it hidden and mark it failed, so a # retried turn never resends a partial (MessageBuilding filters # is_hidden: false). Hosts opt into showing live processing shells. env. = env.loop..create!( role: "assistant", status: "processing", content: "", reasoning_content: "", is_hidden: true ) adapter.on_chunk_proc = proc do |content, reasoning, tool_calls, metrics| # Business logic: Check if we should continue if !env.loop_active_fresh? Rails.logger.warn "Loop #{env.loop.id} is no longer active. Cancelling stream from middleware." raise SolidLoop::CancellationError, "Loop status changed to #{env.loop.status}" end env..trigger_content_append( content, reasoning, tool_calls, **metrics.to_h ) end end env.request_url = adapter.build_url # Execute and capture results back into env result = adapter.call env.response = result[:response] env.normalized_data = result[:normalized_data] env.duration = result[:duration] env.aggregator = result[:aggregator] # Merge logs back to environment buffers env.log_buffer.write(result[:log]) if env.log_buffer env.debug_output.write(result[:debug_log]) if env.debug_output @app.call(env) end |