feat: Enhance AI Agent and Channel Management
- Updated labels in the AI Agents dashboard for clarity, changing "流程状态" to "Playbook 状态" and "未发布流程" to "未发布 Playbook". - Introduced AI Agent rollout percentage management in channel editing, allowing users to set and rollback rollout percentages. - Added new API endpoints for rolling back AI Agent rollout and fetching agent run metrics. - Implemented new UI components for displaying agent run details, including status, duration, and input/output tokens. - Enhanced type definitions for AdminChannel and AIAgent to include rollout percentages and runtime modes. - Updated navigation to include a section for agent runs. - Added new translations for agent run features in both English and Chinese.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
aitooling "agent-desk/internal/ai/tooling"
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/internal/pkg/dto"
|
||||
"agent-desk/internal/pkg/dto/request"
|
||||
"agent-desk/internal/pkg/toolx"
|
||||
)
|
||||
|
||||
// BusinessToolExecutor is the write boundary for built-in business tools.
|
||||
// Autonomous mode deliberately does not expose it; deterministic Playbooks
|
||||
// invoke it only after their human-confirm node has completed.
|
||||
var BusinessToolExecutor = newBusinessToolExecutor(aitooling.DefaultRegistry)
|
||||
|
||||
type BusinessToolInput struct {
|
||||
Conversation models.Conversation
|
||||
AIAgent models.AIAgent
|
||||
ToolCode string
|
||||
Arguments map[string]any
|
||||
IdempotencyKey string
|
||||
Confirmed bool
|
||||
}
|
||||
|
||||
type BusinessToolResult struct {
|
||||
Definition aitooling.Definition
|
||||
ResultData string
|
||||
Reused bool
|
||||
}
|
||||
|
||||
type businessToolExecutor struct {
|
||||
registry *aitooling.Registry
|
||||
}
|
||||
|
||||
func newBusinessToolExecutor(registry *aitooling.Registry) *businessToolExecutor {
|
||||
return &businessToolExecutor{registry: registry}
|
||||
}
|
||||
|
||||
func (e *businessToolExecutor) Execute(_ context.Context, input BusinessToolInput) (*BusinessToolResult, error) {
|
||||
toolCode := toolx.NormalizeToolCodeAlias(strings.TrimSpace(input.ToolCode))
|
||||
definition, err := e.registry.Resolve(toolCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := e.registry.Authorize(definition, aitooling.Policy{AllowedToolCodes: []string{definition.Code}, AllowedRiskLevels: []string{aitooling.RiskLevelWrite}, Confirmed: input.Confirmed}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if input.Conversation.ID <= 0 || strings.TrimSpace(input.IdempotencyKey) == "" {
|
||||
return nil, fmt.Errorf("business tool invocation requires conversation and idempotency key")
|
||||
}
|
||||
claim, err := AgentToolInvocationService.Claim(input.Conversation.ID, input.AIAgent.ID, definition.Code, input.IdempotencyKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if claim == nil || claim.Item == nil {
|
||||
return nil, fmt.Errorf("business tool invocation could not be claimed")
|
||||
}
|
||||
if claim.Completed {
|
||||
return &BusinessToolResult{Definition: definition, ResultData: claim.Item.ResultData, Reused: true}, nil
|
||||
}
|
||||
if !claim.Acquired {
|
||||
return nil, fmt.Errorf("business tool invocation is already running: %s", definition.Code)
|
||||
}
|
||||
|
||||
resultData, err := e.execute(definition.Code, input)
|
||||
if err != nil {
|
||||
_ = AgentToolInvocationService.Fail(claim.Item, err)
|
||||
return nil, err
|
||||
}
|
||||
if err := AgentToolInvocationService.Complete(claim.Item, resultData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &BusinessToolResult{Definition: definition, ResultData: resultData}, nil
|
||||
}
|
||||
|
||||
func (e *businessToolExecutor) execute(toolCode string, input BusinessToolInput) (string, error) {
|
||||
switch toolCode {
|
||||
case toolx.GraphCreateTicketConfirm.Code:
|
||||
item, err := TicketService.CreateFromConversation(request.CreateTicketFromConversationRequest{
|
||||
ConversationID: input.Conversation.ID,
|
||||
Title: businessToolString(input.Arguments["title"]),
|
||||
Description: businessToolString(input.Arguments["description"]),
|
||||
}, businessToolPrincipal(input.AIAgent))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return businessToolJSON(map[string]any{"ticketId": item.ID, "ticketNo": item.TicketNo, "created": true})
|
||||
case toolx.GraphHandoffConversation.Code:
|
||||
result, err := ConversationHumanDispatchService.HandoffByAIWithRequestID(input.Conversation.ID, input.AIAgent, businessToolString(input.Arguments["reason"]), input.IdempotencyKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return businessToolJSON(map[string]any{"decision": result.Decision, "teamId": result.TeamID, "assigneeId": result.AssigneeID, "message": result.Message})
|
||||
default:
|
||||
return "", fmt.Errorf("business tool is not executable: %s", toolCode)
|
||||
}
|
||||
}
|
||||
|
||||
func businessToolString(value any) string {
|
||||
text, _ := value.(string)
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
|
||||
func businessToolPrincipal(agent models.AIAgent) *dto.AuthPrincipal {
|
||||
name := strings.TrimSpace(agent.Name)
|
||||
if name == "" {
|
||||
name = "AI"
|
||||
}
|
||||
return &dto.AuthPrincipal{Username: name, Nickname: name}
|
||||
}
|
||||
|
||||
func businessToolJSON(value any) (string, error) {
|
||||
data, err := json.Marshal(value)
|
||||
return string(data), err
|
||||
}
|
||||
Reference in New Issue
Block a user