Skip to content

Commit f21f54d

Browse files
committed
fix(action-parser): match Python rounding for UI-TARS 1.5
1 parent 7986f5a commit f21f54d

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

packages/ui-tars/action-parser/src/actionParser.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,24 @@ import {
1313
} from '@ui-tars/shared/types';
1414
import isNumber from 'lodash.isnumber';
1515

16+
function roundHalfToEven(num: number): number {
17+
const integerPart = Math.trunc(num);
18+
const fraction = Math.abs(num - integerPart);
19+
20+
if (fraction !== 0.5) {
21+
return Math.round(num);
22+
}
23+
24+
const evenInteger =
25+
Math.abs(integerPart) % 2 === 0
26+
? integerPart
27+
: integerPart + Math.sign(num);
28+
29+
return evenInteger;
30+
}
31+
1632
function roundByFactor(num: number, factor: number): number {
17-
return Math.round(num / factor) * factor;
33+
return roundHalfToEven(num / factor) * factor;
1834
}
1935

2036
function floorByFactor(num: number, factor: number): number {

packages/ui-tars/action-parser/test/actionParser.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import { describe, it, expect } from 'vitest';
6+
import { UITarsModelVersion } from '@ui-tars/shared/types';
67
import { parseActionVlm } from '../src/actionParser';
78

89
describe('parseActionVlm', () => {
@@ -303,6 +304,35 @@ Action: click(start_box='[637,964,637,964]')`;
303304
});
304305

305306
describe('Box coordinates normalization', () => {
307+
it('should match Python smart_resize rounding for UI-TARS 1.5', () => {
308+
const input = `Thought: I need to click on the bottom-right corner
309+
Action: click(start_box='(1512,1008)')`;
310+
311+
const result = parseActionVlm(
312+
input,
313+
[1000, 1000],
314+
'bc',
315+
{
316+
width: 1522,
317+
height: 1022,
318+
},
319+
1,
320+
UITarsModelVersion.V1_5,
321+
);
322+
323+
expect(result).toEqual([
324+
{
325+
action_inputs: {
326+
start_box: '[1,1,1,1]',
327+
start_coords: [1522, 1022],
328+
},
329+
action_type: 'click',
330+
reflection: null,
331+
thought: 'I need to click on the bottom-right corner',
332+
},
333+
]);
334+
});
335+
306336
it('should correctly normalize box with four coordinates', () => {
307337
const input = `Thought: I need to click on this element
308338
Action: click(start_box='[130,226]')`;

0 commit comments

Comments
 (0)