@signalsafe/tree-spec-editor 0.1.0 → 0.1.1
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 +352 -24
- package/dist/TreeSpecGraphEditor.d.ts +1 -1
- package/dist/TreeSpecGraphEditor.d.ts.map +1 -1
- package/dist/TreeSpecGraphEditor.js +20 -4
- package/dist/editorHelpers.d.ts.map +1 -1
- package/dist/editorHelpers.js +10 -6
- package/dist/treeLayout.d.ts.map +1 -1
- package/dist/treeLayout.js +4 -8
- package/docs/tree-spec-editor-happy-path.png +0 -0
- package/docs/tree-spec-editor-issues.png +0 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -1,45 +1,373 @@
|
|
|
1
1
|
# `@signalsafe/tree-spec-editor`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React graph editor for authoring `TreeSpec` content.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This package gives you a ready-to-embed editor component plus a small set of editor helpers. It is focused on graph editing only: nodes, choices, transitions, selection, layout, and validation hints.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Install
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
```bash
|
|
10
|
+
npm install @signalsafe/tree-spec-editor react react-dom reactflow
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Happy Path
|
|
14
|
+
|
|
15
|
+
The smallest useful integration is:
|
|
16
|
+
|
|
17
|
+
1. Keep an `EditorTree` in React state.
|
|
18
|
+
2. Render `TreeSpecGraphEditor`.
|
|
19
|
+
3. Replace the state value in `onChange`.
|
|
20
|
+
|
|
21
|
+
The screenshot below was captured from the exact example tree in this section.
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { useState, type JSX } from 'react';
|
|
27
|
+
import TreeSpecGraphEditor, {
|
|
28
|
+
END_NODE_ID,
|
|
29
|
+
type EditorTree,
|
|
30
|
+
} from '@signalsafe/tree-spec-editor';
|
|
31
|
+
|
|
32
|
+
const initialTree: EditorTree = {
|
|
33
|
+
start_node: 'start',
|
|
34
|
+
nodes: {
|
|
35
|
+
start: {
|
|
36
|
+
id: 'start',
|
|
37
|
+
type: 'prompt',
|
|
38
|
+
prompt: 'A message asks for your company password.',
|
|
39
|
+
choices: [
|
|
40
|
+
{ id: 'report', label: 'Report it to security' },
|
|
41
|
+
{ id: 'enter-password', label: 'Enter my password' },
|
|
42
|
+
],
|
|
43
|
+
position: { x: 40, y: 260 },
|
|
44
|
+
},
|
|
45
|
+
follow_up: {
|
|
46
|
+
id: 'follow_up',
|
|
47
|
+
type: 'prompt',
|
|
48
|
+
prompt: 'Security asks for a screenshot.',
|
|
49
|
+
choices: [{ id: 'send', label: 'Send the screenshot' }],
|
|
50
|
+
position: { x: 420, y: 420 },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
transitions: [
|
|
54
|
+
{
|
|
55
|
+
id: 'start-report',
|
|
56
|
+
fromNodeId: 'start',
|
|
57
|
+
fromChoiceId: 'report',
|
|
58
|
+
toNodeId: 'follow_up',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 'start-enter-password',
|
|
62
|
+
fromNodeId: 'start',
|
|
63
|
+
fromChoiceId: 'enter-password',
|
|
64
|
+
toNodeId: END_NODE_ID,
|
|
65
|
+
outcome: 'compromised',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: 'follow-up-send',
|
|
69
|
+
fromNodeId: 'follow_up',
|
|
70
|
+
fromChoiceId: 'send',
|
|
71
|
+
toNodeId: END_NODE_ID,
|
|
72
|
+
outcome: 'safe',
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
};
|
|
10
76
|
|
|
11
|
-
|
|
77
|
+
export function ExampleTreeSpecEditor(): JSX.Element {
|
|
78
|
+
const [tree, setTree] = useState<EditorTree>(initialTree);
|
|
12
79
|
|
|
13
|
-
|
|
14
|
-
|
|
80
|
+
return (
|
|
81
|
+
<TreeSpecGraphEditor
|
|
82
|
+
tree={tree}
|
|
83
|
+
onChange={setTree}
|
|
84
|
+
className="h-[70vh] rounded border"
|
|
85
|
+
/>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## What You Get
|
|
91
|
+
|
|
92
|
+
- `TreeSpecGraphEditor`: the default React editor component.
|
|
93
|
+
- `EditorTree`, `EditorNode`, `EditorTransition`, `EditorChoice`: editor model types.
|
|
94
|
+
- `GraphSelection` and `GraphEditorIssue`: optional selection and issue-display types.
|
|
95
|
+
- `autoLayoutTree` and `getNextSpawnPosition`: layout helpers for placing nodes.
|
|
96
|
+
- `lintEditorTree`, `getTransition`, `upsertTransition`, `deleteTransitionsForChoice`: editor helpers for validation and transition updates.
|
|
15
97
|
|
|
16
|
-
##
|
|
98
|
+
## Feature Examples
|
|
17
99
|
|
|
18
|
-
|
|
100
|
+
### Show validation issues
|
|
19
101
|
|
|
20
|
-
|
|
102
|
+
Pass `issues` when you want the editor to highlight problem nodes and edges.
|
|
21
103
|
|
|
22
|
-
|
|
23
|
-
- **Does not** depend on `simulator-core` or app packages.
|
|
104
|
+
The screenshot below was captured from the exact example tree in this section.
|
|
24
105
|
|
|
25
|
-
|
|
106
|
+

|
|
26
107
|
|
|
27
108
|
```tsx
|
|
28
|
-
import
|
|
29
|
-
import {
|
|
109
|
+
import { useMemo, useState, type JSX } from 'react';
|
|
110
|
+
import TreeSpecGraphEditor, {
|
|
111
|
+
END_NODE_ID,
|
|
112
|
+
lintEditorTree,
|
|
113
|
+
type EditorTree,
|
|
114
|
+
type GraphEditorIssue,
|
|
115
|
+
} from '@signalsafe/tree-spec-editor';
|
|
116
|
+
|
|
117
|
+
const validationTree: EditorTree = {
|
|
118
|
+
start_node: 'start',
|
|
119
|
+
nodes: {
|
|
120
|
+
start: {
|
|
121
|
+
id: 'start',
|
|
122
|
+
type: 'prompt',
|
|
123
|
+
prompt: 'Review the message before acting.',
|
|
124
|
+
choices: [
|
|
125
|
+
{ id: 'investigate', label: 'Investigate' },
|
|
126
|
+
{ id: 'escalate', label: 'Escalate' },
|
|
127
|
+
],
|
|
128
|
+
position: { x: 40, y: 260 },
|
|
129
|
+
},
|
|
130
|
+
follow_up: {
|
|
131
|
+
id: 'follow_up',
|
|
132
|
+
type: 'prompt',
|
|
133
|
+
prompt: 'Collect a screenshot and report it.',
|
|
134
|
+
choices: [{ id: 'send', label: 'Send screenshot' }],
|
|
135
|
+
position: { x: 420, y: 420 },
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
transitions: [
|
|
139
|
+
{
|
|
140
|
+
id: 'start-investigate',
|
|
141
|
+
fromNodeId: 'start',
|
|
142
|
+
fromChoiceId: 'investigate',
|
|
143
|
+
toNodeId: 'follow_up',
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 'start-escalate',
|
|
147
|
+
fromNodeId: 'start',
|
|
148
|
+
fromChoiceId: 'escalate',
|
|
149
|
+
toNodeId: END_NODE_ID,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
id: 'follow-up-send',
|
|
153
|
+
fromNodeId: 'follow_up',
|
|
154
|
+
fromChoiceId: 'send',
|
|
155
|
+
toNodeId: END_NODE_ID,
|
|
156
|
+
outcome: 'safe',
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export function EditorWithIssues(): JSX.Element {
|
|
162
|
+
const [tree, setTree] = useState<EditorTree>(validationTree);
|
|
163
|
+
const issues = useMemo<GraphEditorIssue[]>(() => lintEditorTree(tree), [tree]);
|
|
164
|
+
|
|
165
|
+
return <TreeSpecGraphEditor tree={tree} onChange={setTree} issues={issues} />;
|
|
166
|
+
}
|
|
30
167
|
```
|
|
31
168
|
|
|
32
|
-
|
|
169
|
+
### Track selection
|
|
33
170
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
171
|
+
Pass `selected` and `onSelect` when another part of your page needs to know which node or edge is active.
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
import { useState, type JSX } from 'react';
|
|
175
|
+
import TreeSpecGraphEditor, {
|
|
176
|
+
type EditorTree,
|
|
177
|
+
type GraphSelection,
|
|
178
|
+
} from '@signalsafe/tree-spec-editor';
|
|
37
179
|
|
|
38
|
-
|
|
180
|
+
type Props = {
|
|
181
|
+
initialTree: EditorTree;
|
|
182
|
+
};
|
|
39
183
|
|
|
40
|
-
|
|
184
|
+
export function EditorWithSelection({ initialTree }: Props): JSX.Element {
|
|
185
|
+
const [tree, setTree] = useState<EditorTree>(initialTree);
|
|
186
|
+
const [selected, setSelected] = useState<GraphSelection>({ kind: null, id: null });
|
|
41
187
|
|
|
42
|
-
|
|
188
|
+
return (
|
|
189
|
+
<TreeSpecGraphEditor
|
|
190
|
+
tree={tree}
|
|
191
|
+
onChange={setTree}
|
|
192
|
+
selected={selected}
|
|
193
|
+
onSelect={setSelected}
|
|
194
|
+
/>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Focus a node and hide the minimap
|
|
200
|
+
|
|
201
|
+
Use `focusNodeId` when you want the editor to center a node, and `showMiniMap={false}` when you want a simpler layout.
|
|
202
|
+
|
|
203
|
+
```tsx
|
|
204
|
+
import { useState, type JSX } from 'react';
|
|
205
|
+
import TreeSpecGraphEditor, {
|
|
206
|
+
type EditorTree,
|
|
207
|
+
} from '@signalsafe/tree-spec-editor';
|
|
208
|
+
|
|
209
|
+
type Props = {
|
|
210
|
+
initialTree: EditorTree;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export function FocusedEditor({ initialTree }: Props): JSX.Element {
|
|
214
|
+
const [tree, setTree] = useState<EditorTree>(initialTree);
|
|
215
|
+
|
|
216
|
+
return (
|
|
217
|
+
<TreeSpecGraphEditor
|
|
218
|
+
tree={tree}
|
|
219
|
+
onChange={setTree}
|
|
220
|
+
focusNodeId="start"
|
|
221
|
+
showMiniMap={false}
|
|
222
|
+
/>
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Refit the graph after external changes
|
|
228
|
+
|
|
229
|
+
Increment `fitViewNonce` when your page adds, removes, or rearranges nodes and you want the editor to recenter everything.
|
|
230
|
+
|
|
231
|
+
```tsx
|
|
232
|
+
import { useState, type JSX } from 'react';
|
|
233
|
+
import TreeSpecGraphEditor, {
|
|
234
|
+
type EditorTree,
|
|
235
|
+
} from '@signalsafe/tree-spec-editor';
|
|
236
|
+
|
|
237
|
+
type Props = {
|
|
238
|
+
initialTree: EditorTree;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
export function EditorWithFitView({ initialTree }: Props): JSX.Element {
|
|
242
|
+
const [tree, setTree] = useState<EditorTree>(initialTree);
|
|
243
|
+
const [fitViewNonce, setFitViewNonce] = useState<number>(1);
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<>
|
|
247
|
+
<button type="button" onClick={() => setFitViewNonce((value) => value + 1)}>
|
|
248
|
+
Refit graph
|
|
249
|
+
</button>
|
|
250
|
+
<TreeSpecGraphEditor
|
|
251
|
+
tree={tree}
|
|
252
|
+
onChange={setTree}
|
|
253
|
+
fitViewNonce={fitViewNonce}
|
|
254
|
+
/>
|
|
255
|
+
</>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
```
|
|
43
259
|
|
|
44
|
-
|
|
45
|
-
|
|
260
|
+
## Helper Examples
|
|
261
|
+
|
|
262
|
+
Use `autoLayoutTree` before first render when you want a readable default layout.
|
|
263
|
+
|
|
264
|
+
```tsx
|
|
265
|
+
import { autoLayoutTree, type EditorTree } from '@signalsafe/tree-spec-editor';
|
|
266
|
+
|
|
267
|
+
const tree: EditorTree = autoLayoutTree({
|
|
268
|
+
start_node: 'start',
|
|
269
|
+
nodes: {
|
|
270
|
+
start: {
|
|
271
|
+
id: 'start',
|
|
272
|
+
type: 'prompt',
|
|
273
|
+
prompt: 'Choose a response',
|
|
274
|
+
choices: [{ id: 'continue', label: 'Continue' }],
|
|
275
|
+
position: { x: 0, y: 0 },
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
transitions: [],
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Use `lintEditorTree` when you want editor-side warnings or errors to display next to nodes and edges.
|
|
283
|
+
|
|
284
|
+
```tsx
|
|
285
|
+
import { lintEditorTree, type GraphEditorIssue } from '@signalsafe/tree-spec-editor';
|
|
286
|
+
|
|
287
|
+
const issues: GraphEditorIssue[] = lintEditorTree(tree);
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Use `getTransition`, `upsertTransition`, and `deleteTransitionsForChoice` when a host toolbar needs to read or replace a choice transition.
|
|
291
|
+
|
|
292
|
+
```tsx
|
|
293
|
+
import {
|
|
294
|
+
END_NODE_ID,
|
|
295
|
+
deleteTransitionsForChoice,
|
|
296
|
+
getTransition,
|
|
297
|
+
upsertTransition,
|
|
298
|
+
type EditorTree,
|
|
299
|
+
type EditorTransition,
|
|
300
|
+
} from '@signalsafe/tree-spec-editor';
|
|
301
|
+
|
|
302
|
+
function connectChoiceToEnd(tree: EditorTree, fromNodeId: string, fromChoiceId: string): EditorTree {
|
|
303
|
+
const existing = getTransition(tree, fromNodeId, fromChoiceId);
|
|
304
|
+
|
|
305
|
+
const nextTransition: EditorTransition = {
|
|
306
|
+
id: existing?.id ?? `${fromNodeId}-${fromChoiceId}-to-end`,
|
|
307
|
+
fromNodeId,
|
|
308
|
+
fromChoiceId,
|
|
309
|
+
toNodeId: END_NODE_ID,
|
|
310
|
+
outcome: 'safe',
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
return upsertTransition(tree, nextTransition);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function clearChoiceTransition(tree: EditorTree, fromNodeId: string, fromChoiceId: string): EditorTree {
|
|
317
|
+
return deleteTransitionsForChoice(tree, fromNodeId, fromChoiceId);
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Use `parsePydanticOutcomeErrors` when your backend returns a generic validation string and you want node-level editor issues.
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
import {
|
|
325
|
+
parsePydanticOutcomeErrors,
|
|
326
|
+
type GraphEditorIssue,
|
|
327
|
+
} from '@signalsafe/tree-spec-editor';
|
|
328
|
+
|
|
329
|
+
const backendMessage =
|
|
330
|
+
"Transition to END must include outcome. input_value={'from': ['start', 'escalate'], 'to': 'END'}";
|
|
331
|
+
|
|
332
|
+
const issues: GraphEditorIssue[] = parsePydanticOutcomeErrors(backendMessage) ?? [];
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Use `getNextSpawnPosition`, `safeUUID`, and `TREE_SPEC_NODE_TYPE_PRESETS` when a host UI creates new nodes outside the canvas.
|
|
336
|
+
|
|
337
|
+
```tsx
|
|
338
|
+
import {
|
|
339
|
+
TREE_SPEC_NODE_TYPE_PRESETS,
|
|
340
|
+
getNextSpawnPosition,
|
|
341
|
+
safeUUID,
|
|
342
|
+
type EditorNode,
|
|
343
|
+
type EditorTree,
|
|
344
|
+
} from '@signalsafe/tree-spec-editor';
|
|
345
|
+
|
|
346
|
+
function addPromptNode(tree: EditorTree): EditorTree {
|
|
347
|
+
const id = safeUUID();
|
|
348
|
+
const position = getNextSpawnPosition(tree);
|
|
349
|
+
const type = TREE_SPEC_NODE_TYPE_PRESETS[0];
|
|
350
|
+
|
|
351
|
+
const nextNode: EditorNode = {
|
|
352
|
+
id,
|
|
353
|
+
type,
|
|
354
|
+
prompt: 'New step',
|
|
355
|
+
choices: [{ id: 'choice-1', label: 'New choice' }],
|
|
356
|
+
position,
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
...tree,
|
|
361
|
+
nodes: {
|
|
362
|
+
...tree.nodes,
|
|
363
|
+
[id]: nextNode,
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## Notes
|
|
370
|
+
|
|
371
|
+
- Import from the package root only.
|
|
372
|
+
- Install `react`, `react-dom`, and `reactflow` in the consuming app.
|
|
373
|
+
- If you need wire-format compile/decompile helpers, use `@signalsafe/tree-spec`.
|
|
@@ -12,5 +12,5 @@ export type TreeSpecGraphEditorProps = {
|
|
|
12
12
|
/** Optional class for the outer container (default includes h-70vh border rounded). */
|
|
13
13
|
className?: string;
|
|
14
14
|
};
|
|
15
|
-
export default function TreeSpecGraphEditor(props: TreeSpecGraphEditorProps): import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
export default function TreeSpecGraphEditor(props: Readonly<TreeSpecGraphEditorProps>): import("react/jsx-runtime").JSX.Element;
|
|
16
16
|
//# sourceMappingURL=TreeSpecGraphEditor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TreeSpecGraphEditor.d.ts","sourceRoot":"","sources":["../src/TreeSpecGraphEditor.tsx"],"names":[],"mappings":"AAiBA,OAAO,0BAA0B,CAAC;AAGlC,OAAO,EAGH,KAAK,UAAU,EAEf,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAGxB,MAAM,SAAS,CAAC;AAEjB,MAAM,MAAM,wBAAwB,GAAG;IACnC,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;IACrC,MAAM,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC5B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,cAAc,KAAK,IAAI,CAAC;IACzC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uFAAuF;IACvF,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;
|
|
1
|
+
{"version":3,"file":"TreeSpecGraphEditor.d.ts","sourceRoot":"","sources":["../src/TreeSpecGraphEditor.tsx"],"names":[],"mappings":"AAiBA,OAAO,0BAA0B,CAAC;AAGlC,OAAO,EAGH,KAAK,UAAU,EAEf,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAGxB,MAAM,SAAS,CAAC;AAEjB,MAAM,MAAM,wBAAwB,GAAG;IACnC,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;IACrC,MAAM,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC5B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,cAAc,KAAK,IAAI,CAAC;IACzC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uFAAuF;IACvF,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AA6YF,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,wBAAwB,CAAC,2CAMpF"}
|
|
@@ -4,11 +4,27 @@ import ReactFlow, { Background, Controls, Handle, MiniMap, Position, ReactFlowPr
|
|
|
4
4
|
import 'reactflow/dist/style.css';
|
|
5
5
|
import { safeUUID } from './editorHelpers';
|
|
6
6
|
import { END_NODE_ID, } from './model';
|
|
7
|
+
function getPromptNodeBorderClass(hasErrors, warningCount) {
|
|
8
|
+
if (hasErrors) {
|
|
9
|
+
return 'border-danger';
|
|
10
|
+
}
|
|
11
|
+
if (warningCount > 0) {
|
|
12
|
+
return 'border-warning';
|
|
13
|
+
}
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
function getIssueEdgeStyle(style, hasIssue) {
|
|
17
|
+
if (!hasIssue) {
|
|
18
|
+
return style;
|
|
19
|
+
}
|
|
20
|
+
return style ? { ...style, strokeWidth: 2, strokeDasharray: '6 4' } : { strokeWidth: 2, strokeDasharray: '6 4' };
|
|
21
|
+
}
|
|
7
22
|
function PromptNode({ data }) {
|
|
8
23
|
const n = data.node;
|
|
9
24
|
const choices = n.choices ?? [];
|
|
10
25
|
const hasErrors = data.issuesErrors > 0;
|
|
11
|
-
|
|
26
|
+
const borderClass = getPromptNodeBorderClass(hasErrors, data.issuesWarnings);
|
|
27
|
+
return (_jsxs("div", { className: `card card-min-width-280 ${borderClass}`, children: [_jsx(Handle, { type: "target", position: Position.Left, id: "in", className: "handle-bg-default" }), _jsxs("div", { className: "card-body p-2", children: [_jsxs("div", { className: "d-flex justify-content-between align-items-center", children: [_jsxs("div", { children: [_jsxs("div", { className: "fw-bold font-size-13", children: [data.isStart ? '▶ ' : '', n.type, data.issuesTotal > 0 ? (_jsxs("span", { className: "ms-2", title: `${data.issuesErrors} errors, ${data.issuesWarnings} warnings, ${data.issuesInfo} info`, children: [data.issuesErrors > 0 ? _jsx("span", { className: "badge bg-danger me-1", children: data.issuesErrors }) : null, data.issuesWarnings > 0 ? (_jsx("span", { className: "badge bg-warning text-dark me-1", children: data.issuesWarnings })) : null, data.issuesInfo > 0 ? _jsx("span", { className: "badge bg-info text-dark", children: data.issuesInfo }) : null] })) : null] }), _jsx("div", { className: "font-size-12 opacity-90 text-truncate", children: n.prompt || _jsx("em", { className: "text-muted", children: "(empty prompt)" }) })] }), _jsx("div", { className: "text-muted font-size-11", children: n.id.slice(0, 8) })] }), _jsx("hr", { className: "my-2" }), _jsx("div", { className: "font-size-12", children: choices.length === 0 ? (_jsx("div", { className: "text-muted", children: _jsx("em", { children: "No choices" }) })) : (_jsx("ul", { className: "list-unstyled mb-0", children: choices.map((c) => (_jsxs("li", { className: "d-flex justify-content-between align-items-center position-relative pr-18", children: [_jsx("span", { className: "text-truncate max-w-210", children: c.label }), _jsx("span", { className: "badge bg-light text-dark", children: c.id }), _jsx(Handle, { type: "source", position: Position.Right, id: `choice:${c.id}`, className: "graph-editor-handle", style: {
|
|
12
28
|
position: 'absolute',
|
|
13
29
|
right: -6,
|
|
14
30
|
top: '50%',
|
|
@@ -52,7 +68,7 @@ function buildTransitionsFromEdges(edges, existing) {
|
|
|
52
68
|
const fromChoiceId = choiceIdFromHandle(e.sourceHandle);
|
|
53
69
|
const prior = existingById.get(String(e.id));
|
|
54
70
|
return {
|
|
55
|
-
id: e.id
|
|
71
|
+
id: String(e.id),
|
|
56
72
|
fromNodeId: String(e.source),
|
|
57
73
|
fromChoiceId: fromChoiceId,
|
|
58
74
|
toNodeId: String(e.target),
|
|
@@ -145,7 +161,7 @@ function TreeSpecGraphInner({ tree, onChange, issues = [], selected, onSelect, f
|
|
|
145
161
|
const n = rf.getNode(focusNodeId);
|
|
146
162
|
if (!n)
|
|
147
163
|
return;
|
|
148
|
-
rf.setCenter(n.position.x + 150, n.position.y + 60, { zoom: 1
|
|
164
|
+
rf.setCenter(n.position.x + 150, n.position.y + 60, { zoom: 1, duration: 300 });
|
|
149
165
|
}
|
|
150
166
|
catch {
|
|
151
167
|
// ignore
|
|
@@ -247,7 +263,7 @@ function TreeSpecGraphInner({ tree, onChange, issues = [], selected, onSelect, f
|
|
|
247
263
|
return {
|
|
248
264
|
...e,
|
|
249
265
|
selected: selected?.kind === 'edge' && selected.id === e.id,
|
|
250
|
-
style:
|
|
266
|
+
style: getIssueEdgeStyle(e.style, hasIssue),
|
|
251
267
|
};
|
|
252
268
|
}), onNodesChange: onNodesChangeWrapped, onEdgesChange: onEdgesChangeWrapped, onConnect: onConnect, onNodeDragStart: onNodeDragStart, onNodeDragStop: onNodeDragStop, onNodeClick: onNodeClick, onEdgeClick: onEdgeClick, nodeTypes: nodeTypes, children: [showMiniMap ? _jsx(MiniMap, {}) : null, _jsx(Controls, {}), _jsx(Background, {})] }) }));
|
|
253
269
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"editorHelpers.d.ts","sourceRoot":"","sources":["../src/editorHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAe,KAAK,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE9E,oDAAoD;AACpD,eAAO,MAAM,2BAA2B,kEAAmE,CAAC;
|
|
1
|
+
{"version":3,"file":"editorHelpers.d.ts","sourceRoot":"","sources":["../src/editorHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAe,KAAK,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE9E,oDAAoD;AACpD,eAAO,MAAM,2BAA2B,kEAAmE,CAAC;AAY5G,wBAAgB,QAAQ,IAAI,MAAM,CAKjC;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAEtH;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,gBAAgB,GAAG,UAAU,CAMrF;AAED,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,UAAU,CAKjH;AAED,gFAAgF;AAChF,wBAAgB,cAAc,CAAC,CAAC,EAAE,UAAU,GAAG,aAAa,EAAE,CAa7D;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,EAAE,GAAG,IAAI,CAW9E"}
|
package/dist/editorHelpers.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { END_NODE_ID } from './model';
|
|
2
2
|
/** Common node `type` presets for authoring UIs. */
|
|
3
3
|
export const TREE_SPEC_NODE_TYPE_PRESETS = ['prompt', 'email', 'sms', 'call', 'web', 'attachment'];
|
|
4
|
+
function parseOutcomeErrorMessage(lowerMessage) {
|
|
5
|
+
if (lowerMessage.includes('transition to end must include outcome')) {
|
|
6
|
+
return 'Transition to END must include outcome (safe / at_risk / compromised).';
|
|
7
|
+
}
|
|
8
|
+
if (lowerMessage.includes('non-end transition must not include outcome')) {
|
|
9
|
+
return 'Non-END transition must not include outcome.';
|
|
10
|
+
}
|
|
11
|
+
return 'Validation error';
|
|
12
|
+
}
|
|
4
13
|
export function safeUUID() {
|
|
5
14
|
const crypto = globalThis.crypto;
|
|
6
15
|
return crypto && typeof crypto.randomUUID === 'function'
|
|
@@ -51,12 +60,7 @@ export function parsePydanticOutcomeErrors(msg) {
|
|
|
51
60
|
while ((m = re.exec(msg)) !== null) {
|
|
52
61
|
const node_id = m[1];
|
|
53
62
|
const choice_id = m[2];
|
|
54
|
-
const
|
|
55
|
-
const message = lower.includes('transition to end must include outcome')
|
|
56
|
-
? 'Transition to END must include outcome (safe / at_risk / compromised).'
|
|
57
|
-
: lower.includes('non-end transition must not include outcome')
|
|
58
|
-
? 'Non-END transition must not include outcome.'
|
|
59
|
-
: 'Validation error';
|
|
63
|
+
const message = parseOutcomeErrorMessage(msg.toLowerCase());
|
|
60
64
|
issues.push({ severity: 'error', message, node_id, choice_id });
|
|
61
65
|
}
|
|
62
66
|
return issues.length ? issues : null;
|
package/dist/treeLayout.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"treeLayout.d.ts","sourceRoot":"","sources":["../src/treeLayout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,KAAK,UAAU,EAAE,MAAM,SAAS,CAAC;AAExE,kFAAkF;AAClF,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAG/E;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,
|
|
1
|
+
{"version":3,"file":"treeLayout.d.ts","sourceRoot":"","sources":["../src/treeLayout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,KAAK,UAAU,EAAE,MAAM,SAAS,CAAC;AAExE,kFAAkF;AAClF,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAG/E;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAyD3D"}
|
package/dist/treeLayout.js
CHANGED
|
@@ -27,7 +27,7 @@ export function autoLayoutTree(tree) {
|
|
|
27
27
|
};
|
|
28
28
|
while (q.length) {
|
|
29
29
|
const cur = q.shift();
|
|
30
|
-
const curLevel = level[cur]
|
|
30
|
+
const curLevel = level[cur];
|
|
31
31
|
for (const nxt of outgoing(cur)) {
|
|
32
32
|
if (level[nxt] !== undefined)
|
|
33
33
|
continue;
|
|
@@ -38,12 +38,10 @@ export function autoLayoutTree(tree) {
|
|
|
38
38
|
const allNodeIds = Object.keys(tree.nodes);
|
|
39
39
|
const maxLevel = Math.max(0, ...Object.values(level));
|
|
40
40
|
for (const id of allNodeIds) {
|
|
41
|
-
|
|
42
|
-
level[id] = maxLevel + 1;
|
|
43
|
-
}
|
|
41
|
+
level[id] ?? (level[id] = maxLevel + 1);
|
|
44
42
|
}
|
|
45
43
|
for (const id of allNodeIds) {
|
|
46
|
-
const l = level[id]
|
|
44
|
+
const l = level[id];
|
|
47
45
|
if (!byLevel[l])
|
|
48
46
|
byLevel[l] = [];
|
|
49
47
|
byLevel[l].push(id);
|
|
@@ -55,11 +53,9 @@ export function autoLayoutTree(tree) {
|
|
|
55
53
|
const nextNodes = { ...tree.nodes };
|
|
56
54
|
for (const [lvlStr, ids] of Object.entries(byLevel)) {
|
|
57
55
|
const lvl = Number(lvlStr);
|
|
58
|
-
ids.sort();
|
|
56
|
+
ids.sort((left, right) => left.localeCompare(right));
|
|
59
57
|
ids.forEach((id, idx) => {
|
|
60
58
|
const n = nextNodes[id];
|
|
61
|
-
if (!n)
|
|
62
|
-
return;
|
|
63
59
|
n.position = { x: marginX + lvl * spacingX, y: marginY + idx * spacingY };
|
|
64
60
|
});
|
|
65
61
|
}
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@signalsafe/tree-spec-editor",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Reusable TreeSpec graph editor (React Flow) and authoring helpers (no API or routing)",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
|
+
"docs",
|
|
26
27
|
"README.md"
|
|
27
28
|
],
|
|
28
29
|
"publishConfig": {
|
|
@@ -57,6 +58,7 @@
|
|
|
57
58
|
"@types/react-dom": "^18.2.22",
|
|
58
59
|
"react": "^18.3.1",
|
|
59
60
|
"react-dom": "^18.3.1",
|
|
61
|
+
"react-test-renderer": "18.3.1",
|
|
60
62
|
"reactflow": "^11.11.4",
|
|
61
63
|
"typescript": "^5.4.2"
|
|
62
64
|
}
|