Class: CodexLimitless::AppServerClient
- Inherits:
-
Object
- Object
- CodexLimitless::AppServerClient
- Defined in:
- lib/codex_limitless/limits.rb
Constant Summary collapse
- REQUEST_TIMEOUT_SECONDS =
Integer(ENV.fetch("CODEX_USAGE_TIMEOUT", "30"))
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(codex_bin) ⇒ AppServerClient
constructor
A new instance of AppServerClient.
- #request(method, params: nil) ⇒ Object
Constructor Details
#initialize(codex_bin) ⇒ AppServerClient
Returns a new instance of AppServerClient.
18 19 20 21 22 23 24 |
# File 'lib/codex_limitless/limits.rb', line 18 def initialize(codex_bin) @stdin, @stdout, @stderr, @wait_thread = Open3.popen3(codex_bin, "app-server", "--stdio") @stderr_reader = Thread.new { @stderr.read } @next_id = 0 rescue Errno::ENOENT raise Error, "Could not find `#{codex_bin}`. Set CODEX_BIN to the Codex CLI path." end |
Instance Method Details
#close ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/codex_limitless/limits.rb', line 37 def close @stdin.close unless @stdin.closed? Timeout.timeout(2) { @wait_thread.value } rescue Timeout::Error Process.kill("TERM", @wait_thread.pid) @wait_thread.value ensure @stderr_reader&.join(0.2) end |
#request(method, params: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/codex_limitless/limits.rb', line 26 def request(method, params: nil) id = next_request_id payload = { "jsonrpc" => "2.0", "id" => id, "method" => method } payload["params"] = params unless params.nil? @stdin.puts(JSON.generate(payload)) @stdin.flush read_response(id) end |