|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Command-line interface for agent-memory-server. |
| 4 | +""" |
| 5 | + |
| 6 | +import datetime |
| 7 | +import importlib |
| 8 | +import sys |
| 9 | + |
| 10 | +import click |
| 11 | +import uvicorn |
| 12 | + |
| 13 | +from agent_memory_server.config import settings |
| 14 | +from agent_memory_server.logging import configure_logging, get_logger |
| 15 | +from agent_memory_server.utils.redis import ensure_search_index_exists, get_redis_conn |
| 16 | + |
| 17 | + |
| 18 | +configure_logging() |
| 19 | +logger = get_logger(__name__) |
| 20 | + |
| 21 | +VERSION = "0.2.0" |
| 22 | + |
| 23 | + |
| 24 | +@click.group() |
| 25 | +def cli(): |
| 26 | + """Command-line interface for agent-memory-server.""" |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +@cli.command() |
| 31 | +def version(): |
| 32 | + """Show the version of agent-memory-server.""" |
| 33 | + click.echo(f"agent-memory-server version {VERSION}") |
| 34 | + |
| 35 | + |
| 36 | +@cli.command() |
| 37 | +@click.option("--port", default=settings.port, help="Port to run the server on") |
| 38 | +@click.option("--host", default="0.0.0.0", help="Host to run the server on") |
| 39 | +@click.option("--reload", is_flag=True, help="Enable auto-reload") |
| 40 | +def api(port: int, host: str, reload: bool): |
| 41 | + """Run the REST API server.""" |
| 42 | + import asyncio |
| 43 | + |
| 44 | + from agent_memory_server.main import app, on_start_logger |
| 45 | + |
| 46 | + async def setup_redis(): |
| 47 | + redis = await get_redis_conn() |
| 48 | + await ensure_search_index_exists(redis) |
| 49 | + |
| 50 | + # Run the async setup |
| 51 | + asyncio.run(setup_redis()) |
| 52 | + |
| 53 | + on_start_logger(port) |
| 54 | + uvicorn.run( |
| 55 | + app, |
| 56 | + host=host, |
| 57 | + port=port, |
| 58 | + reload=reload, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +@cli.command() |
| 63 | +@click.option("--port", default=settings.mcp_port, help="Port to run the MCP server on") |
| 64 | +@click.option("--sse", is_flag=True, help="Run the MCP server in SSE mode") |
| 65 | +def mcp(port: int, sse: bool): |
| 66 | + """Run the MCP server.""" |
| 67 | + import asyncio |
| 68 | + |
| 69 | + # Update the port in settings FIRST |
| 70 | + settings.mcp_port = port |
| 71 | + |
| 72 | + # Import mcp_app AFTER settings have been updated |
| 73 | + from agent_memory_server.mcp import mcp_app |
| 74 | + |
| 75 | + async def setup_and_run(): |
| 76 | + redis = await get_redis_conn() |
| 77 | + await ensure_search_index_exists(redis) |
| 78 | + |
| 79 | + # Run the MCP server |
| 80 | + if sse: |
| 81 | + await mcp_app.run_sse_async() |
| 82 | + else: |
| 83 | + await mcp_app.run_stdio_async() |
| 84 | + |
| 85 | + # Update the port in settings |
| 86 | + settings.mcp_port = port |
| 87 | + |
| 88 | + click.echo(f"Starting MCP server on port {port}") |
| 89 | + |
| 90 | + if sse: |
| 91 | + click.echo("Running in SSE mode") |
| 92 | + else: |
| 93 | + click.echo("Running in stdio mode") |
| 94 | + |
| 95 | + asyncio.run(setup_and_run()) |
| 96 | + |
| 97 | + |
| 98 | +@cli.command() |
| 99 | +@click.argument("task_path") |
| 100 | +@click.option( |
| 101 | + "--args", |
| 102 | + "-a", |
| 103 | + multiple=True, |
| 104 | + help="Arguments to pass to the task in the format key=value", |
| 105 | +) |
| 106 | +def schedule_task(task_path: str, args: list[str]): |
| 107 | + """ |
| 108 | + Schedule a background task by path. |
| 109 | +
|
| 110 | + TASK_PATH is the import path to the task function, e.g., |
| 111 | + "agent_memory_server.long_term_memory.compact_long_term_memories" |
| 112 | + """ |
| 113 | + import asyncio |
| 114 | + |
| 115 | + from docket import Docket |
| 116 | + |
| 117 | + # Parse the arguments |
| 118 | + task_args = {} |
| 119 | + for arg in args: |
| 120 | + try: |
| 121 | + key, value = arg.split("=", 1) |
| 122 | + # Try to convert to appropriate type |
| 123 | + if value.lower() == "true": |
| 124 | + task_args[key] = True |
| 125 | + elif value.lower() == "false": |
| 126 | + task_args[key] = False |
| 127 | + elif value.isdigit(): |
| 128 | + task_args[key] = int(value) |
| 129 | + elif value.replace(".", "", 1).isdigit() and value.count(".") <= 1: |
| 130 | + task_args[key] = float(value) |
| 131 | + else: |
| 132 | + task_args[key] = value |
| 133 | + except ValueError: |
| 134 | + click.echo(f"Invalid argument format: {arg}. Use key=value format.") |
| 135 | + sys.exit(1) |
| 136 | + |
| 137 | + async def setup_and_run_task(): |
| 138 | + redis = await get_redis_conn() |
| 139 | + await ensure_search_index_exists(redis) |
| 140 | + |
| 141 | + # Import the task function |
| 142 | + module_path, function_name = task_path.rsplit(".", 1) |
| 143 | + try: |
| 144 | + module = importlib.import_module(module_path) |
| 145 | + task_func = getattr(module, function_name) |
| 146 | + except (ImportError, AttributeError) as e: |
| 147 | + click.echo(f"Error importing task: {e}") |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + # Initialize Docket client |
| 151 | + async with Docket( |
| 152 | + name=settings.docket_name, |
| 153 | + url=settings.redis_url, |
| 154 | + ) as docket: |
| 155 | + click.echo(f"Scheduling task {task_path} with arguments: {task_args}") |
| 156 | + await docket.add(task_func)(**task_args) |
| 157 | + click.echo("Task scheduled successfully") |
| 158 | + |
| 159 | + asyncio.run(setup_and_run_task()) |
| 160 | + |
| 161 | + |
| 162 | +@cli.command() |
| 163 | +@click.option( |
| 164 | + "--concurrency", default=10, help="Number of tasks to process concurrently" |
| 165 | +) |
| 166 | +@click.option( |
| 167 | + "--redelivery-timeout", |
| 168 | + default=30, |
| 169 | + help="Seconds to wait before redelivering a task to another worker", |
| 170 | +) |
| 171 | +def task_worker(concurrency: int, redelivery_timeout: int): |
| 172 | + """ |
| 173 | + Start a Docket worker using the Docket name from settings. |
| 174 | +
|
| 175 | + This command starts a worker that processes background tasks registered |
| 176 | + with Docket. The worker uses the Docket name from settings. |
| 177 | + """ |
| 178 | + import asyncio |
| 179 | + |
| 180 | + from docket import Worker |
| 181 | + |
| 182 | + if not settings.use_docket: |
| 183 | + click.echo("Docket is disabled in settings. Cannot run worker.") |
| 184 | + sys.exit(1) |
| 185 | + |
| 186 | + asyncio.run( |
| 187 | + Worker.run( |
| 188 | + docket_name=settings.docket_name, |
| 189 | + url=settings.redis_url, |
| 190 | + concurrency=concurrency, |
| 191 | + redelivery_timeout=datetime.timedelta(seconds=redelivery_timeout), |
| 192 | + tasks=["agent_memory_server.docket_tasks:task_collection"], |
| 193 | + ) |
| 194 | + ) |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + cli() |
0 commit comments