pi-auto-web-search 0.1.1

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.
@@ -0,0 +1,139 @@
1
+ import type { ResolvedInstance } from "../config.ts";
2
+ import type { ProviderResponse, RequestSpec } from "../http.ts";
3
+ import type { ExtractOutput, ExtractParams, SearchAdapter, SearchHit, SearchOutput, SearchParams } from "./types.ts";
4
+ import { asArray, asRecord, asString, businessFailure, countryGapNote, qualityGapNote, rejectNoMap, rejectUnsupported, sourceModeGapNote, topicGapNote } from "./util.ts";
5
+
6
+ const MAX_RESULTS = 10;
7
+
8
+ /** 请求体必须带网关 id;每个账号有名为 default 的网关。 */
9
+ const GATEWAY_ID = "default";
10
+
11
+ const EXTRACT_PATH = {
12
+ markdown: "markdown",
13
+ text: "markdown",
14
+ html: "content",
15
+ json: "json",
16
+ links: "links",
17
+ } as const;
18
+
19
+ /** 域名、时间与答案过滤都没有对应请求字段。 */
20
+ function unsupportedParams(params: SearchParams): string[] {
21
+ const names: string[] = [];
22
+ if (params.domains?.length) names.push("domains");
23
+ if (params.excludeDomains?.length) names.push("excludeDomains");
24
+ if (params.freshness) names.push("freshness");
25
+ if (params.answer) names.push("answer");
26
+ return names;
27
+ }
28
+
29
+ /** Cloudflare 用 success 判成败,错误在 errors[] 里;返回 result 原文。 */
30
+ function resultOf(response: ProviderResponse): unknown {
31
+ const body = asRecord(response.data);
32
+ if (!body) throw businessFailure(response, "cloudflare 响应不是 JSON 对象");
33
+ if (body.success === false) {
34
+ const first = asRecord(asArray(body.errors)[0]);
35
+ const code = first?.code;
36
+ const label = code === undefined ? "" : `(${String(code)})`;
37
+ const message = asString(first?.message) ?? "未知错误";
38
+ throw businessFailure(response, `cloudflare 返回失败${label}:${message}`);
39
+ }
40
+ return body.result;
41
+ }
42
+
43
+ /** 按 format 取 result 正文;缺字段时返回 undefined,由 checkExtract 判失败。 */
44
+ function extractContent(result: unknown, params: ExtractParams): string | undefined {
45
+ if (params.format === "json") {
46
+ if (result == null) return undefined;
47
+ return JSON.stringify(result);
48
+ }
49
+ if (params.format === "links") {
50
+ if (!Array.isArray(result)) return undefined;
51
+ return JSON.stringify(result);
52
+ }
53
+ return asString(result);
54
+ }
55
+
56
+ function hitOf(value: unknown): SearchHit | undefined {
57
+ const item = asRecord(value);
58
+ if (!item) return undefined;
59
+ const url = asString(item.url);
60
+ if (!url) return undefined;
61
+
62
+ const hit: SearchHit = { title: asString(item.title) ?? url, url };
63
+ const description = asString(item.description);
64
+ if (description) hit.snippet = description;
65
+ return hit;
66
+ }
67
+
68
+ /** Cloudflare:Bearer 鉴权,Web Search 搜网页,Browser Rendering 按 format 取正文。 */
69
+ export const cloudflareAdapter: SearchAdapter = {
70
+ preset: "cloudflare",
71
+ capabilities: { search: true, fetch: true, map: false },
72
+
73
+ unsupported: unsupportedParams,
74
+
75
+ buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
76
+ rejectUnsupported(instance, "cloudflare", unsupportedParams(params));
77
+
78
+ const body: Record<string, unknown> = {
79
+ query: params.query,
80
+ limit: Math.min(params.limit, MAX_RESULTS),
81
+ options: { gateway: { id: GATEWAY_ID } },
82
+ };
83
+ return { instance, path: "search", method: "POST", body };
84
+ },
85
+
86
+ checkSearch(response: ProviderResponse): void {
87
+ const result = asRecord(resultOf(response));
88
+ if (!result || !Array.isArray(result.items)) {
89
+ throw businessFailure(response, "cloudflare 响应缺少 result.items");
90
+ }
91
+ },
92
+
93
+ mapSearch(response: ProviderResponse, params: SearchParams): SearchOutput {
94
+ const result = asRecord(resultOf(response)) ?? {};
95
+ const hits = asArray(result.items)
96
+ .map((item) => hitOf(item))
97
+ .filter((hit): hit is SearchHit => hit !== undefined);
98
+ const notes: string[] = [];
99
+ const gap = qualityGapNote("cloudflare", params.quality);
100
+ if (gap) notes.push(gap);
101
+ if (params.sourceMode) notes.push(sourceModeGapNote("cloudflare", params.sourceMode));
102
+ const topicGap = topicGapNote("cloudflare", params.topic);
103
+ if (topicGap) notes.push(topicGap);
104
+ const countryGap = countryGapNote("cloudflare", params.country);
105
+ if (countryGap) notes.push(countryGap);
106
+ return { hits, notes, rateLimit: response.rateLimit };
107
+ },
108
+
109
+ buildExtract(instance: ResolvedInstance, params: ExtractParams): RequestSpec {
110
+ const body: Record<string, unknown> = { url: params.url };
111
+ if (params.format === "json") {
112
+ body.response_format = { type: "json_schema", json_schema: params.schema };
113
+ }
114
+ return { instance, path: EXTRACT_PATH[params.format], method: "POST", body };
115
+ },
116
+
117
+ checkExtract(response: ProviderResponse, params: ExtractParams): void {
118
+ if (extractContent(resultOf(response), params) === undefined) {
119
+ throw businessFailure(response, `cloudflare 抓取未返回 ${params.format}`, "network");
120
+ }
121
+ },
122
+
123
+ mapExtract(response: ProviderResponse, params: ExtractParams): ExtractOutput {
124
+ return { content: extractContent(resultOf(response), params) ?? "" };
125
+ },
126
+
127
+ /** 已兑现 markdown、html、json 与 links,不按格式裁链。 */
128
+ unsupportedExtract() {
129
+ return [];
130
+ },
131
+
132
+ buildMap(instance) {
133
+ rejectNoMap(instance.id, "cloudflare");
134
+ },
135
+
136
+ mapSite() {
137
+ rejectNoMap("cloudflare", "cloudflare");
138
+ },
139
+ };
@@ -0,0 +1,108 @@
1
+ import type { ResolvedInstance } from "../config.ts";
2
+ import type { ProviderResponse, RequestSpec } from "../http.ts";
3
+ import type { ExtractOutput, ExtractParams, SearchAdapter, SearchHit, SearchOutput, SearchParams } from "./types.ts";
4
+ import { asArray, asNumber, asRecord, asString, businessFailure, countryGapNote, markdownOnlyExtract, qualityGapNote, rejectNoMap, rejectUnsupported, sourceModeGapNote, topicGapNote } from "./util.ts";
5
+
6
+ const MAX_RESULTS = 50;
7
+
8
+ /** 官方只接受 query、count、need_content 三个搜索字段,域名与时间过滤靠 query 操作符。 */
9
+ function unsupportedParams(params: SearchParams): string[] {
10
+ const names: string[] = [];
11
+ if (params.domains?.length) names.push("domains");
12
+ if (params.excludeDomains?.length) names.push("excludeDomains");
13
+ if (params.freshness) names.push("freshness");
14
+ if (params.answer) names.push("answer");
15
+ return names;
16
+ }
17
+
18
+ function hitOf(value: unknown, params: SearchParams): SearchHit | undefined {
19
+ const item = asRecord(value);
20
+ if (!item) return undefined;
21
+ const url = asString(item.url);
22
+ if (!url) return undefined;
23
+
24
+ const summary = asString(item.summary);
25
+ const hit: SearchHit = { title: asString(item.title) ?? url, url };
26
+ const snippet = asString(item.snippet);
27
+ if (snippet) hit.snippet = snippet;
28
+
29
+ if (params.contents !== "none") {
30
+ const content = params.contents === "markdown" ? asString(item.content) : undefined;
31
+ const body = content ?? summary;
32
+ if (body) hit.content = body;
33
+ }
34
+
35
+ const date = asString(item.date);
36
+ if (date) hit.date = date;
37
+ const score = asNumber(item.rerank_score);
38
+ if (score !== undefined) hit.score = score;
39
+ return hit;
40
+ }
41
+
42
+ /** DeepSeek:Bearer 鉴权,搜索按 results 映射,fetch 返回 markdown 正文。 */
43
+ export const deepseekAdapter: SearchAdapter = {
44
+ preset: "deepseek",
45
+ capabilities: { search: true, fetch: true, map: false },
46
+
47
+ unsupported: unsupportedParams,
48
+
49
+ buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
50
+ rejectUnsupported(instance, "deepseek", unsupportedParams(params));
51
+
52
+ const body: Record<string, unknown> = {
53
+ query: params.query,
54
+ count: Math.min(params.limit, MAX_RESULTS),
55
+ need_content: params.contents === "markdown",
56
+ };
57
+ return { instance, path: "search", method: "POST", body };
58
+ },
59
+
60
+ checkSearch(response: ProviderResponse): void {
61
+ const data = asRecord(response.data);
62
+ if (!data || !Array.isArray(data.results)) {
63
+ throw businessFailure(response, "deepseek 响应缺少 results");
64
+ }
65
+ },
66
+
67
+ mapSearch(response: ProviderResponse, params: SearchParams): SearchOutput {
68
+ const data = asRecord(response.data) ?? {};
69
+ const hits = asArray(data.results)
70
+ .map((item) => hitOf(item, params))
71
+ .filter((hit): hit is SearchHit => hit !== undefined);
72
+ const notes: string[] = [];
73
+ const gap = qualityGapNote("deepseek", params.quality);
74
+ if (gap) notes.push(gap);
75
+ if (params.sourceMode) notes.push(sourceModeGapNote("deepseek", params.sourceMode));
76
+ const topicGap = topicGapNote("deepseek", params.topic);
77
+ if (topicGap) notes.push(topicGap);
78
+ const countryGap = countryGapNote("deepseek", params.country);
79
+ if (countryGap) notes.push(countryGap);
80
+ return { hits, notes, rateLimit: response.rateLimit };
81
+ },
82
+
83
+ buildExtract(instance: ResolvedInstance, params: ExtractParams): RequestSpec {
84
+ return { instance, path: "fetch", method: "POST", body: { url: params.url } };
85
+ },
86
+
87
+ checkExtract(response: ProviderResponse): void {
88
+ const data = asRecord(response.data);
89
+ if (!data || !asString(data.content)) {
90
+ throw businessFailure(response, "deepseek 抓取未返回正文", "network");
91
+ }
92
+ },
93
+
94
+ mapExtract(response: ProviderResponse): ExtractOutput {
95
+ const data = asRecord(response.data) ?? {};
96
+ return { title: asString(data.title), content: asString(data.content) ?? "" };
97
+ },
98
+
99
+ unsupportedExtract: markdownOnlyExtract,
100
+
101
+ buildMap(instance) {
102
+ rejectNoMap(instance.id, "deepseek");
103
+ },
104
+
105
+ mapSite() {
106
+ rejectNoMap("deepseek", "deepseek");
107
+ },
108
+ };
@@ -0,0 +1,161 @@
1
+ import type { ResolvedInstance } from "../config.ts";
2
+ import type { ProviderResponse, RequestSpec } from "../http.ts";
3
+ import type {
4
+ ExtractOutput,
5
+ ExtractParams,
6
+ SearchAdapter,
7
+ SearchHit,
8
+ SearchOutput,
9
+ SearchParams,
10
+ SearchQuality,
11
+ } from "./types.ts";
12
+ import { asArray, asNumber, asRecord, asString, businessFailure, markdownOnlyExtract, rejectNoMap, rejectUnsupported, sourceModeGapNote } from "./util.ts";
13
+
14
+ const MAX_RESULTS = 100;
15
+
16
+ /** 质量档位映射到 Exa 的搜索类型。 */
17
+ const QUALITY_TYPES: Record<SearchQuality, "lite" | "auto" | "deep"> = {
18
+ basic: "lite",
19
+ advanced: "auto",
20
+ deep: "deep",
21
+ };
22
+
23
+ /** 请求正文时按官方 schema 固定 maxAgeHours: 0,正文始终实时抓取。 */
24
+ function contentsOf(params: SearchParams): Record<string, unknown> | undefined {
25
+ if (params.contents === "markdown") return { text: true, maxAgeHours: 0 };
26
+ if (params.contents === "summary") return { summary: { query: params.query }, maxAgeHours: 0 };
27
+ return undefined;
28
+ }
29
+
30
+ /** lite 只接受官方列出的参数,contents.maxAgeHours 不在其中,请求正文时省掉 type 按 auto 请求。 */
31
+ function qualityType(params: SearchParams): { type?: "lite" | "auto" | "deep"; note?: string } {
32
+ if (params.quality === "basic" && contentsOf(params) !== undefined) {
33
+ return { note: "exa 的 lite 不支持 contents.maxAgeHours,quality=basic 已按 auto 请求" };
34
+ }
35
+ return { type: QUALITY_TYPES[params.quality] };
36
+ }
37
+
38
+ /** 官方 startPublishedDate 的精度是天,按 freshness 回溯固定天数。 */
39
+ const FRESHNESS_DAYS = { day: 1, week: 7, month: 30, year: 365 } as const;
40
+
41
+ /** 搜索结果里没有 answer 字段,答案来自独立的 /answer 端点。 */
42
+ function unsupportedParams(params: SearchParams): string[] {
43
+ return params.answer ? ["answer"] : [];
44
+ }
45
+
46
+ function publishedAfter(params: SearchParams, now: number): string | undefined {
47
+ if (!params.freshness) return undefined;
48
+ const days = FRESHNESS_DAYS[params.freshness];
49
+ return new Date(now - days * 24 * 60 * 60 * 1000).toISOString();
50
+ }
51
+
52
+ function hitOf(value: unknown, params: SearchParams): SearchHit | undefined {
53
+ const item = asRecord(value);
54
+ if (!item) return undefined;
55
+ const url = asString(item.url);
56
+ if (!url) return undefined;
57
+
58
+ const hit: SearchHit = { title: asString(item.title) ?? url, url };
59
+ const summary = asString(item.summary);
60
+ if (summary) hit.snippet = summary;
61
+
62
+ if (params.contents === "markdown") {
63
+ const text = asString(item.text);
64
+ if (text) hit.content = text;
65
+ } else if (params.contents === "summary" && summary) {
66
+ hit.content = summary;
67
+ }
68
+
69
+ const date = asString(item.publishedDate);
70
+ if (date) hit.date = date;
71
+ const score = asNumber(item.score);
72
+ if (score !== undefined) hit.score = score;
73
+ return hit;
74
+ }
75
+
76
+ /** Exa:x-api-key 鉴权,search 返回 results,contents 按 ids 抓正文。 */
77
+ export const exaAdapter: SearchAdapter = {
78
+ preset: "exa",
79
+ capabilities: { search: true, fetch: true, map: false },
80
+
81
+ unsupported: unsupportedParams,
82
+
83
+ buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
84
+ rejectUnsupported(instance, "exa", unsupportedParams(params));
85
+
86
+ const body: Record<string, unknown> = {
87
+ query: params.query,
88
+ numResults: Math.min(params.limit, MAX_RESULTS),
89
+ };
90
+ const type = qualityType(params).type;
91
+ if (type) body.type = type;
92
+ // 官方 category 没有默认值,只有 news 需要显式分类。
93
+ if (params.topic === "news") body.category = "news";
94
+ // userLocation.country 只做结果本地化,不做硬地域过滤;lite 也接受该字段。
95
+ if (params.country) body.userLocation = { country: params.country };
96
+ if (params.domains?.length) body.includeDomains = params.domains;
97
+ if (params.excludeDomains?.length) body.excludeDomains = params.excludeDomains;
98
+ const after = publishedAfter(params, Date.now());
99
+ if (after) body.startPublishedDate = after;
100
+ const contents = contentsOf(params);
101
+ if (contents) body.contents = contents;
102
+ return { instance, path: "search", method: "POST", body };
103
+ },
104
+
105
+ checkSearch(response: ProviderResponse): void {
106
+ const data = asRecord(response.data);
107
+ if (!data || !Array.isArray(data.results)) {
108
+ throw businessFailure(response, "exa 响应缺少 results");
109
+ }
110
+ },
111
+
112
+ mapSearch(response: ProviderResponse, params: SearchParams): SearchOutput {
113
+ const data = asRecord(response.data) ?? {};
114
+ const hits = asArray(data.results)
115
+ .map((item) => hitOf(item, params))
116
+ .filter((hit): hit is SearchHit => hit !== undefined);
117
+ const notes: string[] = [];
118
+ const note = qualityType(params).note;
119
+ if (note !== undefined) notes.push(note);
120
+ if (params.sourceMode) notes.push(sourceModeGapNote("exa", params.sourceMode));
121
+ return { hits, notes, rateLimit: response.rateLimit };
122
+ },
123
+
124
+ buildExtract(instance: ResolvedInstance, params: ExtractParams): RequestSpec {
125
+ // Contents 的 HTTP 请求体用 ids 传 URL 或搜索结果里的文档 id。
126
+ // maxAgeHours: 0 让 /contents 始终抓取最新正文,不走缓存。
127
+ const body = { ids: [params.url], text: true, maxAgeHours: 0 };
128
+ return { instance, path: "contents", method: "POST", body };
129
+ },
130
+
131
+ checkExtract(response: ProviderResponse): void {
132
+ const data = asRecord(response.data);
133
+ if (!data) throw businessFailure(response, "exa 响应格式无法解析");
134
+
135
+ const status = asRecord(asArray(data.statuses)[0]);
136
+ const state = asString(status?.status);
137
+ if (state !== undefined && state !== "success") {
138
+ const reason = asString(status?.error) ?? state;
139
+ throw businessFailure(response, `exa 抓取失败:${reason}`, "network");
140
+ }
141
+ if (!asString(asRecord(asArray(data.results)[0])?.text)) {
142
+ throw businessFailure(response, "exa 抓取未返回正文", "network");
143
+ }
144
+ },
145
+
146
+ mapExtract(response: ProviderResponse): ExtractOutput {
147
+ const data = asRecord(response.data) ?? {};
148
+ const first = asRecord(asArray(data.results)[0]) ?? {};
149
+ return { title: asString(first.title), content: asString(first.text) ?? "" };
150
+ },
151
+
152
+ unsupportedExtract: markdownOnlyExtract,
153
+
154
+ buildMap(instance) {
155
+ rejectNoMap(instance.id, "exa");
156
+ },
157
+
158
+ mapSite() {
159
+ rejectNoMap("exa", "exa");
160
+ },
161
+ };
@@ -0,0 +1,202 @@
1
+ import type { ResolvedInstance } from "../config.ts";
2
+ import { ProviderError, type ProviderResponse, type RequestSpec } from "../http.ts";
3
+ import type {
4
+ ExtractOutput,
5
+ ExtractParams,
6
+ MapLink,
7
+ MapOutput,
8
+ MapParams,
9
+ SearchAdapter,
10
+ SearchHit,
11
+ SearchOutput,
12
+ SearchParams,
13
+ } from "./types.ts";
14
+ import { asArray, asRecord, asString, businessFailure, qualityGapNote, rejectUnsupported, sourceModeGapNote } from "./util.ts";
15
+
16
+ const MAX_RESULTS = 100;
17
+
18
+ /** 官方 tbs 的预置时间范围。 */
19
+ const FRESHNESS_TBS = { day: "qdr:d", week: "qdr:w", month: "qdr:m", year: "qdr:y" } as const;
20
+
21
+ /** 搜索响应里没有 answer 字段。 */
22
+ function unsupportedParams(params: SearchParams): string[] {
23
+ return params.answer ? ["answer"] : [];
24
+ }
25
+
26
+ /** 上游用 success 判成败,错误在 error 与 code 里。 */
27
+ function firecrawlBody(response: ProviderResponse): Record<string, unknown> {
28
+ const body = asRecord(response.data);
29
+ if (!body) throw businessFailure(response, "firecrawl 响应不是 JSON 对象");
30
+ if (body.success === false) {
31
+ const code = asString(body.code);
32
+ const message = asString(body.error) ?? "未知错误";
33
+ throw businessFailure(response, `firecrawl 返回失败${code ? `(${code})` : ""}:${message}`);
34
+ }
35
+ return body;
36
+ }
37
+
38
+ /** 搜索与 scrape 把有效载荷放在 data 里;map 没有这层信封。 */
39
+ function envelope(response: ProviderResponse): Record<string, unknown> {
40
+ return asRecord(firecrawlBody(response).data) ?? {};
41
+ }
42
+
43
+ function mapLinkOf(value: unknown): MapLink | undefined {
44
+ const item = asRecord(value);
45
+ if (!item) return undefined;
46
+ const url = asString(item.url);
47
+ if (!url) return undefined;
48
+ const link: MapLink = { url };
49
+ const title = asString(item.title);
50
+ if (title) link.title = title;
51
+ const description = asString(item.description);
52
+ if (description) link.description = description;
53
+ return link;
54
+ }
55
+
56
+ function hitOf(value: unknown, params: SearchParams): SearchHit | undefined {
57
+ const item = asRecord(value);
58
+ if (!item) return undefined;
59
+ const url = asString(item.url);
60
+ if (!url) return undefined;
61
+
62
+ // web 源的摘要字段是 description,news 源是 snippet,时间只在 news 源出现。
63
+ const summary = asString(item.snippet) ?? asString(item.description);
64
+ const hit: SearchHit = { title: asString(item.title) ?? url, url };
65
+ if (summary) hit.snippet = summary;
66
+
67
+ if (params.contents === "markdown") {
68
+ const markdown = asString(item.markdown);
69
+ if (markdown) hit.content = markdown;
70
+ } else if (params.contents !== "none" && summary) {
71
+ hit.content = summary;
72
+ }
73
+ const date = asString(item.date);
74
+ if (date) hit.date = date;
75
+ return hit;
76
+ }
77
+
78
+ /** 主题决定响应数组名:news 源把结果放在 data.news,其余放在 data.web。 */
79
+ function sourceKey(params: SearchParams): "web" | "news" {
80
+ return params.topic === "news" ? "news" : "web";
81
+ }
82
+
83
+ /** 按 format 取 scrape 正文;缺字段时返回 undefined,由 checkExtract 判失败。 */
84
+ function extractContent(data: Record<string, unknown>, params: ExtractParams): string | undefined {
85
+ if (params.format === "json") {
86
+ if (data.json === undefined) return undefined;
87
+ return JSON.stringify(data.json);
88
+ }
89
+ if (params.format === "links") {
90
+ if (!Array.isArray(data.links)) return undefined;
91
+ return JSON.stringify(data.links);
92
+ }
93
+ return asString(params.format === "html" ? data.html : data.markdown);
94
+ }
95
+
96
+ /** Firecrawl:Bearer 鉴权,/v2/search 搜网页,/v2/scrape 按 format 取正文。 */
97
+ export const firecrawlAdapter: SearchAdapter = {
98
+ preset: "firecrawl",
99
+ capabilities: { search: true, fetch: true, map: true },
100
+
101
+ unsupported: unsupportedParams,
102
+
103
+ buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
104
+ rejectUnsupported(instance, "firecrawl", unsupportedParams(params));
105
+ if (params.domains?.length && params.excludeDomains?.length) {
106
+ throw new ProviderError({
107
+ instance: instance.id,
108
+ code: "invalidRequest",
109
+ message: "firecrawl 不能同时指定 domains 与 excludeDomains",
110
+ });
111
+ }
112
+
113
+ const body: Record<string, unknown> = {
114
+ query: params.query,
115
+ limit: Math.min(params.limit, MAX_RESULTS),
116
+ sources: [sourceKey(params)],
117
+ };
118
+ if (params.domains?.length) body.includeDomains = params.domains;
119
+ if (params.excludeDomains?.length) body.excludeDomains = params.excludeDomains;
120
+ if (params.freshness) body.tbs = FRESHNESS_TBS[params.freshness];
121
+ // 官方 country 只做结果地域加权,不传时保持供应商默认。
122
+ if (params.country) body.country = params.country;
123
+ // maxAge: 0 表示正文始终实时抓取,不走官方默认的 2 天缓存。
124
+ if (params.contents === "markdown") body.scrapeOptions = { formats: ["markdown"], maxAge: 0 };
125
+ return { instance, path: "search", method: "POST", body };
126
+ },
127
+
128
+ checkSearch(response: ProviderResponse, params: SearchParams): void {
129
+ const data = envelope(response);
130
+ const key = sourceKey(params);
131
+ if (!Array.isArray(data[key])) {
132
+ throw businessFailure(response, `firecrawl 响应缺少 data.${key}`);
133
+ }
134
+ },
135
+
136
+ mapSearch(response: ProviderResponse, params: SearchParams): SearchOutput {
137
+ const data = envelope(response);
138
+ const hits = asArray(data[sourceKey(params)])
139
+ .map((item) => hitOf(item, params))
140
+ .filter((hit): hit is SearchHit => hit !== undefined);
141
+ const notes: string[] = [];
142
+ const gap = qualityGapNote("firecrawl", params.quality);
143
+ if (gap) notes.push(gap);
144
+ if (params.sourceMode) notes.push(sourceModeGapNote("firecrawl", params.sourceMode));
145
+ return { hits, notes, rateLimit: response.rateLimit };
146
+ },
147
+
148
+ buildExtract(instance: ResolvedInstance, params: ExtractParams): RequestSpec {
149
+ // maxAge: 0 同样让单页抓取始终实时,不用缓存正文。
150
+ const formats =
151
+ params.format === "json"
152
+ ? [{ type: "json", schema: params.schema }]
153
+ : [params.format === "text" ? "markdown" : params.format];
154
+ return { instance, path: "scrape", method: "POST", body: { url: params.url, formats, maxAge: 0 } };
155
+ },
156
+
157
+ checkExtract(response: ProviderResponse, params: ExtractParams): void {
158
+ const data = envelope(response);
159
+ if (extractContent(data, params) === undefined) {
160
+ throw businessFailure(response, `firecrawl 抓取未返回 ${params.format}`, "network");
161
+ }
162
+ },
163
+
164
+ mapExtract(response: ProviderResponse, params: ExtractParams): ExtractOutput {
165
+ const data = envelope(response);
166
+ const metadata = asRecord(data.metadata) ?? {};
167
+ return { title: asString(metadata.title), content: extractContent(data, params) ?? "" };
168
+ },
169
+
170
+ /** 已兑现 markdown、html、json 与 links,不按格式裁链。 */
171
+ unsupportedExtract() {
172
+ return [];
173
+ },
174
+
175
+ buildMap(instance: ResolvedInstance, params: MapParams): RequestSpec {
176
+ return {
177
+ instance,
178
+ path: "map",
179
+ method: "POST",
180
+ body: { url: params.url, limit: params.limit },
181
+ kind: "crawl",
182
+ };
183
+ },
184
+
185
+ checkMap(response: ProviderResponse): void {
186
+ const body = firecrawlBody(response);
187
+ if (!Array.isArray(body.links)) {
188
+ throw businessFailure(response, "firecrawl 响应缺少 links");
189
+ }
190
+ },
191
+
192
+ mapSite(response: ProviderResponse, params: MapParams): MapOutput {
193
+ const body = asRecord(response.data) ?? {};
194
+ const links = asArray(body.links)
195
+ .map((item) => mapLinkOf(item))
196
+ .filter((link): link is MapLink => link !== undefined);
197
+ const notes = params.instructions
198
+ ? ["firecrawl 没有 instructions,已按默认站点地图请求"]
199
+ : [];
200
+ return { links, notes };
201
+ },
202
+ };
@@ -0,0 +1,22 @@
1
+ import type { PresetId } from "../presets.ts";
2
+ import { anysearchAdapter } from "./anysearch.ts";
3
+ import { cloudflareAdapter } from "./cloudflare.ts";
4
+ import { deepseekAdapter } from "./deepseek.ts";
5
+ import { exaAdapter } from "./exa.ts";
6
+ import { firecrawlAdapter } from "./firecrawl.ts";
7
+ import { tavilyAdapter } from "./tavily.ts";
8
+ import { tencentAdapter } from "./tencent.ts";
9
+ import type { SearchAdapter } from "./types.ts";
10
+ import { xaiAdapter } from "./xai.ts";
11
+
12
+ /** 八家内置供应商的适配器;xai 与 tencent 没有抓取接口,由 capabilities 标记。 */
13
+ export const ADAPTERS: Partial<Record<PresetId, SearchAdapter>> = {
14
+ tavily: tavilyAdapter,
15
+ exa: exaAdapter,
16
+ firecrawl: firecrawlAdapter,
17
+ deepseek: deepseekAdapter,
18
+ xai: xaiAdapter,
19
+ cloudflare: cloudflareAdapter,
20
+ anysearch: anysearchAdapter,
21
+ tencent: tencentAdapter,
22
+ };