import {z} from 'zod' export type DAGName = unknown; export type DAGRunId = string; export type CreateApiDagRunsNameDagRunIdStepsStepNameAgentSessionRestartArgs = {remoteNode?: string | undefined, name: DAGName, dagRunId: DAGRunId, stepName: string}; export type CreateApiDagRunsNameDagRunIdStepsStepNameAgentSessionRestartOptions = {baseUrl?: string | undefined, apiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const agentSessionRestartResponse = z.object({dagRunId: z.string(), stepName: z.string(), generation: z.number().int(), resumed: z.boolean(), subDAGRunId: z.string().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 }; /** * Restart a managed-agent session cleanly * * POST /dag-runs/{name}/{dagRunId}/steps/{stepName}/agent-session/restart * * Also accepts auth: basicAuth */ export const createApiDagRunsNameDagRunIdStepsStepNameAgentSessionRestart = async (args: CreateApiDagRunsNameDagRunIdStepsStepNameAgentSessionRestartArgs, options?: CreateApiDagRunsNameDagRunIdStepsStepNameAgentSessionRestartOptions) => { const {baseUrl = "http://localhost/api/v1", fetch: fetchFn = fetch, apiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.remoteNode != null) query.append("remoteNode", String(args.remoteNode)) if (apiToken !== undefined) headers.set('authorization', `Bearer ${apiToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/dag-runs/${encodeURIComponent(String(args.name))}/${encodeURIComponent(String(args.dagRunId))}/steps/${encodeURIComponent(String(args.stepName))}/agent-session/restart${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(agentSessionRestartResponse, await res.json()) };