载入中...
搜索中...
未找到
SequenceRuntime.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7#include "common/Result.h"
8#include "common/StateValue.h"
9#include "common/Value.h"
12
13#include <cstdint>
14#include <functional>
15#include <optional>
16#include <string>
17#include <string_view>
18#include <unordered_map>
19#include <vector>
20
21namespace eve::dnut {
22
25 bool passed = false;
27 std::string reason;
29 std::string error;
30};
31
42
46 enum class Status : std::uint8_t { Completed, Blocked, Failed };
47
50 std::string error;
51};
52
55
68public:
70 static constexpr std::string_view SaveSchema = "eve.dialogue.runner";
72 static constexpr std::int64_t SaveVersion = 2;
73
75 using AssetResolver = std::function<const SequenceAsset*(const std::string&)>;
84
86 enum class EventKind : std::uint8_t { Started, NodeEntered, Blocked, Stepped, Resumed, Ended, Failed };
87
89 struct Event {
90 EventKind kind = EventKind::NodeEntered;
91 std::string assetId;
92 std::string nodeId;
93 std::string detail;
94 };
95
97 using EventSink = std::function<void(const Event&)>;
98
105 static constexpr int kExecutionBudget = 10000;
106
118
124 [[nodiscard]] eve::Result<void> runUntilBlocked();
125
131 [[nodiscard]] eve::Result<void> advance();
132
140 [[nodiscard]] eve::Result<void> select(std::string_view routeLabel);
141
147 [[nodiscard]] eve::Result<void> selectRouteForTransaction(std::string_view routeLabel);
148
156 [[nodiscard]] eve::Result<void> resumeStep(eve::Value result);
157
163 [[nodiscard]] eve::Result<void> resumeCommand(std::string_view requestId, eve::StateValue result);
164
166 [[nodiscard]] eve::Result<void> resumeCommand(std::string_view requestId, eve::Value result);
167
169 void stop();
170
176 [[nodiscard]] eve::Result<void> captureState(eve::Value& out) const;
177
185 [[nodiscard]] eve::Result<void> restoreState(const eve::Value& in);
186
187 void setAssetResolver(AssetResolver resolver) { assetResolver_ = std::move(resolver); }
188 void setConditionEvaluator(ConditionEvaluator evaluator) { conditionEvaluator_ = std::move(evaluator); }
190 void setStepRegistry(const StepKindRegistry* registry) { registry_ = registry; }
192 void registerCommand(std::string name, SequenceCommandHandler handler);
194 void unregisterCommand(std::string_view name);
196 void setCommandDispatcher(SequenceCommandHandler dispatcher) { commandDispatcher_ = std::move(dispatcher); }
198 void clearCommandDispatcher() { commandDispatcher_ = {}; }
206 void setHostContext(void* host) { hostContext_ = host; }
207 void setEventSink(EventSink sink) { eventSink_ = std::move(sink); }
208
210 [[nodiscard]] bool isActive() const { return asset_ != nullptr; }
212 [[nodiscard]] bool isBlocked() const { return blocked_; }
214 [[nodiscard]] bool isWaitingStep() const { return waitingStep_; }
216 [[nodiscard]] bool isWaitingCommand() const { return waitingCommand_; }
218 [[nodiscard]] const SequenceAsset* asset() const { return asset_; }
220 [[nodiscard]] const SequenceNode* currentNode() const;
221 [[nodiscard]] const std::string& currentNodeId() const { return nodeId_; }
222 [[nodiscard]] const eve::Value& bindings() const { return bindings_; }
223 [[nodiscard]] const eve::Value& locals() const { return locals_; }
224 [[nodiscard]] eve::Value& locals() { return locals_; }
226 [[nodiscard]] const eve::Value& lastStepResult() const { return lastStepResult_; }
228 [[nodiscard]] const SequenceCommandRequest* lastCommandRequest() const {
229 return lastCommandRequest_ ? &*lastCommandRequest_ : nullptr;
230 }
232 [[nodiscard]] const std::string& pendingCommandRequestId() const noexcept { return pendingCommandRequestId_; }
238 [[nodiscard]] const SequenceConditionOutcome* lastConditionResult() const {
239 return lastConditionResult_ ? &*lastConditionResult_ : nullptr;
240 }
241
242private:
243 struct Frame {
244 const SequenceAsset* asset = nullptr;
245 std::string returnNode;
248 };
249
251 struct ExecutionState {
252 const SequenceAsset* asset = nullptr;
253 std::string nodeId;
256 bool blocked = false;
257 bool waitingStep = false;
258 bool waitingCommand = false;
259 std::vector<Frame> callStack;
260 eve::Value lastStepResult;
261 std::optional<SequenceCommandRequest> lastCommandRequest;
262 std::string pendingCommandRequestId;
263 std::uint64_t commandSequence = 1;
264 std::optional<SequenceConditionOutcome> lastConditionResult;
265 };
266
267 [[nodiscard]] ExecutionState captureExecutionState() const;
268 void restoreExecutionState(ExecutionState state);
269 [[nodiscard]] eve::Result<void> runUntilBlockedImpl();
270 [[nodiscard]] eve::Result<void> enter(const std::string& nodeId);
271 [[nodiscard]] eve::Result<void> fail(eve::DiagnosticCode code, std::string message, std::string path = {});
273 [[nodiscard]] eve::Result<std::string> evaluateRoute(const SequenceNode& node);
274 [[nodiscard]] eve::Result<void> selectImpl(std::string_view routeLabel);
275 [[nodiscard]] eve::Result<void> resumeCommandImpl(std::string_view requestId, eve::Value result);
276 void emit(EventKind kind, const SequenceNode* node = nullptr, const std::string& detail = {}) const;
277
278 const SequenceAsset* asset_ = nullptr;
279 std::string nodeId_;
280 eve::Value bindings_ = eve::Value::Object{};
281 eve::Value locals_ = eve::Value::Object{};
282 bool blocked_ = false;
283 bool waitingStep_ = false;
284 bool waitingCommand_ = false;
285 std::vector<Frame> callStack_;
286 AssetResolver assetResolver_;
287 ConditionEvaluator conditionEvaluator_;
288 const StepKindRegistry* registry_ = nullptr;
289 std::unordered_map<std::string, SequenceCommandHandler> commandHandlers_;
290 SequenceCommandHandler commandDispatcher_;
291 void* hostContext_ = nullptr;
292 EventSink eventSink_;
293 eve::Value lastStepResult_;
294 std::optional<SequenceCommandRequest> lastCommandRequest_;
295 std::string pendingCommandRequestId_;
296 std::uint64_t commandSequence_ = 1;
297 std::optional<SequenceConditionOutcome> lastConditionResult_;
299 std::string failureText_;
300};
301
302} // namespace eve::dnut
Duration start
std::string nodeId
#define EVENGINE_API_PLATFORM
Definition Export.h:107
std::string message
DiagnosticCode code
TokenKind kind
HSQOBJECT handler
std::string name
std::vector< std::shared_ptr< DeviceBytes > > bindings
Definition OnnxGpgpu.cpp:38
std::string path
Definition PlayHost.cpp:110
int detail
Move-only, checked operation results for the common layer.
const RoadNode * node
Domain-neutral compiled sequence graph for `.dnut` sources.
Extensible step vocabulary, payload schema and dispatch.
UIHostHandle host
Owning, renderer-independent dynamic values.
Move-only operation result carrying either a value or Status.
Definition Result.h:155
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:20
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
Caller-owned interpreter over a compiled sequence asset.
void setCommandDispatcher(SequenceCommandHandler dispatcher)
Install the catch-all command dispatcher used after named lookup.
std::function< void(const Event &)> EventSink
Single event outlet; invoked synchronously without locks.
bool isWaitingStep() const
Whether the suspension is a step awaiting resumeStep.
void setEventSink(EventSink sink)
std::function< const SequenceAsset *(const std::string &)> AssetResolver
Resolves a called asset id to a borrowed, externally owned asset.
bool isActive() const
Whether a sequence is currently loaded.
void setHostContext(void *host)
Install the opaque consumer context forwarded to every step handler.
void setConditionEvaluator(ConditionEvaluator evaluator)
bool isBlocked() const
Whether execution is suspended and awaiting host acknowledgement.
void clearCommandDispatcher()
Remove the catch-all command dispatcher.
const std::string & pendingCommandRequestId() const noexcept
Stable id of the blocked command, or an empty string.
const std::string & currentNodeId() const
EventKind
Observable runtime transition.
bool isWaitingCommand() const
Whether the suspension is a command awaiting resumeCommand.
std::function< SequenceConditionOutcome(const eve::Value &)> ConditionEvaluator
Evaluates one authored condition value.
void setStepRegistry(const StepKindRegistry *registry)
Bind the step vocabulary; passing nullptr disables non-core steps.
const SequenceConditionOutcome * lastConditionResult() const
Last evaluated condition outcome.
const SequenceCommandRequest * lastCommandRequest() const
Last emitted command request, or nullptr when none has been emitted.
const eve::Value & locals() const
const SequenceAsset * asset() const
Borrowed active asset, or nullptr. @lifetime Owned by the caller of start.
const eve::Value & lastStepResult() const
Value supplied by the most recent resumeStep.
const eve::Value & bindings() const
void setAssetResolver(AssetResolver resolver)
Canonical step vocabulary owner and runtime handler router.
std::function< SequenceCommandResponse(const SequenceCommandRequest &)> SequenceCommandHandler
Synchronous owner-thread handler for one command request.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Immutable, parameterized and versioned compiled sequence.
Immutable request emitted by a generic command node.
eve::Value payload
Complete owning command payload for domain-specific decoding.
Structured response returned by a generic command handler.
Outcome of one authored condition, as seen by the language core.
std::string error
Evaluation failure; non-empty means no route may be selected.
std::string reason
Stable machine-readable reason; empty when passed is true.
One immutable step of a compiled sequence.
One observable transition; every field is owned by the event.