refactor: drop dead model/limit plumbing and unused render helper
resolveSearchDefaults no longer accepts a per-call model override and SearchParams no longer carries limit; neither was reachable from the tool schema (limit is applied client-side in formatForLLM). Also fold asPositiveNumber into asPositiveInteger and simplify recency matching.
This commit is contained in:
+3
-10
@@ -48,22 +48,15 @@ export async function saveConfig(config: PerplexityConfig, configPath: string =
|
|||||||
await chmod(configPath, 0o600);
|
await chmod(configPath, 0o600);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Resolve effective search defaults from env vars, config file, and per-call incognito override. */
|
||||||
* Resolve effective values using priority: per-call param > env var > config file > default.
|
|
||||||
* Returns the model and incognito values to use for a search.
|
|
||||||
*/
|
|
||||||
export function resolveSearchDefaults(
|
export function resolveSearchDefaults(
|
||||||
params: { model?: string; incognito?: boolean },
|
params: { incognito?: boolean },
|
||||||
config: PerplexityConfig,
|
config: PerplexityConfig,
|
||||||
): { model: string; incognito: boolean } {
|
): { model: string; incognito: boolean } {
|
||||||
const envModel = process.env.PI_PERPLEXITY_MODEL?.trim() || undefined;
|
const envModel = process.env.PI_PERPLEXITY_MODEL?.trim() || undefined;
|
||||||
const envIncognito = process.env.PI_PERPLEXITY_INCOGNITO || undefined;
|
const envIncognito = process.env.PI_PERPLEXITY_INCOGNITO || undefined;
|
||||||
|
|
||||||
const model = params.model
|
const model = envModel ?? config.model ?? "pplx_pro_upgraded";
|
||||||
?? envModel
|
|
||||||
?? config.model
|
|
||||||
?? "pplx_pro_upgraded";
|
|
||||||
|
|
||||||
const incognito = params.incognito
|
const incognito = params.incognito
|
||||||
?? (envIncognito !== undefined ? envIncognito !== "false" && envIncognito !== "0" : undefined)
|
?? (envIncognito !== undefined ? envIncognito !== "false" && envIncognito !== "0" : undefined)
|
||||||
?? config.incognito
|
?? config.incognito
|
||||||
|
|||||||
+1
-4
@@ -73,9 +73,7 @@ export default function (pi: ExtensionAPI) {
|
|||||||
|
|
||||||
const config = await loadConfig();
|
const config = await loadConfig();
|
||||||
const { model, incognito } = resolveSearchDefaults(
|
const { model, incognito } = resolveSearchDefaults(
|
||||||
{
|
params.incognito !== undefined ? { incognito: params.incognito } : {},
|
||||||
...(params.incognito !== undefined ? { incognito: params.incognito } : {}),
|
|
||||||
},
|
|
||||||
config,
|
config,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -85,7 +83,6 @@ export default function (pi: ExtensionAPI) {
|
|||||||
model,
|
model,
|
||||||
incognito,
|
incognito,
|
||||||
...(params.recency !== undefined ? { recency: params.recency } : {}),
|
...(params.recency !== undefined ? { recency: params.recency } : {}),
|
||||||
...(params.limit !== undefined ? { limit: params.limit } : {}),
|
|
||||||
},
|
},
|
||||||
auth,
|
auth,
|
||||||
signal,
|
signal,
|
||||||
|
|||||||
+2
-10
@@ -6,28 +6,20 @@ interface PerplexityCallArgs {
|
|||||||
query?: unknown;
|
query?: unknown;
|
||||||
recency?: unknown;
|
recency?: unknown;
|
||||||
limit?: unknown;
|
limit?: unknown;
|
||||||
model?: unknown;
|
|
||||||
incognito?: unknown;
|
incognito?: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
const RECENCY_VALUES = new Set(["hour", "day", "week", "month", "year"] as const);
|
const RECENCY_VALUES: readonly string[] = ["hour", "day", "week", "month", "year"];
|
||||||
export function renderPerplexityCall(args: PerplexityCallArgs, theme: Theme): Text {
|
export function renderPerplexityCall(args: PerplexityCallArgs, theme: Theme): Text {
|
||||||
const query = asString(args?.query)?.trim();
|
const query = asString(args?.query)?.trim();
|
||||||
const recencyRaw = asString(args?.recency)?.trim().toLowerCase();
|
const recencyRaw = asString(args?.recency)?.trim().toLowerCase();
|
||||||
const recency = recencyRaw && RECENCY_VALUES.has(recencyRaw as (typeof RECENCY_VALUES extends Set<infer T> ? T : never))
|
const recency = recencyRaw && RECENCY_VALUES.includes(recencyRaw) ? recencyRaw : undefined;
|
||||||
? recencyRaw
|
|
||||||
: undefined;
|
|
||||||
const limit = asPositiveInteger(args?.limit);
|
const limit = asPositiveInteger(args?.limit);
|
||||||
const model = asString(args?.model)?.trim();
|
|
||||||
const incognito = typeof args?.incognito === "boolean" ? args.incognito : undefined;
|
const incognito = typeof args?.incognito === "boolean" ? args.incognito : undefined;
|
||||||
|
|
||||||
let text = theme.fg("toolTitle", theme.bold("perplexity_search "));
|
let text = theme.fg("toolTitle", theme.bold("perplexity_search "));
|
||||||
text += query ? theme.fg("muted", truncate(query, 90)) : theme.fg("warning", "(missing query)");
|
text += query ? theme.fg("muted", truncate(query, 90)) : theme.fg("warning", "(missing query)");
|
||||||
|
|
||||||
if (model) {
|
|
||||||
text += theme.fg("dim", ` • ${model}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof incognito === "boolean") {
|
if (typeof incognito === "boolean") {
|
||||||
text += theme.fg("dim", ` • incognito ${incognito ? "on" : "off"}`);
|
text += theme.fg("dim", ` • incognito ${incognito ? "on" : "off"}`);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-7
@@ -13,16 +13,11 @@ export function asNumber(value: unknown): number | undefined {
|
|||||||
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function asPositiveNumber(value: unknown): number | undefined {
|
export function asPositiveInteger(value: unknown): number | undefined {
|
||||||
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return value;
|
return Math.floor(value);
|
||||||
}
|
|
||||||
|
|
||||||
export function asPositiveInteger(value: unknown): number | undefined {
|
|
||||||
const n = asPositiveNumber(value);
|
|
||||||
return n !== undefined ? Math.floor(n) : undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function truncate(text: string, maxLength: number): string {
|
export function truncate(text: string, maxLength: number): string {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ const PERPLEXITY_ENDPOINT = "https://www.perplexity.ai/rest/sse/perplexity_ask";
|
|||||||
export interface SearchParams {
|
export interface SearchParams {
|
||||||
query: string;
|
query: string;
|
||||||
recency?: "hour" | "day" | "week" | "month" | "year";
|
recency?: "hour" | "day" | "week" | "month" | "year";
|
||||||
limit?: number;
|
|
||||||
model: string;
|
model: string;
|
||||||
incognito: boolean;
|
incognito: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -6,7 +6,7 @@ import { join } from "node:path";
|
|||||||
let loadConfig: (configPath?: string) => Promise<import("../src/config.js").PerplexityConfig>;
|
let loadConfig: (configPath?: string) => Promise<import("../src/config.js").PerplexityConfig>;
|
||||||
let saveConfig: (config: import("../src/config.js").PerplexityConfig, configPath?: string) => Promise<void>;
|
let saveConfig: (config: import("../src/config.js").PerplexityConfig, configPath?: string) => Promise<void>;
|
||||||
let resolveSearchDefaults: (
|
let resolveSearchDefaults: (
|
||||||
params: { model?: string; incognito?: boolean },
|
params: { incognito?: boolean },
|
||||||
config: import("../src/config.js").PerplexityConfig,
|
config: import("../src/config.js").PerplexityConfig,
|
||||||
) => { model: string; incognito: boolean };
|
) => { model: string; incognito: boolean };
|
||||||
|
|
||||||
@@ -142,16 +142,16 @@ describe("resolveSearchDefaults", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test("per-call params override everything", () => {
|
test("per-call incognito overrides env/config without exposing model override", () => {
|
||||||
const originalModel = process.env.PI_PERPLEXITY_MODEL;
|
const originalModel = process.env.PI_PERPLEXITY_MODEL;
|
||||||
try {
|
try {
|
||||||
process.env.PI_PERPLEXITY_MODEL = "experimental";
|
process.env.PI_PERPLEXITY_MODEL = "experimental";
|
||||||
|
|
||||||
const result = resolveSearchDefaults(
|
const result = resolveSearchDefaults(
|
||||||
{ model: "claude46sonnetthinking", incognito: false },
|
{ incognito: false },
|
||||||
{ model: "gpt54", incognito: true },
|
{ model: "gpt54", incognito: true },
|
||||||
);
|
);
|
||||||
expect(result.model).toBe("claude46sonnetthinking");
|
expect(result.model).toBe("experimental");
|
||||||
expect(result.incognito).toBe(false);
|
expect(result.incognito).toBe(false);
|
||||||
} finally {
|
} finally {
|
||||||
if (originalModel === undefined) delete process.env.PI_PERPLEXITY_MODEL;
|
if (originalModel === undefined) delete process.env.PI_PERPLEXITY_MODEL;
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ describe("Perplexity model selection e2e", () => {
|
|||||||
query: "Say exactly OK",
|
query: "Say exactly OK",
|
||||||
model,
|
model,
|
||||||
incognito: true,
|
incognito: true,
|
||||||
limit: 1,
|
|
||||||
},
|
},
|
||||||
token,
|
token,
|
||||||
);
|
);
|
||||||
|
|||||||
+1
-3
@@ -9,18 +9,16 @@ const theme = {
|
|||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
describe("renderPerplexityCall", () => {
|
describe("renderPerplexityCall", () => {
|
||||||
test("shows the selected model in the tool call row", () => {
|
test("shows query filters in the tool call row", () => {
|
||||||
const rendered = renderPerplexityCall(
|
const rendered = renderPerplexityCall(
|
||||||
{
|
{
|
||||||
query: "latest Node release notes",
|
query: "latest Node release notes",
|
||||||
model: "claude46sonnetthinking",
|
|
||||||
recency: "week",
|
recency: "week",
|
||||||
limit: 5,
|
limit: 5,
|
||||||
},
|
},
|
||||||
theme,
|
theme,
|
||||||
).render(200).join("\n");
|
).render(200).join("\n");
|
||||||
|
|
||||||
expect(rendered).toContain("claude46sonnetthinking");
|
|
||||||
expect(rendered).toContain("week");
|
expect(rendered).toContain("week");
|
||||||
expect(rendered).toContain("limit 5");
|
expect(rendered).toContain("limit 5");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user