feat(agent): connect agents to real BOC data
- Add AgentDataProvider with real DB queries for finance, CRM, sales, HR - Inject live data into Anthropic prompts for context-aware responses - Reuse existing ledgerDB connection for agent data access - Agents now respond with actual numbers from the system
This commit is contained in:
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -36,10 +37,11 @@ type AgentChatResponse struct {
|
||||
type AgentOrchestrator struct {
|
||||
anthropicKey string
|
||||
apiEndpoint string
|
||||
dataProvider *AgentDataProvider
|
||||
}
|
||||
|
||||
// NewAgentOrchestrator creates a new agent orchestrator
|
||||
func NewAgentOrchestrator() *AgentOrchestrator {
|
||||
func NewAgentOrchestrator(db, ledgerDB *sql.DB) *AgentOrchestrator {
|
||||
key := os.Getenv("ANTHROPIC_API_KEY")
|
||||
if key == "" {
|
||||
log.Warn().Msg("ANTHROPIC_API_KEY not set, agent will use mock responses")
|
||||
@@ -48,6 +50,7 @@ func NewAgentOrchestrator() *AgentOrchestrator {
|
||||
return &AgentOrchestrator{
|
||||
anthropicKey: key,
|
||||
apiEndpoint: "https://api.anthropic.com/v1/messages",
|
||||
dataProvider: NewAgentDataProvider(db, ledgerDB),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +84,20 @@ func (o *AgentOrchestrator) HandleAgentChat(w http.ResponseWriter, r *http.Reque
|
||||
return
|
||||
}
|
||||
|
||||
// Hämta real-time data för agenten
|
||||
dataContext := ""
|
||||
if o.dataProvider != nil {
|
||||
dataContext = o.dataProvider.FormatDataForPrompt(req.Rum)
|
||||
}
|
||||
|
||||
// Bygg enhanced prompt med data
|
||||
enhancedPrompt := req.SystemPrompt
|
||||
if dataContext != "" {
|
||||
enhancedPrompt = req.SystemPrompt + dataContext
|
||||
}
|
||||
|
||||
// Call Anthropic API
|
||||
svar, err := o.callAnthropic(req.SystemPrompt, req.Meddelanden)
|
||||
svar, err := o.callAnthropic(enhancedPrompt, req.Meddelanden)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("rum", req.Rum).Msg("Agent chat failed")
|
||||
// Fallback to mock
|
||||
|
||||
Reference in New Issue
Block a user