amangs commited on
Commit
07fbb1b
·
1 Parent(s): 5dc4736

Remove legacy standalone telegram_agent and deprecated src/data_sync

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .env
Dockerfile CHANGED
@@ -238,7 +238,6 @@ RUN npm config set ignore-scripts false && \
238
  WORKDIR /app
239
  COPY config/ /app/config/
240
  COPY services/ /app/services/
241
- COPY src/ /app/src/
242
  COPY entrypoint.sh /app/entrypoint.sh
243
  RUN chmod +x /app/entrypoint.sh /app/services/*.sh /app/services/*.py && mkdir -p /data
244
 
 
238
  WORKDIR /app
239
  COPY config/ /app/config/
240
  COPY services/ /app/services/
 
241
  COPY entrypoint.sh /app/entrypoint.sh
242
  RUN chmod +x /app/entrypoint.sh /app/services/*.sh /app/services/*.py && mkdir -p /data
243
 
services/30-telegram-agent.sh DELETED
@@ -1,43 +0,0 @@
1
- #!/bin/bash
2
- # 30-telegram-agent.sh — Avi Telegram AI Coding Agent (@amangt_bot)
3
- mkdir -p /tmp/logs /data/workspace
4
- LOG="/tmp/logs/telegram-agent.log"
5
-
6
- if [ -f /tmp/.env ]; then
7
- set -a
8
- . /tmp/.env
9
- set +a
10
- fi
11
- if [ -f /data/.env ]; then
12
- set -a
13
- . /data/.env
14
- set +a
15
- fi
16
-
17
- # Disable redundant standalone telegram_agent if native Hermes gateway handles Telegram
18
- if [ -n "$TELEGRAM_BOT_TOKEN" ]; then
19
- echo "TELEGRAM_BOT_TOKEN is present; handled directly by Hermes Gateway platform plugin." >> "$LOG"
20
- exit 0
21
- fi
22
-
23
- TOKEN="${TELEGRAM_BOT_TOKEN:-}"
24
- if [ -z "$TOKEN" ]; then
25
- echo "TELEGRAM_BOT_TOKEN not set, skipping telegram agent" >> "$LOG"
26
- exit 0
27
- fi
28
-
29
- export TELEGRAM_BOT_TOKEN="$TOKEN"
30
- export TELEGRAM_BOT_APIBASE="${TELEGRAM_BOT_APIBASE:-tgp.ind.pp.ua}"
31
- export ALLOWED_USERS="${TELEGRAM_USER:-973904286}"
32
- export LLM_AMAN_BASE_URL="${LLM_AMAN_BASE_URL:-https://llm.aman.ltd/v1}"
33
- export LLM_AMAN_API_KEY="${LLM_AMAN_API_KEY:-sk-mSm7P3YFDdeasXvlWgLKCGHsucUtAWJJBnLapDyRVUe6TV8w}"
34
- export DEFAULT_MODEL="gemini-3.8-flash-high"
35
- export WORKSPACE="/data/workspace"
36
-
37
- if pgrep -f "services/telegram_agent.py" >/dev/null 2>&1; then
38
- echo "telegram_agent already running"
39
- exit 0
40
- fi
41
-
42
- cd /app || cd /data || exit 1
43
- exec python3 /app/services/telegram_agent.py >> "$LOG" 2>&1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
services/telegram_agent.py DELETED
@@ -1,170 +0,0 @@
1
- import os
2
- import sys
3
- import json
4
- import asyncio
5
- import subprocess
6
- import httpx
7
- from telegram import Update
8
- from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
9
-
10
- BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "")
11
- API_BASE = os.environ.get("TELEGRAM_BOT_APIBASE", "api.telegram.org")
12
- ALLOWED_USERS = [int(u.strip()) for u in os.environ.get("ALLOWED_USERS", "973904286").split(",") if u.strip().isdigit()]
13
- LLM_BASE = os.environ.get("LLM_AMAN_BASE_URL", "https://llm.aman.ltd/v1")
14
- LLM_KEY = os.environ.get("LLM_AMAN_API_KEY", "")
15
- DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "gemini-3.8-flash-high")
16
- WORKSPACE = os.environ.get("WORKSPACE", "/data/workspace")
17
-
18
- if not BOT_TOKEN:
19
- print("TELEGRAM_BOT_TOKEN not configured. Exiting.")
20
- sys.exit(0)
21
-
22
- # In-memory per-user chat history
23
- HISTORIES = {}
24
- CURRENT_MODELS = {}
25
-
26
- TOOLS = [
27
- {
28
- "type": "function",
29
- "function": {
30
- "name": "bash",
31
- "description": "Execute a shell command in the persistent /data/workspace directory.",
32
- "parameters": {
33
- "type": "object",
34
- "properties": {
35
- "command": {"type": "string", "description": "The shell command to run"}
36
- },
37
- "required": ["command"]
38
- }
39
- }
40
- }
41
- ]
42
-
43
- def run_bash(cmd: str) -> str:
44
- try:
45
- res = subprocess.run(cmd, shell=True, cwd=WORKSPACE, capture_output=True, text=True, timeout=120)
46
- out = (res.stdout + res.stderr).strip()
47
- return out if out else "(command produced no output, exit 0)"
48
- except subprocess.TimeoutExpired:
49
- return "Error: Command timed out after 120 seconds."
50
- except Exception as e:
51
- return f"Error executing command: {e}"
52
-
53
- async def call_llm(user_id: int, user_msg: str) -> str:
54
- model = CURRENT_MODELS.get(user_id, DEFAULT_MODEL)
55
- history = HISTORIES.setdefault(user_id, [
56
- {"role": "system", "content": f"You are Avi, an expert software engineer and reverse-engineering assistant on a dedicated Linux development server. You have full terminal access via the bash tool. Working directory is {WORKSPACE}. Answer concisely and accurately. Use your tools to inspect, build, and debug."}
57
- ])
58
- history.append({"role": "user", "content": user_msg})
59
-
60
- # Cap history to last 16 messages
61
- if len(history) > 17:
62
- history = [history[0]] + history[-16:]
63
- HISTORIES[user_id] = history
64
-
65
- headers = {"Authorization": f"Bearer {LLM_KEY}", "Content-Type": "application/json"}
66
- payload = {
67
- "model": model,
68
- "messages": history,
69
- "tools": TOOLS,
70
- "temperature": 0.2
71
- }
72
-
73
- async with httpx.AsyncClient(timeout=60.0) as client:
74
- for _ in range(5): # Tool call loop up to 5 turns
75
- try:
76
- resp = await client.post(f"{LLM_BASE}/chat/completions", headers=headers, json=payload)
77
- if resp.status_code != 200:
78
- return f"API Error {resp.status_code}: {resp.text[:300]}"
79
- data = resp.json()
80
- msg = data["choices"][0]["message"]
81
- except Exception as e:
82
- return f"LLM error: {e}"
83
-
84
- tool_calls = msg.get("tool_calls")
85
- if not tool_calls:
86
- reply = msg.get("content", "")
87
- history.append({"role": "assistant", "content": reply})
88
- return reply
89
-
90
- # Handle tool calls
91
- history.append(msg)
92
- for tc in tool_calls:
93
- fn_name = tc["function"]["name"]
94
- try:
95
- fn_args = json.loads(tc["function"]["arguments"])
96
- except Exception:
97
- fn_args = {}
98
- if fn_name == "bash":
99
- cmd = fn_args.get("command", "")
100
- output = run_bash(cmd)
101
- else:
102
- output = f"Unknown tool {fn_name}"
103
-
104
- history.append({
105
- "role": "tool",
106
- "tool_call_id": tc["id"],
107
- "content": output[:8000]
108
- })
109
-
110
- return "Reached maximum tool execution turns."
111
-
112
- async def handle_start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
113
- user_id = update.effective_user.id
114
- if ALLOWED_USERS and user_id not in ALLOWED_USERS:
115
- return
116
- model = CURRENT_MODELS.get(user_id, DEFAULT_MODEL)
117
- await update.message.reply_text(
118
- f"Avi online.\nModel: {model} (via {LLM_BASE})\nWorkspace: {WORKSPACE}\nSend a message to start coding or running commands."
119
- )
120
-
121
- async def handle_model(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
122
- user_id = update.effective_user.id
123
- if ALLOWED_USERS and user_id not in ALLOWED_USERS:
124
- return
125
- args = context.args
126
- if not args:
127
- cur = CURRENT_MODELS.get(user_id, DEFAULT_MODEL)
128
- await update.message.reply_text(f"Current model: {cur}\nUsage: /model <name>\nPopular: gemini-3.8-flash-high, deepseek-v4-flash, Codestral, Devstral 2, gpt-oss:120b")
129
- return
130
- new_model = args[0]
131
- CURRENT_MODELS[user_id] = new_model
132
- await update.message.reply_text(f"Model switched to: {new_model}")
133
-
134
- async def handle_clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
135
- user_id = update.effective_user.id
136
- HISTORIES[user_id] = []
137
- await update.message.reply_text("Conversation history cleared.")
138
-
139
- async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
140
- user_id = update.effective_user.id
141
- if ALLOWED_USERS and user_id not in ALLOWED_USERS:
142
- return
143
- text = update.message.text
144
- if not text:
145
- return
146
- await context.bot.send_chat_action(chat_id=update.effective_chat.id, action="typing")
147
- reply = await call_llm(user_id, text)
148
- # Split long replies to Telegram 4096 limit
149
- for chunk in [reply[i:i+4000] for i in range(0, len(reply), 4000)]:
150
- await update.message.reply_text(chunk)
151
-
152
- def main():
153
- builder = Application.builder()
154
- builder.token(BOT_TOKEN)
155
- if API_BASE and API_BASE != "api.telegram.org":
156
- builder.base_url(f"https://{API_BASE}/bot")
157
- builder.base_file_url(f"https://{API_BASE}/file/bot")
158
-
159
- app = builder.build()
160
- app.add_handler(CommandHandler("start", handle_start))
161
- app.add_handler(CommandHandler("model", handle_model))
162
- app.add_handler(CommandHandler("clear", handle_clear))
163
- app.add_handler(CommandHandler("reset", handle_clear))
164
- app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
165
-
166
- print(f"Starting Avi Telegram agent using {API_BASE}...")
167
- app.run_polling()
168
-
169
- if __name__ == "__main__":
170
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/__init__.py DELETED
File without changes
src/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (129 Bytes)
 
src/__pycache__/data_sync.cpython-311.pyc DELETED
Binary file (16.7 kB)
 
src/data_sync.py DELETED
@@ -1,283 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Hermes Agent Data Sync Service
4
- Handles data persistence to/from Hugging Face Dataset strictly on SYNC_INTERVAL schedule
5
- """
6
-
7
- import os
8
- import sys
9
- import time
10
- import json
11
- import shutil
12
- import hashlib
13
- import argparse
14
- from pathlib import Path
15
- from datetime import datetime
16
- from typing import Optional, Dict, List
17
-
18
- from huggingface_hub import HfApi, hf_hub_download, upload_folder
19
- from loguru import logger
20
-
21
-
22
- class DatasetManager:
23
- """Manages data synchronization with Hugging Face Dataset"""
24
-
25
- def __init__(self, dataset_repo: Optional[str] = None, token: Optional[str] = None):
26
- self.dataset_repo = dataset_repo or os.environ.get('HF_DATASET_REPO')
27
- self.token = token or os.environ.get('HF_TOKEN') or os.environ.get('HUGGING_FACE_HUB_TOKEN')
28
- self.api = HfApi(token=self.token)
29
- self.hermes_home = Path(os.environ.get('HERMES_HOME', '/home/appuser/.hermes'))
30
- self.webui_home = Path(os.environ.get('HERMES_WEB_UI_HOME', '/home/appuser/.hermes-web-ui'))
31
- self.temp_dir = Path('/tmp/hermes_sync')
32
- self.last_sync_hash = None
33
-
34
- # Directories that represent REAL persistent user data
35
- self.backup_dirs = [
36
- 'memories',
37
- 'skills',
38
- 'sessions',
39
- 'cron',
40
- 'script',
41
- 'plugins',
42
- 'extensions',
43
- 'prompts',
44
- 'knowledge',
45
- ]
46
-
47
- # Files that represent REAL persistent user config
48
- self.backup_files = [
49
- 'config.yaml',
50
- '.env',
51
- 'auth.json',
52
- 'SOUL.md',
53
- 'state.db',
54
- 'channel_directory.json',
55
- ]
56
-
57
- # STRICT exclusion: never commit volatile logs, locks, or temporary files
58
- self.exclude_patterns = {
59
- '__pycache__', '.git', 'node_modules', '.cache', 'tmp', 'reports', 'src',
60
- 'logs', 'gateway.lock', 'gateway.pid', 'gateway_state.json',
61
- '*.log', '*.db-shm', '*.db-wal', 'bridge.log', 'server.log',
62
- '.login-lock.json', 'preview-action.log'
63
- }
64
-
65
- def validate(self) -> bool:
66
- if not self.dataset_repo:
67
- logger.error("HF_DATASET_REPO not set")
68
- return False
69
- if not self.token:
70
- logger.warning("HF_TOKEN not set")
71
- return True
72
-
73
- def calculate_data_hash(self) -> str:
74
- """Calculate hash of actual user data (sessions, memories, skills, config, webui users)"""
75
- hasher = hashlib.md5()
76
-
77
- for fn in self.backup_files:
78
- fp = self.hermes_home / fn
79
- if fp.exists() and fp.is_file():
80
- try:
81
- stat = fp.stat()
82
- hasher.update(f"{fn}:{stat.st_size}:{stat.st_mtime}".encode())
83
- except:
84
- pass
85
-
86
- for dname in self.backup_dirs:
87
- dp = self.hermes_home / dname
88
- if dp.exists():
89
- for root, _, files in os.walk(dp):
90
- for f in sorted(files):
91
- if any(f.endswith(ext) for ext in ['.log', '.lock', '.pid', '.tmp', '.json']):
92
- # session request dumps / temporary jsons shouldn't trigger commit loops
93
- if 'request_dump' in f:
94
- continue
95
- try:
96
- stat = os.stat(os.path.join(root, f))
97
- hasher.update(f"{f}:{stat.st_size}:{stat.st_mtime}".encode())
98
- except:
99
- pass
100
-
101
- webui_db = self.webui_home / 'hermes-web-ui.db'
102
- if webui_db.exists():
103
- try:
104
- stat = webui_db.stat()
105
- hasher.update(f"webui_db:{stat.st_size}".encode())
106
- except:
107
- pass
108
-
109
- return hasher.hexdigest()
110
-
111
- def prepare_backup_data(self) -> Path:
112
- if self.temp_dir.exists():
113
- shutil.rmtree(self.temp_dir)
114
- self.temp_dir.mkdir(parents=True)
115
-
116
- for dirname in self.backup_dirs:
117
- (self.temp_dir / dirname).mkdir(exist_ok=True)
118
-
119
- def should_exclude(src: str, names: list) -> bool:
120
- ignored = []
121
- for n in names:
122
- if n in self.exclude_patterns or any(n.endswith(ext) for ext in ['.log', '.lock', '.pid', '.db-shm', '.db-wal']):
123
- ignored.append(n)
124
- if 'request_dump' in n:
125
- ignored.append(n)
126
- return ignored
127
-
128
- try:
129
- for dirname in self.backup_dirs:
130
- src_dir = self.hermes_home / dirname
131
- if src_dir.exists():
132
- shutil.copytree(src_dir, self.temp_dir / dirname, dirs_exist_ok=True, ignore=should_exclude)
133
-
134
- for filename in self.backup_files:
135
- src_file = self.hermes_home / filename
136
- if src_file.exists():
137
- shutil.copy2(src_file, self.temp_dir / filename)
138
-
139
- # Backup Web UI sqlite db and tokens only (exclude logs/locks)
140
- if self.webui_home.exists():
141
- webui_dst = self.temp_dir / 'webui_data'
142
- webui_dst.mkdir(parents=True, exist_ok=True)
143
- for f in os.listdir(self.webui_home):
144
- if f.endswith(('.db', '.token')) and not f.endswith(('.db-shm', '.db-wal', '.lock.json')):
145
- src_p = self.webui_home / f
146
- if src_p.is_file():
147
- shutil.copy2(src_p, webui_dst / f)
148
-
149
- return self.temp_dir
150
-
151
- except Exception as e:
152
- logger.error(f"Failed to prepare backup: {e}")
153
- raise
154
-
155
- def upload_to_dataset(self, force: bool = False) -> bool:
156
- try:
157
- current_hash = self.calculate_data_hash()
158
- if not force and self.last_sync_hash is not None and current_hash == self.last_sync_hash:
159
- logger.info("No data changes detected. Skipping backup upload.")
160
- return True
161
-
162
- backup_dir = self.prepare_backup_data()
163
- logger.info(f"Uploading scheduled backup to dataset: {self.dataset_repo}")
164
-
165
- self.api.upload_folder(
166
- folder_path=str(backup_dir),
167
- repo_id=self.dataset_repo,
168
- repo_type="dataset",
169
- commit_message=f"Hermes Agent backup - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
170
- )
171
-
172
- # Squash commit history so the dataset repo always stays at 1 commit
173
- try:
174
- self.api.super_squash_history(
175
- repo_id=self.dataset_repo,
176
- repo_type="dataset",
177
- commit_message=f"Hermes Agent backup - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
178
- )
179
- except Exception as se:
180
- logger.warning(f"Squash history failed: {se}")
181
-
182
- self.last_sync_hash = current_hash
183
- logger.success("Backup uploaded successfully")
184
- return True
185
-
186
- except Exception as e:
187
- logger.error(f"Failed to upload to dataset: {e}")
188
- return False
189
-
190
- def download_from_dataset(self) -> bool:
191
- try:
192
- logger.info(f"Downloading from dataset: {self.dataset_repo}")
193
- download_dir = Path('/tmp/hermes_download')
194
- if download_dir.exists():
195
- shutil.rmtree(download_dir)
196
- download_dir.mkdir(parents=True)
197
-
198
- self.api.snapshot_download(
199
- repo_id=self.dataset_repo,
200
- repo_type="dataset",
201
- local_dir=str(download_dir)
202
- )
203
-
204
- self.restore_from_download(download_dir)
205
- return True
206
- except Exception as e:
207
- logger.error(f"Failed to download from dataset: {e}")
208
- return False
209
-
210
- def restore_from_download(self, download_dir: Path):
211
- self.hermes_home.mkdir(parents=True, exist_ok=True)
212
- self.webui_home.mkdir(parents=True, exist_ok=True)
213
-
214
- skip_restore = os.environ.get('SKIP_CONFIG_RESTORE', 'true').lower() in ('true', '1', 'yes')
215
-
216
- restore_list = []
217
- for dirname in self.backup_dirs:
218
- restore_list.append((dirname, self.hermes_home / dirname))
219
- for filename in self.backup_files:
220
- restore_list.append((filename, self.hermes_home / filename))
221
-
222
- if not skip_restore:
223
- restore_list.append(('config.yaml', self.hermes_home / 'config.yaml'))
224
- else:
225
- restored_path = self.hermes_home / 'config.yaml.restored'
226
- src = download_dir / 'config.yaml'
227
- if src.exists():
228
- shutil.copy2(src, restored_path)
229
-
230
- for src_rel, dst in restore_list:
231
- src = download_dir / src_rel
232
- if src.exists():
233
- try:
234
- if src.is_file():
235
- dst.parent.mkdir(parents=True, exist_ok=True)
236
- shutil.copy2(src, dst)
237
- elif src.is_dir():
238
- if dst.exists():
239
- shutil.rmtree(dst)
240
- shutil.copytree(src, dst)
241
- except Exception as e:
242
- logger.error(f"Failed to restore {src_rel}: {e}")
243
-
244
- webui_src = download_dir / 'webui_data'
245
- if webui_src.exists():
246
- shutil.copytree(webui_src, self.webui_home, dirs_exist_ok=True)
247
-
248
- logger.success("Data restoration completed")
249
-
250
-
251
- def run_daemon():
252
- logger.info("Starting data sync daemon...")
253
- sync_interval = int(os.environ.get('SYNC_INTERVAL', '1800'))
254
- manager = DatasetManager()
255
-
256
- if not manager.validate():
257
- logger.error("Configuration invalid, exiting")
258
- sys.exit(1)
259
-
260
- logger.info(f"Sync interval strictly set to: {sync_interval} seconds")
261
-
262
- try:
263
- while True:
264
- time.sleep(sync_interval)
265
- # Only commits on the strict schedule, AND only if real user data changed!
266
- manager.upload_to_dataset(force=False)
267
- except KeyboardInterrupt:
268
- pass
269
-
270
-
271
- def main():
272
- parser = argparse.ArgumentParser(description='Hermes Agent Data Sync')
273
- parser.add_argument('action', choices=['backup', 'restore', 'daemon'], help='Action')
274
- parser.add_argument('--force', '-f', action='store_true', help='Force')
275
- args = parser.parse_args()
276
- manager = DatasetManager()
277
- if not manager.validate(): sys.exit(1)
278
- if args.action == 'backup': sys.exit(0 if manager.upload_to_dataset(force=args.force) else 1)
279
- elif args.action == 'restore': sys.exit(0 if manager.download_from_dataset() else 1)
280
- elif args.action == 'daemon': run_daemon()
281
-
282
- if __name__ == '__main__':
283
- main()