дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
Adds Settings › Agents › Memory and a Session Memory picker (/memory, /memories, the composer + menu and the context meter link). They list each agent's own instruction files, path-scoped rules and auto-memory notes: AGENTS.md, CLAUDE.md and .claude/rules, .github/copilot-instructions.md and .github/instructions, .cursor/rules, GEMINI.md, and the Claude Code, Codex and Hermes auto-memory folders. Per-file modes (Auto / Always / Off) and the auto-memory policy (Inherit / Read & Write / Read Only / Off) are stored in settings.toml at user or workspace scope and never edit tracked files. Workspace entries load only in trusted projects. Each driver receives the policy natively: Claude Code through --settings and --add-dir, Codex through its memories config and /memories, GitHub Copilot through excludedTools and the system message, and Hermes through config.yaml. A file set to Always is staged into other agents' sessions. Auto-memory notes can be promoted into the workspace AGENTS.md, and an idle session restarts when a memory policy changes. Groups of the same agent are titled by scope, for example Claude Code (User) and Claude Code (Auto Memory). Closes #6810 Co-authored-by: dengliu <[email protected]>
330 рядки
10 KiB
TypeScript
330 рядки
10 KiB
TypeScript
/**
|
||
* Shared agent-memory model behind Settings › Agents › Memory and the composer MemoryPicker:
|
||
* discovered instruction files, path-scoped rules, and learned auto-memories across agents,
|
||
* plus per-session overrides (`AgentTask.memoryOverrides`).
|
||
*/
|
||
import { useEffect } from "react";
|
||
import { create } from "zustand";
|
||
import type {
|
||
AgentMemoryCatalog,
|
||
AgentMemoryMode,
|
||
AgentMemoryOverrides,
|
||
AgentMemorySource,
|
||
AgentMemoryState,
|
||
AutoMemoryMode,
|
||
CreateAgentMemoryInput,
|
||
McpScope,
|
||
PromoteAgentMemoryInput,
|
||
} from "@/bindings";
|
||
import { call, commands, isTauri, onIdeEvent } from "@/platform/ipc";
|
||
import { useProject } from "@/platform/project";
|
||
import { matchesAgentProvider, normalizeProviderId } from "./mcpModel";
|
||
|
||
export const EMPTY_MEMORY_CATALOG: AgentMemoryCatalog = {
|
||
entries: [],
|
||
providers: [],
|
||
autoMemory: "inherit",
|
||
autoMemoryOrigin: "none",
|
||
};
|
||
|
||
export interface MemoryBackend {
|
||
list(): Promise<AgentMemoryCatalog>;
|
||
setMode(id: string, scope: McpScope, mode: AgentMemoryMode, agents?: string[]): Promise<AgentMemoryCatalog>;
|
||
setAuto(scope: McpScope, provider: string | null, mode: AutoMemoryMode): Promise<AgentMemoryCatalog>;
|
||
create?(input: CreateAgentMemoryInput): Promise<AgentMemoryCatalog>;
|
||
promote?(input: PromoteAgentMemoryInput): Promise<AgentMemoryCatalog>;
|
||
remove?(id: string): Promise<AgentMemoryCatalog>;
|
||
resetProvider?(provider: string): Promise<AgentMemoryCatalog>;
|
||
}
|
||
|
||
export const defaultMemoryBackend: MemoryBackend = {
|
||
list: () =>
|
||
isTauri() && typeof commands.agentMemoryList === "function"
|
||
? call(commands.agentMemoryList())
|
||
: Promise.resolve(useMemoryCatalog.getState().catalog),
|
||
setMode: (id, scope, mode, agents = []) => call(commands.agentMemorySetMode(id, scope, mode, agents)),
|
||
setAuto: (scope, provider, mode) => call(commands.agentMemorySetAuto(scope, provider, mode)),
|
||
create: (input) => call(commands.agentMemoryCreate(input)),
|
||
promote: (input) => call(commands.agentMemoryPromote(input)),
|
||
remove: (id) => call(commands.agentMemoryDelete(id)),
|
||
resetProvider: (provider) => call(commands.agentMemoryResetProvider(provider)),
|
||
};
|
||
|
||
export const MEMORY_SOURCE_GROUPS: Record<
|
||
AgentMemorySource,
|
||
{
|
||
label: string;
|
||
/** Tells apart groups of one agent: its user-level files and its auto memory. */
|
||
qualifier: "User" | "Auto Memory" | null;
|
||
paths: string;
|
||
agent: string | null;
|
||
isAuto: boolean;
|
||
}
|
||
> = {
|
||
workspace: {
|
||
label: "Workspace",
|
||
qualifier: null,
|
||
paths: "AGENTS.md · AGENTS.override.md",
|
||
agent: null,
|
||
isAuto: false,
|
||
},
|
||
claudeWorkspace: {
|
||
label: "Claude Code",
|
||
qualifier: null,
|
||
paths: "CLAUDE.md · CLAUDE.local.md · .claude/rules",
|
||
agent: "claude",
|
||
isAuto: false,
|
||
},
|
||
claudeUser: {
|
||
label: "Claude Code",
|
||
qualifier: "User",
|
||
paths: "~/.claude/CLAUDE.md · ~/.claude/rules",
|
||
agent: "claude",
|
||
isAuto: false,
|
||
},
|
||
claudeAuto: {
|
||
label: "Claude Code",
|
||
qualifier: "Auto Memory",
|
||
paths: "~/.claude/projects/…/memory",
|
||
agent: "claude",
|
||
isAuto: true,
|
||
},
|
||
githubWorkspace: {
|
||
label: "GitHub Copilot",
|
||
qualifier: null,
|
||
paths: ".github/copilot-instructions.md · .github/instructions",
|
||
agent: "copilot",
|
||
isAuto: false,
|
||
},
|
||
copilotUser: {
|
||
label: "GitHub Copilot",
|
||
qualifier: "User",
|
||
paths: "~/.copilot/copilot-instructions.md · ~/.copilot/instructions",
|
||
agent: "copilot",
|
||
isAuto: false,
|
||
},
|
||
codexUser: {
|
||
label: "Codex",
|
||
qualifier: "User",
|
||
paths: "~/.codex/AGENTS.md",
|
||
agent: "codex",
|
||
isAuto: false,
|
||
},
|
||
codexAuto: {
|
||
label: "Codex",
|
||
qualifier: "Auto Memory",
|
||
paths: "~/.codex/memories",
|
||
agent: "codex",
|
||
isAuto: true,
|
||
},
|
||
cursorWorkspace: {
|
||
label: "Cursor",
|
||
qualifier: null,
|
||
paths: ".cursor/rules · .cursorrules",
|
||
agent: "cursor",
|
||
isAuto: false,
|
||
},
|
||
geminiWorkspace: {
|
||
label: "Gemini CLI",
|
||
qualifier: null,
|
||
paths: "GEMINI.md",
|
||
agent: "gemini",
|
||
isAuto: false,
|
||
},
|
||
geminiUser: {
|
||
label: "Gemini CLI",
|
||
qualifier: "User",
|
||
paths: "~/.gemini/GEMINI.md",
|
||
agent: "gemini",
|
||
isAuto: false,
|
||
},
|
||
hermesAuto: {
|
||
label: "Hermes Agent",
|
||
qualifier: "Auto Memory",
|
||
paths: "~/.hermes/memories",
|
||
agent: "hermes",
|
||
isAuto: true,
|
||
},
|
||
};
|
||
|
||
/** Group heading: the agent, plus "User" / "Auto Memory" when that agent has several groups. */
|
||
export function memoryGroupTitle(source: AgentMemorySource, tx: (text: string) => string): string {
|
||
const { label, qualifier } = MEMORY_SOURCE_GROUPS[source];
|
||
return qualifier ? `${tx(label)} (${tx(qualifier)})` : tx(label);
|
||
}
|
||
|
||
export function normalizeMemoryProvider(provider: string): string {
|
||
const norm = normalizeProviderId(provider);
|
||
return norm === "hermes" || norm.startsWith("hermes-") ? "hermes" : norm;
|
||
}
|
||
|
||
export function isDiscoveredMemorySource(source: AgentMemorySource): boolean {
|
||
return source !== "workspace";
|
||
}
|
||
|
||
/** Whether `entry` applies to a session running `provider` unless explicitly forced on. */
|
||
export function appliesToProvider(state: AgentMemoryState, provider: string): boolean {
|
||
if (!matchesAgentProvider(state.agents, provider)) return false;
|
||
const group = MEMORY_SOURCE_GROUPS[state.entry.source];
|
||
if (!group.agent) return true;
|
||
const norm = normalizeMemoryProvider(provider);
|
||
// Copilot also reads `CLAUDE.md` natively; Claude reads `AGENTS.md`.
|
||
if (norm === "copilot" && state.entry.source === "claudeWorkspace") return true;
|
||
return group.agent === norm;
|
||
}
|
||
|
||
export interface SessionMemoryItem {
|
||
id: string;
|
||
state: AgentMemoryState;
|
||
defaultMode: AgentMemoryMode;
|
||
mode: AgentMemoryMode;
|
||
overridden: boolean;
|
||
locked: boolean;
|
||
}
|
||
|
||
export interface ResolvedSessionMemory {
|
||
groups: Array<{ source: AgentMemorySource; items: SessionMemoryItem[] }>;
|
||
defaultAutoMemory: AutoMemoryMode;
|
||
autoMemory: AutoMemoryMode;
|
||
autoMemoryOverridden: boolean;
|
||
supportsAutoControl: boolean;
|
||
supportsFileExclusion: boolean;
|
||
activeCount: number;
|
||
totalCount: number;
|
||
overrideCount: number;
|
||
activeTokens: number;
|
||
}
|
||
|
||
export function resolveSessionMemory(
|
||
catalog: AgentMemoryCatalog,
|
||
provider: string,
|
||
overrides?: AgentMemoryOverrides | null,
|
||
): ResolvedSessionMemory {
|
||
const norm = normalizeMemoryProvider(provider);
|
||
const provStatus =
|
||
catalog.providers.find((p) => p.provider === norm) ?? catalog.providers.find((p) => p.provider === "acp");
|
||
const defaultAutoMemory = provStatus?.mode ?? catalog.autoMemory;
|
||
const explicitAuto = overrides?.autoMemory ?? undefined;
|
||
const autoMemory = explicitAuto ?? defaultAutoMemory;
|
||
const autoMemoryOverridden = explicitAuto !== undefined && explicitAuto !== defaultAutoMemory;
|
||
|
||
const entryOverrides = overrides?.entries ?? {};
|
||
const relevant = catalog.entries.filter(
|
||
(s) => appliesToProvider(s, provider) || entryOverrides[s.entry.id] !== undefined,
|
||
);
|
||
|
||
const items = relevant.map<SessionMemoryItem>((state) => {
|
||
const locked = state.origin === "trust";
|
||
const explicit = entryOverrides[state.entry.id];
|
||
const mode = locked ? "off" : (explicit ?? state.mode);
|
||
return {
|
||
id: state.entry.id,
|
||
state,
|
||
defaultMode: state.mode,
|
||
mode,
|
||
overridden: !locked && explicit !== undefined && explicit !== state.mode,
|
||
locked,
|
||
};
|
||
});
|
||
|
||
const groups: ResolvedSessionMemory["groups"] = [];
|
||
const seen = new Set<AgentMemorySource>();
|
||
for (const item of items) {
|
||
const src = item.state.entry.source;
|
||
if (seen.has(src)) continue;
|
||
seen.add(src);
|
||
groups.push({
|
||
source: src,
|
||
items: items.filter((i) => i.state.entry.source === src),
|
||
});
|
||
}
|
||
|
||
const activeItems = items.filter((i) => i.mode !== "off");
|
||
const activeTokens = activeItems.reduce(
|
||
(sum, i) => sum + ((i.state.entry.paths?.length ?? 0) > 0 && i.mode !== "always" ? 0 : i.state.entry.approxTokens),
|
||
0,
|
||
);
|
||
const overrideCount = items.filter((i) => i.overridden).length + (autoMemoryOverridden ? 1 : 0);
|
||
|
||
return {
|
||
groups,
|
||
defaultAutoMemory,
|
||
autoMemory,
|
||
autoMemoryOverridden,
|
||
supportsAutoControl: provStatus?.supportsAutoControl ?? false,
|
||
supportsFileExclusion: provStatus?.supportsFileExclusion ?? false,
|
||
activeCount: activeItems.length,
|
||
totalCount: items.length,
|
||
overrideCount,
|
||
activeTokens,
|
||
};
|
||
}
|
||
|
||
export function nextMemoryEntryOverrides(
|
||
current: AgentMemoryOverrides | null | undefined,
|
||
item: Pick<SessionMemoryItem, "id" | "defaultMode">,
|
||
nextEnabled: boolean,
|
||
): AgentMemoryOverrides {
|
||
const entries = { ...(current?.entries ?? {}) };
|
||
const defaultEnabled = item.defaultMode !== "off";
|
||
if (nextEnabled === defaultEnabled) {
|
||
delete entries[item.id];
|
||
} else {
|
||
entries[item.id] = nextEnabled ? "auto" : "off";
|
||
}
|
||
return {
|
||
autoMemory: current?.autoMemory ?? null,
|
||
entries,
|
||
};
|
||
}
|
||
|
||
export function nextAutoMemoryOverride(
|
||
current: AgentMemoryOverrides | null | undefined,
|
||
defaultAutoMemory: AutoMemoryMode,
|
||
nextMode: AutoMemoryMode,
|
||
): AgentMemoryOverrides {
|
||
return {
|
||
autoMemory: nextMode === defaultAutoMemory ? null : nextMode,
|
||
entries: { ...(current?.entries ?? {}) },
|
||
};
|
||
}
|
||
|
||
interface MemoryCatalogState {
|
||
catalog: AgentMemoryCatalog;
|
||
loaded: boolean;
|
||
}
|
||
|
||
export const useMemoryCatalog = create<MemoryCatalogState>(() => ({
|
||
catalog: EMPTY_MEMORY_CATALOG,
|
||
loaded: false,
|
||
}));
|
||
|
||
let listenerAttached = false;
|
||
|
||
export async function refreshMemoryCatalog(backend: MemoryBackend = defaultMemoryBackend): Promise<void> {
|
||
if (!isTauri()) return;
|
||
const catalog = await backend.list().catch(() => null);
|
||
if (catalog) useMemoryCatalog.setState({ catalog, loaded: true });
|
||
}
|
||
|
||
export function useMemoryCatalogSync(eager: boolean, backend: MemoryBackend = defaultMemoryBackend): void {
|
||
const root = useProject((s) => s.project?.root);
|
||
useEffect(() => {
|
||
if (!isTauri()) return;
|
||
const onFocus = () => void refreshMemoryCatalog(backend);
|
||
if (eager) {
|
||
onFocus();
|
||
window.addEventListener("focus", onFocus);
|
||
}
|
||
if (!listenerAttached && typeof onIdeEvent === "function") {
|
||
listenerAttached = true;
|
||
onIdeEvent("settingsChanged", (e) => {
|
||
if (!e.key || e.key.startsWith("agents.memory")) {
|
||
void refreshMemoryCatalog(backend);
|
||
}
|
||
});
|
||
}
|
||
return () => window.removeEventListener("focus", onFocus);
|
||
}, [eager, root, backend]);
|
||
}
|