forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-mode.ts
More file actions
366 lines (329 loc) · 13.5 KB
/
Copy pathbrowser-mode.ts
File metadata and controls
366 lines (329 loc) · 13.5 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { ConfigIO, findConfigRoot, getWorkingDirectory } from '../../../lib';
import type { AgentCoreProjectSpec } from '../../../schema';
import { ANSI } from '../../constants';
import { isDeploySkippable } from '../../operations/deploy/change-detection';
import { getDevConfig, getDevSupportedAgents, loadDevEnv, loadProjectConfig } from '../../operations/dev';
import { type OtelCollector, startOtelCollector } from '../../operations/dev/otel';
import {
type AgentInfo,
type ListMemoryRecordsHandler,
type RetrieveMemoryRecordsHandler,
runWebUI,
} from '../../operations/dev/web-ui';
import type { HarnessInfo } from '../../operations/dev/web-ui/constants';
import { listMemoryRecords, retrieveMemoryRecords } from '../../operations/memory';
import { loadDeployedProjectConfig, resolveAgentOrHarness } from '../../operations/resolve-agent';
import { fetchTraceRecords, listTraces } from '../../operations/traces';
import { LayoutProvider } from '../../tui/context';
import { requireTTY } from '../../tui/guards';
import { runCliDeploy } from '../deploy/progress';
import { render } from 'ink';
import path from 'node:path';
import React from 'react';
interface DeployedHandlers {
onListMemoryRecords?: ListMemoryRecordsHandler;
onRetrieveMemoryRecords?: RetrieveMemoryRecordsHandler;
}
/**
* Resolve deployed resources (memories, agents) from config and return handlers
* that query them via the AWS SDK. Only resources with "deployed" status are available.
*/
async function resolveDeployedHandlers(
baseDir: string,
onLog: (level: 'info' | 'warn' | 'error', msg: string) => void
): Promise<DeployedHandlers> {
const configIO = new ConfigIO({ baseDir });
if (!configIO.configExists('state') || !configIO.configExists('awsTargets')) {
return {};
}
try {
const deployedState = await configIO.readDeployedState();
const awsTargets = await configIO.readAWSDeploymentTargets();
const targetName = Object.keys(deployedState.targets)[0];
if (!targetName) return {};
const targetState = deployedState.targets[targetName];
const targetConfig = awsTargets.find(t => t.name === targetName);
if (!targetConfig) return {};
const region = targetConfig.region;
const result: DeployedHandlers = {};
// Memory handlers
const memoryEntries = targetState?.resources?.memories ?? {};
const memories = Object.entries(memoryEntries).map(([name, state]) => ({
name,
memoryId: state.memoryId,
region,
}));
if (memories.length > 0) {
onLog(
'info',
`Memory browsing enabled for ${memories.length} deployed memory(ies): ${memories.map(m => m.name).join(', ')}`
);
result.onListMemoryRecords = async args => {
const memory = memories.find(m => m.name === args.memoryName);
if (!memory) return { success: false, error: `Memory "${args.memoryName}" not found in deployed state` };
const res = await listMemoryRecords({
region: memory.region,
memoryId: memory.memoryId,
memoryStrategyId: args.strategyId,
...(args.namespace ? { namespace: args.namespace } : { namespacePath: args.namespacePath! }),
});
return res.success ? res : { success: false as const, error: res.error.message };
};
result.onRetrieveMemoryRecords = async args => {
const memory = memories.find(m => m.name === args.memoryName);
if (!memory) return { success: false, error: `Memory "${args.memoryName}" not found in deployed state` };
const res = await retrieveMemoryRecords({
region: memory.region,
memoryId: memory.memoryId,
searchQuery: args.searchQuery,
memoryStrategyId: args.strategyId,
...(args.namespace ? { namespace: args.namespace } : { namespacePath: args.namespacePath! }),
});
return res.success ? res : { success: false as const, error: res.error.message };
};
}
return result;
} catch (err) {
onLog('warn', `Could not resolve deployed resources: ${err instanceof Error ? err.message : String(err)}`);
return {};
}
}
export interface BrowserModeOptions {
workingDir: string;
project: AgentCoreProjectSpec;
port: number;
agentName?: string;
harnessName?: string;
/** OTEL env vars to pass to dev servers (set by the dev command when collector is active) */
otelEnvVars?: Record<string, string>;
/** OTEL collector instance for local trace collection */
collector?: OtelCollector;
}
/**
* Standalone entry point for launching browser dev mode from the TUI.
* Handles all setup (project loading, OTEL collector, etc.) internally.
*/
export async function launchBrowserDev(): Promise<void> {
const workingDir = getWorkingDirectory();
const project = await loadProjectConfig(workingDir);
if (!project) {
console.error('Error: No agents or harnesses defined in project.');
process.exit(1);
}
const hasRuntimes = project.runtimes.length > 0;
const hasHarnesses = (project.harnesses ?? []).length > 0;
if (!hasRuntimes && !hasHarnesses) {
console.error('Error: No agents or harnesses defined in project.');
process.exit(1);
}
// Only auto-deploy for harness-only projects, and skip if no CDK changes
if (hasHarnesses && !hasRuntimes && !(await isDeploySkippable())) {
await runCliDeploy();
}
const configRoot = findConfigRoot(workingDir);
const persistTracesDir = path.join(configRoot ?? workingDir, '.cli', 'traces');
const { collector, otelEnvVars } = await startOtelCollector(persistTracesDir);
await runBrowserMode({
workingDir,
project,
port: 8080,
otelEnvVars,
collector,
});
}
export async function runBrowserMode(opts: BrowserModeOptions): Promise<void> {
const { workingDir, project, agentName, harnessName, otelEnvVars = {}, collector } = opts;
const configRoot = findConfigRoot(workingDir);
// Browser mode serves multiple agents; we don't know which agent will be
// launched until the user picks one in the web UI. Pass no runtime so
// payment env vars are emitted for any deployed manager and the spawned
// agent decides whether to consume them. Single-agent CLI dev correctly
// narrows by runtime via loadDevEnv(workingDir, runtime) elsewhere.
const { envVars } = await loadDevEnv(workingDir);
const supportedAgents = getDevSupportedAgents(project);
const projectHasHarnesses = (project.harnesses ?? []).length > 0;
if (supportedAgents.length === 0 && !projectHasHarnesses) {
console.error('Error: No dev-supported agents found.');
process.exit(1);
}
if (agentName && !supportedAgents.some(a => a.name === agentName)) {
console.error(`Error: Agent "${agentName}" not found or does not support dev mode.`);
process.exit(1);
}
const onLog = (level: 'info' | 'warn' | 'error', msg: string) => {
if (level === 'error') console.error(`Web UI: ${msg}`);
};
const mergedEnvVars = { ...envVars, ...otelEnvVars };
const agentInfoList: AgentInfo[] = supportedAgents.map(a => ({
name: a.name,
buildType: a.build,
protocol: a.protocol ?? 'HTTP',
}));
// Resolve deployed resources (memories, agents) so memory browsing and
// CloudWatch traces work in dev mode alongside local traces.
// Handlers re-resolve on each call so newly deployed memories are picked up.
const baseDir = configRoot ?? workingDir;
// Discover deployed harnesses from project config + deployed state
const harnessInfoList: HarnessInfo[] = [];
try {
const configIO = new ConfigIO({ baseDir });
if (configIO.configExists('state') && configIO.configExists('awsTargets')) {
const deployedState = await configIO.readDeployedState();
const awsTargets = await configIO.readAWSDeploymentTargets();
const targetName = Object.keys(deployedState.targets)[0];
if (targetName) {
const targetState = deployedState.targets[targetName];
const targetConfig = awsTargets.find(t => t.name === targetName);
if (targetConfig) {
for (const harness of project.harnesses ?? []) {
const state = targetState?.resources?.harnesses?.[harness.name];
if (state) {
harnessInfoList.push({
name: harness.name,
harnessArn: state.harnessArn,
region: targetConfig.region,
});
}
}
if (harnessInfoList.length > 0) {
onLog(
'info',
`Found ${harnessInfoList.length} deployed harness(es): ${harnessInfoList.map(h => h.name).join(', ')}`
);
}
}
}
}
} catch {
// Harness discovery is best-effort — local dev works without it
}
await runWebUI({
logLabel: 'dev',
onLog,
serverOptions: {
mode: 'dev',
agents: agentInfoList,
harnesses: harnessInfoList,
selectedAgent: agentName,
selectedHarness: harnessName,
envVars: mergedEnvVars,
getEnvVars: async () => {
const { envVars: freshEnvVars } = await loadDevEnv(workingDir);
return { ...freshEnvVars, ...otelEnvVars };
},
configRoot: configRoot ?? undefined,
getDevConfig: async name => {
const freshProject = await loadProjectConfig(workingDir);
return getDevConfig(workingDir, freshProject, configRoot ?? undefined, name);
},
reloadAgents: configRoot
? async () => {
const freshProject = await loadProjectConfig(workingDir);
return getDevSupportedAgents(freshProject).map(a => ({
name: a.name,
buildType: a.build,
protocol: a.protocol ?? 'HTTP',
}));
}
: undefined,
onListTraces: collector
? (agentNameParam, startTime, endTime) => collector.listTraces(agentNameParam, startTime, endTime)
: undefined,
onGetTrace: collector ? (agentNameParam, traceId) => collector.getTraceSpans(agentNameParam, traceId) : undefined,
onListCloudWatchTraces: async (agentName, harnessName, startTime, endTime) => {
try {
const configIO = new ConfigIO({ baseDir });
const context = await loadDeployedProjectConfig(configIO);
const resolved = await resolveAgentOrHarness(context, { runtime: agentName, harness: harnessName });
if (!resolved.success) return { success: false, error: resolved.error };
const res = await listTraces({
region: resolved.agent.region,
runtimeId: resolved.agent.runtimeId,
agentName: resolved.agent.agentName,
startTime,
endTime,
});
return res.success ? res : { success: false as const, error: res.error.message };
} catch (err) {
return {
success: false,
error: `Failed to list CloudWatch traces: ${err instanceof Error ? err.message : String(err)}`,
};
}
},
onGetCloudWatchTrace: async (agentName, harnessName, traceId, startTime, endTime) => {
try {
const configIO = new ConfigIO({ baseDir });
const context = await loadDeployedProjectConfig(configIO);
const resolved = await resolveAgentOrHarness(context, { runtime: agentName, harness: harnessName });
if (!resolved.success) return { success: false, error: resolved.error };
const res = await fetchTraceRecords({
region: resolved.agent.region,
runtimeId: resolved.agent.runtimeId,
traceId,
startTime,
endTime,
includeSpans: true,
});
return res.success ? res : { success: false as const, error: res.error.message };
} catch (err) {
return {
success: false,
error: `Failed to get CloudWatch trace: ${err instanceof Error ? err.message : String(err)}`,
};
}
},
onListMemoryRecords: async args => {
const deployed = await resolveDeployedHandlers(baseDir, onLog);
if (!deployed.onListMemoryRecords) return { success: false, error: 'No deployed AgentCore Memory found' };
return deployed.onListMemoryRecords(args);
},
onRetrieveMemoryRecords: async args => {
const deployed = await resolveDeployedHandlers(baseDir, onLog);
if (!deployed.onRetrieveMemoryRecords) return { success: false, error: 'No deployed AgentCore Memory found' };
return deployed.onRetrieveMemoryRecords(args);
},
},
});
}
const { enterAltScreen: ENTER_ALT_SCREEN, exitAltScreen: EXIT_ALT_SCREEN, showCursor: SHOW_CURSOR } = ANSI;
interface TuiPickerResult {
agentName?: string;
harnessName?: string;
}
export async function launchTuiDevScreenWithPicker(
workingDir: string,
options?: { skipDeploy?: boolean }
): Promise<TuiPickerResult | undefined> {
requireTTY();
process.stdout.write(ENTER_ALT_SCREEN);
const exitAltScreen = () => {
process.stdout.write(EXIT_ALT_SCREEN);
process.stdout.write(SHOW_CURSOR);
};
let pickerResult: TuiPickerResult | undefined;
const { DevScreen } = await import('../../tui/screens/dev/DevScreen');
const { unmount, waitUntilExit } = render(
React.createElement(
LayoutProvider,
null,
React.createElement(DevScreen, {
onBack: () => {
exitAltScreen();
unmount();
process.exit(0);
},
workingDir,
skipDeploy: options?.skipDeploy,
onLaunchBrowser: (selection?: { agentName?: string; harnessName?: string }) => {
pickerResult = selection ?? {};
exitAltScreen();
unmount();
},
})
)
);
await waitUntilExit();
exitAltScreen();
return pickerResult;
}