@ghchinoy/litflow 0.2.8 → 0.3.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.
Files changed (158) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/breadboard/data/common.d.ts +35 -0
  3. package/dist/breadboard/engine/loader/capability.d.ts +21 -0
  4. package/dist/breadboard/engine/loader/loader.d.ts +29 -0
  5. package/dist/breadboard/engine/loader/resolve-graph-urls.d.ts +16 -0
  6. package/dist/breadboard/engine/runtime/bubble.d.ts +23 -0
  7. package/dist/breadboard/engine/runtime/graph-based-node-handler.d.ts +16 -0
  8. package/dist/breadboard/engine/runtime/handler.d.ts +27 -0
  9. package/dist/breadboard/engine/runtime/harness/events.d.ts +145 -0
  10. package/dist/breadboard/engine/runtime/harness/local.d.ts +10 -0
  11. package/dist/breadboard/engine/runtime/harness/plan-runner.d.ts +25 -0
  12. package/dist/breadboard/engine/runtime/run/invoke-graph.d.ts +12 -0
  13. package/dist/breadboard/engine/runtime/run/node-invoker.d.ts +12 -0
  14. package/dist/breadboard/engine/runtime/run/run-graph.d.ts +12 -0
  15. package/dist/breadboard/engine/runtime/run.d.ts +29 -0
  16. package/dist/breadboard/engine/runtime/sandbox/capabilities-manager.d.ts +15 -0
  17. package/dist/breadboard/engine/runtime/sandbox/file-system-handler-factory.d.ts +15 -0
  18. package/dist/breadboard/engine/runtime/sandbox/invoke-describer.d.ts +10 -0
  19. package/dist/breadboard/engine/runtime/static/create-plan.d.ts +14 -0
  20. package/dist/breadboard/engine/runtime/static/orchestrator.d.ts +72 -0
  21. package/dist/breadboard/engine/runtime/traversal/index.d.ts +20 -0
  22. package/dist/breadboard/engine/runtime/traversal/iterator.d.ts +12 -0
  23. package/dist/breadboard/engine/runtime/traversal/machine.d.ts +14 -0
  24. package/dist/breadboard/engine/runtime/traversal/representation.d.ts +27 -0
  25. package/dist/breadboard/engine/runtime/traversal/result.d.ts +24 -0
  26. package/dist/breadboard/engine/runtime/traversal/state.d.ts +34 -0
  27. package/dist/breadboard/lit-flow-runner.d.ts +13 -0
  28. package/dist/breadboard/lit-flow-runner.test.d.ts +1 -0
  29. package/dist/breadboard/runner.d.ts +13 -0
  30. package/dist/index.d.ts +2 -0
  31. package/dist/lit-chiclet.d.ts +9 -0
  32. package/dist/lit-schema-node.d.ts +13 -0
  33. package/dist/lit-schema-node.test.d.ts +1 -0
  34. package/dist/litflow.js +708 -442
  35. package/dist/litflow.js.map +1 -1
  36. package/package.json +15 -3
  37. package/src/breadboard/data/common.ts +450 -0
  38. package/src/breadboard/data/file-system.ts +54 -0
  39. package/src/breadboard/data/inline-all-content.ts +126 -0
  40. package/src/breadboard/data/recent-boards.ts +118 -0
  41. package/src/breadboard/data/save-outputs-as-file.ts +104 -0
  42. package/src/breadboard/engine/add-run-module.ts +168 -0
  43. package/src/breadboard/engine/editor/blank.ts +65 -0
  44. package/src/breadboard/engine/editor/edge.ts +27 -0
  45. package/src/breadboard/engine/editor/events.ts +64 -0
  46. package/src/breadboard/engine/editor/graph.ts +383 -0
  47. package/src/breadboard/engine/editor/history.ts +98 -0
  48. package/src/breadboard/engine/editor/index.ts +8 -0
  49. package/src/breadboard/engine/editor/operations/add-asset.ts +45 -0
  50. package/src/breadboard/engine/editor/operations/add-edge.ts +142 -0
  51. package/src/breadboard/engine/editor/operations/add-graph.ts +47 -0
  52. package/src/breadboard/engine/editor/operations/add-module.ts +64 -0
  53. package/src/breadboard/engine/editor/operations/add-node.ts +86 -0
  54. package/src/breadboard/engine/editor/operations/change-asset-metadata.ts +70 -0
  55. package/src/breadboard/engine/editor/operations/change-configuration.ts +82 -0
  56. package/src/breadboard/engine/editor/operations/change-edge-metadata.ts +58 -0
  57. package/src/breadboard/engine/editor/operations/change-edge.ts +111 -0
  58. package/src/breadboard/engine/editor/operations/change-graph-metadata.ts +52 -0
  59. package/src/breadboard/engine/editor/operations/change-metadata.ts +92 -0
  60. package/src/breadboard/engine/editor/operations/change-module.ts +64 -0
  61. package/src/breadboard/engine/editor/operations/error.ts +21 -0
  62. package/src/breadboard/engine/editor/operations/remove-asset.ts +48 -0
  63. package/src/breadboard/engine/editor/operations/remove-edge.ts +89 -0
  64. package/src/breadboard/engine/editor/operations/remove-graph.ts +49 -0
  65. package/src/breadboard/engine/editor/operations/remove-integration.ts +54 -0
  66. package/src/breadboard/engine/editor/operations/remove-module.ts +69 -0
  67. package/src/breadboard/engine/editor/operations/remove-node.ts +86 -0
  68. package/src/breadboard/engine/editor/operations/replace-graph.ts +52 -0
  69. package/src/breadboard/engine/editor/operations/toggle-export.ts +72 -0
  70. package/src/breadboard/engine/editor/operations/upsert-integration.ts +43 -0
  71. package/src/breadboard/engine/editor/selection.ts +58 -0
  72. package/src/breadboard/engine/editor/transforms/configure-sidewire.ts +73 -0
  73. package/src/breadboard/engine/editor/transforms/isolate-selection.ts +54 -0
  74. package/src/breadboard/engine/editor/transforms/merge-graph.ts +58 -0
  75. package/src/breadboard/engine/editor/transforms/move-to-graph.ts +102 -0
  76. package/src/breadboard/engine/editor/transforms/move-to-new-graph.ts +72 -0
  77. package/src/breadboard/engine/editor/transforms/sidewire-to-new-graph.ts +82 -0
  78. package/src/breadboard/engine/file-system/blob-transform.ts +44 -0
  79. package/src/breadboard/engine/file-system/composed-peristent-backend.ts +140 -0
  80. package/src/breadboard/engine/file-system/ephemeral-blob-store.ts +46 -0
  81. package/src/breadboard/engine/file-system/in-memory-blob-store.ts +87 -0
  82. package/src/breadboard/engine/file-system/index.ts +723 -0
  83. package/src/breadboard/engine/file-system/partial-persistent-backend.ts +109 -0
  84. package/src/breadboard/engine/file-system/path.ts +125 -0
  85. package/src/breadboard/engine/file-system/persistent-file.ts +66 -0
  86. package/src/breadboard/engine/file-system/readable-stream-file.ts +61 -0
  87. package/src/breadboard/engine/file-system/stub-file-system.ts +47 -0
  88. package/src/breadboard/engine/file-system/utils.ts +40 -0
  89. package/src/breadboard/engine/inspector/graph/bubbled-node.ts +162 -0
  90. package/src/breadboard/engine/inspector/graph/describe-cache.ts +78 -0
  91. package/src/breadboard/engine/inspector/graph/describe-type-cache.ts +48 -0
  92. package/src/breadboard/engine/inspector/graph/edge-cache.ts +118 -0
  93. package/src/breadboard/engine/inspector/graph/edge.ts +133 -0
  94. package/src/breadboard/engine/inspector/graph/event.ts +35 -0
  95. package/src/breadboard/engine/inspector/graph/exports-describer.ts +45 -0
  96. package/src/breadboard/engine/inspector/graph/graph-cache.ts +54 -0
  97. package/src/breadboard/engine/inspector/graph/graph-describer-manager.ts +338 -0
  98. package/src/breadboard/engine/inspector/graph/graph-descriptor-handle.ts +73 -0
  99. package/src/breadboard/engine/inspector/graph/graph-node-type.ts +111 -0
  100. package/src/breadboard/engine/inspector/graph/graph-queries.ts +256 -0
  101. package/src/breadboard/engine/inspector/graph/graph.ts +163 -0
  102. package/src/breadboard/engine/inspector/graph/inspectable-asset.ts +36 -0
  103. package/src/breadboard/engine/inspector/graph/kits.ts +208 -0
  104. package/src/breadboard/engine/inspector/graph/module.ts +69 -0
  105. package/src/breadboard/engine/inspector/graph/mutable-graph.ts +150 -0
  106. package/src/breadboard/engine/inspector/graph/node-cache.ts +123 -0
  107. package/src/breadboard/engine/inspector/graph/node-describer-manager.ts +279 -0
  108. package/src/breadboard/engine/inspector/graph/node-type-describer-manager.ts +122 -0
  109. package/src/breadboard/engine/inspector/graph/node.ts +149 -0
  110. package/src/breadboard/engine/inspector/graph/port-cache.ts +80 -0
  111. package/src/breadboard/engine/inspector/graph/ports.ts +292 -0
  112. package/src/breadboard/engine/inspector/graph/schemas.ts +131 -0
  113. package/src/breadboard/engine/inspector/graph/virtual-node.ts +184 -0
  114. package/src/breadboard/engine/inspector/graph-store.ts +629 -0
  115. package/src/breadboard/engine/inspector/index.ts +13 -0
  116. package/src/breadboard/engine/inspector/utils.ts +20 -0
  117. package/src/breadboard/engine/loader/capability.ts +184 -0
  118. package/src/breadboard/engine/loader/index.ts +14 -0
  119. package/src/breadboard/engine/loader/loader.ts +244 -0
  120. package/src/breadboard/engine/loader/resolve-graph-urls.ts +111 -0
  121. package/src/breadboard/engine/runtime/bubble.ts +269 -0
  122. package/src/breadboard/engine/runtime/graph-based-node-handler.ts +174 -0
  123. package/src/breadboard/engine/runtime/handler.ts +166 -0
  124. package/src/breadboard/engine/runtime/harness/diagnostics.ts +22 -0
  125. package/src/breadboard/engine/runtime/harness/events.ts +217 -0
  126. package/src/breadboard/engine/runtime/harness/index.ts +14 -0
  127. package/src/breadboard/engine/runtime/harness/local.ts +48 -0
  128. package/src/breadboard/engine/runtime/harness/plan-runner.ts +759 -0
  129. package/src/breadboard/engine/runtime/index.ts +8 -0
  130. package/src/breadboard/engine/runtime/legacy.ts +28 -0
  131. package/src/breadboard/engine/runtime/run/invoke-graph.ts +79 -0
  132. package/src/breadboard/engine/runtime/run/node-invoker.ts +137 -0
  133. package/src/breadboard/engine/runtime/run/run-graph.ts +186 -0
  134. package/src/breadboard/engine/runtime/run.ts +111 -0
  135. package/src/breadboard/engine/runtime/sandbox/capabilities-manager.ts +253 -0
  136. package/src/breadboard/engine/runtime/sandbox/file-system-handler-factory.ts +53 -0
  137. package/src/breadboard/engine/runtime/sandbox/invoke-describer.ts +125 -0
  138. package/src/breadboard/engine/runtime/static/condense.ts +155 -0
  139. package/src/breadboard/engine/runtime/static/create-plan.ts +134 -0
  140. package/src/breadboard/engine/runtime/static/nodes-to-subgraph.ts +168 -0
  141. package/src/breadboard/engine/runtime/static/orchestrator.ts +664 -0
  142. package/src/breadboard/engine/runtime/static/types.ts +77 -0
  143. package/src/breadboard/engine/runtime/traversal/index.ts +58 -0
  144. package/src/breadboard/engine/runtime/traversal/iterator.ts +124 -0
  145. package/src/breadboard/engine/runtime/traversal/machine.ts +58 -0
  146. package/src/breadboard/engine/runtime/traversal/representation.ts +115 -0
  147. package/src/breadboard/engine/runtime/traversal/result.ts +72 -0
  148. package/src/breadboard/engine/runtime/traversal/state.ts +115 -0
  149. package/src/breadboard/engine/telemetry.ts +121 -0
  150. package/src/breadboard/engine/types.ts +32 -0
  151. package/src/breadboard/lit-flow-runner.test.ts +44 -0
  152. package/src/breadboard/lit-flow-runner.ts +98 -0
  153. package/src/breadboard/runner.ts +80 -0
  154. package/src/index.ts +2 -0
  155. package/src/lit-chiclet.ts +69 -0
  156. package/src/lit-flow.ts +17 -7
  157. package/src/lit-schema-node.test.ts +65 -0
  158. package/src/lit-schema-node.ts +194 -0
