loco_agents.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package reconciler
  14. import (
  15. "context"
  16. "crypto/sha256"
  17. "encoding/hex"
  18. "fmt"
  19. "maps"
  20. "github.com/LocoStack/loco-operator/api/v1alpha1"
  21. "go.yaml.in/yaml/v2"
  22. corev1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/runtime"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  27. )
  28. type LocoAgentsReconciler struct {
  29. *DefaultComponentReconciler
  30. *AgentReconciler
  31. client client.Client
  32. scheme *runtime.Scheme
  33. stack *v1alpha1.Stack
  34. component *v1alpha1.Component
  35. }
  36. func NewLocoAgentsReconciler(client client.Client, scheme *runtime.Scheme, stack *v1alpha1.Stack, component *v1alpha1.Component) *LocoAgentsReconciler {
  37. return &LocoAgentsReconciler{
  38. DefaultComponentReconciler: NewDefaultComponentReconciler(client, scheme, stack, component),
  39. AgentReconciler: NewAgentReconciler(client, scheme, stack, component),
  40. client: client,
  41. scheme: scheme,
  42. stack: stack,
  43. component: component,
  44. }
  45. }
  46. func (r *LocoAgentsReconciler) ReconcileComponent(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) ([]client.Object, error) {
  47. deps, comps, err := r.getDependencies(ctx)
  48. if err != nil {
  49. return nil, fmt.Errorf("Failed to get dependencies for %s: %w", r.component.Name, err)
  50. }
  51. agentConfigHash, err := r.reconcileAgentConfig(ctx, r.component.Spec.Variables["systemPrompt"], comps)
  52. if err != nil {
  53. return nil, fmt.Errorf("Failed to reconcile config for %s: %w", r.component.Name, err)
  54. }
  55. mcpConfigHash, err := r.reconcileMcpConfig(ctx, comps)
  56. if err != nil {
  57. return nil, fmt.Errorf("Failed to reconcile MCP config for %s: %w", r.component.Name, err)
  58. }
  59. h := sha256.Sum256([]byte(agentConfigHash + mcpConfigHash))
  60. configHash := hex.EncodeToString(h[:])
  61. if tmpl.Metadata.Annotations == nil {
  62. tmpl.Metadata.Annotations = make(map[string]string)
  63. }
  64. maps.Copy(tmpl.Metadata.Annotations, map[string]string{
  65. "locostack.com/configHash": configHash,
  66. })
  67. if _, err := r.DefaultComponentReconciler.ReconcileComponent(ctx, tmpl, variables); err != nil {
  68. return nil, err
  69. }
  70. return deps, nil
  71. }
  72. type modelConfig struct {
  73. Endpoint string `yaml:"endpoint"`
  74. APIKey string `yaml:"apiKey,omitempty"`
  75. Name string `yaml:"name,omitempty"`
  76. }
  77. type endpoint struct {
  78. Name string `json:"name"`
  79. Endpoint string `json:"endpoint"`
  80. }
  81. type agentConfig struct {
  82. SystemPrompt string `yaml:"systemPrompt"`
  83. Model modelConfig `yaml:"model"`
  84. KnowledgeBases []endpoint `yaml:"knowledgeBases,omitempty"`
  85. MCPConfigPath string `yaml:"mcpConfigPath,omitempty"`
  86. }
  87. func (r *LocoAgentsReconciler) reconcileAgentConfig(ctx context.Context, systemPrompt string, comps *AgentComponents) (string, error) {
  88. config := agentConfig{
  89. SystemPrompt: systemPrompt,
  90. Model: modelConfig{
  91. Endpoint: comps.Model.URL,
  92. APIKey: comps.GatewayKey,
  93. Name: comps.Model.Name,
  94. },
  95. MCPConfigPath: "/etc/mcp/mcp.json",
  96. }
  97. for _, kb := range comps.KnowledgeBases {
  98. config.KnowledgeBases = append(config.KnowledgeBases, endpoint{
  99. Name: kb.Name,
  100. Endpoint: kb.URL,
  101. })
  102. }
  103. yamlBytes, err := yaml.Marshal(config)
  104. h := sha256.Sum256(yamlBytes)
  105. configHash := hex.EncodeToString(h[:])
  106. if err != nil {
  107. return "", fmt.Errorf("marshaling agent config: %w", err)
  108. }
  109. secret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: r.ResourceName("config"), Namespace: r.component.GetNamespace()}}
  110. _, err = controllerutil.CreateOrUpdate(ctx, r.client, secret, func() error {
  111. if secret.Labels == nil {
  112. secret.Labels = map[string]string{}
  113. }
  114. maps.Copy(secret.Labels, r.ResourceLabels())
  115. if secret.Annotations == nil {
  116. secret.Annotations = make(map[string]string)
  117. }
  118. secret.Annotations["hash"] = configHash
  119. secret.Data = map[string][]byte{
  120. "agent.yaml": yamlBytes,
  121. }
  122. return controllerutil.SetControllerReference(r.component, secret, r.scheme)
  123. })
  124. return configHash, err
  125. }