-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcommand.tsx
More file actions
212 lines (185 loc) · 8.08 KB
/
command.tsx
File metadata and controls
212 lines (185 loc) · 8.08 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
import { getWorkingDirectory } from '../../../lib';
import { getErrorMessage } from '../../errors';
import { getDevSupportedAgents, invokeAgent, invokeAgentStreaming, loadProjectConfig } from '../../operations/dev';
import { FatalError } from '../../tui/components';
import { LayoutProvider } from '../../tui/context';
import { COMMAND_DESCRIPTIONS } from '../../tui/copy';
import { requireProject } from '../../tui/guards';
import type { Command } from '@commander-js/extra-typings';
import { Text, render } from 'ink';
import React from 'react';
// Alternate screen buffer - same as main TUI
const ENTER_ALT_SCREEN = '\x1B[?1049h\x1B[H';
const EXIT_ALT_SCREEN = '\x1B[?1049l';
const SHOW_CURSOR = '\x1B[?25h';
async function invokeDevServer(port: number, prompt: string, stream: boolean): Promise<void> {
try {
if (stream) {
// Stream response to stdout
for await (const chunk of invokeAgentStreaming(port, prompt)) {
process.stdout.write(chunk);
}
process.stdout.write('\n');
} else {
const response = await invokeAgent(port, prompt);
console.log(response);
}
} catch (err) {
if (err instanceof Error && err.message.includes('ECONNREFUSED')) {
console.error(`Error: Dev server not running on port ${port}`);
console.error('Start it with: agentcore dev');
} else {
console.error(`Error: ${err instanceof Error ? err.message : String(err)}`);
}
process.exit(1);
}
}
export const registerDev = (program: Command) => {
program
.command('dev')
.alias('d')
.description(COMMAND_DESCRIPTIONS.dev)
.option('-p, --port <port>', 'Port for development server', '8080')
.option('-a, --agent <name>', 'Agent to run or invoke (required if multiple agents)')
.option('-i, --invoke <prompt>', 'Invoke running dev server (use --agent if multiple) [non-interactive]')
.option('-s, --stream', 'Stream response when using --invoke [non-interactive]')
.option('-l, --logs', 'Run dev server with logs to stdout [non-interactive]')
.action(async opts => {
try {
const port = parseInt(opts.port, 10);
// If --invoke provided, call the dev server and exit
if (opts.invoke) {
const { getAgentPort } = await import('../../operations/dev');
const invokeProject = await loadProjectConfig(getWorkingDirectory());
// Determine which agent/port to invoke
let invokePort = port;
let targetAgent = invokeProject?.agents[0];
if (opts.agent && invokeProject) {
invokePort = getAgentPort(invokeProject, opts.agent, port);
targetAgent = invokeProject.agents.find(a => a.name === opts.agent);
} else if (invokeProject && invokeProject.agents.length > 1 && !opts.agent) {
const names = invokeProject.agents.map(a => a.name).join(', ');
console.error(`Error: Multiple agents found. Use --agent to specify which one.`);
console.error(`Available: ${names}`);
process.exit(1);
}
// Show model info if available
if (targetAgent?.modelProvider) {
console.log(`Provider: ${targetAgent.modelProvider}`);
}
await invokeDevServer(invokePort, opts.invoke, opts.stream ?? false);
return;
}
requireProject();
const workingDir = getWorkingDirectory();
const project = await loadProjectConfig(workingDir);
if (!project) {
render(<FatalError message="No agentcore project found." suggestedCommand="agentcore create" />);
process.exit(1);
}
if (!project.agents || project.agents.length === 0) {
render(<FatalError message="No agents defined in project." suggestedCommand="agentcore add agent" />);
process.exit(1);
}
const supportedAgents = getDevSupportedAgents(project);
if (supportedAgents.length === 0) {
render(
<FatalError message="No agents support dev mode. Dev mode requires Python agents with an entrypoint." />
);
process.exit(1);
}
// If --logs provided, run non-interactive mode
if (opts.logs) {
const { findAvailablePort, getDevConfig, getAgentPort, spawnDevServer } =
await import('../../operations/dev');
const { findConfigRoot, readEnvFile } = await import('../../../lib');
const { ExecLogger } = await import('../../logging');
// Require --agent if multiple agents
if (project.agents.length > 1 && !opts.agent) {
const names = project.agents.map(a => a.name).join(', ');
console.error(`Error: Multiple agents found. Use --agent to specify which one.`);
console.error(`Available: ${names}`);
process.exit(1);
}
const agentName = opts.agent ?? project.agents[0]?.name;
const configRoot = findConfigRoot(workingDir);
const envVars = configRoot ? await readEnvFile(configRoot) : {};
const config = getDevConfig(workingDir, project, configRoot ?? undefined, agentName);
if (!config) {
console.error('Error: No dev-supported agents found.');
process.exit(1);
}
// Create logger for log file path
const logger = new ExecLogger({ command: 'dev' });
// Calculate port based on agent index
const basePort = getAgentPort(project, config.agentName, port);
const actualPort = await findAvailablePort(basePort);
if (actualPort !== basePort) {
console.log(`Port ${basePort} in use, using ${actualPort}`);
}
// Get provider info from agent config
const targetAgent = project.agents.find(a => a.name === config.agentName);
const providerInfo = targetAgent?.modelProvider ?? '(see agent code)';
console.log(`Starting dev server...`);
console.log(`Agent: ${config.agentName}`);
console.log(`Provider: ${providerInfo}`);
console.log(`Server: http://localhost:${actualPort}/invocations`);
console.log(`Log: ${logger.getRelativeLogPath()}`);
console.log(`Press Ctrl+C to stop\n`);
const child = spawnDevServer({
module: config.module,
cwd: config.directory,
port: actualPort,
isPython: config.isPython,
envVars,
callbacks: {
onLog: (level, msg) => {
const prefix = level === 'error' ? '❌' : level === 'warn' ? '⚠️' : '→';
console.log(`${prefix} ${msg}`);
logger.log(msg, level === 'error' ? 'error' : 'info');
},
onExit: code => {
console.log(`\nServer exited with code ${code ?? 0}`);
logger.finalize(code === 0);
process.exit(code ?? 0);
},
},
});
// Handle Ctrl+C
process.on('SIGINT', () => {
console.log('\nStopping server...');
child?.kill('SIGTERM');
});
// Keep process alive
// eslint-disable-next-line @typescript-eslint/no-empty-function
await new Promise(() => {});
}
// Enter alternate screen buffer for fullscreen mode
process.stdout.write(ENTER_ALT_SCREEN);
const exitAltScreen = () => {
process.stdout.write(EXIT_ALT_SCREEN);
process.stdout.write(SHOW_CURSOR);
};
const { DevScreen } = await import('../../tui/screens/dev/DevScreen');
const { unmount, waitUntilExit } = render(
<LayoutProvider>
<DevScreen
onBack={() => {
exitAltScreen();
unmount();
process.exit(0);
}}
workingDir={workingDir}
port={port}
agentName={opts.agent}
/>
</LayoutProvider>
);
await waitUntilExit();
exitAltScreen();
} catch (error) {
render(<Text color="red">Error: {getErrorMessage(error)}</Text>);
process.exit(1);
}
});
};