82 lines
3.1 KiB
Go
82 lines
3.1 KiB
Go
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 native task's model proxy session. The container holds
|
|
// a sentinel under each provider's key variable and reaches the providers
|
|
// through the egress proxy, which swaps in the org's bound key.
|
|
type ModelEgress interface {
|
|
Open(token, org string, providers map[string]config.Provider) (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 native container needs for its model proxy session.
|
|
type ModelSession struct {
|
|
Env []string
|
|
Mounts []storage.Mount
|
|
}
|
|
|
|
// openModelEgress opens task's model 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 session and no provider credential.
|
|
func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string) (ModelSession, func(), error) {
|
|
if !workflow.UsesModel(task.WorkflowDefinitionYAML) {
|
|
return ModelSession{}, func() {}, nil
|
|
}
|
|
cfg, err := d.taskConfig(ctx, task)
|
|
if err != nil {
|
|
return ModelSession{}, nil, err
|
|
}
|
|
if d.ModelEgress == nil {
|
|
for _, p := range cfg.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 credential == "" {
|
|
return ModelSession{}, nil, fmt.Errorf("model egress needs a run credential, and no State Store issues one")
|
|
}
|
|
session, err := d.ModelEgress.Open(credential, task.OrgOrDefault(), cfg.Providers)
|
|
if err != nil {
|
|
return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, err}
|
|
}
|
|
return session, func() { d.ModelEgress.Close(ctx, credential) }, nil
|
|
}
|