11import { type NextRequest , NextResponse } from 'next/server'
22import { z } from 'zod'
33import { getSession } from '@/lib/auth'
4- import { createChat , deleteChat , generateChatTitle , getChat , listChats , sendMessage , updateChat } from '@/lib/copilot/service'
4+ import {
5+ createChat ,
6+ deleteChat ,
7+ generateChatTitle ,
8+ getChat ,
9+ listChats ,
10+ sendMessage ,
11+ updateChat ,
12+ } from '@/lib/copilot/service'
513import { createLogger } from '@/lib/logs/console-logger'
614
715const logger = createLogger ( 'CopilotAPI' )
@@ -37,18 +45,26 @@ const CreateChatSchema = z.object({
3745// Schema for updating chats
3846const UpdateChatSchema = z . object ( {
3947 chatId : z . string ( ) . min ( 1 , 'Chat ID is required' ) ,
40- messages : z . array ( z . object ( {
41- id : z . string ( ) ,
42- role : z . enum ( [ 'user' , 'assistant' , 'system' ] ) ,
43- content : z . string ( ) ,
44- timestamp : z . string ( ) ,
45- citations : z . array ( z . object ( {
46- id : z . number ( ) ,
47- title : z . string ( ) ,
48- url : z . string ( ) ,
49- similarity : z . number ( ) . optional ( ) ,
50- } ) ) . optional ( ) ,
51- } ) ) . optional ( ) ,
48+ messages : z
49+ . array (
50+ z . object ( {
51+ id : z . string ( ) ,
52+ role : z . enum ( [ 'user' , 'assistant' , 'system' ] ) ,
53+ content : z . string ( ) ,
54+ timestamp : z . string ( ) ,
55+ citations : z
56+ . array (
57+ z . object ( {
58+ id : z . number ( ) ,
59+ title : z . string ( ) ,
60+ url : z . string ( ) ,
61+ similarity : z . number ( ) . optional ( ) ,
62+ } )
63+ )
64+ . optional ( ) ,
65+ } )
66+ )
67+ . optional ( ) ,
5268 title : z . string ( ) . optional ( ) ,
5369} )
5470
@@ -120,23 +136,23 @@ export async function POST(req: NextRequest) {
120136 // Handle StreamingExecution (from providers with tool calls)
121137 logger . info ( `[${ requestId } ] StreamingExecution detected` )
122138 streamToRead = ( result . response as any ) . stream
123-
139+
124140 // Extract citations from StreamingExecution at API level
125141 const execution = ( result . response as any ) . execution
126142 logger . info ( `[${ requestId } ] Extracting citations from StreamingExecution` , {
127143 hasExecution : ! ! execution ,
128144 hasToolResults : ! ! execution ?. toolResults ,
129145 toolResultsLength : execution ?. toolResults ?. length || 0 ,
130146 } )
131-
147+
132148 if ( execution ?. toolResults ) {
133149 for ( const toolResult of execution . toolResults ) {
134150 logger . info ( `[${ requestId } ] Processing tool result for citations` , {
135151 hasResult : ! ! toolResult ,
136152 resultKeys : toolResult && typeof toolResult === 'object' ? Object . keys ( toolResult ) : [ ] ,
137153 hasResultsArray : ! ! ( toolResult && typeof toolResult === 'object' && toolResult . results ) ,
138154 } )
139-
155+
140156 if ( toolResult && typeof toolResult === 'object' && toolResult . results ) {
141157 // Convert documentation search results to citations
142158 const extractedCitations = toolResult . results . map ( ( res : any , index : number ) => ( {
@@ -146,7 +162,10 @@ export async function POST(req: NextRequest) {
146162 similarity : res . similarity ,
147163 } ) )
148164 result . citations = extractedCitations
149- logger . info ( `[${ requestId } ] Extracted ${ extractedCitations . length } citations from tool results:` , extractedCitations )
165+ logger . info (
166+ `[${ requestId } ] Extracted ${ extractedCitations . length } citations from tool results:` ,
167+ extractedCitations
168+ )
150169 break // Use first set of results found
151170 }
152171 }
@@ -354,12 +373,12 @@ export async function PATCH(req: NextRequest) {
354373
355374 // Get the current chat to check if it has a title
356375 const existingChat = await getChat ( chatId , session . user . id )
357-
376+
358377 let titleToUse = title
359-
378+
360379 // Generate title if chat doesn't have one and we have messages
361380 if ( ! titleToUse && existingChat && ! existingChat . title && messages && messages . length > 0 ) {
362- const firstUserMessage = messages . find ( msg => msg . role === 'user' )
381+ const firstUserMessage = messages . find ( ( msg ) => msg . role === 'user' )
363382 if ( firstUserMessage ) {
364383 logger . info ( 'Generating LLM-based title for chat without title' )
365384 try {
0 commit comments