fetchOpenGraphData protobus migration (#3549)
This commit is contained in:
1 parent
0dad8e178a
commit
2ba2b5b264
10 files changed
+251
-56
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"claude-dev": patch
|
||||
---
|
||||
|
||||
fetchOpenGraphData protobus migration
|
||||
@@ -8,9 +8,19 @@ import "common.proto";
|
||||
|
||||
service WebService {
|
||||
rpc checkIsImageUrl(StringRequest) returns (IsImageUrl);
|
||||
rpc fetchOpenGraphData(StringRequest) returns (OpenGraphData);
|
||||
}
|
||||
|
||||
message IsImageUrl {
|
||||
bool is_image = 1;
|
||||
string url = 2;
|
||||
}
|
||||
|
||||
message OpenGraphData {
|
||||
string title = 1;
|
||||
string description = 2;
|
||||
string image = 3;
|
||||
string url = 4;
|
||||
string site_name = 5;
|
||||
string type = 6;
|
||||
}
|
||||
@@ -338,9 +338,6 @@ export class Controller {
|
||||
vscode.env.openExternal(vscode.Uri.parse(message.url))
|
||||
}
|
||||
break
|
||||
case "fetchOpenGraphData":
|
||||
this.fetchOpenGraphData(message.text!)
|
||||
break
|
||||
case "openMention":
|
||||
openMention(message.text)
|
||||
break
|
||||
@@ -1475,30 +1472,6 @@ export class Controller {
|
||||
|
||||
// secrets
|
||||
|
||||
// Open Graph Data
|
||||
|
||||
async fetchOpenGraphData(url: string) {
|
||||
try {
|
||||
// Use the fetchOpenGraphData function from link-preview.ts
|
||||
const ogData = await fetchOpenGraphData(url)
|
||||
|
||||
// Send the data back to the webview
|
||||
await this.postMessageToWebview({
|
||||
type: "openGraphData",
|
||||
openGraphData: ogData,
|
||||
url: url,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Error fetching Open Graph data for ${url}:`, error)
|
||||
// Send an error response
|
||||
await this.postMessageToWebview({
|
||||
type: "openGraphData",
|
||||
error: `Failed to fetch Open Graph data: ${error}`,
|
||||
url: url,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Git commit message generation
|
||||
|
||||
async generateGitCommitMessage() {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Controller } from ".."
|
||||
import { StringRequest } from "../../../shared/proto/common"
|
||||
import { OpenGraphData } from "../../../shared/proto/web"
|
||||
import { fetchOpenGraphData as fetchOGData } from "../../../integrations/misc/link-preview"
|
||||
import { convertDomainOpenGraphDataToProto } from "../../../shared/proto-conversions/web/open-graph-conversion"
|
||||
|
||||
/**
|
||||
* Fetches Open Graph metadata from a URL
|
||||
* @param controller The controller instance
|
||||
* @param request The request containing the URL to fetch metadata from
|
||||
* @returns Promise resolving to OpenGraphData
|
||||
*/
|
||||
export async function fetchOpenGraphData(controller: Controller, request: StringRequest): Promise<OpenGraphData> {
|
||||
try {
|
||||
const url = request.value || ""
|
||||
// Fetch open graph data using the existing utility
|
||||
const ogData = await fetchOGData(url)
|
||||
|
||||
// Convert domain model to proto model
|
||||
return convertDomainOpenGraphDataToProto(ogData)
|
||||
} catch (error) {
|
||||
console.error(`Error fetching Open Graph data: ${request.value}`, error)
|
||||
// Return empty OpenGraphData object
|
||||
return OpenGraphData.create({})
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,11 @@
|
||||
// Import all method implementations
|
||||
import { registerMethod } from "./index"
|
||||
import { checkIsImageUrl } from "./checkIsImageUrl"
|
||||
import { fetchOpenGraphData } from "./fetchOpenGraphData"
|
||||
|
||||
// Register all web service methods
|
||||
export function registerAllMethods(): void {
|
||||
// Register each method with the registry
|
||||
registerMethod("checkIsImageUrl", checkIsImageUrl)
|
||||
registerMethod("fetchOpenGraphData", fetchOpenGraphData)
|
||||
}
|
||||
@@ -39,7 +39,6 @@ export interface WebviewMessage {
|
||||
| "fetchLatestMcpServersFromHub"
|
||||
| "telemetrySetting"
|
||||
| "openSettings"
|
||||
| "fetchOpenGraphData"
|
||||
| "invoke"
|
||||
| "updateSettings"
|
||||
| "clearAllTaskHistory"
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { OpenGraphData as DomainOpenGraphData } from "@integrations/misc/link-preview"
|
||||
import { OpenGraphData as ProtoOpenGraphData } from "@shared/proto/web"
|
||||
|
||||
/**
|
||||
* Converts domain OpenGraphData objects to proto OpenGraphData objects
|
||||
* @param ogData Domain OpenGraphData object
|
||||
* @returns Proto OpenGraphData object
|
||||
*/
|
||||
export function convertDomainOpenGraphDataToProto(ogData: DomainOpenGraphData): ProtoOpenGraphData {
|
||||
return ProtoOpenGraphData.create({
|
||||
title: ogData.title || "",
|
||||
description: ogData.description || "",
|
||||
image: ogData.image || "",
|
||||
url: ogData.url || "",
|
||||
siteName: ogData.siteName || "",
|
||||
type: ogData.type || "",
|
||||
})
|
||||
}
|
||||
@@ -15,6 +15,15 @@ export interface IsImageUrl {
|
||||
url: string
|
||||
}
|
||||
|
||||
export interface OpenGraphData {
|
||||
title: string
|
||||
description: string
|
||||
image: string
|
||||
url: string
|
||||
siteName: string
|
||||
type: string
|
||||
}
|
||||
|
||||
function createBaseIsImageUrl(): IsImageUrl {
|
||||
return { isImage: false, url: "" }
|
||||
}
|
||||
@@ -91,6 +100,146 @@ export const IsImageUrl: MessageFns<IsImageUrl> = {
|
||||
},
|
||||
}
|
||||
|
||||
function createBaseOpenGraphData(): OpenGraphData {
|
||||
return { title: "", description: "", image: "", url: "", siteName: "", type: "" }
|
||||
}
|
||||
|
||||
export const OpenGraphData: MessageFns<OpenGraphData> = {
|
||||
encode(message: OpenGraphData, writer: BinaryWriter = new BinaryWriter()): BinaryWriter {
|
||||
if (message.title !== "") {
|
||||
writer.uint32(10).string(message.title)
|
||||
}
|
||||
if (message.description !== "") {
|
||||
writer.uint32(18).string(message.description)
|
||||
}
|
||||
if (message.image !== "") {
|
||||
writer.uint32(26).string(message.image)
|
||||
}
|
||||
if (message.url !== "") {
|
||||
writer.uint32(34).string(message.url)
|
||||
}
|
||||
if (message.siteName !== "") {
|
||||
writer.uint32(42).string(message.siteName)
|
||||
}
|
||||
if (message.type !== "") {
|
||||
writer.uint32(50).string(message.type)
|
||||
}
|
||||
return writer
|
||||
},
|
||||
|
||||
decode(input: BinaryReader | Uint8Array, length?: number): OpenGraphData {
|
||||
const reader = input instanceof BinaryReader ? input : new BinaryReader(input)
|
||||
let end = length === undefined ? reader.len : reader.pos + length
|
||||
const message = createBaseOpenGraphData()
|
||||
while (reader.pos < end) {
|
||||
const tag = reader.uint32()
|
||||
switch (tag >>> 3) {
|
||||
case 1: {
|
||||
if (tag !== 10) {
|
||||
break
|
||||
}
|
||||
|
||||
message.title = reader.string()
|
||||
continue
|
||||
}
|
||||
case 2: {
|
||||
if (tag !== 18) {
|
||||
break
|
||||
}
|
||||
|
||||
message.description = reader.string()
|
||||
continue
|
||||
}
|
||||
case 3: {
|
||||
if (tag !== 26) {
|
||||
break
|
||||
}
|
||||
|
||||
message.image = reader.string()
|
||||
continue
|
||||
}
|
||||
case 4: {
|
||||
if (tag !== 34) {
|
||||
break
|
||||
}
|
||||
|
||||
message.url = reader.string()
|
||||
continue
|
||||
}
|
||||
case 5: {
|
||||
if (tag !== 42) {
|
||||
break
|
||||
}
|
||||
|
||||
message.siteName = reader.string()
|
||||
continue
|
||||
}
|
||||
case 6: {
|
||||
if (tag !== 50) {
|
||||
break
|
||||
}
|
||||
|
||||
message.type = reader.string()
|
||||
continue
|
||||
}
|
||||
}
|
||||
if ((tag & 7) === 4 || tag === 0) {
|
||||
break
|
||||
}
|
||||
reader.skip(tag & 7)
|
||||
}
|
||||
return message
|
||||
},
|
||||
|
||||
fromJSON(object: any): OpenGraphData {
|
||||
return {
|
||||
title: isSet(object.title) ? globalThis.String(object.title) : "",
|
||||
description: isSet(object.description) ? globalThis.String(object.description) : "",
|
||||
image: isSet(object.image) ? globalThis.String(object.image) : "",
|
||||
url: isSet(object.url) ? globalThis.String(object.url) : "",
|
||||
siteName: isSet(object.siteName) ? globalThis.String(object.siteName) : "",
|
||||
type: isSet(object.type) ? globalThis.String(object.type) : "",
|
||||
}
|
||||
},
|
||||
|
||||
toJSON(message: OpenGraphData): unknown {
|
||||
const obj: any = {}
|
||||
if (message.title !== "") {
|
||||
obj.title = message.title
|
||||
}
|
||||
if (message.description !== "") {
|
||||
obj.description = message.description
|
||||
}
|
||||
if (message.image !== "") {
|
||||
obj.image = message.image
|
||||
}
|
||||
if (message.url !== "") {
|
||||
obj.url = message.url
|
||||
}
|
||||
if (message.siteName !== "") {
|
||||
obj.siteName = message.siteName
|
||||
}
|
||||
if (message.type !== "") {
|
||||
obj.type = message.type
|
||||
}
|
||||
return obj
|
||||
},
|
||||
|
||||
create<I extends Exact<DeepPartial<OpenGraphData>, I>>(base?: I): OpenGraphData {
|
||||
return OpenGraphData.fromPartial(base ?? ({} as any))
|
||||
},
|
||||
fromPartial<I extends Exact<DeepPartial<OpenGraphData>, I>>(object: I): OpenGraphData {
|
||||
const message = createBaseOpenGraphData()
|
||||
message.title = object.title ?? ""
|
||||
message.description = object.description ?? ""
|
||||
message.image = object.image ?? ""
|
||||
message.url = object.url ?? ""
|
||||
message.siteName = object.siteName ?? ""
|
||||
message.type = object.type ?? ""
|
||||
return message
|
||||
},
|
||||
}
|
||||
|
||||
export type WebServiceDefinition = typeof WebServiceDefinition
|
||||
export const WebServiceDefinition = {
|
||||
name: "WebService",
|
||||
@@ -104,6 +253,14 @@ export const WebServiceDefinition = {
|
||||
responseStream: false,
|
||||
options: {},
|
||||
},
|
||||
fetchOpenGraphData: {
|
||||
name: "fetchOpenGraphData",
|
||||
requestType: StringRequest,
|
||||
requestStream: false,
|
||||
responseType: OpenGraphData,
|
||||
responseStream: false,
|
||||
options: {},
|
||||
},
|
||||
},
|
||||
} as const
|
||||
|
||||
|
||||
@@ -67,6 +67,7 @@ import { taskFeedback } from "../core/controller/task/taskFeedback"
|
||||
|
||||
// Web Service
|
||||
import { checkIsImageUrl } from "../core/controller/web/checkIsImageUrl"
|
||||
import { fetchOpenGraphData } from "../core/controller/web/fetchOpenGraphData"
|
||||
|
||||
export function addServices(
|
||||
server: grpc.Server,
|
||||
@@ -157,5 +158,6 @@ export function addServices(
|
||||
// Web Service
|
||||
server.addService(proto.cline.WebService.service, {
|
||||
checkIsImageUrl: wrapper(checkIsImageUrl, controller),
|
||||
fetchOpenGraphData: wrapper(fetchOpenGraphData, controller),
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { vscode } from "@/utils/vscode"
|
||||
import DOMPurify from "dompurify"
|
||||
import { getSafeHostname, normalizeRelativeUrl } from "./utils/mcpRichUtil"
|
||||
import ChatErrorBoundary from "@/components/chat/ChatErrorBoundary"
|
||||
import { WebServiceClient } from "@/services/grpc-client"
|
||||
|
||||
interface OpenGraphData {
|
||||
title?: string
|
||||
@@ -102,45 +103,47 @@ class LinkPreview extends React.Component<LinkPreviewProps, LinkPreviewState> {
|
||||
}
|
||||
}
|
||||
|
||||
private fetchOpenGraphData() {
|
||||
private async fetchOpenGraphData() {
|
||||
try {
|
||||
// Record fetch start time
|
||||
const startTime = Date.now()
|
||||
this.setState({ fetchStartTime: startTime })
|
||||
|
||||
// Send a message to the extension to fetch Open Graph data
|
||||
vscode.postMessage({
|
||||
type: "fetchOpenGraphData",
|
||||
text: this.props.url,
|
||||
// Use the gRPC client to fetch Open Graph data
|
||||
const response = await WebServiceClient.fetchOpenGraphData({
|
||||
value: this.props.url,
|
||||
})
|
||||
|
||||
// Set up a listener for the response
|
||||
this.messageListener = (event: MessageEvent) => {
|
||||
const message = event.data
|
||||
if (message.type === "openGraphData" && message.url === this.props.url) {
|
||||
// Check if there was an error in the response
|
||||
if (message.error) {
|
||||
this.setState({
|
||||
error: "network",
|
||||
errorMessage: message.error,
|
||||
loading: false,
|
||||
hasCompletedFetch: true,
|
||||
})
|
||||
} else {
|
||||
this.setState({
|
||||
ogData: message.openGraphData,
|
||||
loading: false,
|
||||
hasCompletedFetch: true, // Mark as completed
|
||||
})
|
||||
}
|
||||
this.cleanup()
|
||||
// Process the response
|
||||
if (response) {
|
||||
const ogData: OpenGraphData = {
|
||||
title: response.title || undefined,
|
||||
description: response.description || undefined,
|
||||
image: response.image || undefined,
|
||||
url: response.url || undefined,
|
||||
siteName: response.siteName || undefined,
|
||||
type: response.type || undefined,
|
||||
}
|
||||
|
||||
this.setState({
|
||||
ogData,
|
||||
loading: false,
|
||||
hasCompletedFetch: true,
|
||||
})
|
||||
} else {
|
||||
this.setState({
|
||||
error: "network",
|
||||
errorMessage: "Failed to fetch Open Graph data",
|
||||
loading: false,
|
||||
hasCompletedFetch: true,
|
||||
})
|
||||
}
|
||||
|
||||
window.addEventListener("message", this.messageListener)
|
||||
// Clean up the heartbeat interval
|
||||
// (No message listener is needed with gRPC, unlike the previous message-based approach)
|
||||
this.cleanup()
|
||||
|
||||
// Instead of a fixed timeout, use a heartbeat to update the loading message
|
||||
// with the elapsed time, but don't actually timeout
|
||||
// Set up heartbeat for loading indicator
|
||||
this.heartbeatId = setInterval(() => {
|
||||
const elapsedSeconds = Math.floor((Date.now() - startTime) / 1000)
|
||||
if (elapsedSeconds > 0) {
|
||||
|
||||
Reference in new issue
Block a user