feat: Phase 1+2 架构进化 — 连续思考链/主动消息决策/情感状态机/离线自主思考 (86文件)
Phase 1 (基础设施): - ThinkChain 思考链连续性 + 差异化思考提示词 (persistent) - AutonomousToolPolicy 工具安全策略 (safe/unsafe/conditional) - MessageScheduler 自适应消息节奏 (Idle/Available/Busy) - SessionEnrichmentStore 渐进式上下文丰富 (5层) - ConversationBus 事件总线 + ResponseCache (dedup) - pkg/logger 统一日志 + 所有 handler 替换 fmt.Printf - NPE 守卫/链路优化/数据库表修复/Go workspace Phase 2 (人格交互): - EmotionState/EmotionTracker 情感状态机 (5种心情, 情绪衰减) - ProactiveGuard 主动消息多维决策 (静默时段/紧急度/频率/校验) - Gateway↔ai-core 在线状态感知链路 (presence notification) - 离线思考频率控制 + 重连问候 + 离线消息排队 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -341,7 +341,7 @@ func applyFunc(name string, arg float64) (float64, error) {
|
||||
return math.Ceil(arg), nil
|
||||
case "round":
|
||||
return math.Round(arg), nil
|
||||
case "log":
|
||||
case "github.com/yourname/cyrene-ai/pkg/logger":
|
||||
if arg <= 0 {
|
||||
return 0, fmt.Errorf("log 参数必须大于0")
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
@@ -75,13 +75,13 @@ func (c *IoTClient) GetAllDevices(ctx context.Context) ([]IoTDevice, error) {
|
||||
// 请求 API
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", c.baseURL+"/api/v1/devices", nil)
|
||||
if err != nil {
|
||||
log.Printf("[IoT客户端] 创建请求失败: %v", err)
|
||||
logger.Printf("[IoT客户端] 创建请求失败: %v", err)
|
||||
return nil, fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("[IoT客户端] 请求失败: %v", err)
|
||||
logger.Printf("[IoT客户端] 请求失败: %v", err)
|
||||
return nil, fmt.Errorf("获取设备列表失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
@@ -138,27 +138,27 @@ func (c *IoTClient) GetDevice(ctx context.Context, id string) (*IoTDevice, error
|
||||
|
||||
// ToggleDevice 切换设备开关状态
|
||||
func (c *IoTClient) ToggleDevice(id string) error {
|
||||
log.Printf("[IoT-client] 🔄 切换设备: id=%s, url=%s", id, c.baseURL+"/api/v1/devices/"+id+"/toggle")
|
||||
logger.Printf("[IoT-client] 🔄 切换设备: id=%s, url=%s", id, c.baseURL+"/api/v1/devices/"+id+"/toggle")
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/devices/"+id+"/toggle", nil)
|
||||
if err != nil {
|
||||
log.Printf("[IoT-client] ❌ 创建切换请求失败: device=%s, err=%v", id, err)
|
||||
logger.Printf("[IoT-client] ❌ 创建切换请求失败: device=%s, err=%v", id, err)
|
||||
return fmt.Errorf("创建切换请求失败: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("[IoT-client] ❌ 切换设备 HTTP 失败: device=%s, err=%v", id, err)
|
||||
logger.Printf("[IoT-client] ❌ 切换设备 HTTP 失败: device=%s, err=%v", id, err)
|
||||
return fmt.Errorf("切换设备 %s 失败: %w", id, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
log.Printf("[IoT-client] ❌ 设备不存在: %s", id)
|
||||
logger.Printf("[IoT-client] ❌ 设备不存在: %s", id)
|
||||
return fmt.Errorf("设备 %s 不存在", id)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Printf("[IoT-client] ❌ 切换设备返回非200: device=%s, status=%d", id, resp.StatusCode)
|
||||
logger.Printf("[IoT-client] ❌ 切换设备返回非200: device=%s, status=%d", id, resp.StatusCode)
|
||||
return fmt.Errorf("切换设备 %s 返回状态码 %d", id, resp.StatusCode)
|
||||
}
|
||||
|
||||
@@ -167,26 +167,26 @@ func (c *IoTClient) ToggleDevice(id string) error {
|
||||
c.cache = nil
|
||||
c.mu.Unlock()
|
||||
|
||||
log.Printf("[IoT-client] ✅ 切换设备成功: %s", id)
|
||||
logger.Printf("[IoT-client] ✅ 切换设备成功: %s", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDeviceProperty 设置设备属性(温度、亮度、位置、模式、颜色等)
|
||||
func (c *IoTClient) SetDeviceProperty(id string, field string, value interface{}) error {
|
||||
log.Printf("[IoT-client] 🔧 设置设备属性: device=%s, field=%s, value=%v, url=%s", id, field, value, c.baseURL+"/api/v1/devices/"+id+"/set")
|
||||
logger.Printf("[IoT-client] 🔧 设置设备属性: device=%s, field=%s, value=%v, url=%s", id, field, value, c.baseURL+"/api/v1/devices/"+id+"/set")
|
||||
|
||||
body, err := json.Marshal(map[string]interface{}{
|
||||
"field": field,
|
||||
"value": value,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[IoT-client] ❌ 序列化请求失败: device=%s, err=%v", id, err)
|
||||
logger.Printf("[IoT-client] ❌ 序列化请求失败: device=%s, err=%v", id, err)
|
||||
return fmt.Errorf("序列化请求失败: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/devices/"+id+"/set", nil)
|
||||
if err != nil {
|
||||
log.Printf("[IoT-client] ❌ 创建设置请求失败: device=%s, err=%v", id, err)
|
||||
logger.Printf("[IoT-client] ❌ 创建设置请求失败: device=%s, err=%v", id, err)
|
||||
return fmt.Errorf("创建设置请求失败: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -194,13 +194,13 @@ func (c *IoTClient) SetDeviceProperty(id string, field string, value interface{}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("[IoT-client] ❌ 设置设备属性 HTTP 失败: device=%s, field=%s, err=%v", id, field, err)
|
||||
logger.Printf("[IoT-client] ❌ 设置设备属性 HTTP 失败: device=%s, field=%s, err=%v", id, field, err)
|
||||
return fmt.Errorf("设置设备 %s 属性失败: %w", id, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
log.Printf("[IoT-client] ❌ 设备不存在: %s", id)
|
||||
logger.Printf("[IoT-client] ❌ 设备不存在: %s", id)
|
||||
return fmt.Errorf("设备 %s 不存在", id)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
@@ -209,10 +209,10 @@ func (c *IoTClient) SetDeviceProperty(id string, field string, value interface{}
|
||||
}
|
||||
json.NewDecoder(resp.Body).Decode(&errResp)
|
||||
if errResp.Error != "" {
|
||||
log.Printf("[IoT-client] ❌ 设置设备属性失败: device=%s, err=%s", id, errResp.Error)
|
||||
logger.Printf("[IoT-client] ❌ 设置设备属性失败: device=%s, err=%s", id, errResp.Error)
|
||||
return fmt.Errorf("设置设备 %s 属性失败: %s", id, errResp.Error)
|
||||
}
|
||||
log.Printf("[IoT-client] ❌ 设置设备属性返回非200: device=%s, status=%d", id, resp.StatusCode)
|
||||
logger.Printf("[IoT-client] ❌ 设置设备属性返回非200: device=%s, status=%d", id, resp.StatusCode)
|
||||
return fmt.Errorf("设置设备 %s 属性返回状态码 %d", id, resp.StatusCode)
|
||||
}
|
||||
|
||||
@@ -221,7 +221,7 @@ func (c *IoTClient) SetDeviceProperty(id string, field string, value interface{}
|
||||
c.cache = nil
|
||||
c.mu.Unlock()
|
||||
|
||||
log.Printf("[IoT-client] ✅ 设置设备属性成功: device=%s, field=%s, value=%v", id, field, value)
|
||||
logger.Printf("[IoT-client] ✅ 设置设备属性成功: device=%s, field=%s, value=%v", id, field, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ func (c *IoTClient) SetDeviceProperty(id string, field string, value interface{}
|
||||
func (c *IoTClient) GetDevicesForContext(ctx context.Context) []IoTDevice {
|
||||
devices, err := c.GetAllDevices(ctx)
|
||||
if err != nil {
|
||||
log.Printf("[IoT客户端] 获取设备状态摘要失败: %v", err)
|
||||
logger.Printf("[IoT客户端] 获取设备状态摘要失败: %v", err)
|
||||
return nil
|
||||
}
|
||||
return devices
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -52,7 +52,7 @@ func (r *Registry) Register(executor ToolExecutor) {
|
||||
defer r.mu.Unlock()
|
||||
def := executor.Definition()
|
||||
r.tools[def.Name] = executor
|
||||
log.Printf("[工具注册] 已注册工具: %s", def.Name)
|
||||
logger.Printf("[工具注册] 已注册工具: %s", def.Name)
|
||||
}
|
||||
|
||||
// GetDefinitions 获取所有工具定义(用于 LLM function calling)
|
||||
@@ -81,10 +81,10 @@ func (r *Registry) Execute(ctx context.Context, toolName string, arguments map[s
|
||||
}, nil
|
||||
}
|
||||
|
||||
log.Printf("[工具执行] 调用工具 %s,参数: %v", toolName, arguments)
|
||||
logger.Printf("[工具执行] 调用工具 %s,参数: %v", toolName, arguments)
|
||||
result, err := executor.Execute(ctx, arguments)
|
||||
if err != nil {
|
||||
log.Printf("[工具执行] 工具 %s 执行失败: %v", toolName, err)
|
||||
logger.Printf("[工具执行] 工具 %s 执行失败: %v", toolName, err)
|
||||
return &ToolResult{
|
||||
ToolName: toolName,
|
||||
Success: false,
|
||||
@@ -93,9 +93,9 @@ func (r *Registry) Execute(ctx context.Context, toolName string, arguments map[s
|
||||
}
|
||||
|
||||
if result.Success {
|
||||
log.Printf("[工具执行] 工具 %s 执行成功 (数据长度: %d)", toolName, len(result.Data))
|
||||
logger.Printf("[工具执行] 工具 %s 执行成功 (数据长度: %d)", toolName, len(result.Data))
|
||||
} else {
|
||||
log.Printf("[工具执行] 工具 %s 返回错误: %s", toolName, result.Error)
|
||||
logger.Printf("[工具执行] 工具 %s 返回错误: %s", toolName, result.Error)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"github.com/yourname/cyrene-ai/pkg/logger"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -78,7 +78,7 @@ func (c *ToolEngineClient) GetDefinitions(ctx context.Context) ([]ToolDefinition
|
||||
})
|
||||
}
|
||||
|
||||
log.Printf("[tool-engine-client] 从 tool-engine 获取了 %d 个工具定义", len(defs))
|
||||
logger.Printf("[tool-engine-client] 从 tool-engine 获取了 %d 个工具定义", len(defs))
|
||||
return defs, nil
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (c *ToolEngineClient) Execute(ctx context.Context, toolName string, argumen
|
||||
var lastErr error
|
||||
for attempt := 0; attempt <= maxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
log.Printf("[tool-engine-client] 工具 %s 第 %d 次重试 (上次错误: %v)", toolName, attempt, lastErr)
|
||||
logger.Printf("[tool-engine-client] 工具 %s 第 %d 次重试 (上次错误: %v)", toolName, attempt, lastErr)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return &ToolResult{
|
||||
@@ -121,7 +121,7 @@ func (c *ToolEngineClient) Execute(ctx context.Context, toolName string, argumen
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[tool-engine-client] 工具 %s 所有重试均失败 (最后错误: %v)", toolName, lastErr)
|
||||
logger.Printf("[tool-engine-client] 工具 %s 所有重试均失败 (最后错误: %v)", toolName, lastErr)
|
||||
return &ToolResult{
|
||||
ToolName: toolName,
|
||||
Success: false,
|
||||
|
||||
Reference in New Issue
Block a user