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
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/clacky/cli.rb', line 62 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! # ── Sibling server discovery ─────────────────────────────────────── # Bare-CLI mode does NOT boot an HTTP server, so skills that call # back into /api/* (channels, browser, scheduler) normally can't work. # If the user happens to have a Clacky server running on this machine # (in another terminal or via `clacky server`), auto-wire CLACKY_SERVER_HOST # / CLACKY_SERVER_PORT so those skills can reach it transparently. discover_sibling_server! agent_config = Clacky::AgentConfig.load # Override model if --model option is specified if [:model] unless agent_config.switch_model_by_name([:model]) # During early startup @ui may not be ready; use simple error output $stderr.puts "Error: model '#{[:model]}' not found. Available: #{agent_config.model_names.join(', ')}" exit 1 end end # 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) Clacky::BrowserManager.instance.stop rescue nil end end |
#server ⇒ Object
907 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 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 |
# File 'lib/clacky/cli.rb', line 907 def server if [:help] invoke :help, ["server"] return end # ── 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.key?("CLACKY_ACCESS_KEY") 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" require_relative "server/epipe_safe_io" # Protect $stdout / $stderr from Errno::EPIPE. # # The worker inherits fd 1/2 from the Master process. If the Master's # stdout pipe ever breaks (e.g. it was launched by an installer or GUI # that has since exited), the next `puts` would raise Errno::EPIPE and # crash the worker — destroying all in-memory sessions, agent loops, # and SSE connections, and looping forever because the respawned # worker inherits the same broken fd. # # In healthy state these wrappers are transparent — output goes to # the user's terminal as usual. On first broken-pipe failure they # silently fall back to /dev/null and the worker stays alive. $stdout = Clacky::Server::EPIPESafeIO.new($stdout) $stderr = Clacky::Server::EPIPESafeIO.new($stderr) 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 # Apply CLI overrides to agent config (--no-compression etc.) # These override whatever is stored in config.yml. agent_config.enable_compression = false if [:no_compression] agent_config.memory_update_enabled = false if [:no_memory] agent_config.enable_prompt_caching = false if [:no_caching] if [:no_skill_evolution] agent_config.skill_evolution[:enabled] = false end 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] extra_flags << "--no-compression" if [:no_compression] extra_flags << "--no-memory" if [:no_memory] extra_flags << "--no-caching" if [:no_caching] extra_flags << "--no-skill-evolution" if [:no_skill_evolution] 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 |