Port the FastAPI backend to Go 1.26, preserving all API functionality: - auth (register/login/guest/logout/profile/history) with JWT + bcrypt - scripts (list/detail) backed by embedded sample_data.json - games (create/join/start/chat/phase/clue/vote/state/replay) + WebSocket - rule-based NPC agent plus DeepSeek/Ollama AI backends - SQLite persistence via modernc.org/sqlite (pure Go, no cgo) Frontend and documents are unchanged.
39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
package agents
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"mmgame/internal/scripts"
|
|
)
|
|
|
|
type NPCAgent struct {
|
|
Role *scripts.Role
|
|
Background string
|
|
PublicInfo string
|
|
memory []ChatMessage
|
|
}
|
|
|
|
func NewNPCAgent(role *scripts.Role, background, publicInfo string) *NPCAgent {
|
|
return &NPCAgent{Role: role, Background: background, PublicInfo: publicInfo}
|
|
}
|
|
|
|
func (a *NPCAgent) Respond(phaseName string, recentMessages []map[string]string, playerMessage string, isPrivate bool, knownClues []string, phaseGoal string, allRoles []*scripts.Role) map[string]interface{} {
|
|
if len(recentMessages) > 5 {
|
|
recentMessages = recentMessages[len(recentMessages)-5:]
|
|
}
|
|
var recent []string
|
|
for _, m := range recentMessages {
|
|
sender := m["sender_name"]
|
|
if sender == "" {
|
|
sender = m["sender"]
|
|
}
|
|
recent = append(recent, fmt.Sprintf("%s: %s", sender, m["content"]))
|
|
}
|
|
result := NPCGenerate(a.Role, a.Background, phaseName, recent, playerMessage, isPrivate, knownClues, phaseGoal, allRoles)
|
|
if content, ok := result["content"].(string); ok && content != "" {
|
|
a.memory = append(a.memory, ChatMessage{Role: "assistant", Content: content})
|
|
return result
|
|
}
|
|
return map[string]interface{}{"content": a.Role.Name + "陷入了沉思……", "action": "speak"}
|
|
}
|