From 86ce9e1497c27eafd01b2dc31ee253025df9e7ea Mon Sep 17 00:00:00 2001 From: Timothyxxx <384084775@qq.com> Date: Fri, 29 Dec 2023 22:24:45 +0800 Subject: [PATCH] Initialize getters for Chrome software and general ones; Fix some examples for chrome --- desktop_env/evaluators/metrics/chrome.py | 80 +++++++++++++++++++ desktop_env/evaluators/metrics/general.py | 15 ++++ .../06fe7178-4491-4589-810f-2e2bc9502122.json | 8 +- .../12086550-11c0-466b-b367-1d9e75b3910e.json | 8 +- .../7b6c7e24-c58a-49fc-a5bb-d57b80e5b4c3.json | 8 +- .../b188fe10-ae67-4db8-a154-26a0b8ff8f1e.json | 12 --- .../bb5e4c0d-f964-439c-97b6-bdb9747de3f4.json | 10 ++- 7 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 desktop_env/evaluators/metrics/chrome.py create mode 100644 desktop_env/evaluators/metrics/general.py delete mode 100644 evaluation_examples/examples/chrome/b188fe10-ae67-4db8-a154-26a0b8ff8f1e.json diff --git a/desktop_env/evaluators/metrics/chrome.py b/desktop_env/evaluators/metrics/chrome.py new file mode 100644 index 0000000..016b09c --- /dev/null +++ b/desktop_env/evaluators/metrics/chrome.py @@ -0,0 +1,80 @@ +import json +import os +import platform +import sqlite3 + +""" +WARNING: +1. Functions from this script assume that no account is registered on Chrome, otherwise the default file path needs to be changed. +2. The functions are not tested on Windows and Mac, but they should work. +""" + + +# todo: move to getter module + +def get_default_search_engine(): + if platform.system() == 'Windows': + preference_file_path = os.path.join(os.getenv('LOCALAPPDATA'), + 'Google\\Chrome\\User Data\\Default\\Preferences') + elif platform.system() == 'Darwin': + preference_file_path = os.path.join(os.getenv('HOME'), + 'Library/Application Support/Google/Chrome/Default/Preferences') + else: + preference_file_path = os.path.join(os.getenv('HOME'), '.config/google-chrome/Default/Preferences') + + try: + with open(preference_file_path, 'r', encoding='utf-8') as file: + data = json.load(file) + + # The path within the JSON data to the default search engine might vary + search_engine = data.get('default_search_provider_data', {}).get('template_url_data', {}).get('short_name', + 'Google') + return search_engine + except Exception as e: + print(f"Error: {e}") + return "Google" + + +def get_cookie_data(): + if platform.system() == 'Windows': + chrome_cookie_file_path = os.path.join(os.getenv('LOCALAPPDATA'), 'Google\\Chrome\\User Data\\Default\\Cookies') + elif platform.system() == 'Darwin': + chrome_cookie_file_path = os.path.join(os.getenv('HOME'), + 'Library/Application Support/Google/Chrome/Default/Cookies') + else: + chrome_cookie_file_path = os.path.join(os.getenv('HOME'), '.config/google-chrome/Default/Cookies') + + try: + conn = sqlite3.connect(chrome_cookie_file_path) + cursor = conn.cursor() + + # Query to check for OpenAI cookies + cursor.execute("SELECT * FROM cookies") + cookies = cursor.fetchall() + + return cookies + except Exception as e: + print(f"Error: {e}") + return None + + +def get_bookmarks(): + if platform.system() == 'Windows': + preference_file_path = os.path.join(os.getenv('LOCALAPPDATA'), + 'Google\\Chrome\\User Data\\Default\\Bookmarks') + elif platform.system() == 'Darwin': + preference_file_path = os.path.join(os.getenv('HOME'), + 'Library/Application Support/Google/Chrome/Default/Bookmarks') + else: + preference_file_path = os.path.join(os.getenv('HOME'), '.config/google-chrome/Default/Bookmarks') + + try: + with open(preference_file_path, 'r', encoding='utf-8') as file: + data = json.load(file) + + bookmarks = data.get('roots', {}) + return bookmarks + + except Exception as e: + print(f"Error: {e}") + return None diff --git a/desktop_env/evaluators/metrics/general.py b/desktop_env/evaluators/metrics/general.py new file mode 100644 index 0000000..ace7360 --- /dev/null +++ b/desktop_env/evaluators/metrics/general.py @@ -0,0 +1,15 @@ +import os +import platform + + +# todo: move to getter module +def get_desktop_path(): + username = os.getlogin() # Get the current username + if platform.system() == "Windows": + return os.path.join("C:", "Users", username, "Desktop") + elif platform.system() == "Darwin": # macOS is identified as 'Darwin' + return os.path.join("/Users", username, "Desktop") + elif platform.system() == "Linux": + return os.path.join("/home", username, "Desktop") + else: + raise Exception("Unsupported operating system") diff --git a/evaluation_examples/examples/chrome/06fe7178-4491-4589-810f-2e2bc9502122.json b/evaluation_examples/examples/chrome/06fe7178-4491-4589-810f-2e2bc9502122.json index 2d1215b..78c34c0 100644 --- a/evaluation_examples/examples/chrome/06fe7178-4491-4589-810f-2e2bc9502122.json +++ b/evaluation_examples/examples/chrome/06fe7178-4491-4589-810f-2e2bc9502122.json @@ -8,5 +8,11 @@ "related_apps": [ "chrome" ], - "evaluator": "evaluation_dir" + "evaluator": { + "func": "", + "result": { + }, + "expected": { + } + } } diff --git a/evaluation_examples/examples/chrome/12086550-11c0-466b-b367-1d9e75b3910e.json b/evaluation_examples/examples/chrome/12086550-11c0-466b-b367-1d9e75b3910e.json index 737bb8f..10c9104 100644 --- a/evaluation_examples/examples/chrome/12086550-11c0-466b-b367-1d9e75b3910e.json +++ b/evaluation_examples/examples/chrome/12086550-11c0-466b-b367-1d9e75b3910e.json @@ -8,5 +8,11 @@ "related_apps": [ "chrome" ], - "evaluator": "evaluation_dir" + "evaluator": { + "func": "", + "result": { + }, + "expected": { + } + } } diff --git a/evaluation_examples/examples/chrome/7b6c7e24-c58a-49fc-a5bb-d57b80e5b4c3.json b/evaluation_examples/examples/chrome/7b6c7e24-c58a-49fc-a5bb-d57b80e5b4c3.json index 183aab2..7e6363e 100644 --- a/evaluation_examples/examples/chrome/7b6c7e24-c58a-49fc-a5bb-d57b80e5b4c3.json +++ b/evaluation_examples/examples/chrome/7b6c7e24-c58a-49fc-a5bb-d57b80e5b4c3.json @@ -8,5 +8,11 @@ "related_apps": [ "chrome" ], - "evaluator": "evaluation_dir" + "evaluator": { + "func": "", + "result": { + }, + "expected": { + } + } } diff --git a/evaluation_examples/examples/chrome/b188fe10-ae67-4db8-a154-26a0b8ff8f1e.json b/evaluation_examples/examples/chrome/b188fe10-ae67-4db8-a154-26a0b8ff8f1e.json deleted file mode 100644 index 52b3056..0000000 --- a/evaluation_examples/examples/chrome/b188fe10-ae67-4db8-a154-26a0b8ff8f1e.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "id": "b188fe10-ae67-4db8-a154-26a0b8ff8f1e", - "snapshot": "chrome", - "instruction": "Help me add \"bookmark this page\" button to Chrome.", - "source": "https://superuser.com/questions/1115565/how-to-add-bookmark-this-page-button-to-chrome", - "config": [], - "trajectory": "trajectories/", - "related_apps": [ - "chrome" - ], - "evaluator": "evaluation_dir" -} diff --git a/evaluation_examples/examples/chrome/bb5e4c0d-f964-439c-97b6-bdb9747de3f4.json b/evaluation_examples/examples/chrome/bb5e4c0d-f964-439c-97b6-bdb9747de3f4.json index 7ceb770..3a8ff96 100644 --- a/evaluation_examples/examples/chrome/bb5e4c0d-f964-439c-97b6-bdb9747de3f4.json +++ b/evaluation_examples/examples/chrome/bb5e4c0d-f964-439c-97b6-bdb9747de3f4.json @@ -1,12 +1,18 @@ { "id": "bb5e4c0d-f964-439c-97b6-bdb9747de3f4", "snapshot": "chrome", - "instruction": "Could you help me set my default search engine to be Chrome?", + "instruction": "Could you help me set my default search engine to be Google?", "source": "https://support.google.com/chrome/answer/95426?sjid=16867045591165135686-AP", "config": [], "trajectory": "trajectories/", "related_apps": [ "chrome" ], - "evaluator": "evaluation_dir" + "evaluator": { + "func": "", + "result": { + }, + "expected": { + } + } }