Class: RubyClaude::Client
- Inherits:
-
Object
- Object
- RubyClaude::Client
- Defined in:
- lib/ruby_claude/client.rb
Overview
Composes a Command and a Runner to execute queries and build Response and Event objects.
A client holds an immutable Configuration and a stateless runner, builds fresh argv/env per call, and never mutates shared state — so one instance is safe to reuse and to call concurrently from many threads.
Constant Summary collapse
- AUTH_PATTERNS =
Heuristic patterns in stderr/result text that indicate an auth problem. Deliberately specific: bare “authentication” / “api key” / “credit balance” match too much benign text and would misclassify ordinary execution and billing failures as authentication errors.
Regexp.union( /invalid api key/i, /authentication[ _](?:failed|error|required)/i, /unauthorized/i, /not logged ?in/i, %r{/login}i, /oauth/i, /log ?in to claude/i ).freeze
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
The effective configuration for this client.
Instance Method Summary collapse
-
#initialize(runner: Runner.new, **overrides) ⇒ Client
constructor
A new instance of Client.
-
#query(prompt, resume: nil) ⇒ Response
(also: #ask)
Run a one-shot query and return its Response.
-
#session(id: nil) ⇒ Session
Start a multi-turn Session backed by this client.
-
#stream(prompt, resume: nil) {|event| ... } ⇒ Response
Stream a query, yielding Events as they arrive.
Constructor Details
#initialize(runner: Runner.new, **overrides) ⇒ Client
Returns a new instance of Client.
33 34 35 36 |
# File 'lib/ruby_claude/client.rb', line 33 def initialize(runner: Runner.new, **overrides) @config = RubyClaude.configuration.merge(overrides) @runner = runner end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns the effective configuration for this client.
28 29 30 |
# File 'lib/ruby_claude/client.rb', line 28 def config @config end |
Instance Method Details
#query(prompt, resume: nil) ⇒ Response Also known as: ask
Run a one-shot query and return its Response.
45 46 47 48 49 |
# File 'lib/ruby_claude/client.rb', line 45 def query(prompt, resume: nil) argv, env = Command.new(@config).build(stream: false, resume: resume) result = @runner.run(**run_args(argv, env, prompt)) interpret(result) end |
#session(id: nil) ⇒ Session
Start a multi-turn Session backed by this client.
78 79 80 |
# File 'lib/ruby_claude/client.rb', line 78 def session(id: nil) Session.new(self, id: id) end |
#stream(prompt, resume: nil) {|event| ... } ⇒ Response
Stream a query, yielding Events as they arrive.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruby_claude/client.rb', line 60 def stream(prompt, resume: nil) argv, env = Command.new(@config).build(stream: true, resume: resume) final = nil result = @runner.stream(**run_args(argv, env, prompt)) do |line| data = try_parse(line) next unless data final = data if data["type"] == "result" yield Event.from_hash(data) if block_given? end check_stream_result!(final, result) Response.from_result(final) end |