@blaxel/core 0.2.10-dev.79 → 0.2.10-dev.80

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.
@@ -14,107 +14,115 @@ export type SandboxFilesystemFile = {
14
14
  path: string;
15
15
  content: string;
16
16
  };
17
+ export declare const CpParamsSchema: z.ZodObject<{
18
+ source: z.ZodString;
19
+ destination: z.ZodString;
20
+ }, "strip", z.ZodTypeAny, {
21
+ source: string;
22
+ destination: string;
23
+ }, {
24
+ source: string;
25
+ destination: string;
26
+ }>;
27
+ export declare const MkdirParamsSchema: z.ZodObject<{
28
+ path: z.ZodString;
29
+ permissions: z.ZodDefault<z.ZodOptional<z.ZodString>>;
30
+ }, "strip", z.ZodTypeAny, {
31
+ path: string;
32
+ permissions: string;
33
+ }, {
34
+ path: string;
35
+ permissions?: string | undefined;
36
+ }>;
37
+ export declare const LsParamsSchema: z.ZodObject<{
38
+ path: z.ZodString;
39
+ }, "strip", z.ZodTypeAny, {
40
+ path: string;
41
+ }, {
42
+ path: string;
43
+ }>;
44
+ export declare const RmParamsSchema: z.ZodObject<{
45
+ path: z.ZodString;
46
+ recursive: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
47
+ }, "strip", z.ZodTypeAny, {
48
+ path: string;
49
+ recursive: boolean;
50
+ }, {
51
+ path: string;
52
+ recursive?: boolean | undefined;
53
+ }>;
54
+ export declare const ReadParamsSchema: z.ZodObject<{
55
+ path: z.ZodString;
56
+ }, "strip", z.ZodTypeAny, {
57
+ path: string;
58
+ }, {
59
+ path: string;
60
+ }>;
61
+ export declare const WriteParamsSchema: z.ZodObject<{
62
+ path: z.ZodString;
63
+ content: z.ZodString;
64
+ }, "strip", z.ZodTypeAny, {
65
+ path: string;
66
+ content: string;
67
+ }, {
68
+ path: string;
69
+ content: string;
70
+ }>;
17
71
  export type ToolWithoutExecute = {
18
72
  cp: {
19
73
  description: string;
20
- parameters: z.ZodObject<{
21
- source: z.ZodString;
22
- destination: z.ZodString;
23
- }>;
74
+ parameters: typeof CpParamsSchema;
24
75
  };
25
76
  mkdir: {
26
77
  description: string;
27
- parameters: z.ZodObject<{
28
- path: z.ZodString;
29
- permissions: z.ZodDefault<z.ZodOptional<z.ZodString>>;
30
- }>;
78
+ parameters: typeof MkdirParamsSchema;
31
79
  };
32
80
  ls: {
33
81
  description: string;
34
- parameters: z.ZodObject<{
35
- path: z.ZodString;
36
- }>;
82
+ parameters: typeof LsParamsSchema;
37
83
  };
38
84
  rm: {
39
85
  description: string;
40
- parameters: z.ZodObject<{
41
- path: z.ZodString;
42
- }>;
86
+ parameters: typeof RmParamsSchema;
43
87
  };
44
88
  read: {
45
89
  description: string;
46
- parameters: z.ZodObject<{
47
- path: z.ZodString;
48
- }>;
90
+ parameters: typeof ReadParamsSchema;
49
91
  };
50
92
  write: {
51
93
  description: string;
52
- parameters: z.ZodObject<{
53
- path: z.ZodString;
54
- }>;
94
+ parameters: typeof WriteParamsSchema;
55
95
  };
56
96
  };
