refactor: Introduce searchutil package for web search result conversion and keyword score normalization

- Migrate web search result conversion logic to a new searchutil package to avoid code duplication and improve maintainability.
- Implement robust keyword score normalization using percentile bounds in the searchutil package, enhancing score handling across different contexts.
- Update relevant functions in the application to utilize the new searchutil methods, streamlining the codebase.
This commit is contained in:
wizardchen
2025-12-01 18:27:12 +08:00
parent 3e31fdeefd
commit 85b1c4bc49
6 changed files with 293 additions and 408 deletions
+17 -20
View File
@@ -84,22 +84,8 @@ func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, er
return nil, fmt.Errorf("failed to connect to MCP service: %w", err)
}
// Initialize needs a timeout to prevent hanging, but the connection context should remain alive
// Use a timeout context only for the initialization request
initTimeout := 30 * time.Second
if service.AdvancedConfig != nil && service.AdvancedConfig.Timeout > 0 {
initTimeout = time.Duration(service.AdvancedConfig.Timeout) * time.Second
if initTimeout > 60*time.Second {
initTimeout = 60 * time.Second
}
}
initCtx, initCancel := context.WithTimeout(m.ctx, initTimeout)
defer initCancel() // Safe to cancel after init - it's a one-time operation
if _, err := client.Initialize(initCtx); err != nil {
client.Disconnect()
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
if err := m.initializeClient(service, client, "failed to initialize MCP client"); err != nil {
return nil, err
}
// Store client (only for non-stdio transports)
@@ -127,7 +113,16 @@ func (m *MCPManager) createStdioClient(service *types.MCPService) (MCPClient, er
return nil, fmt.Errorf("failed to connect to stdio MCP service: %w", err)
}
// Initialize needs a timeout to prevent hanging
if err := m.initializeClient(service, client, "failed to initialize stdio MCP client"); err != nil {
return nil, err
}
logger.GetLogger(m.ctx).Infof("MCP stdio client created and initialized for service: %s", service.Name)
return client, nil
}
// initializeClient handles the shared initialization flow with timeout enforcement.
func (m *MCPManager) initializeClient(service *types.MCPService, client MCPClient, errPrefix string) error {
initTimeout := 30 * time.Second
if service.AdvancedConfig != nil && service.AdvancedConfig.Timeout > 0 {
initTimeout = time.Duration(service.AdvancedConfig.Timeout) * time.Second
@@ -141,11 +136,13 @@ func (m *MCPManager) createStdioClient(service *types.MCPService) (MCPClient, er
if _, err := client.Initialize(initCtx); err != nil {
client.Disconnect()
return nil, fmt.Errorf("failed to initialize stdio MCP client: %w", err)
if errPrefix == "" {
errPrefix = "failed to initialize MCP client"
}
return fmt.Errorf("%s: %w", errPrefix, err)
}
logger.GetLogger(m.ctx).Infof("MCP stdio client created and initialized for service: %s", service.Name)
return client, nil
return nil
}
// GetClient gets an existing client