Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add credit estimation feature in PromptInput component

konstantin-paulus932c92d5f6c5
diff --git a/apps/web/src/components/genai/prompt-input.tsx b/apps/web/src/components/genai/prompt-input.tsx
index fc9d67f6..0bb8cbf4 100644
--- a/apps/web/src/components/genai/prompt-input.tsx
+++ b/apps/web/src/components/genai/prompt-input.tsx
@@ -50,6 +50,7 @@ import { loadConfig, saveConfig } from "./saved-config";
import { createStoredSignal } from "@/lib/store";
import { store } from "@/init";
import { useGenerate } from "./use-generate";
+import { useEstimate } from "./use-estimate";
import { PromptInputActions } from "./prompt-input-actions";
import type { GenerationConfig } from "./types";
import { AssetThumbnail } from "@/components/ui/asset-thumbnail";
@@ -88,6 +89,8 @@ export function PromptInput(props: PromptInputProps) {
createEffect(() => saveConfig(config()));
+ const credits = useEstimate(config);
+
/** Shallow-merge updates into the current config. */
const patch = (updates: Partial<GenerationConfig>) => {
setConfig((prev) => ({ ...prev, ...updates }));
@@ -603,7 +606,17 @@ export function PromptInput(props: PromptInputProps) {
>
<Icon name="arrow-right" class="-rotate-90 size-6" />
</TooltipTrigger>
- <TooltipContent shortcut="↵">Generate</TooltipContent>
+ <TooltipContent class="flex-col items-stretch px-2 pt-0.5 pb-2">
+ <div class="flex h-7 items-center gap-2">
+ <span class="flex-1 truncate">Generate</span>
+ <span class="min-w-5 text-right text-xxs font-normal text-muted-foreground">↩︎</span>
+ </div>
+ <Show when={credits() !== undefined}>
+ <span class="font-normal text-muted-foreground">
+ This will cost {credits()!.toLocaleString()} {credits() === 1 ? "credit" : "credits"}
+ </span>
+ </Show>
+ </TooltipContent>
</Tooltip>
</div>
<Show when={isDragging()}>
diff --git a/apps/web/src/components/genai/use-estimate.ts b/apps/web/src/components/genai/use-estimate.ts
new file mode 100644
index 00000000..67b20944
--- /dev/null
+++ b/apps/web/src/components/genai/use-estimate.ts
@@ -0,0 +1,58 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { createEffect, createMemo, createSignal, onCleanup, type Accessor } from "solid-js";
+import { api } from "@/lib/api";
+
+import type { GenerateRequestInput } from "@diffusionstudio/api-contract";
+import type { GenerationConfig } from "./types";
+
+/** How long the settings must hold still before they are priced: typing is not priced per key. */
+const DEBOUNCE_MS = 300;
+
+/**
+ * What generating with `config` would cost, in credits: fetched again after
+ * every change that makes a valid request. The last price is kept while the
+ * next is on its way, and dropped when a request can't be priced.
+ *
+ * Library inputs (references, frames) are left out: pricing them would mean
+ * uploading them first, and the price is set by the settings.
+ */
+export function useEstimate(config: Accessor<GenerationConfig>): Accessor<number | undefined> {
+ const [credits, setCredits] = createSignal<number>();
+
+ const request = createMemo(
+ () => {
+ const { model, prompt, aspectRatio, count, duration, resolution, voice } = config();
+ if (!prompt.trim()) return undefined;
+ const request = { model, prompt: prompt.trim(), aspectRatio, count, duration, resolution, voice };
+ return Object.fromEntries(Object.entries(request).filter(([, value]) => value !== undefined)) as GenerateRequestInput;
+ },
+ undefined,
+ { equals: (a, b) => JSON.stringify(a) === JSON.stringify(b) },
+ );
+
+ createEffect(() => {
+ const input = request();
+ if (!input) {
+ setCredits(undefined);
+ return;
+ }
+
+ let stale = false;
+ const timer = setTimeout(() => {
+ api.estimate
+ .query(input)
+ .then((estimate) => !stale && setCredits(estimate.credits))
+ .catch(() => !stale && setCredits(undefined));
+ }, DEBOUNCE_MS);
+
+ onCleanup(() => {
+ stale = true;
+ clearTimeout(timer);
+ });
+ });
+
+ return credits;
+}