UNPKG

@ui-tars/sdk

Version:

A powerful cross-platform(ANY device/platform) toolkit for building GUI automation agents for UI-TARS

149 lines (148 loc) 5.82 kB
/** * Copyright (c) 2025 Bytedance, Inc. and its affiliates. * SPDX-License-Identifier: Apache-2.0 */ "use strict"; var __webpack_require__ = {}; (()=>{ __webpack_require__.d = (exports1, definition)=>{ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, get: definition[key] }); }; })(); (()=>{ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); })(); (()=>{ __webpack_require__.r = function(exports1) { if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { convertToOpenAIMessages: ()=>convertToOpenAIMessages, getSummary: ()=>getSummary, preprocessResizeImage: ()=>preprocessResizeImage, parseBoxToScreenCoords: ()=>parseBoxToScreenCoords, processVlmParams: ()=>processVlmParams, replaceBase64Prefix: ()=>replaceBase64Prefix, toVlmModelFormat: ()=>toVlmModelFormat }); const external_jimp_namespaceObject = require("jimp"); const constants_namespaceObject = require("@ui-tars/shared/constants"); const external_constants_js_namespaceObject = require("./constants.js"); const parseBoxToScreenCoords = ({ boxStr, screenWidth, screenHeight, factors = external_constants_js_namespaceObject.DEFAULT_FACTORS })=>{ if (!boxStr) return { x: null, y: null }; const coords = boxStr.replace('[', '').replace(']', '').split(',').map((num)=>parseFloat(num.trim())); const [x1, y1, x2 = x1, y2 = y1] = coords; const [widthFactor, heightFactor] = factors; return { x: Math.round((x1 + x2) / 2 * screenWidth * widthFactor) / widthFactor, y: Math.round((y1 + y2) / 2 * screenHeight * heightFactor) / heightFactor }; }; const processVlmParams = (conversations, images)=>{ if (images.length > constants_namespaceObject.MAX_IMAGE_LENGTH) { const excessCount = images.length - constants_namespaceObject.MAX_IMAGE_LENGTH; images = images.slice(excessCount); let imageCountToRemove = excessCount; conversations = conversations.filter((convo)=>{ if (imageCountToRemove > 0 && convo.value === constants_namespaceObject.IMAGE_PLACEHOLDER) { imageCountToRemove--; return false; } return true; }); } return { images, conversations }; }; const toVlmModelFormat = ({ conversations, systemPrompt })=>({ conversations: conversations.map((conv, idx)=>{ if (0 === idx && 'human' === conv.from) return { from: conv.from, value: `${systemPrompt}${conv.value}` }; return { from: conv.from, value: conv.value }; }), images: conversations.filter((conv)=>conv.value === constants_namespaceObject.IMAGE_PLACEHOLDER && !!conv.screenshotBase64).map((conv)=>conv.screenshotBase64) }); const getSummary = (prediction)=>prediction.replace(/Reflection:[\s\S]*?(?=Action_Summary:|Action:|$)/g, '').trim(); const convertToOpenAIMessages = ({ conversations, images })=>{ const messages = []; let imageIndex = 0; conversations.forEach((conv)=>{ if (conv.value === constants_namespaceObject.IMAGE_PLACEHOLDER) { if (imageIndex < images.length) { messages.push({ role: 'user', content: [ { type: 'image_url', image_url: { url: `data:image/png;base64,${images[imageIndex]}` } } ] }); imageIndex++; } } else messages.push({ role: 'human' === conv.from ? 'user' : 'assistant', content: conv.value }); }); return messages; }; function replaceBase64Prefix(base64) { return base64.replace(/^data:image\/\w+;base64,/, ''); } async function preprocessResizeImage(image_base64, maxPixels) { try { const imageBuffer = Buffer.from(image_base64, 'base64'); const image = await external_jimp_namespaceObject.Jimp.read(imageBuffer); const { width, height } = image.bitmap; const currentPixels = width * height; if (currentPixels > maxPixels) { const resizeFactor = Math.sqrt(maxPixels / currentPixels); const newWidth = Math.floor(width * resizeFactor); const newHeight = Math.floor(height * resizeFactor); const resized = await image.resize({ w: newWidth, h: newHeight }).getBuffer('image/png', { quality: 60 }); return resized.toString('base64'); } const base64 = await image.getBase64('image/png', { quality: 60 }); return replaceBase64Prefix(base64); } catch (error) { console.error('preprocessResizeImage error:', error); throw error; } } var __webpack_export_target__ = exports; for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { value: true }); //# sourceMappingURL=utils.js.map