feat(fetch): added optional Kagi summarizer toggle to fetch configuration
- Added `fetch.useKagiSummarizer` configuration setting to toggle Kagi Universal Summarizer usage in fetch tool. - Updated fetch tool to conditionally apply Kagi summarization based on configuration setting. - Added comprehensive test coverage for Kagi summarizer toggle behavior with mocked dependencies.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
## [Unreleased]
|
||||
### Added
|
||||
- Added `fetch.useKagiSummarizer` setting to toggle Kagi Universal Summarizer usage in the fetch tool.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fixed incorrect message history reference in session title generation that could cause missing or stale titles on first message
|
||||
|
||||
@@ -490,6 +490,15 @@ export const SETTINGS_SCHEMA = {
|
||||
default: true,
|
||||
ui: { tab: "tools", label: "Enable Fetch", description: "Enable the fetch tool for URL fetching" },
|
||||
},
|
||||
"fetch.useKagiSummarizer": {
|
||||
type: "boolean",
|
||||
default: true,
|
||||
ui: {
|
||||
tab: "tools",
|
||||
label: "Use Kagi in Fetch",
|
||||
description: "Use Kagi Universal Summarizer when rendering HTML in fetch",
|
||||
},
|
||||
},
|
||||
"web_search.enabled": {
|
||||
type: "boolean",
|
||||
default: true,
|
||||
|
||||
@@ -429,6 +429,7 @@ async function renderHtmlToText(
|
||||
url: string,
|
||||
html: string,
|
||||
timeout: number,
|
||||
useKagiSummarizer: boolean,
|
||||
userSignal?: AbortSignal,
|
||||
): Promise<{ content: string; ok: boolean; method: string }> {
|
||||
const signal = ptree.combineSignals(userSignal, timeout * 1000);
|
||||
@@ -440,15 +441,17 @@ async function renderHtmlToText(
|
||||
signal,
|
||||
};
|
||||
|
||||
// Try Kagi Universal Summarizer first (if KAGI_API_KEY is configured)
|
||||
try {
|
||||
const kagiSummary = await summarizeUrlWithKagi(url, { signal });
|
||||
if (kagiSummary && kagiSummary.length > 100 && !isLowQualityOutput(kagiSummary)) {
|
||||
return { content: kagiSummary, ok: true, method: "kagi" };
|
||||
// Try Kagi Universal Summarizer first (if enabled and KAGI_API_KEY is configured)
|
||||
if (useKagiSummarizer) {
|
||||
try {
|
||||
const kagiSummary = await summarizeUrlWithKagi(url, { signal });
|
||||
if (kagiSummary && kagiSummary.length > 100 && !isLowQualityOutput(kagiSummary)) {
|
||||
return { content: kagiSummary, ok: true, method: "kagi" };
|
||||
}
|
||||
} catch {
|
||||
// Kagi failed, continue to next method
|
||||
signal?.throwIfAborted();
|
||||
}
|
||||
} catch {
|
||||
// Kagi failed, continue to next method
|
||||
signal?.throwIfAborted();
|
||||
}
|
||||
|
||||
// Try jina next (reader API)
|
||||
@@ -564,7 +567,13 @@ async function handleSpecialUrls(url: string, timeout: number, signal?: AbortSig
|
||||
/**
|
||||
* Main render function implementing the full pipeline
|
||||
*/
|
||||
async function renderUrl(url: string, timeout: number, raw: boolean, signal?: AbortSignal): Promise<RenderResult> {
|
||||
async function renderUrl(
|
||||
url: string,
|
||||
timeout: number,
|
||||
raw: boolean,
|
||||
useKagiSummarizer: boolean,
|
||||
signal?: AbortSignal,
|
||||
): Promise<RenderResult> {
|
||||
const notes: string[] = [];
|
||||
const fetchedAt = new Date().toISOString();
|
||||
if (signal?.aborted) {
|
||||
@@ -803,7 +812,7 @@ async function renderUrl(url: string, timeout: number, raw: boolean, signal?: Ab
|
||||
}
|
||||
|
||||
// Step 6: Render HTML with lynx or html2text
|
||||
const htmlResult = await renderHtmlToText(finalUrl, rawContent, timeout, signal);
|
||||
const htmlResult = await renderHtmlToText(finalUrl, rawContent, timeout, useKagiSummarizer, signal);
|
||||
if (!htmlResult.ok) {
|
||||
notes.push("html rendering failed (lynx/html2text unavailable)");
|
||||
const output = finalizeOutput(rawContent);
|
||||
@@ -926,7 +935,8 @@ export class FetchTool implements AgentTool<typeof fetchSchema, FetchToolDetails
|
||||
throw new ToolAbortError();
|
||||
}
|
||||
|
||||
const result = await renderUrl(url, effectiveTimeout, raw, signal);
|
||||
const useKagiSummarizer = this.session.settings.get("fetch.useKagiSummarizer");
|
||||
const result = await renderUrl(url, effectiveTimeout, raw, useKagiSummarizer, signal);
|
||||
const truncation = truncateHead(result.content, {
|
||||
maxBytes: DEFAULT_MAX_BYTES,
|
||||
maxLines: FETCH_DEFAULT_MAX_LINES,
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
|
||||
import * as fs from "node:fs";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import { type SettingPath, Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
||||
import type { ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
|
||||
import { FetchTool } from "@oh-my-pi/pi-coding-agent/tools/fetch";
|
||||
import * as toolsManager from "@oh-my-pi/pi-coding-agent/utils/tools-manager";
|
||||
import * as kagi from "@oh-my-pi/pi-coding-agent/web/kagi";
|
||||
import type { LoadPageResult } from "@oh-my-pi/pi-coding-agent/web/scrapers/types";
|
||||
import * as scrapers from "@oh-my-pi/pi-coding-agent/web/scrapers/types";
|
||||
import { Snowflake } from "@oh-my-pi/pi-utils";
|
||||
|
||||
describe("fetch tool Kagi summarization toggle", () => {
|
||||
let testDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
testDir = path.join(os.tmpdir(), `fetch-kagi-toggle-${Snowflake.next()}`);
|
||||
fs.mkdirSync(testDir, { recursive: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
fs.rmSync(testDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const createSession = (overrides: Partial<Record<SettingPath, unknown>> = {}): ToolSession => {
|
||||
const sessionFile = path.join(testDir, "session.jsonl");
|
||||
return {
|
||||
cwd: testDir,
|
||||
hasUI: false,
|
||||
getSessionFile: () => sessionFile,
|
||||
getSessionSpawns: () => null,
|
||||
settings: Settings.isolated({
|
||||
"fetch.enabled": true,
|
||||
...overrides,
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
const mockLoadPage = () => {
|
||||
const pageResponse = (url: string): LoadPageResult => {
|
||||
if (url === "https://example.com") {
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
contentType: "text/html",
|
||||
finalUrl: "https://example.com",
|
||||
content: "<html><body><h1>Example Domain</h1><p>Short sample content.</p></body></html>",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: false,
|
||||
status: 404,
|
||||
contentType: "",
|
||||
finalUrl: url,
|
||||
content: "",
|
||||
};
|
||||
};
|
||||
|
||||
return vi
|
||||
.spyOn(scrapers, "loadPage")
|
||||
.mockImplementation(async (url: string, _options?: unknown) => pageResponse(url));
|
||||
};
|
||||
|
||||
it("uses Kagi summarizer when enabled", async () => {
|
||||
const session = createSession({ "fetch.useKagiSummarizer": true });
|
||||
const tool = new FetchTool(session);
|
||||
const loadPageSpy = mockLoadPage();
|
||||
const summarizeSpy = vi.spyOn(kagi, "summarizeUrlWithKagi").mockResolvedValue("x".repeat(150));
|
||||
vi.spyOn(toolsManager, "ensureTool").mockResolvedValue(undefined);
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("blocked", { status: 500, statusText: "Blocked" }));
|
||||
|
||||
const result = await tool.execute("fetch-1", { url: "https://example.com" });
|
||||
|
||||
expect(loadPageSpy).toHaveBeenCalled();
|
||||
expect(summarizeSpy).toHaveBeenCalledTimes(1);
|
||||
expect(result.details?.method).toBe("kagi");
|
||||
});
|
||||
|
||||
it("skips Kagi summarizer when disabled", async () => {
|
||||
const session = createSession({ "fetch.useKagiSummarizer": false });
|
||||
const tool = new FetchTool(session);
|
||||
const loadPageSpy = mockLoadPage();
|
||||
const summarizeSpy = vi.spyOn(kagi, "summarizeUrlWithKagi").mockResolvedValue("x".repeat(150));
|
||||
vi.spyOn(toolsManager, "ensureTool").mockResolvedValue(undefined);
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("blocked", { status: 500, statusText: "Blocked" }));
|
||||
|
||||
const result = await tool.execute("fetch-2", { url: "https://example.com" });
|
||||
|
||||
expect(loadPageSpy).toHaveBeenCalled();
|
||||
expect(summarizeSpy).not.toHaveBeenCalled();
|
||||
expect(result.details?.method).not.toBe("kagi");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user