Free, open-source AI video generator for Mac. Run LTX-2.5 and MiniMax H3 (Hailuo) locally on Apple Silicon: text-to-video and image-to-video with sound, lip-sync, character LoRA training, songs from lyrics (YuE2) and image editing (Qwen-Image-Edit). One-click install via Pinokio. Offline: no cloud, no API key, no NVIDIA GPU.
Python ai-filmmakingai-videoai-video-generatorapple-siliconhailuoimage-to-videolip-synclocal-ailora-trainingltx-2ltx-2-5ltx-videomacosminimax-h3mlxmusic-generationoffline-aipinokiotext-to-videovideo-generation
feat(agent): attachments, sidebar polish, project memory
mrbizarro130532483292
diff --git a/agent/project.py b/agent/project.pynew file mode 100644index 0000000..e03ca04--- /dev/null+++ b/agent/project.py@@ -0,0 +1,125 @@+"""Project-level persistent memory for the agent.++Sessions are short-lived chats. A "project" is the long-running thing —+the satirical SaaS spot the user is iterating on for a week, the wedding+recap they're rebuilding from a script, the character bible they keep+returning to. Across many sessions, the agent should remember:++ - the user's evolving direction ("we settled on documentary not+ cinematic", "the brother character has a moustache, NOT a beard")+ - prior decisions ("standard tier was too slow on 12-second shots,+ moved everything to balanced + Sharp 720p")+ - the running cast / anchor library ("the doctor's portrait lives at+ panel_uploads/doctor.png")++Mechanism: one markdown file at `<state_dir>/agent_project_notes.md`.+Entries are appended with a timestamp + author tag. The agent's system+prompt every turn includes the tail of this file (most recent ~6 KB),+so the agent can reference its past notes without re-reading them.++Two tools — `read_project_notes` and `append_project_notes` — let the+agent read the full file (when it needs to) and add new memory entries+when something durable was decided.++The notes file is plain markdown so the user can also read / edit it+manually. There is intentionally no schema.+"""++from __future__ import annotations++import os+import time+from datetime import datetime+from pathlib import Path+++NOTES_FILENAME = "agent_project_notes.md"+DEFAULT_PROMPT_EXCERPT_BYTES = 6 * 1024 # how much we surface in the system prompt+++def notes_path(state_dir: Path) -> Path:+ return state_dir / NOTES_FILENAME+++def read_notes(state_dir: Path) -> str:+ """Return the full notes file, or "" if it doesn't exist."""+ p = notes_path(state_dir)+ if not p.is_file():+ return ""+ try:+ return p.read_text(encoding="utf-8")+ except OSError:+ return ""+++def read_notes_excerpt(state_dir: Path,+ max_bytes: int = DEFAULT_PROMPT_EXCERPT_BYTES) -> str:+ """Return the tail of the notes file, capped at `max_bytes`.++ The tail is what the agent has most recently learned, so it's the+ highest-signal slice for the system prompt. We cut on a newline+ boundary in the FIRST half of the tail — that way a tail that ends+ in a long unbroken paragraph still surfaces (otherwise we might find+ only a trailing "\\n" and accidentally return an empty excerpt).+ """+ full = read_notes(state_dir)+ if not full:+ return ""+ if len(full.encode("utf-8")) <= max_bytes:+ return full+ encoded = full.encode("utf-8")+ tail = encoded[-max_bytes:]+ # Search for the first newline in the FIRST HALF only. If we find one+ # there, trim a partial leading line. If the only newline lives in+ # the last half (or at the very end), keep the whole tail — losing a+ # mid-sentence start is better than throwing away content.+ half = max(1, len(tail) // 2)+ cut = tail[:half].find(b"\n")+ if cut == -1:+ return tail.decode("utf-8", errors="replace")+ return tail[cut + 1:].decode("utf-8", errors="replace")+++def append_note(state_dir: Path, text: str, *,+ kind: str = "note", author: str = "agent") -> dict:+ """Append a timestamped entry to the notes file.++ Returns metadata: {path, bytes_written, total_bytes, kind, author, ts}.++ The format mirrors a chat log:++ ## 2026-05-06 14:33 [note · agent]+ the agreed master style is "documentary, kodak portra 400, …"++ The header on a fresh file describes what the file is for — written+ once on first append.+ """+ p = notes_path(state_dir)+ state_dir.mkdir(parents=True, exist_ok=True)+ fresh = not p.is_file()+ text = (text or "").strip()+ if not text:+ raise ValueError("note text is empty")+ ts = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M")+ block = f"\n## {ts} [{kind} · {author}]\n{text}\n"+ if fresh:+ block = (+ "# Phosphene Agentic Flows — project notes\n"+ "Persistent memory across sessions. The agent reads the tail\n"+ "of this file every turn; you can also edit it manually.\n"+ + block+ )+ with p.open("a", encoding="utf-8") as f:+ f.write(block)+ try:+ os.chmod(p, 0o600)+ except OSError:+ pass+ return {+ "path": str(p),+ "bytes_written": len(block),+ "total_bytes": p.stat().st_size,+ "kind": kind,+ "author": author,+ "ts": ts,+ }diff --git a/agent/prompts.py b/agent/prompts.pyindex 5b92237..573382b 100644--- a/agent/prompts.py+++ b/agent/prompts.py@@ -16,12 +16,18 @@ from datetime import datetimedef build_system_prompt(*, capabilities: dict, tools_doc: str,- repo_version: str = "v2.0.4") -> str:+ repo_version: str = "v2.0.4",+ project_notes: str = "") -> str:"""Render the system prompt for the agent given the current panel state.`capabilities` describes the host Mac (RAM tier, max dimensions,whether Q8 is downloaded). The agent uses these to clamp its plan towhat this hardware can actually render.++ `project_notes` is a tail of the persistent project-memory file —+ surfaced inline so the agent has cross-session context without+ needing to read the whole file every turn. Empty string is fine and+ produces a "no notes yet" line."""tier = capabilities.get("tier", "standard")friendly = {"base": "Compact", "standard": "Comfortable",@@ -30,6 +36,7 @@ def build_system_prompt(*, capabilities: dict, tools_doc: str,max_dim_kf = capabilities.get("max_dim_kf", 768)has_q8 = capabilities.get("allows_q8", False)today = datetime.now().strftime("%Y-%m-%d")+ project_notes_block = _format_project_notes_block(project_notes)return f"""You are the Phosphene Agentic Flows director.@@ -78,8 +85,20 @@ Today is {today}. Phosphene version: {repo_version}. Hardware tier:already (e.g. ends with "queue them all so renders run overnight").3. **One action block per turn.** Each model reply may contain at mostONE tool call. Runtime appends the result and calls you again.-4. **Never invent file paths.** Attached image paths come inline in- the user message. Use them verbatim.+4. **Never invent file paths.** Attached files come at the top of the+ user message inside an `<attachments>` JSON block:+ ```+ <attachments>+ [{{"path":"/abs/path/to/hero.png","name":"hero.png","mime":"image/png"}}, ...]+ </attachments>+ <user's text continues here>+ ```+ Use the `path` value verbatim — for an image, pass it as `image_path`+ in `submit_shot` (mode `i2v`) or as `clip_path` to `extract_frame`.+ For PDFs / text files, call `read_document` first to see the contents.+ Treat the `<attachments>` block itself as METADATA: do not echo it+ back, do not include it in shot prompts, do not invent a different+ path for the same file.5. **No surprises while the user is asleep.** Don't switch qualitytiers, change resolutions, or add bonus clips that weren't in theapproved plan.@@ -424,4 +443,20 @@ fix. Don't retry the same call — adjust the args.If the script asks for a shot that violates LTX's rules (wide multi-character composition, fast hands, etc.), **silently adapt** andnote the change in your plan. The user trusts your director's eye.++# Project memory (persists across sessions)++Below is the tail of your durable project notes. Treat it as YOUR+prior decisions and the user's prior preferences. When something+deserves to outlive the chat — a master style, a character bible+entry, an anchor-PNG path, a tier choice the user approved — call+`append_project_notes` so future sessions remember.++{project_notes_block}"""+++def _format_project_notes_block(notes: str) -> str:+ if not notes.strip():+ return "_(no notes yet — first project, or notes file empty)_"+ return "```\n" + notes.strip() + "\n```"diff --git a/agent/runtime.py b/agent/runtime.pyindex 5cfb3de..b527867 100644--- a/agent/runtime.py+++ b/agent/runtime.py@@ -61,6 +61,7 @@ class Session:created_at: float = field(default_factory=time.time)updated_at: float = field(default_factory=time.time)finished: bool = False+ pinned: bool = False # surfaced at the top of the sidebardef to_dict(self) -> dict:# NEVER include api_key on the wire — to_public_dict() masks it. The@@ -76,6 +77,7 @@ class Session:"created_at": self.created_at,"updated_at": self.updated_at,"finished": self.finished,+ "pinned": self.pinned,}def to_persisted_dict(self) -> dict:@@ -90,6 +92,7 @@ class Session:"created_at": self.created_at,"updated_at": self.updated_at,"finished": self.finished,+ "pinned": self.pinned,}@classmethod@@ -103,6 +106,7 @@ class Session:created_at=d.get("created_at", time.time()),updated_at=d.get("updated_at", time.time()),finished=d.get("finished", False),+ pinned=d.get("pinned", False),)@@ -143,7 +147,15 @@ def load_session(session_id: str, state_dir: Path) -> Session | None:def list_sessions(state_dir: Path) -> list[dict]:- """Compact summaries — id, title, message count, last update."""+ """Compact summaries — id, title, message count, last update.++ Includes:+ preview: first ~80 chars of the first user-text message (skipping+ attachment header and tool-result wrappers).+ pinned: surface above all time buckets in the sidebar.+ submitted_shot_ids: live cross-reference for the "running" dot —+ the UI matches these against the panel queue.+ """out = []d = sessions_dir(state_dir)if not d.is_dir():@@ -153,11 +165,33 @@ def list_sessions(state_dir: Path) -> list[dict]:data = json.loads(p.read_text(encoding="utf-8"))except (OSError, json.JSONDecodeError):continue+ msgs = data.get("messages") or []+ preview = ""+ for m in msgs:+ if m.get("role") != "user":+ continue+ c = (m.get("content") or "").strip()+ if not c or c.startswith("<tool_result"):+ continue+ # Strip leading <attachments>...</attachments> block.+ if c.startswith("<attachments>"):+ end = c.find("</attachments>")+ if end != -1:+ c = c[end + len("</attachments>"):].strip()+ preview = c[:120].replace("\n", " ").strip()+ if len(c) > 120:+ preview += "…"+ break+ sub_shots = (data.get("tool_state") or {}).get("submitted_shots") or []+ shot_ids = [s.get("job_id") for s in sub_shots if isinstance(s, dict) and s.get("job_id")]out.append({"session_id": data.get("session_id", p.stem),"title": data.get("title", "Untitled"),- "messages": len(data.get("messages") or []),- "shots_submitted": len((data.get("tool_state") or {}).get("submitted_shots") or []),+ "messages": len(msgs),+ "shots_submitted": len(sub_shots),+ "submitted_shot_ids": shot_ids,+ "preview": preview,+ "pinned": bool(data.get("pinned", False)),"updated_at": data.get("updated_at", p.stat().st_mtime),"created_at": data.get("created_at", p.stat().st_ctime),"finished": data.get("finished", False),@@ -175,6 +209,31 @@ def delete_session(session_id: str, state_dir: Path) -> bool:return False+def rename_session(session_id: str, new_title: str, state_dir: Path) -> bool:+ if not is_valid_session_id(session_id):+ return False+ sess = load_session(session_id, state_dir)+ if sess is None:+ return False+ new_title = (new_title or "").strip()[:120]+ if not new_title:+ return False+ sess.title = new_title+ save_session(sess, state_dir)+ return True+++def set_pinned(session_id: str, pinned: bool, state_dir: Path) -> bool:+ if not is_valid_session_id(session_id):+ return False+ sess = load_session(session_id, state_dir)+ if sess is None:+ return False+ sess.pinned = bool(pinned)+ save_session(sess, state_dir)+ return True++def new_session(*, title: str, engine_config: engine.EngineConfig) -> Session:sid = uuid.uuid4().hex[:12]return Session(@@ -220,11 +279,20 @@ def run_turn(session: Session, user_message: str | None,passreturn ev- # 1. Inject (or refresh) the system prompt at index 0.+ # 1. Inject (or refresh) the system prompt at index 0. The project+ # notes excerpt is read fresh every turn so a recently appended+ # memory becomes visible immediately on the next turn.+ from agent import project as _project+ notes_excerpt = ""+ try:+ notes_excerpt = _project.read_notes_excerpt(panel_ops.state_dir)+ except Exception: # noqa: BLE001+ passsys_prompt = prompts.build_system_prompt(capabilities=panel_ops.capabilities,tools_doc=tools_doc,repo_version=(system_prompt_overrides or {}).get("version", "v2.0.4"),+ project_notes=notes_excerpt,)if not session.messages or session.messages[0].get("role") != "system":session.messages.insert(0, {"role": "system", "content": sys_prompt})@@ -310,6 +378,8 @@ def render_tools_doc() -> str:order = ["estimate_shot", "submit_shot", "get_queue_status","wait_for_shot", "extract_frame", "upload_image",+ "read_document",+ "read_project_notes", "append_project_notes","write_session_manifest", "finish",]for name in order:diff --git a/agent/tools.py b/agent/tools.pyindex f0821c3..00808fc 100644--- a/agent/tools.py+++ b/agent/tools.py@@ -28,7 +28,7 @@ import osimport reimport subprocessimport time-from dataclasses import dataclass+from dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Any, Callable@@ -69,6 +69,11 @@ class PanelOps:# plan to what this Mac can actually render.capabilities: dict+ # Persistent state root (for project notes, sessions, configs). The+ # agent's project-memory tools write under here so notes survive+ # across sessions and panel restarts.+ state_dir: Path = field(default_factory=lambda: Path("state"))+# ---- Tool registry ---------------------------------------------------------TOOL_HANDLERS: dict[str, Callable] = {}@@ -875,6 +880,147 @@ def _upload_image(args: dict, ops: PanelOps, session: dict) -> dict:}+@tool("read_document")+def _read_document(args: dict, ops: PanelOps, session: dict) -> dict:+ """Read a user-attached text or PDF document and return its contents.++ Use this when the user attaches a script, treatment, character bible,+ or any text/PDF they want you to act on. The chat UI puts the absolute+ path inside the user message's `<attachments>` block — pass that path+ here. Always read the doc BEFORE drafting a plan if one was attached.++ Args:+ path: str — absolute path of the attached file (from <attachments>).+ max_chars: int (optional, default 80000) — clamp the returned text+ so a long PDF doesn't blow the model's context. The tail is+ replaced with "[truncated, N more chars]" when this fires.++ Returns:+ { path, name, mime, kind, char_count, truncated, content,+ page_count? }++ Raises:+ _ToolValidationError if the path is outside uploads_dir, missing,+ or unreadable. PDFs require pypdf — if it isn't installed the tool+ returns {"error": "pypdf not installed; ..."} rather than raising.+ """+ raw = _required(args, "path")+ p = _ensure_under(Path(raw), [ops.uploads_dir])+ if not p.is_file():+ raise _ToolValidationError(f"document not found: {p}")++ max_chars = int(args.get("max_chars") or 80_000)+ name = p.name+ suffix = p.suffix.lower()++ text = ""+ page_count: int | None = None++ if suffix in (".txt", ".md", ".markdown", ".rst", ".json", ".csv"):+ try:+ raw_b = p.read_bytes()+ except OSError as e:+ raise _ToolValidationError(f"cannot read {name}: {e}") from e+ # Decode tolerantly — script writers sometimes paste text with+ # mixed encodings. Replacement is better than refusing the whole doc.+ text = raw_b.decode("utf-8", errors="replace")+ kind = "text"++ elif suffix == ".pdf":+ try:+ import pypdf # noqa: F401 — local import on purpose+ except ImportError:+ return {+ "path": str(p), "name": name, "mime": "application/pdf",+ "kind": "pdf", "error": (+ "pypdf is not installed in Phosphene's venv. "+ "Install with: ltx-2-mlx/env/bin/pip install pypdf — "+ "then re-attach. Alternatively, paste the script text "+ "directly into the chat."+ ),+ }+ try:+ reader = pypdf.PdfReader(str(p))+ pages: list[str] = []+ for i, page in enumerate(reader.pages):+ pages.append(page.extract_text() or "")+ text = "\n\n".join(pages).strip()+ page_count = len(reader.pages)+ except Exception as e: # noqa: BLE001+ raise _ToolValidationError(f"PDF parse failed for {name}: {e}") from e+ kind = "pdf"++ else:+ raise _ToolValidationError(+ f"unsupported document type {suffix!r}. Supported: "+ ".txt, .md, .pdf"+ )++ truncated = False+ full_len = len(text)+ if full_len > max_chars:+ head_len = max_chars - 64 # leave room for the marker+ text = text[:head_len] + f"\n\n[truncated, {full_len - head_len} more chars]"+ truncated = True++ out = {+ "path": str(p),+ "name": name,+ "mime": "application/pdf" if kind == "pdf" else "text/plain",+ "kind": kind,+ "char_count": full_len,+ "truncated": truncated,+ "content": text,+ }+ if page_count is not None:+ out["page_count"] = page_count+ return out+++@tool("read_project_notes")+def _read_project_notes(args: dict, ops: PanelOps, session: dict) -> dict:+ """Read the full project-notes file.++ Project notes are durable memory across sessions. The system prompt+ already includes a 6 KB tail every turn — call this tool only when+ you need OLDER context (e.g. the user references a decision from+ weeks ago, or you need to see the running cast list in full).++ Returns: { path, char_count, content }.+ """+ from agent import project as _project+ full = _project.read_notes(ops.state_dir)+ return {+ "path": str(_project.notes_path(ops.state_dir)),+ "char_count": len(full),+ "content": full,+ }+++@tool("append_project_notes")+def _append_project_notes(args: dict, ops: PanelOps, session: dict) -> dict:+ """Append a durable memory entry to the project notes file.++ Use this whenever a decision deserves to outlive the current chat:+ a master style choice, a character bible entry, an anchor-PNG path+ you want reused, a tier choice the user explicitly approved. The+ entry is timestamped automatically.++ Args:+ text: str — what to remember (one short paragraph or bullet).+ kind: str (optional, default "note") — labels: "note", "style",+ "cast", "decision". Visible in the markdown header for+ skimmability.+ """+ from agent import project as _project+ text = _required(args, "text")+ kind = (args.get("kind") or "note").strip()[:24] or "note"+ try:+ return _project.append_note(ops.state_dir, text, kind=kind, author="agent")+ except ValueError as e:+ raise _ToolValidationError(str(e)) from e++@tool("finish")def _finish(args: dict, ops: PanelOps, session: dict) -> dict:"""Signal that the agent has finished its current task.diff --git a/mlx_ltx_panel.py b/mlx_ltx_panel.pyindex ff35cbd..c95763c 100644--- a/mlx_ltx_panel.py+++ b/mlx_ltx_panel.py@@ -3722,6 +3722,7 @@ def _build_panel_ops() -> agent_tools.PanelOps:outputs_dir=OUTPUT,uploads_dir=UPLOADS,capabilities=_agent_capabilities(include_secrets=True),+ state_dir=STATE_DIR,)@@ -3730,12 +3731,37 @@ def _agent_log_sink(line: str) -> None:push(line)+_ATTACHMENTS_RE = re.compile(+ r"^<attachments>\s*(.*?)\s*</attachments>\s*", re.DOTALL+)+++def _split_user_attachments(content: str) -> tuple[str, list[dict]]:+ """Pull a leading `<attachments>JSON</attachments>` block off a user+ message, returning (visible_text, attachments_list).++ The block is the wire convention for image/PDF/text attachments — see+ the /message handler. Anything that fails to parse is left in place+ so the user can still read what they typed.+ """+ m = _ATTACHMENTS_RE.match(content or "")+ if not m:+ return content, []+ try:+ atts = json.loads(m.group(1))+ if not isinstance(atts, list):+ return content, []+ except (json.JSONDecodeError, ValueError):+ return content, []+ return content[m.end():], atts++def _render_session_messages(sess: agent_runtime.Session) -> list[dict]:"""Format session.messages for the chat UI.The raw message list contains:- the system prompt (skip in UI)- - user messages (show as user bubbles)+ - user messages (show as user bubbles, optionally with attachments)- assistant messages (may contain a fenced action block — strip itfor display, surface the parsed tool name as a chip)- tool result wrapper user messages (parse, show as a tool-result@@ -3760,7 +3786,11 @@ def _render_session_messages(sess: agent_runtime.Session) -> list[dict]:out.append({"kind": "tool_result", "result": parsed})continueif role == "user":- out.append({"kind": "user", "content": content})+ visible, atts = _split_user_attachments(content)+ entry: dict = {"kind": "user", "content": visible}+ if atts:+ entry["attachments"] = atts+ out.append(entry)continueif role == "assistant":action = agent_tools.parse_action_block(content)@@ -4537,6 +4567,15 @@ class Handler(BaseHTTPRequestHandler):self._json({"sessions": agent_runtime.list_sessions(STATE_DIR)})return+ if parsed.path == "/agent/notes":+ try:+ from agent import project as _project+ content = _project.read_notes(STATE_DIR)+ except Exception as e: # noqa: BLE001+ self._json({"error": str(e)}, 500); return+ self._json({"content": content, "bytes": len(content.encode("utf-8"))})+ return+if parsed.path.startswith("/agent/sessions/"):sid = parsed.path[len("/agent/sessions/"):]sid = sid.split("/", 1)[0]@@ -5121,8 +5160,48 @@ class Handler(BaseHTTPRequestHandler):self._json({"error": f"bad JSON: {e}"}, 400); returnuser_msg = payload.get("message") or payload.get("content") or ""user_msg = user_msg.strip()- if not user_msg:- self._json({"error": "message is required"}, 400); return++ # Validate + normalize attachments. Each path must live under+ # UPLOADS — the client can lie about the path even though it+ # came from a /upload response. Without this check, a malicious+ # page could submit /etc/passwd as an attachment and the agent's+ # tools would happily inspect it.+ raw_atts = payload.get("attachments") or []+ attachments: list[dict] = []+ uploads_root = UPLOADS.resolve()+ for a in raw_atts:+ if not isinstance(a, dict):+ continue+ p_str = (a.get("path") or "").strip()+ if not p_str:+ continue+ try:+ p = Path(p_str).resolve()+ except Exception:+ continue+ if not p.is_relative_to(uploads_root) or not p.is_file():+ self._json({"error": f"attachment outside uploads dir or missing: {p_str}"}, 400)+ return+ attachments.append({+ "path": str(p),+ "name": (a.get("name") or p.name)[:200],+ "mime": (a.get("mime") or "")[:80],+ "size": int(a.get("size") or 0),+ })+ if not user_msg and not attachments:+ self._json({"error": "message or attachment is required"}, 400); return++ if attachments:+ # Embed an <attachments> JSON block in front of the user text+ # so the agent sees the file references inline. The renderer+ # strips this block back out for display and exposes the+ # parsed list as m.attachments.+ user_msg = (+ "<attachments>\n"+ + json.dumps(attachments)+ + "\n</attachments>\n"+ + user_msg+ )# Always re-load the engine config in case the user just changed# it via the settings drawer — the session keeps its own copy# for record-keeping but we honor the live config for actual calls.@@ -5201,6 +5280,56 @@ class Handler(BaseHTTPRequestHandler):self._json({"ok": ok})return+ if path == "/agent/notes":+ # Manual edit from the Project Notes modal. We overwrite the+ # whole file, so the user gets full control. Empty content+ # collapses to a deletion (file persists with 0 bytes — the+ # excerpt helper handles that).+ try:+ payload = (json.loads(body or "{}")+ if ctype.startswith("application/json")+ else {k: v[0] if v else "" for k, v in form.items()})+ except json.JSONDecodeError as e:+ self._json({"error": f"bad JSON: {e}"}, 400); return+ content = payload.get("content")+ if content is None:+ self._json({"error": "content is required"}, 400); return+ if len(content.encode("utf-8")) > 4 * 1024 * 1024:+ self._json({"error": "notes file too large (max 4 MB)"}, 413); return+ from agent import project as _project+ STATE_DIR.mkdir(parents=True, exist_ok=True)+ atomic_write_text(_project.notes_path(STATE_DIR), content)+ self._json({"ok": True, "bytes": len(content.encode("utf-8"))})+ return++ if path.startswith("/agent/sessions/") and path.endswith("/rename"):+ sid = path[len("/agent/sessions/"):-len("/rename")]+ try:+ payload = (json.loads(body or "{}")+ if ctype.startswith("application/json")+ else {k: v[0] if v else "" for k, v in form.items()})+ except json.JSONDecodeError as e:+ self._json({"error": f"bad JSON: {e}"}, 400); return+ new_title = (payload.get("title") or "").strip()+ if not new_title:+ self._json({"error": "title is required"}, 400); return+ ok = agent_runtime.rename_session(sid, new_title, STATE_DIR)+ self._json({"ok": ok})+ return++ if path.startswith("/agent/sessions/") and path.endswith("/pin"):+ sid = path[len("/agent/sessions/"):-len("/pin")]+ try:+ payload = (json.loads(body or "{}")+ if ctype.startswith("application/json")+ else {k: v[0] if v else "" for k, v in form.items()})+ except json.JSONDecodeError as e:+ self._json({"error": f"bad JSON: {e}"}, 400); return+ pinned = bool(payload.get("pinned", True))+ ok = agent_runtime.set_pinned(sid, pinned, STATE_DIR)+ self._json({"ok": ok, "pinned": pinned})+ return+# Image-engine config (pluggable: mock | bfl).if path == "/agent/image/config":try:@@ -7033,6 +7162,7 @@ HTML = r"""<!doctype html>/* ---- Pane wrapper ---- */.agent-pane {+ position: relative; /* anchors agent-drop-overlay */display: flex; flex-direction: column;background: var(--panel);border: 1px solid var(--border);@@ -7237,6 +7367,62 @@ HTML = r"""<!doctype html>overflow: hidden; text-overflow: ellipsis; white-space: nowrap;line-height: 1.4;}+ /* Hover-revealed action row at the right edge of each session item.+ Kept hidden until hover so the list stays scannable; revealed always+ on the focused (keyboard-navigated) item too. */+ .asp-actions {+ position: absolute;+ top: 8px; right: 8px;+ display: none;+ gap: 2px;+ align-items: center;+ }+ .asp-item:hover .asp-actions,+ .asp-item.is-focused .asp-actions { display: inline-flex; }+ .asp-action-btn {+ width: 24px; height: 24px;+ border-radius: 6px;+ border: 1px solid transparent;+ background: rgba(255,255,255,0.05);+ color: var(--muted);+ cursor: pointer;+ display: inline-flex;+ align-items: center; justify-content: center;+ transition: background 0.15s, color 0.15s, border-color 0.15s;+ padding: 0;+ }+ .asp-action-btn:hover {+ background: var(--bg-2);+ color: var(--text);+ border-color: var(--border);+ }+ .asp-action-btn.danger:hover { color: #f49a9e; border-color: rgba(244,154,158,0.5); }+ .asp-action-btn svg { width: 12px; height: 12px; }+ .asp-item.is-pinned .asp-pin-icon { color: var(--accent-bright); }+ .asp-item .asp-item-title-edit {+ flex: 1;+ background: var(--bg-2);+ color: var(--text);+ border: 1px solid var(--accent);+ border-radius: 5px;+ padding: 2px 6px;+ font-size: 13px;+ font-weight: 500;+ width: 100%;+ box-sizing: border-box;+ outline: none;+ }+ .asp-item-preview {+ font-size: 11.5px;+ color: var(--muted);+ margin: 4px 0 0 0;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+ max-width: 100%;+ }+ /* Position items so action overlay can absolute. Already in .asp-item base. */+ .asp-item { position: relative; }.asp-item-meta {display: flex; gap: 6px; align-items: center;margin-top: 5px;@@ -8492,7 +8678,7 @@ HTML = r"""<!doctype html>.agent-composer textarea {width: 100%;min-height: 48px; max-height: 220px;- padding: 13px 56px 13px 16px;+ padding: 13px 56px 13px 50px;background: var(--panel);color: var(--text);border: 1px solid var(--border);@@ -8504,6 +8690,171 @@ HTML = r"""<!doctype html>transition: border-color 0.18s, box-shadow 0.18s, background 0.18s;box-sizing: border-box;}+ /* Paperclip — mirrors send-btn placement on the left edge of the+ composer. Same shape, ghost styling so it doesn't compete with Send. */+ .agent-composer .attach-btn {+ position: absolute;+ left: 7px; bottom: 7px;+ width: 34px; height: 34px;+ border-radius: 10px;+ background: transparent;+ color: var(--muted);+ border: 1px solid transparent;+ cursor: pointer;+ display: inline-flex;+ align-items: center; justify-content: center;+ transition: background 0.18s, color 0.18s, border-color 0.18s;+ }+ .agent-composer .attach-btn:hover {+ background: rgba(255,255,255,0.06);+ color: var(--text);+ border-color: var(--border);+ }+ .agent-composer .attach-btn svg {+ width: 16px; height: 16px;+ }+ .agent-composer .attach-btn .count {+ position: absolute;+ top: -3px; right: -3px;+ min-width: 14px; height: 14px;+ padding: 0 3px;+ background: var(--accent);+ color: white;+ border-radius: 7px;+ font-size: 9px;+ font-weight: 700;+ display: none;+ align-items: center; justify-content: center;+ letter-spacing: 0;+ }+ .agent-composer .attach-btn.has-attach { color: var(--accent); }+ .agent-composer .attach-btn.has-attach .count { display: inline-flex; }+ /* Pending-attachment chip row above the textarea. */+ .agent-attach-row {+ display: flex;+ flex-wrap: wrap;+ gap: 6px;+ padding: 0 0 8px 0;+ max-width: 720px;+ margin-left: auto; margin-right: auto;+ }+ .agent-attach-chip {+ display: inline-flex;+ align-items: center;+ gap: 8px;+ padding: 5px 10px 5px 5px;+ background: var(--bg-3, rgba(255,255,255,0.04));+ border: 1px solid var(--border);+ border-radius: 10px;+ font-size: 12px;+ color: var(--text);+ max-width: 240px;+ }+ .agent-attach-chip .thumb {+ width: 28px; height: 28px;+ border-radius: 6px;+ object-fit: cover;+ background: rgba(255,255,255,0.08);+ flex-shrink: 0;+ display: inline-flex;+ align-items: center; justify-content: center;+ color: var(--muted);+ font-size: 10px;+ }+ .agent-attach-chip .name {+ max-width: 140px;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+ }+ .agent-attach-chip .meta {+ color: var(--muted);+ font-size: 10px;+ flex-shrink: 0;+ }+ .agent-attach-chip .remove {+ background: transparent;+ border: none;+ color: var(--muted);+ cursor: pointer;+ padding: 0 2px;+ font-size: 14px;+ line-height: 1;+ }+ .agent-attach-chip .remove:hover { color: #f49a9e; }+ .agent-attach-chip.pending { opacity: 0.6; }+ .agent-attach-chip.error { border-color: rgba(244,154,158,0.6); color: #f49a9e; }++ /* In-message attachment chips (rendered inside user bubbles). Slightly+ softer than the composer chips to read as past, not pending. */+ .agent-msg-attachments {+ display: flex;+ flex-wrap: wrap;+ gap: 6px;+ margin-bottom: 8px;+ }+ .agent-msg-attach {+ display: inline-flex;+ align-items: center;+ gap: 8px;+ padding: 5px 10px 5px 5px;+ background: rgba(255,255,255,0.04);+ border: 1px solid var(--border);+ border-radius: 10px;+ font-size: 11.5px;+ color: var(--text);+ text-decoration: none;+ }+ .agent-msg-attach:hover {+ background: rgba(255,255,255,0.08);+ border-color: var(--accent-dim);+ }+ .agent-msg-attach .thumb {+ width: 26px; height: 26px;+ border-radius: 6px;+ object-fit: cover;+ flex-shrink: 0;+ background: rgba(255,255,255,0.08);+ display: inline-flex;+ align-items: center; justify-content: center;+ color: var(--muted);+ font-size: 10px;+ }+ .agent-msg-attach .name {+ max-width: 200px;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+ }++ /* Drag-drop overlay — full-bleed inside the agent pane, visible only+ while a drag is over the chat surface. Subtle accent ring + dashed+ border so the user understands a drop is wanted. */+ .agent-drop-overlay {+ position: absolute;+ inset: 0;+ background: rgba(47,129,247,0.08);+ border: 2px dashed var(--accent);+ border-radius: 14px;+ display: none;+ align-items: center; justify-content: center;+ pointer-events: none;+ z-index: 50;+ backdrop-filter: blur(2px);+ }+ .agent-drop-overlay.visible { display: flex; }+ .agent-drop-overlay .drop-card {+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 6px;+ color: var(--accent);+ padding: 18px 28px;+ background: var(--panel);+ border: 1px solid var(--accent-dim);+ border-radius: 14px;+ box-shadow: 0 8px 32px rgba(0,0,0,0.4);+ }.agent-composer textarea:focus {outline: none;border-color: var(--accent);@@ -8903,6 +9254,14 @@ HTML = r"""<!doctype html><line x1="3" y1="21" x2="10" y2="14"/></svg></button>+ <button type="button" class="icon-btn" onclick="openProjectNotes()" title="Project notes — persistent memory across sessions">+ <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/>+ <polyline points="14 2 14 8 20 8"/>+ <line x1="9" y1="13" x2="15" y2="13"/>+ <line x1="9" y1="17" x2="15" y2="17"/>+ </svg>+ </button><button type="button" class="icon-btn" onclick="openAgentSettings()" title="Settings"><svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"></circle>@@ -8925,7 +9284,22 @@ HTML = r"""<!doctype html><span class="ref-label" id="agentRefChipLabel">…</span><button type="button" class="clear" onclick="agentClearRefine()" title="Cancel refine">×</button></div>+ <!-- Pending-attachment chip row. Filled by agentAddAttachment() as the+ user picks files (paperclip / drag-drop / paste). Survives between+ messages until cleared. -->+ <div class="agent-attach-row" id="agentAttachRow" style="display:none"></div><div class="agent-composer-wrap">+ <input type="file" id="agentFileInput" multiple+ accept="image/png,image/jpeg,image/webp,image/gif,application/pdf,text/plain,text/markdown,.md,.txt,.pdf"+ style="display:none" onchange="agentOnFilesPicked(event)">+ <button type="button" id="agentAttachBtn" class="attach-btn"+ onclick="document.getElementById('agentFileInput').click()"+ title="Attach images or documents (drag-drop or ⌘V also work)">+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="M21.44 11.05l-9.19 9.19a6 6 0 01-8.49-8.49l9.19-9.19a4 4 0 015.66 5.66l-9.2 9.19a2 2 0 01-2.83-2.83l8.49-8.48"/>+ </svg>+ <span class="count" id="agentAttachCount">0</span>+ </button><textarea id="agentInput"placeholder="Paste a script, or describe a piece. The agent will plan, estimate the wall time, and queue overnight."rows="1"@@ -8936,7 +9310,19 @@ HTML = r"""<!doctype html><polyline points="5 12 12 5 19 12"/></svg></button>- <span class="hint">Cmd/Ctrl + Enter to send</span>+ <span class="hint">Cmd/Ctrl + Enter to send · ⌘V to paste · drag files to attach</span>+ </div>+ <!-- Drag-drop overlay shown only while a drag is over the chat surface. -->+ <div class="agent-drop-overlay" id="agentDropOverlay">+ <div class="drop-card">+ <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">+ <path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/>+ <polyline points="17 8 12 3 7 8"/>+ <line x1="12" y1="3" x2="12" y2="15"/>+ </svg>+ <div style="font-weight:600">Drop to attach</div>+ <div style="color:var(--muted); font-size:12px">images, PDFs, and text files</div>+ </div></div></div></section>@@ -9761,6 +10147,34 @@ HTML = r"""<!doctype html></div></div>+<!-- ============== PROJECT NOTES MODAL ============== -->+<!-- Persistent memory across sessions — the agent reads the tail of+ this file every turn. Editable here too. -->+<div class="agent-settings-modal" id="agentNotesModal" onclick="if(event.target===this)closeProjectNotes()">+ <div class="agent-settings-card" style="max-width:780px">+ <h2>Project notes+ <button type="button" onclick="closeProjectNotes()">Close</button>+ </h2>+ <div class="hint">+ The agent's persistent memory across sessions. It reads the tail+ every turn, and can append new entries via+ <code>append_project_notes</code>. Edit freely; one Markdown file at+ <code>state/agent_project_notes.md</code>.+ </div>+ <div class="field">+ <label>Notes (Markdown)</label>+ <textarea id="agentNotesTextarea" rows="20" style="width:100%; min-height:380px; font-family: ui-monospace, Menlo, monospace; font-size:12px; line-height:1.55"></textarea>+ </div>+ <div style="display:flex; gap:8px; align-items:center; margin-top:8px">+ <span id="agentNotesStatus" style="flex:1; color:var(--muted); font-size:11px"></span>+ <button type="button" onclick="closeProjectNotes()"+ style="padding:7px 14px;border-radius:8px;border:1px solid var(--border);background:transparent;color:var(--text);cursor:pointer">Cancel</button>+ <button type="button" onclick="saveProjectNotes()"+ style="padding:7px 14px;border-radius:8px;border:none;background:var(--accent);color:white;cursor:pointer;font-weight:600">Save</button>+ </div>+ </div>+</div>+<!-- ============== AGENT SETTINGS MODAL ============== --><div class="agent-settings-modal" id="agentSettingsModal" onclick="if(event.target===this)closeAgentSettings()"><div class="agent-settings-card">@@ -13283,14 +13697,25 @@ function aspRender() {}empty.hidden = true;- // Group into time buckets, render with sticky labels.+ // Pinned sessions surface at the top regardless of recency. Then the+ // standard time buckets in order. All rows still share one keyboard+ // focus index so arrow nav flows naturally across sections.+ const pinnedItems = items.filter(s => s.pinned);+ const restItems = items.filter(s => !s.pinned);const buckets = {Today: [], Yesterday: [], 'This week': [], Earlier: []};- for (const s of items) {+ for (const s of restItems) {const b = aspBucket(s.updated_at);if (!buckets[b]) buckets[b] = [];buckets[b].push(s);}let flatIdx = 0;+ if (pinnedItems.length) {+ const head = document.createElement('div');+ head.className = 'asp-section-label';+ head.textContent = 'Pinned';+ list.appendChild(head);+ for (const s of pinnedItems) { list.appendChild(aspMakeItem(s, flatIdx)); flatIdx++; }+ }for (const label of ['Today', 'Yesterday', 'This week', 'Earlier']) {const bucket = buckets[label];if (!bucket || bucket.length === 0) continue;@@ -13310,6 +13735,7 @@ function aspMakeItem(s, flatIdx) {item.className = 'asp-item';if (s.session_id === window.AGENT.sessionId) item.classList.add('is-active');if (window.ASP.focusIndex === flatIdx) item.classList.add('is-focused');+ if (s.pinned) item.classList.add('is-pinned');item.dataset.sid = s.session_id;// Cross-reference live queue: if any of this session's submitted shots// are currently in the panel queue or running, show a live dot.@@ -13320,21 +13746,140 @@ function aspMakeItem(s, flatIdx) {const sessionShotIds = (s.submitted_shot_ids || []); // optional fieldconst isRunning = sessionShotIds.some(id => queueIds.includes(id));const time = aspRelTime(s.updated_at);+ const preview = (s.preview || '').trim();item.innerHTML = `<div class="asp-row"><div class="asp-item-title">${escapeHtml(s.title || 'Untitled')}</div><div class="asp-item-time">${escapeHtml(time)}</div></div>+ ${preview ? `<div class="asp-item-preview">${escapeHtml(preview)}</div>` : ''}<div class="asp-item-meta"><span class="asp-chip">${s.messages || 0} msg</span><span class="asp-chip asp-chip-shots">${s.shots_submitted || 0} shots</span>${isRunning ? '<span class="asp-status-running" title="Active in queue"></span>' : ''}</div>+ <div class="asp-actions">+ <button type="button" class="asp-action-btn" data-action="pin" title="${s.pinned ? 'Unpin' : 'Pin'}">+ <svg class="asp-pin-icon" viewBox="0 0 24 24" fill="${s.pinned ? 'currentColor' : 'none'}" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <line x1="12" y1="17" x2="12" y2="22"/>+ <path d="M5 17h14l-2-7V4H7v6z"/>+ </svg>+ </button>+ <button type="button" class="asp-action-btn" data-action="rename" title="Rename">+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/>+ <path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/>+ </svg>+ </button>+ <button type="button" class="asp-action-btn danger" data-action="delete" title="Delete">+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <polyline points="3 6 5 6 21 6"/>+ <path d="M19 6l-1 14a2 2 0 01-2 2H8a2 2 0 01-2-2L5 6"/>+ <path d="M10 11v6M14 11v6"/>+ </svg>+ </button>+ </div>`;- item.addEventListener('click', () => aspLoad(s.session_id));+ item.addEventListener('click', (e) => {+ // If user clicked one of the action buttons, dispatch the action+ // instead of loading the session.+ const btn = e.target.closest('[data-action]');+ if (btn) {+ e.stopPropagation();+ const action = btn.dataset.action;+ if (action === 'delete') aspDeleteRow(s, item);+ else if (action === 'rename') aspBeginRename(s, item);+ else if (action === 'pin') aspTogglePinRow(s);+ return;+ }+ aspLoad(s.session_id);+ });return item;}+async function aspDeleteRow(s, itemEl) {+ if (!confirm(`Delete "${s.title || 'Untitled'}"? This can't be undone.`)) return;+ try {+ const r = await fetch('/agent/sessions/' + encodeURIComponent(s.session_id) + '/delete', {method: 'POST'});+ if (!r.ok) throw new Error('HTTP ' + r.status);+ } catch (e) { alert('Delete failed: ' + e.message); return; }+ // Remove from in-memory list and re-render. If the deleted session+ // was the active one, fall back to the most-recent remaining.+ window.ASP.all = (window.ASP.all || []).filter(x => x.session_id !== s.session_id);+ if (s.session_id === window.AGENT.sessionId) {+ window.AGENT.sessionId = null;+ try { localStorage.removeItem('phos_agent_session'); } catch (e) {}+ const remaining = (window.ASP.all || []).slice().sort((a,b) => (b.updated_at||0) - (a.updated_at||0));+ if (remaining.length) await agentLoadSession(remaining[0].session_id);+ else {+ // Empty UI — clear the chat surface.+ const chat = document.getElementById('agentChat');+ if (chat) chat.innerHTML = '';+ agentSetSessionTitle('New chat', null);+ }+ }+ aspRefilterAndRender();+}++function aspBeginRename(s, itemEl) {+ const titleEl = itemEl.querySelector('.asp-item-title');+ if (!titleEl) return;+ const original = s.title || '';+ const input = document.createElement('input');+ input.type = 'text';+ input.className = 'asp-item-title-edit';+ input.value = original;+ titleEl.replaceWith(input);+ input.focus();+ input.select();+ const finish = async (commit) => {+ if (!input.parentNode) return;+ const next = (input.value || '').trim();+ const wrap = document.createElement('div');+ wrap.className = 'asp-item-title';+ wrap.textContent = (commit && next) ? next : original;+ input.replaceWith(wrap);+ if (!commit || !next || next === original) return;+ try {+ const r = await fetch('/agent/sessions/' + encodeURIComponent(s.session_id) + '/rename', {+ method: 'POST',+ headers: {'Content-Type': 'application/json'},+ body: JSON.stringify({title: next}),+ });+ if (!r.ok) throw new Error('HTTP ' + r.status);+ s.title = next;+ // Update the in-memory cache + active-session header if relevant.+ const cached = (window.ASP.all || []).find(x => x.session_id === s.session_id);+ if (cached) cached.title = next;+ if (window.AGENT.sessionId === s.session_id) {+ agentSetSessionTitle(next, s.session_id);+ }+ } catch (e) { alert('Rename failed: ' + e.message); }+ };+ input.addEventListener('blur', () => finish(true));+ input.addEventListener('keydown', (e) => {+ if (e.key === 'Enter') { e.preventDefault(); finish(true); }+ else if (e.key === 'Escape') { e.preventDefault(); finish(false); }+ e.stopPropagation(); // don't bubble to global asp shortcuts+ });+}++async function aspTogglePinRow(s) {+ const next = !s.pinned;+ try {+ const r = await fetch('/agent/sessions/' + encodeURIComponent(s.session_id) + '/pin', {+ method: 'POST',+ headers: {'Content-Type': 'application/json'},+ body: JSON.stringify({pinned: next}),+ });+ if (!r.ok) throw new Error('HTTP ' + r.status);+ s.pinned = next;+ const cached = (window.ASP.all || []).find(x => x.session_id === s.session_id);+ if (cached) cached.pinned = next;+ aspRefilterAndRender();+ } catch (e) { alert('Pin failed: ' + e.message); }+}+function aspMoveFocus(delta) {const items = window.ASP.filtered || [];if (items.length === 0) return;@@ -13528,11 +14073,15 @@ function renderMessage(m) {name.className = 'agent-msg-name';name.textContent = m.kind === 'user' ? 'You' : 'Claude';+ body.appendChild(name);++ if (m.attachments && m.attachments.length) {+ body.appendChild(renderAttachmentChips(m.attachments));+ }+const content = document.createElement('div');content.className = 'agent-msg-content agent-md';content.innerHTML = mdToHtml(m.content || '');-- body.appendChild(name);body.appendChild(content);if (m.tool_call) body.appendChild(renderToolCallCard(m.tool_call));@@ -13542,6 +14091,36 @@ function renderMessage(m) {return row;}+function renderAttachmentChips(attachments) {+ const wrap = document.createElement('div');+ wrap.className = 'agent-msg-attachments';+ for (const a of attachments) {+ const kind = AGENT_ATTACH_KIND(a.mime, a.name);+ const link = document.createElement('a');+ link.className = 'agent-msg-attach';+ link.href = '/image?path=' + encodeURIComponent(a.path);+ link.target = '_blank';+ link.rel = 'noopener';+ link.title = a.path;++ const thumb = document.createElement(kind === 'image' ? 'img' : 'div');+ thumb.className = 'thumb';+ if (kind === 'image') {+ thumb.src = '/image?path=' + encodeURIComponent(a.path);+ thumb.alt = a.name;+ } else {+ thumb.textContent = agentAttachIcon(kind);+ }+ const nameEl = document.createElement('span');+ nameEl.className = 'name';+ nameEl.textContent = a.name;+ link.appendChild(thumb);+ link.appendChild(nameEl);+ wrap.appendChild(link);+ }+ return wrap;+}+function renderSystemNote(text) {const div = document.createElement('div');div.style.cssText = 'text-align:center; padding:8px; font-size:11px; color:var(--muted); font-style:italic;';@@ -13788,10 +14367,25 @@ async function agentSend() {const input = document.getElementById('agentInput');const btn = document.getElementById('agentSendBtn');const text = (input.value || '').trim();- if (!text) return;++ // Wait for any in-flight uploads so the message arrives WITH its+ // attachments. UX: don't block the user — if uploads error we surface+ // them in the chip row and the user can remove/retry.+ const stillUploading = (window.AGENT.pendingAttachments || []).some(a => a.status === 'uploading');+ if (stillUploading) {+ btn.disabled = true;+ let waited = 0;+ while ((window.AGENT.pendingAttachments || []).some(a => a.status === 'uploading')) {+ await new Promise(r => setTimeout(r, 120));+ waited += 120;+ if (waited > 60000) break; // 60 s safety bail+ }+ }+ const ready = agentReadyAttachments();+ if (!text && !ready.length) return;if (!window.AGENT.sessionId) {- const sess = await agentNewSession(text);+ const sess = await agentNewSession(text || ready[0].name);if (!sess) return;}@@ -13811,12 +14405,20 @@ async function agentSend() {// Clear empty-state if present, then append user bubble + typingconst empty = chat.querySelector('.agent-empty');if (empty) empty.remove();- chat.appendChild(renderMessage({kind: 'user', content: outgoing}));+ chat.appendChild(renderMessage({+ kind: 'user',+ content: outgoing,+ attachments: ready.map(a => ({path: a.path, name: a.name, mime: a.mime, size: a.size})),+ }));chat.appendChild(renderTypingRow('Drafting plan'));chat.scrollTop = chat.scrollHeight;input.value = '';agentAutoResize(input);+ // Snapshot then clear before send — if the round-trip succeeds we don't+ // want stale chips. If it fails the user can re-attach.+ const sentAttachments = ready.map(a => ({path: a.path, name: a.name, mime: a.mime, size: a.size}));+ agentClearAttachments();agentUpdateSendState();window.AGENT.busy = true;btn.disabled = true;@@ -13856,7 +14458,7 @@ async function agentSend() {{method: 'POST',headers: {'Content-Type': 'application/json'},- body: JSON.stringify({message: outgoing}),+ body: JSON.stringify({message: outgoing, attachments: sentAttachments}),});const j = await r.json();@@ -13918,7 +14520,205 @@ function agentUpdateSendState() {const input = document.getElementById('agentInput');const btn = document.getElementById('agentSendBtn');if (!input || !btn) return;- btn.disabled = !input.value.trim() || window.AGENT.busy;+ // Allow send when there's text OR at least one ready attachment — the+ // agent can act on attachments alone (e.g. "use this image as the lead").+ const hasText = !!input.value.trim();+ const hasAttach = (window.AGENT.pendingAttachments || []).some(a => a.path);+ btn.disabled = (!hasText && !hasAttach) || window.AGENT.busy;+}++// ---- Attachments ----------------------------------------------------------+// `pendingAttachments` is the staged list before send. Each entry:+// { id, name, size, mime, path, status: 'uploading'|'ready'|'error', error }+// `path` is filled in once /upload returns. Send is allowed when at least+// one attachment is `ready` (or there is text).+window.AGENT.pendingAttachments = window.AGENT.pendingAttachments || [];++const AGENT_ATTACH_KIND = (mime, name) => {+ const m = (mime || '').toLowerCase();+ const n = (name || '').toLowerCase();+ if (m.startsWith('image/')) return 'image';+ if (m === 'application/pdf' || n.endsWith('.pdf')) return 'pdf';+ if (m.startsWith('text/') || n.endsWith('.md') || n.endsWith('.txt')) return 'text';+ return 'file';+};++const AGENT_ATTACH_MAX_BYTES = 64 * 1024 * 1024; // mirrors backend cap++function agentFmtBytes(n) {+ if (!n && n !== 0) return '';+ if (n < 1024) return n + ' B';+ if (n < 1024*1024) return (n/1024).toFixed(0) + ' KB';+ return (n/(1024*1024)).toFixed(1) + ' MB';+}++function agentAttachIcon(kind) {+ if (kind === 'image') return '🖼';+ if (kind === 'pdf') return '📄';+ if (kind === 'text') return '📝';+ return '📎';+}++function agentRenderAttachRow() {+ const row = document.getElementById('agentAttachRow');+ const btn = document.getElementById('agentAttachBtn');+ const cnt = document.getElementById('agentAttachCount');+ if (!row) return;+ const list = window.AGENT.pendingAttachments;+ if (btn) btn.classList.toggle('has-attach', list.length > 0);+ if (cnt) cnt.textContent = String(list.length);+ if (!list.length) { row.style.display = 'none'; row.innerHTML = ''; return; }+ row.style.display = 'flex';+ row.innerHTML = '';+ for (const a of list) {+ const chip = document.createElement('div');+ chip.className = 'agent-attach-chip';+ if (a.status === 'uploading') chip.classList.add('pending');+ if (a.status === 'error') chip.classList.add('error');+ const kind = AGENT_ATTACH_KIND(a.mime, a.name);+ const thumb = document.createElement(kind === 'image' && a.path ? 'img' : 'div');+ thumb.className = 'thumb';+ if (kind === 'image' && a.path) {+ thumb.src = '/image?path=' + encodeURIComponent(a.path);+ thumb.alt = a.name;+ } else {+ thumb.textContent = agentAttachIcon(kind);+ }+ const name = document.createElement('span');+ name.className = 'name';+ name.title = a.name;+ name.textContent = a.name;+ const meta = document.createElement('span');+ meta.className = 'meta';+ meta.textContent = a.status === 'uploading' ? 'uploading…'+ : a.status === 'error' ? (a.error || 'failed')+ : agentFmtBytes(a.size);+ const x = document.createElement('button');+ x.type = 'button';+ x.className = 'remove';+ x.title = 'Remove';+ x.textContent = '×';+ x.addEventListener('click', () => agentRemoveAttachment(a.id));+ chip.appendChild(thumb); chip.appendChild(name); chip.appendChild(meta); chip.appendChild(x);+ row.appendChild(chip);+ }+}++function agentRemoveAttachment(id) {+ window.AGENT.pendingAttachments =+ window.AGENT.pendingAttachments.filter(a => a.id !== id);+ agentRenderAttachRow();+ agentUpdateSendState();+}++function agentClearAttachments() {+ window.AGENT.pendingAttachments = [];+ agentRenderAttachRow();+ agentUpdateSendState();+}++async function agentUploadFile(att) {+ // POST one file to /upload. The backend already exists for the legacy+ // gallery picker; we reuse it. Returns { ok, path } on success.+ const fd = new FormData();+ fd.append('image', att._file, att.name);+ try {+ const r = await fetch('/upload', { method: 'POST', body: fd });+ const j = await r.json();+ if (!r.ok || j.error) throw new Error(j.error || ('HTTP ' + r.status));+ att.path = j.path;+ att.status = 'ready';+ } catch (e) {+ att.status = 'error';+ att.error = String(e.message || e);+ } finally {+ delete att._file; // free the File reference+ agentRenderAttachRow();+ agentUpdateSendState();+ }+}++function agentAddFiles(fileList) {+ if (!fileList || !fileList.length) return;+ for (const f of Array.from(fileList)) {+ if (f.size > AGENT_ATTACH_MAX_BYTES) {+ alert(`"${f.name}" is too large (max ${(AGENT_ATTACH_MAX_BYTES/1024/1024).toFixed(0)} MB)`);+ continue;+ }+ const att = {+ id: 'att_' + Math.random().toString(36).slice(2, 10),+ name: f.name,+ size: f.size,+ mime: f.type || '',+ path: '',+ status: 'uploading',+ _file: f,+ };+ window.AGENT.pendingAttachments.push(att);+ agentUploadFile(att); // fires async; UI updates on settle+ }+ agentRenderAttachRow();+ agentUpdateSendState();+}++function agentOnFilesPicked(ev) {+ agentAddFiles(ev.target.files);+ ev.target.value = ''; // allow picking the same file again+}++function agentReadyAttachments() {+ return (window.AGENT.pendingAttachments || []).filter(a => a.status === 'ready' && a.path);+}++// ---- Drag-drop on the chat surface ---------------------------------------+function agentInstallDragDrop() {+ const pane = document.getElementById('agentPane');+ const overlay = document.getElementById('agentDropOverlay');+ if (!pane || !overlay) return;+ let dragDepth = 0;+ pane.addEventListener('dragenter', e => {+ if (!e.dataTransfer || !Array.from(e.dataTransfer.types || []).includes('Files')) return;+ e.preventDefault();+ dragDepth += 1;+ overlay.classList.add('visible');+ });+ pane.addEventListener('dragover', e => {+ if (!e.dataTransfer || !Array.from(e.dataTransfer.types || []).includes('Files')) return;+ e.preventDefault();+ e.dataTransfer.dropEffect = 'copy';+ });+ pane.addEventListener('dragleave', () => {+ dragDepth = Math.max(0, dragDepth - 1);+ if (dragDepth === 0) overlay.classList.remove('visible');+ });+ pane.addEventListener('drop', e => {+ if (!e.dataTransfer || !e.dataTransfer.files || !e.dataTransfer.files.length) return;+ e.preventDefault();+ dragDepth = 0;+ overlay.classList.remove('visible');+ agentAddFiles(e.dataTransfer.files);+ });+}++// ---- Paste-from-clipboard on the textarea --------------------------------+function agentInstallPasteHandler() {+ const ta = document.getElementById('agentInput');+ if (!ta) return;+ ta.addEventListener('paste', e => {+ if (!e.clipboardData) return;+ const items = e.clipboardData.items || [];+ const files = [];+ for (const it of Array.from(items)) {+ if (it.kind === 'file') {+ const f = it.getAsFile();+ if (f) files.push(f);+ }+ }+ if (files.length) {+ e.preventDefault();+ agentAddFiles(files);+ }+ });}document.addEventListener('DOMContentLoaded', () => {@@ -13937,6 +14737,8 @@ document.addEventListener('DOMContentLoaded', () => {agentAutoResize(ta);agentUpdateSendState();}+ agentInstallDragDrop();+ agentInstallPasteHandler();});// ---- Engine settings drawer -----------------------------------------------@@ -14025,6 +14827,52 @@ function closeAgentSettings() {document.getElementById('agentSettingsModal').classList.remove('open');}+// ---- Project notes modal --------------------------------------------------+async function openProjectNotes() {+ const modal = document.getElementById('agentNotesModal');+ const ta = document.getElementById('agentNotesTextarea');+ const st = document.getElementById('agentNotesStatus');+ if (!modal || !ta) return;+ st.textContent = 'Loading…';+ ta.value = '';+ modal.classList.add('open');+ try {+ const r = await fetch('/agent/notes');+ const j = await r.json();+ if (!r.ok) throw new Error(j.error || ('HTTP ' + r.status));+ ta.value = j.content || '';+ const n = (j.content || '').length;+ st.textContent = n ? `${n.toLocaleString()} chars` : 'Empty — type to start.';+ } catch (e) {+ st.textContent = 'Load failed: ' + e.message;+ }+ ta.focus();+}++function closeProjectNotes() {+ document.getElementById('agentNotesModal').classList.remove('open');+}++async function saveProjectNotes() {+ const ta = document.getElementById('agentNotesTextarea');+ const st = document.getElementById('agentNotesStatus');+ if (!ta) return;+ st.textContent = 'Saving…';+ try {+ const r = await fetch('/agent/notes', {+ method: 'POST',+ headers: {'Content-Type': 'application/json'},+ body: JSON.stringify({content: ta.value}),+ });+ const j = await r.json();+ if (!r.ok) throw new Error(j.error || ('HTTP ' + r.status));+ st.textContent = `Saved · ${(j.bytes || 0).toLocaleString()} bytes`;+ setTimeout(closeProjectNotes, 350);+ } catch (e) {+ st.textContent = 'Save failed: ' + e.message;+ }+}+function agentKindChanged() {const kind = document.getElementById('agentKind').value;document.getElementById('agentLocalModelField').style.display = kind === 'phosphene_local' ? '' : 'none';