vibe-sort 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/docs/architecture.md CHANGED
@@ -20,9 +20,10 @@ VibeSort follows a layered architecture with clear separation of concerns:
20
20
  ▼
21
21
  ┌─────────────────────────────────────────┐
22
22
  │ VibeSort::Configuration │
23
- │ - Provider selection │
23
+ │ - Provider selection (explicit or │
24
+ │ inferred via KeyDetector) │
24
25
  │ - API key management │
25
- │ - Model override │
26
+ │ - Model override, extra_params │
26
27
  │ - Temperature settings │
27
28
  └─────────────────┬───────────────────────┘
28
29
  │
@@ -37,10 +38,11 @@ VibeSort follows a layered architecture with clear separation of concerns:
37
38
  │ VibeSort::Providers::Base │
38
39
  │ - Shared prompt │
39
40
  │ - HTTP client (Faraday) │
41
+ │ - extra_params deep merge │
40
42
  │ - Response parsing & validation │
41
43
  ├─────────────────────────────────────────┤
42
44
  │ OpenAI │ Anthropic │ Gemini │ │
43
- │ Groq │ SpaceXAI │
45
+ │ Groq │ SpaceXAI │ OpenRouter │
44
46
  │ (request/response wire formats) │
45
47
  └─────────────────┬───────────────────────┘
46
48
  │
@@ -50,6 +52,7 @@ VibeSort follows a layered architecture with clear separation of concerns:
50
52
  │ api.openai.com │ api.anthropic.com │
51
53
  │ generativelanguage.googleapis.com │
52
54
  │ api.groq.com │ api.x.ai │
55
+ │ openrouter.ai │
53
56
  └─────────────────────────────────────────┘
