import {z} from 'zod' export type DeveloperMessage = {id: string, role?: 'developer' | undefined, content: string, name?: (string | null) | undefined, encryptedValue?: (string | null) | undefined} | {[key: string]: unknown}; export type SystemMessage = {id: string, role?: 'system' | undefined, content: string, name?: (string | null) | undefined, encryptedValue?: (string | null) | undefined} | {[key: string]: unknown}; export type FunctionCall = {name: string, arguments: string} | {[key: string]: unknown}; export type ToolCall = {id: string, type?: 'function' | undefined, function: FunctionCall, encryptedValue?: (string | null) | undefined} | {[key: string]: unknown}; export type AssistantMessage = {id: string, role?: 'assistant' | undefined, content?: (string | null) | undefined, name?: (string | null) | undefined, encryptedValue?: (string | null) | undefined, toolCalls?: (Array | null) | undefined} | {[key: string]: unknown}; export type TextInputContent = {type?: 'text' | undefined, text: string} | {[key: string]: unknown}; export type InputContentDataSource = {type?: 'data' | undefined, value: string, mimeType: string} | {[key: string]: unknown}; export type InputContentUrlSource = {type?: 'url' | undefined, value: string, mimeType?: (string | null) | undefined} | {[key: string]: unknown}; export type ImageInputContent = {type?: 'image' | undefined, source: InputContentDataSource | InputContentUrlSource, metadata: unknown} | {[key: string]: unknown}; export type AudioInputContent = {type?: 'audio' | undefined, source: InputContentDataSource | InputContentUrlSource, metadata: unknown} | {[key: string]: unknown}; export type VideoInputContent = {type?: 'video' | undefined, source: InputContentDataSource | InputContentUrlSource, metadata: unknown} | {[key: string]: unknown}; export type DocumentInputContent = {type?: 'document' | undefined, source: InputContentDataSource | InputContentUrlSource, metadata: unknown} | {[key: string]: unknown}; export type BinaryInputContent = {type?: 'binary' | undefined, mimeType: string, id?: (string | null) | undefined, url?: (string | null) | undefined, data?: (string | null) | undefined, filename?: (string | null) | undefined} | {[key: string]: unknown}; export type UserMessage = {id: string, role?: 'user' | undefined, content: string | Array, name?: (string | null) | undefined, encryptedValue?: (string | null) | undefined} | {[key: string]: unknown}; export type ToolMessage = {id: string, role?: 'tool' | undefined, content: string, toolCallId: string, error?: (string | null) | undefined, encryptedValue?: (string | null) | undefined} | {[key: string]: unknown}; export type ActivityMessage = {id: string, role?: 'activity' | undefined, activityType: string, content: {[key: string]: unknown}} | {[key: string]: unknown}; export type ReasoningMessage = {id: string, role?: 'reasoning' | undefined, content: string, encryptedValue?: (string | null) | undefined} | {[key: string]: unknown}; export type Tool = {name: string, description: string, parameters: unknown} | {[key: string]: unknown}; export type Context = {description: string, value: string} | {[key: string]: unknown}; export type ResumeEntry = {interruptId: string, status: 'resolved' | 'cancelled', payload: unknown} | {[key: string]: unknown}; export type RunAgentInput = {threadId: string, runId: string, parentRunId?: (string | null) | undefined, state: unknown, messages: Array, tools: Array, context: Array, forwardedProps: unknown, resume?: (Array | null) | undefined} | {[key: string]: unknown}; export type CreateApiAguiArgs = {body: RunAgentInput}; export type CreateApiAguiOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiAguiResponse = z.unknown(); 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 }; /** * Run Agent * * POST /agui */ export const createApiAgui = async (args: CreateApiAguiArgs, options: CreateApiAguiOptions) => { 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(/\/+$/, '')}/agui${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(createApiAguiResponse, await res.json()) };