cc-haha/desktop/src/stores/pluginStore.ts
程序员阿江(Relakkes) de63d82633 fix: keep desktop plugin changes visible without restarting chats
Plugin enablement already has a live CLI reload control path, so desktop now applies plugin changes by refreshing the active session instead of waiting for a future conversation startup. The server forwards reload_plugins to the active CLI session, refreshes session slash-command cache, and notifies the client. The desktop plugin store automatically reloads after mutating plugin state, and the empty-session composer refetches skills when plugin capabilities change.

Constraint: Existing CLI exposes reload_plugins as the supported hot-refresh mechanism for commands, agents, plugins, and MCP state.

Rejected: Start a hidden replacement CLI process | higher cost, extra process lifecycle risk, and less precise than the existing control channel.

Confidence: high

Scope-risk: moderate

Directive: Keep plugin refresh routed through reload_plugins unless the CLI control contract is removed or changed.

Tested: bun test src/server/__tests__/plugins.test.ts

Tested: cd desktop && bun run test --run src/stores/pluginStore.test.ts src/__tests__/pluginsSettings.test.tsx src/pages/EmptySession.test.tsx

Tested: bun run check:server

Tested: cd desktop && bun run lint

Tested: cd desktop && bun run build

Not-tested: bun run check:desktop is blocked by a pre-existing color-mix compatibility failure in desktop/src/theme/globals.css.
2026-05-15 19:51:01 +08:00

164 lines
4.4 KiB
TypeScript

import { create } from 'zustand'
import { pluginsApi } from '../api/plugins'
import type {
PluginDetail,
PluginListResponse,
PluginReloadSummary,
PluginScope,
PluginSummary,
} from '../types/plugin'
type PluginStore = {
plugins: PluginSummary[]
marketplaces: PluginListResponse['marketplaces']
summary: PluginListResponse['summary'] | null
selectedPlugin: PluginDetail | null
lastReloadSummary: PluginReloadSummary | null
isLoading: boolean
isDetailLoading: boolean
isApplying: boolean
error: string | null
fetchPlugins: (cwd?: string) => Promise<void>
fetchPluginDetail: (id: string, cwd?: string) => Promise<void>
reloadPlugins: (cwd?: string, sessionId?: string) => Promise<PluginReloadSummary>
enablePlugin: (id: string, scope?: PluginScope, cwd?: string, sessionId?: string) => Promise<string>
disablePlugin: (id: string, scope?: PluginScope, cwd?: string, sessionId?: string) => Promise<string>
updatePlugin: (id: string, scope?: PluginScope, cwd?: string, sessionId?: string) => Promise<string>
uninstallPlugin: (id: string, scope?: PluginScope, keepData?: boolean, cwd?: string, sessionId?: string) => Promise<string>
clearSelection: () => void
}
export const usePluginStore = create<PluginStore>((set, get) => ({
plugins: [],
marketplaces: [],
summary: null,
selectedPlugin: null,
lastReloadSummary: null,
isLoading: false,
isDetailLoading: false,
isApplying: false,
error: null,
fetchPlugins: async (cwd) => {
set({ isLoading: true, error: null })
try {
const data = await pluginsApi.list(cwd)
set({
plugins: data.plugins,
marketplaces: data.marketplaces,
summary: data.summary,
isLoading: false,
})
} catch (err) {
set({
isLoading: false,
error: err instanceof Error ? err.message : String(err),
})
}
},
fetchPluginDetail: async (id, cwd) => {
set({ isDetailLoading: true, error: null })
try {
const { detail } = await pluginsApi.detail(id, cwd)
set({ selectedPlugin: detail, isDetailLoading: false })
} catch (err) {
set({
isDetailLoading: false,
error: err instanceof Error ? err.message : String(err),
})
}
},
reloadPlugins: async (cwd, sessionId) => {
set({ isApplying: true, error: null })
try {
const { summary } = await pluginsApi.reload(cwd, sessionId)
await get().fetchPlugins(cwd)
const selected = get().selectedPlugin
if (selected) {
await get().fetchPluginDetail(selected.id, cwd)
}
set({ isApplying: false, lastReloadSummary: summary })
return summary
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
set({ isApplying: false, error: message })
throw err
}
},
enablePlugin: async (id, scope, cwd, sessionId) => {
return runAction(
() => pluginsApi.enable({ id, scope }),
set,
get,
cwd,
sessionId,
)
},
disablePlugin: async (id, scope, cwd, sessionId) => {
return runAction(
() => pluginsApi.disable({ id, scope }),
set,
get,
cwd,
sessionId,
)
},
updatePlugin: async (id, scope, cwd, sessionId) => {
return runAction(
() => pluginsApi.update({ id, scope }),
set,
get,
cwd,
sessionId,
)
},
uninstallPlugin: async (id, scope, keepData = false, cwd, sessionId) => {
return runAction(
() => pluginsApi.uninstall({ id, scope, keepData }),
set,
get,
cwd,
sessionId,
true,
)
},
clearSelection: () => set({ selectedPlugin: null }),
}))
async function runAction(
action: () => Promise<{ ok: true; message: string }>,
set: (updater: Partial<PluginStore>) => void,
get: () => PluginStore,
cwd?: string,
sessionId?: string,
clearSelection = false,
): Promise<string> {
set({ isApplying: true, error: null })
try {
const { message } = await action()
const { summary } = await pluginsApi.reload(cwd, sessionId)
await get().fetchPlugins(cwd)
const selected = get().selectedPlugin
if (clearSelection) {
set({ selectedPlugin: null })
} else if (selected) {
await get().fetchPluginDetail(selected.id, cwd)
}
set({ isApplying: false, lastReloadSummary: summary })
return message
} catch (err) {
set({
isApplying: false,
error: err instanceof Error ? err.message : String(err),
})
throw err
}
}