llms-py 3.0.12__py3-none-any.whl → 3.0.14__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.
@@ -0,0 +1,461 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import os
4
+ import re
5
+ import shutil
6
+ import subprocess
7
+ import sys
8
+ from typing import Optional, Tuple
9
+
10
+
11
+ def get_screen_resolution() -> Tuple[int, int]:
12
+ """
13
+ Get the current screen resolution (width, height).
14
+
15
+ Supports Linux (Wayland/Hyprland, X11), macOS, and Windows.
16
+ Returns the primary monitor's resolution.
17
+
18
+ Returns:
19
+ Tuple[int, int]: (width, height) in pixels
20
+
21
+ Raises:
22
+ RuntimeError: If unable to determine screen resolution
23
+ """
24
+ if sys.platform == "linux":
25
+ return _get_linux_resolution()
26
+ elif sys.platform == "darwin":
27
+ return _get_macos_resolution()
28
+ elif sys.platform == "win32":
29
+ return _get_windows_resolution()
30
+ else:
31
+ raise RuntimeError(f"Unsupported platform: {sys.platform}")
32
+
33
+
34
+ def get_display_num() -> int:
35
+ """
36
+ Get the display number for the current session.
37
+
38
+ On Linux (X11): Returns the X display number from $DISPLAY (e.g., :0 -> 0)
39
+ On Linux (Wayland): Returns the Wayland display number from $WAYLAND_DISPLAY (e.g., wayland-0 -> 0)
40
+ On macOS: Returns the display ID of the main display
41
+ On Windows: Returns the index of the primary monitor (typically 0)
42
+
43
+ Returns:
44
+ int: The display number
45
+
46
+ Raises:
47
+ RuntimeError: If unable to determine display number
48
+ """
49
+ if sys.platform == "linux":
50
+ return _get_linux_display_num()
51
+ elif sys.platform == "darwin":
52
+ return _get_macos_display_num()
53
+ elif sys.platform == "win32":
54
+ return _get_windows_display_num()
55
+ else:
56
+ raise RuntimeError(f"Unsupported platform: {sys.platform}")
57
+
58
+
59
+ def _get_linux_display_num() -> int:
60
+ """Get display number on Linux (X11 or Wayland)."""
61
+
62
+ # Try X11 DISPLAY environment variable first
63
+ display = os.environ.get("DISPLAY")
64
+ if display:
65
+ # DISPLAY format: [hostname]:displaynumber[.screennumber]
66
+ # Examples: :0, :1, localhost:0, :0.0
67
+ match = re.search(r":(\d+)", display)
68
+ if match:
69
+ return int(match.group(1))
70
+
71
+ # Try Wayland display
72
+ wayland_display = os.environ.get("WAYLAND_DISPLAY")
73
+ if wayland_display:
74
+ # WAYLAND_DISPLAY format: wayland-N or just a socket name
75
+ # Examples: wayland-0, wayland-1
76
+ match = re.search(r"wayland-(\d+)", wayland_display)
77
+ if match:
78
+ return int(match.group(1))
79
+ # If it's just "wayland-0" style, try to extract number
80
+ match = re.search(r"(\d+)", wayland_display)
81
+ if match:
82
+ return int(match.group(1))
83
+ # Default to 0 if we have a Wayland display but can't parse number
84
+ return 0
85
+
86
+ # Try Hyprland-specific: get focused monitor ID
87
+ if shutil.which("hyprctl"):
88
+ try:
89
+ result = subprocess.run(["hyprctl", "monitors", "-j"], capture_output=True, text=True, timeout=5)
90
+ if result.returncode == 0:
91
+ import json
92
+
93
+ monitors = json.loads(result.stdout)
94
+ if monitors:
95
+ # Return focused monitor ID or first monitor's ID
96
+ monitor = next((m for m in monitors if m.get("focused")), monitors[0])
97
+ return monitor.get("id", 0)
98
+ except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError):
99
+ pass
100
+
101
+ raise RuntimeError("Could not determine display number. Neither DISPLAY nor WAYLAND_DISPLAY is set.")
102
+
103
+
104
+ def _get_macos_display_num() -> int:
105
+ """Get display number on macOS."""
106
+
107
+ # Try using CoreGraphics via ctypes
108
+ try:
109
+ import ctypes
110
+ import ctypes.util
111
+
112
+ cg_path = ctypes.util.find_library("CoreGraphics")
113
+ if cg_path:
114
+ cg = ctypes.CDLL(cg_path)
115
+ # CGMainDisplayID returns the display ID of the main display
116
+ cg.CGMainDisplayID.restype = ctypes.c_uint32
117
+ return cg.CGMainDisplayID()
118
+ except (OSError, AttributeError):
119
+ pass
120
+
121
+ # Try using AppKit
122
+ try:
123
+ from AppKit import NSScreen
124
+
125
+ main_screen = NSScreen.mainScreen()
126
+ # Get the display ID from the screen's deviceDescription
127
+ device_desc = main_screen.deviceDescription()
128
+ display_id = device_desc.get("NSScreenNumber", 0)
129
+ return int(display_id)
130
+ except ImportError:
131
+ pass
132
+
133
+ # Try using system_profiler
134
+ try:
135
+ result = subprocess.run(["system_profiler", "SPDisplaysDataType"], capture_output=True, text=True, timeout=10)
136
+ if result.returncode == 0:
137
+ # Look for Display ID or just return 0 for main display
138
+ match = re.search(r"Display ID:\s*(\d+)", result.stdout)
139
+ if match:
140
+ return int(match.group(1))
141
+ # If we found display info but no ID, assume main display is 0
142
+ if "Resolution:" in result.stdout:
143
+ return 0
144
+ except subprocess.TimeoutExpired:
145
+ pass
146
+
147
+ # Default to 0 for main display
148
+ return 0
149
+
150
+
151
+ def _get_windows_display_num() -> int:
152
+ """Get display number on Windows."""
153
+
154
+ # Method 1: Try ctypes to enumerate monitors
155
+ try:
156
+ import ctypes
157
+ from ctypes import wintypes
158
+
159
+ user32 = ctypes.windll.user32
160
+
161
+ # Get the primary monitor handle
162
+ # MonitorFromPoint with MONITOR_DEFAULTTOPRIMARY (0x00000001)
163
+ primary_monitor = user32.MonitorFromPoint(
164
+ ctypes.wintypes.POINT(0, 0),
165
+ 1, # MONITOR_DEFAULTTOPRIMARY
166
+ )
167
+
168
+ # Enumerate all monitors to find the index of the primary
169
+ monitors = []
170
+
171
+ def callback(hMonitor, hdcMonitor, lprcMonitor, dwData):
172
+ monitors.append(hMonitor)
173
+ return True
174
+
175
+ MONITORENUMPROC = ctypes.WINFUNCTYPE(
176
+ ctypes.c_bool,
177
+ ctypes.wintypes.HMONITOR,
178
+ ctypes.wintypes.HDC,
179
+ ctypes.POINTER(ctypes.wintypes.RECT),
180
+ ctypes.wintypes.LPARAM,
181
+ )
182
+
183
+ user32.EnumDisplayMonitors(None, None, MONITORENUMPROC(callback), 0)
184
+
185
+ # Find the index of the primary monitor
186
+ for i, mon in enumerate(monitors):
187
+ if mon == primary_monitor:
188
+ return i
189
+
190
+ # Primary not found in list, return 0
191
+ return 0
192
+
193
+ except (AttributeError, OSError):
194
+ pass
195
+
196
+ # Method 2: Try win32api
197
+ try:
198
+ import win32api
199
+
200
+ # Get all monitors
201
+ monitors = win32api.EnumDisplayMonitors()
202
+ # Find primary (usually the first one, or check flags)
203
+ for i, (handle, dc, rect) in enumerate(monitors):
204
+ info = win32api.GetMonitorInfo(handle)
205
+ if info.get("Flags", 0) & 1: # MONITORINFOF_PRIMARY
206
+ return i
207
+ return 0
208
+ except ImportError:
209
+ pass
210
+
211
+ # Method 3: PowerShell to get monitor index
212
+ try:
213
+ ps_script = """
214
+ Add-Type -AssemblyName System.Windows.Forms
215
+ $screens = [System.Windows.Forms.Screen]::AllScreens
216
+ for ($i = 0; $i -lt $screens.Count; $i++) {
217
+ if ($screens[$i].Primary) {
218
+ Write-Output $i
219
+ break
220
+ }
221
+ }
222
+ """
223
+ result = subprocess.run(
224
+ ["powershell", "-NoProfile", "-Command", ps_script], capture_output=True, text=True, timeout=10
225
+ )
226
+ if result.returncode == 0:
227
+ return int(result.stdout.strip())
228
+ except (subprocess.TimeoutExpired, FileNotFoundError, ValueError):
229
+ pass
230
+
231
+ # Default to 0 (primary monitor)
232
+ return 0
233
+
234
+
235
+ def _get_linux_resolution() -> Tuple[int, int]:
236
+ """Get resolution on Linux, trying Wayland first, then X11."""
237
+
238
+ # Try Hyprland first
239
+ resolution = _try_hyprctl()
240
+ if resolution:
241
+ return resolution
242
+
243
+ # Try wlr-randr (generic Wayland)
244
+ resolution = _try_wlr_randr()
245
+ if resolution:
246
+ return resolution
247
+
248
+ # Try xrandr (X11)
249
+ resolution = _try_xrandr()
250
+ if resolution:
251
+ return resolution
252
+
253
+ # Try xdpyinfo (X11 fallback)
254
+ resolution = _try_xdpyinfo()
255
+ if resolution:
256
+ return resolution
257
+
258
+ raise RuntimeError("Could not determine screen resolution. No supported display server found.")
259
+
260
+
261
+ def _try_hyprctl() -> Optional[Tuple[int, int]]:
262
+ """Try getting resolution via hyprctl (Hyprland)."""
263
+ if not shutil.which("hyprctl"):
264
+ return None
265
+
266
+ try:
267
+ result = subprocess.run(["hyprctl", "monitors", "-j"], capture_output=True, text=True, timeout=5)
268
+ if result.returncode == 0:
269
+ import json
270
+
271
+ monitors = json.loads(result.stdout)
272
+ if monitors:
273
+ # Get the focused monitor or first one
274
+ monitor = next((m for m in monitors if m.get("focused")), monitors[0])
275
+ return (monitor["width"], monitor["height"])
276
+ except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError):
277
+ pass
278
+ return None
279
+
280
+
281
+ def _try_wlr_randr() -> Optional[Tuple[int, int]]:
282
+ """Try getting resolution via wlr-randr (Wayland)."""
283
+ if not shutil.which("wlr-randr"):
284
+ return None
285
+
286
+ try:
287
+ result = subprocess.run(["wlr-randr"], capture_output=True, text=True, timeout=5)
288
+ if result.returncode == 0:
289
+ # Parse output like: " 3840x2160 px, 60.000000 Hz (current)"
290
+ match = re.search(r"(\d+)x(\d+)\s+px.*current", result.stdout)
291
+ if match:
292
+ return (int(match.group(1)), int(match.group(2)))
293
+ except subprocess.TimeoutExpired:
294
+ pass
295
+ return None
296
+
297
+
298
+ def _try_xrandr() -> Optional[Tuple[int, int]]:
299
+ """Try getting resolution via xrandr (X11)."""
300
+ if not shutil.which("xrandr"):
301
+ return None
302
+
303
+ try:
304
+ result = subprocess.run(["xrandr", "--current"], capture_output=True, text=True, timeout=5)
305
+ if result.returncode == 0:
306
+ # Look for current resolution marked with *
307
+ match = re.search(r"(\d+)x(\d+).*\*", result.stdout)
308
+ if match:
309
+ return (int(match.group(1)), int(match.group(2)))
310
+ except subprocess.TimeoutExpired:
311
+ pass
312
+ return None
313
+
314
+
315
+ def _try_xdpyinfo() -> Optional[Tuple[int, int]]:
316
+ """Try getting resolution via xdpyinfo (X11)."""
317
+ if not shutil.which("xdpyinfo"):
318
+ return None
319
+
320
+ try:
321
+ result = subprocess.run(["xdpyinfo"], capture_output=True, text=True, timeout=5)
322
+ if result.returncode == 0:
323
+ match = re.search(r"dimensions:\s+(\d+)x(\d+)", result.stdout)
324
+ if match:
325
+ return (int(match.group(1)), int(match.group(2)))
326
+ except subprocess.TimeoutExpired:
327
+ pass
328
+ return None
329
+
330
+
331
+ def _get_macos_resolution() -> Tuple[int, int]:
332
+ """Get resolution on macOS using system_profiler."""
333
+ try:
334
+ result = subprocess.run(["system_profiler", "SPDisplaysDataType"], capture_output=True, text=True, timeout=10)
335
+ if result.returncode == 0:
336
+ # Look for "Resolution: 2560 x 1440" or similar
337
+ match = re.search(r"Resolution:\s+(\d+)\s*x\s*(\d+)", result.stdout)
338
+ if match:
339
+ return (int(match.group(1)), int(match.group(2)))
340
+ except subprocess.TimeoutExpired:
341
+ pass
342
+
343
+ # Fallback: try using AppKit via Python (if available)
344
+ try:
345
+ from AppKit import NSScreen
346
+
347
+ frame = NSScreen.mainScreen().frame()
348
+ return (int(frame.size.width), int(frame.size.height))
349
+ except ImportError:
350
+ pass
351
+
352
+ raise RuntimeError("Could not determine screen resolution on macOS")
353
+
354
+
355
+ def _get_windows_resolution() -> Tuple[int, int]:
356
+ """Get resolution on Windows."""
357
+
358
+ # Method 1: Try ctypes (no external dependencies)
359
+ resolution = _try_windows_ctypes()
360
+ if resolution:
361
+ return resolution
362
+
363
+ # Method 2: Try win32api (pywin32)
364
+ resolution = _try_windows_win32api()
365
+ if resolution:
366
+ return resolution
367
+
368
+ # Method 3: Try PowerShell
369
+ resolution = _try_windows_powershell()
370
+ if resolution:
371
+ return resolution
372
+
373
+ # Method 4: Try wmic (deprecated but still works on older systems)
374
+ resolution = _try_windows_wmic()
375
+ if resolution:
376
+ return resolution
377
+
378
+ raise RuntimeError("Could not determine screen resolution on Windows")
379
+
380
+
381
+ def _try_windows_ctypes() -> Optional[Tuple[int, int]]:
382
+ """Try getting resolution via ctypes (built-in)."""
383
+ try:
384
+ import ctypes
385
+
386
+ user32 = ctypes.windll.user32
387
+ user32.SetProcessDPIAware() # Handle DPI scaling
388
+ width = user32.GetSystemMetrics(0) # SM_CXSCREEN
389
+ height = user32.GetSystemMetrics(1) # SM_CYSCREEN
390
+ if width > 0 and height > 0:
391
+ return (width, height)
392
+ except (AttributeError, OSError):
393
+ pass
394
+ return None
395
+
396
+
397
+ def _try_windows_win32api() -> Optional[Tuple[int, int]]:
398
+ """Try getting resolution via win32api (pywin32)."""
399
+ try:
400
+ import win32api
401
+
402
+ width = win32api.GetSystemMetrics(0)
403
+ height = win32api.GetSystemMetrics(1)
404
+ if width > 0 and height > 0:
405
+ return (width, height)
406
+ except ImportError:
407
+ pass
408
+ return None
409
+
410
+
411
+ def _try_windows_powershell() -> Optional[Tuple[int, int]]:
412
+ """Try getting resolution via PowerShell."""
413
+ try:
414
+ # Using Add-Type to access System.Windows.Forms
415
+ ps_script = """
416
+ Add-Type -AssemblyName System.Windows.Forms
417
+ $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
418
+ "$($screen.Width)x$($screen.Height)"
419
+ """
420
+ result = subprocess.run(
421
+ ["powershell", "-NoProfile", "-Command", ps_script], capture_output=True, text=True, timeout=10
422
+ )
423
+ if result.returncode == 0:
424
+ match = re.match(r"(\d+)x(\d+)", result.stdout.strip())
425
+ if match:
426
+ return (int(match.group(1)), int(match.group(2)))
427
+ except (subprocess.TimeoutExpired, FileNotFoundError):
428
+ pass
429
+ return None
430
+
431
+
432
+ def _try_windows_wmic() -> Optional[Tuple[int, int]]:
433
+ """Try getting resolution via wmic (legacy, but works on older Windows)."""
434
+ try:
435
+ result = subprocess.run(
436
+ ["wmic", "path", "Win32_VideoController", "get", "CurrentHorizontalResolution,CurrentVerticalResolution"],
437
+ capture_output=True,
438
+ text=True,
439
+ timeout=10,
440
+ )
441
+ if result.returncode == 0:
442
+ lines = result.stdout.strip().split("\n")
443
+ for line in lines[1:]: # Skip header
444
+ match = re.match(r"(\d+)\s+(\d+)", line.strip())
445
+ if match:
446
+ return (int(match.group(1)), int(match.group(2)))
447
+ except (subprocess.TimeoutExpired, FileNotFoundError):
448
+ pass
449
+ return None
450
+
451
+
452
+ if __name__ == "__main__":
453
+ try:
454
+ width, height = get_screen_resolution()
455
+ print(f"Screen resolution: {width}x{height}")
456
+
457
+ display_num = get_display_num()
458
+ print(f"Display number: {display_num}")
459
+ except RuntimeError as e:
460
+ print(f"Error: {e}", file=sys.stderr)
461
+ sys.exit(1)
@@ -0,0 +1,37 @@
1
+ """Utility to run shell commands asynchronously with a timeout."""
2
+
3
+ import asyncio
4
+ import contextlib
5
+
6
+ TRUNCATED_MESSAGE: str = "<response clipped><NOTE>To save on context only part of this file has been shown to you. You should retry this tool after you have searched inside the file with `grep -n` in order to find the line numbers of what you are looking for.</NOTE>"
7
+ MAX_RESPONSE_LEN: int = 16000
8
+
9
+
10
+ def maybe_truncate(content: str, truncate_after: int | None = MAX_RESPONSE_LEN):
11
+ """Truncate content and append a notice if content exceeds the specified length."""
12
+ return (
13
+ content
14
+ if not truncate_after or len(content) <= truncate_after
15
+ else content[:truncate_after] + TRUNCATED_MESSAGE
16
+ )
17
+
18
+
19
+ async def run(
20
+ cmd: str,
21
+ timeout: float | None = 120.0, # seconds
22
+ truncate_after: int | None = MAX_RESPONSE_LEN,
23
+ ):
24
+ """Run a shell command asynchronously with a timeout."""
25
+ process = await asyncio.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
26
+
27
+ try:
28
+ stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
29
+ return (
30
+ process.returncode or 0,
31
+ maybe_truncate(stdout.decode(), truncate_after=truncate_after),
32
+ maybe_truncate(stderr.decode(), truncate_after=truncate_after),
33
+ )
34
+ except asyncio.TimeoutError as exc:
35
+ with contextlib.suppress(ProcessLookupError):
36
+ process.kill()
37
+ raise TimeoutError(f"Command '{cmd}' timed out after {timeout} seconds") from exc
@@ -83,9 +83,10 @@ def install_anthropic(ctx):
83
83
 
