Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: flatten AI generation schema for filters (#44092)

Ali Waseemaa12ae790ae8
diff --git a/apps/studio/lib/api/filterHelpers.test.ts b/apps/studio/lib/api/filterHelpers.test.ts
index b54d120e1cf..0272c3692fb 100644
--- a/apps/studio/lib/api/filterHelpers.test.ts
+++ b/apps/studio/lib/api/filterHelpers.test.ts
@@ -1,7 +1,6 @@
import { describe, expect, test } from 'vitest'
import {
- enforceAndLogicalOperator,
FilterGroupType,
isFilterGroup,
serializeOperators,
@@ -207,59 +206,3 @@ describe('validateFilterGroup', () => {
expect(validateFilterGroup(group, properties)).toBe(true)
})
})
-
-describe('enforceAndLogicalOperator', () => {
- test('converts OR to AND at root level', () => {
- const group: FilterGroupType = {
- logicalOperator: 'OR',
- conditions: [{ propertyName: 'name', operator: '=', value: 'test' }],
- }
- const result = enforceAndLogicalOperator(group)
- expect(result.logicalOperator).toBe('AND')
- })
-
- test('preserves conditions when converting', () => {
- const condition = { propertyName: 'name', operator: '=', value: 'test' }
- const group: FilterGroupType = {
- logicalOperator: 'OR',
- conditions: [condition],
- }
- const result = enforceAndLogicalOperator(group)
- expect(result.conditions).toEqual([condition])
- })
-
- test('recursively converts nested groups to AND', () => {
- const group: FilterGroupType = {
- logicalOperator: 'OR',
- conditions: [
- { propertyName: 'name', operator: '=', value: 'test' },
- {
- logicalOperator: 'OR',
- conditions: [
- { propertyName: 'age', operator: '>', value: 18 },
- {
- logicalOperator: 'OR',
- conditions: [{ propertyName: 'active', operator: '=', value: true }],
- },
- ],
- },
- ],
- }
- const result = enforceAndLogicalOperator(group)
-
- expect(result.logicalOperator).toBe('AND')
- expect((result.conditions[1] as FilterGroupType).logicalOperator).toBe('AND')
- expect(
- ((result.conditions[1] as FilterGroupType).conditions[1] as FilterGroupType).logicalOperator
- ).toBe('AND')
- })
-
- test('handles empty conditions array', () => {
- const group: FilterGroupType = {
- logicalOperator: 'OR',
- conditions: [],
- }
- const result = enforceAndLogicalOperator(group)
- expect(result).toEqual({ logicalOperator: 'AND', conditions: [] })
- })
-})
diff --git a/apps/studio/lib/api/filterHelpers.ts b/apps/studio/lib/api/filterHelpers.ts
index 668bd505a38..72e6ed88763 100644
--- a/apps/studio/lib/api/filterHelpers.ts
+++ b/apps/studio/lib/api/filterHelpers.ts
@@ -46,6 +46,11 @@ export const filterGroupSchema: z.ZodType<FilterGroupType> = z.lazy(() =>
})
)
+export const filterGroupSchemaForAI = z.object({
+ logicalOperator: z.literal('AND'),
+ conditions: z.array(filterConditionSchema),
+})
+
export const requestSchema = z.object({
prompt: z.string().min(1, 'Prompt is required'),
filterProperties: z
@@ -80,15 +85,6 @@ export function validateFilterGroup(
})
}
-export function enforceAndLogicalOperator(group: FilterGroupType): FilterGroupType {
- return {
- logicalOperator: 'AND',
- conditions: group.conditions.map((condition) =>
- isFilterGroup(condition) ? enforceAndLogicalOperator(condition) : condition
- ),
- }
-}
-
export function serializeOptions(
options?: z.infer<typeof filterPropertySchema>['options']
): string[] | undefined {
diff --git a/apps/studio/pages/api/ai/sql/filter-v1.ts b/apps/studio/pages/api/ai/sql/filter-v1.ts
index fd0ecb294b3..8a9f30cc019 100644
--- a/apps/studio/pages/api/ai/sql/filter-v1.ts
+++ b/apps/studio/pages/api/ai/sql/filter-v1.ts
@@ -3,8 +3,7 @@ import { source } from 'common-tags'
import { getModel } from 'lib/ai/model'
import apiWrapper from 'lib/api/apiWrapper'
import {
- enforceAndLogicalOperator,
- filterGroupSchema,
+ filterGroupSchemaForAI,
requestSchema,
serializeOperators,
serializeOptions,
@@ -65,7 +64,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
const result = await generateText({
model,
providerOptions,
- output: Output.object({ schema: filterGroupSchema }),
+ output: Output.object({ schema: filterGroupSchemaForAI }),
prompt: source`
You are an expert Postgres filter builder. Convert the user's request into structured filters.
@@ -74,7 +73,6 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
Rules:
- Use only the provided property names and operators for each property.
- - Prefer logical operator "AND" unless the user explicitly asks for "OR".
- When unsure, default to simple equality comparisons with reasonable values.
- Values should respect property types: booleans must be true/false, dates should be ISO date strings (YYYY-MM-DD), and numbers must be numbers.
- If options are provided for a property, choose from those values when appropriate.
@@ -92,7 +90,7 @@ export async function handlePost(req: NextApiRequest, res: NextApiResponse) {
})
}
- return res.json(enforceAndLogicalOperator(generatedFilters))
+ return res.json(generatedFilters)
} catch (error) {
if (error instanceof Error) {
console.error(`AI filter generation failed: ${error.message}`)