mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-05-18 20:08:19 +00:00
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:
@@ -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();
|
||||
|
||||
14
src/ai/ai-response-schema.ts
Normal file
14
src/ai/ai-response-schema.ts
Normal 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>;
|
||||
Reference in New Issue
Block a user