import {z} from 'zod' export type SpanContext = {trace_id: string, span_id: string, trace_state: string}; export type SpanKind = 'SpanKind.CLIENT' | 'SpanKind.CONSUMER' | 'SpanKind.INTERNAL' | 'SpanKind.PRODUCER' | 'SpanKind.SERVER'; export type StatusCode = 'StatusCode.ERROR' | 'StatusCode.OK' | 'StatusCode.UNSET'; export type SpanStatus = {status_code: StatusCode, description?: (string | null) | undefined}; export type SpanResource = {attributes: {[key: string]: string}, schema_url: string}; export type WebAnnotation = {type: 'url_citation', title: string, url: string, start_index: number, end_index: number, cited_text?: (string | null) | undefined, encrypted_index?: (string | null) | undefined}; export type FileAnnotation = {type: 'file_citation', index: number, file_id: string, filename: string}; export type MapAnnotation = {type: 'map_citation', title: string, url: string, place_id?: (string | null) | undefined, start_index: number, end_index: number, cited_text?: (string | null) | undefined}; export type ContainerFileAnnotation = {type: 'container_file_citation', container_id: string, start_index?: (number | null) | undefined, end_index?: (number | null) | undefined, filename?: (string | null) | undefined, file_id?: (string | null) | undefined}; export type TextContent = {type?: 'text' | undefined, text: string, id?: (string | null) | undefined, annotations?: (Array | null) | undefined, thought_signature?: (string | null) | undefined}; export type ThinkingContent = {signature?: (string | null) | undefined, type?: 'thinking' | undefined, thinking: string, id?: (string | null) | undefined}; export type CodeContent = {type?: 'code' | undefined, code: string, id?: (string | null) | undefined, container_id?: (string | null) | undefined}; export type ImageURL = {url: string, detail?: (string | null) | undefined}; export type ImageContent = {type?: 'image_url' | undefined, image_url: ImageURL, image_variable?: (string | null) | undefined}; export type Media = {title?: string | undefined, type?: (string | null) | undefined, url?: (string | null) | undefined, format?: ('base64' | 'url' | 'neither') | undefined}; export type MediaContent = {type?: 'media' | undefined, media: Media}; export type MediaVariable = {type?: 'media_variable' | undefined, name: string}; export type OutputMediaContent = {type?: 'output_media' | undefined, id?: (string | null) | undefined, url: string, mime_type?: string | undefined, media_type?: ('image' | 'video' | 'audio') | undefined, provider_metadata?: (Record | null) | undefined}; export type ServerToolUseContent = {type?: 'server_tool_use' | undefined, id: string, name: string, input?: Record | undefined}; export type WebSearchResult = {type?: 'web_search_result' | undefined, url?: string | undefined, title?: string | undefined, encrypted_content?: string | undefined, page_age?: (string | null) | undefined}; export type WebSearchToolResultContent = {type?: 'web_search_tool_result' | undefined, tool_use_id: string, content?: Array | undefined}; export type CodeExecutionResultContent = {type?: 'code_execution_result' | undefined, output: string, outcome?: string | undefined}; export type McpListToolsContent = {type?: 'mcp_list_tools' | undefined, id?: (string | null) | undefined, server_label?: string | undefined, tools?: Array> | undefined, error?: (string | Record | null) | undefined}; export type McpCallContent = {type?: 'mcp_call' | undefined, id?: (string | null) | undefined, name?: string | undefined, server_label?: string | undefined, arguments?: string | undefined, output?: (string | null) | undefined, error?: (string | Record | null) | undefined, approval_request_id?: (string | null) | undefined}; export type McpApprovalRequestContent = {type?: 'mcp_approval_request' | undefined, id?: (string | null) | undefined, name?: string | undefined, arguments?: string | undefined, server_label?: string | undefined}; export type McpApprovalResponseContent = {type?: 'mcp_approval_response' | undefined, approval_request_id: string, approve: boolean}; export type BashCodeExecutionToolResultContent = {type?: 'bash_code_execution_tool_result' | undefined, tool_use_id: string, content?: Record | undefined}; export type TextEditorCodeExecutionToolResultContent = {type?: 'text_editor_code_execution_tool_result' | undefined, tool_use_id: string, content?: Record | undefined}; export type ShellCallContent = {type?: 'shell_call' | undefined, id?: (string | null) | undefined, call_id?: (string | null) | undefined, action?: Record | undefined, status?: (string | null) | undefined}; export type ShellCallOutputContent = {type?: 'shell_call_output' | undefined, id?: (string | null) | undefined, call_id?: (string | null) | undefined, output?: Array> | undefined, status?: (string | null) | undefined}; export type ApplyPatchCallContent = {type?: 'apply_patch_call' | undefined, id?: (string | null) | undefined, call_id?: (string | null) | undefined, operation?: Record | undefined, status?: (string | null) | undefined}; export type ApplyPatchCallOutputContent = {type?: 'apply_patch_call_output' | undefined, id?: (string | null) | undefined, call_id?: (string | null) | undefined, output?: (string | null) | undefined, status?: (string | null) | undefined}; export type CompletionPrompt = {content: Array, input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, type?: 'completion' | undefined} | {[key: string]: unknown}; export type SystemMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content: Array, role?: 'system' | undefined, name?: (string | null) | undefined}; export type UserMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content: Array, role?: 'user' | undefined, name?: (string | null) | undefined}; export type FunctionCall = {name: string, arguments: string}; export type ToolCall = {tool_id?: (string | null) | undefined, id: string, type?: 'function' | undefined, function: FunctionCall}; export type AssistantMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content?: (Array | null) | undefined, role?: 'assistant' | undefined, function_call?: (FunctionCall | null) | undefined, name?: (string | null) | undefined, tool_calls?: (Array | null) | undefined}; export type FunctionMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content?: (Array | null) | undefined, role?: 'function' | undefined, name: string}; export type ToolMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content: Array, role?: 'tool' | undefined, tool_call_id: string, name?: (string | null) | undefined}; export type PlaceholderMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content?: (Array | null) | undefined, raw_request_display_role?: string | undefined, role?: 'placeholder' | undefined, name: string}; export type DeveloperMessage = {input_variables?: Array | undefined, template_format?: ('f-string' | 'jinja2') | undefined, content: Array, role?: 'developer' | undefined, name?: (string | null) | undefined}; export type Function = {name: string, description?: string | undefined, strict?: boolean | undefined, parameters?: Record | undefined}; export type FunctionTool = {type?: 'function' | undefined, function: Function}; export type BuiltInTool = {id: string, name: string, description: string, provider: string, type: 'web_search' | 'file_search' | 'code_interpreter' | 'image_generation' | 'google_maps' | 'url_context' | 'mcp' | 'bash' | 'shell' | 'apply_patch' | 'text_editor', config: Record}; export type Tool = FunctionTool | BuiltInTool; export type MessageFunctionCall = {name: string}; export type ChatToolChoice = {type?: 'function' | undefined, function: MessageFunctionCall}; export type ChatPrompt = {messages: Array, functions?: (Array | null) | undefined, tools?: (Array | null) | undefined, function_call?: (string | MessageFunctionCall | null) | undefined, tool_choice?: (string | ChatToolChoice | null) | undefined, type?: 'chat' | undefined, input_variables?: Array | undefined}; export type LogRequest = {provider: string, model: string, input: CompletionPrompt | ChatPrompt, output: CompletionPrompt | ChatPrompt, request_start_time: string, request_end_time: string, parameters?: Record | undefined, tags?: Array | undefined, metadata?: {[key: string]: string} | undefined, prompt_name?: (string | null) | undefined, prompt_id?: (number | null) | undefined, prompt_version_number?: (number | null) | undefined, prompt_input_variables?: Record | undefined, input_tokens?: number | undefined, output_tokens?: number | undefined, price?: number | undefined, function_name?: string | undefined, score?: number | undefined, api_type?: (string | null) | undefined, status?: ('SUCCESS' | 'WARNING' | 'ERROR') | undefined, error_type?: ('PROVIDER_TIMEOUT' | 'PROVIDER_QUOTA_LIMIT' | 'PROVIDER_RATE_LIMIT' | 'PROVIDER_PARTIAL_RESPONSE' | 'PROVIDER_AUTH_ERROR' | 'PROVIDER_ERROR' | 'TEMPLATE_RENDER_ERROR' | 'VARIABLE_MISSING_OR_EMPTY' | 'UNKNOWN_ERROR' | null) | undefined, error_message?: (string | null) | undefined}; export type Span = {name: string, context: SpanContext, kind: SpanKind, parent_id?: (string | null) | undefined, start_time: number, end_time: number, status: SpanStatus, attributes: Record, events?: Array> | undefined, links?: Array> | undefined, resource: SpanResource, log_request?: (LogRequest | null) | undefined}; export type CreateSpansBulk = {spans: Array, close_after?: boolean | undefined}; export type CreateApiSpansBulkArgs = {body: CreateSpansBulk}; export type CreateApiSpansBulkOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createSpansBulkResponse = z.object({success: z.boolean(), spans: z.array(z.object({})), request_logs: z.array(z.object({})).nullable().optional(), rejected_span_ids: z.array(z.string()).nullable().optional(), rejected_trace_ids: z.array(z.string()).nullable().optional(), rejection_reason: z.literal("trace_closed").nullable().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 Spans Bulk * * POST /spans-bulk */ export const createApiSpansBulk = async (args: CreateApiSpansBulkArgs, options: CreateApiSpansBulkOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/spans-bulk${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(createSpansBulkResponse, await res.json()) };