refactor: addresses review comments, move CORS fix logic from card.tsx to qwen-adapter.js
This commit is contained in:
parent
66cbfa9629
commit
ab5dfbd1f6
|
|
@ -2,89 +2,6 @@ import * as React from "react";
|
||||||
|
|
||||||
import { cn } from "@/shared/utils/utils";
|
import { cn } from "@/shared/utils/utils";
|
||||||
|
|
||||||
declare global {
|
|
||||||
interface Window {
|
|
||||||
__dashscopeProxyPatched__?: boolean;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const DASH_SCOPE_ORIGIN = "https://dashscope.aliyuncs.com";
|
|
||||||
const DASH_SCOPE_PROXY_PREFIX = "/dashscope-proxy";
|
|
||||||
|
|
||||||
// Ensure dashscope API traffic is routed through the local proxy during development to avoid browser CORS blocks.
|
|
||||||
if (typeof window !== "undefined" && import.meta.env.DEV) {
|
|
||||||
const globalWindow = window as Window;
|
|
||||||
|
|
||||||
if (!globalWindow.__dashscopeProxyPatched__) {
|
|
||||||
const originalFetch = window.fetch.bind(window);
|
|
||||||
|
|
||||||
const rewriteUrl = (url: string): string => {
|
|
||||||
if (url.startsWith(DASH_SCOPE_PROXY_PREFIX)) return url;
|
|
||||||
|
|
||||||
if (typeof window !== "undefined") {
|
|
||||||
const originPrefixed = `${window.location.origin}${DASH_SCOPE_PROXY_PREFIX}`;
|
|
||||||
if (url.startsWith(originPrefixed)) {
|
|
||||||
return url.slice(window.location.origin.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (url.startsWith(DASH_SCOPE_ORIGIN)) {
|
|
||||||
return url.replace(DASH_SCOPE_ORIGIN, DASH_SCOPE_PROXY_PREFIX);
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
};
|
|
||||||
|
|
||||||
const rewriteRequestInfo = (input: RequestInfo | URL): RequestInfo => {
|
|
||||||
if (typeof input === "string") {
|
|
||||||
return rewriteUrl(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input instanceof URL) {
|
|
||||||
return rewriteUrl(input.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
const rewrittenUrl = rewriteUrl(input.url);
|
|
||||||
if (rewrittenUrl === input.url) {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cloned = input.clone();
|
|
||||||
const init: RequestInit = {
|
|
||||||
method: cloned.method,
|
|
||||||
headers: cloned.headers,
|
|
||||||
body:
|
|
||||||
cloned.method && ["GET", "HEAD"].includes(cloned.method.toUpperCase())
|
|
||||||
? undefined
|
|
||||||
: cloned.body,
|
|
||||||
cache: cloned.cache,
|
|
||||||
credentials: cloned.credentials,
|
|
||||||
integrity: cloned.integrity,
|
|
||||||
keepalive: cloned.keepalive,
|
|
||||||
mode: cloned.mode,
|
|
||||||
redirect: cloned.redirect,
|
|
||||||
referrer: cloned.referrer,
|
|
||||||
referrerPolicy: cloned.referrerPolicy,
|
|
||||||
signal: cloned.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Request(rewrittenUrl, init);
|
|
||||||
};
|
|
||||||
|
|
||||||
window.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {
|
|
||||||
const proxiedInput = rewriteRequestInfo(input);
|
|
||||||
|
|
||||||
if (proxiedInput instanceof Request) {
|
|
||||||
return originalFetch(proxiedInput);
|
|
||||||
}
|
|
||||||
|
|
||||||
return originalFetch(proxiedInput, init);
|
|
||||||
}) as typeof window.fetch;
|
|
||||||
|
|
||||||
globalWindow.__dashscopeProxyPatched__ = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Card({ className, ...props }: React.ComponentProps<"div">) {
|
function Card({ className, ...props }: React.ComponentProps<"div">) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,11 @@ export class QwenAdapter extends BaseLLMAdapter {
|
||||||
|
|
||||||
constructor(config: any) {
|
constructor(config: any) {
|
||||||
super(config);
|
super(config);
|
||||||
this.baseUrl = config.baseUrl || 'https://dashscope.aliyuncs.com/api/v1';
|
if (import.meta.env.DEV) {
|
||||||
|
this.baseUrl = '/dashscope-proxy/api/v1';
|
||||||
|
} else {
|
||||||
|
this.baseUrl = config.baseUrl || 'https://dashscope.aliyuncs.com/api/v1';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async complete(request: LLMRequest): Promise<LLMResponse> {
|
async complete(request: LLMRequest): Promise<LLMResponse> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue