freestyle-sandboxes 0.0.97 → 0.1.0

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.
Files changed (69) hide show
  1. package/README.md +39 -48
  2. package/index.cjs +4362 -0
  3. package/index.d.cts +9401 -0
  4. package/index.d.mts +9401 -0
  5. package/index.mjs +4348 -0
  6. package/package.json +16 -109
  7. package/dist/ai/inde.d.cts +0 -75
  8. package/dist/ai/inde.d.mts +0 -75
  9. package/dist/ai/index.cjs +0 -13
  10. package/dist/ai/index.d.cts +0 -75
  11. package/dist/ai/index.d.mts +0 -75
  12. package/dist/ai/index.mjs +0 -4
  13. package/dist/expo/inde.d.cts +0 -6
  14. package/dist/expo/inde.d.mts +0 -6
  15. package/dist/expo/index.cjs +0 -319
  16. package/dist/expo/index.d.cts +0 -6
  17. package/dist/expo/index.d.mts +0 -6
  18. package/dist/expo/index.mjs +0 -297
  19. package/dist/inde.d.cts +0 -373
  20. package/dist/inde.d.mts +0 -373
  21. package/dist/index-BKAG8L-o.mjs +0 -3061
  22. package/dist/index-DuOpIaWc.cjs +0 -3068
  23. package/dist/index.cjs +0 -1329
  24. package/dist/index.d-9H_wnIbz.d.ts +0 -4223
  25. package/dist/index.d.cts +0 -373
  26. package/dist/index.d.mts +0 -373
  27. package/dist/index.mjs +0 -1327
  28. package/dist/langgraph/inde.d.cts +0 -4180
  29. package/dist/langgraph/inde.d.mts +0 -4180
  30. package/dist/langgraph/index.cjs +0 -17155
  31. package/dist/langgraph/index.d.cts +0 -4180
  32. package/dist/langgraph/index.d.mts +0 -4180
  33. package/dist/langgraph/index.mjs +0 -17153
  34. package/dist/mastra/inde.d.cts +0 -2623
  35. package/dist/mastra/inde.d.mts +0 -2623
  36. package/dist/mastra/index.cjs +0 -55
  37. package/dist/mastra/index.d.cts +0 -2623
  38. package/dist/mastra/index.d.mts +0 -2623
  39. package/dist/mastra/index.mjs +0 -53
  40. package/dist/react/dev-server/index..d.cts +0 -33
  41. package/dist/react/dev-server/index..d.mts +0 -33
  42. package/dist/react/dev-server/index.cjs +0 -148
  43. package/dist/react/dev-server/index.d.cts +0 -33
  44. package/dist/react/dev-server/index.d.mts +0 -33
  45. package/dist/react/dev-server/index.mjs +0 -145
  46. package/dist/types.gen-CJa21P0C.d.ts +0 -1902
  47. package/dist/types.gen-DKjMRuu5.d.ts +0 -1898
  48. package/dist/utils/inde.d.cts +0 -10
  49. package/dist/utils/inde.d.mts +0 -10
  50. package/dist/utils/index.cjs +0 -100
  51. package/dist/utils/index.d.cts +0 -10
  52. package/dist/utils/index.d.mts +0 -10
  53. package/dist/utils/index.mjs +0 -75
  54. package/openapi/index.ts +0 -3
  55. package/openapi/sdk.gen.ts +0 -929
  56. package/openapi/types.gen.ts +0 -2234
  57. package/openapi-ts.config.ts +0 -7
  58. package/openapi.json +0 -1
  59. package/src/ai/index.ts +0 -164
  60. package/src/dev-server.ts +0 -95
  61. package/src/expo/_expo_internals.ts +0 -389
  62. package/src/expo/index.ts +0 -26
  63. package/src/index.ts +0 -1459
  64. package/src/langgraph/index.ts +0 -33
  65. package/src/mastra/index.ts +0 -38
  66. package/src/react/dev-server/index.tsx +0 -195
  67. package/src/react/dev-server/types.ts +0 -5
  68. package/src/utils/index.ts +0 -97
  69. package/tsconfig.json +0 -8
