import {z} from 'zod' export type SlackBotResponseType = 'quotes' | 'citations'; export type SlackBotConfigCreationRequest = {document_sets?: Array | undefined, persona_id?: number | undefined, channel_names: Array, respond_tag_only?: boolean | undefined, respond_to_bots?: boolean | undefined, respond_team_member_list?: Array | undefined, answer_filters?: (Array<'well_answered_postfilter' | 'questionmark_prefilter'>) | undefined, follow_up_tags?: Array | undefined, response_type: SlackBotResponseType}; export type CreateApiManageAdminSlackBotConfigArgs = {body: SlackBotConfigCreationRequest}; export type CreateApiManageAdminSlackBotConfigOptions = {baseUrl: string, APIKeyCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const starterMessage = z.object({name: z.string(), description: z.string(), message: z.string()}); export const promptSnapshot = z.object({id: z.number().int(), name: z.string(), shared: z.boolean(), description: z.string(), system_prompt: z.string(), task_prompt: z.string(), include_citations: z.boolean(), datetime_aware: z.boolean(), default_prompt: z.boolean()}); export const documentSource = z.enum(["ingestion_api", "slack", "web", "google_drive", "gmail", "requesttracker", "github", "gitlab", "guru", "bookstack", "confluence", "discourse", "slab", "salesforce", "jira", "productboard", "file", "notion", "zulip", "linear", "hubspot", "document360", "gong", "google_sites", "zendesk", "loopio", "sharepoint", "teams", "freshdesk"]); export const inputType = z.enum(["load_state", "poll", "event"]); export const connectorSnapshot = z.object({name: z.string(), source: documentSource, input_type: inputType, connector_specific_config: z.object({}), refresh_freq: z.number().int().optional(), disabled: z.boolean(), id: z.number().int(), credential_ids: z.array(z.number().int()), time_created: z.string(), time_updated: z.string()}); export const credentialSnapshot = z.object({credential_json: z.object({}), admin_public: z.boolean(), id: z.number().int(), user_id: z.string().optional(), time_created: z.string(), time_updated: z.string()}); export const connectorCredentialPairDescriptor = z.object({id: z.number().int(), name: z.string().optional(), connector: connectorSnapshot, credential: credentialSnapshot}); export const documentSet = z.object({id: z.number().int(), name: z.string(), description: z.string(), cc_pair_descriptors: z.array(connectorCredentialPairDescriptor), is_up_to_date: z.boolean(), contains_non_public: z.boolean()}); export const personaSnapshot = z.object({id: z.number().int(), name: z.string(), shared: z.boolean(), is_visible: z.boolean(), display_priority: z.number().int().optional(), description: z.string(), num_chunks: z.number().optional(), llm_relevance_filter: z.boolean(), llm_filter_extraction: z.boolean(), llm_model_version_override: z.string().optional(), starter_messages: z.array(starterMessage).optional(), default_persona: z.boolean(), prompts: z.array(promptSnapshot), document_sets: z.array(documentSet)}); export const channelConfig = z.object({channel_names: z.array(z.string()), respond_tag_only: z.boolean().optional(), respond_to_bots: z.boolean().optional(), respond_team_member_list: z.array(z.string()).optional(), answer_filters: z.array(z.union([z.literal("well_answered_postfilter"), z.literal("questionmark_prefilter")])).optional(), follow_up_tags: z.array(z.string()).optional()}); export const slackBotResponseType = z.enum(["quotes", "citations"]); export const slackBotConfig = z.object({id: z.number().int(), persona: personaSnapshot.optional(), channel_config: channelConfig, response_type: slackBotResponseType}); 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 }; /** * Create Slack Bot Config * * POST /manage/admin/slack-bot/config */ export const createApiManageAdminSlackBotConfig = async (args: CreateApiManageAdminSlackBotConfigArgs, options: CreateApiManageAdminSlackBotConfigOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyCookie !== undefined) cookies.push("fastapiusersauth=" + encodeURIComponent(APIKeyCookie)) const url = `${baseUrl.replace(/\/+$/, '')}/manage/admin/slack-bot/config${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(slackBotConfig, await res.json()) };