Skip to content

Commit deaa481

Browse files
authored
Merge pull request #140 from kagent-dev/cli-helper-commands
Cli helper commands
2 parents b2ea268 + c5d24a4 commit deaa481

File tree

9 files changed

+284
-55
lines changed

9 files changed

+284
-55
lines changed

.github/workflows/lint-python.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
pull_request:
55
paths:
66
- '**/*.py' # Only trigger on changes to Python files
7+
- 'python/pyproject.toml'
8+
- 'python/uv.lock'
79

810
jobs:
911
lint:

go/cli/cmd/kagent/main.go

+27
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,32 @@ Examples:
185185
},
186186
})
187187

188+
shell.AddCmd(&ishell.Cmd{
189+
Name: "install",
190+
Aliases: []string{"i"},
191+
Help: "Install kagent.",
192+
Func: func(c *ishell.Context) {
193+
cli.InstallCmd(ctx, c)
194+
},
195+
})
196+
197+
shell.AddCmd(&ishell.Cmd{
198+
Name: "uninstall",
199+
Aliases: []string{"u"},
200+
Help: "Uninstall kagent.",
201+
Func: func(c *ishell.Context) {
202+
cli.UninstallCmd(ctx, c)
203+
},
204+
})
205+
206+
shell.AddCmd(&ishell.Cmd{
207+
Name: "dashboard",
208+
Aliases: []string{"d"},
209+
Help: "Open the kagent dashboard.",
210+
Func: func(c *ishell.Context) {
211+
cli.DashboardCmd(ctx, c)
212+
},
213+
})
214+
188215
shell.Run()
189216
}

go/cli/internal/cli/dashboard.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"os/exec"
6+
"strings"
7+
"time"
8+
9+
"github.com/abiosoft/ishell/v2"
10+
"github.com/kagent-dev/kagent/go/cli/internal/config"
11+
)
12+
13+
func DashboardCmd(ctx context.Context, c *ishell.Context) {
14+
cfg := config.GetCfg(c)
15+
ctx, cancel := context.WithCancel(ctx)
16+
cmd := exec.CommandContext(ctx, "kubectl", "-n", cfg.Namespace, "port-forward", "service/kagent", "8082:80")
17+
18+
defer func() {
19+
cancel()
20+
if err := cmd.Wait(); err != nil { // These 2 errors are expected
21+
if !strings.Contains(err.Error(), "signal: killed") && !strings.Contains(err.Error(), "exec: not started") {
22+
c.Printf("Error waiting for port-forward to exit: %v\n", err)
23+
}
24+
}
25+
}()
26+
27+
if err := cmd.Start(); err != nil {
28+
c.Println("Error port-forwarding kagent:", err)
29+
return
30+
}
31+
32+
// Wait for the port-forward to start
33+
time.Sleep(1 * time.Second)
34+
35+
// Open the dashboard in the browser
36+
openCmd := exec.CommandContext(ctx, "open", "http://localhost:8082")
37+
if err := openCmd.Run(); err != nil {
38+
c.Printf("Error opening kagent dashboard: %v\n", err)
39+
}
40+
41+
c.Println("kagent dashboard is available at http://localhost:8082")
42+
43+
// This waits for user input to stop the port-forward
44+
c.ShowPrompt(false)
45+
c.Println("Press Enter to stop the port-forward...")
46+
if _, err := c.ReadLineErr(); err != nil {
47+
c.Println("Error reading input:", err)
48+
}
49+
c.ShowPrompt(true)
50+
}

