@dxos/plugin-script 0.7.5-labs.071a3e2 → 0.7.5-labs.401163d
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/{ScriptContainer-LGW52UAJ.mjs → ScriptContainer-JHAVO2KA.mjs} +36 -11
- package/dist/lib/browser/ScriptContainer-JHAVO2KA.mjs.map +7 -0
- package/dist/lib/browser/artifact-YCIVHFSS.mjs +243 -0
- package/dist/lib/browser/artifact-YCIVHFSS.mjs.map +7 -0
- package/dist/lib/browser/{chunk-VIX22OMB.mjs → chunk-UDLKK74O.mjs} +13 -4
- package/dist/lib/browser/chunk-UDLKK74O.mjs.map +7 -0
- package/dist/lib/browser/{chunk-2UXYECEH.mjs → chunk-WAXHENSY.mjs} +4 -4
- package/dist/lib/browser/{chunk-2UXYECEH.mjs.map → chunk-WAXHENSY.mjs.map} +2 -2
- package/dist/lib/browser/{compiler-K3FNRDDN.mjs → compiler-YY2XSKHQ.mjs} +2 -4
- package/dist/lib/browser/compiler-YY2XSKHQ.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +10 -10
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/{intent-resolver-CMKQTLBG.mjs → intent-resolver-RV3YXIVN.mjs} +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/{react-surface-XACNR5NR.mjs → react-surface-ETY4XKFG.mjs} +2 -2
- package/dist/lib/node/{ScriptContainer-UI2MDBYG.cjs → ScriptContainer-EBIT6CT4.cjs} +41 -14
- package/dist/lib/node/ScriptContainer-EBIT6CT4.cjs.map +7 -0
- package/dist/lib/node/artifact-IPGGY2Z2.cjs +262 -0
- package/dist/lib/node/artifact-IPGGY2Z2.cjs.map +7 -0
- package/dist/lib/node/{chunk-73345ZL6.cjs → chunk-3VDHZ54A.cjs} +6 -6
- package/dist/lib/node/{chunk-73345ZL6.cjs.map → chunk-3VDHZ54A.cjs.map} +2 -2
- package/dist/lib/node/{chunk-PKT5DD3O.cjs → chunk-JKPNBFU2.cjs} +12 -5
- package/dist/lib/node/chunk-JKPNBFU2.cjs.map +7 -0
- package/dist/lib/node/{compiler-P2HQB22H.cjs → compiler-UIBLKJKG.cjs} +6 -6
- package/dist/lib/node/compiler-UIBLKJKG.cjs.map +7 -0
- package/dist/lib/node/index.cjs +14 -13
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/{intent-resolver-V5ASU5DW.cjs → intent-resolver-WFLQNAID.cjs} +6 -6
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/{react-surface-Y3RD5FW7.cjs → react-surface-OIQJFA7P.cjs} +9 -9
- package/dist/lib/node-esm/{ScriptContainer-F7SNP53Z.mjs → ScriptContainer-QVFTCAML.mjs} +36 -11
- package/dist/lib/node-esm/ScriptContainer-QVFTCAML.mjs.map +7 -0
- package/dist/lib/node-esm/artifact-7H4HGX5W.mjs +245 -0
- package/dist/lib/node-esm/artifact-7H4HGX5W.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-NBAL5DXN.mjs → chunk-5CEKSMA5.mjs} +13 -4
- package/dist/lib/node-esm/chunk-5CEKSMA5.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-PRGBYF4B.mjs → chunk-WCAUDRF4.mjs} +4 -4
- package/dist/lib/node-esm/{chunk-PRGBYF4B.mjs.map → chunk-WCAUDRF4.mjs.map} +2 -2
- package/dist/lib/node-esm/{compiler-PDN46H4Y.mjs → compiler-XTXFGZJM.mjs} +2 -4
- package/dist/lib/node-esm/compiler-XTXFGZJM.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +10 -10
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/{intent-resolver-75BHFRUK.mjs → intent-resolver-CYT6IYYH.mjs} +2 -2
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/{react-surface-PKUVZH4S.mjs → react-surface-N67GNKC2.mjs} +2 -2
- package/dist/types/src/ScriptPlugin.d.ts.map +1 -1
- package/dist/types/src/capabilities/artifact.d.ts +11 -0
- package/dist/types/src/capabilities/artifact.d.ts.map +1 -0
- package/dist/types/src/capabilities/index.d.ts +1 -0
- package/dist/types/src/capabilities/index.d.ts.map +1 -1
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts +2 -2
- package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -1
- package/dist/types/src/hooks/deploy.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/templates/discord.d.ts +3 -0
- package/dist/types/src/templates/discord.d.ts.map +1 -0
- package/dist/types/src/templates/forex-effect.d.ts.map +1 -1
- package/dist/types/src/templates/index.d.ts.map +1 -1
- package/package.json +41 -42
- package/src/ScriptPlugin.tsx +6 -1
- package/src/capabilities/artifact.ts +219 -0
- package/src/capabilities/compiler.ts +1 -1
- package/src/capabilities/index.ts +1 -0
- package/src/components/FrameContainer/FrameContainer.tsx +4 -3
- package/src/components/ScriptSettingsPanel.tsx +2 -2
- package/src/components/TypescriptEditor/TypescriptEditor.tsx +2 -2
- package/src/hooks/deploy.ts +15 -4
- package/src/index.ts +0 -1
- package/src/templates/discord.ts +88 -0
- package/src/templates/forex-effect.ts +16 -14
- package/src/templates/index.ts +10 -2
- package/dist/lib/browser/ScriptContainer-LGW52UAJ.mjs.map +0 -7
- package/dist/lib/browser/chunk-SDHKTS6H.mjs +0 -237
- package/dist/lib/browser/chunk-SDHKTS6H.mjs.map +0 -7
- package/dist/lib/browser/chunk-VIX22OMB.mjs.map +0 -7
- package/dist/lib/browser/compiler-K3FNRDDN.mjs.map +0 -7
- package/dist/lib/node/ScriptContainer-UI2MDBYG.cjs.map +0 -7
- package/dist/lib/node/chunk-PKT5DD3O.cjs.map +0 -7
- package/dist/lib/node/chunk-T4TXCRN5.cjs +0 -260
- package/dist/lib/node/chunk-T4TXCRN5.cjs.map +0 -7
- package/dist/lib/node/compiler-P2HQB22H.cjs.map +0 -7
- package/dist/lib/node-esm/ScriptContainer-F7SNP53Z.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-G76M6O46.mjs +0 -239
- package/dist/lib/node-esm/chunk-G76M6O46.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NBAL5DXN.mjs.map +0 -7
- package/dist/lib/node-esm/compiler-PDN46H4Y.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -39
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -55
- package/src/bundler/bundler.ts +0 -250
- package/src/bundler/index.ts +0 -5
- /package/dist/lib/browser/{intent-resolver-CMKQTLBG.mjs.map → intent-resolver-RV3YXIVN.mjs.map} +0 -0
- /package/dist/lib/browser/{react-surface-XACNR5NR.mjs.map → react-surface-ETY4XKFG.mjs.map} +0 -0
- /package/dist/lib/node/{intent-resolver-V5ASU5DW.cjs.map → intent-resolver-WFLQNAID.cjs.map} +0 -0
- /package/dist/lib/node/{react-surface-Y3RD5FW7.cjs.map → react-surface-OIQJFA7P.cjs.map} +0 -0
- /package/dist/lib/node-esm/{intent-resolver-75BHFRUK.mjs.map → intent-resolver-CYT6IYYH.mjs.map} +0 -0
- /package/dist/lib/node-esm/{react-surface-PKUVZH4S.mjs.map → react-surface-N67GNKC2.mjs.map} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/plugin-script",
|
|
3
|
-
"version": "0.7.5-labs.
|
|
3
|
+
"version": "0.7.5-labs.401163d",
|
|
4
4
|
"description": "Braneframe notebook plugin",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -53,39 +53,40 @@
|
|
|
53
53
|
"prettier": "^3.2.4",
|
|
54
54
|
"react-syntax-highlighter": "^15.5.0",
|
|
55
55
|
"typescript": "^5.7.2",
|
|
56
|
-
"@dxos/app-framework": "0.7.5-labs.
|
|
57
|
-
"@dxos/
|
|
58
|
-
"@dxos/async": "0.7.5-labs.
|
|
59
|
-
"@dxos/
|
|
60
|
-
"@dxos/client
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/
|
|
66
|
-
"@dxos/
|
|
67
|
-
"@dxos/
|
|
68
|
-
"@dxos/
|
|
69
|
-
"@dxos/
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/
|
|
72
|
-
"@dxos/plugin-
|
|
73
|
-
"@dxos/plugin-
|
|
74
|
-
"@dxos/plugin-
|
|
75
|
-
"@dxos/plugin-
|
|
76
|
-
"@dxos/plugin-theme": "0.7.5-labs.
|
|
77
|
-
"@dxos/
|
|
78
|
-
"@dxos/
|
|
79
|
-
"@dxos/react-
|
|
80
|
-
"@dxos/
|
|
81
|
-
"@dxos/react-ui-
|
|
82
|
-
"@dxos/react-ui-
|
|
83
|
-
"@dxos/react-ui-stack": "0.7.5-labs.
|
|
84
|
-
"@dxos/react-ui-
|
|
85
|
-
"@dxos/rpc": "0.7.5-labs.
|
|
86
|
-
"@dxos/
|
|
87
|
-
"@dxos/
|
|
88
|
-
"@dxos/
|
|
56
|
+
"@dxos/app-framework": "0.7.5-labs.401163d",
|
|
57
|
+
"@dxos/artifact": "0.7.5-labs.401163d",
|
|
58
|
+
"@dxos/async": "0.7.5-labs.401163d",
|
|
59
|
+
"@dxos/assistant": "0.7.5-labs.401163d",
|
|
60
|
+
"@dxos/client": "0.7.5-labs.401163d",
|
|
61
|
+
"@dxos/compute": "0.7.5-labs.401163d",
|
|
62
|
+
"@dxos/client-protocol": "0.7.5-labs.401163d",
|
|
63
|
+
"@dxos/debug": "0.7.5-labs.401163d",
|
|
64
|
+
"@dxos/echo-schema": "0.7.5-labs.401163d",
|
|
65
|
+
"@dxos/functions": "0.7.5-labs.401163d",
|
|
66
|
+
"@dxos/invariant": "0.7.5-labs.401163d",
|
|
67
|
+
"@dxos/keys": "0.7.5-labs.401163d",
|
|
68
|
+
"@dxos/live-object": "0.7.5-labs.401163d",
|
|
69
|
+
"@dxos/local-storage": "0.7.5-labs.401163d",
|
|
70
|
+
"@dxos/log": "0.7.5-labs.401163d",
|
|
71
|
+
"@dxos/plugin-graph": "0.7.5-labs.401163d",
|
|
72
|
+
"@dxos/plugin-client": "0.7.5-labs.401163d",
|
|
73
|
+
"@dxos/plugin-explorer": "0.7.5-labs.401163d",
|
|
74
|
+
"@dxos/plugin-markdown": "0.7.5-labs.401163d",
|
|
75
|
+
"@dxos/plugin-space": "0.7.5-labs.401163d",
|
|
76
|
+
"@dxos/plugin-theme": "0.7.5-labs.401163d",
|
|
77
|
+
"@dxos/protocols": "0.7.5-labs.401163d",
|
|
78
|
+
"@dxos/plugin-stack": "0.7.5-labs.401163d",
|
|
79
|
+
"@dxos/react-client": "0.7.5-labs.401163d",
|
|
80
|
+
"@dxos/react-ui-attention": "0.7.5-labs.401163d",
|
|
81
|
+
"@dxos/react-ui-editor": "0.7.5-labs.401163d",
|
|
82
|
+
"@dxos/react-ui-form": "0.7.5-labs.401163d",
|
|
83
|
+
"@dxos/react-ui-stack": "0.7.5-labs.401163d",
|
|
84
|
+
"@dxos/react-ui-menu": "0.7.5-labs.401163d",
|
|
85
|
+
"@dxos/rpc": "0.7.5-labs.401163d",
|
|
86
|
+
"@dxos/react-ui-syntax-highlighter": "0.7.5-labs.401163d",
|
|
87
|
+
"@dxos/rpc-tunnel": "0.7.5-labs.401163d",
|
|
88
|
+
"@dxos/schema": "0.7.5-labs.401163d",
|
|
89
|
+
"@dxos/util": "0.7.5-labs.401163d"
|
|
89
90
|
},
|
|
90
91
|
"devDependencies": {
|
|
91
92
|
"@phosphor-icons/react": "^2.1.5",
|
|
@@ -95,19 +96,17 @@
|
|
|
95
96
|
"react": "~18.2.0",
|
|
96
97
|
"react-dom": "~18.2.0",
|
|
97
98
|
"vite": "5.4.7",
|
|
98
|
-
"@dxos/random": "0.7.5-labs.
|
|
99
|
-
"@dxos/react-ui": "0.7.5-labs.
|
|
100
|
-
"@dxos/
|
|
101
|
-
"@dxos/react-ui-theme": "0.7.5-labs.
|
|
102
|
-
"@dxos/storybook-utils": "0.7.5-labs.071a3e2"
|
|
99
|
+
"@dxos/random": "0.7.5-labs.401163d",
|
|
100
|
+
"@dxos/react-ui": "0.7.5-labs.401163d",
|
|
101
|
+
"@dxos/storybook-utils": "0.7.5-labs.401163d",
|
|
102
|
+
"@dxos/react-ui-theme": "0.7.5-labs.401163d"
|
|
103
103
|
},
|
|
104
104
|
"peerDependencies": {
|
|
105
105
|
"@phosphor-icons/react": "^2.1.5",
|
|
106
106
|
"react": "~18.2.0",
|
|
107
107
|
"react-dom": "~18.2.0",
|
|
108
|
-
"@dxos/react-ui": "0.7.5-labs.
|
|
109
|
-
"@dxos/react-ui-theme": "0.7.5-labs.
|
|
110
|
-
"@dxos/react-ui-mosaic": "0.7.5-labs.071a3e2"
|
|
108
|
+
"@dxos/react-ui": "0.7.5-labs.401163d",
|
|
109
|
+
"@dxos/react-ui-theme": "0.7.5-labs.401163d"
|
|
111
110
|
},
|
|
112
111
|
"publishConfig": {
|
|
113
112
|
"access": "public"
|
package/src/ScriptPlugin.tsx
CHANGED
|
@@ -10,7 +10,7 @@ import { FunctionType, ScriptType } from '@dxos/functions';
|
|
|
10
10
|
import { RefArray } from '@dxos/live-object';
|
|
11
11
|
import { ClientCapabilities, ClientEvents } from '@dxos/plugin-client';
|
|
12
12
|
|
|
13
|
-
import { Compiler, IntentResolver, ReactSurface, ScriptSettings } from './capabilities';
|
|
13
|
+
import { Artifact, Compiler, IntentResolver, ReactSurface, ScriptSettings } from './capabilities';
|
|
14
14
|
import { meta, SCRIPT_PLUGIN } from './meta';
|
|
15
15
|
import translations from './translations';
|
|
16
16
|
import { ScriptAction } from './types';
|
|
@@ -67,4 +67,9 @@ export const ScriptPlugin = () =>
|
|
|
67
67
|
activatesOn: Events.SetupIntents,
|
|
68
68
|
activate: IntentResolver,
|
|
69
69
|
}),
|
|
70
|
+
defineModule({
|
|
71
|
+
id: `${meta.id}/module/artifact`,
|
|
72
|
+
activatesOn: Events.Startup,
|
|
73
|
+
activate: Artifact,
|
|
74
|
+
}),
|
|
70
75
|
]);
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Capabilities, contributes, createIntent, type PromiseIntentDispatcher } from '@dxos/app-framework';
|
|
6
|
+
import { defineArtifact, defineTool, ToolResult } from '@dxos/artifact';
|
|
7
|
+
import { createArtifactElement } from '@dxos/assistant';
|
|
8
|
+
import { ObjectId, S } from '@dxos/echo-schema';
|
|
9
|
+
import { ScriptType } from '@dxos/functions';
|
|
10
|
+
import { invariant } from '@dxos/invariant';
|
|
11
|
+
import { SpaceAction } from '@dxos/plugin-space/types';
|
|
12
|
+
import { create, makeRef, type Space } from '@dxos/react-client/echo';
|
|
13
|
+
import { TextType } from '@dxos/schema';
|
|
14
|
+
|
|
15
|
+
// TODO(burdon): Factor out.
|
|
16
|
+
declare global {
|
|
17
|
+
interface ToolContextExtensions {
|
|
18
|
+
space?: Space;
|
|
19
|
+
dispatch?: PromiseIntentDispatcher;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default () => {
|
|
24
|
+
const definition = defineArtifact({
|
|
25
|
+
id: 'plugin-script',
|
|
26
|
+
// TODO(dmaretskyi): Since writing scripts requires a lot of domain knowledge,
|
|
27
|
+
// we should offload the work of synthesizing the code to a separate model.
|
|
28
|
+
// The main reasoning model will give it a concrete task and the script model will synthesize the code, knowing all the docs.
|
|
29
|
+
instructions: `
|
|
30
|
+
If the user explicitly requests you to write a script, you can create one.
|
|
31
|
+
If the user requests you to change one of the existing script, you can update it.
|
|
32
|
+
Scripts are short code pieces of executable code that can be used to perform computations, integrate with external systems, and perform automated tasks.
|
|
33
|
+
Each script must follow as strict shape for the definition but the body can contain any valid executable code.
|
|
34
|
+
Scripts are written in JavaScript or TypeScript.
|
|
35
|
+
Each script must define an input schema and output schema.
|
|
36
|
+
Each script must have its own detailed description.
|
|
37
|
+
|
|
38
|
+
Important: You cannot execute scripts. Scripts are only available as tools after the user deploys them. You must only call the available tools.
|
|
39
|
+
|
|
40
|
+
Description:
|
|
41
|
+
The description is a detailed description of the script.
|
|
42
|
+
It is used to describe the script's purpose, input, and output.
|
|
43
|
+
The description is used to generate the script's documentation.
|
|
44
|
+
The description is also goes into the script metadata.
|
|
45
|
+
The description is used by AI to understand the script and its purpose.
|
|
46
|
+
The description must provide cases on when the script should be used.
|
|
47
|
+
|
|
48
|
+
Schema:
|
|
49
|
+
The schema is defined using a schema-DSL library called Effect Schema.
|
|
50
|
+
The input schema describes the arguments that the script accepts.
|
|
51
|
+
The output schema describes the result that the script produces.
|
|
52
|
+
The schema is used to validate the script's input and output.
|
|
53
|
+
The schema is also goes into the script metadata.
|
|
54
|
+
The properties in the schema must have valid descriptions.
|
|
55
|
+
The input and output schemas must both be a struct of fields.
|
|
56
|
+
|
|
57
|
+
Restricts:
|
|
58
|
+
The scripts can only import code from "dxos:functions" module. No other modules are allowed.
|
|
59
|
+
Some web APIs are available - like fetch.
|
|
60
|
+
|
|
61
|
+
Reasoning:
|
|
62
|
+
Before writing a script, synthesize the following information:
|
|
63
|
+
- Description
|
|
64
|
+
- What APIs will be used.
|
|
65
|
+
- Detailed input schema
|
|
66
|
+
- Detailed output schema
|
|
67
|
+
|
|
68
|
+
<apis>
|
|
69
|
+
export function defineFunction(params: {
|
|
70
|
+
description: string,
|
|
71
|
+
inputSchema: S.Schema,
|
|
72
|
+
outputSchema: S.Schema,
|
|
73
|
+
handler: (params: { event: { data: TInput } }) => Promise<TOutput>
|
|
74
|
+
}): FunctionDefinition
|
|
75
|
+
/**
|
|
76
|
+
* Effect Schema
|
|
77
|
+
*/
|
|
78
|
+
export const S;
|
|
79
|
+
|
|
80
|
+
// Examples:
|
|
81
|
+
|
|
82
|
+
S.String
|
|
83
|
+
S.Number
|
|
84
|
+
S.Boolean
|
|
85
|
+
S.Struct({
|
|
86
|
+
from: S.String.annotations({ description: 'The source currency' }),
|
|
87
|
+
to: S.String.annotations({ description: 'The target currency' }),
|
|
88
|
+
})
|
|
89
|
+
S.Array(S.String)
|
|
90
|
+
S.Union(S.String, S.Number)
|
|
91
|
+
S.optional(S.String)
|
|
92
|
+
S.String.annotations({ description: 'The source currency' })
|
|
93
|
+
</apis>
|
|
94
|
+
|
|
95
|
+
<handler_function>
|
|
96
|
+
Async function that takes the argument in the form of:
|
|
97
|
+
|
|
98
|
+
type HandlerFunctionParam = {
|
|
99
|
+
event: {
|
|
100
|
+
data: T // The input schema
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
The function must return the result that matches the output schema.
|
|
105
|
+
</handler_function>
|
|
106
|
+
|
|
107
|
+
<example>
|
|
108
|
+
import { defineFunction, S } from 'dxos:functions';
|
|
109
|
+
|
|
110
|
+
export default defineFunction({
|
|
111
|
+
description: 'Returns the exchange rate between two currencies.',
|
|
112
|
+
|
|
113
|
+
inputSchema: S.Struct({
|
|
114
|
+
from: S.String.annotations({ description: 'The source currency' }),
|
|
115
|
+
to: S.String.annotations({ description: 'The target currency' }),
|
|
116
|
+
}),
|
|
117
|
+
|
|
118
|
+
outputSchema: S.Struct({
|
|
119
|
+
rate: S.String.annotations({ description: 'The exchange rate' }),
|
|
120
|
+
}),
|
|
121
|
+
|
|
122
|
+
handler: async ({
|
|
123
|
+
event: {
|
|
124
|
+
data: { from, to },
|
|
125
|
+
},
|
|
126
|
+
}: any) => {
|
|
127
|
+
const res = await fetch(\`https://free.ratesdb.com/v1/rates?from=\${from}&to=\${to}\`);
|
|
128
|
+
const {
|
|
129
|
+
data: { rates },
|
|
130
|
+
} = await res.json();
|
|
131
|
+
|
|
132
|
+
return { rate: rates[to].toString() };
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
</example>
|
|
136
|
+
`,
|
|
137
|
+
schema: ScriptType,
|
|
138
|
+
tools: [
|
|
139
|
+
defineTool({
|
|
140
|
+
name: 'script_new',
|
|
141
|
+
description: 'Create a new script. Returns the artifact definition for the script',
|
|
142
|
+
schema: S.Struct({
|
|
143
|
+
name: S.String.annotations({ description: 'The name of the script' }),
|
|
144
|
+
code: S.String.annotations({
|
|
145
|
+
description: 'The full code of the script in JavaScript or TypeScript. Must be valid executable code.',
|
|
146
|
+
}),
|
|
147
|
+
}),
|
|
148
|
+
execute: async ({ name, code }, { extensions }) => {
|
|
149
|
+
invariant(extensions?.space, 'No space');
|
|
150
|
+
invariant(extensions?.dispatch, 'No intent dispatcher');
|
|
151
|
+
const script = create(ScriptType, {
|
|
152
|
+
name,
|
|
153
|
+
source: makeRef(
|
|
154
|
+
create(TextType, {
|
|
155
|
+
content: code,
|
|
156
|
+
}),
|
|
157
|
+
),
|
|
158
|
+
});
|
|
159
|
+
extensions.space.db.add(script);
|
|
160
|
+
await extensions.space.db.flush();
|
|
161
|
+
|
|
162
|
+
const intent = createIntent(SpaceAction.AddObject, { target: extensions.space, object: script });
|
|
163
|
+
const { data, error } = await extensions.dispatch(intent);
|
|
164
|
+
if (!data || error) {
|
|
165
|
+
return ToolResult.Error(error?.message ?? 'Failed to create chess game');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return ToolResult.Success(createArtifactElement(script.id));
|
|
169
|
+
},
|
|
170
|
+
}),
|
|
171
|
+
defineTool({
|
|
172
|
+
name: 'script_inspect',
|
|
173
|
+
description: 'Inspect a script. Returns the artifact definition for the script',
|
|
174
|
+
schema: S.Struct({
|
|
175
|
+
id: ObjectId,
|
|
176
|
+
}),
|
|
177
|
+
execute: async ({ id }, { extensions }) => {
|
|
178
|
+
invariant(extensions?.space, 'No space');
|
|
179
|
+
|
|
180
|
+
const script = (await extensions.space.db.query({ id }).first()) as ScriptType;
|
|
181
|
+
const { content } = await script.source.load();
|
|
182
|
+
|
|
183
|
+
return ToolResult.Success({
|
|
184
|
+
name: script.name,
|
|
185
|
+
code: content,
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
}),
|
|
189
|
+
defineTool({
|
|
190
|
+
name: 'script_update',
|
|
191
|
+
description: 'Update a script. Returns the artifact definition for the script',
|
|
192
|
+
schema: S.Struct({
|
|
193
|
+
id: ObjectId,
|
|
194
|
+
code: S.String.annotations({
|
|
195
|
+
description: 'The full code of the script in JavaScript or TypeScript. Must be valid executable code.',
|
|
196
|
+
}),
|
|
197
|
+
}),
|
|
198
|
+
execute: async ({ id, code }, { extensions }) => {
|
|
199
|
+
invariant(extensions?.space, 'No space');
|
|
200
|
+
|
|
201
|
+
const script = (await extensions.space.db.query({ id }).first()) as ScriptType;
|
|
202
|
+
const source = await script.source.load();
|
|
203
|
+
if (!source) {
|
|
204
|
+
return ToolResult.Error('Script not found');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
source.content = code;
|
|
208
|
+
await extensions.space?.db.flush();
|
|
209
|
+
|
|
210
|
+
return ToolResult.Success({
|
|
211
|
+
name: script.name,
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
}),
|
|
215
|
+
],
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return contributes(Capabilities.ArtifactDefinition, definition);
|
|
219
|
+
};
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
|
|
7
7
|
|
|
8
8
|
import { contributes } from '@dxos/app-framework';
|
|
9
|
+
import { initializeBundler } from '@dxos/functions/bundler';
|
|
9
10
|
|
|
10
11
|
import { ScriptCapabilities } from './capabilities';
|
|
11
|
-
import { initializeBundler } from '../bundler';
|
|
12
12
|
import { Compiler } from '../compiler';
|
|
13
13
|
|
|
14
14
|
export default async () => {
|
|
@@ -8,5 +8,6 @@ export const Compiler = lazy(() => import('./compiler'));
|
|
|
8
8
|
export const IntentResolver = lazy(() => import('./intent-resolver'));
|
|
9
9
|
export const ReactSurface = lazy(() => import('./react-surface'));
|
|
10
10
|
export const ScriptSettings = lazy(() => import('./settings'));
|
|
11
|
+
export const Artifact = lazy(() => import('./artifact'));
|
|
11
12
|
|
|
12
13
|
export * from './capabilities';
|
|
@@ -5,17 +5,17 @@
|
|
|
5
5
|
import React, { useEffect, useRef } from 'react';
|
|
6
6
|
|
|
7
7
|
import { clientServiceBundle } from '@dxos/client-protocol';
|
|
8
|
+
import { type BundleResult } from '@dxos/functions/bundler';
|
|
9
|
+
import { invariant } from '@dxos/invariant';
|
|
8
10
|
import { log } from '@dxos/log';
|
|
9
11
|
import { useClient } from '@dxos/react-client';
|
|
10
12
|
import { baseSurface, mx } from '@dxos/react-ui-theme';
|
|
11
13
|
import { createProtoRpcPeer } from '@dxos/rpc';
|
|
12
14
|
import { createIFramePort } from '@dxos/rpc-tunnel';
|
|
13
15
|
|
|
14
|
-
import { type BundlerResult } from '../../bundler';
|
|
15
|
-
|
|
16
16
|
export type FrameContainerProps = {
|
|
17
17
|
containerUrl: string;
|
|
18
|
-
result:
|
|
18
|
+
result: BundleResult;
|
|
19
19
|
debug?: boolean;
|
|
20
20
|
};
|
|
21
21
|
|
|
@@ -46,6 +46,7 @@ export const FrameContainer = ({ containerUrl, result, debug = true }: FrameCont
|
|
|
46
46
|
}, [iframeRef]);
|
|
47
47
|
|
|
48
48
|
// Encodes compiled code via URL.
|
|
49
|
+
invariant(result.sourceHash, 'Source hash is required.');
|
|
49
50
|
const sourceHash = Buffer.from(result.sourceHash).toString('hex');
|
|
50
51
|
const src = result.bundle && `${containerUrl}?ts=${sourceHash}#code=${encodeURIComponent(result.bundle)}`;
|
|
51
52
|
|
|
@@ -10,7 +10,7 @@ import { FunctionType, type ScriptType, getInvocationUrl, getUserFunctionUrlInMe
|
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
import { useClient } from '@dxos/react-client';
|
|
12
12
|
import { Filter, getMeta, getSpace, useQuery } from '@dxos/react-client/echo';
|
|
13
|
-
import { Button, Clipboard, Input, Separator,
|
|
13
|
+
import { Button, Clipboard, Input, Separator, useControlledState, useTranslation } from '@dxos/react-ui';
|
|
14
14
|
import { AccessTokenType } from '@dxos/schema';
|
|
15
15
|
|
|
16
16
|
import { SCRIPT_PLUGIN } from '../meta';
|
|
@@ -57,7 +57,7 @@ export const ScriptSettingsPanel = ({ script }: ScriptSettingsPanelProps) => {
|
|
|
57
57
|
return () => clearTimeout(timeout);
|
|
58
58
|
}, [githubToken, gistKey]);
|
|
59
59
|
|
|
60
|
-
const [binding, setBinding] =
|
|
60
|
+
const [binding, setBinding] = useControlledState(fn?.binding ?? '');
|
|
61
61
|
const handleBindingChange = useCallback(
|
|
62
62
|
(event: ChangeEvent<HTMLInputElement>) => {
|
|
63
63
|
setBinding(event.target.value);
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
useTextEditor,
|
|
28
28
|
type UseTextEditorProps,
|
|
29
29
|
} from '@dxos/react-ui-editor';
|
|
30
|
-
import {
|
|
30
|
+
import { isNonNullable } from '@dxos/util';
|
|
31
31
|
|
|
32
32
|
export type TypescriptEditorProps = {
|
|
33
33
|
id: string;
|
|
@@ -94,7 +94,7 @@ export const TypescriptEditor = ({
|
|
|
94
94
|
]
|
|
95
95
|
: [],
|
|
96
96
|
],
|
|
97
|
-
].filter(
|
|
97
|
+
].filter(isNonNullable),
|
|
98
98
|
selection,
|
|
99
99
|
scrollTo,
|
|
100
100
|
}),
|
package/src/hooks/deploy.ts
CHANGED
|
@@ -6,13 +6,14 @@ import { useCallback, useEffect, useMemo } from 'react';
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
FunctionType,
|
|
9
|
+
type ScriptType,
|
|
9
10
|
getInvocationUrl,
|
|
10
11
|
getUserFunctionUrlInMetadata,
|
|
11
12
|
incrementSemverPatch,
|
|
12
|
-
type ScriptType,
|
|
13
13
|
setUserFunctionUrlInMetadata,
|
|
14
14
|
uploadWorkerFunction,
|
|
15
15
|
} from '@dxos/functions';
|
|
16
|
+
import { Bundler } from '@dxos/functions/bundler';
|
|
16
17
|
import { log } from '@dxos/log';
|
|
17
18
|
import { useClient } from '@dxos/react-client';
|
|
18
19
|
import { Filter, create, getMeta, getSpace, makeRef, useQuery } from '@dxos/react-client/echo';
|
|
@@ -20,7 +21,6 @@ import { useTranslation } from '@dxos/react-ui';
|
|
|
20
21
|
import { createMenuAction } from '@dxos/react-ui-menu';
|
|
21
22
|
|
|
22
23
|
import { type ViewState } from './view';
|
|
23
|
-
import { Bundler } from '../bundler';
|
|
24
24
|
import { SCRIPT_PLUGIN } from '../meta';
|
|
25
25
|
|
|
26
26
|
export type DeployActionProperties = { type: 'deploy' } | { type: 'copy' };
|
|
@@ -105,7 +105,7 @@ export const useDeployHandler = ({ state, script }: { state: Partial<DeployState
|
|
|
105
105
|
const existingFunctionId = existingFunctionUrl?.split('/').at(-1);
|
|
106
106
|
|
|
107
107
|
const bundler = new Bundler({ platform: 'browser', sandboxedModules: [], remoteModules: {} });
|
|
108
|
-
const buildResult = await bundler.bundle(script.source.target!.content);
|
|
108
|
+
const buildResult = await bundler.bundle({ source: script.source.target!.content });
|
|
109
109
|
if (buildResult.error || !buildResult.bundle) {
|
|
110
110
|
throw buildResult.error;
|
|
111
111
|
}
|
|
@@ -130,13 +130,24 @@ export const useDeployHandler = ({ state, script }: { state: Partial<DeployState
|
|
|
130
130
|
fn ?? space.db.add(create(FunctionType, { name: functionId, version, source: makeRef(script) }));
|
|
131
131
|
|
|
132
132
|
script.changed = false;
|
|
133
|
-
|
|
133
|
+
if (script.description !== undefined && script.description.trim() !== '') {
|
|
134
|
+
deployedFunction.description = script.description;
|
|
135
|
+
} else if (meta.description) {
|
|
136
|
+
deployedFunction.description = meta.description;
|
|
137
|
+
} else {
|
|
138
|
+
log.verbose('no description in function metadata', { functionId });
|
|
139
|
+
}
|
|
134
140
|
|
|
135
141
|
if (meta.inputSchema) {
|
|
136
142
|
deployedFunction.inputSchema = meta.inputSchema;
|
|
137
143
|
} else {
|
|
138
144
|
log.verbose('no input schema in function metadata', { functionId });
|
|
139
145
|
}
|
|
146
|
+
if (meta.outputSchema) {
|
|
147
|
+
deployedFunction.outputSchema = meta.outputSchema;
|
|
148
|
+
} else {
|
|
149
|
+
log.verbose('no output schema in function metadata', { functionId });
|
|
150
|
+
}
|
|
140
151
|
|
|
141
152
|
setUserFunctionUrlInMetadata(getMeta(deployedFunction), `/${space.id}/${functionId}`);
|
|
142
153
|
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import { createStatic, EchoObject, defineFunction, DXN, Filter, ObjectId, S } from 'dxos:functions';
|
|
7
|
+
import {
|
|
8
|
+
FetchHttpClient,
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
} from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.13.3';
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
import { DiscordConfig, DiscordREST, DiscordRESTMemoryLive } from 'https://esm.sh/dfx@0.113.0?deps=effect@3.13.3';
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
import { Effect, Config, Redacted } from 'https://esm.sh/effect@3.13.3';
|
|
15
|
+
|
|
16
|
+
const MessageSchema = S.Struct({
|
|
17
|
+
id: S.String,
|
|
18
|
+
foreignId: S.Number,
|
|
19
|
+
content: S.String,
|
|
20
|
+
}).pipe(EchoObject('example.com/type/Message', '0.1.0'));
|
|
21
|
+
|
|
22
|
+
export default defineFunction({
|
|
23
|
+
inputSchema: S.Struct({
|
|
24
|
+
// TODO(wittjosiah): Remove. This is used to provide a terminal for a cron trigger.
|
|
25
|
+
tick: S.optional(S.String),
|
|
26
|
+
channelId: S.String,
|
|
27
|
+
pageSize: S.optional(S.Number),
|
|
28
|
+
queueId: S.String,
|
|
29
|
+
}),
|
|
30
|
+
|
|
31
|
+
outputSchema: S.Struct({
|
|
32
|
+
newMessages: S.Number,
|
|
33
|
+
}),
|
|
34
|
+
|
|
35
|
+
handler: ({
|
|
36
|
+
event: {
|
|
37
|
+
data: { channelId, queueId, pageSize = 5 },
|
|
38
|
+
},
|
|
39
|
+
context: { space },
|
|
40
|
+
}: any) =>
|
|
41
|
+
Effect.gen(function* () {
|
|
42
|
+
const { token } = yield* Effect.tryPromise(() =>
|
|
43
|
+
space.db.query(Filter.typename('dxos.org/type/AccessToken', { source: 'discord.com' })).first(),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const enqueueMessages = Effect.gen(function* () {
|
|
47
|
+
const rest = yield* DiscordREST;
|
|
48
|
+
const { objects } = yield* Effect.tryPromise(() => space.queues.queryQueue(DXN.parse(queueId)));
|
|
49
|
+
|
|
50
|
+
let newMessages = 0;
|
|
51
|
+
let lastMessage = objects.at(-1);
|
|
52
|
+
while (true) {
|
|
53
|
+
// NOTE: If `after` is specified, the specified message is included in the results.
|
|
54
|
+
const messages = yield* rest
|
|
55
|
+
.getChannelMessages(channelId, { after: lastMessage?.foreignId, limit: pageSize })
|
|
56
|
+
.pipe((res: any) => res.json);
|
|
57
|
+
if (messages.length === 1) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
const queueMessages = messages
|
|
61
|
+
.map((message: any) =>
|
|
62
|
+
createStatic(MessageSchema, {
|
|
63
|
+
id: ObjectId.random(),
|
|
64
|
+
foreignId: parseInt(message.id),
|
|
65
|
+
content: message.content,
|
|
66
|
+
}),
|
|
67
|
+
)
|
|
68
|
+
.slice(lastMessage ? 1 : 0)
|
|
69
|
+
.toReversed();
|
|
70
|
+
yield* Effect.tryPromise(() => space.queues.insertIntoQueue(DXN.parse(queueId), queueMessages));
|
|
71
|
+
newMessages += queueMessages.length;
|
|
72
|
+
lastMessage = queueMessages.at(-1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { newMessages };
|
|
76
|
+
}).pipe(
|
|
77
|
+
Effect.provide(DiscordRESTMemoryLive),
|
|
78
|
+
Effect.provide(
|
|
79
|
+
DiscordConfig.layerConfig({
|
|
80
|
+
token: Config.succeed(Redacted.make(token)),
|
|
81
|
+
}),
|
|
82
|
+
),
|
|
83
|
+
Effect.provide(FetchHttpClient.layer),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return yield* enqueueMessages;
|
|
87
|
+
}),
|
|
88
|
+
});
|
|
@@ -7,14 +7,11 @@ import { defineFunction, S } from 'dxos:functions';
|
|
|
7
7
|
import {
|
|
8
8
|
HttpClient,
|
|
9
9
|
HttpClientRequest,
|
|
10
|
-
|
|
11
|
-
HttpClientError,
|
|
10
|
+
FetchHttpClient,
|
|
12
11
|
// @ts-ignore
|
|
13
|
-
} from 'https://esm.sh/@effect/platform@0.
|
|
12
|
+
} from 'https://esm.sh/@effect/platform@0.77.2?deps=effect@3.13.3';
|
|
14
13
|
// @ts-ignore
|
|
15
|
-
import
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
import { Effect, Schedule } from 'https://esm.sh/effect@3.6.0';
|
|
14
|
+
import { Effect, Schedule } from 'https://esm.sh/effect@3.13.3';
|
|
18
15
|
|
|
19
16
|
/**
|
|
20
17
|
* Returns the exchange rate between two currencies.
|
|
@@ -25,18 +22,23 @@ export default defineFunction({
|
|
|
25
22
|
to: S.String.annotations({ description: 'The target currency' }),
|
|
26
23
|
}),
|
|
27
24
|
|
|
28
|
-
outputSchema: S.
|
|
25
|
+
outputSchema: S.Struct({
|
|
26
|
+
rate: S.Number,
|
|
27
|
+
}),
|
|
29
28
|
|
|
30
29
|
handler: async ({
|
|
31
30
|
event: {
|
|
32
31
|
data: { from, to },
|
|
33
32
|
},
|
|
34
33
|
}: any) =>
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
Effect.gen(function* () {
|
|
35
|
+
const res = yield* HttpClientRequest.get(`https://free.ratesdb.com/v1/rates?from=${from}&to=${to}`).pipe(
|
|
36
|
+
HttpClient.execute,
|
|
37
|
+
Effect.flatMap((res: any) => res.json),
|
|
38
|
+
Effect.timeout('1 second'),
|
|
39
|
+
Effect.retry(Schedule.exponential(1000).pipe(Schedule.compose(Schedule.recurs(3)))),
|
|
40
|
+
Effect.scoped,
|
|
41
|
+
);
|
|
42
|
+
return { rate: res.data.rates[to].toString() };
|
|
43
|
+
}).pipe(Effect.provide(FetchHttpClient.layer)),
|
|
42
44
|
});
|
package/src/templates/index.ts
CHANGED
|
@@ -7,13 +7,15 @@ import chat from './chat.ts?raw';
|
|
|
7
7
|
// @ts-ignore
|
|
8
8
|
import chess from './chess.ts?raw';
|
|
9
9
|
// @ts-ignore
|
|
10
|
+
import discord from './discord.ts?raw';
|
|
11
|
+
// @ts-ignore
|
|
10
12
|
import echo from './echo.ts?raw';
|
|
11
13
|
// @ts-ignore
|
|
12
14
|
import email from './email.ts?raw';
|
|
13
15
|
// @ts-ignore
|
|
14
|
-
import forex from './forex.ts?raw';
|
|
15
|
-
// @ts-ignore
|
|
16
16
|
import forexEffect from './forex-effect.ts?raw';
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
import forex from './forex.ts?raw';
|
|
17
19
|
|
|
18
20
|
const removeHeader = (str: string) => str.split('\n').slice(4).join('\n');
|
|
19
21
|
|
|
@@ -58,4 +60,10 @@ export const templates: Template[] = [
|
|
|
58
60
|
source: removeHeader(email),
|
|
59
61
|
presetId: 'dxos.org/function/email',
|
|
60
62
|
},
|
|
63
|
+
{
|
|
64
|
+
id: 'dxos.org/script/discord',
|
|
65
|
+
name: 'Discord',
|
|
66
|
+
source: removeHeader(discord),
|
|
67
|
+
presetId: 'dxos.org/function/discord',
|
|
68
|
+
},
|
|
61
69
|
];
|