Files
big-agi/src/modules/beam/gather/instructions/UserInputChecklistInstruction.tsx
T
2024-05-06 23:45:34 -07:00

73 lines
2.4 KiB
TypeScript

import type { BaseInstruction, ExecutionInputState } from './beam.gather.execution';
import { parseTextToChecklist, UserInputChecklistComponent } from './UserInputChecklistComponent';
import { bareBonesPromptMixer } from '~/modules/persona/pmix/pmix';
export interface UserInputChecklistInstruction extends BaseInstruction {
type: 'user-input-checklist';
outputPrompt: string;
}
export interface UserChecklistOption {
id: string,
label: string,
selected: boolean,
}
export async function executeUserInputChecklist(
_i: UserInputChecklistInstruction,
inputs: ExecutionInputState,
previousResult: string,
): Promise<string> {
return new Promise((resolve, reject) => {
// initial text to options
let options = parseTextToChecklist(previousResult, false);
const relaxMatch = options.length < 2;
if (relaxMatch)
options = parseTextToChecklist(previousResult, true);
// if no options, there's an error
if (options.length < 2) {
reject(new Error('Oops! It looks like we had trouble understanding the Model. Could you please try again?'));
return;
}
// react to aborts
const abortHandler = () => reject(new Error('Checklist Selection Stopped.'));
inputs.chainAbortController.signal.addEventListener('abort', abortHandler);
const clearState = () => {
inputs.updateInstructionComponent(undefined);
inputs.chainAbortController.signal.removeEventListener('abort', abortHandler); // Cleanup
};
const onConfirm = (selectedOptions: UserChecklistOption[]) => {
clearState();
// output prompt mixer
const outputPrompt = bareBonesPromptMixer(_i.outputPrompt, undefined, {
'{{YesAnswers}}': selectedOptions.filter(o => o.selected).map(o => `- ${o.label.trim()}`).join('\n') || 'None',
'{{NoAnswers}}': selectedOptions.filter(o => !o.selected).map(o => `- ${o.label.trim()}`).join('\n') || 'None',
});
// Proceed to the next step
resolve(outputPrompt);
};
const onCancel = () => {
clearState();
inputs.chainAbortController.abort('User cancelled the input.');
reject();
};
// Remove the placeholder message
inputs.updateProgressComponent(null);
// Update the instruction component to render the checklist
inputs.updateInstructionComponent(<UserInputChecklistComponent options={options} onConfirm={onConfirm} onCancel={onCancel} />);
});
}