Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add credit display and tooltip functionality in action bar and prompt input components

konstantin-paulus105f81de8921
diff --git a/apps/web/src/assets/icons/ai-credits-small.svg b/apps/web/src/assets/icons/ai-credits-small.svg
new file mode 100644
index 00000000..49cc5934
--- /dev/null
+++ b/apps/web/src/assets/icons/ai-credits-small.svg
@@ -0,0 +1,3 @@
+<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path d="M12.0005 6.5C15.0379 6.50017 17.5005 8.96254 17.5005 12C17.5005 15.0375 15.0379 17.4998 12.0005 17.5C8.96292 17.5 6.50049 15.0376 6.50049 12C6.50049 8.96243 8.96292 6.5 12.0005 6.5ZM12.0005 7.5C9.51521 7.5 7.50049 9.51472 7.50049 12C7.50049 14.4853 9.51521 16.5 12.0005 16.5C14.4856 16.4998 16.5005 14.4852 16.5005 12C16.5005 9.51482 14.4856 7.50017 12.0005 7.5ZM11.6118 9.55176C11.7131 9.14833 12.2869 9.14833 12.3882 9.55176L12.5093 10.0361C12.6885 10.7525 13.2479 11.3121 13.9644 11.4912L14.4478 11.6123C14.8516 11.7133 14.8516 12.2867 14.4478 12.3877L13.9644 12.5088C13.2479 12.6879 12.6885 13.2475 12.5093 13.9639L12.3882 14.4482C12.2869 14.8517 11.7131 14.8517 11.6118 14.4482L11.4907 13.9639C11.3115 13.2475 10.7521 12.6879 10.0356 12.5088L9.55225 12.3877C9.14836 12.2867 9.14836 11.7133 9.55225 11.6123L10.0356 11.4912C10.7521 11.3121 11.3115 10.7525 11.4907 10.0361L11.6118 9.55176Z" fill="currentColor"/>
+</svg>
diff --git a/apps/web/src/components/genai/action-bar.tsx b/apps/web/src/components/genai/action-bar.tsx
index 08546aad..3c5fcab8 100644
--- a/apps/web/src/components/genai/action-bar.tsx
+++ b/apps/web/src/components/genai/action-bar.tsx
@@ -5,6 +5,7 @@
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";
import { Separator } from "@/components/ui/separator";
+import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import {
DropdownMenu,
DropdownMenuContent,
@@ -13,12 +14,13 @@ import {
DropdownMenuPortal,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-import { createMemo, Show } from "solid-js";
+import { createMemo, Show, type JSX } from "solid-js";
import { useGenerationRecords } from "./use-generation-records";
import { useGenerate } from "./use-generate";
import { useAutoCaptions } from "./use-auto-captions";
import { useMediaSelection } from "./selection";
import { useTransforms } from "./use-transforms";
+import { formatCost } from "./use-estimate";
import { restoreConfig } from "./saved-config";
import { toast } from "somoto";
@@ -30,7 +32,7 @@ interface ActionBarProps {
export function ActionBar(props: ActionBarProps) {
const { imageMedia, videoMedia } = useMediaSelection();
- const { run: runTransform } = useTransforms();
+ const { run: runTransform, price } = useTransforms();
const { rerun } = useGenerate();
const { generate: autoCaptions, hasScene } = useAutoCaptions();
@@ -82,22 +84,12 @@ export function ActionBar(props: ActionBarProps) {
</Show>
<Show when={isImage()}>
<div class="flex gap-1 items-center">
- <Button
- variant="ghost"
- class="gap-0 pl-0.5 text-muted-foreground"
- onClick={() => runTransform("removeBackground")}
- >
- <Icon name="ai-generate" />
+ <TransformButton icon="ai-generate" credits={price("removeBackground")} onClick={() => runTransform("removeBackground")}>
Remove background
- </Button>
- <Button
- variant="ghost"
- class="gap-0 pl-0.5 text-muted-foreground"
- onClick={() => runTransform("upscale")}
- >
- <Icon name="arrow-scale" />
+ </TransformButton>
+ <TransformButton icon="arrow-scale" credits={price("upscale")} onClick={() => runTransform("upscale")}>
Upscale
- </Button>
+ </TransformButton>
</div>
<Separator orientation="vertical" class="min-h-5" />
<div class="flex gap-1 items-center">
@@ -144,14 +136,9 @@ export function ActionBar(props: ActionBarProps) {
</Show>
<Show when={isVideo()}>
<div class="flex gap-1 items-center">
- <Button
- variant="ghost"
- class="gap-0 pl-0.5 text-muted-foreground"
- onClick={() => runTransform("upscale")}
- >
- <Icon name="arrow-scale" />
+ <TransformButton icon="arrow-scale" credits={price("upscale")} onClick={() => runTransform("upscale")}>
Upscale
- </Button>
+ </TransformButton>
<Show when={isGenerated()}>
<Separator orientation="vertical" class="min-h-5" />
<DropdownMenu placement="right">
@@ -187,3 +174,28 @@ export function ActionBar(props: ActionBarProps) {
</>
);
}
+
+type TransformButtonProps = {
+ icon: string;
+ /** What running it costs; a blurred stand-in while that isn't known. */
+ credits: number | undefined;
+ onClick(): void;
+ children: JSX.Element;
+};
+
+/** A paid action, its price on hover. */
+function TransformButton(props: TransformButtonProps) {
+ return (
+ <Tooltip>
+ <TooltipTrigger as={Button} variant="ghost" class="gap-0 pl-0.5 text-muted-foreground" onClick={props.onClick}>
+ <Icon name={props.icon} />
+ {props.children}
+ </TooltipTrigger>
+ <TooltipContent class="h-6 py-0">
+ <Show when={props.credits} fallback={<span class="blur-[3px] select-none">This will cost 000 credits</span>}>
+ {(credits) => formatCost(credits())}
+ </Show>
+ </TooltipContent>
+ </Tooltip>
+ );
+}
diff --git a/apps/web/src/components/genai/prompt-input-actions.tsx b/apps/web/src/components/genai/prompt-input-actions.tsx
index c91ed16b..35ffb242 100644
--- a/apps/web/src/components/genai/prompt-input-actions.tsx
+++ b/apps/web/src/components/genai/prompt-input-actions.tsx
@@ -21,6 +21,7 @@ import { useGenerationRecords } from "./use-generation-records";
import { useGenerate } from "./use-generate";
import { useMediaSelection } from "./selection";
import { useTransforms } from "./use-transforms";
+import { formatCost } from "./use-estimate";
import type { TransformType } from "./types";
@@ -59,7 +60,12 @@ export function PromptInputActions() {
>
<Icon name="rerun" />
</TooltipTrigger>
- <TooltipContent>Rerun</TooltipContent>
+ <TooltipContent class="flex-col items-stretch px-2 pt-0.5 pb-2">
+ <div class="flex h-7 items-center">Rerun</div>
+ <Show when={firstJob()}>
+ {(job) => <span class="font-normal text-muted-foreground">{formatCost(job().credits)}</span>}
+ </Show>
+ </TooltipContent>
</Tooltip>
<Separator orientation="vertical" class="min-h-5" />
</Show>
diff --git a/apps/web/src/components/genai/prompt-input.tsx b/apps/web/src/components/genai/prompt-input.tsx
index 0bb8cbf4..b7217113 100644
--- a/apps/web/src/components/genai/prompt-input.tsx
+++ b/apps/web/src/components/genai/prompt-input.tsx
@@ -50,7 +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 { formatCost, priceOf, useDebounced } from "./use-estimate";
import { PromptInputActions } from "./prompt-input-actions";
import type { GenerationConfig } from "./types";
import { AssetThumbnail } from "@/components/ui/asset-thumbnail";
@@ -66,6 +66,8 @@ import type { ThumbnailAsset } from "@/components/ui/asset-thumbnail";
const DEFAULT_BUTTON_CLASS = "text-muted-foreground gap-0 pr-2 pl-0";
+/** The text voices are compared on in the model menu: 1,000 characters. */
+const SAMPLE_SPEECH = "x".repeat(1000);
/** As many references as the prompt box has room for; a model may take fewer. */
const MAX_IMAGE_REFERENCES = 5;
@@ -89,8 +91,6 @@ 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 }));
@@ -102,6 +102,18 @@ export function PromptInput(props: PromptInputProps) {
/** The model and the settings it takes: what the prompt box offers. */
const option = createMemo(() => modelOption(config().model) ?? defaultModelOption(mode()));
const options = createMemo(() => modelOptions(mode()));
+
+ /** What generating costs as set: the price on the generate button. */
+ const settledConfig = useDebounced(config);
+ const credits = () => priceOf(settledConfig());
+
+ /**
+ * What a model costs at its default settings: the price the model menu
+ * lists, to compare the models by. A voice is priced by its text, so voices
+ * are priced on a stand-in of 1,000 characters: their rate per 1k.
+ */
+ const defaultPrice = (model: ModelId) =>
+ priceOf(fitToModel({ mode: mode(), model, prompt: mode() === "VOICE" ? SAMPLE_SPEECH : "" }));
const modeLabel = () => PROMPT_INPUT_MODE_OPTIONS.find((o) => o.value === mode())?.label.toLowerCase();
const maxImageReferences = createMemo(() => Math.min(MAX_IMAGE_REFERENCES, option().references ?? 0));
@@ -541,6 +553,7 @@ export function PromptInput(props: PromptInputProps) {
<ModelMenu
searchPlaceholder={`Search in ${modeLabel()} models`}
options={options()}
+ credits={defaultPrice}
value={modelAccessor}
onChange={handleModelChange}
/>
@@ -613,7 +626,7 @@ export function PromptInput(props: PromptInputProps) {
</div>
<Show when={credits() !== undefined}>
<span class="font-normal text-muted-foreground">
- This will cost {credits()!.toLocaleString()} {credits() === 1 ? "credit" : "credits"}
+ {formatCost(credits()!)}
</span>
</Show>
</TooltipContent>
@@ -632,6 +645,22 @@ export function PromptInput(props: PromptInputProps) {
);
}
+/** A price after a label, in a row with a gap: · ⊕ 46. Nothing without one. */
+function CreditsTag(props: { credits: number | undefined }) {
+ return (
+ <Show when={props.credits}>
+ {(credits) => (
+ <>
+ <Icon name="dot" class="size-3 text-muted-foreground" />
+ <span class="flex shrink-0 items-center gap-1 font-450 text-muted-foreground">
+ <Icon name="ai-credits-small" class="-mx-1.5 -my-1" />
+ {credits().toLocaleString()}
+ </span>
+ </>
+ )}
+ </Show>
+ );
+}
type PromptInputCompactMenuProps = {
menuLabel: string;
@@ -919,7 +948,9 @@ function DurationRangeMenu(props: DurationRangeMenuProps) {
type ModelMenuProps = {
searchPlaceholder: string;
- options: { id: string; name: string; description: string; icon: string }[];
+ options: { id: ModelId; name: string; description: string; icon: string }[];
+ /** A model's price at its default settings, when it has one. */
+ credits(model: ModelId): number | undefined;
value: Accessor<string>;
onChange(value: string): void;
}
@@ -1074,7 +1105,10 @@ function ModelMenu(props: ModelMenuProps) {
<Icon name={option.icon!} class="size-6 text-muted-foreground" />
</div>
<div class="min-w-0 flex-1 text-muted-foreground">
- <div class="truncate font-450" classList={{ "text-foreground": active() || selected() }}>{option.name}</div>
+ <div class="flex items-center gap-0.5 font-450">
+ <span class="truncate" classList={{ "text-foreground": active() || selected() }}>{option.name}</span>
+ <CreditsTag credits={props.credits(option.id)} />
+ </div>
<div class="truncate">{option.description}</div>
</div>
<span class="grid size-7 place-items-center">
diff --git a/apps/web/src/components/genai/use-estimate.ts b/apps/web/src/components/genai/use-estimate.ts
index 67b20944..40553d10 100644
--- a/apps/web/src/components/genai/use-estimate.ts
+++ b/apps/web/src/components/genai/use-estimate.ts
@@ -2,57 +2,105 @@
* 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 { createEffect, createSignal, onCleanup, type Accessor } from "solid-js";
+import { createStore } from "solid-js/store";
import { api } from "@/lib/api";
-import type { GenerateRequestInput } from "@diffusionstudio/api-contract";
+import type { GenerateRequestInput, ModelId, ModelInfo } 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;
+/**
+ * The request `config` is priced by, or undefined when it can't be priced.
+ * Only a voice is priced by its text (per 1k characters); every other model
+ * by its settings alone, so it is priced with a stand-in prompt — before one
+ * is typed, and without pricing it again on every key. Library inputs
+ * (references, frames) don't change a price and are left out: pricing them
+ * would mean uploading them first.
+ */
+function estimateRequest(config: GenerationConfig): GenerateRequestInput | undefined {
+ const { mode, model, aspectRatio, count, duration, resolution, voice } = config;
+ const prompt = mode === "VOICE" ? config.prompt.trim() : "-";
+ if (!prompt) return undefined;
+ const request = { model, prompt, aspectRatio, count, duration, resolution, voice };
+ return Object.fromEntries(Object.entries(request).filter(([, value]) => value !== undefined)) as GenerateRequestInput;
+}
+
+/** Prices by request, for the session: the pickers ask for the same few over and over. */
+const [prices, setPrices] = createStore<Record<string, number>>({});
+const pending = new Set<string>();
+
+/**
+ * What generating with `config` costs, in credits; undefined until the API
+ * has priced it, and when it can't. Reactive: the first read asks the API
+ * (reads in the same tick go as one batch), and the price fills in when it
+ * is back. A request the API couldn't price is asked again on a later read.
+ */
+export function priceOf(config: GenerationConfig): number | undefined {
+ const request = estimateRequest(config);
+ if (!request) return undefined;
+ const key = JSON.stringify(request);
+ const credits = prices[key];
+ if (credits === undefined && !pending.has(key)) {
+ pending.add(key);
+ api.estimate
+ .query(request)
+ .then((estimate) => setPrices(key, estimate.credits))
+ .catch(() => { })
+ .finally(() => pending.delete(key));
+ }
+ return credits;
+}
+
+/** The API's price list, fetched once a session. */
+const [catalog, setCatalog] = createSignal<ModelInfo[]>();
+let catalogRequested = false;
/**
- * 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.
+ * What running `model` costs, in credits, from its rate on the price list:
+ * for a model priced by the length of its input, over `seconds` of it. Rounded
+ * up to at least 1, as the API charges. For the tools, whose price needs no
+ * request: the request would need the media uploaded first. Undefined until
+ * the price list is in, and for a model priced by something else.
*/
-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) },
- );
+export function chargeOf(model: ModelId, seconds?: number): number | undefined {
+ if (!catalogRequested) {
+ catalogRequested = true;
+ api.models.list.query().then(setCatalog, () => (catalogRequested = false));
+ }
+ const pricing = catalog()?.find((info) => info.id === model)?.pricing;
+
+ if (!pricing) {
+ return undefined;
+ }
+
+ const quantity = {
+ "flat": 1,
+ "image": 1,
+ "second": seconds,
+ "input-second": seconds,
+ "minute": seconds === undefined ? undefined : seconds / 60,
+ "input-minute": seconds === undefined ? undefined : seconds / 60,
+ }[pricing.unit as string];
+
+ if (quantity) {
+ return Math.max(1, Math.ceil(quantity * pricing.credits));
+ }
+
+ return undefined;
+}
+
+/** A price the way a tooltip states it: "This will cost 38 credits". */
+export const formatCost = (credits: number) =>
+ `This will cost ${credits.toLocaleString()} ${credits === 1 ? "credit" : "credits"}`;
+
+/** `value`, once it has held still for `ms`: typing is not priced per key. */
+export function useDebounced<T>(value: Accessor<T>, ms = 300): Accessor<T> {
+ const [settled, setSettled] = createSignal(value());
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);
- });
+ const next = value();
+ const timer = setTimeout(() => setSettled(() => next), ms);
+ onCleanup(() => clearTimeout(timer));
});
-
- return credits;
+ return settled;
}
diff --git a/apps/web/src/components/genai/use-transforms.ts b/apps/web/src/components/genai/use-transforms.ts
index d8fb7f25..38b0ec47 100644
--- a/apps/web/src/components/genai/use-transforms.ts
+++ b/apps/web/src/components/genai/use-transforms.ts
@@ -10,9 +10,11 @@ import { useWorld } from "@diffusionstudio/koota-solid";
import { Generating, PaintType } from "@diffusionstudio/runtime";
import { transform } from "@/engine/generate";
import { useMediaSelection } from "./selection";
+import { chargeOf } from "./use-estimate";
import type { AssetRef, GenerateRequestInput } from "@diffusionstudio/api-contract";
import type { NodeMedia } from "@/engine/generate";
+import type { SelectedMedia } from "./selection";
import type { TransformType } from "./types";
interface Tool {
@@ -22,6 +24,8 @@ interface Tool {
takes(type: NodeMedia["type"]): boolean;
/** The request over `input`, an upload of media of `type`. */
request(type: NodeMedia["type"], input: AssetRef): GenerateRequestInput;
+ /** What running it over `media` costs, in credits; undefined while that isn't known. */
+ price(media: SelectedMedia): number | undefined;
}
const TOOLS: Record<TransformType, Tool> = {
@@ -30,11 +34,16 @@ const TOOLS: Record<TransformType, Tool> = {
takes: () => true,
request: (type, input) =>
type === PaintType.VIDEO ? { model: "upscale-video", video: input } : { model: "upscale-image", image: input },
+ price: ({ type, asset }) =>
+ type === PaintType.VIDEO
+ ? asset?.type === "VIDEO" ? chargeOf("upscale-video", asset.duration) : undefined
+ : chargeOf("upscale-image"),
},
removeBackground: {
title: "Background removal failed",
takes: (type) => type === PaintType.IMAGE,
request: (_type, image) => ({ model: "remove-background", image }),
+ price: () => chargeOf("remove-background"),
},
};
@@ -42,14 +51,24 @@ export function useTransforms() {
const world = useWorld();
const { media } = useMediaSelection();
+ /** The selected media `type` is run over: what it applies to and is not generating already. */
+ const targets = (type: TransformType) =>
+ media().filter((entry) => TOOLS[type].takes(entry.type) && !entry.node.has(Generating));
+
/** Runs `type` over every selected node it applies to that is not generating already. */
const run = (type: TransformType): void => {
const tool = TOOLS[type];
- for (const entry of media()) {
- if (!tool.takes(entry.type) || entry.node.has(Generating)) continue;
+ for (const entry of targets(type)) {
transform(world, entry, tool.title, (input) => tool.request(entry.type, input));
}
};
- return { run };
+ /** What `run(type)` costs, in credits, a job per node; undefined while any of it isn't known. */
+ const price = (type: TransformType): number | undefined => {
+ const prices = targets(type).map((entry) => TOOLS[type].price(entry));
+ if (!prices.length || prices.some((credits) => credits === undefined)) return undefined;
+ return prices.reduce<number>((sum, credits) => sum + credits!, 0);
+ };
+
+ return { run, price };
}