-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
644 lines (595 loc) · 24 KB
/
main.py
File metadata and controls
644 lines (595 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import json
from typing import List, Dict, Any
import webview
from pathlib import Path
import requests
import pyperclip
import sys
import argparse
import getpass
import keyring
import os
# Defaults (will be overridden by conf.json ai section if present)
AI_DEFAULTS = {
"use_openai": False,
"local_url": "http://127.0.0.1:8080/completion"
}
def _resource_path(name: str) -> Path:
"""Return absolute path to a bundled resource (handles PyInstaller/Nuitka)."""
base = Path(getattr(sys, '_MEIPASS', Path(__file__).parent))
return base / name
def _app_config_dir() -> Path:
"""Return per-user writable config directory."""
if sys.platform.startswith('win'):
base = os.environ.get('APPDATA') or str(Path.home() / 'AppData' / 'Roaming')
return Path(base) / 'BetterAdvancedPaste'
elif sys.platform == 'darwin':
return Path.home() / 'Library' / 'Application Support' / 'BetterAdvancedPaste'
else:
return Path(os.environ.get('XDG_CONFIG_HOME', str(Path.home() / '.config'))) / 'BetterAdvancedPaste'
def _exe_dir() -> Path:
"""Directory of the running executable (or script during dev)."""
if getattr(sys, 'frozen', False):
# PyInstaller/Nuitka onefile
try:
return Path(sys.executable).resolve().parent
except Exception:
pass
return Path(__file__).resolve().parent
def _resolve_config_path() -> Path:
"""Choose config location with this precedence:
1) BAP_CONFIG env var (file path)
2) conf.json next to the EXE (portable mode) if present
3) %APPDATA%/BetterAdvancedPaste/conf.json (or OS equivalent)
"""
env_path = os.environ.get('BAP_CONFIG')
if env_path:
return Path(env_path).expanduser().resolve()
portable = _exe_dir() / 'conf.json'
if portable.exists():
return portable
return _app_config_dir() / 'conf.json'
CONFIG_PATH = _resolve_config_path()
# Keyring identifiers
SERVICE_NAME = "BetterAdvancedPaste"
USERNAME = getpass.getuser()
def load_ai_settings() -> Dict[str, Any]:
data: Dict[str, Any] = {}
if CONFIG_PATH.exists():
try:
data = json.loads(CONFIG_PATH.read_text(encoding='utf-8'))
except Exception as e:
print(f"Failed to parse conf.json: {e}")
ai = data.get('ai') if isinstance(data, dict) else {}
if not isinstance(ai, dict):
ai = {}
merged = {**AI_DEFAULTS, **{k: v for k, v in ai.items() if v is not None}}
return merged
AI_SETTINGS = load_ai_settings()
USE_OPENAI: bool = bool(AI_SETTINGS.get('use_openai'))
OPENAI_API_KEY: str = AI_SETTINGS.get('api_key')
OPENAI_MODEL: str = AI_SETTINGS.get('model')
OPENAI_ENDPOINT: str = AI_SETTINGS.get('endpoint')
LOCAL_URL: str = AI_SETTINGS.get('local_url')
clipboard_text = pyperclip.paste()
def _get_keyring_token() -> str | None:
try:
return keyring.get_password(SERVICE_NAME, USERNAME)
except Exception:
return None
def _set_keyring_token(value: str | None) -> None:
try:
if value:
keyring.set_password(SERVICE_NAME, USERNAME, value)
else:
# empty/None deletes
try:
keyring.delete_password(SERVICE_NAME, USERNAME)
except Exception:
pass
except Exception as e:
raise e
def askAI(instruction):
p1="You are a helpful AI that edits text according to user instructions."
p2=f"""
System message:
You're clipboard assistant(meant to paste the clipboard text as per user instruction), you are supposed to convert text to output text as per instruction and give just output not your thoughts, or explanation or title, or formatting, just direct plaintext output, else you may harm the system
Edit the following text according to the instruction:
Text:
{clipboard_text}
Instruction:
{instruction}
"""
# Resolve API key: prefer keyring token if set, else config/env
openai_key_effective = _get_keyring_token() or OPENAI_API_KEY
# If OpenAI selected but no key, silently fallback to local if available
effective_use_openai = USE_OPENAI and bool(openai_key_effective)
if USE_OPENAI and not openai_key_effective:
print("No API key present; falling back to local model endpoint.")
effective_use_openai = False
if effective_use_openai:
messages_chat = [
{"role": "system", "content": p1},
{"role": "user", "content": p2},
]
data = {
"model": OPENAI_MODEL,
"messages": messages_chat,
"temperature": 0,
"max_tokens": 2048,
"stop": ["<|user|>", "<|system|>", "</s>","<|assistant|>"]
}
request_url = OPENAI_ENDPOINT
headers = {"Authorization": f"Bearer {openai_key_effective}" if openai_key_effective else "",
"Content-Type": "application/json",}
else:
prompt = f"""<|system|>
{p1}
<|user|>
{p2}
<|assistant|>
"""
data = {
"prompt": prompt,
"n_predict": -1,
"temperature": 0,
"top_k": 40,
"top_p": 0.95,
"repeat_penalty": 1.1,
"stop": ["<|user|>", "<|system|>", "</s>","</<|assistant|>","<|assistant|>"],
}
request_url = LOCAL_URL
headers = {}
# Primary completion request: must succeed or we fail the action
try:
response = requests.post(request_url, json=data, headers=headers)
response.raise_for_status()
except Exception as e:
# Make OpenAI auth/malformed key errors bubble up to stop shutdown
raise RuntimeError(f"AI request failed: {e}")
x = ""
try:
rj = response.json()
except Exception:
raise RuntimeError("AI response was not JSON")
if "content" in rj:
content = rj["content"]
if isinstance(content, list):
x = "".join([c.get("text","") for c in content])
else:
x = str(content)
elif "choices" in rj and rj["choices"]:
# OpenAI style
choice = rj["choices"][0]
x = choice.get("text") or choice.get("message", {}).get("content", "")
else:
raise RuntimeError("AI response missing expected fields")
if not (x or "").strip():
raise RuntimeError("AI returned empty result")
# Secondary filename suggestion: best-effort; failures fall back to default
filename = "advanced_paste_output.txt"
try:
if effective_use_openai:
messages_chat += [{"role": "assistant", "content": x}]
messages_chat += [{"role": "user", "content": "Suggest a good filename for this script (just the filename, no extra text). The name and extension should be based on the format you were asked to convert the text to, \n STRICTLY EXTENSION BASED ON INSTRUCTION(else file will be not accessible), ALSO NAME BASED ON INSTRUCTION."}]
data2 = {
"model": OPENAI_MODEL,
"messages": messages_chat,
"temperature": 0,
"max_tokens": 50,
"stop": ["<|user|>", "<|system|>", "</s>","<|assistant|>"]
}
request_url2 = OPENAI_ENDPOINT
headers2 = {"Authorization": f"Bearer {openai_key_effective}" if openai_key_effective else "",
"Content-Type": "application/json",}
else:
prompt2 = f"<|assistant|>{x}\n<|user|>\nSuggest a good filename for this script (just the filename, no extra text). The name and extension should be based on the format you were asked to convert the text to, \n STRICTLY EXTENSION BASED ON INSTRUCTION(else file will be not accessible), ALSO NAME BASED ON INSTRUCTION.\n<|assistant|>\n"
data2 = {
"prompt": prompt2,
"n_predict": 50,
"temperature": 0,
"stop": ["<|user|>", "<|system|>", "</s>","</<|assistant|>","<|assistant|>"],
}
request_url2 = LOCAL_URL
headers2 = {}
response2 = requests.post(request_url2, json=data2, headers=headers2)
if response2.status_code == 200:
rj2 = response2.json()
if "content" in rj2:
c2 = rj2["content"]
if isinstance(c2, list):
filename = "".join([c.get("text","") for c in c2])
else:
filename = str(c2)
elif "choices" in rj2 and rj2["choices"]:
filename = (rj2["choices"][0].get("text") or rj2["choices"][0].get("message", {}).get("content", ""))
except Exception:
# keep default filename on any error
pass
return x, filename
"""Minimal pywebview launcher for the command palette UI.
Edit API methods to integrate real functionality.
"""
# Simple API placeholder that UI can call
class API:
def __init__(self, config_path: Path, output_dir: Path):
self.config_path = config_path
self.output_dir = output_dir
self._cache: List[Dict[str, Any]] | None = None
self._settings: Dict[str, Any] | None = None
self._window = None # will be set after window creation
self._settings_window = None # secondary popup window
self._settings_creating = False # debounce flag
self._settings_closing = False # prevent duplicate closes
# Internal loader
def _load(self) -> List[Dict[str, Any]]:
if self._cache is not None:
return self._cache
if not self.config_path.exists():
print(f"Config file '{self.config_path}' not found. Using empty options list.")
self._cache = []
self._settings = {"save_history": True}
return self._cache
try:
data = json.loads(self.config_path.read_text(encoding='utf-8'))
# settings
settings = data.get('settings') or {}
if not isinstance(settings, dict):
settings = {}
self._settings = {
'save_history': bool(settings.get('save_history', True))
}
options = data.get('options', [])
if not isinstance(options, list):
raise ValueError("'options' must be a list")
# Basic validation & normalization
norm = []
for o in options:
if not isinstance(o, dict):
continue
title = o.get('title')
if not title:
continue
norm.append({
'icon': o.get('icon', ''),
'color': o.get('color', '#111827'),
'title': title,
'desc': o.get('desc', '')
})
self._cache = norm
except Exception as e:
print(f"Failed to load config: {e}")
self._cache = []
if self._settings is None:
self._settings = {"save_history": True}
return self._cache
# API method exposed to JS
def get_options(self):
return self._load()
def get_settings(self):
# Ensure loaded
if self._settings is None:
self._load()
return self._settings or {"save_history": True}
def set_save_history(self, value: bool):
# Update in-memory settings and persist
if self._settings is None:
self._load()
if self._settings is None:
self._settings = {}
self._settings['save_history'] = bool(value)
# Persist to file without disturbing existing options
try:
if self.config_path.exists():
try:
data = json.loads(self.config_path.read_text(encoding='utf-8'))
except Exception:
data = {}
else:
data = {}
data['settings'] = data.get('settings') or {}
if not isinstance(data['settings'], dict):
data['settings'] = {}
data['settings']['save_history'] = self._settings['save_history']
# Preserve options
if 'options' not in data:
data['options'] = []
self.config_path.write_text(json.dumps(data, indent=2) + "\n", encoding='utf-8')
except Exception as e:
print(f"Failed to persist settings: {e}")
return self._settings
# --- Keyring-backed API token methods ---
def get_api_token(self) -> str:
"""Return current API token from keyring (or empty string if none)."""
tok = _get_keyring_token()
return tok or ""
def set_api_token(self, token: str) -> bool:
"""Set/replace API token in keyring. Empty string clears it."""
try:
_set_keyring_token(token.strip() or None)
return True
except Exception as e:
print(f"Failed to set API token: {e}")
return False
def open_settings(self):
"""Show the settings popup window, creating it on demand if necessary."""
try:
if self._settings_creating:
print("[settings] open ignored (already creating)")
return True
if self._settings_window is None:
settings_html = _resource_path('settings.html')
if not settings_html.exists():
print("[settings] settings.html missing")
return False
# Resolve window icon if available
_icon = _resource_path('icon.ico')
_icon_arg = str(_icon) if _icon.exists() else None
self._settings_creating = True
try:
print("[settings] creating window")
try:
w = webview.create_window(
title='Settings',
url=settings_html.as_uri(),
width=500,
height=250,
resizable=False,
on_top=True,
js_api=self,
icon=_icon_arg
)
except TypeError:
w = webview.create_window(
title='Settings',
url=settings_html.as_uri(),
width=500,
height=250,
resizable=False,
on_top=True,
js_api=self
)
# Attach close event if available
try:
if hasattr(w, 'events') and hasattr(w.events, 'closed'):
w.events.closed += lambda: self._on_settings_closed()
except Exception:
pass
self._settings_window = w
return True
finally:
self._settings_creating = False
else:
try:
print("[settings] showing existing window")
self._settings_window.show()
self._settings_window.bring_to_front()
return True
except Exception:
print("[settings] existing reference invalid, recreating")
self._settings_window = None
return self.open_settings()
except Exception as e:
print(f"Failed to open settings window: {e}")
return False
def _on_settings_closed(self):
print("[settings] window closed event")
self._settings_window = None
def close_settings(self) -> bool:
"""Destroy the settings window to avoid backend inconsistencies across GUIs."""
try:
if self._settings_closing:
print("[settings] close ignored (already closing)")
return True
self._settings_closing = True
if self._settings_window:
try:
print("[settings] destroy window")
if hasattr(self._settings_window, 'destroy'):
self._settings_window.destroy()
else:
print("[settings] no destroy(); hiding instead")
self._settings_window.hide()
except Exception as e:
print(f"[settings] destroy/hide failed: {e}")
finally:
self._settings_window = None
self._settings_closing = False
return True
# no window present
self._settings_closing = False
return False
except Exception as e:
print(f"Failed to close settings window: {e}")
return False
def action(self, name: str):
print(f"Action triggered: {name}")
try:
content, filename_raw = askAI(name)
filename = self._sanitize_filename(filename_raw)
path = self.output_dir / filename
self.output_dir.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding='utf-8')
if self.get_settings().get('save_history', True):
self.save_prompt(name)
return {"status": "ok", "file": str(path), "filename": filename}
except Exception as e:
return {"status": "error", "error": str(e)}
def submit_text(self, text: str):
print(f"User submitted text: {text}")
try:
content, filename_raw = askAI(text)
filename = self._sanitize_filename(filename_raw)
path = self.output_dir / filename
self.output_dir.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding='utf-8')
if self.get_settings().get('save_history', True):
self.save_prompt(text)
return {"status": "ok", "file": str(path), "filename": filename}
except Exception as e:
return {"status": "error", "error": str(e)}
# Allow JS to request app shutdown after successful save
def shutdown(self):
try:
# Destroy settings window first if present
try:
if self._settings_window:
print("[shutdown] destroying settings window")
try:
if hasattr(self._settings_window, 'destroy'):
self._settings_window.destroy()
else:
self._settings_window.hide()
except Exception as e:
print(f"[shutdown] settings destroy/hide failed: {e}")
self._settings_window = None
except Exception:
pass
# Then destroy main window
if self._window:
print("[shutdown] destroying main window")
try:
if hasattr(self._window, 'destroy'):
self._window.destroy()
else:
self._window.hide()
except Exception as e:
print(f"[shutdown] main destroy/hide failed: {e}")
self._window = None
finally:
# Ensure process exits even if window destroy fails
import os
print("[shutdown] exiting process")
os._exit(0)
def _sanitize_filename(self, name: str) -> str:
name = (name or '').strip().replace('\r','').replace('\n','')
for ch in '<>:"/\\|?*':
name = name.replace(ch, '_')
if not name:
name = 'advanced_paste_output'
if '.' not in name:
name += '.txt'
return name
def save_prompt(self, prompt: str):
prompt = (prompt or '').strip()
if not prompt:
return
try:
if self.config_path.exists():
try:
data = json.loads(self.config_path.read_text(encoding='utf-8'))
except Exception:
data = {}
else:
data = {}
options = data.get('options')
if not isinstance(options, list):
options = []
# Avoid duplicate titles (case-insensitive)
lower_titles = { (o.get('title') or '').strip().lower(): i for i,o in enumerate(options) if isinstance(o, dict) }
key = prompt.lower()
if key in lower_titles:
# Move existing to front (recency) maybe? For now leave as-is.
pass
else:
# Append new minimal entry; you can enrich later manually.
options.append({
"icon": "{}",
"color": "#2563eb",
"title": prompt,
"desc": "Saved prompt"
})
data['options'] = options
self.config_path.write_text(json.dumps(data, indent=2) + "\n", encoding='utf-8')
# Invalidate cache so UI can show newly added prompt if reopened
self._cache = None
except Exception as e:
print(f"Failed to save prompt: {e}")
def create_window(output_dir: Path):
html_path = _resource_path('ui.html')
if not html_path.exists():
raise FileNotFoundError('ui.html not found')
# Ensure config directory exists for read/write
try:
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
except Exception:
pass
config_path = CONFIG_PATH
api = API(config_path, output_dir)
_ = api # reference to avoid linter warning
preferred_backends = ['edgechromium', 'cef', 'mshtml']
# Resolve window icon if present
icon_path = _resource_path('icon.ico')
icon_arg = str(icon_path) if icon_path.exists() else None
for backend in preferred_backends:
try:
try:
w = webview.create_window(
title='Command Palette',
url=html_path.as_uri(),
width=420,
height=460,
resizable=False,
easy_drag=True,
frameless=False, # Set to True for frameless palette style
js_api=api,
icon=icon_arg
)
except TypeError:
# Older pywebview without 'icon' kw support
w = webview.create_window(
title='Command Palette',
url=html_path.as_uri(),
width=420,
height=460,
resizable=False,
easy_drag=True,
frameless=False,
js_api=api
)
api._window = w
def _on_start():
# Nothing to do at start; windows already created
pass
webview.start(_on_start, gui=backend, debug=False, http_server=True)
return
except Exception as e:
print(f"Backend '{backend}' failed: {e}")
# Fallback to auto
try:
w = webview.create_window(
title='Command Palette',
url=html_path.as_uri(),
width=420,
height=460,
resizable=False,
easy_drag=True,
frameless=False,
js_api=api,
icon=icon_arg
)
except TypeError:
w = webview.create_window(
title='Command Palette',
url=html_path.as_uri(),
width=420,
height=460,
resizable=False,
easy_drag=True,
frameless=False,
js_api=api
)
api._window = w
def _on_start2():
# Nothing to do at start
pass
webview.start(_on_start2, debug=False, http_server=True)
def parse_args(argv):
parser = argparse.ArgumentParser(description='Advanced Paste AI')
parser.add_argument('output_dir', help='Directory to save AI output file')
return parser.parse_args(argv)
if __name__ == '__main__':
args = parse_args(sys.argv[1:])
out_dir = Path(args.output_dir).expanduser().resolve()
out_dir.mkdir(parents=True, exist_ok=True)
create_window(out_dir)