import {z} from 'zod' export type AppendPublicApiTaskMessageRequestDto = {content?: string | undefined, attachments?: Array<{uploadId: string}> | undefined, model?: ({slug: string, fast?: boolean | undefined, effort?: ('low' | 'medium' | 'high' | 'xhigh' | 'max' | null) | undefined}) | undefined}; export type CreateApiV1TasksTaskIdMessagesArgs = {'Idempotency-Key': string, taskId: string, body: AppendPublicApiTaskMessageRequestDto}; export type CreateApiV1TasksTaskIdMessagesOptions = {baseUrl?: string | undefined, personalApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const appendPublicApiTaskMessageResponseDto = z.object({accepted: z.literal(true), task: z.object({id: z.string(), title: z.string(), status: z.enum(["idle", "queued", "running", "awaiting_input", "completed", "cancelled", "failed"]), executionMode: z.enum(["adam", "external"]).optional(), projectId: z.string().nullable(), model: z.object({slug: z.string().min(1).max(64).nullable(), fast: z.boolean(), effort: z.enum(["low", "medium", "high", "xhigh", "max"]).nullable()}).nullable(), pendingInteraction: z.object({id: z.string(), type: z.enum(["question", "approval"]), prompt: z.string().nullable()}).nullable(), createdAt: z.string(), updatedAt: z.string()})}); 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 }; /** * Send a message to a task * * POST /v1/tasks/{taskId}/messages * * Also accepts auth: adamId */ export const createApiV1TasksTaskIdMessages = async (args: CreateApiV1TasksTaskIdMessagesArgs, options?: CreateApiV1TasksTaskIdMessagesOptions) => { const {baseUrl = "https://api.adam.new", fetch: fetchFn = fetch, personalApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (personalApiKey !== undefined) headers.set('authorization', `Bearer ${personalApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tasks/${encodeURIComponent(String(args.taskId))}/messages${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(appendPublicApiTaskMessageResponseDto, await res.json()) };