From c000e1cc1cf6ffd3bf21f16cc3b98f826f8bfe8a Mon Sep 17 00:00:00 2001 From: HybridTalentComputing <1316645276@qq.com> Date: Fri, 30 Jan 2026 22:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3trae,cursor=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/api/providers/shengsuanyun.ts | 30 +++++++++++++++---- src/core/controller/index.ts | 3 +- .../hostbridge/env/getIdeRedirectUri.ts | 5 +++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/core/api/providers/shengsuanyun.ts b/src/core/api/providers/shengsuanyun.ts index 2019bbe8..51e4288c 100644 --- a/src/core/api/providers/shengsuanyun.ts +++ b/src/core/api/providers/shengsuanyun.ts @@ -2,10 +2,10 @@ import { Anthropic } from "@anthropic-ai/sdk" import { ShengSuanYunModelInfo } from "@shared/proto/cline/models" import OpenAI from "openai" import type { ChatCompletionTool } from "openai/resources/chat/completions" +import { HostProvider } from "@/hosts/host-provider" import { Logger } from "@/services/logging/Logger" import { ClineStorageMessage } from "@/shared/messages" import { calculateApiCostOpenAI } from "@/utils/cost" -// import * as vscode from "vscode" import { shouldSkipReasoningForModel } from "@/utils/model-utils" import { shengSuanYunDefaultModelId, shengSuanYunDefaultModelInfo } from "../../../shared/api" import { ApiHandler, CommonApiHandlerOptions } from "../" @@ -26,13 +26,31 @@ interface ShengSuanYunHandlerOptions extends CommonApiHandlerOptions { export class ShengSuanYunHandler implements ApiHandler { private options: ShengSuanYunHandlerOptions private client: OpenAI | undefined + private httpReferer: string | undefined lastGenerationId?: string constructor(options: ShengSuanYunHandlerOptions) { this.options = options } - private ensureClient(): OpenAI { + private async getHttpReferer(): Promise { + if (this.httpReferer) { + return this.httpReferer + } + + try { + const callbackUrl = await HostProvider.get().getCallbackUrl() + this.httpReferer = `${callbackUrl}/ssy` + } catch (error) { + // Fallback to default if unable to get callback URL (e.g., in CLI mode) + console.warn("Failed to get callback URL for HTTP-Referer, using default:", error) + this.httpReferer = "cline-chinese" + } + + return this.httpReferer + } + + private async ensureClient(): Promise { if (!this.client) { if (!this.options.shengSuanYunApiKey) { throw new Error("shengsuanyun API key is required") @@ -42,7 +60,7 @@ export class ShengSuanYunHandler implements ApiHandler { baseURL: "https://router.shengsuanyun.com/api/v1", apiKey: this.options.shengSuanYunApiKey, defaultHeaders: { - "HTTP-Referer": `vscode://HybridTalentComputing.cline-chinese/ssy`, + "HTTP-Referer": await this.getHttpReferer(), "X-Title": "cline", }, }) @@ -56,6 +74,8 @@ export class ShengSuanYunHandler implements ApiHandler { @withRetry() async *createMessage(systemPrompt: string, messages: ClineStorageMessage[], tools?: ChatCompletionTool[]): ApiStream { const model = this.getModel() + // Ensure client is initialized before using it + await this.ensureClient() if (model?.info?.endPoints?.includes("/v1/chat/completions")) { yield* this.createCompletionStream(systemPrompt, messages) } else if (model?.info?.endPoints?.includes("/v1/responses")) { @@ -86,7 +106,7 @@ export class ShengSuanYunHandler implements ApiHandler { } async *createCompletionStream(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { - const client = this.ensureClient() + const client = await this.ensureClient() const model = this.getModel() try { const stream = await createOpenRouterStream( @@ -192,7 +212,7 @@ export class ShengSuanYunHandler implements ApiHandler { messages: ClineStorageMessage[], tools: ChatCompletionTool[], ): ApiStream { - const client = this.ensureClient() + const client = await this.ensureClient() const model = this.getModel() try { diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 3b0226c6..353936ef 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -727,9 +727,10 @@ export class Controller { try { let shengSuanYunApiKey: string let shengSuanYunToken: string + const callbackUrl = `${await HostProvider.get().getCallbackUrl()}/ssy` const res = await axios.post("https://api.shengsuanyun.com/auth/keys", { code: code, - callback_url: `vscode://HybridTalentComputing.cline-chinese/ssy`, + callback_url: callbackUrl, }) // console.log("https://api.shengsuanyun.com/auth/keys :", res.data) if (res.data && res.data.data && res.data.data.api_key) { diff --git a/src/hosts/vscode/hostbridge/env/getIdeRedirectUri.ts b/src/hosts/vscode/hostbridge/env/getIdeRedirectUri.ts index a18df532..393750b8 100644 --- a/src/hosts/vscode/hostbridge/env/getIdeRedirectUri.ts +++ b/src/hosts/vscode/hostbridge/env/getIdeRedirectUri.ts @@ -3,6 +3,9 @@ import * as vscode from "vscode" export async function getIdeRedirectUri(_: EmptyRequest): Promise { const uriScheme = vscode.env.uriScheme || "vscode" - const url = `${uriScheme}://saoudrizwan.cline-chinese` + // Use the actual extension ID for cline-chinese + // This works across all VS Code-based editors (Cursor, Windsurf, Trae, etc.) + const extensionId = "HybridTalentComputing.cline-chinese" + const url = `${uriScheme}://${extensionId}` return { value: url } }