diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ef0c48efbf..f1acae18a5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -176,6 +176,9 @@ jobs: replicas: 0 kagent-tools: enabled: false + # The example Agent needs kagent-tools, and the e2e suites do not use it. + examples: + enabled: false substrateWorkerPool: create: true replicas: 4 diff --git a/Makefile b/Makefile index 1c25cd5edd..2610843d3f 100644 --- a/Makefile +++ b/Makefile @@ -456,6 +456,11 @@ helm-version: helm-cleanup helm package -d "$(HELM_DIST_FOLDER)" "$(HELM_SOURCE_DIR)/kagent-crds" helm package -d "$(HELM_DIST_FOLDER)" "$(HELM_SOURCE_DIR)/kagent" +.PHONY: builtin-skills-catalog +builtin-skills-catalog: ## Regenerate the built-in skills catalog from the kagent-tools image + bash scripts/builtin-skills-catalog.sh > "$(HELM_SOURCE_DIR)/kagent/files/builtin-skills.json.tmp" + mv "$(HELM_SOURCE_DIR)/kagent/files/builtin-skills.json.tmp" "$(HELM_SOURCE_DIR)/kagent/files/builtin-skills.json" + .PHONY: helm-dev helm-dev: ## Prepare development charts with the images published by local builds @test -d "$(RUNTIME_BUILD_METADATA_DIR)" || { echo "Missing runtime image metadata. Run make build before preparing development charts." >&2; exit 1; } diff --git a/go/core/cli/install_dry_run_test.go b/go/core/cli/install_dry_run_test.go index 6e527e3ad5..07a9bf0123 100644 --- a/go/core/cli/install_dry_run_test.go +++ b/go/core/cli/install_dry_run_test.go @@ -78,7 +78,7 @@ func TestInstallDryRunWithoutClusterOrAPIKey(t *testing.T) { require.Contains(t, string(values), "${OPENAI_API_KEY}") require.Contains(t, string(values), "userProvidedValue: retained") require.Contains(t, string(values), "replicas: 2") - require.Contains(t, string(values), "k8s-agent:\n enabled: false") + require.Contains(t, string(values), "examples:\n enabled: false") require.NotContains(t, string(values), "secret-that-must-not-be-exported") atelet, err := os.ReadFile(filepath.Join(destination, "values", "substrate-atelet.yaml")) require.NoError(t, err) diff --git a/go/core/cli/internal/profiles/minimal.yaml b/go/core/cli/internal/profiles/minimal.yaml index 8086d039b9..6e78ba0764 100644 --- a/go/core/cli/internal/profiles/minimal.yaml +++ b/go/core/cli/internal/profiles/minimal.yaml @@ -1,22 +1,4 @@ # The minimal profile does not install any agents, and is meant as a bare minimum installation for kagent. # This is useful for users who only want to set up kagent without any extra agents. -k8s-agent: - enabled: false -kgateway-agent: - enabled: false -istio-agent: - enabled: false -promql-agent: - enabled: false -observability-agent: - enabled: false -argo-rollouts-agent: - enabled: false -helm-agent: - enabled: false -cilium-policy-agent: - enabled: false -cilium-manager-agent: - enabled: false -cilium-debug-agent: +examples: enabled: false diff --git a/go/core/pkg/install/plan.go b/go/core/pkg/install/plan.go index 9868823297..932817d616 100644 --- a/go/core/pkg/install/plan.go +++ b/go/core/pkg/install/plan.go @@ -120,6 +120,9 @@ config: # Ready-to-reference Harnesses on the WorkerPool created below. harnesses: enabled: true +# The example Agent and skills, which run on those Harnesses. +examples: + enabled: true ` + fmt.Sprintf("\nsubstrateWorkerPool:\n create: true\n workerImage: %s\n", strconv.Quote(cfg.WorkerImage)) if !cfg.SkipDatabaseSetup { kagent.Values = append(kagent.Values, "database.postgres.clientCertificate.enabled=true") diff --git a/go/core/pkg/install/plan_test.go b/go/core/pkg/install/plan_test.go index 35724b95ac..3c7fbdc2a0 100644 --- a/go/core/pkg/install/plan_test.go +++ b/go/core/pkg/install/plan_test.go @@ -428,26 +428,29 @@ func TestPlanEnablesGRPCReflection(t *testing.T) { func TestPlanEnablesDefaultHarnesses(t *testing.T) { scenarios := []struct { - name string - operator string - wantHarness string + name string + operator string + wantHarness string + wantExamples string }{ { - name: "Given chart defaults that disable default Harnesses, when planning an install, assert that the WorkerPool and default Harnesses are enabled", - wantHarness: "true", + name: "Given chart defaults that disable default Harnesses and examples, when planning an install, assert that the WorkerPool, default Harnesses and examples are enabled", + wantHarness: "true", + wantExamples: "true", }, { - name: "Given an operator values file that disables default Harnesses, when planning an install, assert that the operator choice wins", - operator: "harnesses: {enabled: false}\n", - wantHarness: "false", + name: "Given an operator values file that disables default Harnesses and examples, when planning an install, assert that the operator choice wins", + operator: "harnesses: {enabled: false}\nexamples: {enabled: false}\n", + wantHarness: "false", + wantExamples: "false", }, } for _, scenario := range scenarios { t.Run(scenario.name, func(t *testing.T) { cfg := localInstallConfig(t) chart := filepath.Join(cfg.Kagent.Repository, cfg.Kagent.Name) - require.NoError(t, os.WriteFile(filepath.Join(chart, "values.yaml"), []byte("harnesses: {enabled: false}\nsubstrateWorkerPool: {create: false}\n"), 0o600)) - require.NoError(t, os.WriteFile(filepath.Join(chart, "templates", "harnesses.yaml"), []byte("apiVersion: v1\nkind: ConfigMap\nmetadata: {name: harnesses}\ndata:\n harnesses: {{ .Values.harnesses.enabled | quote }}\n pool: {{ .Values.substrateWorkerPool.create | quote }}\n"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(chart, "values.yaml"), []byte("harnesses: {enabled: false}\nexamples: {enabled: false}\nsubstrateWorkerPool: {create: false}\n"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(chart, "templates", "harnesses.yaml"), []byte("apiVersion: v1\nkind: ConfigMap\nmetadata: {name: harnesses}\ndata:\n harnesses: {{ .Values.harnesses.enabled | quote }}\n pool: {{ .Values.substrateWorkerPool.create | quote }}\n examples: {{ .Values.examples.enabled | quote }}\n"), 0o600)) if scenario.operator != "" { operator := filepath.Join(t.TempDir(), "operator.yaml") require.NoError(t, os.WriteFile(operator, []byte(scenario.operator), 0o600)) @@ -467,6 +470,7 @@ func TestPlanEnablesDefaultHarnesses(t *testing.T) { var rendered struct{ Data map[string]string } require.NoError(t, yaml.Unmarshal([]byte(out), &rendered)) require.Equal(t, scenario.wantHarness, rendered.Data["harnesses"]) + require.Equal(t, scenario.wantExamples, rendered.Data["examples"]) require.Equal(t, "true", rendered.Data["pool"]) }) } diff --git a/helm/kagent/Chart-template.yaml b/helm/kagent/Chart-template.yaml index a887f8c28b..d94fdee3e6 100644 --- a/helm/kagent/Chart-template.yaml +++ b/helm/kagent/Chart-template.yaml @@ -7,7 +7,7 @@ annotations: "org.opencontainers.image.source": "https://github.com/kagent-dev/kagent" dependencies: - name: kagent-tools - version: 0.3.0 + version: 0.3.1 repository: oci://ghcr.io/kagent-dev/tools/helm condition: kagent-tools.enabled - name: oauth2-proxy diff --git a/helm/kagent/files/builtin-skills.json b/helm/kagent/files/builtin-skills.json new file mode 100644 index 0000000000..e77f1659dd --- /dev/null +++ b/helm/kagent/files/builtin-skills.json @@ -0,0 +1,36 @@ +{ + "version": "0.3.1", + "image": "ghcr.io/kagent-dev/kagent/tools@sha256:607cb0ac95c9905529ad1cfebeff3aa2fbf68aa81861ed3bbba9d71a7477d640", + "skills": [ + { + "name": "argo-rollouts-progressive-delivery", + "description": "Operate Argo Rollouts canary and blue-green releases (status, promote, pause, change image) with the kagent-tools argo_* MCP tools. Use when a Rollout is progressing, paused, degraded, or needs a new image.", + "path": "opt/kagent-tools-skills/skills/argo-rollouts-progressive-delivery" + }, + { + "name": "helm-release-management", + "description": "Inspect, upgrade, roll forward and uninstall Helm releases with the kagent-tools helm_* MCP tools. Use for questions about installed charts, release status, values, failed upgrades, or adding chart repositories.", + "path": "opt/kagent-tools-skills/skills/helm-release-management" + }, + { + "name": "istio-traffic-debugging", + "description": "Debug Istio service-mesh traffic (503s, mTLS failures, routing not applied, sidecar or ambient/waypoint issues) using the kagent-tools istio_* MCP tools. Use when requests between mesh services fail or routing rules do not take effect.", + "path": "opt/kagent-tools-skills/skills/istio-traffic-debugging" + }, + { + "name": "k8s-troubleshooting", + "description": "Diagnose unhealthy Kubernetes workloads (CrashLoopBackOff, Pending, ImagePullBackOff, OOMKilled, failing readiness, unreachable Services) using the kagent-tools k8s_* MCP tools. Use when a pod, deployment or service is not working as expected.", + "path": "opt/kagent-tools-skills/skills/k8s-troubleshooting" + }, + { + "name": "kubescape-security-posture", + "description": "Review cluster security posture with Kubescape results (configuration scans, image vulnerabilities, application profiles, network neighborhoods) using the kagent-tools kubescape_* MCP tools. Use for security reviews, CVE questions, or hardening recommendations.", + "path": "opt/kagent-tools-skills/skills/kubescape-security-posture" + }, + { + "name": "prometheus-observability", + "description": "Answer questions about cluster and application metrics (error rates, latency, saturation, scrape health) with the kagent-tools prometheus_* MCP tools. Use when investigating performance, alerts, or whether a target is scraped. Requires PROMETHEUS_URL to be configured.", + "path": "opt/kagent-tools-skills/skills/prometheus-observability" + } + ] +} diff --git a/helm/kagent/templates/_helpers.tpl b/helm/kagent/templates/_helpers.tpl index 8774839610..4e6f627aae 100644 --- a/helm/kagent/templates/_helpers.tpl +++ b/helm/kagent/templates/_helpers.tpl @@ -13,6 +13,33 @@ {{- $images | mustToJson -}} {{- end -}} +{{/* +The built-in skills catalog from the kagent-tools image, as JSON. Empty unless +examples.enabled and kagent-tools.enabled are both true. +*/}} +{{- define "kagent.builtinSkillsCatalog" -}} +{{- if and (dig "examples" "enabled" false .Values.AsMap) (dig "kagent-tools" "enabled" false .Values.AsMap) -}} + {{- $catalog := .Files.Get "files/builtin-skills.json" | mustFromJson -}} + {{- range .Chart.Dependencies -}} + {{- if and (eq .Name "kagent-tools") (ne .Version $catalog.version) -}} + {{- fail (printf "files/builtin-skills.json is for kagent-tools %s, but the chart depends on %s: run make builtin-skills-catalog" $catalog.version .Version) -}} + {{- end -}} + {{- end -}} + {{- $_ := unset $catalog "version" -}} + {{/* The Prometheus skill needs a tool server URL it accepts (http:// or https://). */}} + {{- $prometheusURL := dig "kagent-tools" "tools" "prometheus" "url" "" .Values.AsMap -}} + {{- if not (regexMatch "^https?://" $prometheusURL) -}} + {{- $skills := list -}} + {{- range $catalog.skills -}} + {{- if ne .name "prometheus-observability" -}}{{- $skills = append $skills . -}}{{- end -}} + {{- end -}} + {{- $_ := set $catalog "skills" $skills -}} + {{- end -}} + {{- $_ := set $catalog "image" (include "kagent.mirroredImage" (dict "root" $ "image" $catalog.image)) -}} + {{- $catalog | mustToJson -}} +{{- end -}} +{{- end -}} + {{/* Create a default fully qualified app name. */}} diff --git a/helm/kagent/templates/example-agents.yaml b/helm/kagent/templates/example-agents.yaml new file mode 100644 index 0000000000..2050623f22 --- /dev/null +++ b/helm/kagent/templates/example-agents.yaml @@ -0,0 +1,210 @@ +{{- $catalog := include "kagent.builtinSkillsCatalog" . | default "{}" | mustFromJson }} +{{- $byName := dict }} +{{- range $catalog.skills | default (list) }} +{{- $_ := set $byName .name . }} +{{- end }} +{{- $toolServer := printf "%s-tool-server" (include "kagent.fullname" .) }} +{{- if and (dig "examples" "enabled" false .Values.AsMap) (dig "examples" "platformAgent" "enabled" false .Values.AsMap) }} +{{- if not (and (dig "harnesses" "enabled" false .Values.AsMap) (dig "harnesses" "kagent" "enabled" false .Values.AsMap)) }} +{{- fail "examples.platformAgent needs the kagent-default Harness: set harnesses.enabled and harnesses.kagent.enabled, or set examples.platformAgent.enabled=false" }} +{{- end }} +{{- if not (index .Values "kagent-tools" "enabled") }} +{{- fail "examples.platformAgent needs kagent-tools: set kagent-tools.enabled, or set examples.platformAgent.enabled=false" }} +{{- end }} +{{- $kubernetesDescription := "Kubernetes specialist: diagnoses and fixes unhealthy pods, deployments and services." }} +{{- $skill := get $byName "k8s-troubleshooting" }} +apiVersion: api.kagent.dev/v1alpha3 +kind: AgentTemplate +metadata: + name: platform-kubernetes + namespace: {{ include "kagent.namespace" . }} + labels: + {{- include "kagent.labels" . | nindent 4 }} +spec: + description: {{ $kubernetesDescription | quote }} + modelConfig: + name: {{ include "kagent.defaultModelConfigName" . }} + promptTemplate: + dataSources: + - name: kagent-builtin-prompts + alias: builtin + systemPrompt: | + You are a Kubernetes specialist. You help users run and troubleshoot + workloads in the cluster where kagent runs. When a question matches one of + your skills, load that skill and follow it. + + {{`{{include "builtin/skills-usage"}}`}} + + {{`{{include "builtin/kubernetes-context"}}`}} + + {{`{{include "builtin/tool-usage-best-practices"}}`}} + + {{`{{include "builtin/safety-guardrails"}}`}} + tools: + - mcp: + server: + kind: RemoteMCPServer + name: {{ $toolServer }} + tools: + - k8s_describe_resource + - k8s_generate_resource + - k8s_get_available_api_resources + - k8s_get_cluster_configuration + - k8s_get_events + - k8s_get_pod_logs + - k8s_get_resource_yaml + - k8s_get_resources + - k8s_wait + # These tools change the cluster, so each call waits for the user's approval. + - mcp: + server: + kind: RemoteMCPServer + name: {{ $toolServer }} + requireApproval: true + tools: + - k8s_annotate_resource + - k8s_apply_manifest + - k8s_check_service_connectivity + - k8s_create_resource + - k8s_create_resource_from_url + - k8s_delete_resource + - k8s_execute_command + - k8s_label_resource + - k8s_patch_resource + - k8s_patch_status + - k8s_remove_annotation + - k8s_remove_label + - k8s_rollout + - k8s_scale + skills: + - name: {{ $skill.name | quote }} + source: + oci: {{ $catalog.image | quote }} + path: {{ $skill.path | quote }} +{{- /* One specialist per skill. Read-only tools run freely; tools that change things need approval. */}} +{{- $specialists := list + (dict "name" "helm" "skill" "helm-release-management" "topic" "Helm" + "description" "Helm specialist: lists, inspects, upgrades and uninstalls Helm releases and manages chart repositories." + "prompt" "You are a Helm specialist. You help users inspect, troubleshoot, upgrade and uninstall Helm releases." + "read" (list "helm_get_release" "helm_list_releases") + "write" (list "helm_repo_add" "helm_repo_update" "helm_uninstall" "helm_upgrade")) + (dict "name" "istio" "skill" "istio-traffic-debugging" "topic" "Istio" + "description" "Istio specialist: debugs service mesh traffic, mTLS, routing, sidecars, ztunnel and waypoints." + "prompt" "You are an Istio specialist. You diagnose and fix service mesh traffic, security and waypoint problems." + "read" (list "istio_analyze_cluster_configuration" "istio_generate_waypoint" "istio_list_waypoints" "istio_proxy_config" "istio_proxy_status" "istio_version" "istio_waypoint_status" "istio_ztunnel_config") + "write" (list "istio_apply_waypoint" "istio_delete_waypoint")) + (dict "name" "argo-rollouts" "skill" "argo-rollouts-progressive-delivery" "topic" "Argo Rollouts" + "description" "Argo Rollouts specialist: checks, promotes, pauses and updates canary and blue-green Rollouts." + "prompt" "You are an Argo Rollouts specialist. You help users run canary and blue-green releases with Argo Rollouts." + "read" (list "argo_check_plugin_logs" "argo_rollouts_list" "argo_verify_argo_rollouts_controller_install" "argo_verify_kubectl_plugin_install") + "write" (list "argo_pause_rollout" "argo_promote_rollout" "argo_set_rollout_image" "argo_verify_gateway_plugin")) + (dict "name" "kubescape" "skill" "kubescape-security-posture" "topic" "security" + "description" "Security specialist: reviews Kubescape configuration scans, image vulnerabilities and workload behavior." + "prompt" "You are a Kubernetes security specialist. You review Kubescape results and recommend hardening steps." + "read" (list "kubescape_check_health" "kubescape_get_application_profile" "kubescape_get_configuration_scan" "kubescape_get_network_neighborhood" "kubescape_get_vulnerability_details" "kubescape_list_application_profiles" "kubescape_list_configuration_scans" "kubescape_list_network_neighborhoods" "kubescape_list_vulnerabilities" "kubescape_list_vulnerability_manifests") + "write" (list)) + (dict "name" "prometheus" "skill" "prometheus-observability" "topic" "metrics" + "description" "Observability specialist: writes and runs PromQL queries about cluster and application metrics." + "prompt" "You are an observability specialist. You turn questions about metrics into PromQL, run them and explain the results." + "read" (list "prometheus_label_names_tool" "prometheus_promql_tool" "prometheus_query_range_tool" "prometheus_query_tool" "prometheus_targets_tool") + "write" (list)) +}} +{{- /* The catalog has the Prometheus skill only when kagent-tools has a Prometheus URL. */}} +{{- $active := list }} +{{- $topics := list "Kubernetes" }} +{{- range $specialists }}{{ if hasKey $byName .skill }}{{ $active = append $active . }}{{ $topics = append $topics .topic }}{{ end }}{{ end }} +{{- $covers := $topics | first }} +{{- if gt (len $topics) 1 }}{{ $covers = printf "%s and %s" (initial $topics | join ", ") (last $topics) }}{{ end }} +{{- range $active }} +{{- $skill := get $byName .skill }} +--- +apiVersion: api.kagent.dev/v1alpha3 +kind: AgentTemplate +metadata: + name: {{ printf "platform-%s" .name | quote }} + namespace: {{ include "kagent.namespace" $ }} + labels: + {{- include "kagent.labels" $ | nindent 4 }} +spec: + description: {{ .description | quote }} + modelConfig: + name: {{ include "kagent.defaultModelConfigName" $ }} + promptTemplate: + dataSources: + - name: kagent-builtin-prompts + alias: builtin + systemPrompt: | + {{ .prompt }} Load your skill and follow it. + + {{`{{include "builtin/skills-usage"}}`}} + + {{`{{include "builtin/tool-usage-best-practices"}}`}} + + {{`{{include "builtin/safety-guardrails"}}`}} + tools: + - mcp: + server: + kind: RemoteMCPServer + name: {{ $toolServer }} + tools: + {{- toYaml .read | nindent 10 }} + {{- with .write }} + - mcp: + server: + kind: RemoteMCPServer + name: {{ $toolServer }} + requireApproval: true + tools: + {{- toYaml . | nindent 10 }} + {{- end }} + skills: + - name: {{ $skill.name | quote }} + source: + oci: {{ $catalog.image | quote }} + path: {{ $skill.path | quote }} +{{- end }} +--- +apiVersion: api.kagent.dev/v1alpha3 +kind: AgentTemplate +metadata: + name: platform-agent + namespace: {{ include "kagent.namespace" . }} + labels: + {{- include "kagent.labels" . | nindent 4 }} +spec: + description: {{ printf "Platform assistant: sends %s questions to a specialist." $covers | quote }} + modelConfig: + name: {{ include "kagent.defaultModelConfigName" . }} + systemPrompt: | + You help users run and troubleshoot the Kubernetes cluster where kagent + runs. You do not call cluster tools yourself. Send each task to the + specialist that owns it, and pass along what the user asked. If a task + spans several areas, call each specialist in turn and combine the answers. + tools: + - subAgent: + name: kubernetes + description: {{ $kubernetesDescription | quote }} + templateRef: + name: platform-kubernetes + {{- range $active }} + - subAgent: + name: {{ .name | quote }} + description: {{ .description | quote }} + templateRef: + name: {{ printf "platform-%s" .name | quote }} + {{- end }} +--- +apiVersion: api.kagent.dev/v1alpha3 +kind: Agent +metadata: + name: platform-agent + namespace: {{ include "kagent.namespace" . }} + labels: + {{- include "kagent.labels" . | nindent 4 }} +spec: + harnessRef: + name: kagent-default + template: + ref: + name: platform-agent +{{- end }} diff --git a/helm/kagent/templates/ui-configmap.yaml b/helm/kagent/templates/ui-configmap.yaml index 52b61880bd..acd7cb725a 100644 --- a/helm/kagent/templates/ui-configmap.yaml +++ b/helm/kagent/templates/ui-configmap.yaml @@ -19,3 +19,6 @@ data: {{- if .Values.ui.streamTimeoutSeconds }} KAGENT_UI_STREAM_TIMEOUT_MS: {{ mul (int .Values.ui.streamTimeoutSeconds) 1000 | quote }} {{- end }} + {{- with include "kagent.builtinSkillsCatalog" . }} + KAGENT_UI_BUILTIN_SKILLS: {{ . | quote }} + {{- end }} diff --git a/helm/kagent/tests/example-agents_test.yaml b/helm/kagent/tests/example-agents_test.yaml new file mode 100644 index 0000000000..c6ba15e591 --- /dev/null +++ b/helm/kagent/tests/example-agents_test.yaml @@ -0,0 +1,274 @@ +suite: test the example Agents and their AgentTemplates +templates: + - example-agents.yaml +release: + namespace: kagent +set: + examples.enabled: true + harnesses.enabled: true +tests: + - it: platform-agent is the only example Agent, and the Kubernetes specialist is an AgentTemplate + asserts: + - containsDocument: + apiVersion: api.kagent.dev/v1alpha3 + kind: Agent + name: platform-agent + namespace: kagent + any: true + - hasDocuments: + count: 1 + filterAware: true + documentSelector: + path: kind + value: Agent + matchMany: true + - equal: + path: spec.modelConfig.name + value: default-model-config + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-kubernetes")] + - equal: + path: spec.description + value: "Platform assistant: sends Kubernetes, Helm, Istio, Argo Rollouts and security questions to a specialist." + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-agent")] + + - it: the Kubernetes specialist binds only the kagent-tools Kubernetes tools, and cluster changes need approval + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-kubernetes")] + asserts: + - lengthEqual: + path: spec.tools + count: 2 + - equal: + path: spec.tools[0].mcp.server + value: + kind: RemoteMCPServer + name: RELEASE-NAME-tool-server + - notExists: + path: spec.tools[0].mcp.requireApproval + - equal: + path: spec.tools[0].mcp.tools + value: + - k8s_describe_resource + - k8s_generate_resource + - k8s_get_available_api_resources + - k8s_get_cluster_configuration + - k8s_get_events + - k8s_get_pod_logs + - k8s_get_resource_yaml + - k8s_get_resources + - k8s_wait + - equal: + path: spec.tools[1].mcp.requireApproval + value: true + - equal: + path: spec.tools[1].mcp.tools + value: + - k8s_annotate_resource + - k8s_apply_manifest + - k8s_check_service_connectivity + - k8s_create_resource + - k8s_create_resource_from_url + - k8s_delete_resource + - k8s_execute_command + - k8s_label_resource + - k8s_patch_resource + - k8s_patch_status + - k8s_remove_annotation + - k8s_remove_label + - k8s_rollout + - k8s_scale + + - it: the Kubernetes specialist attaches k8s-troubleshooting from the packaged kagent-tools image + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-kubernetes")] + asserts: + - lengthEqual: + path: spec.skills + count: 1 + - equal: + path: spec.skills[0].name + value: k8s-troubleshooting + - matchRegex: + path: spec.skills[0].source.oci + pattern: ^ghcr\.io/kagent-dev/kagent/tools@sha256:[0-9a-f]{64}$ + - equal: + path: spec.skills[0].source.path + value: opt/kagent-tools-skills/skills/k8s-troubleshooting + + - it: the Kubernetes specialist mirrors the packaged image through global.imageRegistry + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-kubernetes")] + set: + global.imageRegistry: mirror.example + asserts: + - matchRegex: + path: spec.skills[0].source.oci + pattern: ^mirror\.example/kagent-dev/kagent/tools@sha256:[0-9a-f]{64}$ + + - it: platform-agent is skipped when all examples are turned off + set: + examples.enabled: false + asserts: + - hasDocuments: + count: 0 + + - it: platform-agent is skipped when the examples values are missing + set: + examples: null + asserts: + - hasDocuments: + count: 0 + + - it: platform-agent fails without the default Harnesses + set: + harnesses.enabled: false + asserts: + - failedTemplate: + errorMessage: "examples.platformAgent needs the kagent-default Harness: set harnesses.enabled and harnesses.kagent.enabled, or set examples.platformAgent.enabled=false" + + - it: the built-in skills need no Harness when platform-agent is off + set: + harnesses.enabled: false + examples.platformAgent.enabled: false + asserts: + - hasDocuments: + count: 0 + + - it: platform-agent fails when the kagent-default Harness is turned off + set: + harnesses.kagent.enabled: false + asserts: + - failedTemplate: + errorMessage: "examples.platformAgent needs the kagent-default Harness: set harnesses.enabled and harnesses.kagent.enabled, or set examples.platformAgent.enabled=false" + + - it: platform-agent fails without kagent-tools + set: + kagent-tools.enabled: false + asserts: + - failedTemplate: + errorMessage: "examples.platformAgent needs kagent-tools: set kagent-tools.enabled, or set examples.platformAgent.enabled=false" + + - it: platform-agent routes to one specialist per skill + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-agent")] + asserts: + - notExists: + path: spec.skills + - equal: + path: spec.tools + value: + - subAgent: + name: kubernetes + description: "Kubernetes specialist: diagnoses and fixes unhealthy pods, deployments and services." + templateRef: + name: platform-kubernetes + - subAgent: + name: helm + description: "Helm specialist: lists, inspects, upgrades and uninstalls Helm releases and manages chart repositories." + templateRef: + name: platform-helm + - subAgent: + name: istio + description: "Istio specialist: debugs service mesh traffic, mTLS, routing, sidecars, ztunnel and waypoints." + templateRef: + name: platform-istio + - subAgent: + name: argo-rollouts + description: "Argo Rollouts specialist: checks, promotes, pauses and updates canary and blue-green Rollouts." + templateRef: + name: platform-argo-rollouts + - subAgent: + name: kubescape + description: "Security specialist: reviews Kubescape configuration scans, image vulnerabilities and workload behavior." + templateRef: + name: platform-kubescape + + - it: platform-agent runs on kagent-default and references its AgentTemplate + documentSelector: + path: $[?(@.kind == "Agent" && @.metadata.name == "platform-agent")] + asserts: + - equal: + path: spec.harnessRef.name + value: kagent-default + - equal: + path: spec.template.ref.name + value: platform-agent + + - it: a specialist gets its own skill, its read-only tools, and approval on tools that change things + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-helm")] + asserts: + - equal: + path: spec.skills[0].name + value: helm-release-management + - matchRegex: + path: spec.skills[0].source.oci + pattern: ^ghcr\.io/kagent-dev/kagent/tools@sha256:[0-9a-f]{64}$ + - equal: + path: spec.skills[0].source.path + value: opt/kagent-tools-skills/skills/helm-release-management + - equal: + path: spec.tools[0].mcp + value: + server: + kind: RemoteMCPServer + name: RELEASE-NAME-tool-server + tools: + - helm_get_release + - helm_list_releases + - equal: + path: spec.tools[1].mcp + value: + server: + kind: RemoteMCPServer + name: RELEASE-NAME-tool-server + requireApproval: true + tools: + - helm_repo_add + - helm_repo_update + - helm_uninstall + - helm_upgrade + + - it: a read-only specialist has no approval binding + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-kubescape")] + asserts: + - lengthEqual: + path: spec.tools + count: 1 + - notExists: + path: spec.tools[0].mcp.requireApproval + + - it: platform-agent adds the Prometheus specialist when a Prometheus URL is set + set: + kagent-tools.tools.prometheus.url: http://prometheus-server.monitoring:9090 + asserts: + - equal: + path: spec.skills[0].name + value: prometheus-observability + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-prometheus")] + - contains: + path: spec.tools + content: + subAgent: + name: prometheus + description: "Observability specialist: writes and runs PromQL queries about cluster and application metrics." + templateRef: + name: platform-prometheus + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-agent")] + - equal: + path: spec.description + value: "Platform assistant: sends Kubernetes, Helm, Istio, Argo Rollouts, security and metrics questions to a specialist." + documentSelector: + path: $[?(@.kind == "AgentTemplate" && @.metadata.name == "platform-agent")] + + - it: platform-agent and its specialists are skipped when turned off + set: + examples.platformAgent.enabled: false + asserts: + - hasDocuments: + count: 0 diff --git a/helm/kagent/tests/kagent-tools-prometheus_test.yaml b/helm/kagent/tests/kagent-tools-prometheus_test.yaml new file mode 100644 index 0000000000..cd975549ae --- /dev/null +++ b/helm/kagent/tests/kagent-tools-prometheus_test.yaml @@ -0,0 +1,21 @@ +suite: test kagent-tools Prometheus URL +templates: + - charts/kagent-tools/templates/deployment.yaml +tests: + - it: leaves PROMETHEUS_URL empty by default + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: PROMETHEUS_URL + value: "" + + - it: points the tool server at the configured Prometheus URL + set: + kagent-tools.tools.prometheus.url: http://prometheus-server.monitoring:9090 + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: PROMETHEUS_URL + value: http://prometheus-server.monitoring:9090 diff --git a/helm/kagent/tests/ui-configmap_test.yaml b/helm/kagent/tests/ui-configmap_test.yaml new file mode 100644 index 0000000000..993282f9f5 --- /dev/null +++ b/helm/kagent/tests/ui-configmap_test.yaml @@ -0,0 +1,65 @@ +suite: test ui configmap +templates: + - ui-configmap.yaml +set: + examples.enabled: true +tests: + - it: offers no built-in skills with the chart defaults + set: + examples.enabled: false + asserts: + - notExists: + path: data.KAGENT_UI_BUILTIN_SKILLS + + - it: offers no built-in skills when kagent-tools is off + set: + kagent-tools.enabled: false + asserts: + - notExists: + path: data.KAGENT_UI_BUILTIN_SKILLS + + - it: offers the skills of the packaged kagent-tools image + asserts: + - matchRegex: + path: data.KAGENT_UI_BUILTIN_SKILLS + pattern: ^\{"image":"ghcr\.io/kagent-dev/kagent/tools@sha256:[0-9a-f]{64}","skills":\[ + - matchRegex: + path: data.KAGENT_UI_BUILTIN_SKILLS + pattern: '"name":"k8s-troubleshooting","path":"opt/kagent-tools-skills/skills/k8s-troubleshooting"' + + - it: mirrors the packaged image through global.imageRegistry + set: + global.imageRegistry: mirror.example + asserts: + - matchRegex: + path: data.KAGENT_UI_BUILTIN_SKILLS + pattern: ^\{"image":"mirror\.example/kagent-dev/kagent/tools@sha256:[0-9a-f]{64}" + + - it: offers no built-in skills when the examples values are missing + set: + examples: null + asserts: + - notExists: + path: data.KAGENT_UI_BUILTIN_SKILLS + + - it: hides the Prometheus skill when no Prometheus URL is set + asserts: + - notMatchRegex: + path: data.KAGENT_UI_BUILTIN_SKILLS + pattern: prometheus-observability + + - it: offers the Prometheus skill when a Prometheus URL is set + set: + kagent-tools.tools.prometheus.url: http://prometheus-server.monitoring:9090 + asserts: + - matchRegex: + path: data.KAGENT_UI_BUILTIN_SKILLS + pattern: '"name":"prometheus-observability","path":"opt/kagent-tools-skills/skills/prometheus-observability"' + + - it: hides the Prometheus skill when the Prometheus URL has no scheme + set: + kagent-tools.tools.prometheus.url: prometheus.kagent.svc.cluster.local:9090 + asserts: + - notMatchRegex: + path: data.KAGENT_UI_BUILTIN_SKILLS + pattern: prometheus-observability diff --git a/helm/kagent/values.yaml b/helm/kagent/values.yaml index ca4b49de74..16f517122b 100644 --- a/helm/kagent/values.yaml +++ b/helm/kagent/values.yaml @@ -23,8 +23,9 @@ global: # air-gap mirror knob: one value redirects all images. Repository and tag stay # per-image. For per-image control, leave this unset and set the per-image # registry keys. Covers this chart's own images: controller, sandbox guest, ui, - # substrateWorkerPool.workerImage, and built-in Harness images - # (both packaged defaults and config.harnessImages overrides). The vendored subcharts + # substrateWorkerPool.workerImage, built-in Harness images + # (both packaged defaults and config.harnessImages overrides), and the built-in + # skills image. The vendored subcharts # kagent-tools adopts it when its pinned version bumps. imageRegistry: "" # -- Pull secrets for every pod. The chart merges this list (union) into each @@ -807,6 +808,25 @@ kagent-tools: loglevel: "debug" metrics: port: 8085 # Use a different port than the controller service port to avoid duplicate port definitions + prometheus: + # -- Prometheus URL for the tool server, with http:// or https://. Setting it + # also offers the Prometheus skill in the UI. + url: "" + +# ============================================================================== +# EXAMPLES +# ============================================================================== +# Ready-made Agents and skills for a new install. Off in the chart; `kagent install` +# turns them on. + +examples: + # -- Create the example Agent and offer the kagent-tools skills in the UI. Both need + # `kagent-tools.enabled`; the Agent also needs `harnesses.enabled` and `harnesses.kagent.enabled`. + enabled: false + platformAgent: + # -- Create platform-agent, which hands each task to a specialist subagent + # (Kubernetes, Helm, Istio, Argo Rollouts, Kubescape, Prometheus). + enabled: true # ============================================================================== # PROXY CONFIGURATION diff --git a/scripts/builtin-skills-catalog.sh b/scripts/builtin-skills-catalog.sh new file mode 100644 index 0000000000..ebf065e2c5 --- /dev/null +++ b/scripts/builtin-skills-catalog.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Prints the built-in skills catalog: the kagent-tools version the chart depends +# on, its image digest, and the name, description and path of every skill in it. +# Rerun with make builtin-skills-catalog after you bump kagent-tools in +# helm/kagent/Chart-template.yaml. +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +repository="ghcr.io/kagent-dev/kagent/tools" +skills_root="opt/kagent-tools-skills/skills" +# The kagent-tools skills the UI offers. Cilium is left out; the chart offers +# Prometheus only when kagent-tools.tools.prometheus.url is set. +skills=(argo-rollouts-progressive-delivery helm-release-management istio-traffic-debugging + k8s-troubleshooting kubescape-security-posture prometheus-observability) + +version="$(awk '/- name: kagent-tools$/ { found = 1; next } found && /version:/ { print $2; exit }' \ + "$root/helm/kagent/Chart-template.yaml")" +[[ -n "$version" ]] || { echo "kagent-tools version not found in helm/kagent/Chart-template.yaml" >&2; exit 1; } + +digest="$(docker buildx imagetools inspect "$repository:$version" --format '{{json .Manifest}}' | jq -r .digest)" +[[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]] || { echo "invalid digest for $repository:$version: $digest" >&2; exit 1; } +image="$repository@$digest" + +work="$(mktemp -d)" +container="" +cleanup() { + [[ -z "$container" ]] || docker rm "$container" > /dev/null + rm -rf "$work" +} +trap cleanup EXIT +container="$(docker create "$image" true)" +docker cp "$container:/$skills_root" - | tar -x -C "$work" + +for dir in "${skills[@]}"; do + file="$work/skills/$dir/SKILL.md" + [[ -f "$file" ]] || { echo "$image has no $skills_root/$dir/SKILL.md" >&2; exit 1; } + awk -v dir="$dir" -v file="$dir/SKILL.md" ' + function fail(message) { printf "%s: %s\n", file, message > "/dev/stderr"; failed = 1; exit 1 } + function plain(field, value) { + if (value ~ /^["\047]/ || value ~ /^[|>]/) { + fail(field " must be one line of unquoted text, not a quoted or block value: " value) + } + return value + } + NR == 1 && $0 != "---" { exit 1 } + NR > 1 && $0 == "---" { closed = 1; exit } + NR > 1 && /^name:/ { name = plain("name", substr($0, 7)) } + NR > 1 && /^description:/ { description = plain("description", substr($0, 14)) } + END { + if (failed) exit 1 + if (!closed || name == "" || description == "") fail("front matter needs a name and a description") + if (name != dir) fail("name " name " must match its directory " dir) + print name; print description + }' "$file" | jq -Rn --arg path "$skills_root/$dir" '[inputs] | {name: .[0], description: .[1], path: $path}' +done | jq -es --arg version "$version" --arg image "$image" ' + if length == 0 then error("no skills found") else {version: $version, image: $image, skills: sort_by(.name)} end' diff --git a/ui/entrypoint/main.go b/ui/entrypoint/main.go index 9ac07b3834..3e60e3cdb2 100644 --- a/ui/entrypoint/main.go +++ b/ui/entrypoint/main.go @@ -3,6 +3,8 @@ package main import ( + "bytes" + "encoding/json" "fmt" "os" "os/exec" @@ -79,6 +81,9 @@ func render(environ []string) (string, string) { fmt.Fprintf(&b, " \"API_BASE_URL\": \"%s\",\n", jsonEscape(get("KAGENT_UI_API_BASE_URL", "/api"))) fmt.Fprintf(&b, " \"SSO_REDIRECT_PATH\": \"%s\",\n", jsonEscape(get("KAGENT_UI_SSO_REDIRECT_PATH", "/oauth2/start"))) fmt.Fprintf(&b, " \"STREAM_TIMEOUT_MS\": \"%s\",\n", timeout) + if skills := env["KAGENT_UI_BUILTIN_SKILLS"]; skills != "" { + fmt.Fprintf(&b, " \"BUILTIN_SKILLS\": \"%s\",\n", jsonEscape(skills)) + } for _, kv := range extensions { name, value, _ := strings.Cut(kv, "=") fmt.Fprintf(&b, " \"%s\": \"%s\",\n", jsonEscape(name), jsonEscape(value)) @@ -89,9 +94,15 @@ func render(environ []string) (string, string) { return b.String(), basePath } -// jsonEscape makes a value safe inside a JSON string in a \"\\", "KAGENT_UI_EXTENSION_EMPTY=", - "KAGENT_UI_EXTENSION_LINES=first\nsecond", "KAGENT_UI_EXTENSIONLESS=ignored", }, } @@ -97,3 +97,31 @@ func TestMatchesInitScript(t *testing.T) { }) } } + +func TestBuiltinSkills(t *testing.T) { + settings := func(environ ...string) map[string]string { + t.Helper() + config, _ := render(environ) + body := strings.TrimSuffix(strings.TrimPrefix(config, "window.environmentVariables = "), ";\n") + var got map[string]string + if err := json.Unmarshal([]byte(body), &got); err != nil { + t.Fatalf("env-config.js is not a JSON object: %v\n%s", err, config) + } + if strings.Contains(config, " element:\n%s", config) + } + return got + } + + catalog := `{"image":"ghcr.io/kagent-dev/kagent/tools@sha256:aaaa","skills":[{"name":"a","description":"Says \"hi\" \\ then "}]}` + if got := settings("KAGENT_UI_BUILTIN_SKILLS=" + catalog)["BUILTIN_SKILLS"]; got != catalog { + t.Errorf("BUILTIN_SKILLS = %q, want %q", got, catalog) + } + pretty := "{\n\t\"skills\": []\r\n}" + if got := settings("KAGENT_UI_BUILTIN_SKILLS=" + pretty)["BUILTIN_SKILLS"]; got != pretty { + t.Errorf("BUILTIN_SKILLS = %q, want %q", got, pretty) + } + if got, ok := settings()["BUILTIN_SKILLS"]; ok { + t.Errorf("BUILTIN_SKILLS = %q without KAGENT_UI_BUILTIN_SKILLS, want it absent", got) + } +} diff --git a/ui/playwright/live/agent-template-skills.spec.ts b/ui/playwright/live/agent-template-skills.spec.ts new file mode 100644 index 0000000000..bd5a0f1930 --- /dev/null +++ b/ui/playwright/live/agent-template-skills.spec.ts @@ -0,0 +1,259 @@ +import { test, expect, type Page } from "@playwright/test"; +import { createClient } from "@connectrpc/connect"; +import { createGrpcWebTransport } from "@connectrpc/connect-web"; +import { AgentTemplateService } from "../../src/generated/kagent/api/v1/agent_templates_pb"; +import { expectNoLoadFailure, liveRoutes, loadLive, throwawayName } from "./helpers/live"; +import { liveApi } from "./helpers/api"; +import { pressOnce } from "../helpers/resource"; + +/** + * Skills in the agent template form, against a real controller. + * + * Needs the chart's built-in skills catalog in `KAGENT_UI_BUILTIN_SKILLS` (the value the + * chart writes to the UI ConfigMap), the `kagent` Harness and `default-model-config` in + * `kagent`. The chat test also needs a working model behind `default-model-config`. + */ + +const NAMESPACE = "kagent"; +const HARNESS = "kagent"; +const MODEL = `${NAMESPACE}/default-model-config`; +const BUILTIN = "k8s-troubleshooting"; +const READY_TIMEOUT = 180_000; + +// A real public repository and commit. A template does not fetch its skills, so these +// only have to be well formed; an Agent would need them to exist. +const GIT_URL = "https://github.com/kagent-dev/kagent.git"; +const GIT_COMMIT = "8cc31cfe7b9ea951bf3d21478e5a9b00f55025f6"; +const S3 = { + endpoint: "https://s3.amazonaws.com", + bucket: "e2e-skills", + key: "skills.tar.gz", + versionId: "3HL4kqtJlcpXroDTDmJ.rmSpXd3dIbrHY", +}; + +const RAW_CATALOG = process.env.KAGENT_UI_BUILTIN_SKILLS ?? ""; +const catalog = RAW_CATALOG + ? (JSON.parse(RAW_CATALOG) as { image: string; skills: { name: string; path: string }[] }) + : undefined; + +const templates = new Set(); +const agents = new Set(); + +test.skip(!catalog, "KAGENT_UI_BUILTIN_SKILLS is not set to the chart's skills catalog"); + +// Through the API, not the UI: teardown runs after failures, when the page may be anywhere. +test.afterAll(async ({ baseURL }) => { + const api = liveApi(baseURL!); + for (const name of agents) await api.removeAgent(NAMESPACE, name); + const client = templateClient(baseURL!); + for (const name of templates) { + await client.deleteAgentTemplate({ ref: { namespace: NAMESPACE, name } }).catch(() => {}); + } +}); + +function templateClient(baseURL: string) { + return createClient(AgentTemplateService, createGrpcWebTransport({ baseUrl: `${baseURL}/api` })); +} + +/** The template's spec as the controller stores it. */ +async function storedSpec(baseURL: string, name: string) { + const { agentTemplate } = await templateClient(baseURL).getAgentTemplate({ ref: { namespace: NAMESPACE, name } }); + return (agentTemplate?.resource?.value as { spec?: { description?: string; skills?: unknown[] } } | undefined)?.spec; +} + +/** + * Picks an option from the dropdown just opened. Each skill row's kind select has its own + * dropdown with the same titles, and the last one can still be fading out; antd appends a + * dropdown to the body the first time it opens, so the newest open one is the last. + */ +async function choose(page: Page, testId: string, title: string) { + await page.getByTestId(testId).click(); + const open = page.locator(".ant-select-dropdown:not(.ant-select-dropdown-hidden)").last(); + await pressOnce(open.locator(`.ant-select-item-option[title="${title}"]`)); +} + +async function addBuiltinSkill(page: Page, name: string) { + await choose(page, "template-form-add-builtin-skill", name); +} + +async function addOwnSkill(page: Page, index: number, name: string, kind: "OCI image" | "Git" | "S3") { + await page.getByTestId("template-form-add-skill").click(); + await page.getByTestId(`template-form-skill-name-${index}`).fill(name); + if (kind !== "OCI image") await choose(page, `template-form-skill-kind-${index}`, kind); +} + +test("live: agent template skills are saved, read back and kept through an edit", async ({ page, baseURL }) => { + test.setTimeout(240_000); + const name = throwawayName("skills"); + const helmPath = catalog!.skills.find((skill) => skill.name === "helm-release-management")!.path; + const builtinPath = catalog!.skills.find((skill) => skill.name === BUILTIN)!.path; + const expectedSkills = [ + { name: BUILTIN, source: { oci: catalog!.image, path: builtinPath } }, + { name: "own-helm", source: { oci: catalog!.image, path: helmPath } }, + { name: "own-git", source: { git: { url: GIT_URL, commit: GIT_COMMIT }, path: "skills/k8s-troubleshooting" } }, + { name: "own-s3", source: { bucket: { s3: S3 }, path: "skills/incident-review" } }, + ]; + + await test.step("1. the page carries the chart's catalog", async () => { + await loadLive(page, "/agent-templates/new"); + await expectNoLoadFailure(page); + const served = await page.evaluate( + () => (window as { environmentVariables?: Record }).environmentVariables?.BUILTIN_SKILLS, + ); + expect(served).toBe(RAW_CATALOG); + }); + + await test.step("2. create a template with a kagent skill and an OCI, a Git and an S3 skill", async () => { + await page.getByTestId("template-form-name").fill(name); + await choose(page, "template-form-model", MODEL); + await page.getByTestId("template-form-description").fill(`Live ${name}`); + await page.getByTestId("template-form-prompt").fill("You are a concise assistant."); + + await addBuiltinSkill(page, BUILTIN); + await expect(page.getByTestId("template-form-skill-0")).toContainText("kagent"); + await expect(page.getByTestId("template-form-skill-0")).toContainText(BUILTIN); + + await addOwnSkill(page, 1, "own-helm", "OCI image"); + await page.getByTestId("template-form-skill-oci-1").fill(catalog!.image); + await page.getByTestId("template-form-skill-path-1").fill(helmPath); + + await addOwnSkill(page, 2, "own-git", "Git"); + await page.getByTestId("template-form-skill-git-url-2").fill(GIT_URL); + await page.getByTestId("template-form-skill-git-commit-2").fill(GIT_COMMIT); + await page.getByTestId("template-form-skill-path-2").fill("skills/k8s-troubleshooting"); + + await addOwnSkill(page, 3, "own-s3", "S3"); + await page.getByTestId("template-form-skill-s3-endpoint-3").fill(S3.endpoint); + await page.getByTestId("template-form-skill-s3-bucket-3").fill(S3.bucket); + await page.getByTestId("template-form-skill-s3-key-3").fill(S3.key); + await page.getByTestId("template-form-skill-s3-version-3").fill(S3.versionId); + await page.getByTestId("template-form-skill-path-3").fill("skills/incident-review"); + + await expect(page.getByTestId("template-form-problems")).toHaveCount(0); + templates.add(name); + await page.getByTestId("template-submit").click(); + await expect(page).toHaveURL(/\/agents\?tab=templates/, { timeout: 60_000 }); + }); + + await test.step("3. the controller stored exactly those four skills", async () => { + expect((await storedSpec(baseURL!, name))?.skills).toEqual(expectedSkills); + }); + + await test.step("4. reading it back shows the kagent skill as one and the others' sources", async () => { + await loadLive(page, `/agent-templates/${NAMESPACE}/${name}`); + await expectNoLoadFailure(page); + await expect(page.getByTestId("template-edit")).toBeVisible({ timeout: 60_000 }); + await expect(page.getByTestId("template-form-add-skill")).toHaveCount(0); + + const builtin = page.getByTestId("template-form-skill-0"); + await expect(builtin.locator(".ant-tag")).toHaveText("kagent"); + await expect(builtin).toContainText(BUILTIN); + + await expect(page.getByTestId("template-form-skill-name-1")).toHaveText("own-helm"); + await expect(page.getByTestId("template-form-skill-oci-1")).toHaveText(catalog!.image); + await expect(page.getByTestId("template-form-skill-path-1")).toHaveText(helmPath); + + await expect(page.getByTestId("template-form-skill-name-2")).toHaveText("own-git"); + await expect(page.getByTestId("template-form-skill-kind-2")).toContainText("Git"); + await expect(page.getByTestId("template-form-skill-git-url-2")).toHaveText(GIT_URL); + await expect(page.getByTestId("template-form-skill-git-commit-2")).toHaveText(GIT_COMMIT); + + await expect(page.getByTestId("template-form-skill-name-3")).toHaveText("own-s3"); + await expect(page.getByTestId("template-form-skill-kind-3")).toContainText("S3"); + await expect(page.getByTestId("template-form-skill-s3-endpoint-3")).toHaveText(S3.endpoint); + await expect(page.getByTestId("template-form-skill-s3-bucket-3")).toHaveText(S3.bucket); + await expect(page.getByTestId("template-form-skill-s3-key-3")).toHaveText(S3.key); + await expect(page.getByTestId("template-form-skill-s3-version-3")).toHaveText(S3.versionId); + }); + + await test.step("5. an edit to the description keeps the skills", async () => { + await page.getByTestId("template-edit").click(); + await page.getByTestId("template-form-description").fill(`Edited ${name}`); + await page.getByTestId("template-submit").click(); + await expect(page.getByTestId("template-edit")).toBeVisible({ timeout: 60_000 }); + const spec = await storedSpec(baseURL!, name); + expect(spec?.description).toBe(`Edited ${name}`); + expect(spec?.skills).toEqual(expectedSkills); + }); + + await test.step("6. a short Git commit and a name with a slash are refused before saving", async () => { + await loadLive(page, "/agent-templates/new"); + await page.getByTestId("template-form-name").fill(throwawayName("refused")); + await choose(page, "template-form-model", MODEL); + await page.getByTestId("template-form-prompt").fill("You are a concise assistant."); + await addOwnSkill(page, 0, "team/runbooks", "Git"); + await page.getByTestId("template-form-skill-git-url-0").fill(GIT_URL); + await page.getByTestId("template-form-skill-git-commit-0").fill("abc"); + + const problems = page.getByTestId("template-form-problems"); + await expect(problems).toContainText('The skill "team/runbooks" needs a name that is one path segment'); + await expect(problems).toContainText('The skill "team/runbooks" needs a full Git commit ID: 40 or 64 hex characters.'); + await expect(page.getByTestId("template-submit")).toBeDisabled(); + }); +}); + +test("live: an agent built with a kagent skill loads it in chat", async ({ page }, testInfo) => { + test.setTimeout(READY_TIMEOUT + 300_000); + const name = throwawayName("skill-chat"); + + await test.step("1. create an Agent whose inline template has only the kagent skill", async () => { + await loadLive(page, liveRoutes.agentNew); + await expectNoLoadFailure(page); + await page.getByTestId("agent-form-name").fill(name); + await page.getByTestId("agent-form-template-source").getByText("Inline", { exact: true }).click(); + await choose(page, "template-form-model", MODEL); + await page.getByTestId("template-form-description").fill(`Live ${name}`); + await page + .getByTestId("template-form-prompt") + .fill( + "You are a Kubernetes assistant. Before you answer, find the skill whose description matches the task and load it with your skill tool. When you cite a skill, put its text in quotes.", + ); + await addBuiltinSkill(page, BUILTIN); + await expect(page.getByTestId("template-form-skill-0")).toContainText(BUILTIN); + await choose(page, "agent-form-harness-ref", HARNESS); + agents.add(name); + await page.getByTestId("agent-form-submit").click(); + await expect(page).toHaveURL(/\/agents\?tab=agents$/, { timeout: 60_000 }); + }); + + await test.step("2. the Agent becomes Ready, so the kagent-tools image was pulled", async () => { + const status = page.getByTestId(`agent-revision-${NAMESPACE}/${name}`); + await expect + .poll( + async () => { + await page.getByRole("button", { name: /Refresh/ }).click(); + return status.getAttribute("data-revision-state"); + }, + { timeout: READY_TIMEOUT, message: `${name} never became Ready` }, + ) + .toBe("ready"); + }); + + await test.step("3. asked about a crashing pod, it loads the skill", async () => { + await page.getByTestId(`agent-link-${NAMESPACE}-${name}`).click(); + await page + .getByTestId("chat-input") + .fill( + // Not "word for word": Azure OpenAI's content filter reads that as a jailbreak and refuses the request. + "A pod in namespace default is in CrashLoopBackOff. Which of your skills applies? Load it, then tell me the opening line of its instructions in quotes.", + ); + await page.getByTestId("chat-send").click(); + await expect(page).toHaveURL(/\/agents\/[0-9a-f-]{36}\/chat$/, { timeout: 60_000 }); + // The turn has started once the agent says something, and has ended once it can no longer be cancelled. + await expect(page.locator('[data-testid="chat-message"][data-role="agent"]').first()).toBeVisible({ timeout: 240_000 }); + await expect(page.getByTestId("chat-cancel")).toHaveCount(0, { timeout: 240_000 }); + + const call = page.locator('[data-testid="chat-tool-call"][data-tool-name="load_skill"]'); + await expect(call.first()).toBeVisible(); + await expect(call.first().getByTestId("chat-tool-payload")).toContainText(BUILTIN); + // The skill's own text, from opt/kagent-tools-skills/skills/k8s-troubleshooting/SKILL.md in the kagent-tools image. + const loaded = page.locator('[data-testid="chat-tool-result"][data-tool-name="load_skill"]'); + await expect(loaded.first().getByTestId("chat-tool-payload")).toContainText("Work from symptoms to cause."); + // The reply's wording varies between runs (it sometimes quotes the heading), so the + // skill's text is checked in the tool result above, not in the reply. + await expect(page.locator('[data-testid="chat-message"][data-role="agent"]').last()).not.toBeEmpty(); + await page.screenshot({ path: testInfo.outputPath("chat-reply.png"), fullPage: true }); + testInfo.annotations.push({ type: "session", description: new URL(page.url()).pathname.split("/")[2] }); + }); +}); + diff --git a/ui/playwright/tests/agent-templates/agent-templates.spec.ts b/ui/playwright/tests/agent-templates/agent-templates.spec.ts index d0ec66a587..ca9ad42ad0 100644 --- a/ui/playwright/tests/agent-templates/agent-templates.spec.ts +++ b/ui/playwright/tests/agent-templates/agent-templates.spec.ts @@ -4,12 +4,15 @@ import { LIFECYCLE_TIMEOUT, confirmation, expectRequired, + optionNamed, pressOnce, selectOption, } from "../../helpers/resource"; /** A reusable template can be authored without choosing an Agent or Harness. */ const CREATED = "browser-made"; +const GIT_URL = "https://github.com/example/runbooks.git"; +const GIT_COMMIT = "0123456789abcdef0123456789abcdef01234567"; /* * A lifecycle is longer than a journey, so it gets its own budget — see @@ -87,6 +90,37 @@ test("agent templates: a template is created, read, edited and deleted", async ( await page.getByTestId("template-form-mcp-remove-0").click(); }); + await test.step("a kagent skill and the reader's own Git skill can be added", async () => { + const builtin = page.getByTestId("template-form-add-builtin-skill"); + await builtin.click(); + await pressOnce(optionNamed(page, "k8s-troubleshooting")); + const added = page.getByTestId("template-form-skill-0"); + await expect(added).toContainText("k8s-troubleshooting"); + await expect(added).toContainText("Diagnose unhealthy Kubernetes workloads"); + + // Offered once: a second copy would be a duplicate name the CRD refuses. + await builtin.click(); + await expect(optionNamed(page, "k8s-troubleshooting")).toHaveClass( + /ant-select-item-option-disabled/, + ); + await page.keyboard.press("Escape"); + + await page.getByTestId("template-form-add-skill").click(); + // Each skill is its own card, with a visible label on every field and a named remove. + const own = page.getByTestId("template-form-skill-1"); + await own.getByLabel("Skill name").fill("incident-review"); + await expect(own.getByRole("button", { name: "Remove skill incident-review" })).toBeVisible(); + await selectOption(page, "template-form-skill-kind-1", "Git"); + await own.getByLabel("Repository").fill(GIT_URL); + await own.getByLabel("Commit").fill("main"); + await expect(page.getByTestId("template-form-problems")).toContainText( + 'The skill "incident-review" needs a full Git commit ID', + ); + await page.getByTestId("template-form-skill-git-commit-1").fill(GIT_COMMIT); + await own.getByLabel("Path").fill("skills/incident-review"); + await expect(page.getByTestId("template-form-problems")).not.toContainText("skill"); + }); + await test.step("a namespace can select a visible model from another namespace", async () => { await selectOption(page, "template-form-namespace", "default"); await selectOption(page, "template-form-model", "kagent/default-model-config"); @@ -153,6 +187,29 @@ test("agent templates: a template is created, read, edited and deleted", async ( await expect(page.getByTestId("template-form-name")).toHaveCount(0); // And nothing that authors: the add buttons only exist when something can be added. await expect(page.getByTestId("template-form-add-label")).toHaveCount(0); + await expect(page.getByTestId("template-form-add-skill")).toHaveCount(0); + + // The skills as the backend stored them: the kagent skill is still recognized as + // one, so it was saved as the kagent-tools image and its directory. + await expect(page.getByTestId("template-form-skill-0")).toContainText("kagent"); + await expect(page.getByTestId("template-form-skill-0")).toContainText("k8s-troubleshooting"); + await expect(page.getByTestId("template-form-skill-name-1")).toHaveText("incident-review"); + await expect(page.getByTestId("template-form-skill-kind-1")).toContainText("Git"); + // The repository opens in a new tab. + const repo = page.getByTestId("template-form-skill-git-url-1"); + await expect(repo).toHaveText(GIT_URL); + await expect(repo).toHaveAttribute("href", GIT_URL); + await expect(repo).toHaveAttribute("target", "_blank"); + await expect(page.getByTestId("template-form-skill-git-commit-1")).toHaveText(GIT_COMMIT); + await expect(page.getByTestId("template-form-skill-path-1")).toHaveText("skills/incident-review"); + // Each read-only source value shows in full, not cut off, even in a narrow window. + const viewport = page.viewportSize()!; + await page.setViewportSize({ width: 480, height: viewport.height }); + for (const testId of ["git-url", "git-commit", "path"]) { + const cell = page.getByTestId(`template-form-skill-${testId}-1`).locator("xpath=ancestor::dd"); + await expect.poll(() => cell.evaluate((el) => el.scrollWidth <= el.clientWidth), { message: testId }).toBe(true); + } + await page.setViewportSize(viewport); // Nor does it mark anything required. Read-only is not a form, so it asks for // nothing — an asterisk here would be demanding a reader supply something they are @@ -218,6 +275,8 @@ test("agent templates: a template is created, read, edited and deleted", async ( await expect(page.getByTestId("template-form-description")).toHaveValue( "Edited by the suite.", ); + await expect(page.getByTestId("template-form-skill-0")).toContainText("k8s-troubleshooting"); + await expect(page.getByTestId("template-form-skill-name-1")).toHaveText("incident-review"); }); await test.step("11. deleting says what it costs, in the confirmation", async () => { @@ -276,9 +335,30 @@ test("agent templates: a template is created, read, edited and deleted", async ( "Answers questions about workloads in the cluster.", ); - // `k8s-agent-7f3a91c` carries a `skills` entry, which this form does not author. A - // save built from the fields it shows would remove it, and the API would accept that - // without a word — so the notice standing here is what tells a reader it survives. + await expect(page.getByTestId("template-form-skill-0")).toContainText("k8s-troubleshooting"); + await expect(page.getByTestId("template-form-skill-git-url-1")).toHaveAttribute( + "href", + "https://github.com/example/runbooks.git", + ); + + // Saved under an earlier kagent-tools image digest, as after a chart upgrade: still a + // kagent skill, marked as older. Reading offers no Update; editing does, and the + // saved row then pins the current image. + const older = page.getByTestId("template-form-skill-2"); + await expect(older).toContainText("helm-release-management"); + await expect(page.getByTestId("template-form-skill-outdated-2")).toBeVisible(); + await expect(page.getByTestId("template-form-skill-update-2")).toHaveCount(0); + await page.getByTestId("template-edit").click(); + await page.getByTestId("template-form-skill-update-2").click(); + await expect(page.getByTestId("template-form-skill-outdated-2")).toHaveCount(0); + await page.getByTestId("template-submit").click(); + await expect(page.getByTestId("template-edit")).toBeVisible({ timeout: 30_000 }); + await expect(older).toContainText("helm-release-management"); + await expect(page.getByTestId("template-form-skill-outdated-2")).toHaveCount(0); + + // `k8s-agent-7f3a91c` carries prompt data sources, which this form does not author. A + // save built from the fields it shows would remove them, and the API would accept that + // without a word — so the notice standing here is what tells a reader they survive. // Asserted on this template rather than on the one the journey made, because the // form cannot create a template that has anything to lose. await expect(page.getByTestId("template-form-unshown")).toContainText( diff --git a/ui/src/api/domain/agentTemplates.ts b/ui/src/api/domain/agentTemplates.ts index b0a0bfd4a2..e6ea1a471d 100644 --- a/ui/src/api/domain/agentTemplates.ts +++ b/ui/src/api/domain/agentTemplates.ts @@ -60,8 +60,7 @@ export interface ToolBinding { /** * One immutable artifact, as `skills` and `plugins` reference content. * - * Exactly one of `oci`, `git` or `bucket`. Left as-is by this build's form, which - * does not author skills or plugins — see `AgentTemplateSpec.skills`. + * Exactly one of `oci`, `git` or `bucket`, plus an optional relative `path` inside it. */ export interface ArtifactSource { oci?: string; diff --git a/ui/src/api/runtimeConfig.test.ts b/ui/src/api/runtimeConfig.test.ts new file mode 100644 index 0000000000..a27266b3cb --- /dev/null +++ b/ui/src/api/runtimeConfig.test.ts @@ -0,0 +1,46 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { runtimeConfig } from "./runtimeConfig"; + +const IMAGE = "ghcr.io/kagent-dev/kagent/tools@sha256:" + "a".repeat(64); + +afterEach(() => { + delete window.environmentVariables; + vi.restoreAllMocks(); +}); + +describe("runtimeConfig builtinSkills", () => { + it("reads the catalog the chart passes", () => { + window.environmentVariables = { + BUILTIN_SKILLS: JSON.stringify({ + image: IMAGE, + skills: [{ name: "k8s-troubleshooting", description: "Diagnose unhealthy workloads.", path: "opt/kagent-tools-skills/skills/k8s-troubleshooting" }], + }), + }; + expect(runtimeConfig().builtinSkills).toEqual({ + image: IMAGE, + skills: [{ name: "k8s-troubleshooting", description: "Diagnose unhealthy workloads.", path: "opt/kagent-tools-skills/skills/k8s-troubleshooting" }], + }); + }); + + it("offers nothing when the chart passes no catalog", () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + expect(runtimeConfig().builtinSkills).toBeUndefined(); + window.environmentVariables = { BUILTIN_SKILLS: "" }; + expect(runtimeConfig().builtinSkills).toBeUndefined(); + expect(warn).not.toHaveBeenCalled(); + }); + + it.each([ + ["not JSON", "{"], + ["not an object", "[]"], + ["no image", JSON.stringify({ skills: [] })], + ["no skills list", JSON.stringify({ image: IMAGE })], + ["a skill without a name", JSON.stringify({ image: IMAGE, skills: [{ description: "x", path: "opt/kagent-tools-skills/skills/x" }] })], + ["a skill without a path", JSON.stringify({ image: IMAGE, skills: [{ name: "x", description: "x" }] })], + ])("warns and offers nothing for %s", (_, value) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + window.environmentVariables = { BUILTIN_SKILLS: value }; + expect(runtimeConfig().builtinSkills).toBeUndefined(); + expect(warn).toHaveBeenCalledOnce(); + }); +}); diff --git a/ui/src/api/runtimeConfig.ts b/ui/src/api/runtimeConfig.ts index e6d17462f3..79d0581b52 100644 --- a/ui/src/api/runtimeConfig.ts +++ b/ui/src/api/runtimeConfig.ts @@ -12,17 +12,33 @@ import { ENV_DEFAULTS, env, withBasePath } from "@/env"; +export interface BuiltinSkill { + name: string; + description: string; + /** The skill's directory inside the catalog image. */ + path: string; +} + +/** The skills kagent ships, all in one digest-pinned image. */ +export interface BuiltinSkillsCatalog { + image: string; + skills: BuiltinSkill[]; +} + export interface RuntimeConfig { /** Where "Sign in with SSO" sends the browser. */ ssoRedirectPath: string; /** Chat stream inactivity timeout before the client aborts. */ streamTimeoutMs: number; + /** Absent when the chart turns built-in skills off, so the UI offers none. */ + builtinSkills: BuiltinSkillsCatalog | undefined; } /** What the app runs on when nothing is configured. */ export const RUNTIME_CONFIG_DEFAULTS: RuntimeConfig = { ssoRedirectPath: ENV_DEFAULTS.SSO_REDIRECT_PATH, streamTimeoutMs: Number(ENV_DEFAULTS.STREAM_TIMEOUT_MS), + builtinSkills: undefined, }; /** @@ -36,6 +52,7 @@ export function runtimeConfig(): RuntimeConfig { return { ssoRedirectPath: withBasePath(env("SSO_REDIRECT_PATH")), streamTimeoutMs: streamTimeoutMs(), + builtinSkills: builtinSkills(), }; } @@ -52,3 +69,40 @@ function streamTimeoutMs(): number { ? parsed : RUNTIME_CONFIG_DEFAULTS.streamTimeoutMs; } + +function builtinSkills(): BuiltinSkillsCatalog | undefined { + const text = env("BUILTIN_SKILLS"); + if (text === "") return undefined; + const catalog = parseBuiltinSkills(text); + if (!catalog) console.warn("BUILTIN_SKILLS is not a valid skills catalog, so no built-in skills are offered."); + return catalog; +} + +function parseBuiltinSkills(text: string): BuiltinSkillsCatalog | undefined { + let value: unknown; + try { + value = JSON.parse(text); + } catch { + return undefined; + } + if (!isRecord(value) || !isText(value.image)) return undefined; + if (!Array.isArray(value.skills)) return undefined; + const skills: BuiltinSkill[] = []; + for (const skill of value.skills) { + if (!isRecord(skill) || !isText(skill.name) || !isText(skill.path)) return undefined; + skills.push({ + name: skill.name, + description: typeof skill.description === "string" ? skill.description : "", + path: skill.path, + }); + } + return { image: value.image, skills }; +} + +function isRecord(value: unknown): value is Record { + return value !== null && typeof value === "object" && !Array.isArray(value); +} + +function isText(value: unknown): value is string { + return typeof value === "string" && value !== ""; +} diff --git a/ui/src/components/agent-template-form/AgentTemplateForm.tsx b/ui/src/components/agent-template-form/AgentTemplateForm.tsx index ea8a847d7a..4979d05656 100644 --- a/ui/src/components/agent-template-form/AgentTemplateForm.tsx +++ b/ui/src/components/agent-template-form/AgentTemplateForm.tsx @@ -22,6 +22,7 @@ import { type AgentTemplateDraft, type OutputSource, } from "./agentTemplateDraft"; +import { SkillsField } from "./SkillsField"; import { referenceLabel, type ObjectReference } from "@/api/domain/agentTemplates"; import { KAGENT_API_VERSION } from "@/api/grpc/wire"; @@ -68,9 +69,7 @@ function referenceFromPicker(value: string): ObjectReference { * * ## What it does not author, and why that is safe * - * `skills`, `plugins` and `promptTemplate` are not on this form. Each is a rich - * shape of its own — three artifact-source variants with strict CEL patterns — and - * authoring them properly is its own piece of work. What matters is that an edit + * `plugins` and `promptTemplate` are not on this form. What matters is that an edit * does not *lose* them: `specFromDraft` merges onto the spec it was given rather * than building a fresh one, and `agentTemplateDraft.test.ts` pins that. * @@ -601,6 +600,8 @@ export function AgentTemplateForm({ + set("skills", next)} readOnly={readOnly} /> + {embedded ? null : ( @@ -662,14 +663,14 @@ export function AgentTemplateForm({ )} {/* Said rather than left to be assumed. A reader who knows their template has - skills and cannot see them here would reasonably conclude a save will + plugins and cannot see them here would reasonably conclude a save will drop them. */} {hasUnshownFields ? ( diff --git a/ui/src/components/agent-template-form/SkillsField.tsx b/ui/src/components/agent-template-form/SkillsField.tsx new file mode 100644 index 0000000000..24773624e6 --- /dev/null +++ b/ui/src/components/agent-template-form/SkillsField.tsx @@ -0,0 +1,338 @@ +import { useMemo, type ReactNode } from "react"; +import { Button, Card, Form, Input, Select, Space, Tag, Tooltip, Typography } from "antd"; +import { useTheme } from "@emotion/react"; +import { ExternalLink, Plus, Trash } from "lucide-react"; +import { runtimeConfig } from "@/api/runtimeConfig"; +import { + builtinSkillDraft, + emptySkillDraft, + matchBuiltinSkill, + type SkillDraft, + type SkillSourceKind, +} from "./agentTemplateDraft"; + +const { Text } = Typography; + +const SOURCE_LABEL: Record = { oci: "OCI image", git: "Git", s3: "S3" }; + +/** + * The template's `spec.skills`: kagent's built-in skills, offered from the catalog + * the chart passes, and the reader's own skills by OCI, Git or S3 source. + */ +export function SkillsField({ + skills, + onChange, + readOnly, +}: { + skills: SkillDraft[]; + onChange: (next: SkillDraft[]) => void; + readOnly: boolean; +}) { + const theme = useTheme(); + const catalog = useMemo(() => runtimeConfig().builtinSkills, []); + + const setSkill = (index: number, patch: Partial) => { + const next = [...skills]; + next[index] = { ...next[index], ...patch }; + onChange(next); + }; + const remove = (index: number) => onChange(skills.filter((_, at) => at !== index)); + + const muted = { color: theme.color.textMuted, fontSize: 12 }; + const mono = { fontFamily: theme.font.mono, fontSize: 12, wordBreak: "break-all" as const }; + + // Each skill is its own card, so its remove button reads as belonging to it. + const card = (index: number, children: ReactNode) => ( + +
{children}
+
+ ); + + const header = (children: ReactNode, index: number, name: string) => ( +
+ {children} + {readOnly ? null : ( + +
+ ); + + const details = (entries: Array<[string, ReactNode]>) => ( +
+ {entries.map(([label, value]) => ( +
+
{label}
+
{value}
+
+ ))} +
+ ); + + const link = (url: string, testId: string) => + /^https?:\/\//.test(url) ? ( + + {url} + + + ) : ( + + {url} + + ); + + const readOnlySource = (skill: SkillDraft, index: number): Array<[string, ReactNode]> => { + const id = (field: string) => `template-form-skill-${field}-${index}`; + const value = (text: string, field: string) => ( + + {text} + + ); + const entries: Array<[string, ReactNode]> = []; + if (skill.sourceKind === "oci") entries.push(["Image", value(skill.oci, "oci")]); + if (skill.sourceKind === "git") { + entries.push(["Repository", link(skill.gitUrl, id("git-url"))]); + entries.push(["Commit", value(skill.gitCommit, "git-commit")]); + } + if (skill.sourceKind === "s3") { + entries.push(["Endpoint", link(skill.s3Endpoint, id("s3-endpoint"))]); + entries.push(["Bucket", value(skill.s3Bucket, "s3-bucket")]); + entries.push(["Key", value(skill.s3Key, "s3-key")]); + entries.push(["Version", value(skill.s3VersionId, "s3-version")]); + if (skill.s3Region) entries.push(["Region", value(skill.s3Region, "s3-region")]); + } + if (skill.path) entries.push(["Path", value(skill.path, "path")]); + return entries; + }; + + return ( + +
+ {readOnly && skills.length === 0 ? No skills. : null} + + {skills.map((skill, index) => { + const builtin = matchBuiltinSkill(skill, catalog); + if (builtin && catalog) { + return card( + index, + <> + {header( + <> + kagent + {skill.name} + {builtin.current ? null : ( + + Older version + + )} + {builtin.current || readOnly ? null : ( + + )} + , + index, + skill.name, + )} + {builtin.skill.description} + , + ); + } + + if (readOnly) { + return card( + index, + <> + {header( + <> + + {SOURCE_LABEL[skill.sourceKind]} + + + {skill.name || "Not set"} + + , + index, + skill.name, + )} + {details(readOnlySource(skill, index))} + , + ); + } + + // Ids keep each visible label tied to its input for screen readers. + const fieldId = (testId: string) => `template-form-skill-${index}-${testId}-input`; + // `required` marks the fields the form refuses to save without. + const field = (label: string, testId: string, width: number, control: ReactNode, required = true) => ( + + {control} + + ); + const input = ( + name: Exclude, + label: string, + testId: string, + example: string, + width: number, + required = true, + ) => + field( + label, + testId, + width, + setSkill(index, { [name]: event.target.value })} + />, + required, + ); + return card( + index, + <> + {header({skill.name.trim() || "New skill"}, index, skill.name)} +
+
+ {input("name", "Skill name", "name", "", 200)} + {field( + "Source", + "kind", + 120, +
+ { + const skill = catalog.skills.find((entry) => entry.name === name); + if (skill) onChange([...skills, builtinSkillDraft(catalog, skill)]); + }} + options={catalog.skills.map((skill) => ({ + value: skill.name, + title: skill.name, + disabled: skills.some((row) => row.name.trim() === skill.name), + label: ( + + {skill.name} + {skill.description} + + ), + }))} + /> +
+ ) : null} + + + )} +
+ + ); +} diff --git a/ui/src/components/agent-template-form/agentTemplateDraft.test.ts b/ui/src/components/agent-template-form/agentTemplateDraft.test.ts index 4556806894..8260709fbf 100644 --- a/ui/src/components/agent-template-form/agentTemplateDraft.test.ts +++ b/ui/src/components/agent-template-form/agentTemplateDraft.test.ts @@ -1,20 +1,24 @@ import { describe, expect, it } from "vitest"; import { + builtinSkillDraft, draftFromTemplate, draftFromSpec, draftProblems, emptyDraft, + emptySkillDraft, labelsFromDraft, + matchBuiltinSkill, specFromDraft, + type AgentTemplateDraft, } from "./agentTemplateDraft"; import type { AgentTemplate } from "@/api/domain/agentTemplates"; /** * The agent-template draft, and the one property that is invisible on screen. * - * An edit form still omits three rich `AgentTemplateSpec` fields. Building the update - * out of only what it shows deletes those fields — `skills`, `plugins`, - * `promptTemplate` — and the API accepts that happily. Nothing on the page changes, + * An edit form still omits two `AgentTemplateSpec` fields. Building the update out of + * only what it shows deletes those fields — `plugins`, `promptTemplate` — and the API + * accepts that happily. Nothing on the page changes, * no error is raised, and the loss only appears on the cluster. * * This repository has made that mistake once already, on the agent form. So the @@ -76,11 +80,7 @@ describe("the agent template draft", () => { // A save with nothing edited at all. const spec = specFromDraft(draft, template.resource.spec); - // The three the form never displays, still there. - expect(spec.skills, "an edit must not delete skills it never showed").toEqual( - template.resource.spec.skills, - ); - expect(spec.plugins).toEqual(template.resource.spec.plugins); + expect(spec.plugins, "an edit must not delete plugins it never showed").toEqual(template.resource.spec.plugins); expect(spec.promptTemplate).toEqual(template.resource.spec.promptTemplate); }); @@ -95,6 +95,7 @@ describe("the agent template draft", () => { expect(spec.systemPrompt).toBe("Be brief."); expect(spec.outputSchema).toEqual(template.resource.spec.outputSchema); expect(spec.tools).toEqual(template.resource.spec.tools); + expect(spec.skills).toEqual(template.resource.spec.skills); }); it("drops what it does show when the draft empties it", () => { @@ -110,7 +111,7 @@ describe("the agent template draft", () => { expect(spec).not.toHaveProperty("systemPrompt"); expect(spec).not.toHaveProperty("tools"); // And still not at the cost of the fields it does not model. - expect(spec.skills).toHaveLength(1); + expect(spec.plugins).toHaveLength(1); }); it("never sends both prompt sources, because the CRD rejects both", () => { @@ -381,3 +382,175 @@ describe("cross-namespace references", () => { expect(cleared.tools).toHaveLength(1); }); }); + +const DIGEST = "sha256:" + "0".repeat(64); +const CATALOG = { + image: `ghcr.io/kagent-dev/kagent/tools@${DIGEST}`, + skills: [{ name: "k8s-troubleshooting", description: "Diagnose unhealthy workloads.", path: "opt/kagent-tools-skills/skills/k8s-troubleshooting" }], +}; + +function draftWithSkills(...skills: Partial[]): AgentTemplateDraft { + const draft = emptyDraft("kagent"); + draft.name = "t"; + draft.skills = skills.map((skill) => ({ ...emptySkillDraft(), ...skill })); + return draft; +} + +describe("skills", () => { + it("round-trips every source kind, with and without the optional fields", () => { + const skills = [ + { name: "oci", source: { oci: `ghcr.io/org/skills@${DIGEST}` } }, + { name: "git", source: { git: { url: "https://github.com/org/skills.git", commit: "a".repeat(40) }, path: "skills/git" } }, + { + name: "s3", + source: { + bucket: { s3: { endpoint: "https://s3.example.com", bucket: "b", key: "k.tgz", versionId: "v1", region: "eu-west-1" } }, + }, + }, + { + name: "s3-no-region", + source: { bucket: { s3: { endpoint: "https://s3.example.com", bucket: "b", key: "k.tgz", versionId: "v2" } }, path: "x" }, + }, + ]; + const draft = draftFromSpec({ skills }, "kagent"); + + expect(draft.skills.map((skill) => skill.sourceKind)).toEqual(["oci", "git", "s3", "s3"]); + expect(specFromDraft(draft, { skills }).skills).toEqual(skills); + }); + + it("trims what it sends, omits an empty path and region, and drops blank rows", () => { + const draft = draftWithSkills( + { name: " review ", sourceKind: "s3", s3Endpoint: " https://s3.example.com ", s3Bucket: "b", s3Key: "k", s3VersionId: "v", path: " " }, + { sourceKind: "git" }, + ); + + expect(specFromDraft(draft).skills).toEqual([ + { name: "review", source: { bucket: { s3: { endpoint: "https://s3.example.com", bucket: "b", key: "k", versionId: "v" } } } }, + ]); + expect(draftProblems(draft, { isCreate: true })).toEqual([]); + }); + + it("removes spec.skills when the draft has none", () => { + const spec = { skills: [{ name: "a", source: { oci: `ghcr.io/org/skills@${DIGEST}` } }] }; + const draft = draftFromSpec(spec, "kagent"); + draft.skills = []; + + expect(specFromDraft(draft, spec)).not.toHaveProperty("skills"); + }); + + it("keeps the other kinds' text when the source kind changes", () => { + const draft = draftWithSkills({ name: "a", sourceKind: "git", gitUrl: "https://x.test/r.git", gitCommit: "b".repeat(40) }); + draft.skills[0].sourceKind = "oci"; + draft.skills[0].sourceKind = "git"; + + expect(specFromDraft(draft).skills?.[0].source).toEqual({ git: { url: "https://x.test/r.git", commit: "b".repeat(40) } }); + }); + + it("writes a kagent skill as the catalog image and path, and recognizes it again", () => { + const draft = draftWithSkills(); + draft.skills = [builtinSkillDraft(CATALOG, CATALOG.skills[0])]; + const spec = specFromDraft(draft); + + expect(spec.skills).toEqual([ + { name: "k8s-troubleshooting", source: { oci: CATALOG.image, path: "opt/kagent-tools-skills/skills/k8s-troubleshooting" } }, + ]); + const reopened = draftFromSpec(spec, "kagent").skills[0]; + expect(matchBuiltinSkill(reopened, CATALOG)).toEqual({ skill: CATALOG.skills[0], current: true }); + expect(matchBuiltinSkill({ ...reopened, path: "opt/kagent-tools-skills/skills/other" }, CATALOG)).toBeUndefined(); + expect(matchBuiltinSkill({ ...reopened, name: "my-copy" }, CATALOG)).toBeUndefined(); + expect(matchBuiltinSkill({ ...reopened, oci: `ghcr.io/org/skills@${DIGEST}` }, CATALOG)).toBeUndefined(); + expect(matchBuiltinSkill(reopened, undefined)).toBeUndefined(); + }); + + it("recognizes a kagent skill from an earlier image of the same repository as an older version", () => { + const older = draftWithSkills({ + name: "k8s-troubleshooting", + oci: `ghcr.io/kagent-dev/kagent/tools@sha256:${"1".repeat(64)}`, + path: "opt/kagent-tools-skills/skills/k8s-troubleshooting", + }).skills[0]; + + expect(matchBuiltinSkill(older, CATALOG)).toEqual({ skill: CATALOG.skills[0], current: false }); + }); + + it("reports what the CRD would refuse, naming the skill", () => { + const draft = draftWithSkills( + { name: "", oci: `ghcr.io/org/skills@${DIGEST}` }, + { name: "tagged", oci: "ghcr.io/org/skills:v1" }, + { name: "git", sourceKind: "git", gitUrl: "git@github.com:org/r.git", gitCommit: "abc123" }, + { name: "s3", sourceKind: "s3", s3Endpoint: "s3.example.com", s3Bucket: "b" }, + { name: "up", oci: `ghcr.io/org/skills@${DIGEST}`, path: "skills/../secrets" }, + { name: "abs", oci: `ghcr.io/org/skills@${DIGEST}`, path: "/skills" }, + { name: "up", oci: `ghcr.io/org/skills@${DIGEST}` }, + { name: "..", oci: `ghcr.io/org/skills@${DIGEST}` }, + { name: "a/b", oci: `ghcr.io/org/skills@${DIGEST}` }, + { name: "a\\b", oci: `ghcr.io/org/skills@${DIGEST}` }, + { name: "from-plugin", oci: `ghcr.io/org/skills@${DIGEST}` }, + ); + draft.pluginSkills = ["from-plugin"]; + + expect(draftProblems(draft, { isCreate: true })).toEqual([ + "Skill 1 needs a name.", + 'The skill "tagged" needs an OCI image pinned by digest, like ghcr.io/org/skills@sha256:<64 hex characters>.', + 'The skill "git" needs a Git URL that starts with http:// or https://.', + 'The skill "git" needs a full Git commit ID: 40 or 64 hex characters.', + 'The skill "s3" needs an S3 endpoint that starts with http:// or https://.', + 'The skill "s3" needs an S3 key and version ID.', + 'The skill "up" needs a path relative to the artifact, without ".." segments.', + 'The skill "abs" needs a path relative to the artifact, without ".." segments.', + 'The skill ".." needs a name that is one path segment: not "." or "..", and without "/" or "\\".', + 'The skill "a/b" needs a name that is one path segment: not "." or "..", and without "/" or "\\".', + 'The skill "a\\b" needs a name that is one path segment: not "." or "..", and without "/" or "\\".', + 'More than one skill is named "up". Skill names must be unique.', + 'A plugin on this template already selects a skill named "from-plugin". Skill names must be unique.', + ]); + }); + + it("refuses an uppercase image path but not an uppercase registry host, as the controller does", () => { + const draft = draftWithSkills( + { name: "org", oci: `ghcr.io/MyOrg/skills@${DIGEST}` }, + { name: "local", oci: `localhost:5001/Skills@${DIGEST}` }, + { name: "hub", oci: `MyOrg/skills@${DIGEST}` }, + { name: "host", oci: `GHCR.io/myorg/skills@${DIGEST}` }, + { name: "lower", oci: `ghcr.io/myorg/skills@${DIGEST}` }, + ); + + expect(draftProblems(draft, { isCreate: true })).toEqual([ + 'The skill "org" needs an image path in lowercase, like ghcr.io/org/skills.', + 'The skill "local" needs an image path in lowercase, like ghcr.io/org/skills.', + 'The skill "hub" needs an image path in lowercase, like ghcr.io/org/skills.', + ]); + }); + + it("checks names against the skills the existing spec's plugins select", () => { + const spec = { + skills: [{ name: "review", source: { oci: `ghcr.io/org/skills@${DIGEST}` } }], + plugins: [{ source: { oci: `ghcr.io/org/plugin@${DIGEST}` }, skills: ["review"] }], + }; + + expect(draftProblems(draftFromSpec(spec, "kagent"), { isCreate: false })).toEqual([ + 'A plugin on this template already selects a skill named "review". Skill names must be unique.', + ]); + }); + + it("numbers a skill by its row, counting blank rows", () => { + const draft = draftWithSkills({}, { oci: `ghcr.io/org/skills@${DIGEST}` }); + + expect(draftProblems(draft, { isCreate: true })).toEqual(["Skill 2 needs a name."]); + }); + + it("treats a row as blank from its name, path and active source, and saves only that source", () => { + const hidden = { sourceKind: "oci" as const, gitUrl: "https://x.test/r.git", s3Bucket: "b" }; + expect(specFromDraft(draftWithSkills(hidden))).not.toHaveProperty("skills"); + expect(draftProblems(draftWithSkills(hidden), { isCreate: true })).toEqual([]); + + const draft = draftWithSkills({ ...hidden, name: "a", oci: `ghcr.io/org/skills@${DIGEST}` }); + expect(specFromDraft(draft).skills).toEqual([{ name: "a", source: { oci: `ghcr.io/org/skills@${DIGEST}` } }]); + }); + + it("allows at most 50 skills", () => { + const skills = Array.from({ length: 51 }, (_, index) => ({ name: `s${index}`, oci: `ghcr.io/org/skills@${DIGEST}` })); + + expect(draftProblems(draftWithSkills(...skills.slice(0, 50)), { isCreate: true })).toEqual([]); + expect(draftProblems(draftWithSkills(...skills), { isCreate: true })).toEqual(["A template can have at most 50 skills."]); + }); +}); diff --git a/ui/src/components/agent-template-form/agentTemplateDraft.ts b/ui/src/components/agent-template-form/agentTemplateDraft.ts index 998c9f70a2..d5e7497851 100644 --- a/ui/src/components/agent-template-form/agentTemplateDraft.ts +++ b/ui/src/components/agent-template-form/agentTemplateDraft.ts @@ -4,14 +4,13 @@ * ## The property that matters most here * * **An edit must not delete what the form does not show.** This form authors the - * common interactive fields; `skills`, `plugins` and - * `promptTemplate` are rich enough — three artifact-source shapes, each with its own - * strict CEL pattern — that authoring them is its own piece of work. + * common interactive fields and `skills`; `plugins` and `promptTemplate` are not on + * it yet. * * Building an update out of the fields a form displays silently drops the rest. That * has already happened in this repository once. `specFromDraft` therefore merges * into the spec it was given rather than constructing a fresh one, so a template carrying - * skills survives an edit that never mentioned them. + * plugins survives an edit that never mentioned them. * * `agentTemplateDraft.test.ts` pins that, because it is invisible on screen — the * form looks identical either way and the loss only shows up on the cluster. @@ -19,10 +18,13 @@ import type { AgentTemplate, + AgentTemplateSkill, AgentTemplateSpec, + ArtifactSource, ToolBinding, ObjectReference, } from "@/api/domain/agentTemplates"; +import type { BuiltinSkill, BuiltinSkillsCatalog } from "@/api/runtimeConfig"; /** One MCP tool selection, flattened for a form to hold. */ export interface McpToolDraft { @@ -44,6 +46,35 @@ export interface SubAgentToolDraft { refName: string; } +export type SkillSourceKind = "oci" | "git" | "s3"; + +/** + * One skill, flattened for a form to hold. Every source kind keeps its own fields, + * so switching kind and back does not lose what was typed. + */ +export interface SkillDraft { + name: string; + sourceKind: SkillSourceKind; + oci: string; + gitUrl: string; + gitCommit: string; + s3Endpoint: string; + s3Bucket: string; + s3Key: string; + s3VersionId: string; + s3Region: string; + path: string; +} + +type SkillTextField = Exclude; + +/** The fields each source kind writes. Text in another kind's fields is ignored. */ +const SKILL_SOURCE_FIELDS: Record = { + oci: ["oci"], + git: ["gitUrl", "gitCommit"], + s3: ["s3Endpoint", "s3Bucket", "s3Key", "s3VersionId", "s3Region"], +}; + /** Where the system prompt comes from. The CRD rejects both at once. */ export type PromptSource = "inline" | "configMap"; @@ -68,6 +99,12 @@ export interface AgentTemplateDraft { outputStrict: boolean; mcpTools: McpToolDraft[]; subAgentTools: SubAgentToolDraft[]; + skills: SkillDraft[]; + /** + * The skill names `spec.plugins` selects. Not authored here, but the controller + * rejects a skill named like one of them. + */ + pluginSkills: string[]; labels: { key: string; value: string }[]; } @@ -89,10 +126,57 @@ export function emptyDraft(namespace: string): AgentTemplateDraft { outputStrict: false, mcpTools: [], subAgentTools: [], + skills: [], + pluginSkills: [], labels: [], }; } +export function emptySkillDraft(): SkillDraft { + return { + name: "", + sourceKind: "oci", + oci: "", + gitUrl: "", + gitCommit: "", + s3Endpoint: "", + s3Bucket: "", + s3Key: "", + s3VersionId: "", + s3Region: "", + path: "", + }; +} + +/** One of kagent's built-in skills, as a row of the form. */ +export function builtinSkillDraft(catalog: BuiltinSkillsCatalog, skill: BuiltinSkill): SkillDraft { + return { ...emptySkillDraft(), name: skill.name, oci: catalog.image, path: skill.path }; +} + +/** + * The built-in skill a row holds: the catalog image's repository and a catalog path. + * `current` is false when the row pins another digest, as a row saved before a + * chart upgrade does. + */ +export function matchBuiltinSkill( + draft: SkillDraft, + catalog: BuiltinSkillsCatalog | undefined, +): { skill: BuiltinSkill; current: boolean } | undefined { + if (!catalog || draft.sourceKind !== "oci") return undefined; + const image = draft.oci.trim(); + if (imageRepository(image) !== imageRepository(catalog.image)) return undefined; + // The name must match too: a user may point their own skill at a catalog path under + // another name, and that row must keep its editable name and source. + const skill = catalog.skills.find( + (entry) => entry.path === draft.path.trim() && entry.name === draft.name.trim(), + ); + return skill && { skill, current: image === catalog.image }; +} + +function imageRepository(image: string): string { + return image.split("@")[0]; +} + /** The draft a form opens with when editing an existing template. */ export function draftFromTemplate(template: AgentTemplate): AgentTemplateDraft { return { @@ -145,6 +229,8 @@ export function draftFromSpec(spec: AgentTemplateSpec, namespace: string): Agent description: binding.subAgent?.description ?? "", refName: binding.subAgent?.templateRef.name ?? "", })), + skills: (spec.skills ?? []).map(skillDraftFromSpec), + pluginSkills: (spec.plugins ?? []).flatMap((plugin) => plugin.skills ?? []), labels: [], }; } @@ -153,8 +239,8 @@ export function draftFromSpec(spec: AgentTemplateSpec, namespace: string): Agent * The spec a draft describes, merged onto the one it came from. * * `existing` is the whole reason this takes an argument. Omit it and every field - * this form does not model — `skills`, `plugins`, `promptTemplate` — is dropped from - * the resource, which the API accepts happily and the reader never sees. + * this form does not model — `plugins`, `promptTemplate` — is dropped from the + * resource, which the API accepts happily and the reader never sees. */ export function specFromDraft( draft: AgentTemplateDraft, @@ -241,6 +327,10 @@ export function specFromDraft( if (tools.length > 0) spec.tools = tools; else delete spec.tools; + const skills = draft.skills.filter((skill) => !isBlankSkill(skill)).map(skillFromDraft); + if (skills.length > 0) spec.skills = skills; + else delete spec.skills; + return spec; } @@ -310,9 +400,139 @@ export function draftProblems( ); } } + problems.push(...skillProblems(draft.skills, draft.pluginSkills)); return problems; } +/** Mirrors the CRD's `AgentTemplateSkill` and `ArtifactSource` validation. */ +const MAX_SKILLS = 50; +const MAX_PATH_LENGTH = 1024; +const OCI_DIGEST_REF = /^[^\s@]+@sha256:[0-9a-f]{64}$/; +const HTTP_URL = /^https?:\/\/\S+$/; +const GIT_COMMIT = /^([0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/; + +/** The repository part of an image reference, split from the host as go-containerregistry's `name.NewRepository` does. */ +function ociRepository(ref: string): string { + const name = ref.split("@")[0]; + const [host, ...rest] = name.split("/"); + return rest.length > 0 && (host === "localhost" || /[.:]/.test(host)) ? rest.join("/") : name; +} + +function skillProblems(drafts: SkillDraft[], pluginSkills: string[]): string[] { + const problems: string[] = []; + if (drafts.filter((skill) => !isBlankSkill(skill)).length > MAX_SKILLS) { + problems.push(`A template can have at most ${MAX_SKILLS} skills.`); + } + + const seen = new Set(); + const repeated = new Set(); + const fromPlugins = new Set(); + drafts.forEach((draft, index) => { + if (isBlankSkill(draft)) return; + const skill = skillFromDraft(draft); + const label = skill.name ? `The skill "${skill.name}"` : `Skill ${index + 1}`; + if (!skill.name) problems.push(`${label} needs a name.`); + else if (skill.name === "." || skill.name === ".." || /[/\\]/.test(skill.name)) { + problems.push(`${label} needs a name that is one path segment: not "." or "..", and without "/" or "\\".`); + } else if (seen.has(skill.name)) repeated.add(skill.name); + else if (pluginSkills.includes(skill.name)) fromPlugins.add(skill.name); + seen.add(skill.name); + + const { source } = skill; + if (draft.sourceKind === "oci" && !OCI_DIGEST_REF.test(source.oci ?? "")) { + problems.push(`${label} needs an OCI image pinned by digest, like ghcr.io/org/skills@sha256:<64 hex characters>.`); + } else if (draft.sourceKind === "oci" && /[A-Z]/.test(ociRepository(source.oci ?? ""))) { + problems.push(`${label} needs an image path in lowercase, like ghcr.io/org/skills.`); + } + if (draft.sourceKind === "git") { + if (!HTTP_URL.test(source.git?.url ?? "")) { + problems.push(`${label} needs a Git URL that starts with http:// or https://.`); + } + if (!GIT_COMMIT.test(source.git?.commit ?? "")) { + problems.push(`${label} needs a full Git commit ID: 40 or 64 hex characters.`); + } + } + if (draft.sourceKind === "s3") { + const s3 = source.bucket?.s3; + if (!HTTP_URL.test(s3?.endpoint ?? "")) { + problems.push(`${label} needs an S3 endpoint that starts with http:// or https://.`); + } + const missing = [ + s3?.bucket ? "" : "bucket", + s3?.key ? "" : "key", + s3?.versionId ? "" : "version ID", + ].filter(Boolean); + if (missing.length > 0) { + const fields = missing.length === 1 ? missing[0] : `${missing.slice(0, -1).join(", ")} and ${missing.at(-1)}`; + problems.push(`${label} needs an S3 ${fields}.`); + } + } + const path = source.path ?? ""; + if (path.startsWith("/") || path.split("/").includes("..")) { + problems.push(`${label} needs a path relative to the artifact, without ".." segments.`); + } + if (path.length > MAX_PATH_LENGTH) { + problems.push(`${label} needs a path of at most ${MAX_PATH_LENGTH} characters.`); + } + }); + for (const name of repeated) { + problems.push(`More than one skill is named "${name}". Skill names must be unique.`); + } + for (const name of fromPlugins) { + problems.push(`A plugin on this template already selects a skill named "${name}". Skill names must be unique.`); + } + return problems; +} + +function skillDraftFromSpec(skill: AgentTemplateSkill): SkillDraft { + const { source } = skill; + const s3 = source.bucket?.s3; + return { + name: skill.name, + sourceKind: source.git ? "git" : s3 ? "s3" : "oci", + oci: source.oci ?? "", + gitUrl: source.git?.url ?? "", + gitCommit: source.git?.commit ?? "", + s3Endpoint: s3?.endpoint ?? "", + s3Bucket: s3?.bucket ?? "", + s3Key: s3?.key ?? "", + s3VersionId: s3?.versionId ?? "", + s3Region: s3?.region ?? "", + path: source.path ?? "", + }; +} + +function skillFromDraft(draft: SkillDraft): AgentTemplateSkill { + let source: ArtifactSource; + if (draft.sourceKind === "git") { + source = { git: { url: draft.gitUrl.trim(), commit: draft.gitCommit.trim() } }; + } else if (draft.sourceKind === "s3") { + const region = draft.s3Region.trim(); + source = { + bucket: { + s3: { + endpoint: draft.s3Endpoint.trim(), + bucket: draft.s3Bucket.trim(), + key: draft.s3Key.trim(), + versionId: draft.s3VersionId.trim(), + ...(region ? { region } : {}), + }, + }, + }; + } else { + source = { oci: draft.oci.trim() }; + } + const path = draft.path.trim(); + if (path) source.path = path; + return { name: draft.name.trim(), source }; +} + +/** A row the reader added and never filled in, which a save drops. */ +function isBlankSkill(draft: SkillDraft): boolean { + const fields: SkillTextField[] = ["name", "path", ...SKILL_SOURCE_FIELDS[draft.sourceKind]]; + return fields.every((field) => draft[field].trim() === ""); +} + /** Keep local manifests concise while preserving shared resource identity, group and kind. */ function normalizedReference(ref: ObjectReference, ownerNamespace: string): ObjectReference { const name = ref.name.trim(); diff --git a/ui/src/components/agent-template-form/unshownFields.ts b/ui/src/components/agent-template-form/unshownFields.ts index ef6fc8a1fe..7630733e2b 100644 --- a/ui/src/components/agent-template-form/unshownFields.ts +++ b/ui/src/components/agent-template-form/unshownFields.ts @@ -3,22 +3,16 @@ import type { AgentTemplateSpec } from "@/api/domain/agentTemplates"; /** * Whether a template carries spec fields the form does not author. * - * `skills`, `plugins` and `promptTemplate` are not on the form — each is a rich - * shape of its own, with three artifact-source variants and strict CEL patterns — - * and an edit deliberately carries them through untouched rather than dropping - * them. + * `plugins` and `promptTemplate` are not on the form, and an edit deliberately + * carries them through untouched rather than dropping them. * * This exists so the form can *say* that. A reader who knows their template has - * skills and cannot see them would reasonably conclude a save will remove them, + * plugins and cannot see them would reasonably conclude a save will remove them, * and the only way to know otherwise is to be told. * * In its own module rather than beside the component so the form file exports a * component and nothing else — which is what keeps fast refresh working on it. */ export function hasUnshownSpecFields(spec: AgentTemplateSpec): boolean { - return Boolean( - (spec.skills?.length ?? 0) > 0 || - (spec.plugins?.length ?? 0) > 0 || - spec.promptTemplate, - ); + return Boolean((spec.plugins?.length ?? 0) > 0 || spec.promptTemplate); } diff --git a/ui/src/env.ts b/ui/src/env.ts index 2a764393db..edb85d56f4 100644 --- a/ui/src/env.ts +++ b/ui/src/env.ts @@ -26,6 +26,8 @@ export interface EnvironmentVariables { ENABLE_MOCK_UI: string; /** Public path prefix the UI is served under, e.g. `/ui`; empty at the root. */ BASE_PATH: string; + /** JSON naming the skills image and the skills in it; read through `runtimeConfig`. */ + BUILTIN_SKILLS: string; } /** @@ -46,6 +48,7 @@ export const CORE_ENV_VARS = { STREAM_TIMEOUT_MS: "KAGENT_UI_STREAM_TIMEOUT_MS", ENABLE_MOCK_UI: "KAGENT_UI_ENABLE_MOCK", BASE_PATH: "KAGENT_UI_BASE_PATH", + BUILTIN_SKILLS: "KAGENT_UI_BUILTIN_SKILLS", } as const satisfies Record; /** @@ -59,6 +62,7 @@ export const ENV_DEFAULTS: EnvironmentVariables = { STREAM_TIMEOUT_MS: "1800000", ENABLE_MOCK_UI: "false", BASE_PATH: "", + BUILTIN_SKILLS: "", }; declare global { diff --git a/ui/src/mocks/fixtures.ts b/ui/src/mocks/fixtures.ts index e16a6a12b7..315e1f2cad 100644 --- a/ui/src/mocks/fixtures.ts +++ b/ui/src/mocks/fixtures.ts @@ -27,6 +27,7 @@ import type { Agent, AgentSpec, AgentStatus } from "@/api/domain/agents"; import type { AgentTemplate } from "@/api/domain/agentTemplates"; import type { EgressPolicy, EgressPolicySpec } from "@/api/domain/egressPolicies"; import type { SandboxTemplate } from "@/api/domain/sandboxTemplates"; +import type { BuiltinSkillsCatalog } from "@/api/runtimeConfig"; export const mockModels: ModelConfig[] = [ { @@ -718,6 +719,43 @@ export const mockHarnesses: Harness[] = [ ]; +/** Served as `BUILTIN_SKILLS`, which a deployment gets from the chart. */ +export const mockBuiltinSkills: BuiltinSkillsCatalog = { + image: "ghcr.io/kagent-dev/kagent/tools@sha256:0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0", + skills: [ + { + name: "argo-rollouts-progressive-delivery", + path: "opt/kagent-tools-skills/skills/argo-rollouts-progressive-delivery", + description: "Operate Argo Rollouts canary and blue-green releases.", + }, + { + name: "helm-release-management", + path: "opt/kagent-tools-skills/skills/helm-release-management", + description: "Inspect, upgrade and uninstall Helm releases.", + }, + { + name: "istio-traffic-debugging", + path: "opt/kagent-tools-skills/skills/istio-traffic-debugging", + description: "Debug Istio service mesh traffic: 503s, mTLS failures, routing not applied.", + }, + { + name: "k8s-troubleshooting", + path: "opt/kagent-tools-skills/skills/k8s-troubleshooting", + description: "Diagnose unhealthy Kubernetes workloads: CrashLoopBackOff, Pending, ImagePullBackOff.", + }, + { + name: "kubescape-security-posture", + path: "opt/kagent-tools-skills/skills/kubescape-security-posture", + description: "Review cluster security posture with Kubescape scan results.", + }, + { + name: "prometheus-observability", + path: "opt/kagent-tools-skills/skills/prometheus-observability", + description: "Answer questions about cluster and application metrics with Prometheus queries.", + }, + ], +}; + export const mockAgentTemplates: AgentTemplate[] = [ { ref: "kagent/k8s-agent-7f3a91c", @@ -743,16 +781,33 @@ export const mockAgentTemplates: AgentTemplate[] = [ }, }, ], - // Not authored by the form, and here on purpose: an edit that dropped it - // would be invisible on screen, so this is what the round-trip test guards. skills: [ + { + name: "k8s-troubleshooting", + source: { oci: mockBuiltinSkills.image, path: "opt/kagent-tools-skills/skills/k8s-troubleshooting" }, + }, { name: "incident-review", source: { - oci: "ghcr.io/kagent-dev/skills@sha256:0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0", + git: { + url: "https://github.com/example/runbooks.git", + commit: "3f1c9d2e5b7a48e0a1c6d9f2b4e8a7c30d5f6e9b", + }, + path: "skills/incident-review", + }, + }, + // Saved before an upgrade changed the kagent-tools image's digest. + { + name: "helm-release-management", + source: { + oci: "ghcr.io/kagent-dev/kagent/tools@sha256:1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f809", + path: "opt/kagent-tools-skills/skills/helm-release-management", }, }, ], + // Not authored by the form, and here on purpose: an edit that dropped it + // would be invisible on screen. + promptTemplate: { dataSources: [{ name: "kagent-builtin-prompts", alias: "builtin" }] }, }, }, }, diff --git a/ui/src/mocks/startMockBackend.ts b/ui/src/mocks/startMockBackend.ts index 335e61ac7b..c677857318 100644 --- a/ui/src/mocks/startMockBackend.ts +++ b/ui/src/mocks/startMockBackend.ts @@ -17,6 +17,7 @@ import { MOCK_API_BASE_URL } from "@/api/config"; import { setApiTransport } from "@/api/transport"; import { mockTransport } from "./transport"; +import { mockBuiltinSkills } from "./fixtures"; import { AUTH_SCENARIOS, AUTH_SCENARIO_PARAM, @@ -30,6 +31,13 @@ import { export async function startMockBackend(): Promise { setApiTransport(mockTransport); + if (!window.environmentVariables?.BUILTIN_SKILLS) { + window.environmentVariables = { + ...window.environmentVariables, + BUILTIN_SKILLS: JSON.stringify(mockBuiltinSkills), + }; + } + const { worker } = await import("./browser"); await worker.start({ diff --git a/ui/src/pages/AgentTemplateDetailsPage.tsx b/ui/src/pages/AgentTemplateDetailsPage.tsx index 4278d2b908..edc5af0b86 100644 --- a/ui/src/pages/AgentTemplateDetailsPage.tsx +++ b/ui/src/pages/AgentTemplateDetailsPage.tsx @@ -146,8 +146,8 @@ export function AgentTemplateDetailsPage() { namespace: template.data.namespace, labels: labelsFromDraft(draft), }, - // Merged onto the spec that was read, so skills, plugins and prompt data - // sources survive an edit that never mentioned them. + // Merged onto the spec that was read, so plugins and prompt data sources + // survive an edit that never mentioned them. spec: specFromDraft(draft, template.data.resource.spec), }, });