Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(bots): 统一设备菜单与侧栏的离线提示判据

Dash8f31ac4aa4c1
diff --git a/apps/desktop/src/renderer/features/bots/BotConnectionStatus.tsx b/apps/desktop/src/renderer/features/bots/BotConnectionStatus.tsx
index e5445d524d..12aa72c312 100644
--- a/apps/desktop/src/renderer/features/bots/BotConnectionStatus.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotConnectionStatus.tsx
@@ -1,14 +1,21 @@
import { useTranslation } from 'react-i18next';
import { cn } from '@/lib/utils';
+/** Cached/unknown connectivity must never be presented as offline. */
+export function isBotKnownOffline(online?: boolean | null, connectionKnown?: boolean): boolean {
+ return online === false && connectionKnown !== false;
+}
+
export function BotConnectionStatus({
online = true,
+ connectionKnown,
deviceName,
inline = false,
homeOffline = false,
className,
}: {
online?: boolean | null;
+ connectionKnown?: boolean;
/** The device is this companion's main computer (cloud companions): say so when offline. */
homeOffline?: boolean;
deviceName?: string;
@@ -16,7 +23,7 @@ export function BotConnectionStatus({
className?: string;
}) {
const { t } = useTranslation();
- const offline = online === false;
+ const offline = isBotKnownOffline(online, connectionKnown);
const status = offline ? t(homeOffline ? 'bots.cloud.homeOfflineShort' : 'bots.remote.offline') : '';
const name = deviceName?.trim();
const label = [status, name].filter(Boolean).join(' · ');
diff --git a/apps/desktop/src/renderer/features/bots/CindyDevicePicker.tsx b/apps/desktop/src/renderer/features/bots/CindyDevicePicker.tsx
index 08fd603537..6728ec2aa8 100644
--- a/apps/desktop/src/renderer/features/bots/CindyDevicePicker.tsx
+++ b/apps/desktop/src/renderer/features/bots/CindyDevicePicker.tsx
@@ -9,6 +9,7 @@ import { useCompanionCloud } from './companionCloudStore';
import { cloudRemoteBots, withoutShadowedBots } from './companionCloudPresentation';
import { cindyDeviceKey, cindyDeviceOptions, type CindyDeviceOption } from './cindyDeviceRoster';
import { formatBotUnreadBadge } from './botListDisplay';
+import { isBotKnownOffline } from './BotConnectionStatus';
import type { BotChatIdentity } from './BotSessionContentHeader';
/** Shared device menu for the single sidebar entry and the chat header. */
@@ -32,37 +33,50 @@ export function CindyDevicePicker({
truncateOptions
className={cn('h-6 w-fit max-w-full px-2 text-11', className)}
contentClassName="w-64 max-w-[var(--radix-select-content-available-width)]"
- triggerAdornment={otherUnread ? (
- <span
- aria-label={t('bots.devicePicker.otherUnread')}
- className="size-1.5 shrink-0 rounded-full bg-[var(--bot-unread-bg)]"
- />
- ) : null}
- options={options.map((option) => ({
- value: option.key,
- label: option.label,
- title: option.deviceName,
- endAdornment: option.unread || !option.online ? (
- <>
- {!option.online ? <span className="text-11 text-[var(--text-secondary)]">{t('deviceId' in option.bot && option.bot.connectionKnown === false ? 'bots.remote.unknown' : 'bots.remote.offline')}</span> : null}
- {option.unread ? (
- option.unreadCount ? (
- <span
- aria-label={t('bots.list.unread', { count: option.unreadCount })}
- className="flex h-4 min-w-4 shrink-0 items-center justify-center rounded-full bg-[var(--bot-unread-bg)] px-1 text-10 text-[var(--bot-unread-fg)]"
- >
- {formatBotUnreadBadge(option.unreadCount)}
- </span>
- ) : (
- <span
- aria-label={t('bots.devicePicker.unread')}
- className="size-1.5 shrink-0 rounded-full bg-[var(--bot-unread-bg)]"
- />
- )
- ) : null}
- </>
- ) : undefined,
- }))}
+ triggerAdornment={
+ otherUnread ? (
+ <span
+ aria-label={t('bots.devicePicker.otherUnread')}
+ className="size-1.5 shrink-0 rounded-full bg-[var(--bot-unread-bg)]"
+ />
+ ) : null
+ }
+ options={options.map((option) => {
+ const offline = isBotKnownOffline(
+ option.online,
+ 'deviceId' in option.bot ? option.bot.connectionKnown : undefined,
+ );
+ return {
+ value: option.key,
+ label: option.label,
+ title: option.deviceName,
+ endAdornment:
+ option.unread || offline ? (
+ <>
+ {offline ? (
+ <span className="text-11 text-[var(--text-secondary)]">
+ {t('bots.remote.offline')}
+ </span>
+ ) : null}
+ {option.unread ? (
+ option.unreadCount ? (
+ <span
+ aria-label={t('bots.list.unread', { count: option.unreadCount })}
+ className="flex h-4 min-w-4 shrink-0 items-center justify-center rounded-full bg-[var(--bot-unread-bg)] px-1 text-10 text-[var(--bot-unread-fg)]"
+ >
+ {formatBotUnreadBadge(option.unreadCount)}
+ </span>
+ ) : (
+ <span
+ aria-label={t('bots.devicePicker.unread')}
+ className="size-1.5 shrink-0 rounded-full bg-[var(--bot-unread-bg)]"
+ />
+ )
+ ) : null}
+ </>
+ ) : undefined,
+ };
+ })}
onValueChange={(key) => {
const option = options.find((candidate) => candidate.key === key);
if (option && option.key !== current.key) onSelect(option);
diff --git a/apps/desktop/src/renderer/features/bots/CindyDeviceRow.tsx b/apps/desktop/src/renderer/features/bots/CindyDeviceRow.tsx
index 99bbe68584..641c39c025 100644
--- a/apps/desktop/src/renderer/features/bots/CindyDeviceRow.tsx
+++ b/apps/desktop/src/renderer/features/bots/CindyDeviceRow.tsx
@@ -59,7 +59,8 @@ export function CindyDeviceRow({
<span className="pointer-events-none relative flex shrink-0">
<BotAvatar bot={bot} size="md" />
<BotConnectionStatus
- online={'deviceId' in bot && bot.connectionKnown === false ? null : current.online}
+ online={current.online}
+ connectionKnown={'deviceId' in bot ? bot.connectionKnown : undefined}
deviceName={current.label}
/>
</span>
diff --git a/apps/desktop/src/renderer/features/bots/TeammatesSection.tsx b/apps/desktop/src/renderer/features/bots/TeammatesSection.tsx
index b2b59ec6d3..96843be908 100644
--- a/apps/desktop/src/renderer/features/bots/TeammatesSection.tsx
+++ b/apps/desktop/src/renderer/features/bots/TeammatesSection.tsx
@@ -617,11 +617,11 @@ export function TeammatesSection() {
<button key={remoteBotKey(bot)} type="button" aria-current={selected ? 'page' : undefined}
onClick={() => navigate(`/bots/remote/${encodeURIComponent(bot.deviceId)}/${encodeURIComponent(bot.id)}`)}
className={cn('flex w-full min-w-0 items-center gap-2.5 rounded-xl px-2.5 py-2 text-left outline-none focus-visible:ring-2 focus-visible:ring-ring', selected ? 'bg-sidebar-item-active text-sidebar-item-active-foreground' : 'text-[var(--sidebar-nav-text)] hover:bg-sidebar-item-hover')}>
- <span className="relative shrink-0"><BotAvatar bot={bot} size="md" /><BotConnectionStatus online={bot.connectionKnown === false ? null : bot.online} deviceName={deviceName} homeOffline={isCloudHomeOffline(cloud, bot)} /></span>
+ <span className="relative shrink-0"><BotAvatar bot={bot} size="md" /><BotConnectionStatus online={bot.online} connectionKnown={bot.connectionKnown} deviceName={deviceName} homeOffline={isCloudHomeOffline(cloud, bot)} /></span>
<span className="flex min-w-0 flex-1 flex-col gap-0.5">
<span className="flex min-w-0 items-baseline gap-2">
<span className="min-w-0 truncate text-14 leading-5">{bot.name}</span>
- <BotConnectionStatus inline online={bot.connectionKnown === false ? null : bot.online} deviceName={deviceName} homeOffline={isCloudHomeOffline(cloud, bot)} className={selected ? 'opacity-70' : 'text-[var(--sidebar-list-muted)]'} />
+ <BotConnectionStatus inline online={bot.online} connectionKnown={bot.connectionKnown} deviceName={deviceName} homeOffline={isCloudHomeOffline(cloud, bot)} className={selected ? 'opacity-70' : 'text-[var(--sidebar-list-muted)]'} />
</span>
<span className={cn('truncate text-12 leading-4', selected ? 'opacity-70' : 'text-[var(--sidebar-list-muted)]')}>{bot.online && bot.generation ? <BotGenerationLabel sessionId={bot.sessionId ?? undefined} {...bot.generation} remote={{ deviceId: bot.deviceId, botId: bot.id }} /> : bot.preview || bot.description || t('bots.list.startChat')}</span>
</span>
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/CindyDevicePicker.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/CindyDevicePicker.test.tsx
index 553fd09580..b822e7f800 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/CindyDevicePicker.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/CindyDevicePicker.test.tsx
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
-import { cleanup, fireEvent, render, screen } from '@testing-library/react';
+import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';
import { createInstance } from 'i18next';
import { I18nextProvider } from 'react-i18next';
import { afterAll, beforeAll, afterEach, describe, expect, it, vi } from 'vitest';
@@ -18,7 +18,7 @@ vi.mock('@/features/device-link/useDeviceLinkDeviceList', () => ({
import { CindyDevicePicker } from '../CindyDevicePicker';
import { CindyDeviceRow } from '../CindyDeviceRow';
-import { cindyDeviceOptions } from '../cindyDeviceRoster';
+import { cindyDeviceOptions, type CindyDeviceOption } from '../cindyDeviceRoster';
import type { BotProfile } from '../botStore';
const originalScrollIntoView = HTMLElement.prototype.scrollIntoView;
@@ -31,6 +31,107 @@ afterAll(() => {
afterEach(cleanup);
+it.each([
+ ['row', 0],
+ ['row', 1],
+ ['row', 5],
+ ['menu', 0],
+ ['menu', 1],
+ ['menu', 5],
+] as const)(
+ 'keeps connectivity separate from unread through updates (%s, unread=%s)',
+ async (surface, unreadCount) => {
+ const i18n = createInstance();
+ await i18n.init({ lng: 'en', resources: { en: { translation: en } } });
+ const onSelect = vi.fn();
+ const makeOptions = (online: boolean, connectionKnown?: boolean): CindyDeviceOption[] => {
+ const options = cindyDeviceOptions(
+ [{ id: 'cindy-default', name: 'Cindy', status: 'active' } as BotProfile],
+ [
+ {
+ id: 'cindy-default',
+ deviceId: 'remote',
+ deviceName: 'Mac mini',
+ name: 'Cindy',
+ avatar: '',
+ avatarColor: '',
+ description: '',
+ preview: '',
+ activityAt: 0,
+ sessionId: 'remote-chat',
+ online,
+ connectionKnown,
+ lastReplyAt: 20,
+ readAt: 10,
+ },
+ ],
+ [],
+ {},
+ 'This Device',
+ );
+ options[1] = {
+ ...options[1],
+ unread: unreadCount > 0,
+ unreadCount: unreadCount > 1 ? unreadCount : undefined,
+ };
+ return options;
+ };
+ const renderSurface = (options: CindyDeviceOption[]) => (
+ <I18nextProvider i18n={i18n}>
+ {surface === 'row' ? (
+ <CindyDeviceRow
+ current={options[1]}
+ options={options}
+ selected={false}
+ subtitle="Ready"
+ timestamp=""
+ onOpen={vi.fn()}
+ onSelect={onSelect}
+ />
+ ) : (
+ <CindyDevicePicker current={options[1]} options={options} onSelect={onSelect} />
+ )}
+ </I18nextProvider>
+ );
+ const view = render(renderSurface(makeOptions(false, false)));
+ fireEvent.keyDown(screen.getByRole('combobox'), { key: 'ArrowDown' });
+ await screen.findByRole('option', { name: /Mac mini/ });
+ // Cached unknown → known offline → online → unknown (including cached online)
+ // → legacy offline → unknown again, without closing the shared device menu.
+ for (const [online, connectionKnown, offline] of [
+ [false, false, false],
+ [false, true, true],
+ [true, true, false],
+ [true, false, false],
+ [false, undefined, true],
+ [false, false, false],
+ ] as const) {
+ view.rerender(renderSurface(makeOptions(online, connectionKnown)));
+ const option = screen.getByRole('option', { name: /Mac mini/ });
+ expect(within(option).queryByText(en.bots.remote.offline) !== null).toBe(offline);
+ expect(screen.queryByText(en.bots.remote.unknown)).toBeNull();
+ expect(screen.queryByText(en.bots.remote.online)).toBeNull();
+ if (surface === 'row') {
+ expect(
+ view.container.querySelector(`span[title="${en.bots.remote.offline} · Mac mini"]`) !==
+ null,
+ ).toBe(offline);
+ }
+ if (unreadCount > 0) {
+ const label =
+ unreadCount > 1
+ ? i18n.t('bots.list.unread', { count: unreadCount })
+ : en.bots.devicePicker.unread;
+ expect(within(option).getByLabelText(label)).toBeTruthy();
+ } else {
+ expect(within(option).queryByLabelText(en.bots.devicePicker.unread)).toBeNull();
+ }
+ }
+ fireEvent.click(screen.getByRole('option', { name: /This Device/ }));
+ expect(onSelect).toHaveBeenCalledWith(expect.objectContaining({ label: 'This Device' }));
+ },
+);
+
it.each([true, false])(
'keeps the sidebar device picker on its row surface (selected=%s)',
async (selected) => {