fix: tavily-search extension — universal AuthStorage compat for @mariozechner/@earendil-works pi packages

This commit is contained in:
2026-07-28 10:55:38 +10:00
parent 06e9e4ee75
commit b82cbf056e

View File

@@ -10,9 +10,27 @@
* patterns (env var, plain auth.json, or shell-resolved 1Password / Keychain).
*/
import { AuthStorage, type ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { type ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { readFileSync, existsSync } from "fs";
import { join } from "path";
import { Type, type Static } from "typebox";
function getTavilyKey(): string | undefined {
// 1. Try env var
const envKey = process.env.TAVILY_API_KEY;
if (envKey) return envKey;
// 2. Try auth.json (works with both old and new pi)
try {
const authPath = join(process.env.HOME || "", ".pi/agent/auth.json");
if (existsSync(authPath)) {
const auth = JSON.parse(readFileSync(authPath, "utf-8"));
const tavily = auth["tavily"];
if (tavily?.type === "api_key") return tavily.key;
}
} catch {}
return undefined;
}
const TAVILY_SEARCH_ENDPOINT = "https://api.tavily.com/search";
// ── Tool parameter schema ──────────────────────────────────────────────
@@ -75,7 +93,6 @@ function formatResults(data: TavilySearchResponse, query: string): string {
// ── Extension factory ──────────────────────────────────────────────────
export default function (pi: ExtensionAPI): void {
const authStorage = AuthStorage.create();
pi.registerTool({
name: "tavily_search",
@@ -84,7 +101,7 @@ export default function (pi: ExtensionAPI): void {
"Performs a web search using the Tavily API to get real-time information from the internet.",
parameters: tavilySearchSchema,
async execute(_toolCallId, params, signal, _onUpdate, _ctx) {
const apiKey = (await authStorage.getApiKey("tavily")) ?? process.env.TAVILY_API_KEY;
const apiKey = getTavilyKey();
if (!apiKey) {
return {
content: [{ type: "text", text: MISSING_KEY_MESSAGE }],