forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
135 lines (111 loc) · 3.47 KB
/
Copy pathindex.ts
File metadata and controls
135 lines (111 loc) · 3.47 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
import { ConfigIO } from '../../../lib';
import { randomUUID } from 'crypto';
/**
* Generate a new session ID using UUID v4.
*/
export function generateSessionId(): string {
return randomUUID();
}
export interface SessionInfo {
sessionId: string | undefined;
agentName: string;
runtimeArn: string;
targetName: string;
}
/**
* Get the session ID for an agent from the deployed state.
*/
export async function getSessionId(
agentName: string,
targetName?: string,
configIO: ConfigIO = new ConfigIO()
): Promise<SessionInfo | null> {
const deployedState = await configIO.readDeployedState();
const awsTargets = await configIO.readAWSDeploymentTargets();
// Resolve target
const targetNames = Object.keys(deployedState.targets);
if (targetNames.length === 0) {
return null;
}
const selectedTargetName = targetName ?? targetNames[0]!;
const targetState = deployedState.targets[selectedTargetName];
const targetConfig = awsTargets.find(t => t.name === selectedTargetName);
if (!targetConfig || !targetState?.resources?.runtimes) {
return null;
}
const agentState = targetState.resources.runtimes[agentName];
if (!agentState) {
return null;
}
return {
sessionId: agentState.sessionId,
agentName,
runtimeArn: agentState.runtimeArn,
targetName: selectedTargetName,
};
}
/**
* Save a session ID for an agent to the deployed state.
*/
export async function saveSessionId(
agentName: string,
sessionId: string,
targetName?: string,
configIO: ConfigIO = new ConfigIO()
): Promise<void> {
const deployedState = await configIO.readDeployedState();
// Resolve target
const targetNames = Object.keys(deployedState.targets);
if (targetNames.length === 0) {
throw new Error('No deployed targets found');
}
const selectedTargetName = targetName ?? targetNames[0]!;
const targetState = deployedState.targets[selectedTargetName];
if (!targetState?.resources?.runtimes?.[agentName]) {
throw new Error(`Agent '${agentName}' not found in deployed state`);
}
// Update the session ID
targetState.resources.runtimes[agentName].sessionId = sessionId;
await configIO.writeDeployedState(deployedState);
}
/**
* Clear the session ID for an agent from the deployed state.
*/
export async function clearSessionId(
agentName: string,
targetName?: string,
configIO: ConfigIO = new ConfigIO()
): Promise<void> {
const deployedState = await configIO.readDeployedState();
// Resolve target
const targetNames = Object.keys(deployedState.targets);
if (targetNames.length === 0) {
return;
}
const selectedTargetName = targetName ?? targetNames[0]!;
const targetState = deployedState.targets[selectedTargetName];
if (!targetState?.resources?.runtimes?.[agentName]) {
return;
}
// Clear the session ID
delete targetState.resources.runtimes[agentName].sessionId;
await configIO.writeDeployedState(deployedState);
}
/**
* Get or create a session ID for an agent.
* If a session ID exists in the deployed state, returns it.
* Otherwise, generates a new one and saves it.
*/
export async function getOrCreateSessionId(
agentName: string,
targetName?: string,
configIO: ConfigIO = new ConfigIO()
): Promise<string> {
const sessionInfo = await getSessionId(agentName, targetName, configIO);
if (sessionInfo?.sessionId) {
return sessionInfo.sessionId;
}
const newSessionId = generateSessionId();
await saveSessionId(agentName, newSessionId, targetName, configIO);
return newSessionId;
}