feat(wecom-aibot): add reasoning_channel_id to configuration and enhance message handling limits
This commit is contained in:
parent
a25726e798
commit
e894f8d39a
3 changed files with 27 additions and 14 deletions
|
|
@ -159,7 +159,8 @@
|
||||||
"webhook_port": 18791,
|
"webhook_port": 18791,
|
||||||
"webhook_path": "/webhook/wecom-aibot",
|
"webhook_path": "/webhook/wecom-aibot",
|
||||||
"max_steps": 10,
|
"max_steps": 10,
|
||||||
"welcome_message": "Hello! I'm your AI assistant. How can I help you today?"
|
"welcome_message": "Hello! I'm your AI assistant. How can I help you today?",
|
||||||
|
"reasoning_channel_id": ""
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"providers": {
|
"providers": {
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,7 @@ func NewWeComAIBotChannel(
|
||||||
|
|
||||||
base := channels.NewBaseChannel("wecom_aibot", cfg, messageBus, cfg.AllowFrom,
|
base := channels.NewBaseChannel("wecom_aibot", cfg, messageBus, cfg.AllowFrom,
|
||||||
channels.WithMaxMessageLength(2048),
|
channels.WithMaxMessageLength(2048),
|
||||||
|
channels.WithReasoningChannelID(cfg.ReasoningChannelID),
|
||||||
)
|
)
|
||||||
|
|
||||||
return &WeComAIBotChannel{
|
return &WeComAIBotChannel{
|
||||||
|
|
@ -336,8 +337,9 @@ func (c *WeComAIBotChannel) handleMessageCallback(
|
||||||
timestamp := r.URL.Query().Get("timestamp")
|
timestamp := r.URL.Query().Get("timestamp")
|
||||||
nonce := r.URL.Query().Get("nonce")
|
nonce := r.URL.Query().Get("nonce")
|
||||||
|
|
||||||
// Read request body
|
// Read request body (limit to 4 MB to prevent memory exhaustion)
|
||||||
body, err := io.ReadAll(r.Body)
|
const maxBodySize = 4 << 20 // 4 MB
|
||||||
|
body, err := io.ReadAll(io.LimitReader(r.Body, maxBodySize+1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.ErrorCF("wecom_aibot", "Failed to read request body", map[string]any{
|
logger.ErrorCF("wecom_aibot", "Failed to read request body", map[string]any{
|
||||||
"error": err,
|
"error": err,
|
||||||
|
|
@ -345,6 +347,10 @@ func (c *WeComAIBotChannel) handleMessageCallback(
|
||||||
http.Error(w, "Failed to read body", http.StatusBadRequest)
|
http.Error(w, "Failed to read body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(body) > maxBodySize {
|
||||||
|
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Parse JSON body to get encrypted message
|
// Parse JSON body to get encrypted message
|
||||||
// Format: {"encrypt": "base64_encrypted_string"}
|
// Format: {"encrypt": "base64_encrypted_string"}
|
||||||
|
|
@ -1024,10 +1030,15 @@ func (c *WeComAIBotChannel) downloadAndDecryptImage(
|
||||||
return nil, fmt.Errorf("download failed with status: %d", resp.StatusCode)
|
return nil, fmt.Errorf("download failed with status: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
encryptedData, err := io.ReadAll(resp.Body)
|
// Limit image download to 20 MB to prevent memory exhaustion
|
||||||
|
const maxImageSize = 20 << 20 // 20 MB
|
||||||
|
encryptedData, err := io.ReadAll(io.LimitReader(resp.Body, maxImageSize+1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read image data: %w", err)
|
return nil, fmt.Errorf("failed to read image data: %w", err)
|
||||||
}
|
}
|
||||||
|
if len(encryptedData) > maxImageSize {
|
||||||
|
return nil, fmt.Errorf("image too large (exceeds %d MB)", maxImageSize>>20)
|
||||||
|
}
|
||||||
|
|
||||||
logger.DebugCF("wecom_aibot", "Image downloaded", map[string]any{
|
logger.DebugCF("wecom_aibot", "Image downloaded", map[string]any{
|
||||||
"size": len(encryptedData),
|
"size": len(encryptedData),
|
||||||
|
|
|
||||||
|
|
@ -372,6 +372,7 @@ type WeComAIBotConfig struct {
|
||||||
ReplyTimeout int `json:"reply_timeout" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_REPLY_TIMEOUT"`
|
ReplyTimeout int `json:"reply_timeout" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_REPLY_TIMEOUT"`
|
||||||
MaxSteps int `json:"max_steps" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_MAX_STEPS"` // Maximum streaming steps
|
MaxSteps int `json:"max_steps" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_MAX_STEPS"` // Maximum streaming steps
|
||||||
WelcomeMessage string `json:"welcome_message" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_WELCOME_MESSAGE"` // Sent on enter_chat event; empty = no welcome
|
WelcomeMessage string `json:"welcome_message" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_WELCOME_MESSAGE"` // Sent on enter_chat event; empty = no welcome
|
||||||
|
ReasoningChannelID string `json:"reasoning_channel_id" env:"PICOCLAW_CHANNELS_WECOM_AIBOT_REASONING_CHANNEL_ID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PicoConfig struct {
|
type PicoConfig struct {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue