Add external AI assistant with Pipelock security proxy (#1069)

* feat(helm): add Pipelock ConfigMap, scanning config, and consolidate compose

- Add ConfigMap template rendering DLP, response scanning, MCP input/tool
  scanning, and forward proxy settings from values
- Mount ConfigMap as /etc/pipelock/pipelock.yaml volume in deployment
- Add checksum/config annotation for automatic pod restart on config change
- Gate HTTPS_PROXY/HTTP_PROXY env injection on forwardProxy.enabled (skip
  in MCP-only mode)
- Use hasKey for all boolean values to prevent Helm default swallowing false
- Single source of truth for ports (forwardProxy.port/mcpProxy.port)
- Pipelock-specific imagePullSecrets with fallback to app secrets
- Merge standalone compose.example.pipelock.yml into compose.example.ai.yml
- Add pipelock.example.yaml for Docker Compose users
- Add exclude-paths to CI workflow for locale file false positives

* Add external assistant support (OpenAI-compatible SSE proxy)

Allow self-hosted instances to delegate chat to an external AI agent
via an OpenAI-compatible streaming endpoint. Configurable per-family
through Settings UI or ASSISTANT_TYPE env override.

- Assistant::External::Client: SSE streaming HTTP client (no new gems)
- Settings UI with type selector, env lock indicator, config status
- Helm chart and Docker Compose env var support
- 45 tests covering client, config, routing, controller, integration

* Add session key routing, email allowlist, and config plumbing

Route to the actual OpenClaw session via x-openclaw-session-key header
instead of creating isolated sessions. Gate external assistant access
behind an email allowlist (EXTERNAL_ASSISTANT_ALLOWED_EMAILS env var).
Plumb session_key and allowedEmails through Helm chart, compose, and
env template.

* Add HTTPS_PROXY support to External::Client for Pipelock integration

Net::HTTP does not auto-read HTTPS_PROXY/HTTP_PROXY env vars (unlike
Faraday). Explicitly resolve proxy from environment in build_http so
outbound traffic to the external assistant routes through Pipelock's
forward proxy when enabled. Respects NO_PROXY for internal hosts.

* Add UI fields for external assistant config (Setting-backed with env fallback)

Follow the same pattern as OpenAI settings: database-backed Setting
fields with env var defaults. Self-hosters can now configure the
external assistant URL, token, and agent ID from the browser
(Settings > Self-Hosting > AI Assistant) instead of requiring env vars.
Fields disable when the corresponding env var is set.

* Improve external assistant UI labels and add help text

Change placeholder to generic OpenAI-compatible URL pattern. Add help
text under each field explaining where the values come from: URL from
agent provider, token for authentication, agent ID for multi-agent
routing.

* Add external assistant docs and fix URL help text

Add External AI Assistant section to docs/hosting/ai.md covering setup
(UI and env vars), how it works, Pipelock security scanning, access
control, and Docker Compose example. Drop "chat completions" jargon
from URL help text.

* Harden external assistant: retry logic, disconnect UI, error handling, and test coverage

- Add retry with backoff for transient network errors (no retry after streaming starts)
- Add disconnect button with confirmation modal in self-hosting settings
- Narrow rescue scope with fallback logging for unexpected errors
- Safe cleanup of partial responses on stream interruption
- Gate ai_available? on family assistant_type instead of OR-ing all providers
- Truncate conversation history to last 20 messages
- Proxy-aware HTTP client with NO_PROXY support
- Sanitize protocol to use generic headers (X-Agent-Id, X-Session-Key)
- Full test coverage for streaming, retries, proxy routing, config, and disconnect

* Exclude external assistant client from Pipelock scan-diff

False positive: `@token` instance variable flagged as "Credential in URL".
Temporary workaround until Pipelock supports inline suppression.

* Address review feedback: NO_PROXY boundary fix, SSE done flag, design tokens

- Fix NO_PROXY matching to require domain boundary (exact match or .suffix),
  case-insensitive. Prevents badexample.com matching example.com.
- Add done flag to SSE streaming so read_body stops after [DONE]
- Move MAX_CONVERSATION_MESSAGES to class level
- Use bg-success/bg-destructive design tokens for status indicators
- Add rationale comment for pipelock scan exclusion
- Update docs last-updated date

* Address second round of review feedback

- Allowlist email comparison is now case-insensitive and nil-safe
- Cap SSE buffer at 1 MB to prevent memory blowup from malformed streams
- Don't expose upstream HTTP response body in user-facing errors (log it instead)
- Fix frozen string warning on buffer initialization
- Fix "builtin" typo in docs (should be "built-in")

* Protect completed responses from cleanup, sanitize error messages

- Don't destroy a fully streamed assistant message if post-stream
  metadata update fails (only cleanup partial responses)
- Log raw connection/HTTP errors internally, show generic messages
  to users to avoid leaking network/proxy details
- Update test assertions for new error message wording

* Fix SSE content guard and NO_PROXY test correctness

Use nil check instead of present? for SSE delta content to preserve
whitespace-only chunks (newlines, spaces) that can occur in code output.

Fix NO_PROXY test to use HTTP_PROXY matching the http:// client URL so
the proxy resolution and NO_PROXY bypass logic are actually exercised.

* Forward proxy credentials to Net::HTTP

Pass proxy_uri.user and proxy_uri.password to Net::HTTP.new so
authenticated proxies (http://user:pass@host:port) work correctly.
Without this, credentials parsed from the proxy URL were silently
dropped. Nil values are safe as positional args when no creds exist.

* Update pipelock integration to v0.3.1 with full scanning config

Bump Helm image tag from 0.2.7 to 0.3.1. Add missing security
sections to both the Helm ConfigMap and compose example config:
mcp_tool_policy, mcp_session_binding, and tool_chain_detection.
These protect the /mcp endpoint against tool injection, session
hijacking, and multi-step exfiltration chains.

Add version and mode fields to config files. Enable include_defaults
for DLP and response scanning to merge user patterns with the 35
built-in patterns. Remove redundant --mode CLI flag from the Helm
deployment template since mode is now in the config file.
This commit is contained in:
LPW
2026-03-03 09:47:51 -05:00
committed by GitHub
parent ad24c3aba5
commit 84bfe5b7ab
24 changed files with 1401 additions and 24 deletions

View File

@@ -136,6 +136,98 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTest
assert_not Balance.exists?(account_balance.id)
end
test "can update assistant type to external" do
with_self_hosting do
assert_equal "builtin", users(:family_admin).family.assistant_type
patch settings_hosting_url, params: { family: { assistant_type: "external" } }
assert_redirected_to settings_hosting_url
assert_equal "external", users(:family_admin).family.reload.assistant_type
end
end
test "ignores invalid assistant type values" do
with_self_hosting do
patch settings_hosting_url, params: { family: { assistant_type: "hacked" } }
assert_redirected_to settings_hosting_url
assert_equal "builtin", users(:family_admin).family.reload.assistant_type
end
end
test "ignores assistant type update when ASSISTANT_TYPE env is set" do
with_self_hosting do
with_env_overrides("ASSISTANT_TYPE" => "external") do
patch settings_hosting_url, params: { family: { assistant_type: "external" } }
assert_redirected_to settings_hosting_url
# DB value should NOT change when env override is active
assert_equal "builtin", users(:family_admin).family.reload.assistant_type
end
end
end
test "can update external assistant settings" do
with_self_hosting do
patch settings_hosting_url, params: { setting: {
external_assistant_url: "https://agent.example.com/v1/chat",
external_assistant_token: "my-secret-token",
external_assistant_agent_id: "finance-bot"
} }
assert_redirected_to settings_hosting_url
assert_equal "https://agent.example.com/v1/chat", Setting.external_assistant_url
assert_equal "my-secret-token", Setting.external_assistant_token
assert_equal "finance-bot", Setting.external_assistant_agent_id
end
ensure
Setting.external_assistant_url = nil
Setting.external_assistant_token = nil
Setting.external_assistant_agent_id = nil
end
test "does not overwrite token with masked placeholder" do
with_self_hosting do
Setting.external_assistant_token = "real-secret"
patch settings_hosting_url, params: { setting: { external_assistant_token: "********" } }
assert_equal "real-secret", Setting.external_assistant_token
end
ensure
Setting.external_assistant_token = nil
end
test "disconnect external assistant clears settings and resets type" do
with_self_hosting do
Setting.external_assistant_url = "https://agent.example.com/v1/chat"
Setting.external_assistant_token = "token"
Setting.external_assistant_agent_id = "finance-bot"
users(:family_admin).family.update!(assistant_type: "external")
delete disconnect_external_assistant_settings_hosting_url
assert_redirected_to settings_hosting_url
assert_not Assistant::External.configured?
assert_equal "builtin", users(:family_admin).family.reload.assistant_type
end
ensure
Setting.external_assistant_url = nil
Setting.external_assistant_token = nil
Setting.external_assistant_agent_id = nil
end
test "disconnect external assistant requires admin" do
with_self_hosting do
sign_in users(:family_member)
delete disconnect_external_assistant_settings_hosting_url
assert_redirected_to settings_hosting_url
assert_equal I18n.t("settings.hostings.not_authorized"), flash[:alert]
end
end
test "can clear data only when admin" do
with_self_hosting do
sign_in users(:family_member)

View File

@@ -0,0 +1,283 @@
require "test_helper"
class Assistant::External::ClientTest < ActiveSupport::TestCase
setup do
@client = Assistant::External::Client.new(
url: "http://localhost:18789/v1/chat",
token: "test-token",
agent_id: "test-agent"
)
end
test "streams text chunks from SSE response" do
sse_body = <<~SSE
data: {"id":"chatcmpl-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}],"model":"test-agent"}
data: {"id":"chatcmpl-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Your net worth"},"finish_reason":null}],"model":"test-agent"}
data: {"id":"chatcmpl-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" is $124,200."},"finish_reason":null}],"model":"test-agent"}
data: {"id":"chatcmpl-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"model":"test-agent"}
data: [DONE]
SSE
mock_http_streaming_response(sse_body)
chunks = []
model = @client.chat(messages: [ { role: "user", content: "test" } ]) do |text|
chunks << text
end
assert_equal [ "Your net worth", " is $124,200." ], chunks
assert_equal "test-agent", model
end
test "raises on non-200 response" do
mock_http_error_response(503, "Service Unavailable")
assert_raises(Assistant::Error) do
@client.chat(messages: [ { role: "user", content: "test" } ]) { |_| }
end
end
test "retries transient errors then raises Assistant::Error" do
Net::HTTP.any_instance.stubs(:request).raises(Net::OpenTimeout, "connection timed out")
error = assert_raises(Assistant::Error) do
@client.chat(messages: [ { role: "user", content: "test" } ]) { |_| }
end
assert_match(/temporarily unavailable/, error.message)
end
test "does not retry after streaming has started" do
call_count = 0
# Custom response that yields one chunk then raises mid-stream
mock_response = Object.new
mock_response.define_singleton_method(:is_a?) { |klass| klass == Net::HTTPSuccess }
mock_response.define_singleton_method(:read_body) do |&blk|
blk.call("data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}],\"model\":\"m\"}\n\n")
raise Errno::ECONNRESET, "connection reset mid-stream"
end
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.define_singleton_method(:request) do |_req, &blk|
call_count += 1
blk.call(mock_response)
end
Net::HTTP.stubs(:new).returns(mock_http)
chunks = []
error = assert_raises(Assistant::Error) do
@client.chat(messages: [ { role: "user", content: "test" } ]) { |t| chunks << t }
end
assert_equal 1, call_count, "Should not retry after streaming started"
assert_equal [ "partial" ], chunks
assert_match(/connection was interrupted/, error.message)
end
test "builds correct request payload" do
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
capture = mock_http_streaming_response(sse_body)
@client.chat(
messages: [
{ role: "user", content: "Hello" },
{ role: "assistant", content: "Hi there" },
{ role: "user", content: "What is my balance?" }
],
user: "sure-family-42"
) { |_| }
body = JSON.parse(capture[0].body)
assert_equal "test-agent", body["model"]
assert_equal true, body["stream"]
assert_equal 3, body["messages"].size
assert_equal "sure-family-42", body["user"]
end
test "sets authorization header and agent_id header" do
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
capture = mock_http_streaming_response(sse_body)
@client.chat(messages: [ { role: "user", content: "test" } ]) { |_| }
assert_equal "Bearer test-token", capture[0]["Authorization"]
assert_equal "test-agent", capture[0]["X-Agent-Id"]
assert_equal "agent:main:main", capture[0]["X-Session-Key"]
assert_equal "text/event-stream", capture[0]["Accept"]
assert_equal "application/json", capture[0]["Content-Type"]
end
test "omits user field when not provided" do
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
capture = mock_http_streaming_response(sse_body)
@client.chat(messages: [ { role: "user", content: "test" } ]) { |_| }
body = JSON.parse(capture[0].body)
assert_not body.key?("user")
end
test "handles malformed JSON in SSE data gracefully" do
sse_body = "data: {not valid json}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"OK\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
mock_http_streaming_response(sse_body)
chunks = []
@client.chat(messages: [ { role: "user", content: "test" } ]) { |t| chunks << t }
assert_equal [ "OK" ], chunks
end
test "handles SSE data: field without space after colon (spec-compliant)" do
sse_body = "data:{\"choices\":[{\"delta\":{\"content\":\"no space\"}}],\"model\":\"m\"}\n\ndata:[DONE]\n\n"
mock_http_streaming_response(sse_body)
chunks = []
@client.chat(messages: [ { role: "user", content: "test" } ]) { |t| chunks << t }
assert_equal [ "no space" ], chunks
end
test "handles chunked SSE data split across read_body calls" do
chunk1 = "data: {\"choices\":[{\"delta\":{\"content\":\"Hel"
chunk2 = "lo\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
mock_http_streaming_response_chunked([ chunk1, chunk2 ])
chunks = []
@client.chat(messages: [ { role: "user", content: "test" } ]) { |t| chunks << t }
assert_equal [ "Hello" ], chunks
end
test "routes through HTTPS_PROXY when set" do
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
mock_response = stub("response")
mock_response.stubs(:code).returns("200")
mock_response.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
mock_response.stubs(:read_body).yields(sse_body)
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.stubs(:request).yields(mock_response)
captured_args = nil
Net::HTTP.stubs(:new).with do |*args|
captured_args = args
true
end.returns(mock_http)
client = Assistant::External::Client.new(
url: "https://example.com/v1/chat",
token: "test-token"
)
ClimateControl.modify(HTTPS_PROXY: "http://proxyuser:proxypass@proxy:8888") do
client.chat(messages: [ { role: "user", content: "test" } ]) { |_| }
end
assert_equal "example.com", captured_args[0]
assert_equal 443, captured_args[1]
assert_equal "proxy", captured_args[2]
assert_equal 8888, captured_args[3]
assert_equal "proxyuser", captured_args[4]
assert_equal "proxypass", captured_args[5]
end
test "skips proxy for hosts in NO_PROXY" do
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
mock_response = stub("response")
mock_response.stubs(:code).returns("200")
mock_response.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
mock_response.stubs(:read_body).yields(sse_body)
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.stubs(:request).yields(mock_response)
captured_args = nil
Net::HTTP.stubs(:new).with do |*args|
captured_args = args
true
end.returns(mock_http)
client = Assistant::External::Client.new(
url: "http://agent.internal.example.com:18789/v1/chat",
token: "test-token"
)
ClimateControl.modify(HTTP_PROXY: "http://proxy:8888", NO_PROXY: "localhost,.example.com") do
client.chat(messages: [ { role: "user", content: "test" } ]) { |_| }
end
# Should NOT pass proxy args — only host and port
assert_equal 2, captured_args.length
end
private
def mock_http_streaming_response(sse_body)
capture = []
mock_response = stub("response")
mock_response.stubs(:code).returns("200")
mock_response.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
mock_response.stubs(:read_body).yields(sse_body)
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.stubs(:request).with do |req|
capture[0] = req
true
end.yields(mock_response)
Net::HTTP.stubs(:new).returns(mock_http)
capture
end
def mock_http_streaming_response_chunked(chunks)
mock_response = stub("response")
mock_response.stubs(:code).returns("200")
mock_response.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
mock_response.stubs(:read_body).multiple_yields(*chunks.map { |c| [ c ] })
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.stubs(:request).yields(mock_response)
Net::HTTP.stubs(:new).returns(mock_http)
end
def mock_http_error_response(code, message)
mock_response = stub("response")
mock_response.stubs(:code).returns(code.to_s)
mock_response.stubs(:is_a?).with(Net::HTTPSuccess).returns(false)
mock_response.stubs(:body).returns(message)
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.stubs(:request).yields(mock_response)
Net::HTTP.stubs(:new).returns(mock_http)
end
end

View File

@@ -0,0 +1,93 @@
require "test_helper"
class Assistant::ExternalConfigTest < ActiveSupport::TestCase
test "config reads URL from environment with priority over Setting" do
with_env_overrides("EXTERNAL_ASSISTANT_URL" => "http://from-env/v1/chat") do
assert_equal "http://from-env/v1/chat", Assistant::External.config.url
assert_equal "main", Assistant::External.config.agent_id
assert_equal "agent:main:main", Assistant::External.config.session_key
end
end
test "config falls back to Setting when env var is absent" do
Setting.external_assistant_url = "http://from-setting/v1/chat"
Setting.external_assistant_token = "setting-token"
with_env_overrides("EXTERNAL_ASSISTANT_URL" => nil, "EXTERNAL_ASSISTANT_TOKEN" => nil) do
assert_equal "http://from-setting/v1/chat", Assistant::External.config.url
assert_equal "setting-token", Assistant::External.config.token
end
ensure
Setting.external_assistant_url = nil
Setting.external_assistant_token = nil
end
test "config reads agent_id with custom value" do
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://example.com/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token",
"EXTERNAL_ASSISTANT_AGENT_ID" => "finance-bot"
) do
assert_equal "finance-bot", Assistant::External.config.agent_id
assert_equal "test-token", Assistant::External.config.token
end
end
test "config reads session_key with custom value" do
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://example.com/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token",
"EXTERNAL_ASSISTANT_SESSION_KEY" => "agent:finance-bot:finance"
) do
assert_equal "agent:finance-bot:finance", Assistant::External.config.session_key
end
end
test "available_for? allows any user when no allowlist is set" do
user = OpenStruct.new(email: "anyone@example.com")
with_env_overrides("EXTERNAL_ASSISTANT_URL" => "http://x", "EXTERNAL_ASSISTANT_TOKEN" => "t", "EXTERNAL_ASSISTANT_ALLOWED_EMAILS" => nil) do
assert Assistant::External.available_for?(user)
end
end
test "available_for? restricts to allowlisted emails" do
allowed = OpenStruct.new(email: "josh@example.com")
denied = OpenStruct.new(email: "other@example.com")
with_env_overrides("EXTERNAL_ASSISTANT_URL" => "http://x", "EXTERNAL_ASSISTANT_TOKEN" => "t", "EXTERNAL_ASSISTANT_ALLOWED_EMAILS" => "josh@example.com, admin@example.com") do
assert Assistant::External.available_for?(allowed)
assert_not Assistant::External.available_for?(denied)
end
end
test "build_conversation_messages truncates to last 20 messages" do
chat = chats(:one)
# Create enough messages to exceed the 20-message cap
25.times do |i|
role_class = i.even? ? UserMessage : AssistantMessage
role_class.create!(chat: chat, content: "msg #{i}", ai_model: "test")
end
with_env_overrides("EXTERNAL_ASSISTANT_URL" => "http://x", "EXTERNAL_ASSISTANT_TOKEN" => "t") do
external = Assistant::External.new(chat)
messages = external.send(:build_conversation_messages)
assert_equal 20, messages.length
# Last message should be the most recent one we created
assert_equal "msg 24", messages.last[:content]
end
end
test "configured? returns true only when URL and token are both present" do
Setting.external_assistant_url = nil
Setting.external_assistant_token = nil
with_env_overrides("EXTERNAL_ASSISTANT_URL" => "http://x", "EXTERNAL_ASSISTANT_TOKEN" => nil) do
assert_not Assistant::External.configured?
end
with_env_overrides("EXTERNAL_ASSISTANT_URL" => "http://x", "EXTERNAL_ASSISTANT_TOKEN" => "t") do
assert Assistant::External.configured?
end
end
end

