File size: 6,079 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * Branding and ASCII art for mem0 CLI.
 */

import chalk from "chalk";
import ora, { type Ora } from "ora";
import { getCurrentCommand, isAgentMode } from "./state.js";
import { CLI_VERSION } from "./version.js";

export const LOGO = `
โ–ˆโ–ˆโ–ˆโ•—   โ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ•—   โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—      โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•—     โ–ˆโ–ˆโ•—
โ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ–ˆโ–ˆโ•—    โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•‘     โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ•‘     โ–ˆโ–ˆโ•‘     โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•  โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ•‘     โ–ˆโ–ˆโ•‘     โ–ˆโ–ˆโ•‘
โ–ˆโ–ˆโ•‘ โ•šโ•โ• โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘ โ•šโ•โ• โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•    โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘
โ•šโ•โ•     โ•šโ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•     โ•šโ•โ• โ•šโ•โ•โ•โ•โ•โ•      โ•šโ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•
`;

export const LOGO_MINI = "โ—† mem0";
export const TAGLINE = "The Memory Layer for AI Agents";

export const BRAND_COLOR = "#8b5cf6";
export const ACCENT_COLOR = "#a78bfa";
export const SUCCESS_COLOR = "#22c55e";
export const ERROR_COLOR = "#ef4444";
export const WARNING_COLOR = "#f59e0b";
export const DIM_COLOR = "#6b7280";

const brand = chalk.hex(BRAND_COLOR);
const accent = chalk.hex(ACCENT_COLOR);
const success = chalk.hex(SUCCESS_COLOR);
const error = chalk.hex(ERROR_COLOR);
const warning = chalk.hex(WARNING_COLOR);
const dim = chalk.hex(DIM_COLOR);

/**
 * Choose a symbol based on TTY/NO_COLOR. Fancy for interactive terminals,
 * plain-text for piped/non-TTY or NO_COLOR environments.
 */
export function sym(fancy: string, plain: string): string {
	if (!process.stdout.isTTY || process.env.NO_COLOR) return plain;
	return fancy;
}

export function printBanner(): void {
	if (isAgentMode()) return;
	const pad = 3; // horizontal padding each side (matches Rich's padding=(0, 2))
	const logoLines = LOGO.trimEnd().split("\n");
	const tagline = `  ${TAGLINE}`;
	const subtitle = `Node.js SDK ยท v${CLI_VERSION}`;
	const contentLines = ["", ...logoLines, "", tagline, ""];

	// Compute inner width from longest content line + padding both sides
	const maxContent = Math.max(...contentLines.map((l) => l.length));
	const innerWidth = maxContent + pad * 2;
	const totalWidth = innerWidth + 2; // + 2 for โ”‚ borders

	const topBorder = brand(`โ•ญ${"โ”€".repeat(totalWidth - 2)}โ•ฎ`);
	const subtitleFill = totalWidth - 2 - subtitle.length - 3; // 3 = "โ”€ " before subtitle + "โ”€" after
	const bottomBorder = brand(
		`โ•ฐ${"โ”€".repeat(subtitleFill)} ${dim(subtitle)} ${"โ”€"}โ•ฏ`,
	);

	const body = contentLines.map((line) => {
		const rightPad = innerWidth - pad - line.length;
		return `${brand("โ”‚")}${" ".repeat(pad)}${brand.bold(line)}${" ".repeat(Math.max(rightPad, 0))}${brand("โ”‚")}`;
	});
	// Re-color tagline line with accent instead of brand.bold
	const taglineIdx = body.length - 2; // second-to-last (before trailing empty line)
	const taglineRightPad = innerWidth - pad - tagline.length;
	body[taglineIdx] =
		`${brand("โ”‚")}${" ".repeat(pad)}${accent(tagline)}${" ".repeat(Math.max(taglineRightPad, 0))}${brand("โ”‚")}`;

	console.log(topBorder);
	for (const line of body) console.log(line);
	console.log(bottomBorder);
}

export function printSuccess(message: string): void {
	if (isAgentMode()) return;
	console.log(`${success(sym("โœ“", "[ok]"))} ${message}`);
}

export function printError(message: string, hint?: string): void {
	if (isAgentMode()) {
		const envelope = {
			status: "error",
			command: getCurrentCommand(),
			error: message,
			data: null,
		};
		console.log(JSON.stringify(envelope));
		return;
	}
	console.error(`${error(`${sym("โœ—", "[error]")} Error:`)} ${message}`);
	const resolvedHint =
		hint ??
		(message.includes("Authentication failed")
			? `Run ${brand("mem0 init")} to reconfigure your API key ยท https://app.mem0.ai/dashboard/api-keys`
			: undefined);
	if (resolvedHint) {
		console.error(`  ${dim(resolvedHint)}`);
	}
}

export function printWarning(message: string): void {
	console.error(`${warning(sym("โš ", "[warn]"))} ${message}`);
}

export function printInfo(message: string): void {
	if (isAgentMode()) return;
	console.error(`${brand(sym("โ—†", "*"))} ${message}`);
}

export function printScope(ids: Record<string, string | undefined>): void {
	if (isAgentMode()) return;
	const parts: string[] = [];
	for (const [key, val] of Object.entries(ids)) {
		if (val) {
			parts.push(`${key}=${val}`);
		}
	}
	if (parts.length > 0) {
		console.error(`  ${dim(`Scope: ${parts.join(", ")}`)}`);
	}
}

export interface TimedStatusContext {
	successMsg: string;
	errorMsg: string;
}

/**
 * Run an async function with a spinner, timing the operation.
 * Equivalent to Python's timed_status context manager.
 */
export async function timedStatus<T>(
	message: string,
	fn: (ctx: TimedStatusContext) => Promise<T>,
): Promise<T> {
	if (isAgentMode()) {
		const ctx: TimedStatusContext = { successMsg: "", errorMsg: "" };
		return fn(ctx);
	}
	const ctx: TimedStatusContext = { successMsg: "", errorMsg: "" };
	const spinner = ora({
		text: dim(message),
		color: "yellow",
		stream: process.stderr,
	}).start();
	const start = performance.now();

	try {
		const result = await fn(ctx);
		const elapsed = ((performance.now() - start) / 1000).toFixed(2);
		spinner.stop();
		if (ctx.successMsg) {
			console.error(`${success("โœ“")} ${ctx.successMsg} (${elapsed}s)`);
		}
		return result;
	} catch (err) {
		const elapsed = ((performance.now() - start) / 1000).toFixed(2);
		spinner.stop();
		if (ctx.errorMsg) {
			printError(`${ctx.errorMsg} (${elapsed}s)`);
		}
		throw err;
	}
}

/** Format helpers using brand colors for external use. */
export const colors = { brand, accent, success, error, warning, dim };