| 2035 | |
| 2036 | |
| 2037 | def dispatch_command(cmd: str) -> None: |
| 2038 | if cmd == "provider": |
| 2039 | from graphify.llm import _custom_providers_path, BACKENDS |
| 2040 | import json as _json |
| 2041 | subcmd = sys.argv[2] if len(sys.argv) > 2 else "" |
| 2042 | global_path = _custom_providers_path(global_=True) |
| 2043 | |
| 2044 | if subcmd == "list": |
| 2045 | global_path.parent.mkdir(parents=True, exist_ok=True) |
| 2046 | existing: dict = {} |
| 2047 | if global_path.is_file(): |
| 2048 | try: |
| 2049 | existing = _json.loads(global_path.read_text(encoding="utf-8")) |
| 2050 | except Exception: |
| 2051 | pass |
| 2052 | if not existing: |
| 2053 | print("No custom providers registered.") |
| 2054 | else: |
| 2055 | for name in existing: |
| 2056 | print(f" {name} ({existing[name].get('base_url', '')})") |
| 2057 | |
| 2058 | elif subcmd == "show": |
| 2059 | name = sys.argv[3] if len(sys.argv) > 3 else "" |
| 2060 | if not name: |
| 2061 | print("Usage: graphify provider show <name>", file=sys.stderr) |
| 2062 | sys.exit(1) |
| 2063 | existing = {} |
| 2064 | if global_path.is_file(): |
| 2065 | try: |
| 2066 | existing = _json.loads(global_path.read_text(encoding="utf-8")) |
| 2067 | except Exception: |
| 2068 | pass |
| 2069 | if name not in existing: |
| 2070 | print(f"Provider '{name}' not found.", file=sys.stderr) |
| 2071 | sys.exit(1) |
| 2072 | print(_json.dumps({name: existing[name]}, indent=2)) |
| 2073 | |
| 2074 | elif subcmd == "add": |
| 2075 | args = sys.argv[3:] |
| 2076 | name = args[0] if args and not args[0].startswith("-") else "" |
| 2077 | if not name: |
| 2078 | print("Usage: graphify provider add <name> --base-url URL --default-model MODEL --env-key KEY", file=sys.stderr) |
| 2079 | sys.exit(1) |
| 2080 | if name in BACKENDS: |
| 2081 | print(f"Error: '{name}' is a built-in provider and cannot be overridden.", file=sys.stderr) |
| 2082 | sys.exit(1) |
| 2083 | base_url = "" |
| 2084 | default_model = "" |
| 2085 | env_key = "" |
| 2086 | pricing_input = 0.0 |
| 2087 | pricing_output = 0.0 |
| 2088 | i = 1 |
| 2089 | while i < len(args): |
| 2090 | a = args[i] |
| 2091 | if a == "--base-url" and i + 1 < len(args): |
| 2092 | base_url = args[i + 1]; i += 2 |
| 2093 | elif a.startswith("--base-url="): |
| 2094 | base_url = a.split("=", 1)[1]; i += 1 |