Skip to content

Commit 4ae6a76

Browse files
committed
central user prompts with consistent formatting into a function.
1 parent 75f214e commit 4ae6a76

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

packages/agent/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ export * from './utils/errorToString.js';
3636
export * from './utils/logger.js';
3737
export * from './utils/mockLogger.js';
3838
export * from './utils/stringifyLimited.js';
39+
export * from './utils/userPrompt.js';

packages/agent/src/tools/interaction/userPrompt.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import * as readline from 'readline';
2-
3-
import chalk from 'chalk';
41
import { z } from 'zod';
52
import { zodToJsonSchema } from 'zod-to-json-schema';
63

74
import { Tool } from '../../core/types.js';
5+
import { userPrompt } from '../../utils/userPrompt.js';
86

97
const parameterSchema = z.object({
108
prompt: z.string().describe('The prompt message to display to the user'),
@@ -23,26 +21,10 @@ export const userPromptTool: Tool<Parameters, ReturnType> = {
2321
execute: async ({ prompt }, { logger }) => {
2422
logger.verbose(`Prompting user with: ${prompt}`);
2523

26-
const rl = readline.createInterface({
27-
input: process.stdin,
28-
output: process.stdout,
29-
terminal: true,
30-
});
31-
32-
// Disable the readline interface's internal input processing
33-
if (rl.terminal) {
34-
process.stdin.setRawMode(false);
35-
}
36-
37-
const response = await new Promise<string>((resolve) => {
38-
rl.question(chalk.green(prompt + ' '), (answer) => {
39-
resolve(answer);
40-
});
41-
});
24+
const response = await userPrompt(prompt);
4225

4326
logger.verbose(`Received user response: ${response}`);
4427

45-
rl.close();
4628
return response;
4729
},
4830
logParameters: () => {},
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as readline from 'readline';
2+
3+
import chalk from 'chalk';
4+
5+
export const userPrompt = async (prompt: string): Promise<string> => {
6+
const rl = readline.createInterface({
7+
input: process.stdin,
8+
output: process.stdout,
9+
terminal: true,
10+
});
11+
12+
try {
13+
// Disable the readline interface's internal input processing
14+
if (rl.terminal) {
15+
process.stdin.setRawMode(false);
16+
}
17+
return await new Promise<string>((resolve) => {
18+
rl.question(chalk.green('\n' + prompt + '\n') + '\n> ', (answer) => {
19+
resolve(answer);
20+
});
21+
});
22+
} finally {
23+
rl.close();
24+
}
25+
};

packages/cli/src/commands/$default.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as fs from 'fs/promises';
22
import { createInterface } from 'readline/promises';
33

4-
import chalk from 'chalk';
54
import {
65
toolAgent,
76
Logger,
87
getTools,
98
getAnthropicApiKeyError,
109
TokenLevel,
10+
userPrompt,
1111
} from 'mycoder-agent';
1212

1313
import { SharedOptions } from '../options.js';
@@ -85,21 +85,9 @@ export const command: CommandModule<object, DefaultArgs> = {
8585

8686
// If interactive mode
8787
if (argv.interactive) {
88-
const readline = createInterface({
89-
input: process.stdin,
90-
output: process.stdout,
91-
});
92-
93-
try {
94-
console.log(
95-
chalk.green(
96-
"Type your request below or 'help' for usage information. Use Ctrl+C to exit.",
97-
),
98-
);
99-
prompt = await readline.question('\n> ');
100-
} finally {
101-
readline.close();
102-
}
88+
prompt = await userPrompt(
89+
"Type your request below or 'help' for usage information. Use Ctrl+C to exit.",
90+
);
10391
} else if (!prompt) {
10492
// Use command line prompt if provided
10593
prompt = argv.prompt;

0 commit comments

Comments
 (0)