|
1 | 1 | import { z } from "../framework";
|
2 | 2 |
|
3 | 3 | import { FailedAPIOperationSchema } from "./common";
|
4 |
| -import { ProjectIdSchema } from "./projects/common"; |
5 | 4 | import { ComputeServerIdSchema } from "./compute/common";
|
| 5 | +import { ProjectIdSchema } from "./projects/common"; |
6 | 6 |
|
7 | 7 | // OpenAPI spec
|
8 | 8 | //
|
9 | 9 | export const ExecInputSchema = z
|
10 |
| - .object({ |
11 |
| - project_id: ProjectIdSchema, |
12 |
| - compute_server_id: ComputeServerIdSchema.describe( |
13 |
| - `If provided, the desired shell command will be run on the compute server whose id |
| 10 | + .union([ |
| 11 | + z.object({ |
| 12 | + project_id: ProjectIdSchema, |
| 13 | + compute_server_id: ComputeServerIdSchema.describe( |
| 14 | + `If provided, the desired shell command will be run on the compute server whose id |
14 | 15 | is specified in this field (if available).`,
|
15 |
| - ).optional(), |
16 |
| - filesystem: z |
17 |
| - .boolean() |
18 |
| - .optional() |
19 |
| - .describe( |
20 |
| - `If \`true\`, this shell command runs in the fileserver container on the compute |
| 16 | + ).optional(), |
| 17 | + filesystem: z |
| 18 | + .boolean() |
| 19 | + .optional() |
| 20 | + .describe( |
| 21 | + `If \`true\`, this shell command runs in the fileserver container on the compute |
21 | 22 | server; otherwise, it runs on the main compute container.`,
|
22 |
| - ), |
23 |
| - path: z |
24 |
| - .string() |
25 |
| - .optional() |
26 |
| - .describe( |
27 |
| - "Path to working directory in which the shell command should be executed.", |
28 |
| - ), |
29 |
| - command: z.string().describe("The shell command to execute."), |
30 |
| - args: z |
31 |
| - .array(z.string()) |
32 |
| - .optional() |
33 |
| - .describe("An array of arguments to pass to the shell command."), |
34 |
| - timeout: z |
35 |
| - .number() |
36 |
| - .min(0) |
37 |
| - .default(60) |
38 |
| - .optional() |
39 |
| - .describe("Number of seconds before this shell command times out."), |
40 |
| - max_output: z |
41 |
| - .number() |
42 |
| - .min(0) |
43 |
| - .optional() |
44 |
| - .describe("Maximum number of bytes to return from shell command output."), |
45 |
| - bash: z |
46 |
| - .boolean() |
47 |
| - .optional() |
48 |
| - .describe( |
49 |
| - `If \`true\`, this command runs in a \`bash\` shell. To do so, the provided shell |
| 23 | + ), |
| 24 | + path: z |
| 25 | + .string() |
| 26 | + .optional() |
| 27 | + .describe( |
| 28 | + "Path to working directory in which the shell command should be executed.", |
| 29 | + ), |
| 30 | + command: z.string().describe("The shell command to execute."), |
| 31 | + args: z |
| 32 | + .array(z.string()) |
| 33 | + .optional() |
| 34 | + .describe("An array of arguments to pass to the shell command."), |
| 35 | + timeout: z |
| 36 | + .number() |
| 37 | + .min(0) |
| 38 | + .default(60) |
| 39 | + .optional() |
| 40 | + .describe("Number of seconds before this shell command times out."), |
| 41 | + max_output: z |
| 42 | + .number() |
| 43 | + .min(0) |
| 44 | + .optional() |
| 45 | + .describe( |
| 46 | + "Maximum number of bytes to return from shell command output.", |
| 47 | + ), |
| 48 | + bash: z |
| 49 | + .boolean() |
| 50 | + .optional() |
| 51 | + .describe( |
| 52 | + `If \`true\`, this command runs in a \`bash\` shell. To do so, the provided shell |
50 | 53 | command is written to a file and then executed via the \`bash\` command.`,
|
51 |
| - ), |
52 |
| - aggregate: z |
53 |
| - .union([ |
54 |
| - z.number(), |
55 |
| - z.string(), |
56 |
| - z.object({ value: z.union([z.string(), z.number()]) }), |
57 |
| - ]) |
58 |
| - .optional() |
59 |
| - .describe( |
60 |
| - `If provided, this shell command is aggregated as in |
| 54 | + ), |
| 55 | + aggregate: z |
| 56 | + .union([ |
| 57 | + z.number(), |
| 58 | + z.string(), |
| 59 | + z.object({ value: z.union([z.string(), z.number()]) }), |
| 60 | + ]) |
| 61 | + .optional() |
| 62 | + .describe( |
| 63 | + `If provided, this shell command is aggregated as in |
61 | 64 | \`src/packages/backend/aggregate.js\`. This parameter allows one to specify
|
62 | 65 | multiple callbacks to be executed against the output of the same command
|
63 | 66 | (given identical arguments) within a 60-second window.`,
|
64 |
| - ), |
65 |
| - err_on_exit: z |
66 |
| - .boolean() |
67 |
| - .optional() |
68 |
| - .describe( |
69 |
| - `When \`true\`, this call will throw an error whenever the provided shell command |
| 67 | + ), |
| 68 | + err_on_exit: z |
| 69 | + .boolean() |
| 70 | + .optional() |
| 71 | + .describe( |
| 72 | + `When \`true\`, this call will throw an error whenever the provided shell command |
70 | 73 | exits with a non-zero exit code.`,
|
71 |
| - ), |
72 |
| - env: z |
73 |
| - .record(z.string(), z.string()) |
74 |
| - .optional() |
75 |
| - .describe( |
76 |
| - "Environment variables to be passed to the shell command upon execution.", |
77 |
| - ), |
78 |
| - async_exec: z.boolean().optional() |
79 |
| - .describe(`If \`true\`, the execution happens asynchroneously. |
80 |
| - This means it the API call does not block and returns an ID (\`async_id\`). |
81 |
| - Later, use that ID in a call to \`async_get\` to eventually get the result`), |
82 |
| - async_get: z.string().optional() |
83 |
| - .describe(`For a given \`async_id\` returned by \`async\`, |
84 |
| - retun the status, or the result as if it is called synchroneously. |
85 |
| - Results are only cached temporarily!`), |
86 |
| - }) |
| 74 | + ), |
| 75 | + env: z |
| 76 | + .record(z.string(), z.string()) |
| 77 | + .optional() |
| 78 | + .describe( |
| 79 | + "Environment variables to be passed to the shell command upon execution.", |
| 80 | + ), |
| 81 | + async_mode: z.boolean().optional() |
| 82 | + .describe(`If \`true\`, the execution happens asynchroneously. |
| 83 | +This means this API call does not block and returns an ID (\`async_id\`). |
| 84 | +Later, use that ID in a call to \`async_get\` to eventually get the result. |
| 85 | +
|
| 86 | +Additionally and if not specified: \`max_output\` is set to 1MB and and \`timeout\` to 10 minutes.`), |
| 87 | + }), |
| 88 | + |
| 89 | + z.object({ |
| 90 | + project_id: ProjectIdSchema, |
| 91 | + async_get: z.string().optional() |
| 92 | + .describe(`For a given \`async_id\` returned by \`async\`, |
| 93 | + retun the status, or the result as if it is called synchroneously. |
| 94 | + Results are only cached temporarily!`), |
| 95 | + }), |
| 96 | + ]) |
87 | 97 | .describe("Perform arbitrary shell commands in a compute server or project.");
|
88 | 98 |
|
89 | 99 | export const ExecOutputSchema = z.union([
|
| 100 | + z |
| 101 | + .object({ |
| 102 | + stdout: z.string().describe("Output to stdout"), |
| 103 | + stderr: z.string().describe("Output to stderr"), |
| 104 | + exit_code: z |
| 105 | + .number() |
| 106 | + .describe( |
| 107 | + "The numeric exit code. 0 usually means it ran without any issues.", |
| 108 | + ), |
| 109 | + async_id: z |
| 110 | + .string() |
| 111 | + .optional() |
| 112 | + .describe("The ID identifying the async operation (async only)"), |
| 113 | + async_start: z |
| 114 | + .number() |
| 115 | + .optional() |
| 116 | + .describe("UNIX timestamp when execution started (async only)"), |
| 117 | + elapsed_s: z |
| 118 | + .string() |
| 119 | + .optional() |
| 120 | + .describe("How long the execution took (async only)"), |
| 121 | + async_status: z // AsyncStatus |
| 122 | + .union([ |
| 123 | + z.literal("running"), |
| 124 | + z.literal("completed"), |
| 125 | + z.literal("error"), |
| 126 | + ]) |
| 127 | + .optional() |
| 128 | + .describe("Status of async operation."), |
| 129 | + }) |
| 130 | + .describe("Output of executed command."), |
90 | 131 | FailedAPIOperationSchema,
|
91 |
| - z.any().describe("Output of executed command."), |
92 | 132 | ]);
|
93 | 133 |
|
94 | 134 | export type ExecInput = z.infer<typeof ExecInputSchema>;
|
|
0 commit comments