@dxos/plugin-script 0.6.12-main.5cc132e → 0.6.12-main.c974201

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 (184) hide show
  1. package/dist/lib/browser/{ScriptEditor-5Y3EROG3.mjs → ScriptContainer-7DE7USCY.mjs} +257 -106
  2. package/dist/lib/browser/ScriptContainer-7DE7USCY.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-O4LWBX6A.mjs → chunk-CAEANETO.mjs} +2 -2
  4. package/dist/lib/browser/{chunk-O4LWBX6A.mjs.map → chunk-CAEANETO.mjs.map} +2 -2
  5. package/dist/lib/browser/{chunk-DL5XQRQM.mjs → chunk-G6FWCTGT.mjs} +7 -5
  6. package/dist/lib/browser/chunk-G6FWCTGT.mjs.map +7 -0
  7. package/dist/lib/{node-esm/chunk-RHEZ5LUU.mjs → browser/chunk-G6H56WFS.mjs} +51 -2
  8. package/dist/lib/{node-esm/chunk-RHEZ5LUU.mjs.map → browser/chunk-G6H56WFS.mjs.map} +4 -4
  9. package/dist/lib/browser/{chunk-JBACGDOJ.mjs → chunk-MDX7ACIK.mjs} +3 -6
  10. package/dist/lib/browser/chunk-MDX7ACIK.mjs.map +7 -0
  11. package/dist/lib/browser/edge/index.mjs +3 -1
  12. package/dist/lib/browser/index.mjs +80 -61
  13. package/dist/lib/browser/index.mjs.map +4 -4
  14. package/dist/lib/browser/meta.json +1 -1
  15. package/dist/lib/browser/meta.mjs +1 -1
  16. package/dist/lib/browser/types/index.mjs +2 -2
  17. package/dist/lib/node/ScriptContainer-J76LKPAM.cjs +667 -0
  18. package/dist/lib/node/ScriptContainer-J76LKPAM.cjs.map +7 -0
  19. package/dist/lib/node/{chunk-5RBVXKXT.cjs → chunk-2PZKJXNP.cjs} +6 -6
  20. package/dist/lib/node/{chunk-5RBVXKXT.cjs.map → chunk-2PZKJXNP.cjs.map} +2 -2
  21. package/dist/lib/node/{chunk-4GTXIELQ.cjs → chunk-ACLCRWDB.cjs} +44 -6
  22. package/dist/lib/node/chunk-ACLCRWDB.cjs.map +7 -0
  23. package/dist/lib/node/{TypescriptEditor-M3WWFIP2.cjs → chunk-OJ77BZRH.cjs} +19 -8
  24. package/dist/lib/node/chunk-OJ77BZRH.cjs.map +7 -0
  25. package/dist/lib/node/{chunk-PFH3ROEH.cjs → chunk-UZ3ZXEBF.cjs} +11 -8
  26. package/dist/lib/node/chunk-UZ3ZXEBF.cjs.map +7 -0
  27. package/dist/lib/node/edge/index.cjs +10 -8
  28. package/dist/lib/node/edge/index.cjs.map +2 -2
  29. package/dist/lib/node/index.cjs +93 -79
  30. package/dist/lib/node/index.cjs.map +4 -4
  31. package/dist/lib/node/meta.cjs +3 -3
  32. package/dist/lib/node/meta.cjs.map +1 -1
  33. package/dist/lib/node/meta.json +1 -1
  34. package/dist/lib/node/types/index.cjs +5 -5
  35. package/dist/lib/node/types/index.cjs.map +1 -1
  36. package/dist/lib/node-esm/{ScriptEditor-5Y3EROG3.mjs → ScriptContainer-F6ACEA63.mjs} +258 -106
  37. package/dist/lib/node-esm/ScriptContainer-F6ACEA63.mjs.map +7 -0
  38. package/dist/lib/node-esm/{chunk-DL5XQRQM.mjs → chunk-2NX3HWPM.mjs} +9 -5
  39. package/dist/lib/node-esm/chunk-2NX3HWPM.mjs.map +7 -0
  40. package/dist/lib/node-esm/{chunk-O4LWBX6A.mjs → chunk-7BZDZF7Z.mjs} +3 -2
  41. package/dist/lib/node-esm/{chunk-O4LWBX6A.mjs.map → chunk-7BZDZF7Z.mjs.map} +2 -2
  42. package/dist/lib/{browser/chunk-RHEZ5LUU.mjs → node-esm/chunk-LDLNTVAK.mjs} +53 -2
  43. package/dist/lib/node-esm/chunk-LDLNTVAK.mjs.map +7 -0
  44. package/dist/lib/node-esm/{chunk-JBACGDOJ.mjs → chunk-TZ3Z4SII.mjs} +5 -6
  45. package/dist/lib/node-esm/chunk-TZ3Z4SII.mjs.map +7 -0
  46. package/dist/lib/node-esm/edge/index.mjs +4 -1
  47. package/dist/lib/node-esm/index.mjs +81 -61
  48. package/dist/lib/node-esm/index.mjs.map +4 -4
  49. package/dist/lib/node-esm/meta.json +1 -1
  50. package/dist/lib/node-esm/meta.mjs +2 -1
  51. package/dist/lib/node-esm/types/index.mjs +3 -2
  52. package/dist/types/src/ScriptPlugin.d.ts.map +1 -1
  53. package/dist/types/src/components/DebugPanel/DebugPanel.d.ts.map +1 -0
  54. package/dist/types/src/components/DebugPanel/index.d.ts +2 -0
  55. package/dist/types/src/components/DebugPanel/index.d.ts.map +1 -0
  56. package/dist/types/src/components/{ScriptBlock/FrameContainer → FrameContainer}/FrameContainer.d.ts +1 -1
  57. package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -0
  58. package/dist/types/src/components/FrameContainer/frame.d.ts.map +1 -0
  59. package/dist/types/src/components/FrameContainer/index.d.ts.map +1 -0
  60. package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts +1 -3
  61. package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts.map +1 -1
  62. package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts +1 -1
  63. package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts.map +1 -1
  64. package/dist/types/src/components/ScriptContainer.d.ts +5 -0
  65. package/dist/types/src/components/ScriptContainer.d.ts.map +1 -0
  66. package/dist/types/src/components/ScriptEditor.d.ts +4 -5
  67. package/dist/types/src/components/ScriptEditor.d.ts.map +1 -1
  68. package/dist/types/src/components/ScriptSettings/ScriptSettings.d.ts +6 -0
  69. package/dist/types/src/components/ScriptSettings/ScriptSettings.d.ts.map +1 -0
  70. package/dist/types/src/components/ScriptSettings/index.d.ts +2 -0
  71. package/dist/types/src/components/ScriptSettings/index.d.ts.map +1 -0
  72. package/dist/types/src/components/Toolbar/Toolbar.d.ts +23 -0
  73. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  74. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +17 -0
  75. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  76. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  77. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  78. package/dist/types/src/components/{TypescriptEditor.d.ts → TypescriptEditor/TypescriptEditor.d.ts} +1 -2
  79. package/dist/types/src/components/TypescriptEditor/TypescriptEditor.d.ts.map +1 -0
  80. package/dist/types/src/components/TypescriptEditor/TypescriptEditor.stories.d.ts +10 -0
  81. package/dist/types/src/components/TypescriptEditor/TypescriptEditor.stories.d.ts.map +1 -0
  82. package/dist/types/src/components/TypescriptEditor/index.d.ts +2 -0
  83. package/dist/types/src/components/TypescriptEditor/index.d.ts.map +1 -0
  84. package/dist/types/src/components/index.d.ts +2 -2
  85. package/dist/types/src/components/index.d.ts.map +1 -1
  86. package/dist/types/src/edge/functions.d.ts +1 -0
  87. package/dist/types/src/edge/functions.d.ts.map +1 -1
  88. package/dist/types/src/meta.d.ts +1 -4
  89. package/dist/types/src/meta.d.ts.map +1 -1
  90. package/dist/types/src/templates/chat.d.ts +6 -0
  91. package/dist/types/src/templates/chat.d.ts.map +1 -0
  92. package/dist/types/src/templates/chess.d.ts +6 -0
  93. package/dist/types/src/templates/chess.d.ts.map +1 -0
  94. package/dist/types/src/templates/echo.d.ts +6 -0
  95. package/dist/types/src/templates/echo.d.ts.map +1 -0
  96. package/dist/types/src/templates/email.d.ts +6 -0
  97. package/dist/types/src/templates/email.d.ts.map +1 -0
  98. package/dist/types/src/templates/forex.d.ts +6 -0
  99. package/dist/types/src/templates/forex.d.ts.map +1 -0
  100. package/dist/types/src/templates.d.ts +8 -0
  101. package/dist/types/src/templates.d.ts.map +1 -0
  102. package/dist/types/src/translations.d.ts +2 -0
  103. package/dist/types/src/translations.d.ts.map +1 -1
  104. package/dist/types/src/types/script.d.ts +4 -4
  105. package/dist/types/src/types/types.d.ts +4 -3
  106. package/dist/types/src/types/types.d.ts.map +1 -1
  107. package/package.json +38 -37
  108. package/src/ScriptPlugin.tsx +21 -36
  109. package/src/components/{DebugPanel.tsx → DebugPanel/DebugPanel.tsx} +48 -41
  110. package/src/components/DebugPanel/index.ts +5 -0
  111. package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/FrameContainer.tsx +1 -1
  112. package/src/components/ScriptBlock/ScriptBlock.stories.tsx +4 -3
  113. package/src/components/ScriptBlock/ScriptBlock.tsx +10 -36
  114. package/src/components/ScriptContainer.tsx +22 -0
  115. package/src/components/ScriptEditor.tsx +47 -46
  116. package/src/components/ScriptSettings/ScriptSettings.tsx +47 -0
  117. package/src/components/ScriptSettings/index.ts +5 -0
  118. package/src/components/{Toolbar.stories.tsx → Toolbar/Toolbar.stories.tsx} +10 -1
  119. package/src/components/Toolbar/Toolbar.tsx +180 -0
  120. package/src/components/{ScriptBlock/Splitter → Toolbar}/index.ts +1 -1
  121. package/src/components/{TypescriptEditor.stories.tsx → TypescriptEditor/TypescriptEditor.stories.tsx} +7 -28
  122. package/src/components/{TypescriptEditor.tsx → TypescriptEditor/TypescriptEditor.tsx} +1 -3
  123. package/src/components/TypescriptEditor/index.ts +5 -0
  124. package/src/components/index.ts +3 -2
  125. package/src/edge/functions.ts +1 -0
  126. package/src/meta.tsx +2 -6
  127. package/src/templates/chat.ts +102 -0
  128. package/src/templates/chess.ts +41 -0
  129. package/src/templates/echo.ts +15 -0
  130. package/src/templates/email.ts +38 -0
  131. package/src/templates/forex.ts +21 -0
  132. package/src/templates.ts +54 -0
  133. package/src/translations.ts +2 -0
  134. package/src/types/types.ts +9 -5
  135. package/dist/lib/browser/ScriptEditor-5Y3EROG3.mjs.map +0 -7
  136. package/dist/lib/browser/TypescriptEditor-GPE72Y6I.mjs +0 -9
  137. package/dist/lib/browser/TypescriptEditor-GPE72Y6I.mjs.map +0 -7
  138. package/dist/lib/browser/chunk-DL5XQRQM.mjs.map +0 -7
  139. package/dist/lib/browser/chunk-JBACGDOJ.mjs.map +0 -7
  140. package/dist/lib/browser/chunk-MRGOT5RF.mjs +0 -78
  141. package/dist/lib/browser/chunk-MRGOT5RF.mjs.map +0 -7
  142. package/dist/lib/browser/chunk-RHEZ5LUU.mjs.map +0 -7
  143. package/dist/lib/node/ScriptEditor-YCUQFWQM.cjs +0 -526
  144. package/dist/lib/node/ScriptEditor-YCUQFWQM.cjs.map +0 -7
  145. package/dist/lib/node/TypescriptEditor-M3WWFIP2.cjs.map +0 -7
  146. package/dist/lib/node/chunk-4GTXIELQ.cjs.map +0 -7
  147. package/dist/lib/node/chunk-4VMNBXWU.cjs +0 -54
  148. package/dist/lib/node/chunk-4VMNBXWU.cjs.map +0 -7
  149. package/dist/lib/node/chunk-IMLDKD5Y.cjs +0 -111
  150. package/dist/lib/node/chunk-IMLDKD5Y.cjs.map +0 -7
  151. package/dist/lib/node/chunk-PFH3ROEH.cjs.map +0 -7
  152. package/dist/lib/node-esm/ScriptEditor-5Y3EROG3.mjs.map +0 -7
  153. package/dist/lib/node-esm/TypescriptEditor-GPE72Y6I.mjs +0 -9
  154. package/dist/lib/node-esm/TypescriptEditor-GPE72Y6I.mjs.map +0 -7
  155. package/dist/lib/node-esm/chunk-DL5XQRQM.mjs.map +0 -7
  156. package/dist/lib/node-esm/chunk-JBACGDOJ.mjs.map +0 -7
  157. package/dist/lib/node-esm/chunk-MRGOT5RF.mjs +0 -78
  158. package/dist/lib/node-esm/chunk-MRGOT5RF.mjs.map +0 -7
  159. package/dist/types/src/components/DebugPanel.d.ts.map +0 -1
  160. package/dist/types/src/components/ScriptBlock/FrameContainer/FrameContainer.d.ts.map +0 -1
  161. package/dist/types/src/components/ScriptBlock/FrameContainer/frame.d.ts.map +0 -1
  162. package/dist/types/src/components/ScriptBlock/FrameContainer/index.d.ts.map +0 -1
  163. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.d.ts +0 -15
  164. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.d.ts.map +0 -1
  165. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.stories.d.ts +0 -13
  166. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.stories.d.ts.map +0 -1
  167. package/dist/types/src/components/ScriptBlock/Splitter/index.d.ts +0 -2
  168. package/dist/types/src/components/ScriptBlock/Splitter/index.d.ts.map +0 -1
  169. package/dist/types/src/components/Toolbar.d.ts +0 -13
  170. package/dist/types/src/components/Toolbar.d.ts.map +0 -1
  171. package/dist/types/src/components/Toolbar.stories.d.ts +0 -14
  172. package/dist/types/src/components/Toolbar.stories.d.ts.map +0 -1
  173. package/dist/types/src/components/TypescriptEditor.d.ts.map +0 -1
  174. package/dist/types/src/components/TypescriptEditor.stories.d.ts +0 -10
  175. package/dist/types/src/components/TypescriptEditor.stories.d.ts.map +0 -1
  176. package/src/components/ScriptBlock/Splitter/Splitter.stories.tsx +0 -37
  177. package/src/components/ScriptBlock/Splitter/Splitter.tsx +0 -52
  178. package/src/components/Toolbar.tsx +0 -147
  179. /package/dist/types/src/components/{DebugPanel.d.ts → DebugPanel/DebugPanel.d.ts} +0 -0
  180. /package/dist/types/src/components/{ScriptBlock/FrameContainer → FrameContainer}/frame.d.ts +0 -0
  181. /package/dist/types/src/components/{ScriptBlock/FrameContainer → FrameContainer}/index.d.ts +0 -0
  182. /package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/frame.html +0 -0
  183. /package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/frame.tsx +0 -0
  184. /package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/index.ts +0 -0
