import {z} from 'zod' export type TextPart = {text: string, type?: 'text' | undefined}; export type DataPart = {data: unknown, type?: 'data' | undefined}; export type CreateTaskEventRequest = {content?: (TextPart | DataPart | null) | undefined, idempotency_key?: (string | null) | undefined, persist_message?: boolean | undefined}; export type CreateApiTasksTaskIdEventsArgs = {task_id: string, body: CreateTaskEventRequest}; export type CreateApiTasksTaskIdEventsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const textPart = z.object({text: z.string(), type: z.literal("text").optional()}); export const dataPart = z.object({data: z.unknown(), type: z.literal("data").optional()}); export const event = z.object({agent_id: z.string(), content: z.union([textPart, dataPart]).nullable().optional(), created_at: z.string().nullable().optional(), id: z.string(), sequence_id: z.number().int(), streamCapabilities: z.array(z.string()).nullable().optional(), task_id: 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 Event to Task * * POST /tasks/{task_id}/events */ export const createApiTasksTaskIdEvents = async (args: CreateApiTasksTaskIdEventsArgs, options: CreateApiTasksTaskIdEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/tasks/${encodeURIComponent(String(args.task_id))}/events${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(event, await res.json()) };