@clduab11/gemini-flow
Version:
Revolutionary AI agent swarm coordination platform with Google Services integration, multimedia processing, and production-ready monitoring. Features 8 Google AI services, quantum computing capabilities, and enterprise-grade security.
274 lines (230 loc) ⢠9.05 kB
text/typescript
/**
* Memory Command - Persistent Memory Management
*
* Implements cross-session memory operations for gemini-flow
*/
import { Command } from "commander";
import chalk from "chalk";
import ora from "ora";
import { Logger } from "../../utils/logger.js";
interface _MemoryOptions {
namespace?: string;
ttl?: number;
format?: "json" | "text" | "yaml";
}
export class MemoryCommand extends Command {
private logger: Logger;
constructor() {
super("memory");
this.logger = new Logger("Memory");
this.description("Manage persistent memory across sessions").alias("mem");
// Store memory
this.command("store <key> <value>")
.description("Store a value in persistent memory")
.option("-n, --namespace <namespace>", "Memory namespace", "default")
.option("-t, --ttl <seconds>", "Time to live in seconds", parseInt)
.option("--json", "Parse value as JSON")
.action(async (key, value, options) => this.store(key, value, options));
// Query memory
this.command("query <pattern>")
.description("Query memory by pattern")
.option("-n, --namespace <namespace>", "Memory namespace")
.option("-l, --limit <number>", "Limit results", parseInt, 10)
.action(async (pattern, options) => this.query(pattern, options));
// List memories
this.command("list")
.description("List all memories")
.option("-n, --namespace <namespace>", "Filter by namespace")
.option("--stats", "Show memory statistics")
.action(async (options) => this.list(options));
// Export memory
this.command("export [filename]")
.description("Export memory to file")
.option("-f, --format <format>", "Export format (json|yaml|csv)", "json")
.option("-n, --namespace <namespace>", "Export specific namespace")
.action(async (filename, options) => this.export(filename, options));
// Import memory
this.command("import <filename>")
.description("Import memory from file")
.option("--merge", "Merge with existing memories", false)
.action(async (filename, options) => this.import(filename, options));
// Clear memory
this.command("clear")
.description("Clear memory")
.option("-n, --namespace <namespace>", "Clear specific namespace")
.option("--all", "Clear all namespaces")
.option("--force", "Skip confirmation")
.action(async (options) => this.clear(options));
}
private async store(key: string, value: string, options: any): Promise<void> {
try {
const parsedValue = options.json ? JSON.parse(value) : value;
this.logger.info("Storing memory", {
key,
namespace: options.namespace,
ttl: options.ttl,
});
// Simulate storage
await new Promise((resolve) => setTimeout(resolve, 500));
console.log(chalk.green("ā
Memory stored successfully"));
console.log(chalk.gray(" Key:"), key);
console.log(chalk.gray(" Namespace:"), options.namespace || "default");
if (options.ttl) {
console.log(chalk.gray(" Expires in:"), options.ttl + " seconds");
}
} catch (error) {
console.error(chalk.red("Failed to store memory:"), error.message);
process.exit(1);
}
}
private async query(pattern: string, options: any): Promise<void> {
const spinner = ora("Querying memory...").start();
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
spinner.succeed("Query completed");
console.log(chalk.blue("\nš Memory Query Results:"));
console.log(chalk.gray("Pattern:"), pattern);
// Simulated results
const results = [
{
key: "project/config",
value: '{"name":"gemini-flow","version":"2.0.0"}',
namespace: "default",
},
{
key: "agent/last-task",
value: "Code review completed",
namespace: "swarm",
},
{
key: "performance/metrics",
value: '{"latency":127,"throughput":2341}',
namespace: "monitoring",
},
];
const filtered =
pattern === "*"
? results
: results.filter((r) => r.key.includes(pattern.replace("*", "")));
console.log(chalk.gray("Found:"), filtered.length + " matches\n");
filtered.slice(0, options.limit || 10).forEach((result, i) => {
console.log(chalk.yellow(`${i + 1}.`), chalk.cyan(result.key));
console.log(chalk.gray(" Namespace:"), result.namespace);
console.log(
chalk.gray(" Value:"),
result.value.substring(0, 100) + "...",
);
console.log("");
});
} catch (error) {
spinner.fail("Query failed");
console.error(chalk.red("Error:"), error.message);
process.exit(1);
}
}
private async list(options: any): Promise<void> {
console.log(chalk.blue("\nš Memory Store:\n"));
if (options.stats) {
console.log(chalk.yellow("Statistics:"));
console.log(chalk.gray(" Total entries:"), "1,247");
console.log(chalk.gray(" Namespaces:"), "12");
console.log(chalk.gray(" Storage used:"), "4.7 MB");
console.log(chalk.gray(" Oldest entry:"), "7 days ago");
console.log("");
}
// Simulated namespace listing
const namespaces = [
{ name: "default", entries: 156, size: "1.2 MB" },
{ name: "swarm", entries: 423, size: "2.1 MB" },
{ name: "agents", entries: 234, size: "0.8 MB" },
{ name: "tasks", entries: 189, size: "0.4 MB" },
{ name: "monitoring", entries: 245, size: "0.2 MB" },
];
const filtered = options.namespace
? namespaces.filter((n) => n.name === options.namespace)
: namespaces;
console.log(chalk.yellow("Namespaces:"));
filtered.forEach((ns) => {
console.log(chalk.cyan(` ${ns.name}:`));
console.log(chalk.gray(" Entries:"), ns.entries);
console.log(chalk.gray(" Size:"), ns.size);
});
}
private async export(filename?: string, options?: any): Promise<void> {
const outputFile =
filename || `memory-export-${Date.now()}.${options?.format || "json"}`;
const spinner = ora("Exporting memory...").start();
try {
spinner.text = "Gathering memory entries...";
await new Promise((resolve) => setTimeout(resolve, 1500));
spinner.text = "Writing to file...";
await new Promise((resolve) => setTimeout(resolve, 1000));
spinner.succeed("Memory exported successfully");
console.log(chalk.green("\nā
Export complete:"));
console.log(chalk.gray(" File:"), outputFile);
console.log(chalk.gray(" Format:"), options?.format || "json");
console.log(chalk.gray(" Entries:"), "1,247");
console.log(chalk.gray(" Size:"), "4.7 MB");
} catch (error) {
spinner.fail("Export failed");
console.error(chalk.red("Error:"), error.message);
process.exit(1);
}
}
private async import(filename: string, options: any): Promise<void> {
const spinner = ora("Importing memory...").start();
try {
spinner.text = "Reading file...";
await new Promise((resolve) => setTimeout(resolve, 1000));
spinner.text = "Validating entries...";
await new Promise((resolve) => setTimeout(resolve, 1500));
spinner.text = options.merge
? "Merging with existing..."
: "Importing entries...";
await new Promise((resolve) => setTimeout(resolve, 2000));
spinner.succeed("Memory imported successfully");
console.log(chalk.green("\nā
Import complete:"));
console.log(chalk.gray(" File:"), filename);
console.log(chalk.gray(" Imported:"), "847 entries");
console.log(chalk.gray(" Skipped:"), "23 duplicates");
console.log(chalk.gray(" Mode:"), options.merge ? "Merge" : "Overwrite");
} catch (error) {
spinner.fail("Import failed");
console.error(chalk.red("Error:"), error.message);
process.exit(1);
}
}
private async clear(options: any): Promise<void> {
if (!options.force) {
console.log(
chalk.yellow(
"\nā ļø Warning: This will permanently delete memory entries",
),
);
console.log(chalk.gray("Use --force to skip this confirmation"));
return;
}
const spinner = ora("Clearing memory...").start();
try {
await new Promise((resolve) => setTimeout(resolve, 1500));
spinner.succeed("Memory cleared");
console.log(chalk.green("\nā
Memory cleared:"));
console.log(
chalk.gray(" Scope:"),
options.all
? "All namespaces"
: options.namespace || "default namespace",
);
console.log(
chalk.gray(" Entries removed:"),
options.all ? "1,247" : "156",
);
} catch (error) {
spinner.fail("Clear operation failed");
console.error(chalk.red("Error:"), error.message);
process.exit(1);
}
}
}
// Export for use in main CLI
export default MemoryCommand;