assistant 2.0.2b1__tar.gz → 2.0.2b2__tar.gz

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 (79) hide show
  1. {assistant-2.0.2b1/assistant.egg-info → assistant-2.0.2b2}/PKG-INFO +1 -1
  2. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/__init__.py +4 -1
  3. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/agent.py +6 -1
  4. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/builtin_cmds.py +28 -23
  5. assistant-2.0.2b2/assistant/listen/__init__.py +22 -0
  6. assistant-2.0.2b2/assistant/listen/interface.py +68 -0
  7. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/listen/main.py +12 -5
  8. assistant-2.0.2b2/assistant/ptk_shell/completer.py +301 -0
  9. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/shell.py +44 -51
  10. {assistant-2.0.2b1 → assistant-2.0.2b2/assistant.egg-info}/PKG-INFO +1 -1
  11. {assistant-2.0.2b1 → assistant-2.0.2b2}/xontrib/assistant.py +7 -0
  12. assistant-2.0.2b1/assistant/listen/__init__.py +0 -18
  13. assistant-2.0.2b1/assistant/listen/interface.py +0 -49
  14. assistant-2.0.2b1/assistant/ptk_shell/completer.py +0 -329
  15. {assistant-2.0.2b1 → assistant-2.0.2b2}/LICENSE +0 -0
  16. {assistant-2.0.2b1 → assistant-2.0.2b2}/MANIFEST.in +0 -0
  17. {assistant-2.0.2b1 → assistant-2.0.2b2}/README.md +0 -0
  18. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/__main__.py +0 -0
  19. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/as_client.py +0 -0
  20. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/as_service.py +0 -0
  21. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/codecache.py +0 -0
  22. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/conditions.py +0 -0
  23. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/config.py +0 -0
  24. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/entry_points/__init__.py +0 -0
  25. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/entry_points/run_assistant.py +0 -0
  26. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/execer.py +0 -0
  27. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/history.py +0 -0
  28. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/listen/__main__.py +0 -0
  29. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/main.py +0 -0
  30. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/memory.py +0 -0
  31. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/__init__.py +0 -0
  32. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/clear.py +0 -0
  33. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/exit.py +0 -0
  34. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/http_client.py +0 -0
  35. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/interface.py +0 -0
  36. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/message.py +0 -0
  37. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/__init__.py +0 -0
  38. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/google.py +0 -0
  39. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/llama_cpp.py +0 -0
  40. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/nim.py +0 -0
  41. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/ollama.py +0 -0
  42. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/openai.py +0 -0
  43. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/openrouter.py +0 -0
  44. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/providers/vllm.py +0 -0
  45. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/think.py +0 -0
  46. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/nlp/xonsh.py +0 -0
  47. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/parser.py +0 -0
  48. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/__init__.py +0 -0
  49. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/bindings.py +0 -0
  50. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/dialogs.py +0 -0
  51. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/floats.py +0 -0
  52. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/layout.py +0 -0
  53. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/operators.py +0 -0
  54. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/strformat_utils.py +0 -0
  55. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/styles.py +0 -0
  56. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/ptk_shell/windows.py +0 -0
  57. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/say/__init__.py +0 -0
  58. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/say/interface.py +0 -0
  59. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/setup_wizard.py +0 -0
  60. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/time.py +0 -0
  61. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/tools.py +0 -0
  62. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/tools_worker.py +0 -0
  63. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/utils.py +0 -0
  64. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/version.py +0 -0
  65. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant/vocabulary.py +0 -0
  66. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant.egg-info/SOURCES.txt +0 -0
  67. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant.egg-info/dependency_links.txt +0 -0
  68. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant.egg-info/entry_points.txt +0 -0
  69. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant.egg-info/requires.txt +0 -0
  70. {assistant-2.0.2b1 → assistant-2.0.2b2}/assistant.egg-info/top_level.txt +0 -0
  71. {assistant-2.0.2b1 → assistant-2.0.2b2}/pyproject.toml +0 -0
  72. {assistant-2.0.2b1 → assistant-2.0.2b2}/setup.cfg +0 -0
  73. {assistant-2.0.2b1 → assistant-2.0.2b2}/setup.py +0 -0
  74. {assistant-2.0.2b1 → assistant-2.0.2b2}/tests/test_config.py +0 -0
  75. {assistant-2.0.2b1 → assistant-2.0.2b2}/tests/test_init.py +0 -0
  76. {assistant-2.0.2b1 → assistant-2.0.2b2}/tests/test_tools.py +0 -0
  77. {assistant-2.0.2b1 → assistant-2.0.2b2}/tests/test_utils.py +0 -0
  78. {assistant-2.0.2b1 → assistant-2.0.2b2}/tests/test_xontrib.py +0 -0
  79. {assistant-2.0.2b1 → assistant-2.0.2b2}/xontrib/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: assistant
