Class: Clacky::CLI
- Inherits:
-
Thor
- Object
- Thor
- Clacky::CLI
- Defined in:
- lib/clacky/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.exit_on_failure? ⇒ Boolean
13 14 15 |
# File 'lib/clacky/cli.rb', line 13 def self.exit_on_failure? true end |
Instance Method Details
#agent ⇒ Object
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 |
# File 'lib/clacky/cli.rb', line 60 def agent # Handle help option if [:help] invoke :help, ["agent"] return end # ── Telemetry (anonymous, opt-out via CLACKY_TELEMETRY=0) ────────── # Fire-and-forget background thread; never blocks startup. Clacky::Telemetry.startup! agent_config = Clacky::AgentConfig.load # Handle session listing if [:list] list_sessions return end # Handle Ctrl+C gracefully - raise exception to be caught in the loop Signal.trap("INT") do Thread.main.raise(Clacky::AgentInterrupted, "Interrupted by user") end # Validate and get working directory working_dir = validate_working_directory([:path]) # Update agent config with CLI options agent_config. = [:mode].to_sym if [:mode] agent_config.verbose = [:verbose] if [:verbose] # Client factory: produces a fresh Client reflecting the *current* # state of agent_config each time it's called. The CLI never holds a # long-lived `client` variable — instead, anyone who needs a client # (initial agent construction, /clear, etc.) calls the factory. # # This mirrors the server-side design (HTTPServer#client_factory) and # avoids the class of bugs where a shared client is ivar_set'd field by # field (easy to miss @model / @use_bedrock) and then reused for a # later Agent.new, serving stale credentials. client_factory = lambda do Clacky::Client.new( agent_config.api_key, base_url: agent_config.base_url, model: agent_config.model_name, anthropic_format: agent_config.anthropic_format? ) end # Resolve agent profile name from --agent option agent_profile = [:agent] || "coding" # Handle session loading/continuation session_manager = Clacky::SessionManager.new agent = nil is_session_load = false if [:continue] agent = load_latest_session(client_factory.call, agent_config, session_manager, working_dir, profile: agent_profile) is_session_load = !agent.nil? elsif [:attach] agent = load_session_by_number(client_factory.call, agent_config, session_manager, working_dir, [:attach], profile: agent_profile) is_session_load = !agent.nil? end # Create new agent if no session loaded if agent.nil? agent = Clacky::Agent.new(client_factory.call, agent_config, working_dir: working_dir, ui: nil, profile: agent_profile, session_id: Clacky::SessionManager.generate_id, source: :manual) agent.rename("CLI Session") end # Change to working directory original_dir = Dir.pwd should_chdir = File.realpath(working_dir) != File.realpath(original_dir) Dir.chdir(working_dir) if should_chdir begin if [:message] file_paths = Array([:file]) + Array([:image]) run_non_interactive(agent, [:message], file_paths, agent_config, session_manager) elsif [:json] run_agent_with_json(agent, working_dir, agent_config, session_manager, client_factory, profile: agent_profile) else run_agent_with_ui2(agent, working_dir, agent_config, session_manager, client_factory, is_session_load: is_session_load) end ensure Dir.chdir(original_dir) end end |
#server ⇒ Object
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 |
# File 'lib/clacky/cli.rb', line 908 def server # ── Security gate ────────────────────────────────────────────────────── # Binding to 0.0.0.0 exposes the server to the public network. # Refuse to start unless CLACKY_ACCESS_KEY env var is set. if [:host] == "0.0.0.0" && ENV.fetch("CLACKY_ACCESS_KEY", "").strip.empty? puts <<~MSG ╔══════════════════════════════════════════════════════════════╗ ║ ⚠️ Security Warning: Refusing to start ║ ╠══════════════════════════════════════════════════════════════╣ ║ ║ ║ Binding to 0.0.0.0 exposes Clacky to the public network. ║ ║ You must set CLACKY_ACCESS_KEY before starting the server. ║ ║ ║ ║ Generate a secure key: ║ ║ openssl rand -hex 32 ║ ║ ║ ║ Then export it: ║ ║ export CLACKY_ACCESS_KEY=<your-generated-key> ║ ║ ║ ╚══════════════════════════════════════════════════════════════╝ MSG exit(1) end # ───────────────────────────────────────────────────────────────────── if ENV["CLACKY_WORKER"] == "1" # ── Worker mode ─────────────────────────────────────────────────────── # Spawned by Master. Inherit the listen socket from the file descriptor # passed via CLACKY_INHERIT_FD, and report back to master via CLACKY_MASTER_PID. require_relative "server/http_server" fd = ENV["CLACKY_INHERIT_FD"].to_i master_pid = ENV["CLACKY_MASTER_PID"].to_i # Must use TCPServer.for_fd (not Socket.for_fd) so that accept_nonblock # returns a single Socket, not [Socket, Addrinfo] — WEBrick expects the former. socket = TCPServer.for_fd(fd) Clacky::Logger.console = true Clacky::Logger.info("[cli worker PID=#{Process.pid}] CLACKY_INHERIT_FD=#{fd} CLACKY_MASTER_PID=#{master_pid} socket=#{socket.class} fd=#{socket.fileno}") agent_config = Clacky::AgentConfig.load agent_config. = :confirm_all client_factory = lambda do Clacky::Client.new( agent_config.api_key, base_url: agent_config.base_url, model: agent_config.model_name, anthropic_format: agent_config.anthropic_format? ) end Clacky::Server::HttpServer.new( host: [:host], port: [:port], agent_config: agent_config, client_factory: client_factory, brand_test: [:brand_test], socket: socket, master_pid: master_pid ).start else # ── Master mode ─────────────────────────────────────────────────────── # First invocation by the user. Start the Master process which holds the # socket and supervises worker processes. require_relative "server/server_master" if [:brand_test] say "⚡ Brand test mode — license activation uses mock data (no remote API calls).", :yellow say "" say " Test license keys (paste any into Settings → Brand & License):", :cyan say "" say " 00000001-FFFFFFFF-DEADBEEF-CAFEBABE-00000001 → Brand1" say " 00000002-FFFFFFFF-DEADBEEF-CAFEBABE-00000002 → Brand2" say " 00000003-FFFFFFFF-DEADBEEF-CAFEBABE-00000003 → Brand3" say "" say " To reset: rm ~/.clacky/brand.yml", :cyan say "" end extra_flags = [] extra_flags << "--brand-test" if [:brand_test] Clacky::Logger.console = true # ── Telemetry (anonymous, opt-out via CLACKY_TELEMETRY=0) ────────── Clacky::Telemetry.startup! Clacky::Server::Master.new( host: [:host], port: [:port], extra_flags: extra_flags ).run end end |