Skip to content

Commit d8983a5

Browse files
authored
Backend cleanup (#2375)
- [x] remove deprecated routes, models and utils - [x] consolidate endpoints - [x] split llm.py file into multiple files
2 parents 4a4e5c1 + 7eb3670 commit d8983a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1943
-2645
lines changed

app/lib/backend/http/api/messages.dart

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Future<List<ServerMessage>> getMessagesServer({
4040
Future<List<ServerMessage>> clearChatServer({String? pluginId}) async {
4141
if (pluginId == 'no_selected') pluginId = null;
4242
var response = await makeApiCall(
43-
url: '${Env.apiBaseUrl}v1/messages?plugin_id=${pluginId ?? ''}',
43+
url: '${Env.apiBaseUrl}v2/messages?plugin_id=${pluginId ?? ''}',
4444
headers: {},
4545
method: 'DELETE',
4646
body: '',
@@ -53,32 +53,6 @@ Future<List<ServerMessage>> clearChatServer({String? pluginId}) async {
5353
}
5454
}
5555

56-
Future<ServerMessage> sendMessageServer(String text, {String? appId, List<String>? fileIds}) {
57-
var url = '${Env.apiBaseUrl}v1/messages?plugin_id=$appId';
58-
if (appId == null || appId.isEmpty || appId == 'null' || appId == 'no_selected') {
59-
url = '${Env.apiBaseUrl}v1/messages';
60-
}
61-
return makeApiCall(
62-
url: url,
63-
headers: {},
64-
method: 'POST',
65-
body: jsonEncode({'text': text, 'file_ids': fileIds}),
66-
).then((response) {
67-
if (response == null) throw Exception('Failed to send message');
68-
if (response.statusCode == 200) {
69-
return ServerMessage.fromJson(jsonDecode(response.body));
70-
} else {
71-
Logger.error('Failed to send message ${response.body}');
72-
CrashReporting.reportHandledCrash(
73-
Exception('Failed to send message ${response.body}'),
74-
StackTrace.current,
75-
level: NonFatalExceptionLevel.error,
76-
);
77-
return ServerMessage.failedMessage();
78-
}
79-
});
80-
}
81-
8256
ServerMessageChunk? parseMessageChunk(String line, String messageId) {
8357
if (line.startsWith('think: ')) {
8458
return ServerMessageChunk(messageId, line.substring(7).replaceAll("__CRLF__", "\n"), MessageChunkType.think);
@@ -164,7 +138,7 @@ Stream<ServerMessageChunk> sendMessageStreamServer(String text, {String? appId,
164138

165139
Future<ServerMessage> getInitialAppMessage(String? appId) {
166140
return makeApiCall(
167-
url: '${Env.apiBaseUrl}v1/initial-message?plugin_id=$appId',
141+
url: '${Env.apiBaseUrl}v2/initial-message?app_id=$appId',
168142
headers: {},
169143
method: 'POST',
170144
body: '',
@@ -235,36 +209,10 @@ Stream<ServerMessageChunk> sendVoiceMessageStreamServer(List<File> files) async*
235209
}
236210
}
237211

238-
Future<List<ServerMessage>> sendVoiceMessageServer(List<File> files) async {
239-
var request = http.MultipartRequest(
240-
'POST',
241-
Uri.parse('${Env.apiBaseUrl}v1/voice-messages'),
242-
);
243-
for (var file in files) {
244-
request.files.add(await http.MultipartFile.fromPath('files', file.path, filename: basename(file.path)));
245-
}
246-
request.headers.addAll({'Authorization': await getAuthHeader()});
247-
248-
try {
249-
var streamedResponse = await request.send();
250-
var response = await http.Response.fromStream(streamedResponse);
251-
if (response.statusCode == 200) {
252-
debugPrint('sendVoiceMessageServer response body: ${jsonDecode(response.body)}');
253-
return ((jsonDecode(response.body) ?? []) as List<dynamic>).map((m) => ServerMessage.fromJson(m)).toList();
254-
} else {
255-
debugPrint('Failed to upload sample. Status code: ${response.statusCode} ${response.body}');
256-
throw Exception('Failed to upload sample. Status code: ${response.statusCode}');
257-
}
258-
} catch (e) {
259-
debugPrint('An error occurred uploadSample: $e');
260-
throw Exception('An error occurred uploadSample: $e');
261-
}
262-
}
263-
264212
Future<List<MessageFile>?> uploadFilesServer(List<File> files, {String? appId}) async {
265-
var url = '${Env.apiBaseUrl}v1/files?plugin_id=$appId';
213+
var url = '${Env.apiBaseUrl}v2/files?app_id=$appId';
266214
if (appId == null || appId.isEmpty || appId == 'null' || appId == 'no_selected') {
267-
url = '${Env.apiBaseUrl}v1/files';
215+
url = '${Env.apiBaseUrl}v2/files';
268216
}
269217
var request = http.MultipartRequest(
270218
'POST',
@@ -301,7 +249,7 @@ Future<List<MessageFile>?> uploadFilesServer(List<File> files, {String? appId})
301249

302250
Future reportMessageServer(String messageId) async {
303251
var response = await makeApiCall(
304-
url: '${Env.apiBaseUrl}v1/messages/$messageId/report',
252+
url: '${Env.apiBaseUrl}v2/messages/$messageId/report',
305253
headers: {},
306254
method: 'POST',
307255
body: '',
@@ -312,20 +260,19 @@ Future reportMessageServer(String messageId) async {
312260
}
313261
}
314262

315-
316263
Future<String> transcribeVoiceMessage(File audioFile) async {
317264
try {
318265
var request = http.MultipartRequest(
319266
'POST',
320-
Uri.parse('${Env.apiBaseUrl}v1/voice-message/transcribe'),
267+
Uri.parse('${Env.apiBaseUrl}v2/voice-message/transcribe'),
321268
);
322-
269+
323270
request.headers.addAll({'Authorization': await getAuthHeader()});
324271
request.files.add(await http.MultipartFile.fromPath('files', audioFile.path));
325-
272+
326273
var streamedResponse = await request.send();
327274
var response = await http.Response.fromStream(streamedResponse);
328-
275+
329276
if (response.statusCode == 200) {
330277
final data = jsonDecode(response.body);
331278
return data['transcript'] ?? '';

app/lib/providers/message_provider.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,6 @@ class MessageProvider extends ChangeNotifier {
412412
setShowTypingIndicator(false);
413413
}
414414

415-
Future sendMessageToServer(String text, String? appId) async {
416-
setShowTypingIndicator(true);
417-
messages.insert(0, ServerMessage.empty(appId: appId));
418-
List<String> fileIds = uploadedFiles.map((e) => e.id).toList();
419-
var mes = await sendMessageServer(text, appId: appId, fileIds: fileIds);
420-
if (messages[0].id == '0000') {
421-
messages[0] = mes;
422-
}
423-
setShowTypingIndicator(false);
424-
notifyListeners();
425-
}
426-
427415
Future sendInitialAppMessage(App? app) async {
428416
setSendingMessage(true);
429417
ServerMessage message = await getInitialAppMessage(app?.id);

backend/database/chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ def batch_delete_messages(parent_doc_ref, batch_size=450, plugin_id: Optional[st
228228
last_doc = docs_list[-1]
229229

230230

231-
def clear_chat(uid: str, plugin_id: Optional[str] = None, chat_session_id: Optional[str] = None):
231+
def clear_chat(uid: str, app_id: Optional[str] = None, chat_session_id: Optional[str] = None):
232232
try:
233233
user_ref = db.collection('users').document(uid)
234234
print(f"Deleting messages for user: {uid}")
235235
if not user_ref.get().exists:
236236
return {"message": "User not found"}
237-
batch_delete_messages(user_ref, plugin_id=plugin_id, chat_session_id=chat_session_id)
237+
batch_delete_messages(user_ref, plugin_id=app_id, chat_session_id=chat_session_id)
238238
return None
239239
except Exception as e:
240240
return {"message": str(e)}

backend/database/processing_conversations.py

Lines changed: 0 additions & 99 deletions
This file was deleted.

backend/database/vector_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pinecone import Pinecone
88

99
from models.conversation import Conversation
10-
from utils.llm import embeddings
10+
from utils.llm.clients import embeddings
1111

1212
if os.getenv('PINECONE_API_KEY') is not None:
1313
pc = Pinecone(api_key=os.getenv('PINECONE_API_KEY', ''))

backend/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from modal import Image, App, asgi_app, Secret
88
from routers import workflow, chat, firmware, plugins, transcribe, notifications, \
9-
speech_profile, agents, users, processing_conversations, trends, sync, apps, custom_auth, \
9+
speech_profile, agents, users, trends, sync, apps, custom_auth, \
1010
payment, integration, conversations, memories, mcp
1111

1212
from utils.other.timeout import TimeoutMiddleware
@@ -31,7 +31,6 @@
3131
app.include_router(integration.router)
3232
app.include_router(agents.router)
3333
app.include_router(users.router)
34-
app.include_router(processing_conversations.router)
3534
app.include_router(trends.router)
3635

3736
app.include_router(firmware.router)

backend/models/plugin.py

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)