claude_code_sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +38 -0
- data/.yardopts +7 -0
- data/CHANGELOG.md +36 -0
- data/CLAUDE.md +86 -0
- data/CONTRIBUTING.md +82 -0
- data/LICENSE +21 -0
- data/README.md +304 -0
- data/Rakefile +32 -0
- data/claude_code_sdk.gemspec +54 -0
- data/examples/debug_process.rb +60 -0
- data/examples/debug_query.rb +34 -0
- data/examples/simple_query.rb +23 -0
- data/examples/simple_test.rb +18 -0
- data/examples/simple_tools.rb +31 -0
- data/examples/simple_tools_fixed.rb +33 -0
- data/examples/streaming_responses.rb +39 -0
- data/examples/test_sdk.rb +45 -0
- data/examples/with_tools.rb +35 -0
- data/lib/claude_code_sdk/configuration.rb +27 -0
- data/lib/claude_code_sdk/errors.rb +45 -0
- data/lib/claude_code_sdk/internal/client.rb +122 -0
- data/lib/claude_code_sdk/messages.rb +101 -0
- data/lib/claude_code_sdk/transport/base.rb +24 -0
- data/lib/claude_code_sdk/transport/simple_cli.rb +108 -0
- data/lib/claude_code_sdk/transport/subprocess_cli.rb +167 -0
- data/lib/claude_code_sdk/types.rb +63 -0
- data/lib/claude_code_sdk/version.rb +5 -0
- data/lib/claude_code_sdk.rb +84 -0
- metadata +262 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7ce4c61015be30c500aa333028e9f14b453144989b8e2040a0482a9914817286
|
4
|
+
data.tar.gz: dbb147b28512697a329e06a770a8e677c147a2ee2e85bdbca9f0347697b6e282
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e8aff923ccef31394dcff359e9a7e4516d1c3fcb449fc69912e1fd7e53d00ab6fa084b7bf5b140b89e28781015b153e0835f292352a6beb7f0dc030ec099bb0c
|
7
|
+
data.tar.gz: d684035d8a139a67b83dd6cebffde09511dc6e0d6bc07035d4fe155b9b397f69d793f68c782be2b85e6970709697b67e4d0a18a02c5f361465875cba57706426
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
4
|
+
plugins:
|
5
|
+
- rubocop-rake
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
TargetRubyVersion: 3.0
|
9
|
+
NewCops: enable
|
10
|
+
Exclude:
|
11
|
+
- 'vendor/**/*'
|
12
|
+
- 'spec/fixtures/**/*'
|
13
|
+
- 'tmp/**/*'
|
14
|
+
|
15
|
+
Style/StringLiterals:
|
16
|
+
Enabled: true
|
17
|
+
EnforcedStyle: double_quotes
|
18
|
+
|
19
|
+
Style/StringLiteralsInInterpolation:
|
20
|
+
Enabled: true
|
21
|
+
EnforcedStyle: double_quotes
|
22
|
+
|
23
|
+
Layout/LineLength:
|
24
|
+
Max: 120
|
25
|
+
|
26
|
+
Metrics/BlockLength:
|
27
|
+
Exclude:
|
28
|
+
- 'spec/**/*'
|
29
|
+
- '*.gemspec'
|
30
|
+
|
31
|
+
Metrics/MethodLength:
|
32
|
+
Max: 20
|
33
|
+
|
34
|
+
RSpec/ExampleLength:
|
35
|
+
Max: 20
|
36
|
+
|
37
|
+
RSpec/MultipleExpectations:
|
38
|
+
Max: 5
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.1.0] - 2025-01-10
|
11
|
+
|
12
|
+
### Added
|
13
|
+
- Initial release of Claude Code SDK for Ruby
|
14
|
+
- Complete port of the official Python SDK functionality
|
15
|
+
- Support for all Claude Code CLI features including:
|
16
|
+
- File operations (read, write, list)
|
17
|
+
- Code generation and refactoring
|
18
|
+
- Interactive development with streaming responses
|
19
|
+
- Tool permissions and restrictions
|
20
|
+
- System prompts and configuration
|
21
|
+
- Strongly typed message system with content blocks
|
22
|
+
- Global and per-query configuration options
|
23
|
+
- Comprehensive error handling
|
24
|
+
- Full test suite with RSpec
|
25
|
+
- Integration tests for CLI interaction
|
26
|
+
- Support for Ruby 3.0 and above
|
27
|
+
|
28
|
+
### Developer Experience
|
29
|
+
- Ruby idiomatic API with block-based iteration
|
30
|
+
- Flexible configuration with sensible defaults
|
31
|
+
- Detailed documentation and examples
|
32
|
+
- RuboCop style enforcement
|
33
|
+
- YARD documentation support
|
34
|
+
|
35
|
+
[Unreleased]: https://github.com/TanookiLabs/claude-code-sdk-ruby/compare/v0.1.0...HEAD
|
36
|
+
[0.1.0]: https://github.com/TanookiLabs/claude-code-sdk-ruby/releases/tag/v0.1.0
|
data/CLAUDE.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# CLAUDE.md
|
2
|
+
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
4
|
+
|
5
|
+
## Development Commands
|
6
|
+
|
7
|
+
### Testing
|
8
|
+
```bash
|
9
|
+
# Run all tests
|
10
|
+
bundle exec rake spec
|
11
|
+
|
12
|
+
# Run tests with coverage
|
13
|
+
bundle exec rake coverage
|
14
|
+
|
15
|
+
# Run integration tests (requires Claude Code CLI)
|
16
|
+
bundle exec rake integration
|
17
|
+
|
18
|
+
# Default task (runs spec + rubocop)
|
19
|
+
bundle exec rake
|
20
|
+
```
|
21
|
+
|
22
|
+
### Code Quality
|
23
|
+
```bash
|
24
|
+
# Run linter
|
25
|
+
bundle exec rubocop
|
26
|
+
|
27
|
+
# Generate documentation
|
28
|
+
bundle exec yard
|
29
|
+
```
|
30
|
+
|
31
|
+
### Development
|
32
|
+
```bash
|
33
|
+
# Install dependencies
|
34
|
+
bundle install
|
35
|
+
|
36
|
+
# Open console with gem loaded
|
37
|
+
bundle exec rake console
|
38
|
+
|
39
|
+
# Build and install gem locally
|
40
|
+
bundle exec rake install
|
41
|
+
|
42
|
+
# Release gem (updates version, creates tag, pushes to rubygems)
|
43
|
+
bundle exec rake release
|
44
|
+
```
|
45
|
+
|
46
|
+
## Architecture Overview
|
47
|
+
|
48
|
+
This is a Ruby SDK for Claude Code CLI that provides a Ruby interface to Anthropic's AI coding assistant. The architecture follows these key patterns:
|
49
|
+
|
50
|
+
### Core Structure
|
51
|
+
- **Main Entry Point**: `ClaudeCodeSDK` module provides `query()` and `ask()` methods
|
52
|
+
- **Transport Layer**: Abstracts communication with Claude Code CLI via subprocess
|
53
|
+
- **Message System**: Strongly typed message objects for different interaction types
|
54
|
+
- **Configuration**: Global and per-query configuration options
|
55
|
+
|
56
|
+
### Key Components
|
57
|
+
|
58
|
+
**Transport Layer** (`lib/claude_code_sdk/transport/`):
|
59
|
+
- `Base`: Abstract transport interface
|
60
|
+
- `SimpleCLI`: Current implementation using direct CLI execution
|
61
|
+
- `SubprocessCLI`: Alternative implementation (currently disabled due to subprocess issues)
|
62
|
+
|
63
|
+
**Message Types** (`lib/claude_code_sdk/messages.rb`):
|
64
|
+
- `UserMessage`: User input
|
65
|
+
- `AssistantMessage`: Claude's responses with content blocks (text, tool use, tool results)
|
66
|
+
- `SystemMessage`: System notifications
|
67
|
+
- `ResultMessage`: Session results with cost/usage data
|
68
|
+
|
69
|
+
**Configuration** (`lib/claude_code_sdk/configuration.rb`):
|
70
|
+
- Singleton pattern for global defaults
|
71
|
+
- Options object for per-query configuration
|
72
|
+
- Supports all Claude Code CLI parameters (tools, permissions, prompts, etc.)
|
73
|
+
|
74
|
+
### Transport Implementation Details
|
75
|
+
The SDK currently uses `SimpleCLI` transport which spawns the Claude Code CLI process for each query. The CLI communication happens via JSON message streaming, with the SDK parsing and converting CLI output into strongly-typed Ruby message objects.
|
76
|
+
|
77
|
+
### Prerequisites
|
78
|
+
- Ruby >= 3.0.0
|
79
|
+
- Claude Code CLI must be installed: `npm install -g @anthropic-ai/claude-code`
|
80
|
+
- Zeitwerk for autoloading
|
81
|
+
|
82
|
+
### Testing Strategy
|
83
|
+
- Unit tests for message parsing and configuration
|
84
|
+
- Integration tests that require actual Claude Code CLI
|
85
|
+
- Coverage reporting via SimpleCov
|
86
|
+
- RuboCop for code style enforcement
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Contributing to Claude Code SDK for Ruby
|
2
|
+
|
3
|
+
We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:
|
4
|
+
|
5
|
+
- Reporting a bug
|
6
|
+
- Discussing the current state of the code
|
7
|
+
- Submitting a fix
|
8
|
+
- Proposing new features
|
9
|
+
- Becoming a maintainer
|
10
|
+
|
11
|
+
## We Develop with GitHub
|
12
|
+
|
13
|
+
We use GitHub to host code, to track issues and feature requests, as well as accept pull requests.
|
14
|
+
|
15
|
+
## We Use [GitHub Flow](https://guides.github.com/introduction/flow/index.html)
|
16
|
+
|
17
|
+
All code changes happen through pull requests. We actively welcome your pull requests:
|
18
|
+
|
19
|
+
1. Fork the repo and create your branch from `main`.
|
20
|
+
2. If you've added code that should be tested, add tests.
|
21
|
+
3. If you've changed APIs, update the documentation.
|
22
|
+
4. Ensure the test suite passes.
|
23
|
+
5. Make sure your code lints.
|
24
|
+
6. Issue that pull request!
|
25
|
+
|
26
|
+
## Any contributions you make will be under the MIT Software License
|
27
|
+
|
28
|
+
In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern.
|
29
|
+
|
30
|
+
## Report bugs using GitHub's [issues](https://github.com/TanookiLabs/claude-code-sdk-ruby/issues)
|
31
|
+
|
32
|
+
We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/TanookiLabs/claude-code-sdk-ruby/issues/new); it's that easy!
|
33
|
+
|
34
|
+
## Write bug reports with detail, background, and sample code
|
35
|
+
|
36
|
+
**Great Bug Reports** tend to have:
|
37
|
+
|
38
|
+
- A quick summary and/or background
|
39
|
+
- Steps to reproduce
|
40
|
+
- Be specific!
|
41
|
+
- Give sample code if you can
|
42
|
+
- What you expected would happen
|
43
|
+
- What actually happens
|
44
|
+
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)
|
45
|
+
|
46
|
+
## Development Process
|
47
|
+
|
48
|
+
1. Clone the repository
|
49
|
+
2. Run `bundle install` to install dependencies
|
50
|
+
3. Run `bundle exec rake spec` to run tests
|
51
|
+
4. Make your changes
|
52
|
+
5. Add tests for your changes
|
53
|
+
6. Run `bundle exec rubocop` to ensure code style
|
54
|
+
7. Create a pull request
|
55
|
+
|
56
|
+
### Running Tests
|
57
|
+
|
58
|
+
```bash
|
59
|
+
# Run all tests
|
60
|
+
bundle exec rake spec
|
61
|
+
|
62
|
+
# Run with coverage
|
63
|
+
bundle exec rake coverage
|
64
|
+
|
65
|
+
# Run integration tests (requires Claude Code CLI)
|
66
|
+
bundle exec rake integration
|
67
|
+
|
68
|
+
# Run linter
|
69
|
+
bundle exec rubocop
|
70
|
+
```
|
71
|
+
|
72
|
+
### Code Style
|
73
|
+
|
74
|
+
We use RuboCop to maintain consistent code style. Please ensure your code passes RuboCop checks before submitting.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
By contributing, you agree that your contributions will be licensed under its MIT License.
|
79
|
+
|
80
|
+
## References
|
81
|
+
|
82
|
+
This document was adapted from the open-source contribution guidelines for [Facebook's Draft](https://github.com/facebook/draft-js/blob/a9316a723f9e918afde44dea68b5f9f39b7d9b00/CONTRIBUTING.md)
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Tanooki Labs LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,304 @@
|
|
1
|
+
# Claude Code SDK for Ruby
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/claude_code_sdk)
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
5
|
+
[](https://www.ruby-lang.org)
|
6
|
+
|
7
|
+
A Ruby port of the official Python SDK for Claude Code, providing a clean, idiomatic Ruby interface for interacting with Anthropic's Claude Code AI assistant. This gem enables Ruby developers to harness the power of Claude's advanced AI capabilities for code generation, refactoring, debugging, and more.
|
8
|
+
|
9
|
+
## Why Claude Code SDK for Ruby?
|
10
|
+
|
11
|
+
- **Fully Featured**: Complete port of the official Python SDK with all the same functionality
|
12
|
+
- **Ruby Idiomatic**: Designed with Ruby best practices in mind - blocks, enumerables, and familiar patterns
|
13
|
+
- **Real-time Streaming**: Process Claude's responses as they arrive for a responsive experience
|
14
|
+
- **Type-Safe Messages**: Strongly typed message objects for better code clarity and error prevention
|
15
|
+
- **Flexible Configuration**: Global defaults with per-query overrides
|
16
|
+
- **Comprehensive Tool Support**: Access to all Claude Code tools including file operations, search, and more
|
17
|
+
- **Production Ready**: Robust error handling, comprehensive test coverage, and battle-tested implementation
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'claude_code_sdk'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle install
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install claude_code_sdk
|
34
|
+
|
35
|
+
## Prerequisites
|
36
|
+
|
37
|
+
The SDK requires the Claude Code CLI to be installed:
|
38
|
+
|
39
|
+
```bash
|
40
|
+
npm install -g @anthropic-ai/claude-code
|
41
|
+
```
|
42
|
+
|
43
|
+
Make sure you have your Anthropic API key configured:
|
44
|
+
|
45
|
+
```bash
|
46
|
+
export ANTHROPIC_API_KEY="your-api-key"
|
47
|
+
```
|
48
|
+
|
49
|
+
## Quick Start
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'claude_code_sdk'
|
53
|
+
|
54
|
+
# Simple question
|
55
|
+
response = ClaudeCodeSDK.ask("What's the best way to implement a singleton in Ruby?")
|
56
|
+
puts response
|
57
|
+
|
58
|
+
# Code generation with streaming
|
59
|
+
ClaudeCodeSDK.query("Create a Ruby class for managing a todo list") do |message|
|
60
|
+
if message.is_a?(ClaudeCodeSDK::AssistantMessage)
|
61
|
+
message.content.each do |block|
|
62
|
+
print block.text if block.is_a?(ClaudeCodeSDK::Content::TextBlock)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
## Key Features
|
69
|
+
|
70
|
+
### 1. Code Generation and Refactoring
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# Generate code with specific requirements
|
74
|
+
ClaudeCodeSDK.query(
|
75
|
+
"Create a thread-safe cache implementation with TTL support",
|
76
|
+
system_prompt: "You are an expert Ruby developer focused on performance and thread safety"
|
77
|
+
) do |message|
|
78
|
+
# Handle streaming responses
|
79
|
+
end
|
80
|
+
|
81
|
+
# Refactor existing code
|
82
|
+
code = File.read("legacy_code.rb")
|
83
|
+
ClaudeCodeSDK.ask("Refactor this code to use modern Ruby patterns:\n\n#{code}")
|
84
|
+
```
|
85
|
+
|
86
|
+
### 2. File Operations
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
# Allow Claude to read and modify files
|
90
|
+
ClaudeCodeSDK.query(
|
91
|
+
"Update all test files to use RSpec 3 syntax",
|
92
|
+
allowed_tools: ["read_file", "write_file", "list_files"],
|
93
|
+
cwd: Rails.root.to_s
|
94
|
+
) do |message|
|
95
|
+
# Claude will analyze and update your test files
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
### 3. Code Analysis and Debugging
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
# Analyze code for potential issues
|
103
|
+
ClaudeCodeSDK.query(
|
104
|
+
"Analyze this Rails controller for security vulnerabilities and performance issues",
|
105
|
+
allowed_tools: ["read_file"],
|
106
|
+
tree: ["app/controllers"],
|
107
|
+
tree_verbose: true
|
108
|
+
) do |message|
|
109
|
+
# Get detailed analysis with file context
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
### 4. Interactive Development
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
# Build features interactively
|
117
|
+
options = ClaudeCodeSDK::Options.new(
|
118
|
+
allowed_tools: ["read_file", "write_file", "bash"],
|
119
|
+
permission_mode: ClaudeCodeSDK::PermissionMode::ACCEPT_EDITS,
|
120
|
+
max_thinking_tokens: 20000
|
121
|
+
)
|
122
|
+
|
123
|
+
ClaudeCodeSDK.query("Help me add user authentication to my Sinatra app", options) do |message|
|
124
|
+
case message
|
125
|
+
when ClaudeCodeSDK::AssistantMessage
|
126
|
+
# Claude's responses and actions
|
127
|
+
when ClaudeCodeSDK::SystemMessage
|
128
|
+
puts "[System] #{message.title}: #{message.message}"
|
129
|
+
when ClaudeCodeSDK::ResultMessage
|
130
|
+
puts "Task completed! Cost: $#{message.cost[:usd]}"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
## Advanced Usage
|
136
|
+
|
137
|
+
### Global Configuration
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
ClaudeCodeSDK.configure do |config|
|
141
|
+
config.default_system_prompt = "You are a Ruby on Rails expert"
|
142
|
+
config.default_cwd = Rails.root.to_s
|
143
|
+
config.default_permission_mode = ClaudeCodeSDK::PermissionMode::DEFAULT
|
144
|
+
config.default_allowed_tools = ["read_file", "list_files"]
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
### Working with Message Types
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
ClaudeCodeSDK.query("Build a REST API endpoint") do |message|
|
152
|
+
case message
|
153
|
+
when ClaudeCodeSDK::UserMessage
|
154
|
+
# Your input to Claude
|
155
|
+
|
156
|
+
when ClaudeCodeSDK::AssistantMessage
|
157
|
+
message.content.each do |block|
|
158
|
+
case block
|
159
|
+
when ClaudeCodeSDK::Content::TextBlock
|
160
|
+
# Claude's text responses
|
161
|
+
puts block.text
|
162
|
+
|
163
|
+
when ClaudeCodeSDK::Content::ToolUseBlock
|
164
|
+
# Claude using a tool
|
165
|
+
puts "Using tool: #{block.name}"
|
166
|
+
|
167
|
+
when ClaudeCodeSDK::Content::ToolResultBlock
|
168
|
+
# Results from tool execution
|
169
|
+
puts "Tool output: #{block.output}"
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
when ClaudeCodeSDK::SystemMessage
|
174
|
+
# System notifications
|
175
|
+
|
176
|
+
when ClaudeCodeSDK::ResultMessage
|
177
|
+
# Final results with usage stats
|
178
|
+
puts "Tokens used: #{message.usage[:total_tokens]}"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
```
|
182
|
+
|
183
|
+
### Error Handling
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
begin
|
187
|
+
ClaudeCodeSDK.query("Complex task") do |message|
|
188
|
+
# Handle messages
|
189
|
+
end
|
190
|
+
rescue ClaudeCodeSDK::CLINotFoundError => e
|
191
|
+
# Claude Code CLI not installed
|
192
|
+
rescue ClaudeCodeSDK::ProcessError => e
|
193
|
+
# Process execution failed
|
194
|
+
puts "Exit code: #{e.exit_code}"
|
195
|
+
puts "Error: #{e.stderr}"
|
196
|
+
rescue ClaudeCodeSDK::TimeoutError => e
|
197
|
+
# Operation timed out
|
198
|
+
rescue ClaudeCodeSDK::CLIJSONDecodeError => e
|
199
|
+
# Invalid response from CLI
|
200
|
+
end
|
201
|
+
```
|
202
|
+
|
203
|
+
## Options Reference
|
204
|
+
|
205
|
+
| Option | Type | Description |
|
206
|
+
|--------|------|-------------|
|
207
|
+
| `allowed_tools` | Array | Tools Claude can use (e.g., `["read_file", "write_file"]`) |
|
208
|
+
| `blocked_tools` | Array | Tools Claude cannot use |
|
209
|
+
| `permission_mode` | String | File edit handling: `"default"`, `"acceptEdits"`, `"bypassPermissions"` |
|
210
|
+
| `max_thinking_tokens` | Integer | Max tokens for Claude's reasoning (default: 8000) |
|
211
|
+
| `system_prompt` | String | System instructions for Claude |
|
212
|
+
| `cwd` | String | Working directory for file operations |
|
213
|
+
| `mcp_servers` | Array | MCP server configurations |
|
214
|
+
| `disable_cache` | Boolean | Disable response caching |
|
215
|
+
| `no_markdown` | Boolean | Disable markdown formatting |
|
216
|
+
| `tree` | Array | Paths to include in file tree context |
|
217
|
+
| `tree_symlinks` | Boolean | Include symlinks in file tree |
|
218
|
+
| `tree_verbose` | Boolean | Verbose file tree output |
|
219
|
+
|
220
|
+
## Real-World Examples
|
221
|
+
|
222
|
+
### Rails Development Assistant
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
# Help with Rails development tasks
|
226
|
+
ClaudeCodeSDK.query(
|
227
|
+
"Add a full-text search feature to the Product model using PostgreSQL",
|
228
|
+
allowed_tools: ["read_file", "write_file", "bash"],
|
229
|
+
cwd: Rails.root.to_s,
|
230
|
+
system_prompt: "You are a Rails expert. Follow Rails conventions and best practices."
|
231
|
+
) do |message|
|
232
|
+
# Claude will analyze your models, create migrations, and implement search
|
233
|
+
end
|
234
|
+
```
|
235
|
+
|
236
|
+
### Test Generation
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
# Generate comprehensive tests
|
240
|
+
ClaudeCodeSDK.query(
|
241
|
+
"Generate RSpec tests for the UserService class with full coverage",
|
242
|
+
allowed_tools: ["read_file", "write_file"],
|
243
|
+
tree: ["app/services", "spec"]
|
244
|
+
) do |message|
|
245
|
+
# Claude analyzes your code and creates thorough test suites
|
246
|
+
end
|
247
|
+
```
|
248
|
+
|
249
|
+
### Code Review Assistant
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
# Automated code review
|
253
|
+
changed_files = `git diff --name-only main`.split("\n")
|
254
|
+
ClaudeCodeSDK.query(
|
255
|
+
"Review these changed files for code quality, potential bugs, and improvements",
|
256
|
+
allowed_tools: ["read_file"],
|
257
|
+
tree: changed_files
|
258
|
+
) do |message|
|
259
|
+
# Get detailed code review feedback
|
260
|
+
end
|
261
|
+
```
|
262
|
+
|
263
|
+
## Development
|
264
|
+
|
265
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
266
|
+
|
267
|
+
```bash
|
268
|
+
# Run all tests
|
269
|
+
bundle exec rake spec
|
270
|
+
|
271
|
+
# Run tests with coverage
|
272
|
+
bundle exec rake coverage
|
273
|
+
|
274
|
+
# Run integration tests (requires Claude Code CLI)
|
275
|
+
bundle exec rake integration
|
276
|
+
|
277
|
+
# Run linter
|
278
|
+
bundle exec rubocop
|
279
|
+
|
280
|
+
# Open console for experimentation
|
281
|
+
bundle exec rake console
|
282
|
+
```
|
283
|
+
|
284
|
+
## Contributing
|
285
|
+
|
286
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/TanookiLabs/claude-code-sdk-ruby. This project is intended to be a safe, welcoming space for collaboration.
|
287
|
+
|
288
|
+
1. Fork it
|
289
|
+
2. Create your feature branch (`git checkout -b feature/my-new-feature`)
|
290
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
291
|
+
4. Push to the branch (`git push origin feature/my-new-feature`)
|
292
|
+
5. Create a new Pull Request
|
293
|
+
|
294
|
+
## About Tanooki Labs
|
295
|
+
|
296
|
+
Claude Code SDK for Ruby is maintained by [Tanooki Labs LLC](https://tanookilabs.com), a software consultancy specializing in AI-powered developer tools and Ruby applications.
|
297
|
+
|
298
|
+
## License
|
299
|
+
|
300
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
301
|
+
|
302
|
+
## Acknowledgments
|
303
|
+
|
304
|
+
This gem is a Ruby port of the official Python SDK for Claude Code. We thank Anthropic for creating Claude and the Claude Code CLI that makes this integration possible.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
require "rubocop/rake_task"
|
6
|
+
require "yard"
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
|
+
RuboCop::RakeTask.new
|
10
|
+
YARD::Rake::YardocTask.new
|
11
|
+
|
12
|
+
task default: %i[spec rubocop]
|
13
|
+
|
14
|
+
desc "Run tests with coverage"
|
15
|
+
task :coverage do
|
16
|
+
ENV["COVERAGE"] = "true"
|
17
|
+
Rake::Task["spec"].invoke
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Open an IRB session with the gem loaded"
|
21
|
+
task :console do
|
22
|
+
require "irb"
|
23
|
+
require "claude_code_sdk"
|
24
|
+
ARGV.clear
|
25
|
+
IRB.start
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Run integration tests (requires Claude Code CLI)"
|
29
|
+
task :integration do
|
30
|
+
ENV["INTEGRATION"] = "true"
|
31
|
+
Rake::Task["spec"].invoke
|
32
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/claude_code_sdk/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "claude_code_sdk"
|
7
|
+
spec.version = ClaudeCodeSDK::VERSION
|
8
|
+
spec.authors = ["Tanooki Labs LLC"]
|
9
|
+
spec.email = ["hello@tanookilabs.com"]
|
10
|
+
|
11
|
+
spec.summary = "Ruby SDK for Claude Code - AI-powered coding assistant"
|
12
|
+
spec.description = "A Ruby port of the official Python SDK for Claude Code. This gem provides a clean, idiomatic Ruby interface for interacting with Anthropic's Claude Code AI assistant through the command-line interface."
|
13
|
+
spec.homepage = "https://github.com/TanookiLabs/claude-code-sdk-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 3.0.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
20
|
+
spec.metadata["documentation_uri"] = "https://www.rubydoc.info/gems/claude_code_sdk"
|
21
|
+
spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
|
22
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
23
|
+
|
24
|
+
# Files to include in the gem
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(File.expand_path(f) == __FILE__) ||
|
28
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github Gemfile])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
# Runtime dependencies
|
37
|
+
spec.add_dependency "zeitwerk", "~> 2.6"
|
38
|
+
|
39
|
+
# Development dependencies
|
40
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
41
|
+
spec.add_development_dependency "pry", "~> 0.14"
|
42
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
43
|
+
spec.add_development_dependency "rspec", "~> 3.13"
|
44
|
+
spec.add_development_dependency "rubocop", "~> 1.50"
|
45
|
+
spec.add_development_dependency "rubocop-rake", "~> 0.6"
|
46
|
+
spec.add_development_dependency "rubocop-rspec", "~> 2.20"
|
47
|
+
spec.add_development_dependency "simplecov", "~> 0.22"
|
48
|
+
spec.add_development_dependency "webmock", "~> 3.19"
|
49
|
+
spec.add_development_dependency "yard", "~> 0.9"
|
50
|
+
|
51
|
+
# Optional runtime dependencies
|
52
|
+
spec.add_development_dependency "async", "~> 2.0" # For async support
|
53
|
+
spec.add_development_dependency "sorbet-runtime", "~> 0.5" # For type checking
|
54
|
+
end
|