@dxos/plugin-script 0.6.13 → 0.6.14-main.69511f5

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 (202) hide show
  1. package/README.md +1 -1
  2. package/dist/lib/browser/AutomationPanel-MDPZ5WAC.mjs +78 -0
  3. package/dist/lib/browser/AutomationPanel-MDPZ5WAC.mjs.map +7 -0
  4. package/dist/lib/browser/ScriptContainer-B3CPWXRE.mjs +423 -0
  5. package/dist/lib/browser/ScriptContainer-B3CPWXRE.mjs.map +7 -0
  6. package/dist/lib/browser/chunk-G66MXS4T.mjs +248 -0
  7. package/dist/lib/browser/chunk-G66MXS4T.mjs.map +7 -0
  8. package/dist/lib/browser/{chunk-RHEZ5LUU.mjs → chunk-G6H56WFS.mjs} +51 -2
  9. package/dist/lib/browser/{chunk-RHEZ5LUU.mjs.map → chunk-G6H56WFS.mjs.map} +4 -4
  10. package/dist/lib/browser/{chunk-DL5XQRQM.mjs → chunk-KHNN3F7J.mjs} +18 -6
  11. package/dist/lib/browser/chunk-KHNN3F7J.mjs.map +7 -0
  12. package/dist/lib/browser/{chunk-O4LWBX6A.mjs → chunk-KXVQPS4Y.mjs} +2 -2
  13. package/dist/lib/browser/{chunk-O4LWBX6A.mjs.map → chunk-KXVQPS4Y.mjs.map} +2 -2
  14. package/dist/lib/browser/chunk-SGQHRB4P.mjs +17 -0
  15. package/dist/lib/browser/chunk-SGQHRB4P.mjs.map +7 -0
  16. package/dist/lib/browser/edge/index.mjs +5 -1
  17. package/dist/lib/browser/index.mjs +91 -61
  18. package/dist/lib/browser/index.mjs.map +4 -4
  19. package/dist/lib/browser/meta.json +1 -1
  20. package/dist/lib/browser/meta.mjs +1 -1
  21. package/dist/lib/browser/types/index.mjs +2 -2
  22. package/dist/lib/node/AutomationPanel-SANUE5X4.cjs +100 -0
  23. package/dist/lib/node/AutomationPanel-SANUE5X4.cjs.map +7 -0
  24. package/dist/lib/node/ScriptContainer-BV7HMUY5.cjs +425 -0
  25. package/dist/lib/node/ScriptContainer-BV7HMUY5.cjs.map +7 -0
  26. package/dist/lib/node/{chunk-5RBVXKXT.cjs → chunk-25PQE7FL.cjs} +6 -6
  27. package/dist/lib/node/{chunk-5RBVXKXT.cjs.map → chunk-25PQE7FL.cjs.map} +2 -2
  28. package/dist/lib/node/{chunk-4GTXIELQ.cjs → chunk-ACLCRWDB.cjs} +44 -6
  29. package/dist/lib/node/chunk-ACLCRWDB.cjs.map +7 -0
  30. package/dist/lib/node/{TypescriptEditor-M3WWFIP2.cjs → chunk-EJC55SDD.cjs} +18 -8
  31. package/dist/lib/node/chunk-EJC55SDD.cjs.map +7 -0
  32. package/dist/lib/node/{chunk-PFH3ROEH.cjs → chunk-FY5KKL5N.cjs} +22 -8
  33. package/dist/lib/node/chunk-FY5KKL5N.cjs.map +7 -0
  34. package/dist/lib/node/chunk-JWKMTYYH.cjs +277 -0
  35. package/dist/lib/node/chunk-JWKMTYYH.cjs.map +7 -0
  36. package/dist/lib/node/edge/index.cjs +12 -8
  37. package/dist/lib/node/edge/index.cjs.map +2 -2
  38. package/dist/lib/node/index.cjs +107 -81
  39. package/dist/lib/node/index.cjs.map +4 -4
  40. package/dist/lib/node/meta.cjs +3 -3
  41. package/dist/lib/node/meta.cjs.map +1 -1
  42. package/dist/lib/node/meta.json +1 -1
  43. package/dist/lib/node/types/index.cjs +5 -5
  44. package/dist/lib/node/types/index.cjs.map +1 -1
  45. package/dist/lib/node-esm/AutomationPanel-H2IKE53E.mjs +79 -0
  46. package/dist/lib/node-esm/AutomationPanel-H2IKE53E.mjs.map +7 -0
  47. package/dist/lib/node-esm/ScriptContainer-HSIOGVP3.mjs +424 -0
  48. package/dist/lib/node-esm/ScriptContainer-HSIOGVP3.mjs.map +7 -0
  49. package/dist/lib/node-esm/chunk-3QHVR4VV.mjs +249 -0
  50. package/dist/lib/node-esm/chunk-3QHVR4VV.mjs.map +7 -0
  51. package/dist/lib/node-esm/chunk-7EDYDVQH.mjs +47 -0
  52. package/dist/lib/node-esm/chunk-7EDYDVQH.mjs.map +7 -0
  53. package/dist/lib/node-esm/chunk-GO7HYFM5.mjs +19 -0
  54. package/dist/lib/node-esm/chunk-GO7HYFM5.mjs.map +7 -0
  55. package/dist/lib/node-esm/chunk-LDLNTVAK.mjs +280 -0
  56. package/dist/lib/node-esm/chunk-LDLNTVAK.mjs.map +7 -0
  57. package/dist/lib/node-esm/chunk-QMTTZYLF.mjs +255 -0
  58. package/dist/lib/node-esm/chunk-QMTTZYLF.mjs.map +7 -0
  59. package/dist/lib/node-esm/edge/index.mjs +24 -0
  60. package/dist/lib/node-esm/index.mjs +322 -0
  61. package/dist/lib/node-esm/index.mjs.map +7 -0
  62. package/dist/lib/node-esm/meta.json +1 -0
  63. package/dist/lib/node-esm/meta.mjs +10 -0
  64. package/dist/lib/node-esm/meta.mjs.map +7 -0
  65. package/dist/lib/node-esm/types/index.mjs +13 -0
  66. package/dist/lib/node-esm/types/index.mjs.map +7 -0
  67. package/dist/types/src/ScriptPlugin.d.ts.map +1 -1
  68. package/dist/types/src/components/AutomationPanel/AutomationPanel.d.ts +17 -0
  69. package/dist/types/src/components/AutomationPanel/AutomationPanel.d.ts.map +1 -0
  70. package/dist/types/src/components/AutomationPanel/index.d.ts +4 -0
  71. package/dist/types/src/components/AutomationPanel/index.d.ts.map +1 -0
  72. package/dist/types/src/components/{DebugPanel.d.ts → DebugPanel/DebugPanel.d.ts} +2 -1
  73. package/dist/types/src/components/DebugPanel/DebugPanel.d.ts.map +1 -0
  74. package/dist/types/src/components/DebugPanel/index.d.ts +2 -0
  75. package/dist/types/src/components/DebugPanel/index.d.ts.map +1 -0
  76. package/dist/types/src/components/{ScriptBlock/FrameContainer → FrameContainer}/FrameContainer.d.ts +1 -1
  77. package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -0
  78. package/dist/types/src/components/FrameContainer/frame.d.ts.map +1 -0
  79. package/dist/types/src/components/FrameContainer/index.d.ts.map +1 -0
  80. package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts +1 -3
  81. package/dist/types/src/components/ScriptBlock/ScriptBlock.d.ts.map +1 -1
  82. package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts +3 -8
  83. package/dist/types/src/components/ScriptBlock/ScriptBlock.stories.d.ts.map +1 -1
  84. package/dist/types/src/components/ScriptContainer.d.ts +5 -0
  85. package/dist/types/src/components/ScriptContainer.d.ts.map +1 -0
  86. package/dist/types/src/components/ScriptEditor.d.ts +4 -5
  87. package/dist/types/src/components/ScriptEditor.d.ts.map +1 -1
  88. package/dist/types/src/components/ScriptSettings/ScriptSettings.d.ts +6 -0
  89. package/dist/types/src/components/ScriptSettings/ScriptSettings.d.ts.map +1 -0
  90. package/dist/types/src/components/ScriptSettings/index.d.ts +2 -0
  91. package/dist/types/src/components/ScriptSettings/index.d.ts.map +1 -0
  92. package/dist/types/src/components/Toolbar/Toolbar.d.ts +23 -0
  93. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  94. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +14 -0
  95. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  96. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  97. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  98. package/dist/types/src/components/{TypescriptEditor.d.ts → TypescriptEditor/TypescriptEditor.d.ts} +1 -2
  99. package/dist/types/src/components/TypescriptEditor/TypescriptEditor.d.ts.map +1 -0
  100. package/dist/types/src/components/TypescriptEditor/TypescriptEditor.stories.d.ts +6 -0
  101. package/dist/types/src/components/TypescriptEditor/TypescriptEditor.stories.d.ts.map +1 -0
  102. package/dist/types/src/components/TypescriptEditor/index.d.ts +2 -0
  103. package/dist/types/src/components/TypescriptEditor/index.d.ts.map +1 -0
  104. package/dist/types/src/components/index.d.ts +3 -3
  105. package/dist/types/src/components/index.d.ts.map +1 -1
  106. package/dist/types/src/edge/functions.d.ts +7 -0
  107. package/dist/types/src/edge/functions.d.ts.map +1 -1
  108. package/dist/types/src/meta.d.ts +4 -9
  109. package/dist/types/src/meta.d.ts.map +1 -1
  110. package/dist/types/src/templates/chat.d.ts +6 -0
  111. package/dist/types/src/templates/chat.d.ts.map +1 -0
  112. package/dist/types/src/templates/chess.d.ts +6 -0
  113. package/dist/types/src/templates/chess.d.ts.map +1 -0
  114. package/dist/types/src/templates/echo.d.ts +6 -0
  115. package/dist/types/src/templates/echo.d.ts.map +1 -0
  116. package/dist/types/src/templates/email.d.ts +6 -0
  117. package/dist/types/src/templates/email.d.ts.map +1 -0
  118. package/dist/types/src/templates/forex.d.ts +6 -0
  119. package/dist/types/src/templates/forex.d.ts.map +1 -0
  120. package/dist/types/src/templates.d.ts +8 -0
  121. package/dist/types/src/templates.d.ts.map +1 -0
  122. package/dist/types/src/translations.d.ts +2 -0
  123. package/dist/types/src/translations.d.ts.map +1 -1
  124. package/dist/types/src/types/script.d.ts +4 -4
  125. package/dist/types/src/types/types.d.ts +4 -3
  126. package/dist/types/src/types/types.d.ts.map +1 -1
  127. package/package.json +59 -64
  128. package/src/ScriptPlugin.tsx +39 -41
  129. package/src/bundler/bundler.test.ts +12 -2
  130. package/src/components/AutomationPanel/AutomationPanel.tsx +101 -0
  131. package/src/components/AutomationPanel/index.ts +9 -0
  132. package/src/components/{DebugPanel.tsx → DebugPanel/DebugPanel.tsx} +83 -78
  133. package/src/components/DebugPanel/index.ts +5 -0
  134. package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/FrameContainer.tsx +1 -1
  135. package/src/components/ScriptBlock/ScriptBlock.stories.tsx +15 -14
  136. package/src/components/ScriptBlock/ScriptBlock.tsx +23 -51
  137. package/src/components/ScriptContainer.tsx +22 -0
  138. package/src/components/ScriptEditor.tsx +58 -55
  139. package/src/components/ScriptSettings/ScriptSettings.tsx +47 -0
  140. package/src/components/ScriptSettings/index.ts +5 -0
  141. package/src/components/Toolbar/Toolbar.stories.tsx +30 -0
  142. package/src/components/Toolbar/Toolbar.tsx +177 -0
  143. package/src/components/{ScriptBlock/Splitter → Toolbar}/index.ts +1 -1
  144. package/src/components/{TypescriptEditor.stories.tsx → TypescriptEditor/TypescriptEditor.stories.tsx} +17 -35
  145. package/src/components/{TypescriptEditor.tsx → TypescriptEditor/TypescriptEditor.tsx} +1 -3
  146. package/src/components/TypescriptEditor/index.ts +5 -0
  147. package/src/components/index.ts +5 -3
  148. package/src/edge/functions.ts +19 -0
  149. package/src/meta.ts +15 -0
  150. package/src/templates/chat.ts +102 -0
  151. package/src/templates/chess.ts +41 -0
  152. package/src/templates/echo.ts +15 -0
  153. package/src/templates/email.ts +38 -0
  154. package/src/templates/forex.ts +21 -0
  155. package/src/templates.ts +54 -0
  156. package/src/translations.ts +2 -0
  157. package/src/types/types.ts +9 -5
  158. package/dist/lib/browser/ScriptEditor-5Y3EROG3.mjs +0 -513
  159. package/dist/lib/browser/ScriptEditor-5Y3EROG3.mjs.map +0 -7
  160. package/dist/lib/browser/TypescriptEditor-GPE72Y6I.mjs +0 -9
  161. package/dist/lib/browser/chunk-DL5XQRQM.mjs.map +0 -7
  162. package/dist/lib/browser/chunk-JBACGDOJ.mjs +0 -21
  163. package/dist/lib/browser/chunk-JBACGDOJ.mjs.map +0 -7
  164. package/dist/lib/browser/chunk-MRGOT5RF.mjs +0 -78
  165. package/dist/lib/browser/chunk-MRGOT5RF.mjs.map +0 -7
  166. package/dist/lib/node/ScriptEditor-YCUQFWQM.cjs +0 -526
  167. package/dist/lib/node/ScriptEditor-YCUQFWQM.cjs.map +0 -7
  168. package/dist/lib/node/TypescriptEditor-M3WWFIP2.cjs.map +0 -7
  169. package/dist/lib/node/chunk-4GTXIELQ.cjs.map +0 -7
  170. package/dist/lib/node/chunk-4VMNBXWU.cjs +0 -54
  171. package/dist/lib/node/chunk-4VMNBXWU.cjs.map +0 -7
  172. package/dist/lib/node/chunk-IMLDKD5Y.cjs +0 -111
  173. package/dist/lib/node/chunk-IMLDKD5Y.cjs.map +0 -7
  174. package/dist/lib/node/chunk-PFH3ROEH.cjs.map +0 -7
  175. package/dist/types/src/components/DebugPanel.d.ts.map +0 -1
  176. package/dist/types/src/components/ScriptBlock/FrameContainer/FrameContainer.d.ts.map +0 -1
  177. package/dist/types/src/components/ScriptBlock/FrameContainer/frame.d.ts.map +0 -1
  178. package/dist/types/src/components/ScriptBlock/FrameContainer/index.d.ts.map +0 -1
  179. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.d.ts +0 -15
  180. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.d.ts.map +0 -1
  181. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.stories.d.ts +0 -13
  182. package/dist/types/src/components/ScriptBlock/Splitter/Splitter.stories.d.ts.map +0 -1
  183. package/dist/types/src/components/ScriptBlock/Splitter/index.d.ts +0 -2
  184. package/dist/types/src/components/ScriptBlock/Splitter/index.d.ts.map +0 -1
  185. package/dist/types/src/components/Toolbar.d.ts +0 -13
  186. package/dist/types/src/components/Toolbar.d.ts.map +0 -1
  187. package/dist/types/src/components/Toolbar.stories.d.ts +0 -14
  188. package/dist/types/src/components/Toolbar.stories.d.ts.map +0 -1
  189. package/dist/types/src/components/TypescriptEditor.d.ts.map +0 -1
  190. package/dist/types/src/components/TypescriptEditor.stories.d.ts +0 -10
  191. package/dist/types/src/components/TypescriptEditor.stories.d.ts.map +0 -1
  192. package/src/components/ScriptBlock/Splitter/Splitter.stories.tsx +0 -37
  193. package/src/components/ScriptBlock/Splitter/Splitter.tsx +0 -52
  194. package/src/components/Toolbar.stories.tsx +0 -17
  195. package/src/components/Toolbar.tsx +0 -147
  196. package/src/meta.tsx +0 -19
  197. /package/dist/lib/{browser/TypescriptEditor-GPE72Y6I.mjs.map → node-esm/edge/index.mjs.map} +0 -0
  198. /package/dist/types/src/components/{ScriptBlock/FrameContainer → FrameContainer}/frame.d.ts +0 -0
  199. /package/dist/types/src/components/{ScriptBlock/FrameContainer → FrameContainer}/index.d.ts +0 -0
  200. /package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/frame.html +0 -0
  201. /package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/frame.tsx +0 -0
  202. /package/src/components/{ScriptBlock/FrameContainer → FrameContainer}/index.ts +0 -0
