forked from firecrawl/firecrawl-convex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
241 lines (223 loc) · 6.96 KB
/
Copy pathexample.ts
File metadata and controls
241 lines (223 loc) · 6.96 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
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
import { v } from "convex/values";
import { paginationOptsValidator } from "convex/server";
import { FirecrawlClient } from "@firecrawl/firecrawl-convex";
import {
action,
internalMutation,
internalQuery,
mutation,
query,
type QueryCtx,
} from "./_generated/server.js";
import { components, internal } from "./_generated/api.js";
import { requireUser } from "./auth.js";
const firecrawl = new FirecrawlClient(components.firecrawl);
/**
* Crawl-scoped operations go through this. Without it, anyone could read,
* cancel, or delete anyone else's crawl by guessing an id — the component
* has no notion of who owns what.
*/
async function requireOwnedCrawl(
ctx: QueryCtx & { auth: any },
crawlId: string,
): Promise<string> {
const userId = await requireUser(ctx);
const owner = await ctx.db
.query("crawlOwners")
.withIndex("crawlId", (q) => q.eq("crawlId", crawlId))
.first();
if (!owner || owner.userId !== userId) {
throw new Error("No such crawl.");
}
return userId;
}
// ---------------------------------------------------------------------------
// One-shot calls — these spend Firecrawl credits, so they require a caller.
// A production app should also rate-limit them, e.g. with @convex-dev/ratelimiter.
// ---------------------------------------------------------------------------
export const scrapePage = action({
args: { url: v.string() },
handler: async (ctx, args) => {
await requireUser(ctx);
return await firecrawl.scrape(ctx, args.url, {
formats: ["markdown"],
onlyMainContent: true,
});
},
});
export const mapSite = action({
args: { url: v.string(), search: v.optional(v.string()) },
handler: async (ctx, args) => {
await requireUser(ctx);
return await firecrawl.map(ctx, args.url, { search: args.search, limit: 100 });
},
});
export const searchWeb = action({
args: { query: v.string(), limit: v.optional(v.number()) },
handler: async (ctx, args) => {
await requireUser(ctx);
return await firecrawl.search(ctx, args.query, {
limit: args.limit ?? 5,
scrapeOptions: { formats: ["markdown"] },
});
},
});
// ---------------------------------------------------------------------------
// Durable crawls
// ---------------------------------------------------------------------------
export const startCrawl = action({
args: {
url: v.string(),
limit: v.optional(v.number()),
label: v.optional(v.string()),
},
handler: async (ctx, args): Promise<{ crawlId: string; jobId: string }> => {
const userId = await requireUser(ctx);
const started = await firecrawl.startCrawl(ctx, {
url: args.url,
options: {
limit: args.limit ?? 10,
scrapeOptions: { formats: ["markdown"], onlyMainContent: true },
},
onComplete: internal.example.onCrawlComplete,
context: { userId, label: args.label },
});
await ctx.runMutation(internal.example.recordOwner, {
crawlId: started.crawlId,
userId,
label: args.label,
});
return started;
},
});
export const recordOwner = internalMutation({
args: { crawlId: v.string(), userId: v.string(), label: v.optional(v.string()) },
returns: v.null(),
handler: async (ctx, args) => {
await ctx.db.insert("crawlOwners", args);
return null;
},
});
/**
* Runs once, when the crawl reaches a terminal state. Whatever you passed as
* `context` comes back untouched.
*/
export const onCrawlComplete = internalMutation({
args: {
crawlId: v.string(),
jobId: v.optional(v.string()),
status: v.union(
v.literal("completed"),
v.literal("failed"),
v.literal("cancelled"),
),
pageCount: v.number(),
unstored: v.optional(v.number()),
error: v.optional(v.string()),
context: v.optional(v.any()),
},
returns: v.null(),
handler: async (ctx, args) => {
await ctx.db.insert("crawlReports", {
crawlId: args.crawlId,
userId: args.context?.userId ?? "unknown",
label: args.context?.label,
status: args.status,
pageCount: args.pageCount,
// Non-zero means content was dropped — worth surfacing, not swallowing.
unstored: args.unstored,
error: args.error,
});
return null;
},
});
/** Subscribe to this for live crawl progress. */
export const crawlProgress = query({
args: { crawlId: v.string() },
handler: async (ctx, args) => {
await requireOwnedCrawl(ctx, args.crawlId);
return await firecrawl.getCrawl(ctx, args.crawlId);
},
});
/** Pages as they arrive. Pairs with React's `usePaginatedQuery`. */
export const crawlPages = query({
args: { crawlId: v.string(), paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
await requireOwnedCrawl(ctx, args.crawlId);
return await firecrawl.listPages(ctx, {
crawlId: args.crawlId,
paginationOpts: args.paginationOpts,
});
},
});
/** Only the caller's crawls — never the whole table. */
export const myCrawls = query({
args: {},
handler: async (ctx) => {
const userId = await requireUser(ctx);
const owned = await ctx.db
.query("crawlOwners")
.withIndex("userId", (q) => q.eq("userId", userId))
.order("desc")
.take(20);
return await Promise.all(
owned.map(async (row) => ({
label: row.label,
crawl: await firecrawl.getCrawl(ctx, row.crawlId),
})),
);
},
});
export const reports = query({
args: {},
handler: async (ctx) => {
const userId = await requireUser(ctx);
return await ctx.db
.query("crawlReports")
.withIndex("userId", (q) => q.eq("userId", userId))
.order("desc")
.take(20);
},
});
export const cancelCrawl = action({
args: { crawlId: v.string() },
handler: async (ctx, args) => {
await ctx.runQuery(internal.example.assertOwned, {
crawlId: args.crawlId,
userId: await requireUser(ctx),
});
return await firecrawl.cancelCrawl(ctx, args.crawlId);
},
});
/** Ownership check callable from an action, which has no `ctx.db`. */
export const assertOwned = internalQuery({
args: { crawlId: v.string(), userId: v.string() },
returns: v.null(),
handler: async (ctx, args) => {
const owner = await ctx.db
.query("crawlOwners")
.withIndex("crawlId", (q) => q.eq("crawlId", args.crawlId))
.first();
if (!owner || owner.userId !== args.userId) throw new Error("No such crawl.");
return null;
},
});
export const resumeCrawl = mutation({
args: { crawlId: v.string() },
handler: async (ctx, args) => {
await requireOwnedCrawl(ctx, args.crawlId);
return await firecrawl.resumeCrawl(ctx, args.crawlId);
},
});
export const deleteCrawl = mutation({
args: { crawlId: v.string() },
handler: async (ctx, args) => {
await requireOwnedCrawl(ctx, args.crawlId);
const owner = await ctx.db
.query("crawlOwners")
.withIndex("crawlId", (q) => q.eq("crawlId", args.crawlId))
.first();
if (owner) await ctx.db.delete("crawlOwners", owner._id);
return await firecrawl.deleteCrawl(ctx, args.crawlId);
},
});