Spaces:
Sleeping
Sleeping
File size: 7,452 Bytes
097eaf0 | 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | import { Response } from "express";
import { config } from "../../config";
import {
RequestWithAuth,
SearchRequest,
SearchResponse,
searchRequestSchema,
} from "./types";
import { billTeam } from "../../services/billing/credit_billing";
import { v7 as uuidv7 } from "uuid";
import { logSearch, logRequest } from "../../services/logging/log_job";
import { logger as _logger } from "../../lib/logger";
import { ScrapeJobTimeoutError } from "../../lib/error";
import { z } from "zod";
import { CategoryOption } from "../../lib/search-query-builder";
import {
applyZdrScope,
captureExceptionWithZdrCheck,
} from "../../services/sentry";
import { executeSearch } from "../../search/execute";
import type { BillingMetadata } from "../../services/billing/types";
import { getSearchZDR } from "../../lib/zdr-helpers";
export async function searchController(
req: RequestWithAuth<{}, SearchResponse, SearchRequest>,
res: Response<SearchResponse>,
) {
const middlewareStartTime =
(req as any).requestTiming?.startTime || new Date().getTime();
const controllerStartTime = new Date().getTime();
const jobId = uuidv7();
let logger = _logger.child({
jobId,
teamId: req.auth.team_id,
module: "api/v2",
method: "searchController",
zeroDataRetention: getSearchZDR(req.acuc?.flags) === "forced",
});
if (getSearchZDR(req.acuc?.flags) === "forced") {
return res.status(400).json({
success: false,
error:
"Your team has zero data retention enabled. This is not supported on search. Please contact support@firecrawl.com to unblock this feature.",
});
}
const middlewareTime = controllerStartTime - middlewareStartTime;
const isSearchPreview =
config.SEARCH_PREVIEW_TOKEN !== undefined &&
config.SEARCH_PREVIEW_TOKEN === req.body.__searchPreviewToken;
let zeroDataRetention = false;
try {
req.body = searchRequestSchema.parse(req.body);
if (
req.body.__agentInterop &&
config.AGENT_INTEROP_SECRET &&
req.body.__agentInterop.auth !== config.AGENT_INTEROP_SECRET
) {
return res.status(403).json({
success: false,
error: "Invalid agent interop.",
});
} else if (req.body.__agentInterop && !config.AGENT_INTEROP_SECRET) {
return res.status(403).json({
success: false,
error: "Agent interop is not enabled.",
});
}
const shouldBill = req.body.__agentInterop?.shouldBill ?? true;
const agentRequestId = req.body.__agentInterop?.requestId ?? null;
const billing: BillingMetadata = req.body.__agentInterop
? { endpoint: "agent" as const, jobId }
: { endpoint: "search" as const, jobId };
logger = logger.child({
version: "v2",
query: req.body.query,
origin: req.body.origin,
});
const isZDR = req.body.enterprise?.includes("zdr");
const isAnon = req.body.enterprise?.includes("anon");
const isZDROrAnon = isZDR || isAnon;
zeroDataRetention = isZDROrAnon ?? false;
applyZdrScope(isZDROrAnon ?? false);
// Verify the team has searchZDR enabled before allowing enterprise ZDR/anon
if (isZDROrAnon) {
const searchMode = getSearchZDR(req.acuc?.flags);
if (searchMode !== "allowed" && searchMode !== "forced") {
return res.status(403).json({
success: false,
error:
"Zero Data Retention (ZDR) search is not enabled for your team. Contact support@firecrawl.com to enable this feature.",
});
}
}
if (!agentRequestId) {
await logRequest({
id: jobId,
kind: "search",
api_version: "v2",
team_id: req.auth.team_id,
origin: req.body.origin ?? "api",
integration: req.body.integration,
target_hint: req.body.query,
zeroDataRetention: isZDROrAnon ?? false,
api_key_id: req.acuc?.api_key_id ?? null,
});
}
const result = await executeSearch(
{
query: req.body.query,
limit: req.body.limit,
tbs: req.body.tbs,
filter: req.body.filter,
lang: req.body.lang,
country: req.body.country,
location: req.body.location,
sources: req.body.sources as Array<{ type: string }>,
categories: req.body.categories as CategoryOption[],
includeDomains: req.body.includeDomains,
excludeDomains: req.body.excludeDomains,
enterprise: req.body.enterprise,
scrapeOptions: req.body.scrapeOptions,
timeout: req.body.timeout,
},
{
teamId: req.auth.team_id,
origin: req.body.origin,
apiKeyId: req.acuc?.api_key_id ?? null,
flags: req.acuc?.flags ?? null,
requestId: agentRequestId ?? jobId,
jobId,
apiVersion: "v2",
bypassBilling: !shouldBill,
zeroDataRetention: isZDROrAnon,
billing,
agentIndexOnly: (req as any).agentIndexOnly ?? false,
},
logger,
);
// Bill team for search credits only (scrape jobs bill themselves)
if (!isSearchPreview && shouldBill) {
billTeam(
req.auth.team_id,
req.acuc?.sub_id ?? undefined,
result.searchCredits,
req.acuc?.api_key_id ?? null,
billing,
).catch(error =>
logger.error(
`Failed to bill team ${req.acuc?.sub_id} for ${result.searchCredits} credits: ${error}`,
),
);
}
const endTime = new Date().getTime();
const timeTakenInSeconds = (endTime - middlewareStartTime) / 1000;
logSearch(
{
id: jobId,
request_id: agentRequestId ?? jobId,
query: req.body.query,
is_successful: true,
error: undefined,
results: result.response as any,
num_results: result.totalResultsCount,
time_taken: timeTakenInSeconds,
team_id: req.auth.team_id,
options: req.body,
credits_cost: shouldBill ? result.searchCredits : 0,
zeroDataRetention: isZDROrAnon ?? false,
},
false,
);
const totalRequestTime = new Date().getTime() - middlewareStartTime;
const controllerTime = new Date().getTime() - controllerStartTime;
logger.info("Request metrics", {
version: "v2",
jobId,
mode: "search",
middlewareStartTime,
controllerStartTime,
middlewareTime,
controllerTime,
totalRequestTime,
searchCredits: result.searchCredits,
scrapeCredits: result.scrapeCredits,
totalCredits: result.totalCredits,
scrapeful: result.shouldScrape,
});
return res.status(200).json({
success: true,
data: result.response,
creditsUsed: result.totalCredits,
id: jobId,
});
} catch (error) {
if (error instanceof z.ZodError) {
logger.warn("Invalid request body", { error: error.issues });
return res.status(400).json({
success: false,
error: "Invalid request body",
details: error.issues,
});
}
if (error instanceof ScrapeJobTimeoutError) {
return res.status(408).json({
success: false,
code: error.code,
error: error.message,
});
}
captureExceptionWithZdrCheck(error, {
extra: { zeroDataRetention },
});
logger.error("Unhandled error occurred in search", {
version: "v2",
error,
});
return res.status(500).json({
success: false,
error: error.message,
});
}
}
|