57
97
  export type ToolWithExecute = {
58
98
  cp: {
59
99
  description: string;
60
- parameters: z.ZodObject<{
61
- source: z.ZodString;
62
- destination: z.ZodString;
63
- }>;
64
- execute: (args: {
65
- source: string;
66
- destination: string;
67
- }) => Promise<string>;
100
+ parameters: typeof CpParamsSchema;
101
+ execute: (args: z.infer<typeof CpParamsSchema>) => Promise<string>;
68
102
  };
69
103
  mkdir: {
70
104
  description: string;
71
- parameters: z.ZodObject<{
72
- path: z.ZodString;
73
- permissions: z.ZodDefault<z.ZodOptional<z.ZodString>>;
74
- }>;
75
- execute: (args: {
76
- path: string;
77
- permissions: string;
78
- }) => Promise<string>;
105
+ parameters: typeof MkdirParamsSchema;
106
+ execute: (args: z.infer<typeof MkdirParamsSchema>) => Promise<string>;
79
107
  };
80
108
  ls: {
81
109
  description: string;
82
- parameters: z.ZodObject<{
83
- path: z.ZodString;
84
- }>;
85
- execute: (args: {
86
- path: string;
87
- }) => Promise<string>;
110
+ parameters: typeof LsParamsSchema;
111
+ execute: (args: z.infer<typeof LsParamsSchema>) => Promise<string>;
88
112
  };
89
113
  rm: {
90
114
  description: string;
91
- parameters: z.ZodObject<{
92
- path: z.ZodString;
93
- recursive: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
94
- }>;
95
- execute: (args: {
96
- path: string;
97
- recursive: boolean;
98
- }) => Promise<string>;
115
+ parameters: typeof RmParamsSchema;
116
+ execute: (args: z.infer<typeof RmParamsSchema>) => Promise<string>;
99
117
  };
100
118
  read: {
101
119
  description: string;
102
- parameters: z.ZodObject<{
103
- path: z.ZodString;
104
- }>;
105
- execute: (args: {
106
- path: string;
107
- }) => Promise<string>;
120
+ parameters: typeof ReadParamsSchema;
121
+ execute: (args: z.infer<typeof ReadParamsSchema>) => Promise<string>;
108
122
  };
109
123
  write: {
110
124
  description: string;
111
- parameters: z.ZodObject<{
112
- path: z.ZodString;
113
- content: z.ZodString;
114
- }>;
115
- execute: (args: {
116
- path: string;
117
- content: string;
118
- }) => Promise<string>;
125
+ parameters: typeof WriteParamsSchema;
126
+ execute: (args: z.infer<typeof WriteParamsSchema>) => Promise<string>;
119
127
  };
120
128
  };
@@ -1,2 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WriteParamsSchema = exports.ReadParamsSchema = exports.RmParamsSchema = exports.LsParamsSchema = exports.MkdirParamsSchema = exports.CpParamsSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.CpParamsSchema = zod_1.z.object({
6
+ source: zod_1.z.string().describe("Source file or directory path"),
7
+ destination: zod_1.z.string().describe("Destination file or directory path"),
8
+ }).describe("Parameters for copying a file or directory");
9
+ exports.MkdirParamsSchema = zod_1.z.object({
10
+ path: zod_1.z.string().describe("Directory path to create"),
11
+ permissions: zod_1.z.string().optional().default("0755").describe("Permissions for the new directory (default 0755)"),
12
+ }).describe("Parameters for creating a directory");
13
+ exports.LsParamsSchema = zod_1.z.object({
14
+ path: zod_1.z.string().describe("Directory path to list"),
15
+ }).describe("Parameters for listing a directory");
16
+ exports.RmParamsSchema = zod_1.z.object({
17
+ path: zod_1.z.string().describe("File or directory path to remove"),
18
+ recursive: zod_1.z.boolean().optional().default(false).describe("Whether to remove recursively (default false)"),
19
+ }).describe("Parameters for removing a file or directory");
20
+ exports.ReadParamsSchema = zod_1.z.object({
21
+ path: zod_1.z.string().describe("File path to read"),
22
+ }).describe("Parameters for reading a file");
23
+ exports.WriteParamsSchema = zod_1.z.object({
24
+ path: zod_1.z.string().describe("File path to write to"),
25
+ content: zod_1.z.string().describe("Content to write to the file"),
26
+ }).describe("Parameters for writing to a file");
@@ -0,0 +1 @@
1
+ export * from "./network.js";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./network.js"), exports);
@@ -0,0 +1,5 @@
1
+ import { Sandbox } from "../../client/types.gen.js";
2
+ import { SandboxAction } from "../action.js";
3
+ export declare class SandboxNetwork extends SandboxAction {
4
+ constructor(sandbox: Sandbox);
5
+ }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SandboxNetwork = void 0;
4
- const action_js_1 = require("./action.js");
4
+ const action_js_1 = require("../action.js");
5
5
  class SandboxNetwork extends action_js_1.SandboxAction {
6
6
  constructor(sandbox) {
7
7
  super(sandbox);
@@ -30,6 +30,7 @@ export declare class SandboxPreviews {
30
30
  get sandboxName(): string;
31
31
  list(): Promise<SandboxPreview[]>;
32
32
  create(preview: Preview): Promise<SandboxPreview>;
33
+ createIfNotExists(preview: Preview): Promise<SandboxPreview>;
33
34
  get(previewName: string): Promise<SandboxPreview>;
34
35
  delete(previewName: string): Promise<Preview>;
35
36
  }
@@ -109,6 +109,18 @@ class SandboxPreviews {
109
109
  });
110
110
  return new SandboxPreview(data);
111
111
  }
