File size: 7,301 Bytes
a154e2b
 
4026086
ee9487b
a154e2b
4026086
a154e2b
538e3f8
4026086
a154e2b
44b08c9
a154e2b
 
 
 
4026086
 
 
 
 
538e3f8
44b08c9
709552f
538e3f8
 
 
 
4026086
 
 
538e3f8
a154e2b
 
538e3f8
7420645
 
 
538e3f8
44b08c9
538e3f8
 
44b08c9
538e3f8
44b08c9
538e3f8
7420645
538e3f8
 
4f7caba
538e3f8
 
ee9487b
4026086
538e3f8
44b08c9
538e3f8
4026086
538e3f8
85f0456
538e3f8
 
 
 
 
 
 
 
85f0456
538e3f8
 
 
 
85f0456
538e3f8
 
 
4026086
 
85f0456
44b08c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538e3f8
a154e2b
538e3f8
4026086
44b08c9
538e3f8
85f0456
44b08c9
 
 
 
 
538e3f8
 
 
 
85f0456
44b08c9
538e3f8
 
44b08c9
85f0456
538e3f8
 
44b08c9
 
 
 
 
538e3f8
44b08c9
7420645
538e3f8
4026086
538e3f8
4026086
 
 
44b08c9
4026086
85f0456
44b08c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6962466
709552f
85f0456
6962466
44b08c9
538e3f8
44b08c9
538e3f8
4026086
538e3f8
44b08c9
 
 
 
 
85f0456
44b08c9
538e3f8
 
4026086
 
 
44b08c9
538e3f8
4026086
 
 
538e3f8
 
4026086
 
 
 
 
 
538e3f8
 
 
709552f
538e3f8
44b08c9
 
4026086
44b08c9
4026086
44b08c9
7420645
 
44b08c9
 
 
 
 
 
 
 
 
 
 
 
 
4026086
44b08c9
ee9487b
709552f
538e3f8
 
 
a154e2b
85f0456
a154e2b
4026086
 
 
 
44b08c9
a154e2b
538e3f8
4026086
 
44b08c9
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from langgraph.graph import StateGraph, END, START
from shared_store import url_time
import time
from langchain_core.rate_limiters import InMemoryRateLimiter
from langgraph.prebuilt import ToolNode
from tools import (
    get_rendered_html, download_file, post_request,
    run_code, add_dependencies, ocr_image_tool, transcribe_audio, encode_image_to_base64
)
from typing import TypedDict, Annotated, List
from langchain_core.messages import trim_messages, HumanMessage
from langchain.chat_models import init_chat_model
from langgraph.graph.message import add_messages
import os
from dotenv import load_dotenv
load_dotenv()

EMAIL = os.getenv("EMAIL")
SECRET = os.getenv("SECRET")

RECURSION_LIMIT = 5000
MAX_TOKENS = 60000


# -------------------------------------------------
# STATE
# -------------------------------------------------
class AgentState(TypedDict):
    messages: Annotated[List, add_messages]


TOOLS = [
    run_code, get_rendered_html, download_file,
    post_request, add_dependencies, ocr_image_tool, transcribe_audio, encode_image_to_base64
]


# -------------------------------------------------
# LLM INIT
# -------------------------------------------------
rate_limiter = InMemoryRateLimiter(
    requests_per_second=4 / 60,
    check_every_n_seconds=1,
    max_bucket_size=4
)

llm = init_chat_model(
    model_provider="google_genai",
    model="gemini-2.5-flash",
    rate_limiter=rate_limiter
).bind_tools(TOOLS)


# -------------------------------------------------
# SYSTEM PROMPT
# -------------------------------------------------
SYSTEM_PROMPT = f"""
You are an autonomous quiz-solving agent.

Your job is to:
1. Load each quiz page from the given URL.
2. Extract instructions, parameters, and submit endpoint.
3. Solve tasks exactly.
4. Submit answers ONLY to the correct endpoint.
5. Follow new URLs until none remain, then output END.

Rules:
- For base64 generation of an image NEVER use your own code, always use the "encode_image_to_base64" tool that's provided
- Never hallucinate URLs or fields.
- Never shorten endpoints.
- Always inspect server response.
- Never stop early.
- Use tools for HTML, downloading, rendering, OCR, or running code.
- Include:
    email = {EMAIL}
    secret = {SECRET}
"""


