freestyle-sandboxes 0.0.1 → 0.0.3
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/README.md +62 -0
- package/dist/ai/index.cjs +57 -3
- package/dist/ai/index.d.cts +22 -6
- package/dist/ai/index.d.mts +22 -6
- package/dist/ai/index.mjs +57 -4
- package/dist/index.cjs +12 -2
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +12 -2
- package/package.json +1 -1
- package/src/ai/index.ts +74 -6
- package/src/index.ts +9 -4
- package/tsconfig.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Freestyle Sandboxes SDK
|
|
2
|
+
|
|
3
|
+
SDK for [Freestyle Sandboxes API](https://api.freestyle.sh)
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install freestyle-sandboxes
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { FreestyleSandboxes } from "freestyle-sandboxes";
|
|
15
|
+
|
|
16
|
+
const sandboxes = new FreestyleSandboxes({
|
|
17
|
+
apiKey: "your-api-key",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
sandboxes.executeScript(
|
|
21
|
+
`export default () => {
|
|
22
|
+
let set1 = [1, 2, 3, 4, 5];
|
|
23
|
+
let set2 = [4, 5, 6, 7, 8];
|
|
24
|
+
|
|
25
|
+
// find the sum of every value of each set multiplied by every value of the other set
|
|
26
|
+
|
|
27
|
+
let sum = 0;
|
|
28
|
+
for (let i = 0; i < set1.length; i++) {
|
|
29
|
+
for (let j = 0; j < set2.length; j++) {
|
|
30
|
+
sum += set1[i] * set2[j];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return sum;
|
|
35
|
+
};`
|
|
36
|
+
);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## AI SDK
|
|
40
|
+
|
|
41
|
+
The freestyle-sandboxes/ai package provides utilities to add Freestyle Sandboxes to your AI.
|
|
42
|
+
|
|
43
|
+
### Usage
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
import { executeTool } from "freestyle-sandboxes/ai";
|
|
47
|
+
import { generateText } from "ai";
|
|
48
|
+
|
|
49
|
+
const codeExecutor = executeTool({
|
|
50
|
+
apiKey: "your-api-key",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const { text, steps } = await generateText({
|
|
54
|
+
model: yourModel,
|
|
55
|
+
tools: {
|
|
56
|
+
codeExecutor,
|
|
57
|
+
},
|
|
58
|
+
maxSteps: 2, // allow up to 5 steps
|
|
59
|
+
prompt:
|
|
60
|
+
"What is the sum of every number between 1 and 12 multiplied by itself?",
|
|
61
|
+
});
|
|
62
|
+
```
|
package/dist/ai/index.cjs
CHANGED
|
@@ -7,14 +7,19 @@ require('@hey-api/client-fetch');
|
|
|
7
7
|
|
|
8
8
|
const executeTool = (config) => {
|
|
9
9
|
const api = new index.FreestyleSandboxes({
|
|
10
|
-
|
|
10
|
+
...config
|
|
11
11
|
});
|
|
12
|
+
const envVars = Object.keys(config.envVars ?? {}).join(", ");
|
|
13
|
+
const nodeModules = Object.keys(config.nodeModules ?? {}).join(", ");
|
|
12
14
|
return ai.tool({
|
|
13
|
-
description:
|
|
15
|
+
description: `Execute a JavaScript or TypeScript script.
|
|
16
|
+
${envVars.length > 0 ? `You can use the following environment variables: ${envVars}` : ""}
|
|
17
|
+
${nodeModules.length > 0 ? `You can use the following node modules: ${nodeModules}` : "You cannot use any node modules."}`,
|
|
14
18
|
parameters: zod.z.object({
|
|
15
19
|
script: zod.z.string().describe(`
|
|
16
20
|
The JavaScript or TypeScript script to execute, must be in the format of:
|
|
17
21
|
|
|
22
|
+
import { someModule } from "someModule";
|
|
18
23
|
export default () => {
|
|
19
24
|
... your code here ...
|
|
20
25
|
return output;
|
|
@@ -22,6 +27,8 @@ const executeTool = (config) => {
|
|
|
22
27
|
|
|
23
28
|
or for async functions:
|
|
24
29
|
|
|
30
|
+
import { someModule } from "someModule";
|
|
31
|
+
|
|
25
32
|
export default async () => {
|
|
26
33
|
... your code here ...
|
|
27
34
|
return output;
|
|
@@ -29,9 +36,56 @@ const executeTool = (config) => {
|
|
|
29
36
|
`)
|
|
30
37
|
}),
|
|
31
38
|
execute: async ({ script }) => {
|
|
32
|
-
|
|
39
|
+
try {
|
|
40
|
+
const res = await api.executeScript(script, config);
|
|
41
|
+
return res;
|
|
42
|
+
} catch (e) {
|
|
43
|
+
console.log("ERROR: ", e.message);
|
|
44
|
+
return `Error executing script:
|
|
45
|
+
|
|
46
|
+
${script}
|
|
47
|
+
|
|
48
|
+
Error: ${e.message}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
const deployWebTool = (config) => {
|
|
54
|
+
const api = new index.FreestyleSandboxes({
|
|
55
|
+
...config
|
|
56
|
+
});
|
|
57
|
+
const envVars = Object.keys(config.envVars ?? {}).join(", ");
|
|
58
|
+
const nodeModules = Object.keys(config.nodeModules ?? {}).join(", ");
|
|
59
|
+
return ai.tool({
|
|
60
|
+
description: `Deploy a Web project. ${envVars.length > 0 ? `You can use the following environment variables: ${envVars}` : ""}
|
|
61
|
+
${nodeModules.length > 0 ? `You can use the following node modules: ${nodeModules}` : "You cannot use any node modules."}`,
|
|
62
|
+
parameters: zod.z.object({
|
|
63
|
+
files: zod.z.record(zod.z.string()).describe(`
|
|
64
|
+
A record of file names and their contents to deploy. For example:
|
|
65
|
+
{
|
|
66
|
+
"index.js": "import http from 'node:http';\\nnconsole.log('starting server');\\n\\nconst server = http.createServer(async(req, res) => {\\n res.writeHead(200, { 'Content-Type': 'text/plain' });\\n res.end('Welcome to New York its been waiting for you');\\n});\\n\\nserver.listen(3000, () => {\\n console.log('Server is running at http://localhost:3000');\\n});",
|
|
67
|
+
}
|
|
68
|
+
`)
|
|
69
|
+
}),
|
|
70
|
+
execute: async ({ files }) => {
|
|
71
|
+
try {
|
|
72
|
+
const res = await api.deployWeb(files, config);
|
|
73
|
+
return res;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
console.log("ERROR: ", e.message);
|
|
76
|
+
return `Error deploying web project:
|
|
77
|
+
|
|
78
|
+
${JSON.stringify(
|
|
79
|
+
files,
|
|
80
|
+
null,
|
|
81
|
+
2
|
|
82
|
+
)}
|
|
83
|
+
|
|
84
|
+
Error: ${e.message}`;
|
|
85
|
+
}
|
|
33
86
|
}
|
|
34
87
|
});
|
|
35
88
|
};
|
|
36
89
|
|
|
90
|
+
exports.deployWebTool = deployWebTool;
|
|
37
91
|
exports.executeTool = executeTool;
|
package/dist/ai/index.d.cts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import * as ai from 'ai';
|
|
2
|
-
import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess } from '../types.gen-BoJEFWW-.js';
|
|
2
|
+
import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess, c as FreestyleDeployWebSuccessResponse } from '../types.gen-BoJEFWW-.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Execute a JavaScript or TypeScript script
|
|
7
7
|
*
|
|
8
|
-
* **Note: Unless you tell it, the AI won't be aware of any node_modules or environment variables you give it, you need to tell it in your prompt what it can use.**
|
|
9
|
-
|
|
10
8
|
*
|
|
11
9
|
* @param config - Configuration for the tool
|
|
12
10
|
* @param config.apiKey - The API key to use
|
|
@@ -20,10 +18,28 @@ declare const executeTool: (config: FreestyleExecuteScriptParamsConfiguration &
|
|
|
20
18
|
script?: string;
|
|
21
19
|
}, {
|
|
22
20
|
script?: string;
|
|
23
|
-
}>, FreestyleExecureScriptResultSuccess> & {
|
|
21
|
+
}>, string | FreestyleExecureScriptResultSuccess> & {
|
|
24
22
|
execute: (args: {
|
|
25
23
|
script?: string;
|
|
26
|
-
}, options: ai.ToolExecutionOptions) => PromiseLike<FreestyleExecureScriptResultSuccess>;
|
|
24
|
+
}, options: ai.ToolExecutionOptions) => PromiseLike<string | FreestyleExecureScriptResultSuccess>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Deploy a Web project
|
|
28
|
+
* @param config - Configuration for the tool
|
|
29
|
+
* @param config.apiKey - The API key to use
|
|
30
|
+
*/
|
|
31
|
+
declare const deployWebTool: (config: FreestyleExecuteScriptParamsConfiguration & {
|
|
32
|
+
apiKey: string;
|
|
33
|
+
}) => ai.CoreTool<z.ZodObject<{
|
|
34
|
+
files: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
35
|
+
}, "strip", z.ZodTypeAny, {
|
|
36
|
+
files?: Record<string, string>;
|
|
37
|
+
}, {
|
|
38
|
+
files?: Record<string, string>;
|
|
39
|
+
}>, string | FreestyleDeployWebSuccessResponse> & {
|
|
40
|
+
execute: (args: {
|
|
41
|
+
files?: Record<string, string>;
|
|
42
|
+
}, options: ai.ToolExecutionOptions) => PromiseLike<string | FreestyleDeployWebSuccessResponse>;
|
|
27
43
|
};
|
|
28
44
|
|
|
29
|
-
export { executeTool };
|
|
45
|
+
export { deployWebTool, executeTool };
|
package/dist/ai/index.d.mts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import * as ai from 'ai';
|
|
2
|
-
import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess } from '../types.gen-BoJEFWW-.js';
|
|
2
|
+
import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess, c as FreestyleDeployWebSuccessResponse } from '../types.gen-BoJEFWW-.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Execute a JavaScript or TypeScript script
|
|
7
7
|
*
|
|
8
|
-
* **Note: Unless you tell it, the AI won't be aware of any node_modules or environment variables you give it, you need to tell it in your prompt what it can use.**
|
|
9
|
-
|
|
10
8
|
*
|
|
11
9
|
* @param config - Configuration for the tool
|
|
12
10
|
* @param config.apiKey - The API key to use
|
|
@@ -20,10 +18,28 @@ declare const executeTool: (config: FreestyleExecuteScriptParamsConfiguration &
|
|
|
20
18
|
script?: string;
|
|
21
19
|
}, {
|
|
22
20
|
script?: string;
|
|
23
|
-
}>, FreestyleExecureScriptResultSuccess> & {
|
|
21
|
+
}>, string | FreestyleExecureScriptResultSuccess> & {
|
|
24
22
|
execute: (args: {
|
|
25
23
|
script?: string;
|
|
26
|
-
}, options: ai.ToolExecutionOptions) => PromiseLike<FreestyleExecureScriptResultSuccess>;
|
|
24
|
+
}, options: ai.ToolExecutionOptions) => PromiseLike<string | FreestyleExecureScriptResultSuccess>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Deploy a Web project
|
|
28
|
+
* @param config - Configuration for the tool
|
|
29
|
+
* @param config.apiKey - The API key to use
|
|
30
|
+
*/
|
|
31
|
+
declare const deployWebTool: (config: FreestyleExecuteScriptParamsConfiguration & {
|
|
32
|
+
apiKey: string;
|
|
33
|
+
}) => ai.CoreTool<z.ZodObject<{
|
|
34
|
+
files: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
35
|
+
}, "strip", z.ZodTypeAny, {
|
|
36
|
+
files?: Record<string, string>;
|
|
37
|
+
}, {
|
|
38
|
+
files?: Record<string, string>;
|
|
39
|
+
}>, string | FreestyleDeployWebSuccessResponse> & {
|
|
40
|
+
execute: (args: {
|
|
41
|
+
files?: Record<string, string>;
|
|
42
|
+
}, options: ai.ToolExecutionOptions) => PromiseLike<string | FreestyleDeployWebSuccessResponse>;
|
|
27
43
|
};
|
|
28
44
|
|
|
29
|
-
export { executeTool };
|
|
45
|
+
export { deployWebTool, executeTool };
|
package/dist/ai/index.mjs
CHANGED
|
@@ -5,14 +5,19 @@ import '@hey-api/client-fetch';
|
|
|
5
5
|
|
|
6
6
|
const executeTool = (config) => {
|
|
7
7
|
const api = new FreestyleSandboxes({
|
|
8
|
-
|
|
8
|
+
...config
|
|
9
9
|
});
|
|
10
|
+
const envVars = Object.keys(config.envVars ?? {}).join(", ");
|
|
11
|
+
const nodeModules = Object.keys(config.nodeModules ?? {}).join(", ");
|
|
10
12
|
return tool({
|
|
11
|
-
description:
|
|
13
|
+
description: `Execute a JavaScript or TypeScript script.
|
|
14
|
+
${envVars.length > 0 ? `You can use the following environment variables: ${envVars}` : ""}
|
|
15
|
+
${nodeModules.length > 0 ? `You can use the following node modules: ${nodeModules}` : "You cannot use any node modules."}`,
|
|
12
16
|
parameters: z.object({
|
|
13
17
|
script: z.string().describe(`
|
|
14
18
|
The JavaScript or TypeScript script to execute, must be in the format of:
|
|
15
19
|
|
|
20
|
+
import { someModule } from "someModule";
|
|
16
21
|
export default () => {
|
|
17
22
|
... your code here ...
|
|
18
23
|
return output;
|
|
@@ -20,6 +25,8 @@ const executeTool = (config) => {
|
|
|
20
25
|
|
|
21
26
|
or for async functions:
|
|
22
27
|
|
|
28
|
+
import { someModule } from "someModule";
|
|
29
|
+
|
|
23
30
|
export default async () => {
|
|
24
31
|
... your code here ...
|
|
25
32
|
return output;
|
|
@@ -27,9 +34,55 @@ const executeTool = (config) => {
|
|
|
27
34
|
`)
|
|
28
35
|
}),
|
|
29
36
|
execute: async ({ script }) => {
|
|
30
|
-
|
|
37
|
+
try {
|
|
38
|
+
const res = await api.executeScript(script, config);
|
|
39
|
+
return res;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.log("ERROR: ", e.message);
|
|
42
|
+
return `Error executing script:
|
|
43
|
+
|
|
44
|
+
${script}
|
|
45
|
+
|
|
46
|
+
Error: ${e.message}`;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const deployWebTool = (config) => {
|
|
52
|
+
const api = new FreestyleSandboxes({
|
|
53
|
+
...config
|
|
54
|
+
});
|
|
55
|
+
const envVars = Object.keys(config.envVars ?? {}).join(", ");
|
|
56
|
+
const nodeModules = Object.keys(config.nodeModules ?? {}).join(", ");
|
|
57
|
+
return tool({
|
|
58
|
+
description: `Deploy a Web project. ${envVars.length > 0 ? `You can use the following environment variables: ${envVars}` : ""}
|
|
59
|
+
${nodeModules.length > 0 ? `You can use the following node modules: ${nodeModules}` : "You cannot use any node modules."}`,
|
|
60
|
+
parameters: z.object({
|
|
61
|
+
files: z.record(z.string()).describe(`
|
|
62
|
+
A record of file names and their contents to deploy. For example:
|
|
63
|
+
{
|
|
64
|
+
"index.js": "import http from 'node:http';\\nnconsole.log('starting server');\\n\\nconst server = http.createServer(async(req, res) => {\\n res.writeHead(200, { 'Content-Type': 'text/plain' });\\n res.end('Welcome to New York its been waiting for you');\\n});\\n\\nserver.listen(3000, () => {\\n console.log('Server is running at http://localhost:3000');\\n});",
|
|
65
|
+
}
|
|
66
|
+
`)
|
|
67
|
+
}),
|
|
68
|
+
execute: async ({ files }) => {
|
|
69
|
+
try {
|
|
70
|
+
const res = await api.deployWeb(files, config);
|
|
71
|
+
return res;
|
|
72
|
+
} catch (e) {
|
|
73
|
+
console.log("ERROR: ", e.message);
|
|
74
|
+
return `Error deploying web project:
|
|
75
|
+
|
|
76
|
+
${JSON.stringify(
|
|
77
|
+
files,
|
|
78
|
+
null,
|
|
79
|
+
2
|
|
80
|
+
)}
|
|
81
|
+
|
|
82
|
+
Error: ${e.message}`;
|
|
83
|
+
}
|
|
31
84
|
}
|
|
32
85
|
});
|
|
33
86
|
};
|
|
34
87
|
|
|
35
|
-
export { executeTool };
|
|
88
|
+
export { deployWebTool, executeTool };
|
package/dist/index.cjs
CHANGED
|
@@ -58,7 +58,17 @@ class FreestyleSandboxes {
|
|
|
58
58
|
if (response.data) {
|
|
59
59
|
return response.data;
|
|
60
60
|
} else {
|
|
61
|
-
throw new Error(
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Failed to execute script:
|
|
63
|
+
|
|
64
|
+
${script}
|
|
65
|
+
|
|
66
|
+
Error:
|
|
67
|
+
|
|
68
|
+
${JSON.stringify(
|
|
69
|
+
response
|
|
70
|
+
)}`
|
|
71
|
+
);
|
|
62
72
|
}
|
|
63
73
|
}
|
|
64
74
|
/**
|
|
@@ -117,7 +127,7 @@ class FreestyleSandboxes {
|
|
|
117
127
|
* @returns The logs for the sandbox.
|
|
118
128
|
* @throws An error if the logs could not be retrieved.
|
|
119
129
|
*/
|
|
120
|
-
async
|
|
130
|
+
async getWebLogs(id) {
|
|
121
131
|
const response = await handleGetLogs({
|
|
122
132
|
client: this.client,
|
|
123
133
|
path: {
|
package/dist/index.d.cts
CHANGED
|
@@ -15,11 +15,11 @@ declare class FreestyleSandboxes {
|
|
|
15
15
|
/**
|
|
16
16
|
* Execute a script in a sandbox.
|
|
17
17
|
*/
|
|
18
|
-
executeScript(script: string, config
|
|
18
|
+
executeScript(script: string, config?: FreestyleExecuteScriptParamsConfiguration): Promise<FreestyleExecureScriptResultSuccess>;
|
|
19
19
|
/**
|
|
20
20
|
* Deploy a Web project to a sandbox.
|
|
21
21
|
*/
|
|
22
|
-
deployWeb(files: Record<string, string>, config
|
|
22
|
+
deployWeb(files: Record<string, string>, config?: FreestyleDeployWebConfiguration): Promise<FreestyleDeployWebSuccessResponse>;
|
|
23
23
|
/**
|
|
24
24
|
* Deploy a Cloudstate project to a sandbox.
|
|
25
25
|
*/
|
|
@@ -37,7 +37,7 @@ declare class FreestyleSandboxes {
|
|
|
37
37
|
* @returns The logs for the sandbox.
|
|
38
38
|
* @throws An error if the logs could not be retrieved.
|
|
39
39
|
*/
|
|
40
|
-
|
|
40
|
+
getWebLogs(id: string): Promise<HandleGetLogsResponse>;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export { FreestyleSandboxes };
|
package/dist/index.d.mts
CHANGED
|
@@ -15,11 +15,11 @@ declare class FreestyleSandboxes {
|
|
|
15
15
|
/**
|
|
16
16
|
* Execute a script in a sandbox.
|
|
17
17
|
*/
|
|
18
|
-
executeScript(script: string, config
|
|
18
|
+
executeScript(script: string, config?: FreestyleExecuteScriptParamsConfiguration): Promise<FreestyleExecureScriptResultSuccess>;
|
|
19
19
|
/**
|
|
20
20
|
* Deploy a Web project to a sandbox.
|
|
21
21
|
*/
|
|
22
|
-
deployWeb(files: Record<string, string>, config
|
|
22
|
+
deployWeb(files: Record<string, string>, config?: FreestyleDeployWebConfiguration): Promise<FreestyleDeployWebSuccessResponse>;
|
|
23
23
|
/**
|
|
24
24
|
* Deploy a Cloudstate project to a sandbox.
|
|
25
25
|
*/
|
|
@@ -37,7 +37,7 @@ declare class FreestyleSandboxes {
|
|
|
37
37
|
* @returns The logs for the sandbox.
|
|
38
38
|
* @throws An error if the logs could not be retrieved.
|
|
39
39
|
*/
|
|
40
|
-
|
|
40
|
+
getWebLogs(id: string): Promise<HandleGetLogsResponse>;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export { FreestyleSandboxes };
|
package/dist/index.mjs
CHANGED
|
@@ -56,7 +56,17 @@ class FreestyleSandboxes {
|
|
|
56
56
|
if (response.data) {
|
|
57
57
|
return response.data;
|
|
58
58
|
} else {
|
|
59
|
-
throw new Error(
|
|
59
|
+
throw new Error(
|
|
60
|
+
`Failed to execute script:
|
|
61
|
+
|
|
62
|
+
${script}
|
|
63
|
+
|
|
64
|
+
Error:
|
|
65
|
+
|
|
66
|
+
${JSON.stringify(
|
|
67
|
+
response
|
|
68
|
+
)}`
|
|
69
|
+
);
|
|
60
70
|
}
|
|
61
71
|
}
|
|
62
72
|
/**
|
|
@@ -115,7 +125,7 @@ class FreestyleSandboxes {
|
|
|
115
125
|
* @returns The logs for the sandbox.
|
|
116
126
|
* @throws An error if the logs could not be retrieved.
|
|
117
127
|
*/
|
|
118
|
-
async
|
|
128
|
+
async getWebLogs(id) {
|
|
119
129
|
const response = await handleGetLogs({
|
|
120
130
|
client: this.client,
|
|
121
131
|
path: {
|
package/package.json
CHANGED
package/src/ai/index.ts
CHANGED
|
@@ -5,9 +5,7 @@ import { z } from "zod";
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Execute a JavaScript or TypeScript script
|
|
8
|
-
*
|
|
9
|
-
* **Note: Unless you tell it, the AI won't be aware of any node_modules or environment variables you give it, you need to tell it in your prompt what it can use.**
|
|
10
|
-
|
|
8
|
+
*
|
|
11
9
|
*
|
|
12
10
|
* @param config - Configuration for the tool
|
|
13
11
|
* @param config.apiKey - The API key to use
|
|
@@ -19,14 +17,26 @@ export const executeTool = (
|
|
|
19
17
|
}
|
|
20
18
|
) => {
|
|
21
19
|
const api = new FreestyleSandboxes({
|
|
22
|
-
|
|
20
|
+
...config,
|
|
23
21
|
});
|
|
22
|
+
|
|
23
|
+
const envVars = Object.keys(config.envVars ?? {}).join(", ");
|
|
24
|
+
const nodeModules = Object.keys(config.nodeModules ?? {}).join(", ");
|
|
24
25
|
return tool({
|
|
25
|
-
description:
|
|
26
|
+
description: `Execute a JavaScript or TypeScript script.\n${
|
|
27
|
+
envVars.length > 0
|
|
28
|
+
? `You can use the following environment variables: ${envVars}`
|
|
29
|
+
: ""
|
|
30
|
+
}\n${
|
|
31
|
+
nodeModules.length > 0
|
|
32
|
+
? `You can use the following node modules: ${nodeModules}`
|
|
33
|
+
: "You cannot use any node modules."
|
|
34
|
+
}`,
|
|
26
35
|
parameters: z.object({
|
|
27
36
|
script: z.string().describe(`
|
|
28
37
|
The JavaScript or TypeScript script to execute, must be in the format of:
|
|
29
38
|
|
|
39
|
+
import { someModule } from "someModule";
|
|
30
40
|
export default () => {
|
|
31
41
|
... your code here ...
|
|
32
42
|
return output;
|
|
@@ -34,6 +44,8 @@ export const executeTool = (
|
|
|
34
44
|
|
|
35
45
|
or for async functions:
|
|
36
46
|
|
|
47
|
+
import { someModule } from "someModule";
|
|
48
|
+
|
|
37
49
|
export default async () => {
|
|
38
50
|
... your code here ...
|
|
39
51
|
return output;
|
|
@@ -41,7 +53,63 @@ export const executeTool = (
|
|
|
41
53
|
`),
|
|
42
54
|
}),
|
|
43
55
|
execute: async ({ script }) => {
|
|
44
|
-
|
|
56
|
+
try {
|
|
57
|
+
const res = await api.executeScript(script, config);
|
|
58
|
+
return res;
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.log("ERROR: ", e.message);
|
|
61
|
+
return `Error executing script:\n\n${script}\n\nError: ${e.message}`;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Deploy a Web project
|
|
69
|
+
* @param config - Configuration for the tool
|
|
70
|
+
* @param config.apiKey - The API key to use
|
|
71
|
+
*/
|
|
72
|
+
export const deployWebTool = (
|
|
73
|
+
config: FreestyleExecuteScriptParamsConfiguration & {
|
|
74
|
+
apiKey: string;
|
|
75
|
+
}
|
|
76
|
+
) => {
|
|
77
|
+
const api = new FreestyleSandboxes({
|
|
78
|
+
...config,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const envVars = Object.keys(config.envVars ?? {}).join(", ");
|
|
82
|
+
const nodeModules = Object.keys(config.nodeModules ?? {}).join(", ");
|
|
83
|
+
return tool({
|
|
84
|
+
description: `Deploy a Web project. ${
|
|
85
|
+
envVars.length > 0
|
|
86
|
+
? `You can use the following environment variables: ${envVars}`
|
|
87
|
+
: ""
|
|
88
|
+
}\n${
|
|
89
|
+
nodeModules.length > 0
|
|
90
|
+
? `You can use the following node modules: ${nodeModules}`
|
|
91
|
+
: "You cannot use any node modules."
|
|
92
|
+
}`,
|
|
93
|
+
parameters: z.object({
|
|
94
|
+
files: z.record(z.string()).describe(`
|
|
95
|
+
A record of file names and their contents to deploy. For example:
|
|
96
|
+
{
|
|
97
|
+
"index.js": "import http from 'node:http';\\nnconsole.log('starting server');\\n\\nconst server = http.createServer(async(req, res) => {\\n res.writeHead(200, { 'Content-Type': 'text/plain' });\\n res.end('Welcome to New York its been waiting for you');\\n});\\n\\nserver.listen(3000, () => {\\n console.log('Server is running at http://localhost:3000');\\n});",
|
|
98
|
+
}
|
|
99
|
+
`),
|
|
100
|
+
}),
|
|
101
|
+
execute: async ({ files }) => {
|
|
102
|
+
try {
|
|
103
|
+
const res = await api.deployWeb(files, config);
|
|
104
|
+
return res;
|
|
105
|
+
} catch (e) {
|
|
106
|
+
console.log("ERROR: ", e.message);
|
|
107
|
+
return `Error deploying web project:\n\n${JSON.stringify(
|
|
108
|
+
files,
|
|
109
|
+
null,
|
|
110
|
+
2
|
|
111
|
+
)}\n\nError: ${e.message}`;
|
|
112
|
+
}
|
|
45
113
|
},
|
|
46
114
|
});
|
|
47
115
|
};
|
package/src/index.ts
CHANGED
|
@@ -25,7 +25,7 @@ export class FreestyleSandboxes {
|
|
|
25
25
|
*/
|
|
26
26
|
async executeScript(
|
|
27
27
|
script: string,
|
|
28
|
-
config
|
|
28
|
+
config?: sandbox_openapi.FreestyleExecuteScriptParamsConfiguration
|
|
29
29
|
): Promise<sandbox_openapi.FreestyleExecureScriptResultSuccess> {
|
|
30
30
|
const response = await sandbox_openapi.handleExecuteScript({
|
|
31
31
|
client: this.client,
|
|
@@ -34,10 +34,15 @@ export class FreestyleSandboxes {
|
|
|
34
34
|
config: config,
|
|
35
35
|
},
|
|
36
36
|
});
|
|
37
|
+
|
|
37
38
|
if (response.data) {
|
|
38
39
|
return response.data;
|
|
39
40
|
} else {
|
|
40
|
-
throw new Error(
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Failed to execute script: \n\n${script}\n\nError:\n\n${JSON.stringify(
|
|
43
|
+
response
|
|
44
|
+
)}`
|
|
45
|
+
);
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
|
|
@@ -46,7 +51,7 @@ export class FreestyleSandboxes {
|
|
|
46
51
|
*/
|
|
47
52
|
async deployWeb(
|
|
48
53
|
files: Record<string, string>,
|
|
49
|
-
config
|
|
54
|
+
config?: sandbox_openapi.FreestyleDeployWebConfiguration
|
|
50
55
|
): Promise<sandbox_openapi.FreestyleDeployWebSuccessResponse> {
|
|
51
56
|
const response = await sandbox_openapi.handleDeployWeb({
|
|
52
57
|
client: this.client,
|
|
@@ -107,7 +112,7 @@ export class FreestyleSandboxes {
|
|
|
107
112
|
* @returns The logs for the sandbox.
|
|
108
113
|
* @throws An error if the logs could not be retrieved.
|
|
109
114
|
*/
|
|
110
|
-
async
|
|
115
|
+
async getWebLogs(id: string): Promise<sandbox_openapi.HandleGetLogsResponse> {
|
|
111
116
|
const response = await sandbox_openapi.handleGetLogs({
|
|
112
117
|
client: this.client,
|
|
113
118
|
path: {
|