-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathagent-tars.ts
More file actions
786 lines (680 loc) · 26.1 KB
/
Copy pathagent-tars.ts
File metadata and controls
786 lines (680 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
import {
InMemoryTransport,
Client,
AgentEventStream,
Tool,
JSONSchema7,
MCPAgent,
MCPServerRegistry,
LLMRequestHookPayload,
LLMResponseHookPayload,
ConsoleLogger,
LoopTerminationCheckResult,
} from '@tarko/mcp-agent';
import {
AgentTARSOptions,
BuiltInMCPServers,
BuiltInMCPServerName,
AgentTARSPlannerOptions,
BrowserState,
} from './types';
import { DEFAULT_SYSTEM_PROMPT, generateBrowserRulesPrompt } from './prompt';
import { BrowserGUIAgent, BrowserManager, BrowserToolsManager } from './browser';
import { validateBrowserControlMode } from './browser/browser-control-validator';
import { SearchToolProvider } from './search';
import { FilesystemToolsManager } from './filesystem';
import { applyDefaultOptions } from './shared/config-utils';
import { MessageHistoryDumper } from './shared/message-history-dumper';
// @ts-expect-error
// Default esm asset has some issues {@see https://github.com/bytedance/UI-TARS-desktop/issues/672}
import * as browserModule from '@agent-infra/mcp-server-browser/dist/server.cjs';
import * as filesystemModule from '@agent-infra/mcp-server-filesystem';
import * as commandsModule from '@agent-infra/mcp-server-commands';
import { WorkspacePathResolver } from './shared/workspace-path-resolver';
import { AgentWebUIImplementation } from '@agent-tars/interface';
/**
* A Agent TARS that uses in-memory MCP tool call
* for built-in MCP Servers.
*/
export class AgentTARS<T extends AgentTARSOptions = AgentTARSOptions> extends MCPAgent<T> {
static label = '@agent-tars/core';
/**
* Default Web UI configuration for Agent TARS
*/
static webuiConfig: AgentWebUIImplementation = {
logo: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/zyha-aulnh/ljhwZthlaukjlkulzlp/appicon.png',
title: 'Agent TARS',
subtitle: 'Offering seamless integration with a wide range of real-world tools.',
welcomTitle: 'An multimodal AI agent',
welcomePrompts: [
'Search for the latest GUI Agent papers',
'Find information about UI TARS',
'Tell me the top 5 most popular projects on ProductHunt today',
'Please book me the earliest flight from Hangzhou to Shenzhen on 10.1',
],
enableContextualSelector: true,
guiAgent: {
defaultScreenshotRenderStrategy: 'beforeAction',
enableScreenshotRenderStrategySwitch: true,
renderGUIAction: true,
},
};
private workspace: string;
// FIXME: remove it since options is strict type already
private tarsOptions: AgentTARSOptions;
private mcpServers: BuiltInMCPServers = {};
private inMemoryMCPClients: Partial<Record<BuiltInMCPServerName, Client>> = {};
private browserGUIAgent?: BrowserGUIAgent;
private browserManager: BrowserManager;
private browserToolsManager?: BrowserToolsManager;
private filesystemToolsManager?: FilesystemToolsManager;
private searchToolProvider?: SearchToolProvider;
private browserState: BrowserState = {};
// Message history dumper for experimental dump feature
private messageHistoryDumper?: MessageHistoryDumper;
// Add workspace path resolver
private workspacePathResolver: WorkspacePathResolver;
constructor(options: T) {
// Apply default config using the new utility function
const tarsOptions = applyDefaultOptions<AgentTARSOptions>(options);
// Validate browser control mode based on model provider
if (tarsOptions.browser?.control) {
const modelProvider = tarsOptions.model?.provider || tarsOptions.model?.providers?.[0]?.name;
tarsOptions.browser.control = validateBrowserControlMode(
modelProvider,
tarsOptions.browser.control,
new ConsoleLogger(options.id || 'AgentTARS'),
);
}
const workspace = tarsOptions.workspace ?? process.cwd();
// Under the 'in-memory' implementation, the built-in mcp server will be implemented independently
// Note that the usage of the attached mcp server will be the same as the implementation,
// because we cannot determine whether it supports same-process calls.
const mcpServers: MCPServerRegistry = {
...(options.mcpImpl === 'stdio'
? {
browser: {
command: 'npx',
args: ['-y', '@agent-infra/mcp-server-browser'],
},
filesystem: {
command: 'npx',
args: ['-y', '@agent-infra/mcp-server-filesystem', workspace],
},
commands: {
command: 'npx',
args: ['-y', '@agent-infra/mcp-server-commands'],
},
}
: {}),
...(options.mcpServers || {}),
};
// Initialize planner options if enabled
const plannerOptions: AgentTARSPlannerOptions | undefined =
typeof tarsOptions.planner === 'boolean'
? tarsOptions.planner
? { enable: true }
: undefined
: tarsOptions.planner;
// Generate browser rules based on control solution
const browserRules = generateBrowserRulesPrompt(tarsOptions.browser?.control);
const systemPrompt = `${DEFAULT_SYSTEM_PROMPT}
${browserRules}
<envirnoment>
Current Working Directory: ${workspace}
</envirnoment>
`;
// Prepare system instructions with user instructions taking priority
// Core system capabilities provide the foundation, but user instructions override behavior
const instructions = options.instructions
? `${systemPrompt}
---
**User Instructions (Higher Priority):**
${options.instructions}`
: systemPrompt;
super({
...tarsOptions,
name: options.name ?? 'AgentTARS',
instructions,
mcpServers,
maxTokens: tarsOptions.maxTokens, // Ensure maxTokens is passed to the parent class
});
this.logger = this.logger.spawn('AgentTARS');
this.tarsOptions = tarsOptions;
this.workspace = workspace;
this.logger.info(`🤖 AgentTARS initialized | Working directory: ${workspace}`);
// Initialize browser manager instead of direct browser instance
this.browserManager = BrowserManager.getInstance(this.logger);
this.browserManager.lastLaunchOptions = {
headless: this.tarsOptions.browser?.headless,
cdpEndpoint: this.tarsOptions.browser?.cdpEndpoint,
};
if (plannerOptions?.enable) {
// Wait for impl
}
// Initialize message history dumper if experimental feature is enabled
if (options.experimental?.dumpMessageHistory) {
this.messageHistoryDumper = new MessageHistoryDumper({
workspace: this.workspace,
agentId: this.id,
agentName: this.name,
logger: this.logger,
});
this.logger.info('📝 Message history dump enabled');
}
this.eventStream.subscribe((event) => {
if (event.type === 'tool_result' && event.name === 'browser_navigate') {
event._extra = this.browserState;
}
});
// Initialize workspace path resolver
this.workspacePathResolver = new WorkspacePathResolver({
workspace: this.workspace,
});
}
/**
* Initialize in-memory MCP modules and register tools
*/
async initialize(): Promise<void> {
this.logger.info('Initializing AgentTARS ...');
try {
// Initialize browser components based on control solution
const control = this.tarsOptions.browser?.control || 'hybrid';
// Always initialize browser tools manager regardless of control mode
this.browserToolsManager = new BrowserToolsManager(this.logger, control);
this.browserToolsManager.setBrowserManager(this.browserManager);
// Initialize filesystem tools manager
this.filesystemToolsManager = new FilesystemToolsManager(this.logger, {
workspace: this.workspace,
});
// First initialize GUI Agent if needed
if (control !== 'dom') {
await this.initializeGUIAgent();
}
// Initialize search tools using direct integration with agent-infra/search
await this.initializeSearchTools();
// Then initialize MCP servers and register tools
if (this.tarsOptions.mcpImpl === 'in-memory') {
await this.initializeInMemoryMCPForBuiltInMCPServers();
}
this.logger.info('✅ AgentTARS initialization complete');
// Log all registered tools in a beautiful format
this.logRegisteredTools();
} catch (error) {
this.logger.error('❌ Failed to initialize AgentTARS:', error);
await this.cleanup();
throw error;
}
await super.initialize();
}
/**
* Initialize search tools using direct integration with agent-infra/search
*/
private async initializeSearchTools(): Promise<void> {
try {
this.logger.info('🔍 Initializing search tools with direct integration');
// Get browser instance from manager for browser_search provider if needed
// const sharedBrowser =
// this.tarsOptions.search?.provider === 'browser_search'
// ? this.browserManager.getBrowser()
// : undefined;
// Create search tool provider with configuration from options
this.searchToolProvider = new SearchToolProvider(this.logger, {
provider: this.tarsOptions.search!.provider,
count: this.tarsOptions.search!.count,
cdpEndpoint: this.tarsOptions.browser!.cdpEndpoint,
browserSearch: this.tarsOptions.search!.browserSearch,
apiKey: this.tarsOptions.search!.apiKey,
baseUrl: this.tarsOptions.search!.baseUrl,
// FIXME: Un-comment it after refine launch state management of `@agent-infra/browser` and
// externalBrowser: sharedBrowser,
});
// Create and register search tool
const searchTool = this.searchToolProvider.createSearchTool();
this.registerTool(searchTool);
this.logger.info('✅ Search tools initialized successfully');
} catch (error) {
this.logger.error('❌ Failed to initialize search tools:', error);
throw error;
}
}
/**
* Log all registered tools in a beautiful format
*/
private logRegisteredTools(): void {
try {
// Get all tools from parent class
const tools = this.getTools();
if (!tools || tools.length === 0) {
this.logger.info('🧰 No tools registered');
return;
}
const toolCount = tools.length;
// Create a beautiful header for the tools log
const header = `🧰 ${toolCount} Tools Registered 🧰`;
const separator = '═'.repeat(header.length);
this.logger.info('\n');
this.logger.info(separator);
this.logger.info(header);
this.logger.info(separator);
// Group tools by their module/category (derived from description)
const toolsByCategory: Record<string, string[]> = {};
tools.forEach((tool) => {
// Extract category from description [category] format if available
const categoryMatch = tool.description?.match(/^\[(.*?)\]/);
const category = categoryMatch ? categoryMatch[1] : 'general';
if (!toolsByCategory[category]) {
toolsByCategory[category] = [];
}
toolsByCategory[category].push(tool.name);
});
// Print tools by category
Object.entries(toolsByCategory).forEach(([category, toolNames]) => {
this.logger.info(`\n📦 ${category} (${toolNames.length}):`);
toolNames.sort().forEach((name) => {
this.logger.info(` • ${name}`);
});
});
this.logger.info('\n' + separator);
this.logger.info(`✨ Total: ${toolCount} tools ready to use`);
this.logger.info(separator + '\n');
} catch (error) {
this.logger.error('❌ Failed to log registered tools:', error);
}
}
/**
* Initialize GUI Agent for visual browser control
*/
private async initializeGUIAgent(): Promise<void> {
try {
this.logger.info('🖥️ Initializing GUI Agent for visual browser control');
// Create GUI Agent instance with browser from manager
this.browserGUIAgent = new BrowserGUIAgent({
logger: this.logger,
headless: this.tarsOptions.browser?.headless,
browser: this.browserManager.getBrowser(), // Get browser from manager
eventStream: this.eventStream, // Pass the event stream
});
// Set GUI Agent in browser tools manager
if (this.browserToolsManager) {
this.browserToolsManager.setBrowserGUIAgent(this.browserGUIAgent);
}
this.logger.info('✅ GUI Agent initialized successfully');
} catch (error) {
this.logger.error(`❌ Failed to initialize GUI Agent: ${error}`);
throw error;
}
}
/**
* Initialize in-memory mcp for built-in mcp servers using the new architecture
* with direct server creation and configuration
*/
private async initializeInMemoryMCPForBuiltInMCPServers(): Promise<void> {
try {
// Get browser instance from manager for reuse
const sharedBrowser = this.browserManager.getBrowser();
this.logger.info('Using shared browser instance for MCP servers');
// Use static imports instead of dynamic imports
const mcpModules = {
browser: browserModule,
filesystem: filesystemModule,
commands: commandsModule,
};
// Create servers with appropriate configurations
this.mcpServers = {
browser: mcpModules.browser.createServer({
externalBrowser: sharedBrowser,
enableAdBlocker: false,
launchOptions: {
headless: this.tarsOptions.browser?.headless,
},
}),
filesystem: mcpModules.filesystem.createServer({
allowedDirectories: [this.workspace],
}),
commands: mcpModules.commands.createServer(),
};
// Create in-memory clients for each server
await Promise.all(
Object.entries(this.mcpServers)
.filter(([_, server]) => server !== null) // Skip null servers
.map(async ([name, server]) => {
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
// Create a client for this server
const client = new Client(
{
name: `${name}-client`,
version: '1.0',
},
{
capabilities: {
roots: {
listChanged: true,
},
},
},
);
// Connect the client and server
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);
// Store the client for later use
this.inMemoryMCPClients[name as BuiltInMCPServerName] = client;
// FIXME: check if global logger level is working.
this.logger.info(`✅ Connected to ${name} MCP server`);
}),
);
// If browser tools manager exists, set the browser client
if (this.browserToolsManager && this.inMemoryMCPClients.browser) {
this.browserToolsManager.setBrowserClient(this.inMemoryMCPClients.browser);
}
// If filesystem tools manager exists, set the filesystem client
if (this.filesystemToolsManager && this.inMemoryMCPClients.filesystem) {
this.filesystemToolsManager.setFilesystemClient(this.inMemoryMCPClients.filesystem);
}
// Register browser tools using the strategy if available
if (this.browserToolsManager) {
const registeredTools = await this.browserToolsManager.registerTools((tool) =>
this.registerTool(tool),
);
this.logger.info(
`✅ Registered ${registeredTools.length} browser tools using '${this.tarsOptions.browser?.control || 'default'}' strategy`,
);
}
// Register filesystem tools using the manager if available
if (this.filesystemToolsManager) {
const registeredTools = await this.filesystemToolsManager.registerTools((tool) =>
this.registerTool(tool),
);
this.logger.info(
`✅ Registered ${registeredTools.length} filesystem tools with safe filtering`,
);
}
// Register remaining non-browser and non-filesystem tools
await Promise.all(
Object.entries(this.inMemoryMCPClients).map(async ([name, client]) => {
if (
(name !== 'browser' || !this.browserToolsManager) &&
(name !== 'filesystem' || !this.filesystemToolsManager)
) {
await this.registerToolsFromClient(name as BuiltInMCPServerName, client!);
}
}),
);
this.logger.info('✅ In-memory MCP initialization complete');
} catch (error) {
this.logger.error('❌ Failed to initialize in-memory MCP:', error);
throw new Error(
`Failed to initialize in-memory MCP: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
/**
* Register tools from a specific MCP client
*/
private async registerToolsFromClient(
moduleName: BuiltInMCPServerName,
client: Client,
): Promise<void> {
try {
// Get tools from the client
const tools = await client.listTools();
if (!tools || !Array.isArray(tools.tools)) {
this.logger.warn(`⚠️ No tools returned from '${moduleName}' module`);
return;
}
// Register each tool with the agent
for (const tool of tools.tools) {
const toolDefinition = new Tool({
id: tool.name,
description: `[${moduleName}] ${tool.description}`,
parameters: (tool.inputSchema || { type: 'object', properties: {} }) as JSONSchema7,
function: async (args: Record<string, unknown>) => {
try {
const result = await client.callTool({
name: tool.name,
arguments: args,
});
return result.content;
} catch (error) {
this.logger.error(`❌ Error executing tool '${tool.name}':`, error);
throw error;
}
},
});
this.registerTool(toolDefinition);
this.logger.info(`Registered tool: ${toolDefinition.name}`);
}
this.logger.info(`Registered ${tools.tools.length} MCP tools from '${moduleName}'`);
} catch (error) {
this.logger.error(`❌ Failed to register tools from '${moduleName}' module:`, error);
throw error;
}
}
/**
* Lazy browser initialization using on-demand pattern
*
* This hook intercepts tool calls and lazily initializes the browser only when
* it's first needed by a browser-related tool. It also resolves workspace paths
* for tools that work with file system operations.
*/
override async onBeforeToolCall(
id: string,
toolCall: { toolCallId: string; name: string },
args: any,
) {
if (toolCall.name.startsWith('browser')) {
// Check if browser is already launching
if (!this.browserManager.isLaunchingComplete()) {
if (this.isReplaySnapshot) {
// Skip actual browser launch in replay mode
} else {
await this.browserManager.launchBrowser({
headless: this.tarsOptions.browser?.headless,
cdpEndpoint: this.tarsOptions.browser?.cdpEndpoint,
});
}
} else {
// Check if browser is still alive, and recover if needed
const isAlive = await this.browserManager.isBrowserAlive(true);
if (!isAlive && !this.isReplaySnapshot) {
// Browser is not alive and auto-recovery failed
// Try one more explicit recovery attempt
this.logger.warn('Browser appears to be terminated, attempting explicit recovery...');
const recovered = await this.browserManager.recoverBrowser();
if (!recovered) {
this.logger.error('Browser recovery failed - tool call may not work correctly');
}
}
}
}
// Resolve workspace paths for all tools that have path parameters
if (this.workspacePathResolver.hasPathParameters(toolCall.name)) {
return this.workspacePathResolver.resolveToolPaths(toolCall.name, args);
}
return args;
}
/**
* Override the onEachAgentLoopStart method to handle GUI Agent initialization
* and planner lifecycle
* This is called at the start of each agent iteration
*/
override async onEachAgentLoopStart(sessionId: string): Promise<void> {
// If GUI Agent is enabled and the browser is launched,
// take a screenshot and send it to the event stream
if (
this.tarsOptions.browser?.control !== 'dom' &&
this.browserGUIAgent &&
this.browserManager.isLaunchingComplete()
) {
// Ensure GUI Agent has access to the current event stream
if (this.browserGUIAgent.setEventStream) {
this.browserGUIAgent.setEventStream(this.eventStream);
}
await this.browserGUIAgent?.onEachAgentLoopStart(this.eventStream, this.isReplaySnapshot);
}
// Call any super implementation if it exists
await super.onEachAgentLoopStart(sessionId);
}
override async onBeforeLoopTermination(
id: string,
finalEvent: AgentEventStream.AssistantMessageEvent,
): Promise<LoopTerminationCheckResult> {
return { finished: true };
}
override async onAgentLoopEnd(id: string): Promise<void> {
await super.onAgentLoopEnd(id);
}
/**
* Get information about the current browser control setup
* @returns Object containing mode and registered tools
*/
public getBrowserControlInfo(): { mode: string; tools: string[] } {
if (this.browserToolsManager) {
return {
mode: this.browserToolsManager.getMode(),
tools: this.browserToolsManager.getRegisteredTools(),
};
}
return {
mode: this.tarsOptions.browser?.control || 'default',
tools: [],
};
}
/**
* Clean up resources when done
*/
async cleanup(): Promise<void> {
this.logger.info('Cleaning up resources...');
const cleanupPromises: Promise<void>[] = [];
// Close each MCP client connection
for (const [name, client] of Object.entries(this.inMemoryMCPClients)) {
cleanupPromises.push(
client.close().catch((error) => {
this.logger.warn(`⚠️ Error while closing ${name} client: ${error}`);
}),
);
}
// Close each MCP server
for (const [name, server] of Object.entries(this.mcpServers)) {
if (server?.close) {
cleanupPromises.push(
server.close().catch((error) => {
this.logger.warn(`⚠️ Error while closing ${name} server: ${error}`);
}),
);
}
}
// Close the shared browser instance through the manager
cleanupPromises.push(
this.browserManager.closeBrowser().catch((error) => {
this.logger.warn(`⚠️ Error while closing shared browser: ${error}`);
}),
);
// Wait for all cleanup operations to complete
await Promise.allSettled(cleanupPromises);
// Clear references
this.inMemoryMCPClients = {};
this.mcpServers = {};
this.browserGUIAgent = undefined;
// Clear message history traces if dumper exists
if (this.messageHistoryDumper) {
this.messageHistoryDumper.clearTraces();
}
this.logger.info('✅ Cleanup complete');
}
/**
* Get the current working directory for filesystem operations
*/
public getWorkingDirectory(): string {
return this.workspace;
}
/**
* Get the logger instance used by this agent
*/
public getLogger(): ConsoleLogger {
return this.logger;
}
/**
* Override onLLMRequest hook to capture requests for message history dump
*/
override onLLMRequest(id: string, payload: LLMRequestHookPayload): void {
// Add to message history if dumper is available
if (this.messageHistoryDumper) {
this.messageHistoryDumper.addRequestTrace(id, payload);
}
}
/**
* Override onLLMResponse hook to capture responses for message history dump
*/
override onLLMResponse(id: string, payload: LLMResponseHookPayload): void {
// Add to message history if dumper is available
if (this.messageHistoryDumper) {
this.messageHistoryDumper.addResponseTrace(id, payload);
}
}
/**
* Get the current abort signal if available
* This allows other components to hook into the abort mechanism
*/
public getAbortSignal(): AbortSignal | undefined {
return this.executionController.getAbortSignal();
}
/**
* Get the browser manager instance
* This allows external components to access browser functionality
*/
getBrowserManager(): BrowserManager | undefined {
return this.browserManager;
}
/**
* Override onAfterToolCall to update browser state after tool calls
* This ensures we have the latest URL and screenshot after each browser operation
*/
override async onAfterToolCall(
id: string,
toolCall: { toolCallId: string; name: string },
result: any,
): Promise<any> {
// Call super method first
const processedResult = await super.onAfterToolCall(id, toolCall, result);
// Update browser state if tool is browser-related and state manager exists
if (
toolCall.name === 'browser_navigate' &&
this.browserManager.isLaunchingComplete() &&
(await this.browserManager.isBrowserAlive())
) {
if (this.tarsOptions.browser?.control === 'dom') {
// console.time('browser_screenshot');
const response = await this.inMemoryMCPClients['browser']?.callTool({
name: 'browser_screenshot',
arguments: {
highlight: true,
},
});
// console.timeEnd('browser_screenshot');
if (Array.isArray(response?.content)) {
const { data, type, mimeType } = response.content[1];
if (type === 'image') {
this.browserState.currentScreenshot = `data:${mimeType};base64,${data}`;
}
}
} else if (this.browserGUIAgent) {
const { compressedBase64 } = await this.browserGUIAgent.screenshot();
this.browserState.currentScreenshot = compressedBase64;
}
}
return processedResult;
}
override async onDispose(): Promise<void> {
const browserManager = this.getBrowserManager();
if (browserManager && browserManager.isLaunchingComplete()) {
console.log(`Closing browser pages for session before creating new session`);
await browserManager.closeAllPages();
}
await super.onDispose();
}
}