Files
tutti/services/tuttid/api/daemon_agent_targets.go
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

106 lines
4.2 KiB
Go

package api
import (
"context"
"errors"
"strings"
agenttargetapi "github.com/tutti-os/tutti/services/tuttid/api/agenttarget"
tuttigenerated "github.com/tutti-os/tutti/services/tuttid/api/generated"
"github.com/tutti-os/tutti/services/tuttid/apierrors"
agenttargetbiz "github.com/tutti-os/tutti/services/tuttid/biz/agenttarget"
workspacedata "github.com/tutti-os/tutti/services/tuttid/data/workspace"
agenttargetservice "github.com/tutti-os/tutti/services/tuttid/service/agenttarget"
)
type AgentTargetService interface {
List(context.Context) ([]agenttargetbiz.Target, error)
SetEnabled(context.Context, agenttargetservice.SetEnabledInput) (agenttargetbiz.Target, error)
}
func (api DaemonAPI) ListAgentTargets(ctx context.Context, _ tuttigenerated.ListAgentTargetsRequestObject) (tuttigenerated.ListAgentTargetsResponseObject, error) {
if api.AgentTargetService == nil {
return tuttigenerated.ListAgentTargets503JSONResponse{
ServiceUnavailableErrorJSONResponse: serviceUnavailableError(
apierrors.ServiceUnavailable(
"agent_target_service_unavailable",
apierrors.WithDeveloperMessage("agent target service is unavailable"),
),
),
}, nil
}
targets, err := api.AgentTargetService.List(ctx)
if err != nil {
return tuttigenerated.ListAgentTargets502JSONResponse{
PreferencesOperationErrorJSONResponse: preferencesOperationError(
apierrors.PreferencesOperationFailed(apierrors.WithCause(err)),
),
}, nil
}
response, err := agenttargetapi.GeneratedListAgentTargetsResponseFromBiz(targets)
if err != nil {
return tuttigenerated.ListAgentTargets502JSONResponse{
PreferencesOperationErrorJSONResponse: preferencesOperationError(
apierrors.PreferencesOperationFailed(apierrors.WithCause(err)),
),
}, nil
}
return tuttigenerated.ListAgentTargets200JSONResponse(response), nil
}
func (api DaemonAPI) SetSystemAgentTargetEnabled(ctx context.Context, request tuttigenerated.SetSystemAgentTargetEnabledRequestObject) (tuttigenerated.SetSystemAgentTargetEnabledResponseObject, error) {
if api.AgentTargetService == nil {
return tuttigenerated.SetSystemAgentTargetEnabled503JSONResponse{
ServiceUnavailableErrorJSONResponse: serviceUnavailableError(
apierrors.ServiceUnavailable(
"agent_target_service_unavailable",
apierrors.WithDeveloperMessage("agent target service is unavailable"),
),
),
}, nil
}
if request.Body == nil || request.Body.Enabled == nil || strings.TrimSpace(request.AgentTargetID) == "" {
return invalidSetSystemAgentTargetEnabledRequest("agent target id, body, and enabled are required"), nil
}
target, err := api.AgentTargetService.SetEnabled(ctx, agenttargetservice.SetEnabledInput{
ID: request.AgentTargetID,
Enabled: *request.Body.Enabled,
})
if err != nil {
if errors.Is(err, workspacedata.ErrAgentTargetNotFound) ||
errors.Is(err, agenttargetservice.ErrSystemTargetImmutable) ||
errors.Is(err, agenttargetservice.ErrTuttiAgentAlwaysEnabled) ||
errors.Is(err, agenttargetbiz.ErrInvalidTarget) {
return invalidSetSystemAgentTargetEnabledRequest(err.Error()), nil
}
return tuttigenerated.SetSystemAgentTargetEnabled502JSONResponse{
PreferencesOperationErrorJSONResponse: preferencesOperationError(
apierrors.PreferencesOperationFailed(apierrors.WithCause(err)),
),
}, nil
}
response, err := agenttargetapi.GeneratedAgentTargetFromBiz(target)
if err != nil {
return tuttigenerated.SetSystemAgentTargetEnabled502JSONResponse{
PreferencesOperationErrorJSONResponse: preferencesOperationError(
apierrors.PreferencesOperationFailed(apierrors.WithCause(err)),
),
}, nil
}
if target.ID == agenttargetbiz.IDLocalTuttiAgent && api.TuttiAgentReadiness != nil {
api.TuttiAgentReadiness.Trigger("target_enabled_changed")
}
return tuttigenerated.SetSystemAgentTargetEnabled200JSONResponse(response), nil
}
func invalidSetSystemAgentTargetEnabledRequest(message string) tuttigenerated.SetSystemAgentTargetEnabled400JSONResponse {
return tuttigenerated.SetSystemAgentTargetEnabled400JSONResponse{
InvalidRequestErrorJSONResponse: invalidRequestError(
apierrors.InvalidRequest(
"invalid_agent_target_enabled_update",
apierrors.WithDeveloperMessage(message),
),
),
}
}