feat: enforce structured JSON output via AI SDK Output.object

- ai-response-schema.ts: Zod schema for { message, suggestions[] }
- ai-chat.controller.ts: Output.object({ schema }) on streamText
  forces the LLM to return valid JSON matching the schema instead
  of free-form prose. Supervisor mode excluded (uses tools, not schema).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 11:40:25 +05:30
parent e03b1e6235
commit ae360a183d
2 changed files with 17 additions and 1 deletions

View File

@@ -0,0 +1,14 @@
import { z } from 'zod';
export const aiResponseSchema = z.object({
message: z.string().describe('Conversational response text for the agent. Plain text, no markdown.'),
suggestions: z.array(z.object({
id: z.string().describe('Unique suggestion ID like s1, s2'),
type: z.enum(['upsell', 'crosssell', 'retention', 'operational']),
title: z.string().describe('Short title for the suggestion pill'),
script: z.string().describe('2-3 sentence script the agent can read aloud to the caller'),
priority: z.enum(['high', 'medium', 'low']),
})).describe('0-4 contextual suggestions based on business rules. Include on first response, update on subsequent.'),
});
export type AiResponse = z.infer<typeof aiResponseSchema>;