[PROTOBUS] file checkIsImageUrl (#3109)
* checkIsImageURL protobus migration * changeset * rebase and move to web-content * cleanup * metadata * rename return message * removed old import, metadata
This commit is contained in:
1 parent
4650ffa86b
commit
4565e067af
14 files changed
+244
-61
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"claude-dev": patch
|
||||
---
|
||||
|
||||
checkIsImageURL migrated to protobus
|
||||
@@ -85,6 +85,7 @@ async function generateMethodRegistrations() {
|
||||
path.join(ROOT_DIR, "src", "core", "controller", "file"),
|
||||
path.join(ROOT_DIR, "src", "core", "controller", "mcp"),
|
||||
path.join(ROOT_DIR, "src", "core", "controller", "task"),
|
||||
path.join(ROOT_DIR, "src", "core", "controller", "web-content"),
|
||||
// Add more service directories here as needed
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package cline;
|
||||
option java_package = "bot.cline.proto";
|
||||
option java_multiple_files = true;
|
||||
|
||||
import "common.proto";
|
||||
|
||||
service WebContentService {
|
||||
rpc checkIsImageUrl(StringRequest) returns (IsImageUrl);
|
||||
}
|
||||
|
||||
message IsImageUrl {
|
||||
bool is_image = 1;
|
||||
string url = 2;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { handleFileServiceRequest } from "./file"
|
||||
import { handleTaskServiceRequest } from "./task"
|
||||
import { handleCheckpointsServiceRequest } from "./checkpoints"
|
||||
import { handleMcpServiceRequest } from "./mcp"
|
||||
import { handleWebContentServiceRequest } from "./web-content"
|
||||
|
||||
/**
|
||||
* Handles gRPC requests from the webview
|
||||
@@ -62,6 +63,11 @@ export class GrpcHandler {
|
||||
message: await handleMcpServiceRequest(this.controller, method, message),
|
||||
request_id: requestId,
|
||||
}
|
||||
case "cline.WebContentService":
|
||||
return {
|
||||
message: await handleWebContentServiceRequest(this.controller, method, message),
|
||||
request_id: requestId,
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown service: ${service}`)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import { handleGrpcRequest } from "./grpc-handler"
|
||||
import { buildApiHandler } from "@api/index"
|
||||
import { cleanupLegacyCheckpoints } from "@integrations/checkpoints/CheckpointMigration"
|
||||
import { downloadTask } from "@integrations/misc/export-markdown"
|
||||
import { fetchOpenGraphData, isImageUrl } from "@integrations/misc/link-preview"
|
||||
import { fetchOpenGraphData } from "@integrations/misc/link-preview"
|
||||
import { handleFileServiceRequest } from "./file"
|
||||
import { selectImages } from "@integrations/misc/process-images"
|
||||
import { getTheme } from "@integrations/theme/getTheme"
|
||||
@@ -385,9 +385,6 @@ export class Controller {
|
||||
case "fetchOpenGraphData":
|
||||
this.fetchOpenGraphData(message.text!)
|
||||
break
|
||||
case "checkIsImageUrl":
|
||||
this.checkIsImageUrl(message.text!)
|
||||
break
|
||||
case "createRuleFile":
|
||||
if (typeof message.isGlobal !== "boolean" || typeof message.filename !== "string" || !message.filename) {
|
||||
console.error("createRuleFile: Missing or invalid parameters", {
|
||||
@@ -1918,29 +1915,6 @@ Here is the project's README to help you get started:\n\n${mcpDetails.readmeCont
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a URL is an image
|
||||
async checkIsImageUrl(url: string) {
|
||||
try {
|
||||
// Check if the URL is an image
|
||||
const isImage = await isImageUrl(url)
|
||||
|
||||
// Send the result back to the webview
|
||||
await this.postMessageToWebview({
|
||||
type: "isImageUrlResult",
|
||||
isImage,
|
||||
url,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Error checking if URL is an image: ${url}`, error)
|
||||
// Send an error response
|
||||
await this.postMessageToWebview({
|
||||
type: "isImageUrlResult",
|
||||
isImage: false,
|
||||
url,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// dev
|
||||
|
||||
async resetState() {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Controller } from "../index"
|
||||
import { StringRequest } from "../../../shared/proto/common"
|
||||
import { IsImageUrl } from "../../../shared/proto/web_content"
|
||||
import { detectImageUrl } from "@integrations/misc/link-preview"
|
||||
|
||||
/**
|
||||
* Checks if a URL is an image URL
|
||||
* @param controller The controller instance
|
||||
* @param request The request containing the URL to check
|
||||
* @returns A result indicating if the URL is an image and the URL that was checked
|
||||
*/
|
||||
export async function checkIsImageUrl(controller: Controller, request: StringRequest): Promise<IsImageUrl> {
|
||||
try {
|
||||
const url = request.value || ""
|
||||
// Check if the URL is an image
|
||||
const isImage = await detectImageUrl(url)
|
||||
|
||||
return {
|
||||
isImage,
|
||||
url,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error checking if URL is an image: ${request.value}`, error)
|
||||
return {
|
||||
isImage: false,
|
||||
url: request.value || "",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { createServiceRegistry, ServiceMethodHandler } from "../grpc-service"
|
||||
import { registerAllMethods } from "./methods"
|
||||
|
||||
// Create web content service registry
|
||||
const webContentService = createServiceRegistry("web-content")
|
||||
|
||||
// Export the method handler type and registration function
|
||||
export type WebContentMethodHandler = ServiceMethodHandler
|
||||
export const registerMethod = webContentService.registerMethod
|
||||
|
||||
// Export the request handler
|
||||
export const handleWebContentServiceRequest = webContentService.handleRequest
|
||||
|
||||
// Register all web content methods
|
||||
registerAllMethods()
|
||||
@@ -0,0 +1,12 @@
|
||||
// AUTO-GENERATED FILE - DO NOT MODIFY DIRECTLY
|
||||
// Generated by proto/build-proto.js
|
||||
|
||||
// Import all method implementations
|
||||
import { registerMethod } from "./index"
|
||||
import { checkIsImageUrl } from "./checkIsImageUrl"
|
||||
|
||||
// Register all web-content service methods
|
||||
export function registerAllMethods(): void {
|
||||
// Register each method with the registry
|
||||
registerMethod("checkIsImageUrl", checkIsImageUrl)
|
||||
}
|
||||
@@ -87,7 +87,7 @@ export async function fetchOpenGraphData(url: string): Promise<OpenGraphData> {
|
||||
* @param url The URL to check
|
||||
* @returns Promise resolving to boolean indicating if the URL is an image
|
||||
*/
|
||||
export async function isImageUrl(url: string): Promise<boolean> {
|
||||
export async function detectImageUrl(url: string): Promise<boolean> {
|
||||
try {
|
||||
const response = await axios.head(url, {
|
||||
headers: {
|
||||
|
||||
@@ -35,7 +35,6 @@ export interface ExtensionMessage {
|
||||
| "mcpDownloadDetails"
|
||||
| "commitSearchResults"
|
||||
| "openGraphData"
|
||||
| "isImageUrlResult"
|
||||
| "didUpdateSettings"
|
||||
| "userCreditsBalance"
|
||||
| "userCreditsUsage"
|
||||
|
||||
@@ -54,7 +54,6 @@ export interface WebviewMessage {
|
||||
| "telemetrySetting"
|
||||
| "openSettings"
|
||||
| "fetchOpenGraphData"
|
||||
| "checkIsImageUrl"
|
||||
| "invoke"
|
||||
| "updateSettings"
|
||||
| "clearAllTaskHistory"
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-ts_proto v2.7.0
|
||||
// protoc v3.19.1
|
||||
// source: web_content.proto
|
||||
|
||||
/* eslint-disable */
|
||||
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire"
|
||||
import { StringRequest } from "./common"
|
||||
|
||||
export const protobufPackage = "cline"
|
||||
|
||||
export interface IsImageUrl {
|
||||
isImage: boolean
|
||||
url: string
|
||||
}
|
||||
|
||||
function createBaseIsImageUrl(): IsImageUrl {
|
||||
return { isImage: false, url: "" }
|
||||
}
|
||||
|
||||
export const IsImageUrl: MessageFns<IsImageUrl> = {
|
||||
encode(message: IsImageUrl, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
||||
if (message.isImage !== false) {
|
||||
writer.uint32(8).bool(message.isImage)
|
||||
}
|
||||
if (message.url !== "") {
|
||||
writer.uint32(18).string(message.url)
|
||||
}
|
||||
return writer
|
||||
},
|
||||
|
||||
decode(input: BinaryReader | Uint8Array, length?: number): IsImageUrl {
|
||||
const reader = input instanceof BinaryReader ? input : new BinaryReader(input)
|
||||
let end = length === undefined ? reader.len : reader.pos + length
|
||||
const message = createBaseIsImageUrl()
|
||||
while (reader.pos < end) {
|
||||
const tag = reader.uint32()
|
||||
switch (tag >>> 3) {
|
||||
case 1: {
|
||||
if (tag !== 8) {
|
||||
break
|
||||
}
|
||||
|
||||
message.isImage = reader.bool()
|
||||
continue
|
||||
}
|
||||
case 2: {
|
||||
if (tag !== 18) {
|
||||
break
|
||||
}
|
||||
|
||||
message.url = reader.string()
|
||||
continue
|
||||
}
|
||||
}
|
||||
if ((tag & 7) === 4 || tag === 0) {
|
||||
break
|
||||
}
|
||||
reader.skip(tag & 7)
|
||||
}
|
||||
return message
|
||||
},
|
||||
|
||||
fromJSON(object: any): IsImageUrl {
|
||||
return {
|
||||
isImage: isSet(object.isImage) ? globalThis.Boolean(object.isImage) : false,
|
||||
url: isSet(object.url) ? globalThis.String(object.url) : "",
|
||||
}
|
||||
},
|
||||
|
||||
toJSON(message: IsImageUrl): unknown {
|
||||
const obj: any = {}
|
||||
if (message.isImage !== false) {
|
||||
obj.isImage = message.isImage
|
||||
}
|
||||
if (message.url !== "") {
|
||||
obj.url = message.url
|
||||
}
|
||||
return obj
|
||||
},
|
||||
|
||||
create<I extends Exact<DeepPartial<IsImageUrl>, I>>(base?: I): IsImageUrl {
|
||||
return IsImageUrl.fromPartial(base ?? ({} as any))
|
||||
},
|
||||
fromPartial<I extends Exact<DeepPartial<IsImageUrl>, I>>(object: I): IsImageUrl {
|
||||
const message = createBaseIsImageUrl()
|
||||
message.isImage = object.isImage ?? false
|
||||
message.url = object.url ?? ""
|
||||
return message
|
||||
},
|
||||
}
|
||||
|
||||
export type WebContentServiceDefinition = typeof WebContentServiceDefinition
|
||||
export const WebContentServiceDefinition = {
|
||||
name: "WebContentService",
|
||||
fullName: "cline.WebContentService",
|
||||
methods: {
|
||||
checkIsImageUrl: {
|
||||
name: "checkIsImageUrl",
|
||||
requestType: StringRequest,
|
||||
requestStream: false,
|
||||
responseType: IsImageUrl,
|
||||
responseStream: false,
|
||||
options: {},
|
||||
},
|
||||
},
|
||||
} as const
|
||||
|
||||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined
|
||||
|
||||
export type DeepPartial<T> = T extends Builtin
|
||||
? T
|
||||
: T extends globalThis.Array<infer U>
|
||||
? globalThis.Array<DeepPartial<U>>
|
||||
: T extends ReadonlyArray<infer U>
|
||||
? ReadonlyArray<DeepPartial<U>>
|
||||
: T extends {}
|
||||
? { [K in keyof T]?: DeepPartial<T[K]> }
|
||||
: Partial<T>
|
||||
|
||||
type KeysOfUnion<T> = T extends T ? keyof T : never
|
||||
export type Exact<P, I extends P> = P extends Builtin
|
||||
? P
|
||||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never }
|
||||
|
||||
function isSet(value: any): boolean {
|
||||
return value !== null && value !== undefined
|
||||
}
|
||||
|
||||
export interface MessageFns<T> {
|
||||
encode(message: T, writer?: BinaryWriter): BinaryWriter
|
||||
decode(input: BinaryReader | Uint8Array, length?: number): T
|
||||
fromJSON(object: any): T
|
||||
toJSON(message: T): unknown
|
||||
create<I extends Exact<DeepPartial<T>, I>>(base?: I): T
|
||||
fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { vscode } from "@/utils/vscode"
|
||||
import { WebContentServiceClient } from "@/services/grpc-client"
|
||||
|
||||
// Safely create a URL object with error handling and ensure HTTPS
|
||||
export const safeCreateUrl = (url: string): URL | null => {
|
||||
@@ -133,44 +133,30 @@ export const checkIfImageUrl = async (url: string): Promise<boolean> => {
|
||||
return false
|
||||
}
|
||||
|
||||
// For https URLs, we need to send a message to the extension
|
||||
// For https URLs, we need to use the gRPC FileService
|
||||
if (url.startsWith("https")) {
|
||||
try {
|
||||
// Create a promise that will resolve when we get a response
|
||||
return new Promise((resolve) => {
|
||||
let timeoutId: ReturnType<typeof setTimeout> | undefined = undefined
|
||||
|
||||
// Set up a one-time listener for the response
|
||||
const messageListener = (event: MessageEvent) => {
|
||||
const message = event.data
|
||||
if (message.type === "isImageUrlResult" && message.url === url) {
|
||||
window.removeEventListener("message", messageListener)
|
||||
resolve(message.isImage)
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("message", messageListener)
|
||||
|
||||
// Send the request to the extension
|
||||
vscode.postMessage({
|
||||
type: "checkIsImageUrl",
|
||||
text: url,
|
||||
})
|
||||
|
||||
// Set a timeout to avoid hanging indefinitely
|
||||
timeoutId = setTimeout(() => {
|
||||
window.removeEventListener("message", messageListener)
|
||||
// Use the gRPC client with timeout
|
||||
const timeoutPromise = new Promise<boolean>((resolve) => {
|
||||
setTimeout(() => {
|
||||
console.log("Hit timeout waiting for checkIsImageUrl")
|
||||
resolve(false)
|
||||
}, 3000)
|
||||
})
|
||||
|
||||
// Create the actual service call
|
||||
const servicePromise = WebContentServiceClient.checkIsImageUrl({ value: url })
|
||||
.then((result) => result.isImage)
|
||||
.catch((error) => {
|
||||
console.error("Error checking if URL is an image via gRPC:", error)
|
||||
return false
|
||||
})
|
||||
|
||||
// Race between the service call and the timeout
|
||||
return Promise.race([servicePromise, timeoutPromise])
|
||||
} catch (error) {
|
||||
console.log("Error checking if URL is an image:", url)
|
||||
// Don't fall back to extension check on error
|
||||
// Instead, return false to indicate it's not an image
|
||||
// Return false to indicate it's not an image
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { EmptyRequest } from "@shared/proto/common"
|
||||
import { FileServiceDefinition } from "@shared/proto/file"
|
||||
import { McpServiceDefinition } from "@shared/proto/mcp"
|
||||
import { TaskServiceDefinition } from "@shared/proto/task"
|
||||
import { WebContentServiceDefinition } from "@shared/proto/web_content"
|
||||
// Generic type for any protobuf service definition
|
||||
type ProtoService = {
|
||||
name: string
|
||||
@@ -102,6 +103,7 @@ const CheckpointsServiceClient = createGrpcClient(CheckpointsServiceDefinition)
|
||||
const FileServiceClient = createGrpcClient(FileServiceDefinition)
|
||||
const McpServiceClient = createGrpcClient(McpServiceDefinition)
|
||||
const TaskServiceClient = createGrpcClient(TaskServiceDefinition)
|
||||
const WebContentServiceClient = createGrpcClient(WebContentServiceDefinition)
|
||||
|
||||
export {
|
||||
AccountServiceClient,
|
||||
@@ -110,4 +112,5 @@ export {
|
||||
FileServiceClient,
|
||||
TaskServiceClient,
|
||||
McpServiceClient,
|
||||
WebContentServiceClient,
|
||||
}
|
||||
Reference in new issue
Block a user