Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
293 changes: 293 additions & 0 deletions agent-server/nodejs/src/api-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ class APIServer {
result = await this.getAccessibilityTree(method === 'POST' ? JSON.parse(body) : parsedUrl.query);
break;

// Recording control endpoints
case '/recording/start':
if (method !== 'POST') {
this.sendError(res, 405, 'Method not allowed');
return;
}
result = await this.startRecording(JSON.parse(body));
break;

case '/recording/stop':
if (method !== 'POST') {
this.sendError(res, 405, 'Method not allowed');
return;
}
result = await this.stopRecording(JSON.parse(body));
break;

case '/recording/status':
if (method !== 'GET') {
this.sendError(res, 405, 'Method not allowed');
return;
}
result = await this.getRecordingStatus(parsedUrl.query);
break;

default:
this.sendError(res, 404, 'Not found');
return;
Expand Down Expand Up @@ -1414,6 +1439,274 @@ class APIServer {
return response;
}

// ============================================================================
// Recording Control Methods
// ============================================================================

/**
* Start recording user interactions on a browser tab.
* @param {Object} payload - Request payload
* @param {string} payload.clientId - The client ID (DevTools connection)
* @param {string} payload.tabId - The tab ID to record
* @param {string} [payload.title] - Optional title for the recording
* @param {string[]} [payload.selectorTypes] - Selector types to record (aria, css, xpath, text)
* @returns {Object} Recording start result
*/
async startRecording(payload) {
const { clientId, tabId, title, selectorTypes, selectorAttribute } = payload;

// Validate required params
if (!clientId) {
throw new Error('Client ID is required');
}
if (!tabId) {
throw new Error('Tab ID is required');
}

// Get the base client ID (without tab suffix)
const baseClientId = clientId.split(':')[0];
const compositeClientId = `${baseClientId}:${tabId}`;

// Find the connected client
const connection = this.browserAgentServer.connectedClients.get(compositeClientId);
if (!connection) {
logger.warn('Recording requires DevTools connection', {
compositeClientId,
availableClients: Array.from(this.browserAgentServer.connectedClients.keys())
});
return {
success: false,
message: `Tab ${tabId} not connected. DevTools may still be initializing - try again in a moment.`,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
}

logger.info('Starting recording', {
clientId: baseClientId,
tabId,
title,
selectorTypes
});

// Send RPC request to DevTools to start recording
const rpcId = uuidv4();
const rpcRequest = {
jsonrpc: '2.0',
method: 'recording_control',
params: {
action: 'start',
title: title || `Recording ${Date.now()}`,
selectorTypes: selectorTypes || ['aria', 'css', 'xpath', 'text'],
selectorAttribute
},
id: rpcId
};

try {
const response = await connection.rpcClient.callMethod(
connection.ws,
'recording_control',
rpcRequest.params,
30000 // 30 second timeout
);

return {
success: response.success,
recordingId: response.recordingId,
message: response.message,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
} catch (error) {
logger.error('Failed to start recording:', error);
return {
success: false,
message: error.message,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
}
}

/**
* Stop recording and return the captured data.
* @param {Object} payload - Request payload
* @param {string} payload.clientId - The client ID
* @param {string} payload.tabId - The tab ID
* @param {string} [payload.format] - Output format: 'userflow' or 'replay'
* @returns {Object} Recording result with userFlow or replayTranscript
*/
async stopRecording(payload) {
const { clientId, tabId, format = 'userflow' } = payload;

// Validate required params
if (!clientId) {
throw new Error('Client ID is required');
}
if (!tabId) {
throw new Error('Tab ID is required');
}

// Get the base client ID (without tab suffix)
const baseClientId = clientId.split(':')[0];
const compositeClientId = `${baseClientId}:${tabId}`;

// Find the connected client
const connection = this.browserAgentServer.connectedClients.get(compositeClientId);
if (!connection) {
logger.warn('Stop recording requires DevTools connection', {
compositeClientId,
availableClients: Array.from(this.browserAgentServer.connectedClients.keys())
});
return {
success: false,
message: `Tab ${tabId} not connected. DevTools may still be initializing - try again in a moment.`,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
}

logger.info('Stopping recording', {
clientId: baseClientId,
tabId,
format
});

// Send RPC request to DevTools to stop recording
const rpcId = uuidv4();
const rpcRequest = {
jsonrpc: '2.0',
method: 'recording_control',
params: {
action: 'stop',
format
},
id: rpcId
};

try {
const response = await connection.rpcClient.callMethod(
connection.ws,
'recording_control',
rpcRequest.params,
20000 // DevTools-side stop has 10s internal timeout; allow extra for serialization + transport
);

return {
success: response.success,
recordingId: response.recordingId,
message: response.message,
userFlow: response.userFlow,
replayTranscript: response.replayTranscript,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
} catch (error) {
logger.error('Failed to stop recording:', error);
return {
success: false,
message: error.message,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
}
}

/**
* Get the current recording status.
* @param {Object} query - Query parameters
* @param {string} query.clientId - The client ID
* @param {string} query.tabId - The tab ID
* @returns {Object} Recording status
*/
async getRecordingStatus(query) {
const { clientId, tabId } = query;

// Validate required params
if (!clientId) {
throw new Error('Client ID is required');
}
if (!tabId) {
throw new Error('Tab ID is required');
}

// Get the base client ID (without tab suffix)
const baseClientId = clientId.split(':')[0];
const compositeClientId = `${baseClientId}:${tabId}`;

// Find the connected client
const connection = this.browserAgentServer.connectedClients.get(compositeClientId);
if (!connection) {
logger.warn('Recording status requires DevTools connection', {
compositeClientId,
availableClients: Array.from(this.browserAgentServer.connectedClients.keys())
});
return {
success: false,
message: `Tab ${tabId} not connected. DevTools may still be initializing - try again in a moment.`,
isRecording: false,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
}

logger.info('Getting recording status', {
clientId: baseClientId,
tabId
});

// Send RPC request to DevTools to get status
const rpcId = uuidv4();
const rpcRequest = {
jsonrpc: '2.0',
method: 'recording_control',
params: {
action: 'status'
},
id: rpcId
};

try {
const response = await connection.rpcClient.callMethod(
connection.ws,
'recording_control',
rpcRequest.params,
10000 // 10 second timeout
);

return {
success: response.success,
recordingId: response.recordingId,
isRecording: response.status?.isRecording || false,
isPaused: response.status?.isPaused || false,
stepCount: response.status?.stepCount || 0,
duration_ms: response.status?.duration_ms || 0,
title: response.status?.title,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
} catch (error) {
logger.error('Failed to get recording status:', error);
return {
success: false,
message: error.message,
isRecording: false,
clientId: baseClientId,
tabId,
timestamp: Date.now()
};
}
}

sendResponse(res, statusCode, data) {
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data, null, 2));
Expand Down
6 changes: 5 additions & 1 deletion config/gni/devtools_grd_files.gni
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ grd_files_bundled_sources = [
"front_end/panels/ai_chat/common/EvaluationConfig.js",
"front_end/panels/ai_chat/evaluation/remote/EvaluationProtocol.js",
"front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.js",
"front_end/panels/ai_chat/replay/ReplayTranscript.js",
"front_end/panels/ai_chat/replay/UserFlowConverter.js",
"front_end/panels/ai_chat/tracing/TracingProvider.js",
"front_end/panels/ai_chat/tracing/LangfuseProvider.js",
"front_end/panels/ai_chat/tracing/TracingConfig.js",
Expand All @@ -854,6 +856,8 @@ grd_files_bundled_sources = [
"front_end/panels/ai_chat/agent_framework/RuntimeContext.js",
"front_end/panels/ai_chat/agent_framework/implementation/ConfiguredAgents.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ActionAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ActionAgentV1.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ActionAgentV2.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ActionVerificationAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/AgentVersion.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ClickActionAgent.js",
Expand All @@ -865,10 +869,10 @@ grd_files_bundled_sources = [
"front_end/panels/ai_chat/agent_framework/implementation/agents/KeyboardInputActionAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ResearchAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ScrollActionAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/WebTaskAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/SearchAgent.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ActionAgentV1.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/ActionAgentV2.js",
"front_end/panels/ai_chat/agent_framework/implementation/agents/WebTaskAgent.js",
"front_end/panels/ai_chat/common/MarkdownViewerUtil.js",
"front_end/panels/ai_chat/evaluation/runner/VisionAgentEvaluationRunner.js",
"front_end/panels/ai_chat/evaluation/runner/EvaluationRunner.js",
Expand Down
Loading