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;