Suggestion System
Last Updated: 2025-01-16
Primary Source: docs/project_status.md
Service Files: backend/src/services/suggestionCore.service.ts, backend/src/services/suggestionStorage.service.ts, backend/src/services/proactiveSuggestion.service.ts, backend/src/services/suggestionHistory.service.ts
Adapters: backend/src/services/suggestionAdapters/
Overview
The Suggestion System provides client-agnostic proactive suggestions that guide users through workflows. It analyzes conversation context, generates actionable suggestions, stores them for retrieval, and tracks user interactions for analytics. The system is designed to work across all clients (Discord, Web Portal, API, etc.) using abstract context IDs.
Architecture
Suggestion System Flow
Core Components
1. Suggestion Core Service
File: backend/src/services/suggestionCore.service.ts
Purpose: Core suggestion types, action definitions, and safety policies
Key Types:
SuggestionType (Enum)
BREAK_DOWN_TASKS- Break down tasksRUN_ORCHESTRATOR- Run orchestratorVIEW_ARTIFACTS- View artifactsTRANSITION_STATE- Transition stateAPPROVE_GUARD- Approve guardATTACH_EVIDENCE- Attach evidenceREVIEW_ARTIFACT- Review artifactSELECT_PROTOCOL- Select protocolCREATE_STATE_MACHINE- Create state machineGENERATE_DOCUMENTATION- Generate documentationSUGGEST_NEXT_STEP- Suggest next stepNONE- No suggestion
ActionType (Enum)
EXECUTE- Execute actionVIEW- View information (read-only)APPROVE- Approve guard/transitionCANCEL- Cancel suggestionSHOW_STATUS- Show flow statusASK_QUESTIONS- Ask clarifying questionsTRANSITION- Transition stateATTACH_EVIDENCE- Attach evidenceREVIEW- Review artifact
ActionSafety (Enum)
SAFE- Safe to auto-executeREQUIRES_CONFIRMATION- Optional auto-executeNEVER_AUTO- Never auto-execute
ActionSuggestion (Interface)
interface ActionSuggestion {
id: string;
type: SuggestionType;
message: string;
confidence: number; // 0.0 to 1.0
metadata: Record<string, any>;
actions: ActionableAction[];
timestamp: string;
ttl: number; // Time to live in seconds
priority: number; // Higher = higher priority
context?: {
contextId?: string;
instanceId?: string;
channelId?: string; // Backward compatibility
userId?: number;
protocolId?: string;
state?: string;
};
}AutoExecutionConfig (Interface)
interface AutoExecutionConfig {
autoRunOrchestrator: boolean;
autoTransitionOnGuard: boolean;
autoSuggestNextStep: boolean;
requireConfirmationFor: ActionType[];
neverAutoExecute: ActionType[];
undoWindowMinutes: number;
}Key Functions:
canAutoExecute(action, config)
- Checks if action can be auto-executed
- Considers safety classification
- Respects configuration settings
isSuggestionExpired(suggestion)
- Checks if suggestion has expired
- Based on TTL and timestamp
prioritizeSuggestions(suggestions)
- Sorts suggestions by priority, confidence, timestamp
- Returns prioritized list
2. Suggestion Storage Service
File: backend/src/services/suggestionStorage.service.ts
Purpose: Client-agnostic suggestion storage and retrieval
Key Functions:
storeSuggestion(contextId, suggestion, options)
- Stores suggestion using appropriate backend
- Converts ProactiveSuggestion to ActionSuggestion if needed
- Enhances with context and metadata
- Returns suggestion ID
getActiveSuggestions(contextId)
- Retrieves active, non-expired suggestions
- Filters by status and expiration
- Sorts by priority and timestamp
- Returns top 10 suggestions
clearSuggestions(contextId, suggestionId?)
- Clears all suggestions or specific suggestion
- Marks suggestions as executed/expired
- Updates storage backend
updateSuggestion(contextId, suggestionId, updates)
- Updates specific suggestion
- Supports status, priority, and custom fields
Storage Backends:
- DiscordStorageBackend - Stores in DiscordChannelConfig metadata
- Future: PortalStorageBackend, SlackStorageBackend, etc.
3. Proactive Suggestion Service
File: backend/src/services/proactiveSuggestion.service.ts
Purpose: Analyzes conversation and generates proactive suggestions
Key Functions:
analyzeConversationForProactiveSuggestions(contextId, userId, messageContent, ...)
- Analyzes conversation context
- Checks for state machine/protocol
- Generates appropriate suggestions
- Returns ActionSuggestion
Suggestion Logic:
No State Machine/Protocol:
- Uses intent analysis
- Suggests protocol selection if development intent detected
Has State Machine:
- Checks current state
- Suggests orchestrator, artifacts, transitions based on context
Has Protocol:
- Analyzes protocol context
- Generates protocol-specific suggestions
generateFollowUpSuggestion(contextId, userId, previousType, result, ...)
- Generates follow-up suggestion after action execution
- Context-aware based on previous action
- Returns ActionSuggestion
4. Suggestion History Service
File: backend/src/services/suggestionHistory.service.ts
Purpose: Tracks suggestions for analytics and debugging
Key Functions:
recordSuggestion(payload)
- Records new suggestion in history
- Stores metadata and context
- Returns SuggestionHistory record
recordSuggestionResponse(payload)
- Records user response to suggestion
- Tracks response time
- Updates status (accepted/declined/expired)
recordSuggestionExecution(payload)
- Records action execution result
- Tracks execution time and errors
- Updates status to executed
getSuggestionHistory(channelId, limit)
- Retrieves suggestion history for channel
- Returns recent suggestions
Suggestion Types
Workflow Suggestions
Break Down Tasks
- Suggests breaking down request into tasks
- Triggered in intake state
- Action: Execute task breakdown
Run Orchestrator
- Suggests running orchestrator for pending tasks
- Triggered when tasks are pending
- Action: Execute orchestrator
View Artifacts
- Suggests viewing artifacts
- Triggered when artifacts exist
- Action: View artifacts (auto-executable)
Transition State
- Suggests state transition
- Triggered when guards satisfied
- Action: Transition state (requires confirmation)
Approve Guard
- Suggests approving guard
- Triggered when guard required
- Action: Approve guard
Attach Evidence
- Suggests attaching evidence
- Triggered when evidence required
- Action: Attach evidence
Review Artifact
- Suggests reviewing artifact
- Triggered when artifacts pending review
- Action: Review artifact
Setup Suggestions
Select Protocol
- Suggests selecting protocol
- Triggered when no protocol configured
- Action: Select protocol
Create State Machine
- Suggests creating state machine
- Triggered when protocol selected but no machine
- Action: Create state machine
Action Safety
Safety Classification
Safe Actions (Auto-executable):
VIEW- View informationSHOW_STATUS- Show status
Requires Confirmation:
APPROVE- Approve guardTRANSITION- Transition stateREVIEW- Review artifactASK_QUESTIONS- Ask questions
Never Auto-execute:
EXECUTE- Execute actionCANCEL- Cancel suggestionATTACH_EVIDENCE- Attach evidence
Auto-Execution Rules
- Check
neverAutoExecutelist - Check action safety classification
- Check
requireConfirmationForlist - Check
autoExecutableflag - Return boolean result
Data Flow
Client Adapters
Location: backend/src/services/suggestionAdapters/
Purpose: Translate between client-specific formats and core suggestion format
Adapters:
discordAdapter.service.ts- Discord-specific formattingapiAdapter.service.ts- API-specific formattingmigrationHelper.service.ts- Migration between formats
Integration Points
Auto-Flow Integration
- Uses suggestions for auto-execution
- Checks safety before executing
- Records execution in history
State Machine Integration
- Generates suggestions based on state
- Suggests transitions when guards satisfied
- Suggests orchestrator when tasks pending
Coder Orchestrator Integration
- Suggests running orchestrator
- Suggests viewing artifacts
- Suggests reviewing artifacts
API Endpoints
Suggestion Management
GET /api/suggestions/:contextId- Get active suggestionsPOST /api/suggestions/:contextId/execute- Execute suggestionPOST /api/suggestions/:contextId/clear- Clear suggestionsPUT /api/suggestions/:contextId/:id- Update suggestion
Suggestion History
GET /api/suggestions/history/:channelId- Get suggestion historyGET /api/suggestions/history/user/:userId- Get user historyGET /api/suggestions/history/instance/:instanceId- Get instance history
Source Files
Primary Sources:
backend/src/services/suggestionCore.service.ts- Core types and functionsbackend/src/services/suggestionStorage.service.ts- Storage and retrievalbackend/src/services/proactiveSuggestion.service.ts- Suggestion generationbackend/src/services/suggestionHistory.service.ts- History tracking
Adapters:
backend/src/services/suggestionAdapters/discordAdapter.service.ts- Discord adapterbackend/src/services/suggestionAdapters/apiAdapter.service.ts- API adapterbackend/src/services/suggestionAdapters/migrationHelper.service.ts- Migration helper
Related Files:
backend/src/services/autoFlow.service.ts- Auto-executionbackend/src/services/actionExecutor.service.ts- Action executionbackend/src/models/suggestionHistory.model.ts- History model
Related Documentation
- Auto-Flow System - Workflow intelligence
- State Machine System - Workflow management
- Coder Orchestrator System - Task execution
- Context Services - Client-agnostic context
- 00-OVERVIEW.md - Complete system overview