import {z} from 'zod' export type ApiSchemasAnnotationLikeDislikeConstraints = {annotation_type: 'like_dislike'}; export type ApiSchemasAnnotationStarConstraints = {annotation_type: 'star'}; export type ApiSchemasAnnotationScoreConstraints = {annotation_type: 'score', min: number, max: number}; export type ApiSchemasAnnotationTagsConstraints = {annotation_type: 'tags', tags: Array, allow_other?: boolean | undefined}; export type ApiSchemasAnnotationTextConstraints = {annotation_type: 'text'}; export type ApiSchemasAnnotationChoiceConstraints = {annotation_type: 'choice', choices: Array, allow_other?: boolean | undefined}; export type ApiSchemasAnnotationTreeChoiceNodeInput = {label: string, id: string, children?: Array | undefined}; export type ApiSchemasAnnotationTreeChoiceConstraints = {annotation_type: 'tree_choice', choices_tree?: (Array | null) | undefined, choices_tree_yaml?: (string | null) | undefined}; export type AnnotationTemplateCreate = {name: string, include_explanation?: boolean | undefined, criteria?: (string | null) | undefined, constraints: ApiSchemasAnnotationLikeDislikeConstraints | ApiSchemasAnnotationStarConstraints | ApiSchemasAnnotationScoreConstraints | ApiSchemasAnnotationTagsConstraints | ApiSchemasAnnotationTextConstraints | ApiSchemasAnnotationChoiceConstraints | ApiSchemasAnnotationTreeChoiceConstraints}; export type CreateApiPublicV2ProjectsProjectIdAnnotationTemplatesArgs = {project_id: unknown, body: AnnotationTemplateCreate}; export type CreateApiPublicV2ProjectsProjectIdAnnotationTemplatesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSchemasAnnotationLikeDislikeConstraints = z.object({annotation_type: z.literal("like_dislike")}); export const apiSchemasAnnotationStarConstraints = z.object({annotation_type: z.literal("star")}); export const apiSchemasAnnotationScoreConstraints = z.object({annotation_type: z.literal("score"), min: z.number().int().gte(0), max: z.number().int().gte(0)}); export const apiSchemasAnnotationTagsConstraints = z.object({annotation_type: z.literal("tags"), tags: z.array(z.string().min(1).max(255)), allow_other: z.boolean().optional()}); export const apiSchemasAnnotationTextConstraints = z.object({annotation_type: z.literal("text")}); export const apiSchemasAnnotationChoiceConstraints = z.object({annotation_type: z.literal("choice"), choices: z.array(z.string().min(1).max(255)), allow_other: z.boolean().optional()}); export const apiSchemasAnnotationTreeChoiceNodeOutput: z.ZodType = z.object({label: z.string().min(1).max(255), id: z.string().min(1).max(255), children: z.array(z.lazy(() => apiSchemasAnnotationTreeChoiceNodeOutput)).optional()}); export const treeChoiceDBConstraints = z.object({annotation_type: z.literal("tree_choice"), choices_tree: z.array(apiSchemasAnnotationTreeChoiceNodeOutput), choices_tree_yaml: z.string().min(1).max(10000)}); export const annotationTemplateDB = z.object({name: z.string().min(1).max(255), include_explanation: z.boolean(), criteria: z.string().min(1).nullable().optional(), constraints: z.discriminatedUnion("annotation_type", [apiSchemasAnnotationLikeDislikeConstraints, apiSchemasAnnotationStarConstraints, apiSchemasAnnotationScoreConstraints, apiSchemasAnnotationTagsConstraints, apiSchemasAnnotationTextConstraints, apiSchemasAnnotationChoiceConstraints, treeChoiceDBConstraints]), id: z.string(), created_at: z.string(), created_by: z.string().nullable(), position: z.number().int(), usage_count: z.number().int()}); 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 Annotation Template * * POST /public/v2/projects/{project_id}/annotation/templates * * Also accepts auth: ClassicAPIKeyHeader, OAuth2PasswordBearer, HTTPBasic */ export const createApiPublicV2ProjectsProjectIdAnnotationTemplates = async (args: CreateApiPublicV2ProjectsProjectIdAnnotationTemplatesArgs, options?: CreateApiPublicV2ProjectsProjectIdAnnotationTemplatesOptions) => { const {baseUrl = "https://api.galileo.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Splunk-AO-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/public/v2/projects/${encodeURIComponent(String(args.project_id))}/annotation/templates${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(annotationTemplateDB, await res.json()) };