From 045bb0e6305a98143f072803071030a8cb109ea3 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 27 Feb 2025 15:03:27 -0800 Subject: [PATCH] can parse url --- core/tools/callTool.ts | 6 +++--- core/util/url.ts | 11 +++++++++++ extensions/vscode/src/stubs/remoteConfig.ts | 15 ++------------- 3 files changed, 16 insertions(+), 16 deletions(-) create mode 100644 core/util/url.ts diff --git a/core/tools/callTool.ts b/core/tools/callTool.ts index f3b565285..959752af1 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -1,5 +1,6 @@ import { ContextItem, Tool, ToolExtras } from ".."; import { MCPManagerSingleton } from "../context/mcp"; +import { canParseUrl } from "../util/url"; import { BuiltInToolNames } from "./builtIn"; import { createNewFileImpl } from "./implementations/createNewFile"; @@ -55,9 +56,8 @@ async function callToolFromUri( args: any, extras: ToolExtras, ): Promise { - // @ts-ignore - const canParse = URL.canParse(uri); - if (!canParse) { + const parseable = canParseUrl(uri); + if (!parseable) { throw new Error(`Invalid URI: ${uri}`); } const parsedUri = new URL(uri); diff --git a/core/util/url.ts b/core/util/url.ts new file mode 100644 index 000000000..83e0edcba --- /dev/null +++ b/core/util/url.ts @@ -0,0 +1,11 @@ +export function canParseUrl(url: string): boolean { + if ((URL as any)?.canParse) { + return (URL as any).canParse(url); + } + try { + new URL(url); + return true; + } catch (e) { + return false; + } +} diff --git a/extensions/vscode/src/stubs/remoteConfig.ts b/extensions/vscode/src/stubs/remoteConfig.ts index ffe1fe89b..e0ff9bb76 100644 --- a/extensions/vscode/src/stubs/remoteConfig.ts +++ b/extensions/vscode/src/stubs/remoteConfig.ts @@ -5,6 +5,7 @@ import { EXTENSION_NAME } from "core/control-plane/env"; import { getConfigJsonPathForRemote } from "core/util/paths"; import * as vscode from "vscode"; +import { canParseUrl } from "core/util/url"; import { CONTINUE_WORKSPACE_KEY } from "../util/workspaceConfig"; export class RemoteConfigSync { @@ -66,18 +67,6 @@ export class RemoteConfigSync { }; } - private canParse(url: string): boolean { - if ((URL as any).canParse) { - return (URL as any).canParse(url); - } - try { - new URL(url); - return true; - } catch (e) { - return false; - } - } - async setup() { if ( this.userToken === null || @@ -86,7 +75,7 @@ export class RemoteConfigSync { ) { return; } - if (!this.canParse(this.remoteConfigServerUrl)) { + if (!canParseUrl(this.remoteConfigServerUrl)) { vscode.window.showErrorMessage( "The value set for 'remoteConfigServerUrl' is not valid: ", this.remoteConfigServerUrl,