feat(server): enhance server startup and shutdown process

- Added support for SO_REUSEPORT in the listenWithRetry function to improve port binding during hot-reloads.
- Implemented graceful shutdown by closing the listener immediately upon receiving a shutdown signal, allowing for quicker port release.
- Updated logging to provide clearer feedback during server shutdown and error handling.
This commit is contained in:
wizardchen
2026-03-23 15:14:18 +08:00
committed by lyingbug
parent 39816f2756
commit 0e01a8ca64
10 changed files with 46 additions and 113 deletions
-17
View File
@@ -231,22 +231,5 @@ func (e *AgentEngine) executeToolCalls(
},
})
// Optional: Reflection after each tool call (streaming)
if e.config.ReflectionEnabled && result != nil {
reflection, err := e.streamReflectionToEventBus(
ctx, tc.ID, tc.Function.Name, result.Output,
iteration, sessionID,
)
if err != nil {
logger.Warnf(ctx, "Reflection failed: %v", err)
} else if reflection != "" {
// Store reflection in the corresponding tool call
// Find the tool call we just added and update it
if len(step.ToolCalls) > 0 {
lastIdx := len(step.ToolCalls) - 1
step.ToolCalls[lastIdx].Reflection = reflection
}
}
}
}
}
-2
View File
@@ -13,8 +13,6 @@ const (
DefaultAgentTemperature = 0.7
// DefaultAgentMaxIterations is the default maximum number of iterations for the agent
DefaultAgentMaxIterations = 20
// DefaultAgentReflectionEnabled is the default whether to enable reflection for the agent
DefaultAgentReflectionEnabled = false
// DefaultUseCustomSystemPrompt is the default whether to use custom system prompt for the agent
DefaultUseCustomSystemPrompt = false
-5
View File
@@ -12,7 +12,6 @@ import (
"github.com/Tencent/WeKnora/internal/agent/token"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
)
const (
@@ -317,7 +316,3 @@ const consolidationSystemPrompt = "" +
"- Be structured with clear sections if the conversation covered multiple topics\n" +
"- Be concise — aim for 30% or less of the original length\n\n" +
"Output only the summary, no preamble or explanation."
// ChatResponse is used to extract content from LLM response.
// This avoids a direct dependency on the types package for the response format.
var _ = (*types.ChatResponse)(nil) // compile-time check that types.ChatResponse exists
-54
View File
@@ -68,60 +68,6 @@ func (e *AgentEngine) streamLLMToEventBus(
return result, nil
}
// streamReflectionToEventBus streams reflection process through EventBus
// Note: Reflection is now handled through the think tool in main loop
func (e *AgentEngine) streamReflectionToEventBus(
ctx context.Context,
toolCallID string,
toolName string,
result string,
iteration int,
sessionID string,
) (string, error) {
// Simplified reflection without BuildReflectionPrompt
reflectionPrompt := fmt.Sprintf(`Evaluate the result of calling tool %s and decide the next action.
Tool returned: %s
Think:
1. Does the result satisfy the requirement?
2. What should be done next?`, toolName, result)
messages := []chat.Message{
{Role: "user", Content: reflectionPrompt},
}
// Generate a single ID for this entire reflection stream
reflectionID := generateEventID("reflection")
llmResult, err := e.streamLLMToEventBus(
ctx,
messages,
&chat.ChatOptions{Temperature: 0.5},
func(chunk *types.StreamResponse, fullContent string) {
if chunk.Content != "" {
e.eventBus.Emit(ctx, event.Event{
ID: reflectionID,
Type: event.EventAgentReflection,
SessionID: sessionID,
Data: event.AgentReflectionData{
ToolCallID: toolCallID,
Content: chunk.Content,
Iteration: iteration,
Done: chunk.Done,
},
})
}
},
)
if err != nil {
logger.Warnf(ctx, "Reflection failed: %v", err)
return "", err
}
return llmResult.Content, nil
}
// streamThinkingToEventBus streams the thinking process through EventBus
func (e *AgentEngine) streamThinkingToEventBus(
ctx context.Context,