diff --git a/legacy/src/human-feedback/cli.ts b/legacy/src/human-feedback/cli.ts index cacd22d9..d67a5f78 100644 --- a/legacy/src/human-feedback/cli.ts +++ b/legacy/src/human-feedback/cli.ts @@ -17,7 +17,7 @@ export class HumanFeedbackMechanismCLI< /** * Prompt the user to select one of a list of options. */ - protected async askUser( + protected async _askUser( message: string, choices: HumanFeedbackUserActions[] ): Promise { @@ -30,21 +30,21 @@ export class HumanFeedbackMechanismCLI< }) } - protected async edit(output: string): Promise { + protected async _edit(output: string): Promise { return editor({ message: 'Edit the output:', default: output }) } - protected async annotate(): Promise { + protected async _annotate(): Promise { return input({ message: 'Please leave an annotation (leave blank to skip; press enter to submit):' }) } - protected async selectOne( + protected async _selectOne( response: TOutput ): Promise { if (!Array.isArray(response)) { @@ -58,7 +58,7 @@ export class HumanFeedbackMechanismCLI< return select({ message: 'Pick one output:', choices }) } - protected async selectN( + protected async _selectN( response: TOutput ): Promise { if (!Array.isArray(response)) { diff --git a/legacy/src/human-feedback/feedback.ts b/legacy/src/human-feedback/feedback.ts index 7936c18b..5a881dac 100644 --- a/legacy/src/human-feedback/feedback.ts +++ b/legacy/src/human-feedback/feedback.ts @@ -11,7 +11,7 @@ export const HumanFeedbackUserActions = { Edit: 'edit', Decline: 'decline', Select: 'select', - Exit: 'exit' + Abort: 'abort' } as const export type HumanFeedbackUserActions = @@ -25,7 +25,7 @@ export const HumanFeedbackUserActionMessages: Record< [HumanFeedbackUserActions.Edit]: 'Edit the output', [HumanFeedbackUserActions.Decline]: 'Decline the output', [HumanFeedbackUserActions.Select]: 'Select outputs to keep', - [HumanFeedbackUserActions.Exit]: 'Exit' + [HumanFeedbackUserActions.Abort]: 'Abort' } /** @@ -48,9 +48,9 @@ export type HumanFeedbackOptions = { type?: T /** - * Whether the user can bail out of the feedback loop. + * Whether the user can abort the process. */ - bail?: boolean + abort?: boolean /** * Whether the user can edit the output. @@ -148,24 +148,24 @@ export abstract class HumanFeedbackMechanism< this._options = options } - protected abstract selectOne( + protected abstract _selectOne( output: TOutput ): Promise - protected abstract selectN( + protected abstract _selectN( response: TOutput ): Promise - protected abstract annotate(): Promise + protected abstract _annotate(): Promise - protected abstract edit(output: string): Promise + protected abstract _edit(output: string): Promise - protected abstract askUser( + protected abstract _askUser( message: string, choices: HumanFeedbackUserActions[] ): Promise - protected parseEditedOutput(editedOutput: string): any { + protected _parseEditedOutput(editedOutput: string): any { const parsedOutput = JSON.parse(editedOutput) return this._task.outputSchema.parse(parsedOutput) } @@ -196,14 +196,14 @@ export abstract class HumanFeedbackMechanism< choices.push(HumanFeedbackUserActions.Edit) } - if (this._options.bail) { - choices.push(HumanFeedbackUserActions.Exit) + if (this._options.abort) { + choices.push(HumanFeedbackUserActions.Abort) } const choice = choices.length === 1 ? HumanFeedbackUserActions.Select - : await this.askUser(msg, choices) + : await this._askUser(msg, choices) const feedback: Record = {} @@ -213,8 +213,8 @@ export abstract class HumanFeedbackMechanism< break case HumanFeedbackUserActions.Edit: { - const editedOutput = await this.edit(stringified) - feedback.editedOutput = await this.parseEditedOutput(editedOutput) + const editedOutput = await this._edit(stringified) + feedback.editedOutput = await this._parseEditedOutput(editedOutput) break } @@ -228,26 +228,26 @@ export abstract class HumanFeedbackMechanism< throw new Error('Expected output to be an array') } - feedback.selected = await this.selectN(output) + feedback.selected = await this._selectN(output) } else if (this._options.type === 'selectOne') { if (!Array.isArray(output)) { throw new Error('Expected output to be an array') } - feedback.chosen = await this.selectOne(output) + feedback.chosen = await this._selectOne(output) } break - case HumanFeedbackUserActions.Exit: - throw new Error('Exiting...') + case HumanFeedbackUserActions.Abort: + throw new Error('Aborting...') default: throw new Error(`Unexpected choice: ${choice}`) } if (this._options.annotations) { - const annotation = await this.annotate() + const annotation = await this._annotate() if (annotation) { feedback.annotation = annotation } @@ -270,7 +270,7 @@ export function withHumanFeedback( const finalOptions: HumanFeedbackOptions = Object.assign( { type: 'confirm', - bail: false, + abort: false, editing: false, annotations: false, mechanism: HumanFeedbackMechanismCLI diff --git a/legacy/src/human-feedback/slack.ts b/legacy/src/human-feedback/slack.ts index 47002025..25b7f9ae 100644 --- a/legacy/src/human-feedback/slack.ts +++ b/legacy/src/human-feedback/slack.ts @@ -13,22 +13,24 @@ export class HumanFeedbackMechanismSlack< T extends HumanFeedbackType, TOutput = any > extends HumanFeedbackMechanism { - private slackClient: SlackClient + protected _slackClient: SlackClient constructor({ task, - options + options, + slackClient = new SlackClient() }: { task: BaseTask options: Required> + slackClient: SlackClient }) { super({ task, options }) - this.slackClient = new SlackClient() + this._slackClient = slackClient } - protected async annotate(): Promise { + protected async _annotate(): Promise { try { - const annotation = await this.slackClient.sendAndWaitForReply({ + const annotation = await this._slackClient.sendAndWaitForReply({ text: 'Please leave an annotation (optional):' }) return annotation.text @@ -38,8 +40,8 @@ export class HumanFeedbackMechanismSlack< } } - protected async edit(): Promise { - let { text: editedOutput } = await this.slackClient.sendAndWaitForReply({ + protected async _edit(): Promise { + let { text: editedOutput } = await this._slackClient.sendAndWaitForReply({ text: 'Copy and edit the output:' }) editedOutput = editedOutput.replace(/```$/g, '') @@ -47,7 +49,7 @@ export class HumanFeedbackMechanismSlack< return editedOutput } - protected async askUser( + protected async _askUser( message: string, choices: HumanFeedbackUserActions[] ): Promise { @@ -59,7 +61,7 @@ export class HumanFeedbackMechanismSlack< .join('\n') message += '\n\n' message += 'Reply with the number of your choice.' - const response = await this.slackClient.sendAndWaitForReply({ + const response = await this._slackClient.sendAndWaitForReply({ text: message, validate: (slackMessage) => { const choice = parseInt(slackMessage.text) @@ -69,15 +71,15 @@ export class HumanFeedbackMechanismSlack< return choices[parseInt(response.text)] } - protected async selectOne( + protected async _selectOne( response: TOutput ): Promise { if (!Array.isArray(response)) { throw new Error('selectOne called on non-array response') } - const { text: selectedOutput } = await this.slackClient.sendAndWaitForReply( - { + const { text: selectedOutput } = + await this._slackClient.sendAndWaitForReply({ text: 'Pick one output:' + response.map((r, idx) => `\n*${idx}* - ${r}`).join('') + @@ -86,20 +88,19 @@ export class HumanFeedbackMechanismSlack< const choice = parseInt(slackMessage.text) return !isNaN(choice) && choice >= 0 && choice < response.length } - } - ) + }) return response[parseInt(selectedOutput)] } - protected async selectN( + protected async _selectN( response: TOutput ): Promise { if (!Array.isArray(response)) { throw new Error('selectN called on non-array response') } - const { text: selectedOutput } = await this.slackClient.sendAndWaitForReply( - { + const { text: selectedOutput } = + await this._slackClient.sendAndWaitForReply({ text: 'Select outputs:' + response.map((r, idx) => `\n*${idx}* - ${r}`).join('') + @@ -113,8 +114,7 @@ export class HumanFeedbackMechanismSlack< ) }) } - } - ) + }) const chosenOutputs = selectedOutput .split(',') .map((choice) => parseInt(choice)) diff --git a/legacy/src/human-feedback/twilio.ts b/legacy/src/human-feedback/twilio.ts index 895d10d8..72f83865 100644 --- a/legacy/src/human-feedback/twilio.ts +++ b/legacy/src/human-feedback/twilio.ts @@ -13,22 +13,24 @@ export class HumanFeedbackMechanismTwilio< T extends HumanFeedbackType, TOutput = any > extends HumanFeedbackMechanism { - private twilioClient: TwilioConversationClient + protected _twilioClient: TwilioConversationClient constructor({ task, - options + options, + twilioClient = new TwilioConversationClient() }: { task: BaseTask options: Required> + twilioClient: TwilioConversationClient }) { super({ task, options }) - this.twilioClient = new TwilioConversationClient() + this._twilioClient = twilioClient } - protected async annotate(): Promise { + protected async _annotate(): Promise { try { - const annotation = await this.twilioClient.sendAndWaitForReply({ + const annotation = await this._twilioClient.sendAndWaitForReply({ name: 'human-feedback-annotation', text: 'Please leave an annotation (optional):' }) @@ -39,8 +41,8 @@ export class HumanFeedbackMechanismTwilio< } } - protected async edit(): Promise { - let { body: editedOutput } = await this.twilioClient.sendAndWaitForReply({ + protected async _edit(): Promise { + let { body: editedOutput } = await this._twilioClient.sendAndWaitForReply({ text: 'Copy and edit the output:', name: 'human-feedback-edit' }) @@ -49,7 +51,7 @@ export class HumanFeedbackMechanismTwilio< return editedOutput } - protected async askUser( + protected async _askUser( message: string, choices: HumanFeedbackUserActions[] ): Promise { @@ -61,7 +63,7 @@ export class HumanFeedbackMechanismTwilio< .join('\n') message += '\n\n' message += 'Reply with the number of your choice.' - const response = await this.twilioClient.sendAndWaitForReply({ + const response = await this._twilioClient.sendAndWaitForReply({ name: 'human-feedback-ask', text: message, validate: (message) => { @@ -72,7 +74,7 @@ export class HumanFeedbackMechanismTwilio< return choices[parseInt(response.body)] } - protected async selectOne( + protected async _selectOne( response: TOutput ): Promise { if (!Array.isArray(response)) { @@ -80,7 +82,7 @@ export class HumanFeedbackMechanismTwilio< } const { body: selectedOutput } = - await this.twilioClient.sendAndWaitForReply({ + await this._twilioClient.sendAndWaitForReply({ name: 'human-feedback-select', text: 'Pick one output:' + @@ -94,7 +96,7 @@ export class HumanFeedbackMechanismTwilio< return response[parseInt(selectedOutput)] } - protected async selectN( + protected async _selectN( response: TOutput ): Promise { if (!Array.isArray(response)) { @@ -102,7 +104,7 @@ export class HumanFeedbackMechanismTwilio< } const { body: selectedOutput } = - await this.twilioClient.sendAndWaitForReply({ + await this._twilioClient.sendAndWaitForReply({ name: 'human-feedback-select', text: 'Select outputs:' +