Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
feat: add webfetch tool for fetching URL content
Rishabh Bajpai0f30a09398e4
diff --git a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt b/app/src/main/java/com/gotcha/tools/ToolDefinitions.ktindex b0608b1..f01eafd 100644--- a/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt+++ b/app/src/main/java/com/gotcha/tools/ToolDefinitions.kt@@ -682,6 +682,26 @@ object ToolDefinitions {})+ val webFetch = tool(+ "webfetch",+ "Fetch content from an HTTP or HTTPS URL and return it as text. " ++ "Use this to look up documentation, read articles, or gather information from the web. " ++ "Only text responses (HTML, JSON, XML) are supported; binary content is rejected.",+ schema {+ putJsonObject("properties") {+ putJsonObject("url") {+ put("type", "string")+ put("description", "HTTP or HTTPS URL to fetch.")+ }+ putJsonObject("format") {+ put("type", "string")+ put("description", "Optional output format: 'text' (default) or 'markdown'. Currently always returns text.")+ }+ }+ putJsonArray("required") { add("url") }+ }+ )+val readImage = tool("read_image","Read an image file from an allowed directory and feed its visual content to the " +@@ -984,6 +1004,8 @@ object ToolDefinitions {edit,// Content search and file discoveryglob, grep,+ // Web fetching+ webFetch,// Image reading (available to both Monitor and Operator)readImage,// Tier 3 additionsdiff --git a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt b/app/src/main/java/com/gotcha/tools/ToolExecutor.ktindex 0194c3b..1fd3925 100644--- a/app/src/main/java/com/gotcha/tools/ToolExecutor.kt+++ b/app/src/main/java/com/gotcha/tools/ToolExecutor.kt@@ -35,6 +35,7 @@ class ToolExecutor(context: Context) {private val clipboardTool = ClipboardTool(appContext)private val mediaCaptureTool = MediaCaptureTool(appContext)// Tier 3 tools+ private val webFetchTool = WebFetchTool()private val questionTool = QuestionTool()private val todoTool = TodoTool()private val editTool = EditTool(appContext)@@ -147,6 +148,10 @@ class ToolExecutor(context: Context) {options = args["options"]?.jsonArray?.mapNotNull { it.jsonPrimitive?.content },allowCustom = args["allowCustom"]?.jsonPrimitive?.booleanOrNull ?: true)+ "webfetch" -> webFetchTool.fetch(+ url = args.requireString("url") ?: return missing("url"),+ format = args.requireString("format")+ )"todowrite" -> todoTool.todowrite(parseTodoItems(args["items"]) ?: return missing("items"))diff --git a/app/src/main/java/com/gotcha/tools/WebFetchTool.kt b/app/src/main/java/com/gotcha/tools/WebFetchTool.ktnew file mode 100644index 0000000..4e76782--- /dev/null+++ b/app/src/main/java/com/gotcha/tools/WebFetchTool.kt@@ -0,0 +1,75 @@+package com.gotcha.tools++import okhttp3.OkHttpClient+import okhttp3.Request+import java.util.concurrent.TimeUnit++/**+ * Web content fetching tool — fetches a URL and returns the content as text/markdown.+ * Uses OkHttp (already a project dependency).+ */+class WebFetchTool {++ companion object {+ private const val MAX_RESPONSE_BYTES = 5 * 1024 * 1024+ private const val TIMEOUT_SECONDS = 30L+ }++ private val client = OkHttpClient.Builder()+ .connectTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)+ .readTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)+ .followRedirects(true)+ .build()++ fun fetch(url: String, format: String?): ToolResult {+ val trimmed = url.trim()++ if (!trimmed.startsWith("http://") && !trimmed.startsWith("https://")) {+ return ToolResult.error("Only http:// and https:// URLs are supported.")+ }++ return try {+ val request = Request.Builder()+ .url(trimmed)+ .header("User-Agent", "Gotcha/0.1.0")+ .build()++ val response = client.newCall(request).execute()+ val body = response.body ?: return ToolResult.error("Empty response (HTTP ${response.code}).")++ if (!response.isSuccessful) {+ return ToolResult.error("HTTP ${response.code} ${response.message}")+ }++ val contentType = body.contentType()+ val mime = contentType?.type?.lowercase() + "/" + contentType?.subtype?.lowercase()++ // Reject binary content+ if (!mime.startsWith("text/") && mime != "application/json" && mime != "application/xml") {+ return ToolResult.error("Unsupported content type: $mime. Only text responses are supported.")+ }++ val source = body.source()+ val buffer = okio.Buffer()+ source.read(buffer, MAX_RESPONSE_BYTES.toLong())+ source.close()++ val raw = buffer.readString(Charsets.UTF_8)+ val truncated = raw.length > MAX_RESPONSE_BYTES++ val output = if (truncated) {+ raw.take(MAX_RESPONSE_BYTES) + "\n…(truncated at ${MAX_RESPONSE_BYTES / 1024} KB)"+ } else raw++ ToolResult.ok(+ "Fetched ${trimmed} (${raw.length / 1024} KB, HTTP ${response.code}):\n\n$output"+ )+ } catch (e: java.net.UnknownHostException) {+ ToolResult.error("Could not resolve host: ${e.message}")+ } catch (e: java.net.SocketTimeoutException) {+ ToolResult.error("Request timed out after ${TIMEOUT_SECONDS}s.")+ } catch (e: Exception) {+ ToolResult.error("Failed to fetch URL: ${e.message}")+ }+ }+}