import {z} from 'zod' export type SentimentV2ModelIdFilter = {field: 'model_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2RegionIdFilter = {field: 'region_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2TopicIdFilter = {field: 'topic_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2PromptIdFilter = {field: 'prompt_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2PersonaIdFilter = {field: 'persona_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2TagIdFilter = {field: 'tag_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2RunIdFilter = {field: 'run_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2ThemeIdFilter = {field: 'theme_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2ThemeFilter = {field: 'theme', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type SentimentV2ClaimIdFilter = {field: 'claim_id', operator: 'is' | 'not_is' | 'in' | 'not_in', value: string | Array}; export type SentimentV2ClaimFilter = {field: 'claim', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type SentimentV2SentimentFilter = {field: 'sentiment', operator: 'is' | 'not_is' | 'in' | 'not_in', value: 'positive' | 'negative' | Array<'positive' | 'negative'>}; export type Pagination = {limit?: number | undefined, offset?: number | undefined}; export type SentimentV2ReportQuery = {category_id: string, asset_name: string, start_date: string, end_date: string, comparison_start_date?: (string | null) | undefined, comparison_end_date?: (string | null) | undefined, date_interval?: ('hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'relative_week') | undefined, dimensions?: (Array<'date' | 'topic' | 'region' | 'model' | 'prompt' | 'persona' | 'tag' | 'theme' | 'claim' | 'run' | 'asset_name'>) | undefined, metrics: Array<'sentiment' | 'occurrence'>, filters?: (Array) | undefined, order_by?: ({[key: string]: 'asc' | 'desc'}) | undefined, pagination?: Pagination | undefined}; export type CreateApiV1ReportsSentimentV2Args = {body: SentimentV2ReportQuery}; export type CreateApiV1ReportsSentimentV2Options = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const sentimentV2ReportInfo = z.object({query: z.record(z.string(), z.unknown()), total_rows: z.number().int()}); export const sentimentScores = z.object({positive_sentiment: z.number(), negative_sentiment: z.number(), assessment_count: z.number().int(), occurrence: z.number().nullable().optional(), response_count: z.number().int().nullable().optional(), total_response_count: z.number().int().nullable().optional()}); export const sentimentPeriodScores = z.object({current: sentimentScores.nullable().optional(), previous: sentimentScores.nullable().optional()}); export const sentimentGroupMetadata = z.object({theme_id: z.string().nullable().optional(), theme: z.string().nullable().optional(), claim_id: z.string().nullable().optional(), claim: z.string().nullable().optional(), sentiment: z.enum(["positive", "negative"]).nullable().optional(), prompt_id: z.string().nullable().optional(), prompt_text: z.string().nullable().optional(), topic_id: z.string().nullable().optional(), run_id: z.string().nullable().optional(), created_at: z.string().nullable().optional(), model_id: z.string().nullable().optional(), region_id: z.string().nullable().optional(), persona_id: z.string().nullable().optional(), asset_name: z.string().nullable().optional(), child_count_total: z.number().int().nullable().optional(), child_count_matching: z.number().int().nullable().optional(), parent_matches_search: z.boolean().nullable().optional(), child_matches_search: z.boolean().nullable().optional()}); export const sentimentDataPoint = z.object({scores: sentimentPeriodScores, date: z.string().nullable().optional(), prev_date: z.string().nullable().optional(), group_ids: z.record(z.string(), z.string()).nullable().optional(), group_names: z.record(z.string(), z.string()).nullable().optional(), group_metadata: sentimentGroupMetadata.nullable().optional(), cited_website_hrefs: z.array(z.string()).optional(), total_count: z.number().int().nullable().optional()}); export const sentimentV2ReportResponse = z.object({info: sentimentV2ReportInfo, data: z.array(sentimentDataPoint).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 }; /** * Query Sentiment V2 * * POST /v1/reports/sentiment-v2 * * Also accepts auth: BearerAuth */ export const createApiV1ReportsSentimentV2 = async (args: CreateApiV1ReportsSentimentV2Args, options?: CreateApiV1ReportsSentimentV2Options) => { const {baseUrl = "https://api.tryprofound.com", 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("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reports/sentiment-v2${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(sentimentV2ReportResponse, await res.json()) };