sd.ts 2.7 KB

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