Files
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

158 lines
5.5 KiB
Go

package agentcontext
import (
"context"
"fmt"
"strings"
agentactivitybiz "github.com/tutti-os/tutti/packages/agent/store-sqlite"
"github.com/tutti-os/tutti/services/tuttid/biz/agentgui"
agenttargetbiz "github.com/tutti-os/tutti/services/tuttid/biz/agenttarget"
preferencesbiz "github.com/tutti-os/tutti/services/tuttid/biz/preferences"
agentservice "github.com/tutti-os/tutti/services/tuttid/service/agent"
agentextensionservice "github.com/tutti-os/tutti/services/tuttid/service/agentextension"
cliservice "github.com/tutti-os/tutti/services/tuttid/service/cli"
)
const appID = "agent-context"
type AgentSessions interface {
CancelTurn(context.Context, string, string, string) (agentservice.CancelTurnResult, error)
CreateWithResult(context.Context, string, agentservice.CreateSessionInput) (agentservice.CreateSessionResult, error)
Get(context.Context, string, string) (agentservice.Session, error)
GetComposerOptions(context.Context, agentservice.ComposerOptionsInput) (agentservice.ComposerOptions, error)
GetSkillBundle(context.Context, string, agentservice.SkillBundleInput) (agentservice.SkillBundle, error)
GetTurn(context.Context, string, string, string) (agentactivitybiz.Turn, bool, error)
List(context.Context, string) ([]agentservice.Session, error)
ListActivePeers(context.Context, string) (agentservice.ActivePeers, error)
ListMessages(context.Context, string, string, agentservice.ListMessagesInput) (agentservice.SessionMessagesPage, error)
ListTurns(context.Context, string, string, agentservice.ListTurnsInput) (agentservice.TurnPage, error)
ListProviderAvailability(context.Context, agentservice.ProviderAvailabilityInput) ([]agentservice.ProviderAvailability, error)
LocalAttachmentPath(context.Context, string, string, string, string) (string, error)
Respond(context.Context, agentservice.RespondInput) (agentservice.RespondResult, error)
SendInput(context.Context, string, string, agentservice.SendInput) (agentservice.SendInputResult, error)
Wait(context.Context, agentservice.WaitInput) (agentservice.WaitResult, error)
}
type AgentGUILaunchPublisher interface {
PublishAgentGUILaunchRequested(context.Context, agentgui.LaunchRequest) error
}
type DesktopPreferencesReader interface {
Get(context.Context) (preferencesbiz.DesktopPreferences, error)
}
type AgentTargetLister interface {
List(context.Context) ([]agenttargetbiz.Target, error)
}
type AgentTargetSetupReader interface {
GetSetup(context.Context, agentextensionservice.InstallPlanInput) (agentextensionservice.SetupSnapshot, error)
}
type Provider struct {
workspaces cliservice.WorkspaceCatalog
sessions AgentSessions
launchPublisher AgentGUILaunchPublisher
preferences DesktopPreferencesReader
agentTargets AgentTargetLister
extensionAvailabilityCache *extensionAvailabilityCache
}
func (p Provider) WithAgentTargetSetup(setup AgentTargetSetupReader) Provider {
p.extensionAvailabilityCache = newExtensionAvailabilityCache(setup)
return p
}
func NewProviderWithAgentTargets(
workspaces cliservice.WorkspaceCatalog,
sessions AgentSessions,
launchPublisher AgentGUILaunchPublisher,
agentTargets AgentTargetLister,
preferences ...DesktopPreferencesReader,
) Provider {
provider := NewProviderWithLaunchPublisher(workspaces, sessions, launchPublisher, preferences...)
provider.agentTargets = agentTargets
return provider
}
func NewProviderWithLaunchPublisher(
workspaces cliservice.WorkspaceCatalog,
sessions AgentSessions,
launchPublisher AgentGUILaunchPublisher,
preferences ...DesktopPreferencesReader,
) Provider {
var preferencesReader DesktopPreferencesReader
if len(preferences) > 0 {
preferencesReader = preferences[0]
}
return Provider{
workspaces: workspaces,
sessions: sessions,
launchPublisher: launchPublisher,
preferences: preferencesReader,
}
}
func (Provider) AppID() string {
return appID
}
func (p Provider) Commands() []cliservice.Command {
return []cliservice.Command{
p.newAgentsCommand(),
p.newLegacyProvidersCommand(),
p.newComposerOptionsCommand(),
p.newSkillBundleCommand(),
p.newLegacyCodexStartCommand(),
p.newLegacyClaudeStartCommand(),
p.newStartCommand(),
p.newGetCommand(),
p.newOpenCommand(),
p.newSendCommand(),
p.newRespondCommand(),
p.newCancelTurnCommand(),
p.newLegacyCancelCommand(),
p.newSessionsCommand([]string{"agent", "sessions"}, appID+".agent.sessions"),
p.newWaitCommand(),
p.newSessionSummaryCommand(),
p.newTurnResourcesCommand(),
p.newActivePeersCommand(),
}
}
func (p Provider) requireSessions() error {
if p.sessions == nil {
return agentservice.ErrInvalidArgument
}
return nil
}
func (p Provider) enabledAgentTargets(ctx context.Context) ([]agenttargetbiz.Target, error) {
if p.agentTargets == nil {
return nil, agentservice.ErrInvalidArgument
}
targets, err := p.agentTargets.List(ctx)
if err != nil {
return nil, err
}
return agenttargetbiz.EnabledTargets(targets), nil
}
func (p Provider) resolveEnabledAgentTarget(ctx context.Context, agentID string) (agenttargetbiz.Target, error) {
agentID = strings.TrimSpace(agentID)
if agentID == "" {
return agenttargetbiz.Target{}, fmt.Errorf("%w: agent id is required; run agent list --json", cliservice.ErrInvalidInput)
}
targets, err := p.enabledAgentTargets(ctx)
if err != nil {
return agenttargetbiz.Target{}, err
}
for _, target := range targets {
if target.ID == agentID {
return target, nil
}
}
return agenttargetbiz.Target{}, fmt.Errorf("%w: enabled agent %q was not found; run agent list --json", cliservice.ErrInvalidInput, agentID)
}