@dxos/react-ui-canvas-compute 0.7.5-labs.ff2ff30 → 0.7.5-staging.2ff1350

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 (67) hide show
  1. package/dist/lib/browser/index.mjs +102 -39
  2. package/dist/lib/browser/index.mjs.map +3 -3
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +131 -69
  5. package/dist/lib/node/index.cjs.map +3 -3
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +102 -39
  8. package/dist/lib/node-esm/index.mjs.map +3 -3
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/components/DiagnosticOverlay.d.ts +1 -2
  11. package/dist/types/src/components/DiagnosticOverlay.d.ts.map +1 -1
  12. package/dist/types/src/graph/controller.d.ts +16 -0
  13. package/dist/types/src/graph/controller.d.ts.map +1 -1
  14. package/dist/types/src/shapes/Append.d.ts +1 -2
  15. package/dist/types/src/shapes/Append.d.ts.map +1 -1
  16. package/dist/types/src/shapes/Array.d.ts +1 -2
  17. package/dist/types/src/shapes/Array.d.ts.map +1 -1
  18. package/dist/types/src/shapes/Audio.d.ts +1 -2
  19. package/dist/types/src/shapes/Audio.d.ts.map +1 -1
  20. package/dist/types/src/shapes/Beacon.d.ts +1 -2
  21. package/dist/types/src/shapes/Beacon.d.ts.map +1 -1
  22. package/dist/types/src/shapes/Chat.d.ts +1 -2
  23. package/dist/types/src/shapes/Chat.d.ts.map +1 -1
  24. package/dist/types/src/shapes/Constant.d.ts +1 -2
  25. package/dist/types/src/shapes/Constant.d.ts.map +1 -1
  26. package/dist/types/src/shapes/Database.d.ts +1 -2
  27. package/dist/types/src/shapes/Database.d.ts.map +1 -1
  28. package/dist/types/src/shapes/Function.d.ts +1 -3
  29. package/dist/types/src/shapes/Function.d.ts.map +1 -1
  30. package/dist/types/src/shapes/Gpt.d.ts +1 -2
  31. package/dist/types/src/shapes/Gpt.d.ts.map +1 -1
  32. package/dist/types/src/shapes/GptRealtime.d.ts +1 -2
  33. package/dist/types/src/shapes/GptRealtime.d.ts.map +1 -1
  34. package/dist/types/src/shapes/Json.d.ts +2 -3
  35. package/dist/types/src/shapes/Json.d.ts.map +1 -1
  36. package/dist/types/src/shapes/Logic.d.ts +2 -3
  37. package/dist/types/src/shapes/Logic.d.ts.map +1 -1
  38. package/dist/types/src/shapes/Queue.d.ts +2 -3
  39. package/dist/types/src/shapes/Queue.d.ts.map +1 -1
  40. package/dist/types/src/shapes/RNG.d.ts +1 -2
  41. package/dist/types/src/shapes/RNG.d.ts.map +1 -1
  42. package/dist/types/src/shapes/Scope.d.ts +1 -2
  43. package/dist/types/src/shapes/Scope.d.ts.map +1 -1
  44. package/dist/types/src/shapes/Surface.d.ts +1 -2
  45. package/dist/types/src/shapes/Surface.d.ts.map +1 -1
  46. package/dist/types/src/shapes/Switch.d.ts +1 -2
  47. package/dist/types/src/shapes/Switch.d.ts.map +1 -1
  48. package/dist/types/src/shapes/Table.d.ts +1 -2
  49. package/dist/types/src/shapes/Table.d.ts.map +1 -1
  50. package/dist/types/src/shapes/Text.d.ts +1 -2
  51. package/dist/types/src/shapes/Text.d.ts.map +1 -1
  52. package/dist/types/src/shapes/TextToImage.d.ts +1 -2
  53. package/dist/types/src/shapes/TextToImage.d.ts.map +1 -1
  54. package/dist/types/src/shapes/Thread.d.ts +2 -3
  55. package/dist/types/src/shapes/Thread.d.ts.map +1 -1
  56. package/dist/types/src/shapes/Trigger.d.ts +3 -2
  57. package/dist/types/src/shapes/Trigger.d.ts.map +1 -1
  58. package/dist/types/src/shapes/common/FunctionBody.d.ts +1 -1
  59. package/dist/types/src/shapes/common/FunctionBody.d.ts.map +1 -1
  60. package/dist/types/src/shapes/common/TypeSelect.d.ts +1 -2
  61. package/dist/types/src/shapes/common/TypeSelect.d.ts.map +1 -1
  62. package/package.json +41 -40
  63. package/src/graph/controller.ts +14 -3
  64. package/src/hooks/useGraphMonitor.ts +2 -2
  65. package/src/shapes/Function.tsx +83 -9
  66. package/src/shapes/Trigger.tsx +11 -8
  67. package/src/shapes/common/FunctionBody.tsx +1 -0
@@ -2,14 +2,24 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React from 'react';
5
+ import React, { useCallback, useRef } from 'react';
6
6
 
