Skip to content

Commit 0ef168a

Browse files
feat: support sorbet aliases at the runtime
1 parent 813a19d commit 0ef168a

File tree

475 files changed

+5541
-711
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

475 files changed

+5541
-711
lines changed

lib/openai/internal.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
module OpenAI
44
module Internal
5+
extend OpenAI::Internal::Util::SorbetRuntimeSupport
6+
57
OMIT =
68
Object.new.tap do
79
_1.define_singleton_method(:inspect) { "#<#{OpenAI::Internal}::OMIT>" }
810
end
911
.freeze
12+
13+
define_sorbet_constant!(:AnyHash) do
14+
T.type_alias { T::Hash[Symbol, T.anything] }
15+
end
1016
end
1117
end

lib/openai/internal/transport/base_client.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Transport
77
#
88
# @abstract
99
class BaseClient
10+
extend OpenAI::Internal::Util::SorbetRuntimeSupport
11+
1012
# from whatwg fetch spec
1113
MAX_REDIRECTS = 20
1214

@@ -477,6 +479,54 @@ def inspect
477479
"#<#{self.class.name}:0x#{object_id.to_s(16)} base_url=#{base_url} max_retries=#{@max_retries} timeout=#{@timeout}>"
478480
# rubocop:enable Layout/LineLength
479481
end
482+
483+
define_sorbet_constant!(:RequestComponents) do
484+
T.type_alias do
485+
{
486+
method: Symbol,
487+
path: T.any(String, T::Array[String]),
488+
query: T.nilable(T::Hash[String, T.nilable(T.any(T::Array[String], String))]),
489+
headers: T.nilable(
490+
T::Hash[String,
491+
T.nilable(
492+
T.any(
493+
String,
494+
Integer,
495+
T::Array[T.nilable(T.any(String, Integer))]
496+
)
497+
)]
498+
),
499+
body: T.nilable(T.anything),
500+
unwrap: T.nilable(
501+
T.any(
502+
Symbol,
503+
Integer,
504+
T::Array[T.any(Symbol, Integer)],
505+
T.proc.params(arg0: T.anything).returns(T.anything)
506+
)
507+
),
508+
page: T.nilable(T::Class[OpenAI::Internal::Type::BasePage[OpenAI::Internal::Type::BaseModel]]),
509+
stream: T.nilable(
510+
T::Class[OpenAI::Internal::Type::BaseStream[T.anything,
511+
OpenAI::Internal::Type::BaseModel]]
512+
),
513+
model: T.nilable(OpenAI::Internal::Type::Converter::Input),
514+
options: T.nilable(OpenAI::RequestOptions::OrHash)
515+
}
516+
end
517+
end
518+
define_sorbet_constant!(:RequestInput) do
519+
T.type_alias do
520+
{
521+
method: Symbol,
522+
url: URI::Generic,
523+
headers: T::Hash[String, String],
524+
body: T.anything,
525+
max_retries: Integer,
526+
timeout: Float
527+
}
528+
end
529+
end
480530
end
481531
end
482532
end

lib/openai/internal/transport/pooled_net_requester.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Internal
55
module Transport
66
# @api private
77
class PooledNetRequester
8+
extend OpenAI::Internal::Util::SorbetRuntimeSupport
9+
810
# from the golang stdlib
911
# https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
1012
KEEP_ALIVE_TIMEOUT = 30
@@ -187,6 +189,18 @@ def initialize(size: Etc.nprocessors)
187189
@size = size
188190
@pools = {}
189191
end
192+
193+
define_sorbet_constant!(:Request) do
194+
T.type_alias do
195+
{
196+
method: Symbol,
197+
url: URI::Generic,
198+
headers: T::Hash[String, String],
199+
body: T.anything,
200+
deadline: Float
201+
}
202+
end
203+
end
190204
end
191205
end
192206
end

lib/openai/internal/type/array_of.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ArrayOf
2929
#
3030
# @option spec [Boolean] :"nil?"
3131
#
32-
# @return [OpenAI::Internal::Type::ArrayOf]
32+
# @return [self]
3333
def self.[](...) = new(...)
3434

3535
# @api public

lib/openai/internal/type/base_model.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@ module Type
66
# @abstract
77
class BaseModel
88
extend OpenAI::Internal::Type::Converter
9+
extend OpenAI::Internal::Util::SorbetRuntimeSupport
910

1011
class << self
1112
# @api private
1213
#
1314
# Assumes superclass fields are totally defined before fields are accessed /
1415
# defined on subclasses.
1516
#
16-
# @return [Hash{Symbol=>Hash{Symbol=>Object}}]
17-
def known_fields
18-
@known_fields ||= (self < OpenAI::Internal::Type::BaseModel ? superclass.known_fields.dup : {})
17+
# @param child [Class<OpenAI::Internal::Type::BaseModel>]
18+
def inherited(child)
19+
super
20+
child.known_fields.replace(known_fields.dup)
1921
end
2022

