Intelligent Context System
Context-Aware League Intelligence - Control exactly which documents the AI uses when answering questions about a specific sport and league.
Key Features
1. League Profile View
- View comprehensive league information
- See all documents uploaded since league entered the pipeline
- Track document processing status and metadata
- Monitor document statistics (total, ready, chunks, etc.)
2. Document Context Control
- Include Mode: AI only uses selected documents
- Exclude Mode: AI uses all documents except selected ones
- Toggle individual documents on/off
- Select/deselect all with one click
- Search and filter documents by type
3. Real-Time Context Indicator
- Visual indicator in chat showing active context
- Display selected document count
- Show current sport/league being analyzed
- Clear feedback on include/exclude mode
Architecture
Components
IntelligentContextSheet
The main sheet component that displays:
- League profile card with stats
- Document selection interface
- Search and filter controls
- Context mode switcher (include/exclude)
// components/intelligent-context-sheet.tsx
import { IntelligentContextSheet } from '@/components/intelligent-context-sheet'
<IntelligentContextSheet
sport={currentSport}
league={currentLeague}
onApply={handleContextApply}
/>IntelligentContextBreadcrumb
Enhanced breadcrumb with context trigger:
- Sport/League category navigation
- Context button with document count badge
- Opens IntelligentContextSheet when clicked
import { IntelligentContextBreadcrumb } from '@/components/intelligent-context-breadcrumb'
<IntelligentContextBreadcrumb />State Management
// store/grok-store.ts
interface GrokState {
// Intelligent context - document selection
selectedDocumentIds: string[]
contextMode: 'include' | 'exclude'
// Setters
setSelectedDocumentIds: (ids: string[]) => void
setContextMode: (mode: 'include' | 'exclude') => void
toggleDocumentSelection: (id: string) => void
}Data Layer
// lib/league-context.ts
// Create/manage league contexts
createLeagueContext(leagueId, name, sport, options)
getLeagueContext(leagueId)
setActiveContext(leagueId)
// Document management
addDocumentToLeague(leagueId, document)
getLeagueDocuments(leagueId)
updateDocumentStatus(leagueId, documentId, status)
// Search and recommendations
searchAcrossLeagues(query, options)
getRecommendedContexts(userIntent)User Flow
Select Sport & League
User clicks breadcrumb β Selects category β Selects sport β Selects league
Open Context
User clicks "Context" button β Sheet opens with league profile
View Documents
Sheet displays:
- League name, description, tags
- Statistics (documents, ready, selected, chunks)
- List of all documents with status indicators
Select Documents
User can:
- Click individual documents to toggle
- Use "Select All" for bulk selection
- Search by document name
- Filter by document type
- Switch between Include/Exclude mode
Apply Context
User clicks "Apply Context" β Sheet closes β Context indicator appears in chat
Ask Questions
User types query β AI uses only selected documents β Response shown with context indicator
UI Components
League Profile Card
βββββββββββββββββββββββββββββββββββββββ
β π NBA π β
β Basketball β
βββββββββββββββββββββββββββββββββββββββ€
β National Basketball Association... β
β β
β βββββββ βββββββ βββββββ βββββββ β
β β 8 β β 8 β β 5 β β 267 β β
β β Docsβ βReadyβ β Sel β βChunkβ β
β βββββββ βββββββ βββββββ βββββββ β
β β
β [Basketball] [NBA] [Premier] β
β π
Created Nov 1, 2025 β
βββββββββββββββββββββββββββββββββββββββDocument List Item
βββββββββββββββββββββββββββββββββββββββ
β βοΈ π League Constitution 2024.pdf β
β β
completed β’ 45 chunks β’ 2.3MBβ
β Uploaded Oct 25, 2025 β
βββββββββββββββββββββββββββββββββββββββContext Indicator (Chat)
βββββββββββββββββββββββββββββββββββββββ
β π Context Active: Basketball - NBA β
β 5 documents selected β
β AI will only use selected documents β
βββββββββββββββββββββββββββββββββββββββUsage Examples
Basic Implementation
import { IntelligentContextBreadcrumb } from '@/components/intelligent-context-breadcrumb'
import { IntelligentChatWrapper } from '@/components/intelligent-chat-wrapper'
export default function IntelligencePage() {
return (
<div>
{/* Sport/League Selector with Context Button */}
<IntelligentContextBreadcrumb />
{/* Chat Interface with Context Support */}
<IntelligentChatWrapper mode="vertex-rag" />
</div>
)
}Accessing Context in Code
import { useGrokStore } from '@/store/grok-store'
import { leagueContextManager } from '@/lib/league-context'
function MyComponent() {
const { sport, league, selectedDocumentIds, contextMode } = useGrokStore()
// Get league context
const leagueId = `${sport}-${league}`.toLowerCase()
const context = leagueContextManager.getLeagueContext(leagueId)
const documents = leagueContextManager.getLeagueDocuments(leagueId)
// Filter based on selection
const activeDocuments = contextMode === 'include'
? documents.filter(d => selectedDocumentIds.includes(d.id))
: documents.filter(d => !selectedDocumentIds.includes(d.id))
return (
<div>
<h2>{context.name}</h2>
<p>{activeDocuments.length} active documents</p>
</div>
)
}AI Integration
Backend Integration
const makeAIQuery = async (query: string) => {
const { sport, league, selectedDocumentIds, contextMode } = useGrokStore.getState()
const response = await fetch('/api/chat', {
method: 'POST',
body: JSON.stringify({
query,
context: {
sport,
league,
documentIds: selectedDocumentIds,
mode: contextMode
}
})
})
return response.json()
}Backend Handler Example
# Python backend example
async def handle_chat(request):
query = request.json['query']
context = request.json['context']
# Filter documents based on context
if context['mode'] == 'include':
docs = get_documents_by_ids(context['documentIds'])
else:
all_docs = get_league_documents(context['league'])
docs = [d for d in all_docs if d.id not in context['documentIds']]
# Use filtered docs for RAG
response = await rag_query(query, documents=docs)
return responseDocument Types & Status
Supported Document Types
| Type | Icon | Description |
|---|---|---|
pdf | π | PDF documents |
document | π | Word/text documents |
spreadsheet | π | Excel/CSV files |
image | πΌοΈ | Images |
video | π¬ | Video files |
audio | π΅ | Audio files |
Document Status
| Status | Icon | Description |
|---|---|---|
completed | β | Ready for AI queries |
processing | β³ | Currently processing |
uploading | π€ | Being uploaded |
error | β οΈ | Processing failed |
Configuration
Connecting to Real API
// Replace mock data with real API call
React.useEffect(() => {
const fetchDocuments = async () => {
const response = await fetch(`/api/leagues/${leagueId}/documents`)
const docs = await response.json()
setDocuments(docs)
}
fetchDocuments()
}, [sport, league])Example Use Cases
Sport: Basketball
League: NBA
Documents: [Contract Template.pdf, Salary Cap Rules.pdf]
Mode: Include
Query: "What are the key terms in our standard player contract?"Success Metrics
| Metric | Description |
|---|---|
| Context Usage Rate | % of queries with active context |
| Document Selection Accuracy | How often users adjust selections |
| Query Satisfaction | User feedback on context-aware responses |
| Processing Time | Impact of context size on response time |
| Error Rate | Queries failing due to context issues |
Future Enhancements
- Document Upload: Direct upload from context sheet
- Document Preview: View document contents inline
- Smart Recommendations: AI suggests relevant documents
- Context History: Track which contexts were used for queries
- Bulk Operations: Archive, delete, or tag multiple documents
- Document Versioning: Track document updates over time
- Collaborative Context: Share context selections with team
- Context Templates: Save and reuse common document sets