Skip to content

feat: AI agents #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/v1/playbook_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (t *AIActionClient) Populate(ctx context.Context) error {
if err != nil {
return err
} else if conn == nil {
return fmt.Errorf("connection(%s) was not found: %w", *t.Connection, err)
return fmt.Errorf("connection(%s) was not found", *t.Connection)
}

if err := t.APIKey.Scan(conn.Password); err != nil {
Expand Down Expand Up @@ -385,6 +385,8 @@ type AIAction struct {
// When enabled, the prompt is simply saved without passing it on to the LLM.
DryRun bool `json:"dryRun,omitempty"`

PlaybookAgents []types.ResourceSelector `json:"playbookAgents,omitempty"`

// Specify selectors for playbooks. The LLM will recommend the best suited playbooks
// in response to the prompt.
RecommendPlaybooks []types.ResourceSelector `json:"recommendPlaybooks,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions config/crds/mission-control.flanksource.com_playbooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,66 @@ spec:
Model name based on the backend chosen.
Example: gpt-4o for openai, claude-3-5-sonnet-latest for Anthropic, llama3.1:8b for Ollama
type: string
playbookAgents:
items:
properties:
agent:
description: |-
Agent can be the agent id or the name of the agent.
Additionally, the special "self" value can be used to select resources without an agent.
type: string
cache:
description: |-
Cache directives
'no-cache' (should not fetch from cache but can be cached)
'no-store' (should not cache)
'max-age=X' (cache for X duration)
type: string
fieldSelector:
type: string
health:
description: |-
Health filters resources by the health.
Multiple healths can be provided separated by comma.
type: string
id:
type: string
includeDeleted:
type: boolean
labelSelector:
type: string
limit:
type: integer
name:
type: string
namespace:
type: string
scope:
description: |-
Scope is the reference for parent of the resource to select.
For config items, the scope is the scraper id
For checks, it's canaries and
For components, it's topology.
It can either be a uuid or namespace/name
type: string
search:
description: Search query that applies to the resource
name, tag & labels.
type: string
statuses:
description: Statuses filter resources by the status
items:
type: string
type: array
tagSelector:
type: string
types:
description: Types filter resources by the type
items:
type: string
type: array
type: object
type: array
prompt:
description: Prompt is the human prompt
type: string
Expand Down
6 changes: 6 additions & 0 deletions config/schemas/playbook-spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"dryRun": {
"type": "boolean"
},
"playbookAgents": {
"items": {
"$ref": "#/$defs/ResourceSelector"
},
"type": "array"
},
"recommendPlaybooks": {
"items": {
"$ref": "#/$defs/ResourceSelector"
Expand Down
6 changes: 6 additions & 0 deletions config/schemas/playbook.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"dryRun": {
"type": "boolean"
},
"playbookAgents": {
"items": {
"$ref": "#/$defs/ResourceSelector"
},
"type": "array"
},
"recommendPlaybooks": {
"items": {
"$ref": "#/$defs/ResourceSelector"
Expand Down
13 changes: 13 additions & 0 deletions events/event_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"gorm.io/gorm/clause"
)

// events received on this channel are saved to DB.
// NOTE: Not sure about this one. will probably remove it.
var EventChan = make(chan models.Event)

type asyncHandlerData struct {
fn func(ctx context.Context, e models.Events) models.Events
batchSize int
Expand Down Expand Up @@ -67,6 +71,15 @@ func StartConsumers(ctx context.Context) {
for _, fn := range registers {
fn(ctx)
}

go func() {
for e := range EventChan {
if err := ctx.DB().Create(&e).Error; err != nil {
log.Errorf("failed to create event: %w", err)
}
}
}()

// We listen to all PG Notifications on one channel and distribute it to other consumers
// based on the events.
notifyRouter := pg.NewNotifyRouter()
Expand Down
47 changes: 47 additions & 0 deletions fixtures/playbooks/ai/ai-agents.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: use-agents
spec:
description: Use AI to diagnose unhealthy config items and send a notification slack
category: AI
configs:
- name: '*'
parameters:
- name: prompt
label: Prompt
default: Find out why {{.config.name}} is unhealthy and report in one short sentence.
properties:
multiline: 'true'
actions:
- name: query LLM
ai:
connection: connection://mc/anthropic
playbookAgents:
- namespace: mc
systemPrompt: |
**Role:** Seasoned Kubernetes engineer and Diagnostic Expert

**Objective:** Assist users in diagnosing issues with unhealthy Kubernetes resources by analyzing provided manifests and related resources.

**Instructions:**

1. **Resource Analysis:** Examine the manifest of the unhealthy resource thoroughly.
2. **Contextual Investigation:** Consider additional related resources provided (e.g., pods, replica sets, namespaces) to gain a comprehensive understanding of the issue.
4. **One-Time Diagnosis:** Aim to diagnose the issue in a single response without requiring follow-up questions.
prompt: '{{.params.prompt}}'
changes:
since: 2d
analysis:
since: 2d
relationships:
- depth: 3
direction: outgoing
changes:
since: 24h
- depth: 5
direction: incoming
changes:
since: 24h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
---
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: diagnose-aws-resource
spec:
description: Use AI to diagnose AWS resources
category: AI
configs:
- types:
- AWS::CloudFormation::Stack
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
---
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: diagnose-kubernetes-resource
spec:
description: Use AI to diagnose unhealthy kubernetes resources
category: AI
configs:
- types:
- Kubernetes::DaemonSet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: diagnose-and-send-to-slack
spec:
description: Use AI to diagnose unhealthy config items and send a notification slack
category: AI
configs:
- health: unhealthy,warning
- health: unhealthy,warning,unknown,healthy
parameters:
- name: prompt
label: Prompt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
# yaml-language-server: $schema=../../config/schemas/playbook.schema.json
# yaml-language-server: $schema=../../../config/schemas/playbook.schema.json
apiVersion: mission-control.flanksource.com/v1
kind: Playbook
metadata:
name: recommend-playbook
spec:
description: Use AI to diagnose unhealthy config items and send a notification slack
configs:
- health: unhealthy,warning
- health: unhealthy,warning,unknown,healthy
category: AI
parameters:
- name: prompt
label: Prompt
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ require (
dario.cat/mergo v1.0.0 // indirect
github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962 // indirect
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/RaveNoX/go-jsonmerge v1.0.0 // indirect
Expand Down Expand Up @@ -167,13 +168,15 @@ require (
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/goph/emperror v0.17.2 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hairyhenderson/yaml v0.0.0-20220618171115-2d35fca545ce // indirect
github.com/hashicorp/hcl/v2 v2.21.0 // indirect
github.com/hirochachacha/go-smb2 v1.1.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/invopop/jsonschema v0.12.0 // indirect
github.com/itchyny/gojq v0.12.17 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
Expand Down Expand Up @@ -204,10 +207,12 @@ require (
github.com/moby/spdystream v0.4.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/nikolalohinski/gonja v1.5.3 // indirect
github.com/ohler55/ojg v1.25.0 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/sftp v1.13.6 // indirect
Expand All @@ -223,10 +228,12 @@ require (
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260 // indirect
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand All @@ -243,6 +250,7 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yargevad/filepathx v1.0.0 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
Expand All @@ -251,6 +259,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.10.0 // indirect
Expand Down
Loading
Loading