From 374590a918e4ef9bd76c75fbe521f87eef7e1e04 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 2 Mar 2026 04:55:46 -0500 Subject: [PATCH] NBP pricing on text to image works --- .../app/src/pages/PageDraw/PageDraw.tsx | 2 +- .../src/pages/PageEdit/BaseImageSelector.tsx | 2 +- .../app/src/pages/PageEdit/PageEdit.tsx | 2 +- .../app/src/pages/PageImage/TextToImage.tsx | 12 +- .../lib/common/generation/CommonImageModel.ts | 7 + .../common/generation/ImageGenerationMode.ts | 7 + .../src/lib/common/generation/index.ts | 2 + .../imageAspectRatioToCommonAspectRatio.ts | 34 ++++ .../convert/imageModelToCommonImageModel.ts | 14 ++ .../imageResolutionToCommonVideoResolution.ts | 29 ++++ .../lib/convert/imageStoreToGenerationMode.ts | 11 ++ .../pricing-modal/src/lib/convert/index.ts | 4 + .../src/lib/cost-breakdown-modal.tsx | 36 ++-- .../src/lib/useImageCostEstimate.ts | 154 ++++++++++++++++++ .../src/lib/useVideoCostEstimate.ts | 6 + .../promptbox/src/lib/PromptBoxImage.tsx | 17 +- .../promptbox/src/lib/promptStore.ts | 11 ++ frontend/libs/tauri-api/src/index.ts | 1 + .../lib/cost_estimate/EstimateImageCost.ts | 60 +++++++ .../lib/cost_estimate/EstimateVideoCost.ts | 1 + 20 files changed, 387 insertions(+), 25 deletions(-) create mode 100644 frontend/libs/api-enums/src/lib/common/generation/CommonImageModel.ts create mode 100644 frontend/libs/api-enums/src/lib/common/generation/ImageGenerationMode.ts create mode 100644 frontend/libs/components/pricing-modal/src/lib/convert/imageAspectRatioToCommonAspectRatio.ts create mode 100644 frontend/libs/components/pricing-modal/src/lib/convert/imageModelToCommonImageModel.ts create mode 100644 frontend/libs/components/pricing-modal/src/lib/convert/imageResolutionToCommonVideoResolution.ts create mode 100644 frontend/libs/components/pricing-modal/src/lib/convert/imageStoreToGenerationMode.ts create mode 100644 frontend/libs/components/pricing-modal/src/lib/useImageCostEstimate.ts create mode 100644 frontend/libs/tauri-api/src/lib/cost_estimate/EstimateImageCost.ts diff --git a/frontend/apps/artcraft/app/src/pages/PageDraw/PageDraw.tsx b/frontend/apps/artcraft/app/src/pages/PageDraw/PageDraw.tsx index 66daadd5a1..5e8b4eb563 100644 --- a/frontend/apps/artcraft/app/src/pages/PageDraw/PageDraw.tsx +++ b/frontend/apps/artcraft/app/src/pages/PageDraw/PageDraw.tsx @@ -971,7 +971,7 @@ const PageDraw = () => { />
- +
diff --git a/frontend/apps/artcraft/app/src/pages/PageEdit/BaseImageSelector.tsx b/frontend/apps/artcraft/app/src/pages/PageEdit/BaseImageSelector.tsx index 1f5fd4e832..f83dbba4f3 100644 --- a/frontend/apps/artcraft/app/src/pages/PageEdit/BaseImageSelector.tsx +++ b/frontend/apps/artcraft/app/src/pages/PageEdit/BaseImageSelector.tsx @@ -172,7 +172,7 @@ export const BaseImageSelector = ({
- +
{ />
- +
diff --git a/frontend/apps/artcraft/app/src/pages/PageImage/TextToImage.tsx b/frontend/apps/artcraft/app/src/pages/PageImage/TextToImage.tsx index bcf1ce39ff..c36d0665b9 100644 --- a/frontend/apps/artcraft/app/src/pages/PageImage/TextToImage.tsx +++ b/frontend/apps/artcraft/app/src/pages/PageImage/TextToImage.tsx @@ -28,7 +28,10 @@ import { import { Badge } from "@storyteller/ui-badge"; import { twMerge } from "tailwind-merge"; import { HelpMenuButton } from "@storyteller/ui-help-menu"; -import { CostCalculatorButton } from "@storyteller/ui-pricing-modal"; +import { + CostCalculatorButton, + useCostBreakdownModalStore, +} from "@storyteller/ui-pricing-modal"; import { GenerationProvider } from "@storyteller/api-enums"; const PAGE_ID: ModelPage = ModelPage.TextToImage; @@ -49,6 +52,10 @@ const TextToImage = ({ imageMediaId, imageUrl }: TextToImageProps) => { const selectedProvider: GenerationProvider | undefined = useSelectedProviderForModel(PAGE_ID, selectedImageModel?.id); + const imageCredits = useCostBreakdownModalStore( + (s) => s.estimatedCreditsByPage[PAGE_ID], + ); + const jobContext: JobContextType = { jobTokens: [], addJobToken: () => {}, @@ -217,6 +224,7 @@ const TextToImage = ({ imageMediaId, imageUrl }: TextToImageProps) => { imageMediaId={imageMediaId} url={imageUrl ?? undefined} onImageRowVisibilityChange={setImageRowVisible} + credits={imageCredits} onEnqueuePressed={async (prompt, count, subscriberId) => { const modelLabel = selectedImageModel?.fullName ?? ""; startBatch(prompt, count, modelLabel, subscriberId); @@ -240,7 +248,7 @@ const TextToImage = ({ imageMediaId, imageUrl }: TextToImageProps) => { />
- +
diff --git a/frontend/libs/api-enums/src/lib/common/generation/CommonImageModel.ts b/frontend/libs/api-enums/src/lib/common/generation/CommonImageModel.ts new file mode 100644 index 0000000000..9d6a56144e --- /dev/null +++ b/frontend/libs/api-enums/src/lib/common/generation/CommonImageModel.ts @@ -0,0 +1,7 @@ + +// NOTE: These are defined in Rust (as the source of truth) and duplicated in the frontend. +// In the future, we should use code gen (protobufs or similar) to keep the two sides in sync. + +export enum CommonImageModel { + NanaBananaPro = "nano_banana_pro", +} diff --git a/frontend/libs/api-enums/src/lib/common/generation/ImageGenerationMode.ts b/frontend/libs/api-enums/src/lib/common/generation/ImageGenerationMode.ts new file mode 100644 index 0000000000..b550ba6c28 --- /dev/null +++ b/frontend/libs/api-enums/src/lib/common/generation/ImageGenerationMode.ts @@ -0,0 +1,7 @@ + +// NOTE: These are defined in Rust (as the source of truth) and duplicated in the frontend. +// In the future, we should use code gen (protobufs or similar) to keep the two sides in sync. + +export type ImageGenerationMode = + | { type: "text_to_image" } + | { type: "image_edit"; count: number }; diff --git a/frontend/libs/api-enums/src/lib/common/generation/index.ts b/frontend/libs/api-enums/src/lib/common/generation/index.ts index 049f03b582..4a3922ff64 100644 --- a/frontend/libs/api-enums/src/lib/common/generation/index.ts +++ b/frontend/libs/api-enums/src/lib/common/generation/index.ts @@ -1,4 +1,6 @@ +export * from "./CommonImageModel.js"; export * from "./CommonVideoModel.js"; export * from "./CommonAspectRatio.js"; export * from "./CommonVideoResolution.js"; export * from "./GenerationMode.js"; +export * from "./ImageGenerationMode.js"; diff --git a/frontend/libs/components/pricing-modal/src/lib/convert/imageAspectRatioToCommonAspectRatio.ts b/frontend/libs/components/pricing-modal/src/lib/convert/imageAspectRatioToCommonAspectRatio.ts new file mode 100644 index 0000000000..e500e3da9e --- /dev/null +++ b/frontend/libs/components/pricing-modal/src/lib/convert/imageAspectRatioToCommonAspectRatio.ts @@ -0,0 +1,34 @@ +import { CommonAspectRatio } from "@storyteller/api-enums"; + +// TODO(bt): This shouldn't exist. We need to standardize types throughout the frontend. + +const VALID_COMMON_ASPECT_RATIOS = new Set( + Object.values(CommonAspectRatio), +); + +/** + * Convert an aspect ratio to a CommonAspectRatio for the image cost estimate API. + * + * Accepts either: + * - A new-style CommonAspectRatio value (e.g. "wide_sixteen_by_nine") — used when the model + * supports the new aspect ratio picker and the value is stored/passed as a string. + * - A legacy aspect ratio string ("wide" | "tall" | "square" | "auto") — used by the old + * prompt stores that predate the new CommonAspectRatio enum. + */ +export function imageAspectRatioToCommonAspectRatio( + newStyleAspectRatio: string | undefined, + legacyAspectRatio?: string, +): CommonAspectRatio | null { + if (newStyleAspectRatio && VALID_COMMON_ASPECT_RATIOS.has(newStyleAspectRatio)) { + return newStyleAspectRatio as CommonAspectRatio; + } + if (legacyAspectRatio) { + switch (legacyAspectRatio) { + case "wide": return CommonAspectRatio.Wide; + case "tall": return CommonAspectRatio.Tall; + case "square": return CommonAspectRatio.Square; + case "auto": return CommonAspectRatio.Auto; + } + } + return null; +} diff --git a/frontend/libs/components/pricing-modal/src/lib/convert/imageModelToCommonImageModel.ts b/frontend/libs/components/pricing-modal/src/lib/convert/imageModelToCommonImageModel.ts new file mode 100644 index 0000000000..17c8649ac8 --- /dev/null +++ b/frontend/libs/components/pricing-modal/src/lib/convert/imageModelToCommonImageModel.ts @@ -0,0 +1,14 @@ +import { CommonImageModel } from "@storyteller/api-enums"; + +// TODO(bt): This shouldn't exist. We need to standardize types throughout the frontend. + +export function imageModelToCommonImageModel( + tauriId: string, +): CommonImageModel | null { + switch (tauriId) { + case "nano_banana_pro": + return CommonImageModel.NanaBananaPro; + default: + return null; + } +} diff --git a/frontend/libs/components/pricing-modal/src/lib/convert/imageResolutionToCommonVideoResolution.ts b/frontend/libs/components/pricing-modal/src/lib/convert/imageResolutionToCommonVideoResolution.ts new file mode 100644 index 0000000000..a510f8b43b --- /dev/null +++ b/frontend/libs/components/pricing-modal/src/lib/convert/imageResolutionToCommonVideoResolution.ts @@ -0,0 +1,29 @@ +import { CommonVideoResolution } from "@storyteller/api-enums"; + +// TODO(bt): This shouldn't exist. We need to standardize types throughout the frontend. + +/** + * Convert a resolution value to CommonVideoResolution for the image cost estimate API. + * The image cost API reuses CommonVideoResolution since the values (1k/2k/4k) are the same. + * + * Accepts either: + * - A new-style CommonResolution value from @storyteller/model-list ("one_k", "two_k", "four_k") + * - A legacy resolution string ("1k", "2k", "4k") from the old prompt stores + */ +export function imageResolutionToCommonVideoResolution( + resolution: string | undefined, +): CommonVideoResolution | null { + switch (resolution) { + case "one_k": + case "1k": + return CommonVideoResolution.OneK; + case "two_k": + case "2k": + return CommonVideoResolution.TwoK; + case "four_k": + case "4k": + return CommonVideoResolution.FourK; + default: + return null; + } +} diff --git a/frontend/libs/components/pricing-modal/src/lib/convert/imageStoreToGenerationMode.ts b/frontend/libs/components/pricing-modal/src/lib/convert/imageStoreToGenerationMode.ts new file mode 100644 index 0000000000..c261ce5384 --- /dev/null +++ b/frontend/libs/components/pricing-modal/src/lib/convert/imageStoreToGenerationMode.ts @@ -0,0 +1,11 @@ +import { ImageGenerationMode } from "@storyteller/api-enums"; +import { RefImage } from "@storyteller/ui-promptbox"; + +export function imageStoreToGenerationMode( + referenceImages: RefImage[], +): ImageGenerationMode { + if (referenceImages.length > 0) { + return { type: "image_edit", count: referenceImages.length }; + } + return { type: "text_to_image" }; +} diff --git a/frontend/libs/components/pricing-modal/src/lib/convert/index.ts b/frontend/libs/components/pricing-modal/src/lib/convert/index.ts index 700f5cf714..ee778ad453 100644 --- a/frontend/libs/components/pricing-modal/src/lib/convert/index.ts +++ b/frontend/libs/components/pricing-modal/src/lib/convert/index.ts @@ -1,3 +1,7 @@ export * from "./videoModelToCommonVideoModel.js"; export * from "./videoAspectRatioToCommonAspectRatio.js"; export * from "./videoStoreToGenerationMode.js"; +export * from "./imageModelToCommonImageModel.js"; +export * from "./imageAspectRatioToCommonAspectRatio.js"; +export * from "./imageResolutionToCommonVideoResolution.js"; +export * from "./imageStoreToGenerationMode.js"; diff --git a/frontend/libs/components/pricing-modal/src/lib/cost-breakdown-modal.tsx b/frontend/libs/components/pricing-modal/src/lib/cost-breakdown-modal.tsx index e4344c440d..a15dcc84e3 100644 --- a/frontend/libs/components/pricing-modal/src/lib/cost-breakdown-modal.tsx +++ b/frontend/libs/components/pricing-modal/src/lib/cost-breakdown-modal.tsx @@ -28,6 +28,7 @@ import { import { Model } from "@storyteller/model-list"; import { useCurrency } from "./use-currency"; import { useVideoCostEstimate } from "./useVideoCostEstimate"; +import { useImageCostEstimate } from "./useImageCostEstimate"; // Drag handle subcomponent that Modal looks for const DragHandle = ({ children }: { children: React.ReactNode }) => ( @@ -113,6 +114,11 @@ export function CostBreakdownModal({ activeTabId }: CostBreakdownModalProps) { selectedModel, selectedProvider, ); + const { isLoading: isImageEstimateLoading } = useImageCostEstimate( + activePage, + selectedModel, + selectedProvider, + ); // Get generation settings from the appropriate stores based on active page const prompt2D = usePrompt2DStore(); @@ -165,17 +171,23 @@ export function CostBreakdownModal({ activeTabId }: CostBreakdownModalProps) { const storeData = getStoreData(); - // For video, use the live estimate from the backend; for others use a default - const videoCredits = - activePage === ModelPage.ImageToVideo - ? (estimatedCreditsByPage[ModelPage.ImageToVideo] ?? null) - : null; + // For video/image, use the live estimate from the backend; for others use a default + const IMAGE_PAGES_SET = new Set([ + ModelPage.TextToImage, + ModelPage.Canvas2D, + ModelPage.ImageEditor, + ]); + + const isLiveEstimatePage = + activePage === ModelPage.ImageToVideo || IMAGE_PAGES_SET.has(activePage); + const liveCredits = isLiveEstimatePage + ? (estimatedCreditsByPage[activePage] ?? null) + : null; const creditsPerGeneration = 1; - const totalCredits = - activePage === ModelPage.ImageToVideo - ? (videoCredits ?? null) - : creditsPerGeneration * storeData.generationCount; + const totalCredits = isLiveEstimatePage + ? liveCredits + : creditsPerGeneration * storeData.generationCount; // Convert credits to USD first (1 credit = $0.01), then to selected currency const usdAmount = (totalCredits ?? 0) * 0.01; @@ -308,8 +320,10 @@ export function CostBreakdownModal({ activeTabId }: CostBreakdownModalProps) { Credits
- {isVideoEstimateLoading && - activePage === ModelPage.ImageToVideo ? ( + {(isVideoEstimateLoading && + activePage === ModelPage.ImageToVideo) || + (isImageEstimateLoading && + IMAGE_PAGES_SET.has(activePage)) ? ( <> ([ + ModelPage.TextToImage, + ModelPage.Canvas2D, + ModelPage.ImageEditor, +]); + +export function useImageCostEstimate( + activePage: ModelPage, + selectedModel: Model | null | undefined, + selectedProvider: string | null | undefined, +): { isLoading: boolean } { + console.log(">>> useCostEstimateImage "); + + const [isLoading, setIsLoading] = useState(false); + const setEstimatedCreditsForPage = useCostBreakdownModalStore( + (s) => s.setEstimatedCreditsForPage, + ); + + // TextToImage store + const imageAspectRatio = usePromptImageStore((s) => s.commonAspectRatio); + const imageLegacyAspectRatio = usePromptImageStore((s) => s.aspectRatio); + const imageResolution = usePromptImageStore((s) => s.commonResolution); + const imageLegacyResolution = usePromptImageStore((s) => s.resolution); + const imageReferenceImages = usePromptImageStore((s) => s.referenceImages); + + // Canvas2D store + const prompt2DAspectRatio = usePrompt2DStore((s) => s.aspectRatio); + const prompt2DResolution = usePrompt2DStore((s) => s.resolution); + const prompt2DReferenceImages = usePrompt2DStore((s) => s.referenceImages); + + // ImageEditor store + const editReferenceImages = usePromptEditStore((s) => s.referenceImages); + const editAspectRatio = usePromptEditStore((s) => s.aspectRatio); + const editResolution = usePromptEditStore((s) => s.resolution); + + useEffect(() => { + if (!IMAGE_PAGES.has(activePage) || !selectedModel) { + console.log(">>> useCostEstimateImage - SKIP useImageCostEstimate (1)"); + return; + } + + const commonModel = imageModelToCommonImageModel(selectedModel.tauriId); + if (!commonModel) { + console.log(">>> useCostEstimateImage - SKIP useImageCostEstimate (2)"); + setEstimatedCreditsForPage(activePage, null); + return; + } + + console.log(">>> useCostEstimateImage - CALLING..."); + + let aspectRatioStr: string | undefined; + let legacyAspectRatioStr: string | undefined; + let resolutionStr: string | undefined; + let referenceImageCount = 0; + + switch (activePage) { + case ModelPage.TextToImage: + aspectRatioStr = imageAspectRatio; + legacyAspectRatioStr = imageLegacyAspectRatio; + resolutionStr = imageResolution ?? imageLegacyResolution; + referenceImageCount = imageReferenceImages.length; + break; + case ModelPage.Canvas2D: + legacyAspectRatioStr = prompt2DAspectRatio; + resolutionStr = prompt2DResolution; + referenceImageCount = prompt2DReferenceImages.length; + break; + case ModelPage.ImageEditor: + legacyAspectRatioStr = editAspectRatio; + resolutionStr = editResolution; + referenceImageCount = editReferenceImages.length; + break; + } + + const commonAspectRatio = imageAspectRatioToCommonAspectRatio( + aspectRatioStr, + legacyAspectRatioStr, + ); + const commonResolution = imageResolutionToCommonVideoResolution(resolutionStr); + const generationMode = + referenceImageCount > 0 + ? { type: "image_edit" as const, count: referenceImageCount } + : { type: "text_to_image" as const }; + + const provider = + (selectedProvider as GenerationProvider | null | undefined) ?? + GenerationProvider.Artcraft; + + setIsLoading(true); + + EstimateImageCost({ + model: commonModel, + provider, + generation_mode: generationMode, + aspect_ratio: commonAspectRatio ?? undefined, + resolution: commonResolution ?? undefined, + }) + .then((result) => { + console.log(">>> useCostEstimateImage - THEN RESULT...", result); + if (isEstimateImageCostSuccess(result)) { + const credits = result.payload.cost_in_credits ?? null; + setEstimatedCreditsForPage(activePage, credits); + } else { + console.log(">>> useCostEstimateImage - FAILURE (1)", result); + setEstimatedCreditsForPage(activePage, null); + } + }) + .catch(() => { + console.log(">>> useCostEstimateImage - FAILURE (2)"); + setEstimatedCreditsForPage(activePage, null); + }) + .finally(() => { + console.log(">>> useCostEstimateImage - FINALLY"); + setIsLoading(false); + }); + }, [ + activePage, + selectedModel?.id, + selectedProvider, + imageAspectRatio, + imageLegacyAspectRatio, + imageResolution, + imageLegacyResolution, + imageReferenceImages.length, + prompt2DAspectRatio, + prompt2DResolution, + prompt2DReferenceImages.length, + editAspectRatio, + editResolution, + editReferenceImages.length, + ]); + + return { isLoading }; +} diff --git a/frontend/libs/components/pricing-modal/src/lib/useVideoCostEstimate.ts b/frontend/libs/components/pricing-modal/src/lib/useVideoCostEstimate.ts index 66731c8d0f..a08c24c6f1 100644 --- a/frontend/libs/components/pricing-modal/src/lib/useVideoCostEstimate.ts +++ b/frontend/libs/components/pricing-modal/src/lib/useVideoCostEstimate.ts @@ -19,6 +19,8 @@ export function useVideoCostEstimate( selectedModel: Model | null | undefined, selectedProvider: string | null | undefined, ): { isLoading: boolean } { + console.log(">>> useCostEstimateVideo"); + const [isLoading, setIsLoading] = useState(false); const setEstimatedCreditsForPage = useCostBreakdownModalStore( (s) => s.setEstimatedCreditsForPage, @@ -32,15 +34,19 @@ export function useVideoCostEstimate( useEffect(() => { if (activePage !== ModelPage.ImageToVideo || !selectedModel) { + console.log(">>> useCostEstimateVideo - SKIP useVideoCostEstimate (1)"); return; } const commonModel = videoModelToCommonVideoModel(selectedModel.tauriId); if (!commonModel) { + console.log(">>> useCostEstimateVideo - SKIP useVideoCostEstimate (2)"); setEstimatedCreditsForPage(ModelPage.ImageToVideo, null); return; } + console.log(">>> useCostEstimateVideo - CALLING..."); + const videoModel = selectedModel as VideoModel; const commonAspectRatio = videoAspectRatioToCommonAspectRatio( aspectRatio, diff --git a/frontend/libs/components/promptbox/src/lib/PromptBoxImage.tsx b/frontend/libs/components/promptbox/src/lib/PromptBoxImage.tsx index 6e8a753193..7b617c4642 100644 --- a/frontend/libs/components/promptbox/src/lib/PromptBoxImage.tsx +++ b/frontend/libs/components/promptbox/src/lib/PromptBoxImage.tsx @@ -52,6 +52,7 @@ interface PromptBoxImageProps { imageMediaId?: string; url?: string; onImageRowVisibilityChange?: (visible: boolean) => void; + credits?: number | null; } export const PromptBoxImage = ({ @@ -63,6 +64,7 @@ export const PromptBoxImage = ({ imageMediaId, url, onImageRowVisibilityChange, + credits, }: PromptBoxImageProps) => { useSignals(); @@ -110,15 +112,11 @@ export const PromptBoxImage = ({ referenceImages.length > 0 || uploadingImages.length > 0; - // New aspect ratio state will begin to phase out old aspect ratio - const [commonAspectRatio, setCommonAspectRatio] = useState< - CommonAspectRatio | undefined - >(undefined); - - // New resolution state will begin to phase out old resolution - const [commonResolution, setCommonResolution] = useState< - CommonResolution | undefined - >(undefined); + // New aspect ratio and resolution — stored globally so cost estimates can observe them + const commonAspectRatio = usePromptImageStore((s) => s.commonAspectRatio); + const setCommonAspectRatio = usePromptImageStore((s) => s.setCommonAspectRatio); + const commonResolution = usePromptImageStore((s) => s.commonResolution); + const setCommonResolution = usePromptImageStore((s) => s.setCommonResolution); useEffect(() => { onImageRowVisibilityChange?.(isImageRowVisible); @@ -545,6 +543,7 @@ export const PromptBoxImage = ({ onClick={handleEnqueue} disabled={!prompt.trim()} loading={isEnqueueing} + credits={credits} > Generate diff --git a/frontend/libs/components/promptbox/src/lib/promptStore.ts b/frontend/libs/components/promptbox/src/lib/promptStore.ts index 3d3eb27886..f76b10e4ce 100644 --- a/frontend/libs/components/promptbox/src/lib/promptStore.ts +++ b/frontend/libs/components/promptbox/src/lib/promptStore.ts @@ -1,4 +1,6 @@ import { create } from "zustand"; +import { CommonAspectRatio } from "@storyteller/model-list"; +import { CommonResolution } from "@storyteller/model-list"; export interface RefImage { id: string; @@ -74,12 +76,17 @@ interface PromptImageStore { useSystemPrompt: boolean; referenceImages: RefImage[]; generationCount: number; + // New-style aspect ratio and resolution (preferred over legacy fields above) + commonAspectRatio: CommonAspectRatio | undefined; + commonResolution: CommonResolution | undefined; setPrompt: (prompt: string) => void; setAspectRatio: (ratio: AspectRatio) => void; setResolution: (resolution: Resolution) => void; setUseSystemPrompt: (value: boolean) => void; setReferenceImages: (images: RefImage[]) => void; setGenerationCount: (count: number) => void; + setCommonAspectRatio: (ratio: CommonAspectRatio | undefined) => void; + setCommonResolution: (resolution: CommonResolution | undefined) => void; } export const usePromptImageStore = create()((set) => ({ @@ -89,12 +96,16 @@ export const usePromptImageStore = create()((set) => ({ useSystemPrompt: true, referenceImages: [], generationCount: 1, + commonAspectRatio: undefined, + commonResolution: undefined, setPrompt: (prompt) => set({ prompt }), setAspectRatio: (aspectRatio) => set({ aspectRatio }), setResolution: (resolution) => set({ resolution }), setUseSystemPrompt: (useSystemPrompt) => set({ useSystemPrompt }), setReferenceImages: (referenceImages) => set({ referenceImages }), setGenerationCount: (generationCount) => set({ generationCount }), + setCommonAspectRatio: (commonAspectRatio) => set({ commonAspectRatio }), + setCommonResolution: (commonResolution) => set({ commonResolution }), })); // ----- Video Prompt Box Store ----- diff --git a/frontend/libs/tauri-api/src/index.ts b/frontend/libs/tauri-api/src/index.ts index 4e44e5b1d8..05939ee51a 100644 --- a/frontend/libs/tauri-api/src/index.ts +++ b/frontend/libs/tauri-api/src/index.ts @@ -41,4 +41,5 @@ export * from "./lib/sora/waitForSoraLogin.js"; export * from "./lib/system/GetAppInfo.js"; export * from "./lib/util/LoadWithoutCors.js"; export * from "./lib/worldlabs/WorldLabsGetCredentialInfo.js"; +export * from "./lib/cost_estimate/EstimateImageCost.js"; export * from "./lib/cost_estimate/EstimateVideoCost.js"; \ No newline at end of file diff --git a/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateImageCost.ts b/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateImageCost.ts new file mode 100644 index 0000000000..9e5ecd17e5 --- /dev/null +++ b/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateImageCost.ts @@ -0,0 +1,60 @@ +import { invoke } from "@tauri-apps/api/core"; +import { CommandResult, CommandSuccessStatus } from "../common/CommandStatus"; +import { + CommonImageModel, + CommonAspectRatio, + CommonVideoResolution, + ImageGenerationMode, + GenerationProvider, +} from "@storyteller/api-enums"; + +export interface EstimateImageCostRequest { + model: CommonImageModel; + provider: GenerationProvider; + generation_mode: ImageGenerationMode; + aspect_ratio?: CommonAspectRatio; + resolution?: CommonVideoResolution; + image_batch_count?: number; +} + +export interface EstimateImageCostPayload { + success: boolean; + cost_in_credits?: number; + cost_in_usd_cents?: number; + is_free: boolean; + is_unlimited: boolean; + is_rate_limited: boolean; + has_watermark: boolean; +} + +export interface EstimateImageCostSuccess extends CommandResult { + payload: EstimateImageCostPayload; +} + +export interface EstimateImageCostErrorPayload { + success: boolean; + error_type: "invalid_provider_for_model" | "invalid_input"; + error_message: string; +} + +export interface EstimateImageCostErrorResult extends CommandResult { + error_details?: EstimateImageCostErrorPayload; +} + +export type EstimateImageCostResult = + | EstimateImageCostSuccess + | EstimateImageCostErrorResult; + +export const EstimateImageCost = async ( + request: EstimateImageCostRequest, +): Promise => { + const result = await invoke("estimate_image_cost_command", { request }); + console.log(">>> useCostEstimateImage - EstimateImageCost() - Image Cost Estimate Result: ", result); + return result as EstimateImageCostResult; +}; + +export function isEstimateImageCostSuccess( + r: EstimateImageCostResult, +): r is EstimateImageCostSuccess { + return r.status === CommandSuccessStatus.Success; +} diff --git a/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateVideoCost.ts b/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateVideoCost.ts index 0afb920709..b6731016a9 100644 --- a/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateVideoCost.ts +++ b/frontend/libs/tauri-api/src/lib/cost_estimate/EstimateVideoCost.ts @@ -50,6 +50,7 @@ export const EstimateVideoCost = async ( request: EstimateVideoCostRequest, ): Promise => { const result = await invoke("estimate_video_cost_command", { request }); + console.log(">>> useCostEstimateVideo - EstimateVideoCost() - Video Cost Estimate Result: ", result); return result as EstimateVideoCostResult; };