import {z} from 'zod' export type ToolCallData = {name: string, description: string, inputParameters?: {[key: string]: unknown} | undefined, output?: string | undefined, reasoning?: string | undefined}; export type BaseSpan = {uuid: string, name: string, input?: string | undefined, output?: string | undefined, error?: string | undefined, status?: ('SUCCESS' | 'ERRORED') | undefined, startTime: string, endTime: string, parentUuid?: string | undefined, metadata?: {[key: string]: unknown} | undefined, metricCollection?: string | undefined, type?: string | undefined, retrievalContext?: Array | undefined, context?: Array | undefined, expectedOutput?: string | undefined, toolsCalled?: Array | undefined, expectedTools?: Array | undefined, integration?: string | undefined}; export type LlmSpan = {uuid?: string | undefined, name?: string | undefined, input?: string | undefined, output?: string | undefined, error?: string | undefined, status?: ('SUCCESS' | 'ERRORED') | undefined, startTime?: string | undefined, endTime?: string | undefined, parentUuid?: string | undefined, metadata?: {[key: string]: unknown} | undefined, metricCollection?: string | undefined, type?: string | undefined, retrievalContext?: Array | undefined, context?: Array | undefined, expectedOutput?: string | undefined, toolsCalled?: Array | undefined, expectedTools?: Array | undefined, model: string, provider?: string | undefined, integration?: string | undefined, promptAlias?: string | undefined, promptCommitHash?: string | undefined, promptLabel?: string | undefined, promptVersion?: string | undefined, costPerInputToken?: number | undefined, costPerOutputToken?: number | undefined, inputTokenCount?: number | undefined, outputTokenCount?: number | undefined}; export type RetrieverSpan = {uuid?: string | undefined, name?: string | undefined, input?: string | undefined, output?: string | undefined, error?: string | undefined, status?: ('SUCCESS' | 'ERRORED') | undefined, startTime?: string | undefined, endTime?: string | undefined, parentUuid?: string | undefined, metadata?: {[key: string]: unknown} | undefined, metricCollection?: string | undefined, type?: string | undefined, retrievalContext?: Array | undefined, context?: Array | undefined, expectedOutput?: string | undefined, integration?: string | undefined, toolsCalled?: Array | undefined, expectedTools?: Array | undefined, embedder: string, topK?: number | undefined, chunkSize?: number | undefined}; export type ToolSpan = {uuid?: string | undefined, name?: string | undefined, input?: string | undefined, output?: string | undefined, error?: string | undefined, integration?: string | undefined, status?: ('SUCCESS' | 'ERRORED') | undefined, startTime?: string | undefined, endTime?: string | undefined, parentUuid?: string | undefined, metadata?: {[key: string]: unknown} | undefined, metricCollection?: string | undefined, type?: string | undefined, retrievalContext?: Array | undefined, context?: Array | undefined, expectedOutput?: string | undefined, toolsCalled?: Array | undefined, expectedTools?: Array | undefined, description?: string | undefined}; export type AgentSpan = {uuid?: string | undefined, name?: string | undefined, input?: string | undefined, output?: string | undefined, integration?: string | undefined, error?: string | undefined, status?: ('SUCCESS' | 'ERRORED') | undefined, startTime?: string | undefined, endTime?: string | undefined, parentUuid?: string | undefined, metadata?: {[key: string]: unknown} | undefined, metricCollection?: string | undefined, type?: string | undefined, retrievalContext?: Array | undefined, context?: Array | undefined, expectedOutput?: string | undefined, toolsCalled?: Array | undefined, expectedTools?: Array | undefined, availableTools?: Array | undefined, agentHandoffs?: Array | undefined}; export type TraceAttachment = {url?: string | undefined, dataBase64?: string | undefined, mimeType?: string | undefined}; export type Trace = {uuid: string, name?: string | undefined, input?: string | undefined, output?: string | undefined, startTime: string, endTime: string, environment?: ('production' | 'development' | 'staging' | 'testing') | undefined, metadata?: {[key: string]: unknown} | undefined, tags?: Array | undefined, spans?: (Array) | undefined, threadId?: string | undefined, thread?: ({id?: string | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, tags?: (Array | null) | undefined}) | undefined, userId?: string | undefined, metricCollection?: string | undefined, testRunId?: string | undefined, retrievalContext?: Array | undefined, context?: Array | undefined, expectedOutput?: string | undefined, toolsCalled?: Array | undefined, expectedTools?: Array | undefined, attachments?: {[key: string]: TraceAttachment} | undefined}; export type CreateApiV1TracesArgs = {body: Trace}; export type CreateApiV1TracesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createTraceResponse = z.object({success: z.boolean(), data: z.object({id: z.string().optional()}), link: z.string(), deprecated: z.boolean()}); 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 }; /** * Trace Ingestion * * POST /v1/traces */ export const createApiV1Traces = async (args: CreateApiV1TracesArgs, options?: CreateApiV1TracesOptions) => { const {baseUrl = "https://api.confident-ai.com", 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(/\/+$/, '')}/v1/traces${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(createTraceResponse, await res.json()) };