diff --git a/eslint.config.js b/eslint.config.js index cf5b0f1..38a7191 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -75,7 +75,7 @@ export default defineConfig([ 'react-dom/no-missing-button-type': 'off', 'react-x/no-nested-component-definitions': 'off', '@typescript-eslint/prefer-optional-chain': 'off', - '@typescript-eslint/use-unknown-in-catch-callback-variable': 'warn', + '@typescript-eslint/use-unknown-in-catch-callback-variable': 'off', '@typescript-eslint/no-unnecessary-type-parameters': 'off', // 'require-await': 'off', diff --git a/packages/extension/src/entrypoints/background.ts b/packages/extension/src/entrypoints/background.ts index d6a47ca..4a667c4 100644 --- a/packages/extension/src/entrypoints/background.ts +++ b/packages/extension/src/entrypoints/background.ts @@ -65,6 +65,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise { // Create message for content script const csMessage: CSRPCMessage = { + isPageAgentMessage: true, type: 'cs:rpc', id, method, @@ -77,6 +78,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise { // Forward response back to sidepanel const response: RPCResponseMessage = { + isPageAgentMessage: true, type: 'rpc:response', id, success: true, @@ -86,6 +88,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise { } catch (error) { // Forward error back to sidepanel const response: RPCResponseMessage = { + isPageAgentMessage: true, type: 'rpc:response', id, success: false, @@ -117,6 +120,7 @@ async function handleCSQuery( // Forward response back to content script if (sender.tab?.id) { const queryResponse: QueryResponseMessage = { + isPageAgentMessage: true, type: 'query:response', id, result: response, @@ -127,6 +131,7 @@ async function handleCSQuery( // Sidepanel not open or no response, return default if (sender.tab?.id) { const queryResponse: QueryResponseMessage = { + isPageAgentMessage: true, type: 'query:response', id, result: queryType === 'shouldShowMask' ? false : null, @@ -145,6 +150,7 @@ async function handleCSQuery( */ chrome.tabs.onRemoved.addListener((tabId) => { const message: TabEventMessage = { + isPageAgentMessage: true, type: 'tab:event', id: generateMessageId(), eventType: 'removed', @@ -163,6 +169,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { if (!changeInfo.status) return const message: TabEventMessage = { + isPageAgentMessage: true, type: 'tab:event', id: generateMessageId(), eventType: 'updated', @@ -182,6 +189,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { */ chrome.tabs.onActivated.addListener((activeInfo) => { const message: TabEventMessage = { + isPageAgentMessage: true, type: 'tab:event', id: generateMessageId(), eventType: 'activated', @@ -202,6 +210,7 @@ chrome.windows.onFocusChanged.addListener((windowId) => { // windowId is chrome.windows.WINDOW_ID_NONE (-1) when all windows lose focus const focused = windowId !== chrome.windows.WINDOW_ID_NONE const message: TabEventMessage = { + isPageAgentMessage: true, type: 'tab:event', id: generateMessageId(), eventType: 'windowFocusChanged', diff --git a/packages/extension/src/entrypoints/content.ts b/packages/extension/src/entrypoints/content.ts index 06314cb..baae6fc 100644 --- a/packages/extension/src/entrypoints/content.ts +++ b/packages/extension/src/entrypoints/content.ts @@ -11,12 +11,7 @@ */ import { PageController } from '@page-agent/page-controller' -import type { - CSQueryMessage, - CSRPCMessage, - QueryResponseMessage, - RPCMethod, -} from '../messaging/protocol' +import type { CSQueryMessage, CSRPCMessage, QueryResponseMessage } from '../messaging/protocol' import { generateMessageId, isExtensionMessage } from '../messaging/protocol' const DEBUG_PREFIX = '[ContentScript]' @@ -86,6 +81,7 @@ async function queryShouldShowMask(getController: () => PageController): Promise const queryId = generateMessageId() const queryMessage: CSQueryMessage = { + isPageAgentMessage: true, type: 'cs:query', id: queryId, queryType: 'shouldShowMask', @@ -198,7 +194,7 @@ function registerRPCHandler( * Handle an RPC call */ async function handleRPCCall( - method: RPCMethod, + method: string, args: unknown[], getController: () => PageController, getControllerIfExists: () => PageController | null, diff --git a/packages/extension/src/messaging/protocol.ts b/packages/extension/src/messaging/protocol.ts index b08af0a..18a13de 100644 --- a/packages/extension/src/messaging/protocol.ts +++ b/packages/extension/src/messaging/protocol.ts @@ -61,6 +61,7 @@ type MessageType = /** Base message structure */ interface BaseMessage { + isPageAgentMessage: true type: MessageType id: string // Unique message ID for request-response matching } @@ -69,28 +70,11 @@ interface BaseMessage { // RPC Messages (SidePanel ↔ SW ↔ ContentScript) // ============================================================================ -/** RPC method names matching PageController interface */ -export type RPCMethod = - | 'getCurrentUrl' - | 'getLastUpdateTime' - | 'getBrowserState' - | 'updateTree' - | 'cleanUpHighlights' - | 'clickElement' - | 'inputText' - | 'selectOption' - | 'scroll' - | 'scrollHorizontally' - | 'executeJavascript' - | 'showMask' - | 'hideMask' - | 'dispose' - /** SidePanel → SW: Request to call PageController method */ export interface RPCCallMessage extends BaseMessage { type: 'rpc:call' tabId: number - method: RPCMethod + method: string args: unknown[] } @@ -105,7 +89,7 @@ export interface RPCResponseMessage extends BaseMessage { /** SW → ContentScript: Forwarded RPC call */ export interface CSRPCMessage extends BaseMessage { type: 'cs:rpc' - method: RPCMethod + method: string args: unknown[] } @@ -179,9 +163,7 @@ export function isExtensionMessage(msg: unknown): msg is ExtensionMessage { return ( typeof msg === 'object' && msg !== null && - 'type' in msg && - 'id' in msg && - typeof (msg as ExtensionMessage).type === 'string' && - typeof (msg as ExtensionMessage).id === 'string' + 'isPageAgentMessage' in msg && + (msg as any).isPageAgentMessage === true ) } diff --git a/packages/extension/src/messaging/rpc.ts b/packages/extension/src/messaging/rpc.ts index 2412dd9..fa82b19 100644 --- a/packages/extension/src/messaging/rpc.ts +++ b/packages/extension/src/messaging/rpc.ts @@ -10,7 +10,6 @@ import { type ActionResult, type BrowserState, type RPCCallMessage, - type RPCMethod, type RPCResponseMessage, type ScrollHorizontallyOptions, type ScrollOptions, @@ -107,11 +106,12 @@ export class RPCError extends Error { /** * Make a single RPC call (no retry) */ -async function callOnce(tabId: number, method: RPCMethod, args: unknown[]): Promise { +async function callOnce(tabId: number, method: string, args: unknown[]): Promise { ensureResponseListener() const id = generateMessageId() const message: RPCCallMessage = { + isPageAgentMessage: true, type: 'rpc:call', id, tabId, @@ -138,7 +138,7 @@ async function callOnce(tabId: number, method: RPCMethod, args: unknown[]): Prom /** * Make an RPC call with retry logic */ -async function call(tabId: number, method: RPCMethod, args: unknown[]): Promise { +async function call(tabId: number, method: string, args: unknown[]): Promise { let lastError: Error | null = null for (let attempt = 0; attempt < RPC_CONFIG.maxRetries; attempt++) {