go/cli/internal/cli/install.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/exec"
7+
"time"
8+
9+
"github.com/abiosoft/ishell/v2"
10+
"github.com/briandowns/spinner"
11+
"github.com/kagent-dev/kagent/go/cli/internal/config"
12+
)
13+
14+
func InstallCmd(ctx context.Context, c *ishell.Context) {
15+
cfg := config.GetCfg(c)
16+
17+
if Version == "dev" {
18+
c.Println("Installation requires released version of kagent")
19+
return
20+
}
21+
22+
if os.Getenv("OPENAI_API_KEY") == "" {
23+
c.Println("OPENAI_API_KEY is not set")
24+
c.Println("Please set the OPENAI_API_KEY environment variable")
25+
return
26+
}
27+
28+
args := []string{
29+
"upgrade",
30+
"--install",
31+
"kagent",
32+
"oci://ghcr.io/kagent-dev/kagent/helm/kagent",
33+
"--version",
34+
Version,
35+
"--namespace",
36+
cfg.Namespace,
37+
"--create-namespace",
38+
"--set",
39+
"openai.apiKey=" + os.Getenv("OPENAI_API_KEY"),
40+
}
41+
cmd := exec.CommandContext(ctx, "helm", args...)
42+
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond)
43+
s.Suffix = " Installing kagent"
44+
s.Start()
45+
46+
if byt, err := cmd.CombinedOutput(); err != nil {
47+
s.Stop()
48+
c.Println("Error installing kagent: ", string(byt))
49+
return
50+
}
51+
s.Stop()
52+
c.Println("kagent installed successfully")
53+
}
54+
55+
func UninstallCmd(ctx context.Context, c *ishell.Context) {
56+
cfg := config.GetCfg(c)
57+
args := []string{
58+
"uninstall",
59+
"kagent",
60+
"--namespace",
61+
cfg.Namespace,
62+
}
63+
cmd := exec.CommandContext(ctx, "helm", args...)
64+
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond)
65+
s.Suffix = " Uninstalling kagent"
66+
s.Start()
67+
68+
if err := cmd.Run(); err != nil {
69+
s.Stop()
70+
c.Println("Error uninstalling kagent:", err)
71+
return
72+
}
73+
s.Stop()
74+
c.Println("kagent uninstalled successfully")
75+
}

go/cli/internal/config/config.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

1111
type Config struct {
12-
APIURL string `mapstructure:"api_url"`
13-
WSURL string `mapstructure:"ws_url"`
14-
UserID string `mapstructure:"user_id"`
12+
APIURL string `mapstructure:"api_url"`
13+
WSURL string `mapstructure:"ws_url"`
14+
UserID string `mapstructure:"user_id"`
15+
Namespace string `mapstructure:"namespace"`
1516
}
1617

1718
func Init() error {
@@ -35,6 +36,7 @@ func Init() error {
3536
viper.SetDefault("ws_url", "ws://localhost:8081/api/ws")
3637
viper.SetDefault("user_id", "[email protected]")
3738
viper.SetDefault("output_format", "table")
39+
viper.SetDefault("namespace", "kagent")
3840

3941
if err := viper.ReadInConfig(); err != nil {
4042
// If config file doesn't exist, create it with defaults

helm/templates/deployment.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ spec:
5656
protocol: TCP
5757
resources:
5858
{{- toYaml .Values.app.resources | nindent 12 }}
59+
readinessProbe:
60+
httpGet:
61+
path: /api/version
62+
port: {{ .Values.service.ports.app.targetPort }}
63+
initialDelaySeconds: 3
64+
periodSeconds: 3
5965
- name: ui
6066
securityContext:
6167
{{- toYaml .Values.securityContext | nindent 12 }}

python/pyproject.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ kagent = "kagent.cli:run"
4141
tool_gen = "kagent.tools.utils.tool_gen:main"
4242

4343
[tool.uv.sources]
44-
autogenstudio = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-studio", rev = "24f376323000ee8523cfdcc4fdef072b1c8c04d1" }
45-
autogen-ext = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-ext", rev = "24f376323000ee8523cfdcc4fdef072b1c8c04d1" }
46-
autogen-core = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-core", rev = "24f376323000ee8523cfdcc4fdef072b1c8c04d1" }
44+
autogenstudio = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-studio", rev = "57baad93f587efe26422c0b2c6b16a26af388c6c" }
45+
autogen-agentchat = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-agentchat", rev = "57baad93f587efe26422c0b2c6b16a26af388c6c" }
46+
autogen-ext = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-ext", rev = "57baad93f587efe26422c0b2c6b16a26af388c6c" }
47+
autogen-core = { git = "https://github.com/kagent-dev/autogen.git", subdirectory = "python/packages/autogen-core", rev = "57baad93f587efe26422c0b2c6b16a26af388c6c" }
4748
kagent = { workspace = true }
4849

4950
[tool.ruff]

0 commit comments

Comments
 (0)