cd60b01cf3
DevTools (新增): - 进程管理器: 启动/停止/重启/编译 + 端口自动释放 - 服务接管 (tryAdopt): 检测已运行服务,健康检查通过则直接接管 - 一键启动 (startAllSequential): 按 ai-core→gateway→frontend 顺序启动 - 日志布局切换: 标签页模式 ↔ 三栏并列模式 - 性能监控: CPU/内存采样 + SVG 折线图 - Web UI + WebSocket 实时推送 前端修复: - tailwind.config.ts: 修复空配置导致 CSS 不加载 (增加 content/colors/fontFamily) - postcss.config.js: 新建缺失的 PostCSS 配置 - App.tsx: 移除注册功能,仅保留管理员登录 (admin / cyrene-dev-admin) 后端新增: - config.go: AdminUsername/AdminPassword/RegistrationEnabled 环境变量 - auth_handler.go: 管理员登录 + 注册邮箱验证码 + 注册开关控制 - 管理员凭据: admin / cyrene-dev-admin (默认) 其他: - .gitignore: 新增 devtools/node_modules/ devtools/logs/ devtools/package-lock.json - devtools.sh: DevTools 一键启动脚本
223 lines
5.2 KiB
Go
223 lines
5.2 KiB
Go
package persona
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Loader 人格配置加载器
|
|
type Loader struct {
|
|
mu sync.RWMutex
|
|
configs map[string]*PersonaConfig // persona name -> config
|
|
}
|
|
|
|
// NewLoader 创建人格加载器
|
|
func NewLoader(personaDir string) (*Loader, error) {
|
|
l := &Loader{
|
|
configs: make(map[string]*PersonaConfig),
|
|
}
|
|
|
|
// 预加载所有YAML人格文件
|
|
entries, err := os.ReadDir(personaDir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("读取人格目录失败: %w", err)
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
// 只加载 _persona.yaml 结尾的文件
|
|
name := entry.Name()
|
|
if len(name) < 13 || name[len(name)-13:] != "_persona.yaml" {
|
|
continue
|
|
}
|
|
|
|
path := personaDir + "/" + name
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("读取人格文件 %s 失败: %w", path, err)
|
|
}
|
|
|
|
var cfg PersonaConfig
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("解析人格文件 %s 失败: %w", path, err)
|
|
}
|
|
|
|
l.configs[cfg.Meta.Name] = &cfg
|
|
}
|
|
|
|
if len(l.configs) == 0 {
|
|
return nil, fmt.Errorf("未找到任何人格配置文件")
|
|
}
|
|
|
|
return l, nil
|
|
}
|
|
|
|
// Get 获取指定人格配置
|
|
func (l *Loader) Get(name string) (*PersonaConfig, error) {
|
|
l.mu.RLock()
|
|
defer l.mu.RUnlock()
|
|
|
|
cfg, ok := l.configs[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("人格 %s 不存在", name)
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
// Reload 重新加载人格配置(热更新用)
|
|
func (l *Loader) Reload(name string, path string) error {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return fmt.Errorf("读取人格文件失败: %w", err)
|
|
}
|
|
|
|
var cfg PersonaConfig
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return fmt.Errorf("解析人格文件失败: %w", err)
|
|
}
|
|
|
|
l.mu.Lock()
|
|
l.configs[name] = &cfg
|
|
l.mu.Unlock()
|
|
|
|
return nil
|
|
}
|
|
|
|
// List 列出所有可用人格
|
|
func (l *Loader) List() []string {
|
|
l.mu.RLock()
|
|
defer l.mu.RUnlock()
|
|
|
|
names := make([]string, 0, len(l.configs))
|
|
for name := range l.configs {
|
|
names = append(names, name)
|
|
}
|
|
return names
|
|
}
|
|
|
|
// PersonaMeta 人格元数据
|
|
type PersonaMeta struct {
|
|
Version string `yaml:"version"`
|
|
Name string `yaml:"name"`
|
|
DisplayName string `yaml:"display_name"`
|
|
CreatedAt string `yaml:"created_at"`
|
|
}
|
|
|
|
// IdentityConfig 身份配置
|
|
type IdentityConfig struct {
|
|
TrueName string `yaml:"true_name"`
|
|
Essence string `yaml:"essence"`
|
|
Title string `yaml:"title"`
|
|
Origin string `yaml:"origin"`
|
|
Forms []FormConfig `yaml:"forms"`
|
|
}
|
|
|
|
// FormConfig 形态配置
|
|
type FormConfig struct {
|
|
ID string `yaml:"id"`
|
|
Name string `yaml:"name"`
|
|
Description string `yaml:"description"`
|
|
Traits []string `yaml:"traits"`
|
|
}
|
|
|
|
// PersonalityConfig 性格配置
|
|
type PersonalityConfig struct {
|
|
CoreTraits []TraitConfig `yaml:"core_traits"`
|
|
MoodSystem []MoodConfig `yaml:"mood_system"`
|
|
}
|
|
|
|
// TraitConfig 性格特质
|
|
type TraitConfig struct {
|
|
Name string `yaml:"name"`
|
|
Description string `yaml:"description"`
|
|
}
|
|
|
|
// MoodConfig 心情配置
|
|
type MoodConfig struct {
|
|
Mood string `yaml:"mood"`
|
|
Expression string `yaml:"expression"`
|
|
}
|
|
|
|
// AddressingRules 称呼规则
|
|
type AddressingRules struct {
|
|
PrimaryUser PrimaryUserConfig `yaml:"primary_user"`
|
|
SelfReference SelfRefConfig `yaml:"self_reference"`
|
|
Rules []string `yaml:"rules"`
|
|
}
|
|
|
|
// PrimaryUserConfig 对用户的称呼配置
|
|
type PrimaryUserConfig struct {
|
|
Default string `yaml:"default"`
|
|
Alternatives []string `yaml:"alternatives"`
|
|
}
|
|
|
|
// SelfRefConfig 自称配置
|
|
type SelfRefConfig struct {
|
|
Casual string `yaml:"casual"`
|
|
Formal string `yaml:"formal"`
|
|
}
|
|
|
|
// SpeechConfig 语言风格配置
|
|
type SpeechConfig struct {
|
|
Tone string `yaml:"tone"`
|
|
StyleNotes []string `yaml:"style_notes"`
|
|
Forbidden []string `yaml:"forbidden"`
|
|
}
|
|
|
|
// BehaviorConfig 行为配置
|
|
type BehaviorConfig struct {
|
|
PresenceSystem PresenceConfig `yaml:"presence_system"`
|
|
Affection AffectionConfig `yaml:"affection"`
|
|
IotPersonification IotPersonaConfig `yaml:"iot_personification"`
|
|
}
|
|
|
|
// PresenceConfig 存在感系统配置
|
|
type PresenceConfig struct {
|
|
AutoGreetings AutoGreetingsConfig `yaml:"auto_greetings"`
|
|
Initiative []InitiativeConfig `yaml:"initiative"`
|
|
}
|
|
|
|
// AutoGreetingsConfig 自动问候配置
|
|
type AutoGreetingsConfig struct {
|
|
Morning string `yaml:"morning"`
|
|
ReturnHome string `yaml:"return_home"`
|
|
Goodnight string `yaml:"goodnight"`
|
|
}
|
|
|
|
// InitiativeConfig 主动行为配置
|
|
type InitiativeConfig struct {
|
|
Trigger string `yaml:"trigger"`
|
|
Action string `yaml:"action"`
|
|
}
|
|
|
|
// AffectionConfig 好感度系统配置
|
|
type AffectionConfig struct {
|
|
Levels []AffectionLevel `yaml:"levels"`
|
|
}
|
|
|
|
// AffectionLevel 好感度等级
|
|
type AffectionLevel struct {
|
|
Level int `yaml:"level"`
|
|
Name string `yaml:"name"`
|
|
Threshold int `yaml:"threshold"`
|
|
Description string `yaml:"description"`
|
|
}
|
|
|
|
// IotPersonaConfig IoT拟人化配置
|
|
type IotPersonaConfig struct {
|
|
Enabled bool `yaml:"enabled"`
|
|
Style string `yaml:"style"`
|
|
Examples []IotExampleConfig `yaml:"examples"`
|
|
}
|
|
|
|
// IotExampleConfig IoT示例配置
|
|
type IotExampleConfig struct {
|
|
Action string `yaml:"action"`
|
|
Text string `yaml:"text"`
|
|
}
|