mirror of
				https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
				synced 2025-11-04 08:13:43 +08:00 
			
		
		
		
	hotfix: can send sd task in client
This commit is contained in:
		@@ -168,6 +168,19 @@ export class ClientApi {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function getBearerToken(
 | 
			
		||||
  apiKey: string,
 | 
			
		||||
  noBearer: boolean = false,
 | 
			
		||||
): string {
 | 
			
		||||
  return validString(apiKey)
 | 
			
		||||
    ? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
 | 
			
		||||
    : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function validString(x: string): boolean {
 | 
			
		||||
  return x?.length > 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function getHeaders() {
 | 
			
		||||
  const accessStore = useAccessStore.getState();
 | 
			
		||||
  const chatStore = useChatStore.getState();
 | 
			
		||||
@@ -214,15 +227,6 @@ export function getHeaders() {
 | 
			
		||||
    return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function getBearerToken(apiKey: string, noBearer: boolean = false): string {
 | 
			
		||||
    return validString(apiKey)
 | 
			
		||||
      ? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
 | 
			
		||||
      : "";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function validString(x: string): boolean {
 | 
			
		||||
    return x?.length > 0;
 | 
			
		||||
  }
 | 
			
		||||
  const {
 | 
			
		||||
    isGoogle,
 | 
			
		||||
    isAzure,
 | 
			
		||||
 
 | 
			
		||||
@@ -46,6 +46,7 @@ export enum ApiPath {
 | 
			
		||||
  Baidu = "/api/baidu",
 | 
			
		||||
  ByteDance = "/api/bytedance",
 | 
			
		||||
  Alibaba = "/api/alibaba",
 | 
			
		||||
  Stability = "/api/stability",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export enum SlotID {
 | 
			
		||||
 
 | 
			
		||||
@@ -39,6 +39,10 @@ const DEFAULT_ALIBABA_URL = isApp
 | 
			
		||||
  ? DEFAULT_API_HOST + "/api/proxy/alibaba"
 | 
			
		||||
  : ApiPath.Alibaba;
 | 
			
		||||
 | 
			
		||||
const DEFAULT_STABILITY_URL = isApp
 | 
			
		||||
  ? DEFAULT_API_HOST + "/api/proxy/stability"
 | 
			
		||||
  : ApiPath.Stability;
 | 
			
		||||
 | 
			
		||||
const DEFAULT_ACCESS_STATE = {
 | 
			
		||||
  accessCode: "",
 | 
			
		||||
  useCustomConfig: false,
 | 
			
		||||
@@ -79,7 +83,7 @@ const DEFAULT_ACCESS_STATE = {
 | 
			
		||||
  alibabaApiKey: "",
 | 
			
		||||
 | 
			
		||||
  //stability
 | 
			
		||||
  stabilityUrl: "",
 | 
			
		||||
  stabilityUrl: DEFAULT_STABILITY_URL,
 | 
			
		||||
  stabilityApiKey: "",
 | 
			
		||||
 | 
			
		||||
  // server config
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,15 @@
 | 
			
		||||
import { Stability, StoreKey } from "@/app/constant";
 | 
			
		||||
import { getHeaders } from "@/app/client/api";
 | 
			
		||||
import {
 | 
			
		||||
  Stability,
 | 
			
		||||
  StoreKey,
 | 
			
		||||
  ACCESS_CODE_PREFIX,
 | 
			
		||||
  ApiPath,
 | 
			
		||||
} from "@/app/constant";
 | 
			
		||||
import { getBearerToken } from "@/app/client/api";
 | 
			
		||||
import { createPersistStore } from "@/app/utils/store";
 | 
			
		||||
import { nanoid } from "nanoid";
 | 
			
		||||
import { uploadImage, base64Image2Blob } from "@/app/utils/chat";
 | 
			
		||||
import { models, getModelParamBasicData } from "@/app/components/sd/sd-panel";
 | 
			
		||||
import { useAccessStore } from "./access";
 | 
			
		||||
 | 
			
		||||
const defaultModel = {
 | 
			
		||||
  name: models[0].name,
 | 
			
		||||
@@ -57,18 +63,30 @@ export const useSdStore = createPersistStore<
 | 
			
		||||
        okCall?.();
 | 
			
		||||
      },
 | 
			
		||||
      stabilityRequestCall(data: any) {
 | 
			
		||||
        const accessStore = useAccessStore.getState();
 | 
			
		||||
        let prefix = ApiPath.Stability;
 | 
			
		||||
        let bearerToken = "";
 | 
			
		||||
        if (accessStore.useCustomConfig) {
 | 
			
		||||
          prefix = accessStore.stabilityUrl || ApiPath.Stability;
 | 
			
		||||
          bearerToken = getBearerToken(accessStore.stabilityApiKey);
 | 
			
		||||
        }
 | 
			
		||||
        if (!bearerToken && accessStore.enabledAccessControl()) {
 | 
			
		||||
          bearerToken = getBearerToken(
 | 
			
		||||
            ACCESS_CODE_PREFIX + accessStore.accessCode,
 | 
			
		||||
          );
 | 
			
		||||
        }
 | 
			
		||||
        const headers = {
 | 
			
		||||
          Accept: "application/json",
 | 
			
		||||
          Authorization: bearerToken,
 | 
			
		||||
        };
 | 
			
		||||
        const path = `${prefix}/${Stability.GeneratePath}/${data.model}`;
 | 
			
		||||
        const formData = new FormData();
 | 
			
		||||
        for (let paramsKey in data.params) {
 | 
			
		||||
          formData.append(paramsKey, data.params[paramsKey]);
 | 
			
		||||
        }
 | 
			
		||||
        const headers = getHeaders();
 | 
			
		||||
        delete headers["Content-Type"];
 | 
			
		||||
        fetch(`/api/stability/${Stability.GeneratePath}/${data.model}`, {
 | 
			
		||||
        fetch(path, {
 | 
			
		||||
          method: "POST",
 | 
			
		||||
          headers: {
 | 
			
		||||
            ...headers,
 | 
			
		||||
            Accept: "application/json",
 | 
			
		||||
          },
 | 
			
		||||
          headers,
 | 
			
		||||
          body: formData,
 | 
			
		||||
        })
 | 
			
		||||
          .then((response) => response.json())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user