@@ -0,0 +1,180 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import React, { useState } from 'react';
6
+
7
+ import {
8
+ DensityProvider,
9
+ ElevationProvider,
10
+ Icon,
11
+ Select,
12
+ type ThemedClassName,
13
+ ToggleGroup,
14
+ ToggleGroupItem,
15
+ type ToolbarButtonProps,
16
+ Toolbar as NaturalToolbar,
17
+ Tooltip,
18
+ useTranslation,
19
+ } from '@dxos/react-ui';
20
+
21
+ import { SCRIPT_PLUGIN } from '../../meta';
22
+ import { type Template } from '../../templates';
23
+
24
+ export type ToolbarProps = ThemedClassName<
25
+ {
26
+ deployed?: boolean;
27
+ error?: string;
28
+ functionUrl?: string;
29
+ onDeploy?: () => Promise<void>;
30
+ onFormat?: () => Promise<void>;
31
+ } & TemplateSelectProps &
32
+ ViewSelectorProps
33
+ >;
34
+
35
+ export const Toolbar = ({
36
+ classNames,
37
+ deployed,
38
+ error,
39
+ functionUrl,
40
+ view,
41
+ templates,
42
+ onDeploy,
43
+ onFormat,
44
+ onViewChange,
45
+ onTemplateSelect,
46
+ }: ToolbarProps) => {
47
+ const { t } = useTranslation(SCRIPT_PLUGIN);
48
+ const [pending, setPending] = useState(false);
49
+
50
+ const handleCopyLink = async () => {
51
+ if (!functionUrl) {
52
+ return;
53
+ }
54
+
55
+ await navigator.clipboard.writeText(functionUrl);
56
+ };
57
+
58
+ const handleDeploy = async () => {
59
+ if (!pending) {
60
+ setPending(true);
61
+ await onDeploy?.();
62
+ setPending(false);
63
+ }
64
+ };
65
+
66
+ // TODO(burdon): Factor out common toolbar components with sheet/editor?
67
+ return (
68
+ <DensityProvider density='fine'>
69
+ <ElevationProvider elevation='chrome'>
70
+ <NaturalToolbar.Root classNames={['p-1', classNames]} style={{ contain: 'layout' }}>
71
+ {templates && <TemplateSelect templates={templates} onTemplateSelect={onTemplateSelect} />}
72
+ {onFormat && <ToolbarIconButton icon='ph--magic-wand--regular' text={t('format label')} onClick={onFormat} />}
73
+
74
+ <div role='separator' className='grow' />
75
+
76
+ {error ? (
77
+ <Tooltip.Root>
78
+ <Tooltip.Trigger>
79
+ <Icon icon='ph--warning-circle--regular' size={4} />
80
+ </Tooltip.Trigger>
81
+ <Tooltip.Portal>
82
+ <Tooltip.Content>
83
+ <Tooltip.Arrow />
84
+ {error}
85
+ </Tooltip.Content>
86
+ </Tooltip.Portal>
87
+ </Tooltip.Root>
88
+ ) : null}
89
+
90
+ {functionUrl && deployed && (
91
+ <ToolbarIconButton icon='ph--link--regular' text={t('copy link label')} onClick={handleCopyLink} />
92
+ )}
93
+
94
+ {onDeploy && (
95
+ <ToolbarIconButton
96
+ icon={pending ? 'ph--spinner-gap--regular' : 'ph--cloud-arrow-up--regular'}
97
+ classNames={[pending && 'animate-spin']}
98
+ text={pending ? t('pending label') : t('deploy label')}
99
+ onClick={handleDeploy}
100
+ />
101
+ )}
102
+
103
+ <ViewSelector view={view} onViewChange={onViewChange} />
104
+ </NaturalToolbar.Root>
105
+ </ElevationProvider>
106
+ </DensityProvider>
107
+ );
108
+ };
109
+
110
+ const ToolbarIconButton = ({
111
+ icon,
112
+ text,
113
+ classNames,
114
+ disabled,
115
+ onClick,
116
+ }: { icon: string; text: string } & Pick<ToolbarButtonProps, 'classNames' | 'disabled' | 'onClick'>) => {
117
+ return (
118
+ <Tooltip.Root>
119
+ <Tooltip.Trigger asChild>
120
+ <NaturalToolbar.Button variant='ghost' disabled={disabled} onClick={onClick}>
121
+ <Icon icon={icon} size={4} classNames={classNames} />
122
+ </NaturalToolbar.Button>
123
+ </Tooltip.Trigger>
124
+ <Tooltip.Portal>
125
+ <Tooltip.Content>
126
+ <Tooltip.Arrow />
127
+ {text}
128
+ </Tooltip.Content>
129
+ </Tooltip.Portal>
130
+ </Tooltip.Root>
131
+ );
132
+ };
133
+
134
+ export type ViewType = 'editor' | 'split' | 'preview';
135
+
136
+ export type TemplateSelectProps = {
137
+ templates?: Template[];
138
+ onTemplateSelect?: (id: string) => void;
139
+ };
140
+
141
+ export const TemplateSelect = ({ templates = [], onTemplateSelect }: TemplateSelectProps) => {
142
+ return (
143
+ <Select.Root value={''} onValueChange={onTemplateSelect}>
144
+ <Select.TriggerButton />
145
+ <Select.Portal>
146
+ <Select.Content>
147
+ <Select.Viewport>
148
+ <Select.Option value={''}>Template</Select.Option>
149
+ {templates.map(({ id, name }) => (
150
+ <Select.Option key={id} value={id}>
151
+ {name}
152
+ </Select.Option>
153
+ ))}
154
+ </Select.Viewport>
155
+ </Select.Content>
156
+ </Select.Portal>
157
+ </Select.Root>
158
+ );
159
+ };
160
+
161
+ export type ViewSelectorProps = {
162
+ view?: ViewType;
163
+ onViewChange?: (view: ViewType) => void;
164
+ };
165
+
166
+ export const ViewSelector = ({ view, onViewChange }: ViewSelectorProps) => {
167
+ return (
168
+ <ToggleGroup type='single' value={view} onValueChange={(value) => onViewChange?.(value as ViewType)}>
169
+ <ToggleGroupItem value='editor' classNames='bg-transparent text-subdued'>
170
+ <Icon icon='ph--code--regular' size={4} />
171
+ </ToggleGroupItem>
172
+ <ToggleGroupItem value='split' classNames='bg-transparent text-subdued'>
173
+ <Icon icon='ph--square-split-vertical--regular' size={4} />
174
+ </ToggleGroupItem>
175
+ <ToggleGroupItem value='preview' classNames='bg-transparent text-subdued'>
176
+ <Icon icon='ph--eye--regular' size={4} />
177
+ </ToggleGroupItem>
178
+ </ToggleGroup>
179
+ );
180
+ };
@@ -2,4 +2,4 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './Splitter';
5
+ export * from './Toolbar';
@@ -10,45 +10,24 @@ import { createDocAccessor, createEchoObject } from '@dxos/react-client/echo';
10
10
  import { createDataExtensions } from '@dxos/react-ui-editor';
