feat(agent): implement core agent functionalities and context management
- Introduced the AgentEngine with methods for executing tool calls, managing context windows, and handling final answer generation. - Added functionality to stream LLM responses and tool results through the EventBus, enhancing real-time interaction. - Implemented context management to consolidate messages and prevent exceeding token limits, ensuring efficient use of context. - Enhanced error handling and logging for tool execution and response analysis, improving robustness and traceability. - Added unit tests to validate the new functionalities and ensure reliability in agent operations.
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultAgentTemperature is the default temperature for the agent
|
||||
DefaultAgentTemperature = 0.7
|
||||
@@ -9,4 +17,51 @@ const (
|
||||
DefaultAgentReflectionEnabled = false
|
||||
// DefaultUseCustomSystemPrompt is the default whether to use custom system prompt for the agent
|
||||
DefaultUseCustomSystemPrompt = false
|
||||
|
||||
// defaultLLMCallTimeout is the default maximum time allowed for a single LLM call.
|
||||
// This prevents a single slow call from consuming the entire pipeline deadline.
|
||||
// Can be overridden via AgentConfig.LLMCallTimeout.
|
||||
defaultLLMCallTimeout = 120 * time.Second
|
||||
|
||||
// defaultToolExecTimeout is the default maximum time for a single tool execution.
|
||||
// Prevents long-running tools (web_fetch, database_query) from hanging indefinitely.
|
||||
defaultToolExecTimeout = 60 * time.Second
|
||||
|
||||
// maxLLMRetries is the maximum number of retries for transient LLM errors.
|
||||
maxLLMRetries = 2
|
||||
)
|
||||
|
||||
// transientErrorMarkers are substrings that indicate a transient (retryable) error.
|
||||
var transientErrorMarkers = []string{
|
||||
"429", "rate limit",
|
||||
"500", "502", "503", "504",
|
||||
"overloaded", "timeout", "timed out",
|
||||
"connection", "server error", "temporarily unavailable",
|
||||
}
|
||||
|
||||
// isTransientError checks whether an error is likely transient and worth retrying.
|
||||
func isTransientError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
errStr := strings.ToLower(err.Error())
|
||||
for _, marker := range transientErrorMarkers {
|
||||
if strings.Contains(errStr, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// getLLMCallTimeout returns the configured LLM call timeout, falling back to default.
|
||||
func (e *AgentEngine) getLLMCallTimeout() time.Duration {
|
||||
if e.config.LLMCallTimeout > 0 {
|
||||
return time.Duration(e.config.LLMCallTimeout) * time.Second
|
||||
}
|
||||
return defaultLLMCallTimeout
|
||||
}
|
||||
|
||||
// generateEventID generates a unique event ID with type suffix for better traceability
|
||||
func generateEventID(suffix string) string {
|
||||
return fmt.Sprintf("%s-%s", uuid.New().String()[:8], suffix)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user