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.
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +195 -0
- package/README_EN.md +195 -0
- package/extensions/web-search.ts +18 -0
- package/package.json +56 -0
- package/src/adapters/anysearch.ts +107 -0
- package/src/adapters/cloudflare.ts +139 -0
- package/src/adapters/deepseek.ts +108 -0
- package/src/adapters/exa.ts +161 -0
- package/src/adapters/firecrawl.ts +202 -0
- package/src/adapters/index.ts +22 -0
- package/src/adapters/tavily.ts +148 -0
- package/src/adapters/tencent.ts +172 -0
- package/src/adapters/types.ts +122 -0
- package/src/adapters/util.ts +93 -0
- package/src/adapters/xai.ts +176 -0
- package/src/base-url.ts +40 -0
- package/src/cloudflare-oauth.ts +551 -0
- package/src/config.ts +547 -0
- package/src/http.ts +777 -0
- package/src/key-pool.ts +144 -0
- package/src/key-quota.ts +532 -0
- package/src/presets.ts +129 -0
- package/src/search-service.ts +411 -0
- package/src/secrets.ts +78 -0
- package/src/settings-ui.ts +1279 -0
- package/src/tools.ts +928 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { ResolvedInstance } from "../config.ts";
|
|
2
|
+
import { ProviderError, type ProviderResponse, type RequestSpec } from "../http.ts";
|
|
3
|
+
import type {
|
|
4
|
+
ExtractOutput,
|
|
5
|
+
Freshness,
|
|
6
|
+
SearchAdapter,
|
|
7
|
+
SearchHit,
|
|
8
|
+
SearchOutput,
|
|
9
|
+
SearchParams,
|
|
10
|
+
SearchQuality,
|
|
11
|
+
SourceMode,
|
|
12
|
+
Topic,
|
|
13
|
+
} from "./types.ts";
|
|
14
|
+
import { asArray, asRecord, asString, businessFailure, rejectNoMap, rejectUnsupported } from "./util.ts";
|
|
15
|
+
|
|
16
|
+
/** Responses API 必须带模型名;官方示例用 grok-4.7,实例可用 model 覆盖。 */
|
|
17
|
+
export const DEFAULT_XAI_MODEL = "grok-4.7";
|
|
18
|
+
|
|
19
|
+
/** web_search 工具单次最多接受 5 个域名。 */
|
|
20
|
+
const MAX_DOMAINS = 5;
|
|
21
|
+
|
|
22
|
+
/** 质量档位只决定思考强度;是否搜索 X 由实例的 xSearch 开关决定。 */
|
|
23
|
+
const QUALITY_EFFORT: Record<SearchQuality, string> = {
|
|
24
|
+
basic: "low",
|
|
25
|
+
advanced: "medium",
|
|
26
|
+
deep: "high",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const NO_FETCH = "xai 没有独立抓取接口";
|
|
30
|
+
|
|
31
|
+
/** 没有正文返回,contents 只能取 none;官方没有发布时间字段,freshness 走提示词软约束。 */
|
|
32
|
+
function unsupportedParams(params: SearchParams): string[] {
|
|
33
|
+
return params.contents !== undefined && params.contents !== "none" ? ["contents"] : [];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** freshness 在官方参数里没有对应字段,只能写进提示词。 */
|
|
37
|
+
const FRESHNESS_WINDOW: Record<Freshness, string> = {
|
|
38
|
+
day: "最近一天",
|
|
39
|
+
week: "最近一周",
|
|
40
|
+
month: "最近一个月",
|
|
41
|
+
year: "最近一年",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** 信源范围官方没有对应字段,只作为提示词里的软约束。 */
|
|
45
|
+
const SOURCE_MODE_HINTS: Record<SourceMode, string> = {
|
|
46
|
+
public: "优先采用公开网页来源。",
|
|
47
|
+
authoritative: "优先采用权威垂直信源,如政府、学术与财经机构发布的内容。",
|
|
48
|
+
mixed: "同时兼顾公开网页与权威垂直信源。",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** 主题官方没有对应字段,新闻主题只写进提示词。 */
|
|
52
|
+
const TOPIC_HINTS: Record<Topic, string> = {
|
|
53
|
+
general: "",
|
|
54
|
+
news: "优先采用近期新闻报道。",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** 提示词固定要求优先当前来源,freshness、sourceMode 与 topic 只追加约束。 */
|
|
58
|
+
function inputOf(params: SearchParams): string {
|
|
59
|
+
const window = params.freshness ? `只采纳${FRESHNESS_WINDOW[params.freshness]}内发布的内容。` : "";
|
|
60
|
+
const source = params.sourceMode ? SOURCE_MODE_HINTS[params.sourceMode] : "";
|
|
61
|
+
const topic = params.topic ? TOPIC_HINTS[params.topic] : "";
|
|
62
|
+
// 地区偏好官方没有对应字段,只写进提示词。
|
|
63
|
+
const country = params.country ? `结果优先考虑 ${params.country} 地区。` : "";
|
|
64
|
+
return `${params.query}\n\n优先采用当前可访问的来源,并核对页面发布或更新日期。${source}${topic}${country}${window}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function invalid(instance: ResolvedInstance, message: string): ProviderError {
|
|
68
|
+
return new ProviderError({ instance: instance.id, code: "invalidRequest", message });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 域名过滤放在 web_search 工具的 filters 里,两者官方互斥。 */
|
|
72
|
+
function toolOf(instance: ResolvedInstance, params: SearchParams): Record<string, unknown> {
|
|
73
|
+
const tool: Record<string, unknown> = { type: "web_search" };
|
|
74
|
+
const domains = params.domains?.slice(0, MAX_DOMAINS);
|
|
75
|
+
const excluded = params.excludeDomains?.slice(0, MAX_DOMAINS);
|
|
76
|
+
if (domains?.length && excluded?.length) {
|
|
77
|
+
throw invalid(instance, "xai 不能同时指定 domains 与 excludeDomains");
|
|
78
|
+
}
|
|
79
|
+
if (domains?.length) tool.filters = { allowed_domains: domains };
|
|
80
|
+
else if (excluded?.length) tool.filters = { excluded_domains: excluded };
|
|
81
|
+
return tool;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** x_search 由实例固定开关决定;域名过滤只作用于 web_search。 */
|
|
85
|
+
function toolsOf(instance: ResolvedInstance, params: SearchParams): Record<string, unknown>[] {
|
|
86
|
+
const tools: Record<string, unknown>[] = [toolOf(instance, params)];
|
|
87
|
+
if (instance.xSearch) tools.push({ type: "x_search" });
|
|
88
|
+
return tools;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 正文在 output 的 output_text 里,来源在 annotations 与 citations 里。 */
|
|
92
|
+
function collect(data: Record<string, unknown>, params: SearchParams): SearchOutput {
|
|
93
|
+
const texts: string[] = [];
|
|
94
|
+
const hits: SearchHit[] = [];
|
|
95
|
+
const seen = new Set<string>();
|
|
96
|
+
|
|
97
|
+
const addHit = (value: unknown): void => {
|
|
98
|
+
const url = asString(value);
|
|
99
|
+
if (!url || seen.has(url)) return;
|
|
100
|
+
seen.add(url);
|
|
101
|
+
// 引用标注的 title 是编号(如 "1"),不是页面标题,直接用 URL。
|
|
102
|
+
hits.push({ title: url, url });
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
for (const value of asArray(data.output)) {
|
|
106
|
+
const item = asRecord(value);
|
|
107
|
+
if (item?.type !== "message") continue;
|
|
108
|
+
for (const block of asArray(item.content)) {
|
|
109
|
+
const part = asRecord(block);
|
|
110
|
+
if (part?.type !== "output_text") continue;
|
|
111
|
+
const text = asString(part.text);
|
|
112
|
+
if (text) texts.push(text);
|
|
113
|
+
for (const annotation of asArray(part.annotations)) {
|
|
114
|
+
const citation = asRecord(annotation);
|
|
115
|
+
if (citation?.type === "url_citation") addHit(citation.url);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const citation of asArray(data.citations)) {
|
|
120
|
+
addHit(typeof citation === "string" ? citation : asRecord(citation)?.url);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const notes: string[] = [];
|
|
124
|
+
if ((params.domains?.length ?? 0) > MAX_DOMAINS || (params.excludeDomains?.length ?? 0) > MAX_DOMAINS) {
|
|
125
|
+
notes.push(`xai 每次最多过滤 ${MAX_DOMAINS} 个域名,超出的已忽略`);
|
|
126
|
+
}
|
|
127
|
+
return { hits, answer: texts.join("\n\n") || undefined, notes };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** xAI:走 Responses API 的 web_search 工具,返回答案与引用来源。 */
|
|
131
|
+
export const xaiAdapter: SearchAdapter = {
|
|
132
|
+
preset: "xai",
|
|
133
|
+
capabilities: { search: true, fetch: false, map: false },
|
|
134
|
+
|
|
135
|
+
unsupported: unsupportedParams,
|
|
136
|
+
|
|
137
|
+
buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
|
|
138
|
+
rejectUnsupported(instance, "xai", unsupportedParams(params));
|
|
139
|
+
|
|
140
|
+
const body: Record<string, unknown> = {
|
|
141
|
+
model: instance.model ?? DEFAULT_XAI_MODEL,
|
|
142
|
+
input: [{ role: "user", content: inputOf(params) }],
|
|
143
|
+
tools: toolsOf(instance, params),
|
|
144
|
+
reasoning: { effort: QUALITY_EFFORT[params.quality] },
|
|
145
|
+
};
|
|
146
|
+
return { instance, path: "responses", method: "POST", body };
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
checkSearch(response: ProviderResponse): void {
|
|
150
|
+
const data = asRecord(response.data);
|
|
151
|
+
if (!data || !Array.isArray(data.output)) {
|
|
152
|
+
throw businessFailure(response, "xai 响应缺少 output");
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
mapSearch(response: ProviderResponse, params: SearchParams): SearchOutput {
|
|
157
|
+
const data = asRecord(response.data) ?? {};
|
|
158
|
+
return { ...collect(data, params), rateLimit: response.rateLimit };
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
buildExtract(instance: ResolvedInstance): RequestSpec {
|
|
162
|
+
throw invalid(instance, NO_FETCH);
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
mapExtract(): ExtractOutput {
|
|
166
|
+
throw new ProviderError({ instance: "xai", code: "invalidRequest", message: NO_FETCH });
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
buildMap(instance) {
|
|
170
|
+
rejectNoMap(instance.id, "xai");
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
mapSite() {
|
|
174
|
+
rejectNoMap("xai", "xai");
|
|
175
|
+
},
|
|
176
|
+
};
|
package/src/base-url.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** 结尾的版本段,如 /v1、/v2、/v1beta;预设路径本身带版本段,base 里重复会拼出双份。 */
|
|
2
|
+
const VERSION_SEGMENT_RE = /\/v\d+[a-z0-9]*$/i;
|
|
3
|
+
|
|
4
|
+
/** 解析 base 地址,只接受 http(s)、无内嵌凭据、无查询串与 fragment。 */
|
|
5
|
+
export function parseBaseUrl(value: string): URL | undefined {
|
|
6
|
+
const trimmed = value.trim();
|
|
7
|
+
if (!trimmed) return undefined;
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(trimmed);
|
|
10
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return undefined;
|
|
11
|
+
if (url.username || url.password) return undefined;
|
|
12
|
+
if (url.search || url.hash) return undefined;
|
|
13
|
+
return url;
|
|
14
|
+
} catch {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** host 加路径前缀,去掉结尾斜杠与版本段;无法解析时原样返回。 */
|
|
20
|
+
export function canonicalBase(value: string): string {
|
|
21
|
+
const url = parseBaseUrl(value);
|
|
22
|
+
if (!url) return value.trim();
|
|
23
|
+
const path = url.pathname.replace(/\/+$/, "").replace(VERSION_SEGMENT_RE, "");
|
|
24
|
+
return `${url.origin}${path}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 校验并规范化 base 地址;失败时抛出带字段名的错误。 */
|
|
28
|
+
export function assertBaseUrl(value: string, label: string): string {
|
|
29
|
+
if (!parseBaseUrl(value)) {
|
|
30
|
+
throw new Error(`${label} 必须是无账号密码、无查询串的 http(s) 地址:${value}`);
|
|
31
|
+
}
|
|
32
|
+
return canonicalBase(value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 拼接 base 与预设路径,路径以预设为准。 */
|
|
36
|
+
export function joinUrl(base: string, path: string): string {
|
|
37
|
+
const normalized = canonicalBase(base);
|
|
38
|
+
const suffix = path.startsWith("/") ? path : `/${path}`;
|
|
39
|
+
return `${normalized}${suffix}`;
|
|
40
|
+
}
|