-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathaction.ts
More file actions
153 lines (125 loc) · 4.79 KB
/
action.ts
File metadata and controls
153 lines (125 loc) · 4.79 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
import { ConfigIO } from '../../../lib';
import type { AgentCoreProjectSpec, AwsDeploymentTargets, DeployedState } from '../../../schema';
import { invokeAgentRuntime, invokeAgentRuntimeStreaming } from '../../aws';
import { InvokeLogger } from '../../logging';
import type { InvokeOptions, InvokeResult } from './types';
export interface InvokeContext {
project: AgentCoreProjectSpec;
deployedState: DeployedState;
awsTargets: AwsDeploymentTargets;
}
/**
* Loads configuration required for invocation
*/
export async function loadInvokeConfig(configIO: ConfigIO = new ConfigIO()): Promise<InvokeContext> {
return {
project: await configIO.readProjectSpec(),
deployedState: await configIO.readDeployedState(),
awsTargets: await configIO.readAWSDeploymentTargets(),
};
}
/**
* Main invoke handler
*/
export async function handleInvoke(context: InvokeContext, options: InvokeOptions = {}): Promise<InvokeResult> {
const { project, deployedState, awsTargets } = context;
// 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 = options.targetName ?? targetNames[0]!;
if (options.targetName && !targetNames.includes(options.targetName)) {
return { success: false, error: `Target '${options.targetName}' not found. Available: ${targetNames.join(', ')}` };
}
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` };
}
if (project.agents.length === 0) {
return { success: false, error: 'No agents defined in configuration' };
}
// Resolve agent
const agentNames = project.agents.map(a => a.name);
if (!options.agentName && project.agents.length > 1) {
return { success: false, error: `Multiple agents found. Use --agent to specify one: ${agentNames.join(', ')}` };
}
const agentSpec = options.agentName ? project.agents.find(a => a.name === options.agentName) : project.agents[0];
if (options.agentName && !agentSpec) {
return { success: false, error: `Agent '${options.agentName}' not found. Available: ${agentNames.join(', ')}` };
}
if (!agentSpec) {
return { success: false, error: 'No agents defined in configuration' };
}
if (agentSpec.networkMode === 'VPC') {
console.warn(
'Warning: This agent uses VPC network mode. Invocation may require setting up VPC Endpoints for S3, ECR, Bedrock. If your agent uses a non-Bedrock model provider, VPC will require public internet access.'
);
}
// 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}'` };
}
if (!options.prompt) {
return { success: false, error: 'No prompt provided. Usage: agentcore invoke "your prompt"' };
}
// Get provider info if available
const providerInfo = agentSpec.modelProvider;
// Create logger for this invocation
const logger = new InvokeLogger({
agentName: agentSpec.name,
runtimeArn: agentState.runtimeArn,
region: targetConfig.region,
});
logger.logPrompt(options.prompt, undefined, options.userId);
if (options.stream) {
// Streaming mode
let fullResponse = '';
try {
const result = await invokeAgentRuntimeStreaming({
region: targetConfig.region,
runtimeArn: agentState.runtimeArn,
payload: options.prompt,
sessionId: options.sessionId,
userId: options.userId,
logger, // Pass logger for SSE event debugging
});
for await (const chunk of result.stream) {
fullResponse += chunk;
process.stdout.write(chunk);
}
process.stdout.write('\n');
logger.logResponse(fullResponse);
return {
success: true,
agentName: agentSpec.name,
targetName: selectedTargetName,
response: fullResponse,
logFilePath: logger.logFilePath,
providerInfo,
};
} catch (err) {
logger.logError(err, 'invoke streaming failed');
throw err;
}
}
// Non-streaming mode
const response = await invokeAgentRuntime({
region: targetConfig.region,
runtimeArn: agentState.runtimeArn,
payload: options.prompt,
sessionId: options.sessionId,
userId: options.userId,
});
logger.logResponse(response.content);
return {
success: true,
agentName: agentSpec.name,
targetName: selectedTargetName,
response: response.content,
logFilePath: logger.logFilePath,
providerInfo,
};
}