import {z} from 'zod' export type AiAgentModelConfig = {reasoning?: boolean | undefined, modelProvider: string, modelName: string}; export type DateGranularity = 'Second' | 'Minute' | 'Hour' | 'Day' | 'Week' | 'Month' | 'Quarter' | 'Year'; export type DateZoom = {xAxisFieldId?: string | undefined, granularity?: (DateGranularity | string) | undefined}; export type ParameterValue = string | number | Array | Array; export type ParametersValuesMap = {[key: string]: ParameterValue}; export type AnyType = unknown; export type FilterOperator = 'isNull' | 'notNull' | 'equals' | 'notEquals' | 'startsWith' | 'endsWith' | 'include' | 'doesNotInclude' | 'lessThan' | 'lessThanOrEqual' | 'greaterThan' | 'greaterThanOrEqual' | 'inThePast' | 'notInThePast' | 'inTheNext' | 'inTheCurrent' | 'notInTheCurrent' | 'inBetween' | 'notInBetween' | 'inPeriodToDate'; export type DimensionType = 'string' | 'number' | 'timestamp' | 'date' | 'boolean'; export type DashboardFieldTarget = {fallbackType?: DimensionType | undefined, isSqlColumn?: boolean | undefined, tableName: string, fieldId: string}; export type DashboardTileTarget = DashboardFieldTarget | false; export type DashboardTileTargets = {[key: string]: DashboardTileTarget}; export type DashboardFilterRule = {includeNull?: boolean | undefined, values?: Array | undefined, operator: FilterOperator, id: string, target: DashboardFieldTarget, settings?: AnyType | undefined, disabled?: boolean | undefined, required?: boolean | undefined, caseSensitive?: boolean | undefined, lockedTabUuids?: Array | undefined, requiredGroupId?: string | undefined, singleValue?: boolean | undefined, label?: string | undefined, tileTargets?: DashboardTileTargets | undefined} | {[key: string]: unknown}; export type DashboardFilters = {tableCalculations: Array, metrics: Array, dimensions: Array}; export type AiChartRuntimeOverrides = {dateZoom?: DateZoom | undefined, dashboardParameters?: ParametersValuesMap | undefined, dashboardFilters?: DashboardFilters | undefined}; export type PickDashboardFilterRuleExcludeKeyofDashboardFilterRuleIdOrTileTargetsOrLockedTabUuidsOrRequiredGroupId = {label?: string | undefined, target: DashboardFieldTarget, settings: unknown, disabled?: boolean | undefined, required?: boolean | undefined, caseSensitive?: boolean | undefined, operator: FilterOperator, values?: Array | undefined, includeNull?: boolean | undefined, singleValue?: boolean | undefined}; export type AiDashboardFilterRule = PickDashboardFilterRuleExcludeKeyofDashboardFilterRuleIdOrTileTargetsOrLockedTabUuidsOrRequiredGroupId; export type AiDashboardFilters = {tableCalculations: Array, metrics: Array, dimensions: Array}; export type AiDashboardActiveTab = {name: string, uuid: string}; export type AiDashboardRuntimeOverrides = {dateZoom?: (DateZoom | null) | undefined, dashboardParameters?: ParametersValuesMap | undefined, dashboardFilters?: AiDashboardFilters | undefined, activeTab?: AiDashboardActiveTab | undefined}; export type AiPromptContextItemInput = {runtimeOverrides?: AiChartRuntimeOverrides | undefined, chartSlug?: (string | null) | undefined, chartUuid: string, type: 'chart'} | {runtimeOverrides?: AiDashboardRuntimeOverrides | undefined, dashboardSlug?: (string | null) | undefined, dashboardUuid: string, type: 'dashboard'} | {promptUuid?: (string | null) | undefined, threadUuid: string, type: 'thread'} | {path: string, type: 'file'} | {fullName: string, type: 'repository'} | {prUrl: string, type: 'pull_request'} | {fingerprint: string, type: 'proposed_change'} | {fingerprint: string, type: 'review_finding'} | {previewProjectUuid: string, type: 'preview_environment'}; export type AiPromptContextInput = Array; export type ApiAiAgentThreadCreateRequest = {modelConfig?: AiAgentModelConfig | undefined, context?: AiPromptContextInput | undefined, prompt?: string | undefined}; export type CreateApiApiV1ProjectsProjectUuidAiAgentsAgentUuidThreadsArgs = {projectUuid: string, agentUuid: string, body: ApiAiAgentThreadCreateRequest}; export type CreateApiApiV1ProjectsProjectUuidAiAgentsAgentUuidThreadsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const aiAgentUser = z.object({name: z.string(), uuid: z.string()}); export const aiThreadCreatedFrom = z.enum(["slack", "web_app", "evals", "scheduler"]); export const aiAgentThreadSummary = z.object({user: aiAgentUser, firstMessage: z.object({message: z.string(), uuid: z.string()}), titleGeneratedAt: z.string().nullable(), title: z.string().nullable(), createdFrom: aiThreadCreatedFrom, createdAt: z.string(), agentUuid: z.string(), uuid: z.string()}); export const apiAiAgentThreadCreateResponse = z.object({results: aiAgentThreadSummary, status: z.literal("ok")}); 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 }; /** * POST /api/v1/projects/{projectUuid}/aiAgents/{agentUuid}/threads */ export const createApiApiV1ProjectsProjectUuidAiAgentsAgentUuidThreads = async (args: CreateApiApiV1ProjectsProjectUuidAiAgentsAgentUuidThreadsArgs, options: CreateApiApiV1ProjectsProjectUuidAiAgentsAgentUuidThreadsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/aiAgents/${encodeURIComponent(String(args.agentUuid))}/threads${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(apiAiAgentThreadCreateResponse, await res.json()) };