@@ -0,0 +1,69 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @license
4
+ * Copyright 2024 Google LLC
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+
8
+ import type {
9
+ GraphDescriptor,
10
+ InspectableModule,
11
+ InspectableModuleCache,
12
+ InspectableModules,
13
+ ModuleCode,
14
+ ModuleIdentifier,
15
+ ModuleMetadata,
16
+ Module as ModuleType,
17
+ } from "@breadboard-ai/types";
18
+
19
+ class Module implements InspectableModule {
20
+ #code: ModuleCode | undefined;
21
+ #metadata: ModuleMetadata | undefined;
22
+
23
+ constructor({ code, metadata }: ModuleType) {
24
+ this.#code = code;
25
+ this.#metadata = metadata;
26
+ }
27
+
28
+ code(): ModuleCode {
29
+ return this.#code ?? "";
30
+ }
31
+
32
+ metadata(): ModuleMetadata {
33
+ return this.#metadata ?? {};
34
+ }
35
+ }
36
+
37
+ export class ModuleCache implements InspectableModuleCache {
38
+ #modules: Record<ModuleIdentifier, InspectableModule> = {};
39
+
40
+ get(id: ModuleIdentifier): InspectableModule | undefined {
41
+ return this.#modules[id];
42
+ }
43
+
44
+ add(id: ModuleIdentifier, module: ModuleType): void {
45
+ this.#modules[id] = new Module(module);
46
+ }
47
+
48
+ remove(id: ModuleIdentifier): void {
49
+ if (!this.#modules[id]) {
50
+ return;
51
+ }
52
+
53
+ delete this.#modules[id];
54
+ }
55
+
56
+ modules(): InspectableModules {
57
+ return this.#modules ?? {};
58
+ }
59
+
60
+ rebuild(graph: GraphDescriptor) {
61
+ if (!graph.modules) {
62
+ return;
63
+ }
64
+
65
+ for (const [id, module] of Object.entries(graph.modules)) {
66
+ this.add(id, module);
67
+ }
68
+ }
69
+ }
@@ -0,0 +1,150 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @license
4
+ * Copyright 2024 Google LLC
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+
8
+ import { GraphRepresentationImpl } from "../../runtime/legacy.js";
9
+ import type {
10
+ AffectedNode,
11
+ GraphDescriptor,
12
+ GraphIdentifier,
13
+ GraphRepresentation,
14
+ InspectableDescriberResultCache,
15
+ InspectableEdgeCache,
16
+ InspectableGraphCache,
17
+ InspectableKitCache,
18
+ InspectableModuleCache,
19
+ InspectableNodeCache,
20
+ InspectablePortCache,
21
+ KitDescriptor,
22
+ MainGraphIdentifier,
23
+ ModuleIdentifier,
24
+ MutableGraph,
25
+ MutableGraphStore,
26
+ } from "@breadboard-ai/types";
27
+ import { isImperativeGraph, toDeclarativeGraph } from "@breadboard-ai/utils";
28
+ import { DescribeResultCache } from "./describe-cache.js";
29
+ import { EdgeCache } from "./edge-cache.js";
30
+ import { Edge } from "./edge.js";
31
+ import { UpdateEvent } from "./event.js";
32
+ import { GraphCache } from "./graph-cache.js";
33
+ import { Graph } from "./graph.js";
34
+ import { KitCache } from "./kits.js";
35
+ import { ModuleCache } from "./module.js";
36
+ import { NodeCache } from "./node-cache.js";
37
+ import { NodeDescriberManager } from "./node-describer-manager.js";
38
+ import { Node } from "./node.js";
39
+ import { PortCache } from "./port-cache.js";
40
+
41
+ export { MutableGraphImpl };
42
+
43
+ class MutableGraphImpl implements MutableGraph {
44
+ readonly store: MutableGraphStore;
45
+ readonly id: MainGraphIdentifier;
46
+
47
+ legacyKitMetadata: KitDescriptor | null = null;
48
+
49
+ graph!: GraphDescriptor;
50
+ graphs!: InspectableGraphCache;
51
+ nodes!: InspectableNodeCache;
52
+ edges!: InspectableEdgeCache;
53
+ modules!: InspectableModuleCache;
54
+ describe!: InspectableDescriberResultCache;
55
+ kits!: InspectableKitCache;
56
+ ports!: InspectablePortCache;
57
+ representation!: GraphRepresentation;
58
+
59
+ constructor(graph: GraphDescriptor, store: MutableGraphStore) {
60
+ this.store = store;
61
+ this.id = crypto.randomUUID();
62
+ this.rebuild(graph);
63
+ }
64
+
65
+ update(
66
+ graph: GraphDescriptor,
67
+ visualOnly: boolean,
68
+ affectedNodes: AffectedNode[],
69
+ affectedModules: ModuleIdentifier[],
70
+ topologyChange: boolean
71
+ ): void {
72
+ // TODO: Handle this a better way?
73
+ for (const id of affectedModules) {
74
+ this.modules.remove(id);
75
+ if (!graph.modules || !graph.modules[id]) {
76
+ continue;
77
+ }
78
+
79
+ this.modules.add(id, graph.modules[id]);
80
+
81
+ // Find any nodes configured to use this module and clear its describer.
82
+ const runModulesNodes = this.nodes.byType("runModule", "");
83
+ for (const node of runModulesNodes) {
84
+ if (
85
+ node.configuration().$module &&
86
+ node.configuration().$module === id &&
87
+ !affectedNodes.find((n) => n.id === node.descriptor.id)
88
+ ) {
89
+ affectedNodes.push({
90
+ id: node.descriptor.id,
91
+ graphId: "",
92
+ });
93
+ visualOnly = false;
94
+ }
95
+ }
96
+ }
97
+
98
+ // TODO: Handle removals, etc.
99
+ if (!visualOnly) {
100
+ this.describe.update(affectedNodes);
101
+ this.store.dispatchEvent(
102
+ new UpdateEvent(this.id, "", "", [], topologyChange)
103
+ );
104
+ }
105
+ this.representation = new GraphRepresentationImpl(graph);
106
+ this.graph = graph;
107
+ }
108
+
109
+ addSubgraph(subgraph: GraphDescriptor, graphId: GraphIdentifier): void {
110
+ this.graphs.add(graphId);
111
+ this.nodes.addSubgraphNodes(subgraph, graphId);
112
+ this.edges.addSubgraphEdges(subgraph, graphId);
113
+ }
114
+
115
+ removeSubgraph(graphId: GraphIdentifier): void {
116
+ this.graphs.remove(graphId);
117
+ this.nodes.removeSubgraphNodes(graphId);
118
+ this.edges.removeSubgraphEdges(graphId);
119
+ }
120
+
121
+ rebuild(graph: GraphDescriptor) {
122
+ if (isImperativeGraph(graph)) {
123
+ graph = toDeclarativeGraph(graph);
124
+ }
125
+ this.representation = new GraphRepresentationImpl(graph);
126
+ this.graph = graph;
127
+ this.nodes = new NodeCache((descriptor, graphId) => {
128
+ const graph = graphId ? this.graphs.get(graphId) : this;
129
+ if (!graph) {
130
+ throw new Error(
131
+ `Inspect API Integrity error: unable to find subgraph "${graphId}"`
132
+ );
133
+ }
134
+ return new Node(descriptor, this, graphId);
135
+ });
136
+ this.edges = new EdgeCache(
137
+ (edge, graphId) => new Edge(this, edge, graphId)
138
+ );
139
+ this.modules = new ModuleCache();
140
+ this.describe = new DescribeResultCache(new NodeDescriberManager(this));
141
+ this.kits = new KitCache(this);
142
+ this.graphs = new GraphCache((id) => new Graph(id, this));
143
+ this.ports = new PortCache();
144
+ this.graphs.rebuild(graph);
145
+ this.nodes.rebuild(graph);
146
+ this.edges.rebuild(graph);
147
+ this.modules.rebuild(graph);
148
+ this.kits.rebuild(graph);
149
+ }
150
+ }
@@ -0,0 +1,123 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @license
4
+ * Copyright 2024 Google LLC
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+
8
+ import type {
9
+ GraphDescriptor,
10
+ GraphIdentifier,
11
+ InspectableNode,
12
+ InspectableNodeCache,
13
+ NodeDescriptor,
14
+ NodeIdentifier,
15
+ NodeTypeIdentifier,
16
+ } from "@breadboard-ai/types";
17
+ import { Node } from "./node.js";
18
+
19
+ type NodeFactory = (
20
+ node: NodeDescriptor,
21
+ graphId: GraphIdentifier
22
+ ) => InspectableNode;
23
+
24
+ export class NodeCache implements InspectableNodeCache {
25
+ #factory: NodeFactory;
26
+ #map: Map<GraphIdentifier, Map<NodeIdentifier, InspectableNode>> = new Map();
27
+ #typeMap: Map<GraphIdentifier, Map<NodeTypeIdentifier, InspectableNode[]>> =
28
+ new Map();
29
+
30
+ constructor(factory: NodeFactory) {
31
+ this.#factory = factory;
32
+ }
33
+
34
+ rebuild(graph: GraphDescriptor) {
35
+ graph.nodes.forEach((node) => this.#addNodeInternal(node, ""));
36
+ Object.entries(graph.graphs || {}).forEach(([graphId, graph]) => {
37
+ graph.nodes.forEach((node) => this.#addNodeInternal(node, graphId));
38
+ });
39
+ }
40
+
41
+ addSubgraphNodes(subgraph: GraphDescriptor, graphId: GraphIdentifier): void {
42
+ subgraph.nodes.forEach((node) => this.#addNodeInternal(node, graphId));
43
+ }
44
+
45
+ removeSubgraphNodes(graphId: GraphIdentifier): void {
46
+ const subgraph = this.#map.get(graphId);
47
+ subgraph?.forEach((node) => {
48
+ (node as Node).setDeleted();
49
+ });
50
+ this.#map.delete(graphId);
51
+ }
52
+
53
+ #addNodeInternal(node: NodeDescriptor, graphId: GraphIdentifier) {
54
+ const graphTypes = getOrCreate(this.#typeMap, graphId, () => new Map());
55
+ const inspectableNode = this.#factory(node, graphId);
56
+ const type = node.type;
57
+ let list = graphTypes.get(type);
58
+ if (!list) {
59
+ list = [];
60
+ graphTypes.set(type, list);
61
+ }
62
+ list.push(inspectableNode);
63
+ const graphNodes = getOrCreate(this.#map, graphId, () => new Map());
64
+ graphNodes.set(node.id, inspectableNode);
65
+ return inspectableNode;
66
+
67
+ function getOrCreate<K, V>(map: Map<K, V>, key: K, factory: () => V): V {
68
+ let v = map.get(key);
69
+ if (v) return v;
70
+ v = factory();
71
+ map.set(key, v);
72
+ return v;
73
+ }
74
+ }
75
+
76
+ byType(
77
+ type: NodeTypeIdentifier,
78
+ graphId: GraphIdentifier
79
+ ): InspectableNode[] {
80
+ return this.#typeMap.get(graphId)?.get(type) || [];
81
+ }
82
+
83
+ get(
84
+ id: NodeIdentifier,
85
+ graphId: GraphIdentifier
86
+ ): InspectableNode | undefined {
87
+ return this.#map.get(graphId)?.get(id);
88
+ }
89
+
90
+ add(node: NodeDescriptor, graphId: GraphIdentifier) {
91
+ if (!this.#map) {
92
+ return;
93
+ }
94
+ this.#addNodeInternal(node, graphId);
95
+ }
96
+
97
+ remove(id: NodeIdentifier, graphId: GraphIdentifier) {
98
+ if (!this.#map) {
99
+ return;
100
+ }
101
+ const nodeMap = this.#map.get(graphId);
102
+ if (!nodeMap) {
103
+ console.error(
104
+ `Can't remove node "${id}": graph "${graphId}" was not found`
105
+ );
106
+ return;
107
+ }
108
+ const node = nodeMap.get(id) as Node;
109
+ console.assert(node, "Node does not exist in cache.");
110
+ const type = node!.descriptor.type;
111
+ const list = this.#typeMap?.get(graphId)?.get(type);
112
+ if (list) {
113
+ const index = list.indexOf(node!);
114
+ list.splice(index, 1);
115
+ }
116
+ nodeMap.delete(id);
117
+ node.setDeleted();
118
+ }
119
+
120
+ nodes(graphId: GraphIdentifier): InspectableNode[] {
121
+ return Array.from(this.#map.get(graphId)?.values() || []);
122
+ }
123
+ }
@@ -0,0 +1,279 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @license
4
+ * Copyright 2024 Google LLC
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+
8
+ import { createLoader } from "../../loader/index.js";
9
+ import { getHandler, invokeMainDescriber } from "../../runtime/legacy.js";
10
+ import type {
11
+ DescribeResultCacheArgs,
12
+ GraphIdentifier,
13
+ InspectableEdge,
14
+ MutableGraph,
15
+ NodeDescriberContext,
16
+ NodeDescriberFunction,
17
+ NodeDescriberResult,
18
+ NodeHandler,
19
+ NodeIdentifier,
20
+ NodeTypeDescriberOptions,
21
+ NodeTypeIdentifier,
22
+ } from "@breadboard-ai/types";
23
+ import { SchemaDiffer } from "@breadboard-ai/utils";
24
+ import { contextFromMutableGraph } from "../graph-store.js";
25
+ import { UpdateEvent } from "./event.js";
26
+ import { GraphDescriptorHandle } from "./graph-descriptor-handle.js";
27
+ import {
28
+ describeInput,
29
+ describeOutput,
30
+ edgesToSchema,
31
+ EdgeType,
32
+ } from "./schemas.js";
33
+ import {
34
+ assetsFromGraphDescriptor,
35
+ envFromGraphDescriptor,
36
+ } from "../../../data/file-system.js";
37
+
38
+ export { emptyResult, NodeDescriberManager };
39
+
40
+ function emptyResult(): NodeDescriberResult {
41
+ return {
42
+ inputSchema: { type: "object" },
43
+ outputSchema: { type: "object" },
44
+ };
45
+ }
46
+
47
+ class NodeDescriberManager implements DescribeResultCacheArgs {
48
+ public constructor(public readonly mutable: MutableGraph) {}
49
+
50
+ initial(
51
+ graphId: GraphIdentifier,
52
+ nodeId: NodeIdentifier
53
+ ): NodeDescriberResult {
54
+ const node = this.mutable.nodes.get(nodeId, graphId);
55
+ if (!node) {
56
+ return emptyResult();
57
+ }
58
+ return NodeDescriberManager.asWired(node.incoming(), node.outgoing());
59
+ }
60
+
61
+ async latest(
62
+ graphId: GraphIdentifier,
63
+ nodeId: NodeIdentifier
64
+ ): Promise<NodeDescriberResult> {
65
+ const node = this.mutable.nodes.get(nodeId, graphId);
66
+ if (!node) {
67
+ return emptyResult();
68
+ }
69
+ const result = await this.getLatestDescription(
70
+ node.descriptor.type,
71
+ graphId,
72
+ {
73
+ incoming: node.incoming(),
74
+ outgoing: node.outgoing(),
75
+ inputs: { ...node.configuration() },
76
+ }
77
+ );
78
+ return result;
79
+ }
80
+
81
+ willUpdate(
82
+ previous: NodeDescriberResult,
83
+ current: NodeDescriberResult
84
+ ): void {
85
+ const inputsDiffer = new SchemaDiffer(
86
+ previous.inputSchema,
87
+ current.inputSchema
88
+ );
89
+ inputsDiffer.computeDiff();
90
+
91
+ const outputsDiffer = new SchemaDiffer(
92
+ previous.outputSchema,
93
+ current.outputSchema
94
+ );
95
+ outputsDiffer.computeDiff();
96
+
97
+ if (
98
+ inputsDiffer.same() &&
99
+ outputsDiffer.same() &&
100
+ sameMetadata(previous, current)
101
+ ) {
102
+ return;
103
+ }
104
+ }
105
+
106
+ updated(graphId: GraphIdentifier, nodeId: NodeIdentifier): void {
107
+ this.mutable.store.dispatchEvent(
108
+ new UpdateEvent(this.mutable.id, graphId, nodeId, [], false)
109
+ );
110
+ }
111
+
112
+ async #getDescriber(
113
+ type: NodeTypeIdentifier
114
+ ): Promise<NodeDescriberFunction | undefined> {
115
+ let handler: NodeHandler | undefined;
116
+ try {
117
+ handler = await getHandler(type, contextFromMutableGraph(this.mutable));
118
+ } catch (e) {
119
+ console.warn(`Error getting describer for node type ${type}`, e);
120
+ }
121
+ if (!handler || !("describe" in handler) || !handler.describe) {
122
+ return undefined;
123
+ }
124
+ return handler.describe;
125
+ }
126
+
127
+ async getLatestDescription(
128
+ type: NodeTypeIdentifier,
129
+ graphId: GraphIdentifier,
130
+ options: NodeTypeDescriberOptions = {}
131
+ ) {
132
+ const gettingHandle = GraphDescriptorHandle.create(
133
+ this.mutable.graph,
134
+ graphId
135
+ );
136
+ if (!gettingHandle.success) {
137
+ throw new Error(gettingHandle.error);
138
+ }
139
+ const handle = gettingHandle.result;
140
+ // The schema of an input or an output is defined by their
141
+ // configuration schema or their incoming/outgoing edges.
142
+ if (type === "input") {
143
+ if (handle.main()) {
144
+ if (!this.mutable.store.sandbox) {
145
+ throw new Error(
146
+ "Sandbox not supplied, won't be able to describe this graph correctly"
147
+ );
148
+ }
149
+ const result = await invokeMainDescriber(
150
+ {},
151
+ this.mutable,
152
+ handle.graph(),
153
+ options.inputs!,
154
+ {},
155
+ {}
156
+ );
157
+ if (result)
158
+ return describeInput({
159
+ inputs: {
160
+ schema: result.inputSchema,
161
+ },
162
+ incoming: options?.incoming,
163
+ outgoing: options?.outgoing,
164
+ });
165
+ return describeInput(options);
166
+ }
167
+ return describeInput(options);
168
+ }
169
+ if (type === "output") {
170
+ if (handle.main()) {
171
+ if (!this.mutable.store.sandbox) {
172
+ throw new Error(
173
+ "Sandbox not supplied, won't be able to describe this graph correctly"
174
+ );
175
+ }
176
+ const result = await invokeMainDescriber(
177
+ {},
178
+ this.mutable,
179
+ handle.graph(),
180
+ options.inputs!,
181
+ {},
182
+ {}
183
+ );
184
+ if (result)
185
+ return describeOutput({
186
+ inputs: {
187
+ schema: result.outputSchema,
188
+ },
189
+ incoming: options?.incoming,
190
+ outgoing: options?.outgoing,
191
+ });
192
+ return describeInput(options);
193
+ }
194
+ return describeOutput(options);
195
+ }
196
+
197
+ const kits = [...this.mutable.store.kits];
198
+ const describer = await this.#getDescriber(type);
199
+ const asWired = NodeDescriberManager.asWired(
200
+ options.incoming,
201
+ options.outgoing
202
+ );
203
+ if (!describer) {
204
+ return asWired;
205
+ }
206
+ const loader = this.mutable.store.loader || createLoader();
207
+ const context: NodeDescriberContext = {
208
+ outerGraph: handle.outerGraph(),
209
+ loader,
210
+ kits,
211
+ sandbox: this.mutable.store.sandbox,
212
+ graphStore: this.mutable.store,
213
+ fileSystem: this.mutable.store.fileSystem.createRunFileSystem({
214
+ graphUrl: handle.outerGraph().url!,
215
+ env: envFromGraphDescriptor(
216
+ this.mutable.store.fileSystem.env(),
217
+ handle.outerGraph()
218
+ ),
219
+ assets: assetsFromGraphDescriptor(handle.outerGraph()),
220
+ }),
221
+ flags: this.mutable.store.flags,
222
+ wires: {
223
+ incoming: Object.fromEntries(
224
+ (options?.incoming ?? []).map((edge) => [
225
+ edge.in,
226
+ {
227
+ outputPort: {
228
+ describe: async () => (await edge.outPort()).type.schema,
229
+ },
230
+ },
231
+ ])
232
+ ),
233
+ outgoing: Object.fromEntries(
234
+ (options?.outgoing ?? []).map((edge) => [
235
+ edge.out,
236
+ {
237
+ inputPort: {
238
+ describe: async () => (await edge.inPort()).type.schema,
239
+ },
240
+ },
241
+ ])
242
+ ),
243
+ },
244
+ asType: !!options?.asType,
245
+ };
246
+ if (handle.url()) {
247
+ context.base = handle.url();
248
+ }
249
+ try {
250
+ return describer(
251
+ options?.inputs || undefined,
252
+ asWired.inputSchema,
253
+ asWired.outputSchema,
254
+ context
255
+ );
256
+ } catch (e) {
257
+ console.warn(`Error describing node type ${type}`, e);
258
+ return asWired;
259
+ }
260
+ }
261
+
262
+ static asWired(
263
+ incoming: InspectableEdge[] = [],
264
+ outgoing: InspectableEdge[] = []
265
+ ) {
266
+ return {
267
+ inputSchema: edgesToSchema(EdgeType.In, incoming),
268
+ outputSchema: edgesToSchema(EdgeType.Out, outgoing),
269
+ } satisfies NodeDescriberResult;
270
+ }
271
+ }
272
+
273
+ function sameMetadata(a: NodeDescriberResult, b: NodeDescriberResult) {
274
+ if (a.title !== b.title) return false;
275
+ if (a.description !== b.description) return false;
276
+ if (a.metadata?.icon !== b.metadata?.icon) return false;
277
+ // TODO: Compare tags and help.
278
+ return true;
279
+ }