Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nquang29 committed Jan 17, 2025
1 parent cc0d751 commit 2317444
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
58 changes: 53 additions & 5 deletions app/lib/backend/http/api/conversations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ Future<CreateConversationResponse?> processInProgressConversation() async {
}

Future<List<ServerConversation>> getConversations(
{int limit = 50, int offset = 0, List<ConversationStatus> statuses = const [], int? segment_limit}) async {
{int limit = 50, int offset = 0, List<ConversationStatus> 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,
Expand All @@ -56,13 +55,62 @@ Future<List<ServerConversation>> getConversations(
var memories =
(jsonDecode(body) as List<dynamic>).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<TranscriptSegment> 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}');
}
return [];
}


Future<List<TranscriptSegment>> 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<dynamic>).map((segment) {
return TranscriptSegment.fromJson(segment);
}).toList();

return segments;
} else {
debugPrint('getTranscriptSegmentsForConversation error ${response.statusCode}');
}

return [];
}

Future<ServerConversation?> reProcessConversationServer(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$conversationId/reprocess',
Expand Down
4 changes: 4 additions & 0 deletions app/lib/backend/schema/conversation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class ServerConversation {
return transcript;
}
}

void addTranscriptSegments(List<TranscriptSegment> newSegments) {
transcriptSegments.addAll(newSegments);
}
}

class SyncLocalFilesResponse {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/providers/capture_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/lib/providers/conversation_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions backend/routers/memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit 2317444

Please sign in to comment.