refactor(im): improve tool invocation and result formatting
- Updated formatToolCallStart and formatToolCallResult functions to return plain-text lines instead of Markdown snippets for better integration within <think> blocks. - Introduced briefToolSummary function to extract concise summaries from tool output. - Enhanced handleMessageStream to manage <think> blocks more effectively during agent interactions. - Added handling for server-side events in WeCom long connection management to improve reconnection logic.
This commit is contained in:
@@ -39,6 +39,11 @@ const (
|
||||
defaultReconnectBaseDelay = 1 * time.Second
|
||||
defaultReconnectMaxDelay = 30 * time.Second
|
||||
defaultMaxReconnectAttempts = -1 // infinite
|
||||
|
||||
// readTimeout is how long the receive loop waits for any message (including
|
||||
// heartbeat pong) before treating the connection as dead. Set to 3× heartbeat
|
||||
// interval so a single missed pong does not cause a spurious reconnect.
|
||||
readTimeout = 3 * defaultHeartbeatInterval
|
||||
)
|
||||
|
||||
// wsFrame is the JSON frame exchanged over the WeCom bot WebSocket.
|
||||
@@ -85,6 +90,9 @@ type botMessage struct {
|
||||
MsgItem []botMixedItem `json:"msg_item"`
|
||||
} `json:"mixed"`
|
||||
Quote *botMessage `json:"quote,omitempty"` // quoted message (optional)
|
||||
Event struct {
|
||||
EventType string `json:"eventtype"`
|
||||
} `json:"event"`
|
||||
}
|
||||
|
||||
// botMixedItem is one element in a mixed (text+image) message.
|
||||
@@ -149,6 +157,7 @@ func (c *LongConnClient) Start(ctx context.Context) error {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
connectedAt := time.Now()
|
||||
err := c.connectAndRun(ctx)
|
||||
if c.closed.Load() {
|
||||
return nil
|
||||
@@ -157,6 +166,12 @@ func (c *LongConnClient) Start(ctx context.Context) error {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
// If the connection was up for longer than the max backoff window,
|
||||
// the disconnect is likely transient — reset so we retry quickly.
|
||||
if time.Since(connectedAt) > defaultReconnectMaxDelay {
|
||||
attempts = 0
|
||||
}
|
||||
|
||||
attempts++
|
||||
if defaultMaxReconnectAttempts >= 0 && attempts >= defaultMaxReconnectAttempts {
|
||||
return fmt.Errorf("max reconnect attempts reached: %w", err)
|
||||
@@ -323,8 +338,12 @@ func (c *LongConnClient) connectAndRun(ctx context.Context) error {
|
||||
defer heartbeatCancel()
|
||||
go c.heartbeatLoop(heartbeatCtx)
|
||||
|
||||
// Message receive loop
|
||||
// Message receive loop with read deadline.
|
||||
// The deadline is reset on every successful read; if no message arrives
|
||||
// within readTimeout (including heartbeat pong frames), the connection
|
||||
// is considered dead and we fall through to reconnect.
|
||||
for {
|
||||
_ = conn.SetReadDeadline(time.Now().Add(readTimeout))
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read message: %w", err)
|
||||
@@ -405,7 +424,8 @@ func (c *LongConnClient) heartbeatLoop(ctx context.Context) {
|
||||
Headers: map[string]string{"req_id": reqID},
|
||||
}
|
||||
if err := c.writeJSON(frame); err != nil {
|
||||
logger.Warnf(ctx, "[WeCom] Heartbeat failed: %v", err)
|
||||
logger.Warnf(ctx, "[WeCom] Heartbeat failed: %v, closing connection to trigger reconnect", err)
|
||||
c.closeConn()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -426,6 +446,18 @@ func (c *LongConnClient) handleCallback(ctx context.Context, frame wsFrame) {
|
||||
msg.MsgID, msg.MsgType, msg.From.UserID, msg.ChatType,
|
||||
msg.Text.Content, msg.Image.URL, msg.File.URL, msg.Voice.Content, len(msg.Mixed.MsgItem))
|
||||
|
||||
// Handle server-side events (e.g. disconnected_event) before normal messages.
|
||||
if msg.MsgType == "event" {
|
||||
switch msg.Event.EventType {
|
||||
case "disconnected_event":
|
||||
logger.Warnf(ctx, "[WeCom] Server sent disconnected_event, closing connection to trigger reconnect")
|
||||
c.closeConn()
|
||||
default:
|
||||
logger.Infof(ctx, "[WeCom] Ignoring event type: %s", msg.Event.EventType)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
chatType := im.ChatTypeDirect
|
||||
chatID := ""
|
||||
if msg.ChatType == "group" {
|
||||
@@ -582,6 +614,16 @@ func convertMixedMessage(msg *botMessage, chatID string, chatType im.ChatType, r
|
||||
return nil
|
||||
}
|
||||
|
||||
// closeConn forcibly closes the underlying WebSocket, which unblocks any
|
||||
// pending ReadMessage call in the receive loop and triggers a reconnection.
|
||||
func (c *LongConnClient) closeConn() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.conn != nil {
|
||||
_ = c.conn.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *LongConnClient) writeJSON(v interface{}) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user