Repository navigation
Add OpenAI Live and Spend chart switch #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd3e84f
Add OpenAI Live and Spend chart switch
60cfc49
Merge remote-tracking branch 'origin/main' into feat/openai-live-spen…
5eeb515
Merge remote-tracking branch 'origin/main' into feat/openai-live-spen…
edaa55a
Keep Escape from closing the tray panel while a bar is selected
04f32b0
Gate the OpenAI Live/Spend switch on reported traffic
8f49cba
Merge remote-tracking branch 'origin/main' into feat/openai-live-spen…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
apps/desktop-tauri/src/components/OpenAILiveSpendChart.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { useId, useState } from "react"; | ||
| import type { LocaleKey } from "../i18n/keys"; | ||
| import { hasReportedTraffic } from "../lib/openaiThroughput"; | ||
| import { | ||
| hasSpendDays, | ||
| type ProviderChartMode, | ||
| readChartMode, | ||
| spendDaySummary, | ||
| storeChartMode, | ||
| } from "../lib/openaiSpend"; | ||
| import type { OpenAiApiUsageSnapshot, ProviderThroughputSnapshot } from "../types/bridge"; | ||
| import { ScrubBarStrip } from "./charts/ScrubBarStrip"; | ||
| import { OpenAITokensPerMinuteChart } from "./OpenAITokensPerMinuteChart"; | ||
|
|
||
| /** | ||
| * OpenAI API card chart with the upstream 0.75.0 Live · Spend switch | ||
| * (`ProviderMenuChartContent`). The switch only appears when there is both a | ||
| * Tokens/min window and daily spend to show; otherwise the card keeps the plain | ||
| * Tokens/min grid. Both layers stay mounted in one grid cell so switching never | ||
| * changes the card height, and the choice persists per provider. | ||
| */ | ||
|
|
||
| type T = (key: LocaleKey) => string; | ||
|
|
||
| const MODES: readonly { id: ProviderChartMode; label: LocaleKey }[] = [ | ||
| { id: "live", label: "OpenAiChartModeLive" }, | ||
| { id: "spend", label: "OpenAiChartModeSpend" }, | ||
| ]; | ||
|
|
||
| export interface OpenAILiveSpendChartProps { | ||
| providerId: string; | ||
| throughput: ProviderThroughputSnapshot; | ||
| usage: OpenAiApiUsageSnapshot | null | undefined; | ||
| t: T; | ||
| } | ||
|
|
||
| export function OpenAILiveSpendChart({ providerId, throughput, usage, t }: OpenAILiveSpendChartProps) { | ||
| const [mode, setModeState] = useState<ProviderChartMode>(() => readChartMode(providerId)); | ||
| // The switch needs both charts to have something to show: reported Tokens/min | ||
| // traffic and daily spend. Otherwise the card keeps the plain grid. | ||
| const spendUsage = usage && hasSpendDays(usage) && hasReportedTraffic(throughput) ? usage : null; | ||
| if (!spendUsage) return <OpenAITokensPerMinuteChart throughput={throughput} t={t} />; | ||
|
|
||
| const setMode = (next: ProviderChartMode) => { | ||
| setModeState(next); | ||
| storeChartMode(providerId, next); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="openai-live-spend" data-mode={mode}> | ||
| <div | ||
| className="provider-detail-segmented openai-live-spend__switch" | ||
| role="group" | ||
| aria-label={t("OpenAiChartModeLabel")} | ||
| > | ||
| {MODES.map((option) => ( | ||
| <button | ||
| key={option.id} | ||
| type="button" | ||
| aria-pressed={option.id === mode} | ||
| className={`provider-detail-segmented__option${option.id === mode ? " is-active" : ""}`} | ||
| onClick={() => setMode(option.id)} | ||
| > | ||
| {t(option.label)} | ||
| </button> | ||
| ))} | ||
| </div> | ||
| <div className="openai-live-spend__stack"> | ||
| <div | ||
| className={`openai-live-spend__layer${mode === "live" ? "" : " is-hidden"}`} | ||
| aria-hidden={mode !== "live"} | ||
| data-testid="openai-live-layer" | ||
| > | ||
| <OpenAITokensPerMinuteChart throughput={throughput} t={t} /> | ||
| </div> | ||
| <div | ||
| className={`openai-live-spend__layer${mode === "spend" ? "" : " is-hidden"}`} | ||
| aria-hidden={mode !== "spend"} | ||
| data-testid="openai-spend-layer" | ||
| > | ||
| <OpenAISpendChart usage={spendUsage} t={t} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function OpenAISpendChart({ usage, t }: { usage: OpenAiApiUsageSnapshot; t: T }) { | ||
| const summaryId = useId(); | ||
| const [selectedId, setSelectedId] = useState<string | null>(null); | ||
| const bars = usage.daily.map((day) => ({ id: String(day.startTime), value: day.costUsd })); | ||
| const selectedDay = usage.daily.find((day) => String(day.startTime) === selectedId) ?? null; | ||
| const title = t("OpenAiSpendChartTitle").replace("{}", String(usage.historyDays)); | ||
|
|
||
| return ( | ||
| <section className="openai-tpm openai-spend" aria-label={title}> | ||
| <div className="openai-tpm__title">{title}</div> | ||
| <ScrubBarStrip | ||
| bars={bars} | ||
| selectedId={selectedId} | ||
| onSelect={setSelectedId} | ||
| classPrefix="openai-tpm" | ||
| className="openai-tpm__chart" | ||
| label={title} | ||
| describedBy={summaryId} | ||
| testId="openai-spend-chart" | ||
| /> | ||
| <p id={summaryId} className="openai-tpm__summary" aria-live="polite"> | ||
| {selectedDay ? spendDaySummary(selectedDay, t) : "\u00a0"} | ||
| </p> | ||
| </section> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.