Files
tutti/services/tuttid/wiring_agent_service.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

132 lines
4.0 KiB
Go

package main
import (
"context"
"log/slog"
workspacedata "github.com/tutti-os/tutti/services/tuttid/data/workspace"
agentservice "github.com/tutti-os/tutti/services/tuttid/service/agent"
eventstreamservice "github.com/tutti-os/tutti/services/tuttid/service/eventstream"
)
type workspaceAgentTargetResolverSetter interface {
SetWorkspaceAgentTargetResolver(agentservice.WorkspaceAgentTargetResolver)
}
type agentAuthInvalidationSessions interface {
InvalidateLiveComposerModels(provider string)
InvalidateProviderRuntimeCredentials(provider string)
SetLiveModelCatalogUpdated(func(provider string))
}
func configureWorkspaceAgentProjection(
activityProjection workspaceAgentTargetResolverSetter,
workspaceAgentTargets agentservice.WorkspaceAgentTargetResolver,
) {
if workspaceAgentTargets != nil {
activityProjection.SetWorkspaceAgentTargetResolver(workspaceAgentTargets)
}
}
func startAgentModelInvalidationAuthWatcher(
replayComposition bool,
modelCatalog *agentservice.CachedAgentModelCatalog,
sessions agentAuthInvalidationSessions,
events *eventstreamservice.Service,
) *agentservice.ProviderAuthWatcher {
// External credential switchers (for example cc-switch) rewrite provider
// auth/config files without notifying tuttid. Watch those files so model
// catalogs become stale, live provider connections are replaced at their
// next idle send boundary, and the GUI hears about it immediately.
publisher := eventstreamservice.AgentModelCatalogPublisher{Service: events}
publish := func(providers []string, event string, changes []agentservice.ProviderAuthChange) {
if len(providers) == 0 {
return
}
if err := publisher.PublishAgentModelCatalogInvalidated(context.Background(), providers); err != nil {
slog.Warn("agent model catalog invalidation publish failed",
"event", "agent.model_catalog.invalidation_publish_failed",
"providers", providers,
"error", err,
)
}
slog.Info("agent model catalog invalidation published",
"event", event,
"providers", providers,
"changed_files", providerAuthChangeDiagnostics(changes),
)
}
sessions.SetLiveModelCatalogUpdated(func(provider string) {
publish([]string{provider}, "agent.model_catalog.refreshed", nil)
})
watcher := startProviderAuthWatcher(
replayComposition,
nil,
func(changes []agentservice.ProviderAuthChange) {
providers := providerAuthChangeProviders(changes)
modelCatalog.Invalidate(providers...)
for _, provider := range providers {
sessions.InvalidateLiveComposerModels(provider)
sessions.InvalidateProviderRuntimeCredentials(provider)
}
publish(providers, "agent.model_catalog.invalidated", changes)
},
)
if watcher != nil {
modelCatalog.OnRefresh = func(provider string) {
publish([]string{provider}, "agent.model_catalog.refreshed", nil)
}
watcher.OnClose = func() {
_ = modelCatalog.Close()
}
}
return watcher
}
func providerAuthChangeProviders(changes []agentservice.ProviderAuthChange) []string {
providers := make([]string, 0, len(changes))
seen := make(map[string]struct{}, len(changes))
for _, change := range changes {
if change.Provider == "" {
continue
}
if _, ok := seen[change.Provider]; ok {
continue
}
seen[change.Provider] = struct{}{}
providers = append(providers, change.Provider)
}
return providers
}
func providerAuthChangeDiagnostics(changes []agentservice.ProviderAuthChange) []map[string]string {
if len(changes) == 0 {
return nil
}
result := make([]map[string]string, 0, len(changes))
for _, change := range changes {
result = append(result, map[string]string{
"provider": change.Provider,
"path": change.Path,
"kind": change.Kind,
})
}
return result
}
func agentWorkspaceIDs(
store workspacedata.CatalogStore,
) func(context.Context) ([]string, error) {
return func(ctx context.Context) ([]string, error) {
workspaces, err := store.List(ctx)
if err != nil {
return nil, err
}
ids := make([]string, 0, len(workspaces))
for _, workspace := range workspaces {
ids = append(ids, workspace.ID)
}
return ids, nil
}
}