instant-ctrl-flow-logic 0.1.1 → 0.1.3
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/package.json +5 -4
- package/src/filesystem.ts +17 -0
- package/src/handlers.ts +11 -4
- package/src/loader.ts +23 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "instant-ctrl-flow-logic",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "InstantCtrlFlow 引擎:xnl bundle 加载 → 行为树编译 → 无状态单次执行",
|
|
6
6
|
"type": "module",
|
|
@@ -12,14 +12,15 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"depa-behavior-tree": "0.1.0",
|
|
15
|
-
"
|
|
15
|
+
"flow-step-space-contract": "0.1.0",
|
|
16
|
+
"flow-step-space-logic": "0.1.0",
|
|
17
|
+
"instant-ctrl-flow-contract": "0.1.1",
|
|
16
18
|
"xnl-core": "^0.1.8"
|
|
17
19
|
},
|
|
18
20
|
"files": [
|
|
19
21
|
"src"
|
|
20
22
|
],
|
|
21
23
|
"publishConfig": {
|
|
22
|
-
"access": "public"
|
|
23
|
-
"registry": "https://registry.npmjs.com"
|
|
24
|
+
"access": "public"
|
|
24
25
|
}
|
|
25
26
|
}
|
package/src/filesystem.ts
CHANGED
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
4
|
import type { FlowCodeFunction, FlowCodeResolver, LoadFlowSourcesOptions } from 'instant-ctrl-flow-contract';
|
|
5
5
|
import { loadFlowBundleFromSources, type LoadResult } from './loader.js';
|
|
6
|
+
import { assembleDefinitionStepProfileDirectory } from 'flow-step-space-logic/filesystem';
|
|
6
7
|
import { FlowLoadError, runInstantCtrlFlowSpec } from './engine.js';
|
|
7
8
|
import type { InstantCtrlFlowRunOptions } from 'instant-ctrl-flow-contract';
|
|
8
9
|
|
|
@@ -18,6 +19,22 @@ export function loadFlowBundle(dir: string, options: LoadFlowSourcesOptions = {}
|
|
|
18
19
|
content: fs.readFileSync(path.join(absoluteDir, entry.name), 'utf8'),
|
|
19
20
|
}))
|
|
20
21
|
: [];
|
|
22
|
+
for (const profileRoot of ['InstantCtrlFlow', 'WorkCtrlFlow', 'BPCtrlFlow']) {
|
|
23
|
+
const assembled = assembleDefinitionStepProfileDirectory(absoluteDir, profileRoot, options.stepSpace);
|
|
24
|
+
if (assembled.diagnostics.length > 0) {
|
|
25
|
+
return { diagnostics: assembled.diagnostics.map(({ code, message }) => ({ code, message })) };
|
|
26
|
+
}
|
|
27
|
+
if (!assembled.forest) continue;
|
|
28
|
+
const projectedSources = (assembled.sources as readonly ({ readonly ref: string; readonly content: string } | { readonly name: string; readonly content: string })[])
|
|
29
|
+
.map((source) => ({ name: 'name' in source ? source.name : source.ref, content: source.content }));
|
|
30
|
+
const loaded = loadFlowBundleFromSources(projectedSources, {
|
|
31
|
+
...options,
|
|
32
|
+
baseUri: options.baseUri ?? absoluteDir,
|
|
33
|
+
});
|
|
34
|
+
return loaded.spec
|
|
35
|
+
? { ...loaded, spec: { ...loaded.spec, definitionStepForest: assembled.forest } }
|
|
36
|
+
: loaded;
|
|
37
|
+
}
|
|
21
38
|
return loadFlowBundleFromSources(sources, { ...options, baseUri: options.baseUri ?? absoluteDir });
|
|
22
39
|
}
|
|
23
40
|
|
package/src/handlers.ts
CHANGED
|
@@ -93,6 +93,7 @@ export interface FlowDecisionContext {
|
|
|
93
93
|
export interface FlowDecisionHooks {
|
|
94
94
|
begin(ifPath: string): FlowDecisionContext;
|
|
95
95
|
record(decision: FlowBranchDecision): Promise<void> | void;
|
|
96
|
+
runtimeForNode?(nodeId: string, runtime: InstantCtrlFlowRuntime): InstantCtrlFlowRuntime;
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
class FlowFunctionLoader {
|
|
@@ -100,8 +101,13 @@ class FlowFunctionLoader {
|
|
|
100
101
|
constructor(
|
|
101
102
|
private spec: FlowBundleSpec,
|
|
102
103
|
private resolveCode: FlowCodeResolver,
|
|
104
|
+
private runtimeHooks?: FlowDecisionHooks,
|
|
103
105
|
) {}
|
|
104
106
|
|
|
107
|
+
runtimeForNode(nodeId: string, runtime: InstantCtrlFlowRuntime): InstantCtrlFlowRuntime {
|
|
108
|
+
return this.runtimeHooks?.runtimeForNode?.(nodeId, runtime) ?? runtime;
|
|
109
|
+
}
|
|
110
|
+
|
|
105
111
|
load(src: string, nodeId: string): Promise<FlowCodeFunction> {
|
|
106
112
|
let loaded = this.cache.get(src);
|
|
107
113
|
if (!loaded) {
|
|
@@ -128,7 +134,8 @@ function configOf(ctx: Ctx): NodeConfig {
|
|
|
128
134
|
async function callCode(ctx: Ctx, loader: FlowFunctionLoader, src: string): Promise<unknown> {
|
|
129
135
|
const state = flowStateOf(ctx);
|
|
130
136
|
const fn = await loader.load(src, ctx.node.Key);
|
|
131
|
-
|
|
137
|
+
const runtime = (ctx.runtime ?? {}) as InstantCtrlFlowRuntime;
|
|
138
|
+
return fn(loader.runtimeForNode(ctx.node.Key, runtime), state.current, configOf(ctx).config ?? {});
|
|
132
139
|
}
|
|
133
140
|
|
|
134
141
|
function fail(state: FlowExecutionState, error: unknown): BehaviorResult {
|
|
@@ -277,7 +284,7 @@ class StrategyStartHandler implements ActionHandler<CtrlFlowNodeType, INodeConfi
|
|
|
277
284
|
if (state.returned) return BehaviorResult.Success;
|
|
278
285
|
const key = String(configOf(ctx).fallbackKey);
|
|
279
286
|
state.current = state.fallbackInputs[key];
|
|
280
|
-
state.fault
|
|
287
|
+
delete state.fault;
|
|
281
288
|
return BehaviorResult.Success;
|
|
282
289
|
}
|
|
283
290
|
}
|
|
@@ -288,7 +295,7 @@ class FallbackStartHandler implements ActionHandler<CtrlFlowNodeType, INodeConfi
|
|
|
288
295
|
if (state.returned) return BehaviorResult.Success;
|
|
289
296
|
const key = String(configOf(ctx).fallbackKey);
|
|
290
297
|
state.fallbackInputs[key] = state.current;
|
|
291
|
-
state.fault
|
|
298
|
+
delete state.fault;
|
|
292
299
|
return BehaviorResult.Success;
|
|
293
300
|
}
|
|
294
301
|
}
|
|
@@ -440,7 +447,7 @@ export function createFlowHandlers(
|
|
|
440
447
|
executeBody?: FlowBodyExecutor,
|
|
441
448
|
executeTarget?: FlowTargetExecutor,
|
|
442
449
|
): Record<string, ActionHandler<CtrlFlowNodeType, INodeConfig>> {
|
|
443
|
-
const loader = new FlowFunctionLoader(spec, resolveCode);
|
|
450
|
+
const loader = new FlowFunctionLoader(spec, resolveCode, decisionHooks);
|
|
444
451
|
const activeDecisions = new Map<string, FlowDecisionContext>();
|
|
445
452
|
return {
|
|
446
453
|
'flow.Run': new RunHandler(loader),
|
package/src/loader.ts
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* 完整 lint 由 tools/flow-dsl-check 承担;此处只做引擎必需的结构化与硬校验。
|
|
5
5
|
*/
|
|
6
6
|
import { parseXnl } from 'xnl-core';
|
|
7
|
+
import { assembleDefinitionStepProfileSources } from 'flow-step-space-logic';
|
|
8
|
+
import type { DefinitionStepForest } from 'flow-step-space-contract';
|
|
7
9
|
import type {
|
|
8
10
|
FlowBundleSpec,
|
|
9
11
|
CtrlFlowContract,
|
|
@@ -167,9 +169,26 @@ function loadCtrlFlowSources(
|
|
|
167
169
|
}
|
|
168
170
|
};
|
|
169
171
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
172
|
+
let definitionStepForest: DefinitionStepForest | undefined;
|
|
173
|
+
let effectiveSources = sources;
|
|
174
|
+
for (const candidateRoot of profile ? [profile.rootTag] : FORMS) {
|
|
175
|
+
const assembled = assembleDefinitionStepProfileSources(sources, candidateRoot, options.stepSpace);
|
|
176
|
+
if (assembled.diagnostics.length > 0) {
|
|
177
|
+
return { diagnostics: assembled.diagnostics.map(({ code, message }) => ({ code, message })) };
|
|
178
|
+
}
|
|
179
|
+
if (assembled.forest) {
|
|
180
|
+
effectiveSources = assembled.sources as FlowSourceCollection;
|
|
181
|
+
definitionStepForest = assembled.forest;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const sourceEntries = (Array.isArray(effectiveSources)
|
|
187
|
+
? effectiveSources.map((source) => ({
|
|
188
|
+
name: 'name' in source ? source.name : source.ref,
|
|
189
|
+
content: source.content,
|
|
190
|
+
}))
|
|
191
|
+
: Object.entries(effectiveSources).map(([name, content]) => ({ name, content })))
|
|
173
192
|
.filter(({ name }) => name.endsWith('.xnl'))
|
|
174
193
|
.sort((left, right) => left.name.localeCompare(right.name));
|
|
175
194
|
const sourceNames = sourceEntries.map(({ name }) => name);
|
|
@@ -408,6 +427,7 @@ function loadCtrlFlowSources(
|
|
|
408
427
|
baseUri: options.baseUri,
|
|
409
428
|
dir: options.baseUri ?? '',
|
|
410
429
|
statements: statementEls.map(toNodeSpec),
|
|
430
|
+
...(definitionStepForest ? { definitionStepForest } : {}),
|
|
411
431
|
flowContract,
|
|
412
432
|
config,
|
|
413
433
|
configDef,
|