forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseCreateMemory.ts
More file actions
88 lines (74 loc) · 2.67 KB
/
Copy pathuseCreateMemory.ts
File metadata and controls
88 lines (74 loc) · 2.67 KB
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
import { ConfigIO } from '../../../lib';
import type { Memory } from '../../../schema';
import { getAvailableAgents } from '../../operations/attach';
import { memoryPrimitive } from '../../primitives/registry';
import { useCallback, useEffect, useState } from 'react';
interface CreateMemoryConfig {
name: string;
eventExpiryDuration: number;
strategies: { type: string }[];
streaming?: { dataStreamArn: string; contentLevel: string };
}
interface CreateStatus<T> {
state: 'idle' | 'loading' | 'success' | 'error';
error?: string;
result?: T;
}
export function useCreateMemory() {
const [status, setStatus] = useState<CreateStatus<Memory>>({ state: 'idle' });
const create = useCallback(async (config: CreateMemoryConfig) => {
setStatus({ state: 'loading' });
try {
const strategiesStr = config.strategies.map(s => s.type).join(',');
const addResult = await memoryPrimitive.add({
name: config.name,
expiry: config.eventExpiryDuration,
strategies: strategiesStr || undefined,
dataStreamArn: config.streaming?.dataStreamArn,
contentLevel: config.streaming?.contentLevel,
});
if (!addResult.success) {
throw new Error(addResult.error ?? 'Failed to create memory');
}
// Read back the memory object
const configIO = new ConfigIO();
const project = await configIO.readProjectSpec();
const memory = project.memories.find(m => m.name === config.name);
if (!memory) {
throw new Error(`Memory "${config.name}" not found after creation`);
}
setStatus({ state: 'success', result: memory });
return { ok: true as const, result: memory };
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to create memory.';
setStatus({ state: 'error', error: message });
return { ok: false as const, error: message };
}
}, []);
const reset = useCallback(() => {
setStatus({ state: 'idle' });
}, []);
return { status, createMemory: create, reset };
}
export function useExistingMemoryNames() {
const [names, setNames] = useState<string[]>([]);
useEffect(() => {
void memoryPrimitive.getAllNames().then(setNames);
}, []);
const refresh = useCallback(async () => {
const result = await memoryPrimitive.getAllNames();
setNames(result);
}, []);
return { names, refresh };
}
export function useAvailableAgentsForMemory() {
const [agents, setAgents] = useState<string[]>([]);
useEffect(() => {
void getAvailableAgents().then(setAgents);
}, []);
const refresh = useCallback(async () => {
const result = await getAvailableAgents();
setAgents(result);
}, []);
return { agents, refresh };
}