package daemon import ( "context" "fmt" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/taskstate" ) // ModelEgress opens a task's egress session. A Docker container holds a // sentinel under each provider's key variable and reaches the providers, and // the extra hosts, through the egress proxy, which swaps in the org's bound // key and refuses every other host. type ModelEgress interface { Open(ctx context.Context, token, org string, providers map[string]config.Provider, hosts []string) (ModelSession, error) Close(ctx context.Context, token string) } // TaskInstructions answers the instance and organisation instructions for a // task's org. type TaskInstructions interface { TaskInstructions(ctx context.Context, taskID int64) (instance, organisation string, err error) } // TaskModels answers what a task's org resolves models against. type TaskModels interface { TaskModels(ctx context.Context, taskID int64) (config.OrgModels, error) } // taskConfig is task's configuration with its org's model aliases and own // providers, narrowed to the providers those aliases reach, each carrying the // variable its credential is read from. A provider no alias names is not part // of the run at all: requiring its credential would park a task whose models // never touch it. func (d *Daemon) taskConfig(ctx context.Context, task *workflow.Task) (config.Config, error) { cfg := d.configFor(task) models, err := d.TaskModels.TaskModels(ctx, task.ID) if err != nil { return config.Config{}, fmt.Errorf("resolve the task's models: %w", err) } cfg = cfg.WithOrgModels(models) cfg.Providers = cfg.ModelProviders() return cfg, nil } // ModelSession is what a task's agent needs to call its model providers: // environment and mounts for the Docker egress proxy, or the gateway // providers an OpenShell sandbox attaches. type ModelSession struct { Env []string Mounts []storage.Mount Providers []ModelProvider } // ModelProvider is a credentialed provider on an OpenShell gateway and the // host its key may be sent to. type ModelProvider struct { Gateway string Host string Port int } // openModelEgress opens task's egress session, or refuses: provider keys // never reach a container, so without the proxy no model call can run. A // workflow with no model step needs no provider credential, only the session // that lets its tools reach hosts. func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string, hosts []string) (ModelSession, func(), error) { var providers map[string]config.Provider if workflow.UsesModel(task.WorkflowDefinitionYAML) { cfg, err := d.taskConfig(ctx, task) if err != nil { return ModelSession{}, nil, err } providers = cfg.Providers } if d.ModelEgress == nil { for _, p := range providers { if p.APIKeyEnv != "" { return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, fmt.Errorf("model egress proxy is unavailable on this daemon, and provider keys never enter a container")} } } return ModelSession{}, func() {}, nil } if len(providers) == 0 && len(hosts) == 0 { return ModelSession{}, func() {}, nil } if credential == "" { return ModelSession{}, nil, fmt.Errorf("model egress needs a run credential, and no State Store issues one") } session, err := d.ModelEgress.Open(ctx, credential, task.OrgOrDefault(), providers, hosts) if err != nil { return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, err} } return session, func() { d.ModelEgress.Close(ctx, credential) }, nil }