pi-chrome-search 2.0.0 → 2.2.0

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.
@@ -29,6 +29,7 @@
29
29
 
30
30
  import type { CdpPage } from "../cdp/types.ts";
31
31
  import { waitForMarker } from "../cdp/humanize.ts";
32
+ import { asFacts } from "../extract/facts.ts";
32
33
  import { normalizeUrl } from "../extract/urls.ts";
33
34
  import { systemClock } from "../runtime/clock.ts";
34
35
  import { fail } from "../runtime/errors.ts";
@@ -134,6 +135,8 @@ export interface YandexExtractedItem {
134
135
  title: string;
135
136
  url: string;
136
137
  snippet: string;
138
+ /** Card facts (S7.7): a shop rating and its review count, when shown. */
139
+ facts?: string[];
137
140
  }
138
141
 
139
142
  export interface YandexExtractedAi {
@@ -543,11 +546,33 @@ export function extractFromDocument(doc: Document, arg: YandexExtractionArg): Ya
543
546
  organics += 1;
544
547
 
545
548
  let snippet = "";
549
+ const facts: string[] = [];
546
550
  if (card !== null) {
547
- const snippetElement = card.querySelector(".OrganicTextContentSpan");
551
+ // `.OrganicTextContent` is today's layout (`yandex-rating`), the
552
+ // `…Span` wrapper is the older one (`yandex-neuro`): the old selector alone
553
+ // returned empty snippets on a live SERP.
554
+ const snippetElement = card.querySelector(".OrganicTextContentSpan, .OrganicTextContent");
548
555
  if (snippetElement !== null) snippet = normalize(snippetElement.textContent);
556
+
557
+ /**
558
+ * A shop rating, not a product rating: the card prints "Рейтинг 4,4 из 5"
559
+ * and "414K отзывов на магазин". Both strings are copied, never
560
+ * recomposed, so the label that says what the rating is about survives
561
+ * into the output (S7.7: report, do not invent).
562
+ */
563
+ const label = card.querySelector('[class*="EReviewsLabel"]');
564
+ if (label !== null) {
565
+ const ratingNode = label.querySelector(".A11yHidden");
566
+ const rating = ratingNode === null ? "" : normalize(ratingNode.textContent);
567
+ if (rating !== "") facts.push(rating);
568
+ const reviewsNode = label.querySelector('[class*="EReviewsLabel-Reviews"]');
569
+ const reviews = reviewsNode === null ? "" : normalize(reviewsNode.textContent);
570
+ if (reviews !== "") facts.push(reviews);
571
+ }
572
+ }
573
+ if (results.length < limit) {
574
+ results.push({ title, url, snippet, ...(facts.length === 0 ? {} : { facts }) });
549
575
  }
550
- if (results.length < limit) results.push({ title, url, snippet });
551
576
  }
552
577
 
553
578
  /**
@@ -728,7 +753,8 @@ function asExtraction(value: unknown): { results: SerpItem[]; ai: AiSummary | un
728
753
  const normalized = normalizeUrl(url);
729
754
  if (normalized === null || seenUrls.has(normalized)) continue;
730
755
  seenUrls.add(normalized);
731
- results.push({ title, url: normalized, snippet });
756
+ const facts = asFacts(rawItem.facts);
757
+ results.push({ title, url: normalized, snippet, ...(facts.length === 0 ? {} : { facts }) });
732
758
  }
733
759
 
734
760
  let ai: AiSummary | undefined;
@@ -25,11 +25,13 @@
25
25
  * "ai": { "text": "…", "sources": ["a.com"] }, // omitted when absent
26
26
  * "results": [
27
27
  * { "title": "…", "url": "…", "domain": "…", "snippet": "…", "engine": "google",
28
- * "unresolved": true }
28
+ * "unresolved": true, "facts": ["23 990 ₽", "Самара"] }
29
29
  * // "engine" only when the caller mixed engines; "unresolved" only for an
30
- * // opaque Google /goto wrapper that stayed unresolved (S3.5, S7.4)
30
+ * // opaque Google /goto wrapper that stayed unresolved (S3.5, S7.4); "facts"
31
+ * // only when the card showed price/rating/location (SPEC §15 D13)
31
32
  * ],
32
33
  * "pageCarried": true,
34
+ * "notice": "no exact match: …", // only when the engine answered with a fallback
33
35
  * "ignored": { "page": "duckduckgo" }, // only when an argument was not honoured
34
36
  * "degraded": true, // only when true
35
37
  * "cached": true, // only when true
@@ -65,6 +67,12 @@ export const COMPACT_CAPS = {
65
67
  maxN: 10,
66
68
  /** Results per domain before the domain cap starts deferring them. */
