gradio-pr-bot commited on
Commit
fa6b54c
·
verified ·
1 Parent(s): 0ae8788

Upload folder using huggingface_hub

Browse files
6.19.0/workflowcanvas/workflow/WorkflowApiPanel.svelte ADDED
@@ -0,0 +1,450 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount } from "svelte";
3
+
4
+ interface ApiParam {
5
+ label: string;
6
+ parameter_name?: string;
7
+ type: string;
8
+ python_type: string;
9
+ }
10
+ interface ApiEndpoint {
11
+ api_name: string;
12
+ label: string;
13
+ parameters: ApiParam[];
14
+ returns: ApiParam[];
15
+ }
16
+
17
+ let {
18
+ server = {},
19
+ workflowName = "Workflow",
20
+ onClose
21
+ }: {
22
+ server?: Record<string, any>;
23
+ workflowName?: string;
24
+ onClose: () => void;
25
+ } = $props();
26
+
27
+ type Lang = "python" | "javascript" | "bash";
28
+ const LANGS: { key: Lang; label: string }[] = [
29
+ { key: "python", label: "Python" },
30
+ { key: "javascript", label: "JavaScript" },
31
+ { key: "bash", label: "curl" }
32
+ ];
33
+
34
+ let endpoints = $state<ApiEndpoint[]>([]);
35
+ let loading = $state(true);
36
+ let error = $state<string | null>(null);
37
+ let lang = $state<Lang>("python");
38
+ let copied = $state<string | null>(null);
39
+
40
+ // The Workflow app is served at the site root, so the API base is the
41
+ // current origin (+ any sub-path the app is mounted under, minus a trailing
42
+ // slash).
43
+ const root =
44
+ typeof window !== "undefined"
45
+ ? (window.location.origin + window.location.pathname).replace(/\/$/, "")
46
+ : "";
47
+
48
+ onMount(async () => {
49
+ try {
50
+ if (!server?.get_workflow_api) {
51
+ throw new Error("This server does not expose a workflow API.");
52
+ }
53
+ const raw = await server.get_workflow_api();
54
+ const parsed = JSON.parse(raw);
55
+ endpoints = parsed.endpoints ?? [];
56
+ } catch (e) {
57
+ error = e instanceof Error ? e.message : String(e);
58
+ } finally {
59
+ loading = false;
60
+ }
61
+ });
62
+
63
+ function example(p: ApiParam): string {
64
+ if (
65
+ p.python_type === "filepath" ||
66
+ p.python_type.startsWith("list[filepath")
67
+ )
68
+ return lang === "python"
69
+ ? `handle_file('https://example.com/file')`
70
+ : `"https://example.com/file"`;
71
+ if (p.python_type === "float") return "3";
72
+ if (p.python_type === "bool") return lang === "python" ? "True" : "true";
73
+ if (p.python_type === "dict") return "{}";
74
+ return lang === "python" ? '"Hello!!"' : '"Hello!!"';
75
+ }
76
+
77
+ function pySnippet(ep: ApiEndpoint): string {
78
+ const needsHandleFile = ep.parameters.some((p) =>
79
+ p.python_type.includes("filepath")
80
+ );
81
+ const imports = needsHandleFile
82
+ ? "from gradio_client import Client, handle_file"
83
+ : "from gradio_client import Client";
84
+ const args = ep.parameters.map((p) => `\t\t${paramName(p)}=${example(p)},`);
85
+ return [
86
+ imports,
87
+ "",
88
+ `client = Client("${root}")`,
89
+ "result = client.predict(",
90
+ ...args,
91
+ `\t\tapi_name="${ep.api_name}"`,
92
+ ")",
93
+ "print(result)"
94
+ ].join("\n");
95
+ }
96
+
97
+ function jsSnippet(ep: ApiEndpoint): string {
98
+ const args = ep.parameters
99
+ .map((p) => `\t${paramName(p)}: ${example(p)}`)
100
+ .join(",\n");
101
+ return [
102
+ 'import { Client } from "@gradio/client";',
103
+ "",
104
+ `const client = await Client.connect("${root}");`,
105
+ `const result = await client.predict("${ep.api_name}", {`,
106
+ args,
107
+ "});",
108
+ "console.log(result.data);"
109
+ ].join("\n");
110
+ }
111
+
112
+ function bashSnippet(ep: ApiEndpoint): string {
113
+ const data = ep.parameters.map((p) => example(p)).join(", ");
114
+ return [
115
+ `curl -X POST ${root}/gradio_api/call${ep.api_name} \\`,
116
+ `\t-H "Content-Type: application/json" \\`,
117
+ `\t-d '{"data": [${data}]}' \\`,
118
+ `\t| awk -F'"' '{ print $4 }' \\`,
119
+ `\t| xargs -I {} curl -N ${root}/gradio_api/call${ep.api_name}/{}`
120
+ ].join("\n");
121
+ }
122
+
123
+ function argName(label: string): string {
124
+ return (
125
+ label
126
+ .trim()
127
+ .toLowerCase()
128
+ .replace(/[^a-z0-9]+/g, "_")
129
+ .replace(/^_+|_+$/g, "") || "value"
130
+ );
131
+ }
132
+
133
+ function paramName(param: ApiParam): string {
134
+ return param.parameter_name || argName(param.label);
135
+ }
136
+
137
+ function snippet(ep: ApiEndpoint): string {
138
+ if (lang === "python") return pySnippet(ep);
139
+ if (lang === "javascript") return jsSnippet(ep);
140
+ return bashSnippet(ep);
141
+ }
142
+
143
+ async function copy(ep: ApiEndpoint): Promise<void> {
144
+ try {
145
+ await navigator.clipboard.writeText(snippet(ep));
146
+ copied = ep.api_name;
147
+ setTimeout(() => {
148
+ if (copied === ep.api_name) copied = null;
149
+ }, 1400);
150
+ } catch {
151
+ /* clipboard unavailable */
152
+ }
153
+ }
154
+ </script>
155
+
156
+ <!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
157
+ <div class="api-overlay" onclick={onClose}>
158
+ <div class="api-panel" onclick={(e) => e.stopPropagation()}>
159
+ <div class="api-header">
160
+ <div class="api-title">
161
+ <span class="api-glyph">&lt;/&gt;</span>
162
+ <div>
163
+ <div class="api-title-main">API</div>
164
+ <div class="api-title-sub">
165
+ Call <strong>{workflowName}</strong> programmatically — one endpoint per
166
+ subgraph
167
+ </div>
168
+ </div>
169
+ </div>
170
+ <button class="api-close" onclick={onClose} title="Close">&times;</button>
171
+ </div>
172
+
173
+ <div class="api-langs">
174
+ {#each LANGS as l}
175
+ <button
176
+ class="api-lang"
177
+ class:active={lang === l.key}
178
+ onclick={() => (lang = l.key)}>{l.label}</button
179
+ >
180
+ {/each}
181
+ </div>
182
+
183
+ <div class="api-body">
184
+ {#if loading}
185
+ <div class="api-empty">Loading API…</div>
186
+ {:else if error}
187
+ <div class="api-empty api-error">{error}</div>
188
+ {:else if endpoints.length === 0}
189
+ <div class="api-empty">
190
+ No API endpoints yet — add an output node to expose one.
191
+ </div>
192
+ {:else}
193
+ {#each endpoints as ep}
194
+ <div class="api-endpoint">
195
+ <div class="api-endpoint-head">
196
+ <span class="api-method">POST</span>
197
+ <span class="api-name">{ep.api_name}</span>
198
+ <button class="api-copy" onclick={() => copy(ep)}>
199
+ {copied === ep.api_name ? "Copied!" : "Copy"}
200
+ </button>
201
+ </div>
202
+
203
+ <div class="api-io">
204
+ <div class="api-io-col">
205
+ <div class="api-io-label">
206
+ Accepts {ep.parameters.length} parameter{ep.parameters
207
+ .length === 1
208
+ ? ""
209
+ : "s"}
210
+ </div>
211
+ {#each ep.parameters as p}
212
+ <div class="api-port">
213
+ <span class="api-port-name">{paramName(p)}</span>
214
+ <span class="api-port-type">{p.python_type}</span>
215
+ </div>
216
+ {:else}
217
+ <div class="api-port api-port-empty">no inputs</div>
218
+ {/each}
219
+ </div>
220
+ <div class="api-io-col">
221
+ <div class="api-io-label">Returns</div>
222
+ {#each ep.returns as r}
223
+ <div class="api-port">
224
+ <span class="api-port-name">{paramName(r)}</span>
225
+ <span class="api-port-type">{r.python_type}</span>
226
+ </div>
227
+ {/each}
228
+ </div>
229
+ </div>
230
+
231
+ <pre class="api-code"><code>{snippet(ep)}</code></pre>
232
+ </div>
233
+ {/each}
234
+ {/if}
235
+ </div>
236
+ </div>
237
+ </div>
238
+
239
+ <style>
240
+ .api-overlay {
241
+ position: fixed;
242
+ inset: 0;
243
+ background: rgba(8, 9, 13, 0.72);
244
+ backdrop-filter: blur(2px);
245
+ display: flex;
246
+ align-items: center;
247
+ justify-content: center;
248
+ z-index: 1000;
249
+ padding: 24px;
250
+ }
251
+ .api-panel {
252
+ width: min(720px, 100%);
253
+ max-height: 86vh;
254
+ display: flex;
255
+ flex-direction: column;
256
+ background: #101118;
257
+ border: 1px solid #2a2b38;
258
+ border-radius: 12px;
259
+ box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
260
+ overflow: hidden;
261
+ }
262
+ .api-header {
263
+ display: flex;
264
+ align-items: flex-start;
265
+ justify-content: space-between;
266
+ padding: 16px 18px;
267
+ border-bottom: 1px solid #1e1f2a;
268
+ }
269
+ .api-title {
270
+ display: flex;
271
+ gap: 12px;
272
+ align-items: center;
273
+ }
274
+ .api-glyph {
275
+ font-family: "JetBrains Mono", monospace;
276
+ font-size: 13px;
277
+ color: var(--color-accent, #f97316);
278
+ background: #1a1b25;
279
+ border: 1px solid #2a2b38;
280
+ border-radius: 8px;
281
+ padding: 8px 9px;
282
+ line-height: 1;
283
+ }
284
+ .api-title-main {
285
+ font-family: "Manrope", sans-serif;
286
+ font-size: 15px;
287
+ font-weight: 700;
288
+ color: #e6e7ec;
289
+ }
290
+ .api-title-sub {
291
+ font-family: "Manrope", sans-serif;
292
+ font-size: 12px;
293
+ color: #8a8c98;
294
+ margin-top: 2px;
295
+ }
296
+ .api-title-sub strong {
297
+ color: #a0a2ae;
298
+ font-weight: 600;
299
+ }
300
+ .api-close {
301
+ background: transparent;
302
+ border: none;
303
+ color: #6b6e78;
304
+ font-size: 22px;
305
+ line-height: 1;
306
+ cursor: pointer;
307
+ padding: 0 4px;
308
+ }
309
+ .api-close:hover {
310
+ color: #e6e7ec;
311
+ }
312
+ .api-langs {
313
+ display: flex;
314
+ gap: 4px;
315
+ padding: 10px 18px;
316
+ border-bottom: 1px solid #1e1f2a;
317
+ }
318
+ .api-lang {
319
+ font-family: "Manrope", sans-serif;
320
+ font-size: 12px;
321
+ font-weight: 500;
322
+ padding: 4px 12px;
323
+ border-radius: 999px;
324
+ border: 1px solid transparent;
325
+ background: transparent;
326
+ color: #6b6e78;
327
+ cursor: pointer;
328
+ }
329
+ .api-lang:hover {
330
+ color: #a0a2ae;
331
+ }
332
+ .api-lang.active {
333
+ background: #1a1b25;
334
+ border-color: #2a2b38;
335
+ color: #e6e7ec;
336
+ }
337
+ .api-body {
338
+ overflow-y: auto;
339
+ padding: 16px 18px;
340
+ display: flex;
341
+ flex-direction: column;
342
+ gap: 16px;
343
+ }
344
+ .api-empty {
345
+ font-family: "Manrope", sans-serif;
346
+ font-size: 13px;
347
+ color: #8a8c98;
348
+ text-align: center;
349
+ padding: 32px 0;
350
+ }
351
+ .api-error {
352
+ color: #f87171;
353
+ }
354
+ .api-endpoint {
355
+ border: 1px solid #1e1f2a;
356
+ border-radius: 10px;
357
+ overflow: hidden;
358
+ }
359
+ .api-endpoint-head {
360
+ display: flex;
361
+ align-items: center;
362
+ gap: 10px;
363
+ padding: 10px 12px;
364
+ background: #16171f;
365
+ border-bottom: 1px solid #1e1f2a;
366
+ }
367
+ .api-method {
368
+ font-family: "JetBrains Mono", monospace;
369
+ font-size: 10px;
370
+ font-weight: 700;
371
+ letter-spacing: 0.05em;
372
+ color: #1a1b25;
373
+ background: var(--color-accent, #f97316);
374
+ border-radius: 4px;
375
+ padding: 2px 6px;
376
+ }
377
+ .api-name {
378
+ font-family: "JetBrains Mono", monospace;
379
+ font-size: 13px;
380
+ color: #e6e7ec;
381
+ flex: 1;
382
+ }
383
+ .api-copy {
384
+ font-family: "Manrope", sans-serif;
385
+ font-size: 11px;
386
+ font-weight: 600;
387
+ padding: 4px 10px;
388
+ border: 1px solid #2a2b38;
389
+ border-radius: 6px;
390
+ background: transparent;
391
+ color: #a0a2ae;
392
+ cursor: pointer;
393
+ }
394
+ .api-copy:hover {
395
+ background: #1a1b25;
396
+ color: #e6e7ec;
397
+ }
398
+ .api-io {
399
+ display: flex;
400
+ gap: 24px;
401
+ padding: 12px;
402
+ border-bottom: 1px solid #1e1f2a;
403
+ }
404
+ .api-io-col {
405
+ flex: 1;
406
+ min-width: 0;
407
+ }
408
+ .api-io-label {
409
+ font-family: "Manrope", sans-serif;
410
+ font-size: 10px;
411
+ font-weight: 600;
412
+ text-transform: uppercase;
413
+ letter-spacing: 0.06em;
414
+ color: #6b6e78;
415
+ margin-bottom: 6px;
416
+ }
417
+ .api-port {
418
+ display: flex;
419
+ justify-content: space-between;
420
+ gap: 8px;
421
+ font-family: "JetBrains Mono", monospace;
422
+ font-size: 12px;
423
+ padding: 3px 0;
424
+ }
425
+ .api-port-name {
426
+ color: #c5c7d0;
427
+ }
428
+ .api-port-type {
429
+ color: var(--color-accent, #f97316);
430
+ }
431
+ .api-port-empty {
432
+ color: #5a5d68;
433
+ font-style: italic;
434
+ }
435
+ .api-code {
436
+ margin: 0;
437
+ padding: 12px 14px;
438
+ background: #0b0c12;
439
+ font-family: "JetBrains Mono", monospace;
440
+ font-size: 12px;
441
+ line-height: 1.55;
442
+ color: #c5c7d0;
443
+ overflow-x: auto;
444
+ white-space: pre;
445
+ tab-size: 2;
446
+ }
447
+ .api-code code {
448
+ font-family: inherit;
449
+ }
450
+ </style>
6.19.0/workflowcanvas/workflow/WorkflowCanvas.svelte CHANGED
@@ -7,6 +7,7 @@
7
  import type { BoundFnTemplate } from "./WorkflowBottomBar.svelte";
8
  import NodeModelPicker from "./NodeModelPicker.svelte";
9
  import WorkflowEmptyState from "./WorkflowEmptyState.svelte";
 
10
  import CheckIcon from "./icons/CheckIcon.svelte";
11
  import LayoutIcon from "./icons/LayoutIcon.svelte";
12
  import InfoIcon from "./icons/InfoIcon.svelte";
@@ -49,6 +50,7 @@
49
  import { stream_text_generation } from "./inference-stream";
50
  import {
51
  findFreeSpot as findFreeSpotImpl,
 
52
  topoSort,
53
  resolveCurrentInputs as resolveCurrentInputsImpl,
54
  computeStaleNodes,
@@ -274,6 +276,7 @@
274
  );
275
  let showShortcuts = $state(false);
276
  let showUserMenu = $state(false);
 
277
  // Popover shown when the "Run only" badge is clicked, explaining why editing
278
  // is disabled and how to enable it.
279
  let showAccessInfo = $state(false);
@@ -337,6 +340,9 @@
337
  const nodeCount = $derived(legacyView.nodes.length);
338
  const hasTransforms = $derived($workflow.operators.length > 0);
339
  const edgeCount = $derived($workflow.edges.length);
 
 
 
340
 
341
  const connectedPortsSet = $derived(() => {
342
  const set = new Set<string>();
@@ -1440,15 +1446,17 @@
1440
  callModelWithToken,
1441
  fetchDatasetWithToken,
1442
  callFnWithToken,
1443
- (modelId, prompt, provider, signal, onChunk) =>
1444
- stream_text_generation({
1445
- modelId,
1446
- prompt,
1447
- provider,
1448
- hfToken: auth.token || undefined,
1449
- signal: signal ?? undefined,
1450
- onChunk
1451
- })
 
 
1452
  );
1453
 
1454
  running = false;
@@ -2013,8 +2021,17 @@
2013
  </button>
2014
  {/if}
2015
  <span class="toolbar-stat"
2016
- >{nodeCount} nodes &middot; {edgeCount} edges</span
 
 
 
 
 
 
2017
  >
 
 
 
2018
  {#if saveIndicator}
2019
  <span
2020
  class="save-indicator"
@@ -2482,6 +2499,14 @@
2482
  </div>
2483
  </div>
2484
  {/if}
 
 
 
 
 
 
 
 
2485
  </div>
2486
 
2487
  <style>
 
7
  import type { BoundFnTemplate } from "./WorkflowBottomBar.svelte";
8
  import NodeModelPicker from "./NodeModelPicker.svelte";
9
  import WorkflowEmptyState from "./WorkflowEmptyState.svelte";
10
+ import WorkflowApiPanel from "./WorkflowApiPanel.svelte";
11
  import CheckIcon from "./icons/CheckIcon.svelte";
12
  import LayoutIcon from "./icons/LayoutIcon.svelte";
13
  import InfoIcon from "./icons/InfoIcon.svelte";
 
50
  import { stream_text_generation } from "./inference-stream";
51
  import {
52
  findFreeSpot as findFreeSpotImpl,
53
+ countSubgraphs,
54
  topoSort,
55
  resolveCurrentInputs as resolveCurrentInputsImpl,
56
  computeStaleNodes,
 
276
  );
277
  let showShortcuts = $state(false);
278
  let showUserMenu = $state(false);
279
+ let showApiPanel = $state(false);
280
  // Popover shown when the "Run only" badge is clicked, explaining why editing
281
  // is disabled and how to enable it.
282
  let showAccessInfo = $state(false);
 
340
  const nodeCount = $derived(legacyView.nodes.length);
341
  const hasTransforms = $derived($workflow.operators.length > 0);
342
  const edgeCount = $derived($workflow.edges.length);
343
+ const subgraphCount = $derived(
344
+ countSubgraphs(legacyView.nodes, $workflow.edges)
345
+ );
346
 
347
  const connectedPortsSet = $derived(() => {
348
  const set = new Set<string>();
 
1446
  callModelWithToken,
1447
  fetchDatasetWithToken,
1448
  callFnWithToken,
1449
+ auth.token
1450
+ ? (modelId, prompt, provider, signal, onChunk) =>
1451
+ stream_text_generation({
1452
+ modelId,
1453
+ prompt,
1454
+ provider,
1455
+ hfToken: auth.token,
1456
+ signal: signal ?? undefined,
1457
+ onChunk
1458
+ })
1459
+ : undefined
1460
  );
1461
 
1462
  running = false;
 
2021
  </button>
2022
  {/if}
2023
  <span class="toolbar-stat"
2024
+ >{subgraphCount}
2025
+ {subgraphCount === 1 ? "subgraph" : "subgraphs"}</span
2026
+ >
2027
+ <button
2028
+ class="tool-btn api-btn"
2029
+ onclick={() => (showApiPanel = true)}
2030
+ title="View the API for this workflow"
2031
  >
2032
+ <span class="api-btn-glyph">&lt;/&gt;</span>
2033
+ View API
2034
+ </button>
2035
  {#if saveIndicator}
2036
  <span
2037
  class="save-indicator"
 
2499
  </div>
2500
  </div>
2501
  {/if}
2502
+
2503
+ {#if showApiPanel}
2504
+ <WorkflowApiPanel
2505
+ {server}
2506
+ workflowName={$workflow.name}
2507
+ onClose={() => (showApiPanel = false)}
2508
+ />
2509
+ {/if}
2510
  </div>
2511
 
2512
  <style>
6.19.0/workflowcanvas/workflow/workflow-graph.ts CHANGED
@@ -27,6 +27,34 @@ export function findFreeSpot(
27
  return { x: nx, y: ny };
28
  }
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  /**
31
  * Topological sort: returns a sorted list of nodes such that every edge
32
  * goes from an earlier node to a later one. Standard Kahn's algorithm.
 
27
  return { x: nx, y: ny };
28
  }
29
 
30
+ /**
31
+ * Count the number of independent subgraphs (weakly connected components)
32
+ * in the workflow — i.e. how many disconnected pipelines exist. Edges are
33
+ * treated as undirected; an unconnected node counts as its own subgraph.
34
+ * This is what the toolbar surfaces instead of raw node/edge counts: it
35
+ * maps to the number of distinct things the workflow produces.
36
+ */
37
+ export function countSubgraphs(nodes: WFNode[], edges: WFEdge[]): number {
38
+ const parent = new Map<string, string>(nodes.map((n) => [n.id, n.id]));
39
+ const find = (id: string): string => {
40
+ let root = id;
41
+ while (parent.get(root) !== root) root = parent.get(root)!;
42
+ while (parent.get(id) !== root) {
43
+ const next = parent.get(id)!;
44
+ parent.set(id, root);
45
+ id = next;
46
+ }
47
+ return root;
48
+ };
49
+ for (const e of edges) {
50
+ if (!parent.has(e.from_node_id) || !parent.has(e.to_node_id)) continue;
51
+ parent.set(find(e.from_node_id), find(e.to_node_id));
52
+ }
53
+ const roots = new Set<string>();
54
+ for (const n of nodes) roots.add(find(n.id));
55
+ return roots.size;
56
+ }
57
+
58
  /**
59
  * Topological sort: returns a sorted list of nodes such that every edge
60
  * goes from an earlier node to a later one. Standard Kahn's algorithm.