File size: 12,498 Bytes
864b105
 
 
 
 
 
35be521
864b105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35be521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a960f0b
35be521
a960f0b
35be521
 
 
 
 
 
864b105
35be521
864b105
 
35be521
864b105
 
35be521
864b105
35be521
864b105
35be521
864b105
 
35be521
 
864b105
 
5b5d9ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
864b105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b5d9ca
 
 
 
 
 
 
 
 
 
 
 
 
864b105
 
 
 
 
 
 
 
 
 
 
 
5b5d9ca
 
 
 
 
 
 
 
 
 
 
864b105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b5d9ca
 
 
 
864b105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b5d9ca
864b105
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# ui/agent/tools.py
from __future__ import annotations

import json
import time
from typing import Any
from urllib.parse import urlparse

from gradio import ChatMessage

from apis.country_profile import get_country_profiles
from apis.exa import search_immigration
from apis.firecrawl import crawl_site, scrape_page
from ui.globe_commands import apply_update_globe

from .messages import assistant_message_dict
from .traces import record_tool_trace


def truncate(text: str, limit: int = 4000) -> str:
    if len(text) <= limit:
        return text
    return text[:limit] + "\n… (truncated)"


def run_tool(
    name: str,
    arguments: str,
    *,
    globe_state: dict[str, Any] | None = None,
) -> tuple[str, dict[str, Any] | None]:
    try:
        args = json.loads(arguments or "{}")
        result, updated_globe = _run_tool(name, args, globe_state=globe_state)
        return json.dumps(result, default=str), updated_globe
    except Exception as exc:
        return json.dumps({"error": str(exc)}), globe_state


def _run_tool(
    name: str,
    args: dict[str, Any],
    *,
    globe_state: dict[str, Any] | None = None,
) -> tuple[Any, dict[str, Any] | None]:
    if name == "think":
        return {"ok": True}, globe_state
    if name == "get_country_profile":
        return get_country_profiles(args["countries"]), globe_state
    if name == "search_immigration_info":
        return (
            search_immigration(
                query=args["query"],
                num_results=min(args.get("num_results", 8), 15),
                country=args.get("country"),
                include_domains=args.get("include_domains"),
            ),
            globe_state,
        )
    if name == "scrape_web_page":
        return (
            scrape_page(
                url=args["url"],
                country=args.get("country"),
            ),
            globe_state,
        )
    if name == "crawl_web_site":
        return (
            crawl_site(
                url=args["url"],
                limit=min(args.get("limit", 10), 20),
                include_paths=args.get("include_paths"),
                country=args.get("country"),
            ),
            globe_state,
        )
    if name == "update_globe":
        if globe_state is None:
            return {"error": "Globe state is unavailable"}, None
        return apply_update_globe(globe_state, args)

    return {"error": f"Unknown tool: {name}"}, globe_state


def _parse_arguments(arguments: str) -> dict[str, Any]:
    try:
        parsed = json.loads(arguments or "{}")
        return parsed if isinstance(parsed, dict) else {}
    except json.JSONDecodeError:
        return {}


def _load_result(result: str) -> dict[str, Any]:
    try:
        parsed = json.loads(result)
        return parsed if isinstance(parsed, dict) else {}
    except json.JSONDecodeError:
        return {}


def _normalized_tool_calls(tool_calls: list[dict[str, Any]]) -> list[dict[str, Any]]:
    """Keep replayed assistant tool calls valid for the chat-completion API."""
    normalized = []
    for tool_call in tool_calls:
        function = tool_call.get("function") or {}
        normalized.append(
            {
                **tool_call,
                "function": {
                    **function,
                    "arguments": json.dumps(
                        _parse_arguments(str(function.get("arguments") or ""))
                    ),
                },
            }
        )
    return normalized


def _join(items: list[str], fallback: str) -> str:
    clean = [str(item) for item in items if item]
    return ", ".join(clean) if clean else fallback


def _url_host_or_path(url: str, *, limit: int = 80) -> str:
    raw = str(url or "").strip()
    if not raw:
        return "page"
    try:
        parsed = urlparse(raw if "://" in raw else f"https://{raw}")
        host = parsed.netloc
        path = parsed.path.strip("/")
        if host and path:
            return truncate(f"{host}/{path}", limit)
        return truncate(host or path or raw, limit)
    except Exception:
        return truncate(raw, limit)


def tool_display_title(tool_name: str, args: dict[str, Any]) -> str:
    if tool_name == "get_country_profile":
        countries = _join(args.get("countries") or [], "selected countries")
        return f"Profiles · {countries}"
    if tool_name == "search_immigration_info":
        query = truncate(str(args.get("query") or "immigration sources"), 80)
        return f"Search · {query}"
    if tool_name == "scrape_web_page":
        return f"Read · {_url_host_or_path(str(args.get('url') or ''))}"
    if tool_name == "crawl_web_site":
        return f"Crawl · {_url_host_or_path(str(args.get('url') or ''), limit=60)}"
    if tool_name == "update_globe":
        countries = _join(args.get("countries") or [], "selected countries")
        return f"Globe · {countries}"
    return f"Using {tool_name}"


def _pending_tool_message(tool_name: str, args: dict[str, Any]) -> tuple[str, str]:
    title = tool_display_title(tool_name, args)
    if tool_name == "get_country_profile":
        countries = _join(args.get("countries") or [], "selected countries")
        return title, f"Looking up country metadata for {countries}."
    if tool_name == "search_immigration_info":
        query = truncate(str(args.get("query") or "immigration sources"), 180)
        return title, f"Searching for official immigration information: {query}"
    if tool_name == "scrape_web_page":
        return title, f"Reading {args.get('url', 'the selected page')}."
    if tool_name == "crawl_web_site":
        return title, f"Crawling related pages from {args.get('url', 'the selected site')}."
    if tool_name == "update_globe":
        countries = _join(args.get("countries") or [], "the selected countries")
        return title, f"Showing {countries} on the globe."
    return title, f"Running `{tool_name}`."