67
69
  perDomainLimit: 2,
70
+ /**
71
+ * Card facts joined on one line (`23 990 ₽ · Самара · Вчера`, SPEC §15 D13).
72
+ * A fact line is display sugar: it never displaces the snippet, and it is cut
73
+ * here so an adapter cannot flood the output with page text.
74
+ */
75
+ factsMaxChars: 120,
68
76
  } as const;
69
77
 
70
78
  export interface CompactOptions {
@@ -99,6 +107,8 @@ export interface CompactItem {
99
107
  engine?: EngineId;
100
108
  /** The URL is an opaque `/goto` wrapper that stayed unresolved (S3.5, S7.4). */
101
109
  unresolved?: boolean;
110
+ /** Card facts (price, location, date) rendered after the snippet; already capped. */
111
+ facts?: readonly string[];
102
112
  }
103
113
 
104
114
  export interface CompactAi {
@@ -118,6 +128,8 @@ export interface CompactResult {
118
128
  pageCarried: boolean;
119
129
  degraded: boolean;
120
130
  cached: boolean;
131
+ /** Adapter-authored caveat about the page (`no exact match: …`); rendered as `note: `. */
132
+ notice?: string;
121
133
  /** Markdown render (always produced). */
122
134
  text: string;
123
135
  /** JSON render; see the schema above. */
@@ -137,6 +149,7 @@ interface MergedItem {
137
149
  snippet: string;
138
150
  engine?: EngineId;
139
151
  unresolved?: boolean;
152
+ facts?: readonly string[];
140
153
  }
141
154
 
142
155
  interface Opts {
@@ -182,6 +195,30 @@ const AI_MIN_BLOCK = AI_NEWLINE + AI_PREFIX.length + 1;
182
195
  */
183
196
  const AI_SOURCES_MAX_SHARE = 0.25;
184
197
  const PAGE_LINE = "\npage: more results available";
198
+ /** Card facts are one line, joined the way a SERP card groups them. */
199
+ const FACTS_SEPARATOR = " · ";
200
+ /** Adapter notice is one short line (`note: …`), never a paragraph. */
201
+ const NOTICE_MAX_CHARS = 160;
202
+
203
+ /**
204
+ * Cap a card's facts as a joined line by dropping whole facts from the tail.
205
+ * A truncated price ("23 9…") would be a wrong price, and a wrong price is worse
206
+ * than a missing one, so facts are never cut mid-way.
207
+ */
208
+ function capFacts(facts: readonly string[] | undefined): string[] | undefined {
209
+ if (facts === undefined) return undefined;
210
+ const kept: string[] = [];
211
+ let width = 0;
212
+ for (const raw of facts) {
213
+ const fact = collapse(raw);
214
+ if (fact === "") continue;
215
+ const extra = kept.length === 0 ? fact.length : fact.length + FACTS_SEPARATOR.length;
216
+ if (width + extra > COMPACT_CAPS.factsMaxChars) break;
217
+ kept.push(fact);
218
+ width += extra;
219
+ }
220
+ return kept.length === 0 ? undefined : kept;
221
+ }
185
222
 
186
223
  function clampInt(value: number | undefined, fallback: number, min: number, max: number): number {
187
224
  if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
@@ -361,6 +398,9 @@ function renderItem(item: CompactItem, index: number): string {
361
398
  const marker = item.unresolved === true ? " (unresolved)" : "";
362
399
  const lines = [`${index + 1}. ${heading}`, ` ${item.url}${marker}`];
363
400
  if (item.snippet) lines.push(` ${item.snippet}`);
401
+ if (item.facts !== undefined && item.facts.length > 0) {
402
+ lines.push(` ${item.facts.join(FACTS_SEPARATOR)}`);
403
+ }
364
404
  return lines.join("\n");
365
405
  }
366
406
 
@@ -373,6 +413,7 @@ function jsonItem(item: CompactItem): Record<string, unknown> {
373
413
  };
374
414
  if (item.engine) out.engine = item.engine;
375
415
  if (item.unresolved === true) out.unresolved = true;
416
+ if (item.facts !== undefined && item.facts.length > 0) out.facts = [...item.facts];
376
417
  return out;
377
418
  }
378
419
 
@@ -418,6 +459,13 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
418
459
  const engineLabel = contributing.length === 0 ? "none" : contributing.join("+");
419
460
  const query = collapse(input[0]?.query ?? "");
420
461
  const degraded = input.some((serp) => serp.degraded === true);
462
+ // Adapter-authored notice (S7.6): "Avito answered with similar listings". One
463
+ // line, first non-empty wins, capped — a page cannot push prose through it
464
+ // because adapters pass a constant, not page text.
465
+ const notice = truncate(
466
+ collapse(input.find((serp) => typeof serp.notice === "string" && serp.notice !== "")?.notice ?? ""),
467
+ NOTICE_MAX_CHARS,
468
+ );
421
469
 
422
470
  const merged = new Map<string, MergedItem>();
423
471
  for (const serp of input) {
@@ -430,6 +478,7 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
430
478
 
431
479
  const title = truncate(collapse(raw.title), COMPACT_CAPS.titleMaxChars);
432
480
  const snippet = truncate(collapse(raw.snippet), opts.snippetMaxChars);
481
+ const facts = capFacts(raw.facts);
433
482
  const engine = raw.engine ?? serp.engine;
434
483
  const existing = merged.get(normalized);
435
484
 
@@ -441,14 +490,16 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
441
490
  snippet,
442
491
  engine,
443
492
  ...(raw.unresolved === true ? { unresolved: true } : {}),
493
+ ...(facts === undefined ? {} : { facts }),
444
494
  });
445
495
  continue;
446
496
  }
447
497
 
448
- // First non-empty title wins; the longest capped snippet wins (ties keep
449
- // the earlier engine, so the outcome does not depend on map internals).
498
+ // First non-empty title/facts win; the longest capped snippet wins (ties
499
+ // keep the earlier engine, so the outcome does not depend on map internals).
450
500
  if (!existing.title && title) existing.title = title;
451
501
  if (snippet.length > existing.snippet.length) existing.snippet = snippet;
502
+ if (existing.facts === undefined && facts !== undefined) existing.facts = facts;
452
503
  if (raw.unresolved === true) existing.unresolved = true;
453
504
  }
454
505
  }
