Nick: fixed credits issue
Some checks are pending
Fly Deploy / Pre-deploy checks (push) Waiting to run
Fly Deploy / Test Suite (push) Blocked by required conditions
Fly Deploy / Python SDK Tests (push) Blocked by required conditions
Fly Deploy / JavaScript SDK Tests (push) Blocked by required conditions
Fly Deploy / Go SDK Tests (push) Blocked by required conditions
Fly Deploy / Deploy app (push) Blocked by required conditions
Fly Deploy / Build and publish Python SDK (push) Blocked by required conditions
Fly Deploy / Build and publish JavaScript SDK (push) Blocked by required conditions

This commit is contained in:
Nicolas 2024-08-28 18:32:45 -03:00
parent c3158b0f98
commit 67229c6b3a
5 changed files with 51 additions and 47 deletions

View File

@ -63,6 +63,7 @@ export async function scrapeHelper(
pageOptions, pageOptions,
extractorOptions, extractorOptions,
origin: req.body.origin ?? defaultOrigin, origin: req.body.origin ?? defaultOrigin,
is_scrape: true,
}, },
{}, {},
jobId, jobId,
@ -179,12 +180,10 @@ export async function scrapeController(req: Request, res: Response) {
typeof extractorOptions.extractionSchema !== "object" || typeof extractorOptions.extractionSchema !== "object" ||
extractorOptions.extractionSchema === null extractorOptions.extractionSchema === null
) { ) {
return res return res.status(400).json({
.status(400) error:
.json({ "extractorOptions.extractionSchema must be an object if llm-extraction mode is specified",
error: });
"extractorOptions.extractionSchema must be an object if llm-extraction mode is specified",
});
} }
pageOptions.onlyMainContent = true; pageOptions.onlyMainContent = true;
@ -202,12 +201,10 @@ export async function scrapeController(req: Request, res: Response) {
} catch (error) { } catch (error) {
Logger.error(error); Logger.error(error);
earlyReturn = true; earlyReturn = true;
return res return res.status(500).json({
.status(500) error:
.json({ "Error checking team credits. Please contact hello@firecrawl.com for help.",
error: });
"Error checking team credits. Please contact hello@firecrawl.com for help.",
});
} }
const jobId = uuidv4(); const jobId = uuidv4();
@ -231,8 +228,8 @@ export async function scrapeController(req: Request, res: Response) {
: 0; : 0;
if (result.success) { if (result.success) {
let creditsToBeBilled = 0; // billing for doc done on queue end let creditsToBeBilled = 1;
const creditsPerLLMExtract = 50; const creditsPerLLMExtract = 49;
if (extractorOptions.mode.includes("llm-extraction")) { if (extractorOptions.mode.includes("llm-extraction")) {
// creditsToBeBilled = creditsToBeBilled + (creditsPerLLMExtract * filteredDocs.length); // creditsToBeBilled = creditsToBeBilled + (creditsPerLLMExtract * filteredDocs.length);
@ -245,13 +242,16 @@ export async function scrapeController(req: Request, res: Response) {
// Don't bill if we're early returning // Don't bill if we're early returning
return; return;
} }
const billingResult = await billTeam(team_id, creditsToBeBilled); if (creditsToBeBilled > 0) {
if (!billingResult.success) { // billing for doc done on queue end, bill only for llm extraction
return res.status(402).json({ const billingResult = await billTeam(team_id, creditsToBeBilled);
success: false, if (!billingResult.success) {
error: return res.status(402).json({
"Failed to bill team. Insufficient credits or subscription not found.", success: false,
}); error:
"Failed to bill team. Insufficient credits or subscription not found.",
});
}
} }
} }
@ -276,13 +276,11 @@ export async function scrapeController(req: Request, res: Response) {
} catch (error) { } catch (error) {
Sentry.captureException(error); Sentry.captureException(error);
Logger.error(error); Logger.error(error);
return res return res.status(500).json({
.status(500) error:
.json({ typeof error === "string"
error: ? error
typeof error === "string" : error?.message ?? "Internal Server Error",
? error });
: error?.message ?? "Internal Server Error",
});
} }
} }

