Skip to content

Commit 8f6e5d7

Browse files
committed
hotfix: can send sd task in client
1 parent fd441d9 commit 8f6e5d7

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

app/client/api.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ export class ClientApi {
168168
}
169169
}
170170

171+
export function getBearerToken(
172+
apiKey: string,
173+
noBearer: boolean = false,
174+
): string {
175+
return validString(apiKey)
176+
? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
177+
: "";
178+
}
179+
180+
export function validString(x: string): boolean {
181+
return x?.length > 0;
182+
}
183+
171184
export function getHeaders() {
172185
const accessStore = useAccessStore.getState();
173186
const chatStore = useChatStore.getState();
@@ -214,15 +227,6 @@ export function getHeaders() {
214227
return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
215228
}
216229

217-
function getBearerToken(apiKey: string, noBearer: boolean = false): string {
218-
return validString(apiKey)
219-
? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
220-
: "";
221-
}
222-
223-
function validString(x: string): boolean {
224-
return x?.length > 0;
225-
}
226230
const {
227231
isGoogle,
228232
isAzure,

app/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export enum ApiPath {
4646
Baidu = "/api/baidu",
4747
ByteDance = "/api/bytedance",
4848
Alibaba = "/api/alibaba",
49+
Stability = "/api/stability",
4950
}
5051

5152
export enum SlotID {

app/store/access.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const DEFAULT_ALIBABA_URL = isApp
3939
? DEFAULT_API_HOST + "/api/proxy/alibaba"
4040
: ApiPath.Alibaba;
4141

42+
const DEFAULT_STABILITY_URL = isApp
43+
? DEFAULT_API_HOST + "/api/proxy/stability"
44+
: ApiPath.Stability;
45+
4246
const DEFAULT_ACCESS_STATE = {
4347
accessCode: "",
4448
useCustomConfig: false,
@@ -79,7 +83,7 @@ const DEFAULT_ACCESS_STATE = {
7983
alibabaApiKey: "",
8084

8185
//stability
82-
stabilityUrl: "",
86+
stabilityUrl: DEFAULT_STABILITY_URL,
8387
stabilityApiKey: "",
8488

8589
// server config

app/store/sd.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { Stability, StoreKey } from "@/app/constant";
2-
import { getHeaders } from "@/app/client/api";
1+
import {
2+
Stability,
3+
StoreKey,
4+
ACCESS_CODE_PREFIX,
5+
ApiPath,
6+
} from "@/app/constant";
7+
import { getBearerToken } from "@/app/client/api";
38
import { createPersistStore } from "@/app/utils/store";
49
import { nanoid } from "nanoid";
510
import { uploadImage, base64Image2Blob } from "@/app/utils/chat";
611
import { models, getModelParamBasicData } from "@/app/components/sd/sd-panel";
12+
import { useAccessStore } from "./access";
713

814
const defaultModel = {
915
name: models[0].name,
@@ -57,18 +63,30 @@ export const useSdStore = createPersistStore<
5763
okCall?.();
5864
},
5965
stabilityRequestCall(data: any) {
66+
const accessStore = useAccessStore.getState();
67+
let prefix = ApiPath.Stability;
68+
let bearerToken = "";
69+
if (accessStore.useCustomConfig) {
70+
prefix = accessStore.stabilityUrl || ApiPath.Stability;
71+
bearerToken = getBearerToken(accessStore.stabilityApiKey);
72+
}
73+
if (!bearerToken && accessStore.enabledAccessControl()) {
74+
bearerToken = getBearerToken(
75+
ACCESS_CODE_PREFIX + accessStore.accessCode,
76+
);
77+
}
78+
const headers = {
79+
Accept: "application/json",
80+
Authorization: bearerToken,
81+
};
82+
const path = `${prefix}/${Stability.GeneratePath}/${data.model}`;
6083
const formData = new FormData();
6184
for (let paramsKey in data.params) {
6285
formData.append(paramsKey, data.params[paramsKey]);
6386
}
64-
const headers = getHeaders();
65-
delete headers["Content-Type"];
66-
fetch(`/api/stability/${Stability.GeneratePath}/${data.model}`, {
87+
fetch(path, {
6788
method: "POST",
68-
headers: {
69-
...headers,
70-
Accept: "application/json",
71-
},
89+
headers,
7290
body: formData,
7391
})
7492
.then((response) => response.json())

0 commit comments

Comments
 (0)