|
| 1 | +"""Clipboard toolkit — copy text, notes, tasks, and links to the system clipboard.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import logging |
| 7 | +import platform |
| 8 | +from pathlib import Path |
| 9 | +from typing import TYPE_CHECKING |
| 10 | + |
| 11 | +from hive.tools.base import Toolkit, tool |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from hive.memory.semantic import SemanticMemory |
| 15 | + from hive.memory.store import HiveStore |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +async def _copy_to_system_clipboard(text: str) -> bool: |
| 21 | + """Copy text to system clipboard. Supports macOS and Linux.""" |
| 22 | + system = platform.system() |
| 23 | + if system == "Darwin": |
| 24 | + cmd = ["pbcopy"] |
| 25 | + elif system == "Linux": |
| 26 | + cmd = ["xclip", "-selection", "clipboard"] |
| 27 | + else: |
| 28 | + logger.warning("Clipboard not supported on %s", system) |
| 29 | + return False |
| 30 | + |
| 31 | + try: |
| 32 | + proc = await asyncio.create_subprocess_exec( |
| 33 | + *cmd, |
| 34 | + stdin=asyncio.subprocess.PIPE, |
| 35 | + stdout=asyncio.subprocess.PIPE, |
| 36 | + stderr=asyncio.subprocess.PIPE, |
| 37 | + ) |
| 38 | + await asyncio.wait_for(proc.communicate(input=text.encode()), timeout=5) |
| 39 | + return proc.returncode == 0 |
| 40 | + except Exception as e: |
| 41 | + logger.warning("Clipboard copy failed: %s", e) |
| 42 | + return False |
| 43 | + |
| 44 | + |
| 45 | +class ClipboardToolkit(Toolkit): |
| 46 | + """Tools for copying content to the system clipboard. |
| 47 | +
|
| 48 | + Usage: |
| 49 | + # With access to store + memory (can copy tasks, notes, links): |
| 50 | + tk = ClipboardToolkit(store=hive_store, memory=semantic_memory) |
| 51 | +
|
| 52 | + # Standalone (copy text only): |
| 53 | + tk = ClipboardToolkit() |
| 54 | + """ |
| 55 | + |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + store: HiveStore | None = None, |
| 59 | + memory: SemanticMemory | None = None, |
| 60 | + db_path: str | Path | None = None, |
| 61 | + memory_dir: str | Path | None = None, |
| 62 | + ) -> None: |
| 63 | + self._store: HiveStore | None = None |
| 64 | + self._memory: SemanticMemory | None = None |
| 65 | + self._memory_dir: Path | None = None |
| 66 | + self._initialized = False |
| 67 | + |
| 68 | + if store is not None: |
| 69 | + self._store = store |
| 70 | + self._initialized = True |
| 71 | + elif db_path is not None: |
| 72 | + from hive.memory.store import HiveStore as _Store |
| 73 | + |
| 74 | + self._store = _Store(Path(db_path)) |
| 75 | + |
| 76 | + if memory is not None: |
| 77 | + self._memory = memory |
| 78 | + elif memory_dir is not None: |
| 79 | + self._memory_dir = Path(memory_dir) |
| 80 | + |
| 81 | + def bind(self, agent_id: str) -> None: |
| 82 | + super().bind(agent_id) |
| 83 | + if self._memory_dir is not None: |
| 84 | + from hive.memory.semantic import SemanticMemory |
| 85 | + |
| 86 | + self._memory = SemanticMemory(self._memory_dir, agent_id) |
| 87 | + |
| 88 | + def rebind(self, agent_id: str) -> None: |
| 89 | + super().rebind(agent_id) |
| 90 | + if self._memory_dir is not None: |
| 91 | + from hive.memory.semantic import SemanticMemory |
| 92 | + |
| 93 | + self._memory = SemanticMemory(self._memory_dir, agent_id) |
| 94 | + |
| 95 | + async def _ensure_init(self) -> None: |
| 96 | + if not self._initialized and self._store is not None: |
| 97 | + await self._store.initialize() |
| 98 | + self._initialized = True |
| 99 | + |
| 100 | + @property |
| 101 | + def instructions(self) -> str: |
| 102 | + return "You can copy text, notes, tasks, or links to the user's clipboard." |
| 103 | + |
| 104 | + @tool() |
| 105 | + async def copy_to_clipboard(self, text: str) -> str: |
| 106 | + """Copy text to the system clipboard. |
| 107 | +
|
| 108 | + Args: |
| 109 | + text: The text to copy. |
| 110 | + """ |
| 111 | + ok = await _copy_to_system_clipboard(text) |
| 112 | + if ok: |
| 113 | + preview = text[:80] + ("..." if len(text) > 80 else "") |
| 114 | + return f"Copied to clipboard: {preview}" |
| 115 | + return "Failed to copy to clipboard." |
| 116 | + |
| 117 | + @tool() |
| 118 | + async def copy_note(self, note_id: str) -> str: |
| 119 | + """Copy a note's content to the clipboard. |
| 120 | +
|
| 121 | + Args: |
| 122 | + note_id: The note/memory ID to copy. |
| 123 | + """ |
| 124 | + if self._memory is None: |
| 125 | + return "No knowledge base available." |
| 126 | + |
| 127 | + record = await self._memory.recall(note_id) |
| 128 | + if record is None: |
| 129 | + return f"Note {note_id} not found." |
| 130 | + |
| 131 | + ok = await _copy_to_system_clipboard(record.thought) |
| 132 | + if ok: |
| 133 | + preview = record.thought[:80] + ("..." if len(record.thought) > 80 else "") |
| 134 | + return f"Copied note to clipboard: {preview}" |
| 135 | + return "Failed to copy to clipboard." |
| 136 | + |
| 137 | + @tool() |
| 138 | + async def copy_task(self, task_id: str) -> str: |
| 139 | + """Copy a task's description to the clipboard. |
| 140 | +
|
| 141 | + Args: |
| 142 | + task_id: The task ID to copy. |
| 143 | + """ |
| 144 | + if self._store is None: |
| 145 | + return "No task store available." |
| 146 | + |
| 147 | + await self._ensure_init() |
| 148 | + tasks = await self._store.list_tasks(self._agent_id, "pending") |
| 149 | + tasks += await self._store.list_tasks(self._agent_id, "done") |
| 150 | + |
| 151 | + task = next((t for t in tasks if t["task_id"] == task_id), None) |
| 152 | + if task is None: |
| 153 | + return f"Task {task_id} not found." |
| 154 | + |
| 155 | + text = task["description"] |
| 156 | + if task.get("due_date"): |
| 157 | + text += f" (due: {task['due_date']})" |
| 158 | + |
| 159 | + ok = await _copy_to_system_clipboard(text) |
| 160 | + if ok: |
| 161 | + return f"Copied task to clipboard: {text[:80]}" |
| 162 | + return "Failed to copy to clipboard." |
| 163 | + |
| 164 | + @tool() |
| 165 | + async def copy_link(self, query: str) -> str: |
| 166 | + """Find a saved link by search and copy its URL to the clipboard. |
| 167 | +
|
| 168 | + Args: |
| 169 | + query: Search query to find the link. |
| 170 | + """ |
| 171 | + if self._memory is None: |
| 172 | + return "No knowledge base available." |
| 173 | + |
| 174 | + results = await self._memory.search(query, top_k=5) |
| 175 | + links = [r for r in results if r.metadata.get("type") == "link"] |
| 176 | + |
| 177 | + if not links: |
| 178 | + return f"No saved link matching '{query}'." |
| 179 | + |
| 180 | + link = links[0] |
| 181 | + url = link.metadata.get("url", "") |
| 182 | + if not url: |
| 183 | + return "Link found but has no URL." |
| 184 | + |
| 185 | + ok = await _copy_to_system_clipboard(url) |
| 186 | + if ok: |
| 187 | + title = link.metadata.get("title", url) |
| 188 | + return f"Copied URL to clipboard: {title} ({url})" |
| 189 | + return "Failed to copy to clipboard." |
0 commit comments