Spaces:
Sleeping
Sleeping
File size: 3,230 Bytes
c5997d8 | 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 | from datetime import datetime, timedelta
class RuleBasedAgent:
def choose_action(self, state):
files = state.get("files", [])
task = state.get("task")
# -------------------------------
# EASY TASK
# -------------------------------
if task == "easy":
for f in files:
name = f.get("name")
path = f.get("path", "")
# Skip already organized
if "/" in path:
continue
if name.endswith(".pdf"):
return {
"action": "move_file",
"filename": name,
"destination": "Documents"
}
if name.endswith(".jpg"):
return {
"action": "move_file",
"filename": name,
"destination": "Images"
}
return {
"action": "move_file",
"filename": name,
"destination": "Misc"
}
# -------------------------------
# MEDIUM TASK
# -------------------------------
elif task == "medium":
for f in files:
name = f.get("name")
name_lower = name.lower()
# Detect duplicate patterns
if (
"(" in name_lower and ")" in name_lower
or "_copy" in name_lower
or " copy" in name_lower
):
return {
"action": "delete_file",
"filename": name
}
return {
"action": "done"
}
# -------------------------------
# HARD TASK
# -------------------------------
elif task == "hard":
now = datetime.now()
threshold = now - timedelta(days=30)
# Sort largest files first
files_sorted = sorted(
files,
key=lambda x: x.get("size_mb", 0),
reverse=True
)
for f in files_sorted:
name = f.get("name")
size = f.get("size_mb", 0)
last_accessed = f.get("last_accessed")
if not last_accessed:
continue
try:
last_accessed_dt = datetime.fromisoformat(
last_accessed
)
except Exception:
continue
# Safe deletion rule
if (
last_accessed_dt < threshold
and size > 10
):
return {
"action": "delete_file",
"filename": name
}
return {
"action": "done"
}
# -------------------------------
# DEFAULT
# -------------------------------
return {
"action": "done"
} |