From a5e1a8030129085f6678fa2b6df817002eedaa91 Mon Sep 17 00:00:00 2001 From: Matteo Benedetto Date: Tue, 11 Aug 2026 21:22:56 +0200 Subject: [PATCH] feat: supporto ai Focus Modes di Perplexity (parametro 'focus') - Aggiunto il parametro 'focus' al tool perplexity_search per selezionare la fonte di conoscenza (Focus Mode). - FOCUS_MAP: mappa focus -> search_focus + sources usati dall'endpoint perplexity_ask (internet/web, academic/scholar, writing, wolfram, youtube, reddit, math, social, finance). - default: internet (comportamento invariato). - README aggiornato. --- README.md | 4 ++++ src/index.ts | 10 ++++++++++ src/search/client.ts | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1c95c23..e92dc48 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,12 @@ Once installed, the agent automatically calls `perplexity_search` whenever it ne |---|---|---|---| | `query` | string | ✅ | The search query | | `recency` | string | — | Filter by age: `hour` · `day` · `week` · `month` · `year` | +| `focus` | string | — | Perplexity **Focus Mode** (source domain): `internet` · `academic` · `writing` · `wolfram` · `youtube` · `reddit` · `math` · `social` · `finance` (default `internet`) | | `limit` | number | — | Max sources to include (1–50) | +**Focus Modes** let the agent route the search to a specific knowledge source: +`academic` → peer-reviewed/scholarly (PubMed, arXiv, Semantic Scholar), `youtube` → video transcripts, `reddit` → community/opinions, `wolfram`/`math` → computations, `social` → social platforms, `finance` → financial/SEC data, `writing` → model-only (no live search). + Model selection is configured globally with `/perplexity-config` or `PI_PERPLEXITY_MODEL`; it is not exposed as a tool parameter, so agent-generated tool calls cannot accidentally override your configured model. ### Output format diff --git a/src/index.ts b/src/index.ts index a56f806..982deaa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,15 @@ export default function (pi: ExtensionAPI) { description: "Filter results by recency", }), ), + focus: Type.Optional( + StringEnum( + ["internet", "academic", "writing", "wolfram", "youtube", "reddit", "math", "social", "finance"] as const, + { + description: + "Perplexity Focus Mode: source domain to search (internet=web, academic, writing, wolfram/math, youtube, reddit, social, finance)", + }, + ), + ), limit: Type.Optional( Type.Number({ description: "Max sources to return", minimum: 1, maximum: 50 }), ), @@ -77,6 +86,7 @@ export default function (pi: ExtensionAPI) { query: params.query, model, ...(params.recency !== undefined ? { recency: params.recency } : {}), + ...(params.focus !== undefined ? { focus: params.focus } : {}), }, auth, signal, diff --git a/src/search/client.ts b/src/search/client.ts index 503a67f..24955a8 100644 --- a/src/search/client.ts +++ b/src/search/client.ts @@ -8,10 +8,35 @@ import { PERPLEXITY_USER_AGENT, PERPLEXITY_API_VERSION } from "../constants.js"; const PERPLEXITY_ENDPOINT = "https://www.perplexity.ai/rest/sse/perplexity_ask"; +export type FocusMode = + | "internet" + | "academic" + | "writing" + | "wolfram" + | "youtube" + | "reddit" + | "math" + | "social" + | "finance"; + +// Mappa Focus Mode → search_focus + sources usati dall'endpoint perplexity_ask +const FOCUS_MAP: Record = { + internet: { focus: "internet", sources: ["web"] }, + academic: { focus: "academic", sources: ["scholar"] }, + writing: { focus: "writing", sources: [] }, + wolfram: { focus: "wolfram", sources: ["wolfram"] }, + youtube: { focus: "youtube", sources: ["youtube"] }, + reddit: { focus: "reddit", sources: ["reddit"] }, + math: { focus: "math", sources: ["wolfram"] }, + social: { focus: "social", sources: ["social"] }, + finance: { focus: "finance", sources: ["finance"] }, +}; + export interface SearchParams { - query: string; - recency?: "hour" | "day" | "week" | "month" | "year"; - model: string; + query: string; + recency?: "hour" | "day" | "week" | "month" | "year"; + focus?: FocusMode; + model: string; } function normalizeUrl(url: string): string { @@ -120,10 +145,10 @@ function buildRequestBody(params: SearchParams): Record { query_str: query, params: { query_str: query, - search_focus: "internet", + search_focus: FOCUS_MAP[params.focus ?? "internet"].focus, mode: "copilot", model_preference: params.model, - sources: ["web"], + sources: FOCUS_MAP[params.focus ?? "internet"].sources, attachments: [], frontend_uuid: randomUUID(), frontend_context_uuid: randomUUID(),