@dxos/plugin-script 0.8.1-staging.9eaf14f → 0.8.2-main.10c050d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/DeploymentDialog-NLWQHNKE.mjs +12 -0
- package/dist/lib/browser/ScriptContainer-Q2K5FJSC.mjs +272 -0
- package/dist/lib/browser/ScriptContainer-Q2K5FJSC.mjs.map +7 -0
- package/dist/lib/browser/{app-graph-builder-RRLY67QC.mjs → app-graph-builder-V3HPXJAD.mjs} +10 -10
- package/dist/lib/browser/app-graph-builder-V3HPXJAD.mjs.map +7 -0
- package/dist/lib/browser/{artifact-definition-WXFYD2V5.mjs → artifact-definition-L3AIY4XA.mjs} +45 -48
- package/dist/lib/browser/artifact-definition-L3AIY4XA.mjs.map +7 -0
- package/dist/lib/browser/{chunk-MQZQB5VD.mjs → chunk-6T64VQHF.mjs} +10 -10
- package/dist/lib/browser/chunk-6T64VQHF.mjs.map +7 -0
- package/dist/lib/browser/{chunk-WSK3YQXI.mjs → chunk-6ZHHWU6S.mjs} +4 -3
- package/dist/lib/browser/{chunk-WSK3YQXI.mjs.map → chunk-6ZHHWU6S.mjs.map} +2 -2
- package/dist/lib/browser/chunk-74DEDPHT.mjs +530 -0
- package/dist/lib/browser/chunk-74DEDPHT.mjs.map +7 -0
- package/dist/lib/browser/chunk-BTEFFA4C.mjs +153 -0
- package/dist/lib/browser/chunk-BTEFFA4C.mjs.map +7 -0
- package/dist/lib/browser/{chunk-KLEOGVXY.mjs → chunk-JQOAZJTK.mjs} +87 -156
- package/dist/lib/browser/chunk-JQOAZJTK.mjs.map +7 -0
- package/dist/lib/browser/chunk-M6QMJBKQ.mjs +87 -0
- package/dist/lib/browser/chunk-M6QMJBKQ.mjs.map +7 -0
- package/dist/lib/browser/chunk-WB3X5Z7Q.mjs +40 -0
- package/dist/lib/browser/chunk-WB3X5Z7Q.mjs.map +7 -0
- package/dist/lib/browser/{chunk-4VD3K3PY.mjs → chunk-YSQMXTYO.mjs} +3 -3
- package/dist/lib/{node-esm/chunk-EB5DSAD4.mjs.map → browser/chunk-YSQMXTYO.mjs.map} +1 -1
- package/dist/lib/browser/{compiler-56LTBEQB.mjs → compiler-W4BYJYTL.mjs} +3 -3
- package/dist/lib/browser/compiler-W4BYJYTL.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +17 -16
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/{intent-resolver-WCVXKRBR.mjs → intent-resolver-U5YRUE7W.mjs} +14 -14
- package/dist/lib/browser/intent-resolver-U5YRUE7W.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/{react-surface-WXZVPNGI.mjs → react-surface-3MSPT54G.mjs} +12 -12
- package/dist/lib/browser/react-surface-3MSPT54G.mjs.map +7 -0
- package/dist/lib/browser/{settings-Z7B37V2I.mjs → settings-FU7NTENS.mjs} +5 -5
- package/dist/lib/browser/settings-FU7NTENS.mjs.map +7 -0
- package/dist/lib/browser/types.mjs +2 -2
- package/dist/lib/node/{DeploymentDialog-O4MU4OMF.cjs → DeploymentDialog-PC5AL3BB.cjs} +10 -10
- package/dist/lib/node/DeploymentDialog-PC5AL3BB.cjs.map +7 -0
- package/dist/lib/node/ScriptContainer-56SH6R24.cjs +289 -0
- package/dist/lib/node/ScriptContainer-56SH6R24.cjs.map +7 -0
- package/dist/lib/node/{app-graph-builder-6I2BWD46.cjs → app-graph-builder-YE4OM4OC.cjs} +17 -17
- package/dist/lib/node/app-graph-builder-YE4OM4OC.cjs.map +7 -0
- package/dist/lib/node/{artifact-definition-6NVXEHJD.cjs → artifact-definition-6BD6CVOV.cjs} +55 -58
- package/dist/lib/node/artifact-definition-6BD6CVOV.cjs.map +7 -0
- package/dist/lib/node/chunk-5LHP5EUJ.cjs +557 -0
- package/dist/lib/node/chunk-5LHP5EUJ.cjs.map +7 -0
- package/dist/lib/node/chunk-BYLHJ26I.cjs +91 -0
- package/dist/lib/node/chunk-BYLHJ26I.cjs.map +7 -0
- package/dist/lib/node/{chunk-XFZPWFEG.cjs → chunk-HCRW6T3G.cjs} +19 -20
- package/dist/lib/node/chunk-HCRW6T3G.cjs.map +7 -0
- package/dist/lib/node/{chunk-F4DP7WON.cjs → chunk-NRZJV5SV.cjs} +93 -160
- package/dist/lib/node/chunk-NRZJV5SV.cjs.map +7 -0
- package/dist/lib/node/{chunk-YJQZHKKA.cjs → chunk-QG2X6J74.cjs} +7 -6
- package/dist/lib/node/{chunk-YJQZHKKA.cjs.map → chunk-QG2X6J74.cjs.map} +2 -2
- package/dist/lib/node/chunk-RARNZD62.cjs +179 -0
- package/dist/lib/node/chunk-RARNZD62.cjs.map +7 -0
- package/dist/lib/node/{chunk-VYBEBLDO.cjs → chunk-V6L73D7S.cjs} +14 -14
- package/dist/lib/node/chunk-V6L73D7S.cjs.map +7 -0
- package/dist/lib/node/{chunk-JICWAOWZ.cjs → chunk-ZEV3VARQ.cjs} +7 -7
- package/dist/lib/node/{chunk-JICWAOWZ.cjs.map → chunk-ZEV3VARQ.cjs.map} +1 -1
- package/dist/lib/node/{compiler-W2RU3M7U.cjs → compiler-TFSJRLL4.cjs} +7 -7
- package/dist/lib/node/{compiler-W2RU3M7U.cjs.map → compiler-TFSJRLL4.cjs.map} +3 -3
- package/dist/lib/node/index.cjs +46 -45
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/{intent-resolver-J4NBDNFH.cjs → intent-resolver-7SF26VGC.cjs} +21 -21
- package/dist/lib/node/intent-resolver-7SF26VGC.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/{react-surface-27KTUBIZ.cjs → react-surface-L6RS7BT2.cjs} +38 -38
- package/dist/lib/node/react-surface-L6RS7BT2.cjs.map +7 -0
- package/dist/lib/node/{settings-EI6VPNKN.cjs → settings-RRCQ7ZQD.cjs} +9 -9
- package/dist/lib/node/settings-RRCQ7ZQD.cjs.map +7 -0
- package/dist/lib/node/types.cjs +5 -5
- package/dist/lib/node/types.cjs.map +1 -1
- package/dist/lib/node-esm/{DeploymentDialog-SSQSWLHX.mjs → DeploymentDialog-QHB2BGNJ.mjs} +5 -5
- package/dist/lib/node-esm/ScriptContainer-6RCZ43TI.mjs +273 -0
- package/dist/lib/node-esm/ScriptContainer-6RCZ43TI.mjs.map +7 -0
- package/dist/lib/node-esm/{app-graph-builder-H5XLRVX3.mjs → app-graph-builder-4CN7URJQ.mjs} +10 -10
- package/dist/lib/node-esm/app-graph-builder-4CN7URJQ.mjs.map +7 -0
- package/dist/lib/node-esm/{artifact-definition-KQ64AYBP.mjs → artifact-definition-PPV2XKU7.mjs} +45 -48
- package/dist/lib/node-esm/artifact-definition-PPV2XKU7.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-25GOLUXY.mjs +154 -0
- package/dist/lib/node-esm/chunk-25GOLUXY.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-EB5DSAD4.mjs → chunk-2KJYPUQD.mjs} +3 -3
- package/dist/lib/{browser/chunk-4VD3K3PY.mjs.map → node-esm/chunk-2KJYPUQD.mjs.map} +1 -1
- package/dist/lib/node-esm/{chunk-F4HA5C34.mjs → chunk-47MABIVX.mjs} +10 -10
- package/dist/lib/node-esm/chunk-47MABIVX.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-FDEAVT67.mjs → chunk-AEB5TPEW.mjs} +4 -3
- package/dist/lib/node-esm/{chunk-FDEAVT67.mjs.map → chunk-AEB5TPEW.mjs.map} +2 -2
- package/dist/lib/node-esm/chunk-KOO76P2Y.mjs +531 -0
- package/dist/lib/node-esm/chunk-KOO76P2Y.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-UESZKXLR.mjs +41 -0
- package/dist/lib/node-esm/chunk-UESZKXLR.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-PBGVTGL2.mjs → chunk-WD7OFSM3.mjs} +87 -156
- package/dist/lib/node-esm/chunk-WD7OFSM3.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-Z44O2YIK.mjs +89 -0
- package/dist/lib/node-esm/chunk-Z44O2YIK.mjs.map +7 -0
- package/dist/lib/node-esm/{compiler-VT5C2Z3T.mjs → compiler-7PQ4D43R.mjs} +3 -3
- package/dist/lib/{browser/compiler-56LTBEQB.mjs.map → node-esm/compiler-7PQ4D43R.mjs.map} +3 -3
- package/dist/lib/node-esm/index.mjs +17 -16
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/{intent-resolver-PDZ2A26X.mjs → intent-resolver-3APO7YDF.mjs} +14 -14
- package/dist/lib/node-esm/intent-resolver-3APO7YDF.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/{react-surface-UCUVDVSH.mjs → react-surface-S5LHD3IA.mjs} +12 -12
- package/dist/lib/node-esm/react-surface-S5LHD3IA.mjs.map +7 -0
- package/dist/lib/node-esm/{settings-HU5UEFRF.mjs → settings-NKNC32QC.mjs} +5 -5
- package/dist/lib/node-esm/settings-NKNC32QC.mjs.map +7 -0
- package/dist/lib/node-esm/types.mjs +2 -2
- package/dist/types/src/ScriptPlugin.d.ts.map +1 -1
- package/dist/types/src/capabilities/app-graph-builder.d.ts +2 -179
- package/dist/types/src/capabilities/app-graph-builder.d.ts.map +1 -1
- package/dist/types/src/capabilities/artifact-definition.d.ts.map +1 -1
- package/dist/types/src/capabilities/index.d.ts +1 -177
- package/dist/types/src/capabilities/index.d.ts.map +1 -1
- package/dist/types/src/compiler/compiler.d.ts.map +1 -1
- package/dist/types/src/components/DeploymentDialog/DeploymentDialog.d.ts +2 -2
- package/dist/types/src/components/DeploymentDialog/DeploymentDialog.d.ts.map +1 -1
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -1
- package/dist/types/src/components/ScriptContainer.d.ts +1 -1
- package/dist/types/src/components/ScriptContainer.d.ts.map +1 -1
- package/dist/types/src/components/ScriptToolbar/ScriptToolbar.d.ts +1 -1
- package/dist/types/src/components/ScriptToolbar/ScriptToolbar.d.ts.map +1 -1
- package/dist/types/src/components/Settings/ScriptObjectSettings.d.ts +1 -1
- package/dist/types/src/components/Settings/ScriptObjectSettings.d.ts.map +1 -1
- package/dist/types/src/components/Settings/ScriptPluginSettings.d.ts.map +1 -1
- package/dist/types/src/components/TestPanel/TestPanel.d.ts.map +1 -1
- package/dist/types/src/components/TypescriptEditor/TypescriptEditor.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -1
- package/dist/types/src/hooks/deploy.d.ts +22 -19
- package/dist/types/src/hooks/deploy.d.ts.map +1 -1
- package/dist/types/src/hooks/format.d.ts +2 -5
- package/dist/types/src/hooks/format.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +0 -1
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/template.d.ts +2 -5
- package/dist/types/src/hooks/template.d.ts.map +1 -1
- package/dist/types/src/hooks/useCreateAndDeployScriptTemplates.d.ts.map +1 -1
- package/dist/types/src/hooks/useToolbarState.d.ts +1 -1
- package/dist/types/src/hooks/useToolbarState.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/templates/chat.d.ts +1 -1
- package/dist/types/src/templates/chat.d.ts.map +1 -1
- package/dist/types/src/templates/chess.d.ts.map +1 -1
- package/dist/types/src/templates/data-generator.d.ts +0 -7
- package/dist/types/src/templates/data-generator.d.ts.map +1 -1
- package/dist/types/src/templates/discord.d.ts.map +1 -1
- package/dist/types/src/templates/echo.d.ts +1 -1
- package/dist/types/src/templates/echo.d.ts.map +1 -1
- package/dist/types/src/templates/email.d.ts +1 -1
- package/dist/types/src/templates/email.d.ts.map +1 -1
- package/dist/types/src/templates/forex-effect.d.ts.map +1 -1
- package/dist/types/src/templates/forex.d.ts.map +1 -1
- package/dist/types/src/templates/gmail.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +2 -110
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +27 -26
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util/deploy.d.ts.map +1 -1
- package/dist/types/src/util/functions.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +59 -53
- package/src/ScriptPlugin.tsx +4 -2
- package/src/capabilities/app-graph-builder.ts +46 -29
- package/src/capabilities/artifact-definition.ts +40 -38
- package/src/capabilities/intent-resolver.ts +7 -7
- package/src/capabilities/react-surface.tsx +5 -5
- package/src/capabilities/settings.ts +2 -2
- package/src/compiler/compiler.ts +3 -3
- package/src/components/DeploymentDialog/DeploymentDialog.stories.tsx +1 -1
- package/src/components/DeploymentDialog/DeploymentDialog.tsx +2 -2
- package/src/components/ScriptContainer.tsx +2 -2
- package/src/components/ScriptToolbar/ScriptToolbar.stories.tsx +1 -1
- package/src/components/ScriptToolbar/ScriptToolbar.tsx +27 -18
- package/src/components/Settings/ScriptObjectSettings.tsx +5 -5
- package/src/components/TypescriptEditor/TypescriptEditor.tsx +0 -6
- package/src/hooks/deploy.ts +61 -55
- package/src/hooks/format.ts +26 -26
- package/src/hooks/index.ts +0 -1
- package/src/hooks/template.ts +24 -32
- package/src/hooks/useToolbarState.ts +2 -2
- package/src/meta.ts +3 -2
- package/src/templates/chat.ts +4 -10
- package/src/templates/chess.ts +2 -7
- package/src/templates/data-generator.ts +20 -45
- package/src/templates/discord.ts +17 -15
- package/src/templates/echo.ts +1 -12
- package/src/templates/email.ts +8 -14
- package/src/templates/forex-effect.ts +4 -8
- package/src/templates/forex.ts +1 -5
- package/src/templates/gmail.ts +37 -28
- package/src/translations.ts +1 -1
- package/src/types.ts +13 -14
- package/src/util/deploy.ts +6 -10
- package/dist/lib/browser/DeploymentDialog-AEPYJKPO.mjs +0 -12
- package/dist/lib/browser/ScriptContainer-R25PRK2D.mjs +0 -248
- package/dist/lib/browser/ScriptContainer-R25PRK2D.mjs.map +0 -7
- package/dist/lib/browser/app-graph-builder-RRLY67QC.mjs.map +0 -7
- package/dist/lib/browser/artifact-definition-WXFYD2V5.mjs.map +0 -7
- package/dist/lib/browser/chunk-2CGZFDCR.mjs +0 -480
- package/dist/lib/browser/chunk-2CGZFDCR.mjs.map +0 -7
- package/dist/lib/browser/chunk-CSZXPLYY.mjs +0 -147
- package/dist/lib/browser/chunk-CSZXPLYY.mjs.map +0 -7
- package/dist/lib/browser/chunk-KLEOGVXY.mjs.map +0 -7
- package/dist/lib/browser/chunk-MQZQB5VD.mjs.map +0 -7
- package/dist/lib/browser/chunk-R2CHVWJ2.mjs +0 -41
- package/dist/lib/browser/chunk-R2CHVWJ2.mjs.map +0 -7
- package/dist/lib/browser/chunk-SUCBNCEQ.mjs +0 -87
- package/dist/lib/browser/chunk-SUCBNCEQ.mjs.map +0 -7
- package/dist/lib/browser/intent-resolver-WCVXKRBR.mjs.map +0 -7
- package/dist/lib/browser/react-surface-WXZVPNGI.mjs.map +0 -7
- package/dist/lib/browser/settings-Z7B37V2I.mjs.map +0 -7
- package/dist/lib/node/DeploymentDialog-O4MU4OMF.cjs.map +0 -7
- package/dist/lib/node/ScriptContainer-5KY6ZOQD.cjs +0 -267
- package/dist/lib/node/ScriptContainer-5KY6ZOQD.cjs.map +0 -7
- package/dist/lib/node/app-graph-builder-6I2BWD46.cjs.map +0 -7
- package/dist/lib/node/artifact-definition-6NVXEHJD.cjs.map +0 -7
- package/dist/lib/node/chunk-65MIJR3Q.cjs +0 -173
- package/dist/lib/node/chunk-65MIJR3Q.cjs.map +0 -7
- package/dist/lib/node/chunk-F4DP7WON.cjs.map +0 -7
- package/dist/lib/node/chunk-SLDJM45C.cjs +0 -507
- package/dist/lib/node/chunk-SLDJM45C.cjs.map +0 -7
- package/dist/lib/node/chunk-VYBEBLDO.cjs.map +0 -7
- package/dist/lib/node/chunk-X7MHLHHS.cjs +0 -91
- package/dist/lib/node/chunk-X7MHLHHS.cjs.map +0 -7
- package/dist/lib/node/chunk-XFZPWFEG.cjs.map +0 -7
- package/dist/lib/node/intent-resolver-J4NBDNFH.cjs.map +0 -7
- package/dist/lib/node/react-surface-27KTUBIZ.cjs.map +0 -7
- package/dist/lib/node/settings-EI6VPNKN.cjs.map +0 -7
- package/dist/lib/node-esm/ScriptContainer-W2F4FGDV.mjs +0 -249
- package/dist/lib/node-esm/ScriptContainer-W2F4FGDV.mjs.map +0 -7
- package/dist/lib/node-esm/app-graph-builder-H5XLRVX3.mjs.map +0 -7
- package/dist/lib/node-esm/artifact-definition-KQ64AYBP.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-4MTPYMV2.mjs +0 -481
- package/dist/lib/node-esm/chunk-4MTPYMV2.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-F4HA5C34.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-MU3JKVL2.mjs +0 -148
- package/dist/lib/node-esm/chunk-MU3JKVL2.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-PBGVTGL2.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-QDYGTY4L.mjs +0 -89
- package/dist/lib/node-esm/chunk-QDYGTY4L.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-RW2APVMV.mjs +0 -42
- package/dist/lib/node-esm/chunk-RW2APVMV.mjs.map +0 -7
- package/dist/lib/node-esm/compiler-VT5C2Z3T.mjs.map +0 -7
- package/dist/lib/node-esm/intent-resolver-PDZ2A26X.mjs.map +0 -7
- package/dist/lib/node-esm/react-surface-UCUVDVSH.mjs.map +0 -7
- package/dist/lib/node-esm/settings-HU5UEFRF.mjs.map +0 -7
- package/dist/types/src/hooks/useToolbarAction.d.ts +0 -13
- package/dist/types/src/hooks/useToolbarAction.d.ts.map +0 -1
- package/src/hooks/useToolbarAction.ts +0 -50
- /package/dist/lib/browser/{DeploymentDialog-AEPYJKPO.mjs.map → DeploymentDialog-NLWQHNKE.mjs.map} +0 -0
- /package/dist/lib/node-esm/{DeploymentDialog-SSQSWLHX.mjs.map → DeploymentDialog-QHB2BGNJ.mjs.map} +0 -0
package/src/hooks/format.ts
CHANGED
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
import { format } from 'prettier';
|
|
6
6
|
import prettierPluginEstree from 'prettier/plugins/estree';
|
|
7
7
|
import prettierPluginTypescript from 'prettier/plugins/typescript';
|
|
8
|
-
import { useCallback } from 'react';
|
|
9
8
|
|
|
10
|
-
import { type ScriptType } from '@dxos/functions
|
|
9
|
+
import { type ScriptType } from '@dxos/functions';
|
|
11
10
|
import { log } from '@dxos/log';
|
|
12
11
|
import { createMenuAction } from '@dxos/react-ui-menu';
|
|
13
12
|
|
|
@@ -15,33 +14,34 @@ import { SCRIPT_PLUGIN } from '../meta';
|
|
|
15
14
|
|
|
16
15
|
export type FormatActionProperties = { type: 'format' };
|
|
17
16
|
|
|
18
|
-
export const createFormat = () => {
|
|
19
|
-
const formatAction = createMenuAction(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
export const createFormat = (script: ScriptType) => {
|
|
18
|
+
const formatAction = createMenuAction(
|
|
19
|
+
'format',
|
|
20
|
+
async () => {
|
|
21
|
+
if (!script.source) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
script.source.target!.content = await format(script.source.target!.content, {
|
|
27
|
+
parser: 'typescript',
|
|
28
|
+
plugins: [prettierPluginEstree, prettierPluginTypescript],
|
|
29
|
+
semi: true,
|
|
30
|
+
singleQuote: true,
|
|
31
|
+
});
|
|
32
|
+
} catch (err: any) {
|
|
33
|
+
// TODO(wittjosiah): Show error in UI.
|
|
34
|
+
log.catch(err);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
label: ['format label', { ns: SCRIPT_PLUGIN }],
|
|
39
|
+
icon: 'ph--magic-wand--regular',
|
|
40
|
+
},
|
|
41
|
+
);
|
|
23
42
|
|
|
24
43
|
return {
|
|
25
44
|
nodes: [formatAction],
|
|
26
45
|
edges: [{ source: 'root', target: 'format' }],
|
|
27
46
|
};
|
|
28
47
|
};
|
|
29
|
-
|
|
30
|
-
export const useFormatHandler = ({ script }: { script: ScriptType }) =>
|
|
31
|
-
useCallback(async () => {
|
|
32
|
-
if (!script.source) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
script.source.target!.content = await format(script.source.target!.content, {
|
|
38
|
-
parser: 'typescript',
|
|
39
|
-
plugins: [prettierPluginEstree, prettierPluginTypescript],
|
|
40
|
-
semi: true,
|
|
41
|
-
singleQuote: true,
|
|
42
|
-
});
|
|
43
|
-
} catch (err: any) {
|
|
44
|
-
// TODO(wittjosiah): Show error in UI.
|
|
45
|
-
log.catch(err);
|
|
46
|
-
}
|
|
47
|
-
}, [script.source]);
|
package/src/hooks/index.ts
CHANGED
package/src/hooks/template.ts
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import { FUNCTIONS_PRESET_META_KEY, type ScriptType } from '@dxos/functions/types';
|
|
5
|
+
import { FUNCTIONS_PRESET_META_KEY, type ScriptType } from '@dxos/functions';
|
|
8
6
|
import { getMeta } from '@dxos/live-object';
|
|
9
7
|
import { createMenuItemGroup, createMenuAction } from '@dxos/react-ui-menu';
|
|
10
8
|
|
|
@@ -20,19 +18,34 @@ const createTemplateSelectGroup = () => {
|
|
|
20
18
|
});
|
|
21
19
|
};
|
|
22
20
|
|
|
23
|
-
const createTemplateSelectActions = () => {
|
|
21
|
+
const createTemplateSelectActions = (script: ScriptType) => {
|
|
24
22
|
return templates.map((template) => {
|
|
25
|
-
return createMenuAction<TemplateActionProperties>(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
return createMenuAction<TemplateActionProperties>(
|
|
24
|
+
`template--${template.id}`,
|
|
25
|
+
() => {
|
|
26
|
+
script.name = template.name;
|
|
27
|
+
script.source!.target!.content = template.source;
|
|
28
|
+
const metaKeys = getMeta(script).keys;
|
|
29
|
+
const oldPresetIndex = metaKeys.findIndex((key) => key.source === FUNCTIONS_PRESET_META_KEY);
|
|
30
|
+
if (oldPresetIndex >= 0) {
|
|
31
|
+
metaKeys.splice(oldPresetIndex, 1);
|
|
32
|
+
}
|
|
33
|
+
if (template.presetId) {
|
|
34
|
+
metaKeys.push({ source: FUNCTIONS_PRESET_META_KEY, id: template.presetId });
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
label: template.name,
|
|
39
|
+
value: template.id,
|
|
40
|
+
type: 'template',
|
|
41
|
+
},
|
|
42
|
+
);
|
|
30
43
|
});
|
|
31
44
|
};
|
|
32
45
|
|
|
33
|
-
export const createTemplateSelect = () => {
|
|
46
|
+
export const createTemplateSelect = (script: ScriptType) => {
|
|
34
47
|
const templateSelectGroup = createTemplateSelectGroup();
|
|
35
|
-
const templateSelectActions = createTemplateSelectActions();
|
|
48
|
+
const templateSelectActions = createTemplateSelectActions(script);
|
|
36
49
|
return {
|
|
37
50
|
nodes: [templateSelectGroup, ...templateSelectActions],
|
|
38
51
|
edges: [
|
|
@@ -41,24 +54,3 @@ export const createTemplateSelect = () => {
|
|
|
41
54
|
],
|
|
42
55
|
};
|
|
43
56
|
};
|
|
44
|
-
|
|
45
|
-
export const useTemplateSelectHandler = ({ script }: { script: ScriptType }) => {
|
|
46
|
-
return useCallback(
|
|
47
|
-
(templateId: string) => {
|
|
48
|
-
const template = templates.find((template) => template.id === templateId);
|
|
49
|
-
if (template) {
|
|
50
|
-
script.name = template.name;
|
|
51
|
-
script.source!.target!.content = template.source;
|
|
52
|
-
const metaKeys = getMeta(script).keys;
|
|
53
|
-
const oldPresetIndex = metaKeys.findIndex((key) => key.source === FUNCTIONS_PRESET_META_KEY);
|
|
54
|
-
if (oldPresetIndex >= 0) {
|
|
55
|
-
metaKeys.splice(oldPresetIndex, 1);
|
|
56
|
-
}
|
|
57
|
-
if (template.presetId) {
|
|
58
|
-
metaKeys.push({ source: FUNCTIONS_PRESET_META_KEY, id: template.presetId });
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
[script],
|
|
63
|
-
);
|
|
64
|
-
};
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { useMemo } from 'react';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { live } from '@dxos/live-object';
|
|
8
8
|
|
|
9
9
|
import { type DeployState } from './deploy';
|
|
10
10
|
|
|
@@ -12,5 +12,5 @@ export type ScriptToolbarState = Partial<DeployState>;
|
|
|
12
12
|
|
|
13
13
|
// TODO(burdon): Replace with context provider?
|
|
14
14
|
export const useToolbarState = (initialState: ScriptToolbarState = {}) => {
|
|
15
|
-
return useMemo(() =>
|
|
15
|
+
return useMemo(() => live<ScriptToolbarState>(initialState), []);
|
|
16
16
|
};
|
package/src/meta.ts
CHANGED
|
@@ -12,12 +12,13 @@ export const meta: PluginMeta = {
|
|
|
12
12
|
description:
|
|
13
13
|
'Scripts in Composer allow you to deploy custom functions that run on the edge. These functions can be referenced by your AI agent and called inside of cells in any Sheet. Use scripts to interact with API’s or external data inside of Composer.',
|
|
14
14
|
icon: 'ph--code--regular',
|
|
15
|
-
source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/
|
|
16
|
-
tags: ['
|
|
15
|
+
source: 'https://github.com/dxos/dxos/tree/main/packages/plugins/plugin-explorer',
|
|
16
|
+
tags: ['labs'],
|
|
17
17
|
screenshots: ['https://dxos.network/plugin-details-scripts-dark.png'],
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
// TODO(ZaymonFC): Configure by scopes?
|
|
21
21
|
export const defaultScriptsForIntegration: Record<string, string[]> = {
|
|
22
|
+
// TODO(wittjosiah): Also include content extraction scripts in the default set.
|
|
22
23
|
'gmail.com': ['dxos.org/script/gmail'],
|
|
23
24
|
};
|
package/src/templates/chat.ts
CHANGED
|
@@ -3,21 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
import { transformServerSentEvents } from 'https://esm.sh/@dxos/script-toolbox';
|
|
6
|
+
import { transformServerSentEvents } from 'https://esm.sh/@dxos/script-toolbox?bundle=false';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Chatbot that answers questions based on the context data.
|
|
10
10
|
*/
|
|
11
|
-
export default async ({
|
|
12
|
-
event: {
|
|
13
|
-
data: { request },
|
|
14
|
-
},
|
|
15
|
-
context: { space, ai },
|
|
16
|
-
}: any) => {
|
|
17
|
-
const textBody = await request.text();
|
|
11
|
+
export default async ({ data: { bodyText }, context: { space, ai } }: any) => {
|
|
18
12
|
let parsedRequest: any;
|
|
19
13
|
try {
|
|
20
|
-
parsedRequest = JSON.parse(
|
|
14
|
+
parsedRequest = JSON.parse(bodyText);
|
|
21
15
|
} catch (err) {
|
|
22
16
|
const answer = await ai.run('@cf/meta/llama-3.1-8b-instruct', {
|
|
23
17
|
stream: true,
|
|
@@ -29,7 +23,7 @@ export default async ({
|
|
|
29
23
|
${await getContext({ space })}
|
|
30
24
|
|
|
31
25
|
QUESTION:
|
|
32
|
-
${
|
|
26
|
+
${bodyText}
|
|
33
27
|
`,
|
|
34
28
|
});
|
|
35
29
|
return new Response(transformServerSentEvents(answer));
|
package/src/templates/chess.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
import { defineFunction, S } from 'dxos:functions';
|
|
7
7
|
// @ts-ignore
|
|
8
|
-
import { Chess } from 'https://esm.sh/chess.js@0.13.1';
|
|
8
|
+
import { Chess } from 'https://esm.sh/chess.js@0.13.1?bundle=false';
|
|
9
9
|
|
|
10
10
|
export default defineFunction({
|
|
11
11
|
description: 'Plays a random move in a chess game.',
|
|
@@ -25,12 +25,7 @@ export default defineFunction({
|
|
|
25
25
|
}),
|
|
26
26
|
}),
|
|
27
27
|
|
|
28
|
-
handler: async ({
|
|
29
|
-
event: {
|
|
30
|
-
data: { changedObjectId, player = 'b' },
|
|
31
|
-
},
|
|
32
|
-
context: { space },
|
|
33
|
-
}: any) => {
|
|
28
|
+
handler: async ({ data: { changedObjectId, player = 'b' }, context: { space } }: any) => {
|
|
34
29
|
const { pgn } = await space.db.query({ id: changedObjectId }).first();
|
|
35
30
|
const game = new Chess();
|
|
36
31
|
game.load_pgn(pgn);
|
|
@@ -2,35 +2,10 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
EchoObject,
|
|
7
|
-
defineFunction,
|
|
8
|
-
S,
|
|
9
|
-
createStatic,
|
|
10
|
-
Expando,
|
|
11
|
-
TypedObject,
|
|
12
|
-
Ref,
|
|
13
|
-
LabelAnnotationId,
|
|
14
|
-
// @ts-ignore
|
|
15
|
-
} from 'dxos:functions';
|
|
16
5
|
// @ts-ignore
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
content: S.String,
|
|
21
|
-
}) {}
|
|
22
|
-
|
|
23
|
-
export const DocumentSchema = S.Struct({
|
|
24
|
-
name: S.optional(S.String),
|
|
25
|
-
fallbackName: S.optional(S.String),
|
|
26
|
-
content: Ref(TextType),
|
|
27
|
-
threads: S.mutable(S.Array(Ref(Expando))),
|
|
28
|
-
}).annotations({
|
|
29
|
-
[LabelAnnotationId]: ['name', 'fallbackName'],
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
export const DocumentType = DocumentSchema.pipe(EchoObject('dxos.org/type/Document', '0.1.0'));
|
|
33
|
-
export type DocumentType = S.Schema.Type<typeof DocumentType>;
|
|
6
|
+
import { defineFunction, S, create, Expando } from 'dxos:functions';
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
import { randText } from 'https://esm.sh/@ngneat/falso@7.1.1?bundle=false';
|
|
34
9
|
|
|
35
10
|
export default defineFunction({
|
|
36
11
|
inputSchema: S.Struct({
|
|
@@ -41,33 +16,33 @@ export default defineFunction({
|
|
|
41
16
|
|
|
42
17
|
outputSchema: S.Struct({}),
|
|
43
18
|
|
|
44
|
-
handler: async ({
|
|
19
|
+
handler: async ({ data: { documentAmount = 1, textSize = 10, mutationAmount = 0 }, context: { space } }: any) => {
|
|
45
20
|
await space.db.graph.schemaRegistry.addSchema([Expando]);
|
|
21
|
+
const result: Record<string, any> = {};
|
|
22
|
+
const objects: Expando[] = [];
|
|
46
23
|
|
|
47
|
-
|
|
48
|
-
// const rootCollection = await space.properties[COLLECTION_TYPENAME]?.load();
|
|
24
|
+
// Create objects.
|
|
49
25
|
for (let i = 0; i < documentAmount; i++) {
|
|
50
26
|
const obj = space.db.add(
|
|
51
|
-
|
|
27
|
+
create(Expando, {
|
|
52
28
|
name: 'scriptGenerated',
|
|
53
|
-
content:
|
|
29
|
+
content: randText({ charCount: textSize }),
|
|
54
30
|
}),
|
|
55
31
|
);
|
|
56
|
-
// rootCollection.objects.push(obj);
|
|
57
32
|
objects.push(obj);
|
|
33
|
+
const objectResult: any = {};
|
|
34
|
+
// Mutate the object.
|
|
35
|
+
for (let i = 0; i < mutationAmount; i++) {
|
|
36
|
+
obj.content = randText({ charCount: textSize });
|
|
37
|
+
}
|
|
38
|
+
objectResult.mutations = mutationAmount;
|
|
39
|
+
objectResult.contentLength = obj.content.length;
|
|
40
|
+
result[obj.id] = objectResult;
|
|
58
41
|
}
|
|
59
42
|
|
|
60
|
-
|
|
61
|
-
const obj = objects[Math.floor(Math.random() * objects.length)];
|
|
62
|
-
obj.content = randParagraph({ length: textSize });
|
|
63
|
-
}
|
|
64
|
-
|
|
43
|
+
// Flush and wait for data to propagate.
|
|
65
44
|
await space.db.flush();
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
await space.db.query({ id: obj.id }).first();
|
|
69
|
-
console.log('obj', obj.id);
|
|
70
|
-
}
|
|
71
|
-
return 'OK';
|
|
45
|
+
await new Promise((resolve) => setTimeout(resolve, 10_000));
|
|
46
|
+
return result;
|
|
72
47
|
},
|
|
73
48
|
});
|
package/src/templates/discord.ts
CHANGED
|
@@ -3,23 +3,30 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
import {
|
|
6
|
+
import { create, defineFunction, DXN, EchoObject, Filter, ObjectId, S } from 'dxos:functions';
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
import { FetchHttpClient } from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.14.21&bundle=false';
|
|
7
9
|
import {
|
|
8
|
-
|
|
10
|
+
DiscordConfig,
|
|
11
|
+
DiscordREST,
|
|
12
|
+
DiscordRESTMemoryLive,
|
|
9
13
|
// @ts-ignore
|
|
10
|
-
} from 'https://esm.sh
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
import { DiscordConfig, DiscordREST, DiscordRESTMemoryLive } from 'https://esm.sh/dfx@0.113.0?deps=effect@3.13.3';
|
|
14
|
+
} from 'https://esm.sh/dfx@0.113.0?deps=effect@3.14.21&bundle=false';
|
|
13
15
|
// @ts-ignore
|
|
14
|
-
import { Effect, Config, Redacted, Ref } from 'https://esm.sh/effect@3.
|
|
16
|
+
import { Effect, Config, Redacted, Ref } from 'https://esm.sh/effect@3.14.21?bundle=false';
|
|
15
17
|
|
|
16
18
|
const MessageSchema = S.Struct({
|
|
17
|
-
id:
|
|
19
|
+
id: ObjectId,
|
|
18
20
|
foreignId: S.Any, // bigint?
|
|
19
21
|
from: S.String,
|
|
20
22
|
created: S.String,
|
|
21
23
|
content: S.String,
|
|
22
|
-
}).pipe(
|
|
24
|
+
}).pipe(
|
|
25
|
+
EchoObject({
|
|
26
|
+
typename: 'example.com/type/Message',
|
|
27
|
+
version: '0.1.0',
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
23
30
|
|
|
24
31
|
const DEFAULT_AFTER = 1704067200; // 2024-01-01
|
|
25
32
|
|
|
@@ -42,12 +49,7 @@ export default defineFunction({
|
|
|
42
49
|
newMessages: S.Number,
|
|
43
50
|
}),
|
|
44
51
|
|
|
45
|
-
handler: ({
|
|
46
|
-
event: {
|
|
47
|
-
data: { channelId, queueId, after = DEFAULT_AFTER, pageSize = 5 },
|
|
48
|
-
},
|
|
49
|
-
context: { space },
|
|
50
|
-
}: any) =>
|
|
52
|
+
handler: ({ data: { channelId, queueId, after = DEFAULT_AFTER, pageSize = 5 }, context: { space } }: any) =>
|
|
51
53
|
Effect.gen(function* () {
|
|
52
54
|
const { token } = yield* Effect.tryPromise({
|
|
53
55
|
try: () => space.db.query(Filter.typename('dxos.org/type/AccessToken', { source: 'discord.com' })).first(),
|
|
@@ -73,7 +75,7 @@ export default defineFunction({
|
|
|
73
75
|
const messages = yield* rest.getChannelMessages(channelId, options).pipe((res: any) => res.json);
|
|
74
76
|
const queueMessages = messages
|
|
75
77
|
.map((message: any) =>
|
|
76
|
-
|
|
78
|
+
create(MessageSchema, {
|
|
77
79
|
id: ObjectId.random(),
|
|
78
80
|
foreignId: message.id,
|
|
79
81
|
from: message.author.username,
|
package/src/templates/echo.ts
CHANGED
|
@@ -5,15 +5,4 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* Echos the request data.
|
|
7
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 ?? {}), {
|
|
15
|
-
headers: {
|
|
16
|
-
'Content-Type': 'application/json',
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
};
|
|
8
|
+
export default async ({ data }: any) => data;
|
package/src/templates/email.ts
CHANGED
|
@@ -6,15 +6,9 @@
|
|
|
6
6
|
* Appends a message to the end of a Document configured through a trigger.
|
|
7
7
|
* Trigger must have a targetDocumentId field set in Meta.
|
|
8
8
|
*/
|
|
9
|
-
export default async ({
|
|
10
|
-
|
|
11
|
-
data: { request },
|
|
12
|
-
},
|
|
13
|
-
context: { space },
|
|
14
|
-
}: any) => {
|
|
15
|
-
const { data, trigger } = (await request.json()) as RequestPayload;
|
|
9
|
+
export default async ({ data: { bodyText }, context: { space } }: any) => {
|
|
10
|
+
const { email, targetDocumentId: documentId } = JSON.parse(bodyText) as RequestPayload;
|
|
16
11
|
|
|
17
|
-
const documentId = trigger.meta.targetDocumentId;
|
|
18
12
|
const document = await space.db.query({ id: documentId }, { format: 'plain' }).first();
|
|
19
13
|
|
|
20
14
|
const contentId = document.content['/'].split(':')[3];
|
|
@@ -23,11 +17,11 @@ export default async ({
|
|
|
23
17
|
documentContent.content,
|
|
24
18
|
'',
|
|
25
19
|
'---',
|
|
26
|
-
`**From:** ${
|
|
27
|
-
`**Subject:** ${
|
|
28
|
-
`**Date:** ${
|
|
20
|
+
`**From:** ${email.from}`,
|
|
21
|
+
`**Subject:** ${email.subject}`,
|
|
22
|
+
`**Date:** ${email.created}`,
|
|
29
23
|
'',
|
|
30
|
-
|
|
24
|
+
email.body,
|
|
31
25
|
].join('\n');
|
|
32
26
|
|
|
33
27
|
await space.db.update({ id: contentId }, { content: modifiedContent });
|
|
@@ -36,6 +30,6 @@ export default async ({
|
|
|
36
30
|
};
|
|
37
31
|
|
|
38
32
|
type RequestPayload = {
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
email: { from: string; subject: string; created: string; body: string };
|
|
34
|
+
targetDocumentId: string;
|
|
41
35
|
};
|
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
import {
|
|
6
|
+
import { S, defineFunction } from 'dxos:functions';
|
|
7
7
|
import {
|
|
8
8
|
HttpClient,
|
|
9
9
|
HttpClientRequest,
|
|
10
10
|
FetchHttpClient,
|
|
11
11
|
// @ts-ignore
|
|
12
|
-
} from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.
|
|
12
|
+
} from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.14.21&bundle=false';
|
|
13
13
|
// @ts-ignore
|
|
14
|
-
import { Effect, Schedule } from 'https://esm.sh/effect@3.
|
|
14
|
+
import { Effect, Schedule } from 'https://esm.sh/effect@3.14.21?bundle=false';
|
|
15
15
|
|
|
16
16
|
export default defineFunction({
|
|
17
17
|
description: 'Returns the exchange rate between two currencies.',
|
|
@@ -23,11 +23,7 @@ export default defineFunction({
|
|
|
23
23
|
|
|
24
24
|
outputSchema: S.String.annotations({ description: 'The exchange rate between the two currencies' }),
|
|
25
25
|
|
|
26
|
-
handler: async ({
|
|
27
|
-
event: {
|
|
28
|
-
data: { from, to },
|
|
29
|
-
},
|
|
30
|
-
}: any) =>
|
|
26
|
+
handler: async ({ data: { from, to } }: any) =>
|
|
31
27
|
Effect.gen(function* () {
|
|
32
28
|
const res = yield* HttpClientRequest.get(`https://free.ratesdb.com/v1/rates?from=${from}&to=${to}`).pipe(
|
|
33
29
|
HttpClient.execute,
|
package/src/templates/forex.ts
CHANGED
|
@@ -13,11 +13,7 @@ export default defineFunction({
|
|
|
13
13
|
to: S.String.annotations({ description: 'The target currency' }),
|
|
14
14
|
}),
|
|
15
15
|
|
|
16
|
-
handler: async ({
|
|
17
|
-
event: {
|
|
18
|
-
data: { from, to },
|
|
19
|
-
},
|
|
20
|
-
}: any) => {
|
|
16
|
+
handler: async ({ data: { from, to } }: any) => {
|
|
21
17
|
const res = await fetch(`https://free.ratesdb.com/v1/rates?from=${from}&to=${to}`);
|
|
22
18
|
const {
|
|
23
19
|
data: { rates },
|
package/src/templates/gmail.ts
CHANGED
|
@@ -3,48 +3,46 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
import {
|
|
6
|
+
import { create, defineFunction, EchoObject, Filter, ObjectId, S } from 'dxos:functions';
|
|
7
|
+
// @ts-ignore
|
|
7
8
|
import {
|
|
8
9
|
HttpClient,
|
|
9
10
|
HttpClientRequest,
|
|
10
11
|
FetchHttpClient,
|
|
11
12
|
// @ts-ignore
|
|
12
|
-
} from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.
|
|
13
|
+
} from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.14.21&bundle=false';
|
|
13
14
|
// @ts-ignore
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const DEFAULT_AFTER = '2025-01-01';
|
|
15
|
+
import { format, subDays } from 'https://esm.sh/date-fns@3.3.1?bundle=false';
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
import { pipe, Chunk, Effect, Ref, Schedule, Stream } from 'https://esm.sh/effect@3.14.21?bundle=false';
|
|
18
18
|
|
|
19
19
|
export default defineFunction({
|
|
20
20
|
inputSchema: S.Struct({
|
|
21
|
+
mailboxId: S.String,
|
|
22
|
+
userId: S.optional(S.String).pipe(S.withDecodingDefault(() => 'me')),
|
|
23
|
+
after: S.optional(S.Union(S.Number, S.String)).pipe(
|
|
24
|
+
S.withDecodingDefault(() => format(subDays(new Date(), 30), 'yyyy-MM-dd')),
|
|
25
|
+
),
|
|
26
|
+
pageSize: S.optional(S.Number).pipe(S.withDecodingDefault(() => 100)),
|
|
21
27
|
// TODO(wittjosiah): Remove. This is used to provide a terminal for a cron trigger.
|
|
22
28
|
tick: S.optional(S.String),
|
|
23
|
-
userId: S.optional(S.String),
|
|
24
|
-
after: S.optional(S.Union(S.Number, S.String)),
|
|
25
|
-
pageSize: S.optional(S.Number),
|
|
26
|
-
mailboxId: S.String,
|
|
27
29
|
}),
|
|
28
30
|
|
|
29
31
|
outputSchema: S.Struct({
|
|
30
32
|
newMessages: S.Number,
|
|
31
33
|
}),
|
|
32
34
|
|
|
33
|
-
handler: ({
|
|
34
|
-
event: {
|
|
35
|
-
data: { mailboxId, userId = 'me', after = DEFAULT_AFTER, pageSize = 100 },
|
|
36
|
-
},
|
|
37
|
-
context: { space },
|
|
38
|
-
}: any) =>
|
|
35
|
+
handler: ({ context: { space }, data: { mailboxId, userId, after, pageSize } }: any) =>
|
|
39
36
|
Effect.gen(function* () {
|
|
40
37
|
const { token } = yield* Effect.tryPromise({
|
|
41
38
|
try: () => space.db.query(Filter.typename('dxos.org/type/AccessToken', { source: 'gmail.com' })).first(),
|
|
42
39
|
catch: (e: any) => e,
|
|
43
40
|
});
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
// NOTE: Google API bundles size is v. large and caused runtime issues.
|
|
43
|
+
const makeRequest = (url: string) =>
|
|
46
44
|
pipe(
|
|
47
|
-
|
|
45
|
+
url,
|
|
48
46
|
HttpClientRequest.get,
|
|
49
47
|
HttpClientRequest.setHeaders({ Authorization: `Bearer ${token}`, Accept: 'application/json' }),
|
|
50
48
|
HttpClient.execute,
|
|
@@ -54,13 +52,10 @@ export default defineFunction({
|
|
|
54
52
|
Effect.scoped,
|
|
55
53
|
);
|
|
56
54
|
|
|
57
|
-
const listMessages = (userId: string, q: string, pageSize: number, pageToken: string | null) =>
|
|
58
|
-
|
|
59
|
-
return makeRequest(pageToken ? `${url}&pageToken=${pageToken}` : url);
|
|
60
|
-
};
|
|
55
|
+
const listMessages = (userId: string, q: string, pageSize: number, pageToken: string | null) =>
|
|
56
|
+
makeRequest(getUrl(userId, undefined, { q, pageSize, pageToken }));
|
|
61
57
|
|
|
62
|
-
const getMessage = (userId: string,
|
|
63
|
-
makeRequest(`https://gmail.googleapis.com/gmail/v1/users/${userId}/messages/${id}`);
|
|
58
|
+
const getMessage = (userId: string, messageId: string) => makeRequest(getUrl(userId, messageId));
|
|
64
59
|
|
|
65
60
|
const mailbox = yield* Effect.tryPromise({
|
|
66
61
|
try: () => space.db.query({ id: mailboxId }).first(),
|
|
@@ -97,7 +92,7 @@ export default defineFunction({
|
|
|
97
92
|
continue;
|
|
98
93
|
}
|
|
99
94
|
const subject = messageDetails.payload.headers.find((h: any) => h.name === 'Subject')?.value;
|
|
100
|
-
const object =
|
|
95
|
+
const object = create(MessageType, {
|
|
101
96
|
id: ObjectId.random(),
|
|
102
97
|
created,
|
|
103
98
|
sender,
|
|
@@ -137,6 +132,16 @@ export default defineFunction({
|
|
|
137
132
|
}).pipe(Effect.provide(FetchHttpClient.layer)),
|
|
138
133
|
});
|
|
139
134
|
|
|
135
|
+
const getUrl = (userId: string, messageId?: string, params?: Record<string, any>) => {
|
|
136
|
+
const api = new URL(
|
|
137
|
+
[`https://gmail.googleapis.com/gmail/v1/users/${userId}/messages`, messageId].filter(Boolean).join('/'),
|
|
138
|
+
);
|
|
139
|
+
Object.entries(params ?? {})
|
|
140
|
+
.filter(([_, value]) => value != null)
|
|
141
|
+
.forEach(([key, value]) => api.searchParams.set(key, value));
|
|
142
|
+
return api.toString();
|
|
143
|
+
};
|
|
144
|
+
|
|
140
145
|
/**
|
|
141
146
|
* Parses an email string in the format "Name <email@example.com>" into separate name and email components.
|
|
142
147
|
*/
|
|
@@ -153,10 +158,9 @@ const parseEmailString = (emailString: string): { name?: string; email: string }
|
|
|
153
158
|
return undefined;
|
|
154
159
|
};
|
|
155
160
|
|
|
156
|
-
// TODO(wittjosiah): These schemas should be imported from @dxos/schema.
|
|
157
|
-
|
|
158
161
|
//
|
|
159
162
|
// Schemas
|
|
163
|
+
// TODO(wittjosiah): These schemas should be imported from @dxos/S.
|
|
160
164
|
//
|
|
161
165
|
|
|
162
166
|
const ActorRoles = ['user', 'assistant'] as const;
|
|
@@ -240,5 +244,10 @@ const MessageType = S.Struct({
|
|
|
240
244
|
}),
|
|
241
245
|
),
|
|
242
246
|
),
|
|
243
|
-
}).pipe(
|
|
247
|
+
}).pipe(
|
|
248
|
+
EchoObject({
|
|
249
|
+
typename: 'dxos.org/type/Message',
|
|
250
|
+
version: '0.1.0',
|
|
251
|
+
}),
|
|
252
|
+
);
|
|
244
253
|
type MessageType = S.Schema.Type<typeof MessageType>;
|