3
- Version: 2.0.2b1
3
+ Version: 2.0.2b2
4
4
  Summary: Your very own Assistant. Because you deserve it.
5
5
  Home-page: https://gitlab.com/waser-technologies/technologies/assistant
6
6
  Author: Danny Waser
@@ -1,7 +1,10 @@
1
1
  # from xonsh.main import setup
2
2
  import os
3
+ import warnings
4
+ warnings.filterwarnings("ignore", category=DeprecationWarning, module="webrtcvad")
5
+ warnings.filterwarnings("ignore", category=UserWarning, module="webrtcvad")
3
6
 
4
- __version__ = "2.0.2b1"
7
+ __version__ = "2.0.2b2"
5
8
 
6
9
  USERNAME = os.environ.get("USER", "root")
7
10
  HOME = os.environ.get("HOME", f"/home/{USERNAME}" if USERNAME != "root" else "/root")
@@ -368,6 +368,7 @@ class Orchestrator:
368
368
  self._abort_event.clear()
369
369
 
370
370
  # Install signal handler only right before streaming
371
+ # Only works in main thread; ignore in thread workers
371
372
  import signal as _sig
372
373
 
373
374
  def _sigint_handler(signum, frame):
@@ -376,7 +377,11 @@ class Orchestrator:
376
377
  _ag.close_client()
377
378
  raise KeyboardInterrupt("Ctrl+C")
378
379
 
379
- _sig.signal(_sig.SIGINT, _sigint_handler)
380
+ try:
381
+ _sig.signal(_sig.SIGINT, _sigint_handler)
382
+ except ValueError:
383
+ # signal only works in main thread of the main interpreter
384
+ pass
380
385
 
381
386
  self._emit({"type": "status", "text": "Searching memory..."})
382
387
 
@@ -185,7 +185,7 @@ Run the following command:
185
185
  pip install -r stt-listen
186
186
  ```
187
187
  """
188
- _log_md(shell, output)
188
+ shell.log_error(output)
189
189
  return 0
190
190
 
191
191
  if not shell.kernel_interface.listen_interface.is_asr_server_up():
@@ -200,20 +200,16 @@ To start the server, run the following command (outside of Assistant in a separa
200
200
  uvicorn listen.Wav2Vec.as_service:app --port 5063
201
201
  ```
202
202
  """
203
- _log_md(shell, output)
203
+ shell.log_error(output)
204
204
  return 0
205
205
 
206
206
  try:
207
207
  if args[0] == "start":
208
- try:
209
- shell.start_listening()
210
- except (KeyboardInterrupt, EOFError):
211
- shell.stop_listening()
212
- return 0
208
+ shell.start_listening()
213
209
  return 0
214
210
  elif args[0] == "stop":
215
211
  shell.stop_listening()
216
- _log_md(shell, "# Stopped listening")
212
+ _log_md(shell, "Stopped listening.")
217
213
  return 0
218
214
  else:
219
215
  n = int(args[0])
@@ -223,6 +219,7 @@ uvicorn listen.Wav2Vec.as_service:app --port 5063
223
219
  _log_md(shell, "Usage: `listen [OPTIONS]... [INT]...`")
224
220
  return 0
225
221
  else:
222
+ shell.kernel_interface.spinner.start("Preparing to listen...")
226
223
  if not shell.kernel_interface.is_asr:
227
224
  output = """# Module Not Found Error
228
225
  To speak with Assistant, you need to install the listen module.
