@dxos/react-ui-canvas-compute 0.7.5-labs.071a3e2

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 (155) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +1 -0
  3. package/dist/lib/browser/index.mjs +2535 -0
  4. package/dist/lib/browser/index.mjs.map +7 -0
  5. package/dist/lib/browser/meta.json +1 -0
  6. package/dist/lib/node/index.cjs +2627 -0
  7. package/dist/lib/node/index.cjs.map +7 -0
  8. package/dist/lib/node/meta.json +1 -0
  9. package/dist/lib/node-esm/index.mjs +2535 -0
  10. package/dist/lib/node-esm/index.mjs.map +7 -0
  11. package/dist/lib/node-esm/meta.json +1 -0
  12. package/dist/types/src/components/DiagnosticOverlay.d.ts +7 -0
  13. package/dist/types/src/components/DiagnosticOverlay.d.ts.map +1 -0
  14. package/dist/types/src/components/index.d.ts +2 -0
  15. package/dist/types/src/components/index.d.ts.map +1 -0
  16. package/dist/types/src/compute-layout.d.ts +9 -0
  17. package/dist/types/src/compute-layout.d.ts.map +1 -0
  18. package/dist/types/src/compute.stories.d.ts +28 -0
  19. package/dist/types/src/compute.stories.d.ts.map +1 -0
  20. package/dist/types/src/graph/controller.d.ts +142 -0
  21. package/dist/types/src/graph/controller.d.ts.map +1 -0
  22. package/dist/types/src/graph/index.d.ts +3 -0
  23. package/dist/types/src/graph/index.d.ts.map +1 -0
  24. package/dist/types/src/graph/node-defs.d.ts +6 -0
  25. package/dist/types/src/graph/node-defs.d.ts.map +1 -0
  26. package/dist/types/src/hooks/compute-context.d.ts +7 -0
  27. package/dist/types/src/hooks/compute-context.d.ts.map +1 -0
  28. package/dist/types/src/hooks/index.d.ts +4 -0
  29. package/dist/types/src/hooks/index.d.ts.map +1 -0
  30. package/dist/types/src/hooks/useComputeNodeState.d.ts +19 -0
  31. package/dist/types/src/hooks/useComputeNodeState.d.ts.map +1 -0
  32. package/dist/types/src/hooks/useGraphMonitor.d.ts +14 -0
  33. package/dist/types/src/hooks/useGraphMonitor.d.ts.map +1 -0
  34. package/dist/types/src/index.d.ts +6 -0
  35. package/dist/types/src/index.d.ts.map +1 -0
  36. package/dist/types/src/json.test.d.ts +21 -0
  37. package/dist/types/src/json.test.d.ts.map +1 -0
  38. package/dist/types/src/registry.d.ts +9 -0
  39. package/dist/types/src/registry.d.ts.map +1 -0
  40. package/dist/types/src/schema.test.d.ts +2 -0
  41. package/dist/types/src/schema.test.d.ts.map +1 -0
  42. package/dist/types/src/shapes/Append.d.ts +54 -0
  43. package/dist/types/src/shapes/Append.d.ts.map +1 -0
  44. package/dist/types/src/shapes/Array.d.ts +38 -0
  45. package/dist/types/src/shapes/Array.d.ts.map +1 -0
  46. package/dist/types/src/shapes/Audio.d.ts +54 -0
  47. package/dist/types/src/shapes/Audio.d.ts.map +1 -0
  48. package/dist/types/src/shapes/Beacon.d.ts +54 -0
  49. package/dist/types/src/shapes/Beacon.d.ts.map +1 -0
  50. package/dist/types/src/shapes/Boolean.d.ts +233 -0
  51. package/dist/types/src/shapes/Boolean.d.ts.map +1 -0
  52. package/dist/types/src/shapes/Chat.d.ts +57 -0
  53. package/dist/types/src/shapes/Chat.d.ts.map +1 -0
  54. package/dist/types/src/shapes/Constant.d.ts +60 -0
  55. package/dist/types/src/shapes/Constant.d.ts.map +1 -0
  56. package/dist/types/src/shapes/Database.d.ts +54 -0
  57. package/dist/types/src/shapes/Database.d.ts.map +1 -0
  58. package/dist/types/src/shapes/Function.d.ts +54 -0
  59. package/dist/types/src/shapes/Function.d.ts.map +1 -0
  60. package/dist/types/src/shapes/Gpt.d.ts +54 -0
  61. package/dist/types/src/shapes/Gpt.d.ts.map +1 -0
  62. package/dist/types/src/shapes/GptRealtime.d.ts +54 -0
  63. package/dist/types/src/shapes/GptRealtime.d.ts.map +1 -0
  64. package/dist/types/src/shapes/Json.d.ts +107 -0
  65. package/dist/types/src/shapes/Json.d.ts.map +1 -0
  66. package/dist/types/src/shapes/Logic.d.ts +109 -0
  67. package/dist/types/src/shapes/Logic.d.ts.map +1 -0
  68. package/dist/types/src/shapes/Queue.d.ts +58 -0
  69. package/dist/types/src/shapes/Queue.d.ts.map +1 -0
  70. package/dist/types/src/shapes/RNG.d.ts +58 -0
  71. package/dist/types/src/shapes/RNG.d.ts.map +1 -0
  72. package/dist/types/src/shapes/Scope.d.ts +54 -0
  73. package/dist/types/src/shapes/Scope.d.ts.map +1 -0
  74. package/dist/types/src/shapes/Surface.d.ts +54 -0
  75. package/dist/types/src/shapes/Surface.d.ts.map +1 -0
  76. package/dist/types/src/shapes/Switch.d.ts +54 -0
  77. package/dist/types/src/shapes/Switch.d.ts.map +1 -0
  78. package/dist/types/src/shapes/Table.d.ts +54 -0
  79. package/dist/types/src/shapes/Table.d.ts.map +1 -0
  80. package/dist/types/src/shapes/Template.d.ts +56 -0
  81. package/dist/types/src/shapes/Template.d.ts.map +1 -0
  82. package/dist/types/src/shapes/Text.d.ts +54 -0
  83. package/dist/types/src/shapes/Text.d.ts.map +1 -0
  84. package/dist/types/src/shapes/TextToImage.d.ts +54 -0
  85. package/dist/types/src/shapes/TextToImage.d.ts.map +1 -0
  86. package/dist/types/src/shapes/Thread.d.ts +58 -0
  87. package/dist/types/src/shapes/Thread.d.ts.map +1 -0
  88. package/dist/types/src/shapes/Trigger.d.ts +64 -0
  89. package/dist/types/src/shapes/Trigger.d.ts.map +1 -0
  90. package/dist/types/src/shapes/common/Box.d.ts +25 -0
  91. package/dist/types/src/shapes/common/Box.d.ts.map +1 -0
  92. package/dist/types/src/shapes/common/FunctionBody.d.ts +15 -0
  93. package/dist/types/src/shapes/common/FunctionBody.d.ts.map +1 -0
  94. package/dist/types/src/shapes/common/TypeSelect.d.ts +4 -0
  95. package/dist/types/src/shapes/common/TypeSelect.d.ts.map +1 -0
  96. package/dist/types/src/shapes/common/index.d.ts +4 -0
  97. package/dist/types/src/shapes/common/index.d.ts.map +1 -0
  98. package/dist/types/src/shapes/defs.d.ts +39 -0
  99. package/dist/types/src/shapes/defs.d.ts.map +1 -0
  100. package/dist/types/src/shapes/index.d.ts +27 -0
  101. package/dist/types/src/shapes/index.d.ts.map +1 -0
  102. package/dist/types/src/testing/circuits.d.ts +193 -0
  103. package/dist/types/src/testing/circuits.d.ts.map +1 -0
  104. package/dist/types/src/testing/index.d.ts +2 -0
  105. package/dist/types/src/testing/index.d.ts.map +1 -0
  106. package/dist/types/tsconfig.tsbuildinfo +1 -0
  107. package/package.json +85 -0
  108. package/src/README.md +47 -0
  109. package/src/components/DiagnosticOverlay.tsx +23 -0
  110. package/src/components/index.ts +5 -0
  111. package/src/compute-layout.ts +37 -0
  112. package/src/compute.stories.tsx +365 -0
  113. package/src/graph/controller.ts +419 -0
  114. package/src/graph/index.ts +6 -0
  115. package/src/graph/node-defs.ts +82 -0
  116. package/src/hooks/compute-context.ts +19 -0
  117. package/src/hooks/index.ts +7 -0
  118. package/src/hooks/useComputeNodeState.ts +83 -0
  119. package/src/hooks/useGraphMonitor.ts +133 -0
  120. package/src/index.ts +9 -0
  121. package/src/json.test.ts +35 -0
  122. package/src/registry.ts +100 -0
  123. package/src/schema.test.ts +62 -0
  124. package/src/shapes/Append.tsx +43 -0
  125. package/src/shapes/Array.tsx +61 -0
  126. package/src/shapes/Audio.tsx +55 -0
  127. package/src/shapes/Beacon.tsx +56 -0
  128. package/src/shapes/Boolean.tsx +215 -0
  129. package/src/shapes/Chat.tsx +77 -0
  130. package/src/shapes/Constant.tsx +125 -0
  131. package/src/shapes/Database.tsx +39 -0
  132. package/src/shapes/Function.tsx +40 -0
  133. package/src/shapes/Gpt.tsx +91 -0
  134. package/src/shapes/GptRealtime.tsx +175 -0
  135. package/src/shapes/Json.tsx +103 -0
  136. package/src/shapes/Logic.tsx +82 -0
  137. package/src/shapes/Queue.tsx +78 -0
  138. package/src/shapes/RNG.tsx +84 -0
  139. package/src/shapes/Scope.tsx +54 -0
  140. package/src/shapes/Surface.tsx +57 -0
  141. package/src/shapes/Switch.tsx +53 -0
  142. package/src/shapes/Table.tsx +45 -0
  143. package/src/shapes/Template.tsx +98 -0
  144. package/src/shapes/Text.tsx +56 -0
  145. package/src/shapes/TextToImage.tsx +39 -0
  146. package/src/shapes/Thread.tsx +87 -0
  147. package/src/shapes/Trigger.tsx +152 -0
  148. package/src/shapes/common/Box.tsx +74 -0
  149. package/src/shapes/common/FunctionBody.tsx +122 -0
  150. package/src/shapes/common/TypeSelect.tsx +31 -0
  151. package/src/shapes/common/index.ts +7 -0
  152. package/src/shapes/defs.ts +50 -0
  153. package/src/shapes/index.ts +31 -0
  154. package/src/testing/circuits.ts +320 -0
  155. package/src/testing/index.ts +5 -0