def _format_log_result(result: str) -> Any:
    loaded = _load_result(result)
    serialized = json.dumps(loaded, default=str, indent=2)
    if len(serialized) <= 1500:
        return loaded
    return serialized[:1500] + "\n… (truncated)"


def _tool_log_metadata(
    tool_name: str,
    parsed_args: dict[str, Any],
    result: str,
) -> dict[str, Any]:
    return {
        "tool": tool_name,
        "arguments": parsed_args,
        "result": _format_log_result(result),
    }


def should_emit_reasoning(
    reasoning: str,
    tool_calls: list[dict[str, Any]] | None,
) -> bool:
    thought = reasoning.strip()
    if not thought:
        return False
    if tool_calls and len(tool_calls) == 1:
        function = tool_calls[0].get("function") or {}
        if function.get("name") == "think":
            return False
    return True


def emit_thinking_message(
    ui_messages: list[ChatMessage],
    reasoning: str,
    globe_state: dict[str, Any],
):
    thought = reasoning.strip()
    if not thought:
        return
    ui_messages.append(
        ChatMessage(
            role="assistant",
            content=thought,
            metadata={
                "title": "Thinking",
                "status": "done",
            },
        )
    )
    yield ui_messages, globe_state


def _done_tool_message(tool_name: str, args: dict[str, Any], result: str) -> str:
    parsed = _load_result(result)
    if parsed.get("error"):
        return f"Tool returned an issue: {parsed['error']}"

    if tool_name == "get_country_profile":
        countries = [
            country.get("name", "")
            for country in parsed.get("countries", [])
            if isinstance(country, dict)
        ]
        return f"Found country metadata for {_join(countries, 'the selected countries')}."

    if tool_name == "search_immigration_info":
        count = parsed.get("num_results", 0)
        official = parsed.get("official_results", 0)
        hints = parsed.get("official_domain_hints") or []
        hint_text = f" Suggested official domains: {_join(hints, 'none')}." if hints else ""
        return f"Found {count} search results, including {official} likely official source(s).{hint_text}"

    if tool_name == "scrape_web_page":
        title = parsed.get("title") or args.get("url") or "the page"
        source = parsed.get("source_url") or parsed.get("url")
        return f"Extracted official page content from {title}. Source: {source}"

    if tool_name == "crawl_web_site":
        pages = parsed.get("pages_found", 0)
        return f"Collected {pages} related page(s) from the official site."

    if tool_name == "update_globe":
        countries = [
            country.get("name", country.get("iso2", ""))
            for country in parsed.get("countries", [])
            if isinstance(country, dict)
        ]
        return f"Updated the globe with {_join(countries, 'the selected countries')}."

    return f"`{tool_name}` completed."


def execute_tool_calls(
    api_messages: list[dict[str, Any]],
    ui_messages: list[ChatMessage],
    tool_calls: list[dict[str, Any]],
    content: str,
    globe_state: dict[str, Any],
):
    tool_calls = _normalized_tool_calls(tool_calls)
    api_messages.append(assistant_message_dict(content, tool_calls))

    for tool_call in tool_calls:
        tool_name = tool_call["function"]["name"]
        tool_args = tool_call["function"]["arguments"]
        parsed_args = _parse_arguments(tool_args)
        started = time.monotonic()

        if tool_name == "think":
            thought = str(parsed_args.get("thought") or "").strip() or "Planning next steps."
            ui_messages.append(
                ChatMessage(
                    role="assistant",
                    content=thought,
                    metadata={
                        "title": "Thinking",
                        "status": "done",
                    },
                )
            )
            yield ui_messages, globe_state

            result, globe_state = run_tool(
                tool_name,
                tool_args,
                globe_state=globe_state,
            )
            duration = time.monotonic() - started
            record_tool_trace(
                tool_name=tool_name,
                arguments=tool_args,
                result=result,
                duration=duration,
            )
            ui_messages[-1] = ChatMessage(
                role="assistant",
                content=thought,
                metadata={
                    "title": "Thinking",
                    "status": "done",
                    "duration": duration,
                    "log": _tool_log_metadata(tool_name, parsed_args, result),
                },
            )
            yield ui_messages, globe_state

            api_messages.append(
                {
                    "role": "tool",
                    "tool_call_id": tool_call["id"],
                    "name": tool_name,
                    "content": result,
                }
            )
            continue

        title, pending_message = _pending_tool_message(tool_name, parsed_args)
        ui_messages.append(
            ChatMessage(
                role="assistant",
                content=pending_message,
                metadata={
                    "title": title,
                    "status": "pending",
                    "log": {
                        "tool": tool_name,
                        "arguments": parsed_args,
                    },
                },
            )
        )
        yield ui_messages, globe_state

        result, globe_state = run_tool(
            tool_name,
            tool_args,
            globe_state=globe_state,
        )
        duration = time.monotonic() - started
        record_tool_trace(
            tool_name=tool_name,
            arguments=tool_args,
            result=result,
            duration=duration,
        )

        ui_messages[-1] = ChatMessage(
            role="assistant",
            content=_done_tool_message(tool_name, parsed_args, result),
            metadata={
                "title": title,
                "status": "done",
                "duration": duration,
                "log": _tool_log_metadata(tool_name, parsed_args, result),
            },
        )
        yield ui_messages, globe_state

        api_messages.append(
            {
                "role": "tool",
                "tool_call_id": tool_call["id"],
                "name": tool_name,
                "content": result,
            }
        )