@@ -518,6 +569,7 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
518
569
 
519
570
  const render = (items: CompactItem[], summary: CompactAi | undefined, state: AiState): string => {
520
571
  const head = [headerFor(items, state)];
572
+ if (notice !== "") head.push(`note: ${notice}`);
521
573
  if (summary) head.push(aiLine(summary));
522
574
  let text = head.join("\n");
523
575
  if (items.length > 0) text += `\n\n${items.map(renderItem).join("\n")}`;
@@ -534,12 +586,14 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
534
586
  };
535
587
  if (mixed || perEngine) out.engine = item.engine;
536
588
  if (item.unresolved === true) out.unresolved = true;
589
+ if (item.facts !== undefined && item.facts.length > 0) out.facts = item.facts;
537
590
  return out;
538
591
  };
539
592
 
540
593
  const outputMaxChars = opts.outputMaxChars;
541
594
  const fallbackCap = Math.min(opts.snippetMaxChars, MIN_SNIPPET);
542
595
  const pageSuffixLen = pageCarried ? PAGE_LINE.length : 0;
596
+ const noticeLen = notice === "" ? 0 : `\nnote: ${notice}`.length;
543
597
 
544
598
  // Results floor: the exact chars needed for the selected items at MIN_SNIPPET.
545
599
  // Titles are already capped by the merge step and URLs are full, so this is an
