(messages: list, context: dict)
| 859 | # queue processor (useQueueProcessor.ts) when items arrive. |
| 860 | |
| 861 | def agent_loop(messages: list, context: dict): |
| 862 | system = get_system_prompt(context) |
| 863 | while True: |
| 864 | # Consume fired cron jobs → inject as messages |
| 865 | fired = consume_cron_queue() |
| 866 | for job in fired: |
| 867 | messages.append({"role": "user", |
| 868 | "content": f"[Scheduled] {job.prompt}"}) |
| 869 | print(f" \033[35m[inject cron] {job.prompt[:50]}\033[0m") |
| 870 | |
| 871 | try: |
| 872 | response = client.messages.create( |
| 873 | model=MODEL, system=system, messages=messages, |
| 874 | tools=TOOLS, max_tokens=8000) |
| 875 | except Exception as e: |
| 876 | messages.append({"role": "assistant", "content": [ |
| 877 | {"type": "text", |
| 878 | "text": f"[Error] {type(e).__name__}: {e}"}]}) |
| 879 | return |
| 880 | |
| 881 | messages.append({"role": "assistant", "content": response.content}) |
| 882 | if response.stop_reason != "tool_use": |
| 883 | return |
| 884 | |
| 885 | results = [] |
| 886 | for block in response.content: |
| 887 | if block.type != "tool_use": |
| 888 | continue |
| 889 | print(f"\033[36m> {block.name}\033[0m") |
| 890 | |
| 891 | if should_run_background(block.name, block.input): |
| 892 | bg_id = start_background_task(block) |
| 893 | results.append({"type": "tool_result", |
| 894 | "tool_use_id": block.id, |
| 895 | "content": f"[Background task {bg_id} started] " |
| 896 | f"Result will be available when complete."}) |
| 897 | else: |
| 898 | output = execute_tool(block) |
| 899 | print(str(output)[:300]) |
| 900 | results.append({"type": "tool_result", |
| 901 | "tool_use_id": block.id, |
| 902 | "content": output}) |
| 903 | |
| 904 | # Merge background tool results + notifications into one user message |
| 905 | user_content = list(results) |
| 906 | bg_notifications = collect_background_results() |
| 907 | if bg_notifications: |
| 908 | for notif in bg_notifications: |
| 909 | user_content.append({"type": "text", "text": notif}) |
| 910 | messages.append({"role": "user", "content": user_content}) |
| 911 | context = update_context(context, messages) |
| 912 | system = get_system_prompt(context) |
| 913 | |
| 914 | |
| 915 | if __name__ == "__main__": |
no test coverage detected