inquirex 0.11.0 → 0.12.0
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/lib/inquirex/engine.rb +113 -2
- data/lib/inquirex/headless.rb +111 -0
- data/lib/inquirex/version.rb +1 -1
- data/lib/inquirex.rb +1 -0
- metadata +26 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b7434e2fb6848ea3155939c7a1c49a6447652aa9ae08285bc1a3d3ed76d5019e
|
|
4
|
+
data.tar.gz: e01afc532faf0001fd6fb011a58c2f871a30e100581171df58a42eebab7be730
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: afa3f0101993f5ccfdf3e9a646bbd7fb8f6540cb93168f05e01541dccdf5ede38f5cf55922d09a3fe44e6551a7c9344b020394908a3ca3e094858f4eb1d17fa6
|
|
7
|
+
data.tar.gz: 248c2fc2a30480493e1b38ecefa3d619a8f533d7a641c5cc62acf04179b9e4c21761ee3cad6862c6ff34cb365ad9f3c94414b2c14f99194c633ae11895a3ec74
|
data/lib/inquirex/engine.rb
CHANGED
|
@@ -62,6 +62,8 @@ module Inquirex
|
|
|
62
62
|
@completion_hook_errors = []
|
|
63
63
|
@suggestions = {}
|
|
64
64
|
@skipped = []
|
|
65
|
+
@snapshots = []
|
|
66
|
+
@rewound_answers = {}
|
|
65
67
|
@history << @current_step_id
|
|
66
68
|
skip_display_steps_if_needed
|
|
67
69
|
end
|
|
@@ -119,6 +121,7 @@ module Inquirex
|
|
|
119
121
|
raise Errors::ValidationError, "Validation failed: #{result.errors.join(", ")}" unless result.valid?
|
|
120
122
|
|
|
121
123
|
node = current_step
|
|
124
|
+
push_snapshot
|
|
122
125
|
@answers[@current_step_id] = value
|
|
123
126
|
@suggestions.delete(@current_step_id)
|
|
124
127
|
apply_accumulations(node, value)
|
|
@@ -133,6 +136,7 @@ module Inquirex
|
|
|
133
136
|
raise Errors::AlreadyFinishedError, "Flow is already finished" if finished?
|
|
134
137
|
|
|
135
138
|
node = current_step
|
|
139
|
+
push_snapshot
|
|
136
140
|
capture_transcript(Transcript.display_entry(node)) if node.display?
|
|
137
141
|
advance_step
|
|
138
142
|
end
|
|
@@ -166,6 +170,7 @@ module Inquirex
|
|
|
166
170
|
if current_step.required?
|
|
167
171
|
|
|
168
172
|
node = current_step
|
|
173
|
+
push_snapshot
|
|
169
174
|
default = resolve_default(node)
|
|
170
175
|
unless default.nil?
|
|
171
176
|
@answers[@current_step_id] = default
|
|
@@ -177,6 +182,74 @@ module Inquirex
|
|
|
177
182
|
advance_step
|
|
178
183
|
end
|
|
179
184
|
|
|
185
|
+
# Undoes the user's last answer or skip: the engine returns to the most
|
|
186
|
+
# recent collecting step the user acted on, with answers, totals (including
|
|
187
|
+
# the :text transcript), skipped, suggestions, history and completion
|
|
188
|
+
# metadata exactly as they were before that step was answered.
|
|
189
|
+
#
|
|
190
|
+
# Display steps passed with #advance in between are stepped over, since
|
|
191
|
+
# there is nothing on them to change. Steps elided by skip_if or by a
|
|
192
|
+
# prefilled answer were never presented and so are never returned to.
|
|
193
|
+
#
|
|
194
|
+
# The undone answer stays available through #previous_answer, so a
|
|
195
|
+
# renderer can re-ask the question with it pre-filled.
|
|
196
|
+
#
|
|
197
|
+
# Rebuilding the earlier state by replaying answers is not equivalent: a
|
|
198
|
+
# Proc default or a prefill! suggestion does not re-derive. So #answer,
|
|
199
|
+
# #skip and #advance each push a #to_state snapshot before they mutate,
|
|
200
|
+
# and this restores one.
|
|
201
|
+
#
|
|
202
|
+
# The snapshot stack is deliberately excluded from #to_state: it is
|
|
203
|
+
# session-local undo, not flow state, and persisting it would nest every
|
|
204
|
+
# earlier state inside each saved one. An engine rebuilt by .from_state
|
|
205
|
+
# therefore starts with nothing to go back to.
|
|
206
|
+
#
|
|
207
|
+
# @example Re-ask the previous question with its answer filled in
|
|
208
|
+
# engine.answer("married")
|
|
209
|
+
# engine.back # => true
|
|
210
|
+
# engine.previous_answer(:filing_status) # => "married"
|
|
211
|
+
#
|
|
212
|
+
# @return [Boolean] true when the engine moved back; false, with nothing
|
|
213
|
+
# changed, when no collecting step has been answered yet
|
|
214
|
+
def back
|
|
215
|
+
index = @snapshots.rindex { |state| collecting_step?(state[:current_step_id]) }
|
|
216
|
+
return false unless index
|
|
217
|
+
|
|
218
|
+
@rewound_answers.merge!(@answers)
|
|
219
|
+
load_state(@snapshots[index])
|
|
220
|
+
@snapshots.slice!(index..)
|
|
221
|
+
true
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Whether #back would move the engine.
|
|
225
|
+
#
|
|
226
|
+
# @return [Boolean]
|
|
227
|
+
def can_back?
|
|
228
|
+
@snapshots.any? { |state| collecting_step?(state[:current_step_id]) }
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# The answer to pre-fill when a step is asked again after #back: the
|
|
232
|
+
# step's current answer when it has one, otherwise the last answer #back
|
|
233
|
+
# took away from it.
|
|
234
|
+
#
|
|
235
|
+
# @param step_id [Symbol, String] step id
|
|
236
|
+
# @return [Object, nil] nil when the step was never answered
|
|
237
|
+
def previous_answer(step_id)
|
|
238
|
+
sym = step_id.to_sym
|
|
239
|
+
@answers.fetch(sym) { @rewound_answers[sym] }
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The current step's default as a concrete value, resolved exactly as
|
|
243
|
+
# #skip records it: a Proc default is called with the answers so far.
|
|
244
|
+
# Renderers pre-fill with it; a headless run under `yes` answers with it.
|
|
245
|
+
#
|
|
246
|
+
# @return [Object, nil] nil when the flow is finished or the step has no default
|
|
247
|
+
def current_default
|
|
248
|
+
return nil if finished?
|
|
249
|
+
|
|
250
|
+
resolve_default(current_step)
|
|
251
|
+
end
|
|
252
|
+
|
|
180
253
|
# Whether the user explicitly skipped the given step via #skip.
|
|
181
254
|
#
|
|
182
255
|
# @param step_id [Symbol, String] step id
|
|
@@ -311,17 +384,55 @@ module Inquirex
|
|
|
311
384
|
def restore_state(definition, state, validator)
|
|
312
385
|
@definition = definition
|
|
313
386
|
@validator = validator
|
|
387
|
+
@after_completion_hooks = []
|
|
388
|
+
@completion_hook_errors = []
|
|
389
|
+
@snapshots = []
|
|
390
|
+
@rewound_answers = {}
|
|
391
|
+
load_state(state)
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# Loads the flow-state fields of a #to_state hash, leaving the session
|
|
395
|
+
# (validator, hooks, the #back stack) alone. Shared by .from_state and
|
|
396
|
+
# #back so the two cannot disagree about what a state contains.
|
|
397
|
+
#
|
|
398
|
+
# @param state [Hash] symbol-keyed state, as #to_state returns it
|
|
399
|
+
# @return [void]
|
|
400
|
+
def load_state(state)
|
|
314
401
|
@current_step_id = state[:current_step_id]
|
|
315
402
|
@answers = state[:answers] || {}
|
|
316
403
|
@history = state[:history] || []
|
|
317
404
|
@totals = state[:totals] || init_totals
|
|
318
405
|
@completion_metadata = CompletionMetadata.from_h(state[:completion_metadata])
|
|
319
|
-
@after_completion_hooks = []
|
|
320
|
-
@completion_hook_errors = []
|
|
321
406
|
@suggestions = state[:suggestions] || {}
|
|
322
407
|
@skipped = state[:skipped] || []
|
|
323
408
|
end
|
|
324
409
|
|
|
410
|
+
# Records the state before #answer, #skip or #advance changes it. The
|
|
411
|
+
# copy is deep: those methods mutate the answers, totals, history,
|
|
412
|
+
# suggestions and skipped collections in place.
|
|
413
|
+
#
|
|
414
|
+
# @return [void]
|
|
415
|
+
def push_snapshot
|
|
416
|
+
@snapshots << deep_copy(to_state)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
# @param value [Object]
|
|
420
|
+
# @return [Object] a copy sharing no Hash, Array or String with +value+
|
|
421
|
+
def deep_copy(value)
|
|
422
|
+
case value
|
|
423
|
+
when Hash then value.to_h { |k, v| [k, deep_copy(v)] }
|
|
424
|
+
when Array then value.map { |v| deep_copy(v) }
|
|
425
|
+
when String then value.dup
|
|
426
|
+
else value
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# @param step_id [Symbol, nil]
|
|
431
|
+
# @return [Boolean] whether +step_id+ names a step that collects an answer
|
|
432
|
+
def collecting_step?(step_id)
|
|
433
|
+
!step_id.nil? && @definition.step(step_id).collecting?
|
|
434
|
+
end
|
|
435
|
+
|
|
325
436
|
# Whether +step_id+ names a multi-select step in the definition. Unknown
|
|
326
437
|
# ids (e.g. an extract schema field that matches no step) are not.
|
|
327
438
|
#
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Inquirex
|
|
4
|
+
# Runs a flow to the end from a hash of preset answers, with no terminal,
|
|
5
|
+
# no prompt and no LLM: the driver behind `inquirex-tty run --answers` and
|
|
6
|
+
# `--yes`, and the way to run a flow in tests and CI.
|
|
7
|
+
#
|
|
8
|
+
# Answers reach the engine through Engine#answer and Engine#skip only,
|
|
9
|
+
# never Engine#prefill!. prefill! records a multi-select as a suggestion
|
|
10
|
+
# that no skip_if rule can see, so a flow whose multi-select is gated on
|
|
11
|
+
# its own answer would stop on that question with nobody to answer it.
|
|
12
|
+
#
|
|
13
|
+
# Step by step:
|
|
14
|
+
#
|
|
15
|
+
# - a display step advances silently;
|
|
16
|
+
# - a step present in +answers+ is answered with that value, as given (an
|
|
17
|
+
# LLM step included: the value stands in for the adapter's output, and
|
|
18
|
+
# nothing is splatted into other answers);
|
|
19
|
+
# - otherwise, with +yes+, a step with a default is answered with it (a
|
|
20
|
+
# Proc default resolved against the answers so far);
|
|
21
|
+
# - otherwise an optional step is skipped, recording its default as
|
|
22
|
+
# Engine#skip does;
|
|
23
|
+
# - otherwise, with +yes+, a required LLM step is answered with nil;
|
|
24
|
+
# - otherwise the run stops with MissingAnswer naming the step.
|
|
25
|
+
#
|
|
26
|
+
# No LLM adapter is ever called: core has none, and a run under +yes+ is
|
|
27
|
+
# meant to be offline and repeatable.
|
|
28
|
+
#
|
|
29
|
+
# @example Run a flow from an answers file's contents
|
|
30
|
+
# results = Inquirex::Headless.new(definition, answers: { filing_status: "single" }, yes: true).call
|
|
31
|
+
# results[:answers] # => { filing_status: "single", dependents: 0, ... }
|
|
32
|
+
# results[:path_taken] # => [:filing_status, :dependents, :done]
|
|
33
|
+
class Headless
|
|
34
|
+
# Raised when a step needs an answer that neither +answers+ nor (with
|
|
35
|
+
# +yes+) a default supplies.
|
|
36
|
+
class MissingAnswer < Errors::EngineError
|
|
37
|
+
# @return [Symbol] the step left without an answer
|
|
38
|
+
attr_reader :step_id
|
|
39
|
+
|
|
40
|
+
# @param step_id [Symbol]
|
|
41
|
+
def initialize(step_id)
|
|
42
|
+
@step_id = step_id
|
|
43
|
+
super("No answer for required step #{step_id.inspect}: give one in the answers, or a default with yes")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The engine the run drives. Register Engine#after_completion hooks on it
|
|
48
|
+
# before #call to attach completion metadata.
|
|
49
|
+
#
|
|
50
|
+
# @return [Engine]
|
|
51
|
+
attr_reader :engine
|
|
52
|
+
|
|
53
|
+
# @param definition [Definition] the flow to run
|
|
54
|
+
# @param answers [Hash{Symbol, String => Object}] preset answers by step id;
|
|
55
|
+
# string keys, as JSON and YAML produce them, are accepted
|
|
56
|
+
# @param yes [Boolean] take each unanswered step's default
|
|
57
|
+
def initialize(definition, answers: {}, yes: false)
|
|
58
|
+
@engine = Engine.new(definition)
|
|
59
|
+
@answers = answers.to_h { |step_id, value| [step_id.to_sym, value] }
|
|
60
|
+
@yes = yes
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Drives the engine until the flow finishes.
|
|
64
|
+
#
|
|
65
|
+
# @return [Hash] +:path_taken+ (step ids, in order), +:answers+
|
|
66
|
+
# (Engine#answers_with_metadata) and +:steps_completed+. A caller adds
|
|
67
|
+
# whatever it knows that the engine does not, such as the flow file or a
|
|
68
|
+
# completion time.
|
|
69
|
+
# @raise [MissingAnswer] when a step can be given no answer
|
|
70
|
+
# @raise [Errors::ValidationError] when the validator rejects an answer
|
|
71
|
+
def call
|
|
72
|
+
take(@engine.current_step) until @engine.finished?
|
|
73
|
+
results
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# Moves the engine past one step.
|
|
79
|
+
#
|
|
80
|
+
# @param node [Node]
|
|
81
|
+
# @return [void]
|
|
82
|
+
def take(node)
|
|
83
|
+
return @engine.advance if node.display?
|
|
84
|
+
return @engine.answer(@answers[node.id]) if @answers.key?(node.id)
|
|
85
|
+
|
|
86
|
+
# Only under yes: resolving a Proc default runs host code.
|
|
87
|
+
default = @yes ? @engine.current_default : nil
|
|
88
|
+
return @engine.answer(default) unless default.nil?
|
|
89
|
+
return @engine.skip unless node.required?
|
|
90
|
+
return @engine.answer(nil) if @yes && llm_step?(node)
|
|
91
|
+
|
|
92
|
+
raise MissingAnswer, node.id
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# LLM steps come from inquirex-llm, which core does not load, so they are
|
|
96
|
+
# recognised by the predicate their node class answers.
|
|
97
|
+
#
|
|
98
|
+
# @param node [Node]
|
|
99
|
+
# @return [Boolean]
|
|
100
|
+
def llm_step?(node) = node.respond_to?(:llm_verb?) && node.llm_verb?
|
|
101
|
+
|
|
102
|
+
# @return [Hash]
|
|
103
|
+
def results
|
|
104
|
+
{
|
|
105
|
+
path_taken: @engine.history,
|
|
106
|
+
answers: @engine.answers_with_metadata,
|
|
107
|
+
steps_completed: @engine.history.length
|
|
108
|
+
}
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
data/lib/inquirex/version.rb
CHANGED
data/lib/inquirex.rb
CHANGED
|
@@ -49,6 +49,7 @@ require_relative "inquirex/safe_source"
|
|
|
49
49
|
# Engine
|
|
50
50
|
require_relative "inquirex/engine/state_serializer"
|
|
51
51
|
require_relative "inquirex/engine"
|
|
52
|
+
require_relative "inquirex/headless"
|
|
52
53
|
|
|
53
54
|
# Graph utilities
|
|
54
55
|
require_relative "inquirex/graph/mermaid_exporter"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: inquirex
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Konstantin Gredeskoul
|
|
@@ -25,6 +25,30 @@ dependencies:
|
|
|
25
25
|
- ~>
|
|
26
26
|
- !ruby/object:Gem::Version
|
|
27
27
|
version: "0.6"
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: prism
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: "1.7"
|
|
36
|
+
-
|
|
37
|
+
- "<"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: "2"
|
|
40
|
+
type: :runtime
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
-
|
|
45
|
+
- ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: "1.7"
|
|
48
|
+
-
|
|
49
|
+
- "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "2"
|
|
28
52
|
description: "Inquirex lets you define multi-step questionnaires as directed graphs with conditional branching, using a conversational DSL (ask, say, mention) and an AST-based rule system (contains, equals, greater_than, all, any). The engine walks the graph, collects structured answers, and serializes everything to JSON — making it the ideal backbone for cross-platform intake forms where the frontend is a chat widget, a terminal, or a mobile app. Framework-agnostic, no third-party dependencies, thread-safe immutable definitions."
|
|
29
53
|
email:
|
|
30
54
|
- kigster@gmail.com
|
|
@@ -50,6 +74,7 @@ files:
|
|
|
50
74
|
- lib/inquirex/errors.rb
|
|
51
75
|
- lib/inquirex/evaluator.rb
|
|
52
76
|
- lib/inquirex/graph/mermaid_exporter.rb
|
|
77
|
+
- lib/inquirex/headless.rb
|
|
53
78
|
- lib/inquirex/node.rb
|
|
54
79
|
- lib/inquirex/rules/all.rb
|
|
55
80
|
- lib/inquirex/rules/any.rb
|