View File

@@ -187,14 +187,218 @@ class AssistantTest < ActiveSupport::TestCase
test "for_chat returns External when family assistant_type is external" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
assert_instance_of Assistant::External, assistant
assert_no_difference "AssistantMessage.count" do
assistant.respond_to(@message)
assert_instance_of Assistant::External, Assistant.for_chat(@chat)
end
test "ASSISTANT_TYPE env override forces external regardless of DB value" do
assert_equal "builtin", @chat.user.family.assistant_type
with_env_overrides("ASSISTANT_TYPE" => "external") do
assert_instance_of Assistant::External, Assistant.for_chat(@chat)
end
assert_instance_of Assistant::Builtin, Assistant.for_chat(@chat)
end
test "external assistant responds with streamed text" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
sse_body = <<~SSE
data: {"choices":[{"delta":{"content":"Your net worth"}}],"model":"ext-agent:main"}
data: {"choices":[{"delta":{"content":" is $124,200."}}],"model":"ext-agent:main"}
data: [DONE]
SSE
mock_external_sse_response(sse_body)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assert_difference "AssistantMessage.count", 1 do
assistant.respond_to(@message)
end
response_msg = @chat.messages.where(type: "AssistantMessage").last
assert_equal "Your net worth is $124,200.", response_msg.content
assert_equal "ext-agent:main", response_msg.ai_model
end
end
test "external assistant adds error when not configured" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => nil,
"EXTERNAL_ASSISTANT_TOKEN" => nil
) do
assert_no_difference "AssistantMessage.count" do
assistant.respond_to(@message)
end
@chat.reload
assert @chat.error.present?
assert_includes @chat.error, "not configured"
end
end
test "external assistant adds error on connection failure" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
Net::HTTP.any_instance.stubs(:request).raises(Errno::ECONNREFUSED, "Connection refused")
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assert_no_difference "AssistantMessage.count" do
assistant.respond_to(@message)
end
@chat.reload
assert @chat.error.present?
end
end
test "external assistant handles empty response gracefully" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
sse_body = <<~SSE
data: {"choices":[{"delta":{"role":"assistant"}}],"model":"ext-agent:main"}
data: {"choices":[{"delta":{}}],"model":"ext-agent:main"}
data: [DONE]
SSE
mock_external_sse_response(sse_body)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assert_no_difference "AssistantMessage.count" do
assistant.respond_to(@message)
end
@chat.reload
assert @chat.error.present?
assert_includes @chat.error, "empty response"
end
end
test "external assistant sends conversation history" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
AssistantMessage.create!(chat: @chat, content: "I can help with that.", ai_model: "external")
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"Sure!\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
capture = mock_external_sse_response(sse_body)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assistant.respond_to(@message)
body = JSON.parse(capture[0].body)
messages = body["messages"]
assert messages.size >= 2
assert_equal "user", messages.first["role"]
end
end
test "full external assistant flow: config check, stream, save, error recovery" do
@chat.user.family.update!(assistant_type: "external")
# Phase 1: Without config, errors gracefully
with_env_overrides("EXTERNAL_ASSISTANT_URL" => nil, "EXTERNAL_ASSISTANT_TOKEN" => nil) do
assistant = Assistant::External.new(@chat)
assistant.respond_to(@message)
@chat.reload
assert @chat.error.present?
end
# Phase 2: With config, streams response
@chat.update!(error: nil)
sse_body = <<~SSE
data: {"choices":[{"delta":{"content":"Based on your accounts, "}}],"model":"ext-agent:main"}
data: {"choices":[{"delta":{"content":"your net worth is $50,000."}}],"model":"ext-agent:main"}
data: [DONE]
SSE
mock_external_sse_response(sse_body)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assistant = Assistant::External.new(@chat)
assistant.respond_to(@message)
@chat.reload
assert_nil @chat.error
response = @chat.messages.where(type: "AssistantMessage").last
assert_equal "Based on your accounts, your net worth is $50,000.", response.content
assert_equal "ext-agent:main", response.ai_model
end
end
test "ASSISTANT_TYPE env override with unknown value falls back to builtin" do
with_env_overrides("ASSISTANT_TYPE" => "nonexistent") do
assert_instance_of Assistant::Builtin, Assistant.for_chat(@chat)
end
end
test "external assistant sets user identifier with family_id" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"OK\"}}],\"model\":\"m\"}\n\ndata: [DONE]\n\n"
capture = mock_external_sse_response(sse_body)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assistant.respond_to(@message)
body = JSON.parse(capture[0].body)
assert_equal "sure-family-#{@chat.user.family_id}", body["user"]
end
end
test "external assistant updates ai_model from SSE response model field" do
@chat.user.family.update!(assistant_type: "external")
assistant = Assistant.for_chat(@chat)
sse_body = "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}],\"model\":\"ext-agent:custom\"}\n\ndata: [DONE]\n\n"
mock_external_sse_response(sse_body)
with_env_overrides(
"EXTERNAL_ASSISTANT_URL" => "http://localhost:18789/v1/chat",
"EXTERNAL_ASSISTANT_TOKEN" => "test-token"
) do
assistant.respond_to(@message)
response = @chat.messages.where(type: "AssistantMessage").last
assert_equal "ext-agent:custom", response.ai_model
end
@chat.reload
assert @chat.error.present?
assert_includes @chat.error, "not yet implemented"
end
test "for_chat raises when chat is blank" do
@@ -202,6 +406,27 @@ class AssistantTest < ActiveSupport::TestCase
end
private
def mock_external_sse_response(sse_body)
capture = []
mock_response = stub("response")
mock_response.stubs(:code).returns("200")
mock_response.stubs(:is_a?).with(Net::HTTPSuccess).returns(true)
mock_response.stubs(:read_body).yields(sse_body)
mock_http = stub("http")
mock_http.stubs(:use_ssl=)
mock_http.stubs(:open_timeout=)
mock_http.stubs(:read_timeout=)
mock_http.stubs(:request).with do |req|
capture[0] = req
true
end.yields(mock_response)
Net::HTTP.stubs(:new).returns(mock_http)
capture
end
def provider_function_request(id:, call_id:, function_name:, function_args:)
Provider::LlmConcept::ChatFunctionRequest.new(
id: id,

View File

@@ -149,7 +149,7 @@ class UserTest < ActiveSupport::TestCase
test "ai_available? returns true when openai access token set in settings" do
Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)
previous = Setting.openai_access_token
with_env_overrides OPENAI_ACCESS_TOKEN: nil do
with_env_overrides OPENAI_ACCESS_TOKEN: nil, EXTERNAL_ASSISTANT_URL: nil, EXTERNAL_ASSISTANT_TOKEN: nil do
Setting.openai_access_token = nil
assert_not @user.ai_available?
@@ -160,6 +160,43 @@ class UserTest < ActiveSupport::TestCase
Setting.openai_access_token = previous
end
test "ai_available? returns true when external assistant is configured and family type is external" do
Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)
previous = Setting.openai_access_token
@user.family.update!(assistant_type: "external")
with_env_overrides OPENAI_ACCESS_TOKEN: nil, EXTERNAL_ASSISTANT_URL: "http://localhost:18789/v1/chat", EXTERNAL_ASSISTANT_TOKEN: "test-token" do
Setting.openai_access_token = nil
assert @user.ai_available?
end
ensure
Setting.openai_access_token = previous
@user.family.update!(assistant_type: "builtin")
end
test "ai_available? returns false when external assistant is configured but family type is builtin" do
Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)
previous = Setting.openai_access_token
with_env_overrides OPENAI_ACCESS_TOKEN: nil, EXTERNAL_ASSISTANT_URL: "http://localhost:18789/v1/chat", EXTERNAL_ASSISTANT_TOKEN: "test-token" do
Setting.openai_access_token = nil
assert_not @user.ai_available?
end
ensure
Setting.openai_access_token = previous
end
test "ai_available? returns false when external assistant is configured but user is not in allowlist" do
Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)
previous = Setting.openai_access_token
@user.family.update!(assistant_type: "external")
with_env_overrides OPENAI_ACCESS_TOKEN: nil, EXTERNAL_ASSISTANT_URL: "http://localhost:18789/v1/chat", EXTERNAL_ASSISTANT_TOKEN: "test-token", EXTERNAL_ASSISTANT_ALLOWED_EMAILS: "other@example.com" do
Setting.openai_access_token = nil
assert_not @user.ai_available?
end
ensure
Setting.openai_access_token = previous
@user.family.update!(assistant_type: "builtin")
end
test "intro layout collapses sidebars and enables ai" do
user = User.new(
family: families(:empty),