Class: CollavreOpenclaw::OpenclawAdapter

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre_openclaw/openclaw_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(user:, system_prompt:, context: {}) ⇒ OpenclawAdapter

Pure transport adapter for OpenClaw AI Gateway. Session context filtering (full vs incremental) is handled upstream by SessionContextResolver — this adapter sends exactly what it receives.

Transport modes:

  1. WebSocket (primary) - via faye-websocket + EventMachine

  2. HTTP (fallback) - via Faraday POST /v1/chat/completions

Session mapping:

Collavre Topic → OpenClaw Session (1:1)
Same Topic, multiple users → shared context
Different Topics → isolated sessions


19
20
21
22
23
# File 'app/services/collavre_openclaw/openclaw_adapter.rb', line 19

def initialize(user:, system_prompt:, context: {})
  @user = user
  @system_prompt = system_prompt
  @context = context
end

Instance Method Details

#callback_urlObject

Get the callback URL for this user (kept for backward compatibility)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/collavre_openclaw/openclaw_adapter.rb', line 55

def callback_url
  return nil unless @user

  host_options = default_url_options
  return nil if host_options[:host].blank?

  CollavreOpenclaw::Engine.routes.url_helpers.callback_url(
    user_id: @user.id,
    **host_options
  )
rescue StandardError => e
  Rails.logger.warn("[CollavreOpenclaw] Failed to generate callback URL: #{e.message}")
  nil
end

#chat(messages_data, &block) ⇒ Object

Parameters:

  • messages_data (Hash)

    { messages:, first_message:, context_changed: }



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
# File 'app/services/collavre_openclaw/openclaw_adapter.rb', line 26

def chat(messages_data, &block)
  parse_messages_data!(messages_data)

  unless @user&.gateway_url.present?
    Rails.logger.error("[CollavreOpenclaw] No Gateway URL configured for user #{@user&.id}")
    yield "Error: OpenClaw Gateway URL not configured" if block_given?
    return nil
  end

  unless @user&.llm_api_key.present?
    Rails.logger.error("[CollavreOpenclaw] No API key configured for user #{@user&.id}")
    yield "Error: OpenClaw API key not configured or decryption failed. " \
          "Please re-enter the API key in AI agent settings." if block_given?
    return nil
  end

  # Try WebSocket first, fall back to HTTP
  # Set OPENCLAW_TRANSPORT=http to force HTTP-only mode
  if CollavreOpenclaw.config.transport == "http"
    Rails.logger.info("[CollavreOpenclaw::WS] TRANSPORT mode=http_forced")
    chat_via_http(&block)
  elsif websocket_available?
    chat_via_websocket(&block)
  else
    chat_via_http(&block)
  end
end

#session_keyObject

Get the stable session key for this context



71
72
73
# File 'app/services/collavre_openclaw/openclaw_adapter.rb', line 71

def session_key
  @session_key ||= build_session_key
end