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(devices): keep name helpers independent of React initialization

Chris2b933fa5819e
diff --git a/apps/desktop/src/renderer/__tests__/remoteAgentRelocationWiring.test.ts b/apps/desktop/src/renderer/__tests__/remoteAgentRelocationWiring.test.ts
index d46a184951..bbcae56b03 100644
--- a/apps/desktop/src/renderer/__tests__/remoteAgentRelocationWiring.test.ts
+++ b/apps/desktop/src/renderer/__tests__/remoteAgentRelocationWiring.test.ts
@@ -148,7 +148,7 @@ describe('被控电脑上的任务:换 Agent 所在电脑', () => {
});
it('改回被控电脑的确认标题写那台电脑的名字', () => {
- expect(chatInputSource).toContain('device: deviceLinkDeviceName || deviceLinkDeviceId,');
+ expect(chatInputSource).toContain('device: deviceDisplayName(deviceLinkDeviceId, deviceLinkDeviceName),');
});
it('会话视图:候选电脑去掉被控电脑本身,并入全部可用分享;目录跟随 Agent 所在电脑', () => {
diff --git a/apps/desktop/src/renderer/lib/deviceName.ts b/apps/desktop/src/renderer/lib/deviceName.ts
index 968453533b..e5f3fb56f8 100644
--- a/apps/desktop/src/renderer/lib/deviceName.ts
+++ b/apps/desktop/src/renderer/lib/deviceName.ts
@@ -1,6 +1,11 @@
import { deviceDisplayName as displayName, type DeviceNameSource } from '@cindy/maker-shared/device-list';
-import i18n from '@/i18n';
+// The renderer initializes this singleton; display helpers do not initialize React bindings.
+import i18n from 'i18next';
+import enCommon from '@/i18n/locales/en/common.json';
export function deviceDisplayName(deviceId: string, name?: string | null, devices: readonly DeviceNameSource[] = []): string {
- return displayName(deviceId, name, i18n.t('bots.cloud.unknownDevice'), devices);
+ const unnamed = i18n.isInitialized
+ ? i18n.t('bots.cloud.unknownDevice', { defaultValue: enCommon.bots.cloud.unknownDevice })
+ : enCommon.bots.cloud.unknownDevice;
+ return displayName(deviceId, name, unnamed, devices);
}
diff --git a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
index db6397d75b..537fbe0f0e 100644
--- a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
+++ b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
@@ -938,7 +938,7 @@ it('opens only the target computer desktop without resolving or authorizing the
await click('interaction.pluginSetup.remoteDesktop');
await click('interaction.pluginSetup.remoteDesktop');
expect(push).toHaveBeenCalledTimes(1);
- expect(push).toHaveBeenLastCalledWith({ pathname: '/devices/desktop/[deviceId]', params: { deviceId: 'd1', deviceName: 'd1' } });
+ expect(push).toHaveBeenLastCalledWith({ pathname: '/devices/desktop/[deviceId]', params: { deviceId: 'd1', deviceName: 'another computer' } });
expect(resolveInteraction).not.toHaveBeenCalled(); expect(invoke).not.toHaveBeenCalled();
expect(host.querySelector('input')).toBeNull();
forwardNavigationLock.reset();
diff --git a/apps/mobile/src/__tests__/mobileGoalCommands.test.ts b/apps/mobile/src/__tests__/mobileGoalCommands.test.ts
index cdf1bfb9d3..0459174646 100644
--- a/apps/mobile/src/__tests__/mobileGoalCommands.test.ts
+++ b/apps/mobile/src/__tests__/mobileGoalCommands.test.ts
@@ -10,6 +10,7 @@ import { composerDocumentProjectedText, emptyComposerDocument, normalizeComposer
import { captureNewSessionOwner } from '@/session/newSessionForm';
import { reconcileNewSessionSubmitDraft } from '@/session/newSessionRuntimeReconciliation';
import { getMobileAuthOwner, isMobileAuthOwnerCurrent, setMobileAuthOwner } from '@/auth/authOwnerGeneration';
+import { deviceDisplayName } from '@/device-link/deviceName';
// Execute the actual page callbacks, excluding unrelated native/network UI.
function callback(path: string, name: string) {
@@ -47,6 +48,7 @@ function newHarness(text = '/goal target') {
const noop = vi.fn();
const draft = { agentKind: 'pi', model: 'test-model', providerId: 'test-provider', workspaceKind: 'dialogue', workingDir: '', firstMessage: text, agentDeviceId: 'agent-device' };
const bindings = {
+ deviceDisplayName,
parseGoalCommand, parseMobileDesktopCommand,
firstMessageRef: { current: text }, slashCommands: [], pendingGoalSkillRef: { current: null as string | null },
creatingRef: { current: false }, voiceIsBusy: false, attachmentsRef: { current: [] as unknown[] }, pendingUploads: [] as unknown[],
diff --git a/apps/mobile/src/__tests__/remoteTaskSuggestions.test.ts b/apps/mobile/src/__tests__/remoteTaskSuggestions.test.ts
index 49112d8545..f44f1e6f7e 100644
--- a/apps/mobile/src/__tests__/remoteTaskSuggestions.test.ts
+++ b/apps/mobile/src/__tests__/remoteTaskSuggestions.test.ts
@@ -9,6 +9,7 @@ import {
} from "@/session/remoteTaskSuggestionsModel";
import type { MobileHomePresentation } from "@/session/mobileHome";
import type { RemoteSessionListItem } from "@/session/sessionList";
+import { deviceDisplayName } from "@/device-link/deviceName";
// Source-fragment assertions must also work with Windows CRLF checkouts.
const readTextLf = (...args: Parameters<typeof readFileSync>): string =>
@@ -145,7 +146,7 @@ describe("recommendation route target", () => {
expect(suggestedBody).toBeTruthy();
expect(source.match(/onSelect=\{openSuggestedSession\}/g)).toHaveLength(2);
expect(source.match(/onNewSession=\{\(\) => openSuggestedSession\(\)\}/g)).toHaveLength(2);
- const open = new Function("project", "suggestion", "explicitDeviceId", "guardedPush", `
+ const open = new Function("project", "suggestion", "explicitDeviceId", "guardedPush", "deviceDisplayName", `
const newSessionTargetRef = { current: {
primaryDevice: { deviceId: 'fallback', label: 'Fallback' },
newSessionDeviceOptions: [{ deviceId: 'checked', name: 'Checked' }],
@@ -162,7 +163,7 @@ describe("recommendation route target", () => {
for (const suggestion of [undefined, "findFile"]) {
let route: { params: Record<string, string> } | undefined;
suggested(suggestion, (project: unknown, id: unknown, deviceId: unknown) =>
- open(project, id, deviceId, (value: typeof route) => { route = value; }), "checked");
+ open(project, id, deviceId, (value: typeof route) => { route = value; }, deviceDisplayName), "checked");
expect(route?.params).toMatchObject({ deviceId: "checked", deviceName: "Checked", deviceExplicit: "1" });
expect(route?.params.suggestion).toBe(suggestion);
}
diff --git a/apps/mobile/src/__tests__/sideChatTaskBoundary.test.tsx b/apps/mobile/src/__tests__/sideChatTaskBoundary.test.tsx
index 481b6ff5a8..1ea3a7f98b 100644
--- a/apps/mobile/src/__tests__/sideChatTaskBoundary.test.tsx
+++ b/apps/mobile/src/__tests__/sideChatTaskBoundary.test.tsx
@@ -221,7 +221,7 @@ describe('side chat task lifecycle', () => {
state.route = { deviceId: 'host', sessionId: 'old', resourceHistorySessionId: 'old', resourceId: 'bot', resourceKind: 'bot', focusClientId: 'message' };
act(() => root.render(<SessionTaskLayout>{() => <textarea aria-label="Legacy history" />}</SessionTaskLayout>));
expect(host.querySelector('textarea')).toBeNull();
- expect(state.replaceParams).toHaveBeenCalledWith({ deviceId: 'host', deviceName: 'host', sessionId: 'old', focusClientId: 'message' });
+ expect(state.replaceParams).toHaveBeenCalledWith({ deviceId: 'host', deviceName: 'another computer', sessionId: 'old', focusClientId: 'message' });
});
// #7105: a same-ID search hit must exit the companion resource skin, whereas
@@ -235,7 +235,7 @@ describe('side chat task lifecycle', () => {
} as unknown as RemoteSessionListItem;
act(() => state.sidebar!.onSelectSession!(target));
if (search) expect(state.replaceParams).toHaveBeenCalledExactlyOnceWith({
- deviceId: 'host', deviceName: 'host', sessionId: 'child',
+ deviceId: 'host', deviceName: 'another computer', sessionId: 'child',
});
else expect(state.replaceParams).not.toHaveBeenCalled();
});
diff --git a/apps/mobile/src/__tests__/useTeammateRoster.test.tsx b/apps/mobile/src/__tests__/useTeammateRoster.test.tsx
index eba11cb84e..818fee09b4 100644
--- a/apps/mobile/src/__tests__/useTeammateRoster.test.tsx
+++ b/apps/mobile/src/__tests__/useTeammateRoster.test.tsx
@@ -38,7 +38,7 @@ afterEach(() => { act(() => root?.unmount()); root = undefined; vi.useRealTimers
it('offers only real online hosts advertising create, including a host with an empty roster', async () => {
await render();
expect(result.targets.map(host => host.deviceId)).toEqual(['old', 'new']);
- expect(result.createTargets).toEqual([{ deviceId: 'new', deviceName: 'new' }]);
+ expect(result.createTargets).toEqual([{ deviceId: 'new', deviceName: 'another computer (1)' }]);
expect(h.link.invoke).toHaveBeenCalledTimes(2); // No extra capability probe or mutation.
expect(result.authoritative).toBe(true);
h.list.isOnline.mockReturnValue(false); await render(); expect(result.createTargets).toEqual([]);
@@ -137,7 +137,7 @@ it('exposes only computers whose manifest advertises group chats, keeping them t
] });
h.link.invoke.mockImplementation(async (deviceId) => manifest(deviceId === 'new'));
await render();
- expect(result.groupTargets).toEqual([{ deviceId: 'new', deviceName: 'new' }]);
+ expect(result.groupTargets).toEqual([{ deviceId: 'new', deviceName: 'another computer (1)' }]);
// A transient manifest failure keeps the computer's groups; an older desktop never gains them.
h.link.invoke.mockImplementation(async (deviceId) => { if (deviceId === 'new') throw new Error('timeout'); return manifest(false); });
h.link.connectionEpoch++; await render();
diff --git a/apps/mobile/src/device-link/deviceName.ts b/apps/mobile/src/device-link/deviceName.ts
index 4538a96bb5..7cb0f511bf 100644
--- a/apps/mobile/src/device-link/deviceName.ts
+++ b/apps/mobile/src/device-link/deviceName.ts
@@ -1,8 +1,13 @@
import { deviceDisplayName as displayName, type DeviceNameSource } from '@cindy/maker-shared/device-list';
-import i18n from '@/i18n';
+// The app initializes this singleton; plain name consumers must not load React bindings.
+import i18n from 'i18next';
+import enDevices from '@/i18n/locales/en/devices.json';
export const MAX_DEVICE_NAME_LENGTH = 64;
export function deviceDisplayName(deviceId: string, name?: string | null, devices: readonly DeviceNameSource[] = []): string {
- return displayName(deviceId, name, i18n.t('devices.cloudCompanion.unknownDevice'), devices);
+ const unnamed = i18n.isInitialized
+ ? i18n.t('devices.cloudCompanion.unknownDevice', { defaultValue: enDevices.cloudCompanion.unknownDevice })
+ : enDevices.cloudCompanion.unknownDevice;
+ return displayName(deviceId, name, unnamed, devices);
}