11
11
 
12
12
  import { TypescriptEditor } from './TypescriptEditor';
13
+ import { templates } from '../../templates';
13
14
 
14
- // TODO(burdon): Typescript.
15
- // TODO(burdon): Effect schema.
16
- // TODO(burdon): JSX.
17
- // TODO(burdon): react-buddy for storybook?
18
-
19
- // Marijn
15
+ // TODO(burdon): Features:
20
16
  // - language support for S
21
17
  // - hierarchical editor (DND)
22
18
  // - virtual document image rendering
23
19
  // - mobile rendering error
24
20
 
25
- const examples: string[] = [
26
- [
27
- '// Example function.',
28
- 'export default function() {',
29
- ' const value = 100',
30
- ' return <div>{value}</div>;',
31
- '}',
32
- ].join('\n'),
33
- [
34
- '// Example schema.',
35
- 'S.Struct({',
36
- ' timestamp: S.Date,',
37
- ' title: S.String,',
38
- ' content: R.Text,',
39
- "}).pipe(S.identifier('dxos.org/schema/Test'))",
40
- '',
41
- ].join('\n'),
42
- ];
21
+ // TODO(burdon): JSX.
22
+ // TODO(burdon): Effect schema.
23
+ // TODO(burdon): react-buddy for storybook?
43
24
 
