473 473
474 474
475 475
476
477
478Add `com.microsoft.playwright:playwright:1.61.0` to your Maven dependencies. Run `com.microsoft.playwright.CLI` with the arguments `install chromium` from your IDE or build tool, using the same dependency classpath. Run subsequent browser actions and capture screenshots inside the setup’s `try` block before the setup closes the browser and Playwright resources.
479
480
481
482
476Start a browser instance483Start a browser instance
477 484
478```typescript485```typescript
503 page = browser.new_page(viewport={"width": 1280, "height": 720})510 page = browser.new_page(viewport={"width": 1280, "height": 720})
504```511```
505 512
513```java
514import com.microsoft.playwright.*;
515import com.microsoft.playwright.options.*;
516import com.openai.core.ObjectMappers;
517import java.nio.file.*;
518import java.util.*;
519
520try (var playwright = Playwright.create();
521 var browser =
522 playwright
523 .chromium()
524 .launch(
525 new BrowserType.LaunchOptions()
526 .setHeadless(true)
527 .setChromiumSandbox(true)
528 .setEnv(Map.of())
529 .setArgs(List.of("--disable-extensions", "--disable-file-system")))) {
530 var page = browser.newPage(new Browser.NewPageOptions().setViewportSize(1280, 720));
531
532}
533
534
535```
536
506```ruby537```ruby
507require "playwright"538require "playwright"
508 539
675vm = VM(display=":99", container_name="cua-image")706vm = VM(display=":99", container_name="cua-image")
676```707```
677 708
709```java
710import com.openai.core.ObjectMappers;
711import java.nio.charset.StandardCharsets;
712import java.nio.file.*;
713import java.util.*;
714
715
716
717record VM(String display, String containerName) {}
718
719static byte[] dockerExec(
720 String containerName,
721 String executable,
722 List<String> arguments,
723 Map<String, String> environment)
724 throws Exception {
725 var command = new ArrayList<String>(List.of("docker", "exec"));
726 environment.forEach(
727 (name, value) -> {
728 command.add("--env");
729 command.add(name + "=" + value);
730 });
731 command.add(containerName);
732 command.add(executable);
733 command.addAll(arguments);
734
735var process =
736 new ProcessBuilder(command).redirectError(ProcessBuilder.Redirect.INHERIT).start();
737 process.getOutputStream().close();
738 try (var output = process.getInputStream()) {
739 byte[] result = output.readAllBytes();
740 if (process.waitFor() != 0) throw new IllegalStateException("Container command failed");
741 return result;
742 } finally {
743 process.destroy();
744 }
745}
746
747static void xdotool(VM vm, String... arguments) throws Exception {
748 dockerExec(vm.containerName(), "xdotool", List.of(arguments), Map.of("DISPLAY", vm.display()));
749}
750```
751
678```ruby752```ruby
679def docker_exec(container_name, executable, args, decode: true, env: {})753def docker_exec(container_name, executable, args, decode: true, env: {})
680 environment = env.flat_map { |name, value| ["--env", "#{name}=#{value}"] }754 environment = env.flat_map { |name, value| ["--env", "#{name}=#{value}"] }
892 return [(point.x, point.y) for point in path]966 return [(point.x, point.y) for point in path]
893```967```
894 968
969```java
970import com.microsoft.playwright.*;
971import com.microsoft.playwright.options.*;
972import com.openai.core.ObjectMappers;
973import com.openai.models.responses.ComputerAction;
974import java.nio.file.*;
975import java.util.*;
976
977
978
979static String normalizeKey(String key) {
980 return switch (key) {
981 case "ENTER", "RETURN" -> "Enter";
982 case "ESC", "ESCAPE" -> "Escape";
983 case "TAB" -> "Tab";
984 case "SPACE" -> "Space";
985 case "BACKSPACE" -> "Backspace";
986 case "DELETE", "DEL" -> "Delete";
987 case "HOME" -> "Home";
988 case "END" -> "End";
989 case "PAGEUP" -> "PageUp";
990 case "PAGEDOWN" -> "PageDown";
991 case "UP", "ARROWUP" -> "ArrowUp";
992 case "DOWN", "ARROWDOWN" -> "ArrowDown";
993 case "LEFT", "ARROWLEFT" -> "ArrowLeft";
994 case "RIGHT", "ARROWRIGHT" -> "ArrowRight";
995 case "CTRL", "CONTROL" -> "Control";
996 case "SHIFT" -> "Shift";
997 case "ALT", "OPTION" -> "Alt";
998 case "META", "CMD", "COMMAND" -> "Meta";
999 default -> key;
1000 };
1001}
1002
1003static MouseButton normalizeButton(String button) {
1004 return switch (button) {
1005 case "left" -> MouseButton.LEFT;
1006 case "wheel" -> MouseButton.MIDDLE;
1007 case "right" -> MouseButton.RIGHT;
1008 default -> throw new IllegalArgumentException("Unsupported mouse button: " + button);
1009 };
1010}
1011
1012static List<List<Long>> normalizeDragPath(List<ComputerAction.Drag.Path> path) {
1013 return path.stream().map(point -> List.of(point.x(), point.y())).toList();
1014}
1015```
1016
895```ruby1017```ruby
896def normalize_key(key)1018def normalize_key(key)
897 names = {1019 names = {
1160 return [(point.x, point.y) for point in path]1282 return [(point.x, point.y) for point in path]
1161```1283```
1162 1284
1285```java
1286import com.openai.core.ObjectMappers;
1287import com.openai.models.responses.ComputerAction;
1288import java.nio.file.*;
1289import java.util.*;
1290
1291
1292
1293static String normalizeKey(String key) {
1294 return switch (key) {
1295 case "ENTER", "RETURN" -> "Return";
1296 case "ESC", "ESCAPE" -> "Escape";
1297 case "TAB" -> "Tab";
1298 case "SPACE" -> "space";
1299 case "BACKSPACE" -> "BackSpace";
1300 case "DELETE", "DEL" -> "Delete";
1301 case "HOME" -> "Home";
1302 case "END" -> "End";
1303 case "PAGEUP" -> "Page_Up";
1304 case "PAGEDOWN" -> "Page_Down";
1305 case "UP", "ARROWUP" -> "Up";
1306 case "DOWN", "ARROWDOWN" -> "Down";
1307 case "LEFT", "ARROWLEFT" -> "Left";
1308 case "RIGHT", "ARROWRIGHT" -> "Right";
1309 case "CTRL", "CONTROL" -> "ctrl";
1310 case "SHIFT" -> "shift";
1311 case "ALT", "OPTION" -> "alt";
1312 case "META", "CMD", "COMMAND" -> "super";
1313 default -> key;
1314 };
1315}
1316
1317static int normalizeButton(String button) {
1318 return switch (button) {
1319 case "left" -> 1;
1320 case "wheel" -> 2;
1321 case "right" -> 3;
1322 case "back" -> 8;
1323 case "forward" -> 9;
1324 default -> throw new IllegalArgumentException("Unsupported mouse button: " + button);
1325 };
1326}
1327
1328static List<List<Long>> normalizeDragPath(List<ComputerAction.Drag.Path> path) {
1329 return path.stream().map(point -> List.of(point.x(), point.y())).toList();
1330}
1331
1332static List<Integer> scrollButtons(long x, long y) {
1333 var buttons = new ArrayList<Integer>();
1334 for (long[] direction : new long[][] {{y, 4, 5}, {x, 6, 7}}) {
1335 if (direction[0] == 0) continue;
1336 int button = (int) (direction[0] < 0 ? direction[1] : direction[2]);
1337 int clicks = Math.max(1, (int) Math.abs(Math.rint(direction[0] / 100.0)));
1338 for (int i = 0; i < clicks; i++) buttons.add(button);
1339 }
1340 return buttons;
1341}
1342```
1343
1163```ruby1344```ruby
1164def normalize_xdotool_key(key)1345def normalize_xdotool_key(key)
1165 names = {1346 names = {
1394 raise ValueError(f"Unsupported action: {action.type}")1575 raise ValueError(f"Unsupported action: {action.type}")
1395```1576```
1396 1577
1578```java
1579import com.microsoft.playwright.*;
1580import com.microsoft.playwright.options.*;
1581import com.openai.core.ObjectMappers;
1582import com.openai.models.responses.ComputerAction;
1583import java.nio.file.*;
1584import java.util.*;
1585
1586
1587
1588static void handleComputerActions(Page page, List<ComputerAction> actions) {
1589 for (var action : actions) {
1590 List<String> keys = List.of();
1591 Runnable operation;
1592 if (action.isClick()) {
1593 var click = action.asClick();
1594 keys = click.keys().orElse(List.of());
1595 operation =
1596 () ->
1597 page.mouse()
1598 .click(
1599 click.x(),
1600 click.y(),
1601 new Mouse.ClickOptions()
1602 .setButton(normalizeButton(click.button().toString())));
1603 } else if (action.isDoubleClick()) {
1604 var click = action.asDoubleClick();
1605 keys = click.keys().orElse(List.of());
1606 operation = () -> page.mouse().dblclick(click.x(), click.y());
1607 } else if (action.isMove()) {
1608 var move = action.asMove();
1609 keys = move.keys().orElse(List.of());
1610 operation = () -> page.mouse().move(move.x(), move.y());
1611 } else if (action.isDrag()) {
1612 var drag = action.asDrag();
1613 keys = drag.keys().orElse(List.of());
1614 var path = normalizeDragPath(drag.path());
1615 if (path.size() < 2)
1616 throw new IllegalArgumentException("Drag requires at least two points");
1617 operation =
1618 () -> {
1619 page.mouse().move(path.get(0).get(0), path.get(0).get(1));
1620 page.mouse().down();
1621 try {
1622 for (var point : path.subList(1, path.size()))
1623 page.mouse().move(point.get(0), point.get(1));
1624 } finally {
1625 page.mouse().up();
1626 }
1627 };
1628 } else if (action.isScroll()) {
1629 var scroll = action.asScroll();
1630 keys = scroll.keys().orElse(List.of());
1631 operation =
1632 () -> {
1633 page.mouse().move(scroll.x(), scroll.y());
1634 page.mouse().wheel(scroll.scrollX(), scroll.scrollY());
1635 };
1636 } else if (action.isKeypress()) {
1637 page.keyboard()
1638 .press(
1639 String.join(
1640 "+",
1641 action.asKeypress().keys().stream().map(key -> normalizeKey(key)).toList()));
1642 continue;
1643 } else if (action.isType()) {
1644 page.keyboard().type(action.asType().text());
1645 continue;
1646 } else if (action.isWait()) {
1647 page.waitForTimeout(2000);
1648 continue;
1649 } else if (action.isScreenshot()) {
1650 continue; // The caller captures the page after processing actions.
1651 } else throw new IllegalArgumentException("Unsupported computer action");
1652 if (!keys.isEmpty()) throw new IllegalArgumentException("Use the modifier-aware handler");
1653 operation.run();
1654 }
1655}
1656```
1657
1397```ruby1658```ruby
1398def perform_browser_action(page, action)1659def perform_browser_action(page, action)
1399 case action.type1660 case action.type
1684 raise ValueError(f"Unsupported action: {action.type}")1945 raise ValueError(f"Unsupported action: {action.type}")
1685```1946```
1686 1947
1948```java
1949import com.openai.core.ObjectMappers;
1950import com.openai.models.responses.ComputerAction;
1951import java.nio.charset.StandardCharsets;
1952import java.nio.file.*;
1953import java.util.*;
1954
1955
1956
1957interface Operation {
1958 void run() throws Exception;
1959}
1960
1961static void handleComputerActions(VM vm, List<ComputerAction> actions) throws Exception {
1962 for (var action : actions) {
1963 List<String> keys = List.of();
1964 Operation operation;
1965 if (action.isClick()) {
1966 var click = action.asClick();
1967 keys = click.keys().orElse(List.of());
1968 operation =
1969 () ->
1970 xdotool(
1971 vm,
1972 "mousemove",
1973 "--sync",
1974 Long.toString(click.x()),
1975 Long.toString(click.y()),
1976 "click",
1977 Integer.toString(normalizeButton(click.button().toString())));
1978 } else if (action.isDoubleClick()) {
1979 var click = action.asDoubleClick();
1980 keys = click.keys().orElse(List.of());
1981 operation =
1982 () ->
1983 xdotool(
1984 vm,
1985 "mousemove",
1986 "--sync",
1987 Long.toString(click.x()),
1988 Long.toString(click.y()),
1989 "click",
1990 "--repeat",
1991 "2",
1992 "--delay",
1993 "100",
1994 "1");
1995 } else if (action.isMove()) {
1996 var move = action.asMove();
1997 keys = move.keys().orElse(List.of());
1998 operation =
1999 () ->
2000 xdotool(
2001 vm, "mousemove", "--sync", Long.toString(move.x()), Long.toString(move.y()));
2002 } else if (action.isDrag()) {
2003 var drag = action.asDrag();
2004 keys = drag.keys().orElse(List.of());
2005 var path = normalizeDragPath(drag.path());
2006 if (path.size() < 2)
2007 throw new IllegalArgumentException("Drag requires at least two points");
2008 operation =
2009 () -> {
2010 xdotool(
2011 vm,
2012 "mousemove",
2013 "--sync",
2014 path.get(0).get(0).toString(),
2015 path.get(0).get(1).toString(),
2016 "mousedown",
2017 "1");
2018 try {
2019 for (var point : path.subList(1, path.size()))
2020 xdotool(
2021 vm, "mousemove", "--sync", point.get(0).toString(), point.get(1).toString());
2022 } finally {
2023 xdotool(vm, "mouseup", "1");
2024 }
2025 };
2026 } else if (action.isScroll()) {
2027 var scroll = action.asScroll();
2028 keys = scroll.keys().orElse(List.of());
2029 operation =
2030 () -> {
2031 xdotool(
2032 vm, "mousemove", "--sync", Long.toString(scroll.x()), Long.toString(scroll.y()));
2033 for (int button : scrollButtons(scroll.scrollX(), scroll.scrollY()))
2034 xdotool(vm, "click", Integer.toString(button));
2035 };
2036 } else if (action.isKeypress()) {
2037 xdotool(
2038 vm,
2039 "key",
2040 String.join(
2041 "+", action.asKeypress().keys().stream().map(key -> normalizeKey(key)).toList()));
2042 continue;
2043 } else if (action.isType()) {
2044 xdotool(vm, "type", "--clearmodifiers", "--", action.asType().text());
2045 continue;
2046 } else if (action.isWait()) {
2047 Thread.sleep(2000);
2048 continue;
2049 } else if (action.isScreenshot()) {
2050 continue;
2051 } else throw new IllegalArgumentException("Unsupported computer action");
2052 if (!keys.isEmpty()) throw new IllegalArgumentException("Use the modifier-aware handler");
2053 operation.run();
2054 }
2055}
2056```
2057
1687```ruby2058```ruby
1688def run_xdotool(vm, *args)2059def run_xdotool(vm, *args)
1689 docker_exec(vm.container_name, "xdotool", args, env: { "DISPLAY" => vm.display })2060 docker_exec(vm.container_name, "xdotool", args, env: { "DISPLAY" => vm.display })
1747 2118
1748 2119
1749 2120
1750Mouse actions can include an optional `keys` array for modifier-assisted workflows such as `Ctrl`+click to open a link in a new tab or `Shift`+click to extend a selection. When `keys` is present on `click`, `double_click`, `drag`, `move`, or `scroll`, hold those modifiers for the duration of the mouse action, then release them before continuing to the next action.2121Mouse actions can include an optional `keys` array for modifier-assisted workflows such as `Ctrl`+click to open a link in a new tab or `Shift`+click to extend a selection. When `keys` is present on `click`, `double_click`, `drag`, `move`, or `scroll`, hold those modifiers throughout the mouse action, then release them before continuing to the next action.
1751 2122
1752You may also need to map model-emitted key names such as `CTRL`, `ALT`, `META`, and `ARROWLEFT` to the names your runtime expects.2123You may also need to map model-emitted key names such as `CTRL`, `ALT`, `META`, and `ARROWLEFT` to the names your runtime expects.
1753 2124
1964 raise ValueError(f"Unsupported action: {action.type}")2335 raise ValueError(f"Unsupported action: {action.type}")
1965```2336```
1966 2337
2338```java
2339import com.microsoft.playwright.*;
2340import com.microsoft.playwright.options.*;
2341import com.openai.core.ObjectMappers;
2342import com.openai.models.responses.ComputerAction;
2343import java.nio.file.*;
2344import java.util.*;
2345
2346
2347
2348static void withModifiers(Page page, List<String> keys, Runnable callback) {
2349 var pressed = new ArrayList<String>();
2350 try {
2351 for (String key : keys) {
2352 String normalized = normalizeKey(key);
2353 page.keyboard().down(normalized);
2354 pressed.add(normalized);
2355 }
2356 callback.run();
2357 } finally {
2358 Collections.reverse(pressed);
2359 for (String key : pressed) page.keyboard().up(key);
2360 }
2361}
2362
2363static void handleComputerActions(Page page, List<ComputerAction> actions) {
2364 for (var action : actions) {
2365 List<String> keys = List.of();
2366 Runnable operation;
2367 if (action.isClick()) {
2368 var click = action.asClick();
2369 keys = click.keys().orElse(List.of());
2370 operation =
2371 () ->
2372 page.mouse()
2373 .click(
2374 click.x(),
2375 click.y(),
2376 new Mouse.ClickOptions()
2377 .setButton(normalizeButton(click.button().toString())));
2378 } else if (action.isDoubleClick()) {
2379 var click = action.asDoubleClick();
2380 keys = click.keys().orElse(List.of());
2381 operation = () -> page.mouse().dblclick(click.x(), click.y());
2382 } else if (action.isMove()) {
2383 var move = action.asMove();
2384 keys = move.keys().orElse(List.of());
2385 operation = () -> page.mouse().move(move.x(), move.y());
2386 } else if (action.isDrag()) {
2387 var drag = action.asDrag();
2388 keys = drag.keys().orElse(List.of());
2389 var path = normalizeDragPath(drag.path());
2390 if (path.size() < 2)
2391 throw new IllegalArgumentException("Drag requires at least two points");
2392 operation =
2393 () -> {
2394 page.mouse().move(path.get(0).get(0), path.get(0).get(1));
2395 page.mouse().down();
2396 try {
2397 for (var point : path.subList(1, path.size()))
2398 page.mouse().move(point.get(0), point.get(1));
2399 } finally {
2400 page.mouse().up();
2401 }
2402 };
2403 } else if (action.isScroll()) {
2404 var scroll = action.asScroll();
2405 keys = scroll.keys().orElse(List.of());
2406 operation =
2407 () -> {
2408 page.mouse().move(scroll.x(), scroll.y());
2409 page.mouse().wheel(scroll.scrollX(), scroll.scrollY());
2410 };
2411 } else if (action.isKeypress()) {
2412 page.keyboard()
2413 .press(
2414 String.join(
2415 "+",
2416 action.asKeypress().keys().stream().map(key -> normalizeKey(key)).toList()));
2417 continue;
2418 } else if (action.isType()) {
2419 page.keyboard().type(action.asType().text());
2420 continue;
2421 } else if (action.isWait()) {
2422 page.waitForTimeout(2000);
2423 continue;
2424 } else if (action.isScreenshot()) {
2425 continue; // The caller captures the page after processing actions.
2426 } else throw new IllegalArgumentException("Unsupported computer action");
2427 withModifiers(page, keys, operation);
2428 }
2429}
2430```
2431
1967```ruby2432```ruby
1968def with_modifiers(page, keys)2433def with_modifiers(page, keys)
1969 pressed = []2434 pressed = []
2288 raise ValueError(f"Unsupported action: {action.type}")2753 raise ValueError(f"Unsupported action: {action.type}")
2289```2754```
2290 2755
2756```java
2757import com.openai.core.ObjectMappers;
2758import com.openai.models.responses.ComputerAction;
2759import java.nio.charset.StandardCharsets;
2760import java.nio.file.*;
2761import java.util.*;
2762
2763
2764
2765interface Operation {
2766 void run() throws Exception;
2767}
2768
2769static void withModifiers(VM vm, List<String> keys, Operation callback) throws Exception {
2770 var pressed = new ArrayList<String>();
2771 try {
2772 for (String key : keys) {
2773 String normalized = normalizeKey(key);
2774 xdotool(vm, "keydown", normalized);
2775 pressed.add(normalized);
2776 }
2777 callback.run();
2778 } finally {
2779 Collections.reverse(pressed);
2780 for (String key : pressed) xdotool(vm, "keyup", key);
2781 }
2782}
2783
2784static void handleComputerActions(VM vm, List<ComputerAction> actions) throws Exception {
2785 for (var action : actions) {
2786 List<String> keys = List.of();
2787 Operation operation;
2788 if (action.isClick()) {
2789 var click = action.asClick();
2790 keys = click.keys().orElse(List.of());
2791 operation =
2792 () ->
2793 xdotool(
2794 vm,
2795 "mousemove",
2796 "--sync",
2797 Long.toString(click.x()),
2798 Long.toString(click.y()),
2799 "click",
2800 Integer.toString(normalizeButton(click.button().toString())));
2801 } else if (action.isDoubleClick()) {
2802 var click = action.asDoubleClick();
2803 keys = click.keys().orElse(List.of());
2804 operation =
2805 () ->
2806 xdotool(
2807 vm,
2808 "mousemove",
2809 "--sync",
2810 Long.toString(click.x()),
2811 Long.toString(click.y()),
2812 "click",
2813 "--repeat",
2814 "2",
2815 "--delay",
2816 "100",
2817 "1");
2818 } else if (action.isMove()) {
2819 var move = action.asMove();
2820 keys = move.keys().orElse(List.of());
2821 operation =
2822 () ->
2823 xdotool(
2824 vm, "mousemove", "--sync", Long.toString(move.x()), Long.toString(move.y()));
2825 } else if (action.isDrag()) {
2826 var drag = action.asDrag();
2827 keys = drag.keys().orElse(List.of());
2828 var path = normalizeDragPath(drag.path());
2829 if (path.size() < 2)
2830 throw new IllegalArgumentException("Drag requires at least two points");
2831 operation =
2832 () -> {
2833 xdotool(
2834 vm,
2835 "mousemove",
2836 "--sync",
2837 path.get(0).get(0).toString(),
2838 path.get(0).get(1).toString(),
2839 "mousedown",
2840 "1");
2841 try {
2842 for (var point : path.subList(1, path.size()))
2843 xdotool(
2844 vm, "mousemove", "--sync", point.get(0).toString(), point.get(1).toString());
2845 } finally {
2846 xdotool(vm, "mouseup", "1");
2847 }
2848 };
2849 } else if (action.isScroll()) {
2850 var scroll = action.asScroll();
2851 keys = scroll.keys().orElse(List.of());
2852 operation =
2853 () -> {
2854 xdotool(
2855 vm, "mousemove", "--sync", Long.toString(scroll.x()), Long.toString(scroll.y()));
2856 for (int button : scrollButtons(scroll.scrollX(), scroll.scrollY()))
2857 xdotool(vm, "click", Integer.toString(button));
2858 };
2859 } else if (action.isKeypress()) {
2860 xdotool(
2861 vm,
2862 "key",
2863 String.join(
2864 "+", action.asKeypress().keys().stream().map(key -> normalizeKey(key)).toList()));
2865 continue;
2866 } else if (action.isType()) {
2867 xdotool(vm, "type", "--clearmodifiers", "--", action.asType().text());
2868 continue;
2869 } else if (action.isWait()) {
2870 Thread.sleep(2000);
2871 continue;
2872 } else if (action.isScreenshot()) {
2873 continue;
2874 } else throw new IllegalArgumentException("Unsupported computer action");
2875 withModifiers(vm, keys, operation);
2876 }
2877}
2878```
2879
2291```ruby2880```ruby
2292def with_modifiers(vm, keys)2881def with_modifiers(vm, keys)
2293 pressed = []2882 pressed = []
2738 return page.screenshot(type="png")3327 return page.screenshot(type="png")
2739```3328```
2740 3329
3330```java
3331import com.microsoft.playwright.*;
3332import com.microsoft.playwright.options.*;
3333import com.openai.core.ObjectMappers;
3334import java.nio.file.*;
3335import java.util.*;
3336
3337
3338
3339static byte[] captureScreenshot(Page page) {
3340 return page.screenshot(new Page.ScreenshotOptions().setType(ScreenshotType.PNG));
3341}
3342```
3343
2741```ruby3344```ruby
2742def capture_screenshot(page)3345def capture_screenshot(page)
2743 page.screenshot(type: "png")3346 page.screenshot(type: "png")
2778 )3381 )
2779```3382```
2780 3383
3384```java
3385import com.openai.core.ObjectMappers;
3386import java.nio.file.*;
3387import java.util.*;
3388
3389
3390
3391static byte[] captureScreenshot(VM vm) throws Exception {
3392 return dockerExec(
3393 vm.containerName(),
3394 "import",
3395 List.of("-window", "root", "png:-"),
3396 Map.of("DISPLAY", vm.display()));
3397}
3398```
3399
2781```ruby3400```ruby
2782def capture_screenshot(vm)3401def capture_screenshot(vm)
2783 docker_exec(3402 docker_exec(
2813 3432
2814The examples here use ordinary function tools named `exec_js` and `exec_py`. Their `code` argument contains the generated script. Your application sends that script to your execution service, then returns its text and image outputs to the model. If the model asks for clarification instead of returning a tool call, surface that question to the user before continuing.3433The examples here use ordinary function tools named `exec_js` and `exec_py`. Their `code` argument contains the generated script. Your application sends that script to your execution service, then returns its text and image outputs to the model. If the model asks for clarification instead of returning a tool call, surface that question to the user before continuing.
2815 3434
2816The code runtime can be temporary or persistent. If you need to resume the same browser session, preserve that session separately from individual scripts. A persistent runtime can also retain variables between tool calls. Tell the model which objects, helpers, and state are available.3435The code runtime can be temporary or persistent. If you need to continue the same browser session, preserve that session separately from individual scripts. A persistent runtime can also retain variables between tool calls. Tell the model which objects, helpers, and state are available.
2817 3436
2818Provide only the capabilities the task requires:3437Provide only the capabilities the task requires:
2819 3438
2852go get github.com/openai/openai-go/v3@v3.70.03471go get github.com/openai/openai-go/v3@v3.70.0
2853```3472```
2854 3473
2855For Java 17 or later, add these dependencies to your Maven `pom.xml`:3474For Java, add these dependencies to your Maven `pom.xml`:
2856 3475
2857```xml3476```xml
2858<dependencies>3477<dependencies>
3232 3851
3233Check permissions before executing an action. For an action batch, stop before the first action that needs confirmation. For generated code, enforce permissions in the exposed helpers and runtime; a single script can perform many actions. Instructions to the model complement these controls but do not replace them.3852Check permissions before executing an action. For an action batch, stop before the first action that needs confirmation. For generated code, enforce permissions in the exposed helpers and runtime; a single script can perform many actions. Instructions to the model complement these controls but do not replace them.
3234 3853
3235Let the agent complete safe work before pausing at the point of risk. Explain the proposed action, obtain any required consent, and resume only the approved work. If the user declines, do not execute the request. Your integration must communicate what did and did not run before asking the model to continue.3854Let the agent complete safe work before pausing at the point of risk. Explain the proposed action, obtain any required consent, and continue only the approved work. If the user declines, do not execute the request. Your integration must communicate what did and did not run before asking the model to continue.
3236 3855
3237<a id="keep-a-human-in-the-loop"></a>3856<a id="keep-a-human-in-the-loop"></a>
3238 3857