minimalcode 1.5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- minimalcode/__init__.py +3 -0
- minimalcode/__main__.py +3 -0
- minimalcode/agent.py +1084 -0
- minimalcode/cli.py +1520 -0
- minimalcode/clipboard.py +212 -0
- minimalcode/config.py +1065 -0
- minimalcode/defaults.json +491 -0
- minimalcode/errors.py +31 -0
- minimalcode/highlight/__init__.py +47 -0
- minimalcode/highlight/code.py +261 -0
- minimalcode/highlight/color.py +58 -0
- minimalcode/highlight/markdown.py +710 -0
- minimalcode/highlight/palette.py +73 -0
- minimalcode/highlight/sanitize.py +175 -0
- minimalcode/highlight/thinking.py +71 -0
- minimalcode/httpclient.py +278 -0
- minimalcode/importer.py +253 -0
- minimalcode/prompt.py +195 -0
- minimalcode/provider/__init__.py +28 -0
- minimalcode/provider/anthropic.py +805 -0
- minimalcode/provider/anthropic_env.py +161 -0
- minimalcode/provider/chat.py +394 -0
- minimalcode/provider/client.py +243 -0
- minimalcode/provider/common.py +176 -0
- minimalcode/provider/deepseek_web.py +1158 -0
- minimalcode/provider/factory.py +63 -0
- minimalcode/provider/responses.py +692 -0
- minimalcode/provider/sha3_wasm_bg.wasm +0 -0
- minimalcode/provider/transport.py +241 -0
- minimalcode/provider/types.py +54 -0
- minimalcode/sandbox/__init__.py +64 -0
- minimalcode/sandbox/audit.py +120 -0
- minimalcode/sandbox/backends.py +343 -0
- minimalcode/sandbox/execution.py +435 -0
- minimalcode/sandbox/network.py +793 -0
- minimalcode/sandbox/policy.py +253 -0
- minimalcode/sandbox/runner.py +347 -0
- minimalcode/sandbox/sensitive.py +286 -0
- minimalcode/session.py +465 -0
- minimalcode/shell.py +333 -0
- minimalcode/subagent.py +120 -0
- minimalcode/tools/__init__.py +46 -0
- minimalcode/tools/files.py +1139 -0
- minimalcode/tools/process.py +164 -0
- minimalcode/tools/remote.py +74 -0
- minimalcode/tools/runtime.py +219 -0
- minimalcode/tools/schemas.py +418 -0
- minimalcode/tools/search.py +521 -0
- minimalcode/tools/search_tools.py +583 -0
- minimalcode/tools/validation.py +99 -0
- minimalcode/vision.py +231 -0
- minimalcode/web.py +238 -0
- minimalcode-1.5.2.dist-info/METADATA +646 -0
- minimalcode-1.5.2.dist-info/RECORD +58 -0
- minimalcode-1.5.2.dist-info/WHEEL +5 -0
- minimalcode-1.5.2.dist-info/entry_points.txt +2 -0
- minimalcode-1.5.2.dist-info/licenses/LICENSE +201 -0
- minimalcode-1.5.2.dist-info/top_level.txt +1 -0
minimalcode/clipboard.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""Read an image from the system clipboard without requiring a file on disk."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import binascii
|
|
7
|
+
import re
|
|
8
|
+
import shutil
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
from .errors import ClipboardError
|
|
13
|
+
from .vision import SUPPORTED_IMAGE_FORMATS, image_media_type
|
|
14
|
+
|
|
15
|
+
_TIMEOUT_SECONDS = 10.0
|
|
16
|
+
_MAX_IMAGE_BYTES = 64 * 1024 * 1024
|
|
17
|
+
|
|
18
|
+
# Windows PowerShell has no direct "write bytes to stdout" primitive that
|
|
19
|
+
# survives the console encoding, so the PNG is emitted as base64.
|
|
20
|
+
_WINDOWS_SCRIPT = """
|
|
21
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
22
|
+
Add-Type -AssemblyName System.Drawing
|
|
23
|
+
$image = [System.Windows.Forms.Clipboard]::GetImage()
|
|
24
|
+
if ($null -eq $image) { exit 3 }
|
|
25
|
+
$stream = New-Object System.IO.MemoryStream
|
|
26
|
+
try {
|
|
27
|
+
$image.Save($stream, [System.Drawing.Imaging.ImageFormat]::Png)
|
|
28
|
+
[Convert]::ToBase64String($stream.ToArray())
|
|
29
|
+
} finally {
|
|
30
|
+
$stream.Dispose()
|
|
31
|
+
$image.Dispose()
|
|
32
|
+
}
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
# AppleScript renders pasted raw data as «data PNGf<hex>».
|
|
36
|
+
_MACOS_SCRIPT = "get the clipboard as «class PNGf»"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _platform() -> str:
|
|
40
|
+
return sys.platform
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def clipboard_image() -> tuple[str, bytes] | None:
|
|
44
|
+
"""Return ``(media_type, data)`` for the clipboard image, or None.
|
|
45
|
+
|
|
46
|
+
The caller never has to save a file first. A clipboard without an image is
|
|
47
|
+
not an error; a clipboard read failure raises :class:`ClipboardError`.
|
|
48
|
+
"""
|
|
49
|
+
if _platform() == "win32":
|
|
50
|
+
data = _windows_image()
|
|
51
|
+
elif _platform() == "darwin":
|
|
52
|
+
data = _macos_image()
|
|
53
|
+
else:
|
|
54
|
+
data = _linux_image()
|
|
55
|
+
if not data:
|
|
56
|
+
return None
|
|
57
|
+
if len(data) > _MAX_IMAGE_BYTES:
|
|
58
|
+
raise ClipboardError(
|
|
59
|
+
f"clipboard image is too large: {len(data)} bytes exceeds "
|
|
60
|
+
f"{_MAX_IMAGE_BYTES}"
|
|
61
|
+
)
|
|
62
|
+
media_type = image_media_type(data)
|
|
63
|
+
if media_type is None:
|
|
64
|
+
raise ClipboardError(f"clipboard image must be {SUPPORTED_IMAGE_FORMATS}")
|
|
65
|
+
return media_type, data
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _run(command: list[str], *, label: str) -> subprocess.CompletedProcess[bytes]:
|
|
69
|
+
try:
|
|
70
|
+
return subprocess.run(
|
|
71
|
+
command,
|
|
72
|
+
capture_output=True,
|
|
73
|
+
timeout=_TIMEOUT_SECONDS,
|
|
74
|
+
check=False,
|
|
75
|
+
)
|
|
76
|
+
except FileNotFoundError as exc:
|
|
77
|
+
raise ClipboardError(
|
|
78
|
+
f"{label} clipboard reader is not available: {command[0]}"
|
|
79
|
+
) from exc
|
|
80
|
+
except subprocess.TimeoutExpired as exc:
|
|
81
|
+
raise ClipboardError(f"{label} clipboard read timed out") from exc
|
|
82
|
+
except OSError as exc:
|
|
83
|
+
raise ClipboardError(f"{label} clipboard read failed: {exc}") from exc
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _first_error_line(completed: subprocess.CompletedProcess[bytes]) -> str:
|
|
87
|
+
# Only the Windows reader uses this, and redirected PowerShell stderr is
|
|
88
|
+
# written in the console code page rather than UTF-8; decoding it as UTF-8
|
|
89
|
+
# turned every non-ASCII character of a localized message into U+FFFD.
|
|
90
|
+
import locale
|
|
91
|
+
|
|
92
|
+
encoding = locale.getpreferredencoding(False) or "utf-8"
|
|
93
|
+
text = completed.stderr.decode(encoding, errors="replace").strip()
|
|
94
|
+
if not text:
|
|
95
|
+
return f"exit code {completed.returncode}"
|
|
96
|
+
return text.splitlines()[0][:200]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _windows_image() -> bytes:
|
|
100
|
+
executable = None
|
|
101
|
+
for name in ("powershell", "pwsh"):
|
|
102
|
+
executable = shutil.which(name)
|
|
103
|
+
if executable:
|
|
104
|
+
break
|
|
105
|
+
if executable is None:
|
|
106
|
+
raise ClipboardError("clipboard image paste needs PowerShell on Windows")
|
|
107
|
+
completed = _run(
|
|
108
|
+
[
|
|
109
|
+
executable,
|
|
110
|
+
"-NoLogo",
|
|
111
|
+
"-NoProfile",
|
|
112
|
+
"-STA",
|
|
113
|
+
"-NonInteractive",
|
|
114
|
+
"-Command",
|
|
115
|
+
_WINDOWS_SCRIPT,
|
|
116
|
+
],
|
|
117
|
+
label="Windows",
|
|
118
|
+
)
|
|
119
|
+
if completed.returncode == 3:
|
|
120
|
+
return b""
|
|
121
|
+
if completed.returncode != 0:
|
|
122
|
+
raise ClipboardError(
|
|
123
|
+
f"Windows clipboard read failed: {_first_error_line(completed)}"
|
|
124
|
+
)
|
|
125
|
+
return _decode_base64(completed.stdout)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _decode_base64(data: bytes) -> bytes:
|
|
129
|
+
compact = b"".join(data.split())
|
|
130
|
+
if not compact:
|
|
131
|
+
return b""
|
|
132
|
+
try:
|
|
133
|
+
return base64.b64decode(compact, validate=True)
|
|
134
|
+
except (binascii.Error, ValueError) as exc:
|
|
135
|
+
raise ClipboardError("clipboard reader returned invalid image data") from exc
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _macos_image() -> bytes:
|
|
139
|
+
completed = _run(["osascript", "-e", _MACOS_SCRIPT], label="macOS")
|
|
140
|
+
if completed.returncode != 0:
|
|
141
|
+
detail = completed.stderr.decode("utf-8", errors="replace").lower()
|
|
142
|
+
# An empty clipboard is not an error, but a refused Apple Event or a
|
|
143
|
+
# broken osascript invocation must not look like "no image".
|
|
144
|
+
no_image_markers = (
|
|
145
|
+
"no_png",
|
|
146
|
+
"can't make",
|
|
147
|
+
"can’t make",
|
|
148
|
+
"can't get",
|
|
149
|
+
"can’t get",
|
|
150
|
+
"(-1700)",
|
|
151
|
+
"(-1728)",
|
|
152
|
+
"no image",
|
|
153
|
+
)
|
|
154
|
+
if any(marker in detail for marker in no_image_markers):
|
|
155
|
+
return b""
|
|
156
|
+
raise ClipboardError(
|
|
157
|
+
f"macOS clipboard read failed: {_first_error_line(completed)}"
|
|
158
|
+
)
|
|
159
|
+
return _parse_applescript_data(completed.stdout)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _parse_applescript_data(data: bytes) -> bytes:
|
|
163
|
+
text = data.decode("utf-8", errors="replace")
|
|
164
|
+
match = re.search(r"PNGf([0-9A-Fa-f]+)", text)
|
|
165
|
+
if match is None:
|
|
166
|
+
return b""
|
|
167
|
+
try:
|
|
168
|
+
return bytes.fromhex(match.group(1))
|
|
169
|
+
except ValueError as exc:
|
|
170
|
+
raise ClipboardError("macOS clipboard returned invalid image data") from exc
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _linux_image() -> bytes:
|
|
174
|
+
candidates = (
|
|
175
|
+
("wl-paste", ["--type", "image/png"]),
|
|
176
|
+
("xclip", ["-selection", "clipboard", "-t", "image/png", "-o"]),
|
|
177
|
+
)
|
|
178
|
+
found = False
|
|
179
|
+
last_error: ClipboardError | None = None
|
|
180
|
+
for name, arguments in candidates:
|
|
181
|
+
executable = shutil.which(name)
|
|
182
|
+
if executable is None:
|
|
183
|
+
continue
|
|
184
|
+
found = True
|
|
185
|
+
try:
|
|
186
|
+
completed = _run([executable, *arguments], label=name)
|
|
187
|
+
except ClipboardError as exc:
|
|
188
|
+
# A missing Wayland session is common on X11; try the next reader,
|
|
189
|
+
# but remember why it failed so a real read error is not reported
|
|
190
|
+
# as an empty clipboard.
|
|
191
|
+
last_error = exc
|
|
192
|
+
continue
|
|
193
|
+
if completed.returncode == 0:
|
|
194
|
+
if completed.stdout:
|
|
195
|
+
return completed.stdout
|
|
196
|
+
# This reader answered cleanly with no image; that is an empty
|
|
197
|
+
# clipboard, not a failure of a previous candidate.
|
|
198
|
+
last_error = None
|
|
199
|
+
continue
|
|
200
|
+
last_error = ClipboardError(
|
|
201
|
+
f"{name} clipboard read failed: {_first_error_line(completed)}"
|
|
202
|
+
)
|
|
203
|
+
if last_error is not None:
|
|
204
|
+
raise last_error
|
|
205
|
+
if not found:
|
|
206
|
+
raise ClipboardError(
|
|
207
|
+
"clipboard image paste needs wl-paste (Wayland) or xclip (X11)"
|
|
208
|
+
)
|
|
209
|
+
return b""
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
__all__ = ["clipboard_image"]
|