44
25
  const Story = () => {
45
- const object = useMemo(() => createEchoObject({ content: examples[0] }), []);
26
+ const object = useMemo(() => createEchoObject({ content: templates[0].source }), []);
46
27
  const initialValue = useMemo(() => object.content, [object]);
47
28
  const accessor = useMemo(() => createDocAccessor(object, ['content']), [object]);
48
29
  const extensions = useMemo(() => [createDataExtensions({ id: object.id, text: accessor })], [object.id, accessor]);
49
-
50
- // TODO(wittjosiah): Scroll past end breaks the editor.
51
- return <TypescriptEditor id='test' initialValue={initialValue} extensions={extensions} scrollPastEnd={false} />;
30
+ return <TypescriptEditor id='test' initialValue={initialValue} extensions={extensions} />;
52
31
  };
53
32
 
54
33
  export default {
@@ -23,13 +23,11 @@ import { nonNullable } from '@dxos/util';
23
23
 
24
24
  export type TypescriptEditorProps = {
25
25
  id: string;
26
- scrollPastEnd?: boolean;
27
26
  env?: VirtualTypeScriptEnvironment;
28
27
  } & Pick<UseTextEditorProps, 'className' | 'initialValue' | 'extensions' | 'scrollTo' | 'selection'>;
29
28
 
30
29
  export const TypescriptEditor = ({
31
30
  id,
32
- scrollPastEnd,
33
31
  env,
34
32
  className,
35
33
  initialValue,
@@ -49,7 +47,7 @@ export const TypescriptEditor = ({
49
47
  indentWithTab: true,
50
48
  lineNumbers: true,
51
49
  lineWrapping: false,
52
- scrollPastEnd,
50
+ scrollPastEnd: true,
53
51
  }),
54
52
  createThemeExtensions({
55
53
  themeMode,
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './TypescriptEditor';
@@ -4,5 +4,6 @@
4
4
 
5
5
  import React from 'react';
6
6
 
7
- export const ScriptEditor = React.lazy(() => import('./ScriptEditor'));
8
- export const TypescriptEditor = React.lazy(() => import('./TypescriptEditor'));
7
+ export * from './ScriptSettings';
8
+
9
+ export const ScriptContainer = React.lazy(() => import('./ScriptContainer'));
@@ -17,6 +17,7 @@ import { matchServiceCredential } from './hub-protocol';
17
17
 
18
18
  // TODO: use URL scheme for source?
19
19
  const USERFUNCTIONS_META_KEY = 'dxos.org/service/function';
20
+ export const USERFUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';
20
21
  const USERFUNCTIONS_CREDENTIAL_CAPABILITY = 'composer:beta';
21
22
 
22
23
  // TODO: Synchronize API types with server
package/src/meta.tsx CHANGED
@@ -2,9 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Code, type IconProps } from '@phosphor-icons/react';
6
- import React from 'react';
7
-
8
5
  import { pluginMeta } from '@dxos/app-framework';
9
6
 
10
7
  export const SCRIPT_PLUGIN = 'dxos.org/plugin/script';
@@ -12,8 +9,7 @@ export const SCRIPT_PLUGIN = 'dxos.org/plugin/script';
12
9
  export default pluginMeta({
13
10
  id: SCRIPT_PLUGIN,
14
11
  name: 'Scripts',
15
- description: 'Live code notebooks.',
12
+ description: 'Distributed functions.',
16
13
  tags: ['experimental'],
17
- iconComponent: (props: IconProps) => <Code {...props} />,
18
- iconSymbol: 'ph--code--regular',
14
+ icon: 'ph--code--regular',
19
15
  });
@@ -0,0 +1,102 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ // @ts-ignore
6
+ import { transformServerSentEvents } from 'https://esm.sh/@dxos/script-toolbox';
7
+
8
+ /**
9
+ * Chatbot that answers questions based on the context data.
10
+ */
11
+ export default async ({
12
+ event: {
13
+ data: { request },
14
+ },
15
+ context: { space, ai },
16
+ }: any) => {
17
+ const textBody = await request.text();
18
+ let parsedRequest: any;
19
+ try {
20
+ parsedRequest = JSON.parse(textBody);
21
+ } catch (err) {
22
+ const answer = await ai.run('@cf/meta/llama-3.1-8b-instruct', {
23
+ stream: true,
24
+ prompt: `
25
+ Using only the context data below to answer the user's question.
26
+ If you cannot answer the question from the provided content, then reply with "I don't know."
27
+
28
+ CONTEXT:
29
+ ${await getContext({ space })}
30
+
31
+ QUESTION:
32
+ ${textBody}
33
+ `,
34
+ });
35
+ return new Response(transformServerSentEvents(answer));
36
+ }
37
+
38
+ const promptId = parsedRequest.trigger.promptId;
39
+ const promptObject = await space.crud.query({ id: promptId }).first();
40
+
41
+ if (parsedRequest.data.objects.length !== 1) {
42
+ return new Response('Message batches not supported.');
43
+ }
44
+
45
+ const messageId = parsedRequest.data.objects[0];
46
+ let message;
47
+ try {
48
+ message = await space.crud.query({ id: messageId }).first();
49
+ } catch (err) {
50
+ return new Response('Message not replicated yet.', { status: 409 });
51
+ }
52
+
53
+ if (message.text.includes('--------')) {
54
+ return new Response('Message was already processed.');
55
+ }
56
+
57
+ let prompt = promptObject.template;
58
+ for (const input of promptObject.inputs) {
59
+ let value: string | null = null;
60
+ if (input.type === ChainInputType.VALUE) {
61
+ value = String(input.value);
62
+ } else if (input.type === ChainInputType.RETRIEVER) {
63
+ value = await getContext({ space });
64
+ } else if (input.type === ChainInputType.PASS_THROUGH) {
65
+ value = message.text;
66
+ }
67
+ if (value) {
68
+ prompt = prompt.replaceAll(`{${input.name}}`, value);
69
+ }
70
+ }
71
+
72
+ const answer = await ai.run('@cf/meta/llama-3.1-8b-instruct', {
73
+ stream: false,
74
+ prompt,
75
+ });
76
+ const response = { response: answer.response, prompt, message };
77
+ const textWithAnswer = message.text + '\n--------\n\n' + answer.response;
78
+ await space.crud.update({ id: messageId }, { text: textWithAnswer });
79
+
80
+ return new Response(JSON.stringify(response));
81
+ };
82
+
83
+ const getContext = async ({ space }: any) => {
84
+ let context = '';
85
+ const { objects: docs } = await space.crud.query({ __typename: 'dxos.org/type/Document' }).run();
86
+ for (const doc of docs) {
87
+ const { content } = await space.crud.query({ id: doc.content }).first();
88
+ context += content + '\n\n';
89
+ }
90
+ return context;
91
+ };
92
+
93
+ enum ChainInputType {
94
+ VALUE = 0,
95
+ PASS_THROUGH = 1,
96
+ RETRIEVER = 2,
97
+ FUNCTION = 3,
98
+ QUERY = 4,
99
+ RESOLVER = 5,
100
+ CONTEXT = 6,
101
+ SCHEMA = 7,
102
+ }
@@ -0,0 +1,41 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ // @ts-ignore
6
+ import { Chess } from 'https://esm.sh/chess.js@0.13.1';
7
+
8
+ /**
9
+ * Plays a random move in a chess game.
10
+ */
11
+ export default async ({
12
+ event: {
13
+ data: { request },
14
+ },
15
+ context: { space },
16
+ }: any) => {
17
+ const {
18
+ data: {
19
+ objects: [gameId],
20
+ player = 'b',
21
+ },
22
+ } = await request.json();
23
+
24
+ const { pgn } = await space.crud.query({ id: gameId }).first();
25
+ const game = new Chess();
26
+ game.load_pgn(pgn);
27
+ if (game.turn() !== player) {
28
+ return new Response('Invalid turn', { status: 409 });
29
+ }
30
+
31
+ const moves = game.moves();
32
+ const move = moves[Math.floor(Math.random() * moves.length)];
33
+ if (!move) {
34
+ return new Response('No legal moves', { status: 406 });
35
+ }
36
+
37
+ game.move(move);
38
+ const newPgn = game.pgn();
39
+ await space.crud.update({ id: gameId }, { pgn: newPgn });
40
+ return new Response(game.ascii());
41
+ };
@@ -0,0 +1,15 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Echos the request data.
7
+ */
8
+ export default async ({
9
+ event: {
10
+ data: { request },
11
+ },
12
+ }: any) => {
13
+ const data = await request.json();
14
+ return new Response(JSON.stringify(data ?? {}), { headers: { 'Content-Type': 'application/json' } });
15
+ };
@@ -0,0 +1,38 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Stores a message into ECHO Mailbox object.
7
+ */
8
+ export default async ({
9
+ event: {
10
+ data: { request },
11
+ },
12
+ context: { space },
13
+ }: any) => {
14
+ const {
15
+ data: {
16
+ objects: [message],
17
+ },
18
+ } = await request.json();
19
+
20
+ const mailbox = await space.crud.query({ __typename: 'dxos.org/type/MailboxType' }).first();
21
+ if (!mailbox) {
22
+ return new Response('Mailbox not found.', { status: 500 });
23
+ }
24
+
25
+ await space.crud.push(
26
+ { id: mailbox.id },
27
+ {
28
+ sender: { email: message.from },
29
+ timestamp: new Date(message.created).toISOString(),
30
+ text: message.body,
31
+ properties: {
32
+ subject: message.subject,
33
+ to: [{ email: message.to }],
34
+ },
35
+ },
36
+ );
37
+ return new Response(JSON.stringify(message));
38
+ };
@@ -0,0 +1,21 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Returns the exchange rate between two currencies.
7
+ */
8
+ export default async ({
9
+ event: {
10
+ data: { request },
11
+ },
12
+ }: any) => {
13
+ const { args: [from = 'EUR', to = 'USD'] = [] } = await request.json();
14
+
15
+ const res = await fetch(`https://free.ratesdb.com/v1/rates?from=${from}&to=${to}`);
16
+ const {
17
+ data: { rates },
18
+ } = await res.json();
19
+
20
+ return new Response(rates[to].toString());
21
+ };
@@ -0,0 +1,54 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ // @ts-ignore
6
+ import chat from './templates/chat.ts?raw';
7
+ // @ts-ignore
8
+ import chess from './templates/chess.ts?raw';
9
+ // @ts-ignore
10
+ import echo from './templates/echo.ts?raw';
11
+ // @ts-ignore
12
+ import email from './templates/email.ts?raw';
13
+ // @ts-ignore
14
+ import forex from './templates/forex.ts?raw';
15
+
16
+ const removeHeader = (str: string) => str.split('\n').slice(4).join('\n');
17
+
18
+ export type Template = {
19
+ id: string;
20
+ name: string;
21
+ source: string;
22
+ presetId?: string;
23
+ };
24
+
25
+ export const templates: Template[] = [
26
+ {
27
+ id: 'dxos.org/script/echo',
28
+ name: 'Echo',
29
+ source: removeHeader(echo),
30
+ },
31
+ {
32
+ id: 'dxos.org/script/forex',
33
+ name: 'Forex',
34
+ source: removeHeader(forex),
35
+ },
36
+ {
37
+ id: 'dxos.org/script/gpt',
38
+ name: 'Gpt',
39
+ source: removeHeader(chat),
40
+ presetId: 'dxos.org/function/gpt',
41
+ },
42
+ {
43
+ id: 'dxos.org/script/chess',
44
+ name: 'Chess',
45
+ source: removeHeader(chess),
46
+ presetId: 'dxos.org/function/chess',
47
+ },
48
+ {
49
+ id: 'dxos.org/script/email',
50
+ name: 'Email',
51
+ source: removeHeader(email),
52
+ presetId: 'dxos.org/function/email',
53
+ },
54
+ ];
@@ -20,6 +20,8 @@ export default [
20
20
  'copy link label': 'Copy function link',
21
21
  'format label': 'Format',
22
22
  'toggle details label': 'Toggle details panel',
23
+ 'authenticate action label': 'Authenticate functions service',
24
+ 'authenticate button label': 'Authenticate',
23
25
  },
24
26
  },
25
27
  },
@@ -6,11 +6,12 @@ import type {
6
6
  GraphBuilderProvides,
7
7
  IntentResolverProvides,
8
8
  MetadataRecordsProvides,
9
+ SettingsProvides,
9
10
  SurfaceProvides,
10
11
  TranslationsProvides,
11
12
  } from '@dxos/app-framework';
12
13
  import { type SchemaProvides } from '@dxos/plugin-client';
13
- import { type StackProvides } from '@dxos/plugin-stack';
14
+ import { type SpaceInitProvides } from '@dxos/plugin-space';
14
15
 
15
16
  import { SCRIPT_PLUGIN } from '../meta';
16
17
 
@@ -18,10 +19,13 @@ export enum ScriptAction {
18
19
  CREATE = `${SCRIPT_PLUGIN}/create`,
19
20
  }
20
21
 
21
- export type ScriptPluginProvides = SurfaceProvides &
22
+ export type ScriptSettingsProps = {};
23
+
24
+ export type ScriptPluginProvides = GraphBuilderProvides &
22
25
  IntentResolverProvides &
23
- GraphBuilderProvides &
24
26
  MetadataRecordsProvides &
25
- TranslationsProvides &
26
27
  SchemaProvides &
27
- StackProvides;
28
+ SettingsProvides<ScriptSettingsProps> &
29
+ SpaceInitProvides &
30
+ SurfaceProvides &
31
+ TranslationsProvides;