11import { type NextRequest , NextResponse } from 'next/server'
22import { z } from 'zod'
3- import { createLogger } from '@/lib/logs/console/logger'
43import { authenticateCopilotRequestSessionOnly } from '@/lib/copilot/auth'
5-
6- // Import server-side tool implementations from lib
7- import { searchDocsTool } from '@/lib/copilot/tools/server-tools/docs/search-docs'
84import { getBlocksAndToolsTool } from '@/lib/copilot/tools/server-tools/blocks/get-blocks-and-tools'
95import { getBlocksMetadataTool } from '@/lib/copilot/tools/server-tools/blocks/get-blocks-metadata'
10- import { getEnvironmentVariablesTool } from '@/ lib/copilot/tools/server-tools/user/get-environment-variables'
11- import { getOAuthCredentialsTool } from '@/lib/copilot/tools/server-tools/user/get-oauth-credentials '
6+ // Import server-side tool implementations from lib
7+ import { searchDocsTool } from '@/lib/copilot/tools/server-tools/docs/search-docs '
128import { listGDriveFilesTool } from '@/lib/copilot/tools/server-tools/gdrive/list-gdrive-files'
139import { readGDriveFileTool } from '@/lib/copilot/tools/server-tools/gdrive/read-gdrive-file'
1410import { gdriveRequestAccessServerTool } from '@/lib/copilot/tools/server-tools/other/gdrive-request-access'
1511import { makeApiRequestTool } from '@/lib/copilot/tools/server-tools/other/make-api-request'
1612import { onlineSearchTool } from '@/lib/copilot/tools/server-tools/other/online-search'
17- import { getWorkflowConsoleTool } from '@/lib/copilot/tools/server-tools/workflow/get-workflow-console'
13+ import { getEnvironmentVariablesTool } from '@/lib/copilot/tools/server-tools/user/get-environment-variables'
14+ import { getOAuthCredentialsTool } from '@/lib/copilot/tools/server-tools/user/get-oauth-credentials'
1815import { buildWorkflowTool } from '@/lib/copilot/tools/server-tools/workflow/build-workflow'
1916import { editWorkflowTool } from '@/lib/copilot/tools/server-tools/workflow/edit-workflow'
17+ import { getWorkflowConsoleTool } from '@/lib/copilot/tools/server-tools/workflow/get-workflow-console'
18+ import { createLogger } from '@/lib/logs/console/logger'
2019
2120const logger = createLogger ( 'CopilotToolsExecuteAPI' )
2221
2322const Schema = z . object ( {
24- methodId : z . string ( ) . min ( 1 ) ,
25- params : z . record ( z . any ( ) ) . optional ( ) . default ( { } ) ,
23+ methodId : z . string ( ) . min ( 1 ) ,
24+ params : z . record ( z . any ( ) ) . optional ( ) . default ( { } ) ,
2625} )
2726
2827const HANDLERS : Record < string , ( params : any ) => Promise < any > > = {
29- search_documentation : ( p ) => searchDocsTool . execute ( p ) ,
30- get_blocks_and_tools : ( p ) => getBlocksAndToolsTool . execute ( p ) ,
31- get_blocks_metadata : ( p ) => getBlocksMetadataTool . execute ( p ) ,
32- get_environment_variables : ( p ) => getEnvironmentVariablesTool . execute ( p ) ,
33- get_oauth_credentials : ( p ) => getOAuthCredentialsTool . execute ( p ) ,
34- list_gdrive_files : ( p ) => listGDriveFilesTool . execute ( p ) ,
35- read_gdrive_file : ( p ) => readGDriveFileTool . execute ( p ) ,
36- gdrive_request_access : ( p ) => gdriveRequestAccessServerTool . execute ( p ) ,
37- make_api_request : ( p ) => makeApiRequestTool . execute ( p ) ,
38- search_online : ( p ) => onlineSearchTool . execute ( p ) ,
39- get_workflow_console : ( p ) => getWorkflowConsoleTool . execute ( p ) ,
40- build_workflow : ( p ) => buildWorkflowTool . execute ( p ) ,
41- edit_workflow : ( p ) => editWorkflowTool . execute ( p ) ,
28+ search_documentation : ( p ) => searchDocsTool . execute ( p ) ,
29+ get_blocks_and_tools : ( p ) => getBlocksAndToolsTool . execute ( p ) ,
30+ get_blocks_metadata : ( p ) => getBlocksMetadataTool . execute ( p ) ,
31+ get_environment_variables : ( p ) => getEnvironmentVariablesTool . execute ( p ) ,
32+ get_oauth_credentials : ( p ) => getOAuthCredentialsTool . execute ( p ) ,
33+ list_gdrive_files : ( p ) => listGDriveFilesTool . execute ( p ) ,
34+ read_gdrive_file : ( p ) => readGDriveFileTool . execute ( p ) ,
35+ gdrive_request_access : ( p ) => gdriveRequestAccessServerTool . execute ( p ) ,
36+ make_api_request : ( p ) => makeApiRequestTool . execute ( p ) ,
37+ search_online : ( p ) => onlineSearchTool . execute ( p ) ,
38+ get_workflow_console : ( p ) => getWorkflowConsoleTool . execute ( p ) ,
39+ build_workflow : ( p ) => buildWorkflowTool . execute ( p ) ,
40+ edit_workflow : ( p ) => editWorkflowTool . execute ( p ) ,
4241}
4342
4443export async function POST ( req : NextRequest ) {
45- const requestId = crypto . randomUUID ( )
46- const start = Date . now ( )
47- try {
48- // Require session
49- const sessionAuth = await authenticateCopilotRequestSessionOnly ( )
50- if ( ! sessionAuth . isAuthenticated ) {
51- return NextResponse . json ( { success : false , error : 'Unauthorized' } , { status : 401 } )
52- }
44+ const requestId = crypto . randomUUID ( )
45+ const start = Date . now ( )
46+ try {
47+ // Require session
48+ const sessionAuth = await authenticateCopilotRequestSessionOnly ( )
49+ if ( ! sessionAuth . isAuthenticated ) {
50+ return NextResponse . json ( { success : false , error : 'Unauthorized' } , { status : 401 } )
51+ }
5352
54- const body = await req . json ( )
55- const { methodId, params } = Schema . parse ( body )
53+ const body = await req . json ( )
54+ const { methodId, params } = Schema . parse ( body )
5655
57- const handler = HANDLERS [ methodId ]
58- if ( ! handler ) {
59- logger . warn ( `[${ requestId } ] Unknown methodId` , { methodId } )
60- return NextResponse . json (
61- { success : false , error : `Unknown method: ${ methodId } ` } ,
62- { status : 400 }
63- )
64- }
56+ const handler = HANDLERS [ methodId ]
57+ if ( ! handler ) {
58+ logger . warn ( `[${ requestId } ] Unknown methodId` , { methodId } )
59+ return NextResponse . json (
60+ { success : false , error : `Unknown method: ${ methodId } ` } ,
61+ { status : 400 }
62+ )
63+ }
6564
66- logger . info ( `[${ requestId } ] Executing tool` , {
67- methodId,
68- paramsKeys : Object . keys ( params || { } ) ,
69- } )
65+ logger . info ( `[${ requestId } ] Executing tool` , {
66+ methodId,
67+ paramsKeys : Object . keys ( params || { } ) ,
68+ } )
7069
71- const result = await handler ( params )
72- const duration = Date . now ( ) - start
73- logger . info ( `[${ requestId } ] Tool executed` , { methodId, success : result ?. success , duration } )
70+ const result = await handler ( params )
71+ const duration = Date . now ( ) - start
72+ logger . info ( `[${ requestId } ] Tool executed` , { methodId, success : result ?. success , duration } )
7473
75- return NextResponse . json ( result , { status : result ?. success ? 200 : 400 } )
76- } catch ( e ) {
77- logger . error ( 'Execute failed' , { error : e instanceof Error ? e . message : 'Unknown error' } )
78- if ( e instanceof z . ZodError ) {
79- return NextResponse . json (
80- { success : false , error : e . errors . map ( ( er ) => er . message ) . join ( ', ' ) } ,
81- { status : 400 }
82- )
83- }
84- return NextResponse . json ( { success : false , error : 'Internal server error' } , { status : 500 } )
85- }
86- }
74+ return NextResponse . json ( result , { status : result ?. success ? 200 : 400 } )
75+ } catch ( e ) {
76+ logger . error ( 'Execute failed' , { error : e instanceof Error ? e . message : 'Unknown error' } )
77+ if ( e instanceof z . ZodError ) {
78+ return NextResponse . json (
79+ { success : false , error : e . errors . map ( ( er ) => er . message ) . join ( ', ' ) } ,
80+ { status : 400 }
81+ )
82+ }
83+ return NextResponse . json ( { success : false , error : 'Internal server error' } , { status : 500 } )
84+ }
85+ }
0 commit comments