Skip to content

Commit

Permalink
Refactor tools
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTraub committed Mar 19, 2024
1 parent 11549a9 commit 3ab88f4
Showing 1 changed file with 80 additions and 77 deletions.
157 changes: 80 additions & 77 deletions javascriptv3/example_code/bedrock-runtime/tools/user_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,99 @@

import * as readline from "readline";

function askAgain(askQuestion, newQuestion) {
readline.moveCursor(process.stdout, 0, -1);
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
askQuestion(newQuestion);
}

function createInterface() {
return readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
/**
* @typedef {Object} FoundationModel
* @property {string} modelName - The name of the model
*/

/**
* @param {FoundationModel[]} models - An array of models to choose from
* @returns {Promise<FoundationModel>} - A Promise that resolves with the selected model
*/
export const selectModel = (models) => {
return new Promise(resolve => {
const rl = createInterface();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const ask = (question, validate, onValid, repeat) => {
if (repeat) {
// Overwrite previous line
readline.moveCursor(process.stdout, 0, -1);
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
}

const printOptions = () => {
models.forEach((model, index) => {
console.log(`${index + 1}. ${model.modelName}`);
});
};
rl.question(question, (answer) => {
if (validate(answer)) {
onValid(answer);
} else {
ask(question, validate, onValid, true);
}
});
};

const askForModel = (question) => {
rl.question(question, answer => {
if (answer === "q") {
rl.close();
resolve(null);
}
else {
const selectedIndex = parseInt(answer, 10) - 1;
if (selectedIndex >= 0 && selectedIndex < models.length) {
rl.close();
resolve(models[selectedIndex]);
} else {
askAgain(askForModel, "Invalid input. Please enter a valid number (q to quit): ");
}
}
});
};
const select = (
/** @type {string[]} options */ options,
text,
validate,
onValid,
) => {
// Print the options
options.forEach((option, index) => {
console.log(`${index + 1}. ${option}`);
});

printOptions();
askForModel("Select a model: (q to quit): ");
});
ask(text, validate, onValid, false);
};

export const askForPrompt = () => {
return new Promise(resolve => {
const rl = createInterface();
export const selectModel = (/** @type {FoundationModel[]} */ models) => {
return new Promise((resolve) => {
const validate = (answer) => {
if (answer === "q") return true;
else {
const selectedIndex = parseInt(answer, 10) - 1;
return selectedIndex >= 0 && selectedIndex < models.length;
}
};

const askForPrompt = (question) => {
rl.question(question, answer => {
if (answer.trim() === "") {
askAgain(askForPrompt, "Invalid input. Please enter a prompt: ");
} else {
rl.close();
resolve(answer);
}
});
};
askForPrompt("Now, enter your prompt: ");
});
const onValid = (answer) => {
if (answer === "q") {
rl.close();
resolve(null);
} else {
resolve(models[parseInt(answer, 10) - 1]);
}
};

const text = "Select a model number (q to quit): ";
select(
models.map((m) => m.modelName),
text,
validate,
onValid,
);
});
};

export const askForChoice = () => {
return new Promise((resolve) => {
const rl = createInterface();
export const selectNextStep = (/** @type {string} */ modelName) => {
return new Promise((resolve) => {
const options = [`Prompt ${modelName} again`, "Select another model"];
const text = "Choose your next step (q to quit): ";

const validate = (answer) => ["1", "2", "q"].includes(answer);
const onValid = (answer) => {
if (answer === "q") {
rl.close();
resolve(null);
} else {
resolve(answer);
}
};

const askForChoice = (question) => {
rl.question(question, (answer) => {
if (["1", "2", "q"].includes(answer)) {
rl.close();
resolve(answer);
} else {
askAgain(askForChoice, "Invalid input. Please enter 1, 2, or q: ");
}
});
};
select(options, text, validate, onValid);
});
};

askForChoice(
"Enter 1 for a new prompt to the same model, 2 for a different model, or q to quit: "
);
});
export const askForPrompt = () => {
return new Promise((resolve) => {
const validate = (/** @type {string} */ answer) => answer.trim() !== "";
const onValid = (answer) => resolve(answer);
ask("Now, enter your prompt: ", validate, onValid);
});
};

0 comments on commit 3ab88f4

Please sign in to comment.