Skip to content

feat: add action parser supporting for ouput #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 9 additions & 4 deletions packages/ui-tars/action-parser/src/actionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,24 @@ function parseAction(actionStr: string) {
const kwargs = {};

if (argsStr.trim()) {
// Split on commas that aren't inside quotes or parentheses
const argPairs = argsStr.match(/([^,']|'[^']*')+/g) || [];
const argPairs =
argsStr.match(/([^,']|'[^']*'|'<bbox>.*?<\/bbox>')+/g) || [];

for (const pair of argPairs) {
const [key, ...valueParts] = pair.split('=');
if (!key) continue;

// Join value parts back together in case there were = signs in the value
const value = valueParts
let value = valueParts
.join('=')
.trim()
.replace(/^['"]|['"]$/g, ''); // Remove surrounding quotes

// 处理 bbox 格式
if (value.includes('<bbox>')) {
value = value.replace(/<bbox>|<\/bbox>/g, '').replace(/\s+/g, ',');
value = `(${value})`;
}

//@ts-ignore
kwargs[key.trim()] = value;
}
Expand Down
87 changes: 87 additions & 0 deletions packages/ui-tars/action-parser/test/actionParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,93 @@ Action: click(start_box='(100,200)')
});
});

// M8 mode tests
describe('M8 mode', () => {
it('should correctly parse M8 format input', () => {
const input = `Thought: 我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。
Action: click(start_box='<bbox>637 964 637 964</bbox>')`;

const result = parseActionVlm(input, [1000, 1000], 'bc');

expect(result).toEqual([
{
reflection: null,
thought:
'我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。',
action_type: 'click',
action_inputs: {
start_box: '[0.637,0.964,0.637,0.964]',
},
},
]);
});

it('should correctly parse M8 format input', () => {
const input = `Thought: 我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。
Action: click(start_box='<bbox>637 964 637 964</bbox>')`;

const result = parseActionVlm(input, [1000, 1000], 'bc', {
width: 2560,
height: 1440,
});

expect(result).toEqual([
{
reflection: null,
thought:
'我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。',
action_type: 'click',
action_inputs: {
start_box: '[0.637,0.964,0.637,0.964]',
start_coords: [1630.72, 1388.16],
},
},
]);
});

it('should correctly parse M8 format input', () => {
const input = `Thought: 我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。
Action: click(start_box='[637,964,637,964]')`;

const result = parseActionVlm(input, [1000, 1000], 'bc');

expect(result).toEqual([
{
reflection: null,
thought:
'我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。',
action_type: 'click',
action_inputs: {
start_box: '[0.637,0.964,0.637,0.964]',
},
},
]);
});

it('should correctly parse M8 format input', () => {
const input = `Thought: 我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。
Action: click(start_box='[637,964,637,964]')`;

const result = parseActionVlm(input, [1000, 1000], 'bc', {
width: 2560,
height: 1440,
});

expect(result).toEqual([
{
reflection: null,
thought:
'我看到当前屏幕显示的是一个电子表格软件和一个聊天窗口,而任务要求我需要在浏览器中搜索北京明天天气。我需要先点击任务栏上的浏览器图标来启动浏览器。',
action_type: 'click',
action_inputs: {
start_box: '[0.637,0.964,0.637,0.964]',
start_coords: [1630.72, 1388.16],
},
},
]);
});
});

describe('Box coordinates normalization', () => {
it('should correctly normalize box with four coordinates', () => {
const input = `Thought: I need to click on this element
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-tars/action-parser/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineProject({
root: './',
test: {
globals: true,
setupFiles: [resolve(__dirname, '../../scripts/vitest-setup.ts')],
setupFiles: [resolve(__dirname, '../../../scripts/vitest-setup.ts')],
environment: 'node',
includeSource: [resolve(__dirname, '.')],
},
Expand Down
Loading