Skip to content

Commit 9519fbc

Browse files
committed
feat: add verbose mode
1 parent c2a31b5 commit 9519fbc

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

packages/agent-api/agent-cli.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const agentSystemPrompt = `
2121
## Role
2222
You an expert assistant that helps users with managing burger orders. Use the provided tools to get the information you need and perform actions on behalf of the user.
2323
Only answer to requests that are related to burger orders and the menu. If the user asks for something else, politely inform them that you can only assist with burger orders.
24+
You are invoked from a command line interface.
2425
2526
## Task
2627
Help the user with their request, ask any clarifying questions if needed.
@@ -30,7 +31,7 @@ Help the user with their request, ask any clarifying questions if needed.
3031
- If you get any errors when trying to use a tool that does not seem related to missing parameters, try again
3132
- If you cannot get the information needed to answer the user's question or perform the specified action, inform the user that you are unable to do so. Never make up information.
3233
- The get_burger tool can help you get informations about the burgers
33-
- Creating or cancelling an order requires a \`userId\`: if not provided, ask the user to provide it or to run the CLI with the \`--userId\` option.
34+
- Creating or cancelling an order requires a \`userId\`: if not provided, ask the user to provide it or to run the CLI with the \`--userId\` option. To get its user ID, the user must connect to ${process.env.AGENT_API_URL ?? 'http://localhost:4280 (make sure that agent-webapp is running)'}.
3435
3536
## Output
3637
Your response will be printed to a terminal. Do not use markdown formatting or any other special formatting. Just provide the plain text response.
@@ -40,6 +41,7 @@ interface CliArgs {
4041
question: string;
4142
userId?: string;
4243
isNew: boolean;
44+
verbose: boolean;
4345
}
4446

4547
interface SessionData {
@@ -51,16 +53,18 @@ function parseArgs(): CliArgs {
5153
const args = process.argv.slice(2);
5254

5355
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
54-
console.log('Usage: agent-cli <question> [--userId <userId>] [--new]');
56+
console.log('Usage: agent-cli <question> [--userId <userId>] [--new] [--verbose]');
5557
console.log(' question: Your question about burger orders');
5658
console.log(' --userId: Optional user ID (needed for some tasks)');
5759
console.log(' --new: Start a new session');
60+
console.log(' --verbose: Enable verbose mode to show intermediate steps');
5861
process.exit(0);
5962
}
6063

6164
const questionParts: string[] = [];
6265
let userId: string | undefined;
6366
let isNew = false;
67+
let verbose = false;
6468

6569
for (let i = 0; i < args.length; i++) {
6670
const arg = args[i];
@@ -70,6 +74,8 @@ function parseArgs(): CliArgs {
7074
i++;
7175
} else if (arg === '--new') {
7276
isNew = true;
77+
} else if (arg === '--verbose') {
78+
verbose = true;
7379
} else {
7480
questionParts.push(arg);
7581
}
@@ -82,7 +88,7 @@ function parseArgs(): CliArgs {
8288
process.exit(1);
8389
}
8490

85-
return { question, userId, isNew };
91+
return { question, userId, isNew, verbose };
8692
}
8793

8894
async function getSessionPath(): Promise<string> {
@@ -119,17 +125,19 @@ function convertHistoryToMessages(history: SessionData['history']): BaseMessage[
119125
}
120126

121127
export async function run() {
122-
const { question, userId, isNew } = parseArgs();
128+
const { question, userId, isNew, verbose } = parseArgs();
123129
const azureOpenAiEndpoint = process.env.AZURE_OPENAI_API_ENDPOINT;
124130
const burgerMcpEndpoint = process.env.BURGER_MCP_URL ?? 'http://localhost:3000/mcp';
125131

132+
let client: Client | undefined;
133+
126134
try {
127135
let model: BaseChatModel;
128136

129137
if (!azureOpenAiEndpoint || !burgerMcpEndpoint) {
130138
const errorMessage = 'Missing required environment variables: AZURE_OPENAI_API_ENDPOINT or BURGER_MCP_URL';
131139
console.error(errorMessage);
132-
return;
140+
process.exit(1);
133141
}
134142

135143
let session: SessionData;
@@ -150,7 +158,7 @@ export async function run() {
150158
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
151159
});
152160

153-
const client = new Client({
161+
client = new Client({
154162
name: 'burger-mcp',
155163
version: '1.0.0',
156164
});
@@ -178,7 +186,7 @@ export async function run() {
178186
const agentExecutor = new AgentExecutor({
179187
agent,
180188
tools,
181-
returnIntermediateSteps: false,
189+
returnIntermediateSteps: verbose,
182190
});
183191

184192
const chatHistory = convertHistoryToMessages(session.history);
@@ -188,18 +196,36 @@ export async function run() {
188196
chat_history: chatHistory
189197
});
190198

191-
console.log('----------\n' + response.output);
199+
if (verbose && response.intermediateSteps && response.intermediateSteps.length > 0) {
200+
console.log('--------------------\nIntermediate steps\n--------------------');
201+
for (const [index, step] of response.intermediateSteps.entries()) {
202+
console.log(`*** Step ${index + 1} ***`);
203+
console.log(`Action: ${step.action.tool}`);
204+
console.log(`Input: ${JSON.stringify(step.action.toolInput, null, 2)}`);
205+
console.log(`Output: ${step.observation}`);
206+
}
207+
}
208+
209+
console.log('--------------------\n' + response.output);
192210

193211
session.history.push({ type: 'human', content: question });
194212
session.history.push({ type: 'ai', content: response.output });
195213

196214
await saveSession(session);
197215

198-
console.log('----------\nDone.');
199-
200216
} catch (_error: unknown) {
201217
const error = _error as Error;
202218
console.error(`Error when processing request: ${error.message}`);
219+
process.exitCode = 1;
220+
} finally {
221+
if (client) {
222+
try {
223+
await client.close();
224+
} catch (error) {
225+
console.error('Error closing MCP client:', error);
226+
}
227+
}
228+
process.exitCode = 0;
203229
}
204230
}
205231

0 commit comments

Comments
 (0)