-
Notifications
You must be signed in to change notification settings - Fork 0
setup backend services for source handling #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7af20b8
setup backend services for source handling
tsiq-ikennao 2501d70
delete vectors during re-ingest
tsiq-ikennao 535ee3e
update service interface
tsiq-ikennao 91544b7
address type errors
tsiq-ikennao 68316d8
delete sources on conversation delete
tsiq-ikennao 39b7d11
address pr comment
tsiq-ikennao a233813
address pr comment
tsiq-ikennao 4794dcd
address pr comment
tsiq-ikennao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| import './type'; | ||
| import './query'; | ||
| import './mutation'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { builder } from 'config/builder'; | ||
|
|
||
| // ---- Delete external source | ||
| builder.mutationField('deleteExternalSource', (t) => | ||
| t.field({ | ||
| type: 'Boolean', | ||
| nullable: false, | ||
| args: { | ||
| id: t.arg.string({ required: true }), | ||
| }, | ||
| resolve: async (_parent, args, ctx) => { | ||
| try { | ||
| // Get the source first to determine the vector namespace | ||
| const source = await ctx.services.externalSourceService.findById( | ||
| args.id | ||
| ); | ||
| if (!source) { | ||
| throw new Error(`External source ${args.id} not found`); | ||
| } | ||
|
|
||
| // TODO: Add ownership verification once user auth is implemented | ||
| // if (source.conversation?.userId !== ctx.user.id) { | ||
| // throw new Error('Not authorized to delete this source'); | ||
| // } | ||
|
|
||
| // Delete vectors from Pinecone first | ||
| // Namespace is conversationId for conversation-linked sources, or sourceId for standalone | ||
| const vectorNamespace = source.conversationId || args.id; | ||
| await ctx.vectorService.deleteVectors( | ||
| args.id, | ||
| vectorNamespace, | ||
| source.conversationId ?? undefined | ||
| ); | ||
|
|
||
| // Delete the source and its chunks from the database | ||
| await ctx.services.externalSourceService.deleteWithChunks(args.id); | ||
|
|
||
| // TODO: Implement R2 file cleanup - currently leaves orphaned files | ||
| // Options: | ||
| // 1. Delete synchronously here: ctx.r2.deleteFile(metadata?.fileKey) | ||
| // 2. Create an outbox event for async cleanup | ||
| // 3. Implement a scheduled cleanup job to remove orphaned R2 files | ||
| // Tracked in: [add tracking issue URL when created] | ||
|
|
||
| return true; | ||
| } catch (error) { | ||
| console.error(`Failed to delete external source ${args.id}:`, error); | ||
| throw new Error( | ||
| `Failed to delete external source: ${error instanceof Error ? error.message : 'Unknown error'}` | ||
| ); | ||
| } | ||
| }, | ||
| }) | ||
| ); | ||
|
|
||
| // ---- Re-ingest external source | ||
| /** | ||
| * Re-ingest an external source by triggering the processing pipeline again | ||
| * This clears existing chunks and re-processes the file from R2 storage | ||
| */ | ||
| builder.mutationField('reIngestSource', (t) => | ||
| t.prismaField({ | ||
| type: 'ExternalSource', | ||
| nullable: false, | ||
| args: { | ||
| id: t.arg.string({ required: true }), | ||
| }, | ||
| resolve: async (_query, _parent, args, ctx) => { | ||
| try { | ||
| // Get the source to verify it exists and get file info | ||
| const source = await ctx.services.externalSourceService.findById( | ||
| args.id | ||
| ); | ||
| if (!source) { | ||
| throw new Error(`External source ${args.id} not found`); | ||
| } | ||
|
|
||
| // Get metadata for file info | ||
| const metadata = source.metadata as Record<string, unknown> | null; | ||
| const fileKey = metadata?.fileKey as string | undefined; | ||
|
|
||
| if (!fileKey) { | ||
| throw new Error( | ||
| 'Cannot re-ingest: source does not have a stored file' | ||
| ); | ||
| } | ||
|
|
||
| // Delete existing vectors from Pinecone first (external service, not transactional) | ||
| // Namespace is conversationId for conversation-linked sources, or sourceId for standalone | ||
| // Note: If Pinecone deletion fails, we throw before DB changes, maintaining consistency. | ||
| // If Pinecone succeeds but DB fails, vectors are orphaned (acceptable - will be overwritten on retry). | ||
| const vectorNamespace = source.conversationId || args.id; | ||
| await ctx.vectorService.deleteVectors( | ||
| args.id, | ||
| vectorNamespace, | ||
| source.conversationId ?? undefined | ||
| ); | ||
|
|
||
| // Wrap DB operations in a transaction to ensure atomicity: | ||
| // - Delete chunks | ||
| // - Update source metadata | ||
| // - Create outbox event | ||
| // This prevents the source from being in an inconsistent state if any DB operation fails. | ||
| const updatedSource = await ctx.prisma.$transaction(async (tx: any) => { | ||
| // Delete existing chunks (they will be re-created during processing) | ||
| await tx.chunk.deleteMany({ | ||
| where: { externalSourceId: args.id }, | ||
| }); | ||
|
|
||
| // Update source metadata to mark as pending re-ingest | ||
| const updated = await tx.externalSource.update({ | ||
| where: { id: args.id }, | ||
| data: { | ||
| metadata: { | ||
| ...metadata, | ||
| processingFailed: false, | ||
| processingError: null, | ||
| reIngestRequestedAt: new Date().toISOString(), | ||
| status: 'pending', | ||
| }, | ||
| }, | ||
| include: { conversation: true }, | ||
| }); | ||
|
|
||
| // Create outbox event for async re-processing | ||
| await tx.outbox.create({ | ||
| data: { | ||
| type: 'SourceReIngestRequested', | ||
| payload: { | ||
| externalSourceId: args.id, | ||
| fileKey, | ||
| conversationId: source.conversationId, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return updated; | ||
| }); | ||
|
|
||
| return updatedSource; | ||
| } catch (error) { | ||
| console.error(`Failed to re-ingest external source ${args.id}:`, error); | ||
| throw new Error( | ||
| `Failed to re-ingest source: ${error instanceof Error ? error.message : 'Unknown error'}` | ||
| ); | ||
| } | ||
| }, | ||
| }) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...prisma/migrations/20251130081107_make_external_source_conversation_optional/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| -- DropForeignKey | ||
| ALTER TABLE "ExternalSource" DROP CONSTRAINT "ExternalSource_conversationId_fkey"; | ||
|
|
||
| -- AlterTable | ||
| ALTER TABLE "ExternalSource" ALTER COLUMN "conversationId" DROP NOT NULL; | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "ExternalSource_conversationId_idx" ON "ExternalSource"("conversationId"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "ExternalSource_type_idx" ON "ExternalSource"("type"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "ExternalSource_createdAt_idx" ON "ExternalSource"("createdAt"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "ExternalSource" ADD CONSTRAINT "ExternalSource_conversationId_fkey" FOREIGN KEY ("conversationId") REFERENCES "Conversation"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
carterax marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.