Skip to content

Commit e7d0670

Browse files
committed
Fix Anthropic Gem Compatability
1 parent 46846da commit e7d0670

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,47 @@ jobs:
7070
RAILS_ENV: test
7171
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
7272
run: bin/test
73+
74+
test-api-gems:
75+
name: Test API Gems
76+
runs-on: ubuntu-latest
77+
env:
78+
BUNDLE_JOBS: 4
79+
BUNDLE_RETRY: 3
80+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
81+
CI: true
82+
ANTHROPIC_API_KEY: ANTHROPIC_API_KEY
83+
OPEN_AI_API_KEY: OPEN_AI_API_KEY
84+
OPEN_ROUTER_API_KEY: OPEN_ROUTER_API_KEY
85+
strategy:
86+
fail-fast: false
87+
matrix:
88+
ruby: ["3.4"]
89+
gemfile:
90+
- "gemfiles/anthropic_1.12.gemfile"
91+
- "gemfiles/anthropic_1.14.gemfile"
92+
steps:
93+
- uses: actions/checkout@v5
94+
- name: Install system deps
95+
run: |
96+
sudo apt-get update
97+
sudo apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config
98+
- uses: ruby/setup-ruby@v1
99+
with:
100+
ruby-version: ${{ matrix.ruby }}
101+
bundler-cache: true
102+
- name: Setup database
103+
env:
104+
RAILS_ENV: test
105+
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
106+
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
107+
run: |
108+
cd test/dummy
109+
bundle exec rails db:create
110+
bundle exec rails db:migrate
111+
cd ../..
112+
- name: Run tests
113+
env:
114+
RAILS_ENV: test
115+
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
116+
run: bin/test

gemfiles/anthropic_1.12.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "anthropic", "~> 1.12.0"
4+
5+
gemspec path: ".."

gemfiles/anthropic_1.14.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "anthropic", "~> 1.14.0"
4+
5+
gemspec path: ".."

lib/active_agent/providers/anthropic_provider.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ def process_stream_chunk(api_response_chunk)
127127

128128
# Message Completed [Full Message]
129129
when :message_stop
130-
self.message_stack[-1] = api_response_chunk.fetch(:message)
130+
api_message = api_response_chunk.fetch(:message)
131+
132+
# Patch for Anthropic 1.14.0 ##################################
133+
api_message[:content]&.each do |content_block|
134+
content_block.delete(:_json_buf) if content_block[:type] == "tool_use"
135+
end
136+
###############################################################
137+
138+
self.message_stack[-1] = api_message
131139

132140
# Once we are finished, close out and run tooling callbacks (Recursive)
133141
process_prompt_finished if message_stack.last[:stop_reason]
@@ -203,6 +211,12 @@ def process_prompt_finished_extract_function_calls
203211
message_stack.pluck(:content).flatten.select { _1 in { type: "tool_use" } }.map do |api_function_call|
204212
json_buf = api_function_call.delete(:json_buf)
205213
api_function_call[:input] = JSON.parse(json_buf, symbolize_names: true) if json_buf
214+
215+
# Handle case where :input is still a JSON string (gem >= 1.14.0)
216+
if api_function_call[:input].is_a?(String)
217+
api_function_call[:input] = JSON.parse(api_function_call[:input], symbolize_names: true)
218+
end
219+
206220
api_function_call
207221
end
208222
end

0 commit comments

Comments
 (0)