7
7
  import { AnyOutput, FunctionInput } from '@dxos/conductor';
8
- import { S } from '@dxos/echo-schema';
9
- import { type ShapeComponentProps, type ShapeDef } from '@dxos/react-ui-canvas-editor';
8
+ import { getSnapshot, S } from '@dxos/echo-schema';
9
+ import { FunctionType, ScriptType } from '@dxos/functions';
10
+ import { useClient } from '@dxos/react-client';
11
+ import { Filter, makeRef, parseId } from '@dxos/react-client/echo';
12
+ import {
13
+ TextBox,
14
+ type TextBoxControl,
15
+ type TextBoxProps,
16
+ type ShapeComponentProps,
17
+ type ShapeDef,
18
+ } from '@dxos/react-ui-canvas-editor';
10
19
 
11
- import { createFunctionAnchors, FunctionBody, getHeight } from './common';
20
+ import { Box, createFunctionAnchors } from './common';
12
21
  import { ComputeShape, createShape, type CreateShapeProps } from './defs';
22
+ import { useComputeNodeState } from '../hooks';
13
23
 
14
24
  export const FunctionShape = S.extend(
15
25
  ComputeShape,
@@ -23,18 +33,82 @@ export type FunctionShape = S.Schema.Type<typeof FunctionShape>;
23
33
  export type CreateFunctionProps = CreateShapeProps<FunctionShape>;
24
34
 
25
35
  export const createFunction = (props: CreateFunctionProps) =>
26
- createShape<FunctionShape>({ type: 'function', size: { width: 192, height: getHeight(FunctionInput) }, ...props });
36
+ createShape<FunctionShape>({ type: 'function', size: { width: 256, height: 192 }, ...props });
27
37
 
28
- export const FunctionComponent = ({ shape }: ShapeComponentProps<FunctionShape>) => {
29
- return <FunctionBody shape={shape} inputSchema={FunctionInput} outputSchema={AnyOutput} />;
38
+ //
39
+ // Component
40
+ //
41
+
42
+ type TextInputComponentProps = ShapeComponentProps<FunctionShape> & TextBoxProps & { title?: string };
43
+
44
+ const TextInputComponent = ({ shape, title, ...props }: TextInputComponentProps) => {
45
+ const client = useClient();
46
+ const { node, runtime } = useComputeNodeState(shape);
47
+ const inputRef = useRef<TextBoxControl>(null);
48
+
49
+ const handleEnter = useCallback(
50
+ async (text: string) => {
51
+ const value = text.trim();
52
+ const { spaceId, objectId } = parseId(value);
53
+ if (!spaceId || !objectId) {
54
+ return;
55
+ }
56
+
57
+ const space = client.spaces.get(spaceId);
58
+ const object = space?.db.getObjectById(objectId);
59
+ if (!space || !(object instanceof ScriptType)) {
60
+ return;
61
+ }
62
+
63
+ const {
64
+ objects: [fn],
65
+ } = await space.db.query(Filter.schema(FunctionType, { source: object })).run();
66
+ if (!fn) {
67
+ return;
68
+ }
69
+
70
+ node.value = value;
71
+ node.function = makeRef(fn);
72
+ node.inputSchema = getSnapshot(fn.inputSchema);
73
+ node.outputSchema = getSnapshot(fn.outputSchema);
74
+ },
75
+ [client, node],
76
+ );
77
+
78
+ const handleAction = useCallback(
79
+ (action: 'run' | 'open' | 'close') => {
80
+ if (action !== 'run') {
81
+ return;
82
+ }
83
+
84
+ runtime.evalNode();
85
+ },
86
+ [runtime],
87
+ );
88
+
89
+ return (
90
+ <Box shape={shape} title='Function' onAction={handleAction}>
91
+ <TextBox
92
+ {...props}
93
+ ref={inputRef}
94
+ value={node.value}
95
+ language={node.valueType === 'object' ? 'json' : undefined}
96
+ onBlur={handleEnter}
97
+ onEnter={handleEnter}
98
+ />
99
+ </Box>
100
+ );
30
101
  };
31
102
 
103
+ //
104
+ // Defs
105
+ //
106
+
32
107
  export const functionShape: ShapeDef<FunctionShape> = {
33
108
  type: 'function',
34
109
  name: 'Function',
35
110
  icon: 'ph--function--regular',
36
- component: FunctionComponent,
111
+ component: TextInputComponent,
37
112
  createShape: createFunction,
38
- // TODO(burdon): Get dynamic schema.
39
113
  getAnchors: (shape) => createFunctionAnchors(shape, FunctionInput, AnyOutput),
40
114
  };
@@ -6,11 +6,11 @@ import React, { useEffect } from 'react';
6
6
 
7
7
  import {
8
8
  EmailTriggerOutput,
9
+ QueueTriggerOutput,
9
10
  SubscriptionTriggerOutput,
10
11
  TimerTriggerOutput,
11
12
  VoidInput,
12
13
  WebhookTriggerOutput,
13
- QueueTriggerOutput,
14
14
  } from '@dxos/conductor';
15
15
  import { ObjectId, Ref, S } from '@dxos/echo-schema';
16
16
  import {
@@ -24,7 +24,7 @@ import {
24
24
  type WebhookTrigger,
25
25
  } from '@dxos/functions';
26
26
  import { DXN, SpaceId } from '@dxos/keys';
27
- import { create, makeRef } from '@dxos/react-client/echo';
27
+ import { create, makeRef, useSpace } from '@dxos/react-client/echo';
28
28
  import { Select, type SelectRootProps } from '@dxos/react-ui';
29
29
  import { type ShapeComponentProps, type ShapeDef } from '@dxos/react-ui-canvas-editor';
30
30
 
@@ -41,13 +41,14 @@ export const TriggerShape = S.extend(
41
41
  export type TriggerShape = S.Schema.Type<typeof TriggerShape>;
42
42
 
43
43
  export type CreateTriggerProps = CreateShapeProps<Omit<TriggerShape, 'functionTrigger'>> & {
44
+ spaceId?: SpaceId;
44
45
  triggerKind?: TriggerKind;
45
46
  };
46
47
 
47
48
  export const createTrigger = (props: CreateTriggerProps): TriggerShape => {
48
49
  const functionTrigger = create(FunctionTrigger, {
49
50
  enabled: true,
50
- spec: createTriggerSpec(props.triggerKind ?? TriggerKind.Email),
51
+ spec: createTriggerSpec(props),
51
52
  });
52
53
  return createShape<TriggerShape>({
53
54
  type: 'trigger',
@@ -60,11 +61,12 @@ export const createTrigger = (props: CreateTriggerProps): TriggerShape => {
60
61
  export type TriggerComponentProps = ShapeComponentProps<TriggerShape>;
61
62
 
62
63
  export const TriggerComponent = ({ shape }: TriggerComponentProps) => {
64
+ const space = useSpace();
63
65
  const functionTrigger = shape.functionTrigger?.target;
64
66
 
65
67
  useEffect(() => {
66
68
  if (functionTrigger && !functionTrigger.spec) {
67
- functionTrigger.spec = createTriggerSpec(TriggerKind.Email);
69
+ functionTrigger.spec = createTriggerSpec({ triggerKind: TriggerKind.Email, spaceId: space?.id });
68
70
  }
69
71
  }, [functionTrigger, functionTrigger?.spec]);
70
72
 
@@ -74,7 +76,7 @@ export const TriggerComponent = ({ shape }: TriggerComponentProps) => {
74
76
 
75
77
  const setKind = (kind: TriggerKind) => {
76
78
  if (functionTrigger?.spec?.type !== kind) {
77
- functionTrigger!.spec = createTriggerSpec(kind);
79
+ functionTrigger!.spec = createTriggerSpec({ triggerKind: TriggerKind.Email, spaceId: space?.id });
78
80
  }
79
81
  };
80
82
 
@@ -117,10 +119,11 @@ const TriggerKindSelect = ({ value, onValueChange }: Pick<SelectRootProps, 'valu
117
119
  );
118
120
  };
119
121
 
120
- const createTriggerSpec = (kind: TriggerKind): TriggerType => {
122
+ const createTriggerSpec = (props: { triggerKind?: TriggerKind; spaceId?: SpaceId }): TriggerType => {
123
+ const kind = props.triggerKind ?? TriggerKind.Email;
121
124
  switch (kind) {
122
125
  case TriggerKind.Timer:
123
- return { type: TriggerKind.Timer, cron: '0 0 * * *' } satisfies TimerTrigger;
126
+ return { type: TriggerKind.Timer, cron: '*/10 * * * * *' } satisfies TimerTrigger;
124
127
  case TriggerKind.Webhook:
125
128
  return { type: TriggerKind.Webhook, method: 'POST' } satisfies WebhookTrigger;
126
129
  case TriggerKind.Subscription:
@@ -128,7 +131,7 @@ const createTriggerSpec = (kind: TriggerKind): TriggerType => {
128
131
  case TriggerKind.Email:
129
132
  return { type: TriggerKind.Email } satisfies EmailTrigger;
130
133
  case TriggerKind.Queue: {
131
- const dxn = new DXN(DXN.kind.QUEUE, ['data', SpaceId.random(), ObjectId.random()]).toString();
134
+ const dxn = new DXN(DXN.kind.QUEUE, ['data', props.spaceId ?? SpaceId.random(), ObjectId.random()]).toString();
132
135
  return { type: TriggerKind.Queue, queue: dxn } satisfies QueueTrigger;
133
136
  }
134
137
  }
@@ -24,6 +24,7 @@ export type FunctionBodyProps = {
24
24
  outputSchema?: S.Schema.Any;
25
25
  } & Pick<BoxProps, 'status'>;
26
26
 
27
+ // TODO(wittjosiah): Rename, not used for functions.
27
28
  export const FunctionBody = ({
28
29
  shape,
29
30
  name,