|
| 1 | +import { Chatbot } from '../types/chatbot' |
| 2 | +import { CHATBOTS } from '@shared/constants/chatbots' |
| 3 | +import { |
| 4 | + add_apply_response_button, |
| 5 | + observe_for_responses |
| 6 | +} from '../utils/add-apply-response-button' |
| 7 | +import { |
| 8 | + InitializationError, |
| 9 | + report_initialization_error |
| 10 | +} from '../utils/report-initialization-error' |
| 11 | + |
| 12 | +export const copilot: Chatbot = { |
| 13 | + wait_until_ready: async () => { |
| 14 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 15 | + }, |
| 16 | + set_model: async (model?: string) => { |
| 17 | + if (!model) return |
| 18 | + |
| 19 | + const model_selector_button = document.querySelector( |
| 20 | + 'button[data-testid="composer-chat-mode-quick-button"]' |
| 21 | + ) as HTMLButtonElement |
| 22 | + if (!model_selector_button) { |
| 23 | + report_initialization_error({ |
| 24 | + function_name: 'set_model', |
| 25 | + log_message: 'Model selector button not found', |
| 26 | + alert_message: InitializationError.UNABLE_TO_SET_MODEL |
| 27 | + }) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + const model_label_to_find = (CHATBOTS['Copilot'].models as any)[model] |
| 32 | + ?.label |
| 33 | + if (!model_label_to_find) return |
| 34 | + |
| 35 | + // The current model text is inside the button |
| 36 | + if (model_selector_button.textContent?.includes(model_label_to_find)) { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + model_selector_button.click() |
| 41 | + await new Promise((r) => requestAnimationFrame(r)) |
| 42 | + |
| 43 | + const options_container = document.querySelector('div[id="popoverPortal"]') |
| 44 | + if (!options_container) { |
| 45 | + report_initialization_error({ |
| 46 | + function_name: 'set_model', |
| 47 | + log_message: 'Model options container not found', |
| 48 | + alert_message: InitializationError.UNABLE_TO_SET_MODEL |
| 49 | + }) |
| 50 | + // click again to close |
| 51 | + model_selector_button.click() |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const options = options_container.querySelectorAll( |
| 56 | + 'button[role="menuitem"]' |
| 57 | + ) |
| 58 | + |
| 59 | + let found = false |
| 60 | + for (const option of Array.from(options)) { |
| 61 | + if (option.textContent?.includes(model_label_to_find)) { |
| 62 | + ;(option as HTMLElement).click() |
| 63 | + found = true |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (!found) { |
| 69 | + report_initialization_error({ |
| 70 | + function_name: 'set_model', |
| 71 | + log_message: `Model option "${model_label_to_find}" not found`, |
| 72 | + alert_message: InitializationError.UNABLE_TO_SET_MODEL |
| 73 | + }) |
| 74 | + // click again to close dropdown |
| 75 | + model_selector_button.click() |
| 76 | + } |
| 77 | + |
| 78 | + await new Promise((r) => requestAnimationFrame(r)) |
| 79 | + }, |
| 80 | + enter_message_and_send: async ( |
| 81 | + message: string, |
| 82 | + without_submission?: boolean |
| 83 | + ) => { |
| 84 | + const input_element = document.querySelector( |
| 85 | + 'textarea' |
| 86 | + ) as HTMLTextAreaElement |
| 87 | + if (!input_element) { |
| 88 | + report_initialization_error({ |
| 89 | + function_name: 'enter_message_and_send', |
| 90 | + log_message: 'Message input textarea not found for Copilot', |
| 91 | + alert_message: InitializationError.UNABLE_TO_SEND_MESSAGE |
| 92 | + }) |
| 93 | + return |
| 94 | + } |
| 95 | + input_element.value = message |
| 96 | + input_element.dispatchEvent(new Event('input', { bubbles: true })) |
| 97 | + input_element.dispatchEvent(new Event('change', { bubbles: true })) |
| 98 | + await new Promise((r) => requestAnimationFrame(r)) |
| 99 | + |
| 100 | + if (without_submission) return |
| 101 | + |
| 102 | + const send_button = document.querySelector( |
| 103 | + 'button[data-testid="submit-button"]' |
| 104 | + ) as HTMLElement |
| 105 | + |
| 106 | + if (!send_button || send_button.hasAttribute('disabled')) { |
| 107 | + report_initialization_error({ |
| 108 | + function_name: 'enter_message_and_send', |
| 109 | + log_message: 'Send button not found or disabled for Copilot', |
| 110 | + alert_message: InitializationError.UNABLE_TO_SEND_MESSAGE |
| 111 | + }) |
| 112 | + return |
| 113 | + } |
| 114 | + send_button.click() |
| 115 | + }, |
| 116 | + inject_apply_response_button: ( |
| 117 | + client_id: number, |
| 118 | + raw_instructions?: string |
| 119 | + ) => { |
| 120 | + const add_buttons = (footer: Element) => { |
| 121 | + add_apply_response_button({ |
| 122 | + client_id, |
| 123 | + raw_instructions, |
| 124 | + footer, |
| 125 | + get_chat_turn: (f) => f.closest('div[data-content="ai-message"]'), |
| 126 | + get_code_blocks: (t) => t.querySelectorAll('code'), |
| 127 | + perform_copy: (f) => { |
| 128 | + const copy_button = f.querySelector( |
| 129 | + 'button[data-testid="copy-message-button"]' |
| 130 | + ) as HTMLElement |
| 131 | + if (!copy_button) { |
| 132 | + report_initialization_error({ |
| 133 | + function_name: 'copilot.perform_copy', |
| 134 | + log_message: 'Copy button not found', |
| 135 | + alert_message: InitializationError.UNABLE_TO_COPY_RESPONSE |
| 136 | + }) |
| 137 | + return |
| 138 | + } |
| 139 | + copy_button.click() |
| 140 | + }, |
| 141 | + insert_button: (f, b) => f.insertBefore(b, f.firstChild) |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + observe_for_responses({ |
| 146 | + chatbot_name: 'Copilot', |
| 147 | + is_generating: () => |
| 148 | + !!document.querySelector('button[data-testid="stop-button"]'), |
| 149 | + footer_selector: 'div[data-testid="message-item-reactions"]', |
| 150 | + add_buttons |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments