import {z} from 'zod' export type GetApiApiCopilotTicketsTicketIdThreadArgs = {ticketId: number, accountId?: number | undefined, departmentId?: number | undefined}; export type GetApiApiCopilotTicketsTicketIdThreadOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ticketThreadMessageDto = z.object({author: z.string().nullable().optional(), text: z.string().nullable().optional(), at: z.string().nullable().optional()}); export const ticketThreadDto = z.object({subject: z.string().nullable().optional(), accountName: z.string().nullable().optional(), departmentName: z.string().nullable().optional(), messages: z.array(ticketThreadMessageDto).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 }; /** * Returns the ticket's conversation thread ({subject, messages[]}, oldest-first) for * the AI summary-service. Same shared-secret auth as the config endpoint (class-level * attribute; the absolute route below only overrides the class route TEMPLATE, not the * auth filter). Hard caps enforced by Infoset.Service.Hubs.Ticket.Service.ITicketThreadService: private notes * excluded, last 50 logs, 2000 chars per message, 24000 chars total. A ticket that does * not exist in the account — including a foreign-tenant ticket id, since every query is * ACCOUNT_ID-scoped — yields 404. * * GET /api/copilot/tickets/{ticketId}/thread * * Also accepts auth: API Key */ export const getApiApiCopilotTicketsTicketIdThread = async (args: GetApiApiCopilotTicketsTicketIdThreadArgs, options: GetApiApiCopilotTicketsTicketIdThreadOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.departmentId != null) query.append("departmentId", String(args.departmentId)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/copilot/tickets/${encodeURIComponent(String(args.ticketId))}/thread${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(ticketThreadDto, await res.json()) };