chore: add isPageAgentMessage to message

This commit is contained in:
Simon
2026-01-26 18:55:54 +08:00
parent 969dd31db5
commit f18b756390
5 changed files with 21 additions and 34 deletions

View File

@@ -65,6 +65,7 @@ async function handleRPCCall(msg: RPCCallMessage): Promise<void> {
// 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<void> {
// 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<void> {
} 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',

View File

@@ -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,

View File

@@ -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
)
}

View File

@@ -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<unknown> {
async function callOnce(tabId: number, method: string, args: unknown[]): Promise<unknown> {
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<unknown> {
async function call(tabId: number, method: string, args: unknown[]): Promise<unknown> {
let lastError: Error | null = null
for (let attempt = 0; attempt < RPC_CONFIG.maxRetries; attempt++) {