2026-08-18 02:59:04 +03:00
|
|
|
/**
|
|
|
|
|
* Keeps agent memory loaded for whatever project the session belongs to.
|
|
|
|
|
*
|
|
|
|
|
* This does not belong to the Memory tab. The session index is built from the
|
|
|
|
|
* loaded snapshot, so leaving the load to the panel meant a user who never
|
|
|
|
|
* opened Project notes sent every message with no memory index at all — the
|
|
|
|
|
* agent had memories it was never told about.
|
|
|
|
|
*
|
|
|
|
|
* The session directory is resolved to its project first. A session in a
|
|
|
|
|
* worktree has the worktree's path, and loading by that path reads a store the
|
|
|
|
|
* agent does not write to, which is the same mismatch in the other direction.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
|
|
import { subscribeOpenchamberEvents } from '@/lib/openchamberEvents';
|
|
|
|
|
import { useAgentMemoryStore } from '@/stores/useAgentMemoryStore';
|
|
|
|
|
import { useUIStore } from '@/stores/useUIStore';
|
2026-08-27 18:23:40 +03:00
|
|
|
import { useProjectContextOwner } from '@/hooks/useProjectContextOwner';
|
2026-08-18 02:59:04 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The directory is a parameter rather than read from `useEffectiveDirectory`,
|
|
|
|
|
* because this runs above `SyncProvider` — that hook reads the sync context and
|
|
|
|
|
* throws outside it, which took the whole app down with a blank window.
|
|
|
|
|
*/
|
2026-08-29 17:01:15 +03:00
|
|
|
const AGENT_MEMORY_FRESH_MS = 60_000;
|
|
|
|
|
|
2026-08-18 02:59:04 +03:00
|
|
|
export const useAgentMemorySync = (directory: string | null): void => {
|
|
|
|
|
const enabled = useUIStore((state) => (
|
|
|
|
|
state.agentMemoryFeatureAvailable && state.agentMemoryToolEnabled
|
|
|
|
|
));
|
|
|
|
|
const load = useAgentMemoryStore((state) => state.load);
|
2026-08-27 18:23:40 +03:00
|
|
|
const owner = useProjectContextOwner(directory);
|
|
|
|
|
const projectPath = owner?.path ?? null;
|
2026-08-18 02:59:04 +03:00
|
|
|
|
2026-08-29 17:01:15 +03:00
|
|
|
// The owner re-resolves on every directory switch; entries loaded moments
|
|
|
|
|
// ago for the same project are still current, and the change event below
|
|
|
|
|
// forces a re-read when the agent writes memory.
|
2026-08-18 02:59:04 +03:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-29 17:01:15 +03:00
|
|
|
void load(projectPath, { maxAgeMs: AGENT_MEMORY_FRESH_MS });
|
2026-08-18 02:59:04 +03:00
|
|
|
}, [enabled, load, projectPath]);
|
|
|
|
|
|
|
|
|
|
// The agent writes memory mid-turn through its own tool, so the index for the
|
|
|
|
|
// next message has to come from a fresh read rather than the snapshot taken
|
|
|
|
|
// before the turn started.
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return subscribeOpenchamberEvents((event) => {
|
|
|
|
|
if (event.type === 'agent-memory-changed') {
|
|
|
|
|
void load(projectPath);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, [enabled, load, projectPath]);
|
|
|
|
|
};
|