112
+ async createIfNotExists(preview) {
113
+ try {
114
+ const previewInstance = await this.get(preview.metadata?.name ?? "");
115
+ return previewInstance;
116
+ }
117
+ catch (e) {
118
+ if (typeof e === "object" && e !== null && "code" in e && e.code === 404) {
119
+ return this.create(preview);
120
+ }
121
+ throw e;
122
+ }
123
+ }
112
124
  async get(previewName) {
113
125
  const { data } = await (0, index_js_1.getSandboxPreview)({
114
126
  path: {
@@ -0,0 +1,2 @@
1
+ export * from "./process.js";
2
+ export * from "./types.js";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./process.js"), exports);
18
+ __exportStar(require("./types.js"), exports);
@@ -1,6 +1,7 @@
1
- import { Sandbox } from "../client/types.gen.js";
2
- import { SandboxAction } from "./action.js";
3
- import { DeleteProcessByIdentifierKillResponse, DeleteProcessByIdentifierResponse, GetProcessByIdentifierResponse, GetProcessResponse, PostProcessResponse, ProcessRequest } from "./client/index.js";
1
+ import { Sandbox } from "../../client/types.gen.js";
2
+ import { SandboxAction } from "../action.js";
3
+ import { DeleteProcessByIdentifierKillResponse, DeleteProcessByIdentifierResponse, GetProcessByIdentifierResponse, GetProcessResponse, PostProcessResponse, ProcessRequest } from "../client/index.js";
4
+ import { ProcessToolWithExecute, ProcessToolWithoutExecute } from "./types.js";
4
5
  export declare class SandboxProcess extends SandboxAction {
5
6
  constructor(sandbox: Sandbox);
6
7
  streamLogs(identifier: string, options: {
@@ -20,4 +21,6 @@ export declare class SandboxProcess extends SandboxAction {
20
21
  stop(identifier: string): Promise<DeleteProcessByIdentifierResponse>;
21
22
  kill(identifier: string): Promise<DeleteProcessByIdentifierKillResponse>;
22
23
  logs(identifier: string, type?: "stdout" | "stderr" | "all"): Promise<string>;
24
+ get toolsWithoutExecute(): ProcessToolWithoutExecute;
25
+ get tools(): ProcessToolWithExecute;
23
26
  }
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SandboxProcess = void 0;
4
+ const settings_js_1 = require("../../common/settings.js");
5
+ const action_js_1 = require("../action.js");
6
+ const index_js_1 = require("../client/index.js");
7
+ const types_js_1 = require("./types.js");
8
+ class SandboxProcess extends action_js_1.SandboxAction {
9
+ constructor(sandbox) {
10
+ super(sandbox);
11
+ }
12
+ streamLogs(identifier, options) {
13
+ const controller = new AbortController();
14
+ (async () => {
15
+ try {
16
+ const headers = this.sandbox.forceUrl ? this.sandbox.headers : settings_js_1.settings.headers;
17
+ const stream = await fetch(`${this.url}/process/${identifier}/logs/stream`, {
18
+ method: 'GET',
19
+ signal: controller.signal,
20
+ headers,
21
+ });
22
+ if (stream.status !== 200) {
23
+ throw new Error(`Failed to stream logs: ${await stream.text()}`);
24
+ }
25
+ if (!stream.body)
26
+ throw new Error('No stream body');
27
+ const reader = stream.body.getReader();
28
+ const decoder = new TextDecoder();
29
+ let buffer = '';
30
+ while (true) {
31
+ const { done, value } = await reader.read();
32
+ if (done)
33
+ break;
34
+ buffer += decoder.decode(value, { stream: true });
35
+ let lines = buffer.split(/\r?\n/);
36
+ buffer = lines.pop();
37
+ for (const line of lines) {
38
+ if (line.startsWith('stdout:')) {
39
+ options.onStdout?.(line.slice(7));
40
+ options.onLog?.(line.slice(7));
41
+ }
42
+ else if (line.startsWith('stderr:')) {
43
+ options.onStderr?.(line.slice(7));
44
+ options.onLog?.(line.slice(7));
45
+ }
46
+ else {
47
+ options.onLog?.(line);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ catch (err) {
53
+ if (err && err.name !== 'AbortError') {
54
+ console.error("Stream error:", err);
55
+ throw err;
56
+ }
57
+ }
58
+ })();
59
+ return {
60
+ close: () => controller.abort(),
61
+ };
62
+ }
63
+ async exec(process) {
64
+ const { response, data, error } = await (0, index_js_1.postProcess)({
65
+ body: process,
66
+ baseUrl: this.url,
67
+ client: this.client,
68
+ });
69
+ this.handleResponseError(response, data, error);
70
+ return data;
71
+ }
72
+ async wait(identifier, { maxWait = 60000, interval = 1000 } = {}) {
73
+ const startTime = Date.now();
74
+ let status = "running";
75
+ let data = await this.get(identifier);
76
+ while (status === "running") {
77
+ await new Promise((resolve) => setTimeout(resolve, interval));
78
+ try {
79
+ data = await this.get(identifier);
80
+ status = data.status ?? "running";
81
+ }
82
+ catch {
83
+ break;
84
+ }
85
+ if (Date.now() - startTime > maxWait) {
86
+ throw new Error("Process did not finish in time");
87
+ }
88
+ }
89
+ return data;
90
+ }
91
+ async get(identifier) {
92
+ const { response, data, error } = await (0, index_js_1.getProcessByIdentifier)({
93
+ path: { identifier },
94
+ baseUrl: this.url,
95
+ client: this.client,
96
+ });
97
+ this.handleResponseError(response, data, error);
98
+ return data;
99
+ }
100
+ async list() {
101
+ const { response, data, error } = await (0, index_js_1.getProcess)({
102
+ baseUrl: this.url,
103
+ client: this.client,
104
+ });
105
+ this.handleResponseError(response, data, error);
106
+ return data;
107
+ }
108
+ async stop(identifier) {
109
+ const { response, data, error } = await (0, index_js_1.deleteProcessByIdentifier)({
110
+ path: { identifier },
111
+ baseUrl: this.url,
112
+ client: this.client,
113
+ });
114
+ this.handleResponseError(response, data, error);
115
+ return data;
116
+ }
117
+ async kill(identifier) {
118
+ const { response, data, error } = await (0, index_js_1.deleteProcessByIdentifierKill)({
119
+ path: { identifier },
120
+ baseUrl: this.url,
121
+ client: this.client,
122
+ });
123
+ this.handleResponseError(response, data, error);
124
+ return data;
125
+ }
126
+ async logs(identifier, type = "all") {
127
+ const { response, data, error } = await (0, index_js_1.getProcessByIdentifierLogs)({
128
+ path: { identifier },
129
+ baseUrl: this.url,
130
+ client: this.client,
131
+ });
132
+ this.handleResponseError(response, data, error);
133
+ if (type === "all") {
134
+ return data?.logs || "";
135
+ }
136
+ else if (type === "stdout") {
137
+ return data?.stdout || "";
138
+ }
139
+ else if (type === "stderr") {
140
+ return data?.stderr || "";
141
+ }
142
+ throw new Error("Unsupported log type");
143
+ }
144
+ get toolsWithoutExecute() {
145
+ return {
146
+ exec: {
147
+ description: "Execute a process in the sandbox",
148
+ parameters: types_js_1.ExecParamsSchema,
149
+ },
150
+ wait: {
151
+ description: "Wait for a process to finish by identifier",
152
+ parameters: types_js_1.WaitParamsSchema,
153
+ },
154
+ get: {
155
+ description: "Get process info by identifier",
156
+ parameters: types_js_1.GetParamsSchema,
157
+ },
158
+ list: {
159
+ description: "List all processes in the sandbox",
160
+ parameters: types_js_1.ListParamsSchema,
161
+ },
162
+ stop: {
163
+ description: "Stop a process by identifier",
164
+ parameters: types_js_1.StopParamsSchema,
165
+ },
166
+ kill: {
167
+ description: "Kill a process by identifier",
168
+ parameters: types_js_1.KillParamsSchema,
169
+ },
170
+ logs: {
171
+ description: "Get logs for a process by identifier",
172
+ parameters: types_js_1.LogsParamsSchema,
173
+ },
174
+ };
175
+ }
176
+ get tools() {
177
+ return {
178
+ exec: {
179
+ description: "Execute a process in the sandbox",
180
+ parameters: types_js_1.ExecParamsSchema,
181
+ execute: async (args) => {
182
+ try {
183
+ const result = await this.exec(args.process);
184
+ return JSON.stringify(result);
185
+ }
186
+ catch (e) {
187
+ return JSON.stringify({ message: e.message, process: args.process });
188
+ }
189
+ },
190
+ },
191
+ wait: {
192
+ description: "Wait for a process to finish by identifier",
193
+ parameters: types_js_1.WaitParamsSchema,
194
+ execute: async (args) => {
195
+ try {
196
+ const result = await this.wait(args.identifier, { maxWait: args.maxWait, interval: args.interval });
197
+ return JSON.stringify(result);
198
+ }
199
+ catch (e) {
200
+ return JSON.stringify({ message: e.message, identifier: args.identifier });
201
+ }
202
+ },
203
+ },
204
+ get: {
205
+ description: "Get process info by identifier",
206
+ parameters: types_js_1.GetParamsSchema,
207
+ execute: async (args) => {
208
+ try {
209
+ const result = await this.get(args.identifier);
210
+ return JSON.stringify(result);
211
+ }
212
+ catch (e) {
213
+ return JSON.stringify({ message: e.message, identifier: args.identifier });
214
+ }
215
+ },
216
+ },
217
+ list: {
218
+ description: "List all processes in the sandbox",
219
+ parameters: types_js_1.ListParamsSchema,
220
+ execute: async () => {
221
+ try {
222
+ const result = await this.list();
223
+ return JSON.stringify(result);
224
+ }
225
+ catch (e) {
226
+ return JSON.stringify({ message: e.message });
227
+ }
228
+ },
229
+ },
230
+ stop: {
231
+ description: "Stop a process by identifier",
232
+ parameters: types_js_1.StopParamsSchema,
233
+ execute: async (args) => {
234
+ try {
235
+ const result = await this.stop(args.identifier);
236
+ return JSON.stringify(result);
237
+ }
238
+ catch (e) {
239
+ return JSON.stringify({ message: e.message, identifier: args.identifier });
240
+ }
241
+ },
242
+ },
243
+ kill: {
244
+ description: "Kill a process by identifier",
245
+ parameters: types_js_1.KillParamsSchema,
246
+ execute: async (args) => {
247
+ try {
248
+ const result = await this.kill(args.identifier);
249
+ return JSON.stringify(result);
250
+ }
251
+ catch (e) {
252
+ return JSON.stringify({ message: e.message, identifier: args.identifier });
253
+ }
254
+ },
255
+ },
256
+ logs: {
257
+ description: "Get logs for a process by identifier",
258
+ parameters: types_js_1.LogsParamsSchema,
259
+ execute: async (args) => {
260
+ try {
261
+ const result = await this.logs(args.identifier, args.type || "all");
262
+ return JSON.stringify({ logs: result });
263
+ }
264
+ catch (e) {
265
+ return JSON.stringify({ message: e.message, identifier: args.identifier });
266
+ }
267
+ },
268
+ },
269
+ };
270
+ }
271
+ }
272
+ exports.SandboxProcess = SandboxProcess;
@@ -0,0 +1,120 @@
1
+ import { z } from "zod";
2
+ export declare const ExecParamsSchema: z.ZodObject<{
3
+ process: z.ZodAny;
4
+ }, "strip", z.ZodTypeAny, {
5
+ process?: any;
6
+ }, {
7
+ process?: any;
8
+ }>;
9
+ export declare const WaitParamsSchema: z.ZodObject<{
10
+ identifier: z.ZodString;
11
+ maxWait: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
12
+ interval: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
13
+ }, "strip", z.ZodTypeAny, {
14
+ identifier: string;
15
+ maxWait: number;
16
+ interval: number;
17
+ }, {
18
+ identifier: string;
19
+ maxWait?: number | undefined;
20
+ interval?: number | undefined;
21
+ }>;
22
+ export declare const GetParamsSchema: z.ZodObject<{
23
+ identifier: z.ZodString;
24
+ }, "strip", z.ZodTypeAny, {
25
+ identifier: string;
26
+ }, {
27
+ identifier: string;
28
+ }>;
29
+ export declare const ListParamsSchema: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
30
+ export declare const StopParamsSchema: z.ZodObject<{
31
+ identifier: z.ZodString;
32
+ }, "strip", z.ZodTypeAny, {
33
+ identifier: string;
34
+ }, {
35
+ identifier: string;
36
+ }>;
37
+ export declare const KillParamsSchema: z.ZodObject<{
38
+ identifier: z.ZodString;
39
+ }, "strip", z.ZodTypeAny, {
40
+ identifier: string;
41
+ }, {
42
+ identifier: string;
43
+ }>;
44
+ export declare const LogsParamsSchema: z.ZodObject<{
45
+ identifier: z.ZodString;
46
+ type: z.ZodDefault<z.ZodOptional<z.ZodEnum<["stdout", "stderr", "all"]>>>;
47
+ }, "strip", z.ZodTypeAny, {
48
+ type: "stdout" | "stderr" | "all";
49
+ identifier: string;
50
+ }, {
51
+ identifier: string;
52
+ type?: "stdout" | "stderr" | "all" | undefined;
53
+ }>;
54
+ export type ProcessToolWithoutExecute = {
55
+ exec: {
56
+ description: string;
57
+ parameters: typeof ExecParamsSchema;
58
+ };
59
+ wait: {
60
+ description: string;
61
+ parameters: typeof WaitParamsSchema;
62
+ };
63
+ get: {
64
+ description: string;
65
+ parameters: typeof GetParamsSchema;
66
+ };
67
+ list: {
68
+ description: string;
69
+ parameters: typeof ListParamsSchema;
70
+ };
71
+ stop: {
72
+ description: string;
73
+ parameters: typeof StopParamsSchema;
74
+ };
75
+ kill: {
76
+ description: string;
77
+ parameters: typeof KillParamsSchema;
78
+ };
79
+ logs: {
80
+ description: string;
81
+ parameters: typeof LogsParamsSchema;
82
+ };
83
+ };
84
+ export type ProcessToolWithExecute = {
85
+ exec: {
86
+ description: string;
87
+ parameters: typeof ExecParamsSchema;
88
+ execute: (args: z.infer<typeof ExecParamsSchema>) => Promise<string>;
89
+ };
90
+ wait: {
91
+ description: string;
92
+ parameters: typeof WaitParamsSchema;
93
+ execute: (args: z.infer<typeof WaitParamsSchema>) => Promise<string>;
94
+ };
95
+ get: {
96
+ description: string;
97
+ parameters: typeof GetParamsSchema;
98
+ execute: (args: z.infer<typeof GetParamsSchema>) => Promise<string>;
99
+ };
100
+ list: {
101
+ description: string;
102
+ parameters: typeof ListParamsSchema;
103
+ execute: (args: z.infer<typeof ListParamsSchema>) => Promise<string>;
104
+ };
105
+ stop: {
106
+ description: string;
107
+ parameters: typeof StopParamsSchema;
108
+ execute: (args: z.infer<typeof StopParamsSchema>) => Promise<string>;
109
+ };
110
+ kill: {
111
+ description: string;
112
+ parameters: typeof KillParamsSchema;
113
+ execute: (args: z.infer<typeof KillParamsSchema>) => Promise<string>;
114
+ };
115
+ logs: {
116
+ description: string;
117
+ parameters: typeof LogsParamsSchema;
118
+ execute: (args: z.infer<typeof LogsParamsSchema>) => Promise<string>;
119
+ };
120
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LogsParamsSchema = exports.KillParamsSchema = exports.StopParamsSchema = exports.ListParamsSchema = exports.GetParamsSchema = exports.WaitParamsSchema = exports.ExecParamsSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ exports.ExecParamsSchema = zod_1.z.object({
6
+ process: zod_1.z.any().describe("ProcessRequest object to execute in the sandbox"), // Refine if you have a zod schema for ProcessRequest
7
+ }).describe("Parameters for executing a process in the sandbox");
8
+ exports.WaitParamsSchema = zod_1.z.object({
9
+ identifier: zod_1.z.string().describe("Process identifier to wait for"),
10
+ maxWait: zod_1.z.number().optional().default(60000).describe("Maximum wait time in milliseconds (default 60000)"),
11
+ interval: zod_1.z.number().optional().default(1000).describe("Polling interval in milliseconds (default 1000)"),
12
+ }).describe("Parameters for waiting for a process to finish");
13
+ exports.GetParamsSchema = zod_1.z.object({
14
+ identifier: zod_1.z.string().describe("Process identifier to get info for"),
15
+ }).describe("Parameters for getting process info");
16
+ exports.ListParamsSchema = zod_1.z.object({}).describe("Parameters for listing all processes (none)");
17
+ exports.StopParamsSchema = zod_1.z.object({
18
+ identifier: zod_1.z.string().describe("Process identifier to stop"),
19
+ }).describe("Parameters for stopping a process");
20
+ exports.KillParamsSchema = zod_1.z.object({
21
+ identifier: zod_1.z.string().describe("Process identifier to kill"),
22
+ }).describe("Parameters for killing a process");
23
+ exports.LogsParamsSchema = zod_1.z.object({
24
+ identifier: zod_1.z.string().describe("Process identifier to get logs for"),
25
+ type: zod_1.z.enum(["stdout", "stderr", "all"]).optional().default("all").describe("Type of logs to retrieve: stdout, stderr, or all (default all)"),
26
+ }).describe("Parameters for retrieving process logs");
@@ -1,8 +1,8 @@
1
1
  import { Sandbox as SandboxModel } from "../client/index.js";
2
2
  import { SandboxFileSystem } from "./filesystem/index.js";
3
- import { SandboxNetwork } from "./network.js";
3
+ import { SandboxNetwork } from "./network/index.js";
4
4
  import { SandboxPreviews } from "./preview.js";
5
- import { SandboxProcess } from "./process.js";
5
+ import { SandboxProcess } from "./process/index.js";
6
6
  import { SandboxSessions } from "./session.js";
7
7
  import { SandboxConfiguration, SessionWithToken } from "./types.js";
8
8
  export declare class SandboxInstance {
@@ -25,5 +25,6 @@ export declare class SandboxInstance {
25
25
  static get(sandboxName: string): Promise<SandboxInstance>;
26
26
  static list(): Promise<SandboxInstance[]>;
27
27
  static delete(sandboxName: string): Promise<SandboxModel>;
28
+ static createIfNotExists(sandbox: SandboxModel): Promise<SandboxInstance>;
28
29
  static fromSession(session: SessionWithToken): Promise<SandboxInstance>;
29
30
  }
@@ -4,9 +4,9 @@ exports.SandboxInstance = void 0;
4
4
  const index_js_1 = require("../client/index.js");
5
5
  const logger_js_1 = require("../common/logger.js");
6
6
  const index_js_2 = require("./filesystem/index.js");
7
- const network_js_1 = require("./network.js");
7
+ const index_js_3 = require("./network/index.js");
8
8
  const preview_js_1 = require("./preview.js");
9
- const process_js_1 = require("./process.js");
9
+ const index_js_4 = require("./process/index.js");
10
10
  const session_js_1 = require("./session.js");
11
11
  class SandboxInstance {
12
12
  sandbox;
@@ -18,8 +18,8 @@ class SandboxInstance {
18
18
  constructor(sandbox) {
19
19
  this.sandbox = sandbox;
20
20
  this.fs = new index_js_2.SandboxFileSystem(sandbox);
21
- this.network = new network_js_1.SandboxNetwork(sandbox);
22
- this.process = new process_js_1.SandboxProcess(sandbox);
21
+ this.network = new index_js_3.SandboxNetwork(sandbox);
22
+ this.process = new index_js_4.SandboxProcess(sandbox);
23
23
  this.previews = new preview_js_1.SandboxPreviews(sandbox);
24
24
  this.sessions = new session_js_1.SandboxSessions(sandbox);
25
25
  }
@@ -94,6 +94,18 @@ class SandboxInstance {
94
94
  });
95
95
  return data;
96
96
  }
97
+ static async createIfNotExists(sandbox) {
98
+ try {
99
+ const sandboxInstance = await SandboxInstance.get(sandbox.metadata?.name ?? "");
100
+ return sandboxInstance;
101
+ }
102
+ catch (e) {
103
+ if (typeof e === "object" && e !== null && "code" in e && e.code === 404) {
104
+ return SandboxInstance.create(sandbox);
105
+ }
106
+ throw e;
107
+ }
108
+ }
97
109
  static async fromSession(session) {
98
110
  return new SandboxInstance({ forceUrl: session.url, params: { bl_preview_token: session.token }, headers: { "X-Blaxel-Preview-Token": session.token } });
99
111
  }
@@ -5,6 +5,12 @@ export declare class SandboxSessions {
5
5
  constructor(sandbox: Sandbox);
6
6
  get sandboxName(): string;
7
7
  create(options?: SessionCreateOptions): Promise<SessionWithToken>;
8
+ createIfExpired(options?: SessionCreateOptions, delta?: number): Promise<{
9
+ name: string;
10
+ url: string;
11
+ token: string;
12
+ expiresAt: string | Date;
13
+ }>;
8
14
  list(): Promise<{
9
15
  name: string;
10
16
  url: string;
@@ -41,6 +41,27 @@ class SandboxSessions {
41
41
  expiresAt: typeof tokenObj.expiresAt === 'string' ? new Date(tokenObj.expiresAt) : tokenObj.expiresAt,
42
42
  };
43
43
  }
44
+ async createIfExpired(options = {}, delta = 1000 * 60 * 60) {
45
+ // First, list all sessions
46
+ const allSessions = await this.list();
47
+ // Variable to hold our final session
48
+ let sessionData;
49
+ const now = new Date();
50
+ const threshold = new Date(now.getTime() + delta);
51
+ // If no valid session exists, create a new one
52
+ if (allSessions.length > 0) {
53
+ sessionData = allSessions[0];
54
+ if (sessionData.expiresAt < threshold) {
55
+ await this.delete(sessionData.name);
56
+ sessionData = await this.create(options);
57
+ }
58
+ }
59
+ else {
60
+ // Create a new session
61
+ sessionData = await this.create(options);
62
+ }
63
+ return sessionData;
64
+ }
44
65
  async list() {
45
66
  const { data } = await (0, index_js_1.listSandboxPreviews)({
46
67
  path: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/core",
3
- "version": "0.2.10-dev.79",
3
+ "version": "0.2.10-dev.80",
4
4
  "description": "Blaxel Core SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",
@@ -1,5 +0,0 @@
1
- import { Sandbox } from "../client/types.gen.js";
2
- import { SandboxAction } from "./action.js";
3
- export declare class SandboxNetwork extends SandboxAction {
4
- constructor(sandbox: Sandbox);
5
- }
@@ -1,144 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SandboxProcess = void 0;
4
- const settings_js_1 = require("../common/settings.js");
5
- const action_js_1 = require("./action.js");
6
- const index_js_1 = require("./client/index.js");
7
- class SandboxProcess extends action_js_1.SandboxAction {
8
- constructor(sandbox) {
9
- super(sandbox);
10
- }
11
- streamLogs(identifier, options) {
12
- const controller = new AbortController();
13
- (async () => {
14
- try {
15
- const headers = this.sandbox.forceUrl ? this.sandbox.headers : settings_js_1.settings.headers;
16
- const stream = await fetch(`${this.url}/process/${identifier}/logs/stream`, {
17
- method: 'GET',
18
- signal: controller.signal,
19
- headers,
20
- });
21
- if (stream.status !== 200) {
22
- throw new Error(`Failed to stream logs: ${await stream.text()}`);
23
- }
24
- if (!stream.body)
25
- throw new Error('No stream body');
26
- const reader = stream.body.getReader();
27
- const decoder = new TextDecoder();
28
- let buffer = '';
29
- while (true) {
30
- const { done, value } = await reader.read();
31
- if (done)
32
- break;
33
- buffer += decoder.decode(value, { stream: true });
34
- let lines = buffer.split(/\r?\n/);
35
- buffer = lines.pop();
36
- for (const line of lines) {
37
- if (line.startsWith('stdout:')) {
38
- options.onStdout?.(line.slice(7));
39
- options.onLog?.(line.slice(7));
40
- }
41
- else if (line.startsWith('stderr:')) {
42
- options.onStderr?.(line.slice(7));
43
- options.onLog?.(line.slice(7));
44
- }
45
- else {
46
- options.onLog?.(line);
47
- }
48
- }
49
- }
50
- }
51
- catch (err) {
52
- if (err && err.name !== 'AbortError') {
53
- console.error("Stream error:", err);
54
- throw err;
55
- }
56
- }
57
- })();
58
- return {
59
- close: () => controller.abort(),
60
- };
61
- }
62
- async exec(process) {
63
- const { response, data, error } = await (0, index_js_1.postProcess)({
64
- body: process,
65
- baseUrl: this.url,
66
- client: this.client,
67
- });
68
- this.handleResponseError(response, data, error);
69
- return data;
70
- }
71
- async wait(identifier, { maxWait = 60000, interval = 1000 } = {}) {
72
- const startTime = Date.now();
73
- let status = "running";
74
- let data = await this.get(identifier);
75
- while (status === "running") {
76
- await new Promise((resolve) => setTimeout(resolve, interval));
77
- try {
78
- data = await this.get(identifier);
79
- status = data.status ?? "running";
80
- }
81
- catch {
82
- break;
83
- }
84
- if (Date.now() - startTime > maxWait) {
85
- throw new Error("Process did not finish in time");
86
- }
87
- }
88
- return data;
89
- }
90
- async get(identifier) {
91
- const { response, data, error } = await (0, index_js_1.getProcessByIdentifier)({
92
- path: { identifier },
93
- baseUrl: this.url,
94
- client: this.client,
95
- });
96
- this.handleResponseError(response, data, error);
97
- return data;
98
- }
99
- async list() {
100
- const { response, data, error } = await (0, index_js_1.getProcess)({
101
- baseUrl: this.url,
102
- client: this.client,
103
- });
104
- this.handleResponseError(response, data, error);
105
- return data;
106
- }
107
- async stop(identifier) {
108
- const { response, data, error } = await (0, index_js_1.deleteProcessByIdentifier)({
109
- path: { identifier },
110
- baseUrl: this.url,
111
- client: this.client,
112
- });
113
- this.handleResponseError(response, data, error);
114
- return data;
115
- }
116
- async kill(identifier) {
117
- const { response, data, error } = await (0, index_js_1.deleteProcessByIdentifierKill)({
118
- path: { identifier },
119
- baseUrl: this.url,
120
- client: this.client,
121
- });
122
- this.handleResponseError(response, data, error);
123
- return data;
124
- }
125
- async logs(identifier, type = "all") {
126
- const { response, data, error } = await (0, index_js_1.getProcessByIdentifierLogs)({
127
- path: { identifier },
128
- baseUrl: this.url,
129
- client: this.client,
130
- });
131
- this.handleResponseError(response, data, error);
132
- if (type === "all") {
133
- return data?.logs || "";
134
- }
135
- else if (type === "stdout") {
136
- return data?.stdout || "";
137
- }
138
- else if (type === "stderr") {
139
- return data?.stderr || "";
140
- }
141
- throw new Error("Unsupported log type");
142
- }
143
- }
144
- exports.SandboxProcess = SandboxProcess;