#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
agent_core — a self-contained minimal autonomous agent.
This module implements the core of an autonomous agent: an LLM client
(OpenAI-compatible, streaming, with function-calling), a ~100-line agent
loop, and 9 atomic tools for system-level control (code execution, file
I/O, web fetching, user interaction, and memory management).
The architecture and design philosophy are inspired by and reference the
GenericAgent project (https://github.com/lsdefine/GenericAgent), which is
licensed under the MIT License. See the LICENSE file and README for full
attribution.
Dependencies: only the Python standard library + `requests` (already a
dependency of most agent setups). No LangChain, no Playwright, no browser
binaries.
Usage:
from agent_core import Agent
agent = Agent()
import threading; threading.Thread(target=agent.run, daemon=True).start()
dq = agent.put_task("Hello, what can you do?")
while True:
item = dq.get()
if "done" in item: print(item["done"]); break
if "next" in item: print(item["next"], end="")
"""
import json
import os
import queue
import re
import subprocess
import sys
import tempfile
import threading
import time
import traceback
import socket as _socket
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
from typing import Any, Optional
# Bypass VPN/proxy fake-IP DNS hijack for upstream API hosts
# Maps domain -> real IP so connections go directly, avoiding intermittent SSL errors
_UPSTREAM_HOST_OVERRIDES = {
"l0veyou.com": "152.53.152.103",
}
_orig_getaddrinfo = _socket.getaddrinfo
def _patched_getaddrinfo(host, port, *args, **kwargs):
if host in _UPSTREAM_HOST_OVERRIDES:
real_ip = _UPSTREAM_HOST_OVERRIDES[host]
return [(_socket.AF_INET, _socket.SOCK_STREAM, 6, '', (real_ip, port))]
return _orig_getaddrinfo(host, port, *args, **kwargs)
_socket.getaddrinfo = _patched_getaddrinfo
try:
import requests
except ImportError:
requests = None # web tools will degrade gracefully
# ---------------------------------------------------------------------------
# License attribution
# ---------------------------------------------------------------------------
# Portions of this agent core (the agent loop structure, the 9-tool design,
# the memory layering concept, and the system prompt philosophy) are derived
# from GenericAgent by lsdefine, licensed under the MIT License:
#
# Copyright (c) 2025 lsdefine
# https://github.com/lsdefine/GenericAgent
#
# See LICENSE for the full MIT text.
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = SCRIPT_DIR
TEMP_DIR = os.path.join(SCRIPT_DIR, "temp")
MEMORY_DIR = os.path.join(SCRIPT_DIR, "memory")
os.makedirs(TEMP_DIR, exist_ok=True)
os.makedirs(MEMORY_DIR, exist_ok=True)
# Ensure memory files exist
_MEM_FILES = {
"global_mem.txt": "# [Global Memory - L2]\n",
"global_mem_insight.txt": "# [Global Memory Insight - L1]\nRead global_mem.txt for L2 facts.\nL2: currently empty\nL3: (none yet)\n",
}
for _f, _default in _MEM_FILES.items():
_p = os.path.join(MEMORY_DIR, _f)
if not os.path.exists(_p):
with open(_p, "w", encoding="utf-8") as fh:
fh.write(_default)
FILE_HINT = "If you need to show files to user, use [FILE:filepath] in your response."
SYSTEM_PROMPT = """# Role: Physical-Level Omnipotent Executor
You have full physical access: file I/O, script execution, web fetching, and system-level intervention. Never deflect with "can't do it" — don't speculate, use tools to probe.
Summarize and reply in the user's language or follow the user's prompt.
## Action Principles
Before each tool call, reason about: current phase, whether the last result met expectations, and next strategy. Put your internal reasoning inside ... tags so it is visually separated from your answer. After the tag, write the actual reply or tool call. Include a in the reply text of each turn.
- Probe first: on failure, gather sufficient info (logs/status/context), store key findings in working memory, then decide to retry or pivot. Ask the user before irreversible operations.
- Failure escalation: 1st fail → read error and understand cause; 2nd → probe environment state; 3rd → deep analysis then switch approach or ask user. Never repeat an action without new information.
## Memory
- L0: Meta rules (this prompt)
- L1: ../memory/global_mem_insight.txt (minimal index)
- L2: ../memory/global_mem.txt (stable facts)
- L3: ../memory/*.md (task SOPs)
- Use update_working_checkpoint for short-term notes during long tasks.
- Use start_long_term_update when a task completes and has lessons worth saving.
## Constitution
1. Execute step by step, control granularity, limit blast radius; request intervention after 3 failures.
2. Check memory before decisions; always use existing SOPs; revisit on repeated failures.
3. Key/secret files: reference only, never read or move.
4. Files under memory/ should be patched (not overwritten) unless creating new ones.
## XML Tool Calling Fallback
If native function calling is unavailable (no tool_calls in API response), use XML format to call tools:
{"name": "tool_name", "arguments": {"key": "value"}}
You can make multiple tool calls in one response. After each tool_use block, the system will execute it and return results.
Always prefer native function calling when available. Only use XML format when the API doesn't return tool_calls.
"""
# ---------------------------------------------------------------------------
# Tool schema (OpenAI function-calling format)
# ---------------------------------------------------------------------------
TOOLS_SCHEMA = [
{"type": "function", "function": {
"name": "code_run",
"description": "Code executor. Prefer python. Runs Python scripts or bash/shell commands.",
"parameters": {"type": "object", "properties": {
"script": {"type": "string", "description": "The code to run. For python, this is a multi-line script. For shell, a single command."},
"type": {"type": "string", "enum": ["python", "bash", "shell"], "description": "Code type", "default": "python"},
"timeout": {"type": "integer", "description": "Timeout in seconds", "default": 60},
"cwd": {"type": "string", "description": "Working directory"},
}, "required": ["script"]},
}},
{"type": "function", "function": {
"name": "file_read",
"description": "Read file content. Read before modify for latest context and line numbers.",
"parameters": {"type": "object", "properties": {
"path": {"type": "string", "description": "Relative or absolute file path"},
"start": {"type": "integer", "description": "Start line (1-based)", "default": 1},
"count": {"type": "integer", "description": "Number of lines to read", "default": 200},
"show_linenos": {"type": "boolean", "description": "Show line numbers", "default": True},
}, "required": ["path"]},
}},
{"type": "function", "function": {
"name": "file_write",
"description": "Create/overwrite/append files. Content goes in the 'content' parameter.",
"parameters": {"type": "object", "properties": {
"path": {"type": "string", "description": "File path"},
"content": {"type": "string", "description": "Content to write"},
"mode": {"type": "string", "enum": ["overwrite", "append", "prepend"], "description": "Write mode", "default": "overwrite"},
}, "required": ["path", "content"]},
}},
{"type": "function", "function": {
"name": "file_patch",
"description": "Replace a unique old_content block with new_content. Exact match required. On failure, file_read to recheck.",
"parameters": {"type": "object", "properties": {
"path": {"type": "string", "description": "File path"},
"old_content": {"type": "string", "description": "Original text block to replace (must be unique in the file)"},
"new_content": {"type": "string", "description": "New content"},
}, "required": ["path", "old_content", "new_content"]},
}},
{"type": "function", "function": {
"name": "web_scan",
"description": "Fetch a URL and return simplified text content. Uses HTTP requests (no JavaScript rendering).",
"parameters": {"type": "object", "properties": {
"url": {"type": "string", "description": "URL to fetch"},
"text_only": {"type": "boolean", "description": "Return plain text only (strip HTML)", "default": True},
}, "required": ["url"]},
}},
{"type": "function", "function": {
"name": "web_execute_js",
"description": "Execute JavaScript in a browser. Requires TMWebDriver extension to be set up. Returns setup instructions if not available.",
"parameters": {"type": "object", "properties": {
"script": {"type": "string", "description": "JavaScript code to execute"},
"switch_tab_id": {"type": "string", "description": "Optional tab ID"},
}, "required": ["script"]},
}},
{"type": "function", "function": {
"name": "ask_user",
"description": "Interrupt task to ask the user for decisions, extra info, or to resolve blockers.",
"parameters": {"type": "object", "properties": {
"question": {"type": "string", "description": "Question for the user"},
"candidates": {"type": "array", "items": {"type": "string"}, "description": "Optional quick-select choices"},
}, "required": ["question"]},
}},
{"type": "function", "function": {
"name": "update_working_checkpoint",
"description": "Short-term working notepad. Auto-injected each turn to prevent info loss in long tasks. Call during early/mid stages.",
"parameters": {"type": "object", "properties": {
"key_info": {"type": "string", "description": "Key info to remember (<200 tokens): pitfalls, requirements, findings, file paths, progress, next steps."},
"related_sop": {"type": "string", "description": "Related SOP names for further reading"},
}, "required": ["key_info"]},
}},
{"type": "function", "function": {
"name": "start_long_term_update",
"description": "Start distilling long-term memory. Call when discovering info worth remembering (env facts, user prefs, lessons learned).",
"parameters": {"type": "object", "properties": {}},
}},
{"type": "function", "function": {
"name": "generate_image",
"description": "Generate an image from a text prompt via the l0veyou image API (gpt-image-2). Saves the image locally and returns a [FILE:] reference. Use when the user asks to create/generate/draw/design an image.",
"parameters": {"type": "object", "properties": {
"prompt": {"type": "string", "description": "Detailed image prompt. English works best. Describe subject, style, lighting, composition, colors."},
"size": {"type": "string", "enum": ["1024x1024", "1024x1536", "1536x1024"], "description": "Image dimensions", "default": "1024x1024"},
"model": {"type": "string", "description": "Image model", "default": "gpt-image-2"},
}, "required": ["prompt"]},
}},
{"type": "function", "function": {
"name": "view_image",
"description": "View and analyze a generated image. Reads the image file and uses a vision-capable model to describe what's in it. Use this to review/evaluate images you have generated, check quality, verify content matches the prompt, and provide feedback.",
"parameters": {"type": "object", "properties": {
"path": {"type": "string", "description": "Path to the image file to analyze (use the path returned by generate_image)"},
"question": {"type": "string", "description": "Optional specific question about the image (e.g. 'Does this match the comic panel description?')", "default": "Describe this image in detail: subject, style, colors, composition, and any notable details."},
}, "required": ["path"]},
}},
]
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def smart_format(data, max_str_len=200, omit_str="\n\n[omitted long content]\n\n"):
if not isinstance(data, str):
data = str(data)
if len(data) < max_str_len + len(omit_str) * 2:
return data
return f"{data[:max_str_len // 2]}{omit_str}{data[-max_str_len // 2:]}"
def format_error(e):
exc_type, exc_value, exc_tb = sys.exc_info()
tb = traceback.extract_tb(exc_tb)
if tb:
f = tb[-1]
return f"{exc_type.__name__}: {exc_value} @ {os.path.basename(f.filename)}:{f.lineno}"
return f"{exc_type.__name__}: {exc_value}"
def sanitize_error(err_text):
"""Clean up error messages for display.
Returns a generic error message that doesn't expose internal infrastructure."""
if not err_text:
return "Service temporarily unavailable"
s = str(err_text)
# Hide HTTP status codes that reveal rate limiting / auth issues
# 429 -> generic, 401/403 -> generic
if "429" in s:
return "Model is busy, please try again later"
if "401" in s or "403" in s:
return "Model authentication failed, please check configuration"
# Clean URLs
s = re.sub(r"https?://[^\s<>]+", "[api-endpoint]", s)
# Clean service names
s = re.sub(r"(?i)friendli[\w-]*", "[upstream]", s)
s = re.sub(r"(?i)l0veyou[\w-]*", "[upstream]", s)
s = re.sub(r"(?i)chatgpt2api[\w-]*", "[upstream]", s)
# Clean API keys
s = re.sub(r"(sk-[a-zA-Z0-9]{6})[a-zA-Z0-9]*", r"\1***", s)
s = re.sub(r"(sess-[a-zA-Z0-9]{6})[a-zA-Z0-9]*", r"\1***", s)
s = re.sub(r"Bearer\s+[a-zA-Z0-9_-]+", "Bearer ***", s)
# Clean IP addresses
s = re.sub(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", "[ip]", s)
# Clean file paths
s = re.sub(r"/root/[^\s]+", "[server-path]", s)
s = re.sub(r"/home/[^\s]+", "[server-path]", s)
# Remove traceback details
s = re.sub(r"Traceback[\s\S]*?(?=\n[A-Za-z]|$)", "[error]", s)
# Limit length
if len(s) > 200:
s = s[:200] + "..."
return s
def clean_reply(text):
"""Strip internal tags for display."""
for pat in [r"[\s\S]*?", r"[\s\S]*?",
r"[\s\S]*?", r"[\s\S]*?"]:
text = re.sub(pat, "", text or "", flags=re.DOTALL)
return re.sub(r"\n{3,}", "\n\n", text).strip() or "..."
def extract_files(text):
return re.findall(r"\[FILE:([^\]]+)\]", text or "")
def strip_files(text):
return re.sub(r"\[FILE:[^\]]+\]", "", text or "").strip()
# ---------------------------------------------------------------------------
# Tool implementations
# ---------------------------------------------------------------------------
def tool_code_run(script, code_type="python", timeout=60, cwd=None, stop_signal=None):
"""Execute Python or shell code. Yields progress, returns result dict."""
cwd = cwd or TEMP_DIR
os.makedirs(cwd, exist_ok=True)
preview = (script[:80].replace("\n", " ") + "...") if len(script) > 80 else script.strip()
yield f"[Action] Running {code_type}: {preview}\n"
if code_type in ("python", "py"):
tmp = tempfile.NamedTemporaryFile(suffix=".ai.py", delete=False, mode="w", encoding="utf-8", dir=cwd)
tmp.write(script)
tmp.close()
cmd = [sys.executable, "-X", "utf8", "-u", tmp.name]
elif code_type in ("bash", "shell", "sh", "powershell", "ps"):
if sys.platform == "win32":
# Use PowerShell on Windows
cmd = ["powershell", "-NoProfile", "-NonInteractive", "-Command", script]
else:
cmd = ["bash", "-c", script]
else:
return {"status": "error", "msg": f"Unsupported type: {code_type}"}
try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
bufsize=0, cwd=cwd, text=True, encoding="utf-8", errors="replace")
stdout_lines = []
start_t = time.time()
for line in iter(proc.stdout.readline, ""):
stdout_lines.append(line)
if time.time() - start_t > timeout or (stop_signal and stop_signal):
proc.kill()
stdout_lines.append("\n[Stopped] Timeout or user abort\n")
break
proc.wait(timeout=5)
exit_code = proc.returncode
stdout_str = "".join(stdout_lines)
status = "success" if exit_code == 0 else "error"
icon = "✅" if exit_code == 0 else "❌"
yield f"[Status] {icon} Exit Code: {exit_code}\n[Stdout]\n{smart_format(stdout_str, max_str_len=8000)}\n"
return {"status": status, "stdout": smart_format(stdout_str, max_str_len=10000), "exit_code": exit_code}
except Exception as e:
return {"status": "error", "msg": str(e)}
finally:
if code_type == "python" and "tmp" in dir() and os.path.exists(tmp.name):
os.remove(tmp.name)
def tool_file_read(path, start=1, count=200, show_linenos=True):
"""Read file content with optional line numbers."""
try:
with open(path, "r", encoding="utf-8", errors="replace") as f:
lines = f.readlines()
end = min(start - 1 + count, len(lines))
result_lines = lines[start - 1:end]
if show_linenos:
result = f"[FILE] {len(lines)} lines | showing {start}-{end}\n"
result += "\n".join(f"{start + i}|{line.rstrip()}" for i, line in enumerate(result_lines))
else:
result = "".join(result_lines)
remaining = len(lines) - end
if remaining > 0:
result += f"\n\n[{remaining} more lines below]"
return smart_format(result, max_str_len=15000)
except FileNotFoundError:
return f"Error: File not found: {path}"
except Exception as e:
return f"Error: {e}"
def tool_file_write(path, content, mode="overwrite"):
"""Write content to file."""
try:
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
if mode == "prepend":
old = open(path, "r", encoding="utf-8").read() if os.path.exists(path) else ""
with open(path, "w", encoding="utf-8") as f:
f.write(content + old)
elif mode == "append":
with open(path, "a", encoding="utf-8") as f:
f.write(content)
else:
with open(path, "w", encoding="utf-8") as f:
f.write(content)
return {"status": "success", "writed_bytes": len(content)}
except Exception as e:
return {"status": "error", "msg": str(e)}
def tool_file_patch(path, old_content, new_content):
"""Replace unique old_content with new_content in file."""
try:
if not os.path.exists(path):
return {"status": "error", "msg": "File not found"}
with open(path, "r", encoding="utf-8") as f:
full = f.read()
if not old_content:
return {"status": "error", "msg": "old_content is empty"}
count = full.count(old_content)
if count == 0:
return {"status": "error", "msg": "old_content not found. Use file_read to check current content."}
if count > 1:
return {"status": "error", "msg": f"Found {count} matches. Provide a longer, more specific old_content."}
updated = full.replace(old_content, new_content)
with open(path, "w", encoding="utf-8") as f:
f.write(updated)
return {"status": "success", "msg": "File patched successfully"}
except Exception as e:
return {"status": "error", "msg": str(e)}
def tool_web_scan(url, text_only=True):
"""Fetch a URL and return text content."""
if requests is None:
return {"status": "error", "msg": "requests library not installed. Run: pip install requests"}
# SECURITY: SSRF protection - block internal/private IPs
try:
from urllib.parse import urlparse as _up
parsed = _up(url)
hostname = parsed.hostname or ""
# Block private/internal IPs and metadata endpoints
import ipaddress as _ipa
try:
ip = _ipa.ip_address(hostname)
if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved:
return {"status": "error", "msg": f"Blocked: internal IP {hostname}"}
except ValueError:
pass # Not an IP, it's a hostname
# Block cloud metadata endpoints
if hostname in ("169.254.169.254", "metadata.google.internal", "metadata"):
return {"status": "error", "msg": "Blocked: cloud metadata endpoint"}
# Only allow http/https
if parsed.scheme not in ("http", "https"):
return {"status": "error", "msg": f"Blocked: scheme {parsed.scheme}"}
except Exception:
return {"status": "error", "msg": "Invalid URL"}
try:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
resp = requests.get(url, headers=headers, timeout=30, verify=True)
content_type = resp.headers.get("content-type", "")
if text_only and "html" in content_type:
# Simple HTML to text
import html.parser
class _Stripper(html.parser.HTMLParser):
def __init__(self):
super().__init__(); self.text = []
def handle_data(self, d): self.text.append(d)
def get_text(self): return " ".join(self.text)
s = _Stripper()
s.feed(resp.text)
text = re.sub(r"\s+", " ", s.get_text()).strip()
return {"status": "success", "url": str(resp.url), "content": smart_format(text, max_str_len=30000)}
return {"status": "success", "url": str(resp.url), "content": smart_format(resp.text, max_str_len=30000)}
except Exception as e:
return {"status": "error", "msg": format_error(e)}
def tool_web_execute_js(script, switch_tab_id=None):
"""Execute JS in browser. Requires TMWebDriver (not bundled)."""
return {"status": "error", "msg": "Browser JS execution requires TMWebDriver. To set up: install the Chrome extension from the tmwd_cdp_bridge/ directory, or use web_scan for basic HTTP fetching."}
def tool_ask_user(question, candidates=None):
"""Return an interrupt signal for human intervention."""
return {"status": "INTERRUPT", "intent": "HUMAN_INTERVENTION",
"data": {"question": question, "candidates": candidates or []}}
def tool_generate_image(prompt, size="1024x1024", model="gpt-image-2", api_base="", api_key="", out_dir=None):
"""Generate an image via the l0veyou async creation-tasks API.
Creates a task, polls until done, downloads the image, saves locally."""
if requests is None:
return {"status": "error", "msg": "requests library not installed. Run: pip install requests"}
if not api_base or not api_key:
return {"status": "error", "msg": "No LLM configured. Please log in via the web UI first."}
try:
import base64 as _b64, uuid as _uuid
import warnings as _w
_w.filterwarnings("ignore")
except Exception:
pass
out_dir = out_dir or os.path.join(SCRIPT_DIR, "output")
os.makedirs(out_dir, exist_ok=True)
base = api_base.rstrip("/")
if "/v1" in base:
base = base.split("/v1")[0]
task_id = "img_" + str(int(time.time() * 1000))
headers = {"Authorization": "Bearer " + api_key, "Content-Type": "application/json"}
payload = {"client_task_id": task_id, "prompt": prompt, "model": model, "size": size, "mode": "generate"}
try:
r = requests.post(base + "/api/creation-tasks/image-generations",
json=payload, headers=headers, timeout=30, verify=True)
if r.status_code not in (200, 201):
return {"status": "error", "msg": sanitize_error("HTTP " + str(r.status_code) + ": " + r.text[:200])}
except Exception as e:
return {"status": "error", "msg": format_error(e)}
saved = []
for _ in range(36):
time.sleep(5)
try:
r = requests.get(base + "/api/creation-tasks", params={"id": task_id},
headers=headers, timeout=15, verify=True)
items = r.json().get("items", [])
match = next((it for it in items if it.get("id") == task_id), None)
if not match:
continue
status = match.get("status", "")
if status in ("success", "succeeded", "completed"):
data = match.get("data", [])
for di, d in enumerate(data):
url = d.get("url")
if not url:
continue
try:
ir = requests.get(url, headers={"Authorization": "Bearer " + api_key},
timeout=60, verify=True)
if ir.status_code != 200 or not ir.content:
ir = requests.get(url, timeout=60, verify=True)
if ir.status_code == 200 and ir.content:
fpath = os.path.join(out_dir, task_id + "_" + str(di) + ".png")
with open(fpath, "wb") as fh:
fh.write(ir.content)
saved.append(fpath)
except Exception:
pass
break
if status in ("failed", "error"):
return {"status": "error", "msg": sanitize_error("task failed: " + str(match.get("error", ""))[:200])}
except Exception:
pass
if not saved:
return {"status": "error", "msg": "image generation timed out or returned no image"}
return {"status": "success", "images": saved,
"files": ["[FILE:" + p + "]" for p in saved]}
def tool_view_image(image_path, question="Describe this image in detail", api_base="", api_key=""):
"""Analyze an image using a vision-capable model via the l0veyou API.
Reads the image, converts to base64, and sends to a vision model for description."""
if requests is None:
return {"status": "error", "msg": "requests library not installed"}
if not os.path.exists(image_path):
return {"status": "error", "msg": f"Image not found: {image_path}"}
if not api_base or not api_key:
return {"status": "error", "msg": "No LLM configured for vision analysis"}
try:
import base64 as _b64
import warnings as _w
_w.filterwarnings("ignore")
except Exception:
pass
# Read and optionally resize the image to keep payload manageable
file_size = os.path.getsize(image_path)
try:
with open(image_path, "rb") as f:
img_data = f.read()
# If image is larger than 1MB, try to resize using PIL
if len(img_data) > 1024 * 1024:
try:
from PIL import Image
import io
img = Image.open(image_path)
# Resize to max 512x512 for analysis
img.thumbnail((512, 512), Image.LANCZOS)
buf = io.BytesIO()
img.save(buf, format="PNG")
img_data = buf.getvalue()
except ImportError:
# PIL not available, use as-is but truncate if too large
if len(img_data) > 2 * 1024 * 1024:
img_data = img_data[:2 * 1024 * 1024]
img_b64 = _b64.b64encode(img_data).decode()
except Exception as e:
return {"status": "error", "msg": f"Failed to read image: {e}"}
base = api_base.rstrip("/")
if "/v1" in base:
base = base.split("/v1")[0]
data_url = f"data:image/png;base64,{img_b64}"
headers = {"Authorization": "Bearer " + api_key, "Content-Type": "application/json"}
# Try vision-capable models (fast timeout, fail quickly if accounts expired)
vision_models = ["Free/gpt-4o", "Free/gpt-4o-mini", "Free/gemini-3.5-flash"]
for vmodel in vision_models:
payload = {
"model": vmodel,
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": question},
{"type": "image_url", "image_url": {"url": data_url}}
]
}],
"stream": True,
"max_tokens": 500,
}
try:
r = requests.post(base + "/v1/chat/completions", json=payload,
headers=headers, timeout=12, verify=True, stream=True)
content_parts = []
has_error = False
line_count = 0
for line in r.iter_lines():
if not line:
continue
line = line.decode("utf-8", errors="replace") if isinstance(line, bytes) else line
if not line.startswith("data:"):
continue
data_str = line[5:].strip()
if data_str == "[DONE]":
break
line_count += 1
# Early exit: if we've seen 3 lines and no content, skip this model
if line_count > 5 and not content_parts:
break
try:
import json as _json
evt = _json.loads(data_str)
except Exception:
continue
if "error" in evt and "choices" not in evt:
has_error = True
break
choices = evt.get("choices") or [{}]
delta = choices[0].get("delta") or {}
if delta.get("content"):
content_parts.append(delta["content"])
content = "".join(content_parts).strip()
# Check if the response is a real description (not "I can't see images")
if content and len(content) > 10 and "cannot see" not in content.lower() and "can't see" not in content.lower() and "text-based" not in content.lower():
return {"status": "success", "model_used": vmodel, "description": content,
"file_path": image_path, "file_size": file_size}
except Exception:
continue
# Fallback: no vision model worked
return {
"status": "partial",
"msg": "Vision analysis unavailable (no vision model responded). The image file exists and is valid.",
"file_path": image_path,
"file_size": file_size,
"file_size_human": f"{file_size / 1024:.0f} KB",
"suggestion": "Evaluate the image based on the prompt used to generate it. The image was successfully generated and saved."
}
# ---------------------------------------------------------------------------
# Step outcome + handler
# ---------------------------------------------------------------------------
@dataclass
class StepOutcome:
data: Any
next_prompt: Optional[str] = None
should_exit: bool = False
class AgentHandler:
"""Dispatches tool calls and manages working memory + history."""
def __init__(self, parent, last_history=None, cwd=TEMP_DIR):
self.parent = parent
self.working = {}
self.cwd = cwd
self.current_turn = 0
self.history_info = last_history if last_history else []
self.code_stop_signal = []
self.max_turns = 180
def _get_abs_path(self, path):
if not path:
return ""
# SECURITY: sandbox to ROOT_DIR/TEMP_DIR
_ALLOWED_ROOTS = [
os.path.realpath(ROOT_DIR),
os.path.realpath(TEMP_DIR),
os.path.realpath(os.path.join(ROOT_DIR, "output")),
]
if os.path.isabs(path):
real = os.path.realpath(path)
for root in _ALLOWED_ROOTS:
if real.startswith(root + os.sep) or real == root:
return real
# Outside sandbox - deny
return ""
resolved = os.path.realpath(os.path.join(self.cwd, path))
for root in _ALLOWED_ROOTS:
if resolved.startswith(root + os.sep) or resolved == root:
return resolved
return ""
def _get_anchor_prompt(self):
"""Build the working-memory prompt injected each turn."""
h = self.history_info
W = 30
h_str = "\n".join(h[-W:])
prompt = f"\n### [WORKING MEMORY]\n\n{h_str}\n"
prompt += f"\nCurrent turn: {self.current_turn}\n"
if self.working.get("key_info"):
prompt += f"\n{self.working.get('key_info')}"
return prompt
def dispatch(self, tool_name, args, response, index=0, tool_num=1):
"""Dispatch a tool call. Yields progress text, returns StepOutcome."""
method = getattr(self, f"do_{tool_name}", None)
if method is None:
yield f"Unknown tool: {tool_name}\n"
return StepOutcome(None, next_prompt=f"Unknown tool {tool_name}")
args["_index"] = index
args["_tool_num"] = tool_num
ret = method(args, response)
# Handle both generators and direct returns
if hasattr(ret, "__iter__") and not isinstance(ret, (str, dict, list)):
return (yield from ret)
return ret
def turn_end_callback(self, response, tool_calls, tool_results, turn, next_prompt, exit_reason):
"""Extract summary and add periodic reminders."""
content = getattr(response, "content", "") or ""
rsumm = re.search(r"(.*?)", content, re.DOTALL)
if rsumm:
summary = rsumm.group(1).strip()
else:
tc = tool_calls[0]
tool_name, targs = tc["tool_name"], tc["args"]
clean_args = {k: v for k, v in targs.items() if not k.startswith("_")}
summary = f"{tool_name}, args: {clean_args}"
if tool_name == "no_tool":
summary = "Responded directly"
summary = smart_format(summary.replace("\n", ""), max_str_len=80)
self.history_info.append(f"[Agent] {summary}")
if turn % 7 == 0:
next_prompt += f"\n\n[SYSTEM] Turn {turn}. Call update_working_checkpoint to save key context."
return next_prompt
# ---- tool implementations ----
def do_code_run(self, args, response):
code = args.get("script") or args.get("code")
if not code:
return StepOutcome("[Error] No code provided.", next_prompt="\n")
code_type = args.get("type", "python")
timeout = int(args.get("timeout", 60))
cwd = self._get_abs_path(args.get("cwd", "./"))
result = yield from tool_code_run(code, code_type, timeout, cwd, self.code_stop_signal)
return StepOutcome(result, next_prompt=self._get_anchor_prompt())
def do_file_read(self, args, response):
path = self._get_abs_path(args.get("path", ""))
yield f"[Action] Reading: {path}\n"
result = tool_file_read(path, args.get("start", 1), args.get("count", 200), args.get("show_linenos", True))
return StepOutcome(smart_format(result, max_str_len=15000), next_prompt=self._get_anchor_prompt())
def do_file_write(self, args, response):
path = self._get_abs_path(args.get("path", ""))
content = args.get("content", "")
mode = args.get("mode", "overwrite")
yield f"[Action] Writing {mode}: {os.path.basename(path)}\n"
result = tool_file_write(path, content, mode)
yield f"[Status] {result}\n"
return StepOutcome(result, next_prompt=self._get_anchor_prompt())
def do_file_patch(self, args, response):
path = self._get_abs_path(args.get("path", ""))
yield f"[Action] Patching: {path}\n"
result = tool_file_patch(path, args.get("old_content", ""), args.get("new_content", ""))
yield f"{result}\n"
return StepOutcome(result, next_prompt=self._get_anchor_prompt())
def do_web_scan(self, args, response):
url = args.get("url", "")
yield f"[Action] Fetching: {url}\n"
result = tool_web_scan(url, args.get("text_only", True))
yield f"[Result] {smart_format(str(result), max_str_len=500)}\n"
return StepOutcome(smart_format(json.dumps(result, ensure_ascii=False), max_str_len=8000),
next_prompt=self._get_anchor_prompt())
def do_web_execute_js(self, args, response):
result = tool_web_execute_js(args.get("script", ""), args.get("switch_tab_id"))
yield f"[Result] {result}\n"
return StepOutcome(result, next_prompt=self._get_anchor_prompt())
def do_ask_user(self, args, response):
question = args.get("question", "Please provide input:")
candidates = args.get("candidates", [])
result = tool_ask_user(question, candidates)
yield f"Waiting for your answer...\n"
return StepOutcome(result, next_prompt="", should_exit=True)
def do_generate_image(self, args, response):
prompt = args.get("prompt", "")
if not prompt:
return StepOutcome("[Error] No prompt provided.", next_prompt="\n")
size = args.get("size", "1024x1024")
model = args.get("model", "gpt-image-2")
# Use l0veyou backend for image generation (g0i.ai doesn't support it)
# Admin session required for image generation API
api_base = "https://l0veyou.com"
# SECURITY: load admin token from env var, not hardcoded
api_key = os.environ.get("L0VEYOU_ADMIN_TOKEN", "")
if not api_key:
# Fallback: search in llm configs
for cfg in getattr(self.parent, "_llm_configs", []):
if "l0veyou" in cfg.get("apibase", ""):
api_key = cfg["apikey"]
break
yield f"[Action] Generating image: {str(prompt)[:60]}...\n"
result = tool_generate_image(prompt, size, model, api_base, api_key)
if isinstance(result, dict) and result.get("status") == "success":
yield f"[Status] Generated {len(result.get('images', []))} image(s)\n"
else:
yield f"[Status] Image generation failed: {smart_format(str(result), max_str_len=300)}\n"
return StepOutcome(smart_format(json.dumps(result, ensure_ascii=False), max_str_len=4000),
next_prompt=self._get_anchor_prompt())
def do_view_image(self, args, response):
image_path = args.get("path", "")
if not image_path:
return StepOutcome("[Error] No image path provided.", next_prompt="\n")
question = args.get("question", "Describe this image in detail: subject, style, colors, composition, and any notable details.")
lc = getattr(self.parent, "llmclient", None)
api_base = lc.api_base if lc else ""
api_key = lc.api_key if lc else ""
yield f"[Action] Viewing image: {os.path.basename(image_path)}...\n"
result = tool_view_image(image_path, question, api_base, api_key)
if isinstance(result, dict):
status = result.get("status", "unknown")
if status == "success":
desc = result.get("description", "")
model = result.get("model_used", "?")
yield f"[Status] Vision analysis ({model}): {str(desc)[:200]}...\n"
elif status == "partial":
yield f"[Status] Vision unavailable - {result.get('msg', '')}\n"
else:
yield f"[Status] View failed: {smart_format(str(result), max_str_len=300)}\n"
return StepOutcome(smart_format(json.dumps(result, ensure_ascii=False), max_str_len=6000),
next_prompt=self._get_anchor_prompt())
def do_update_working_checkpoint(self, args, response):
key_info = args.get("key_info", "")
related_sop = args.get("related_sop", "")
if key_info:
self.working["key_info"] = key_info
if related_sop:
self.working["related_sop"] = related_sop
yield f"[Info] Updated working memory.\n"
return StepOutcome({"result": "working key_info updated"}, next_prompt=self._get_anchor_prompt())
def do_start_long_term_update(self, args, response):
prompt = """### [Distill Experience]
Extract verified, long-term-useful info from the recent task:
- **Environment facts** (paths, configs) → file_patch into memory/global_mem.txt
- **Task experience** (pitfalls, key steps) → write a new SOP .md in memory/
Only extract verified, reusable info. Skip ephemeral data, unverified guesses, or common knowledge.
Use file_read to check existing memory first, then file_patch for minimal updates.
"""
yield "[Info] Starting long-term memory distillation.\n"
insight = ""
insight_path = os.path.join(MEMORY_DIR, "global_mem_insight.txt")
if os.path.exists(insight_path):
with open(insight_path, "r", encoding="utf-8") as f:
insight = f.read()
return StepOutcome(f"Current L1 insight:\n{insight}", next_prompt=prompt)
def do_no_tool(self, args, response):
"""Called when the LLM doesn't invoke any tool — signals task completion."""
content = getattr(response, "content", "") or ""
if not content.strip():
yield "[Warn] Empty response. Retrying...\n"
return StepOutcome({}, next_prompt="[System] Blank response, please respond with content or a tool call.")
return StepOutcome(response, next_prompt=None)
# ---------------------------------------------------------------------------
# LLM Client (OpenAI-compatible, streaming, with function calling)
# ---------------------------------------------------------------------------
class LLMResponse:
"""Parsed LLM response."""
def __init__(self, content="", tool_calls=None, raw=None):
self.content = content
self.tool_calls = tool_calls or []
self.raw = raw or ""
class MockToolCall:
def __init__(self, name, args, id=""):
self.id = id
self.function = type("F", (), {"name": name, "arguments": json.dumps(args, ensure_ascii=False)})()
def _parse_xml_tool_calls(content):
"""Parse XML-formatted tool calls from LLM text output.
Supports formats:
{"name": "tool_name", "arguments": {"key": "value"}}
{"key": "value"}
```tool_use\n{"name": "...", "arguments": {...}}\n```
Returns list of MockToolCall objects. Empty list if none found.
"""
if not content:
return []
tool_calls = []
# Format 1: {...}
for match in re.finditer(r'\s*(\{.*?\})\s*', content, re.DOTALL):
try:
data = json.loads(match.group(1))
name = data.get("name", "")
args = data.get("arguments", data.get("args", {}))
if name:
tool_calls.append(MockToolCall(name, args, id=f"xml-{len(tool_calls)}"))
except (json.JSONDecodeError, AttributeError):
pass
# Format 2: {...}
if not tool_calls:
for match in re.finditer(r'\s*(\{.*?\}|\S*)\s*', content, re.DOTALL):
name = match.group(1)
args_str = match.group(2).strip()
try:
args = json.loads(args_str) if args_str else {}
except json.JSONDecodeError:
args = {"_raw": args_str}
tool_calls.append(MockToolCall(name, args, id=f"xml-{len(tool_calls)}"))
# Format 3: code block with tool_use header containing JSON
if not tool_calls:
for match in re.finditer(r'```(?:tool_use|tool_call)?\s*\n\s*(\{.*?\})\s*\n```', content, re.DOTALL):
try:
data = json.loads(match.group(1))
name = data.get("name", "")
args = data.get("arguments", data.get("args", {}))
if name:
tool_calls.append(MockToolCall(name, args, id=f"xml-{len(tool_calls)}"))
except (json.JSONDecodeError, AttributeError):
pass
return tool_calls
class LLMClient:
"""OpenAI-compatible streaming LLM client with function-calling support.
Works with OpenAI, DeepSeek, Kimi/Moonshot, local models (Ollama, vLLM),
and any endpoint that implements /v1/chat/completions with tools.
"""
def __init__(self, cfg):
self.api_key = cfg.get("apikey", "")
self.api_base = cfg.get("apibase", "https://api.openai.com").rstrip("/")
self.model = cfg.get("model", "gpt-4o")
self.name = cfg.get("name", self.model)
self.context_win = cfg.get("context_win", 30000)
self.temperature = cfg.get("temperature", 1)
self.max_tokens = cfg.get("max_tokens")
self.stream = cfg.get("stream", True)
self.connect_timeout = cfg.get("timeout", 10)
self.read_timeout = cfg.get("read_timeout", 240)
self.max_retries = cfg.get("max_retries", 3)
self.history = []
self.system = ""
self.tools = None
self._lock = threading.Lock()
self.log_path = None
def _make_url(self):
base = self.api_base.rstrip("/")
if "/v1" in base:
return f"{base}/chat/completions"
return f"{base}/v1/chat/completions"
def _build_messages(self, messages):
"""Convert internal message format to OpenAI format."""
msgs = []
if self.system:
msgs.append({"role": "system", "content": self.system})
for m in messages:
role = m.get("role", "user")
content = m.get("content", "")
tool_results = m.get("tool_results", [])
if isinstance(content, list):
# Extract text from content blocks
texts = []
for b in content:
if isinstance(b, dict) and b.get("type") == "text":
texts.append(b.get("text", ""))
elif isinstance(b, str):
texts.append(b)
content = "\n".join(texts)
if tool_results:
# Embed tool results as text in user message (avoids role:tool requiring preceding tool_calls)
tool_text = "\n\n".join([f"[Tool Result]: {tr.get('content', '')}" for tr in tool_results])
content = (str(content) + "\n\n" + tool_text) if content else tool_text
if content:
msgs.append({"role": role, "content": str(content)})
return msgs
def chat(self, messages, tools=None):
"""Stream LLM response. Yields text chunks, returns LLMResponse."""
if requests is None:
raise RuntimeError("requests library required. Install: pip install requests")
self.tools = tools
oai_messages = self._build_messages(messages)
url = self._make_url()
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
"Accept": "text/event-stream" if self.stream else "application/json",
"User-Agent": "CyberCode/1.0",
}
payload = {"model": self.model, "messages": oai_messages, "stream": self.stream}
if self.temperature != 1:
payload["temperature"] = self.temperature
if self.max_tokens:
payload["max_tokens"] = self.max_tokens
if tools:
payload["tools"] = tools
if self.stream:
payload["stream_options"] = {"include_usage": True}
# Retry logic - use fresh session per attempt to avoid stale SSL connections
retryable = {408, 409, 425, 429, 500, 502, 503, 504}
for attempt in range(self.max_retries + 1):
try:
# Fresh session each attempt to clear connection pool (fixes intermittent SSL errors)
session = requests.Session()
session.trust_env = False # Bypass proxy env vars
with session.post(url, headers=headers, json=payload, stream=self.stream,
timeout=(self.connect_timeout, self.read_timeout),
verify=True) as resp:
if resp.status_code >= 400:
body = resp.text[:500]
if resp.status_code in retryable and attempt < self.max_retries:
delay = min(30, 2.0 * (2 ** attempt))
print(f"[LLM Retry] HTTP {resp.status_code}, retry in {delay:.1f}s")
time.sleep(delay)
continue
# Clean error for display
err_text = "!!!Error: " + sanitize_error(f"HTTP {resp.status_code}: {body}")
yield err_text
return LLMResponse(content=err_text)
if self.stream:
return (yield from self._parse_stream(resp))
else:
return self._parse_json(resp.json())
except (requests.Timeout, requests.ConnectionError) as e:
if attempt < self.max_retries:
delay = min(30, 2.0 * (2 ** attempt))
print(f"[LLM Retry] {type(e).__name__}, retry in {delay:.1f}s")
time.sleep(delay)
continue
err = "!!!Error: " + sanitize_error(f"{type(e).__name__}: {e}")
yield err
return LLMResponse(content=err)
return LLMResponse(content="!!!Error: Max retries exceeded")
def _parse_stream(self, resp):
"""Parse OpenAI SSE stream. Yields text chunks, returns LLMResponse."""
content_text = ""
tc_buf = {} # index -> {id, name, args}
_in_think = False # track think block state for proper aggregation
for line in resp.iter_lines():
if not line:
continue
line = line.decode("utf-8", errors="replace") if isinstance(line, bytes) else line
if not line.startswith("data:"):
continue
data_str = line[5:].strip()
if data_str == "[DONE]":
break
try:
evt = json.loads(data_str)
except json.JSONDecodeError:
continue
# Detect error responses (some providers return errors in-stream)
if "error" in evt and "choices" not in evt:
err_msg = evt["error"].get("message", str(evt["error"])) if isinstance(evt.get("error"), dict) else str(evt["error"])
safe_msg = sanitize_error(err_msg)
yield f"\n[Error] {safe_msg}\n"
return LLMResponse(content=f"!!!Error: {safe_msg}")
choices = evt.get("choices") or [{}]
ch = choices[0]
delta = ch.get("delta") or {}
# Reasoning content (some providers) - wrap in tags for frontend separation
if rc := delta.get("reasoning_content") or delta.get("reasoning", ""):
if not _in_think:
yield f"{rc}"
_in_think = True
else:
yield rc
if delta.get("content"):
if _in_think:
yield ""
_in_think = False
text = delta["content"]
content_text += text
yield text
for tc in (delta.get("tool_calls") or []):
idx = tc.get("index", 0)
if idx not in tc_buf:
tc_buf[idx] = {"id": "", "name": "", "args": ""}
func = tc.get("function", {})
if func.get("name"):
tc_buf[idx]["name"] = func["name"]
if func.get("arguments"):
tc_buf[idx]["args"] += func["arguments"]
if tc.get("id") and not tc_buf[idx]["id"]:
tc_buf[idx]["id"] = tc["id"]
# Close any open think block at end of stream
if _in_think:
yield ""
_in_think = False
# Build tool calls
tool_calls = []
for idx in sorted(tc_buf):
tc = tc_buf[idx]
try:
args = json.loads(tc["args"]) if tc["args"] else {}
except json.JSONDecodeError:
args = {"_raw": tc["args"]}
tool_calls.append(MockToolCall(tc["name"], args, id=tc["id"]))
# Fallback: parse XML tool calls from content if no native tool_calls
if not tool_calls and content_text:
tool_calls = _parse_xml_tool_calls(content_text)
return LLMResponse(content=content_text, tool_calls=tool_calls)
def _parse_json(self, data):
"""Parse non-streaming JSON response."""
msg = (data.get("choices") or [{}])[0].get("message", {})
content = msg.get("content", "") or ""
tool_calls = []
for tc in (msg.get("tool_calls") or []):
fn = tc.get("function", {})
try:
args = json.loads(fn.get("arguments", "")) if fn.get("arguments") else {}
except json.JSONDecodeError:
args = {"_raw": fn.get("arguments", "")}
tool_calls.append(MockToolCall(fn.get("name", ""), args, id=tc.get("id", "")))
# Fallback: parse XML tool calls from content if no native tool_calls
if not tool_calls and content:
tool_calls = _parse_xml_tool_calls(content)
return LLMResponse(content=content, tool_calls=tool_calls)
# ---------------------------------------------------------------------------
# Agent Loop
# ---------------------------------------------------------------------------
def agent_loop(client, system_prompt, user_input, handler, tools_schema,
max_turns=180, verbose=True):
"""The core agent loop: LLM → tool dispatch → next prompt → repeat.
Yields text chunks for streaming. Returns when the task is done.
"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": [{"type": "text", "text": user_input}]},
]
handler.max_turns = max_turns
turn = 0
while turn < handler.max_turns:
turn += 1
yield f"\n\n**LLM Running (Turn {turn}) ...**\n\n"
# Call LLM
client.system = system_prompt
try:
response = yield from client.chat(messages, tools=tools_schema)
except Exception as e:
yield f"\n[Error] LLM call failed: {format_error(e)}\n"
break
if response is None:
response = LLMResponse(content="!!!Error: No response")
# Parse tool calls (native function calling or XML text fallback)
if response.tool_calls:
tool_calls = [{"tool_name": tc.function.name,
"args": json.loads(tc.function.arguments) if tc.function.arguments else {},
"id": tc.id} for tc in response.tool_calls]
elif response.content and _parse_xml_tool_calls(response.content):
# XML text-based tool calling fallback
xml_tcs = _parse_xml_tool_calls(response.content)
tool_calls = [{"tool_name": tc.function.name,
"args": json.loads(tc.function.arguments) if tc.function.arguments else {},
"id": tc.id} for tc in xml_tcs]
if verbose:
yield f"\n[XML Tool Call Fallback] Parsed {len(tool_calls)} tool call(s) from text\n"
else:
tool_calls = [{"tool_name": "no_tool", "args": {}}]
tool_results = []
next_prompts = set()
exit_reason = {}
for ii, tc in enumerate(tool_calls):
tool_name, args, tid = tc["tool_name"], tc["args"], tc.get("id", "")
if tool_name != "no_tool":
yield f"🛠️ Tool: `{tool_name}` 📥 args:\n````text\n{json.dumps(args, indent=2, ensure_ascii=False)}\n````\n"
handler.current_turn = turn
gen = handler.dispatch(tool_name, args, response, index=ii, tool_num=len(tool_calls))
try:
v = next(gen)
if verbose:
yield "`````\n"
outcome = (yield from gen) if verbose else (yield from gen)
if verbose:
yield "`````\n"
except StopIteration as e:
outcome = e.value
if outcome is None:
outcome = StepOutcome(None, next_prompt="\n")
if outcome.should_exit:
exit_reason = {"result": "EXITED", "data": outcome.data}
break
if not outcome.next_prompt:
exit_reason = {"result": "CURRENT_TASK_DONE", "data": outcome.data}
break
if outcome.data is not None and tool_name != "no_tool":
datastr = json.dumps(outcome.data, ensure_ascii=False, default=str) if isinstance(outcome.data, (dict, list)) else str(outcome.data)
tool_results.append({"tool_use_id": tid, "content": datastr})
next_prompts.add(outcome.next_prompt)
if not next_prompts or exit_reason:
break
next_prompt = handler.turn_end_callback(response, tool_calls, tool_results, turn,
"\n".join(next_prompts), exit_reason)
messages = [{"role": "user", "content": next_prompt, "tool_results": tool_results}]
return exit_reason or {"result": "MAX_TURNS_EXCEEDED"}
# ---------------------------------------------------------------------------
# Config loading (mykey.py / mykey.json)
# ---------------------------------------------------------------------------
def load_mykeys():
"""Load LLM configs from mykey.py or mykey.json.
Format (mykey.py):
llm1 = {"apikey": "sk-...", "apibase": "https://api.openai.com", "model": "gpt-4o", "name": "GPT-4o"}
llm2 = {"apikey": "sk-...", "apibase": "https://api.deepseek.com", "model": "deepseek-chat", "name": "DeepSeek"}
Format (mykey.json):
{"llm1": {"apikey": "...", "apibase": "...", "model": "...", "name": "..."}}
"""
# Try mykey.py
for search_dir in [SCRIPT_DIR, os.getcwd()]:
mykey_py = os.path.join(search_dir, "mykey.py")
if os.path.exists(mykey_py):
import importlib.util
spec = importlib.util.spec_from_file_location("mykey", mykey_py)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return {k: v for k, v in vars(mod).items()
if not k.startswith("_") and isinstance(v, dict) and "apikey" in v}
# Try mykey.json
for search_dir in [SCRIPT_DIR, os.getcwd()]:
mykey_json = os.path.join(search_dir, "mykey.json")
if os.path.exists(mykey_json):
with open(mykey_json, "r", encoding="utf-8") as f:
data = json.load(f)
return {k: v for k, v in data.items()
if isinstance(v, dict) and "apikey" in v}
return {}
def get_global_memory():
"""Read L1 + L2 memory for the system prompt."""
prompt = f"\ncwd = {TEMP_DIR} (./)\n"
prompt += f"\n[Memory] ({MEMORY_DIR})\n"
insight_path = os.path.join(MEMORY_DIR, "global_mem_insight.txt")
if os.path.exists(insight_path):
with open(insight_path, "r", encoding="utf-8", errors="replace") as f:
prompt += f"L1 Insight:\n{f.read()}\n"
return prompt
# ---------------------------------------------------------------------------
# Agent (the main class users interact with)
# ---------------------------------------------------------------------------
class Agent:
"""The main agent. Put tasks on a queue, drain the output queue for results.
Usage:
agent = Agent()
threading.Thread(target=agent.run, daemon=True).start()
dq = agent.put_task("Hello!")
while True:
item = dq.get()
if "done" in item: print(item["done"]); break
if "next" in item: print(item["next"], end="")
"""
def __init__(self):
self.lock = threading.Lock()
self.history = []
self.handler = None
self.task_queue = queue.Queue()
self.is_running = False
self.stop_sig = False
self.llm_no = 0
self.inc_out = False
self.verbose = True
self.log_path = ""
self.llmclient = None
self._llm_clients = []
self._llm_names = []
self._llm_configs = []
self.custom_system_prompt = ""
# Load custom system prompt from file if it exists
_sp_path = os.path.join(SCRIPT_DIR, "custom_system_prompt.txt")
if os.path.exists(_sp_path):
try:
with open(_sp_path, "r", encoding="utf-8") as f:
self.custom_system_prompt = f.read()
except Exception:
pass
self._load_llms()
def set_custom_system_prompt(self, text):
"""Set or clear the custom system prompt."""
self.custom_system_prompt = text or ""
def _load_llms(self):
"""Initialize LLM clients from mykey config."""
mykeys = load_mykeys()
if not mykeys:
print("[agent_core] WARNING: no LLM config found. Create mykey.py or mykey.json.")
return
self._llm_clients = []
self._llm_names = []
self._llm_configs = []
for k, cfg in mykeys.items():
try:
client = LLMClient(cfg)
self._llm_clients.append(client)
self._llm_names.append(cfg.get("name", cfg.get("model", k)))
self._llm_configs.append(dict(cfg))
except Exception as e:
print(f"[agent_core] Failed to init LLM '{k}': {e}")
if self._llm_clients:
# Auto-discover models from l0veyou gateway if we have a valid session key
self._auto_discover_models()
# Default to deepseek-v4-flash if available
default_idx = 0
for i, cfg in enumerate(self._llm_configs):
model = cfg.get("model", "").lower()
if "deepseek-v4-flash" in model:
default_idx = i
break
self.llm_no = default_idx
self.llmclient = self._llm_clients[default_idx]
if default_idx > 0:
print(f"[agent_core] Default LLM set to: {self._llm_names[default_idx]} ({self._llm_configs[default_idx].get('model', '')})")
def _auto_discover_models(self):
"""Auto-discover available models from l0veyou gateway and merge into mykey.json."""
try:
import requests as _req
# Find a l0veyou config with a valid session key
l0_cfg = None
for cfg in self._llm_configs:
apibase = cfg.get("apibase", "").lower()
if "l0veyou" in apibase and cfg.get("apikey", "").startswith("sess-"):
l0_cfg = cfg
break
if not l0_cfg:
return # No l0veyou session key, skip discovery
base = l0_cfg["apibase"].rstrip("/").replace("/v1", "")
apikey = l0_cfg["apikey"]
url = base + "/v1/models"
r = _req.get(url, headers={"Authorization": f"Bearer {apikey}"}, timeout=10)
if r.status_code != 200:
return
models = r.json().get("data", [])
if not models:
return
# Build set of existing model names (lowercase for comparison)
existing = set()
for cfg in self._llm_configs:
existing.add(cfg.get("model", "").lower())
# Define display names for known models
name_map = {
"gpt-5.5": "GPT-5.5",
"gpt-5.4": "GPT-5.4",
"gpt-5-mini": "GPT-5 Mini",
"claude-opus-4-8": "Claude Opus 4.8",
"claude-opus-4-7": "Claude Opus 4.7",
"gemini-3.1-pro-preview": "Gemini 3.1 Pro",
"gemini-3.5-flash": "Gemini 3.5 Flash",
"deepseek-v4-flash": "DeepSeek V4 Flash",
"deepseek-v4-pro": "DeepSeek V4 Pro",
"deepseek-r1-14b": "DeepSeek R1",
"glm-5.2": "GLM-5.2",
"glm-5.1": "GLM-5.1",
"kimi-k2.7": "Kimi K2.7",
"minimax-m3": "MiniMax M3",
"mistral-small-24b": "Mistral Small",
"qwen-2.5-coder-14b": "Qwen Coder",
"llama-3.1-8b": "Llama 3.1",
}
added = 0
for m in models:
model_id = m.get("id", "")
# l0veyou returns "Free/model-name" prefix; strip it for the model field
# but keep the original for API calls
clean_model = model_id
if clean_model.startswith("Free/"):
clean_model = clean_model[5:] # Remove "Free/" prefix
if clean_model.lower() in existing:
continue # Already configured
# Generate display name
display_name = name_map.get(clean_model.lower(), clean_model)
new_cfg = {
"name": display_name,
"model": clean_model,
"apibase": base + "/v1",
"apikey": apikey,
"remark": "auto-discovered",
}
try:
client = LLMClient(new_cfg)
self._llm_clients.append(client)
self._llm_names.append(display_name)
self._llm_configs.append(new_cfg)
existing.add(clean_model.lower())
added += 1
except Exception:
pass
if added > 0:
print(f"[agent_core] Auto-discovered {added} models from gateway")
self._save_mykeys()
except Exception as e:
# Silent fail - auto-discovery is best-effort
pass
def _mykey_path(self):
"""Return the path to mykey.json (create location if not exists)."""
for search_dir in [SCRIPT_DIR, os.getcwd()]:
p = os.path.join(search_dir, "mykey.json")
if os.path.exists(p):
return p
return os.path.join(SCRIPT_DIR, "mykey.json")
def _save_mykeys(self):
"""Persist current LLM configs to mykey.json."""
data = {}
for i, cfg in enumerate(self._llm_configs):
data[f"llm{i+1}"] = cfg
path = self._mykey_path()
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"[agent_core] Failed to save mykey.json: {e}")
def add_llm(self, cfg):
"""Add a new LLM at runtime and persist to mykey.json.
cfg must contain: name, model, apibase, apikey, remark (optional).
Returns the new LLM index."""
with self.lock:
client = LLMClient(cfg)
self._llm_clients.append(client)
self._llm_names.append(cfg.get("name", cfg.get("model", "")))
self._llm_configs.append(dict(cfg))
self._save_mykeys()
return len(self._llm_clients) - 1
def update_llm(self, idx, cfg):
"""Update an existing LLM at index idx and persist.
Returns idx."""
with self.lock:
if not (0 <= idx < len(self._llm_clients)):
raise Exception("invalid LLM index")
client = LLMClient(cfg)
self._llm_clients[idx] = client
self._llm_names[idx] = cfg.get("name", cfg.get("model", ""))
self._llm_configs[idx] = dict(cfg)
if self.llm_no == idx:
self.llmclient = client
self._save_mykeys()
return idx
def delete_llm(self, idx):
"""Delete LLM at index idx and persist. Cannot delete the last one."""
with self.lock:
if not (0 <= idx < len(self._llm_clients)):
raise Exception("invalid LLM index")
if len(self._llm_clients) <= 1:
raise Exception("cannot delete the last LLM")
self._llm_clients.pop(idx)
self._llm_names.pop(idx)
self._llm_configs.pop(idx)
if self.llm_no >= len(self._llm_clients):
self.llm_no = 0
self.llmclient = self._llm_clients[self.llm_no] if self._llm_clients else None
self._save_mykeys()
return self.llm_no
def llm_remark(self, idx):
"""Return the remark string for LLM at idx."""
if 0 <= idx < len(self._llm_configs):
return self._llm_configs[idx].get("remark", "")
return ""
def get_llm_detail(self, idx):
"""Return the full config dict for LLM at idx (for editing)."""
if 0 <= idx < len(self._llm_configs):
return dict(self._llm_configs[idx])
return {}
def list_llms(self):
"""Return [(index, name, is_active), ...]"""
return [(i, name, i == self.llm_no) for i, name in enumerate(self._llm_names)]
def is_configured(self):
"""True only when at least one LLM client has a real (non-placeholder) API key."""
for c in self._llm_clients:
key = getattr(c, "api_key", "") or ""
if key and "YOUR-" not in key and not key.endswith("..."):
return True
return False
def get_llm_name(self, client=None):
if client is None:
client = self.llmclient
if client is None:
return "not configured"
return client.name
def next_llm(self, n=-1):
"""Switch LLM. n=-1 for next, or pass an explicit index."""
if not self._llm_clients:
raise Exception("No LLM clients available")
self.llm_no = ((self.llm_no + 1) if n < 0 else n) % len(self._llm_clients)
self.llmclient = self._llm_clients[self.llm_no]
return self.llm_no
def abort(self):
if not self.is_running:
return
print("Aborting current task...")
self.stop_sig = True
if self.handler is not None:
self.handler.code_stop_signal.append(1)
def put_task(self, query, source="user", images=None):
"""Submit a task. Returns an output queue to drain."""
display_queue = queue.Queue()
self.task_queue.put({"query": query, "source": source, "images": images or [], "output": display_queue})
return display_queue
def run(self):
"""Main agent loop (run in a daemon thread)."""
while True:
task = self.task_queue.get()
if isinstance(task, str):
break
raw_query, source, display_queue = task["query"], task["source"], task["output"]
self.is_running = True
self.stop_sig = False
# Truncate very long prompts to a file
if len(raw_query) > 10000:
task_file = os.path.join(TEMP_DIR, f"user_prompt_{int(time.time())}.md")
with open(task_file, "w", encoding="utf-8") as f:
f.write(raw_query)
raw_query = f"Long user prompt saved to {task_file}. Read and execute it."
rquery = raw_query.replace("\n", " ")[:200]
self.history.append(f"[USER]: {rquery}")
# Build system prompt
_base_prompt = self.custom_system_prompt if self.custom_system_prompt else SYSTEM_PROMPT
sys_prompt = _base_prompt + f"\nToday: {time.strftime('%Y-%m-%d %a')}\n" + get_global_memory()
# Create handler
handler = AgentHandler(self, self.history, TEMP_DIR)
if self.handler and "key_info" in self.handler.working:
handler.working["key_info"] = self.handler.working.get("key_info", "")
self.handler = handler
# Set log path
import random
logid = f"{(time.time_ns() + random.randrange(1_000_000)) % 1_000_000:06d}"
self.log_path = os.path.join(TEMP_DIR, f"model_responses/model_responses_{logid}.txt")
os.makedirs(os.path.dirname(self.log_path), exist_ok=True)
if self.llmclient:
self.llmclient.log_path = self.log_path
# Run the agent loop
try:
full_resp = ""
last_pos = 0
curr_turn = 0
gen = agent_loop(self.llmclient, sys_prompt, raw_query, handler,
TOOLS_SCHEMA, max_turns=180, verbose=self.verbose)
for chunk in gen:
if self.stop_sig:
break
if isinstance(chunk, dict) and "turn" in chunk:
curr_turn = chunk["turn"]
continue
full_resp += chunk
if len(full_resp) - last_pos > 30 or "LLM Running" in chunk:
display_queue.put({
"next": full_resp[last_pos:] if self.inc_out else full_resp,
"source": source, "turn": curr_turn,
})
last_pos = len(full_resp)
if self.inc_out and last_pos < len(full_resp):
display_queue.put({"next": full_resp[last_pos:], "source": source, "turn": curr_turn})
display_queue.put({"done": full_resp, "source": source, "turn": curr_turn})
self.history = handler.history_info
except Exception as e:
err = sanitize_error(format_error(e))
print(f"[agent_core] Error: {err}")
display_queue.put({"done": full_resp + f"\n```\n{err}\n```", "source": source})
finally:
if self.stop_sig:
print("[agent_core] Task aborted by user.")
self.is_running = self.stop_sig = False
self.task_queue.task_done()
if self.handler is not None:
self.handler.code_stop_signal.append(1)