Skip to content

The agent that grows with you

fix(webapp): keep per-profile usage totals on the host's profile groups

Tekniumf2703ac57813
diff --git a/apps/desktop/src/app/chat/sidebar/gateway-groups-webapp.test.tsx b/apps/desktop/src/app/chat/sidebar/gateway-groups-webapp.test.tsx
index b5f958ca210..a0cdf53ae01 100644
--- a/apps/desktop/src/app/chat/sidebar/gateway-groups-webapp.test.tsx
+++ b/apps/desktop/src/app/chat/sidebar/gateway-groups-webapp.test.tsx
@@ -5,9 +5,9 @@ import { afterEach, expect, it } from 'vitest'
import { SidebarProvider } from '@/components/ui/sidebar'
import { $connectionsRegistry } from '@/store/connection-registry-state'
-import { setSidebarGrouping } from '@/store/layout'
+import { $sidebarRowMeta, setSidebarGrouping } from '@/store/layout'
import { $profiles, $showAllProfiles } from '@/store/profile'
-import { $sessions } from '@/store/session'
+import { $sessionProfilesUsage, $sessions } from '@/store/session'
import { makeSessionInfo } from '@/test/session-info'
import { ChatSidebar } from './index'
@@ -40,12 +40,16 @@ const mount = () =>
afterEach(() => {
cleanup()
+ delete window.document.documentElement.dataset.hermesDesktopHost
$showAllProfiles.set(false)
+ $sidebarRowMeta.set([])
+ $sessionProfilesUsage.set({})
$sessions.set([])
})
// The Webapp publishes no connection registry: its one gateway is the `local` source.
it('names the Webapp gateway section after the host instead of its raw `local` id', () => {
+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'
mount()
act(() => {
$connectionsRegistry.set(null)
@@ -55,6 +59,8 @@ it('names the Webapp gateway section after the host instead of its raw `local` i
] as typeof $profiles.value)
$showAllProfiles.set(true)
setSidebarGrouping('profile')
+ $sidebarRowMeta.set(['cost', 'tokens'])
+ $sessionProfilesUsage.set({ default: { cost_usd: 3, tokens: 100 }, work: { cost_usd: 1.25, tokens: 40 } })
$sessions.set(
[
['listed', 'default'],
@@ -66,7 +72,7 @@ it('names the Webapp gateway section after the host instead of its raw `local` i
)
})
- const sections = document.querySelectorAll('[data-gateway-section]')
+ const sections = window.document.querySelectorAll('[data-gateway-section]')
expect(sections).toHaveLength(1)
const gateway = sections[0] as HTMLElement
expect(within(gateway).getByText('Hermes host')).toBeTruthy()
@@ -75,4 +81,11 @@ it('names the Webapp gateway section after the host instead of its raw `local` i
expect(within(gateway).getByText('listed title')).toBeTruthy()
expect(within(gateway).getByText('new-chat title')).toBeTruthy()
expect(within(gateway).getByText('work-chat title')).toBeTruthy()
+
+ // The serving host's per-profile totals belong to its `local` profile groups.
+ const groupOf = (text: RegExp) =>
+ screen.getByText(text).closest('[data-gateway-group]')?.getAttribute('data-gateway-group')
+
+ expect(groupOf(/\$3\.00/)).toBe(JSON.stringify(['local', 'default']))
+ expect(groupOf(/\$1\.25/)).toBe(JSON.stringify(['local', 'work']))
})
diff --git a/apps/desktop/src/app/chat/sidebar/gateway-groups.tsx b/apps/desktop/src/app/chat/sidebar/gateway-groups.tsx
index 70df2002a24..02e9847fbab 100644
--- a/apps/desktop/src/app/chat/sidebar/gateway-groups.tsx
+++ b/apps/desktop/src/app/chat/sidebar/gateway-groups.tsx
@@ -1,5 +1,6 @@
import type { useSensors } from '@dnd-kit/core'
import { arrayMove } from '@dnd-kit/sortable'
+import { LOCAL_CONNECTION_ID } from '@hermes/shared'
import { useStore } from '@nanostores/react'
import type { ReactNode } from 'react'
import { useMemo, useState } from 'react'
@@ -23,6 +24,7 @@ import { useContributions } from '@/contrib'
import { ContribBoundary, ContribRender } from '@/contrib/react/boundary'
import type { SessionInfo } from '@/hermes'
import { useI18n } from '@/i18n'
+import { isBrowserHostedDesktop } from '@/lib/platform'
import { useStoreSelector } from '@/lib/use-session-slice'
import { cn } from '@/lib/utils'
import { $connectionsRegistry } from '@/store/connection-registry-state'
@@ -166,9 +168,13 @@ function GatewayProfileGroup({
// Legacy totals are keyed only by profile. Never attribute those figures to
// a registry gateway that happens to expose the same profile name, or to
- // one messaging platform.
+ // one messaging platform. A Webapp tab's `local` rows are the serving host's
+ // own, the gateway those totals were read from.
+ const ownsProfileTotals =
+ !group.connectionId || (isBrowserHostedDesktop() && group.connectionId === LOCAL_CONNECTION_ID)
+
const usage = useStoreSelector($sessionProfilesUsage, all =>
- group.connectionId || embedded ? undefined : all[group.profile!]
+ ownsProfileTotals && !embedded ? all[group.profile!] : undefined
)
const [renaming, setRenaming] = useState(false)