render-lab-tasks-firecrawl 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- render_lab_tasks_firecrawl/__init__.py +1 -0
- render_lab_tasks_firecrawl/_app.py +3 -0
- render_lab_tasks_firecrawl/await_batch_scrape.py +18 -0
- render_lab_tasks_firecrawl/await_crawl.py +18 -0
- render_lab_tasks_firecrawl/await_extract.py +18 -0
- render_lab_tasks_firecrawl/cancel_batch_scrape.py +18 -0
- render_lab_tasks_firecrawl/cancel_crawl.py +18 -0
- render_lab_tasks_firecrawl/client.py +329 -0
- render_lab_tasks_firecrawl/get_job_results.py +18 -0
- render_lab_tasks_firecrawl/map.py +16 -0
- render_lab_tasks_firecrawl/py.typed +0 -0
- render_lab_tasks_firecrawl/retry.py +6 -0
- render_lab_tasks_firecrawl/scrape.py +18 -0
- render_lab_tasks_firecrawl/search.py +18 -0
- render_lab_tasks_firecrawl/start_batch_scrape.py +18 -0
- render_lab_tasks_firecrawl/start_crawl.py +18 -0
- render_lab_tasks_firecrawl/start_extract.py +18 -0
- render_lab_tasks_firecrawl/tasks.py +41 -0
- render_lab_tasks_firecrawl/types.py +206 -0
- render_lab_tasks_firecrawl/webhooks.py +47 -0
- render_lab_tasks_firecrawl-0.1.0.dist-info/METADATA +93 -0
- render_lab_tasks_firecrawl-0.1.0.dist-info/RECORD +24 -0
- render_lab_tasks_firecrawl-0.1.0.dist-info/WHEEL +4 -0
- render_lab_tasks_firecrawl-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Import .tasks explicitly to register durable tasks."""
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_AWAIT_RETRY
|
|
6
|
+
from .types import AwaitJobInput, FirecrawlJobState
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def await_batch_scrape_impl(
|
|
10
|
+
ctx: TaskContext, input: AwaitJobInput, *, deps: Deps | None = None
|
|
11
|
+
) -> FirecrawlJobState:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.await_batch_scrape(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.awaitBatchScrape", retry=FIRECRAWL_AWAIT_RETRY)
|
|
17
|
+
async def await_batch_scrape(ctx: TaskContext, input: AwaitJobInput) -> FirecrawlJobState:
|
|
18
|
+
return await await_batch_scrape_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_AWAIT_RETRY
|
|
6
|
+
from .types import AwaitJobInput, FirecrawlJobState
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def await_crawl_impl(
|
|
10
|
+
ctx: TaskContext, input: AwaitJobInput, *, deps: Deps | None = None
|
|
11
|
+
) -> FirecrawlJobState:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.await_crawl(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.awaitCrawl", retry=FIRECRAWL_AWAIT_RETRY)
|
|
17
|
+
async def await_crawl(ctx: TaskContext, input: AwaitJobInput) -> FirecrawlJobState:
|
|
18
|
+
return await await_crawl_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_AWAIT_RETRY
|
|
6
|
+
from .types import AwaitExtractInput, ExtractResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def await_extract_impl(
|
|
10
|
+
ctx: TaskContext, input: AwaitExtractInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ExtractResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.await_extract(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.awaitExtract", retry=FIRECRAWL_AWAIT_RETRY)
|
|
17
|
+
async def await_extract(ctx: TaskContext, input: AwaitExtractInput) -> ExtractResult:
|
|
18
|
+
return await await_extract_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_CANCEL_RETRY
|
|
6
|
+
from .types import CancelJobInput, CancelJobResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def cancel_batch_scrape_impl(
|
|
10
|
+
ctx: TaskContext, input: CancelJobInput, *, deps: Deps | None = None
|
|
11
|
+
) -> CancelJobResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.cancel_batch_scrape(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.cancelBatchScrape", retry=FIRECRAWL_CANCEL_RETRY)
|
|
17
|
+
async def cancel_batch_scrape(ctx: TaskContext, input: CancelJobInput) -> CancelJobResult:
|
|
18
|
+
return await cancel_batch_scrape_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_CANCEL_RETRY
|
|
6
|
+
from .types import CancelJobInput, CancelJobResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def cancel_crawl_impl(
|
|
10
|
+
ctx: TaskContext, input: CancelJobInput, *, deps: Deps | None = None
|
|
11
|
+
) -> CancelJobResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.cancel_crawl(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.cancelCrawl", retry=FIRECRAWL_CANCEL_RETRY)
|
|
17
|
+
async def cancel_crawl(ctx: TaskContext, input: CancelJobInput) -> CancelJobResult:
|
|
18
|
+
return await cancel_crawl_impl(ctx, input)
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"""Injected vendor interface and one-attempt async HTTP adapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from collections.abc import AsyncIterator, Mapping
|
|
8
|
+
from contextlib import asynccontextmanager
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import Any, Literal, Protocol, cast
|
|
11
|
+
from urllib.parse import urlsplit
|
|
12
|
+
|
|
13
|
+
import httpx
|
|
14
|
+
from render_lab_tasks_core.http import (
|
|
15
|
+
ApiError,
|
|
16
|
+
HttpClient,
|
|
17
|
+
enc,
|
|
18
|
+
js_string,
|
|
19
|
+
loads_json,
|
|
20
|
+
nullish,
|
|
21
|
+
required,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
from . import types as t
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def obj(value: Any) -> dict[str, Any]:
|
|
28
|
+
return value if isinstance(value, dict) else {}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def array(value: Any) -> list[Any]:
|
|
32
|
+
return value if isinstance(value, list) else []
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def numeric(value: Any) -> bool:
|
|
36
|
+
return isinstance(value, (int, float)) and not isinstance(value, bool)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def without(value: Mapping[str, Any], *keys: str) -> dict[str, Any]:
|
|
40
|
+
return {k: v for k, v in value.items() if k not in keys}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def kind_path(kind: t.FirecrawlJobKind) -> str:
|
|
44
|
+
return "crawl" if kind == "crawl" else "batch/scrape"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def document(value: Any) -> t.FirecrawlDocument:
|
|
48
|
+
row = obj(value)
|
|
49
|
+
meta = obj(row.get("metadata"))
|
|
50
|
+
doc: dict[str, Any] = {
|
|
51
|
+
"url": nullish(
|
|
52
|
+
meta.get("sourceURL"), nullish(meta.get("url"), nullish(row.get("url"), ""))
|
|
53
|
+
),
|
|
54
|
+
"title": nullish(meta.get("title"), ""),
|
|
55
|
+
"description": nullish(meta.get("description"), ""),
|
|
56
|
+
"statusCode": meta["statusCode"] if numeric(meta.get("statusCode")) else None,
|
|
57
|
+
}
|
|
58
|
+
for key in ("markdown", "html", "rawHtml", "summary"):
|
|
59
|
+
if isinstance(row.get(key), str):
|
|
60
|
+
doc[key] = row[key]
|
|
61
|
+
for key in ("links", "images"):
|
|
62
|
+
if isinstance(row.get(key), list):
|
|
63
|
+
doc[key] = row[key]
|
|
64
|
+
return cast(t.FirecrawlDocument, doc)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def chars(value: str) -> int:
|
|
68
|
+
return len(value.encode("utf-16-le")) // 2
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def content_budget(doc: t.FirecrawlDocument, maximum: float) -> None:
|
|
72
|
+
n = sum(
|
|
73
|
+
chars(cast(str, doc.get(k, "")))
|
|
74
|
+
for k in ("title", "description", "markdown", "html", "rawHtml", "summary")
|
|
75
|
+
)
|
|
76
|
+
n += sum(chars(v) for k in ("links", "images") for v in cast(list[str], doc.get(k, [])))
|
|
77
|
+
if n > maximum:
|
|
78
|
+
raise ValueError(
|
|
79
|
+
f"Firecrawl document {doc['url'] or '(unknown url)'} is "
|
|
80
|
+
f"{n} characters, exceeding maxContentChars "
|
|
81
|
+
f"{js_string(maximum)}. Reduce maxResults or "
|
|
82
|
+
f"maxContentChars."
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def job(kind: t.FirecrawlJobKind, id: str, value: Any) -> t.FirecrawlJobState:
|
|
87
|
+
row = obj(value)
|
|
88
|
+
return {
|
|
89
|
+
"jobId": id,
|
|
90
|
+
"kind": kind,
|
|
91
|
+
"status": nullish(row.get("status"), "scraping"),
|
|
92
|
+
"completed": nullish(row.get("completed"), 0),
|
|
93
|
+
"total": nullish(row.get("total"), 0),
|
|
94
|
+
"creditsUsed": nullish(row.get("creditsUsed"), 0),
|
|
95
|
+
"expiresAt": nullish(row.get("expiresAt"), ""),
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class Port(Protocol):
|
|
100
|
+
async def await_batch_scrape(self, input: t.AwaitJobInput) -> t.FirecrawlJobState: ...
|
|
101
|
+
async def await_crawl(self, input: t.AwaitJobInput) -> t.FirecrawlJobState: ...
|
|
102
|
+
async def await_extract(self, input: t.AwaitExtractInput) -> t.ExtractResult: ...
|
|
103
|
+
async def cancel_batch_scrape(self, input: t.CancelJobInput) -> t.CancelJobResult: ...
|
|
104
|
+
async def cancel_crawl(self, input: t.CancelJobInput) -> t.CancelJobResult: ...
|
|
105
|
+
async def get_job_results(self, input: t.GetJobResultsInput) -> t.JobResultsPage: ...
|
|
106
|
+
async def map(self, input: t.MapInput) -> t.MapResult: ...
|
|
107
|
+
async def scrape(self, input: t.ScrapeInput) -> t.ScrapeResult: ...
|
|
108
|
+
async def search(self, input: t.SearchInput) -> t.SearchResult: ...
|
|
109
|
+
async def start_batch_scrape(self, input: t.StartBatchScrapeInput) -> t.FirecrawlJobRef: ...
|
|
110
|
+
async def start_crawl(self, input: t.StartCrawlInput) -> t.FirecrawlJobRef: ...
|
|
111
|
+
async def start_extract(self, input: t.StartExtractInput) -> t.ExtractJobRef: ...
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass(frozen=True)
|
|
115
|
+
class Deps:
|
|
116
|
+
firecrawl: Port
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class Client:
|
|
120
|
+
def __init__(self, http: httpx.AsyncClient, env: Mapping[str, str] | None = None) -> None:
|
|
121
|
+
self.env = os.environ if env is None else env
|
|
122
|
+
|
|
123
|
+
def auth() -> dict[str, str]:
|
|
124
|
+
return {"authorization": "Bearer " + required(self.env, "FIRECRAWL_API_KEY")}
|
|
125
|
+
|
|
126
|
+
self.api = HttpClient(
|
|
127
|
+
http, "https://api.firecrawl.dev/v2", auth=auth, label="Firecrawl API"
|
|
128
|
+
)
|
|
129
|
+
self.absolute = HttpClient(http, "", auth=auth, label="Firecrawl API")
|
|
130
|
+
|
|
131
|
+
async def scrape(self, input: t.ScrapeInput) -> t.ScrapeResult:
|
|
132
|
+
data = obj(
|
|
133
|
+
await self.api.call("/scrape", method="POST", body=without(input, "maxContentChars"))
|
|
134
|
+
)
|
|
135
|
+
doc = document(nullish(data.get("data"), data))
|
|
136
|
+
content_budget(doc, input["maxContentChars"])
|
|
137
|
+
result: t.ScrapeResult = {"document": doc}
|
|
138
|
+
warning = nullish(data.get("warning"), obj(data.get("data")).get("warning"))
|
|
139
|
+
if warning:
|
|
140
|
+
result["warning"] = warning
|
|
141
|
+
return result
|
|
142
|
+
|
|
143
|
+
async def map(self, input: t.MapInput) -> t.MapResult:
|
|
144
|
+
data = obj(await self.api.call("/map", method="POST", body=input))
|
|
145
|
+
return {
|
|
146
|
+
"links": [
|
|
147
|
+
{"url": r, "title": "", "description": ""}
|
|
148
|
+
if isinstance(r, str)
|
|
149
|
+
else {
|
|
150
|
+
"url": nullish(obj(r).get("url"), ""),
|
|
151
|
+
"title": nullish(obj(r).get("title"), ""),
|
|
152
|
+
"description": nullish(obj(r).get("description"), ""),
|
|
153
|
+
}
|
|
154
|
+
for r in array(data.get("links"))
|
|
155
|
+
]
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async def search(self, input: t.SearchInput) -> t.SearchResult:
|
|
159
|
+
data = obj(
|
|
160
|
+
await self.api.call("/search", method="POST", body=without(input, "maxContentChars"))
|
|
161
|
+
)
|
|
162
|
+
groups = (
|
|
163
|
+
[("web", data["data"])]
|
|
164
|
+
if isinstance(data.get("data"), list)
|
|
165
|
+
else [
|
|
166
|
+
("web", obj(data.get("data")).get("web", [])),
|
|
167
|
+
("news", obj(data.get("data")).get("news", [])),
|
|
168
|
+
]
|
|
169
|
+
)
|
|
170
|
+
hits: list[t.SearchHit] = []
|
|
171
|
+
for source, rows in groups:
|
|
172
|
+
for i, value in enumerate(rows):
|
|
173
|
+
row = obj(value)
|
|
174
|
+
hit: t.SearchHit = {
|
|
175
|
+
"source": cast(Literal["web", "news"], source),
|
|
176
|
+
"url": nullish(row.get("url"), ""),
|
|
177
|
+
"title": nullish(row.get("title"), ""),
|
|
178
|
+
"description": nullish(row.get("description"), ""),
|
|
179
|
+
"position": row["position"] if numeric(row.get("position")) else i + 1,
|
|
180
|
+
}
|
|
181
|
+
if any(
|
|
182
|
+
row.get(k) or isinstance(row.get(k), dict)
|
|
183
|
+
for k in ("markdown", "html", "rawHtml", "summary", "metadata")
|
|
184
|
+
):
|
|
185
|
+
doc = document(row)
|
|
186
|
+
content_budget(doc, input["maxContentChars"])
|
|
187
|
+
hit["document"] = doc
|
|
188
|
+
hits.append(hit)
|
|
189
|
+
result: t.SearchResult = {"hits": hits}
|
|
190
|
+
if data.get("warning"):
|
|
191
|
+
result["warning"] = data["warning"]
|
|
192
|
+
return result
|
|
193
|
+
|
|
194
|
+
async def start_crawl(self, input: t.StartCrawlInput) -> t.FirecrawlJobRef:
|
|
195
|
+
data = obj(await self.api.call("/crawl", method="POST", body=input))
|
|
196
|
+
return {
|
|
197
|
+
"jobId": nullish(data.get("id"), ""),
|
|
198
|
+
"kind": "crawl",
|
|
199
|
+
"statusUrl": nullish(data.get("url"), ""),
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async def start_batch_scrape(self, input: t.StartBatchScrapeInput) -> t.FirecrawlJobRef:
|
|
203
|
+
data = obj(await self.api.call("/batch/scrape", method="POST", body=input))
|
|
204
|
+
return {
|
|
205
|
+
"jobId": nullish(data.get("id"), ""),
|
|
206
|
+
"kind": "batch",
|
|
207
|
+
"statusUrl": nullish(data.get("url"), ""),
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async def _await(self, kind: t.FirecrawlJobKind, id: str) -> t.FirecrawlJobState:
|
|
211
|
+
result = job(kind, id, await self.api.call("/" + kind_path(kind) + "/" + enc(id)))
|
|
212
|
+
label = "crawl" if kind == "crawl" else "batch scrape"
|
|
213
|
+
status = result["status"]
|
|
214
|
+
if status == "completed":
|
|
215
|
+
return result
|
|
216
|
+
if status == "scraping":
|
|
217
|
+
raise RuntimeError(f"Firecrawl {label} {id} is still scraping.")
|
|
218
|
+
if status in ("failed", "cancelled"):
|
|
219
|
+
raise RuntimeError(f"Firecrawl {label} {id} ended with status {status}.")
|
|
220
|
+
raise RuntimeError(f"Unknown Firecrawl {label} status: {status}")
|
|
221
|
+
|
|
222
|
+
async def await_crawl(self, input: t.AwaitJobInput) -> t.FirecrawlJobState:
|
|
223
|
+
return await self._await("crawl", input["jobId"])
|
|
224
|
+
|
|
225
|
+
async def await_batch_scrape(self, input: t.AwaitJobInput) -> t.FirecrawlJobState:
|
|
226
|
+
return await self._await("batch", input["jobId"])
|
|
227
|
+
|
|
228
|
+
async def _cancel(self, kind: t.FirecrawlJobKind, id: str) -> t.CancelJobResult:
|
|
229
|
+
try:
|
|
230
|
+
await self.api.call(
|
|
231
|
+
"/" + kind_path(kind) + "/" + enc(id), method="DELETE", response_type="text"
|
|
232
|
+
)
|
|
233
|
+
except ApiError as error:
|
|
234
|
+
if error.status != 404:
|
|
235
|
+
raise
|
|
236
|
+
return {"jobId": id, "kind": kind, "status": "cancelled"}
|
|
237
|
+
|
|
238
|
+
async def cancel_crawl(self, input: t.CancelJobInput) -> t.CancelJobResult:
|
|
239
|
+
return await self._cancel("crawl", input["jobId"])
|
|
240
|
+
|
|
241
|
+
async def cancel_batch_scrape(self, input: t.CancelJobInput) -> t.CancelJobResult:
|
|
242
|
+
return await self._cancel("batch", input["jobId"])
|
|
243
|
+
|
|
244
|
+
async def get_job_results(self, input: t.GetJobResultsInput) -> t.JobResultsPage:
|
|
245
|
+
if input.get("cursor"):
|
|
246
|
+
cursor = input["cursor"]
|
|
247
|
+
url = urlsplit(cursor)
|
|
248
|
+
if (
|
|
249
|
+
url.scheme != "https"
|
|
250
|
+
or url.hostname != "api.firecrawl.dev"
|
|
251
|
+
or url.port not in (None, 443)
|
|
252
|
+
or url.username is not None
|
|
253
|
+
or url.password is not None
|
|
254
|
+
):
|
|
255
|
+
raise ValueError(
|
|
256
|
+
"Firecrawl cursor must use https://api.firecrawl.dev to "
|
|
257
|
+
"receive API credentials."
|
|
258
|
+
)
|
|
259
|
+
raw = obj(loads_json(await self.absolute.call(cursor, response_type="text")))
|
|
260
|
+
else:
|
|
261
|
+
raw = obj(
|
|
262
|
+
await self.api.call("/" + kind_path(input["kind"]) + "/" + enc(input["jobId"]))
|
|
263
|
+
)
|
|
264
|
+
docs = [document(v) for v in array(raw.get("data"))[: int(input["maxResults"])]]
|
|
265
|
+
for doc in docs:
|
|
266
|
+
content_budget(doc, input["maxContentChars"])
|
|
267
|
+
return {
|
|
268
|
+
"job": job(input["kind"], input["jobId"], raw),
|
|
269
|
+
"documents": docs,
|
|
270
|
+
"nextCursor": raw.get("next"),
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async def start_extract(self, input: t.StartExtractInput) -> t.ExtractJobRef:
|
|
274
|
+
if not input.get("urls") and not input.get("prompt"):
|
|
275
|
+
raise ValueError("firecrawl.startExtract requires at least one URL or a prompt.")
|
|
276
|
+
raw = obj(await self.api.call("/extract", method="POST", body=input))
|
|
277
|
+
return {
|
|
278
|
+
"jobId": nullish(raw.get("id"), ""),
|
|
279
|
+
"statusUrl": nullish(raw.get("url"), ""),
|
|
280
|
+
"invalidURLs": array(raw.get("invalidURLs")),
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async def await_extract(self, input: t.AwaitExtractInput) -> t.ExtractResult:
|
|
284
|
+
id = input["jobId"]
|
|
285
|
+
raw = obj(await self.api.call("/extract/" + enc(id)))
|
|
286
|
+
status = nullish(raw.get("status"), "processing")
|
|
287
|
+
if status == "processing":
|
|
288
|
+
raise RuntimeError(f"Firecrawl extract {id} is still processing.")
|
|
289
|
+
if status in ("failed", "cancelled"):
|
|
290
|
+
raise RuntimeError(
|
|
291
|
+
f"Firecrawl extract {id} ended with status {status}: "
|
|
292
|
+
f"{nullish(raw.get('error'), '')}"
|
|
293
|
+
)
|
|
294
|
+
size = len(
|
|
295
|
+
json.dumps(
|
|
296
|
+
raw.get("data"), ensure_ascii=False, separators=(",", ":"), allow_nan=False
|
|
297
|
+
).encode()
|
|
298
|
+
)
|
|
299
|
+
if size > input["maxOutputBytes"]:
|
|
300
|
+
raise ValueError(
|
|
301
|
+
f"Firecrawl extraction result is {size} bytes, exceeding "
|
|
302
|
+
f"maxOutputBytes {js_string(input['maxOutputBytes'])}. "
|
|
303
|
+
f"Reduce the extraction schema or maxOutputBytes."
|
|
304
|
+
)
|
|
305
|
+
result: t.ExtractResult = {
|
|
306
|
+
"jobId": id,
|
|
307
|
+
"status": "completed",
|
|
308
|
+
"data": raw.get("data"),
|
|
309
|
+
"expiresAt": nullish(raw.get("expiresAt"), ""),
|
|
310
|
+
}
|
|
311
|
+
if isinstance(raw.get("sources"), list):
|
|
312
|
+
result["sources"] = raw["sources"]
|
|
313
|
+
return result
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
@asynccontextmanager
|
|
317
|
+
async def dependencies(deps: Deps | None) -> AsyncIterator[Deps]:
|
|
318
|
+
if deps is not None:
|
|
319
|
+
yield deps
|
|
320
|
+
else:
|
|
321
|
+
async with httpx.AsyncClient(
|
|
322
|
+
timeout=30, transport=httpx.AsyncHTTPTransport(retries=0)
|
|
323
|
+
) as http:
|
|
324
|
+
yield Deps(firecrawl=Client(http))
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
FirecrawlPort = Port
|
|
328
|
+
FirecrawlClient = Client
|
|
329
|
+
FirecrawlDeps = Deps
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_RETRY
|
|
6
|
+
from .types import GetJobResultsInput, JobResultsPage
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def get_job_results_impl(
|
|
10
|
+
ctx: TaskContext, input: GetJobResultsInput, *, deps: Deps | None = None
|
|
11
|
+
) -> JobResultsPage:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.get_job_results(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.getJobResults", retry=FIRECRAWL_RETRY)
|
|
17
|
+
async def get_job_results(ctx: TaskContext, input: GetJobResultsInput) -> JobResultsPage:
|
|
18
|
+
return await get_job_results_impl(ctx, input)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_RETRY
|
|
6
|
+
from .types import MapInput, MapResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def map_impl(ctx: TaskContext, input: MapInput, *, deps: Deps | None = None) -> MapResult:
|
|
10
|
+
async with dependencies(deps) as resolved:
|
|
11
|
+
return await resolved.firecrawl.map(input)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.task(name="firecrawl.map", retry=FIRECRAWL_RETRY)
|
|
15
|
+
async def map(ctx: TaskContext, input: MapInput) -> MapResult:
|
|
16
|
+
return await map_impl(ctx, input)
|
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
from render import Retry
|
|
2
|
+
|
|
3
|
+
FIRECRAWL_AWAIT_RETRY = Retry(max_retries=360, wait_duration_ms=10000, backoff_scaling=1)
|
|
4
|
+
FIRECRAWL_CANCEL_RETRY = Retry(max_retries=3, wait_duration_ms=1000, backoff_scaling=2)
|
|
5
|
+
FIRECRAWL_RETRY = Retry(max_retries=3, wait_duration_ms=1000, backoff_scaling=2)
|
|
6
|
+
FIRECRAWL_TRIGGER_RETRY = Retry(max_retries=0, wait_duration_ms=0, backoff_scaling=1)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_RETRY
|
|
6
|
+
from .types import ScrapeInput, ScrapeResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def scrape_impl(
|
|
10
|
+
ctx: TaskContext, input: ScrapeInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ScrapeResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.scrape(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.scrape", retry=FIRECRAWL_RETRY)
|
|
17
|
+
async def scrape(ctx: TaskContext, input: ScrapeInput) -> ScrapeResult:
|
|
18
|
+
return await scrape_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_RETRY
|
|
6
|
+
from .types import SearchInput, SearchResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def search_impl(
|
|
10
|
+
ctx: TaskContext, input: SearchInput, *, deps: Deps | None = None
|
|
11
|
+
) -> SearchResult:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.search(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.search", retry=FIRECRAWL_RETRY)
|
|
17
|
+
async def search(ctx: TaskContext, input: SearchInput) -> SearchResult:
|
|
18
|
+
return await search_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_TRIGGER_RETRY
|
|
6
|
+
from .types import FirecrawlJobRef, StartBatchScrapeInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def start_batch_scrape_impl(
|
|
10
|
+
ctx: TaskContext, input: StartBatchScrapeInput, *, deps: Deps | None = None
|
|
11
|
+
) -> FirecrawlJobRef:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.start_batch_scrape(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.startBatchScrape", retry=FIRECRAWL_TRIGGER_RETRY)
|
|
17
|
+
async def start_batch_scrape(ctx: TaskContext, input: StartBatchScrapeInput) -> FirecrawlJobRef:
|
|
18
|
+
return await start_batch_scrape_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_TRIGGER_RETRY
|
|
6
|
+
from .types import FirecrawlJobRef, StartCrawlInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def start_crawl_impl(
|
|
10
|
+
ctx: TaskContext, input: StartCrawlInput, *, deps: Deps | None = None
|
|
11
|
+
) -> FirecrawlJobRef:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.start_crawl(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.startCrawl", retry=FIRECRAWL_TRIGGER_RETRY)
|
|
17
|
+
async def start_crawl(ctx: TaskContext, input: StartCrawlInput) -> FirecrawlJobRef:
|
|
18
|
+
return await start_crawl_impl(ctx, input)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from render import TaskContext
|
|
2
|
+
|
|
3
|
+
from ._app import app
|
|
4
|
+
from .client import Deps, dependencies
|
|
5
|
+
from .retry import FIRECRAWL_TRIGGER_RETRY
|
|
6
|
+
from .types import ExtractJobRef, StartExtractInput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def start_extract_impl(
|
|
10
|
+
ctx: TaskContext, input: StartExtractInput, *, deps: Deps | None = None
|
|
11
|
+
) -> ExtractJobRef:
|
|
12
|
+
async with dependencies(deps) as resolved:
|
|
13
|
+
return await resolved.firecrawl.start_extract(input)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.task(name="firecrawl.startExtract", retry=FIRECRAWL_TRIGGER_RETRY)
|
|
17
|
+
async def start_extract(ctx: TaskContext, input: StartExtractInput) -> ExtractJobRef:
|
|
18
|
+
return await start_extract_impl(ctx, input)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from ._app import app
|
|
2
|
+
from .await_batch_scrape import await_batch_scrape, await_batch_scrape_impl
|
|
3
|
+
from .await_crawl import await_crawl, await_crawl_impl
|
|
4
|
+
from .await_extract import await_extract, await_extract_impl
|
|
5
|
+
from .cancel_batch_scrape import cancel_batch_scrape, cancel_batch_scrape_impl
|
|
6
|
+
from .cancel_crawl import cancel_crawl, cancel_crawl_impl
|
|
7
|
+
from .get_job_results import get_job_results, get_job_results_impl
|
|
8
|
+
from .map import map, map_impl
|
|
9
|
+
from .scrape import scrape, scrape_impl
|
|
10
|
+
from .search import search, search_impl
|
|
11
|
+
from .start_batch_scrape import start_batch_scrape, start_batch_scrape_impl
|
|
12
|
+
from .start_crawl import start_crawl, start_crawl_impl
|
|
13
|
+
from .start_extract import start_extract, start_extract_impl
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"app",
|
|
17
|
+
"await_batch_scrape",
|
|
18
|
+
"await_batch_scrape_impl",
|
|
19
|
+
"await_crawl",
|
|
20
|
+
"await_crawl_impl",
|
|
21
|
+
"await_extract",
|
|
22
|
+
"await_extract_impl",
|
|
23
|
+
"cancel_batch_scrape",
|
|
24
|
+
"cancel_batch_scrape_impl",
|
|
25
|
+
"cancel_crawl",
|
|
26
|
+
"cancel_crawl_impl",
|
|
27
|
+
"get_job_results",
|
|
28
|
+
"get_job_results_impl",
|
|
29
|
+
"map",
|
|
30
|
+
"map_impl",
|
|
31
|
+
"scrape",
|
|
32
|
+
"scrape_impl",
|
|
33
|
+
"search",
|
|
34
|
+
"search_impl",
|
|
35
|
+
"start_batch_scrape",
|
|
36
|
+
"start_batch_scrape_impl",
|
|
37
|
+
"start_crawl",
|
|
38
|
+
"start_crawl_impl",
|
|
39
|
+
"start_extract",
|
|
40
|
+
"start_extract_impl",
|
|
41
|
+
]
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"""JSON contracts ported from the pinned TypeScript pack."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Literal, NotRequired, TypedDict
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ExtractResultSourcesItem(TypedDict):
|
|
9
|
+
url: str
|
|
10
|
+
title: str
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
type JsonPrimitive = str | float | bool | None
|
|
14
|
+
|
|
15
|
+
type JsonValue = JsonPrimitive | list[JsonValue] | dict[str, JsonValue]
|
|
16
|
+
|
|
17
|
+
type FirecrawlFormat = (
|
|
18
|
+
Literal["markdown"]
|
|
19
|
+
| Literal["html"]
|
|
20
|
+
| Literal["rawHtml"]
|
|
21
|
+
| Literal["links"]
|
|
22
|
+
| Literal["images"]
|
|
23
|
+
| Literal["summary"]
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
type FirecrawlJobKind = Literal["crawl"] | Literal["batch"]
|
|
27
|
+
|
|
28
|
+
type FirecrawlJobStatus = (
|
|
29
|
+
Literal["scraping"] | Literal["completed"] | Literal["failed"] | Literal["cancelled"]
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class FirecrawlDocument(TypedDict):
|
|
34
|
+
url: str
|
|
35
|
+
title: str
|
|
36
|
+
description: str
|
|
37
|
+
statusCode: float | None
|
|
38
|
+
markdown: NotRequired[str]
|
|
39
|
+
html: NotRequired[str]
|
|
40
|
+
rawHtml: NotRequired[str]
|
|
41
|
+
links: NotRequired[list[str]]
|
|
42
|
+
images: NotRequired[list[str]]
|
|
43
|
+
summary: NotRequired[str]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class ScrapeOptions(TypedDict):
|
|
47
|
+
formats: NotRequired[list[FirecrawlFormat]]
|
|
48
|
+
onlyMainContent: NotRequired[bool]
|
|
49
|
+
includeTags: NotRequired[list[str]]
|
|
50
|
+
excludeTags: NotRequired[list[str]]
|
|
51
|
+
maxAge: NotRequired[float]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class WebhookConfig(TypedDict):
|
|
55
|
+
url: str
|
|
56
|
+
events: NotRequired[
|
|
57
|
+
list[
|
|
58
|
+
Literal["started"]
|
|
59
|
+
| Literal["page"]
|
|
60
|
+
| Literal["completed"]
|
|
61
|
+
| Literal["failed"]
|
|
62
|
+
| Literal["cancelled"]
|
|
63
|
+
]
|
|
64
|
+
]
|
|
65
|
+
metadata: NotRequired[dict[str, str]]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class ScrapeInput(ScrapeOptions):
|
|
69
|
+
url: str
|
|
70
|
+
maxContentChars: float
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class ScrapeResult(TypedDict):
|
|
74
|
+
document: FirecrawlDocument
|
|
75
|
+
warning: NotRequired[str]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class MapInput(TypedDict):
|
|
79
|
+
url: str
|
|
80
|
+
search: NotRequired[str]
|
|
81
|
+
limit: float
|
|
82
|
+
sitemap: NotRequired[Literal["include"] | Literal["skip"] | Literal["only"]]
|
|
83
|
+
includeSubdomains: NotRequired[bool]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class MapLink(TypedDict):
|
|
87
|
+
url: str
|
|
88
|
+
title: str
|
|
89
|
+
description: str
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class MapResult(TypedDict):
|
|
93
|
+
links: list[MapLink]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class SearchInput(TypedDict):
|
|
97
|
+
query: str
|
|
98
|
+
limit: float
|
|
99
|
+
sources: NotRequired[list[Literal["web"] | Literal["news"]]]
|
|
100
|
+
scrapeOptions: NotRequired[ScrapeOptions]
|
|
101
|
+
maxContentChars: float
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class SearchHit(TypedDict):
|
|
105
|
+
source: Literal["web"] | Literal["news"]
|
|
106
|
+
url: str
|
|
107
|
+
title: str
|
|
108
|
+
description: str
|
|
109
|
+
position: float | None
|
|
110
|
+
document: NotRequired[FirecrawlDocument]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class SearchResult(TypedDict):
|
|
114
|
+
hits: list[SearchHit]
|
|
115
|
+
warning: NotRequired[str]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class FirecrawlJobRef(TypedDict):
|
|
119
|
+
jobId: str
|
|
120
|
+
kind: FirecrawlJobKind
|
|
121
|
+
statusUrl: str
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class StartCrawlInput(TypedDict):
|
|
125
|
+
url: str
|
|
126
|
+
limit: float
|
|
127
|
+
maxDepth: NotRequired[float]
|
|
128
|
+
includePaths: NotRequired[list[str]]
|
|
129
|
+
excludePaths: NotRequired[list[str]]
|
|
130
|
+
allowExternalLinks: NotRequired[bool]
|
|
131
|
+
scrapeOptions: NotRequired[ScrapeOptions]
|
|
132
|
+
webhook: NotRequired[WebhookConfig]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class StartBatchScrapeInput(TypedDict):
|
|
136
|
+
urls: list[str]
|
|
137
|
+
maxConcurrency: NotRequired[float]
|
|
138
|
+
ignoreInvalidURLs: NotRequired[bool]
|
|
139
|
+
options: NotRequired[ScrapeOptions]
|
|
140
|
+
webhook: NotRequired[WebhookConfig]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class AwaitJobInput(TypedDict):
|
|
144
|
+
jobId: str
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class FirecrawlJobState(TypedDict):
|
|
148
|
+
jobId: str
|
|
149
|
+
kind: FirecrawlJobKind
|
|
150
|
+
status: FirecrawlJobStatus
|
|
151
|
+
completed: float
|
|
152
|
+
total: float
|
|
153
|
+
creditsUsed: float
|
|
154
|
+
expiresAt: str
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class CancelJobInput(TypedDict):
|
|
158
|
+
jobId: str
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class CancelJobResult(TypedDict):
|
|
162
|
+
jobId: str
|
|
163
|
+
kind: FirecrawlJobKind
|
|
164
|
+
status: Literal["cancelled"]
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class GetJobResultsInput(TypedDict):
|
|
168
|
+
jobId: str
|
|
169
|
+
kind: FirecrawlJobKind
|
|
170
|
+
cursor: NotRequired[str]
|
|
171
|
+
maxResults: float
|
|
172
|
+
maxContentChars: float
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
class JobResultsPage(TypedDict):
|
|
176
|
+
job: FirecrawlJobState
|
|
177
|
+
documents: list[FirecrawlDocument]
|
|
178
|
+
nextCursor: str | None
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class StartExtractInput(TypedDict):
|
|
182
|
+
urls: NotRequired[list[str]]
|
|
183
|
+
prompt: NotRequired[str]
|
|
184
|
+
schema: NotRequired[dict[str, JsonValue]]
|
|
185
|
+
enableWebSearch: NotRequired[bool]
|
|
186
|
+
ignoreInvalidURLs: NotRequired[bool]
|
|
187
|
+
webhook: NotRequired[WebhookConfig]
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class ExtractJobRef(TypedDict):
|
|
191
|
+
jobId: str
|
|
192
|
+
statusUrl: str
|
|
193
|
+
invalidURLs: list[str]
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
class AwaitExtractInput(TypedDict):
|
|
197
|
+
jobId: str
|
|
198
|
+
maxOutputBytes: float
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class ExtractResult(TypedDict):
|
|
202
|
+
jobId: str
|
|
203
|
+
status: Literal["completed"]
|
|
204
|
+
data: JsonValue
|
|
205
|
+
expiresAt: str
|
|
206
|
+
sources: NotRequired[list[ExtractResultSourcesItem]]
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Registration-free firecrawl webhook verification and event mapping."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import Callable
|
|
7
|
+
|
|
8
|
+
from render_lab_tasks_core.signatures import digest, safe_equal
|
|
9
|
+
from render_lab_triggers.types import Adapter, Json, WebhookContext, WebhookDispatch, WebhookRequest
|
|
10
|
+
|
|
11
|
+
type OnEvent = Callable[[dict[str, Json]], WebhookDispatch | None]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def verify_firecrawl_signature(secret: str, raw_body: str, signature: str | None) -> bool:
|
|
15
|
+
if not signature:
|
|
16
|
+
return False
|
|
17
|
+
expected = "sha256=" + digest(secret, raw_body, algorithm="sha256", encoding="hex")
|
|
18
|
+
return safe_equal(signature, expected)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def firecrawl_adapter(on_event: OnEvent, *, secret: str | None = None) -> Adapter:
|
|
22
|
+
def verify(req: WebhookRequest) -> bool:
|
|
23
|
+
key = secret if secret is not None else os.getenv("FIRECRAWL_WEBHOOK_SECRET")
|
|
24
|
+
return bool(key) and (
|
|
25
|
+
verify_firecrawl_signature(
|
|
26
|
+
key or "", req["rawBody"], req["headers"].get("x-firecrawl-signature")
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def map_event(ctx: WebhookContext) -> WebhookDispatch | None:
|
|
31
|
+
payload = ctx["body"] if ctx["body"] is not None else {}
|
|
32
|
+
fields = payload if isinstance(payload, dict) else {}
|
|
33
|
+
kind = fields.get("type")
|
|
34
|
+
if not isinstance(kind, str):
|
|
35
|
+
return None
|
|
36
|
+
family, _, event = kind.partition(".")
|
|
37
|
+
if family not in {"crawl", "batch_scrape", "extract"} or event not in {
|
|
38
|
+
"started",
|
|
39
|
+
"page",
|
|
40
|
+
"completed",
|
|
41
|
+
"failed",
|
|
42
|
+
"cancelled",
|
|
43
|
+
}:
|
|
44
|
+
return None
|
|
45
|
+
return on_event({"family": family, "event": event, "payload": payload})
|
|
46
|
+
|
|
47
|
+
return Adapter(verify, map_event)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: render-lab-tasks-firecrawl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: firecrawl tasks for Render Workflows
|
|
5
|
+
Project-URL: Repository, https://github.com/render-lab/render-tasks-python
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: httpx<0.29,>=0.28
|
|
10
|
+
Requires-Dist: render-lab-tasks-core<0.2,>=0.1.1
|
|
11
|
+
Requires-Dist: render-lab-triggers<0.2,>=0.1.0
|
|
12
|
+
Requires-Dist: render==1.0.1
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# render-lab-tasks-firecrawl
|
|
16
|
+
|
|
17
|
+
Unreleased Python port: **12 registered tasks** from
|
|
18
|
+
[render-lab/render-tasks](https://github.com/render-lab/render-tasks/tree/45f9c2d44bd28e01ae9813e0d2ff56ee6c533816/packages/tasks-firecrawl).
|
|
19
|
+
Python 3.12+, Render SDK 1.0.1. Requires core 0.1.1 or later.
|
|
20
|
+
|
|
21
|
+
From the repository root:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
uv sync --all-packages --locked
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Import `render_lab_tasks_firecrawl.tasks` to register tasks. The package root is inert.
|
|
28
|
+
Compose the exported `app` with `Workflows.from_workflows`; call tasks through
|
|
29
|
+
`ctx.run`. Every operation also exports `*_impl(ctx, input, *, deps=None)` for
|
|
30
|
+
injection. `Client(http, env)` accepts a caller-owned HTTPX client, and
|
|
31
|
+
`Deps(firecrawl=client)` injects it. Default dependencies open and close the HTTP
|
|
32
|
+
client per invocation; credentials are read lazily at use. No HTTP retries or
|
|
33
|
+
background vendor polling occur; `retry.py` owns the durable retry policies.
|
|
34
|
+
|
|
35
|
+
## Task surface
|
|
36
|
+
|
|
37
|
+
| Registered task | Python export |
|
|
38
|
+
| --- | --- |
|
|
39
|
+
| `firecrawl.awaitBatchScrape` | `await_batch_scrape` |
|
|
40
|
+
| `firecrawl.awaitCrawl` | `await_crawl` |
|
|
41
|
+
| `firecrawl.awaitExtract` | `await_extract` |
|
|
42
|
+
| `firecrawl.cancelBatchScrape` | `cancel_batch_scrape` |
|
|
43
|
+
| `firecrawl.cancelCrawl` | `cancel_crawl` |
|
|
44
|
+
| `firecrawl.getJobResults` | `get_job_results` |
|
|
45
|
+
| `firecrawl.map` | `map` |
|
|
46
|
+
| `firecrawl.scrape` | `scrape` |
|
|
47
|
+
| `firecrawl.search` | `search` |
|
|
48
|
+
| `firecrawl.startBatchScrape` | `start_batch_scrape` |
|
|
49
|
+
| `firecrawl.startCrawl` | `start_crawl` |
|
|
50
|
+
| `firecrawl.startExtract` | `start_extract` |
|
|
51
|
+
|
|
52
|
+
Typed JSON inputs and results are in `types.py`. Task names and JSON field names
|
|
53
|
+
match the pinned source; Python function names use snake_case.
|
|
54
|
+
|
|
55
|
+
## Environment
|
|
56
|
+
|
|
57
|
+
| Variable | Requirement |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| `FIRECRAWL_API_KEY` | Required at first API call. |
|
|
60
|
+
|
|
61
|
+
## Behavior and limits
|
|
62
|
+
|
|
63
|
+
Uses Firecrawl v2. Each await makes one status check per durable attempt. Crawl and batch cancellation normalize 404. Returned content budgets use JavaScript UTF-16 character counts. Cursor requests are restricted to HTTPS api.firecrawl.dev before attaching credentials (ADR-0010). The inherited getJobResults maxResults slice can discard excess documents in a vendor page; use a page-sized maximum until cursor preservation is verified. Unknown extract statuses currently follow the source completion mapping; vendor status changes need live verification.
|
|
64
|
+
|
|
65
|
+
## Verification
|
|
66
|
+
|
|
67
|
+
All registered tasks have hermetic contract fixtures executed independently against
|
|
68
|
+
the pinned TS implementation. SDK-backed tasks also have explicit HTTP request
|
|
69
|
+
fixtures. See `tests/test_batch3_contracts.py` and `tests/test_batch3_edges.py`.
|
|
70
|
+
These are not live vendor results.
|
|
71
|
+
|
|
72
|
+
Vendor webhooks subpaths and other registration-free TS exports are outside this
|
|
73
|
+
batch unless explicitly listed above. Full registered-task coverage does not
|
|
74
|
+
imply all supporting exports are ported.
|
|
75
|
+
|
|
76
|
+
Pending scopes, test resources, replay, and hosted checks are in the
|
|
77
|
+
[live-testing backlog](https://github.com/render-lab/render-tasks-python/issues/1)
|
|
78
|
+
and [verification tracker](../../docs/verification-tracker.md).
|
|
79
|
+
|
|
80
|
+
## Webhook adapter
|
|
81
|
+
|
|
82
|
+
`render_lab_tasks_firecrawl.webhooks.firecrawl_adapter(on_event, ...)` returns a
|
|
83
|
+
registration-free adapter for `render_lab_triggers`. The callback receives the
|
|
84
|
+
verified event and returns `{"task": "namespace.task", "args": [event]}` or `None`.
|
|
85
|
+
The signing credential `FIRECRAWL_WEBHOOK_SECRET` is read inside verification unless
|
|
86
|
+
provided explicitly. Importing this module does not register tasks. Signature
|
|
87
|
+
checks use the raw body; live delivery remains in the testing backlog.
|
|
88
|
+
|
|
89
|
+
## Installation
|
|
90
|
+
|
|
91
|
+
```sh
|
|
92
|
+
pip install render-lab-tasks-firecrawl==0.1.0
|
|
93
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
render_lab_tasks_firecrawl/__init__.py,sha256=fGcyw3Sxjue9wB_1RdFbEFKm5DXSlw_6vD2ud03a6og,58
|
|
2
|
+
render_lab_tasks_firecrawl/_app.py,sha256=nkWSD_6eq3xzdPJ_G7z6d5mv8tSB-Rq21yp07usJihA,48
|
|
3
|
+
render_lab_tasks_firecrawl/await_batch_scrape.py,sha256=MmIfRfxZjlaKbxNI1oQ3rCWVRvk-3lPPdWcFT7UBAZw,652
|
|
4
|
+
render_lab_tasks_firecrawl/await_crawl.py,sha256=NWARcWGCG94yCrJsb2IpaxCKRpwFkpWMl0LjrGQEJ-s,618
|
|
5
|
+
render_lab_tasks_firecrawl/await_extract.py,sha256=6q3OcOLwiraZavigq-BuGx-MURuDFXAACqO8YtVkl70,628
|
|
6
|
+
render_lab_tasks_firecrawl/cancel_batch_scrape.py,sha256=P_5GoRvSkVqySGPF2j2PoRiCwvgVTlFQX-Ds694LwTQ,656
|
|
7
|
+
render_lab_tasks_firecrawl/cancel_crawl.py,sha256=YVoXZ6TckFffPp1we0sorT5i6mxHQQmZfEWT5s5C-XE,622
|
|
8
|
+
render_lab_tasks_firecrawl/client.py,sha256=vULi2eeseOGv2Awk7oI6se3pkqT27Co3ieb1WOTP5d0,12856
|
|
9
|
+
render_lab_tasks_firecrawl/get_job_results.py,sha256=qg2LDU1SHUZ5sDDH1Qa_5nL_wIsFVaMY4Ip9V8lL9Ng,631
|
|
10
|
+
render_lab_tasks_firecrawl/map.py,sha256=bT408UrPwUHDFZZnsxwt8Juv_f-EJ-2sbbDVLWs6sFo,522
|
|
11
|
+
render_lab_tasks_firecrawl/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
render_lab_tasks_firecrawl/retry.py,sha256=9RjlO8C-N0cSXNnNxq6gc5p8HAEBgLR7sYpAgzxWQPs,371
|
|
13
|
+
render_lab_tasks_firecrawl/scrape.py,sha256=GfrPpSPDJu9DuJLDLuKvZXQa1HSsqcEkh7RvePH2c1I,561
|
|
14
|
+
render_lab_tasks_firecrawl/search.py,sha256=YpAgbfObUkjZQv_CJemE2fjk5Ciaan3baN7wYLp-X3M,561
|
|
15
|
+
render_lab_tasks_firecrawl/start_batch_scrape.py,sha256=s97ipgEIPv_mlk46D2V_8ZLXfBqDK8z1c2hnHBu2RRU,674
|
|
16
|
+
render_lab_tasks_firecrawl/start_crawl.py,sha256=HCdUPhAAAUY3VBQ-pEpEfqRK71KPjgNkMSc_5FZRYvk,622
|
|
17
|
+
render_lab_tasks_firecrawl/start_extract.py,sha256=bXRcqP5JiV8paFHW3d6ZmHZLj69ZcZaCnIFZQhtNMgc,632
|
|
18
|
+
render_lab_tasks_firecrawl/tasks.py,sha256=NFjDWKuRB34GiEw80BKlA1LjHj4lZwOqj1orkwpl5gc,1289
|
|
19
|
+
render_lab_tasks_firecrawl/types.py,sha256=DsOlxBval3tJGQUoNbxsgA8AnOLqevj9yrgjqBTydOg,4473
|
|
20
|
+
render_lab_tasks_firecrawl/webhooks.py,sha256=l5bnEBNuIqOxiPGqpWSbJ3AfZX6xmkQ1L0jUTEJNDo0,1743
|
|
21
|
+
render_lab_tasks_firecrawl-0.1.0.dist-info/METADATA,sha256=4qD7PiANUhcDFoRO3GvGGG2CxHx7uO2_pvl5nVt5PTU,4108
|
|
22
|
+
render_lab_tasks_firecrawl-0.1.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
23
|
+
render_lab_tasks_firecrawl-0.1.0.dist-info/licenses/LICENSE,sha256=LvmsuKGkhvlSISJRqvBIPUQ_BqVDFRe0RghPSmDebzU,1067
|
|
24
|
+
render_lab_tasks_firecrawl-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Render Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|