84
84
  content = message.get("content", "")
85
85
  if isinstance(content, str):
86
- if anthropic_message["content"]:
87
- # If we have thinking, we must use blocks for text
88
- anthropic_message["content"].append({"type": "text", "text": content})
86
+ if anthropic_message["content"] or message.get("tool_calls"):
87
+ # If we have thinking or tools, we must use blocks for text
88
+ if content:
89
+ anthropic_message["content"].append({"type": "text", "text": content})
89
90
  else:
90
91
  anthropic_message["content"] = content
91
92
  elif isinstance(content, list):
@@ -108,6 +109,24 @@ def install_anthropic(ctx):
108
109
  }
109
110
  )
110
111
 
112
+ # Handle tool_calls
113
+ if "tool_calls" in message and message["tool_calls"]:
114
+ # specific check for content being a string and not empty, because we might have converted it above
115
+ if isinstance(anthropic_message["content"], str):
116
+ anthropic_message["content"] = []
117
+ if content:
118
+ anthropic_message["content"].append({"type": "text", "text": content})
119
+
120
+ for tool_call in message["tool_calls"]:
121
+ function = tool_call.get("function", {})
122
+ tool_use = {
123
+ "type": "tool_use",
124
+ "id": tool_call.get("id"),
125
+ "name": function.get("name"),
126
+ "input": json.loads(function.get("arguments", "{}")),
127
+ }
128
+ anthropic_message["content"].append(tool_use)
129
+
111
130
  anthropic_request["messages"].append(anthropic_message)
