|
| 1 | +package build |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/ForestHubAI/edge-agents/go/api/workflow" |
| 7 | + "github.com/ForestHubAI/edge-agents/go/engine" |
| 8 | + "github.com/ForestHubAI/edge-agents/go/llmproxy" |
| 9 | + "github.com/ForestHubAI/edge-agents/go/llmproxy/provider/selfhosted" |
| 10 | +) |
| 11 | + |
| 12 | +// buildDeployProviders resolves a workflow's declared models into a single |
| 13 | +// per-deploy self-hosted provider. Every entry in wf.Models is a custom/ |
| 14 | +// self-hosted model while catalog models are referenced by id and never declared. |
| 15 | +// An unbound or unconfigured model is a deploy error. |
| 16 | +func buildDeployProviders(wf *workflow.Workflow, dm engine.DeploymentMapping, ext *engine.ExternalResources) ([]llmproxy.Provider, error) { |
| 17 | + endpoints := make([]selfhosted.ModelEndpoint, 0, len(wf.Models)) |
| 18 | + for _, mu := range wf.Models { |
| 19 | + m, err := mu.AsLLMModel() |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("declared model: %w", err) |
| 22 | + } |
| 23 | + b, ok := dm[m.Id] |
| 24 | + if !ok || b.Ref == "" { |
| 25 | + return nil, fmt.Errorf("model %q: declared but not bound by the deployment mapping", m.Id) |
| 26 | + } |
| 27 | + var cfg engine.LLMProviderConfig |
| 28 | + if ext != nil { |
| 29 | + cfg, ok = ext.Providers[b.Ref] |
| 30 | + } |
| 31 | + if !ok { |
| 32 | + return nil, fmt.Errorf("model %q: bound to %q but no provider config in deploy externalResources", m.Id, b.Ref) |
| 33 | + } |
| 34 | + if cfg.Model != "" && cfg.Model != m.Id { |
| 35 | + return nil, fmt.Errorf("model %q: upstream model-name aliasing (%q) is not supported yet", m.Id, cfg.Model) |
| 36 | + } |
| 37 | + caps, err := toCapabilities(m) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + endpoints = append(endpoints, selfhosted.ModelEndpoint{ |
| 42 | + URL: cfg.URL, |
| 43 | + APIKey: cfg.APIKey, |
| 44 | + ID: llmproxy.ModelID(m.Id), |
| 45 | + Label: m.Label, |
| 46 | + Capabilities: caps, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + // No declared custom models → no deploy provider. Returning a slice here |
| 51 | + // would wrap NewProvider's nil (it returns nil for an empty config) and |
| 52 | + // panic NewClient on ProviderID(). |
| 53 | + if len(endpoints) == 0 { |
| 54 | + return nil, nil |
| 55 | + } |
| 56 | + return []llmproxy.Provider{selfhosted.NewProvider(selfhosted.Config{Endpoints: endpoints})}, nil |
| 57 | +} |
| 58 | + |
| 59 | +// requiredModelIDs collects the chat-model ids referenced by Agent nodes across |
| 60 | +// the main graph and every function body — the models the deploy must be able |
| 61 | +// to serve. Only Agent nodes reference chat models today; nodes with a missing |
| 62 | +// model id are skipped here (the graph build reports that as a MissingField). |
| 63 | +func requiredModelIDs(wf *workflow.Workflow) ([]string, error) { |
| 64 | + seen := make(map[string]struct{}) |
| 65 | + var out []string |
| 66 | + scan := func(nodes []workflow.Node) error { |
| 67 | + for _, n := range nodes { |
| 68 | + v, err := n.ValueByDiscriminator() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + a, ok := v.(workflow.AgentNode) |
| 73 | + if !ok || a.Arguments.Model == nil || *a.Arguments.Model == "" { |
| 74 | + continue |
| 75 | + } |
| 76 | + id := *a.Arguments.Model |
| 77 | + if _, dup := seen[id]; dup { |
| 78 | + continue |
| 79 | + } |
| 80 | + seen[id] = struct{}{} |
| 81 | + out = append(out, id) |
| 82 | + } |
| 83 | + return nil |
| 84 | + } |
| 85 | + if err := scan(wf.Nodes); err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + for _, f := range wf.Functions { |
| 89 | + if err := scan(f.Nodes); err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + } |
| 93 | + return out, nil |
| 94 | +} |
| 95 | + |
| 96 | +// validateModelsResolvable fails the build when an Agent node references a model |
| 97 | +// that no provider in the composed client can serve. Without this the failure |
| 98 | +// surfaces lazily at the first Chat ("no suitable provider"); here it's a clear |
| 99 | +// deploy-time error naming the model. |
| 100 | +func validateModelsResolvable(wf *workflow.Workflow, client *llmproxy.Client) error { |
| 101 | + ids, err := requiredModelIDs(wf) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("scanning referenced models: %w", err) |
| 104 | + } |
| 105 | + available := make(map[string]struct{}) |
| 106 | + for _, m := range client.AvailableModels() { |
| 107 | + available[string(m.ID)] = struct{}{} |
| 108 | + } |
| 109 | + for _, id := range ids { |
| 110 | + if _, ok := available[id]; !ok { |
| 111 | + return fmt.Errorf("model %q is referenced by an agent node but no configured provider serves it (no local API key, backend route, or declared custom model)", id) |
| 112 | + } |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// toCapabilities maps a declared model's capabilities onto llmproxy's. Embedding |
| 118 | +// is rejected for now: self-hosted deploy providers need a vector dimension that |
| 119 | +// the workflow model declaration doesn't carry yet. |
| 120 | +func toCapabilities(m workflow.LLMModel) ([]llmproxy.ModelCapability, error) { |
| 121 | + caps := make([]llmproxy.ModelCapability, 0, len(m.Capabilities)) |
| 122 | + for _, c := range m.Capabilities { |
| 123 | + if c == workflow.ModelCapabilityEmbedding { |
| 124 | + return nil, fmt.Errorf("model %q: the embedding capability is not supported for self-hosted deploy providers yet (no dimension in the workflow declaration)", m.Id) |
| 125 | + } |
| 126 | + caps = append(caps, llmproxy.ModelCapability(c)) |
| 127 | + } |
| 128 | + return caps, nil |
| 129 | +} |
0 commit comments