@ghchinoy/litflow 0.2.7 → 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.
- package/CHANGELOG.md +59 -0
- package/dist/breadboard/data/common.d.ts +35 -0
- package/dist/breadboard/engine/loader/capability.d.ts +21 -0
- package/dist/breadboard/engine/loader/loader.d.ts +29 -0
- package/dist/breadboard/engine/loader/resolve-graph-urls.d.ts +16 -0
- package/dist/breadboard/engine/runtime/bubble.d.ts +23 -0
- package/dist/breadboard/engine/runtime/graph-based-node-handler.d.ts +16 -0
- package/dist/breadboard/engine/runtime/handler.d.ts +27 -0
- package/dist/breadboard/engine/runtime/harness/events.d.ts +145 -0
- package/dist/breadboard/engine/runtime/harness/local.d.ts +10 -0
- package/dist/breadboard/engine/runtime/harness/plan-runner.d.ts +25 -0
- package/dist/breadboard/engine/runtime/run/invoke-graph.d.ts +12 -0
- package/dist/breadboard/engine/runtime/run/node-invoker.d.ts +12 -0
- package/dist/breadboard/engine/runtime/run/run-graph.d.ts +12 -0
- package/dist/breadboard/engine/runtime/run.d.ts +29 -0
- package/dist/breadboard/engine/runtime/sandbox/capabilities-manager.d.ts +15 -0
- package/dist/breadboard/engine/runtime/sandbox/file-system-handler-factory.d.ts +15 -0
- package/dist/breadboard/engine/runtime/sandbox/invoke-describer.d.ts +10 -0
- package/dist/breadboard/engine/runtime/static/create-plan.d.ts +14 -0
- package/dist/breadboard/engine/runtime/static/orchestrator.d.ts +72 -0
- package/dist/breadboard/engine/runtime/traversal/index.d.ts +20 -0
- package/dist/breadboard/engine/runtime/traversal/iterator.d.ts +12 -0
- package/dist/breadboard/engine/runtime/traversal/machine.d.ts +14 -0
- package/dist/breadboard/engine/runtime/traversal/representation.d.ts +27 -0
- package/dist/breadboard/engine/runtime/traversal/result.d.ts +24 -0
- package/dist/breadboard/engine/runtime/traversal/state.d.ts +34 -0
- package/dist/breadboard/lit-flow-runner.d.ts +13 -0
- package/dist/breadboard/lit-flow-runner.test.d.ts +1 -0
- package/dist/breadboard/runner.d.ts +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/lit-chiclet.d.ts +9 -0
- package/dist/lit-schema-node.d.ts +13 -0
- package/dist/lit-schema-node.test.d.ts +1 -0
- package/dist/litflow.js +708 -442
- package/dist/litflow.js.map +1 -1
- package/package.json +18 -4
- package/src/breadboard/data/common.ts +450 -0
- package/src/breadboard/data/file-system.ts +54 -0
- package/src/breadboard/data/inline-all-content.ts +126 -0
- package/src/breadboard/data/recent-boards.ts +118 -0
- package/src/breadboard/data/save-outputs-as-file.ts +104 -0
- package/src/breadboard/engine/add-run-module.ts +168 -0
- package/src/breadboard/engine/editor/blank.ts +65 -0
- package/src/breadboard/engine/editor/edge.ts +27 -0
- package/src/breadboard/engine/editor/events.ts +64 -0
- package/src/breadboard/engine/editor/graph.ts +383 -0
- package/src/breadboard/engine/editor/history.ts +98 -0
- package/src/breadboard/engine/editor/index.ts +8 -0
- package/src/breadboard/engine/editor/operations/add-asset.ts +45 -0
- package/src/breadboard/engine/editor/operations/add-edge.ts +142 -0
- package/src/breadboard/engine/editor/operations/add-graph.ts +47 -0
- package/src/breadboard/engine/editor/operations/add-module.ts +64 -0
- package/src/breadboard/engine/editor/operations/add-node.ts +86 -0
- package/src/breadboard/engine/editor/operations/change-asset-metadata.ts +70 -0
- package/src/breadboard/engine/editor/operations/change-configuration.ts +82 -0
- package/src/breadboard/engine/editor/operations/change-edge-metadata.ts +58 -0
- package/src/breadboard/engine/editor/operations/change-edge.ts +111 -0
- package/src/breadboard/engine/editor/operations/change-graph-metadata.ts +52 -0
- package/src/breadboard/engine/editor/operations/change-metadata.ts +92 -0
- package/src/breadboard/engine/editor/operations/change-module.ts +64 -0
- package/src/breadboard/engine/editor/operations/error.ts +21 -0
- package/src/breadboard/engine/editor/operations/remove-asset.ts +48 -0
- package/src/breadboard/engine/editor/operations/remove-edge.ts +89 -0
- package/src/breadboard/engine/editor/operations/remove-graph.ts +49 -0
- package/src/breadboard/engine/editor/operations/remove-integration.ts +54 -0
- package/src/breadboard/engine/editor/operations/remove-module.ts +69 -0
- package/src/breadboard/engine/editor/operations/remove-node.ts +86 -0
- package/src/breadboard/engine/editor/operations/replace-graph.ts +52 -0
- package/src/breadboard/engine/editor/operations/toggle-export.ts +72 -0
- package/src/breadboard/engine/editor/operations/upsert-integration.ts +43 -0
- package/src/breadboard/engine/editor/selection.ts +58 -0
- package/src/breadboard/engine/editor/transforms/configure-sidewire.ts +73 -0
- package/src/breadboard/engine/editor/transforms/isolate-selection.ts +54 -0
- package/src/breadboard/engine/editor/transforms/merge-graph.ts +58 -0
- package/src/breadboard/engine/editor/transforms/move-to-graph.ts +102 -0
- package/src/breadboard/engine/editor/transforms/move-to-new-graph.ts +72 -0
- package/src/breadboard/engine/editor/transforms/sidewire-to-new-graph.ts +82 -0
- package/src/breadboard/engine/file-system/blob-transform.ts +44 -0
- package/src/breadboard/engine/file-system/composed-peristent-backend.ts +140 -0
- package/src/breadboard/engine/file-system/ephemeral-blob-store.ts +46 -0
- package/src/breadboard/engine/file-system/in-memory-blob-store.ts +87 -0
- package/src/breadboard/engine/file-system/index.ts +723 -0
- package/src/breadboard/engine/file-system/partial-persistent-backend.ts +109 -0
- package/src/breadboard/engine/file-system/path.ts +125 -0
- package/src/breadboard/engine/file-system/persistent-file.ts +66 -0
- package/src/breadboard/engine/file-system/readable-stream-file.ts +61 -0
- package/src/breadboard/engine/file-system/stub-file-system.ts +47 -0
- package/src/breadboard/engine/file-system/utils.ts +40 -0
- package/src/breadboard/engine/inspector/graph/bubbled-node.ts +162 -0
- package/src/breadboard/engine/inspector/graph/describe-cache.ts +78 -0
- package/src/breadboard/engine/inspector/graph/describe-type-cache.ts +48 -0
- package/src/breadboard/engine/inspector/graph/edge-cache.ts +118 -0
- package/src/breadboard/engine/inspector/graph/edge.ts +133 -0
- package/src/breadboard/engine/inspector/graph/event.ts +35 -0
- package/src/breadboard/engine/inspector/graph/exports-describer.ts +45 -0
- package/src/breadboard/engine/inspector/graph/graph-cache.ts +54 -0
- package/src/breadboard/engine/inspector/graph/graph-describer-manager.ts +338 -0
- package/src/breadboard/engine/inspector/graph/graph-descriptor-handle.ts +73 -0
- package/src/breadboard/engine/inspector/graph/graph-node-type.ts +111 -0
- package/src/breadboard/engine/inspector/graph/graph-queries.ts +256 -0
- package/src/breadboard/engine/inspector/graph/graph.ts +163 -0
- package/src/breadboard/engine/inspector/graph/inspectable-asset.ts +36 -0
- package/src/breadboard/engine/inspector/graph/kits.ts +208 -0
- package/src/breadboard/engine/inspector/graph/module.ts +69 -0
- package/src/breadboard/engine/inspector/graph/mutable-graph.ts +150 -0
- package/src/breadboard/engine/inspector/graph/node-cache.ts +123 -0
- package/src/breadboard/engine/inspector/graph/node-describer-manager.ts +279 -0
- package/src/breadboard/engine/inspector/graph/node-type-describer-manager.ts +122 -0
- package/src/breadboard/engine/inspector/graph/node.ts +149 -0
- package/src/breadboard/engine/inspector/graph/port-cache.ts +80 -0
- package/src/breadboard/engine/inspector/graph/ports.ts +292 -0
- package/src/breadboard/engine/inspector/graph/schemas.ts +131 -0
- package/src/breadboard/engine/inspector/graph/virtual-node.ts +184 -0
- package/src/breadboard/engine/inspector/graph-store.ts +629 -0
- package/src/breadboard/engine/inspector/index.ts +13 -0
- package/src/breadboard/engine/inspector/utils.ts +20 -0
- package/src/breadboard/engine/loader/capability.ts +184 -0
- package/src/breadboard/engine/loader/index.ts +14 -0
- package/src/breadboard/engine/loader/loader.ts +244 -0
- package/src/breadboard/engine/loader/resolve-graph-urls.ts +111 -0
- package/src/breadboard/engine/runtime/bubble.ts +269 -0
- package/src/breadboard/engine/runtime/graph-based-node-handler.ts +174 -0
- package/src/breadboard/engine/runtime/handler.ts +166 -0
- package/src/breadboard/engine/runtime/harness/diagnostics.ts +22 -0
- package/src/breadboard/engine/runtime/harness/events.ts +217 -0
- package/src/breadboard/engine/runtime/harness/index.ts +14 -0
- package/src/breadboard/engine/runtime/harness/local.ts +48 -0
- package/src/breadboard/engine/runtime/harness/plan-runner.ts +759 -0
- package/src/breadboard/engine/runtime/index.ts +8 -0
- package/src/breadboard/engine/runtime/legacy.ts +28 -0
- package/src/breadboard/engine/runtime/run/invoke-graph.ts +79 -0
- package/src/breadboard/engine/runtime/run/node-invoker.ts +137 -0
- package/src/breadboard/engine/runtime/run/run-graph.ts +186 -0
- package/src/breadboard/engine/runtime/run.ts +111 -0
- package/src/breadboard/engine/runtime/sandbox/capabilities-manager.ts +253 -0
- package/src/breadboard/engine/runtime/sandbox/file-system-handler-factory.ts +53 -0
- package/src/breadboard/engine/runtime/sandbox/invoke-describer.ts +125 -0
- package/src/breadboard/engine/runtime/static/condense.ts +155 -0
- package/src/breadboard/engine/runtime/static/create-plan.ts +134 -0
- package/src/breadboard/engine/runtime/static/nodes-to-subgraph.ts +168 -0
- package/src/breadboard/engine/runtime/static/orchestrator.ts +664 -0
- package/src/breadboard/engine/runtime/static/types.ts +77 -0
- package/src/breadboard/engine/runtime/traversal/index.ts +58 -0
- package/src/breadboard/engine/runtime/traversal/iterator.ts +124 -0
- package/src/breadboard/engine/runtime/traversal/machine.ts +58 -0
- package/src/breadboard/engine/runtime/traversal/representation.ts +115 -0
- package/src/breadboard/engine/runtime/traversal/result.ts +72 -0
- package/src/breadboard/engine/runtime/traversal/state.ts +115 -0
- package/src/breadboard/engine/telemetry.ts +121 -0
- package/src/breadboard/engine/types.ts +32 -0
- package/src/breadboard/lit-flow-runner.test.ts +44 -0
- package/src/breadboard/lit-flow-runner.ts +98 -0
- package/src/breadboard/runner.ts +80 -0
- package/src/index.ts +2 -0
- package/src/lit-chiclet.ts +69 -0
- package/src/lit-flow.ts +17 -7
- package/src/lit-schema-node.test.ts +65 -0
- package/src/lit-schema-node.ts +194 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2024 Google LLC
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
CapabilitiesManagerImpl,
|
|
10
|
+
invokeDescriber,
|
|
11
|
+
invokeGraph,
|
|
12
|
+
invokeMainDescriber,
|
|
13
|
+
} from "../../runtime/legacy.js";
|
|
14
|
+
import type {
|
|
15
|
+
GraphDescriber,
|
|
16
|
+
GraphIdentifier,
|
|
17
|
+
InputValues,
|
|
18
|
+
InspectableNode,
|
|
19
|
+
ModuleIdentifier,
|
|
20
|
+
MutableGraph,
|
|
21
|
+
NodeDescriberContext,
|
|
22
|
+
NodeDescriberResult,
|
|
23
|
+
NodeTypeIdentifier,
|
|
24
|
+
Outcome,
|
|
25
|
+
Schema,
|
|
26
|
+
} from "@breadboard-ai/types";
|
|
27
|
+
import {
|
|
28
|
+
combineSchemas,
|
|
29
|
+
emptyDescriberResult,
|
|
30
|
+
err,
|
|
31
|
+
filterEmptyValues,
|
|
32
|
+
ok,
|
|
33
|
+
removeProperty,
|
|
34
|
+
} from "@breadboard-ai/utils";
|
|
35
|
+
import { getModuleId, isModule } from "../utils.js";
|
|
36
|
+
import { ExportsDescriber } from "./exports-describer.js";
|
|
37
|
+
import { GraphDescriptorHandle } from "./graph-descriptor-handle.js";
|
|
38
|
+
import { describeInput, describeOutput } from "./schemas.js";
|
|
39
|
+
|
|
40
|
+
export { GraphDescriberManager };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Contains all machinery that allows
|
|
44
|
+
* describing a node or a graph
|
|
45
|
+
*/
|
|
46
|
+
class GraphDescriberManager implements GraphDescriber {
|
|
47
|
+
private readonly exports: ExportsDescriber;
|
|
48
|
+
|
|
49
|
+
private constructor(
|
|
50
|
+
public readonly handle: GraphDescriptorHandle,
|
|
51
|
+
public readonly mutable: MutableGraph
|
|
52
|
+
) {
|
|
53
|
+
this.exports = new ExportsDescriber(mutable, (graphId, mutable) => {
|
|
54
|
+
if (isModule(graphId)) {
|
|
55
|
+
return new ModuleDescriber(mutable, getModuleId(graphId));
|
|
56
|
+
}
|
|
57
|
+
return GraphDescriberManager.create(graphId, mutable);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#nodesByType(type: NodeTypeIdentifier): InspectableNode[] {
|
|
62
|
+
return this.mutable.nodes.byType(type, this.handle.graphId);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async #describeWithStaticAnalysis(): Promise<NodeDescriberResult> {
|
|
66
|
+
const inputSchemas = (
|
|
67
|
+
await Promise.all(
|
|
68
|
+
this.#nodesByType("input")
|
|
69
|
+
.filter((n) => n.isEntry())
|
|
70
|
+
.map((input) =>
|
|
71
|
+
describeInput({
|
|
72
|
+
inputs: input.configuration(),
|
|
73
|
+
incoming: input.incoming(),
|
|
74
|
+
outgoing: input.outgoing(),
|
|
75
|
+
asType: true,
|
|
76
|
+
})
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
).map((result) => result.outputSchema);
|
|
80
|
+
|
|
81
|
+
const outputSchemas = (
|
|
82
|
+
await Promise.all(
|
|
83
|
+
this.#nodesByType("output")
|
|
84
|
+
.filter((n) => n.isExit())
|
|
85
|
+
.map((output) =>
|
|
86
|
+
describeOutput({
|
|
87
|
+
inputs: output.configuration(),
|
|
88
|
+
incoming: output.incoming(),
|
|
89
|
+
outgoing: output.outgoing(),
|
|
90
|
+
asType: true,
|
|
91
|
+
})
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
.map((result) =>
|
|
96
|
+
result.inputSchema.behavior?.includes("bubble")
|
|
97
|
+
? null
|
|
98
|
+
: result.inputSchema
|
|
99
|
+
)
|
|
100
|
+
.filter(Boolean) as Schema[];
|
|
101
|
+
|
|
102
|
+
const inputSchema = combineSchemas(inputSchemas, (result, schema) => {
|
|
103
|
+
if (schema.additionalProperties !== false) {
|
|
104
|
+
result.additionalProperties = true;
|
|
105
|
+
} else if (!("additionalProperties" in result)) {
|
|
106
|
+
result.additionalProperties = false;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
const outputSchema = removeProperty(
|
|
110
|
+
combineSchemas(outputSchemas),
|
|
111
|
+
"schema"
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const adjustedResults = this.#presumeContextInOut({
|
|
115
|
+
inputSchema,
|
|
116
|
+
outputSchema,
|
|
117
|
+
});
|
|
118
|
+
// Do not add export describers when we are in a subgraph.
|
|
119
|
+
if (this.handle.graphId) return adjustedResults;
|
|
120
|
+
|
|
121
|
+
return this.exports.transform(adjustedResults);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* This is a bit hacky, but it gets the job done.
|
|
126
|
+
* This function tweaks the result to look like a context in / context out
|
|
127
|
+
* in cases when the graph being described is a subgraph and the
|
|
128
|
+
* static analysis yielded nothing.
|
|
129
|
+
*
|
|
130
|
+
* Additionally, we scan for parameters in the graph and add them as schema
|
|
131
|
+
* parameters.
|
|
132
|
+
*
|
|
133
|
+
* We use this logic to allow subgraphs that are "custom tools" to
|
|
134
|
+
* participate as normal steps in the graph.
|
|
135
|
+
*/
|
|
136
|
+
#presumeContextInOut(result: NodeDescriberResult): NodeDescriberResult {
|
|
137
|
+
const { inputSchema, outputSchema } = result;
|
|
138
|
+
if (
|
|
139
|
+
!inputSchema.properties &&
|
|
140
|
+
!outputSchema.properties &&
|
|
141
|
+
this.handle.graphId
|
|
142
|
+
) {
|
|
143
|
+
return filterEmptyValues({
|
|
144
|
+
...result,
|
|
145
|
+
inputSchema: {
|
|
146
|
+
properties: {
|
|
147
|
+
context: {
|
|
148
|
+
type: "array",
|
|
149
|
+
items: { type: "object", behavior: ["llm-content"] },
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
outputSchema: {
|
|
154
|
+
properties: {
|
|
155
|
+
context: {
|
|
156
|
+
type: "array",
|
|
157
|
+
items: { type: "object", behavior: ["llm-content"] },
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async #tryDescribingWithCustomDescriber(
|
|
167
|
+
inputs: InputValues,
|
|
168
|
+
context?: NodeDescriberContext
|
|
169
|
+
): Promise<Outcome<NodeDescriberResult>> {
|
|
170
|
+
const customDescriber =
|
|
171
|
+
this.handle.graph().metadata?.describer ||
|
|
172
|
+
(this.handle.graph().main
|
|
173
|
+
? `module:${this.handle.graph().main}`
|
|
174
|
+
: undefined);
|
|
175
|
+
if (!customDescriber) {
|
|
176
|
+
return err("Unable to find custom describer");
|
|
177
|
+
}
|
|
178
|
+
// invoke graph
|
|
179
|
+
try {
|
|
180
|
+
const { sandbox } = this.mutable.store;
|
|
181
|
+
if (sandbox && customDescriber.startsWith("module:")) {
|
|
182
|
+
const { inputSchema, outputSchema } =
|
|
183
|
+
await this.#describeWithStaticAnalysis();
|
|
184
|
+
|
|
185
|
+
const moduleId = customDescriber.slice("module:".length);
|
|
186
|
+
|
|
187
|
+
let result;
|
|
188
|
+
if (this.handle.main() === moduleId) {
|
|
189
|
+
result = await invokeMainDescriber(
|
|
190
|
+
context || {},
|
|
191
|
+
this.mutable,
|
|
192
|
+
this.mutable.graph,
|
|
193
|
+
inputs,
|
|
194
|
+
inputSchema,
|
|
195
|
+
outputSchema,
|
|
196
|
+
new CapabilitiesManagerImpl(context),
|
|
197
|
+
context?.asType || false
|
|
198
|
+
);
|
|
199
|
+
} else {
|
|
200
|
+
result = await invokeDescriber(
|
|
201
|
+
context || {},
|
|
202
|
+
moduleId,
|
|
203
|
+
this.mutable,
|
|
204
|
+
this.mutable.graph,
|
|
205
|
+
inputs,
|
|
206
|
+
inputSchema,
|
|
207
|
+
outputSchema,
|
|
208
|
+
new CapabilitiesManagerImpl(context),
|
|
209
|
+
context?.asType || false
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
if (result) {
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
if (result === false) {
|
|
216
|
+
return err("Custom describer could not provide results.");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const base = this.handle.url();
|
|
220
|
+
|
|
221
|
+
const loader = this.mutable.store.loader;
|
|
222
|
+
|
|
223
|
+
// try loading the describer graph.
|
|
224
|
+
const loadResult = await loader.load(customDescriber, {
|
|
225
|
+
base,
|
|
226
|
+
board: this.handle.graph(),
|
|
227
|
+
outerGraph: this.handle.graph(),
|
|
228
|
+
});
|
|
229
|
+
if (!loadResult.success) {
|
|
230
|
+
const error = `Could not load custom describer graph ${customDescriber}: ${loadResult.error}`;
|
|
231
|
+
console.warn(error);
|
|
232
|
+
return err(error);
|
|
233
|
+
}
|
|
234
|
+
const { inputSchema: $inputSchema, outputSchema: $outputSchema } =
|
|
235
|
+
await this.#describeWithStaticAnalysis();
|
|
236
|
+
// Remove the artifacts of the describer from the input/output schemas.
|
|
237
|
+
// TODO: The right fix here is for static describer to not include
|
|
238
|
+
// describer outputs.
|
|
239
|
+
// delete $outputSchema.properties?.inputSchema;
|
|
240
|
+
// delete $outputSchema.properties?.outputSchema;
|
|
241
|
+
const result = (await invokeGraph(
|
|
242
|
+
loadResult,
|
|
243
|
+
{ ...inputs, $inputSchema, $outputSchema },
|
|
244
|
+
{
|
|
245
|
+
base,
|
|
246
|
+
kits: [...this.mutable.store.kits],
|
|
247
|
+
loader,
|
|
248
|
+
}
|
|
249
|
+
)) as NodeDescriberResult;
|
|
250
|
+
if ("$error" in result) {
|
|
251
|
+
const message = `Error while invoking graph's custom describer`;
|
|
252
|
+
console.warn(message, result.$error);
|
|
253
|
+
return err(`${message}: ${JSON.stringify(result.$error)}`);
|
|
254
|
+
}
|
|
255
|
+
if (!result.inputSchema || !result.outputSchema) {
|
|
256
|
+
const message = `Custom describer did not return input/output schemas`;
|
|
257
|
+
console.warn(message, result);
|
|
258
|
+
return err(`${message}: ${JSON.stringify(result)}`);
|
|
259
|
+
}
|
|
260
|
+
return result;
|
|
261
|
+
} catch (e) {
|
|
262
|
+
const message = `Error while invoking graph's custom describer`;
|
|
263
|
+
console.warn(message, e);
|
|
264
|
+
return err(`${message}: ${JSON.stringify(e)}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async describe(
|
|
269
|
+
inputs?: InputValues,
|
|
270
|
+
_inputSchema?: Schema,
|
|
271
|
+
_outputSchema?: Schema,
|
|
272
|
+
context?: NodeDescriberContext
|
|
273
|
+
): Promise<NodeDescriberResult> {
|
|
274
|
+
const result = await this.#tryDescribingWithCustomDescriber(
|
|
275
|
+
inputs || {},
|
|
276
|
+
context
|
|
277
|
+
);
|
|
278
|
+
if (ok(result)) {
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
const staticResult = await this.#describeWithStaticAnalysis();
|
|
282
|
+
const graph = this.handle.graph();
|
|
283
|
+
const metadata: Omit<NodeDescriberResult, "inputSchema" | "outputSchema"> =
|
|
284
|
+
filterEmptyValues({
|
|
285
|
+
title: graph.title,
|
|
286
|
+
description: graph.description,
|
|
287
|
+
metadata: filterEmptyValues({
|
|
288
|
+
icon: graph.metadata?.icon,
|
|
289
|
+
help: graph.metadata?.help,
|
|
290
|
+
tags: graph.metadata?.tags,
|
|
291
|
+
}),
|
|
292
|
+
});
|
|
293
|
+
return {
|
|
294
|
+
...metadata,
|
|
295
|
+
...staticResult,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static create(
|
|
300
|
+
graphId: GraphIdentifier,
|
|
301
|
+
cache: MutableGraph
|
|
302
|
+
): Outcome<GraphDescriberManager> {
|
|
303
|
+
const handle = GraphDescriptorHandle.create(cache.graph, graphId);
|
|
304
|
+
if (!handle.success) {
|
|
305
|
+
return err(handle.error);
|
|
306
|
+
}
|
|
307
|
+
return new GraphDescriberManager(handle.result, cache);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
class ModuleDescriber implements GraphDescriber {
|
|
312
|
+
constructor(
|
|
313
|
+
public readonly mutable: MutableGraph,
|
|
314
|
+
public readonly moduleId: ModuleIdentifier
|
|
315
|
+
) {}
|
|
316
|
+
|
|
317
|
+
async describe(
|
|
318
|
+
inputs?: InputValues,
|
|
319
|
+
inputSchema?: Schema,
|
|
320
|
+
outputSchema?: Schema,
|
|
321
|
+
context?: NodeDescriberContext
|
|
322
|
+
): Promise<NodeDescriberResult> {
|
|
323
|
+
const result = await invokeDescriber(
|
|
324
|
+
context || {},
|
|
325
|
+
this.moduleId,
|
|
326
|
+
this.mutable,
|
|
327
|
+
this.mutable.graph,
|
|
328
|
+
inputs || {},
|
|
329
|
+
inputSchema,
|
|
330
|
+
outputSchema,
|
|
331
|
+
new CapabilitiesManagerImpl(context),
|
|
332
|
+
context?.asType || false
|
|
333
|
+
);
|
|
334
|
+
if (!result) return emptyDescriberResult();
|
|
335
|
+
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2024 Google LLC
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { isImperativeGraph, toDeclarativeGraph } from "@breadboard-ai/utils";
|
|
9
|
+
import type {
|
|
10
|
+
GraphDescriptor,
|
|
11
|
+
GraphIdentifier,
|
|
12
|
+
ModuleIdentifier,
|
|
13
|
+
Result,
|
|
14
|
+
} from "@breadboard-ai/types";
|
|
15
|
+
|
|
16
|
+
export { GraphDescriptorHandle };
|
|
17
|
+
|
|
18
|
+
class GraphDescriptorHandle {
|
|
19
|
+
#graph: GraphDescriptor;
|
|
20
|
+
#imperativeMain: ModuleIdentifier | undefined;
|
|
21
|
+
#url: URL | undefined;
|
|
22
|
+
|
|
23
|
+
private constructor(
|
|
24
|
+
graph: GraphDescriptor,
|
|
25
|
+
public readonly graphId: GraphIdentifier
|
|
26
|
+
) {
|
|
27
|
+
if (isImperativeGraph(graph)) {
|
|
28
|
+
const { main } = graph;
|
|
29
|
+
graph = toDeclarativeGraph(graph);
|
|
30
|
+
this.#imperativeMain = main;
|
|
31
|
+
}
|
|
32
|
+
this.#graph = graph;
|
|
33
|
+
this.#url = maybeURL(graph.url);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
main() {
|
|
37
|
+
return this.#imperativeMain;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
url() {
|
|
41
|
+
return this.#url;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
outerGraph() {
|
|
45
|
+
return this.#graph;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
graph() {
|
|
49
|
+
return this.graphId ? this.#graph.graphs![this.graphId]! : this.#graph;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static create(
|
|
53
|
+
graph: GraphDescriptor,
|
|
54
|
+
graphId: GraphIdentifier
|
|
55
|
+
): Result<GraphDescriptorHandle> {
|
|
56
|
+
if (graphId && !graph.graphs?.[graphId]) {
|
|
57
|
+
return {
|
|
58
|
+
success: false,
|
|
59
|
+
error: `Unable to create a valid GraphDEscriptorHandle: subgraph "${graphId}" is not in the graph.`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return { success: true, result: new GraphDescriptorHandle(graph, graphId) };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function maybeURL(url?: string): URL | undefined {
|
|
67
|
+
url = url || "";
|
|
68
|
+
try {
|
|
69
|
+
return new URL(url);
|
|
70
|
+
} catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2025 Google LLC
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
describerResultToNodeHandlerMetadata,
|
|
10
|
+
getGraphHandlerFromMutableGraph,
|
|
11
|
+
} from "../../runtime/legacy.js";
|
|
12
|
+
import type {
|
|
13
|
+
InspectableNodePorts,
|
|
14
|
+
InspectableNodeType,
|
|
15
|
+
MutableGraph,
|
|
16
|
+
NodeConfiguration,
|
|
17
|
+
NodeDescriberResult,
|
|
18
|
+
NodeDescriberWires,
|
|
19
|
+
NodeHandler,
|
|
20
|
+
NodeHandlerMetadata,
|
|
21
|
+
NodeHandlerObject,
|
|
22
|
+
} from "@breadboard-ai/types";
|
|
23
|
+
import { portsFromHandler } from "./ports.js";
|
|
24
|
+
|
|
25
|
+
export { GraphNodeType };
|
|
26
|
+
|
|
27
|
+
class GraphNodeType implements InspectableNodeType {
|
|
28
|
+
#type: string;
|
|
29
|
+
#metadata: NodeHandlerMetadata | null = null;
|
|
30
|
+
#handlerPromise: Promise<NodeHandlerObject | undefined> | null = null;
|
|
31
|
+
#mutable: MutableGraph;
|
|
32
|
+
|
|
33
|
+
constructor(type: string, mutable: MutableGraph) {
|
|
34
|
+
this.#type = type;
|
|
35
|
+
this.#mutable = mutable;
|
|
36
|
+
this.#handlerPromise = getGraphHandlerFromMutableGraph(type, mutable);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#extractExamples(
|
|
40
|
+
describeResult: NodeDescriberResult
|
|
41
|
+
): NodeConfiguration | undefined {
|
|
42
|
+
const example = describeResult.inputSchema.examples?.at(0);
|
|
43
|
+
if (!example) return;
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(example) as NodeConfiguration;
|
|
46
|
+
} catch {
|
|
47
|
+
// eat the error.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async #readMetadata() {
|
|
52
|
+
const handler = await this.#handlerPromise;
|
|
53
|
+
const describeResult = await handler?.describe?.(
|
|
54
|
+
undefined,
|
|
55
|
+
undefined,
|
|
56
|
+
undefined,
|
|
57
|
+
{
|
|
58
|
+
graphStore: this.#mutable.store,
|
|
59
|
+
outerGraph: this.#mutable.graph,
|
|
60
|
+
kits: [...this.#mutable.store.kits],
|
|
61
|
+
wires: {} as NodeDescriberWires,
|
|
62
|
+
fileSystem: this.#mutable.store.fileSystem,
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
if (
|
|
66
|
+
describeResult &&
|
|
67
|
+
describeResult.metadata &&
|
|
68
|
+
Object.keys(describeResult.metadata).length > 0
|
|
69
|
+
) {
|
|
70
|
+
const example = this.#extractExamples(describeResult);
|
|
71
|
+
return {
|
|
72
|
+
...describeResult.metadata,
|
|
73
|
+
example,
|
|
74
|
+
title: describeResult.title,
|
|
75
|
+
description: describeResult.description,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (handler && "metadata" in handler && handler.metadata) {
|
|
79
|
+
return handler.metadata;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
title: shortUrlTitle(this.#type),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
currentMetadata(): NodeHandlerMetadata {
|
|
87
|
+
const { current, updating } = this.#mutable.store.types.get(this.#type);
|
|
88
|
+
const result = describerResultToNodeHandlerMetadata(current, updating);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async metadata(): Promise<NodeHandlerMetadata> {
|
|
93
|
+
this.#metadata ??= await this.#readMetadata();
|
|
94
|
+
return this.#metadata;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
type() {
|
|
98
|
+
return this.#type;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async ports(): Promise<InspectableNodePorts> {
|
|
102
|
+
const handler = await this.#handlerPromise;
|
|
103
|
+
return portsFromHandler(this.#type, handler as NodeHandler);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function shortUrlTitle(url: string) {
|
|
108
|
+
const urlObj = new URL(url);
|
|
109
|
+
const path = urlObj.pathname.split("/").pop();
|
|
110
|
+
return path || urlObj.host;
|
|
111
|
+
}
|