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
1 change: 1 addition & 0 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export * from './utils/errorToString.js';
export * from './utils/logger.js';
export * from './utils/mockLogger.js';
export * from './utils/stringifyLimited.js';
export * from './utils/userPrompt.js';
22 changes: 2 additions & 20 deletions packages/agent/src/tools/interaction/userPrompt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as readline from 'readline';

import chalk from 'chalk';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';

import { Tool } from '../../core/types.js';
import { userPrompt } from '../../utils/userPrompt.js';

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

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
});

// Disable the readline interface's internal input processing
if (rl.terminal) {
process.stdin.setRawMode(false);
}

const response = await new Promise<string>((resolve) => {
rl.question(chalk.green(prompt + ' '), (answer) => {
resolve(answer);
});
});
const response = await userPrompt(prompt);

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

rl.close();
return response;
},
logParameters: () => {},
Expand Down
16 changes: 16 additions & 0 deletions packages/agent/src/utils/userPrompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createInterface } from 'readline/promises';

import chalk from 'chalk';

export const userPrompt = async (prompt: string): Promise<string> => {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});

try {
return await rl.question(chalk.green('\n' + prompt + '\n') + '\n> ');
} finally {
rl.close();
}
};
20 changes: 4 additions & 16 deletions packages/cli/src/commands/$default.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as fs from 'fs/promises';
import { createInterface } from 'readline/promises';

import chalk from 'chalk';
import {
toolAgent,
Logger,
getTools,
getAnthropicApiKeyError,
TokenLevel,
userPrompt,
} from 'mycoder-agent';

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

// If interactive mode
if (argv.interactive) {
const readline = createInterface({
input: process.stdin,
output: process.stdout,
});

try {
console.log(
chalk.green(
"Type your request below or 'help' for usage information. Use Ctrl+C to exit.",
),
);
prompt = await readline.question('\n> ');
} finally {
readline.close();
}
prompt = await userPrompt(
"Type your request below or 'help' for usage information. Use Ctrl+C to exit.",
);
} else if (!prompt) {
// Use command line prompt if provided
prompt = argv.prompt;
Expand Down
Loading