2026-07-25 12:04:06 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
aitooling "code.tczkiot.com/wlw/ai-agent/internal/ai/tooling"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/dto"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/toolx"
|
2026-07-25 12:04:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// BusinessToolExecutor is the write boundary for built-in business tools.
|
2026-07-27 23:29:02 +08:00
|
|
|
// AgentDesk services remain the write boundary. Workflow nodes invoke this
|
|
|
|
|
// executor only after their human-confirm node has completed.
|
2026-07-25 12:04:06 +08:00
|
|
|
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}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
func (e *businessToolExecutor) Execute(ctx context.Context, input BusinessToolInput) (*BusinessToolResult, error) {
|
2026-07-25 12:04:06 +08:00
|
|
|
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
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
if claim.UnknownOutcome {
|
|
|
|
|
return nil, fmt.Errorf("business tool outcome requires reconciliation; refusing replay: %s", definition.Code)
|
|
|
|
|
}
|
2026-07-25 12:04:06 +08:00
|
|
|
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 {
|
2026-08-28 22:23:13 +08:00
|
|
|
// Built-in write executors may have committed before returning an error.
|
|
|
|
|
// Unless the host explicitly marks the failure as pre-side-effect, never
|
|
|
|
|
// replay the same idempotency key automatically.
|
|
|
|
|
if businessActionFailureIsRetryable(ctx, err) {
|
|
|
|
|
_ = AgentToolInvocationService.FailRetryable(claim.Item, err)
|
|
|
|
|
} else {
|
|
|
|
|
_ = AgentToolInvocationService.MarkUnknownOutcome(claim.Item, err)
|
|
|
|
|
}
|
2026-07-25 12:04:06 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := AgentToolInvocationService.Complete(claim.Item, resultData); err != nil {
|
2026-08-28 22:23:13 +08:00
|
|
|
_ = AgentToolInvocationService.MarkUnknownOutcome(claim.Item, err)
|
2026-07-25 12:04:06 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &BusinessToolResult{Definition: definition, ResultData: resultData}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *businessToolExecutor) execute(toolCode string, input BusinessToolInput) (string, error) {
|
|
|
|
|
switch toolCode {
|
|
|
|
|
case toolx.GraphHandoffConversation.Code:
|
|
|
|
|
result, err := ConversationHumanDispatchService.HandoffByAIWithRequestID(input.Conversation.ID, input.AIAgent, businessToolString(input.Arguments["reason"]), input.IdempotencyKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
return businessToolJSON(map[string]any{"decision": result.Decision, "team_id": result.TeamID, "assignee_id": result.AssigneeID, "message": result.Message})
|
2026-07-25 12:04:06 +08:00
|
|
|
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
|
|
|
|
|
}
|