Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): let Antigravity read public web pages when the chat allows it
Elian Doran2638f4648f7e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 03f46ece11..b16231cf72 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4121,7 +4121,8 @@"clone_note": "Clone note","search_icons": "Search icons","search_help": "Search User Guide",- "get_help_toc": "Get User Guide contents"+ "get_help_toc": "Get User Guide contents",+ "read_web_page": "Read web page"}},"common": {diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex 561d29c267..44e7cff695 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -48,7 +48,7 @@ function getToolCallContext(toolCall: ToolCall): ToolCallContext {return { noteId, parentNoteId: null, detailText: null };}- const detailText = (input?.name ?? input?.query) as string | undefined;+ const detailText = (input?.name ?? input?.query ?? input?.url) as string | undefined;return { noteId: null, parentNoteId: null, detailText: detailText || null };}diff --git a/apps/server/src/services/llm/providers/acp_agent.ts b/apps/server/src/services/llm/providers/acp_agent.tsindex 2c72cd7e72..642c8b4b5e 100644--- a/apps/server/src/services/llm/providers/acp_agent.ts+++ b/apps/server/src/services/llm/providers/acp_agent.ts@@ -44,6 +44,12 @@ export interface AcpLaunchSpec {/** The ACP permission callback's answer. */export type AcpPermissionOutcome = { outcome: { outcome: "selected"; optionId: string } | { outcome: "cancelled" } };+/** How the chat shows a built-in tool call: a name it has a label for, and the input it reads. */+export interface BuiltInToolDisplay {+ toolName: string;+ toolInput?: Record<string, unknown>;+}+export interface AcpPermissionRequest {sessionId?: string;toolCall?: {@@ -188,7 +194,7 @@ export abstract class AcpAgentProvider implements LlmProvider {* `config` is the chat turn's configuration, and absent outside a chat* turn (the model probe, the title).*/- protected decidePermission(request: AcpPermissionRequest, _config?: LlmProviderConfig): AcpPermissionOutcome {+ protected decidePermission(request: AcpPermissionRequest, _config?: LlmProviderConfig): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {return denyPermission(request, this.logLabel);}@@ -203,9 +209,10 @@ export abstract class AcpAgentProvider implements LlmProvider {/*** The chat's name for a built-in tool call it has a label for, such as- * `web_search`; undefined shows the call's title. Display only.+ * `web_search`, and optionally its input in the shape the chat reads;+ * undefined shows the call's title. Display only.*/- protected builtInToolName(_update: AcpToolCallUpdate): string | undefined {+ protected describeBuiltInTool(_update: AcpToolCallUpdate): BuiltInToolDisplay | undefined {return undefined;}@@ -341,7 +348,7 @@ export abstract class AcpAgentProvider implements LlmProvider {wakeup?.();};- const collector = createUpdateCollector(emit, update => this.isInternalToolCall(update), update => this.builtInToolName(update));+ const collector = createUpdateCollector(emit, update => this.isInternalToolCall(update), update => this.describeBuiltInTool(update));let client: AcpClient | undefined;let sessionId: string | undefined;let sessionModels: AcpSessionModelState | undefined;@@ -634,13 +641,13 @@ export function denyPermission(request: AcpPermissionRequest, logLabel: string):* pushes them through `emit`. `muted` suppresses the replay flood during* session/load; `sessionId` filters stray updates from other sessions.* `isHidden` keeps a tool call, and with it every update for that call, out- * of the chat. `nameTool` names a built-in tool call the chat has a label for;- * a call it leaves unnamed shows its title.+ * of the chat. `describeBuiltIn` names a built-in tool call the chat has a+ * label for; a call it leaves undescribed shows its title.*/export function createUpdateCollector(emit: (chunk: LlmStreamChunk) => void,isHidden?: (update: AcpToolCallUpdate) => boolean,- nameTool?: (update: AcpToolCallUpdate) => string | undefined+ describeBuiltIn?: (update: AcpToolCallUpdate) => BuiltInToolDisplay | undefined) {// toolCallId → display name, for labelling results; also the guard that// only this turn's tool calls produce result chunks.@@ -678,13 +685,14 @@ export function createUpdateCollector(break; // malformed, a re-announcement of a known call, or hidden}const mcpTool = mcpToolName(update._meta);- const toolName = mcpTool ?? nameTool?.(update) ?? (update.title || "tool");+ const builtIn = mcpTool ? undefined : describeBuiltIn?.(update);+ const toolName = mcpTool ?? builtIn?.toolName ?? (update.title || "tool");toolNamesById.set(update.toolCallId, toolName);emit({type: "tool_use",toolCallId: update.toolCallId,toolName,- toolInput: (mcpTool ? unwrapMcpArguments(update.rawInput) : update.rawInput ?? {}) as Record<string, unknown>+ toolInput: (mcpTool ? unwrapMcpArguments(update.rawInput) : builtIn?.toolInput ?? update.rawInput ?? {}) as Record<string, unknown>});break;}diff --git a/apps/server/src/services/llm/providers/antigravity_agent.spec.ts b/apps/server/src/services/llm/providers/antigravity_agent.spec.tsindex b5085bb4c8..acb8201805 100644--- a/apps/server/src/services/llm/providers/antigravity_agent.spec.ts+++ b/apps/server/src/services/llm/providers/antigravity_agent.spec.ts@@ -236,6 +236,28 @@ describe("AntigravityAgentProvider", () => {.toEqual({ outcome: { outcome: "selected", optionId: "deny" } });});+ it("lets the agent read a public web page only when the chat allows web access", async () => {+ // Shape captured from agy_acp_server 1.2.1. IP literals need no DNS.+ const readUrl = (url: string) => ({+ toolCall: { kind: "fetch", status: "pending", title: "Run read_url_content?", rawInput: { Url: url } },+ options: PERMISSION_OPTIONS+ });+ const allowed = { outcome: { outcome: "selected", optionId: "allow" } };+ const denied = { outcome: { outcome: "selected", optionId: "deny" } };++ await collect(new AntigravityAgentProvider().chatChunks([{ role: "user", content: "hi" }], { enableWebSearch: true }));+ const decide = (request: unknown) => FakeAcpClient.current?.onAgentRequest?.("session/request_permission", request);+ expect(await decide(readUrl("https://93.184.215.14/"))).toEqual(allowed);+ expect(await decide(readUrl("http://127.0.0.1:8080/"))).toEqual(denied);+ expect(await decide(readUrl("file:///C:/antigravity-acp/acp_token.json"))).toEqual(denied);+ // A shell command the model titled like a page read.+ expect(await decide({ ...readUrl("https://93.184.215.14/"), toolCall: { kind: "execute", title: "Run read_url_content?", rawInput: { Url: "https://93.184.215.14/" } } }))+ .toEqual(denied);++ await collect(new AntigravityAgentProvider().chatChunks([{ role: "user", content: "hi" }], {}));+ expect(await FakeAcpClient.current?.onAgentRequest?.("session/request_permission", readUrl("https://93.184.215.14/"))).toEqual(denied);+ });+it("signs in during the model probe, but reports a missing sign-in in the chat instead", async () => {FakeAcpClient.signedIn = false;const chunks = await collect(new AntigravityAgentProvider().chatChunks([{ role: "user", content: "hi" }], {}));@@ -360,14 +382,16 @@ describe("AntigravityAgentProvider tool calls", () => {FakeAcpClient.promptUpdates = [{ sessionUpdate: "tool_call", toolCallId: "7bba", title: "Run search_web?", kind: "search", status: "pending", rawInput: { query: "weather Sibiu today" } },{ sessionUpdate: "tool_call_update", toolCallId: "7bba", status: "completed", rawOutput: "Sunny" },- { sessionUpdate: "tool_call", toolCallId: "sh1", title: "Run search_web?", kind: "execute", status: "pending", rawInput: { CommandLine: "Run search_web?" } }+ { sessionUpdate: "tool_call", toolCallId: "sh1", title: "Run search_web?", kind: "execute", status: "pending", rawInput: { CommandLine: "Run search_web?" } },+ { sessionUpdate: "tool_call", toolCallId: "0601", title: "Run read_url_content?", kind: "fetch", status: "pending", rawInput: { Url: "https://en.wikipedia.org/wiki/Quantum_computing" } }];const chunks = await collect(new AntigravityAgentProvider().chatChunks([{ role: "user", content: "hi" }], { enableWebSearch: true }));expect(chunks.filter(c => c.type === "tool_use" || c.type === "tool_result")).toEqual([{ type: "tool_use", toolCallId: "7bba", toolName: "web_search", toolInput: { query: "weather Sibiu today" } },{ type: "tool_result", toolCallId: "7bba", toolName: "web_search", result: "Sunny", isError: false },- { type: "tool_use", toolCallId: "sh1", toolName: "Run search_web?", toolInput: { CommandLine: "Run search_web?" } }+ { type: "tool_use", toolCallId: "sh1", toolName: "Run search_web?", toolInput: { CommandLine: "Run search_web?" } },+ { type: "tool_use", toolCallId: "0601", toolName: "read_web_page", toolInput: { url: "https://en.wikipedia.org/wiki/Quantum_computing" } }]);});});diff --git a/apps/server/src/services/llm/providers/antigravity_agent.ts b/apps/server/src/services/llm/providers/antigravity_agent.tsindex 5231c4f029..dbb4d57317 100644--- a/apps/server/src/services/llm/providers/antigravity_agent.ts+++ b/apps/server/src/services/llm/providers/antigravity_agent.ts@@ -25,11 +25,12 @@ import { existsSync } from "fs";import path from "path";import dataDirs from "../../data_dir.js";-import { AcpAgentProvider, type AcpLaunchSpec, type AcpNewSessionParams, type AcpPermissionOutcome, type AcpPermissionRequest, type AcpSessionModelState, type AcpToolCallUpdate, denyPermission, describeError, NOTE_TOOLS_MCP_SERVER_NAME } from "./acp_agent.js";+import { AcpAgentProvider, type AcpLaunchSpec, type AcpNewSessionParams, type AcpPermissionOutcome, type AcpPermissionRequest, type AcpSessionModelState, type AcpToolCallUpdate, type BuiltInToolDisplay, denyPermission, describeError, NOTE_TOOLS_MCP_SERVER_NAME } from "./acp_agent.js";import { type AcpClient, AcpError } from "./acp_client.js";import { getAcpHookEndpointUrl } from "./acp_mcp_endpoint.js";import { resolveAntigravityBinaryPath } from "./antigravity_binary.js";import { type AntigravityDirs, type AntigravityHookDecision, buildHookCommand, decideAntigravityToolCall, resolveCurlPath, writeAntigravityHooks } from "./antigravity_hook.js";+import { isPublicHttpUrl } from "./public_url.js";/** The model id that leaves the session on the model the server picks. */const DEFAULT_MODEL_ID = "default";@@ -133,8 +134,12 @@ export class AntigravityAgentProvider extends AcpAgentProvider {return buildAntigravityModelList(remote);}- protected decidePermission(request: AcpPermissionRequest, config?: LlmProviderConfig): AcpPermissionOutcome {- return decideAntigravityPermission(request, this.logLabel, { webSearch: config?.enableWebSearch === true });+ protected decidePermission(request: AcpPermissionRequest, config?: LlmProviderConfig): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {+ const webSearch = config?.enableWebSearch === true;+ if (webSearch && isUrlRead(request.toolCall)) {+ return decideAntigravityUrlRead(request, this.logLabel);+ }+ return decideAntigravityPermission(request, this.logLabel, { webSearch });}/** Answer the file-access hook for one tool call, logging each denial. */@@ -157,9 +162,18 @@ export class AntigravityAgentProvider extends AcpAgentProvider {return isToolDescriptionAccess(update, agentHome());}- /** The web search, under the name the other providers' searches carry. */- protected builtInToolName(update: AcpToolCallUpdate): string | undefined {- return isWebSearch(update) ? "web_search" : undefined;+ /**+ * The web search, under the name the other providers' searches carry, and+ * the page read, with the URL where the chat looks for a detail.+ */+ protected describeBuiltInTool(update: AcpToolCallUpdate): BuiltInToolDisplay | undefined {+ if (isWebSearch(update)) {+ return { toolName: "web_search" };+ }+ if (isUrlRead(update)) {+ return { toolName: "read_web_page", toolInput: { url: requestedUrl(update.rawInput) } };+ }+ return undefined;}/**@@ -399,6 +413,35 @@ function agentHome(): string {return path.resolve(dataDirs.TRILIUM_DATA_DIR, "antigravity-agent", "home");}+/**+ * Approve reading a web page once when its URL leads to the public internet+ * (see {@link isPublicHttpUrl}), and deny it otherwise. The caller has checked+ * that the chat allows web access and that the request is the page read.+ */+export async function decideAntigravityUrlRead(request: AcpPermissionRequest, logLabel: string): Promise<AcpPermissionOutcome> {+ const url = requestedUrl(request.toolCall?.rawInput);+ const allowOnce = request.options?.find(o => o.kind === "allow_once");+ if (url && allowOnce && await isPublicHttpUrl(url)) {+ return { outcome: { outcome: "selected", optionId: allowOnce.optionId } };+ }+ return denyPermission(request, logLabel);+}++/**+ * Whether a tool call or permission request is the server's page read: the+ * kind `fetch` with the title the server gives it. The model writes only the+ * URL, which travels in the input.+ */+function isUrlRead(toolCall: { kind?: string; title?: string } | undefined): boolean {+ return toolCall?.kind === "fetch" && /^Run(?:ning)? read_url_content\??$/.test(toolCall.title ?? "");+}++/** The URL a page read names, in its `Url` argument. */+function requestedUrl(rawInput: unknown): string | undefined {+ const url = (rawInput as { Url?: unknown } | null | undefined)?.Url;+ return typeof url === "string" ? url : undefined;+}+/*** Whether a tool call or permission request is the server's web search: the* kind `search` with the title the server gives it, `Run search_web?` while itdiff --git a/apps/server/src/services/llm/providers/public_url.spec.ts b/apps/server/src/services/llm/providers/public_url.spec.tsnew file mode 100644index 0000000000..23c28ee33a--- /dev/null+++ b/apps/server/src/services/llm/providers/public_url.spec.ts@@ -0,0 +1,64 @@+import { describe, expect, it } from "vitest";++import { isPublicHttpUrl } from "./public_url.js";++/** Resolves the names a test lists, and fails like DNS for any other. */+function lookupFrom(records: Record<string, string[]>) {+ return async (hostname: string) => {+ const addresses = records[hostname];+ if (!addresses) {+ throw new Error(`getaddrinfo ENOTFOUND ${hostname}`);+ }+ return addresses;+ };+}++const lookup = lookupFrom({+ "localhost": [ "::1", "127.0.0.1" ],+ "en.wikipedia.org": [ "185.15.59.224", "2a02:ec80:300:ed1a::1" ],+ "intranet.example": [ "10.0.0.5" ],+ "mixed.example": [ "93.184.215.14", "192.168.1.10" ],+ "rebind.example": [ "127.0.0.1" ]+});++describe("isPublicHttpUrl", () => {+ it("accepts an http(s) URL whose host has only public addresses", async () => {+ expect(await isPublicHttpUrl("https://en.wikipedia.org/wiki/Quantum_computing", lookup)).toBe(true);+ expect(await isPublicHttpUrl("http://93.184.215.14/", lookup)).toBe(true);+ expect(await isPublicHttpUrl("https://[2a02:ec80:300:ed1a::1]/", lookup)).toBe(true);+ });++ it("refuses other schemes and URLs that do not parse", async () => {+ for (const url of [ "file:///C:/data/antigravity-acp/acp_token.json", "ftp://en.wikipedia.org/", "data:text/plain,hi", "not a url" ]) {+ expect(await isPublicHttpUrl(url, lookup), url).toBe(false);+ }+ });++ it("refuses local, private and reserved addresses, however they are written", async () => {+ for (const url of [+ "http://localhost:8080/",+ "http://127.0.0.1:8080/",+ "http://2130706433/", // 127.0.0.1 as one number+ "http://0.0.0.0/",+ "http://10.1.2.3/",+ "http://172.16.0.1/",+ "http://192.168.1.1/",+ "http://100.64.0.1/",+ "http://169.254.169.254/latest/meta-data/",+ "http://224.0.0.1/",+ "http://[::1]/",+ "http://[fe80::1]/",+ "http://[fd00::1]/",+ "http://[::ffff:127.0.0.1]/"+ ]) {+ expect(await isPublicHttpUrl(url, lookup), url).toBe(false);+ }+ });++ it("refuses a name that resolves to any non-public address, or not at all", async () => {+ expect(await isPublicHttpUrl("https://intranet.example/", lookup)).toBe(false);+ expect(await isPublicHttpUrl("https://mixed.example/", lookup)).toBe(false);+ expect(await isPublicHttpUrl("https://rebind.example/", lookup)).toBe(false);+ expect(await isPublicHttpUrl("https://unknown.example/", lookup)).toBe(false);+ });+});diff --git a/apps/server/src/services/llm/providers/public_url.ts b/apps/server/src/services/llm/providers/public_url.tsnew file mode 100644index 0000000000..67a3d3b1a9--- /dev/null+++ b/apps/server/src/services/llm/providers/public_url.ts@@ -0,0 +1,75 @@+/**+ * Decides whether an agent can fetch a URL: only `http(s)` URLs whose host is+ * on the public internet. A fetch runs on the device hosting Trilium, so a+ * local or private address would reach services that are not meant to be+ * reachable from outside, such as a router or a cloud metadata endpoint.+ *+ * The host is resolved when the fetch is approved. A server that redirects to+ * a local address, or a name whose DNS answer changes before the agent+ * fetches it, is not caught here.+ */++import { lookup as dnsLookup } from "dns/promises";+import { BlockList, isIP } from "net";++/** Every address a host name resolves to. */+export type HostLookup = (hostname: string) => Promise<string[]>;++export async function isPublicHttpUrl(url: string, lookup: HostLookup = lookupAll): Promise<boolean> {+ let parsed: URL;+ try {+ parsed = new URL(url);+ } catch {+ return false;+ }+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {+ return false;+ }++ // The URL parser writes an IPv6 host in brackets and any IPv4 form as a dotted quad.+ const host = parsed.hostname.replace(/^\[(.*)\]$/, "$1");+ let addresses: string[];+ if (isIP(host)) {+ addresses = [ host ];+ } else {+ try {+ addresses = await lookup(host);+ } catch {+ return false;+ }+ }+ return addresses.length > 0 && addresses.every(isPublicAddress);+}++async function lookupAll(hostname: string): Promise<string[]> {+ const records = await dnsLookup(hostname, { all: true, verbatim: true });+ return records.map(record => record.address);+}++/** `BlockList` also checks an IPv4-mapped IPv6 address against the IPv4 ranges. */+function isPublicAddress(address: string): boolean {+ return !nonPublicAddresses().check(address, isIP(address) === 6 ? "ipv6" : "ipv4");+}++let cachedBlockList: BlockList | undefined;++/** Loopback, private, link-local, shared, multicast and reserved ranges (RFC 6890). */+function nonPublicAddresses(): BlockList {+ if (!cachedBlockList) {+ const list = new BlockList();+ for (const [ network, prefix ] of [+ [ "0.0.0.0", 8 ], [ "10.0.0.0", 8 ], [ "100.64.0.0", 10 ], [ "127.0.0.0", 8 ],+ [ "169.254.0.0", 16 ], [ "172.16.0.0", 12 ], [ "192.0.0.0", 24 ], [ "192.168.0.0", 16 ],+ [ "198.18.0.0", 15 ], [ "224.0.0.0", 4 ], [ "240.0.0.0", 4 ]+ ] as const) {+ list.addSubnet(network, prefix, "ipv4");+ }+ for (const [ network, prefix ] of [+ [ "::", 128 ], [ "::1", 128 ], [ "fc00::", 7 ], [ "fe80::", 10 ], [ "ff00::", 8 ]+ ] as const) {+ list.addSubnet(network, prefix, "ipv6");+ }+ cachedBlockList = list;+ }+ return cachedBlockList;+}