main.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import json
  2. from typing import Any
  3. import asyncclick as click
  4. from rich.console import Console
  5. from cli.command_group import cli
  6. from cli.commands import (
  7. collections,
  8. config,
  9. conversations,
  10. database,
  11. documents,
  12. graphs,
  13. indices,
  14. prompts,
  15. retrieval,
  16. system,
  17. users,
  18. )
  19. from cli.utils.telemetry import posthog, telemetry
  20. from r2r import R2RAsyncClient
  21. from .command_group import CONFIG_DIR, CONFIG_FILE, load_config
  22. console = Console()
  23. def add_command_with_telemetry(command):
  24. cli.add_command(telemetry(command))
  25. # Chunks
  26. add_command_with_telemetry(collections.collections)
  27. add_command_with_telemetry(conversations.conversations)
  28. add_command_with_telemetry(documents.documents)
  29. add_command_with_telemetry(graphs.graphs)
  30. # Graph
  31. add_command_with_telemetry(indices.indices)
  32. add_command_with_telemetry(prompts.prompts)
  33. add_command_with_telemetry(retrieval.retrieval)
  34. add_command_with_telemetry(users.users)
  35. add_command_with_telemetry(system.system)
  36. # Database
  37. add_command_with_telemetry(database.db)
  38. add_command_with_telemetry(database.upgrade)
  39. add_command_with_telemetry(database.downgrade)
  40. add_command_with_telemetry(database.current)
  41. add_command_with_telemetry(database.history)
  42. add_command_with_telemetry(config.configure)
  43. def main():
  44. try:
  45. cli()
  46. except SystemExit:
  47. pass
  48. except Exception as e:
  49. console.print("[red]CLI error: An error occurred[/red]")
  50. console.print_exception()
  51. finally:
  52. if posthog:
  53. posthog.flush()
  54. posthog.shutdown()
  55. def _ensure_config_dir_exists() -> None:
  56. """Ensure that the ~/.r2r/ directory exists."""
  57. CONFIG_DIR.mkdir(parents=True, exist_ok=True)
  58. def save_config(config_data: dict[str, Any]) -> None:
  59. """
  60. Persist the given config data to ~/.r2r/config.json.
  61. """
  62. _ensure_config_dir_exists()
  63. with open(CONFIG_FILE, "w", encoding="utf-8") as f:
  64. json.dump(config_data, f, indent=2)
  65. @cli.command("set-api-key", short_help="Set your R2R API key")
  66. @click.argument("api_key", required=True, type=str)
  67. @click.pass_context
  68. async def set_api_key(ctx, api_key: str):
  69. """
  70. Store your R2R API key locally so you don’t have to pass it on every command.
  71. Example usage:
  72. r2r set-api sk-1234abcd
  73. """
  74. try:
  75. # 1) Load existing config
  76. config = load_config()
  77. # 2) Overwrite or add the API key
  78. config["api_key"] = api_key
  79. # 3) Save changes
  80. save_config(config)
  81. console.print("[green]API key set successfully![/green]")
  82. except Exception as e:
  83. console.print("[red]Failed to set API key:[/red]", str(e))
  84. @cli.command("get-api", short_help="Get your stored R2R API key")
  85. @click.pass_context
  86. async def get_api(ctx):
  87. """
  88. Display your stored R2R API key.
  89. Example usage:
  90. r2r get-api
  91. """
  92. try:
  93. config = load_config()
  94. api_key = config.get("api_key")
  95. if api_key:
  96. console.print(f"API Key: {api_key}")
  97. else:
  98. console.print(
  99. "[yellow]No API key found. Set one using 'r2r set-api <key>'[/yellow]"
  100. )
  101. except Exception as e:
  102. console.print("[red]Failed to retrieve API key:[/red]", str(e))
  103. if __name__ == "__main__":
  104. main()