-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved computer use functionality
- Loading branch information
1 parent
87af569
commit 32f8ccc
Showing
5 changed files
with
257 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
```yaml | ||
type: function | ||
function: | ||
name: attach_file | ||
description: Attaches a file to a file input element on the page. | ||
parameters: | ||
type: object | ||
properties: | ||
file_location: | ||
type: string | ||
description: The location of the file to attach | ||
form_id: | ||
type: string | ||
description: The form id to attach the file to | ||
required: | ||
- file_location | ||
- form_id | ||
exec: | ||
language: javascript | ||
execute_on_all_frames: true | ||
trigger_response: true | ||
``` | ||
```javascript | ||
console.log("Attaching file to form: {{ form_id }} - Frame: {{ voqal_frame_id }}"); | ||
let element = document.getElementById("{{ form_id }}"); | ||
console.log({element, form_id: "{{ form_id }}"}); | ||
|
||
if (element == null) { | ||
console.error("Element not found - Frame: {{ voqal_frame_id }}"); | ||
return; | ||
} | ||
|
||
let value = "{{ file_location }}"; | ||
console.log("File: " + value); | ||
const selfHost = window.location.origin; | ||
const storageLocation = selfHost + "/{{ voqal_storage_uuid }}/" + value; | ||
console.log("Storage Location: " + storageLocation); | ||
loadFileToInput(storageLocation, element); | ||
async function loadFileToInput(url, fileInput) { | ||
try { | ||
const response = await fetch(url); | ||
const fileContent = await response.blob(); | ||
const urlSegments = url.split('/'); | ||
const fileName = urlSegments[urlSegments.length - 1]; | ||
const myFile = new File([fileContent], fileName); | ||
const dataTransfer = new DataTransfer(); | ||
dataTransfer.items.add(myFile); | ||
fileInput.files = dataTransfer.files; | ||
console.log('File loaded and set in input:', fileInput.files); | ||
fileInput.dispatchEvent(new Event('change', { bubbles: true })); | ||
} catch (error) { | ||
console.error('Error loading file:', error); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
```yaml | ||
type: function | ||
function: | ||
name: click_button | ||
description: Click a button on the current page. Use get_button_ids first to get the button coordinates. | ||
parameters: | ||
type: object | ||
properties: | ||
x_coord: | ||
type: number | ||
description: The x coordinate of the button | ||
y_coord: | ||
type: number | ||
description: The y coordinate of the button | ||
required: | ||
- x_coord | ||
- y_coord | ||
exec: kotlin | ||
``` | ||
```kotlin | ||
import java.awt.MouseInfo | ||
import java.awt.Robot | ||
import java.awt.event.InputEvent | ||
import kotlin.math.atan2 | ||
import kotlin.math.cos | ||
import kotlin.math.sin | ||
|
||
fun moveMouseSmoothly(targetX: Int, targetY: Int, steps: Int = 75, delay: Long = 5L) { | ||
val robot = Robot() | ||
val startX = MouseInfo.getPointerInfo().location.x | ||
val startY = MouseInfo.getPointerInfo().location.y | ||
|
||
val deltaX = targetX - startX | ||
val deltaY = targetY - startY | ||
val distance = Math.hypot(deltaX.toDouble(), deltaY.toDouble()) | ||
if (distance == 0.0) return | ||
|
||
val stepSize = distance / steps | ||
val angle = atan2(deltaY.toDouble(), deltaX.toDouble()) | ||
|
||
for (i in 0 until steps) { | ||
val x = startX + (i * stepSize * cos(angle)).toInt() | ||
val y = startY + (i * stepSize * sin(angle)).toInt() | ||
robot.mouseMove(x, y) | ||
Thread.sleep(delay) | ||
} | ||
|
||
robot.mouseMove(targetX, targetY) | ||
} | ||
|
||
fun clickMouse(button: Int = 1) { | ||
val robot = Robot() | ||
|
||
val buttonMask = when (button) { | ||
1 -> InputEvent.BUTTON1_DOWN_MASK // Left button | ||
2 -> InputEvent.BUTTON2_DOWN_MASK // Middle button | ||
3 -> InputEvent.BUTTON3_DOWN_MASK // Right button | ||
else -> throw IllegalArgumentException("Invalid mouse button: $button") | ||
} | ||
|
||
robot.mousePress(buttonMask) | ||
robot.mouseRelease(buttonMask) | ||
} | ||
|
||
val browserCoords = browser.uiComponent.getLocationOnScreen() | ||
moveMouseSmoothly(x_coord.toInt() + browserCoords.x, y_coord.toInt() + browserCoords.y) | ||
clickMouse() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
```yaml | ||
type: function | ||
function: | ||
name: get_available_files | ||
description: Returns a list of the files available in the local storage. This is how you get files from the "storage". | ||
exec: | ||
language: kotlin | ||
manual_confirm: true | ||
``` | ||
```kotlin | ||
import java.io.File | ||
|
||
val storageLocation = File(installDir, "storage") | ||
val files = storageLocation.listFiles() | ||
contextManager.confirmFinished( | ||
mapOf( | ||
"respId" to voqal_resp_id, | ||
"data" to mapOf( | ||
"files" to files.map { it.name } | ||
) | ||
) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters