| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- import { create } from "zustand";
- import {
- normalizeAgentProviderToZCodeAgent,
- ZCODE_AGENT_PROVIDER,
- type ZCodeProvider,
- type SkillSummary,
- type SkillsCapability,
- } from "@zcode/shared";
- import type { ISkillsService } from "@zcode/services";
- import { shouldExposeE2EStoreBridge } from "@/lib/e2eStoreBridge.js";
- import { logger } from "@/logger.js";
- interface SkillStoreState {
- workspacePath: string | null;
- workspaceIdentity: string | null;
- loadedWorkspacePath: string | null;
- loadedWorkspaceIdentity: string | null;
- provider: ZCodeProvider;
- loadedProvider: ZCodeProvider | null;
- skills: SkillSummary[];
- capability: SkillsCapability | null;
- loading: boolean;
- error: string | null;
- initialize: (
- workspacePath: string,
- providerOrSkillsService: ZCodeProvider | ISkillsService,
- maybeSkillsService?: ISkillsService,
- workspaceIdentity?: string,
- ) => Promise<void>;
- refresh: (skillsService: ISkillsService, workspaceIdentity?: string) => Promise<void>;
- setEnabled: (
- skillId: string,
- providerOrEnabled: ZCodeProvider | boolean,
- scopeOrSkillsService: SkillSummary["scope"] | ISkillsService,
- enabledOrSkillsService?: boolean | ISkillsService,
- maybeSkillsService?: ISkillsService,
- workspaceIdentity?: string,
- ) => Promise<void>;
- }
- const inFlightSkillLoads = new Map<string, ReturnType<ISkillsService["list"]>>();
- function getSkillLoadKey(
- workspacePath: string,
- provider: ZCodeProvider,
- workspaceIdentity?: string,
- ): string {
- return `${workspaceIdentity?.trim() || workspacePath}::${provider}`;
- }
- function loadSkillsOnce(
- workspacePath: string,
- provider: ZCodeProvider,
- skillsService: ISkillsService,
- workspaceIdentity?: string,
- options: { bypassCache?: boolean } = {},
- ): ReturnType<ISkillsService["list"]> {
- const key = getSkillLoadKey(workspacePath, provider, workspaceIdentity);
- if (!options.bypassCache) {
- const current = inFlightSkillLoads.get(key);
- if (current) {
- return current;
- }
- }
- // 复制/移除技能到通用目录后 refresh 必须拿到最新结果。
- // 若复用 in-flight Promise 会返回旧扫描结果,导致 chat mention 看不到新加的技能。
- const request = skillsService.list({ workspacePath, workspaceIdentity, provider }).finally(() => {
- // 仅在自己是当前活跃 in-flight 时清理,避免覆盖其他并发请求。
- if (inFlightSkillLoads.get(key) === request) {
- inFlightSkillLoads.delete(key);
- }
- });
- inFlightSkillLoads.set(key, request);
- return request;
- }
- export const useSkillStore = create<SkillStoreState>((set, get) => ({
- workspacePath: null,
- workspaceIdentity: null,
- loadedWorkspacePath: null,
- loadedWorkspaceIdentity: null,
- provider: ZCODE_AGENT_PROVIDER,
- loadedProvider: null,
- skills: [],
- capability: null,
- loading: false,
- error: null,
- async initialize(
- workspacePath: string,
- providerOrSkillsService: ZCodeProvider | ISkillsService,
- maybeSkillsService?: ISkillsService,
- workspaceIdentity?: string,
- ) {
- const currentState = get();
- const hasProvider = typeof providerOrSkillsService === "string";
- const provider = normalizeAgentProviderToZCodeAgent(
- hasProvider ? providerOrSkillsService : ZCODE_AGENT_PROVIDER,
- );
- const skillsService = hasProvider ? maybeSkillsService : providerOrSkillsService;
- const normalizedWorkspaceIdentity = workspaceIdentity?.trim() || null;
- if (!skillsService) {
- set({
- workspacePath,
- workspaceIdentity: normalizedWorkspaceIdentity,
- provider,
- loading: false,
- error: "skillsService is required",
- loadedWorkspacePath: workspacePath,
- loadedWorkspaceIdentity: normalizedWorkspaceIdentity,
- loadedProvider: provider,
- });
- return;
- }
- const hasCachedSkills =
- currentState.skills.length > 0 &&
- currentState.loadedWorkspacePath === workspacePath &&
- currentState.loadedWorkspaceIdentity === normalizedWorkspaceIdentity &&
- currentState.loadedProvider === provider;
- // 切换 agent 筛选时会触发 initialize。
- // 之前总是 loading=true,会先清空成“加载中”再渲染结果,导致列表闪烁。
- // 这里改成“只有同一 workspace+provider 的缓存才允许复用”,避免上一套技能误显示到当前会话里。
- set({
- workspacePath,
- workspaceIdentity: normalizedWorkspaceIdentity,
- provider,
- skills: hasCachedSkills ? currentState.skills : [],
- capability: hasCachedSkills ? currentState.capability : null,
- loading: !hasCachedSkills,
- error: null,
- });
- try {
- // 聊天输入区会同时挂载多个技能消费者(例如 $ mention 与 / 面板)。
- // 之前它们首屏会并发触发相同的 list 请求,进而把服务层镜像同步竞争放大成用户可见报错。
- // 这里先按 workspace+provider 去重,同一轮只复用一个请求结果。
- const result = await loadSkillsOnce(
- workspacePath,
- provider,
- skillsService,
- normalizedWorkspaceIdentity ?? undefined,
- );
- set({
- skills: result.skills,
- capability: result.capability,
- loading: false,
- loadedWorkspacePath: workspacePath,
- loadedWorkspaceIdentity: normalizedWorkspaceIdentity,
- loadedProvider: provider,
- });
- } catch (error) {
- logger.error("[skills] initialize failed", {
- workspacePath,
- provider,
- error: error instanceof Error ? error.message : String(error),
- });
- set({
- loading: false,
- error: error instanceof Error ? error.message : String(error),
- loadedWorkspacePath: workspacePath,
- loadedWorkspaceIdentity: normalizedWorkspaceIdentity,
- loadedProvider: provider,
- });
- }
- },
- async refresh(skillsService: ISkillsService, workspaceIdentity?: string) {
- const workspacePath = get().workspacePath;
- if (!workspacePath) {
- return;
- }
- const workspaceIdentityFromState =
- workspaceIdentity?.trim() || get().workspaceIdentity || undefined;
- const provider = normalizeAgentProviderToZCodeAgent(get().provider);
- const hasCachedSkills = get().skills.length > 0;
- // 开关技能后会触发 refresh,之前每次都把 loading 置 true,
- // Settings 列表会先切到“加载中”再切回数据,用户看到整列表闪烁。
- // 这里改成“仅首次无缓存时显示阻塞 loading”,有缓存时后台刷新并保留当前列表。
- set({ loading: !hasCachedSkills, error: null });
- // refresh 必须保证拿到「最新一次」的服务端结果。
- // 直接调 loadSkillsOnce 会复用 in-flight 的旧请求,
- // 导致复制 skill 到通用目录后 chat mention 仍看到旧列表。
- // 这里在 refresh 时显式跳过 in-flight 缓存,强制发起一次新请求。
- inFlightSkillLoads.delete(getSkillLoadKey(workspacePath, provider, workspaceIdentityFromState));
- try {
- const result = await loadSkillsOnce(
- workspacePath,
- provider,
- skillsService,
- workspaceIdentityFromState,
- );
- set({
- skills: result.skills,
- capability: result.capability,
- loading: false,
- loadedWorkspacePath: workspacePath,
- loadedWorkspaceIdentity: workspaceIdentityFromState ?? null,
- loadedProvider: provider,
- });
- } catch (error) {
- logger.error("[skills] refresh failed", {
- workspacePath,
- provider,
- error: error instanceof Error ? error.message : String(error),
- });
- set({
- loading: false,
- error: error instanceof Error ? error.message : String(error),
- loadedWorkspacePath: workspacePath,
- loadedWorkspaceIdentity: workspaceIdentityFromState ?? null,
- loadedProvider: provider,
- });
- }
- },
- async setEnabled(
- skillId: string,
- providerOrEnabled: ZCodeProvider | boolean,
- scopeOrSkillsService: SkillSummary["scope"] | ISkillsService,
- enabledOrSkillsService?: boolean | ISkillsService,
- maybeSkillsService?: ISkillsService,
- workspaceIdentity?: string,
- ) {
- const workspacePath = get().workspacePath;
- if (!workspacePath) {
- return;
- }
- const workspaceIdentityFromState =
- workspaceIdentity?.trim() || get().workspaceIdentity || undefined;
- const legacyCall = typeof providerOrEnabled === "boolean";
- const provider = normalizeAgentProviderToZCodeAgent(
- legacyCall ? get().provider : providerOrEnabled,
- );
- const scope = legacyCall ? undefined : (scopeOrSkillsService as SkillSummary["scope"]);
- const enabled = legacyCall ? providerOrEnabled : (enabledOrSkillsService as boolean);
- const skillsService = legacyCall
- ? (scopeOrSkillsService as ISkillsService)
- : maybeSkillsService;
- if (!skillsService) {
- set({ error: "skillsService is required" });
- return;
- }
- try {
- await skillsService.setEnabled({
- workspacePath,
- workspaceIdentity: workspaceIdentityFromState,
- provider,
- ...(scope ? { scope } : {}),
- skillId,
- enabled,
- });
- await get().refresh(skillsService, workspaceIdentityFromState);
- } catch (error) {
- set({
- error: error instanceof Error ? error.message : String(error),
- });
- }
- },
- }));
- type SkillStoreE2EBridge = typeof useSkillStore;
- declare global {
- interface Window {
- __skillStoreE2E?: SkillStoreE2EBridge;
- }
- }
- if (shouldExposeE2EStoreBridge()) {
- // E2E 诊断入口必须由 WDIO 显式打开,不能复用 ZCODE_ENV=test,避免产品测试环境暴露可变全局 store。
- window.__skillStoreE2E = useSkillStore;
- }
|