import {z} from 'zod' export type SpanItemV3UpdateSchema = Record; export type UpdateApiSpanIdArgs = {id: string, body: {data: SpanItemV3UpdateSchema}}; export type UpdateApiSpanIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const spanItemV3ReadSchema = z.object({projectId: z.string(), primaryEntityId: z.string(), primaryEntityType: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), startTimeUnixNano: z.string().optional(), durationUnixNano: z.number(), endTimeUnixNano: z.string().optional(), traceId: z.string(), spanId: z.string(), sessionId: z.string().optional(), parentSpanId: z.string().optional(), traceState: z.string().optional(), attributes: z.string().optional(), attributeKeys: z.array(z.string()).optional(), entityKeys: z.array(z.string()).optional(), serviceEntityKey: z.string().optional(), hostEntityKey: z.string().optional(), k8sPodEntityKey: z.string().optional(), k8sNodeEntityKey: z.string().optional(), k8sClusterEntityKey: z.string().optional(), containerEntityKey: z.string().optional(), events: z.array(z.object({})).optional(), links: z.object({}).optional(), statusCode: z.number().optional(), statusMessage: z.string().optional(), name: z.string().optional(), kind: z.string().optional(), hasException: z.boolean().optional(), isRootSpan: z.boolean().optional(), isEntrySpan: z.boolean().optional(), isLlmSpan: z.boolean().optional(), llmSystem: z.string().optional(), llmOperation: z.string().optional(), llmRequestModel: z.string().optional(), llmResponseModel: z.string().optional(), llmAgentName: z.string().optional(), llmToolName: z.string().optional(), llmInputTokens: z.number().optional(), llmOutputTokens: z.number().optional(), llmTotalTokens: z.number().optional(), llmCost: z.number().optional(), llmConversationId: z.string().optional(), llmUserId: z.string().optional(), llmUserEmail: z.string().optional(), llmTeam: z.string().optional(), llmCallKind: z.string().optional(), llmIssues: z.array(z.string()).optional(), llmUserMessagePreview: z.string().optional()}); export const updateApiSpanIdResponse = z.object({data: spanItemV3ReadSchema.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 }; /** * Update Span * * PUT /span/{id} */ export const updateApiSpanId = async (args: UpdateApiSpanIdArgs, options?: UpdateApiSpanIdOptions) => { const {baseUrl = "https://oneuptime.com/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("APIKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/span/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiSpanIdResponse, await res.json()) };