-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathagent_loader.py
More file actions
67 lines (53 loc) · 1.99 KB
/
Copy pathagent_loader.py
File metadata and controls
67 lines (53 loc) · 1.99 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
import os
import glob
from typing import List
# Path absolut menuju folder agency-agents-id-main
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
AGENTS_DIR = os.path.join(BASE_DIR, 'agency-agents-id')
_agents_dict = {}
_is_scanned = False
def _scan_agents():
global _is_scanned
if _is_scanned:
return
if not os.path.exists(AGENTS_DIR):
print(f"[AgentLoader] Folder {AGENTS_DIR} tidak ditemukan.")
return
# Scan seluruh file .md secara rekursif
search_pattern = os.path.join(AGENTS_DIR, '**', '*.md')
md_files = glob.glob(search_pattern, recursive=True)
for filepath in md_files:
filename = os.path.basename(filepath)
name_without_ext, _ = os.path.splitext(filename)
# Simpan pemetaan nama file
_agents_dict[filename] = filepath
_agents_dict[name_without_ext] = filepath
_is_scanned = True
def load_agent(name: str) -> str:
_scan_agents()
filepath = _agents_dict.get(name)
# Pencarian fleksibel (fallback) jika nama tidak persis sama
if not filepath:
name_lower = name.lower().replace(" ", "-")
for key, path in _agents_dict.items():
if name_lower in key.lower():
filepath = path
break
if not filepath:
return ""
try:
with open(filepath, 'r', encoding='utf-8') as f:
return f.read().strip()
except Exception as e:
print(f"[AgentLoader] Gagal membaca agent '{name}': {e}")
return ""
def compose_agents(agent_names: List[str]) -> str:
composed_prompt = []
for name in agent_names:
content = load_agent(name)
if content:
composed_prompt.append(f"=== PERSONA: {name.upper()} ===\n{content}")
else:
print(f"[AgentLoader] Peringatan: Agent '{name}' tidak ditemukan.")
# Gabungkan seluruh markdown dengan jarak newline ganda
return "\n\n".join(composed_prompt)