File size: 4,032 Bytes
0ae3f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Config management commands: show, set, get."""

from __future__ import annotations

from rich.console import Console
from rich.table import Table

from mem0_cli.branding import ACCENT_COLOR, BRAND_COLOR, DIM_COLOR, print_error, print_success
from mem0_cli.config import (
    get_nested_value,
    load_config,
    redact_key,
    save_config,
    set_nested_value,
)

console = Console()
err_console = Console(stderr=True)


def cmd_config_show(*, output: str = "text") -> None:
    """Display current configuration (secrets redacted)."""
    from mem0_cli.output import format_agent_envelope
    from mem0_cli.state import is_agent_mode, set_current_command

    set_current_command("config show")
    if is_agent_mode():
        output = "agent"

    config = load_config()

    if output in ("json", "agent"):
        format_agent_envelope(
            console,
            command="config show",
            data={
                "defaults": {
                    "user_id": config.defaults.user_id or None,
                    "agent_id": config.defaults.agent_id or None,
                    "app_id": config.defaults.app_id or None,
                    "run_id": config.defaults.run_id or None,
                    "enable_graph": config.defaults.enable_graph,
                },
                "platform": {
                    "api_key": redact_key(config.platform.api_key),
                    "base_url": config.platform.base_url,
                },
            },
        )
        return

    console.print()
    console.print(f"  [{BRAND_COLOR}]◆ mem0 Configuration[/]\n")

    table = Table(border_style=BRAND_COLOR, header_style=f"bold {ACCENT_COLOR}", padding=(0, 2))
    table.add_column("Key", style="bold")
    table.add_column("Value")

    # Defaults
    table.add_row(
        "defaults.user_id",
        config.defaults.user_id or f"[{DIM_COLOR}](not set)[/]",
    )
    table.add_row(
        "defaults.agent_id",
        config.defaults.agent_id or f"[{DIM_COLOR}](not set)[/]",
    )
    table.add_row(
        "defaults.app_id",
        config.defaults.app_id or f"[{DIM_COLOR}](not set)[/]",
    )
    table.add_row(
        "defaults.run_id",
        config.defaults.run_id or f"[{DIM_COLOR}](not set)[/]",
    )
    table.add_row(
        "defaults.enable_graph",
        str(config.defaults.enable_graph).lower(),
    )
    table.add_row("", "")

    # Platform
    table.add_row("[bold]platform.api_key[/]", redact_key(config.platform.api_key))
    table.add_row("platform.base_url", config.platform.base_url)

    console.print(table)
    console.print()


def cmd_config_get(key: str) -> None:
    """Get a config value."""
    from mem0_cli.output import format_agent_envelope
    from mem0_cli.state import is_agent_mode, set_current_command

    set_current_command("config get")
    config = load_config()
    value = get_nested_value(config, key)

    if value is None:
        print_error(err_console, f"Unknown config key: {key}")
        return

    display_value = (
        redact_key(str(value)) if ("api_key" in key or "key" in key.split(".")[-1:]) else str(value)
    )

    if is_agent_mode():
        format_agent_envelope(
            console, command="config get", data={"key": key, "value": display_value}
        )
    else:
        console.print(display_value)


def cmd_config_set(key: str, value: str) -> None:
    """Set a config value."""
    from mem0_cli.output import format_agent_envelope
    from mem0_cli.state import is_agent_mode, set_current_command

    set_current_command("config set")
    config = load_config()
    if set_nested_value(config, key, value):
        save_config(config)
        display = redact_key(value) if "key" in key else value
        if is_agent_mode():
            format_agent_envelope(
                console, command="config set", data={"key": key, "value": display}
            )
        else:
            print_success(console, f"{key} = {display}")
    else:
        print_error(err_console, f"Unknown config key: {key}")