thinkthen 0.1.1-aarch64-linux → 0.2.0-aarch64-linux
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.
- checksums.yaml +4 -4
- data/API.md +40 -0
- data/LICENSE +21 -0
- data/README.md +562 -0
- data/THIRD-PARTY-NOTICES/anstream-1.0.0/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstream-1.0.0/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-1.0.14/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-1.0.14/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-parse-1.0.0/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-parse-1.0.0/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-query-1.1.5/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-query-1.1.5/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/base64-0.23.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/base64-0.23.1/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/bitflags-2.13.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/bitflags-2.13.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/block-buffer-0.10.4/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/block-buffer-0.10.4/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/bytes-1.12.1/LICENSE +25 -0
- data/THIRD-PARTY-NOTICES/cfg-if-1.0.5/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/cfg-if-1.0.5/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/clap-4.6.7/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap-4.6.7/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_builder-4.6.7/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_builder-4.6.7/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_complete-4.6.11/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_complete-4.6.11/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_lex-1.1.1/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_lex-1.1.1/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_mangen-0.2.33/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_mangen-0.2.33/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/colorchoice-1.0.5/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/colorchoice-1.0.5/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/command-group-5.0.1/COPYRIGHT +29 -0
- data/THIRD-PARTY-NOTICES/command-group-5.0.1/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/command-group-5.0.1/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/cpufeatures-0.2.17/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/cpufeatures-0.2.17/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/crc32fast-1.5.3/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/crc32fast-1.5.3/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/crypto-common-0.1.7/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/crypto-common-0.1.7/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/ctrlc-3.5.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/ctrlc-3.5.2/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/digest-0.10.7/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/digest-0.10.7/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/dyn-clone-1.0.20/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/dyn-clone-1.0.20/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/equivalent-1.0.3/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/equivalent-1.0.3/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/fastrand-2.5.0/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/fastrand-2.5.0/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/flate2-1.1.10/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/flate2-1.1.10/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/generic-array-0.14.7/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.2.17/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.2.17/LICENSE-MIT +26 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.3.4/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.3.4/LICENSE-MIT +26 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.4.3/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.4.3/LICENSE-MIT +26 -0
- data/THIRD-PARTY-NOTICES/hashbrown-0.17.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/hashbrown-0.17.1/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/http-1.5.0/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/http-1.5.0/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/httparse-1.10.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/httparse-1.10.1/LICENSE-MIT +20 -0
- data/THIRD-PARTY-NOTICES/httpdate-1.0.3/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/httpdate-1.0.3/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/index.json +633 -0
- data/THIRD-PARTY-NOTICES/indexmap-2.14.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/indexmap-2.14.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/is_terminal_polyfill-1.70.2/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/is_terminal_polyfill-1.70.2/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/itoa-1.0.18/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/itoa-1.0.18/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/libc-0.2.190/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/libc-0.2.190/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/linux-raw-sys-0.12.1/COPYRIGHT +29 -0
- data/THIRD-PARTY-NOTICES/linux-raw-sys-0.12.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/linux-raw-sys-0.12.1/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- data/THIRD-PARTY-NOTICES/linux-raw-sys-0.12.1/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/log-0.4.34/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/log-0.4.34/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/memchr-2.8.3/COPYING +3 -0
- data/THIRD-PARTY-NOTICES/memchr-2.8.3/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/nix-0.27.1/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/nix-0.31.3/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/once_cell-1.21.4/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/once_cell-1.21.4/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/openssl-probe-0.2.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/openssl-probe-0.2.1/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/percent-encoding-2.3.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/percent-encoding-2.3.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/quick-xml-0.41.0/LICENSE-MIT.md +23 -0
- data/THIRD-PARTY-NOTICES/ref-cast-1.0.27/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/ref-cast-1.0.27/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/LICENSE +9 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/LICENSE-BoringSSL +272 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/LICENSE-other-bits +13 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/src/polyfill/once_cell/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/src/polyfill/once_cell/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/third_party/fiat/LICENSE +15 -0
- data/THIRD-PARTY-NOTICES/roff-1.1.1/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/roff-1.1.1/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/rustix-1.1.5/COPYRIGHT +29 -0
- data/THIRD-PARTY-NOTICES/rustix-1.1.5/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustix-1.1.5/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- data/THIRD-PARTY-NOTICES/rustix-1.1.5/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/rustls-0.23.46/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-0.23.46/LICENSE-ISC +15 -0
- data/THIRD-PARTY-NOTICES/rustls-0.23.46/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/rustls-native-certs-0.8.5/LICENSE +10 -0
- data/THIRD-PARTY-NOTICES/rustls-native-certs-0.8.5/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-native-certs-0.8.5/LICENSE-ISC +15 -0
- data/THIRD-PARTY-NOTICES/rustls-native-certs-0.8.5/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/rustls-pki-types-1.15.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-pki-types-1.15.1/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/rustls-platform-verifier-0.7.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-platform-verifier-0.7.1/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/rustls-webpki-0.103.16/LICENSE +19 -0
- data/THIRD-PARTY-NOTICES/schemars-1.2.3/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/serde-1.0.229/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/serde-1.0.229/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/serde_core-1.0.229/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/serde_core-1.0.229/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/serde_json-1.0.151/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/serde_json-1.0.151/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/sha2-0.10.9/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/sha2-0.10.9/LICENSE-MIT +27 -0
- data/THIRD-PARTY-NOTICES/strsim-0.11.1/LICENSE +23 -0
- data/THIRD-PARTY-NOTICES/subtle-2.6.1/LICENSE +29 -0
- data/THIRD-PARTY-NOTICES/tempfile-3.23.0/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/tempfile-3.23.0/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/thiserror-2.0.21/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/thiserror-2.0.21/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/tinyvec-1.13.3/LICENSE-APACHE.md +202 -0
- data/THIRD-PARTY-NOTICES/tinyvec-1.13.3/LICENSE-MIT.md +5 -0
- data/THIRD-PARTY-NOTICES/tinyvec-1.13.3/LICENSE-ZLIB.md +11 -0
- data/THIRD-PARTY-NOTICES/typed-path-0.13.0/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/typed-path-0.13.0/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/typenum-1.20.1/LICENSE +1 -0
- data/THIRD-PARTY-NOTICES/typenum-1.20.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/typenum-1.20.1/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/unicode-general-category-1.1.0/LICENSE +201 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/COPYRIGHT +7 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/LICENSE-UNICODE +39 -0
- data/THIRD-PARTY-NOTICES/untrusted-0.9.0/LICENSE.txt +13 -0
- data/THIRD-PARTY-NOTICES/ureq-3.4.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/ureq-3.4.2/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/ureq-proto-0.6.4/LICENSE-APACHE.txt +202 -0
- data/THIRD-PARTY-NOTICES/ureq-proto-0.6.4/LICENSE-MIT.txt +7 -0
- data/THIRD-PARTY-NOTICES/utf8-zero-0.8.1/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/utf8-zero-0.8.1/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/utf8parse-0.2.2/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/utf8parse-0.2.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/zeroize-1.9.0/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/zeroize-1.9.0/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/zip-9.1.2/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/zlib-rs-0.6.8/LICENSE +19 -0
- data/THIRD-PARTY-NOTICES/zmij-1.0.23/LICENSE-MIT +23 -0
- data/lib/thinkthen/call_owner.rb +68 -0
- data/lib/thinkthen/client.rb +293 -0
- data/lib/thinkthen/configuration.rb +76 -0
- data/lib/thinkthen/contract.rb +89 -0
- data/lib/thinkthen/libthinkthen_ffi.so +0 -0
- data/lib/thinkthen/native.rb +350 -0
- data/lib/thinkthen/native_allocator.rb +35 -0
- data/lib/thinkthen/native_reader.rb +87 -0
- data/lib/thinkthen/questions.rb +184 -0
- data/lib/thinkthen/row.rb +173 -0
- data/lib/thinkthen/rows.rb +199 -0
- data/lib/thinkthen/values.rb +111 -0
- data/lib/thinkthen/version.rb +3 -1
- data/lib/thinkthen.rb +79 -699
- data/sig/error_codes.rbs +3 -0
- data/sig/internal.rbs +277 -0
- data/sig/thinkthen.rbs +440 -0
- metadata +207 -13
- data/lib/thinkthen/thinkthen.so +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
class RedactedKey
|
|
5
|
+
def initialize(value)
|
|
6
|
+
raise UsageError.new("api_key must be a String", code: :"usage.settings") unless value.is_a?(String)
|
|
7
|
+
ThinkThen.send(:native)
|
|
8
|
+
@settings = Native.settings
|
|
9
|
+
Native.bytes(value) do |bytes, length|
|
|
10
|
+
Native.checked { |error| Native.thinkthen_settings_set_api_key(@settings, bytes, length, error) }
|
|
11
|
+
end
|
|
12
|
+
freeze
|
|
13
|
+
end
|
|
14
|
+
def inspect = "#<ThinkThen::RedactedKey [REDACTED]>"
|
|
15
|
+
alias to_s inspect
|
|
16
|
+
def copy_to(settings)
|
|
17
|
+
Native.checked { |error| Native.thinkthen_settings_copy_api_key(settings, @settings, error) }
|
|
18
|
+
end
|
|
19
|
+
private :copy_to
|
|
20
|
+
end
|
|
21
|
+
class Configuration
|
|
22
|
+
attr_reader :api_key
|
|
23
|
+
attr_accessor :backend, :model, :url, :batch, :max_requests, :retry
|
|
24
|
+
def initialize
|
|
25
|
+
@backend = @model = @url = @batch = @max_requests = @retry = nil
|
|
26
|
+
end
|
|
27
|
+
def api_key=(value)
|
|
28
|
+
@api_key = value.nil? || value.is_a?(RedactedKey) ? value : RedactedKey.new(value)
|
|
29
|
+
end
|
|
30
|
+
def inspect = "#<ThinkThen::Configuration>"
|
|
31
|
+
alias to_s inspect
|
|
32
|
+
def to_h
|
|
33
|
+
{backend: backend, model: model, url: url, api_key: api_key, batch: batch, max_requests: max_requests, retry: self.retry}
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
@configuration_mutex = Mutex.new
|
|
37
|
+
@configuration = Configuration.new
|
|
38
|
+
class << self
|
|
39
|
+
def configure
|
|
40
|
+
@configuration_mutex.synchronize do
|
|
41
|
+
raise IOError, "shared client is already configured" if @shared_client
|
|
42
|
+
candidate = @configuration.dup
|
|
43
|
+
yield candidate
|
|
44
|
+
@configuration = candidate.dup
|
|
45
|
+
@configuration.model = Values.freeze_copy(candidate.model)
|
|
46
|
+
@configuration.url = Values.freeze_copy(candidate.url)
|
|
47
|
+
end
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
def client
|
|
51
|
+
@configuration_mutex.synchronize { @shared_client ||= Client.new(**@configuration.to_h) }
|
|
52
|
+
end
|
|
53
|
+
def close
|
|
54
|
+
@configuration_mutex.synchronize { @shared_client&.close; @shared_client = nil }
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
def decide(question, record, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET) = client.public_send(:decide, question, record, threshold: threshold, field: field, name: name, cancel: cancel, yes_means: yes_means, no_means: no_means)
|
|
58
|
+
def decide_each(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET, &block) = client.public_send(:decide_each, question, records, threshold: threshold, field: field, name: name, cancel: cancel, yes_means: yes_means, no_means: no_means, &block)
|
|
59
|
+
def choose(question, record, choices: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET) = client.public_send(:choose, question, record, choices: choices, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions)
|
|
60
|
+
def choose_each(question, records, choices: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block) = client.public_send(:choose_each, question, records, choices: choices, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions, &block)
|
|
61
|
+
def tag(question, record, labels: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET) = client.public_send(:tag, question, record, labels: labels, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions)
|
|
62
|
+
def tag_each(question, records, labels: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block) = client.public_send(:tag_each, question, records, labels: labels, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions, &block)
|
|
63
|
+
def score(question, record, levels: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET) = client.public_send(:score, question, record, levels: levels, field: field, name: name, cancel: cancel, descriptions: descriptions)
|
|
64
|
+
def score_each(question, records, levels: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block) = client.public_send(:score_each, question, records, levels: levels, field: field, name: name, cancel: cancel, descriptions: descriptions, &block)
|
|
65
|
+
def filter(question, records, **options, &block) = client.public_send(:filter, question, records, **options, &block)
|
|
66
|
+
def rank(question, records, **options, &block) = client.public_send(:rank, question, records, **options, &block)
|
|
67
|
+
def find(question, record, threshold: UNSET, field: UNSET, name: nil, cancel: nil) = client.public_send(:find, question, record, threshold: threshold, field: field, name: name, cancel: cancel)
|
|
68
|
+
def find_each(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, &block) = client.public_send(:find_each, question, records, threshold: threshold, field: field, name: name, cancel: cancel, &block)
|
|
69
|
+
def recognize(record, **options) = client.public_send(:recognize, record, **options)
|
|
70
|
+
def recognize_each(records, **options, &block) = client.public_send(:recognize_each, records, **options, &block)
|
|
71
|
+
def relate(record, **options) = client.public_send(:relate, record, **options)
|
|
72
|
+
def relate_each(records, **options, &block) = client.public_send(:relate_each, records, **options, &block)
|
|
73
|
+
def annotate(question, record, field: UNSET, name: nil, cancel: nil) = client.public_send(:annotate, question, record, field: field, name: name, cancel: cancel)
|
|
74
|
+
def annotate_each(question, records, field: UNSET, name: nil, cancel: nil, &block) = client.public_send(:annotate_each, question, records, field: field, name: name, cancel: cancel, &block)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# Generated native requirement. Do not edit.
|
|
3
|
+
module ThinkThen
|
|
4
|
+
module NativeContract
|
|
5
|
+
ABI_VERSION = 327681
|
|
6
|
+
ABI_CODE = "usage.abi"
|
|
7
|
+
ABI_MESSAGE = "the native ABI is incompatible"
|
|
8
|
+
LIBRARY_VARIABLE = "THINKTHEN_LIBRARY"
|
|
9
|
+
LIBRARY_PATH_MESSAGE = "THINKTHEN_LIBRARY must be an absolute path"
|
|
10
|
+
FUNCTION_NAMES = {
|
|
11
|
+
1 => "decide",
|
|
12
|
+
2 => "choose",
|
|
13
|
+
3 => "tag",
|
|
14
|
+
4 => "score",
|
|
15
|
+
7 => "find",
|
|
16
|
+
8 => "recognize",
|
|
17
|
+
9 => "relate",
|
|
18
|
+
5 => "filter",
|
|
19
|
+
6 => "rank",
|
|
20
|
+
10 => "annotate",
|
|
21
|
+
}.freeze
|
|
22
|
+
STATUS_NAMES = {
|
|
23
|
+
1 => "answered",
|
|
24
|
+
2 => "unsure",
|
|
25
|
+
3 => "failed",
|
|
26
|
+
}.freeze
|
|
27
|
+
BACKEND_NAMES = {
|
|
28
|
+
1 => "systemone",
|
|
29
|
+
2 => "openai",
|
|
30
|
+
3 => "stub",
|
|
31
|
+
}.freeze
|
|
32
|
+
ID_NAMES = {
|
|
33
|
+
0 => "absent",
|
|
34
|
+
1 => "string",
|
|
35
|
+
2 => "signed",
|
|
36
|
+
3 => "unsigned",
|
|
37
|
+
}.freeze
|
|
38
|
+
RECORD_NAMES = {
|
|
39
|
+
0 => "text",
|
|
40
|
+
1 => "json",
|
|
41
|
+
2 => "missing",
|
|
42
|
+
3 => "invalid",
|
|
43
|
+
}.freeze
|
|
44
|
+
OFFSET_NAMES = {
|
|
45
|
+
0 => "utf8",
|
|
46
|
+
1 => "utf16",
|
|
47
|
+
2 => "code_points",
|
|
48
|
+
3 => "code_points_inclusive",
|
|
49
|
+
}.freeze
|
|
50
|
+
CODE_NAMES = {
|
|
51
|
+
1001 => "usage.threshold",
|
|
52
|
+
1002 => "usage.question",
|
|
53
|
+
1003 => "usage.name",
|
|
54
|
+
1004 => "usage.settings",
|
|
55
|
+
1005 => "usage.backend",
|
|
56
|
+
1006 => "usage.url",
|
|
57
|
+
1007 => "usage.key-missing",
|
|
58
|
+
1008 => "usage.batch",
|
|
59
|
+
1009 => "usage.pointer",
|
|
60
|
+
1010 => "usage.sql-policy",
|
|
61
|
+
1011 => "usage.sql-selection",
|
|
62
|
+
1012 => "usage.abi",
|
|
63
|
+
1013 => "usage.closed",
|
|
64
|
+
1014 => "usage.function",
|
|
65
|
+
1015 => "usage.record-kind",
|
|
66
|
+
1016 => "usage.offsets",
|
|
67
|
+
1017 => "usage.argument",
|
|
68
|
+
1018 => "usage.option",
|
|
69
|
+
1019 => "usage.column",
|
|
70
|
+
1020 => "usage.sheet",
|
|
71
|
+
1021 => "usage.output",
|
|
72
|
+
1022 => "usage.table",
|
|
73
|
+
2001 => "record.json",
|
|
74
|
+
2002 => "record.missing",
|
|
75
|
+
2003 => "record.too-large",
|
|
76
|
+
2004 => "record.invalid",
|
|
77
|
+
2005 => "record.empty",
|
|
78
|
+
3001 => "backend.transport",
|
|
79
|
+
3002 => "backend.status",
|
|
80
|
+
3003 => "backend.reply",
|
|
81
|
+
4001 => "local.cancelled",
|
|
82
|
+
4002 => "local.io",
|
|
83
|
+
4003 => "local.toolbox",
|
|
84
|
+
4004 => "local.accounting",
|
|
85
|
+
4005 => "local.request-limit",
|
|
86
|
+
5001 => "internal.bug",
|
|
87
|
+
}.freeze
|
|
88
|
+
end
|
|
89
|
+
end
|
|
Binary file
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "contract"
|
|
4
|
+
require_relative "native_allocator"
|
|
5
|
+
|
|
6
|
+
module ThinkThen
|
|
7
|
+
module Native
|
|
8
|
+
extend FFI::Library
|
|
9
|
+
|
|
10
|
+
library = if FFI::Platform.windows?
|
|
11
|
+
"thinkthen_ffi.dll"
|
|
12
|
+
elsif FFI::Platform.mac?
|
|
13
|
+
"libthinkthen_ffi.dylib"
|
|
14
|
+
else
|
|
15
|
+
"libthinkthen_ffi.so"
|
|
16
|
+
end
|
|
17
|
+
chosen = ENV.fetch(NativeContract::LIBRARY_VARIABLE, "")
|
|
18
|
+
unless chosen.empty? || File.absolute_path?(chosen)
|
|
19
|
+
raise SetupError.new(NativeContract::LIBRARY_PATH_MESSAGE, code: :"local.io")
|
|
20
|
+
end
|
|
21
|
+
path = chosen.empty? ? File.expand_path(library, __dir__ || ".") : chosen
|
|
22
|
+
begin
|
|
23
|
+
ffi_lib path
|
|
24
|
+
rescue LoadError => error
|
|
25
|
+
raise SetupError.new("cannot load native engine #{path}: #{error.message}", code: :"local.io")
|
|
26
|
+
end
|
|
27
|
+
begin
|
|
28
|
+
attach_function :thinkthen_abi_version, [], :uint32
|
|
29
|
+
attach_function :thinkthen_abi_compatible, [:uint32, :uint32], :uint8
|
|
30
|
+
attach_function :thinkthen_code_name, [:int32], :string
|
|
31
|
+
attach_function :thinkthen_code_family, [:int32], :string
|
|
32
|
+
actual = thinkthen_abi_version
|
|
33
|
+
required = NativeContract::ABI_VERSION
|
|
34
|
+
unless thinkthen_abi_compatible(required, actual) != 0
|
|
35
|
+
raise UsageError.new(NativeContract::ABI_MESSAGE, code: NativeContract::ABI_CODE)
|
|
36
|
+
end
|
|
37
|
+
rescue FFI::NotFoundError
|
|
38
|
+
raise UsageError.new(NativeContract::ABI_MESSAGE, code: NativeContract::ABI_CODE)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
class Buffer < FFI::Struct
|
|
42
|
+
layout :data, :pointer, :len, :size_t
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class RecordInput < FFI::Struct
|
|
46
|
+
layout :struct_size, :uint32, :kind, :uint32, :data, :pointer, :len, :size_t
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class NativeError < FFI::Struct
|
|
50
|
+
layout :struct_size, :uint32, :code, :int32, :http_status, :uint16, :message, Buffer
|
|
51
|
+
def initialize
|
|
52
|
+
super
|
|
53
|
+
self[:struct_size] = self.class.size
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class Row < FFI::Struct
|
|
58
|
+
layout :struct_size, :uint32, :function, :uint32, :status, :uint32,
|
|
59
|
+
:position, :uint64, :probability, :double, :error_code, :int32,
|
|
60
|
+
:private_data, :pointer
|
|
61
|
+
def initialize
|
|
62
|
+
super
|
|
63
|
+
self[:struct_size] = self.class.size
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
begin
|
|
68
|
+
attach_function :thinkthen_toolbox_from_json, [:pointer, :size_t, :pointer, :pointer], :int32
|
|
69
|
+
attach_function :thinkthen_toolbox_from_definitions_json, [:pointer, :size_t, :pointer, :pointer], :int32
|
|
70
|
+
attach_function :thinkthen_toolbox_to_json, [:pointer, :pointer, :pointer], :int32
|
|
71
|
+
attach_function :thinkthen_toolbox_definitions_json, [:pointer, :pointer, :pointer], :int32
|
|
72
|
+
attach_function :thinkthen_toolbox_select_as, [:pointer, :pointer, :size_t, :int32, :pointer, :pointer], :int32
|
|
73
|
+
attach_function :thinkthen_toolbox_free, [:pointer], :void
|
|
74
|
+
attach_function :thinkthen_buffer_free, [:pointer], :void
|
|
75
|
+
attach_function :thinkthen_question_from_json, [:pointer, :size_t, :pointer, :pointer], :int32
|
|
76
|
+
attach_function :thinkthen_question_new, [:uint32, :pointer, :size_t, :pointer, :pointer], :int32
|
|
77
|
+
attach_function :thinkthen_question_set_cut, [:pointer, :double, :pointer], :int32
|
|
78
|
+
attach_function :thinkthen_question_set_band, [:pointer, :double, :double, :pointer], :int32
|
|
79
|
+
attach_function :thinkthen_question_free, [:pointer], :void
|
|
80
|
+
attach_function :thinkthen_settings_new, [:pointer, :pointer], :int32
|
|
81
|
+
attach_function :thinkthen_settings_set_api_key, [:pointer, :pointer, :size_t, :pointer], :int32
|
|
82
|
+
attach_function :thinkthen_settings_from_json, [:pointer, :size_t, :pointer, :pointer], :int32
|
|
83
|
+
attach_function :thinkthen_settings_copy_api_key, [:pointer, :pointer, :pointer], :int32
|
|
84
|
+
attach_function :thinkthen_settings_free, [:pointer], :void
|
|
85
|
+
attach_function :thinkthen_client_open, [:pointer, :pointer, :pointer], :int32, blocking: true
|
|
86
|
+
attach_function :thinkthen_client_open_json, [:pointer, :size_t, :pointer, :pointer], :int32, blocking: true
|
|
87
|
+
attach_function :thinkthen_settings_conversion_error, [:pointer], :int32
|
|
88
|
+
attach_function :thinkthen_question_conversion_error, [:pointer], :int32
|
|
89
|
+
attach_function :thinkthen_cancelled_error, [:pointer], :int32
|
|
90
|
+
attach_function :thinkthen_call_send_invalid, [:pointer, :pointer], :int32, blocking: true
|
|
91
|
+
attach_function :thinkthen_client_usage, [:pointer, :pointer, :pointer], :int32
|
|
92
|
+
attach_function :thinkthen_client_usage_settled, [:pointer, :pointer, :pointer], :int32, blocking: true
|
|
93
|
+
attach_function :thinkthen_client_free, [:pointer], :void
|
|
94
|
+
attach_function :thinkthen_call_open_records_json,
|
|
95
|
+
[:pointer, :pointer, :size_t, :pointer, :size_t, :size_t, :uint32, :pointer, :pointer], :int32, blocking: true
|
|
96
|
+
attach_function :thinkthen_call_open_json,
|
|
97
|
+
[:pointer, :pointer, :size_t, :uint32, :pointer, :pointer], :int32, blocking: true
|
|
98
|
+
attach_function :thinkthen_call_send_text, [:pointer, :pointer, :size_t, :pointer], :int32, blocking: true
|
|
99
|
+
attach_function :thinkthen_call_send_json, [:pointer, :pointer, :size_t, :pointer], :int32, blocking: true
|
|
100
|
+
attach_function :thinkthen_call_send_missing, [:pointer, :pointer], :int32, blocking: true
|
|
101
|
+
attach_function :thinkthen_call_end_input, [:pointer], :void
|
|
102
|
+
attach_function :thinkthen_call_next, [:pointer, :pointer, :pointer], :int32, blocking: true
|
|
103
|
+
attach_function :thinkthen_call_cancel, [:pointer], :void
|
|
104
|
+
attach_function :thinkthen_call_free, [:pointer], :void
|
|
105
|
+
attach_function :thinkthen_error_free, [:pointer], :void
|
|
106
|
+
attach_function :thinkthen_row_free, [:pointer], :void
|
|
107
|
+
attach_function :thinkthen_row_to_json, [:pointer, :pointer, :pointer], :int32
|
|
108
|
+
rescue FFI::NotFoundError
|
|
109
|
+
raise UsageError.new(NativeContract::ABI_MESSAGE, code: NativeContract::ABI_CODE)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
class SettingsPointer < FFI::AutoPointer
|
|
113
|
+
def self.release(pointer) = Native.thinkthen_settings_free(pointer)
|
|
114
|
+
end
|
|
115
|
+
def self.settings
|
|
116
|
+
output = FFI::MemoryPointer.new(:pointer)
|
|
117
|
+
checked { |error| thinkthen_settings_new(output, error) }
|
|
118
|
+
SettingsPointer.new(output.read_pointer)
|
|
119
|
+
end
|
|
120
|
+
class ClientPointer < FFI::AutoPointer
|
|
121
|
+
def self.release(pointer)
|
|
122
|
+
Native.thinkthen_client_free(pointer)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
class CallPointer < FFI::AutoPointer
|
|
127
|
+
def self.release(pointer)
|
|
128
|
+
Native.thinkthen_call_free(pointer)
|
|
129
|
+
NativeAllocator.trim
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def self.validate_threshold(function, text, threshold)
|
|
134
|
+
return if threshold.nil?
|
|
135
|
+
output = FFI::MemoryPointer.new(:pointer)
|
|
136
|
+
bytes(text || "") do |data, length|
|
|
137
|
+
checked { |error| thinkthen_question_new(NativeContract::FUNCTION_NAMES.key(function.to_s), data, length, output, error) }
|
|
138
|
+
end
|
|
139
|
+
if threshold.is_a?(Hash)
|
|
140
|
+
checked { |error| thinkthen_question_set_band(output.read_pointer, threshold.fetch("low"), threshold.fetch("high"), error) }
|
|
141
|
+
else
|
|
142
|
+
checked { |error| thinkthen_question_set_cut(output.read_pointer, threshold, error) }
|
|
143
|
+
end
|
|
144
|
+
ensure
|
|
145
|
+
thinkthen_question_free(output.read_pointer) if output && !output.read_pointer.null?
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def self.validate_question(value)
|
|
149
|
+
question = FFI::MemoryPointer.new(:pointer)
|
|
150
|
+
bytes(context_dump(value, :question)) do |data, length|
|
|
151
|
+
checked { |error| thinkthen_question_from_json(data, length, question, error) }
|
|
152
|
+
end
|
|
153
|
+
ensure
|
|
154
|
+
thinkthen_question_free(question.read_pointer) if question && !question.read_pointer.null?
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def self.toolbox_json(json, saved: false, definitions: false, select: nil)
|
|
158
|
+
output = FFI::MemoryPointer.new(:pointer)
|
|
159
|
+
result = Buffer.new
|
|
160
|
+
selected = FFI::MemoryPointer.new(:pointer) if select
|
|
161
|
+
bytes(json) do |data, length|
|
|
162
|
+
checked do |error|
|
|
163
|
+
if saved
|
|
164
|
+
thinkthen_toolbox_from_json(data, length, output, error)
|
|
165
|
+
else
|
|
166
|
+
thinkthen_toolbox_from_definitions_json(data, length, output, error)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
if select
|
|
171
|
+
name, function = select
|
|
172
|
+
bytes(name) do |data, length|
|
|
173
|
+
checked { |error| thinkthen_toolbox_select_as(output.read_pointer, data, length, NativeContract::FUNCTION_NAMES.key(function), selected, error) }
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
checked do |error|
|
|
177
|
+
if definitions
|
|
178
|
+
thinkthen_toolbox_definitions_json(output.read_pointer, result, error)
|
|
179
|
+
else
|
|
180
|
+
thinkthen_toolbox_to_json(output.read_pointer, result, error)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
copy(result)
|
|
184
|
+
ensure
|
|
185
|
+
thinkthen_buffer_free(result) if result
|
|
186
|
+
thinkthen_question_free(selected.read_pointer) if selected && !selected.read_pointer.null?
|
|
187
|
+
thinkthen_toolbox_free(output.read_pointer) if output && !output.read_pointer.null?
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def self.dump(value, symbols: false)
|
|
191
|
+
JSON.generate(snapshot(value, {}, symbols), max_nesting: false)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def self.snapshot(value, seen = {}, symbols = false)
|
|
195
|
+
case value
|
|
196
|
+
when String then String.new(value)
|
|
197
|
+
when Integer, Float, NilClass, TrueClass, FalseClass then value
|
|
198
|
+
when Symbol
|
|
199
|
+
raise TypeError, "expected a JSON value" unless symbols
|
|
200
|
+
value.to_s
|
|
201
|
+
when Array, Hash
|
|
202
|
+
raise ArgumentError, "cyclic JSON value" if seen[value.object_id]
|
|
203
|
+
seen[value.object_id] = true
|
|
204
|
+
begin
|
|
205
|
+
if value.is_a?(Array)
|
|
206
|
+
Array.new(value).map { |entry| snapshot(entry, seen, symbols) }
|
|
207
|
+
else
|
|
208
|
+
members = Hash.new #: Hash[String, untyped]
|
|
209
|
+
value.to_a.each_with_object(members) do |(key, entry), output|
|
|
210
|
+
raise TypeError, "JSON keys must be strings" unless key.is_a?(String) || (symbols && key.is_a?(Symbol))
|
|
211
|
+
name = key.to_s
|
|
212
|
+
raise ArgumentError, "String and Symbol keys name the same JSON member" if output.key?(name)
|
|
213
|
+
output[name] = snapshot(entry, seen, symbols)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
ensure
|
|
217
|
+
seen.delete(value.object_id)
|
|
218
|
+
end
|
|
219
|
+
else
|
|
220
|
+
raise TypeError, "expected a JSON value or an object with to_h" unless value.respond_to?(:to_h)
|
|
221
|
+
raise ArgumentError, "cyclic JSON value" if seen[value.object_id]
|
|
222
|
+
seen[value.object_id] = true
|
|
223
|
+
begin
|
|
224
|
+
converted = value.to_h
|
|
225
|
+
raise TypeError, "to_h must return a Hash" unless converted.is_a?(Hash)
|
|
226
|
+
snapshot(converted, seen, symbols)
|
|
227
|
+
ensure
|
|
228
|
+
seen.delete(value.object_id)
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def self.context_dump(value, context)
|
|
234
|
+
dump(value, symbols: true).encode(Encoding::UTF_8)
|
|
235
|
+
rescue StandardError, SystemStackError
|
|
236
|
+
checked { |error| public_send("thinkthen_#{context}_conversion_error", error) }
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Fill invalid with each unconvertible record's one-based position and reason.
|
|
240
|
+
def self.records(values)
|
|
241
|
+
values = Array.new(values)
|
|
242
|
+
memory = FFI::MemoryPointer.new(RecordInput.size, [values.length, 1].max)
|
|
243
|
+
# @type var buffers: Array[untyped]
|
|
244
|
+
buffers = Array.new
|
|
245
|
+
values.each_with_index do |value, index|
|
|
246
|
+
record = RecordInput.new(memory + index * RecordInput.size)
|
|
247
|
+
record[:struct_size] = RecordInput.size
|
|
248
|
+
kind, bytes = record_value(value)
|
|
249
|
+
record[:kind] = NativeContract::RECORD_NAMES.key(kind)
|
|
250
|
+
buffer = bytes && FFI::MemoryPointer.from_string(bytes)
|
|
251
|
+
record[:data] = buffer || FFI::Pointer::NULL
|
|
252
|
+
record[:len] = bytes ? bytes.bytesize : 0
|
|
253
|
+
buffers << buffer
|
|
254
|
+
end
|
|
255
|
+
yield memory, values.length
|
|
256
|
+
ensure
|
|
257
|
+
buffers&.each { |buffer| buffer&.free }
|
|
258
|
+
memory&.free
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# Return the record kind and bytes, or "invalid" with the class and reason it has no JSON form.
|
|
262
|
+
def self.record_value(value)
|
|
263
|
+
return ["missing", nil] if value.nil?
|
|
264
|
+
bytes = value.is_a?(String) ? value.encode(Encoding::UTF_8) : dump(value, symbols: true).encode(Encoding::UTF_8)
|
|
265
|
+
[value.is_a?(String) ? "text" : "json", bytes]
|
|
266
|
+
rescue StandardError, SystemStackError
|
|
267
|
+
["invalid", nil]
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def self.parse(value)
|
|
271
|
+
JSON.parse(value, max_nesting: false)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# Copy the engine's canonical saved-row form before the native row is freed.
|
|
275
|
+
def self.row_json(row)
|
|
276
|
+
output = Buffer.new
|
|
277
|
+
checked { |error| thinkthen_row_to_json(row, output, error) }
|
|
278
|
+
copy(output)
|
|
279
|
+
ensure
|
|
280
|
+
thinkthen_buffer_free(output) if output
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def self.usage(pointer, settled: false)
|
|
284
|
+
output = Buffer.new
|
|
285
|
+
checked do |error|
|
|
286
|
+
settled ? thinkthen_client_usage_settled(pointer, output, error) : thinkthen_client_usage(pointer, output, error)
|
|
287
|
+
end
|
|
288
|
+
fields = JSON.parse(copy(output), symbolize_names: true)
|
|
289
|
+
input = fields.fetch(:input_tokens)
|
|
290
|
+
output_tokens = fields.fetch(:output_tokens)
|
|
291
|
+
Usage.new(attempts: fields.fetch(:attempts), responses: fields.fetch(:responses),
|
|
292
|
+
status_retries: fields.fetch(:status_retries), connection_retries: fields.fetch(:connection_retries),
|
|
293
|
+
request_body_bytes: fields.fetch(:request_body_bytes), active_attempts: fields.fetch(:active_attempts),
|
|
294
|
+
active_calls: fields.fetch(:active_calls),
|
|
295
|
+
input_tokens: TokenUsage.new(reported: input.fetch(:reported), unknown_attempts: input.fetch(:unknown_attempts)),
|
|
296
|
+
output_tokens: TokenUsage.new(reported: output_tokens.fetch(:reported), unknown_attempts: output_tokens.fetch(:unknown_attempts)))
|
|
297
|
+
ensure
|
|
298
|
+
thinkthen_buffer_free(output) if output
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def self.copy(buffer)
|
|
302
|
+
buffer[:data].read_string_length(buffer[:len]).force_encoding(Encoding::UTF_8)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def self.error_symbol(value)
|
|
306
|
+
unless value == "unknown" || NativeContract::CODE_NAMES.value?(value)
|
|
307
|
+
raise UsageError.new(NativeContract::ABI_MESSAGE, code: NativeContract::ABI_CODE)
|
|
308
|
+
end
|
|
309
|
+
# The generated registry guard supplies the literal union at this boundary.
|
|
310
|
+
# @type var result: untyped
|
|
311
|
+
result = value.to_sym
|
|
312
|
+
result
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def self.error_class(value, code)
|
|
316
|
+
family = thinkthen_code_family(value)
|
|
317
|
+
klass = {"usage" => UsageError, "backend" => SetupError, "internal" => InternalError,
|
|
318
|
+
"record" => FailedError, "local" => SetupError}.fetch(family) do
|
|
319
|
+
raise UsageError.new(NativeContract::ABI_MESSAGE, code: NativeContract::ABI_CODE)
|
|
320
|
+
end
|
|
321
|
+
return CancelledError if code == "local.cancelled"
|
|
322
|
+
code == "usage.key-missing" ? SetupError : klass
|
|
323
|
+
end
|
|
324
|
+
private_class_method :error_class
|
|
325
|
+
|
|
326
|
+
def self.checked
|
|
327
|
+
error = NativeError.new
|
|
328
|
+
result = yield error
|
|
329
|
+
if result < 0
|
|
330
|
+
code = thinkthen_code_name(error[:code])
|
|
331
|
+
unless code && NativeContract::CODE_NAMES.key?(error[:code])
|
|
332
|
+
raise UnknownCodeError.new(copy(error[:message]), raw_code: error[:code], raw_name: code, http_status: error[:http_status].zero? ? nil : error[:http_status])
|
|
333
|
+
end
|
|
334
|
+
klass = error_class(error[:code], code)
|
|
335
|
+
raise klass.new(copy(error[:message]), code: error_symbol(code), http_status: error[:http_status].zero? ? nil : error[:http_status])
|
|
336
|
+
end
|
|
337
|
+
result
|
|
338
|
+
ensure
|
|
339
|
+
thinkthen_error_free(error) if error
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def self.bytes(value)
|
|
343
|
+
text = value.encode(Encoding::UTF_8)
|
|
344
|
+
memory = FFI::MemoryPointer.from_string(text)
|
|
345
|
+
yield memory, text.bytesize
|
|
346
|
+
ensure
|
|
347
|
+
memory&.free
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
module NativeAllocator
|
|
5
|
+
def self.prepare
|
|
6
|
+
return unless FFI::Platform::IS_LINUX
|
|
7
|
+
flags = FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL
|
|
8
|
+
@library = FFI::DynamicLibrary.open(FFI::Library::LIBC, flags)
|
|
9
|
+
return unless @library.find_function("gnu_get_libc_version")
|
|
10
|
+
process = FFI::DynamicLibrary.open(nil, flags)
|
|
11
|
+
return unless %w[malloc calloc realloc free].all? do |name|
|
|
12
|
+
active = process.find_function(name)
|
|
13
|
+
expected = @library.find_function(name)
|
|
14
|
+
active && expected && active.address == expected.address
|
|
15
|
+
end
|
|
16
|
+
function = @library.find_function("malloc_trim")
|
|
17
|
+
@trim = FFI::Function.new(:int, [:size_t], function) if function
|
|
18
|
+
rescue LoadError, FFI::NotFoundError
|
|
19
|
+
@trim = nil
|
|
20
|
+
end
|
|
21
|
+
private_class_method :prepare
|
|
22
|
+
|
|
23
|
+
def self.trim
|
|
24
|
+
@trim&.call(0)
|
|
25
|
+
nil
|
|
26
|
+
rescue StandardError
|
|
27
|
+
# Reclaiming free pages must not interrupt an already completed release.
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@trim = nil
|
|
32
|
+
prepare
|
|
33
|
+
end
|
|
34
|
+
private_constant :NativeAllocator
|
|
35
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
# A read keeps its outcome available when interruption follows completion.
|
|
5
|
+
class NativeRead
|
|
6
|
+
def initialize(native)
|
|
7
|
+
@native = native
|
|
8
|
+
@ready = Queue.new
|
|
9
|
+
@result = @error = nil
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def run(owner)
|
|
13
|
+
@result = owner.use do |pointer|
|
|
14
|
+
Native.checked { |error| Native.thinkthen_call_next(pointer, @native, error) }
|
|
15
|
+
end
|
|
16
|
+
rescue Exception => error
|
|
17
|
+
@error = error
|
|
18
|
+
ensure
|
|
19
|
+
@ready.close
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def abandon
|
|
23
|
+
@error = ThreadError.new("native reader stopped")
|
|
24
|
+
@ready.close
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def value
|
|
28
|
+
@ready.pop
|
|
29
|
+
error = @error
|
|
30
|
+
raise error if error
|
|
31
|
+
@result
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# One helper owns blocking reads for a stream and exits when that stream closes.
|
|
36
|
+
class NativeReader
|
|
37
|
+
def initialize(owner)
|
|
38
|
+
@requests = Queue.new
|
|
39
|
+
@thread = self.class.worker(@requests, WeakRef.new(owner))
|
|
40
|
+
ObjectSpace.define_finalizer(self, self.class.finalizer(@requests))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.finalizer(requests)
|
|
44
|
+
proc { requests.close }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.worker(requests, reference)
|
|
48
|
+
Thread.new do
|
|
49
|
+
# @type var read: untyped
|
|
50
|
+
read = nil
|
|
51
|
+
Thread.handle_interrupt(Object => :never) do
|
|
52
|
+
begin
|
|
53
|
+
# Ruby's kill at exit must reach an idle helper, and must never split a read.
|
|
54
|
+
while Thread.handle_interrupt(Object => :immediate) { read = requests.pop }
|
|
55
|
+
read.run(reference.__getobj__)
|
|
56
|
+
read = nil
|
|
57
|
+
end
|
|
58
|
+
ensure
|
|
59
|
+
# A driver waits on every read it started, so none may stay unanswered.
|
|
60
|
+
requests.close
|
|
61
|
+
read&.abandon
|
|
62
|
+
while (read = requests.pop)
|
|
63
|
+
read.abandon
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def start(native)
|
|
71
|
+
read = NativeRead.new(native)
|
|
72
|
+
begin
|
|
73
|
+
@requests << read
|
|
74
|
+
rescue ClosedQueueError
|
|
75
|
+
read.abandon
|
|
76
|
+
end
|
|
77
|
+
read
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def close
|
|
81
|
+
@requests.close
|
|
82
|
+
@thread.join
|
|
83
|
+
nil
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
private_constant :NativeRead, :NativeReader
|
|
87
|
+
end
|