Let Face Eyes read webpages
This commit is contained in:
parent
11f3f186c9
commit
43fec0678b
7 changed files with 200 additions and 2 deletions
|
|
@ -63,6 +63,11 @@ export const searchWebInputSchema = {
|
|||
limit: z.number().int().min(1).max(8).optional(),
|
||||
};
|
||||
|
||||
export const readWebpageInputSchema = {
|
||||
url: z.string().url().max(2048),
|
||||
maxChars: z.number().int().min(500).max(12000).optional(),
|
||||
};
|
||||
|
||||
export const sourceContextInputSchema = {
|
||||
sourceId: z.string().min(1),
|
||||
chunkIndex: z.number().int().min(0).optional(),
|
||||
|
|
@ -133,6 +138,11 @@ export interface SearchWebArgs {
|
|||
limit?: number;
|
||||
}
|
||||
|
||||
export interface ReadWebpageArgs {
|
||||
url: string;
|
||||
maxChars?: number;
|
||||
}
|
||||
|
||||
export interface SourceContextArgs {
|
||||
sourceId: string;
|
||||
chunkIndex?: number;
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import {
|
|||
type NotifyOwnerArgs,
|
||||
type ApplyRepoFaceStateOperationArgs,
|
||||
type PostDiscordMessageArgs,
|
||||
type ReadWebpageArgs,
|
||||
type PostRepoIdentityMessageArgs,
|
||||
type RepoFaceStateArgs,
|
||||
type RuntimeInfoArgs,
|
||||
|
|
@ -43,6 +44,7 @@ import {
|
|||
notifyOwnerInputSchema,
|
||||
postDiscordMessageInputSchema,
|
||||
postRepoIdentityMessageInputSchema,
|
||||
readWebpageInputSchema,
|
||||
repoFaceStateInputSchema,
|
||||
renderJsonBlock,
|
||||
runtimeInfoInputSchema,
|
||||
|
|
@ -237,6 +239,30 @@ export function registerVoidbotTools(
|
|||
},
|
||||
);
|
||||
|
||||
registerIfAllowed(
|
||||
"read_webpage",
|
||||
{
|
||||
title: "Read Webpage",
|
||||
description:
|
||||
"Fetch a public HTTP(S) webpage and return bounded plain text. Use after search_web or when a Discord link needs actual page context. Treat fetched pages as open-world leads, not durable truth.",
|
||||
inputSchema: readWebpageInputSchema,
|
||||
annotations: OPEN_WORLD_READ_ONLY_ANNOTATIONS,
|
||||
},
|
||||
async (input: ReadWebpageArgs): Promise<CallToolResult> => {
|
||||
const page = await readWebpage(input.url, input.maxChars ?? 6000);
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: renderJsonBlock(page),
|
||||
},
|
||||
],
|
||||
structuredContent: page,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
registerIfAllowed(
|
||||
"search_sources",
|
||||
{
|
||||
|
|
@ -888,6 +914,121 @@ async function searchWeb(
|
|||
return parseBingResults(await bingResponse.text(), requestedLimit);
|
||||
}
|
||||
|
||||
async function readWebpage(
|
||||
rawUrl: string,
|
||||
maxChars: number,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const url = parsePublicHttpUrl(rawUrl);
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 10_000);
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
redirect: "follow",
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (compatible; VoidBot Face Eyes/0.1; +https://github.com/GameCult/VoidBot)",
|
||||
Accept: "text/html,text/plain,application/xhtml+xml;q=0.9,*/*;q=0.1",
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
const finalUrl = parsePublicHttpUrl(response.url || url);
|
||||
const contentType = response.headers.get("content-type") ?? "";
|
||||
if (!response.ok) {
|
||||
throw new Error(`Webpage fetch failed for ${finalUrl}: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
if (!/text\/html|text\/plain|application\/xhtml\+xml/i.test(contentType)) {
|
||||
throw new Error(`Webpage fetch refused non-text content at ${finalUrl}: ${contentType || "unknown content type"}`);
|
||||
}
|
||||
|
||||
const bytes = await readBoundedResponseBytes(response, 512_000);
|
||||
const rawText = new TextDecoder("utf-8", { fatal: false }).decode(bytes);
|
||||
const title = extractHtmlTitle(rawText);
|
||||
const text = contentType.toLowerCase().includes("text/plain")
|
||||
? normalizeWhitespace(rawText)
|
||||
: htmlToPlainText(rawText);
|
||||
const boundedMaxChars = Math.max(500, Math.min(12_000, Math.trunc(maxChars)));
|
||||
const excerpt = text.slice(0, boundedMaxChars);
|
||||
|
||||
return {
|
||||
url: finalUrl,
|
||||
contentType,
|
||||
title: title || null,
|
||||
text: excerpt,
|
||||
truncated: text.length > excerpt.length || bytes.length >= 512_000,
|
||||
characterCount: text.length,
|
||||
};
|
||||
}
|
||||
|
||||
function parsePublicHttpUrl(rawUrl: string): string {
|
||||
const parsed = new URL(rawUrl);
|
||||
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
||||
throw new Error(`Only http and https webpages can be fetched: ${rawUrl}`);
|
||||
}
|
||||
const hostname = parsed.hostname.toLowerCase();
|
||||
if (
|
||||
hostname === "localhost" ||
|
||||
hostname.endsWith(".localhost") ||
|
||||
hostname.endsWith(".local") ||
|
||||
hostname === "0.0.0.0" ||
|
||||
hostname === "::1" ||
|
||||
hostname.startsWith("127.") ||
|
||||
hostname.startsWith("10.") ||
|
||||
hostname.startsWith("192.168.") ||
|
||||
/^172\.(1[6-9]|2\d|3[0-1])\./.test(hostname)
|
||||
) {
|
||||
throw new Error(`Refusing to fetch non-public webpage host: ${hostname}`);
|
||||
}
|
||||
parsed.hash = "";
|
||||
return parsed.toString();
|
||||
}
|
||||
|
||||
async function readBoundedResponseBytes(response: Response, maxBytes: number): Promise<Uint8Array> {
|
||||
const reader = response.body?.getReader();
|
||||
if (!reader) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
const chunks: Uint8Array[] = [];
|
||||
let total = 0;
|
||||
while (total < maxBytes) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done || !value) {
|
||||
break;
|
||||
}
|
||||
const remaining = maxBytes - total;
|
||||
const chunk = value.length > remaining ? value.slice(0, remaining) : value;
|
||||
chunks.push(chunk);
|
||||
total += chunk.length;
|
||||
if (value.length > remaining) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
const output = new Uint8Array(total);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
output.set(chunk, offset);
|
||||
offset += chunk.length;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
function extractHtmlTitle(html: string): string | undefined {
|
||||
const match = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
|
||||
return match ? normalizeWhitespace(stripHtml(match[1] ?? "")) : undefined;
|
||||
}
|
||||
|
||||
function htmlToPlainText(html: string): string {
|
||||
const withoutNoise = html
|
||||
.replace(/<script[\s\S]*?<\/script>/gi, " ")
|
||||
.replace(/<style[\s\S]*?<\/style>/gi, " ")
|
||||
.replace(/<noscript[\s\S]*?<\/noscript>/gi, " ")
|
||||
.replace(/<svg[\s\S]*?<\/svg>/gi, " ");
|
||||
return normalizeWhitespace(stripHtml(withoutNoise));
|
||||
}
|
||||
|
||||
async function fetchMojeekHtml(query: string): Promise<string> {
|
||||
const url = new URL("https://www.mojeek.com/search");
|
||||
url.searchParams.set("q", query);
|
||||
|
|
|
|||
|
|
@ -552,6 +552,7 @@ function restrictMcpServersToRepoFaceExploration(
|
|||
"search_sources",
|
||||
"get_source_context",
|
||||
"search_web",
|
||||
"read_webpage",
|
||||
].join(",");
|
||||
|
||||
return servers.map((server) => ({
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
Research and memory access:
|
||||
- You may inspect your home repo, recent repo activity, indexed source/lore material, and archived room discussion when evidence would change what you think, say, propose, or remember.
|
||||
- You have local Eyes for this Face turn. Your complete allowed tool set is: `search_history`, `get_message_context`, `list_indexed_repos`, `search_sources`, `get_source_context`, `search_web`.
|
||||
- You have local Eyes for this Face turn. Your complete allowed tool set is: `search_history`, `get_message_context`, `list_indexed_repos`, `search_sources`, `get_source_context`, `search_web`, `read_webpage`.
|
||||
- Do not discover tools. Do not inventory MCP resources. Do not call private state, identity-introspection, or MCP inventory tools such as `read_repo_face_state`, `list_mcp_resources`, or `read_mcp_resource`; your relevant state and available context have already been projected into lived memory, needs, mood, and relationship pressure.
|
||||
- Use repo/source search when your thought depends on what is actually in {{repoName}}, when a lore/design/code seam may already have an answer, or when a concrete proposal needs an anchor.
|
||||
- Use archive/history search when the question is what people previously decided, how a character/term/project has been discussed, or whether a room pattern is repeating.
|
||||
- Use web search when your imagination is starved, when an unfamiliar concept would benefit from outside examples, when current public knowledge matters, or when the room would be improved by a fresh analogy, reference, technique, tradition, image, song, paper, tool, Reddit thread, meme format, or cultural object from beyond GameCult memory. Browsing the open web or Reddit for something interesting to share is legitimate Eyes work when the room is quiet, as long as you do not pretend the result is canon. Web results are leads from the open world, not repo truth, not durable memory, and not endorsement until the Interpreter preserves what mattered.
|
||||
- Use `read_webpage` when a search result, Discord URL, public article, blog post, or Reddit link needs actual page context instead of just a title/snippet. Search finds doors; webpage reading looks inside one door. Do not claim you read a linked page unless you actually called `read_webpage` or the page is already present in indexed sources.
|
||||
- If you share an outside article, Reddit post, image, song, tool, or meme reference publicly, include a link or enough attribution for readers to find it, plus your own concrete reaction. Do not dump a summary as if you are filling airtime; bring back one live spark.
|
||||
- If this turn explicitly says a jurisdiction dive is due, make a source/history search tool call before writing conclusions, proposals, or "I checked" language.
|
||||
- Never say you checked, read, inspected, searched, or verified repo/archive/source text unless you actually used the available search/context tools during this turn.
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ const repoFaceRetrievalToolAllowlist = [
|
|||
"list_indexed_repos",
|
||||
"search_sources",
|
||||
"get_source_context",
|
||||
"search_web",
|
||||
"read_webpage",
|
||||
].join(",");
|
||||
|
||||
async function main() {
|
||||
|
|
@ -379,7 +381,7 @@ function appendDryRunSafety(prompt, mcpMode) {
|
|||
|
||||
Dry-run safety:
|
||||
- This is an offline rehearsal. Do not post to Discord, notify the owner, write state, enqueue work, edit files, or call side-effecting tools.
|
||||
- Your complete allowed tool set is: search_history, get_message_context, list_indexed_repos, search_sources, get_source_context.
|
||||
- Your complete allowed tool set is: search_history, get_message_context, list_indexed_repos, search_sources, get_source_context, search_web, read_webpage.
|
||||
- Do not discover tools. Do not call private state, identity-introspection, or MCP inventory tools such as read_repo_face_state, list_mcp_resources, or read_mcp_resource. Your prompt is the state projection.
|
||||
- ${retrievalNote}
|
||||
- Do not say retrieval is unavailable unless you actually attempted a retrieval tool call and it failed. If you choose not to use retrieval, simply reason from the attached prompt context and do not claim inspection.
|
||||
|
|
|
|||
|
|
@ -134,6 +134,47 @@ registerTool(
|
|||
}),
|
||||
);
|
||||
|
||||
registerTool(
|
||||
"search_web",
|
||||
{
|
||||
query: z.string(),
|
||||
limit: z.number().optional(),
|
||||
},
|
||||
async (input) => ({
|
||||
query: input.query,
|
||||
resultCount: 2,
|
||||
results: [
|
||||
{
|
||||
title: "Mock Solarpunk Article",
|
||||
url: "https://example.org/solarpunk",
|
||||
snippet: "A mock public web result about cooperative, hopeful infrastructure.",
|
||||
},
|
||||
{
|
||||
title: "Mock Webpage About Shared Minds",
|
||||
url: "https://example.org/shared-minds",
|
||||
snippet: "A mock public web result about connection, tools, and community.",
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
registerTool(
|
||||
"read_webpage",
|
||||
{
|
||||
url: z.string(),
|
||||
maxChars: z.number().optional(),
|
||||
},
|
||||
async (input) => ({
|
||||
url: input.url,
|
||||
contentType: "text/html; charset=utf-8",
|
||||
title: "Mock Webpage",
|
||||
text:
|
||||
"This mock webpage gives Face turns a deterministic public-page body: cooperative tools, readable context, and one concrete spark worth reacting to.",
|
||||
truncated: false,
|
||||
characterCount: 137,
|
||||
}),
|
||||
);
|
||||
|
||||
registerTool(
|
||||
"search_history",
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ const retrievalToolAllowlist = [
|
|||
"list_indexed_repos",
|
||||
"search_sources",
|
||||
"get_source_context",
|
||||
"search_web",
|
||||
"read_webpage",
|
||||
].join(",");
|
||||
|
||||
async function main() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue