hyacinth-ai 0.9.14 → 0.9.15
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.
- package/dist/machine/runner.js +19 -20
- package/package.json +1 -1
package/dist/machine/runner.js
CHANGED
|
@@ -77,46 +77,45 @@ export class MachineRunner {
|
|
|
77
77
|
if (this.status !== 'active') {
|
|
78
78
|
return { ok: false, reason: `Machine "${this.definition.id}" is not active (status: ${this.status}).` };
|
|
79
79
|
}
|
|
80
|
-
// 1.
|
|
81
|
-
|
|
80
|
+
// 1. 找匹配的转移(按优先级——跳过 guard 不通过的)
|
|
81
|
+
let transition;
|
|
82
|
+
for (const t of this.definition.transitions) {
|
|
82
83
|
const fromMatch = Array.isArray(t.from)
|
|
83
84
|
? t.from.includes(this.currentState)
|
|
84
85
|
: t.from === this.currentState;
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
if (fromMatch && t.event === event) {
|
|
87
|
+
if (t.guard) {
|
|
88
|
+
const guardResult = t.guard(this.context);
|
|
89
|
+
if (!guardResult.ok)
|
|
90
|
+
continue; // guard 不通过,试下一个
|
|
91
|
+
}
|
|
92
|
+
transition = t;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
87
96
|
if (!transition) {
|
|
88
97
|
return {
|
|
89
98
|
ok: false,
|
|
90
99
|
reason: `Event "${event}" is not valid in state "${this.currentState}" of machine "${this.definition.id}". Available events: [${this.getAvailableEvents().join(', ')}]`,
|
|
91
100
|
};
|
|
92
101
|
}
|
|
93
|
-
// 2.
|
|
94
|
-
if (transition.guard) {
|
|
95
|
-
const guardResult = transition.guard(this.context);
|
|
96
|
-
if (!guardResult.ok) {
|
|
97
|
-
return {
|
|
98
|
-
ok: false,
|
|
99
|
-
reason: guardResult.reason ?? `Guard rejected transition from "${this.currentState}" to "${transition.to}" via "${event}".`,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// 3. onExit 副作用(fire-and-forget)
|
|
102
|
+
// 2. onExit 副作用(fire-and-forget)
|
|
104
103
|
const oldState = this.currentState;
|
|
105
104
|
const oldDef = this.definition.states[oldState];
|
|
106
105
|
safeFire(oldDef?.onExit, this.context);
|
|
107
|
-
//
|
|
106
|
+
// 3. onTransition 副作用(同步执行——可阻塞)
|
|
108
107
|
transition.onTransition?.(this.context);
|
|
109
|
-
//
|
|
108
|
+
// 4. 更新状态
|
|
110
109
|
this.currentState = transition.to;
|
|
111
|
-
//
|
|
110
|
+
// 5. onEnter 副作用(fire-and-forget)
|
|
112
111
|
const newDef = this.definition.states[this.currentState];
|
|
113
112
|
safeFire(newDef?.onEnter, this.context);
|
|
114
|
-
//
|
|
113
|
+
// 6. 记录历史
|
|
115
114
|
this.transitionHistory.push({ from: oldState, to: transition.to, event });
|
|
116
115
|
if (this.transitionHistory.length > MAX_HISTORY) {
|
|
117
116
|
this.transitionHistory = this.transitionHistory.slice(-MAX_HISTORY);
|
|
118
117
|
}
|
|
119
|
-
//
|
|
118
|
+
// 7. 终端状态检查
|
|
120
119
|
const isTerminal = this.definition.terminalStates?.includes(this.currentState) ?? false;
|
|
121
120
|
if (isTerminal) {
|
|
122
121
|
this.status = 'completed';
|