From ae360a183dbb2eb50d2c6dd41ffdd9607722af40 Mon Sep 17 00:00:00 2001 From: saridsa2 Date: Fri, 17 Apr 2026 11:40:25 +0530 Subject: [PATCH] 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) --- src/ai/ai-chat.controller.ts | 4 +++- src/ai/ai-response-schema.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/ai/ai-response-schema.ts diff --git a/src/ai/ai-chat.controller.ts b/src/ai/ai-chat.controller.ts index de65de4..9b4fce3 100644 --- a/src/ai/ai-chat.controller.ts +++ b/src/ai/ai-chat.controller.ts @@ -1,8 +1,9 @@ import { Controller, Post, Body, Headers, Req, Res, HttpException, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import type { Request, Response } from 'express'; -import { generateText, streamText, tool, stepCountIs } from 'ai'; +import { generateText, streamText, Output, tool, stepCountIs } from 'ai'; import type { LanguageModel } from 'ai'; +import { aiResponseSchema } from './ai-response-schema'; import { z } from 'zod'; import { PlatformGraphqlService } from '../platform/platform-graphql.service'; import { CallerResolutionService } from '../caller/caller-resolution.service'; @@ -629,6 +630,7 @@ export class AiChatController { messages, stopWhen: stepCountIs(5), tools: isSupervisor ? supervisorTools : agentTools, + ...(isSupervisor ? {} : { output: Output.object({ schema: aiResponseSchema }) }), }); const response = result.toTextStreamResponse(); diff --git a/src/ai/ai-response-schema.ts b/src/ai/ai-response-schema.ts new file mode 100644 index 0000000..e576321 --- /dev/null +++ b/src/ai/ai-response-schema.ts @@ -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;