mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-03-17 03:00:27 -04:00
Compare commits
4 Commits
feat/githu
...
fix/enable
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdeefb8621 | ||
|
|
ba6d585170 | ||
|
|
90eac56525 | ||
|
|
75f8772f8a |
@@ -1,79 +0,0 @@
|
|||||||
---
|
|
||||||
name: pr-address
|
|
||||||
description: Address PR review comments and loop until CI green and all comments resolved. TRIGGER when user asks to address comments, fix PR feedback, respond to reviewers, or babysit/monitor a PR.
|
|
||||||
user-invocable: true
|
|
||||||
args: "[PR number or URL] — if omitted, finds PR for current branch."
|
|
||||||
metadata:
|
|
||||||
author: autogpt-team
|
|
||||||
version: "1.0.0"
|
|
||||||
---
|
|
||||||
|
|
||||||
# PR Address
|
|
||||||
|
|
||||||
## Find the PR
|
|
||||||
|
|
||||||
```bash
|
|
||||||
gh pr list --head $(git branch --show-current) --repo Significant-Gravitas/AutoGPT
|
|
||||||
gh pr view {N}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Fetch comments (all sources)
|
|
||||||
|
|
||||||
```bash
|
|
||||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/reviews # top-level reviews
|
|
||||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments # inline review comments
|
|
||||||
gh api repos/Significant-Gravitas/AutoGPT/issues/{N}/comments # PR conversation comments
|
|
||||||
```
|
|
||||||
|
|
||||||
**Bots to watch for:**
|
|
||||||
- `autogpt-reviewer` — posts "Blockers", "Should Fix", "Nice to Have". Address ALL of them.
|
|
||||||
- `sentry[bot]` — bug predictions. Fix real bugs, explain false positives.
|
|
||||||
- `coderabbitai[bot]` — automated review. Address actionable items.
|
|
||||||
|
|
||||||
## For each unaddressed comment
|
|
||||||
|
|
||||||
Address comments **one at a time**: fix → commit → push → inline reply → next.
|
|
||||||
|
|
||||||
1. Read the referenced code, make the fix (or reply explaining why it's not needed)
|
|
||||||
2. Commit and push the fix
|
|
||||||
3. Reply **inline** (not as a new top-level comment) referencing the fixing commit — this is what resolves the conversation for bot reviewers (coderabbitai, sentry):
|
|
||||||
|
|
||||||
| Comment type | How to reply |
|
|
||||||
|---|---|
|
|
||||||
| Inline review (`pulls/{N}/comments`) | `gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments/{ID}/replies -f body="Fixed in <commit-sha>: <description>"` |
|
|
||||||
| Conversation (`issues/{N}/comments`) | `gh api repos/Significant-Gravitas/AutoGPT/issues/{N}/comments -f body="Fixed in <commit-sha>: <description>"` |
|
|
||||||
|
|
||||||
## Format and commit
|
|
||||||
|
|
||||||
After fixing, format the changed code:
|
|
||||||
|
|
||||||
- **Backend** (from `autogpt_platform/backend/`): `poetry run format`
|
|
||||||
- **Frontend** (from `autogpt_platform/frontend/`): `pnpm format && pnpm lint && pnpm types`
|
|
||||||
|
|
||||||
If API routes changed, regenerate the frontend client:
|
|
||||||
```bash
|
|
||||||
cd autogpt_platform/backend && poetry run rest &
|
|
||||||
REST_PID=$!
|
|
||||||
trap "kill $REST_PID 2>/dev/null" EXIT
|
|
||||||
WAIT=0; until curl -sf http://localhost:8006/health > /dev/null 2>&1; do sleep 1; WAIT=$((WAIT+1)); [ $WAIT -ge 60 ] && echo "Timed out" && exit 1; done
|
|
||||||
cd ../frontend && pnpm generate:api:force
|
|
||||||
kill $REST_PID 2>/dev/null; trap - EXIT
|
|
||||||
```
|
|
||||||
Never manually edit files in `src/app/api/__generated__/`.
|
|
||||||
|
|
||||||
Then commit and **push immediately** — never batch commits without pushing.
|
|
||||||
|
|
||||||
For backend commits in worktrees: `poetry run git commit` (pre-commit hooks).
|
|
||||||
|
|
||||||
## The loop
|
|
||||||
|
|
||||||
```text
|
|
||||||
address comments → format → commit → push
|
|
||||||
→ re-check comments → fix new ones → push
|
|
||||||
→ wait for CI → re-check comments after CI settles
|
|
||||||
→ repeat until: all comments addressed AND CI green AND no new comments arriving
|
|
||||||
```
|
|
||||||
|
|
||||||
While CI runs, stay productive: run local tests, address remaining comments.
|
|
||||||
|
|
||||||
**The loop ends when:** CI fully green + all comments addressed + no new comments since CI settled.
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
---
|
|
||||||
name: pr-review
|
|
||||||
description: Review a PR for correctness, security, code quality, and testing issues. TRIGGER when user asks to review a PR, check PR quality, or give feedback on a PR.
|
|
||||||
user-invocable: true
|
|
||||||
args: "[PR number or URL] — if omitted, finds PR for current branch."
|
|
||||||
metadata:
|
|
||||||
author: autogpt-team
|
|
||||||
version: "1.0.0"
|
|
||||||
---
|
|
||||||
|
|
||||||
# PR Review
|
|
||||||
|
|
||||||
## Find the PR
|
|
||||||
|
|
||||||
```bash
|
|
||||||
gh pr list --head $(git branch --show-current) --repo Significant-Gravitas/AutoGPT
|
|
||||||
gh pr view {N}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Read the diff
|
|
||||||
|
|
||||||
```bash
|
|
||||||
gh pr diff {N}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Fetch existing review comments
|
|
||||||
|
|
||||||
Before posting anything, fetch existing inline comments to avoid duplicates:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments
|
|
||||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/reviews
|
|
||||||
```
|
|
||||||
|
|
||||||
## What to check
|
|
||||||
|
|
||||||
**Correctness:** logic errors, off-by-one, missing edge cases, race conditions (TOCTOU in file access, credit charging), error handling gaps, async correctness (missing `await`, unclosed resources).
|
|
||||||
|
|
||||||
**Security:** input validation at boundaries, no injection (command, XSS, SQL), secrets not logged, file paths sanitized (`os.path.basename()` in error messages).
|
|
||||||
|
|
||||||
**Code quality:** apply rules from backend/frontend CLAUDE.md files.
|
|
||||||
|
|
||||||
**Architecture:** DRY, single responsibility, modular functions. `Security()` vs `Depends()` for FastAPI auth. `data:` for SSE events, `: comment` for heartbeats. `transaction=True` for Redis pipelines.
|
|
||||||
|
|
||||||
**Testing:** edge cases covered, colocated `*_test.py` (backend) / `__tests__/` (frontend), mocks target where symbol is **used** not defined, `AsyncMock` for async.
|
|
||||||
|
|
||||||
## Output format
|
|
||||||
|
|
||||||
Every comment **must** be prefixed with `🤖` and a criticality badge:
|
|
||||||
|
|
||||||
| Tier | Badge | Meaning |
|
|
||||||
|---|---|---|
|
|
||||||
| Blocker | `🔴 **Blocker**` | Must fix before merge |
|
|
||||||
| Should Fix | `🟠 **Should Fix**` | Important improvement |
|
|
||||||
| Nice to Have | `🟡 **Nice to Have**` | Minor suggestion |
|
|
||||||
| Nit | `🔵 **Nit**` | Style / wording |
|
|
||||||
|
|
||||||
Example: `🤖 🔴 **Blocker**: Missing error handling for X — suggest wrapping in try/except.`
|
|
||||||
|
|
||||||
## Post inline comments
|
|
||||||
|
|
||||||
For each finding, post an inline comment on the PR (do not just write a local report):
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Get the latest commit SHA for the PR
|
|
||||||
COMMIT_SHA=$(gh api repos/Significant-Gravitas/AutoGPT/pulls/{N} --jq '.head.sha')
|
|
||||||
|
|
||||||
# Post an inline comment on a specific file/line
|
|
||||||
gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments \
|
|
||||||
-f body="🤖 🔴 **Blocker**: <description>" \
|
|
||||||
-f commit_id="$COMMIT_SHA" \
|
|
||||||
-f path="<file path>" \
|
|
||||||
-F line=<line number>
|
|
||||||
```
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
---
|
|
||||||
name: worktree
|
|
||||||
description: Set up a new git worktree for parallel development. Creates the worktree, copies .env files, installs dependencies, and generates Prisma client. TRIGGER when user asks to set up a worktree, work on a branch in isolation, or needs a separate environment for a branch or PR.
|
|
||||||
user-invocable: true
|
|
||||||
args: "[name] — optional worktree name (e.g., 'AutoGPT7'). If omitted, uses next available AutoGPT<N>."
|
|
||||||
metadata:
|
|
||||||
author: autogpt-team
|
|
||||||
version: "3.0.0"
|
|
||||||
---
|
|
||||||
|
|
||||||
# Worktree Setup
|
|
||||||
|
|
||||||
## Create the worktree
|
|
||||||
|
|
||||||
Derive paths from the git toplevel. If a name is provided as argument, use it. Otherwise, check `git worktree list` and pick the next `AutoGPT<N>`.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
ROOT=$(git rev-parse --show-toplevel)
|
|
||||||
PARENT=$(dirname "$ROOT")
|
|
||||||
|
|
||||||
# From an existing branch
|
|
||||||
git worktree add "$PARENT/<NAME>" <branch-name>
|
|
||||||
|
|
||||||
# From a new branch off dev
|
|
||||||
git worktree add -b <new-branch> "$PARENT/<NAME>" dev
|
|
||||||
```
|
|
||||||
|
|
||||||
## Copy environment files
|
|
||||||
|
|
||||||
Copy `.env` from the root worktree. Falls back to `.env.default` if `.env` doesn't exist.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
ROOT=$(git rev-parse --show-toplevel)
|
|
||||||
TARGET="$(dirname "$ROOT")/<NAME>"
|
|
||||||
|
|
||||||
for envpath in autogpt_platform/backend autogpt_platform/frontend autogpt_platform; do
|
|
||||||
if [ -f "$ROOT/$envpath/.env" ]; then
|
|
||||||
cp "$ROOT/$envpath/.env" "$TARGET/$envpath/.env"
|
|
||||||
elif [ -f "$ROOT/$envpath/.env.default" ]; then
|
|
||||||
cp "$ROOT/$envpath/.env.default" "$TARGET/$envpath/.env"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
```
|
|
||||||
|
|
||||||
## Install dependencies
|
|
||||||
|
|
||||||
```bash
|
|
||||||
TARGET="$(dirname "$(git rev-parse --show-toplevel)")/<NAME>"
|
|
||||||
cd "$TARGET/autogpt_platform/autogpt_libs" && poetry install
|
|
||||||
cd "$TARGET/autogpt_platform/backend" && poetry install && poetry run prisma generate
|
|
||||||
cd "$TARGET/autogpt_platform/frontend" && pnpm install
|
|
||||||
```
|
|
||||||
|
|
||||||
Replace `<NAME>` with the actual worktree name (e.g., `AutoGPT7`).
|
|
||||||
|
|
||||||
## Running the app (optional)
|
|
||||||
|
|
||||||
Backend uses ports: 8001, 8002, 8003, 8005, 8006, 8007, 8008. Free them first if needed:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
TARGET="$(dirname "$(git rev-parse --show-toplevel)")/<NAME>"
|
|
||||||
for port in 8001 8002 8003 8005 8006 8007 8008; do
|
|
||||||
lsof -ti :$port | xargs kill -9 2>/dev/null || true
|
|
||||||
done
|
|
||||||
cd "$TARGET/autogpt_platform/backend" && poetry run app
|
|
||||||
```
|
|
||||||
|
|
||||||
## CoPilot testing
|
|
||||||
|
|
||||||
SDK mode spawns a Claude subprocess — won't work inside Claude Code. Set `CHAT_USE_CLAUDE_AGENT_SDK=false` in `backend/.env` to use baseline mode.
|
|
||||||
|
|
||||||
## Cleanup
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Replace <NAME> with the actual worktree name (e.g., AutoGPT7)
|
|
||||||
git worktree remove "$(dirname "$(git rev-parse --show-toplevel)")/<NAME>"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Alternative: Branchlet (optional)
|
|
||||||
|
|
||||||
If [branchlet](https://www.npmjs.com/package/branchlet) is installed:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
branchlet create -n <name> -s <source-branch> -b <new-branch>
|
|
||||||
```
|
|
||||||
@@ -5,13 +5,42 @@
|
|||||||
!docs/
|
!docs/
|
||||||
|
|
||||||
# Platform - Libs
|
# Platform - Libs
|
||||||
!autogpt_platform/autogpt_libs/
|
!autogpt_platform/autogpt_libs/autogpt_libs/
|
||||||
|
!autogpt_platform/autogpt_libs/pyproject.toml
|
||||||
|
!autogpt_platform/autogpt_libs/poetry.lock
|
||||||
|
!autogpt_platform/autogpt_libs/README.md
|
||||||
|
|
||||||
# Platform - Backend
|
# Platform - Backend
|
||||||
!autogpt_platform/backend/
|
!autogpt_platform/backend/backend/
|
||||||
|
!autogpt_platform/backend/test/e2e_test_data.py
|
||||||
|
!autogpt_platform/backend/migrations/
|
||||||
|
!autogpt_platform/backend/schema.prisma
|
||||||
|
!autogpt_platform/backend/pyproject.toml
|
||||||
|
!autogpt_platform/backend/poetry.lock
|
||||||
|
!autogpt_platform/backend/README.md
|
||||||
|
!autogpt_platform/backend/.env
|
||||||
|
!autogpt_platform/backend/gen_prisma_types_stub.py
|
||||||
|
|
||||||
|
# Platform - Market
|
||||||
|
!autogpt_platform/market/market/
|
||||||
|
!autogpt_platform/market/scripts.py
|
||||||
|
!autogpt_platform/market/schema.prisma
|
||||||
|
!autogpt_platform/market/pyproject.toml
|
||||||
|
!autogpt_platform/market/poetry.lock
|
||||||
|
!autogpt_platform/market/README.md
|
||||||
|
|
||||||
# Platform - Frontend
|
# Platform - Frontend
|
||||||
!autogpt_platform/frontend/
|
!autogpt_platform/frontend/src/
|
||||||
|
!autogpt_platform/frontend/public/
|
||||||
|
!autogpt_platform/frontend/scripts/
|
||||||
|
!autogpt_platform/frontend/package.json
|
||||||
|
!autogpt_platform/frontend/pnpm-lock.yaml
|
||||||
|
!autogpt_platform/frontend/tsconfig.json
|
||||||
|
!autogpt_platform/frontend/README.md
|
||||||
|
## config
|
||||||
|
!autogpt_platform/frontend/*.config.*
|
||||||
|
!autogpt_platform/frontend/.env.*
|
||||||
|
!autogpt_platform/frontend/.env
|
||||||
|
|
||||||
# Classic - AutoGPT
|
# Classic - AutoGPT
|
||||||
!classic/original_autogpt/autogpt/
|
!classic/original_autogpt/autogpt/
|
||||||
@@ -35,38 +64,6 @@
|
|||||||
# Classic - Frontend
|
# Classic - Frontend
|
||||||
!classic/frontend/build/web/
|
!classic/frontend/build/web/
|
||||||
|
|
||||||
# Explicitly re-ignore unwanted files from whitelisted directories
|
# Explicitly re-ignore some folders
|
||||||
# Note: These patterns MUST come after the whitelist rules to take effect
|
.*
|
||||||
|
**/__pycache__
|
||||||
# Hidden files and directories (but keep frontend .env files needed for build)
|
|
||||||
**/.*
|
|
||||||
!autogpt_platform/frontend/.env
|
|
||||||
!autogpt_platform/frontend/.env.default
|
|
||||||
!autogpt_platform/frontend/.env.production
|
|
||||||
|
|
||||||
# Python artifacts
|
|
||||||
**/__pycache__/
|
|
||||||
**/*.pyc
|
|
||||||
**/*.pyo
|
|
||||||
**/.venv/
|
|
||||||
**/.ruff_cache/
|
|
||||||
**/.pytest_cache/
|
|
||||||
**/.coverage
|
|
||||||
**/htmlcov/
|
|
||||||
|
|
||||||
# Node artifacts
|
|
||||||
**/node_modules/
|
|
||||||
**/.next/
|
|
||||||
**/storybook-static/
|
|
||||||
**/playwright-report/
|
|
||||||
**/test-results/
|
|
||||||
|
|
||||||
# Build artifacts
|
|
||||||
**/dist/
|
|
||||||
**/build/
|
|
||||||
!autogpt_platform/frontend/src/**/build/
|
|
||||||
**/target/
|
|
||||||
|
|
||||||
# Logs and temp files
|
|
||||||
**/*.log
|
|
||||||
**/*.tmp
|
|
||||||
|
|||||||
1229
.github/scripts/detect_overlaps.py
vendored
1229
.github/scripts/detect_overlaps.py
vendored
File diff suppressed because it is too large
Load Diff
44
.github/workflows/claude-ci-failure-auto-fix.yml
vendored
44
.github/workflows/claude-ci-failure-auto-fix.yml
vendored
@@ -22,7 +22,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.event.workflow_run.head_branch }}
|
ref: ${{ github.event.workflow_run.head_branch }}
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
@@ -40,48 +40,6 @@ jobs:
|
|||||||
git checkout -b "$BRANCH_NAME"
|
git checkout -b "$BRANCH_NAME"
|
||||||
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
|
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
# Backend Python/Poetry setup (so Claude can run linting/tests)
|
|
||||||
- name: Set up Python
|
|
||||||
uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: "3.11"
|
|
||||||
|
|
||||||
- name: Set up Python dependency cache
|
|
||||||
uses: actions/cache@v5
|
|
||||||
with:
|
|
||||||
path: ~/.cache/pypoetry
|
|
||||||
key: poetry-${{ runner.os }}-${{ hashFiles('autogpt_platform/backend/poetry.lock') }}
|
|
||||||
|
|
||||||
- name: Install Poetry
|
|
||||||
run: |
|
|
||||||
cd autogpt_platform/backend
|
|
||||||
HEAD_POETRY_VERSION=$(python3 ../../.github/workflows/scripts/get_package_version_from_lockfile.py poetry)
|
|
||||||
curl -sSL https://install.python-poetry.org | POETRY_VERSION=$HEAD_POETRY_VERSION python3 -
|
|
||||||
echo "$HOME/.local/bin" >> $GITHUB_PATH
|
|
||||||
|
|
||||||
- name: Install Python dependencies
|
|
||||||
working-directory: autogpt_platform/backend
|
|
||||||
run: poetry install
|
|
||||||
|
|
||||||
- name: Generate Prisma Client
|
|
||||||
working-directory: autogpt_platform/backend
|
|
||||||
run: poetry run prisma generate && poetry run gen-prisma-stub
|
|
||||||
|
|
||||||
# Frontend Node.js/pnpm setup (so Claude can run linting/tests)
|
|
||||||
- name: Enable corepack
|
|
||||||
run: corepack enable
|
|
||||||
|
|
||||||
- name: Set up Node.js
|
|
||||||
uses: actions/setup-node@v6
|
|
||||||
with:
|
|
||||||
node-version: "22"
|
|
||||||
cache: "pnpm"
|
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
|
||||||
|
|
||||||
- name: Install JavaScript dependencies
|
|
||||||
working-directory: autogpt_platform/frontend
|
|
||||||
run: pnpm install --frozen-lockfile
|
|
||||||
|
|
||||||
- name: Get CI failure details
|
- name: Get CI failure details
|
||||||
id: failure_details
|
id: failure_details
|
||||||
uses: actions/github-script@v8
|
uses: actions/github-script@v8
|
||||||
|
|||||||
24
.github/workflows/claude-dependabot.yml
vendored
24
.github/workflows/claude-dependabot.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
|||||||
actions: read # Required for CI access
|
actions: read # Required for CI access
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 1
|
fetch-depth: 1
|
||||||
|
|
||||||
@@ -77,15 +77,27 @@ jobs:
|
|||||||
run: poetry run prisma generate && poetry run gen-prisma-stub
|
run: poetry run prisma generate && poetry run gen-prisma-stub
|
||||||
|
|
||||||
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
|
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
|
||||||
- name: Enable corepack
|
|
||||||
run: corepack enable
|
|
||||||
|
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v6
|
||||||
with:
|
with:
|
||||||
node-version: "22"
|
node-version: "22"
|
||||||
cache: "pnpm"
|
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
- name: Enable corepack
|
||||||
|
run: corepack enable
|
||||||
|
|
||||||
|
- name: Set pnpm store directory
|
||||||
|
run: |
|
||||||
|
pnpm config set store-dir ~/.pnpm-store
|
||||||
|
echo "PNPM_HOME=$HOME/.pnpm-store" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Cache frontend dependencies
|
||||||
|
uses: actions/cache@v5
|
||||||
|
with:
|
||||||
|
path: ~/.pnpm-store
|
||||||
|
key: ${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml', 'autogpt_platform/frontend/package.json') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
- name: Install JavaScript dependencies
|
- name: Install JavaScript dependencies
|
||||||
working-directory: autogpt_platform/frontend
|
working-directory: autogpt_platform/frontend
|
||||||
|
|||||||
24
.github/workflows/claude.yml
vendored
24
.github/workflows/claude.yml
vendored
@@ -40,7 +40,7 @@ jobs:
|
|||||||
actions: read # Required for CI access
|
actions: read # Required for CI access
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 1
|
fetch-depth: 1
|
||||||
|
|
||||||
@@ -93,15 +93,27 @@ jobs:
|
|||||||
run: poetry run prisma generate && poetry run gen-prisma-stub
|
run: poetry run prisma generate && poetry run gen-prisma-stub
|
||||||
|
|
||||||
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
|
# Frontend Node.js/pnpm setup (mirrors platform-frontend-ci.yml)
|
||||||
- name: Enable corepack
|
|
||||||
run: corepack enable
|
|
||||||
|
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v6
|
||||||
with:
|
with:
|
||||||
node-version: "22"
|
node-version: "22"
|
||||||
cache: "pnpm"
|
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
- name: Enable corepack
|
||||||
|
run: corepack enable
|
||||||
|
|
||||||
|
- name: Set pnpm store directory
|
||||||
|
run: |
|
||||||
|
pnpm config set store-dir ~/.pnpm-store
|
||||||
|
echo "PNPM_HOME=$HOME/.pnpm-store" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Cache frontend dependencies
|
||||||
|
uses: actions/cache@v5
|
||||||
|
with:
|
||||||
|
path: ~/.pnpm-store
|
||||||
|
key: ${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml', 'autogpt_platform/frontend/package.json') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
- name: Install JavaScript dependencies
|
- name: Install JavaScript dependencies
|
||||||
working-directory: autogpt_platform/frontend
|
working-directory: autogpt_platform/frontend
|
||||||
|
|||||||
6
.github/workflows/codeql.yml
vendored
6
.github/workflows/codeql.yml
vendored
@@ -58,11 +58,11 @@ jobs:
|
|||||||
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
|
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@v4
|
uses: github/codeql-action/init@v3
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
build-mode: ${{ matrix.build-mode }}
|
build-mode: ${{ matrix.build-mode }}
|
||||||
@@ -93,6 +93,6 @@ jobs:
|
|||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@v4
|
uses: github/codeql-action/analyze@v3
|
||||||
with:
|
with:
|
||||||
category: "/language:${{matrix.language}}"
|
category: "/language:${{matrix.language}}"
|
||||||
|
|||||||
2
.github/workflows/copilot-setup-steps.yml
vendored
2
.github/workflows/copilot-setup-steps.yml
vendored
@@ -27,7 +27,7 @@ jobs:
|
|||||||
# If you do not check out your code, Copilot will do this for you.
|
# If you do not check out your code, Copilot will do this for you.
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|||||||
2
.github/workflows/docs-block-sync.yml
vendored
2
.github/workflows/docs-block-sync.yml
vendored
@@ -23,7 +23,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 1
|
fetch-depth: 1
|
||||||
|
|
||||||
|
|||||||
36
.github/workflows/docs-claude-review.yml
vendored
36
.github/workflows/docs-claude-review.yml
vendored
@@ -7,10 +7,6 @@ on:
|
|||||||
- "docs/integrations/**"
|
- "docs/integrations/**"
|
||||||
- "autogpt_platform/backend/backend/blocks/**"
|
- "autogpt_platform/backend/backend/blocks/**"
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: claude-docs-review-${{ github.event.pull_request.number }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
claude-review:
|
claude-review:
|
||||||
# Only run for PRs from members/collaborators
|
# Only run for PRs from members/collaborators
|
||||||
@@ -27,7 +23,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
@@ -95,35 +91,5 @@ jobs:
|
|||||||
3. Read corresponding documentation files to verify accuracy
|
3. Read corresponding documentation files to verify accuracy
|
||||||
4. Provide your feedback as a PR comment
|
4. Provide your feedback as a PR comment
|
||||||
|
|
||||||
## IMPORTANT: Comment Marker
|
|
||||||
Start your PR comment with exactly this HTML comment marker on its own line:
|
|
||||||
<!-- CLAUDE_DOCS_REVIEW -->
|
|
||||||
|
|
||||||
This marker is used to identify and replace your comment on subsequent runs.
|
|
||||||
|
|
||||||
Be constructive and specific. If everything looks good, say so!
|
Be constructive and specific. If everything looks good, say so!
|
||||||
If there are issues, explain what's wrong and suggest how to fix it.
|
If there are issues, explain what's wrong and suggest how to fix it.
|
||||||
|
|
||||||
- name: Delete old Claude review comments
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
run: |
|
|
||||||
# Get all comment IDs with our marker, sorted by creation date (oldest first)
|
|
||||||
COMMENT_IDS=$(gh api \
|
|
||||||
repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
|
|
||||||
--jq '[.[] | select(.body | contains("<!-- CLAUDE_DOCS_REVIEW -->"))] | sort_by(.created_at) | .[].id')
|
|
||||||
|
|
||||||
# Count comments
|
|
||||||
COMMENT_COUNT=$(echo "$COMMENT_IDS" | grep -c . || true)
|
|
||||||
|
|
||||||
if [ "$COMMENT_COUNT" -gt 1 ]; then
|
|
||||||
# Delete all but the last (newest) comment
|
|
||||||
echo "$COMMENT_IDS" | head -n -1 | while read -r COMMENT_ID; do
|
|
||||||
if [ -n "$COMMENT_ID" ]; then
|
|
||||||
echo "Deleting old review comment: $COMMENT_ID"
|
|
||||||
gh api -X DELETE repos/${{ github.repository }}/issues/comments/$COMMENT_ID
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
else
|
|
||||||
echo "No old review comments to clean up"
|
|
||||||
fi
|
|
||||||
|
|||||||
2
.github/workflows/docs-enhance.yml
vendored
2
.github/workflows/docs-enhance.yml
vendored
@@ -28,7 +28,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 1
|
fetch-depth: 1
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.event.inputs.git_ref || github.ref_name }}
|
ref: ${{ github.event.inputs.git_ref || github.ref_name }}
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Trigger deploy workflow
|
- name: Trigger deploy workflow
|
||||||
uses: peter-evans/repository-dispatch@v4
|
uses: peter-evans/repository-dispatch@v3
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.DEPLOY_TOKEN }}
|
token: ${{ secrets.DEPLOY_TOKEN }}
|
||||||
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.ref_name || 'master' }}
|
ref: ${{ github.ref_name || 'master' }}
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Trigger deploy workflow
|
- name: Trigger deploy workflow
|
||||||
uses: peter-evans/repository-dispatch@v4
|
uses: peter-evans/repository-dispatch@v3
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.DEPLOY_TOKEN }}
|
token: ${{ secrets.DEPLOY_TOKEN }}
|
||||||
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
||||||
|
|||||||
11
.github/workflows/platform-backend-ci.yml
vendored
11
.github/workflows/platform-backend-ci.yml
vendored
@@ -41,18 +41,13 @@ jobs:
|
|||||||
ports:
|
ports:
|
||||||
- 6379:6379
|
- 6379:6379
|
||||||
rabbitmq:
|
rabbitmq:
|
||||||
image: rabbitmq:4.1.4
|
image: rabbitmq:3.12-management
|
||||||
ports:
|
ports:
|
||||||
- 5672:5672
|
- 5672:5672
|
||||||
|
- 15672:15672
|
||||||
env:
|
env:
|
||||||
RABBITMQ_DEFAULT_USER: ${{ env.RABBITMQ_DEFAULT_USER }}
|
RABBITMQ_DEFAULT_USER: ${{ env.RABBITMQ_DEFAULT_USER }}
|
||||||
RABBITMQ_DEFAULT_PASS: ${{ env.RABBITMQ_DEFAULT_PASS }}
|
RABBITMQ_DEFAULT_PASS: ${{ env.RABBITMQ_DEFAULT_PASS }}
|
||||||
options: >-
|
|
||||||
--health-cmd "rabbitmq-diagnostics -q ping"
|
|
||||||
--health-interval 30s
|
|
||||||
--health-timeout 10s
|
|
||||||
--health-retries 5
|
|
||||||
--health-start-period 10s
|
|
||||||
clamav:
|
clamav:
|
||||||
image: clamav/clamav-debian:latest
|
image: clamav/clamav-debian:latest
|
||||||
ports:
|
ports:
|
||||||
@@ -73,7 +68,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Dispatch Deploy Event
|
- name: Dispatch Deploy Event
|
||||||
if: steps.check_status.outputs.should_deploy == 'true'
|
if: steps.check_status.outputs.should_deploy == 'true'
|
||||||
uses: peter-evans/repository-dispatch@v4
|
uses: peter-evans/repository-dispatch@v3
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.DISPATCH_TOKEN }}
|
token: ${{ secrets.DISPATCH_TOKEN }}
|
||||||
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
||||||
@@ -110,7 +110,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Dispatch Undeploy Event (from comment)
|
- name: Dispatch Undeploy Event (from comment)
|
||||||
if: steps.check_status.outputs.should_undeploy == 'true'
|
if: steps.check_status.outputs.should_undeploy == 'true'
|
||||||
uses: peter-evans/repository-dispatch@v4
|
uses: peter-evans/repository-dispatch@v3
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.DISPATCH_TOKEN }}
|
token: ${{ secrets.DISPATCH_TOKEN }}
|
||||||
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
||||||
@@ -168,7 +168,7 @@ jobs:
|
|||||||
github.event_name == 'pull_request' &&
|
github.event_name == 'pull_request' &&
|
||||||
github.event.action == 'closed' &&
|
github.event.action == 'closed' &&
|
||||||
steps.check_pr_close.outputs.should_undeploy == 'true'
|
steps.check_pr_close.outputs.should_undeploy == 'true'
|
||||||
uses: peter-evans/repository-dispatch@v4
|
uses: peter-evans/repository-dispatch@v3
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.DISPATCH_TOKEN }}
|
token: ${{ secrets.DISPATCH_TOKEN }}
|
||||||
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
|
||||||
|
|||||||
259
.github/workflows/platform-frontend-ci.yml
vendored
259
.github/workflows/platform-frontend-ci.yml
vendored
@@ -6,16 +6,10 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- ".github/workflows/platform-frontend-ci.yml"
|
- ".github/workflows/platform-frontend-ci.yml"
|
||||||
- "autogpt_platform/frontend/**"
|
- "autogpt_platform/frontend/**"
|
||||||
- "autogpt_platform/backend/Dockerfile"
|
|
||||||
- "autogpt_platform/docker-compose.yml"
|
|
||||||
- "autogpt_platform/docker-compose.platform.yml"
|
|
||||||
pull_request:
|
pull_request:
|
||||||
paths:
|
paths:
|
||||||
- ".github/workflows/platform-frontend-ci.yml"
|
- ".github/workflows/platform-frontend-ci.yml"
|
||||||
- "autogpt_platform/frontend/**"
|
- "autogpt_platform/frontend/**"
|
||||||
- "autogpt_platform/backend/Dockerfile"
|
|
||||||
- "autogpt_platform/docker-compose.yml"
|
|
||||||
- "autogpt_platform/docker-compose.platform.yml"
|
|
||||||
merge_group:
|
merge_group:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
@@ -32,11 +26,12 @@ jobs:
|
|||||||
setup:
|
setup:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
outputs:
|
outputs:
|
||||||
|
cache-key: ${{ steps.cache-key.outputs.key }}
|
||||||
components-changed: ${{ steps.filter.outputs.components }}
|
components-changed: ${{ steps.filter.outputs.components }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Check for component changes
|
- name: Check for component changes
|
||||||
uses: dorny/paths-filter@v3
|
uses: dorny/paths-filter@v3
|
||||||
@@ -46,17 +41,28 @@ jobs:
|
|||||||
components:
|
components:
|
||||||
- 'autogpt_platform/frontend/src/components/**'
|
- 'autogpt_platform/frontend/src/components/**'
|
||||||
|
|
||||||
- name: Enable corepack
|
- name: Set up Node.js
|
||||||
run: corepack enable
|
|
||||||
|
|
||||||
- name: Set up Node
|
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v6
|
||||||
with:
|
with:
|
||||||
node-version: "22.18.0"
|
node-version: "22.18.0"
|
||||||
cache: "pnpm"
|
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
|
||||||
|
|
||||||
- name: Install dependencies to populate cache
|
- name: Enable corepack
|
||||||
|
run: corepack enable
|
||||||
|
|
||||||
|
- name: Generate cache key
|
||||||
|
id: cache-key
|
||||||
|
run: echo "key=${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml', 'autogpt_platform/frontend/package.json') }}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Cache dependencies
|
||||||
|
uses: actions/cache@v5
|
||||||
|
with:
|
||||||
|
path: ~/.pnpm-store
|
||||||
|
key: ${{ steps.cache-key.outputs.key }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
@@ -65,17 +71,24 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@v6
|
||||||
|
with:
|
||||||
|
node-version: "22.18.0"
|
||||||
|
|
||||||
- name: Enable corepack
|
- name: Enable corepack
|
||||||
run: corepack enable
|
run: corepack enable
|
||||||
|
|
||||||
- name: Set up Node
|
- name: Restore dependencies cache
|
||||||
uses: actions/setup-node@v6
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
node-version: "22.18.0"
|
path: ~/.pnpm-store
|
||||||
cache: "pnpm"
|
key: ${{ needs.setup.outputs.cache-key }}
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
@@ -94,19 +107,26 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@v6
|
||||||
|
with:
|
||||||
|
node-version: "22.18.0"
|
||||||
|
|
||||||
- name: Enable corepack
|
- name: Enable corepack
|
||||||
run: corepack enable
|
run: corepack enable
|
||||||
|
|
||||||
- name: Set up Node
|
- name: Restore dependencies cache
|
||||||
uses: actions/setup-node@v6
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
node-version: "22.18.0"
|
path: ~/.pnpm-store
|
||||||
cache: "pnpm"
|
key: ${{ needs.setup.outputs.cache-key }}
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
@@ -121,20 +141,30 @@ jobs:
|
|||||||
exitOnceUploaded: true
|
exitOnceUploaded: true
|
||||||
|
|
||||||
e2e_test:
|
e2e_test:
|
||||||
name: end-to-end tests
|
|
||||||
runs-on: big-boi
|
runs-on: big-boi
|
||||||
|
needs: setup
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- name: Set up Platform - Copy default supabase .env
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@v6
|
||||||
|
with:
|
||||||
|
node-version: "22.18.0"
|
||||||
|
|
||||||
|
- name: Enable corepack
|
||||||
|
run: corepack enable
|
||||||
|
|
||||||
|
- name: Copy default supabase .env
|
||||||
run: |
|
run: |
|
||||||
cp ../.env.default ../.env
|
cp ../.env.default ../.env
|
||||||
|
|
||||||
- name: Set up Platform - Copy backend .env and set OpenAI API key
|
- name: Copy backend .env and set OpenAI API key
|
||||||
run: |
|
run: |
|
||||||
cp ../backend/.env.default ../backend/.env
|
cp ../backend/.env.default ../backend/.env
|
||||||
echo "OPENAI_INTERNAL_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> ../backend/.env
|
echo "OPENAI_INTERNAL_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> ../backend/.env
|
||||||
@@ -142,125 +172,77 @@ jobs:
|
|||||||
# Used by E2E test data script to generate embeddings for approved store agents
|
# Used by E2E test data script to generate embeddings for approved store agents
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||||
|
|
||||||
- name: Set up Platform - Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
with:
|
|
||||||
driver: docker-container
|
|
||||||
driver-opts: network=host
|
|
||||||
|
|
||||||
- name: Set up Platform - Expose GHA cache to docker buildx CLI
|
- name: Cache Docker layers
|
||||||
uses: crazy-max/ghaction-github-runtime@v4
|
|
||||||
|
|
||||||
- name: Set up Platform - Build Docker images (with cache)
|
|
||||||
working-directory: autogpt_platform
|
|
||||||
run: |
|
|
||||||
pip install pyyaml
|
|
||||||
|
|
||||||
# Resolve extends and generate a flat compose file that bake can understand
|
|
||||||
docker compose -f docker-compose.yml config > docker-compose.resolved.yml
|
|
||||||
|
|
||||||
# Add cache configuration to the resolved compose file
|
|
||||||
python ../.github/workflows/scripts/docker-ci-fix-compose-build-cache.py \
|
|
||||||
--source docker-compose.resolved.yml \
|
|
||||||
--cache-from "type=gha" \
|
|
||||||
--cache-to "type=gha,mode=max" \
|
|
||||||
--backend-hash "${{ hashFiles('autogpt_platform/backend/Dockerfile', 'autogpt_platform/backend/poetry.lock', 'autogpt_platform/backend/backend') }}" \
|
|
||||||
--frontend-hash "${{ hashFiles('autogpt_platform/frontend/Dockerfile', 'autogpt_platform/frontend/pnpm-lock.yaml', 'autogpt_platform/frontend/src') }}" \
|
|
||||||
--git-ref "${{ github.ref }}"
|
|
||||||
|
|
||||||
# Build with bake using the resolved compose file (now includes cache config)
|
|
||||||
docker buildx bake --allow=fs.read=.. -f docker-compose.resolved.yml --load
|
|
||||||
env:
|
|
||||||
NEXT_PUBLIC_PW_TEST: true
|
|
||||||
|
|
||||||
- name: Set up tests - Cache E2E test data
|
|
||||||
id: e2e-data-cache
|
|
||||||
uses: actions/cache@v5
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
path: /tmp/e2e_test_data.sql
|
path: /tmp/.buildx-cache
|
||||||
key: e2e-test-data-${{ hashFiles('autogpt_platform/backend/test/e2e_test_data.py', 'autogpt_platform/backend/migrations/**', '.github/workflows/platform-frontend-ci.yml') }}
|
key: ${{ runner.os }}-buildx-frontend-test-${{ hashFiles('autogpt_platform/docker-compose.yml', 'autogpt_platform/backend/Dockerfile', 'autogpt_platform/backend/pyproject.toml', 'autogpt_platform/backend/poetry.lock') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-buildx-frontend-test-
|
||||||
|
|
||||||
- name: Set up Platform - Start Supabase DB + Auth
|
- name: Run docker compose
|
||||||
run: |
|
run: |
|
||||||
docker compose -f ../docker-compose.resolved.yml up -d db auth --no-build
|
NEXT_PUBLIC_PW_TEST=true docker compose -f ../docker-compose.yml up -d
|
||||||
echo "Waiting for database to be ready..."
|
|
||||||
timeout 60 sh -c 'until docker compose -f ../docker-compose.resolved.yml exec -T db pg_isready -U postgres 2>/dev/null; do sleep 2; done'
|
|
||||||
echo "Waiting for auth service to be ready..."
|
|
||||||
timeout 60 sh -c 'until docker compose -f ../docker-compose.resolved.yml exec -T db psql -U postgres -d postgres -c "SELECT 1 FROM auth.users LIMIT 1" 2>/dev/null; do sleep 2; done' || echo "Auth schema check timeout, continuing..."
|
|
||||||
|
|
||||||
- name: Set up Platform - Run migrations
|
|
||||||
run: |
|
|
||||||
echo "Running migrations..."
|
|
||||||
docker compose -f ../docker-compose.resolved.yml run --rm migrate
|
|
||||||
echo "✅ Migrations completed"
|
|
||||||
env:
|
env:
|
||||||
NEXT_PUBLIC_PW_TEST: true
|
DOCKER_BUILDKIT: 1
|
||||||
|
BUILDX_CACHE_FROM: type=local,src=/tmp/.buildx-cache
|
||||||
|
BUILDX_CACHE_TO: type=local,dest=/tmp/.buildx-cache-new,mode=max
|
||||||
|
|
||||||
- name: Set up tests - Load cached E2E test data
|
- name: Move cache
|
||||||
if: steps.e2e-data-cache.outputs.cache-hit == 'true'
|
|
||||||
run: |
|
run: |
|
||||||
echo "✅ Found cached E2E test data, restoring..."
|
rm -rf /tmp/.buildx-cache
|
||||||
{
|
if [ -d "/tmp/.buildx-cache-new" ]; then
|
||||||
echo "SET session_replication_role = 'replica';"
|
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
|
||||||
cat /tmp/e2e_test_data.sql
|
fi
|
||||||
echo "SET session_replication_role = 'origin';"
|
|
||||||
} | docker compose -f ../docker-compose.resolved.yml exec -T db psql -U postgres -d postgres -b
|
|
||||||
# Refresh materialized views after restore
|
|
||||||
docker compose -f ../docker-compose.resolved.yml exec -T db \
|
|
||||||
psql -U postgres -d postgres -b -c "SET search_path TO platform; SELECT refresh_store_materialized_views();" || true
|
|
||||||
|
|
||||||
echo "✅ E2E test data restored from cache"
|
- name: Wait for services to be ready
|
||||||
|
|
||||||
- name: Set up Platform - Start (all other services)
|
|
||||||
run: |
|
run: |
|
||||||
docker compose -f ../docker-compose.resolved.yml up -d --no-build
|
|
||||||
echo "Waiting for rest_server to be ready..."
|
echo "Waiting for rest_server to be ready..."
|
||||||
timeout 60 sh -c 'until curl -f http://localhost:8006/health 2>/dev/null; do sleep 2; done' || echo "Rest server health check timeout, continuing..."
|
timeout 60 sh -c 'until curl -f http://localhost:8006/health 2>/dev/null; do sleep 2; done' || echo "Rest server health check timeout, continuing..."
|
||||||
env:
|
echo "Waiting for database to be ready..."
|
||||||
NEXT_PUBLIC_PW_TEST: true
|
timeout 60 sh -c 'until docker compose -f ../docker-compose.yml exec -T db pg_isready -U postgres 2>/dev/null; do sleep 2; done' || echo "Database ready check timeout, continuing..."
|
||||||
|
|
||||||
- name: Set up tests - Create E2E test data
|
- name: Create E2E test data
|
||||||
if: steps.e2e-data-cache.outputs.cache-hit != 'true'
|
|
||||||
run: |
|
run: |
|
||||||
echo "Creating E2E test data..."
|
echo "Creating E2E test data..."
|
||||||
docker cp ../backend/test/e2e_test_data.py $(docker compose -f ../docker-compose.resolved.yml ps -q rest_server):/tmp/e2e_test_data.py
|
# First try to run the script from inside the container
|
||||||
docker compose -f ../docker-compose.resolved.yml exec -T rest_server sh -c "cd /app/autogpt_platform && python /tmp/e2e_test_data.py" || {
|
if docker compose -f ../docker-compose.yml exec -T rest_server test -f /app/autogpt_platform/backend/test/e2e_test_data.py; then
|
||||||
echo "❌ E2E test data creation failed!"
|
echo "✅ Found e2e_test_data.py in container, running it..."
|
||||||
docker compose -f ../docker-compose.resolved.yml logs --tail=50 rest_server
|
docker compose -f ../docker-compose.yml exec -T rest_server sh -c "cd /app/autogpt_platform && python backend/test/e2e_test_data.py" || {
|
||||||
exit 1
|
echo "❌ E2E test data creation failed!"
|
||||||
}
|
docker compose -f ../docker-compose.yml logs --tail=50 rest_server
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
else
|
||||||
|
echo "⚠️ e2e_test_data.py not found in container, copying and running..."
|
||||||
|
# Copy the script into the container and run it
|
||||||
|
docker cp ../backend/test/e2e_test_data.py $(docker compose -f ../docker-compose.yml ps -q rest_server):/tmp/e2e_test_data.py || {
|
||||||
|
echo "❌ Failed to copy script to container"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
docker compose -f ../docker-compose.yml exec -T rest_server sh -c "cd /app/autogpt_platform && python /tmp/e2e_test_data.py" || {
|
||||||
|
echo "❌ E2E test data creation failed!"
|
||||||
|
docker compose -f ../docker-compose.yml logs --tail=50 rest_server
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
# Dump auth.users + platform schema for cache (two separate dumps)
|
- name: Restore dependencies cache
|
||||||
echo "Dumping database for cache..."
|
uses: actions/cache@v5
|
||||||
{
|
|
||||||
docker compose -f ../docker-compose.resolved.yml exec -T db \
|
|
||||||
pg_dump -U postgres --data-only --column-inserts \
|
|
||||||
--table='auth.users' postgres
|
|
||||||
docker compose -f ../docker-compose.resolved.yml exec -T db \
|
|
||||||
pg_dump -U postgres --data-only --column-inserts \
|
|
||||||
--schema=platform \
|
|
||||||
--exclude-table='platform._prisma_migrations' \
|
|
||||||
--exclude-table='platform.apscheduler_jobs' \
|
|
||||||
--exclude-table='platform.apscheduler_jobs_batched_notifications' \
|
|
||||||
postgres
|
|
||||||
} > /tmp/e2e_test_data.sql
|
|
||||||
|
|
||||||
echo "✅ Database dump created for caching ($(wc -l < /tmp/e2e_test_data.sql) lines)"
|
|
||||||
|
|
||||||
- name: Set up tests - Enable corepack
|
|
||||||
run: corepack enable
|
|
||||||
|
|
||||||
- name: Set up tests - Set up Node
|
|
||||||
uses: actions/setup-node@v6
|
|
||||||
with:
|
with:
|
||||||
node-version: "22.18.0"
|
path: ~/.pnpm-store
|
||||||
cache: "pnpm"
|
key: ${{ needs.setup.outputs.cache-key }}
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
- name: Set up tests - Install dependencies
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
|
|
||||||
- name: Set up tests - Install browser 'chromium'
|
- name: Install Browser 'chromium'
|
||||||
run: pnpm playwright install --with-deps chromium
|
run: pnpm playwright install --with-deps chromium
|
||||||
|
|
||||||
- name: Run Playwright tests
|
- name: Run Playwright tests
|
||||||
@@ -287,7 +269,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Print Final Docker Compose logs
|
- name: Print Final Docker Compose logs
|
||||||
if: always()
|
if: always()
|
||||||
run: docker compose -f ../docker-compose.resolved.yml logs
|
run: docker compose -f ../docker-compose.yml logs
|
||||||
|
|
||||||
integration_test:
|
integration_test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -295,19 +277,26 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@v6
|
||||||
|
with:
|
||||||
|
node-version: "22.18.0"
|
||||||
|
|
||||||
- name: Enable corepack
|
- name: Enable corepack
|
||||||
run: corepack enable
|
run: corepack enable
|
||||||
|
|
||||||
- name: Set up Node
|
- name: Restore dependencies cache
|
||||||
uses: actions/setup-node@v6
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
node-version: "22.18.0"
|
path: ~/.pnpm-store
|
||||||
cache: "pnpm"
|
key: ${{ needs.setup.outputs.cache-key }}
|
||||||
cache-dependency-path: autogpt_platform/frontend/pnpm-lock.yaml
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pnpm-${{ hashFiles('autogpt_platform/frontend/pnpm-lock.yaml') }}
|
||||||
|
${{ runner.os }}-pnpm-
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --frozen-lockfile
|
||||||
|
|||||||
4
.github/workflows/platform-fullstack-ci.yml
vendored
4
.github/workflows/platform-fullstack-ci.yml
vendored
@@ -29,7 +29,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v6
|
uses: actions/setup-node@v6
|
||||||
@@ -63,7 +63,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v6
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
|
|||||||
39
.github/workflows/pr-overlap-check.yml
vendored
39
.github/workflows/pr-overlap-check.yml
vendored
@@ -1,39 +0,0 @@
|
|||||||
name: PR Overlap Detection
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, synchronize, reopened]
|
|
||||||
branches:
|
|
||||||
- dev
|
|
||||||
- master
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
pull-requests: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
check-overlaps:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0 # Need full history for merge testing
|
|
||||||
|
|
||||||
- name: Set up Python
|
|
||||||
uses: actions/setup-python@v5
|
|
||||||
with:
|
|
||||||
python-version: '3.11'
|
|
||||||
|
|
||||||
- name: Configure git
|
|
||||||
run: |
|
|
||||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
|
||||||
git config user.name "github-actions[bot]"
|
|
||||||
|
|
||||||
- name: Run overlap detection
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
# Always succeed - this check informs contributors, it shouldn't block merging
|
|
||||||
continue-on-error: true
|
|
||||||
run: |
|
|
||||||
python .github/scripts/detect_overlaps.py ${{ github.event.pull_request.number }}
|
|
||||||
2
.github/workflows/repo-workflow-checker.yml
vendored
2
.github/workflows/repo-workflow-checker.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
# - name: Wait some time for all actions to start
|
# - name: Wait some time for all actions to start
|
||||||
# run: sleep 30
|
# run: sleep 30
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v4
|
||||||
# with:
|
# with:
|
||||||
# fetch-depth: 0
|
# fetch-depth: 0
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
|
|||||||
@@ -1,195 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
Add cache configuration to a resolved docker-compose file for all services
|
|
||||||
that have a build key, and ensure image names match what docker compose expects.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_BRANCH = "dev"
|
|
||||||
CACHE_BUILDS_FOR_COMPONENTS = ["backend", "frontend"]
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description="Add cache config to a resolved compose file"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--source",
|
|
||||||
required=True,
|
|
||||||
help="Source compose file to read (should be output of `docker compose config`)",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--cache-from",
|
|
||||||
default="type=gha",
|
|
||||||
help="Cache source configuration",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--cache-to",
|
|
||||||
default="type=gha,mode=max",
|
|
||||||
help="Cache destination configuration",
|
|
||||||
)
|
|
||||||
for component in CACHE_BUILDS_FOR_COMPONENTS:
|
|
||||||
parser.add_argument(
|
|
||||||
f"--{component}-hash",
|
|
||||||
default="",
|
|
||||||
help=f"Hash for {component} cache scope (e.g., from hashFiles())",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--git-ref",
|
|
||||||
default="",
|
|
||||||
help="Git ref for branch-based cache scope (e.g., refs/heads/master)",
|
|
||||||
)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# Normalize git ref to a safe scope name (e.g., refs/heads/master -> master)
|
|
||||||
git_ref_scope = ""
|
|
||||||
if args.git_ref:
|
|
||||||
git_ref_scope = args.git_ref.replace("refs/heads/", "").replace("/", "-")
|
|
||||||
|
|
||||||
with open(args.source, "r") as f:
|
|
||||||
compose = yaml.safe_load(f)
|
|
||||||
|
|
||||||
# Get project name from compose file or default
|
|
||||||
project_name = compose.get("name", "autogpt_platform")
|
|
||||||
|
|
||||||
def get_image_name(dockerfile: str, target: str) -> str:
|
|
||||||
"""Generate image name based on Dockerfile folder and build target."""
|
|
||||||
dockerfile_parts = dockerfile.replace("\\", "/").split("/")
|
|
||||||
if len(dockerfile_parts) >= 2:
|
|
||||||
folder_name = dockerfile_parts[-2] # e.g., "backend" or "frontend"
|
|
||||||
else:
|
|
||||||
folder_name = "app"
|
|
||||||
return f"{project_name}-{folder_name}:{target}"
|
|
||||||
|
|
||||||
def get_build_key(dockerfile: str, target: str) -> str:
|
|
||||||
"""Generate a unique key for a Dockerfile+target combination."""
|
|
||||||
return f"{dockerfile}:{target}"
|
|
||||||
|
|
||||||
def get_component(dockerfile: str) -> str | None:
|
|
||||||
"""Get component name (frontend/backend) from dockerfile path."""
|
|
||||||
for component in CACHE_BUILDS_FOR_COMPONENTS:
|
|
||||||
if component in dockerfile:
|
|
||||||
return component
|
|
||||||
return None
|
|
||||||
|
|
||||||
# First pass: collect all services with build configs and identify duplicates
|
|
||||||
# Track which (dockerfile, target) combinations we've seen
|
|
||||||
build_key_to_first_service: dict[str, str] = {}
|
|
||||||
services_to_build: list[str] = []
|
|
||||||
services_to_dedupe: list[str] = []
|
|
||||||
|
|
||||||
for service_name, service_config in compose.get("services", {}).items():
|
|
||||||
if "build" not in service_config:
|
|
||||||
continue
|
|
||||||
|
|
||||||
build_config = service_config["build"]
|
|
||||||
dockerfile = build_config.get("dockerfile", "Dockerfile")
|
|
||||||
target = build_config.get("target", "default")
|
|
||||||
build_key = get_build_key(dockerfile, target)
|
|
||||||
|
|
||||||
if build_key not in build_key_to_first_service:
|
|
||||||
# First service with this build config - it will do the actual build
|
|
||||||
build_key_to_first_service[build_key] = service_name
|
|
||||||
services_to_build.append(service_name)
|
|
||||||
else:
|
|
||||||
# Duplicate - will just use the image from the first service
|
|
||||||
services_to_dedupe.append(service_name)
|
|
||||||
|
|
||||||
# Second pass: configure builds and deduplicate
|
|
||||||
modified_services = []
|
|
||||||
for service_name, service_config in compose.get("services", {}).items():
|
|
||||||
if "build" not in service_config:
|
|
||||||
continue
|
|
||||||
|
|
||||||
build_config = service_config["build"]
|
|
||||||
dockerfile = build_config.get("dockerfile", "Dockerfile")
|
|
||||||
target = build_config.get("target", "latest")
|
|
||||||
image_name = get_image_name(dockerfile, target)
|
|
||||||
|
|
||||||
# Set image name for all services (needed for both builders and deduped)
|
|
||||||
service_config["image"] = image_name
|
|
||||||
|
|
||||||
if service_name in services_to_dedupe:
|
|
||||||
# Remove build config - this service will use the pre-built image
|
|
||||||
del service_config["build"]
|
|
||||||
continue
|
|
||||||
|
|
||||||
# This service will do the actual build - add cache config
|
|
||||||
cache_from_list = []
|
|
||||||
cache_to_list = []
|
|
||||||
|
|
||||||
component = get_component(dockerfile)
|
|
||||||
if not component:
|
|
||||||
# Skip services that don't clearly match frontend/backend
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Get the hash for this component
|
|
||||||
component_hash = getattr(args, f"{component}_hash")
|
|
||||||
|
|
||||||
# Scope format: platform-{component}-{target}-{hash|ref}
|
|
||||||
# Example: platform-backend-server-abc123
|
|
||||||
|
|
||||||
if "type=gha" in args.cache_from:
|
|
||||||
# 1. Primary: exact hash match (most specific)
|
|
||||||
if component_hash:
|
|
||||||
hash_scope = f"platform-{component}-{target}-{component_hash}"
|
|
||||||
cache_from_list.append(f"{args.cache_from},scope={hash_scope}")
|
|
||||||
|
|
||||||
# 2. Fallback: branch-based cache
|
|
||||||
if git_ref_scope:
|
|
||||||
ref_scope = f"platform-{component}-{target}-{git_ref_scope}"
|
|
||||||
cache_from_list.append(f"{args.cache_from},scope={ref_scope}")
|
|
||||||
|
|
||||||
# 3. Fallback: dev branch cache (for PRs/feature branches)
|
|
||||||
if git_ref_scope and git_ref_scope != DEFAULT_BRANCH:
|
|
||||||
master_scope = f"platform-{component}-{target}-{DEFAULT_BRANCH}"
|
|
||||||
cache_from_list.append(f"{args.cache_from},scope={master_scope}")
|
|
||||||
|
|
||||||
if "type=gha" in args.cache_to:
|
|
||||||
# Write to both hash-based and branch-based scopes
|
|
||||||
if component_hash:
|
|
||||||
hash_scope = f"platform-{component}-{target}-{component_hash}"
|
|
||||||
cache_to_list.append(f"{args.cache_to},scope={hash_scope}")
|
|
||||||
|
|
||||||
if git_ref_scope:
|
|
||||||
ref_scope = f"platform-{component}-{target}-{git_ref_scope}"
|
|
||||||
cache_to_list.append(f"{args.cache_to},scope={ref_scope}")
|
|
||||||
|
|
||||||
# Ensure we have at least one cache source/target
|
|
||||||
if not cache_from_list:
|
|
||||||
cache_from_list.append(args.cache_from)
|
|
||||||
if not cache_to_list:
|
|
||||||
cache_to_list.append(args.cache_to)
|
|
||||||
|
|
||||||
build_config["cache_from"] = cache_from_list
|
|
||||||
build_config["cache_to"] = cache_to_list
|
|
||||||
modified_services.append(service_name)
|
|
||||||
|
|
||||||
# Write back to the same file
|
|
||||||
with open(args.source, "w") as f:
|
|
||||||
yaml.dump(compose, f, default_flow_style=False, sort_keys=False)
|
|
||||||
|
|
||||||
print(f"Added cache config to {len(modified_services)} services in {args.source}:")
|
|
||||||
for svc in modified_services:
|
|
||||||
svc_config = compose["services"][svc]
|
|
||||||
build_cfg = svc_config.get("build", {})
|
|
||||||
cache_from_list = build_cfg.get("cache_from", ["none"])
|
|
||||||
cache_to_list = build_cfg.get("cache_to", ["none"])
|
|
||||||
print(f" - {svc}")
|
|
||||||
print(f" image: {svc_config.get('image', 'N/A')}")
|
|
||||||
print(f" cache_from: {cache_from_list}")
|
|
||||||
print(f" cache_to: {cache_to_list}")
|
|
||||||
if services_to_dedupe:
|
|
||||||
print(
|
|
||||||
f"Deduplicated {len(services_to_dedupe)} services (will use pre-built images):"
|
|
||||||
)
|
|
||||||
for svc in services_to_dedupe:
|
|
||||||
print(f" - {svc} -> {compose['services'][svc].get('image', 'N/A')}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -180,6 +180,4 @@ autogpt_platform/backend/settings.py
|
|||||||
.claude/settings.local.json
|
.claude/settings.local.json
|
||||||
CLAUDE.local.md
|
CLAUDE.local.md
|
||||||
/autogpt_platform/backend/logs
|
/autogpt_platform/backend/logs
|
||||||
.next
|
.next
|
||||||
# Implementation plans (generated by AI agents)
|
|
||||||
plans/
|
|
||||||
@@ -1,10 +1,3 @@
|
|||||||
default_install_hook_types:
|
|
||||||
- pre-commit
|
|
||||||
- pre-push
|
|
||||||
- post-checkout
|
|
||||||
|
|
||||||
default_stages: [pre-commit]
|
|
||||||
|
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.4.0
|
rev: v4.4.0
|
||||||
@@ -24,7 +17,6 @@ repos:
|
|||||||
name: Detect secrets
|
name: Detect secrets
|
||||||
description: Detects high entropy strings that are likely to be passwords.
|
description: Detects high entropy strings that are likely to be passwords.
|
||||||
files: ^autogpt_platform/
|
files: ^autogpt_platform/
|
||||||
exclude: pnpm-lock\.yaml$
|
|
||||||
stages: [pre-push]
|
stages: [pre-push]
|
||||||
|
|
||||||
- repo: local
|
- repo: local
|
||||||
@@ -34,106 +26,49 @@ repos:
|
|||||||
- id: poetry-install
|
- id: poetry-install
|
||||||
name: Check & Install dependencies - AutoGPT Platform - Backend
|
name: Check & Install dependencies - AutoGPT Platform - Backend
|
||||||
alias: poetry-install-platform-backend
|
alias: poetry-install-platform-backend
|
||||||
|
entry: poetry -C autogpt_platform/backend install
|
||||||
# include autogpt_libs source (since it's a path dependency)
|
# include autogpt_libs source (since it's a path dependency)
|
||||||
entry: >
|
files: ^autogpt_platform/(backend|autogpt_libs)/poetry\.lock$
|
||||||
bash -c '
|
types: [file]
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^autogpt_platform/(backend|autogpt_libs)/poetry\.lock$" || exit 0;
|
|
||||||
poetry -C autogpt_platform/backend install
|
|
||||||
'
|
|
||||||
always_run: true
|
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- id: poetry-install
|
- id: poetry-install
|
||||||
name: Check & Install dependencies - AutoGPT Platform - Libs
|
name: Check & Install dependencies - AutoGPT Platform - Libs
|
||||||
alias: poetry-install-platform-libs
|
alias: poetry-install-platform-libs
|
||||||
entry: >
|
entry: poetry -C autogpt_platform/autogpt_libs install
|
||||||
bash -c '
|
files: ^autogpt_platform/autogpt_libs/poetry\.lock$
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
types: [file]
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^autogpt_platform/autogpt_libs/poetry\.lock$" || exit 0;
|
|
||||||
poetry -C autogpt_platform/autogpt_libs install
|
|
||||||
'
|
|
||||||
always_run: true
|
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- id: pnpm-install
|
|
||||||
name: Check & Install dependencies - AutoGPT Platform - Frontend
|
|
||||||
alias: pnpm-install-platform-frontend
|
|
||||||
entry: >
|
|
||||||
bash -c '
|
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^autogpt_platform/frontend/pnpm-lock\.yaml$" || exit 0;
|
|
||||||
pnpm --prefix autogpt_platform/frontend install
|
|
||||||
'
|
|
||||||
always_run: true
|
|
||||||
language: system
|
|
||||||
pass_filenames: false
|
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- id: poetry-install
|
- id: poetry-install
|
||||||
name: Check & Install dependencies - Classic - AutoGPT
|
name: Check & Install dependencies - Classic - AutoGPT
|
||||||
alias: poetry-install-classic-autogpt
|
alias: poetry-install-classic-autogpt
|
||||||
entry: >
|
entry: poetry -C classic/original_autogpt install
|
||||||
bash -c '
|
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^classic/(original_autogpt|forge)/poetry\.lock$" || exit 0;
|
|
||||||
poetry -C classic/original_autogpt install
|
|
||||||
'
|
|
||||||
# include forge source (since it's a path dependency)
|
# include forge source (since it's a path dependency)
|
||||||
always_run: true
|
files: ^classic/(original_autogpt|forge)/poetry\.lock$
|
||||||
|
types: [file]
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- id: poetry-install
|
- id: poetry-install
|
||||||
name: Check & Install dependencies - Classic - Forge
|
name: Check & Install dependencies - Classic - Forge
|
||||||
alias: poetry-install-classic-forge
|
alias: poetry-install-classic-forge
|
||||||
entry: >
|
entry: poetry -C classic/forge install
|
||||||
bash -c '
|
files: ^classic/forge/poetry\.lock$
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
types: [file]
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^classic/forge/poetry\.lock$" || exit 0;
|
|
||||||
poetry -C classic/forge install
|
|
||||||
'
|
|
||||||
always_run: true
|
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- id: poetry-install
|
- id: poetry-install
|
||||||
name: Check & Install dependencies - Classic - Benchmark
|
name: Check & Install dependencies - Classic - Benchmark
|
||||||
alias: poetry-install-classic-benchmark
|
alias: poetry-install-classic-benchmark
|
||||||
entry: >
|
entry: poetry -C classic/benchmark install
|
||||||
bash -c '
|
files: ^classic/benchmark/poetry\.lock$
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
types: [file]
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^classic/benchmark/poetry\.lock$" || exit 0;
|
|
||||||
poetry -C classic/benchmark install
|
|
||||||
'
|
|
||||||
always_run: true
|
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- repo: local
|
- repo: local
|
||||||
# For proper type checking, Prisma client must be up-to-date.
|
# For proper type checking, Prisma client must be up-to-date.
|
||||||
@@ -141,54 +76,12 @@ repos:
|
|||||||
- id: prisma-generate
|
- id: prisma-generate
|
||||||
name: Prisma Generate - AutoGPT Platform - Backend
|
name: Prisma Generate - AutoGPT Platform - Backend
|
||||||
alias: prisma-generate-platform-backend
|
alias: prisma-generate-platform-backend
|
||||||
entry: >
|
entry: bash -c 'cd autogpt_platform/backend && poetry run prisma generate'
|
||||||
bash -c '
|
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
|
||||||
git diff --name-only "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF"
|
|
||||||
else
|
|
||||||
git diff --cached --name-only
|
|
||||||
fi | grep -qE "^autogpt_platform/((backend|autogpt_libs)/poetry\.lock|backend/schema\.prisma)$" || exit 0;
|
|
||||||
cd autogpt_platform/backend
|
|
||||||
&& poetry run prisma generate
|
|
||||||
&& poetry run gen-prisma-stub
|
|
||||||
'
|
|
||||||
# include everything that triggers poetry install + the prisma schema
|
# include everything that triggers poetry install + the prisma schema
|
||||||
always_run: true
|
files: ^autogpt_platform/((backend|autogpt_libs)/poetry\.lock|backend/schema.prisma)$
|
||||||
|
types: [file]
|
||||||
language: system
|
language: system
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- id: export-api-schema
|
|
||||||
name: Export API schema - AutoGPT Platform - Backend -> Frontend
|
|
||||||
alias: export-api-schema-platform
|
|
||||||
entry: >
|
|
||||||
bash -c '
|
|
||||||
cd autogpt_platform/backend
|
|
||||||
&& poetry run export-api-schema --output ../frontend/src/app/api/openapi.json
|
|
||||||
&& cd ../frontend
|
|
||||||
&& pnpm prettier --write ./src/app/api/openapi.json
|
|
||||||
'
|
|
||||||
files: ^autogpt_platform/backend/
|
|
||||||
language: system
|
|
||||||
pass_filenames: false
|
|
||||||
|
|
||||||
- id: generate-api-client
|
|
||||||
name: Generate API client - AutoGPT Platform - Frontend
|
|
||||||
alias: generate-api-client-platform-frontend
|
|
||||||
entry: >
|
|
||||||
bash -c '
|
|
||||||
SCHEMA=autogpt_platform/frontend/src/app/api/openapi.json;
|
|
||||||
if [ -n "$PRE_COMMIT_FROM_REF" ]; then
|
|
||||||
git diff --quiet "$PRE_COMMIT_FROM_REF" "$PRE_COMMIT_TO_REF" -- "$SCHEMA" && exit 0
|
|
||||||
else
|
|
||||||
git diff --quiet HEAD -- "$SCHEMA" && exit 0
|
|
||||||
fi;
|
|
||||||
cd autogpt_platform/frontend && pnpm generate:api
|
|
||||||
'
|
|
||||||
always_run: true
|
|
||||||
language: system
|
|
||||||
pass_filenames: false
|
|
||||||
stages: [pre-commit, post-checkout]
|
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.7.2
|
rev: v0.7.2
|
||||||
|
|||||||
3
autogpt_platform/.gitignore
vendored
3
autogpt_platform/.gitignore
vendored
@@ -1,3 +1,2 @@
|
|||||||
*.ignore.*
|
*.ignore.*
|
||||||
*.ign.*
|
*.ign.*
|
||||||
.application.logs
|
|
||||||
@@ -45,11 +45,6 @@ AutoGPT Platform is a monorepo containing:
|
|||||||
- Backend/Frontend services use YAML anchors for consistent configuration
|
- Backend/Frontend services use YAML anchors for consistent configuration
|
||||||
- Supabase services (`db/docker/docker-compose.yml`) follow the same pattern
|
- Supabase services (`db/docker/docker-compose.yml`) follow the same pattern
|
||||||
|
|
||||||
### Branching Strategy
|
|
||||||
|
|
||||||
- **`dev`** is the main development branch. All PRs should target `dev`.
|
|
||||||
- **`master`** is the production branch. Only used for production releases.
|
|
||||||
|
|
||||||
### Creating Pull Requests
|
### Creating Pull Requests
|
||||||
|
|
||||||
- Create the PR against the `dev` branch of the repository.
|
- Create the PR against the `dev` branch of the repository.
|
||||||
@@ -60,12 +55,9 @@ AutoGPT Platform is a monorepo containing:
|
|||||||
|
|
||||||
### Reviewing/Revising Pull Requests
|
### Reviewing/Revising Pull Requests
|
||||||
|
|
||||||
Use `/pr-review` to review a PR or `/pr-address` to address comments.
|
- When the user runs /pr-comments or tries to fetch them, also run gh api /repos/Significant-Gravitas/AutoGPT/pulls/[issuenum]/reviews to get the reviews
|
||||||
|
- Use gh api /repos/Significant-Gravitas/AutoGPT/pulls/[issuenum]/reviews/[review_id]/comments to get the review contents
|
||||||
When fetching comments manually:
|
- Use gh api /repos/Significant-Gravitas/AutoGPT/issues/9924/comments to get the pr specific comments
|
||||||
- `gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/reviews` — top-level reviews
|
|
||||||
- `gh api repos/Significant-Gravitas/AutoGPT/pulls/{N}/comments` — inline review comments
|
|
||||||
- `gh api repos/Significant-Gravitas/AutoGPT/issues/{N}/comments` — PR conversation comments
|
|
||||||
|
|
||||||
### Conventional Commits
|
### Conventional Commits
|
||||||
|
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.auth_activities
|
|
||||||
-- Looker source alias: ds49 | Charts: 1
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Tracks authentication events (login, logout, SSO, password
|
|
||||||
-- reset, etc.) from Supabase's internal audit log.
|
|
||||||
-- Useful for monitoring sign-in patterns and detecting anomalies.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- auth.audit_log_entries — Supabase internal auth event log
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- created_at TIMESTAMPTZ When the auth event occurred
|
|
||||||
-- actor_id TEXT User ID who triggered the event
|
|
||||||
-- actor_via_sso TEXT Whether the action was via SSO ('true'/'false')
|
|
||||||
-- action TEXT Event type (e.g. 'login', 'logout', 'token_refreshed')
|
|
||||||
--
|
|
||||||
-- WINDOW
|
|
||||||
-- Rolling 90 days from current date
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Daily login counts
|
|
||||||
-- SELECT DATE_TRUNC('day', created_at) AS day, COUNT(*) AS logins
|
|
||||||
-- FROM analytics.auth_activities
|
|
||||||
-- WHERE action = 'login'
|
|
||||||
-- GROUP BY 1 ORDER BY 1;
|
|
||||||
--
|
|
||||||
-- -- SSO vs password login breakdown
|
|
||||||
-- SELECT actor_via_sso, COUNT(*) FROM analytics.auth_activities
|
|
||||||
-- WHERE action = 'login' GROUP BY 1;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
created_at,
|
|
||||||
payload->>'actor_id' AS actor_id,
|
|
||||||
payload->>'actor_via_sso' AS actor_via_sso,
|
|
||||||
payload->>'action' AS action
|
|
||||||
FROM auth.audit_log_entries
|
|
||||||
WHERE created_at >= NOW() - INTERVAL '90 days'
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.graph_execution
|
|
||||||
-- Looker source alias: ds16 | Charts: 21
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- One row per agent graph execution (last 90 days).
|
|
||||||
-- Unpacks the JSONB stats column into individual numeric columns
|
|
||||||
-- and normalises the executionStatus — runs that failed due to
|
|
||||||
-- insufficient credits are reclassified as 'NO_CREDITS' for
|
|
||||||
-- easier filtering. Error messages are scrubbed of IDs and URLs
|
|
||||||
-- to allow safe grouping.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.AgentGraphExecution — Execution records
|
|
||||||
-- platform.AgentGraph — Agent graph metadata (for name)
|
|
||||||
-- platform.LibraryAgent — To flag possibly-AI (safe-mode) agents
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- id TEXT Execution UUID
|
|
||||||
-- agentGraphId TEXT Agent graph UUID
|
|
||||||
-- agentGraphVersion INT Graph version number
|
|
||||||
-- executionStatus TEXT COMPLETED | FAILED | NO_CREDITS | RUNNING | QUEUED | TERMINATED
|
|
||||||
-- createdAt TIMESTAMPTZ When the execution was queued
|
|
||||||
-- updatedAt TIMESTAMPTZ Last status update time
|
|
||||||
-- userId TEXT Owner user UUID
|
|
||||||
-- agentGraphName TEXT Human-readable agent name
|
|
||||||
-- cputime DECIMAL Total CPU seconds consumed
|
|
||||||
-- walltime DECIMAL Total wall-clock seconds
|
|
||||||
-- node_count DECIMAL Number of nodes in the graph
|
|
||||||
-- nodes_cputime DECIMAL CPU time across all nodes
|
|
||||||
-- nodes_walltime DECIMAL Wall time across all nodes
|
|
||||||
-- execution_cost DECIMAL Credit cost of this execution
|
|
||||||
-- correctness_score FLOAT AI correctness score (if available)
|
|
||||||
-- possibly_ai BOOLEAN True if agent has sensitive_action_safe_mode enabled
|
|
||||||
-- groupedErrorMessage TEXT Scrubbed error string (IDs/URLs replaced with wildcards)
|
|
||||||
--
|
|
||||||
-- WINDOW
|
|
||||||
-- Rolling 90 days (createdAt > CURRENT_DATE - 90 days)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Daily execution counts by status
|
|
||||||
-- SELECT DATE_TRUNC('day', "createdAt") AS day, "executionStatus", COUNT(*)
|
|
||||||
-- FROM analytics.graph_execution
|
|
||||||
-- GROUP BY 1, 2 ORDER BY 1;
|
|
||||||
--
|
|
||||||
-- -- Average cost per execution by agent
|
|
||||||
-- SELECT "agentGraphName", AVG("execution_cost") AS avg_cost, COUNT(*) AS runs
|
|
||||||
-- FROM analytics.graph_execution
|
|
||||||
-- WHERE "executionStatus" = 'COMPLETED'
|
|
||||||
-- GROUP BY 1 ORDER BY avg_cost DESC;
|
|
||||||
--
|
|
||||||
-- -- Top error messages
|
|
||||||
-- SELECT "groupedErrorMessage", COUNT(*) AS occurrences
|
|
||||||
-- FROM analytics.graph_execution
|
|
||||||
-- WHERE "executionStatus" = 'FAILED'
|
|
||||||
-- GROUP BY 1 ORDER BY 2 DESC LIMIT 20;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
ge."id" AS id,
|
|
||||||
ge."agentGraphId" AS agentGraphId,
|
|
||||||
ge."agentGraphVersion" AS agentGraphVersion,
|
|
||||||
CASE
|
|
||||||
WHEN jsonb_exists(ge."stats"::jsonb, 'error')
|
|
||||||
AND (
|
|
||||||
(ge."stats"::jsonb->>'error') ILIKE '%insufficient balance%'
|
|
||||||
OR (ge."stats"::jsonb->>'error') ILIKE '%you have no credits left%'
|
|
||||||
)
|
|
||||||
THEN 'NO_CREDITS'
|
|
||||||
ELSE CAST(ge."executionStatus" AS TEXT)
|
|
||||||
END AS executionStatus,
|
|
||||||
ge."createdAt" AS createdAt,
|
|
||||||
ge."updatedAt" AS updatedAt,
|
|
||||||
ge."userId" AS userId,
|
|
||||||
g."name" AS agentGraphName,
|
|
||||||
(ge."stats"::jsonb->>'cputime')::decimal AS cputime,
|
|
||||||
(ge."stats"::jsonb->>'walltime')::decimal AS walltime,
|
|
||||||
(ge."stats"::jsonb->>'node_count')::decimal AS node_count,
|
|
||||||
(ge."stats"::jsonb->>'nodes_cputime')::decimal AS nodes_cputime,
|
|
||||||
(ge."stats"::jsonb->>'nodes_walltime')::decimal AS nodes_walltime,
|
|
||||||
(ge."stats"::jsonb->>'cost')::decimal AS execution_cost,
|
|
||||||
(ge."stats"::jsonb->>'correctness_score')::float AS correctness_score,
|
|
||||||
COALESCE(la.possibly_ai, FALSE) AS possibly_ai,
|
|
||||||
REGEXP_REPLACE(
|
|
||||||
REGEXP_REPLACE(
|
|
||||||
TRIM(BOTH '"' FROM ge."stats"::jsonb->>'error'),
|
|
||||||
'(https?://)([A-Za-z0-9.-]+)(:[0-9]+)?(/[^\s]*)?',
|
|
||||||
'\1\2/...', 'gi'
|
|
||||||
),
|
|
||||||
'[a-zA-Z0-9_:-]*\d[a-zA-Z0-9_:-]*', '*', 'g'
|
|
||||||
) AS groupedErrorMessage
|
|
||||||
FROM platform."AgentGraphExecution" ge
|
|
||||||
LEFT JOIN platform."AgentGraph" g
|
|
||||||
ON ge."agentGraphId" = g."id"
|
|
||||||
AND ge."agentGraphVersion" = g."version"
|
|
||||||
LEFT JOIN (
|
|
||||||
SELECT DISTINCT ON ("userId", "agentGraphId")
|
|
||||||
"userId", "agentGraphId",
|
|
||||||
("settings"::jsonb->>'sensitive_action_safe_mode')::boolean AS possibly_ai
|
|
||||||
FROM platform."LibraryAgent"
|
|
||||||
WHERE "isDeleted" = FALSE
|
|
||||||
AND "isArchived" = FALSE
|
|
||||||
ORDER BY "userId", "agentGraphId", "agentGraphVersion" DESC
|
|
||||||
) la ON la."userId" = ge."userId" AND la."agentGraphId" = ge."agentGraphId"
|
|
||||||
WHERE ge."createdAt" > CURRENT_DATE - INTERVAL '90 days'
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.node_block_execution
|
|
||||||
-- Looker source alias: ds14 | Charts: 11
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- One row per node (block) execution (last 90 days).
|
|
||||||
-- Unpacks stats JSONB and joins to identify which block type
|
|
||||||
-- was run. For failed nodes, joins the error output and
|
|
||||||
-- scrubs it for safe grouping.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.AgentNodeExecution — Node execution records
|
|
||||||
-- platform.AgentNode — Node → block mapping
|
|
||||||
-- platform.AgentBlock — Block name/ID
|
|
||||||
-- platform.AgentNodeExecutionInputOutput — Error output values
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- id TEXT Node execution UUID
|
|
||||||
-- agentGraphExecutionId TEXT Parent graph execution UUID
|
|
||||||
-- agentNodeId TEXT Node UUID within the graph
|
|
||||||
-- executionStatus TEXT COMPLETED | FAILED | QUEUED | RUNNING | TERMINATED
|
|
||||||
-- addedTime TIMESTAMPTZ When the node was queued
|
|
||||||
-- queuedTime TIMESTAMPTZ When it entered the queue
|
|
||||||
-- startedTime TIMESTAMPTZ When execution started
|
|
||||||
-- endedTime TIMESTAMPTZ When execution finished
|
|
||||||
-- inputSize BIGINT Input payload size in bytes
|
|
||||||
-- outputSize BIGINT Output payload size in bytes
|
|
||||||
-- walltime NUMERIC Wall-clock seconds for this node
|
|
||||||
-- cputime NUMERIC CPU seconds for this node
|
|
||||||
-- llmRetryCount INT Number of LLM retries
|
|
||||||
-- llmCallCount INT Number of LLM API calls made
|
|
||||||
-- inputTokenCount BIGINT LLM input tokens consumed
|
|
||||||
-- outputTokenCount BIGINT LLM output tokens produced
|
|
||||||
-- blockName TEXT Human-readable block name (e.g. 'OpenAIBlock')
|
|
||||||
-- blockId TEXT Block UUID
|
|
||||||
-- groupedErrorMessage TEXT Scrubbed error (IDs/URLs wildcarded)
|
|
||||||
-- errorMessage TEXT Raw error output (only set when FAILED)
|
|
||||||
--
|
|
||||||
-- WINDOW
|
|
||||||
-- Rolling 90 days (addedTime > CURRENT_DATE - 90 days)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Most-used blocks by execution count
|
|
||||||
-- SELECT "blockName", COUNT(*) AS executions,
|
|
||||||
-- COUNT(*) FILTER (WHERE "executionStatus"='FAILED') AS failures
|
|
||||||
-- FROM analytics.node_block_execution
|
|
||||||
-- GROUP BY 1 ORDER BY executions DESC LIMIT 20;
|
|
||||||
--
|
|
||||||
-- -- Average LLM token usage per block
|
|
||||||
-- SELECT "blockName",
|
|
||||||
-- AVG("inputTokenCount") AS avg_input_tokens,
|
|
||||||
-- AVG("outputTokenCount") AS avg_output_tokens
|
|
||||||
-- FROM analytics.node_block_execution
|
|
||||||
-- WHERE "llmCallCount" > 0
|
|
||||||
-- GROUP BY 1 ORDER BY avg_input_tokens DESC;
|
|
||||||
--
|
|
||||||
-- -- Top failure reasons
|
|
||||||
-- SELECT "blockName", "groupedErrorMessage", COUNT(*) AS count
|
|
||||||
-- FROM analytics.node_block_execution
|
|
||||||
-- WHERE "executionStatus" = 'FAILED'
|
|
||||||
-- GROUP BY 1, 2 ORDER BY count DESC LIMIT 20;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
ne."id" AS id,
|
|
||||||
ne."agentGraphExecutionId" AS agentGraphExecutionId,
|
|
||||||
ne."agentNodeId" AS agentNodeId,
|
|
||||||
CAST(ne."executionStatus" AS TEXT) AS executionStatus,
|
|
||||||
ne."addedTime" AS addedTime,
|
|
||||||
ne."queuedTime" AS queuedTime,
|
|
||||||
ne."startedTime" AS startedTime,
|
|
||||||
ne."endedTime" AS endedTime,
|
|
||||||
(ne."stats"::jsonb->>'input_size')::bigint AS inputSize,
|
|
||||||
(ne."stats"::jsonb->>'output_size')::bigint AS outputSize,
|
|
||||||
(ne."stats"::jsonb->>'walltime')::numeric AS walltime,
|
|
||||||
(ne."stats"::jsonb->>'cputime')::numeric AS cputime,
|
|
||||||
(ne."stats"::jsonb->>'llm_retry_count')::int AS llmRetryCount,
|
|
||||||
(ne."stats"::jsonb->>'llm_call_count')::int AS llmCallCount,
|
|
||||||
(ne."stats"::jsonb->>'input_token_count')::bigint AS inputTokenCount,
|
|
||||||
(ne."stats"::jsonb->>'output_token_count')::bigint AS outputTokenCount,
|
|
||||||
b."name" AS blockName,
|
|
||||||
b."id" AS blockId,
|
|
||||||
REGEXP_REPLACE(
|
|
||||||
REGEXP_REPLACE(
|
|
||||||
TRIM(BOTH '"' FROM eio."data"::text),
|
|
||||||
'(https?://)([A-Za-z0-9.-]+)(:[0-9]+)?(/[^\s]*)?',
|
|
||||||
'\1\2/...', 'gi'
|
|
||||||
),
|
|
||||||
'[a-zA-Z0-9_:-]*\d[a-zA-Z0-9_:-]*', '*', 'g'
|
|
||||||
) AS groupedErrorMessage,
|
|
||||||
eio."data" AS errorMessage
|
|
||||||
FROM platform."AgentNodeExecution" ne
|
|
||||||
LEFT JOIN platform."AgentNode" nd
|
|
||||||
ON ne."agentNodeId" = nd."id"
|
|
||||||
LEFT JOIN platform."AgentBlock" b
|
|
||||||
ON nd."agentBlockId" = b."id"
|
|
||||||
LEFT JOIN platform."AgentNodeExecutionInputOutput" eio
|
|
||||||
ON eio."referencedByOutputExecId" = ne."id"
|
|
||||||
AND eio."name" = 'error'
|
|
||||||
AND ne."executionStatus" = 'FAILED'
|
|
||||||
WHERE ne."addedTime" > CURRENT_DATE - INTERVAL '90 days'
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.retention_agent
|
|
||||||
-- Looker source alias: ds35 | Charts: 2
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Weekly cohort retention broken down per individual agent.
|
|
||||||
-- Cohort = week of a user's first use of THAT specific agent.
|
|
||||||
-- Tells you which agents keep users coming back vs. one-shot
|
|
||||||
-- use. Only includes cohorts from the last 180 days.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.AgentGraphExecution — Execution records (user × agent × time)
|
|
||||||
-- platform.AgentGraph — Agent names
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- agent_id TEXT Agent graph UUID
|
|
||||||
-- agent_label TEXT 'AgentName [first8chars]'
|
|
||||||
-- agent_label_n TEXT 'AgentName [first8chars] (n=total_users)'
|
|
||||||
-- cohort_week_start DATE Week users first ran this agent
|
|
||||||
-- cohort_label TEXT ISO week label
|
|
||||||
-- cohort_label_n TEXT ISO week label with cohort size
|
|
||||||
-- user_lifetime_week INT Weeks since first use of this agent
|
|
||||||
-- cohort_users BIGINT Users in this cohort for this agent
|
|
||||||
-- active_users BIGINT Users who ran the agent again in week k
|
|
||||||
-- retention_rate FLOAT active_users / cohort_users
|
|
||||||
-- cohort_users_w0 BIGINT cohort_users only at week 0 (safe to SUM)
|
|
||||||
-- agent_total_users BIGINT Total users across all cohorts for this agent
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Best-retained agents at week 2
|
|
||||||
-- SELECT agent_label, AVG(retention_rate) AS w2_retention
|
|
||||||
-- FROM analytics.retention_agent
|
|
||||||
-- WHERE user_lifetime_week = 2 AND cohort_users >= 10
|
|
||||||
-- GROUP BY 1 ORDER BY w2_retention DESC LIMIT 10;
|
|
||||||
--
|
|
||||||
-- -- Agents with most unique users
|
|
||||||
-- SELECT DISTINCT agent_label, agent_total_users
|
|
||||||
-- FROM analytics.retention_agent
|
|
||||||
-- ORDER BY agent_total_users DESC LIMIT 20;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH params AS (SELECT 12::int AS max_weeks, (CURRENT_DATE - INTERVAL '180 days') AS cohort_start),
|
|
||||||
events AS (
|
|
||||||
SELECT e."userId"::text AS user_id, e."agentGraphId" AS agent_id,
|
|
||||||
e."createdAt"::timestamptz AS created_at,
|
|
||||||
DATE_TRUNC('week', e."createdAt")::date AS week_start
|
|
||||||
FROM platform."AgentGraphExecution" e
|
|
||||||
),
|
|
||||||
first_use AS (
|
|
||||||
SELECT user_id, agent_id, MIN(created_at) AS first_use_at,
|
|
||||||
DATE_TRUNC('week', MIN(created_at))::date AS cohort_week_start
|
|
||||||
FROM events GROUP BY 1,2
|
|
||||||
HAVING MIN(created_at) >= (SELECT cohort_start FROM params)
|
|
||||||
),
|
|
||||||
activity_weeks AS (SELECT DISTINCT user_id, agent_id, week_start FROM events),
|
|
||||||
user_week_age AS (
|
|
||||||
SELECT aw.user_id, aw.agent_id, fu.cohort_week_start,
|
|
||||||
((aw.week_start - DATE_TRUNC('week',fu.first_use_at)::date)/7)::int AS user_lifetime_week
|
|
||||||
FROM activity_weeks aw JOIN first_use fu USING (user_id, agent_id)
|
|
||||||
WHERE aw.week_start >= DATE_TRUNC('week',fu.first_use_at)::date
|
|
||||||
),
|
|
||||||
active_counts AS (
|
|
||||||
SELECT agent_id, cohort_week_start, user_lifetime_week, COUNT(DISTINCT user_id) AS active_users
|
|
||||||
FROM user_week_age WHERE user_lifetime_week >= 0 GROUP BY 1,2,3
|
|
||||||
),
|
|
||||||
cohort_sizes AS (
|
|
||||||
SELECT agent_id, cohort_week_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_use GROUP BY 1,2
|
|
||||||
),
|
|
||||||
cohort_caps AS (
|
|
||||||
SELECT cs.agent_id, cs.cohort_week_start, cs.cohort_users,
|
|
||||||
LEAST((SELECT max_weeks FROM params),
|
|
||||||
GREATEST(0,((DATE_TRUNC('week',CURRENT_DATE)::date-cs.cohort_week_start)/7)::int)) AS cap_weeks
|
|
||||||
FROM cohort_sizes cs
|
|
||||||
),
|
|
||||||
grid AS (
|
|
||||||
SELECT cc.agent_id, cc.cohort_week_start, gs AS user_lifetime_week, cc.cohort_users
|
|
||||||
FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_weeks) gs
|
|
||||||
),
|
|
||||||
agent_names AS (SELECT DISTINCT ON (g."id") g."id" AS agent_id, g."name" AS agent_name FROM platform."AgentGraph" g ORDER BY g."id", g."version" DESC),
|
|
||||||
agent_total_users AS (SELECT agent_id, SUM(cohort_users) AS agent_total_users FROM cohort_sizes GROUP BY 1)
|
|
||||||
SELECT
|
|
||||||
g.agent_id,
|
|
||||||
COALESCE(an.agent_name,'(unnamed)')||' ['||LEFT(g.agent_id::text,8)||']' AS agent_label,
|
|
||||||
COALESCE(an.agent_name,'(unnamed)')||' ['||LEFT(g.agent_id::text,8)||'] (n='||COALESCE(atu.agent_total_users,0)||')' AS agent_label_n,
|
|
||||||
g.cohort_week_start,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW') AS cohort_label,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW')||' (n='||g.cohort_users||')' AS cohort_label_n,
|
|
||||||
g.user_lifetime_week, g.cohort_users,
|
|
||||||
COALESCE(ac.active_users,0) AS active_users,
|
|
||||||
COALESCE(ac.active_users,0)::float / NULLIF(g.cohort_users,0) AS retention_rate,
|
|
||||||
CASE WHEN g.user_lifetime_week=0 THEN g.cohort_users ELSE 0 END AS cohort_users_w0,
|
|
||||||
COALESCE(atu.agent_total_users,0) AS agent_total_users
|
|
||||||
FROM grid g
|
|
||||||
LEFT JOIN active_counts ac ON ac.agent_id=g.agent_id AND ac.cohort_week_start=g.cohort_week_start AND ac.user_lifetime_week=g.user_lifetime_week
|
|
||||||
LEFT JOIN agent_names an ON an.agent_id=g.agent_id
|
|
||||||
LEFT JOIN agent_total_users atu ON atu.agent_id=g.agent_id
|
|
||||||
ORDER BY agent_label, g.cohort_week_start, g.user_lifetime_week;
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.retention_execution_daily
|
|
||||||
-- Looker source alias: ds111 | Charts: 1
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Daily cohort retention based on agent executions.
|
|
||||||
-- Cohort anchor = day of user's FIRST ever execution.
|
|
||||||
-- Only includes cohorts from the last 90 days, up to day 30.
|
|
||||||
-- Great for early engagement analysis (did users run another
|
|
||||||
-- agent the next day?).
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.AgentGraphExecution — Execution records
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- Same pattern as retention_login_daily.
|
|
||||||
-- cohort_day_start = day of first execution (not first login)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Day-3 execution retention
|
|
||||||
-- SELECT cohort_label, retention_rate_bounded AS d3_retention
|
|
||||||
-- FROM analytics.retention_execution_daily
|
|
||||||
-- WHERE user_lifetime_day = 3 ORDER BY cohort_day_start;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH params AS (SELECT 30::int AS max_days, (CURRENT_DATE - INTERVAL '90 days') AS cohort_start),
|
|
||||||
events AS (
|
|
||||||
SELECT e."userId"::text AS user_id, e."createdAt"::timestamptz AS created_at,
|
|
||||||
DATE_TRUNC('day', e."createdAt")::date AS day_start
|
|
||||||
FROM platform."AgentGraphExecution" e WHERE e."userId" IS NOT NULL
|
|
||||||
),
|
|
||||||
first_exec AS (
|
|
||||||
SELECT user_id, MIN(created_at) AS first_exec_at,
|
|
||||||
DATE_TRUNC('day', MIN(created_at))::date AS cohort_day_start
|
|
||||||
FROM events GROUP BY 1
|
|
||||||
HAVING MIN(created_at) >= (SELECT cohort_start FROM params)
|
|
||||||
),
|
|
||||||
activity_days AS (SELECT DISTINCT user_id, day_start FROM events),
|
|
||||||
user_day_age AS (
|
|
||||||
SELECT ad.user_id, fe.cohort_day_start,
|
|
||||||
(ad.day_start - DATE_TRUNC('day',fe.first_exec_at)::date)::int AS user_lifetime_day
|
|
||||||
FROM activity_days ad JOIN first_exec fe USING (user_id)
|
|
||||||
WHERE ad.day_start >= DATE_TRUNC('day',fe.first_exec_at)::date
|
|
||||||
),
|
|
||||||
bounded_counts AS (
|
|
||||||
SELECT cohort_day_start, user_lifetime_day, COUNT(DISTINCT user_id) AS active_users_bounded
|
|
||||||
FROM user_day_age WHERE user_lifetime_day >= 0 GROUP BY 1,2
|
|
||||||
),
|
|
||||||
last_active AS (
|
|
||||||
SELECT cohort_day_start, user_id, MAX(user_lifetime_day) AS last_active_day FROM user_day_age GROUP BY 1,2
|
|
||||||
),
|
|
||||||
unbounded_counts AS (
|
|
||||||
SELECT la.cohort_day_start, gs AS user_lifetime_day, COUNT(*) AS retained_users_unbounded
|
|
||||||
FROM last_active la
|
|
||||||
CROSS JOIN LATERAL generate_series(0, LEAST(la.last_active_day,(SELECT max_days FROM params))) gs
|
|
||||||
GROUP BY 1,2
|
|
||||||
),
|
|
||||||
cohort_sizes AS (SELECT cohort_day_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_exec GROUP BY 1),
|
|
||||||
cohort_caps AS (
|
|
||||||
SELECT cs.cohort_day_start, cs.cohort_users,
|
|
||||||
LEAST((SELECT max_days FROM params), GREATEST(0,(CURRENT_DATE-cs.cohort_day_start)::int)) AS cap_days
|
|
||||||
FROM cohort_sizes cs
|
|
||||||
),
|
|
||||||
grid AS (
|
|
||||||
SELECT cc.cohort_day_start, gs AS user_lifetime_day, cc.cohort_users
|
|
||||||
FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_days) gs
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
g.cohort_day_start,
|
|
||||||
TO_CHAR(g.cohort_day_start,'YYYY-MM-DD') AS cohort_label,
|
|
||||||
TO_CHAR(g.cohort_day_start,'YYYY-MM-DD')||' (n='||g.cohort_users||')' AS cohort_label_n,
|
|
||||||
g.user_lifetime_day, g.cohort_users,
|
|
||||||
COALESCE(b.active_users_bounded,0) AS active_users_bounded,
|
|
||||||
COALESCE(u.retained_users_unbounded,0) AS retained_users_unbounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(b.active_users_bounded,0)::float/g.cohort_users END AS retention_rate_bounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(u.retained_users_unbounded,0)::float/g.cohort_users END AS retention_rate_unbounded,
|
|
||||||
CASE WHEN g.user_lifetime_day=0 THEN g.cohort_users ELSE 0 END AS cohort_users_d0
|
|
||||||
FROM grid g
|
|
||||||
LEFT JOIN bounded_counts b ON b.cohort_day_start=g.cohort_day_start AND b.user_lifetime_day=g.user_lifetime_day
|
|
||||||
LEFT JOIN unbounded_counts u ON u.cohort_day_start=g.cohort_day_start AND u.user_lifetime_day=g.user_lifetime_day
|
|
||||||
ORDER BY g.cohort_day_start, g.user_lifetime_day;
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.retention_execution_weekly
|
|
||||||
-- Looker source alias: ds92 | Charts: 2
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Weekly cohort retention based on agent executions.
|
|
||||||
-- Cohort anchor = week of user's FIRST ever agent execution
|
|
||||||
-- (not first login). Only includes cohorts from the last 180 days.
|
|
||||||
-- Useful when you care about product engagement, not just visits.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.AgentGraphExecution — Execution records
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- Same pattern as retention_login_weekly.
|
|
||||||
-- cohort_week_start = week of first execution (not first login)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Week-2 execution retention
|
|
||||||
-- SELECT cohort_label, retention_rate_bounded
|
|
||||||
-- FROM analytics.retention_execution_weekly
|
|
||||||
-- WHERE user_lifetime_week = 2 ORDER BY cohort_week_start;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH params AS (SELECT 12::int AS max_weeks, (CURRENT_DATE - INTERVAL '180 days') AS cohort_start),
|
|
||||||
events AS (
|
|
||||||
SELECT e."userId"::text AS user_id, e."createdAt"::timestamptz AS created_at,
|
|
||||||
DATE_TRUNC('week', e."createdAt")::date AS week_start
|
|
||||||
FROM platform."AgentGraphExecution" e WHERE e."userId" IS NOT NULL
|
|
||||||
),
|
|
||||||
first_exec AS (
|
|
||||||
SELECT user_id, MIN(created_at) AS first_exec_at,
|
|
||||||
DATE_TRUNC('week', MIN(created_at))::date AS cohort_week_start
|
|
||||||
FROM events GROUP BY 1
|
|
||||||
HAVING MIN(created_at) >= (SELECT cohort_start FROM params)
|
|
||||||
),
|
|
||||||
activity_weeks AS (SELECT DISTINCT user_id, week_start FROM events),
|
|
||||||
user_week_age AS (
|
|
||||||
SELECT aw.user_id, fe.cohort_week_start,
|
|
||||||
((aw.week_start - DATE_TRUNC('week',fe.first_exec_at)::date)/7)::int AS user_lifetime_week
|
|
||||||
FROM activity_weeks aw JOIN first_exec fe USING (user_id)
|
|
||||||
WHERE aw.week_start >= DATE_TRUNC('week',fe.first_exec_at)::date
|
|
||||||
),
|
|
||||||
bounded_counts AS (
|
|
||||||
SELECT cohort_week_start, user_lifetime_week, COUNT(DISTINCT user_id) AS active_users_bounded
|
|
||||||
FROM user_week_age WHERE user_lifetime_week >= 0 GROUP BY 1,2
|
|
||||||
),
|
|
||||||
last_active AS (
|
|
||||||
SELECT cohort_week_start, user_id, MAX(user_lifetime_week) AS last_active_week FROM user_week_age GROUP BY 1,2
|
|
||||||
),
|
|
||||||
unbounded_counts AS (
|
|
||||||
SELECT la.cohort_week_start, gs AS user_lifetime_week, COUNT(*) AS retained_users_unbounded
|
|
||||||
FROM last_active la
|
|
||||||
CROSS JOIN LATERAL generate_series(0, LEAST(la.last_active_week,(SELECT max_weeks FROM params))) gs
|
|
||||||
GROUP BY 1,2
|
|
||||||
),
|
|
||||||
cohort_sizes AS (SELECT cohort_week_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_exec GROUP BY 1),
|
|
||||||
cohort_caps AS (
|
|
||||||
SELECT cs.cohort_week_start, cs.cohort_users,
|
|
||||||
LEAST((SELECT max_weeks FROM params),
|
|
||||||
GREATEST(0,((DATE_TRUNC('week',CURRENT_DATE)::date-cs.cohort_week_start)/7)::int)) AS cap_weeks
|
|
||||||
FROM cohort_sizes cs
|
|
||||||
),
|
|
||||||
grid AS (
|
|
||||||
SELECT cc.cohort_week_start, gs AS user_lifetime_week, cc.cohort_users
|
|
||||||
FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_weeks) gs
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
g.cohort_week_start,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW') AS cohort_label,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW')||' (n='||g.cohort_users||')' AS cohort_label_n,
|
|
||||||
g.user_lifetime_week, g.cohort_users,
|
|
||||||
COALESCE(b.active_users_bounded,0) AS active_users_bounded,
|
|
||||||
COALESCE(u.retained_users_unbounded,0) AS retained_users_unbounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(b.active_users_bounded,0)::float/g.cohort_users END AS retention_rate_bounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(u.retained_users_unbounded,0)::float/g.cohort_users END AS retention_rate_unbounded,
|
|
||||||
CASE WHEN g.user_lifetime_week=0 THEN g.cohort_users ELSE 0 END AS cohort_users_w0
|
|
||||||
FROM grid g
|
|
||||||
LEFT JOIN bounded_counts b ON b.cohort_week_start=g.cohort_week_start AND b.user_lifetime_week=g.user_lifetime_week
|
|
||||||
LEFT JOIN unbounded_counts u ON u.cohort_week_start=g.cohort_week_start AND u.user_lifetime_week=g.user_lifetime_week
|
|
||||||
ORDER BY g.cohort_week_start, g.user_lifetime_week;
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.retention_login_daily
|
|
||||||
-- Looker source alias: ds112 | Charts: 1
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Daily cohort retention based on login sessions.
|
|
||||||
-- Same logic as retention_login_weekly but at day granularity,
|
|
||||||
-- showing up to day 30 for cohorts from the last 90 days.
|
|
||||||
-- Useful for analysing early activation (days 1-7) in detail.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- auth.sessions — Login session records
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS (same pattern as retention_login_weekly)
|
|
||||||
-- cohort_day_start DATE First day the cohort logged in
|
|
||||||
-- cohort_label TEXT Date string (e.g. '2025-03-01')
|
|
||||||
-- cohort_label_n TEXT Date + cohort size (e.g. '2025-03-01 (n=12)')
|
|
||||||
-- user_lifetime_day INT Days since first login (0 = signup day)
|
|
||||||
-- cohort_users BIGINT Total users in cohort
|
|
||||||
-- active_users_bounded BIGINT Users active on exactly day k
|
|
||||||
-- retained_users_unbounded BIGINT Users active any time on/after day k
|
|
||||||
-- retention_rate_bounded FLOAT bounded / cohort_users
|
|
||||||
-- retention_rate_unbounded FLOAT unbounded / cohort_users
|
|
||||||
-- cohort_users_d0 BIGINT cohort_users only at day 0, else 0 (safe to SUM)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Day-1 retention rate (came back next day)
|
|
||||||
-- SELECT cohort_label, retention_rate_bounded AS d1_retention
|
|
||||||
-- FROM analytics.retention_login_daily
|
|
||||||
-- WHERE user_lifetime_day = 1 ORDER BY cohort_day_start;
|
|
||||||
--
|
|
||||||
-- -- Average retention curve across all cohorts
|
|
||||||
-- SELECT user_lifetime_day,
|
|
||||||
-- SUM(active_users_bounded)::float / NULLIF(SUM(cohort_users_d0), 0) AS avg_retention
|
|
||||||
-- FROM analytics.retention_login_daily
|
|
||||||
-- GROUP BY 1 ORDER BY 1;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH params AS (SELECT 30::int AS max_days, (CURRENT_DATE - INTERVAL '90 days')::date AS cohort_start),
|
|
||||||
events AS (
|
|
||||||
SELECT s.user_id::text AS user_id, s.created_at::timestamptz AS created_at,
|
|
||||||
DATE_TRUNC('day', s.created_at)::date AS day_start
|
|
||||||
FROM auth.sessions s WHERE s.user_id IS NOT NULL
|
|
||||||
),
|
|
||||||
first_login AS (
|
|
||||||
SELECT user_id, MIN(created_at) AS first_login_time,
|
|
||||||
DATE_TRUNC('day', MIN(created_at))::date AS cohort_day_start
|
|
||||||
FROM events GROUP BY 1
|
|
||||||
HAVING MIN(created_at) >= (SELECT cohort_start FROM params)
|
|
||||||
),
|
|
||||||
activity_days AS (SELECT DISTINCT user_id, day_start FROM events),
|
|
||||||
user_day_age AS (
|
|
||||||
SELECT ad.user_id, fl.cohort_day_start,
|
|
||||||
(ad.day_start - DATE_TRUNC('day', fl.first_login_time)::date)::int AS user_lifetime_day
|
|
||||||
FROM activity_days ad JOIN first_login fl USING (user_id)
|
|
||||||
WHERE ad.day_start >= DATE_TRUNC('day', fl.first_login_time)::date
|
|
||||||
),
|
|
||||||
bounded_counts AS (
|
|
||||||
SELECT cohort_day_start, user_lifetime_day, COUNT(DISTINCT user_id) AS active_users_bounded
|
|
||||||
FROM user_day_age WHERE user_lifetime_day >= 0 GROUP BY 1,2
|
|
||||||
),
|
|
||||||
last_active AS (
|
|
||||||
SELECT cohort_day_start, user_id, MAX(user_lifetime_day) AS last_active_day FROM user_day_age GROUP BY 1,2
|
|
||||||
),
|
|
||||||
unbounded_counts AS (
|
|
||||||
SELECT la.cohort_day_start, gs AS user_lifetime_day, COUNT(*) AS retained_users_unbounded
|
|
||||||
FROM last_active la
|
|
||||||
CROSS JOIN LATERAL generate_series(0, LEAST(la.last_active_day,(SELECT max_days FROM params))) gs
|
|
||||||
GROUP BY 1,2
|
|
||||||
),
|
|
||||||
cohort_sizes AS (SELECT cohort_day_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_login GROUP BY 1),
|
|
||||||
cohort_caps AS (
|
|
||||||
SELECT cs.cohort_day_start, cs.cohort_users,
|
|
||||||
LEAST((SELECT max_days FROM params), GREATEST(0,(CURRENT_DATE-cs.cohort_day_start)::int)) AS cap_days
|
|
||||||
FROM cohort_sizes cs
|
|
||||||
),
|
|
||||||
grid AS (
|
|
||||||
SELECT cc.cohort_day_start, gs AS user_lifetime_day, cc.cohort_users
|
|
||||||
FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_days) gs
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
g.cohort_day_start,
|
|
||||||
TO_CHAR(g.cohort_day_start,'YYYY-MM-DD') AS cohort_label,
|
|
||||||
TO_CHAR(g.cohort_day_start,'YYYY-MM-DD')||' (n='||g.cohort_users||')' AS cohort_label_n,
|
|
||||||
g.user_lifetime_day, g.cohort_users,
|
|
||||||
COALESCE(b.active_users_bounded,0) AS active_users_bounded,
|
|
||||||
COALESCE(u.retained_users_unbounded,0) AS retained_users_unbounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(b.active_users_bounded,0)::float/g.cohort_users END AS retention_rate_bounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(u.retained_users_unbounded,0)::float/g.cohort_users END AS retention_rate_unbounded,
|
|
||||||
CASE WHEN g.user_lifetime_day=0 THEN g.cohort_users ELSE 0 END AS cohort_users_d0
|
|
||||||
FROM grid g
|
|
||||||
LEFT JOIN bounded_counts b ON b.cohort_day_start=g.cohort_day_start AND b.user_lifetime_day=g.user_lifetime_day
|
|
||||||
LEFT JOIN unbounded_counts u ON u.cohort_day_start=g.cohort_day_start AND u.user_lifetime_day=g.user_lifetime_day
|
|
||||||
ORDER BY g.cohort_day_start, g.user_lifetime_day;
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.retention_login_onboarded_weekly
|
|
||||||
-- Looker source alias: ds101 | Charts: 2
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Weekly cohort retention from login sessions, restricted to
|
|
||||||
-- users who "onboarded" — defined as running at least one
|
|
||||||
-- agent within 365 days of their first login.
|
|
||||||
-- Filters out users who signed up but never activated,
|
|
||||||
-- giving a cleaner view of engaged-user retention.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- auth.sessions — Login session records
|
|
||||||
-- platform.AgentGraphExecution — Used to identify onboarders
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- Same as retention_login_weekly (cohort_week_start, user_lifetime_week,
|
|
||||||
-- retention_rate_bounded, retention_rate_unbounded, etc.)
|
|
||||||
-- Only difference: cohort is filtered to onboarded users only.
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Compare week-4 retention: all users vs onboarded only
|
|
||||||
-- SELECT 'all_users' AS segment, AVG(retention_rate_bounded) AS w4_retention
|
|
||||||
-- FROM analytics.retention_login_weekly WHERE user_lifetime_week = 4
|
|
||||||
-- UNION ALL
|
|
||||||
-- SELECT 'onboarded', AVG(retention_rate_bounded)
|
|
||||||
-- FROM analytics.retention_login_onboarded_weekly WHERE user_lifetime_week = 4;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH params AS (SELECT 12::int AS max_weeks, 365::int AS onboarding_window_days),
|
|
||||||
events AS (
|
|
||||||
SELECT s.user_id::text AS user_id, s.created_at::timestamptz AS created_at,
|
|
||||||
DATE_TRUNC('week', s.created_at)::date AS week_start
|
|
||||||
FROM auth.sessions s WHERE s.user_id IS NOT NULL
|
|
||||||
),
|
|
||||||
first_login_all AS (
|
|
||||||
SELECT user_id, MIN(created_at) AS first_login_time,
|
|
||||||
DATE_TRUNC('week', MIN(created_at))::date AS cohort_week_start
|
|
||||||
FROM events GROUP BY 1
|
|
||||||
),
|
|
||||||
onboarders AS (
|
|
||||||
SELECT fl.user_id FROM first_login_all fl
|
|
||||||
WHERE EXISTS (
|
|
||||||
SELECT 1 FROM platform."AgentGraphExecution" e
|
|
||||||
WHERE e."userId"::text = fl.user_id
|
|
||||||
AND e."createdAt" >= fl.first_login_time
|
|
||||||
AND e."createdAt" < fl.first_login_time
|
|
||||||
+ make_interval(days => (SELECT onboarding_window_days FROM params))
|
|
||||||
)
|
|
||||||
),
|
|
||||||
first_login AS (SELECT * FROM first_login_all WHERE user_id IN (SELECT user_id FROM onboarders)),
|
|
||||||
activity_weeks AS (SELECT DISTINCT user_id, week_start FROM events),
|
|
||||||
user_week_age AS (
|
|
||||||
SELECT aw.user_id, fl.cohort_week_start,
|
|
||||||
((aw.week_start - DATE_TRUNC('week',fl.first_login_time)::date)/7)::int AS user_lifetime_week
|
|
||||||
FROM activity_weeks aw JOIN first_login fl USING (user_id)
|
|
||||||
WHERE aw.week_start >= DATE_TRUNC('week',fl.first_login_time)::date
|
|
||||||
),
|
|
||||||
bounded_counts AS (
|
|
||||||
SELECT cohort_week_start, user_lifetime_week, COUNT(DISTINCT user_id) AS active_users_bounded
|
|
||||||
FROM user_week_age WHERE user_lifetime_week >= 0 GROUP BY 1,2
|
|
||||||
),
|
|
||||||
last_active AS (
|
|
||||||
SELECT cohort_week_start, user_id, MAX(user_lifetime_week) AS last_active_week FROM user_week_age GROUP BY 1,2
|
|
||||||
),
|
|
||||||
unbounded_counts AS (
|
|
||||||
SELECT la.cohort_week_start, gs AS user_lifetime_week, COUNT(*) AS retained_users_unbounded
|
|
||||||
FROM last_active la
|
|
||||||
CROSS JOIN LATERAL generate_series(0, LEAST(la.last_active_week,(SELECT max_weeks FROM params))) gs
|
|
||||||
GROUP BY 1,2
|
|
||||||
),
|
|
||||||
cohort_sizes AS (SELECT cohort_week_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_login GROUP BY 1),
|
|
||||||
cohort_caps AS (
|
|
||||||
SELECT cs.cohort_week_start, cs.cohort_users,
|
|
||||||
LEAST((SELECT max_weeks FROM params),
|
|
||||||
GREATEST(0,((DATE_TRUNC('week',CURRENT_DATE)::date-cs.cohort_week_start)/7)::int)) AS cap_weeks
|
|
||||||
FROM cohort_sizes cs
|
|
||||||
),
|
|
||||||
grid AS (
|
|
||||||
SELECT cc.cohort_week_start, gs AS user_lifetime_week, cc.cohort_users
|
|
||||||
FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_weeks) gs
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
g.cohort_week_start,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW') AS cohort_label,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW')||' (n='||g.cohort_users||')' AS cohort_label_n,
|
|
||||||
g.user_lifetime_week, g.cohort_users,
|
|
||||||
COALESCE(b.active_users_bounded,0) AS active_users_bounded,
|
|
||||||
COALESCE(u.retained_users_unbounded,0) AS retained_users_unbounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(b.active_users_bounded,0)::float/g.cohort_users END AS retention_rate_bounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(u.retained_users_unbounded,0)::float/g.cohort_users END AS retention_rate_unbounded,
|
|
||||||
CASE WHEN g.user_lifetime_week=0 THEN g.cohort_users ELSE 0 END AS cohort_users_w0
|
|
||||||
FROM grid g
|
|
||||||
LEFT JOIN bounded_counts b ON b.cohort_week_start=g.cohort_week_start AND b.user_lifetime_week=g.user_lifetime_week
|
|
||||||
LEFT JOIN unbounded_counts u ON u.cohort_week_start=g.cohort_week_start AND u.user_lifetime_week=g.user_lifetime_week
|
|
||||||
ORDER BY g.cohort_week_start, g.user_lifetime_week;
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.retention_login_weekly
|
|
||||||
-- Looker source alias: ds83 | Charts: 2
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Weekly cohort retention based on login sessions.
|
|
||||||
-- Users are grouped by the ISO week of their first ever login.
|
|
||||||
-- For each cohort × lifetime-week combination, outputs both:
|
|
||||||
-- - bounded rate: % active in exactly that week
|
|
||||||
-- - unbounded rate: % who were ever active on or after that week
|
|
||||||
-- Weeks are capped to the cohort's actual age (no future data points).
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- auth.sessions — Login session records
|
|
||||||
--
|
|
||||||
-- HOW TO READ THE OUTPUT
|
|
||||||
-- cohort_week_start The Monday of the week users first logged in
|
|
||||||
-- user_lifetime_week 0 = signup week, 1 = one week later, etc.
|
|
||||||
-- retention_rate_bounded = active_users_bounded / cohort_users
|
|
||||||
-- retention_rate_unbounded = retained_users_unbounded / cohort_users
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- cohort_week_start DATE First day of the cohort's signup week
|
|
||||||
-- cohort_label TEXT ISO week label (e.g. '2025-W01')
|
|
||||||
-- cohort_label_n TEXT ISO week label with cohort size (e.g. '2025-W01 (n=42)')
|
|
||||||
-- user_lifetime_week INT Weeks since first login (0 = signup week)
|
|
||||||
-- cohort_users BIGINT Total users in this cohort (denominator)
|
|
||||||
-- active_users_bounded BIGINT Users active in exactly week k
|
|
||||||
-- retained_users_unbounded BIGINT Users active any time on/after week k
|
|
||||||
-- retention_rate_bounded FLOAT bounded active / cohort_users
|
|
||||||
-- retention_rate_unbounded FLOAT unbounded retained / cohort_users
|
|
||||||
-- cohort_users_w0 BIGINT cohort_users only at week 0, else 0 (safe to SUM in pivot tables)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Week-1 retention rate per cohort
|
|
||||||
-- SELECT cohort_label, retention_rate_bounded AS w1_retention
|
|
||||||
-- FROM analytics.retention_login_weekly
|
|
||||||
-- WHERE user_lifetime_week = 1
|
|
||||||
-- ORDER BY cohort_week_start;
|
|
||||||
--
|
|
||||||
-- -- Overall average retention curve (all cohorts combined)
|
|
||||||
-- SELECT user_lifetime_week,
|
|
||||||
-- SUM(active_users_bounded)::float / NULLIF(SUM(cohort_users_w0), 0) AS avg_retention
|
|
||||||
-- FROM analytics.retention_login_weekly
|
|
||||||
-- GROUP BY 1 ORDER BY 1;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH params AS (SELECT 12::int AS max_weeks),
|
|
||||||
events AS (
|
|
||||||
SELECT s.user_id::text AS user_id, s.created_at::timestamptz AS created_at,
|
|
||||||
DATE_TRUNC('week', s.created_at)::date AS week_start
|
|
||||||
FROM auth.sessions s WHERE s.user_id IS NOT NULL
|
|
||||||
),
|
|
||||||
first_login AS (
|
|
||||||
SELECT user_id, MIN(created_at) AS first_login_time,
|
|
||||||
DATE_TRUNC('week', MIN(created_at))::date AS cohort_week_start
|
|
||||||
FROM events GROUP BY 1
|
|
||||||
),
|
|
||||||
activity_weeks AS (SELECT DISTINCT user_id, week_start FROM events),
|
|
||||||
user_week_age AS (
|
|
||||||
SELECT aw.user_id, fl.cohort_week_start,
|
|
||||||
((aw.week_start - DATE_TRUNC('week', fl.first_login_time)::date) / 7)::int AS user_lifetime_week
|
|
||||||
FROM activity_weeks aw JOIN first_login fl USING (user_id)
|
|
||||||
WHERE aw.week_start >= DATE_TRUNC('week', fl.first_login_time)::date
|
|
||||||
),
|
|
||||||
bounded_counts AS (
|
|
||||||
SELECT cohort_week_start, user_lifetime_week, COUNT(DISTINCT user_id) AS active_users_bounded
|
|
||||||
FROM user_week_age WHERE user_lifetime_week >= 0 GROUP BY 1,2
|
|
||||||
),
|
|
||||||
last_active AS (
|
|
||||||
SELECT cohort_week_start, user_id, MAX(user_lifetime_week) AS last_active_week FROM user_week_age GROUP BY 1,2
|
|
||||||
),
|
|
||||||
unbounded_counts AS (
|
|
||||||
SELECT la.cohort_week_start, gs AS user_lifetime_week, COUNT(*) AS retained_users_unbounded
|
|
||||||
FROM last_active la
|
|
||||||
CROSS JOIN LATERAL generate_series(0, LEAST(la.last_active_week,(SELECT max_weeks FROM params))) gs
|
|
||||||
GROUP BY 1,2
|
|
||||||
),
|
|
||||||
cohort_sizes AS (SELECT cohort_week_start, COUNT(DISTINCT user_id) AS cohort_users FROM first_login GROUP BY 1),
|
|
||||||
cohort_caps AS (
|
|
||||||
SELECT cs.cohort_week_start, cs.cohort_users,
|
|
||||||
LEAST((SELECT max_weeks FROM params),
|
|
||||||
GREATEST(0,((DATE_TRUNC('week',CURRENT_DATE)::date - cs.cohort_week_start)/7)::int)) AS cap_weeks
|
|
||||||
FROM cohort_sizes cs
|
|
||||||
),
|
|
||||||
grid AS (
|
|
||||||
SELECT cc.cohort_week_start, gs AS user_lifetime_week, cc.cohort_users
|
|
||||||
FROM cohort_caps cc CROSS JOIN LATERAL generate_series(0, cc.cap_weeks) gs
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
g.cohort_week_start,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW') AS cohort_label,
|
|
||||||
TO_CHAR(g.cohort_week_start,'IYYY-"W"IW')||' (n='||g.cohort_users||')' AS cohort_label_n,
|
|
||||||
g.user_lifetime_week, g.cohort_users,
|
|
||||||
COALESCE(b.active_users_bounded,0) AS active_users_bounded,
|
|
||||||
COALESCE(u.retained_users_unbounded,0) AS retained_users_unbounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(b.active_users_bounded,0)::float/g.cohort_users END AS retention_rate_bounded,
|
|
||||||
CASE WHEN g.cohort_users>0 THEN COALESCE(u.retained_users_unbounded,0)::float/g.cohort_users END AS retention_rate_unbounded,
|
|
||||||
CASE WHEN g.user_lifetime_week=0 THEN g.cohort_users ELSE 0 END AS cohort_users_w0
|
|
||||||
FROM grid g
|
|
||||||
LEFT JOIN bounded_counts b ON b.cohort_week_start=g.cohort_week_start AND b.user_lifetime_week=g.user_lifetime_week
|
|
||||||
LEFT JOIN unbounded_counts u ON u.cohort_week_start=g.cohort_week_start AND u.user_lifetime_week=g.user_lifetime_week
|
|
||||||
ORDER BY g.cohort_week_start, g.user_lifetime_week
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.user_block_spending
|
|
||||||
-- Looker source alias: ds6 | Charts: 5
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- One row per credit transaction (last 90 days).
|
|
||||||
-- Shows how users spend credits broken down by block type,
|
|
||||||
-- LLM provider and model. Joins node execution stats for
|
|
||||||
-- token-level detail.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.CreditTransaction — Credit debit/credit records
|
|
||||||
-- platform.AgentNodeExecution — Node execution stats (for token counts)
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- transactionKey TEXT Unique transaction identifier
|
|
||||||
-- userId TEXT User who was charged
|
|
||||||
-- amount DECIMAL Credit amount (positive = credit, negative = debit)
|
|
||||||
-- negativeAmount DECIMAL amount * -1 (convenience for spend charts)
|
|
||||||
-- transactionType TEXT Transaction type (e.g. 'USAGE', 'REFUND', 'TOP_UP')
|
|
||||||
-- transactionTime TIMESTAMPTZ When the transaction was recorded
|
|
||||||
-- blockId TEXT Block UUID that triggered the spend
|
|
||||||
-- blockName TEXT Human-readable block name
|
|
||||||
-- llm_provider TEXT LLM provider (e.g. 'openai', 'anthropic')
|
|
||||||
-- llm_model TEXT Model name (e.g. 'gpt-4o', 'claude-3-5-sonnet')
|
|
||||||
-- node_exec_id TEXT Linked node execution UUID
|
|
||||||
-- llm_call_count INT LLM API calls made in that execution
|
|
||||||
-- llm_retry_count INT LLM retries in that execution
|
|
||||||
-- llm_input_token_count INT Input tokens consumed
|
|
||||||
-- llm_output_token_count INT Output tokens produced
|
|
||||||
--
|
|
||||||
-- WINDOW
|
|
||||||
-- Rolling 90 days (createdAt > CURRENT_DATE - 90 days)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Total spend per user (last 90 days)
|
|
||||||
-- SELECT "userId", SUM("negativeAmount") AS total_spent
|
|
||||||
-- FROM analytics.user_block_spending
|
|
||||||
-- WHERE "transactionType" = 'USAGE'
|
|
||||||
-- GROUP BY 1 ORDER BY total_spent DESC;
|
|
||||||
--
|
|
||||||
-- -- Spend by LLM provider + model
|
|
||||||
-- SELECT "llm_provider", "llm_model",
|
|
||||||
-- SUM("negativeAmount") AS total_cost,
|
|
||||||
-- SUM("llm_input_token_count") AS input_tokens,
|
|
||||||
-- SUM("llm_output_token_count") AS output_tokens
|
|
||||||
-- FROM analytics.user_block_spending
|
|
||||||
-- WHERE "llm_provider" IS NOT NULL
|
|
||||||
-- GROUP BY 1, 2 ORDER BY total_cost DESC;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
c."transactionKey" AS transactionKey,
|
|
||||||
c."userId" AS userId,
|
|
||||||
c."amount" AS amount,
|
|
||||||
c."amount" * -1 AS negativeAmount,
|
|
||||||
c."type" AS transactionType,
|
|
||||||
c."createdAt" AS transactionTime,
|
|
||||||
c.metadata->>'block_id' AS blockId,
|
|
||||||
c.metadata->>'block' AS blockName,
|
|
||||||
c.metadata->'input'->'credentials'->>'provider' AS llm_provider,
|
|
||||||
c.metadata->'input'->>'model' AS llm_model,
|
|
||||||
c.metadata->>'node_exec_id' AS node_exec_id,
|
|
||||||
(ne."stats"->>'llm_call_count')::int AS llm_call_count,
|
|
||||||
(ne."stats"->>'llm_retry_count')::int AS llm_retry_count,
|
|
||||||
(ne."stats"->>'input_token_count')::int AS llm_input_token_count,
|
|
||||||
(ne."stats"->>'output_token_count')::int AS llm_output_token_count
|
|
||||||
FROM platform."CreditTransaction" c
|
|
||||||
LEFT JOIN platform."AgentNodeExecution" ne
|
|
||||||
ON (c.metadata->>'node_exec_id') = ne."id"::text
|
|
||||||
WHERE c."createdAt" > CURRENT_DATE - INTERVAL '90 days'
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.user_onboarding
|
|
||||||
-- Looker source alias: ds68 | Charts: 3
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- One row per user onboarding record. Contains the user's
|
|
||||||
-- stated usage reason, selected integrations, completed
|
|
||||||
-- onboarding steps and optional first agent selection.
|
|
||||||
-- Full history (no date filter) since onboarding happens
|
|
||||||
-- once per user.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.UserOnboarding — Onboarding state per user
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- id TEXT Onboarding record UUID
|
|
||||||
-- createdAt TIMESTAMPTZ When onboarding started
|
|
||||||
-- updatedAt TIMESTAMPTZ Last update to onboarding state
|
|
||||||
-- usageReason TEXT Why user signed up (e.g. 'work', 'personal')
|
|
||||||
-- integrations TEXT[] Array of integration names the user selected
|
|
||||||
-- userId TEXT User UUID
|
|
||||||
-- completedSteps TEXT[] Array of onboarding step enums completed
|
|
||||||
-- selectedStoreListingVersionId TEXT First marketplace agent the user chose (if any)
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Usage reason breakdown
|
|
||||||
-- SELECT "usageReason", COUNT(*) FROM analytics.user_onboarding GROUP BY 1;
|
|
||||||
--
|
|
||||||
-- -- Completion rate per step
|
|
||||||
-- SELECT step, COUNT(*) AS users_completed
|
|
||||||
-- FROM analytics.user_onboarding
|
|
||||||
-- CROSS JOIN LATERAL UNNEST("completedSteps") AS step
|
|
||||||
-- GROUP BY 1 ORDER BY users_completed DESC;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
SELECT
|
|
||||||
id,
|
|
||||||
"createdAt",
|
|
||||||
"updatedAt",
|
|
||||||
"usageReason",
|
|
||||||
integrations,
|
|
||||||
"userId",
|
|
||||||
"completedSteps",
|
|
||||||
"selectedStoreListingVersionId"
|
|
||||||
FROM platform."UserOnboarding"
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.user_onboarding_funnel
|
|
||||||
-- Looker source alias: ds74 | Charts: 1
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Pre-aggregated onboarding funnel showing how many users
|
|
||||||
-- completed each step and the drop-off percentage from the
|
|
||||||
-- previous step. One row per onboarding step (all 22 steps
|
|
||||||
-- always present, even with 0 completions — prevents sparse
|
|
||||||
-- gaps from making LAG compare the wrong predecessors).
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.UserOnboarding — Onboarding records with completedSteps array
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- step TEXT Onboarding step enum name (e.g. 'WELCOME', 'CONGRATS')
|
|
||||||
-- step_order INT Numeric position in the funnel (1=first, 22=last)
|
|
||||||
-- users_completed BIGINT Distinct users who completed this step
|
|
||||||
-- pct_from_prev NUMERIC % of users from the previous step who reached this one
|
|
||||||
--
|
|
||||||
-- STEP ORDER
|
|
||||||
-- 1 WELCOME 9 MARKETPLACE_VISIT 17 SCHEDULE_AGENT
|
|
||||||
-- 2 USAGE_REASON 10 MARKETPLACE_ADD_AGENT 18 RUN_AGENTS
|
|
||||||
-- 3 INTEGRATIONS 11 MARKETPLACE_RUN_AGENT 19 RUN_3_DAYS
|
|
||||||
-- 4 AGENT_CHOICE 12 BUILDER_OPEN 20 TRIGGER_WEBHOOK
|
|
||||||
-- 5 AGENT_NEW_RUN 13 BUILDER_SAVE_AGENT 21 RUN_14_DAYS
|
|
||||||
-- 6 AGENT_INPUT 14 BUILDER_RUN_AGENT 22 RUN_AGENTS_100
|
|
||||||
-- 7 CONGRATS 15 VISIT_COPILOT
|
|
||||||
-- 8 GET_RESULTS 16 RE_RUN_AGENT
|
|
||||||
--
|
|
||||||
-- WINDOW
|
|
||||||
-- Users who started onboarding in the last 90 days
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Full funnel
|
|
||||||
-- SELECT * FROM analytics.user_onboarding_funnel ORDER BY step_order;
|
|
||||||
--
|
|
||||||
-- -- Biggest drop-off point
|
|
||||||
-- SELECT step, pct_from_prev FROM analytics.user_onboarding_funnel
|
|
||||||
-- ORDER BY pct_from_prev ASC LIMIT 3;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH all_steps AS (
|
|
||||||
-- Complete ordered grid of all 22 steps so zero-completion steps
|
|
||||||
-- are always present, keeping LAG comparisons correct.
|
|
||||||
SELECT step_name, step_order
|
|
||||||
FROM (VALUES
|
|
||||||
('WELCOME', 1),
|
|
||||||
('USAGE_REASON', 2),
|
|
||||||
('INTEGRATIONS', 3),
|
|
||||||
('AGENT_CHOICE', 4),
|
|
||||||
('AGENT_NEW_RUN', 5),
|
|
||||||
('AGENT_INPUT', 6),
|
|
||||||
('CONGRATS', 7),
|
|
||||||
('GET_RESULTS', 8),
|
|
||||||
('MARKETPLACE_VISIT', 9),
|
|
||||||
('MARKETPLACE_ADD_AGENT', 10),
|
|
||||||
('MARKETPLACE_RUN_AGENT', 11),
|
|
||||||
('BUILDER_OPEN', 12),
|
|
||||||
('BUILDER_SAVE_AGENT', 13),
|
|
||||||
('BUILDER_RUN_AGENT', 14),
|
|
||||||
('VISIT_COPILOT', 15),
|
|
||||||
('RE_RUN_AGENT', 16),
|
|
||||||
('SCHEDULE_AGENT', 17),
|
|
||||||
('RUN_AGENTS', 18),
|
|
||||||
('RUN_3_DAYS', 19),
|
|
||||||
('TRIGGER_WEBHOOK', 20),
|
|
||||||
('RUN_14_DAYS', 21),
|
|
||||||
('RUN_AGENTS_100', 22)
|
|
||||||
) AS t(step_name, step_order)
|
|
||||||
),
|
|
||||||
raw AS (
|
|
||||||
SELECT
|
|
||||||
u."userId",
|
|
||||||
step_txt::text AS step
|
|
||||||
FROM platform."UserOnboarding" u
|
|
||||||
CROSS JOIN LATERAL UNNEST(u."completedSteps") AS step_txt
|
|
||||||
WHERE u."createdAt" >= CURRENT_DATE - INTERVAL '90 days'
|
|
||||||
),
|
|
||||||
step_counts AS (
|
|
||||||
SELECT step, COUNT(DISTINCT "userId") AS users_completed
|
|
||||||
FROM raw GROUP BY step
|
|
||||||
),
|
|
||||||
funnel AS (
|
|
||||||
SELECT
|
|
||||||
a.step_name AS step,
|
|
||||||
a.step_order,
|
|
||||||
COALESCE(sc.users_completed, 0) AS users_completed,
|
|
||||||
ROUND(
|
|
||||||
100.0 * COALESCE(sc.users_completed, 0)
|
|
||||||
/ NULLIF(
|
|
||||||
LAG(COALESCE(sc.users_completed, 0)) OVER (ORDER BY a.step_order),
|
|
||||||
0
|
|
||||||
),
|
|
||||||
2
|
|
||||||
) AS pct_from_prev
|
|
||||||
FROM all_steps a
|
|
||||||
LEFT JOIN step_counts sc ON sc.step = a.step_name
|
|
||||||
)
|
|
||||||
SELECT * FROM funnel ORDER BY step_order
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.user_onboarding_integration
|
|
||||||
-- Looker source alias: ds75 | Charts: 1
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- Pre-aggregated count of users who selected each integration
|
|
||||||
-- during onboarding. One row per integration type, sorted
|
|
||||||
-- by popularity.
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- platform.UserOnboarding — integrations array column
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- integration TEXT Integration name (e.g. 'github', 'slack', 'notion')
|
|
||||||
-- users_with_integration BIGINT Distinct users who selected this integration
|
|
||||||
--
|
|
||||||
-- WINDOW
|
|
||||||
-- Users who started onboarding in the last 90 days
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Full integration popularity ranking
|
|
||||||
-- SELECT * FROM analytics.user_onboarding_integration;
|
|
||||||
--
|
|
||||||
-- -- Top 5 integrations
|
|
||||||
-- SELECT * FROM analytics.user_onboarding_integration LIMIT 5;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH exploded AS (
|
|
||||||
SELECT
|
|
||||||
u."userId" AS user_id,
|
|
||||||
UNNEST(u."integrations") AS integration
|
|
||||||
FROM platform."UserOnboarding" u
|
|
||||||
WHERE u."createdAt" >= CURRENT_DATE - INTERVAL '90 days'
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
integration,
|
|
||||||
COUNT(DISTINCT user_id) AS users_with_integration
|
|
||||||
FROM exploded
|
|
||||||
WHERE integration IS NOT NULL AND integration <> ''
|
|
||||||
GROUP BY integration
|
|
||||||
ORDER BY users_with_integration DESC
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
-- =============================================================
|
|
||||||
-- View: analytics.users_activities
|
|
||||||
-- Looker source alias: ds56 | Charts: 5
|
|
||||||
-- =============================================================
|
|
||||||
-- DESCRIPTION
|
|
||||||
-- One row per user with lifetime activity summary.
|
|
||||||
-- Joins login sessions with agent graphs, executions and
|
|
||||||
-- node-level runs to give a full picture of how engaged
|
|
||||||
-- each user is. Includes a convenience flag for 7-day
|
|
||||||
-- activation (did the user return at least 7 days after
|
|
||||||
-- their first login?).
|
|
||||||
--
|
|
||||||
-- SOURCE TABLES
|
|
||||||
-- auth.sessions — Login/session records
|
|
||||||
-- platform.AgentGraph — Graphs (agents) built by the user
|
|
||||||
-- platform.AgentGraphExecution — Agent run history
|
|
||||||
-- platform.AgentNodeExecution — Individual block execution history
|
|
||||||
--
|
|
||||||
-- PERFORMANCE NOTE
|
|
||||||
-- Each CTE aggregates its own table independently by userId.
|
|
||||||
-- This avoids the fan-out that occurs when driving every join
|
|
||||||
-- from user_logins across the two largest tables
|
|
||||||
-- (AgentGraphExecution and AgentNodeExecution).
|
|
||||||
--
|
|
||||||
-- OUTPUT COLUMNS
|
|
||||||
-- user_id TEXT Supabase user UUID
|
|
||||||
-- first_login_time TIMESTAMPTZ First ever session created_at
|
|
||||||
-- last_login_time TIMESTAMPTZ Most recent session created_at
|
|
||||||
-- last_visit_time TIMESTAMPTZ Max of last refresh or login
|
|
||||||
-- last_agent_save_time TIMESTAMPTZ Last time user saved an agent graph
|
|
||||||
-- agent_count BIGINT Number of distinct active graphs built (0 if none)
|
|
||||||
-- first_agent_run_time TIMESTAMPTZ First ever graph execution
|
|
||||||
-- last_agent_run_time TIMESTAMPTZ Most recent graph execution
|
|
||||||
-- unique_agent_runs BIGINT Distinct agent graphs ever run (0 if none)
|
|
||||||
-- agent_runs BIGINT Total graph execution count (0 if none)
|
|
||||||
-- node_execution_count BIGINT Total node executions across all runs
|
|
||||||
-- node_execution_failed BIGINT Node executions with FAILED status
|
|
||||||
-- node_execution_completed BIGINT Node executions with COMPLETED status
|
|
||||||
-- node_execution_terminated BIGINT Node executions with TERMINATED status
|
|
||||||
-- node_execution_queued BIGINT Node executions with QUEUED status
|
|
||||||
-- node_execution_running BIGINT Node executions with RUNNING status
|
|
||||||
-- is_active_after_7d INT 1=returned after day 7, 0=did not, NULL=too early to tell
|
|
||||||
-- node_execution_incomplete BIGINT Node executions with INCOMPLETE status
|
|
||||||
-- node_execution_review BIGINT Node executions with REVIEW status
|
|
||||||
--
|
|
||||||
-- EXAMPLE QUERIES
|
|
||||||
-- -- Users who ran at least one agent and returned after 7 days
|
|
||||||
-- SELECT COUNT(*) FROM analytics.users_activities
|
|
||||||
-- WHERE agent_runs > 0 AND is_active_after_7d = 1;
|
|
||||||
--
|
|
||||||
-- -- Top 10 most active users by agent runs
|
|
||||||
-- SELECT user_id, agent_runs, node_execution_count
|
|
||||||
-- FROM analytics.users_activities
|
|
||||||
-- ORDER BY agent_runs DESC LIMIT 10;
|
|
||||||
--
|
|
||||||
-- -- 7-day activation rate
|
|
||||||
-- SELECT
|
|
||||||
-- SUM(CASE WHEN is_active_after_7d = 1 THEN 1 ELSE 0 END)::float
|
|
||||||
-- / NULLIF(COUNT(CASE WHEN is_active_after_7d IS NOT NULL THEN 1 END), 0)
|
|
||||||
-- AS activation_rate
|
|
||||||
-- FROM analytics.users_activities;
|
|
||||||
-- =============================================================
|
|
||||||
|
|
||||||
WITH user_logins AS (
|
|
||||||
SELECT
|
|
||||||
user_id::text AS user_id,
|
|
||||||
MIN(created_at) AS first_login_time,
|
|
||||||
MAX(created_at) AS last_login_time,
|
|
||||||
GREATEST(
|
|
||||||
MAX(refreshed_at)::timestamptz,
|
|
||||||
MAX(created_at)::timestamptz
|
|
||||||
) AS last_visit_time
|
|
||||||
FROM auth.sessions
|
|
||||||
GROUP BY user_id
|
|
||||||
),
|
|
||||||
user_agents AS (
|
|
||||||
-- Aggregate AgentGraph directly by userId (no fan-out from user_logins)
|
|
||||||
SELECT
|
|
||||||
"userId"::text AS user_id,
|
|
||||||
MAX("updatedAt") AS last_agent_save_time,
|
|
||||||
COUNT(DISTINCT "id") AS agent_count
|
|
||||||
FROM platform."AgentGraph"
|
|
||||||
WHERE "isActive"
|
|
||||||
GROUP BY "userId"
|
|
||||||
),
|
|
||||||
user_graph_runs AS (
|
|
||||||
-- Aggregate AgentGraphExecution directly by userId
|
|
||||||
SELECT
|
|
||||||
"userId"::text AS user_id,
|
|
||||||
MIN("createdAt") AS first_agent_run_time,
|
|
||||||
MAX("createdAt") AS last_agent_run_time,
|
|
||||||
COUNT(DISTINCT "agentGraphId") AS unique_agent_runs,
|
|
||||||
COUNT("id") AS agent_runs
|
|
||||||
FROM platform."AgentGraphExecution"
|
|
||||||
GROUP BY "userId"
|
|
||||||
),
|
|
||||||
user_node_runs AS (
|
|
||||||
-- Aggregate AgentNodeExecution directly; resolve userId via a
|
|
||||||
-- single join to AgentGraphExecution instead of fanning out from
|
|
||||||
-- user_logins through both large tables.
|
|
||||||
SELECT
|
|
||||||
g."userId"::text AS user_id,
|
|
||||||
COUNT(*) AS node_execution_count,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'FAILED') AS node_execution_failed,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'COMPLETED') AS node_execution_completed,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'TERMINATED') AS node_execution_terminated,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'QUEUED') AS node_execution_queued,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'RUNNING') AS node_execution_running,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'INCOMPLETE') AS node_execution_incomplete,
|
|
||||||
COUNT(*) FILTER (WHERE n."executionStatus" = 'REVIEW') AS node_execution_review
|
|
||||||
FROM platform."AgentNodeExecution" n
|
|
||||||
JOIN platform."AgentGraphExecution" g
|
|
||||||
ON g."id" = n."agentGraphExecutionId"
|
|
||||||
GROUP BY g."userId"
|
|
||||||
)
|
|
||||||
SELECT
|
|
||||||
ul.user_id,
|
|
||||||
ul.first_login_time,
|
|
||||||
ul.last_login_time,
|
|
||||||
ul.last_visit_time,
|
|
||||||
ua.last_agent_save_time,
|
|
||||||
COALESCE(ua.agent_count, 0) AS agent_count,
|
|
||||||
gr.first_agent_run_time,
|
|
||||||
gr.last_agent_run_time,
|
|
||||||
COALESCE(gr.unique_agent_runs, 0) AS unique_agent_runs,
|
|
||||||
COALESCE(gr.agent_runs, 0) AS agent_runs,
|
|
||||||
COALESCE(nr.node_execution_count, 0) AS node_execution_count,
|
|
||||||
COALESCE(nr.node_execution_failed, 0) AS node_execution_failed,
|
|
||||||
COALESCE(nr.node_execution_completed, 0) AS node_execution_completed,
|
|
||||||
COALESCE(nr.node_execution_terminated, 0) AS node_execution_terminated,
|
|
||||||
COALESCE(nr.node_execution_queued, 0) AS node_execution_queued,
|
|
||||||
COALESCE(nr.node_execution_running, 0) AS node_execution_running,
|
|
||||||
CASE
|
|
||||||
WHEN ul.first_login_time < NOW() - INTERVAL '7 days'
|
|
||||||
AND ul.last_visit_time >= ul.first_login_time + INTERVAL '7 days' THEN 1
|
|
||||||
WHEN ul.first_login_time < NOW() - INTERVAL '7 days'
|
|
||||||
AND ul.last_visit_time < ul.first_login_time + INTERVAL '7 days' THEN 0
|
|
||||||
ELSE NULL
|
|
||||||
END AS is_active_after_7d,
|
|
||||||
COALESCE(nr.node_execution_incomplete, 0) AS node_execution_incomplete,
|
|
||||||
COALESCE(nr.node_execution_review, 0) AS node_execution_review
|
|
||||||
FROM user_logins ul
|
|
||||||
LEFT JOIN user_agents ua ON ul.user_id = ua.user_id
|
|
||||||
LEFT JOIN user_graph_runs gr ON ul.user_id = gr.user_id
|
|
||||||
LEFT JOIN user_node_runs nr ON ul.user_id = nr.user_id
|
|
||||||
169
autogpt_platform/autogpt_libs/poetry.lock
generated
169
autogpt_platform/autogpt_libs/poetry.lock
generated
@@ -448,61 +448,61 @@ toml = ["tomli ; python_full_version <= \"3.11.0a6\""]
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cryptography"
|
name = "cryptography"
|
||||||
version = "46.0.5"
|
version = "46.0.4"
|
||||||
description = "cryptography is a package which provides cryptographic recipes and primitives to Python developers."
|
description = "cryptography is a package which provides cryptographic recipes and primitives to Python developers."
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = "!=3.9.0,!=3.9.1,>=3.8"
|
python-versions = "!=3.9.0,!=3.9.1,>=3.8"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-macosx_10_9_universal2.whl", hash = "sha256:351695ada9ea9618b3500b490ad54c739860883df6c1f555e088eaf25b1bbaad"},
|
{file = "cryptography-46.0.4-cp311-abi3-macosx_10_9_universal2.whl", hash = "sha256:281526e865ed4166009e235afadf3a4c4cba6056f99336a99efba65336fd5485"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:c18ff11e86df2e28854939acde2d003f7984f721eba450b56a200ad90eeb0e6b"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:5f14fba5bf6f4390d7ff8f086c566454bff0411f6d8aa7af79c88b6f9267aecc"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:4d7e3d356b8cd4ea5aff04f129d5f66ebdc7b6f8eae802b93739ed520c47c79b"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:47bcd19517e6389132f76e2d5303ded6cf3f78903da2158a671be8de024f4cd0"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:50bfb6925eff619c9c023b967d5b77a54e04256c4281b0e21336a130cd7fc263"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:01df4f50f314fbe7009f54046e908d1754f19d0c6d3070df1e6268c5a4af09fa"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:803812e111e75d1aa73690d2facc295eaefd4439be1023fefc4995eaea2af90d"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:5aa3e463596b0087b3da0dbe2b2487e9fc261d25da85754e30e3b40637d61f81"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:3ee190460e2fbe447175cda91b88b84ae8322a104fc27766ad09428754a618ed"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:0a9ad24359fee86f131836a9ac3bffc9329e956624a2d379b613f8f8abaf5255"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:f145bba11b878005c496e93e257c1e88f154d278d2638e6450d17e0f31e558d2"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:dc1272e25ef673efe72f2096e92ae39dea1a1a450dd44918b15351f72c5a168e"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:e9251e3be159d1020c4030bd2e5f84d6a43fe54b6c19c12f51cde9542a2817b2"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:de0f5f4ec8711ebc555f54735d4c673fc34b65c44283895f1a08c2b49d2fd99c"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:47fb8a66058b80e509c47118ef8a75d14c455e81ac369050f20ba0d23e77fee0"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:eeeb2e33d8dbcccc34d64651f00a98cb41b2dc69cef866771a5717e6734dfa32"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:4c3341037c136030cb46e4b1e17b7418ea4cbd9dd207e4a6f3b2b24e0d4ac731"},
|
{file = "cryptography-46.0.4-cp311-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:3d425eacbc9aceafd2cb429e42f4e5d5633c6f873f5e567077043ef1b9bbf616"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:890bcb4abd5a2d3f852196437129eb3667d62630333aacc13dfd470fad3aaa82"},
|
{file = "cryptography-46.0.4-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:91627ebf691d1ea3976a031b61fb7bac1ccd745afa03602275dda443e11c8de0"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:80a8d7bfdf38f87ca30a5391c0c9ce4ed2926918e017c29ddf643d0ed2778ea1"},
|
{file = "cryptography-46.0.4-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:2d08bc22efd73e8854b0b7caff402d735b354862f1145d7be3b9c0f740fef6a0"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-win32.whl", hash = "sha256:60ee7e19e95104d4c03871d7d7dfb3d22ef8a9b9c6778c94e1c8fcc8365afd48"},
|
{file = "cryptography-46.0.4-cp311-abi3-win32.whl", hash = "sha256:82a62483daf20b8134f6e92898da70d04d0ef9a75829d732ea1018678185f4f5"},
|
||||||
{file = "cryptography-46.0.5-cp311-abi3-win_amd64.whl", hash = "sha256:38946c54b16c885c72c4f59846be9743d699eee2b69b6988e0a00a01f46a61a4"},
|
{file = "cryptography-46.0.4-cp311-abi3-win_amd64.whl", hash = "sha256:6225d3ebe26a55dbc8ead5ad1265c0403552a63336499564675b29eb3184c09b"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-macosx_10_9_universal2.whl", hash = "sha256:94a76daa32eb78d61339aff7952ea819b1734b46f73646a07decb40e5b3448e2"},
|
{file = "cryptography-46.0.4-cp314-cp314t-macosx_10_9_universal2.whl", hash = "sha256:485e2b65d25ec0d901bca7bcae0f53b00133bf3173916d8e421f6fddde103908"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:5be7bf2fb40769e05739dd0046e7b26f9d4670badc7b032d6ce4db64dddc0678"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:078e5f06bd2fa5aea5a324f2a09f914b1484f1d0c2a4d6a8a28c74e72f65f2da"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:fe346b143ff9685e40192a4960938545c699054ba11d4f9029f94751e3f71d87"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:dce1e4f068f03008da7fa51cc7abc6ddc5e5de3e3d1550334eaf8393982a5829"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:c69fd885df7d089548a42d5ec05be26050ebcd2283d89b3d30676eb32ff87dee"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:2067461c80271f422ee7bdbe79b9b4be54a5162e90345f86a23445a0cf3fd8a2"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_28_ppc64le.whl", hash = "sha256:8293f3dea7fc929ef7240796ba231413afa7b68ce38fd21da2995549f5961981"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_28_ppc64le.whl", hash = "sha256:c92010b58a51196a5f41c3795190203ac52edfd5dc3ff99149b4659eba9d2085"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_28_x86_64.whl", hash = "sha256:1abfdb89b41c3be0365328a410baa9df3ff8a9110fb75e7b52e66803ddabc9a9"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_28_x86_64.whl", hash = "sha256:829c2b12bbc5428ab02d6b7f7e9bbfd53e33efd6672d21341f2177470171ad8b"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_31_armv7l.whl", hash = "sha256:d66e421495fdb797610a08f43b05269e0a5ea7f5e652a89bfd5a7d3c1dee3648"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_31_armv7l.whl", hash = "sha256:62217ba44bf81b30abaeda1488686a04a702a261e26f87db51ff61d9d3510abd"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_34_aarch64.whl", hash = "sha256:4e817a8920bfbcff8940ecfd60f23d01836408242b30f1a708d93198393a80b4"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_34_aarch64.whl", hash = "sha256:9c2da296c8d3415b93e6053f5a728649a87a48ce084a9aaf51d6e46c87c7f2d2"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_34_ppc64le.whl", hash = "sha256:68f68d13f2e1cb95163fa3b4db4bf9a159a418f5f6e7242564fc75fcae667fd0"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_34_ppc64le.whl", hash = "sha256:9b34d8ba84454641a6bf4d6762d15847ecbd85c1316c0a7984e6e4e9f748ec2e"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-manylinux_2_34_x86_64.whl", hash = "sha256:a3d1fae9863299076f05cb8a778c467578262fae09f9dc0ee9b12eb4268ce663"},
|
{file = "cryptography-46.0.4-cp314-cp314t-manylinux_2_34_x86_64.whl", hash = "sha256:df4a817fa7138dd0c96c8c8c20f04b8aaa1fac3bbf610913dcad8ea82e1bfd3f"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:c4143987a42a2397f2fc3b4d7e3a7d313fbe684f67ff443999e803dd75a76826"},
|
{file = "cryptography-46.0.4-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:b1de0ebf7587f28f9190b9cb526e901bf448c9e6a99655d2b07fff60e8212a82"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:7d731d4b107030987fd61a7f8ab512b25b53cef8f233a97379ede116f30eb67d"},
|
{file = "cryptography-46.0.4-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:9b4d17bc7bd7cdd98e3af40b441feaea4c68225e2eb2341026c84511ad246c0c"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-win32.whl", hash = "sha256:c3bcce8521d785d510b2aad26ae2c966092b7daa8f45dd8f44734a104dc0bc1a"},
|
{file = "cryptography-46.0.4-cp314-cp314t-win32.whl", hash = "sha256:c411f16275b0dea722d76544a61d6421e2cc829ad76eec79280dbdc9ddf50061"},
|
||||||
{file = "cryptography-46.0.5-cp314-cp314t-win_amd64.whl", hash = "sha256:4d8ae8659ab18c65ced284993c2265910f6c9e650189d4e3f68445ef82a810e4"},
|
{file = "cryptography-46.0.4-cp314-cp314t-win_amd64.whl", hash = "sha256:728fedc529efc1439eb6107b677f7f7558adab4553ef8669f0d02d42d7b959a7"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-macosx_10_9_universal2.whl", hash = "sha256:4108d4c09fbbf2789d0c926eb4152ae1760d5a2d97612b92d508d96c861e4d31"},
|
{file = "cryptography-46.0.4-cp38-abi3-macosx_10_9_universal2.whl", hash = "sha256:a9556ba711f7c23f77b151d5798f3ac44a13455cc68db7697a1096e6d0563cab"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:7d1f30a86d2757199cb2d56e48cce14deddf1f9c95f1ef1b64ee91ea43fe2e18"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:8bf75b0259e87fa70bddc0b8b4078b76e7fd512fd9afae6c1193bcf440a4dbef"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:039917b0dc418bb9f6edce8a906572d69e74bd330b0b3fea4f79dab7f8ddd235"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:3c268a3490df22270955966ba236d6bc4a8f9b6e4ffddb78aac535f1a5ea471d"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:ba2a27ff02f48193fc4daeadf8ad2590516fa3d0adeeb34336b96f7fa64c1e3a"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:812815182f6a0c1d49a37893a303b44eaac827d7f0d582cecfc81b6427f22973"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:61aa400dce22cb001a98014f647dc21cda08f7915ceb95df0c9eaf84b4b6af76"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:a90e43e3ef65e6dcf969dfe3bb40cbf5aef0d523dff95bfa24256be172a845f4"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:3ce58ba46e1bc2aac4f7d9290223cead56743fa6ab94a5d53292ffaac6a91614"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:a05177ff6296644ef2876fce50518dffb5bcdf903c85250974fc8bc85d54c0af"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:420d0e909050490d04359e7fdb5ed7e667ca5c3c402b809ae2563d7e66a92229"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:daa392191f626d50f1b136c9b4cf08af69ca8279d110ea24f5c2700054d2e263"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:582f5fcd2afa31622f317f80426a027f30dc792e9c80ffee87b993200ea115f1"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:e07ea39c5b048e085f15923511d8121e4a9dc45cee4e3b970ca4f0d338f23095"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:bfd56bb4b37ed4f330b82402f6f435845a5f5648edf1ad497da51a8452d5d62d"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:d5a45ddc256f492ce42a4e35879c5e5528c09cd9ad12420828c972951d8e016b"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:a3d507bb6a513ca96ba84443226af944b0f7f47dcc9a399d110cd6146481d24c"},
|
{file = "cryptography-46.0.4-cp38-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:6bb5157bf6a350e5b28aee23beb2d84ae6f5be390b2f8ee7ea179cda077e1019"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:9f16fbdf4da055efb21c22d81b89f155f02ba420558db21288b3d0035bafd5f4"},
|
{file = "cryptography-46.0.4-cp38-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:dd5aba870a2c40f87a3af043e0dee7d9eb02d4aff88a797b48f2b43eff8c3ab4"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:ced80795227d70549a411a4ab66e8ce307899fad2220ce5ab2f296e687eacde9"},
|
{file = "cryptography-46.0.4-cp38-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:93d8291da8d71024379ab2cb0b5c57915300155ad42e07f76bea6ad838d7e59b"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-win32.whl", hash = "sha256:02f547fce831f5096c9a567fd41bc12ca8f11df260959ecc7c3202555cc47a72"},
|
{file = "cryptography-46.0.4-cp38-abi3-win32.whl", hash = "sha256:0563655cb3c6d05fb2afe693340bc050c30f9f34e15763361cf08e94749401fc"},
|
||||||
{file = "cryptography-46.0.5-cp38-abi3-win_amd64.whl", hash = "sha256:556e106ee01aa13484ce9b0239bca667be5004efb0aabbed28d353df86445595"},
|
{file = "cryptography-46.0.4-cp38-abi3-win_amd64.whl", hash = "sha256:fa0900b9ef9c49728887d1576fd8d9e7e3ea872fa9b25ef9b64888adc434e976"},
|
||||||
{file = "cryptography-46.0.5-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:3b4995dc971c9fb83c25aa44cf45f02ba86f71ee600d81091c2f0cbae116b06c"},
|
{file = "cryptography-46.0.4-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:766330cce7416c92b5e90c3bb71b1b79521760cdcfc3a6a1a182d4c9fab23d2b"},
|
||||||
{file = "cryptography-46.0.5-pp311-pypy311_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:bc84e875994c3b445871ea7181d424588171efec3e185dced958dad9e001950a"},
|
{file = "cryptography-46.0.4-pp311-pypy311_pp73-manylinux_2_28_aarch64.whl", hash = "sha256:c236a44acfb610e70f6b3e1c3ca20ff24459659231ef2f8c48e879e2d32b73da"},
|
||||||
{file = "cryptography-46.0.5-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:2ae6971afd6246710480e3f15824ed3029a60fc16991db250034efd0b9fb4356"},
|
{file = "cryptography-46.0.4-pp311-pypy311_pp73-manylinux_2_28_x86_64.whl", hash = "sha256:8a15fb869670efa8f83cbffbc8753c1abf236883225aed74cd179b720ac9ec80"},
|
||||||
{file = "cryptography-46.0.5-pp311-pypy311_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:d861ee9e76ace6cf36a6a89b959ec08e7bc2493ee39d07ffe5acb23ef46d27da"},
|
{file = "cryptography-46.0.4-pp311-pypy311_pp73-manylinux_2_34_aarch64.whl", hash = "sha256:fdc3daab53b212472f1524d070735b2f0c214239df131903bae1d598016fa822"},
|
||||||
{file = "cryptography-46.0.5-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:2b7a67c9cd56372f3249b39699f2ad479f6991e62ea15800973b956f4b73e257"},
|
{file = "cryptography-46.0.4-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl", hash = "sha256:44cc0675b27cadb71bdbb96099cca1fa051cd11d2ade09e5cd3a2edb929ed947"},
|
||||||
{file = "cryptography-46.0.5-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:8456928655f856c6e1533ff59d5be76578a7157224dbd9ce6872f25055ab9ab7"},
|
{file = "cryptography-46.0.4-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:be8c01a7d5a55f9a47d1888162b76c8f49d62b234d88f0ff91a9fbebe32ffbc3"},
|
||||||
{file = "cryptography-46.0.5.tar.gz", hash = "sha256:abace499247268e3757271b2f1e244b36b06f8515cf27c4d49468fc9eb16e93d"},
|
{file = "cryptography-46.0.4.tar.gz", hash = "sha256:bfd019f60f8abc2ed1b9be4ddc21cfef059c841d86d710bb69909a688cbb8f59"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -516,7 +516,7 @@ nox = ["nox[uv] (>=2024.4.15)"]
|
|||||||
pep8test = ["check-sdist", "click (>=8.0.1)", "mypy (>=1.14)", "ruff (>=0.11.11)"]
|
pep8test = ["check-sdist", "click (>=8.0.1)", "mypy (>=1.14)", "ruff (>=0.11.11)"]
|
||||||
sdist = ["build (>=1.0.0)"]
|
sdist = ["build (>=1.0.0)"]
|
||||||
ssh = ["bcrypt (>=3.1.5)"]
|
ssh = ["bcrypt (>=3.1.5)"]
|
||||||
test = ["certifi (>=2024)", "cryptography-vectors (==46.0.5)", "pretend (>=0.7)", "pytest (>=7.4.0)", "pytest-benchmark (>=4.0)", "pytest-cov (>=2.10.1)", "pytest-xdist (>=3.5.0)"]
|
test = ["certifi (>=2024)", "cryptography-vectors (==46.0.4)", "pretend (>=0.7)", "pytest (>=7.4.0)", "pytest-benchmark (>=4.0)", "pytest-cov (>=2.10.1)", "pytest-xdist (>=3.5.0)"]
|
||||||
test-randomorder = ["pytest-randomly"]
|
test-randomorder = ["pytest-randomly"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -570,25 +570,24 @@ tests = ["coverage", "coveralls", "dill", "mock", "nose"]
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastapi"
|
name = "fastapi"
|
||||||
version = "0.128.7"
|
version = "0.128.0"
|
||||||
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
|
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "fastapi-0.128.7-py3-none-any.whl", hash = "sha256:6bd9bd31cb7047465f2d3fa3ba3f33b0870b17d4eaf7cdb36d1576ab060ad662"},
|
{file = "fastapi-0.128.0-py3-none-any.whl", hash = "sha256:aebd93f9716ee3b4f4fcfe13ffb7cf308d99c9f3ab5622d8877441072561582d"},
|
||||||
{file = "fastapi-0.128.7.tar.gz", hash = "sha256:783c273416995486c155ad2c0e2b45905dedfaf20b9ef8d9f6a9124670639a24"},
|
{file = "fastapi-0.128.0.tar.gz", hash = "sha256:1cc179e1cef10a6be60ffe429f79b829dce99d8de32d7acb7e6c8dfdf7f2645a"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
annotated-doc = ">=0.0.2"
|
annotated-doc = ">=0.0.2"
|
||||||
pydantic = ">=2.7.0"
|
pydantic = ">=2.7.0"
|
||||||
starlette = ">=0.40.0,<1.0.0"
|
starlette = ">=0.40.0,<0.51.0"
|
||||||
typing-extensions = ">=4.8.0"
|
typing-extensions = ">=4.8.0"
|
||||||
typing-inspection = ">=0.4.2"
|
|
||||||
|
|
||||||
[package.extras]
|
[package.extras]
|
||||||
all = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.8)", "httpx (>=0.23.0,<1.0.0)", "itsdangerous (>=1.1.0)", "jinja2 (>=3.1.5)", "orjson (>=3.9.3)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "pyyaml (>=5.3.1)", "ujson (>=5.8.0)", "uvicorn[standard] (>=0.12.0)"]
|
all = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.8)", "httpx (>=0.23.0,<1.0.0)", "itsdangerous (>=1.1.0)", "jinja2 (>=3.1.5)", "orjson (>=3.2.1)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "pyyaml (>=5.3.1)", "ujson (>=4.0.1,!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0)", "uvicorn[standard] (>=0.12.0)"]
|
||||||
standard = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.8)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"]
|
standard = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.8)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"]
|
||||||
standard-no-fastapi-cloud-cli = ["email-validator (>=2.0.0)", "fastapi-cli[standard-no-fastapi-cloud-cli] (>=0.0.8)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"]
|
standard-no-fastapi-cloud-cli = ["email-validator (>=2.0.0)", "fastapi-cli[standard-no-fastapi-cloud-cli] (>=0.0.8)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"]
|
||||||
|
|
||||||
@@ -1063,14 +1062,14 @@ urllib3 = ">=1.26.0,<3"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "launchdarkly-server-sdk"
|
name = "launchdarkly-server-sdk"
|
||||||
version = "9.15.0"
|
version = "9.14.1"
|
||||||
description = "LaunchDarkly SDK for Python"
|
description = "LaunchDarkly SDK for Python"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.10"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "launchdarkly_server_sdk-9.15.0-py3-none-any.whl", hash = "sha256:c267e29bfa3fb5e2a06a208448ada6ed5557a2924979b8d79c970b45d227c668"},
|
{file = "launchdarkly_server_sdk-9.14.1-py3-none-any.whl", hash = "sha256:a9e2bd9ecdef845cd631ae0d4334a1115e5b44257c42eb2349492be4bac7815c"},
|
||||||
{file = "launchdarkly_server_sdk-9.15.0.tar.gz", hash = "sha256:f31441b74bc1a69c381db57c33116509e407a2612628ad6dff0a7dbb39d5020b"},
|
{file = "launchdarkly_server_sdk-9.14.1.tar.gz", hash = "sha256:1df44baf0a0efa74d8c1dad7a00592b98bce7d19edded7f770da8dbc49922213"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -1479,14 +1478,14 @@ testing = ["coverage", "pytest", "pytest-benchmark"]
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "postgrest"
|
name = "postgrest"
|
||||||
version = "2.28.0"
|
version = "2.27.2"
|
||||||
description = "PostgREST client for Python. This library provides an ORM interface to PostgREST."
|
description = "PostgREST client for Python. This library provides an ORM interface to PostgREST."
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "postgrest-2.28.0-py3-none-any.whl", hash = "sha256:7bca2f24dd1a1bf8a3d586c7482aba6cd41662da6733045fad585b63b7f7df75"},
|
{file = "postgrest-2.27.2-py3-none-any.whl", hash = "sha256:1666fef3de05ca097a314433dd5ae2f2d71c613cb7b233d0f468c4ffe37277da"},
|
||||||
{file = "postgrest-2.28.0.tar.gz", hash = "sha256:c36b38646d25ea4255321d3d924ce70f8d20ec7799cb42c1221d6a818d4f6515"},
|
{file = "postgrest-2.27.2.tar.gz", hash = "sha256:55407d530b5af3d64e883a71fec1f345d369958f723ce4a8ab0b7d169e313242"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -2249,14 +2248,14 @@ cli = ["click (>=5.0)"]
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "realtime"
|
name = "realtime"
|
||||||
version = "2.28.0"
|
version = "2.27.2"
|
||||||
description = ""
|
description = ""
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "realtime-2.28.0-py3-none-any.whl", hash = "sha256:db1bd59bab9b1fcc9f9d3b1a073bed35bf4994d720e6751f10031a58d57a3836"},
|
{file = "realtime-2.27.2-py3-none-any.whl", hash = "sha256:34a9cbb26a274e707e8fc9e3ee0a66de944beac0fe604dc336d1e985db2c830f"},
|
||||||
{file = "realtime-2.28.0.tar.gz", hash = "sha256:d18cedcebd6a8f22fcd509bc767f639761eb218b7b2b6f14fc4205b6259b50fc"},
|
{file = "realtime-2.27.2.tar.gz", hash = "sha256:b960a90294d2cea1b3f1275ecb89204304728e08fff1c393cc1b3150739556b3"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -2437,14 +2436,14 @@ full = ["httpx (>=0.27.0,<0.29.0)", "itsdangerous", "jinja2", "python-multipart
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "storage3"
|
name = "storage3"
|
||||||
version = "2.28.0"
|
version = "2.27.2"
|
||||||
description = "Supabase Storage client for Python."
|
description = "Supabase Storage client for Python."
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "storage3-2.28.0-py3-none-any.whl", hash = "sha256:ecb50efd2ac71dabbdf97e99ad346eafa630c4c627a8e5a138ceb5fbbadae716"},
|
{file = "storage3-2.27.2-py3-none-any.whl", hash = "sha256:e6f16e7a260729e7b1f46e9bf61746805a02e30f5e419ee1291007c432e3ec63"},
|
||||||
{file = "storage3-2.28.0.tar.gz", hash = "sha256:bc1d008aff67de7a0f2bd867baee7aadbcdb6f78f5a310b4f7a38e8c13c19865"},
|
{file = "storage3-2.27.2.tar.gz", hash = "sha256:cb4807b7f86b4bb1272ac6fdd2f3cfd8ba577297046fa5f88557425200275af5"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -2488,35 +2487,35 @@ python-dateutil = ">=2.6.0"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "supabase"
|
name = "supabase"
|
||||||
version = "2.28.0"
|
version = "2.27.2"
|
||||||
description = "Supabase client for Python."
|
description = "Supabase client for Python."
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "supabase-2.28.0-py3-none-any.whl", hash = "sha256:42776971c7d0ccca16034df1ab96a31c50228eb1eb19da4249ad2f756fc20272"},
|
{file = "supabase-2.27.2-py3-none-any.whl", hash = "sha256:d4dce00b3a418ee578017ec577c0e5be47a9a636355009c76f20ed2faa15bc54"},
|
||||||
{file = "supabase-2.28.0.tar.gz", hash = "sha256:aea299aaab2a2eed3c57e0be7fc035c6807214194cce795a3575add20268ece1"},
|
{file = "supabase-2.27.2.tar.gz", hash = "sha256:2aed40e4f3454438822442a1e94a47be6694c2c70392e7ae99b51a226d4293f7"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
httpx = ">=0.26,<0.29"
|
httpx = ">=0.26,<0.29"
|
||||||
postgrest = "2.28.0"
|
postgrest = "2.27.2"
|
||||||
realtime = "2.28.0"
|
realtime = "2.27.2"
|
||||||
storage3 = "2.28.0"
|
storage3 = "2.27.2"
|
||||||
supabase-auth = "2.28.0"
|
supabase-auth = "2.27.2"
|
||||||
supabase-functions = "2.28.0"
|
supabase-functions = "2.27.2"
|
||||||
yarl = ">=1.22.0"
|
yarl = ">=1.22.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "supabase-auth"
|
name = "supabase-auth"
|
||||||
version = "2.28.0"
|
version = "2.27.2"
|
||||||
description = "Python Client Library for Supabase Auth"
|
description = "Python Client Library for Supabase Auth"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "supabase_auth-2.28.0-py3-none-any.whl", hash = "sha256:2ac85026cc285054c7fa6d41924f3a333e9ec298c013e5b5e1754039ba7caec9"},
|
{file = "supabase_auth-2.27.2-py3-none-any.whl", hash = "sha256:78ec25b11314d0a9527a7205f3b1c72560dccdc11b38392f80297ef98664ee91"},
|
||||||
{file = "supabase_auth-2.28.0.tar.gz", hash = "sha256:2bb8f18ff39934e44b28f10918db965659f3735cd6fbfcc022fe0b82dbf8233e"},
|
{file = "supabase_auth-2.27.2.tar.gz", hash = "sha256:0f5bcc79b3677cb42e9d321f3c559070cfa40d6a29a67672cc8382fb7dc2fe97"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -2526,14 +2525,14 @@ pyjwt = {version = ">=2.10.1", extras = ["crypto"]}
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "supabase-functions"
|
name = "supabase-functions"
|
||||||
version = "2.28.0"
|
version = "2.27.2"
|
||||||
description = "Library for Supabase Functions"
|
description = "Library for Supabase Functions"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
groups = ["main"]
|
groups = ["main"]
|
||||||
files = [
|
files = [
|
||||||
{file = "supabase_functions-2.28.0-py3-none-any.whl", hash = "sha256:30bf2d586f8df285faf0621bb5d5bb3ec3157234fc820553ca156f009475e4ae"},
|
{file = "supabase_functions-2.27.2-py3-none-any.whl", hash = "sha256:db480efc669d0bca07605b9b6f167312af43121adcc842a111f79bea416ef754"},
|
||||||
{file = "supabase_functions-2.28.0.tar.gz", hash = "sha256:db3dddfc37aca5858819eb461130968473bd8c75bd284581013958526dac718b"},
|
{file = "supabase_functions-2.27.2.tar.gz", hash = "sha256:d0c8266207a94371cb3fd35ad3c7f025b78a97cf026861e04ccd35ac1775f80b"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@@ -2912,4 +2911,4 @@ type = ["pytest-mypy"]
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.1"
|
lock-version = "2.1"
|
||||||
python-versions = ">=3.10,<4.0"
|
python-versions = ">=3.10,<4.0"
|
||||||
content-hash = "9619cae908ad38fa2c48016a58bcf4241f6f5793aa0e6cc140276e91c433cbbb"
|
content-hash = "40eae94995dc0a388fa832ed4af9b6137f28d5b5ced3aaea70d5f91d4d9a179d"
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ python = ">=3.10,<4.0"
|
|||||||
colorama = "^0.4.6"
|
colorama = "^0.4.6"
|
||||||
cryptography = "^46.0"
|
cryptography = "^46.0"
|
||||||
expiringdict = "^1.2.2"
|
expiringdict = "^1.2.2"
|
||||||
fastapi = "^0.128.7"
|
fastapi = "^0.128.0"
|
||||||
google-cloud-logging = "^3.13.0"
|
google-cloud-logging = "^3.13.0"
|
||||||
launchdarkly-server-sdk = "^9.15.0"
|
launchdarkly-server-sdk = "^9.14.1"
|
||||||
pydantic = "^2.12.5"
|
pydantic = "^2.12.5"
|
||||||
pydantic-settings = "^2.12.0"
|
pydantic-settings = "^2.12.0"
|
||||||
pyjwt = { version = "^2.11.0", extras = ["crypto"] }
|
pyjwt = { version = "^2.11.0", extras = ["crypto"] }
|
||||||
redis = "^6.2.0"
|
redis = "^6.2.0"
|
||||||
supabase = "^2.28.0"
|
supabase = "^2.27.2"
|
||||||
uvicorn = "^0.40.0"
|
uvicorn = "^0.40.0"
|
||||||
|
|
||||||
[tool.poetry.group.dev.dependencies]
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
|||||||
@@ -37,10 +37,6 @@ JWT_VERIFY_KEY=your-super-secret-jwt-token-with-at-least-32-characters-long
|
|||||||
ENCRYPTION_KEY=dvziYgz0KSK8FENhju0ZYi8-fRTfAdlz6YLhdB_jhNw=
|
ENCRYPTION_KEY=dvziYgz0KSK8FENhju0ZYi8-fRTfAdlz6YLhdB_jhNw=
|
||||||
UNSUBSCRIBE_SECRET_KEY=HlP8ivStJjmbf6NKi78m_3FnOogut0t5ckzjsIqeaio=
|
UNSUBSCRIBE_SECRET_KEY=HlP8ivStJjmbf6NKi78m_3FnOogut0t5ckzjsIqeaio=
|
||||||
|
|
||||||
## ===== SIGNUP / INVITE GATE ===== ##
|
|
||||||
# Set to true to require an invite before users can sign up
|
|
||||||
ENABLE_INVITE_GATE=false
|
|
||||||
|
|
||||||
## ===== IMPORTANT OPTIONAL CONFIGURATION ===== ##
|
## ===== IMPORTANT OPTIONAL CONFIGURATION ===== ##
|
||||||
# Platform URLs (set these for webhooks and OAuth to work)
|
# Platform URLs (set these for webhooks and OAuth to work)
|
||||||
PLATFORM_BASE_URL=http://localhost:8000
|
PLATFORM_BASE_URL=http://localhost:8000
|
||||||
@@ -108,12 +104,6 @@ TWITTER_CLIENT_SECRET=
|
|||||||
# Make a new workspace for your OAuth APP -- trust me
|
# Make a new workspace for your OAuth APP -- trust me
|
||||||
# https://linear.app/settings/api/applications/new
|
# https://linear.app/settings/api/applications/new
|
||||||
# Callback URL: http://localhost:3000/auth/integrations/oauth_callback
|
# Callback URL: http://localhost:3000/auth/integrations/oauth_callback
|
||||||
LINEAR_API_KEY=
|
|
||||||
# Linear project and team IDs for the feature request tracker.
|
|
||||||
# Find these in your Linear workspace URL: linear.app/<workspace>/project/<project-id>
|
|
||||||
# and in team settings. Used by the chat copilot to file and search feature requests.
|
|
||||||
LINEAR_FEATURE_REQUEST_PROJECT_ID=
|
|
||||||
LINEAR_FEATURE_REQUEST_TEAM_ID=
|
|
||||||
LINEAR_CLIENT_ID=
|
LINEAR_CLIENT_ID=
|
||||||
LINEAR_CLIENT_SECRET=
|
LINEAR_CLIENT_SECRET=
|
||||||
|
|
||||||
@@ -194,8 +184,5 @@ ZEROBOUNCE_API_KEY=
|
|||||||
POSTHOG_API_KEY=
|
POSTHOG_API_KEY=
|
||||||
POSTHOG_HOST=https://eu.i.posthog.com
|
POSTHOG_HOST=https://eu.i.posthog.com
|
||||||
|
|
||||||
# Tally Form Integration (pre-populate business understanding on signup)
|
|
||||||
TALLY_API_KEY=
|
|
||||||
|
|
||||||
# Other Services
|
# Other Services
|
||||||
AUTOMOD_API_KEY=
|
AUTOMOD_API_KEY=
|
||||||
|
|||||||
@@ -58,31 +58,10 @@ poetry run pytest path/to/test.py --snapshot-update
|
|||||||
- **Authentication**: JWT-based with Supabase integration
|
- **Authentication**: JWT-based with Supabase integration
|
||||||
- **Security**: Cache protection middleware prevents sensitive data caching in browsers/proxies
|
- **Security**: Cache protection middleware prevents sensitive data caching in browsers/proxies
|
||||||
|
|
||||||
## Code Style
|
|
||||||
|
|
||||||
- **Top-level imports only** — no local/inner imports (lazy imports only for heavy optional deps like `openpyxl`)
|
|
||||||
- **No duck typing** — no `hasattr`/`getattr`/`isinstance` for type dispatch; use typed interfaces/unions/protocols
|
|
||||||
- **Pydantic models** over dataclass/namedtuple/dict for structured data
|
|
||||||
- **No linter suppressors** — no `# type: ignore`, `# noqa`, `# pyright: ignore`; fix the type/code
|
|
||||||
- **List comprehensions** over manual loop-and-append
|
|
||||||
- **Early return** — guard clauses first, avoid deep nesting
|
|
||||||
- **Lazy `%s` logging** — `logger.info("Processing %s items", count)` not `logger.info(f"Processing {count} items")`
|
|
||||||
- **Sanitize error paths** — `os.path.basename()` in error messages to avoid leaking directory structure
|
|
||||||
- **TOCTOU awareness** — avoid check-then-act patterns for file access and credit charging
|
|
||||||
- **`Security()` vs `Depends()`** — use `Security()` for auth deps to get proper OpenAPI security spec
|
|
||||||
- **Redis pipelines** — `transaction=True` for atomicity on multi-step operations
|
|
||||||
- **`max(0, value)` guards** — for computed values that should never be negative
|
|
||||||
- **SSE protocol** — `data:` lines for frontend-parsed events (must match Zod schema), `: comment` lines for heartbeats/status
|
|
||||||
- **File length** — keep files under ~300 lines; if a file grows beyond this, split by responsibility (e.g. extract helpers, models, or a sub-module into a new file). Never keep appending to a long file.
|
|
||||||
- **Function length** — keep functions under ~40 lines; extract named helpers when a function grows longer. Long functions are a sign of mixed concerns, not complexity.
|
|
||||||
|
|
||||||
## Testing Approach
|
## Testing Approach
|
||||||
|
|
||||||
- Uses pytest with snapshot testing for API responses
|
- Uses pytest with snapshot testing for API responses
|
||||||
- Test files are colocated with source files (`*_test.py`)
|
- Test files are colocated with source files (`*_test.py`)
|
||||||
- Mock at boundaries — mock where the symbol is **used**, not where it's **defined**
|
|
||||||
- After refactoring, update mock targets to match new module paths
|
|
||||||
- Use `AsyncMock` for async functions (`from unittest.mock import AsyncMock`)
|
|
||||||
|
|
||||||
## Database Schema
|
## Database Schema
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
# ============================ DEPENDENCY BUILDER ============================ #
|
|
||||||
|
|
||||||
FROM debian:13-slim AS builder
|
FROM debian:13-slim AS builder
|
||||||
|
|
||||||
# Set environment variables
|
# Set environment variables
|
||||||
@@ -53,106 +51,60 @@ COPY autogpt_platform/backend/backend/data/partial_types.py ./backend/data/parti
|
|||||||
COPY autogpt_platform/backend/gen_prisma_types_stub.py ./
|
COPY autogpt_platform/backend/gen_prisma_types_stub.py ./
|
||||||
RUN poetry run prisma generate && poetry run gen-prisma-stub
|
RUN poetry run prisma generate && poetry run gen-prisma-stub
|
||||||
|
|
||||||
# =============================== DB MIGRATOR =============================== #
|
FROM debian:13-slim AS server_dependencies
|
||||||
|
|
||||||
# Lightweight migrate stage - only needs Prisma CLI, not full Python environment
|
|
||||||
FROM debian:13-slim AS migrate
|
|
||||||
|
|
||||||
WORKDIR /app/autogpt_platform/backend
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
# Install only what's needed for prisma migrate: Node.js and minimal Python for prisma-python
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
||||||
python3.13 \
|
|
||||||
python3-pip \
|
|
||||||
ca-certificates \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Copy Node.js from builder (needed for Prisma CLI)
|
|
||||||
COPY --from=builder /usr/bin/node /usr/bin/node
|
|
||||||
COPY --from=builder /usr/lib/node_modules /usr/lib/node_modules
|
|
||||||
COPY --from=builder /usr/bin/npm /usr/bin/npm
|
|
||||||
|
|
||||||
# Copy Prisma binaries
|
|
||||||
COPY --from=builder /root/.cache/prisma-python/binaries /root/.cache/prisma-python/binaries
|
|
||||||
|
|
||||||
# Install prisma-client-py directly (much smaller than copying full venv)
|
|
||||||
RUN pip3 install prisma>=0.15.0 --break-system-packages
|
|
||||||
|
|
||||||
COPY autogpt_platform/backend/schema.prisma ./
|
|
||||||
COPY autogpt_platform/backend/backend/data/partial_types.py ./backend/data/partial_types.py
|
|
||||||
COPY autogpt_platform/backend/gen_prisma_types_stub.py ./
|
|
||||||
COPY autogpt_platform/backend/migrations ./migrations
|
|
||||||
|
|
||||||
# ============================== BACKEND SERVER ============================== #
|
|
||||||
|
|
||||||
FROM debian:13-slim AS server
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
ENV POETRY_HOME=/opt/poetry \
|
||||||
|
POETRY_NO_INTERACTION=1 \
|
||||||
|
POETRY_VIRTUALENVS_CREATE=true \
|
||||||
|
POETRY_VIRTUALENVS_IN_PROJECT=true \
|
||||||
|
DEBIAN_FRONTEND=noninteractive
|
||||||
|
ENV PATH=/opt/poetry/bin:$PATH
|
||||||
|
|
||||||
# Install Python, FFmpeg, ImageMagick, and CLI tools for agent use.
|
# Install Python, FFmpeg, and ImageMagick (required for video processing blocks)
|
||||||
# bubblewrap provides OS-level sandbox (whitelist-only FS + no network)
|
RUN apt-get update && apt-get install -y \
|
||||||
# for the bash_exec MCP tool (fallback when E2B is not configured).
|
|
||||||
# Using --no-install-recommends saves ~650MB by skipping unnecessary deps like llvm, mesa, etc.
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
||||||
python3.13 \
|
python3.13 \
|
||||||
python3-pip \
|
python3-pip \
|
||||||
ffmpeg \
|
ffmpeg \
|
||||||
imagemagick \
|
imagemagick \
|
||||||
jq \
|
|
||||||
ripgrep \
|
|
||||||
tree \
|
|
||||||
bubblewrap \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Copy poetry (build-time only, for `poetry install --only-root` to create entry points)
|
# Copy only necessary files from builder
|
||||||
|
COPY --from=builder /app /app
|
||||||
COPY --from=builder /usr/local/lib/python3* /usr/local/lib/python3*
|
COPY --from=builder /usr/local/lib/python3* /usr/local/lib/python3*
|
||||||
COPY --from=builder /usr/local/bin/poetry /usr/local/bin/poetry
|
COPY --from=builder /usr/local/bin/poetry /usr/local/bin/poetry
|
||||||
# Copy Node.js installation for Prisma and agent-browser.
|
# Copy Node.js installation for Prisma
|
||||||
# npm/npx are symlinks in the builder (-> ../lib/node_modules/npm/bin/*-cli.js);
|
|
||||||
# COPY resolves them to regular files, breaking require() paths. Recreate as
|
|
||||||
# proper symlinks so npm/npx can find their modules.
|
|
||||||
COPY --from=builder /usr/bin/node /usr/bin/node
|
COPY --from=builder /usr/bin/node /usr/bin/node
|
||||||
COPY --from=builder /usr/lib/node_modules /usr/lib/node_modules
|
COPY --from=builder /usr/lib/node_modules /usr/lib/node_modules
|
||||||
RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/bin/npm \
|
COPY --from=builder /usr/bin/npm /usr/bin/npm
|
||||||
&& ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/bin/npx
|
COPY --from=builder /usr/bin/npx /usr/bin/npx
|
||||||
COPY --from=builder /root/.cache/prisma-python/binaries /root/.cache/prisma-python/binaries
|
COPY --from=builder /root/.cache/prisma-python/binaries /root/.cache/prisma-python/binaries
|
||||||
|
|
||||||
# Install agent-browser (Copilot browser tool) + Chromium runtime dependencies.
|
ENV PATH="/app/autogpt_platform/backend/.venv/bin:$PATH"
|
||||||
# These are the runtime libraries Chromium/Playwright needs on Debian 13 (trixie).
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN mkdir -p /app/autogpt_platform/autogpt_libs
|
||||||
libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 \
|
RUN mkdir -p /app/autogpt_platform/backend
|
||||||
libdbus-1-3 libxkbcommon0 libatspi2.0-0t64 libxcomposite1 libxdamage1 \
|
|
||||||
libxfixes3 libxrandr2 libgbm1 libasound2t64 libpango-1.0-0 libcairo2 \
|
COPY autogpt_platform/autogpt_libs /app/autogpt_platform/autogpt_libs
|
||||||
libx11-6 libx11-xcb1 libxcb1 libxext6 libglib2.0-0t64 \
|
|
||||||
fonts-liberation libfontconfig1 \
|
COPY autogpt_platform/backend/poetry.lock autogpt_platform/backend/pyproject.toml /app/autogpt_platform/backend/
|
||||||
&& rm -rf /var/lib/apt/lists/* \
|
|
||||||
&& npm install -g agent-browser \
|
|
||||||
&& agent-browser install \
|
|
||||||
&& rm -rf /tmp/* /root/.npm
|
|
||||||
|
|
||||||
WORKDIR /app/autogpt_platform/backend
|
WORKDIR /app/autogpt_platform/backend
|
||||||
|
|
||||||
# Copy only the .venv from builder (not the entire /app directory)
|
FROM server_dependencies AS migrate
|
||||||
# The .venv includes the generated Prisma client
|
|
||||||
COPY --from=builder /app/autogpt_platform/backend/.venv ./.venv
|
|
||||||
ENV PATH="/app/autogpt_platform/backend/.venv/bin:$PATH"
|
|
||||||
|
|
||||||
# Copy dependency files + autogpt_libs (path dependency)
|
# Migration stage only needs schema and migrations - much lighter than full backend
|
||||||
COPY autogpt_platform/autogpt_libs /app/autogpt_platform/autogpt_libs
|
COPY autogpt_platform/backend/schema.prisma /app/autogpt_platform/backend/
|
||||||
COPY autogpt_platform/backend/poetry.lock autogpt_platform/backend/pyproject.toml ./
|
COPY autogpt_platform/backend/backend/data/partial_types.py /app/autogpt_platform/backend/backend/data/partial_types.py
|
||||||
|
COPY autogpt_platform/backend/migrations /app/autogpt_platform/backend/migrations
|
||||||
|
|
||||||
# Copy backend code + docs (for Copilot docs search)
|
FROM server_dependencies AS server
|
||||||
COPY autogpt_platform/backend ./
|
|
||||||
|
COPY autogpt_platform/backend /app/autogpt_platform/backend
|
||||||
COPY docs /app/docs
|
COPY docs /app/docs
|
||||||
# Install the project package to create entry point scripts in .venv/bin/
|
RUN poetry install --no-ansi --only-root
|
||||||
# (e.g., rest, executor, ws, db, scheduler, notification - see [tool.poetry.scripts])
|
|
||||||
RUN POETRY_VIRTUALENVS_CREATE=true POETRY_VIRTUALENVS_IN_PROJECT=true \
|
|
||||||
poetry install --no-ansi --only-root
|
|
||||||
|
|
||||||
ENV PORT=8000
|
ENV PORT=8000
|
||||||
|
|
||||||
CMD ["rest"]
|
CMD ["poetry", "run", "rest"]
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
"""Common test fixtures for server tests.
|
"""Common test fixtures for server tests."""
|
||||||
|
|
||||||
Note: Common fixtures like test_user_id, admin_user_id, target_user_id,
|
|
||||||
setup_test_user, and setup_admin_user are defined in the parent conftest.py
|
|
||||||
(backend/conftest.py) and are available here automatically.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pytest_snapshot.plugin import Snapshot
|
from pytest_snapshot.plugin import Snapshot
|
||||||
@@ -16,6 +11,54 @@ def configured_snapshot(snapshot: Snapshot) -> Snapshot:
|
|||||||
return snapshot
|
return snapshot
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def test_user_id() -> str:
|
||||||
|
"""Test user ID fixture."""
|
||||||
|
return "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def admin_user_id() -> str:
|
||||||
|
"""Admin user ID fixture."""
|
||||||
|
return "4e53486c-cf57-477e-ba2a-cb02dc828e1b"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def target_user_id() -> str:
|
||||||
|
"""Target user ID fixture."""
|
||||||
|
return "5e53486c-cf57-477e-ba2a-cb02dc828e1c"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def setup_test_user(test_user_id):
|
||||||
|
"""Create test user in database before tests."""
|
||||||
|
from backend.data.user import get_or_create_user
|
||||||
|
|
||||||
|
# Create the test user in the database using JWT token format
|
||||||
|
user_data = {
|
||||||
|
"sub": test_user_id,
|
||||||
|
"email": "test@example.com",
|
||||||
|
"user_metadata": {"name": "Test User"},
|
||||||
|
}
|
||||||
|
await get_or_create_user(user_data)
|
||||||
|
return test_user_id
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def setup_admin_user(admin_user_id):
|
||||||
|
"""Create admin user in database before tests."""
|
||||||
|
from backend.data.user import get_or_create_user
|
||||||
|
|
||||||
|
# Create the admin user in the database using JWT token format
|
||||||
|
user_data = {
|
||||||
|
"sub": admin_user_id,
|
||||||
|
"email": "test-admin@example.com",
|
||||||
|
"user_metadata": {"name": "Test Admin"},
|
||||||
|
}
|
||||||
|
await get_or_create_user(user_data)
|
||||||
|
return admin_user_id
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_jwt_user(test_user_id):
|
def mock_jwt_user(test_user_id):
|
||||||
"""Provide mock JWT payload for regular user testing."""
|
"""Provide mock JWT payload for regular user testing."""
|
||||||
|
|||||||
@@ -88,23 +88,20 @@ async def require_auth(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def require_permission(*permissions: APIKeyPermission):
|
def require_permission(permission: APIKeyPermission):
|
||||||
"""
|
"""
|
||||||
Dependency function for checking required permissions.
|
Dependency function for checking specific permissions
|
||||||
All listed permissions must be present.
|
|
||||||
(works with API keys and OAuth tokens)
|
(works with API keys and OAuth tokens)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def check_permissions(
|
async def check_permission(
|
||||||
auth: APIAuthorizationInfo = Security(require_auth),
|
auth: APIAuthorizationInfo = Security(require_auth),
|
||||||
) -> APIAuthorizationInfo:
|
) -> APIAuthorizationInfo:
|
||||||
missing = [p for p in permissions if p not in auth.scopes]
|
if permission not in auth.scopes:
|
||||||
if missing:
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
detail=f"Missing required permission(s): "
|
detail=f"Missing required permission: {permission.value}",
|
||||||
f"{', '.join(p.value for p in missing)}",
|
|
||||||
)
|
)
|
||||||
return auth
|
return auth
|
||||||
|
|
||||||
return check_permissions
|
return check_permission
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from typing import Annotated, Any, Optional, Sequence
|
from typing import Annotated, Any, Literal, Optional, Sequence
|
||||||
|
|
||||||
from fastapi import APIRouter, Body, HTTPException, Security
|
from fastapi import APIRouter, Body, HTTPException, Security
|
||||||
from prisma.enums import AgentExecutionStatus, APIKeyPermission
|
from prisma.enums import AgentExecutionStatus, APIKeyPermission
|
||||||
@@ -9,17 +9,15 @@ from pydantic import BaseModel, Field
|
|||||||
from typing_extensions import TypedDict
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
import backend.api.features.store.cache as store_cache
|
import backend.api.features.store.cache as store_cache
|
||||||
import backend.api.features.store.db as store_db
|
|
||||||
import backend.api.features.store.model as store_model
|
import backend.api.features.store.model as store_model
|
||||||
import backend.blocks
|
import backend.data.block
|
||||||
from backend.api.external.middleware import require_auth, require_permission
|
from backend.api.external.middleware import require_permission
|
||||||
from backend.data import execution as execution_db
|
from backend.data import execution as execution_db
|
||||||
from backend.data import graph as graph_db
|
from backend.data import graph as graph_db
|
||||||
from backend.data import user as user_db
|
from backend.data import user as user_db
|
||||||
from backend.data.auth.base import APIAuthorizationInfo
|
from backend.data.auth.base import APIAuthorizationInfo
|
||||||
from backend.data.block import BlockInput, CompletedBlockOutput
|
from backend.data.block import BlockInput, CompletedBlockOutput
|
||||||
from backend.executor.utils import add_graph_execution
|
from backend.executor.utils import add_graph_execution
|
||||||
from backend.integrations.webhooks.graph_lifecycle_hooks import on_graph_activate
|
|
||||||
from backend.util.settings import Settings
|
from backend.util.settings import Settings
|
||||||
|
|
||||||
from .integrations import integrations_router
|
from .integrations import integrations_router
|
||||||
@@ -69,7 +67,7 @@ async def get_user_info(
|
|||||||
dependencies=[Security(require_permission(APIKeyPermission.READ_BLOCK))],
|
dependencies=[Security(require_permission(APIKeyPermission.READ_BLOCK))],
|
||||||
)
|
)
|
||||||
async def get_graph_blocks() -> Sequence[dict[Any, Any]]:
|
async def get_graph_blocks() -> Sequence[dict[Any, Any]]:
|
||||||
blocks = [block() for block in backend.blocks.get_blocks().values()]
|
blocks = [block() for block in backend.data.block.get_blocks().values()]
|
||||||
return [b.to_dict() for b in blocks if not b.disabled]
|
return [b.to_dict() for b in blocks if not b.disabled]
|
||||||
|
|
||||||
|
|
||||||
@@ -85,7 +83,7 @@ async def execute_graph_block(
|
|||||||
require_permission(APIKeyPermission.EXECUTE_BLOCK)
|
require_permission(APIKeyPermission.EXECUTE_BLOCK)
|
||||||
),
|
),
|
||||||
) -> CompletedBlockOutput:
|
) -> CompletedBlockOutput:
|
||||||
obj = backend.blocks.get_block(block_id)
|
obj = backend.data.block.get_block(block_id)
|
||||||
if not obj:
|
if not obj:
|
||||||
raise HTTPException(status_code=404, detail=f"Block #{block_id} not found.")
|
raise HTTPException(status_code=404, detail=f"Block #{block_id} not found.")
|
||||||
if obj.disabled:
|
if obj.disabled:
|
||||||
@@ -97,43 +95,6 @@ async def execute_graph_block(
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@v1_router.post(
|
|
||||||
path="/graphs",
|
|
||||||
tags=["graphs"],
|
|
||||||
status_code=201,
|
|
||||||
dependencies=[
|
|
||||||
Security(
|
|
||||||
require_permission(
|
|
||||||
APIKeyPermission.WRITE_GRAPH, APIKeyPermission.WRITE_LIBRARY
|
|
||||||
)
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def create_graph(
|
|
||||||
graph: graph_db.Graph,
|
|
||||||
auth: APIAuthorizationInfo = Security(
|
|
||||||
require_permission(APIKeyPermission.WRITE_GRAPH, APIKeyPermission.WRITE_LIBRARY)
|
|
||||||
),
|
|
||||||
) -> graph_db.GraphModel:
|
|
||||||
"""
|
|
||||||
Create a new agent graph.
|
|
||||||
|
|
||||||
The graph will be validated and assigned a new ID.
|
|
||||||
It is automatically added to the user's library.
|
|
||||||
"""
|
|
||||||
from backend.api.features.library import db as library_db
|
|
||||||
|
|
||||||
graph_model = graph_db.make_graph_model(graph, auth.user_id)
|
|
||||||
graph_model.reassign_ids(user_id=auth.user_id, reassign_graph_id=True)
|
|
||||||
graph_model.validate_graph(for_run=False)
|
|
||||||
|
|
||||||
await graph_db.create_graph(graph_model, user_id=auth.user_id)
|
|
||||||
await library_db.create_library_agent(graph_model, auth.user_id)
|
|
||||||
activated_graph = await on_graph_activate(graph_model, user_id=auth.user_id)
|
|
||||||
|
|
||||||
return activated_graph
|
|
||||||
|
|
||||||
|
|
||||||
@v1_router.post(
|
@v1_router.post(
|
||||||
path="/graphs/{graph_id}/execute/{graph_version}",
|
path="/graphs/{graph_id}/execute/{graph_version}",
|
||||||
tags=["graphs"],
|
tags=["graphs"],
|
||||||
@@ -231,13 +192,13 @@ async def get_graph_execution_results(
|
|||||||
@v1_router.get(
|
@v1_router.get(
|
||||||
path="/store/agents",
|
path="/store/agents",
|
||||||
tags=["store"],
|
tags=["store"],
|
||||||
dependencies=[Security(require_auth)], # data is public; auth required as anti-DDoS
|
dependencies=[Security(require_permission(APIKeyPermission.READ_STORE))],
|
||||||
response_model=store_model.StoreAgentsResponse,
|
response_model=store_model.StoreAgentsResponse,
|
||||||
)
|
)
|
||||||
async def get_store_agents(
|
async def get_store_agents(
|
||||||
featured: bool = False,
|
featured: bool = False,
|
||||||
creator: str | None = None,
|
creator: str | None = None,
|
||||||
sorted_by: store_db.StoreAgentsSortOptions | None = None,
|
sorted_by: Literal["rating", "runs", "name", "updated_at"] | None = None,
|
||||||
search_query: str | None = None,
|
search_query: str | None = None,
|
||||||
category: str | None = None,
|
category: str | None = None,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
@@ -279,7 +240,7 @@ async def get_store_agents(
|
|||||||
@v1_router.get(
|
@v1_router.get(
|
||||||
path="/store/agents/{username}/{agent_name}",
|
path="/store/agents/{username}/{agent_name}",
|
||||||
tags=["store"],
|
tags=["store"],
|
||||||
dependencies=[Security(require_auth)], # data is public; auth required as anti-DDoS
|
dependencies=[Security(require_permission(APIKeyPermission.READ_STORE))],
|
||||||
response_model=store_model.StoreAgentDetails,
|
response_model=store_model.StoreAgentDetails,
|
||||||
)
|
)
|
||||||
async def get_store_agent(
|
async def get_store_agent(
|
||||||
@@ -307,13 +268,13 @@ async def get_store_agent(
|
|||||||
@v1_router.get(
|
@v1_router.get(
|
||||||
path="/store/creators",
|
path="/store/creators",
|
||||||
tags=["store"],
|
tags=["store"],
|
||||||
dependencies=[Security(require_auth)], # data is public; auth required as anti-DDoS
|
dependencies=[Security(require_permission(APIKeyPermission.READ_STORE))],
|
||||||
response_model=store_model.CreatorsResponse,
|
response_model=store_model.CreatorsResponse,
|
||||||
)
|
)
|
||||||
async def get_store_creators(
|
async def get_store_creators(
|
||||||
featured: bool = False,
|
featured: bool = False,
|
||||||
search_query: str | None = None,
|
search_query: str | None = None,
|
||||||
sorted_by: store_db.StoreCreatorsSortOptions | None = None,
|
sorted_by: Literal["agent_rating", "agent_runs", "num_agents"] | None = None,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
page_size: int = 20,
|
page_size: int = 20,
|
||||||
) -> store_model.CreatorsResponse:
|
) -> store_model.CreatorsResponse:
|
||||||
@@ -349,7 +310,7 @@ async def get_store_creators(
|
|||||||
@v1_router.get(
|
@v1_router.get(
|
||||||
path="/store/creators/{username}",
|
path="/store/creators/{username}",
|
||||||
tags=["store"],
|
tags=["store"],
|
||||||
dependencies=[Security(require_auth)], # data is public; auth required as anti-DDoS
|
dependencies=[Security(require_permission(APIKeyPermission.READ_STORE))],
|
||||||
response_model=store_model.CreatorDetails,
|
response_model=store_model.CreatorDetails,
|
||||||
)
|
)
|
||||||
async def get_store_creator(
|
async def get_store_creator(
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ from prisma.enums import APIKeyPermission
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
from backend.api.external.middleware import require_permission
|
from backend.api.external.middleware import require_permission
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
from backend.copilot.tools import find_agent_tool, run_agent_tool
|
from backend.api.features.chat.tools import find_agent_tool, run_agent_tool
|
||||||
from backend.copilot.tools.models import ToolResponseBase
|
from backend.api.features.chat.tools.models import ToolResponseBase
|
||||||
from backend.data.auth.base import APIAuthorizationInfo
|
from backend.data.auth.base import APIAuthorizationInfo
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|||||||
@@ -1,17 +1,8 @@
|
|||||||
from __future__ import annotations
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
from typing import TYPE_CHECKING, Any, Literal, Optional
|
|
||||||
|
|
||||||
import prisma.enums
|
|
||||||
from pydantic import BaseModel, EmailStr
|
|
||||||
|
|
||||||
from backend.data.model import UserTransaction
|
from backend.data.model import UserTransaction
|
||||||
from backend.util.models import Pagination
|
from backend.util.models import Pagination
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from backend.data.invited_user import BulkInvitedUsersResult, InvitedUserRecord
|
|
||||||
|
|
||||||
|
|
||||||
class UserHistoryResponse(BaseModel):
|
class UserHistoryResponse(BaseModel):
|
||||||
"""Response model for listings with version history"""
|
"""Response model for listings with version history"""
|
||||||
@@ -23,70 +14,3 @@ class UserHistoryResponse(BaseModel):
|
|||||||
class AddUserCreditsResponse(BaseModel):
|
class AddUserCreditsResponse(BaseModel):
|
||||||
new_balance: int
|
new_balance: int
|
||||||
transaction_key: str
|
transaction_key: str
|
||||||
|
|
||||||
|
|
||||||
class CreateInvitedUserRequest(BaseModel):
|
|
||||||
email: EmailStr
|
|
||||||
name: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
class InvitedUserResponse(BaseModel):
|
|
||||||
id: str
|
|
||||||
email: str
|
|
||||||
status: prisma.enums.InvitedUserStatus
|
|
||||||
auth_user_id: Optional[str] = None
|
|
||||||
name: Optional[str] = None
|
|
||||||
tally_understanding: Optional[dict[str, Any]] = None
|
|
||||||
tally_status: prisma.enums.TallyComputationStatus
|
|
||||||
tally_computed_at: Optional[datetime] = None
|
|
||||||
tally_error: Optional[str] = None
|
|
||||||
created_at: datetime
|
|
||||||
updated_at: datetime
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_record(cls, record: InvitedUserRecord) -> InvitedUserResponse:
|
|
||||||
return cls.model_validate(record.model_dump())
|
|
||||||
|
|
||||||
|
|
||||||
class InvitedUsersResponse(BaseModel):
|
|
||||||
invited_users: list[InvitedUserResponse]
|
|
||||||
pagination: Pagination
|
|
||||||
|
|
||||||
|
|
||||||
class BulkInvitedUserRowResponse(BaseModel):
|
|
||||||
row_number: int
|
|
||||||
email: Optional[str] = None
|
|
||||||
name: Optional[str] = None
|
|
||||||
status: Literal["CREATED", "SKIPPED", "ERROR"]
|
|
||||||
message: str
|
|
||||||
invited_user: Optional[InvitedUserResponse] = None
|
|
||||||
|
|
||||||
|
|
||||||
class BulkInvitedUsersResponse(BaseModel):
|
|
||||||
created_count: int
|
|
||||||
skipped_count: int
|
|
||||||
error_count: int
|
|
||||||
results: list[BulkInvitedUserRowResponse]
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_result(cls, result: BulkInvitedUsersResult) -> BulkInvitedUsersResponse:
|
|
||||||
return cls(
|
|
||||||
created_count=result.created_count,
|
|
||||||
skipped_count=result.skipped_count,
|
|
||||||
error_count=result.error_count,
|
|
||||||
results=[
|
|
||||||
BulkInvitedUserRowResponse(
|
|
||||||
row_number=row.row_number,
|
|
||||||
email=row.email,
|
|
||||||
name=row.name,
|
|
||||||
status=row.status,
|
|
||||||
message=row.message,
|
|
||||||
invited_user=(
|
|
||||||
InvitedUserResponse.from_record(row.invited_user)
|
|
||||||
if row.invited_user is not None
|
|
||||||
else None
|
|
||||||
),
|
|
||||||
)
|
|
||||||
for row in result.results
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -24,13 +24,14 @@ router = fastapi.APIRouter(
|
|||||||
@router.get(
|
@router.get(
|
||||||
"/listings",
|
"/listings",
|
||||||
summary="Get Admin Listings History",
|
summary="Get Admin Listings History",
|
||||||
|
response_model=store_model.StoreListingsWithVersionsResponse,
|
||||||
)
|
)
|
||||||
async def get_admin_listings_with_versions(
|
async def get_admin_listings_with_versions(
|
||||||
status: typing.Optional[prisma.enums.SubmissionStatus] = None,
|
status: typing.Optional[prisma.enums.SubmissionStatus] = None,
|
||||||
search: typing.Optional[str] = None,
|
search: typing.Optional[str] = None,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
page_size: int = 20,
|
page_size: int = 20,
|
||||||
) -> store_model.StoreListingsWithVersionsAdminViewResponse:
|
):
|
||||||
"""
|
"""
|
||||||
Get store listings with their version history for admins.
|
Get store listings with their version history for admins.
|
||||||
|
|
||||||
@@ -44,26 +45,36 @@ async def get_admin_listings_with_versions(
|
|||||||
page_size: Number of items per page
|
page_size: Number of items per page
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Paginated listings with their versions
|
StoreListingsWithVersionsResponse with listings and their versions
|
||||||
"""
|
"""
|
||||||
listings = await store_db.get_admin_listings_with_versions(
|
try:
|
||||||
status=status,
|
listings = await store_db.get_admin_listings_with_versions(
|
||||||
search_query=search,
|
status=status,
|
||||||
page=page,
|
search_query=search,
|
||||||
page_size=page_size,
|
page=page,
|
||||||
)
|
page_size=page_size,
|
||||||
return listings
|
)
|
||||||
|
return listings
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception("Error getting admin listings with versions: %s", e)
|
||||||
|
return fastapi.responses.JSONResponse(
|
||||||
|
status_code=500,
|
||||||
|
content={
|
||||||
|
"detail": "An error occurred while retrieving listings with versions"
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/submissions/{store_listing_version_id}/review",
|
"/submissions/{store_listing_version_id}/review",
|
||||||
summary="Review Store Submission",
|
summary="Review Store Submission",
|
||||||
|
response_model=store_model.StoreSubmission,
|
||||||
)
|
)
|
||||||
async def review_submission(
|
async def review_submission(
|
||||||
store_listing_version_id: str,
|
store_listing_version_id: str,
|
||||||
request: store_model.ReviewSubmissionRequest,
|
request: store_model.ReviewSubmissionRequest,
|
||||||
user_id: str = fastapi.Security(autogpt_libs.auth.get_user_id),
|
user_id: str = fastapi.Security(autogpt_libs.auth.get_user_id),
|
||||||
) -> store_model.StoreSubmissionAdminView:
|
):
|
||||||
"""
|
"""
|
||||||
Review a store listing submission.
|
Review a store listing submission.
|
||||||
|
|
||||||
@@ -73,24 +84,31 @@ async def review_submission(
|
|||||||
user_id: Authenticated admin user performing the review
|
user_id: Authenticated admin user performing the review
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
StoreSubmissionAdminView with updated review information
|
StoreSubmission with updated review information
|
||||||
"""
|
"""
|
||||||
already_approved = await store_db.check_submission_already_approved(
|
try:
|
||||||
store_listing_version_id=store_listing_version_id,
|
already_approved = await store_db.check_submission_already_approved(
|
||||||
)
|
store_listing_version_id=store_listing_version_id,
|
||||||
submission = await store_db.review_store_submission(
|
)
|
||||||
store_listing_version_id=store_listing_version_id,
|
submission = await store_db.review_store_submission(
|
||||||
is_approved=request.is_approved,
|
store_listing_version_id=store_listing_version_id,
|
||||||
external_comments=request.comments,
|
is_approved=request.is_approved,
|
||||||
internal_comments=request.internal_comments or "",
|
external_comments=request.comments,
|
||||||
reviewer_id=user_id,
|
internal_comments=request.internal_comments or "",
|
||||||
)
|
reviewer_id=user_id,
|
||||||
|
)
|
||||||
|
|
||||||
state_changed = already_approved != request.is_approved
|
state_changed = already_approved != request.is_approved
|
||||||
# Clear caches whenever approval state changes, since store visibility can change
|
# Clear caches when the request is approved as it updates what is shown on the store
|
||||||
if state_changed:
|
if state_changed:
|
||||||
store_cache.clear_all_caches()
|
store_cache.clear_all_caches()
|
||||||
return submission
|
return submission
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception("Error reviewing submission: %s", e)
|
||||||
|
return fastapi.responses.JSONResponse(
|
||||||
|
status_code=500,
|
||||||
|
content={"detail": "An error occurred while reviewing the submission"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
|
|||||||
@@ -1,137 +0,0 @@
|
|||||||
import logging
|
|
||||||
import math
|
|
||||||
|
|
||||||
from autogpt_libs.auth import get_user_id, requires_admin_user
|
|
||||||
from fastapi import APIRouter, File, Query, Security, UploadFile
|
|
||||||
|
|
||||||
from backend.data.invited_user import (
|
|
||||||
bulk_create_invited_users_from_file,
|
|
||||||
create_invited_user,
|
|
||||||
list_invited_users,
|
|
||||||
retry_invited_user_tally,
|
|
||||||
revoke_invited_user,
|
|
||||||
)
|
|
||||||
from backend.data.tally import mask_email
|
|
||||||
from backend.util.models import Pagination
|
|
||||||
|
|
||||||
from .model import (
|
|
||||||
BulkInvitedUsersResponse,
|
|
||||||
CreateInvitedUserRequest,
|
|
||||||
InvitedUserResponse,
|
|
||||||
InvitedUsersResponse,
|
|
||||||
)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(
|
|
||||||
prefix="/admin",
|
|
||||||
tags=["users", "admin"],
|
|
||||||
dependencies=[Security(requires_admin_user)],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
|
||||||
"/invited-users",
|
|
||||||
response_model=InvitedUsersResponse,
|
|
||||||
summary="List Invited Users",
|
|
||||||
)
|
|
||||||
async def get_invited_users(
|
|
||||||
admin_user_id: str = Security(get_user_id),
|
|
||||||
page: int = Query(1, ge=1),
|
|
||||||
page_size: int = Query(50, ge=1, le=200),
|
|
||||||
) -> InvitedUsersResponse:
|
|
||||||
logger.info("Admin user %s requested invited users", admin_user_id)
|
|
||||||
invited_users, total = await list_invited_users(page=page, page_size=page_size)
|
|
||||||
return InvitedUsersResponse(
|
|
||||||
invited_users=[InvitedUserResponse.from_record(iu) for iu in invited_users],
|
|
||||||
pagination=Pagination(
|
|
||||||
total_items=total,
|
|
||||||
total_pages=max(1, math.ceil(total / page_size)),
|
|
||||||
current_page=page,
|
|
||||||
page_size=page_size,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
|
||||||
"/invited-users",
|
|
||||||
response_model=InvitedUserResponse,
|
|
||||||
summary="Create Invited User",
|
|
||||||
)
|
|
||||||
async def create_invited_user_route(
|
|
||||||
request: CreateInvitedUserRequest,
|
|
||||||
admin_user_id: str = Security(get_user_id),
|
|
||||||
) -> InvitedUserResponse:
|
|
||||||
logger.info(
|
|
||||||
"Admin user %s creating invited user for %s",
|
|
||||||
admin_user_id,
|
|
||||||
mask_email(request.email),
|
|
||||||
)
|
|
||||||
invited_user = await create_invited_user(request.email, request.name)
|
|
||||||
logger.info(
|
|
||||||
"Admin user %s created invited user %s",
|
|
||||||
admin_user_id,
|
|
||||||
invited_user.id,
|
|
||||||
)
|
|
||||||
return InvitedUserResponse.from_record(invited_user)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
|
||||||
"/invited-users/bulk",
|
|
||||||
response_model=BulkInvitedUsersResponse,
|
|
||||||
summary="Bulk Create Invited Users",
|
|
||||||
operation_id="postV2BulkCreateInvitedUsers",
|
|
||||||
)
|
|
||||||
async def bulk_create_invited_users_route(
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
admin_user_id: str = Security(get_user_id),
|
|
||||||
) -> BulkInvitedUsersResponse:
|
|
||||||
logger.info(
|
|
||||||
"Admin user %s bulk invited users from %s",
|
|
||||||
admin_user_id,
|
|
||||||
file.filename or "<unnamed>",
|
|
||||||
)
|
|
||||||
content = await file.read()
|
|
||||||
result = await bulk_create_invited_users_from_file(file.filename, content)
|
|
||||||
return BulkInvitedUsersResponse.from_result(result)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
|
||||||
"/invited-users/{invited_user_id}/revoke",
|
|
||||||
response_model=InvitedUserResponse,
|
|
||||||
summary="Revoke Invited User",
|
|
||||||
)
|
|
||||||
async def revoke_invited_user_route(
|
|
||||||
invited_user_id: str,
|
|
||||||
admin_user_id: str = Security(get_user_id),
|
|
||||||
) -> InvitedUserResponse:
|
|
||||||
logger.info(
|
|
||||||
"Admin user %s revoking invited user %s", admin_user_id, invited_user_id
|
|
||||||
)
|
|
||||||
invited_user = await revoke_invited_user(invited_user_id)
|
|
||||||
logger.info("Admin user %s revoked invited user %s", admin_user_id, invited_user_id)
|
|
||||||
return InvitedUserResponse.from_record(invited_user)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
|
||||||
"/invited-users/{invited_user_id}/retry-tally",
|
|
||||||
response_model=InvitedUserResponse,
|
|
||||||
summary="Retry Invited User Tally",
|
|
||||||
)
|
|
||||||
async def retry_invited_user_tally_route(
|
|
||||||
invited_user_id: str,
|
|
||||||
admin_user_id: str = Security(get_user_id),
|
|
||||||
) -> InvitedUserResponse:
|
|
||||||
logger.info(
|
|
||||||
"Admin user %s retrying Tally seed for invited user %s",
|
|
||||||
admin_user_id,
|
|
||||||
invited_user_id,
|
|
||||||
)
|
|
||||||
invited_user = await retry_invited_user_tally(invited_user_id)
|
|
||||||
logger.info(
|
|
||||||
"Admin user %s retried Tally seed for invited user %s",
|
|
||||||
admin_user_id,
|
|
||||||
invited_user_id,
|
|
||||||
)
|
|
||||||
return InvitedUserResponse.from_record(invited_user)
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
from datetime import datetime, timezone
|
|
||||||
from unittest.mock import AsyncMock
|
|
||||||
|
|
||||||
import fastapi
|
|
||||||
import fastapi.testclient
|
|
||||||
import prisma.enums
|
|
||||||
import pytest
|
|
||||||
import pytest_mock
|
|
||||||
from autogpt_libs.auth.jwt_utils import get_jwt_payload
|
|
||||||
|
|
||||||
from backend.data.invited_user import (
|
|
||||||
BulkInvitedUserRowResult,
|
|
||||||
BulkInvitedUsersResult,
|
|
||||||
InvitedUserRecord,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .user_admin_routes import router as user_admin_router
|
|
||||||
|
|
||||||
app = fastapi.FastAPI()
|
|
||||||
app.include_router(user_admin_router)
|
|
||||||
|
|
||||||
client = fastapi.testclient.TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def setup_app_admin_auth(mock_jwt_admin):
|
|
||||||
app.dependency_overrides[get_jwt_payload] = mock_jwt_admin["get_jwt_payload"]
|
|
||||||
yield
|
|
||||||
app.dependency_overrides.clear()
|
|
||||||
|
|
||||||
|
|
||||||
def _sample_invited_user() -> InvitedUserRecord:
|
|
||||||
now = datetime.now(timezone.utc)
|
|
||||||
return InvitedUserRecord(
|
|
||||||
id="invite-1",
|
|
||||||
email="invited@example.com",
|
|
||||||
status=prisma.enums.InvitedUserStatus.INVITED,
|
|
||||||
auth_user_id=None,
|
|
||||||
name="Invited User",
|
|
||||||
tally_understanding=None,
|
|
||||||
tally_status=prisma.enums.TallyComputationStatus.PENDING,
|
|
||||||
tally_computed_at=None,
|
|
||||||
tally_error=None,
|
|
||||||
created_at=now,
|
|
||||||
updated_at=now,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _sample_bulk_invited_users_result() -> BulkInvitedUsersResult:
|
|
||||||
return BulkInvitedUsersResult(
|
|
||||||
created_count=1,
|
|
||||||
skipped_count=1,
|
|
||||||
error_count=0,
|
|
||||||
results=[
|
|
||||||
BulkInvitedUserRowResult(
|
|
||||||
row_number=1,
|
|
||||||
email="invited@example.com",
|
|
||||||
name=None,
|
|
||||||
status="CREATED",
|
|
||||||
message="Invite created",
|
|
||||||
invited_user=_sample_invited_user(),
|
|
||||||
),
|
|
||||||
BulkInvitedUserRowResult(
|
|
||||||
row_number=2,
|
|
||||||
email="duplicate@example.com",
|
|
||||||
name=None,
|
|
||||||
status="SKIPPED",
|
|
||||||
message="An invited user with this email already exists",
|
|
||||||
invited_user=None,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_invited_users(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
) -> None:
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.admin.user_admin_routes.list_invited_users",
|
|
||||||
AsyncMock(return_value=([_sample_invited_user()], 1)),
|
|
||||||
)
|
|
||||||
|
|
||||||
response = client.get("/admin/invited-users")
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
data = response.json()
|
|
||||||
assert len(data["invited_users"]) == 1
|
|
||||||
assert data["invited_users"][0]["email"] == "invited@example.com"
|
|
||||||
assert data["invited_users"][0]["status"] == "INVITED"
|
|
||||||
assert data["pagination"]["total_items"] == 1
|
|
||||||
assert data["pagination"]["current_page"] == 1
|
|
||||||
assert data["pagination"]["page_size"] == 50
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_invited_user(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
) -> None:
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.admin.user_admin_routes.create_invited_user",
|
|
||||||
AsyncMock(return_value=_sample_invited_user()),
|
|
||||||
)
|
|
||||||
|
|
||||||
response = client.post(
|
|
||||||
"/admin/invited-users",
|
|
||||||
json={"email": "invited@example.com", "name": "Invited User"},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
data = response.json()
|
|
||||||
assert data["email"] == "invited@example.com"
|
|
||||||
assert data["name"] == "Invited User"
|
|
||||||
|
|
||||||
|
|
||||||
def test_bulk_create_invited_users(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
) -> None:
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.admin.user_admin_routes.bulk_create_invited_users_from_file",
|
|
||||||
AsyncMock(return_value=_sample_bulk_invited_users_result()),
|
|
||||||
)
|
|
||||||
|
|
||||||
response = client.post(
|
|
||||||
"/admin/invited-users/bulk",
|
|
||||||
files={
|
|
||||||
"file": ("invites.txt", b"invited@example.com\nduplicate@example.com\n")
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
data = response.json()
|
|
||||||
assert data["created_count"] == 1
|
|
||||||
assert data["skipped_count"] == 1
|
|
||||||
assert data["results"][0]["status"] == "CREATED"
|
|
||||||
assert data["results"][1]["status"] == "SKIPPED"
|
|
||||||
|
|
||||||
|
|
||||||
def test_revoke_invited_user(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
) -> None:
|
|
||||||
revoked = _sample_invited_user().model_copy(
|
|
||||||
update={"status": prisma.enums.InvitedUserStatus.REVOKED}
|
|
||||||
)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.admin.user_admin_routes.revoke_invited_user",
|
|
||||||
AsyncMock(return_value=revoked),
|
|
||||||
)
|
|
||||||
|
|
||||||
response = client.post("/admin/invited-users/invite-1/revoke")
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json()["status"] == "REVOKED"
|
|
||||||
|
|
||||||
|
|
||||||
def test_retry_invited_user_tally(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
) -> None:
|
|
||||||
retried = _sample_invited_user().model_copy(
|
|
||||||
update={"tally_status": prisma.enums.TallyComputationStatus.RUNNING}
|
|
||||||
)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.admin.user_admin_routes.retry_invited_user_tally",
|
|
||||||
AsyncMock(return_value=retried),
|
|
||||||
)
|
|
||||||
|
|
||||||
response = client.post("/admin/invited-users/invite-1/retry-tally")
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json()["tally_status"] == "RUNNING"
|
|
||||||
@@ -1,26 +1,20 @@
|
|||||||
import logging
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
from difflib import SequenceMatcher
|
from difflib import SequenceMatcher
|
||||||
from typing import Any, Sequence, get_args, get_origin
|
from typing import Sequence
|
||||||
|
|
||||||
import prisma
|
import prisma
|
||||||
from prisma.enums import ContentType
|
|
||||||
from prisma.models import mv_suggested_blocks
|
|
||||||
|
|
||||||
import backend.api.features.library.db as library_db
|
import backend.api.features.library.db as library_db
|
||||||
import backend.api.features.library.model as library_model
|
import backend.api.features.library.model as library_model
|
||||||
import backend.api.features.store.db as store_db
|
import backend.api.features.store.db as store_db
|
||||||
import backend.api.features.store.model as store_model
|
import backend.api.features.store.model as store_model
|
||||||
from backend.api.features.store.hybrid_search import unified_hybrid_search
|
import backend.data.block
|
||||||
from backend.blocks import load_all_blocks
|
from backend.blocks import load_all_blocks
|
||||||
from backend.blocks._base import (
|
|
||||||
AnyBlockSchema,
|
|
||||||
BlockCategory,
|
|
||||||
BlockInfo,
|
|
||||||
BlockSchema,
|
|
||||||
BlockType,
|
|
||||||
)
|
|
||||||
from backend.blocks.llm import LlmModel
|
from backend.blocks.llm import LlmModel
|
||||||
|
from backend.data.block import AnyBlockSchema, BlockCategory, BlockInfo, BlockSchema
|
||||||
|
from backend.data.db import query_raw_with_schema
|
||||||
from backend.integrations.providers import ProviderName
|
from backend.integrations.providers import ProviderName
|
||||||
from backend.util.cache import cached
|
from backend.util.cache import cached
|
||||||
from backend.util.models import Pagination
|
from backend.util.models import Pagination
|
||||||
@@ -28,7 +22,7 @@ from backend.util.models import Pagination
|
|||||||
from .model import (
|
from .model import (
|
||||||
BlockCategoryResponse,
|
BlockCategoryResponse,
|
||||||
BlockResponse,
|
BlockResponse,
|
||||||
BlockTypeFilter,
|
BlockType,
|
||||||
CountResponse,
|
CountResponse,
|
||||||
FilterType,
|
FilterType,
|
||||||
Provider,
|
Provider,
|
||||||
@@ -43,16 +37,6 @@ MAX_LIBRARY_AGENT_RESULTS = 100
|
|||||||
MAX_MARKETPLACE_AGENT_RESULTS = 100
|
MAX_MARKETPLACE_AGENT_RESULTS = 100
|
||||||
MIN_SCORE_FOR_FILTERED_RESULTS = 10.0
|
MIN_SCORE_FOR_FILTERED_RESULTS = 10.0
|
||||||
|
|
||||||
# Boost blocks over marketplace agents in search results
|
|
||||||
BLOCK_SCORE_BOOST = 50.0
|
|
||||||
|
|
||||||
# Block IDs to exclude from search results
|
|
||||||
EXCLUDED_BLOCK_IDS = frozenset(
|
|
||||||
{
|
|
||||||
"e189baac-8c20-45a1-94a7-55177ea42565", # AgentExecutorBlock
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
SearchResultItem = BlockInfo | library_model.LibraryAgent | store_model.StoreAgent
|
SearchResultItem = BlockInfo | library_model.LibraryAgent | store_model.StoreAgent
|
||||||
|
|
||||||
|
|
||||||
@@ -75,8 +59,8 @@ def get_block_categories(category_blocks: int = 3) -> list[BlockCategoryResponse
|
|||||||
|
|
||||||
for block_type in load_all_blocks().values():
|
for block_type in load_all_blocks().values():
|
||||||
block: AnyBlockSchema = block_type()
|
block: AnyBlockSchema = block_type()
|
||||||
# Skip disabled and excluded blocks
|
# Skip disabled blocks
|
||||||
if block.disabled or block.id in EXCLUDED_BLOCK_IDS:
|
if block.disabled:
|
||||||
continue
|
continue
|
||||||
# Skip blocks that don't have categories (all should have at least one)
|
# Skip blocks that don't have categories (all should have at least one)
|
||||||
if not block.categories:
|
if not block.categories:
|
||||||
@@ -104,7 +88,7 @@ def get_block_categories(category_blocks: int = 3) -> list[BlockCategoryResponse
|
|||||||
def get_blocks(
|
def get_blocks(
|
||||||
*,
|
*,
|
||||||
category: str | None = None,
|
category: str | None = None,
|
||||||
type: BlockTypeFilter | None = None,
|
type: BlockType | None = None,
|
||||||
provider: ProviderName | None = None,
|
provider: ProviderName | None = None,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
page_size: int = 50,
|
page_size: int = 50,
|
||||||
@@ -127,9 +111,6 @@ def get_blocks(
|
|||||||
# Skip disabled blocks
|
# Skip disabled blocks
|
||||||
if block.disabled:
|
if block.disabled:
|
||||||
continue
|
continue
|
||||||
# Skip excluded blocks
|
|
||||||
if block.id in EXCLUDED_BLOCK_IDS:
|
|
||||||
continue
|
|
||||||
# Skip blocks that don't match the category
|
# Skip blocks that don't match the category
|
||||||
if category and category not in {c.name.lower() for c in block.categories}:
|
if category and category not in {c.name.lower() for c in block.categories}:
|
||||||
continue
|
continue
|
||||||
@@ -269,25 +250,14 @@ async def _build_cached_search_results(
|
|||||||
"my_agents": 0,
|
"my_agents": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use hybrid search when query is present, otherwise list all blocks
|
block_results, block_total, integration_total = _collect_block_results(
|
||||||
if (include_blocks or include_integrations) and normalized_query:
|
normalized_query=normalized_query,
|
||||||
block_results, block_total, integration_total = await _hybrid_search_blocks(
|
include_blocks=include_blocks,
|
||||||
query=search_query,
|
include_integrations=include_integrations,
|
||||||
include_blocks=include_blocks,
|
)
|
||||||
include_integrations=include_integrations,
|
scored_items.extend(block_results)
|
||||||
)
|
total_items["blocks"] = block_total
|
||||||
scored_items.extend(block_results)
|
total_items["integrations"] = integration_total
|
||||||
total_items["blocks"] = block_total
|
|
||||||
total_items["integrations"] = integration_total
|
|
||||||
elif include_blocks or include_integrations:
|
|
||||||
# No query - list all blocks using in-memory approach
|
|
||||||
block_results, block_total, integration_total = _collect_block_results(
|
|
||||||
include_blocks=include_blocks,
|
|
||||||
include_integrations=include_integrations,
|
|
||||||
)
|
|
||||||
scored_items.extend(block_results)
|
|
||||||
total_items["blocks"] = block_total
|
|
||||||
total_items["integrations"] = integration_total
|
|
||||||
|
|
||||||
if include_library_agents:
|
if include_library_agents:
|
||||||
library_response = await library_db.list_library_agents(
|
library_response = await library_db.list_library_agents(
|
||||||
@@ -332,14 +302,10 @@ async def _build_cached_search_results(
|
|||||||
|
|
||||||
def _collect_block_results(
|
def _collect_block_results(
|
||||||
*,
|
*,
|
||||||
|
normalized_query: str,
|
||||||
include_blocks: bool,
|
include_blocks: bool,
|
||||||
include_integrations: bool,
|
include_integrations: bool,
|
||||||
) -> tuple[list[_ScoredItem], int, int]:
|
) -> tuple[list[_ScoredItem], int, int]:
|
||||||
"""
|
|
||||||
Collect all blocks for listing (no search query).
|
|
||||||
|
|
||||||
All blocks get BLOCK_SCORE_BOOST to prioritize them over marketplace agents.
|
|
||||||
"""
|
|
||||||
results: list[_ScoredItem] = []
|
results: list[_ScoredItem] = []
|
||||||
block_count = 0
|
block_count = 0
|
||||||
integration_count = 0
|
integration_count = 0
|
||||||
@@ -352,10 +318,6 @@ def _collect_block_results(
|
|||||||
if block.disabled:
|
if block.disabled:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Skip excluded blocks
|
|
||||||
if block.id in EXCLUDED_BLOCK_IDS:
|
|
||||||
continue
|
|
||||||
|
|
||||||
block_info = block.get_info()
|
block_info = block.get_info()
|
||||||
credentials = list(block.input_schema.get_credentials_fields().values())
|
credentials = list(block.input_schema.get_credentials_fields().values())
|
||||||
is_integration = len(credentials) > 0
|
is_integration = len(credentials) > 0
|
||||||
@@ -365,6 +327,10 @@ def _collect_block_results(
|
|||||||
if not is_integration and not include_blocks:
|
if not is_integration and not include_blocks:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
score = _score_block(block, block_info, normalized_query)
|
||||||
|
if not _should_include_item(score, normalized_query):
|
||||||
|
continue
|
||||||
|
|
||||||
filter_type: FilterType = "integrations" if is_integration else "blocks"
|
filter_type: FilterType = "integrations" if is_integration else "blocks"
|
||||||
if is_integration:
|
if is_integration:
|
||||||
integration_count += 1
|
integration_count += 1
|
||||||
@@ -375,122 +341,8 @@ def _collect_block_results(
|
|||||||
_ScoredItem(
|
_ScoredItem(
|
||||||
item=block_info,
|
item=block_info,
|
||||||
filter_type=filter_type,
|
filter_type=filter_type,
|
||||||
score=BLOCK_SCORE_BOOST,
|
score=score,
|
||||||
sort_key=block_info.name.lower(),
|
sort_key=_get_item_name(block_info),
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return results, block_count, integration_count
|
|
||||||
|
|
||||||
|
|
||||||
async def _hybrid_search_blocks(
|
|
||||||
*,
|
|
||||||
query: str,
|
|
||||||
include_blocks: bool,
|
|
||||||
include_integrations: bool,
|
|
||||||
) -> tuple[list[_ScoredItem], int, int]:
|
|
||||||
"""
|
|
||||||
Search blocks using hybrid search with builder-specific filtering.
|
|
||||||
|
|
||||||
Uses unified_hybrid_search for semantic + lexical search, then applies
|
|
||||||
post-filtering for block/integration types and scoring adjustments.
|
|
||||||
|
|
||||||
Scoring:
|
|
||||||
- Base: hybrid relevance score (0-1) scaled to 0-100, plus BLOCK_SCORE_BOOST
|
|
||||||
to prioritize blocks over marketplace agents in combined results
|
|
||||||
- +30 for exact name match, +15 for prefix name match
|
|
||||||
- +20 if the block has an LlmModel field and the query matches an LLM model name
|
|
||||||
|
|
||||||
Args:
|
|
||||||
query: The search query string
|
|
||||||
include_blocks: Whether to include regular blocks
|
|
||||||
include_integrations: Whether to include integration blocks
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Tuple of (scored_items, block_count, integration_count)
|
|
||||||
"""
|
|
||||||
results: list[_ScoredItem] = []
|
|
||||||
block_count = 0
|
|
||||||
integration_count = 0
|
|
||||||
|
|
||||||
if not include_blocks and not include_integrations:
|
|
||||||
return results, block_count, integration_count
|
|
||||||
|
|
||||||
normalized_query = query.strip().lower()
|
|
||||||
|
|
||||||
# Fetch more results to account for post-filtering
|
|
||||||
search_results, _ = await unified_hybrid_search(
|
|
||||||
query=query,
|
|
||||||
content_types=[ContentType.BLOCK],
|
|
||||||
page=1,
|
|
||||||
page_size=150,
|
|
||||||
min_score=0.10,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Load all blocks for getting BlockInfo
|
|
||||||
all_blocks = load_all_blocks()
|
|
||||||
|
|
||||||
for result in search_results:
|
|
||||||
block_id = result["content_id"]
|
|
||||||
|
|
||||||
# Skip excluded blocks
|
|
||||||
if block_id in EXCLUDED_BLOCK_IDS:
|
|
||||||
continue
|
|
||||||
|
|
||||||
metadata = result.get("metadata", {})
|
|
||||||
hybrid_score = result.get("relevance", 0.0)
|
|
||||||
|
|
||||||
# Get the actual block class
|
|
||||||
if block_id not in all_blocks:
|
|
||||||
continue
|
|
||||||
|
|
||||||
block_cls = all_blocks[block_id]
|
|
||||||
block: AnyBlockSchema = block_cls()
|
|
||||||
|
|
||||||
if block.disabled:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Check block/integration filter using metadata
|
|
||||||
is_integration = metadata.get("is_integration", False)
|
|
||||||
|
|
||||||
if is_integration and not include_integrations:
|
|
||||||
continue
|
|
||||||
if not is_integration and not include_blocks:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Get block info
|
|
||||||
block_info = block.get_info()
|
|
||||||
|
|
||||||
# Calculate final score: scale hybrid score and add builder-specific bonuses
|
|
||||||
# Hybrid scores are 0-1, builder scores were 0-200+
|
|
||||||
# Add BLOCK_SCORE_BOOST to prioritize blocks over marketplace agents
|
|
||||||
final_score = hybrid_score * 100 + BLOCK_SCORE_BOOST
|
|
||||||
|
|
||||||
# Add LLM model match bonus
|
|
||||||
has_llm_field = metadata.get("has_llm_model_field", False)
|
|
||||||
if has_llm_field and _matches_llm_model(block.input_schema, normalized_query):
|
|
||||||
final_score += 20
|
|
||||||
|
|
||||||
# Add exact/prefix match bonus for deterministic tie-breaking
|
|
||||||
name = block_info.name.lower()
|
|
||||||
if name == normalized_query:
|
|
||||||
final_score += 30
|
|
||||||
elif name.startswith(normalized_query):
|
|
||||||
final_score += 15
|
|
||||||
|
|
||||||
# Track counts
|
|
||||||
filter_type: FilterType = "integrations" if is_integration else "blocks"
|
|
||||||
if is_integration:
|
|
||||||
integration_count += 1
|
|
||||||
else:
|
|
||||||
block_count += 1
|
|
||||||
|
|
||||||
results.append(
|
|
||||||
_ScoredItem(
|
|
||||||
item=block_info,
|
|
||||||
filter_type=filter_type,
|
|
||||||
score=final_score,
|
|
||||||
sort_key=name,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -615,8 +467,6 @@ async def _get_static_counts():
|
|||||||
block: AnyBlockSchema = block_type()
|
block: AnyBlockSchema = block_type()
|
||||||
if block.disabled:
|
if block.disabled:
|
||||||
continue
|
continue
|
||||||
if block.id in EXCLUDED_BLOCK_IDS:
|
|
||||||
continue
|
|
||||||
|
|
||||||
all_blocks += 1
|
all_blocks += 1
|
||||||
|
|
||||||
@@ -643,25 +493,47 @@ async def _get_static_counts():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _contains_type(annotation: Any, target: type) -> bool:
|
|
||||||
"""Check if an annotation is or contains the target type (handles Optional/Union/Annotated)."""
|
|
||||||
if annotation is target:
|
|
||||||
return True
|
|
||||||
origin = get_origin(annotation)
|
|
||||||
if origin is None:
|
|
||||||
return False
|
|
||||||
return any(_contains_type(arg, target) for arg in get_args(annotation))
|
|
||||||
|
|
||||||
|
|
||||||
def _matches_llm_model(schema_cls: type[BlockSchema], query: str) -> bool:
|
def _matches_llm_model(schema_cls: type[BlockSchema], query: str) -> bool:
|
||||||
for field in schema_cls.model_fields.values():
|
for field in schema_cls.model_fields.values():
|
||||||
if _contains_type(field.annotation, LlmModel):
|
if field.annotation == LlmModel:
|
||||||
# Check if query matches any value in llm_models
|
# Check if query matches any value in llm_models
|
||||||
if any(query in name for name in llm_models):
|
if any(query in name for name in llm_models):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _score_block(
|
||||||
|
block: AnyBlockSchema,
|
||||||
|
block_info: BlockInfo,
|
||||||
|
normalized_query: str,
|
||||||
|
) -> float:
|
||||||
|
if not normalized_query:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
name = block_info.name.lower()
|
||||||
|
description = block_info.description.lower()
|
||||||
|
score = _score_primary_fields(name, description, normalized_query)
|
||||||
|
|
||||||
|
category_text = " ".join(
|
||||||
|
category.get("category", "").lower() for category in block_info.categories
|
||||||
|
)
|
||||||
|
score += _score_additional_field(category_text, normalized_query, 12, 6)
|
||||||
|
|
||||||
|
credentials_info = block.input_schema.get_credentials_fields_info().values()
|
||||||
|
provider_names = [
|
||||||
|
provider.value.lower()
|
||||||
|
for info in credentials_info
|
||||||
|
for provider in info.provider
|
||||||
|
]
|
||||||
|
provider_text = " ".join(provider_names)
|
||||||
|
score += _score_additional_field(provider_text, normalized_query, 15, 6)
|
||||||
|
|
||||||
|
if _matches_llm_model(block.input_schema, normalized_query):
|
||||||
|
score += 20
|
||||||
|
|
||||||
|
return score
|
||||||
|
|
||||||
|
|
||||||
def _score_library_agent(
|
def _score_library_agent(
|
||||||
agent: library_model.LibraryAgent,
|
agent: library_model.LibraryAgent,
|
||||||
normalized_query: str,
|
normalized_query: str,
|
||||||
@@ -768,32 +640,45 @@ def _get_all_providers() -> dict[ProviderName, Provider]:
|
|||||||
return providers
|
return providers
|
||||||
|
|
||||||
|
|
||||||
@cached(ttl_seconds=3600, shared_cache=True)
|
@cached(ttl_seconds=3600)
|
||||||
async def get_suggested_blocks(count: int = 5) -> list[BlockInfo]:
|
async def get_suggested_blocks(count: int = 5) -> list[BlockInfo]:
|
||||||
"""Return the most-executed blocks from the last 14 days.
|
suggested_blocks = []
|
||||||
|
# Sum the number of executions for each block type
|
||||||
|
# Prisma cannot group by nested relations, so we do a raw query
|
||||||
|
# Calculate the cutoff timestamp
|
||||||
|
timestamp_threshold = datetime.now(timezone.utc) - timedelta(days=30)
|
||||||
|
|
||||||
Queries the mv_suggested_blocks materialized view (refreshed hourly via pg_cron)
|
results = await query_raw_with_schema(
|
||||||
and returns the top `count` blocks sorted by execution count, excluding
|
"""
|
||||||
Input/Output/Agent block types and blocks in EXCLUDED_BLOCK_IDS.
|
SELECT
|
||||||
"""
|
agent_node."agentBlockId" AS block_id,
|
||||||
results = await mv_suggested_blocks.prisma().find_many()
|
COUNT(execution.id) AS execution_count
|
||||||
|
FROM {schema_prefix}"AgentNodeExecution" execution
|
||||||
|
JOIN {schema_prefix}"AgentNode" agent_node ON execution."agentNodeId" = agent_node.id
|
||||||
|
WHERE execution."endedTime" >= $1::timestamp
|
||||||
|
GROUP BY agent_node."agentBlockId"
|
||||||
|
ORDER BY execution_count DESC;
|
||||||
|
""",
|
||||||
|
timestamp_threshold,
|
||||||
|
)
|
||||||
|
|
||||||
# Get the top blocks based on execution count
|
# Get the top blocks based on execution count
|
||||||
# But ignore Input, Output, Agent, and excluded blocks
|
# But ignore Input and Output blocks
|
||||||
blocks: list[tuple[BlockInfo, int]] = []
|
blocks: list[tuple[BlockInfo, int]] = []
|
||||||
execution_counts = {row.block_id: row.execution_count for row in results}
|
|
||||||
|
|
||||||
for block_type in load_all_blocks().values():
|
for block_type in load_all_blocks().values():
|
||||||
block: AnyBlockSchema = block_type()
|
block: AnyBlockSchema = block_type()
|
||||||
if block.disabled or block.block_type in (
|
if block.disabled or block.block_type in (
|
||||||
BlockType.INPUT,
|
backend.data.block.BlockType.INPUT,
|
||||||
BlockType.OUTPUT,
|
backend.data.block.BlockType.OUTPUT,
|
||||||
BlockType.AGENT,
|
backend.data.block.BlockType.AGENT,
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
if block.id in EXCLUDED_BLOCK_IDS:
|
# Find the execution count for this block
|
||||||
continue
|
execution_count = next(
|
||||||
execution_count = execution_counts.get(block.id, 0)
|
(row["execution_count"] for row in results if row["block_id"] == block.id),
|
||||||
|
0,
|
||||||
|
)
|
||||||
blocks.append((block.get_info(), execution_count))
|
blocks.append((block.get_info(), execution_count))
|
||||||
# Sort blocks by execution count
|
# Sort blocks by execution count
|
||||||
blocks.sort(key=lambda x: x[1], reverse=True)
|
blocks.sort(key=lambda x: x[1], reverse=True)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from pydantic import BaseModel
|
|||||||
|
|
||||||
import backend.api.features.library.model as library_model
|
import backend.api.features.library.model as library_model
|
||||||
import backend.api.features.store.model as store_model
|
import backend.api.features.store.model as store_model
|
||||||
from backend.blocks._base import BlockInfo
|
from backend.data.block import BlockInfo
|
||||||
from backend.integrations.providers import ProviderName
|
from backend.integrations.providers import ProviderName
|
||||||
from backend.util.models import Pagination
|
from backend.util.models import Pagination
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ FilterType = Literal[
|
|||||||
"my_agents",
|
"my_agents",
|
||||||
]
|
]
|
||||||
|
|
||||||
BlockTypeFilter = Literal["all", "input", "action", "output"]
|
BlockType = Literal["all", "input", "action", "output"]
|
||||||
|
|
||||||
|
|
||||||
class SearchEntry(BaseModel):
|
class SearchEntry(BaseModel):
|
||||||
@@ -27,6 +27,7 @@ class SearchEntry(BaseModel):
|
|||||||
|
|
||||||
# Suggestions
|
# Suggestions
|
||||||
class SuggestionsResponse(BaseModel):
|
class SuggestionsResponse(BaseModel):
|
||||||
|
otto_suggestions: list[str]
|
||||||
recent_searches: list[SearchEntry]
|
recent_searches: list[SearchEntry]
|
||||||
providers: list[ProviderName]
|
providers: list[ProviderName]
|
||||||
top_blocks: list[BlockInfo]
|
top_blocks: list[BlockInfo]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Annotated, Sequence, cast, get_args
|
from typing import Annotated, Sequence
|
||||||
|
|
||||||
import fastapi
|
import fastapi
|
||||||
from autogpt_libs.auth.dependencies import get_user_id, requires_user
|
from autogpt_libs.auth.dependencies import get_user_id, requires_user
|
||||||
@@ -10,8 +10,6 @@ from backend.util.models import Pagination
|
|||||||
from . import db as builder_db
|
from . import db as builder_db
|
||||||
from . import model as builder_model
|
from . import model as builder_model
|
||||||
|
|
||||||
VALID_FILTER_VALUES = get_args(builder_model.FilterType)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
router = fastapi.APIRouter(
|
router = fastapi.APIRouter(
|
||||||
@@ -51,6 +49,11 @@ async def get_suggestions(
|
|||||||
Get all suggestions for the Blocks Menu.
|
Get all suggestions for the Blocks Menu.
|
||||||
"""
|
"""
|
||||||
return builder_model.SuggestionsResponse(
|
return builder_model.SuggestionsResponse(
|
||||||
|
otto_suggestions=[
|
||||||
|
"What blocks do I need to get started?",
|
||||||
|
"Help me create a list",
|
||||||
|
"Help me feed my data to Google Maps",
|
||||||
|
],
|
||||||
recent_searches=await builder_db.get_recent_searches(user_id),
|
recent_searches=await builder_db.get_recent_searches(user_id),
|
||||||
providers=[
|
providers=[
|
||||||
ProviderName.TWITTER,
|
ProviderName.TWITTER,
|
||||||
@@ -85,7 +88,7 @@ async def get_block_categories(
|
|||||||
)
|
)
|
||||||
async def get_blocks(
|
async def get_blocks(
|
||||||
category: Annotated[str | None, fastapi.Query()] = None,
|
category: Annotated[str | None, fastapi.Query()] = None,
|
||||||
type: Annotated[builder_model.BlockTypeFilter | None, fastapi.Query()] = None,
|
type: Annotated[builder_model.BlockType | None, fastapi.Query()] = None,
|
||||||
provider: Annotated[ProviderName | None, fastapi.Query()] = None,
|
provider: Annotated[ProviderName | None, fastapi.Query()] = None,
|
||||||
page: Annotated[int, fastapi.Query()] = 1,
|
page: Annotated[int, fastapi.Query()] = 1,
|
||||||
page_size: Annotated[int, fastapi.Query()] = 50,
|
page_size: Annotated[int, fastapi.Query()] = 50,
|
||||||
@@ -148,7 +151,7 @@ async def get_providers(
|
|||||||
async def search(
|
async def search(
|
||||||
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
user_id: Annotated[str, fastapi.Security(get_user_id)],
|
||||||
search_query: Annotated[str | None, fastapi.Query()] = None,
|
search_query: Annotated[str | None, fastapi.Query()] = None,
|
||||||
filter: Annotated[str | None, fastapi.Query()] = None,
|
filter: Annotated[list[builder_model.FilterType] | None, fastapi.Query()] = None,
|
||||||
search_id: Annotated[str | None, fastapi.Query()] = None,
|
search_id: Annotated[str | None, fastapi.Query()] = None,
|
||||||
by_creator: Annotated[list[str] | None, fastapi.Query()] = None,
|
by_creator: Annotated[list[str] | None, fastapi.Query()] = None,
|
||||||
page: Annotated[int, fastapi.Query()] = 1,
|
page: Annotated[int, fastapi.Query()] = 1,
|
||||||
@@ -157,20 +160,9 @@ async def search(
|
|||||||
"""
|
"""
|
||||||
Search for blocks (including integrations), marketplace agents, and user library agents.
|
Search for blocks (including integrations), marketplace agents, and user library agents.
|
||||||
"""
|
"""
|
||||||
# Parse and validate filter parameter
|
# If no filters are provided, then we will return all types
|
||||||
filters: list[builder_model.FilterType]
|
if not filter:
|
||||||
if filter:
|
filter = [
|
||||||
filter_values = [f.strip() for f in filter.split(",")]
|
|
||||||
invalid_filters = [f for f in filter_values if f not in VALID_FILTER_VALUES]
|
|
||||||
if invalid_filters:
|
|
||||||
raise fastapi.HTTPException(
|
|
||||||
status_code=400,
|
|
||||||
detail=f"Invalid filter value(s): {', '.join(invalid_filters)}. "
|
|
||||||
f"Valid values are: {', '.join(VALID_FILTER_VALUES)}",
|
|
||||||
)
|
|
||||||
filters = cast(list[builder_model.FilterType], filter_values)
|
|
||||||
else:
|
|
||||||
filters = [
|
|
||||||
"blocks",
|
"blocks",
|
||||||
"integrations",
|
"integrations",
|
||||||
"marketplace_agents",
|
"marketplace_agents",
|
||||||
@@ -182,7 +174,7 @@ async def search(
|
|||||||
cached_results = await builder_db.get_sorted_search_results(
|
cached_results = await builder_db.get_sorted_search_results(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
search_query=search_query,
|
search_query=search_query,
|
||||||
filters=filters,
|
filters=filter,
|
||||||
by_creator=by_creator,
|
by_creator=by_creator,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -204,7 +196,7 @@ async def search(
|
|||||||
user_id,
|
user_id,
|
||||||
builder_model.SearchEntry(
|
builder_model.SearchEntry(
|
||||||
search_query=search_query,
|
search_query=search_query,
|
||||||
filter=filters,
|
filter=filter,
|
||||||
by_creator=by_creator,
|
by_creator=by_creator,
|
||||||
search_id=search_id,
|
search_id=search_id,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -0,0 +1,368 @@
|
|||||||
|
"""Redis Streams consumer for operation completion messages.
|
||||||
|
|
||||||
|
This module provides a consumer (ChatCompletionConsumer) that listens for
|
||||||
|
completion notifications (OperationCompleteMessage) from external services
|
||||||
|
(like Agent Generator) and triggers the appropriate stream registry and
|
||||||
|
chat service updates via process_operation_success/process_operation_failure.
|
||||||
|
|
||||||
|
Why Redis Streams instead of RabbitMQ?
|
||||||
|
--------------------------------------
|
||||||
|
While the project typically uses RabbitMQ for async task queues (e.g., execution
|
||||||
|
queue), Redis Streams was chosen for chat completion notifications because:
|
||||||
|
|
||||||
|
1. **Unified Infrastructure**: The SSE reconnection feature already uses Redis
|
||||||
|
Streams (via stream_registry) for message persistence and replay. Using Redis
|
||||||
|
Streams for completion notifications keeps all chat streaming infrastructure
|
||||||
|
in one system, simplifying operations and reducing cross-system coordination.
|
||||||
|
|
||||||
|
2. **Message Replay**: Redis Streams support XREAD with arbitrary message IDs,
|
||||||
|
allowing consumers to replay missed messages after reconnection. This aligns
|
||||||
|
with the SSE reconnection pattern where clients can resume from last_message_id.
|
||||||
|
|
||||||
|
3. **Consumer Groups with XAUTOCLAIM**: Redis consumer groups provide automatic
|
||||||
|
load balancing across pods with explicit message claiming (XAUTOCLAIM) for
|
||||||
|
recovering from dead consumers - ideal for the completion callback pattern.
|
||||||
|
|
||||||
|
4. **Lower Latency**: For real-time SSE updates, Redis (already in-memory for
|
||||||
|
stream_registry) provides lower latency than an additional RabbitMQ hop.
|
||||||
|
|
||||||
|
5. **Atomicity with Task State**: Completion processing often needs to update
|
||||||
|
task metadata stored in Redis. Keeping both in Redis enables simpler
|
||||||
|
transactional semantics without distributed coordination.
|
||||||
|
|
||||||
|
The consumer uses Redis Streams with consumer groups for reliable message
|
||||||
|
processing across multiple platform pods, with XAUTOCLAIM for reclaiming
|
||||||
|
stale pending messages from dead consumers.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import orjson
|
||||||
|
from prisma import Prisma
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from redis.exceptions import ResponseError
|
||||||
|
|
||||||
|
from backend.data.redis_client import get_redis_async
|
||||||
|
|
||||||
|
from . import stream_registry
|
||||||
|
from .completion_handler import process_operation_failure, process_operation_success
|
||||||
|
from .config import ChatConfig
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
config = ChatConfig()
|
||||||
|
|
||||||
|
|
||||||
|
class OperationCompleteMessage(BaseModel):
|
||||||
|
"""Message format for operation completion notifications."""
|
||||||
|
|
||||||
|
operation_id: str
|
||||||
|
task_id: str
|
||||||
|
success: bool
|
||||||
|
result: dict | str | None = None
|
||||||
|
error: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class ChatCompletionConsumer:
|
||||||
|
"""Consumer for chat operation completion messages from Redis Streams.
|
||||||
|
|
||||||
|
This consumer initializes its own Prisma client in start() to ensure
|
||||||
|
database operations work correctly within this async context.
|
||||||
|
|
||||||
|
Uses Redis consumer groups to allow multiple platform pods to consume
|
||||||
|
messages reliably with automatic redelivery on failure.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._consumer_task: asyncio.Task | None = None
|
||||||
|
self._running = False
|
||||||
|
self._prisma: Prisma | None = None
|
||||||
|
self._consumer_name = f"consumer-{uuid.uuid4().hex[:8]}"
|
||||||
|
|
||||||
|
async def start(self) -> None:
|
||||||
|
"""Start the completion consumer."""
|
||||||
|
if self._running:
|
||||||
|
logger.warning("Completion consumer already running")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create consumer group if it doesn't exist
|
||||||
|
try:
|
||||||
|
redis = await get_redis_async()
|
||||||
|
await redis.xgroup_create(
|
||||||
|
config.stream_completion_name,
|
||||||
|
config.stream_consumer_group,
|
||||||
|
id="0",
|
||||||
|
mkstream=True,
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
f"Created consumer group '{config.stream_consumer_group}' "
|
||||||
|
f"on stream '{config.stream_completion_name}'"
|
||||||
|
)
|
||||||
|
except ResponseError as e:
|
||||||
|
if "BUSYGROUP" in str(e):
|
||||||
|
logger.debug(
|
||||||
|
f"Consumer group '{config.stream_consumer_group}' already exists"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
self._running = True
|
||||||
|
self._consumer_task = asyncio.create_task(self._consume_messages())
|
||||||
|
logger.info(
|
||||||
|
f"Chat completion consumer started (consumer: {self._consumer_name})"
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _ensure_prisma(self) -> Prisma:
|
||||||
|
"""Lazily initialize Prisma client on first use."""
|
||||||
|
if self._prisma is None:
|
||||||
|
database_url = os.getenv("DATABASE_URL", "postgresql://localhost:5432")
|
||||||
|
self._prisma = Prisma(datasource={"url": database_url})
|
||||||
|
await self._prisma.connect()
|
||||||
|
logger.info("[COMPLETION] Consumer Prisma client connected (lazy init)")
|
||||||
|
return self._prisma
|
||||||
|
|
||||||
|
async def stop(self) -> None:
|
||||||
|
"""Stop the completion consumer."""
|
||||||
|
self._running = False
|
||||||
|
|
||||||
|
if self._consumer_task:
|
||||||
|
self._consumer_task.cancel()
|
||||||
|
try:
|
||||||
|
await self._consumer_task
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
self._consumer_task = None
|
||||||
|
|
||||||
|
if self._prisma:
|
||||||
|
await self._prisma.disconnect()
|
||||||
|
self._prisma = None
|
||||||
|
logger.info("[COMPLETION] Consumer Prisma client disconnected")
|
||||||
|
|
||||||
|
logger.info("Chat completion consumer stopped")
|
||||||
|
|
||||||
|
async def _consume_messages(self) -> None:
|
||||||
|
"""Main message consumption loop with retry logic."""
|
||||||
|
max_retries = 10
|
||||||
|
retry_delay = 5 # seconds
|
||||||
|
retry_count = 0
|
||||||
|
block_timeout = 5000 # milliseconds
|
||||||
|
|
||||||
|
while self._running and retry_count < max_retries:
|
||||||
|
try:
|
||||||
|
redis = await get_redis_async()
|
||||||
|
|
||||||
|
# Reset retry count on successful connection
|
||||||
|
retry_count = 0
|
||||||
|
|
||||||
|
while self._running:
|
||||||
|
# First, claim any stale pending messages from dead consumers
|
||||||
|
# Redis does NOT auto-redeliver pending messages; we must explicitly
|
||||||
|
# claim them using XAUTOCLAIM
|
||||||
|
try:
|
||||||
|
claimed_result = await redis.xautoclaim(
|
||||||
|
name=config.stream_completion_name,
|
||||||
|
groupname=config.stream_consumer_group,
|
||||||
|
consumername=self._consumer_name,
|
||||||
|
min_idle_time=config.stream_claim_min_idle_ms,
|
||||||
|
start_id="0-0",
|
||||||
|
count=10,
|
||||||
|
)
|
||||||
|
# xautoclaim returns: (next_start_id, [(id, data), ...], [deleted_ids])
|
||||||
|
if claimed_result and len(claimed_result) >= 2:
|
||||||
|
claimed_entries = claimed_result[1]
|
||||||
|
if claimed_entries:
|
||||||
|
logger.info(
|
||||||
|
f"Claimed {len(claimed_entries)} stale pending messages"
|
||||||
|
)
|
||||||
|
for entry_id, data in claimed_entries:
|
||||||
|
if not self._running:
|
||||||
|
return
|
||||||
|
await self._process_entry(redis, entry_id, data)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"XAUTOCLAIM failed (non-fatal): {e}")
|
||||||
|
|
||||||
|
# Read new messages from the stream
|
||||||
|
messages = await redis.xreadgroup(
|
||||||
|
groupname=config.stream_consumer_group,
|
||||||
|
consumername=self._consumer_name,
|
||||||
|
streams={config.stream_completion_name: ">"},
|
||||||
|
block=block_timeout,
|
||||||
|
count=10,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not messages:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for stream_name, entries in messages:
|
||||||
|
for entry_id, data in entries:
|
||||||
|
if not self._running:
|
||||||
|
return
|
||||||
|
await self._process_entry(redis, entry_id, data)
|
||||||
|
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.info("Consumer cancelled")
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
retry_count += 1
|
||||||
|
logger.error(
|
||||||
|
f"Consumer error (retry {retry_count}/{max_retries}): {e}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
if self._running and retry_count < max_retries:
|
||||||
|
await asyncio.sleep(retry_delay)
|
||||||
|
else:
|
||||||
|
logger.error("Max retries reached, stopping consumer")
|
||||||
|
return
|
||||||
|
|
||||||
|
async def _process_entry(
|
||||||
|
self, redis: Any, entry_id: str, data: dict[str, Any]
|
||||||
|
) -> None:
|
||||||
|
"""Process a single stream entry and acknowledge it on success.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
redis: Redis client connection
|
||||||
|
entry_id: The stream entry ID
|
||||||
|
data: The entry data dict
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Handle the message
|
||||||
|
message_data = data.get("data")
|
||||||
|
if message_data:
|
||||||
|
await self._handle_message(
|
||||||
|
message_data.encode()
|
||||||
|
if isinstance(message_data, str)
|
||||||
|
else message_data
|
||||||
|
)
|
||||||
|
|
||||||
|
# Acknowledge the message after successful processing
|
||||||
|
await redis.xack(
|
||||||
|
config.stream_completion_name,
|
||||||
|
config.stream_consumer_group,
|
||||||
|
entry_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(
|
||||||
|
f"Error processing completion message {entry_id}: {e}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
# Message remains in pending state and will be claimed by
|
||||||
|
# XAUTOCLAIM after min_idle_time expires
|
||||||
|
|
||||||
|
async def _handle_message(self, body: bytes) -> None:
|
||||||
|
"""Handle a completion message using our own Prisma client."""
|
||||||
|
try:
|
||||||
|
data = orjson.loads(body)
|
||||||
|
message = OperationCompleteMessage(**data)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to parse completion message: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"[COMPLETION] Received completion for operation {message.operation_id} "
|
||||||
|
f"(task_id={message.task_id}, success={message.success})"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Find task in registry
|
||||||
|
task = await stream_registry.find_task_by_operation_id(message.operation_id)
|
||||||
|
if task is None:
|
||||||
|
task = await stream_registry.get_task(message.task_id)
|
||||||
|
|
||||||
|
if task is None:
|
||||||
|
logger.warning(
|
||||||
|
f"[COMPLETION] Task not found for operation {message.operation_id} "
|
||||||
|
f"(task_id={message.task_id})"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"[COMPLETION] Found task: task_id={task.task_id}, "
|
||||||
|
f"session_id={task.session_id}, tool_call_id={task.tool_call_id}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Guard against empty task fields
|
||||||
|
if not task.task_id or not task.session_id or not task.tool_call_id:
|
||||||
|
logger.error(
|
||||||
|
f"[COMPLETION] Task has empty critical fields! "
|
||||||
|
f"task_id={task.task_id!r}, session_id={task.session_id!r}, "
|
||||||
|
f"tool_call_id={task.tool_call_id!r}"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if message.success:
|
||||||
|
await self._handle_success(task, message)
|
||||||
|
else:
|
||||||
|
await self._handle_failure(task, message)
|
||||||
|
|
||||||
|
async def _handle_success(
|
||||||
|
self,
|
||||||
|
task: stream_registry.ActiveTask,
|
||||||
|
message: OperationCompleteMessage,
|
||||||
|
) -> None:
|
||||||
|
"""Handle successful operation completion."""
|
||||||
|
prisma = await self._ensure_prisma()
|
||||||
|
await process_operation_success(task, message.result, prisma)
|
||||||
|
|
||||||
|
async def _handle_failure(
|
||||||
|
self,
|
||||||
|
task: stream_registry.ActiveTask,
|
||||||
|
message: OperationCompleteMessage,
|
||||||
|
) -> None:
|
||||||
|
"""Handle failed operation completion."""
|
||||||
|
prisma = await self._ensure_prisma()
|
||||||
|
await process_operation_failure(task, message.error, prisma)
|
||||||
|
|
||||||
|
|
||||||
|
# Module-level consumer instance
|
||||||
|
_consumer: ChatCompletionConsumer | None = None
|
||||||
|
|
||||||
|
|
||||||
|
async def start_completion_consumer() -> None:
|
||||||
|
"""Start the global completion consumer."""
|
||||||
|
global _consumer
|
||||||
|
if _consumer is None:
|
||||||
|
_consumer = ChatCompletionConsumer()
|
||||||
|
await _consumer.start()
|
||||||
|
|
||||||
|
|
||||||
|
async def stop_completion_consumer() -> None:
|
||||||
|
"""Stop the global completion consumer."""
|
||||||
|
global _consumer
|
||||||
|
if _consumer:
|
||||||
|
await _consumer.stop()
|
||||||
|
_consumer = None
|
||||||
|
|
||||||
|
|
||||||
|
async def publish_operation_complete(
|
||||||
|
operation_id: str,
|
||||||
|
task_id: str,
|
||||||
|
success: bool,
|
||||||
|
result: dict | str | None = None,
|
||||||
|
error: str | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Publish an operation completion message to Redis Streams.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
operation_id: The operation ID that completed.
|
||||||
|
task_id: The task ID associated with the operation.
|
||||||
|
success: Whether the operation succeeded.
|
||||||
|
result: The result data (for success).
|
||||||
|
error: The error message (for failure).
|
||||||
|
"""
|
||||||
|
message = OperationCompleteMessage(
|
||||||
|
operation_id=operation_id,
|
||||||
|
task_id=task_id,
|
||||||
|
success=success,
|
||||||
|
result=result,
|
||||||
|
error=error,
|
||||||
|
)
|
||||||
|
|
||||||
|
redis = await get_redis_async()
|
||||||
|
await redis.xadd(
|
||||||
|
config.stream_completion_name,
|
||||||
|
{"data": message.model_dump_json()},
|
||||||
|
maxlen=config.stream_max_length,
|
||||||
|
)
|
||||||
|
logger.info(f"Published completion for operation {operation_id}")
|
||||||
@@ -0,0 +1,344 @@
|
|||||||
|
"""Shared completion handling for operation success and failure.
|
||||||
|
|
||||||
|
This module provides common logic for handling operation completion from both:
|
||||||
|
- The Redis Streams consumer (completion_consumer.py)
|
||||||
|
- The HTTP webhook endpoint (routes.py)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import orjson
|
||||||
|
from prisma import Prisma
|
||||||
|
|
||||||
|
from . import service as chat_service
|
||||||
|
from . import stream_registry
|
||||||
|
from .response_model import StreamError, StreamToolOutputAvailable
|
||||||
|
from .tools.models import ErrorResponse
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Tools that produce agent_json that needs to be saved to library
|
||||||
|
AGENT_GENERATION_TOOLS = {"create_agent", "edit_agent"}
|
||||||
|
|
||||||
|
# Keys that should be stripped from agent_json when returning in error responses
|
||||||
|
SENSITIVE_KEYS = frozenset(
|
||||||
|
{
|
||||||
|
"api_key",
|
||||||
|
"apikey",
|
||||||
|
"api_secret",
|
||||||
|
"password",
|
||||||
|
"secret",
|
||||||
|
"credentials",
|
||||||
|
"credential",
|
||||||
|
"token",
|
||||||
|
"access_token",
|
||||||
|
"refresh_token",
|
||||||
|
"private_key",
|
||||||
|
"privatekey",
|
||||||
|
"auth",
|
||||||
|
"authorization",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _sanitize_agent_json(obj: Any) -> Any:
|
||||||
|
"""Recursively sanitize agent_json by removing sensitive keys.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj: The object to sanitize (dict, list, or primitive)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Sanitized copy with sensitive keys removed/redacted
|
||||||
|
"""
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
return {
|
||||||
|
k: "[REDACTED]" if k.lower() in SENSITIVE_KEYS else _sanitize_agent_json(v)
|
||||||
|
for k, v in obj.items()
|
||||||
|
}
|
||||||
|
elif isinstance(obj, list):
|
||||||
|
return [_sanitize_agent_json(item) for item in obj]
|
||||||
|
else:
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
class ToolMessageUpdateError(Exception):
|
||||||
|
"""Raised when updating a tool message in the database fails."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
async def _update_tool_message(
|
||||||
|
session_id: str,
|
||||||
|
tool_call_id: str,
|
||||||
|
content: str,
|
||||||
|
prisma_client: Prisma | None,
|
||||||
|
) -> None:
|
||||||
|
"""Update tool message in database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
session_id: The session ID
|
||||||
|
tool_call_id: The tool call ID to update
|
||||||
|
content: The new content for the message
|
||||||
|
prisma_client: Optional Prisma client. If None, uses chat_service.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ToolMessageUpdateError: If the database update fails. The caller should
|
||||||
|
handle this to avoid marking the task as completed with inconsistent state.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if prisma_client:
|
||||||
|
# Use provided Prisma client (for consumer with its own connection)
|
||||||
|
updated_count = await prisma_client.chatmessage.update_many(
|
||||||
|
where={
|
||||||
|
"sessionId": session_id,
|
||||||
|
"toolCallId": tool_call_id,
|
||||||
|
},
|
||||||
|
data={"content": content},
|
||||||
|
)
|
||||||
|
# Check if any rows were updated - 0 means message not found
|
||||||
|
if updated_count == 0:
|
||||||
|
raise ToolMessageUpdateError(
|
||||||
|
f"No message found with tool_call_id={tool_call_id} in session {session_id}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Use service function (for webhook endpoint)
|
||||||
|
await chat_service._update_pending_operation(
|
||||||
|
session_id=session_id,
|
||||||
|
tool_call_id=tool_call_id,
|
||||||
|
result=content,
|
||||||
|
)
|
||||||
|
except ToolMessageUpdateError:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[COMPLETION] Failed to update tool message: {e}", exc_info=True)
|
||||||
|
raise ToolMessageUpdateError(
|
||||||
|
f"Failed to update tool message for tool_call_id={tool_call_id}: {e}"
|
||||||
|
) from e
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_result(result: dict | list | str | int | float | bool | None) -> str:
|
||||||
|
"""Serialize result to JSON string with sensible defaults.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
result: The result to serialize. Can be a dict, list, string,
|
||||||
|
number, boolean, or None.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string representation of the result. Returns '{"status": "completed"}'
|
||||||
|
only when result is explicitly None.
|
||||||
|
"""
|
||||||
|
if isinstance(result, str):
|
||||||
|
return result
|
||||||
|
if result is None:
|
||||||
|
return '{"status": "completed"}'
|
||||||
|
return orjson.dumps(result).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
async def _save_agent_from_result(
|
||||||
|
result: dict[str, Any],
|
||||||
|
user_id: str | None,
|
||||||
|
tool_name: str,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Save agent to library if result contains agent_json.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
result: The result dict that may contain agent_json
|
||||||
|
user_id: The user ID to save the agent for
|
||||||
|
tool_name: The tool name (create_agent or edit_agent)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Updated result dict with saved agent details, or original result if no agent_json
|
||||||
|
"""
|
||||||
|
if not user_id:
|
||||||
|
logger.warning("[COMPLETION] Cannot save agent: no user_id in task")
|
||||||
|
return result
|
||||||
|
|
||||||
|
agent_json = result.get("agent_json")
|
||||||
|
if not agent_json:
|
||||||
|
logger.warning(
|
||||||
|
f"[COMPLETION] {tool_name} completed but no agent_json in result"
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
try:
|
||||||
|
from .tools.agent_generator import save_agent_to_library
|
||||||
|
|
||||||
|
is_update = tool_name == "edit_agent"
|
||||||
|
created_graph, library_agent = await save_agent_to_library(
|
||||||
|
agent_json, user_id, is_update=is_update
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"[COMPLETION] Saved agent '{created_graph.name}' to library "
|
||||||
|
f"(graph_id={created_graph.id}, library_agent_id={library_agent.id})"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Return a response similar to AgentSavedResponse
|
||||||
|
return {
|
||||||
|
"type": "agent_saved",
|
||||||
|
"message": f"Agent '{created_graph.name}' has been saved to your library!",
|
||||||
|
"agent_id": created_graph.id,
|
||||||
|
"agent_name": created_graph.name,
|
||||||
|
"library_agent_id": library_agent.id,
|
||||||
|
"library_agent_link": f"/library/agents/{library_agent.id}",
|
||||||
|
"agent_page_link": f"/build?flowID={created_graph.id}",
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(
|
||||||
|
f"[COMPLETION] Failed to save agent to library: {e}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
# Return error but don't fail the whole operation
|
||||||
|
# Sanitize agent_json to remove sensitive keys before returning
|
||||||
|
return {
|
||||||
|
"type": "error",
|
||||||
|
"message": f"Agent was generated but failed to save: {str(e)}",
|
||||||
|
"error": str(e),
|
||||||
|
"agent_json": _sanitize_agent_json(agent_json),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def process_operation_success(
|
||||||
|
task: stream_registry.ActiveTask,
|
||||||
|
result: dict | str | None,
|
||||||
|
prisma_client: Prisma | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Handle successful operation completion.
|
||||||
|
|
||||||
|
Publishes the result to the stream registry, updates the database,
|
||||||
|
generates LLM continuation, and marks the task as completed.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task: The active task that completed
|
||||||
|
result: The result data from the operation
|
||||||
|
prisma_client: Optional Prisma client for database operations.
|
||||||
|
If None, uses chat_service._update_pending_operation instead.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ToolMessageUpdateError: If the database update fails. The task will be
|
||||||
|
marked as failed instead of completed to avoid inconsistent state.
|
||||||
|
"""
|
||||||
|
# For agent generation tools, save the agent to library
|
||||||
|
if task.tool_name in AGENT_GENERATION_TOOLS and isinstance(result, dict):
|
||||||
|
result = await _save_agent_from_result(result, task.user_id, task.tool_name)
|
||||||
|
|
||||||
|
# Serialize result for output (only substitute default when result is exactly None)
|
||||||
|
result_output = result if result is not None else {"status": "completed"}
|
||||||
|
output_str = (
|
||||||
|
result_output
|
||||||
|
if isinstance(result_output, str)
|
||||||
|
else orjson.dumps(result_output).decode("utf-8")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Publish result to stream registry
|
||||||
|
await stream_registry.publish_chunk(
|
||||||
|
task.task_id,
|
||||||
|
StreamToolOutputAvailable(
|
||||||
|
toolCallId=task.tool_call_id,
|
||||||
|
toolName=task.tool_name,
|
||||||
|
output=output_str,
|
||||||
|
success=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Update pending operation in database
|
||||||
|
# If this fails, we must not continue to mark the task as completed
|
||||||
|
result_str = serialize_result(result)
|
||||||
|
try:
|
||||||
|
await _update_tool_message(
|
||||||
|
session_id=task.session_id,
|
||||||
|
tool_call_id=task.tool_call_id,
|
||||||
|
content=result_str,
|
||||||
|
prisma_client=prisma_client,
|
||||||
|
)
|
||||||
|
except ToolMessageUpdateError:
|
||||||
|
# DB update failed - mark task as failed to avoid inconsistent state
|
||||||
|
logger.error(
|
||||||
|
f"[COMPLETION] DB update failed for task {task.task_id}, "
|
||||||
|
"marking as failed instead of completed"
|
||||||
|
)
|
||||||
|
await stream_registry.publish_chunk(
|
||||||
|
task.task_id,
|
||||||
|
StreamError(errorText="Failed to save operation result to database"),
|
||||||
|
)
|
||||||
|
await stream_registry.mark_task_completed(task.task_id, status="failed")
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Generate LLM continuation with streaming
|
||||||
|
try:
|
||||||
|
await chat_service._generate_llm_continuation_with_streaming(
|
||||||
|
session_id=task.session_id,
|
||||||
|
user_id=task.user_id,
|
||||||
|
task_id=task.task_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(
|
||||||
|
f"[COMPLETION] Failed to generate LLM continuation: {e}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mark task as completed and release Redis lock
|
||||||
|
await stream_registry.mark_task_completed(task.task_id, status="completed")
|
||||||
|
try:
|
||||||
|
await chat_service._mark_operation_completed(task.tool_call_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[COMPLETION] Failed to mark operation completed: {e}")
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"[COMPLETION] Successfully processed completion for task {task.task_id}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def process_operation_failure(
|
||||||
|
task: stream_registry.ActiveTask,
|
||||||
|
error: str | None,
|
||||||
|
prisma_client: Prisma | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Handle failed operation completion.
|
||||||
|
|
||||||
|
Publishes the error to the stream registry, updates the database with
|
||||||
|
the error response, and marks the task as failed.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task: The active task that failed
|
||||||
|
error: The error message from the operation
|
||||||
|
prisma_client: Optional Prisma client for database operations.
|
||||||
|
If None, uses chat_service._update_pending_operation instead.
|
||||||
|
"""
|
||||||
|
error_msg = error or "Operation failed"
|
||||||
|
|
||||||
|
# Publish error to stream registry
|
||||||
|
await stream_registry.publish_chunk(
|
||||||
|
task.task_id,
|
||||||
|
StreamError(errorText=error_msg),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Update pending operation with error
|
||||||
|
# If this fails, we still continue to mark the task as failed
|
||||||
|
error_response = ErrorResponse(
|
||||||
|
message=error_msg,
|
||||||
|
error=error,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
await _update_tool_message(
|
||||||
|
session_id=task.session_id,
|
||||||
|
tool_call_id=task.tool_call_id,
|
||||||
|
content=error_response.model_dump_json(),
|
||||||
|
prisma_client=prisma_client,
|
||||||
|
)
|
||||||
|
except ToolMessageUpdateError:
|
||||||
|
# DB update failed - log but continue with cleanup
|
||||||
|
logger.error(
|
||||||
|
f"[COMPLETION] DB update failed while processing failure for task {task.task_id}, "
|
||||||
|
"continuing with cleanup"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mark task as failed and release Redis lock
|
||||||
|
await stream_registry.mark_task_completed(task.task_id, status="failed")
|
||||||
|
try:
|
||||||
|
await chat_service._mark_operation_completed(task.tool_call_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[COMPLETION] Failed to mark operation completed: {e}")
|
||||||
|
|
||||||
|
logger.info(f"[COMPLETION] Processed failure for task {task.task_id}: {error_msg}")
|
||||||
158
autogpt_platform/backend/backend/api/features/chat/config.py
Normal file
158
autogpt_platform/backend/backend/api/features/chat/config.py
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
"""Configuration management for chat system."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from pydantic import Field, field_validator
|
||||||
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
|
class ChatConfig(BaseSettings):
|
||||||
|
"""Configuration for the chat system."""
|
||||||
|
|
||||||
|
# OpenAI API Configuration
|
||||||
|
model: str = Field(
|
||||||
|
default="anthropic/claude-opus-4.6", description="Default model to use"
|
||||||
|
)
|
||||||
|
title_model: str = Field(
|
||||||
|
default="openai/gpt-4o-mini",
|
||||||
|
description="Model to use for generating session titles (should be fast/cheap)",
|
||||||
|
)
|
||||||
|
api_key: str | None = Field(default=None, description="OpenAI API key")
|
||||||
|
base_url: str | None = Field(
|
||||||
|
default="https://openrouter.ai/api/v1",
|
||||||
|
description="Base URL for API (e.g., for OpenRouter)",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Session TTL Configuration - 12 hours
|
||||||
|
session_ttl: int = Field(default=43200, description="Session TTL in seconds")
|
||||||
|
|
||||||
|
# Streaming Configuration
|
||||||
|
max_context_messages: int = Field(
|
||||||
|
default=50, ge=1, le=200, description="Maximum context messages"
|
||||||
|
)
|
||||||
|
|
||||||
|
stream_timeout: int = Field(default=300, description="Stream timeout in seconds")
|
||||||
|
max_retries: int = Field(default=3, description="Maximum number of retries")
|
||||||
|
max_agent_runs: int = Field(default=30, description="Maximum number of agent runs")
|
||||||
|
max_agent_schedules: int = Field(
|
||||||
|
default=30, description="Maximum number of agent schedules"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Long-running operation configuration
|
||||||
|
long_running_operation_ttl: int = Field(
|
||||||
|
default=600,
|
||||||
|
description="TTL in seconds for long-running operation tracking in Redis (safety net if pod dies)",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Stream registry configuration for SSE reconnection
|
||||||
|
stream_ttl: int = Field(
|
||||||
|
default=3600,
|
||||||
|
description="TTL in seconds for stream data in Redis (1 hour)",
|
||||||
|
)
|
||||||
|
stream_max_length: int = Field(
|
||||||
|
default=10000,
|
||||||
|
description="Maximum number of messages to store per stream",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Redis Streams configuration for completion consumer
|
||||||
|
stream_completion_name: str = Field(
|
||||||
|
default="chat:completions",
|
||||||
|
description="Redis Stream name for operation completions",
|
||||||
|
)
|
||||||
|
stream_consumer_group: str = Field(
|
||||||
|
default="chat_consumers",
|
||||||
|
description="Consumer group name for completion stream",
|
||||||
|
)
|
||||||
|
stream_claim_min_idle_ms: int = Field(
|
||||||
|
default=60000,
|
||||||
|
description="Minimum idle time in milliseconds before claiming pending messages from dead consumers",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Redis key prefixes for stream registry
|
||||||
|
task_meta_prefix: str = Field(
|
||||||
|
default="chat:task:meta:",
|
||||||
|
description="Prefix for task metadata hash keys",
|
||||||
|
)
|
||||||
|
task_stream_prefix: str = Field(
|
||||||
|
default="chat:stream:",
|
||||||
|
description="Prefix for task message stream keys",
|
||||||
|
)
|
||||||
|
task_op_prefix: str = Field(
|
||||||
|
default="chat:task:op:",
|
||||||
|
description="Prefix for operation ID to task ID mapping keys",
|
||||||
|
)
|
||||||
|
internal_api_key: str | None = Field(
|
||||||
|
default=None,
|
||||||
|
description="API key for internal webhook callbacks (env: CHAT_INTERNAL_API_KEY)",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Langfuse Prompt Management Configuration
|
||||||
|
# Note: Langfuse credentials are in Settings().secrets (settings.py)
|
||||||
|
langfuse_prompt_name: str = Field(
|
||||||
|
default="CoPilot Prompt",
|
||||||
|
description="Name of the prompt in Langfuse to fetch",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Extended thinking configuration for Claude models
|
||||||
|
thinking_enabled: bool = Field(
|
||||||
|
default=True,
|
||||||
|
description="Enable extended thinking for Claude models via OpenRouter",
|
||||||
|
)
|
||||||
|
thinking_budget_tokens: int = Field(
|
||||||
|
default=10000,
|
||||||
|
ge=1000,
|
||||||
|
le=100000,
|
||||||
|
description="Maximum tokens for extended thinking (budget_tokens for Claude)",
|
||||||
|
)
|
||||||
|
|
||||||
|
@field_validator("api_key", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def get_api_key(cls, v):
|
||||||
|
"""Get API key from environment if not provided."""
|
||||||
|
if v is None:
|
||||||
|
# Try to get from environment variables
|
||||||
|
# First check for CHAT_API_KEY (Pydantic prefix)
|
||||||
|
v = os.getenv("CHAT_API_KEY")
|
||||||
|
if not v:
|
||||||
|
# Fall back to OPEN_ROUTER_API_KEY
|
||||||
|
v = os.getenv("OPEN_ROUTER_API_KEY")
|
||||||
|
if not v:
|
||||||
|
# Fall back to OPENAI_API_KEY
|
||||||
|
v = os.getenv("OPENAI_API_KEY")
|
||||||
|
return v
|
||||||
|
|
||||||
|
@field_validator("base_url", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def get_base_url(cls, v):
|
||||||
|
"""Get base URL from environment if not provided."""
|
||||||
|
if v is None:
|
||||||
|
# Check for OpenRouter or custom base URL
|
||||||
|
v = os.getenv("CHAT_BASE_URL")
|
||||||
|
if not v:
|
||||||
|
v = os.getenv("OPENROUTER_BASE_URL")
|
||||||
|
if not v:
|
||||||
|
v = os.getenv("OPENAI_BASE_URL")
|
||||||
|
if not v:
|
||||||
|
v = "https://openrouter.ai/api/v1"
|
||||||
|
return v
|
||||||
|
|
||||||
|
@field_validator("internal_api_key", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def get_internal_api_key(cls, v):
|
||||||
|
"""Get internal API key from environment if not provided."""
|
||||||
|
if v is None:
|
||||||
|
v = os.getenv("CHAT_INTERNAL_API_KEY")
|
||||||
|
return v
|
||||||
|
|
||||||
|
# Prompt paths for different contexts
|
||||||
|
PROMPT_PATHS: dict[str, str] = {
|
||||||
|
"default": "prompts/chat_system.md",
|
||||||
|
"onboarding": "prompts/onboarding_system.md",
|
||||||
|
}
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
"""Pydantic config."""
|
||||||
|
|
||||||
|
env_file = ".env"
|
||||||
|
env_file_encoding = "utf-8"
|
||||||
|
extra = "ignore" # Ignore extra environment variables
|
||||||
288
autogpt_platform/backend/backend/api/features/chat/db.py
Normal file
288
autogpt_platform/backend/backend/api/features/chat/db.py
Normal file
@@ -0,0 +1,288 @@
|
|||||||
|
"""Database operations for chat sessions."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from typing import Any, cast
|
||||||
|
|
||||||
|
from prisma.models import ChatMessage as PrismaChatMessage
|
||||||
|
from prisma.models import ChatSession as PrismaChatSession
|
||||||
|
from prisma.types import (
|
||||||
|
ChatMessageCreateInput,
|
||||||
|
ChatSessionCreateInput,
|
||||||
|
ChatSessionUpdateInput,
|
||||||
|
ChatSessionWhereInput,
|
||||||
|
)
|
||||||
|
|
||||||
|
from backend.data.db import transaction
|
||||||
|
from backend.util.json import SafeJson
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_chat_session(session_id: str) -> PrismaChatSession | None:
|
||||||
|
"""Get a chat session by ID from the database."""
|
||||||
|
session = await PrismaChatSession.prisma().find_unique(
|
||||||
|
where={"id": session_id},
|
||||||
|
include={"Messages": True},
|
||||||
|
)
|
||||||
|
if session and session.Messages:
|
||||||
|
# Sort messages by sequence in Python - Prisma Python client doesn't support
|
||||||
|
# order_by in include clauses (unlike Prisma JS), so we sort after fetching
|
||||||
|
session.Messages.sort(key=lambda m: m.sequence)
|
||||||
|
return session
|
||||||
|
|
||||||
|
|
||||||
|
async def create_chat_session(
|
||||||
|
session_id: str,
|
||||||
|
user_id: str,
|
||||||
|
) -> PrismaChatSession:
|
||||||
|
"""Create a new chat session in the database."""
|
||||||
|
data = ChatSessionCreateInput(
|
||||||
|
id=session_id,
|
||||||
|
userId=user_id,
|
||||||
|
credentials=SafeJson({}),
|
||||||
|
successfulAgentRuns=SafeJson({}),
|
||||||
|
successfulAgentSchedules=SafeJson({}),
|
||||||
|
)
|
||||||
|
return await PrismaChatSession.prisma().create(data=data)
|
||||||
|
|
||||||
|
|
||||||
|
async def update_chat_session(
|
||||||
|
session_id: str,
|
||||||
|
credentials: dict[str, Any] | None = None,
|
||||||
|
successful_agent_runs: dict[str, Any] | None = None,
|
||||||
|
successful_agent_schedules: dict[str, Any] | None = None,
|
||||||
|
total_prompt_tokens: int | None = None,
|
||||||
|
total_completion_tokens: int | None = None,
|
||||||
|
title: str | None = None,
|
||||||
|
) -> PrismaChatSession | None:
|
||||||
|
"""Update a chat session's metadata."""
|
||||||
|
data: ChatSessionUpdateInput = {"updatedAt": datetime.now(UTC)}
|
||||||
|
|
||||||
|
if credentials is not None:
|
||||||
|
data["credentials"] = SafeJson(credentials)
|
||||||
|
if successful_agent_runs is not None:
|
||||||
|
data["successfulAgentRuns"] = SafeJson(successful_agent_runs)
|
||||||
|
if successful_agent_schedules is not None:
|
||||||
|
data["successfulAgentSchedules"] = SafeJson(successful_agent_schedules)
|
||||||
|
if total_prompt_tokens is not None:
|
||||||
|
data["totalPromptTokens"] = total_prompt_tokens
|
||||||
|
if total_completion_tokens is not None:
|
||||||
|
data["totalCompletionTokens"] = total_completion_tokens
|
||||||
|
if title is not None:
|
||||||
|
data["title"] = title
|
||||||
|
|
||||||
|
session = await PrismaChatSession.prisma().update(
|
||||||
|
where={"id": session_id},
|
||||||
|
data=data,
|
||||||
|
include={"Messages": True},
|
||||||
|
)
|
||||||
|
if session and session.Messages:
|
||||||
|
# Sort in Python - Prisma Python doesn't support order_by in include clauses
|
||||||
|
session.Messages.sort(key=lambda m: m.sequence)
|
||||||
|
return session
|
||||||
|
|
||||||
|
|
||||||
|
async def add_chat_message(
|
||||||
|
session_id: str,
|
||||||
|
role: str,
|
||||||
|
sequence: int,
|
||||||
|
content: str | None = None,
|
||||||
|
name: str | None = None,
|
||||||
|
tool_call_id: str | None = None,
|
||||||
|
refusal: str | None = None,
|
||||||
|
tool_calls: list[dict[str, Any]] | None = None,
|
||||||
|
function_call: dict[str, Any] | None = None,
|
||||||
|
) -> PrismaChatMessage:
|
||||||
|
"""Add a message to a chat session."""
|
||||||
|
# Build input dict dynamically rather than using ChatMessageCreateInput directly
|
||||||
|
# because Prisma's TypedDict validation rejects optional fields set to None.
|
||||||
|
# We only include fields that have values, then cast at the end.
|
||||||
|
data: dict[str, Any] = {
|
||||||
|
"Session": {"connect": {"id": session_id}},
|
||||||
|
"role": role,
|
||||||
|
"sequence": sequence,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add optional string fields
|
||||||
|
if content is not None:
|
||||||
|
data["content"] = content
|
||||||
|
if name is not None:
|
||||||
|
data["name"] = name
|
||||||
|
if tool_call_id is not None:
|
||||||
|
data["toolCallId"] = tool_call_id
|
||||||
|
if refusal is not None:
|
||||||
|
data["refusal"] = refusal
|
||||||
|
|
||||||
|
# Add optional JSON fields only when they have values
|
||||||
|
if tool_calls is not None:
|
||||||
|
data["toolCalls"] = SafeJson(tool_calls)
|
||||||
|
if function_call is not None:
|
||||||
|
data["functionCall"] = SafeJson(function_call)
|
||||||
|
|
||||||
|
# Run message create and session timestamp update in parallel for lower latency
|
||||||
|
_, message = await asyncio.gather(
|
||||||
|
PrismaChatSession.prisma().update(
|
||||||
|
where={"id": session_id},
|
||||||
|
data={"updatedAt": datetime.now(UTC)},
|
||||||
|
),
|
||||||
|
PrismaChatMessage.prisma().create(data=cast(ChatMessageCreateInput, data)),
|
||||||
|
)
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
|
async def add_chat_messages_batch(
|
||||||
|
session_id: str,
|
||||||
|
messages: list[dict[str, Any]],
|
||||||
|
start_sequence: int,
|
||||||
|
) -> list[PrismaChatMessage]:
|
||||||
|
"""Add multiple messages to a chat session in a batch.
|
||||||
|
|
||||||
|
Uses a transaction for atomicity - if any message creation fails,
|
||||||
|
the entire batch is rolled back.
|
||||||
|
"""
|
||||||
|
if not messages:
|
||||||
|
return []
|
||||||
|
|
||||||
|
created_messages = []
|
||||||
|
|
||||||
|
async with transaction() as tx:
|
||||||
|
for i, msg in enumerate(messages):
|
||||||
|
# Build input dict dynamically rather than using ChatMessageCreateInput
|
||||||
|
# directly because Prisma's TypedDict validation rejects optional fields
|
||||||
|
# set to None. We only include fields that have values, then cast.
|
||||||
|
data: dict[str, Any] = {
|
||||||
|
"Session": {"connect": {"id": session_id}},
|
||||||
|
"role": msg["role"],
|
||||||
|
"sequence": start_sequence + i,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add optional string fields
|
||||||
|
if msg.get("content") is not None:
|
||||||
|
data["content"] = msg["content"]
|
||||||
|
if msg.get("name") is not None:
|
||||||
|
data["name"] = msg["name"]
|
||||||
|
if msg.get("tool_call_id") is not None:
|
||||||
|
data["toolCallId"] = msg["tool_call_id"]
|
||||||
|
if msg.get("refusal") is not None:
|
||||||
|
data["refusal"] = msg["refusal"]
|
||||||
|
|
||||||
|
# Add optional JSON fields only when they have values
|
||||||
|
if msg.get("tool_calls") is not None:
|
||||||
|
data["toolCalls"] = SafeJson(msg["tool_calls"])
|
||||||
|
if msg.get("function_call") is not None:
|
||||||
|
data["functionCall"] = SafeJson(msg["function_call"])
|
||||||
|
|
||||||
|
created = await PrismaChatMessage.prisma(tx).create(
|
||||||
|
data=cast(ChatMessageCreateInput, data)
|
||||||
|
)
|
||||||
|
created_messages.append(created)
|
||||||
|
|
||||||
|
# Update session's updatedAt timestamp within the same transaction.
|
||||||
|
# Note: Token usage (total_prompt_tokens, total_completion_tokens) is updated
|
||||||
|
# separately via update_chat_session() after streaming completes.
|
||||||
|
await PrismaChatSession.prisma(tx).update(
|
||||||
|
where={"id": session_id},
|
||||||
|
data={"updatedAt": datetime.now(UTC)},
|
||||||
|
)
|
||||||
|
|
||||||
|
return created_messages
|
||||||
|
|
||||||
|
|
||||||
|
async def get_user_chat_sessions(
|
||||||
|
user_id: str,
|
||||||
|
limit: int = 50,
|
||||||
|
offset: int = 0,
|
||||||
|
) -> list[PrismaChatSession]:
|
||||||
|
"""Get chat sessions for a user, ordered by most recent."""
|
||||||
|
return await PrismaChatSession.prisma().find_many(
|
||||||
|
where={"userId": user_id},
|
||||||
|
order={"updatedAt": "desc"},
|
||||||
|
take=limit,
|
||||||
|
skip=offset,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_user_session_count(user_id: str) -> int:
|
||||||
|
"""Get the total number of chat sessions for a user."""
|
||||||
|
return await PrismaChatSession.prisma().count(where={"userId": user_id})
|
||||||
|
|
||||||
|
|
||||||
|
async def delete_chat_session(session_id: str, user_id: str | None = None) -> bool:
|
||||||
|
"""Delete a chat session and all its messages.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
session_id: The session ID to delete.
|
||||||
|
user_id: If provided, validates that the session belongs to this user
|
||||||
|
before deletion. This prevents unauthorized deletion of other
|
||||||
|
users' sessions.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if deleted successfully, False otherwise.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Build typed where clause with optional user_id validation
|
||||||
|
where_clause: ChatSessionWhereInput = {"id": session_id}
|
||||||
|
if user_id is not None:
|
||||||
|
where_clause["userId"] = user_id
|
||||||
|
|
||||||
|
result = await PrismaChatSession.prisma().delete_many(where=where_clause)
|
||||||
|
if result == 0:
|
||||||
|
logger.warning(
|
||||||
|
f"No session deleted for {session_id} "
|
||||||
|
f"(user_id validation: {user_id is not None})"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to delete chat session {session_id}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def get_chat_session_message_count(session_id: str) -> int:
|
||||||
|
"""Get the number of messages in a chat session."""
|
||||||
|
count = await PrismaChatMessage.prisma().count(where={"sessionId": session_id})
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
async def update_tool_message_content(
|
||||||
|
session_id: str,
|
||||||
|
tool_call_id: str,
|
||||||
|
new_content: str,
|
||||||
|
) -> bool:
|
||||||
|
"""Update the content of a tool message in chat history.
|
||||||
|
|
||||||
|
Used by background tasks to update pending operation messages with final results.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
session_id: The chat session ID.
|
||||||
|
tool_call_id: The tool call ID to find the message.
|
||||||
|
new_content: The new content to set.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if a message was updated, False otherwise.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
result = await PrismaChatMessage.prisma().update_many(
|
||||||
|
where={
|
||||||
|
"sessionId": session_id,
|
||||||
|
"toolCallId": tool_call_id,
|
||||||
|
},
|
||||||
|
data={
|
||||||
|
"content": new_content,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if result == 0:
|
||||||
|
logger.warning(
|
||||||
|
f"No message found to update for session {session_id}, "
|
||||||
|
f"tool_call_id {tool_call_id}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(
|
||||||
|
f"Failed to update tool message for session {session_id}, "
|
||||||
|
f"tool_call_id {tool_call_id}: {e}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
@@ -2,7 +2,7 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from typing import Any, Self, cast
|
from typing import Any
|
||||||
from weakref import WeakValueDictionary
|
from weakref import WeakValueDictionary
|
||||||
|
|
||||||
from openai.types.chat import (
|
from openai.types.chat import (
|
||||||
@@ -23,17 +23,26 @@ from prisma.models import ChatMessage as PrismaChatMessage
|
|||||||
from prisma.models import ChatSession as PrismaChatSession
|
from prisma.models import ChatSession as PrismaChatSession
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from backend.data.db_accessors import chat_db
|
|
||||||
from backend.data.redis_client import get_redis_async
|
from backend.data.redis_client import get_redis_async
|
||||||
from backend.util import json
|
from backend.util import json
|
||||||
from backend.util.exceptions import DatabaseError, RedisError
|
from backend.util.exceptions import DatabaseError, RedisError
|
||||||
|
|
||||||
|
from . import db as chat_db
|
||||||
from .config import ChatConfig
|
from .config import ChatConfig
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
config = ChatConfig()
|
config = ChatConfig()
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_json_field(value: str | dict | list | None, default: Any = None) -> Any:
|
||||||
|
"""Parse a JSON field that may be stored as string or already parsed."""
|
||||||
|
if value is None:
|
||||||
|
return default
|
||||||
|
if isinstance(value, str):
|
||||||
|
return json.loads(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
# Redis cache key prefix for chat sessions
|
# Redis cache key prefix for chat sessions
|
||||||
CHAT_SESSION_CACHE_PREFIX = "chat:session:"
|
CHAT_SESSION_CACHE_PREFIX = "chat:session:"
|
||||||
|
|
||||||
@@ -43,7 +52,28 @@ def _get_session_cache_key(session_id: str) -> str:
|
|||||||
return f"{CHAT_SESSION_CACHE_PREFIX}{session_id}"
|
return f"{CHAT_SESSION_CACHE_PREFIX}{session_id}"
|
||||||
|
|
||||||
|
|
||||||
# ===================== Chat data models ===================== #
|
# Session-level locks to prevent race conditions during concurrent upserts.
|
||||||
|
# Uses WeakValueDictionary to automatically garbage collect locks when no longer referenced,
|
||||||
|
# preventing unbounded memory growth while maintaining lock semantics for active sessions.
|
||||||
|
# Invalidation: Locks are auto-removed by GC when no coroutine holds a reference (after
|
||||||
|
# async with lock: completes). Explicit cleanup also occurs in delete_chat_session().
|
||||||
|
_session_locks: WeakValueDictionary[str, asyncio.Lock] = WeakValueDictionary()
|
||||||
|
_session_locks_mutex = asyncio.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
async def _get_session_lock(session_id: str) -> asyncio.Lock:
|
||||||
|
"""Get or create a lock for a specific session to prevent concurrent upserts.
|
||||||
|
|
||||||
|
Uses WeakValueDictionary for automatic cleanup: locks are garbage collected
|
||||||
|
when no coroutine holds a reference to them, preventing memory leaks from
|
||||||
|
unbounded growth of session locks.
|
||||||
|
"""
|
||||||
|
async with _session_locks_mutex:
|
||||||
|
lock = _session_locks.get(session_id)
|
||||||
|
if lock is None:
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
_session_locks[session_id] = lock
|
||||||
|
return lock
|
||||||
|
|
||||||
|
|
||||||
class ChatMessage(BaseModel):
|
class ChatMessage(BaseModel):
|
||||||
@@ -55,19 +85,6 @@ class ChatMessage(BaseModel):
|
|||||||
tool_calls: list[dict] | None = None
|
tool_calls: list[dict] | None = None
|
||||||
function_call: dict | None = None
|
function_call: dict | None = None
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def from_db(prisma_message: PrismaChatMessage) -> "ChatMessage":
|
|
||||||
"""Convert a Prisma ChatMessage to a Pydantic ChatMessage."""
|
|
||||||
return ChatMessage(
|
|
||||||
role=prisma_message.role,
|
|
||||||
content=prisma_message.content,
|
|
||||||
name=prisma_message.name,
|
|
||||||
tool_call_id=prisma_message.toolCallId,
|
|
||||||
refusal=prisma_message.refusal,
|
|
||||||
tool_calls=_parse_json_field(prisma_message.toolCalls),
|
|
||||||
function_call=_parse_json_field(prisma_message.functionCall),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Usage(BaseModel):
|
class Usage(BaseModel):
|
||||||
prompt_tokens: int
|
prompt_tokens: int
|
||||||
@@ -75,10 +92,11 @@ class Usage(BaseModel):
|
|||||||
total_tokens: int
|
total_tokens: int
|
||||||
|
|
||||||
|
|
||||||
class ChatSessionInfo(BaseModel):
|
class ChatSession(BaseModel):
|
||||||
session_id: str
|
session_id: str
|
||||||
user_id: str
|
user_id: str
|
||||||
title: str | None = None
|
title: str | None = None
|
||||||
|
messages: list[ChatMessage]
|
||||||
usage: list[Usage]
|
usage: list[Usage]
|
||||||
credentials: dict[str, dict] = {} # Map of provider -> credential metadata
|
credentials: dict[str, dict] = {} # Map of provider -> credential metadata
|
||||||
started_at: datetime
|
started_at: datetime
|
||||||
@@ -86,9 +104,40 @@ class ChatSessionInfo(BaseModel):
|
|||||||
successful_agent_runs: dict[str, int] = {}
|
successful_agent_runs: dict[str, int] = {}
|
||||||
successful_agent_schedules: dict[str, int] = {}
|
successful_agent_schedules: dict[str, int] = {}
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def from_db(cls, prisma_session: PrismaChatSession) -> Self:
|
def new(user_id: str) -> "ChatSession":
|
||||||
"""Convert Prisma ChatSession to Pydantic ChatSession."""
|
return ChatSession(
|
||||||
|
session_id=str(uuid.uuid4()),
|
||||||
|
user_id=user_id,
|
||||||
|
title=None,
|
||||||
|
messages=[],
|
||||||
|
usage=[],
|
||||||
|
credentials={},
|
||||||
|
started_at=datetime.now(UTC),
|
||||||
|
updated_at=datetime.now(UTC),
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_db(
|
||||||
|
prisma_session: PrismaChatSession,
|
||||||
|
prisma_messages: list[PrismaChatMessage] | None = None,
|
||||||
|
) -> "ChatSession":
|
||||||
|
"""Convert Prisma models to Pydantic ChatSession."""
|
||||||
|
messages = []
|
||||||
|
if prisma_messages:
|
||||||
|
for msg in prisma_messages:
|
||||||
|
messages.append(
|
||||||
|
ChatMessage(
|
||||||
|
role=msg.role,
|
||||||
|
content=msg.content,
|
||||||
|
name=msg.name,
|
||||||
|
tool_call_id=msg.toolCallId,
|
||||||
|
refusal=msg.refusal,
|
||||||
|
tool_calls=_parse_json_field(msg.toolCalls),
|
||||||
|
function_call=_parse_json_field(msg.functionCall),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# Parse JSON fields from Prisma
|
# Parse JSON fields from Prisma
|
||||||
credentials = _parse_json_field(prisma_session.credentials, default={})
|
credentials = _parse_json_field(prisma_session.credentials, default={})
|
||||||
successful_agent_runs = _parse_json_field(
|
successful_agent_runs = _parse_json_field(
|
||||||
@@ -110,10 +159,11 @@ class ChatSessionInfo(BaseModel):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return cls(
|
return ChatSession(
|
||||||
session_id=prisma_session.id,
|
session_id=prisma_session.id,
|
||||||
user_id=prisma_session.userId,
|
user_id=prisma_session.userId,
|
||||||
title=prisma_session.title,
|
title=prisma_session.title,
|
||||||
|
messages=messages,
|
||||||
usage=usage,
|
usage=usage,
|
||||||
credentials=credentials,
|
credentials=credentials,
|
||||||
started_at=prisma_session.createdAt,
|
started_at=prisma_session.createdAt,
|
||||||
@@ -122,56 +172,6 @@ class ChatSessionInfo(BaseModel):
|
|||||||
successful_agent_schedules=successful_agent_schedules,
|
successful_agent_schedules=successful_agent_schedules,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ChatSession(ChatSessionInfo):
|
|
||||||
messages: list[ChatMessage]
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def new(cls, user_id: str) -> Self:
|
|
||||||
return cls(
|
|
||||||
session_id=str(uuid.uuid4()),
|
|
||||||
user_id=user_id,
|
|
||||||
title=None,
|
|
||||||
messages=[],
|
|
||||||
usage=[],
|
|
||||||
credentials={},
|
|
||||||
started_at=datetime.now(UTC),
|
|
||||||
updated_at=datetime.now(UTC),
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_db(cls, prisma_session: PrismaChatSession) -> Self:
|
|
||||||
"""Convert Prisma ChatSession to Pydantic ChatSession."""
|
|
||||||
if prisma_session.Messages is None:
|
|
||||||
raise ValueError(
|
|
||||||
f"Prisma session {prisma_session.id} is missing Messages relation"
|
|
||||||
)
|
|
||||||
|
|
||||||
return cls(
|
|
||||||
**ChatSessionInfo.from_db(prisma_session).model_dump(),
|
|
||||||
messages=[ChatMessage.from_db(m) for m in prisma_session.Messages],
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_tool_call_to_current_turn(self, tool_call: dict) -> None:
|
|
||||||
"""Attach a tool_call to the current turn's assistant message.
|
|
||||||
|
|
||||||
Searches backwards for the most recent assistant message (stopping at
|
|
||||||
any user message boundary). If found, appends the tool_call to it.
|
|
||||||
Otherwise creates a new assistant message with the tool_call.
|
|
||||||
"""
|
|
||||||
for msg in reversed(self.messages):
|
|
||||||
if msg.role == "user":
|
|
||||||
break
|
|
||||||
if msg.role == "assistant":
|
|
||||||
if not msg.tool_calls:
|
|
||||||
msg.tool_calls = []
|
|
||||||
msg.tool_calls.append(tool_call)
|
|
||||||
return
|
|
||||||
|
|
||||||
self.messages.append(
|
|
||||||
ChatMessage(role="assistant", content="", tool_calls=[tool_call])
|
|
||||||
)
|
|
||||||
|
|
||||||
def to_openai_messages(self) -> list[ChatCompletionMessageParam]:
|
def to_openai_messages(self) -> list[ChatCompletionMessageParam]:
|
||||||
messages = []
|
messages = []
|
||||||
for message in self.messages:
|
for message in self.messages:
|
||||||
@@ -258,72 +258,43 @@ class ChatSession(ChatSessionInfo):
|
|||||||
name=message.name or "",
|
name=message.name or "",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return self._merge_consecutive_assistant_messages(messages)
|
return messages
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _merge_consecutive_assistant_messages(
|
|
||||||
messages: list[ChatCompletionMessageParam],
|
|
||||||
) -> list[ChatCompletionMessageParam]:
|
|
||||||
"""Merge consecutive assistant messages into single messages.
|
|
||||||
|
|
||||||
Long-running tool flows can create split assistant messages: one with
|
|
||||||
text content and another with tool_calls. Anthropic's API requires
|
|
||||||
tool_result blocks to reference a tool_use in the immediately preceding
|
|
||||||
assistant message, so these splits cause 400 errors via OpenRouter.
|
|
||||||
"""
|
|
||||||
if len(messages) < 2:
|
|
||||||
return messages
|
|
||||||
|
|
||||||
result: list[ChatCompletionMessageParam] = [messages[0]]
|
|
||||||
for msg in messages[1:]:
|
|
||||||
prev = result[-1]
|
|
||||||
if prev.get("role") != "assistant" or msg.get("role") != "assistant":
|
|
||||||
result.append(msg)
|
|
||||||
continue
|
|
||||||
|
|
||||||
prev = cast(ChatCompletionAssistantMessageParam, prev)
|
|
||||||
curr = cast(ChatCompletionAssistantMessageParam, msg)
|
|
||||||
|
|
||||||
curr_content = curr.get("content") or ""
|
|
||||||
if curr_content:
|
|
||||||
prev_content = prev.get("content") or ""
|
|
||||||
prev["content"] = (
|
|
||||||
f"{prev_content}\n{curr_content}" if prev_content else curr_content
|
|
||||||
)
|
|
||||||
|
|
||||||
curr_tool_calls = curr.get("tool_calls")
|
|
||||||
if curr_tool_calls:
|
|
||||||
prev_tool_calls = prev.get("tool_calls")
|
|
||||||
prev["tool_calls"] = (
|
|
||||||
list(prev_tool_calls) + list(curr_tool_calls)
|
|
||||||
if prev_tool_calls
|
|
||||||
else list(curr_tool_calls)
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_json_field(value: str | dict | list | None, default: Any = None) -> Any:
|
async def _get_session_from_cache(session_id: str) -> ChatSession | None:
|
||||||
"""Parse a JSON field that may be stored as string or already parsed."""
|
"""Get a chat session from Redis cache."""
|
||||||
if value is None:
|
redis_key = _get_session_cache_key(session_id)
|
||||||
return default
|
async_redis = await get_redis_async()
|
||||||
if isinstance(value, str):
|
raw_session: bytes | None = await async_redis.get(redis_key)
|
||||||
return json.loads(value)
|
|
||||||
return value
|
if raw_session is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
session = ChatSession.model_validate_json(raw_session)
|
||||||
|
logger.info(
|
||||||
|
f"Loading session {session_id} from cache: "
|
||||||
|
f"message_count={len(session.messages)}, "
|
||||||
|
f"roles={[m.role for m in session.messages]}"
|
||||||
|
)
|
||||||
|
return session
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to deserialize session {session_id}: {e}", exc_info=True)
|
||||||
|
raise RedisError(f"Corrupted session data for {session_id}") from e
|
||||||
|
|
||||||
|
|
||||||
# ================ Chat cache + DB operations ================ #
|
async def _cache_session(session: ChatSession) -> None:
|
||||||
|
"""Cache a chat session in Redis."""
|
||||||
# NOTE: Database calls are automatically routed through DatabaseManager if Prisma is not
|
|
||||||
# connected directly.
|
|
||||||
|
|
||||||
|
|
||||||
async def cache_chat_session(session: ChatSession) -> None:
|
|
||||||
"""Cache a chat session in Redis (without persisting to the database)."""
|
|
||||||
redis_key = _get_session_cache_key(session.session_id)
|
redis_key = _get_session_cache_key(session.session_id)
|
||||||
async_redis = await get_redis_async()
|
async_redis = await get_redis_async()
|
||||||
await async_redis.setex(redis_key, config.session_ttl, session.model_dump_json())
|
await async_redis.setex(redis_key, config.session_ttl, session.model_dump_json())
|
||||||
|
|
||||||
|
|
||||||
|
async def cache_chat_session(session: ChatSession) -> None:
|
||||||
|
"""Cache a chat session without persisting to the database."""
|
||||||
|
await _cache_session(session)
|
||||||
|
|
||||||
|
|
||||||
async def invalidate_session_cache(session_id: str) -> None:
|
async def invalidate_session_cache(session_id: str) -> None:
|
||||||
"""Invalidate a chat session from Redis cache.
|
"""Invalidate a chat session from Redis cache.
|
||||||
|
|
||||||
@@ -339,6 +310,80 @@ async def invalidate_session_cache(session_id: str) -> None:
|
|||||||
logger.warning(f"Failed to invalidate session cache for {session_id}: {e}")
|
logger.warning(f"Failed to invalidate session cache for {session_id}: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
async def _get_session_from_db(session_id: str) -> ChatSession | None:
|
||||||
|
"""Get a chat session from the database."""
|
||||||
|
prisma_session = await chat_db.get_chat_session(session_id)
|
||||||
|
if not prisma_session:
|
||||||
|
return None
|
||||||
|
|
||||||
|
messages = prisma_session.Messages
|
||||||
|
logger.info(
|
||||||
|
f"Loading session {session_id} from DB: "
|
||||||
|
f"has_messages={messages is not None}, "
|
||||||
|
f"message_count={len(messages) if messages else 0}, "
|
||||||
|
f"roles={[m.role for m in messages] if messages else []}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return ChatSession.from_db(prisma_session, messages)
|
||||||
|
|
||||||
|
|
||||||
|
async def _save_session_to_db(
|
||||||
|
session: ChatSession, existing_message_count: int
|
||||||
|
) -> None:
|
||||||
|
"""Save or update a chat session in the database."""
|
||||||
|
# Check if session exists in DB
|
||||||
|
existing = await chat_db.get_chat_session(session.session_id)
|
||||||
|
|
||||||
|
if not existing:
|
||||||
|
# Create new session
|
||||||
|
await chat_db.create_chat_session(
|
||||||
|
session_id=session.session_id,
|
||||||
|
user_id=session.user_id,
|
||||||
|
)
|
||||||
|
existing_message_count = 0
|
||||||
|
|
||||||
|
# Calculate total tokens from usage
|
||||||
|
total_prompt = sum(u.prompt_tokens for u in session.usage)
|
||||||
|
total_completion = sum(u.completion_tokens for u in session.usage)
|
||||||
|
|
||||||
|
# Update session metadata
|
||||||
|
await chat_db.update_chat_session(
|
||||||
|
session_id=session.session_id,
|
||||||
|
credentials=session.credentials,
|
||||||
|
successful_agent_runs=session.successful_agent_runs,
|
||||||
|
successful_agent_schedules=session.successful_agent_schedules,
|
||||||
|
total_prompt_tokens=total_prompt,
|
||||||
|
total_completion_tokens=total_completion,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add new messages (only those after existing count)
|
||||||
|
new_messages = session.messages[existing_message_count:]
|
||||||
|
if new_messages:
|
||||||
|
messages_data = []
|
||||||
|
for msg in new_messages:
|
||||||
|
messages_data.append(
|
||||||
|
{
|
||||||
|
"role": msg.role,
|
||||||
|
"content": msg.content,
|
||||||
|
"name": msg.name,
|
||||||
|
"tool_call_id": msg.tool_call_id,
|
||||||
|
"refusal": msg.refusal,
|
||||||
|
"tool_calls": msg.tool_calls,
|
||||||
|
"function_call": msg.function_call,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
f"Saving {len(new_messages)} new messages to DB for session {session.session_id}: "
|
||||||
|
f"roles={[m['role'] for m in messages_data]}, "
|
||||||
|
f"start_sequence={existing_message_count}"
|
||||||
|
)
|
||||||
|
await chat_db.add_chat_messages_batch(
|
||||||
|
session_id=session.session_id,
|
||||||
|
messages=messages_data,
|
||||||
|
start_sequence=existing_message_count,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def get_chat_session(
|
async def get_chat_session(
|
||||||
session_id: str,
|
session_id: str,
|
||||||
user_id: str | None = None,
|
user_id: str | None = None,
|
||||||
@@ -370,7 +415,7 @@ async def get_chat_session(
|
|||||||
logger.warning(f"Unexpected cache error for session {session_id}: {e}")
|
logger.warning(f"Unexpected cache error for session {session_id}: {e}")
|
||||||
|
|
||||||
# Fall back to database
|
# Fall back to database
|
||||||
logger.debug(f"Session {session_id} not in cache, checking database")
|
logger.info(f"Session {session_id} not in cache, checking database")
|
||||||
session = await _get_session_from_db(session_id)
|
session = await _get_session_from_db(session_id)
|
||||||
|
|
||||||
if session is None:
|
if session is None:
|
||||||
@@ -386,7 +431,7 @@ async def get_chat_session(
|
|||||||
|
|
||||||
# Cache the session from DB
|
# Cache the session from DB
|
||||||
try:
|
try:
|
||||||
await cache_chat_session(session)
|
await _cache_session(session)
|
||||||
logger.info(f"Cached session {session_id} from database")
|
logger.info(f"Cached session {session_id} from database")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to cache session {session_id}: {e}")
|
logger.warning(f"Failed to cache session {session_id}: {e}")
|
||||||
@@ -394,44 +439,6 @@ async def get_chat_session(
|
|||||||
return session
|
return session
|
||||||
|
|
||||||
|
|
||||||
async def _get_session_from_cache(session_id: str) -> ChatSession | None:
|
|
||||||
"""Get a chat session from Redis cache."""
|
|
||||||
redis_key = _get_session_cache_key(session_id)
|
|
||||||
async_redis = await get_redis_async()
|
|
||||||
raw_session: bytes | None = await async_redis.get(redis_key)
|
|
||||||
|
|
||||||
if raw_session is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
session = ChatSession.model_validate_json(raw_session)
|
|
||||||
logger.info(
|
|
||||||
f"Loading session {session_id} from cache: "
|
|
||||||
f"message_count={len(session.messages)}, "
|
|
||||||
f"roles={[m.role for m in session.messages]}"
|
|
||||||
)
|
|
||||||
return session
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to deserialize session {session_id}: {e}", exc_info=True)
|
|
||||||
raise RedisError(f"Corrupted session data for {session_id}") from e
|
|
||||||
|
|
||||||
|
|
||||||
async def _get_session_from_db(session_id: str) -> ChatSession | None:
|
|
||||||
"""Get a chat session from the database."""
|
|
||||||
session = await chat_db().get_chat_session(session_id)
|
|
||||||
if not session:
|
|
||||||
return None
|
|
||||||
|
|
||||||
logger.info(
|
|
||||||
f"Loaded session {session_id} from DB: "
|
|
||||||
f"has_messages={bool(session.messages)}, "
|
|
||||||
f"message_count={len(session.messages)}, "
|
|
||||||
f"roles={[m.role for m in session.messages]}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return session
|
|
||||||
|
|
||||||
|
|
||||||
async def upsert_chat_session(
|
async def upsert_chat_session(
|
||||||
session: ChatSession,
|
session: ChatSession,
|
||||||
) -> ChatSession:
|
) -> ChatSession:
|
||||||
@@ -451,35 +458,25 @@ async def upsert_chat_session(
|
|||||||
lock = await _get_session_lock(session.session_id)
|
lock = await _get_session_lock(session.session_id)
|
||||||
|
|
||||||
async with lock:
|
async with lock:
|
||||||
# Always query DB for existing message count to ensure consistency
|
# Get existing message count from DB for incremental saves
|
||||||
existing_message_count = await chat_db().get_next_sequence(session.session_id)
|
existing_message_count = await chat_db.get_chat_session_message_count(
|
||||||
|
session.session_id
|
||||||
|
)
|
||||||
|
|
||||||
db_error: Exception | None = None
|
db_error: Exception | None = None
|
||||||
|
|
||||||
# Save to database (primary storage)
|
# Save to database (primary storage)
|
||||||
try:
|
try:
|
||||||
await _save_session_to_db(
|
await _save_session_to_db(session, existing_message_count)
|
||||||
session,
|
|
||||||
existing_message_count,
|
|
||||||
skip_existence_check=existing_message_count > 0,
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Failed to save session {session.session_id} to database: {e}"
|
f"Failed to save session {session.session_id} to database: {e}"
|
||||||
)
|
)
|
||||||
db_error = e
|
db_error = e
|
||||||
|
|
||||||
# Save to cache (best-effort, even if DB failed).
|
# Save to cache (best-effort, even if DB failed)
|
||||||
# Title updates (update_session_title) run *outside* this lock because
|
|
||||||
# they only touch the title field, not messages. So a concurrent rename
|
|
||||||
# or auto-title may have written a newer title to Redis while this
|
|
||||||
# upsert was in progress. Always prefer the cached title to avoid
|
|
||||||
# overwriting it with the stale in-memory copy.
|
|
||||||
try:
|
try:
|
||||||
existing_cached = await _get_session_from_cache(session.session_id)
|
await _cache_session(session)
|
||||||
if existing_cached and existing_cached.title:
|
|
||||||
session = session.model_copy(update={"title": existing_cached.title})
|
|
||||||
await cache_chat_session(session)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# If DB succeeded but cache failed, raise cache error
|
# If DB succeeded but cache failed, raise cache error
|
||||||
if db_error is None:
|
if db_error is None:
|
||||||
@@ -500,107 +497,6 @@ async def upsert_chat_session(
|
|||||||
return session
|
return session
|
||||||
|
|
||||||
|
|
||||||
async def _save_session_to_db(
|
|
||||||
session: ChatSession,
|
|
||||||
existing_message_count: int,
|
|
||||||
*,
|
|
||||||
skip_existence_check: bool = False,
|
|
||||||
) -> None:
|
|
||||||
"""Save or update a chat session in the database.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
skip_existence_check: When True, skip the ``get_chat_session`` query
|
|
||||||
and assume the session row already exists. Saves one DB round trip
|
|
||||||
for incremental saves during streaming.
|
|
||||||
"""
|
|
||||||
db = chat_db()
|
|
||||||
|
|
||||||
if not skip_existence_check:
|
|
||||||
# Check if session exists in DB
|
|
||||||
existing = await db.get_chat_session(session.session_id)
|
|
||||||
|
|
||||||
if not existing:
|
|
||||||
# Create new session
|
|
||||||
await db.create_chat_session(
|
|
||||||
session_id=session.session_id,
|
|
||||||
user_id=session.user_id,
|
|
||||||
)
|
|
||||||
existing_message_count = 0
|
|
||||||
|
|
||||||
# Calculate total tokens from usage
|
|
||||||
total_prompt = sum(u.prompt_tokens for u in session.usage)
|
|
||||||
total_completion = sum(u.completion_tokens for u in session.usage)
|
|
||||||
|
|
||||||
# Update session metadata
|
|
||||||
await db.update_chat_session(
|
|
||||||
session_id=session.session_id,
|
|
||||||
credentials=session.credentials,
|
|
||||||
successful_agent_runs=session.successful_agent_runs,
|
|
||||||
successful_agent_schedules=session.successful_agent_schedules,
|
|
||||||
total_prompt_tokens=total_prompt,
|
|
||||||
total_completion_tokens=total_completion,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add new messages (only those after existing count)
|
|
||||||
new_messages = session.messages[existing_message_count:]
|
|
||||||
if new_messages:
|
|
||||||
messages_data = []
|
|
||||||
for msg in new_messages:
|
|
||||||
messages_data.append(
|
|
||||||
{
|
|
||||||
"role": msg.role,
|
|
||||||
"content": msg.content,
|
|
||||||
"name": msg.name,
|
|
||||||
"tool_call_id": msg.tool_call_id,
|
|
||||||
"refusal": msg.refusal,
|
|
||||||
"tool_calls": msg.tool_calls,
|
|
||||||
"function_call": msg.function_call,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
logger.info(
|
|
||||||
f"Saving {len(new_messages)} new messages to DB for session {session.session_id}: "
|
|
||||||
f"roles={[m['role'] for m in messages_data]}, "
|
|
||||||
f"start_sequence={existing_message_count}"
|
|
||||||
)
|
|
||||||
await db.add_chat_messages_batch(
|
|
||||||
session_id=session.session_id,
|
|
||||||
messages=messages_data,
|
|
||||||
start_sequence=existing_message_count,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def append_and_save_message(session_id: str, message: ChatMessage) -> ChatSession:
|
|
||||||
"""Atomically append a message to a session and persist it.
|
|
||||||
|
|
||||||
Acquires the session lock, re-fetches the latest session state,
|
|
||||||
appends the message, and saves — preventing message loss when
|
|
||||||
concurrent requests modify the same session.
|
|
||||||
"""
|
|
||||||
lock = await _get_session_lock(session_id)
|
|
||||||
|
|
||||||
async with lock:
|
|
||||||
session = await get_chat_session(session_id)
|
|
||||||
if session is None:
|
|
||||||
raise ValueError(f"Session {session_id} not found")
|
|
||||||
|
|
||||||
session.messages.append(message)
|
|
||||||
existing_message_count = await chat_db().get_next_sequence(session_id)
|
|
||||||
|
|
||||||
try:
|
|
||||||
await _save_session_to_db(session, existing_message_count)
|
|
||||||
except Exception as e:
|
|
||||||
raise DatabaseError(
|
|
||||||
f"Failed to persist message to session {session_id}"
|
|
||||||
) from e
|
|
||||||
|
|
||||||
try:
|
|
||||||
await cache_chat_session(session)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Cache write failed for session {session_id}: {e}")
|
|
||||||
|
|
||||||
return session
|
|
||||||
|
|
||||||
|
|
||||||
async def create_chat_session(user_id: str) -> ChatSession:
|
async def create_chat_session(user_id: str) -> ChatSession:
|
||||||
"""Create a new chat session and persist it.
|
"""Create a new chat session and persist it.
|
||||||
|
|
||||||
@@ -613,7 +509,7 @@ async def create_chat_session(user_id: str) -> ChatSession:
|
|||||||
|
|
||||||
# Create in database first - fail fast if this fails
|
# Create in database first - fail fast if this fails
|
||||||
try:
|
try:
|
||||||
await chat_db().create_chat_session(
|
await chat_db.create_chat_session(
|
||||||
session_id=session.session_id,
|
session_id=session.session_id,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
)
|
)
|
||||||
@@ -625,7 +521,7 @@ async def create_chat_session(user_id: str) -> ChatSession:
|
|||||||
|
|
||||||
# Cache the session (best-effort optimization, DB is source of truth)
|
# Cache the session (best-effort optimization, DB is source of truth)
|
||||||
try:
|
try:
|
||||||
await cache_chat_session(session)
|
await _cache_session(session)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to cache new session {session.session_id}: {e}")
|
logger.warning(f"Failed to cache new session {session.session_id}: {e}")
|
||||||
|
|
||||||
@@ -636,16 +532,20 @@ async def get_user_sessions(
|
|||||||
user_id: str,
|
user_id: str,
|
||||||
limit: int = 50,
|
limit: int = 50,
|
||||||
offset: int = 0,
|
offset: int = 0,
|
||||||
) -> tuple[list[ChatSessionInfo], int]:
|
) -> tuple[list[ChatSession], int]:
|
||||||
"""Get chat sessions for a user from the database with total count.
|
"""Get chat sessions for a user from the database with total count.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A tuple of (sessions, total_count) where total_count is the overall
|
A tuple of (sessions, total_count) where total_count is the overall
|
||||||
number of sessions for the user (not just the current page).
|
number of sessions for the user (not just the current page).
|
||||||
"""
|
"""
|
||||||
db = chat_db()
|
prisma_sessions = await chat_db.get_user_chat_sessions(user_id, limit, offset)
|
||||||
sessions = await db.get_user_chat_sessions(user_id, limit, offset)
|
total_count = await chat_db.get_user_session_count(user_id)
|
||||||
total_count = await db.get_user_session_count(user_id)
|
|
||||||
|
sessions = []
|
||||||
|
for prisma_session in prisma_sessions:
|
||||||
|
# Convert without messages for listing (lighter weight)
|
||||||
|
sessions.append(ChatSession.from_db(prisma_session, None))
|
||||||
|
|
||||||
return sessions, total_count
|
return sessions, total_count
|
||||||
|
|
||||||
@@ -663,7 +563,7 @@ async def delete_chat_session(session_id: str, user_id: str | None = None) -> bo
|
|||||||
"""
|
"""
|
||||||
# Delete from database first (with optional user_id validation)
|
# Delete from database first (with optional user_id validation)
|
||||||
# This confirms ownership before invalidating cache
|
# This confirms ownership before invalidating cache
|
||||||
deleted = await chat_db().delete_chat_session(session_id, user_id)
|
deleted = await chat_db.delete_chat_session(session_id, user_id)
|
||||||
|
|
||||||
if not deleted:
|
if not deleted:
|
||||||
return False
|
return False
|
||||||
@@ -680,89 +580,38 @@ async def delete_chat_session(session_id: str, user_id: str | None = None) -> bo
|
|||||||
async with _session_locks_mutex:
|
async with _session_locks_mutex:
|
||||||
_session_locks.pop(session_id, None)
|
_session_locks.pop(session_id, None)
|
||||||
|
|
||||||
# Shut down any local browser daemon for this session (best-effort).
|
|
||||||
# Inline import required: all tool modules import ChatSession from this
|
|
||||||
# module, so any top-level import from tools.* would create a cycle.
|
|
||||||
try:
|
|
||||||
from .tools.agent_browser import close_browser_session
|
|
||||||
|
|
||||||
await close_browser_session(session_id, user_id=user_id)
|
|
||||||
except Exception as e:
|
|
||||||
logger.debug(f"Browser cleanup for session {session_id}: {e}")
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def update_session_title(
|
async def update_session_title(session_id: str, title: str) -> bool:
|
||||||
session_id: str,
|
"""Update only the title of a chat session.
|
||||||
user_id: str,
|
|
||||||
title: str,
|
|
||||||
*,
|
|
||||||
only_if_empty: bool = False,
|
|
||||||
) -> bool:
|
|
||||||
"""Update the title of a chat session, scoped to the owning user.
|
|
||||||
|
|
||||||
Lightweight operation that doesn't touch messages, avoiding race conditions
|
This is a lightweight operation that doesn't touch messages, avoiding
|
||||||
with concurrent message updates.
|
race conditions with concurrent message updates. Use this for background
|
||||||
|
title generation instead of upsert_chat_session.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session_id: The session ID to update.
|
session_id: The session ID to update.
|
||||||
user_id: Owning user — the DB query filters on this.
|
|
||||||
title: The new title to set.
|
title: The new title to set.
|
||||||
only_if_empty: When True, uses an atomic ``UPDATE WHERE title IS NULL``
|
|
||||||
so auto-generated titles never overwrite a user-set title.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
True if updated successfully, False otherwise (not found, wrong user,
|
True if updated successfully, False otherwise.
|
||||||
or — when only_if_empty — title was already set).
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
updated = await chat_db().update_chat_session_title(
|
result = await chat_db.update_chat_session(session_id=session_id, title=title)
|
||||||
session_id, user_id, title, only_if_empty=only_if_empty
|
if result is None:
|
||||||
)
|
logger.warning(f"Session {session_id} not found for title update")
|
||||||
if not updated:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Update title in cache if it exists (instead of invalidating).
|
# Invalidate cache so next fetch gets updated title
|
||||||
# This prevents race conditions where cache invalidation causes
|
|
||||||
# the frontend to see stale DB data while streaming is still in progress.
|
|
||||||
try:
|
try:
|
||||||
cached = await _get_session_from_cache(session_id)
|
redis_key = _get_session_cache_key(session_id)
|
||||||
if cached:
|
async_redis = await get_redis_async()
|
||||||
cached.title = title
|
await async_redis.delete(redis_key)
|
||||||
await cache_chat_session(cached)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(
|
logger.warning(f"Failed to invalidate cache for session {session_id}: {e}")
|
||||||
f"Cache title update failed for session {session_id} (non-critical): {e}"
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to update title for session {session_id}: {e}")
|
logger.error(f"Failed to update title for session {session_id}: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# ==================== Chat session locks ==================== #
|
|
||||||
|
|
||||||
_session_locks: WeakValueDictionary[str, asyncio.Lock] = WeakValueDictionary()
|
|
||||||
_session_locks_mutex = asyncio.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
async def _get_session_lock(session_id: str) -> asyncio.Lock:
|
|
||||||
"""Get or create a lock for a specific session to prevent concurrent upserts.
|
|
||||||
|
|
||||||
This was originally added to solve the specific problem of race conditions between
|
|
||||||
the session title thread and the conversation thread, which always occurs on the
|
|
||||||
same instance as we prevent rapid request sends on the frontend.
|
|
||||||
|
|
||||||
Uses WeakValueDictionary for automatic cleanup: locks are garbage collected
|
|
||||||
when no coroutine holds a reference to them, preventing memory leaks from
|
|
||||||
unbounded growth of session locks. Explicit cleanup also occurs
|
|
||||||
in `delete_chat_session()`.
|
|
||||||
"""
|
|
||||||
async with _session_locks_mutex:
|
|
||||||
lock = _session_locks.get(session_id)
|
|
||||||
if lock is None:
|
|
||||||
lock = asyncio.Lock()
|
|
||||||
_session_locks[session_id] = lock
|
|
||||||
return lock
|
|
||||||
119
autogpt_platform/backend/backend/api/features/chat/model_test.py
Normal file
119
autogpt_platform/backend/backend/api/features/chat/model_test.py
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from .model import (
|
||||||
|
ChatMessage,
|
||||||
|
ChatSession,
|
||||||
|
Usage,
|
||||||
|
get_chat_session,
|
||||||
|
upsert_chat_session,
|
||||||
|
)
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
ChatMessage(content="Hello, how are you?", role="user"),
|
||||||
|
ChatMessage(
|
||||||
|
content="I'm fine, thank you!",
|
||||||
|
role="assistant",
|
||||||
|
tool_calls=[
|
||||||
|
{
|
||||||
|
"id": "t123",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "get_weather",
|
||||||
|
"arguments": '{"city": "New York"}',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
ChatMessage(
|
||||||
|
content="I'm using the tool to get the weather",
|
||||||
|
role="tool",
|
||||||
|
tool_call_id="t123",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_chatsession_serialization_deserialization():
|
||||||
|
s = ChatSession.new(user_id="abc123")
|
||||||
|
s.messages = messages
|
||||||
|
s.usage = [Usage(prompt_tokens=100, completion_tokens=200, total_tokens=300)]
|
||||||
|
serialized = s.model_dump_json()
|
||||||
|
s2 = ChatSession.model_validate_json(serialized)
|
||||||
|
assert s2.model_dump() == s.model_dump()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_chatsession_redis_storage(setup_test_user, test_user_id):
|
||||||
|
|
||||||
|
s = ChatSession.new(user_id=test_user_id)
|
||||||
|
s.messages = messages
|
||||||
|
|
||||||
|
s = await upsert_chat_session(s)
|
||||||
|
|
||||||
|
s2 = await get_chat_session(
|
||||||
|
session_id=s.session_id,
|
||||||
|
user_id=s.user_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert s2 == s
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_chatsession_redis_storage_user_id_mismatch(
|
||||||
|
setup_test_user, test_user_id
|
||||||
|
):
|
||||||
|
|
||||||
|
s = ChatSession.new(user_id=test_user_id)
|
||||||
|
s.messages = messages
|
||||||
|
s = await upsert_chat_session(s)
|
||||||
|
|
||||||
|
s2 = await get_chat_session(s.session_id, "different_user_id")
|
||||||
|
|
||||||
|
assert s2 is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_chatsession_db_storage(setup_test_user, test_user_id):
|
||||||
|
"""Test that messages are correctly saved to and loaded from DB (not cache)."""
|
||||||
|
from backend.data.redis_client import get_redis_async
|
||||||
|
|
||||||
|
# Create session with messages including assistant message
|
||||||
|
s = ChatSession.new(user_id=test_user_id)
|
||||||
|
s.messages = messages # Contains user, assistant, and tool messages
|
||||||
|
assert s.session_id is not None, "Session id is not set"
|
||||||
|
# Upsert to save to both cache and DB
|
||||||
|
s = await upsert_chat_session(s)
|
||||||
|
|
||||||
|
# Clear the Redis cache to force DB load
|
||||||
|
redis_key = f"chat:session:{s.session_id}"
|
||||||
|
async_redis = await get_redis_async()
|
||||||
|
await async_redis.delete(redis_key)
|
||||||
|
|
||||||
|
# Load from DB (cache was cleared)
|
||||||
|
s2 = await get_chat_session(
|
||||||
|
session_id=s.session_id,
|
||||||
|
user_id=s.user_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert s2 is not None, "Session not found after loading from DB"
|
||||||
|
assert len(s2.messages) == len(
|
||||||
|
s.messages
|
||||||
|
), f"Message count mismatch: expected {len(s.messages)}, got {len(s2.messages)}"
|
||||||
|
|
||||||
|
# Verify all roles are present
|
||||||
|
roles = [m.role for m in s2.messages]
|
||||||
|
assert "user" in roles, f"User message missing. Roles found: {roles}"
|
||||||
|
assert "assistant" in roles, f"Assistant message missing. Roles found: {roles}"
|
||||||
|
assert "tool" in roles, f"Tool message missing. Roles found: {roles}"
|
||||||
|
|
||||||
|
# Verify message content
|
||||||
|
for orig, loaded in zip(s.messages, s2.messages):
|
||||||
|
assert orig.role == loaded.role, f"Role mismatch: {orig.role} != {loaded.role}"
|
||||||
|
assert (
|
||||||
|
orig.content == loaded.content
|
||||||
|
), f"Content mismatch for {orig.role}: {orig.content} != {loaded.content}"
|
||||||
|
if orig.tool_calls:
|
||||||
|
assert (
|
||||||
|
loaded.tool_calls is not None
|
||||||
|
), f"Tool calls missing for {orig.role} message"
|
||||||
|
assert len(orig.tool_calls) == len(loaded.tool_calls)
|
||||||
@@ -5,18 +5,11 @@ This module implements the AI SDK UI Stream Protocol (v1) for streaming chat res
|
|||||||
See: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol
|
See: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
from backend.util.json import dumps as json_dumps
|
|
||||||
from backend.util.truncate import truncate
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class ResponseType(str, Enum):
|
class ResponseType(str, Enum):
|
||||||
"""Types of streaming responses following AI SDK protocol."""
|
"""Types of streaming responses following AI SDK protocol."""
|
||||||
@@ -52,8 +45,7 @@ class StreamBaseResponse(BaseModel):
|
|||||||
|
|
||||||
def to_sse(self) -> str:
|
def to_sse(self) -> str:
|
||||||
"""Convert to SSE format."""
|
"""Convert to SSE format."""
|
||||||
json_str = self.model_dump_json(exclude_none=True)
|
return f"data: {self.model_dump_json()}\n\n"
|
||||||
return f"data: {json_str}\n\n"
|
|
||||||
|
|
||||||
|
|
||||||
# ========== Message Lifecycle ==========
|
# ========== Message Lifecycle ==========
|
||||||
@@ -64,13 +56,15 @@ class StreamStart(StreamBaseResponse):
|
|||||||
|
|
||||||
type: ResponseType = ResponseType.START
|
type: ResponseType = ResponseType.START
|
||||||
messageId: str = Field(..., description="Unique message ID")
|
messageId: str = Field(..., description="Unique message ID")
|
||||||
sessionId: str | None = Field(
|
taskId: str | None = Field(
|
||||||
default=None,
|
default=None,
|
||||||
description="Session ID for SSE reconnection.",
|
description="Task ID for SSE reconnection. Clients can reconnect using GET /tasks/{taskId}/stream",
|
||||||
)
|
)
|
||||||
|
|
||||||
def to_sse(self) -> str:
|
def to_sse(self) -> str:
|
||||||
"""Convert to SSE format, excluding non-protocol fields like sessionId."""
|
"""Convert to SSE format, excluding non-protocol fields like taskId."""
|
||||||
|
import json
|
||||||
|
|
||||||
data: dict[str, Any] = {
|
data: dict[str, Any] = {
|
||||||
"type": self.type.value,
|
"type": self.type.value,
|
||||||
"messageId": self.messageId,
|
"messageId": self.messageId,
|
||||||
@@ -151,9 +145,6 @@ class StreamToolInputAvailable(StreamBaseResponse):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
_MAX_TOOL_OUTPUT_SIZE = 100_000 # ~100 KB; truncate to avoid bloating SSE/DB
|
|
||||||
|
|
||||||
|
|
||||||
class StreamToolOutputAvailable(StreamBaseResponse):
|
class StreamToolOutputAvailable(StreamBaseResponse):
|
||||||
"""Tool execution result."""
|
"""Tool execution result."""
|
||||||
|
|
||||||
@@ -168,12 +159,10 @@ class StreamToolOutputAvailable(StreamBaseResponse):
|
|||||||
default=True, description="Whether the tool execution succeeded"
|
default=True, description="Whether the tool execution succeeded"
|
||||||
)
|
)
|
||||||
|
|
||||||
def model_post_init(self, __context: Any) -> None:
|
|
||||||
"""Truncate oversized outputs after construction."""
|
|
||||||
self.output = truncate(self.output, _MAX_TOOL_OUTPUT_SIZE)
|
|
||||||
|
|
||||||
def to_sse(self) -> str:
|
def to_sse(self) -> str:
|
||||||
"""Convert to SSE format, excluding non-spec fields."""
|
"""Convert to SSE format, excluding non-spec fields."""
|
||||||
|
import json
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"type": self.type.value,
|
"type": self.type.value,
|
||||||
"toolCallId": self.toolCallId,
|
"toolCallId": self.toolCallId,
|
||||||
@@ -204,18 +193,6 @@ class StreamError(StreamBaseResponse):
|
|||||||
default=None, description="Additional error details"
|
default=None, description="Additional error details"
|
||||||
)
|
)
|
||||||
|
|
||||||
def to_sse(self) -> str:
|
|
||||||
"""Convert to SSE format, only emitting fields required by AI SDK protocol.
|
|
||||||
|
|
||||||
The AI SDK uses z.strictObject({type, errorText}) which rejects
|
|
||||||
any extra fields like `code` or `details`.
|
|
||||||
"""
|
|
||||||
data = {
|
|
||||||
"type": self.type.value,
|
|
||||||
"errorText": self.errorText,
|
|
||||||
}
|
|
||||||
return f"data: {json_dumps(data)}\n\n"
|
|
||||||
|
|
||||||
|
|
||||||
class StreamHeartbeat(StreamBaseResponse):
|
class StreamHeartbeat(StreamBaseResponse):
|
||||||
"""Heartbeat to keep SSE connection alive during long-running operations.
|
"""Heartbeat to keep SSE connection alive during long-running operations.
|
||||||
@@ -1,41 +1,29 @@
|
|||||||
"""Chat API routes for chat session management and streaming via SSE."""
|
"""Chat API routes for chat session management and streaming via SSE."""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import uuid as uuid_module
|
||||||
from collections.abc import AsyncGenerator
|
from collections.abc import AsyncGenerator
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
from uuid import uuid4
|
|
||||||
|
|
||||||
from autogpt_libs import auth
|
from autogpt_libs import auth
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response, Security
|
from fastapi import APIRouter, Depends, Header, HTTPException, Query, Response, Security
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
from prisma.models import UserWorkspaceFile
|
from pydantic import BaseModel
|
||||||
from pydantic import BaseModel, Field, field_validator
|
|
||||||
|
|
||||||
from backend.copilot import service as chat_service
|
from backend.util.exceptions import NotFoundError
|
||||||
from backend.copilot import stream_registry
|
|
||||||
from backend.copilot.config import ChatConfig
|
from . import service as chat_service
|
||||||
from backend.copilot.executor.utils import enqueue_cancel_task, enqueue_copilot_turn
|
from . import stream_registry
|
||||||
from backend.copilot.model import (
|
from .completion_handler import process_operation_failure, process_operation_success
|
||||||
ChatMessage,
|
from .config import ChatConfig
|
||||||
ChatSession,
|
from .model import ChatSession, create_chat_session, get_chat_session, get_user_sessions
|
||||||
append_and_save_message,
|
from .response_model import StreamFinish, StreamHeartbeat
|
||||||
create_chat_session,
|
from .tools.models import (
|
||||||
delete_chat_session,
|
|
||||||
get_chat_session,
|
|
||||||
get_user_sessions,
|
|
||||||
update_session_title,
|
|
||||||
)
|
|
||||||
from backend.copilot.response_model import StreamError, StreamFinish, StreamHeartbeat
|
|
||||||
from backend.copilot.tools.e2b_sandbox import kill_sandbox
|
|
||||||
from backend.copilot.tools.models import (
|
|
||||||
AgentDetailsResponse,
|
AgentDetailsResponse,
|
||||||
AgentOutputResponse,
|
AgentOutputResponse,
|
||||||
AgentPreviewResponse,
|
AgentPreviewResponse,
|
||||||
AgentSavedResponse,
|
AgentSavedResponse,
|
||||||
AgentsFoundResponse,
|
AgentsFoundResponse,
|
||||||
BlockDetailsResponse,
|
|
||||||
BlockListResponse,
|
BlockListResponse,
|
||||||
BlockOutputResponse,
|
BlockOutputResponse,
|
||||||
ClarificationNeededResponse,
|
ClarificationNeededResponse,
|
||||||
@@ -44,25 +32,17 @@ from backend.copilot.tools.models import (
|
|||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
ExecutionStartedResponse,
|
ExecutionStartedResponse,
|
||||||
InputValidationErrorResponse,
|
InputValidationErrorResponse,
|
||||||
MCPToolOutputResponse,
|
|
||||||
MCPToolsDiscoveredResponse,
|
|
||||||
NeedLoginResponse,
|
NeedLoginResponse,
|
||||||
NoResultsResponse,
|
NoResultsResponse,
|
||||||
|
OperationInProgressResponse,
|
||||||
|
OperationPendingResponse,
|
||||||
|
OperationStartedResponse,
|
||||||
SetupRequirementsResponse,
|
SetupRequirementsResponse,
|
||||||
SuggestedGoalResponse,
|
|
||||||
UnderstandingUpdatedResponse,
|
UnderstandingUpdatedResponse,
|
||||||
)
|
)
|
||||||
from backend.copilot.tracking import track_user_message
|
|
||||||
from backend.data.redis_client import get_redis_async
|
|
||||||
from backend.data.understanding import get_business_understanding
|
|
||||||
from backend.data.workspace import get_or_create_workspace
|
|
||||||
from backend.util.exceptions import NotFoundError
|
|
||||||
|
|
||||||
config = ChatConfig()
|
config = ChatConfig()
|
||||||
|
|
||||||
_UUID_RE = re.compile(
|
|
||||||
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I
|
|
||||||
)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -91,9 +71,6 @@ class StreamChatRequest(BaseModel):
|
|||||||
message: str
|
message: str
|
||||||
is_user_message: bool = True
|
is_user_message: bool = True
|
||||||
context: dict[str, str] | None = None # {url: str, content: str}
|
context: dict[str, str] | None = None # {url: str, content: str}
|
||||||
file_ids: list[str] | None = Field(
|
|
||||||
default=None, max_length=20
|
|
||||||
) # Workspace file IDs attached to this message
|
|
||||||
|
|
||||||
|
|
||||||
class CreateSessionResponse(BaseModel):
|
class CreateSessionResponse(BaseModel):
|
||||||
@@ -107,8 +84,10 @@ class CreateSessionResponse(BaseModel):
|
|||||||
class ActiveStreamInfo(BaseModel):
|
class ActiveStreamInfo(BaseModel):
|
||||||
"""Information about an active stream for reconnection."""
|
"""Information about an active stream for reconnection."""
|
||||||
|
|
||||||
turn_id: str
|
task_id: str
|
||||||
last_message_id: str # Redis Stream message ID for resumption
|
last_message_id: str # Redis Stream message ID for resumption
|
||||||
|
operation_id: str # Operation ID for completion tracking
|
||||||
|
tool_name: str # Name of the tool being executed
|
||||||
|
|
||||||
|
|
||||||
class SessionDetailResponse(BaseModel):
|
class SessionDetailResponse(BaseModel):
|
||||||
@@ -129,7 +108,6 @@ class SessionSummaryResponse(BaseModel):
|
|||||||
created_at: str
|
created_at: str
|
||||||
updated_at: str
|
updated_at: str
|
||||||
title: str | None = None
|
title: str | None = None
|
||||||
is_processing: bool
|
|
||||||
|
|
||||||
|
|
||||||
class ListSessionsResponse(BaseModel):
|
class ListSessionsResponse(BaseModel):
|
||||||
@@ -139,25 +117,12 @@ class ListSessionsResponse(BaseModel):
|
|||||||
total: int
|
total: int
|
||||||
|
|
||||||
|
|
||||||
class CancelSessionResponse(BaseModel):
|
class OperationCompleteRequest(BaseModel):
|
||||||
"""Response model for the cancel session endpoint."""
|
"""Request model for external completion webhook."""
|
||||||
|
|
||||||
cancelled: bool
|
success: bool
|
||||||
reason: str | None = None
|
result: dict | str | None = None
|
||||||
|
error: str | None = None
|
||||||
|
|
||||||
class UpdateSessionTitleRequest(BaseModel):
|
|
||||||
"""Request model for updating a session's title."""
|
|
||||||
|
|
||||||
title: str
|
|
||||||
|
|
||||||
@field_validator("title")
|
|
||||||
@classmethod
|
|
||||||
def title_must_not_be_blank(cls, v: str) -> str:
|
|
||||||
stripped = v.strip()
|
|
||||||
if not stripped:
|
|
||||||
raise ValueError("Title must not be blank")
|
|
||||||
return stripped
|
|
||||||
|
|
||||||
|
|
||||||
# ========== Routes ==========
|
# ========== Routes ==========
|
||||||
@@ -188,28 +153,6 @@ async def list_sessions(
|
|||||||
"""
|
"""
|
||||||
sessions, total_count = await get_user_sessions(user_id, limit, offset)
|
sessions, total_count = await get_user_sessions(user_id, limit, offset)
|
||||||
|
|
||||||
# Batch-check Redis for active stream status on each session
|
|
||||||
processing_set: set[str] = set()
|
|
||||||
if sessions:
|
|
||||||
try:
|
|
||||||
redis = await get_redis_async()
|
|
||||||
pipe = redis.pipeline(transaction=False)
|
|
||||||
for session in sessions:
|
|
||||||
pipe.hget(
|
|
||||||
f"{config.session_meta_prefix}{session.session_id}",
|
|
||||||
"status",
|
|
||||||
)
|
|
||||||
statuses = await pipe.execute()
|
|
||||||
processing_set = {
|
|
||||||
session.session_id
|
|
||||||
for session, st in zip(sessions, statuses)
|
|
||||||
if st == "running"
|
|
||||||
}
|
|
||||||
except Exception:
|
|
||||||
logger.warning(
|
|
||||||
"Failed to fetch processing status from Redis; " "defaulting to empty"
|
|
||||||
)
|
|
||||||
|
|
||||||
return ListSessionsResponse(
|
return ListSessionsResponse(
|
||||||
sessions=[
|
sessions=[
|
||||||
SessionSummaryResponse(
|
SessionSummaryResponse(
|
||||||
@@ -217,7 +160,6 @@ async def list_sessions(
|
|||||||
created_at=session.started_at.isoformat(),
|
created_at=session.started_at.isoformat(),
|
||||||
updated_at=session.updated_at.isoformat(),
|
updated_at=session.updated_at.isoformat(),
|
||||||
title=session.title,
|
title=session.title,
|
||||||
is_processing=session.session_id in processing_set,
|
|
||||||
)
|
)
|
||||||
for session in sessions
|
for session in sessions
|
||||||
],
|
],
|
||||||
@@ -257,92 +199,6 @@ async def create_session(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.delete(
|
|
||||||
"/sessions/{session_id}",
|
|
||||||
dependencies=[Security(auth.requires_user)],
|
|
||||||
status_code=204,
|
|
||||||
responses={404: {"description": "Session not found or access denied"}},
|
|
||||||
)
|
|
||||||
async def delete_session(
|
|
||||||
session_id: str,
|
|
||||||
user_id: Annotated[str, Security(auth.get_user_id)],
|
|
||||||
) -> Response:
|
|
||||||
"""
|
|
||||||
Delete a chat session.
|
|
||||||
|
|
||||||
Permanently removes a chat session and all its messages.
|
|
||||||
Only the owner can delete their sessions.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
session_id: The session ID to delete.
|
|
||||||
user_id: The authenticated user's ID.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
204 No Content on success.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
HTTPException: 404 if session not found or not owned by user.
|
|
||||||
"""
|
|
||||||
deleted = await delete_chat_session(session_id, user_id)
|
|
||||||
|
|
||||||
if not deleted:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=404,
|
|
||||||
detail=f"Session {session_id} not found or access denied",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Best-effort cleanup of the E2B sandbox (if any).
|
|
||||||
# sandbox_id is in Redis; kill_sandbox() fetches it from there.
|
|
||||||
e2b_cfg = ChatConfig()
|
|
||||||
if e2b_cfg.e2b_active:
|
|
||||||
assert e2b_cfg.e2b_api_key # guaranteed by e2b_active check
|
|
||||||
try:
|
|
||||||
await kill_sandbox(session_id, e2b_cfg.e2b_api_key)
|
|
||||||
except Exception:
|
|
||||||
logger.warning(
|
|
||||||
"[E2B] Failed to kill sandbox for session %s", session_id[:12]
|
|
||||||
)
|
|
||||||
|
|
||||||
return Response(status_code=204)
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch(
|
|
||||||
"/sessions/{session_id}/title",
|
|
||||||
summary="Update session title",
|
|
||||||
dependencies=[Security(auth.requires_user)],
|
|
||||||
status_code=200,
|
|
||||||
responses={404: {"description": "Session not found or access denied"}},
|
|
||||||
)
|
|
||||||
async def update_session_title_route(
|
|
||||||
session_id: str,
|
|
||||||
request: UpdateSessionTitleRequest,
|
|
||||||
user_id: Annotated[str, Security(auth.get_user_id)],
|
|
||||||
) -> dict:
|
|
||||||
"""
|
|
||||||
Update the title of a chat session.
|
|
||||||
|
|
||||||
Allows the user to rename their chat session.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
session_id: The session ID to update.
|
|
||||||
request: Request body containing the new title.
|
|
||||||
user_id: The authenticated user's ID.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
dict: Status of the update.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
HTTPException: 404 if session not found or not owned by user.
|
|
||||||
"""
|
|
||||||
success = await update_session_title(session_id, user_id, request.title)
|
|
||||||
if not success:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=404,
|
|
||||||
detail=f"Session {session_id} not found or access denied",
|
|
||||||
)
|
|
||||||
return {"status": "ok"}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/sessions/{session_id}",
|
"/sessions/{session_id}",
|
||||||
)
|
)
|
||||||
@@ -354,7 +210,7 @@ async def get_session(
|
|||||||
Retrieve the details of a specific chat session.
|
Retrieve the details of a specific chat session.
|
||||||
|
|
||||||
Looks up a chat session by ID for the given user (if authenticated) and returns all session data including messages.
|
Looks up a chat session by ID for the given user (if authenticated) and returns all session data including messages.
|
||||||
If there's an active stream for this session, returns active_stream info for reconnection.
|
If there's an active stream for this session, returns the task_id for reconnection.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session_id: The unique identifier for the desired chat session.
|
session_id: The unique identifier for the desired chat session.
|
||||||
@@ -372,21 +228,24 @@ async def get_session(
|
|||||||
|
|
||||||
# Check if there's an active stream for this session
|
# Check if there's an active stream for this session
|
||||||
active_stream_info = None
|
active_stream_info = None
|
||||||
active_session, last_message_id = await stream_registry.get_active_session(
|
active_task, last_message_id = await stream_registry.get_active_task_for_session(
|
||||||
session_id, user_id
|
session_id, user_id
|
||||||
)
|
)
|
||||||
logger.info(
|
if active_task:
|
||||||
f"[GET_SESSION] session={session_id}, active_session={active_session is not None}, "
|
# Filter out the in-progress assistant message from the session response.
|
||||||
f"msg_count={len(messages)}, last_role={messages[-1].get('role') if messages else 'none'}"
|
# The client will receive the complete assistant response through the SSE
|
||||||
)
|
# stream replay instead, preventing duplicate content.
|
||||||
if active_session:
|
if messages and messages[-1].get("role") == "assistant":
|
||||||
# Keep the assistant message (including tool_calls) so the frontend can
|
messages = messages[:-1]
|
||||||
# render the correct tool UI (e.g. CreateAgent with mini game).
|
|
||||||
# convertChatSessionToUiMessages handles isComplete=false by setting
|
# Use "0-0" as last_message_id to replay the stream from the beginning.
|
||||||
# tool parts without output to state "input-available".
|
# Since we filtered out the cached assistant message, the client needs
|
||||||
|
# the full stream to reconstruct the response.
|
||||||
active_stream_info = ActiveStreamInfo(
|
active_stream_info = ActiveStreamInfo(
|
||||||
turn_id=active_session.turn_id,
|
task_id=active_task.task_id,
|
||||||
last_message_id=last_message_id,
|
last_message_id="0-0",
|
||||||
|
operation_id=active_task.operation_id,
|
||||||
|
tool_name=active_task.tool_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
return SessionDetailResponse(
|
return SessionDetailResponse(
|
||||||
@@ -399,51 +258,6 @@ async def get_session(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
|
||||||
"/sessions/{session_id}/cancel",
|
|
||||||
status_code=200,
|
|
||||||
)
|
|
||||||
async def cancel_session_task(
|
|
||||||
session_id: str,
|
|
||||||
user_id: Annotated[str | None, Depends(auth.get_user_id)],
|
|
||||||
) -> CancelSessionResponse:
|
|
||||||
"""Cancel the active streaming task for a session.
|
|
||||||
|
|
||||||
Publishes a cancel event to the executor via RabbitMQ FANOUT, then
|
|
||||||
polls Redis until the task status flips from ``running`` or a timeout
|
|
||||||
(5 s) is reached. Returns only after the cancellation is confirmed.
|
|
||||||
"""
|
|
||||||
await _validate_and_get_session(session_id, user_id)
|
|
||||||
|
|
||||||
active_session, _ = await stream_registry.get_active_session(session_id, user_id)
|
|
||||||
if not active_session:
|
|
||||||
return CancelSessionResponse(cancelled=True, reason="no_active_session")
|
|
||||||
|
|
||||||
await enqueue_cancel_task(session_id)
|
|
||||||
logger.info(f"[CANCEL] Published cancel for session ...{session_id[-8:]}")
|
|
||||||
|
|
||||||
# Poll until the executor confirms the task is no longer running.
|
|
||||||
poll_interval = 0.5
|
|
||||||
max_wait = 5.0
|
|
||||||
waited = 0.0
|
|
||||||
while waited < max_wait:
|
|
||||||
await asyncio.sleep(poll_interval)
|
|
||||||
waited += poll_interval
|
|
||||||
session_state = await stream_registry.get_session(session_id)
|
|
||||||
if session_state is None or session_state.status != "running":
|
|
||||||
logger.info(
|
|
||||||
f"[CANCEL] Session ...{session_id[-8:]} confirmed stopped "
|
|
||||||
f"(status={session_state.status if session_state else 'gone'}) after {waited:.1f}s"
|
|
||||||
)
|
|
||||||
return CancelSessionResponse(cancelled=True)
|
|
||||||
|
|
||||||
logger.warning(
|
|
||||||
f"[CANCEL] Session ...{session_id[-8:]} not confirmed after {max_wait}s, force-completing"
|
|
||||||
)
|
|
||||||
await stream_registry.mark_session_completed(session_id, error_message="Cancelled")
|
|
||||||
return CancelSessionResponse(cancelled=True)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
@router.post(
|
||||||
"/sessions/{session_id}/stream",
|
"/sessions/{session_id}/stream",
|
||||||
)
|
)
|
||||||
@@ -461,15 +275,16 @@ async def stream_chat_post(
|
|||||||
- Tool execution results
|
- Tool execution results
|
||||||
|
|
||||||
The AI generation runs in a background task that continues even if the client disconnects.
|
The AI generation runs in a background task that continues even if the client disconnects.
|
||||||
All chunks are written to a per-turn Redis stream for reconnection support. If the client
|
All chunks are written to Redis for reconnection support. If the client disconnects,
|
||||||
disconnects, they can reconnect using GET /sessions/{session_id}/stream to resume.
|
they can reconnect using GET /tasks/{task_id}/stream to resume from where they left off.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session_id: The chat session identifier to associate with the streamed messages.
|
session_id: The chat session identifier to associate with the streamed messages.
|
||||||
request: Request body containing message, is_user_message, and optional context.
|
request: Request body containing message, is_user_message, and optional context.
|
||||||
user_id: Optional authenticated user ID.
|
user_id: Optional authenticated user ID.
|
||||||
Returns:
|
Returns:
|
||||||
StreamingResponse: SSE-formatted response chunks.
|
StreamingResponse: SSE-formatted response chunks. First chunk is a "start" event
|
||||||
|
containing the task_id for reconnection.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -485,9 +300,10 @@ async def stream_chat_post(
|
|||||||
f"user={user_id}, message_len={len(request.message)}",
|
f"user={user_id}, message_len={len(request.message)}",
|
||||||
extra={"json_fields": log_meta},
|
extra={"json_fields": log_meta},
|
||||||
)
|
)
|
||||||
await _validate_and_get_session(session_id, user_id)
|
|
||||||
|
session = await _validate_and_get_session(session_id, user_id)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[TIMING] session validated in {(time.perf_counter() - stream_start_time) * 1000:.1f}ms",
|
f"[TIMING] session validated in {(time.perf_counter() - stream_start_time)*1000:.1f}ms",
|
||||||
extra={
|
extra={
|
||||||
"json_fields": {
|
"json_fields": {
|
||||||
**log_meta,
|
**log_meta,
|
||||||
@@ -496,95 +312,106 @@ async def stream_chat_post(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Enrich message with file metadata if file_ids are provided.
|
|
||||||
# Also sanitise file_ids so only validated, workspace-scoped IDs are
|
|
||||||
# forwarded downstream (e.g. to the executor via enqueue_copilot_turn).
|
|
||||||
sanitized_file_ids: list[str] | None = None
|
|
||||||
if request.file_ids and user_id:
|
|
||||||
# Filter to valid UUIDs only to prevent DB abuse
|
|
||||||
valid_ids = [fid for fid in request.file_ids if _UUID_RE.match(fid)]
|
|
||||||
|
|
||||||
if valid_ids:
|
|
||||||
workspace = await get_or_create_workspace(user_id)
|
|
||||||
# Batch query instead of N+1
|
|
||||||
files = await UserWorkspaceFile.prisma().find_many(
|
|
||||||
where={
|
|
||||||
"id": {"in": valid_ids},
|
|
||||||
"workspaceId": workspace.id,
|
|
||||||
"isDeleted": False,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
# Only keep IDs that actually exist in the user's workspace
|
|
||||||
sanitized_file_ids = [wf.id for wf in files] or None
|
|
||||||
file_lines: list[str] = [
|
|
||||||
f"- {wf.name} ({wf.mimeType}, {round(wf.sizeBytes / 1024, 1)} KB), file_id={wf.id}"
|
|
||||||
for wf in files
|
|
||||||
]
|
|
||||||
if file_lines:
|
|
||||||
files_block = (
|
|
||||||
"\n\n[Attached files]\n"
|
|
||||||
+ "\n".join(file_lines)
|
|
||||||
+ "\nUse read_workspace_file with the file_id to access file contents."
|
|
||||||
)
|
|
||||||
request.message += files_block
|
|
||||||
|
|
||||||
# Atomically append user message to session BEFORE creating task to avoid
|
|
||||||
# race condition where GET_SESSION sees task as "running" but message isn't
|
|
||||||
# saved yet. append_and_save_message re-fetches inside a lock to prevent
|
|
||||||
# message loss from concurrent requests.
|
|
||||||
if request.message:
|
|
||||||
message = ChatMessage(
|
|
||||||
role="user" if request.is_user_message else "assistant",
|
|
||||||
content=request.message,
|
|
||||||
)
|
|
||||||
if request.is_user_message:
|
|
||||||
track_user_message(
|
|
||||||
user_id=user_id,
|
|
||||||
session_id=session_id,
|
|
||||||
message_length=len(request.message),
|
|
||||||
)
|
|
||||||
logger.info(f"[STREAM] Saving user message to session {session_id}")
|
|
||||||
await append_and_save_message(session_id, message)
|
|
||||||
logger.info(f"[STREAM] User message saved for session {session_id}")
|
|
||||||
|
|
||||||
# Create a task in the stream registry for reconnection support
|
# Create a task in the stream registry for reconnection support
|
||||||
turn_id = str(uuid4())
|
task_id = str(uuid_module.uuid4())
|
||||||
log_meta["turn_id"] = turn_id
|
operation_id = str(uuid_module.uuid4())
|
||||||
|
log_meta["task_id"] = task_id
|
||||||
|
|
||||||
session_create_start = time.perf_counter()
|
task_create_start = time.perf_counter()
|
||||||
await stream_registry.create_session(
|
await stream_registry.create_task(
|
||||||
|
task_id=task_id,
|
||||||
session_id=session_id,
|
session_id=session_id,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
tool_call_id="chat_stream",
|
tool_call_id="chat_stream", # Not a tool call, but needed for the model
|
||||||
tool_name="chat",
|
tool_name="chat",
|
||||||
turn_id=turn_id,
|
operation_id=operation_id,
|
||||||
)
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[TIMING] create_session completed in {(time.perf_counter() - session_create_start) * 1000:.1f}ms",
|
f"[TIMING] create_task completed in {(time.perf_counter() - task_create_start)*1000:.1f}ms",
|
||||||
extra={
|
extra={
|
||||||
"json_fields": {
|
"json_fields": {
|
||||||
**log_meta,
|
**log_meta,
|
||||||
"duration_ms": (time.perf_counter() - session_create_start) * 1000,
|
"duration_ms": (time.perf_counter() - task_create_start) * 1000,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# Per-turn stream is always fresh (unique turn_id), subscribe from beginning
|
# Background task that runs the AI generation independently of SSE connection
|
||||||
subscribe_from_id = "0-0"
|
async def run_ai_generation():
|
||||||
|
import time as time_module
|
||||||
|
|
||||||
await enqueue_copilot_turn(
|
gen_start_time = time_module.perf_counter()
|
||||||
session_id=session_id,
|
logger.info(
|
||||||
user_id=user_id,
|
f"[TIMING] run_ai_generation STARTED, task={task_id}, session={session_id}, user={user_id}",
|
||||||
message=request.message,
|
extra={"json_fields": log_meta},
|
||||||
turn_id=turn_id,
|
)
|
||||||
is_user_message=request.is_user_message,
|
first_chunk_time, ttfc = None, None
|
||||||
context=request.context,
|
chunk_count = 0
|
||||||
file_ids=sanitized_file_ids,
|
try:
|
||||||
)
|
async for chunk in chat_service.stream_chat_completion(
|
||||||
|
session_id,
|
||||||
|
request.message,
|
||||||
|
is_user_message=request.is_user_message,
|
||||||
|
user_id=user_id,
|
||||||
|
session=session, # Pass pre-fetched session to avoid double-fetch
|
||||||
|
context=request.context,
|
||||||
|
_task_id=task_id, # Pass task_id so service emits start with taskId for reconnection
|
||||||
|
):
|
||||||
|
chunk_count += 1
|
||||||
|
if first_chunk_time is None:
|
||||||
|
first_chunk_time = time_module.perf_counter()
|
||||||
|
ttfc = first_chunk_time - gen_start_time
|
||||||
|
logger.info(
|
||||||
|
f"[TIMING] FIRST AI CHUNK at {ttfc:.2f}s, type={type(chunk).__name__}",
|
||||||
|
extra={
|
||||||
|
"json_fields": {
|
||||||
|
**log_meta,
|
||||||
|
"chunk_type": type(chunk).__name__,
|
||||||
|
"time_to_first_chunk_ms": ttfc * 1000,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
# Write to Redis (subscribers will receive via XREAD)
|
||||||
|
await stream_registry.publish_chunk(task_id, chunk)
|
||||||
|
|
||||||
|
gen_end_time = time_module.perf_counter()
|
||||||
|
total_time = (gen_end_time - gen_start_time) * 1000
|
||||||
|
logger.info(
|
||||||
|
f"[TIMING] run_ai_generation FINISHED in {total_time/1000:.1f}s; "
|
||||||
|
f"task={task_id}, session={session_id}, "
|
||||||
|
f"ttfc={ttfc or -1:.2f}s, n_chunks={chunk_count}",
|
||||||
|
extra={
|
||||||
|
"json_fields": {
|
||||||
|
**log_meta,
|
||||||
|
"total_time_ms": total_time,
|
||||||
|
"time_to_first_chunk_ms": (
|
||||||
|
ttfc * 1000 if ttfc is not None else None
|
||||||
|
),
|
||||||
|
"n_chunks": chunk_count,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await stream_registry.mark_task_completed(task_id, "completed")
|
||||||
|
except Exception as e:
|
||||||
|
elapsed = time_module.perf_counter() - gen_start_time
|
||||||
|
logger.error(
|
||||||
|
f"[TIMING] run_ai_generation ERROR after {elapsed:.2f}s: {e}",
|
||||||
|
extra={
|
||||||
|
"json_fields": {
|
||||||
|
**log_meta,
|
||||||
|
"elapsed_ms": elapsed * 1000,
|
||||||
|
"error": str(e),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await stream_registry.mark_task_completed(task_id, "failed")
|
||||||
|
|
||||||
|
# Start the AI generation in a background task
|
||||||
|
bg_task = asyncio.create_task(run_ai_generation())
|
||||||
|
await stream_registry.set_task_asyncio_task(task_id, bg_task)
|
||||||
setup_time = (time.perf_counter() - stream_start_time) * 1000
|
setup_time = (time.perf_counter() - stream_start_time) * 1000
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[TIMING] Task enqueued to RabbitMQ, setup={setup_time:.1f}ms",
|
f"[TIMING] Background task started, setup={setup_time:.1f}ms",
|
||||||
extra={"json_fields": {**log_meta, "setup_time_ms": setup_time}},
|
extra={"json_fields": {**log_meta, "setup_time_ms": setup_time}},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -594,7 +421,7 @@ async def stream_chat_post(
|
|||||||
|
|
||||||
event_gen_start = time_module.perf_counter()
|
event_gen_start = time_module.perf_counter()
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[TIMING] event_generator STARTED, turn={turn_id}, session={session_id}, "
|
f"[TIMING] event_generator STARTED, task={task_id}, session={session_id}, "
|
||||||
f"user={user_id}",
|
f"user={user_id}",
|
||||||
extra={"json_fields": log_meta},
|
extra={"json_fields": log_meta},
|
||||||
)
|
)
|
||||||
@@ -602,12 +429,11 @@ async def stream_chat_post(
|
|||||||
first_chunk_yielded = False
|
first_chunk_yielded = False
|
||||||
chunks_yielded = 0
|
chunks_yielded = 0
|
||||||
try:
|
try:
|
||||||
# Subscribe from the position we captured before enqueuing
|
# Subscribe to the task stream (this replays existing messages + live updates)
|
||||||
# This avoids replaying old messages while catching all new ones
|
subscriber_queue = await stream_registry.subscribe_to_task(
|
||||||
subscriber_queue = await stream_registry.subscribe_to_session(
|
task_id=task_id,
|
||||||
session_id=session_id,
|
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
last_message_id=subscribe_from_id,
|
last_message_id="0-0", # Get all messages from the beginning
|
||||||
)
|
)
|
||||||
|
|
||||||
if subscriber_queue is None:
|
if subscriber_queue is None:
|
||||||
@@ -622,7 +448,7 @@ async def stream_chat_post(
|
|||||||
)
|
)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
chunk = await asyncio.wait_for(subscriber_queue.get(), timeout=10.0)
|
chunk = await asyncio.wait_for(subscriber_queue.get(), timeout=30.0)
|
||||||
chunks_yielded += 1
|
chunks_yielded += 1
|
||||||
|
|
||||||
if not first_chunk_yielded:
|
if not first_chunk_yielded:
|
||||||
@@ -680,29 +506,23 @@ async def stream_chat_post(
|
|||||||
"json_fields": {**log_meta, "elapsed_ms": elapsed, "error": str(e)}
|
"json_fields": {**log_meta, "elapsed_ms": elapsed, "error": str(e)}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
# Surface error to frontend so it doesn't appear stuck
|
|
||||||
yield StreamError(
|
|
||||||
errorText="An error occurred. Please try again.",
|
|
||||||
code="stream_error",
|
|
||||||
).to_sse()
|
|
||||||
yield StreamFinish().to_sse()
|
|
||||||
finally:
|
finally:
|
||||||
# Unsubscribe when client disconnects or stream ends
|
# Unsubscribe when client disconnects or stream ends to prevent resource leak
|
||||||
if subscriber_queue is not None:
|
if subscriber_queue is not None:
|
||||||
try:
|
try:
|
||||||
await stream_registry.unsubscribe_from_session(
|
await stream_registry.unsubscribe_from_task(
|
||||||
session_id, subscriber_queue
|
task_id, subscriber_queue
|
||||||
)
|
)
|
||||||
except Exception as unsub_err:
|
except Exception as unsub_err:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Error unsubscribing from session {session_id}: {unsub_err}",
|
f"Error unsubscribing from task {task_id}: {unsub_err}",
|
||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
# AI SDK protocol termination - always yield even if unsubscribe fails
|
# AI SDK protocol termination - always yield even if unsubscribe fails
|
||||||
total_time = time_module.perf_counter() - event_gen_start
|
total_time = time_module.perf_counter() - event_gen_start
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[TIMING] event_generator FINISHED in {total_time:.2f}s; "
|
f"[TIMING] event_generator FINISHED in {total_time:.2f}s; "
|
||||||
f"turn={turn_id}, session={session_id}, n_chunks={chunks_yielded}",
|
f"task={task_id}, session={session_id}, n_chunks={chunks_yielded}",
|
||||||
extra={
|
extra={
|
||||||
"json_fields": {
|
"json_fields": {
|
||||||
**log_meta,
|
**log_meta,
|
||||||
@@ -749,21 +569,17 @@ async def resume_session_stream(
|
|||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
active_session, last_message_id = await stream_registry.get_active_session(
|
active_task, _last_id = await stream_registry.get_active_task_for_session(
|
||||||
session_id, user_id
|
session_id, user_id
|
||||||
)
|
)
|
||||||
|
|
||||||
if not active_session:
|
if not active_task:
|
||||||
return Response(status_code=204)
|
return Response(status_code=204)
|
||||||
|
|
||||||
# Always replay from the beginning ("0-0") on resume.
|
subscriber_queue = await stream_registry.subscribe_to_task(
|
||||||
# We can't use last_message_id because it's the latest ID in the backend
|
task_id=active_task.task_id,
|
||||||
# stream, not the latest the frontend received — the gap causes lost
|
|
||||||
# messages. The frontend deduplicates replayed content.
|
|
||||||
subscriber_queue = await stream_registry.subscribe_to_session(
|
|
||||||
session_id=session_id,
|
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
last_message_id="0-0",
|
last_message_id="0-0", # Full replay so useChat rebuilds the message
|
||||||
)
|
)
|
||||||
|
|
||||||
if subscriber_queue is None:
|
if subscriber_queue is None:
|
||||||
@@ -775,7 +591,7 @@ async def resume_session_stream(
|
|||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
chunk = await asyncio.wait_for(subscriber_queue.get(), timeout=10.0)
|
chunk = await asyncio.wait_for(subscriber_queue.get(), timeout=30.0)
|
||||||
if chunk_count < 3:
|
if chunk_count < 3:
|
||||||
logger.info(
|
logger.info(
|
||||||
"Resume stream chunk",
|
"Resume stream chunk",
|
||||||
@@ -799,12 +615,12 @@ async def resume_session_stream(
|
|||||||
logger.error(f"Error in resume stream for session {session_id}: {e}")
|
logger.error(f"Error in resume stream for session {session_id}: {e}")
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
await stream_registry.unsubscribe_from_session(
|
await stream_registry.unsubscribe_from_task(
|
||||||
session_id, subscriber_queue
|
active_task.task_id, subscriber_queue
|
||||||
)
|
)
|
||||||
except Exception as unsub_err:
|
except Exception as unsub_err:
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Error unsubscribing from session {active_session.session_id}: {unsub_err}",
|
f"Error unsubscribing from task {active_task.task_id}: {unsub_err}",
|
||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -832,6 +648,7 @@ async def resume_session_stream(
|
|||||||
@router.patch(
|
@router.patch(
|
||||||
"/sessions/{session_id}/assign-user",
|
"/sessions/{session_id}/assign-user",
|
||||||
dependencies=[Security(auth.requires_user)],
|
dependencies=[Security(auth.requires_user)],
|
||||||
|
status_code=200,
|
||||||
)
|
)
|
||||||
async def session_assign_user(
|
async def session_assign_user(
|
||||||
session_id: str,
|
session_id: str,
|
||||||
@@ -854,34 +671,229 @@ async def session_assign_user(
|
|||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
# ========== Suggested Prompts ==========
|
# ========== Task Streaming (SSE Reconnection) ==========
|
||||||
|
|
||||||
|
|
||||||
class SuggestedPromptsResponse(BaseModel):
|
|
||||||
"""Response model for user-specific suggested prompts."""
|
|
||||||
|
|
||||||
prompts: list[str]
|
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/suggested-prompts",
|
"/tasks/{task_id}/stream",
|
||||||
dependencies=[Security(auth.requires_user)],
|
|
||||||
)
|
)
|
||||||
async def get_suggested_prompts(
|
async def stream_task(
|
||||||
user_id: Annotated[str, Security(auth.get_user_id)],
|
task_id: str,
|
||||||
) -> SuggestedPromptsResponse:
|
user_id: str | None = Depends(auth.get_user_id),
|
||||||
|
last_message_id: str = Query(
|
||||||
|
default="0-0",
|
||||||
|
description="Last Redis Stream message ID received (e.g., '1706540123456-0'). Use '0-0' for full replay.",
|
||||||
|
),
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Get LLM-generated suggested prompts for the authenticated user.
|
Reconnect to a long-running task's SSE stream.
|
||||||
|
|
||||||
Returns personalized quick-action prompts based on the user's
|
When a long-running operation (like agent generation) starts, the client
|
||||||
business understanding. Returns an empty list if no custom prompts
|
receives a task_id. If the connection drops, the client can reconnect
|
||||||
are available.
|
using this endpoint to resume receiving updates.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: The task ID from the operation_started response.
|
||||||
|
user_id: Authenticated user ID for ownership validation.
|
||||||
|
last_message_id: Last Redis Stream message ID received ("0-0" for full replay).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
StreamingResponse: SSE-formatted response chunks starting after last_message_id.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPException: 404 if task not found, 410 if task expired, 403 if access denied.
|
||||||
"""
|
"""
|
||||||
understanding = await get_business_understanding(user_id)
|
# Check task existence and expiry before subscribing
|
||||||
if understanding is None:
|
task, error_code = await stream_registry.get_task_with_expiry_info(task_id)
|
||||||
return SuggestedPromptsResponse(prompts=[])
|
|
||||||
|
|
||||||
return SuggestedPromptsResponse(prompts=understanding.suggested_prompts)
|
if error_code == "TASK_EXPIRED":
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=410,
|
||||||
|
detail={
|
||||||
|
"code": "TASK_EXPIRED",
|
||||||
|
"message": "This operation has expired. Please try again.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if error_code == "TASK_NOT_FOUND":
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail={
|
||||||
|
"code": "TASK_NOT_FOUND",
|
||||||
|
"message": f"Task {task_id} not found.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Validate ownership if task has an owner
|
||||||
|
if task and task.user_id and user_id != task.user_id:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=403,
|
||||||
|
detail={
|
||||||
|
"code": "ACCESS_DENIED",
|
||||||
|
"message": "You do not have access to this task.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get subscriber queue from stream registry
|
||||||
|
subscriber_queue = await stream_registry.subscribe_to_task(
|
||||||
|
task_id=task_id,
|
||||||
|
user_id=user_id,
|
||||||
|
last_message_id=last_message_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if subscriber_queue is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail={
|
||||||
|
"code": "TASK_NOT_FOUND",
|
||||||
|
"message": f"Task {task_id} not found or access denied.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
async def event_generator() -> AsyncGenerator[str, None]:
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
heartbeat_interval = 15.0 # Send heartbeat every 15 seconds
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
# Wait for next chunk with timeout for heartbeats
|
||||||
|
chunk = await asyncio.wait_for(
|
||||||
|
subscriber_queue.get(), timeout=heartbeat_interval
|
||||||
|
)
|
||||||
|
yield chunk.to_sse()
|
||||||
|
|
||||||
|
# Check for finish signal
|
||||||
|
if isinstance(chunk, StreamFinish):
|
||||||
|
break
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
# Send heartbeat to keep connection alive
|
||||||
|
yield StreamHeartbeat().to_sse()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in task stream {task_id}: {e}", exc_info=True)
|
||||||
|
finally:
|
||||||
|
# Unsubscribe when client disconnects or stream ends
|
||||||
|
try:
|
||||||
|
await stream_registry.unsubscribe_from_task(task_id, subscriber_queue)
|
||||||
|
except Exception as unsub_err:
|
||||||
|
logger.error(
|
||||||
|
f"Error unsubscribing from task {task_id}: {unsub_err}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
# AI SDK protocol termination - always yield even if unsubscribe fails
|
||||||
|
yield "data: [DONE]\n\n"
|
||||||
|
|
||||||
|
return StreamingResponse(
|
||||||
|
event_generator(),
|
||||||
|
media_type="text/event-stream",
|
||||||
|
headers={
|
||||||
|
"Cache-Control": "no-cache",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"X-Accel-Buffering": "no",
|
||||||
|
"x-vercel-ai-ui-message-stream": "v1",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/tasks/{task_id}",
|
||||||
|
)
|
||||||
|
async def get_task_status(
|
||||||
|
task_id: str,
|
||||||
|
user_id: str | None = Depends(auth.get_user_id),
|
||||||
|
) -> dict:
|
||||||
|
"""
|
||||||
|
Get the status of a long-running task.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
task_id: The task ID to check.
|
||||||
|
user_id: Authenticated user ID for ownership validation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: Task status including task_id, status, tool_name, and operation_id.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
NotFoundError: If task_id is not found or user doesn't have access.
|
||||||
|
"""
|
||||||
|
task = await stream_registry.get_task(task_id)
|
||||||
|
|
||||||
|
if task is None:
|
||||||
|
raise NotFoundError(f"Task {task_id} not found.")
|
||||||
|
|
||||||
|
# Validate ownership - if task has an owner, requester must match
|
||||||
|
if task.user_id and user_id != task.user_id:
|
||||||
|
raise NotFoundError(f"Task {task_id} not found.")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"task_id": task.task_id,
|
||||||
|
"session_id": task.session_id,
|
||||||
|
"status": task.status,
|
||||||
|
"tool_name": task.tool_name,
|
||||||
|
"operation_id": task.operation_id,
|
||||||
|
"created_at": task.created_at.isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ========== External Completion Webhook ==========
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/operations/{operation_id}/complete",
|
||||||
|
status_code=200,
|
||||||
|
)
|
||||||
|
async def complete_operation(
|
||||||
|
operation_id: str,
|
||||||
|
request: OperationCompleteRequest,
|
||||||
|
x_api_key: str | None = Header(default=None),
|
||||||
|
) -> dict:
|
||||||
|
"""
|
||||||
|
External completion webhook for long-running operations.
|
||||||
|
|
||||||
|
Called by Agent Generator (or other services) when an operation completes.
|
||||||
|
This triggers the stream registry to publish completion and continue LLM generation.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
operation_id: The operation ID to complete.
|
||||||
|
request: Completion payload with success status and result/error.
|
||||||
|
x_api_key: Internal API key for authentication.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: Status of the completion.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTPException: If API key is invalid or operation not found.
|
||||||
|
"""
|
||||||
|
# Validate internal API key - reject if not configured or invalid
|
||||||
|
if not config.internal_api_key:
|
||||||
|
logger.error(
|
||||||
|
"Operation complete webhook rejected: CHAT_INTERNAL_API_KEY not configured"
|
||||||
|
)
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=503,
|
||||||
|
detail="Webhook not available: internal API key not configured",
|
||||||
|
)
|
||||||
|
if x_api_key != config.internal_api_key:
|
||||||
|
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||||
|
|
||||||
|
# Find task by operation_id
|
||||||
|
task = await stream_registry.find_task_by_operation_id(operation_id)
|
||||||
|
if task is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=f"Operation {operation_id} not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"Received completion webhook for operation {operation_id} "
|
||||||
|
f"(task_id={task.task_id}, success={request.success})"
|
||||||
|
)
|
||||||
|
|
||||||
|
if request.success:
|
||||||
|
await process_operation_success(task, request.result)
|
||||||
|
else:
|
||||||
|
await process_operation_failure(task, request.error)
|
||||||
|
|
||||||
|
return {"status": "ok", "task_id": task.task_id}
|
||||||
|
|
||||||
|
|
||||||
# ========== Configuration ==========
|
# ========== Configuration ==========
|
||||||
@@ -958,14 +970,13 @@ ToolResponseUnion = (
|
|||||||
| AgentPreviewResponse
|
| AgentPreviewResponse
|
||||||
| AgentSavedResponse
|
| AgentSavedResponse
|
||||||
| ClarificationNeededResponse
|
| ClarificationNeededResponse
|
||||||
| SuggestedGoalResponse
|
|
||||||
| BlockListResponse
|
| BlockListResponse
|
||||||
| BlockDetailsResponse
|
|
||||||
| BlockOutputResponse
|
| BlockOutputResponse
|
||||||
| DocSearchResultsResponse
|
| DocSearchResultsResponse
|
||||||
| DocPageResponse
|
| DocPageResponse
|
||||||
| MCPToolsDiscoveredResponse
|
| OperationStartedResponse
|
||||||
| MCPToolOutputResponse
|
| OperationPendingResponse
|
||||||
|
| OperationInProgressResponse
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,310 +0,0 @@
|
|||||||
"""Tests for chat API routes: session title update, file attachment validation, and suggested prompts."""
|
|
||||||
|
|
||||||
from unittest.mock import AsyncMock, MagicMock
|
|
||||||
|
|
||||||
import fastapi
|
|
||||||
import fastapi.testclient
|
|
||||||
import pytest
|
|
||||||
import pytest_mock
|
|
||||||
|
|
||||||
from backend.api.features.chat import routes as chat_routes
|
|
||||||
|
|
||||||
app = fastapi.FastAPI()
|
|
||||||
app.include_router(chat_routes.router)
|
|
||||||
|
|
||||||
client = fastapi.testclient.TestClient(app)
|
|
||||||
|
|
||||||
TEST_USER_ID = "3e53486c-cf57-477e-ba2a-cb02dc828e1a"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def setup_app_auth(mock_jwt_user):
|
|
||||||
"""Setup auth overrides for all tests in this module"""
|
|
||||||
from autogpt_libs.auth.jwt_utils import get_jwt_payload
|
|
||||||
|
|
||||||
app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]
|
|
||||||
yield
|
|
||||||
app.dependency_overrides.clear()
|
|
||||||
|
|
||||||
|
|
||||||
def _mock_update_session_title(
|
|
||||||
mocker: pytest_mock.MockerFixture, *, success: bool = True
|
|
||||||
):
|
|
||||||
"""Mock update_session_title."""
|
|
||||||
return mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.update_session_title",
|
|
||||||
new_callable=AsyncMock,
|
|
||||||
return_value=success,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# ─── Update title: success ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_title_success(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
mock_update = _mock_update_session_title(mocker, success=True)
|
|
||||||
|
|
||||||
response = client.patch(
|
|
||||||
"/sessions/sess-1/title",
|
|
||||||
json={"title": "My project"},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json() == {"status": "ok"}
|
|
||||||
mock_update.assert_called_once_with("sess-1", test_user_id, "My project")
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_title_trims_whitespace(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
mock_update = _mock_update_session_title(mocker, success=True)
|
|
||||||
|
|
||||||
response = client.patch(
|
|
||||||
"/sessions/sess-1/title",
|
|
||||||
json={"title": " trimmed "},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
mock_update.assert_called_once_with("sess-1", test_user_id, "trimmed")
|
|
||||||
|
|
||||||
|
|
||||||
# ─── Update title: blank / whitespace-only → 422 ──────────────────────
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_title_blank_rejected(
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
"""Whitespace-only titles must be rejected before hitting the DB."""
|
|
||||||
response = client.patch(
|
|
||||||
"/sessions/sess-1/title",
|
|
||||||
json={"title": " "},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 422
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_title_empty_rejected(
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
response = client.patch(
|
|
||||||
"/sessions/sess-1/title",
|
|
||||||
json={"title": ""},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 422
|
|
||||||
|
|
||||||
|
|
||||||
# ─── Update title: session not found or wrong user → 404 ──────────────
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_title_not_found(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
_mock_update_session_title(mocker, success=False)
|
|
||||||
|
|
||||||
response = client.patch(
|
|
||||||
"/sessions/sess-1/title",
|
|
||||||
json={"title": "New name"},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 404
|
|
||||||
|
|
||||||
|
|
||||||
# ─── file_ids Pydantic validation ─────────────────────────────────────
|
|
||||||
|
|
||||||
|
|
||||||
def test_stream_chat_rejects_too_many_file_ids():
|
|
||||||
"""More than 20 file_ids should be rejected by Pydantic validation (422)."""
|
|
||||||
response = client.post(
|
|
||||||
"/sessions/sess-1/stream",
|
|
||||||
json={
|
|
||||||
"message": "hello",
|
|
||||||
"file_ids": [f"00000000-0000-0000-0000-{i:012d}" for i in range(21)],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert response.status_code == 422
|
|
||||||
|
|
||||||
|
|
||||||
def _mock_stream_internals(mocker: pytest_mock.MockFixture):
|
|
||||||
"""Mock the async internals of stream_chat_post so tests can exercise
|
|
||||||
validation and enrichment logic without needing Redis/RabbitMQ."""
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes._validate_and_get_session",
|
|
||||||
return_value=None,
|
|
||||||
)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.append_and_save_message",
|
|
||||||
return_value=None,
|
|
||||||
)
|
|
||||||
mock_registry = mocker.MagicMock()
|
|
||||||
mock_registry.create_session = mocker.AsyncMock(return_value=None)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.stream_registry",
|
|
||||||
mock_registry,
|
|
||||||
)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.enqueue_copilot_turn",
|
|
||||||
return_value=None,
|
|
||||||
)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.track_user_message",
|
|
||||||
return_value=None,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_stream_chat_accepts_20_file_ids(mocker: pytest_mock.MockFixture):
|
|
||||||
"""Exactly 20 file_ids should be accepted (not rejected by validation)."""
|
|
||||||
_mock_stream_internals(mocker)
|
|
||||||
# Patch workspace lookup as imported by the routes module
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.get_or_create_workspace",
|
|
||||||
return_value=type("W", (), {"id": "ws-1"})(),
|
|
||||||
)
|
|
||||||
mock_prisma = mocker.MagicMock()
|
|
||||||
mock_prisma.find_many = mocker.AsyncMock(return_value=[])
|
|
||||||
mocker.patch(
|
|
||||||
"prisma.models.UserWorkspaceFile.prisma",
|
|
||||||
return_value=mock_prisma,
|
|
||||||
)
|
|
||||||
|
|
||||||
response = client.post(
|
|
||||||
"/sessions/sess-1/stream",
|
|
||||||
json={
|
|
||||||
"message": "hello",
|
|
||||||
"file_ids": [f"00000000-0000-0000-0000-{i:012d}" for i in range(20)],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
# Should get past validation — 200 streaming response expected
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
|
|
||||||
# ─── UUID format filtering ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
|
|
||||||
def test_file_ids_filters_invalid_uuids(mocker: pytest_mock.MockFixture):
|
|
||||||
"""Non-UUID strings in file_ids should be silently filtered out
|
|
||||||
and NOT passed to the database query."""
|
|
||||||
_mock_stream_internals(mocker)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.get_or_create_workspace",
|
|
||||||
return_value=type("W", (), {"id": "ws-1"})(),
|
|
||||||
)
|
|
||||||
|
|
||||||
mock_prisma = mocker.MagicMock()
|
|
||||||
mock_prisma.find_many = mocker.AsyncMock(return_value=[])
|
|
||||||
mocker.patch(
|
|
||||||
"prisma.models.UserWorkspaceFile.prisma",
|
|
||||||
return_value=mock_prisma,
|
|
||||||
)
|
|
||||||
|
|
||||||
valid_id = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
|
|
||||||
client.post(
|
|
||||||
"/sessions/sess-1/stream",
|
|
||||||
json={
|
|
||||||
"message": "hello",
|
|
||||||
"file_ids": [
|
|
||||||
valid_id,
|
|
||||||
"not-a-uuid",
|
|
||||||
"../../../etc/passwd",
|
|
||||||
"",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
# The find_many call should only receive the one valid UUID
|
|
||||||
mock_prisma.find_many.assert_called_once()
|
|
||||||
call_kwargs = mock_prisma.find_many.call_args[1]
|
|
||||||
assert call_kwargs["where"]["id"]["in"] == [valid_id]
|
|
||||||
|
|
||||||
|
|
||||||
# ─── Cross-workspace file_ids ─────────────────────────────────────────
|
|
||||||
|
|
||||||
|
|
||||||
def test_file_ids_scoped_to_workspace(mocker: pytest_mock.MockFixture):
|
|
||||||
"""The batch query should scope to the user's workspace."""
|
|
||||||
_mock_stream_internals(mocker)
|
|
||||||
mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.get_or_create_workspace",
|
|
||||||
return_value=type("W", (), {"id": "my-workspace-id"})(),
|
|
||||||
)
|
|
||||||
|
|
||||||
mock_prisma = mocker.MagicMock()
|
|
||||||
mock_prisma.find_many = mocker.AsyncMock(return_value=[])
|
|
||||||
mocker.patch(
|
|
||||||
"prisma.models.UserWorkspaceFile.prisma",
|
|
||||||
return_value=mock_prisma,
|
|
||||||
)
|
|
||||||
|
|
||||||
fid = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
|
|
||||||
client.post(
|
|
||||||
"/sessions/sess-1/stream",
|
|
||||||
json={"message": "hi", "file_ids": [fid]},
|
|
||||||
)
|
|
||||||
|
|
||||||
call_kwargs = mock_prisma.find_many.call_args[1]
|
|
||||||
assert call_kwargs["where"]["workspaceId"] == "my-workspace-id"
|
|
||||||
assert call_kwargs["where"]["isDeleted"] is False
|
|
||||||
|
|
||||||
|
|
||||||
# ─── Suggested prompts endpoint ──────────────────────────────────────
|
|
||||||
|
|
||||||
|
|
||||||
def _mock_get_business_understanding(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
*,
|
|
||||||
return_value=None,
|
|
||||||
):
|
|
||||||
"""Mock get_business_understanding."""
|
|
||||||
return mocker.patch(
|
|
||||||
"backend.api.features.chat.routes.get_business_understanding",
|
|
||||||
new_callable=AsyncMock,
|
|
||||||
return_value=return_value,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_suggested_prompts_returns_prompts(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
"""User with understanding and prompts gets them back."""
|
|
||||||
mock_understanding = MagicMock()
|
|
||||||
mock_understanding.suggested_prompts = ["Do X", "Do Y", "Do Z"]
|
|
||||||
_mock_get_business_understanding(mocker, return_value=mock_understanding)
|
|
||||||
|
|
||||||
response = client.get("/suggested-prompts")
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json() == {"prompts": ["Do X", "Do Y", "Do Z"]}
|
|
||||||
|
|
||||||
|
|
||||||
def test_suggested_prompts_no_understanding(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
"""User with no understanding gets empty list."""
|
|
||||||
_mock_get_business_understanding(mocker, return_value=None)
|
|
||||||
|
|
||||||
response = client.get("/suggested-prompts")
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json() == {"prompts": []}
|
|
||||||
|
|
||||||
|
|
||||||
def test_suggested_prompts_empty_prompts(
|
|
||||||
mocker: pytest_mock.MockerFixture,
|
|
||||||
test_user_id: str,
|
|
||||||
) -> None:
|
|
||||||
"""User with understanding but no prompts gets empty list."""
|
|
||||||
mock_understanding = MagicMock()
|
|
||||||
mock_understanding.suggested_prompts = []
|
|
||||||
_mock_get_business_understanding(mocker, return_value=mock_understanding)
|
|
||||||
|
|
||||||
response = client.get("/suggested-prompts")
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert response.json() == {"prompts": []}
|
|
||||||
2062
autogpt_platform/backend/backend/api/features/chat/service.py
Normal file
2062
autogpt_platform/backend/backend/api/features/chat/service.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,82 @@
|
|||||||
|
import logging
|
||||||
|
from os import getenv
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from . import service as chat_service
|
||||||
|
from .model import create_chat_session, get_chat_session, upsert_chat_session
|
||||||
|
from .response_model import (
|
||||||
|
StreamError,
|
||||||
|
StreamFinish,
|
||||||
|
StreamTextDelta,
|
||||||
|
StreamToolOutputAvailable,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_stream_chat_completion(setup_test_user, test_user_id):
|
||||||
|
"""
|
||||||
|
Test the stream_chat_completion function.
|
||||||
|
"""
|
||||||
|
api_key: str | None = getenv("OPEN_ROUTER_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
return pytest.skip("OPEN_ROUTER_API_KEY is not set, skipping test")
|
||||||
|
|
||||||
|
session = await create_chat_session(test_user_id)
|
||||||
|
|
||||||
|
has_errors = False
|
||||||
|
has_ended = False
|
||||||
|
assistant_message = ""
|
||||||
|
async for chunk in chat_service.stream_chat_completion(
|
||||||
|
session.session_id, "Hello, how are you?", user_id=session.user_id
|
||||||
|
):
|
||||||
|
logger.info(chunk)
|
||||||
|
if isinstance(chunk, StreamError):
|
||||||
|
has_errors = True
|
||||||
|
if isinstance(chunk, StreamTextDelta):
|
||||||
|
assistant_message += chunk.delta
|
||||||
|
if isinstance(chunk, StreamFinish):
|
||||||
|
has_ended = True
|
||||||
|
|
||||||
|
assert has_ended, "Chat completion did not end"
|
||||||
|
assert not has_errors, "Error occurred while streaming chat completion"
|
||||||
|
assert assistant_message, "Assistant message is empty"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_stream_chat_completion_with_tool_calls(setup_test_user, test_user_id):
|
||||||
|
"""
|
||||||
|
Test the stream_chat_completion function.
|
||||||
|
"""
|
||||||
|
api_key: str | None = getenv("OPEN_ROUTER_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
return pytest.skip("OPEN_ROUTER_API_KEY is not set, skipping test")
|
||||||
|
|
||||||
|
session = await create_chat_session(test_user_id)
|
||||||
|
session = await upsert_chat_session(session)
|
||||||
|
|
||||||
|
has_errors = False
|
||||||
|
has_ended = False
|
||||||
|
had_tool_calls = False
|
||||||
|
async for chunk in chat_service.stream_chat_completion(
|
||||||
|
session.session_id,
|
||||||
|
"Please find me an agent that can help me with my business. Use the query 'moneny printing agent'",
|
||||||
|
user_id=session.user_id,
|
||||||
|
):
|
||||||
|
logger.info(chunk)
|
||||||
|
if isinstance(chunk, StreamError):
|
||||||
|
has_errors = True
|
||||||
|
|
||||||
|
if isinstance(chunk, StreamFinish):
|
||||||
|
has_ended = True
|
||||||
|
if isinstance(chunk, StreamToolOutputAvailable):
|
||||||
|
had_tool_calls = True
|
||||||
|
|
||||||
|
assert has_ended, "Chat completion did not end"
|
||||||
|
assert not has_errors, "Error occurred while streaming chat completion"
|
||||||
|
assert had_tool_calls, "Tool calls did not occur"
|
||||||
|
session = await get_chat_session(session.session_id)
|
||||||
|
assert session, "Session not found"
|
||||||
|
assert session.usage, "Usage is empty"
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,44 +1,24 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from openai.types.chat import ChatCompletionToolParam
|
from openai.types.chat import ChatCompletionToolParam
|
||||||
|
|
||||||
from backend.copilot.tracking import track_tool_called
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.api.features.chat.tracking import track_tool_called
|
||||||
|
|
||||||
from .add_understanding import AddUnderstandingTool
|
from .add_understanding import AddUnderstandingTool
|
||||||
from .agent_browser import BrowserActTool, BrowserNavigateTool, BrowserScreenshotTool
|
|
||||||
from .agent_output import AgentOutputTool
|
from .agent_output import AgentOutputTool
|
||||||
from .base import BaseTool
|
from .base import BaseTool
|
||||||
from .bash_exec import BashExecTool
|
|
||||||
from .connect_integration import ConnectIntegrationTool
|
|
||||||
from .continue_run_block import ContinueRunBlockTool
|
|
||||||
from .create_agent import CreateAgentTool
|
from .create_agent import CreateAgentTool
|
||||||
from .customize_agent import CustomizeAgentTool
|
from .customize_agent import CustomizeAgentTool
|
||||||
from .edit_agent import EditAgentTool
|
from .edit_agent import EditAgentTool
|
||||||
from .feature_requests import CreateFeatureRequestTool, SearchFeatureRequestsTool
|
|
||||||
from .find_agent import FindAgentTool
|
from .find_agent import FindAgentTool
|
||||||
from .find_block import FindBlockTool
|
from .find_block import FindBlockTool
|
||||||
from .find_library_agent import FindLibraryAgentTool
|
from .find_library_agent import FindLibraryAgentTool
|
||||||
from .fix_agent import FixAgentGraphTool
|
|
||||||
from .get_agent_building_guide import GetAgentBuildingGuideTool
|
|
||||||
from .get_doc_page import GetDocPageTool
|
from .get_doc_page import GetDocPageTool
|
||||||
from .get_mcp_guide import GetMCPGuideTool
|
|
||||||
from .manage_folders import (
|
|
||||||
CreateFolderTool,
|
|
||||||
DeleteFolderTool,
|
|
||||||
ListFoldersTool,
|
|
||||||
MoveAgentsToFolderTool,
|
|
||||||
MoveFolderTool,
|
|
||||||
UpdateFolderTool,
|
|
||||||
)
|
|
||||||
from .run_agent import RunAgentTool
|
from .run_agent import RunAgentTool
|
||||||
from .run_block import RunBlockTool
|
from .run_block import RunBlockTool
|
||||||
from .run_mcp_tool import RunMCPToolTool
|
|
||||||
from .search_docs import SearchDocsTool
|
from .search_docs import SearchDocsTool
|
||||||
from .validate_agent import ValidateAgentGraphTool
|
|
||||||
from .web_fetch import WebFetchTool
|
|
||||||
from .workspace_files import (
|
from .workspace_files import (
|
||||||
DeleteWorkspaceFileTool,
|
DeleteWorkspaceFileTool,
|
||||||
ListWorkspaceFilesTool,
|
ListWorkspaceFilesTool,
|
||||||
@@ -47,8 +27,7 @@ from .workspace_files import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.response_model import StreamToolOutputAvailable
|
||||||
from backend.copilot.response_model import StreamToolOutputAvailable
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -61,38 +40,11 @@ TOOL_REGISTRY: dict[str, BaseTool] = {
|
|||||||
"find_agent": FindAgentTool(),
|
"find_agent": FindAgentTool(),
|
||||||
"find_block": FindBlockTool(),
|
"find_block": FindBlockTool(),
|
||||||
"find_library_agent": FindLibraryAgentTool(),
|
"find_library_agent": FindLibraryAgentTool(),
|
||||||
# Folder management tools
|
|
||||||
"create_folder": CreateFolderTool(),
|
|
||||||
"list_folders": ListFoldersTool(),
|
|
||||||
"update_folder": UpdateFolderTool(),
|
|
||||||
"move_folder": MoveFolderTool(),
|
|
||||||
"delete_folder": DeleteFolderTool(),
|
|
||||||
"move_agents_to_folder": MoveAgentsToFolderTool(),
|
|
||||||
"run_agent": RunAgentTool(),
|
"run_agent": RunAgentTool(),
|
||||||
"run_block": RunBlockTool(),
|
"run_block": RunBlockTool(),
|
||||||
"continue_run_block": ContinueRunBlockTool(),
|
|
||||||
"run_mcp_tool": RunMCPToolTool(),
|
|
||||||
"get_mcp_guide": GetMCPGuideTool(),
|
|
||||||
"view_agent_output": AgentOutputTool(),
|
"view_agent_output": AgentOutputTool(),
|
||||||
"search_docs": SearchDocsTool(),
|
"search_docs": SearchDocsTool(),
|
||||||
"get_doc_page": GetDocPageTool(),
|
"get_doc_page": GetDocPageTool(),
|
||||||
"get_agent_building_guide": GetAgentBuildingGuideTool(),
|
|
||||||
# Web fetch for safe URL retrieval
|
|
||||||
"web_fetch": WebFetchTool(),
|
|
||||||
# Agent-browser multi-step automation (navigate, act, screenshot)
|
|
||||||
"browser_navigate": BrowserNavigateTool(),
|
|
||||||
"browser_act": BrowserActTool(),
|
|
||||||
"browser_screenshot": BrowserScreenshotTool(),
|
|
||||||
# Sandboxed code execution (bubblewrap)
|
|
||||||
"bash_exec": BashExecTool(),
|
|
||||||
"connect_integration": ConnectIntegrationTool(),
|
|
||||||
# Persistent workspace tools (cloud storage, survives across sessions)
|
|
||||||
# Feature request tools
|
|
||||||
"search_feature_requests": SearchFeatureRequestsTool(),
|
|
||||||
"create_feature_request": CreateFeatureRequestTool(),
|
|
||||||
# Agent generation tools (local validation/fixing)
|
|
||||||
"validate_agent_graph": ValidateAgentGraphTool(),
|
|
||||||
"fix_agent_graph": FixAgentGraphTool(),
|
|
||||||
# Workspace tools for CoPilot file operations
|
# Workspace tools for CoPilot file operations
|
||||||
"list_workspace_files": ListWorkspaceFilesTool(),
|
"list_workspace_files": ListWorkspaceFilesTool(),
|
||||||
"read_workspace_file": ReadWorkspaceFileTool(),
|
"read_workspace_file": ReadWorkspaceFileTool(),
|
||||||
@@ -104,17 +56,10 @@ TOOL_REGISTRY: dict[str, BaseTool] = {
|
|||||||
find_agent_tool = TOOL_REGISTRY["find_agent"]
|
find_agent_tool = TOOL_REGISTRY["find_agent"]
|
||||||
run_agent_tool = TOOL_REGISTRY["run_agent"]
|
run_agent_tool = TOOL_REGISTRY["run_agent"]
|
||||||
|
|
||||||
|
# Generated from registry for OpenAI API
|
||||||
def get_available_tools() -> list[ChatCompletionToolParam]:
|
tools: list[ChatCompletionToolParam] = [
|
||||||
"""Return OpenAI tool schemas for tools available in the current environment.
|
tool.as_openai_tool() for tool in TOOL_REGISTRY.values()
|
||||||
|
]
|
||||||
Called per-request so that env-var or binary availability is evaluated
|
|
||||||
fresh each time (e.g. browser_* tools are excluded when agent-browser
|
|
||||||
CLI is not installed).
|
|
||||||
"""
|
|
||||||
return [
|
|
||||||
tool.as_openai_tool() for tool in TOOL_REGISTRY.values() if tool.is_available
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def get_tool(tool_name: str) -> BaseTool | None:
|
def get_tool(tool_name: str) -> BaseTool | None:
|
||||||
@@ -1,46 +1,22 @@
|
|||||||
import logging
|
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_asyncio
|
|
||||||
from prisma.types import ProfileCreateInput
|
from prisma.types import ProfileCreateInput
|
||||||
from pydantic import SecretStr
|
from pydantic import SecretStr
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
from backend.api.features.store import db as store_db
|
from backend.api.features.store import db as store_db
|
||||||
from backend.blocks.firecrawl.scrape import FirecrawlScrapeBlock
|
from backend.blocks.firecrawl.scrape import FirecrawlScrapeBlock
|
||||||
from backend.blocks.io import AgentInputBlock, AgentOutputBlock
|
from backend.blocks.io import AgentInputBlock, AgentOutputBlock
|
||||||
from backend.blocks.llm import AITextGeneratorBlock
|
from backend.blocks.llm import AITextGeneratorBlock
|
||||||
from backend.copilot.model import ChatSession
|
|
||||||
from backend.data import db as db_module
|
|
||||||
from backend.data.db import prisma
|
from backend.data.db import prisma
|
||||||
from backend.data.graph import Graph, Link, Node, create_graph
|
from backend.data.graph import Graph, Link, Node, create_graph
|
||||||
from backend.data.model import APIKeyCredentials
|
from backend.data.model import APIKeyCredentials
|
||||||
from backend.data.user import get_or_create_user
|
from backend.data.user import get_or_create_user
|
||||||
from backend.integrations.credentials_store import IntegrationCredentialsStore
|
from backend.integrations.credentials_store import IntegrationCredentialsStore
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def _ensure_db_connected() -> None:
|
|
||||||
"""Ensure the Prisma connection is alive on the current event loop.
|
|
||||||
|
|
||||||
On Python 3.11, the httpx transport inside Prisma can reference a stale
|
|
||||||
(closed) event loop when session-scoped async fixtures are evaluated long
|
|
||||||
after the initial ``server`` fixture connected Prisma. A cheap health-check
|
|
||||||
followed by a reconnect fixes this without affecting other fixtures.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
await prisma.query_raw("SELECT 1")
|
|
||||||
except Exception:
|
|
||||||
_logger.info("Prisma connection stale – reconnecting")
|
|
||||||
try:
|
|
||||||
await db_module.disconnect()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
await db_module.connect()
|
|
||||||
|
|
||||||
|
|
||||||
def make_session(user_id: str):
|
def make_session(user_id: str):
|
||||||
return ChatSession(
|
return ChatSession(
|
||||||
@@ -55,19 +31,15 @@ def make_session(user_id: str):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture(scope="session", loop_scope="session")
|
@pytest.fixture(scope="session")
|
||||||
async def setup_test_data(server):
|
async def setup_test_data():
|
||||||
"""
|
"""
|
||||||
Set up test data for run_agent tests:
|
Set up test data for run_agent tests:
|
||||||
1. Create a test user
|
1. Create a test user
|
||||||
2. Create a test graph (agent input -> agent output)
|
2. Create a test graph (agent input -> agent output)
|
||||||
3. Create a store listing and store listing version
|
3. Create a store listing and store listing version
|
||||||
4. Approve the store listing version
|
4. Approve the store listing version
|
||||||
|
|
||||||
Depends on ``server`` to ensure Prisma is connected.
|
|
||||||
"""
|
"""
|
||||||
await _ensure_db_connected()
|
|
||||||
|
|
||||||
# 1. Create a test user
|
# 1. Create a test user
|
||||||
user_data = {
|
user_data = {
|
||||||
"sub": f"test-user-{uuid.uuid4()}",
|
"sub": f"test-user-{uuid.uuid4()}",
|
||||||
@@ -151,8 +123,8 @@ async def setup_test_data(server):
|
|||||||
unique_slug = f"test-agent-{str(uuid.uuid4())[:8]}"
|
unique_slug = f"test-agent-{str(uuid.uuid4())[:8]}"
|
||||||
store_submission = await store_db.create_store_submission(
|
store_submission = await store_db.create_store_submission(
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
graph_id=created_graph.id,
|
agent_id=created_graph.id,
|
||||||
graph_version=created_graph.version,
|
agent_version=created_graph.version,
|
||||||
slug=unique_slug,
|
slug=unique_slug,
|
||||||
name="Test Agent",
|
name="Test Agent",
|
||||||
description="A simple test agent",
|
description="A simple test agent",
|
||||||
@@ -161,10 +133,10 @@ async def setup_test_data(server):
|
|||||||
image_urls=["https://example.com/image.jpg"],
|
image_urls=["https://example.com/image.jpg"],
|
||||||
)
|
)
|
||||||
|
|
||||||
assert store_submission.listing_version_id is not None
|
assert store_submission.store_listing_version_id is not None
|
||||||
# 4. Approve the store listing version
|
# 4. Approve the store listing version
|
||||||
await store_db.review_store_submission(
|
await store_db.review_store_submission(
|
||||||
store_listing_version_id=store_submission.listing_version_id,
|
store_listing_version_id=store_submission.store_listing_version_id,
|
||||||
is_approved=True,
|
is_approved=True,
|
||||||
external_comments="Approved for testing",
|
external_comments="Approved for testing",
|
||||||
internal_comments="Test approval",
|
internal_comments="Test approval",
|
||||||
@@ -178,19 +150,15 @@ async def setup_test_data(server):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture(scope="session", loop_scope="session")
|
@pytest.fixture(scope="session")
|
||||||
async def setup_llm_test_data(server):
|
async def setup_llm_test_data():
|
||||||
"""
|
"""
|
||||||
Set up test data for LLM agent tests:
|
Set up test data for LLM agent tests:
|
||||||
1. Create a test user
|
1. Create a test user
|
||||||
2. Create test OpenAI credentials for the user
|
2. Create test OpenAI credentials for the user
|
||||||
3. Create a test graph with input -> LLM block -> output
|
3. Create a test graph with input -> LLM block -> output
|
||||||
4. Create and approve a store listing
|
4. Create and approve a store listing
|
||||||
|
|
||||||
Depends on ``server`` to ensure Prisma is connected.
|
|
||||||
"""
|
"""
|
||||||
await _ensure_db_connected()
|
|
||||||
|
|
||||||
key = getenv("OPENAI_API_KEY")
|
key = getenv("OPENAI_API_KEY")
|
||||||
if not key:
|
if not key:
|
||||||
return pytest.skip("OPENAI_API_KEY is not set")
|
return pytest.skip("OPENAI_API_KEY is not set")
|
||||||
@@ -321,8 +289,8 @@ async def setup_llm_test_data(server):
|
|||||||
unique_slug = f"llm-test-agent-{str(uuid.uuid4())[:8]}"
|
unique_slug = f"llm-test-agent-{str(uuid.uuid4())[:8]}"
|
||||||
store_submission = await store_db.create_store_submission(
|
store_submission = await store_db.create_store_submission(
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
graph_id=created_graph.id,
|
agent_id=created_graph.id,
|
||||||
graph_version=created_graph.version,
|
agent_version=created_graph.version,
|
||||||
slug=unique_slug,
|
slug=unique_slug,
|
||||||
name="LLM Test Agent",
|
name="LLM Test Agent",
|
||||||
description="An agent with LLM capabilities",
|
description="An agent with LLM capabilities",
|
||||||
@@ -330,9 +298,9 @@ async def setup_llm_test_data(server):
|
|||||||
categories=["testing", "ai"],
|
categories=["testing", "ai"],
|
||||||
image_urls=["https://example.com/image.jpg"],
|
image_urls=["https://example.com/image.jpg"],
|
||||||
)
|
)
|
||||||
assert store_submission.listing_version_id is not None
|
assert store_submission.store_listing_version_id is not None
|
||||||
await store_db.review_store_submission(
|
await store_db.review_store_submission(
|
||||||
store_listing_version_id=store_submission.listing_version_id,
|
store_listing_version_id=store_submission.store_listing_version_id,
|
||||||
is_approved=True,
|
is_approved=True,
|
||||||
external_comments="Approved for testing",
|
external_comments="Approved for testing",
|
||||||
internal_comments="Test approval for LLM agent",
|
internal_comments="Test approval for LLM agent",
|
||||||
@@ -347,18 +315,14 @@ async def setup_llm_test_data(server):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture(scope="session", loop_scope="session")
|
@pytest.fixture(scope="session")
|
||||||
async def setup_firecrawl_test_data(server):
|
async def setup_firecrawl_test_data():
|
||||||
"""
|
"""
|
||||||
Set up test data for Firecrawl agent tests (missing credentials scenario):
|
Set up test data for Firecrawl agent tests (missing credentials scenario):
|
||||||
1. Create a test user (WITHOUT Firecrawl credentials)
|
1. Create a test user (WITHOUT Firecrawl credentials)
|
||||||
2. Create a test graph with input -> Firecrawl block -> output
|
2. Create a test graph with input -> Firecrawl block -> output
|
||||||
3. Create and approve a store listing
|
3. Create and approve a store listing
|
||||||
|
|
||||||
Depends on ``server`` to ensure Prisma is connected.
|
|
||||||
"""
|
"""
|
||||||
await _ensure_db_connected()
|
|
||||||
|
|
||||||
# 1. Create a test user
|
# 1. Create a test user
|
||||||
user_data = {
|
user_data = {
|
||||||
"sub": f"test-user-{uuid.uuid4()}",
|
"sub": f"test-user-{uuid.uuid4()}",
|
||||||
@@ -476,8 +440,8 @@ async def setup_firecrawl_test_data(server):
|
|||||||
unique_slug = f"firecrawl-test-agent-{str(uuid.uuid4())[:8]}"
|
unique_slug = f"firecrawl-test-agent-{str(uuid.uuid4())[:8]}"
|
||||||
store_submission = await store_db.create_store_submission(
|
store_submission = await store_db.create_store_submission(
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
graph_id=created_graph.id,
|
agent_id=created_graph.id,
|
||||||
graph_version=created_graph.version,
|
agent_version=created_graph.version,
|
||||||
slug=unique_slug,
|
slug=unique_slug,
|
||||||
name="Firecrawl Test Agent",
|
name="Firecrawl Test Agent",
|
||||||
description="An agent with Firecrawl integration (no credentials)",
|
description="An agent with Firecrawl integration (no credentials)",
|
||||||
@@ -485,9 +449,9 @@ async def setup_firecrawl_test_data(server):
|
|||||||
categories=["testing", "scraping"],
|
categories=["testing", "scraping"],
|
||||||
image_urls=["https://example.com/image.jpg"],
|
image_urls=["https://example.com/image.jpg"],
|
||||||
)
|
)
|
||||||
assert store_submission.listing_version_id is not None
|
assert store_submission.store_listing_version_id is not None
|
||||||
await store_db.review_store_submission(
|
await store_db.review_store_submission(
|
||||||
store_listing_version_id=store_submission.listing_version_id,
|
store_listing_version_id=store_submission.store_listing_version_id,
|
||||||
is_approved=True,
|
is_approved=True,
|
||||||
external_comments="Approved for testing",
|
external_comments="Approved for testing",
|
||||||
internal_comments="Test approval for Firecrawl agent",
|
internal_comments="Test approval for Firecrawl agent",
|
||||||
@@ -3,9 +3,11 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
from backend.data.db_accessors import understanding_db
|
from backend.data.understanding import (
|
||||||
from backend.data.understanding import BusinessUnderstandingInput
|
BusinessUnderstandingInput,
|
||||||
|
upsert_business_understanding,
|
||||||
|
)
|
||||||
|
|
||||||
from .base import BaseTool
|
from .base import BaseTool
|
||||||
from .models import ErrorResponse, ToolResponseBase, UnderstandingUpdatedResponse
|
from .models import ErrorResponse, ToolResponseBase, UnderstandingUpdatedResponse
|
||||||
@@ -97,9 +99,7 @@ and automations for the user's specific needs."""
|
|||||||
]
|
]
|
||||||
|
|
||||||
# Upsert with merge
|
# Upsert with merge
|
||||||
understanding = await understanding_db().upsert_business_understanding(
|
understanding = await upsert_business_understanding(user_id, input_data)
|
||||||
user_id, input_data
|
|
||||||
)
|
|
||||||
|
|
||||||
# Build current understanding summary (filter out empty values)
|
# Build current understanding summary (filter out empty values)
|
||||||
current_understanding = {
|
current_understanding = {
|
||||||
@@ -1,20 +1,24 @@
|
|||||||
"""Agent generator package - Creates agents from natural language."""
|
"""Agent generator package - Creates agents from natural language."""
|
||||||
|
|
||||||
from .core import (
|
from .core import (
|
||||||
|
AgentGeneratorNotConfiguredError,
|
||||||
AgentJsonValidationError,
|
AgentJsonValidationError,
|
||||||
AgentSummary,
|
AgentSummary,
|
||||||
DecompositionResult,
|
DecompositionResult,
|
||||||
DecompositionStep,
|
DecompositionStep,
|
||||||
LibraryAgentSummary,
|
LibraryAgentSummary,
|
||||||
MarketplaceAgentSummary,
|
MarketplaceAgentSummary,
|
||||||
|
customize_template,
|
||||||
|
decompose_goal,
|
||||||
enrich_library_agents_from_steps,
|
enrich_library_agents_from_steps,
|
||||||
extract_search_terms_from_steps,
|
extract_search_terms_from_steps,
|
||||||
extract_uuids_from_text,
|
extract_uuids_from_text,
|
||||||
|
generate_agent,
|
||||||
|
generate_agent_patch,
|
||||||
get_agent_as_json,
|
get_agent_as_json,
|
||||||
get_all_relevant_agents_for_generation,
|
get_all_relevant_agents_for_generation,
|
||||||
get_library_agent_by_graph_id,
|
get_library_agent_by_graph_id,
|
||||||
get_library_agent_by_id,
|
get_library_agent_by_id,
|
||||||
get_library_agents_by_ids,
|
|
||||||
get_library_agents_for_generation,
|
get_library_agents_for_generation,
|
||||||
graph_to_json,
|
graph_to_json,
|
||||||
json_to_graph,
|
json_to_graph,
|
||||||
@@ -22,28 +26,33 @@ from .core import (
|
|||||||
search_marketplace_agents_for_generation,
|
search_marketplace_agents_for_generation,
|
||||||
)
|
)
|
||||||
from .errors import get_user_message_for_error
|
from .errors import get_user_message_for_error
|
||||||
from .validation import AgentFixer, AgentValidator
|
from .service import health_check as check_external_service_health
|
||||||
|
from .service import is_external_service_configured
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"AgentFixer",
|
"AgentGeneratorNotConfiguredError",
|
||||||
"AgentValidator",
|
|
||||||
"AgentJsonValidationError",
|
"AgentJsonValidationError",
|
||||||
"AgentSummary",
|
"AgentSummary",
|
||||||
"DecompositionResult",
|
"DecompositionResult",
|
||||||
"DecompositionStep",
|
"DecompositionStep",
|
||||||
"LibraryAgentSummary",
|
"LibraryAgentSummary",
|
||||||
"MarketplaceAgentSummary",
|
"MarketplaceAgentSummary",
|
||||||
|
"check_external_service_health",
|
||||||
|
"customize_template",
|
||||||
|
"decompose_goal",
|
||||||
"enrich_library_agents_from_steps",
|
"enrich_library_agents_from_steps",
|
||||||
"extract_search_terms_from_steps",
|
"extract_search_terms_from_steps",
|
||||||
"extract_uuids_from_text",
|
"extract_uuids_from_text",
|
||||||
|
"generate_agent",
|
||||||
|
"generate_agent_patch",
|
||||||
"get_agent_as_json",
|
"get_agent_as_json",
|
||||||
"get_all_relevant_agents_for_generation",
|
"get_all_relevant_agents_for_generation",
|
||||||
"get_library_agent_by_graph_id",
|
"get_library_agent_by_graph_id",
|
||||||
"get_library_agent_by_id",
|
"get_library_agent_by_id",
|
||||||
"get_library_agents_by_ids",
|
|
||||||
"get_library_agents_for_generation",
|
"get_library_agents_for_generation",
|
||||||
"get_user_message_for_error",
|
"get_user_message_for_error",
|
||||||
"graph_to_json",
|
"graph_to_json",
|
||||||
|
"is_external_service_configured",
|
||||||
"json_to_graph",
|
"json_to_graph",
|
||||||
"save_agent_to_library",
|
"save_agent_to_library",
|
||||||
"search_marketplace_agents_for_generation",
|
"search_marketplace_agents_for_generation",
|
||||||
@@ -3,14 +3,20 @@
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import uuid
|
import uuid
|
||||||
from collections.abc import Sequence
|
|
||||||
from typing import Any, NotRequired, TypedDict
|
from typing import Any, NotRequired, TypedDict
|
||||||
|
|
||||||
from backend.data.db_accessors import graph_db, library_db, store_db
|
from backend.api.features.library import db as library_db
|
||||||
from backend.data.graph import Graph, Link, Node
|
from backend.api.features.store import db as store_db
|
||||||
|
from backend.data.graph import Graph, Link, Node, get_graph, get_store_listed_graphs
|
||||||
from backend.util.exceptions import DatabaseError, NotFoundError
|
from backend.util.exceptions import DatabaseError, NotFoundError
|
||||||
|
|
||||||
from .helpers import UUID_RE_STR
|
from .service import (
|
||||||
|
customize_template_external,
|
||||||
|
decompose_goal_external,
|
||||||
|
generate_agent_external,
|
||||||
|
generate_agent_patch_external,
|
||||||
|
is_external_service_configured,
|
||||||
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -72,7 +78,38 @@ class DecompositionResult(TypedDict, total=False):
|
|||||||
AgentSummary = LibraryAgentSummary | MarketplaceAgentSummary | dict[str, Any]
|
AgentSummary = LibraryAgentSummary | MarketplaceAgentSummary | dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
_UUID_PATTERN = re.compile(UUID_RE_STR, re.IGNORECASE)
|
def _to_dict_list(
|
||||||
|
agents: list[AgentSummary] | list[dict[str, Any]] | None,
|
||||||
|
) -> list[dict[str, Any]] | None:
|
||||||
|
"""Convert typed agent summaries to plain dicts for external service calls."""
|
||||||
|
if agents is None:
|
||||||
|
return None
|
||||||
|
return [dict(a) for a in agents]
|
||||||
|
|
||||||
|
|
||||||
|
class AgentGeneratorNotConfiguredError(Exception):
|
||||||
|
"""Raised when the external Agent Generator service is not configured."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _check_service_configured() -> None:
|
||||||
|
"""Check if the external Agent Generator service is configured.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AgentGeneratorNotConfiguredError: If the service is not configured.
|
||||||
|
"""
|
||||||
|
if not is_external_service_configured():
|
||||||
|
raise AgentGeneratorNotConfiguredError(
|
||||||
|
"Agent Generator service is not configured. "
|
||||||
|
"Set AGENTGENERATOR_HOST environment variable to enable agent generation."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_UUID_PATTERN = re.compile(
|
||||||
|
r"[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def extract_uuids_from_text(text: str) -> list[str]:
|
def extract_uuids_from_text(text: str) -> list[str]:
|
||||||
@@ -108,9 +145,8 @@ async def get_library_agent_by_id(
|
|||||||
Returns:
|
Returns:
|
||||||
LibraryAgentSummary if found, None otherwise
|
LibraryAgentSummary if found, None otherwise
|
||||||
"""
|
"""
|
||||||
db = library_db()
|
|
||||||
try:
|
try:
|
||||||
agent = await db.get_library_agent_by_graph_id(user_id, agent_id)
|
agent = await library_db.get_library_agent_by_graph_id(user_id, agent_id)
|
||||||
if agent:
|
if agent:
|
||||||
logger.debug(f"Found library agent by graph_id: {agent.name}")
|
logger.debug(f"Found library agent by graph_id: {agent.name}")
|
||||||
return LibraryAgentSummary(
|
return LibraryAgentSummary(
|
||||||
@@ -127,7 +163,7 @@ async def get_library_agent_by_id(
|
|||||||
logger.debug(f"Could not fetch library agent by graph_id {agent_id}: {e}")
|
logger.debug(f"Could not fetch library agent by graph_id {agent_id}: {e}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
agent = await db.get_library_agent(agent_id, user_id)
|
agent = await library_db.get_library_agent(agent_id, user_id)
|
||||||
if agent:
|
if agent:
|
||||||
logger.debug(f"Found library agent by library_id: {agent.name}")
|
logger.debug(f"Found library agent by library_id: {agent.name}")
|
||||||
return LibraryAgentSummary(
|
return LibraryAgentSummary(
|
||||||
@@ -154,36 +190,6 @@ async def get_library_agent_by_id(
|
|||||||
get_library_agent_by_graph_id = get_library_agent_by_id
|
get_library_agent_by_graph_id = get_library_agent_by_id
|
||||||
|
|
||||||
|
|
||||||
async def get_library_agents_by_ids(
|
|
||||||
user_id: str,
|
|
||||||
agent_ids: list[str],
|
|
||||||
) -> list[LibraryAgentSummary]:
|
|
||||||
"""Fetch multiple library agents by their IDs.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
user_id: The user ID
|
|
||||||
agent_ids: List of agent IDs (can be graph_ids or library agent IDs)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of LibraryAgentSummary for found agents (silently skips not found)
|
|
||||||
"""
|
|
||||||
agents: list[LibraryAgentSummary] = []
|
|
||||||
for agent_id in agent_ids:
|
|
||||||
try:
|
|
||||||
agent = await get_library_agent_by_id(user_id, agent_id)
|
|
||||||
if agent:
|
|
||||||
agents.append(agent)
|
|
||||||
logger.debug(f"Fetched library agent by ID: {agent['name']}")
|
|
||||||
else:
|
|
||||||
logger.warning(f"Library agent not found for ID: {agent_id}")
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"Failed to fetch library agent {agent_id}: {e}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
logger.info(f"Fetched {len(agents)}/{len(agent_ids)} library agents by ID")
|
|
||||||
return agents
|
|
||||||
|
|
||||||
|
|
||||||
async def get_library_agents_for_generation(
|
async def get_library_agents_for_generation(
|
||||||
user_id: str,
|
user_id: str,
|
||||||
search_query: str | None = None,
|
search_query: str | None = None,
|
||||||
@@ -208,17 +214,10 @@ async def get_library_agents_for_generation(
|
|||||||
Returns:
|
Returns:
|
||||||
List of LibraryAgentSummary with schemas and recent executions for sub-agent composition
|
List of LibraryAgentSummary with schemas and recent executions for sub-agent composition
|
||||||
"""
|
"""
|
||||||
search_term = search_query.strip() if search_query else None
|
|
||||||
if search_term and len(search_term) > 100:
|
|
||||||
raise ValueError(
|
|
||||||
f"Search query is too long ({len(search_term)} chars, max 100). "
|
|
||||||
f"Please use a shorter, more specific search term."
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = await library_db().list_library_agents(
|
response = await library_db.list_library_agents(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
search_term=search_term,
|
search_term=search_query,
|
||||||
page=1,
|
page=1,
|
||||||
page_size=max_results,
|
page_size=max_results,
|
||||||
include_executions=True,
|
include_executions=True,
|
||||||
@@ -272,16 +271,9 @@ async def search_marketplace_agents_for_generation(
|
|||||||
Returns:
|
Returns:
|
||||||
List of LibraryAgentSummary with full input/output schemas
|
List of LibraryAgentSummary with full input/output schemas
|
||||||
"""
|
"""
|
||||||
search_term = search_query.strip()
|
|
||||||
if len(search_term) > 100:
|
|
||||||
raise ValueError(
|
|
||||||
f"Search query is too long ({len(search_term)} chars, max 100). "
|
|
||||||
f"Please use a shorter, more specific search term."
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = await store_db().get_store_agents(
|
response = await store_db.get_store_agents(
|
||||||
search_query=search_term,
|
search_query=search_query,
|
||||||
page=1,
|
page=1,
|
||||||
page_size=max_results,
|
page_size=max_results,
|
||||||
)
|
)
|
||||||
@@ -294,7 +286,7 @@ async def search_marketplace_agents_for_generation(
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
graph_ids = [agent.agent_graph_id for agent in agents_with_graphs]
|
graph_ids = [agent.agent_graph_id for agent in agents_with_graphs]
|
||||||
graphs = await graph_db().get_store_listed_graphs(graph_ids)
|
graphs = await get_store_listed_graphs(*graph_ids)
|
||||||
|
|
||||||
results: list[LibraryAgentSummary] = []
|
results: list[LibraryAgentSummary] = []
|
||||||
for agent in agents_with_graphs:
|
for agent in agents_with_graphs:
|
||||||
@@ -432,7 +424,7 @@ def extract_search_terms_from_steps(
|
|||||||
async def enrich_library_agents_from_steps(
|
async def enrich_library_agents_from_steps(
|
||||||
user_id: str,
|
user_id: str,
|
||||||
decomposition_result: DecompositionResult | dict[str, Any],
|
decomposition_result: DecompositionResult | dict[str, Any],
|
||||||
existing_agents: Sequence[AgentSummary] | Sequence[dict[str, Any]],
|
existing_agents: list[AgentSummary] | list[dict[str, Any]],
|
||||||
exclude_graph_id: str | None = None,
|
exclude_graph_id: str | None = None,
|
||||||
include_marketplace: bool = True,
|
include_marketplace: bool = True,
|
||||||
max_additional_results: int = 10,
|
max_additional_results: int = 10,
|
||||||
@@ -456,7 +448,7 @@ async def enrich_library_agents_from_steps(
|
|||||||
search_terms = extract_search_terms_from_steps(decomposition_result)
|
search_terms = extract_search_terms_from_steps(decomposition_result)
|
||||||
|
|
||||||
if not search_terms:
|
if not search_terms:
|
||||||
return list(existing_agents)
|
return existing_agents
|
||||||
|
|
||||||
existing_ids: set[str] = set()
|
existing_ids: set[str] = set()
|
||||||
existing_names: set[str] = set()
|
existing_names: set[str] = set()
|
||||||
@@ -516,6 +508,79 @@ async def enrich_library_agents_from_steps(
|
|||||||
return all_agents
|
return all_agents
|
||||||
|
|
||||||
|
|
||||||
|
async def decompose_goal(
|
||||||
|
description: str,
|
||||||
|
context: str = "",
|
||||||
|
library_agents: list[AgentSummary] | None = None,
|
||||||
|
) -> DecompositionResult | None:
|
||||||
|
"""Break down a goal into steps or return clarifying questions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
description: Natural language goal description
|
||||||
|
context: Additional context (e.g., answers to previous questions)
|
||||||
|
library_agents: User's library agents available for sub-agent composition
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
DecompositionResult with either:
|
||||||
|
- {"type": "clarifying_questions", "questions": [...]}
|
||||||
|
- {"type": "instructions", "steps": [...]}
|
||||||
|
Or None on error
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AgentGeneratorNotConfiguredError: If the external service is not configured.
|
||||||
|
"""
|
||||||
|
_check_service_configured()
|
||||||
|
logger.info("Calling external Agent Generator service for decompose_goal")
|
||||||
|
result = await decompose_goal_external(
|
||||||
|
description, context, _to_dict_list(library_agents)
|
||||||
|
)
|
||||||
|
return result # type: ignore[return-value]
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_agent(
|
||||||
|
instructions: DecompositionResult | dict[str, Any],
|
||||||
|
library_agents: list[AgentSummary] | list[dict[str, Any]] | None = None,
|
||||||
|
operation_id: str | None = None,
|
||||||
|
task_id: str | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Generate agent JSON from instructions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
instructions: Structured instructions from decompose_goal
|
||||||
|
library_agents: User's library agents available for sub-agent composition
|
||||||
|
operation_id: Operation ID for async processing (enables Redis Streams
|
||||||
|
completion notification)
|
||||||
|
task_id: Task ID for async processing (enables Redis Streams persistence
|
||||||
|
and SSE delivery)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Agent JSON dict, {"status": "accepted"} for async, error dict {"type": "error", ...}, or None on error
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AgentGeneratorNotConfiguredError: If the external service is not configured.
|
||||||
|
"""
|
||||||
|
_check_service_configured()
|
||||||
|
logger.info("Calling external Agent Generator service for generate_agent")
|
||||||
|
result = await generate_agent_external(
|
||||||
|
dict(instructions), _to_dict_list(library_agents), operation_id, task_id
|
||||||
|
)
|
||||||
|
|
||||||
|
# Don't modify async response
|
||||||
|
if result and result.get("status") == "accepted":
|
||||||
|
return result
|
||||||
|
|
||||||
|
if result:
|
||||||
|
if isinstance(result, dict) and result.get("type") == "error":
|
||||||
|
return result
|
||||||
|
if "id" not in result:
|
||||||
|
result["id"] = str(uuid.uuid4())
|
||||||
|
if "version" not in result:
|
||||||
|
result["version"] = 1
|
||||||
|
if "is_active" not in result:
|
||||||
|
result["is_active"] = True
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class AgentJsonValidationError(Exception):
|
class AgentJsonValidationError(Exception):
|
||||||
"""Raised when agent JSON is invalid or missing required fields."""
|
"""Raised when agent JSON is invalid or missing required fields."""
|
||||||
|
|
||||||
@@ -595,10 +660,7 @@ def json_to_graph(agent_json: dict[str, Any]) -> Graph:
|
|||||||
|
|
||||||
|
|
||||||
async def save_agent_to_library(
|
async def save_agent_to_library(
|
||||||
agent_json: dict[str, Any],
|
agent_json: dict[str, Any], user_id: str, is_update: bool = False
|
||||||
user_id: str,
|
|
||||||
is_update: bool = False,
|
|
||||||
folder_id: str | None = None,
|
|
||||||
) -> tuple[Graph, Any]:
|
) -> tuple[Graph, Any]:
|
||||||
"""Save agent to database and user's library.
|
"""Save agent to database and user's library.
|
||||||
|
|
||||||
@@ -606,16 +668,14 @@ async def save_agent_to_library(
|
|||||||
agent_json: Agent JSON dict
|
agent_json: Agent JSON dict
|
||||||
user_id: User ID
|
user_id: User ID
|
||||||
is_update: Whether this is an update to an existing agent
|
is_update: Whether this is an update to an existing agent
|
||||||
folder_id: Optional folder ID to place the agent in
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple of (created Graph, LibraryAgent)
|
Tuple of (created Graph, LibraryAgent)
|
||||||
"""
|
"""
|
||||||
graph = json_to_graph(agent_json)
|
graph = json_to_graph(agent_json)
|
||||||
db = library_db()
|
|
||||||
if is_update:
|
if is_update:
|
||||||
return await db.update_graph_in_library(graph, user_id)
|
return await library_db.update_graph_in_library(graph, user_id)
|
||||||
return await db.create_graph_in_library(graph, user_id, folder_id=folder_id)
|
return await library_db.create_graph_in_library(graph, user_id)
|
||||||
|
|
||||||
|
|
||||||
def graph_to_json(graph: Graph) -> dict[str, Any]:
|
def graph_to_json(graph: Graph) -> dict[str, Any]:
|
||||||
@@ -675,14 +735,12 @@ async def get_agent_as_json(
|
|||||||
Returns:
|
Returns:
|
||||||
Agent as JSON dict or None if not found
|
Agent as JSON dict or None if not found
|
||||||
"""
|
"""
|
||||||
db = graph_db()
|
graph = await get_graph(agent_id, version=None, user_id=user_id)
|
||||||
|
|
||||||
graph = await db.get_graph(agent_id, version=None, user_id=user_id)
|
|
||||||
|
|
||||||
if not graph and user_id:
|
if not graph and user_id:
|
||||||
try:
|
try:
|
||||||
library_agent = await library_db().get_library_agent(agent_id, user_id)
|
library_agent = await library_db.get_library_agent(agent_id, user_id)
|
||||||
graph = await db.get_graph(
|
graph = await get_graph(
|
||||||
library_agent.graph_id, version=None, user_id=user_id
|
library_agent.graph_id, version=None, user_id=user_id
|
||||||
)
|
)
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
@@ -692,3 +750,76 @@ async def get_agent_as_json(
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
return graph_to_json(graph)
|
return graph_to_json(graph)
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_agent_patch(
|
||||||
|
update_request: str,
|
||||||
|
current_agent: dict[str, Any],
|
||||||
|
library_agents: list[AgentSummary] | None = None,
|
||||||
|
operation_id: str | None = None,
|
||||||
|
task_id: str | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Update an existing agent using natural language.
|
||||||
|
|
||||||
|
The external Agent Generator service handles:
|
||||||
|
- Generating the patch
|
||||||
|
- Applying the patch
|
||||||
|
- Fixing and validating the result
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update_request: Natural language description of changes
|
||||||
|
current_agent: Current agent JSON
|
||||||
|
library_agents: User's library agents available for sub-agent composition
|
||||||
|
operation_id: Operation ID for async processing (enables Redis Streams callback)
|
||||||
|
task_id: Task ID for async processing (enables Redis Streams callback)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Updated agent JSON, clarifying questions dict {"type": "clarifying_questions", ...},
|
||||||
|
{"status": "accepted"} for async, error dict {"type": "error", ...}, or None on error
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AgentGeneratorNotConfiguredError: If the external service is not configured.
|
||||||
|
"""
|
||||||
|
_check_service_configured()
|
||||||
|
logger.info("Calling external Agent Generator service for generate_agent_patch")
|
||||||
|
return await generate_agent_patch_external(
|
||||||
|
update_request,
|
||||||
|
current_agent,
|
||||||
|
_to_dict_list(library_agents),
|
||||||
|
operation_id,
|
||||||
|
task_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def customize_template(
|
||||||
|
template_agent: dict[str, Any],
|
||||||
|
modification_request: str,
|
||||||
|
context: str = "",
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Customize a template/marketplace agent using natural language.
|
||||||
|
|
||||||
|
This is used when users want to modify a template or marketplace agent
|
||||||
|
to fit their specific needs before adding it to their library.
|
||||||
|
|
||||||
|
The external Agent Generator service handles:
|
||||||
|
- Understanding the modification request
|
||||||
|
- Applying changes to the template
|
||||||
|
- Fixing and validating the result
|
||||||
|
|
||||||
|
Args:
|
||||||
|
template_agent: The template agent JSON to customize
|
||||||
|
modification_request: Natural language description of customizations
|
||||||
|
context: Additional context (e.g., answers to previous questions)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Customized agent JSON, clarifying questions dict {"type": "clarifying_questions", ...},
|
||||||
|
error dict {"type": "error", ...}, or None on unexpected error
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
AgentGeneratorNotConfiguredError: If the external service is not configured.
|
||||||
|
"""
|
||||||
|
_check_service_configured()
|
||||||
|
logger.info("Calling external Agent Generator service for customize_template")
|
||||||
|
return await customize_template_external(
|
||||||
|
template_agent, modification_request, context
|
||||||
|
)
|
||||||
@@ -0,0 +1,498 @@
|
|||||||
|
"""External Agent Generator service client.
|
||||||
|
|
||||||
|
This module provides a client for communicating with the external Agent Generator
|
||||||
|
microservice. When AGENTGENERATOR_HOST is configured, the agent generation functions
|
||||||
|
will delegate to the external service instead of using the built-in LLM-based implementation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from backend.util.settings import Settings
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _create_error_response(
|
||||||
|
error_message: str,
|
||||||
|
error_type: str = "unknown",
|
||||||
|
details: dict[str, Any] | None = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Create a standardized error response dict.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
error_message: Human-readable error message
|
||||||
|
error_type: Machine-readable error type
|
||||||
|
details: Optional additional error details
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Error dict with type="error" and error details
|
||||||
|
"""
|
||||||
|
response: dict[str, Any] = {
|
||||||
|
"type": "error",
|
||||||
|
"error": error_message,
|
||||||
|
"error_type": error_type,
|
||||||
|
}
|
||||||
|
if details:
|
||||||
|
response["details"] = details
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def _classify_http_error(e: httpx.HTTPStatusError) -> tuple[str, str]:
|
||||||
|
"""Classify an HTTP error into error_type and message.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
e: The HTTP status error
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (error_type, error_message)
|
||||||
|
"""
|
||||||
|
status = e.response.status_code
|
||||||
|
if status == 429:
|
||||||
|
return "rate_limit", f"Agent Generator rate limited: {e}"
|
||||||
|
elif status == 503:
|
||||||
|
return "service_unavailable", f"Agent Generator unavailable: {e}"
|
||||||
|
elif status == 504 or status == 408:
|
||||||
|
return "timeout", f"Agent Generator timed out: {e}"
|
||||||
|
else:
|
||||||
|
return "http_error", f"HTTP error calling Agent Generator: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
def _classify_request_error(e: httpx.RequestError) -> tuple[str, str]:
|
||||||
|
"""Classify a request error into error_type and message.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
e: The request error
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (error_type, error_message)
|
||||||
|
"""
|
||||||
|
error_str = str(e).lower()
|
||||||
|
if "timeout" in error_str or "timed out" in error_str:
|
||||||
|
return "timeout", f"Agent Generator request timed out: {e}"
|
||||||
|
elif "connect" in error_str:
|
||||||
|
return "connection_error", f"Could not connect to Agent Generator: {e}"
|
||||||
|
else:
|
||||||
|
return "request_error", f"Request error calling Agent Generator: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
_client: httpx.AsyncClient | None = None
|
||||||
|
_settings: Settings | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_settings() -> Settings:
|
||||||
|
"""Get or create settings singleton."""
|
||||||
|
global _settings
|
||||||
|
if _settings is None:
|
||||||
|
_settings = Settings()
|
||||||
|
return _settings
|
||||||
|
|
||||||
|
|
||||||
|
def is_external_service_configured() -> bool:
|
||||||
|
"""Check if external Agent Generator service is configured."""
|
||||||
|
settings = _get_settings()
|
||||||
|
return bool(settings.config.agentgenerator_host)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_base_url() -> str:
|
||||||
|
"""Get the base URL for the external service."""
|
||||||
|
settings = _get_settings()
|
||||||
|
host = settings.config.agentgenerator_host
|
||||||
|
port = settings.config.agentgenerator_port
|
||||||
|
return f"http://{host}:{port}"
|
||||||
|
|
||||||
|
|
||||||
|
def _get_client() -> httpx.AsyncClient:
|
||||||
|
"""Get or create the HTTP client for the external service."""
|
||||||
|
global _client
|
||||||
|
if _client is None:
|
||||||
|
settings = _get_settings()
|
||||||
|
_client = httpx.AsyncClient(
|
||||||
|
base_url=_get_base_url(),
|
||||||
|
timeout=httpx.Timeout(settings.config.agentgenerator_timeout),
|
||||||
|
)
|
||||||
|
return _client
|
||||||
|
|
||||||
|
|
||||||
|
async def decompose_goal_external(
|
||||||
|
description: str,
|
||||||
|
context: str = "",
|
||||||
|
library_agents: list[dict[str, Any]] | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Call the external service to decompose a goal.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
description: Natural language goal description
|
||||||
|
context: Additional context (e.g., answers to previous questions)
|
||||||
|
library_agents: User's library agents available for sub-agent composition
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict with either:
|
||||||
|
- {"type": "clarifying_questions", "questions": [...]}
|
||||||
|
- {"type": "instructions", "steps": [...]}
|
||||||
|
- {"type": "unachievable_goal", ...}
|
||||||
|
- {"type": "vague_goal", ...}
|
||||||
|
- {"type": "error", "error": "...", "error_type": "..."} on error
|
||||||
|
Or None on unexpected error
|
||||||
|
"""
|
||||||
|
client = _get_client()
|
||||||
|
|
||||||
|
if context:
|
||||||
|
description = f"{description}\n\nAdditional context from user:\n{context}"
|
||||||
|
|
||||||
|
payload: dict[str, Any] = {"description": description}
|
||||||
|
if library_agents:
|
||||||
|
payload["library_agents"] = library_agents
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.post("/api/decompose-description", json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if not data.get("success"):
|
||||||
|
error_msg = data.get("error", "Unknown error from Agent Generator")
|
||||||
|
error_type = data.get("error_type", "unknown")
|
||||||
|
logger.error(
|
||||||
|
f"Agent Generator decomposition failed: {error_msg} "
|
||||||
|
f"(type: {error_type})"
|
||||||
|
)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
|
||||||
|
# Map the response to the expected format
|
||||||
|
response_type = data.get("type")
|
||||||
|
if response_type == "instructions":
|
||||||
|
return {"type": "instructions", "steps": data.get("steps", [])}
|
||||||
|
elif response_type == "clarifying_questions":
|
||||||
|
return {
|
||||||
|
"type": "clarifying_questions",
|
||||||
|
"questions": data.get("questions", []),
|
||||||
|
}
|
||||||
|
elif response_type == "unachievable_goal":
|
||||||
|
return {
|
||||||
|
"type": "unachievable_goal",
|
||||||
|
"reason": data.get("reason"),
|
||||||
|
"suggested_goal": data.get("suggested_goal"),
|
||||||
|
}
|
||||||
|
elif response_type == "vague_goal":
|
||||||
|
return {
|
||||||
|
"type": "vague_goal",
|
||||||
|
"suggested_goal": data.get("suggested_goal"),
|
||||||
|
}
|
||||||
|
elif response_type == "error":
|
||||||
|
# Pass through error from the service
|
||||||
|
return _create_error_response(
|
||||||
|
data.get("error", "Unknown error"),
|
||||||
|
data.get("error_type", "unknown"),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
f"Unknown response type from external service: {response_type}"
|
||||||
|
)
|
||||||
|
return _create_error_response(
|
||||||
|
f"Unknown response type from Agent Generator: {response_type}",
|
||||||
|
"invalid_response",
|
||||||
|
)
|
||||||
|
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
error_type, error_msg = _classify_http_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
error_type, error_msg = _classify_request_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Unexpected error calling Agent Generator: {e}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, "unexpected_error")
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_agent_external(
|
||||||
|
instructions: dict[str, Any],
|
||||||
|
library_agents: list[dict[str, Any]] | None = None,
|
||||||
|
operation_id: str | None = None,
|
||||||
|
task_id: str | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Call the external service to generate an agent from instructions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
instructions: Structured instructions from decompose_goal
|
||||||
|
library_agents: User's library agents available for sub-agent composition
|
||||||
|
operation_id: Operation ID for async processing (enables Redis Streams callback)
|
||||||
|
task_id: Task ID for async processing (enables Redis Streams callback)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Agent JSON dict, {"status": "accepted"} for async, or error dict {"type": "error", ...} on error
|
||||||
|
"""
|
||||||
|
client = _get_client()
|
||||||
|
|
||||||
|
# Build request payload
|
||||||
|
payload: dict[str, Any] = {"instructions": instructions}
|
||||||
|
if library_agents:
|
||||||
|
payload["library_agents"] = library_agents
|
||||||
|
if operation_id and task_id:
|
||||||
|
payload["operation_id"] = operation_id
|
||||||
|
payload["task_id"] = task_id
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.post("/api/generate-agent", json=payload)
|
||||||
|
|
||||||
|
# Handle 202 Accepted for async processing
|
||||||
|
if response.status_code == 202:
|
||||||
|
logger.info(
|
||||||
|
f"Agent Generator accepted async request "
|
||||||
|
f"(operation_id={operation_id}, task_id={task_id})"
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"status": "accepted",
|
||||||
|
"operation_id": operation_id,
|
||||||
|
"task_id": task_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if not data.get("success"):
|
||||||
|
error_msg = data.get("error", "Unknown error from Agent Generator")
|
||||||
|
error_type = data.get("error_type", "unknown")
|
||||||
|
logger.error(
|
||||||
|
f"Agent Generator generation failed: {error_msg} (type: {error_type})"
|
||||||
|
)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
|
||||||
|
return data.get("agent_json")
|
||||||
|
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
error_type, error_msg = _classify_http_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
error_type, error_msg = _classify_request_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Unexpected error calling Agent Generator: {e}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, "unexpected_error")
|
||||||
|
|
||||||
|
|
||||||
|
async def generate_agent_patch_external(
|
||||||
|
update_request: str,
|
||||||
|
current_agent: dict[str, Any],
|
||||||
|
library_agents: list[dict[str, Any]] | None = None,
|
||||||
|
operation_id: str | None = None,
|
||||||
|
task_id: str | None = None,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Call the external service to generate a patch for an existing agent.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update_request: Natural language description of changes
|
||||||
|
current_agent: Current agent JSON
|
||||||
|
library_agents: User's library agents available for sub-agent composition
|
||||||
|
operation_id: Operation ID for async processing (enables Redis Streams callback)
|
||||||
|
task_id: Task ID for async processing (enables Redis Streams callback)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Updated agent JSON, clarifying questions dict, {"status": "accepted"} for async, or error dict on error
|
||||||
|
"""
|
||||||
|
client = _get_client()
|
||||||
|
|
||||||
|
# Build request payload
|
||||||
|
payload: dict[str, Any] = {
|
||||||
|
"update_request": update_request,
|
||||||
|
"current_agent_json": current_agent,
|
||||||
|
}
|
||||||
|
if library_agents:
|
||||||
|
payload["library_agents"] = library_agents
|
||||||
|
if operation_id and task_id:
|
||||||
|
payload["operation_id"] = operation_id
|
||||||
|
payload["task_id"] = task_id
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.post("/api/update-agent", json=payload)
|
||||||
|
|
||||||
|
# Handle 202 Accepted for async processing
|
||||||
|
if response.status_code == 202:
|
||||||
|
logger.info(
|
||||||
|
f"Agent Generator accepted async update request "
|
||||||
|
f"(operation_id={operation_id}, task_id={task_id})"
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"status": "accepted",
|
||||||
|
"operation_id": operation_id,
|
||||||
|
"task_id": task_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if not data.get("success"):
|
||||||
|
error_msg = data.get("error", "Unknown error from Agent Generator")
|
||||||
|
error_type = data.get("error_type", "unknown")
|
||||||
|
logger.error(
|
||||||
|
f"Agent Generator patch generation failed: {error_msg} "
|
||||||
|
f"(type: {error_type})"
|
||||||
|
)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
|
||||||
|
# Check if it's clarifying questions
|
||||||
|
if data.get("type") == "clarifying_questions":
|
||||||
|
return {
|
||||||
|
"type": "clarifying_questions",
|
||||||
|
"questions": data.get("questions", []),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if it's an error passed through
|
||||||
|
if data.get("type") == "error":
|
||||||
|
return _create_error_response(
|
||||||
|
data.get("error", "Unknown error"),
|
||||||
|
data.get("error_type", "unknown"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Otherwise return the updated agent JSON
|
||||||
|
return data.get("agent_json")
|
||||||
|
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
error_type, error_msg = _classify_http_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
error_type, error_msg = _classify_request_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Unexpected error calling Agent Generator: {e}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, "unexpected_error")
|
||||||
|
|
||||||
|
|
||||||
|
async def customize_template_external(
|
||||||
|
template_agent: dict[str, Any],
|
||||||
|
modification_request: str,
|
||||||
|
context: str = "",
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
|
"""Call the external service to customize a template/marketplace agent.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
template_agent: The template agent JSON to customize
|
||||||
|
modification_request: Natural language description of customizations
|
||||||
|
context: Additional context (e.g., answers to previous questions)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Customized agent JSON, clarifying questions dict, or error dict on error
|
||||||
|
"""
|
||||||
|
client = _get_client()
|
||||||
|
|
||||||
|
request = modification_request
|
||||||
|
if context:
|
||||||
|
request = f"{modification_request}\n\nAdditional context from user:\n{context}"
|
||||||
|
|
||||||
|
payload: dict[str, Any] = {
|
||||||
|
"template_agent_json": template_agent,
|
||||||
|
"modification_request": request,
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.post("/api/template-modification", json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if not data.get("success"):
|
||||||
|
error_msg = data.get("error", "Unknown error from Agent Generator")
|
||||||
|
error_type = data.get("error_type", "unknown")
|
||||||
|
logger.error(
|
||||||
|
f"Agent Generator template customization failed: {error_msg} "
|
||||||
|
f"(type: {error_type})"
|
||||||
|
)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
|
||||||
|
# Check if it's clarifying questions
|
||||||
|
if data.get("type") == "clarifying_questions":
|
||||||
|
return {
|
||||||
|
"type": "clarifying_questions",
|
||||||
|
"questions": data.get("questions", []),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if it's an error passed through
|
||||||
|
if data.get("type") == "error":
|
||||||
|
return _create_error_response(
|
||||||
|
data.get("error", "Unknown error"),
|
||||||
|
data.get("error_type", "unknown"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Otherwise return the customized agent JSON
|
||||||
|
return data.get("agent_json")
|
||||||
|
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
error_type, error_msg = _classify_http_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
error_type, error_msg = _classify_request_error(e)
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, error_type)
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Unexpected error calling Agent Generator: {e}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return _create_error_response(error_msg, "unexpected_error")
|
||||||
|
|
||||||
|
|
||||||
|
async def get_blocks_external() -> list[dict[str, Any]] | None:
|
||||||
|
"""Get available blocks from the external service.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of block info dicts or None on error
|
||||||
|
"""
|
||||||
|
client = _get_client()
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.get("/api/blocks")
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
if not data.get("success"):
|
||||||
|
logger.error("External service returned error getting blocks")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return data.get("blocks", [])
|
||||||
|
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
logger.error(f"HTTP error getting blocks from external service: {e}")
|
||||||
|
return None
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
logger.error(f"Request error getting blocks from external service: {e}")
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected error getting blocks from external service: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def health_check() -> bool:
|
||||||
|
"""Check if the external service is healthy.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if healthy, False otherwise
|
||||||
|
"""
|
||||||
|
if not is_external_service_configured():
|
||||||
|
return False
|
||||||
|
|
||||||
|
client = _get_client()
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.get("/health")
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
return data.get("status") == "healthy" and data.get("blocks_loaded", False)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"External agent generator health check failed: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def close_client() -> None:
|
||||||
|
"""Close the HTTP client."""
|
||||||
|
global _client
|
||||||
|
if _client is not None:
|
||||||
|
await _client.aclose()
|
||||||
|
_client = None
|
||||||
@@ -5,15 +5,15 @@ import re
|
|||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pydantic import BaseModel, Field, field_validator
|
from pydantic import BaseModel, field_validator
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.api.features.library import db as library_db
|
||||||
from backend.api.features.library.model import LibraryAgent
|
from backend.api.features.library.model import LibraryAgent
|
||||||
from backend.copilot.model import ChatSession
|
from backend.data import execution as execution_db
|
||||||
from backend.data.db_accessors import execution_db, library_db
|
|
||||||
from backend.data.execution import ExecutionStatus, GraphExecution, GraphExecutionMeta
|
from backend.data.execution import ExecutionStatus, GraphExecution, GraphExecutionMeta
|
||||||
|
|
||||||
from .base import BaseTool
|
from .base import BaseTool
|
||||||
from .execution_utils import TERMINAL_STATUSES, wait_for_execution
|
|
||||||
from .models import (
|
from .models import (
|
||||||
AgentOutputResponse,
|
AgentOutputResponse,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
@@ -34,7 +34,6 @@ class AgentOutputInput(BaseModel):
|
|||||||
store_slug: str = ""
|
store_slug: str = ""
|
||||||
execution_id: str = ""
|
execution_id: str = ""
|
||||||
run_time: str = "latest"
|
run_time: str = "latest"
|
||||||
wait_if_running: int = Field(default=0, ge=0, le=300)
|
|
||||||
|
|
||||||
@field_validator(
|
@field_validator(
|
||||||
"agent_name",
|
"agent_name",
|
||||||
@@ -118,11 +117,6 @@ class AgentOutputTool(BaseTool):
|
|||||||
Select which run to retrieve using:
|
Select which run to retrieve using:
|
||||||
- execution_id: Specific execution ID
|
- execution_id: Specific execution ID
|
||||||
- run_time: 'latest' (default), 'yesterday', 'last week', or ISO date 'YYYY-MM-DD'
|
- run_time: 'latest' (default), 'yesterday', 'last week', or ISO date 'YYYY-MM-DD'
|
||||||
|
|
||||||
Wait for completion (optional):
|
|
||||||
- wait_if_running: Max seconds to wait if execution is still running (0-300).
|
|
||||||
If the execution is running/queued, waits up to this many seconds for completion.
|
|
||||||
Returns current status on timeout. If already finished, returns immediately.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -152,13 +146,6 @@ class AgentOutputTool(BaseTool):
|
|||||||
"Time filter: 'latest', 'yesterday', 'last week', or 'YYYY-MM-DD'"
|
"Time filter: 'latest', 'yesterday', 'last week', or 'YYYY-MM-DD'"
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
"wait_if_running": {
|
|
||||||
"type": "integer",
|
|
||||||
"description": (
|
|
||||||
"Max seconds to wait if execution is still running (0-300). "
|
|
||||||
"If running, waits for completion. Returns current state on timeout."
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
"required": [],
|
"required": [],
|
||||||
}
|
}
|
||||||
@@ -178,12 +165,10 @@ class AgentOutputTool(BaseTool):
|
|||||||
Resolve agent from provided identifiers.
|
Resolve agent from provided identifiers.
|
||||||
Returns (library_agent, error_message).
|
Returns (library_agent, error_message).
|
||||||
"""
|
"""
|
||||||
lib_db = library_db()
|
|
||||||
|
|
||||||
# Priority 1: Exact library agent ID
|
# Priority 1: Exact library agent ID
|
||||||
if library_agent_id:
|
if library_agent_id:
|
||||||
try:
|
try:
|
||||||
agent = await lib_db.get_library_agent(library_agent_id, user_id)
|
agent = await library_db.get_library_agent(library_agent_id, user_id)
|
||||||
return agent, None
|
return agent, None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to get library agent by ID: {e}")
|
logger.warning(f"Failed to get library agent by ID: {e}")
|
||||||
@@ -197,7 +182,7 @@ class AgentOutputTool(BaseTool):
|
|||||||
return None, f"Agent '{store_slug}' not found in marketplace"
|
return None, f"Agent '{store_slug}' not found in marketplace"
|
||||||
|
|
||||||
# Find in user's library by graph_id
|
# Find in user's library by graph_id
|
||||||
agent = await lib_db.get_library_agent_by_graph_id(user_id, graph.id)
|
agent = await library_db.get_library_agent_by_graph_id(user_id, graph.id)
|
||||||
if not agent:
|
if not agent:
|
||||||
return (
|
return (
|
||||||
None,
|
None,
|
||||||
@@ -209,7 +194,7 @@ class AgentOutputTool(BaseTool):
|
|||||||
# Priority 3: Fuzzy name search in library
|
# Priority 3: Fuzzy name search in library
|
||||||
if agent_name:
|
if agent_name:
|
||||||
try:
|
try:
|
||||||
response = await lib_db.list_library_agents(
|
response = await library_db.list_library_agents(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
search_term=agent_name,
|
search_term=agent_name,
|
||||||
page_size=5,
|
page_size=5,
|
||||||
@@ -238,20 +223,14 @@ class AgentOutputTool(BaseTool):
|
|||||||
execution_id: str | None,
|
execution_id: str | None,
|
||||||
time_start: datetime | None,
|
time_start: datetime | None,
|
||||||
time_end: datetime | None,
|
time_end: datetime | None,
|
||||||
include_running: bool = False,
|
|
||||||
) -> tuple[GraphExecution | None, list[GraphExecutionMeta], str | None]:
|
) -> tuple[GraphExecution | None, list[GraphExecutionMeta], str | None]:
|
||||||
"""
|
"""
|
||||||
Fetch execution(s) based on filters.
|
Fetch execution(s) based on filters.
|
||||||
Returns (single_execution, available_executions_meta, error_message).
|
Returns (single_execution, available_executions_meta, error_message).
|
||||||
|
|
||||||
Args:
|
|
||||||
include_running: If True, also look for running/queued executions (for waiting)
|
|
||||||
"""
|
"""
|
||||||
exec_db = execution_db()
|
|
||||||
|
|
||||||
# If specific execution_id provided, fetch it directly
|
# If specific execution_id provided, fetch it directly
|
||||||
if execution_id:
|
if execution_id:
|
||||||
execution = await exec_db.get_graph_execution(
|
execution = await execution_db.get_graph_execution(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
execution_id=execution_id,
|
execution_id=execution_id,
|
||||||
include_node_executions=False,
|
include_node_executions=False,
|
||||||
@@ -260,25 +239,11 @@ class AgentOutputTool(BaseTool):
|
|||||||
return None, [], f"Execution '{execution_id}' not found"
|
return None, [], f"Execution '{execution_id}' not found"
|
||||||
return execution, [], None
|
return execution, [], None
|
||||||
|
|
||||||
# Determine which statuses to query
|
# Get completed executions with time filters
|
||||||
statuses = [ExecutionStatus.COMPLETED]
|
executions = await execution_db.get_graph_executions(
|
||||||
if include_running:
|
|
||||||
statuses.extend(
|
|
||||||
[
|
|
||||||
ExecutionStatus.RUNNING,
|
|
||||||
ExecutionStatus.QUEUED,
|
|
||||||
ExecutionStatus.INCOMPLETE,
|
|
||||||
ExecutionStatus.REVIEW,
|
|
||||||
ExecutionStatus.FAILED,
|
|
||||||
ExecutionStatus.TERMINATED,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get executions with time filters
|
|
||||||
executions = await exec_db.get_graph_executions(
|
|
||||||
graph_id=graph_id,
|
graph_id=graph_id,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
statuses=statuses,
|
statuses=[ExecutionStatus.COMPLETED],
|
||||||
created_time_gte=time_start,
|
created_time_gte=time_start,
|
||||||
created_time_lte=time_end,
|
created_time_lte=time_end,
|
||||||
limit=10,
|
limit=10,
|
||||||
@@ -289,7 +254,7 @@ class AgentOutputTool(BaseTool):
|
|||||||
|
|
||||||
# If only one execution, fetch full details
|
# If only one execution, fetch full details
|
||||||
if len(executions) == 1:
|
if len(executions) == 1:
|
||||||
full_execution = await exec_db.get_graph_execution(
|
full_execution = await execution_db.get_graph_execution(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
execution_id=executions[0].id,
|
execution_id=executions[0].id,
|
||||||
include_node_executions=False,
|
include_node_executions=False,
|
||||||
@@ -297,7 +262,7 @@ class AgentOutputTool(BaseTool):
|
|||||||
return full_execution, [], None
|
return full_execution, [], None
|
||||||
|
|
||||||
# Multiple executions - return latest with full details, plus list of available
|
# Multiple executions - return latest with full details, plus list of available
|
||||||
full_execution = await exec_db.get_graph_execution(
|
full_execution = await execution_db.get_graph_execution(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
execution_id=executions[0].id,
|
execution_id=executions[0].id,
|
||||||
include_node_executions=False,
|
include_node_executions=False,
|
||||||
@@ -345,33 +310,10 @@ class AgentOutputTool(BaseTool):
|
|||||||
for e in available_executions[:5]
|
for e in available_executions[:5]
|
||||||
]
|
]
|
||||||
|
|
||||||
# Build appropriate message based on execution status
|
message = f"Found execution outputs for agent '{agent.name}'"
|
||||||
if execution.status == ExecutionStatus.COMPLETED:
|
|
||||||
message = f"Found execution outputs for agent '{agent.name}'"
|
|
||||||
elif execution.status == ExecutionStatus.FAILED:
|
|
||||||
message = f"Execution for agent '{agent.name}' failed"
|
|
||||||
elif execution.status == ExecutionStatus.TERMINATED:
|
|
||||||
message = f"Execution for agent '{agent.name}' was terminated"
|
|
||||||
elif execution.status == ExecutionStatus.REVIEW:
|
|
||||||
message = (
|
|
||||||
f"Execution for agent '{agent.name}' is awaiting human review. "
|
|
||||||
"The user needs to approve it before it can continue."
|
|
||||||
)
|
|
||||||
elif execution.status in (
|
|
||||||
ExecutionStatus.RUNNING,
|
|
||||||
ExecutionStatus.QUEUED,
|
|
||||||
ExecutionStatus.INCOMPLETE,
|
|
||||||
):
|
|
||||||
message = (
|
|
||||||
f"Execution for agent '{agent.name}' is still {execution.status.value}. "
|
|
||||||
"Results may be incomplete. Use wait_if_running to wait for completion."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
message = f"Found execution for agent '{agent.name}' (status: {execution.status.value})"
|
|
||||||
|
|
||||||
if len(available_executions) > 1:
|
if len(available_executions) > 1:
|
||||||
message += (
|
message += (
|
||||||
f" Showing latest of {len(available_executions)} matching executions."
|
f". Showing latest of {len(available_executions)} matching executions."
|
||||||
)
|
)
|
||||||
|
|
||||||
return AgentOutputResponse(
|
return AgentOutputResponse(
|
||||||
@@ -438,7 +380,7 @@ class AgentOutputTool(BaseTool):
|
|||||||
and not input_data.store_slug
|
and not input_data.store_slug
|
||||||
):
|
):
|
||||||
# Fetch execution directly to get graph_id
|
# Fetch execution directly to get graph_id
|
||||||
execution = await execution_db().get_graph_execution(
|
execution = await execution_db.get_graph_execution(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
execution_id=input_data.execution_id,
|
execution_id=input_data.execution_id,
|
||||||
include_node_executions=False,
|
include_node_executions=False,
|
||||||
@@ -450,7 +392,7 @@ class AgentOutputTool(BaseTool):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Find library agent by graph_id
|
# Find library agent by graph_id
|
||||||
agent = await library_db().get_library_agent_by_graph_id(
|
agent = await library_db.get_library_agent_by_graph_id(
|
||||||
user_id, execution.graph_id
|
user_id, execution.graph_id
|
||||||
)
|
)
|
||||||
if not agent:
|
if not agent:
|
||||||
@@ -486,17 +428,13 @@ class AgentOutputTool(BaseTool):
|
|||||||
# Parse time expression
|
# Parse time expression
|
||||||
time_start, time_end = parse_time_expression(input_data.run_time)
|
time_start, time_end = parse_time_expression(input_data.run_time)
|
||||||
|
|
||||||
# Check if we should wait for running executions
|
# Fetch execution(s)
|
||||||
wait_timeout = input_data.wait_if_running
|
|
||||||
|
|
||||||
# Fetch execution(s) - include running if we're going to wait
|
|
||||||
execution, available_executions, exec_error = await self._get_execution(
|
execution, available_executions, exec_error = await self._get_execution(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
graph_id=agent.graph_id,
|
graph_id=agent.graph_id,
|
||||||
execution_id=input_data.execution_id or None,
|
execution_id=input_data.execution_id or None,
|
||||||
time_start=time_start,
|
time_start=time_start,
|
||||||
time_end=time_end,
|
time_end=time_end,
|
||||||
include_running=wait_timeout > 0,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if exec_error:
|
if exec_error:
|
||||||
@@ -505,17 +443,4 @@ class AgentOutputTool(BaseTool):
|
|||||||
session_id=session_id,
|
session_id=session_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# If we have an execution that's still running and we should wait
|
|
||||||
if execution and wait_timeout > 0 and execution.status not in TERMINAL_STATUSES:
|
|
||||||
logger.info(
|
|
||||||
f"Execution {execution.id} is {execution.status}, "
|
|
||||||
f"waiting up to {wait_timeout}s for completion"
|
|
||||||
)
|
|
||||||
execution = await wait_for_execution(
|
|
||||||
user_id=user_id,
|
|
||||||
graph_id=agent.graph_id,
|
|
||||||
execution_id=execution.id,
|
|
||||||
timeout_seconds=wait_timeout,
|
|
||||||
)
|
|
||||||
|
|
||||||
return self._build_response(agent, execution, available_executions, session_id)
|
return self._build_response(agent, execution, available_executions, session_id)
|
||||||
@@ -1,15 +1,11 @@
|
|||||||
"""Shared agent search functionality for find_agent and find_library_agent tools."""
|
"""Shared agent search functionality for find_agent and find_library_agent tools."""
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from typing import TYPE_CHECKING, Literal
|
from typing import Literal
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
from backend.api.features.library import db as library_db
|
||||||
from backend.api.features.library.model import LibraryAgent
|
from backend.api.features.store import db as store_db
|
||||||
|
|
||||||
from backend.data.db_accessors import library_db, store_db
|
|
||||||
from backend.util.exceptions import DatabaseError, NotFoundError
|
from backend.util.exceptions import DatabaseError, NotFoundError
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
@@ -29,24 +25,92 @@ _UUID_PATTERN = re.compile(
|
|||||||
re.IGNORECASE,
|
re.IGNORECASE,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Keywords that should be treated as "list all" rather than a literal search
|
|
||||||
_LIST_ALL_KEYWORDS = frozenset({"all", "*", "everything", "any", ""})
|
def _is_uuid(text: str) -> bool:
|
||||||
|
"""Check if text is a valid UUID v4."""
|
||||||
|
return bool(_UUID_PATTERN.match(text.strip()))
|
||||||
|
|
||||||
|
|
||||||
|
async def _get_library_agent_by_id(user_id: str, agent_id: str) -> AgentInfo | None:
|
||||||
|
"""Fetch a library agent by ID (library agent ID or graph_id).
|
||||||
|
|
||||||
|
Tries multiple lookup strategies:
|
||||||
|
1. First by graph_id (AgentGraph primary key)
|
||||||
|
2. Then by library agent ID (LibraryAgent primary key)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id: The user ID
|
||||||
|
agent_id: The ID to look up (can be graph_id or library agent ID)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
AgentInfo if found, None otherwise
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
agent = await library_db.get_library_agent_by_graph_id(user_id, agent_id)
|
||||||
|
if agent:
|
||||||
|
logger.debug(f"Found library agent by graph_id: {agent.name}")
|
||||||
|
return AgentInfo(
|
||||||
|
id=agent.id,
|
||||||
|
name=agent.name,
|
||||||
|
description=agent.description or "",
|
||||||
|
source="library",
|
||||||
|
in_library=True,
|
||||||
|
creator=agent.creator_name,
|
||||||
|
status=agent.status.value,
|
||||||
|
can_access_graph=agent.can_access_graph,
|
||||||
|
has_external_trigger=agent.has_external_trigger,
|
||||||
|
new_output=agent.new_output,
|
||||||
|
graph_id=agent.graph_id,
|
||||||
|
)
|
||||||
|
except DatabaseError:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(
|
||||||
|
f"Could not fetch library agent by graph_id {agent_id}: {e}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
agent = await library_db.get_library_agent(agent_id, user_id)
|
||||||
|
if agent:
|
||||||
|
logger.debug(f"Found library agent by library_id: {agent.name}")
|
||||||
|
return AgentInfo(
|
||||||
|
id=agent.id,
|
||||||
|
name=agent.name,
|
||||||
|
description=agent.description or "",
|
||||||
|
source="library",
|
||||||
|
in_library=True,
|
||||||
|
creator=agent.creator_name,
|
||||||
|
status=agent.status.value,
|
||||||
|
can_access_graph=agent.can_access_graph,
|
||||||
|
has_external_trigger=agent.has_external_trigger,
|
||||||
|
new_output=agent.new_output,
|
||||||
|
graph_id=agent.graph_id,
|
||||||
|
)
|
||||||
|
except NotFoundError:
|
||||||
|
logger.debug(f"Library agent not found by library_id: {agent_id}")
|
||||||
|
except DatabaseError:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(
|
||||||
|
f"Could not fetch library agent by library_id {agent_id}: {e}",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
async def search_agents(
|
async def search_agents(
|
||||||
query: str,
|
query: str,
|
||||||
source: SearchSource,
|
source: SearchSource,
|
||||||
session_id: str | None = None,
|
session_id: str | None,
|
||||||
user_id: str | None = None,
|
user_id: str | None = None,
|
||||||
) -> ToolResponseBase:
|
) -> ToolResponseBase:
|
||||||
"""
|
"""
|
||||||
Search for agents in marketplace or user library.
|
Search for agents in marketplace or user library.
|
||||||
|
|
||||||
For library searches, keywords like "all", "*", "everything", or an empty
|
|
||||||
query will list all agents without filtering.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query: Search query string. Special keywords list all library agents.
|
query: Search query string
|
||||||
source: "marketplace" or "library"
|
source: "marketplace" or "library"
|
||||||
session_id: Chat session ID
|
session_id: Chat session ID
|
||||||
user_id: User ID (required for library search)
|
user_id: User ID (required for library search)
|
||||||
@@ -54,11 +118,7 @@ async def search_agents(
|
|||||||
Returns:
|
Returns:
|
||||||
AgentsFoundResponse, NoResultsResponse, or ErrorResponse
|
AgentsFoundResponse, NoResultsResponse, or ErrorResponse
|
||||||
"""
|
"""
|
||||||
# Normalize list-all keywords to empty string for library searches
|
if not query:
|
||||||
if source == "library" and query.lower().strip() in _LIST_ALL_KEYWORDS:
|
|
||||||
query = ""
|
|
||||||
|
|
||||||
if source == "marketplace" and not query:
|
|
||||||
return ErrorResponse(
|
return ErrorResponse(
|
||||||
message="Please provide a search query", session_id=session_id
|
message="Please provide a search query", session_id=session_id
|
||||||
)
|
)
|
||||||
@@ -73,7 +133,7 @@ async def search_agents(
|
|||||||
try:
|
try:
|
||||||
if source == "marketplace":
|
if source == "marketplace":
|
||||||
logger.info(f"Searching marketplace for: {query}")
|
logger.info(f"Searching marketplace for: {query}")
|
||||||
results = await store_db().get_store_agents(search_query=query, page_size=5)
|
results = await store_db.get_store_agents(search_query=query, page_size=5)
|
||||||
for agent in results.agents:
|
for agent in results.agents:
|
||||||
agents.append(
|
agents.append(
|
||||||
AgentInfo(
|
AgentInfo(
|
||||||
@@ -98,18 +158,28 @@ async def search_agents(
|
|||||||
logger.info(f"Found agent by direct ID lookup: {agent.name}")
|
logger.info(f"Found agent by direct ID lookup: {agent.name}")
|
||||||
|
|
||||||
if not agents:
|
if not agents:
|
||||||
search_term = query or None
|
logger.info(f"Searching user library for: {query}")
|
||||||
logger.info(
|
results = await library_db.list_library_agents(
|
||||||
f"{'Listing all agents in' if not query else 'Searching'} "
|
|
||||||
f"user library{'' if not query else f' for: {query}'}"
|
|
||||||
)
|
|
||||||
results = await library_db().list_library_agents(
|
|
||||||
user_id=user_id, # type: ignore[arg-type]
|
user_id=user_id, # type: ignore[arg-type]
|
||||||
search_term=search_term,
|
search_term=query,
|
||||||
page_size=50 if not query else 10,
|
page_size=10,
|
||||||
)
|
)
|
||||||
for agent in results.agents:
|
for agent in results.agents:
|
||||||
agents.append(_library_agent_to_info(agent))
|
agents.append(
|
||||||
|
AgentInfo(
|
||||||
|
id=agent.id,
|
||||||
|
name=agent.name,
|
||||||
|
description=agent.description or "",
|
||||||
|
source="library",
|
||||||
|
in_library=True,
|
||||||
|
creator=agent.creator_name,
|
||||||
|
status=agent.status.value,
|
||||||
|
can_access_graph=agent.can_access_graph,
|
||||||
|
has_external_trigger=agent.has_external_trigger,
|
||||||
|
new_output=agent.new_output,
|
||||||
|
graph_id=agent.graph_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
logger.info(f"Found {len(agents)} agents in {source}")
|
logger.info(f"Found {len(agents)} agents in {source}")
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
pass
|
pass
|
||||||
@@ -122,62 +192,42 @@ async def search_agents(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not agents:
|
if not agents:
|
||||||
if source == "marketplace":
|
suggestions = (
|
||||||
suggestions = [
|
[
|
||||||
"Try more general terms",
|
"Try more general terms",
|
||||||
"Browse categories in the marketplace",
|
"Browse categories in the marketplace",
|
||||||
"Check spelling",
|
"Check spelling",
|
||||||
]
|
]
|
||||||
no_results_msg = (
|
if source == "marketplace"
|
||||||
f"No agents found matching '{query}'. Let the user know they can "
|
else [
|
||||||
"try different keywords or browse the marketplace. Also let them "
|
|
||||||
"know you can create a custom agent for them based on their needs."
|
|
||||||
)
|
|
||||||
elif not query:
|
|
||||||
# User asked to list all but library is empty
|
|
||||||
suggestions = [
|
|
||||||
"Browse the marketplace to find and add agents",
|
|
||||||
"Use find_agent to search the marketplace",
|
|
||||||
]
|
|
||||||
no_results_msg = (
|
|
||||||
"Your library is empty. Let the user know they can browse the "
|
|
||||||
"marketplace to find agents, or you can create a custom agent "
|
|
||||||
"for them based on their needs."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
suggestions = [
|
|
||||||
"Try different keywords",
|
"Try different keywords",
|
||||||
"Use find_agent to search the marketplace",
|
"Use find_agent to search the marketplace",
|
||||||
"Check your library at /library",
|
"Check your library at /library",
|
||||||
]
|
]
|
||||||
no_results_msg = (
|
)
|
||||||
f"No agents matching '{query}' found in your library. Let the "
|
no_results_msg = (
|
||||||
"user know you can create a custom agent for them based on "
|
f"No agents found matching '{query}'. Let the user know they can try different keywords or browse the marketplace. Also let them know you can create a custom agent for them based on their needs."
|
||||||
"their needs."
|
if source == "marketplace"
|
||||||
)
|
else f"No agents matching '{query}' found in your library. Let the user know you can create a custom agent for them based on their needs."
|
||||||
|
)
|
||||||
return NoResultsResponse(
|
return NoResultsResponse(
|
||||||
message=no_results_msg, session_id=session_id, suggestions=suggestions
|
message=no_results_msg, session_id=session_id, suggestions=suggestions
|
||||||
)
|
)
|
||||||
|
|
||||||
if source == "marketplace":
|
title = f"Found {len(agents)} agent{'s' if len(agents) != 1 else ''} "
|
||||||
title = (
|
title += (
|
||||||
f"Found {len(agents)} agent{'s' if len(agents) != 1 else ''} for '{query}'"
|
f"for '{query}'"
|
||||||
)
|
if source == "marketplace"
|
||||||
elif not query:
|
else f"in your library for '{query}'"
|
||||||
title = f"Found {len(agents)} agent{'s' if len(agents) != 1 else ''} in your library"
|
)
|
||||||
else:
|
|
||||||
title = f"Found {len(agents)} agent{'s' if len(agents) != 1 else ''} in your library for '{query}'"
|
|
||||||
|
|
||||||
message = (
|
message = (
|
||||||
"Now you have found some options for the user to choose from. "
|
"Now you have found some options for the user to choose from. "
|
||||||
"You can add a link to a recommended agent at: /marketplace/agent/agent_id "
|
"You can add a link to a recommended agent at: /marketplace/agent/agent_id "
|
||||||
"Please ask the user if they would like to use any of these agents. "
|
"Please ask the user if they would like to use any of these agents. Let the user know we can create a custom agent for them based on their needs."
|
||||||
"Let the user know we can create a custom agent for them based on their needs."
|
|
||||||
if source == "marketplace"
|
if source == "marketplace"
|
||||||
else "Found agents in the user's library. You can provide a link to view "
|
else "Found agents in the user's library. You can provide a link to view an agent at: "
|
||||||
"an agent at: /library/agents/{agent_id}. Use agent_output to get "
|
"/library/agents/{agent_id}. Use agent_output to get execution results, or run_agent to execute. Let the user know we can create a custom agent for them based on their needs."
|
||||||
"execution results, or run_agent to execute. Let the user know we can "
|
|
||||||
"create a custom agent for them based on their needs."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return AgentsFoundResponse(
|
return AgentsFoundResponse(
|
||||||
@@ -187,70 +237,3 @@ async def search_agents(
|
|||||||
count=len(agents),
|
count=len(agents),
|
||||||
session_id=session_id,
|
session_id=session_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _is_uuid(text: str) -> bool:
|
|
||||||
"""Check if text is a valid UUID v4."""
|
|
||||||
return bool(_UUID_PATTERN.match(text.strip()))
|
|
||||||
|
|
||||||
|
|
||||||
def _library_agent_to_info(agent: LibraryAgent) -> AgentInfo:
|
|
||||||
"""Convert a library agent model to an AgentInfo."""
|
|
||||||
return AgentInfo(
|
|
||||||
id=agent.id,
|
|
||||||
name=agent.name,
|
|
||||||
description=agent.description or "",
|
|
||||||
source="library",
|
|
||||||
in_library=True,
|
|
||||||
creator=agent.creator_name,
|
|
||||||
status=agent.status.value,
|
|
||||||
can_access_graph=agent.can_access_graph,
|
|
||||||
has_external_trigger=agent.has_external_trigger,
|
|
||||||
new_output=agent.new_output,
|
|
||||||
graph_id=agent.graph_id,
|
|
||||||
graph_version=agent.graph_version,
|
|
||||||
input_schema=agent.input_schema,
|
|
||||||
output_schema=agent.output_schema,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def _get_library_agent_by_id(user_id: str, agent_id: str) -> AgentInfo | None:
|
|
||||||
"""Fetch a library agent by ID (library agent ID or graph_id).
|
|
||||||
|
|
||||||
Tries multiple lookup strategies:
|
|
||||||
1. First by graph_id (AgentGraph primary key)
|
|
||||||
2. Then by library agent ID (LibraryAgent primary key)
|
|
||||||
"""
|
|
||||||
lib_db = library_db()
|
|
||||||
|
|
||||||
try:
|
|
||||||
agent = await lib_db.get_library_agent_by_graph_id(user_id, agent_id)
|
|
||||||
if agent:
|
|
||||||
logger.debug(f"Found library agent by graph_id: {agent.name}")
|
|
||||||
return _library_agent_to_info(agent)
|
|
||||||
except NotFoundError:
|
|
||||||
logger.debug(f"Library agent not found by graph_id: {agent_id}")
|
|
||||||
except DatabaseError:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(
|
|
||||||
f"Could not fetch library agent by graph_id {agent_id}: {e}",
|
|
||||||
exc_info=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
agent = await lib_db.get_library_agent(agent_id, user_id)
|
|
||||||
if agent:
|
|
||||||
logger.debug(f"Found library agent by library_id: {agent.name}")
|
|
||||||
return _library_agent_to_info(agent)
|
|
||||||
except NotFoundError:
|
|
||||||
logger.debug(f"Library agent not found by library_id: {agent_id}")
|
|
||||||
except DatabaseError:
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(
|
|
||||||
f"Could not fetch library agent by library_id {agent_id}: {e}",
|
|
||||||
exc_info=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
return None
|
|
||||||
129
autogpt_platform/backend/backend/api/features/chat/tools/base.py
Normal file
129
autogpt_platform/backend/backend/api/features/chat/tools/base.py
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
"""Base classes and shared utilities for chat tools."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from openai.types.chat import ChatCompletionToolParam
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.api.features.chat.response_model import StreamToolOutputAvailable
|
||||||
|
|
||||||
|
from .models import ErrorResponse, NeedLoginResponse, ToolResponseBase
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseTool:
|
||||||
|
"""Base class for all chat tools."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
"""Tool name for OpenAI function calling."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
"""Tool description for OpenAI."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
"""Tool parameters schema for OpenAI."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
"""Whether this tool requires authentication."""
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_long_running(self) -> bool:
|
||||||
|
"""Whether this tool is long-running and should execute in background.
|
||||||
|
|
||||||
|
Long-running tools (like agent generation) are executed via background
|
||||||
|
tasks to survive SSE disconnections. The result is persisted to chat
|
||||||
|
history and visible when the user refreshes.
|
||||||
|
"""
|
||||||
|
return False
|
||||||
|
|
||||||
|
def as_openai_tool(self) -> ChatCompletionToolParam:
|
||||||
|
"""Convert to OpenAI tool format."""
|
||||||
|
return ChatCompletionToolParam(
|
||||||
|
type="function",
|
||||||
|
function={
|
||||||
|
"name": self.name,
|
||||||
|
"description": self.description,
|
||||||
|
"parameters": self.parameters,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
async def execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
tool_call_id: str,
|
||||||
|
**kwargs,
|
||||||
|
) -> StreamToolOutputAvailable:
|
||||||
|
"""Execute the tool with authentication check.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id: User ID (may be anonymous like "anon_123")
|
||||||
|
session_id: Chat session ID
|
||||||
|
**kwargs: Tool-specific parameters
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Pydantic response object
|
||||||
|
|
||||||
|
"""
|
||||||
|
if self.requires_auth and not user_id:
|
||||||
|
logger.error(
|
||||||
|
f"Attempted tool call for {self.name} but user not authenticated"
|
||||||
|
)
|
||||||
|
return StreamToolOutputAvailable(
|
||||||
|
toolCallId=tool_call_id,
|
||||||
|
toolName=self.name,
|
||||||
|
output=NeedLoginResponse(
|
||||||
|
message=f"Please sign in to use {self.name}",
|
||||||
|
session_id=session.session_id,
|
||||||
|
).model_dump_json(),
|
||||||
|
success=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = await self._execute(user_id, session, **kwargs)
|
||||||
|
return StreamToolOutputAvailable(
|
||||||
|
toolCallId=tool_call_id,
|
||||||
|
toolName=self.name,
|
||||||
|
output=result.model_dump_json(),
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in {self.name}: {e}", exc_info=True)
|
||||||
|
return StreamToolOutputAvailable(
|
||||||
|
toolCallId=tool_call_id,
|
||||||
|
toolName=self.name,
|
||||||
|
output=ErrorResponse(
|
||||||
|
message=f"An error occurred while executing {self.name}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session.session_id,
|
||||||
|
).model_dump_json(),
|
||||||
|
success=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
"""Internal execution logic to be implemented by subclasses.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id: User ID (authenticated or anonymous)
|
||||||
|
session_id: Chat session ID
|
||||||
|
**kwargs: Tool-specific parameters
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Pydantic response object
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
@@ -0,0 +1,335 @@
|
|||||||
|
"""CreateAgentTool - Creates agents from natural language descriptions."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
|
||||||
|
from .agent_generator import (
|
||||||
|
AgentGeneratorNotConfiguredError,
|
||||||
|
decompose_goal,
|
||||||
|
enrich_library_agents_from_steps,
|
||||||
|
generate_agent,
|
||||||
|
get_all_relevant_agents_for_generation,
|
||||||
|
get_user_message_for_error,
|
||||||
|
save_agent_to_library,
|
||||||
|
)
|
||||||
|
from .base import BaseTool
|
||||||
|
from .models import (
|
||||||
|
AgentPreviewResponse,
|
||||||
|
AgentSavedResponse,
|
||||||
|
AsyncProcessingResponse,
|
||||||
|
ClarificationNeededResponse,
|
||||||
|
ClarifyingQuestion,
|
||||||
|
ErrorResponse,
|
||||||
|
ToolResponseBase,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class CreateAgentTool(BaseTool):
|
||||||
|
"""Tool for creating agents from natural language descriptions."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "create_agent"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Create a new agent workflow from a natural language description. "
|
||||||
|
"First generates a preview, then saves to library if save=true."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_long_running(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"description": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Natural language description of what the agent should do. "
|
||||||
|
"Be specific about inputs, outputs, and the workflow steps."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"context": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Additional context or answers to previous clarifying questions. "
|
||||||
|
"Include any preferences or constraints mentioned by the user."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"save": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": (
|
||||||
|
"Whether to save the agent to the user's library. "
|
||||||
|
"Default is true. Set to false for preview only."
|
||||||
|
),
|
||||||
|
"default": True,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": ["description"],
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
"""Execute the create_agent tool.
|
||||||
|
|
||||||
|
Flow:
|
||||||
|
1. Decompose the description into steps (may return clarifying questions)
|
||||||
|
2. Generate agent JSON (external service handles fixing and validation)
|
||||||
|
3. Preview or save based on the save parameter
|
||||||
|
"""
|
||||||
|
description = kwargs.get("description", "").strip()
|
||||||
|
context = kwargs.get("context", "")
|
||||||
|
save = kwargs.get("save", True)
|
||||||
|
session_id = session.session_id if session else None
|
||||||
|
|
||||||
|
# Extract async processing params (passed by long-running tool handler)
|
||||||
|
operation_id = kwargs.get("_operation_id")
|
||||||
|
task_id = kwargs.get("_task_id")
|
||||||
|
|
||||||
|
if not description:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide a description of what the agent should do.",
|
||||||
|
error="Missing description parameter",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
library_agents = None
|
||||||
|
if user_id:
|
||||||
|
try:
|
||||||
|
library_agents = await get_all_relevant_agents_for_generation(
|
||||||
|
user_id=user_id,
|
||||||
|
search_query=description,
|
||||||
|
include_marketplace=True,
|
||||||
|
)
|
||||||
|
logger.debug(
|
||||||
|
f"Found {len(library_agents)} relevant agents for sub-agent composition"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to fetch library agents: {e}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
decomposition_result = await decompose_goal(
|
||||||
|
description, context, library_agents
|
||||||
|
)
|
||||||
|
except AgentGeneratorNotConfiguredError:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
"Agent generation is not available. "
|
||||||
|
"The Agent Generator service is not configured."
|
||||||
|
),
|
||||||
|
error="service_not_configured",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if decomposition_result is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to analyze the goal. The agent generation service may be unavailable. Please try again.",
|
||||||
|
error="decomposition_failed",
|
||||||
|
details={"description": description[:100]},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if decomposition_result.get("type") == "error":
|
||||||
|
error_msg = decomposition_result.get("error", "Unknown error")
|
||||||
|
error_type = decomposition_result.get("error_type", "unknown")
|
||||||
|
user_message = get_user_message_for_error(
|
||||||
|
error_type,
|
||||||
|
operation="analyze the goal",
|
||||||
|
llm_parse_message="The AI had trouble understanding this request. Please try rephrasing your goal.",
|
||||||
|
)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=user_message,
|
||||||
|
error=f"decomposition_failed:{error_type}",
|
||||||
|
details={
|
||||||
|
"description": description[:100],
|
||||||
|
"service_error": error_msg,
|
||||||
|
"error_type": error_type,
|
||||||
|
},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if decomposition_result.get("type") == "clarifying_questions":
|
||||||
|
questions = decomposition_result.get("questions", [])
|
||||||
|
return ClarificationNeededResponse(
|
||||||
|
message=(
|
||||||
|
"I need some more information to create this agent. "
|
||||||
|
"Please answer the following questions:"
|
||||||
|
),
|
||||||
|
questions=[
|
||||||
|
ClarifyingQuestion(
|
||||||
|
question=q.get("question", ""),
|
||||||
|
keyword=q.get("keyword", ""),
|
||||||
|
example=q.get("example"),
|
||||||
|
)
|
||||||
|
for q in questions
|
||||||
|
],
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if decomposition_result.get("type") == "unachievable_goal":
|
||||||
|
suggested = decomposition_result.get("suggested_goal", "")
|
||||||
|
reason = decomposition_result.get("reason", "")
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
f"This goal cannot be accomplished with the available blocks. "
|
||||||
|
f"{reason} "
|
||||||
|
f"Suggestion: {suggested}"
|
||||||
|
),
|
||||||
|
error="unachievable_goal",
|
||||||
|
details={"suggested_goal": suggested, "reason": reason},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if decomposition_result.get("type") == "vague_goal":
|
||||||
|
suggested = decomposition_result.get("suggested_goal", "")
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
f"The goal is too vague to create a specific workflow. "
|
||||||
|
f"Suggestion: {suggested}"
|
||||||
|
),
|
||||||
|
error="vague_goal",
|
||||||
|
details={"suggested_goal": suggested},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if user_id and library_agents is not None:
|
||||||
|
try:
|
||||||
|
library_agents = await enrich_library_agents_from_steps(
|
||||||
|
user_id=user_id,
|
||||||
|
decomposition_result=decomposition_result,
|
||||||
|
existing_agents=library_agents,
|
||||||
|
include_marketplace=True,
|
||||||
|
)
|
||||||
|
logger.debug(
|
||||||
|
f"After enrichment: {len(library_agents)} total agents for sub-agent composition"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to enrich library agents from steps: {e}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
agent_json = await generate_agent(
|
||||||
|
decomposition_result,
|
||||||
|
library_agents,
|
||||||
|
operation_id=operation_id,
|
||||||
|
task_id=task_id,
|
||||||
|
)
|
||||||
|
except AgentGeneratorNotConfiguredError:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
"Agent generation is not available. "
|
||||||
|
"The Agent Generator service is not configured."
|
||||||
|
),
|
||||||
|
error="service_not_configured",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if agent_json is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to generate the agent. The agent generation service may be unavailable. Please try again.",
|
||||||
|
error="generation_failed",
|
||||||
|
details={"description": description[:100]},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if isinstance(agent_json, dict) and agent_json.get("type") == "error":
|
||||||
|
error_msg = agent_json.get("error", "Unknown error")
|
||||||
|
error_type = agent_json.get("error_type", "unknown")
|
||||||
|
user_message = get_user_message_for_error(
|
||||||
|
error_type,
|
||||||
|
operation="generate the agent",
|
||||||
|
llm_parse_message="The AI had trouble generating the agent. Please try again or simplify your goal.",
|
||||||
|
validation_message=(
|
||||||
|
"I wasn't able to create a valid agent for this request. "
|
||||||
|
"The generated workflow had some structural issues. "
|
||||||
|
"Please try simplifying your goal or breaking it into smaller steps."
|
||||||
|
),
|
||||||
|
error_details=error_msg,
|
||||||
|
)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=user_message,
|
||||||
|
error=f"generation_failed:{error_type}",
|
||||||
|
details={
|
||||||
|
"description": description[:100],
|
||||||
|
"service_error": error_msg,
|
||||||
|
"error_type": error_type,
|
||||||
|
},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if Agent Generator accepted for async processing
|
||||||
|
if agent_json.get("status") == "accepted":
|
||||||
|
logger.info(
|
||||||
|
f"Agent generation delegated to async processing "
|
||||||
|
f"(operation_id={operation_id}, task_id={task_id})"
|
||||||
|
)
|
||||||
|
return AsyncProcessingResponse(
|
||||||
|
message="Agent generation started. You'll be notified when it's complete.",
|
||||||
|
operation_id=operation_id,
|
||||||
|
task_id=task_id,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
agent_name = agent_json.get("name", "Generated Agent")
|
||||||
|
agent_description = agent_json.get("description", "")
|
||||||
|
node_count = len(agent_json.get("nodes", []))
|
||||||
|
link_count = len(agent_json.get("links", []))
|
||||||
|
|
||||||
|
if not save:
|
||||||
|
return AgentPreviewResponse(
|
||||||
|
message=(
|
||||||
|
f"I've generated an agent called '{agent_name}' with {node_count} blocks. "
|
||||||
|
f"Review it and call create_agent with save=true to save it to your library."
|
||||||
|
),
|
||||||
|
agent_json=agent_json,
|
||||||
|
agent_name=agent_name,
|
||||||
|
description=agent_description,
|
||||||
|
node_count=node_count,
|
||||||
|
link_count=link_count,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="You must be logged in to save agents.",
|
||||||
|
error="auth_required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
created_graph, library_agent = await save_agent_to_library(
|
||||||
|
agent_json, user_id
|
||||||
|
)
|
||||||
|
|
||||||
|
return AgentSavedResponse(
|
||||||
|
message=f"Agent '{created_graph.name}' has been saved to your library!",
|
||||||
|
agent_id=created_graph.id,
|
||||||
|
agent_name=created_graph.name,
|
||||||
|
library_agent_id=library_agent.id,
|
||||||
|
library_agent_link=f"/library/agents/{library_agent.id}",
|
||||||
|
agent_page_link=f"/build?flowID={created_graph.id}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to save the agent: {str(e)}",
|
||||||
|
error="save_failed",
|
||||||
|
details={"exception": str(e)},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
@@ -0,0 +1,337 @@
|
|||||||
|
"""CustomizeAgentTool - Customizes marketplace/template agents using natural language."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.api.features.store import db as store_db
|
||||||
|
from backend.api.features.store.exceptions import AgentNotFoundError
|
||||||
|
|
||||||
|
from .agent_generator import (
|
||||||
|
AgentGeneratorNotConfiguredError,
|
||||||
|
customize_template,
|
||||||
|
get_user_message_for_error,
|
||||||
|
graph_to_json,
|
||||||
|
save_agent_to_library,
|
||||||
|
)
|
||||||
|
from .base import BaseTool
|
||||||
|
from .models import (
|
||||||
|
AgentPreviewResponse,
|
||||||
|
AgentSavedResponse,
|
||||||
|
ClarificationNeededResponse,
|
||||||
|
ClarifyingQuestion,
|
||||||
|
ErrorResponse,
|
||||||
|
ToolResponseBase,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomizeAgentTool(BaseTool):
|
||||||
|
"""Tool for customizing marketplace/template agents using natural language."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "customize_agent"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Customize a marketplace or template agent using natural language. "
|
||||||
|
"Takes an existing agent from the marketplace and modifies it based on "
|
||||||
|
"the user's requirements before adding to their library."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_long_running(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"agent_id": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"The marketplace agent ID in format 'creator/slug' "
|
||||||
|
"(e.g., 'autogpt/newsletter-writer'). "
|
||||||
|
"Get this from find_agent results."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"modifications": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Natural language description of how to customize the agent. "
|
||||||
|
"Be specific about what changes you want to make."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"context": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Additional context or answers to previous clarifying questions."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"save": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": (
|
||||||
|
"Whether to save the customized agent to the user's library. "
|
||||||
|
"Default is true. Set to false for preview only."
|
||||||
|
),
|
||||||
|
"default": True,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": ["agent_id", "modifications"],
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
"""Execute the customize_agent tool.
|
||||||
|
|
||||||
|
Flow:
|
||||||
|
1. Parse the agent ID to get creator/slug
|
||||||
|
2. Fetch the template agent from the marketplace
|
||||||
|
3. Call customize_template with the modification request
|
||||||
|
4. Preview or save based on the save parameter
|
||||||
|
"""
|
||||||
|
agent_id = kwargs.get("agent_id", "").strip()
|
||||||
|
modifications = kwargs.get("modifications", "").strip()
|
||||||
|
context = kwargs.get("context", "")
|
||||||
|
save = kwargs.get("save", True)
|
||||||
|
session_id = session.session_id if session else None
|
||||||
|
|
||||||
|
if not agent_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide the marketplace agent ID (e.g., 'creator/agent-name').",
|
||||||
|
error="missing_agent_id",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not modifications:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please describe how you want to customize this agent.",
|
||||||
|
error="missing_modifications",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Parse agent_id in format "creator/slug"
|
||||||
|
parts = [p.strip() for p in agent_id.split("/")]
|
||||||
|
if len(parts) != 2 or not parts[0] or not parts[1]:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
f"Invalid agent ID format: '{agent_id}'. "
|
||||||
|
"Expected format is 'creator/agent-name' "
|
||||||
|
"(e.g., 'autogpt/newsletter-writer')."
|
||||||
|
),
|
||||||
|
error="invalid_agent_id_format",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
creator_username, agent_slug = parts
|
||||||
|
|
||||||
|
# Fetch the marketplace agent details
|
||||||
|
try:
|
||||||
|
agent_details = await store_db.get_store_agent_details(
|
||||||
|
username=creator_username, agent_name=agent_slug
|
||||||
|
)
|
||||||
|
except AgentNotFoundError:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
f"Could not find marketplace agent '{agent_id}'. "
|
||||||
|
"Please check the agent ID and try again."
|
||||||
|
),
|
||||||
|
error="agent_not_found",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error fetching marketplace agent {agent_id}: {e}")
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to fetch the marketplace agent. Please try again.",
|
||||||
|
error="fetch_error",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not agent_details.store_listing_version_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
f"The agent '{agent_id}' does not have an available version. "
|
||||||
|
"Please try a different agent."
|
||||||
|
),
|
||||||
|
error="no_version_available",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get the full agent graph
|
||||||
|
try:
|
||||||
|
graph = await store_db.get_agent(agent_details.store_listing_version_id)
|
||||||
|
template_agent = graph_to_json(graph)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error fetching agent graph for {agent_id}: {e}")
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to fetch the agent configuration. Please try again.",
|
||||||
|
error="graph_fetch_error",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Call customize_template
|
||||||
|
try:
|
||||||
|
result = await customize_template(
|
||||||
|
template_agent=template_agent,
|
||||||
|
modification_request=modifications,
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
except AgentGeneratorNotConfiguredError:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
"Agent customization is not available. "
|
||||||
|
"The Agent Generator service is not configured."
|
||||||
|
),
|
||||||
|
error="service_not_configured",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error calling customize_template for {agent_id}: {e}")
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
"Failed to customize the agent due to a service error. "
|
||||||
|
"Please try again."
|
||||||
|
),
|
||||||
|
error="customization_service_error",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
"Failed to customize the agent. "
|
||||||
|
"The agent generation service may be unavailable or timed out. "
|
||||||
|
"Please try again."
|
||||||
|
),
|
||||||
|
error="customization_failed",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Handle error response
|
||||||
|
if isinstance(result, dict) and result.get("type") == "error":
|
||||||
|
error_msg = result.get("error", "Unknown error")
|
||||||
|
error_type = result.get("error_type", "unknown")
|
||||||
|
user_message = get_user_message_for_error(
|
||||||
|
error_type,
|
||||||
|
operation="customize the agent",
|
||||||
|
llm_parse_message=(
|
||||||
|
"The AI had trouble customizing the agent. "
|
||||||
|
"Please try again or simplify your request."
|
||||||
|
),
|
||||||
|
validation_message=(
|
||||||
|
"The customized agent failed validation. "
|
||||||
|
"Please try rephrasing your request."
|
||||||
|
),
|
||||||
|
error_details=error_msg,
|
||||||
|
)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=user_message,
|
||||||
|
error=f"customization_failed:{error_type}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Handle clarifying questions
|
||||||
|
if isinstance(result, dict) and result.get("type") == "clarifying_questions":
|
||||||
|
questions = result.get("questions") or []
|
||||||
|
if not isinstance(questions, list):
|
||||||
|
logger.error(
|
||||||
|
f"Unexpected clarifying questions format: {type(questions)}"
|
||||||
|
)
|
||||||
|
questions = []
|
||||||
|
return ClarificationNeededResponse(
|
||||||
|
message=(
|
||||||
|
"I need some more information to customize this agent. "
|
||||||
|
"Please answer the following questions:"
|
||||||
|
),
|
||||||
|
questions=[
|
||||||
|
ClarifyingQuestion(
|
||||||
|
question=q.get("question", ""),
|
||||||
|
keyword=q.get("keyword", ""),
|
||||||
|
example=q.get("example"),
|
||||||
|
)
|
||||||
|
for q in questions
|
||||||
|
if isinstance(q, dict)
|
||||||
|
],
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Result should be the customized agent JSON
|
||||||
|
if not isinstance(result, dict):
|
||||||
|
logger.error(f"Unexpected customize_template response type: {type(result)}")
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to customize the agent due to an unexpected response.",
|
||||||
|
error="unexpected_response_type",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
customized_agent = result
|
||||||
|
|
||||||
|
agent_name = customized_agent.get(
|
||||||
|
"name", f"Customized {agent_details.agent_name}"
|
||||||
|
)
|
||||||
|
agent_description = customized_agent.get("description", "")
|
||||||
|
nodes = customized_agent.get("nodes")
|
||||||
|
links = customized_agent.get("links")
|
||||||
|
node_count = len(nodes) if isinstance(nodes, list) else 0
|
||||||
|
link_count = len(links) if isinstance(links, list) else 0
|
||||||
|
|
||||||
|
if not save:
|
||||||
|
return AgentPreviewResponse(
|
||||||
|
message=(
|
||||||
|
f"I've customized the agent '{agent_details.agent_name}'. "
|
||||||
|
f"The customized agent has {node_count} blocks. "
|
||||||
|
f"Review it and call customize_agent with save=true to save it."
|
||||||
|
),
|
||||||
|
agent_json=customized_agent,
|
||||||
|
agent_name=agent_name,
|
||||||
|
description=agent_description,
|
||||||
|
node_count=node_count,
|
||||||
|
link_count=link_count,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="You must be logged in to save agents.",
|
||||||
|
error="auth_required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save to user's library
|
||||||
|
try:
|
||||||
|
created_graph, library_agent = await save_agent_to_library(
|
||||||
|
customized_agent, user_id, is_update=False
|
||||||
|
)
|
||||||
|
|
||||||
|
return AgentSavedResponse(
|
||||||
|
message=(
|
||||||
|
f"Customized agent '{created_graph.name}' "
|
||||||
|
f"(based on '{agent_details.agent_name}') "
|
||||||
|
f"has been saved to your library!"
|
||||||
|
),
|
||||||
|
agent_id=created_graph.id,
|
||||||
|
agent_name=created_graph.name,
|
||||||
|
library_agent_id=library_agent.id,
|
||||||
|
library_agent_link=f"/library/agents/{library_agent.id}",
|
||||||
|
agent_page_link=f"/build?flowID={created_graph.id}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error saving customized agent: {e}")
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to save the customized agent. Please try again.",
|
||||||
|
error="save_failed",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
@@ -0,0 +1,284 @@
|
|||||||
|
"""EditAgentTool - Edits existing agents using natural language."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
|
||||||
|
from .agent_generator import (
|
||||||
|
AgentGeneratorNotConfiguredError,
|
||||||
|
generate_agent_patch,
|
||||||
|
get_agent_as_json,
|
||||||
|
get_all_relevant_agents_for_generation,
|
||||||
|
get_user_message_for_error,
|
||||||
|
save_agent_to_library,
|
||||||
|
)
|
||||||
|
from .base import BaseTool
|
||||||
|
from .models import (
|
||||||
|
AgentPreviewResponse,
|
||||||
|
AgentSavedResponse,
|
||||||
|
AsyncProcessingResponse,
|
||||||
|
ClarificationNeededResponse,
|
||||||
|
ClarifyingQuestion,
|
||||||
|
ErrorResponse,
|
||||||
|
ToolResponseBase,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class EditAgentTool(BaseTool):
|
||||||
|
"""Tool for editing existing agents using natural language."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "edit_agent"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Edit an existing agent from the user's library using natural language. "
|
||||||
|
"Generates updates to the agent while preserving unchanged parts."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_long_running(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"agent_id": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"The ID of the agent to edit. "
|
||||||
|
"Can be a graph ID or library agent ID."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"changes": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Natural language description of what changes to make. "
|
||||||
|
"Be specific about what to add, remove, or modify."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"context": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Additional context or answers to previous clarifying questions."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"save": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": (
|
||||||
|
"Whether to save the changes. "
|
||||||
|
"Default is true. Set to false for preview only."
|
||||||
|
),
|
||||||
|
"default": True,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": ["agent_id", "changes"],
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
"""Execute the edit_agent tool.
|
||||||
|
|
||||||
|
Flow:
|
||||||
|
1. Fetch the current agent
|
||||||
|
2. Generate updated agent (external service handles fixing and validation)
|
||||||
|
3. Preview or save based on the save parameter
|
||||||
|
"""
|
||||||
|
agent_id = kwargs.get("agent_id", "").strip()
|
||||||
|
changes = kwargs.get("changes", "").strip()
|
||||||
|
context = kwargs.get("context", "")
|
||||||
|
save = kwargs.get("save", True)
|
||||||
|
session_id = session.session_id if session else None
|
||||||
|
|
||||||
|
# Extract async processing params (passed by long-running tool handler)
|
||||||
|
operation_id = kwargs.get("_operation_id")
|
||||||
|
task_id = kwargs.get("_task_id")
|
||||||
|
|
||||||
|
if not agent_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide the agent ID to edit.",
|
||||||
|
error="Missing agent_id parameter",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not changes:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please describe what changes you want to make.",
|
||||||
|
error="Missing changes parameter",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
current_agent = await get_agent_as_json(agent_id, user_id)
|
||||||
|
|
||||||
|
if current_agent is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Could not find agent with ID '{agent_id}' in your library.",
|
||||||
|
error="agent_not_found",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
library_agents = None
|
||||||
|
if user_id:
|
||||||
|
try:
|
||||||
|
graph_id = current_agent.get("id")
|
||||||
|
library_agents = await get_all_relevant_agents_for_generation(
|
||||||
|
user_id=user_id,
|
||||||
|
search_query=changes,
|
||||||
|
exclude_graph_id=graph_id,
|
||||||
|
include_marketplace=True,
|
||||||
|
)
|
||||||
|
logger.debug(
|
||||||
|
f"Found {len(library_agents)} relevant agents for sub-agent composition"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to fetch library agents: {e}")
|
||||||
|
|
||||||
|
update_request = changes
|
||||||
|
if context:
|
||||||
|
update_request = f"{changes}\n\nAdditional context:\n{context}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = await generate_agent_patch(
|
||||||
|
update_request,
|
||||||
|
current_agent,
|
||||||
|
library_agents,
|
||||||
|
operation_id=operation_id,
|
||||||
|
task_id=task_id,
|
||||||
|
)
|
||||||
|
except AgentGeneratorNotConfiguredError:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
"Agent editing is not available. "
|
||||||
|
"The Agent Generator service is not configured."
|
||||||
|
),
|
||||||
|
error="service_not_configured",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Failed to generate changes. The agent generation service may be unavailable or timed out. Please try again.",
|
||||||
|
error="update_generation_failed",
|
||||||
|
details={"agent_id": agent_id, "changes": changes[:100]},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if Agent Generator accepted for async processing
|
||||||
|
if result.get("status") == "accepted":
|
||||||
|
logger.info(
|
||||||
|
f"Agent edit delegated to async processing "
|
||||||
|
f"(operation_id={operation_id}, task_id={task_id})"
|
||||||
|
)
|
||||||
|
return AsyncProcessingResponse(
|
||||||
|
message="Agent edit started. You'll be notified when it's complete.",
|
||||||
|
operation_id=operation_id,
|
||||||
|
task_id=task_id,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if the result is an error from the external service
|
||||||
|
if isinstance(result, dict) and result.get("type") == "error":
|
||||||
|
error_msg = result.get("error", "Unknown error")
|
||||||
|
error_type = result.get("error_type", "unknown")
|
||||||
|
user_message = get_user_message_for_error(
|
||||||
|
error_type,
|
||||||
|
operation="generate the changes",
|
||||||
|
llm_parse_message="The AI had trouble generating the changes. Please try again or simplify your request.",
|
||||||
|
validation_message="The generated changes failed validation. Please try rephrasing your request.",
|
||||||
|
error_details=error_msg,
|
||||||
|
)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=user_message,
|
||||||
|
error=f"update_generation_failed:{error_type}",
|
||||||
|
details={
|
||||||
|
"agent_id": agent_id,
|
||||||
|
"changes": changes[:100],
|
||||||
|
"service_error": error_msg,
|
||||||
|
"error_type": error_type,
|
||||||
|
},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.get("type") == "clarifying_questions":
|
||||||
|
questions = result.get("questions", [])
|
||||||
|
return ClarificationNeededResponse(
|
||||||
|
message=(
|
||||||
|
"I need some more information about the changes. "
|
||||||
|
"Please answer the following questions:"
|
||||||
|
),
|
||||||
|
questions=[
|
||||||
|
ClarifyingQuestion(
|
||||||
|
question=q.get("question", ""),
|
||||||
|
keyword=q.get("keyword", ""),
|
||||||
|
example=q.get("example"),
|
||||||
|
)
|
||||||
|
for q in questions
|
||||||
|
],
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
updated_agent = result
|
||||||
|
|
||||||
|
agent_name = updated_agent.get("name", "Updated Agent")
|
||||||
|
agent_description = updated_agent.get("description", "")
|
||||||
|
node_count = len(updated_agent.get("nodes", []))
|
||||||
|
link_count = len(updated_agent.get("links", []))
|
||||||
|
|
||||||
|
if not save:
|
||||||
|
return AgentPreviewResponse(
|
||||||
|
message=(
|
||||||
|
f"I've updated the agent. "
|
||||||
|
f"The agent now has {node_count} blocks. "
|
||||||
|
f"Review it and call edit_agent with save=true to save the changes."
|
||||||
|
),
|
||||||
|
agent_json=updated_agent,
|
||||||
|
agent_name=agent_name,
|
||||||
|
description=agent_description,
|
||||||
|
node_count=node_count,
|
||||||
|
link_count=link_count,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="You must be logged in to save agents.",
|
||||||
|
error="auth_required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
created_graph, library_agent = await save_agent_to_library(
|
||||||
|
updated_agent, user_id, is_update=True
|
||||||
|
)
|
||||||
|
|
||||||
|
return AgentSavedResponse(
|
||||||
|
message=f"Updated agent '{created_graph.name}' has been saved to your library!",
|
||||||
|
agent_id=created_graph.id,
|
||||||
|
agent_name=created_graph.name,
|
||||||
|
library_agent_id=library_agent.id,
|
||||||
|
library_agent_link=f"/library/agents/{library_agent.id}",
|
||||||
|
agent_page_link=f"/build?flowID={created_graph.id}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to save the updated agent: {str(e)}",
|
||||||
|
error="save_failed",
|
||||||
|
details={"exception": str(e)},
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
|
||||||
from .agent_search import search_agents
|
from .agent_search import search_agents
|
||||||
from .base import BaseTool
|
from .base import BaseTool
|
||||||
@@ -3,18 +3,17 @@ from typing import Any
|
|||||||
|
|
||||||
from prisma.enums import ContentType
|
from prisma.enums import ContentType
|
||||||
|
|
||||||
from backend.blocks import get_block
|
from backend.api.features.chat.model import ChatSession
|
||||||
from backend.blocks._base import BlockType
|
from backend.api.features.chat.tools.base import BaseTool, ToolResponseBase
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.tools.models import (
|
||||||
from backend.data.db_accessors import search
|
|
||||||
|
|
||||||
from .base import BaseTool, ToolResponseBase
|
|
||||||
from .models import (
|
|
||||||
BlockInfoSummary,
|
BlockInfoSummary,
|
||||||
|
BlockInputFieldInfo,
|
||||||
BlockListResponse,
|
BlockListResponse,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
NoResultsResponse,
|
NoResultsResponse,
|
||||||
)
|
)
|
||||||
|
from backend.api.features.store.hybrid_search import unified_hybrid_search
|
||||||
|
from backend.data.block import BlockType, get_block
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -32,7 +31,6 @@ COPILOT_EXCLUDED_BLOCK_TYPES = {
|
|||||||
BlockType.NOTE, # Visual annotation only - no runtime behavior
|
BlockType.NOTE, # Visual annotation only - no runtime behavior
|
||||||
BlockType.HUMAN_IN_THE_LOOP, # Pauses for human approval - CoPilot IS human-in-the-loop
|
BlockType.HUMAN_IN_THE_LOOP, # Pauses for human approval - CoPilot IS human-in-the-loop
|
||||||
BlockType.AGENT, # AgentExecutorBlock requires execution_context - use run_agent tool
|
BlockType.AGENT, # AgentExecutorBlock requires execution_context - use run_agent tool
|
||||||
BlockType.MCP_TOOL, # Has dedicated run_mcp_tool tool with discovery + auth flow
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Specific block IDs excluded from CoPilot (STANDARD type but still require graph context)
|
# Specific block IDs excluded from CoPilot (STANDARD type but still require graph context)
|
||||||
@@ -56,8 +54,7 @@ class FindBlockTool(BaseTool):
|
|||||||
"Blocks are reusable components that perform specific tasks like "
|
"Blocks are reusable components that perform specific tasks like "
|
||||||
"sending emails, making API calls, processing text, etc. "
|
"sending emails, making API calls, processing text, etc. "
|
||||||
"IMPORTANT: Use this tool FIRST to get the block's 'id' before calling run_block. "
|
"IMPORTANT: Use this tool FIRST to get the block's 'id' before calling run_block. "
|
||||||
"The response includes each block's id, name, and description. "
|
"The response includes each block's id, required_inputs, and input_schema."
|
||||||
"Call run_block with the block's id **with no inputs** to see detailed inputs/outputs and execute it."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -72,15 +69,6 @@ class FindBlockTool(BaseTool):
|
|||||||
"Use keywords like 'email', 'http', 'text', 'ai', etc."
|
"Use keywords like 'email', 'http', 'text', 'ai', etc."
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
"include_schemas": {
|
|
||||||
"type": "boolean",
|
|
||||||
"description": (
|
|
||||||
"If true, include full input_schema and output_schema "
|
|
||||||
"for each block. Use when generating agent JSON that "
|
|
||||||
"needs block schemas. Default is false."
|
|
||||||
),
|
|
||||||
"default": False,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
"required": ["query"],
|
"required": ["query"],
|
||||||
}
|
}
|
||||||
@@ -108,7 +96,6 @@ class FindBlockTool(BaseTool):
|
|||||||
ErrorResponse: Error message
|
ErrorResponse: Error message
|
||||||
"""
|
"""
|
||||||
query = kwargs.get("query", "").strip()
|
query = kwargs.get("query", "").strip()
|
||||||
include_schemas = kwargs.get("include_schemas", False)
|
|
||||||
session_id = session.session_id
|
session_id = session.session_id
|
||||||
|
|
||||||
if not query:
|
if not query:
|
||||||
@@ -119,7 +106,7 @@ class FindBlockTool(BaseTool):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Search for blocks using hybrid search
|
# Search for blocks using hybrid search
|
||||||
results, total = await search().unified_hybrid_search(
|
results, total = await unified_hybrid_search(
|
||||||
query=query,
|
query=query,
|
||||||
content_types=[ContentType.BLOCK],
|
content_types=[ContentType.BLOCK],
|
||||||
page=1,
|
page=1,
|
||||||
@@ -136,7 +123,7 @@ class FindBlockTool(BaseTool):
|
|||||||
session_id=session_id,
|
session_id=session_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Enrich results with block information
|
# Enrich results with full block information
|
||||||
blocks: list[BlockInfoSummary] = []
|
blocks: list[BlockInfoSummary] = []
|
||||||
for result in results:
|
for result in results:
|
||||||
block_id = result["content_id"]
|
block_id = result["content_id"]
|
||||||
@@ -153,21 +140,68 @@ class FindBlockTool(BaseTool):
|
|||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
summary = BlockInfoSummary(
|
# Get input/output schemas
|
||||||
id=block_id,
|
input_schema = {}
|
||||||
name=block.name,
|
output_schema = {}
|
||||||
description=block.optimized_description or block.description or "",
|
try:
|
||||||
categories=[c.value for c in block.categories],
|
input_schema = block.input_schema.jsonschema()
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(
|
||||||
|
"Failed to generate input schema for block %s: %s",
|
||||||
|
block_id,
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
output_schema = block.output_schema.jsonschema()
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(
|
||||||
|
"Failed to generate output schema for block %s: %s",
|
||||||
|
block_id,
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get categories from block instance
|
||||||
|
categories = []
|
||||||
|
if hasattr(block, "categories") and block.categories:
|
||||||
|
categories = [cat.value for cat in block.categories]
|
||||||
|
|
||||||
|
# Extract required inputs for easier use
|
||||||
|
required_inputs: list[BlockInputFieldInfo] = []
|
||||||
|
if input_schema:
|
||||||
|
properties = input_schema.get("properties", {})
|
||||||
|
required_fields = set(input_schema.get("required", []))
|
||||||
|
# Get credential field names to exclude from required inputs
|
||||||
|
credentials_fields = set(
|
||||||
|
block.input_schema.get_credentials_fields().keys()
|
||||||
|
)
|
||||||
|
|
||||||
|
for field_name, field_schema in properties.items():
|
||||||
|
# Skip credential fields - they're handled separately
|
||||||
|
if field_name in credentials_fields:
|
||||||
|
continue
|
||||||
|
|
||||||
|
required_inputs.append(
|
||||||
|
BlockInputFieldInfo(
|
||||||
|
name=field_name,
|
||||||
|
type=field_schema.get("type", "string"),
|
||||||
|
description=field_schema.get("description", ""),
|
||||||
|
required=field_name in required_fields,
|
||||||
|
default=field_schema.get("default"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
blocks.append(
|
||||||
|
BlockInfoSummary(
|
||||||
|
id=block_id,
|
||||||
|
name=block.name,
|
||||||
|
description=block.description or "",
|
||||||
|
categories=categories,
|
||||||
|
input_schema=input_schema,
|
||||||
|
output_schema=output_schema,
|
||||||
|
required_inputs=required_inputs,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if include_schemas:
|
|
||||||
info = block.get_info()
|
|
||||||
summary.input_schema = info.inputSchema
|
|
||||||
summary.output_schema = info.outputSchema
|
|
||||||
summary.static_output = info.staticOutput
|
|
||||||
|
|
||||||
blocks.append(summary)
|
|
||||||
|
|
||||||
if len(blocks) >= _TARGET_RESULTS:
|
if len(blocks) >= _TARGET_RESULTS:
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -193,7 +227,8 @@ class FindBlockTool(BaseTool):
|
|||||||
return BlockListResponse(
|
return BlockListResponse(
|
||||||
message=(
|
message=(
|
||||||
f"Found {len(blocks)} block(s) matching '{query}'. "
|
f"Found {len(blocks)} block(s) matching '{query}'. "
|
||||||
"To see a block's inputs/outputs and execute it, use run_block with the block's 'id' - providing no inputs."
|
"To execute a block, use run_block with the block's 'id' field "
|
||||||
|
"and provide 'input_data' matching the block's input_schema."
|
||||||
),
|
),
|
||||||
blocks=blocks,
|
blocks=blocks,
|
||||||
count=len(blocks),
|
count=len(blocks),
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
"""Tests for block filtering in FindBlockTool."""
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from backend.api.features.chat.tools.find_block import (
|
||||||
|
COPILOT_EXCLUDED_BLOCK_IDS,
|
||||||
|
COPILOT_EXCLUDED_BLOCK_TYPES,
|
||||||
|
FindBlockTool,
|
||||||
|
)
|
||||||
|
from backend.api.features.chat.tools.models import BlockListResponse
|
||||||
|
from backend.data.block import BlockType
|
||||||
|
|
||||||
|
from ._test_data import make_session
|
||||||
|
|
||||||
|
_TEST_USER_ID = "test-user-find-block"
|
||||||
|
|
||||||
|
|
||||||
|
def make_mock_block(
|
||||||
|
block_id: str, name: str, block_type: BlockType, disabled: bool = False
|
||||||
|
):
|
||||||
|
"""Create a mock block for testing."""
|
||||||
|
mock = MagicMock()
|
||||||
|
mock.id = block_id
|
||||||
|
mock.name = name
|
||||||
|
mock.description = f"{name} description"
|
||||||
|
mock.block_type = block_type
|
||||||
|
mock.disabled = disabled
|
||||||
|
mock.input_schema = MagicMock()
|
||||||
|
mock.input_schema.jsonschema.return_value = {"properties": {}, "required": []}
|
||||||
|
mock.input_schema.get_credentials_fields.return_value = {}
|
||||||
|
mock.output_schema = MagicMock()
|
||||||
|
mock.output_schema.jsonschema.return_value = {}
|
||||||
|
mock.categories = []
|
||||||
|
return mock
|
||||||
|
|
||||||
|
|
||||||
|
class TestFindBlockFiltering:
|
||||||
|
"""Tests for block filtering in FindBlockTool."""
|
||||||
|
|
||||||
|
def test_excluded_block_types_contains_expected_types(self):
|
||||||
|
"""Verify COPILOT_EXCLUDED_BLOCK_TYPES contains all graph-only types."""
|
||||||
|
assert BlockType.INPUT in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
assert BlockType.OUTPUT in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
assert BlockType.WEBHOOK in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
assert BlockType.WEBHOOK_MANUAL in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
assert BlockType.NOTE in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
assert BlockType.HUMAN_IN_THE_LOOP in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
assert BlockType.AGENT in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
|
||||||
|
def test_excluded_block_ids_contains_smart_decision_maker(self):
|
||||||
|
"""Verify SmartDecisionMakerBlock is in COPILOT_EXCLUDED_BLOCK_IDS."""
|
||||||
|
assert "3b191d9f-356f-482d-8238-ba04b6d18381" in COPILOT_EXCLUDED_BLOCK_IDS
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_excluded_block_type_filtered_from_results(self):
|
||||||
|
"""Verify blocks with excluded BlockTypes are filtered from search results."""
|
||||||
|
session = make_session(user_id=_TEST_USER_ID)
|
||||||
|
|
||||||
|
# Mock search returns an INPUT block (excluded) and a STANDARD block (included)
|
||||||
|
search_results = [
|
||||||
|
{"content_id": "input-block-id", "score": 0.9},
|
||||||
|
{"content_id": "standard-block-id", "score": 0.8},
|
||||||
|
]
|
||||||
|
|
||||||
|
input_block = make_mock_block("input-block-id", "Input Block", BlockType.INPUT)
|
||||||
|
standard_block = make_mock_block(
|
||||||
|
"standard-block-id", "HTTP Request", BlockType.STANDARD
|
||||||
|
)
|
||||||
|
|
||||||
|
def mock_get_block(block_id):
|
||||||
|
return {
|
||||||
|
"input-block-id": input_block,
|
||||||
|
"standard-block-id": standard_block,
|
||||||
|
}.get(block_id)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.find_block.unified_hybrid_search",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value=(search_results, 2),
|
||||||
|
):
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.find_block.get_block",
|
||||||
|
side_effect=mock_get_block,
|
||||||
|
):
|
||||||
|
tool = FindBlockTool()
|
||||||
|
response = await tool._execute(
|
||||||
|
user_id=_TEST_USER_ID, session=session, query="test"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Should only return the standard block, not the INPUT block
|
||||||
|
assert isinstance(response, BlockListResponse)
|
||||||
|
assert len(response.blocks) == 1
|
||||||
|
assert response.blocks[0].id == "standard-block-id"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_excluded_block_id_filtered_from_results(self):
|
||||||
|
"""Verify SmartDecisionMakerBlock is filtered from search results."""
|
||||||
|
session = make_session(user_id=_TEST_USER_ID)
|
||||||
|
|
||||||
|
smart_decision_id = "3b191d9f-356f-482d-8238-ba04b6d18381"
|
||||||
|
search_results = [
|
||||||
|
{"content_id": smart_decision_id, "score": 0.9},
|
||||||
|
{"content_id": "normal-block-id", "score": 0.8},
|
||||||
|
]
|
||||||
|
|
||||||
|
# SmartDecisionMakerBlock has STANDARD type but is excluded by ID
|
||||||
|
smart_block = make_mock_block(
|
||||||
|
smart_decision_id, "Smart Decision Maker", BlockType.STANDARD
|
||||||
|
)
|
||||||
|
normal_block = make_mock_block(
|
||||||
|
"normal-block-id", "Normal Block", BlockType.STANDARD
|
||||||
|
)
|
||||||
|
|
||||||
|
def mock_get_block(block_id):
|
||||||
|
return {
|
||||||
|
smart_decision_id: smart_block,
|
||||||
|
"normal-block-id": normal_block,
|
||||||
|
}.get(block_id)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.find_block.unified_hybrid_search",
|
||||||
|
new_callable=AsyncMock,
|
||||||
|
return_value=(search_results, 2),
|
||||||
|
):
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.find_block.get_block",
|
||||||
|
side_effect=mock_get_block,
|
||||||
|
):
|
||||||
|
tool = FindBlockTool()
|
||||||
|
response = await tool._execute(
|
||||||
|
user_id=_TEST_USER_ID, session=session, query="decision"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Should only return normal block, not SmartDecisionMakerBlock
|
||||||
|
assert isinstance(response, BlockListResponse)
|
||||||
|
assert len(response.blocks) == 1
|
||||||
|
assert response.blocks[0].id == "normal-block-id"
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
|
||||||
from .agent_search import search_agents
|
from .agent_search import search_agents
|
||||||
from .base import BaseTool
|
from .base import BaseTool
|
||||||
@@ -19,13 +19,9 @@ class FindLibraryAgentTool(BaseTool):
|
|||||||
@property
|
@property
|
||||||
def description(self) -> str:
|
def description(self) -> str:
|
||||||
return (
|
return (
|
||||||
"Search for or list agents in the user's library. Use this to find "
|
"Search for agents in the user's library. Use this to find agents "
|
||||||
"agents the user has already added to their library, including agents "
|
"the user has already added to their library, including agents they "
|
||||||
"they created or added from the marketplace. "
|
"created or added from the marketplace."
|
||||||
"When creating agents with sub-agent composition, use this to get "
|
|
||||||
"the agent's graph_id, graph_version, input_schema, and output_schema "
|
|
||||||
"needed for AgentExecutorBlock nodes. "
|
|
||||||
"Omit the query to list all agents."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -35,13 +31,10 @@ class FindLibraryAgentTool(BaseTool):
|
|||||||
"properties": {
|
"properties": {
|
||||||
"query": {
|
"query": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": (
|
"description": "Search query to find agents by name or description.",
|
||||||
"Search query to find agents by name or description. "
|
|
||||||
"Omit to list all agents in the library."
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"required": [],
|
"required": ["query"],
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -52,7 +45,7 @@ class FindLibraryAgentTool(BaseTool):
|
|||||||
self, user_id: str | None, session: ChatSession, **kwargs
|
self, user_id: str | None, session: ChatSession, **kwargs
|
||||||
) -> ToolResponseBase:
|
) -> ToolResponseBase:
|
||||||
return await search_agents(
|
return await search_agents(
|
||||||
query=(kwargs.get("query") or "").strip(),
|
query=kwargs.get("query", "").strip(),
|
||||||
source="library",
|
source="library",
|
||||||
session_id=session.session_id,
|
session_id=session.session_id,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
@@ -4,10 +4,13 @@ import logging
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.api.features.chat.tools.base import BaseTool
|
||||||
from .base import BaseTool
|
from backend.api.features.chat.tools.models import (
|
||||||
from .models import DocPageResponse, ErrorResponse, ToolResponseBase
|
DocPageResponse,
|
||||||
|
ErrorResponse,
|
||||||
|
ToolResponseBase,
|
||||||
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
"""Shared helpers for chat tools."""
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
def get_inputs_from_schema(
|
||||||
|
input_schema: dict[str, Any],
|
||||||
|
exclude_fields: set[str] | None = None,
|
||||||
|
) -> list[dict[str, Any]]:
|
||||||
|
"""Extract input field info from JSON schema."""
|
||||||
|
if not isinstance(input_schema, dict):
|
||||||
|
return []
|
||||||
|
|
||||||
|
exclude = exclude_fields or set()
|
||||||
|
properties = input_schema.get("properties", {})
|
||||||
|
required = set(input_schema.get("required", []))
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"name": name,
|
||||||
|
"title": schema.get("title", name),
|
||||||
|
"type": schema.get("type", "string"),
|
||||||
|
"description": schema.get("description", ""),
|
||||||
|
"required": name in required,
|
||||||
|
"default": schema.get("default"),
|
||||||
|
}
|
||||||
|
for name, schema in properties.items()
|
||||||
|
if name not in exclude
|
||||||
|
]
|
||||||
@@ -0,0 +1,423 @@
|
|||||||
|
"""Pydantic models for tool responses."""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from backend.data.model import CredentialsMetaInput
|
||||||
|
|
||||||
|
|
||||||
|
class ResponseType(str, Enum):
|
||||||
|
"""Types of tool responses."""
|
||||||
|
|
||||||
|
AGENTS_FOUND = "agents_found"
|
||||||
|
AGENT_DETAILS = "agent_details"
|
||||||
|
SETUP_REQUIREMENTS = "setup_requirements"
|
||||||
|
EXECUTION_STARTED = "execution_started"
|
||||||
|
NEED_LOGIN = "need_login"
|
||||||
|
ERROR = "error"
|
||||||
|
NO_RESULTS = "no_results"
|
||||||
|
AGENT_OUTPUT = "agent_output"
|
||||||
|
UNDERSTANDING_UPDATED = "understanding_updated"
|
||||||
|
AGENT_PREVIEW = "agent_preview"
|
||||||
|
AGENT_SAVED = "agent_saved"
|
||||||
|
CLARIFICATION_NEEDED = "clarification_needed"
|
||||||
|
BLOCK_LIST = "block_list"
|
||||||
|
BLOCK_OUTPUT = "block_output"
|
||||||
|
DOC_SEARCH_RESULTS = "doc_search_results"
|
||||||
|
DOC_PAGE = "doc_page"
|
||||||
|
# Workspace response types
|
||||||
|
WORKSPACE_FILE_LIST = "workspace_file_list"
|
||||||
|
WORKSPACE_FILE_CONTENT = "workspace_file_content"
|
||||||
|
WORKSPACE_FILE_METADATA = "workspace_file_metadata"
|
||||||
|
WORKSPACE_FILE_WRITTEN = "workspace_file_written"
|
||||||
|
WORKSPACE_FILE_DELETED = "workspace_file_deleted"
|
||||||
|
# Long-running operation types
|
||||||
|
OPERATION_STARTED = "operation_started"
|
||||||
|
OPERATION_PENDING = "operation_pending"
|
||||||
|
OPERATION_IN_PROGRESS = "operation_in_progress"
|
||||||
|
# Input validation
|
||||||
|
INPUT_VALIDATION_ERROR = "input_validation_error"
|
||||||
|
|
||||||
|
|
||||||
|
# Base response model
|
||||||
|
class ToolResponseBase(BaseModel):
|
||||||
|
"""Base model for all tool responses."""
|
||||||
|
|
||||||
|
type: ResponseType
|
||||||
|
message: str
|
||||||
|
session_id: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# Agent discovery models
|
||||||
|
class AgentInfo(BaseModel):
|
||||||
|
"""Information about an agent."""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
source: str = Field(description="marketplace or library")
|
||||||
|
in_library: bool = False
|
||||||
|
creator: str | None = None
|
||||||
|
category: str | None = None
|
||||||
|
rating: float | None = None
|
||||||
|
runs: int | None = None
|
||||||
|
is_featured: bool | None = None
|
||||||
|
status: str | None = None
|
||||||
|
can_access_graph: bool | None = None
|
||||||
|
has_external_trigger: bool | None = None
|
||||||
|
new_output: bool | None = None
|
||||||
|
graph_id: str | None = None
|
||||||
|
inputs: dict[str, Any] | None = Field(
|
||||||
|
default=None,
|
||||||
|
description="Input schema for the agent, including field names, types, and defaults",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AgentsFoundResponse(ToolResponseBase):
|
||||||
|
"""Response for find_agent tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.AGENTS_FOUND
|
||||||
|
title: str = "Available Agents"
|
||||||
|
agents: list[AgentInfo]
|
||||||
|
count: int
|
||||||
|
name: str = "agents_found"
|
||||||
|
|
||||||
|
|
||||||
|
class NoResultsResponse(ToolResponseBase):
|
||||||
|
"""Response when no agents found."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.NO_RESULTS
|
||||||
|
suggestions: list[str] = []
|
||||||
|
name: str = "no_results"
|
||||||
|
|
||||||
|
|
||||||
|
# Agent details models
|
||||||
|
class InputField(BaseModel):
|
||||||
|
"""Input field specification."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
type: str = "string"
|
||||||
|
description: str = ""
|
||||||
|
required: bool = False
|
||||||
|
default: Any | None = None
|
||||||
|
options: list[Any] | None = None
|
||||||
|
format: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class ExecutionOptions(BaseModel):
|
||||||
|
"""Available execution options for an agent."""
|
||||||
|
|
||||||
|
manual: bool = True
|
||||||
|
scheduled: bool = True
|
||||||
|
webhook: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class AgentDetails(BaseModel):
|
||||||
|
"""Detailed agent information."""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
in_library: bool = False
|
||||||
|
inputs: dict[str, Any] = {}
|
||||||
|
credentials: list[CredentialsMetaInput] = []
|
||||||
|
execution_options: ExecutionOptions = Field(default_factory=ExecutionOptions)
|
||||||
|
trigger_info: dict[str, Any] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class AgentDetailsResponse(ToolResponseBase):
|
||||||
|
"""Response for get_details action."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.AGENT_DETAILS
|
||||||
|
agent: AgentDetails
|
||||||
|
user_authenticated: bool = False
|
||||||
|
graph_id: str | None = None
|
||||||
|
graph_version: int | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# Setup info models
|
||||||
|
class UserReadiness(BaseModel):
|
||||||
|
"""User readiness status."""
|
||||||
|
|
||||||
|
has_all_credentials: bool = False
|
||||||
|
missing_credentials: dict[str, Any] = {}
|
||||||
|
ready_to_run: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class SetupInfo(BaseModel):
|
||||||
|
"""Complete setup information."""
|
||||||
|
|
||||||
|
agent_id: str
|
||||||
|
agent_name: str
|
||||||
|
requirements: dict[str, list[Any]] = Field(
|
||||||
|
default_factory=lambda: {
|
||||||
|
"credentials": [],
|
||||||
|
"inputs": [],
|
||||||
|
"execution_modes": [],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
user_readiness: UserReadiness = Field(default_factory=UserReadiness)
|
||||||
|
|
||||||
|
|
||||||
|
class SetupRequirementsResponse(ToolResponseBase):
|
||||||
|
"""Response for validate action."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.SETUP_REQUIREMENTS
|
||||||
|
setup_info: SetupInfo
|
||||||
|
graph_id: str | None = None
|
||||||
|
graph_version: int | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# Execution models
|
||||||
|
class ExecutionStartedResponse(ToolResponseBase):
|
||||||
|
"""Response for run/schedule actions."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.EXECUTION_STARTED
|
||||||
|
execution_id: str
|
||||||
|
graph_id: str
|
||||||
|
graph_name: str
|
||||||
|
library_agent_id: str | None = None
|
||||||
|
library_agent_link: str | None = None
|
||||||
|
status: str = "QUEUED"
|
||||||
|
|
||||||
|
|
||||||
|
# Auth/error models
|
||||||
|
class NeedLoginResponse(ToolResponseBase):
|
||||||
|
"""Response when login is needed."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.NEED_LOGIN
|
||||||
|
agent_info: dict[str, Any] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorResponse(ToolResponseBase):
|
||||||
|
"""Response for errors."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.ERROR
|
||||||
|
error: str | None = None
|
||||||
|
details: dict[str, Any] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class InputValidationErrorResponse(ToolResponseBase):
|
||||||
|
"""Response when run_agent receives unknown input fields."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.INPUT_VALIDATION_ERROR
|
||||||
|
unrecognized_fields: list[str] = Field(
|
||||||
|
description="List of input field names that were not recognized"
|
||||||
|
)
|
||||||
|
inputs: dict[str, Any] = Field(
|
||||||
|
description="The agent's valid input schema for reference"
|
||||||
|
)
|
||||||
|
graph_id: str | None = None
|
||||||
|
graph_version: int | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# Agent output models
|
||||||
|
class ExecutionOutputInfo(BaseModel):
|
||||||
|
"""Summary of a single execution's outputs."""
|
||||||
|
|
||||||
|
execution_id: str
|
||||||
|
status: str
|
||||||
|
started_at: datetime | None = None
|
||||||
|
ended_at: datetime | None = None
|
||||||
|
outputs: dict[str, list[Any]]
|
||||||
|
inputs_summary: dict[str, Any] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class AgentOutputResponse(ToolResponseBase):
|
||||||
|
"""Response for agent_output tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.AGENT_OUTPUT
|
||||||
|
agent_name: str
|
||||||
|
agent_id: str
|
||||||
|
library_agent_id: str | None = None
|
||||||
|
library_agent_link: str | None = None
|
||||||
|
execution: ExecutionOutputInfo | None = None
|
||||||
|
available_executions: list[dict[str, Any]] | None = None
|
||||||
|
total_executions: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
# Business understanding models
|
||||||
|
class UnderstandingUpdatedResponse(ToolResponseBase):
|
||||||
|
"""Response for add_understanding tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.UNDERSTANDING_UPDATED
|
||||||
|
updated_fields: list[str] = Field(default_factory=list)
|
||||||
|
current_understanding: dict[str, Any] = Field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
|
# Agent generation models
|
||||||
|
class ClarifyingQuestion(BaseModel):
|
||||||
|
"""A question that needs user clarification."""
|
||||||
|
|
||||||
|
question: str
|
||||||
|
keyword: str
|
||||||
|
example: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class AgentPreviewResponse(ToolResponseBase):
|
||||||
|
"""Response for previewing a generated agent before saving."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.AGENT_PREVIEW
|
||||||
|
agent_json: dict[str, Any]
|
||||||
|
agent_name: str
|
||||||
|
description: str
|
||||||
|
node_count: int
|
||||||
|
link_count: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
class AgentSavedResponse(ToolResponseBase):
|
||||||
|
"""Response when an agent is saved to the library."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.AGENT_SAVED
|
||||||
|
agent_id: str
|
||||||
|
agent_name: str
|
||||||
|
library_agent_id: str
|
||||||
|
library_agent_link: str
|
||||||
|
agent_page_link: str # Link to the agent builder/editor page
|
||||||
|
|
||||||
|
|
||||||
|
class ClarificationNeededResponse(ToolResponseBase):
|
||||||
|
"""Response when the LLM needs more information from the user."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.CLARIFICATION_NEEDED
|
||||||
|
questions: list[ClarifyingQuestion] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
# Documentation search models
|
||||||
|
class DocSearchResult(BaseModel):
|
||||||
|
"""A single documentation search result."""
|
||||||
|
|
||||||
|
title: str
|
||||||
|
path: str
|
||||||
|
section: str
|
||||||
|
snippet: str # Short excerpt for UI display
|
||||||
|
score: float
|
||||||
|
doc_url: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class DocSearchResultsResponse(ToolResponseBase):
|
||||||
|
"""Response for search_docs tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.DOC_SEARCH_RESULTS
|
||||||
|
results: list[DocSearchResult]
|
||||||
|
count: int
|
||||||
|
query: str
|
||||||
|
|
||||||
|
|
||||||
|
class DocPageResponse(ToolResponseBase):
|
||||||
|
"""Response for get_doc_page tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.DOC_PAGE
|
||||||
|
title: str
|
||||||
|
path: str
|
||||||
|
content: str # Full document content
|
||||||
|
doc_url: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
# Block models
|
||||||
|
class BlockInputFieldInfo(BaseModel):
|
||||||
|
"""Information about a block input field."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
type: str
|
||||||
|
description: str = ""
|
||||||
|
required: bool = False
|
||||||
|
default: Any | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class BlockInfoSummary(BaseModel):
|
||||||
|
"""Summary of a block for search results."""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
categories: list[str]
|
||||||
|
input_schema: dict[str, Any]
|
||||||
|
output_schema: dict[str, Any]
|
||||||
|
required_inputs: list[BlockInputFieldInfo] = Field(
|
||||||
|
default_factory=list,
|
||||||
|
description="List of required input fields for this block",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class BlockListResponse(ToolResponseBase):
|
||||||
|
"""Response for find_block tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.BLOCK_LIST
|
||||||
|
blocks: list[BlockInfoSummary]
|
||||||
|
count: int
|
||||||
|
query: str
|
||||||
|
usage_hint: str = Field(
|
||||||
|
default="To execute a block, call run_block with block_id set to the block's "
|
||||||
|
"'id' field and input_data containing the required fields from input_schema."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class BlockOutputResponse(ToolResponseBase):
|
||||||
|
"""Response for run_block tool."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.BLOCK_OUTPUT
|
||||||
|
block_id: str
|
||||||
|
block_name: str
|
||||||
|
outputs: dict[str, list[Any]]
|
||||||
|
success: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
# Long-running operation models
|
||||||
|
class OperationStartedResponse(ToolResponseBase):
|
||||||
|
"""Response when a long-running operation has been started in the background.
|
||||||
|
|
||||||
|
This is returned immediately to the client while the operation continues
|
||||||
|
to execute. The user can close the tab and check back later.
|
||||||
|
|
||||||
|
The task_id can be used to reconnect to the SSE stream via
|
||||||
|
GET /chat/tasks/{task_id}/stream?last_idx=0
|
||||||
|
"""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.OPERATION_STARTED
|
||||||
|
operation_id: str
|
||||||
|
tool_name: str
|
||||||
|
task_id: str | None = None # For SSE reconnection
|
||||||
|
|
||||||
|
|
||||||
|
class OperationPendingResponse(ToolResponseBase):
|
||||||
|
"""Response stored in chat history while a long-running operation is executing.
|
||||||
|
|
||||||
|
This is persisted to the database so users see a pending state when they
|
||||||
|
refresh before the operation completes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.OPERATION_PENDING
|
||||||
|
operation_id: str
|
||||||
|
tool_name: str
|
||||||
|
|
||||||
|
|
||||||
|
class OperationInProgressResponse(ToolResponseBase):
|
||||||
|
"""Response when an operation is already in progress.
|
||||||
|
|
||||||
|
Returned for idempotency when the same tool_call_id is requested again
|
||||||
|
while the background task is still running.
|
||||||
|
"""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.OPERATION_IN_PROGRESS
|
||||||
|
tool_call_id: str
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncProcessingResponse(ToolResponseBase):
|
||||||
|
"""Response when an operation has been delegated to async processing.
|
||||||
|
|
||||||
|
This is returned by tools when the external service accepts the request
|
||||||
|
for async processing (HTTP 202 Accepted). The Redis Streams completion
|
||||||
|
consumer will handle the result when the external service completes.
|
||||||
|
|
||||||
|
The status field is specifically "accepted" to allow the long-running tool
|
||||||
|
handler to detect this response and skip LLM continuation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.OPERATION_STARTED
|
||||||
|
status: str = "accepted" # Must be "accepted" for detection
|
||||||
|
operation_id: str | None = None
|
||||||
|
task_id: str | None = None
|
||||||
@@ -5,13 +5,16 @@ from typing import Any
|
|||||||
|
|
||||||
from pydantic import BaseModel, Field, field_validator
|
from pydantic import BaseModel, Field, field_validator
|
||||||
|
|
||||||
from backend.copilot.config import ChatConfig
|
from backend.api.features.chat.config import ChatConfig
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
from backend.copilot.tracking import track_agent_run_success, track_agent_scheduled
|
from backend.api.features.chat.tracking import (
|
||||||
from backend.data.db_accessors import graph_db, library_db, user_db
|
track_agent_run_success,
|
||||||
from backend.data.execution import ExecutionStatus
|
track_agent_scheduled,
|
||||||
|
)
|
||||||
|
from backend.api.features.library import db as library_db
|
||||||
from backend.data.graph import GraphModel
|
from backend.data.graph import GraphModel
|
||||||
from backend.data.model import CredentialsMetaInput
|
from backend.data.model import CredentialsMetaInput
|
||||||
|
from backend.data.user import get_user_by_id
|
||||||
from backend.executor import utils as execution_utils
|
from backend.executor import utils as execution_utils
|
||||||
from backend.util.clients import get_scheduler_client
|
from backend.util.clients import get_scheduler_client
|
||||||
from backend.util.exceptions import DatabaseError, NotFoundError
|
from backend.util.exceptions import DatabaseError, NotFoundError
|
||||||
@@ -21,15 +24,12 @@ from backend.util.timezone_utils import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from .base import BaseTool
|
from .base import BaseTool
|
||||||
from .execution_utils import get_execution_outputs, wait_for_execution
|
|
||||||
from .helpers import get_inputs_from_schema
|
from .helpers import get_inputs_from_schema
|
||||||
from .models import (
|
from .models import (
|
||||||
AgentDetails,
|
AgentDetails,
|
||||||
AgentDetailsResponse,
|
AgentDetailsResponse,
|
||||||
AgentOutputResponse,
|
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
ExecutionOptions,
|
ExecutionOptions,
|
||||||
ExecutionOutputInfo,
|
|
||||||
ExecutionStartedResponse,
|
ExecutionStartedResponse,
|
||||||
InputValidationErrorResponse,
|
InputValidationErrorResponse,
|
||||||
SetupInfo,
|
SetupInfo,
|
||||||
@@ -70,7 +70,6 @@ class RunAgentInput(BaseModel):
|
|||||||
schedule_name: str = ""
|
schedule_name: str = ""
|
||||||
cron: str = ""
|
cron: str = ""
|
||||||
timezone: str = "UTC"
|
timezone: str = "UTC"
|
||||||
wait_for_result: int = Field(default=0, ge=0, le=300)
|
|
||||||
|
|
||||||
@field_validator(
|
@field_validator(
|
||||||
"username_agent_slug",
|
"username_agent_slug",
|
||||||
@@ -152,14 +151,6 @@ class RunAgentTool(BaseTool):
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "IANA timezone for schedule (default: UTC)",
|
"description": "IANA timezone for schedule (default: UTC)",
|
||||||
},
|
},
|
||||||
"wait_for_result": {
|
|
||||||
"type": "integer",
|
|
||||||
"description": (
|
|
||||||
"Max seconds to wait for execution to complete (0-300). "
|
|
||||||
"If >0, blocks until the execution finishes or times out. "
|
|
||||||
"Returns execution outputs when complete."
|
|
||||||
),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
"required": [],
|
"required": [],
|
||||||
}
|
}
|
||||||
@@ -209,7 +200,7 @@ class RunAgentTool(BaseTool):
|
|||||||
|
|
||||||
# Priority: library_agent_id if provided
|
# Priority: library_agent_id if provided
|
||||||
if has_library_id:
|
if has_library_id:
|
||||||
library_agent = await library_db().get_library_agent(
|
library_agent = await library_db.get_library_agent(
|
||||||
params.library_agent_id, user_id
|
params.library_agent_id, user_id
|
||||||
)
|
)
|
||||||
if not library_agent:
|
if not library_agent:
|
||||||
@@ -218,7 +209,9 @@ class RunAgentTool(BaseTool):
|
|||||||
session_id=session_id,
|
session_id=session_id,
|
||||||
)
|
)
|
||||||
# Get the graph from the library agent
|
# Get the graph from the library agent
|
||||||
graph = await graph_db().get_graph(
|
from backend.data.graph import get_graph
|
||||||
|
|
||||||
|
graph = await get_graph(
|
||||||
library_agent.graph_id,
|
library_agent.graph_id,
|
||||||
library_agent.graph_version,
|
library_agent.graph_version,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
@@ -354,7 +347,6 @@ class RunAgentTool(BaseTool):
|
|||||||
graph=graph,
|
graph=graph,
|
||||||
graph_credentials=graph_credentials,
|
graph_credentials=graph_credentials,
|
||||||
inputs=params.inputs,
|
inputs=params.inputs,
|
||||||
wait_for_result=params.wait_for_result,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
except NotFoundError as e:
|
except NotFoundError as e:
|
||||||
@@ -438,9 +430,8 @@ class RunAgentTool(BaseTool):
|
|||||||
graph: GraphModel,
|
graph: GraphModel,
|
||||||
graph_credentials: dict[str, CredentialsMetaInput],
|
graph_credentials: dict[str, CredentialsMetaInput],
|
||||||
inputs: dict[str, Any],
|
inputs: dict[str, Any],
|
||||||
wait_for_result: int = 0,
|
|
||||||
) -> ToolResponseBase:
|
) -> ToolResponseBase:
|
||||||
"""Execute an agent immediately, optionally waiting for completion."""
|
"""Execute an agent immediately."""
|
||||||
session_id = session.session_id
|
session_id = session.session_id
|
||||||
|
|
||||||
# Check rate limits
|
# Check rate limits
|
||||||
@@ -477,93 +468,6 @@ class RunAgentTool(BaseTool):
|
|||||||
)
|
)
|
||||||
|
|
||||||
library_agent_link = f"/library/agents/{library_agent.id}"
|
library_agent_link = f"/library/agents/{library_agent.id}"
|
||||||
|
|
||||||
# If wait_for_result is requested, wait for execution to complete
|
|
||||||
if wait_for_result > 0:
|
|
||||||
logger.info(
|
|
||||||
f"Waiting up to {wait_for_result}s for execution {execution.id}"
|
|
||||||
)
|
|
||||||
completed = await wait_for_execution(
|
|
||||||
user_id=user_id,
|
|
||||||
graph_id=library_agent.graph_id,
|
|
||||||
execution_id=execution.id,
|
|
||||||
timeout_seconds=wait_for_result,
|
|
||||||
)
|
|
||||||
|
|
||||||
if completed and completed.status == ExecutionStatus.COMPLETED:
|
|
||||||
outputs = get_execution_outputs(completed)
|
|
||||||
return AgentOutputResponse(
|
|
||||||
message=(
|
|
||||||
f"Agent '{library_agent.name}' completed successfully. "
|
|
||||||
f"View at {library_agent_link}."
|
|
||||||
),
|
|
||||||
session_id=session_id,
|
|
||||||
agent_name=library_agent.name,
|
|
||||||
agent_id=library_agent.graph_id,
|
|
||||||
library_agent_id=library_agent.id,
|
|
||||||
library_agent_link=library_agent_link,
|
|
||||||
execution=ExecutionOutputInfo(
|
|
||||||
execution_id=execution.id,
|
|
||||||
status=completed.status.value,
|
|
||||||
started_at=completed.started_at,
|
|
||||||
ended_at=completed.ended_at,
|
|
||||||
outputs=outputs or {},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
elif completed and completed.status == ExecutionStatus.FAILED:
|
|
||||||
error_detail = completed.stats.error if completed.stats else None
|
|
||||||
return ErrorResponse(
|
|
||||||
message=(
|
|
||||||
f"Agent '{library_agent.name}' execution failed. "
|
|
||||||
f"View details at {library_agent_link}."
|
|
||||||
),
|
|
||||||
session_id=session_id,
|
|
||||||
error=error_detail,
|
|
||||||
)
|
|
||||||
elif completed and completed.status == ExecutionStatus.TERMINATED:
|
|
||||||
error_detail = completed.stats.error if completed.stats else None
|
|
||||||
return ErrorResponse(
|
|
||||||
message=(
|
|
||||||
f"Agent '{library_agent.name}' execution was terminated. "
|
|
||||||
f"View details at {library_agent_link}."
|
|
||||||
),
|
|
||||||
session_id=session_id,
|
|
||||||
error=error_detail,
|
|
||||||
)
|
|
||||||
elif completed and completed.status == ExecutionStatus.REVIEW:
|
|
||||||
return ExecutionStartedResponse(
|
|
||||||
message=(
|
|
||||||
f"Agent '{library_agent.name}' is awaiting human review. "
|
|
||||||
f"The user can approve or reject inline. After approval, "
|
|
||||||
f"the execution resumes automatically. Use view_agent_output "
|
|
||||||
f"with execution_id='{execution.id}' to check the result."
|
|
||||||
),
|
|
||||||
session_id=session_id,
|
|
||||||
execution_id=execution.id,
|
|
||||||
graph_id=library_agent.graph_id,
|
|
||||||
graph_name=library_agent.name,
|
|
||||||
library_agent_id=library_agent.id,
|
|
||||||
library_agent_link=library_agent_link,
|
|
||||||
status=ExecutionStatus.REVIEW.value,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
status = completed.status.value if completed else "unknown"
|
|
||||||
return ExecutionStartedResponse(
|
|
||||||
message=(
|
|
||||||
f"Agent '{library_agent.name}' is still {status} after "
|
|
||||||
f"{wait_for_result}s. Check results later at "
|
|
||||||
f"{library_agent_link}. "
|
|
||||||
f"Use view_agent_output with wait_if_running to check again."
|
|
||||||
),
|
|
||||||
session_id=session_id,
|
|
||||||
execution_id=execution.id,
|
|
||||||
graph_id=library_agent.graph_id,
|
|
||||||
graph_name=library_agent.name,
|
|
||||||
library_agent_id=library_agent.id,
|
|
||||||
library_agent_link=library_agent_link,
|
|
||||||
status=status,
|
|
||||||
)
|
|
||||||
|
|
||||||
return ExecutionStartedResponse(
|
return ExecutionStartedResponse(
|
||||||
message=(
|
message=(
|
||||||
f"Agent '{library_agent.name}' execution started successfully. "
|
f"Agent '{library_agent.name}' execution started successfully. "
|
||||||
@@ -618,7 +522,7 @@ class RunAgentTool(BaseTool):
|
|||||||
library_agent = await get_or_create_library_agent(graph, user_id)
|
library_agent = await get_or_create_library_agent(graph, user_id)
|
||||||
|
|
||||||
# Get user timezone
|
# Get user timezone
|
||||||
user = await user_db().get_user_by_id(user_id)
|
user = await get_user_by_id(user_id)
|
||||||
user_timezone = get_user_timezone_or_utc(user.timezone if user else timezone)
|
user_timezone = get_user_timezone_or_utc(user.timezone if user else timezone)
|
||||||
|
|
||||||
# Create schedule
|
# Create schedule
|
||||||
@@ -0,0 +1,355 @@
|
|||||||
|
"""Tool for executing blocks directly."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import uuid
|
||||||
|
from collections import defaultdict
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from pydantic_core import PydanticUndefined
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.api.features.chat.tools.find_block import (
|
||||||
|
COPILOT_EXCLUDED_BLOCK_IDS,
|
||||||
|
COPILOT_EXCLUDED_BLOCK_TYPES,
|
||||||
|
)
|
||||||
|
from backend.data.block import AnyBlockSchema, get_block
|
||||||
|
from backend.data.execution import ExecutionContext
|
||||||
|
from backend.data.model import CredentialsFieldInfo, CredentialsMetaInput
|
||||||
|
from backend.data.workspace import get_or_create_workspace
|
||||||
|
from backend.integrations.creds_manager import IntegrationCredentialsManager
|
||||||
|
from backend.util.exceptions import BlockError
|
||||||
|
|
||||||
|
from .base import BaseTool
|
||||||
|
from .helpers import get_inputs_from_schema
|
||||||
|
from .models import (
|
||||||
|
BlockOutputResponse,
|
||||||
|
ErrorResponse,
|
||||||
|
SetupInfo,
|
||||||
|
SetupRequirementsResponse,
|
||||||
|
ToolResponseBase,
|
||||||
|
UserReadiness,
|
||||||
|
)
|
||||||
|
from .utils import (
|
||||||
|
build_missing_credentials_from_field_info,
|
||||||
|
match_credentials_to_requirements,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RunBlockTool(BaseTool):
|
||||||
|
"""Tool for executing a block and returning its outputs."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "run_block"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Execute a specific block with the provided input data. "
|
||||||
|
"IMPORTANT: You MUST call find_block first to get the block's 'id' - "
|
||||||
|
"do NOT guess or make up block IDs. "
|
||||||
|
"Use the 'id' from find_block results and provide input_data "
|
||||||
|
"matching the block's required_inputs."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"block_id": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"The block's 'id' field from find_block results. "
|
||||||
|
"NEVER guess this - always get it from find_block first."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"input_data": {
|
||||||
|
"type": "object",
|
||||||
|
"description": (
|
||||||
|
"Input values for the block. Use the 'required_inputs' field "
|
||||||
|
"from find_block to see what fields are needed."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": ["block_id", "input_data"],
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
"""Execute a block with the given input data.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id: User ID (required)
|
||||||
|
session: Chat session
|
||||||
|
block_id: Block UUID to execute
|
||||||
|
input_data: Input values for the block
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
BlockOutputResponse: Block execution outputs
|
||||||
|
SetupRequirementsResponse: Missing credentials
|
||||||
|
ErrorResponse: Error message
|
||||||
|
"""
|
||||||
|
block_id = kwargs.get("block_id", "").strip()
|
||||||
|
input_data = kwargs.get("input_data", {})
|
||||||
|
session_id = session.session_id
|
||||||
|
|
||||||
|
if not block_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide a block_id",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not isinstance(input_data, dict):
|
||||||
|
return ErrorResponse(
|
||||||
|
message="input_data must be an object",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Authentication required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get the block
|
||||||
|
block = get_block(block_id)
|
||||||
|
if not block:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Block '{block_id}' not found",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
if block.disabled:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Block '{block_id}' is disabled",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check if block is excluded from CoPilot (graph-only blocks)
|
||||||
|
if (
|
||||||
|
block.block_type in COPILOT_EXCLUDED_BLOCK_TYPES
|
||||||
|
or block.id in COPILOT_EXCLUDED_BLOCK_IDS
|
||||||
|
):
|
||||||
|
return ErrorResponse(
|
||||||
|
message=(
|
||||||
|
f"Block '{block.name}' cannot be run directly in CoPilot. "
|
||||||
|
"This block is designed for use within graphs only."
|
||||||
|
),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(f"Executing block {block.name} ({block_id}) for user {user_id}")
|
||||||
|
|
||||||
|
creds_manager = IntegrationCredentialsManager()
|
||||||
|
matched_credentials, missing_credentials = (
|
||||||
|
await self._resolve_block_credentials(user_id, block, input_data)
|
||||||
|
)
|
||||||
|
|
||||||
|
if missing_credentials:
|
||||||
|
# Return setup requirements response with missing credentials
|
||||||
|
credentials_fields_info = block.input_schema.get_credentials_fields_info()
|
||||||
|
missing_creds_dict = build_missing_credentials_from_field_info(
|
||||||
|
credentials_fields_info, set(matched_credentials.keys())
|
||||||
|
)
|
||||||
|
missing_creds_list = list(missing_creds_dict.values())
|
||||||
|
|
||||||
|
return SetupRequirementsResponse(
|
||||||
|
message=(
|
||||||
|
f"Block '{block.name}' requires credentials that are not configured. "
|
||||||
|
"Please set up the required credentials before running this block."
|
||||||
|
),
|
||||||
|
session_id=session_id,
|
||||||
|
setup_info=SetupInfo(
|
||||||
|
agent_id=block_id,
|
||||||
|
agent_name=block.name,
|
||||||
|
user_readiness=UserReadiness(
|
||||||
|
has_all_credentials=False,
|
||||||
|
missing_credentials=missing_creds_dict,
|
||||||
|
ready_to_run=False,
|
||||||
|
),
|
||||||
|
requirements={
|
||||||
|
"credentials": missing_creds_list,
|
||||||
|
"inputs": self._get_inputs_list(block),
|
||||||
|
"execution_modes": ["immediate"],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
graph_id=None,
|
||||||
|
graph_version=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Get or create user's workspace for CoPilot file operations
|
||||||
|
workspace = await get_or_create_workspace(user_id)
|
||||||
|
|
||||||
|
# Generate synthetic IDs for CoPilot context
|
||||||
|
# Each chat session is treated as its own agent with one continuous run
|
||||||
|
# This means:
|
||||||
|
# - graph_id (agent) = session (memories scoped to session when limit_to_agent=True)
|
||||||
|
# - graph_exec_id (run) = session (memories scoped to session when limit_to_run=True)
|
||||||
|
# - node_exec_id = unique per block execution
|
||||||
|
synthetic_graph_id = f"copilot-session-{session.session_id}"
|
||||||
|
synthetic_graph_exec_id = f"copilot-session-{session.session_id}"
|
||||||
|
synthetic_node_id = f"copilot-node-{block_id}"
|
||||||
|
synthetic_node_exec_id = (
|
||||||
|
f"copilot-{session.session_id}-{uuid.uuid4().hex[:8]}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create unified execution context with all required fields
|
||||||
|
execution_context = ExecutionContext(
|
||||||
|
# Execution identity
|
||||||
|
user_id=user_id,
|
||||||
|
graph_id=synthetic_graph_id,
|
||||||
|
graph_exec_id=synthetic_graph_exec_id,
|
||||||
|
graph_version=1, # Versions are 1-indexed
|
||||||
|
node_id=synthetic_node_id,
|
||||||
|
node_exec_id=synthetic_node_exec_id,
|
||||||
|
# Workspace with session scoping
|
||||||
|
workspace_id=workspace.id,
|
||||||
|
session_id=session.session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Prepare kwargs for block execution
|
||||||
|
# Keep individual kwargs for backwards compatibility with existing blocks
|
||||||
|
exec_kwargs: dict[str, Any] = {
|
||||||
|
"user_id": user_id,
|
||||||
|
"execution_context": execution_context,
|
||||||
|
# Legacy: individual kwargs for blocks not yet using execution_context
|
||||||
|
"workspace_id": workspace.id,
|
||||||
|
"graph_exec_id": synthetic_graph_exec_id,
|
||||||
|
"node_exec_id": synthetic_node_exec_id,
|
||||||
|
"node_id": synthetic_node_id,
|
||||||
|
"graph_version": 1, # Versions are 1-indexed
|
||||||
|
"graph_id": synthetic_graph_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
for field_name, cred_meta in matched_credentials.items():
|
||||||
|
# Inject metadata into input_data (for validation)
|
||||||
|
if field_name not in input_data:
|
||||||
|
input_data[field_name] = cred_meta.model_dump()
|
||||||
|
|
||||||
|
# Fetch actual credentials and pass as kwargs (for execution)
|
||||||
|
actual_credentials = await creds_manager.get(
|
||||||
|
user_id, cred_meta.id, lock=False
|
||||||
|
)
|
||||||
|
if actual_credentials:
|
||||||
|
exec_kwargs[field_name] = actual_credentials
|
||||||
|
else:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to retrieve credentials for {field_name}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Execute the block and collect outputs
|
||||||
|
outputs: dict[str, list[Any]] = defaultdict(list)
|
||||||
|
async for output_name, output_data in block.execute(
|
||||||
|
input_data,
|
||||||
|
**exec_kwargs,
|
||||||
|
):
|
||||||
|
outputs[output_name].append(output_data)
|
||||||
|
|
||||||
|
return BlockOutputResponse(
|
||||||
|
message=f"Block '{block.name}' executed successfully",
|
||||||
|
block_id=block_id,
|
||||||
|
block_name=block.name,
|
||||||
|
outputs=dict(outputs),
|
||||||
|
success=True,
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except BlockError as e:
|
||||||
|
logger.warning(f"Block execution failed: {e}")
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Block execution failed: {e}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected error executing block: {e}", exc_info=True)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to execute block: {str(e)}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _resolve_block_credentials(
|
||||||
|
self,
|
||||||
|
user_id: str,
|
||||||
|
block: AnyBlockSchema,
|
||||||
|
input_data: dict[str, Any] | None = None,
|
||||||
|
) -> tuple[dict[str, CredentialsMetaInput], list[CredentialsMetaInput]]:
|
||||||
|
"""
|
||||||
|
Resolve credentials for a block by matching user's available credentials.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id: User ID
|
||||||
|
block: Block to resolve credentials for
|
||||||
|
input_data: Input data for the block (used to determine provider via discriminator)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple of (matched_credentials, missing_credentials) - matched credentials
|
||||||
|
are used for block execution, missing ones indicate setup requirements.
|
||||||
|
"""
|
||||||
|
input_data = input_data or {}
|
||||||
|
requirements = self._resolve_discriminated_credentials(block, input_data)
|
||||||
|
|
||||||
|
if not requirements:
|
||||||
|
return {}, []
|
||||||
|
|
||||||
|
return await match_credentials_to_requirements(user_id, requirements)
|
||||||
|
|
||||||
|
def _get_inputs_list(self, block: AnyBlockSchema) -> list[dict[str, Any]]:
|
||||||
|
"""Extract non-credential inputs from block schema."""
|
||||||
|
schema = block.input_schema.jsonschema()
|
||||||
|
credentials_fields = set(block.input_schema.get_credentials_fields().keys())
|
||||||
|
return get_inputs_from_schema(schema, exclude_fields=credentials_fields)
|
||||||
|
|
||||||
|
def _resolve_discriminated_credentials(
|
||||||
|
self,
|
||||||
|
block: AnyBlockSchema,
|
||||||
|
input_data: dict[str, Any],
|
||||||
|
) -> dict[str, CredentialsFieldInfo]:
|
||||||
|
"""Resolve credential requirements, applying discriminator logic where needed."""
|
||||||
|
credentials_fields_info = block.input_schema.get_credentials_fields_info()
|
||||||
|
if not credentials_fields_info:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
resolved: dict[str, CredentialsFieldInfo] = {}
|
||||||
|
|
||||||
|
for field_name, field_info in credentials_fields_info.items():
|
||||||
|
effective_field_info = field_info
|
||||||
|
|
||||||
|
if field_info.discriminator and field_info.discriminator_mapping:
|
||||||
|
discriminator_value = input_data.get(field_info.discriminator)
|
||||||
|
if discriminator_value is None:
|
||||||
|
field = block.input_schema.model_fields.get(
|
||||||
|
field_info.discriminator
|
||||||
|
)
|
||||||
|
if field and field.default is not PydanticUndefined:
|
||||||
|
discriminator_value = field.default
|
||||||
|
|
||||||
|
if (
|
||||||
|
discriminator_value
|
||||||
|
and discriminator_value in field_info.discriminator_mapping
|
||||||
|
):
|
||||||
|
effective_field_info = field_info.discriminate(discriminator_value)
|
||||||
|
# For host-scoped credentials, add the discriminator value
|
||||||
|
# (e.g., URL) so _credential_is_for_host can match it
|
||||||
|
effective_field_info.discriminator_values.add(discriminator_value)
|
||||||
|
logger.debug(
|
||||||
|
f"Discriminated provider for {field_name}: "
|
||||||
|
f"{discriminator_value} -> {effective_field_info.provider}"
|
||||||
|
)
|
||||||
|
|
||||||
|
resolved[field_name] = effective_field_info
|
||||||
|
|
||||||
|
return resolved
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
"""Tests for block execution guards in RunBlockTool."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from backend.api.features.chat.tools.models import ErrorResponse
|
||||||
|
from backend.api.features.chat.tools.run_block import RunBlockTool
|
||||||
|
from backend.data.block import BlockType
|
||||||
|
|
||||||
|
from ._test_data import make_session
|
||||||
|
|
||||||
|
_TEST_USER_ID = "test-user-run-block"
|
||||||
|
|
||||||
|
|
||||||
|
def make_mock_block(
|
||||||
|
block_id: str, name: str, block_type: BlockType, disabled: bool = False
|
||||||
|
):
|
||||||
|
"""Create a mock block for testing."""
|
||||||
|
mock = MagicMock()
|
||||||
|
mock.id = block_id
|
||||||
|
mock.name = name
|
||||||
|
mock.block_type = block_type
|
||||||
|
mock.disabled = disabled
|
||||||
|
mock.input_schema = MagicMock()
|
||||||
|
mock.input_schema.jsonschema.return_value = {"properties": {}, "required": []}
|
||||||
|
mock.input_schema.get_credentials_fields_info.return_value = []
|
||||||
|
return mock
|
||||||
|
|
||||||
|
|
||||||
|
class TestRunBlockFiltering:
|
||||||
|
"""Tests for block execution guards in RunBlockTool."""
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_excluded_block_type_returns_error(self):
|
||||||
|
"""Attempting to execute a block with excluded BlockType returns error."""
|
||||||
|
session = make_session(user_id=_TEST_USER_ID)
|
||||||
|
|
||||||
|
input_block = make_mock_block("input-block-id", "Input Block", BlockType.INPUT)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.run_block.get_block",
|
||||||
|
return_value=input_block,
|
||||||
|
):
|
||||||
|
tool = RunBlockTool()
|
||||||
|
response = await tool._execute(
|
||||||
|
user_id=_TEST_USER_ID,
|
||||||
|
session=session,
|
||||||
|
block_id="input-block-id",
|
||||||
|
input_data={},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(response, ErrorResponse)
|
||||||
|
assert "cannot be run directly in CoPilot" in response.message
|
||||||
|
assert "designed for use within graphs only" in response.message
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_excluded_block_id_returns_error(self):
|
||||||
|
"""Attempting to execute SmartDecisionMakerBlock returns error."""
|
||||||
|
session = make_session(user_id=_TEST_USER_ID)
|
||||||
|
|
||||||
|
smart_decision_id = "3b191d9f-356f-482d-8238-ba04b6d18381"
|
||||||
|
smart_block = make_mock_block(
|
||||||
|
smart_decision_id, "Smart Decision Maker", BlockType.STANDARD
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.run_block.get_block",
|
||||||
|
return_value=smart_block,
|
||||||
|
):
|
||||||
|
tool = RunBlockTool()
|
||||||
|
response = await tool._execute(
|
||||||
|
user_id=_TEST_USER_ID,
|
||||||
|
session=session,
|
||||||
|
block_id=smart_decision_id,
|
||||||
|
input_data={},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(response, ErrorResponse)
|
||||||
|
assert "cannot be run directly in CoPilot" in response.message
|
||||||
|
|
||||||
|
@pytest.mark.asyncio(loop_scope="session")
|
||||||
|
async def test_non_excluded_block_passes_guard(self):
|
||||||
|
"""Non-excluded blocks pass the filtering guard (may fail later for other reasons)."""
|
||||||
|
session = make_session(user_id=_TEST_USER_ID)
|
||||||
|
|
||||||
|
standard_block = make_mock_block(
|
||||||
|
"standard-id", "HTTP Request", BlockType.STANDARD
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"backend.api.features.chat.tools.run_block.get_block",
|
||||||
|
return_value=standard_block,
|
||||||
|
):
|
||||||
|
tool = RunBlockTool()
|
||||||
|
response = await tool._execute(
|
||||||
|
user_id=_TEST_USER_ID,
|
||||||
|
session=session,
|
||||||
|
block_id="standard-id",
|
||||||
|
input_data={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Should NOT be an ErrorResponse about CoPilot exclusion
|
||||||
|
# (may be other errors like missing credentials, but not the exclusion guard)
|
||||||
|
if isinstance(response, ErrorResponse):
|
||||||
|
assert "cannot be run directly in CoPilot" not in response.message
|
||||||
@@ -5,17 +5,16 @@ from typing import Any
|
|||||||
|
|
||||||
from prisma.enums import ContentType
|
from prisma.enums import ContentType
|
||||||
|
|
||||||
from backend.copilot.model import ChatSession
|
from backend.api.features.chat.model import ChatSession
|
||||||
from backend.data.db_accessors import search
|
from backend.api.features.chat.tools.base import BaseTool
|
||||||
|
from backend.api.features.chat.tools.models import (
|
||||||
from .base import BaseTool
|
|
||||||
from .models import (
|
|
||||||
DocSearchResult,
|
DocSearchResult,
|
||||||
DocSearchResultsResponse,
|
DocSearchResultsResponse,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
NoResultsResponse,
|
NoResultsResponse,
|
||||||
ToolResponseBase,
|
ToolResponseBase,
|
||||||
)
|
)
|
||||||
|
from backend.api.features.store.hybrid_search import unified_hybrid_search
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -118,7 +117,7 @@ class SearchDocsTool(BaseTool):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Search using hybrid search for DOCUMENTATION content type only
|
# Search using hybrid search for DOCUMENTATION content type only
|
||||||
results, total = await search().unified_hybrid_search(
|
results, total = await unified_hybrid_search(
|
||||||
query=query,
|
query=query,
|
||||||
content_types=[ContentType.DOCUMENTATION],
|
content_types=[ContentType.DOCUMENTATION],
|
||||||
page=1,
|
page=1,
|
||||||
@@ -3,8 +3,9 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.api.features.library import db as library_db
|
||||||
from backend.api.features.library import model as library_model
|
from backend.api.features.library import model as library_model
|
||||||
from backend.data.db_accessors import library_db, store_db
|
from backend.api.features.store import db as store_db
|
||||||
from backend.data.graph import GraphModel
|
from backend.data.graph import GraphModel
|
||||||
from backend.data.model import (
|
from backend.data.model import (
|
||||||
Credentials,
|
Credentials,
|
||||||
@@ -14,7 +15,6 @@ from backend.data.model import (
|
|||||||
OAuth2Credentials,
|
OAuth2Credentials,
|
||||||
)
|
)
|
||||||
from backend.integrations.creds_manager import IntegrationCredentialsManager
|
from backend.integrations.creds_manager import IntegrationCredentialsManager
|
||||||
from backend.integrations.providers import ProviderName
|
|
||||||
from backend.util.exceptions import NotFoundError
|
from backend.util.exceptions import NotFoundError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -38,14 +38,13 @@ async def fetch_graph_from_store_slug(
|
|||||||
Raises:
|
Raises:
|
||||||
DatabaseError: If there's a database error during lookup.
|
DatabaseError: If there's a database error during lookup.
|
||||||
"""
|
"""
|
||||||
sdb = store_db()
|
|
||||||
try:
|
try:
|
||||||
store_agent = await sdb.get_store_agent_details(username, agent_name)
|
store_agent = await store_db.get_store_agent_details(username, agent_name)
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
# Get the graph from store listing version
|
# Get the graph from store listing version
|
||||||
graph = await sdb.get_available_graph(
|
graph = await store_db.get_available_graph(
|
||||||
store_agent.store_listing_version_id, hide_nodes=False
|
store_agent.store_listing_version_id, hide_nodes=False
|
||||||
)
|
)
|
||||||
return graph, store_agent
|
return graph, store_agent
|
||||||
@@ -210,13 +209,13 @@ async def get_or_create_library_agent(
|
|||||||
Returns:
|
Returns:
|
||||||
LibraryAgent instance
|
LibraryAgent instance
|
||||||
"""
|
"""
|
||||||
existing = await library_db().get_library_agent_by_graph_id(
|
existing = await library_db.get_library_agent_by_graph_id(
|
||||||
graph_id=graph.id, user_id=user_id
|
graph_id=graph.id, user_id=user_id
|
||||||
)
|
)
|
||||||
if existing:
|
if existing:
|
||||||
return existing
|
return existing
|
||||||
|
|
||||||
library_agents = await library_db().create_library_agent(
|
library_agents = await library_db.create_library_agent(
|
||||||
graph=graph,
|
graph=graph,
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
create_library_agents_for_sub_graphs=False,
|
create_library_agents_for_sub_graphs=False,
|
||||||
@@ -360,7 +359,7 @@ async def match_user_credentials_to_graph(
|
|||||||
_,
|
_,
|
||||||
_,
|
_,
|
||||||
) in aggregated_creds.items():
|
) in aggregated_creds.items():
|
||||||
# Find first matching credential by provider, type, scopes, and host/URL
|
# Find first matching credential by provider, type, and scopes
|
||||||
matching_cred = next(
|
matching_cred = next(
|
||||||
(
|
(
|
||||||
cred
|
cred
|
||||||
@@ -375,10 +374,6 @@ async def match_user_credentials_to_graph(
|
|||||||
cred.type != "host_scoped"
|
cred.type != "host_scoped"
|
||||||
or _credential_is_for_host(cred, credential_requirements)
|
or _credential_is_for_host(cred, credential_requirements)
|
||||||
)
|
)
|
||||||
and (
|
|
||||||
cred.provider != ProviderName.MCP
|
|
||||||
or _credential_is_for_mcp_server(cred, credential_requirements)
|
|
||||||
)
|
|
||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
@@ -449,22 +444,6 @@ def _credential_is_for_host(
|
|||||||
return credential.matches_url(list(requirements.discriminator_values)[0])
|
return credential.matches_url(list(requirements.discriminator_values)[0])
|
||||||
|
|
||||||
|
|
||||||
def _credential_is_for_mcp_server(
|
|
||||||
credential: Credentials,
|
|
||||||
requirements: CredentialsFieldInfo,
|
|
||||||
) -> bool:
|
|
||||||
"""Check if an MCP OAuth credential matches the required server URL."""
|
|
||||||
if not requirements.discriminator_values:
|
|
||||||
return True
|
|
||||||
|
|
||||||
server_url = (
|
|
||||||
credential.metadata.get("mcp_server_url")
|
|
||||||
if isinstance(credential, OAuth2Credentials)
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
return server_url in requirements.discriminator_values if server_url else False
|
|
||||||
|
|
||||||
|
|
||||||
async def check_user_has_required_credentials(
|
async def check_user_has_required_credentials(
|
||||||
user_id: str,
|
user_id: str,
|
||||||
required_credentials: list[CredentialsMetaInput],
|
required_credentials: list[CredentialsMetaInput],
|
||||||
@@ -0,0 +1,620 @@
|
|||||||
|
"""CoPilot tools for workspace file operations."""
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import logging
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from backend.api.features.chat.model import ChatSession
|
||||||
|
from backend.data.workspace import get_or_create_workspace
|
||||||
|
from backend.util.settings import Config
|
||||||
|
from backend.util.virus_scanner import scan_content_safe
|
||||||
|
from backend.util.workspace import WorkspaceManager
|
||||||
|
|
||||||
|
from .base import BaseTool
|
||||||
|
from .models import ErrorResponse, ResponseType, ToolResponseBase
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class WorkspaceFileInfoData(BaseModel):
|
||||||
|
"""Data model for workspace file information (not a response itself)."""
|
||||||
|
|
||||||
|
file_id: str
|
||||||
|
name: str
|
||||||
|
path: str
|
||||||
|
mime_type: str
|
||||||
|
size_bytes: int
|
||||||
|
|
||||||
|
|
||||||
|
class WorkspaceFileListResponse(ToolResponseBase):
|
||||||
|
"""Response containing list of workspace files."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.WORKSPACE_FILE_LIST
|
||||||
|
files: list[WorkspaceFileInfoData]
|
||||||
|
total_count: int
|
||||||
|
|
||||||
|
|
||||||
|
class WorkspaceFileContentResponse(ToolResponseBase):
|
||||||
|
"""Response containing workspace file content (legacy, for small text files)."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.WORKSPACE_FILE_CONTENT
|
||||||
|
file_id: str
|
||||||
|
name: str
|
||||||
|
path: str
|
||||||
|
mime_type: str
|
||||||
|
content_base64: str
|
||||||
|
|
||||||
|
|
||||||
|
class WorkspaceFileMetadataResponse(ToolResponseBase):
|
||||||
|
"""Response containing workspace file metadata and download URL (prevents context bloat)."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.WORKSPACE_FILE_METADATA
|
||||||
|
file_id: str
|
||||||
|
name: str
|
||||||
|
path: str
|
||||||
|
mime_type: str
|
||||||
|
size_bytes: int
|
||||||
|
download_url: str
|
||||||
|
preview: str | None = None # First 500 chars for text files
|
||||||
|
|
||||||
|
|
||||||
|
class WorkspaceWriteResponse(ToolResponseBase):
|
||||||
|
"""Response after writing a file to workspace."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.WORKSPACE_FILE_WRITTEN
|
||||||
|
file_id: str
|
||||||
|
name: str
|
||||||
|
path: str
|
||||||
|
size_bytes: int
|
||||||
|
|
||||||
|
|
||||||
|
class WorkspaceDeleteResponse(ToolResponseBase):
|
||||||
|
"""Response after deleting a file from workspace."""
|
||||||
|
|
||||||
|
type: ResponseType = ResponseType.WORKSPACE_FILE_DELETED
|
||||||
|
file_id: str
|
||||||
|
success: bool
|
||||||
|
|
||||||
|
|
||||||
|
class ListWorkspaceFilesTool(BaseTool):
|
||||||
|
"""Tool for listing files in user's workspace."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "list_workspace_files"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"List files in the user's workspace. "
|
||||||
|
"Returns file names, paths, sizes, and metadata. "
|
||||||
|
"Optionally filter by path prefix."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"path_prefix": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Optional path prefix to filter files "
|
||||||
|
"(e.g., '/documents/' to list only files in documents folder). "
|
||||||
|
"By default, only files from the current session are listed."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"limit": {
|
||||||
|
"type": "integer",
|
||||||
|
"description": "Maximum number of files to return (default 50, max 100)",
|
||||||
|
"minimum": 1,
|
||||||
|
"maximum": 100,
|
||||||
|
},
|
||||||
|
"include_all_sessions": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": (
|
||||||
|
"If true, list files from all sessions. "
|
||||||
|
"Default is false (only current session's files)."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
session_id = session.session_id
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Authentication required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
path_prefix: Optional[str] = kwargs.get("path_prefix")
|
||||||
|
limit = min(kwargs.get("limit", 50), 100)
|
||||||
|
include_all_sessions: bool = kwargs.get("include_all_sessions", False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
workspace = await get_or_create_workspace(user_id)
|
||||||
|
# Pass session_id for session-scoped file access
|
||||||
|
manager = WorkspaceManager(user_id, workspace.id, session_id)
|
||||||
|
|
||||||
|
files = await manager.list_files(
|
||||||
|
path=path_prefix,
|
||||||
|
limit=limit,
|
||||||
|
include_all_sessions=include_all_sessions,
|
||||||
|
)
|
||||||
|
total = await manager.get_file_count(
|
||||||
|
path=path_prefix,
|
||||||
|
include_all_sessions=include_all_sessions,
|
||||||
|
)
|
||||||
|
|
||||||
|
file_infos = [
|
||||||
|
WorkspaceFileInfoData(
|
||||||
|
file_id=f.id,
|
||||||
|
name=f.name,
|
||||||
|
path=f.path,
|
||||||
|
mime_type=f.mimeType,
|
||||||
|
size_bytes=f.sizeBytes,
|
||||||
|
)
|
||||||
|
for f in files
|
||||||
|
]
|
||||||
|
|
||||||
|
scope_msg = "all sessions" if include_all_sessions else "current session"
|
||||||
|
return WorkspaceFileListResponse(
|
||||||
|
files=file_infos,
|
||||||
|
total_count=total,
|
||||||
|
message=f"Found {len(files)} files in workspace ({scope_msg})",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error listing workspace files: {e}", exc_info=True)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to list workspace files: {str(e)}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ReadWorkspaceFileTool(BaseTool):
|
||||||
|
"""Tool for reading file content from workspace."""
|
||||||
|
|
||||||
|
# Size threshold for returning full content vs metadata+URL
|
||||||
|
# Files larger than this return metadata with download URL to prevent context bloat
|
||||||
|
MAX_INLINE_SIZE_BYTES = 32 * 1024 # 32KB
|
||||||
|
# Preview size for text files
|
||||||
|
PREVIEW_SIZE = 500
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "read_workspace_file"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Read a file from the user's workspace. "
|
||||||
|
"Specify either file_id or path to identify the file. "
|
||||||
|
"For small text files, returns content directly. "
|
||||||
|
"For large or binary files, returns metadata and a download URL. "
|
||||||
|
"Paths are scoped to the current session by default. "
|
||||||
|
"Use /sessions/<session_id>/... for cross-session access."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"file_id": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The file's unique ID (from list_workspace_files)",
|
||||||
|
},
|
||||||
|
"path": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"The virtual file path (e.g., '/documents/report.pdf'). "
|
||||||
|
"Scoped to current session by default."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"force_download_url": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": (
|
||||||
|
"If true, always return metadata+URL instead of inline content. "
|
||||||
|
"Default is false (auto-selects based on file size/type)."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": [], # At least one must be provided
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _is_text_mime_type(self, mime_type: str) -> bool:
|
||||||
|
"""Check if the MIME type is a text-based type."""
|
||||||
|
text_types = [
|
||||||
|
"text/",
|
||||||
|
"application/json",
|
||||||
|
"application/xml",
|
||||||
|
"application/javascript",
|
||||||
|
"application/x-python",
|
||||||
|
"application/x-sh",
|
||||||
|
]
|
||||||
|
return any(mime_type.startswith(t) for t in text_types)
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
session_id = session.session_id
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Authentication required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
file_id: Optional[str] = kwargs.get("file_id")
|
||||||
|
path: Optional[str] = kwargs.get("path")
|
||||||
|
force_download_url: bool = kwargs.get("force_download_url", False)
|
||||||
|
|
||||||
|
if not file_id and not path:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide either file_id or path",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
workspace = await get_or_create_workspace(user_id)
|
||||||
|
# Pass session_id for session-scoped file access
|
||||||
|
manager = WorkspaceManager(user_id, workspace.id, session_id)
|
||||||
|
|
||||||
|
# Get file info
|
||||||
|
if file_id:
|
||||||
|
file_info = await manager.get_file_info(file_id)
|
||||||
|
if file_info is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"File not found: {file_id}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
target_file_id = file_id
|
||||||
|
else:
|
||||||
|
# path is guaranteed to be non-None here due to the check above
|
||||||
|
assert path is not None
|
||||||
|
file_info = await manager.get_file_info_by_path(path)
|
||||||
|
if file_info is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"File not found at path: {path}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
target_file_id = file_info.id
|
||||||
|
|
||||||
|
# Decide whether to return inline content or metadata+URL
|
||||||
|
is_small_file = file_info.sizeBytes <= self.MAX_INLINE_SIZE_BYTES
|
||||||
|
is_text_file = self._is_text_mime_type(file_info.mimeType)
|
||||||
|
|
||||||
|
# Return inline content for small text files (unless force_download_url)
|
||||||
|
if is_small_file and is_text_file and not force_download_url:
|
||||||
|
content = await manager.read_file_by_id(target_file_id)
|
||||||
|
content_b64 = base64.b64encode(content).decode("utf-8")
|
||||||
|
|
||||||
|
return WorkspaceFileContentResponse(
|
||||||
|
file_id=file_info.id,
|
||||||
|
name=file_info.name,
|
||||||
|
path=file_info.path,
|
||||||
|
mime_type=file_info.mimeType,
|
||||||
|
content_base64=content_b64,
|
||||||
|
message=f"Successfully read file: {file_info.name}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Return metadata + workspace:// reference for large or binary files
|
||||||
|
# This prevents context bloat (100KB file = ~133KB as base64)
|
||||||
|
# Use workspace:// format so frontend urlTransform can add proxy prefix
|
||||||
|
download_url = f"workspace://{target_file_id}"
|
||||||
|
|
||||||
|
# Generate preview for text files
|
||||||
|
preview: str | None = None
|
||||||
|
if is_text_file:
|
||||||
|
try:
|
||||||
|
content = await manager.read_file_by_id(target_file_id)
|
||||||
|
preview_text = content[: self.PREVIEW_SIZE].decode(
|
||||||
|
"utf-8", errors="replace"
|
||||||
|
)
|
||||||
|
if len(content) > self.PREVIEW_SIZE:
|
||||||
|
preview_text += "..."
|
||||||
|
preview = preview_text
|
||||||
|
except Exception:
|
||||||
|
pass # Preview is optional
|
||||||
|
|
||||||
|
return WorkspaceFileMetadataResponse(
|
||||||
|
file_id=file_info.id,
|
||||||
|
name=file_info.name,
|
||||||
|
path=file_info.path,
|
||||||
|
mime_type=file_info.mimeType,
|
||||||
|
size_bytes=file_info.sizeBytes,
|
||||||
|
download_url=download_url,
|
||||||
|
preview=preview,
|
||||||
|
message=f"File: {file_info.name} ({file_info.sizeBytes} bytes). Use download_url to retrieve content.",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error reading workspace file: {e}", exc_info=True)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to read workspace file: {str(e)}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class WriteWorkspaceFileTool(BaseTool):
|
||||||
|
"""Tool for writing files to workspace."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "write_workspace_file"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Write or create a file in the user's workspace. "
|
||||||
|
"Provide the content as a base64-encoded string. "
|
||||||
|
f"Maximum file size is {Config().max_file_size_mb}MB. "
|
||||||
|
"Files are saved to the current session's folder by default. "
|
||||||
|
"Use /sessions/<session_id>/... for cross-session access."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"filename": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Name for the file (e.g., 'report.pdf')",
|
||||||
|
},
|
||||||
|
"content_base64": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Base64-encoded file content",
|
||||||
|
},
|
||||||
|
"path": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Optional virtual path where to save the file "
|
||||||
|
"(e.g., '/documents/report.pdf'). "
|
||||||
|
"Defaults to '/{filename}'. Scoped to current session."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"mime_type": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"Optional MIME type of the file. "
|
||||||
|
"Auto-detected from filename if not provided."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
"overwrite": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Whether to overwrite if file exists at path (default: false)",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": ["filename", "content_base64"],
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
session_id = session.session_id
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Authentication required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
filename: str = kwargs.get("filename", "")
|
||||||
|
content_b64: str = kwargs.get("content_base64", "")
|
||||||
|
path: Optional[str] = kwargs.get("path")
|
||||||
|
mime_type: Optional[str] = kwargs.get("mime_type")
|
||||||
|
overwrite: bool = kwargs.get("overwrite", False)
|
||||||
|
|
||||||
|
if not filename:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide a filename",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not content_b64:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide content_base64",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Decode content
|
||||||
|
try:
|
||||||
|
content = base64.b64decode(content_b64)
|
||||||
|
except Exception:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Invalid base64-encoded content",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check size
|
||||||
|
max_file_size = Config().max_file_size_mb * 1024 * 1024
|
||||||
|
if len(content) > max_file_size:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"File too large. Maximum size is {Config().max_file_size_mb}MB",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Virus scan
|
||||||
|
await scan_content_safe(content, filename=filename)
|
||||||
|
|
||||||
|
workspace = await get_or_create_workspace(user_id)
|
||||||
|
# Pass session_id for session-scoped file access
|
||||||
|
manager = WorkspaceManager(user_id, workspace.id, session_id)
|
||||||
|
|
||||||
|
file_record = await manager.write_file(
|
||||||
|
content=content,
|
||||||
|
filename=filename,
|
||||||
|
path=path,
|
||||||
|
mime_type=mime_type,
|
||||||
|
overwrite=overwrite,
|
||||||
|
)
|
||||||
|
|
||||||
|
return WorkspaceWriteResponse(
|
||||||
|
file_id=file_record.id,
|
||||||
|
name=file_record.name,
|
||||||
|
path=file_record.path,
|
||||||
|
size_bytes=file_record.sizeBytes,
|
||||||
|
message=f"Successfully wrote file: {file_record.name}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except ValueError as e:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error writing workspace file: {e}", exc_info=True)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to write workspace file: {str(e)}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteWorkspaceFileTool(BaseTool):
|
||||||
|
"""Tool for deleting files from workspace."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return "delete_workspace_file"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self) -> str:
|
||||||
|
return (
|
||||||
|
"Delete a file from the user's workspace. "
|
||||||
|
"Specify either file_id or path to identify the file. "
|
||||||
|
"Paths are scoped to the current session by default. "
|
||||||
|
"Use /sessions/<session_id>/... for cross-session access."
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parameters(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"file_id": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The file's unique ID (from list_workspace_files)",
|
||||||
|
},
|
||||||
|
"path": {
|
||||||
|
"type": "string",
|
||||||
|
"description": (
|
||||||
|
"The virtual file path (e.g., '/documents/report.pdf'). "
|
||||||
|
"Scoped to current session by default."
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": [], # At least one must be provided
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def requires_auth(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def _execute(
|
||||||
|
self,
|
||||||
|
user_id: str | None,
|
||||||
|
session: ChatSession,
|
||||||
|
**kwargs,
|
||||||
|
) -> ToolResponseBase:
|
||||||
|
session_id = session.session_id
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Authentication required",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
file_id: Optional[str] = kwargs.get("file_id")
|
||||||
|
path: Optional[str] = kwargs.get("path")
|
||||||
|
|
||||||
|
if not file_id and not path:
|
||||||
|
return ErrorResponse(
|
||||||
|
message="Please provide either file_id or path",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
workspace = await get_or_create_workspace(user_id)
|
||||||
|
# Pass session_id for session-scoped file access
|
||||||
|
manager = WorkspaceManager(user_id, workspace.id, session_id)
|
||||||
|
|
||||||
|
# Determine the file_id to delete
|
||||||
|
target_file_id: str
|
||||||
|
if file_id:
|
||||||
|
target_file_id = file_id
|
||||||
|
else:
|
||||||
|
# path is guaranteed to be non-None here due to the check above
|
||||||
|
assert path is not None
|
||||||
|
file_info = await manager.get_file_info_by_path(path)
|
||||||
|
if file_info is None:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"File not found at path: {path}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
target_file_id = file_info.id
|
||||||
|
|
||||||
|
success = await manager.delete_file(target_file_id)
|
||||||
|
|
||||||
|
if not success:
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"File not found: {target_file_id}",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
return WorkspaceDeleteResponse(
|
||||||
|
file_id=target_file_id,
|
||||||
|
success=True,
|
||||||
|
message="File deleted successfully",
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error deleting workspace file: {e}", exc_info=True)
|
||||||
|
return ErrorResponse(
|
||||||
|
message=f"Failed to delete workspace file: {str(e)}",
|
||||||
|
error=str(e),
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
@@ -638,7 +638,7 @@ async def test_process_review_action_auto_approve_creates_auto_approval_records(
|
|||||||
|
|
||||||
# Mock get_node_executions to return node_id mapping
|
# Mock get_node_executions to return node_id mapping
|
||||||
mock_get_node_executions = mocker.patch(
|
mock_get_node_executions = mocker.patch(
|
||||||
"backend.api.features.executions.review.routes.get_node_executions"
|
"backend.data.execution.get_node_executions"
|
||||||
)
|
)
|
||||||
mock_node_exec = mocker.Mock(spec=NodeExecutionResult)
|
mock_node_exec = mocker.Mock(spec=NodeExecutionResult)
|
||||||
mock_node_exec.node_exec_id = "test_node_123"
|
mock_node_exec.node_exec_id = "test_node_123"
|
||||||
@@ -936,7 +936,7 @@ async def test_process_review_action_auto_approve_only_applies_to_approved_revie
|
|||||||
|
|
||||||
# Mock get_node_executions to return node_id mapping
|
# Mock get_node_executions to return node_id mapping
|
||||||
mock_get_node_executions = mocker.patch(
|
mock_get_node_executions = mocker.patch(
|
||||||
"backend.api.features.executions.review.routes.get_node_executions"
|
"backend.data.execution.get_node_executions"
|
||||||
)
|
)
|
||||||
mock_node_exec = mocker.Mock(spec=NodeExecutionResult)
|
mock_node_exec = mocker.Mock(spec=NodeExecutionResult)
|
||||||
mock_node_exec.node_exec_id = "node_exec_approved"
|
mock_node_exec.node_exec_id = "node_exec_approved"
|
||||||
@@ -1148,7 +1148,7 @@ async def test_process_review_action_per_review_auto_approve_granularity(
|
|||||||
|
|
||||||
# Mock get_node_executions to return batch node data
|
# Mock get_node_executions to return batch node data
|
||||||
mock_get_node_executions = mocker.patch(
|
mock_get_node_executions = mocker.patch(
|
||||||
"backend.api.features.executions.review.routes.get_node_executions"
|
"backend.data.execution.get_node_executions"
|
||||||
)
|
)
|
||||||
# Create mock node executions for each review
|
# Create mock node executions for each review
|
||||||
mock_node_execs = []
|
mock_node_execs = []
|
||||||
|
|||||||
@@ -6,15 +6,10 @@ import autogpt_libs.auth as autogpt_auth_lib
|
|||||||
from fastapi import APIRouter, HTTPException, Query, Security, status
|
from fastapi import APIRouter, HTTPException, Query, Security, status
|
||||||
from prisma.enums import ReviewStatus
|
from prisma.enums import ReviewStatus
|
||||||
|
|
||||||
from backend.copilot.constants import (
|
|
||||||
is_copilot_synthetic_id,
|
|
||||||
parse_node_id_from_exec_id,
|
|
||||||
)
|
|
||||||
from backend.data.execution import (
|
from backend.data.execution import (
|
||||||
ExecutionContext,
|
ExecutionContext,
|
||||||
ExecutionStatus,
|
ExecutionStatus,
|
||||||
get_graph_execution_meta,
|
get_graph_execution_meta,
|
||||||
get_node_executions,
|
|
||||||
)
|
)
|
||||||
from backend.data.graph import get_graph_settings
|
from backend.data.graph import get_graph_settings
|
||||||
from backend.data.human_review import (
|
from backend.data.human_review import (
|
||||||
@@ -27,7 +22,6 @@ from backend.data.human_review import (
|
|||||||
)
|
)
|
||||||
from backend.data.model import USER_TIMEZONE_NOT_SET
|
from backend.data.model import USER_TIMEZONE_NOT_SET
|
||||||
from backend.data.user import get_user_by_id
|
from backend.data.user import get_user_by_id
|
||||||
from backend.data.workspace import get_or_create_workspace
|
|
||||||
from backend.executor.utils import add_graph_execution
|
from backend.executor.utils import add_graph_execution
|
||||||
|
|
||||||
from .model import PendingHumanReviewModel, ReviewRequest, ReviewResponse
|
from .model import PendingHumanReviewModel, ReviewRequest, ReviewResponse
|
||||||
@@ -41,38 +35,6 @@ router = APIRouter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _resolve_node_ids(
|
|
||||||
node_exec_ids: list[str],
|
|
||||||
graph_exec_id: str,
|
|
||||||
is_copilot: bool,
|
|
||||||
) -> dict[str, str]:
|
|
||||||
"""Resolve node_exec_id -> node_id for auto-approval records.
|
|
||||||
|
|
||||||
CoPilot synthetic IDs encode node_id in the format "{node_id}:{random}".
|
|
||||||
Graph executions look up node_id from NodeExecution records.
|
|
||||||
"""
|
|
||||||
if not node_exec_ids:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
if is_copilot:
|
|
||||||
return {neid: parse_node_id_from_exec_id(neid) for neid in node_exec_ids}
|
|
||||||
|
|
||||||
node_execs = await get_node_executions(
|
|
||||||
graph_exec_id=graph_exec_id, include_exec_data=False
|
|
||||||
)
|
|
||||||
node_exec_map = {ne.node_exec_id: ne.node_id for ne in node_execs}
|
|
||||||
|
|
||||||
result = {}
|
|
||||||
for neid in node_exec_ids:
|
|
||||||
if neid in node_exec_map:
|
|
||||||
result[neid] = node_exec_map[neid]
|
|
||||||
else:
|
|
||||||
logger.error(
|
|
||||||
f"Failed to resolve node_id for {neid}: Node execution not found."
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/pending",
|
"/pending",
|
||||||
summary="Get Pending Reviews",
|
summary="Get Pending Reviews",
|
||||||
@@ -147,16 +109,14 @@ async def list_pending_reviews_for_execution(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Verify user owns the graph execution before returning reviews
|
# Verify user owns the graph execution before returning reviews
|
||||||
# (CoPilot synthetic IDs don't have graph execution records)
|
graph_exec = await get_graph_execution_meta(
|
||||||
if not is_copilot_synthetic_id(graph_exec_id):
|
user_id=user_id, execution_id=graph_exec_id
|
||||||
graph_exec = await get_graph_execution_meta(
|
)
|
||||||
user_id=user_id, execution_id=graph_exec_id
|
if not graph_exec:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail=f"Graph execution #{graph_exec_id} not found",
|
||||||
)
|
)
|
||||||
if not graph_exec:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail=f"Graph execution #{graph_exec_id} not found",
|
|
||||||
)
|
|
||||||
|
|
||||||
return await get_pending_reviews_for_execution(graph_exec_id, user_id)
|
return await get_pending_reviews_for_execution(graph_exec_id, user_id)
|
||||||
|
|
||||||
@@ -199,26 +159,30 @@ async def process_review_action(
|
|||||||
)
|
)
|
||||||
|
|
||||||
graph_exec_id = next(iter(graph_exec_ids))
|
graph_exec_id = next(iter(graph_exec_ids))
|
||||||
is_copilot = is_copilot_synthetic_id(graph_exec_id)
|
|
||||||
|
|
||||||
# Validate execution status for graph executions (skip for CoPilot synthetic IDs)
|
# Validate execution status before processing reviews
|
||||||
if not is_copilot:
|
graph_exec_meta = await get_graph_execution_meta(
|
||||||
graph_exec_meta = await get_graph_execution_meta(
|
user_id=user_id, execution_id=graph_exec_id
|
||||||
user_id=user_id, execution_id=graph_exec_id
|
)
|
||||||
|
|
||||||
|
if not graph_exec_meta:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail=f"Graph execution #{graph_exec_id} not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Only allow processing reviews if execution is paused for review
|
||||||
|
# or incomplete (partial execution with some reviews already processed)
|
||||||
|
if graph_exec_meta.status not in (
|
||||||
|
ExecutionStatus.REVIEW,
|
||||||
|
ExecutionStatus.INCOMPLETE,
|
||||||
|
):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_409_CONFLICT,
|
||||||
|
detail=f"Cannot process reviews while execution status is {graph_exec_meta.status}. "
|
||||||
|
f"Reviews can only be processed when execution is paused (REVIEW status). "
|
||||||
|
f"Current status: {graph_exec_meta.status}",
|
||||||
)
|
)
|
||||||
if not graph_exec_meta:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail=f"Graph execution #{graph_exec_id} not found",
|
|
||||||
)
|
|
||||||
if graph_exec_meta.status not in (
|
|
||||||
ExecutionStatus.REVIEW,
|
|
||||||
ExecutionStatus.INCOMPLETE,
|
|
||||||
):
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_409_CONFLICT,
|
|
||||||
detail=f"Cannot process reviews while execution status is {graph_exec_meta.status}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Build review decisions map and track which reviews requested auto-approval
|
# Build review decisions map and track which reviews requested auto-approval
|
||||||
# Auto-approved reviews use original data (no modifications allowed)
|
# Auto-approved reviews use original data (no modifications allowed)
|
||||||
@@ -271,7 +235,7 @@ async def process_review_action(
|
|||||||
)
|
)
|
||||||
return (node_id, False)
|
return (node_id, False)
|
||||||
|
|
||||||
# Collect node_exec_ids that need auto-approval and resolve their node_ids
|
# Collect node_exec_ids that need auto-approval
|
||||||
node_exec_ids_needing_auto_approval = [
|
node_exec_ids_needing_auto_approval = [
|
||||||
node_exec_id
|
node_exec_id
|
||||||
for node_exec_id, review_result in updated_reviews.items()
|
for node_exec_id, review_result in updated_reviews.items()
|
||||||
@@ -279,16 +243,29 @@ async def process_review_action(
|
|||||||
and auto_approve_requests.get(node_exec_id, False)
|
and auto_approve_requests.get(node_exec_id, False)
|
||||||
]
|
]
|
||||||
|
|
||||||
node_id_map = await _resolve_node_ids(
|
# Batch-fetch node executions to get node_ids
|
||||||
node_exec_ids_needing_auto_approval, graph_exec_id, is_copilot
|
|
||||||
)
|
|
||||||
|
|
||||||
# Deduplicate by node_id — one auto-approval per node
|
|
||||||
nodes_needing_auto_approval: dict[str, Any] = {}
|
nodes_needing_auto_approval: dict[str, Any] = {}
|
||||||
for node_exec_id in node_exec_ids_needing_auto_approval:
|
if node_exec_ids_needing_auto_approval:
|
||||||
node_id = node_id_map.get(node_exec_id)
|
from backend.data.execution import get_node_executions
|
||||||
if node_id and node_id not in nodes_needing_auto_approval:
|
|
||||||
nodes_needing_auto_approval[node_id] = updated_reviews[node_exec_id]
|
node_execs = await get_node_executions(
|
||||||
|
graph_exec_id=graph_exec_id, include_exec_data=False
|
||||||
|
)
|
||||||
|
node_exec_map = {node_exec.node_exec_id: node_exec for node_exec in node_execs}
|
||||||
|
|
||||||
|
for node_exec_id in node_exec_ids_needing_auto_approval:
|
||||||
|
node_exec = node_exec_map.get(node_exec_id)
|
||||||
|
if node_exec:
|
||||||
|
review_result = updated_reviews[node_exec_id]
|
||||||
|
# Use the first approved review for this node (deduplicate by node_id)
|
||||||
|
if node_exec.node_id not in nodes_needing_auto_approval:
|
||||||
|
nodes_needing_auto_approval[node_exec.node_id] = review_result
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
f"Failed to create auto-approval record for {node_exec_id}: "
|
||||||
|
f"Node execution not found. This may indicate a race condition "
|
||||||
|
f"or data inconsistency."
|
||||||
|
)
|
||||||
|
|
||||||
# Execute all auto-approval creations in parallel (deduplicated by node_id)
|
# Execute all auto-approval creations in parallel (deduplicated by node_id)
|
||||||
auto_approval_results = await asyncio.gather(
|
auto_approval_results = await asyncio.gather(
|
||||||
@@ -303,11 +280,13 @@ async def process_review_action(
|
|||||||
auto_approval_failed_count = 0
|
auto_approval_failed_count = 0
|
||||||
for result in auto_approval_results:
|
for result in auto_approval_results:
|
||||||
if isinstance(result, Exception):
|
if isinstance(result, Exception):
|
||||||
|
# Unexpected exception during auto-approval creation
|
||||||
auto_approval_failed_count += 1
|
auto_approval_failed_count += 1
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Unexpected exception during auto-approval creation: {result}"
|
f"Unexpected exception during auto-approval creation: {result}"
|
||||||
)
|
)
|
||||||
elif isinstance(result, tuple) and len(result) == 2 and not result[1]:
|
elif isinstance(result, tuple) and len(result) == 2 and not result[1]:
|
||||||
|
# Auto-approval creation failed (returned False)
|
||||||
auto_approval_failed_count += 1
|
auto_approval_failed_count += 1
|
||||||
|
|
||||||
# Count results
|
# Count results
|
||||||
@@ -322,31 +301,30 @@ async def process_review_action(
|
|||||||
if review.status == ReviewStatus.REJECTED
|
if review.status == ReviewStatus.REJECTED
|
||||||
)
|
)
|
||||||
|
|
||||||
# Resume graph execution only for real graph executions (not CoPilot)
|
# Resume execution only if ALL pending reviews for this execution have been processed
|
||||||
# CoPilot sessions are resumed by the LLM retrying run_block with review_id
|
if updated_reviews:
|
||||||
if not is_copilot and updated_reviews:
|
|
||||||
still_has_pending = await has_pending_reviews_for_graph_exec(graph_exec_id)
|
still_has_pending = await has_pending_reviews_for_graph_exec(graph_exec_id)
|
||||||
|
|
||||||
if not still_has_pending:
|
if not still_has_pending:
|
||||||
|
# Get the graph_id from any processed review
|
||||||
first_review = next(iter(updated_reviews.values()))
|
first_review = next(iter(updated_reviews.values()))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Fetch user and settings to build complete execution context
|
||||||
user = await get_user_by_id(user_id)
|
user = await get_user_by_id(user_id)
|
||||||
settings = await get_graph_settings(
|
settings = await get_graph_settings(
|
||||||
user_id=user_id, graph_id=first_review.graph_id
|
user_id=user_id, graph_id=first_review.graph_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Preserve user's timezone preference when resuming execution
|
||||||
user_timezone = (
|
user_timezone = (
|
||||||
user.timezone if user.timezone != USER_TIMEZONE_NOT_SET else "UTC"
|
user.timezone if user.timezone != USER_TIMEZONE_NOT_SET else "UTC"
|
||||||
)
|
)
|
||||||
|
|
||||||
workspace = await get_or_create_workspace(user_id)
|
|
||||||
|
|
||||||
execution_context = ExecutionContext(
|
execution_context = ExecutionContext(
|
||||||
human_in_the_loop_safe_mode=settings.human_in_the_loop_safe_mode,
|
human_in_the_loop_safe_mode=settings.human_in_the_loop_safe_mode,
|
||||||
sensitive_action_safe_mode=settings.sensitive_action_safe_mode,
|
sensitive_action_safe_mode=settings.sensitive_action_safe_mode,
|
||||||
user_timezone=user_timezone,
|
user_timezone=user_timezone,
|
||||||
workspace_id=workspace.id,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
await add_graph_execution(
|
await add_graph_execution(
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user