@followbin/dsh-selection 0.0.0-stage → 0.2.0

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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +151 -2
  3. package/README.zh-CN.md +152 -0
  4. package/THIRD_PARTY_NOTICES.md +22 -0
  5. package/client.js +1070 -0
  6. package/cordis.patch.yml +4 -0
  7. package/icon.svg +1 -0
  8. package/index.js +41 -0
  9. package/locale/en.json +6 -0
  10. package/locale/zh.json +6 -0
  11. package/package.json +90 -4
  12. package/src/host/files.mjs +29 -0
  13. package/src/host/http.mjs +153 -0
  14. package/src/host/llm.mjs +194 -0
  15. package/src/host/log.mjs +40 -0
  16. package/src/host/service.mjs +334 -0
  17. package/src/host/settings.mjs +282 -0
  18. package/src/host/skills.mjs +88 -0
  19. package/src/host/supervisor.mjs +126 -0
  20. package/src/host/usage.mjs +128 -0
  21. package/src/shared/icons.mjs +66 -0
  22. package/src/shared/open-with-system.mjs +15 -0
  23. package/src/shared/paths.mjs +7 -0
  24. package/src/shared/protocol.mjs +4 -0
  25. package/src/worker/controller.mjs +274 -0
  26. package/src/worker/main.mjs +147 -0
  27. package/src/worker/native-window.mjs +319 -0
  28. package/src/worker/open-url.mjs +20 -0
  29. package/src/worker/overlay.mjs +266 -0
  30. package/src/worker/page.mjs +14 -0
  31. package/src/worker/placement.mjs +58 -0
  32. package/src/worker/speech.mjs +136 -0
  33. package/ui/markdown.js +122 -0
  34. package/ui/toolbar.css +162 -0
  35. package/ui/toolbar.html +54 -0
  36. package/ui/toolbar.js +552 -0
  37. package/vendor/selection-hook/LICENSE +21 -0
  38. package/vendor/selection-hook/index.d.ts +517 -0
  39. package/vendor/selection-hook/index.js +662 -0
  40. package/vendor/selection-hook/package.json +10 -0
  41. package/vendor/selection-hook/prebuilds/darwin-arm64/selection-hook.node +0 -0
  42. package/vendor/selection-hook/prebuilds/darwin-x64/selection-hook.node +0 -0
  43. package/vendor/selection-hook/prebuilds/win32-arm64/selection-hook.node +0 -0
  44. package/vendor/selection-hook/prebuilds/win32-x64/selection-hook.node +0 -0
@@ -0,0 +1,517 @@
1
+ /**
2
+ * Node Selection Hook
3
+ *
4
+ * This module provides a Node.js interface for monitoring text selections
5
+ * across applications on Windows, macOS, and Linux using platform-specific
6
+ * accessibility and input APIs.
7
+ */
8
+
9
+ import { EventEmitter } from "events";
10
+
11
+ /**
12
+ * Represents a 2D coordinate point in screen coordinates (pixels).
13
+ */
14
+ export interface Point {
15
+ x: number;
16
+ y: number;
17
+ }
18
+
19
+ /**
20
+ * Text selection data returned by the native module
21
+ *
22
+ * Contains the selected text and its position information.
23
+ * Position coordinates are in screen coordinates (pixels).
24
+ *
25
+ * On Linux, `startTop`/`startBottom`/`endTop`/`endBottom` are always
26
+ * `-99999` (INVALID_COORDINATE) because selection bounding rectangles are
27
+ * not available. On Linux Wayland, `mousePosStart`/`mousePosEnd` may also
28
+ * be `-99999` when the coordinate source cannot provide screen positions.
29
+ */
30
+ export interface TextSelectionData {
31
+ /** The selected text content */
32
+ text: string;
33
+ /** The program name that triggered the selection */
34
+ programName: string;
35
+ /** First paragraph's top-left point (x, y) in pixels */
36
+ startTop: Point;
37
+ /** First paragraph's bottom-left point (x, y) in pixels */
38
+ startBottom: Point;
39
+ /** Last paragraph's top-right point (x, y) in pixels */
40
+ endTop: Point;
41
+ /** Last paragraph's bottom-right point (x, y) in pixels */
42
+ endBottom: Point;
43
+ /** Mouse position when selection started (x, y) in pixels */
44
+ mousePosStart: Point;
45
+ /** Mouse position when selection ended (x, y) in pixels */
46
+ mousePosEnd: Point;
47
+ /** Selection method identifier */
48
+ method: (typeof SelectionHook.SelectionMethod)[keyof typeof SelectionHook.SelectionMethod];
49
+ /** Position level identifier */
50
+ posLevel: (typeof SelectionHook.PositionLevel)[keyof typeof SelectionHook.PositionLevel];
51
+ /** Whether the current app's front window is in fullscreen mode, macOS only */
52
+ isFullscreen?: boolean;
53
+ }
54
+
55
+ /**
56
+ * Mouse event data structure
57
+ *
58
+ * Contains information about mouse events such as clicks and movements.
59
+ * Coordinates are in screen coordinates (pixels).
60
+ * On Linux Wayland, `x`/`y` may be `-99999` (INVALID_COORDINATE) when
61
+ * the coordinate source cannot provide screen positions.
62
+ */
63
+ export interface MouseEventData {
64
+ /** X coordinate of mouse pointer (px) */
65
+ x: number;
66
+ /** Y coordinate of mouse pointer (px) */
67
+ y: number;
68
+ /** Mouse button identifier,
69
+ * same as WebAPIs' MouseEvent.button
70
+ * None = -1, Left = 0, Middle = 1, Right = 2, Back = 3, Forward = 4,
71
+ * Unknown = 99
72
+ */
73
+ button: number;
74
+ }
75
+
76
+ /**
77
+ * Mouse wheel event data structure
78
+ *
79
+ * Contains information about mouse wheel events.
80
+ * On Linux Wayland, `x`/`y` may be `-99999` (INVALID_COORDINATE) when
81
+ * the coordinate source cannot provide screen positions.
82
+ */
83
+ export interface MouseWheelEventData {
84
+ /** X coordinate of mouse pointer (px) */
85
+ x: number;
86
+ /** Y coordinate of mouse pointer (px) */
87
+ y: number;
88
+ /** Mouse wheel button type
89
+ * 0: Vertical
90
+ * 1: Horizontal
91
+ */
92
+ button: number;
93
+ /** Mouse wheel direction flag
94
+ * 1: Up/Right
95
+ * -1: Down/Left
96
+ */
97
+ flag: number;
98
+ }
99
+
100
+ /**
101
+ * Keyboard event data structure
102
+ *
103
+ * Contains information about keyboard events such as key presses and releases.
104
+ */
105
+ export interface KeyboardEventData {
106
+ /**
107
+ * Unified key value of the vkCode. Same as MDN `KeyboardEvent.key` values.
108
+ * Converted from platform-specific vkCode.
109
+ *
110
+ * Values defined in https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
111
+ */
112
+ uniKey: string;
113
+ /** Virtual key code. The value is different on different platforms.
114
+ *
115
+ * Windows: VK_* values of vkCode, refer to https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
116
+ * macOS: kVK_* values of kCGKeyboardEventKeycode, defined in `HIToolbox/Events.h`
117
+ * Linux: KEY_* values from `<linux/input-event-codes.h>`, refer to https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
118
+ */
119
+ vkCode: number;
120
+ /** Whether modifier keys (Alt/Ctrl/Win/⌘/⌥/Fn) are pressed simultaneously */
121
+ sys: boolean;
122
+ /** Keyboard scan code. Windows Only. */
123
+ scanCode?: number;
124
+ /** Additional key flags. Varies on different platforms.
125
+ *
126
+ * Linux: Modifier bitmask — 0x01 Shift, 0x02 Ctrl, 0x04 Alt, 0x08 Meta(Super)
127
+ */
128
+ flags: number;
129
+ }
130
+
131
+ /**
132
+ * Configuration interface for text selection monitoring
133
+ *
134
+ * Contains settings that control the behavior of the text selection hook
135
+ * and its various features like mouse tracking and clipboard fallback.
136
+ */
137
+ export interface SelectionConfig {
138
+ /** Enable debug logging for warnings and errors */
139
+ debug?: boolean;
140
+ /** Enable high CPU usage mouse movement tracking */
141
+ enableMouseMoveEvent?: boolean;
142
+ /** Enable clipboard fallback for text selection */
143
+ enableClipboard?: boolean;
144
+ /** Enable passive mode where selection requires manual trigger */
145
+ selectionPassiveMode?: boolean;
146
+ /** Mode for clipboard fallback behavior */
147
+ clipboardMode?: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode];
148
+ /** Mode for global filter behavior */
149
+ globalFilterMode?: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode];
150
+ /** List of program names for clipboard mode filtering */
151
+ clipboardFilterList?: string[];
152
+ /** List of program names for global filter mode filtering */
153
+ globalFilterList?: string[];
154
+ }
155
+
156
+ /**
157
+ * Linux environment information returned by linuxGetEnvInfo()
158
+ */
159
+ export interface LinuxEnvInfo {
160
+ /** Display protocol: 0=Unknown, 1=X11, 2=Wayland */
161
+ displayProtocol: (typeof SelectionHook.DisplayProtocol)[keyof typeof SelectionHook.DisplayProtocol];
162
+ /** Compositor type: 0=Unknown, 1=KWin, 2=Mutter, 3=Hyprland, 4=Sway, 5=Wlroots, 6=CosmicComp */
163
+ compositorType: (typeof SelectionHook.CompositorType)[keyof typeof SelectionHook.CompositorType];
164
+ /** Whether the user can access input devices (needed for Wayland input monitoring) */
165
+ hasInputDeviceAccess: boolean;
166
+ /** Whether the process is running as root */
167
+ isRoot: boolean;
168
+ }
169
+
170
+ /**
171
+ * SelectionHook - Main class for text selection monitoring
172
+ *
173
+ * This class provides methods to start/stop monitoring text selections
174
+ * across applications on Windows, macOS, and Linux, and emits events when selections occur.
175
+ */
176
+ declare class SelectionHook extends EventEmitter {
177
+ static SelectionMethod: {
178
+ NONE: 0;
179
+ UIA: 1;
180
+ /** @deprecated This method has been removed */
181
+ FOCUSCTL: 2;
182
+ ACCESSIBLE: 3;
183
+ AXAPI: 11;
184
+ /** @reserved AT-SPI method is reserved for future use */
185
+ ATSPI: 21;
186
+ PRIMARY: 22;
187
+ CLIPBOARD: 99;
188
+ };
189
+
190
+ static PositionLevel: {
191
+ NONE: 0;
192
+ MOUSE_SINGLE: 1;
193
+ MOUSE_DUAL: 2;
194
+ SEL_FULL: 3;
195
+ SEL_DETAILED: 4;
196
+ };
197
+
198
+ static FilterMode: {
199
+ DEFAULT: 0;
200
+ INCLUDE_LIST: 1;
201
+ EXCLUDE_LIST: 2;
202
+ };
203
+
204
+ static FineTunedListType: {
205
+ EXCLUDE_CLIPBOARD_CURSOR_DETECT: 0;
206
+ INCLUDE_CLIPBOARD_DELAY_READ: 1;
207
+ };
208
+
209
+ /**
210
+ * Sentinel value indicating coordinates are unavailable (-99999).
211
+ * Returned when the coordinate source (e.g. libevdev on Wayland) cannot
212
+ * provide actual screen positions. Check coordinate fields against this
213
+ * value before using them for positioning.
214
+ */
215
+ static INVALID_COORDINATE: -99999;
216
+
217
+ static DisplayProtocol: {
218
+ UNKNOWN: 0;
219
+ X11: 1;
220
+ WAYLAND: 2;
221
+ };
222
+
223
+ /**
224
+ * Compositor type constants (Linux only).
225
+ *
226
+ * Values represent the compositor, not the desktop environment (DE).
227
+ * DE-bundled compositors are detected via XDG_CURRENT_DESKTOP:
228
+ * KDE Plasma → KWin, GNOME → mutter, COSMIC → cosmic-comp.
229
+ * Standalone compositors are detected via their own env vars:
230
+ * Hyprland → HYPRLAND_INSTANCE_SIGNATURE, sway → SWAYSOCK.
231
+ * WLROOTS is a catch-all for generic wlroots-based compositors.
232
+ */
233
+ static CompositorType: {
234
+ UNKNOWN: 0;
235
+ /** KWin — KDE Plasma's compositor (kwin_wayland) */
236
+ KWIN: 1;
237
+ /** mutter — GNOME's compositor (mutter / gnome-shell) */
238
+ MUTTER: 2;
239
+ /** Hyprland — standalone tiling compositor */
240
+ HYPRLAND: 3;
241
+ /** sway — standalone i3-compatible compositor */
242
+ SWAY: 4;
243
+ /** wlroots-based compositors (labwc, river, etc.) */
244
+ WLROOTS: 5;
245
+ /** cosmic-comp — System76 COSMIC's compositor */
246
+ COSMIC_COMP: 6;
247
+ };
248
+
249
+ /**
250
+ * Start monitoring text selections
251
+ *
252
+ * Initiates the native hooks to listen for text selection events
253
+ * across all applications. This must be called before any events
254
+ * will be emitted.
255
+ *
256
+ * Configuration methods (e.g., enableClipboard, setGlobalFilterMode) can be
257
+ * called before start(). If start() is called with a config object, the config
258
+ * values will override any pre-start settings.
259
+ *
260
+ * @param config Optional configuration object
261
+ * @returns Success status (true if started successfully)
262
+ */
263
+ start(config?: SelectionConfig | null): boolean;
264
+
265
+ /**
266
+ * Stop monitoring text selections
267
+ *
268
+ * Stops the native hooks and prevents further events from being emitted.
269
+ * This should be called when monitoring is no longer needed to free resources.
270
+ *
271
+ * @returns Success status (true if stopped successfully or wasn't running)
272
+ */
273
+ stop(): boolean;
274
+
275
+ /**
276
+ * Check if hook is running
277
+ *
278
+ * Determines if the selection monitoring is currently active.
279
+ *
280
+ * @returns Running status (true if monitoring is active)
281
+ */
282
+ isRunning(): boolean;
283
+
284
+ /**
285
+ * Get current text selection
286
+ *
287
+ * Retrieves the current text selection, if any exists.
288
+ * Returns null if no text is currently selected or hook isn't running.
289
+ *
290
+ * @returns Current selection data or null if no selection exists
291
+ */
292
+ getCurrentSelection(): TextSelectionData | null;
293
+
294
+ /**
295
+ * Enable mousemove events (high CPU usage)
296
+ *
297
+ * Enables "mouse-move" events to be emitted when the mouse moves.
298
+ * Note: This can cause high CPU usage due to frequent event firing.
299
+ * Can be called before start().
300
+ *
301
+ * @returns Success status (true if enabled successfully)
302
+ */
303
+ enableMouseMoveEvent(): boolean;
304
+
305
+ /**
306
+ * Disable mousemove events
307
+ *
308
+ * Stops emitting "mouse-move" events to reduce CPU usage.
309
+ * This is the default state after starting the hook.
310
+ * Can be called before start().
311
+ *
312
+ * @returns Success status (true if disabled successfully)
313
+ */
314
+ disableMouseMoveEvent(): boolean;
315
+
316
+ /**
317
+ * Enable clipboard fallback for text selection
318
+ *
319
+ * Uses Ctrl+C as a last resort to get selected text when other methods fail.
320
+ * This might modify clipboard contents.
321
+ * Can be called before start().
322
+ *
323
+ * @returns Success status (true if enabled successfully)
324
+ */
325
+ enableClipboard(): boolean;
326
+
327
+ /**
328
+ * Disable clipboard fallback for text selection
329
+ *
330
+ * Will not use Ctrl+C to get selected text.
331
+ * This preserves clipboard contents.
332
+ * Can be called before start().
333
+ *
334
+ * @returns Success status (true if disabled successfully)
335
+ */
336
+ disableClipboard(): boolean;
337
+
338
+ /**
339
+ * Set clipboard mode and program list for text selection
340
+ *
341
+ * Configures how the clipboard fallback mechanism works for different programs.
342
+ * Can be called before start().
343
+ * Mode can be:
344
+ * - DEFAULT: Use clipboard for all programs
345
+ * - INCLUDE_LIST: Only use clipboard for programs in the list
346
+ * - EXCLUDE_LIST: Use clipboard for all programs except those in the list
347
+ *
348
+ * @param {number} mode - Clipboard mode (SelectionHook.ClipboardMode)
349
+ * @param {string[]} programList - Array of program names to include/exclude
350
+ * @returns {boolean} Success status
351
+ */
352
+ setClipboardMode(
353
+ mode: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode],
354
+ programList?: string[]
355
+ ): boolean;
356
+
357
+ /**
358
+ * Set global filter mode for text selection
359
+ *
360
+ * Configures how the global filter mechanism works for different programs.
361
+ * Can be called before start().
362
+ * Mode can be:
363
+ * - DEFAULT: disable global filter
364
+ * - INCLUDE_LIST: Only use global filter for programs in the list
365
+ * - EXCLUDE_LIST: Use global filter for all programs except those in the list
366
+ *
367
+ * @param {number} mode - Filter mode (SelectionHook.FilterMode)
368
+ * @param {string[]} programList - Array of program names to include/exclude
369
+ * @returns {boolean} Success status
370
+ */
371
+ setGlobalFilterMode(
372
+ mode: (typeof SelectionHook.FilterMode)[keyof typeof SelectionHook.FilterMode],
373
+ programList?: string[]
374
+ ): boolean;
375
+
376
+ /**
377
+ * Set fine-tuned list for specific behaviors
378
+ *
379
+ * Configures fine-tuned lists for specific application behaviors.
380
+ * Can be called before start().
381
+ * List types:
382
+ * - EXCLUDE_CLIPBOARD_CURSOR_DETECT: Exclude cursor detection for clipboard operations
383
+ * - INCLUDE_CLIPBOARD_DELAY_READ: Include delay when reading clipboard content
384
+ *
385
+ * @param {number} listType - Fine-tuned list type (SelectionHook.FineTunedListType)
386
+ * @param {string[]} programList - Array of program names for the fine-tuned list
387
+ * @returns {boolean} Success status
388
+ */
389
+ setFineTunedList(
390
+ listType: (typeof SelectionHook.FineTunedListType)[keyof typeof SelectionHook.FineTunedListType],
391
+ programList?: string[]
392
+ ): boolean;
393
+
394
+ /**
395
+ * Set selection passive mode
396
+ *
397
+ * Can be called before start().
398
+ *
399
+ * @param {boolean} passive - Passive mode
400
+ * @returns {boolean} Success status
401
+ */
402
+ setSelectionPassiveMode(passive: boolean): boolean;
403
+
404
+ /**
405
+ * Write text to clipboard
406
+ *
407
+ * Not supported on Linux. Host applications should use their own clipboard API
408
+ * (e.g., Electron clipboard).
409
+ * Can be called before start() (Windows/macOS only).
410
+ *
411
+ * @param {string} text - Text to write to clipboard
412
+ * @returns {boolean} Success status. Always returns false on Linux.
413
+ */
414
+ writeToClipboard(text: string): boolean;
415
+
416
+ /**
417
+ * Read text from clipboard
418
+ *
419
+ * Not supported on Linux. Host applications should use their own clipboard API
420
+ * (e.g., Electron clipboard).
421
+ * Can be called before start() (Windows/macOS only).
422
+ *
423
+ * @returns {string|null} Text from clipboard or null if empty or error. Always returns null on Linux.
424
+ */
425
+ readFromClipboard(): string | null;
426
+
427
+ /**
428
+ * Check if the process is trusted for accessibility (macOS only)
429
+ *
430
+ * Checks whether the current process has accessibility permissions
431
+ * required for text selection monitoring on macOS.
432
+ * Can be called before start().
433
+ *
434
+ * @returns {boolean} True if the process is trusted for accessibility, false otherwise
435
+ */
436
+ macIsProcessTrusted(): boolean;
437
+
438
+ /**
439
+ * Try to request accessibility permissions (macOS only)
440
+ *
441
+ * This MAY show a dialog to the user if permissions are not granted.
442
+ * Can be called before start().
443
+ *
444
+ * @returns {boolean} The current permission status, not the request result
445
+ */
446
+ macRequestProcessTrust(): boolean;
447
+
448
+ /**
449
+ * Get Linux environment information (Linux only)
450
+ *
451
+ * Returns an object containing display protocol, compositor type,
452
+ * input device access status, and root status. All values are detected
453
+ * once at construction time and cached.
454
+ * Can be called before start().
455
+ *
456
+ * @returns {LinuxEnvInfo | null} Linux environment info or null on non-Linux
457
+ */
458
+ linuxGetEnvInfo(): LinuxEnvInfo | null;
459
+
460
+ /**
461
+ * Release resources
462
+ *
463
+ * Stops monitoring and releases all native resources.
464
+ * Should be called before the application exits to prevent memory leaks.
465
+ */
466
+ cleanup(): void;
467
+
468
+ /**
469
+ * Register event listeners
470
+ *
471
+ * The hook emits various events that can be listened for:
472
+ */
473
+
474
+ /**
475
+ * Emitted when text is selected in any application
476
+ * @param event The event name
477
+ * @param listener Callback function receiving selection data
478
+ */
479
+ on(event: "text-selection", listener: (data: TextSelectionData) => void): this;
480
+
481
+ on(event: "mouse-up", listener: (data: MouseEventData) => void): this;
482
+ on(event: "mouse-down", listener: (data: MouseEventData) => void): this;
483
+ on(event: "mouse-move", listener: (data: MouseEventData) => void): this;
484
+ on(event: "mouse-wheel", listener: (data: MouseWheelEventData) => void): this;
485
+
486
+ on(event: "key-down", listener: (data: KeyboardEventData) => void): this;
487
+ on(event: "key-up", listener: (data: KeyboardEventData) => void): this;
488
+
489
+ on(event: "status", listener: (status: string) => void): this;
490
+ on(event: "error", listener: (error: Error) => void): this;
491
+
492
+ // Same events available with once() for one-time listeners
493
+ once(event: "text-selection", listener: (data: TextSelectionData) => void): this;
494
+ once(event: "mouse-up", listener: (data: MouseEventData) => void): this;
495
+ once(event: "mouse-down", listener: (data: MouseEventData) => void): this;
496
+ once(event: "mouse-move", listener: (data: MouseEventData) => void): this;
497
+ once(event: "mouse-wheel", listener: (data: MouseWheelEventData) => void): this;
498
+ once(event: "key-down", listener: (data: KeyboardEventData) => void): this;
499
+ once(event: "key-up", listener: (data: KeyboardEventData) => void): this;
500
+ once(event: "status", listener: (status: string) => void): this;
501
+ once(event: "error", listener: (error: Error) => void): this;
502
+ }
503
+
504
+ /**
505
+ * Instance type for the SelectionHook class
506
+ */
507
+ export type SelectionHookInstance = InstanceType<typeof SelectionHook>;
508
+
509
+ // LinuxEnvInfo is already exported via its interface declaration above
510
+
511
+ /**
512
+ * Constructor type for the SelectionHook class
513
+ */
514
+ export type SelectionHookConstructor = typeof SelectionHook;
515
+
516
+ // Export the SelectionHook class
517
+ export default SelectionHook;