From 2317444835cd4432265bf4ccb297b2c8427868d8 Mon Sep 17 00:00:00 2001 From: nquanq <98quangnd@gmail.com> Date: Fri, 17 Jan 2025 16:28:32 +0700 Subject: [PATCH] update --- app/lib/backend/http/api/conversations.dart | 58 ++++++++++++++++++-- app/lib/backend/schema/conversation.dart | 4 ++ app/lib/providers/capture_provider.dart | 2 +- app/lib/providers/conversation_provider.dart | 4 +- backend/routers/memories.py | 9 +++ 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/app/lib/backend/http/api/conversations.dart b/app/lib/backend/http/api/conversations.dart index 8d8016ac1..0ce852408 100644 --- a/app/lib/backend/http/api/conversations.dart +++ b/app/lib/backend/http/api/conversations.dart @@ -37,12 +37,11 @@ Future processInProgressConversation() async { } Future> getConversations( - {int limit = 50, int offset = 0, List statuses = const [], int? segment_limit}) async { + {int limit = 50, int offset = 0, List statuses = const []}) async { - var url = '${Env.apiBaseUrl}v1/memories?limit=$limit&offset=$offset&statuses=${statuses.map((val) => val.toString().split(".").last).join(",")}'; - if (segment_limit != null) { - url += '&segment_limit=$segment_limit'; - } + int segmentLimit = 50; + + var url = '${Env.apiBaseUrl}v1/memories?limit=$limit&offset=$offset&statuses=${statuses.map((val) => val.toString().split(".").last).join(",")}&segment_limit=$segmentLimit'; var response = await makeApiCall( url: url, @@ -56,6 +55,25 @@ Future> getConversations( var memories = (jsonDecode(body) as List).map((conversation) => ServerConversation.fromJson(conversation)).toList(); debugPrint('getMemories length: ${memories.length}'); + + for (var memory in memories) { + if (memory.transcriptSegments.length < segmentLimit) { + continue; + } + // Get all transcript segments for this memory, with paging + List allSegments = []; + int segmentOffset = memory.transcriptSegments.length; + + while (true) { + var segments = await getTranscriptSegmentsForConversation(memory.id, segmentLimit, offset:segmentOffset); + if (segments.isEmpty) break; + + allSegments.addAll(segments); + segmentOffset += segmentLimit; + } + memory.addTranscriptSegments(allSegments); + } + return memories; } else { debugPrint('getMemories error ${response.statusCode}'); @@ -63,6 +81,36 @@ Future> getConversations( return []; } + +Future> getTranscriptSegmentsForConversation(String conversationId, int? limit, {int offset = 0}) async { + var url = '${Env.apiBaseUrl}v1/memories/$conversationId/transcript_segments'; + if (limit != null) { + url += '?limit=$limit&offset=$offset'; + } + + var response = await makeApiCall( + url: url, + headers: {}, + method: 'GET', + body: '' + ); + + if (response == null) return []; + + if (response.statusCode == 200) { + var body = utf8.decode(response.bodyBytes); + var segments = (jsonDecode(body) as List).map((segment) { + return TranscriptSegment.fromJson(segment); + }).toList(); + + return segments; + } else { + debugPrint('getTranscriptSegmentsForConversation error ${response.statusCode}'); + } + + return []; +} + Future reProcessConversationServer(String conversationId) async { var response = await makeApiCall( url: '${Env.apiBaseUrl}v1/memories/$conversationId/reprocess', diff --git a/app/lib/backend/schema/conversation.dart b/app/lib/backend/schema/conversation.dart index 23b951400..e674f9cc2 100644 --- a/app/lib/backend/schema/conversation.dart +++ b/app/lib/backend/schema/conversation.dart @@ -204,6 +204,10 @@ class ServerConversation { return transcript; } } + + void addTranscriptSegments(List newSegments) { + transcriptSegments.addAll(newSegments); + } } class SyncLocalFilesResponse { diff --git a/app/lib/providers/capture_provider.dart b/app/lib/providers/capture_provider.dart index 28cd2ed23..647f9b8a4 100644 --- a/app/lib/providers/capture_provider.dart +++ b/app/lib/providers/capture_provider.dart @@ -533,7 +533,7 @@ class CaptureProvider extends ChangeNotifier } void _loadInProgressConversation() async { - var memories = await getConversations(statuses: [ConversationStatus.in_progress], limit: 1, segment_limit: 50); + var memories = await getConversations(statuses: [ConversationStatus.in_progress], limit: 1); _inProgressConversation = memories.isNotEmpty ? memories.first : null; if (_inProgressConversation != null) { segments = _inProgressConversation!.transcriptSegments; diff --git a/app/lib/providers/conversation_provider.dart b/app/lib/providers/conversation_provider.dart index edb44d62f..efe645d0d 100644 --- a/app/lib/providers/conversation_provider.dart +++ b/app/lib/providers/conversation_provider.dart @@ -151,7 +151,7 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener Future getConversationsFromServer() async { setLoadingConversations(true); - var mem = await getConversations(segment_limit: 50); + var mem = await getConversations(); conversations = mem; conversations.sort((a, b) => b.createdAt.compareTo(a.createdAt)); setLoadingConversations(false); @@ -181,7 +181,7 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener if (conversations.length % 50 != 0) return; if (isLoadingConversations) return; setLoadingConversations(true); - var newConversations = await getConversations(offset: conversations.length, segment_limit: 50); + var newConversations = await getConversations(offset: conversations.length); conversations.addAll(newConversations); conversations.sort((a, b) => b.createdAt.compareTo(a.createdAt)); filterGroupedConversations(previousQuery); diff --git a/backend/routers/memories.py b/backend/routers/memories.py index 988bb7cb8..a2e96c475 100644 --- a/backend/routers/memories.py +++ b/backend/routers/memories.py @@ -81,6 +81,15 @@ def get_memories(limit: int = 100, offset: int = 0, statuses: str = "", segment_ def get_memory_by_id(memory_id: str, uid: str = Depends(auth.get_current_user_uid)): return _get_memory_by_id(uid, memory_id) +@router.get("/v1/memories/{memory_id}/transcript_segments", response_model=List[TranscriptSegment], tags=['memories']) +def get_transcript_segments_memory(memory_id: str, uid: str = Depends(auth.get_current_user_uid), limit: int = None, offset: int = 0): + memory = _get_memory_by_id(uid, memory_id) + transcript_segments = memory["transcript_segments"] + if limit is not None: + transcript_segments = transcript_segments[offset:limit+offset] + return transcript_segments + + @router.patch("/v1/memories/{memory_id}/title", tags=['memories']) def patch_memory_title(memory_id: str, title: str, uid: str = Depends(auth.get_current_user_uid)):