23+
# @api private
24+
#
25+
# @return [Hash{Symbol=>Hash{Symbol=>Object}}]
26+
def known_fields = @known_fields ||= {}
27+
2128
# @api private
2229
#
2330
# @return [Hash{Symbol=>Hash{Symbol=>Object}}]
@@ -199,7 +206,7 @@ class << self
199206
#
200207
# @option state [Integer] :branched
201208
#
202-
# @return [OpenAI::Internal::Type::BaseModel, Object]
209+
# @return [self, Object]
203210
def coerce(value, state:)
204211
exactness = state.fetch(:exactness)
205212

@@ -258,7 +265,7 @@ def coerce(value, state:)
258265

259266
# @api private
260267
#
261-
# @param value [OpenAI::Internal::Type::BaseModel, Object]
268+
# @param value [self, Object]
262269
#
263270
# @param state [Hash{Symbol=>Object}] .
264271
#
@@ -424,6 +431,10 @@ def to_s = self.class.walk(@data).to_s
424431
#
425432
# @return [String]
426433
def inspect = "#<#{self.class}:0x#{object_id.to_s(16)} #{self}>"
434+
435+
define_sorbet_constant!(:KnownField) do
436+
T.type_alias { {mode: T.nilable(Symbol), required: T::Boolean, nilable: T::Boolean} }
437+
end
427438
end
428439
end
429440
end

lib/openai/internal/type/converter.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Internal
55
module Type
66
# @api private
77
module Converter
8+
extend OpenAI::Internal::Util::SorbetRuntimeSupport
9+
810
# rubocop:disable Lint/UnusedMethodArgument
911

1012
# @api private
@@ -268,6 +270,22 @@ def inspect(target, depth:)
268270
end
269271
end
270272
end
273+
274+
define_sorbet_constant!(:Input) do
275+
T.type_alias { T.any(OpenAI::Internal::Type::Converter, T::Class[T.anything]) }
276+
end
277+
define_sorbet_constant!(:CoerceState) do
278+
T.type_alias do
279+
{
280+
strictness: T.any(T::Boolean, Symbol),
281+
exactness: {yes: Integer, no: Integer, maybe: Integer},
282+
branched: Integer
283+
}
284+
end
285+
end
286+
define_sorbet_constant!(:DumpState) do
287+
T.type_alias { {can_retry: T::Boolean} }
288+
end
271289
end
272290
end
273291
end

lib/openai/internal/type/enum.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module Type
4242
# end
4343
module Enum
4444
include OpenAI::Internal::Type::Converter
45+
include OpenAI::Internal::Util::SorbetRuntimeSupport
4546

4647
# All of the valid Symbol values for this enum.
4748
#

lib/openai/internal/type/hash_of.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class HashOf
2929
#
3030
# @option spec [Boolean] :"nil?"
3131
#
32-
# @return [OpenAI::Internal::Type::HashOf]
32+
# @return [self]
3333
def self.[](...) = new(...)
3434

3535
# @api public

lib/openai/internal/type/union.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Type
3131
# end
3232
module Union
3333
include OpenAI::Internal::Type::Converter
34+
include OpenAI::Internal::Util::SorbetRuntimeSupport
3435

3536
# @api private
3637
#

lib/openai/internal/util.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,62 @@ def decode_sse(lines)
801801
end
802802
end
803803
end
804+
805+
# @api private
806+
module SorbetRuntimeSupport
807+
class MissingSorbetRuntimeError < ::RuntimeError
808+
end
809+
810+
# @api private
811+
#
812+
# @return [Hash{Symbol=>Object}]
813+
private def sorbet_runtime_constants = @sorbet_runtime_constants ||= {}
814+
815+
# @api private
816+
#
817+
# @param name [Symbol]
818+
def const_missing(name)
819+
super unless sorbet_runtime_constants.key?(name)
820+
821+
unless Object.const_defined?(:T)
822+
message = "Trying to access a Sorbet constant #{name.inspect} without `sorbet-runtime`."
823+
raise MissingSorbetRuntimeError.new(message)
824+
end
825+
826+
sorbet_runtime_constants.fetch(name).call
827+
end
828+
829+
# @api private
830+
#
831+
# @param name [Symbol]
832+
# @param blk [Proc]
833+
def define_sorbet_constant!(name, &blk) = sorbet_runtime_constants.store(name, blk)
834+
end
835+
836+
extend OpenAI::Internal::Util::SorbetRuntimeSupport
837+
838+
define_sorbet_constant!(:ParsedUri) do
839+
T.type_alias do
840+
{
841+
scheme: T.nilable(String),
842+
host: T.nilable(String),
843+
port: T.nilable(Integer),
844+
path: T.nilable(String),
845+
query: T::Hash[String, T::Array[String]]
846+
}
847+
end
848+
end
849+
850+
define_sorbet_constant!(:ServerSentEvent) do
851+
T.type_alias do
852+
{
853+
event: T.nilable(String),
854+
data: T.nilable(String),
855+
id: T.nilable(String),
856+
retry: T.nilable(Integer)
857+
}
858+
end
859+
end
804860
end
805861
end
806862
end

0 commit comments

Comments
 (0)