@@ -4,57 +4,39 @@
4
4
 
5
5
  import '@dxos-theme';
6
6
 
7
+ import { type Meta } from '@storybook/react';
7
8
  import React, { useMemo } from 'react';
8
9
 
9
- import { createDocAccessor, createEchoObject } from '@dxos/react-client/echo';
10
+ import { createDocAccessor, createObject } from '@dxos/react-client/echo';
10
11
  import { createDataExtensions } from '@dxos/react-ui-editor';
11
12
 
12
13
  import { TypescriptEditor } from './TypescriptEditor';
14
+ import { templates } from '../../templates';
13
15
 
14
- // TODO(burdon): Typescript.
15
- // TODO(burdon): Effect schema.
16
- // TODO(burdon): JSX.
17
- // TODO(burdon): react-buddy for storybook?
18
-
19
- // Marijn
16
+ // TODO(burdon): Features:
20
17
  // - language support for S
21
18
  // - hierarchical editor (DND)
22
19
  // - virtual document image rendering
23
20
  // - mobile rendering error
24
21
 
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
- ];
43
-
44
- const Story = () => {
45
- const object = useMemo(() => createEchoObject({ content: examples[0] }), []);
22
+ // TODO(burdon): JSX.
23
+ // TODO(burdon): Effect schema.
24
+ // TODO(burdon): react-buddy for storybook?
25
+
26
+ const DefaultStory = () => {
27
+ const object = useMemo(() => createObject({ content: templates[0].source }), []);
46
28
  const initialValue = useMemo(() => object.content, [object]);
47
29
  const accessor = useMemo(() => createDocAccessor(object, ['content']), [object]);
48
30
  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} />;
31
+ return <TypescriptEditor id='test' initialValue={initialValue} extensions={extensions} />;
52
32
  };
