import {z} from 'zod' export type GetApiV20ConversationsSessionIdArgs = {Accept?: ('application/json' | 'application/hal+json' | 'application/xml' | 'text/xml' | 'text/csv') | undefined, projectId?: string | undefined, sessionId: string, expertMode?: boolean | undefined}; export type GetApiV20ConversationsSessionIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV20ConversationsSessionIdResponse = z.object({items: z.array(z.object({projectId: z.string().min(24).max(24).optional(), projectName: z.string().optional(), inputId: z.string().optional(), sessionId: z.string().optional(), contactId: z.string().optional(), organisation: z.string().min(24).max(24).optional(), inputText: z.string().optional(), inputData: z.object({}).optional(), type: z.enum(["input", "output"]).optional(), source: z.enum(["user", "bot", "agent", "suggestion"]).optional(), flowName: z.string().optional(), flowReferenceId: z.string().optional(), channel: z.enum(["facebook", "alexa", "slack", "generic", "inject", "rest", "realtime", "socket", "adminconsole", "webchat2", "dialogflow", "twilio", "twilio-sms", "line", "intercom", "microsoftBotFramework", "microsoftTeams", "sunshineConversations", "admin-webchat", "avaya", "nonConversational", "voiceGateway2", "amazonLex", "workplace", "webhook", "abstractRest", "userlike", "ringCentralEngage", "audioCodes", "bandwidth", "whatsapp", "eightByEight", "genesysBotConnector", "niceCXOne", "agentAssistVoice", "webchat3", "niceCXOneAAH", "zoomContactCenter", "mcpServer"]).optional(), timestamp: z.object({}).optional(), inHandoverRequest: z.boolean().optional(), inHandoverConversation: z.boolean().optional(), outputId: z.string().optional(), expiresAt: z.object({}).optional(), endpointUrlToken: z.string().optional(), endpointName: z.string().optional(), localeReferenceId: z.string().optional(), localeName: z.string().optional(), snapshotId: z.string().min(24).max(24).optional(), snapshotName: z.string().optional(), rating: z.number().optional(), ratingComment: z.string().optional()})).optional(), total: z.number().int().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 a Conversation * * GET /v2.0/conversations/{sessionId} * * Also accepts auth: APIKeyQueryParam, CXoneTokenHeader, OAuth2 */ export const getApiV20ConversationsSessionId = async (args: GetApiV20ConversationsSessionIdArgs, options?: GetApiV20ConversationsSessionIdOptions) => { const {baseUrl = "https://api-trial.cognigy.ai/testing", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.expertMode != null) query.append("expertMode", String(args.expertMode)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v2.0/conversations/${encodeURIComponent(String(args.sessionId))}${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(getApiV20ConversationsSessionIdResponse, await res.json()) };