File size: 2,957 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
/**
 * Config management commands: show, set, get.
 */

import Table from "cli-table3";
import { colors, printError, printSuccess } from "../branding.js";
import {
	getNestedValue,
	loadConfig,
	redactKey,
	saveConfig,
	setNestedValue,
} from "../config.js";
import { formatAgentEnvelope, formatJsonEnvelope } from "../output.js";
import { isAgentMode, setCurrentCommand } from "../state.js";

const { brand, accent, dim } = colors;

export function cmdConfigShow(opts: { output?: string } = {}): void {
	setCurrentCommand("config show");
	const config = loadConfig();

	if (opts.output === "agent" || opts.output === "json") {
		formatAgentEnvelope({
			command: "config show",
			data: {
				defaults: {
					user_id: config.defaults.userId || null,
					agent_id: config.defaults.agentId || null,
					app_id: config.defaults.appId || null,
					run_id: config.defaults.runId || null,
					enable_graph: config.defaults.enableGraph,
				},
				platform: {
					api_key: redactKey(config.platform.apiKey),
					base_url: config.platform.baseUrl,
				},
			},
		});
		return;
	}

	console.log();
	console.log(`  ${brand("◆ mem0 Configuration")}\n`);

	const table = new Table({
		head: [accent("Key"), accent("Value")],
		style: { head: [], border: [] },
	});

	// Defaults
	table.push(["defaults.user_id", config.defaults.userId || dim("(not set)")]);
	table.push([
		"defaults.agent_id",
		config.defaults.agentId || dim("(not set)"),
	]);
	table.push(["defaults.app_id", config.defaults.appId || dim("(not set)")]);
	table.push(["defaults.run_id", config.defaults.runId || dim("(not set)")]);
	table.push(["defaults.enable_graph", String(config.defaults.enableGraph)]);
	table.push(["", ""]);

	// Platform
	table.push(["platform.api_key", redactKey(config.platform.apiKey)]);
	table.push(["platform.base_url", config.platform.baseUrl]);

	console.log(table.toString());
	console.log();
}

export function cmdConfigGet(key: string): void {
	setCurrentCommand("config get");
	const config = loadConfig();
	const value = getNestedValue(config, key);

	if (value === undefined) {
		printError(`Unknown config key: ${key}`);
	} else {
		// Redact secrets
		const displayValue =
			key.includes("api_key") || key.split(".").pop() === "key"
				? redactKey(String(value))
				: String(value);
		if (isAgentMode()) {
			formatAgentEnvelope({
				command: "config get",
				data: { key, value: displayValue },
			});
		} else {
			console.log(displayValue);
		}
	}
}

export function cmdConfigSet(key: string, value: string): void {
	setCurrentCommand("config set");
	const config = loadConfig();
	if (setNestedValue(config, key, value)) {
		saveConfig(config);
		const display = key.includes("key") ? redactKey(value) : value;
		if (isAgentMode()) {
			formatAgentEnvelope({
				command: "config set",
				data: { key, value: display },
			});
		} else {
			printSuccess(`${key} = ${display}`);
		}
	} else {
		printError(`Unknown config key: ${key}`);
	}
}