# -------------------------------------------------
# NEW NODE: HANDLE MALFORMED JSON
# -------------------------------------------------
def handle_malformed_node(state: AgentState):
    """
    If the LLM generates invalid JSON, this node sends a correction message
    so the LLM can try again.
    """
    print("--- DETECTED MALFORMED JSON. ASKING AGENT TO RETRY ---")
    return {
        "messages": [
            {
                "role": "user", 
                "content": "SYSTEM ERROR: Your last tool call was Malformed (Invalid JSON). Please rewrite the code and try again. Ensure you escape newlines and quotes correctly inside the JSON."
            }
        ]
    }


# -------------------------------------------------
# AGENT NODE
# -------------------------------------------------
def agent_node(state: AgentState):
    # --- TIME HANDLING START ---
    cur_time = time.time()
    cur_url = os.getenv("url")
    
    # SAFE GET: Prevents crash if url is None or not in dict
    prev_time = url_time.get(cur_url) 
    offset = os.getenv("offset", "0")

    if prev_time is not None:
        prev_time = float(prev_time)
        diff = cur_time - prev_time

        if diff >= 180 or (offset != "0" and (cur_time - float(offset)) > 90):
            print(f"Timeout exceeded ({diff}s) — instructing LLM to purposely submit wrong answer.")

            fail_instruction = """
            You have exceeded the time limit for this task (over 180 seconds).
            Immediately call the `post_request` tool and submit a WRONG answer for the CURRENT quiz.
            """

            # Using HumanMessage (as you correctly implemented)
            fail_msg = HumanMessage(content=fail_instruction)

            # We invoke the LLM immediately with this new instruction
            result = llm.invoke(state["messages"] + [fail_msg])
            return {"messages": [result]}
    # --- TIME HANDLING END ---

    trimmed_messages = trim_messages(
        messages=state["messages"],
        max_tokens=MAX_TOKENS,
        strategy="last",
        include_system=True,
        start_on="human",
        token_counter=llm, 
    )
    
    # Better check: Does it have a HumanMessage?
    has_human = any(msg.type == "human" for msg in trimmed_messages)
    
    if not has_human:
        print("WARNING: Context was trimmed too far. Injecting state reminder.")
        # We remind the agent of the current URL from the environment
        current_url = os.getenv("url", "Unknown URL")
        reminder = HumanMessage(content=f"Context cleared due to length. Continue processing URL: {current_url}")
        
        # We append this to the trimmed list (temporarily for this invoke)
        trimmed_messages.append(reminder)
    # ----------------------------------------

    print(f"--- INVOKING AGENT (Context: {len(trimmed_messages)} items) ---")
    
    result = llm.invoke(trimmed_messages)

    return {"messages": [result]}


# -------------------------------------------------
# ROUTE LOGIC (UPDATED FOR MALFORMED CALLS)
# -------------------------------------------------
def route(state):
    last = state["messages"][-1]
    
    # 1. CHECK FOR MALFORMED FUNCTION CALLS
    if "finish_reason" in last.response_metadata:
        if last.response_metadata["finish_reason"] == "MALFORMED_FUNCTION_CALL":
            return "handle_malformed"

    # 2. CHECK FOR VALID TOOLS
    tool_calls = getattr(last, "tool_calls", None)
    if tool_calls:
        print("Route → tools")
        return "tools"

    # 3. CHECK FOR END
    content = getattr(last, "content", None)
    if isinstance(content, str) and content.strip() == "END":
        return END

    if isinstance(content, list) and len(content) and isinstance(content[0], dict):
        if content[0].get("text", "").strip() == "END":
            return END

    print("Route → agent")
    return "agent"


# -------------------------------------------------
# GRAPH
# -------------------------------------------------
graph = StateGraph(AgentState)

# Add Nodes
graph.add_node("agent", agent_node)
graph.add_node("tools", ToolNode(TOOLS))
graph.add_node("handle_malformed", handle_malformed_node) # Add the repair node

# Add Edges
graph.add_edge(START, "agent")
graph.add_edge("tools", "agent")
graph.add_edge("handle_malformed", "agent") # Retry loop

# Conditional Edges
graph.add_conditional_edges(
    "agent", 
    route,
    {
        "tools": "tools",
        "agent": "agent",
        "handle_malformed": "handle_malformed", # Map the new route
        END: END
    }
)

app = graph.compile()


# -------------------------------------------------
# RUNNER
# -------------------------------------------------
def run_agent(url: str):
    # system message is seeded ONCE here
    initial_messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": url}
    ]

    app.invoke(
        {"messages": initial_messages},
        config={"recursion_limit": RECURSION_LIMIT}
    )

    print("Tasks completed successfully!")