From 02f8b000493d793009b30bdf1082c56ce13029d4 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 17:59:37 +0100 Subject: [PATCH 01/24] added the github workflows to build the images --- .github/workflows/docker-nightly.yml | 42 +++++++++++++++++++++++++ .github/workflows/docker-release.yml | 47 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 .github/workflows/docker-nightly.yml create mode 100644 .github/workflows/docker-release.yml diff --git a/.github/workflows/docker-nightly.yml b/.github/workflows/docker-nightly.yml new file mode 100644 index 0000000..6216802 --- /dev/null +++ b/.github/workflows/docker-nightly.yml @@ -0,0 +1,42 @@ +name: Docker Nightly Build + +on: + push: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=raw,value=nightly,enable={{is_default_branch}} + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml new file mode 100644 index 0000000..2522f0f --- /dev/null +++ b/.github/workflows/docker-release.yml @@ -0,0 +1,47 @@ +name: Docker Release Build + +on: + push: + tags: + - 'v*-alpha' + - 'v*' + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=semver,pattern={{version}}-alpha,enable={{starts_with(github.ref, 'refs/tags/v') && ends_with(github.ref, '-alpha')}} + type=raw,value=alpha,enable={{starts_with(github.ref, 'refs/tags/v') && ends_with(github.ref, '-alpha')}} + type=semver,pattern={{version}},enable={{starts_with(github.ref, 'refs/tags/v') && !ends_with(github.ref, '-alpha')}} + type=raw,value=latest,enable={{starts_with(github.ref, 'refs/tags/v') && !ends_with(github.ref, '-alpha')}} + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file From c5cc08e52f6bccae43f81a0be0c535739fac03fe Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 17:59:54 +0100 Subject: [PATCH 02/24] added helpful scripts to easily trigger the build workflows --- scripts/alpha-release.sh | 21 +++++++++++++++++++++ scripts/stable-release.sh | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 scripts/alpha-release.sh create mode 100755 scripts/stable-release.sh diff --git a/scripts/alpha-release.sh b/scripts/alpha-release.sh new file mode 100755 index 0000000..b8806e9 --- /dev/null +++ b/scripts/alpha-release.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +if [ -z "$1" ]; then + echo "Usage: $0 " + echo "Example: $0 1.0.0" + exit 1 +fi + +VERSION=$1 + +# Validate version format (semantic versioning) +if ! [[ $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "Error: Version must follow semantic versioning (e.g., 1.0.0)" + exit 1 +fi + +# Create and push the tag +git tag -a "v$VERSION-alpha" -m "Alpha release v$VERSION" +git push origin "v$VERSION-alpha" + +echo "Created and pushed alpha release tag v$VERSION-alpha" \ No newline at end of file diff --git a/scripts/stable-release.sh b/scripts/stable-release.sh new file mode 100755 index 0000000..f53f2ab --- /dev/null +++ b/scripts/stable-release.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +if [ -z "$1" ]; then + echo "Usage: $0 " + echo "Example: $0 1.0.0" + exit 1 +fi + +VERSION=$1 + +# Validate version format (semantic versioning) +if ! [[ $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "Error: Version must follow semantic versioning (e.g., 1.0.0)" + exit 1 +fi + +# Create and push the tag +git tag -a "v$VERSION" -m "Release v$VERSION" +git push origin "v$VERSION" + +echo "Created and pushed stable release tag v$VERSION" \ No newline at end of file From 3751a0ecd6584b8832a6b7951451281b4662857a Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:04:09 +0100 Subject: [PATCH 03/24] readme adding easier docker usage. --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 7cb12d3..eadd9c3 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,45 @@ We are live on ProductHunt today, please upvote us if you find this useful! 🙏 For detailed installation instructions, please refer to our [Installation Guide](https://www.cursorlens.com/docs/getting-started/installation). + +### Using Docker + +CursorLens provides different images for various release types: + +- `nightly`: Latest development build (updated on every push to main) +- `alpha`: Pre-release versions (tagged as vX.Y.Z-alpha) +- `stable`: Production-ready releases (tagged as vX.Y.Z) + +```yaml +services: + cursorlens: + image: ghcr.io/haouarihk/cursorlens:latest + ports: + - "3000:3000" + environment: + - DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres + depends_on: + db: + condition: service_healthy + + db: + image: postgres:14 + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=postgres + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + +volumes: + postgres_data: +``` + ### Prerequisites - Node.js (v14 or later) @@ -143,6 +182,7 @@ If you encounter any issues or have questions, please file an issue on the GitHu For more detailed information, please visit our [documentation](https://www.cursorlens.com/docs/project/introduction). + --- Happy coding with Cursor Lens! From b733075867702647b1bb77a9c0b83c73fdbbc1db Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:16:53 +0100 Subject: [PATCH 04/24] adding support for multiple platforms --- .github/workflows/docker-nightly.yml | 27 +++++++++++++++++++------ .github/workflows/docker-release.yml | 30 +++++++++++++++++++++------- run.Dockerfile | 24 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 run.Dockerfile diff --git a/.github/workflows/docker-nightly.yml b/.github/workflows/docker-nightly.yml index 6216802..0b27404 100644 --- a/.github/workflows/docker-nightly.yml +++ b/.github/workflows/docker-nightly.yml @@ -12,12 +12,6 @@ jobs: packages: write steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - name: Login to GitHub Container Registry uses: docker/login-action@v2 with: @@ -25,6 +19,26 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '23' + + - name: Install pnpm + run: npm install -g pnpm + + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install dependencies + run: pnpm install + + - name: Build the project + run: pnpm run build + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Extract metadata for Docker id: meta uses: docker/metadata-action@v4 @@ -37,6 +51,7 @@ jobs: uses: docker/build-push-action@v4 with: context: . + file: run.Dockerfile push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 2522f0f..1a8afb3 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -14,12 +14,6 @@ jobs: packages: write steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - name: Login to GitHub Container Registry uses: docker/login-action@v2 with: @@ -27,6 +21,26 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '23' + + - name: Install pnpm + run: npm install -g pnpm + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install dependencies + run: pnpm install + + - name: Build the project + run: pnpm run build + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Extract metadata for Docker id: meta uses: docker/metadata-action@v4 @@ -42,6 +56,8 @@ jobs: uses: docker/build-push-action@v4 with: context: . + file: run.Dockerfile push: true tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 \ No newline at end of file diff --git a/run.Dockerfile b/run.Dockerfile new file mode 100644 index 0000000..83c6d0e --- /dev/null +++ b/run.Dockerfile @@ -0,0 +1,24 @@ +# This docker image does not build the application, it just runs it + +FROM node:18-alpine + +WORKDIR /app + +RUN apk update +RUN apk add --no-cache libc6-compat + +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +USER nextjs + +COPY --chown=nextjs:nodejs ./public ./public +COPY --chown=nextjs:nodejs ./.next/standalone ./ +COPY --chown=nextjs:nodejs ./.next/static ./.next/static + +RUN chmod -R 777 .next + +CMD ["node", "server.js"] \ No newline at end of file From d455fa6ac490f5a2c4da41a08ccd6d24d5ad5a97 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:41:54 +0100 Subject: [PATCH 05/24] added bulid enviroment variables --- .env.build | 8 ++++++++ .github/workflows/docker-nightly.yml | 3 +++ .github/workflows/docker-release.yml | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 .env.build diff --git a/.env.build b/.env.build new file mode 100644 index 0000000..38735a6 --- /dev/null +++ b/.env.build @@ -0,0 +1,8 @@ +OPENAI_API_KEY= +ANTHROPIC_API_KEY= +COHERE_API_KEY= +MISTRAL_API_KEY= +GROQ_API_KEY= + +# Dummy database URL +DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres \ No newline at end of file diff --git a/.github/workflows/docker-nightly.yml b/.github/workflows/docker-nightly.yml index 0b27404..ea2c077 100644 --- a/.github/workflows/docker-nightly.yml +++ b/.github/workflows/docker-nightly.yml @@ -29,6 +29,9 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 + + - name: Use .env.build instead of .env + run: cp .env.build .env - name: Install dependencies run: pnpm install diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 1a8afb3..0daa8b0 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -32,6 +32,9 @@ jobs: - name: Checkout code uses: actions/checkout@v3 + - name: Use .env.build instead of .env + run: cp .env.build .env + - name: Install dependencies run: pnpm install From 09a13322471f13936f035feb1f015527ccd16121 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:43:11 +0100 Subject: [PATCH 06/24] use @/lib/prisma instead of reinitializing prisma client. --- scripts/update-log-costs.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/update-log-costs.ts b/scripts/update-log-costs.ts index 490f3f1..bc2084e 100644 --- a/scripts/update-log-costs.ts +++ b/scripts/update-log-costs.ts @@ -1,7 +1,6 @@ -import { PrismaClient } from "@prisma/client"; +import prisma from "@/lib/prisma"; import { getModelCost } from "../src/lib/cost-calculator"; -const prisma = new PrismaClient(); async function updateLogCosts() { const logs = await prisma.log.findMany(); From 8386d8100f84332ca4a5170d568da584b02c95eb Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:43:24 +0100 Subject: [PATCH 07/24] fixed a logic bug here --- src/lib/cost-calculator.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/cost-calculator.ts b/src/lib/cost-calculator.ts index b9b129b..8cad294 100644 --- a/src/lib/cost-calculator.ts +++ b/src/lib/cost-calculator.ts @@ -1,6 +1,4 @@ -import { PrismaClient } from "@prisma/client"; - -const prisma = new PrismaClient(); +import prisma from "@/lib/prisma"; export async function getModelCost(provider: string, model: string) { const currentDate = new Date(); @@ -8,8 +6,10 @@ export async function getModelCost(provider: string, model: string) { where: { provider, model, - OR: [{ validFrom: null }, { validFrom: { lte: currentDate } }], - OR: [{ validTo: null }, { validTo: { gte: currentDate } }], + AND: [ + { OR: [{ validFrom: undefined }, { validFrom: { lte: currentDate } }] }, + { OR: [{ validTo: undefined }, { validTo: { gte: currentDate } }] }, + ], }, orderBy: { validFrom: "desc" }, }); From fd265b4df8079ffe01e4f748607fd1cd4b705540 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:46:09 +0100 Subject: [PATCH 08/24] added postinstall for prisma to generate types --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1876a3b..44c9c47 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "test:coverage": "vitest run --coverage", "test:ui": "vitest --ui", "seed": "tsx prisma/seed.ts", - "update-log-costs": "tsx scripts/update-log-costs.ts" + "update-log-costs": "tsx scripts/update-log-costs.ts", + "postinstall": "npx prisma generate" }, "prisma": { "seed": "tsx prisma/seed.ts" From 4f2caac1f596fe8405efbcf04603dcdbbef24701 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 18:50:02 +0100 Subject: [PATCH 09/24] build a standalone app --- .env.build | 5 ++++- next.config.js | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.env.build b/.env.build index 38735a6..644397f 100644 --- a/.env.build +++ b/.env.build @@ -5,4 +5,7 @@ MISTRAL_API_KEY= GROQ_API_KEY= # Dummy database URL -DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres \ No newline at end of file +DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres + +# Build standalone +BUILD_STANDALONE=true \ No newline at end of file diff --git a/next.config.js b/next.config.js index e0b5fdf..0f8d6ec 100644 --- a/next.config.js +++ b/next.config.js @@ -1,8 +1,11 @@ +const IsStandalone = process.env.BUILD_STANDALONE == "true"; + /** @type {import('next').NextConfig} */ const nextConfig = { typescript: { ignoreBuildErrors: true, }, + output: IsStandalone ? "standalone" : undefined, }; module.exports = nextConfig; From 9e85db8acc59a454a687a7ca0d439e5c6668af52 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 19:14:45 +0100 Subject: [PATCH 10/24] fixed bug with libssl1.1 not found --- Dockerfile | 8 +++++--- README.md | 30 +++++++++++++++++++++--------- prisma/schema.prisma | 2 +- run.Dockerfile | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index c8d3d0c..e075607 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,8 @@ FROM node:18-alpine WORKDIR /app -RUN apk add --no-cache libc6-compat RUN apk update +RUN apk add --no-cache libc6-compat openssl # Install pnpm RUN npm install -g pnpm @@ -11,12 +11,14 @@ RUN npm install -g pnpm # Copy package.json and pnpm-lock.yaml COPY package.json pnpm-lock.yaml ./ -# Install dependencies -RUN pnpm install +RUN pnpm fetch # Copy the rest of the application COPY . . +# Install dependencies +RUN pnpm install + # Generate Prisma Client RUN pnpm prisma generate diff --git a/README.md b/README.md index eadd9c3..cbc35e5 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,18 @@ We are live on ProductHunt today, please upvote us if you find this useful! 🙏 - **AI Library**: Vercel AI SDK - **Styling**: Tailwind CSS with shadcn/ui components -## Getting Started - -For detailed installation instructions, please refer to our [Installation Guide](https://www.cursorlens.com/docs/getting-started/installation). -### Using Docker +## Getting Started (Using Docker Compose) -CursorLens provides different images for various release types: - -- `nightly`: Latest development build (updated on every push to main) -- `alpha`: Pre-release versions (tagged as vX.Y.Z-alpha) -- `stable`: Production-ready releases (tagged as vX.Y.Z) +1. Create a new file named `docker-compose.yaml` in your project directory +2. Copy and paste the YAML configuration below into the file +3. Open a terminal in the project directory +4. Run the command: + ```bash + docker compose up + ``` +(Optional: Add `-d` flag to run in detached mode) ```yaml services: @@ -46,6 +46,7 @@ services: - "3000:3000" environment: - DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres + # restart: unless-stopped depends_on: db: condition: service_healthy @@ -58,6 +59,7 @@ services: - POSTGRES_DB=postgres volumes: - postgres_data:/var/lib/postgresql/data + # restart: unless-stopped healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s @@ -67,6 +69,16 @@ services: volumes: postgres_data: ``` +CursorLens provides different images for various release types: + +- `nightly`: Latest development build +- `alpha`: Pre-release versions +- `stable`: Production-ready releases + + +## Getting Started (From Source) + +For detailed installation instructions, please refer to our [Installation Guide](https://www.cursorlens.com/docs/getting-started/installation). ### Prerequisites diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0c94760..91b9978 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -3,7 +3,7 @@ generator client { provider = "prisma-client-js" - binaryTargets = ["native", "linux-musl-openssl-3.0.x"] + binaryTargets = ["native", "linux-musl"] } datasource db { diff --git a/run.Dockerfile b/run.Dockerfile index 83c6d0e..b2464b6 100644 --- a/run.Dockerfile +++ b/run.Dockerfile @@ -5,7 +5,7 @@ FROM node:18-alpine WORKDIR /app RUN apk update -RUN apk add --no-cache libc6-compat +RUN apk add --no-cache libc6-compat openssl ENV NODE_ENV=production ENV NEXT_TELEMETRY_DISABLED=1 From d7368c40dcd2e93b0f472ae17e696c967fb73dbd Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 19:21:58 +0100 Subject: [PATCH 11/24] updated releases action --- .github/workflows/docker-release.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 0daa8b0..11f5cb0 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -50,10 +50,10 @@ jobs: with: images: ghcr.io/${{ github.repository }} tags: | - type=semver,pattern={{version}}-alpha,enable={{starts_with(github.ref, 'refs/tags/v') && ends_with(github.ref, '-alpha')}} - type=raw,value=alpha,enable={{starts_with(github.ref, 'refs/tags/v') && ends_with(github.ref, '-alpha')}} - type=semver,pattern={{version}},enable={{starts_with(github.ref, 'refs/tags/v') && !ends_with(github.ref, '-alpha')}} - type=raw,value=latest,enable={{starts_with(github.ref, 'refs/tags/v') && !ends_with(github.ref, '-alpha')}} + type=semver,pattern={{version}}-alpha,enable={{startsWith(github.ref, 'refs/tags/v') && endsWith(github.ref, '-alpha')}} + type=raw,value=alpha,enable={{startsWith(github.ref, 'refs/tags/v') && endsWith(github.ref, '-alpha')}} + type=semver,pattern={{version}},enable={{startsWith(github.ref, 'refs/tags/v') && !endsWith(github.ref, '-alpha')}} + type=raw,value=latest,enable={{startsWith(github.ref, 'refs/tags/v') && !endsWith(github.ref, '-alpha')}} - name: Build and push uses: docker/build-push-action@v4 From 63077fe56dd6a3563dbf234af7c67efb61b0298d Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 19:31:46 +0100 Subject: [PATCH 12/24] fixed bug with docker tags retrival --- .github/workflows/docker-release.yml | 21 ++++------ scripts/_github_action_tags_extractor.sh | 52 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 scripts/_github_action_tags_extractor.sh diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 11f5cb0..21c8649 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -31,10 +31,15 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - + - name: Use .env.build instead of .env run: cp .env.build .env + - name: Extract Docker tags + run: | + chmod +x ./scripts/_github_action_tags_extractor.sh + ./scripts/_github_action_tags_extractor.sh "${{ github.ref_name }}" + - name: Install dependencies run: pnpm install @@ -44,23 +49,11 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Extract metadata for Docker - id: meta - uses: docker/metadata-action@v4 - with: - images: ghcr.io/${{ github.repository }} - tags: | - type=semver,pattern={{version}}-alpha,enable={{startsWith(github.ref, 'refs/tags/v') && endsWith(github.ref, '-alpha')}} - type=raw,value=alpha,enable={{startsWith(github.ref, 'refs/tags/v') && endsWith(github.ref, '-alpha')}} - type=semver,pattern={{version}},enable={{startsWith(github.ref, 'refs/tags/v') && !endsWith(github.ref, '-alpha')}} - type=raw,value=latest,enable={{startsWith(github.ref, 'refs/tags/v') && !endsWith(github.ref, '-alpha')}} - - name: Build and push uses: docker/build-push-action@v4 with: context: . file: run.Dockerfile push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + tags: ghcr.io/${{ github.repository }}:${{ env.DOCKER_TAGS }} platforms: linux/amd64,linux/arm64 \ No newline at end of file diff --git a/scripts/_github_action_tags_extractor.sh b/scripts/_github_action_tags_extractor.sh new file mode 100644 index 0000000..e5bf176 --- /dev/null +++ b/scripts/_github_action_tags_extractor.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Exit on error +set -e + +# Check if tag name is provided +if [ -z "$1" ]; then + echo "Error: Tag name is required" + echo "Usage: $0 " + exit 1 +fi + +# Function to extract version from tag +extract_version() { + local tag=$1 + # Remove 'v' prefix and any suffix after '-' + echo "$tag" | sed -E 's/^v//' | sed -E 's/-.*$//' +} + +# Function to check if tag is alpha +is_alpha() { + local tag=$1 + [[ "$tag" == *"-alpha" ]] +} + +TAG_NAME=$1 + +# Extract version +VERSION=$(extract_version "$TAG_NAME") + +# Initialize tags array +TAGS=() + +# Add appropriate tags based on tag type +if is_alpha "$TAG_NAME"; then + # For alpha releases + TAGS+=("$VERSION-alpha") + TAGS+=("alpha") +else + # For regular releases + TAGS+=("$VERSION") + TAGS+=("latest") +fi + +# Convert array to comma-separated string +TAGS_STRING=$(IFS=,; echo "${TAGS[*]}") + +# Export the tags as environment variable +echo "DOCKER_TAGS=$TAGS_STRING" >> $GITHUB_ENV + +# Print for debugging +echo "Extracted tags: $TAGS_STRING" \ No newline at end of file From 9fb71c705e5f81f8fe04bf47bf5485e3ee7badcc Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 19:36:31 +0100 Subject: [PATCH 13/24] fixed bug repository name must be lowercase --- .github/workflows/docker-nightly.yml | 2 +- .github/workflows/docker-release.yml | 2 +- scripts/_github_action_tags_extractor.sh | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-nightly.yml b/.github/workflows/docker-nightly.yml index ea2c077..267a49e 100644 --- a/.github/workflows/docker-nightly.yml +++ b/.github/workflows/docker-nightly.yml @@ -48,7 +48,7 @@ jobs: with: images: ghcr.io/${{ github.repository }} tags: | - type=raw,value=nightly,enable={{is_default_branch}} + type=raw,value=nightly - name: Build and push uses: docker/build-push-action@v4 diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 21c8649..965c699 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -55,5 +55,5 @@ jobs: context: . file: run.Dockerfile push: true - tags: ghcr.io/${{ github.repository }}:${{ env.DOCKER_TAGS }} + tags: ${{ env.DOCKER_TAGS }} platforms: linux/amd64,linux/arm64 \ No newline at end of file diff --git a/scripts/_github_action_tags_extractor.sh b/scripts/_github_action_tags_extractor.sh index e5bf176..130fd87 100644 --- a/scripts/_github_action_tags_extractor.sh +++ b/scripts/_github_action_tags_extractor.sh @@ -45,8 +45,11 @@ fi # Convert array to comma-separated string TAGS_STRING=$(IFS=,; echo "${TAGS[*]}") +# Convert repository name to lowercase +REPO_NAME=$(echo "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]') + # Export the tags as environment variable -echo "DOCKER_TAGS=$TAGS_STRING" >> $GITHUB_ENV +echo "DOCKER_TAGS=ghcr.io/$REPO_NAME:$TAGS_STRING" >> $GITHUB_ENV # Print for debugging echo "Extracted tags: $TAGS_STRING" \ No newline at end of file From e67998eb50f195ac60e43738fcf98b0f6acf8339 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 19:43:42 +0100 Subject: [PATCH 14/24] why is this bug taking me soo long to fix --- scripts/_github_action_tags_extractor.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/_github_action_tags_extractor.sh b/scripts/_github_action_tags_extractor.sh index 130fd87..ceb7b9f 100644 --- a/scripts/_github_action_tags_extractor.sh +++ b/scripts/_github_action_tags_extractor.sh @@ -28,28 +28,28 @@ TAG_NAME=$1 # Extract version VERSION=$(extract_version "$TAG_NAME") +# Convert repository name to lowercase +REPO_NAME=$(echo "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]') + # Initialize tags array TAGS=() # Add appropriate tags based on tag type if is_alpha "$TAG_NAME"; then # For alpha releases - TAGS+=("$VERSION-alpha") - TAGS+=("alpha") + TAGS+=("ghcr.io/$REPO_NAME:$VERSION-alpha") + TAGS+=("ghcr.io/$REPO_NAME:alpha") else # For regular releases - TAGS+=("$VERSION") - TAGS+=("latest") + TAGS+=("ghcr.io/$REPO_NAME:$VERSION") + TAGS+=("ghcr.io/$REPO_NAME:latest") fi # Convert array to comma-separated string TAGS_STRING=$(IFS=,; echo "${TAGS[*]}") -# Convert repository name to lowercase -REPO_NAME=$(echo "$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]') - # Export the tags as environment variable -echo "DOCKER_TAGS=ghcr.io/$REPO_NAME:$TAGS_STRING" >> $GITHUB_ENV +# echo "DOCKER_TAGS=$TAGS_STRING" >> $GITHUB_ENV # Print for debugging -echo "Extracted tags: $TAGS_STRING" \ No newline at end of file +echo "Extracted tags: $TAGS_STRING" \ No newline at end of file From 957901e4876c17e7dbcdcced754bfb1dce280f56 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 19:45:13 +0100 Subject: [PATCH 15/24] forgot to uncomment this --- scripts/_github_action_tags_extractor.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/_github_action_tags_extractor.sh b/scripts/_github_action_tags_extractor.sh index ceb7b9f..1e98e21 100644 --- a/scripts/_github_action_tags_extractor.sh +++ b/scripts/_github_action_tags_extractor.sh @@ -49,7 +49,7 @@ fi TAGS_STRING=$(IFS=,; echo "${TAGS[*]}") # Export the tags as environment variable -# echo "DOCKER_TAGS=$TAGS_STRING" >> $GITHUB_ENV +echo "DOCKER_TAGS=$TAGS_STRING" >> $GITHUB_ENV # Print for debugging echo "Extracted tags: $TAGS_STRING" \ No newline at end of file From a80a6b851631c5c3fbf0480a845f35319274bec4 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 20:22:41 +0100 Subject: [PATCH 16/24] reverting this change --- prisma/schema.prisma | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 91b9978..0c94760 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -3,7 +3,7 @@ generator client { provider = "prisma-client-js" - binaryTargets = ["native", "linux-musl"] + binaryTargets = ["native", "linux-musl-openssl-3.0.x"] } datasource db { From 35700a17c03e5b895d3b9899ae8cd151976fd5c0 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 20:30:31 +0100 Subject: [PATCH 17/24] added prisma files for a db push workaround --- run.Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/run.Dockerfile b/run.Dockerfile index b2464b6..a0494e3 100644 --- a/run.Dockerfile +++ b/run.Dockerfile @@ -19,6 +19,9 @@ COPY --chown=nextjs:nodejs ./public ./public COPY --chown=nextjs:nodejs ./.next/standalone ./ COPY --chown=nextjs:nodejs ./.next/static ./.next/static +# Copy the Prisma schema. +COPY --chown=nextjs:nodejs ./prisma ./prisma + RUN chmod -R 777 .next CMD ["node", "server.js"] \ No newline at end of file From 846aa15aee375313d604067b2a0488973ed33b42 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 20:38:42 +0100 Subject: [PATCH 18/24] added prisma migration (for the run.dockerfile) --- run.Dockerfile | 7 +++++-- scripts/_docker_run.sh | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 scripts/_docker_run.sh diff --git a/run.Dockerfile b/run.Dockerfile index a0494e3..1003345 100644 --- a/run.Dockerfile +++ b/run.Dockerfile @@ -18,10 +18,13 @@ USER nextjs COPY --chown=nextjs:nodejs ./public ./public COPY --chown=nextjs:nodejs ./.next/standalone ./ COPY --chown=nextjs:nodejs ./.next/static ./.next/static +COPY --chown=nextjs:nodejs ./scripts ./scripts +RUN chmod -R 777 .next # Copy the Prisma schema. COPY --chown=nextjs:nodejs ./prisma ./prisma -RUN chmod -R 777 .next +RUN npm install -g prisma + -CMD ["node", "server.js"] \ No newline at end of file +CMD ["sh", "scripts/_docker_run.sh"] \ No newline at end of file diff --git a/scripts/_docker_run.sh b/scripts/_docker_run.sh new file mode 100644 index 0000000..a26ee0a --- /dev/null +++ b/scripts/_docker_run.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Run migrations +npx prisma migrate deploy + +# Seed the database +npx prisma db seed + +# Start the application +node server.js \ No newline at end of file From adc0fcbcd45b7ceb8377377e955a052fe8daae44 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 20:41:28 +0100 Subject: [PATCH 19/24] npm install -g prisma before switching users --- run.Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/run.Dockerfile b/run.Dockerfile index 1003345..96b1ffd 100644 --- a/run.Dockerfile +++ b/run.Dockerfile @@ -13,6 +13,8 @@ ENV NEXT_TELEMETRY_DISABLED=1 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs +RUN npm install -g prisma + USER nextjs COPY --chown=nextjs:nodejs ./public ./public @@ -24,7 +26,7 @@ RUN chmod -R 777 .next # Copy the Prisma schema. COPY --chown=nextjs:nodejs ./prisma ./prisma -RUN npm install -g prisma + CMD ["sh", "scripts/_docker_run.sh"] \ No newline at end of file From 48947e94cd489960810604cb5cfbff567f9f39b8 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 20:58:58 +0100 Subject: [PATCH 20/24] added OLLAMA_BASE_URL env var --- .env.build | 2 ++ .env.example | 2 ++ src/app/[...openai]/route.ts | 6 +++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.env.build b/.env.build index 644397f..43f54ff 100644 --- a/.env.build +++ b/.env.build @@ -4,6 +4,8 @@ COHERE_API_KEY= MISTRAL_API_KEY= GROQ_API_KEY= +OLLAMA_BASE_URL= + # Dummy database URL DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres diff --git a/.env.example b/.env.example index 85c59e3..b228755 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,6 @@ COHERE_API_KEY=your_cohere_api_key_here MISTRAL_API_KEY=your_mistral_api_key_here GROQ_API_KEY=your_groq_api_key_here +OLLAMA_BASE_URL= + DATABASE_URL= diff --git a/src/app/[...openai]/route.ts b/src/app/[...openai]/route.ts index 2c023f7..e2e2c7b 100644 --- a/src/app/[...openai]/route.ts +++ b/src/app/[...openai]/route.ts @@ -53,7 +53,11 @@ async function getAIModelClient(provider: string, model: string) { return groqClient(model); } case "ollama": - return ollama("llama3.1"); + const olm = ollama(model || "llama3.1"); + if (process.env.OLLAMA_BASE_URL) { + olm.config.baseURL = process.env.OLLAMA_BASE_URL; + } + return olm; case "google-vertex": throw new Error("Google Vertex AI is not currently supported"); default: From 9571fb6331f644a423afe7f9ca400ddbd7368155 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 21:49:06 +0100 Subject: [PATCH 21/24] reverting this --- src/lib/cost-calculator.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/cost-calculator.ts b/src/lib/cost-calculator.ts index 8cad294..0c64bab 100644 --- a/src/lib/cost-calculator.ts +++ b/src/lib/cost-calculator.ts @@ -7,8 +7,10 @@ export async function getModelCost(provider: string, model: string) { provider, model, AND: [ - { OR: [{ validFrom: undefined }, { validFrom: { lte: currentDate } }] }, - { OR: [{ validTo: undefined }, { validTo: { gte: currentDate } }] }, + // @ts-ignore + { OR: [{ validFrom: null }, { validFrom: { lte: currentDate } }] }, + // @ts-ignore + { OR: [{ validTo: null }, { validTo: { gte: currentDate } }] }, ], }, orderBy: { validFrom: "desc" }, From 02e7939c20968671078e4995000b1aca2d970904 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 21:55:45 +0100 Subject: [PATCH 22/24] making validFrom optional --- prisma/schema.prisma | 2 +- src/lib/cost-calculator.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0c94760..1a93826 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -46,7 +46,7 @@ model ModelCost { model String inputTokenCost Float outputTokenCost Float - validFrom DateTime + validFrom DateTime? validTo DateTime? @@unique([provider, model, validFrom]) diff --git a/src/lib/cost-calculator.ts b/src/lib/cost-calculator.ts index 0c64bab..dc877b3 100644 --- a/src/lib/cost-calculator.ts +++ b/src/lib/cost-calculator.ts @@ -7,9 +7,7 @@ export async function getModelCost(provider: string, model: string) { provider, model, AND: [ - // @ts-ignore { OR: [{ validFrom: null }, { validFrom: { lte: currentDate } }] }, - // @ts-ignore { OR: [{ validTo: null }, { validTo: { gte: currentDate } }] }, ], }, From 31715f3841bb0dd6ebf572b7b0489c621382d264 Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 22:00:33 +0100 Subject: [PATCH 23/24] catching errors coming from calculating cost --- src/app/[...openai]/route.ts | 40 ++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/app/[...openai]/route.ts b/src/app/[...openai]/route.ts index e2e2c7b..69088f9 100644 --- a/src/app/[...openai]/route.ts +++ b/src/app/[...openai]/route.ts @@ -184,6 +184,8 @@ export async function POST( const outputTokens = usage?.completionTokens ?? 0; const totalTokens = usage?.totalTokens ?? 0; + + try{ const modelCost = await getModelCost(provider, model); const inputCost = (inputTokens / 1000000) * modelCost.inputTokenCost; const outputCost = @@ -206,7 +208,10 @@ export async function POST( inputCost, outputCost, totalCost, - }; + }; + } catch (err) { + console.error("Error while inserting log:", err); + } await insertLog(logEntry); }, }); @@ -254,21 +259,26 @@ export async function POST( const outputTokens = result.usage?.completionTokens ?? 0; const totalTokens = result.usage?.totalTokens ?? 0; - const modelCost = await getModelCost(provider, model); - const inputCost = inputTokens * modelCost.inputTokenCost; - const outputCost = outputTokens * modelCost.outputTokenCost; - const totalCost = inputCost + outputCost; - logEntry.response = result; - logEntry.metadata = { - ...logEntry.metadata, - inputTokens, - outputTokens, - totalTokens, - inputCost, - outputCost, - totalCost, - }; + + try{ + const modelCost = await getModelCost(provider, model); + const inputCost = inputTokens * modelCost.inputTokenCost; + const outputCost = outputTokens * modelCost.outputTokenCost; + const totalCost = inputCost + outputCost; + + logEntry.metadata = { + ...logEntry.metadata, + inputTokens, + outputTokens, + totalTokens, + inputCost, + outputCost, + totalCost, + }; + } catch (err) { + console.error("Error while calculating cost:", err); + } await insertLog(logEntry); return NextResponse.json(result); From c6ecfb405cb9af37aaf9085a45e51156e6a9858f Mon Sep 17 00:00:00 2001 From: haouarihk Date: Tue, 8 Apr 2025 22:08:31 +0100 Subject: [PATCH 24/24] special case getModelCost for ollama --- src/app/[...openai]/route.ts | 52 ++++++++++++++++++------------------ src/lib/cost-calculator.ts | 16 ++++++++++- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/app/[...openai]/route.ts b/src/app/[...openai]/route.ts index 69088f9..8d81b78 100644 --- a/src/app/[...openai]/route.ts +++ b/src/app/[...openai]/route.ts @@ -185,29 +185,29 @@ export async function POST( const totalTokens = usage?.totalTokens ?? 0; - try{ - const modelCost = await getModelCost(provider, model); - const inputCost = (inputTokens / 1000000) * modelCost.inputTokenCost; - const outputCost = - (outputTokens / 1000000) * modelCost.outputTokenCost; - const totalCost = inputCost + outputCost; - - logEntry.response = { - text, - toolCalls, - toolResults, - usage, - finishReason, - ...otherProps, - }; - logEntry.metadata = { - ...logEntry.metadata, - inputTokens, - outputTokens, - totalTokens, - inputCost, - outputCost, - totalCost, + try { + const modelCost = await getModelCost(provider, model); + const inputCost = (inputTokens / 1000000) * modelCost.inputTokenCost; + const outputCost = + (outputTokens / 1000000) * modelCost.outputTokenCost; + const totalCost = inputCost + outputCost; + + logEntry.response = { + text, + toolCalls, + toolResults, + usage, + finishReason, + ...otherProps, + }; + logEntry.metadata = { + ...logEntry.metadata, + inputTokens, + outputTokens, + totalTokens, + inputCost, + outputCost, + totalCost, }; } catch (err) { console.error("Error while inserting log:", err); @@ -260,13 +260,13 @@ export async function POST( const totalTokens = result.usage?.totalTokens ?? 0; logEntry.response = result; - - try{ + + try { const modelCost = await getModelCost(provider, model); const inputCost = inputTokens * modelCost.inputTokenCost; const outputCost = outputTokens * modelCost.outputTokenCost; const totalCost = inputCost + outputCost; - + logEntry.metadata = { ...logEntry.metadata, inputTokens, diff --git a/src/lib/cost-calculator.ts b/src/lib/cost-calculator.ts index dc877b3..ceccde2 100644 --- a/src/lib/cost-calculator.ts +++ b/src/lib/cost-calculator.ts @@ -1,7 +1,21 @@ import prisma from "@/lib/prisma"; +import { ModelCost } from "@prisma/client"; -export async function getModelCost(provider: string, model: string) { +export async function getModelCost(provider: string, model: string): Promise { const currentDate = new Date(); + + if (provider.toLowerCase() === "ollama") { + return { + id: "ollama", + provider, + model, + inputTokenCost: 0, + outputTokenCost: 0, + validFrom: null, + validTo: null, + }; + } + const modelCost = await prisma.modelCost.findFirst({ where: { provider,