mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
39 lines
995 B
TypeScript
39 lines
995 B
TypeScript
import { NextResponse } from 'next/server'
|
|
import { executeTool, getTool } from '@/tools'
|
|
import { validateToolRequest } from '@/tools/utils'
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const { toolId, params } = await request.json()
|
|
|
|
const tool = getTool(toolId)
|
|
|
|
// Validate the tool and its parameters
|
|
validateToolRequest(toolId, tool, params)
|
|
|
|
try {
|
|
if (!tool) {
|
|
throw new Error(`Tool not found: ${toolId}`)
|
|
}
|
|
|
|
// Use executeTool with skipProxy=true to prevent recursive proxy calls
|
|
const result = await executeTool(toolId, params, true)
|
|
|
|
if (!result.success) {
|
|
throw new Error(
|
|
tool.transformError ? tool.transformError(result) : 'Tool returned an error'
|
|
)
|
|
}
|
|
|
|
return NextResponse.json(result)
|
|
} catch (error: any) {
|
|
throw error
|
|
}
|
|
} catch (error: any) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
error: error.message || 'Unknown error',
|
|
})
|
|
}
|
|
}
|