From e0d3bcbd052adfb95e7c98f9203e2437db28f7b7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 6 Jul 2025 20:34:25 -0400 Subject: [PATCH] cleanup apis, sync apis with tauri --- .../app/src/api/SyncStorytellerApiConfig.ts | 49 ++++++++ frontend/apps/editor3d/app/src/api/index.tsx | 10 -- frontend/apps/editor3d/app/src/index.tsx | 7 +- .../pages/PageEnigma/Editor/api_fetchers.ts | 12 +- .../pages/PageEnigma/Editor/api_manager.ts | 8 +- .../notes/note_batch_media_endpoint.json | 113 ------------------ .../notes/osx_disable_security_chrome.sh | 2 - .../src/lib/config/StorytellerApiHostStore.ts | 4 +- 8 files changed, 67 insertions(+), 138 deletions(-) create mode 100644 frontend/apps/editor3d/app/src/api/SyncStorytellerApiConfig.ts delete mode 100644 frontend/apps/editor3d/app/src/api/index.tsx delete mode 100644 frontend/apps/editor3d/app/src/pages/PageEnigma/notes/note_batch_media_endpoint.json delete mode 100644 frontend/apps/editor3d/app/src/pages/PageEnigma/notes/osx_disable_security_chrome.sh diff --git a/frontend/apps/editor3d/app/src/api/SyncStorytellerApiConfig.ts b/frontend/apps/editor3d/app/src/api/SyncStorytellerApiConfig.ts new file mode 100644 index 0000000000..a1e75e3b15 --- /dev/null +++ b/frontend/apps/editor3d/app/src/api/SyncStorytellerApiConfig.ts @@ -0,0 +1,49 @@ +import { GetAppInfo } from "@storyteller/tauri-api"; +import { StorytellerApiHostStore } from "@storyteller/api"; + +const SYNC_THRESHOLD = 10 * 1000; + +class Cache { + private static instance: Cache; + private lastFetchSuccess?: number; + + public static getInstance(): Cache { + if (Cache.instance !== undefined) { + return Cache.instance; + } + const instance = new Cache(); + Cache.instance = instance; + return instance; + } + + public canCall() : boolean { + if (this.lastFetchSuccess === undefined) { + return true; + } + return Date.now() - this.lastFetchSuccess > SYNC_THRESHOLD; + } + + public setCallSuccess() { + this.lastFetchSuccess = Date.now(); + } +} + +export const SyncStorytellerApiConfig = async () => { + console.log("SyncStorytellerApiConfig()") + + const cache = Cache.getInstance(); + if (!cache.canCall()) { + return; + } + + GetAppInfo().then((appInfo) => { + console.log("SyncStorytellerApiConfig() - appInfo", appInfo); + const schemeAndHost = appInfo.payload.storyteller_host; + if (!schemeAndHost) { + return ; + } + console.log(`Updating hostname to ${schemeAndHost}`); + StorytellerApiHostStore.getInstance().setApiSchemeAndHost(schemeAndHost); + cache.setCallSuccess(); + }); +} diff --git a/frontend/apps/editor3d/app/src/api/index.tsx b/frontend/apps/editor3d/app/src/api/index.tsx deleted file mode 100644 index 9dd8e927a1..0000000000 --- a/frontend/apps/editor3d/app/src/api/index.tsx +++ /dev/null @@ -1,10 +0,0 @@ -export const apiHost = "https://api.storyteller.ai"; - -// Upload File -export const uploadMedia = `${apiHost}/v1/media_files/upload`; - -// Scenes -export const uploadNewScene = `${apiHost}/v1/media_files/upload/new_scene`; -export const uploadThumbnail = `${apiHost}/v1/media_files/cover_image/`; -export const updateExistingScene = (sceneToken: string) => - `${apiHost}/v1/media_files/upload/saved_scene/${sceneToken}`; diff --git a/frontend/apps/editor3d/app/src/index.tsx b/frontend/apps/editor3d/app/src/index.tsx index 817430f44d..a2f8bc7f85 100644 --- a/frontend/apps/editor3d/app/src/index.tsx +++ b/frontend/apps/editor3d/app/src/index.tsx @@ -10,7 +10,7 @@ import "@fortawesome/fontawesome-svg-core/styles.css"; import { config } from "@fortawesome/fontawesome-svg-core"; import EnvironmentVariables from "~/Classes/EnvironmentVariables"; import { pageHeight, pageWidth, persistLogin } from "~/signals"; - +import { SyncStorytellerApiConfig } from "./api/SyncStorytellerApiConfig"; import { posthog } from "posthog-js"; config.autoAddCss = false; /* eslint-disable import/first */ @@ -25,7 +25,12 @@ const ENV = { }; const GlobalSettingsManager = ({ env }: { env: Record }) => { + console.log("GlobalSettingsManager()"); + + SyncStorytellerApiConfig(); + useSignals(); + useSignalEffect(() => { persistLogin(); }); diff --git a/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_fetchers.ts b/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_fetchers.ts index 17338f706d..5c83b4d13d 100644 --- a/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_fetchers.ts +++ b/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_fetchers.ts @@ -1,12 +1,11 @@ import { v4 as uuidv4 } from "uuid"; -import { - uploadNewScene as uploadNewSceneEndpoint, - updateExistingScene as updateExistingSceneEndpoint, -} from "~/api"; import { FetchProxy as fetch } from "@storyteller/tauri-utils"; +import { StorytellerApiHostStore } from "@storyteller/api"; export const uploadNewScene = async (file: File, sceneTitle: string) => { - const endpoint = uploadNewSceneEndpoint; + const apiSchemeAndHost = StorytellerApiHostStore.getInstance().getApiSchemeAndHost(); + const endpoint = `${apiSchemeAndHost}/v1/media_files/upload/new_scene`; + const formData = new FormData(); formData.append("uuid_idempotency_token", uuidv4()); formData.append("file", file); @@ -36,7 +35,8 @@ export const uploadNewScene = async (file: File, sceneTitle: string) => { }; export const updateExistingScene = async (file: File, sceneToken: string) => { - const endpoint = updateExistingSceneEndpoint(sceneToken); + const apiSchemeAndHost = StorytellerApiHostStore.getInstance().getApiSchemeAndHost(); + const endpoint = `${apiSchemeAndHost}/v1/media_files/upload/saved_scene/${sceneToken}`; const formData = new FormData(); formData.append("uuid_idempotency_token", uuidv4()); diff --git a/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_manager.ts b/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_manager.ts index 95cacf2b8e..2619856df2 100644 --- a/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_manager.ts +++ b/frontend/apps/editor3d/app/src/pages/PageEnigma/Editor/api_manager.ts @@ -3,7 +3,6 @@ import * as THREE from "three"; import { GLTFExporter } from "three/addons/exporters/GLTFExporter.js"; import { signalScene, startPollingActiveJobs } from "~/signals"; import { updateExistingScene, uploadNewScene } from "./api_fetchers"; -import { uploadThumbnail } from "~/api"; import environmentVariables from "~/Classes/EnvironmentVariables"; import { GetCdnOrigin } from "~/api/GetCdnOrigin"; import { StorytellerApiHostStore } from "@storyteller/api"; @@ -164,7 +163,9 @@ export class APIManager { if (image_resp["media_file_token"]) { const image_token = image_resp["media_file_token"]; - await fetch(uploadThumbnail + uploadSceneResponse["media_file_token"], { + const endpoint = `${this.getApiSchemeAndHost()}/v1/media_files/cover_image/${uploadSceneResponse["media_file_token"]}`; + + await fetch(endpoint, { method: "POST", credentials: "include", headers: { @@ -183,8 +184,7 @@ export class APIManager { public async loadSceneState( scene_media_file_token: string | null, ): Promise { - const api_base_url = environmentVariables.values.BASE_API; - const url = `${api_base_url}/v1/media_files/file/${scene_media_file_token}`; + const url = `${this.getApiSchemeAndHost()}/v1/media_files/file/${scene_media_file_token}`; const response = await fetch(url); if (response.status > 200) { throw new APIManagerResponseError("Failed to load scene"); diff --git a/frontend/apps/editor3d/app/src/pages/PageEnigma/notes/note_batch_media_endpoint.json b/frontend/apps/editor3d/app/src/pages/PageEnigma/notes/note_batch_media_endpoint.json deleted file mode 100644 index 7368bf4d40..0000000000 --- a/frontend/apps/editor3d/app/src/pages/PageEnigma/notes/note_batch_media_endpoint.json +++ /dev/null @@ -1,113 +0,0 @@ -https://api.fakeyou.com/v1/media_files/batch?tokens=m_jh74zz1nfj2jxrkw7n00yamt32zrhb&tokens=m_0rqwx4kbnn2gz5s37apzftkw25vkzb&tokens=m_je04hbk7bqjdq9cnegp1j10mf4dzdb -{ - "success": true, - "media_files": [ - { - "token": "m_0rqwx4kbnn2gz5s37apzftkw25vkzb", - "media_type": "video", - "media_class": "unknown", - "maybe_media_subtype": null, - "maybe_engine_extension": null, - "maybe_batch_token": null, - "public_bucket_path": "/media/c/0/n/g/p/c0ngp9c570w66q3vhs0ygmwpasf2sfk1/videoc0ngp9c570w66q3vhs0ygmwpasf2sfk1mp4", - "maybe_model_weight_info": null, - "maybe_creator_user": { - "user_token": "U:937C1VN0S5G8H", - "username": "echelon", - "display_name": "echelon", - "gravatar_hash": "37e7e18f19d8f654a1ace405c846a9de", - "default_avatar": { - "image_index": 10, - "color_index": 3 - } - }, - "creator_set_visibility": "private", - "maybe_text_transcript": null, - "maybe_prompt_token": "prompt_3sf5p1n7g3sna6pc44nqgpvyd", - "maybe_original_filename": null, - "is_emulated_media_file": false, - "stats": { - "positive_rating_count": 0, - "bookmark_count": 0 - }, - "created_at": "2024-03-25T02:25:18Z", - "updated_at": "2024-03-25T02:25:18Z" - }, - { - "token": "m_je04hbk7bqjdq9cnegp1j10mf4dzdb", - "media_type": "video", - "media_class": "unknown", - "maybe_media_subtype": null, - "maybe_engine_extension": null, - "maybe_batch_token": null, - "public_bucket_path": "/media/d/a/3/b/6/da3b640rh3zjj1easv96vc4d7v4jq8c1/da3b640rh3zjj1easv96vc4d7v4jq8c1.mp4", - "maybe_model_weight_info": null, - "maybe_creator_user": { - "user_token": "U:937C1VN0S5G8H", - "username": "echelon", - "display_name": "echelon", - "gravatar_hash": "37e7e18f19d8f654a1ace405c846a9de", - "default_avatar": { - "image_index": 10, - "color_index": 3 - } - }, - "creator_set_visibility": "public", - "maybe_text_transcript": null, - "maybe_prompt_token": null, - "maybe_original_filename": null, - "is_emulated_media_file": false, - "stats": { - "positive_rating_count": 0, - "bookmark_count": 0 - }, - "created_at": "2024-03-25T02:04:22Z", - "updated_at": "2024-03-25T02:04:22Z" - }, - { - "token": "m_jh74zz1nfj2jxrkw7n00yamt32zrhb", - "media_type": "video", - "media_class": "unknown", - "maybe_media_subtype": null, - "maybe_engine_extension": null, - "maybe_batch_token": null, - "public_bucket_path": "/media/a/c/s/z/e/acszeddd464edypg9hcp5cga0482sjfv/videoacszeddd464edypg9hcp5cga0482sjfvmp4", - "maybe_model_weight_info": null, - "maybe_creator_user": { - "user_token": "U:937C1VN0S5G8H", - "username": "echelon", - "display_name": "echelon", - "gravatar_hash": "37e7e18f19d8f654a1ace405c846a9de", - "default_avatar": { - "image_index": 10, - "color_index": 3 - } - }, - "creator_set_visibility": "private", - "maybe_text_transcript": null, - "maybe_prompt_token": "prompt_dc8x82w2gqsvbpmp5ksxypssj", - "maybe_original_filename": null, - "is_emulated_media_file": false, - "stats": { - "positive_rating_count": 0, - "bookmark_count": 0 - }, - "created_at": "2024-03-25T02:12:46Z", - "updated_at": "2024-03-25T02:12:46Z" - } - ] -} - -// lip sync will be generated through TTS -async get_media_url() { - //This is for prod when we have the proper info on the url. - let api_base_url = environmentVariables.value.BASE_API; - let url = ${api_base_url}/v1/media_files/file/${this.media_id} - let responce = await fetch(url); - let json = await JSON.parse(await responce.text()); - let bucketPath = json["media_file"]["public_bucket_path"]; - let media_base_url = "https://storage.googleapis.com/vocodes-public" - let media_url = ${media_base_url}${bucketPath} - return media_url; - } -https://storage.googleapis.com/vocodes-public diff --git a/frontend/apps/editor3d/app/src/pages/PageEnigma/notes/osx_disable_security_chrome.sh b/frontend/apps/editor3d/app/src/pages/PageEnigma/notes/osx_disable_security_chrome.sh deleted file mode 100644 index edbc8e7881..0000000000 --- a/frontend/apps/editor3d/app/src/pages/PageEnigma/notes/osx_disable_security_chrome.sh +++ /dev/null @@ -1,2 +0,0 @@ -mkdir /User/ChromeData -open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/User/ChromeData" --disable-web-security \ No newline at end of file diff --git a/frontend/libs/api/src/lib/config/StorytellerApiHostStore.ts b/frontend/libs/api/src/lib/config/StorytellerApiHostStore.ts index 0041f857c1..225c9689b7 100644 --- a/frontend/libs/api/src/lib/config/StorytellerApiHostStore.ts +++ b/frontend/libs/api/src/lib/config/StorytellerApiHostStore.ts @@ -1,6 +1,6 @@ -//const DEFAULT_API_HOST = "https://api.storyteller.ai"; -const DEFAULT_API_HOST = "http://localhost:12345"; +//const DEFAULT_API_HOST = "http://localhost:12345"; +const DEFAULT_API_HOST = "https://api.storyteller.ai"; export class StorytellerApiHostStore { private static instance: StorytellerApiHostStore;