From 7b5797e08b9aa46c3af49f6d1bc7211288af10bd Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Tue, 13 Jun 2023 15:52:45 -0400 Subject: [PATCH] refactor: move annotate to own method --- legacy/src/human-feedback/cli.ts | 31 +++++++------ legacy/src/human-feedback/feedback.ts | 15 +++++-- legacy/src/human-feedback/slack.ts | 59 ++++++++----------------- legacy/src/human-feedback/twilio.ts | 63 ++++++++------------------- 4 files changed, 64 insertions(+), 104 deletions(-) diff --git a/legacy/src/human-feedback/cli.ts b/legacy/src/human-feedback/cli.ts index f864f526..ad5f2636 100644 --- a/legacy/src/human-feedback/cli.ts +++ b/legacy/src/human-feedback/cli.ts @@ -42,7 +42,21 @@ export class HumanFeedbackMechanismCLI extends HumanFeedbackMechanism { this._options = options } - public async confirm( + protected async annotate( + response: any, + metadata: TaskResponseMetadata + ): Promise { + const annotation = await input({ + message: + 'Please leave an annotation (leave blank to skip; press enter to submit):' + }) + + if (annotation) { + metadata.feedback.annotation = annotation + } + } + + protected async confirm( response: any, metadata: TaskResponseMetadata ): Promise { @@ -90,20 +104,9 @@ export class HumanFeedbackMechanismCLI extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - const annotation = await input({ - message: - 'Please leave an annotation (leave blank to skip; press enter to submit):' - }) - - if (annotation) { - metadata.feedback.annotation = annotation - } - } } - public async selectOne( + protected async selectOne( response: any[], metadata: TaskResponseMetadata ): Promise { @@ -155,7 +158,7 @@ export class HumanFeedbackMechanismCLI extends HumanFeedbackMechanism { } } - public async selectN( + protected async selectN( response: any[], metadata: TaskResponseMetadata ): Promise { diff --git a/legacy/src/human-feedback/feedback.ts b/legacy/src/human-feedback/feedback.ts index 41fac1bf..4619a9df 100644 --- a/legacy/src/human-feedback/feedback.ts +++ b/legacy/src/human-feedback/feedback.ts @@ -82,15 +82,20 @@ export abstract class HumanFeedbackMechanism { this._options = options } - public abstract confirm( + protected abstract confirm( response: any, metadata: TaskResponseMetadata ): Promise - public abstract selectOne( + protected abstract selectOne( response: any, metadata: TaskResponseMetadata ): Promise - public abstract selectN( + protected abstract selectN( + response: any, + metadata: TaskResponseMetadata + ): Promise + + protected abstract annotate( response: any, metadata: TaskResponseMetadata ): Promise @@ -103,6 +108,10 @@ export abstract class HumanFeedbackMechanism { } else if (this._options.type === 'selectOne') { await this.selectOne(response, metadata) } + + if (this._options.annotations) { + await this.annotate(response, metadata) + } } } diff --git a/legacy/src/human-feedback/slack.ts b/legacy/src/human-feedback/slack.ts index 5f3d1ea2..e8172d19 100644 --- a/legacy/src/human-feedback/slack.ts +++ b/legacy/src/human-feedback/slack.ts @@ -23,6 +23,23 @@ export class HumanFeedbackMechanismSlack extends HumanFeedbackMechanism { this.slackClient = new SlackClient() } + protected async annotate( + response: any, + metadata: TaskResponseMetadata + ): Promise { + try { + const annotation = await this.slackClient.sendAndWaitForReply({ + text: 'Please leave an annotation (optional):' + }) + + if (annotation) { + metadata.feedback.annotation = annotation.text + } + } catch (e) { + // Deliberately swallow the error here as the user is not required to leave an annotation + } + } + private async askUser( message: string, choices: UserActions[] @@ -97,20 +114,6 @@ export class HumanFeedbackMechanismSlack extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - try { - const annotation = await this.slackClient.sendAndWaitForReply({ - text: 'Please leave an annotation (optional):' - }) - - if (annotation) { - metadata.feedback.annotation = annotation.text - } - } catch (e) { - // Deliberately swallow the error here as the user is not required to leave an annotation - } - } } public async selectOne( @@ -178,20 +181,6 @@ export class HumanFeedbackMechanismSlack extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - try { - const annotation = await this.slackClient.sendAndWaitForReply({ - text: 'Please leave an annotation (optional):' - }) - - if (annotation) { - metadata.feedback.annotation = annotation.text - } - } catch (e) { - // Deliberately swallow the error here as the user is not required to leave an annotation - } - } } public async selectN( @@ -268,19 +257,5 @@ export class HumanFeedbackMechanismSlack extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - try { - const annotation = await this.slackClient.sendAndWaitForReply({ - text: 'Please leave an annotation (optional):' - }) - - if (annotation) { - metadata.feedback.annotation = annotation.text - } - } catch (e) { - // Deliberately swallow the error here as the user is not required to leave an annotation - } - } } } diff --git a/legacy/src/human-feedback/twilio.ts b/legacy/src/human-feedback/twilio.ts index 383f35b1..d7cae0c3 100644 --- a/legacy/src/human-feedback/twilio.ts +++ b/legacy/src/human-feedback/twilio.ts @@ -23,6 +23,24 @@ export class HumanFeedbackMechanismTwilio extends HumanFeedbackMechanism { this.twilioClient = new TwilioConversationClient() } + protected async annotate( + response: any, + metadata: TaskResponseMetadata + ): Promise { + try { + const annotation = await this.twilioClient.sendAndWaitForReply({ + name: 'human-feedback-annotation', + text: 'Please leave an annotation (optional):' + }) + + if (annotation) { + metadata.feedback.annotation = annotation.body + } + } catch (e) { + // Deliberately swallow the error here as the user is not required to leave an annotation + } + } + private async askUser( message: string, choices: UserActions[] @@ -98,21 +116,6 @@ export class HumanFeedbackMechanismTwilio extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - try { - const annotation = await this.twilioClient.sendAndWaitForReply({ - name: 'human-feedback-annotation', - text: 'Please leave an annotation (optional):' - }) - - if (annotation) { - metadata.feedback.annotation = annotation.body - } - } catch (e) { - // Deliberately swallow the error here as the user is not required to leave an annotation - } - } } public async selectOne( @@ -181,21 +184,6 @@ export class HumanFeedbackMechanismTwilio extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - try { - const annotation = await this.twilioClient.sendAndWaitForReply({ - text: 'Please leave an annotation (optional):', - name: 'human-feedback-annotation' - }) - - if (annotation) { - metadata.feedback.annotation = annotation.body - } - } catch (e) { - // Deliberately swallow the error here as the user is not required to leave an annotation - } - } } public async selectN( @@ -275,20 +263,5 @@ export class HumanFeedbackMechanismTwilio extends HumanFeedbackMechanism { default: throw new Error(`Unexpected feedback: ${feedback}`) } - - if (this._options.annotations) { - try { - const annotation = await this.twilioClient.sendAndWaitForReply({ - text: 'Please leave an annotation (optional):', - name: 'human-feedback-annotation' - }) - - if (annotation) { - metadata.feedback.annotation = annotation.body - } - } catch (e) { - // Deliberately swallow the error here as the user is not required to leave an annotation - } - } } }