Skip to main content

AI Integration

Tap uses Claude AI (via the Anthropic SDK) for all of its intelligent features — from crafting unbiased questions during campaign setup, to generating follow-up questions during conversations, to producing comprehensive analysis after a campaign completes.

All AI functions live in a single file: backend/src/services/aiService.js (1000+ lines). They use a utility function extractJSONFromResponse() for safe JSON parsing from AI responses.

AI Model

All functions currently use Claude 3.5 Haiku (claude-3-5-haiku-20241022), chosen for its fast response times and cost efficiency — important because AI calls happen in real time during participant conversations.

AI Functions

Mission Wizard Functions

These functions power the campaign creation flow, helping organizers go from a vague idea to a well-crafted, unbiased question.

1. clarifyMissionInput

What it does: Takes the organizer's initial description of what they want to learn and extracts structured information — the topic, audience, and success criteria. If the input is unclear, it asks one clarifying question.

SettingValue
Max Tokens800
Temperature0
ReturnsObject with status, extracted topic/audience/successCriteria, and optional question

2. refineMissionQuestion

What it does: Turns the organizer's feedback goals into polished survey questions. It detects bias and leading language, then presents 3 question options with rationale for each.

SettingValue
Max Tokens1,500
ReturnsObject with type (clarification, options, or final) and corresponding data

3. generateMissionSummary

What it does: Creates a 2-3 sentence description of the campaign's purpose, written in an engaging tone suitable for invitation emails.

SettingValue
Max Tokens500
Temperature0.7 (more creative)
ReturnsPlain text summary

Conversation Functions

These run in real time during participant chats.

4. generateFollowupQuestion

What it does: Reads the full conversation history and campaign context to generate a relevant follow-up question. When enough depth has been reached, it returns "CONVERSATION_COMPLETE" to signal the end of the chat.

SettingValue
Max Tokens200
ReturnsFollow-up question string or null

This is the most latency-sensitive function — participants are waiting for a response.

5. analyzeResponse

What it does: Analyzes an individual response for sentiment (positive, neutral, or negative) and extracts 3-5 themes.

SettingValue
Max Tokens300
ReturnsObject with sentiment, themes array, and summary

Analysis Functions

These run when a campaign creator requests analysis. They process all conversations in bulk.

6. analyzeSentiment

What it does: Processes all responses in a campaign and calculates sentiment distribution (positive, neutral, negative percentages). Handles large campaigns by batching responses in groups of 50.

SettingValue
Max Tokens500
ReturnsObject with positive/neutral/negative counts and percentages, plus total

7. extractThemes

What it does: Identifies up to 10 common themes across all campaign responses, sorted by how frequently they appear.

SettingValue
Max Tokens1,500
ReturnsArray of objects with name, description, mentionCount, and percentage

8. generateExecutiveSummary

What it does: Creates a comprehensive summary including an overview, key findings, sentiment assessment, top themes, and actionable recommendations.

SettingValue
Max Tokens2,000
ReturnsObject with summary, keyFindings, sentiment, topThemes, recommendations, participationStats

9. generateComprehensiveAnalysis

What it does: Groups conversations by theme, provides a one-sentence campaign summary, 3 key findings, and 3-6 conversation groups with the IDs of conversations in each group.

SettingValue
Max Tokens3,000
ReturnsObject with summary, keyFindings, conversationGroups, overallSentiment, responseCount

10. answerQuery

What it does: Natural language Q&A over campaign response data. A campaign creator can ask questions like "What are the main complaints?" and get an answer with 2-3 supporting quotes from actual responses.

SettingValue
Max Tokens1,500
ReturnsObject with answer, quotes array, and confidence level (high, medium, or low)

Bonus Functions

Two additional functions exist in the source code beyond the original 10:

11. unifiedMissionAgent

A combined clarification and refinement agent that handles the full mission wizard flow from initial input to final question selection in a single function.

12. generateCampaignSummary

Generates a campaign-level summary with themes, sentiment distribution, key insights, and recommendations. Used alongside the other analysis functions.

Error Handling

All AI functions use try-catch blocks with graceful fallback defaults. If an AI call fails, the system returns safe defaults rather than crashing — for example, an empty themes array or a generic "analysis unavailable" message. This ensures that a temporary Anthropic API issue doesn't break the user experience.

JSON Extraction

AI responses sometimes include markdown code blocks around JSON output. The jsonExtractor.js utility uses a ReDoS-safe approach to extract valid JSON from these responses, preventing regex denial-of-service vulnerabilities.