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,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
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
# A result owns its immutable value; item remains the caller's own object.
|
|
5
|
+
class Result
|
|
6
|
+
undef_method :<=>
|
|
7
|
+
attr_reader :function, :status, :probability, :error, :position, :id, :source, :detail, :item
|
|
8
|
+
|
|
9
|
+
def initialize(fields, question: nil, item: nil, parent: nil, raw_code: nil)
|
|
10
|
+
if parent
|
|
11
|
+
fields = fields.merge("position" => parent.position, "source" => parent.source.to_h)
|
|
12
|
+
fields = fields.merge("id" => parent.id) if parent.id_present?
|
|
13
|
+
end
|
|
14
|
+
@wire = Values.freeze_copy(fields)
|
|
15
|
+
@function, @status = fields.fetch("function").to_sym, fields.fetch("status").to_sym
|
|
16
|
+
@position, @id, @item = parent ? [parent.position, parent.id, parent.item] : [fields["position"], Values.freeze_copy(fields["id"]), item]
|
|
17
|
+
@source = parent ? parent.source : Row.value(Source, fields.fetch("source"))
|
|
18
|
+
@probability = fields["probability"]
|
|
19
|
+
@error = fields["error"] && Row.row_error(fields["error"], raw_code: raw_code)
|
|
20
|
+
@detail = Row.detail(fields.fetch("function"), fields["detail"])
|
|
21
|
+
@answer = answered? ? Row.answer(fields.fetch("function"), fields["answer"], question, self) : nil
|
|
22
|
+
@value = [function, status, @answer, probability, Values.freeze_copy(@error&.to_h), position, id, source, detail].freeze
|
|
23
|
+
freeze
|
|
24
|
+
end
|
|
25
|
+
private_class_method :new
|
|
26
|
+
|
|
27
|
+
def answered? = status == :answered
|
|
28
|
+
def unsure? = status == :unsure
|
|
29
|
+
def failed? = status == :failed
|
|
30
|
+
def id_present? = @wire.key?("id")
|
|
31
|
+
|
|
32
|
+
def answer
|
|
33
|
+
raise UnsureError.new(self) if unsure?
|
|
34
|
+
raise_failure(self) if failed?
|
|
35
|
+
@answer
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def raise_if_failed
|
|
39
|
+
failed = failed? ? self : function == :annotate && @answer&.each_value&.find(&:failed?)
|
|
40
|
+
raise_failure(failed) if failed
|
|
41
|
+
self
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def ==(other)
|
|
45
|
+
raise TypeError, "compare the result's .answer" unless other.is_a?(Result)
|
|
46
|
+
eql?(other)
|
|
47
|
+
end
|
|
48
|
+
def eql?(other) = other.is_a?(Result) && @value.eql?(other.instance_variable_get(:@value))
|
|
49
|
+
def hash = @value.hash
|
|
50
|
+
def to_h = Values.wire(@wire)
|
|
51
|
+
def to_json(*_args) = JSON.generate(@wire)
|
|
52
|
+
def to_s = Row.text(self, @answer)
|
|
53
|
+
def inspect = "#<#{self.class.name} #{to_s}>"
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def raise_failure(row)
|
|
58
|
+
# Native failed outcomes carry an error; the public getter remains optional.
|
|
59
|
+
# @type var failure: untyped
|
|
60
|
+
failure = row.error
|
|
61
|
+
raise FailedError.new(failure.message, code: failure.code, http_status: failure.http_status, rows: [row])
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class DecideRow < Result
|
|
66
|
+
def yes? = answer
|
|
67
|
+
end
|
|
68
|
+
class ChooseRow < Result; end
|
|
69
|
+
class TagRow < Result; end
|
|
70
|
+
class ScoreRow < Result; end
|
|
71
|
+
class RankRow < Result; end
|
|
72
|
+
class FindRow < Result; end
|
|
73
|
+
class RecognizeRow < Result; end
|
|
74
|
+
class RelateRow < Result; end
|
|
75
|
+
class AnnotateRow < Result; end
|
|
76
|
+
|
|
77
|
+
# Symbols and strings address the same canonical question name, loudly.
|
|
78
|
+
class AnnotationAnswers < Hash
|
|
79
|
+
def [](name) = fetch(name)
|
|
80
|
+
def fetch(name) = super(name.to_s)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
module Row
|
|
84
|
+
def self.decode(native, question: nil, item: nil)
|
|
85
|
+
variant(Native.parse(Native.row_json(native)), question: question, item: item, raw_code: native[:error_code])
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def self.variant(fields, question: nil, item: nil, parent: nil, raw_code: nil)
|
|
89
|
+
function = fields.fetch("function")
|
|
90
|
+
klass = function == "filter" ? Result : ThinkThen.const_get("#{function.capitalize}Row")
|
|
91
|
+
klass.send(:new, fields, question: question, item: item, parent: parent, raw_code: raw_code)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.value(klass, fields)
|
|
95
|
+
# @type var values: Hash[Symbol, untyped]
|
|
96
|
+
values = klass.members.to_h { |key| [key, Values.freeze_copy(fields[key.to_s])] }
|
|
97
|
+
klass.new(**values)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def self.row_error(fields, raw_code: nil)
|
|
101
|
+
name = fields.fetch("code")
|
|
102
|
+
known = NativeContract::CODE_NAMES.value?(name)
|
|
103
|
+
RowError.new(code: known ? Native.error_symbol(name) : :unknown, message: Values.freeze_copy(fields.fetch("message")),
|
|
104
|
+
http_status: fields["http_status"], raw_code: known ? fields["raw_code"] : raw_code || fields["raw_code"],
|
|
105
|
+
raw_name: fields["raw_name"] || (known ? nil : name)).freeze
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def self.relation(fields)
|
|
109
|
+
value(Relation, fields.merge("either" => fields.fetch("either", false), "source" => value(RelationName, fields.fetch("source")),
|
|
110
|
+
"target" => value(RelationName, fields.fetch("target"))))
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def self.answer(function, fields, question, parent)
|
|
114
|
+
case function
|
|
115
|
+
when "find" then fields && value(Span, fields)
|
|
116
|
+
when "recognize" then fields.map { |entry| value(Name, entry) }.freeze
|
|
117
|
+
when "relate" then fields.map { |entry| relation(entry) }.freeze
|
|
118
|
+
when "annotate"
|
|
119
|
+
# @type var questions: Hash[String, untyped]
|
|
120
|
+
questions = question.is_a?(AnnotateQuestion) ? question.questions : {}
|
|
121
|
+
AnnotationAnswers[fields.to_h { |name, entry| [name, variant(entry, question: questions[name], parent: parent)] }].freeze
|
|
122
|
+
when "choose" then question&.symbols? ? fields.to_sym : Values.freeze_copy(fields)
|
|
123
|
+
when "tag" then fields.map { |entry| question&.symbols? ? entry.to_sym : Values.freeze_copy(entry) }.freeze
|
|
124
|
+
when "score" then fields.to_f
|
|
125
|
+
else Values.freeze_copy(fields)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def self.detail(function, fields)
|
|
130
|
+
return nil unless fields
|
|
131
|
+
case function
|
|
132
|
+
when "choose", "tag" then value(LabelDetail, fields)
|
|
133
|
+
when "score", "rank" then value(LevelDetail, fields)
|
|
134
|
+
when "relate"
|
|
135
|
+
RelationDetail.new(names: fields.fetch("names").map { |entry| value(Name, entry) }.freeze,
|
|
136
|
+
pairs: fields.fetch("pairs").map { |entry| relation(entry) }.freeze)
|
|
137
|
+
when "recognize"
|
|
138
|
+
RecognizeDetail.new(pieces: fields.fetch("pieces").map { |entry| value(PieceDetail, entry) }.freeze,
|
|
139
|
+
names: fields.fetch("names").map { |entry| value(KindDetail, entry) }.freeze)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def self.number(value)
|
|
144
|
+
parts = value.to_s.split("e")
|
|
145
|
+
mantissa, exponent = parts.fetch(0), parts[1]
|
|
146
|
+
return mantissa.sub(/\.0\z/, "") unless exponent
|
|
147
|
+
"0." + "0" * (-exponent.to_i - 1) + mantissa.delete(".").sub(/0+\z/, "")
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Fold only string leaves. The tabs joining labels/entries remain separators.
|
|
151
|
+
def self.leaf(value) = value.to_s.tr("\r\n\t", " ")
|
|
152
|
+
def self.text(row, answer)
|
|
153
|
+
return "unsure" if row.unsure?
|
|
154
|
+
if row.failed?
|
|
155
|
+
# @type var failure: untyped
|
|
156
|
+
failure = row.error
|
|
157
|
+
return "failed: #{leaf(failure.raw_name || failure.code)}: #{leaf(failure.message)}"
|
|
158
|
+
end
|
|
159
|
+
case row.function
|
|
160
|
+
when :decide, :filter then answer ? "true" : "false"
|
|
161
|
+
when :choose then leaf(answer)
|
|
162
|
+
when :tag then answer.map { |label| leaf(label) }.join("\t")
|
|
163
|
+
when :score then number(answer)
|
|
164
|
+
when :rank then answer.to_s
|
|
165
|
+
when :find then answer ? leaf(answer.text) : ""
|
|
166
|
+
when :recognize then answer.map { |name| "#{leaf(name.kind)}: #{leaf(name.text)} (#{name.start}..#{name.end})" }.join("\t")
|
|
167
|
+
when :relate then answer.map { |entry| "#{leaf(entry.source.text)} --#{leaf(entry.relation)}--> #{leaf(entry.target.text)}" }.join("\t")
|
|
168
|
+
when :annotate then answer.map { |name, entry| "#{leaf(name)}=#{entry}" }.join("\t")
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
private_constant :Row, :Values, :WireValue, :AnnotationAnswers, :NativeContract
|
|
173
|
+
end
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
# A single-pass Enumerable of native outcomes. Close it when external iteration stops early.
|
|
5
|
+
class Rows < Enumerator
|
|
6
|
+
|
|
7
|
+
def initialize(client, request, records, cancel, question)
|
|
8
|
+
@client, @question, @items, @position = client, question, {}, 0
|
|
9
|
+
initial = records.is_a?(Array) ? Array.new(records) : nil
|
|
10
|
+
@initial = initial
|
|
11
|
+
@owner = CallOwner.new(client.send(:admit, request, initial))
|
|
12
|
+
@cancel = cancel
|
|
13
|
+
@cancel&.attach(@owner)
|
|
14
|
+
# @type var input: untyped
|
|
15
|
+
input = initial ? [] : records
|
|
16
|
+
@records = input.to_enum(:each)
|
|
17
|
+
@reader = nil
|
|
18
|
+
@driver = Mutex.new
|
|
19
|
+
@traversal = Mutex.new
|
|
20
|
+
@traversing = false
|
|
21
|
+
# Steep crashes on super with this native Enumerator constructor block.
|
|
22
|
+
# Only the core initializer crosses this boundary; produce stays checked.
|
|
23
|
+
# @type var initializer: untyped
|
|
24
|
+
initializer = Enumerator.instance_method(:initialize)
|
|
25
|
+
initializer.bind_call(self, initial ? initial.size : records.respond_to?(:size) ? records.size : nil) { |yielder| produce(yielder) }
|
|
26
|
+
# The block above captures this scope; drop the admitted copy it no longer needs.
|
|
27
|
+
initial = nil
|
|
28
|
+
rescue Exception
|
|
29
|
+
close if @owner
|
|
30
|
+
raise
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def each
|
|
34
|
+
return self unless block_given?
|
|
35
|
+
entered = false
|
|
36
|
+
begin
|
|
37
|
+
@traversal.synchronize do
|
|
38
|
+
raise ThreadError, "call already has an active driver" if @traversing
|
|
39
|
+
@traversing = entered = true
|
|
40
|
+
end
|
|
41
|
+
super { |row| yield row }
|
|
42
|
+
ensure
|
|
43
|
+
if entered
|
|
44
|
+
begin
|
|
45
|
+
close
|
|
46
|
+
ensure
|
|
47
|
+
@traversal.synchronize { @traversing = false }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def cancel
|
|
54
|
+
@client.send(:check_process)
|
|
55
|
+
@owner.cancel
|
|
56
|
+
end
|
|
57
|
+
def close
|
|
58
|
+
@client.send(:check_process)
|
|
59
|
+
@owner.close
|
|
60
|
+
@cancel&.detach(@owner)
|
|
61
|
+
release
|
|
62
|
+
@client.send(:forget, self)
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
def closed? = @owner.closed?
|
|
66
|
+
|
|
67
|
+
def rewind
|
|
68
|
+
raise IOError, "calls cannot be rewound"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def produce(yielder)
|
|
74
|
+
begin
|
|
75
|
+
while true
|
|
76
|
+
row = begin
|
|
77
|
+
pull
|
|
78
|
+
rescue StopIteration
|
|
79
|
+
break
|
|
80
|
+
end
|
|
81
|
+
yielder << row
|
|
82
|
+
end
|
|
83
|
+
ensure
|
|
84
|
+
close
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def pull
|
|
89
|
+
@client.send(:check_process)
|
|
90
|
+
unless @driver.try_lock
|
|
91
|
+
raise ThreadError, "call already has an active driver"
|
|
92
|
+
end
|
|
93
|
+
begin
|
|
94
|
+
# Thread#kill is not an Exception, so only Object holds it back here.
|
|
95
|
+
Thread.handle_interrupt(Object => :never) do
|
|
96
|
+
begin
|
|
97
|
+
drive
|
|
98
|
+
rescue Interrupt => interruption
|
|
99
|
+
@owner.cancel
|
|
100
|
+
begin
|
|
101
|
+
Native.checked { |error| Native.thinkthen_cancelled_error(error) }
|
|
102
|
+
rescue CancelledError => error
|
|
103
|
+
raise error, cause: interruption
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
ensure
|
|
108
|
+
@driver.unlock
|
|
109
|
+
release if @owner.closed?
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# A driver held in caller input must not hold up close, so it releases on its way out.
|
|
114
|
+
def release
|
|
115
|
+
return unless @driver&.try_lock
|
|
116
|
+
begin
|
|
117
|
+
@reader&.close
|
|
118
|
+
@reader = nil
|
|
119
|
+
@initial = nil
|
|
120
|
+
@items.clear
|
|
121
|
+
@records = nil
|
|
122
|
+
ensure
|
|
123
|
+
@driver.unlock
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def drive
|
|
128
|
+
while true
|
|
129
|
+
native = Native::Row.new
|
|
130
|
+
begin
|
|
131
|
+
status = blocking_next(native)
|
|
132
|
+
if status == 0
|
|
133
|
+
position = native[:position]
|
|
134
|
+
item = @initial ? @initial[position - 1] : @items.delete(position)
|
|
135
|
+
return Row.decode(native, question: @question, item: item)
|
|
136
|
+
end
|
|
137
|
+
raise StopIteration if status == 1
|
|
138
|
+
ensure
|
|
139
|
+
Native.thinkthen_row_free(native)
|
|
140
|
+
end
|
|
141
|
+
supply_input
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Ruby can interrupt a thread wait; FFI cannot interrupt a foreign call.
|
|
146
|
+
# Cancel before joining, then release row memory after the foreign call ends.
|
|
147
|
+
def blocking_next(native)
|
|
148
|
+
@reader ||= NativeReader.new(@owner)
|
|
149
|
+
read = @reader.start(native)
|
|
150
|
+
finished = false
|
|
151
|
+
begin
|
|
152
|
+
status = Thread.handle_interrupt(Object => :immediate) { read.value }
|
|
153
|
+
finished = true
|
|
154
|
+
status
|
|
155
|
+
ensure
|
|
156
|
+
begin
|
|
157
|
+
# Thread#kill runs ensure blocks but no rescue, so cancel here.
|
|
158
|
+
@owner.cancel unless finished
|
|
159
|
+
ensure
|
|
160
|
+
# Keep the row pinned until the native operation has stopped.
|
|
161
|
+
read.value rescue nil
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def supply_input
|
|
167
|
+
record = begin
|
|
168
|
+
# Caller input may block in Ruby; native storage stays protected outside it.
|
|
169
|
+
Thread.handle_interrupt(Object => :immediate) { @records.next }
|
|
170
|
+
rescue StopIteration
|
|
171
|
+
@owner.use { |pointer| Native.thinkthen_call_end_input(pointer) }
|
|
172
|
+
return
|
|
173
|
+
end
|
|
174
|
+
@position += 1
|
|
175
|
+
@items[@position] = record
|
|
176
|
+
kind, value = Native.record_value(record)
|
|
177
|
+
if value.nil?
|
|
178
|
+
@owner.use do |pointer|
|
|
179
|
+
Native.checked do |error|
|
|
180
|
+
kind == "missing" ? Native.thinkthen_call_send_missing(pointer, error) : Native.thinkthen_call_send_invalid(pointer, error)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
return
|
|
184
|
+
end
|
|
185
|
+
text = kind == "text"
|
|
186
|
+
Native.bytes(value) do |bytes, length|
|
|
187
|
+
@owner.use do |pointer|
|
|
188
|
+
Native.checked do |error|
|
|
189
|
+
if text
|
|
190
|
+
Native.thinkthen_call_send_text(pointer, bytes, length, error)
|
|
191
|
+
else
|
|
192
|
+
Native.thinkthen_call_send_json(pointer, bytes, length, error)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
module Values
|
|
5
|
+
def self.freeze_copy(value)
|
|
6
|
+
case value
|
|
7
|
+
when String then String.new(value).freeze
|
|
8
|
+
when Array then Array.new(value).map { |entry| freeze_copy(entry) }.freeze
|
|
9
|
+
when Hash then value.to_h { |key, entry| [freeze_copy(key), freeze_copy(entry)] }.freeze
|
|
10
|
+
else value
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
def self.wire(value)
|
|
14
|
+
case value
|
|
15
|
+
when Data, RowError then value.to_h
|
|
16
|
+
when Symbol then value.to_s
|
|
17
|
+
when Array then value.map { |entry| wire(entry) }
|
|
18
|
+
when Hash then value.to_h { |key, entry| [key.to_s, wire(entry)] }
|
|
19
|
+
else value
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
module WireValue
|
|
24
|
+
# Serialize the contract value, including when JSON.generate calls this method.
|
|
25
|
+
def to_json(*_args) = JSON.generate(to_h)
|
|
26
|
+
def to_h
|
|
27
|
+
# @type var klass: untyped
|
|
28
|
+
klass = self.class
|
|
29
|
+
klass.members.to_h { |key| [key.to_s, Values.wire(public_send(key))] }.reject { |_, value| value.nil? }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
# Provider-reported token sum (nil on overflow), with finished unknown attempts.
|
|
33
|
+
TokenUsage = Data.define(:reported, :unknown_attempts)
|
|
34
|
+
# An immutable copied cumulative client snapshot, independent of native ownership.
|
|
35
|
+
Usage = Data.define(:attempts, :responses, :status_retries, :connection_retries,
|
|
36
|
+
:request_body_bytes, :active_attempts, :active_calls, :input_tokens, :output_tokens)
|
|
37
|
+
# The question identity, backend, model and optional execution name.
|
|
38
|
+
Source = Data.define(:question_identity, :backend, :model, :name)
|
|
39
|
+
class Source
|
|
40
|
+
include WireValue
|
|
41
|
+
def initialize(question_identity:, backend:, model:, name: nil)
|
|
42
|
+
super(question_identity: question_identity, backend: backend.to_sym, model: model, name: name)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
# A record failure can be inspected as a value or raised as an exception.
|
|
46
|
+
class RowError < FailedError
|
|
47
|
+
attr_reader :raw_code, :raw_name
|
|
48
|
+
def initialize(code:, message:, http_status: nil, raw_code: nil, raw_name: nil)
|
|
49
|
+
@raw_code, @raw_name = raw_code, Values.freeze_copy(raw_name)
|
|
50
|
+
super(message, code: code, http_status: http_status)
|
|
51
|
+
end
|
|
52
|
+
def to_h
|
|
53
|
+
{"code" => code.to_s, "message" => message, "http_status" => http_status,
|
|
54
|
+
"raw_code" => raw_code, "raw_name" => raw_name}.reject { |_, value| value.nil? }
|
|
55
|
+
end
|
|
56
|
+
def to_json(*_args) = JSON.generate(to_h)
|
|
57
|
+
end
|
|
58
|
+
# A named source/target relationship and its optional phrase.
|
|
59
|
+
RelationRule = Data.define(:source, :target, :relation, :reads, :either)
|
|
60
|
+
class RelationRule
|
|
61
|
+
include WireValue
|
|
62
|
+
def initialize(source:, target:, relation:, reads: nil, either: false)
|
|
63
|
+
super(source: Values.freeze_copy(source), target: Values.freeze_copy(target), relation: Values.freeze_copy(relation),
|
|
64
|
+
reads: Values.freeze_copy(reads), either: either)
|
|
65
|
+
end
|
|
66
|
+
private
|
|
67
|
+
def definition
|
|
68
|
+
{"source" => source, "target" => target, "relation" => relation, "reads" => reads, "either" => either}.reject { |_, value| value.nil? }
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
# Retry delays and attempt timeouts are expressed in seconds.
|
|
72
|
+
RetryPolicy = Data.define(:max_retries, :initial_backoff, :attempt_timeout)
|
|
73
|
+
class RetryPolicy
|
|
74
|
+
include WireValue
|
|
75
|
+
def initialize(max_retries: nil, initial_backoff: nil, attempt_timeout: nil)
|
|
76
|
+
super
|
|
77
|
+
end
|
|
78
|
+
def to_h
|
|
79
|
+
{"max_retries" => max_retries, "initial_backoff_ms" => initial_backoff && (initial_backoff * 1000).round,
|
|
80
|
+
"attempt_timeout_ms" => attempt_timeout && (attempt_timeout * 1000).round}.reject { |_, value| value.nil? }
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
# Per-label probabilities for choose and tag answers.
|
|
84
|
+
LabelDetail = Data.define(:labels) { include WireValue }
|
|
85
|
+
# Per-level probabilities for score and rank answers.
|
|
86
|
+
LevelDetail = Data.define(:levels) { include WireValue }
|
|
87
|
+
# Matched text and zero-based character offsets.
|
|
88
|
+
Span = Data.define(:text, :start, :end) { include WireValue }
|
|
89
|
+
# Recognized text, character offsets, kind and probability.
|
|
90
|
+
Name = Data.define(:kind, :text, :start, :end, :probability) { include WireValue }
|
|
91
|
+
# An endpoint name, its character offsets and its kind.
|
|
92
|
+
RelationName = Data.define(:kind, :text, :start, :end) { include WireValue }
|
|
93
|
+
# The source and target names, relation probability and symmetry.
|
|
94
|
+
Relation = Data.define(:source, :target, :relation, :probability, :either)
|
|
95
|
+
class Relation
|
|
96
|
+
include WireValue
|
|
97
|
+
def to_h
|
|
98
|
+
result = super
|
|
99
|
+
result.delete("either") unless self.either
|
|
100
|
+
result
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
# The recognized names and evaluated relation pairs.
|
|
104
|
+
RelationDetail = Data.define(:names, :pairs) { include WireValue }
|
|
105
|
+
# A text piece, character offsets and tag probabilities.
|
|
106
|
+
PieceDetail = Data.define(:text, :start, :end, :tags) { include WireValue }
|
|
107
|
+
# Name text, character offsets and kind probabilities.
|
|
108
|
+
KindDetail = Data.define(:text, :start, :end, :kinds) { include WireValue }
|
|
109
|
+
# The text pieces and recognized name details.
|
|
110
|
+
RecognizeDetail = Data.define(:pieces, :names) { include WireValue }
|
|
111
|
+
end
|
data/lib/thinkthen/version.rb
CHANGED