@psimona/darkf 0.0.0-stage → 0.4.0
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/README.md +28 -2
- package/bunfig.toml +13 -0
- package/examples/article-classifier/data/articles.ts +38 -0
- package/examples/article-classifier/scripts/next.ts +7 -0
- package/examples/article-classifier/scripts/queue.ts +33 -0
- package/examples/article-classifier/scripts/record.ts +9 -0
- package/examples/article-classifier/workflow.ts +44 -0
- package/examples/haiku-commit/scripts/check.ts +24 -0
- package/examples/haiku-commit/workflow.ts +28 -0
- package/examples/haiku-readme/scripts/test.ts +13 -0
- package/examples/haiku-readme/scripts/write.ts +10 -0
- package/examples/haiku-readme/workflow.ts +41 -0
- package/examples/task-queue/scripts/next.ts +20 -0
- package/examples/task-queue/scripts/record.ts +20 -0
- package/examples/task-queue/supervise.ts +57 -0
- package/examples/task-queue/workflow.ts +33 -0
- package/feedback/2026-10-30-self-update.md +29 -0
- package/package.json +30 -4
- package/src/cli/detached.test.ts +560 -0
- package/src/cli/index.ts +1040 -0
- package/src/cli/update.test.ts +486 -0
- package/src/cli/update.ts +191 -0
- package/src/core/graph.test.ts +172 -0
- package/src/core/graph.ts +148 -0
- package/src/core/index.ts +4 -0
- package/src/core/journal.ts +21 -0
- package/src/core/mermaid.test.ts +34 -0
- package/src/core/mermaid.ts +95 -0
- package/src/core/types.ts +279 -0
- package/src/engine/eventLog.ts +17 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/run.test.ts +1068 -0
- package/src/engine/run.ts +359 -0
- package/src/exports.ts +2 -0
- package/src/kit/agents/claude-code.test.ts +37 -0
- package/src/kit/agents/claude-code.ts +180 -0
- package/src/kit/agents/handoff.test.ts +128 -0
- package/src/kit/agents/handoff.ts +69 -0
- package/src/kit/agents/opencode.test.ts +136 -0
- package/src/kit/agents/opencode.ts +327 -0
- package/src/kit/registry.test.ts +386 -0
- package/src/kit/registry.ts +296 -0
- package/src/kit/status.test.ts +423 -0
- package/src/kit/status.ts +282 -0
- package/tsconfig.json +4 -0
- package/workflows/dev-loop/scripts/begin.ts +22 -0
- package/workflows/dev-loop/scripts/check.ts +19 -0
- package/workflows/dev-loop/scripts/finish.ts +114 -0
- package/workflows/dev-loop/workflow.ts +135 -0
- package/workflows/series-ordering/scripts/verify.ts +18 -0
- package/workflows/series-ordering/workflow.ts +80 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { buildRegistry, validateWorkflow } from "./graph.ts";
|
|
3
|
+
import { DONE, EXIT, FAILED, type WorkflowDef } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
function registryFor(workflow: WorkflowDef) {
|
|
6
|
+
return buildRegistry(workflow);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
test("reports no errors for a well-formed workflow", () => {
|
|
10
|
+
const workflow: WorkflowDef = {
|
|
11
|
+
initial: "A",
|
|
12
|
+
states: {
|
|
13
|
+
A: { type: "script", file: "a.ts", next: "B" },
|
|
14
|
+
B: { type: "agent", prompt: "b", on: { ok: DONE, bad: FAILED } },
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("flags an unresolved top-level initial, with from: null", () => {
|
|
22
|
+
const workflow: WorkflowDef = {
|
|
23
|
+
initial: "Nope",
|
|
24
|
+
states: { A: { type: "script", file: "a.ts", next: DONE } },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
28
|
+
{ kind: "unresolved-target", from: null, target: "Nope" },
|
|
29
|
+
]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("flags an unresolved leaf on/next target", () => {
|
|
33
|
+
const workflow: WorkflowDef = {
|
|
34
|
+
initial: "A",
|
|
35
|
+
states: { A: { type: "script", file: "a.ts", on: { ok: "Missing" }, next: "AlsoMissing" } },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
39
|
+
{ kind: "unresolved-target", from: "A", target: "Missing" },
|
|
40
|
+
{ kind: "unresolved-target", from: "A", target: "AlsoMissing" },
|
|
41
|
+
]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("flags an unresolved composite initial", () => {
|
|
45
|
+
const workflow: WorkflowDef = {
|
|
46
|
+
initial: "Outer",
|
|
47
|
+
states: {
|
|
48
|
+
Outer: { initial: "Missing", states: { Leaf: { type: "script", file: "l.ts", next: DONE } } },
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
53
|
+
{ kind: "unresolved-target", from: "Outer", target: "Missing" },
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("DONE/FAILED/deprecated-EXIT-alias are valid leaf on/next targets", () => {
|
|
58
|
+
const workflow: WorkflowDef = {
|
|
59
|
+
initial: "A",
|
|
60
|
+
states: {
|
|
61
|
+
A: { type: "script", file: "a.ts", on: { ok: DONE, bad: FAILED, legacy: EXIT } },
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("DONE/FAILED are never valid as a top-level initial", () => {
|
|
69
|
+
const states: WorkflowDef["states"] = { A: { type: "script", file: "a.ts", next: DONE } };
|
|
70
|
+
|
|
71
|
+
expect(validateWorkflow(registryFor({ initial: DONE, states }), DONE)).toEqual([
|
|
72
|
+
{ kind: "unresolved-target", from: null, target: DONE },
|
|
73
|
+
]);
|
|
74
|
+
expect(validateWorkflow(registryFor({ initial: FAILED, states }), FAILED)).toEqual([
|
|
75
|
+
{ kind: "unresolved-target", from: null, target: FAILED },
|
|
76
|
+
]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("DONE/FAILED are never valid as a composite initial", () => {
|
|
80
|
+
const workflow: WorkflowDef = {
|
|
81
|
+
initial: "Outer",
|
|
82
|
+
states: {
|
|
83
|
+
Outer: { initial: FAILED, states: { Leaf: { type: "script", file: "l.ts", next: DONE } } },
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
88
|
+
{ kind: "unresolved-target", from: "Outer", target: FAILED },
|
|
89
|
+
]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("flags a dead-end leaf with neither on nor next", () => {
|
|
93
|
+
const workflow: WorkflowDef = {
|
|
94
|
+
initial: "A",
|
|
95
|
+
states: { A: { type: "script", file: "a.ts" } },
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
99
|
+
{ kind: "dead-end-leaf", state: "A" },
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("an empty on map with no next is still a dead-end leaf", () => {
|
|
104
|
+
const workflow: WorkflowDef = {
|
|
105
|
+
initial: "A",
|
|
106
|
+
states: { A: { type: "script", file: "a.ts", on: {} } },
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
110
|
+
{ kind: "dead-end-leaf", state: "A" },
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("finds a two-composite initial cycle exactly once, regardless of which feeder it's discovered from", () => {
|
|
115
|
+
const workflow: WorkflowDef = {
|
|
116
|
+
initial: "A",
|
|
117
|
+
states: {
|
|
118
|
+
A: { initial: "B", states: { LeafA: { type: "script", file: "l.ts", next: DONE } } },
|
|
119
|
+
B: { initial: "A", states: { LeafB: { type: "script", file: "l.ts", next: DONE } } },
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
124
|
+
{ kind: "initial-cycle", cycle: ["A", "B"] },
|
|
125
|
+
]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("finds a self-referential composite initial cycle", () => {
|
|
129
|
+
const workflow: WorkflowDef = {
|
|
130
|
+
initial: "A",
|
|
131
|
+
states: { A: { initial: "A", states: { Leaf: { type: "script", file: "l.ts", next: DONE } } } },
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
135
|
+
{ kind: "initial-cycle", cycle: ["A"] },
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("a cycle mid-chain that resolves to an unresolved id reports unresolved-target, not a cycle", () => {
|
|
140
|
+
const workflow: WorkflowDef = {
|
|
141
|
+
initial: "A",
|
|
142
|
+
states: {
|
|
143
|
+
A: { initial: "B", states: { LeafA: { type: "script", file: "l.ts", next: DONE } } },
|
|
144
|
+
B: { initial: "Missing", states: { LeafB: { type: "script", file: "l.ts", next: DONE } } },
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
expect(validateWorkflow(registryFor(workflow), workflow.initial)).toEqual([
|
|
149
|
+
{ kind: "unresolved-target", from: "B", target: "Missing" },
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("collects every problem in one pass instead of stopping at the first", () => {
|
|
154
|
+
const workflow: WorkflowDef = {
|
|
155
|
+
initial: "A",
|
|
156
|
+
states: {
|
|
157
|
+
A: { type: "script", file: "a.ts", next: "Missing1" },
|
|
158
|
+
B: { type: "script", file: "b.ts" }, // dead-end
|
|
159
|
+
Loop: { initial: "Loop", states: { Leaf: { type: "script", file: "l.ts", next: DONE } } },
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const errors = validateWorkflow(registryFor(workflow), workflow.initial);
|
|
164
|
+
expect(errors).toEqual(
|
|
165
|
+
expect.arrayContaining([
|
|
166
|
+
{ kind: "unresolved-target", from: "A", target: "Missing1" },
|
|
167
|
+
{ kind: "dead-end-leaf", state: "B" },
|
|
168
|
+
{ kind: "initial-cycle", cycle: ["Loop"] },
|
|
169
|
+
]),
|
|
170
|
+
);
|
|
171
|
+
expect(errors.length).toBe(3);
|
|
172
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// Pure, synchronous structure of a WorkflowDef — no dispatch, no I/O, no
|
|
2
|
+
// async. Anything that just wants to know the graph's shape (a renderer, a
|
|
3
|
+
// validator, a future web viewer) depends on this, not on the execution loop.
|
|
4
|
+
import {
|
|
5
|
+
DONE,
|
|
6
|
+
FAILED,
|
|
7
|
+
isComposite,
|
|
8
|
+
type StateDef,
|
|
9
|
+
type Target,
|
|
10
|
+
type WorkflowDef,
|
|
11
|
+
} from "./types.ts";
|
|
12
|
+
|
|
13
|
+
export interface RegistryEntry {
|
|
14
|
+
def: StateDef;
|
|
15
|
+
scopePath: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class DuplicateStateIdError extends Error {
|
|
19
|
+
constructor(public readonly id: string) {
|
|
20
|
+
super(`Duplicate state id "${id}"`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function buildRegistry(workflow: WorkflowDef): Map<string, RegistryEntry> {
|
|
25
|
+
const registry = new Map<string, RegistryEntry>();
|
|
26
|
+
|
|
27
|
+
function walk(states: Record<string, StateDef>, scopePath: string[]) {
|
|
28
|
+
for (const [id, def] of Object.entries(states)) {
|
|
29
|
+
if (registry.has(id)) {
|
|
30
|
+
throw new DuplicateStateIdError(id);
|
|
31
|
+
}
|
|
32
|
+
registry.set(id, { def, scopePath });
|
|
33
|
+
if (isComposite(def)) {
|
|
34
|
+
walk(def.states, [...scopePath, id]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
walk(workflow.states, []);
|
|
40
|
+
return registry;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Descend composites via their `initial` field, recursively, down to a leaf. */
|
|
44
|
+
export function enter(registry: Map<string, RegistryEntry>, id: string): string {
|
|
45
|
+
let current = id;
|
|
46
|
+
for (;;) {
|
|
47
|
+
const entry = registry.get(current);
|
|
48
|
+
if (!entry) return current; // unresolved — caller checks registry.has() and reports the crash
|
|
49
|
+
if (!isComposite(entry.def)) return current;
|
|
50
|
+
current = entry.def.initial;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Exact event-name match against `on`, else the `next` fallback. No throw — undefined means no route. */
|
|
55
|
+
export function resolveTarget(def: StateDef, event: string | undefined): Target | undefined {
|
|
56
|
+
if (isComposite(def)) return undefined;
|
|
57
|
+
if (event !== undefined && def.on?.[event] !== undefined) return def.on[event];
|
|
58
|
+
return def.next;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type WorkflowValidationError =
|
|
62
|
+
| { kind: "unresolved-target"; from: string | null; target: string }
|
|
63
|
+
| { kind: "initial-cycle"; cycle: string[] }
|
|
64
|
+
| { kind: "dead-end-leaf"; state: string };
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Eager, whole-graph structural check over an already-built registry — a
|
|
68
|
+
* second, cheap pass over the same flat `Map` `buildRegistry` produced, not
|
|
69
|
+
* a second tree-walk. Collects every problem found; never stops at the
|
|
70
|
+
* first. `DONE`/`FAILED` are valid targets only for a leaf's `on`/`next`,
|
|
71
|
+
* never for any `initial` (composite or top-level).
|
|
72
|
+
*/
|
|
73
|
+
export function validateWorkflow(
|
|
74
|
+
registry: Map<string, RegistryEntry>,
|
|
75
|
+
initial: string,
|
|
76
|
+
): WorkflowValidationError[] {
|
|
77
|
+
const errors: WorkflowValidationError[] = [];
|
|
78
|
+
|
|
79
|
+
if (!registry.has(initial)) {
|
|
80
|
+
errors.push({ kind: "unresolved-target", from: null, target: initial });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const [id, entry] of registry) {
|
|
84
|
+
if (isComposite(entry.def)) {
|
|
85
|
+
if (!registry.has(entry.def.initial)) {
|
|
86
|
+
errors.push({ kind: "unresolved-target", from: id, target: entry.def.initial });
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const def = entry.def;
|
|
92
|
+
const targets = Object.values(def.on ?? {});
|
|
93
|
+
if (def.next !== undefined) targets.push(def.next);
|
|
94
|
+
for (const target of targets) {
|
|
95
|
+
if (target !== DONE && target !== FAILED && !registry.has(target)) {
|
|
96
|
+
errors.push({ kind: "unresolved-target", from: id, target });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (Object.keys(def.on ?? {}).length === 0 && def.next === undefined) {
|
|
101
|
+
errors.push({ kind: "dead-end-leaf", state: id });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Every composite in the registry is a chain-walk start, plus the
|
|
106
|
+
// workflow's own top-level `initial` — a hazard sitting in the registry is
|
|
107
|
+
// live regardless of how (or whether) a run happens to reach it.
|
|
108
|
+
const chainStarts = [initial];
|
|
109
|
+
for (const [id, entry] of registry) {
|
|
110
|
+
if (isComposite(entry.def)) chainStarts.push(id);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const foundCycles: string[][] = [];
|
|
114
|
+
for (const start of chainStarts) {
|
|
115
|
+
const visited: string[] = [];
|
|
116
|
+
let current = start;
|
|
117
|
+
for (;;) {
|
|
118
|
+
const seenAt = visited.indexOf(current);
|
|
119
|
+
if (seenAt !== -1) {
|
|
120
|
+
foundCycles.push(visited.slice(seenAt));
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
const entry = registry.get(current);
|
|
124
|
+
if (!entry || !isComposite(entry.def)) break; // unresolved or leaf — not a cycle
|
|
125
|
+
visited.push(current);
|
|
126
|
+
current = entry.def.initial;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// The same cycle is found once per feeder composite (A -> B -> A starting
|
|
131
|
+
// from A, and again starting from B) — dedupe by rotating each to start at
|
|
132
|
+
// its lexicographically smallest member before comparing.
|
|
133
|
+
const seenCanonical = new Set<string>();
|
|
134
|
+
for (const cycle of foundCycles) {
|
|
135
|
+
let minIdx = 0;
|
|
136
|
+
for (let i = 1; i < cycle.length; i++) {
|
|
137
|
+
if (cycle[i]! < cycle[minIdx]!) minIdx = i;
|
|
138
|
+
}
|
|
139
|
+
const rotated = [...cycle.slice(minIdx), ...cycle.slice(0, minIdx)];
|
|
140
|
+
const key = rotated.join("\u0000");
|
|
141
|
+
if (!seenCanonical.has(key)) {
|
|
142
|
+
seenCanonical.add(key);
|
|
143
|
+
errors.push({ kind: "initial-cycle", cycle: rotated });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return errors;
|
|
148
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { NodeEvent } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse a JSONL event log's text content back into NodeEvents. Pure and
|
|
5
|
+
* synchronous — no filesystem access — so a web viewer can fetch the file's
|
|
6
|
+
* contents itself and hand the text here. Tolerates a torn trailing line
|
|
7
|
+
* (e.g. the process crashed mid-append): unparsable lines are skipped rather
|
|
8
|
+
* than failing the whole read.
|
|
9
|
+
*/
|
|
10
|
+
export function parseEventLog(text: string): NodeEvent[] {
|
|
11
|
+
const events: NodeEvent[] = [];
|
|
12
|
+
for (const line of text.split("\n")) {
|
|
13
|
+
if (line.trim() === "") continue;
|
|
14
|
+
try {
|
|
15
|
+
events.push(JSON.parse(line) as NodeEvent);
|
|
16
|
+
} catch {
|
|
17
|
+
// skip malformed/torn line
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return events;
|
|
21
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { renderMermaid } from "./mermaid.ts";
|
|
3
|
+
import { DONE, FAILED, type WorkflowDef } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
test("renderMermaid maps DONE to mermaid's own end pseudostate", () => {
|
|
6
|
+
const workflow: WorkflowDef = {
|
|
7
|
+
initial: "Solo",
|
|
8
|
+
states: { Solo: { type: "script", file: "solo.ts", on: { ok: DONE } } },
|
|
9
|
+
};
|
|
10
|
+
const out = renderMermaid(workflow);
|
|
11
|
+
expect(out).toContain("Solo --> [*] : ok");
|
|
12
|
+
expect(out).not.toContain("FAILED");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("renderMermaid gives FAILED a real, styled node instead of the literal '[-]' token", () => {
|
|
16
|
+
const workflow: WorkflowDef = {
|
|
17
|
+
initial: "RunFailure",
|
|
18
|
+
states: { RunFailure: { type: "script", file: "run_failure.ts", on: { gave_up: FAILED } } },
|
|
19
|
+
};
|
|
20
|
+
const out = renderMermaid(workflow);
|
|
21
|
+
expect(out).not.toContain("[-]");
|
|
22
|
+
expect(out).toContain("RunFailure --> FAILED : gave_up");
|
|
23
|
+
expect(out).toContain("FAILED : ✗ failed");
|
|
24
|
+
expect(out).toContain("class FAILED darkfFailed");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("renderMermaid omits the FAILED node entirely when no edge targets it", () => {
|
|
28
|
+
const workflow: WorkflowDef = {
|
|
29
|
+
initial: "Solo",
|
|
30
|
+
states: { Solo: { type: "script", file: "solo.ts", on: { ok: DONE } } },
|
|
31
|
+
};
|
|
32
|
+
const out = renderMermaid(workflow);
|
|
33
|
+
expect(out).not.toContain("darkfFailed");
|
|
34
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { DONE, FAILED, isComposite, type StateDef, type WorkflowDef } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
const TYPE_ICON: Record<"agent" | "script", string> = {
|
|
4
|
+
agent: "🤖",
|
|
5
|
+
script: "▸",
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const CLASS_COLORS = {
|
|
9
|
+
darkfAgent: "#6d8dfa",
|
|
10
|
+
darkfScript: "#3fae6a",
|
|
11
|
+
darkfActive: "#f96",
|
|
12
|
+
darkfFailed: "#e05252",
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
// A node id, never rendered as literal "[-]" — `[*]` is stateDiagram-v2's one
|
|
16
|
+
// hardcoded pseudostate token; `[-]` has no built-in meaning, so left as the
|
|
17
|
+
// literal sentinel it would render as a structurally broken diagram (a plain
|
|
18
|
+
// state named "[-]"), not merely an unlabeled one.
|
|
19
|
+
const FAILED_NODE_ID = "FAILED";
|
|
20
|
+
|
|
21
|
+
function target(t: string): string {
|
|
22
|
+
if (t === DONE) return "[*]";
|
|
23
|
+
if (t === FAILED) return FAILED_NODE_ID;
|
|
24
|
+
return t;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function walk(
|
|
28
|
+
states: Record<string, StateDef>,
|
|
29
|
+
lines: string[],
|
|
30
|
+
agentIds: string[],
|
|
31
|
+
scriptIds: string[],
|
|
32
|
+
usesFailed: { value: boolean },
|
|
33
|
+
): void {
|
|
34
|
+
for (const [id, def] of Object.entries(states)) {
|
|
35
|
+
if (isComposite(def)) {
|
|
36
|
+
lines.push(`state ${id} {`);
|
|
37
|
+
lines.push(` [*] --> ${def.initial}`);
|
|
38
|
+
walk(def.states, lines, agentIds, scriptIds, usesFailed);
|
|
39
|
+
lines.push(`}`);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (def.type === "agent") agentIds.push(id);
|
|
44
|
+
else scriptIds.push(id);
|
|
45
|
+
lines.push(`${id} : ${TYPE_ICON[def.type]} ${id}`);
|
|
46
|
+
|
|
47
|
+
for (const [event, dest] of Object.entries(def.on ?? {})) {
|
|
48
|
+
const t = target(dest);
|
|
49
|
+
if (t === FAILED_NODE_ID) usesFailed.value = true;
|
|
50
|
+
lines.push(`${id} --> ${t} : ${event}`);
|
|
51
|
+
}
|
|
52
|
+
if (def.next) {
|
|
53
|
+
const t = target(def.next);
|
|
54
|
+
if (t === FAILED_NODE_ID) usesFailed.value = true;
|
|
55
|
+
lines.push(`${id} --> ${t}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function renderMermaid(workflow: WorkflowDef): string {
|
|
61
|
+
const lines: string[] = ["stateDiagram-v2", `[*] --> ${workflow.initial}`];
|
|
62
|
+
const agentIds: string[] = [];
|
|
63
|
+
const scriptIds: string[] = [];
|
|
64
|
+
const usesFailed = { value: false };
|
|
65
|
+
|
|
66
|
+
walk(workflow.states, lines, agentIds, scriptIds, usesFailed);
|
|
67
|
+
|
|
68
|
+
if (usesFailed.value) {
|
|
69
|
+
lines.push(`${FAILED_NODE_ID} : ✗ failed`);
|
|
70
|
+
}
|
|
71
|
+
lines.push(`classDef darkfAgent fill:${CLASS_COLORS.darkfAgent}`);
|
|
72
|
+
lines.push(`classDef darkfScript fill:${CLASS_COLORS.darkfScript}`);
|
|
73
|
+
if (agentIds.length) lines.push(`class ${agentIds.join(",")} darkfAgent`);
|
|
74
|
+
if (scriptIds.length) lines.push(`class ${scriptIds.join(",")} darkfScript`);
|
|
75
|
+
if (usesFailed.value) {
|
|
76
|
+
lines.push(`classDef darkfFailed fill:${CLASS_COLORS.darkfFailed}`);
|
|
77
|
+
lines.push(`class ${FAILED_NODE_ID} darkfFailed`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return lines.join("\n");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function renderActive(
|
|
84
|
+
workflow: WorkflowDef,
|
|
85
|
+
state: string,
|
|
86
|
+
ancestors: string[] = [],
|
|
87
|
+
): string {
|
|
88
|
+
const base = renderMermaid(workflow);
|
|
89
|
+
const activeIds = [state, ...ancestors].join(",");
|
|
90
|
+
return [
|
|
91
|
+
base,
|
|
92
|
+
`classDef darkfActive fill:${CLASS_COLORS.darkfActive}`,
|
|
93
|
+
`class ${activeIds} darkfActive`,
|
|
94
|
+
].join("\n");
|
|
95
|
+
}
|