-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
270 lines (214 loc) · 8.47 KB
/
Copy pathapi.py
File metadata and controls
270 lines (214 loc) · 8.47 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
"""
API routes module for voice command application.
"""
from flask import Blueprint, request, jsonify, send_from_directory, current_app
import db
import os
from speech_recognition_handler import start_speech_recognition, stop_speech_recognition, update_openai_api_key, toggle_sentiment_mode, get_sentiment_mode_state, execute_script, get_scripts_execution_state, toggle_scripts_execution
# Create the API blueprint
api_bp = Blueprint('api', __name__)
# Routes for serving static files
@api_bp.route('/')
def index():
return send_from_directory('public', 'index.html')
@api_bp.route('/<path:path>')
def serve_public(path):
return send_from_directory('public', path)
# API Routes
@api_bp.route('/api/commands', methods=['GET'])
def get_commands():
commands = db.get_commands()
return jsonify(commands)
@api_bp.route('/api/commands', methods=['POST'])
def add_command():
data = request.get_json()
if not data or 'script' not in data:
return jsonify({'error': 'Missing required fields'}), 400
# Handle both single phrase and multiple phrases
phrases = data.get('phrases', [])
if not phrases and 'phrase' in data:
phrases = [data['phrase']]
if not phrases:
return jsonify({'error': 'At least one phrase is required'}), 400
# Get sentiment analysis field
understand_sentiment = data.get('understand_sentiment', False)
# Get partial match field
partial_match = data.get('partial_match', False)
command_id = db.add_command(
phrases,
data['script'],
understand_sentiment=understand_sentiment,
partial_match=partial_match
)
return jsonify({
'id': command_id,
'phrases': phrases,
'phrase': phrases[0], # For backward compatibility
'script': data['script'],
'understand_sentiment': understand_sentiment,
'partial_match': partial_match
}), 201
@api_bp.route('/api/commands/<int:command_id>', methods=['PUT'])
def update_command(command_id):
data = request.get_json()
if not data or 'script' not in data:
return jsonify({'error': 'Missing required fields'}), 400
# Handle both single phrase and multiple phrases
phrases = data.get('phrases', [])
if not phrases and 'phrase' in data:
phrases = [data['phrase']]
if not phrases:
return jsonify({'error': 'At least one phrase is required'}), 400
# Get sentiment analysis field
understand_sentiment = data.get('understand_sentiment', False)
# Get partial match field
partial_match = data.get('partial_match', False)
success = db.update_command(
command_id,
phrases,
data['script'],
understand_sentiment=understand_sentiment,
partial_match=partial_match
)
if not success:
return jsonify({'error': 'Command not found'}), 404
return jsonify({
'id': command_id,
'phrases': phrases,
'phrase': phrases[0], # For backward compatibility
'script': data['script'],
'understand_sentiment': understand_sentiment,
'partial_match': partial_match
})
@api_bp.route('/api/commands/<int:command_id>', methods=['DELETE'])
def delete_command(command_id):
success = db.delete_command(command_id)
if not success:
return jsonify({'error': 'Command not found'}), 404
return jsonify({'message': 'Command deleted successfully'}), 200
@api_bp.route('/api/active', methods=['GET'])
def get_active():
active = db.get_active_state()
return jsonify({'active': active})
@api_bp.route('/api/active', methods=['POST'])
def set_active():
data = request.get_json()
if 'active' not in data:
return jsonify({'error': 'Missing active state'}), 400
active = data['active']
db.set_active_state(active)
if active:
# Start speech recognition with socketio instance
socketio = request.environ.get('socketio')
start_speech_recognition(socketio)
else:
# Stop speech recognition
stop_speech_recognition()
return jsonify({'active': active})
@api_bp.route('/api/openai-key', methods=['GET'])
def get_openai_key_status():
api_key = db.get_openai_api_key()
is_set = bool(api_key)
# Mask the API key for security
masked_key = ""
if api_key and len(api_key) > 4:
masked_key = '*' * (len(api_key) - 4) + api_key[-4:]
elif api_key:
masked_key = api_key # Don't mask if it's too short
return jsonify({
'isSet': is_set,
'apiKey': masked_key
})
@api_bp.route('/api/openai-key', methods=['POST'])
def set_openai_key():
data = request.get_json()
if 'apiKey' not in data:
return jsonify({'error': 'Missing API key'}), 400
api_key = data['apiKey']
db.set_openai_api_key(api_key)
# Update the API key in the speech recognition handler
update_openai_api_key(api_key)
return jsonify({'success': True})
@api_bp.route('/api/openai-stats', methods=['GET'])
def get_openai_stats():
request_count = db.get_openai_request_count()
return jsonify({
'requestCount': request_count
})
@api_bp.route('/api/shortcut-key', methods=['GET'])
def get_shortcut_key():
shortcut_key = db.get_global_shortcut_key()
return jsonify({
'shortcutKey': shortcut_key
})
@api_bp.route('/api/shortcut-key', methods=['POST'])
def set_shortcut_key():
data = request.get_json()
if 'shortcutKey' not in data:
return jsonify({'error': 'Missing shortcut key'}), 400
shortcut_key = data['shortcutKey']
db.set_global_shortcut_key(shortcut_key)
return jsonify({'success': True})
@api_bp.route('/api/sentiment-mode', methods=['GET'])
def get_sentiment_mode():
active = get_sentiment_mode_state()
return jsonify({'active': active})
@api_bp.route('/api/sentiment-mode', methods=['POST'])
def set_sentiment_mode():
data = request.get_json()
if 'active' not in data:
return jsonify({'error': 'Missing active state'}), 400
active = data['active']
new_state = toggle_sentiment_mode() if active != get_sentiment_mode_state() else get_sentiment_mode_state()
return jsonify({'active': new_state})
@api_bp.route('/api/ai-timeout', methods=['GET'])
def get_ai_timeout():
"""Get AI mode timeout settings."""
timeout_settings = db.get_ai_timeout_settings()
return jsonify(timeout_settings)
@api_bp.route('/api/ai-timeout', methods=['POST'])
def update_ai_timeout():
try:
data = request.get_json()
enabled = data.get('enabled', False)
seconds = data.get('seconds', 60)
db.update_ai_timeout_setting(enabled, seconds)
# No need to call update_ai_timeout_state since the settings will be read from the DB when needed
print(f"Updated AI timeout: enabled={enabled}, seconds={seconds}")
return jsonify({'success': True, 'enabled': enabled, 'seconds': seconds})
except Exception as e:
print(f"Error updating AI timeout: {str(e)}")
return jsonify({'error': str(e)}), 400
@api_bp.route('/api/scripts-execution', methods=['GET'])
def get_scripts_execution():
"""Get the current scripts execution state."""
try:
active = get_scripts_execution_state()
return jsonify({'active': active})
except Exception as e:
print(f"Error getting scripts execution state: {str(e)}")
return jsonify({'error': str(e)}), 500
@api_bp.route('/api/scripts-execution', methods=['POST'])
def toggle_scripts_execution_api():
"""Toggle the scripts execution state."""
try:
active = toggle_scripts_execution()
# If socketio is available, emit the event
socketio = request.environ.get('socketio')
if socketio:
socketio.emit('scripts_execution', {'active': active})
return jsonify({'active': active})
except Exception as e:
print(f"Error toggling scripts execution: {str(e)}")
return jsonify({'error': str(e)}), 500
@api_bp.route('/api/script-preview', methods=['POST'])
def preview_script():
data = request.get_json()
if 'script' not in data:
return jsonify({'error': 'Missing script'}), 400
try:
# Execute the script, with a flag indicating this is just a preview
result = execute_script(data['script'], preview_mode=True)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500