pi-auto-web-search 0.1.1 → 1.2.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.
@@ -11,7 +11,7 @@ import type {
11
11
  SourceMode,
12
12
  Topic,
13
13
  } from "./types.ts";
14
- import { asArray, asRecord, asString, businessFailure, rejectNoMap, rejectUnsupported } from "./util.ts";
14
+ import { asArray, asRecord, asString, businessFailure, exclusiveDomainGaps, rejectNoMap, rejectUnsupported } from "./util.ts";
15
15
 
16
16
  /** Responses API 必须带模型名;官方示例用 grok-4.7,实例可用 model 覆盖。 */
17
17
  export const DEFAULT_XAI_MODEL = "grok-4.7";
@@ -28,9 +28,15 @@ const QUALITY_EFFORT: Record<SearchQuality, string> = {
28
28
 
29
29
  const NO_FETCH = "xai 没有独立抓取接口";
30
30
 
31
- /** 没有正文返回,contents 只能取 none;官方没有发布时间字段,freshness 走提示词软约束。 */
32
- function unsupportedParams(params: SearchParams): string[] {
33
- return params.contents !== undefined && params.contents !== "none" ? ["contents"] : [];
31
+ function unsupportedParams(params: SearchParams, instance: ResolvedInstance): string[] {
32
+ const names = exclusiveDomainGaps(params);
33
+ const included = params.domains?.length ?? 0;
34
+ const excluded = params.excludeDomains?.length ?? 0;
35
+ if (included > MAX_DOMAINS) names.push(`超过 ${MAX_DOMAINS} 个 domains`);
36
+ if (excluded > MAX_DOMAINS) names.push(`超过 ${MAX_DOMAINS} 个 excludeDomains`);
37
+ if (instance.xSearch && (included > 0 || excluded > 0)) names.push("同时搜索 X 并按网站域名过滤");
38
+ if (params.contents !== undefined && params.contents !== "none") names.push("contents");
39
+ return names;
34
40
  }
35
41
 
36
42
  /** freshness 在官方参数里没有对应字段,只能写进提示词。 */
@@ -69,27 +75,22 @@ function invalid(instance: ResolvedInstance, message: string): ProviderError {
69
75
  }
70
76
 
71
77
  /** 域名过滤放在 web_search 工具的 filters 里,两者官方互斥。 */
72
- function toolOf(instance: ResolvedInstance, params: SearchParams): Record<string, unknown> {
78
+ function toolOf(params: SearchParams): Record<string, unknown> {
73
79
  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 };
80
+ if (params.domains?.length) tool.filters = { allowed_domains: params.domains };
81
+ else if (params.excludeDomains?.length) tool.filters = { excluded_domains: params.excludeDomains };
81
82
  return tool;
82
83
  }
83
84
 
84
85
  /** x_search 由实例固定开关决定;域名过滤只作用于 web_search。 */
85
86
  function toolsOf(instance: ResolvedInstance, params: SearchParams): Record<string, unknown>[] {
86
- const tools: Record<string, unknown>[] = [toolOf(instance, params)];
87
+ const tools: Record<string, unknown>[] = [toolOf(params)];
87
88
  if (instance.xSearch) tools.push({ type: "x_search" });
88
89
  return tools;
89
90
  }
90
91
 
91
92
  /** 正文在 output 的 output_text 里,来源在 annotations 与 citations 里。 */
92
- function collect(data: Record<string, unknown>, params: SearchParams): SearchOutput {
93
+ function collect(data: Record<string, unknown>): SearchOutput {
93
94
  const texts: string[] = [];
94
95
  const hits: SearchHit[] = [];
95
96
  const seen = new Set<string>();
@@ -120,11 +121,7 @@ function collect(data: Record<string, unknown>, params: SearchParams): SearchOut
120
121
  addHit(typeof citation === "string" ? citation : asRecord(citation)?.url);
121
122
  }
122
123
 
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 };
124
+ return { hits, answer: texts.join("\n\n") || undefined };
128
125
  }
129
126
 
130
127
  /** xAI:走 Responses API 的 web_search 工具,返回答案与引用来源。 */
@@ -135,7 +132,7 @@ export const xaiAdapter: SearchAdapter = {
135
132
  unsupported: unsupportedParams,
136
133
 
137
134
  buildSearch(instance: ResolvedInstance, params: SearchParams): RequestSpec {
138
- rejectUnsupported(instance, "xai", unsupportedParams(params));
135
+ rejectUnsupported(instance, "xai", unsupportedParams(params, instance));
139
136
 
140
137
  const body: Record<string, unknown> = {
141
138
  model: instance.model ?? DEFAULT_XAI_MODEL,
@@ -153,9 +150,9 @@ export const xaiAdapter: SearchAdapter = {
153
150
  }
154
151
  },
155
152
 
156
- mapSearch(response: ProviderResponse, params: SearchParams): SearchOutput {
153
+ mapSearch(response: ProviderResponse): SearchOutput {
157
154
  const data = asRecord(response.data) ?? {};
158
- return { ...collect(data, params), rateLimit: response.rateLimit };
155
+ return { ...collect(data), rateLimit: response.rateLimit };
159
156
  },
160
157
 
161
158
  buildExtract(instance: ResolvedInstance): RequestSpec {