Let a Start input or a variable name the state machine it carries
A sub-behavior that gets its machine through a Start input had no state ports on Is in state, Set state and the other state nodes. Their ports came only from an inline pick or a State machine literal, so a machine handed in from outside left them empty. - A statemachine port of a Start node can now name a machine (DeclaredPort.machine). The inspector shows the pick next to the port name. SetPortMachineCommand sets it with undo. - machineIdOf follows an edge from such a port to that machine, so the state nodes it feeds offer its states. - A Run behavior node has the machine picked in advance on that input. - A different pick there is allowed, but the node warns, because the called graph asks for the states of its own machine. - A statemachine variable of a Variables node can name a machine the same way, picked next to the variable's row. - The inspector signature of a node now includes the picked machine. Before, the pick changed the ports but the dropdown kept its text until the node was selected again. - A local machine handed in still starts in no state every call. That rule from #229 is unchanged. Tests: - tests/core/start-machine.test.ts (7): ports with and without a machine and undo, save and load, the preset value moving a shared machine through the call, the warning, a port of another kind, the inspector signature, a Variables node. - Counter checks: without the new branch in machineIdOf 2 tests fail, without the machine in the signature 1 test fails. - Gate on melektaus-atx: 239 files, 3599 tests, CHECK EXIT 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3f12a77aea
commit
d80d5cce26
8 files changed
+284
-10
No files matched your search
@@ -279,6 +279,48 @@ export class SetPortWriteCommand implements Command {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Says which machine a `statemachine` port carries – an input of a start node, or a variable a
|
||||
* Variables node reads; empty says none. Edges on the states of the one before stay where they are
|
||||
* and hang there, as when a machine loses a state.
|
||||
*/
|
||||
export class SetPortMachineCommand implements Command {
|
||||
readonly label: string;
|
||||
private was: string | undefined;
|
||||
|
||||
constructor(
|
||||
private readonly assetId: string,
|
||||
private readonly nodeId: string,
|
||||
private readonly key: string,
|
||||
private readonly machine: string,
|
||||
) {
|
||||
this.label = `Set machine of ${key}`;
|
||||
}
|
||||
|
||||
execute(project: Project): void {
|
||||
const node = requireNode(project, this.assetId, this.nodeId);
|
||||
if (!isEntryTerminal(node.type) && !declaresVariablePorts(node.type))
|
||||
throw new CommandError('Only a start node or a Variables node carries a machine');
|
||||
const port = (node.ports ?? []).find((p) => p.key === this.key);
|
||||
if (!port) throw new CommandError(`Port "${this.key}" not found`);
|
||||
if (port.kind !== 'statemachine') throw new CommandError(`"${this.key}" is not a state machine`);
|
||||
this.was = port.machine;
|
||||
this.mark(node, this.machine);
|
||||
}
|
||||
|
||||
undo(project: Project): void {
|
||||
this.mark(requireNode(project, this.assetId, this.nodeId), this.was ?? '');
|
||||
}
|
||||
|
||||
private mark(node: GraphNode, machine: string): void {
|
||||
node.ports = (node.ports ?? []).map((port) => {
|
||||
if (port.key !== this.key) return port;
|
||||
const { machine: _, ...plain } = port;
|
||||
return machine ? { ...plain, machine } : plain;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Rewrites the edges and inline values pointing at the port too, in every graph that has any. */
|
||||
export class RenamePortCommand extends RenameCommand {
|
||||
constructor(
|
||||
|
||||
@@ -55,6 +55,11 @@ export interface DeclaredPort {
|
||||
kind: DeclaredPortKind;
|
||||
/** On a node declaring `both` sides: the port is an input too, so the node writes it. */
|
||||
write?: true;
|
||||
/**
|
||||
* On a `statemachine` port: the machine it carries. The state nodes it feeds take their states
|
||||
* from it. On a start node, a node running the behavior has it picked in advance as well.
|
||||
*/
|
||||
machine?: string;
|
||||
}
|
||||
|
||||
export function declaredPortType(kind: DeclaredPortKind): PortType {
|
||||
|
||||
@@ -156,8 +156,9 @@ export function isFlowPort(port: DeclaredPort): boolean {
|
||||
*/
|
||||
export function portDefOf(port: DeclaredPort): PortDef {
|
||||
const def: PortDef = { key: port.key, label: port.key, type: declaredPortType(port.kind) };
|
||||
// The machine a start node's input carries is what the field offers before anyone picks.
|
||||
return isTaggedKind(port.kind) && port.kind !== 'object'
|
||||
? { ...def, field: flatDef(port.key, port.kind) }
|
||||
? { ...def, field: flatDef(port.key, port.kind, { value: port.machine }) }
|
||||
: def;
|
||||
}
|
||||
|
||||
@@ -360,6 +361,20 @@ export function callProblem(node: GraphNode, lookup: GraphLookup, ownerId = ''):
|
||||
const program = lookup.behavior(id);
|
||||
if (!program) return 'That behavior is gone';
|
||||
if (callEntries(program.graph).length === 0) return 'No start or On start node – does nothing';
|
||||
return otherMachineProblem(node, program.graph, lookup);
|
||||
}
|
||||
|
||||
/**
|
||||
* A machine picked here other than the one the start node's input carries: the called graph asks for
|
||||
* the states of its own, so every one of them would read as never entered.
|
||||
*/
|
||||
function otherMachineProblem(node: GraphNode, graph: Graph, lookup: GraphLookup): string | null {
|
||||
for (const port of terminalNode(graph, BEHAVIOR_START)?.ports ?? []) {
|
||||
const picked = textOf(node.params[port.key] ?? '');
|
||||
if (port.kind !== 'statemachine' || !port.machine || !picked || picked === port.machine) continue;
|
||||
const machine = lookup.machine(port.machine);
|
||||
return `"${port.key}" expects ${machine ? `"${machine.name}"` : 'a machine that is gone'}`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -127,15 +127,17 @@ export const machinePort = (): PortDef => ({
|
||||
});
|
||||
|
||||
/**
|
||||
* The machine asset a node's `machine` input names while the graph is drawn: the inline pick, or the
|
||||
* `State machine` literal an edge brings in. A port is derived from the graph, not from a running
|
||||
* value, so a variable or a list item feeding the input yields nothing.
|
||||
* The machine asset a node's `machine` input names while the graph is drawn: the inline pick, the
|
||||
* `State machine` literal an edge brings in, or the machine a declared port says it carries – an
|
||||
* input of a behavior's start node or a variable of a Variables node. A port is derived from the
|
||||
* graph, not from a running value, so anything else feeding the input yields nothing.
|
||||
*/
|
||||
export function machineIdOf({ node, graph }: PortSource): string {
|
||||
const edge = edgesInto(graph, node.id, MACHINE_PORT)[0];
|
||||
const source = edge && graph.nodes.find((n) => n.id === edge.from.node);
|
||||
if (!edge) return textOf(node.params[MACHINE_PORT] ?? '');
|
||||
return source?.type === STATE_MACHINE ? textOf(source.params.value ?? '') : '';
|
||||
if (source?.type === STATE_MACHINE) return textOf(source.params.value ?? '');
|
||||
return source?.ports?.find((p) => p.key === edge.from.port)?.machine ?? '';
|
||||
}
|
||||
|
||||
/** What that machine is, looked up – null where the input names none, or none that exists. */
|
||||
|
||||
@@ -480,7 +480,11 @@ function normalizePorts(raw: unknown, fixed: readonly string[], writable: boolea
|
||||
const kind = (DECLARED_PORT_KINDS as readonly string[]).includes(String(p.kind))
|
||||
? (p.kind as DeclaredPortKind)
|
||||
: 'number';
|
||||
ports.push(writable && p.write === true ? { key, kind, write: true } : { key, kind });
|
||||
const port: DeclaredPort = writable && p.write === true ? { key, kind, write: true } : { key, kind };
|
||||
// A machine that is gone is kept, as a struct type is: the port then offers no states.
|
||||
if (kind === 'statemachine' && typeof p.machine === 'string' && p.machine !== '')
|
||||
port.machine = p.machine;
|
||||
ports.push(port);
|
||||
}
|
||||
return ports;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user