-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_communication.py
More file actions
154 lines (123 loc) · 5.71 KB
/
Copy pathagent_communication.py
File metadata and controls
154 lines (123 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Agent communication module for sharing files between aider instances
import os
import json
import time
import shutil
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class SharedWorkspace:
def __init__(self, base_dir="."):
"""Initialize the shared workspace for agent communication"""
self.base_dir = base_dir
self.gpt4o_dir = os.path.join(base_dir, "agent_gpt4o")
self.sonnet_dir = os.path.join(base_dir, "agent_sonnet")
self.shared_dir = os.path.join(base_dir, "shared_workspace")
# Create directories if they don't exist
for directory in [self.gpt4o_dir, self.sonnet_dir, self.shared_dir]:
os.makedirs(directory, exist_ok=True)
# Create metadata file to track file ownership and status
self.metadata_file = os.path.join(self.shared_dir, "metadata.json")
if not os.path.exists(self.metadata_file):
with open(self.metadata_file, 'w') as f:
json.dump({"files": {}}, f)
def share_file(self, source_path, agent_name):
"""Share a file from an agent's directory to the shared workspace"""
if not os.path.exists(source_path):
print(f"Error: File {source_path} does not exist")
return False
filename = os.path.basename(source_path)
dest_path = os.path.join(self.shared_dir, filename)
# Copy the file to shared workspace
shutil.copy2(source_path, dest_path)
# Update metadata
with open(self.metadata_file, 'r') as f:
metadata = json.load(f)
metadata["files"][filename] = {
"owner": agent_name,
"last_modified": time.time(),
"shared_with": [],
"status": "shared"
}
with open(self.metadata_file, 'w') as f:
json.dump(metadata, f, indent=2)
print(f"File {filename} shared by {agent_name}")
return True
def request_file(self, filename, requesting_agent):
"""Request a file from the shared workspace"""
shared_file_path = os.path.join(self.shared_dir, filename)
if not os.path.exists(shared_file_path):
print(f"Error: File {filename} not found in shared workspace")
return False
# Determine destination based on requesting agent
if requesting_agent == "gpt-4o":
dest_dir = self.gpt4o_dir
elif requesting_agent == "sonnet":
dest_dir = self.sonnet_dir
else:
print(f"Error: Unknown agent {requesting_agent}")
return False
dest_path = os.path.join(dest_dir, filename)
# Copy the file to the agent's directory
shutil.copy2(shared_file_path, dest_path)
# Update metadata
with open(self.metadata_file, 'r') as f:
metadata = json.load(f)
if filename in metadata["files"]:
if requesting_agent not in metadata["files"][filename]["shared_with"]:
metadata["files"][filename]["shared_with"].append(requesting_agent)
with open(self.metadata_file, 'w') as f:
json.dump(metadata, f, indent=2)
print(f"File {filename} requested by {requesting_agent}")
return True
class FileWatcher(FileSystemEventHandler):
def __init__(self, shared_workspace, agent_name):
self.shared_workspace = shared_workspace
self.agent_name = agent_name
def on_modified(self, event):
if event.is_directory:
return
# Check if file is in agent's directory and not already in shared workspace
if self.agent_name == "gpt-4o" and event.src_path.startswith(self.shared_workspace.gpt4o_dir):
self.shared_workspace.share_file(event.src_path, "gpt-4o")
elif self.agent_name == "sonnet" and event.src_path.startswith(self.shared_workspace.sonnet_dir):
self.shared_workspace.share_file(event.src_path, "sonnet")
def start_file_watcher(agent_name):
"""Start a file watcher for the specified agent"""
shared_workspace = SharedWorkspace()
event_handler = FileWatcher(shared_workspace, agent_name)
# Determine which directory to watch based on agent name
if agent_name == "gpt-4o":
watch_dir = shared_workspace.gpt4o_dir
elif agent_name == "sonnet":
watch_dir = shared_workspace.sonnet_dir
else:
print(f"Error: Unknown agent {agent_name}")
return None
observer = Observer()
observer.schedule(event_handler, watch_dir, recursive=True)
observer.start()
print(f"File watcher started for {agent_name} in {watch_dir}")
return observer
def stop_file_watcher(observer):
"""Stop the file watcher"""
if observer:
observer.stop()
observer.join()
if __name__ == "__main__":
print("Initializing agent communication system...")
shared_workspace = SharedWorkspace()
print(f"Created directories: {shared_workspace.gpt4o_dir}, {shared_workspace.sonnet_dir}, {shared_workspace.shared_dir}")
# Start file watchers for both agents
gpt4o_observer = start_file_watcher("gpt-4o")
sonnet_observer = start_file_watcher("sonnet")
print("Agent communication system is running. Press Ctrl+C to stop.")
try:
# Keep the script running
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping file watchers...")
stop_file_watcher(gpt4o_observer)
stop_file_watcher(sonnet_observer)
print("Agent communication system stopped.")