-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathroute.ts
More file actions
146 lines (125 loc) · 3.77 KB
/
route.ts
File metadata and controls
146 lines (125 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { getBYOKKey } from '@/lib/api-key/byok'
import { checkHybridAuth } from '@/lib/auth/hybrid'
import { SEARCH_TOOL_COST } from '@/lib/billing/constants'
import { env } from '@/lib/core/config/env'
import { createLogger } from '@/lib/logs/console/logger'
import { executeTool } from '@/tools'
const logger = createLogger('search')
const SearchRequestSchema = z.object({
query: z.string().min(1),
workspaceId: z.string().optional(),
})
export const maxDuration = 60
export const dynamic = 'force-dynamic'
export async function POST(request: NextRequest) {
const requestId = crypto.randomUUID()
try {
const { searchParams: urlParams } = new URL(request.url)
const workflowId = urlParams.get('workflowId') || undefined
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
if (!authResult.success || !authResult.userId) {
const errorMessage = workflowId ? 'Workflow not found' : authResult.error || 'Unauthorized'
const statusCode = workflowId ? 404 : 401
return NextResponse.json({ success: false, error: errorMessage }, { status: statusCode })
}
const userId = authResult.userId
logger.info(`[${requestId}] Authenticated search request via ${authResult.authType}`, {
userId,
})
const body = await request.json()
const validated = SearchRequestSchema.parse(body)
let exaApiKey = env.EXA_API_KEY
let isBYOK = false
if (validated.workspaceId) {
const byokResult = await getBYOKKey(validated.workspaceId, 'exa')
if (byokResult) {
exaApiKey = byokResult.apiKey
isBYOK = true
logger.info(`[${requestId}] Using workspace BYOK key for Exa search`)
}
}
if (!exaApiKey) {
logger.error(`[${requestId}] No Exa API key available`)
return NextResponse.json(
{ success: false, error: 'Search service not configured' },
{ status: 503 }
)
}
logger.info(`[${requestId}] Executing search`, {
userId,
query: validated.query,
isBYOK,
})
const result = await executeTool('exa_search', {
query: validated.query,
type: 'auto',
useAutoprompt: true,
highlights: true,
apiKey: exaApiKey,
})
if (!result.success) {
logger.error(`[${requestId}] Search failed`, {
userId,
error: result.error,
})
return NextResponse.json(
{
success: false,
error: result.error || 'Search failed',
},
{ status: 500 }
)
}
const results = (result.output.results || []).map((r: any, index: number) => ({
title: r.title || '',
link: r.url || '',
snippet: Array.isArray(r.highlights) ? r.highlights.join(' ... ') : '',
date: r.publishedDate || undefined,
position: index + 1,
}))
const cost = {
input: 0,
output: 0,
total: isBYOK ? 0 : SEARCH_TOOL_COST,
tokens: {
input: 0,
output: 0,
total: 0,
},
model: 'search-exa',
pricing: {
input: 0,
cachedInput: 0,
output: 0,
updatedAt: new Date().toISOString(),
},
}
logger.info(`[${requestId}] Search completed`, {
userId,
resultCount: results.length,
cost: cost.total,
isBYOK,
})
return NextResponse.json({
results,
query: validated.query,
totalResults: results.length,
source: 'exa',
cost,
})
} catch (error: any) {
logger.error(`[${requestId}] Search failed`, {
error: error.message,
stack: error.stack,
})
return NextResponse.json(
{
success: false,
error: error.message || 'Search failed',
},
{ status: 500 }
)
}
}