Skip to content

Commit ff40c0b

Browse files
authored
Merge pull request #509 from DefangLabs/kevin/mastraUI
2 parents 9bc5682 + 83d0958 commit ff40c0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+16221
-5
lines changed

.github/workflows/deploy-changed-samples.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jobs:
6565
if: env.should_continue == 'true'
6666
env:
6767
FIXED_VERIFIER_PK: ${{ secrets.FIXED_VERIFIER_PK }}
68+
TEST_ALLOWED_HOSTS: ${{ secrets.TEST_ALLOWED_HOSTS }}
6869
TEST_ANTHROPIC_API_KEY: ${{ secrets.TEST_ANTHROPIC_API_KEY }}
6970
TEST_AWS_ACCESS_KEY: ${{ secrets.TEST_AWS_ACCESS_KEY }}
7071
TEST_AWS_SECRET_KEY: ${{ secrets.TEST_AWS_SECRET_KEY }}
@@ -74,6 +75,11 @@ jobs:
7475
TEST_DATABASE_PASSWORD: ${{ secrets.TEST_DATABASE_PASSWORD }}
7576
TEST_DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
7677
TEST_DATABASE_USERNAME: ${{ secrets.TEST_DATABASE_USERNAME }}
78+
TEST_DB_POSTGRESDB_PASSWORD: ${{ secrets.TEST_POSTGRES_PASSWORD }}
79+
TEST_DB_POSTGRESDB_SSL_ENABLED: ${{ secrets.TEST_DB_POSTGRESDB_SSL_ENABLED }}
80+
TEST_DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED: ${{ secrets.TEST_DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED }}
81+
TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }}
82+
TEST_GOOGLE_GENERATIVE_AI_API_KEY: ${{ secrets.TEST_GOOGLE_GENERATIVE_AI_API_KEY }}
7783
TEST_HASURA_GRAPHQL_ADMIN_SECRET: ${{ secrets.TEST_HASURA_GRAPHQL_ADMIN_SECRET }}
7884
TEST_HASURA_GRAPHQL_DATABASE_URL: ${{ secrets.TEST_HASURA_GRAPHQL_DATABASE_URL }}
7985
TEST_JUPYTER_TOKEN: ${{ secrets.TEST_JUPYTER_TOKEN }}
@@ -87,6 +93,7 @@ jobs:
8793
TEST_MODEL: ${{ secrets.TEST_MODEL }}
8894
TEST_MONGO_INITDB_ROOT_USERNAME: ${{ secrets.TEST_MONGO_INITDB_ROOT_USERNAME }}
8995
TEST_MONGO_INITDB_ROOT_PASSWORD: ${{ secrets.TEST_MONGO_INITDB_ROOT_PASSWORD }}
96+
TEST_N8N_ENCRYPTION_KEY: ${{ secrets.TEST_N8N_ENCRYPTION_KEY }}
9097
TEST_NC_DB: ${{ secrets.TEST_NC_DB }}
9198
TEST_NC_S3_ENDPOINT: ${{ secrets.TEST_NC_S3_ENDPOINT }}
9299
TEST_NC_S3_BUCKET_NAME: ${{ secrets.TEST_NC_S3_BUCKET_NAME }}
@@ -105,11 +112,6 @@ jobs:
105112
TEST_SHARED_SECRETS: ${{ secrets.TEST_SHARED_SECRETS}}
106113
TEST_SSL_MODE: ${{ secrets.TEST_SSL_MODE }}
107114
TEST_TAVILY_API_KEY: ${{ secrets.TEST_TAVILY_API_KEY }}
108-
TEST_ALLOWED_HOSTS: ${{ secrets.TEST_ALLOWED_HOSTS }}
109-
TEST_N8N_ENCRYPTION_KEY: ${{ secrets.TEST_N8N_ENCRYPTION_KEY }}
110-
TEST_DB_POSTGRESDB_SSL_ENABLED: ${{ secrets.TEST_DB_POSTGRESDB_SSL_ENABLED }}
111-
TEST_DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED: ${{ secrets.TEST_DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED }}
112-
TEST_DB_POSTGRESDB_PASSWORD: ${{ secrets.TEST_POSTGRES_PASSWORD }}
113115
run: |
114116
SAMPLES=$(sed 's|^samples/||' changed_samples.txt | paste -s -d ',' -)
115117
echo "Running tests for samples: $SAMPLES"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]
22+
with:
23+
config-env-vars: POSTGRES_PASSWORD GOOGLE_GENERATIVE_AI_API_KEY GITHUB_TOKEN
24+
env:
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
GOOGLE_GENERATIVE_AI_API_KEY: ${{ secrets.GOOGLE_GENERATIVE_AI_API_KEY }}

