Explorar el Código

feat: add endpoint to clear pending embeddings

Added functionality to clean up old pending embeddings that were created
before the content change detection fix.

Changes:
- Added Database.clearPendingEmbeddings() method to delete pending embeddings
- Added DELETE /api/shops/:shopId/qdrant/pending-embeddings endpoint
- Supports both internal shop ID and custom ID for lookups

This allows cleaning up the 27 pending embeddings from the old buggy code
and starting fresh with the fixed embedding system.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh hace 8 meses
padre
commit
38df10625a
Se han modificado 2 ficheros con 62 adiciones y 0 borrados
  1. 51 0
      src/api/components/QdrantEndpoint.ts
  2. 11 0
      src/database/Database.ts

+ 51 - 0
src/api/components/QdrantEndpoint.ts

@@ -563,6 +563,30 @@ export class QdrantEndpoint extends BaseEndpointComponent {
             '200': { description: 'Errors cleared' }
             '200': { description: 'Errors cleared' }
           }
           }
         }
         }
+      ),
+
+      this.createEndpoint(
+        'DELETE',
+        '/api/shops/:shopId/qdrant/pending-embeddings',
+        this.clearPendingEmbeddings.bind(this),
+        {
+          summary: 'Clear pending embeddings',
+          description: 'Clear all pending embeddings for a shop',
+          tags: ['Qdrant'],
+          requiresAuth: true,
+          parameters: [
+            {
+              name: 'shopId',
+              in: 'path',
+              type: 'string',
+              required: true,
+              description: 'Shop ID'
+            }
+          ],
+          responses: {
+            '200': { description: 'Pending embeddings cleared' }
+          }
+        }
       )
       )
     ];
     ];
   }
   }
@@ -990,6 +1014,33 @@ export class QdrantEndpoint extends BaseEndpointComponent {
     }
     }
   }
   }
 
 
+  private async clearPendingEmbeddings(req: Request, res: Response): Promise<void> {
+    try {
+      const { shopId } = req.params;
+
+      // Get shop from database - try both internal ID and custom ID
+      let shop = this.db.getShopById(shopId);
+      if (!shop) {
+        shop = this.db.getShopByCustomId(shopId);
+      }
+      if (!shop) {
+        res.status(404).json({ error: 'Shop not found' });
+        return;
+      }
+
+      const deletedCount = this.db.clearPendingEmbeddings(shop.id);
+
+      res.json({
+        message: `Cleared ${deletedCount} pending embeddings for shop ${shop.id}`,
+        deleted_count: deletedCount,
+        success: true
+      });
+    } catch (error) {
+      logger.error('Failed to clear pending embeddings:', error);
+      res.status(500).json({ error: 'Internal server error' });
+    }
+  }
+
   private async triggerCleanup(req: Request, res: Response): Promise<void> {
   private async triggerCleanup(req: Request, res: Response): Promise<void> {
     try {
     try {
       // Trigger manual cleanup using the scheduler service
       // Trigger manual cleanup using the scheduler service

+ 11 - 0
src/database/Database.ts

@@ -1435,6 +1435,17 @@ export class ShopDatabase {
     `).run(collectionName);
     `).run(collectionName);
   }
   }
 
 
+  clearPendingEmbeddings(shopId: string): number {
+    const result = this.db.prepare(`
+      DELETE FROM shop_embeddings
+      WHERE shop_content_id IN (
+        SELECT id FROM shop_content WHERE shop_id = ?
+      )
+      AND embedding_status = 'pending'
+    `).run(shopId);
+    return result.changes;
+  }
+
   // MCP Analytics Methods
   // MCP Analytics Methods
 
 
   /**
   /**