import { useMutation, UseMutationOptions } from '@tanstack/react-query' import toast from 'react-hot-toast' import { isResponseOk, post } from 'lib/common/fetch' import { BASE_PATH } from 'lib/constants' import type { ResponseError } from 'types' export type SqlDebugResponse = { solution: string sql: string } export type SqlDebugVariables = { errorMessage: string sql: string entityDefinitions?: string[] } export async function debugSql({ errorMessage, sql, entityDefinitions }: SqlDebugVariables) { const response = await post(BASE_PATH + '/api/ai/sql/debug', { errorMessage, sql, entityDefinitions, }) if (!isResponseOk(response)) { throw response.error } return response } type SqlDebugData = Awaited> export const useSqlDebugMutation = ({ onSuccess, onError, ...options }: Omit, 'mutationFn'> = {}) => { return useMutation((vars) => debugSql(vars), { async onSuccess(data, variables, context) { await onSuccess?.(data, variables, context) }, async onError(data, variables, context) { if (onError === undefined) { toast.error(`Failed to debug SQL: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }