/** * Context-menu translation entry (background). * * Receives the raw `menus.OnClickData.selectionText`, validates it through * the shared pipeline and starts a translation without a sink. The popup * observes results via the shared translation state (GET_POPUP_STATE). */ import { validateSelectionText, runTranslation, messageForCode } from './translation-service.js'; import { createRequestId } from '../shared/messages.js'; import { ERROR_CODES } from '../shared/constants.js'; /** * Start a translation from a context-menu selection. * @param {*} selectionText raw value from menus.OnClickData * @returns {{ok: boolean, requestId?: string, error?: string, message?: string}} */ export function startFromSelection(selectionText) { const validated = validateSelectionText(selectionText); if (!validated.ok) { return { ok: false, error: validated.code, message: messageForCode(validated.code) }; } const requestId = createRequestId(); // No sink: the popup polls GET_POPUP_STATE and receives streaming updates // through the shared translation state. void runTranslation({ text: validated.text, requestId, source: 'context-menu', sink: null }); return { ok: true, requestId }; }