forked from ShamanicArts/Flow.Launcher.Plugin.AI-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
299 lines (251 loc) · 9.7 KB
/
main.py
File metadata and controls
299 lines (251 loc) · 9.7 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
import sys
import os
from pathlib import Path
# Add lib directory to path for external modules
plugindir = Path.absolute(Path(__file__).parent)
paths = (".", "lib", "plugin")
sys.path = [str(plugindir / p) for p in paths] + sys.path
import re
import tempfile
import subprocess
import requests
from pyflowlauncher import Plugin, Result, send_results
from pyflowlauncher.result import ResultResponse
from pyflowlauncher.settings import settings
DEFAULT_LLM_PROVIDER_URL = "https://openrouter.ai/api/v1/chat/completions"
DEFAULT_MODEL = "mistralai/mistral-small-3.2-24b-instruct:free"
DEFAULT_DELIMITER = "||"
DEFAULT_SYSTEM_PROMPT = "You are an assistant providing concise, factually accurate responses with no formatting. Reply only with a plain-text answer: no markdown, lists, explanations, or extra text. Prioritize brevity and precise truth above all else. If a user asks a question that can be answered in the terminal, only provide commands."
DEFAULT_TEXT_EDITOR = r"notepad.exe"
# Flag to force settings API key test - set to True when testing
FORCE_SETTINGS_API_KEY = False
# Cache for settings to handle inconsistent settings() behavior
_settings_cache = {}
def get_env_api_key():
"""
Get the API key from environment variable, respecting the test flag.
When FORCE_SETTINGS_API_KEY is True, this will return None as if no env var exists.
"""
if FORCE_SETTINGS_API_KEY:
return None
return os.environ.get("FLOWLLM_API_KEY")
def get_settings(key=None, default=None):
"""
Get settings with environment variable priority for API key.
"""
global _settings_cache
# Try to get current settings
current_settings = settings()
# If settings is available, update cache
if current_settings is not None:
_settings_cache.update(current_settings)
# Special case for API key - prioritize environment variable
if key == "api_key":
env_api_key = get_env_api_key()
if env_api_key:
return env_api_key
# If a specific key is requested
if key is not None:
value = _settings_cache.get(key, default)
if not value:
value = default
return value
# Return all settings
return _settings_cache
# Run at module load to initialize settings
try:
settings_obj = settings()
if settings_obj is not None:
_settings_cache.update(settings_obj)
except Exception:
pass
plugin = Plugin()
@plugin.on_method
def query(query: str) -> ResultResponse:
"""Main entry point for the plugin."""
# Get settings using our cache mechanism
api_provider_url = get_settings("api_url", DEFAULT_LLM_PROVIDER_URL)
api_key = get_settings("api_key", "")
default_model = get_settings("default_model", DEFAULT_MODEL)
delimiter = get_settings("delimiter", DEFAULT_DELIMITER)
system_prompt = get_settings("system_prompt", DEFAULT_SYSTEM_PROMPT)
reasoning = get_settings("reasoning", True)
full_response = get_settings("full_response", True)
text_editor = get_settings("text_editor", DEFAULT_TEXT_EDITOR)
if not query.strip():
return send_results([
Result(
Title="AI Assistant",
SubTitle=f"Type a question followed by {delimiter} to ask {default_model}",
IcoPath="Images/app.png"
)
])
# Execute only if delimiter is present
if delimiter in query:
query = query.split(delimiter, 1)[0].strip()
if not api_key:
return send_results([
Result(
Title="API Key not set",
SubTitle="Set FLOWLLM_API_KEY environment variable",
IcoPath="Images/app.png"
)
])
model_messages = []
if system_prompt.strip():
model_messages.append({"role": "system", "content": system_prompt})
model_messages.append({"role": "user", "content": query})
# Make the API call
try:
response = requests.post(
api_provider_url,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"HTTP-Referer": "https://github.com/xenongee/Flow.Launcher.Plugin.AI-Assistant",
"X-Title": "AI Assistant - Flow Launcher Plugin"
},
json={
"model": default_model,
"messages": model_messages,
"reasoning": {
"enabled": reasoning
}
},
timeout=30
)
if response.status_code == 200:
result = response.json()
answer = result["choices"][0]["message"]["content"]
# Show result with actions
preview = answer[:100] + "..." if len(answer) > 100 else answer
if full_response:
subtitle = answer
else:
subtitle = preview
subtitle = re.sub(r'[\s]+', ' ', subtitle).strip()
return send_results([
Result(
Title=f"AI Response ({default_model})",
SubTitle=subtitle,
IcoPath="Images/app.png",
JsonRPCAction={
"method": "copy_to_clipboard",
"parameters": [answer]
},
ContextData=[answer, text_editor],
Score=100
),
Result(
Title=f"Open in {Path(text_editor).stem}",
SubTitle=f"Open the full response in {Path(text_editor).stem}",
IcoPath="Images/note.png",
JsonRPCAction={
"method": "open_in_notepad",
"parameters": [answer, text_editor]
},
ContextData=[answer, text_editor]
)
])
else:
error_data = response.json().get("error", {})
error_message = error_data.get("message", "Unknown error")
return send_results([
Result(
Title=f"Error: {response.status_code}",
SubTitle=error_message,
IcoPath="Images/app.png"
)
])
except Exception as e:
return send_results([
Result(
Title="Error",
SubTitle=str(e),
IcoPath="Images/app.png"
)
])
# Just preview when typing, no Enter execution
return send_results([
Result(
Title=f"Ask AI: {query}",
SubTitle=f"Add '{delimiter}' to send to {default_model}",
IcoPath="Images/app.png",
JsonRPCAction={
"method": "copy_to_clipboard",
"parameters": [query]
},
ContextData=query
)
])
@plugin.on_method
def copy_to_clipboard(text: str) -> ResultResponse:
"""Copy text to clipboard."""
try:
import pyperclip
pyperclip.copy(text)
return send_results([
Result(
Title="Copied to clipboard",
SubTitle=text[:100] + "..." if len(text) > 100 else text,
IcoPath="Images/copy.png"
)
])
except ImportError:
return send_results([
Result(
Title="Error: Could not copy to clipboard",
SubTitle="The pyperclip module is not installed properly",
IcoPath="Images/app.png"
)
])
@plugin.on_method
def open_in_notepad(text: str, text_editor: str) -> None:
"""Open text in notepad - properly implemented to actually open notepad."""
if not text_editor or not text_editor.strip():
text_editor = DEFAULT_TEXT_EDITOR
try:
text_editor = os.path.normpath(text_editor)
# Validate that the editor executable exists
if not os.path.isfile(text_editor):
print(f"Error: Text editor not found: {text_editor}")
return
# Create a temporary file with the text content
fd, path = tempfile.mkstemp(suffix=".txt", prefix="ai_response_")
with os.fdopen(fd, 'w', encoding='utf-8') as f:
f.write(text)
# Open the file with notepad using subprocess
subprocess.Popen([text_editor, path])
except Exception as e:
print(f"Error opening notepad: {e}")
@plugin.on_method
def context_menu(data: str) -> ResultResponse:
"""Context menu for showing additional actions on results."""
if isinstance(data, list) and len(data) >= 2:
answer = data[0]
text_editor = data[1]
else:
answer = data if isinstance(data, str) else str(data)
text_editor = DEFAULT_TEXT_EDITOR
return send_results([
Result(
Title="Copy to clipboard",
SubTitle="Copy the full response to clipboard",
IcoPath="Images/copy.png",
JsonRPCAction={
"method": "copy_to_clipboard",
"parameters": [answer]
}
),
Result(
Title=f"Open in {Path(text_editor).stem}",
SubTitle=f"Open the full response in {Path(text_editor).stem} for viewing/editing",
IcoPath="Images/note.png",
JsonRPCAction={
"method": "open_in_notepad",
"parameters": [answer, text_editor]
}
)
])
if __name__ == "__main__":
plugin.run()