Skip to content

Commit

Permalink
Refactor memories to conversations (#1447)
Browse files Browse the repository at this point in the history
Closes #1311 

- [x] Rename `memories` to `conversations` everywhere

Note: This PR does not rename `memory` to `conversation` for mixpanel
events
  • Loading branch information
beastoin authored Dec 9, 2024
2 parents 7fa9ac3 + 4799fdb commit a98f6b6
Show file tree
Hide file tree
Showing 68 changed files with 1,672 additions and 1,645 deletions.
2 changes: 1 addition & 1 deletion app/lib/backend/http/api/apps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Future<bool> isAppSetupCompleted(String? url) async {
debugPrint('Response not a valid json: $e');
return false;
} catch (e) {
debugPrint('Error triggering memory request at endpoint: $e');
debugPrint('Error triggering request at endpoint: $e');
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ import 'dart:io';

import 'package:flutter/material.dart';
import 'package:friend_private/backend/http/shared.dart';
import 'package:friend_private/backend/schema/memory.dart';
import 'package:friend_private/backend/schema/conversation.dart';
import 'package:friend_private/backend/schema/structured.dart';
import 'package:friend_private/backend/schema/transcript_segment.dart';
import 'package:friend_private/env/env.dart';
import 'package:http/http.dart' as http;
import 'package:instabug_flutter/instabug_flutter.dart';
import 'package:path/path.dart';

Future<CreateMemoryResponse?> processInProgressMemory() async {
Future<CreateConversationResponse?> processInProgressConversation() async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v2/memories',
headers: {},
method: 'POST',
body: jsonEncode({}),
);
if (response == null) return null;
debugPrint('createMemoryServer: ${response.body}');
debugPrint('createConversationServer: ${response.body}');
if (response.statusCode == 200) {
return CreateMemoryResponse.fromJson(jsonDecode(response.body));
return CreateConversationResponse.fromJson(jsonDecode(response.body));
} else {
// TODO: Server returns 304 doesn't recover
CrashReporting.reportHandledCrash(
Exception('Failed to create memory'),
Exception('Failed to create conversation'),
StackTrace.current,
level: NonFatalExceptionLevel.info,
userAttributes: {
Expand All @@ -36,7 +36,8 @@ Future<CreateMemoryResponse?> processInProgressMemory() async {
return null;
}

Future<List<ServerMemory>> getMemories({int limit = 50, int offset = 0, List<MemoryStatus> statuses = const []}) async {
Future<List<ServerConversation>> getConversations(
{int limit = 50, int offset = 0, List<ConversationStatus> statuses = const []}) async {
var response = await makeApiCall(
url:
'${Env.apiBaseUrl}v1/memories?limit=$limit&offset=$offset&statuses=${statuses.map((val) => val.toString().split(".").last).join(",")}',
Expand All @@ -47,7 +48,8 @@ Future<List<ServerMemory>> getMemories({int limit = 50, int offset = 0, List<Mem
if (response.statusCode == 200) {
// decode body bytes to utf8 string and then parse json so as to avoid utf8 char issues
var body = utf8.decode(response.bodyBytes);
var memories = (jsonDecode(body) as List<dynamic>).map((memory) => ServerMemory.fromJson(memory)).toList();
var memories =
(jsonDecode(body) as List<dynamic>).map((conversation) => ServerConversation.fromJson(conversation)).toList();
debugPrint('getMemories length: ${memories.length}');
return memories;
} else {
Expand All @@ -56,71 +58,71 @@ Future<List<ServerMemory>> getMemories({int limit = 50, int offset = 0, List<Mem
return [];
}

Future<ServerMemory?> reProcessMemoryServer(String memoryId) async {
Future<ServerConversation?> reProcessConversationServer(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/reprocess',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/reprocess',
headers: {},
method: 'POST',
body: '',
);
if (response == null) return null;
debugPrint('reProcessMemoryServer: ${response.body}');
debugPrint('reProcessConversationServer: ${response.body}');
if (response.statusCode == 200) {
return ServerMemory.fromJson(jsonDecode(response.body));
return ServerConversation.fromJson(jsonDecode(response.body));
}
return null;
}

Future<bool> deleteMemoryServer(String memoryId) async {
Future<bool> deleteConversationServer(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId',
url: '${Env.apiBaseUrl}v1/memories/$conversationId',
headers: {},
method: 'DELETE',
body: '',
);
if (response == null) return false;
debugPrint('deleteMemory: ${response.statusCode}');
debugPrint('deleteConversation: ${response.statusCode}');
return response.statusCode == 204;
}

Future<ServerMemory?> getMemoryById(String memoryId) async {
Future<ServerConversation?> getConversationById(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId',
url: '${Env.apiBaseUrl}v1/memories/$conversationId',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return null;
debugPrint('getMemoryById: ${response.body}');
debugPrint('getConversationById: ${response.body}');
if (response.statusCode == 200) {
return ServerMemory.fromJson(jsonDecode(response.body));
return ServerConversation.fromJson(jsonDecode(response.body));
}
return null;
}

Future<bool> updateMemoryTitle(String memoryId, String title) async {
Future<bool> updateConversationTitle(String conversationId, String title) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/title?title=$title',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/title?title=$title',
headers: {},
method: 'PATCH',
body: '',
);
if (response == null) return false;
debugPrint('updateMemoryTitle: ${response.body}');
debugPrint('updateConversationTitle: ${response.body}');
return response.statusCode == 200;
}

Future<List<MemoryPhoto>> getMemoryPhotos(String memoryId) async {
Future<List<ConversationPhoto>> getConversationPhotos(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/photos',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/photos',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return [];
debugPrint('getMemoryPhotos: ${response.body}');
debugPrint('getConversationPhotos: ${response.body}');
if (response.statusCode == 200) {
return (jsonDecode(response.body) as List<dynamic>).map((photo) => MemoryPhoto.fromJson(photo)).toList();
return (jsonDecode(response.body) as List<dynamic>).map((photo) => ConversationPhoto.fromJson(photo)).toList();
}
return [];
}
Expand Down Expand Up @@ -149,71 +151,71 @@ class TranscriptsResponse {
}
}

Future<TranscriptsResponse> getMemoryTranscripts(String memoryId) async {
Future<TranscriptsResponse> getConversationTranscripts(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/transcripts',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/transcripts',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return TranscriptsResponse();
debugPrint('getMemoryTranscripts: ${response.body}');
debugPrint('getConversationTranscripts: ${response.body}');
if (response.statusCode == 200) {
var transcripts = (jsonDecode(response.body) as Map<String, dynamic>);
return TranscriptsResponse.fromJson(transcripts);
}
return TranscriptsResponse();
}

Future<bool> hasMemoryRecording(String memoryId) async {
Future<bool> hasConversationRecording(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/recording',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/recording',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return false;
debugPrint('getMemoryPhotos: ${response.body}');
debugPrint('getConversationPhotos: ${response.body}');
if (response.statusCode == 200) {
return jsonDecode(response.body)['has_recording'] ?? false;
}
return false;
}

Future<bool> assignMemoryTranscriptSegment(
String memoryId,
Future<bool> assignConversationTranscriptSegment(
String conversationId,
int segmentIdx, {
bool? isUser,
String? personId,
bool useForSpeechTraining = true,
}) async {
String assignType = isUser != null ? 'is_user' : 'person_id';
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/segments/$segmentIdx/assign?value=${isUser ?? personId}'
url: '${Env.apiBaseUrl}v1/memories/$conversationId/segments/$segmentIdx/assign?value=${isUser ?? personId}'
'&assign_type=$assignType&use_for_speech_training=$useForSpeechTraining',
headers: {},
method: 'PATCH',
body: '',
);
if (response == null) return false;
debugPrint('assignMemoryTranscriptSegment: ${response.body}');
debugPrint('assignConversationTranscriptSegment: ${response.body}');
return response.statusCode == 200;
}

Future<bool> setMemoryVisibility(String memoryId, {String visibility = 'shared'}) async {
Future<bool> setConversationVisibility(String conversationId, {String visibility = 'shared'}) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/visibility?value=$visibility&visibility=$visibility',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/visibility?value=$visibility&visibility=$visibility',
headers: {},
method: 'PATCH',
body: '',
);
if (response == null) return false;
debugPrint('setMemoryVisibility: ${response.body}');
debugPrint('setConversationVisibility: ${response.body}');
return response.statusCode == 200;
}

Future<bool> setMemoryEventsState(
String memoryId,
Future<bool> setConversationEventsState(
String conversationId,
List<int> eventsIdx,
List<bool> values,
) async {
Expand All @@ -222,7 +224,7 @@ Future<bool> setMemoryEventsState(
'values': values,
}));
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/events',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/events',
headers: {},
method: 'PATCH',
body: jsonEncode({
Expand All @@ -231,12 +233,12 @@ Future<bool> setMemoryEventsState(
}),
);
if (response == null) return false;
debugPrint('setMemoryEventsState: ${response.body}');
debugPrint('setConversationEventsState: ${response.body}');
return response.statusCode == 200;
}

Future<bool> setMemoryActionItemState(
String memoryId,
Future<bool> setConversationActionItemState(
String conversationId,
List<int> actionItemsIdx,
List<bool> values,
) async {
Expand All @@ -245,7 +247,7 @@ Future<bool> setMemoryActionItemState(
'values': values,
}));
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/action-items',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/action-items',
headers: {},
method: 'PATCH',
body: jsonEncode({
Expand All @@ -254,13 +256,13 @@ Future<bool> setMemoryActionItemState(
}),
);
if (response == null) return false;
debugPrint('setMemoryActionItemState: ${response.body}');
debugPrint('setConversationActionItemState: ${response.body}');
return response.statusCode == 200;
}

Future<bool> deleteMemoryActionItem(String memoryId, ActionItem item) async {
Future<bool> deleteConversationActionItem(String conversationId, ActionItem item) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/action-items',
url: '${Env.apiBaseUrl}v1/memories/$conversationId/action-items',
headers: {},
method: 'DELETE',
body: jsonEncode({
Expand All @@ -269,12 +271,12 @@ Future<bool> deleteMemoryActionItem(String memoryId, ActionItem item) async {
}),
);
if (response == null) return false;
debugPrint('deleteMemoryActionItem: ${response.body}');
debugPrint('deleteConversationActionItem: ${response.body}');
return response.statusCode == 204;
}

//this is expected to return complete memories
Future<List<ServerMemory>> sendStorageToBackend(File file, String sdCardDateTimeString) async {
Future<List<ServerConversation>> sendStorageToBackend(File file, String sdCardDateTimeString) async {
var request = http.MultipartRequest(
'POST',
Uri.parse('${Env.apiBaseUrl}sdcard_memory?date_time=$sdCardDateTimeString'),
Expand All @@ -292,7 +294,9 @@ Future<List<ServerMemory>> sendStorageToBackend(File file, String sdCardDateTime
return [];
}

var memories = (jsonDecode(response.body) as List<dynamic>).map((memory) => ServerMemory.fromJson(memory)).toList();
var memories = (jsonDecode(response.body) as List<dynamic>)
.map((conversation) => ServerConversation.fromJson(conversation))
.toList();
debugPrint('getMemories length: ${memories.length}');

return memories;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backend/http/api/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Future<List<ServerMessage>> getMessagesServer({
if (decodedBody.isEmpty) {
return [];
}
var messages = decodedBody.map((memory) => ServerMessage.fromJson(memory)).toList();
var messages = decodedBody.map((conversation) => ServerMessage.fromJson(conversation)).toList();
debugPrint('getMessages length: ${messages.length}');
return messages;
}
Expand Down
5 changes: 3 additions & 2 deletions app/lib/backend/http/api/speech_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ Future<List<String>> getExpandedProfileSamples() async {
}

Future<bool> deleteProfileSample(
String memoryId,
String conversationId,
int segmentIdx, {
String? personId,
}) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v3/speech-profile/expand?memory_id=$memoryId&segment_idx=$segmentIdx&person_id=$personId',
url:
'${Env.apiBaseUrl}v3/speech-profile/expand?memory_id=$conversationId&segment_idx=$segmentIdx&person_id=$personId',
headers: {},
method: 'DELETE',
body: '',
Expand Down
18 changes: 8 additions & 10 deletions app/lib/backend/http/api/users.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ Future<bool> deletePerson(String personId) async {
return response.statusCode == 204;
}

Future<String> getFollowUpQuestion({String memoryId = '0'}) async {
Future<String> getFollowUpQuestion({String conversationId = '0'}) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/joan/$memoryId/followup-question',
url: '${Env.apiBaseUrl}v1/joan/$conversationId/followup-question',
headers: {},
method: 'GET',
body: '',
Expand All @@ -239,19 +239,18 @@ Future<String> getFollowUpQuestion({String memoryId = '0'}) async {

/*Analytics*/

Future<bool> setMemorySummaryRating(String memoryId, int value, {String? reason}) async {
Future<bool> setConversationSummaryRating(String conversationId, int value, {String? reason}) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/users/analytics/memory_summary?memory_id=$memoryId&value=$value&reason=$reason',
url: '${Env.apiBaseUrl}v1/users/analytics/memory_summary?memory_id=$conversationId&value=$value&reason=$reason',
headers: {},
method: 'POST',
body: '',
);
if (response == null) return false;
debugPrint('setMemorySummaryRating response: ${response.body}');
debugPrint('setConversationSummaryRating response: ${response.body}');
return response.statusCode == 200;
}


Future<bool> setMessageResponseRating(String messageId, int value) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/users/analytics/chat_message?message_id=$messageId&value=$value',
Expand All @@ -264,16 +263,15 @@ Future<bool> setMessageResponseRating(String messageId, int value) async {
return response.statusCode == 200;
}


Future<bool> getHasMemorySummaryRating(String memoryId) async {
Future<bool> getHasConversationSummaryRating(String conversationId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/users/analytics/memory_summary?memory_id=$memoryId',
url: '${Env.apiBaseUrl}v1/users/analytics/memory_summary?memory_id=$conversationId',
headers: {},
method: 'GET',
body: '',
);
if (response == null) return false;
debugPrint('getHasMemorySummaryRating response: ${response.body}');
debugPrint('getHasConversationSummaryRating response: ${response.body}');

try {
var jsonResponse = jsonDecode(response.body);
Expand Down
Loading

0 comments on commit a98f6b6

Please sign in to comment.