112
131
 
113
132
  # Handle max_tokens (required by Anthropic, uses max_tokens not max_completion_tokens)
@@ -18,7 +18,7 @@ const ToolResult = {
18
18
  preview
19
19
  </span>
20
20
  </div>
21
- <div class="not-prose px-3 py-2">
21
+ <div class="not-prose py-2">
22
22
  <pre v-if="ext.prefs.toolFormat !== 'preview'" class="tool-output">{{ origResult }}</pre>
23
23
  <div v-else>
24
24
  <ViewTypes v-if="Array.isArray(result)" :results="result" />
@@ -191,7 +191,7 @@ const Tools = {
191
191
  </div>
192
192
 
193
193
  <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
194
- <div v-for="tool in filteredTools" :key="tool.function.name"
194
+ <div v-for="tool in filteredTools" :key="tool.function.name" :id="'tool-' + tool.function.name"
195
195
  class="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden flex flex-col">
196
196
 
197
197
  <div class="p-4 border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50 flex justify-between items-center">
@@ -262,7 +262,6 @@ const Tools = {
262
262
  const ctx = inject('ctx')
263
263
 
264
264
  // Execution State
265
- const executingTool = ref(null)
266
265
  const execForm = ref({})
267
266
  const execResult = ref(null)
268
267
  const execError = ref(null)
@@ -270,6 +269,12 @@ const Tools = {
270
269
  const refForm = ref()
271
270
  const refTop = ref()
272
271
 
272
+ const executingTool = computed(() => {
273
+ const tool = ext.prefs.selectedTool
274
+ if (!tool) return null
275
+ return ctx.state.tool.definitions.find(x => x.function.name === tool)
276
+ })
277
+
273
278
  // UI State
274
279
  const expandedDescriptions = ref({})
275
280
 
@@ -284,7 +289,7 @@ const Tools = {
284
289
  })
285
290
 
286
291
  function startExec(tool) {
287
- executingTool.value = tool
292
+ ext.setPrefs({ selectedTool: tool.function.name })
288
293
  execForm.value = {}
289
294
  execResult.value = null
290
295
  execError.value = null
@@ -309,7 +314,7 @@ const Tools = {
309
314
  }
310
315
 
311
316
  function closeExec() {
312
- executingTool.value = null
317
+ ext.setPrefs({ selectedTool: null })
313
318
  execForm.value = {}
314
319
  execResult.value = null
315
320
  execError.value = null
@@ -588,9 +593,16 @@ function useTools(ctx) {
588
593
  Object.assign(toolPageHeaders, components)
589
594
  }
590
595
 
596
+ function selectTool({ group, tool }) {
597
+ ext.setPrefs({ selectedGroup: group, selectedTool: tool })
598
+ }
599
+
591
600
  return {
592
601
  toolPageHeaders,
593
602
  setToolPageHeaders,
603
+ selectTool,
604
+ get selectedGroup() { return ext.prefs.selectedGroup },
605
+ get selectedTool() { return ext.prefs.selectedTool },
594
606
  }
595
607
  }
596
608