|
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 | import { AgentPlugin, COMPUTER_USE_ENVIRONMENT } from '@omni-tars/core'; |
6 | | -import { |
7 | | - Tool, |
8 | | - LLMRequestHookPayload, |
9 | | - LLMResponseHookPayload, |
10 | | - AgentEventStream, |
11 | | - ChatCompletionContentPart, |
12 | | -} from '@tarko/agent'; |
13 | | -import { |
14 | | - GUIExecuteResult, |
15 | | - convertToGUIResponse, |
16 | | - createGUIErrorResponse, |
17 | | -} from '@tarko/shared-utils'; |
| 6 | +import { Tool, LLMRequestHookPayload, ChatCompletionContentPart } from '@tarko/agent'; |
| 7 | +import { createGUIErrorResponse } from '@tarko/shared-utils'; |
18 | 8 | import { Base64ImageParser } from '@agent-infra/media-utils'; |
| 9 | +import { ImageCompressor, formatBytes } from '@tarko/shared-media-utils'; |
19 | 10 | import { setScreenInfo } from './shared'; |
20 | 11 | import { OperatorManager } from './OperatorManager'; |
21 | 12 | import { BrowserOperator } from '@gui-agent/operator-browser'; |
@@ -96,22 +87,35 @@ export class GuiAgentPlugin extends AgentPlugin { |
96 | 87 |
|
97 | 88 | const operator = await this.operatorManager.getInstance(); |
98 | 89 | const output = await operator?.doScreenshot(); |
99 | | - if (!output) { |
| 90 | + if (!output?.base64) { |
100 | 91 | this.agent.logger.error('Failed to get screenshot'); |
101 | 92 | return; |
102 | 93 | } |
103 | 94 | const base64Tool = new Base64ImageParser(output.base64); |
104 | | - const base64Uri = base64Tool.getDataUri(); |
105 | | - if (!base64Uri) { |
106 | | - this.agent.logger.error('Failed to get base64 image uri'); |
107 | | - return; |
108 | | - } |
| 95 | + const originalBuffer = Buffer.from(output.base64, 'base64'); |
| 96 | + const originalSize = originalBuffer.byteLength; |
| 97 | + |
| 98 | + // Create image compressor with WebP format and 80% quality |
| 99 | + const compressor = new ImageCompressor({ |
| 100 | + quality: 80, |
| 101 | + format: 'webp', |
| 102 | + }); |
| 103 | + const compressedBuffer = await compressor.compressToBuffer(originalBuffer); |
| 104 | + const compressedBase64 = `data:image/webp;base64,${compressedBuffer.toString('base64')}`; |
| 105 | + const compressedSize = compressedBuffer.byteLength; |
| 106 | + const compressionRatio = (((originalSize - compressedSize) / originalSize) * 100).toFixed(2); |
| 107 | + |
| 108 | + this.agent.logger.debug(`compression stat: `, { |
| 109 | + originalSize: formatBytes(originalSize), |
| 110 | + compressedSize: formatBytes(compressedSize), |
| 111 | + compressionRatio: `${compressionRatio}% reduction`, |
| 112 | + }); |
109 | 113 |
|
110 | 114 | const content: ChatCompletionContentPart[] = [ |
111 | 115 | { |
112 | 116 | type: 'image_url', |
113 | 117 | image_url: { |
114 | | - url: base64Uri, |
| 118 | + url: compressedBase64, |
115 | 119 | }, |
116 | 120 | }, |
117 | 121 | ]; |
|
0 commit comments