diff --git a/src/toad/client/bus_submission.py b/src/toad/client/bus_submission.py index 402939a08..0622778ba 100644 --- a/src/toad/client/bus_submission.py +++ b/src/toad/client/bus_submission.py @@ -22,6 +22,7 @@ RouteChangedError, ) from agent_comms.messaging.delivery_failure import delivery_failure +from agent_comms.messaging.message_files import FileUpload from agent_comms.messaging.message_reference import MessageReference from toad.client.application import ClientApplication @@ -29,6 +30,7 @@ from toad.client.input_decision import InputAccepted, InputDecision, InputRefused from toad.client.line_blocks import MutedRole, Span from toad.client.notification_status import DetailLines +from toad.client.prompt_composer import PromptComposer from toad.client.submission import ( BusDestination, MessageDestination, @@ -57,13 +59,24 @@ def refusal_notice(description: str) -> str: def unknown_notice(description: str) -> str: return f"Send outcome unknown: {description}. Check this conversation before resending." + @property @abstractmethod - def delivery_refused(self, description: str, body: str) -> None: - """Nothing left this machine, or Core refused it: the draft comes back with why.""" + def submission_composer(self) -> PromptComposer: + """The composer a returned draft goes back into.""" @abstractmethod - def delivery_unknown(self, description: str, body: str) -> None: + def show_send_failure(self, text: str) -> None: + """Say why a send came back, where this view says what went wrong.""" + + def delivery_refused(self, description: str, draft: SubmittedText) -> None: + """Nothing left this machine, or Core refused it: the draft comes back with why.""" + self.submission_composer.restore(draft) + self.show_send_failure(self.refusal_notice(description)) + + def delivery_unknown(self, description: str, draft: SubmittedText) -> None: """The request may have reached Core; the draft comes back with a warning.""" + self.submission_composer.restore(draft) + self.show_send_failure(self.unknown_notice(description)) @abstractmethod async def refresh_composer(self, read_only: bool) -> None: @@ -88,9 +101,10 @@ def submissions_changed(self) -> None: @dataclass(frozen=True) class TypedText(SubmittedText): - """Text typed into a message composer, exactly as typed.""" + """Text typed into a message composer, exactly as typed, and the files attached to it.""" source_text: str + files: tuple[FileUpload, ...] @dataclass(frozen=True) @@ -115,7 +129,7 @@ def __init__(self, owner: BusSubmissions, submission: SubmittedText, refusal: In self.owner, self.submission, self.refusal = owner, submission, refusal async def prepare(self): - self.owner.view.delivery_refused(self.refusal.message, self.submission.source_text) + self.owner.view.delivery_refused(self.refusal.message, self.submission) class SubmissionExecution(SubmissionStart): @@ -250,7 +264,7 @@ def delivering(self) -> tuple[str, ...]: def retire_unsent(self) -> None: """A message to a view that changed before it left comes back as the draft.""" - self.owner.view.delivery_refused("Message route changed; reopen this view", self.submission.source_text) + self.owner.view.delivery_refused("Message route changed; reopen this view", self.submission) self.owner.finish(self) def settle(self) -> None: @@ -269,7 +283,7 @@ async def send(self): async def deliver(self) -> DeliveryOutcome: """Send after the earlier messages; a refusal is answered here.""" - view, body, destination = self.owner.view, self.submission.source_text, self.destination + view, draft, destination = self.owner.view, self.submission, self.destination access, preparation = view.app.coordination_access, view.app.preparation try: await self.after.wait() @@ -277,35 +291,36 @@ async def deliver(self) -> DeliveryOutcome: read_only = await preparation.run_thread(destination.conversation.read_only, observed.core) await view.refresh_composer(read_only) if read_only: - view.delivery_refused(MessageDestination.read_only_reason, body) + view.delivery_refused(MessageDestination.read_only_reason, draft) return RefusedMessage() route_current = await preparation.run_thread(lambda: access.current(destination.root)) if not route_current or not self.current: raise HumanSendRefusedError(RelationViolationError("Message route changed; reopen this view")) except asyncio.CancelledError: - view.delivery_refused("Cancelled before sending", body) + view.delivery_refused("Cancelled before sending", draft) raise except (HumanSendError, RouteChangedError) as error: - delivery_failure(error).present(view, body) + view.delivery_refused(delivery_failure(error).description, draft) return RefusedMessage() except Exception: # Nothing left this machine: the draft comes back, and the failure stays loud. - view.delivery_refused("the send failed before it left this machine", body) + view.delivery_refused("the send failed before it left this machine", draft) raise try: - return await self.commit(observed, body) + return await self.commit(observed, draft) except (HumanSendError, RouteChangedError) as error: - delivery_failure(error).present(view, body) + view.delivery_refused(delivery_failure(error).description, draft) return RefusedMessage() - async def commit(self, observed: Observing, body: str) -> DeliveryOutcome: + async def commit(self, observed: Observing, draft: SubmittedText) -> DeliveryOutcome: view = self.owner.view task = asyncio.create_task(asyncio.to_thread( - observed.send_user_message, self.destination.conversation.send_target(), body)) + observed.send_user_message, self.destination.conversation.send_target(), draft.source_text, + draft.files)) try: return SentMessage(await asyncio.shield(task)) except asyncio.CancelledError: - view.delivery_unknown("the send was cancelled while it may still be writing", body) + view.delivery_unknown("the send was cancelled while it may still be writing", draft) def _surface_late_exc(done: asyncio.Future) -> None: if done.cancelled(): return @@ -326,7 +341,7 @@ def _surface_late_exc(done: asyncio.Future) -> None: raise except Exception: # The request left: Core may have published it. Raise for the log, never hold compose. - view.delivery_unknown("its reply could not be read", body) + view.delivery_unknown("its reply could not be read", draft) raise diff --git a/src/toad/client/connection.py b/src/toad/client/connection.py index 56294dfdd..70d3746fd 100644 --- a/src/toad/client/connection.py +++ b/src/toad/client/connection.py @@ -22,6 +22,7 @@ from typing import ClassVar, TypeVar from weakref import WeakKeyDictionary, WeakSet +from agent_comms.messaging.message_files import FileUpload from agent_comms.errors import RouteChangedError from agent_comms.execution.child_process import ( HelperStart, @@ -702,10 +703,12 @@ def session_thread(self, project: Path, preferred: str) -> ProjectThreadSelectio """The thread an unbound session at ``project`` names; Core's host resolves the path.""" return self.core.call(CommsSessionThreadTool(project=str(project), preferred=preferred)) - def send_user_message(self, target: NamedTarget, body: str) -> MessageReference: - """The owner's input, published by Core under the owner's identity, in process or - across the connector; the reference is what the view waits to draw.""" - return self.core.call(CommsUserSendTool(target=target.name, body=body)).message.source + def send_user_message(self, target: NamedTarget, body: str, + files: tuple[FileUpload, ...]) -> MessageReference: + """The owner's input and the files attached to it, published by Core under the + owner's identity, in process or across the connector; the reference is what the view + waits to draw.""" + return self.core.call(CommsUserSendTool(target=target.name, body=body, files=files)).message.source def mark_thread_read(self, identity, project: Path, through: TranscriptCursor) -> None: """Acknowledge, as the owner, what this UI displayed of one thread: Core applies it, diff --git a/src/toad/client/fragments.py b/src/toad/client/fragments.py index 095d07ca0..abb487a6b 100644 --- a/src/toad/client/fragments.py +++ b/src/toad/client/fragments.py @@ -42,6 +42,7 @@ ShownFragment, UncheckedNotifications, ) +from toad.client.message_file_links import MessageFileLink, file_chips from toad.commit_claims import CommitClaim from toad.core.projection import MroProjection from toad.message_rows import MarkdownMessageStyle, MessageRow, WireMessageStyle @@ -104,6 +105,10 @@ def status_blocks(self, shown: MessageShown, link: LineLink) -> tuple[LineBlock, @abstractmethod def outgoing_sources(self, publication: PublishedSentMessage): ... + @abstractmethod + def file_blocks(self, links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + """The chips of the message's files; only the part carrying its status draws them.""" + def draw_divider(self, consumer, divider: Divider) -> None: """A message's first part draws its header; later parts draw none.""" consumer.blocks.append(divider) @@ -155,6 +160,9 @@ def status_blocks(self, shown: MessageShown, link: LineLink): def outgoing_sources(self, publication: PublishedSentMessage): return (publication.reference,) + def file_blocks(self, links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + return file_chips(links) + class InnerPart(PartPosition): __slots__ = () @@ -169,6 +177,9 @@ def status_blocks(self, shown: MessageShown, link: LineLink): def outgoing_sources(self, publication: PublishedSentMessage): return () + def file_blocks(self, links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + return () + class OnlyPart(LastPart): """A message in one part: it draws the header and carries the status.""" @@ -374,7 +385,32 @@ def blocks(self, shown: Shown) -> tuple[LineBlock, ...]: @dataclass(kw_only=True) -class BusMessageHistoryFragment(HistoryFragment): +class MessageLines(HistoryFragment): + """A bus message drawn as lines: what leads it, its text, the chips of the files it + carries, then what trails it. `files` is what its page's read answered for it.""" + + files: tuple[MessageFileLink, ...] = () + + @property + def leading(self) -> tuple[LineBlock, ...]: + """Blocks above the message's own text.""" + return () + + @abstractmethod + def text(self, shown) -> tuple[LineBlock, ...]: + """The message's own text.""" + + def trailing(self, shown) -> tuple[LineBlock, ...]: + """Blocks below the message's own text and files.""" + return () + + def blocks(self, shown): + return (*self.leading, *self.text(shown), *self.part_position.file_blocks(self.files), + *self.trailing(shown)) + + +@dataclass(kw_only=True) +class BusMessageHistoryFragment(MessageLines): """One bus-message builder: header, styled body and Core notification feedback.""" shown: MessageShown = field(default_factory=MessageShown) @@ -427,12 +463,9 @@ def text(self, shown): def trailing(self, shown): return self.part_position.status_blocks(shown, self.details_link) - def blocks(self, shown): - return (*self.leading, *self.text(shown), *self.trailing(shown)) - @dataclass(kw_only=True) -class EvidencedFragment(DeclaredViews, HistoryFragment, ABC): +class EvidencedFragment(DeclaredViews, MessageLines, ABC): """One wire message drawn as lines. `evidence` is Core's page whose display evidence can mark this message @@ -465,21 +498,6 @@ def timestamp(self) -> float: def clipboard_text(self) -> str: return self.message.body - @property - def leading(self) -> tuple[LineBlock, ...]: - """Blocks above the message's own text.""" - return () - - def trailing(self, shown: MessageShown) -> tuple[LineBlock, ...]: - """Blocks below the message's own text.""" - return () - - @abstractmethod - def text(self, shown: MessageShown) -> tuple[LineBlock, ...]: - """The message's own text.""" - - def blocks(self, shown): - return (*self.leading, *self.text(shown), *self.trailing(shown)) class MembershipText(EvidencedFragment): diff --git a/src/toad/client/history.py b/src/toad/client/history.py index e1ee7c090..c69b146af 100644 --- a/src/toad/client/history.py +++ b/src/toad/client/history.py @@ -42,6 +42,7 @@ from toad.client.fragments import EvidencedFragment, FragmentKey, MessageShown from toad.client.history_preparation import HistorySourceSnapshot, PageRequest from toad.client.history_reads import NothingToRead, PaintedRead, ReadMarking, ReadWork +from toad.client.message_file_links import PageFiles from toad.client.history_state import ( LatestReplacementViewportRequest, ReplacementViewportRequest, @@ -62,7 +63,7 @@ SubmissionStart, SubmittedText, ) -from toad.message_rows import WireMessageStyle +from toad.message_rows import MessageRow, WireMessageStyle from toad.submission_queue import BusQueueSource from toad.target_commands import TargetContext @@ -429,11 +430,13 @@ def read(self, request: PageRequest[WireCut], through: AfterSequence) -> Prepare # "Checking": the answer that follows then changes nothing it has to redraw. scope = ReadNotifications(str(self.root), LiveNotificationRead(()), display=page.selection.notification_identity) + files = PageFiles.read(str(self.root), self.core, ( + (message.reference, MessageRow(message, self.me).file_reader) for message in page.messages)) fragments = tuple( self.fragments.of(message, shown=MessageShown( style=self.style, status=self.access.notification_status(scope, message.role.notification_references(message))), - evidence=evidence, reader=self.me, page=page) + evidence=evidence, reader=self.me, page=page, files=files.of(message.reference)) for message, evidence in page.message_evidence() ) wire = WirePage( diff --git a/src/toad/client/message_file_links.py b/src/toad/client/message_file_links.py new file mode 100644 index 000000000..77adffa29 --- /dev/null +++ b/src/toad/client/message_file_links.py @@ -0,0 +1,129 @@ +"""The files a message carries, as a history draws them: one chip per file under the +message's text, each a link to the read that returns the file's bytes. No toolkit here, and +nothing of the composer: the transcript and wire readers import this alone. + +A page learns its messages' files from Core beside the page, one comms_message_files read +for each conversation the page holds, never one per message. The read names the +conversation and the participant reading it, so Core returns only files of messages that +reader may read. Both frontends draw the chips as the spans they already draw, and open +them through `OpensLineLinks`. +""" + +from __future__ import annotations + +import base64 +import hashlib +from collections.abc import Iterable +from dataclasses import dataclass +from pathlib import Path + +from agent_comms.messaging.message_files import MessageFile, MessageFileContent +from agent_comms.messaging.message_reference import MessageReference +from agent_comms.transport.request import CoreAccess +from agent_comms.transport.tools.sidebar import ( + CommsMessageFilesTool, + CommsMessageFileTool, +) + +from toad import paths +from toad.client.line_blocks import LineBlock, LineLink, MutedRole, Span, Spans + + +def file_size(size: int) -> str: + """A byte count as a reader reads it: B under 1024, else one decimal in KB or MB.""" + if size < 1024: + return f"{size} B" + if size < 1024 * 1024: + return f"{size / 1024:.1f} KB" + return f"{size / (1024 * 1024):.1f} MB" + + +@dataclass(frozen=True, slots=True) +class FileReader: + """Whose read a message's files are: the conversation they are read in (a #channel, or + a DM's other participant) and the participant reading it.""" + + conversation: str + participant: str + + def files_of(self, core: CoreAccess, messages: tuple[MessageReference, ...]) -> tuple[MessageFile, ...]: + return core.call(CommsMessageFilesTool( + conversation=self.conversation, participant=self.participant, messages=messages)).files + + +@dataclass(frozen=True) +class MessageFileLink(LineLink): + """One file of one message on one bus, and the reader its read names.""" + + root: str + conversation: str + participant: str + file: MessageFile + + @property + def request(self) -> CommsMessageFileTool: + return CommsMessageFileTool( + conversation=self.conversation, participant=self.participant, + message=self.file.message, position=self.file.position) + + def fetched(self, core: CoreAccess) -> Path: + """The file's bytes, read from Core and kept where this machine opens them: under + the data folder, by the file's digest, in its own name. Bytes that do not match the + digest Core listed are refused, never kept.""" + return saved_copy(core.call(self.request)) + + @property + def chip(self) -> Span: + return Span(f"[{self.file.name} · {file_size(self.file.size)}]", MutedRole, self) + + +@dataclass(frozen=True) +class PageFiles: + """The files of one page's messages, as Core answered them for the page's readers.""" + + links: tuple[tuple[MessageReference, tuple[MessageFileLink, ...]], ...] = () + + @classmethod + def read(cls, root: str, core: CoreAccess, + readers: Iterable[tuple[MessageReference, FileReader]]) -> PageFiles: + """One read per reader the page's messages name.""" + grouped: dict[FileReader, list[MessageReference]] = {} + for reference, reader in readers: + grouped.setdefault(reader, []).append(reference) + links: dict[MessageReference, list[MessageFileLink]] = {} + for reader, references in grouped.items(): + for file in reader.files_of(core, tuple(references)): + links.setdefault(file.message, []).append(MessageFileLink( + str(root), reader.conversation, reader.participant, file)) + return cls(tuple((reference, tuple(found)) for reference, found in links.items())) + + def of(self, reference: MessageReference) -> tuple[MessageFileLink, ...]: + for message, found in self.links: + if message == reference: + return found + return () + + +NO_PAGE_FILES = PageFiles() +"""A page read with no file store beside it: an attached history, or a draft not yet committed.""" + + +def file_chips(links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + """Under a message's text, one line per file it carries, each its chip.""" + return tuple(Spans((link.chip,)) for link in links) + + +def saved_copy(content: MessageFileContent) -> Path: + data = base64.b64decode(content.data, validate=True) + file = content.file + # The record's name already names one file: MessageFile refuses any other as it decodes. + if hashlib.sha256(data).hexdigest() != file.sha256: + raise ValueError(f"{file.name} arrived with bytes that do not match its SHA-256.") + folder = paths.get_data() / "message-files" / file.sha256[:16] + folder.mkdir(mode=0o700, parents=True, exist_ok=True) + path = folder / file.name + if not path.is_file() or path.read_bytes() != data: + staged = path.with_name(f".{file.name}.part") + staged.write_bytes(data) + staged.replace(path) + return path diff --git a/src/toad/client/message_files.py b/src/toad/client/message_files.py new file mode 100644 index 000000000..50f57cbfd --- /dev/null +++ b/src/toad/client/message_files.py @@ -0,0 +1,169 @@ +"""The files a message carries, in Toad: what a message composer attaches and sends, and what +a history draws under each message. No toolkit here. + +Attaching: the attach action opens the picker the `ui.input.file_picker` setting names. Each +frontend answers only its pickers and how it draws the attached files: Qt's composer opens a +file dialog (on Android, the system's own file picker) or its path search, and Textual's +prompt opens its terminal pickers. A picked file becomes the upload Core checks again. The +files ride the submission with the text, and a send that does not land returns them to the +composer with its draft. + +A history draws the files under each message through toad.client.message_file_links. +""" + +from __future__ import annotations + +import asyncio +import mimetypes +from abc import abstractmethod +from pathlib import Path + +from agent_comms.messaging.message_files import ( + MAX_MESSAGE_FILE_BYTES, + FileUpload, +) +from agent_comms.nominal.declared_choice import DeclaredChoice, TitledChoice +from formbridge.operations import ExclusiveKind, OperationKind + +from toad.client.input_decision import InputAccepted, InputDecision +from toad.client.message_file_links import file_size +from toad.client.prompt_composer import PromptComposer + + +class FilePicker(DeclaredChoice, affix="FilePicker"): + """Which picker the attach action opens; each member names the composer's own.""" + + @classmethod + @abstractmethod + def open(cls, composer: MessagePromptComposer) -> None: ... + + +class WindowFilePicker(TitledChoice, FilePicker): + """The platform's file dialog. A phone shows its own file picker, which reaches the + phone's storage and its cloud drives.""" + + title = "File dialog" + + @classmethod + def open(cls, composer: MessagePromptComposer) -> None: + composer.pick_in_window() + + +class PathSearchFilePicker(TitledChoice, FilePicker): + """The path search @ completion uses, over the project's files.""" + + title = "Project path search" + + @classmethod + def open(cls, composer: MessagePromptComposer) -> None: + composer.pick_by_path_search() + + +class FileSearch(OperationKind, ExclusiveKind): + """A path search picker listing or ranking the project's paths; a newer one replaces it.""" + + +class AttachedFileChip: + """One attached file as its composer draws it: its caption, and taking it off. Qt's and + Textual's chip widgets inherit it; a plain mixin, as the toolkits' metaclasses need.""" + + composer: MessagePromptComposer + position: int + + def show_file(self, composer: MessagePromptComposer, position: int) -> None: + self.composer, self.position = composer, position + + @property + def caption(self) -> str: + """Named apart from the toolkits' own: Textual's Button renders `label`.""" + return self.composer.chip_label(self.composer.attached_files[self.position]) + + def take_off(self) -> None: + """Named apart from the toolkits' own: Textual's `Widget.remove` leaves the DOM.""" + self.composer.detach_file(self.position) + + +class MessagePromptComposer(PromptComposer): + """A message composer: always the agent glyph, every input accepted, and the files it + sends with the text.""" + + attached_files: tuple[FileUpload, ...] = () + + @property + def input_decision(self) -> InputDecision: + """A message is accepted whether or not an agent is attached: its submission owner + refuses a send it cannot make. An agent prompt asks its owner instead.""" + return InputAccepted() + + def prompt_symbol(self, text: str, shell_mode: bool) -> str: + return self.settings.ui.input.agent_symbol + + def attach_file(self) -> None: + """The attach action: the picker this composer's settings name opens.""" + self.settings.ui.input.file_picker.open(self) + + @abstractmethod + def pick_in_window(self) -> None: + """Open this frontend's file dialog; what it returns reaches `file_picked`.""" + + @abstractmethod + def pick_by_path_search(self) -> None: + """Open this frontend's path search; what it returns reaches `file_picked`.""" + + @abstractmethod + def show_attached(self) -> None: + """Draw `attached_files` beside the editor, each with a way to take it off.""" + + @property + def read_limit(self) -> int: + """The most bytes a picker reads of one file: one past what still fits, so an + oversized file is refused below without being read whole.""" + return MAX_MESSAGE_FILE_BYTES - sum(upload.size for upload in self.attached_files) + 1 + + async def attach_path(self, path: Path) -> None: + """A picker chose a file on this machine: read at most `read_limit` of it and attach it.""" + limit = self.read_limit + + def read() -> bytes: + with path.open("rb") as source: + return source.read(limit) + + try: + content = await asyncio.to_thread(read) + except OSError as error: + self.show_failure(f"{path.name} not attached: {error.strerror or error}") + return + self.file_picked(path.name, content) + + def file_picked(self, name: str, content: bytes, mime_type: str = "") -> None: + """A picker returned a file: attach it, or say why it cannot go with this message.""" + try: + upload = FileUpload.of( + name, mime_type or mimetypes.guess_type(name)[0] or "application/octet-stream", content) + FileUpload.require_bounded((*self.attached_files, upload)) + except ValueError as error: + self.show_failure(f"{name} not attached: {error}") + return + self.attached_files = (*self.attached_files, upload) + self.show_attached() + + def detach_file(self, position: int) -> None: + files = self.attached_files + self.attached_files = files[:position] + files[position + 1:] + self.show_attached() + + def take_files(self) -> tuple[FileUpload, ...]: + """The files the message being sent carries; the composer holds none after.""" + files, self.attached_files = self.attached_files, () + self.show_attached() + return files + + def restore(self, draft) -> None: + """A send that did not land returns its files with its text.""" + super().restore(draft) + self.attached_files = (*self.attached_files, *draft.files) + self.show_attached() + + @staticmethod + def chip_label(upload: FileUpload) -> str: + return f"{upload.name} · {file_size(upload.size)}" diff --git a/src/toad/client/project_paths.py b/src/toad/client/project_paths.py new file mode 100644 index 000000000..d8a0883ce --- /dev/null +++ b/src/toad/client/project_paths.py @@ -0,0 +1,51 @@ +"""A path search over a project's paths, with no toolkit: Textual's PathSearch (the @ +completion and its file picker) and Qt's QtPathSearch inherit it and draw what it answers. + +The listing is what git does not ignore under the root, each path spelled relative to it (a +directory with a trailing slash), shallow paths first, and ranked against what was typed +by `PathSearchRanking`. +""" + +from __future__ import annotations + +import asyncio +from pathlib import Path + +from toad import directory +from toad.path_filter import PathFilter +from toad.path_search_ranking import PathSearchRanking + + +class ProjectPathSearch: + """One project root's listing and its ranking. A plain mixin: Textual's widget + metaclass refuses an ABC base and Qt's shiboken metaclass clashes with one.""" + + root: Path + ranking: PathSearchRanking + path_filter: PathFilter | None = None + + def start_path_search(self, root: Path) -> None: + self.root = root + self.ranking = PathSearchRanking() + + async def scan_paths(self, *, add_directories: bool) -> list[Path]: + """Every path git does not ignore under the root, absolute.""" + root = self.root + self.path_filter = await asyncio.to_thread(PathFilter.from_git_root, root) + paths = await directory.scan(root, path_filter=self.path_filter, add_directories=add_directories) + return await asyncio.to_thread(lambda: [path.absolute() for path in paths]) + + def spell_paths(self, paths: list[Path]) -> list[str]: + """Each path relative to the root, a directory with a trailing slash, shallow first.""" + spellings = sorted( + (str(path.relative_to(self.root)) + ("/" if path.is_dir() else "") for path in paths), + key=str.lower) + spellings.sort(key=lambda spelling: spelling.count("/")) + return spellings + + async def rank_paths(self, spellings: list[str]) -> None: + """Rank these spellings from now on.""" + await self.ranking.update_paths(spellings) + + async def ranked(self, text: str) -> list[str]: + return [match.path for match in await self.ranking.search(text)] diff --git a/src/toad/client/prompt_composer.py b/src/toad/client/prompt_composer.py index ae6c2209a..75e0b6e5b 100644 --- a/src/toad/client/prompt_composer.py +++ b/src/toad/client/prompt_composer.py @@ -10,9 +10,10 @@ from abc import abstractmethod +from agent_comms.messaging.message_files import FileUpload from metaclass_registry import AbstractHooksMeta -from toad.client.input_decision import InputAccepted, InputDecision +from toad.client.input_decision import InputDecision from toad.setting_declarations import EachOpenWidget @@ -84,6 +85,22 @@ def prompt_symbol(self, text: str, shell_mode: bool) -> str: return symbols.shell_symbol return symbols.multiline_symbol if "\n" in text or "```" in text else symbols.agent_symbol + @abstractmethod + def show_failure(self, text: str) -> None: + """Say, under the input, why something typed or attached did not go.""" + + @abstractmethod + def restore_draft(self, text: str) -> None: + """Join returned text to what is typed (`restored_suffix`).""" + + def restore(self, draft) -> None: + """A send that did not land comes back as the draft.""" + self.restore_draft(draft.source_text) + + def take_files(self) -> tuple[FileUpload, ...]: + """The files the input being sent carries: an agent prompt attaches none.""" + return () + @staticmethod def restored_suffix(current: str, text: str) -> str: """What a send that did not land appends to the draft: nothing when the draft @@ -93,19 +110,6 @@ def restored_suffix(current: str, text: str) -> str: return ("\n\n" if current else "") + text -class MessagePromptComposer(PromptComposer): - """A message composer: always the agent glyph, and every input is accepted.""" - - @property - def input_decision(self) -> InputDecision: - """A message is accepted whether or not an agent is attached: its submission owner - refuses a send it cannot make. An agent prompt asks its owner instead.""" - return InputAccepted() - - def prompt_symbol(self, text: str, shell_mode: bool) -> str: - return self.settings.ui.input.agent_symbol - - class ComposerStatus: """The status the info bar shows: why compose is held, else the thread's own status from its owner (Core's configuration answer for a CLI thread, the context measurement diff --git a/src/toad/client/submission.py b/src/toad/client/submission.py index cac08a99c..706a472d9 100644 --- a/src/toad/client/submission.py +++ b/src/toad/client/submission.py @@ -9,6 +9,7 @@ from typing import ClassVar from agent_comms.messaging.channel_targets import NamedTarget +from agent_comms.messaging.message_files import FileUpload from agent_comms.messaging.message_reference import MessageReference from agent_comms.transport.request import CoreAccess @@ -23,9 +24,11 @@ async def prepare(self): ... class SubmittedText(ABC): - """Text the owner submitted, exactly as typed; its request sends it stripped.""" + """Text the owner submitted, exactly as typed, and the files attached to it; its request + sends the text stripped.""" source_text: str + files: tuple[FileUpload, ...] @property def text(self) -> str: diff --git a/src/toad/client/transcript_fragments.py b/src/toad/client/transcript_fragments.py index c351b8410..651e2035b 100644 --- a/src/toad/client/transcript_fragments.py +++ b/src/toad/client/transcript_fragments.py @@ -18,6 +18,7 @@ from functools import cached_property, partial from typing import ClassVar +from agent_comms.messaging.message_reference import MessageReference from agent_comms.messaging.sent_message import PublishedSentMessage from agent_comms.nominal.field_codec import ToolCallId from agent_comms.storage.recorded_time import EventTime @@ -43,6 +44,7 @@ PartPosition, Shown, ) +from toad.client.message_file_links import NO_PAGE_FILES, FileReader, PageFiles from toad.message_rows import MessageRow from toad.client.line_blocks import ( AgentRole, @@ -364,12 +366,13 @@ class SentSlot(BuiltSlot): report: SentTranscript classes: FragmentClasses + files: PageFiles def sent_reports(self) -> tuple[SentTranscript, ...]: return (self.report,) def fragments(self, resolved: ResolvedSent) -> tuple[TranscriptFragment, ...]: - builder = TranscriptFragmentConsumer(self.classes) + builder = TranscriptFragmentConsumer(self.classes, self.files) builder.project_value(resolved[self.report]) return builder.unresolved() @@ -426,13 +429,35 @@ def unresolved(self) -> tuple[TranscriptFragment, ...]: return self.fragments({}) +class BusMessageRow(MroProjection[tuple[MessageRow, ...]]): + """The bus message a transcript event draws, as its row: a received message read by its + recipient, a sent one once resolved read by the recipient it was published to. Every + other event, a sent report not yet resolved included, draws none.""" + + @handles(IncomingTranscript) + def incoming(self, event: IncomingTranscript) -> tuple[MessageRow, ...]: + return (MessageRow(event.message, event.reader),) + + @handles(PublishedSentMessage) + def published(self, message: PublishedSentMessage) -> tuple[MessageRow, ...]: + return (MessageRow(message.original, message.scope.recipient.name),) + + @handles(TranscriptEvent) + def other(self, event: TranscriptEvent) -> tuple[MessageRow, ...]: + return () + + +BUS_MESSAGE_ROW = BusMessageRow() + + class TranscriptFragmentConsumer(MroProjection[None]): """Build a frontend's fragment family from a stream of transcript events. The frontend picks which subclasses to construct by passing its `FragmentClasses`; Textual passes `TEXTUAL_FRAGMENT_CLASSES` from `widgets/transcript_fragments.py`, Qt passes its own.""" - def __init__(self, classes: FragmentClasses) -> None: + def __init__(self, classes: FragmentClasses, files: PageFiles) -> None: self.classes = classes + self.files = files self.slots: list[FragmentSlot] = [] self.tools: dict[str, ToolSlot] = {} self.budget = RenderBudget() @@ -448,20 +473,22 @@ def context(self, event: ContextTranscript) -> None: @handles(IncomingTranscript) def incoming(self, event: IncomingTranscript) -> None: - self.text(event, self.classes.bus_message, row=MessageRow(event.message, event.reader)) + (row,) = BUS_MESSAGE_ROW.project_value(event) + self.text(event, self.classes.bus_message, row=row, files=self.files.of(row.message.reference)) @handles(SentTranscript) def sent(self, event: SentTranscript) -> None: - self.slots.append(SentSlot(self.classes.transcript((event,)), event, self.classes)) + self.slots.append(SentSlot(self.classes.transcript((event,)), event, self.classes, self.files)) @handles(PublishedSentMessage) def published(self, message: PublishedSentMessage) -> None: - original = message.original + (row,) = BUS_MESSAGE_ROW.project_value(message) self.text( message.message, self.classes.sent_message, publication=message, - row=MessageRow(original, message.scope.recipient.name), + row=row, + files=self.files.of(row.message.reference), ) @handles(UserTranscript, MarkdownTranscript) @@ -492,8 +519,9 @@ def read( *, resolve_sent: Callable[[Iterable[SentTranscript]], ResolvedSent], classes: FragmentClasses, + files: PageFiles, ) -> FragmentPlan: - consumer = TranscriptFragmentConsumer(classes) + consumer = TranscriptFragmentConsumer(classes, files) for event in events: consumer.project_value(event) resolved = resolve_sent(report for slot in consumer.slots for report in slot.sent_reports()) @@ -504,7 +532,7 @@ def for_draft(cls, draft, identity, classes: FragmentClasses) -> FragmentPlan: """Core's in-progress CLI draft, read as committed events are, its sent reports resolved against the read identity's wire frontier.""" return cls.read(draft.events, resolve_sent=identity.wire_frontier.resolve_sent_messages, - classes=classes) + classes=classes, files=NO_PAGE_FILES) def fragments(self) -> tuple[TranscriptFragment, ...]: return tuple(fragment for slot in self.slots for fragment in slot.fragments(self.resolved)) @@ -515,5 +543,20 @@ def transcript_fragments( *, resolve_sent: Callable[[Iterable[SentTranscript]], ResolvedSent], classes: FragmentClasses, + files: PageFiles, ) -> tuple[TranscriptFragment, ...]: - return FragmentPlan.read(events, resolve_sent=resolve_sent, classes=classes).fragments() + return FragmentPlan.read(events, resolve_sent=resolve_sent, classes=classes, files=files).fragments() + + +def transcript_file_readers( + events: tuple[TranscriptEvent, ...], + *, + resolve_sent: Callable[[Iterable[SentTranscript]], ResolvedSent], + classes: FragmentClasses, +) -> tuple[tuple[MessageReference, FileReader], ...]: + """Every bus message these events draw once their sent reports resolve, with the read its + files are listed under: what one page's file read asks before the page is drawn.""" + plan = FragmentPlan.read(events, resolve_sent=resolve_sent, classes=classes, files=NO_PAGE_FILES) + return tuple((row.message.reference, row.file_reader) + for drawn in (*events, *plan.resolved.values()) + for row in BUS_MESSAGE_ROW.project_value(drawn)) diff --git a/src/toad/client/transcript_reader.py b/src/toad/client/transcript_reader.py index 0ebffd260..87d8646cf 100644 --- a/src/toad/client/transcript_reader.py +++ b/src/toad/client/transcript_reader.py @@ -13,6 +13,7 @@ from __future__ import annotations import asyncio +from collections.abc import Callable, Iterable from functools import partial from agent_comms.acp_extension import TranscriptDraftUpdate, TranscriptSnapshotUpdate @@ -22,10 +23,12 @@ PageLimit, PagePosition, ) +from agent_comms.messaging.message_reference import MessageReference from agent_comms.registry.thread_presentation import ThreadPresentation from agent_comms.transcript.transcripts import ( LivePageSource, PageSource, + TranscriptPage, ) from agent_comms.transport.tools.sidebar import CommsThreadPresentationTool from agent_comms.ui_model.observation import ( @@ -36,6 +39,7 @@ UnreadableHistory, ) +from toad.client.message_file_links import FileReader, PageFiles from toad.client.transcript_requests import TranscriptRequests @@ -66,6 +70,16 @@ async def thread_presentation( partial(core.call, CommsThreadPresentationTool(thread=thread)), ) + async def page_files( + self, page: TranscriptPage, + readers: Callable[[], Iterable[tuple[MessageReference, FileReader]]], + ) -> PageFiles: + """One read of the files the page's bus messages carry, on the bus the page was read + from; the messages are found and read off the UI thread, like the page itself.""" + root = page.snapshot.root + core = await self.core(root) + return await self.access.preparation.run_thread(lambda: PageFiles.read(root, core, readers())) + async def publication( self, update: TranscriptSnapshotUpdate ) -> TranscriptSnapshotUpdate: diff --git a/src/toad/conversation_submission.py b/src/toad/conversation_submission.py index ebe680f19..116e3012b 100644 --- a/src/toad/conversation_submission.py +++ b/src/toad/conversation_submission.py @@ -15,6 +15,7 @@ QueueScope, SteerPromptRequest, ) +from agent_comms.messaging.message_files import FileUpload from agent_comms.nominal.declared_family import DeclaredFamily from toad.acp.agent import Agent @@ -34,6 +35,7 @@ @dataclass(frozen=True) class InputSubmission(SubmittedText, DeclaredFamily, affix="InputSubmission"): source_text: str + files: tuple[FileUpload, ...] priority: ClassVar[int] = 0 @classmethod @@ -50,7 +52,7 @@ def from_event(cls, event: UserInputSubmitted, owner: ConversationSubmissions): for kind in sorted(cls.members_with(InputSubmission), key=lambda kind: kind.priority, reverse=True) if kind.matches(event, owner) ) - return kind(event.body) + return kind(event.body, event.files) class NoInputSubmission(InputSubmission): diff --git a/src/toad/core/input_events.py b/src/toad/core/input_events.py index f8838bb19..8fbad3a33 100644 --- a/src/toad/core/input_events.py +++ b/src/toad/core/input_events.py @@ -3,6 +3,8 @@ from dataclasses import dataclass from typing import Literal +from agent_comms.messaging.message_files import FileUpload + from toad.core.events import CoreEvent from toad.goal_controls import GoalInteraction from toad.input_history import InputHistory @@ -28,6 +30,8 @@ class UserInputSubmitted(CoreEvent): shell: bool = False auto_complete: bool = False immediate: bool = False + files: tuple[FileUpload, ...] = () + """What the message composer attached; an agent prompt attaches none.""" @dataclass(frozen=True) diff --git a/src/toad/delivery_failure_view.py b/src/toad/delivery_failure_view.py index e85134a29..25abe2ba4 100644 --- a/src/toad/delivery_failure_view.py +++ b/src/toad/delivery_failure_view.py @@ -55,15 +55,15 @@ async def refresh_composer(self, read_only: bool) -> None: if not area.disabled and self.screen.focused is None: self.screen.set_focus(area) - def delivery_unknown(self, description: str, body: str) -> None: - """The request may have reached Core. The owner sees whether it landed in this view, - so the draft comes back instead of waiting on an answer it cannot get.""" - self.prompt.restore_draft(body) - self.feedback.flash(self.unknown_notice(description), style="error") - - def delivery_refused(self, description: str, body: str) -> None: - self.prompt.restore_draft(body) - self.feedback.flash(self.refusal_notice(description), style="error") + @property + def submission_composer(self): + """The prompt a returned draft goes back into. A send whose outcome is unknown comes + back too: the owner sees whether it landed in this view, rather than waiting on an + answer it cannot get.""" + return self.prompt + + def show_send_failure(self, text: str) -> None: + self.feedback.flash(text, style="error") def record_input(self, text: str) -> None: InputHistoryRecord.run_on(self, partial(self.input_histories.prompt.record, text)) diff --git a/src/toad/link_opening.py b/src/toad/link_opening.py index 427841681..efc4b0466 100644 --- a/src/toad/link_opening.py +++ b/src/toad/link_opening.py @@ -11,6 +11,9 @@ from __future__ import annotations from abc import abstractmethod +from collections.abc import Awaitable, Callable +from functools import partial +from pathlib import Path from agent_comms.messaging.display_references import ( ChannelDisplayReference, @@ -20,6 +23,7 @@ from metaclass_registry.mro_dispatch import MroDispatch, handles from toad.client.line_blocks import LineLink, OpenHistoricalTarget, OpenReference, OpenTarget +from toad.client.message_file_links import MessageFileLink from toad.navigation_target import NavigationTarget from toad.project_link import ProjectLink @@ -55,6 +59,22 @@ def open_recorded_reference(self, reference) -> None: def open_project_link(self, link: ProjectLink) -> None: self.preview_link(link) + @handles(MessageFileLink) + def open_message_file(self, link: MessageFileLink) -> None: + self.run_link(partial(self.fetch_message_file, link)) + + async def fetch_message_file(self, link: MessageFileLink) -> None: + """Read the file from the bus its message is on, keep it, and open it; a refusal or a + lost connection is told to the reader.""" + access = self.app.coordination_access + try: + path = await access.preparation.run_thread( + lambda: link.fetched(access.observing(link.root).core)) + except (ValueError, OSError) as error: + self.warn_link(f"{link.file.name} could not be read: {error}") + return + self.show_file(path) + def open_link(self, href: str) -> None: """A drawn link hands its href back as a string; decode it once into the link it carries.""" self.dispatch_sync(LineLink.from_href(href)) @@ -76,3 +96,11 @@ def preview_link(self, link: ProjectLink) -> None: @abstractmethod def warn_link(self, text: str) -> None: """Tell the reader why a link opened nothing.""" + + @abstractmethod + def run_link(self, work: Callable[[], Awaitable[None]]) -> None: + """Run a link's background work as this view's; its failure reaches the view's owner.""" + + @abstractmethod + def show_file(self, path: Path) -> None: + """Open a file kept on this machine the way this frontend opens files.""" diff --git a/src/toad/message_rows.py b/src/toad/message_rows.py index a25016847..5782e4275 100644 --- a/src/toad/message_rows.py +++ b/src/toad/message_rows.py @@ -28,6 +28,7 @@ from agent_comms.storage.recorded_time import EventTime, RecordedEventTime, UnrecordedEventTime from metaclass_registry.mro_dispatch import handles +from toad.client.message_file_links import FileReader from toad.core.projection import MroProjection from toad.inline_spans import inline_spans from toad.client.line_blocks import ( @@ -132,6 +133,22 @@ def recorded(self, source: HistorySource, name: str) -> LineLink: THREAD_LINK = ThreadLink() +class MessageFileReader(MroProjection[FileReader]): + """Whose read a message's files are, from where it was sent: a channel message is read in + its channel; a direct message in the DM with the participant that is not its reader.""" + + @handles(ChannelTarget) + def channel(self, target: ChannelTarget, row: MessageRow) -> FileReader: + return FileReader(target.name, row.reader) + + @handles(ThreadTarget) + def direct(self, target: ThreadTarget, row: MessageRow) -> FileReader: + return FileReader(target.name if row.outgoing else row.message.sender, row.reader) + + +MESSAGE_FILE_READER = MessageFileReader() + + @dataclass(frozen=True) class MessageRow(MroProjection[LineLink]): """One message: who sent it to whom, when, its text, and whether the reader's thread sent it.""" @@ -172,6 +189,11 @@ def thread_link(self, target: ThreadTarget) -> LineLink: def channel_link(self, target: ChannelTarget) -> LineLink: return OpenTarget(target.name) + @property + def file_reader(self) -> FileReader: + """The read this message's files are listed and fetched under.""" + return MESSAGE_FILE_READER.project_value(CommandTarget.named(self.message.target), self) + @property def sender(self) -> LineLink: """What opens the thread that sent the message.""" diff --git a/src/toad/preferences.py b/src/toad/preferences.py index 3923961e8..4822886e4 100644 --- a/src/toad/preferences.py +++ b/src/toad/preferences.py @@ -56,6 +56,7 @@ TopNavigationBar, ) from toad.setting_declarations import FamilyChoices, SettingsDeclaration +from toad.client.message_files import FilePicker, WindowFilePicker from toad.client.prompt_composer import PromptSettingEffects from toad.client.view_settings import ConversationSettingsOwner, ViewSettingsOwner from toad.terminal_origin import TerminalLayout @@ -91,6 +92,11 @@ class InputSettings(SettingsDeclaration, scopes=(ConversationSettingsOwner,)): ] = "$" """Shown before a shell command.""" + file_picker: Annotated[ + type[FilePicker], FieldLabel("File picker"), FamilyChoices(FilePicker), WireName("file-picker"), + ] = WindowFilePicker + """What attaching a file to a message opens: the file dialog (on a phone, its own file picker) or the project path search.""" + @dataclass(frozen=True) class ViewUiSettings(SettingsDeclaration, scopes=(ViewSettingsOwner,)): diff --git a/src/toad/prompt_actions.py b/src/toad/prompt_actions.py index 5fa8b9ea5..1c59bad23 100644 --- a/src/toad/prompt_actions.py +++ b/src/toad/prompt_actions.py @@ -5,6 +5,7 @@ from toad.widget_actions import Action, EngineKey from toad.core.input_events import UserInputSubmitted +from agent_comms.messaging.message_files import FileUpload from typing import TYPE_CHECKING @@ -53,6 +54,20 @@ async def apply(self, editor: PromptTextArea) -> None: editor.prompt.shell_mode = True +class MessageAction(Action, affix="Action"): + """Keys only a message composer's editor answers: an agent prompt binds none of them.""" + + +class AttachFileAction(MessageAction): + """Attach a file to the message: the picker the file picker setting names opens.""" + + keys = {TextEntryMode: ("ctrl+t",)} + title = "Attach" + + async def apply(self, editor: PromptTextArea) -> None: + editor.prompt.attach_file() + + class SubmitInputAction(PromptAction, declared_name=None): """Submission shares eligibility, while each command owns how it sends.""" priority = True @@ -66,8 +81,8 @@ async def apply(self, editor: PromptTextArea) -> None: def coalesce(self, following: SubmitInputAction) -> SubmitInputAction: return following - def submission_event(self, text: str, shell: bool) -> UserInputSubmitted: - return UserInputSubmitted(text, shell) + def submission_event(self, text: str, shell: bool, files: tuple[FileUpload, ...]) -> UserInputSubmitted: + return UserInputSubmitted(text, shell, files=files) class SubmitAction(SubmitInputAction): @@ -90,8 +105,8 @@ def available(self, editor): def coalesce(self, following: SubmitInputAction) -> SubmitInputAction: return self - def submission_event(self, text: str, shell: bool) -> UserInputSubmitted: - return UserInputSubmitted(text, shell, immediate=True) + def submission_event(self, text: str, shell: bool, files: tuple[FileUpload, ...]) -> UserInputSubmitted: + return UserInputSubmitted(text, shell, immediate=True, files=files) class CopyOrClearInputAction(PromptAction): diff --git a/src/toad/qt/composer.py b/src/toad/qt/composer.py index f707ba49b..51e06fa8b 100644 --- a/src/toad/qt/composer.py +++ b/src/toad/qt/composer.py @@ -6,7 +6,9 @@ from __future__ import annotations from abc import ABC, abstractmethod +from collections.abc import Callable from dataclasses import dataclass +from typing import ClassVar from agent_comms.messaging.mentions import MentionCandidate from agent_comms.messaging.message_reference import MessageReference @@ -35,7 +37,8 @@ from toad.client.commands import ConversationCommands from toad.client.line_blocks import FailureRole, LineRole, MutedRole from toad.client.mention_completion import MentionCompletion, MentionEditor -from toad.client.prompt_composer import MessagePromptComposer, PromptComposer, PromptSettingEffects +from toad.client.message_files import AttachedFileChip, MessagePromptComposer +from toad.client.prompt_composer import PromptComposer, PromptSettingEffects from toad.client.condensed_path import home_relative from toad.key_modes import Standard, VimInsert from pyqt_reactive.protocols.widget_adapters import PyQtWidgetMeta @@ -47,7 +50,15 @@ UncheckedNotifications, ) from toad.client.subscribers import NotificationSubscriber -from toad.prompt_actions import NewlineAction, SubmitAction, SubmitInputAction, SubmitNowAction +from toad.prompt_actions import ( + AttachFileAction, + MessageAction, + NewlineAction, + PromptAction, + SubmitAction, + SubmitInputAction, + SubmitNowAction, +) from toad.widget_actions import DismissAction from toad.widget_contracts import ( DismissibleContract, @@ -56,6 +67,7 @@ SubmitsContract, ) from toad.qt.composer_vim import ComposerVimMode, apply_normal_key +from toad.qt.file_pickers import open_file_dialog, open_path_search from toad.qt.lines import QtLineRenderer from toad.qt.queue_ui import QtDeliveryControls, QtQueueInspection, QtQueueSummary from toad.qt.prompt_stack import QtInfoBar @@ -64,25 +76,13 @@ from toad.qt.metrics import UiGap -# The declared TextEntryMode key spellings each prompt action exposes drive the Qt -# composer's QShortcuts. Keeping the composer method as the callback (not the action's -# own .apply, which expects a Textual `PromptTextArea`) lets both backends consume the -# same key declarations without Qt dragging Textual's PromptSubmission chassis. -_COMPOSER_ACTION_METHODS = ( - (SubmitAction, "submit"), - (SubmitNowAction, "submit_now"), - (DismissAction, "dismiss_control"), - (NewlineAction, "_insert_newline"), -) - - def _bind_composer_shortcuts(composer: Composer): """One `QShortcut` per composer action on the editor, answering the action's keys in the current key mode and keymap (`ActionKeys`) and running the composer's own method while the editor (or a child) has focus. Textual reaches these methods through its own PromptTextArea action system — the keys live in one place.""" - return [composer.host.app.action_keys.bind(composer.editor, action_cls(), getattr(composer, method_name)) - for action_cls, method_name in _COMPOSER_ACTION_METHODS] + return [composer.host.app.action_keys.bind(composer.editor, action_cls(), method.__get__(composer)) + for action_cls, method in composer.action_methods] class ComposerHost(ConversationCommands): @@ -305,6 +305,13 @@ class Composer(PromptSettingEffects, PromptComposer, MentionEditor, SubmitsContr tap runs `submit`: one send path, two triggers. The shared action families bind to it through `SubmitsContract` + `DismissibleContract`.""" + # The declared TextEntryMode key spellings each prompt action exposes drive the Qt + # composer's QShortcuts. Keeping the composer method as the callback (not the action's + # own .apply, which expects a Textual `PromptTextArea`) lets both backends consume the + # same key declarations without Qt dragging Textual's PromptSubmission chassis. A kind + # of composer adds the actions only it answers. + action_methods: ClassVar[tuple[tuple[type[PromptAction | MessageAction], Callable[..., None]], ...]] + def __init__(self, host: ComposerHost, renderer: QtLineRenderer, placeholder: str) -> None: super().__init__() self.host, self.renderer = host, renderer @@ -506,7 +513,7 @@ def send(self, action: SubmitInputAction = SubmitAction()) -> None: self.editor.clear() self.dismissed_text = "" self.failure.hide() - self.host.submit(TypedText(text), action) + self.host.submit(TypedText(text, self.take_files()), action) # Toolkit-neutral action-family contract: Textual's Prompt inherits Submits + # DismissibleControl from widget_actions.py; the mixin lift itself is fork @@ -587,11 +594,66 @@ def labelled(self, label: str) -> None: """The view's label changed; a channel's composer keeps the placeholder its kind gave it.""" -class ChannelComposer(MessagePromptComposer, Composer): +Composer.action_methods = ( + (SubmitAction, Composer.submit), + (SubmitNowAction, Composer.submit_now), + (DismissAction, Composer.dismiss_control), + (NewlineAction, Composer._insert_newline), +) + + +class QtAttachedFileChip(AttachedFileChip, QPushButton): + """An attached file as a button over the editor; tapping it takes the file off.""" + + def __init__(self, composer: QtMessageComposer, position: int) -> None: + super().__init__(composer.attached_row) + self.show_file(composer, position) + self.setText(f"{self.caption} ✕") + self.setFocusPolicy(Qt.FocusPolicy.NoFocus) + self.setToolTip("Remove this file") + self.clicked.connect(self.take_off) + + +class QtMessageComposer(MessagePromptComposer, Composer): + """A bus message's composer on Qt: the shared message composer, an Attach button beside + Send, the attached files as chips over the editor, and Qt's two pickers.""" + + action_methods = (*Composer.action_methods, (AttachFileAction, MessagePromptComposer.attach_file)) + + def __init__(self, host: ComposerHost, renderer: QtLineRenderer, placeholder: str) -> None: + super().__init__(host, renderer, placeholder) + self.attach_button = QPushButton(AttachFileAction.title, self) + self.attach_button.setFocusPolicy(Qt.FocusPolicy.NoFocus) # tapping keeps the keyboard up + self.attach_button.clicked.connect(self.attach_file) + self.frame_row.insertWidget(1, self.attach_button, 0, Qt.AlignmentFlag.AlignBottom) + self.attached_row = QWidget(self) + chips = QHBoxLayout(self.attached_row) + chips.setContentsMargins(UiGap.frame, 0, UiGap.frame, 0) + chips.setAlignment(Qt.AlignmentFlag.AlignLeft) + layout = self.layout() + layout.insertWidget(layout.indexOf(self.frame_row), self.attached_row) + self.attached_row.hide() + + def pick_in_window(self) -> None: + open_file_dialog(self, self) + + def pick_by_path_search(self) -> None: + open_path_search(self, self, self.host.app.project_dir.path, self.host.app.run_operation) + + def show_attached(self) -> None: + chips = self.attached_row.layout() + while (item := chips.takeAt(0)) is not None: + item.widget().deleteLater() + for position in range(len(self.attached_files)): + chips.addWidget(QtAttachedFileChip(self, position)) + self.attached_row.setVisible(bool(self.attached_files)) + + +class ChannelComposer(QtMessageComposer): """A channel's composer, which offers its members after an @.""" -class MessageComposer(MessagePromptComposer, Composer): +class MessageComposer(QtMessageComposer): """A direct message's composer: it names its peer, as Textual's MessagePrompt does.""" def __init__(self, host: ComposerHost, renderer: QtLineRenderer) -> None: diff --git a/src/toad/qt/conversation.py b/src/toad/qt/conversation.py index dd1afd4eb..6dded3b88 100644 --- a/src/toad/qt/conversation.py +++ b/src/toad/qt/conversation.py @@ -188,7 +188,7 @@ def submit(self, text: TypedText, action: SubmitInputAction) -> None: """The composer's input enters the view's submissions, as Textual's prompt's `UserInputSubmitted` reaches `Conversation.submissions.submit`. No Qt view runs a shell, so typed input is never a shell command.""" - event = action.submission_event(text.text, False) + event = action.submission_event(text.text, False, text.files) self.run_worker(partial(self.submissions.submit, event), group="submit", exclusive=False) def update_slash_commands(self) -> None: diff --git a/src/toad/qt/file_pickers.py b/src/toad/qt/file_pickers.py new file mode 100644 index 000000000..75a8c2091 --- /dev/null +++ b/src/toad/qt/file_pickers.py @@ -0,0 +1,103 @@ +"""Qt's file pickers for a message composer: the file dialog and the project path search. + +The dialog is Qt's own QFileDialog. On Android, Qt answers it with the system's document +picker (the Storage Access Framework), which returns a `content://` URL that QFile reads; +on a desktop it is the platform's dialog. The path search ranks the project's paths through +the shared ProjectPaths, as Textual's @ completion does. Both hand the bytes they read to +`MessagePromptComposer.file_picked`, which decides what is attached. +""" + +from __future__ import annotations + +from collections.abc import Callable +from pathlib import Path + +from PySide6.QtCore import QFile, QFileInfo, QIODevice, Qt, QUrl +from PySide6.QtWidgets import QDialog, QFileDialog, QLineEdit, QListWidget, QVBoxLayout, QWidget + +from toad.client.message_files import FileSearch, MessagePromptComposer +from toad.client.project_paths import ProjectPathSearch +from toad.prompt_actions import AttachFileAction + + +def read_picked(url: QUrl, composer: MessagePromptComposer) -> None: + """Read what a picker chose, up to what the composer can still attach, and attach it.""" + # A local file's path, else the URL itself: QFile opens an Android content:// URL as is. + location = url.toLocalFile() or url.toString() + picked = QFile(location) + if not picked.open(QIODevice.OpenModeFlag.ReadOnly): + composer.show_failure(f"Could not read {location}: {picked.errorString()}") + return + try: + content = bytes(picked.read(composer.read_limit).data()) + finally: + picked.close() + composer.file_picked(QFileInfo(location).fileName(), content) + + +def open_file_dialog(parent: QWidget, composer: MessagePromptComposer) -> QFileDialog: + """Qt's file dialog over ``parent``, attaching the file it returns.""" + dialog = QFileDialog(parent, AttachFileAction.title) + dialog.setFileMode(QFileDialog.FileMode.ExistingFile) + # An accepted dialog holds its one choice: Qt's own dialog emits no urlSelected, and + # Android's document picker answers with a content:// URL, so both are read from here. + dialog.accepted.connect(lambda: [read_picked(url, composer) for url in dialog.selectedUrls()]) + dialog.finished.connect(dialog.deleteLater) + dialog.open() + return dialog + + +class QtPathSearch(ProjectPathSearch, QDialog): + """The project's paths, ranked as you type; choosing one attaches that file.""" + + shown_limit = 100 + + def __init__(self, parent: QWidget, root: Path, chosen: Callable[[Path], None], + run: Callable) -> None: + super().__init__(parent) + self.start_path_search(root) + self.setWindowTitle(AttachFileAction.title) + self.chosen, self.run = chosen, run + self.listing: list[str] = [] + self.query = QLineEdit(self) + self.query.setPlaceholderText(f"Search {root}") + self.results = QListWidget(self) + layout = QVBoxLayout(self) + layout.addWidget(self.query) + layout.addWidget(self.results, 1) + self.query.textChanged.connect(self.search) + self.query.returnPressed.connect(self.choose_current) + self.results.itemActivated.connect(lambda item: self.choose(item.text())) + self.finished.connect(self.deleteLater) + run(FileSearch, self.list_project) + + async def list_project(self) -> None: + self.listing = self.spell_paths(await self.scan_paths(add_directories=False)) + await self.rank_paths(self.listing) + self.show_paths(self.listing) + + def search(self, text: str) -> None: + self.run(FileSearch, lambda: self.show_ranked(text)) + + async def show_ranked(self, text: str) -> None: + self.show_paths(await self.ranked(text) if text else self.listing) + + def show_paths(self, spellings: list[str]) -> None: + self.results.clear() + self.results.addItems(spellings[:self.shown_limit]) + self.results.setCurrentRow(0) + + def choose_current(self) -> None: + if (item := self.results.currentItem()) is not None: + self.choose(item.text()) + + def choose(self, spelling: str) -> None: + self.accept() + self.chosen(self.root / spelling) + + +def open_path_search(parent: QWidget, composer: MessagePromptComposer, root: Path, run: Callable) -> QtPathSearch: + search = QtPathSearch(parent, root, lambda path: run(FileSearch, lambda: composer.attach_path(path)), run) + search.setWindowModality(Qt.WindowModality.WindowModal) + search.open() + return search diff --git a/src/toad/qt/history.py b/src/toad/qt/history.py index 5c1135ee1..e475d17cb 100644 --- a/src/toad/qt/history.py +++ b/src/toad/qt/history.py @@ -21,6 +21,7 @@ from contextlib import asynccontextmanager, contextmanager from dataclasses import dataclass from functools import partial +from pathlib import Path from typing import ClassVar from weakref import WeakSet @@ -848,6 +849,14 @@ def preview_link(self, link: ProjectLink) -> None: def warn_link(self, text: str) -> None: self.history.view.flash_error(text) + def run_link(self, work: Callable[[], Awaitable[None]]) -> None: + self.history.run_worker(work) + + def show_file(self, path: Path) -> None: + """A kept file opens in the application the system names for it.""" + if not QDesktopServices.openUrl(QUrl.fromLocalFile(str(path))): + self.warn_link(f"No application opens {path.name}; it is kept at {path}.") + @handles(FragmentDetails) def toggle_details(self, link: FragmentDetails) -> None: """A tap on a ▶ row opens or closes that fragment's details, as Textual's page does.""" diff --git a/src/toad/qt/submission_view.py b/src/toad/qt/submission_view.py index a8fed6ff1..f9dd1a611 100644 --- a/src/toad/qt/submission_view.py +++ b/src/toad/qt/submission_view.py @@ -11,13 +11,12 @@ class QtSubmissionView(SubmissionView): - def delivery_refused(self, description: str, body: str) -> None: - self.composer.restore_draft(body) - self.composer.show_failure(self.refusal_notice(description)) + @property + def submission_composer(self): + return self.composer - def delivery_unknown(self, description: str, body: str) -> None: - self.composer.restore_draft(body) - self.composer.show_failure(self.unknown_notice(description)) + def show_send_failure(self, text: str) -> None: + self.composer.show_failure(text) composer_block: str = "" diff --git a/src/toad/qt/transcript_history.py b/src/toad/qt/transcript_history.py index 059569c1d..bc89ffcb1 100644 --- a/src/toad/qt/transcript_history.py +++ b/src/toad/qt/transcript_history.py @@ -39,11 +39,11 @@ def empty_page(cls): def _initial_page_source(self, prepared, app): """Bind a `SavedTranscript` over the view's `TranscriptPresentation`. The loader - callable is `view.transcript.get_transcript_page`; the fragment family is + source is `view.transcript`, its pages and their files; the fragment family is `QT_FRAGMENT_CLASSES` (Qt-flavored siblings of the canonical neutral family — same decode, Qt-flavored rendering, no data duplication).""" return SavedTranscript( - self.view.transcript.get_transcript_page, + self.view.transcript, prepared.page.after, app.preparation, fragments=QT_FRAGMENT_CLASSES, diff --git a/src/toad/screens/file_picker.py b/src/toad/screens/file_picker.py new file mode 100644 index 000000000..f9d343046 --- /dev/null +++ b/src/toad/screens/file_picker.py @@ -0,0 +1,111 @@ +"""The terminal's file pickers for a message composer: a file browser that can leave the +project, and the project's path search. Either reads the chosen file through the +composer's shared `attach_path`; Escape closes either and attaches nothing.""" + +from __future__ import annotations + +from abc import abstractmethod +from pathlib import Path + +from formbridge.key_declarations import TextEntryMode +from textual import events, on +from textual.app import ComposeResult +from textual.widgets import DirectoryTree, Input, Label, OptionList +from textual.widgets.option_list import Option + +from toad.client.message_files import MessagePromptComposer +from toad.client.project_paths import ProjectPathSearch +from toad.screens.modal import CenteredScreen, DismissibleScreen, ModalFrame +from toad.widget_actions import Action, NativeMethodKeys + + +class FilePickerScreen(CenteredScreen, DismissibleScreen[None]): + """A picker over the files of one composer's machine, starting at its project.""" + + heading: str + + def __init__(self, composer: MessagePromptComposer, root: Path) -> None: + super().__init__() + self.composer, self.root = composer, root + + def compose(self) -> ComposeResult: + with ModalFrame(): + yield Label(self.heading) + yield from self.compose_picker() + + @abstractmethod + def compose_picker(self) -> ComposeResult: + """The picker's own controls, under its title.""" + + async def attach(self, path: Path) -> None: + """The chosen file goes to the composer; the picker closes first.""" + self.dismiss() + await self.composer.attach_path(path) + + +class FileBrowserAction(Action, affix="Action"): + """Keys the file browser answers.""" + + +class ParentFolderAction(FileBrowserAction): + """Browse the folder above the one shown.""" + + keys = {TextEntryMode: ("backspace",)} + show = True + + def apply(self, tree: FileBrowserTree) -> None: + tree.path = Path(tree.path).parent + + +class FileBrowserTree(NativeMethodKeys, DirectoryTree, actions=(FileBrowserAction,)): + """Every file and folder, not only what git keeps: a file to attach can be anywhere.""" + + +class FileBrowserScreen(FilePickerScreen): + heading = "Attach a file · enter opens or attaches · backspace goes up · escape closes" + + def compose_picker(self) -> ComposeResult: + yield FileBrowserTree(self.root) + + @on(DirectoryTree.FileSelected) + async def file_selected(self, event: DirectoryTree.FileSelected) -> None: + event.stop() + await self.attach(event.path) + + +class PathSearchScreen(ProjectPathSearch, FilePickerScreen): + heading = "Attach a project file · type to search · enter attaches · escape closes" + + def __init__(self, composer: MessagePromptComposer, root: Path) -> None: + super().__init__(composer, root) + self.start_path_search(root) + self.spellings: list[str] = [] + + def compose_picker(self) -> ComposeResult: + yield Input(compact=True, placeholder="fuzzy search") + yield OptionList() + + @on(events.Mount) + async def list_project(self) -> None: + self.query_one(Input).focus() + self.spellings = self.spell_paths(await self.scan_paths(add_directories=False)) + await self.rank_paths(self.spellings) + self.show_paths(self.spellings) + + def show_paths(self, paths: list[str]) -> None: + options = self.query_one(OptionList) + options.set_options([Option(path, id=path) for path in paths[:100]]) + options.highlighted = 0 + + @on(Input.Changed) + async def search(self, event: Input.Changed) -> None: + self.show_paths(await self.ranked(event.value) if event.value else self.spellings) + + @on(Input.Submitted) + def submit(self, event: Input.Submitted) -> None: + """Enter in the search attaches the highlighted path, as choosing it does.""" + self.query_one(OptionList).action_select_cursor() + + @on(OptionList.OptionSelected) + async def option_selected(self, event: OptionList.OptionSelected) -> None: + await self.attach(self.root / event.option.id) diff --git a/src/toad/screens/historical_sessions.py b/src/toad/screens/historical_sessions.py index 71459c74b..dbe8e965e 100644 --- a/src/toad/screens/historical_sessions.py +++ b/src/toad/screens/historical_sessions.py @@ -28,7 +28,8 @@ from toad.widgets.history_anchor import HistoryWindow from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES from toad.widgets.transcript_history import LineHistory -from toad.transcript_preparation import SavedTranscript, TranscriptPageRenderTask +from toad.client.message_file_links import NO_PAGE_FILES +from toad.transcript_preparation import LoadedTranscriptPages, SavedTranscript, TranscriptPageRenderTask class RecordedContents(Contents): @@ -148,10 +149,10 @@ async def load(**kwargs): Static("No saved conversation records are available for this identity.") ) else: - prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES).prepared(self.app.preparation) + prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(self.app.preparation) if generation != self._selection_generation or not self.is_attached: return - history = LineHistory(prepared, SavedTranscript(load, page.after, self.app.preparation, + history = LineHistory(prepared, SavedTranscript(LoadedTranscriptPages(load), page.after, self.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES), notification_view=content, source_state=LiveTranscript()) window = self.query_one(HistoryWindow) diff --git a/src/toad/transcript_preparation.py b/src/toad/transcript_preparation.py index e62014a06..5a692c1ff 100644 --- a/src/toad/transcript_preparation.py +++ b/src/toad/transcript_preparation.py @@ -5,10 +5,16 @@ import asyncio import pickle +from abc import ABC, abstractmethod +from collections.abc import Awaitable, Callable from dataclasses import dataclass, field, replace from hashlib import sha256 from agent_comms.execution.transcript_sources import ( + AtFrontier, + LatestPage, + PageLimit, + PagePosition, TranscriptCursor, ) from agent_comms.nominal.field_codec import ContentExclude, FieldCodec @@ -26,6 +32,7 @@ SourcePageWork, TranscriptNoProgress, ) +from toad.client.message_file_links import NO_PAGE_FILES, PageFiles from toad.client.transcript_fragments import FragmentClasses, transcript_fragments from toad.render_backend import ContentAddressedRenderTask from toad.work_preparation import ( @@ -37,14 +44,46 @@ ) +class TranscriptPages(ABC): + """Where a saved transcript's pages are read, and the files each page's messages carry.""" + + @abstractmethod + async def get_transcript_page( + self, *, position: PagePosition = LatestPage(), limit: PageLimit = AtFrontier(), + ) -> TranscriptPage: ... + + @abstractmethod + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + """One read of the files every bus message on ``page`` carries.""" + + +@dataclass(frozen=True) +class LoadedTranscriptPages(TranscriptPages): + """Pages read by ``load`` with no file store beside them, so a page shows no files: an + attached history's, whose files stay with the bus that recorded them.""" + + load: Callable[..., Awaitable[TranscriptPage]] + + async def get_transcript_page( + self, *, position: PagePosition = LatestPage(), limit: PageLimit = AtFrontier(), + ) -> TranscriptPage: + return await self.load(position=position, limit=limit) + + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + return NO_PAGE_FILES + + @dataclass(frozen=True) class TranscriptPageRenderTask(ReusableStorage[PreparedTranscriptPage], ContentAddressedRenderTask[PreparedTranscriptPage]): page: TranscriptPage classes: FragmentClasses + files: PageFiles def preparation_digest(self, work: RenderPreparation[PreparedTranscriptPage]) -> bytes: - """Read marks and coordination activity cannot change prepared content.""" - return sha256(pickle.dumps(FieldCodec.project(self.page, ContentExclude), protocol=5)).digest() + """Read marks and coordination activity cannot change prepared content; the files its + messages carry do.""" + return sha256(pickle.dumps( + (FieldCodec.project(self.page, ContentExclude), self.files), protocol=5)).digest() def result_size(self, result: PreparedTranscriptPage) -> int: return result.retained_bytes @@ -55,6 +94,7 @@ def execute(self) -> PreparedTranscriptPage: self.page.events, resolve_sent=self.page.resolve_sent_messages, classes=self.classes, + files=self.files, ) return PreparedTranscriptPage.measured(self.page, fragments) @@ -72,12 +112,13 @@ class TranscriptPageWork(SourcePageWork[TranscriptCursor]): async def execute(self, runtime: PreparationRuntime, key: WorkKey) -> PreparedResult[PreparedTranscriptPage]: request = self.request - page = await request.end.load_page(self.loader, request.cursor, self.through) + page = await request.end.load_page(self.loader.get_transcript_page, request.cursor, self.through) if not request.progressed(page): raise TranscriptNoProgress(request, self.through, page) + files = await self.loader.page_files(page, self.classes) if self.scope.closed: raise asyncio.CancelledError - prepared = await TranscriptPageRenderTask(page, self.classes).prepared(runtime) + prepared = await TranscriptPageRenderTask(page, self.classes, files).prepared(runtime) return await runtime.run_thread(self.finish_result, key, prepared) diff --git a/src/toad/transcript_publication.py b/src/toad/transcript_publication.py index c45e37075..802889a90 100644 --- a/src/toad/transcript_publication.py +++ b/src/toad/transcript_publication.py @@ -63,7 +63,9 @@ from toad.session_scope import SessionScope from toad.transcript_scope import NoTranscript, TranscriptScope from toad.entry_placement import ChronologicalEntry, EntryPlacement -from toad.transcript_preparation import SavedSuffix +from toad.client.message_file_links import PageFiles +from toad.client.transcript_fragments import FragmentClasses, transcript_file_readers +from toad.transcript_preparation import SavedSuffix, TranscriptPages from toad.client.prepared_pages import ( FrozenClosedHistory, FrozenSavedPrefix, @@ -113,6 +115,11 @@ def current(self): async def read_page(self, scope, *, position: PagePosition, limit: PageLimit) -> TranscriptPage: return await self.binding.reader.page(scope, position=position, limit=limit) + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + return await self.binding.reader.page_files(page, partial( + transcript_file_readers, page.events, resolve_sent=page.resolve_sent_messages, + classes=classes)) + def after_refresh(self, callback: Callable[[], object]) -> None: self.binding.view.after_refresh(callback) @@ -320,6 +327,10 @@ async def read_page(self, scope, *, position: PagePosition, limit: PageLimit) -> # stands, and the request a new binding makes reads again. raise ReadAgain() + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + """A page read before the view lost its binding is read again, its files with it.""" + raise ReadAgain() + async def publish(self) -> bool: """Nothing to publish: the request is done.""" return True @@ -756,7 +767,7 @@ async def keep_before_later_history(self, owner: TranscriptPresentation, entry: await history.retire_source(parked=True) source = history.page_source prefix = cursor.painted_read( - lambda at: FrozenSavedPrefix(source, owner.get_transcript_page, at, self.view.app.preparation, fragments=source.fragments), + lambda at: FrozenSavedPrefix(source, owner, at, self.view.app.preparation, fragments=source.fragments), FrozenClosedHistory(cursor, self.view.app.preparation), ) prepared = await prefix.latest(history.pages[-1].prepared) @@ -772,7 +783,7 @@ async def keep_before_later_history(self, owner: TranscriptPresentation, entry: )) later = self.view.later_history( PreparedTranscriptPage.empty(UnreadTranscriptPage(before=cursor, after=cursor)), - SavedSuffix(owner.get_transcript_page, cursor, self.view.app.preparation, after=cursor, fragments=source.fragments), + SavedSuffix(owner, cursor, self.view.app.preparation, after=cursor, fragments=source.fragments), entry.conversation_order + 1, ) self._history = later @@ -959,7 +970,7 @@ def commits(self, live_blocks: int, limit: int) -> bool: COMMIT_DEMAND = CommitDemand() -class TranscriptPresentation(EntryPlacement): +class TranscriptPresentation(TranscriptPages, EntryPlacement): """Owns the transcript document; attached native publishers can be replaced.""" def __init__(self) -> None: @@ -1021,6 +1032,9 @@ async def get_transcript_page( raise ValueError("Thread source changed while reading transcript") return page + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + return await self.binding.capture(self, BoundFrame).page_files(page, classes) + @property def retained_source_bytes(self) -> int: return sum(source.retained_source_bytes for source in self.documents) diff --git a/src/toad/widgets/committed_presentation.py b/src/toad/widgets/committed_presentation.py index e1367db65..a91c16361 100644 --- a/src/toad/widgets/committed_presentation.py +++ b/src/toad/widgets/committed_presentation.py @@ -120,7 +120,9 @@ async def read_snapshot[Snapshot]( @classmethod async def capture(cls, view: Conversation, history: TranscriptHistory, page: TranscriptPage, sequences: frozenset[int]) -> Self: - prepared = await TranscriptPageRenderTask(page, history.page_source.fragments).prepared(view.app.preparation) + classes = history.page_source.fragments + files = await view.transcript.page_files(page, classes) + prepared = await TranscriptPageRenderTask(page, classes, files).prepared(view.app.preparation) return cls(history, sequences, prepared) @property diff --git a/src/toad/widgets/linked_rows.py b/src/toad/widgets/linked_rows.py index c10373570..4d43d343f 100644 --- a/src/toad/widgets/linked_rows.py +++ b/src/toad/widgets/linked_rows.py @@ -3,6 +3,7 @@ from __future__ import annotations from abc import abstractmethod +from collections.abc import Awaitable, Callable from functools import cached_property, partial from pathlib import Path from typing import cast @@ -84,6 +85,13 @@ def preview_link(self, link: ProjectLink) -> None: def warn_link(self, text: str) -> None: self.notify(text, severity="warning") + def run_link(self, work: Callable[[], Awaitable[None]]) -> None: + self.run_worker(work(), group="open-link") + + def show_file(self, path: Path) -> None: + """A kept file previews as a project file does.""" + self.app.run_worker(partial(self.app.session_navigation.preview, path, position=None)) + async def link_menu(self, href: str, event: object) -> bool: return await LineLink.from_href(href).copy_menu(self, event) diff --git a/src/toad/widgets/path_search.py b/src/toad/widgets/path_search.py index 1d9950440..aca5e0e76 100644 --- a/src/toad/widgets/path_search.py +++ b/src/toad/widgets/path_search.py @@ -43,13 +43,11 @@ from metaclass_registry.mro_dispatch import handles -from toad import directory from toad.core.events import LinkContextChanged from toad.core_event_carrier import CoreEventReceiver from toad.project_path_owner import ProjectPathOwner -from toad.path_search_ranking import PathSearchRanking +from toad.client.project_paths import ProjectPathSearch from toad.messages import Dismiss, InsertPath, PromptSuggestion -from toad.path_filter import PathFilter from ui_theme.scheme import ForegroundColour from toad.native_style import MutedText, SideMargin, SidePadding @@ -268,7 +266,7 @@ async def apply(self, context: PathSearch) -> None: class PathSearch( - CoreEventReceiver, PriorityActions, HiddenActions, VisibleActions, ForwardsCursor, Submits, CompletionPopup, + ProjectPathSearch, CoreEventReceiver, PriorityActions, HiddenActions, VisibleActions, ForwardsCursor, Submits, CompletionPopup, scopes={HiddenActions: (DismissAction, SubmitAction)}, actions=(SwitchPathPickerAction,), ): @@ -286,23 +284,20 @@ class PathSearch( def __init__(self) -> None: super().__init__() - self.ranking = PathSearchRanking() self._paths_dirty = True @classmethod def for_prompt(cls, prompt: Prompt) -> Self | None: return cls() - @property - def root(self) -> Path: - return ProjectPathOwner.containing(self).project_root - @on(events.Mount) def on_mount(self) -> None: + self.start_path_search(ProjectPathOwner.containing(self).project_root) self.observe_core(ProjectPathOwner.containing(self).link_context_events) @handles(LinkContextChanged) def project_changed(self, event: LinkContextChanged) -> None: + self.start_path_search(ProjectPathOwner.containing(self).project_root) for tree in self.query(ProjectDirectoryTree): tree.path = self.root self.invalidate_paths() @@ -454,27 +449,10 @@ async def refresh_paths(self): self.option_list.set_loading(True) root = self.root try: - path_filter = await asyncio.to_thread(PathFilter.from_git_root, root) + paths = await self.scan_paths(add_directories=True) if tree := self.query_one_optional(ProjectDirectoryTree): - tree.path_filter = path_filter + tree.path_filter = self.path_filter tree.invalidate() - paths = await directory.scan( - root, path_filter=path_filter, add_directories=True - ) - - def make_absolute(paths: list[Path]) -> list[Path]: - """Make all paths absolute. - - Args: - paths: A list of paths. - - Returns: - List of absolute paths, - - """ - return [path.absolute() for path in paths] - - paths = await asyncio.to_thread(make_absolute, paths) if self.root != root: return self.paths = paths @@ -496,20 +474,7 @@ def highlight_path(self, path: str) -> PathContent: @work(description="watch_paths") async def watch_paths(self, paths: list[Path]) -> None: - - def path_display(path: Path) -> str: - is_directory = path.is_dir() - if is_directory: - return str(path.relative_to(self.root)) + "/" - else: - return str(path.relative_to(self.root)) - - def make_display_paths() -> list[str]: - display_paths = sorted(map(path_display, paths), key=str.lower) - display_paths.sort(key=lambda path: path.count("/")) - return display_paths - - self.display_paths = await asyncio.to_thread(make_display_paths) + self.display_paths = await asyncio.to_thread(self.spell_paths, paths) self.option_list.highlighted = None self._update_paths(self.display_paths) @@ -532,5 +497,5 @@ async def _update_paths(self, paths: list[str]) -> None: Args: paths: A list of paths. """ - await self.ranking.update_paths(paths) + await self.rank_paths(paths) self.call_after_refresh(self.option_list.set_loading, False) diff --git a/src/toad/widgets/prompt.py b/src/toad/widgets/prompt.py index 68ed33bce..f7e4438ec 100644 --- a/src/toad/widgets/prompt.py +++ b/src/toad/widgets/prompt.py @@ -35,7 +35,7 @@ from textual.reactive import Initialize, Required, var from textual.strip import Strip from textual.style import Style -from textual.widgets import Label, OptionList, TextArea +from textual.widgets import Button, Label, OptionList, TextArea from textual.widgets.option_list import Option, OptionListComponent from textual.widgets.text_area import Selection, TextAreaComponent from ui_theme.scheme import ErrorColour, MutedColour, PrimaryColour, PromptToneColour, SecondaryColour @@ -46,7 +46,9 @@ from toad.core import input_events from toad.core_event_carrier import CoreEventReceiver from toad.client.input_decision import InputAccepted, InputDecision, RefusalFeedback -from toad.client.prompt_composer import MessagePromptComposer, PromptComposer +from toad.client.message_files import AttachedFileChip, MessagePromptComposer +from toad.screens.file_picker import FileBrowserScreen, PathSearchScreen +from toad.client.prompt_composer import PromptComposer from toad.client.prompt_stack import EditingModeBadge from toad.client.queue_inspection import QueueDeliveryControls from toad.native_style import ( @@ -75,7 +77,7 @@ ) from toad.tone_paint import TonePaint from toad.path_complete import AnyPath, CompletingEditor, DirectoriesOnly, FilesOnly, PathComplete -from toad.prompt_actions import PromptAction, SubmitInputAction, SubmitNowAction +from toad.prompt_actions import MessageAction, PromptAction, SubmitInputAction, SubmitNowAction from toad.app_node import ToadNode from toad.prompt_cursor import ( CommandText, @@ -646,7 +648,7 @@ async def _submit(self, action: SubmitInputAction, mode: EditingMode) -> None: shell = self.shell_mode text = self.text self.clear() - self.publish_core(action.submission_event(text, shell)) + self.publish_core(action.submission_event(text, shell, self.prompt.take_files())) if shell: self.prompt.shell_mode = False @@ -691,7 +693,7 @@ def watch_selection( self.input_cursor.changed(previous_selection, selection) -class MessageTextArea(PromptTextArea): +class MessageTextArea(PromptTextArea, actions=(MessageAction,)): @property def can_enter_shell(self) -> bool: return False @@ -786,6 +788,10 @@ def restore_draft(self, text: str) -> None: editor.insert(suffix, editor.document.end, maintain_selection_offset=True) editor.history.checkpoint() + def show_failure(self, text: str) -> None: + """Where this prompt's conversation tells a send failure.""" + self.conversation.show_send_failure(text) + def watch_agent_binding(self) -> None: if self.is_mounted: self.sync_configuration() @@ -1063,9 +1069,14 @@ def compose_mode_controls(self) -> ComposeResult: yield ModeSwitcher() yield ModeInfo("mode") + def compose_attached(self) -> ComposeResult: + """What the message being written carries; an agent prompt carries nothing.""" + return () + def compose(self) -> ComposeResult: yield from self.compose_completions() yield from self.compose_delivery_controls() + yield from self.compose_attached() with PromptContainer(): yield PromptQuestion() with TextPrompt(): @@ -1101,9 +1112,33 @@ def dismiss_control(self) -> None: raise SkipAction() +class FileChip(AttachedFileChip, Button): + """One attached file as Textual draws it; pressing it takes the file off the message.""" + + def __init__(self, composer: MessagePromptComposer, position: int) -> None: + self.show_file(composer, position) + super().__init__(f"{self.caption} ✕", compact=True, tooltip="Remove from this message") + + @on(Button.Pressed) + def pressed(self, event: Button.Pressed) -> None: + event.stop() + self.take_off() + + +class AttachedFiles(containers.HorizontalGroup): + """The files the message being written carries, as chips above the text.""" + + def show(self, composer: MessagePromptComposer) -> None: + self.remove_children() + self.mount_all(FileChip(composer, position) for position in range(len(composer.attached_files))) + self.display = bool(composer.attached_files) + + class MessagePrompt(MessagePromptComposer, Prompt): TEXT_AREA_CLASS = MessageTextArea + attached = getters.query_one(AttachedFiles) + def __init__(self, *, placeholder: str | None = None, **kwargs): self.simple_placeholder = placeholder super().__init__(**kwargs) @@ -1142,6 +1177,21 @@ def compose_completions(self) -> ComposeResult: if (popup := SlashComplete.for_prompt(self)) is not None: yield popup + def compose_attached(self) -> ComposeResult: + files = AttachedFiles() + files.display = False + yield files + + def pick_in_window(self) -> None: + """A terminal's window is a file browser over the whole disk, opened at the project.""" + self.app.push_screen(FileBrowserScreen(self, self.project_path.path)) + + def pick_by_path_search(self) -> None: + self.app.push_screen(PathSearchScreen(self, self.project_path.path)) + + def show_attached(self) -> None: + self.attached.show(self) + class MailboxPrompt(MessagePrompt): """A CLI thread's composer shows its project; Core's status line names the model, read-only.""" diff --git a/src/toad/widgets/transcript_history.py b/src/toad/widgets/transcript_history.py index 3db7e7bae..8783d43ef 100644 --- a/src/toad/widgets/transcript_history.py +++ b/src/toad/widgets/transcript_history.py @@ -951,7 +951,7 @@ def empty_page(cls): @classmethod def empty_source(cls, view, through): from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES - return SavedTranscript(view.transcript.get_transcript_page, through, view.app.preparation, + return SavedTranscript(view.transcript, through, view.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES) async def _page_added(self, page: TranscriptPage, fragments: tuple[TranscriptFragment, ...]) -> None: diff --git a/tests/model/test_message_file_reader.py b/tests/model/test_message_file_reader.py new file mode 100644 index 000000000..7104011fd --- /dev/null +++ b/tests/model/test_message_file_reader.py @@ -0,0 +1,21 @@ +"""A message's files are read in the conversation its reader sees it in: a channel message in +its channel, a direct message in the DM with the participant that is not the reader.""" + +import pytest +from agent_comms.messaging.messages import InfoKind, Message + +from toad.client.message_file_links import FileReader, file_size +from toad.message_rows import MessageRow + + +@pytest.mark.parametrize(("sender", "target", "reader", "read"), ( + ("peer", "owner", "owner", FileReader("peer", "owner")), + ("owner", "peer", "owner", FileReader("peer", "owner")), + ("peer", "#team", "owner", FileReader("#team", "owner")), +), ids=("dm-incoming", "dm-outgoing", "channel")) +def test_a_row_names_the_read_its_files_are_listed_under(sender, target, reader, read): + assert MessageRow(Message(sender, target, "body", InfoKind), reader).file_reader == read + + +def test_a_chip_names_its_size_as_a_reader_reads_it(): + assert [file_size(size) for size in (512, 1536, 2 * 1024 * 1024)] == ["512 B", "1.5 KB", "2.0 MB"] diff --git a/tests/model/test_message_files.py b/tests/model/test_message_files.py new file mode 100644 index 000000000..13f0d0cc8 --- /dev/null +++ b/tests/model/test_message_files.py @@ -0,0 +1,97 @@ +"""The message composer's files, through the shared owner both frontends inherit.""" + +from __future__ import annotations + +import base64 +from dataclasses import dataclass, field + +from agent_comms.messaging.message_files import MAX_MESSAGE_FILE_BYTES + +from toad.client.bus_submission import TypedText +from toad.client.message_files import FilePicker, MessagePromptComposer, PathSearchFilePicker, WindowFilePicker +from toad.client.message_file_links import file_size + + +@dataclass +class Input: + file_picker: type[FilePicker] + agent_symbol: str = "❯" + + +@dataclass +class Ui: + input: Input + + +@dataclass +class Settings: + ui: Ui + + +@dataclass +class Composer(MessagePromptComposer): + """A frontend that records what the shared owner asks of it.""" + + picker: type[FilePicker] = WindowFilePicker + asked: list[str] = field(default_factory=list) + text: str = "" + submissions = None + + @property + def settings(self): + return Settings(Ui(Input(self.picker))) + + def pick_in_window(self) -> None: + self.asked.append("window") + + def pick_by_path_search(self) -> None: + self.asked.append("path search") + + def show_attached(self) -> None: + self.asked.append(f"chips {[self.chip_label(upload) for upload in self.attached_files]}") + + def show_failure(self, text: str) -> None: + self.asked.append(f"failure {text}") + + def restore_draft(self, text: str) -> None: + self.text += self.restored_suffix(self.text, text) + + +def test_attach_opens_the_picker_the_setting_names() -> None: + for picker, asked in ((WindowFilePicker, "window"), (PathSearchFilePicker, "path search")): + composer = Composer(picker=picker) + composer.attach_file() + assert composer.asked == [asked] + + +def test_picked_files_ride_the_send_and_come_back_with_a_returned_draft() -> None: + composer = Composer() + composer.file_picked("/home/me/notes.txt", b"notes") + composer.file_picked("shot.png", b"\x89PNG") + assert [(upload.name, upload.mime_type) for upload in composer.attached_files] == [ + ("notes.txt", "text/plain"), ("shot.png", "image/png")] + assert composer.asked[-1] == "chips ['notes.txt · 5 B', 'shot.png · 4 B']" + composer.detach_file(0) + assert [upload.name for upload in composer.attached_files] == ["shot.png"] + sent = TypedText("look at this", composer.take_files()) + assert composer.attached_files == () and composer.asked[-1] == "chips []" + assert base64.b64decode(sent.files[0].data) == b"\x89PNG" + composer.restore(sent) + assert composer.text == "look at this" + assert composer.attached_files == sent.files + + +def test_a_file_past_the_message_limit_is_refused_and_attaches_nothing() -> None: + composer = Composer() + composer.file_picked("half.bin", b"a" * (MAX_MESSAGE_FILE_BYTES // 2 + 1)) + assert composer.read_limit == MAX_MESSAGE_FILE_BYTES // 2 + composer.file_picked("other-half.bin", b"b" * composer.read_limit) + assert [upload.name for upload in composer.attached_files] == ["half.bin"] + assert composer.asked[-1].startswith("failure other-half.bin not attached: ") + composer.file_picked("empty.txt", b"") + assert composer.asked[-1].startswith("failure empty.txt not attached: ") + + +def test_sizes_read_at_a_glance() -> None: + assert [file_size(size) for size in (0, 1023, 1536, 5 * 1024 * 1024)] == [ + "0 B", "1023 B", "1.5 KB", "5.0 MB"] diff --git a/tests/support/textual_steps.py b/tests/support/textual_steps.py index 40f35d074..04ccc9133 100644 --- a/tests/support/textual_steps.py +++ b/tests/support/textual_steps.py @@ -5,7 +5,8 @@ from __future__ import annotations from toad.client.history_state import LiveTranscript, ProvisionalTranscript -from toad.transcript_preparation import SavedTranscript, TranscriptPageRenderTask +from toad.client.message_file_links import NO_PAGE_FILES +from toad.transcript_preparation import LoadedTranscriptPages, SavedTranscript, TranscriptPageRenderTask from toad.widgets.side_bar import ThreadSidebar from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES from toad.widgets.transcript_history import TranscriptHistory @@ -139,7 +140,7 @@ def painted_text(widget, text) -> bool: async def saved_history(view, page, loader, *, committed=True): """A saved transcript history prepared and paged the way the application builds one.""" - prepared = await view.app.preparation.submit(RenderPreparation(TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES))) - return TranscriptHistory(prepared, SavedTranscript(loader, page.after, view.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES), + prepared = await view.app.preparation.submit(RenderPreparation(TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES))) + return TranscriptHistory(prepared, SavedTranscript(LoadedTranscriptPages(loader), page.after, view.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES), view=view, notification_view=view.contents, source_state=LiveTranscript() if committed else ProvisionalTranscript()) diff --git a/tests/textual/test_fragment_source_occurrence.py b/tests/textual/test_fragment_source_occurrence.py index 0c7010932..83cbf4a6c 100644 --- a/tests/textual/test_fragment_source_occurrence.py +++ b/tests/textual/test_fragment_source_occurrence.py @@ -9,12 +9,13 @@ ThinkingTranscript, ) +from toad.client.message_file_links import NO_PAGE_FILES from toad.client.transcript_fragments import RenderBudget, transcript_fragments from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES def fragments(*events): - return transcript_fragments(events, resolve_sent=lambda reports: {}, classes=TEXTUAL_FRAGMENT_CLASSES) + return transcript_fragments(events, resolve_sent=lambda reports: {}, classes=TEXTUAL_FRAGMENT_CLASSES, files=NO_PAGE_FILES) def test_thinking_parts_of_one_reply_have_distinct_keys(): diff --git a/tests/textual/test_input_failure_draft.py b/tests/textual/test_input_failure_draft.py index 3a894cc24..454212f30 100644 --- a/tests/textual/test_input_failure_draft.py +++ b/tests/textual/test_input_failure_draft.py @@ -86,7 +86,7 @@ async def test_a_prompt_the_agent_cannot_be_reached_for_is_restored_to_the_compo ui.attach_agent(receiver) await ui.write_draft("") with patch.object(receiver.process, "prompt", side_effect=FileNotFoundError(2, "agent socket disappeared")): - worker = await ImmediateInputSubmission("the local failure prompt").execute(conversation.submissions) + worker = await ImmediateInputSubmission("the local failure prompt", ()).execute(conversation.submissions) await worker.wait() await ui.until(lambda: ui.draft() == "the local failure prompt", why="the failed prompt back in the draft") await receiver.stop() @@ -123,9 +123,9 @@ async def test_wire_sends_queue_in_order_while_compose_stays_open(workspace): gate = threading.Event() send = Observing.send_user_message - def held(observed, target, body): + def held(observed, target, body, files): gate.wait(10) - return send(observed, target, body) + return send(observed, target, body, files) async with workspace.app(size=(110, 32)) as ui: await ui.open_direct("peer", actor=workspace.viewer) diff --git a/tests/textual/test_message_file_attach.py b/tests/textual/test_message_file_attach.py new file mode 100644 index 000000000..e43919e02 --- /dev/null +++ b/tests/textual/test_message_file_attach.py @@ -0,0 +1,112 @@ +"""A message prompt attaches files from its terminal pickers, sends them with the message, +and gets them back with the draft when a send does not land.""" + +import hashlib + +from agent_comms.messaging.channel_targets import ChannelTarget, CommandTarget + +from toad.client.conversation import ChannelConversation +from toad.client.history import ChannelDestination +from toad.client.message_files import PathSearchFilePicker +from toad.conversation_submission import OrdinaryInputSubmission +from toad.preferences import SETTINGS +from toad.screens.file_picker import FileBrowserScreen, PathSearchScreen +from toad.widgets.comms_chat import CommsChatView +from toad.widgets.prompt import FileChip, MessageTextArea, PromptTextArea +from toad.prompt_actions import AttachFileAction + + +def chips(prompt) -> list[str]: + return [str(chip.label) for chip in prompt.query(FileChip)] + + +async def test_a_picked_file_rides_the_message_and_comes_back_with_a_refused_draft(workspace): + workspace.thread("peer", "team") + workspace.comms.messaging.send("peer", "#team", "CURRENT_HISTORY") + async with workspace.app() as ui: + await ui.open_channel("#team", actor=workspace.viewer) + await ui.until_shows("CURRENT_HISTORY") + view = ui.app.selected_session.query_one(CommsChatView) + prompt = view.prompt + content = b"line one\nline two\n" + picked = prompt.project_path.path / "notes.txt" + picked.write_bytes(content) + + await prompt.attach_path(picked) + await ui.until(lambda: chips(prompt) == ["notes.txt · 18 B ✕"], why="the attached file's chip") + + prompt.prompt_text_area.focus() + await ui.type("with a file") + await ui.press("enter") + + def sent_messages(): + return [message for message in workspace.comms.views.history_page(CommandTarget.named("#team")).messages + if message.body == "with a file"] + + await ui.until(sent_messages, why="the message on the bus") + (sent,) = sent_messages() + (stored,) = workspace.comms.messaging.files.of((sent.reference,)) + assert (stored.name, stored.size, stored.sha256) == ( + "notes.txt", len(content), hashlib.sha256(content).hexdigest()) + assert chips(prompt) == [] and prompt.attached_files == () + + await prompt.attach_path(picked) + files = prompt.take_files() + stale = ChannelDestination(workspace.roots.wire, ChannelConversation(ChannelTarget("#elsewhere")), view) + await view.submissions.start_bus(OrdinaryInputSubmission("REFUSED_DRAFT", files), stale).prepare() + await ui.until(lambda: prompt.text == "REFUSED_DRAFT", why="the refused text back in the composer") + assert prompt.attached_files == files + await ui.until(lambda: chips(prompt) == ["notes.txt · 18 B ✕"], why="the refused file's chip") + + prompt.query_one(FileChip).press() + await ui.until(lambda: chips(prompt) == [], why="the chip took its file off") + assert prompt.attached_files == () + + +async def test_the_attach_key_opens_the_named_picker_and_escape_attaches_nothing(workspace): + workspace.thread("peer", "team") + workspace.comms.messaging.send("peer", "#team", "CURRENT_HISTORY") + async with workspace.app() as ui: + app, _pilot = ui.textual + await ui.open_channel("#team", actor=workspace.viewer) + await ui.until_shows("CURRENT_HISTORY") + prompt = app.selected_session.query_one(CommsChatView).prompt + (prompt.project_path.path / "picked-by-search.txt").write_bytes(b"found") + + prompt.prompt_text_area.focus() + await ui.press("ctrl+t") + await ui.until(lambda: isinstance(app.screen, FileBrowserScreen), why="the file browser") + await ui.press("escape") + await ui.until(lambda: not isinstance(app.screen, FileBrowserScreen), why="escape closed it") + assert prompt.attached_files == () + + app.saved_settings.set(SETTINGS.ui.input.file_picker, PathSearchFilePicker) + prompt.prompt_text_area.focus() + await ui.press("ctrl+t") + await ui.until(lambda: isinstance(app.screen, PathSearchScreen), why="the path search") + await ui.until(lambda: "picked-by-search.txt" in app.screen.spellings, why="the project listing") + await ui.type("picked-by") + await ui.press("enter") + await ui.until(lambda: chips(prompt) == ["picked-by-search.txt · 5 B ✕"], why="the searched file's chip") + + +def test_only_a_message_editor_answers_the_attach_key(): + assert AttachFileAction in MessageTextArea.declared_actions() + assert AttachFileAction not in PromptTextArea.declared_actions() + + +async def test_a_sent_file_draws_as_a_chip_under_its_message_in_the_terminal(workspace): + """The history row a reader sees: the message's text, then a chip per file it carries.""" + workspace.thread("peer", "team") + async with workspace.app() as ui: + await ui.open_channel("#team", actor=workspace.viewer) + view = ui.app.selected_session.query_one(CommsChatView) + prompt = view.prompt + picked = prompt.project_path.path / "shot.png" + picked.write_bytes(b"\x89PNG" + bytes(2044)) + await prompt.attach_path(picked) + prompt.prompt_text_area.focus() + await ui.type("a screenshot") + await ui.press("enter") + await ui.until(lambda: ui.shows("a screenshot") and ui.shows("[shot.png · 2.0 KB]"), + why="the sent message and its file's chip under it") diff --git a/tests/textual/test_message_file_chips.py b/tests/textual/test_message_file_chips.py new file mode 100644 index 000000000..f3ad80fb9 --- /dev/null +++ b/tests/textual/test_message_file_chips.py @@ -0,0 +1,71 @@ +"""A page's messages draw their files as chips, from one read beside the page, and a chip's +link fetches the very bytes that were sent.""" + +import base64 +import os +from pathlib import Path + +from agent_comms.comms import Comms +from agent_comms.execution.launch_root import LaunchRoot +from agent_comms.messaging.channel_targets import ChannelTarget +from agent_comms.messaging.historical_views import UnobservedConversation +from agent_comms.messaging.message_files import FileUpload +from agent_comms.projects.checkouts import Checkout +from agent_comms.registry.threads import NativeThread +from agent_comms.transport.request import CoreAccess +from agent_comms.transport.tool_caller import HumanCaller +from agent_comms.transport.tool_request import ServedRequest +from agent_comms.transport.tools.served import ServedCommsUserSendTool +from support.agents import AppAgents +from support.roots import installed_wire + +from toad.client.conversation import ChannelConversation +from toad.client.history import WireSource +from toad.client.history_preparation import OLDER_END, PageRequest +from toad.client.line_blocks import Spans +from toad.client.message_file_links import MessageFileLink +from toad.message_rows import IrcMessageStyle +from toad.widgets.wire_history import WireFragment + + +class ServedCore(CoreAccess): + """This test's Core, every request served in process as the human caller, the caller the + observation child serves a UI's reads as.""" + + def __init__(self, comms: Comms) -> None: + self.comms = comms + self.called: list[str] = [] + + def call(self, request): + self.called.append(request.declared_name) + return ServedRequest.serving(request).apply_without_thread(self.comms, HumanCaller()) + + +def test_a_page_draws_each_message_s_files_and_a_chip_fetches_the_sent_bytes(tmp_path: Path, monkeypatch) -> None: + monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "data")) + comms = installed_wire(tmp_path / "wire") + comms.registry.declare(NativeThread("peer", frozenset({"team"}), Checkout(tmp_path))) + content = os.urandom(3000) + HumanCaller().invoke(ServedCommsUserSendTool(target="#team", body="the trace", files=( + FileUpload("trace.bin", "application/octet-stream", base64.b64encode(content).decode()),)), comms) + HumanCaller().invoke(ServedCommsUserSendTool(target="#team", body="no file"), comms) + project = Checkout(tmp_path) + core = ServedCore(comms) + source = WireSource(comms.root, ChannelConversation(ChannelTarget("#team")), project, + comms.messaging.user_identity(project).name, IrcMessageStyle(), + access=AppAgents.unrun(project.path, LaunchRoot.from_path(comms.root)).app.coordination_access, + core=core, fragments=WireFragment) + through = source.observe(UnobservedConversation()).through + prepared = source.read(PageRequest(OLDER_END, through), through) + assert core.called.count("comms_message_files") == 1 + with_file, without = prepared.fragments + chips = [span for block in with_file.blocks(with_file.shown) if isinstance(block, Spans) + for span in block.spans if isinstance(span.link, MessageFileLink)] + assert [span.text for span in chips] == ["[trace.bin · 2.9 KB]"] + assert not [span for block in without.blocks(without.shown) if isinstance(block, Spans) + for span in block.spans if isinstance(span.link, MessageFileLink)] + (link,) = (span.link for span in chips) + assert (link.conversation, link.root) == ("#team", str(comms.root)) + kept = link.fetched(core) + assert kept.read_bytes() == content + assert kept.is_relative_to(tmp_path / "data") and kept.name == "trace.bin" diff --git a/tests/textual/test_message_filter_restyle.py b/tests/textual/test_message_filter_restyle.py index 86706c673..9c4649461 100644 --- a/tests/textual/test_message_filter_restyle.py +++ b/tests/textual/test_message_filter_restyle.py @@ -12,6 +12,8 @@ ) from support.journals import assistant_entry, user_entry, write_journal +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.message_rows import MarkdownMessageStyle from toad.transcript_preparation import TranscriptPageRenderTask from toad.client.history_state import PreparedReplacementViewportRequest @@ -75,7 +77,7 @@ async def test_filtering_saved_history_paints_an_older_projection_and_returns_to workspace.comms.registry.require("source"), session_file=RegistrySessionLocation.from_path(str(replacement), workspace.comms.root))) page = await ui.app.preparation.run_thread( lambda: workspace.comms.transcripts.capture_page_read("source").read()) - prepared = await TranscriptPageRenderTask(page).prepared(ui.app.preparation) + prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(ui.app.preparation) # A replacement is source work, as the app's own commit publication runs it: wait until # that operation releases the source, then reserve, so the two reads never overlap. await ui.until(lambda: history.state.accepts_source_work, why="the app's source operation to finish") diff --git a/tests/textual/test_remote_transcript_publications.py b/tests/textual/test_remote_transcript_publications.py index d48b9b416..99536e2dc 100644 --- a/tests/textual/test_remote_transcript_publications.py +++ b/tests/textual/test_remote_transcript_publications.py @@ -3,6 +3,8 @@ from agent_comms.nominal.field_codec import FieldCodec from agent_comms.transcript.transcripts import TranscriptPage +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.transcript_preparation import TranscriptPageRenderTask @@ -16,7 +18,7 @@ def test_remote_page_renders_without_the_server_bus(workspace): absent = database.with_suffix(".off-device") database.rename(absent) try: - prepared = TranscriptPageRenderTask(remote).execute() + prepared = TranscriptPageRenderTask(remote, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).execute() finally: absent.rename(database) assert len(prepared.outgoing_coverage) == 1 diff --git a/tests/textual/test_saved_history_coverage.py b/tests/textual/test_saved_history_coverage.py index e83807684..0e5f7c946 100644 --- a/tests/textual/test_saved_history_coverage.py +++ b/tests/textual/test_saved_history_coverage.py @@ -11,6 +11,8 @@ from agent_comms.registry.threads import NativeThread from support import journals +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.screens.main import MainScreen from toad.client.tab_identity.kinds import MailboxTab from toad.widgets.agent_response import AgentResponse @@ -84,13 +86,13 @@ async def test_read_receipts_reuse_original_prepared_suppliers(workspace): async with workspace.app(size=(110, 35)) as ui: page, _, _ = await saved_history(workspace, ui) runtime = ui.app.preparation - first = await TranscriptPageRenderTask(page).prepared(runtime) + first = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(runtime) page.snapshot.records_cut.mark_read( workspace.comms.bus.reads, "saved", page.snapshot.runtime.read_key, page.after.offset) current = workspace.comms.transcripts.capture_page_read("saved").read() assert page.snapshot != current.snapshot assert page.snapshot.same_content(current.snapshot) - second = await TranscriptPageRenderTask(current).prepared(runtime) + second = await TranscriptPageRenderTask(current, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(runtime) assert first is not second assert first.page is page assert second.page is current diff --git a/tests/textual/test_stale_route_writes.py b/tests/textual/test_stale_route_writes.py index e09b90080..6a84661a1 100644 --- a/tests/textual/test_stale_route_writes.py +++ b/tests/textual/test_stale_route_writes.py @@ -20,7 +20,7 @@ async def test_a_view_that_changed_before_its_send_left_keeps_the_draft_editable before = messaging.bus.bus.revision() # Built for a conversation the view no longer shows: the view moved on before the send left. stale = ChannelDestination(workspace.roots.wire, ChannelConversation(ChannelTarget("#elsewhere")), view) - await view.submissions.start_bus(OrdinaryInputSubmission("STALE_DRAFT"), stale).prepare() + await view.submissions.start_bus(OrdinaryInputSubmission("STALE_DRAFT", ()), stale).prepare() await ui.until(lambda: view.prompt.text == "STALE_DRAFT", why="the refused draft back in the composer") assert not view.submissions.delivering assert not view.prompt.prompt_text_area.disabled diff --git a/tests/textual/test_transcript_presentation_fencing.py b/tests/textual/test_transcript_presentation_fencing.py index 59a46c985..96657d678 100644 --- a/tests/textual/test_transcript_presentation_fencing.py +++ b/tests/textual/test_transcript_presentation_fencing.py @@ -10,6 +10,8 @@ from agent_comms.registry.threads import NativeThread from support.journals import assistant_entry, write_journal +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.transcript_publication import ( CLEAN_PUBLICATION, COMMIT_DEMAND, @@ -394,7 +396,7 @@ async def test_a_closed_history_accepts_a_prepared_replacement_without_a_storage () if empty else (assistant_entry("PREPARED_REPLACEMENT", id="new"),))) workspace.comms.registry.declare(replace(thread, session_file=RegistrySessionLocation.from_path(str(new), workspace.comms.root))) page = workspace.comms.transcripts.capture_page_read("source").read() - prepared = await TranscriptPageRenderTask(page).prepared(ui.app.preparation) + prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(ui.app.preparation) assert await history.show_newest(PreparedReplacementViewportRequest( LatestPosition.hold(history.window), prepared)) await ui.step() diff --git a/tools/qt_drive.py b/tools/qt_drive.py index 24268ab6a..5ca3ce489 100644 --- a/tools/qt_drive.py +++ b/tools/qt_drive.py @@ -12,6 +12,7 @@ from __future__ import annotations import asyncio +import hashlib import time import os import sys @@ -25,7 +26,7 @@ from PySide6.QtCore import QEvent, QObject, QPoint, QPointF, Qt from PySide6.QtGui import QInputMethodEvent, QKeyEvent, QKeySequence, QMouseEvent from PySide6.QtTest import QTest -from PySide6.QtWidgets import QAbstractButton, QAbstractScrollArea, QDialog, QLabel, QScroller, QStyleOptionViewItem, QApplication, QPushButton, QWidget +from PySide6.QtWidgets import QAbstractButton, QAbstractScrollArea, QDialog, QFileDialog, QLabel, QLineEdit, QScroller, QStyleOptionViewItem, QApplication, QPushButton, QWidget from agent_comms.messaging.channel_targets import ChannelTarget from agent_comms.registry.thread_presentation import LiveThreadOwnerBinding @@ -185,15 +186,19 @@ async def open_named(self, name: str) -> None: await self.expand(name) await self.open(self.channel_row(name), name) else: - pending, rows = list(self.app.sidebar.items()), [] - while pending: - item = pending.pop() - rows.append(item) - pending.extend(item.child(index) for index in range(item.childCount())) - await self.open(next(row for row in rows if isinstance(row, ConversationRow) - and row.target.name == name), name) + await until(lambda: self.named_rows(name), name) + await self.open(self.named_rows(name)[0], name) await asyncio.sleep(2.0) + def named_rows(self, name: str) -> list[ConversationRow]: + """The sidebar rows opening this thread: one under each channel it belongs to.""" + pending, rows = list(self.app.sidebar.items()), [] + while pending: + item = pending.pop() + rows.append(item) + pending.extend(item.child(index) for index in range(item.childCount())) + return [row for row in rows if isinstance(row, ConversationRow) and row.target.name == name] + async def channel_listed(self, name: str) -> None: """The sidebar lists this channel: the welcome's first rows can come before it.""" await until(lambda: any(isinstance(row, ConversationRow) and row.target.name == name @@ -953,6 +958,12 @@ async def press_bound_keys(self, label: str) -> None: combination = sequence[0] QTest.keyClick(target, combination.key(), combination.keyboardModifiers()) await asyncio.sleep(0.3) + # A key whose action opens a dialog (Attach's file picker) leaves it open; it + # is closed unanswered and the window active again, so the next key reaches the + # view; offscreen Qt hands activation back to no window when a dialog closes. + self.close_dialogs() + self.app.activateWindow() + await asyncio.sleep(0.1) ran = [type(action).__name__ for action in keys.ran] results.append(f"{where}:{spelling}->{'+'.join(ran) or 'none available'}") if ran != expected: @@ -977,9 +988,11 @@ async def run(self) -> None: self.report(f"2-settings-page-{index}", page=dialog.tabs.tabText(index)) self.close_dialogs() await self.step("3-channel", lambda: self.open_named("#comms")) + await self.step("3-channel-attach", self.open_attach) await self.press_bound_keys("3-channel-keys") await self.step("4-thread", lambda: self.open_named("scratch-live-pipeline")) await self.step("4-thread-details", self.open_details) + await self.step("4-thread-attach", self.open_attach) await self.press_bound_keys("4-thread-keys") # The reader keys (ctrl+d, ctrl+u, G) are declared for the vim modes only: press every # key again in vim normal mode, as a person choosing that keyboard mode would. @@ -991,6 +1004,18 @@ async def run(self) -> None: if self.faults: raise AssertionError(f"{len(self.faults)} fault(s) on the shipped app:\n" + "\n".join(self.faults)) + async def open_attach(self) -> None: + """The composer's Attach opens its file picker; it is closed without picking.""" + self.page.composer.attach_button.click() + await asyncio.sleep(0.5) + dialogs = [dialog for dialog in self.app.findChildren(QFileDialog) if dialog.isVisible()] + if len(dialogs) != 1: + raise AssertionError(f"Attach opened {len(dialogs)} file dialogs") + dialogs[0].reject() + await asyncio.sleep(0.3) + if self.page.composer.attached_files: + raise AssertionError("closing the picker attached a file") + async def open_details(self) -> None: self.page.thread_activity.toggle() await asyncio.sleep(0.3) @@ -998,6 +1023,40 @@ async def open_details(self) -> None: await asyncio.sleep(1.0) +class AttachScenario(Scenario): + summary = ("attach OUTDIR/attached.bin to a message to the scratch thread scratch-live-pipeline " + "through the composer's file dialog and send it: run it on a scratch bus only") + + async def run(self) -> None: + await self.listed() + await self.open_named("scratch-live-pipeline") + composer = self.page.composer + picked = self.out / "attached.bin" + picked.write_bytes(os.urandom(300_000) + b"the end of the attached file\n") + composer.attach_button.click() + await until(lambda: any(dialog.isVisible() for dialog in self.app.findChildren(QFileDialog)), + "file dialog", tenths=50) + (dialog,) = [dialog for dialog in self.app.findChildren(QFileDialog) if dialog.isVisible()] + # As a person picks it: the path typed into the dialog's file name field, then Enter. + # (QFileDialog.selectFile leaves a field that has focus as it is.) + (field,) = dialog.findChildren(QLineEdit) + field.setFocus() + field.setText(str(picked)) + QTest.keyClick(field, Qt.Key.Key_Return) + await asyncio.sleep(1.0) + await until(lambda: composer.attached_files, "attached file", tenths=50) + chips = [chip.text() for chip in composer.attached_row.findChildren(QPushButton)] + self.report("1-attached", chips=chips, sha256=hashlib.sha256(picked.read_bytes()).hexdigest()) + await self.type("a file from the Qt composer") + composer.submit() + await until(lambda: not composer.attached_files and not composer.submissions.delivering, + "sent", tenths=150) + await asyncio.sleep(2.0) + self.report("2-sent", chips=[chip.text() for chip in composer.attached_row.findChildren(QPushButton) + if chip.isVisible()], + failure=composer.failure.text() if composer.failure.isVisible() else "") + + class MissingChannelDestination(ChannelDestination): """A destination its view still shows whose channel Core does not know: its read-only check fails before anything leaves, the way a refused send does.""" @@ -1035,7 +1094,7 @@ async def run(self) -> None: missing = ChannelConversation(ChannelTarget("#composer-drive-missing")) destination = MissingChannelDestination(app.source().root, missing, view) app.run_operation(ConversationSubmission, - lambda: view.submissions.start_bus(TypedText("this send must fail"), destination).prepare()) + lambda: view.submissions.start_bus(TypedText("this send must fail", ()), destination).prepare()) await until(lambda: composer.failure.isVisible(), "failure", tenths=100) await asyncio.sleep(0.5) self.report("6-failed", failure=composer.failure.text(), draft=composer.text,