@@ -0,0 +1,2535 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+
3
+ // packages/ui/react-ui-canvas-compute/src/graph/controller.ts
4
+ import { Effect, Either, Exit, Layer, Scope } from "effect";
5
+ import { Event, synchronized } from "@dxos/async";
6
+ import { isNotExecuted, makeValueBag, EventLogger, GptService, GraphExecutor, MockGpt, QueueService, SpaceService } from "@dxos/conductor";
7
+ import { Resource } from "@dxos/context";
8
+ import { log } from "@dxos/log";
9
+
10
+ // packages/ui/react-ui-canvas-compute/src/graph/node-defs.ts
11
+ import { NODE_INPUT, NODE_OUTPUT, registry, getTemplateInputSchema } from "@dxos/conductor";
12
+ import { raise } from "@dxos/debug";
13
+ import { ObjectId, toJsonSchema } from "@dxos/echo-schema";
14
+ import { invariant } from "@dxos/invariant";
15
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/graph/node-defs.ts";
16
+ var resolveComputeNode = async (node) => {
17
+ const impl = registry[node.type];
18
+ invariant(impl, `Unknown node type: ${node.type}`, {
19
+ F: __dxlog_file,
20
+ L: 22,
21
+ S: void 0,
22
+ A: [
23
+ "impl",
24
+ "`Unknown node type: ${node.type}`"
25
+ ]
26
+ });
27
+ return impl;
28
+ };
29
+ var isValidComputeNode = (type) => {
30
+ return nodeFactory[type] !== void 0;
31
+ };
32
+ var createComputeNode = (shape) => {
33
+ const type = shape.type ?? raise(new Error("Type not specified"));
34
+ const factory = nodeFactory[type] ?? raise(new Error(`Unknown shape type: ${type}`));
35
+ return factory(shape);
36
+ };
37
+ var nodeFactory = {
38
+ // System.
39
+ [NODE_INPUT]: () => createNode(NODE_INPUT),
40
+ [NODE_OUTPUT]: () => createNode(NODE_OUTPUT),
41
+ // Extensions.
42
+ ["text-to-image"]: () => createNode("text-to-image"),
43
+ ["and"]: () => createNode("and"),
44
+ ["append"]: () => createNode("append"),
45
+ ["audio"]: () => createNode("audio"),
46
+ ["beacon"]: () => createNode("beacon"),
47
+ ["chat"]: () => createNode("chat"),
48
+ ["constant"]: (shape) => createNode("constant", {
49
+ value: shape.value
50
+ }),
51
+ ["database"]: () => createNode("database"),
52
+ ["gpt"]: () => createNode("gpt"),
53
+ ["gpt-realtime"]: () => createNode("gpt-realtime"),
54
+ ["if"]: () => createNode("if"),
55
+ ["if-else"]: () => createNode("if-else"),
56
+ ["function"]: () => createNode("function"),
57
+ ["json"]: () => createNode("json"),
58
+ ["json-transform"]: () => createNode("json-transform"),
59
+ ["not"]: () => createNode("not"),
60
+ ["or"]: () => createNode("or"),
61
+ ["queue"]: () => createNode("queue"),
62
+ ["rng"]: () => createNode("rng"),
63
+ ["reducer"]: () => createNode("reducer"),
64
+ ["scope"]: () => createNode("scope"),
65
+ ["surface"]: () => createNode("surface"),
66
+ ["switch"]: () => createNode("switch"),
67
+ ["template"]: (shape) => {
68
+ const node = createNode("template", {
69
+ valueType: shape.valueType,
70
+ value: shape.text
71
+ });
72
+ node.inputSchema = toJsonSchema(getTemplateInputSchema(node));
73
+ return node;
74
+ },
75
+ ["text"]: () => createNode("text"),
76
+ ["thread"]: () => createNode("thread"),
77
+ ["trigger"]: () => createNode(NODE_INPUT)
78
+ };
79
+ var createNode = (type, props) => ({
80
+ id: ObjectId.random(),
81
+ type,
82
+ ...props
83
+ });
84
+
85
+ // packages/ui/react-ui-canvas-compute/src/hooks/compute-context.ts
86
+ import { createContext, useContext } from "react";
87
+ import { raise as raise2 } from "@dxos/debug";
88
+ var ComputeContext = createContext(null);
89
+ var useComputeContext = () => {
90
+ return useContext(ComputeContext) ?? raise2(new Error("Missing ComputeContext"));
91
+ };
92
+
93
+ // packages/ui/react-ui-canvas-compute/src/hooks/useComputeNodeState.ts
94
+ import { useCallback, useEffect, useState } from "react";
95
+ import { S } from "@dxos/echo-schema";
96
+ import { invariant as invariant2 } from "@dxos/invariant";
97
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/hooks/useComputeNodeState.ts";
98
+ var useComputeNodeState = (shape) => {
99
+ const { controller } = useComputeContext();
100
+ invariant2(controller, void 0, {
101
+ F: __dxlog_file2,
102
+ L: 32,
103
+ S: void 0,
104
+ A: [
105
+ "controller",
106
+ ""
107
+ ]
108
+ });
109
+ const [meta, setMeta] = useState();
110
+ useEffect(() => {
111
+ let disposed = false;
112
+ queueMicrotask(async () => {
113
+ invariant2(shape.node, "Node not specified", {
114
+ F: __dxlog_file2,
115
+ L: 38,
116
+ S: void 0,
117
+ A: [
118
+ "shape.node",
119
+ "'Node not specified'"
120
+ ]
121
+ });
122
+ const node = controller.getComputeNode(shape.node);
123
+ const meta2 = await controller.getMeta(node);
124
+ if (disposed) {
125
+ return;
126
+ }
127
+ setMeta(meta2);
128
+ });
129
+ return () => {
130
+ disposed = true;
131
+ };
132
+ }, [
133
+ shape.node
134
+ ]);
135
+ const evalNode = useCallback(() => {
136
+ return controller.evalNode(shape.node);
137
+ }, [
138
+ shape.node
139
+ ]);
140
+ const subscribeToEventLog = useCallback((cb) => {
141
+ return controller.events.on((ev) => {
142
+ if (ev.nodeId === shape.node) {
143
+ cb(ev);
144
+ }
145
+ });
146
+ }, [
147
+ shape.node
148
+ ]);
149
+ return {
150
+ node: controller.getComputeNode(shape.node),
151
+ meta: meta ?? {
152
+ input: S.Struct({}),
153
+ output: S.Struct({})
154
+ },
155
+ runtime: {
156
+ inputs: controller.getInputs(shape.node),
157
+ outputs: controller.getOutputs(shape.node),
158
+ setOutput: (property, value) => {
159
+ controller.setOutput(shape.node, property, value);
160
+ },
161
+ evalNode,
162
+ subscribeToEventLog
163
+ }
164
+ };
165
+ };
166
+
167
+ // packages/ui/react-ui-canvas-compute/src/hooks/useGraphMonitor.ts
168
+ import { useMemo } from "react";
169
+ import { ComputeGraphModel, DEFAULT_INPUT, DEFAULT_OUTPUT } from "@dxos/conductor";
170
+ import { ObjectId as ObjectId2 } from "@dxos/echo-schema";
171
+ import { invariant as invariant3 } from "@dxos/invariant";
172
+ import { DXN } from "@dxos/keys";
173
+ import { getSpace } from "@dxos/react-client/echo";
174
+ import { nonNullable } from "@dxos/util";
175
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/hooks/useGraphMonitor.ts";
176
+ var mapEdge = (graph, { source, target, output = DEFAULT_OUTPUT, input = DEFAULT_INPUT }) => {
177
+ const sourceNode = graph.findNode(source);
178
+ const targetNode = graph.findNode(target);
179
+ invariant3(sourceNode?.node, void 0, {
180
+ F: __dxlog_file3,
181
+ L: 27,
182
+ S: void 0,
183
+ A: [
184
+ "sourceNode?.node",
185
+ ""
186
+ ]
187
+ });
188
+ invariant3(targetNode?.node, void 0, {
189
+ F: __dxlog_file3,
190
+ L: 28,
191
+ S: void 0,
192
+ A: [
193
+ "targetNode?.node",
194
+ ""
195
+ ]
196
+ });
197
+ return {
198
+ id: ObjectId2.random(),
199
+ source: sourceNode.node,
200
+ target: targetNode.node,
201
+ output,
202
+ input
203
+ };
204
+ };
205
+ var useGraphMonitor = (model) => {
206
+ return useMemo(() => {
207
+ return {
208
+ onCreate: ({ node }) => {
209
+ if (!model) {
210
+ return;
211
+ }
212
+ invariant3(node.type, void 0, {
213
+ F: __dxlog_file3,
214
+ L: 53,
215
+ S: void 0,
216
+ A: [
217
+ "node.type",
218
+ ""
219
+ ]
220
+ });
221
+ if (!isValidComputeNode(node.type)) {
222
+ return;
223
+ }
224
+ const computeNode = createComputeNode(node);
225
+ if (node.type === "trigger") {
226
+ linkTriggerToCompute(model, computeNode, node);
227
+ }
228
+ model.addNode(computeNode);
229
+ node.node = computeNode.id;
230
+ },
231
+ onLink: ({ graph, edge }) => {
232
+ if (model) {
233
+ model.addEdge(mapEdge(graph, edge));
234
+ }
235
+ },
236
+ onDelete: ({ subgraph }) => {
237
+ if (model) {
238
+ const nodeIds = subgraph.nodes.map((shape) => shape.node);
239
+ const edgeIds = subgraph.edges.map(({ source, target, output = DEFAULT_OUTPUT, input = DEFAULT_INPUT }) => {
240
+ return model.edges.find((computeEdge) => computeEdge.input === input && computeEdge.output === output)?.id;
241
+ }).filter(nonNullable);
242
+ model.removeNodes(nodeIds);
243
+ model.removeEdges(edgeIds);
244
+ deleteTriggerObjects(model, subgraph);
245
+ }
246
+ }
247
+ };
248
+ }, [
249
+ model
250
+ ]);
251
+ };
252
+ var createComputeGraph = (graph) => {
253
+ const computeGraph = ComputeGraphModel.create();
254
+ if (graph) {
255
+ for (const shape of graph.nodes) {
256
+ if (isValidComputeNode(shape.type)) {
257
+ const node = createComputeNode(shape);
258
+ computeGraph.addNode(node);
259
+ shape.node = node.id;
260
+ }
261
+ }
262
+ for (const edge of graph.edges) {
263
+ computeGraph.addEdge(mapEdge(graph, edge));
264
+ }
265
+ }
266
+ return computeGraph;
267
+ };
268
+ var linkTriggerToCompute = (graph, computeNode, triggerData) => {
269
+ const functionTrigger = triggerData.functionTrigger?.target;
270
+ invariant3(functionTrigger, void 0, {
271
+ F: __dxlog_file3,
272
+ L: 116,
273
+ S: void 0,
274
+ A: [
275
+ "functionTrigger",
276
+ ""
277
+ ]
278
+ });
279
+ functionTrigger.function = DXN.fromLocalObjectId(graph.root.id).toString();
280
+ functionTrigger.meta ??= {};
281
+ functionTrigger.meta.computeNodeId = computeNode.id;
282
+ };
283
+ var deleteTriggerObjects = (computeGraph, deleted) => {
284
+ const space = getSpace(computeGraph.root);
285
+ if (!space) {
286
+ return;
287
+ }
288
+ for (const node of deleted.nodes) {
289
+ if (node.type === "trigger") {
290
+ const trigger = node;
291
+ space.db.remove(trigger.functionTrigger.target);
292
+ }
293
+ }
294
+ };
295
+
296
+ // packages/ui/react-ui-canvas-compute/src/graph/controller.ts
297
+ function _ts_decorate(decorators, target, key, desc) {
298
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
299
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
300
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
301
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
302
+ }
303
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/graph/controller.ts";
304
+ var InvalidStateError = Error;
305
+ var AUTO_TRIGGER_NODES = [
306
+ "chat",
307
+ "switch",
308
+ "constant"
309
+ ];
310
+ var createComputeGraphController = (graph, services) => {
311
+ const computeGraph = createComputeGraph(graph);
312
+ const controller = new ComputeGraphController(computeGraph);
313
+ controller.setServices(services ?? {});
314
+ return {
315
+ controller,
316
+ graph
317
+ };
318
+ };
319
+ var ComputeGraphController = class extends Resource {
320
+ constructor(_graph) {
321
+ super();
322
+ this._graph = _graph;
323
+ this._executor = new GraphExecutor({
324
+ computeNodeResolver: (node) => resolveComputeNode(node)
325
+ });
326
+ this._diagnostics = [];
327
+ this._services = {};
328
+ this._forcedOutputs = {};
329
+ this._runtimeStateInputs = {};
330
+ this._runtimeStateOutputs = {};
331
+ this.update = new Event();
332
+ this.output = new Event();
333
+ this.events = new Event();
334
+ }
335
+ toJSON() {
336
+ return {
337
+ graph: this._graph,
338
+ state: {
339
+ inputs: this._runtimeStateInputs,
340
+ outputs: this._runtimeStateOutputs
341
+ },
342
+ forcedOutputs: this._forcedOutputs
343
+ };
344
+ }
345
+ setServices(services) {
346
+ Object.assign(this._services, services);
347
+ }
348
+ get graph() {
349
+ return this._graph;
350
+ }
351
+ get diagnostics() {
352
+ return this._diagnostics;
353
+ }
354
+ get userState() {
355
+ return this._forcedOutputs;
356
+ }
357
+ get inputStates() {
358
+ return this._runtimeStateInputs;
359
+ }
360
+ get outputStates() {
361
+ return this._runtimeStateOutputs;
362
+ }
363
+ /**
364
+ * Inputs and outputs for all nodes.
365
+ */
366
+ get state() {
367
+ const ids = [
368
+ .../* @__PURE__ */ new Set([
369
+ ...Object.keys(this._runtimeStateInputs),
370
+ ...Object.keys(this._runtimeStateOutputs)
371
+ ])
372
+ ];
373
+ return Object.fromEntries(ids.map((id) => [
374
+ id,
375
+ {
376
+ node: this._graph.getNode(id),
377
+ input: this._runtimeStateInputs[id],
378
+ output: this._runtimeStateOutputs[id]
379
+ }
380
+ ]));
381
+ }
382
+ addNode(node) {
383
+ this._graph.addNode(node);
384
+ }
385
+ addEdge(edge) {
386
+ this._graph.addEdge(edge);
387
+ }
388
+ getComputeNode(nodeId) {
389
+ return this._graph.getNode(nodeId);
390
+ }
391
+ getInputs(nodeId) {
392
+ return this._runtimeStateInputs[nodeId] ?? {};
393
+ }
394
+ getOutputs(nodeId) {
395
+ return this._runtimeStateOutputs[nodeId] ?? {};
396
+ }
397
+ setOutput(nodeId, property, value) {
398
+ this._forcedOutputs[nodeId] ??= {};
399
+ this._forcedOutputs[nodeId][property] = value;
400
+ queueMicrotask(async () => {
401
+ try {
402
+ await this.exec(nodeId);
403
+ } catch (err) {
404
+ log.catch(err, void 0, {
405
+ F: __dxlog_file4,
406
+ L: 215,
407
+ S: this,
408
+ C: (f, a) => f(...a)
409
+ });
410
+ }
411
+ });
412
+ }
413
+ async getMeta(node) {
414
+ const { meta } = await resolveComputeNode(node);
415
+ return meta;
416
+ }
417
+ async checkGraph() {
418
+ const executor = this._executor.clone();
419
+ await executor.load(this._graph);
420
+ this._diagnostics = executor.getDiagnostics();
421
+ }
422
+ async evalNode(nodeId) {
423
+ const executor = this._executor.clone();
424
+ await executor.load(this._graph);
425
+ for (const [nodeId2, outputs] of Object.entries(this._forcedOutputs)) {
426
+ executor.setOutputs(nodeId2, Effect.succeed(makeValueBag(outputs)));
427
+ }
428
+ const services = this._createServiceLayer();
429
+ await Effect.runPromise(Effect.gen(this, function* () {
430
+ const scope = yield* Scope.make();
431
+ const executable = yield* Effect.promise(() => resolveComputeNode(this._graph.getNode(nodeId)));
432
+ const computingOutputs = executable.exec != null;
433
+ const effect = (computingOutputs ? executor.computeOutputs(nodeId) : executor.computeInputs(nodeId)).pipe(Effect.withSpan("runGraph"), Effect.provide(services), Scope.extend(scope), Effect.flatMap(computeValueBag), Effect.withSpan("test"), Effect.tap((values) => {
434
+ for (const [key, value] of Object.entries(values)) {
435
+ if (computingOutputs) {
436
+ this._onOutputComputed(nodeId, key, value);
437
+ } else {
438
+ this._onInputComputed(nodeId, key, value);
439
+ }
440
+ }
441
+ }));
442
+ yield* effect;
443
+ yield* Scope.close(scope, Exit.void);
444
+ }));
445
+ this.update.emit();
446
+ }
447
+ /**
448
+ * Executes the graph.
449
+ * @param startFromNode - Node to start from, otherwise all {@link AUTO_TRIGGER_NODES} are executed.
450
+ */
451
+ async exec(startFromNode) {
452
+ this._runtimeStateInputs = {};
453
+ this._runtimeStateOutputs = {};
454
+ const executor = this._executor.clone();
455
+ await executor.load(this._graph);
456
+ for (const [nodeId, outputs] of Object.entries(this._forcedOutputs)) {
457
+ executor.setOutputs(nodeId, Effect.succeed(makeValueBag(outputs)));
458
+ }
459
+ const triggerNodes = startFromNode != null ? [
460
+ this._graph.getNode(startFromNode)
461
+ ] : this._graph.nodes.filter((node) => node.type != null && AUTO_TRIGGER_NODES.includes(node.type));
462
+ const allAffectedNodes = [
463
+ ...new Set(triggerNodes.flatMap((node) => executor.getAllDependantNodes(node.id)))
464
+ ];
465
+ const services = this._createServiceLayer();
466
+ await Effect.runPromise(Effect.gen(this, function* () {
467
+ const scope = yield* Scope.make();
468
+ const tasks = [];
469
+ for (const node of allAffectedNodes) {
470
+ const executable = yield* Effect.promise(() => resolveComputeNode(this._graph.getNode(node)));
471
+ const computingOutputs = executable.exec != null;
472
+ const effect = (computingOutputs ? executor.computeOutputs(node) : executor.computeInputs(node)).pipe(Effect.withSpan("runGraph"), Effect.provide(services), Scope.extend(scope), Effect.flatMap(computeValueBag), Effect.withSpan("test"), Effect.tap((values) => {
473
+ for (const [key, value] of Object.entries(values)) {
474
+ if (computingOutputs) {
475
+ this._onOutputComputed(node, key, value);
476
+ } else {
477
+ this._onInputComputed(node, key, value);
478
+ }
479
+ }
480
+ }));
481
+ tasks.push(effect);
482
+ }
483
+ yield* Effect.all(tasks);
484
+ yield* Scope.close(scope, Exit.void);
485
+ }));
486
+ this.update.emit();
487
+ }
488
+ _createServiceLayer() {
489
+ const services = {
490
+ ...DEFAULT_SERVICES,
491
+ ...this._services
492
+ };
493
+ const logLayer = Layer.succeed(EventLogger, this._createLogger());
494
+ const gptLayer = Layer.succeed(GptService, services.gpt);
495
+ const queueLayer = services.edgeHttpClient != null ? QueueService.fromClient(services.edgeHttpClient) : QueueService.notAvailable;
496
+ const spaceLayer = services.spaceService != null ? Layer.succeed(SpaceService, services.spaceService) : SpaceService.empty;
497
+ return Layer.mergeAll(logLayer, gptLayer, queueLayer, spaceLayer);
498
+ }
499
+ _createLogger() {
500
+ return {
501
+ log: (event) => {
502
+ this._handleEvent(event);
503
+ },
504
+ nodeId: void 0
505
+ };
506
+ }
507
+ _handleEvent(event) {
508
+ log("handleEvent", {
509
+ event
510
+ }, {
511
+ F: __dxlog_file4,
512
+ L: 365,
513
+ S: this,
514
+ C: (f, a) => f(...a)
515
+ });
516
+ switch (event.type) {
517
+ case "compute-input": {
518
+ this._onInputComputed(event.nodeId, event.property, {
519
+ type: "executed",
520
+ value: event.value
521
+ });
522
+ break;
523
+ }
524
+ case "compute-output": {
525
+ this._onOutputComputed(event.nodeId, event.property, {
526
+ type: "executed",
527
+ value: event.value
528
+ });
529
+ break;
530
+ }
531
+ }
532
+ this.events.emit(event);
533
+ }
534
+ _onInputComputed(nodeId, property, value) {
535
+ this._runtimeStateInputs[nodeId] ??= {};
536
+ this._runtimeStateInputs[nodeId][property] = value;
537
+ }
538
+ _onOutputComputed(nodeId, property, value) {
539
+ this._runtimeStateOutputs[nodeId] ??= {};
540
+ this._runtimeStateOutputs[nodeId][property] = value;
541
+ this.output.emit({
542
+ nodeId,
543
+ property,
544
+ value
545
+ });
546
+ }
547
+ };
548
+ _ts_decorate([
549
+ synchronized
550
+ ], ComputeGraphController.prototype, "checkGraph", null);
551
+ _ts_decorate([
552
+ synchronized
553
+ ], ComputeGraphController.prototype, "exec", null);
554
+ var DEFAULT_SERVICES = {
555
+ gpt: new MockGpt()
556
+ };
557
+ var computeValueBag = (bag) => {
558
+ return Effect.all(Object.entries(bag.values).map(([key, eff]) => Effect.either(eff).pipe(Effect.map((value) => {
559
+ if (Either.isLeft(value)) {
560
+ if (isNotExecuted(value.left)) {
561
+ return [
562
+ key,
563
+ {
564
+ type: "not-executed"
565
+ }
566
+ ];
567
+ } else {
568
+ return [
569
+ key,
570
+ {
571
+ type: "error",
572
+ error: value.left
573
+ }
574
+ ];
575
+ }
576
+ } else {
577
+ return [
578
+ key,
579
+ {
580
+ type: "executed",
581
+ value: value.right
582
+ }
583
+ ];
584
+ }
585
+ })))).pipe(Effect.map((entries) => Object.fromEntries(entries)));
586
+ };
587
+
588
+ // packages/ui/react-ui-canvas-compute/src/registry.ts
589
+ import { noteShape } from "@dxos/react-ui-canvas-editor";
590
+
591
+ // packages/ui/react-ui-canvas-compute/src/shapes/common/Box.tsx
592
+ import React, { forwardRef } from "react";
593
+ import { invariant as invariant4 } from "@dxos/invariant";
594
+ import { Icon, IconButton } from "@dxos/react-ui";
595
+ import { useEditorContext, useShapeDef } from "@dxos/react-ui-canvas-editor";
596
+ import { mx } from "@dxos/react-ui-theme";
597
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/shapes/common/Box.tsx";
598
+ var headerHeight = 32;
599
+ var footerHeight = 32;
600
+ var Box = /* @__PURE__ */ forwardRef(({ children, classNames, shape, title, status, open, onAction }, forwardedRef) => {
601
+ invariant4(shape.type, void 0, {
602
+ F: __dxlog_file5,
603
+ L: 30,
604
+ S: void 0,
605
+ A: [
606
+ "shape.type",
607
+ ""
608
+ ]
609
+ });
610
+ const { icon, name, openable } = useShapeDef(shape.type) ?? {
611
+ icon: "ph--placeholder--regular"
612
+ };
613
+ const { debug } = useEditorContext();
614
+ return /* @__PURE__ */ React.createElement("div", {
615
+ ref: forwardedRef,
616
+ className: "flex flex-col h-full w-full justify-between"
617
+ }, /* @__PURE__ */ React.createElement("div", {
618
+ className: "flex shrink-0 w-full justify-between items-center h-[32px] bg-hoverSurface"
619
+ }, /* @__PURE__ */ React.createElement(Icon, {
620
+ icon,
621
+ classNames: "mx-2"
622
+ }), /* @__PURE__ */ React.createElement("div", {
623
+ className: "grow text-sm truncate"
624
+ }, debug ? shape.type : name ?? shape.text ?? title), /* @__PURE__ */ React.createElement(IconButton, {
625
+ classNames: "p-1 text-green-500",
626
+ variant: "ghost",
627
+ icon: "ph--play--regular",
628
+ size: 4,
629
+ label: "run",
630
+ iconOnly: true,
631
+ onDoubleClick: (ev) => ev.stopPropagation(),
632
+ onClick: (ev) => {
633
+ ev.stopPropagation();
634
+ onAction?.("run");
635
+ }
636
+ })), /* @__PURE__ */ React.createElement("div", {
637
+ className: mx("flex flex-col h-full grow overflow-hidden", classNames)
638
+ }, children), /* @__PURE__ */ React.createElement("div", {
639
+ className: "flex shrink-0 w-full justify-between items-center h-[32px] bg-hoverSurface"
640
+ }, /* @__PURE__ */ React.createElement("div", {
641
+ className: "grow px-2 text-sm truncate"
642
+ }, debug ? shape.id : status), openable && /* @__PURE__ */ React.createElement(IconButton, {
643
+ classNames: "p-1",
644
+ variant: "ghost",
645
+ icon: open ? "ph--caret-up--regular" : "ph--caret-down--regular",
646
+ size: 4,
647
+ label: open ? "close" : "open",
648
+ iconOnly: true,
649
+ onClick: (ev) => {
650
+ ev.stopPropagation();
651
+ onAction?.(open ? "close" : "open");
652
+ }
653
+ })));
654
+ });
655
+
656
+ // packages/ui/react-ui-canvas-compute/src/shapes/common/FunctionBody.tsx
657
+ import React2, { useRef, useState as useState2 } from "react";
658
+ import { VoidInput, VoidOutput } from "@dxos/conductor";
659
+ import { AST as AST2 } from "@dxos/echo-schema";
660
+ import { useCanvasContext } from "@dxos/react-ui-canvas";
661
+ import { getParentShapeElement, createAnchors, rowHeight } from "@dxos/react-ui-canvas-editor";
662
+
663
+ // packages/ui/react-ui-canvas-compute/src/shapes/defs.ts
664
+ import { AST } from "@effect/schema";
665
+ import { DEFAULT_INPUT as DEFAULT_INPUT2, DEFAULT_OUTPUT as DEFAULT_OUTPUT2 } from "@dxos/conductor";
666
+ import { ObjectId as ObjectId3, S as S2 } from "@dxos/echo-schema";
667
+ import { Polygon } from "@dxos/react-ui-canvas-editor";
668
+ var getProperties = (ast) => AST.getPropertySignatures(ast).map(({ name }) => ({
669
+ name: name.toString()
670
+ }));
671
+ var createAnchorId = (kind, property = kind === "input" ? DEFAULT_INPUT2 : DEFAULT_OUTPUT2) => [
672
+ kind,
673
+ property
674
+ ].join(".");
675
+ var parseAnchorId = (id) => {
676
+ const parts = id.match(/(input|output)\.(.+)/);
677
+ return parts ? parts.slice(1) : [
678
+ void 0,
679
+ id
680
+ ];
681
+ };
682
+ var ComputeShape = S2.extend(Polygon, S2.Struct({
683
+ // TODO(burdon): Rename computeNode?
684
+ node: S2.optional(ObjectId3.annotations({
685
+ [AST.DescriptionAnnotationId]: "Compute node id"
686
+ }))
687
+ }).pipe(S2.mutable));
688
+ var createShape = ({ id, ...rest }) => {
689
+ return {
690
+ id: id ?? ObjectId3.random(),
691
+ ...rest
692
+ };
693
+ };
694
+
695
+ // packages/ui/react-ui-canvas-compute/src/shapes/common/FunctionBody.tsx
696
+ var bodyPadding = 8;
697
+ var expandedHeight = 200;
698
+ var FunctionBody = ({ shape, name, content, inputSchema = VoidInput, outputSchema = VoidOutput, ...props }) => {
699
+ const { scale } = useCanvasContext();
700
+ const rootRef = useRef(null);
701
+ const [open, setOpen] = useState2(false);
702
+ const handleAction = (action) => {
703
+ if (!rootRef.current) {
704
+ return;
705
+ }
706
+ switch (action) {
707
+ case "open": {
708
+ const el = getParentShapeElement(rootRef.current, shape.id);
709
+ const { height } = el.getBoundingClientRect();
710
+ el.style.height = `${height / scale + expandedHeight}px`;
711
+ setOpen(true);
712
+ break;
713
+ }
714
+ case "close": {
715
+ const el = getParentShapeElement(rootRef.current, shape.id);
716
+ el.style.height = "";
717
+ setOpen(false);
718
+ break;
719
+ }
720
+ }
721
+ };
722
+ const inputs = getProperties(inputSchema.ast);
723
+ const outputs = getProperties(outputSchema.ast);
724
+ const columnCount = inputs.length && outputs.length ? 2 : 1;
725
+ return /* @__PURE__ */ React2.createElement(Box, {
726
+ ref: rootRef,
727
+ shape,
728
+ title: name,
729
+ classNames: "divide-y divide-separator",
730
+ open,
731
+ onAction: handleAction,
732
+ ...props
733
+ }, /* @__PURE__ */ React2.createElement("div", {
734
+ className: `grid grid-cols-${columnCount} items-center`,
735
+ style: {
736
+ paddingTop: bodyPadding,
737
+ paddingBottom: bodyPadding
738
+ }
739
+ }, (inputs?.length ?? 0) > 0 && /* @__PURE__ */ React2.createElement("div", {
740
+ className: "flex flex-col"
741
+ }, inputs?.map(({ name: name2 }) => /* @__PURE__ */ React2.createElement("div", {
742
+ key: name2,
743
+ className: "px-2 truncate text-sm font-mono items-center",
744
+ style: {
745
+ height: rowHeight
746
+ }
747
+ }, name2))), (outputs?.length ?? 0) > 0 && /* @__PURE__ */ React2.createElement("div", {
748
+ className: "flex flex-col"
749
+ }, outputs?.map(({ name: name2 }) => /* @__PURE__ */ React2.createElement("div", {
750
+ key: name2,
751
+ className: "px-2 truncate text-sm font-mono items-center text-right",
752
+ style: {
753
+ height: rowHeight
754
+ }
755
+ }, name2)))), open && /* @__PURE__ */ React2.createElement("div", {
756
+ className: "flex flex-col grow overflow-hidden"
757
+ }, content));
758
+ };
759
+ var getHeight = (input) => {
760
+ const properties = AST2.getPropertySignatures(input.ast);
761
+ return headerHeight + footerHeight + bodyPadding * 2 + properties.length * rowHeight + 2;
762
+ };
763
+ var createFunctionAnchors = (shape, input = VoidInput, output = VoidOutput) => {
764
+ const inputs = AST2.getPropertySignatures(input.ast).map(({ name }) => createAnchorId("input", name.toString()));
765
+ const outputs = AST2.getPropertySignatures(output.ast).map(({ name }) => createAnchorId("output", name.toString()));
766
+ return createAnchors({
767
+ shape,
768
+ inputs,
769
+ outputs,
770
+ center: {
771
+ x: 0,
772
+ y: (headerHeight - footerHeight) / 2 + 1
773
+ }
774
+ });
775
+ };
776
+
777
+ // packages/ui/react-ui-canvas-compute/src/shapes/common/TypeSelect.tsx
778
+ import React3 from "react";
779
+ import { ComputeValueType } from "@dxos/conductor";
780
+ import { Select } from "@dxos/react-ui";
781
+ var TypeSelect = ({ value, onValueChange }) => {
782
+ return /* @__PURE__ */ React3.createElement(Select.Root, {
783
+ value,
784
+ onValueChange
785
+ }, /* @__PURE__ */ React3.createElement(Select.TriggerButton, {
786
+ variant: "ghost",
787
+ classNames: "w-full !px-0"
788
+ }), /* @__PURE__ */ React3.createElement(Select.Portal, null, /* @__PURE__ */ React3.createElement(Select.Content, null, /* @__PURE__ */ React3.createElement(Select.ScrollUpButton, null), /* @__PURE__ */ React3.createElement(Select.Viewport, null, ComputeValueType.literals.map((type) => /* @__PURE__ */ React3.createElement(Select.Option, {
789
+ key: type,
790
+ value: type
791
+ }, type))), /* @__PURE__ */ React3.createElement(Select.ScrollDownButton, null), /* @__PURE__ */ React3.createElement(Select.Arrow, null))));
792
+ };
793
+
794
+ // packages/ui/react-ui-canvas-compute/src/shapes/Array.tsx
795
+ import React4 from "react";
796
+ import { ReducerInput, ReducerOutput } from "@dxos/conductor";
797
+ import { S as S3 } from "@dxos/echo-schema";
798
+ var ReducerShape = S3.extend(ComputeShape, S3.Struct({
799
+ type: S3.Literal("reducer")
800
+ }));
801
+ var ReducerComponent = ({ shape }) => {
802
+ return /* @__PURE__ */ React4.createElement(FunctionBody, {
803
+ shape,
804
+ inputSchema: ReducerInput,
805
+ outputSchema: ReducerOutput
806
+ });
807
+ };
808
+ var createReducer = ({ id, size = {
809
+ width: 192,
810
+ height: getHeight(ReducerInput)
811
+ }, ...rest }) => createShape({
812
+ type: "reducer",
813
+ size,
814
+ ...rest
815
+ });
816
+ var reducerShape = {
817
+ type: "reducer",
818
+ name: "Reducer",
819
+ icon: "ph--repeat--regular",
820
+ component: (props) => /* @__PURE__ */ React4.createElement(ReducerComponent, props),
821
+ createShape: createReducer,
822
+ getAnchors: (shape) => createFunctionAnchors(shape, ReducerInput, ReducerOutput)
823
+ };
824
+
825
+ // packages/ui/react-ui-canvas-compute/src/shapes/Append.tsx
826
+ import React5 from "react";
827
+ import { AppendInput } from "@dxos/conductor";
828
+ import { S as S4 } from "@dxos/echo-schema";
829
+ var AppendShape = S4.extend(ComputeShape, S4.Struct({
830
+ type: S4.Literal("append")
831
+ }));
832
+ var createAppend = (props) => createShape({
833
+ type: "append",
834
+ size: {
835
+ width: 128,
836
+ height: getHeight(AppendInput)
837
+ },
838
+ ...props
839
+ });
840
+ var AppendComponent = ({ shape }) => {
841
+ return /* @__PURE__ */ React5.createElement(FunctionBody, {
842
+ shape,
843
+ inputSchema: AppendInput
844
+ });
845
+ };
846
+ var appendShape = {
847
+ type: "append",
848
+ name: "Append",
849
+ icon: "ph--list-plus--regular",
850
+ component: AppendComponent,
851
+ createShape: createAppend,
852
+ getAnchors: (shape) => createFunctionAnchors(shape, AppendInput)
853
+ };
854
+
855
+ // packages/ui/react-ui-canvas-compute/src/shapes/Audio.tsx
856
+ import React6, { useEffect as useEffect2, useState as useState3 } from "react";
857
+ import { S as S5 } from "@dxos/echo-schema";
858
+ import { Icon as Icon2 } from "@dxos/react-ui";
859
+ import { createAnchorMap } from "@dxos/react-ui-canvas-editor";
860
+ var AudioShape = S5.extend(ComputeShape, S5.Struct({
861
+ type: S5.Literal("audio")
862
+ }));
863
+ var createAudio = (props) => createShape({
864
+ type: "audio",
865
+ size: {
866
+ width: 64,
867
+ height: 64
868
+ },
869
+ ...props
870
+ });
871
+ var AudioComponent = ({ shape }) => {
872
+ const { node } = useComputeNodeState(shape);
873
+ const [active, setActive] = useState3(false);
874
+ useEffect2(() => {
875
+ node.value = active;
876
+ }, [
877
+ active
878
+ ]);
879
+ return /* @__PURE__ */ React6.createElement("div", {
880
+ className: "flex w-full justify-center items-center"
881
+ }, /* @__PURE__ */ React6.createElement(Icon2, {
882
+ icon: active ? "ph--microphone--regular" : "ph--microphone-slash--regular",
883
+ classNames: [
884
+ "transition opacity-20 duration-1000",
885
+ active && "opacity-100 text-red-500"
886
+ ],
887
+ size: 8,
888
+ onClick: () => setActive(!active)
889
+ }));
890
+ };
891
+ var audioShape = {
892
+ type: "audio",
893
+ name: "Audio",
894
+ icon: "ph--microphone--regular",
895
+ component: AudioComponent,
896
+ createShape: createAudio,
897
+ getAnchors: (shape) => createAnchorMap(shape, {
898
+ [createAnchorId("output")]: {
899
+ x: 1,
900
+ y: 0
901
+ }
902
+ })
903
+ };
904
+
905
+ // packages/ui/react-ui-canvas-compute/src/shapes/Beacon.tsx
906
+ import React7 from "react";
907
+ import { DEFAULT_INPUT as DEFAULT_INPUT3, isTruthy } from "@dxos/conductor";
908
+ import { S as S6 } from "@dxos/echo-schema";
909
+ import { Icon as Icon3 } from "@dxos/react-ui";
910
+ import { createAnchorMap as createAnchorMap2 } from "@dxos/react-ui-canvas-editor";
911
+ import { mx as mx2 } from "@dxos/react-ui-theme";
912
+ var BeaconShape = S6.extend(ComputeShape, S6.Struct({
913
+ type: S6.Literal("beacon")
914
+ }));
915
+ var createBeacon = (props) => createShape({
916
+ type: "beacon",
917
+ size: {
918
+ width: 64,
919
+ height: 64
920
+ },
921
+ ...props
922
+ });
923
+ var BeaconComponent = ({ shape }) => {
924
+ const { runtime } = useComputeNodeState(shape);
925
+ const input = runtime.inputs[DEFAULT_INPUT3];
926
+ const value = input?.type === "executed" ? input.value : false;
927
+ return /* @__PURE__ */ React7.createElement("div", {
928
+ className: "flex w-full justify-center items-center"
929
+ }, /* @__PURE__ */ React7.createElement(Icon3, {
930
+ icon: "ph--sun--regular",
931
+ classNames: mx2("transition opacity-20 duration-1000", isTruthy(value) && "opacity-100 text-yellow-500"),
932
+ size: 8
933
+ }));
934
+ };
935
+ var beaconShape = {
936
+ type: "beacon",
937
+ name: "Beacon",
938
+ icon: "ph--sun--regular",
939
+ component: BeaconComponent,
940
+ createShape: createBeacon,
941
+ getAnchors: (shape) => createAnchorMap2(shape, {
942
+ [createAnchorId("input")]: {
943
+ x: -1,
944
+ y: 0
945
+ }
946
+ })
947
+ };
948
+
949
+ // packages/ui/react-ui-canvas-compute/src/shapes/Boolean.tsx
950
+ import React8 from "react";
951
+ import { S as S7 } from "@dxos/echo-schema";
952
+ import { createAnchors as createAnchors2, getAnchorPoints } from "@dxos/react-ui-canvas-editor";
953
+ var GateShape = S7.extend(ComputeShape, S7.Struct({
954
+ type: S7.String
955
+ }));
956
+ var createGate = (props) => createShape({
957
+ size: {
958
+ width: 64,
959
+ height: 64
960
+ },
961
+ ...props
962
+ });
963
+ var GateComponent = (Symbol2) => () => {
964
+ return /* @__PURE__ */ React8.createElement("div", {
965
+ className: "flex w-full justify-center items-center"
966
+ }, /* @__PURE__ */ React8.createElement(Symbol2, null));
967
+ };
968
+ var defineShape = ({ type, name, icon, Symbol: Symbol2, createShape: createShape2, inputs, outputs = [
969
+ createAnchorId("output")
970
+ ] }) => ({
971
+ type,
972
+ name,
973
+ icon,
974
+ component: GateComponent(Symbol2),
975
+ createShape: createShape2,
976
+ getAnchors: (shape) => createAnchors2({
977
+ shape,
978
+ inputs,
979
+ outputs
980
+ })
981
+ });
982
+ var Symbol = (pathConstructor, inputs) => ({
983
+ width = 64,
984
+ height = 32,
985
+ // TODO(burdon): Same as line color.
986
+ className = "fill-neutral-200 dark:fill-neutral-800 stroke-neutral-500",
987
+ strokeWidth = 1
988
+ }) => {
989
+ const startX = width * 0.25;
990
+ const endX = width * 0.75;
991
+ const centerY = height / 2;
992
+ const paths = pathConstructor({
993
+ startX,
994
+ endX,
995
+ height
996
+ });
997
+ return /* @__PURE__ */ React8.createElement("svg", {
998
+ viewBox: `0 0 ${width} ${height}`,
999
+ className: "w-full h-full"
1000
+ }, getAnchorPoints({
1001
+ x: 0,
1002
+ y: centerY
1003
+ }, inputs).map(({ x, y }, i) => /* @__PURE__ */ React8.createElement("line", {
1004
+ key: i,
1005
+ x1: x,
1006
+ y1: y,
1007
+ x2: startX * 1.3,
1008
+ y2: y,
1009
+ strokeWidth,
1010
+ className
1011
+ })), /* @__PURE__ */ React8.createElement("line", {
1012
+ x1: endX,
1013
+ y1: centerY,
1014
+ x2: width,
1015
+ y2: centerY,
1016
+ strokeWidth,
1017
+ className
1018
+ }), paths.map((path, i) => /* @__PURE__ */ React8.createElement("path", {
1019
+ key: i,
1020
+ d: path,
1021
+ strokeWidth,
1022
+ className
1023
+ })));
1024
+ };
1025
+ var AndSymbol = Symbol(({ startX, endX, height }) => {
1026
+ const arcRadius = (endX - startX) / 2;
1027
+ return [
1028
+ `
1029
+ M ${startX},${height * 0.1}
1030
+ L ${endX - arcRadius},${height * 0.1}
1031
+ A ${arcRadius},${height * 0.4} 0 0 1 ${endX - arcRadius},${height * 0.9}
1032
+ L ${startX},${height * 0.9}
1033
+ Z
1034
+ `
1035
+ ];
1036
+ }, 2);
1037
+ var AndShape = GateShape;
1038
+ var createAnd = (props) => {
1039
+ return createGate({
1040
+ ...props,
1041
+ type: "and"
1042
+ });
1043
+ };
1044
+ var andShape = defineShape({
1045
+ type: "and",
1046
+ name: "AND",
1047
+ icon: "ph--intersection--regular",
1048
+ Symbol: AndSymbol,
1049
+ createShape: createAnd,
1050
+ inputs: [
1051
+ "input.a",
1052
+ "input.b"
1053
+ ]
1054
+ });
1055
+ var OrSymbol = Symbol(({ startX, endX, height }) => {
1056
+ const arcRadius = (endX - startX) / 2;
1057
+ return [
1058
+ `
1059
+ M ${startX},${height * 0.1}
1060
+ L ${endX - arcRadius},${height * 0.1}
1061
+ A ${arcRadius},${height * 0.4} 0 0 1 ${endX - arcRadius},${height * 0.9}
1062
+ L ${startX},${height * 0.9}
1063
+ C ${startX * 1.4},${height * 0.5} ${startX * 1.4},${height * 0.5} ${startX},${height * 0.1}
1064
+ Z
1065
+ `
1066
+ ];
1067
+ }, 2);
1068
+ var OrShape = GateShape;
1069
+ var createOr = (props) => {
1070
+ return createGate({
1071
+ ...props,
1072
+ type: "or"
1073
+ });
1074
+ };
1075
+ var orShape = defineShape({
1076
+ type: "or",
1077
+ name: "OR",
1078
+ icon: "ph--union--regular",
1079
+ Symbol: OrSymbol,
1080
+ createShape: createOr,
1081
+ inputs: [
1082
+ "input.a",
1083
+ "input.b"
1084
+ ]
1085
+ });
1086
+ var NotSymbol = Symbol(({ startX, endX, height }) => {
1087
+ return [
1088
+ `
1089
+ M ${startX},${height * 0.1}
1090
+ L ${endX * 0.9},${height * 0.5}
1091
+ L ${startX},${height * 0.9}
1092
+ Z
1093
+ `,
1094
+ `
1095
+ M ${endX - height * 0.2},${height * 0.5}
1096
+ A ${height * 0.1} ${height * 0.1} 0 0 1 ${endX},${height * 0.5}
1097
+ A ${height * 0.1} ${height * 0.1} 0 0 1 ${endX - height * 0.2},${height * 0.5}
1098
+ Z
1099
+ `
1100
+ ];
1101
+ }, 1);
1102
+ var NotShape = GateShape;
1103
+ var createNot = (props) => {
1104
+ return createGate({
1105
+ ...props,
1106
+ type: "not"
1107
+ });
1108
+ };
1109
+ var notShape = defineShape({
1110
+ type: "not",
1111
+ name: "NOT",
1112
+ icon: "ph--x--regular",
1113
+ Symbol: NotSymbol,
1114
+ createShape: createNot,
1115
+ inputs: [
1116
+ createAnchorId("input")
1117
+ ]
1118
+ });
1119
+
1120
+ // packages/ui/react-ui-canvas-compute/src/shapes/Chat.tsx
1121
+ import React9, { useRef as useRef2 } from "react";
1122
+ import { DEFAULT_OUTPUT as DEFAULT_OUTPUT3 } from "@dxos/conductor";
1123
+ import { S as S8 } from "@dxos/echo-schema";
1124
+ import { TextBox } from "@dxos/react-ui-canvas-editor";
1125
+ import { createAnchorMap as createAnchorMap3 } from "@dxos/react-ui-canvas-editor";
1126
+ var ChatShape = S8.extend(ComputeShape, S8.Struct({
1127
+ type: S8.Literal("chat")
1128
+ }));
1129
+ var TextInputComponent = ({ shape, title, ...props }) => {
1130
+ const { runtime } = useComputeNodeState(shape);
1131
+ const inputRef = useRef2(null);
1132
+ const handleEnter = (text) => {
1133
+ const value = text.trim();
1134
+ if (value.length) {
1135
+ runtime.setOutput(DEFAULT_OUTPUT3, value);
1136
+ inputRef.current?.setText("");
1137
+ }
1138
+ };
1139
+ return /* @__PURE__ */ React9.createElement(Box, {
1140
+ shape,
1141
+ title
1142
+ }, /* @__PURE__ */ React9.createElement(TextBox, {
1143
+ ref: inputRef,
1144
+ onEnter: handleEnter,
1145
+ ...props
1146
+ }));
1147
+ };
1148
+ var createChat = (props) => createShape({
1149
+ type: "chat",
1150
+ size: {
1151
+ width: 256,
1152
+ height: 128
1153
+ },
1154
+ ...props
1155
+ });
1156
+ var chatShape = {
1157
+ type: "chat",
1158
+ name: "Chat",
1159
+ icon: "ph--textbox--regular",
1160
+ component: (props) => /* @__PURE__ */ React9.createElement(TextInputComponent, {
1161
+ ...props,
1162
+ title: "Prompt",
1163
+ placeholder: "Message"
1164
+ }),
1165
+ createShape: createChat,
1166
+ getAnchors: (shape) => createAnchorMap3(shape, {
1167
+ [createAnchorId("output")]: {
1168
+ x: 1,
1169
+ y: 0
1170
+ }
1171
+ }),
1172
+ resizable: true
1173
+ };
1174
+
1175
+ // packages/ui/react-ui-canvas-compute/src/shapes/Constant.tsx
1176
+ import React10, { useCallback as useCallback2, useRef as useRef3, useState as useState4 } from "react";
1177
+ import { ComputeValueType as ComputeValueType2 } from "@dxos/conductor";
1178
+ import { S as S9 } from "@dxos/echo-schema";
1179
+ import { Input } from "@dxos/react-ui";
1180
+ import { TextBox as TextBox2 } from "@dxos/react-ui-canvas-editor";
1181
+ import { createAnchorMap as createAnchorMap4 } from "@dxos/react-ui-canvas-editor";
1182
+ import { safeParseJson } from "@dxos/util";
1183
+ var ConstantShape = S9.extend(ComputeShape, S9.Struct({
1184
+ type: S9.Literal("constant"),
1185
+ value: S9.optional(S9.Any)
1186
+ }));
1187
+ var inferType = (value) => {
1188
+ if (typeof value === "string") {
1189
+ return "string";
1190
+ } else if (typeof value === "number") {
1191
+ return "number";
1192
+ } else if (typeof value === "boolean") {
1193
+ return "boolean";
1194
+ } else if (typeof value === "object") {
1195
+ return "object";
1196
+ }
1197
+ };
1198
+ var ConstantComponent = ({ shape, title, chat, ...props }) => {
1199
+ const { node } = useComputeNodeState(shape);
1200
+ const [type, setType] = useState4(inferType(node.value) ?? ComputeValueType2.literals[0]);
1201
+ const inputRef = useRef3(null);
1202
+ const handleEnter = useCallback2((text) => {
1203
+ const value = text.trim();
1204
+ if (value.length) {
1205
+ if (type === "number") {
1206
+ const floatValue = parseFloat(value);
1207
+ if (!isNaN(floatValue)) {
1208
+ node.value = floatValue;
1209
+ }
1210
+ } else if (type === "object") {
1211
+ node.value = safeParseJson(value, {});
1212
+ } else {
1213
+ node.value = value;
1214
+ }
1215
+ inputRef.current?.focus();
1216
+ }
1217
+ }, [
1218
+ type
1219
+ ]);
1220
+ return /* @__PURE__ */ React10.createElement(Box, {
1221
+ shape,
1222
+ title,
1223
+ status: /* @__PURE__ */ React10.createElement(TypeSelect, {
1224
+ value: type,
1225
+ onValueChange: setType
1226
+ })
1227
+ }, (type === "string" || type === "number") && /* @__PURE__ */ React10.createElement(TextBox2, {
1228
+ ...props,
1229
+ ref: inputRef,
1230
+ value: node.value,
1231
+ onEnter: handleEnter
1232
+ }), type === "object" && /* @__PURE__ */ React10.createElement(TextBox2, {
1233
+ ...props,
1234
+ ref: inputRef,
1235
+ value: JSON.stringify(node.value, null, 2),
1236
+ language: "json"
1237
+ }), type === "boolean" && /* @__PURE__ */ React10.createElement("div", {
1238
+ className: "flex grow justify-center items-center"
1239
+ }, /* @__PURE__ */ React10.createElement(Input.Root, null, /* @__PURE__ */ React10.createElement(Input.Switch, {
1240
+ checked: node.value,
1241
+ onCheckedChange: (value) => {
1242
+ node.value = value;
1243
+ }
1244
+ }))));
1245
+ };
1246
+ var createConstant = (props) => createShape({
1247
+ type: "constant",
1248
+ size: {
1249
+ width: 192,
1250
+ height: 128
1251
+ },
1252
+ ...props
1253
+ });
1254
+ var constantShape = {
1255
+ type: "constant",
1256
+ name: "Value",
1257
+ icon: "ph--dots-three-circle--regular",
1258
+ component: (props) => /* @__PURE__ */ React10.createElement(ConstantComponent, {
1259
+ ...props,
1260
+ placeholder: "Constant"
1261
+ }),
1262
+ createShape: createConstant,
1263
+ getAnchors: (shape) => createAnchorMap4(shape, {
1264
+ [createAnchorId("output")]: {
1265
+ x: 1,
1266
+ y: 0
1267
+ }
1268
+ }),
1269
+ resizable: true
1270
+ };
1271
+
1272
+ // packages/ui/react-ui-canvas-compute/src/shapes/Database.tsx
1273
+ import React11 from "react";
1274
+ import { S as S10 } from "@dxos/echo-schema";
1275
+ import { createAnchorMap as createAnchorMap5 } from "@dxos/react-ui-canvas-editor";
1276
+ var DatabaseShape = S10.extend(ComputeShape, S10.Struct({
1277
+ type: S10.Literal("database")
1278
+ }));
1279
+ var createDatabase = (props) => createShape({
1280
+ type: "database",
1281
+ size: {
1282
+ width: 128,
1283
+ height: 64
1284
+ },
1285
+ ...props
1286
+ });
1287
+ var DatabaseComponent = ({ shape }) => {
1288
+ return /* @__PURE__ */ React11.createElement(Box, {
1289
+ shape
1290
+ });
1291
+ };
1292
+ var databaseShape = {
1293
+ type: "database",
1294
+ name: "ECHO",
1295
+ icon: "ph--database--regular",
1296
+ component: DatabaseComponent,
1297
+ createShape: createDatabase,
1298
+ getAnchors: (shape) => createAnchorMap5(shape, {
1299
+ [createAnchorId("output")]: {
1300
+ x: 1,
1301
+ y: 0
1302
+ }
1303
+ })
1304
+ };
1305
+
1306
+ // packages/ui/react-ui-canvas-compute/src/shapes/Function.tsx
1307
+ import React12 from "react";
1308
+ import { AnyOutput, FunctionInput } from "@dxos/conductor";
1309
+ import { S as S11 } from "@dxos/echo-schema";
1310
+ var FunctionShape = S11.extend(ComputeShape, S11.Struct({
1311
+ type: S11.Literal("function")
1312
+ }));
1313
+ var createFunction = (props) => createShape({
1314
+ type: "function",
1315
+ size: {
1316
+ width: 192,
1317
+ height: getHeight(FunctionInput)
1318
+ },
1319
+ ...props
1320
+ });
1321
+ var FunctionComponent = ({ shape }) => {
1322
+ return /* @__PURE__ */ React12.createElement(FunctionBody, {
1323
+ shape,
1324
+ inputSchema: FunctionInput,
1325
+ outputSchema: AnyOutput
1326
+ });
1327
+ };
1328
+ var functionShape = {
1329
+ type: "function",
1330
+ name: "Function",
1331
+ icon: "ph--function--regular",
1332
+ component: FunctionComponent,
1333
+ createShape: createFunction,
1334
+ // TODO(burdon): Get dynamic schema.
1335
+ getAnchors: (shape) => createFunctionAnchors(shape, FunctionInput, AnyOutput)
1336
+ };
1337
+
1338
+ // packages/ui/react-ui-canvas-compute/src/shapes/Gpt.tsx
1339
+ import React13, { useEffect as useEffect3, useState as useState5 } from "react";
1340
+ import { GptInput, GptOutput } from "@dxos/conductor";
1341
+ import { S as S12 } from "@dxos/echo-schema";
1342
+ var GptShape = S12.extend(ComputeShape, S12.Struct({
1343
+ type: S12.Literal("gpt")
1344
+ }));
1345
+ var createGpt = (props) => createShape({
1346
+ type: "gpt",
1347
+ size: {
1348
+ width: 256,
1349
+ height: Math.max(getHeight(GptInput), getHeight(GptOutput))
1350
+ },
1351
+ ...props
1352
+ });
1353
+ var GptComponent = ({ shape }) => {
1354
+ const { meta, runtime } = useComputeNodeState(shape);
1355
+ const [text, setText] = useState5("");
1356
+ const [tokens, setTokens] = useState5(0);
1357
+ useEffect3(() => {
1358
+ return runtime.subscribeToEventLog((ev) => {
1359
+ switch (ev.type) {
1360
+ case "begin-compute": {
1361
+ setText("");
1362
+ break;
1363
+ }
1364
+ case "custom": {
1365
+ const token = ev.event;
1366
+ switch (token.type) {
1367
+ case "content_block_delta":
1368
+ switch (token.delta.type) {
1369
+ case "text_delta": {
1370
+ const delta = token.delta.text;
1371
+ setText((prev) => {
1372
+ const text2 = prev + delta;
1373
+ setTokens(text2.split(" ").length);
1374
+ return text2;
1375
+ });
1376
+ break;
1377
+ }
1378
+ }
1379
+ break;
1380
+ }
1381
+ break;
1382
+ }
1383
+ }
1384
+ });
1385
+ }, [
1386
+ runtime?.subscribeToEventLog
1387
+ ]);
1388
+ return /* @__PURE__ */ React13.createElement(FunctionBody, {
1389
+ shape,
1390
+ content: /* @__PURE__ */ React13.createElement("div", {
1391
+ className: "px-2 py-1 overflow-y-scroll"
1392
+ }, text),
1393
+ status: `${tokens} tokens`,
1394
+ inputSchema: meta.input,
1395
+ outputSchema: meta.output
1396
+ });
1397
+ };
1398
+ var gptShape = {
1399
+ type: "gpt",
1400
+ name: "GPT",
1401
+ icon: "ph--brain--regular",
1402
+ component: GptComponent,
1403
+ createShape: createGpt,
1404
+ getAnchors: (shape) => createFunctionAnchors(shape, GptInput, GptOutput),
1405
+ openable: true
1406
+ };
1407
+
1408
+ // packages/ui/react-ui-canvas-compute/src/shapes/Json.tsx
1409
+ import React14 from "react";
1410
+ import { DEFAULT_INPUT as DEFAULT_INPUT4, DefaultOutput, JsonTransformInput } from "@dxos/conductor";
1411
+ import { S as S13 } from "@dxos/echo-schema";
1412
+ import { createAnchorMap as createAnchorMap6 } from "@dxos/react-ui-canvas-editor";
1413
+ import { JsonFilter } from "@dxos/react-ui-syntax-highlighter";
1414
+ var JsonShape = S13.extend(ComputeShape, S13.Struct({
1415
+ type: S13.Literal("json")
1416
+ }));
1417
+ var JsonTransformShape = S13.extend(ComputeShape, S13.Struct({
1418
+ type: S13.Literal("json-transform")
1419
+ }));
1420
+ var JsonComponent = ({ shape, ...props }) => {
1421
+ const { runtime } = useComputeNodeState(shape);
1422
+ const input = runtime.inputs[DEFAULT_INPUT4];
1423
+ const value = input?.type === "executed" ? input.value : void 0;
1424
+ return /* @__PURE__ */ React14.createElement(Box, {
1425
+ shape
1426
+ }, /* @__PURE__ */ React14.createElement(JsonFilter, {
1427
+ data: value,
1428
+ classNames: "text-xs"
1429
+ }));
1430
+ };
1431
+ var JsonTransformComponent = ({ shape, ...props }) => {
1432
+ return /* @__PURE__ */ React14.createElement(Box, {
1433
+ shape
1434
+ });
1435
+ };
1436
+ var createJson = (props) => createShape({
1437
+ type: "json",
1438
+ size: {
1439
+ width: 256,
1440
+ height: 256
1441
+ },
1442
+ ...props
1443
+ });
1444
+ var jsonShape = {
1445
+ type: "json",
1446
+ name: "JSON",
1447
+ icon: "ph--code--regular",
1448
+ component: (props) => /* @__PURE__ */ React14.createElement(JsonComponent, props),
1449
+ createShape: createJson,
1450
+ getAnchors: (shape) => createAnchorMap6(shape, {
1451
+ [createAnchorId("input")]: {
1452
+ x: -1,
1453
+ y: 0
1454
+ },
1455
+ [createAnchorId("output")]: {
1456
+ x: 1,
1457
+ y: 0
1458
+ }
1459
+ }),
1460
+ resizable: true
1461
+ };
1462
+ var createJsonTransform = (props) => createShape({
1463
+ type: "json-transform",
1464
+ size: {
1465
+ width: 128,
1466
+ height: getHeight(JsonTransformInput)
1467
+ },
1468
+ ...props
1469
+ });
1470
+ var jsonTransformShape = {
1471
+ type: "json-transform",
1472
+ name: "Transform",
1473
+ icon: "ph--shuffle-simple--regular",
1474
+ component: (props) => /* @__PURE__ */ React14.createElement(JsonTransformComponent, props),
1475
+ createShape: createJsonTransform,
1476
+ getAnchors: (shape) => createFunctionAnchors(shape, JsonTransformInput, DefaultOutput),
1477
+ resizable: true
1478
+ };
1479
+
1480
+ // packages/ui/react-ui-canvas-compute/src/shapes/Logic.tsx
1481
+ import React15 from "react";
1482
+ import { IfElseInput, IfElseOutput, IfInput, IfOutput } from "@dxos/conductor";
1483
+ import { S as S14 } from "@dxos/echo-schema";
1484
+ var IfShape = S14.extend(ComputeShape, S14.Struct({
1485
+ type: S14.Literal("if")
1486
+ }));
1487
+ var IfElseShape = S14.extend(ComputeShape, S14.Struct({
1488
+ type: S14.Literal("if-else")
1489
+ }));
1490
+ var IfComponent = ({ shape, ...props }) => {
1491
+ return /* @__PURE__ */ React15.createElement(FunctionBody, {
1492
+ shape,
1493
+ inputSchema: IfInput,
1494
+ outputSchema: IfOutput
1495
+ });
1496
+ };
1497
+ var IfElseComponent = ({ shape, ...props }) => {
1498
+ return /* @__PURE__ */ React15.createElement(FunctionBody, {
1499
+ shape,
1500
+ inputSchema: IfElseInput,
1501
+ outputSchema: IfElseOutput
1502
+ });
1503
+ };
1504
+ var createIf = (props) => createShape({
1505
+ type: "if",
1506
+ size: {
1507
+ width: 192,
1508
+ height: getHeight(IfInput)
1509
+ },
1510
+ ...props
1511
+ });
1512
+ var ifShape = {
1513
+ type: "if",
1514
+ name: "IF",
1515
+ icon: "ph--arrows-split--regular",
1516
+ component: (props) => /* @__PURE__ */ React15.createElement(IfComponent, props),
1517
+ createShape: createIf,
1518
+ getAnchors: (shape) => createFunctionAnchors(shape, IfInput, IfOutput)
1519
+ };
1520
+ var createIfElse = (props) => createShape({
1521
+ type: "if-else",
1522
+ size: {
1523
+ width: 192,
1524
+ height: getHeight(IfElseInput)
1525
+ },
1526
+ ...props
1527
+ });
1528
+ var ifElseShape = {
1529
+ type: "if-else",
1530
+ name: "IF/ELSE",
1531
+ icon: "ph--arrows-merge--regular",
1532
+ component: (props) => /* @__PURE__ */ React15.createElement(IfElseComponent, props),
1533
+ createShape: createIfElse,
1534
+ getAnchors: (shape) => createFunctionAnchors(shape, IfElseInput, IfElseOutput)
1535
+ };
1536
+
1537
+ // packages/ui/react-ui-canvas-compute/src/shapes/Queue.tsx
1538
+ import React16, { Fragment } from "react";
1539
+ import { DEFAULT_OUTPUT as DEFAULT_OUTPUT4, QueueInput, QueueOutput } from "@dxos/conductor";
1540
+ import { S as S15 } from "@dxos/echo-schema";
1541
+ import { mx as mx3 } from "@dxos/react-ui-theme";
1542
+ var QueueShape = S15.extend(ComputeShape, S15.Struct({
1543
+ type: S15.Literal("queue")
1544
+ }));
1545
+ var createQueue = (props) => createShape({
1546
+ type: "queue",
1547
+ size: {
1548
+ width: 256,
1549
+ height: 512
1550
+ },
1551
+ ...props
1552
+ });
1553
+ var QueueComponent = ({ shape }) => {
1554
+ const { runtime } = useComputeNodeState(shape);
1555
+ const items = runtime.outputs[DEFAULT_OUTPUT4]?.type === "executed" ? runtime.outputs[DEFAULT_OUTPUT4].value : [];
1556
+ const handleAction = (action) => {
1557
+ if (action === "run") {
1558
+ runtime.evalNode();
1559
+ }
1560
+ };
1561
+ return /* @__PURE__ */ React16.createElement(Box, {
1562
+ shape,
1563
+ status: `${items.length} items`,
1564
+ onAction: handleAction
1565
+ }, /* @__PURE__ */ React16.createElement("div", {
1566
+ className: "flex flex-col w-full overflow-y-scroll divide-y divide-separator"
1567
+ }, [
1568
+ ...items
1569
+ ].map((item, i) => /* @__PURE__ */ React16.createElement(QueueItem, {
1570
+ key: i,
1571
+ classNames: "p-1 px-2",
1572
+ item
1573
+ }))));
1574
+ };
1575
+ var QueueItem = ({ classNames, item }) => {
1576
+ if (typeof item !== "object") {
1577
+ return /* @__PURE__ */ React16.createElement("div", {
1578
+ className: mx3(classNames, "whitespace-pre-wrap")
1579
+ }, item);
1580
+ }
1581
+ return /* @__PURE__ */ React16.createElement("div", {
1582
+ className: mx3("grid grid-cols-[80px,1fr]", classNames)
1583
+ }, Object.entries(item).map(([key, value]) => /* @__PURE__ */ React16.createElement(Fragment, {
1584
+ key
1585
+ }, /* @__PURE__ */ React16.createElement("div", {
1586
+ className: "p-1 text-xs text-subdued"
1587
+ }, key), /* @__PURE__ */ React16.createElement("div", null, typeof value === "string" ? value : JSON.stringify(value)))));
1588
+ };
1589
+ var queueShape = {
1590
+ type: "queue",
1591
+ name: "Queue",
1592
+ icon: "ph--queue--regular",
1593
+ component: QueueComponent,
1594
+ createShape: createQueue,
1595
+ getAnchors: (shape) => createFunctionAnchors(shape, QueueInput, QueueOutput),
1596
+ resizable: true
1597
+ };
1598
+
1599
+ // packages/ui/react-ui-canvas-compute/src/shapes/RNG.tsx
1600
+ import React17, { useEffect as useEffect4, useState as useState6 } from "react";
1601
+ import { DEFAULT_OUTPUT as DEFAULT_OUTPUT5 } from "@dxos/conductor";
1602
+ import { S as S16 } from "@dxos/echo-schema";
1603
+ import { Icon as Icon4 } from "@dxos/react-ui";
1604
+ import { createAnchorMap as createAnchorMap7 } from "@dxos/react-ui-canvas-editor";
1605
+ import { mx as mx4 } from "@dxos/react-ui-theme";
1606
+ var RandomShape = S16.extend(ComputeShape, S16.Struct({
1607
+ type: S16.Literal("rng"),
1608
+ min: S16.optional(S16.Number),
1609
+ max: S16.optional(S16.Number)
1610
+ }));
1611
+ var createRandom = (props) => createShape({
1612
+ type: "rng",
1613
+ size: {
1614
+ width: 64,
1615
+ height: 64
1616
+ },
1617
+ ...props
1618
+ });
1619
+ var icons = [
1620
+ "ph--dice-one--regular",
1621
+ "ph--dice-two--regular",
1622
+ "ph--dice-three--regular",
1623
+ "ph--dice-four--regular",
1624
+ "ph--dice-five--regular",
1625
+ "ph--dice-six--regular"
1626
+ ];
1627
+ var pickIcon = () => icons[Math.floor(Math.random() * icons.length)];
1628
+ var RandomComponent = ({ shape }) => {
1629
+ const { runtime } = useComputeNodeState(shape);
1630
+ const [spin, setSpin] = useState6(false);
1631
+ const [icon, setIcon] = useState6(pickIcon());
1632
+ useEffect4(() => {
1633
+ if (!spin) {
1634
+ return;
1635
+ }
1636
+ const i = setInterval(() => setIcon(pickIcon()), 250);
1637
+ const t1 = setTimeout(() => clearInterval(i), 900);
1638
+ const t2 = setTimeout(() => setSpin(false), 1100);
1639
+ return () => {
1640
+ clearInterval(i);
1641
+ clearTimeout(t1);
1642
+ clearTimeout(t2);
1643
+ };
1644
+ }, [
1645
+ spin
1646
+ ]);
1647
+ const handleClick = (ev) => {
1648
+ ev.stopPropagation();
1649
+ runtime.setOutput(DEFAULT_OUTPUT5, Math.random());
1650
+ setSpin(true);
1651
+ };
1652
+ return /* @__PURE__ */ React17.createElement("div", {
1653
+ className: "flex grow items-center justify-center"
1654
+ }, /* @__PURE__ */ React17.createElement(Icon4, {
1655
+ icon,
1656
+ classNames: mx4(spin && "animate-[spin_1s]"),
1657
+ size: 10,
1658
+ onClick: handleClick
1659
+ }));
1660
+ };
1661
+ var randomShape = {
1662
+ type: "rng",
1663
+ name: "Random",
1664
+ icon: "ph--dice-six--regular",
1665
+ component: RandomComponent,
1666
+ createShape: createRandom,
1667
+ getAnchors: (shape) => createAnchorMap7(shape, {
1668
+ [createAnchorId("output")]: {
1669
+ x: 1,
1670
+ y: 0
1671
+ }
1672
+ })
1673
+ };
1674
+
1675
+ // packages/ui/react-ui-canvas-compute/src/shapes/Scope.tsx
1676
+ import React18 from "react";
1677
+ import { DEFAULT_INPUT as DEFAULT_INPUT5 } from "@dxos/conductor";
1678
+ import { S as S17 } from "@dxos/echo-schema";
1679
+ import { createAnchorMap as createAnchorMap8 } from "@dxos/react-ui-canvas-editor";
1680
+ import { useAudioStream, Chaos, shaderPresets } from "@dxos/react-ui-sfx";
1681
+ var ScopeShape = S17.extend(ComputeShape, S17.Struct({
1682
+ type: S17.Literal("scope")
1683
+ }));
1684
+ var createScope = (props) => createShape({
1685
+ type: "scope",
1686
+ size: {
1687
+ width: 128,
1688
+ height: 128
1689
+ },
1690
+ classNames: "rounded-full border-primary-800",
1691
+ ...props
1692
+ });
1693
+ var ScopeComponent = ({ shape }) => {
1694
+ const { runtime } = useComputeNodeState(shape);
1695
+ const input = runtime.inputs[DEFAULT_INPUT5];
1696
+ const active = input?.type === "executed" ? input.value : false;
1697
+ const { getAverage } = useAudioStream(active);
1698
+ return /* @__PURE__ */ React18.createElement("div", {
1699
+ className: "flex w-full justify-center items-center bg-black"
1700
+ }, /* @__PURE__ */ React18.createElement(Chaos, {
1701
+ active,
1702
+ getValue: getAverage,
1703
+ options: {
1704
+ ...shaderPresets.heptapod,
1705
+ zoom: 1.2
1706
+ }
1707
+ }));
1708
+ };
1709
+ var scopeShape = {
1710
+ type: "scope",
1711
+ name: "Scope",
1712
+ icon: "ph--waveform--regular",
1713
+ component: ScopeComponent,
1714
+ createShape: createScope,
1715
+ getAnchors: (shape) => createAnchorMap8(shape, {
1716
+ [createAnchorId("input")]: {
1717
+ x: -1,
1718
+ y: 0
1719
+ }
1720
+ })
1721
+ };
1722
+
1723
+ // packages/ui/react-ui-canvas-compute/src/shapes/Surface.tsx
1724
+ import React19 from "react";
1725
+ import { Surface } from "@dxos/app-framework";
1726
+ import { DEFAULT_INPUT as DEFAULT_INPUT6 } from "@dxos/conductor";
1727
+ import { S as S18 } from "@dxos/echo-schema";
1728
+ import { createAnchorMap as createAnchorMap9 } from "@dxos/react-ui-canvas-editor";
1729
+ var SurfaceShape = S18.extend(ComputeShape, S18.Struct({
1730
+ type: S18.Literal("surface")
1731
+ }));
1732
+ var createSurface = (props) => createShape({
1733
+ type: "surface",
1734
+ size: {
1735
+ width: 384,
1736
+ height: 384
1737
+ },
1738
+ ...props
1739
+ });
1740
+ var SurfaceComponent = ({ shape }) => {
1741
+ const { runtime } = useComputeNodeState(shape);
1742
+ const input = runtime.inputs[DEFAULT_INPUT6];
1743
+ const value = input?.type === "executed" ? input.value : null;
1744
+ const handleAction = (action) => {
1745
+ if (action === "run") {
1746
+ runtime.evalNode();
1747
+ }
1748
+ };
1749
+ return /* @__PURE__ */ React19.createElement(Box, {
1750
+ shape,
1751
+ onAction: handleAction
1752
+ }, value !== null && /* @__PURE__ */ React19.createElement(Surface, {
1753
+ role: "canvas-node",
1754
+ data: {
1755
+ value
1756
+ },
1757
+ limit: 1
1758
+ }));
1759
+ };
1760
+ var surfaceShape = {
1761
+ type: "surface",
1762
+ name: "Surface",
1763
+ icon: "ph--frame-corners--regular",
1764
+ component: SurfaceComponent,
1765
+ createShape: createSurface,
1766
+ getAnchors: (shape) => createAnchorMap9(shape, {
1767
+ [createAnchorId("input")]: {
1768
+ x: -1,
1769
+ y: 0
1770
+ }
1771
+ }),
1772
+ resizable: true
1773
+ };
1774
+
1775
+ // packages/ui/react-ui-canvas-compute/src/shapes/Switch.tsx
1776
+ import React20, { useEffect as useEffect5, useState as useState7 } from "react";
1777
+ import { DEFAULT_OUTPUT as DEFAULT_OUTPUT6 } from "@dxos/conductor";
1778
+ import { S as S19 } from "@dxos/echo-schema";
1779
+ import { Input as Input2 } from "@dxos/react-ui";
1780
+ import { createAnchorMap as createAnchorMap10 } from "@dxos/react-ui-canvas-editor";
1781
+ var SwitchShape = S19.extend(ComputeShape, S19.Struct({
1782
+ type: S19.Literal("switch")
1783
+ }));
1784
+ var createSwitch = (props) => createShape({
1785
+ type: "switch",
1786
+ size: {
1787
+ width: 64,
1788
+ height: 64
1789
+ },
1790
+ ...props
1791
+ });
1792
+ var SwitchComponent = ({ shape }) => {
1793
+ const { runtime } = useComputeNodeState(shape);
1794
+ const [value, setValue] = useState7(false);
1795
+ useEffect5(() => {
1796
+ runtime.setOutput(DEFAULT_OUTPUT6, value);
1797
+ }, [
1798
+ value
1799
+ ]);
1800
+ return /* @__PURE__ */ React20.createElement("div", {
1801
+ className: "flex w-full justify-center items-center",
1802
+ onClick: (ev) => ev.stopPropagation()
1803
+ }, /* @__PURE__ */ React20.createElement(Input2.Root, null, /* @__PURE__ */ React20.createElement(Input2.Switch, {
1804
+ checked: value,
1805
+ onCheckedChange: (value2) => setValue(value2)
1806
+ })));
1807
+ };
1808
+ var switchShape = {
1809
+ type: "switch",
1810
+ name: "Switch",
1811
+ icon: "ph--toggle-left--regular",
1812
+ component: SwitchComponent,
1813
+ createShape: createSwitch,
1814
+ getAnchors: (shape) => createAnchorMap10(shape, {
1815
+ [createAnchorId("output")]: {
1816
+ x: 1,
1817
+ y: 0
1818
+ }
1819
+ })
1820
+ };
1821
+
1822
+ // packages/ui/react-ui-canvas-compute/src/shapes/Table.tsx
1823
+ import React21 from "react";
1824
+ import { createInputSchema, createOutputSchema, GptMessage } from "@dxos/conductor";
1825
+ import { S as S20 } from "@dxos/echo-schema";
1826
+ var InputSchema = createInputSchema(GptMessage);
1827
+ var OutputSchema = createOutputSchema(S20.mutable(S20.Array(GptMessage)));
1828
+ var TableShape = S20.extend(ComputeShape, S20.Struct({
1829
+ type: S20.Literal("table")
1830
+ }));
1831
+ var createTable = (props) => createShape({
1832
+ type: "table",
1833
+ size: {
1834
+ width: 320,
1835
+ height: 512
1836
+ },
1837
+ ...props
1838
+ });
1839
+ var TableComponent = ({ shape }) => {
1840
+ return /* @__PURE__ */ React21.createElement(Box, {
1841
+ shape
1842
+ });
1843
+ };
1844
+ var tableShape = {
1845
+ type: "table",
1846
+ name: "Table",
1847
+ icon: "ph--table--regular",
1848
+ component: TableComponent,
1849
+ createShape: createTable,
1850
+ getAnchors: (shape) => createFunctionAnchors(shape, InputSchema, OutputSchema),
1851
+ resizable: true
1852
+ };
1853
+
1854
+ // packages/ui/react-ui-canvas-compute/src/shapes/Template.tsx
1855
+ import React22, { useRef as useRef4 } from "react";
1856
+ import { ComputeValueType as ComputeValueType3, getTemplateInputSchema as getTemplateInputSchema2, TemplateOutput, VoidInput as VoidInput2 } from "@dxos/conductor";
1857
+ import { S as S21, toJsonSchema as toJsonSchema2 } from "@dxos/echo-schema";
1858
+ import { invariant as invariant5 } from "@dxos/invariant";
1859
+ import { TextBox as TextBox3 } from "@dxos/react-ui-canvas-editor";
1860
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/shapes/Template.tsx";
1861
+ var TemplateShape = S21.extend(ComputeShape, S21.Struct({
1862
+ type: S21.Literal("template"),
1863
+ valueType: S21.optional(ComputeValueType3)
1864
+ }));
1865
+ var TextInputComponent2 = ({ shape, title, ...props }) => {
1866
+ const { node } = useComputeNodeState(shape);
1867
+ const inputRef = useRef4(null);
1868
+ const handleEnter = (text) => {
1869
+ const value = text.trim();
1870
+ if (value.length) {
1871
+ const schema = getTemplateInputSchema2(node);
1872
+ node.value = value;
1873
+ node.inputSchema = toJsonSchema2(schema);
1874
+ }
1875
+ };
1876
+ const handleTypeChange = (newType) => {
1877
+ invariant5(S21.is(ComputeValueType3)(newType), "Invalid type", {
1878
+ F: __dxlog_file6,
1879
+ L: 57,
1880
+ S: void 0,
1881
+ A: [
1882
+ "S.is(ComputeValueType)(newType)",
1883
+ "'Invalid type'"
1884
+ ]
1885
+ });
1886
+ node.valueType = newType;
1887
+ node.inputSchema = toJsonSchema2(getTemplateInputSchema2(node));
1888
+ };
1889
+ return /* @__PURE__ */ React22.createElement(Box, {
1890
+ shape,
1891
+ title: "Template",
1892
+ status: /* @__PURE__ */ React22.createElement(TypeSelect, {
1893
+ value: node.valueType ?? "string",
1894
+ onValueChange: handleTypeChange
1895
+ })
1896
+ }, /* @__PURE__ */ React22.createElement(TextBox3, {
1897
+ ...props,
1898
+ ref: inputRef,
1899
+ value: node.value,
1900
+ language: node.valueType === "object" ? "json" : void 0,
1901
+ onBlur: handleEnter,
1902
+ onEnter: handleEnter
1903
+ }));
1904
+ };
1905
+ var createTemplate = (props) => createShape({
1906
+ type: "template",
1907
+ size: {
1908
+ width: 256,
1909
+ height: 384
1910
+ },
1911
+ ...props
1912
+ });
1913
+ var templateShape = {
1914
+ type: "template",
1915
+ name: "Template",
1916
+ icon: "ph--article--regular",
1917
+ component: (props) => /* @__PURE__ */ React22.createElement(TextInputComponent2, {
1918
+ ...props,
1919
+ placeholder: "Prompt"
1920
+ }),
1921
+ createShape: createTemplate,
1922
+ getAnchors: (shape) => createFunctionAnchors(shape, VoidInput2, TemplateOutput),
1923
+ resizable: true
1924
+ };
1925
+
1926
+ // packages/ui/react-ui-canvas-compute/src/shapes/Text.tsx
1927
+ import React23 from "react";
1928
+ import { DEFAULT_INPUT as DEFAULT_INPUT7 } from "@dxos/conductor";
1929
+ import { S as S22 } from "@dxos/echo-schema";
1930
+ import { TextBox as TextBox4 } from "@dxos/react-ui-canvas-editor";
1931
+ import { createAnchorMap as createAnchorMap11 } from "@dxos/react-ui-canvas-editor";
1932
+ var TextShape = S22.extend(ComputeShape, S22.Struct({
1933
+ type: S22.Literal("text")
1934
+ }));
1935
+ var createText = (props) => createShape({
1936
+ type: "text",
1937
+ size: {
1938
+ width: 384,
1939
+ height: 384
1940
+ },
1941
+ ...props
1942
+ });
1943
+ var TextComponent = ({ shape }) => {
1944
+ const { runtime } = useComputeNodeState(shape);
1945
+ const input = runtime.inputs[DEFAULT_INPUT7];
1946
+ const value = input?.type === "executed" ? input.value : 0;
1947
+ const handleAction = (action) => {
1948
+ if (action === "run") {
1949
+ runtime.evalNode();
1950
+ }
1951
+ };
1952
+ return /* @__PURE__ */ React23.createElement(Box, {
1953
+ shape,
1954
+ onAction: handleAction
1955
+ }, /* @__PURE__ */ React23.createElement(TextBox4, {
1956
+ value
1957
+ }));
1958
+ };
1959
+ var textShape = {
1960
+ type: "text",
1961
+ name: "Text",
1962
+ icon: "ph--article--regular",
1963
+ component: TextComponent,
1964
+ createShape: createText,
1965
+ getAnchors: (shape) => createAnchorMap11(shape, {
1966
+ [createAnchorId("input")]: {
1967
+ x: -1,
1968
+ y: 0
1969
+ }
1970
+ }),
1971
+ resizable: true
1972
+ };
1973
+
1974
+ // packages/ui/react-ui-canvas-compute/src/shapes/Thread.tsx
1975
+ import React24, { useEffect as useEffect6, useRef as useRef5 } from "react";
1976
+ import { createInputSchema as createInputSchema2, createOutputSchema as createOutputSchema2, GptMessage as GptMessage2 } from "@dxos/conductor";
1977
+ import { S as S23 } from "@dxos/echo-schema";
1978
+ import { mx as mx5 } from "@dxos/react-ui-theme";
1979
+ var InputSchema2 = createInputSchema2(GptMessage2);
1980
+ var OutputSchema2 = createOutputSchema2(S23.mutable(S23.Array(GptMessage2)));
1981
+ var ThreadShape = S23.extend(ComputeShape, S23.Struct({
1982
+ type: S23.Literal("thread")
1983
+ }));
1984
+ var createThread = (props) => createShape({
1985
+ type: "thread",
1986
+ size: {
1987
+ width: 384,
1988
+ height: 384
1989
+ },
1990
+ ...props
1991
+ });
1992
+ var ThreadComponent = ({ shape }) => {
1993
+ const items = [];
1994
+ const scrollRef = useRef5(null);
1995
+ useEffect6(() => {
1996
+ if (scrollRef.current) {
1997
+ scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
1998
+ }
1999
+ }, [
2000
+ items
2001
+ ]);
2002
+ return /* @__PURE__ */ React24.createElement(Box, {
2003
+ shape
2004
+ }, /* @__PURE__ */ React24.createElement("div", {
2005
+ ref: scrollRef,
2006
+ className: "flex flex-col w-full overflow-y-scroll gap-2 p-2"
2007
+ }, [
2008
+ ...items
2009
+ ].map((item, i) => /* @__PURE__ */ React24.createElement(ThreadItem, {
2010
+ key: i,
2011
+ item
2012
+ }))));
2013
+ };
2014
+ var ThreadItem = ({ classNames, item }) => {
2015
+ if (typeof item !== "object") {
2016
+ return /* @__PURE__ */ React24.createElement("div", {
2017
+ className: mx5(classNames)
2018
+ }, item);
2019
+ }
2020
+ const { role, message } = item;
2021
+ return /* @__PURE__ */ React24.createElement("div", {
2022
+ className: mx5("flex", classNames, role === "user" && "justify-end")
2023
+ }, /* @__PURE__ */ React24.createElement("div", {
2024
+ className: mx5("block rounded-md p-1 px-2 text-sm", role === "user" ? "bg-blue-100 dark:bg-blue-800" : role === "system" ? "bg-red-100, dark:bg-red-800" : "whitespace-pre-wrap bg-neutral-50 dark:bg-neutral-800")
2025
+ }, message));
2026
+ };
2027
+ var threadShape = {
2028
+ type: "thread",
2029
+ name: "Thread",
2030
+ icon: "ph--chats-circle--regular",
2031
+ component: ThreadComponent,
2032
+ createShape: createThread,
2033
+ getAnchors: (shape) => createFunctionAnchors(shape, InputSchema2, OutputSchema2),
2034
+ resizable: true
2035
+ };
2036
+
2037
+ // packages/ui/react-ui-canvas-compute/src/shapes/TextToImage.tsx
2038
+ import React25 from "react";
2039
+ import { S as S24 } from "@dxos/echo-schema";
2040
+ import { createAnchorMap as createAnchorMap12 } from "@dxos/react-ui-canvas-editor";
2041
+ var TextToImageShape = S24.extend(ComputeShape, S24.Struct({
2042
+ type: S24.Literal("text-to-image")
2043
+ }));
2044
+ var createTextToImage = (props) => createShape({
2045
+ type: "text-to-image",
2046
+ size: {
2047
+ width: 128,
2048
+ height: 64
2049
+ },
2050
+ ...props
2051
+ });
2052
+ var TextToImageComponent = ({ shape }) => {
2053
+ return /* @__PURE__ */ React25.createElement(Box, {
2054
+ shape
2055
+ });
2056
+ };
2057
+ var textToImageShape = {
2058
+ type: "text-to-image",
2059
+ name: "Image",
2060
+ icon: "ph--image--regular",
2061
+ component: TextToImageComponent,
2062
+ createShape: createTextToImage,
2063
+ getAnchors: (shape) => createAnchorMap12(shape, {
2064
+ [createAnchorId("output")]: {
2065
+ x: 1,
2066
+ y: 0
2067
+ }
2068
+ })
2069
+ };
2070
+
2071
+ // packages/ui/react-ui-canvas-compute/src/shapes/Trigger.tsx
2072
+ import React26, { useEffect as useEffect7 } from "react";
2073
+ import { EmailTriggerOutput, SubscriptionTriggerOutput, TimerTriggerOutput, VoidInput as VoidInput3, WebhookTriggerOutput } from "@dxos/conductor";
2074
+ import { Ref, S as S25 } from "@dxos/echo-schema";
2075
+ import { FunctionTrigger, TriggerKind } from "@dxos/functions";
2076
+ import { create, makeRef } from "@dxos/react-client/echo";
2077
+ import { Select as Select2 } from "@dxos/react-ui";
2078
+ var TriggerShape = S25.extend(ComputeShape, S25.Struct({
2079
+ type: S25.Literal("trigger"),
2080
+ functionTrigger: S25.optional(Ref(FunctionTrigger))
2081
+ }));
2082
+ var createTrigger = (props) => {
2083
+ const functionTrigger = create(FunctionTrigger, {
2084
+ enabled: true,
2085
+ spec: createTriggerSpec(props.triggerKind ?? TriggerKind.Email)
2086
+ });
2087
+ return createShape({
2088
+ type: "trigger",
2089
+ functionTrigger: makeRef(functionTrigger),
2090
+ size: {
2091
+ width: 192,
2092
+ height: getHeight(EmailTriggerOutput)
2093
+ },
2094
+ ...props
2095
+ });
2096
+ };
2097
+ var TriggerComponent = ({ shape }) => {
2098
+ const functionTrigger = shape.functionTrigger?.target;
2099
+ useEffect7(() => {
2100
+ if (functionTrigger && !functionTrigger.spec) {
2101
+ functionTrigger.spec = createTriggerSpec(TriggerKind.Email);
2102
+ }
2103
+ }, [
2104
+ functionTrigger,
2105
+ functionTrigger?.spec
2106
+ ]);
2107
+ useEffect7(() => {
2108
+ shape.size.height = getHeight(getOutputSchema(functionTrigger?.spec?.type ?? TriggerKind.Email));
2109
+ }, [
2110
+ functionTrigger?.spec?.type
2111
+ ]);
2112
+ const setKind = (kind) => {
2113
+ if (functionTrigger?.spec?.type !== kind) {
2114
+ functionTrigger.spec = createTriggerSpec(kind);
2115
+ }
2116
+ };
2117
+ if (!functionTrigger?.spec) {
2118
+ return;
2119
+ }
2120
+ return /* @__PURE__ */ React26.createElement(FunctionBody, {
2121
+ shape,
2122
+ status: /* @__PURE__ */ React26.createElement(TriggerKindSelect, {
2123
+ value: functionTrigger.spec?.type,
2124
+ onValueChange: (kind) => setKind(kind)
2125
+ }),
2126
+ inputSchema: VoidInput3,
2127
+ outputSchema: getOutputSchema(functionTrigger.spec.type)
2128
+ });
2129
+ };
2130
+ var TriggerKindSelect = ({ value, onValueChange }) => {
2131
+ return /* @__PURE__ */ React26.createElement(Select2.Root, {
2132
+ value,
2133
+ onValueChange
2134
+ }, /* @__PURE__ */ React26.createElement(Select2.TriggerButton, {
2135
+ variant: "ghost",
2136
+ classNames: "w-full !px-0"
2137
+ }), /* @__PURE__ */ React26.createElement(Select2.Portal, null, /* @__PURE__ */ React26.createElement(Select2.Content, null, /* @__PURE__ */ React26.createElement(Select2.ScrollUpButton, null), /* @__PURE__ */ React26.createElement(Select2.Viewport, null, Object.values(TriggerKind).map((type) => /* @__PURE__ */ React26.createElement(Select2.Option, {
2138
+ key: type,
2139
+ value: type
2140
+ }, type))), /* @__PURE__ */ React26.createElement(Select2.ScrollDownButton, null), /* @__PURE__ */ React26.createElement(Select2.Arrow, null))));
2141
+ };
2142
+ var createTriggerSpec = (kind) => {
2143
+ switch (kind) {
2144
+ case TriggerKind.Timer:
2145
+ return {
2146
+ type: TriggerKind.Timer,
2147
+ cron: "0 0 * * *"
2148
+ };
2149
+ case TriggerKind.Webhook:
2150
+ return {
2151
+ type: TriggerKind.Webhook,
2152
+ method: "POST"
2153
+ };
2154
+ case TriggerKind.Subscription:
2155
+ return {
2156
+ type: TriggerKind.Subscription,
2157
+ filter: {}
2158
+ };
2159
+ case TriggerKind.Email:
2160
+ return {
2161
+ type: TriggerKind.Email
2162
+ };
2163
+ }
2164
+ };
2165
+ var getOutputSchema = (kind) => {
2166
+ const kindToSchema = {
2167
+ [TriggerKind.Email]: EmailTriggerOutput,
2168
+ [TriggerKind.Subscription]: SubscriptionTriggerOutput,
2169
+ [TriggerKind.Timer]: TimerTriggerOutput,
2170
+ [TriggerKind.Webhook]: WebhookTriggerOutput
2171
+ };
2172
+ return kindToSchema[kind];
2173
+ };
2174
+ var triggerShape = {
2175
+ type: "trigger",
2176
+ name: "Trigger",
2177
+ icon: "ph--lightning--regular",
2178
+ component: TriggerComponent,
2179
+ createShape: createTrigger,
2180
+ getAnchors: (shape) => createFunctionAnchors(shape, VoidInput3, getOutputSchema(shape.functionTrigger?.target?.spec?.type ?? TriggerKind.Email))
2181
+ };
2182
+
2183
+ // packages/ui/react-ui-canvas-compute/src/shapes/GptRealtime.tsx
2184
+ import React27, { useState as useState8 } from "react";
2185
+ import { S as S26 } from "@dxos/echo-schema";
2186
+ import { log as log2 } from "@dxos/log";
2187
+ import { useConfig } from "@dxos/react-client";
2188
+ import { Icon as Icon5 } from "@dxos/react-ui";
2189
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-canvas-compute/src/shapes/GptRealtime.tsx";
2190
+ var GptRealtimeShape = S26.extend(ComputeShape, S26.Struct({
2191
+ type: S26.Literal("gpt-realtime")
2192
+ }));
2193
+ var createGptRealtime = (props) => createShape({
2194
+ type: "gpt-realtime",
2195
+ size: {
2196
+ width: 256,
2197
+ height: 256
2198
+ },
2199
+ ...props
2200
+ });
2201
+ var GptRealtimeComponent = ({ shape }) => {
2202
+ const [isLive, setIsLive] = useState8(false);
2203
+ const [isReady, setIsReady] = useState8(false);
2204
+ const config = useConfig();
2205
+ const start = async () => {
2206
+ setIsLive(true);
2207
+ try {
2208
+ const peerConnection = new RTCPeerConnection();
2209
+ peerConnection.ontrack = (event) => {
2210
+ const audioElement = document.createElement("audio");
2211
+ audioElement.srcObject = event.streams[0];
2212
+ audioElement.autoplay = true;
2213
+ audioElement.controls = false;
2214
+ audioElement.style.display = "none";
2215
+ document.body.appendChild(audioElement);
2216
+ setIsReady(true);
2217
+ };
2218
+ const stream = await navigator.mediaDevices.getUserMedia({
2219
+ audio: true
2220
+ });
2221
+ stream.getTracks().forEach((track) => peerConnection.addTransceiver(track, {
2222
+ direction: "sendrecv"
2223
+ }));
2224
+ const offer = await peerConnection.createOffer();
2225
+ await peerConnection.setLocalDescription(offer);
2226
+ const aiServiceUrl = new URL("/rtc-connect", config.values.runtime?.services?.ai?.server ?? DEFAULT_AI_SERVICE_URL);
2227
+ const response = await fetch(aiServiceUrl, {
2228
+ method: "POST",
2229
+ body: offer.sdp,
2230
+ headers: {
2231
+ "Content-Type": "application/sdp"
2232
+ }
2233
+ });
2234
+ const answer = await response.text();
2235
+ await peerConnection.setRemoteDescription({
2236
+ sdp: answer,
2237
+ type: "answer"
2238
+ });
2239
+ const dataChannel = peerConnection.createDataChannel("response");
2240
+ const configureData = () => {
2241
+ log2.info("Configuring data channel", void 0, {
2242
+ F: __dxlog_file7,
2243
+ L: 87,
2244
+ S: void 0,
2245
+ C: (f, a) => f(...a)
2246
+ });
2247
+ const event = {
2248
+ type: "session.update",
2249
+ session: {
2250
+ modalities: [
2251
+ "text",
2252
+ "audio"
2253
+ ],
2254
+ // Provide the tools. Note they match the keys in the `fns` object above
2255
+ tools: []
2256
+ }
2257
+ };
2258
+ dataChannel.send(JSON.stringify(event));
2259
+ };
2260
+ dataChannel.addEventListener("open", (ev) => {
2261
+ log2.info("Opening data channel", {
2262
+ ev
2263
+ }, {
2264
+ F: __dxlog_file7,
2265
+ L: 100,
2266
+ S: void 0,
2267
+ C: (f, a) => f(...a)
2268
+ });
2269
+ configureData();
2270
+ });
2271
+ dataChannel.addEventListener("message", async (ev) => {
2272
+ const msg = JSON.parse(ev.data);
2273
+ if (msg.type === "response.function_call_arguments.done") {
2274
+ }
2275
+ });
2276
+ } catch (error) {
2277
+ log2.error("Error in realtime session:", {
2278
+ error
2279
+ }, {
2280
+ F: __dxlog_file7,
2281
+ L: 140,
2282
+ S: void 0,
2283
+ C: (f, a) => f(...a)
2284
+ });
2285
+ throw error;
2286
+ }
2287
+ };
2288
+ return /* @__PURE__ */ React27.createElement("div", {
2289
+ className: "flex w-full justify-center items-center"
2290
+ }, /* @__PURE__ */ React27.createElement(Icon5, {
2291
+ icon: isReady ? "ph--waveform--regular" : isLive ? "ph--pulse--regular" : "ph--play--regular",
2292
+ size: 16,
2293
+ classNames: !isLive && "cursor-pointer",
2294
+ onClick: start
2295
+ }));
2296
+ };
2297
+ var gptRealtimeShape = {
2298
+ type: "gpt-realtime",
2299
+ name: "GPT Realtime",
2300
+ icon: "ph--pulse--regular",
2301
+ component: GptRealtimeComponent,
2302
+ createShape: createGptRealtime,
2303
+ // TODO(dmaretskyi): Can we fetch the schema dynamically?
2304
+ getAnchors: (shape) => createFunctionAnchors(shape, S26.Struct({
2305
+ audio: S26.Any
2306
+ }), S26.Struct({})),
2307
+ resizable: true
2308
+ };
2309
+ var DEFAULT_AI_SERVICE_URL = "http://localhost:8788";
2310
+
2311
+ // packages/ui/react-ui-canvas-compute/src/registry.ts
2312
+ var computeShapes = [
2313
+ {
2314
+ title: "Inputs",
2315
+ shapes: [
2316
+ //
2317
+ constantShape,
2318
+ templateShape,
2319
+ chatShape,
2320
+ switchShape,
2321
+ audioShape,
2322
+ triggerShape,
2323
+ randomShape
2324
+ ]
2325
+ },
2326
+ {
2327
+ title: "Transform",
2328
+ shapes: [
2329
+ //
2330
+ gptShape,
2331
+ gptRealtimeShape,
2332
+ functionShape,
2333
+ databaseShape,
2334
+ textToImageShape,
2335
+ appendShape
2336
+ ]
2337
+ },
2338
+ {
2339
+ title: "Operations",
2340
+ shapes: [
2341
+ //
2342
+ ifShape,
2343
+ ifElseShape,
2344
+ andShape,
2345
+ orShape,
2346
+ notShape,
2347
+ reducerShape,
2348
+ jsonTransformShape
2349
+ ]
2350
+ },
2351
+ {
2352
+ title: "Outputs",
2353
+ shapes: [
2354
+ //
2355
+ jsonShape,
2356
+ queueShape,
2357
+ threadShape,
2358
+ textShape,
2359
+ surfaceShape,
2360
+ beaconShape,
2361
+ scopeShape
2362
+ ]
2363
+ },
2364
+ {
2365
+ title: "Misc",
2366
+ shapes: [
2367
+ //
2368
+ noteShape
2369
+ ]
2370
+ }
2371
+ ];
2372
+
2373
+ // packages/ui/react-ui-canvas-compute/src/compute-layout.ts
2374
+ import { DefaultInput, DefaultOutput as DefaultOutput2 } from "@dxos/conductor";
2375
+ import { toEffectSchema } from "@dxos/echo-schema";
2376
+ import { ShapeLayout } from "@dxos/react-ui-canvas-editor";
2377
+ var ComputeShapeLayout = class extends ShapeLayout {
2378
+ constructor(_controller, registry2) {
2379
+ super(registry2);
2380
+ this._controller = _controller;
2381
+ }
2382
+ // TODO(burdon): Doesn't update.
2383
+ getAnchors(shape) {
2384
+ const shapeDef = this._registry.getShapeDef(shape.type);
2385
+ let anchors = shapeDef?.getAnchors?.(shape) ?? {};
2386
+ if (shape.node) {
2387
+ const node = this._controller.graph.getNode(shape.node);
2388
+ if (node.inputSchema || node.outputSchema) {
2389
+ const inputSchema = node.inputSchema ? toEffectSchema(node.inputSchema) : DefaultInput;
2390
+ const outputSchema = node.outputSchema ? toEffectSchema(node.outputSchema) : DefaultOutput2;
2391
+ anchors = createFunctionAnchors(shape, inputSchema, outputSchema);
2392
+ }
2393
+ }
2394
+ return anchors;
2395
+ }
2396
+ };
2397
+ export {
2398
+ AndShape,
2399
+ AppendComponent,
2400
+ AppendShape,
2401
+ AudioComponent,
2402
+ AudioShape,
2403
+ BeaconComponent,
2404
+ BeaconShape,
2405
+ Box,
2406
+ ChatShape,
2407
+ ComputeContext,
2408
+ ComputeGraphController,
2409
+ ComputeShape,
2410
+ ComputeShapeLayout,
2411
+ ConstantComponent,
2412
+ ConstantShape,
2413
+ DatabaseComponent,
2414
+ DatabaseShape,
2415
+ FunctionBody,
2416
+ FunctionComponent,
2417
+ FunctionShape,
2418
+ GptComponent,
2419
+ GptRealtimeComponent,
2420
+ GptRealtimeShape,
2421
+ GptShape,
2422
+ IfComponent,
2423
+ IfElseComponent,
2424
+ IfElseShape,
2425
+ IfShape,
2426
+ InvalidStateError,
2427
+ JsonComponent,
2428
+ JsonShape,
2429
+ JsonTransformComponent,
2430
+ JsonTransformShape,
2431
+ NotShape,
2432
+ OrShape,
2433
+ QueueComponent,
2434
+ QueueItem,
2435
+ QueueShape,
2436
+ RandomComponent,
2437
+ RandomShape,
2438
+ ReducerComponent,
2439
+ ReducerShape,
2440
+ ScopeComponent,
2441
+ ScopeShape,
2442
+ SurfaceComponent,
2443
+ SurfaceShape,
2444
+ SwitchComponent,
2445
+ SwitchShape,
2446
+ TableComponent,
2447
+ TableShape,
2448
+ TemplateShape,
2449
+ TextComponent,
2450
+ TextInputComponent,
2451
+ TextShape,
2452
+ TextToImageComponent,
2453
+ TextToImageShape,
2454
+ ThreadComponent,
2455
+ ThreadItem,
2456
+ ThreadShape,
2457
+ TriggerComponent,
2458
+ TriggerShape,
2459
+ TypeSelect,
2460
+ andShape,
2461
+ appendShape,
2462
+ audioShape,
2463
+ beaconShape,
2464
+ chatShape,
2465
+ computeShapes,
2466
+ constantShape,
2467
+ createAnchorId,
2468
+ createAnd,
2469
+ createAppend,
2470
+ createAudio,
2471
+ createBeacon,
2472
+ createChat,
2473
+ createComputeGraph,
2474
+ createComputeGraphController,
2475
+ createComputeNode,
2476
+ createConstant,
2477
+ createDatabase,
2478
+ createFunction,
2479
+ createFunctionAnchors,
2480
+ createGpt,
2481
+ createGptRealtime,
2482
+ createIf,
2483
+ createIfElse,
2484
+ createJson,
2485
+ createJsonTransform,
2486
+ createNot,
2487
+ createOr,
2488
+ createQueue,
2489
+ createRandom,
2490
+ createReducer,
2491
+ createScope,
2492
+ createShape,
2493
+ createSurface,
2494
+ createSwitch,
2495
+ createTable,
2496
+ createTemplate,
2497
+ createText,
2498
+ createTextToImage,
2499
+ createThread,
2500
+ createTrigger,
2501
+ databaseShape,
2502
+ footerHeight,
2503
+ functionShape,
2504
+ getHeight,
2505
+ getProperties,
2506
+ gptRealtimeShape,
2507
+ gptShape,
2508
+ headerHeight,
2509
+ ifElseShape,
2510
+ ifShape,
2511
+ isValidComputeNode,
2512
+ jsonShape,
2513
+ jsonTransformShape,
2514
+ mapEdge,
2515
+ notShape,
2516
+ orShape,
2517
+ parseAnchorId,
2518
+ queueShape,
2519
+ randomShape,
2520
+ reducerShape,
2521
+ resolveComputeNode,
2522
+ scopeShape,
2523
+ surfaceShape,
2524
+ switchShape,
2525
+ tableShape,
2526
+ templateShape,
2527
+ textShape,
2528
+ textToImageShape,
2529
+ threadShape,
2530
+ triggerShape,
2531
+ useComputeContext,
2532
+ useComputeNodeState,
2533
+ useGraphMonitor
2534
+ };
2535
+ //# sourceMappingURL=index.mjs.map