import {z} from 'zod' export type GetApiV1TicketsIdSuggestionsArgs = {id: number}; export type GetApiV1TicketsIdSuggestionsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const copilotSuggestionArticle = z.object({title: z.string().nullable().optional(), articleId: z.string().nullable().optional(), slug: z.string().nullable().optional(), language: z.string().nullable().optional(), deskId: z.string().nullable().optional(), updatedAt: z.string().nullable().optional()}); export const copilotSuggestionSources = z.object({articles: z.array(copilotSuggestionArticle).nullable().optional(), pastConversationUsed: z.boolean().optional(), pastSuggestionUsed: z.boolean().optional(), contactInfoUsed: z.boolean().optional()}); export const copilotSuggestionItem = z.object({title: z.string().nullable().optional(), text: z.string().nullable().optional(), confidence: z.number().int().optional(), sources: copilotSuggestionSources.optional(), suggestionId: z.number().int().nullable().optional(), feedback: z.string().nullable().optional()}); export const roomSuggestionsDto = z.object({suggestions: z.array(copilotSuggestionItem).nullable().optional(), generatedAt: z.string().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 }; /** * Get the current copilot suggestion snapshot for a ticket, proxied from * copilot-service. Ticket analog of `GET /v1/chat/rooms/{roomId}/suggestions`. * Returns the raw DTO — NOT wrapped in `ApiResponse` — to mirror the chat * suggestions endpoint and the shape the ticket FE hook consumes (reads * `.suggestions` off the body directly). * FAIL-OPEN: copilot-service being unconfigured, slow or down yields an empty * snapshot (200 with an empty list), never an error. * * GET /v1/tickets/{id}/suggestions * * Also accepts auth: API Key */ export const getApiV1TicketsIdSuggestions = async (args: GetApiV1TicketsIdSuggestionsArgs, options: GetApiV1TicketsIdSuggestionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tickets/${encodeURIComponent(String(args.id))}/suggestions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(roomSuggestionsDto, await res.json()) };