@@ -551,7 +605,7 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
551
605
  // `headerFor(selected.length, …)` is the widest header still possible: dropping
552
606
  // tail items only shortens the count, and `ai:lead` (7) is the widest AI label.
553
607
  const headerReserve =
554
- headerFor(selected.map((item) => toItem(item, fallbackCap)), "lead").length + pageSuffixLen;
608
+ headerFor(selected.map((item) => toItem(item, fallbackCap)), "lead").length + pageSuffixLen + noticeLen;
555
609
  const aiBudget = Math.max(
556
610
  0,
557
611
  Math.min(opts.summaryMaxChars, outputMaxChars - headerReserve - resultsFloor),
@@ -617,6 +671,7 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
617
671
  }
618
672
  if (aiOut) data.ai = { text: aiOut.text, sources: [...aiOut.sources], state: "answer" };
619
673
  if (aiState === "lead") data.aiState = "lead";
674
+ if (notice !== "") data.notice = notice;
620
675
  if (degraded) data.degraded = true;
621
676
  if (opts.cached) data.cached = true;
622
677
 
@@ -634,6 +689,7 @@ export function compactSerps(serps: readonly RawSerp[], options: CompactOptions)
634
689
  resultsChars,
635
690
  truncated,
636
691
  };
692
+ if (notice !== "") result.notice = notice;
637
693
  if (aiOut) result.ai = aiOut;
638
694
  return result;
639
695
  }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Card facts (SPEC S7.7, §15 D13): the short strings a SERP card shows *outside*
3
+ * its snippet — a price, an availability, a rating, a place, an age.
4
+ *
5
+ * Why one module: three adapters (avito, google, yandex) report facts, and an
6
+ * adapter must not be able to disagree about how many a card may carry or how
7
+ * long one may be. The display cap (one joined line, 120 chars) lives in
8
+ * `compact.ts`; this is the *input* cap, applied to untrusted page output before
9
+ * it becomes a `SerpItem`.
10
+ *
11
+ * Facts are copied from the page, never composed from numbers we found: "От
12
+ * 4 057,00 ₽" and "Рейтинг 4,4 из 5" are the page's own words, so a fact cannot
13
+ * claim more than the SERP did. A fact is dropped whole when it does not fit —
14
+ * half a price is worse than no price.
15
+ */
16
+
17
+ /** A card with more than four facts is reporting a table, not a card. */
18
+ export const FACT_MAX_ITEMS = 4;
19
+
20
+ /** Longer than this is prose that belongs in the snippet (57 + margin). */
21
+ export const FACT_MAX_CHARS = 60;
22
+
23
+ /**
24
+ * Guard for the TS side of the page boundary: the DOM is untrusted even when we
25
+ * wrote the extractor. Non-strings, blanks and over-long fragments are dropped;
26
+ * the count and the length are capped.
27
+ */
28
+ export function asFacts(value: unknown): string[] {
29
+ if (!Array.isArray(value)) return [];
30
+ const facts: string[] = [];
31
+ for (const raw of value) {
32
+ if (typeof raw !== "string") continue;
33
+ const fact = raw.replace(/[\s\u00a0\u202f]+/g, " ").trim();
34
+ if (fact === "") continue;
35
+ facts.push(fact.slice(0, FACT_MAX_CHARS));
36
+ if (facts.length >= FACT_MAX_ITEMS) break;
37
+ }
38
+ return facts;
39
+ }
@@ -69,8 +69,18 @@ const WALL_KINDS = ["captcha", "consent", "sorry", "blocked"] as const;
69
69
  * `skipped` means "not exercised" (caller asked for no probe, or CDP was down),
70
70
  * which is different from `unavailable` ("tried and could not"). It still keeps
