Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend): pre-populate CoPilotUnderstanding from Tally form on signup (#12119)

Swifty163b0b3c9de2
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.default
index 2711bd2df9..f73c652387 100644
--- a/autogpt_platform/backend/.env.default
+++ b/autogpt_platform/backend/.env.default
@@ -190,5 +190,8 @@ ZEROBOUNCE_API_KEY=
POSTHOG_API_KEY=
POSTHOG_HOST=https://eu.i.posthog.com
+# Tally Form Integration (pre-populate business understanding on signup)
+TALLY_API_KEY=
+
# Other Services
AUTOMOD_API_KEY=
diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.py
index dd8ef3611f..62f2a769e4 100644
--- a/autogpt_platform/backend/backend/api/features/v1.py
+++ b/autogpt_platform/backend/backend/api/features/v1.py
@@ -126,6 +126,9 @@ v1_router = APIRouter()
########################################################
+_tally_background_tasks: set[asyncio.Task] = set()
+
+
@v1_router.post(
"/auth/user",
summary="Get or create user",
@@ -134,6 +137,24 @@ v1_router = APIRouter()
)
async def get_or_create_user_route(user_data: dict = Security(get_jwt_payload)):
user = await get_or_create_user(user_data)
+
+ # Fire-and-forget: populate business understanding from Tally form.
+ # We use created_at proximity instead of an is_new flag because
+ # get_or_create_user is cached — a separate is_new return value would be
+ # unreliable on repeated calls within the cache TTL.
+ age_seconds = (datetime.now(timezone.utc) - user.created_at).total_seconds()
+ if age_seconds < 30:
+ try:
+ from backend.data.tally import populate_understanding_from_tally
+
+ task = asyncio.create_task(
+ populate_understanding_from_tally(user.id, user.email)
+ )
+ _tally_background_tasks.add(task)
+ task.add_done_callback(_tally_background_tasks.discard)
+ except Exception:
+ logger.debug("Failed to start Tally population task", exc_info=True)
+
return user.model_dump()
diff --git a/autogpt_platform/backend/backend/api/features/v1_test.py b/autogpt_platform/backend/backend/api/features/v1_test.py
index d57ad49949..f0ee22deb6 100644
--- a/autogpt_platform/backend/backend/api/features/v1_test.py
+++ b/autogpt_platform/backend/backend/api/features/v1_test.py
@@ -1,5 +1,5 @@
import json
-from datetime import datetime
+from datetime import datetime, timezone
from io import BytesIO
from unittest.mock import AsyncMock, Mock, patch
@@ -43,6 +43,7 @@ def test_get_or_create_user_route(
) -> None:
"""Test get or create user endpoint"""
mock_user = Mock()
+ mock_user.created_at = datetime.now(timezone.utc)
mock_user.model_dump.return_value = {
"id": test_user_id,
"email": "[email protected]",
diff --git a/autogpt_platform/backend/backend/data/tally.py b/autogpt_platform/backend/backend/data/tally.py
new file mode 100644
index 0000000000..306273ec3d
--- /dev/null
+++ b/autogpt_platform/backend/backend/data/tally.py
@@ -0,0 +1,426 @@
+"""Tally form integration: cache submissions, match by email, extract business understanding."""
+
+import asyncio
+import json
+import logging
+from datetime import datetime, timezone
+from typing import Optional
+
+from openai import AsyncOpenAI
+
+from backend.data.redis_client import get_redis_async
+from backend.data.understanding import (
+ BusinessUnderstandingInput,
+ get_business_understanding,
+ upsert_business_understanding,
+)
+from backend.util.request import Requests
+from backend.util.settings import Settings
+
+logger = logging.getLogger(__name__)
+
+TALLY_API_BASE = "https://api.tally.so"
+_settings = Settings()
+TALLY_FORM_ID = _settings.secrets.tally_form_id
+
+# Redis key templates
+_EMAIL_INDEX_KEY = "tally:form:{form_id}:email_index"
+_QUESTIONS_KEY = "tally:form:{form_id}:questions"
+_LAST_FETCH_KEY = "tally:form:{form_id}:last_fetch"
+
+# TTLs — keep aligned so last_fetch never outlives the index
+_INDEX_TTL = 3600 # 1 hour
+_LAST_FETCH_TTL = 3600 # 1 hour (same as index)
+
+# Pagination
+_PAGE_LIMIT = 500
+_MAX_PAGES = 100
+
+# LLM extraction timeout (seconds)
+_LLM_TIMEOUT = 30
+
+
+def _mask_email(email: str) -> str:
+ """Mask an email for safe logging: '[email protected]' -> 'a***[email protected]'."""
+ try:
+ local, domain = email.rsplit("@", 1)
+ if len(local) <= 2:
+ masked_local = local[0] + "***"
+ else:
+ masked_local = local[0] + "***" + local[-1]
+ return f"{masked_local}@{domain}"
+ except (ValueError, IndexError):
+ return "***"
+
+
+async def _fetch_tally_page(
+ client: Requests,
+ form_id: str,
+ page: int,
+ limit: int = _PAGE_LIMIT,
+ start_date: Optional[str] = None,
+) -> dict:
+ """Fetch a single page of submissions from the Tally API."""
+ url = f"{TALLY_API_BASE}/forms/{form_id}/submissions?page={page}&limit={limit}"
+ if start_date:
+ url += f"&startDate={start_date}"
+
+ response = await client.get(url)
+ return response.json()
+
+
+def _make_tally_client(api_key: str) -> Requests:
+ """Create a Requests client configured for the Tally API."""
+ return Requests(
+ trusted_origins=[TALLY_API_BASE],
+ raise_for_status=True,
+ extra_headers={
+ "Authorization": f"Bearer {api_key}",
+ "Accept": "application/json",
+ },
+ )
+
+
+async def _fetch_all_submissions(
+ client: Requests,
+ form_id: str,
+ start_date: Optional[str] = None,
+ max_pages: int = _MAX_PAGES,
+) -> tuple[list[dict], list[dict]]:
+ """Paginate through all Tally submissions. Returns (questions, submissions)."""
+
+ questions: list[dict] = []
+ all_submissions: list[dict] = []
+ page = 1
+
+ while True:
+ data = await _fetch_tally_page(client, form_id, page, start_date=start_date)
+
+ if page == 1:
+ questions = data.get("questions", [])
+
+ submissions = data.get("submissions", [])
+ all_submissions.extend(submissions)
+
+ # Tally API uses `hasMore` for pagination
+ has_more = data.get("hasMore", False)
+ if not has_more:
+ break
+ if page >= max_pages:
+ total = data.get("totalNumberOfSubmissionsPerFilter", {}).get("all", "?")
+ logger.warning(
+ f"Tally: hit max page cap ({max_pages}) for form {form_id}, "
+ f"fetched {len(all_submissions)} of {total} total submissions"
+ )
+ break
+ page += 1
+
+ return questions, all_submissions
+
+
+def _build_email_index(
+ submissions: list[dict], questions: list[dict]
+) -> dict[str, dict]:
+ """Build an {email -> submission_data} index from submissions.
+
+ Scans question titles for email/contact fields to find the email answer.
+ """
+ # Find question IDs that are likely email fields
+ email_question_ids: list[str] = []
+ for q in questions:
+ label = (q.get("label") or q.get("title") or q.get("name") or "").lower()
+ q_type = (q.get("type") or "").lower()
+ if q_type in ("input_email", "email"):
+ email_question_ids.append(q["id"])
+ elif any(kw in label for kw in ("email", "e-mail", "contact")):
+ email_question_ids.append(q["id"])
+
+ index: dict[str, dict] = {}
+ for sub in submissions:
+ email = _extract_email_from_submission(sub, email_question_ids)
+ if email:
+ index[email.lower()] = {
+ "responses": sub.get("responses", sub.get("fields", [])),
+ "submitted_at": sub.get("submittedAt", sub.get("createdAt", "")),
+ "questions": sub.get("questions", []),
+ }
+ return index
+
+
+def _extract_email_from_submission(
+ submission: dict, email_question_ids: list[str]
+) -> Optional[str]:
+ """Extract email address from a submission by checking respondentEmail, then field responses."""
+ # Try respondent email first (Tally often includes this)
+ respondent_email = submission.get("respondentEmail")
+ if respondent_email:
+ return respondent_email
+
+ # Search through responses/fields for matching question IDs
+ responses = submission.get("responses", submission.get("fields", []))
+ if isinstance(responses, list):
+ for resp in responses:
+ q_id = resp.get("questionId") or resp.get("key") or resp.get("id")
+ if q_id in email_question_ids:
+ value = resp.get("value") or resp.get("answer")
+ if isinstance(value, str) and "@" in value:
+ return value
+ elif isinstance(responses, dict):
+ for q_id in email_question_ids:
+ value = responses.get(q_id)
+ if isinstance(value, str) and "@" in value:
+ return value
+
+ return None
+
+
+async def _get_cached_index(
+ form_id: str,
+) -> tuple[Optional[dict], Optional[list]]:
+ """Return (email_index, questions) from Redis, or (None, None) on cache miss."""
+ redis = await get_redis_async()
+ index_key = _EMAIL_INDEX_KEY.format(form_id=form_id)
+ questions_key = _QUESTIONS_KEY.format(form_id=form_id)
+
+ raw_index = await redis.get(index_key)
+ raw_questions = await redis.get(questions_key)
+
+ if raw_index and raw_questions:
+ return json.loads(raw_index), json.loads(raw_questions)
+ return None, None
+
+
+async def _refresh_cache(form_id: str) -> tuple[dict, list]:
+ """Refresh the Tally submission cache. Uses incremental fetch when possible.
+
+ Returns (email_index, questions).
+ """
+ settings = Settings()
+ client = _make_tally_client(settings.secrets.tally_api_key)
+
+ redis = await get_redis_async()
+ last_fetch_key = _LAST_FETCH_KEY.format(form_id=form_id)
+ index_key = _EMAIL_INDEX_KEY.format(form_id=form_id)
+ questions_key = _QUESTIONS_KEY.format(form_id=form_id)
+
+ last_fetch = await redis.get(last_fetch_key)
+
+ if last_fetch:
+ # Try to load existing index for incremental merge
+ raw_existing = await redis.get(index_key)
+
+ if raw_existing is None:
+ # Index expired but last_fetch still present — fall back to full fetch
+ logger.info("Tally: last_fetch present but index missing, doing full fetch")
+ questions, submissions = await _fetch_all_submissions(client, form_id)
+ email_index = _build_email_index(submissions, questions)
+ else:
+ # Incremental fetch: only get new submissions since last fetch
+ logger.info(f"Tally incremental fetch since {last_fetch}")
+ questions, new_submissions = await _fetch_all_submissions(
+ client, form_id, start_date=last_fetch
+ )
+
+ existing_index: dict[str, dict] = json.loads(raw_existing)
+
+ if not questions:
+ raw_q = await redis.get(questions_key)
+ if raw_q:
+ questions = json.loads(raw_q)
+
+ new_index = _build_email_index(new_submissions, questions)
+ existing_index.update(new_index)
+ email_index = existing_index
+ else:
+ # Full initial fetch
+ logger.info("Tally full initial fetch")
+ questions, submissions = await _fetch_all_submissions(client, form_id)
+ email_index = _build_email_index(submissions, questions)
+
+ # Store in Redis
+ now = datetime.now(timezone.utc).isoformat()
+ await redis.setex(index_key, _INDEX_TTL, json.dumps(email_index))
+ await redis.setex(questions_key, _INDEX_TTL, json.dumps(questions))
+ await redis.setex(last_fetch_key, _LAST_FETCH_TTL, now)
+
+ logger.info(f"Tally cache refreshed: {len(email_index)} emails indexed")
+ return email_index, questions
+
+
+async def find_submission_by_email(
+ form_id: str, email: str
+) -> Optional[tuple[dict, list]]:
+ """Look up a Tally submission by email. Uses cache when available.
+
+ Returns (submission_data, questions) or None.
+ """
+ email_lower = email.lower()
+
+ # Try cache first
+ email_index, questions = await _get_cached_index(form_id)
+ if email_index is not None and questions is not None:
+ sub = email_index.get(email_lower)
+ if sub is not None:
+ return sub, questions
+ return None
+
+ # Cache miss - refresh
+ email_index, questions = await _refresh_cache(form_id)
+ sub = email_index.get(email_lower)
+ if sub is not None:
+ return sub, questions
+ return None
+
+
+def format_submission_for_llm(submission: dict, questions: list[dict]) -> str:
+ """Format a submission as readable Q&A text for LLM consumption."""
+ # Build question ID -> title lookup
+ q_titles: dict[str, str] = {}
+ for q in questions:
+ q_id = q.get("id", "")
+ title = q.get("label") or q.get("title") or q.get("name") or f"Question {q_id}"
+ q_titles[q_id] = title
+
+ lines: list[str] = []
+ responses = submission.get("responses", [])
+
+ if isinstance(responses, list):
+ for resp in responses:
+ q_id = resp.get("questionId") or resp.get("key") or resp.get("id") or ""
+ title = q_titles.get(q_id, f"Question {q_id}")
+ value = resp.get("value") or resp.get("answer") or ""
+ lines.append(f"Q: {title}\nA: {_format_answer(value)}")
+ elif isinstance(responses, dict):
+ for q_id, value in responses.items():
+ title = q_titles.get(q_id, f"Question {q_id}")
+ lines.append(f"Q: {title}\nA: {_format_answer(value)}")
+
+ return "\n\n".join(lines)
+
+
+def _format_answer(value: object) -> str:
+ """Convert an answer value (str, list, dict, None) to a human-readable string."""
+ if value is None:
+ return "(no answer)"
+ if isinstance(value, list):
+ return ", ".join(str(v) for v in value)
+ if isinstance(value, dict):
+ parts = [f"{k}: {v}" for k, v in value.items() if v]
+ return "; ".join(parts) if parts else "(no answer)"
+ return str(value)
+
+
+_EXTRACTION_PROMPT = """\
+You are a business analyst. Given the following form submission data, extract structured business understanding information.
+
+Return a JSON object with ONLY the fields that can be confidently extracted. Use null for fields that cannot be determined.
+
+Fields:
+- user_name (string): the person's name
+- job_title (string): their job title
+- business_name (string): company/business name
+- industry (string): industry or sector
+- business_size (string): company size e.g. "1-10", "11-50", "51-200"
+- user_role (string): their role context e.g. "decision maker", "implementer"
+- key_workflows (list of strings): key business workflows
+- daily_activities (list of strings): daily activities performed
+- pain_points (list of strings): current pain points
+- bottlenecks (list of strings): process bottlenecks
+- manual_tasks (list of strings): manual/repetitive tasks
+- automation_goals (list of strings): desired automation goals
+- current_software (list of strings): software/tools currently used
+- existing_automation (list of strings): existing automations
+- additional_notes (string): any additional context
+
+Form data:
+"""
+
+_EXTRACTION_SUFFIX = "\n\nReturn ONLY valid JSON."
+
+
+async def extract_business_understanding(
+ formatted_text: str,
+) -> BusinessUnderstandingInput:
+ """Use an LLM to extract structured business understanding from form text.
+
+ Raises on timeout or unparseable response so the caller can handle it.
+ """
+ settings = Settings()
+ api_key = settings.secrets.open_router_api_key
+ client = AsyncOpenAI(api_key=api_key, base_url="https://openrouter.ai/api/v1")
+
+ try:
+ response = await asyncio.wait_for(
+ client.chat.completions.create(
+ model="openai/gpt-4o-mini",
+ messages=[
+ {
+ "role": "user",
+ "content": f"{_EXTRACTION_PROMPT}{formatted_text}{_EXTRACTION_SUFFIX}",
+ }
+ ],
+ response_format={"type": "json_object"},
+ temperature=0.0,
+ ),
+ timeout=_LLM_TIMEOUT,
+ )
+ except asyncio.TimeoutError:
+ logger.warning("Tally: LLM extraction timed out")
+ raise
+
+ raw = response.choices[0].message.content or "{}"
+ try:
+ data = json.loads(raw)
+ except json.JSONDecodeError:
+ logger.warning("Tally: LLM returned invalid JSON, skipping extraction")
+ raise
+
+ # Filter out null values before constructing
+ cleaned = {k: v for k, v in data.items() if v is not None}
+ return BusinessUnderstandingInput(**cleaned)
+
+
+async def populate_understanding_from_tally(user_id: str, email: str) -> None:
+ """Main orchestrator: check Tally for a matching submission and populate understanding.
+
+ Fire-and-forget safe — all exceptions are caught and logged.
+ """
+ try:
+ # Check if understanding already exists (idempotency)
+ existing = await get_business_understanding(user_id)
+ if existing is not None:
+ logger.debug(
+ f"Tally: user {user_id} already has business understanding, skipping"
+ )
+ return
+
+ # Check API key is configured
+ settings = Settings()
+ if not settings.secrets.tally_api_key:
+ logger.debug("Tally: no API key configured, skipping")
+ return
+
+ # Look up submission by email
+ masked = _mask_email(email)
+ result = await find_submission_by_email(TALLY_FORM_ID, email)
+ if result is None:
+ logger.debug(f"Tally: no submission found for {masked}")
+ return
+
+ submission, questions = result
+ logger.info(f"Tally: found submission for {masked}, extracting understanding")
+
+ # Format and extract
+ formatted = format_submission_for_llm(submission, questions)
+ if not formatted.strip():
+ logger.warning("Tally: formatted submission was empty, skipping")
+ return
+
+ understanding_input = await extract_business_understanding(formatted)
+
+ # Upsert into database
+ await upsert_business_understanding(user_id, understanding_input)
+ logger.info(f"Tally: successfully populated understanding for user {user_id}")
+
+ except Exception:
+ logger.exception(f"Tally: error populating understanding for user {user_id}")
diff --git a/autogpt_platform/backend/backend/data/tally_test.py b/autogpt_platform/backend/backend/data/tally_test.py
new file mode 100644
index 0000000000..ee8536f253
--- /dev/null
+++ b/autogpt_platform/backend/backend/data/tally_test.py
@@ -0,0 +1,589 @@
+"""Tests for backend.data.tally module."""
+
+import asyncio
+import json
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.data.tally import (
+ _EXTRACTION_PROMPT,
+ _EXTRACTION_SUFFIX,
+ _build_email_index,
+ _format_answer,
+ _make_tally_client,
+ _mask_email,
+ _refresh_cache,
+ extract_business_understanding,
+ find_submission_by_email,
+ format_submission_for_llm,
+ populate_understanding_from_tally,
+)
+
+# ── Fixtures ──────────────────────────────────────────────────────────────────
+
+SAMPLE_QUESTIONS = [
+ {"id": "q1", "label": "What is your name?", "type": "INPUT_TEXT"},
+ {"id": "q2", "label": "Email address", "type": "INPUT_EMAIL"},
+ {"id": "q3", "label": "Company name", "type": "INPUT_TEXT"},
+ {"id": "q4", "label": "Industry", "type": "INPUT_TEXT"},
+]
+
+SAMPLE_SUBMISSIONS = [
+ {
+ "respondentEmail": None,
+ "responses": [
+ {"questionId": "q1", "value": "Alice Smith"},
+ {"questionId": "q2", "value": "[email protected]"},
+ {"questionId": "q3", "value": "Acme Corp"},
+ {"questionId": "q4", "value": "Technology"},
+ ],
+ "submittedAt": "2025-01-15T10:00:00Z",
+ },
+ {
+ "respondentEmail": "[email protected]",
+ "responses": [
+ {"questionId": "q1", "value": "Bob Jones"},
+ {"questionId": "q2", "value": "[email protected]"},
+ {"questionId": "q3", "value": "Bob's Burgers"},
+ {"questionId": "q4", "value": "Food"},
+ ],
+ "submittedAt": "2025-01-16T10:00:00Z",
+ },
+]
+
+
+# ── _build_email_index ────────────────────────────────────────────────────────
+
+
+def test_build_email_index():
+ index = _build_email_index(SAMPLE_SUBMISSIONS, SAMPLE_QUESTIONS)
+ assert "[email protected]" in index
+ assert "[email protected]" in index
+ assert len(index) == 2
+
+
+def test_build_email_index_case_insensitive():
+ submissions = [
+ {
+ "respondentEmail": None,
+ "responses": [
+ {"questionId": "q2", "value": "[email protected]"},
+ ],
+ "submittedAt": "2025-01-15T10:00:00Z",
+ },
+ ]
+ index = _build_email_index(submissions, SAMPLE_QUESTIONS)
+ assert "[email protected]" in index
+ assert "[email protected]" not in index
+
+
+def test_build_email_index_empty():
+ index = _build_email_index([], SAMPLE_QUESTIONS)
+ assert index == {}
+
+
+def test_build_email_index_no_email_field():
+ questions = [{"id": "q1", "label": "Name", "type": "INPUT_TEXT"}]
+ submissions = [
+ {
+ "responses": [{"questionId": "q1", "value": "Alice"}],
+ "submittedAt": "2025-01-15T10:00:00Z",
+ }
+ ]
+ index = _build_email_index(submissions, questions)
+ assert index == {}
+
+
+def test_build_email_index_respondent_email():
+ """respondentEmail takes precedence over field scanning."""
+ submissions = [
+ {
+ "respondentEmail": "[email protected]",
+ "responses": [
+ {"questionId": "q2", "value": "[email protected]"},
+ ],
+ "submittedAt": "2025-01-15T10:00:00Z",
+ }
+ ]
+ index = _build_email_index(submissions, SAMPLE_QUESTIONS)
+ assert "[email protected]" in index
+ assert "[email protected]" not in index
+
+
+# ── format_submission_for_llm ─────────────────────────────────────────────────
+
+
+def test_format_submission_for_llm():
+ submission = {
+ "responses": [
+ {"questionId": "q1", "value": "Alice Smith"},
+ {"questionId": "q3", "value": "Acme Corp"},
+ ],
+ }
+ result = format_submission_for_llm(submission, SAMPLE_QUESTIONS)
+ assert "Q: What is your name?" in result
+ assert "A: Alice Smith" in result
+ assert "Q: Company name" in result
+ assert "A: Acme Corp" in result
+
+
+def test_format_submission_for_llm_dict_responses():
+ submission = {
+ "responses": {
+ "q1": "Alice Smith",
+ "q3": "Acme Corp",
+ },
+ }
+ result = format_submission_for_llm(submission, SAMPLE_QUESTIONS)
+ assert "A: Alice Smith" in result
+ assert "A: Acme Corp" in result
+
+
+def test_format_answer_types():
+ assert _format_answer(None) == "(no answer)"
+ assert _format_answer("hello") == "hello"
+ assert _format_answer(["a", "b"]) == "a, b"
+ assert _format_answer({"key": "val"}) == "key: val"
+ assert _format_answer(42) == "42"
+
+
+# ── find_submission_by_email ──────────────────────────────────────────────────
+
+
+async def test_find_submission_by_email_cache_hit():
+ cached_index = {
+ "[email protected]": {"responses": [], "submitted_at": "2025-01-15"},
+ }
+ cached_questions = SAMPLE_QUESTIONS
+
+ with patch(
+ "backend.data.tally._get_cached_index",
+ new_callable=AsyncMock,
+ return_value=(cached_index, cached_questions),
+ ) as mock_cache:
+ result = await find_submission_by_email("form123", "[email protected]")
+
+ mock_cache.assert_awaited_once_with("form123")
+ assert result is not None
+ sub, questions = result
+ assert sub["submitted_at"] == "2025-01-15"
+
+
+async def test_find_submission_by_email_cache_miss():
+ refreshed_index = {
+ "[email protected]": {"responses": [], "submitted_at": "2025-01-15"},
+ }
+
+ with (
+ patch(
+ "backend.data.tally._get_cached_index",
+ new_callable=AsyncMock,
+ return_value=(None, None),
+ ),
+ patch(
+ "backend.data.tally._refresh_cache",
+ new_callable=AsyncMock,
+ return_value=(refreshed_index, SAMPLE_QUESTIONS),
+ ) as mock_refresh,
+ ):
+ result = await find_submission_by_email("form123", "[email protected]")
+
+ mock_refresh.assert_awaited_once_with("form123")
+ assert result is not None
+
+
+async def test_find_submission_by_email_no_match():
+ cached_index = {
+ "[email protected]": {"responses": [], "submitted_at": "2025-01-15"},
+ }
+
+ with patch(
+ "backend.data.tally._get_cached_index",
+ new_callable=AsyncMock,
+ return_value=(cached_index, SAMPLE_QUESTIONS),
+ ):
+ result = await find_submission_by_email("form123", "[email protected]")
+
+ assert result is None
+
+
+# ── populate_understanding_from_tally ─────────────────────────────────────────
+
+
+async def test_populate_understanding_skips_existing():
+ """If user already has understanding, skip entirely."""
+ mock_understanding = MagicMock()
+
+ with (
+ patch(
+ "backend.data.tally.get_business_understanding",
+ new_callable=AsyncMock,
+ return_value=mock_understanding,
+ ) as mock_get,
+ patch(
+ "backend.data.tally.find_submission_by_email",
+ new_callable=AsyncMock,
+ ) as mock_find,
+ ):
+ await populate_understanding_from_tally("user-1", "[email protected]")
+
+ mock_get.assert_awaited_once_with("user-1")
+ mock_find.assert_not_awaited()
+
+
+async def test_populate_understanding_skips_no_api_key():
+ """If no Tally API key, skip gracefully."""
+ mock_settings = MagicMock()
+ mock_settings.secrets.tally_api_key = ""
+
+ with (
+ patch(
+ "backend.data.tally.get_business_understanding",
+ new_callable=AsyncMock,
+ return_value=None,
+ ),
+ patch("backend.data.tally.Settings", return_value=mock_settings),
+ patch(
+ "backend.data.tally.find_submission_by_email",
+ new_callable=AsyncMock,
+ ) as mock_find,
+ ):
+ await populate_understanding_from_tally("user-1", "[email protected]")
+
+ mock_find.assert_not_awaited()
+
+
+async def test_populate_understanding_handles_errors():
+ """Must never raise, even on unexpected errors."""
+ with patch(
+ "backend.data.tally.get_business_understanding",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("DB down"),
+ ):
+ # Should not raise
+ await populate_understanding_from_tally("user-1", "[email protected]")
+
+
+async def test_populate_understanding_full_flow():
+ """Happy path: no existing understanding, finds submission, extracts, upserts."""
+ mock_settings = MagicMock()
+ mock_settings.secrets.tally_api_key = "test-key"
+
+ submission = {
+ "responses": [
+ {"questionId": "q1", "value": "Alice"},
+ {"questionId": "q3", "value": "Acme"},
+ ],
+ }
+ mock_input = MagicMock()
+
+ with (
+ patch(
+ "backend.data.tally.get_business_understanding",
+ new_callable=AsyncMock,
+ return_value=None,
+ ),
+ patch("backend.data.tally.Settings", return_value=mock_settings),
+ patch(
+ "backend.data.tally.find_submission_by_email",
+ new_callable=AsyncMock,
+ return_value=(submission, SAMPLE_QUESTIONS),
+ ),
+ patch(
+ "backend.data.tally.extract_business_understanding",
+ new_callable=AsyncMock,
+ return_value=mock_input,
+ ) as mock_extract,
+ patch(
+ "backend.data.tally.upsert_business_understanding",
+ new_callable=AsyncMock,
+ ) as mock_upsert,
+ ):
+ await populate_understanding_from_tally("user-1", "[email protected]")
+
+ mock_extract.assert_awaited_once()
+ mock_upsert.assert_awaited_once_with("user-1", mock_input)
+
+
+async def test_populate_understanding_handles_llm_timeout():
+ """LLM timeout is caught and doesn't raise."""
+ import asyncio
+
+ mock_settings = MagicMock()
+ mock_settings.secrets.tally_api_key = "test-key"
+
+ submission = {
+ "responses": [{"questionId": "q1", "value": "Alice"}],
+ }
+
+ with (
+ patch(
+ "backend.data.tally.get_business_understanding",
+ new_callable=AsyncMock,
+ return_value=None,
+ ),
+ patch("backend.data.tally.Settings", return_value=mock_settings),
+ patch(
+ "backend.data.tally.find_submission_by_email",
+ new_callable=AsyncMock,
+ return_value=(submission, SAMPLE_QUESTIONS),
+ ),
+ patch(
+ "backend.data.tally.extract_business_understanding",
+ new_callable=AsyncMock,
+ side_effect=asyncio.TimeoutError(),
+ ),
+ patch(
+ "backend.data.tally.upsert_business_understanding",
+ new_callable=AsyncMock,
+ ) as mock_upsert,
+ ):
+ await populate_understanding_from_tally("user-1", "[email protected]")
+
+ mock_upsert.assert_not_awaited()
+
+
+# ── _mask_email ───────────────────────────────────────────────────────────────
+
+
+def test_mask_email():
+ assert _mask_email("[email protected]") == "a***[email protected]"
+ assert _mask_email("[email protected]") == "a***@example.com"
+ assert _mask_email("[email protected]") == "a***@example.com"
+
+
+def test_mask_email_invalid():
+ assert _mask_email("no-at-sign") == "***"
+
+
+# ── Prompt construction (curly-brace safety) ─────────────────────────────────
+
+
+def test_extraction_prompt_safe_with_curly_braces():
+ """User content with curly braces must not break prompt construction.
+
+ Previously _EXTRACTION_PROMPT.format(submission_text=...) would raise
+ KeyError/ValueError if the user text contained { or }.
+ """
+ text_with_braces = "Q: What tools do you use?\nA: We use {Slack} and {{Jira}}"
+ # This must not raise — the old .format() call would fail here
+ prompt = f"{_EXTRACTION_PROMPT}{text_with_braces}{_EXTRACTION_SUFFIX}"
+ assert text_with_braces in prompt
+ assert prompt.startswith("You are a business analyst.")
+ assert prompt.endswith("Return ONLY valid JSON.")
+
+
+def test_extraction_prompt_no_format_placeholders():
+ """_EXTRACTION_PROMPT must not contain Python format placeholders."""
+ assert "{submission_text}" not in _EXTRACTION_PROMPT
+ # Ensure no stray single-brace placeholders
+ # (double braces {{ are fine — they're literal in format strings)
+ import re
+
+ single_braces = re.findall(r"(?<!\{)\{[^{].*?\}(?!\})", _EXTRACTION_PROMPT)
+ assert single_braces == [], f"Found format placeholders: {single_braces}"
+
+
+# ── extract_business_understanding ────────────────────────────────────────────
+
+
+async def test_extract_business_understanding_success():
+ """Happy path: LLM returns valid JSON that maps to BusinessUnderstandingInput."""
+ mock_choice = MagicMock()
+ mock_choice.message.content = json.dumps(
+ {
+ "user_name": "Alice",
+ "business_name": "Acme Corp",
+ "industry": "Technology",
+ "pain_points": ["manual reporting"],
+ }
+ )
+ mock_response = MagicMock()
+ mock_response.choices = [mock_choice]
+
+ mock_client = AsyncMock()
+ mock_client.chat.completions.create.return_value = mock_response
+
+ with patch("backend.data.tally.AsyncOpenAI", return_value=mock_client):
+ result = await extract_business_understanding("Q: Name?\nA: Alice")
+
+ assert result.user_name == "Alice"
+ assert result.business_name == "Acme Corp"
+ assert result.industry == "Technology"
+ assert result.pain_points == ["manual reporting"]
+
+
+async def test_extract_business_understanding_filters_nulls():
+ """Null values from LLM should be excluded from the result."""
+ mock_choice = MagicMock()
+ mock_choice.message.content = json.dumps(
+ {"user_name": "Alice", "business_name": None, "industry": None}
+ )
+ mock_response = MagicMock()
+ mock_response.choices = [mock_choice]
+
+ mock_client = AsyncMock()
+ mock_client.chat.completions.create.return_value = mock_response
+
+ with patch("backend.data.tally.AsyncOpenAI", return_value=mock_client):
+ result = await extract_business_understanding("Q: Name?\nA: Alice")
+
+ assert result.user_name == "Alice"
+ assert result.business_name is None
+ assert result.industry is None
+
+
+async def test_extract_business_understanding_invalid_json():
+ """Invalid JSON from LLM should raise JSONDecodeError."""
+ mock_choice = MagicMock()
+ mock_choice.message.content = "not valid json {"
+ mock_response = MagicMock()
+ mock_response.choices = [mock_choice]
+
+ mock_client = AsyncMock()
+ mock_client.chat.completions.create.return_value = mock_response
+
+ with (
+ patch("backend.data.tally.AsyncOpenAI", return_value=mock_client),
+ pytest.raises(json.JSONDecodeError),
+ ):
+ await extract_business_understanding("Q: Name?\nA: Alice")
+
+
+async def test_extract_business_understanding_timeout():
+ """LLM timeout should propagate as asyncio.TimeoutError."""
+ mock_client = AsyncMock()
+ mock_client.chat.completions.create.side_effect = asyncio.TimeoutError()
+
+ with (
+ patch("backend.data.tally.AsyncOpenAI", return_value=mock_client),
+ patch("backend.data.tally._LLM_TIMEOUT", 0.001),
+ pytest.raises(asyncio.TimeoutError),
+ ):
+ await extract_business_understanding("Q: Name?\nA: Alice")
+
+
+# ── _refresh_cache ───────────────────────────────────────────────────────────
+
+
+async def test_refresh_cache_full_fetch():
+ """First fetch (no last_fetch in Redis) should do a full fetch and store in Redis."""
+ mock_settings = MagicMock()
+ mock_settings.secrets.tally_api_key = "test-key"
+
+ mock_redis = AsyncMock()
+ mock_redis.get.return_value = None # No last_fetch, no cached index
+
+ questions = SAMPLE_QUESTIONS
+ submissions = SAMPLE_SUBMISSIONS
+
+ with (
+ patch("backend.data.tally.Settings", return_value=mock_settings),
+ patch(
+ "backend.data.tally.get_redis_async",
+ new_callable=AsyncMock,
+ return_value=mock_redis,
+ ),
+ patch(
+ "backend.data.tally._fetch_all_submissions",
+ new_callable=AsyncMock,
+ return_value=(questions, submissions),
+ ) as mock_fetch,
+ ):
+ index, returned_questions = await _refresh_cache("form123")
+
+ mock_fetch.assert_awaited_once()
+ assert "[email protected]" in index
+ assert "[email protected]" in index
+ assert returned_questions == questions
+ # Verify Redis setex was called for index, questions, and last_fetch
+ assert mock_redis.setex.await_count == 3
+
+
+async def test_refresh_cache_incremental_fetch():
+ """When last_fetch and index both exist, should do incremental fetch and merge."""
+ mock_settings = MagicMock()
+ mock_settings.secrets.tally_api_key = "test-key"
+
+ existing_index = {
+ "[email protected]": {"responses": [], "submitted_at": "2025-01-01"}
+ }
+
+ mock_redis = AsyncMock()
+
+ def mock_get(key):
+ if "last_fetch" in key:
+ return "2025-01-14T00:00:00Z"
+ if "email_index" in key:
+ return json.dumps(existing_index)
+ if "questions" in key:
+ return json.dumps(SAMPLE_QUESTIONS)
+ return None
+
+ mock_redis.get.side_effect = mock_get
+
+ new_submissions = [SAMPLE_SUBMISSIONS[0]] # Just Alice
+
+ with (
+ patch("backend.data.tally.Settings", return_value=mock_settings),
+ patch(
+ "backend.data.tally.get_redis_async",
+ new_callable=AsyncMock,
+ return_value=mock_redis,
+ ),
+ patch(
+ "backend.data.tally._fetch_all_submissions",
+ new_callable=AsyncMock,
+ return_value=(SAMPLE_QUESTIONS, new_submissions),
+ ),
+ ):
+ index, _ = await _refresh_cache("form123")
+
+ # Should contain both old and new entries
+ assert "[email protected]" in index
+ assert "[email protected]" in index
+
+
+# ── _make_tally_client ───────────────────────────────────────────────────────
+
+
+def test_make_tally_client_returns_configured_client():
+ """_make_tally_client should create a Requests client with auth headers."""
+ client = _make_tally_client("test-api-key")
+ assert client.extra_headers is not None
+ assert client.extra_headers.get("Authorization") == "Bearer test-api-key"
+
+
+async def test_fetch_tally_page_uses_provided_client():
+ """_fetch_tally_page should use the passed client, not create its own."""
+ from backend.data.tally import _fetch_tally_page
+
+ mock_response = MagicMock()
+ mock_response.json.return_value = {"submissions": [], "questions": []}
+
+ mock_client = AsyncMock()
+ mock_client.get.return_value = mock_response
+
+ result = await _fetch_tally_page(mock_client, "form123", page=1)
+
+ mock_client.get.assert_awaited_once()
+ call_url = mock_client.get.call_args[0][0]
+ assert "form123" in call_url
+ assert "page=1" in call_url
+ assert result == {"submissions": [], "questions": []}
diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py
index 1bd4044709..2c27dcad9f 100644
--- a/autogpt_platform/backend/backend/util/settings.py
+++ b/autogpt_platform/backend/backend/util/settings.py
@@ -691,6 +691,15 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):
screenshotone_api_key: str = Field(default="", description="ScreenshotOne API Key")
+ tally_api_key: str = Field(
+ default="",
+ description="Tally API key for form submission lookup on signup",
+ )
+ tally_form_id: str = Field(
+ default="npGe0q",
+ description="Tally form ID for signup business understanding form",
+ )
+
apollo_api_key: str = Field(default="", description="Apollo API Key")
smartlead_api_key: str = Field(default="", description="SmartLead API Key")
zerobounce_api_key: str = Field(default="", description="ZeroBounce API Key")