forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseRemove.ts
More file actions
322 lines (278 loc) · 9.71 KB
/
Copy pathuseRemove.ts
File metadata and controls
322 lines (278 loc) · 9.71 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import type { ResourceType } from '../../commands/remove/types';
import { RemoveLogger } from '../../logging';
import type { RemovableGatewayTarget, RemovalPreview, RemovalResult } from '../../operations/remove';
import type { RemovableCredential } from '../../primitives/CredentialPrimitive';
import type { RemovableMemory } from '../../primitives/MemoryPrimitive';
import type { RemovablePolicyResource } from '../../primitives/PolicyPrimitive';
import {
agentPrimitive,
credentialPrimitive,
evaluatorPrimitive,
gatewayPrimitive,
gatewayTargetPrimitive,
memoryPrimitive,
onlineEvalConfigPrimitive,
policyEnginePrimitive,
policyPrimitive,
} from '../../primitives/registry';
import { useCallback, useEffect, useRef, useState } from 'react';
// Re-export types for consumers
export type {
RemovableMemory,
RemovableCredential as RemovableIdentity,
RemovableGatewayTarget,
RemovablePolicyResource,
};
// ============================================================================
// Generic Hooks
// ============================================================================
/**
* Generic hook for loading removable resources from a primitive.
* All useRemovable* hooks delegate to this.
*/
function useRemovableResources<T>(loader: () => Promise<T[]>) {
// Ref captures the initial loader; all callers pass stable functions referencing singletons
const loaderRef = useRef(loader);
const [items, setItems] = useState<T[] | null>(null);
useEffect(() => {
void loaderRef.current().then(setItems);
}, []);
const refresh = useCallback(async () => {
setItems(await loaderRef.current());
}, []);
return { items: items ?? [], isLoading: items === null, refresh };
}
/**
* Generic hook for removing a resource with logging.
* All useRemove* hooks delegate to this.
*/
function useRemoveResource<TIdentifier>(
removeFn: (id: TIdentifier) => Promise<RemovalResult>,
resourceType: ResourceType,
getResourceName: (id: TIdentifier) => string
) {
// Refs capture initial values; all callers pass stable functions referencing singletons
const removeFnRef = useRef(removeFn);
const resourceTypeRef = useRef(resourceType);
const getNameRef = useRef(getResourceName);
const [state, setState] = useState<RemovalState>({ isLoading: false, result: null });
const [logFilePath, setLogFilePath] = useState<string | null>(null);
const remove = useCallback(async (id: TIdentifier, preview?: RemovalPreview): Promise<RemoveResult> => {
setState({ isLoading: true, result: null });
const result = await removeFnRef.current(id);
setState({ isLoading: false, result });
let logPath: string | undefined;
if (preview) {
const logger = new RemoveLogger({
resourceType: resourceTypeRef.current,
resourceName: getNameRef.current(id),
});
logger.logRemoval(preview, result.success, result.success ? undefined : result.error);
logPath = logger.getAbsoluteLogPath();
setLogFilePath(logPath);
}
return { ...result, logFilePath: logPath };
}, []);
const reset = useCallback(() => {
setState({ isLoading: false, result: null });
setLogFilePath(null);
}, []);
return { ...state, logFilePath, remove, reset };
}
// ============================================================================
// Removable Resources Hooks
// ============================================================================
export function useRemovableAgents() {
const { items: agents, ...rest } = useRemovableResources(() =>
agentPrimitive.getRemovable().then(r => r.map(a => a.name))
);
return { agents, ...rest };
}
export function useRemovableGateways() {
const { items: gateways, ...rest } = useRemovableResources(() =>
gatewayPrimitive.getRemovable().then(r => r.map(g => g.name))
);
return { gateways, ...rest };
}
export function useRemovableGatewayTargets() {
const { items: tools, ...rest } = useRemovableResources(() => gatewayTargetPrimitive.getRemovable());
return { tools, ...rest };
}
export function useRemovableMemories() {
const { items: memories, ...rest } = useRemovableResources(() => memoryPrimitive.getRemovable());
return { memories, ...rest };
}
export function useRemovableIdentities() {
const { items: identities, ...rest } = useRemovableResources(() => credentialPrimitive.getRemovable());
return { identities, ...rest };
}
export function useRemovableEvaluators() {
const { items: evaluators, ...rest } = useRemovableResources(() => evaluatorPrimitive.getRemovable());
return { evaluators, ...rest };
}
export function useRemovableOnlineEvalConfigs() {
const { items: onlineEvalConfigs, ...rest } = useRemovableResources(() => onlineEvalConfigPrimitive.getRemovable());
return { onlineEvalConfigs, ...rest };
}
export function useRemovablePolicyEngines() {
const { items: policyEngines, ...rest } = useRemovableResources(() => policyEnginePrimitive.getRemovable());
return { policyEngines, ...rest };
}
export function useRemovablePolicies() {
const { items: policies, ...rest } = useRemovableResources(() => policyPrimitive.getRemovable());
return { policies, ...rest };
}
// ============================================================================
// Preview Hook
// ============================================================================
interface PreviewState {
isLoading: boolean;
preview: RemovalPreview | null;
error: string | null;
}
type PreviewResult = { ok: true; preview: RemovalPreview } | { ok: false; error: string };
export function useRemovalPreview() {
const [state, setState] = useState<PreviewState>({
isLoading: false,
preview: null,
error: null,
});
const loadPreview = useCallback(
async <T>(previewFn: (id: T) => Promise<RemovalPreview>, id: T): Promise<PreviewResult> => {
setState({ isLoading: true, preview: null, error: null });
try {
const preview = await previewFn(id);
setState({ isLoading: false, preview, error: null });
return { ok: true, preview };
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to load preview';
setState({ isLoading: false, preview: null, error: message });
return { ok: false, error: message };
}
},
[]
);
const loadAgentPreview = useCallback(
(name: string) => loadPreview(n => agentPrimitive.previewRemove(n), name),
[loadPreview]
);
const loadGatewayPreview = useCallback(
(name: string) => loadPreview(n => gatewayPrimitive.previewRemove(n), name),
[loadPreview]
);
const loadGatewayTargetPreview = useCallback(
(tool: RemovableGatewayTarget) => loadPreview(t => gatewayTargetPrimitive.previewRemoveGatewayTarget(t), tool),
[loadPreview]
);
const loadMemoryPreview = useCallback(
(name: string) => loadPreview(n => memoryPrimitive.previewRemove(n), name),
[loadPreview]
);
const loadIdentityPreview = useCallback(
(name: string) => loadPreview(n => credentialPrimitive.previewRemove(n), name),
[loadPreview]
);
const loadEvaluatorPreview = useCallback(
(name: string) => loadPreview(n => evaluatorPrimitive.previewRemove(n), name),
[loadPreview]
);
const loadOnlineEvalPreview = useCallback(
(name: string) => loadPreview(n => onlineEvalConfigPrimitive.previewRemove(n), name),
[loadPreview]
);
const loadPolicyEnginePreview = useCallback(
(name: string) => loadPreview(n => policyEnginePrimitive.previewRemove(n), name),
[loadPreview]
);
const loadPolicyPreview = useCallback(
(compositeKey: string) => loadPreview(k => policyPrimitive.previewRemove(k), compositeKey),
[loadPreview]
);
const reset = useCallback(() => {
setState({ isLoading: false, preview: null, error: null });
}, []);
return {
...state,
loadAgentPreview,
loadGatewayPreview,
loadGatewayTargetPreview,
loadMemoryPreview,
loadIdentityPreview,
loadEvaluatorPreview,
loadOnlineEvalPreview,
loadPolicyEnginePreview,
loadPolicyPreview,
reset,
};
}
// ============================================================================
// Removal Hooks
// ============================================================================
interface RemovalState {
isLoading: boolean;
result: RemovalResult | null;
}
type RemoveResult = RemovalResult & { logFilePath?: string };
export function useRemoveAgent() {
return useRemoveResource(
(name: string) => agentPrimitive.remove(name),
'agent',
name => name
);
}
export function useRemoveGateway() {
return useRemoveResource(
(name: string) => gatewayPrimitive.remove(name),
'gateway',
name => name
);
}
export function useRemoveGatewayTarget() {
return useRemoveResource(
(tool: RemovableGatewayTarget) => gatewayTargetPrimitive.removeGatewayTarget(tool),
'gateway-target',
tool => tool.name
);
}
export function useRemoveMemory() {
return useRemoveResource(
(name: string) => memoryPrimitive.remove(name),
'memory',
name => name
);
}
export function useRemoveIdentity() {
return useRemoveResource(
(name: string) => credentialPrimitive.remove(name),
'credential',
name => name
);
}
export function useRemoveEvaluator() {
return useRemoveResource(
(name: string) => evaluatorPrimitive.remove(name),
'evaluator',
name => name
);
}
export function useRemovePolicyEngine() {
return useRemoveResource(
(name: string) => policyEnginePrimitive.remove(name),
'policy-engine',
name => name
);
}
export function useRemoveOnlineEvalConfig() {
return useRemoveResource(
(name: string) => onlineEvalConfigPrimitive.remove(name),
'online-eval',
name => name
);
}
export function useRemovePolicy() {
return useRemoveResource(
(compositeKey: string) => policyPrimitive.remove(compositeKey),
'policy',
k => k
);
}