diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index d8441a3..a38b6f2 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -27,7 +27,10 @@ env: jobs: build-and-push: - if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' + # Fixed conditional logic to handle both push and PR events properly + if: | + (github.event_name == 'push' && github.ref == 'refs/heads/main') || + (github.event_name == 'pull_request' && github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main') runs-on: ubuntu-latest permissions: contents: read @@ -72,7 +75,6 @@ jobs: with: images: ${{ env.DOCKER_IMAGE }} tags: | - type=raw,value=latest,enable={{is_default_branch}} type=raw,value=latest,enable={{is_default_branch}} type=ref,event=branch type=ref,event=pr @@ -82,7 +84,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . - push: ${{ github.event_name != 'pull_request' }} + push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: | @@ -107,25 +109,47 @@ jobs: - name: Validate Coolify variables env: - COOLIFY_URL: ${{env.COOLIFY_URL}} + COOLIFY_URL: ${{ env.COOLIFY_URL }} run: | echo "Validating that coolify_url doesn't end in '/'" echo "$COOLIFY_URL" | grep -P '[^/]$' + echo "Validating required environment variables are set" + if [ -z "$COOLIFY_URL" ] || [ -z "${{ env.COOLIFY_RESOURCE_ID }}" ] || [ -z "${{ env.COOLIFY_API_TOKEN }}" ]; then + echo "Error: Missing required Coolify environment variables" + exit 1 + fi - name: Update source commit SHA in Coolify uses: fjogeleit/http-request-action@v1 with: - url: ${{env.COOLIFY_URL}}/api/v1/applications/${{env.COOLIFY_RESOURCE_ID}} + url: ${{ env.COOLIFY_URL }}/api/v1/applications/${{ env.COOLIFY_RESOURCE_ID }} method: PATCH - bearerToken: ${{env.COOLIFY_API_TOKEN}} + bearerToken: ${{ env.COOLIFY_API_TOKEN }} data: >- { - "git_commit_sha": "${{github.sha}}" + "git_commit_sha": "${{ github.sha }}" } + continue-on-error: false - name: Trigger Coolify deployment via webhook uses: fjogeleit/http-request-action@v1 with: - url: ${{env.COOLIFY_URL}}/api/v1/deploy?uuid=${{env.COOLIFY_RESOURCE_ID}}&force=false + url: ${{ env.COOLIFY_URL }}/api/v1/deploy?uuid=${{ env.COOLIFY_RESOURCE_ID }}&force=false method: GET - bearerToken: ${{env.COOLIFY_API_TOKEN}} + bearerToken: ${{ env.COOLIFY_API_TOKEN }} + continue-on-error: false + + - name: Deployment status notification + if: success() + run: | + echo "✅ Docker image built and pushed successfully" + echo "✅ Coolify deployment triggered successfully" + echo "📦 Image: ${{ env.DOCKER_IMAGE }}" + echo "🏷️ Tags: ${{ steps.meta.outputs.tags }}" + echo "🔗 Commit SHA: ${{ github.sha }}" + + - name: Failure notification + if: failure() + run: | + echo "❌ Workflow failed" + echo "🔍 Please check the logs above for details" diff --git a/.gitignore b/.gitignore index 06e47ed..b9b4742 100644 --- a/.gitignore +++ b/.gitignore @@ -88,4 +88,5 @@ Thumbs.db *.sqlite3 media/ static/ -uploads/ \ No newline at end of file +uploads/ +reset_db.py diff --git a/database.py b/database.py index 2d86e10..0bfdbb7 100644 --- a/database.py +++ b/database.py @@ -76,13 +76,19 @@ async def add_conversation(self, user_id: str, message: str, response: str, raise async def check_repetition(self, user_id: str, new_message_embedding: List[float], - threshold: float = 0.8) -> Tuple[bool, Optional[Dict]]: - """Check if the message is similar to previous conversations""" + current_timestamp: datetime, threshold: float = 0.8, + time_window_seconds: int = 30) -> Tuple[bool, Optional[Dict]]: + """Check if the message is similar to previous conversations within a time window""" try: async with self.async_session() as session: - # Get recent conversations + time_threshold = current_timestamp - timedelta(seconds=time_window_seconds) + + # Get recent conversations within the time window query = select(Conversation).where( - Conversation.user_id == user_id + and_( + Conversation.user_id == user_id, + Conversation.timestamp >= time_threshold + ) ).order_by(Conversation.timestamp.desc()).limit(10) result = await session.execute(query) diff --git a/main.py b/main.py index 1b9fc77..16b3485 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import logging import os from concurrent.futures import ThreadPoolExecutor -from datetime import datetime +from datetime import datetime, timedelta from typing import Dict, List, Optional, Tuple import aiohttp @@ -248,6 +248,7 @@ async def process_message(self, user_id: str, message: str, language: str) -> Di is_repetition, similar_conv = await db.check_repetition( user_id, message_embedding.tolist(), + datetime.now(), threshold=0.8 ) except Exception as e: