Initial commit: Ajarbot with optimizations
Features: - Multi-platform bot (Slack, Telegram) - Memory system with SQLite FTS - Tool use capabilities (file ops, commands) - Scheduled tasks system - Dynamic model switching (/sonnet, /haiku) - Prompt caching for cost optimization Optimizations: - Default to Haiku 4.5 (12x cheaper) - Reduced context: 3 messages, 2 memory results - Optimized SOUL.md (48% smaller) - Automatic caching when using Sonnet (90% savings) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
109
example_bot_with_scheduler.py
Normal file
109
example_bot_with_scheduler.py
Normal file
@@ -0,0 +1,109 @@
|
||||
"""
|
||||
Example: Bot with scheduled tasks.
|
||||
|
||||
Demonstrates how to use the TaskScheduler for cron-like scheduled tasks
|
||||
that require Agent/LLM execution and can send outputs to messaging platforms.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from adapters.base import AdapterConfig
|
||||
from adapters.runtime import AdapterRuntime
|
||||
from adapters.slack.adapter import SlackAdapter
|
||||
from adapters.telegram.adapter import TelegramAdapter
|
||||
from agent import Agent
|
||||
from scheduled_tasks import ScheduledTask, TaskScheduler
|
||||
|
||||
|
||||
def _on_task_complete(task: ScheduledTask, response: str) -> None:
|
||||
"""Callback for task completion."""
|
||||
print(f"\n[Task Complete] {task.name}")
|
||||
print(f"Response preview: {response[:100]}...\n")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
print("=" * 60)
|
||||
print("Ajarbot with Scheduled Tasks")
|
||||
print("=" * 60)
|
||||
|
||||
# 1. Create agent
|
||||
agent = Agent(
|
||||
provider="claude",
|
||||
workspace_dir="./memory_workspace",
|
||||
enable_heartbeat=False,
|
||||
)
|
||||
|
||||
# 2. Create runtime
|
||||
runtime = AdapterRuntime(agent)
|
||||
|
||||
# 3. Add adapters
|
||||
slack_adapter = SlackAdapter(AdapterConfig(
|
||||
platform="slack",
|
||||
enabled=True,
|
||||
credentials={
|
||||
"bot_token": "xoxb-YOUR-TOKEN",
|
||||
"app_token": "xapp-YOUR-TOKEN",
|
||||
},
|
||||
))
|
||||
runtime.add_adapter(slack_adapter)
|
||||
|
||||
telegram_adapter = TelegramAdapter(AdapterConfig(
|
||||
platform="telegram",
|
||||
enabled=True,
|
||||
credentials={"bot_token": "YOUR-TELEGRAM-TOKEN"},
|
||||
))
|
||||
runtime.add_adapter(telegram_adapter)
|
||||
|
||||
# 4. Create and configure scheduler
|
||||
scheduler = TaskScheduler(
|
||||
agent, config_file="config/scheduled_tasks.yaml",
|
||||
)
|
||||
|
||||
scheduler.add_adapter("slack", slack_adapter)
|
||||
scheduler.add_adapter("telegram", telegram_adapter)
|
||||
|
||||
scheduler.on_task_complete = _on_task_complete
|
||||
|
||||
print("\n[Setup] Scheduled tasks:")
|
||||
for task_info in scheduler.list_tasks():
|
||||
enabled = task_info.get("enabled")
|
||||
status = "enabled" if enabled else "disabled"
|
||||
next_run = task_info.get("next_run", "N/A")
|
||||
send_to = task_info.get("send_to") or "local only"
|
||||
print(f" [{status}] {task_info['name']}")
|
||||
print(f" Schedule: {task_info['schedule']}")
|
||||
print(f" Next run: {next_run}")
|
||||
print(f" Output: {send_to}")
|
||||
|
||||
# 5. Start everything
|
||||
print("\n" + "=" * 60)
|
||||
print("Starting bot with scheduler...")
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
await runtime.start()
|
||||
scheduler.start()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Bot is running! Press Ctrl+C to stop.")
|
||||
print("=" * 60)
|
||||
print(
|
||||
"\nScheduled tasks will run automatically "
|
||||
"at their scheduled times."
|
||||
)
|
||||
print(
|
||||
"Users can also chat with the bot normally "
|
||||
"from Slack/Telegram.\n"
|
||||
)
|
||||
|
||||
try:
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
print("\n\n[Shutdown] Stopping...")
|
||||
finally:
|
||||
scheduler.stop()
|
||||
await runtime.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user