-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
374 lines (308 loc) · 13.1 KB
/
Copy pathtools.py
File metadata and controls
374 lines (308 loc) · 13.1 KB
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
"""Hermes tool handlers for the Index Network plugin.
Handlers follow the official Hermes plugin contract:
- signature: handler(args: dict, **kwargs) -> str
- always return a JSON string
- catch errors and return JSON error payloads instead of raising
"""
from __future__ import annotations
import copy
import json
import os
import platform
import shutil
import subprocess
import urllib.parse
from typing import Any
from .env_transport import TransportError
from .transport import get_transport, reset_transport, set_transport_for_tests
# Universal-link host for Index deep links. The macOS app claims /c/*, /o/* and
# /u/* through its apple-app-site-association file, so the same https URL opens
# the app when it is installed and the web landing page when it is not. The
# plugin never detects app installation: it runs on the agent's host, which is
# usually not the user's Mac, so the OS decides at click time.
INDEX_APP_BASE_URL = "https://index.network"
_MAX_APP_URL_WALK_DEPTH = 16
_OPEN_URL_TIMEOUT_SECONDS = 15
_FORWARDED_MCP_TOOLS = frozenset(
{
"research_profile",
"create_intent",
"update_intent",
"add_intent_to_network",
"list_intent_networks",
"search_intents",
"read_networks",
"read_network_memberships",
"update_network",
"create_network",
"create_network_membership",
"list_opportunities",
"update_opportunity",
"read_docs",
}
)
def _json(payload: dict[str, Any]) -> str:
return json.dumps(payload, separators=(",", ":"))
def _error(message: str, **extra: Any) -> str:
payload: dict[str, Any] = {"success": False, "error": message}
payload.update(extra)
return _json(payload)
def _error_payload(message: str, **extra: Any) -> dict[str, Any]:
payload: dict[str, Any] = {"success": False, "error": message}
payload.update(extra)
return payload
def _clean_string(value: Any) -> str | None:
if not isinstance(value, str):
return None
value = value.strip()
return value or None
def _positive_int(value: Any, name: str, *, maximum: int | None = None) -> tuple[int | None, str | None]:
if value is None:
return None, None
if isinstance(value, bool):
return None, f"{name} must be an integer."
try:
parsed = int(value)
except (TypeError, ValueError):
return None, f"{name} must be an integer."
if parsed < 1:
return None, f"{name} must be at least 1."
if maximum is not None and parsed > maximum:
return None, f"{name} must be at most {maximum}."
return parsed, None
def _app_base_url() -> str:
"""Return the universal-link origin used for Index deep links.
Only a well-formed `https://<host>` origin is honored. A malformed or
schemeless override (for example `index.network`) falls back to the constant:
a base that parses to an empty scheme/netloc would make every relative path
compare equal to it in `index_open_app` and turn that tool into a generic
local-file opener.
"""
raw = os.environ.get("INDEX_APP_BASE_URL", "").strip().rstrip("/")
if not raw:
return INDEX_APP_BASE_URL
try:
parts = urllib.parse.urlsplit(raw)
except ValueError:
return INDEX_APP_BASE_URL
if parts.scheme != "https" or not parts.netloc:
return INDEX_APP_BASE_URL
return raw
def _attach_app_urls(value: Any, base_url: str, depth: int = 0) -> None:
"""Attach `appUrl` to every opportunity-shaped object in a decoded payload.
An object counts as an opportunity when it carries a non-empty
`opportunityId`. Existing `appUrl` values are never overwritten.
"""
if depth > _MAX_APP_URL_WALK_DEPTH:
return
if isinstance(value, dict):
opportunity_id = _clean_string(value.get("opportunityId"))
if opportunity_id and not _clean_string(value.get("appUrl")):
value["appUrl"] = f"{base_url}/o/{opportunity_id}"
for item in value.values():
_attach_app_urls(item, base_url, depth + 1)
return
if isinstance(value, list):
for item in value:
_attach_app_urls(item, base_url, depth + 1)
def _with_app_urls(payload: Any) -> Any:
"""Return the payload with deep links attached, or untouched on any surprise."""
try:
enriched = copy.deepcopy(payload)
_attach_app_urls(enriched, _app_base_url())
return enriched
except Exception: # noqa: BLE001 - deep links are additive; never fail a response.
return payload
def _parse_json(data: str) -> Any:
return json.loads(data)
def _parse_sse(data: str) -> Any:
"""Return the last JSON data payload from an SSE response."""
last_payload: Any = None
data_lines: list[str] = []
def flush() -> None:
nonlocal last_payload, data_lines
if not data_lines:
return
raw = "\n".join(data_lines).strip()
data_lines = []
if not raw or raw == "[DONE]":
return
last_payload = _parse_json(raw)
for line in data.splitlines():
if not line.strip():
flush()
continue
if line.startswith("data:"):
data_lines.append(line[5:].lstrip())
flush()
if last_payload is None:
raise ValueError("SSE response did not include a JSON data payload")
return last_payload
def _decode_tool_result(message: dict[str, Any]) -> dict[str, Any]:
if "error" in message:
err = message.get("error") or {}
if isinstance(err, dict):
return {
"success": False,
"error": str(err.get("message") or "Index MCP request failed."),
"code": err.get("code"),
}
return {"success": False, "error": str(err)}
result = message.get("result")
if not isinstance(result, dict):
return {"success": True, "data": result}
content = result.get("content")
if isinstance(content, list):
texts = [item.get("text") for item in content if isinstance(item, dict) and item.get("type") == "text"]
text = "\n".join(str(item) for item in texts if item is not None).strip()
if text:
try:
parsed_text = _parse_json(text)
except json.JSONDecodeError:
parsed_text = None
if isinstance(parsed_text, dict):
# An MCP capability denial / tool error comes back as a JSON dict
# (e.g. {"error": ..., "code": "MCP_CAPABILITY_DENIED"}) with no
# "success" key while result.isError is true. Without this, callers
# that check `payload.get("success") is False` would read the
# denial as success. Derive success from isError when absent.
if "success" not in parsed_text:
parsed_text["success"] = not bool(result.get("isError"))
return parsed_text
return {"success": not bool(result.get("isError")), "text": text}
return {"success": not bool(result.get("isError")), "data": result}
def _call_index_mcp(tool_name: str, arguments: dict[str, Any]) -> str:
try:
result = get_transport().call_mcp(tool_name, arguments)
return _json(_with_app_urls(_decode_tool_result({"result": result})))
except TransportError as exc:
return _json(exc.as_payload())
except Exception as exc: # noqa: BLE001 - Hermes handlers must not raise.
return _error(f"Index transport response could not be processed: {exc}")
def _api_request(
method: str,
path: str,
body: dict[str, Any] | None = None,
*,
no_content_payload: dict[str, Any] | None = None,
) -> dict[str, Any]:
try:
result = get_transport().request_rest(method, path, body)
if result.get("no_content") is True and no_content_payload is not None:
return no_content_payload
return result
except TransportError as exc:
return exc.as_payload()
except Exception as exc: # noqa: BLE001 - Hermes handlers must not raise.
return _error_payload(f"Index transport response could not be processed: {exc}")
def index_forwarded_mcp_tool(tool_name: str, args: dict, **kwargs) -> str:
"""Forward a Hermes tool call to an allowlisted Index MCP tool."""
del kwargs
if tool_name not in _FORWARDED_MCP_TOOLS:
return _error(f"Unsupported Index MCP tool: {tool_name}")
if not isinstance(args, dict):
return _error("Arguments must be an object.")
return _call_index_mcp(tool_name, args)
def make_mcp_tool_handler(tool_name: str):
"""Create a Hermes handler for an allowlisted pass-through Index MCP tool."""
if tool_name not in _FORWARDED_MCP_TOOLS:
raise ValueError(f"Unsupported Index MCP tool: {tool_name}")
def handler(args: dict, **kwargs) -> str:
return index_forwarded_mcp_tool(tool_name, args, **kwargs)
handler.__name__ = f"index_{tool_name}"
handler.__doc__ = f"Forward to the Index MCP {tool_name} tool."
return handler
def index_read_intents(args: dict, **kwargs) -> str:
"""Read Index Network intents through the canonical MCP read_intents tool."""
del kwargs
if not isinstance(args, dict):
return _error("Arguments must be an object.")
arguments: dict[str, Any] = {}
network_id = _clean_string(args.get("networkId"))
if network_id:
arguments["networkId"] = network_id
user_id = _clean_string(args.get("userId"))
if user_id:
arguments["userId"] = user_id
limit, limit_error = _positive_int(args.get("limit"), "limit", maximum=100)
if limit_error:
return _error(limit_error)
if limit is not None:
arguments["limit"] = limit
page, page_error = _positive_int(args.get("page"), "page")
if page_error:
return _error(page_error)
if page is not None:
arguments["page"] = page
return _call_index_mcp("read_intents", arguments)
def _url_opener_command(url: str, system: str | None = None) -> list[str] | None:
"""Return the platform command that hands a URL to the OS, if there is one."""
resolved = (system or platform.system() or "").strip().lower()
if resolved == "darwin":
return ["open", url]
if resolved == "windows":
# Never route through `cmd /c start`: subprocess quotes an argument only
# when it contains whitespace, so cmd.exe metacharacters (& | ^ < > %)
# inside an otherwise valid https://index.network URL would survive
# unquoted and execute as separate commands. rundll32 is handed the URL
# as a single argv entry and no shell ever re-parses it.
return ["rundll32", "url.dll,FileProtocolHandler", url]
if shutil.which("xdg-open"):
return ["xdg-open", url]
return None
def _open_url(command: list[str]) -> str | None:
"""Run a URL-opener command. Returns None on success, an error string otherwise."""
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=_OPEN_URL_TIMEOUT_SECONDS,
check=False,
)
except Exception as exc: # noqa: BLE001 - Hermes handlers must not raise.
return str(exc)
if result.returncode != 0:
return (result.stderr or "").strip() or f"exit code {result.returncode}"
return None
def index_open_app(args: dict, **kwargs) -> str:
"""Open an Index universal link with the operating system's default handler."""
del kwargs
if not isinstance(args, dict):
return _error("Arguments must be an object.")
base_url = _app_base_url()
target = _clean_string(args.get("target")) or base_url
try:
base_parts = urllib.parse.urlsplit(base_url)
target_parts = urllib.parse.urlsplit(target)
except ValueError:
return _error(f"target must be an {base_url} URL.")
# An absolute https origin is required in its own right, not just an origin
# that matches the base: a bare filesystem path used as a target has an empty
# scheme and netloc and must never be handed to the OS opener.
if target_parts.scheme != "https" or not target_parts.netloc:
return _error(f"target must be an {base_url} URL.")
if target_parts.scheme != base_parts.scheme or target_parts.netloc != base_parts.netloc:
return _error(f"target must be an {base_url} URL.")
command = _url_opener_command(target)
if command is None:
return _error(
f"No URL opener is available on this host. Open {target} manually to continue in the Index app.",
url=target,
)
failure = _open_url(command)
if failure is not None:
return _error(f"Could not open {target}: {failure}", url=target)
return _json({"success": True, "url": target})
def index_agent_me(args: dict, **kwargs) -> str:
"""Return the agent the key's owner selected to handle negotiations."""
del kwargs
if not isinstance(args, dict):
return _error("Arguments must be an object.")
payload = _api_request("GET", "/agents/me")
if payload.get("success") is False:
return _json(payload)
merged = {"success": True}
merged.update(payload)
merged["success"] = True
return _json(merged)