Skip to content

Commit ef7d870

Browse files
committed
Refactor OpenRouter to use OpenAI's Native Gem Types
1 parent 94dbd07 commit ef7d870

17 files changed

Lines changed: 710 additions & 206 deletions

File tree

lib/active_agent/providers/open_router/_types.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,25 @@
88
module ActiveAgent
99
module Providers
1010
module OpenRouter
11-
# Type for Request model
11+
# ActiveModel type for casting and serializing OpenRouter requests
12+
#
13+
# Handles conversion between hashes, Request objects, and serialized
14+
# request hashes for the OpenRouter API.
15+
#
16+
# @example Type casting
17+
# type = RequestType.new
18+
# request = type.cast({ model: "openai/gpt-4", messages: "Hello" })
19+
# # => #<Request ...>
20+
#
21+
# @example Serialization
22+
# serialized = type.serialize(request)
23+
# # => { model: "openai/gpt-4", messages: [...] }
1224
class RequestType < ActiveModel::Type::Value
25+
# Casts value to Request object
26+
#
27+
# @param value [Request, Hash, nil]
28+
# @return [Request, nil]
29+
# @raise [ArgumentError] if value cannot be cast
1330
def cast(value)
1431
case value
1532
when Request
@@ -23,6 +40,11 @@ def cast(value)
2340
end
2441
end
2542

43+
# Serializes Request to hash for API submission
44+
#
45+
# @param value [Request, Hash, nil]
46+
# @return [Hash, nil]
47+
# @raise [ArgumentError] if value cannot be serialized
2648
def serialize(value)
2749
case value
2850
when Request
@@ -36,6 +58,10 @@ def serialize(value)
3658
end
3759
end
3860

61+
# Deserializes value from storage
62+
#
63+
# @param value [Object]
64+
# @return [Request, nil]
3965
def deserialize(value)
4066
cast(value)
4167
end

lib/active_agent/providers/open_router/options.rb

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,48 @@
88
module ActiveAgent
99
module Providers
1010
module OpenRouter
11+
# Configuration options for OpenRouter provider
12+
#
13+
# Extends OpenAI::Options with OpenRouter-specific settings including
14+
# HTTP-Referer and X-Title headers for app identification and ranking.
15+
#
16+
# @example Basic configuration
17+
# options = Options.new(
18+
# api_key: 'sk-or-v1-...',
19+
# app_name: 'MyApp',
20+
# site_url: 'https://myapp.com'
21+
# )
22+
#
23+
# @example Rails auto-configuration
24+
# # Automatically resolves app_name from Rails.application
25+
# # and site_url from routes.default_url_options
26+
# options = Options.new(api_key: ENV['OPENROUTER_API_KEY'])
27+
#
28+
# @see https://openrouter.ai/docs/api-keys OpenRouter API Keys
29+
# @see https://openrouter.ai/docs/rankings OpenRouter App Rankings
1130
class Options < ActiveAgent::Providers::OpenAI::Options
31+
# @!attribute base_url
32+
# @return [String] API endpoint (default: "https://openrouter.ai/api/v1")
1233
attribute :base_url, :string, as: "https://openrouter.ai/api/v1"
34+
35+
# @!attribute app_name
36+
# @return [String] application name for X-Title header (default: "ActiveAgent" or Rails app name)
1337
attribute :app_name, :string, fallback: "ActiveAgent"
38+
39+
# @!attribute site_url
40+
# @return [String] site URL for HTTP-Referer header (default: "https://activeagents.ai/" or Rails URL)
1441
attribute :site_url, :string, fallback: "https://activeagents.ai/"
1542

43+
# Creates new OpenRouter options with auto-resolution
44+
#
45+
# Automatically resolves app_name from Rails application name and
46+
# site_url from Rails routes/ActionMailer default_url_options.
47+
#
48+
# @param kwargs [Hash] configuration options
49+
# @option kwargs [String] :api_key OpenRouter API key
50+
# @option kwargs [String] :app_name application name for rankings
51+
# @option kwargs [String] :site_url site URL for rankings
52+
# @return [Options]
1653
def initialize(kwargs = {})
1754
kwargs = kwargs.deep_symbolize_keys if kwargs.respond_to?(:deep_symbolize_keys)
1855

@@ -22,11 +59,22 @@ def initialize(kwargs = {})
2259
)))
2360
end
2461

62+
# Serializes options for API requests
63+
#
64+
# Excludes app_name and site_url as they're sent via headers.
65+
#
66+
# @return [Hash] serialized options
2567
def serialize
2668
super.except(:app_name, :site_url)
2769
end
2870

29-
# We fallback to ActiveAgent but allow empty strings to unset
71+
# Returns extra headers for OpenRouter API
72+
#
73+
# Maps app_name and site_url to OpenRouter's required headers:
74+
# - HTTP-Referer: site_url
75+
# - X-Title: app_name
76+
#
77+
# @return [Hash] headers hash
3078
def extra_headers
3179
deep_compact(
3280
"http-referer" => site_url.presence,

0 commit comments

Comments
 (0)