package/src/index.ts DELETED
@@ -1,1459 +0,0 @@
1
- import { type Client, createClient } from "@hey-api/client-fetch";
2
- import * as sandbox_openapi from "../openapi/index.ts";
3
-
4
- import type {
5
- AccessLevel,
6
- CreatedToken,
7
- CreateRepositoryResponseSuccess,
8
- DescribePermissionResponseSuccess,
9
- DevServer,
10
- FreestyleCloudstateDeployRequest,
11
- FreestyleCloudstateDeploySuccessResponse,
12
- FreestyleDeployWebConfiguration,
13
- FreestyleDeployWebSuccessResponseV2,
14
- FreestyleExecuteScriptParamsConfiguration,
15
- FreestyleExecuteScriptResultSuccess,
16
- GitIdentity,
17
- HandleBackupCloudstateResponse,
18
- HandleCreateDomainVerificationResponse,
19
- HandleDeleteDomainVerificationResponse,
20
- HandleDeleteIdentityResponse,
21
- HandleDeleteRepoResponse,
22
- HandleGetExecuteRunResponse,
23
- HandleGetLogsResponse,
24
- HandleListDomainsResponse,
25
- HandleListDomainVerificationRequestsResponse,
26
- HandleListExecuteRunsResponse,
27
- HandleListRepositoriesResponse,
28
- HandleListWebDeploysResponse,
29
- HandleVerifyDomainError,
30
- HandleVerifyDomainResponse,
31
- ListGitTokensResponseSuccess,
32
- ListPermissionResponseSuccess,
33
- } from "../openapi/index.ts";
34
-
35
- import {
36
- FreestyleDevServer,
37
- FreestyleDevServerFilesystem,
38
- } from "./dev-server.ts";
39
-
40
- export type { FreestyleDevServer, FreestyleDevServerFilesystem };
41
-
42
- export type {
43
- AccessLevel,
44
- CreatedToken,
45
- CreateRepositoryResponseSuccess,
46
- DescribePermissionResponseSuccess,
47
- FreestyleCloudstateDeployRequest,
48
- FreestyleCloudstateDeploySuccessResponse,
49
- FreestyleDeployWebConfiguration,
50
- FreestyleDeployWebSuccessResponseV2,
51
- FreestyleExecuteScriptParamsConfiguration,
52
- FreestyleExecuteScriptResultSuccess,
53
- GitIdentity,
54
- HandleBackupCloudstateResponse,
55
- HandleCreateDomainVerificationResponse,
56
- HandleDeleteDomainVerificationResponse,
57
- HandleDeleteIdentityResponse,
58
- HandleDeleteRepoResponse,
59
- HandleGetExecuteRunResponse,
60
- HandleGetLogsResponse,
61
- HandleListDomainsResponse,
62
- HandleListDomainVerificationRequestsResponse,
63
- HandleListExecuteRunsResponse,
64
- HandleListRepositoriesResponse,
65
- HandleListWebDeploysResponse,
66
- HandleVerifyDomainError,
67
- HandleVerifyDomainResponse,
68
- ListGitTokensResponseSuccess,
69
- ListPermissionResponseSuccess,
70
- DeploymentBuildOptions,
71
- DeploymentSource,
72
- HandleListDomainMappingsResponse,
73
- HandleListDomainMappingsData,
74
- HandleInsertDomainMappingResponse
75
- } from "../openapi/index.ts";
76
-
77
- /**
78
- * Create a new git repository with a single commit from a static import.
79
- */
80
- type CreateGitRepositoryImport = {
81
- /**
82
- * Create a new git repository with a single commit from a static import.
83
- */
84
- import: sandbox_openapi.CreateRepoImport;
85
- /**
86
- * Create a git repository from another git reopsitory
87
- */
88
- source?: never;
89
- };
90
-
91
- /**
92
- * Create a git repository from another git reopsitory
93
- */
94
- type CreateGitRepositorySource = {
95
- /**
96
- * Create a git repository from another git reopsitory
97
- */
98
- source: sandbox_openapi.CreateRepoSource;
99
- /**
100
- * Create a new git repository with a single commit from a static import.
101
- */
102
- import?: never;
103
- };
104
-
105
- type Options = {
106
- /**
107
- * The base URL for the API.
108
- */
109
- baseUrl?: string;
110
- /**
111
- * The API key to use for requests.
112
- */
113
- apiKey?: string;
114
- /**
115
- * Custom Headers to be sent with each request.
116
- */
117
- headers?: Record<string, string>;
118
- };
119
-
120
- export class FreestyleSandboxes {
121
- private client: Client;
122
- options: Options;
123
-
124
- constructor(options?: Options) {
125
- this.options = options ?? {};
126
-
127
- if (!options?.apiKey) {
128
- this.options.apiKey = process.env.FREESTYLE_API_KEY;
129
- }
130
-
131
- //@ts-expect-error Deno has a weird behavior thats patched here
132
- if (typeof Deno !== "undefined") {
133
- class FreestyleRequest extends Request {
134
- constructor(input, init) {
135
- if (init.client !== undefined) {
136
- console.warn("Unsupported client detected, using default client");
137
- delete init.client;
138
- }
139
- super(input, init);
140
- }
141
- }
142
-
143
- Request = FreestyleRequest;
144
- }
145
- this.client = createClient({
146
- baseUrl: this.options?.baseUrl ?? "https://api.freestyle.sh",
147
- headers: {
148
- Authorization: `Bearer ${this.options.apiKey}`,
149
- ...this.options?.headers,
150
- },
151
- });
152
- }
153
-
154
- /**
155
- * Execute a script in a sandbox.
156
- */
157
- async executeScript(
158
- script: string,
159
- config?: FreestyleExecuteScriptParamsConfiguration
160
- ): Promise<FreestyleExecuteScriptResultSuccess> {
161
- const response = await sandbox_openapi.handleExecuteScript({
162
- client: this.client,
163
- body: {
164
- script,
165
- config: config,
166
- },
167
- });
168
-
169
- if (response.data) {
170
- return response.data;
171
- }
172
- throw {
173
- message: `Failed to execute script: \n\n${script}\n\nError:\n\n${JSON.stringify(
174
- response
175
- )}`,
176
- error: response.error,
177
- };
178
- // `Failed to execute script: \n\n${script}\n\nError:\n\n${JSON.stringify(
179
- // response
180
- // )}`
181
- }
182
-
183
- /**
184
- * Deploy a Web project to a sandbox.
185
- */
186
- async deployWeb(
187
- source: sandbox_openapi.DeploymentSource,
188
- config?: FreestyleDeployWebConfiguration
189
- ): Promise<FreestyleDeployWebSuccessResponseV2> {
190
- const response = await sandbox_openapi.handleDeployWebV2({
191
- client: this.client,
192
- body: {
193
- source: source,
194
- config: config as FreestyleDeployWebConfiguration,
195
- },
196
- });
197
- if (response.data) {
198
- return response.data;
199
- }
200
- throw new Error(
201
- `Failed to deploy web project\n\nStatus: ${response.response.status}\n\nMessage: ${response.error}`
202
- );
203
- }
204
-
205
- /**
206
- * Deploy a Cloudstate project to a sandbox.
207
- */
208
- async deployCloudstate(
209
- body: FreestyleCloudstateDeployRequest
210
- ): Promise<FreestyleCloudstateDeploySuccessResponse> {
211
- const response = await sandbox_openapi.handleDeployCloudstate({
212
- client: this.client,
213
- body: body,
214
- });
215
-
216
- if (response.data) {
217
- return response.data;
218
- }
219
-
220
- throw new Error("Failed to deploy Cloudstate project");
221
- }
222
-
223
- /**
224
- * Get a backup of a Cloudstate project in a sandbox.
225
- * @param id The ID of the Cloudstate project.
226
- * @returns The backup of the Cloudstate project.
227
- * @throws An error if the backup could not be retrieved.
228
- */
229
- async backupCloudstate(id: string): Promise<HandleBackupCloudstateResponse> {
230
- const response = await sandbox_openapi.handleBackupCloudstate({
231
- client: this.client,
232
- path: {
233
- id: id,
234
- },
235
- });
236
-
237
- if (response.data) {
238
- return response.data;
239
- }
240
-
241
- throw new Error("Failed to get backup of Cloudstate project");
242
- }
243
-
244
- /**
245
- * Get logs for an execute run, or web deployment.
246
- * @param id The ID of the sandbox.
247
- * @returns The logs for the sandbox.
248
- * @throws An error if the logs could not be retrieved.
249
- */
250
- async getLogs(id: string): Promise<HandleGetLogsResponse> {
251
- const response = await sandbox_openapi.handleGetLogs({
252
- client: this.client,
253
- query: {
254
- deploymentId: id,
255
- },
256
- // path: {
257
- // id: id,
258
- // },
259
- });
260
- if (response.data) {
261
- return response.data;
262
- }
263
- throw new Error("Failed to get logs for sandbox");
264
- }
265
-
266
- /**
267
- * Create a a domain verification request.
268
- * @param domain The domain to verify.
269
- * @returns The domain verification token.
270
- */
271
- async createDomainVerificationRequest(
272
- domain: string
273
- ): Promise<HandleCreateDomainVerificationResponse> {
274
- const response = await sandbox_openapi.handleCreateDomainVerification({
275
- client: this.client,
276
- body: {
277
- domain: domain,
278
- },
279
- });
280
- if (response.data) {
281
- return response.data;
282
- }
283
-
284
- throw new Error(response.error.message);
285
- }
286
-
287
- async verifyDomainVerificationRequest(
288
- id: string
289
- ): Promise<HandleVerifyDomainResponse> {
290
- const response = await sandbox_openapi.handleVerifyDomain({
291
- client: this.client,
292
- body: {
293
- id,
294
- },
295
- });
296
-
297
- if (response.data) {
298
- return response.data;
299
- }
300
- throw new Error(
301
- `Failed to verify domain verification request with ID ${id}: ${response.error.message}`
302
- );
303
- }
304
-
305
- /**
306
- * Verify a domain. Note, this requires the domain verification token to be already set up.
307
- * @param domain The domain to verify.
308
- * @returns The domain verification request.
309
- */
310
- async verifyDomain(
311
- domain: string
312
- ): Promise<HandleVerifyDomainResponse | HandleVerifyDomainError> {
313
- const response = await sandbox_openapi.handleVerifyDomain({
314
- client: this.client,
315
- body: {
316
- domain: domain,
317
- },
318
- });
319
- if (response.data) {
320
- return response.data;
321
- }
322
- throw new Error(
323
- `Failed to verify domain ${domain}: ${response.error.message}`
324
- );
325
- }
326
-
327
- async listDomains(): Promise<HandleListDomainsResponse> {
328
- const response = await sandbox_openapi.handleListDomains({
329
- client: this.client,
330
- });
331
- if (response.data) {
332
- return response.data;
333
- }
334
-
335
- throw new Error(`Failed to list domains\n${response.error.message}`);
336
- }
337
-
338
- async listDomainVerificationRequests(): Promise<HandleListDomainVerificationRequestsResponse> {
339
- const response = await sandbox_openapi.handleListDomainVerificationRequests(
340
- {
341
- client: this.client,
342
- }
343
- );
344
- if (response.data) {
345
- return response.data;
346
- }
347
-
348
- throw new Error(
349
- `Failed to list domain verification requests\n${response.error.message}`
350
- );
351
- }
352
-
353
- async deleteDomainVerificationRequest(
354
- domain: string,
355
- verificationCode: string
356
- ): Promise<HandleDeleteDomainVerificationResponse> {
357
- const response = await sandbox_openapi.handleDeleteDomainVerification({
358
- client: this.client,
359
- body: {
360
- domain: domain,
361
- verificationCode: verificationCode,
362
- },
363
- });
364
- if (response.data) {
365
- return response.data;
366
- }
367
-
368
- throw new Error(
369
- `Failed to delete domain verification request for domain ${domain}: ${response.error.message}`
370
- );
371
- }
372
-
373
- async listWebDeployments(
374
- limit?: number,
375
- offset?: number
376
- ): Promise<HandleListWebDeploysResponse> {
377
- const response = await sandbox_openapi.handleListWebDeploys({
378
- client: this.client,
379
- query: {
380
- limit: limit ?? 10,
381
- offset: offset ?? 0,
382
- },
383
- });
384
-
385
- if (response.data) {
386
- return response.data;
387
- }
388
-
389
- throw new Error(
390
- `Failed to list web deployments\n${response.error.message}`
391
- );
392
- }
393
-
394
- async listExecuteRuns(
395
- limit?: number,
396
- offset?: number
397
- ): Promise<HandleListExecuteRunsResponse> {
398
- const response = await sandbox_openapi.handleListExecuteRuns({
399
- client: this.client,
400
- query: {
401
- limit: limit ?? 10,
402
- offset: offset ?? 0,
403
- },
404
- });
405
- if (response.data) {
406
- return response.data;
407
- }
408
- throw new Error(`Failed to list execute runs\n${response.error.message}`);
409
- }
410
-
411
- async getExecuteRun(id: string): Promise<HandleGetExecuteRunResponse> {
412
- const response = await sandbox_openapi.handleGetExecuteRun({
413
- client: this.client,
414
- path: {
415
- deployment: id,
416
- },
417
- });
418
-
419
- if (response.data) {
420
- return response.data;
421
- }
422
-
423
- throw new Error(
424
- `Failed to get execute run with ID ${id}: ${response.error.message}`
425
- );
426
- }
427
-
428
- /** Provision a wildcard certificate for domain. */
429
- async provisionWildcard(domain: string) {
430
- const response = await sandbox_openapi.handleVerifyWildcard({
431
- client: this.client,
432
- path: {
433
- domain,
434
- },
435
- });
436
-
437
- if (response.data) {
438
- return response.data;
439
- }
440
-
441
- throw new Error(
442
- `Failed to provision wildcard for domain ${domain}: ${response.error.message}`
443
- );
444
- }
445
-
446
- /**
447
- * Insert a domain mapping for a deployment.
448
- */
449
- async insertDomainMapping({
450
- domain,
451
- deploymentId,
452
- }: {
453
- domain: string;
454
- deploymentId: string;
455
- }): Promise<sandbox_openapi.HandleInsertDomainMappingResponse> {
456
- const response = await sandbox_openapi.handleInsertDomainMapping({
457
- client: this.client,
458
- path: {
459
- domain,
460
- },
461
- body: {
462
- deploymentId,
463
- },
464
- });
465
- if (response.data) {
466
- return response.data;
467
- }
468
- throw new Error(
469
- `Failed to insert domain mapping for domain ${domain} and deployment ${deploymentId}: ${response.error.message}`
470
- );
471
- }
472
-
473
- /**
474
- * Remove a domain mapping for a deployment.
475
- */
476
- async removeDomainMapping({
477
- domain,
478
- }: {
479
- domain: string;
480
- }): Promise<void> {
481
- const response = await sandbox_openapi.handleDeleteDomainMapping({
482
- client: this.client,
483
- path: {
484
- domain,
485
- },
486
- });
487
- if (response.response.ok) {
488
- return;
489
- }
490
- throw new Error(
491
- `Failed to remove domain mapping for domain ${domain}: ${response.error}`
492
- );
493
- }
494
-
495
- async listDomainMappings({
496
- domain,
497
- domainOwnership,
498
- limit = 10,
499
- offset = 0,
500
- }: {
501
- domain?: string;
502
- domainOwnership?: string;
503
- limit?: number;
504
- offset?: number;
505
- }): Promise<sandbox_openapi.HandleListDomainMappingsResponse> {
506
- const response = await sandbox_openapi.handleListDomainMappings({
507
- client: this.client,
508
- query: {
509
- limit,
510
- offset,
511
- domain,
512
- domainOwnership,
513
- },
514
- });
515
- if (response.data) {
516
- return response.data;
517
- }
518
- throw new Error(
519
- `Failed to list domain mappings: ${JSON.stringify(response.error)}`
520
- );
521
- }
522
-
523
- /**
524
- * Create a new git repository.
525
- */
526
- async createGitRepository({
527
- name,
528
- public: pub = false,
529
- source,
530
- import: _import,
531
- defaultBranch,
532
- devServers,
533
- }: {
534
- name?: string;
535
- public?: boolean;
536
- defaultBranch?: string;
537
- devServers?: sandbox_openapi.DevServerConfiguration;
538
- } & (
539
- | CreateGitRepositorySource
540
- | CreateGitRepositoryImport
541
- | { source?: never; import?: never }
542
- )): Promise<CreateRepositoryResponseSuccess> {
543
- const response = await sandbox_openapi.handleCreateRepo({
544
- client: this.client,
545
- body: {
546
- name,
547
- public: pub,
548
- source,
549
- import: _import,
550
- defaultBranch,
551
- devServers
552
- },
553
- });
554
-
555
- if (response.data) {
556
- return response.data;
557
- }
558
-
559
- throw new Error(
560
- `Failed to create git repository ${name}: ${response.error}`
561
- );
562
- }
563
-
564
- /**
565
- * List git repositories.
566
- */
567
- async listGitRepositories({
568
- limit = 10,
569
- offset = 0,
570
- }: {
571
- limit?: number;
572
- offset?: number;
573
- } = {}): Promise<HandleListRepositoriesResponse> {
574
- const response = await sandbox_openapi.handleListRepositories({
575
- client: this.client,
576
- query: {
577
- limit,
578
- offset,
579
- },
580
- });
581
-
582
- if (response.data) {
583
- return response.data;
584
- }
585
-
586
- throw new Error(`Failed to list git repositories: ${response.error}`);
587
- }
588
-
589
- /**
590
- * Delete a git repository.
591
- */
592
- async deleteGitRepository({
593
- repoId,
594
- }: {
595
- repoId: string;
596
- }): Promise<HandleDeleteRepoResponse> {
597
- const response = await sandbox_openapi.handleDeleteRepo({
598
- client: this.client,
599
- path: {
600
- repo: repoId,
601
- },
602
- });
603
-
604
- if (response.data) {
605
- return response.data;
606
- }
607
-
608
- throw new Error(
609
- `Failed to delete git repository ${repoId}: ${response.error}`
610
- );
611
- }
612
-
613
- /**
614
- * Create a new git identity.
615
- */
616
- async createGitIdentity(): Promise<GitIdentity> {
617
- const response = await sandbox_openapi.handleCreateIdentity({
618
- client: this.client,
619
- });
620
-
621
- if (response.data) {
622
- return response.data;
623
- }
624
-
625
- throw new Error(`Failed to create git identity: ${response.error}`);
626
- }
627
-
628
- /**
629
- * Delete a git identity.
630
- */
631
- async deleteGitIdentity({
632
- identityId,
633
- }: {
634
- identityId: string;
635
- }): Promise<HandleDeleteIdentityResponse> {
636
- const response = await sandbox_openapi.handleDeleteIdentity({
637
- client: this.client,
638
- path: {
639
- identity: identityId,
640
- },
641
- });
642
-
643
- if (response.data) {
644
- return response.data;
645
- }
646
-
647
- throw new Error(`Failed to delete git identity: ${response.error}`);
648
- }
649
-
650
- /**
651
- * Grant a git identity access to a repository.
652
- */
653
- async grantGitPermission({
654
- repoId,
655
- identityId,
656
- permission,
657
- }: {
658
- repoId: string;
659
- identityId: string;
660
- permission: AccessLevel;
661
- }) {
662
- const response = await sandbox_openapi.handleGrantPermission({
663
- client: this.client,
664
- path: {
665
- repo: repoId,
666
- identity: identityId,
667
- },
668
- body: {
669
- permission,
670
- },
671
- });
672
-
673
- if (response.data) {
674
- return response.data;
675
- }
676
-
677
- throw new Error(
678
- `Failed to grant access to git identity ${identityId} for repository ${repoId}: ${response.error}`
679
- );
680
- }
681
-
682
- /**
683
- * Update a git identity's permissions on a repository.
684
- */
685
- async updateGitPermission({
686
- repoId,
687
- identityId,
688
- permission,
689
- }: {
690
- repoId: string;
691
- identityId: string;
692
- permission: AccessLevel;
693
- }): Promise<void> {
694
- const response = await sandbox_openapi.handleUpdatePermission({
695
- client: this.client,
696
- path: {
697
- repo: repoId,
698
- identity: identityId,
699
- },
700
- body: {
701
- permission,
702
- },
703
- });
704
-
705
- if (response.data) {
706
- return;
707
- }
708
-
709
- throw new Error(
710
- `Failed to update permission for git identity ${identityId} for repository ${repoId}: ${response.error}`
711
- );
712
- }
713
-
714
- /**
715
- * Revoke a git identity's access to a repository.
716
- */
717
- async revokeGitPermission({
718
- repoId,
719
- identityId,
720
- }: {
721
- repoId: string;
722
- identityId: string;
723
- }): Promise<void> {
724
- const response = await sandbox_openapi.handleRevokePermission({
725
- client: this.client,
726
- path: {
727
- repo: repoId,
728
- identity: identityId,
729
- },
730
- });
731
-
732
- if (response.data) {
733
- return;
734
- }
735
-
736
- throw new Error(
737
- `Failed to revoke access to git identity ${identityId} for repository ${repoId}: ${response.error}`
738
- );
739
- }
740
-
741
- /**
742
- * List access permissions for a git identity.
743
- */
744
- async listGitPermissions({
745
- identityId,
746
- }: {
747
- identityId: string;
748
- }): Promise<ListPermissionResponseSuccess> {
749
- const response = await sandbox_openapi.handleListPermissions({
750
- client: this.client,
751
- path: {
752
- identity: identityId,
753
- },
754
- });
755
-
756
- if (response.data) {
757
- return response.data;
758
- }
759
-
760
- throw new Error(
761
- `Failed to list permissions for git identity ${identityId}: ${response.error}`
762
- );
763
- }
764
-
765
- /**
766
- * Get the permission of a git identity on a repository.
767
- */
768
- async getGitPermission({
769
- repoId,
770
- identityId,
771
- }: {
772
- repoId: string;
773
- identityId: string;
774
- }): Promise<DescribePermissionResponseSuccess> {
775
- const response = await sandbox_openapi.handleDescribePermission({
776
- client: this.client,
777
- path: {
778
- repo: repoId,
779
- identity: identityId,
780
- },
781
- });
782
-
783
- if (response.data) {
784
- return response.data;
785
- }
786
-
787
- throw new Error(
788
- `Failed to get permission for git identity ${identityId} on repository ${repoId}: ${response.error}`
789
- );
790
- }
791
-
792
- /**
793
- * Create a new git access token for an identity.
794
- */
795
- async createGitAccessToken({
796
- identityId,
797
- }: {
798
- identityId: string;
799
- }): Promise<CreatedToken> {
800
- const response = await sandbox_openapi.handleCreateGitToken({
801
- client: this.client,
802
- path: {
803
- identity: identityId,
804
- },
805
- });
806
-
807
- if (response.data) {
808
- return response.data;
809
- }
810
-
811
- throw new Error(
812
- `Failed to create git access token: ${response.error.message}`
813
- );
814
- }
815
-
816
- /**
817
- * Revoke a git access token.
818
- */
819
- async revokeGitAccessToken({
820
- identityId,
821
- tokenId,
822
- }: {
823
- identityId: string;
824
- tokenId: string;
825
- }): Promise<void> {
826
- const response = await sandbox_openapi.handleRevokeGitToken({
827
- client: this.client,
828
- body: {
829
- tokenId,
830
- },
831
- path: {
832
- identity: identityId,
833
- },
834
- });
835
-
836
- if (response.data) {
837
- return;
838
- }
839
-
840
- throw new Error(
841
- `Failed to revoke git access token ${tokenId}: ${response.error.message}`
842
- );
843
- }
844
-
845
- /**
846
- * List git access tokens for an identity.
847
- */
848
- async listGitAccessTokens({
849
- identityId,
850
- }: {
851
- identityId: string;
852
- }): Promise<ListGitTokensResponseSuccess> {
853
- const response = await sandbox_openapi.handleListGitTokens({
854
- client: this.client,
855
- path: {
856
- identity: identityId,
857
- },
858
- });
859
-
860
- if (response.data) {
861
- return response.data;
862
- }
863
-
864
- throw new Error(
865
- `Failed to list git access tokens: ${response.error.message}`
866
- );
867
- }
868
-
869
- /**
870
- * List git triggers for a repository.
871
- */
872
- async listGitTriggers({
873
- repoId,
874
- }: {
875
- repoId: string;
876
- }): Promise<sandbox_openapi.HandleListGitTriggersResponse> {
877
- const response = await sandbox_openapi.handleListGitTriggers({
878
- client: this.client,
879
- path: {
880
- repo: repoId,
881
- },
882
- });
883
-
884
- if (response.data) {
885
- return response.data;
886
- }
887
-
888
- throw new Error(
889
- `Failed to list git triggers for repository ${repoId}: ${response.error.message}`
890
- );
891
- }
892
-
893
- /**
894
- * Create a git trigger for a repository.
895
- */
896
- async createGitTrigger({
897
- repoId,
898
- trigger,
899
- action,
900
- }: {
901
- repoId: string;
902
- trigger: sandbox_openapi.GitTrigger;
903
- action: sandbox_openapi.GitTriggerAction;
904
- }): Promise<sandbox_openapi.HandleCreateGitTriggerResponse> {
905
- const response = await sandbox_openapi.handleCreateGitTrigger({
906
- client: this.client,
907
- path: {
908
- repo: repoId,
909
- },
910
- body: {
911
- trigger,
912
- action,
913
- },
914
- });
915
-
916
- if (response.data) {
917
- return response.data;
918
- }
919
-
920
- throw new Error(
921
- `Failed to create git trigger for repository ${repoId}: ${response.error.message}`
922
- );
923
- }
924
-
925
- /**
926
- * Delete a git trigger.
927
- */
928
- async deleteGitTrigger({
929
- triggerId,
930
- repo,
931
- }: {
932
- triggerId: string;
933
- repo: string;
934
- }): Promise<void> {
935
- const response = await sandbox_openapi.handleDeleteGitTrigger({
936
- client: this.client,
937
- path: {
938
- trigger: triggerId,
939
- repo: repo,
940
- },
941
- });
942
-
943
- if (response.data) {
944
- return;
945
- }
946
-
947
- throw new Error(
948
- `Failed to delete git trigger ${triggerId}: ${response.error.message}`
949
- );
950
- }
951
-
952
- /**
953
- * Set the default branch for a git repository.
954
- */
955
- async setGitRepoDefaultBranch({
956
- repoId,
957
- defaultBranch,
958
- }: sandbox_openapi.SetDefaultBranchRequest & {
959
- repoId: string;
960
- }): Promise<void> {
961
- const response = await sandbox_openapi.handleSetDefaultBranch({
962
- client: this.client,
963
- path: {
964
- repo_id: repoId,
965
- },
966
- body: {
967
- defaultBranch,
968
- },
969
- });
970
-
971
- if (response.error) {
972
- throw new Error(`Failed to set default branch: ${response.error}`);
973
- }
974
- }
975
-
976
- /**
977
- * Get the default branch for a git repository.
978
- */
979
- async getGitRepoDefaultBranch({
980
- repoId,
981
- }: {
982
- repoId: string;
983
- }): Promise<string> {
984
- const response = await sandbox_openapi.handleGetDefaultBranch({
985
- client: this.client,
986
- path: { repo_id: repoId },
987
- });
988
-
989
- if (response.data) {
990
- return response.data.defaultBranch;
991
- }
992
-
993
- throw new Error(
994
- `Failed to get default branch for repository ${repoId}: ${response.error}`
995
- );
996
- }
997
-
998
- /**
999
- * Get the contents of a git repository at the given path.
1000
- */
1001
- async getGitRepoContents({
1002
- repoId,
1003
- path,
1004
- ref,
1005
- }: {
1006
- repoId: string;
1007
- path?: string;
1008
- ref?: string;
1009
- }): Promise<sandbox_openapi.HandleGetContentsResponse> {
1010
- const response = await sandbox_openapi.handleGetContents({
1011
- client: this.client,
1012
- path: {
1013
- repo: repoId,
1014
- path: path ?? "",
1015
- "*path": path ?? null,
1016
- },
1017
- query: {
1018
- ref: ref,
1019
- },
1020
- });
1021
-
1022
- if (response.data) {
1023
- return response.data;
1024
- }
1025
-
1026
- throw new Error(
1027
- `Failed to get git repository contents: ${response.error.message}`
1028
- );
1029
- }
1030
-
1031
- /**
1032
- * Configure a git repository to sync with GitHub.
1033
- */
1034
- async configureGitRepoGitHubSync({
1035
- repoId,
1036
- githubRepoName,
1037
- }: {
1038
- repoId: string;
1039
- githubRepoName: string;
1040
- }): Promise<void> {
1041
- const response = await sandbox_openapi.configureGithubSync({
1042
- client: this.client,
1043
- path: {
1044
- repo_id: repoId,
1045
- },
1046
- body: {
1047
- githubRepoName,
1048
- },
1049
- });
1050
-
1051
- if (response.error) {
1052
- throw new Error(`Failed to configure GitHub sync: ${response.error}`);
1053
- }
1054
- }
1055
-
1056
- /**
1057
- * Remove the GitHub sync configuration for a git repository.
1058
- */
1059
- async removeGitRepoGitHubSync({ repoId }: { repoId: string }): Promise<void> {
1060
- const response = await sandbox_openapi.removeGithubSync({
1061
- client: this.client,
1062
- path: {
1063
- repo_id: repoId,
1064
- },
1065
- });
1066
-
1067
- if (response.error) {
1068
- throw new Error(`Failed to remove GitHub sync: ${response.error}`);
1069
- }
1070
- }
1071
-
1072
- /**
1073
- * Get the GitHub sync configuration for a git repository.
1074
- */
1075
- async getGitRepoGitHubSyncConfig({
1076
- repoId,
1077
- }): Promise<sandbox_openapi.GetGithubSyncResponse | null> {
1078
- const response = await sandbox_openapi.getGithubSync({
1079
- client: this.client,
1080
- path: {
1081
- repo_id: repoId,
1082
- },
1083
- });
1084
-
1085
- if (response.response.status === 404) {
1086
- return null;
1087
- }
1088
-
1089
- if (response.error) {
1090
- throw new Error(`Failed to get GitHub sync config: ${response.error}`);
1091
- }
1092
-
1093
- return response.data ?? null;
1094
- }
1095
-
1096
- /**
1097
- * Request a dev server for a repository. If a dev server is already running
1098
- * for that repository, it will return a url to that server. Dev servers are
1099
- * ephemeral so you should call this function every time you need a url. Do
1100
- * not store the url in your database!
1101
- */
1102
- async requestDevServer(options: {
1103
- /**
1104
- * @deprecated
1105
- */
1106
- repoUrl?: string;
1107
- repoId?: string;
1108
- /**
1109
- * @deprecated
1110
- */
1111
- repo?: string;
1112
- baseId?: string;
1113
- devCommand?: string;
1114
- preDevCommandOnce?: string;
1115
- envVars?: Record<string, string>;
1116
- computeClass?: string;
1117
- timeout?: number;
1118
- gitRef?: string;
1119
- ports?: {
1120
- port: number;
1121
- targetPort: number;
1122
- }[];
1123
- }): Promise<FreestyleDevServer> {
1124
- function formatHook(serverUrl: string, repoUrl: string) {
1125
- const hook =
1126
- serverUrl +
1127
- "/__freestyle_dev_server/update/git?repo=" +
1128
- encodeURIComponent(repoUrl);
1129
- return hook;
1130
- }
1131
-
1132
- const response = await sandbox_openapi.handleEphemeralDevServer({
1133
- client: this.client,
1134
- body: {
1135
- ...options,
1136
- // @ts-ignore
1137
- repo: options.repoUrl,
1138
- },
1139
- });
1140
-
1141
- if (response.error) {
1142
- throw new Error(
1143
- // @ts-ignore
1144
- `Failed to request dev server: ${response.error.message}`
1145
- );
1146
- }
1147
-
1148
- if (response.data.isNew) {
1149
- const rId = options.repoId || options.repoUrl?.split("/").at(-1)!;
1150
-
1151
- await this.createGitTrigger({
1152
- repoId: rId,
1153
- action: {
1154
- endpoint: formatHook(
1155
- response.data?.url!,
1156
- options.repoUrl || `https://git.freestyle.sh/${rId}`
1157
- ),
1158
- action: "webhook",
1159
- },
1160
- trigger: {
1161
- event: "push",
1162
- },
1163
- });
1164
- }
1165
-
1166
- if (!response.data) {
1167
- throw new Error(`Failed to request dev server: ${response.error}`);
1168
- }
1169
-
1170
- const data: typeof response.data & { codeServerUrl?: string } =
1171
- response.data;
1172
-
1173
- const devServerInstance: DevServer = {
1174
- repoId: options.repoId || options.repo || "",
1175
- kind: "repo",
1176
- gitRef: options.gitRef,
1177
- };
1178
-
1179
- const client = this.client;
1180
-
1181
- const that = this;
1182
-
1183
- return {
1184
- ...response.data,
1185
- isNew: data.isNew,
1186
-
1187
- ephemeralUrl: data.ephemeralUrl ?? data.url,
1188
-
1189
- mcpEphemeralUrl: data.mcpEphemeralUrl ?? data.url + "/mcp",
1190
-
1191
- codeServerUrl:
1192
- data.codeServerUrl ??
1193
- (data.ephemeralUrl ?? data.url) +
1194
- "/__freestyle_code_server/?folder=/template",
1195
-
1196
- async status() {
1197
- const response = await sandbox_openapi.handleDevServerStatus({
1198
- client,
1199
- body: {
1200
- devServer: devServerInstance,
1201
- },
1202
- });
1203
-
1204
- if (response.error) {
1205
- throw new Error(`Failed to get status: ${response.error}`);
1206
- }
1207
-
1208
- return {
1209
- installing: response.data.installing,
1210
- devRunning: response.data.devRunning,
1211
- };
1212
- },
1213
-
1214
- async commitAndPush(message: string) {
1215
- const response = await sandbox_openapi.handleGitCommitPush({
1216
- client,
1217
- body: {
1218
- devServer: devServerInstance,
1219
- message,
1220
- },
1221
- });
1222
-
1223
- if (response.error) {
1224
- throw new Error(`Failed to commit and push: ${response.error}`);
1225
- }
1226
- },
1227
-
1228
- async shutdown() {
1229
- const response = await sandbox_openapi.handleShutdownDevServer({
1230
- client,
1231
- body: {
1232
- devServer: devServerInstance,
1233
- },
1234
- });
1235
-
1236
- if (response.error) {
1237
- throw new Error(`Failed to shutdown dev server: ${response.error}`);
1238
- }
1239
-
1240
- return {
1241
- success: response.data.success,
1242
- message: response.data.message,
1243
- };
1244
- },
1245
-
1246
- fs: {
1247
- async ls(path = "") {
1248
- const response =
1249
- await sandbox_openapi.handleReadFileFromEphemeralDevServer({
1250
- client,
1251
- path: {
1252
- filepath: path,
1253
- "*filepath": path,
1254
- },
1255
- body: {
1256
- devServer: devServerInstance,
1257
- encoding: "utf-8",
1258
- },
1259
- });
1260
-
1261
- if (response.error) {
1262
- throw new Error(`Failed to list directory: ${response.error}`);
1263
- }
1264
-
1265
- if (!response.data?.content) {
1266
- return [];
1267
- }
1268
-
1269
- if (response.data.content.kind === "directory") {
1270
- return response.data.content.files;
1271
- }
1272
-
1273
- return [];
1274
- },
1275
-
1276
- async *watch(): AsyncGenerator<{
1277
- eventType: string;
1278
- filename: string;
1279
- }> {
1280
- const response = await that.fetch(
1281
- "/ephemeral/v1/dev-servers/watch-files",
1282
- {
1283
- method: "POST",
1284
- body: JSON.stringify({
1285
- devServer: {
1286
- repoId: devServerInstance.repoId,
1287
- kind: devServerInstance.kind,
1288
- },
1289
- }),
1290
- }
1291
- );
1292
-
1293
- if (!response.ok) {
1294
- throw new Error(
1295
- `Failed to fetch stream: ${response.status} ${response.statusText}`
1296
- );
1297
- }
1298
-
1299
- if (!response.body) {
1300
- throw new Error("Failed to fetch stream: No response body");
1301
- }
1302
-
1303
- const reader = response.body.getReader();
1304
- const decoder = new TextDecoder("utf-8");
1305
- let buffer = "";
1306
-
1307
- while (true) {
1308
- const { done, value } = await reader.read();
1309
- if (done) break;
1310
-
1311
- buffer += decoder.decode(value, { stream: true });
1312
-
1313
- let newlineIndex;
1314
- while ((newlineIndex = buffer.indexOf("\n")) >= 0) {
1315
- const line = buffer.slice(0, newlineIndex).trim();
1316
- buffer = buffer.slice(newlineIndex + 1);
1317
-
1318
- if (line) {
1319
- yield JSON.parse(line) as {
1320
- eventType: string;
1321
- filename: string;
1322
- };
1323
- }
1324
- }
1325
- }
1326
-
1327
- if (buffer.trim()) {
1328
- yield JSON.parse(buffer.trim()) as {
1329
- eventType: string;
1330
- filename: string;
1331
- };
1332
- }
1333
- },
1334
-
1335
- async readFile(path: string, encoding = "utf-8") {
1336
- const response =
1337
- await sandbox_openapi.handleReadFileFromEphemeralDevServer({
1338
- client,
1339
- path: {
1340
- filepath: path,
1341
- "*filepath": path,
1342
- },
1343
- body: {
1344
- devServer: devServerInstance,
1345
- encoding,
1346
- },
1347
- });
1348
-
1349
- if (response.error) {
1350
- throw new Error(`Failed to read file: ${response.error}`);
1351
- }
1352
-
1353
- if (
1354
- !response.data?.content ||
1355
- response.data.content.kind !== "file"
1356
- ) {
1357
- throw new Error(`Not a file or file not found: ${path}`);
1358
- }
1359
-
1360
- return response.data.content.content;
1361
- },
1362
-
1363
- async writeFile(
1364
- path: string,
1365
- content: string | ArrayBuffer,
1366
- encoding: BufferEncoding = "utf-8"
1367
- ) {
1368
- const contentStr =
1369
- typeof content === "string"
1370
- ? content
1371
- : Buffer.from(content).toString(encoding);
1372
-
1373
- const response =
1374
- await sandbox_openapi.handleWriteFileFromEphemeralDevServer({
1375
- client,
1376
- path: {
1377
- filepath: path,
1378
- "*filepath": path,
1379
- },
1380
- body: {
1381
- devServer: devServerInstance,
1382
- content: contentStr,
1383
- encoding,
1384
- },
1385
- });
1386
-
1387
- if (response.error) {
1388
- throw new Error(
1389
- `Failed to write file: ${JSON.stringify(response.error)}`
1390
- );
1391
- }
1392
- },
1393
- },
1394
-
1395
- process: {
1396
- async exec(cmd: string, background = false) {
1397
- const response = await sandbox_openapi.handleExecOnEphemeralDevServer(
1398
- {
1399
- client,
1400
- body: {
1401
- devServer: devServerInstance,
1402
- command: cmd,
1403
- background,
1404
- },
1405
- }
1406
- );
1407
-
1408
- if (response.error) {
1409
- throw new Error(`Failed to execute command: ${response.error}`);
1410
- }
1411
-
1412
- return {
1413
- id: response.data.id,
1414
- isNew: response.data.isNew,
1415
- stdout: response.data.stdout,
1416
- stderr: response.data.stderr,
1417
- };
1418
- },
1419
- },
1420
- };
1421
- }
1422
-
1423
- fetch(path: string, init?: RequestInit) {
1424
- const headers = new Headers(init?.headers);
1425
-
1426
- for (const [key, value] of Object.entries(this.options.headers ?? {})) {
1427
- if (!headers.has(key)) {
1428
- headers.append(key, value);
1429
- }
1430
- }
1431
-
1432
- if (!headers.has("Authorization")) {
1433
- headers.append("Authorization", `Bearer ${this.options.apiKey}`);
1434
- }
1435
-
1436
- if (!headers.has("Content-Type")) {
1437
- headers.append("Content-Type", "application/json");
1438
- }
1439
-
1440
- const url = new URL(
1441
- path,
1442
- this.options.baseUrl ?? "https://api.freestyle.sh"
1443
- );
1444
-
1445
- return fetch(url, {
1446
- ...(init ?? {}),
1447
- headers,
1448
- });
1449
- }
1450
- }
1451
-
1452
- export * from "../openapi/types.gen.ts";
1453
-
1454
- // const { process, fs } = await api.requestDevServer({
1455
- // repoId: "test",
1456
- // });
1457
- //
1458
- // await process.exec()
1459
- // await fs.ls()