import {z} from 'zod' export type GetApiApiAnalyticsTeamSnapshotAgentDistributionArgs = {agentIds?: Array | undefined, pipelineId?: string | undefined, stageIds?: Array | undefined, hideArchived?: boolean | undefined, hideUnassigned: boolean, 'x-api-key': string}; export type GetApiApiAnalyticsTeamSnapshotAgentDistributionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiAnalyticsTeamSnapshotAgentDistributionResponse = z.object({agentDistribution: z.array(z.object({agent_id: z.string().optional(), agent_name: z.string().optional(), count: z.number().optional(), averageWaitingTime: z.number().optional()})).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 team member distribution of open conversations * * GET /api/analytics/team/snapshot/agent-distribution */ export const getApiApiAnalyticsTeamSnapshotAgentDistribution = async (args: GetApiApiAnalyticsTeamSnapshotAgentDistributionArgs, options?: GetApiApiAnalyticsTeamSnapshotAgentDistributionOptions) => { const {baseUrl = "https://api.vambe.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.agentIds ?? []) query.append("agentIds", String(item)) if (args.pipelineId != null) query.append("pipelineId", String(args.pipelineId)) for (const item of args.stageIds ?? []) query.append("stageIds", String(item)) if (args.hideArchived != null) query.append("hideArchived", String(args.hideArchived)) if (args.hideUnassigned != null) query.append("hideUnassigned", String(args.hideUnassigned)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/analytics/team/snapshot/agent-distribution${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(getApiApiAnalyticsTeamSnapshotAgentDistributionResponse, await res.json()) };