53
33
 
54
- export default {
55
- title: 'plugin-script/TypescriptEditor',
34
+ export const Default = {};
35
+
36
+ const meta: Meta = {
37
+ title: 'plugins/plugin-script/TypescriptEditor',
56
38
  component: TypescriptEditor,
57
- render: Story,
39
+ render: DefaultStory,
58
40
  };
59
41
 
60
- export const Default = {};
42
+ export default meta;
@@ -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';
@@ -2,7 +2,9 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import React from 'react';
5
+ import { lazy } 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 AutomationPanel = lazy(() => import('./AutomationPanel'));
10
+ export const ScriptContainer = lazy(() => import('./ScriptContainer'));
@@ -9,6 +9,7 @@ import { type Config } from '@dxos/client';
9
9
  import { type Halo } from '@dxos/client-protocol';
10
10
  import { type ObjectMeta } from '@dxos/echo-schema';
11
11
  import { invariant } from '@dxos/invariant';
12
+ import type { SpaceId } from '@dxos/keys';
12
13
  import { log } from '@dxos/log';
13
14
  import { type Credential } from '@dxos/protocols/proto/dxos/halo/credentials';
14
15
 
@@ -17,6 +18,7 @@ import { matchServiceCredential } from './hub-protocol';
17
18
 
18
19
  // TODO: use URL scheme for source?
19
20
  const USERFUNCTIONS_META_KEY = 'dxos.org/service/function';
21
+ export const USERFUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';
20
22
  const USERFUNCTIONS_CREDENTIAL_CAPABILITY = 'composer:beta';
21
23
 
22
24
  // TODO: Synchronize API types with server
@@ -132,3 +134,20 @@ const presentationForIdentity = async ({ halo, timeout }: { halo: Halo; timeout?
132
134
 
133
135
  return { Authorization: `Bearer ${codec.encode(presentation)}` };
134
136
  };
137
+
138
+ export type InvocationOptions = {
139
+ spaceId?: SpaceId;
140
+ subjectId?: string;
141
+ };
142
+
143
+ export const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
144
+ const baseUrl = new URL('functions/', edgeUrl);
145
+
146
+ // Leading slashes cause the URL to be treated as an absolute path.
147
+ const relativeUrl = functionUrl.replace(/^\//, '');
148
+ const url = new URL(`./${relativeUrl}`, baseUrl.toString());
149
+ options.spaceId && url.searchParams.set('spaceId', options.spaceId);
150
+ options.subjectId && url.searchParams.set('subjectId', options.subjectId);
151
+ url.protocol = 'https';
152
+ return url.toString();
153
+ };
package/src/meta.ts ADDED
@@ -0,0 +1,15 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type PluginMeta } from '@dxos/app-framework';
6
+
7
+ export const SCRIPT_PLUGIN = 'dxos.org/plugin/script';
8
+
9
+ export default {
10
+ id: SCRIPT_PLUGIN,
11
+ name: 'Scripts',
12
+ description: 'Distributed functions.',
13
+ tags: ['experimental'],
14
+ icon: 'ph--code--regular',
15
+ } satisfies PluginMeta;
@@ -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;