@@ -248,22 +245,30 @@ uvicorn listen.Wav2Vec.as_service:app --port 5063
248
245
  """
249
246
  _log_md(shell, output)
250
247
  return 0
251
- shell.kernel_interface.spinner.text = "Listening..."
252
- shell.kernel_interface.spinner.start()
253
- query = shell.kernel_interface.listen_interface.listen()
254
- shell.kernel_interface.spinner.stop()
255
- shell.kernel_interface.spinner.info("Stopped listening.")
256
- if query:
257
- from rich.markdown import Markdown
258
-
259
- shell.log_query(Markdown(f"> {query}"))
260
- answer = shell.kernel_interface.session_assistant(query)
261
- if answer:
262
- shell.say(answer)
248
+ # Start spinner before listening
249
+ shell.kernel_interface.spinner.start("Listening...")
250
+ try:
251
+ query = shell.kernel_interface.listen_interface.listen()
252
+ # Stop spinner and show stopped message
253
+ shell.kernel_interface.spinner.stop()
254
+ shell.kernel_interface.spinner.info("Stopped listening.")
255
+
256
+ if query:
257
+ shell.log_query(f"{query}")
258
+ try:
259
+ answer = shell.kernel_interface.assistant(query)
260
+ if answer:
261
+ shell.say(answer)
262
+ # else:
263
+ # shell.log_error("Assistant failed to reply.")
264
+ except Exception as e:
265
+ shell.kernel_interface.spinner.stop()
266
+ shell.log_error(e)
263
267
  else:
264
- _log_md(shell, "Assistant failed to reply.")
265
- else:
266
- _log_md(shell, f"# Nothing heard\nTranscript: '{query}'")
268
+ _log_md(shell, f"# Nothing heard\nTranscript: '{query}'")
269
+ except Exception as e:
270
+ shell.kernel_interface.spinner.stop()
271
+ _log_md(shell, f"# Error\n{str(e)}")
267
272
  return 0
268
273
 
269
274
 
@@ -0,0 +1,22 @@
1
+ import sys
2
+
3
+ import warnings
4
+ with warnings.catch_warnings():
5
+ warnings.filterwarnings("ignore", category=DeprecationWarning, module="webrtcvad")
6
+ warnings.filterwarnings("ignore", category=UserWarning, module="webrtcvad")
7
+ try:
8
+ import listen
9
+ except (ModuleNotFoundError, ImportError) as e:
10
+ # print(e)
11
+ # print("To speak with Assistant, you need to install the listen module.")
12
+ # print("Run the following command:\n```shell\npip install -r stt-listen\n```")
13
+ raise e
14
+ # sys.exit(1)
15
+
16
+ if __name__ == "__main__":
17
+ from assistant.listen.main import main
18
+
19
+ try:
20
+ main()
21
+ except KeyboardInterrupt:
22
+ sys.exit(1)
@@ -0,0 +1,68 @@
1
+ import warnings
2
+ import threading
3
+
4
+ with warnings.catch_warnings():
5
+ warnings.filterwarnings("ignore", category=DeprecationWarning, module="webrtcvad")
6
+ warnings.filterwarnings("ignore", category=UserWarning, module="webrtcvad")
7
+ import requests
8
+
9
+ disclamer = """To speak with Assistant, you need to install the listen module.
10
+ Run the following command:
11
+ ```shell
12
+ pip install -r stt-listen
13
+ ```"""
14
+
15
+ try:
16
+ from listen import mic
17
+ except (ModuleNotFoundError, ImportError) as e:
18
+ e.msg = disclamer
19
+ raise e
20
+
21
+
22
+ class ListenInterface:
23
+ asr_host = "localhost"
24
+ asr_port = "5063"
25
+
26
+ def __init__(self, asr_host=asr_host, asr_port=asr_port, **kwargs):
27
+ self.asr_host = asr_host
28
+ self.asr_port = asr_port
29
+ self._current_mic = None
30
+
31
+ for k, v in kwargs.items():
32
+ setattr(self, k, v)
33
+
34
+ def is_asr_server_up(self):
35
+ try:
36
+ r = requests.get(f"http://{self.asr_host}:{self.asr_port}")
37
+ if r.status_code == 200:
38
+ return True
39
+ else:
40
+ return False
41
+ except (requests.ConnectionError, Exception):
42
+ return False
43
+
44
+ def listen(self):
45
+ """Listen for one utterance and return transcript."""
46
+ try:
47
+ source = mic.Microphone()
48
+ self._current_mic = source
49
+ source.record()
50
+ query = source.transcribe_until_silence()
51
+ source.stop_record()
52
+ try:
53
+ if hasattr(source, "vad_audio") and source.vad_audio is not None:
54
+ if hasattr(source.vad_audio, "pa"):
55
+ source.vad_audio.pa.terminate()
56
+ source.vad_audio.destroy()
57
+ except Exception:
58
+ pass
59
+ return query
60
+ except (KeyboardInterrupt, EOFError) as e:
61
+ raise e
62
+ finally:
63
+ self._current_mic = None
64
+
65
+ def stop(self):
66
+ """Stop the current listening operation."""
67
+ if self._current_mic is not None:
68
+ self._current_mic.stop_listening()
@@ -1,10 +1,17 @@
1
- import os, sys
1
+ import os
2
+ import sys
2
3
  import json
3
4
 
4
- # import websockets
5
- import asyncio
6
- import signal
7
- from time import sleep
5
+ import warnings
6
+ # Suppress pkg_resources deprecation warning from webrtcvad
7
+ with warnings.catch_warnings():
8
+ warnings.filterwarnings("ignore", category=DeprecationWarning, module="webrtcvad")
9
+ warnings.filterwarnings("ignore", category=UserWarning, module="webrtcvad")
10
+
11
+ # import websockets
12
+ import asyncio
13
+ import signal
14
+ from time import sleep
8
15
 
9
16
  # from websockets.exceptions import ConnectionClosedOK
10
17
  from rich.console import Console
@@ -0,0 +1,301 @@
1
+ """Prompt Toolkit completer for Assistant with intelligent debouncing."""
2
+
3
+ import os
4
+ import threading
5
+ import time
6
+ from pathlib import Path
7
+ from typing import Dict, List
8
+ from prompt_toolkit.completion import Completion
9
+
10
+ class AssistantCompleter:
11
+ """
12
+ Completer wrapper that avoids spamming the provider while keeping UI responsive.
13
+
14
+ Strategy:
15
+ 1. Use PTK's built-in `complete_in_thread=True` for non-blocking behavior
16
+ 2. Implement local debouncing at the completer level to coalesce rapid requests
17
+ 3. Cache results with TTL to minimize API calls
18
+ """
19
+
20
+ def __init__(self):
21
+ self._cache: Dict[str, List[str]] = {}
22
+ self._cache_ttl = 30 # seconds
23
+
24
+ # Debouncing mechanism - prevents spamming provider with rapid calls
25
+ self._debounce_lock = threading.Lock()
26
+ self._pending_request_key: str = None
27
+ self._pending_result: List[str] = []
28
+
29
+ self._agent = None
30
+ self._completion_enabled: bool = True
31
+ self._max_token: int = 1
32
+ self._wait_timeout: float = 0.3 # Reduced timeout for better UX
33
+ self._request_timeout: tuple = (10.0, 300.0)
34
+
35
+ def _init_agent(self):
36
+ if self._agent is None and self._completion_enabled:
37
+ try:
38
+ from assistant.config import get_config
39
+
40
+ cfg = get_config()
41
+ self._completion_enabled = cfg.completion_enabled
42
+ self._wait_timeout = cfg.completion_wait_timeout
43
+ self._request_timeout = cfg.completion_request_timeout
44
+
45
+ agent_cfg = cfg.get_agent_config("completer")
46
+ model_id = agent_cfg.get("model", "qwen")
47
+ model_cfg = cfg.get_model_config(model_id)
48
+ self._max_token = model_cfg.get("max_token", 1)
49
+
50
+ from assistant.agent import Agent
51
+
52
+ self._agent = Agent("completer", cfg)
53
+ except Exception as e:
54
+ print(f"[DEBUG] _init_agent FAILED: {e}")
55
+
56
+ @property
57
+ def agent(self):
58
+ if not self._completion_enabled:
59
+ return None
60
+ self._init_agent()
61
+ return self._agent
62
+
63
+ def _get_cache_key(self, text: str) -> str:
64
+ """Get cache key from last 30 chars of text."""
65
+ return text[-30:] if len(text) > 30 else text
66
+
67
+ def _is_cache_valid(self, key: str) -> bool:
68
+ """Check if cached result is still valid (not expired)."""
69
+ if key not in self._cache:
70
+ return False
71
+ timestamp = self._cache_timestamp.get(key, 0)
72
+ return (time.time() - timestamp) < self._cache_ttl
73
+
74
+ def _get_history_completions(self, word: str) -> List[str]:
75
+ """Get completions from command history."""
76
+ history_path = Path.home() / ".assistant_history"
77
+ completions: List[str] = []
78
+ seen = set()
79
+
80
+ if history_path.exists():
81
+ try:
82
+ with open(history_path, "r", encoding="utf-8") as f:
83
+ for line in f:
84
+ line = line.strip()
85
+ if not line:
86
+ continue
87
+ parts = line.split(maxsplit=1)
88
+ if len(parts) >= 1:
89
+ cmd = parts[0]
90
+ if cmd.startswith(word) and cmd not in seen:
91
+ seen.add(cmd)
92
+ completions.append(cmd)
93
+ except Exception:
94
+ pass
95
+
96
+ return completions
97
+
98
+ def _get_local_completions(self, text: str) -> List[str]:
99
+ """Get local completions from current directory and history."""
100
+ word = text.split()[-1] if text.split() else ""
101
+ if not word:
102
+ return []
103
+
104
+ completions = self._get_history_completions(word)
105
+
106
+ try:
107
+ cwd = os.getcwd()
108
+ for entry in os.listdir(cwd):
109
+ if entry.startswith(word):
110
+ completions.append(entry)
111
+ except Exception:
112
+ pass
113
+
114
+ # Deduplicate while preserving order
115
+ seen = set()
116
+ unique = []
117
+ for c in completions:
118
+ if c not in seen:
119
+ seen.add(c)
120
+ unique.append(c)
121
+
122
+ return unique
123
+
124
+ def _should_debounce(self, text: str) -> bool:
125
+ """Check if this request should be debounced."""
126
+ with self._debounce_lock:
127
+ last_time = self._last_request_time.get(text, 0)
128
+ now = time.time()
129
+
130
+ # Debounce window - coalesce rapid requests (200ms is fast but effective)
131
+ if now - last_time < 0.2:
132
+ return True
133
+
134
+ # Update timestamp for this text
135
+ self._last_request_time[text] = now
136
+
137
+ return False
138
+
139
+ def _get_completions(self, text: str) -> List[str]:
140
+ """Get completions with proper debouncing to avoid provider spam."""
141
+
142
+ if not self._completion_enabled or not self.agent:
143
+ return self._get_local_completions(text)
144
+
145
+ key = self._get_cache_key(text)
146
+
147
+ # Check cache first - fastest path
148
+ if self._is_cache_valid(key):
149
+ return list(self._cache.get(key, []))
150
+
151
+ # Debounce rapid requests to avoid spamming provider
152
+ if self._should_debounce(text):
153
+ time.sleep(0.1) # Small delay before re-checking cache/local completions
154
+ # Re-check after debounce window - might have local matches now
155
+ return self._get_local_completions(text)
156
+
157
+ # No cached result and no debounce - fetch from provider
158
+ with self._debounce_lock:
159
+ if self._pending_request_key == key:
160
+ # Request already pending, wait for it to complete
161
+ while not self._pending_result: # Wait until we have a result
162
+ time.sleep(0.1)
163
+
164
+ return list(self._pending_result)
165
+
166
+ # Mark this request as pending and start worker thread
167
+ self._pending_request_key = key
168
+ self._pending_result = []
169
+
170
+ def worker():
171
+ try:
172
+ result = self._fetch_completions(text)
173
+
174
+ with self._debounce_lock:
175
+ self._pending_result = result
176
+
177
+ # Cache the results for future use
178
+ if not self._completion_enabled or not self.agent:
179
+ pass # Don't cache if settings changed during request
180
+ else:
181
+ self._cache[key] = result
182
+ self._cache_timestamp[key] = time.time()
183
+ except Exception as e:
184
+ import traceback
185
+ print(f"[DEBUG] _fetch_completions error: {e}")
186
+
187
+ thread = threading.Thread(target=worker, daemon=True)
188
+ thread.start()
189
+
190
+ return list(self._pending_result) if self._pending_request_key == key else []
191
+
192
+ def _fetch_completions(self, text: str) -> List[str]:
193
+ """Fetch completions from the LLM provider."""
194
+ if not self.agent:
195
+ return []
196
+
197
+ try:
198
+ import json as _json
199
+ import requests as _requests
200
+
201
+ base_url = self.agent.base_url.rstrip("/")
202
+ # Use the /completions endpoint for text completion (better for single-word prediction)
203
+ if not base_url.endswith("/completions"):
204
+ completions_url = f"{base_url}/completions"
205
+
206
+ results = []
207
+ seen = set()
208
+
209
+ # Strategy 1: Ask model to output multiple possible next words separated by newlines
210
+ completion_data = {
211
+ "model": self.agent.model_name,
212
+ "prompt": text,
213
+ "temperature": 0.1,
214
+ "max_tokens": 3,
215
+ "n": 3,
216
+ "stop": ["\n", "\n\n", "\n\n\n"],
217
+ "stream": False,
218
+ }
219
+
220
+ resp = _requests.post(
221
+ completions_url,
222
+ headers={
223
+ "Content-Type": "application/json",
224
+ "Authorization": f"Bearer {self.agent.api_key}",
225
+ },
226
+ data=_json.dumps(completion_data),
227
+ timeout=self._request_timeout,
228
+ )
229
+
230
+ if resp.status_code == 200:
231
+ raw_response = resp.json()
232
+ for choice in raw_response.get("choices", []):
233
+ full_text = (choice.get("text") or "").strip()
234
+ # Split by newlines and take each line as a potential completion
235
+ lines = [l.strip() for l in full_text.split("\n") if l.strip()]
236
+ for word in lines:
237
+ if word and word.lower() not in seen:
238
+ results.append(word)
239
+ seen.add(word.lower())
240
+
241
+ return results
242
+ except Exception as e:
243
+ print(f"[DEBUG] _fetch_completions failed: {e}")
244
+ return []
245
+
246
+ def get_completions(self, document, complete_event):
247
+ """
248
+ Prompt Toolkit completion interface.
249
+
250
+ This is called by PTK's prompt loop and runs in a thread-safe manner.
251
+ The actual heavy lifting (network calls) happens asynchronously via worker threads.
252
+ """
253
+ try:
254
+ text_before_cursor = document.text_before_cursor
255
+
256
+ if not text_before_cursor or not text_before_cursor.strip():
257
+ return
258
+
259
+ # Get completions - this may block briefly but with debouncing it's usually fast
260
+ preds = self._get_completions(text_before_cursor)
261
+
262
+ word_before_cursor = document.get_word_before_cursor() or ""
263
+
264
+ seen = set()
265
+ for pred in preds:
266
+ if not pred:
267
+ continue
268
+
269
+ # Get first word only (avoid multi-word completions confusing the UI)
270
+ pred_clean = pred.split()[0] if pred.split() else pred
271
+
272
+ if not pred_clean or pred_clean in seen:
273
+ continue
274
+ seen.add(pred_clean)
275
+
276
+ combined_word = word_before_cursor + " " + pred_clean if word_before_cursor else pred_clean
277
+ meta = "✨" if self._completion_enabled and self.agent else "📋"
278
+
279
+ yield from (
280
+ Completion(
281
+ combined_word,
282
+ start_position=-len(word_before_cursor),
283
+ display_meta=meta
284
+ )
285
+ for _ in [None] # Generator wrapper to ensure we yield
286
+ )
287
+ except KeyboardInterrupt:
288
+ raise
289
+ except EOFError:
290
+ raise
291
+ except Exception:
292
+ pass
293
+
294
+
295
+ # Backward compatibility alias
296
+ Completer = AssistantCompleter
297
+
298
+
299
+ class Completion(Completion):
300
+ """Backward compatibility - use prompt_toolkit.completion.Completion directly."""
301
+ pass