fix(channels): check json marshal/unmarshal errors in toChannelHashes
Replace silently discarded json.Marshal and json.Unmarshal errors with explicit checks. If serialization fails, log a warning and either return early (for the config-level marshal/unmarshal) or skip the channel (for per-channel marshal). This prevents silent data loss when channel configuration contains unexpected types.
This commit is contained in:
parent
413d37494b
commit
7338df2cfb
2 changed files with 20 additions and 5 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/config"
|
"github.com/sipeed/picoclaw/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
@ -11,17 +12,27 @@ import (
|
||||||
func toChannelHashes(cfg *config.Config) map[string]string {
|
func toChannelHashes(cfg *config.Config) map[string]string {
|
||||||
result := make(map[string]string)
|
result := make(map[string]string)
|
||||||
ch := cfg.Channels
|
ch := cfg.Channels
|
||||||
// should not be error
|
marshal, err := json.Marshal(ch)
|
||||||
marshal, _ := json.Marshal(ch)
|
if err != nil {
|
||||||
|
log.Printf("[manager_channel] failed to marshal channels config: %v", err)
|
||||||
|
return result
|
||||||
|
}
|
||||||
var channelConfig map[string]map[string]any
|
var channelConfig map[string]map[string]any
|
||||||
_ = json.Unmarshal(marshal, &channelConfig)
|
if err := json.Unmarshal(marshal, &channelConfig); err != nil {
|
||||||
|
log.Printf("[manager_channel] failed to unmarshal channels config: %v", err)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
for key, value := range channelConfig {
|
for key, value := range channelConfig {
|
||||||
if enabled, ok := value["enabled"].(bool); !ok || !enabled {
|
if enabled, ok := value["enabled"].(bool); !ok || !enabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
hiddenValues(key, value, ch.Get(key))
|
hiddenValues(key, value, ch.Get(key))
|
||||||
valueBytes, _ := json.Marshal(value)
|
valueBytes, err := json.Marshal(value)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[manager_channel] failed to marshal channel %s config: %v", key, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
hash := md5.Sum(valueBytes)
|
hash := md5.Sum(valueBytes)
|
||||||
result[key] = hex.EncodeToString(hash[:])
|
result[key] = hex.EncodeToString(hash[:])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,11 @@ func RunToolLoop(
|
||||||
Content: response.Content,
|
Content: response.Content,
|
||||||
}
|
}
|
||||||
for _, tc := range normalizedToolCalls {
|
for _, tc := range normalizedToolCalls {
|
||||||
argumentsJSON, _ := json.Marshal(tc.Arguments)
|
argumentsJSON, err := json.Marshal(tc.Arguments)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("toolloop: failed to marshal tool call arguments for %s: %v", tc.Name, err)
|
||||||
|
argumentsJSON = []byte("{}")
|
||||||
|
}
|
||||||
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
|
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
|
||||||
ID: tc.ID,
|
ID: tc.ID,
|
||||||
Type: "function",
|
Type: "function",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue