freestyle-sandboxes 0.0.67 → 0.0.69

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/index.mjs CHANGED
@@ -55,6 +55,42 @@ const handleEphemeralDevServer = (options) => {
55
55
  url: "/ephemeral/v1/dev-servers"
56
56
  });
57
57
  };
58
+ const handleExecOnEphemeralDevServer = (options) => {
59
+ return (options?.client ?? client).post({
60
+ ...options,
61
+ url: "/ephemeral/v1/dev-servers/exec"
62
+ });
63
+ };
64
+ const handleWriteFileFromEphemeralDevServer = (options) => {
65
+ return (options?.client ?? client).put({
66
+ ...options,
67
+ url: "/ephemeral/v1/dev-servers/files/{*filepath}"
68
+ });
69
+ };
70
+ const handleReadFileFromEphemeralDevServer = (options) => {
71
+ return (options?.client ?? client).post({
72
+ ...options,
73
+ url: "/ephemeral/v1/dev-servers/files/{*filepath}"
74
+ });
75
+ };
76
+ const handleGitCommitPush = (options) => {
77
+ return (options?.client ?? client).post({
78
+ ...options,
79
+ url: "/ephemeral/v1/dev-servers/git/commit-push"
80
+ });
81
+ };
82
+ const handleShutdownDevServer = (options) => {
83
+ return (options?.client ?? client).post({
84
+ ...options,
85
+ url: "/ephemeral/v1/dev-servers/shutdown"
86
+ });
87
+ };
88
+ const handleDevServerStatus = (options) => {
89
+ return (options?.client ?? client).get({
90
+ ...options,
91
+ url: "/ephemeral/v1/dev-servers/status"
92
+ });
93
+ };
58
94
  const handleListExecuteRuns = (options) => {
59
95
  return (options?.client ?? client).get({
60
96
  ...options,
@@ -190,7 +226,17 @@ const handleListWebDeploys = (options) => {
190
226
 
191
227
  class FreestyleSandboxes {
192
228
  client;
229
+ options;
193
230
  constructor(options) {
231
+ this.options = options ?? {};
232
+ if (!options?.apiKey) {
233
+ this.options.apiKey = process.env.FREESTYLE_API_KEY;
234
+ }
235
+ if (!this.options.apiKey) {
236
+ throw new Error(
237
+ "No API key provided. Please set the FREESTYLE_API_KEY environment variable or configure apiKey when constructing FreestyleSandboxes."
238
+ );
239
+ }
194
240
  if (typeof Deno !== "undefined") {
195
241
  class FreestyleRequest extends Request {
196
242
  constructor(input, init) {
@@ -204,10 +250,10 @@ class FreestyleSandboxes {
204
250
  Request = FreestyleRequest;
205
251
  }
206
252
  this.client = createClient({
207
- baseUrl: options.baseUrl ?? "https://api.freestyle.sh",
253
+ baseUrl: this.options?.baseUrl ?? "https://api.freestyle.sh",
208
254
  headers: {
209
- Authorization: `Bearer ${options.apiKey}`,
210
- ...options.headers
255
+ Authorization: `Bearer ${this.options.apiKey}`,
256
+ ...this.options?.headers
211
257
  }
212
258
  });
213
259
  }
@@ -795,7 +841,7 @@ ${response.error.message}`);
795
841
  );
796
842
  }
797
843
  if (response.data.isNew) {
798
- const rId = options.repoId || options.repoUrl.split("/").at(-1);
844
+ const rId = options.repoId || options.repoUrl?.split("/").at(-1);
799
845
  await this.createGitTrigger({
800
846
  repoId: rId,
801
847
  action: {
@@ -813,17 +859,208 @@ ${response.error.message}`);
813
859
  if (!response.data) {
814
860
  throw new Error(`Failed to request dev server: ${response.error}`);
815
861
  }
862
+ const data = response.data;
863
+ const devServerInstance = {
864
+ repoId: options.repoId || options.repo || "",
865
+ kind: "repo"
866
+ };
867
+ const client = this.client;
868
+ const that = this;
816
869
  return {
817
- ...response.data,
818
- // @ts-ignore
819
- mcpEphemeralUrl: response.data.mcpEphemeralUrl || response.data.url + "/mcp",
820
- ephemeralUrl: response.data.ephemeralUrl ?? response.data.url,
821
- codeServerUrl: (
822
- // @ts-ignore
823
- response.data.codeServerUrl ?? response.data.ephemeralUrl + "/__freestyle_code_server/?folder=/template"
824
- )
870
+ isNew: data.isNew,
871
+ ephemeralUrl: data.ephemeralUrl ?? data.url,
872
+ mcpEphemeralUrl: data.mcpEphemeralUrl ?? data.url + "/mcp",
873
+ codeServerUrl: data.codeServerUrl ?? (data.ephemeralUrl ?? data.url) + "/__freestyle_code_server/?folder=/template",
874
+ async status() {
875
+ const response2 = await handleDevServerStatus({
876
+ client,
877
+ body: {
878
+ devServer: devServerInstance
879
+ }
880
+ });
881
+ if (response2.error) {
882
+ throw new Error(`Failed to get status: ${response2.error}`);
883
+ }
884
+ return {
885
+ installing: response2.data.installing,
886
+ devRunning: response2.data.devRunning
887
+ };
888
+ },
889
+ async commitAndPush(message) {
890
+ const response2 = await handleGitCommitPush({
891
+ client,
892
+ body: {
893
+ devServer: devServerInstance,
894
+ message
895
+ }
896
+ });
897
+ if (response2.error) {
898
+ throw new Error(`Failed to commit and push: ${response2.error}`);
899
+ }
900
+ },
901
+ async shutdown() {
902
+ const response2 = await handleShutdownDevServer({
903
+ client,
904
+ body: {
905
+ devServer: devServerInstance
906
+ }
907
+ });
908
+ if (response2.error) {
909
+ throw new Error(`Failed to shutdown dev server: ${response2.error}`);
910
+ }
911
+ return {
912
+ success: response2.data.success,
913
+ message: response2.data.message
914
+ };
915
+ },
916
+ fs: {
917
+ async ls(path = "") {
918
+ const response2 = await handleReadFileFromEphemeralDevServer({
919
+ client,
920
+ path: {
921
+ filepath: path
922
+ },
923
+ body: {
924
+ devServer: devServerInstance,
925
+ encoding: "utf-8"
926
+ }
927
+ });
928
+ if (response2.error) {
929
+ throw new Error(`Failed to list directory: ${response2.error}`);
930
+ }
931
+ if (!response2.data?.content) {
932
+ return [];
933
+ }
934
+ if (response2.data.content.kind === "directory") {
935
+ return response2.data.content.files;
936
+ }
937
+ return [];
938
+ },
939
+ async *watch() {
940
+ const response2 = await that.fetch(
941
+ "/ephemeral/v1/dev-servers/watch-files",
942
+ {
943
+ method: "POST",
944
+ body: JSON.stringify({
945
+ devServer: {
946
+ repoId: devServerInstance.repoId,
947
+ kind: devServerInstance.kind
948
+ }
949
+ })
950
+ }
951
+ );
952
+ if (!response2.ok) {
953
+ throw new Error(
954
+ `Failed to fetch stream: ${response2.status} ${response2.statusText}`
955
+ );
956
+ }
957
+ if (!response2.body) {
958
+ throw new Error("Failed to fetch stream: No response body");
959
+ }
960
+ const reader = response2.body.getReader();
961
+ const decoder = new TextDecoder("utf-8");
962
+ let buffer = "";
963
+ while (true) {
964
+ const { done, value } = await reader.read();
965
+ if (done) break;
966
+ buffer += decoder.decode(value, { stream: true });
967
+ let newlineIndex;
968
+ while ((newlineIndex = buffer.indexOf("\n")) >= 0) {
969
+ const line = buffer.slice(0, newlineIndex).trim();
970
+ buffer = buffer.slice(newlineIndex + 1);
971
+ if (line) {
972
+ yield JSON.parse(line);
973
+ }
974
+ }
975
+ }
976
+ if (buffer.trim()) {
977
+ yield JSON.parse(buffer.trim());
978
+ }
979
+ },
980
+ async readFile(path, encoding = "utf-8") {
981
+ const response2 = await handleReadFileFromEphemeralDevServer({
982
+ client,
983
+ path: {
984
+ filepath: path
985
+ },
986
+ body: {
987
+ devServer: devServerInstance,
988
+ encoding
989
+ }
990
+ });
991
+ if (response2.error) {
992
+ throw new Error(`Failed to read file: ${response2.error}`);
993
+ }
994
+ if (!response2.data?.content || response2.data.content.kind !== "file") {
995
+ throw new Error(`Not a file or file not found: ${path}`);
996
+ }
997
+ return response2.data.content.content;
998
+ },
999
+ async writeFile(path, content, encoding = "utf-8") {
1000
+ const contentStr = typeof content === "string" ? content : new TextDecoder(encoding).decode(content);
1001
+ const response2 = await handleWriteFileFromEphemeralDevServer({
1002
+ client,
1003
+ path: {
1004
+ filepath: path
1005
+ },
1006
+ body: {
1007
+ devServer: devServerInstance,
1008
+ content: contentStr,
1009
+ encoding
1010
+ }
1011
+ });
1012
+ if (response2.error) {
1013
+ throw new Error(`Failed to write file: ${response2.error}`);
1014
+ }
1015
+ }
1016
+ },
1017
+ process: {
1018
+ async exec(cmd, background = false) {
1019
+ const response2 = await handleExecOnEphemeralDevServer(
1020
+ {
1021
+ client,
1022
+ body: {
1023
+ devServer: devServerInstance,
1024
+ command: cmd,
1025
+ background
1026
+ }
1027
+ }
1028
+ );
1029
+ if (response2.error) {
1030
+ throw new Error(`Failed to execute command: ${response2.error}`);
1031
+ }
1032
+ return {
1033
+ id: response2.data.id,
1034
+ isNew: response2.data.isNew,
1035
+ stdout: response2.data.stdout,
1036
+ stderr: response2.data.stderr
1037
+ };
1038
+ }
1039
+ }
825
1040
  };
826
1041
  }
1042
+ fetch(path, init) {
1043
+ const headers = new Headers(init?.headers);
1044
+ for (const [key, value] of Object.entries(this.options.headers ?? {})) {
1045
+ if (!headers.has(key)) {
1046
+ headers.append(key, value);
1047
+ }
1048
+ }
1049
+ if (!headers.has("Authorization")) {
1050
+ headers.append("Authorization", `Bearer ${this.options.apiKey}`);
1051
+ }
1052
+ if (!headers.has("Content-Type")) {
1053
+ headers.append("Content-Type", "application/json");
1054
+ }
1055
+ const url = new URL(
1056
+ path,
1057
+ this.options.baseUrl ?? "https://api.freestyle.sh"
1058
+ );
1059
+ return fetch(url, {
1060
+ ...init ?? {},
1061
+ headers
1062
+ });
1063
+ }
827
1064
  }
828
1065
 
829
1066
  export { FreestyleSandboxes };
@@ -1,4 +1,4 @@
1
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
1
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  type AgentAction = {
@@ -1,4 +1,4 @@
1
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
1
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  type AgentAction = {
@@ -1,4 +1,4 @@
1
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
1
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  type AgentAction = {
@@ -1,4 +1,4 @@
1
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
1
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  type AgentAction = {
@@ -5,7 +5,7 @@ import { Transform } from 'stream';
5
5
  import { EventEmitter } from 'events';
6
6
  import { ServerResponse, IncomingMessage } from 'http';
7
7
  import { WorkerOptions } from 'worker_threads';
8
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
8
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
9
9
  import 'node:http';
10
10
 
11
11
  // Type definitions for pino-std-serializers 2.4
@@ -5,7 +5,7 @@ import { Transform } from 'stream';
5
5
  import { EventEmitter } from 'events';
6
6
  import { ServerResponse, IncomingMessage } from 'http';
7
7
  import { WorkerOptions } from 'worker_threads';
8
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
8
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
9
9
  import 'node:http';
10
10
 
11
11
  // Type definitions for pino-std-serializers 2.4
@@ -5,7 +5,7 @@ import { Transform } from 'stream';
5
5
  import { EventEmitter } from 'events';
6
6
  import { ServerResponse, IncomingMessage } from 'http';
7
7
  import { WorkerOptions } from 'worker_threads';
8
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
8
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
9
9
  import 'node:http';
10
10
 
11
11
  // Type definitions for pino-std-serializers 2.4
@@ -5,7 +5,7 @@ import { Transform } from 'stream';
5
5
  import { EventEmitter } from 'events';
6
6
  import { ServerResponse, IncomingMessage } from 'http';
7
7
  import { WorkerOptions } from 'worker_threads';
8
- import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-BbekD8Sd.js';
8
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-wmZuN8DG.js';
9
9
  import 'node:http';
10
10
 
11
11
  // Type definitions for pino-std-serializers 2.4
@@ -1,12 +1,33 @@
1
+ import React from 'react';
2
+
3
+ type RequestDevServerActions = {
4
+ requestDevServer: (args: {
5
+ repoId: string;
6
+ }) => Promise<{
7
+ ephemeralUrl: string;
8
+ devCommandRunning: boolean;
9
+ installCommandRunning: boolean;
10
+ }>;
11
+ };
12
+
1
13
  declare function DefaultLoadingComponent({ installCommandRunning, }: {
2
14
  devCommandRunning: boolean;
3
15
  installCommandRunning: boolean;
4
16
  serverStarting: boolean;
5
17
  iframeLoading: boolean;
6
- }): any;
18
+ }): React.JSX.Element;
7
19
  interface FreestyleDevServerHandle {
8
20
  refresh: () => void;
9
21
  }
10
- declare const FreestyleDevServer: any;
22
+ declare const FreestyleDevServer: React.ForwardRefExoticComponent<{
23
+ repoId: string;
24
+ loadingComponent?: (props: {
25
+ devCommandRunning: boolean;
26
+ installCommandRunning: boolean;
27
+ serverStarting: boolean;
28
+ iframeLoading: boolean;
29
+ }) => React.ReactNode;
30
+ actions: RequestDevServerActions;
31
+ } & React.RefAttributes<FreestyleDevServerHandle>>;
11
32
 
12
33
  export { DefaultLoadingComponent, FreestyleDevServer, type FreestyleDevServerHandle };
@@ -1,12 +1,33 @@
1
+ import React from 'react';
2
+
3
+ type RequestDevServerActions = {
4
+ requestDevServer: (args: {
5
+ repoId: string;
6
+ }) => Promise<{
7
+ ephemeralUrl: string;
8
+ devCommandRunning: boolean;
9
+ installCommandRunning: boolean;
10
+ }>;
11
+ };
12
+
1
13
  declare function DefaultLoadingComponent({ installCommandRunning, }: {
2
14
  devCommandRunning: boolean;
3
15
  installCommandRunning: boolean;
4
16
  serverStarting: boolean;
5
17
  iframeLoading: boolean;
6
- }): any;
18
+ }): React.JSX.Element;
7
19
  interface FreestyleDevServerHandle {
8
20
  refresh: () => void;
9
21
  }
10
- declare const FreestyleDevServer: any;
22
+ declare const FreestyleDevServer: React.ForwardRefExoticComponent<{
23
+ repoId: string;
24
+ loadingComponent?: (props: {
25
+ devCommandRunning: boolean;
26
+ installCommandRunning: boolean;
27
+ serverStarting: boolean;
28
+ iframeLoading: boolean;
29
+ }) => React.ReactNode;
30
+ actions: RequestDevServerActions;
31
+ } & React.RefAttributes<FreestyleDevServerHandle>>;
11
32
 
12
33
  export { DefaultLoadingComponent, FreestyleDevServer, type FreestyleDevServerHandle };
@@ -1,12 +1,33 @@
1
+ import React from 'react';
2
+
3
+ type RequestDevServerActions = {
4
+ requestDevServer: (args: {
5
+ repoId: string;
6
+ }) => Promise<{
7
+ ephemeralUrl: string;
8
+ devCommandRunning: boolean;
9
+ installCommandRunning: boolean;
10
+ }>;
11
+ };
12
+
1
13
  declare function DefaultLoadingComponent({ installCommandRunning, }: {
2
14
  devCommandRunning: boolean;
3
15
  installCommandRunning: boolean;
4
16
  serverStarting: boolean;
5
17
  iframeLoading: boolean;
6
- }): any;
18
+ }): React.JSX.Element;
7
19
  interface FreestyleDevServerHandle {
8
20
  refresh: () => void;
9
21
  }
10
- declare const FreestyleDevServer: any;
22
+ declare const FreestyleDevServer: React.ForwardRefExoticComponent<{
23
+ repoId: string;
24
+ loadingComponent?: (props: {
25
+ devCommandRunning: boolean;
26
+ installCommandRunning: boolean;
27
+ serverStarting: boolean;
28
+ iframeLoading: boolean;
29
+ }) => React.ReactNode;
30
+ actions: RequestDevServerActions;
31
+ } & React.RefAttributes<FreestyleDevServerHandle>>;
11
32
 
12
33
  export { DefaultLoadingComponent, FreestyleDevServer, type FreestyleDevServerHandle };
@@ -1,12 +1,33 @@
1
+ import React from 'react';
2
+
3
+ type RequestDevServerActions = {
4
+ requestDevServer: (args: {
5
+ repoId: string;
6
+ }) => Promise<{
7
+ ephemeralUrl: string;
8
+ devCommandRunning: boolean;
9
+ installCommandRunning: boolean;
10
+ }>;
11
+ };
12
+
1
13
  declare function DefaultLoadingComponent({ installCommandRunning, }: {
2
14
  devCommandRunning: boolean;
3
15
  installCommandRunning: boolean;
4
16
  serverStarting: boolean;
5
17
  iframeLoading: boolean;
6
- }): any;
18
+ }): React.JSX.Element;
7
19
  interface FreestyleDevServerHandle {
8
20
  refresh: () => void;
9
21
  }
10
- declare const FreestyleDevServer: any;
22
+ declare const FreestyleDevServer: React.ForwardRefExoticComponent<{
23
+ repoId: string;
24
+ loadingComponent?: (props: {
25
+ devCommandRunning: boolean;
26
+ installCommandRunning: boolean;
27
+ serverStarting: boolean;
28
+ iframeLoading: boolean;
29
+ }) => React.ReactNode;
30
+ actions: RequestDevServerActions;
31
+ } & React.RefAttributes<FreestyleDevServerHandle>>;
11
32
 
12
33
  export { DefaultLoadingComponent, FreestyleDevServer, type FreestyleDevServerHandle };