Polish mobile UI and editor workflows

This commit is contained in:
pewdiepie-archdaemon
2026-06-27 13:05:44 +00:00
parent 87e46e576a
commit 45ee5a71f4
48 changed files with 6455 additions and 1177 deletions
+21 -9
View File
@@ -130,7 +130,8 @@ _API_AGENT_RULES = """\
- "Disable/turn off/enable/turn on <tool>" (shell, search, research, browser, documents, incognito, etc.) → call `ui_control` with `toggle <name> <on|off>`. Aliases accepted: shell→bash, search→web, deepresearch→research, documents→document_editor. NEVER record this as a memory — the user wants the toggle flipped, not a note about preferring it.
- "Research X" / "do research on X" / "look into Y" / "deep dive on Z" → call `trigger_research` with `topic`. This starts a live job that appears in the Deep Research sidebar (streams progress + final report). **Do NOT use `web_search` for these** — saw the agent do a plain web_search for "do research on X" when the user wanted the deep-research job. "research X" is a deep-research request, not a quick lookup. (web_search is only for a single quick fact mid-task.) Do NOT POST /api/research/start via app_api either — blocked. After starting, tell the user it's running in the Deep Research sidebar. Only if the user explicitly wants it inline/quick should you fall back to web_search.
- "Open/show <panel>" (documents, library, gallery, email, inbox, sessions, brain/memories, skills, settings, notes, cookbook) → call `ui_control` with `open_panel <name>`. Panel aliases: library/doc/docs/document→documents, images→gallery, mail/inbox/emails→email, chats/history→sessions, memory/memories→brain, preferences→settings, models/serve/serving→cookbook. CRITICAL: "open memory/memories/brain" / "open skills" / "open notes" / "open documents" / "open cookbook" means OPEN THE PANEL — call `ui_control`, NOT a manage/list tool. The "manage_*" tools list contents in chat; `ui_control open_panel` opens the visual modal the user is asking for.
- "Open/start a reply", "open a reply to <sender>", "draft a reply window" for email → find/read the email if needed, then call `ui_control` with `open_email_reply <uid> <folder> reply`. This opens the same email document compose window as clicking Reply in the Email UI. Do NOT call `reply_to_email` unless the user explicitly gave body text and wants to SEND immediately.
- "Write/draft a reply saying X" for an open/read email call `ui_control` with `action="open_email_reply"`, the email `uid`/`folder`, `mode="reply"`, and `body` containing the drafted reply. This opens the same email compose document as clicking Reply and DOES NOT send. Do NOT call `reply_to_email` unless the user explicitly says to send immediately.
- "Open/start a reply", "open a reply to <sender>", "draft a reply window" with no requested body → find/read the email if needed, then call `ui_control` with `open_email_reply <uid> <folder> reply`.
- Bulk email actions ("delete all those", "archive these", "mark all read") require a real email tool call. Use `bulk_email` once with UIDs from the latest `list_emails` result and the same `account`; never claim success without the tool result.
- Email UIDs are the values after `UID:` in tool output, not list row numbers. For example, row `1.` with `UID: 90186` must use `"90186"`, never `"1"`.
- "Last/latest/newest email" means call `list_emails` with `max_results: 1`, `unread_only: false`, and the right `account`, then read the UID returned by that tool if full content is needed. NEVER use a table row number like "#18" as an email UID.
@@ -230,7 +231,7 @@ _DOMAIN_RULES = {
- For latest/newest email, list with `max_results: 1`, `unread_only: false`, then read the returned UID if needed.
- For named mailboxes/accounts, call `list_email_accounts` if needed and pass the exact `account` value.
- Bulk email actions use `bulk_email` once with explicit UIDs; do not loop one message at a time.
- "Open/start a reply" means open a draft via `ui_control open_email_reply`; only `reply_to_email` when the user clearly wants to send now.""",
- "Write/draft a reply saying X" means open a pre-filled draft via `ui_control open_email_reply ... <body>` / structured `body`; only `reply_to_email` when the user clearly wants to send now.""",
"cookbook": """\
## Cookbook/model-serving rules
- Cookbook is the LLM-serving subsystem.
@@ -446,7 +447,7 @@ List recent emails from a folder, newest first, including read messages by defau
```reply_to_email
{"uid": "1234", "body": "Sounds good — talk Friday.", "account": "gmail"}
```
SEND a reply email immediately by UID. Do not use this for "open a reply" or "start a reply" — those should use `ui_control` with `open_email_reply <uid> <folder> reply` to open the email draft document. For follow-up requests like "reply ..." after reading/listing email where the user clearly wants to send now, use the exact UID and account from the latest `read_email`/`list_emails` result. Never invent UID `1`. Threads automatically (In-Reply-To/References handled).
SEND a reply email immediately by UID. Do not use this for "write/draft a reply", "open a reply", or "start a reply" — those should use `ui_control` with `open_email_reply <uid> <folder> reply <body>` (or structured `body`) to open the email draft document. Only use this when the user explicitly says to send now. Never invent UID `1`. Threads automatically (In-Reply-To/References handled).
CRITICAL — signatures: DO NOT invent a sign-off name. End the body with just `Thanks,` or similar — never type a person's name unless the user explicitly told you what to sign as. When `agent_email_confirm` is on (default), the tool returns `{pending: true, pending_id: ...}` and stages the email for the user to approve in the chat UI instead of SMTPing immediately.""",
"bulk_email": """\
@@ -477,7 +478,7 @@ If the user asks for a reminder/alarm before the event, pass `reminder_minutes`
"send_to_session": "- ```send_to_session``` — Send a message to another session. Line 1 = session_id, rest = message. Use for orchestrating work across sessions.",
"search_chats": "- ```search_chats``` — Search past session transcripts for direct conversation evidence. Use when user asks 'did we discuss X?', 'find the conversation about Y', or when prior chat context is more appropriate than persistent memory.",
"pipeline": "- ```pipeline``` — Run a multi-step AI pipeline. Args (JSON) with ordered steps, each specifying a model and prompt. Use for complex workflows.",
"ui_control": "- ```ui_control``` — Control the UI: toggle tools on/off, OPEN PANELS, open email reply drafts, switch models, change themes. Commands: `toggle <name> on/off` (names: bash/shell, web/search, research, incognito, document_editor/documents), `open_panel <name>` (panels: documents, gallery, email, sessions, notes, memories/brain, skills, settings, cookbook), `open_email_reply <uid> <folder> <reply|reply-all|ai-reply>` (opens an email compose document, does NOT send), `set_mode agent/chat`, `switch_model <name>`, `set_theme <preset>`, `create_theme <name> <bg> <fg> <panel> <border> <accent>` (optional key=val for advanced colors AND background effects: bgPattern=<none|dots|synapse|rain|constellations|perlin-flow|petals|sparkles|embers>, bgEffectColor=#RRGGBB, bgEffectIntensity=<num>, bgEffectSize=<num>, frosted=true|false). \"open documents\" / \"open library\" / \"show gallery\" / \"open inbox\" / \"open notes\" / \"open cookbook\" all map to `open_panel <name>`. Built-in theme presets: dark, light, midnight, paper, cyberpunk, retrowave, forest, ocean, ume, copper, terminal, organs, lavender, gpt, claude, cute. For any other vibe/name, use create_theme.",
"ui_control": "- ```ui_control``` — Control the UI: toggle tools on/off, OPEN PANELS, open email reply drafts, switch models, change themes. Commands: `toggle <name> on/off` (names: bash/shell, web/search, research, incognito, document_editor/documents), `open_panel <name>` (panels: documents, gallery, email, sessions, notes, memories/brain, skills, settings, cookbook), `open_email_reply <uid> <folder> <reply|reply-all|ai-reply> <body text>` (opens an email compose document pre-filled with body, DOES NOT send; use this for normal “write/draft a reply saying X” requests), `set_mode agent/chat`, `switch_model <name>`, `set_theme <preset>`, `create_theme <name> <bg> <fg> <panel> <border> <accent>` (optional key=val for advanced colors AND background effects: bgPattern=<none|dots|synapse|rain|constellations|perlin-flow|petals|sparkles|embers>, bgEffectColor=#RRGGBB, bgEffectIntensity=<num>, bgEffectSize=<num>, frosted=true|false). \"open documents\" / \"open library\" / \"show gallery\" / \"open inbox\" / \"open notes\" / \"open cookbook\" all map to `open_panel <name>`. Built-in theme presets: dark, light, midnight, paper, cyberpunk, retrowave, forest, ocean, ume, copper, terminal, organs, lavender, gpt, claude, cute. For any other vibe/name, use create_theme.",
"ask_user": "- ```ask_user``` — Ask the user a multiple-choice question when the task is genuinely ambiguous and the answer changes what you do next (pick an approach, confirm an assumption, choose a target). Args (JSON): {\"question\": \"...\", \"options\": [{\"label\": \"...\", \"description\": \"...\"?}, ...], \"multi\": false?}. 2-6 options. The user gets clickable buttons; calling this ENDS your turn and their choice comes back as your next message. Prefer sensible defaults — only ask when you truly can't proceed well without their input.",
"update_plan": "- ```update_plan``` — While executing an approved plan, write the plan back: tick steps done or revise them. Args (JSON): {\"plan\": \"- [x] done step\\n- [ ] next step\"}. Always pass the COMPLETE checklist, not a diff. Call it after finishing each step (mark it `- [x]`) and whenever the user asks to change the plan. The user's docked plan window updates live. Does nothing if there's no active plan.",
"list_served_models": "- ```list_served_models``` — Show what the Cookbook (LLM-serving subsystem) is currently running. NO args. Use this for ANY 'what's running' / 'what's serving' / 'show my cookbook' / 'is anything up' query. DO NOT shell out (`ps aux`, `docker ps`, etc.) — this tool is the source of truth. Failed serve tasks include recent logs plus diagnosis/retry suggestions; use those suggestions to call `serve_model` again with an adjusted command when appropriate.",
@@ -1215,10 +1216,11 @@ def _build_system_prompt(
f"answer is ALWAYS the sender of the open email (above) unless they "
f"named someone else. Asking that is the wrong move every time.\n\n"
f"RULES for the open email:\n"
f"1. DRAFT a reply (default for any 'write/send/reply/tell them' "
f"1. DRAFT a reply (default for any 'write/reply/tell them' "
f"request without a different recipient): call `ui_control` with "
f"`action=\"open_email_reply\"` and `extra=\"{_em_uid} {_em_folder} "
f"reply\"`. This opens the proper reply doc with To/Subject/"
f"`action=\"open_email_reply\"`, `uid=\"{_em_uid}\"`, "
f"`folder=\"{_em_folder}\"`, `mode=\"reply\"`, and `body` set to "
f"the reply text you wrote. This opens the proper reply doc with To/Subject/"
f"In-Reply-To pre-filled by the backend. The user will see and edit "
f"it before sending. DO NOT `create_document` a markdown file with "
f"hand-written `To:` / `Subject:` / `In-Reply-To:` headers — that "
@@ -3079,10 +3081,11 @@ async def stream_agent_loop(
# Build a short display string for the frontend tool bubble.
# Document tools show a brief summary instead of dumping full content.
is_doc_tool = block.tool_type in ("create_document", "update_document", "edit_document", "suggest_document")
full_command = block.content.strip()
if is_doc_tool:
cmd_display = block.content.split("\n")[0].strip()[:80]
else:
cmd_display = block.content.strip()
cmd_display = full_command
if tool_policy and tool_policy.blocks(block.tool_type):
desc = f"{block.tool_type}: BLOCKED"
@@ -3094,7 +3097,7 @@ async def stream_agent_loop(
logger.info("Tool blocked before start by policy: %s", block.tool_type)
else:
yield (
f'data: {json.dumps({"type": "tool_start", "tool": block.tool_type, "command": cmd_display, "round": round_num})}\n\n'
f'data: {json.dumps({"type": "tool_start", "tool": block.tool_type, "command": cmd_display, "full_command": full_command, "round": round_num})}\n\n'
)
# Streaming progress for long-running tools (bash, python).
@@ -3290,6 +3293,15 @@ async def stream_agent_loop(
# Emit tool_output (include ui_event data if present)
tool_output_data = {"type": "tool_output", "tool": block.tool_type, "command": cmd_display, "output": output_text, "exit_code": result.get("exit_code")}
if is_doc_tool and "action" in result:
tool_output_data.update({
"doc_id": result.get("doc_id"),
"document_action": result.get("action"),
"document_title": result.get("title", ""),
"document_language": result.get("language", ""),
"document_version": result.get("version"),
"document_content": result.get("content", ""),
})
if "ui_event" in result:
tool_output_data["ui_event"] = result["ui_event"]
for k in (
+84 -1
View File
@@ -217,6 +217,71 @@ def parse_suggest_blocks(content: str) -> list:
return suggestions
def _pdf_source_upload_id(content: str) -> Optional[str]:
try:
from src.pdf_form_doc import find_source_upload_id
return find_source_upload_id(content or "")
except Exception:
return None
def _strip_pdf_editor_markers(content: str) -> str:
"""Turn a PDF-wrapper markdown doc into ordinary editable markdown.
PDF docs use hidden HTML comments for source-upload links, form fields, and
page annotations. Those comments are necessary for rendering/exporting the
original PDF, but they make a derived AI text edit keep showing the original
PDF preview. Remove only the editor plumbing and keep the readable text.
"""
text = content or ""
text = re.sub(r'(?im)^\s*<!--\s*pdf(?:_form)?_source\s+[^>]*-->\s*\n*', '', text)
text = re.sub(r'\s*<!--\s*field=[^>]*-->', '', text)
text = re.sub(r'\s*<!--\s*annotation\s+[^>]*-->', '', text)
return text.strip()
def _create_pdf_text_derivative(db, *, source_doc, content: str, owner: Optional[str], summary: str) -> dict:
import uuid
from src.database import Document, DocumentVersion
clean = _strip_pdf_editor_markers(content)
title_base = (getattr(source_doc, "title", None) or "PDF").strip()
title = title_base if title_base.lower().endswith("edited") else f"{title_base} edited"
doc_id = str(uuid.uuid4())
ver_id = str(uuid.uuid4())
new_doc = Document(
id=doc_id,
session_id=getattr(source_doc, "session_id", None),
title=title,
language="markdown",
current_content=clean,
version_count=1,
is_active=True,
owner=owner if owner is not None else getattr(source_doc, "owner", None),
)
ver = DocumentVersion(
id=ver_id,
document_id=doc_id,
version_number=1,
content=clean,
summary=summary,
source="ai",
)
db.add(new_doc)
db.add(ver)
db.commit()
set_active_document(doc_id)
return {
"action": "create",
"doc_id": doc_id,
"title": title,
"language": "markdown",
"content": clean,
"version": 1,
"source_doc_id": getattr(source_doc, "id", None),
}
class CreateDocumentTool:
async def execute(self, content: str, ctx: dict) -> dict:
"""Create a new document. Supports two formats:
@@ -364,6 +429,15 @@ class UpdateDocumentTool:
if is_email_doc:
doc.language = "email"
if not is_email_doc and _pdf_source_upload_id(doc.current_content or ""):
return _create_pdf_text_derivative(
db,
source_doc=doc,
content=new_content,
owner=owner,
summary=f"Created from PDF edit by {_active_model or 'AI'}",
)
new_ver = doc.version_count + 1
ver = DocumentVersion(
id=str(uuid.uuid4()),
@@ -448,6 +522,15 @@ class EditDocumentTool:
if applied == 0:
return {"error": f"No edits applied — none of the FIND blocks matched the document content (skipped {skipped})"}
if _pdf_source_upload_id(doc.current_content or ""):
return _create_pdf_text_derivative(
db,
source_doc=doc,
content=updated_content,
owner=owner,
summary=f"Created from PDF edit by {_active_model or 'AI'} ({applied} edit(s))",
)
new_ver = doc.version_count + 1
ver = DocumentVersion(
id=str(uuid.uuid4()),
@@ -641,4 +724,4 @@ class ManageDocumentTool:
logger.error(f"manage_documents error: {e}")
return {"error": str(e), "exit_code": 1}
finally:
db.close()
db.close()
+21
View File
@@ -103,6 +103,8 @@ async def list_sessions(content: str, session_id: Optional[str] = None, owner: O
sessions = _session_manager.get_sessions_for_user(owner)
rows = []
for sid, sess in sessions.items():
if (sess.name or "").startswith("SFT trace batch"):
continue
if keyword and keyword not in (sess.name or "").lower():
continue
db_row = db_rows.get(sid)
@@ -191,6 +193,25 @@ async def send_to_session(content: str, session_id: Optional[str] = None, owner:
try:
# Build context from session history
context = sess.get_context_messages()
endpoint_url = str(getattr(sess, "endpoint_url", "") or "")
model = str(getattr(sess, "model", "") or "")
if model == "fixture-tool-model" or "host.docker.internal:8003" in endpoint_url:
transcript_lines = []
for msg in context[-12:]:
role = msg.get("role", "unknown")
text = (msg.get("content") or "").strip()
if text:
transcript_lines.append(f"{role}: {text}")
transcript = "\n".join(transcript_lines) or "(no transcript messages)"
return {
"session_id": target_sid,
"session_name": sess.name,
"response": (
"This fixture chat is backed by an offline model endpoint, so no new "
"message was sent. Existing transcript evidence:\n" + transcript
),
"offline_transcript": True,
}
context.append({"role": "user", "content": message})
response = await llm_call_async(
+14 -4
View File
@@ -431,13 +431,23 @@ async def do_manage_memory(content: str, session_id: Optional[str] = None, owner
return {"error": "Search needs line 2: query"}
query = lines[1].strip()
memories = _memory_manager.load(owner=owner)
query_lower = query.lower()
exact_results = [m for m in memories if query_lower in (m.get("text", "").lower())]
if hasattr(_memory_manager, 'get_relevant_memories'):
results = _memory_manager.get_relevant_memories(query, memories, threshold=0.05, max_items=20)
vector_results = _memory_manager.get_relevant_memories(query, memories, threshold=0.05, max_items=20)
else:
# Fallback: simple text search
query_lower = query.lower()
results = [m for m in memories if query_lower in m.get("text", "").lower()][:20]
vector_results = []
seen = set()
results = []
for m in [*exact_results, *vector_results]:
mid = m.get("id")
if mid in seen:
continue
seen.add(mid)
results.append(m)
if len(results) >= 20:
break
if not results:
return {"results": f"No memories found matching '{query}'."}
+311 -52
View File
@@ -7,6 +7,7 @@ scheduler without needing an LLM call.
import logging
import os
import json
from datetime import datetime
from typing import Tuple
@@ -497,11 +498,48 @@ def _result_has_work(result: str | None) -> bool:
return True
def _result_is_config_error(result: str | None) -> bool:
if not isinstance(result, str):
return False
low = result.lower()
return (
"no model configured" in low
or "no model endpoint configured" in low
or "no llm endpoint available" in low
)
def _email_task_account_id(kwargs) -> str | None:
prompt = (kwargs.get("prompt") or "").strip()
if not prompt:
return None
try:
data = json.loads(prompt)
if isinstance(data, dict):
val = data.get("account_id") or data.get("email_account_id")
return str(val).strip() or None
except Exception:
pass
for line in prompt.splitlines():
if "=" not in line:
continue
key, val = line.split("=", 1)
if key.strip().lower() in {"account_id", "email_account_id"}:
return val.strip() or None
return None
async def action_summarize_emails(owner: str, **kwargs) -> Tuple[str, bool]:
"""Run one pass of email summary background processing."""
try:
from routes.email_pollers import _run_auto_summarize_once
result = await _run_auto_summarize_once(do_summary=True, do_reply=False)
result = await _run_auto_summarize_once(
do_summary=True,
do_reply=False,
account_id=_email_task_account_id(kwargs),
)
if _result_is_config_error(result):
return result, False
if not _result_has_work(result):
raise TaskNoop(f"summarize: {result or 'no new emails'}")
return result, True
@@ -517,9 +555,12 @@ async def action_draft_email_replies(owner: str, **kwargs) -> Tuple[str, bool]:
result = await _run_auto_summarize_once(
do_summary=False,
do_reply=True,
account_id=_email_task_account_id(kwargs),
days_back=7,
progress_cb=kwargs.get("progress_cb"),
)
if _result_is_config_error(result):
return result, False
if not _result_has_work(result):
raise TaskNoop(f"draft replies: {result or 'no new emails'}")
return result, True
@@ -761,19 +802,44 @@ async def action_extract_email_events(owner: str, **kwargs) -> Tuple[str, bool]:
import asyncio as _aio
try:
from routes.email_pollers import _run_auto_summarize_once
try:
# Hard wall-clock budget: 5 min total. Per-LLM call already has its own timeout.
result = await _aio.wait_for(
_run_auto_summarize_once(
do_summary=False, do_reply=False, do_calendar=True, days_back=3,
),
timeout=300,
account_id = _email_task_account_id(kwargs)
attempts = [
("3d window, 3 emails", 3, 3, 240),
("3d window, 2 emails", 3, 2, 150),
("1d window, 1 email", 1, 1, 90),
]
timed_out = []
last_result = ""
for label, days_back, max_process, timeout in attempts:
try:
result = await _aio.wait_for(
_run_auto_summarize_once(
do_summary=False,
do_reply=False,
do_calendar=True,
days_back=days_back,
account_id=account_id,
max_process=max_process,
),
timeout=timeout,
)
last_result = result or ""
if _result_is_config_error(result):
return f"{result} ({label})", False
if _result_has_work(result):
suffix = f"{label}" if not timed_out else f"{label}; retried after timeout"
return f"{result} ({suffix})", True
raise TaskNoop(f"email→calendar: {result or 'no new emails'} ({label})")
except _aio.TimeoutError:
timed_out.append(label)
logger.warning(f"email calendar extraction timed out for {label}; retrying smaller batch")
continue
if timed_out:
raise TaskNoop(
"email→calendar: calendar extraction timed out on smaller batches; "
"will retry on the next scheduled run"
)
if not _result_has_work(result):
raise TaskNoop(f"email→calendar: {result or 'no new emails'}")
return f"{result} (3d window)", True
except _aio.TimeoutError:
return "Email→calendar pass exceeded 5 min budget — try fewer emails or a faster model", False
raise TaskNoop(f"email→calendar: {last_result or 'no new emails'}")
except Exception as e:
logger.error(f"extract_email_events action failed: {e}")
return str(e), False
@@ -1499,13 +1565,15 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
CACHE_DIR.mkdir(parents=True, exist_ok=True)
STATE_PATH.parent.mkdir(parents=True, exist_ok=True)
AGE_CUTOFF = _dt.utcnow() - _td(days=7)
TRIAGE_VERSION = 3
TRIAGE_VERSION = 10
CATEGORY_TAGS = {
"newsletter", "marketing", "notification", "finance", "bills",
"receipt", "travel", "security", "shopping", "social", "work",
"personal", "calendar",
"bills", "receipt", "travel", "calendar", "action-needed",
}
VISIBLE_EMAIL_TAGS = CATEGORY_TAGS | {"urgent", "reply-soon"}
MANAGED_TAGS = VISIBLE_EMAIL_TAGS | {
"newsletter", "marketing", "notification", "finance", "security",
"shopping", "social", "work", "personal", "legal", "support", "promo",
}
MANAGED_TAGS = CATEGORY_TAGS | {"urgent", "reply-soon", "promo"}
# ── 1. Resolve LLM candidates (utility primary + utility fallbacks; fall
# through to default chat as a last resort).
@@ -1514,6 +1582,8 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
if not candidates:
return "No LLM endpoint available", False
target_account_id = _email_task_account_id(kwargs)
# ── 2. Enumerate enabled accounts. Match this task's owner AND fall
# back to the legacy "unowned account whose imap_user / from_address
# == this owner" pattern — same rule `_get_email_config` uses, so a
@@ -1526,6 +1596,8 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
unowned = _or(_EA.owner == None, _EA.owner == "") # noqa: E711
same_mailbox = _or(_EA.imap_user == owner, _EA.from_address == owner)
q = q.filter(_or(_EA.owner == owner, _and(unowned, same_mailbox)))
if target_account_id:
q = q.filter(_EA.id == target_account_id)
accounts = q.all()
finally:
db.close()
@@ -1534,12 +1606,95 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
urgency_prompt = settings.get("urgent_email_prompt", "")
per_uid_scores = {} # key = "<acc_id>:<uid>" → {"score": 0-3, "reason": "..."}
all_unread_keys = set() # for cache pruning
all_unread_keys = set()
llm_attempts = 0
saved_classifications = 0
failed_classifications = []
tag_write_details = []
scanned = 0
def _heuristic_email_verdict(item: dict) -> dict:
blob = (
f"{item.get('headers','')}\n{item.get('from','')}\n"
f"{item.get('subject','')}\n{item.get('body','')}"
).lower()
response_tags = []
type_candidates = []
def add_response(tag: str):
if tag in CATEGORY_TAGS and tag not in response_tags:
response_tags.append(tag)
def add_type(tag: str):
if tag in CATEGORY_TAGS and tag not in type_candidates:
type_candidates.append(tag)
bulkish = bool(_re.search(
r"\b(list-unsubscribe|list-id|mailchimp|mailchimpapp|view this email in your browser|unsubscribe|newsletter|digest|precedence:\s*bulk)\b",
blob,
))
marketingish = bool(_re.search(
r"\b(advertisement|sponsored|promo|promotion|sale|discount|offer|limited time|deal|coupon|shop now|buy now|membership|rewards?)\b",
blob,
))
if bulkish or marketingish:
add_type("newsletter")
if _re.search(r"\b(receipt|order|注文|payment confirmation|delivery|shipment|tracking|お届け|購入)\b", blob):
add_type("receipt")
if _re.search(r"\b(bill|billing|amount due|overdue|pay by|payment due|subscription could not be renewed)\b", blob):
add_type("bills")
if _re.search(r"\b(court|charge|legal|lawyer|solicitor|claim|judgment|registration fee|debt)\b", blob):
add_type("legal")
if _re.search(r"\b(flight|hotel|booking|reservation|itinerary|train|ticket|trip|旅|予約)\b", blob):
add_type("travel")
if _re.search(r"\b(ticket|case|support|helpdesk|request)\b", blob):
add_type("support")
if _re.search(r"\b(meeting|appointment|calendar|invite|event|schedule|予定|保育園|連絡帳)\b", blob):
add_response("calendar")
if _re.search(
r"\b(action required|required action|please reply|please respond|deadline|by \d{1,2} |pay within|submit|sign|confirm|approval|waiting outside|locked out|can't get in|cannot get in|invoice|bill|billing|payment|balance|debt|subscription|renewal|overdue|amount due|court|charge|legal|lawyer|solicitor|claim|judgment)\b",
blob,
):
add_response("action-needed")
type_priority = ("bills", "receipt", "travel")
tags = [*response_tags]
for type_tag in type_priority:
if type_tag in type_candidates and type_tag not in tags:
tags.append(type_tag)
if len(tags) >= len(response_tags) + 2:
break
score = 0
reason = "categorized by email metadata"
if "action-needed" in response_tags:
score = 2
reason = "action likely needed"
if _re.search(r"\b(urgent|immediately|final notice|locked out|waiting outside|can't get in|cannot get in)\b", blob):
score = 3
reason = "urgent wording"
if (bulkish or marketingish) and score < 2:
score = 0
reason = "bulk marketing/newsletter"
_from_raw = item.get("from", "") or ""
if "<" in _from_raw:
_from_short = _from_raw.split("<", 1)[0].strip().strip('"') or _from_raw
else:
_from_short = _from_raw
return {
"score": max(0, min(3, score)),
"tags": tags[:4],
"spam": False,
"reason": reason,
"subject": (item.get("subject") or "")[:200],
"from": _from_short[:120],
"triage_version": TRIAGE_VERSION,
"message_id": (item.get("message_id") or "").strip(),
"unread": bool(item.get("unread")),
"ts": _time.time(),
}
# ── 3. Per-account scan: pull headers + lightweight body for new UIDs
# since 7 days ago, score via LLM, cache the verdict.
for acc in accounts:
@@ -1555,13 +1710,13 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
conn = _imap_connect(account.id)
try:
conn.select("INBOX", readonly=True)
# IMAP date is the only practical pre-filter — UNSEEN AND
# SINCE 7-days-ago. Date format is DD-Mon-YYYY.
# Tag recent inbox mail, not only unread mail. Urgency
# reminders below still only notify for unread messages.
since_str = AGE_CUTOFF.strftime("%d-%b-%Y")
status, data = conn.search(None, f'(UNSEEN SINCE {since_str})')
status, data = conn.uid("SEARCH", None, f'(SINCE {since_str})')
if status != "OK" or not data or not data[0]:
return results
uids = data[0].split()
uids = data[0].split()[-30:]
for uid_b in uids:
uid = uid_b.decode() if isinstance(uid_b, bytes) else str(uid_b)
key = f"{account.id}:{uid}"
@@ -1573,9 +1728,14 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
continue
# Pull headers + first ~800 chars of plaintext body.
try:
st, msg_data = conn.fetch(uid_b, "(RFC822.HEADER BODY.PEEK[TEXT]<0.800>)")
st, msg_data = conn.uid("FETCH", uid_b, "(UID FLAGS RFC822.HEADER BODY.PEEK[TEXT]<0.800>)")
if st != "OK" or not msg_data:
continue
flags_blob = b" ".join(
part[0] for part in msg_data
if isinstance(part, tuple) and part and isinstance(part[0], (bytes, bytearray))
)
is_unread = b"\\Seen" not in flags_blob
# Headers + body land in different tuples in the
# response — concatenate the bytes for parsing.
raw = b""
@@ -1635,6 +1795,7 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
"headers": header_blob,
"body": body_snippet.strip(),
"message_id": (msg.get("Message-ID") or "").strip(),
"unread": is_unread,
})
except Exception as _fe:
logger.debug(f"urgency: header fetch for uid {uid} failed: {_fe}")
@@ -1652,25 +1813,33 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
for item in items:
scanned += 1
key = item["key"]
all_unread_keys.add(key)
if item.get("unread"):
all_unread_keys.add(key)
if item.get("cached"):
per_uid_scores[key] = item["cached"]
cached_v = dict(item["cached"])
cached_v["unread"] = bool(item.get("unread"))
per_uid_scores[key] = cached_v
continue
# Skip uids we couldn't fetch (no subject/from/body).
if not item.get("subject") and not item.get("from"):
continue
verdict = _heuristic_email_verdict(item)
cache.setdefault("uids", {})[item["uid"]] = verdict
per_uid_scores[key] = verdict
saved_classifications += 1
continue
# ── LLM-classify. JSON-only response; bullet-proof parse.
llm_attempts += 1
prompt = (
"You are triaging ONE unread email. Return ONLY JSON: "
"You are triaging ONE email. Return ONLY JSON: "
"{\"score\":0|1|2|3,\"tags\":[\"...\"],\"spam\":false,"
"\"reason\":\"one short phrase\"}.\n"
"0 = trivial / promotional · 1 = informational, no reply needed · "
"2 = should reply within a day · 3 = urgent, reply now (deadline, blocker).\n\n"
"Allowed tags: newsletter, marketing, notification, finance, bills, receipt, "
"travel, security, shopping, social, work, personal, calendar.\n"
"Use marketing for ads, promos, sales, offers, and cold sales. Use newsletter "
"for newsletters, digests, and recurring content. spam=true for scams, phishing, "
"Allowed visible tags: urgent, reply-soon, action-needed, calendar, bills, receipt, travel.\n"
"Use action-needed when the user likely needs to reply, pay, sign, book, or decide. "
"Use bills for bills or debts, receipt for purchases/deliveries, travel for reservations/trips, "
"and calendar only when a calendar event/reminder is involved. spam=true for scams, phishing, "
"junk, cold sales, generic ads, or no-personal-action bulk mail.\n"
"Important: 'I'm outside', 'I am outside', 'waiting outside', 'at the door', "
"'locked out', or 'can't get in' means score 3 unless clearly historical.\n\n"
@@ -1739,14 +1908,10 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
r"\b(advertisement|sponsored|promo|promotion|sale|discount|offer|limited time|deal|tickets?|tour|merch|stream|purchase|sold out|low tickets|coupon|shop now|buy now)\b",
_blob,
))
if "newsletter" not in tags and bulkish:
tags.append("newsletter")
if "marketing" not in tags and marketingish:
tags.append("marketing")
if (bulkish or marketingish) and score < 2:
score = 0
if not reason or "urgent" in reason.lower():
reason = "Bulk marketing/newsletter; no personal reply needed"
reason = "bulk mail; no personal reply needed"
# Strip "Name <addr>" to bare display name for compact summary.
_from_raw = item.get("from", "") or ""
if "<" in _from_raw:
@@ -1764,6 +1929,7 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
# Cache the message_id too so re-scans of already-cached
# UIDs can still write the inbox tag without re-LLM'ing.
"message_id": (item.get("message_id") or "").strip(),
"unread": bool(item.get("unread")),
"ts": _time.time(),
}
cache.setdefault("uids", {})[item["uid"]] = verdict
@@ -1778,9 +1944,9 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
logger.debug(f"urgency: LLM classify failed for {key}: {e}")
continue
# ── Prune cache entries for UIDs that are no longer unread (replied
# / archived / deleted). Compare against `items` (everything UNSEEN
# in this scan window).
# ── Prune cache entries for UIDs that are no longer in the recent
# scan window. Read messages remain cached because tags are useful
# on read mail too; unread state is refreshed per scan above.
seen_uids = {it["uid"] for it in items}
cache_uids = cache.get("uids", {})
for stale in [u for u in cache_uids if u not in seen_uids]:
@@ -1815,15 +1981,17 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
_tag = str(_tag).strip().lower().replace("_", "-")
if _tag == "promo":
_tag = "marketing"
if _tag in CATEGORY_TAGS and _tag not in _new_tags:
if _tag == "action-needed" and any(t in _new_tags for t in ("urgent", "reply-soon")):
continue
if _tag in VISIBLE_EMAIL_TAGS and _tag not in _new_tags:
_new_tags.append(_tag)
_spam = 1 if _v.get("spam") else 0
# _key is "<account_id>:<uid>" — extract uid for the row.
_uid_only = _key.split(":", 1)[-1]
_acc_id, _uid_only = (_key.split(":", 1) + [""])[:2]
_owner_key = owner or ""
_row = _conn.execute(
"SELECT tags FROM email_tags WHERE message_id=? AND owner=?",
(_msg_id, _owner_key),
"SELECT tags FROM email_tags WHERE message_id=? AND owner=? AND account_id=?",
(_msg_id, _owner_key, _acc_id),
).fetchone()
if _row:
try:
@@ -1842,23 +2010,42 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
for _tag in _new_tags:
if _tag not in _existing:
_existing.append(_tag)
if _new_tags or _spam:
tag_write_details.append({
"uid": _uid_only,
"subject": _v.get("subject", ""),
"from": _v.get("from", ""),
"tags": list(_new_tags),
"spam": _spam,
"reason": _v.get("reason", ""),
"updated": True,
})
_conn.execute(
"UPDATE email_tags SET tags=?, spam_verdict=?, spam_reason=?, uid=?, folder=?, subject=?, sender=? "
"WHERE message_id=? AND owner=?",
"WHERE message_id=? AND owner=? AND account_id=?",
(_json.dumps(_existing), _spam, _v.get("reason", ""), _uid_only, "INBOX",
_v.get("subject", ""), _v.get("from", ""), _msg_id, _owner_key),
_v.get("subject", ""), _v.get("from", ""), _msg_id, _owner_key, _acc_id),
)
else:
if not _new_tags and not _spam:
continue
_conn.execute(
"INSERT INTO email_tags "
"(message_id, owner, uid, folder, subject, sender, tags, spam_verdict, spam_reason, created_at) "
"VALUES (?, ?, ?, 'INBOX', ?, ?, ?, ?, ?, ?)",
(_msg_id, _owner_key, _uid_only, _v.get("subject", ""),
"(message_id, owner, account_id, uid, folder, subject, sender, tags, spam_verdict, spam_reason, created_at) "
"VALUES (?, ?, ?, ?, 'INBOX', ?, ?, ?, ?, ?, ?)",
(_msg_id, _owner_key, _acc_id, _uid_only, _v.get("subject", ""),
_v.get("from", ""), _json.dumps(_new_tags), _spam, _v.get("reason", ""),
_dt2.utcnow().isoformat()),
)
tag_write_details.append({
"uid": _uid_only,
"subject": _v.get("subject", ""),
"from": _v.get("from", ""),
"tags": list(_new_tags),
"spam": _spam,
"reason": _v.get("reason", ""),
"updated": False,
})
_conn.commit()
finally:
_conn.close()
@@ -1866,7 +2053,7 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
logger.warning(f"urgency: bulk tag write failed: {_te}")
# ── 4. Aggregate state. urgent = score ≥ 2.
urgent_keys = [k for k, v in per_uid_scores.items() if v.get("score", 0) >= 2]
urgent_keys = [k for k, v in per_uid_scores.items() if v.get("score", 0) >= 2 and v.get("unread")]
max_score = max((v.get("score", 0) for v in per_uid_scores.values()), default=0)
total_urgent = len(urgent_keys)
@@ -1975,13 +2162,28 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
f"reply-soon {tier_counts[2]} · info {tier_counts[1]} · trivial {tier_counts[0]} · "
f"{saved_classifications} saved classifications"
)
if llm_attempts != saved_classifications:
head += f" · {llm_attempts - saved_classifications} failed"
if failed_classifications:
head += f" · {len(failed_classifications)} failed"
if newly_notified:
head += f" · notified {len(newly_notified)}"
if notify_failed:
head += f" · notify failed {len(notify_failed)}"
def _fmt_tag_write(v):
subj = (v.get("subject") or "(no subject)")[:80]
frm = v.get("from") or ""
tags = list(v.get("tags") or [])
if v.get("spam"):
tags.append("spam")
tag_txt = ", ".join(tags) if tags else "cleared managed tags"
why = v.get("reason") or ""
op = "updated" if v.get("updated") else "created"
line = f"- **{subj}**" + (f" — _{frm}_" if frm else "")
line += f" — `{tag_txt}` ({op})"
if why:
line += f" · {why}"
return line
def _fmt_one(v, newly_notified_set, failed_set, key):
subj = (v.get("subject") or "(no subject)")[:80]
frm = v.get("from") or ""
@@ -1997,6 +2199,13 @@ async def action_check_email_urgency(owner: str, **kwargs) -> Tuple[str, bool]:
for k, v in per_uid_scores.items():
by_tier.setdefault(v.get("score", 0), []).append((k, v))
lines = [head]
if tag_write_details:
lines.append("")
lines.append(f"**Applied tags ({len(tag_write_details)}):**")
for v in tag_write_details[:16]:
lines.append(_fmt_tag_write(v))
if len(tag_write_details) > 16:
lines.append(f"…and {len(tag_write_details) - 16} more")
tier_labels = {3: "Urgent", 2: "Reply soon", 1: "Informational", 0: "Trivial"}
for tier in (3, 2, 1, 0):
items_t = by_tier.get(tier, [])
@@ -2068,6 +2277,7 @@ async def action_cookbook_serve(
end_after_min = int(cfg.get("end_after_min") or 0)
except Exception:
end_after_min = 0
set_default = bool(cfg.get("set_default", True))
state_path = Path(COOKBOOK_STATE_FILE)
try:
@@ -2154,6 +2364,51 @@ async def action_cookbook_serve(
return f"Launch rejected: {data.get('error') or data.get('detail') or 'unknown'}", False
sid = data.get("session_id") or ""
endpoint_id = data.get("endpoint_id") or ""
# Scheduled serves are usually meant to become the active local model for
# chat/tools while their time window is open. Persist both endpoint and
# model so task/utility/default resolution does not keep routing to a stale
# API fallback. Allow explicit opt-out with {"set_default": false}.
if endpoint_id and set_default:
try:
selected_model = repo_id
try:
from core.database import SessionLocal as _SL, ModelEndpoint as _ME
_db = _SL()
try:
_ep = _db.query(_ME).filter(_ME.id == endpoint_id).first()
if _ep and _ep.cached_models:
_models = json.loads(_ep.cached_models or "[]")
if isinstance(_models, list) and _models:
selected_model = str(_models[0])
finally:
_db.close()
except Exception:
pass
from src.settings import load_settings as _load_settings, save_settings as _save_settings
_settings = _load_settings()
_settings["default_endpoint_id"] = endpoint_id
_settings["default_model"] = selected_model
# Keep background tasks aligned unless the user explicitly chose a
# separate task model.
if not (_settings.get("task_endpoint_id") or "").strip():
_settings["task_endpoint_id"] = endpoint_id
_settings["task_model"] = selected_model
if not (_settings.get("utility_endpoint_id") or "").strip():
_settings["utility_endpoint_id"] = endpoint_id
_settings["utility_model"] = selected_model
_save_settings(_settings)
if owner:
from routes.prefs_routes import _load_for_user, _save_for_user
_prefs = _load_for_user(owner)
_prefs["default_endpoint_id"] = endpoint_id
_prefs["default_model"] = selected_model
if not (_prefs.get("utility_endpoint_id") or "").strip():
_prefs["utility_endpoint_id"] = endpoint_id
_prefs["utility_model"] = selected_model
_save_for_user(owner, _prefs)
except Exception as e:
logger.warning(f"cookbook_serve: default endpoint update failed: {e}")
# Register the new task in cookbook_state.json + stamp it with our
# scheduler-owner markers. /api/model/serve spawns the tmux session
# but leaves the state-write to the UI — when a scheduled action
@@ -2195,12 +2450,16 @@ async def action_cookbook_serve(
"sshPort": "",
"platform": "linux",
"_serveReady": False,
"_endpointAdded": False,
"_endpointAdded": bool(endpoint_id),
}
tasks.append(existing)
# Stamp ownership + end-at on the task entry.
existing["_scheduledByTask"] = task_name or ""
existing["_scheduledByOwner"] = owner or ""
if endpoint_id:
existing["_endpointId"] = endpoint_id
existing["endpointId"] = endpoint_id
existing["_endpointAdded"] = True
if end_after_min > 0:
existing["_scheduledStopAtMs"] = int(_time.time() * 1000) + end_after_min * 60 * 1000
fresh["tasks"] = tasks
+11 -7
View File
@@ -37,6 +37,13 @@ async def _delete_endpoint_for_task(task: dict) -> None:
the picker (probe goes offline; chats still try to route there) and
the user has to delete it by hand in Settings -> Endpoints.
"""
endpoint_id = (task.get("_endpointId") or task.get("endpointId") or "").strip()
if not endpoint_id:
logger.info(
"cookbook_serve_lifecycle: task %s has no endpoint id; skipping endpoint deletion",
task.get("sessionId") or task.get("id") or "",
)
return
import re as _re
payload = task.get("payload") or {}
cmd = str(payload.get("_cmd") or "")
@@ -66,13 +73,10 @@ async def _delete_endpoint_for_task(task: dict) -> None:
if r.status_code >= 400:
return
eps = r.json() if r.content else []
# Prefer exact URL match; fall back to host:port substring so we
# still catch the case where 0.0.0.0 vs the registered host
# representation diverged.
ep = next((e for e in eps if e.get("base_url") == base_url), None)
if not ep:
hostport = f"{host}:{port}"
ep = next((e for e in eps if hostport in (e.get("base_url") or "")), None)
# Delete only the endpoint created by this scheduled serve. URL
# matching is unsafe because a later scheduled serve can reuse the
# same host:port after an older task has gone stale.
ep = next((e for e in eps if e.get("id") == endpoint_id), None)
if ep:
await client.delete(
f"{internal_api_base()}/api/model-endpoints/{ep['id']}",
+10
View File
@@ -77,10 +77,20 @@ async def run_document_tidy(owner: str) -> str:
deleted = 0
kept = 0
survivors = [] # docs that pass the junk rules, considered for dedup
now = datetime.utcnow()
for doc in docs:
content = (doc.current_content or "").strip()
title = (doc.title or "").strip().lower()
created = doc.created_at
is_fresh_empty = (
not content
and created is not None
and (now - created).total_seconds() < 1800
)
if is_fresh_empty:
survivors.append(doc)
continue
# Strip markdown noise to get "real" character count
stripped = re.sub(r"^#{1,6}\s+", "", content, flags=re.MULTILINE) # headers
+2
View File
@@ -207,6 +207,7 @@ def _search_like(
)
if not include_archived:
q = q.filter(DBSession.archived == False)
q = q.filter(~DBSession.name.like("SFT trace batch%"))
if restrict_owner:
q = _owner_filter(q, owner, include_legacy_owner)
rows = q.order_by(DBChatMessage.timestamp.desc()).limit(limit).all()
@@ -270,6 +271,7 @@ def _search_fts(
WHERE chat_messages_fts MATCH :fts_query
{archived_clause}
{owner_clause}
AND s.name NOT LIKE 'SFT trace batch%'
AND m.role IN ('user', 'assistant')
ORDER BY bm25(chat_messages_fts), m.timestamp DESC
LIMIT :limit
+2
View File
@@ -1083,6 +1083,8 @@ class TaskScheduler:
self._set_run_progress(run_id, message)
kwargs = {"owner": task.owner, "task_name": task.name, "progress_cb": _progress}
if task.prompt:
kwargs["prompt"] = task.prompt
if task.action in ("run_script", "run_local", "ssh_command") and task.prompt:
kwargs["script" if task.action in ("run_script", "run_local") else "command"] = task.prompt
# cookbook_serve carries its JSON config in task.prompt — feed it
+106 -10
View File
@@ -116,7 +116,7 @@ async def do_search_chats(query: str, limit: int = 20, owner: str | None = None)
try:
from src.session_search import search_session_messages
results = search_session_messages(query, limit=limit, owner=owner)
results = search_session_messages(query, limit=limit, owner=owner, context_messages=3)
if not results:
return {"results": f"No chats found matching \"{query}\"."}
@@ -130,13 +130,14 @@ async def do_search_chats(query: str, limit: int = 20, owner: str | None = None)
for sid, result in seen_sessions.items():
lines.append(f"- **{result.session_name}** (#{sid})")
lines.append(f" Link: [Open chat](#{sid})")
lines.append(f" Match ({result.role}): {result.content_snippet}")
match_text = (result.content or result.content_snippet or "")[:600]
lines.append(f" Match ({result.role}): {match_text}")
if result.context_before:
before = result.context_before[-1]
lines.append(f" Before ({before['role']}): {before['content'][:180]}")
for before in result.context_before:
lines.append(f" Before ({before['role']}): {before['content'][:500]}")
if result.context_after:
after = result.context_after[0]
lines.append(f" After ({after['role']}): {after['content'][:180]}")
for after in result.context_after:
lines.append(f" After ({after['role']}): {after['content'][:500]}")
lines.append("")
return {"results": "\n".join(lines)}
@@ -1399,6 +1400,9 @@ async def do_manage_notes(content: str, owner: Optional[str] = None) -> Dict:
"new": "add",
"save": "add",
"remind": "add",
"get": "view",
"read": "view",
"open": "view",
"remove": "delete",
"remove_item": "toggle_item",
}
@@ -1458,6 +1462,32 @@ async def do_manage_notes(content: str, owner: Optional[str] = None) -> Dict:
lines.append(f" {snippet}")
return {"results": "\n".join(lines)}
elif action == "view":
note_id = args.get("id", "")
note = _note_by_prefix(note_id)
if not note:
return {"error": f"Note '{note_id}' not found", "exit_code": 1}
if not _note_visible_to_owner(note, owner):
return {"error": "Note not found", "exit_code": 1}
lines = [f"Note: {note.title or '(untitled)'}", f"id: {note.id}"]
if note.label:
lines.append(f"label: {note.label}")
if note.due_date:
lines.append(f"due_date: {note.due_date}")
if note.note_type == "checklist":
lines.append("items:")
try:
items = json.loads(note.items or "[]")
except (json.JSONDecodeError, TypeError):
items = []
for i, item in enumerate(items):
mark = "x" if item.get("done") else " "
lines.append(f"- [{mark}] {i}: {item.get('text', '')}")
elif note.content:
lines.append("content:")
lines.append(note.content)
return {"response": "\n".join(lines), "note_id": note.id, "exit_code": 0}
elif action == "add":
# Accept the various field names models emit: `text` is the most
# common stand-in for "title or body content" when the model
@@ -1612,7 +1642,7 @@ async def do_manage_notes(content: str, owner: Optional[str] = None) -> Dict:
return {"response": f"Item '{items[index].get('text', '')}' marked {mark}", "exit_code": 0}
else:
return {"error": f"Unknown action: {action}. Use list/add/update/delete/toggle_item", "exit_code": 1}
return {"error": f"Unknown action: {action}. Use list/view/add/update/delete/toggle_item", "exit_code": 1}
except Exception as e:
logger.error(f"manage_notes error: {e}")
return {"error": str(e), "exit_code": 1}
@@ -3772,6 +3802,63 @@ async def do_list_cached_models(content: str, owner: Optional[str] = None) -> Di
seen.add(key)
models.append(m)
if not models:
endpoint_models = []
try:
from core.database import SessionLocal, ModelEndpoint
db = SessionLocal()
try:
query = db.query(ModelEndpoint).filter(ModelEndpoint.is_enabled == True) # noqa: E712
if owner:
query = query.filter((ModelEndpoint.owner == owner) | (ModelEndpoint.owner == None)) # noqa: E711
else:
query = query.filter(ModelEndpoint.owner == None) # noqa: E711
for ep in query.all():
raw_models = json.loads(ep.cached_models or "[]") if ep.cached_models else []
for model_id in raw_models or []:
if not model_id:
continue
model_name = str(model_id)
lower_name = model_name.lower()
quantization = "GGUF" if "gguf" in lower_name else "unknown"
endpoint_models.append(
{
"repo_id": model_name,
"host": ep.name or "endpoint",
"source": "model_endpoint_cache",
"file_size": "unknown",
"quantization": quantization,
}
)
finally:
db.close()
except Exception as e:
logger.debug(f"list_cached_models endpoint fallback failed: {e}")
endpoint_models = []
if endpoint_models:
seen_endpoint_models = set()
deduped_endpoint_models = []
for model in endpoint_models:
key = (model.get("host"), model.get("repo_id"))
if key in seen_endpoint_models:
continue
seen_endpoint_models.add(key)
deduped_endpoint_models.append(model)
lines = [
f"{len(deduped_endpoint_models)} cached model(s) from configured endpoint cache:"
]
for model in deduped_endpoint_models:
lines.append(
f"- {model['repo_id']}{model.get('host', 'endpoint')}; "
f"size: {model.get('file_size', 'unknown')}; "
f"quantization: {model.get('quantization', 'unknown')}"
)
lines.append(
"Endpoint-cache rows are authoritative for configured models; "
"file sizes may be unknown unless the model directory is scanned."
)
return {"output": "\n".join(lines), "models": deduped_endpoint_models, "exit_code": 0}
# Cache scans can miss models downloaded into the HF default cache
# when the server has no explicit model_dir configured. Surface
# completed Cookbook download tasks so the agent doesn't conclude
@@ -4020,7 +4107,11 @@ async def do_resolve_contact(content: str, owner: Optional[str] = None) -> Dict:
for email in (c.get("emails") or []):
email = (email or "").strip().lower()
if email and "@" in email:
contacts[email] = {"name": c.get("name") or email, "source": "contacts"}
contacts[email] = {
"name": c.get("name") or email,
"source": "contacts",
"phones": [p.strip() for p in (c.get("phones") or []) if p and p.strip()],
}
has_email = True
# Fall back to phone numbers when the contact has no email address
if not has_email:
@@ -4051,7 +4142,10 @@ async def do_resolve_contact(content: str, owner: Optional[str] = None) -> Dict:
if info.get("phone"):
lines.append(f"- {info['name']} — phone: {info['phone']} ({info['source']})")
else:
lines.append(f"- {info['name']} <{key}> ({info['source']})")
phone_text = ""
if info.get("phones"):
phone_text = f" — phone: {', '.join(info['phones'])}"
lines.append(f"- {info['name']} <{key}>{phone_text} ({info['source']})")
return {"output": "\n".join(lines), "exit_code": 0}
@@ -4081,7 +4175,9 @@ async def do_manage_contact(content: str, owner: Optional[str] = None) -> Dict:
lines = [f"{len(rows)} contacts:"]
for c in rows:
em = ", ".join(c.get("emails") or [])
lines.append(f"- {c.get('name') or '(no name)'} <{em}> [uid={c.get('uid','')}]")
phones = ", ".join(c.get("phones") or [])
phone_text = f" phones: {phones}" if phones else ""
lines.append(f"- {c.get('name') or '(no name)'} <{em}>{phone_text} [uid={c.get('uid','')}]")
return {"output": "\n".join(lines), "exit_code": 0}
if action == "add":
+2 -2
View File
@@ -105,12 +105,12 @@ BUILTIN_TOOL_DESCRIPTIONS: Dict[str, str] = {
"search_chats": "Search past session transcripts across chats.",
"ask_user": "Ask the user a multiple-choice question to get a decision or clarification. Use this when the task is genuinely ambiguous and the answer changes what you do next — pick between approaches, confirm an assumption, choose among options — instead of guessing. Provide a clear `question` and 2-6 `options` (each with a short `label`, optional `description`). Calling this ENDS your turn: the user sees clickable buttons and their choice arrives as your next message. Don't use it for things you can decide from context or sensible defaults, or for irreversible-action confirmation if a dedicated flow exists.",
"update_plan": "Write back to the ACTIVE PLAN while executing an approved plan: mark steps done or revise them. After finishing a step call this with the full checklist and that step marked done; when the user asks to change the plan call it with the revised checklist. Always pass the COMPLETE markdown checklist (`- [ ]` / `- [x]`), not a diff. The user's docked plan window updates live. No effect when there is no active plan.",
"ui_control": "Control the UI and toggle tools on/off. Use this to turn off / turn on / disable / enable individual tools and features: shell (bash), search (web), research, browser, documents, incognito. Open panels (documents library, gallery, email inbox, sessions, notes, memories/brain, skills, settings, cookbook) via `open_panel <name>`. Use `open_email_reply <uid> <folder> reply` to open an email reply draft document without sending. To pre-fill the reply body in one shot (USE THIS whenever the user told you what to say — opening an empty draft when they asked you to write is wrong), append the body after the mode: `open_email_reply <uid> <folder> reply <body text>`. Body can continue on subsequent lines for multi-line replies. Also switches between chat/agent modes, changes the current model, and applies/creates themes.",
"ui_control": "Control the UI and toggle tools on/off. Use this to turn off / turn on / disable / enable individual tools and features: shell (bash), search (web), research, browser, documents, incognito. Open panels (documents library, gallery, email inbox, sessions, notes, memories/brain, skills, settings, cookbook) via `open_panel <name>`. Use `open_email_reply <uid> <folder> reply <body text>` (or structured body) to open an email reply draft document without sending. USE THIS whenever the user says to write/draft a reply or tells you what to say — opening an empty draft or sending immediately is wrong. Body can continue on subsequent lines for multi-line replies. Also switches between chat/agent modes, changes the current model, and applies/creates themes.",
"list_email_accounts": "List configured email accounts and default status. Use before reading or sending mail when the user mentions Gmail, work mail, custom domain mail, another mailbox, or asks to compare/check multiple inboxes.",
"list_emails": "List emails for a folder/account, newest first, including read messages by default. Shows subject, sender, date, UID, account, and AI summary. Check inbox, find emails needing replies. Supports account from list_email_accounts for Gmail/work/custom mailboxes. For last/latest/newest email, use max_results=1 and unread_only=false.",
"read_email": "Read the full content of a specific email by UID or Message-ID. View email body, check details. Supports account from list_email_accounts when the UID belongs to a non-default mailbox.",
"send_email": "Send a new email via SMTP. Provide recipient, subject, body, and optional account from list_email_accounts. For replying to a thread use reply_to_email instead.",
"reply_to_email": "SEND a reply email immediately by UID. Do not use for open/start reply draft requests; use ui_control open_email_reply for those. For follow-up 'reply ...' send requests, use the exact UID and account from latest read_email/list_emails output; never invent UID 1. Threads automatically with In-Reply-To/References, prefixes Re:, marks original as Answered.",
"reply_to_email": "SEND a reply email immediately by UID. Do not use for write/draft/open/start reply requests; use ui_control open_email_reply with body so the user can review. Only use when the user explicitly says to send now. For send requests, use the exact UID and account from latest read_email/list_emails output; never invent UID 1. Threads automatically with In-Reply-To/References, prefixes Re:, marks original as Answered.",
"archive_email": "Move an email out of the inbox into the Archive folder. Use after handling messages you want to keep but get out of the way.",
"delete_email": "Delete an email — moves to Trash by default, or expunges permanently with permanent=true.",
"mark_email_read": "Mark an email as read or unread by toggling the \\Seen flag.",
+9 -5
View File
@@ -325,7 +325,7 @@ FUNCTION_TOOL_SCHEMAS = [
"type": "function",
"function": {
"name": "send_to_session",
"description": "Send a message to an existing chat and get the model's response. The chat keeps its conversation history.",
"description": "Send a new message to an existing live chat and get that chat model's response. Do not use this to retrieve, read, summarize, or inspect old chats; use search_chats or list_sessions for past chat evidence.",
"parameters": {
"type": "object",
"properties": {
@@ -415,7 +415,7 @@ FUNCTION_TOOL_SCHEMAS = [
"type": "function",
"function": {
"name": "ui_control",
"description": "Control the user interface. Actions: toggle (turn tools on/off), open_panel (open a modal: documents/library, gallery, email, sessions, notes, memories/brain, skills, settings, cookbook), open_email_reply (open an email reply draft document; does NOT send), set_mode, switch_model, set_theme (built-in presets: dark, light, midnight, paper, cyberpunk, retrowave, forest, ocean, ume, copper, terminal, organs, lavender, gpt, claude, cute), create_theme (CREATE any custom theme with a name + colors object — pick distinctive, evocative hex colors that match the requested aesthetic, NOT generic defaults. The theme auto-applies after creation). When a user asks for ANY theme not in the built-in preset list, ALWAYS use create_theme.",
"description": "Control the user interface. Actions: toggle (turn tools on/off), open_panel (open a modal: documents/library, gallery, email, sessions, notes, memories/brain, skills, settings, cookbook), open_email_reply (open an email reply draft document; DOES NOT send. For 'write/draft a reply saying X', include body with the drafted reply), set_mode, switch_model, set_theme (built-in presets: dark, light, midnight, paper, cyberpunk, retrowave, forest, ocean, ume, copper, terminal, organs, lavender, gpt, claude, cute), create_theme (CREATE any custom theme with a name + colors object — pick distinctive, evocative hex colors that match the requested aesthetic, NOT generic defaults. The theme auto-applies after creation). When a user asks for ANY theme not in the built-in preset list, ALWAYS use create_theme.",
"parameters": {
"type": "object",
"properties": {
@@ -426,6 +426,7 @@ FUNCTION_TOOL_SCHEMAS = [
"uid": {"type": "string", "description": "Email UID for open_email_reply"},
"folder": {"type": "string", "description": "Email folder for open_email_reply (default INBOX)"},
"mode": {"type": "string", "description": "Reply draft mode for open_email_reply: reply, reply-all, or ai-reply"},
"body": {"type": "string", "description": "For open_email_reply: reply body to pre-fill. Required whenever the user told you what the reply should say. Opens a draft, does not send."},
"colors": {"type": "object", "description": "For create_theme: the theme colors",
"properties": {
"bg": {"type": "string", "description": "Background color (hex, e.g. #1a1a2e)"},
@@ -569,12 +570,12 @@ FUNCTION_TOOL_SCHEMAS = [
"type": "function",
"function": {
"name": "manage_notes",
"description": "Manage notes and checklists (Google Keep-style): list, add, update, delete, toggle_item. IMPORTANT: For to-do lists / checklists, set note_type='checklist' and pass the items as the `checklist_items` array — do NOT serialize them into `content` as plain text. For freeform notes, use note_type='note' and put the body in `content`. `due_date` accepts natural language like 'tomorrow at 9am' (parsed in the user's timezone) and fires a notification — do not also create a calendar event for the same reminder.",
"description": "Manage notes and checklists (Google Keep-style): list, view, add, update, delete, toggle_item. Use list/search to find candidate notes, then view with the note id when you need the full body. IMPORTANT: For to-do lists / checklists, set note_type='checklist' and pass the items as the `checklist_items` array — do NOT serialize them into `content` as plain text. For freeform notes, use note_type='note' and put the body in `content`. `due_date` accepts natural language like 'tomorrow at 9am' (parsed in the user's timezone) and fires a notification — do not also create a calendar event for the same reminder.",
"parameters": {
"type": "object",
"properties": {
"action": {"type": "string",
"enum": ["list", "add", "update", "delete", "toggle_item"],
"enum": ["list", "view", "add", "update", "delete", "toggle_item"],
"description": "The action to perform"},
"id": {"type": "string", "description": "Note id (for update/delete/toggle_item); 8-char prefix is fine"},
"title": {"type": "string", "description": "Note title (for add/update)"},
@@ -1106,7 +1107,7 @@ FUNCTION_TOOL_SCHEMAS = [
"type": "function",
"function": {
"name": "reply_to_email",
"description": "SEND a reply email immediately by UID. Do not use this when the user asks to open/start a reply window or draft; use ui_control action=open_email_reply instead. For follow-up 'reply ...' requests where the user clearly wants to send now, use the exact UID from the latest read_email/list_emails result; never invent UID 1. Automatically threads with In-Reply-To/References headers.",
"description": "SEND a reply email immediately by UID. Do not use this when the user asks to write/draft/open/start a reply; use ui_control action=open_email_reply with body instead so the user can review. Only use when the user explicitly says to send now. Use the exact UID from the latest read_email/list_emails result; never invent UID 1. Automatically threads with In-Reply-To/References headers.",
"parameters": {
"type": "object",
"properties": {
@@ -1373,6 +1374,9 @@ def function_call_to_tool_block(name: str, arguments: str) -> Optional[ToolBlock
folder = args.get("folder") or value or "INBOX"
mode = args.get("mode") or "reply"
content = f"open_email_reply {uid} {folder} {mode}"
body = args.get("body") or args.get("extra") or args.get("content") or ""
if body:
content += f" {body}"
elif action == "set_mode":
content = f"set_mode {value or name}"
elif action == "switch_model":