66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var _ ControlAgent = (*ApprovedOriginator)(nil)
|
|
|
|
// SendControl dispatches a task-level instruction using the same authenticated
|
|
// Agent session as approved execution. Each delivery gets an internal RPC trace
|
|
// ID, but no external command ID, idempotency key or revision CAS is invented.
|
|
func (o *ApprovedOriginator) SendControl(ctx context.Context, spec ControlSpec) error {
|
|
if ctx == nil || o == nil || spec.DispatcherID == "" || spec.DispatcherID != o.DispatcherID || spec.TenantID <= 0 || strings.TrimSpace(spec.TaskID) == "" {
|
|
return errors.New("approved Agent task control identity is incomplete")
|
|
}
|
|
var action agentpb.ControlAction
|
|
switch spec.Action {
|
|
case "pause":
|
|
action = agentpb.ControlAction_CONTROL_ACTION_PAUSE
|
|
case "stop":
|
|
action = agentpb.ControlAction_CONTROL_ACTION_STOP
|
|
case "resume":
|
|
action = agentpb.ControlAction_CONTROL_ACTION_RESUME
|
|
default:
|
|
return errors.New("approved Agent task control action is invalid")
|
|
}
|
|
var policy agentpb.ActiveCallPolicy
|
|
if spec.Action == "resume" {
|
|
if spec.ActiveCallPolicy != "" {
|
|
return errors.New("approved Agent resume cannot carry an active-call policy")
|
|
}
|
|
} else {
|
|
switch spec.ActiveCallPolicy {
|
|
case "hangup":
|
|
policy = agentpb.ActiveCallPolicy_ACTIVE_CALL_POLICY_HANGUP
|
|
case "drain":
|
|
policy = agentpb.ActiveCallPolicy_ACTIVE_CALL_POLICY_DRAIN
|
|
default:
|
|
return errors.New("approved Agent pause/stop requires an active-call policy")
|
|
}
|
|
}
|
|
meta, err := o.activeMeta(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
meta.OperationId = uuid.NewString()
|
|
meta.IdempotencyKey = ""
|
|
response, err := o.Client.ApplyApprovedTaskControl(ctx, &agentpb.ApplyApprovedTaskControlRequest{
|
|
Meta: meta, DispatcherId: spec.DispatcherID, TenantId: spec.TenantID,
|
|
TaskId: spec.TaskID, Action: action, ActiveCallPolicy: policy,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("Agent task control outcome unknown: %w", err)
|
|
}
|
|
if response == nil || !response.Accepted {
|
|
return errors.New("Agent did not confirm task control application")
|
|
}
|
|
return nil
|
|
}
|