freestyle-sandboxes 0.0.1 → 0.0.4

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 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
- apiKey: config.apiKey
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: "Execute a JavaScript or TypeScript script",
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
- return api.executeScript(script, config);
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;
@@ -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-CKNcEsUr.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 };
@@ -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-CKNcEsUr.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
- apiKey: config.apiKey
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: "Execute a JavaScript or TypeScript script",
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
- return api.executeScript(script, config);
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("Failed to execute script");
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 getLogs(id) {
130
+ async getWebLogs(id) {
121
131
  const response = await handleGetLogs({
122
132
  client: this.client,
123
133
  path: {
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
- import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess, b as FreestyleDeployWebConfiguration, c as FreestyleDeployWebSuccessResponse, d as FreestyleCloudstateDeployRequest, e as FreestyleCloudstateDeploySuccessResponse, H as HandleBackupCloudstateResponse, f as HandleGetLogsResponse } from './types.gen-BoJEFWW-.js';
1
+ import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess, b as FreestyleDeployWebConfiguration, c as FreestyleDeployWebSuccessResponse, d as FreestyleCloudstateDeployRequest, e as FreestyleCloudstateDeploySuccessResponse, H as HandleBackupCloudstateResponse, f as HandleGetLogsResponse } from './types.gen-CKNcEsUr.js';
2
+ export { g as FreestyleCloudstateDeployConfiguration, h as FreestyleCloudstateDeployErrorResponse, i as FreestyleDeployWebErrorResponse, j as FreestyleDeployWebPayload, k as FreestyleExecureScriptResultError, l as FreestyleExecuteScriptParams, m as FreestyleLogResponseObject, q as HandleBackupCloudstateError, n as HandleDeployCloudstateData, p as HandleDeployCloudstateError, o as HandleDeployCloudstateResponse, u as HandleDeployWebData, w as HandleDeployWebError, v as HandleDeployWebResponse, r as HandleExecuteScriptData, t as HandleExecuteScriptError, s as HandleExecuteScriptResponse, x as HandleGetLogsError } from './types.gen-CKNcEsUr.js';
2
3
 
3
4
  declare class FreestyleSandboxes {
4
5
  private client;
@@ -15,11 +16,11 @@ declare class FreestyleSandboxes {
15
16
  /**
16
17
  * Execute a script in a sandbox.
17
18
  */
18
- executeScript(script: string, config: FreestyleExecuteScriptParamsConfiguration): Promise<FreestyleExecureScriptResultSuccess>;
19
+ executeScript(script: string, config?: FreestyleExecuteScriptParamsConfiguration): Promise<FreestyleExecureScriptResultSuccess>;
19
20
  /**
20
21
  * Deploy a Web project to a sandbox.
21
22
  */
22
- deployWeb(files: Record<string, string>, config: FreestyleDeployWebConfiguration): Promise<FreestyleDeployWebSuccessResponse>;
23
+ deployWeb(files: Record<string, string>, config?: FreestyleDeployWebConfiguration): Promise<FreestyleDeployWebSuccessResponse>;
23
24
  /**
24
25
  * Deploy a Cloudstate project to a sandbox.
25
26
  */
@@ -37,7 +38,7 @@ declare class FreestyleSandboxes {
37
38
  * @returns The logs for the sandbox.
38
39
  * @throws An error if the logs could not be retrieved.
39
40
  */
40
- getLogs(id: string): Promise<HandleGetLogsResponse>;
41
+ getWebLogs(id: string): Promise<HandleGetLogsResponse>;
41
42
  }
42
43
 
43
- export { FreestyleSandboxes };
44
+ export { FreestyleCloudstateDeployRequest, FreestyleCloudstateDeploySuccessResponse, FreestyleDeployWebConfiguration, FreestyleDeployWebSuccessResponse, FreestyleExecureScriptResultSuccess, FreestyleExecuteScriptParamsConfiguration, FreestyleSandboxes, HandleBackupCloudstateResponse, HandleGetLogsResponse };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess, b as FreestyleDeployWebConfiguration, c as FreestyleDeployWebSuccessResponse, d as FreestyleCloudstateDeployRequest, e as FreestyleCloudstateDeploySuccessResponse, H as HandleBackupCloudstateResponse, f as HandleGetLogsResponse } from './types.gen-BoJEFWW-.js';
1
+ import { F as FreestyleExecuteScriptParamsConfiguration, a as FreestyleExecureScriptResultSuccess, b as FreestyleDeployWebConfiguration, c as FreestyleDeployWebSuccessResponse, d as FreestyleCloudstateDeployRequest, e as FreestyleCloudstateDeploySuccessResponse, H as HandleBackupCloudstateResponse, f as HandleGetLogsResponse } from './types.gen-CKNcEsUr.js';
2
+ export { g as FreestyleCloudstateDeployConfiguration, h as FreestyleCloudstateDeployErrorResponse, i as FreestyleDeployWebErrorResponse, j as FreestyleDeployWebPayload, k as FreestyleExecureScriptResultError, l as FreestyleExecuteScriptParams, m as FreestyleLogResponseObject, q as HandleBackupCloudstateError, n as HandleDeployCloudstateData, p as HandleDeployCloudstateError, o as HandleDeployCloudstateResponse, u as HandleDeployWebData, w as HandleDeployWebError, v as HandleDeployWebResponse, r as HandleExecuteScriptData, t as HandleExecuteScriptError, s as HandleExecuteScriptResponse, x as HandleGetLogsError } from './types.gen-CKNcEsUr.js';
2
3
 
3
4
  declare class FreestyleSandboxes {
4
5
  private client;
@@ -15,11 +16,11 @@ declare class FreestyleSandboxes {
15
16
  /**
16
17
  * Execute a script in a sandbox.
17
18
  */
18
- executeScript(script: string, config: FreestyleExecuteScriptParamsConfiguration): Promise<FreestyleExecureScriptResultSuccess>;
19
+ executeScript(script: string, config?: FreestyleExecuteScriptParamsConfiguration): Promise<FreestyleExecureScriptResultSuccess>;
19
20
  /**
20
21
  * Deploy a Web project to a sandbox.
21
22
  */
22
- deployWeb(files: Record<string, string>, config: FreestyleDeployWebConfiguration): Promise<FreestyleDeployWebSuccessResponse>;
23
+ deployWeb(files: Record<string, string>, config?: FreestyleDeployWebConfiguration): Promise<FreestyleDeployWebSuccessResponse>;
23
24
  /**
24
25
  * Deploy a Cloudstate project to a sandbox.
25
26
  */
@@ -37,7 +38,7 @@ declare class FreestyleSandboxes {
37
38
  * @returns The logs for the sandbox.
38
39
  * @throws An error if the logs could not be retrieved.
39
40
  */
40
- getLogs(id: string): Promise<HandleGetLogsResponse>;
41
+ getWebLogs(id: string): Promise<HandleGetLogsResponse>;
41
42
  }
42
43
 
43
- export { FreestyleSandboxes };
44
+ export { FreestyleCloudstateDeployRequest, FreestyleCloudstateDeploySuccessResponse, FreestyleDeployWebConfiguration, FreestyleDeployWebSuccessResponse, FreestyleExecureScriptResultSuccess, FreestyleExecuteScriptParamsConfiguration, FreestyleSandboxes, HandleBackupCloudstateResponse, HandleGetLogsResponse };
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("Failed to execute script");
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 getLogs(id) {
128
+ async getWebLogs(id) {
119
129
  const response = await handleGetLogs({
120
130
  client: this.client,
121
131
  path: {
@@ -0,0 +1,128 @@
1
+ type FreestyleCloudstateDeployConfiguration = {
2
+ /**
3
+ * ID of the project to deploy, if not provided will create a new project
4
+ */
5
+ projectId?: string | null;
6
+ /**
7
+ * The environment variables that the cloudstate deploy can access
8
+ */
9
+ envVars?: {
10
+ [key: string]: string;
11
+ };
12
+ };
13
+ type FreestyleCloudstateDeployErrorResponse = {
14
+ message: string;
15
+ };
16
+ type FreestyleCloudstateDeployRequest = {
17
+ classes: string;
18
+ config?: FreestyleCloudstateDeployConfiguration;
19
+ };
20
+ type FreestyleCloudstateDeploySuccessResponse = {
21
+ /**
22
+ * The id of the project deployed to
23
+ */
24
+ projectId: string;
25
+ };
26
+ type FreestyleDeployWebConfiguration = {
27
+ /**
28
+ * The entrypoint file for the website
29
+ */
30
+ entrypoint?: string | null;
31
+ /**
32
+ * The custom domains for the website, eg. [\"subdomain.yourwebsite.com\"]. You may not include *.style.dev domains here, those are reserved for projectIds
33
+ */
34
+ domains?: Array<string> | null;
35
+ /**
36
+ * The project id to deploy to, if not provided will create a new project, may be used to provision a new project with a specific id if that id is available
37
+ */
38
+ projectId?: string | null;
39
+ /**
40
+ * Node Modules to install for the website, a map of package names to versions, e.g. { \"express\": \"4.17.1\" }. If this and a package-lock.json are provided, the package-lock.json, bun.lockb, pnpm-lock.yaml, or yarn.lock is also provided, the versions here will override the versions in those lock files.
41
+ */
42
+ nodeModules?: {
43
+ [key: string]: string;
44
+ } | null;
45
+ /**
46
+ * The environment variables that the website can access
47
+ * e.g. { \"RESEND_API_KEY\": \"re_123456789\" }
48
+ */
49
+ envVars?: {
50
+ [key: string]: string;
51
+ } | null;
52
+ };
53
+ type FreestyleDeployWebErrorResponse = {
54
+ message: string;
55
+ };
56
+ type FreestyleDeployWebPayload = {
57
+ /**
58
+ * The files to deploy, a map of file paths to file contents, e.g. { \"index.js\": \"your main", \"file2.js\": \"your helper\" }
59
+ *
60
+ * **Do not include node modules in this bundle, they will not work**. Instead, includes a package-lock.json, bun.lockb, pnpm-lock.yaml, or yarn.lock, the node modules for the project will be installed from that lock file, or use the node_modules field in the configuration to specify the node modules to install.
61
+ */
62
+ files: {
63
+ [key: string]: string;
64
+ };
65
+ config?: FreestyleDeployWebConfiguration;
66
+ };
67
+ type FreestyleDeployWebSuccessResponse = {
68
+ projectId: string;
69
+ };
70
+ type FreestyleExecureScriptResultError = {
71
+ error: string;
72
+ };
73
+ type FreestyleExecureScriptResultSuccess = {
74
+ result: unknown;
75
+ };
76
+ type FreestyleExecuteScriptParams = {
77
+ /**
78
+ * The JavaScript or TypeScript script to execute
79
+ */
80
+ script: string;
81
+ config?: FreestyleExecuteScriptParamsConfiguration;
82
+ };
83
+ type FreestyleExecuteScriptParamsConfiguration = {
84
+ /**
85
+ * The environment variables to set for the script
86
+ */
87
+ envVars?: {
88
+ [key: string]: string;
89
+ };
90
+ /**
91
+ * The node modules to install for the script
92
+ */
93
+ nodeModules?: {
94
+ [key: string]: string;
95
+ };
96
+ /**
97
+ * Tags for you to organize your scripts, useful for tracking what you're running
98
+ */
99
+ tags?: Array<string>;
100
+ /**
101
+ * The script timeout
102
+ */
103
+ timeout?: string | null;
104
+ };
105
+ type FreestyleLogResponseObject = {
106
+ message: string;
107
+ };
108
+ type HandleDeployCloudstateData = {
109
+ body: FreestyleCloudstateDeployRequest;
110
+ };
111
+ type HandleDeployCloudstateResponse = FreestyleCloudstateDeploySuccessResponse;
112
+ type HandleDeployCloudstateError = FreestyleCloudstateDeployErrorResponse;
113
+ type HandleBackupCloudstateResponse = Array<number>;
114
+ type HandleBackupCloudstateError = unknown;
115
+ type HandleExecuteScriptData = {
116
+ body: FreestyleExecuteScriptParams;
117
+ };
118
+ type HandleExecuteScriptResponse = FreestyleExecureScriptResultSuccess;
119
+ type HandleExecuteScriptError = FreestyleExecureScriptResultError;
120
+ type HandleDeployWebData = {
121
+ body: FreestyleDeployWebPayload;
122
+ };
123
+ type HandleDeployWebResponse = FreestyleDeployWebSuccessResponse;
124
+ type HandleDeployWebError = FreestyleDeployWebErrorResponse;
125
+ type HandleGetLogsResponse = Array<FreestyleLogResponseObject>;
126
+ type HandleGetLogsError = unknown;
127
+
128
+ export type { FreestyleExecuteScriptParamsConfiguration as F, HandleBackupCloudstateResponse as H, FreestyleExecureScriptResultSuccess as a, FreestyleDeployWebConfiguration as b, FreestyleDeployWebSuccessResponse as c, FreestyleCloudstateDeployRequest as d, FreestyleCloudstateDeploySuccessResponse as e, HandleGetLogsResponse as f, FreestyleCloudstateDeployConfiguration as g, FreestyleCloudstateDeployErrorResponse as h, FreestyleDeployWebErrorResponse as i, FreestyleDeployWebPayload as j, FreestyleExecureScriptResultError as k, FreestyleExecuteScriptParams as l, FreestyleLogResponseObject as m, HandleDeployCloudstateData as n, HandleDeployCloudstateResponse as o, HandleDeployCloudstateError as p, HandleBackupCloudstateError as q, HandleExecuteScriptData as r, HandleExecuteScriptResponse as s, HandleExecuteScriptError as t, HandleDeployWebData as u, HandleDeployWebResponse as v, HandleDeployWebError as w, HandleGetLogsError as x };
@@ -1,153 +1,155 @@
1
1
  // This file is auto-generated by @hey-api/openapi-ts
2
2
 
3
3
  export type FreestyleCloudstateDeployConfiguration = {
4
- /**
5
- * ID of the project to deploy, if not provided will create a new project
6
- */
7
- projectId?: (string) | null;
8
- /**
9
- * The environment variables that the cloudstate deploy can access
10
- */
11
- envVars?: {
12
- [key: string]: (string);
13
- };
4
+ /**
5
+ * ID of the project to deploy, if not provided will create a new project
6
+ */
7
+ projectId?: string | null;
8
+ /**
9
+ * The environment variables that the cloudstate deploy can access
10
+ */
11
+ envVars?: {
12
+ [key: string]: string;
13
+ };
14
14
  };
15
15
 
16
16
  export type FreestyleCloudstateDeployErrorResponse = {
17
- message: string;
17
+ message: string;
18
18
  };
19
19
 
20
20
  export type FreestyleCloudstateDeployRequest = {
21
- classes: string;
22
- config?: FreestyleCloudstateDeployConfiguration;
21
+ classes: string;
22
+ config?: FreestyleCloudstateDeployConfiguration;
23
23
  };
24
24
 
25
25
  export type FreestyleCloudstateDeploySuccessResponse = {
26
- /**
27
- * The id of the project deployed to
28
- */
29
- projectId: string;
26
+ /**
27
+ * The id of the project deployed to
28
+ */
29
+ projectId: string;
30
30
  };
31
31
 
32
32
  export type FreestyleDeployWebConfiguration = {
33
- /**
34
- * The entrypoint file for the website
35
- */
36
- entrypoint?: (string) | null;
37
- /**
38
- * The custom domains for the website, eg. [\"subdomain.yourwebsite.com\"]. You may not include *.style.dev domains here, those are reserved for projectIds
39
- */
40
- domains?: Array<(string)> | null;
41
- /**
42
- * The project id to deploy to, if not provided will create a new project, may be used to provision a new project with a specific id if that id is available
43
- */
44
- projectId?: (string) | null;
45
- /**
46
- * Node Modules to install for the website, a map of package names to versions, e.g. { \"express\": \"4.17.1\" }. If this and a package-lock.json are provided, the package-lock.json, bun.lockb, pnpm-lock.yaml, or yarn.lock is also provided, the versions here will override the versions in those lock files.
47
- */
48
- nodeModules?: {
49
- [key: string]: (string);
50
- } | null;
51
- /**
52
- * The environment variables that the website can access
53
- * e.g. { \"RESEND_API_KEY\": \"re_123456789\" }
54
- */
55
- envVars?: {
56
- [key: string]: (string);
57
- } | null;
33
+ /**
34
+ * The entrypoint file for the website
35
+ */
36
+ entrypoint?: string | null;
37
+ /**
38
+ * The custom domains for the website, eg. [\"subdomain.yourwebsite.com\"]. You may not include *.style.dev domains here, those are reserved for projectIds
39
+ */
40
+ domains?: Array<string> | null;
41
+ /**
42
+ * The project id to deploy to, if not provided will create a new project, may be used to provision a new project with a specific id if that id is available
43
+ */
44
+ projectId?: string | null;
45
+ /**
46
+ * Node Modules to install for the website, a map of package names to versions, e.g. { \"express\": \"4.17.1\" }. If this and a package-lock.json are provided, the package-lock.json, bun.lockb, pnpm-lock.yaml, or yarn.lock is also provided, the versions here will override the versions in those lock files.
47
+ */
48
+ nodeModules?: {
49
+ [key: string]: string;
50
+ } | null;
51
+ /**
52
+ * The environment variables that the website can access
53
+ * e.g. { \"RESEND_API_KEY\": \"re_123456789\" }
54
+ */
55
+ envVars?: {
56
+ [key: string]: string;
57
+ } | null;
58
58
  };
59
59
 
60
60
  export type FreestyleDeployWebErrorResponse = {
61
- message: string;
61
+ message: string;
62
62
  };
63
63
 
64
64
  export type FreestyleDeployWebPayload = {
65
- /**
66
- * The files to deploy, a map of file paths to file contents, e.g. { \"index.js\": \"your main", \"file2.js\": \"your helper\" }
67
- *
68
- * **Do not include node modules in this bundle, they will not work**. Instead, includes a package-lock.json, bun.lockb, pnpm-lock.yaml, or yarn.lock, the node modules for the project will be installed from that lock file, or use the node_modules field in the configuration to specify the node modules to install.
69
- */
70
- files: {
71
- [key: string]: (string);
72
- };
73
- config?: FreestyleDeployWebConfiguration;
65
+ /**
66
+ * The files to deploy, a map of file paths to file contents, e.g. { \"index.js\": \"your main", \"file2.js\": \"your helper\" }
67
+ *
68
+ * **Do not include node modules in this bundle, they will not work**. Instead, includes a package-lock.json, bun.lockb, pnpm-lock.yaml, or yarn.lock, the node modules for the project will be installed from that lock file, or use the node_modules field in the configuration to specify the node modules to install.
69
+ */
70
+ files: {
71
+ [key: string]: string;
72
+ };
73
+ config?: FreestyleDeployWebConfiguration;
74
74
  };
75
75
 
76
76
  export type FreestyleDeployWebSuccessResponse = {
77
- projectId: string;
77
+ projectId: string;
78
78
  };
79
79
 
80
80
  export type FreestyleExecureScriptResultError = {
81
- error: string;
81
+ error: string;
82
82
  };
83
83
 
84
84
  export type FreestyleExecureScriptResultSuccess = {
85
- result: unknown;
85
+ result: unknown;
86
86
  };
87
87
 
88
88
  export type FreestyleExecuteScriptParams = {
89
- /**
90
- * The JavaScript or TypeScript script to execute
91
- */
92
- script: string;
93
- config?: FreestyleExecuteScriptParamsConfiguration;
89
+ /**
90
+ * The JavaScript or TypeScript script to execute
91
+ */
92
+ script: string;
93
+ config?: FreestyleExecuteScriptParamsConfiguration;
94
94
  };
95
95
 
96
96
  export type FreestyleExecuteScriptParamsConfiguration = {
97
- /**
98
- * The environment variables to set for the script
99
- */
100
- envVars?: {
101
- [key: string]: (string);
102
- };
103
- /**
104
- * The node modules to install for the script
105
- */
106
- nodeModules?: {
107
- [key: string]: (string);
108
- };
109
- /**
110
- * Tags for you to organize your scripts, useful for tracking what you're running
111
- */
112
- tags?: Array<(string)>;
113
- /**
114
- * The script timeout
115
- */
116
- timeout?: (string) | null;
97
+ /**
98
+ * The environment variables to set for the script
99
+ */
100
+ envVars?: {
101
+ [key: string]: string;
102
+ };
103
+ /**
104
+ * The node modules to install for the script
105
+ */
106
+ nodeModules?: {
107
+ [key: string]: string;
108
+ };
109
+ /**
110
+ * Tags for you to organize your scripts, useful for tracking what you're running
111
+ */
112
+ tags?: Array<string>;
113
+ /**
114
+ * The script timeout
115
+ */
116
+ timeout?: string | null;
117
117
  };
118
118
 
119
119
  export type FreestyleLogResponseObject = {
120
- message: string;
120
+ message: string;
121
121
  };
122
122
 
123
123
  export type HandleDeployCloudstateData = {
124
- body: FreestyleCloudstateDeployRequest;
124
+ body: FreestyleCloudstateDeployRequest;
125
125
  };
126
126
 
127
- export type HandleDeployCloudstateResponse = (FreestyleCloudstateDeploySuccessResponse);
127
+ export type HandleDeployCloudstateResponse =
128
+ FreestyleCloudstateDeploySuccessResponse;
128
129
 
129
- export type HandleDeployCloudstateError = (FreestyleCloudstateDeployErrorResponse);
130
+ export type HandleDeployCloudstateError =
131
+ FreestyleCloudstateDeployErrorResponse;
130
132
 
131
- export type HandleBackupCloudstateResponse = (Array<(number)>);
133
+ export type HandleBackupCloudstateResponse = Array<number>;
132
134
 
133
- export type HandleBackupCloudstateError = (unknown);
135
+ export type HandleBackupCloudstateError = unknown;
134
136
 
135
137
  export type HandleExecuteScriptData = {
136
- body: FreestyleExecuteScriptParams;
138
+ body: FreestyleExecuteScriptParams;
137
139
  };
138
140
 
139
- export type HandleExecuteScriptResponse = (FreestyleExecureScriptResultSuccess);
141
+ export type HandleExecuteScriptResponse = FreestyleExecureScriptResultSuccess;
140
142
 
141
- export type HandleExecuteScriptError = (FreestyleExecureScriptResultError);
143
+ export type HandleExecuteScriptError = FreestyleExecureScriptResultError;
142
144
 
143
145
  export type HandleDeployWebData = {
144
- body: FreestyleDeployWebPayload;
146
+ body: FreestyleDeployWebPayload;
145
147
  };
146
148
 
147
- export type HandleDeployWebResponse = (FreestyleDeployWebSuccessResponse);
149
+ export type HandleDeployWebResponse = FreestyleDeployWebSuccessResponse;
148
150
 
149
- export type HandleDeployWebError = (FreestyleDeployWebErrorResponse);
151
+ export type HandleDeployWebError = FreestyleDeployWebErrorResponse;
150
152
 
151
- export type HandleGetLogsResponse = (Array<FreestyleLogResponseObject>);
153
+ export type HandleGetLogsResponse = Array<FreestyleLogResponseObject>;
152
154
 
153
- export type HandleGetLogsError = unknown;
155
+ export type HandleGetLogsError = unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freestyle-sandboxes",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
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
- apiKey: config.apiKey,
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: "Execute a JavaScript or TypeScript script",
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
- return api.executeScript(script, config);
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: sandbox_openapi.FreestyleExecuteScriptParamsConfiguration
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("Failed to execute script");
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: sandbox_openapi.FreestyleDeployWebConfiguration
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 getLogs(id: string): Promise<sandbox_openapi.HandleGetLogsResponse> {
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: {
@@ -121,3 +126,5 @@ export class FreestyleSandboxes {
121
126
  }
122
127
  }
123
128
  }
129
+
130
+ export * from "../openapi/types.gen.ts";
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "allowImportingTsExtensions": true,
4
- "lib": ["ESNext"]
4
+ "lib": ["ESNext", "DOM"]
5
5
  }
6
6
  }