sd.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { initDB, useIndexedDB } from "react-indexed-db-hook";
  2. import { StabilityPath, StoreKey } from "@/app/constant";
  3. import { create, StoreApi } from "zustand";
  4. import { showToast } from "@/app/components/ui-lib";
  5. import { getHeaders } from "@/app/client/api";
  6. export const SdDbConfig = {
  7. name: "@chatgpt-next-web/sd",
  8. version: 1,
  9. objectStoresMeta: [
  10. {
  11. store: StoreKey.SdList,
  12. storeConfig: { keyPath: "id", autoIncrement: true },
  13. storeSchema: [
  14. { name: "model", keypath: "model", options: { unique: false } },
  15. {
  16. name: "model_name",
  17. keypath: "model_name",
  18. options: { unique: false },
  19. },
  20. { name: "status", keypath: "status", options: { unique: false } },
  21. { name: "params", keypath: "params", options: { unique: false } },
  22. { name: "img_data", keypath: "img_data", options: { unique: false } },
  23. { name: "error", keypath: "error", options: { unique: false } },
  24. {
  25. name: "created_at",
  26. keypath: "created_at",
  27. options: { unique: false },
  28. },
  29. ],
  30. },
  31. ],
  32. };
  33. export function SdDbInit() {
  34. initDB(SdDbConfig);
  35. }
  36. type SdStore = {
  37. execCount: number;
  38. execCountInc: () => void;
  39. };
  40. export const useSdStore = create<SdStore>()((set) => ({
  41. execCount: 1,
  42. execCountInc: () => set((state) => ({ execCount: state.execCount + 1 })),
  43. }));
  44. export function sendSdTask(data: any, db: any, inc: any, okCall?: Function) {
  45. db.add(data).then(
  46. (id: number) => {
  47. data = { ...data, id, status: "running" };
  48. db.update(data);
  49. inc();
  50. stabilityRequestCall(data, db, inc);
  51. okCall?.();
  52. },
  53. (error: any) => {
  54. console.error(error);
  55. showToast(`error: ` + error.message);
  56. },
  57. );
  58. }
  59. export function stabilityRequestCall(data: any, db: any, inc: any) {
  60. const formData = new FormData();
  61. for (let paramsKey in data.params) {
  62. formData.append(paramsKey, data.params[paramsKey]);
  63. }
  64. const headers = getHeaders();
  65. delete headers["Content-Type"];
  66. fetch(`/api/stability/${StabilityPath.GeneratePath}/${data.model}`, {
  67. method: "POST",
  68. headers: {
  69. ...headers,
  70. Accept: "application/json",
  71. },
  72. body: formData,
  73. })
  74. .then((response) => response.json())
  75. .then((resData) => {
  76. if (resData.errors && resData.errors.length > 0) {
  77. db.update({ ...data, status: "error", error: resData.errors[0] });
  78. inc();
  79. return;
  80. }
  81. if (resData.finish_reason === "SUCCESS") {
  82. db.update({ ...data, status: "success", img_data: resData.image });
  83. } else {
  84. db.update({ ...data, status: "error", error: JSON.stringify(resData) });
  85. }
  86. inc();
  87. })
  88. .catch((error) => {
  89. db.update({ ...data, status: "error", error: error.message });
  90. console.error("Error:", error);
  91. inc();
  92. });
  93. }