🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(goal): show budget token counts as K → M → B and right-align the columns (#21270)
Arvin Xu4028677d4547
diff --git a/locales/ar/chat.json b/locales/ar/chat.jsonindex b98f311df0c..16b0d5bb89e 100644--- a/locales/ar/chat.json+++ b/locales/ar/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} تشغيلات","goalProcess.metricDetail.budget.saveFailed": "تعذر تحديث الميزانية","goalProcess.metricDetail.budget.title": "الميزانية",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} رموز","goalProcess.metricDetail.budget.totalCost": "حد التكلفة الإجمالية","goalProcess.metricDetail.budget.uncapped": "غير محدد","goalProcess.metricDetail.duration.now": "الآن",diff --git a/locales/bg-BG/chat.json b/locales/bg-BG/chat.jsonindex 9a70fa62475..2160f15bee8 100644--- a/locales/bg-BG/chat.json+++ b/locales/bg-BG/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} изпълнения","goalProcess.metricDetail.budget.saveFailed": "Неуспешно актуализиране на бюджета","goalProcess.metricDetail.budget.title": "Бюджет",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} токени","goalProcess.metricDetail.budget.totalCost": "Общо ограничение на разходите","goalProcess.metricDetail.budget.uncapped": "Без ограничение","goalProcess.metricDetail.duration.now": "сега",diff --git a/locales/de-DE/chat.json b/locales/de-DE/chat.jsonindex 0b3b88b503b..3cd5f8f09b9 100644--- a/locales/de-DE/chat.json+++ b/locales/de-DE/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} Läufe","goalProcess.metricDetail.budget.saveFailed": "Das Budget konnte nicht aktualisiert werden","goalProcess.metricDetail.budget.title": "Budget",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} Tokens","goalProcess.metricDetail.budget.totalCost": "Gesamtkostenobergrenze","goalProcess.metricDetail.budget.uncapped": "Unbegrenzt","goalProcess.metricDetail.duration.now": "jetzt",diff --git a/locales/en-US/chat.json b/locales/en-US/chat.jsonindex 28f30489526..50ff483355e 100644--- a/locales/en-US/chat.json+++ b/locales/en-US/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} runs","goalProcess.metricDetail.budget.saveFailed": "Could not update the budget","goalProcess.metricDetail.budget.title": "Cost",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} tokens","goalProcess.metricDetail.budget.totalCost": "Spend","goalProcess.metricDetail.budget.uncapped": "Uncapped","goalProcess.metricDetail.duration.now": "now",diff --git a/locales/es-ES/chat.json b/locales/es-ES/chat.jsonindex f13c9190140..5b2a7b72bab 100644--- a/locales/es-ES/chat.json+++ b/locales/es-ES/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} ejecuciones","goalProcess.metricDetail.budget.saveFailed": "No se pudo actualizar el presupuesto","goalProcess.metricDetail.budget.title": "Presupuesto",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} tokens","goalProcess.metricDetail.budget.totalCost": "Límite de costo total","goalProcess.metricDetail.budget.uncapped": "Sin límite","goalProcess.metricDetail.duration.now": "ahora",diff --git a/locales/fa-IR/chat.json b/locales/fa-IR/chat.jsonindex 88b4d9cf60a..2b0c35ca915 100644--- a/locales/fa-IR/chat.json+++ b/locales/fa-IR/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} اجرا","goalProcess.metricDetail.budget.saveFailed": "بهروزرسانی بودجه امکانپذیر نبود","goalProcess.metricDetail.budget.title": "بودجه",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} توکن","goalProcess.metricDetail.budget.totalCost": "حد کل هزینه","goalProcess.metricDetail.budget.uncapped": "بدون محدودیت","goalProcess.metricDetail.duration.now": "اکنون",diff --git a/locales/fr-FR/chat.json b/locales/fr-FR/chat.jsonindex 4c25baf7e2c..f67b4de9ff0 100644--- a/locales/fr-FR/chat.json+++ b/locales/fr-FR/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} exécutions","goalProcess.metricDetail.budget.saveFailed": "Impossible de mettre à jour le budget","goalProcess.metricDetail.budget.title": "Budget",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} jetons","goalProcess.metricDetail.budget.totalCost": "Plafond des coûts totaux","goalProcess.metricDetail.budget.uncapped": "Sans limite","goalProcess.metricDetail.duration.now": "maintenant",diff --git a/locales/it-IT/chat.json b/locales/it-IT/chat.jsonindex 0e75e3655a6..27e5caff6bf 100644--- a/locales/it-IT/chat.json+++ b/locales/it-IT/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} esecuzioni","goalProcess.metricDetail.budget.saveFailed": "Impossibile aggiornare il budget","goalProcess.metricDetail.budget.title": "Budget",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} token","goalProcess.metricDetail.budget.totalCost": "Limite di costo totale","goalProcess.metricDetail.budget.uncapped": "Senza limite","goalProcess.metricDetail.duration.now": "ora",diff --git a/locales/ja-JP/chat.json b/locales/ja-JP/chat.jsonindex 85365f2d31d..7bd16c0f467 100644--- a/locales/ja-JP/chat.json+++ b/locales/ja-JP/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} 実行","goalProcess.metricDetail.budget.saveFailed": "予算を更新できませんでした","goalProcess.metricDetail.budget.title": "予算",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} トークン","goalProcess.metricDetail.budget.totalCost": "総費用上限","goalProcess.metricDetail.budget.uncapped": "上限なし","goalProcess.metricDetail.duration.now": "現在",diff --git a/locales/ko-KR/chat.json b/locales/ko-KR/chat.jsonindex dba1c68fd5e..8fc0271e9fb 100644--- a/locales/ko-KR/chat.json+++ b/locales/ko-KR/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} 실행","goalProcess.metricDetail.budget.saveFailed": "예산을 업데이트할 수 없습니다","goalProcess.metricDetail.budget.title": "예산",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} 토큰","goalProcess.metricDetail.budget.totalCost": "총 비용 한도","goalProcess.metricDetail.budget.uncapped": "제한 없음","goalProcess.metricDetail.duration.now": "현재",diff --git a/locales/nl-NL/chat.json b/locales/nl-NL/chat.jsonindex 9c88b68b454..878f5667187 100644--- a/locales/nl-NL/chat.json+++ b/locales/nl-NL/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} runs","goalProcess.metricDetail.budget.saveFailed": "Kon het budget niet bijwerken","goalProcess.metricDetail.budget.title": "Budget",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} tokens","goalProcess.metricDetail.budget.totalCost": "Totale kostenlimiet","goalProcess.metricDetail.budget.uncapped": "Onbeperkt","goalProcess.metricDetail.duration.now": "nu",diff --git a/locales/pl-PL/chat.json b/locales/pl-PL/chat.jsonindex 9284c816423..1e634d1b696 100644--- a/locales/pl-PL/chat.json+++ b/locales/pl-PL/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} przebiegów","goalProcess.metricDetail.budget.saveFailed": "Nie udało się zaktualizować budżetu","goalProcess.metricDetail.budget.title": "Budżet",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} tokenów","goalProcess.metricDetail.budget.totalCost": "Całkowity limit kosztów","goalProcess.metricDetail.budget.uncapped": "Bez limitu","goalProcess.metricDetail.duration.now": "teraz",diff --git a/locales/pt-BR/chat.json b/locales/pt-BR/chat.jsonindex f3ca62f36e6..5e6063b0df5 100644--- a/locales/pt-BR/chat.json+++ b/locales/pt-BR/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} execuções","goalProcess.metricDetail.budget.saveFailed": "Não foi possível atualizar o orçamento","goalProcess.metricDetail.budget.title": "Orçamento",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} tokens","goalProcess.metricDetail.budget.totalCost": "Limite de custo total","goalProcess.metricDetail.budget.uncapped": "Sem limite","goalProcess.metricDetail.duration.now": "agora",diff --git a/locales/ru-RU/chat.json b/locales/ru-RU/chat.jsonindex 8afea3ac906..0414434eb86 100644--- a/locales/ru-RU/chat.json+++ b/locales/ru-RU/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} запусков","goalProcess.metricDetail.budget.saveFailed": "Не удалось обновить бюджет","goalProcess.metricDetail.budget.title": "Бюджет",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} токенов","goalProcess.metricDetail.budget.totalCost": "Общее ограничение затрат","goalProcess.metricDetail.budget.uncapped": "Без ограничений","goalProcess.metricDetail.duration.now": "сейчас",diff --git a/locales/tr-TR/chat.json b/locales/tr-TR/chat.jsonindex c241ab6af26..f669254ff15 100644--- a/locales/tr-TR/chat.json+++ b/locales/tr-TR/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} çalışma","goalProcess.metricDetail.budget.saveFailed": "Bütçe güncellenemedi","goalProcess.metricDetail.budget.title": "Bütçe",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} jeton","goalProcess.metricDetail.budget.totalCost": "Toplam maliyet sınırı","goalProcess.metricDetail.budget.uncapped": "Sınırsız","goalProcess.metricDetail.duration.now": "şimdi",diff --git a/locales/vi-VN/chat.json b/locales/vi-VN/chat.jsonindex 77cd42fd627..cc7deed8246 100644--- a/locales/vi-VN/chat.json+++ b/locales/vi-VN/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} lần chạy","goalProcess.metricDetail.budget.saveFailed": "Không thể cập nhật ngân sách","goalProcess.metricDetail.budget.title": "Ngân sách",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} token","goalProcess.metricDetail.budget.totalCost": "Giới hạn tổng chi phí","goalProcess.metricDetail.budget.uncapped": "Không giới hạn","goalProcess.metricDetail.duration.now": "bây giờ",diff --git a/locales/zh-CN/chat.json b/locales/zh-CN/chat.jsonindex 0a4eea76e5d..49071ddd1ff 100644--- a/locales/zh-CN/chat.json+++ b/locales/zh-CN/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} 次运行","goalProcess.metricDetail.budget.saveFailed": "预算更新失败","goalProcess.metricDetail.budget.title": "成本统计",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} tokens","goalProcess.metricDetail.budget.totalCost": "开销","goalProcess.metricDetail.budget.uncapped": "未设限","goalProcess.metricDetail.duration.now": "现在",diff --git a/locales/zh-TW/chat.json b/locales/zh-TW/chat.jsonindex 97c8a305c83..901a4085384 100644--- a/locales/zh-TW/chat.json+++ b/locales/zh-TW/chat.json@@ -963,7 +963,6 @@"goalProcess.metricDetail.budget.runsValue": "{{count}} 次執行","goalProcess.metricDetail.budget.saveFailed": "無法更新預算","goalProcess.metricDetail.budget.title": "預算",- "goalProcess.metricDetail.budget.tokensValue": "{{value}} 代幣","goalProcess.metricDetail.budget.totalCost": "總成本上限","goalProcess.metricDetail.budget.uncapped": "無上限","goalProcess.metricDetail.duration.now": "現在",diff --git a/packages/locales/src/default/chat.ts b/packages/locales/src/default/chat.tsindex 2ad7e2aa67e..13fa094baab 100644--- a/packages/locales/src/default/chat.ts+++ b/packages/locales/src/default/chat.ts@@ -2805,7 +2805,6 @@ export default {'goalProcess.metricDetail.budget.perTaskEmpty': 'No run has been billed yet.','goalProcess.metricDetail.budget.perTaskTitle': 'Where it went','goalProcess.metricDetail.budget.runsValue': '{{count}} runs',- 'goalProcess.metricDetail.budget.tokensValue': '{{value}} tokens','goalProcess.metricDetail.budget.raiseNote':'Raising a cap is what restarts a goal the coordinator parked on its budget \u2014 it starts moving again on its own, no Resume needed. Leave a field empty to run uncapped.','goalProcess.metricDetail.budget.rounds': 'Rounds',diff --git a/src/features/Portal/GoalMetric/Body.tsx b/src/features/Portal/GoalMetric/Body.tsxindex 5b21db8714d..304ee98b6b2 100644--- a/src/features/Portal/GoalMetric/Body.tsx+++ b/src/features/Portal/GoalMetric/Body.tsx@@ -31,6 +31,7 @@ import { useChatStore } from '@/store/chat';import { chatPortalSelectors } from '@/store/chat/selectors';import { goalSelectors, useGoalStore } from '@/store/goal';+import { costBreakdownColumns, costBreakdownRows } from './costBreakdownRows';import Lifecycle from './Lifecycle';import {metricRowTaskId,@@ -366,10 +367,6 @@ const BudgetField = memo<{BudgetField.displayName = 'GoalMetricBudgetField';-/** `12.4k` — token counts are read for magnitude, not audited digit by digit. */-const formatTokens = (value: number): string =>- value >= 1000 ? `${Math.round(value / 100) / 10}k` : String(value);-/*** Where the money went, one row per dispatched Task. The caps above answer* "how much is left"; without this the panel could not answer the question a@@ -382,16 +379,8 @@ const CostBreakdown = memo<{ graph: GoalGraphView; spend?: GoalSpend }>(({ graphconst { t } = useTranslation('chat');const openTaskDetail = useChatStore((s) => s.openTaskDetail);- const rows = useMemo(() => {- const nodeByTaskId = new Map(- graph.nodes- .filter((view) => view.node.taskId)- .map((view) => [view.node.taskId!, view] as const),- );- return (spend?.byTask ?? [])- .map((row) => ({ ...row, view: nodeByTaskId.get(row.taskId) }))- .sort((a, b) => b.totalCost - a.totalCost || b.totalTokens - a.totalTokens);- }, [graph, spend]);+ const rows = useMemo(() => costBreakdownRows(graph, spend), [graph, spend]);+ const columns = useMemo(() => costBreakdownColumns(rows), [rows]);if (rows.length === 0)return (@@ -423,15 +412,17 @@ const CostBreakdown = memo<{ graph: GoalGraphView; spend?: GoalSpend }>(({ graph<TextclassName={styles.mono}fontSize={12}- style={{ flex: 'none' }}+ style={{ flex: 'none', textAlign: 'right', width: columns.tokens }}type={'secondary'}>- {t('goalProcess.metricDetail.budget.tokensValue', {- value: formatTokens(row.totalTokens),- })}+ {row.tokensLabel}</Text>- <Text className={styles.mono} style={{ flex: 'none' }} weight={500}>- {formatUsd(row.totalCost)}+ <Text+ className={styles.mono}+ style={{ flex: 'none', textAlign: 'right', width: columns.cost }}+ weight={500}+ >+ {row.costLabel}</Text></Flexbox>);diff --git a/src/features/Portal/GoalMetric/costBreakdownRows.test.ts b/src/features/Portal/GoalMetric/costBreakdownRows.test.tsnew file mode 100644index 00000000000..badc8e0afc2--- /dev/null+++ b/src/features/Portal/GoalMetric/costBreakdownRows.test.ts@@ -0,0 +1,65 @@+import type { GoalSpend } from '@lobechat/types';+import { describe, expect, it } from 'vitest';++import type { GoalGraphView } from '@/features/AgentGoals/ProcessControl/goalGraphViewModel';++import { costBreakdownColumns, costBreakdownRows } from './costBreakdownRows';++const graph = (taskIds: string[]) =>+ ({+ nodes: taskIds.map((taskId, index) => ({ node: { taskId, title: `Task ${index}` } })),+ }) as unknown as GoalGraphView;++const spend = (byTask: GoalSpend['byTask']) =>+ ({ byTask, runs: byTask.length, totalCost: 0, totalTokens: 0 }) as GoalSpend;++describe('costBreakdownRows', () => {+ // Regression: a local helper only ever divided by 1000, so a Task that used+ // 98,445,300 tokens read "98445.3k tokens".+ it('climbs the token units K → M → B', () => {+ const rows = costBreakdownRows(+ graph(['a', 'b', 'c', 'd']),+ spend([+ { runs: 1, taskId: 'a', totalCost: 29.25, totalTokens: 98_445_300 },+ { runs: 1, taskId: 'b', totalCost: 3, totalTokens: 12_400 },+ { runs: 1, taskId: 'c', totalCost: 2, totalTokens: 1_250_000_000 },+ { runs: 1, taskId: 'd', totalCost: 1, totalTokens: 640 },+ ]),+ );++ expect(rows.map((row) => row.tokensLabel)).toEqual(['98.4M', '12.4K', '1.3B', '640']);+ });++ it('orders by spend, then tokens, and keeps unsettled Tasks at $0', () => {+ const rows = costBreakdownRows(+ graph(['a', 'b']),+ spend([+ { runs: 1, taskId: 'unsettled', totalCost: 0, totalTokens: 82_027_400 },+ { runs: 1, taskId: 'b', totalCost: 11.03, totalTokens: 31_323_400 },+ { runs: 1, taskId: 'a', totalCost: 0, totalTokens: 20_815_400 },+ ]),+ );++ expect(rows.map((row) => row.taskId)).toEqual(['b', 'unsettled', 'a']);+ expect(rows.find((row) => row.taskId === 'unsettled')?.view).toBeUndefined();+ expect(rows.find((row) => row.taskId === 'a')?.view?.node.title).toBe('Task 0');+ });+});++describe('costBreakdownColumns', () => {+ // Regression: each figure hugged its own text, so `12.4K` sat a character+ // off from `98.4M` and a wider cost shifted its row's tokens left.+ it('sizes each column to its longest value so every row right-aligns', () => {+ const rows = costBreakdownRows(+ graph(['a', 'b', 'c']),+ spend([+ { runs: 1, taskId: 'a', totalCost: 118.31, totalTokens: 98_445_300 },+ { runs: 1, taskId: 'b', totalCost: 0.38, totalTokens: 12_400 },+ { runs: 1, taskId: 'c', totalCost: 0, totalTokens: 640 },+ ]),+ );++ expect(rows.map((row) => row.costLabel)).toEqual(['$118.31', '$0.38', '$0']);+ expect(costBreakdownColumns(rows)).toEqual({ cost: '7ch', tokens: '5ch' });+ });+});diff --git a/src/features/Portal/GoalMetric/costBreakdownRows.ts b/src/features/Portal/GoalMetric/costBreakdownRows.tsnew file mode 100644index 00000000000..45fe7170663--- /dev/null+++ b/src/features/Portal/GoalMetric/costBreakdownRows.ts@@ -0,0 +1,37 @@+import type { GoalSpend } from '@lobechat/types';+import { formatUsageValue } from '@lobechat/utils';++import { formatUsd } from '@/features/AgentGoals/goalPresentation';+import type { GoalGraphView } from '@/features/AgentGoals/ProcessControl/goalGraphViewModel';++/**+ * The per-Task rows of the budget panel, ordered by spend. A Task whose runs+ * have not settled keeps its row at $0: it is still consuming rounds. Token+ * counts are read for magnitude, so they climb K → M → B like every other+ * token count in the product.+ */+export const costBreakdownRows = (graph: GoalGraphView, spend?: GoalSpend) => {+ const nodeByTaskId = new Map(+ graph.nodes+ .filter((view) => view.node.taskId)+ .map((view) => [view.node.taskId!, view] as const),+ );+ return (spend?.byTask ?? [])+ .map((row) => ({+ ...row,+ costLabel: formatUsd(row.totalCost),+ tokensLabel: formatUsageValue(row.totalTokens),+ view: nodeByTaskId.get(row.taskId),+ }))+ .sort((a, b) => b.totalCost - a.totalCost || b.totalTokens - a.totalTokens);+};++/**+ * The widths of the token and cost columns. Both figures are monospace, so a+ * column as wide as its longest value in `ch` right-aligns every row: `12.4K`+ * sits under `98.4M`, and a `$118.31` row does not push its tokens left.+ */+export const costBreakdownColumns = (rows: ReturnType<typeof costBreakdownRows>) => ({+ cost: `${Math.max(0, ...rows.map((row) => row.costLabel.length))}ch`,+ tokens: `${Math.max(0, ...rows.map((row) => row.tokensLabel.length))}ch`,+});