Skip to content
Merged
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
17 changes: 17 additions & 0 deletions packages/core/src/tools/computer-use/tool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('coerceTypes', () => {
properties: {
x: { type: 'number' },
y: { type: 'number' },
element_index: { type: 'integer' },
label: { type: 'string' },
app: { type: 'string' },
},
Expand All @@ -153,6 +154,22 @@ describe('coerceTypes', () => {
expect(typeof result['y']).toBe('number');
});

it('coerces integer strings to numbers (schema type: integer)', () => {
const result = coerceTypes({ app: 'X', element_index: '11' }, schema);
expect(result['element_index']).toBe(11);
expect(typeof result['element_index']).toBe('number');
});

it('does not truncate fractional strings for integer fields', () => {
const result = coerceTypes({ app: 'X', element_index: '11.5' }, schema);
expect(result['element_index']).toBe('11.5');
});

it('continues to coerce fractional strings for number fields', () => {
const result = coerceTypes({ app: 'X', x: '11.5' }, schema);
expect(result['x']).toBe(11.5);
});

// Direction 2: number → string (schema wants string, model sent number)
it('coerces integer label to string (schema type: string)', () => {
const result = coerceTypes({ app: 'X', label: 11 }, schema);
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/tools/computer-use/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ export function coerceTypes(
) {
const trimmed = value.trim();
// Only coerce if the string is a clean numeric — don't swallow garbage.
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
const matchesType =
fieldType === 'integer'
? /^-?\d+$/.test(trimmed)
: /^-?\d+(\.\d+)?$/.test(trimmed);
if (matchesType) {
const parsed =
fieldType === 'integer' ? parseInt(trimmed, 10) : parseFloat(trimmed);
if (Number.isFinite(parsed)) {
Expand Down
Loading