import {z} from 'zod' export type LLMObsPromptConfig = {[key: string]: unknown}; export type LLMObsPromptVersionLabel = 'production' | 'development'; export type LLMObsPromptTextTemplate = string; export type LLMObsPromptChatMessage = {content: string, role: string}; export type LLMObsPromptMessagePlaceholderType = 'placeholder'; export type LLMObsPromptMessagePlaceholder = {name: string, type: LLMObsPromptMessagePlaceholderType}; export type LLMObsPromptChatTemplateItem = LLMObsPromptChatMessage | LLMObsPromptMessagePlaceholder; export type LLMObsPromptChatTemplate = Array; export type LLMObsPromptInclude = {items?: Array | undefined, prompt_id: string, version: number}; export type LLMObsPromptIncludeItem = {include: LLMObsPromptInclude}; export type LLMObsPromptAuthoringItem = LLMObsPromptChatMessage | LLMObsPromptIncludeItem; export type LLMObsPromptAuthoringChatTemplate = Array; export type LLMObsPromptAuthoringMessagesTemplate = {messages: LLMObsPromptAuthoringChatTemplate}; export type LLMObsPromptTemplate = LLMObsPromptTextTemplate | LLMObsPromptChatTemplate | LLMObsPromptAuthoringMessagesTemplate; export type LLMObsCreatePromptDataAttributes = {config?: LLMObsPromptConfig | undefined, description?: string | undefined, env_ids?: Array | undefined, labels?: Array | undefined, prompt_id: string, template: LLMObsPromptTemplate, title?: string | undefined, user_version?: string | undefined}; export type LLMObsPromptType = 'prompt-templates'; export type LLMObsCreatePromptData = {attributes: LLMObsCreatePromptDataAttributes, type: LLMObsPromptType}; export type LLMObsCreatePromptRequest = {data: LLMObsCreatePromptData}; export type CreateApiApiV2LlmObsV1PromptsArgs = {body: LLMObsCreatePromptRequest}; export type CreateApiApiV2LlmObsV1PromptsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const lLMObsPromptConfig = z.record(z.string(), z.unknown()); export const lLMObsPromptDataset = z.object({id: z.string(), name: z.string().optional()}); export const lLMObsPromptResponseSource = z.enum(["registry", "code"]); export const lLMObsCreatePromptResponseDataAttributes = z.object({author: z.string().optional(), config: lLMObsPromptConfig.optional(), created_at: z.string().optional(), created_from: z.string(), datasets: z.array(lLMObsPromptDataset).optional(), description: z.string().optional(), extracted_from: z.string().optional(), in_registry: z.boolean(), last_seen_at: z.string().optional(), last_version_created_at: z.string().optional(), ml_app: z.string().optional(), ml_apps: z.array(z.string()).optional(), num_versions: z.number().int(), prompt_id: z.string(), source: lLMObsPromptResponseSource, tags: z.array(z.string()).optional(), title: z.string().optional()}); export const lLMObsPromptType = z.literal("prompt-templates"); export const lLMObsCreatePromptResponseData = z.object({attributes: lLMObsCreatePromptResponseDataAttributes, id: z.string(), type: lLMObsPromptType}); export const lLMObsCreatePromptResponse = z.object({data: lLMObsCreatePromptResponseData}); 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 an Agent Observability prompt * * POST /api/v2/llm-obs/v1/prompts * * Also accepts auth: AuthZ */ export const createApiApiV2LlmObsV1Prompts = async (args: CreateApiApiV2LlmObsV1PromptsArgs, options?: CreateApiApiV2LlmObsV1PromptsOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = 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("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/llm-obs/v1/prompts${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(lLMObsCreatePromptResponse, await res.json()) };