1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
import { useState, useEffect, useCallback } from "react";
import {
type DirectiveMemoryEntry,
type DirectiveMemoryConfig,
type MemoryCategory,
type CreateDirectiveMemoryRequest,
getDirectiveMemoryConfig,
setDirectiveMemoryEnabled,
listDirectiveMemories,
addDirectiveMemory,
deleteDirectiveMemory,
clearDirectiveMemories,
} from "../lib/api";
export function useDirectiveMemories(directiveId: string | undefined) {
const [memories, setMemories] = useState<DirectiveMemoryEntry[]>([]);
const [config, setConfig] = useState<DirectiveMemoryConfig | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const refreshConfig = useCallback(async () => {
if (!directiveId) return;
try {
const c = await getDirectiveMemoryConfig(directiveId);
setConfig(c);
} catch (e) {
// Config may not exist yet — treat as disabled
setConfig({ directiveId, enabled: false, updatedAt: new Date().toISOString() });
}
}, [directiveId]);
const refreshMemories = useCallback(async () => {
if (!directiveId) return;
try {
setLoading(true);
setError(null);
const entries = await listDirectiveMemories(directiveId);
setMemories(entries);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to load memories");
} finally {
setLoading(false);
}
}, [directiveId]);
const refresh = useCallback(async () => {
await Promise.all([refreshConfig(), refreshMemories()]);
}, [refreshConfig, refreshMemories]);
useEffect(() => {
refresh();
}, [refresh]);
const toggleEnabled = useCallback(async (enabled: boolean) => {
if (!directiveId) return;
try {
setError(null);
const c = await setDirectiveMemoryEnabled(directiveId, enabled);
setConfig(c);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to toggle memory");
}
}, [directiveId]);
const add = useCallback(async (req: CreateDirectiveMemoryRequest) => {
if (!directiveId) return;
try {
setError(null);
await addDirectiveMemory(directiveId, req);
await refreshMemories();
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to add memory");
}
}, [directiveId, refreshMemories]);
const remove = useCallback(async (memoryId: string) => {
if (!directiveId) return;
try {
setError(null);
await deleteDirectiveMemory(directiveId, memoryId);
await refreshMemories();
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to delete memory");
}
}, [directiveId, refreshMemories]);
const clearAll = useCallback(async () => {
if (!directiveId) return;
try {
setError(null);
await clearDirectiveMemories(directiveId);
setMemories([]);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to clear memories");
}
}, [directiveId]);
/** Group entries by category */
const grouped = memories.reduce<Record<MemoryCategory, DirectiveMemoryEntry[]>>(
(acc, entry) => {
acc[entry.category].push(entry);
return acc;
},
{ decision: [], context: [], preference: [], learning: [], other: [] },
);
return {
memories,
grouped,
config,
loading,
error,
refresh,
toggleEnabled,
add,
remove,
clearAll,
};
}
|