71
71
  * the overall verdict at `degraded`: a doctor with no evidence is not healthy.
72
+ *
73
+ * `disabled` is the one exception: an engine the user turned off (the opt-in
74
+ * avito ships off, SPEC D12) is a config choice, not a fault.
72
75
  */
73
- export type EngineVerdict = "ok" | "wall" | "layout_changed" | "blocked" | "unavailable" | "skipped";
76
+ export type EngineVerdict =
77
+ | "ok"
78
+ | "wall"
79
+ | "layout_changed"
80
+ | "blocked"
81
+ | "unavailable"
82
+ | "skipped"
83
+ | "disabled";
74
84
  export type CdpStatus = "ok" | "unavailable" | "busy" | "error" | "awaiting_allow";
75
85
  export type DoctorVerdict = "ok" | "degraded" | "blocked";
76
86
 
@@ -138,6 +148,15 @@ function probeLocale(engine: EngineId, config: Config): Pick<Query, "lang" | "re
138
148
  const region = resolveProbeLocale(config.engines.yandex.lr);
139
149
  return region === undefined ? {} : { region };
140
150
  }
151
+ case "avito": {
152
+ // `auto` means every region here; the adapter's own default is `all` too.
153
+ const region = resolveProbeLocale(config.engines.avito.location);
154
+ return region === undefined ? {} : { region };
155
+ }
156
+ case "ozon": {
157
+ // Ozon has no URL locale/region parameter (SPEC D14).
158
+ return {};
159
+ }
141
160
  }
142
161
  }
143
162
 
