import {z} from 'zod' export type CreateApiApiV1WWIdSpacesSpaceIdAppsAIdRunsArgs = {wId: string, spaceId: string, aId: string, body: {specification_hash: string, config: {model?: ({provider_id?: string | undefined, model_id?: string | undefined, use_cache?: boolean | undefined, use_stream?: boolean | undefined}) | undefined}, inputs: Array<{[key: string]: unknown}>, stream?: boolean | undefined, blocking?: boolean | undefined, block_filter?: Array | undefined}}; export type CreateApiApiV1WWIdSpacesSpaceIdAppsAIdRunsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const run = z.object({run_id: z.string().optional(), app_id: z.string().optional(), status: z.object({run: z.string().optional(), build: z.string().optional()}).optional(), results: z.object({}).optional(), specification_hash: z.string().optional(), traces: z.array(z.array(z.object({timestamp: z.number().optional(), trace: z.object({}).optional()}))).optional()}); export const createApiApiV1WWIdSpacesSpaceIdAppsAIdRunsResponse = z.object({run: run.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 }; /** * Create an app run * * POST /api/v1/w/{wId}/spaces/{spaceId}/apps/{aId}/runs */ export const createApiApiV1WWIdSpacesSpaceIdAppsAIdRuns = async (args: CreateApiApiV1WWIdSpacesSpaceIdAppsAIdRunsArgs, options?: CreateApiApiV1WWIdSpacesSpaceIdAppsAIdRunsOptions) => { const {baseUrl = "https://dust.tt", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/w/${encodeURIComponent(String(args.wId))}/spaces/${encodeURIComponent(String(args.spaceId))}/apps/${encodeURIComponent(String(args.aId))}/runs${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(createApiApiV1WWIdSpacesSpaceIdAppsAIdRunsResponse, await res.json()) };