54
57
  ```
55
58
 
@@ -67,7 +70,7 @@ VibeSort follows a layered architecture with clear separation of concerns:
67
70
  - Return standardized response hashes
68
71
 
69
72
  **Key Methods**:
70
- - `initialize(api_key:, temperature: 0.0, provider: :openai, model: nil)`: Creates client with configuration
73
+ - `initialize(api_key:, temperature: 0.0, provider: nil, model: nil, extra_params: {})`: Creates client with configuration
71
74
  - `sort(array)`: Sorts array and returns result hash
72
75
 
73
76
  **Return Format**:
@@ -84,19 +87,26 @@ VibeSort follows a layered architecture with clear separation of concerns:
84
87
  **Purpose**: Encapsulates configuration settings.
85
88
 
86
89
  **Responsibilities**:
87
- - Store provider selection and API key
88
- - Store model override and temperature setting
90
+ - Resolve the provider: explicit argument wins (with a non-fatal stderr warning on a mismatched key prefix); otherwise inferred from the key prefix via `KeyDetector`, defaulting to `:openai`
91
+ - Store API key, model override, temperature, and extra_params
89
92
  - Validate API key presence and provider name
90
93
 
91
94
  **Key Methods**:
92
- - `initialize(api_key:, temperature: 0.0, provider: :openai, model: nil)`: Creates configuration
93
- - Raises `ArgumentError` if API key is nil/empty or provider is unknown
95
+ - `initialize(api_key:, temperature: 0.0, provider: nil, model: nil, extra_params: {})`: Creates configuration
96
+ - Raises `ArgumentError` if API key is nil/empty or an explicit provider is unknown
94
97
 
95
98
  **Attributes**:
96
99
  - `api_key`: Provider API key (String)
97
- - `provider`: `:openai`, `:anthropic`, `:gemini`, `:groq`, or `:spacexai` (Symbol)
100
+ - `provider`: `:openai`, `:anthropic`, `:gemini`, `:groq`, `:spacexai`, `:openrouter`, or `:jev` / `:typesafe` (Symbol). Routing between these is decided by `VibeSort::ProviderResolver`
98
101
  - `model`: Model ID override, or nil for the provider default (String or nil)
99
102
  - `temperature`: Model temperature (Float, 0.0-2.0; not sent to Anthropic)
103
+ - `extra_params`: Provider-native request parameters deep-merged into the payload last (Hash)
104
+
105
+ ### VibeSort::KeyDetector
106
+
107
+ **Purpose**: Best-effort provider inference from API key prefixes (`sk-ant-` → `:anthropic`, `sk-or-` → `:openrouter`, `gsk_` → `:groq`, `AIza`/`AQ.` → `:gemini`, `xai-` → `:spacexai`, `sk-` → `:openai`).
108
+
109
+ Prefixes are conventions, not contracts — providers ship multiple key formats concurrently and new formats appear without notice. Detection is therefore only a soft default and a source of non-fatal warnings, never validation: `detect` returns nil for unrecognized formats, and callers fall back to `:openai`.
100
110
 
101
111
  ### VibeSort::Sorter
102
112
 
@@ -113,6 +123,7 @@ VibeSort follows a layered architecture with clear separation of concerns:
113
123
  **`Providers::Base` responsibilities**:
114
124
  - Hold the shared sorting prompt
115
125
  - Build the HTTP connection with Faraday
126
+ - Deep-merge `config.extra_params` into the adapter payload last (nested hashes merge recursively; arrays and scalars replace)
116
127
  - Parse and validate JSON responses (shared across providers)
117
128
  - Raise `ApiError` on failures
118
129
 
@@ -124,6 +135,8 @@ VibeSort follows a layered architecture with clear separation of concerns:
124
135
  - `Providers::Gemini`: generateContent with JSON response mode, `gemini-2.5-flash`
125
136
  - `Providers::Groq`: OpenAI-compatible (subclasses `Providers::OpenAI`), `llama-3.3-70b-versatile`
126
137
  - `Providers::SpaceXAI`: OpenAI-compatible (subclasses `Providers::OpenAI`), `grok-4`
138
+ - `Providers::OpenRouter`: OpenAI-compatible (subclasses `Providers::OpenAI`), `openai/gpt-4o-mini`, adds OpenRouter's recommended attribution headers
139
+ - `Providers::Jev`: TypeSafe System One (does not subclass `Providers::Base`). Builds a `SystemOne::Client` (`POST /v1/systemone`, TypeSafe Direct or OpenRouter) and runs one of `SystemOne::Strategies::{ChoiceSort, ScoreSort, NoulSort, BogoSort}`, which work on item indices and return the original objects as a `VibeSort::Result`. `SystemOne::Serializer` sanitises values for TypeSafe's JSON rules
127
140
 
128
141
  ### VibeSort::ApiError
129
142
 
@@ -0,0 +1,134 @@
1
+ # Free-Tier Model Benchmark — 2026-07-12
2
+
3
+ An empirical snapshot of how free LLM tiers handle vibe-sort's sorting contract, gathered
4
+ while designing v0.4.0 (OpenRouter provider, key-format detection, `extra_params` —
5
+ see [v0.4.0_PLAN.md](v0.4.0_PLAN.md)). All requests used free models / free-tier quota only.
6
+
7
+ **TL;DR:** Google's current Gemini flash models all sort correctly through the gem's exact
8
+ payload, with `gemini-3.1-flash-lite` the clear free-tier pick. OpenRouter's `:free` pool was
9
+ too congested on a Saturday afternoon (AEST) to be usable — 14 of 17 tested models failed with
10
+ rate limits or upstream errors, and one burned 10 minutes and 20k tokens to return nothing.
11
+
12
+ ---
13
+
14
+ ## Methodology
15
+
16
+ - **Task:** sort `[42, "banana", 7, "Apple", 3.14, "cherry", 1]`
17
+ → expected `[1, 3.14, 7, 42, "Apple", "banana", "cherry"]`
18
+ - **Payloads:** byte-for-byte what the gem sends — `Providers::OpenAI#build_payload` for
19
+ OpenRouter (system prompt + `temperature: 0.0` + `response_format: {type: "json_object"}`),
20
+ `Providers::Gemini#build_payload` for Gemini (`systemInstruction` +
21
+ `generationConfig.responseMimeType: "application/json"`).
22
+ - **Success criteria:** HTTP 200, content parses as JSON with a `sorted_array` key
23
+ (the gem's contract), array exactly matches the expected sort.
24
+ - **Fallbacks tried on rejection:** OpenRouter — retry without `response_format`;
25
+ Gemini — retry with system prompt folded into the user turn and no JSON mime type.
26
+ - **Pacing/retries:** 3.5–8s between requests; 5xx retried up to 3×; 429 handling varied by
27
+ pass (see Lessons). Plain Ruby `net/http`, no SDKs.
28
+ - **Cost:** $0. OpenRouter restricted to `$0`-priced models; Gemini restricted to
29
+ free-tier models with non-zero quota.
30
+
31
+ ---
32
+
33
+ ## Gemini API (free tier)
34
+
35
+ 7 models tested — every model whose free-tier quota is non-zero.
36
+
37
+ | Model | Outcome | Latency | Tokens in/out (+thinking) | Notes |
38
+ |---|---|---|---|---|
39
+ | gemini-3.1-flash-lite | ✅ correct | **2.08s** | 87/35 (+0) | Fastest; no thinking tokens; best quota (15 RPM / 500 RPD) |
40
+ | gemini-2.5-flash | ✅ correct | 2.66s | 87/49 (+247) | Gem's current `DEFAULT_MODEL` — still works |
41
+ | gemini-3.5-flash | ✅ correct | 5.98s | 87/50 (+418) | Most thinking tokens |
42
+ | gemini-3-flash-preview | ✅ correct | 24.14s | 87/49 (+321) | Correct but slow |
43
+ | gemma-4-26b-a4b-it | ❌ bad JSON | 28.6s | 87/49 (+318) | Ignored JSON mime type; replied with markdown reasoning; 1×500 on the way |
44
+ | gemma-4-31b-it | ❌ bad JSON | 9.8s | 87/30 (+256) | Same failure shape; also 1×500 |
45
+ | gemini-2.5-flash-lite | ❌ HTTP 404 | — | — | "No longer available to new users" (still listed by the models endpoint) |
46
+
47
+ ### Observations
48
+
49
+ - **All four current Gemini-branded flash models pass the gem's contract as-is** — JSON mode
50
+ honored, sort correct, numbers-before-strings and case-sensitivity respected.
51
+ - **Gemma models are incompatible with the gem today:** they accept the request but ignore
52
+ `responseMimeType: "application/json"` and return reasoning-style markdown. They'd need
53
+ `extra_params` surgery plus response post-processing — not worth it.
54
+ - **The 500s are real:** 2 of 7 models needed a 5xx retry on their very first request.
55
+ Any Gemini integration should retry 5xx at least once.
56
+ - **429 retries are a trap on the free tier:** every retry counts against RPM *and* RPD, so a
57
+ retry loop converts one throttled request into three consumed ones. After the daily caps
58
+ (20 RPD on most flash models) tripped, throttling also turned intermittent — consecutive
59
+ requests to the same model went 429, 429, 200. Policy that survives: never auto-retry a
60
+ free-tier 429; skip and move on.
61
+ - An input-length scaling test (5/20/50/100 elements) was attempted but the RPD caps were
62
+ exhausted before it could run. Future work, pending quota reset — only
63
+ `gemini-3.1-flash-lite` (500 RPD) realistically supports this kind of testing for free.
64
+
65
+ ---
66
+
67
+ ## OpenRouter (free models)
68
+
69
+ 26 models on openrouter.ai were $0-priced with text input (of 345 listed). 17 were tested
70
+ before the sweep was aborted; 9 were never reached. Tested on a Saturday afternoon AEST —
71
+ peak congestion for the shared free pool, which colours everything below.
72
+
73
+ | Model | Outcome | Notes |
74
+ |---|---|---|
75
+ | nvidia/nemotron-3-super-120b-a12b:free | ❌ empty content | **600s, 20,673 completion tokens, no answer** — presumably all reasoning |
76
+ | nvidia/nemotron-3.5-content-safety:free | ❌ bad JSON | Replied "User Safety: safe" — it's a classifier, not a chat model |
77
+ | nvidia/nemotron-nano-12b-v2-vl:free | ❌ upstream timeout | "Upstream idle timeout exceeded" after 121s |
78
+ | nvidia/nemotron-3-nano-30b-a3b:free | ❌ upstream error | NVIDIA "ResourceExhausted: worker limit reached" |
79
+ | nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free | ❌ upstream error | Same |
80
+ | nvidia/nemotron-3-ultra-550b-a55b:free | ❌ upstream error | Same |
81
+ | nvidia/nemotron-nano-9b-v2:free | ⏹ aborted | Stalled the sweep twice with a slow-drip connection (>20 min) |
82
+ | liquid/lfm-2.5-1.2b-instruct:free | ❌ HTTP 502 | Upstream down |
83
+ | liquid/lfm-2.5-1.2b-thinking:free | ❌ HTTP 502 | Upstream down |
84
+ | google/lyria-3-clip-preview | ❌ HTTP 400 | Music model; listed as $0 text-input but not a chat model |
85
+ | google/lyria-3-pro-preview | ❌ HTTP 400 | Same |
86
+ | cognitivecomputations/dolphin-mistral-24b-venice-edition:free | ❌ HTTP 429 | Free pool congested (retried in a 2nd pass ~1h later — still 429) |
87
+ | cohere/north-mini-code:free | ❌ HTTP 429 | Explicit "High demand" RPM-limit message |
88
+ | google/gemma-4-26b-a4b-it:free | ❌ HTTP 429 | Congested both passes |
89
+ | google/gemma-4-31b-it:free | ❌ HTTP 429 | Congested both passes |
90
+ | meta-llama/llama-3.2-3b-instruct:free | ❌ HTTP 429 | Congested both passes |
91
+ | meta-llama/llama-3.3-70b-instruct:free | ❌ HTTP 429 | Congested both passes |
92
+ | nousresearch/hermes-3-llama-3.1-405b:free | ❌ HTTP 429 | Congested both passes |
93
+ | openai/gpt-oss-120b:free, openai/gpt-oss-20b:free, openrouter/free, poolside/laguna-m.1:free, poolside/laguna-xs-2.1:free, qwen/qwen3-coder:free, qwen/qwen3-next-80b-a3b-instruct:free, tencent/hy3:free | ⏸ not tested | Sweep aborted before reaching them (blocked behind the stalling Nemotron) |
94
+
95
+ ### Observations
96
+
97
+ - **Zero of 17 tested free models completed a correct sort** during this window. This is a
98
+ statement about the free pool's availability on a weekend afternoon, not about the models —
99
+ nearly every failure was infrastructure (429/5xx/upstream), not model output.
100
+ - **The transport layer of a vibe-sort OpenRouter adapter is validated**, though: auth,
101
+ routing, the OpenAI-compatible payload, and OpenRouter's error shape
102
+ (`error.message` — including passed-through upstream provider errors) all behaved exactly
103
+ as `Providers::Base#extract_error_message` expects.
104
+ - **Free-model quirks to document:** `$0` pricing does not imply chat-usable (Lyria = music,
105
+ content-safety = classifier); reasoning models can consume enormous token counts and still
106
+ return empty content; per-request wall time is unbounded in practice (slow-drip responses
107
+ blow through per-read timeouts), so an overall deadline is the only safe timeout.
108
+ - Free-model requests are also account-capped on OpenRouter (daily request limits tied to
109
+ credit balance) — fine for smoke tests, unusable for CI.
110
+
111
+ ---
112
+
113
+ ## Implications for vibe-sort v0.4.0
114
+
115
+ 1. **OpenRouter adapter: ship it.** The mechanics are proven even though the free pool was
116
+ congested; with any paid model (or an off-peak free window) it should behave like the
117
+ other OpenAI-compatible adapters. Don't default to a `:free` model — congestion would make
118
+ the gem look broken.
119
+ 2. **Gemini `DEFAULT_MODEL`:** `gemini-2.5-flash` still works, but Google has begun retiring
120
+ 2.5-era models for new users (2.5-flash-lite already 404s). `gemini-3.1-flash-lite` is
121
+ faster here, spends no thinking tokens on this task, and has the widest free quota —
122
+ a strong candidate for the next default.
123
+ 3. **`extra_params` earns its place:** overriding `response_format`, capping `max_tokens` on
124
+ reasoning models, and OpenRouter provider-routing preferences were all wanted during this
125
+ exercise.
126
+ 4. **Never auto-retry 429s against free tiers** in examples/docs; retries multiply quota
127
+ consumption and prolong the lockout.
128
+
129
+ ## Reproduction
130
+
131
+ Standalone Ruby scripts (plain `net/http`, no gem dependencies) sent the exact adapter
132
+ payloads; per-model raw results (latency, token usage, full error messages, returned arrays)
133
+ were captured as JSON. API keys were session-scoped test keys, not committed. Numbers above
134
+ are single-shot measurements on free tiers — directional, not statistical.
@@ -0,0 +1,203 @@
1
+ # v0.4.0 Plan: OpenRouter, Key Detection, and Extra Params
2
+
3
+ ## Executive Summary
4
+
5
+ This document outlines the plan for VibeSort v0.4.0, which adds three related features:
6
+
7
+ 1. **OpenRouter provider** — a sixth provider adapter, OpenAI-compatible
8
+ 2. **API key format detection** — infer the provider from the key prefix when `provider:` is omitted; warn (never fail) on mismatch when it is explicit
9
+ 3. **`extra_params` escape hatch** — a single hash of provider-native request parameters deep-merged into the payload, instead of enumerating every model knob as first-class config
10
+
11
+ **Status:** Implemented — shipped in v0.4.0
12
+
13
+ **Target Version:** 0.4.0 (minor bump — provider auto-detection is a behavior change, see Compatibility Notes)
14
+
15
+ ---
16
+
17
+ ## Motivation
18
+
19
+ - OpenRouter gives access to hundreds of models through one OpenAI-compatible endpoint — maximum flavour for a gem whose whole point is sorting arrays with unreasonable amounts of AI.
20
+ - Provider key prefixes are distinctive enough that `VibeSort::Client.new(api_key: key)` can usually "just work" without a `provider:` argument. That fits the gem's vibe.
21
+ - Users routing through OpenRouter (or tuning any provider) need occasional knobs (`max_tokens`, `top_p`, OpenRouter `provider` routing preferences). One passthrough hash covers all of them without a config matrix to maintain.
22
+
23
+ ## Non-Goals
24
+
25
+ - First-class config attributes for every model parameter (`top_p`, `max_tokens`, penalties, …) — `extra_params` covers these
26
+ - Validating API keys against provider formats (prefixes are conventions, not contracts — OpenAI already changed theirs once with `sk-proj-`)
27
+ - Cross-provider normalization of `extra_params` keys (they are provider-native by design)
28
+ - Streaming, retries, or any other transport changes
29
+
30
+ ---
31
+
32
+ ## Feature 1: OpenRouter Provider
33
+
34
+ ### Design
35
+
36
+ `Providers::OpenRouter < OpenAI`, mirroring `groq.rb` and `space_x_ai.rb`:
37
+
38
+ ```ruby
39
+ module VibeSort
40
+ module Providers
41
+ # Adapter for the OpenRouter API (OpenAI-compatible Chat Completions)
42
+ class OpenRouter < OpenAI
43
+ ENDPOINT = "https://openrouter.ai/api/v1/chat/completions"
44
+ DEFAULT_MODEL = "openai/gpt-4o-mini"
45
+
46
+ private
47
+
48
+ def provider_name
49
+ "OpenRouter"
50
+ end
51
+
52
+ def endpoint
53
+ ENDPOINT
54
+ end
55
+
56
+ def headers
57
+ super.merge(
58
+ "HTTP-Referer" => "https://github.com/chayuto/vibe-sort",
59
+ "X-Title" => "vibe-sort"
60
+ )
61
+ end
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ ### Decisions
68
+
69
+ - **Default model:** `openai/gpt-4o-mini` for parity with the OpenAI adapter's default. OpenRouter is the provider where users are most likely to pass `model:` anyway (already supported).
70
+ - **Attribution headers:** `HTTP-Referer` / `X-Title` are optional but recommended by OpenRouter; they cost nothing and get the gem listed on openrouter.ai rankings.
71
+ - **JSON mode caveat:** OpenRouter forwards `response_format: { type: "json_object" }` but only some underlying models honor it. The existing `JSON::ParserError → ApiError` path already handles models that ignore it; document the caveat in the README and note that `extra_params` can override `response_format` if needed.
72
+
73
+ ### Checklist
74
+
75
+ - [x] `lib/vibe_sort/providers/open_router.rb`
76
+ - [x] Add `:openrouter` to `Configuration::PROVIDERS`
77
+ - [x] Add `openrouter: Providers::OpenRouter` to `Sorter::PROVIDER_CLASSES`
78
+ - [x] Require the new file from `lib/vibe_sort.rb`
79
+ - [x] `spec/vibe_sort/providers/open_router_spec.rb` (mirror `groq_spec.rb`: endpoint, default model, headers, payload, error prefix)
80
+ - [x] README: provider table row + usage example + JSON-mode caveat
81
+
82
+ ---
83
+
84
+ ## Feature 2: Key Format Detection
85
+
86
+ ### Design
87
+
88
+ A small module with an ordered prefix map (order matters — specific prefixes before `sk-`):
89
+
90
+ ```ruby
91
+ module VibeSort
92
+ # Best-effort provider inference from API key prefixes.
93
+ # Prefixes are conventions, not contracts — used only as a default
94
+ # and for non-fatal mismatch warnings, never for validation.
95
+ module KeyDetector
96
+ PREFIXES = {
97
+ "sk-ant-" => :anthropic,
98
+ "sk-or-" => :openrouter,
99
+ "sk-" => :openai,
100
+ "gsk_" => :groq,
101
+ "AIza" => :gemini, # classic Google API key
102
+ "AQ." => :gemini, # newer Gemini API key format (observed in the wild, 2026)
103
+ "xai-" => :spacexai
104
+ }.freeze
105
+
106
+ # @return [Symbol, nil] detected provider, or nil if unrecognized
107
+ def self.detect(api_key)
108
+ PREFIXES.each { |prefix, provider| return provider if api_key.start_with?(prefix) }
109
+ nil
110
+ end
111
+ end
112
+ end
113
+ ```
114
+
115
+ ### Wiring in `Configuration`
116
+
117
+ `provider:` defaults change from `:openai` to `nil` in both `Client` and `Configuration`:
118
+
119
+ - `provider: nil` (omitted) → `KeyDetector.detect(api_key) || :openai`
120
+ - `provider:` explicit and detection disagrees → `Kernel#warn` one line (e.g. `vibe-sort: api_key looks like an anthropic key but provider is :openai`) and proceed with the explicit provider
121
+ - `provider:` explicit and detection returns `nil` → silent (unknown formats are normal: proxies, Azure, self-hosted gateways)
122
+
123
+ Providers ship multiple key formats concurrently (Google: classic `AIza…` and newer `AQ.…`; OpenAI: `sk-…` and `sk-proj-…`), and new formats appear without notice — another reason detection must stay a soft default, never validation. An unrecognized prefix simply means "no inference", not "bad key".
124
+
125
+ ### Behavior Matrix
126
+
127
+ | `provider:` arg | Key prefix | Result |
128
+ |-----------------|-------------------|---------------------------------|
129
+ | omitted | `sk-ant-…` | `:anthropic` (detected) |
130
+ | omitted | unrecognized | `:openai` (unchanged fallback) |
131
+ | `:openai` | `sk-…` | `:openai`, no warning |
132
+ | `:openai` | `sk-ant-…` | `:openai`, one-line warning |
133
+ | `:groq` | unrecognized | `:groq`, no warning |
134
+
135
+ ### Checklist
136
+
137
+ - [x] `lib/vibe_sort/key_detector.rb`
138
+ - [x] `Configuration`: `provider: nil` default, detection fallback, mismatch warning
139
+ - [x] `Client`: `provider: nil` default passthrough, doc examples updated
140
+ - [x] `spec/vibe_sort/key_detector_spec.rb` (each prefix, ordering — `sk-ant-` and `sk-or-` beat `sk-`, unknown → nil)
141
+ - [x] `configuration_spec.rb`: detection fallback, explicit-provider precedence, warning emission (capture stderr), unknown-key → `:openai`
142
+ - [x] README: "provider is optional — detected from your key" section
143
+
144
+ ---
145
+
146
+ ## Feature 3: `extra_params` Escape Hatch
147
+
148
+ ### Design
149
+
150
+ One new optional config attribute, deep-merged into the request payload **last** so it can override anything:
151
+
152
+ ```ruby
153
+ # Configuration
154
+ def initialize(api_key:, temperature: 0.0, provider: nil, model: nil, extra_params: {})
155
+ ```
156
+
157
+ In `Providers::Base`:
158
+
159
+ ```ruby
160
+ def perform(array)
161
+ response = connection.post do |req|
162
+ req.body = deep_merge(build_payload(array), config.extra_params)
163
+ end
164
+ handle_response(response)
165
+ end
166
+ ```
167
+
168
+ with a private `deep_merge` helper (recursive for nested hashes, e.g. Gemini's `generationConfig`; arrays and scalars replace).
169
+
170
+ ### Decisions
171
+
172
+ - **Provider-native keys, no translation.** `{ max_tokens: 100 }` means what OpenAI/Groq/OpenRouter/xAI say it means; Gemini users write `{ generationConfig: { maxOutputTokens: 100 } }`. Documented explicitly.
173
+ - **Merged last** so users can override `response_format`, `temperature`, even `model` — full escape hatch semantics.
174
+ - **Symbol keys** in examples; deep_merge treats the hash as-is (payloads are built with symbol keys throughout).
175
+
176
+ ### Checklist
177
+
178
+ - [x] `Configuration`: `extra_params` attr + default `{}`
179
+ - [x] `Client`: passthrough kwarg + doc example
180
+ - [x] `Base#perform`: merge step + `deep_merge` helper
181
+ - [x] Specs: merge adds new keys, overrides existing (`response_format`), deep-merges nested hashes, empty hash is a no-op (existing payload specs unchanged)
182
+ - [x] README: examples — `max_tokens` on OpenAI, `generationConfig` on Gemini, `provider` routing prefs on OpenRouter
183
+
184
+ ---
185
+
186
+ ## Compatibility Notes
187
+
188
+ - **Behavior change:** an Anthropic/Gemini/Groq/xAI/OpenRouter-prefixed key with **no** `provider:` argument previously hit the OpenAI API and failed; it now routes to the detected provider and works. Strictly an improvement, but observable — hence 0.4.0, not 0.3.2.
189
+ - Unrecognized keys with no `provider:` still default to `:openai` (unchanged).
190
+ - All existing explicit-provider call sites behave identically (worst case: a new one-line stderr warning on a mismatched key).
191
+ - No new runtime dependencies.
192
+
193
+ ---
194
+
195
+ ## Implementation Order
196
+
197
+ 1. `extra_params` (self-contained, touches `Base` — land first so the OpenRouter spec can cover interplay)
198
+ 2. OpenRouter provider
199
+ 3. Key detection (depends on `:openrouter` existing for the `sk-or-` mapping)
200
+ 4. Docs sweep: README, CHANGELOG, `docs/api_reference.md`, `docs/architecture.md` provider list
201
+ 5. Version bump to 0.4.0
202
+
203
+ Each step is independently green (rubocop + rspec) and committable.
@@ -10,20 +10,48 @@ module VibeSort
10
10
  # @param api_key [String] API key for the selected provider
11
11
  # @param temperature [Float] Temperature for the model (default: 0.0).
12
12
  # Ignored by providers whose current models do not accept it (Anthropic).
13
- # @param provider [Symbol, String] LLM provider: :openai (default), :anthropic, or :gemini
13
+ # @param provider [Symbol, String, nil] LLM provider: :openai, :anthropic, :gemini,
14
+ # :groq, :spacexai, or :openrouter. When omitted, the provider is inferred from
15
+ # the api_key prefix (e.g. "sk-ant-..." routes to Anthropic), falling back to
16
+ # :openai for unrecognized key formats.
14
17
  # @param model [String, nil] Model ID override (nil uses the provider's default)
18
+ # @param extra_params [Hash] Provider-native request parameters deep-merged into
19
+ # the request payload last, so they can override anything the adapter builds
15
20
  # @raise [ArgumentError] if api_key or provider is invalid
16
21
  #
17
- # @example OpenAI (default provider)
18
- # client = VibeSort::Client.new(api_key: ENV['OPENAI_API_KEY'])
22
+ # @example Provider inferred from the key prefix
23
+ # client = VibeSort::Client.new(api_key: ENV['ANTHROPIC_API_KEY']) # sk-ant-... => :anthropic
19
24
  #
20
- # @example Anthropic Claude
21
- # client = VibeSort::Client.new(provider: :anthropic, api_key: ENV['ANTHROPIC_API_KEY'])
25
+ # @example OpenAI (explicit)
26
+ # client = VibeSort::Client.new(provider: :openai, api_key: ENV['OPENAI_API_KEY'])
22
27
  #
23
28
  # @example Google Gemini with a custom model
24
29
  # client = VibeSort::Client.new(provider: :gemini, api_key: ENV['GEMINI_API_KEY'], model: 'gemini-2.5-pro')
25
- def initialize(api_key:, temperature: 0.0, provider: :openai, model: nil)
26
- @config = Configuration.new(api_key: api_key, temperature: temperature, provider: provider, model: model)
30
+ #
31
+ # @example OpenRouter with extra request parameters
32
+ # client = VibeSort::Client.new(
33
+ # provider: :openrouter,
34
+ # api_key: ENV['OPENROUTER_API_KEY'],
35
+ # model: 'meta-llama/llama-3.3-70b-instruct',
36
+ # extra_params: { max_tokens: 200 }
37
+ # )
38
+ #
39
+ # @example TypeSafe Jev (System One) with the pairwise merge-sort strategy
40
+ # client = VibeSort::Client.new(api_key: ENV['TYPESAFE_API_KEY'], provider: :jev, strategy: :noul)
41
+ #
42
+ # @example Jev through OpenRouter (an sk-or- key plus a jev model or a strategy: routes there)
43
+ # client = VibeSort::Client.new(api_key: ENV['OPENROUTER_API_KEY'], model: 'jev-latest')
44
+ #
45
+ # System One accepts extra keywords (+strategy:+, +by:+, +order:+, +long_context:+, ...), all
46
+ # of which can also be passed per call to #sort / #sort_with_metadata. +api_key+ may be omitted
47
+ # when TYPESAFE_API_KEY is set. See VibeSort::Providers::Jev for the full list.
48
+ #
49
+ # @param base_url [String, nil] System One gateway root override (Jev only)
50
+ def initialize(api_key: nil, temperature: 0.0, provider: nil, model: nil, extra_params: {}, base_url: nil, **jev_options)
51
+ validate_jev_options!(jev_options)
52
+ @config = Configuration.new(api_key: api_key, temperature: temperature, provider: provider, model: model,
53
+ extra_params: extra_params, base_url: base_url, jev_options: jev_options)
54
+ reject_jev_options_for_llm!(jev_options)
27
55
  end
28
56
 
29
57
  # Sort an array of numbers and/or strings using the configured provider's API
@@ -53,21 +81,28 @@ module VibeSort
53
81
  # @example API error
54
82
  # result = client.sort([1, 2, 3]) # with invalid API key
55
83
  # #=> { success: false, sorted_array: [], error: "OpenAI API error: Invalid API key" }
56
- # # The error prefix names the configured provider (OpenAI, Anthropic, or Gemini)
57
- def sort(array)
58
- # Validate input
84
+ # # The error prefix names the configured provider (OpenAI, Anthropic, Gemini, ...)
85
+ #
86
+ # With a System One (Jev) provider the array may hold any objects (the originals are
87
+ # returned, never rebuilt from model text), an empty array is valid, and per-call
88
+ # options such as +strategy:+, +by:+ and +order:+ are accepted.
89
+ #
90
+ # @example Jev, descending, by a custom criterion
91
+ # client.sort(%w[ghost-pepper jalapeno habanero], by: "spiciness", order: :desc)
92
+ # #=> { success: true, sorted_array: ["ghost-pepper", "habanero", "jalapeno"] }
93
+ def sort(array, **options)
59
94
  unless valid_input?(array)
60
95
  return {
61
96
  success: false,
62
97
  sorted_array: [],
63
- error: "Input must be an array of numbers or strings"
98
+ error: config.jev? ? "Input must be an array" : "Input must be an array of numbers or strings"
64
99
  }
65
100
  end
66
101
 
67
102
  # Perform the sort via API
68
103
  sorter = Sorter.new(config)
69
- sorter.perform(array)
70
- rescue ApiError => e
104
+ sorter.perform(array, **options)
105
+ rescue ApiError, ConfigurationError, BogoExhaustedError => e
71
106
  {
72
107
  success: false,
73
108
  sorted_array: [],
@@ -81,14 +116,50 @@ module VibeSort
81
116
  }
82
117
  end
83
118
 
119
+ # Sort with a System One (Jev) provider and get the full picture back.
120
+ #
121
+ # Unlike #sort this raises on failure (VibeSort::ApiError subclasses,
122
+ # ConfigurationError, BogoExhaustedError) and returns a VibeSort::Result carrying the
123
+ # sorted items, resolved model, API call count, token usage, per-item scores /
124
+ # confidences / probabilities, and warnings.
125
+ #
126
+ # @param array [Array] any objects
127
+ # @param options [Hash] per-call System One options (+strategy:+, +by:+, +order:+, +rubric:+, ...)
128
+ # @return [VibeSort::Result]
129
+ #
130
+ # @example
131
+ # result = client.sort_with_metadata(%w[pizza kale cake], by: "tastiness", strategy: :score)
132
+ # result.items #=> ["kale", "pizza", "cake"]
133
+ # result.item_metadata #=> [{ index: 1, score: 1.2, confidence: 0.8, ... }, ...]
134
+ def sort_with_metadata(array, **options)
135
+ raise ArgumentError, "Input must be an array" unless array.is_a?(Array)
136
+
137
+ Sorter.new(config).perform_with_metadata(array, **options)
138
+ end
139
+
84
140
  private
85
141
 
86
- # Validate that input is an array of numbers and/or strings
142
+ def validate_jev_options!(jev_options)
143
+ unknown = jev_options.keys - Providers::Jev::CLIENT_OPTIONS
144
+ return if unknown.empty?
145
+
146
+ raise ConfigurationError, "Unknown option(s): #{unknown.join(", ")}"
147
+ end
148
+
149
+ def reject_jev_options_for_llm!(jev_options)
150
+ return if config.jev? || jev_options.empty?
151
+
152
+ raise ConfigurationError, "#{jev_options.keys.join(", ")}: only supported by System One (:jev) providers, not :#{config.provider}"
153
+ end
154
+
155
+ # Validate the input array: Jev accepts any array; generative providers need a
156
+ # non-empty array of numbers and/or strings
87
157
  #
88
158
  # @param array [Object] Input to validate
89
159
  # @return [Boolean] true if valid, false otherwise
90
160
  def valid_input?(array)
91
161
  return false unless array.is_a?(Array)
162
+ return true if config.jev?
92
163
  return false if array.empty?
93
164
 
94
165
  array.all? { |item| item.is_a?(Numeric) || item.is_a?(String) }
@@ -2,29 +2,81 @@
2
2
 
3
3
  module VibeSort
4
4
  # Configuration class for VibeSort
5
- # Holds provider, API key, model, and temperature settings
5
+ # Holds provider, API key, model, temperature, and extra request parameters
6
6
  class Configuration
7
- PROVIDERS = %i[openai anthropic gemini groq spacexai].freeze
7
+ PROVIDERS = %i[openai anthropic gemini groq spacexai openrouter].freeze
8
8
 
9
- attr_reader :api_key, :temperature, :provider, :model
9
+ attr_reader :api_key, :temperature, :provider, :model, :extra_params, :base_url, :gateway, :jev_options
10
10
 
11
11
  # Initialize a new Configuration
12
12
  #
13
13
  # @param api_key [String] API key for the selected provider
14
14
  # @param temperature [Float] Temperature for the model (0.0 to 2.0).
15
15
  # Ignored by providers whose current models do not accept it (Anthropic).
16
- # @param provider [Symbol, String] LLM provider: :openai (default), :anthropic, :gemini, :groq, or :spacexai
16
+ # @param provider [Symbol, String, nil] LLM provider: :openai, :anthropic, :gemini,
17
+ # :groq, :spacexai, or :openrouter. When nil (default), the provider is inferred
18
+ # from the api_key prefix, falling back to :openai for unrecognized keys.
17
19
  # @param model [String, nil] Model ID override (nil uses the provider's default)
20
+ # @param extra_params [Hash] Provider-native request parameters deep-merged into
21
+ # the payload last, so they can override anything (e.g. { max_tokens: 100 })
22
+ # @param base_url [String, nil] System One gateway root (Jev only)
23
+ # @param jev_options [Hash] System One options (strategy:, long_context:, ...); see Providers::Jev
18
24
  # @raise [ArgumentError] if api_key is nil or empty, or provider is unknown
19
- def initialize(api_key:, temperature: 0.0, provider: :openai, model: nil)
20
- raise ArgumentError, "API key cannot be nil or empty" if api_key.nil? || api_key.empty?
25
+ # @raise [ConfigurationError] for contradictory options (e.g. strategy: with a generative provider)
26
+ def initialize(api_key: nil, temperature: 0.0, provider: nil, model: nil, extra_params: {}, base_url: nil, jev_options: {})
27
+ validate_provider_name!(provider)
28
+ resolution = ProviderResolver.resolve(api_key: api_key, provider: provider, model: model,
29
+ strategy: jev_options[:strategy], base_url: base_url)
30
+ @temperature = temperature
31
+ @extra_params = extra_params
32
+ @jev_options = jev_options
33
+ apply(resolution, provider, model)
34
+ end
21
35
 
22
- @provider = provider.to_s.to_sym
23
- raise ArgumentError, "Unknown provider: #{provider.inspect} (supported: #{PROVIDERS.join(", ")})" unless PROVIDERS.include?(@provider)
36
+ # @return [Boolean] true when routed to TypeSafe System One (Jev)
37
+ def jev?
38
+ provider == :jev
39
+ end
24
40
 
25
- @api_key = api_key
26
- @temperature = temperature
27
- @model = model
41
+ private
42
+
43
+ def apply(resolution, requested_provider, model)
44
+ @api_key = resolution.api_key
45
+ @base_url = resolution.base_url
46
+ @gateway = resolution.gateway
47
+ if resolution.jev?
48
+ @provider = :jev
49
+ @model = resolution.model
50
+ else
51
+ @provider = resolve_provider(requested_provider, @api_key)
52
+ @model = model
53
+ end
54
+ end
55
+
56
+ def validate_provider_name!(provider)
57
+ return if provider.nil?
58
+
59
+ explicit = provider.to_s.to_sym
60
+ return if PROVIDERS.include?(explicit) || ProviderResolver::JEV_PROVIDERS.include?(explicit)
61
+
62
+ raise ArgumentError, "Unknown provider: #{provider.inspect} (supported: #{(PROVIDERS + ProviderResolver::JEV_PROVIDERS).join(", ")})"
63
+ end
64
+
65
+ # Resolve the provider: explicit argument wins (with a non-fatal warning if the
66
+ # key prefix suggests a different provider); otherwise infer from the key prefix,
67
+ # defaulting to :openai for unrecognized formats.
68
+ #
69
+ # @param provider [Symbol, String, nil] Explicit provider argument, if any
70
+ # @param api_key [String] API key used for prefix inference
71
+ # @return [Symbol] Resolved provider
72
+ # @raise [ArgumentError] if an explicit provider is unknown
73
+ def resolve_provider(provider, api_key)
74
+ detected = KeyDetector.detect(api_key)
75
+ return detected || :openai if provider.nil?
76
+
77
+ explicit = provider.to_s.to_sym
78
+ warn "vibe-sort: api_key format suggests :#{detected} but provider is :#{explicit}" if detected && detected != explicit
79
+ explicit
28
80
  end
29
81
  end
30
82
  end