Repository navigation
Expand file tree
/
Copy pathwatch_utils.py
More file actions
123 lines (103 loc) · 4.18 KB
/
Copy pathwatch_utils.py
File metadata and controls
123 lines (103 loc) · 4.18 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
import asyncio
import json
import os
import time
from groq import Groq
from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer
from src.loader import get_dir_summaries, get_file_summary
class Handler(FileSystemEventHandler):
def __init__(self, base_path, callback, queue):
self.base_path = base_path
self.callback = callback
self.queue = queue
self.events = []
print(f"Watching directory {base_path}")
async def set_summaries(self):
print(f"Getting summaries for {self.base_path}")
self.summaries = await get_dir_summaries(self.base_path)
self.summaries_cache = {s["file_path"]: s for s in self.summaries}
def update_summary(self, file_path):
print(f"Updating summary for {file_path}")
path = os.path.join(self.base_path, file_path)
if not os.path.exists(path):
self.summaries_cache.pop(file_path)
return
self.summaries_cache[file_path] = get_file_summary(path)
self.summaries = list(self.summaries_cache.values())
self.queue.put(
{
"files": [
{
"src_path": file_path,
"dst_path": file_path,
"summary": self.summaries_cache[file_path]["summary"],
}
]
}
)
def on_created(self, event: FileSystemEvent) -> None:
src_path = os.path.relpath(event.src_path, self.base_path)
print(f"Created {src_path}")
if not event.is_directory:
self.update_summary(src_path)
def on_deleted(self, event: FileSystemEvent) -> None:
src_path = os.path.relpath(event.src_path, self.base_path)
print(f"Deleted {src_path}")
if not event.is_directory:
self.update_summary(src_path)
def on_modified(self, event: FileSystemEvent) -> None:
src_path = os.path.relpath(event.src_path, self.base_path)
print(f"Modified {src_path}")
if not event.is_directory:
self.update_summary(src_path)
def on_moved(self, event: FileSystemEvent) -> None:
src_path = os.path.relpath(event.src_path, self.base_path)
dest_path = os.path.relpath(event.dest_path, self.base_path)
print(f"Moved {src_path} > {dest_path}")
self.events.append({"src_path": src_path, "dst_path": dest_path})
self.update_summary(src_path)
self.update_summary(dest_path)
print("Summaries: ", self.summaries)
print("Events: ", self.events)
files = self.callback(
summaries=self.summaries, fs_events=json.dumps(
{"files": self.events})
)
self.queue.put(files)
def create_file_tree(summaries, fs_events):
FILE_PROMPT = """
You will be provided with list of source files and a summary of their contents. For each file, propose a new path and filename, using a directory structure that optimally organizes the files using known conventions and best practices.
If the file is already named well or matches a known convention, set the destination path to the same as the source path.
Your response must be a JSON object with the following schema:
```json
{
"files": [
{
"src_path": "original file path",
"dst_path": "new file path under proposed directory structure with proposed file name"
}
]
}
```
""".strip()
WATCH_PROMPT = f"""
Here are a few examples of good file naming conventions to emulate, based on the files provided:
```json
{fs_events}
```
Include the above items in your response exactly as is, along all other proposed changes.
""".strip()
client = Groq()
cmpl = client.chat.completions.create(
messages=[
{"content": FILE_PROMPT, "role": "system"},
{"content": json.dumps(summaries), "role": "user"},
{"content": WATCH_PROMPT, "role": "system"},
{"content": json.dumps(fs_events), "role": "user"},
],
model="llama-3.1-70b-versatile",
response_format={"type": "json_object"},
temperature=0,
)
return json.loads(cmpl.choices[0].message.content)["files"]