({
memory,
documents,
account,
overwrite,
isGitSync = false,
docsToDelete = []
}: {
memory: MemoryI;
documents: MemoryDocumentI[];
account: Account;
overwrite: boolean;
isGitSync?: boolean;
docsToDelete?: string[];
})
| 524 | } |
| 525 | |
| 526 | export async function upsertMemory({ |
| 527 | memory, |
| 528 | documents, |
| 529 | account, |
| 530 | overwrite, |
| 531 | isGitSync = false, |
| 532 | docsToDelete = [] |
| 533 | }: { |
| 534 | memory: MemoryI; |
| 535 | documents: MemoryDocumentI[]; |
| 536 | account: Account; |
| 537 | overwrite: boolean; |
| 538 | isGitSync?: boolean; |
| 539 | docsToDelete?: string[]; |
| 540 | }): Promise<void> { |
| 541 | const { createMemory } = getMemoryApiUrls({ |
| 542 | memoryName: memory.name |
| 543 | }); |
| 544 | |
| 545 | try { |
| 546 | await new Promise(resolve => setTimeout(resolve, 800)); // To avoid rate limiting |
| 547 | const createResponse = await fetch(createMemory, { |
| 548 | method: 'POST', |
| 549 | headers: { |
| 550 | 'Content-Type': 'application/json', |
| 551 | Authorization: `Bearer ${account.apiKey}` |
| 552 | }, |
| 553 | body: JSON.stringify(memory) |
| 554 | }); |
| 555 | |
| 556 | if (!createResponse.ok) { |
| 557 | const errorData = (await createResponse.json()) as ErrorResponse; |
| 558 | |
| 559 | // If memory already exists, handle it. |
| 560 | if (errorData.error?.message.includes('already exists')) { |
| 561 | if (!isGitSync) { |
| 562 | // Show that Memory already exists |
| 563 | p.log.info( |
| 564 | `Memory "${memory.name}" already exists in production.` |
| 565 | ); |
| 566 | await handleExistingMemoryDeploy({ |
| 567 | memory, |
| 568 | account, |
| 569 | documents, |
| 570 | overwrite |
| 571 | }); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | // If Git-sync memory, update the existing memory |
| 576 | if (isGitSync) { |
| 577 | p.log.info( |
| 578 | `Memory "${memory.name}" already exists. Updating changed documents.` |
| 579 | ); |
| 580 | await handleGitSyncMemoryDeploy({ |
| 581 | memory, |
| 582 | account, |
| 583 | documents, |
no test coverage detected