@@ -827,7 +846,10 @@ async function engineEntry(
827
846
  ): Promise<DoctorEngine> {
828
847
  const enabled = deps.config.engines[id].enabled;
829
848
  if (!enabled) {
830
- return { id, enabled: false, verdict: "unavailable", note: "disabled in config" };
849
+ // A deliberate config choice, reported as `disabled` so it does not degrade
850
+ // the verdict: the opt-in avito engine ships off (SPEC D12), and a fresh
851
+ // install must not look broken because of it.
852
+ return { id, enabled: false, verdict: "disabled", note: "disabled in config" };
831
853
  }
832
854
 
833
855
  if (!deps.registry.has(id)) {
@@ -948,7 +970,12 @@ async function readArtifacts(artifacts: Artifacts | undefined): Promise<DoctorAr
948
970
 
949
971
  function overallVerdict(cdp: DoctorCdp, engines: DoctorEngine[]): DoctorVerdict {
950
972
  if (cdp.status !== "ok" || cdp.webdriver === true) return "blocked";
951
- if (engines.some((engine) => engine.verdict !== "ok")) return "degraded";
973
+ // `disabled` (an engine the user turned off, e.g. the opt-in avito) is a config
974
+ // choice, not a fault: only a broken, unavailable or probe-skipped engine
975
+ // degrades the report ("probe absence is not health").
976
+ if (engines.some((engine) => engine.verdict !== "ok" && engine.verdict !== "disabled")) {
977
+ return "degraded";
978
+ }
952
979
  return "ok";
953
980
  }
954
981
 
@@ -29,11 +29,11 @@ interface EngineCounters {
29
29
  }
30
30
 
31
31
  function emptyCounters(): Record<EngineId, EngineCounters> {
32
- return {
33
- google: { lastAt: null, today: 0 },
34
- duckduckgo: { lastAt: null, today: 0 },
35
- yandex: { lastAt: null, today: 0 },
36
- };
32
+ // Derived from ENGINE_IDS so a new engine cannot be missing from the counters
33
+ // (SPEC §15 D11: the wiring is data, not a second list to keep in sync).
34
+ const counters = {} as Record<EngineId, EngineCounters>;
35
+ for (const id of ENGINE_IDS) counters[id] = { lastAt: null, today: 0 };
36
+ return counters;
37
37
  }
38
38
 
39
39
  function cancelled(engine: EngineId): WebSearchError {
@@ -29,7 +29,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
29
29
 
30
30
  import type { PageKey } from "../cdp/types.ts";
31
31
  import type { PacingWindow } from "../config/index.ts";
32
- import type { EngineId } from "../engines/types.ts";
32
+ import { ENGINE_IDS, type EngineId } from "../engines/types.ts";
33
33
  import type { Clock } from "./types.ts";
34
34
 
35
35
  /** Highest number of panel paints per second (PLAN-v2 P10-A). */
@@ -136,7 +136,7 @@ const STAGES: ReadonlySet<string> = new Set<PanelStage>([
136
136
  "error",
137
137
  ]);
138
138
 
139
- const ENGINES: ReadonlySet<string> = new Set<EngineId>(["google", "duckduckgo", "yandex"]);
139
+ const ENGINES: ReadonlySet<string> = new Set<string>(ENGINE_IDS);
140
140
 
141
141
  /** Control characters would corrupt the rendered single line; strip, never escape. */
142
142
  function clean(value: string, max: number): string {
@@ -67,17 +67,24 @@ const engineIdSchema = Type.Union([
67
67
  Type.Literal("google"),
68
68
  Type.Literal("duckduckgo"),
69
69
  Type.Literal("yandex"),
70
+ Type.Literal("avito"),
71
+ Type.Literal("ozon"),
70
72
  ]);
71
73
 
72
74
  /**
73
75
  * SPEC §7.1 / D9. `engines` accepts one id, a list, or `auto`; the array form is
74
- * capped at three because that is every engine the extension supports.
76
+ * capped at `ENGINE_IDS.length` because that is every engine the extension
77
+ * supports.
75
78
  */
76
79
  export const webSearchSchema = Type.Object({
77
80
  q: Type.String({ minLength: 1 }),
78
81
  n: Type.Optional(Type.Integer({ minimum: 1, maximum: 10 })),
79
82
  engines: Type.Optional(
80
- Type.Union([Type.Literal("auto"), engineIdSchema, Type.Array(engineIdSchema, { minItems: 1, maxItems: 3 })]),
83
+ Type.Union([
84
+ Type.Literal("auto"),
85
+ engineIdSchema,
86
+ Type.Array(engineIdSchema, { minItems: 1, maxItems: ENGINE_IDS.length }),
87
+ ]),
81
88
  ),
82
89
  summary: Type.Optional(Type.Boolean()),
83
90
  page: Type.Optional(Type.Integer({ minimum: 1 })),
@@ -192,7 +199,7 @@ export interface WebSearchTool {
192
199
  * every word.
193
200
  */
194
201
  export const WEB_SEARCH_DESCRIPTION =
195
- "Search the web for free in the user's visible Chrome (Google, DuckDuckGo, Yandex) with each engine's native AI summary. Compact results, n<=10. Call web_read on a result URL to get that page as paged Markdown. site:/filetype:/quoted queries pass through; engines: id, list, or auto.";
202
+ "Search the web for free in the user's visible Chrome (Google, DuckDuckGo, Yandex; Avito and Ozon opt-in) with each engine's native AI summary. Compact results, n<=10. Call web_read on a result URL to get that page as paged Markdown. site:/filetype:/quoted queries pass through; engines: id, list, or auto.";
196
203
 
197
204
  /**
198
205
  * SPEC S3.6: `q` is passed verbatim, so narrowing happens in the query. The
@@ -200,6 +207,7 @@ export const WEB_SEARCH_DESCRIPTION =
200
207
  */
201
208
  export const WEB_SEARCH_GUIDELINES: readonly string[] = [
202
209
  "web_search passes q verbatim: use quoted phrases, -term, OR, site:, filetype:; narrow by date with after:/before: on Google or date: on Yandex (there is no date argument).",
210
+ "engines:\"avito\" (listings) and \"ozon\" (goods) are opt-in marketplaces: enable engines.<id>.enabled in web-search.json first; auto never uses them.",
203
211
  ];
204
212
 
205
213
  /**
@@ -217,8 +225,7 @@ const FALLBACK_CODES: ReadonlySet<ErrorCode> = new Set<ErrorCode>([
217
225
  ]);
218
226
 
219
227
  /** Every defensive validation failure shares this hint: the fix is in the call, not the file. */
220
- const INPUT_HINT =
221
- "check the web_search arguments: q (non-empty), n 1..10, page >= 1, engines auto|google|duckduckgo|yandex";
228
+ const INPUT_HINT = `check the web_search arguments: q (non-empty), n 1..10, page >= 1, engines auto|${ENGINE_IDS.join("|")}`;
222
229
 
223
230
  const UNSUPPORTED_HINT =
224
231
  "no enabled engine matched the request; run /web-doctor (or node src/cli/doctor.ts --json) and check config.engines";
@@ -350,7 +357,13 @@ function resolveLocale(value: string): string | undefined {
350
357
  * parameter rather than inventing one.
351
358
  */
352
359
  function buildQuery(engine: EngineId, input: NormalizedInput, config: Config): Query {
353
- const query: Query = { text: input.q, n: input.n, summary: input.summary };
360
+ const query: Query = {
361
+ text: input.q,
362
+ n: input.n,
363
+ // S4.2 as data: an engine without a native AI block (avito) never receives
364
+ // `summary: true`; the caller sees `ignored.summary` instead (S7.1).
365
+ summary: SUPPORT_MATRIX.summary[engine] === "yes" ? input.summary : false,
366
+ };
354
367
  // S4.2 is data (`SUPPORT_MATRIX`): an engine that cannot paginate never
355
368
  // receives `page`, and the caller is told so through `ignored` below.
356
369
  if (SUPPORT_MATRIX.page[engine] === "yes") query.page = input.page;
@@ -373,6 +386,18 @@ function buildQuery(engine: EngineId, input: NormalizedInput, config: Config): Q
373
386
  if (region !== undefined) query.region = region;
374
387
  break;
375
388
  }
389
+ case "avito": {
390
+ // The region is a URL path slug, not a locale; `auto` means "every
391
+ // region" here, which is the adapter's own default (SPEC D12).
392
+ query.region = resolveLocale(config.engines.avito.location) ?? "all";
393
+ break;
394
+ }
395
+ case "ozon": {
396
+ // No locale plumbing: Ozon has no URL region/language parameter (its city
397
+ // is a cookie-backed UI choice), so a value here would be a phantom key
398
+ // (S7.5). The query text is the whole input.
399
+ break;
400
+ }
376
401
  }
377
402
  return query;
378
403
  }
@@ -967,7 +992,7 @@ async function runEngineLive(
967
992
  const human = await humanSearch({
968
993
  page,
969
994
  query: query.text,
970
- homeUrl: interaction.homeUrl,
995
+ homeUrl: interaction.homeUrlFor?.(query) ?? interaction.homeUrl,
971
996
  serpUrl: engine.buildUrl(query),
972
997
  boxSelectors: [...interaction.boxSelectors],
973
998
  marker: interaction.marker,
@@ -1209,6 +1234,20 @@ export function createWebSearchTool(
1209
1234
  return failureResult(asFailure(error, "resolve"));
1210
1235
  }
1211
1236
  if (order.length === 0) {
1237
+ // The selection came back empty. When the caller named engines, ask the
1238
+ // registry *why* each one was dropped: a disabled engine (the opt-in avito
1239
+ // ships off, D12) gets its own "set config.engines.avito.enabled" hint
1240
+ // instead of a generic "no enabled engine" (S9.2). `auto` keeps the
1241
+ // generic message: there is no id to explain.
1242
+ const named =
1243
+ input.engines === "auto" ? [] : Array.isArray(input.engines) ? input.engines : [input.engines];
1244
+ for (const id of named) {
1245
+ try {
1246
+ deps.engines.get(id);
1247
+ } catch (error) {
1248
+ return failureResult(asFailure(error, "resolve", id));
1249
+ }
1250
+ }
1212
1251
  return failureResult(
1213
1252
  fail("E_UNSUPPORTED", "resolve", {
1214
1253
  hint: UNSUPPORTED_HINT,