summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.gitignore14
-rw-r--r--.rspec3
-rw-r--r--.rubocop.yml29
-rw-r--r--Gemfile13
-rw-r--r--Gemfile.lock125
-rw-r--r--LICENSE.txt21
-rw-r--r--README.md39
-rw-r--r--Rakefile12
-rwxr-xr-xbin/check32
-rwxr-xr-xbin/console11
-rwxr-xr-xbin/install23
-rwxr-xr-xbin/setup8
-rw-r--r--dispatch-adapter-interface-0.1.0.gembin0 -> 6656 bytes
-rw-r--r--dispatch-adapter-interface.gemspec31
-rw-r--r--lib/dispatch/adapter/interface.rb17
-rw-r--r--lib/dispatch/adapter/interface/base.rb31
-rw-r--r--lib/dispatch/adapter/interface/errors.rb30
-rw-r--r--lib/dispatch/adapter/interface/message.rb31
-rw-r--r--lib/dispatch/adapter/interface/model_info.rb17
-rw-r--r--lib/dispatch/adapter/interface/response.rb23
-rw-r--r--lib/dispatch/adapter/interface/tool_definition.rb7
-rw-r--r--lib/dispatch/adapter/interface/version.rb9
-rw-r--r--sig/dispatch/adapter/interface.rbs8
-rw-r--r--spec/dispatch/adapter/interface/base_spec.rb41
-rw-r--r--spec/dispatch/adapter/interface/errors_spec.rb69
-rw-r--r--spec/dispatch/adapter/interface/structs_spec.rb211
-rw-r--r--spec/dispatch/adapter/interface_spec.rb33
-rw-r--r--spec/spec_helper.rb12
28 files changed, 900 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a450396
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,14 @@
+/.bundle/
+/.yardoc
+/_yardoc/
+/coverage/
+/doc/
+/pkg/
+/spec/reports/
+/tmp/
+
+# rspec failure tracking
+.rspec_status
+
+# Test results
+test_results.txt
diff --git a/.rspec b/.rspec
new file mode 100644
index 0000000..34c5164
--- /dev/null
+++ b/.rspec
@@ -0,0 +1,3 @@
+--format documentation
+--color
+--require spec_helper
diff --git a/.rubocop.yml b/.rubocop.yml
new file mode 100644
index 0000000..bbf7073
--- /dev/null
+++ b/.rubocop.yml
@@ -0,0 +1,29 @@
+AllCops:
+ TargetRubyVersion: 3.2
+ NewCops: enable
+
+Style/StringLiterals:
+ EnforcedStyle: double_quotes
+
+Style/StringLiteralsInInterpolation:
+ EnforcedStyle: double_quotes
+
+Style/FrozenStringLiteralComment:
+ Enabled: true
+ EnforcedStyle: always
+
+Metrics/BlockLength:
+ Exclude:
+ - "spec/**/*"
+
+Metrics/MethodLength:
+ Max: 40
+
+Metrics/AbcSize:
+ Enabled: false
+
+Style/Documentation:
+ Enabled: false
+
+Style/RedundantStructKeywordInit:
+ Enabled: false
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..49ed832
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+source "https://rubygems.org"
+
+# Specify your gem's dependencies in dispatch-adapter-interface.gemspec
+gemspec
+
+gem "irb"
+gem "rake", "~> 13.0"
+
+gem "rspec", "~> 3.0"
+
+gem "rubocop", "~> 1.21"
diff --git a/Gemfile.lock b/Gemfile.lock
new file mode 100644
index 0000000..8f42933
--- /dev/null
+++ b/Gemfile.lock
@@ -0,0 +1,125 @@
+PATH
+ remote: .
+ specs:
+ dispatch-adapter-interface (0.1.0)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ ast (2.4.3)
+ date (3.5.1)
+ diff-lcs (1.6.2)
+ erb (6.0.2)
+ io-console (0.8.2)
+ irb (1.17.0)
+ pp (>= 0.6.0)
+ prism (>= 1.3.0)
+ rdoc (>= 4.0.0)
+ reline (>= 0.4.2)
+ json (2.19.3)
+ language_server-protocol (3.17.0.5)
+ lint_roller (1.1.0)
+ parallel (2.0.1)
+ parser (3.3.11.1)
+ ast (~> 2.4.1)
+ racc
+ pp (0.6.3)
+ prettyprint
+ prettyprint (0.2.0)
+ prism (1.9.0)
+ psych (5.3.1)
+ date
+ stringio
+ racc (1.8.1)
+ rainbow (3.1.1)
+ rake (13.3.1)
+ rdoc (7.2.0)
+ erb
+ psych (>= 4.0.0)
+ tsort
+ regexp_parser (2.12.0)
+ reline (0.6.3)
+ io-console (~> 0.5)
+ rspec (3.13.2)
+ rspec-core (~> 3.13.0)
+ rspec-expectations (~> 3.13.0)
+ rspec-mocks (~> 3.13.0)
+ rspec-core (3.13.6)
+ rspec-support (~> 3.13.0)
+ rspec-expectations (3.13.5)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.13.0)
+ rspec-mocks (3.13.8)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.13.0)
+ rspec-support (3.13.7)
+ rubocop (1.86.1)
+ json (~> 2.3)
+ language_server-protocol (~> 3.17.0.2)
+ lint_roller (~> 1.1.0)
+ parallel (>= 1.10)
+ parser (>= 3.3.0.2)
+ rainbow (>= 2.2.2, < 4.0)
+ regexp_parser (>= 2.9.3, < 3.0)
+ rubocop-ast (>= 1.49.0, < 2.0)
+ ruby-progressbar (~> 1.7)
+ unicode-display_width (>= 2.4.0, < 4.0)
+ rubocop-ast (1.49.1)
+ parser (>= 3.3.7.2)
+ prism (~> 1.7)
+ ruby-progressbar (1.13.0)
+ stringio (3.2.0)
+ tsort (0.2.0)
+ unicode-display_width (3.2.0)
+ unicode-emoji (~> 4.1)
+ unicode-emoji (4.2.0)
+
+PLATFORMS
+ ruby
+ x86_64-linux
+
+DEPENDENCIES
+ dispatch-adapter-interface!
+ irb
+ rake (~> 13.0)
+ rspec (~> 3.0)
+ rubocop (~> 1.21)
+
+CHECKSUMS
+ ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
+ date (3.5.1) sha256=750d06384d7b9c15d562c76291407d89e368dda4d4fff957eb94962d325a0dc0
+ diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
+ dispatch-adapter-interface (0.1.0)
+ erb (6.0.2) sha256=9fe6264d44f79422c87490a1558479bd0e7dad4dd0e317656e67ea3077b5242b
+ io-console (0.8.2) sha256=d6e3ae7a7cc7574f4b8893b4fca2162e57a825b223a177b7afa236c5ef9814cc
+ irb (1.17.0) sha256=168c4ddb93d8a361a045c41d92b2952c7a118fa73f23fe14e55609eb7a863aae
+ json (2.19.3) sha256=289b0bb53052a1fa8c34ab33cc750b659ba14a5c45f3fcf4b18762dc67c78646
+ language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc
+ lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87
+ parallel (2.0.1) sha256=337782d3e39f4121e67563bf91dd8ece67f48923d90698614773a0ec9a5b2c7d
+ parser (3.3.11.1) sha256=d17ace7aabe3e72c3cc94043714be27cc6f852f104d81aa284c2281aecc65d54
+ pp (0.6.3) sha256=2951d514450b93ccfeb1df7d021cae0da16e0a7f95ee1e2273719669d0ab9df6
+ prettyprint (0.2.0) sha256=2bc9e15581a94742064a3cc8b0fb9d45aae3d03a1baa6ef80922627a0766f193
+ prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85
+ psych (5.3.1) sha256=eb7a57cef10c9d70173ff74e739d843ac3b2c019a003de48447b2963d81b1974
+ racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f
+ rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
+ rake (13.3.1) sha256=8c9e89d09f66a26a01264e7e3480ec0607f0c497a861ef16063604b1b08eb19c
+ rdoc (7.2.0) sha256=8650f76cd4009c3b54955eb5d7e3a075c60a57276766ebf36f9085e8c9f23192
+ regexp_parser (2.12.0) sha256=35a916a1d63190ab5c9009457136ae5f3c0c7512d60291d0d1378ba18ce08ebb
+ reline (0.6.3) sha256=1198b04973565b36ec0f11542ab3f5cfeeec34823f4e54cebde90968092b1835
+ rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587
+ rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d
+ rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836
+ rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47
+ rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c
+ rubocop (1.86.1) sha256=44415f3f01d01a21e01132248d2fd0867572475b566ca188a0a42133a08d4531
+ rubocop-ast (1.49.1) sha256=4412f3ee70f6fe4546cc489548e0f6fcf76cafcfa80fa03af67098ffed755035
+ ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
+ stringio (3.2.0) sha256=c37cb2e58b4ffbd33fe5cd948c05934af997b36e0b6ca6fdf43afa234cf222e1
+ tsort (0.2.0) sha256=9650a793f6859a43b6641671278f79cfead60ac714148aabe4e3f0060480089f
+ unicode-display_width (3.2.0) sha256=0cdd96b5681a5949cdbc2c55e7b420facae74c4aaf9a9815eee1087cb1853c42
+ unicode-emoji (4.2.0) sha256=519e69150f75652e40bf736106cfbc8f0f73aa3fb6a65afe62fefa7f80b0f80f
+
+BUNDLED WITH
+ 4.0.9
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..974325c
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2026 Adam Malczewski
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3a21a20
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# Dispatch::Adapter::Interface
+
+TODO: Delete this and the text below, and describe your gem
+
+Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dispatch/adapter/interface`. To experiment with that code, run `bin/console` for an interactive prompt.
+
+## Installation
+
+TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
+
+Install the gem and add to the application's Gemfile by executing:
+
+```bash
+bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
+```
+
+If bundler is not being used to manage dependencies, install the gem by executing:
+
+```bash
+gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
+```
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Development
+
+After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
+
+To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
+
+## Contributing
+
+Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dispatch-adapter-interface.
+
+## License
+
+The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..cca7175
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+require "bundler/gem_tasks"
+require "rspec/core/rake_task"
+
+RSpec::Core::RakeTask.new(:spec)
+
+require "rubocop/rake_task"
+
+RuboCop::RakeTask.new
+
+task default: %i[spec rubocop]
diff --git a/bin/check b/bin/check
new file mode 100755
index 0000000..ddcad0c
--- /dev/null
+++ b/bin/check
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+cd "$(dirname "$0")/.."
+
+RESULTS_FILE="test_results.txt"
+
+{
+ echo "============================================"
+ echo "dispatch-adapter-interface — $(date)"
+ echo "============================================"
+ echo ""
+
+ echo "--- bundle install ---"
+ bundle install 2>&1
+ echo ""
+
+ echo "--- rubocop --autocorrect-all ---"
+ bundle exec rubocop --autocorrect-all 2>&1 || true
+ echo ""
+
+ echo "--- rspec ---"
+ bundle exec rspec 2>&1 || true
+ echo ""
+
+ echo "============================================"
+ echo "Done."
+ echo "============================================"
+} | tee "$RESULTS_FILE"
+
+echo ""
+echo "Results written to $RESULTS_FILE"
diff --git a/bin/console b/bin/console
new file mode 100755
index 0000000..9d66b1c
--- /dev/null
+++ b/bin/console
@@ -0,0 +1,11 @@
+#!/usr/bin/env ruby
+# frozen_string_literal: true
+
+require "bundler/setup"
+require "dispatch/adapter/interface"
+
+# You can add fixtures and/or initialization code here to make experimenting
+# with your gem easier. You can also use a different console, if you like.
+
+require "irb"
+IRB.start(__FILE__)
diff --git a/bin/install b/bin/install
new file mode 100755
index 0000000..1132573
--- /dev/null
+++ b/bin/install
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+cd "$(dirname "$0")/.."
+
+GEM_NAME="dispatch-adapter-interface"
+
+echo "--- Building $GEM_NAME ---"
+gem build "$GEM_NAME.gemspec"
+
+GEM_FILE=$(ls -t "$GEM_NAME"-*.gem 2>/dev/null | head -1)
+
+if [ -z "$GEM_FILE" ]; then
+ echo "ERROR: No .gem file found after build."
+ exit 1
+fi
+
+echo ""
+echo "--- Installing $GEM_FILE ---"
+gem install "$GEM_FILE" --local
+
+echo ""
+echo "Done. $GEM_NAME installed."
diff --git a/bin/setup b/bin/setup
new file mode 100755
index 0000000..dce67d8
--- /dev/null
+++ b/bin/setup
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+set -euo pipefail
+IFS=$'\n\t'
+set -vx
+
+bundle install
+
+# Do any other automated setup that you need to do here
diff --git a/dispatch-adapter-interface-0.1.0.gem b/dispatch-adapter-interface-0.1.0.gem
new file mode 100644
index 0000000..cebeb26
--- /dev/null
+++ b/dispatch-adapter-interface-0.1.0.gem
Binary files differ
diff --git a/dispatch-adapter-interface.gemspec b/dispatch-adapter-interface.gemspec
new file mode 100644
index 0000000..0cd449a
--- /dev/null
+++ b/dispatch-adapter-interface.gemspec
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require_relative "lib/dispatch/adapter/interface/version"
+
+Gem::Specification.new do |spec|
+ spec.name = "dispatch-adapter-interface"
+ spec.version = Dispatch::Adapter::Interface::VERSION
+ spec.authors = ["Adam Malczewski"]
+ spec.email = ["[email protected]"]
+
+ spec.summary = "Shared interface for Dispatch LLM adapter gems"
+ spec.description = "Defines the base class, data structs, and error hierarchy shared by all " \
+ "Dispatch adapter gems (Copilot, Claude, Tester, etc.)."
+ spec.homepage = "https://github.com/realtradam/dispatch-adapter-interface"
+ spec.license = "MIT"
+ spec.required_ruby_version = ">= 3.2.0"
+ spec.metadata["homepage_uri"] = spec.homepage
+ spec.metadata["source_code_uri"] = spec.homepage
+ spec.metadata["rubygems_mfa_required"] = "true"
+
+ gemspec = File.basename(__FILE__)
+ spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
+ ls.readlines("\x0", chomp: true).reject do |f|
+ (f == gemspec) ||
+ f.start_with?(*%w[bin/ Gemfile .gitignore .rspec spec/ .rubocop.yml])
+ end
+ end
+ spec.bindir = "exe"
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
+ spec.require_paths = ["lib"]
+end
diff --git a/lib/dispatch/adapter/interface.rb b/lib/dispatch/adapter/interface.rb
new file mode 100644
index 0000000..54692c0
--- /dev/null
+++ b/lib/dispatch/adapter/interface.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+require_relative "interface/version"
+
+require_relative "interface/errors"
+require_relative "interface/message"
+require_relative "interface/response"
+require_relative "interface/tool_definition"
+require_relative "interface/model_info"
+require_relative "interface/base"
+
+module Dispatch
+ module Adapter
+ module Interface
+ end
+ end
+end
diff --git a/lib/dispatch/adapter/interface/base.rb b/lib/dispatch/adapter/interface/base.rb
new file mode 100644
index 0000000..4b7a6ed
--- /dev/null
+++ b/lib/dispatch/adapter/interface/base.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ class Base
+ def chat(_messages, system: nil, tools: [], stream: false, max_tokens: nil, thinking: nil, &_block)
+ raise NotImplementedError, "#{self.class}#chat must be implemented"
+ end
+
+ def model_name
+ raise NotImplementedError, "#{self.class}#model_name must be implemented"
+ end
+
+ def count_tokens(_messages, system: nil, tools: []) # rubocop:disable Lint/UnusedMethodArgument
+ -1
+ end
+
+ def list_models
+ raise NotImplementedError, "#{self.class}#list_models must be implemented"
+ end
+
+ def provider_name
+ self.class.name
+ end
+
+ def max_context_tokens
+ nil
+ end
+ end
+ end
+end
diff --git a/lib/dispatch/adapter/interface/errors.rb b/lib/dispatch/adapter/interface/errors.rb
new file mode 100644
index 0000000..86f9c14
--- /dev/null
+++ b/lib/dispatch/adapter/interface/errors.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ class Error < StandardError
+ attr_reader :status_code, :provider
+
+ def initialize(message = nil, status_code: nil, provider: nil)
+ @status_code = status_code
+ @provider = provider
+ super(message)
+ end
+ end
+
+ class AuthenticationError < Error; end
+
+ class RateLimitError < Error
+ attr_reader :retry_after
+
+ def initialize(message = nil, status_code: nil, provider: nil, retry_after: nil)
+ @retry_after = retry_after
+ super(message, status_code:, provider:)
+ end
+ end
+
+ class ServerError < Error; end
+ class RequestError < Error; end
+ class ConnectionError < Error; end
+ end
+end
diff --git a/lib/dispatch/adapter/interface/message.rb b/lib/dispatch/adapter/interface/message.rb
new file mode 100644
index 0000000..eb51c99
--- /dev/null
+++ b/lib/dispatch/adapter/interface/message.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ Message = Struct.new(:role, :content, keyword_init: true)
+
+ TextBlock = Struct.new(:type, :text, keyword_init: true) do
+ def initialize(text:, type: "text")
+ super(type:, text:)
+ end
+ end
+
+ ImageBlock = Struct.new(:type, :source, :media_type, keyword_init: true) do
+ def initialize(source:, media_type:, type: "image")
+ super(type:, source:, media_type:)
+ end
+ end
+
+ ToolUseBlock = Struct.new(:type, :id, :name, :arguments, keyword_init: true) do
+ def initialize(id:, name:, arguments:, type: "tool_use")
+ super(type:, id:, name:, arguments:)
+ end
+ end
+
+ ToolResultBlock = Struct.new(:type, :tool_use_id, :content, :is_error, keyword_init: true) do
+ def initialize(tool_use_id:, content:, is_error: false, type: "tool_result")
+ super(type:, tool_use_id:, content:, is_error:)
+ end
+ end
+ end
+end
diff --git a/lib/dispatch/adapter/interface/model_info.rb b/lib/dispatch/adapter/interface/model_info.rb
new file mode 100644
index 0000000..8ba2977
--- /dev/null
+++ b/lib/dispatch/adapter/interface/model_info.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ ModelInfo = Struct.new(
+ :id, :name, :max_context_tokens,
+ :supports_vision, :supports_tool_use, :supports_streaming,
+ :premium_request_multiplier,
+ keyword_init: true
+ ) do
+ def initialize(id:, name:, max_context_tokens:, supports_vision:, supports_tool_use:, supports_streaming:,
+ premium_request_multiplier: nil)
+ super
+ end
+ end
+ end
+end
diff --git a/lib/dispatch/adapter/interface/response.rb b/lib/dispatch/adapter/interface/response.rb
new file mode 100644
index 0000000..b4ba3eb
--- /dev/null
+++ b/lib/dispatch/adapter/interface/response.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ Response = Struct.new(:content, :tool_calls, :model, :stop_reason, :usage, keyword_init: true) do
+ def initialize(model:, stop_reason:, usage:, content: nil, tool_calls: [])
+ super
+ end
+ end
+
+ Usage = Struct.new(:input_tokens, :output_tokens, :cache_read_tokens, :cache_creation_tokens, keyword_init: true) do
+ def initialize(input_tokens:, output_tokens:, cache_read_tokens: 0, cache_creation_tokens: 0)
+ super
+ end
+ end
+
+ StreamDelta = Struct.new(:type, :text, :tool_call_id, :tool_name, :argument_delta, keyword_init: true) do
+ def initialize(type:, text: nil, tool_call_id: nil, tool_name: nil, argument_delta: nil)
+ super
+ end
+ end
+ end
+end
diff --git a/lib/dispatch/adapter/interface/tool_definition.rb b/lib/dispatch/adapter/interface/tool_definition.rb
new file mode 100644
index 0000000..7b435a3
--- /dev/null
+++ b/lib/dispatch/adapter/interface/tool_definition.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ ToolDefinition = Struct.new(:name, :description, :parameters, keyword_init: true)
+ end
+end
diff --git a/lib/dispatch/adapter/interface/version.rb b/lib/dispatch/adapter/interface/version.rb
new file mode 100644
index 0000000..0a53b34
--- /dev/null
+++ b/lib/dispatch/adapter/interface/version.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+module Dispatch
+ module Adapter
+ module Interface
+ VERSION = "0.1.0"
+ end
+ end
+end
diff --git a/sig/dispatch/adapter/interface.rbs b/sig/dispatch/adapter/interface.rbs
new file mode 100644
index 0000000..9625a4c
--- /dev/null
+++ b/sig/dispatch/adapter/interface.rbs
@@ -0,0 +1,8 @@
+module Dispatch
+ module Adapter
+ module Interface
+ VERSION: String
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
+ end
+ end
+end
diff --git a/spec/dispatch/adapter/interface/base_spec.rb b/spec/dispatch/adapter/interface/base_spec.rb
new file mode 100644
index 0000000..8c3a279
--- /dev/null
+++ b/spec/dispatch/adapter/interface/base_spec.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+RSpec.describe Dispatch::Adapter::Base do
+ subject(:base) { described_class.new }
+
+ describe "#chat" do
+ it "raises NotImplementedError" do
+ expect { base.chat([]) }.to raise_error(NotImplementedError, /chat must be implemented/)
+ end
+ end
+
+ describe "#model_name" do
+ it "raises NotImplementedError" do
+ expect { base.model_name }.to raise_error(NotImplementedError, /model_name must be implemented/)
+ end
+ end
+
+ describe "#count_tokens" do
+ it "returns -1" do
+ expect(base.count_tokens([])).to eq(-1)
+ end
+ end
+
+ describe "#list_models" do
+ it "raises NotImplementedError" do
+ expect { base.list_models }.to raise_error(NotImplementedError, /list_models must be implemented/)
+ end
+ end
+
+ describe "#provider_name" do
+ it "returns the class name" do
+ expect(base.provider_name).to eq("Dispatch::Adapter::Base")
+ end
+ end
+
+ describe "#max_context_tokens" do
+ it "returns nil" do
+ expect(base.max_context_tokens).to be_nil
+ end
+ end
+end
diff --git a/spec/dispatch/adapter/interface/errors_spec.rb b/spec/dispatch/adapter/interface/errors_spec.rb
new file mode 100644
index 0000000..906a4c2
--- /dev/null
+++ b/spec/dispatch/adapter/interface/errors_spec.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+RSpec.describe Dispatch::Adapter::Error do
+ it "carries message, status_code, and provider" do
+ error = described_class.new("test error", status_code: 500, provider: "TestProvider")
+ expect(error.message).to eq("test error")
+ expect(error.status_code).to eq(500)
+ expect(error.provider).to eq("TestProvider")
+ end
+
+ it "defaults status_code and provider to nil" do
+ error = described_class.new("simple error")
+ expect(error.status_code).to be_nil
+ expect(error.provider).to be_nil
+ end
+
+ it "inherits from StandardError" do
+ expect(described_class.ancestors).to include(StandardError)
+ end
+
+ it "can be rescued as StandardError" do
+ expect do
+ raise described_class, "test"
+ end.to raise_error(StandardError)
+ end
+end
+
+RSpec.describe Dispatch::Adapter::AuthenticationError do
+ it "inherits from Error" do
+ expect(described_class.ancestors).to include(Dispatch::Adapter::Error)
+ end
+end
+
+RSpec.describe Dispatch::Adapter::RateLimitError do
+ it "carries retry_after" do
+ error = described_class.new("rate limited", status_code: 429, provider: "Test", retry_after: 30)
+ expect(error.retry_after).to eq(30)
+ expect(error.status_code).to eq(429)
+ end
+
+ it "defaults retry_after to nil" do
+ error = described_class.new("rate limited")
+ expect(error.retry_after).to be_nil
+ end
+
+ it "is rescuable as Dispatch::Adapter::Error" do
+ expect do
+ raise described_class, "rate limited"
+ end.to raise_error(Dispatch::Adapter::Error)
+ end
+end
+
+RSpec.describe Dispatch::Adapter::ServerError do
+ it "inherits from Error" do
+ expect(described_class.ancestors).to include(Dispatch::Adapter::Error)
+ end
+end
+
+RSpec.describe Dispatch::Adapter::RequestError do
+ it "inherits from Error" do
+ expect(described_class.ancestors).to include(Dispatch::Adapter::Error)
+ end
+end
+
+RSpec.describe Dispatch::Adapter::ConnectionError do
+ it "inherits from Error" do
+ expect(described_class.ancestors).to include(Dispatch::Adapter::Error)
+ end
+end
diff --git a/spec/dispatch/adapter/interface/structs_spec.rb b/spec/dispatch/adapter/interface/structs_spec.rb
new file mode 100644
index 0000000..ef8ec73
--- /dev/null
+++ b/spec/dispatch/adapter/interface/structs_spec.rb
@@ -0,0 +1,211 @@
+# frozen_string_literal: true
+
+RSpec.describe Dispatch::Adapter do
+ describe "Message" do
+ it "creates with keyword args" do
+ msg = Dispatch::Adapter::Message.new(role: "user", content: "Hello")
+ expect(msg.role).to eq("user")
+ expect(msg.content).to eq("Hello")
+ end
+
+ it "accepts array content" do
+ blocks = [Dispatch::Adapter::TextBlock.new(text: "hi")]
+ msg = Dispatch::Adapter::Message.new(role: "user", content: blocks)
+ expect(msg.content).to be_an(Array)
+ expect(msg.content.first.text).to eq("hi")
+ end
+ end
+
+ describe "TextBlock" do
+ it "defaults type to 'text'" do
+ block = Dispatch::Adapter::TextBlock.new(text: "hello")
+ expect(block.type).to eq("text")
+ expect(block.text).to eq("hello")
+ end
+ end
+
+ describe "ImageBlock" do
+ it "defaults type to 'image'" do
+ block = Dispatch::Adapter::ImageBlock.new(source: "data:image/png;base64,abc", media_type: "image/png")
+ expect(block.type).to eq("image")
+ expect(block.source).to eq("data:image/png;base64,abc")
+ expect(block.media_type).to eq("image/png")
+ end
+ end
+
+ describe "ToolUseBlock" do
+ it "defaults type to 'tool_use'" do
+ block = Dispatch::Adapter::ToolUseBlock.new(id: "call_1", name: "get_weather", arguments: { "city" => "NYC" })
+ expect(block.type).to eq("tool_use")
+ expect(block.id).to eq("call_1")
+ expect(block.name).to eq("get_weather")
+ expect(block.arguments).to eq({ "city" => "NYC" })
+ end
+ end
+
+ describe "ToolResultBlock" do
+ it "defaults type to 'tool_result' and is_error to false" do
+ block = Dispatch::Adapter::ToolResultBlock.new(tool_use_id: "call_1", content: "72F")
+ expect(block.type).to eq("tool_result")
+ expect(block.tool_use_id).to eq("call_1")
+ expect(block.content).to eq("72F")
+ expect(block.is_error).to be(false)
+ end
+
+ it "accepts is_error flag" do
+ block = Dispatch::Adapter::ToolResultBlock.new(tool_use_id: "call_1", content: "Error", is_error: true)
+ expect(block.is_error).to be(true)
+ end
+ end
+
+ describe "ToolDefinition" do
+ it "creates with keyword args" do
+ td = Dispatch::Adapter::ToolDefinition.new(
+ name: "search",
+ description: "Search the web",
+ parameters: { "type" => "object", "properties" => {} }
+ )
+ expect(td.name).to eq("search")
+ expect(td.description).to eq("Search the web")
+ expect(td.parameters).to be_a(Hash)
+ end
+ end
+
+ describe "Response" do
+ it "creates with defaults" do
+ usage = Dispatch::Adapter::Usage.new(input_tokens: 10, output_tokens: 20)
+ resp = Dispatch::Adapter::Response.new(model: "gpt-4", stop_reason: :end_turn, usage: usage)
+ expect(resp.content).to be_nil
+ expect(resp.tool_calls).to eq([])
+ expect(resp.model).to eq("gpt-4")
+ expect(resp.stop_reason).to eq(:end_turn)
+ expect(resp.usage).to eq(usage)
+ end
+
+ it "creates with all fields" do
+ usage = Dispatch::Adapter::Usage.new(input_tokens: 10, output_tokens: 20)
+ tool_call = Dispatch::Adapter::ToolUseBlock.new(id: "1", name: "test", arguments: {})
+ resp = Dispatch::Adapter::Response.new(
+ content: "Hello",
+ tool_calls: [tool_call],
+ model: "gpt-4",
+ stop_reason: :tool_use,
+ usage: usage
+ )
+ expect(resp.content).to eq("Hello")
+ expect(resp.tool_calls.size).to eq(1)
+ end
+ end
+
+ describe "Usage" do
+ it "defaults cache tokens to 0" do
+ usage = Dispatch::Adapter::Usage.new(input_tokens: 100, output_tokens: 50)
+ expect(usage.cache_read_tokens).to eq(0)
+ expect(usage.cache_creation_tokens).to eq(0)
+ end
+
+ it "accepts cache tokens" do
+ usage = Dispatch::Adapter::Usage.new(
+ input_tokens: 100,
+ output_tokens: 50,
+ cache_read_tokens: 10,
+ cache_creation_tokens: 5
+ )
+ expect(usage.cache_read_tokens).to eq(10)
+ expect(usage.cache_creation_tokens).to eq(5)
+ end
+ end
+
+ describe "StreamDelta" do
+ it "creates a text_delta" do
+ delta = Dispatch::Adapter::StreamDelta.new(type: :text_delta, text: "Hello")
+ expect(delta.type).to eq(:text_delta)
+ expect(delta.text).to eq("Hello")
+ expect(delta.tool_call_id).to be_nil
+ end
+
+ it "creates a tool_use_start" do
+ delta = Dispatch::Adapter::StreamDelta.new(type: :tool_use_start, tool_call_id: "1", tool_name: "search")
+ expect(delta.type).to eq(:tool_use_start)
+ expect(delta.tool_call_id).to eq("1")
+ expect(delta.tool_name).to eq("search")
+ end
+
+ it "creates a tool_use_delta" do
+ delta = Dispatch::Adapter::StreamDelta.new(type: :tool_use_delta, tool_call_id: "1", argument_delta: '{"q":')
+ expect(delta.type).to eq(:tool_use_delta)
+ expect(delta.argument_delta).to eq('{"q":')
+ end
+ end
+
+ describe "ModelInfo" do
+ it "creates with all fields" do
+ info = Dispatch::Adapter::ModelInfo.new(
+ id: "gpt-4",
+ name: "GPT-4",
+ max_context_tokens: 8192,
+ supports_vision: false,
+ supports_tool_use: true,
+ supports_streaming: true
+ )
+ expect(info.id).to eq("gpt-4")
+ expect(info.name).to eq("GPT-4")
+ expect(info.max_context_tokens).to eq(8192)
+ expect(info.supports_vision).to be(false)
+ expect(info.supports_tool_use).to be(true)
+ expect(info.supports_streaming).to be(true)
+ expect(info.premium_request_multiplier).to be_nil
+ end
+
+ it "accepts premium_request_multiplier" do
+ info = Dispatch::Adapter::ModelInfo.new(
+ id: "o3",
+ name: "o3",
+ max_context_tokens: 200_000,
+ supports_vision: false,
+ supports_tool_use: true,
+ supports_streaming: true,
+ premium_request_multiplier: 30.0
+ )
+ expect(info.premium_request_multiplier).to eq(30.0)
+ end
+
+ it "defaults premium_request_multiplier to nil" do
+ info = Dispatch::Adapter::ModelInfo.new(
+ id: "gpt-4.1-nano",
+ name: "GPT 4.1 Nano",
+ max_context_tokens: 1_047_576,
+ supports_vision: false,
+ supports_tool_use: true,
+ supports_streaming: true
+ )
+ expect(info.premium_request_multiplier).to be_nil
+ end
+ end
+
+ describe "Struct equality" do
+ it "considers structs with same values equal" do
+ a = Dispatch::Adapter::Message.new(role: "user", content: "hello")
+ b = Dispatch::Adapter::Message.new(role: "user", content: "hello")
+ expect(a).to eq(b)
+ end
+
+ it "considers structs with different values not equal" do
+ a = Dispatch::Adapter::Message.new(role: "user", content: "hello")
+ b = Dispatch::Adapter::Message.new(role: "user", content: "goodbye")
+ expect(a).not_to eq(b)
+ end
+
+ it "Usage structs are equal with same tokens" do
+ a = Dispatch::Adapter::Usage.new(input_tokens: 10, output_tokens: 20)
+ b = Dispatch::Adapter::Usage.new(input_tokens: 10, output_tokens: 20)
+ expect(a).to eq(b)
+ end
+
+ it "ToolUseBlock structs are equal with same fields" do
+ a = Dispatch::Adapter::ToolUseBlock.new(id: "1", name: "test", arguments: { "k" => "v" })
+ b = Dispatch::Adapter::ToolUseBlock.new(id: "1", name: "test", arguments: { "k" => "v" })
+ expect(a).to eq(b)
+ end
+ end
+end
diff --git a/spec/dispatch/adapter/interface_spec.rb b/spec/dispatch/adapter/interface_spec.rb
new file mode 100644
index 0000000..a62ed81
--- /dev/null
+++ b/spec/dispatch/adapter/interface_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+RSpec.describe Dispatch::Adapter::Interface do
+ it "has a version number" do
+ expect(Dispatch::Adapter::Interface::VERSION).not_to be_nil
+ end
+
+ it "exposes the Base class" do
+ expect(Dispatch::Adapter::Base).to be_a(Class)
+ end
+
+ it "exposes error classes" do
+ expect(Dispatch::Adapter::Error).to be < StandardError
+ expect(Dispatch::Adapter::AuthenticationError).to be < Dispatch::Adapter::Error
+ expect(Dispatch::Adapter::RateLimitError).to be < Dispatch::Adapter::Error
+ expect(Dispatch::Adapter::ServerError).to be < Dispatch::Adapter::Error
+ expect(Dispatch::Adapter::RequestError).to be < Dispatch::Adapter::Error
+ expect(Dispatch::Adapter::ConnectionError).to be < Dispatch::Adapter::Error
+ end
+
+ it "exposes data structs" do
+ expect(Dispatch::Adapter::Message).to be_a(Class)
+ expect(Dispatch::Adapter::TextBlock).to be_a(Class)
+ expect(Dispatch::Adapter::ImageBlock).to be_a(Class)
+ expect(Dispatch::Adapter::ToolUseBlock).to be_a(Class)
+ expect(Dispatch::Adapter::ToolResultBlock).to be_a(Class)
+ expect(Dispatch::Adapter::ToolDefinition).to be_a(Class)
+ expect(Dispatch::Adapter::Response).to be_a(Class)
+ expect(Dispatch::Adapter::Usage).to be_a(Class)
+ expect(Dispatch::Adapter::StreamDelta).to be_a(Class)
+ expect(Dispatch::Adapter::ModelInfo).to be_a(Class)
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..8aa8377
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+require "dispatch/adapter/interface"
+
+RSpec.configure do |config|
+ config.example_status_persistence_file_path = ".rspec_status"
+ config.disable_monkey_patching!
+
+ config.expect_with :rspec do |c|
+ c.syntax = :expect
+ end
+end