本項目旨在增強 AI 助理的功能。AI 助理基於 OS-Copilot(GitHub),可以幫助用戶完成一些任務。
我負責增強了: 基本提示並添加了一些新功能,例如支持 Windows 環境的批處理文件執行、用於先前子任務的額外記憶體插槽、記錄先前聊天歷史以及一些新工具。
PROMPT = {
"_USER_WINDOWS_GENERATE_PROMPT": "
User's information is as follows:
System Version: {system_version} # System Information, Windows / Linux / Mac etc.
System language: {system_language}
Working Directory: {working_dir}
Task Name: {task_name}
Task Description: {task_description}
Information of Prerequisite Tasks: {pre_tasks_info} # Previous History
Code Type: {Type}"
}
PROMPT = {
# ! shell for Windows (Batch)
"_SYSTEM_WINDOWS_GENERATE_PROMPT": "
You are a world-class programmer that can complete any task by executing code, your goal is to generate the corresponding code based on the type of code to complete the task.
You could only respond with a code.
Windows Batch File code output Format:
```batch
batch code
```
The code you write should follow the following criteria:
1. You must generate code of the specified 'Code Type' to complete the task.
2. The code logic should be clear and highly readable, able to meet the requirements of the task.
"
}
### 處理不同環境
```Python
# env.py
class Env(BaseEnv):
def __init__(self):
super().__init__()
self.languages = [
PythonJupyterEnv,
Shell,
Batch, # 新增 Windows 批處理
AppleScript,
]
self._active_languages = {}
def step(self, language, code, stream=False, display=False):
...
if os.name == "nt": # Windows
# * 顯示當前目錄中的檔案
cmd = ["cmd", "/c", "dir"]
else: # Unix/Linux
# * 顯示當前目錄中的檔案
cmd = ["ls"]
class Batch(SubprocessEnv):
"
表示 Windows 命令提示字元環境,用於執行 .bat 腳本。
完全遵循 Shell 類的結構
但是不包含主線處理,因為批處理腳本不需要
"
file_extension = "bat"
name = "Batch"
aliases = ["bat", "cmd"]
def __init__(self):
"
初始化 WindowsShell 環境。
使用 cmd.exe 執行 Windows 批處理腳本 (.bat)。
"
super().__init__()
self.start_cmd = ["cmd.exe", "/c"] # "/c" 會執行並關閉 cmd
def run_bat_file(self, code):
task_complete = {
"status": False,
"content": "",
}
try:
# 步驟 0: 預處理代碼
# 添加執行結束標記
code = self.preprocess_code(code)
# 步驟 1: 創建臨時 .bat 文件
with open("temp.bat", "w") as f:
f.write(code)
# 步驟 2: 使用 subprocess.Popen 執行 .bat 腳本
process = subprocess.Popen(
["cmd.exe", "/c", "temp.bat"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True, # 捕獲輸出為文字,而非位元組
)
# 步驟 3: 監控輸出以查找 '##end_of_execution##' 標記
while True:
output = process.stdout.readline() # 每次讀取一行輸出
# 如果過程結束且無更多輸出,則跳出循環
if output == "" and process.poll() is not None:
break
# 輸出調試信息
if output:
task_complete["content"] += output
print(f"OUTPUT: {output.strip()}")
# 檢查是否找到結束執行標記
if "##end_of_execution##" in output:
task_complete["status"] = True
print("任務完成!")
break
# 暫停片刻避免忙等
time.sleep(0.1)
# 步驟 4: 檢查任何剩餘的 stderr 輸出
# 如果未找到 "##end_of_execution##",則表示任務未完成(錯誤)
# 無需更改狀態,默認為 False
error_output = process.stderr.read()
if error_output:
print(f"ERROR: {error_output.strip()}")
# 步驟 5: 確保進程已完成
process.wait()
# 步驟 6: 清理臨時文件
try:
os.remove("temp.bat")
except Exception as e:
print(f"刪除 temp.bat 時發生錯誤: {e}")
return task_complete
except Exception as e:
print(f"運行批處理腳本時出錯: {e}")
task_complete["content"] = f"運行批處理腳本時出錯: {e}"
# * 錯誤批處理仍在 temp.bat 中,可以進行調試
return task_complete
def preprocess_code(self, code):
"
預處理批處理腳本代碼。
由於批處理腳本的預處理較少,因此可以簡化。
"
return preprocess_bat(code)
def line_postprocessor(self, line):
"
後處理批處理腳本執行輸出的每一行。
參數:
line (str): 批處理腳本執行輸出的某一行。
返回:
str: 處理後的行。
"
return line
def detect_active_line(self, line):
"
批處理腳本沒有特定的活動行標記,但我們可以添加一個。
目前返回 None。
"
return None
def detect_end_of_execution(self, line):
"
檢測輸出中的執行結束標記。
參數:
line (str): 輸出中的某一行。
返回:
bool: 如果找到執行結束標記則返回 True,否則返回 False。
"
return "##end_of_execution##" in line
這是團隊項目,項目報告無法公開查看。原始 OS-Copilot 可在 GitHub 查找。