summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/adapter/interface
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dispatch/adapter/interface')
-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
7 files changed, 148 insertions, 0 deletions
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