@@ -16,7 +16,7 @@ import fs from 'fs/promises';
16
16
import fetch from 'node-fetch' ;
17
17
import path from 'path' ;
18
18
import color from 'picocolors' ;
19
- import type { MemoryI } from 'types/memory' ;
19
+ import { type MemoryI } from 'types/memory' ;
20
20
import type { Pipe , PipeOld } from 'types/pipe' ;
21
21
import { getStoredAuth } from './../auth/index' ;
22
22
import {
@@ -484,17 +484,24 @@ export async function deployMemory({
484
484
p . log . step ( `Processing documents for memory: ${ memoryNameWithoutExt } ` ) ;
485
485
486
486
let filesToDeploy : string [ ] = [ ] ;
487
+ let filesToDelete : string [ ] = [ ] ;
487
488
let memoryDocs : MemoryDocumentI [ ] = [ ] ;
488
489
489
490
// Git sync memories
490
491
if ( memoryObject . config ?. useGitRepo ) {
491
492
// Get names of files to deploy, i.e., changed or new files
492
- filesToDeploy = await handleGitSyncMemories ( {
493
+ const {
494
+ filesToDeploy : gitFilesToDeploy ,
495
+ filesToDelete : gitFilesToDelete
496
+ } = await handleGitSyncMemories ( {
493
497
memoryName : memoryNameWithoutExt ,
494
498
config : memoryObject . config ,
495
499
account
496
500
} ) ;
497
501
502
+ filesToDeploy = gitFilesToDeploy ;
503
+ filesToDelete = gitFilesToDelete ;
504
+
498
505
// Load all documents contents for the memory
499
506
memoryDocs = await loadMemoryFiles ( memoryNameWithoutExt ) ;
500
507
@@ -513,7 +520,6 @@ export async function deployMemory({
513
520
spinner . stop (
514
521
`No documents to deploy for memory: ${ memoryNameWithoutExt } . Skipping.`
515
522
) ;
516
- return ;
517
523
}
518
524
519
525
spinner . stop ( `Processed memory: ${ memoryName . split ( '.' ) [ 0 ] } ` ) ;
@@ -525,7 +531,8 @@ export async function deployMemory({
525
531
documents : memoryDocs ,
526
532
account,
527
533
overwrite,
528
- isGitSync : memoryObject . config ?. useGitRepo
534
+ isGitSync : memoryObject . config ?. useGitRepo ,
535
+ docsToDelete : filesToDelete
529
536
} ) ;
530
537
spinner . stop ( `Deployment finished memory: ${ memoryObject . name } ` ) ;
531
538
} catch ( error ) {
@@ -549,13 +556,15 @@ export async function upsertMemory({
549
556
documents,
550
557
account,
551
558
overwrite,
552
- isGitSync = false
559
+ isGitSync = false ,
560
+ docsToDelete = [ ]
553
561
} : {
554
562
memory : MemoryI ;
555
563
documents : MemoryDocumentI [ ] ;
556
564
account : Account ;
557
565
overwrite : boolean ;
558
566
isGitSync ?: boolean ;
567
+ docsToDelete ?: string [ ] ;
559
568
} ) : Promise < void > {
560
569
const { createMemory } = getMemoryApiUrls ( {
561
570
account,
@@ -603,6 +612,21 @@ export async function upsertMemory({
603
612
documents,
604
613
overwrite
605
614
} ) ;
615
+
616
+ if ( docsToDelete ?. length > 0 ) {
617
+ await deleteDocumentsFromMemory ( {
618
+ documents : docsToDelete ,
619
+ name : memory . name ,
620
+ account
621
+ } ) ;
622
+ }
623
+
624
+ await updateDeployedCommitHash ( memory . name ) ;
625
+
626
+ p . log . info (
627
+ `Updated deployed commit hash for memory: ${ memory . name } `
628
+ ) ;
629
+
606
630
return ;
607
631
}
608
632
}
@@ -620,7 +644,18 @@ export async function upsertMemory({
620
644
await uploadDocumentsToMemory ( { documents, name, account } ) ;
621
645
622
646
if ( isGitSync ) {
647
+ if ( docsToDelete ?. length > 0 ) {
648
+ await deleteDocumentsFromMemory ( {
649
+ documents : docsToDelete ,
650
+ name : memory . name ,
651
+ account
652
+ } ) ;
653
+ }
654
+
623
655
await updateDeployedCommitHash ( memory . name ) ;
656
+ p . log . info (
657
+ `Updated deployed commit hash for memory: ${ memory . name } `
658
+ ) ;
624
659
}
625
660
} catch ( error ) {
626
661
dlog ( 'Error in createNewMemory:' , error ) ;
@@ -657,6 +692,38 @@ export async function uploadDocumentsToMemory({
657
692
}
658
693
}
659
694
695
+ export async function deleteDocumentsFromMemory ( {
696
+ documents,
697
+ name,
698
+ account
699
+ } : {
700
+ documents : string [ ] ;
701
+ name : string ;
702
+ account : Account ;
703
+ } ) {
704
+ p . log . info ( `Deleting documents from memory: ${ name } ` ) ;
705
+
706
+ for ( const doc of documents ) {
707
+ try {
708
+ p . log . message ( `Deleting document: ${ doc } ....` ) ;
709
+ await new Promise ( resolve => setTimeout ( resolve , 800 ) ) ; // To avoid rate limiting
710
+
711
+ const deleteResponse = await deleteDocument ( {
712
+ documentName : doc ,
713
+ memoryName : name ,
714
+ account
715
+ } ) ;
716
+
717
+ dlog ( `Delete response status: ${ deleteResponse . status } ` ) ;
718
+
719
+ p . log . message ( `Deleted document: ${ doc } ` ) ;
720
+ } catch ( error ) {
721
+ throw error ;
722
+ }
723
+ }
724
+ p . log . info ( `Deleted documents from memory: ${ name } ` ) ;
725
+ }
726
+
660
727
export async function handleExistingMemoryDeploy ( {
661
728
memory,
662
729
account,
@@ -892,6 +959,44 @@ async function getSignedUploadUrl({
892
959
}
893
960
}
894
961
962
+ async function deleteDocument ( {
963
+ documentName,
964
+ memoryName,
965
+ account
966
+ } : {
967
+ documentName : string ;
968
+ memoryName : string ;
969
+ account : Account ;
970
+ } ) {
971
+ const { deleteDocument } = getMemoryApiUrls ( {
972
+ account,
973
+ memoryName,
974
+ documentName
975
+ } ) ;
976
+
977
+ try {
978
+ const response = await fetch ( deleteDocument , {
979
+ method : 'DELETE' ,
980
+ headers : {
981
+ 'Content-Type' : 'application/json' ,
982
+ Authorization : `Bearer ${ account . apiKey } `
983
+ }
984
+ } ) ;
985
+
986
+ if ( ! response . ok ) {
987
+ const errorData = ( await response . json ( ) ) as ErrorResponse ;
988
+ throw new Error (
989
+ `HTTP error! status: ${ response . status } , message: ${ errorData . error ?. message } `
990
+ ) ;
991
+ }
992
+
993
+ return response ;
994
+ } catch ( error ) {
995
+ dlog ( 'Error in deleteDocument:' , error ) ;
996
+ throw error ;
997
+ }
998
+ }
999
+
895
1000
async function uploadDocument ( signedUrl : string , document : Blob ) {
896
1001
let mimeType = document . type ;
897
1002
@@ -930,16 +1035,19 @@ async function uploadDocument(signedUrl: string, document: Blob) {
930
1035
931
1036
export function getMemoryApiUrls ( {
932
1037
account,
933
- memoryName
1038
+ memoryName,
1039
+ documentName
934
1040
} : {
935
1041
account : Account ;
936
1042
memoryName : string ;
1043
+ documentName ?: string ;
937
1044
} ) {
938
1045
const isOrgAccount = account . apiKey . includes ( ':' ) ;
939
1046
const ownerLogin = isOrgAccount
940
1047
? account . apiKey . split ( ':' ) [ 0 ]
941
1048
: account . login ;
942
1049
const baseUrl = `https://api.langbase.com/beta` ;
1050
+ const baseUrlV1 = `https://api.langbase.com/v1` ;
943
1051
944
1052
// Create memory URL
945
1053
const createUrlOrg = `${ baseUrl } /org/${ ownerLogin } /memorysets` ;
@@ -955,9 +1063,13 @@ export function getMemoryApiUrls({
955
1063
// Delete memory URL
956
1064
const deleteMemory = `${ baseUrl } /memorysets/${ ownerLogin } /${ memoryName } ` ;
957
1065
1066
+ // Delete document URL
1067
+ const deleteDocument = `${ baseUrlV1 } /memory/${ memoryName } /documents/${ documentName } ` ;
1068
+
958
1069
return {
959
1070
listDocuments,
960
1071
deleteMemory,
1072
+ deleteDocument,
961
1073
createMemory : isOrgAccount ? createUrlOrg : createUrlUser ,
962
1074
uploadDocument : isOrgAccount ? uploadDocumentOrg : uploadDocumentUser
963
1075
} ;
0 commit comments