bridges: add an Outlook (Microsoft Graph) bridge + seeds
A standalone Outlook bridge in front of Microsoft Graph — mail, calendar events, Microsoft To Do tasks, contacts, and OneDrive files — so skill bodies never speak Graph or OAuth directly. Stdlib-only, no daemon (like Nextcloud), and registered in CATALOG so describe_bridges() feeds it into every codegen prompt. Authorization is the OAuth2 device code flow: scripts/outlook-auth.py runs it once and stores the refresh token 0600 under .runtime/outlook/token.json; outlook_client.py refreshes on expiry/401, follows @odata.nextLink paging, and backs off on 429/Retry-After. An unauthenticated route is a 503 (run the helper), a real Graph failure a 502. Seven seeds/outlook/* starter skills (next_message, send_message — compose via the LLM bridge and confirm before sending — next_event, create_event, create_task, list_contacts, list_files), a bridges.outlook config block, and tests/test_outlook_bridge.py driving the real client over a loopback server faking the token endpoint and Graph shapes.
This commit is contained in:
1 parent
b7d405454a
commit
e434b9fbe5
37 files changed
+4505
-7
No files matched your search
@@ -0,0 +1,92 @@
|
||||
"""List the user's OneDrive files.
|
||||
|
||||
Real integration: the local Outlook bridge (Microsoft Graph). Connection comes
|
||||
from `ctx.config` (`outlook_bridge_url`, `outlook_bridge_token`); the body never
|
||||
speaks Graph or OAuth directly. It lists a OneDrive folder (root by default) and
|
||||
reports the entries, or the real failure honestly.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
from semif_agent.skills import ActionResult
|
||||
|
||||
INTEGRATION = {
|
||||
"service": "outlook",
|
||||
"transport": "http",
|
||||
"config_vars": ["outlook_bridge_url", "outlook_bridge_token"],
|
||||
}
|
||||
|
||||
CONTRACT = {
|
||||
"outlook_bridge_url": "Base URL of the local Outlook bridge, e.g. http://127.0.0.1:5231 (no trailing slash needed).",
|
||||
"outlook_bridge_token": "Shared secret for the Outlook bridge, if one is configured; sent as the X-Semif-Token header. Leave blank when the bridge requires no auth.",
|
||||
}
|
||||
|
||||
TIMEOUT_SECONDS = 30
|
||||
_STOPWORDS = re.compile(
|
||||
r"\b(list|show|what'?s|whats|in|my|the|onedrive|files|file|folder|drive|of|me)\b",
|
||||
re.I,
|
||||
)
|
||||
|
||||
|
||||
class OutlookBridgeError(Exception):
|
||||
"""A real failure talking to the local Outlook bridge."""
|
||||
|
||||
|
||||
def _get(ctx, path, params=None):
|
||||
base = str(ctx.config.get("outlook_bridge_url", "") or "").strip().rstrip("/")
|
||||
if not base:
|
||||
raise OutlookBridgeError("outlook_bridge_url is not configured")
|
||||
token = str(ctx.config.get("outlook_bridge_token", "") or "").strip()
|
||||
url = base + path
|
||||
if params:
|
||||
url += ("&" if "?" in url else "?") + urllib.parse.urlencode(params)
|
||||
headers = {"Accept": "application/json"}
|
||||
if token:
|
||||
headers["X-Semif-Token"] = token
|
||||
http_request = urllib.request.Request(url, method="GET", headers=headers)
|
||||
try:
|
||||
with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response:
|
||||
return json.loads(response.read().decode("utf-8") or "{}")
|
||||
except urllib.error.HTTPError as exc:
|
||||
detail = exc.read().decode("utf-8", "replace").strip()[:200]
|
||||
raise OutlookBridgeError(f"GET {path}: HTTP {exc.code} {detail}") from exc
|
||||
except (urllib.error.URLError, TimeoutError, ValueError) as exc:
|
||||
raise OutlookBridgeError(f"GET {path}: {exc}") from exc
|
||||
|
||||
|
||||
def _folder(text):
|
||||
cleaned = re.sub(r"\s+", " ", _STOPWORDS.sub(" ", text or "")).strip()
|
||||
return cleaned or "/"
|
||||
|
||||
|
||||
def act(ctx, request):
|
||||
path = _folder(request.text)
|
||||
try:
|
||||
data = _get(ctx, "/files", {"path": path})
|
||||
except OutlookBridgeError as exc:
|
||||
return ActionResult(action_log=str(exc), new_state=request.text)
|
||||
|
||||
entries = data.get("entries") or []
|
||||
if not entries:
|
||||
return ActionResult(
|
||||
action_log=f"{data.get('path') or path} is empty.",
|
||||
new_state=f"{data.get('path') or path} is empty.",
|
||||
)
|
||||
|
||||
lines = []
|
||||
for entry in entries[:20]:
|
||||
name = entry.get("name") or "(unnamed)"
|
||||
kind = "folder" if entry.get("is_folder") else "file"
|
||||
size = entry.get("size")
|
||||
detail = f"{kind}"
|
||||
if kind == "file" and size is not None:
|
||||
detail += f", {size} bytes"
|
||||
lines.append(f"- {name} ({detail})")
|
||||
report = f"Files in {data.get('path') or path}:\n" + "\n".join(lines)
|
||||
return ActionResult(action_log=report, new_state=report)
|
||||
Reference in new issue
Block a user