import {z} from 'zod' export type CommandSpec = Array | string; export type MachineCommandRequest = {background?: boolean | undefined, command: CommandSpec, cwd?: (string | null) | undefined, env?: {[key: string]: string} | undefined, stdin?: (string | null) | undefined, stream?: boolean | undefined, timeoutSeconds?: (number | null) | undefined, user?: (string | null) | undefined}; export type CreateApiV1MachinesIdSessionsSessionIdExecArgs = {id: string, session_id: string, body: MachineCommandRequest}; export type CreateApiV1MachinesIdSessionsSessionIdExecOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const machineExecResponse = z.object({durationMs: z.number().int().gte(0), exitCode: z.number().int(), machineId: z.string(), stderr: z.string(), stderrB64: z.string().optional(), stderrTruncated: z.boolean().optional(), stdout: z.string(), stdoutB64: z.string().optional(), stdoutTruncated: z.boolean().optional(), user: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Execute in a session. * * POST /v1/machines/{id}/sessions/{session_id}/exec */ export const createApiV1MachinesIdSessionsSessionIdExec = async (args: CreateApiV1MachinesIdSessionsSessionIdExecArgs, options?: CreateApiV1MachinesIdSessionsSessionIdExecOptions) => { const {baseUrl = "https://api.smolmachines.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines/${encodeURIComponent(String(args.id))}/sessions/${encodeURIComponent(String(args.session_id))}/exec${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(machineExecResponse, await res.json()) };