1- import { type NextRequest , NextResponse } from 'next/server'
2- import { z } from 'zod'
3- import { searchDocumentation } from '@/lib/copilot/service'
1+ import { NextRequest , NextResponse } from 'next/server'
42import { createLogger } from '@/lib/logs/console-logger'
3+ import { searchDocumentation } from '@/lib/copilot/service'
54
65const logger = createLogger ( 'DocsSearchAPI' )
76
8- const SearchSchema = z . object ( {
9- query : z . string ( ) . min ( 1 , 'Query is required' ) ,
10- topK : z . number ( ) . min ( 1 ) . max ( 20 ) . default ( 5 ) ,
11- } )
12-
13- /**
14- * POST /api/docs/search
15- * Search documentation for copilot tools
16- */
17- export async function POST ( req : NextRequest ) {
18- const requestId = crypto . randomUUID ( )
19-
7+ export async function POST ( request : NextRequest ) {
208 try {
21- const body = await req . json ( )
22- const { query, topK } = SearchSchema . parse ( body )
9+ const { query, topK = 5 } = await request . json ( )
2310
24- logger . info ( `[${ requestId } ] Documentation search request: "${ query } "` , { topK } )
11+ if ( ! query ) {
12+ return NextResponse . json ( { error : 'Query is required' } , { status : 400 } )
13+ }
14+
15+ logger . info ( 'Executing documentation search' , { query, topK } )
2516
2617 const results = await searchDocumentation ( query , { topK } )
2718
28- logger . info ( `[ ${ requestId } ] Found ${ results . length } documentation results` , { query } )
19+ logger . info ( `Found ${ results . length } documentation results` , { query } )
2920
3021 return NextResponse . json ( {
3122 success : true ,
3223 results,
3324 query,
3425 totalResults : results . length ,
35- metadata : {
36- requestId,
37- query,
38- topK,
39- } ,
4026 } )
4127 } catch ( error ) {
42- if ( error instanceof z . ZodError ) {
43- return NextResponse . json (
44- { error : 'Invalid request data' , details : error . errors } ,
45- { status : 400 }
46- )
47- }
48-
49- logger . error ( `[${ requestId } ] Documentation search error:` , error )
28+ logger . error ( 'Documentation search API failed' , error )
5029 return NextResponse . json (
5130 {
52- error : 'Failed to search documentation' ,
53- details : error instanceof Error ? error . message : 'Unknown error' ,
31+ success : false ,
32+ error : `Documentation search failed: ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
5433 } ,
5534 { status : 500 }
5635 )
5736 }
58- }
37+ }
0 commit comments