载入中...
搜索中...
未找到
SequenceRuntime.cpp
浏览该文件的文档.
2
3#include <string>
4#include <utility>
5
6namespace eve::dnut {
7
8namespace {
9
11void mergeMissing(eve::Value& target, const eve::Value& defaults) {
12 const auto* targetObject = target.getIf<eve::Value::Object>();
13 const auto* defaultsObject = defaults.getIf<eve::Value::Object>();
14 if (!targetObject || !defaultsObject) return;
15 for (const auto& entry : *defaultsObject) {
16 if (target.find(entry.first) == nullptr) target.set(entry.first, entry.second);
17 }
18}
19
20bool readString(const eve::Value& object, const std::string& key, std::string& out) {
21 const eve::Value* value = object.find(key);
22 if (!value || !value->isString()) return false;
23 out = value->asString();
24 return true;
25}
26
28 switch (value.kind()) {
29 case eve::StateValue::Kind::Null: return {};
36 out.reserve(value.arraySize());
37 for (std::size_t index = 0; index < value.arraySize(); ++index)
38 out.push_back(toCanonicalValue(value.at(index)));
39 return eve::Value(std::move(out));
40 }
43 for (const auto& key : value.keys()) out.emplace(key, toCanonicalValue(*value.find(key)));
44 return eve::Value(std::move(out));
45 }
46 }
47 return {};
48}
49
50bool parameterTypeMatches(const eve::Value& value, SequenceParameterType type) {
51 switch (type) {
52 case SequenceParameterType::Any: return true;
53 case SequenceParameterType::String: return value.isString();
54 case SequenceParameterType::Integer: return value.isInt64();
55 case SequenceParameterType::Number: return value.isNumeric();
56 case SequenceParameterType::Boolean: return value.isBool();
57 }
58 return false;
59}
60
61eve::Value captureFrame(const SequenceAsset* asset, const std::string& node, const eve::Value& bindings,
62 const eve::Value& locals) {
64 out.set("asset", eve::Value::string(asset ? asset->id : std::string{}));
65 out.set("version", eve::Value::integer(asset ? asset->version : 0));
66 out.set("node", eve::Value::string(node));
67 out.set("bindings", bindings);
68 out.set("locals", locals);
69 return out;
70}
71
72} // namespace
73
75 return asset_ ? asset_->findNode(nodeId_) : nullptr;
76}
77
78SequenceRuntime::ExecutionState SequenceRuntime::captureExecutionState() const {
79 return {asset_,
80 nodeId_,
81 bindings_,
82 locals_,
83 blocked_,
84 waitingStep_,
85 waitingCommand_,
86 callStack_,
87 loopStack_,
88 parallelStack_,
89 lastStepResult_,
90 lastCommandRequest_,
91 pendingCommandRequestId_,
92 commandSequence_,
93 lastConditionResult_};
94}
95
96void SequenceRuntime::restoreExecutionState(ExecutionState state) {
97 asset_ = state.asset;
98 nodeId_ = std::move(state.nodeId);
99 bindings_ = std::move(state.bindings);
100 locals_ = std::move(state.locals);
101 blocked_ = state.blocked;
102 waitingStep_ = state.waitingStep;
103 waitingCommand_ = state.waitingCommand;
104 callStack_ = std::move(state.callStack);
105 loopStack_ = std::move(state.loopStack);
106 parallelStack_ = std::move(state.parallelStack);
107 lastStepResult_ = std::move(state.lastStepResult);
108 lastCommandRequest_ = std::move(state.lastCommandRequest);
109 pendingCommandRequestId_ = std::move(state.pendingCommandRequestId);
110 commandSequence_ = state.commandSequence;
111 lastConditionResult_ = std::move(state.lastConditionResult);
112}
113
114eve::Result<void> SequenceRuntime::fail(eve::DiagnosticCode code, std::string message, std::string path) {
115 failureText_ = message;
116 return eve::Result<void>::failure(eve::Diagnostic::error(code, message, std::move(path), {}, "dnut.runtime"));
117}
118
119void SequenceRuntime::emit(EventKind kind, const SequenceNode* node, const std::string& detail) const {
120 if (!eventSink_) return;
121 eventSink_({kind, asset_ ? asset_->id : std::string{}, node ? node->id : nodeId_, detail});
122}
123
125 asset_ = nullptr;
126 nodeId_.clear();
127 bindings_ = eve::Value::Object{};
128 locals_ = eve::Value::Object{};
129 blocked_ = false;
130 waitingStep_ = false;
131 waitingCommand_ = false;
132 callStack_.clear();
133 loopStack_.clear();
134 parallelStack_.clear();
135 lastStepResult_ = eve::Value{};
136 lastCommandRequest_.reset();
137 pendingCommandRequestId_.clear();
138 lastConditionResult_.reset();
139}
140
142 if (!name.empty() && handler) commandHandlers_[std::move(name)] = std::move(handler);
143}
144
145void SequenceRuntime::unregisterCommand(std::string_view name) { commandHandlers_.erase(std::string(name)); }
146
148 if (!asset) return fail(eve::DiagnosticCode::InvalidArgument, "sequence: null asset", "asset");
149 if (!bindings.isObject())
150 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: bindings must be an object", "bindings");
151 if (auto validation = asset->validate(); !validation.ok()) {
152 const auto* diagnostic = validation.error();
154 diagnostic ? diagnostic->message() : "sequence: asset validation failed", "asset");
155 }
156
157 eve::Value resolvedBindings = bindings;
158 for (const auto& parameter : asset->parameters) {
159 const eve::Value* value = resolvedBindings.find(parameter.name);
160 if (!value && !parameter.defaultValue.isNull()) {
161 resolvedBindings.set(parameter.name, parameter.defaultValue);
162 value = resolvedBindings.find(parameter.name);
163 }
164 if (!value && parameter.required)
166 "sequence '" + asset->id + "': missing required binding '" + parameter.name + "'",
167 "bindings." + parameter.name);
168 if (value && !parameterTypeMatches(*value, parameter.type))
170 "sequence '" + asset->id + "': binding '" + parameter.name + "' must be " +
172 "bindings." + parameter.name);
173 }
174 for (const auto& key : bindings.keys()) {
175 bool declared = false;
176 for (const auto& parameter : asset->parameters)
177 if (parameter.name == key) {
178 declared = true;
179 break;
180 }
181 if (!declared)
183 "sequence '" + asset->id + "': undeclared binding '" + key + "'", "bindings." + key);
184 }
185
186 ExecutionState before = captureExecutionState();
187 failureText_.clear();
188 lastConditionResult_.reset();
189 lastStepResult_ = eve::Value{};
190 asset_ = asset;
191 bindings_ = std::move(resolvedBindings);
192 locals_ = eve::Value::Object{};
193 callStack_.clear();
194 loopStack_.clear();
195 parallelStack_.clear();
196 blocked_ = false;
197 waitingStep_ = false;
198 waitingCommand_ = false;
199 pendingCommandRequestId_.clear();
200 lastCommandRequest_.reset();
201 emit(EventKind::Started);
202 if (auto entered = enter(asset_->entry); !entered.ok()) {
203 restoreExecutionState(std::move(before));
204 return entered;
205 }
206 auto ran = runUntilBlockedImpl();
207 if (!ran.ok()) restoreExecutionState(std::move(before));
208 return ran;
209}
210
211eve::Result<void> SequenceRuntime::enter(const std::string& nodeId) {
212 if (!asset_) return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: no active asset", "node");
213 blocked_ = false;
214 waitingStep_ = false;
215 waitingCommand_ = false;
216 if (nodeId.empty()) {
217 stop();
219 }
220 const SequenceNode* node = asset_->findNode(nodeId);
221 if (!node)
223 "sequence: asset '" + asset_->id + "' has no node '" + nodeId + "'", "node");
224 nodeId_ = nodeId;
227}
228
229eve::Result<std::string> SequenceRuntime::evaluateRoute(const SequenceNode& node) {
230 for (const auto& route : node.routes) {
231 if (route.condition.isNull()) return eve::Result<std::string>::success(route.target);
232 if (!conditionEvaluator_)
235 "sequence: node '" + node.id + "' requires a condition evaluator", "condition", {},
236 "dnut.runtime"));
237 SequenceConditionOutcome outcome = conditionEvaluator_(route.condition);
238 lastConditionResult_ = outcome;
239 if (!outcome.error.empty())
241 eve::Diagnostic::error(eve::DiagnosticCode::Failed, outcome.error, "condition", {}, "dnut.runtime"));
242 if (outcome.passed) return eve::Result<std::string>::success(route.target);
243 }
245}
246
248 ExecutionState before = captureExecutionState();
249 auto ran = runUntilBlockedImpl();
250 if (!ran.ok()) restoreExecutionState(std::move(before));
251 return ran;
252}
253
254eve::Result<void> SequenceRuntime::runUntilBlockedImpl() {
255 int budget = kExecutionBudget;
256 while (asset_ != nullptr) {
257 if (budget-- <= 0)
258 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: execution budget exceeded", "budget");
259 const SequenceNode* node = currentNode();
260 if (!node) return fail(eve::DiagnosticCode::InvariantViolation, "sequence: invalid execution cursor", "cursor");
261
262 if (node->type == "branch") {
263 auto target = evaluateRoute(*node);
264 if (!target.ok()) {
265 const auto* diagnostic = target.error();
267 diagnostic ? diagnostic->message() : "sequence: branch evaluation failed", "branch");
268 }
269 const std::string& next = target.value();
270 if (auto entered = enter(next); !entered.ok()) return entered;
271 continue;
272 }
273 if (node->type == "choice") {
274 blocked_ = true;
275 waitingStep_ = false;
276 emit(EventKind::Blocked, node, "choice");
278 }
279 if (node->type == "wait") {
280 blocked_ = true;
281 waitingStep_ = false;
282 emit(EventKind::Blocked, node, "wait");
284 }
285 if (node->type == "label") {
286 if (auto entered = enter(node->next); !entered.ok()) return entered;
287 continue;
288 }
289 if (node->type == "jump") {
290 const eve::Value* target = node->payload.find("target");
291 if (!target || !target->isString() || target->asString().empty())
293 "sequence: jump node '" + node->id + "' has no target", "jump.target");
294 if (auto entered = enter(target->asString()); !entered.ok()) return entered;
295 continue;
296 }
297 if (node->type == "loop") {
298 if (auto handled = handleLoop(*node); !handled.ok()) return handled;
299 continue;
300 }
301 if (node->type == "break") {
302 if (auto handled = handleBreakOrContinue(*node, true); !handled.ok()) return handled;
303 continue;
304 }
305 if (node->type == "continue") {
306 if (auto handled = handleBreakOrContinue(*node, false); !handled.ok()) return handled;
307 continue;
308 }
309 if (node->type == "parallel") {
310 if (auto handled = handleParallel(*node); !handled.ok()) return handled;
311 if (blocked_) return eve::Result<void>::success();
312 continue;
313 }
314 if (node->type == "join") {
315 if (auto handled = handleJoin(*node); !handled.ok()) return handled;
316 if (blocked_) return eve::Result<void>::success();
317 continue;
318 }
319 if (node->type == "end") {
320 if (callStack_.empty()) {
321 emit(EventKind::Ended, node);
322 stop();
324 }
325 Frame frame = std::move(callStack_.back());
326 callStack_.pop_back();
327 asset_ = frame.asset;
328 bindings_ = std::move(frame.bindings);
329 locals_ = std::move(frame.locals);
330 loopStack_ = std::move(frame.loopStack);
331 parallelStack_ = std::move(frame.parallelStack);
332 if (auto entered = enter(frame.returnNode); !entered.ok()) return entered;
333 continue;
334 }
335 if (node->type == "call") {
336 if (!assetResolver_)
338 "sequence: call node requires an asset resolver", "call");
339 const eve::Value* targetId = node->payload.find("target");
340 if (!targetId || !targetId->isString() || targetId->asString().empty())
342 "sequence: call node '" + node->id + "' has no target asset id", "call.target");
343 const SequenceAsset* target = assetResolver_(targetId->asString());
344 if (!target)
346 "sequence: missing called asset '" + targetId->asString() + "'", "call.target");
347 if (auto validation = target->validate(); !validation.ok()) {
348 const auto* diagnostic = validation.error();
350 diagnostic ? diagnostic->message() : "sequence: called asset is invalid", "call.target");
351 }
352 Frame frame;
353 frame.asset = asset_;
354 frame.returnNode = node->next;
355 if (const eve::Value* returnNode = node->payload.find("return");
356 returnNode && returnNode->isString() && !returnNode->asString().empty())
357 frame.returnNode = returnNode->asString();
358 frame.bindings = bindings_;
359 frame.locals = locals_;
360 frame.loopStack = loopStack_;
361 frame.parallelStack = parallelStack_;
362 callStack_.push_back(std::move(frame));
363 eve::Value targetBindings = eve::Value::Object{};
364 if (const eve::Value* arguments = node->payload.find("arguments"); arguments && arguments->isObject())
365 targetBindings = *arguments;
366 mergeMissing(targetBindings, bindings_);
367 asset_ = target;
368 bindings_ = std::move(targetBindings);
369 locals_ = eve::Value::Object{};
370 loopStack_.clear();
371 parallelStack_.clear();
372 if (auto entered = enter(target->entry); !entered.ok()) return entered;
373 continue;
374 }
375
376 if (node->type == "command") {
377 const eve::Value* name = node->payload.find("name");
378 if (!name) name = node->payload.find("target");
379 if (!name || !name->isString() || name->asString().empty())
381 "sequence: command node '" + node->id + "' has no name", "command.name");
382 const auto named = commandHandlers_.find(name->asString());
383 if (named == commandHandlers_.end() && !commandDispatcher_)
385 "sequence: command '" + name->asString() + "' is not registered", "command.name");
386
387 SequenceCommandRequest request;
388 request.requestId = asset_->id + ":" + node->id + ":" + std::to_string(commandSequence_++);
389 request.name = name->asString();
390 if (const eve::Value* arguments = node->payload.find("arguments"); arguments && arguments->isObject())
391 request.arguments = *arguments;
392 request.bindings = bindings_;
393 request.locals = locals_;
394 request.payload = node->payload;
395 lastCommandRequest_ = request;
396 SequenceCommandResponse response =
397 named != commandHandlers_.end() ? named->second(request) : commandDispatcher_(request);
399 return fail(eve::DiagnosticCode::Failed,
400 response.error.empty() ? "sequence: command failed" : response.error,
401 "command." + node->id);
403 blocked_ = true;
404 waitingCommand_ = true;
405 waitingStep_ = false;
406 pendingCommandRequestId_ = request.requestId;
407 emit(EventKind::Blocked, node, "command");
409 }
410 const eve::Value* resultLocal = node->payload.find("resultLocal");
411 if (!resultLocal) resultLocal = node->payload.find("result");
412 if (resultLocal && resultLocal->isString() && !resultLocal->asString().empty())
413 locals_.set(resultLocal->asString(), response.value);
414 lastStepResult_ = std::move(response.value);
415 emit(EventKind::Stepped, node, "command");
416 if (auto entered = enter(node->next); !entered.ok()) return entered;
417 continue;
418 }
419
420 if (!registry_)
422 "sequence: step '" + node->type + "' requires a step registry", "steps." + node->id);
423 const StepContext context{asset_->id, node->id, &bindings_, &locals_, hostContext_};
424 StepOutcome outcome = registry_->dispatch(*node, context);
425 if (outcome.status == StepStatus::Failed)
426 return fail(eve::DiagnosticCode::Failed,
427 outcome.error.empty() ? "sequence: step '" + node->type + "' failed" : outcome.error,
428 "steps." + node->id);
429 if (outcome.status == StepStatus::Blocked) {
430 blocked_ = true;
431 waitingStep_ = true;
432 lastStepResult_ = outcome.value;
433 emit(EventKind::Blocked, node, node->type);
435 }
436 lastStepResult_ = std::move(outcome.value);
437 emit(EventKind::Stepped, node, node->type);
438 if (auto entered = enter(node->next); !entered.ok()) return entered;
439 }
441}
442
444 const SequenceNode* node = currentNode();
445 if (!node || !blocked_)
446 return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: runtime is not blocked", "advance");
447 if (waitingCommand_)
449 "sequence: resume the pending command instead of advancing", "advance");
450 if (node->type == "choice")
452 "sequence: select a choice route instead of advancing", "advance");
453 ExecutionState before = captureExecutionState();
454 const std::string next = node->next;
455 waitingStep_ = false;
456 if (!parallelStack_.empty() && parallelStack_.back().hostBlocked) {
457 ParallelFrame& frame = parallelStack_.back();
458 ParallelLane* lane = nullptr;
459 for (auto& candidate : frame.lanes) {
460 if (!candidate.done && candidate.blocked && candidate.nodeId == nodeId_) {
461 lane = &candidate;
462 break;
463 }
464 }
465 if (!lane) {
466 restoreExecutionState(std::move(before));
468 "sequence: parallel host block has no matching lane", "parallel");
469 }
470 lane->blocked = false;
471 lane->waitingStep = false;
472 lane->nodeId = next;
473 blocked_ = false;
474 frame.hostBlocked = false;
475 if (auto driven = driveParallelUntilBlocked(); !driven.ok()) {
476 restoreExecutionState(std::move(before));
477 return driven;
478 }
479 if (!blocked_) {
480 auto ran = runUntilBlockedImpl();
481 if (!ran.ok()) restoreExecutionState(std::move(before));
482 return ran;
483 }
485 }
486 if (auto entered = enter(next); !entered.ok()) {
487 restoreExecutionState(std::move(before));
488 return entered;
489 }
490 auto ran = runUntilBlockedImpl();
491 if (!ran.ok()) restoreExecutionState(std::move(before));
492 return ran;
493}
494
495eve::Result<void> SequenceRuntime::select(std::string_view routeLabel) { return selectImpl(routeLabel); }
496
498 return selectImpl(routeLabel);
499}
500
501eve::Result<void> SequenceRuntime::selectImpl(std::string_view routeLabel) {
502 const SequenceNode* node = currentNode();
503 if (!node || !blocked_ || node->type != "choice")
505 "sequence: runtime is not waiting for a choice", "route");
506
507 for (const auto& route : node->routes) {
508 if (route.label != routeLabel) continue;
509 if (!route.condition.isNull()) {
510 if (!conditionEvaluator_)
512 "sequence: choice condition requires an evaluator", "route.condition");
513 SequenceConditionOutcome outcome = conditionEvaluator_(route.condition);
514 lastConditionResult_ = outcome;
515 if (!outcome.error.empty()) return fail(eve::DiagnosticCode::Failed, outcome.error, "route.condition");
516 if (!outcome.passed)
518 "sequence: choice condition rejected (" +
519 (outcome.reason.empty() ? std::string("rejected") : outcome.reason) + ")",
520 "route.condition");
521 }
522
523 ExecutionState before = captureExecutionState();
524 blocked_ = false;
525 waitingStep_ = false;
526 std::string error;
527 if (auto entered = enter(route.target); !entered.ok()) {
528 error = failureText_;
529 } else if (auto ran = runUntilBlockedImpl(); !ran.ok()) {
530 error = failureText_;
531 } else {
533 }
534 restoreExecutionState(std::move(before));
535 return fail(eve::DiagnosticCode::Failed,
536 error.empty() ? "sequence: choice selection failed" : std::move(error), "route");
537 }
539 "sequence: unknown choice route '" + std::string(routeLabel) + "'", "route");
540}
541
543 const SequenceNode* node = currentNode();
544 if (!node || !blocked_ || !waitingStep_)
546 "sequence: runtime is not waiting for a step result", "step");
547 ExecutionState before = captureExecutionState();
548
549 if (const eve::Value* key = node->payload.find("result"); key && key->isString() && !key->asString().empty())
550 locals_.set(key->asString(), result);
551 lastStepResult_ = std::move(result);
552 blocked_ = false;
553 waitingStep_ = false;
554
555 if (!parallelStack_.empty() && parallelStack_.back().hostBlocked) {
556 ParallelFrame& frame = parallelStack_.back();
557 ParallelLane* lane = nullptr;
558 for (auto& candidate : frame.lanes) {
559 if (!candidate.done && candidate.blocked && candidate.waitingStep && candidate.nodeId == nodeId_) {
560 lane = &candidate;
561 break;
562 }
563 }
564 if (!lane) {
565 restoreExecutionState(std::move(before));
567 "sequence: parallel step resume has no matching lane", "parallel");
568 }
569 lane->blocked = false;
570 lane->waitingStep = false;
571 lane->lastStepResult = lastStepResult_;
572 lane->nodeId = node->next;
573 frame.hostBlocked = false;
574 if (auto driven = driveParallelUntilBlocked(); !driven.ok()) {
575 restoreExecutionState(std::move(before));
576 return driven;
577 }
578 emit(EventKind::Resumed, node, node->type);
579 if (!blocked_) {
580 auto ran = runUntilBlockedImpl();
581 if (!ran.ok()) {
582 restoreExecutionState(std::move(before));
583 return ran;
584 }
585 }
587 }
588
589 std::string error;
590 if (auto entered = enter(node->next); !entered.ok()) {
591 error = failureText_;
592 } else if (auto ran = runUntilBlockedImpl(); !ran.ok()) {
593 error = failureText_;
594 } else {
595 emit(EventKind::Resumed, node, node->type);
597 }
598 restoreExecutionState(std::move(before));
599 return fail(eve::DiagnosticCode::Failed,
600 error.empty() ? "sequence: step resume failed" : std::move(error), "step");
601}
602
603eve::Result<void> SequenceRuntime::handleLoop(const SequenceNode& node) {
604 const eve::Value* modeValue = node.payload.find("mode");
605 if (!modeValue || !modeValue->isString())
606 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: loop missing mode", "loop.mode");
607 const std::string& mode = modeValue->asString();
608 if (node.routes.empty())
609 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: loop missing body", "loop.routes");
610
611 LoopFrame* frame = nullptr;
612 if (!loopStack_.empty() && loopStack_.back().loopNodeId == node.id) frame = &loopStack_.back();
613 if (!frame) {
614 LoopFrame created;
615 created.loopNodeId = node.id;
616 created.afterNodeId = node.next;
617 created.mode = mode;
618 if (mode == "times") {
619 const eve::Value* count = node.payload.find("count");
620 if (!count || !count->isNumeric())
621 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: loop times missing count",
622 "loop.count");
623 created.remaining = count->isInt64() ? static_cast<int>(count->asInt())
624 : static_cast<int>(count->asDouble());
625 if (created.remaining < 0) created.remaining = 0;
626 }
627 loopStack_.push_back(std::move(created));
628 frame = &loopStack_.back();
629 }
630
631 const std::string& body = node.routes.front().target;
632 if (mode == "forever") {
633 frame->entered = true;
634 return enter(body);
635 }
636 if (mode == "while") {
637 auto target = evaluateRoute(node);
638 if (!target.ok()) {
639 const auto* diagnostic = target.error();
641 diagnostic ? diagnostic->message() : "sequence: loop while evaluation failed", "loop");
642 }
643 if (target.value() == body) {
644 frame->entered = true;
645 return enter(body);
646 }
647 loopStack_.pop_back();
648 return enter(node.next);
649 }
650 if (mode == "times") {
651 if (frame->remaining <= 0) {
652 loopStack_.pop_back();
653 return enter(node.next);
654 }
655 frame->remaining -= 1;
656 frame->entered = true;
657 return enter(body);
658 }
659 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: unknown loop mode '" + mode + "'", "loop.mode");
660}
661
662eve::Result<void> SequenceRuntime::handleBreakOrContinue(const SequenceNode& node, bool isBreak) {
663 if (loopStack_.empty())
665 std::string("sequence: ") + (isBreak ? "break" : "continue") + " without an active loop",
666 isBreak ? "break" : "continue");
667 if (isBreak) {
668 const std::string after = loopStack_.back().afterNodeId;
669 loopStack_.pop_back();
670 return enter(after.empty() ? node.next : after);
671 }
672 return enter(loopStack_.back().loopNodeId);
673}
674
675eve::Result<void> SequenceRuntime::handleParallel(const SequenceNode& node) {
676 if (node.routes.size() < 2)
677 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel needs two lanes", "parallel");
678 const eve::Value* join = node.payload.find("join");
679 if (!join || !join->isString() || join->asString().empty())
680 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel missing join", "parallel.join");
681 if (parallelStack_.empty() || parallelStack_.back().parallelNodeId != node.id) {
682 ParallelFrame frame;
683 frame.parallelNodeId = node.id;
684 frame.joinNext = node.next;
685 frame.joinNodeId = join->asString();
686 for (const auto& route : node.routes) {
687 ParallelLane lane;
688 lane.nodeId = route.target;
689 frame.lanes.push_back(std::move(lane));
690 }
691 parallelStack_.push_back(std::move(frame));
692 }
693 return driveParallelUntilBlocked();
694}
695
696eve::Result<void> SequenceRuntime::handleJoin(const SequenceNode& node) {
697 if (parallelStack_.empty())
698 return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: join without parallel", "join");
699 ParallelFrame& frame = parallelStack_.back();
700 for (auto& lane : frame.lanes) {
701 if (!lane.done && lane.nodeId == node.id) lane.done = true;
702 }
703 (void)node;
704 return driveParallelUntilBlocked();
705}
706
707eve::Result<void> SequenceRuntime::stepLaneOnce(ParallelLane& lane) {
708 if (lane.done || lane.blocked) return eve::Result<void>::success();
709 if (lane.nodeId.empty()) {
710 lane.done = true;
712 }
713 const SequenceNode* node = asset_->findNode(lane.nodeId);
714 if (!node)
715 return fail(eve::DiagnosticCode::NotFound, "sequence: parallel lane missing node '" + lane.nodeId + "'",
716 "parallel.lane");
717
718 if (node->type == "join") {
719 lane.done = true;
721 }
722 if (node->type == "label") {
723 lane.nodeId = node->next;
725 }
726 if (node->type == "jump") {
727 const eve::Value* target = node->payload.find("target");
728 if (!target || !target->isString() || target->asString().empty())
729 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: jump has no target", "jump.target");
730 lane.nodeId = target->asString();
732 }
733 if (node->type == "wait") {
734 lane.blocked = true;
735 lane.waitingStep = false;
737 }
738 if (node->type == "choice" || node->type == "command" || node->type == "call" || node->type == "parallel" ||
739 node->type == "loop" || node->type == "break" || node->type == "continue" || node->type == "end" ||
740 node->type == "branch") {
741 return fail(eve::DiagnosticCode::Failed,
742 "sequence: '" + node->type + "' is not supported inside a parallel lane", "parallel.lane");
743 }
744
745 if (!registry_)
747 "sequence: step '" + node->type + "' requires a step registry", "steps." + node->id);
748 const StepContext context{asset_->id, node->id, &bindings_, &locals_, hostContext_};
749 StepOutcome outcome = registry_->dispatch(*node, context);
750 if (outcome.status == StepStatus::Failed)
751 return fail(eve::DiagnosticCode::Failed,
752 outcome.error.empty() ? "sequence: step '" + node->type + "' failed" : outcome.error,
753 "steps." + node->id);
754 if (outcome.status == StepStatus::Blocked) {
755 lane.blocked = true;
756 lane.waitingStep = true;
757 lane.lastStepResult = outcome.value;
759 }
760 lastStepResult_ = std::move(outcome.value);
761 emit(EventKind::Stepped, node, node->type);
762 lane.nodeId = node->next;
763 if (lane.nodeId.empty()) lane.done = true;
764 else if (const SequenceNode* landed = asset_->findNode(lane.nodeId); landed && landed->type == "join")
765 lane.done = true;
767}
768
769eve::Result<void> SequenceRuntime::driveParallelUntilBlocked() {
770 if (parallelStack_.empty())
771 return fail(eve::DiagnosticCode::PreconditionViolation, "sequence: no parallel frame", "parallel");
772 int budget = kExecutionBudget;
773 while (true) {
774 if (budget-- <= 0)
775 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel budget exceeded", "budget");
776 ParallelFrame& frame = parallelStack_.back();
777 bool allDone = true;
778 bool progressed = false;
779 for (std::size_t index = 0; index < frame.lanes.size(); ++index) {
780 ParallelLane& lane = frame.lanes[index];
781 if (lane.done) continue;
782 allDone = false;
783 while (!lane.done && !lane.blocked) {
784 const std::string before = lane.nodeId;
785 if (auto stepped = stepLaneOnce(lane); !stepped.ok()) return stepped;
786 if (lane.nodeId != before || lane.done || lane.blocked) progressed = true;
787 if (lane.blocked) break;
788 if (budget-- <= 0)
789 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel budget exceeded",
790 "budget");
791 }
792 }
793 if (allDone) {
794 const std::string joinNext = frame.joinNext;
795 parallelStack_.pop_back();
796 blocked_ = false;
797 waitingStep_ = false;
798 return enter(joinNext);
799 }
800 ParallelLane* blockedLane = nullptr;
801 for (auto& lane : frame.lanes) {
802 if (!lane.done && lane.blocked) {
803 blockedLane = &lane;
804 break;
805 }
806 }
807 if (blockedLane) {
808 nodeId_ = blockedLane->nodeId;
809 blocked_ = true;
810 waitingStep_ = blockedLane->waitingStep;
811 lastStepResult_ = blockedLane->lastStepResult;
812 frame.hostBlocked = true;
813 frame.activeLane = static_cast<std::size_t>(blockedLane - frame.lanes.data());
814 const SequenceNode* node = currentNode();
815 emit(EventKind::Blocked, node, node ? node->type : "parallel");
817 }
818 if (!progressed)
819 return fail(eve::DiagnosticCode::InvariantViolation, "sequence: parallel made no progress", "parallel");
820 }
821}
822
824 return resumeCommandImpl(requestId, toCanonicalValue(result));
825}
826
828 return resumeCommandImpl(requestId, std::move(result));
829}
830
831eve::Result<void> SequenceRuntime::resumeCommandImpl(std::string_view requestId, eve::Value result) {
832 const SequenceNode* node = currentNode();
833 if (!node || !blocked_ || !waitingCommand_ || node->type != "command")
834 return fail(eve::DiagnosticCode::DialogueNotWaitingForCommand, "sequence: runtime is not waiting for a command",
835 "command");
836 if (requestId.empty() || requestId != pendingCommandRequestId_)
837 return fail(eve::DiagnosticCode::Conflict, "sequence: command request id is stale or does not match",
838 "command.requestId");
839
840 ExecutionState before = captureExecutionState();
841
842 const eve::Value* resultLocal = node->payload.find("resultLocal");
843 if (!resultLocal) resultLocal = node->payload.find("result");
844 if (resultLocal && resultLocal->isString() && !resultLocal->asString().empty())
845 locals_.set(resultLocal->asString(), result);
846 lastStepResult_ = std::move(result);
847 blocked_ = false;
848 waitingCommand_ = false;
849 pendingCommandRequestId_.clear();
850
851 std::string error;
852 if (auto entered = enter(node->next); !entered.ok()) {
853 error = failureText_;
854 } else if (auto ran = runUntilBlockedImpl(); !ran.ok()) {
855 error = failureText_;
856 } else {
857 emit(EventKind::Resumed, node, "command");
859 }
860 restoreExecutionState(std::move(before));
861 return fail(eve::DiagnosticCode::Failed, error.empty() ? "sequence: command resume failed" : std::move(error),
862 "command");
863}
864
866 const auto writeLoopStack = [](const std::vector<LoopFrame>& frames) {
867 eve::Value::Array stack;
868 stack.reserve(frames.size());
869 for (const auto& frame : frames) {
871 item.set("loopNodeId", eve::Value::string(frame.loopNodeId));
872 item.set("afterNodeId", eve::Value::string(frame.afterNodeId));
873 item.set("mode", eve::Value::string(frame.mode));
874 item.set("remaining", eve::Value::integer(frame.remaining));
875 item.set("entered", eve::Value::boolean(frame.entered));
876 stack.push_back(std::move(item));
877 }
878 return eve::Value(std::move(stack));
879 };
880 const auto writeParallelStack = [](const std::vector<ParallelFrame>& frames) {
881 eve::Value::Array stack;
882 stack.reserve(frames.size());
883 for (const auto& frame : frames) {
885 item.set("parallelNodeId", eve::Value::string(frame.parallelNodeId));
886 item.set("joinNext", eve::Value::string(frame.joinNext));
887 item.set("joinNodeId", eve::Value::string(frame.joinNodeId));
888 item.set("activeLane", eve::Value::integer(static_cast<std::int64_t>(frame.activeLane)));
889 item.set("hostBlocked", eve::Value::boolean(frame.hostBlocked));
891 lanes.reserve(frame.lanes.size());
892 for (const auto& lane : frame.lanes) {
893 eve::Value laneValue = eve::Value::Object{};
894 laneValue.set("nodeId", eve::Value::string(lane.nodeId));
895 laneValue.set("done", eve::Value::boolean(lane.done));
896 laneValue.set("blocked", eve::Value::boolean(lane.blocked));
897 laneValue.set("waitingStep", eve::Value::boolean(lane.waitingStep));
898 laneValue.set("lastStepResult", lane.lastStepResult);
899 lanes.push_back(std::move(laneValue));
900 }
901 item.set("lanes", eve::Value(std::move(lanes)));
902 stack.push_back(std::move(item));
903 }
904 return eve::Value(std::move(stack));
905 };
906
908 out.set("schema", eve::Value::string(std::string(SaveSchema)));
909 out.set("schemaVersion", eve::Value::integer(SaveVersion));
910 out.set("active", eve::Value::boolean(asset_ != nullptr));
911 if (!asset_) return eve::Result<void>::success();
912 out.set("current", captureFrame(asset_, nodeId_, bindings_, locals_));
913 out.set("blocked", eve::Value::boolean(blocked_));
914 out.set("waitingStep", eve::Value::boolean(waitingStep_));
915 out.set("waitingCommand", eve::Value::boolean(waitingCommand_));
916 out.set("pendingCommandRequestId", eve::Value::string(pendingCommandRequestId_));
917 out.set("commandSequence", eve::Value::integer(static_cast<std::int64_t>(commandSequence_)));
918 out.set("loopStack", writeLoopStack(loopStack_));
919 out.set("parallelStack", writeParallelStack(parallelStack_));
920 eve::Value::Array stack;
921 stack.reserve(callStack_.size());
922 for (const auto& frame : callStack_) {
923 eve::Value item = captureFrame(frame.asset, frame.returnNode, frame.bindings, frame.locals);
924 item.set("loopStack", writeLoopStack(frame.loopStack));
925 item.set("parallelStack", writeParallelStack(frame.parallelStack));
926 stack.push_back(std::move(item));
927 }
928 out.set("stack", eve::Value(std::move(stack)));
930}
931
933 if (!in.isObject())
934 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state must be an object", "state");
935 const eve::Value* schema = in.find("schema");
936 const eve::Value* schemaVersion = in.find("schemaVersion");
937 if (!schema || !schema->isString() || schema->asString() != SaveSchema || !schemaVersion ||
938 !schemaVersion->isInt64() || schemaVersion->asInt() < MinSaveVersion ||
939 schemaVersion->asInt() > SaveVersion)
940 return fail(eve::DiagnosticCode::UnknownVersion, "sequence: unsupported runtime save schema or version",
941 "state.schemaVersion");
942 const std::int64_t restoredVersion = schemaVersion->asInt();
943 const eve::Value* active = in.find("active");
944 if (!active || !active->isBool())
945 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'active'", "state");
946 if (!active->asBool()) {
947 stop();
949 }
950 if (!assetResolver_)
952 "sequence: restore requires an asset resolver", "state");
953
954 const eve::Value* current = in.find("current");
955 if (!current || !current->isObject())
956 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'current'", "state");
957 std::string assetId;
958 std::string nodeId;
959 if (!readString(*current, "asset", assetId) || !readString(*current, "node", nodeId))
960 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: saved frame is malformed", "state.current");
961
962 const SequenceAsset* restoredAsset = assetResolver_(assetId);
963 if (!restoredAsset)
964 return fail(eve::DiagnosticCode::NotFound, "sequence: saved asset '" + assetId + "' is missing", "state.asset");
965 const eve::Value* version = current->find("version");
966 if (!version || !version->isInt64() || version->asInt() != restoredAsset->version)
968 "sequence: saved asset version does not match '" + assetId + "'", "state.version");
969 const eve::Value* savedBindings = current->find("bindings");
970 const eve::Value* savedLocals = current->find("locals");
971 if (!savedBindings || !savedBindings->isObject() || !savedLocals || !savedLocals->isObject())
972 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: saved values are malformed", "state.current");
973
974 const auto readLoopStack = [&](const eve::Value* value, std::vector<LoopFrame>& outFrames) -> eve::Result<void> {
975 outFrames.clear();
976 if (!value) return eve::Result<void>::success();
977 if (!value->isArray())
978 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: loopStack is malformed", "state.loopStack");
979 outFrames.reserve(value->arraySize());
980 for (std::size_t index = 0; index < value->arraySize(); ++index) {
981 const eve::Value& item = value->at(index);
982 LoopFrame frame;
983 if (!item.isObject() || !readString(item, "loopNodeId", frame.loopNodeId) ||
984 !readString(item, "afterNodeId", frame.afterNodeId) || !readString(item, "mode", frame.mode))
985 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: loop frame is malformed",
986 "state.loopStack");
987 const eve::Value* remaining = item.find("remaining");
988 const eve::Value* entered = item.find("entered");
989 if (!remaining || !remaining->isInt64() || !entered || !entered->isBool())
990 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: loop frame counters are malformed",
991 "state.loopStack");
992 frame.remaining = static_cast<int>(remaining->asInt());
993 frame.entered = entered->asBool();
994 outFrames.push_back(std::move(frame));
995 }
997 };
998 const auto readParallelStack = [&](const eve::Value* value,
999 std::vector<ParallelFrame>& outFrames) -> eve::Result<void> {
1000 outFrames.clear();
1001 if (!value) return eve::Result<void>::success();
1002 if (!value->isArray())
1003 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallelStack is malformed",
1004 "state.parallelStack");
1005 outFrames.reserve(value->arraySize());
1006 for (std::size_t index = 0; index < value->arraySize(); ++index) {
1007 const eve::Value& item = value->at(index);
1008 ParallelFrame frame;
1009 if (!item.isObject() || !readString(item, "parallelNodeId", frame.parallelNodeId) ||
1010 !readString(item, "joinNext", frame.joinNext) || !readString(item, "joinNodeId", frame.joinNodeId))
1011 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallel frame is malformed",
1012 "state.parallelStack");
1013 const eve::Value* activeLane = item.find("activeLane");
1014 const eve::Value* hostBlocked = item.find("hostBlocked");
1015 const eve::Value* lanes = item.find("lanes");
1016 if (!activeLane || !activeLane->isInt64() || !hostBlocked || !hostBlocked->isBool() || !lanes ||
1017 !lanes->isArray())
1018 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallel frame lanes are malformed",
1019 "state.parallelStack");
1020 frame.activeLane = static_cast<std::size_t>(activeLane->asInt());
1021 frame.hostBlocked = hostBlocked->asBool();
1022 for (std::size_t laneIndex = 0; laneIndex < lanes->arraySize(); ++laneIndex) {
1023 const eve::Value& laneValue = lanes->at(laneIndex);
1024 ParallelLane lane;
1025 if (!laneValue.isObject() || !readString(laneValue, "nodeId", lane.nodeId))
1026 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallel lane is malformed",
1027 "state.parallelStack");
1028 const eve::Value* done = laneValue.find("done");
1029 const eve::Value* blocked = laneValue.find("blocked");
1030 const eve::Value* waitingStep = laneValue.find("waitingStep");
1031 const eve::Value* lastResult = laneValue.find("lastStepResult");
1032 if (!done || !done->isBool() || !blocked || !blocked->isBool() || !waitingStep ||
1033 !waitingStep->isBool())
1034 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: parallel lane flags are malformed",
1035 "state.parallelStack");
1036 lane.done = done->asBool();
1037 lane.blocked = blocked->asBool();
1038 lane.waitingStep = waitingStep->asBool();
1039 if (lastResult) lane.lastStepResult = *lastResult;
1040 frame.lanes.push_back(std::move(lane));
1041 }
1042 outFrames.push_back(std::move(frame));
1043 }
1045 };
1046
1047 const eve::Value* stack = in.find("stack");
1048 if (!stack || !stack->isArray())
1049 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'stack'", "state");
1050 std::vector<Frame> restoredStack;
1051 restoredStack.reserve(stack->arraySize());
1052 for (std::size_t index = 0; index < stack->arraySize(); ++index) {
1053 const eve::Value& saved = stack->at(index);
1054 std::string savedAssetId;
1055 std::string returnNode;
1056 if (!saved.isObject() || !readString(saved, "asset", savedAssetId) ||
1057 !readString(saved, "node", returnNode))
1058 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: saved call frame is malformed",
1059 "state.stack");
1060 const SequenceAsset* savedAsset = assetResolver_(savedAssetId);
1061 const eve::Value* savedVersion = saved.find("version");
1062 const eve::Value* frameBindings = saved.find("bindings");
1063 const eve::Value* frameLocals = saved.find("locals");
1064 if (!savedAsset || !savedVersion || !savedVersion->isInt64() ||
1065 savedVersion->asInt() != savedAsset->version || !frameBindings || !frameBindings->isObject() ||
1066 !frameLocals || !frameLocals->isObject() || !savedAsset->findNode(returnNode))
1067 return fail(eve::DiagnosticCode::Conflict, "sequence: saved call frame cannot be restored", "state.stack");
1068 Frame frame{savedAsset, returnNode, *frameBindings, *frameLocals, {}, {}};
1069 if (restoredVersion >= 3) {
1070 if (auto loops = readLoopStack(saved.find("loopStack"), frame.loopStack); !loops.ok()) return loops;
1071 if (auto parallels = readParallelStack(saved.find("parallelStack"), frame.parallelStack); !parallels.ok())
1072 return parallels;
1073 }
1074 restoredStack.push_back(std::move(frame));
1075 }
1076 std::vector<LoopFrame> restoredLoops;
1077 std::vector<ParallelFrame> restoredParallels;
1078 if (restoredVersion >= 3) {
1079 if (auto loops = readLoopStack(in.find("loopStack"), restoredLoops); !loops.ok()) return loops;
1080 if (auto parallels = readParallelStack(in.find("parallelStack"), restoredParallels); !parallels.ok())
1081 return parallels;
1082 }
1083 const eve::Value* blocked = in.find("blocked");
1084 if (!blocked || !blocked->isBool())
1085 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: runtime state is missing 'blocked'", "state");
1086 const eve::Value* waitingStep = in.find("waitingStep");
1087 if (waitingStep && !waitingStep->isBool())
1088 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: waitingStep is malformed", "state");
1089 const eve::Value* waitingCommand = in.find("waitingCommand");
1090 if (waitingCommand && !waitingCommand->isBool())
1091 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: waitingCommand is malformed", "state");
1092 const eve::Value* pendingRequestId = in.find("pendingCommandRequestId");
1093 const eve::Value* commandSequence = in.find("commandSequence");
1094 if (!pendingRequestId || !pendingRequestId->isString() || !commandSequence || !commandSequence->isInt64() ||
1095 commandSequence->asInt() < 1)
1096 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: pending command state is malformed", "state");
1097 const bool restoredWaitingCommand = waitingCommand && waitingCommand->asBool();
1098 if (restoredWaitingCommand != !pendingRequestId->asString().empty())
1100 "sequence: pending command identity does not match waiting state", "state");
1101 if (!restoredAsset->findNode(nodeId))
1102 return fail(eve::DiagnosticCode::NotFound, "sequence: saved node '" + nodeId + "' is missing", "state.node");
1103 const SequenceNode* restoredNode = restoredAsset->findNode(nodeId);
1104 if (restoredWaitingCommand && (!restoredNode || restoredNode->type != "command"))
1105 return fail(eve::DiagnosticCode::InvalidArgument, "sequence: pending command cursor is not a command node",
1106 "state.node");
1107
1108 asset_ = restoredAsset;
1109 nodeId_ = std::move(nodeId);
1110 bindings_ = *savedBindings;
1111 locals_ = *savedLocals;
1112 blocked_ = blocked->asBool();
1113 waitingStep_ = waitingStep && waitingStep->asBool();
1114 waitingCommand_ = restoredWaitingCommand;
1115 pendingCommandRequestId_ = pendingRequestId->asString();
1116 commandSequence_ = static_cast<std::uint64_t>(commandSequence->asInt());
1117 callStack_ = std::move(restoredStack);
1118 loopStack_ = std::move(restoredLoops);
1119 parallelStack_ = std::move(restoredParallels);
1120 failureText_.clear();
1121 lastCommandRequest_.reset();
1122 if (waitingCommand_) {
1124 request.requestId = pendingCommandRequestId_;
1125 const eve::Value* name = restoredNode->payload.find("name");
1126 if (!name) name = restoredNode->payload.find("target");
1127 request.name = name && name->isString() ? name->asString() : std::string{};
1128 if (const eve::Value* arguments = restoredNode->payload.find("arguments"); arguments && arguments->isObject())
1129 request.arguments = *arguments;
1130 request.bindings = bindings_;
1131 request.locals = locals_;
1132 request.payload = restoredNode->payload;
1133 lastCommandRequest_ = std::move(request);
1134 emit(EventKind::Blocked, restoredNode, "command");
1135 }
1137}
1138
1139} // namespace eve::dnut
LogicalId target
bool & active
std::vector< std::vector< CompiledStatement > > lanes
Parallel lane bodies (parallel { lane {…} lane {…} }).
std::string nodeId
std::vector< Value > defaults
Definition ECS.cpp:49
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::string lastResult
std::string message
DiagnosticCode code
const GltfImportRequest & request
std::string id
std::string response
Poco::JSON::Object::Ptr result
bool done
HSQOBJECT handler
ssq::Object frame
std::string error
Definition Package.cpp:174
std::vector< ParticleBakeMaterial > bindings
std::string path
Definition PlayHost.cpp:111
std::uint32_t targetId
std::string body
Object::Ptr value
int detail
std::string parameter
const RoadNode * node
int created
ScriptStructure out
std::string kind
Cross-frame interpreter for compiled `.dnut` sequences.
double current
std::string string
int count
uint32_t index
const VegetationPresetContext & context
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:514
void value() const
Mark a successful void operation as checked.
Definition Result.h:560
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:30
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
bool isInt64() const noexcept
Return true when this value is an Int64.
Definition Value.h:102
std::map< std::string, Value > Object
Definition Value.h:34
bool isArray() const noexcept
Return true when this value is an array.
Definition Value.h:108
std::vector< std::string > keys() const
Return object member names in deterministic order.
Definition Value.cpp:258
void set(const std::string &key, Value value)
Insert or replace an object member.
Definition Value.cpp:238
static Value number(double value)
Compatibility factory for a Double value.
Definition Value.h:117
bool isString() const noexcept
Return true when this value is a string.
Definition Value.h:106
static Value string(std::string value)
Compatibility factory for a string value.
Definition Value.h:121
std::int64_t asInt() const
Return the Int64 payload; the caller must have checked the kind.
Definition Value.cpp:189
std::vector< Value > Array
Definition Value.h:33
static Value integer(std::int64_t value)
Compatibility factory for an Int64 value.
Definition Value.h:115
std::size_t arraySize() const
Return the number of array elements.
Definition Value.cpp:220
static Value boolean(bool value)
Compatibility factory for a boolean value.
Definition Value.h:119
bool asBool() const
Return the boolean payload; the caller must have checked the kind.
Definition Value.cpp:201
bool isBool() const noexcept
Return true when this value is a boolean.
Definition Value.h:100
bool isObject() const noexcept
Return true when this value is an object.
Definition Value.h:110
Value & at(std::size_t index)
Return a bounds-checked array element.
Definition Value.cpp:226
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
eve::Result< void > runUntilBlocked()
Execute nodes until the sequence blocks, ends or fails.
const SequenceNode * currentNode() const
Borrowed current node, or nullptr. @lifetime Invalidated by the next transition.
static constexpr std::int64_t MinSaveVersion
Oldest restoreable cursor save schema version.
void stop()
Stop and clear the active sequence without emitting Ended.
static constexpr std::string_view SaveSchema
Stable cursor save schema retained across the L1 ownership migration.
eve::Result< void > captureState(eve::Value &out) const
Capture the complete cursor, bindings, locals and call stack.
static constexpr std::int64_t SaveVersion
Current cursor save schema version (v3 adds loop/parallel stacks).
static constexpr int kExecutionBudget
Upper bound on nodes executed by one runUntilBlocked call.
eve::Result< void > advance()
Acknowledge a host-presented wait or handler-less Await step.
eve::Result< void > resumeCommand(std::string_view requestId, eve::StateValue result)
Resume the exact blocked command with a legacy state value.
eve::Result< void > start(const SequenceAsset *asset, eve::Value bindings=eve::Value::Object{})
Begin a sequence at the asset entry node.
void unregisterCommand(std::string_view name)
Remove a named generic command handler.
void registerCommand(std::string name, SequenceCommandHandler handler)
Register a named generic command handler.
eve::Result< void > resumeStep(eve::Value result)
Resume a step whose handler returned Blocked.
const SequenceAsset * asset() const
Borrowed active asset, or nullptr. @lifetime Owned by the caller of start.
const eve::Value & bindings() const
eve::Result< void > selectRouteForTransaction(std::string_view routeLabel)
Enter one already prepared choice route transactionally.
eve::Result< void > select(std::string_view routeLabel)
Answer a blocked choice node by route label.
eve::Result< void > restoreState(const eve::Value &in)
Restore a captured cursor through the configured asset resolver.
StepOutcome dispatch(const SequenceNode &node, const StepContext &context) const
Route one already validated node to its handler.
eve::Value toCanonicalValue(const eve::StateValue &value)
Convert the legacy reload value into the canonical common value.
const char * sequenceParameterTypeName(SequenceParameterType type) noexcept
Return the stable lowercase spelling of a parameter type.
SequenceParameterType
Declared value kind for one sequence invocation parameter.
std::function< SequenceCommandResponse(const SequenceCommandRequest &)> SequenceCommandHandler
Synchronous owner-thread handler for one command request.
void keys(const Value &value, std::initializer_list< const char * > names)
bool readString(const eve::Value::Object &object, const char *name, std::string &output)
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
@ DialogueNotWaitingForCommand
Dialogue runner is not positioned at an asynchronous command.
Immutable, parameterized and versioned compiled sequence.
eve::Result< void > validate() const
Validate stable identifiers, the entry point and every graph reference.
const SequenceNode * findNode(const std::string &nodeId) const noexcept
Find one node by its stable identifier.
std::vector< SequenceParameter > parameters
Immutable request emitted by a generic command node.
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.