Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
606 changes: 302 additions & 304 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { registerLogs } from './commands/logs';
import { registerPackage } from './commands/package';
import { registerRemove } from './commands/remove';
import { registerStatus } from './commands/status';
import { registerTraces } from './commands/traces';
import { registerUpdate } from './commands/update';
import { registerValidate } from './commands/validate';
import { PACKAGE_VERSION } from './constants';
Expand Down Expand Up @@ -134,6 +135,7 @@ export function registerCommands(program: Command) {
registerPackage(program);
registerRemove(program);
registerStatus(program);
registerTraces(program);
registerUpdate(program);
registerValidate(program);
}
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export { registerInvoke } from './invoke';
export { registerPackage } from './package';
export { registerRemove } from './remove';
export { registerStatus } from './status';
export { registerTraces } from './traces';
export { registerUpdate } from './update';
4 changes: 2 additions & 2 deletions src/cli/commands/logs/__tests__/action.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { detectMode, formatLogLine, resolveAgentContext } from '../action';
import type { LogsContext } from '../action';
import type { DeployedProjectConfig } from '../action';
import { describe, expect, it } from 'vitest';

describe('detectMode', () => {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('formatLogLine', () => {

describe('resolveAgentContext', () => {
// Use 'as any' to avoid branded type issues with FilePath/DirectoryPath
const makeContext = (overrides?: Partial<LogsContext>): LogsContext => ({
const makeContext = (overrides?: Partial<DeployedProjectConfig>): DeployedProjectConfig => ({
project: {
name: 'TestProject',
version: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/logs/__tests__/time-parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseTimeString } from '../time-parser';
import { parseTimeString } from '../../../../lib/utils/time-parser';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

describe('parseTimeString', () => {
Expand Down
98 changes: 17 additions & 81 deletions src/cli/commands/logs/action.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { ConfigIO } from '../../../lib';
import type { AgentCoreProjectSpec, AwsDeploymentTargets, DeployedState } from '../../../schema';
import { parseTimeString } from '../../../lib/utils';
import { searchLogs, streamLogs } from '../../aws/cloudwatch';
import { DEFAULT_ENDPOINT_NAME } from '../../constants';
import type { DeployedProjectConfig } from '../../operations/resolve-agent';
import { loadDeployedProjectConfig, resolveAgent } from '../../operations/resolve-agent';
import { VALID_LEVELS, buildFilterPattern } from './filter-pattern';
import { parseTimeString } from './time-parser';
import type { LogsOptions } from './types';

export interface LogsContext {
project: AgentCoreProjectSpec;
deployedState: DeployedState;
awsTargets: AwsDeploymentTargets;
}
export type { DeployedProjectConfig };

export interface AgentContext {
agentId: string;
Expand All @@ -25,17 +22,6 @@ export interface LogsResult {
error?: string;
}

/**
* Loads configuration required for logs
*/
export async function loadLogsConfig(configIO: ConfigIO = new ConfigIO()): Promise<LogsContext> {
return {
project: await configIO.readProjectSpec(),
deployedState: await configIO.readDeployedState(),
awsTargets: await configIO.readAWSDeploymentTargets(),
};
}

/**
* Detect whether to stream or search based on options
*/
Expand All @@ -61,73 +47,23 @@ export function formatLogLine(event: { timestamp: number; message: string }, jso
* Resolve agent context from config + options
*/
export function resolveAgentContext(
context: LogsContext,
context: DeployedProjectConfig,
options: LogsOptions
): { success: true; agentContext: AgentContext } | { success: false; error: string } {
const { project, deployedState, awsTargets } = context;

if (project.agents.length === 0) {
return { success: false, error: 'No agents defined in agentcore.json' };
}

// Resolve agent
const agentNames = project.agents.map(a => a.name);

if (!options.agent && project.agents.length > 1) {
return {
success: false,
error: `Multiple agents found. Use --agent to specify one: ${agentNames.join(', ')}`,
};
const result = resolveAgent(context, options);
if (!result.success) {
return { success: false, error: result.error };
}

const agentSpec = options.agent ? project.agents.find(a => a.name === options.agent) : project.agents[0];

if (options.agent && !agentSpec) {
return {
success: false,
error: `Agent '${options.agent}' not found. Available: ${agentNames.join(', ')}`,
};
}

if (!agentSpec) {
return { success: false, error: 'No agents defined in agentcore.json' };
}

// Resolve target
const targetNames = Object.keys(deployedState.targets);
if (targetNames.length === 0) {
return { success: false, error: 'No deployed targets found. Run `agentcore deploy` first.' };
}
const selectedTargetName = targetNames[0]!;

const targetState = deployedState.targets[selectedTargetName];
const targetConfig = awsTargets.find(t => t.name === selectedTargetName);

if (!targetConfig) {
return { success: false, error: `Target config '${selectedTargetName}' not found in aws-targets` };
}

// Get the deployed state for this specific agent
const agentState = targetState?.resources?.agents?.[agentSpec.name];

if (!agentState) {
return {
success: false,
error: `Agent '${agentSpec.name}' is not deployed to target '${selectedTargetName}'. Run 'agentcore deploy' first.`,
};
}

const agentId = agentState.runtimeId;
const endpointName = 'DEFAULT';
const logGroupName = `/aws/bedrock-agentcore/runtimes/${agentId}-${endpointName}`;

const { agent } = result;
const endpointName = DEFAULT_ENDPOINT_NAME;
const logGroupName = `/aws/bedrock-agentcore/runtimes/${agent.runtimeId}-${endpointName}`;
return {
success: true,
agentContext: {
agentId,
agentName: agentSpec.name,
accountId: targetConfig.account,
region: targetConfig.region,
agentId: agent.runtimeId,
agentName: agent.agentName,
accountId: agent.accountId,
region: agent.region,
endpointName,
logGroupName,
},
Expand All @@ -146,7 +82,7 @@ export async function handleLogs(options: LogsOptions): Promise<LogsResult> {
};
}

const context = await loadLogsConfig();
const context = await loadDeployedProjectConfig();
const resolution = resolveAgentContext(context, options);

if (!resolution.success) {
Expand Down
130 changes: 130 additions & 0 deletions src/cli/commands/traces/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { parseTimeString } from '../../../lib/utils';
import type { DeployedProjectConfig } from '../../operations/resolve-agent';
import { resolveAgent } from '../../operations/resolve-agent';
import { buildTraceConsoleUrl, getTrace, listTraces } from '../../operations/traces';
import type { TracesGetOptions, TracesListOptions } from './types';

export interface TracesListResult {
success: boolean;
agentName?: string;
targetName?: string;
consoleUrl?: string;
traces?: { traceId: string; timestamp: string; sessionId?: string }[];
error?: string;
}

export async function handleTracesList(
context: DeployedProjectConfig,
options: TracesListOptions
): Promise<TracesListResult> {
const resolved = resolveAgent(context, options);
if (!resolved.success) {
return { success: false, error: resolved.error };
}

const { agent } = resolved;

const consoleUrl = buildTraceConsoleUrl({
region: agent.region,
accountId: agent.accountId,
runtimeId: agent.runtimeId,
agentName: agent.agentName,
});

const limit = options.limit ? parseInt(options.limit, 10) : 20;
if (isNaN(limit)) {
return { success: false, error: '--limit must be a number' };
}

// Parse time options
let startTime: number | undefined;
let endTime: number | undefined;
if (options.since) {
startTime = parseTimeString(options.since);
}
if (options.until) {
endTime = parseTimeString(options.until);
}

const result = await listTraces({
region: agent.region,
runtimeId: agent.runtimeId,
agentName: agent.agentName,
limit,
startTime,
endTime,
});

if (!result.success) {
return { success: false, error: result.error, consoleUrl };
}

return {
success: true,
agentName: agent.agentName,
targetName: agent.targetName,
consoleUrl,
traces: result.traces,
};
}

export interface TracesGetResult {
success: boolean;
agentName?: string;
targetName?: string;
consoleUrl?: string;
filePath?: string;
error?: string;
}

export async function handleTracesGet(
context: DeployedProjectConfig,
traceId: string,
options: TracesGetOptions
): Promise<TracesGetResult> {
const resolved = resolveAgent(context, options);
if (!resolved.success) {
return { success: false, error: resolved.error };
}

const { agent } = resolved;

const consoleUrl = buildTraceConsoleUrl({
region: agent.region,
accountId: agent.accountId,
runtimeId: agent.runtimeId,
agentName: agent.agentName,
});

// Parse time options
let startTime: number | undefined;
let endTime: number | undefined;
if (options.since) {
startTime = parseTimeString(options.since);
}
if (options.until) {
endTime = parseTimeString(options.until);
}

const result = await getTrace({
region: agent.region,
runtimeId: agent.runtimeId,
agentName: agent.agentName,
traceId,
outputPath: options.output,
startTime,
endTime,
});

if (!result.success) {
return { success: false, error: result.error, consoleUrl };
}

return {
success: true,
agentName: agent.agentName,
targetName: agent.targetName,
consoleUrl,
filePath: result.filePath,
};
}
Loading
Loading