Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ro-kirchev committed May 6, 2024
2 parents 9f100b2 + 780b3b8 commit 0b9d978
Show file tree
Hide file tree
Showing 69 changed files with 4,652 additions and 6,190 deletions.
4 changes: 4 additions & 0 deletions apps/AppStandalone/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,9 @@
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
103 changes: 37 additions & 66 deletions apps/AppStandalone/lib/actions/actions.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:sama/backend/storage/memories.dart';
import 'package:uuid/uuid.dart';
import '/auth/firebase_auth/auth_util.dart';
import '/backend/api_requests/api_calls.dart';
import '/backend/backend.dart';
Expand All @@ -22,22 +24,21 @@ Future<void> onFinishAction(BuildContext context) async {
triggerFinishNotification();
}

// Update memory when finishing an action
void updateLastMemory(String lastWords) {
// FFAppState().lastMemory = '${FFAppState().lastMemory} $lastWords';
FFAppState().lastMemory = lastWords;
debugPrint('FFAppState().lastMemory ${FFAppState().lastMemory}');
// TODO: retrieve most recent memory (previous) and do a few tokens overlapping with the new one.
}

// Process the creation of memory records
Future<void> memoryCreationBlock(BuildContext context) async {
changeAppStateMemoryCreating();
var structuredMemory = await structureMemory();
debugPrint('Structured Memory: $structuredMemory');
if (structuredMemory.contains("N/A")) {
await saveFailureMemory(structuredMemory);
} else {
await processStructuredMemory(structuredMemory);
await finalizeMemoryRecord(structuredMemory);
}
}

Expand All @@ -49,33 +50,14 @@ Future<String> structureMemory() async {

// Save failure memory when structured memory contains NA
Future<void> saveFailureMemory(String structuredMemory) async {
logFirebaseEvent('memoryCreationBlock_backend_call');
await MemoriesRecord.collection.doc().set({
...createMemoriesRecordData(
memory: FFAppState().lastMemory,
user: currentUserReference,
MemoryRecord memory = MemoryRecord(
id: const Uuid().v4(),
date: DateTime.now(),
rawMemory: FFAppState().lastMemory,
structuredMemory: structuredMemory,
feedback: '',
toShowToUserShowHide: 'Hide',
emptyMemory: FFAppState().lastMemory == '',
isUselessMemory: true,
),
...mapToFirestore({'date': FieldValue.serverTimestamp()}),
});
}

// Process structured memory when it's valid
Future<void> processStructuredMemory(String structuredMemory) async {
debugPrint('processStructuredMemory: $structuredMemory');

logFirebaseEvent('memoryCreationBlock_backend_call', parameters: {
'jsonBody': (structuredMemory),
'memory': FFAppState().lastMemory,
'user': currentUserReference,
});
changeAppStateMemoryCreating();
await finalizeMemoryRecord(structuredMemory);
changeAppStateMemoryCreating();
isEmpty: FFAppState().lastMemory == '',
isUseless: true);
MemoryStorage.addMemory(memory);
}

// Update app state when starting memory processing
Expand All @@ -88,60 +70,49 @@ void changeAppStateMemoryCreating() {

// Finalize memory record after processing feedback
Future<void> finalizeMemoryRecord(String structuredMemory) async {
var vector = await vectorizeMemory(structuredMemory);
logFirebaseEvent('memoryCreationBlock_backend_call');
var memoryRecord = await createMemoryRecord(structuredMemory, vector);
logFirebaseEvent('memoryCreationBlock_backend_call');
MemoryRecord memoryRecord = await createMemoryRecord(structuredMemory);
changeAppStateMemoryCreating();
List<double> vector = await getEmbeddingsFromInput(structuredMemory);
await storeVectorData(memoryRecord, vector);
}

// Call vectorization API for structured memory
Future<List<double>> vectorizeMemory(String structuredMemory) async {
return await getEmbeddingsFromInput(structuredMemory);
}

// Create memory record
Future<MemoriesRecord> createMemoryRecord(String structuredMemory, List<double> vector) async {
var recordRef = MemoriesRecord.collection.doc();
await recordRef.set({
...createMemoriesRecordData(
memory: FFAppState().lastMemory,
user: currentUserReference,
Future<MemoryRecord> createMemoryRecord(String structuredMemory) async {
var memory = MemoryRecord(
id: const Uuid().v4(),
date: DateTime.now(),
rawMemory: FFAppState().lastMemory,
structuredMemory: structuredMemory,
feedback: '',
toShowToUserShowHide: FFAppState().isFeedbackUseful,
emptyMemory: FFAppState().lastMemory == '',
isUselessMemory: false,
),
...mapToFirestore({'date': FieldValue.serverTimestamp()}),
});
return MemoriesRecord.getDocumentOnce(recordRef);
isEmpty: FFAppState().lastMemory == '',
isUseless: false);
MemoryStorage.addMemory(memory);
return memory;
}

// Store vector data after memory record creation
Future<void> storeVectorData(MemoriesRecord memoryRecord, List<double> vector) async {
Future<void> storeVectorData(MemoryRecord memoryRecord, List<double> vector) async {
// debugPrint('storeVectorData: memoryRecord -> $memoryRecord');
// debugPrint('storeVectorData: vectorResponse -> ${vectorResponse.jsonBody}');

await createPineconeVector(
vector,
memoryRecord.structuredMemory,
memoryRecord.reference.id,
memoryRecord.id,
);
// debugPrint('storeVectorData VectorAdded: ${vectorAdded.statusCode} ${vectorAdded.jsonBody}');
// TODO: never triggers because `toShowToUserShowHide` is always 'Hide', because feedback logic was removed.
if (memoryRecord.toShowToUserShowHide == 'Show' && !memoryRecord.emptyMemory && !memoryRecord.isUselessMemory) {
logFirebaseEvent('memoryCreationBlock_trigger_push_notific');
if (currentUserReference != null) {
triggerPushNotification(
notificationTitle: 'Sama',
notificationText: FFAppState().feedback,
userRefs: [currentUserReference!],
initialPageName: 'chat',
parameterData: {},
);
}
}
// if (memoryRecord.toShowToUserShowHide == 'Show' && !memoryRecord.emptyMemory && !memoryRecord.isUselessMemory) {
// logFirebaseEvent('memoryCreationBlock_trigger_push_notific');
// if (currentUserReference != null) {
// triggerPushNotification(
// notificationTitle: 'Sama',
// notificationText: FFAppState().feedback,
// userRefs: [currentUserReference!],
// initialPageName: 'chat',
// parameterData: {},
// );
// }
// }
}

// Fetch latest valid memories
Expand Down
Loading

0 comments on commit 0b9d978

Please sign in to comment.