thinkthen 0.2.0-x64-mingw-ucrt
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 +7 -0
- 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/anstyle-wincon-3.0.11/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-wincon-3.0.11/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/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 +605 -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/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/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/once_cell_polyfill-1.70.2/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/once_cell_polyfill-1.70.2/LICENSE-MIT +19 -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/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-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/winapi-0.3.9/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/winapi-0.3.9/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/windows-link-0.2.1/license-apache-2.0 +201 -0
- data/THIRD-PARTY-NOTICES/windows-link-0.2.1/license-mit +21 -0
- data/THIRD-PARTY-NOTICES/windows-sys-0.61.2/license-apache-2.0 +201 -0
- data/THIRD-PARTY-NOTICES/windows-sys-0.61.2/license-mit +21 -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/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/thinkthen_ffi.dll +0 -0
- data/lib/thinkthen/values.rb +111 -0
- data/lib/thinkthen/version.rb +5 -0
- data/lib/thinkthen.rb +105 -0
- data/sig/error_codes.rbs +3 -0
- data/sig/internal.rbs +277 -0
- data/sig/thinkthen.rbs +440 -0
- metadata +231 -0
|
@@ -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
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
# An immutable question validated once by the native engine.
|
|
5
|
+
class Question
|
|
6
|
+
attr_reader :value
|
|
7
|
+
def symbols? = !!@symbols
|
|
8
|
+
def initialize(function, text = nil, **options)
|
|
9
|
+
raise UsageError.new("question must be a String", code: :"usage.question") unless text.nil? || text.is_a?(String)
|
|
10
|
+
ThinkThen.send(:native)
|
|
11
|
+
Native.validate_threshold(function, text, options[:threshold])
|
|
12
|
+
@value = Values.freeze_copy({"function" => function.to_s, **(text.nil? ? {} : {"question" => text}), **options.transform_keys(&:to_s)})
|
|
13
|
+
Native.validate_question(@value)
|
|
14
|
+
freeze
|
|
15
|
+
rescue UsageError => error
|
|
16
|
+
if function == :filter && options[:threshold].is_a?(Hash) && error.code == :"usage.threshold"
|
|
17
|
+
raise UsageError.new(error.message + "; use ThinkThen.decide_each", code: error.code, http_status: error.http_status)
|
|
18
|
+
end
|
|
19
|
+
raise
|
|
20
|
+
end
|
|
21
|
+
def self.field(value)
|
|
22
|
+
return value if value.nil? || value.is_a?(String)
|
|
23
|
+
raise UsageError.new("field must be a pointer or path", code: :"usage.question") unless value.is_a?(Array)
|
|
24
|
+
value.map do |part|
|
|
25
|
+
case part
|
|
26
|
+
when String, Symbol then "/" + part.to_s.gsub("~", "~0").gsub("/", "~1")
|
|
27
|
+
when Integer
|
|
28
|
+
raise UsageError.new("path indices must be nonnegative", code: :"usage.question") if part.negative?
|
|
29
|
+
"/#{part}"
|
|
30
|
+
else raise UsageError.new("path components must be strings, symbols or integers", code: :"usage.question")
|
|
31
|
+
end
|
|
32
|
+
end.join
|
|
33
|
+
end
|
|
34
|
+
def self.threshold(value, band:)
|
|
35
|
+
return value if value.nil? || value.is_a?(Float)
|
|
36
|
+
if band && value.is_a?(Range)
|
|
37
|
+
raise UsageError.new("threshold range must have inclusive endpoints", code: :"usage.question") if value.exclude_end? || value.begin.nil? || value.end.nil?
|
|
38
|
+
raise UsageError.new("threshold endpoints must be Float", code: :"usage.question") unless value.begin.is_a?(Float) && value.end.is_a?(Float)
|
|
39
|
+
return {"low" => value.begin, "high" => value.end}
|
|
40
|
+
end
|
|
41
|
+
raise UsageError.new("threshold must be a Float#{band ? ' or inclusive Range' : ''}", code: :"usage.question")
|
|
42
|
+
end
|
|
43
|
+
def self.list(field, values, descriptions = nil)
|
|
44
|
+
unless values.is_a?(Array) && (values.all? { |value| value.is_a?(String) } || values.all? { |value| value.is_a?(Symbol) })
|
|
45
|
+
raise UsageError.new("expected an Array of String or an Array of Symbol", code: :"usage.question")
|
|
46
|
+
end
|
|
47
|
+
unless descriptions.nil? || descriptions.is_a?(Hash)
|
|
48
|
+
raise UsageError.new("descriptions must be a Hash", code: :"usage.question")
|
|
49
|
+
end
|
|
50
|
+
descriptions = descriptions&.transform_keys(&:to_s)
|
|
51
|
+
options = values.map do |value|
|
|
52
|
+
description = descriptions && descriptions.delete(value.to_s)
|
|
53
|
+
description.nil? ? value.to_s : {"name" => value.to_s, "description" => description}
|
|
54
|
+
end
|
|
55
|
+
# @type var result: Hash[Symbol, untyped]
|
|
56
|
+
result = {field => options}
|
|
57
|
+
# Unmatched keys go to shared admission, which owns their refusal.
|
|
58
|
+
result[:descriptions] = descriptions if descriptions && !descriptions.empty?
|
|
59
|
+
result
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
# A yes-or-no question with an optional cut or uncertainty band.
|
|
63
|
+
class DecideQuestion < Question
|
|
64
|
+
def initialize(text, threshold: nil, field: nil, yes_means: nil, no_means: nil)
|
|
65
|
+
super(:decide, text, **{yes_means: yes_means, no_means: no_means, threshold: Question.threshold(threshold, band: true), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
# A question selecting one named choice.
|
|
69
|
+
class ChooseQuestion < Question
|
|
70
|
+
def initialize(text, choices:, threshold: nil, field: nil, descriptions: nil)
|
|
71
|
+
@symbols = choices.is_a?(Array) && choices.all? { |entry| entry.is_a?(Symbol) }
|
|
72
|
+
super(:choose, text, **{**Question.list(:choices, choices, descriptions), threshold: Question.threshold(threshold, band: false), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
# A question selecting every label above the threshold.
|
|
76
|
+
class TagQuestion < Question
|
|
77
|
+
def initialize(text, labels:, threshold: nil, field: nil, descriptions: nil)
|
|
78
|
+
@symbols = labels.is_a?(Array) && labels.all? { |entry| entry.is_a?(Symbol) }
|
|
79
|
+
super(:tag, text, **{**Question.list(:labels, labels, descriptions), threshold: Question.threshold(threshold, band: false), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
# A question scoring text on ordered levels.
|
|
83
|
+
class ScoreQuestion < Question
|
|
84
|
+
def initialize(text, levels:, field: nil, descriptions: nil)
|
|
85
|
+
super(:score, text, **{**Question.list(:levels, levels, descriptions), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
# A question deciding which input records to keep.
|
|
89
|
+
class FilterQuestion < Question
|
|
90
|
+
def initialize(text, threshold: nil, field: nil, yes_means: nil, no_means: nil)
|
|
91
|
+
super(:filter, text, **{yes_means: yes_means, no_means: no_means, threshold: Question.threshold(threshold, band: true), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
# A question ordering records against levels, with an optional top count.
|
|
95
|
+
class RankQuestion < Question
|
|
96
|
+
def initialize(text, levels:, top: nil, field: nil, descriptions: nil)
|
|
97
|
+
raise UsageError.new("top must be an Integer", code: :"usage.question") unless top.nil? || top.is_a?(Integer)
|
|
98
|
+
super(:rank, text, **{**Question.list(:levels, levels, descriptions), top: top, field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
# A question finding a text span.
|
|
102
|
+
class FindQuestion < Question
|
|
103
|
+
def initialize(text, threshold: nil, field: nil)
|
|
104
|
+
super(:find, text, **{threshold: Question.threshold(threshold, band: false), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
# A set of kinds to recognize in text.
|
|
108
|
+
class RecognizeQuestion < Question
|
|
109
|
+
def initialize(kinds:, threshold: nil, field: nil)
|
|
110
|
+
super(:recognize, nil, **{**Question.list(:kinds, kinds), threshold: Question.threshold(threshold, band: false), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
# Relationship rules to evaluate between recognized names.
|
|
114
|
+
class RelateQuestion < Question
|
|
115
|
+
def initialize(rules:, threshold: nil, field: nil)
|
|
116
|
+
raise UsageError.new("rules must be an Array", code: :"usage.question") unless rules.is_a?(Array)
|
|
117
|
+
super(:relate, nil, **{rules: rules.map { |rule| raise UsageError.new("expected RelationRule", code: :"usage.question") unless rule.is_a?(RelationRule); rule.send(:definition) }, threshold: Question.threshold(threshold, band: false), field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
# An immutable collection of saved, named questions.
|
|
121
|
+
class Toolbox
|
|
122
|
+
# The immutable named question definitions.
|
|
123
|
+
attr_reader :questions
|
|
124
|
+
def initialize(questions:)
|
|
125
|
+
raise UsageError.new("questions must be a Hash", code: :"usage.question") unless questions.is_a?(Hash)
|
|
126
|
+
@questions = questions.to_h do |name, question|
|
|
127
|
+
raise UsageError.new("expected named non-annotate questions", code: :"usage.question") unless (name.is_a?(String) || name.is_a?(Symbol)) && question.is_a?(Question) && !question.is_a?(AnnotateQuestion)
|
|
128
|
+
[name.to_s.freeze, question]
|
|
129
|
+
end.freeze
|
|
130
|
+
freeze
|
|
131
|
+
end
|
|
132
|
+
NativeContract::FUNCTION_NAMES.values.reject { |function| function == "annotate" }.each do |function|
|
|
133
|
+
define_method(function) do |name|
|
|
134
|
+
native = ThinkThen.send(:native)
|
|
135
|
+
native.toolbox_json(native.context_dump(@questions.transform_values(&:value), :question), select: [name.to_s, function])
|
|
136
|
+
@questions.fetch(name.to_s)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
# Read a saved toolbox from a UTF-8 JSON file.
|
|
140
|
+
def self.load_file(path) = from_json(File.read(path))
|
|
141
|
+
# Read the canonical saved toolbox JSON format.
|
|
142
|
+
def self.from_json(json)
|
|
143
|
+
definitions = ThinkThen.send(:native).toolbox_json(json, saved: true, definitions: true)
|
|
144
|
+
questions = Native.parse(definitions).transform_values do |value|
|
|
145
|
+
klass = {"decide" => DecideQuestion, "choose" => ChooseQuestion, "tag" => TagQuestion,
|
|
146
|
+
"score" => ScoreQuestion, "filter" => FilterQuestion, "rank" => RankQuestion,
|
|
147
|
+
"find" => FindQuestion, "recognize" => RecognizeQuestion, "relate" => RelateQuestion}.fetch(value.fetch("function"))
|
|
148
|
+
question = klass.allocate
|
|
149
|
+
question.instance_variable_set(:@value, Values.freeze_copy(value))
|
|
150
|
+
question.freeze
|
|
151
|
+
end
|
|
152
|
+
self == AnnotationQuestions ? AnnotationQuestions.new(**questions.transform_keys(&:to_sym)) : new(questions: questions)
|
|
153
|
+
end
|
|
154
|
+
def to_json(*_args)
|
|
155
|
+
native = ThinkThen.send(:native)
|
|
156
|
+
native.toolbox_json(native.context_dump(@questions.transform_values(&:value), :question))
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
# Named questions evaluated together for one record.
|
|
160
|
+
class AnnotationQuestions < Toolbox
|
|
161
|
+
attr_reader :questions
|
|
162
|
+
def initialize(**questions)
|
|
163
|
+
super(questions: questions)
|
|
164
|
+
Native.validate_question({"function" => "annotate", "questions" => value})
|
|
165
|
+
end
|
|
166
|
+
def value = @questions.transform_values(&:value)
|
|
167
|
+
end
|
|
168
|
+
# A reusable annotation form with optional field selection.
|
|
169
|
+
class AnnotateQuestion < Question
|
|
170
|
+
attr_reader :questions
|
|
171
|
+
def initialize(questions, field: nil)
|
|
172
|
+
questions = AnnotationQuestions.new(**questions.transform_keys(&:to_sym)) if questions.is_a?(Hash)
|
|
173
|
+
if questions.is_a?(Toolbox) && !questions.is_a?(AnnotationQuestions)
|
|
174
|
+
# A Toolbox may hold rank questions; AnnotationQuestions refuses them when it is built.
|
|
175
|
+
# @type var held: untyped
|
|
176
|
+
held = questions.questions
|
|
177
|
+
questions = AnnotationQuestions.new(**held)
|
|
178
|
+
end
|
|
179
|
+
raise UsageError.new("expected named annotation questions", code: :"usage.question") unless questions.is_a?(AnnotationQuestions)
|
|
180
|
+
@questions = questions.questions
|
|
181
|
+
super(:annotate, nil, questions: questions.value, **{field: Question.field(field)}.reject { |_, value| value.nil? })
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|