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(plugins): follow server order in plugin catalog (#566)

xxxcc51fc2e5ebf1e
diff --git a/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx b/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx
index 52e9c05647..62a5f7100b 100644
--- a/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx
+++ b/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx
@@ -69,6 +69,7 @@ import { GhostPluginIcon } from './GhostPluginIcon';
import { MarketPluginDetailView } from './MarketPluginDetailView';
import { PluginScopePicker, usePluginRecentWorkdirs } from './PluginScopePicker';
import {
+ orderPluginCatalogItems,
pluginPresentationOrigin,
type PluginPresentationOrigin,
} from './lib/pluginMarketPresentation';
@@ -363,6 +364,10 @@ export function GhostPluginPage() {
),
[effectiveOriginFilter, searchedAvailableMarketItems],
);
+ const catalogItems = useMemo(
+ () => orderPluginCatalogItems(marketItems, items, availableMarketItems),
+ [availableMarketItems, items, marketItems],
+ );
const originCounts = useMemo(() => {
const counts: Record<PluginPresentationOrigin, number> = {
public: 0,
@@ -985,34 +990,40 @@ export function GhostPluginPage() {
/>
) : null}
- {items.length > 0 || availableMarketItems.length > 0 ? (
+ {catalogItems.length > 0 ? (
<div className={cn('plugin-motion-stagger', PLUGIN_MANAGEMENT_CARD_GRID_CLASS)}>
- {items.map((item) => (
- <GhostPluginCard
- key={item.id}
- item={item}
- sourceLabel={t(`settings.ghosts.page.origin.${item.origin}`)}
- onSelect={() => setSelectedId(item.id)}
- onAction={() => void handleUseGhost(item.id)}
- updateVersion={item.marketUpdate?.version}
- updateBusy={item.marketUpdate !== null && marketBusyId !== null}
- onUpdate={
- item.marketUpdate ? () => void handleMarketUpdate(item.id) : undefined
- }
- effectiveEnabled={effectiveEnabled(item.id, item.enabled)}
- toggleDisabled={scopeDir !== null && !item.enabled}
- onToggle={(enabled) => void handleToggle(item.id, enabled)}
- />
- ))}
- {availableMarketItems.map((item) => (
- <MarketPluginCard
- key={item.pluginId}
- item={item}
- busy={marketBusyId !== null}
- onSelect={() => void handleSelectMarket(item.pluginId)}
- onIconLoadError={handleMarketIconLoadError}
- />
- ))}
+ {catalogItems.map((catalogItem) =>
+ catalogItem.kind === 'installed' ? (
+ <GhostPluginCard
+ key={`installed:${catalogItem.item.id}`}
+ item={catalogItem.item}
+ sourceLabel={t(`settings.ghosts.page.origin.${catalogItem.item.origin}`)}
+ onSelect={() => setSelectedId(catalogItem.item.id)}
+ onAction={() => void handleUseGhost(catalogItem.item.id)}
+ updateVersion={catalogItem.item.marketUpdate?.version}
+ updateBusy={catalogItem.item.marketUpdate !== null && marketBusyId !== null}
+ onUpdate={
+ catalogItem.item.marketUpdate
+ ? () => void handleMarketUpdate(catalogItem.item.id)
+ : undefined
+ }
+ effectiveEnabled={effectiveEnabled(
+ catalogItem.item.id,
+ catalogItem.item.enabled,
+ )}
+ toggleDisabled={scopeDir !== null && !catalogItem.item.enabled}
+ onToggle={(enabled) => void handleToggle(catalogItem.item.id, enabled)}
+ />
+ ) : (
+ <MarketPluginCard
+ key={`market:${catalogItem.item.pluginId}`}
+ item={catalogItem.item}
+ busy={marketBusyId !== null}
+ onSelect={() => void handleSelectMarket(catalogItem.item.pluginId)}
+ onIconLoadError={handleMarketIconLoadError}
+ />
+ ),
+ )}
</div>
) : !legacyRecoveryStatus ? (
<div className="rounded-xl border-[0.5px] border-[var(--border-default)] px-5 py-10 text-center">
diff --git a/apps/desktop/src/renderer/features/plugin/lib/__tests__/pluginMarketPresentation.test.ts b/apps/desktop/src/renderer/features/plugin/lib/__tests__/pluginMarketPresentation.test.ts
index 661c1a97fd..1be58c46fa 100644
--- a/apps/desktop/src/renderer/features/plugin/lib/__tests__/pluginMarketPresentation.test.ts
+++ b/apps/desktop/src/renderer/features/plugin/lib/__tests__/pluginMarketPresentation.test.ts
@@ -1,5 +1,29 @@
import { describe, expect, it } from 'vitest';
-import { pluginPresentationOrigin } from '../pluginMarketPresentation';
+import type { PluginMarketItem } from '../../../../../shared/pluginMarket';
+import { orderPluginCatalogItems, pluginPresentationOrigin } from '../pluginMarketPresentation';
+
+function marketItem(
+ pluginId: string,
+ ghostId: string,
+ installState: PluginMarketItem['installState'],
+): PluginMarketItem {
+ return {
+ pluginId,
+ ghostId,
+ name: ghostId,
+ description: null,
+ author: null,
+ scope: 'public',
+ organizationId: null,
+ defaultInstall: false,
+ releaseId: `release-${pluginId}`,
+ version: '1.0.0',
+ publishedAt: '2026-07-27T00:00:00.000Z',
+ icon: null,
+ installState,
+ enabled: installState === 'not-installed' ? null : true,
+ };
+}
describe('pluginPresentationOrigin', () => {
it('maps public plugins independently of their default-install policy', () => {
@@ -18,3 +42,78 @@ describe('pluginPresentationOrigin', () => {
expect(pluginPresentationOrigin(item)).toBe('local');
});
});
+
+describe('orderPluginCatalogItems', () => {
+ it('renders installed and available cards in the server response order', () => {
+ const first = marketItem('plugin-first', 'first', 'not-installed');
+ const second = marketItem('plugin-second', 'second', 'installed');
+ const third = marketItem('plugin-third', 'third', 'not-installed');
+
+ const ordered = orderPluginCatalogItems(
+ [first, second, third],
+ [{ id: 'second' }],
+ [first, third],
+ );
+
+ expect(
+ ordered.map(({ kind, item }) => `${kind}:${kind === 'installed' ? item.id : item.ghostId}`),
+ ).toEqual(['market:first', 'installed:second', 'market:third']);
+ });
+
+ it('keeps local-only installed plugins after the server-ordered catalog', () => {
+ const market = marketItem('plugin-market', 'market', 'not-installed');
+
+ const ordered = orderPluginCatalogItems(
+ [market],
+ [{ id: 'local-z' }, { id: 'local-a' }],
+ [market],
+ );
+
+ expect(
+ ordered.map(({ kind, item }) => `${kind}:${kind === 'installed' ? item.id : item.ghostId}`),
+ ).toEqual(['market:market', 'installed:local-z', 'installed:local-a']);
+ });
+
+ it('keeps a conflicting market card and its local install at the server position', () => {
+ const first = marketItem('plugin-first', 'first', 'not-installed');
+ const conflict = marketItem('plugin-conflict', 'collision', 'conflict');
+ const third = marketItem('plugin-third', 'third', 'not-installed');
+
+ const ordered = orderPluginCatalogItems(
+ [first, conflict, third],
+ [{ id: 'collision' }],
+ [first, conflict, third],
+ );
+
+ expect(
+ ordered.map(({ kind, item }) => `${kind}:${kind === 'installed' ? item.id : item.ghostId}`),
+ ).toEqual(['market:first', 'market:collision', 'installed:collision', 'market:third']);
+ });
+
+ it('does not duplicate installed records passed through the available-item input', () => {
+ const installed = marketItem('plugin-installed', 'installed', 'installed');
+ const update = marketItem('plugin-update', 'update', 'update-available');
+
+ const ordered = orderPluginCatalogItems(
+ [installed, update],
+ [{ id: 'installed' }, { id: 'update' }],
+ [installed, update],
+ );
+
+ expect(
+ ordered.map(({ kind, item }) => `${kind}:${kind === 'installed' ? item.id : item.ghostId}`),
+ ).toEqual(['installed:installed', 'installed:update']);
+ });
+
+ it('preserves server order after search or origin filters remove entries', () => {
+ const first = marketItem('plugin-first', 'first', 'not-installed');
+ const hidden = marketItem('plugin-hidden', 'hidden', 'not-installed');
+ const third = marketItem('plugin-third', 'third', 'update-available');
+
+ const ordered = orderPluginCatalogItems([first, hidden, third], [{ id: 'third' }], [first]);
+
+ expect(
+ ordered.map(({ kind, item }) => `${kind}:${kind === 'installed' ? item.id : item.ghostId}`),
+ ).toEqual(['market:first', 'installed:third']);
+ });
+});
diff --git a/apps/desktop/src/renderer/features/plugin/lib/pluginMarketPresentation.ts b/apps/desktop/src/renderer/features/plugin/lib/pluginMarketPresentation.ts
index bac80d4407..e30b19707f 100644
--- a/apps/desktop/src/renderer/features/plugin/lib/pluginMarketPresentation.ts
+++ b/apps/desktop/src/renderer/features/plugin/lib/pluginMarketPresentation.ts
@@ -12,6 +12,9 @@ export type PluginPresentationOrigin =
| 'organization'
| 'local';
+export type PluginCatalogPresentationItem<TInstalled> =
+ { kind: 'installed'; item: TInstalled } | { kind: 'market'; item: PluginMarketItem };
+
export function pluginPresentationOrigin(
item: Pick<PluginMarketItem, 'scope'> | null | undefined,
): PluginPresentationOrigin {
@@ -22,3 +25,46 @@ export function pluginPresentationOrigin(
// Keep the renderer taxonomy closed even if an older/newer server returns it.
return 'local';
}
+
+/**
+ * Keeps the complete catalog in server order while rendering an installed card
+ * for market records already owned by this client. Local-only installs have no
+ * server position, so they remain visible after the ordered market catalog.
+ */
+export function orderPluginCatalogItems<TInstalled extends { id: string }>(
+ marketItems: readonly PluginMarketItem[],
+ installedItems: readonly TInstalled[],
+ availableMarketItems: readonly PluginMarketItem[],
+): PluginCatalogPresentationItem<TInstalled>[] {
+ const installedByGhostId = new Map(installedItems.map((item) => [item.id, item]));
+ const availableByPluginId = new Map(availableMarketItems.map((item) => [item.pluginId, item]));
+ const emittedInstalledIds = new Set<string>();
+ const ordered: PluginCatalogPresentationItem<TInstalled>[] = [];
+
+ for (const marketItem of marketItems) {
+ const availableItem = availableByPluginId.get(marketItem.pluginId);
+ if (
+ availableItem &&
+ (marketItem.installState === 'not-installed' || marketItem.installState === 'conflict')
+ ) {
+ ordered.push({ kind: 'market', item: availableItem });
+ }
+ if (
+ marketItem.installState !== 'installed' &&
+ marketItem.installState !== 'update-available' &&
+ marketItem.installState !== 'conflict'
+ ) {
+ continue;
+ }
+ const installedItem = installedByGhostId.get(marketItem.ghostId);
+ if (!installedItem || emittedInstalledIds.has(installedItem.id)) continue;
+ emittedInstalledIds.add(installedItem.id);
+ ordered.push({ kind: 'installed', item: installedItem });
+ }
+
+ for (const installedItem of installedItems) {
+ if (emittedInstalledIds.has(installedItem.id)) continue;
+ ordered.push({ kind: 'installed', item: installedItem });
+ }
+ return ordered;
+}