@@ -6,18 +6,27 @@ import {
66 CreateChatDiff ,
77 initContext ,
88} from "@/lib/chatHistory" ;
9- import { getPagesList , introSectionId , PagePath , SectionId } from "@/lib/docs" ;
10- import { DynamicMarkdownSection } from "@/(docs)/@docs/[lang]/[pageId]/pageContent" ;
11- import { ReplCommand , ReplOutput } from "@my-code/runtime/interface" ;
9+ import {
10+ DynamicMarkdownSectionSchema ,
11+ getPagesList ,
12+ introSectionId ,
13+ PagePathSchema ,
14+ SectionId ,
15+ } from "@/lib/docs" ;
16+ import {
17+ ReplCommandSchema ,
18+ ReplOutputSchema ,
19+ } from "@my-code/runtime/interface" ;
20+ import { z } from "zod" ;
1221
13- type ChatParams = {
14- path : PagePath ;
15- userQuestion : string ;
16- sectionContent : DynamicMarkdownSection [ ] ;
17- replOutputs : Record < string , ReplCommand [ ] > ;
18- files : Record < string , string > ;
19- execResults : Record < string , ReplOutput [ ] > ;
20- } ;
22+ const ChatParamsSchema = z . object ( {
23+ path : PagePathSchema ,
24+ userQuestion : z . string ( ) . min ( 1 ) ,
25+ sectionContent : z . array ( DynamicMarkdownSectionSchema ) ,
26+ replOutputs : z . record ( z . string ( ) , z . array ( ReplCommandSchema ) ) ,
27+ files : z . record ( z . string ( ) , z . string ( ) ) ,
28+ execResults : z . record ( z . string ( ) , z . array ( ReplOutputSchema ) ) ,
29+ } ) ;
2130
2231export type ChatStreamEvent =
2332 | { type : "chat" ; chatId : string ; sectionId : string }
@@ -31,9 +40,18 @@ export async function POST(request: NextRequest) {
3140 return new Response ( "Unauthorized" , { status : 401 } ) ;
3241 }
3342
34- const params = ( await request . json ( ) ) as ChatParams ;
35- const { path, userQuestion, sectionContent, replOutputs, files, execResults } =
36- params ;
43+ const parseResult = ChatParamsSchema . safeParse ( await request . json ( ) ) ;
44+ if ( ! parseResult . success ) {
45+ return new Response ( JSON . stringify ( parseResult . error ) , { status : 400 } ) ;
46+ }
47+ const {
48+ path,
49+ userQuestion,
50+ sectionContent,
51+ replOutputs,
52+ files,
53+ execResults,
54+ } = parseResult . data ;
3755
3856 const pagesList = await getPagesList ( ) ;
3957 const langName = pagesList . find ( ( lang ) => lang . id === path . lang ) ?. name ;
@@ -202,7 +220,7 @@ export async function POST(request: NextRequest) {
202220 prompt . join ( "\n" )
203221 ) ) {
204222 console . log ( "Received chunk:" , [ chunk ] ) ;
205-
223+
206224 fullText += chunk ;
207225
208226 if ( ! headerParsed ) {
@@ -294,9 +312,7 @@ export async function POST(request: NextRequest) {
294312 await addMessagesAndDiffs (
295313 chatId ,
296314 path ,
297- [
298- { role : "ai" , content : cleanMessage } ,
299- ] ,
315+ [ { role : "ai" , content : cleanMessage } ] ,
300316 diffRaw ,
301317 context
302318 ) ;
0 commit comments