samples/mastra-nextjs/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Mastra and Next.js Sample
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-mastra-nextjs-template%26template_owner%3DDefangSamples)
4+
5+
This sample shows how to deploy an AI-powered GitHub repository chat tool using [Mastra](https://mastra.ai/), a TypeScript AI framework. Mastra-nextjs allows you to chat with and understand any GitHub repository by fetching file trees, contents, pull requests, and issues, making it easy to navigate and understand codebases of any size.
6+
7+
## Features
8+
9+
- **Repository Analysis**: Enter a GitHub repository URL and instantly start a conversation about it
10+
- **Code Exploration**: Navigate file trees, view file contents, and understand code structure
11+
- **PR & Issue Access**: Query information about pull requests and issues directly in chat
12+
- **Large Codebase Support**: Powered by Google's Gemini Flash model with its large context window
13+
- **Intuitive UI**: Built with assistant-UI for a seamless chat experience with retries, copy, and message branching
14+
15+
## Prerequisites
16+
17+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
18+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
19+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
20+
21+
## Development
22+
23+
To run the application locally for development, use the development compose file:
24+
25+
```bash
26+
docker compose -f compose.dev.yaml up
27+
```
28+
29+
This will:
30+
31+
- Start PostgreSQL with volume persistence for local development
32+
- Expose PostgreSQL on port 5432 for direct access if needed
33+
- Start the Next.js application on port 3000 with hot reload
34+
35+
You can access mastra-nextjs at `http://localhost:3000` once the containers are running.
36+
37+
## Configuration
38+
39+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration). Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
40+
41+
### `GOOGLE_GENERATIVE_AI_API_KEY`
42+
43+
Your Google Generative AI API key for accessing the Gemini Flash model. You can get this from the [Google AI Studio](https://aistudio.google.com/).
44+
45+
### `POSTGRES_PASSWORD`
46+
47+
The password for your Postgres database. You need to set this before deploying for the first time.
48+
49+
_You can easily set this to a random string using `defang config set POSTGRES_PASSWORD --random`_
50+
51+
### `DB_URL`
52+
53+
The [PostgreSQL database connection string](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING). This will be automatically configured when using BYOC managed database services. It should look something like this: `postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]`.
54+
55+
### `DB_SSL`
56+
57+
Set to `true` to enable SSL. Set to `false` to disable SSL. (Can be set directly in the docker compose file)
58+
59+
### `GITHUB_TOKEN` (Optional)
60+
61+
A [GitHub personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token) to increase API rate limits when fetching repository data. This is optional but recommended for better performance.
62+
Setting the permissions to public repositories only is sufficient, unless you want to access private repositories that you have access to.
63+
64+
## Usage
65+
66+
1. Enter a GitHub repository URL in the input field (e.g., `https://github.com/DefangLabs/defang`)
67+
2. Start chatting with mastra-nextjs about the repository
68+
3. Use commands like:
69+
- "Show me the file structure"
70+
- "What are the recent pull requests?"
71+
- "Explain the purpose of [filename]"
72+
- "How many open issues are there?"
73+
74+
## How It Works
75+
76+
Mastra-nextjs uses a tool-based approach rather than traditional RAG systems, making it more efficient for large codebases. When you provide a repository URL, Mastra-nextjs uses tools to:
77+
78+
1. Fetch the repository's file tree
79+
2. Access file contents on demand
80+
3. Retrieve information about pull requests and issues
81+
4. Store conversation history using Mastra's memory package
82+
83+
The large context window of Gemini Flash allows the agent to hold more code in memory, making the conversation more coherent and informed.
84+
85+
## Deployment
86+
87+
> [!NOTE]
88+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
89+
90+
### Defang Playground
91+
92+
Deploy your application to the Defang Playground by opening up your terminal and typing:
93+
94+
```bash
95+
defang compose up
96+
```
97+
98+
### BYOC (Deploy to your own AWS or GCP cloud account)
99+
100+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).
101+
102+
> [!WARNING] > **Extended deployment time:** This sample creates a managed PostgreSQL database which may take upwards of 20 minutes to provision on first deployment. Subsequent deployments are much faster (2-5 minutes).
103+
104+
This sample was base off of mastra's [repo-chat sample](https://github.com/mastra-ai/repo-base).
105+
106+
---
107+
108+
Title: Mastra & Next.js
109+
110+
Short Description: An AI-powered tool for chatting with GitHub repositories using Mastra and Google Gemini.
111+
112+
Tags: AI, GitHub, Mastra, Next.js, PostgreSQL, TypeScript
113+
114+
Languages: TypeScript, JavaScript, Docker
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Default .dockerignore file for Defang
2+
**/__pycache__
3+
**/.direnv
4+
**/.DS_Store
5+
**/.envrc
6+
**/.git
7+
**/.github
8+
**/.idea
9+
**/.next
10+
**/.vscode
11+
**/compose.*.yaml
12+
**/compose.*.yml
13+
**/compose.yaml
14+
**/compose.yml
15+
**/docker-compose.*.yaml
16+
**/docker-compose.*.yml
17+
**/docker-compose.yaml
18+
**/docker-compose.yml
19+
**/node_modules
20+
**/Thumbs.db
21+
Dockerfile
22+
*.Dockerfile
23+
# Ignore our own binary, but only in the root to avoid ignoring subfolders
24+
defang
25+
defang.exe
26+
# Ignore our project-level state
27+
.defang*
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts
42+
43+
.mastra
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:24-alpine
2+
3+
# Used for container health checks
4+
RUN apk add --no-cache curl
5+
6+
WORKDIR /app
7+
8+
# Install dependencies based on the preferred package manager
9+
COPY package.json package-lock.json* pnpm-lock.yaml* .npmrc* ./
10+
11+
RUN corepack enable pnpm && pnpm install --frozen-lockfile --prefer-offline
12+
13+
COPY . .
14+
15+
RUN pnpm run build
16+
17+
CMD [ "pnpm", "run", "start" ]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://ui.shadcn.com/schema.json",
3+
"style": "new-york",
4+
"rsc": true,
5+
"tsx": true,
6+
"tailwind": {
7+
"config": "tailwind.config.ts",
8+
"css": "src/app/globals.css",
9+
"baseColor": "neutral",
10+
"cssVariables": true,
11+
"prefix": ""
12+
},
13+
"aliases": {
14+
"components": "@/components",
15+
"utils": "@/lib/utils",
16+
"ui": "@/components/ui",
17+
"lib": "@/lib",
18+
"hooks": "@/hooks"
19+
},
20+
"iconLibrary": "lucide"
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import "dotenv/config";
2+
import { defineConfig } from "drizzle-kit";
3+
4+
export default defineConfig({
5+
out: "./drizzle",
6+
dialect: "postgresql",
7+
dbCredentials: {
8+
url: process.env.DB_URL!,
9+
},
10+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { dirname } from "path";
2+
import { fileURLToPath } from "url";
3+
import { FlatCompat } from "@eslint/eslintrc";
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = dirname(__filename);
7+
8+
const compat = new FlatCompat({
9+
baseDirectory: __dirname,
10+
});
11+
12+
const eslintConfig = [
13+
...compat.extends("next/core-web-vitals", "next/typescript"),
14+
];
15+
16+
export default eslintConfig;

samples/mastra-nextjs/app/memory-vector.db

Whitespace-only changes.

0 commit comments

Comments
 (0)