View File

@ -29,6 +29,7 @@ export async function scrapeController(req: RequestWithAuth<{}, ScrapeResponse,
pageOptions, pageOptions,
extractorOptions: {}, extractorOptions: {},
origin: req.body.origin, origin: req.body.origin,
is_scrape: true,
}, {}, jobId, jobPriority); }, {}, jobId, jobPriority);
let doc: any | undefined; let doc: any | undefined;

View File

@ -57,6 +57,7 @@ export async function startWebScraperPipeline({
team_id: job.data.team_id, team_id: job.data.team_id,
bull_job_id: job.id.toString(), bull_job_id: job.id.toString(),
priority: job.opts.priority, priority: job.opts.priority,
is_scrape: job.data.is_scrape ?? false,
})) as { success: boolean; message: string; docs: Document[] }; })) as { success: boolean; message: string; docs: Document[] };
} }
export async function runWebScraper({ export async function runWebScraper({
@ -71,6 +72,7 @@ export async function runWebScraper({
team_id, team_id,
bull_job_id, bull_job_id,
priority, priority,
is_scrape=false,
}: RunWebScraperParams): Promise<RunWebScraperResult> { }: RunWebScraperParams): Promise<RunWebScraperResult> {
try { try {
const provider = new WebScraperDataProvider(); const provider = new WebScraperDataProvider();
@ -117,18 +119,21 @@ export async function runWebScraper({
} }
}) })
: docs; : docs;
const billingResult = await billTeam(team_id, filteredDocs.length);
if (!billingResult.success) { if(is_scrape === false) {
// throw new Error("Failed to bill team, no subscription was found"); const billingResult = await billTeam(team_id, filteredDocs.length);
return { if (!billingResult.success) {
success: false, // throw new Error("Failed to bill team, no subscription was found");
message: "Failed to bill team, no subscription was found", return {
docs: [], success: false,
}; message: "Failed to bill team, no subscription was found",
docs: [],
};
}
} }
// This is where the returnvalue from the job is set // This is where the returnvalue from the job is set
onSuccess(filteredDocs, mode); onSuccess(filteredDocs, mode);

View File

@ -32,6 +32,7 @@ export interface WebScraperOptions {
sitemapped?: boolean; sitemapped?: boolean;
webhook?: string; webhook?: string;
v1?: boolean; v1?: boolean;
is_scrape?: boolean;
} }
export interface RunWebScraperParams { export interface RunWebScraperParams {
@ -46,6 +47,7 @@ export interface RunWebScraperParams {
team_id: string; team_id: string;
bull_job_id: string; bull_job_id: string;
priority?: number; priority?: number;
is_scrape?: boolean;
} }
export interface RunWebScraperResult { export interface RunWebScraperResult {

View File

@ -153,16 +153,14 @@ export interface ScrapeResponseV0 {
* Includes options for both scraping and mapping during a crawl. * Includes options for both scraping and mapping during a crawl.
*/ */
export interface CrawlParams { export interface CrawlParams {
includePaths?: string[];
excludePaths?: string[];
maxDepth?: number;
limit?: number;
allowBackwardLinks?: boolean;
allowExternalLinks?: boolean;
ignoreSitemap?: boolean;
scrapeOptions?: ScrapeParams; scrapeOptions?: ScrapeParams;
crawlerOptions?: {
includePaths?: string[];
excludePaths?: string[];
maxDepth?: number;
limit?: number;
allowBackwardLinks?: boolean;
allowExternalLinks?: boolean;
ignoreSitemap?: boolean;
};
} }
/** /**
* Parameters for crawling operations on v0. * Parameters for crawling operations on v0.