import {z} from 'zod' export type GetApiWorkflowIdResultArgs = {id: string}; export type GetApiWorkflowIdResultOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowResultStatus = z.enum(["completed", "failed", "cancelled", "terminated", "timed_out", "continued_as_new"]); export const traceInfoV2 = z.object({local: z.string().optional(), remote: z.string().optional()}).nullable(); export const serializedWorkflowError = z.object({activityType: z.string().optional(), name: z.string().optional(), message: z.string().optional()}).and(z.record(z.string(), z.unknown())).nullable(); export const workflowResultV2Response = z.object({v: z.literal("2"), workflowId: z.string(), runId: z.string().nullable(), status: workflowResultStatus, input: z.unknown(), output: z.unknown(), trace: traceInfoV2, error: serializedWorkflowError.nullable()}); export const traceInfoV1 = z.object({destinations: z.object({local: z.string().optional(), remote: z.string().optional()}).optional()}).nullable(); export const workflowResultV1Response = z.object({workflowId: z.string(), runId: z.string().nullable(), status: workflowResultStatus, input: z.unknown(), output: z.unknown(), trace: traceInfoV1, error: z.string().nullable(), errorDetails: z.object({message: z.string().nullable().optional(), name: z.string().nullable().optional(), retryable: z.boolean().nullable().optional(), activityId: z.string().nullable().optional(), cause: z.record(z.string(), z.unknown()).nullable().optional()}).nullable()}); export const workflowResultResponse = z.union([workflowResultV2Response, workflowResultV1Response]); 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 }; /** * Return the result of a workflow (latest run) * * GET /workflow/{id}/result */ export const getApiWorkflowIdResult = async (args: GetApiWorkflowIdResultArgs, options?: GetApiWorkflowIdResultOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/workflow/${encodeURIComponent(String(args.id))}/result${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(workflowResultResponse, await res.json()) };