anthropic 1.54.1 → 1.56.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.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +29 -0
  3. data/README.md +1 -1
  4. data/lib/anthropic/google_cloud.rb +6 -0
  5. data/lib/anthropic/helpers/google_cloud/client.rb +275 -0
  6. data/lib/anthropic/helpers/sessions/event_accumulator.rb +147 -0
  7. data/lib/anthropic/helpers/streaming/events.rb +4 -2
  8. data/lib/anthropic/helpers/streaming/message_stream.rb +27 -1
  9. data/lib/anthropic/models/anthropic_beta.rb +6 -0
  10. data/lib/anthropic/models/beta/beta_dream.rb +131 -0
  11. data/lib/anthropic/models/beta/beta_dream_error.rb +27 -0
  12. data/lib/anthropic/models/beta/beta_dream_input.rb +25 -0
  13. data/lib/anthropic/models/beta/beta_dream_memory_store_input.rb +37 -0
  14. data/lib/anthropic/models/beta/beta_dream_memory_store_output.rb +37 -0
  15. data/lib/anthropic/models/beta/beta_dream_model_config.rb +51 -0
  16. data/lib/anthropic/models/beta/beta_dream_model_config_param.rb +50 -0
  17. data/lib/anthropic/models/beta/beta_dream_output.rb +37 -0
  18. data/lib/anthropic/models/beta/beta_dream_sessions_input.rb +37 -0
  19. data/lib/anthropic/models/beta/beta_dream_status.rb +23 -0
  20. data/lib/anthropic/models/beta/beta_dream_usage.rb +46 -0
  21. data/lib/anthropic/models/beta/beta_fallback_param.rb +12 -5
  22. data/lib/anthropic/models/beta/beta_managed_agents_custom_tool_params.rb +1 -1
  23. data/lib/anthropic/models/beta/beta_tunnel.rb +67 -0
  24. data/lib/anthropic/models/beta/beta_tunnel_token.rb +42 -0
  25. data/lib/anthropic/models/beta/beta_usage.rb +7 -3
  26. data/lib/anthropic/models/beta/dream_archive_params.rb +31 -0
  27. data/lib/anthropic/models/beta/dream_cancel_params.rb +31 -0
  28. data/lib/anthropic/models/beta/dream_create_params.rb +59 -0
  29. data/lib/anthropic/models/beta/dream_list_params.rb +78 -0
  30. data/lib/anthropic/models/beta/dream_retrieve_params.rb +31 -0
  31. data/lib/anthropic/models/beta/memory_stores/memory_list_params.rb +1 -28
  32. data/lib/anthropic/models/beta/message_count_tokens_params.rb +7 -5
  33. data/lib/anthropic/models/beta/message_create_params.rb +7 -5
  34. data/lib/anthropic/models/beta/messages/batch_create_params.rb +7 -5
  35. data/lib/anthropic/models/beta/tunnel_archive_params.rb +31 -0
  36. data/lib/anthropic/models/beta/tunnel_create_params.rb +32 -0
  37. data/lib/anthropic/models/beta/tunnel_list_params.rb +48 -0
  38. data/lib/anthropic/models/beta/tunnel_retrieve_params.rb +31 -0
  39. data/lib/anthropic/models/beta/tunnel_reveal_token_params.rb +31 -0
  40. data/lib/anthropic/models/beta/tunnel_rotate_token_params.rb +39 -0
  41. data/lib/anthropic/models/beta/tunnels/beta_tunnel_certificate.rb +70 -0
  42. data/lib/anthropic/models/beta/tunnels/certificate_archive_params.rb +40 -0
  43. data/lib/anthropic/models/beta/tunnels/certificate_create_params.rb +45 -0
  44. data/lib/anthropic/models/beta/tunnels/certificate_list_params.rb +60 -0
  45. data/lib/anthropic/models/beta/tunnels/certificate_retrieve_params.rb +40 -0
  46. data/lib/anthropic/resources/beta/dreams.rb +159 -0
  47. data/lib/anthropic/resources/beta/memory_stores/memories.rb +7 -11
  48. data/lib/anthropic/resources/beta/memory_stores/memory_versions.rb +3 -3
  49. data/lib/anthropic/resources/beta/memory_stores.rb +6 -6
  50. data/lib/anthropic/resources/beta/messages.rb +3 -3
  51. data/lib/anthropic/resources/beta/tunnels/certificates.rb +172 -0
  52. data/lib/anthropic/resources/beta/tunnels.rb +222 -0
  53. data/lib/anthropic/resources/beta.rb +8 -0
  54. data/lib/anthropic/version.rb +1 -1
  55. data/lib/anthropic.rb +35 -0
  56. data/rbi/anthropic/google_cloud.rbi +6 -0
  57. data/rbi/anthropic/helpers/google_cloud/client.rbi +87 -0
  58. data/rbi/anthropic/helpers/sessions/event_accumulator.rbi +56 -0
  59. data/rbi/anthropic/models/anthropic_beta.rbi +7 -0
  60. data/rbi/anthropic/models/beta/beta_dream.rbi +169 -0
  61. data/rbi/anthropic/models/beta/beta_dream_error.rbi +31 -0
  62. data/rbi/anthropic/models/beta/beta_dream_input.rbi +28 -0
  63. data/rbi/anthropic/models/beta/beta_dream_memory_store_input.rbi +74 -0
  64. data/rbi/anthropic/models/beta/beta_dream_memory_store_output.rbi +74 -0
  65. data/rbi/anthropic/models/beta/beta_dream_model_config.rbi +105 -0
  66. data/rbi/anthropic/models/beta/beta_dream_model_config_param.rbi +103 -0
  67. data/rbi/anthropic/models/beta/beta_dream_output.rbi +70 -0
  68. data/rbi/anthropic/models/beta/beta_dream_sessions_input.rbi +72 -0
  69. data/rbi/anthropic/models/beta/beta_dream_status.rbi +36 -0
  70. data/rbi/anthropic/models/beta/beta_dream_usage.rbi +66 -0
  71. data/rbi/anthropic/models/beta/beta_fallback_param.rbi +13 -4
  72. data/rbi/anthropic/models/beta/beta_managed_agents_custom_tool_params.rbi +2 -2
  73. data/rbi/anthropic/models/beta/beta_tunnel.rbi +84 -0
  74. data/rbi/anthropic/models/beta/beta_tunnel_token.rbi +53 -0
  75. data/rbi/anthropic/models/beta/beta_usage.rbi +9 -3
  76. data/rbi/anthropic/models/beta/dream_archive_params.rbi +68 -0
  77. data/rbi/anthropic/models/beta/dream_cancel_params.rbi +68 -0
  78. data/rbi/anthropic/models/beta/dream_create_params.rbi +124 -0
  79. data/rbi/anthropic/models/beta/dream_list_params.rbi +142 -0
  80. data/rbi/anthropic/models/beta/dream_retrieve_params.rbi +68 -0
  81. data/rbi/anthropic/models/beta/memory_stores/memory_list_params.rbi +0 -70
  82. data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +9 -6
  83. data/rbi/anthropic/models/beta/message_create_params.rbi +9 -6
  84. data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +9 -6
  85. data/rbi/anthropic/models/beta/tunnel_archive_params.rbi +68 -0
  86. data/rbi/anthropic/models/beta/tunnel_create_params.rbi +70 -0
  87. data/rbi/anthropic/models/beta/tunnel_list_params.rbi +95 -0
  88. data/rbi/anthropic/models/beta/tunnel_retrieve_params.rbi +68 -0
  89. data/rbi/anthropic/models/beta/tunnel_reveal_token_params.rbi +68 -0
  90. data/rbi/anthropic/models/beta/tunnel_rotate_token_params.rbi +76 -0
  91. data/rbi/anthropic/models/beta/tunnels/beta_tunnel_certificate.rbi +91 -0
  92. data/rbi/anthropic/models/beta/tunnels/certificate_archive_params.rbi +77 -0
  93. data/rbi/anthropic/models/beta/tunnels/certificate_create_params.rbi +81 -0
  94. data/rbi/anthropic/models/beta/tunnels/certificate_list_params.rbi +104 -0
  95. data/rbi/anthropic/models/beta/tunnels/certificate_retrieve_params.rbi +77 -0
  96. data/rbi/anthropic/resources/beta/dreams.rbi +130 -0
  97. data/rbi/anthropic/resources/beta/memory_stores/memories.rbi +0 -7
  98. data/rbi/anthropic/resources/beta/messages.rbi +9 -6
  99. data/rbi/anthropic/resources/beta/tunnels/certificates.rbi +140 -0
  100. data/rbi/anthropic/resources/beta/tunnels.rbi +173 -0
  101. data/rbi/anthropic/resources/beta.rbi +6 -0
  102. data/sig/anthropic/google_cloud.rbs +5 -0
  103. data/sig/anthropic/helpers/google_cloud/client.rbs +52 -0
  104. data/sig/anthropic/helpers/sessions/event_accumulator.rbs +30 -0
  105. data/sig/anthropic/models/anthropic_beta.rbs +4 -0
  106. data/sig/anthropic/models/beta/beta_dream.rbs +94 -0
  107. data/sig/anthropic/models/beta/beta_dream_error.rbs +19 -0
  108. data/sig/anthropic/models/beta/beta_dream_input.rbs +17 -0
  109. data/sig/anthropic/models/beta/beta_dream_memory_store_input.rbs +39 -0
  110. data/sig/anthropic/models/beta/beta_dream_memory_store_output.rbs +39 -0
  111. data/sig/anthropic/models/beta/beta_dream_model_config.rbs +44 -0
  112. data/sig/anthropic/models/beta/beta_dream_model_config_param.rbs +40 -0
  113. data/sig/anthropic/models/beta/beta_dream_output.rbs +39 -0
  114. data/sig/anthropic/models/beta/beta_dream_sessions_input.rbs +39 -0
  115. data/sig/anthropic/models/beta/beta_dream_status.rbs +22 -0
  116. data/sig/anthropic/models/beta/beta_dream_usage.rbs +39 -0
  117. data/sig/anthropic/models/beta/beta_tunnel.rbs +49 -0
  118. data/sig/anthropic/models/beta/beta_tunnel_token.rbs +30 -0
  119. data/sig/anthropic/models/beta/dream_archive_params.rbs +34 -0
  120. data/sig/anthropic/models/beta/dream_cancel_params.rbs +34 -0
  121. data/sig/anthropic/models/beta/dream_create_params.rbs +55 -0
  122. data/sig/anthropic/models/beta/dream_list_params.rbs +76 -0
  123. data/sig/anthropic/models/beta/dream_retrieve_params.rbs +34 -0
  124. data/sig/anthropic/models/beta/memory_stores/memory_list_params.rbs +0 -27
  125. data/sig/anthropic/models/beta/tunnel_archive_params.rbs +34 -0
  126. data/sig/anthropic/models/beta/tunnel_create_params.rbs +37 -0
  127. data/sig/anthropic/models/beta/tunnel_list_params.rbs +53 -0
  128. data/sig/anthropic/models/beta/tunnel_retrieve_params.rbs +34 -0
  129. data/sig/anthropic/models/beta/tunnel_reveal_token_params.rbs +34 -0
  130. data/sig/anthropic/models/beta/tunnel_rotate_token_params.rbs +42 -0
  131. data/sig/anthropic/models/beta/tunnels/beta_tunnel_certificate.rbs +54 -0
  132. data/sig/anthropic/models/beta/tunnels/certificate_archive_params.rbs +44 -0
  133. data/sig/anthropic/models/beta/tunnels/certificate_create_params.rbs +44 -0
  134. data/sig/anthropic/models/beta/tunnels/certificate_list_params.rbs +60 -0
  135. data/sig/anthropic/models/beta/tunnels/certificate_retrieve_params.rbs +44 -0
  136. data/sig/anthropic/resources/beta/dreams.rbs +46 -0
  137. data/sig/anthropic/resources/beta/memory_stores/memories.rbs +0 -2
  138. data/sig/anthropic/resources/beta/tunnels/certificates.rbs +41 -0
  139. data/sig/anthropic/resources/beta/tunnels.rbs +50 -0
  140. data/sig/anthropic/resources/beta.rbs +4 -0
  141. metadata +107 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b20f5272e4bfc8e9d59cc7f4dfcbb782e15de065e1df4fb7efae19b8d70f9b22
4
- data.tar.gz: 7ff464adb217ac4acd2d800649cc08a33d544c847304a7f940c16aa0c3c1cacc
3
+ metadata.gz: 58294c110c5b712622ddf3921622929576a73ec5c08cb4316f5c34f6dfcf6d2a
4
+ data.tar.gz: 665f92b3d88328dff5a61a02481f3d11a05b207d2928a2e8d29e18041ee95db3
5
5
  SHA512:
6
- metadata.gz: 46357a712cb1de2f2c37a7fb0c8ed445abf59eeff5d5d50b310094bbb72b5471eb7148b32b437edc6dd742f7f256ffc4f03bd557d290765de078ec5ded1765c7
7
- data.tar.gz: 7acbb4df6dd1aa2a3668b466086d5a103475995dc4233a66a4abb7b27b5ad15d0f8a8a7ef476321f9687556df20085e6bbaeeac6cd6e6c438b763c7247f15db7
6
+ metadata.gz: 68c7f02ba359316458ef143f503a1986f144390541b4723c2e7e24694d7183eb76b17581a36c06de13fca3421e50fa82b9a32d05879785b2676bee4f3c6d58e6
7
+ data.tar.gz: d0fcf027e0eec3331b01c392f857f55a9098a8b7e16b9fa9a6188dff9a9d3474c72bdbcd250dd5e853c151c42e300b828c691bd5e39e4c0863b5e7e39c5fb753
data/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.56.0 (2026-07-16)
4
+
5
+ Full Changelog: [v1.55.0...v1.56.0](https://github.com/anthropics/anthropic-sdk-ruby/compare/v1.55.0...v1.56.0)
6
+
7
+ ### Features
8
+
9
+ * **api:** add support for dreaming ([6277ed9](https://github.com/anthropics/anthropic-sdk-ruby/commit/6277ed90a84e5b0b6574de7301b5f95470627fcc))
10
+ * **api:** add support for MCP Tunnels ([4ecd4ba](https://github.com/anthropics/anthropic-sdk-ruby/commit/4ecd4ba4ebda2a08eeac2635668753ef7ee968b5))
11
+ * **helpers:** add session delta accumulator for agent.message previews ([#83](https://github.com/anthropics/anthropic-sdk-ruby/issues/83)) ([01616a4](https://github.com/anthropics/anthropic-sdk-ruby/commit/01616a41bdfe88e9397fad901e0198b6a7c02c7b))
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * **streaming:** decode tool_use input when the content block stops ([#109](https://github.com/anthropics/anthropic-sdk-ruby/issues/109)) ([800277b](https://github.com/anthropics/anthropic-sdk-ruby/commit/800277b6e9f46a50533e696d192a6d23eddc920d))
17
+
18
+
19
+ ### Chores
20
+
21
+ * **docs:** small updates to field descriptions ([0d7bf61](https://github.com/anthropics/anthropic-sdk-ruby/commit/0d7bf611ef0a96aba1c3604476f48d6da7b5bc1d))
22
+ * **docs:** updates to descriptions and examples ([ea6bfd7](https://github.com/anthropics/anthropic-sdk-ruby/commit/ea6bfd74e293c62f6ca919387dc60154af959bbd))
23
+
24
+ ## 1.55.0 (2026-07-02)
25
+
26
+ Full Changelog: [v1.54.1...v1.55.0](https://github.com/anthropics/anthropic-sdk-ruby/compare/v1.54.1...v1.55.0)
27
+
28
+ ### Features
29
+
30
+ * **api:** add agent-memory-2026-07-22 beta header ([c3e9511](https://github.com/anthropics/anthropic-sdk-ruby/commit/c3e9511391e1976a580484e31d6dd2818afecc07))
31
+
3
32
  ## 1.54.1 (2026-07-01)
4
33
 
5
34
  Full Changelog: [v1.54.0...v1.54.1](https://github.com/anthropics/anthropic-sdk-ruby/compare/v1.54.0...v1.54.1)
data/README.md CHANGED
@@ -15,7 +15,7 @@ Add to your application's Gemfile:
15
15
  <!-- x-release-please-start-version -->
16
16
 
17
17
  ```ruby
18
- gem "anthropic", "~> 1.54.1"
18
+ gem "anthropic", "~> 1.56.0"
19
19
  ```
20
20
 
21
21
  <!-- x-release-please-end -->
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ GoogleCloud = Anthropic::Helpers::GoogleCloud
5
+ GoogleCloudClient = Anthropic::Helpers::GoogleCloud::Client
6
+ end
@@ -0,0 +1,275 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module GoogleCloud
6
+ # Client for Claude Platform on Google Cloud — the first-party Anthropic
7
+ # API served through the Google Cloud gateway.
8
+ #
9
+ # This is distinct from {Anthropic::VertexClient}, which targets the
10
+ # `:rawPredict` publisher-model API (publisher model IDs, messages
11
+ # only). This client speaks the full first-party Anthropic API: requests
12
+ # pass through the gateway unchanged — standard `/v1/*` paths, standard
13
+ # model names, the complete API surface. The deprecated text Completions
14
+ # endpoint is intentionally not exposed.
15
+ class Client < Anthropic::Client
16
+ # OAuth2 scope required to call the gateway.
17
+ SCOPE = "https://www.googleapis.com/auth/cloud-platform"
18
+
19
+ # Gateway base URL template; override via `base_url` or
20
+ # `ANTHROPIC_GOOGLE_CLOUD_BASE_URL`.
21
+ URL_TEMPLATE =
22
+ "https://claude.googleapis.com/v1alpha" \
23
+ "/projects/%<project>s/locations/%<location>s/workspaces/%<workspace_id>s/invoke"
24
+
25
+ # @return [String, nil]
26
+ attr_reader :project
27
+
28
+ # @return [String, nil]
29
+ attr_reader :location
30
+
31
+ # @return [String, nil]
32
+ attr_reader :workspace_id
33
+
34
+ # @return [Anthropic::Resources::Messages]
35
+ attr_reader :messages
36
+
37
+ # @return [Anthropic::Resources::Models]
38
+ attr_reader :models
39
+
40
+ # @return [Anthropic::Resources::Beta]
41
+ attr_reader :beta
42
+
43
+ # Creates a new client for the Anthropic API on the Google Cloud gateway.
44
+ #
45
+ # Authentication uses Google credentials by precedence (unless
46
+ # `skip_auth` is true):
47
+ # 1. `token_provider` — a callable returning a GCP access token, called
48
+ # per attempt
49
+ # 2. `google_credentials` — a `googleauth` credentials object (anything
50
+ # responding to `#apply`), refreshed by the library as needed
51
+ # 3. Application Default Credentials (`Google::Auth.get_application_default`)
52
+ # with the `cloud-platform` scope
53
+ #
54
+ # @param project [String, nil] GCP consumer project id. Defaults to
55
+ # `ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"]`, then
56
+ # `ENV["GOOGLE_CLOUD_PROJECT"]`, then the project reported by the
57
+ # resolved Google credentials. Only required when the base URL must be
58
+ # derived.
59
+ #
60
+ # @param location [String, nil] GCP location. Defaults to
61
+ # `ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"]`, then `"global"`; an
62
+ # explicit `nil` falls back to the same defaults.
63
+ #
64
+ # @param workspace_id [String, nil] Defaults to
65
+ # `ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"]`. Required unless
66
+ # `skip_auth` is true and an explicit `base_url` is given.
67
+ #
68
+ # @param base_url [String, nil] Override the gateway base URL. Defaults to
69
+ # `ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"]`, otherwise derived from
70
+ # `project`, `location`, and `workspace_id` via {URL_TEMPLATE}.
71
+ #
72
+ # @param google_credentials [#apply, nil] A `googleauth`-compatible
73
+ # credentials object. Mutually exclusive with `skip_auth`.
74
+ #
75
+ # @param token_provider [#call, nil] A callable returning a GCP access
76
+ # token string. Takes precedence over `google_credentials`. Mutually
77
+ # exclusive with `skip_auth`.
78
+ #
79
+ # @param skip_auth [Boolean] When true, no token is attached and
80
+ # `workspace_id` is only required when the base URL must be derived.
81
+ # For callers fronting with their own authenticated proxy. Mutually
82
+ # exclusive with `token_provider` and `google_credentials`.
83
+ #
84
+ # @param max_retries [Integer] Max number of retries to attempt after a
85
+ # failed retryable request.
86
+ #
87
+ # @param timeout [Float]
88
+ #
89
+ # @param initial_retry_delay [Float]
90
+ #
91
+ # @param max_retry_delay [Float]
92
+ #
93
+ # @param middleware [Array<#call>, #call, nil] Per-attempt HTTP
94
+ # around-middleware. See {Anthropic::Middleware}. The Google bearer
95
+ # is applied per attempt, after user middleware.
96
+ #
97
+ # @raise [ArgumentError]
98
+ def initialize( # rubocop:disable Lint/MissingSuper
99
+ project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"],
100
+ location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global",
101
+ workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"],
102
+ base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"],
103
+ google_credentials: nil,
104
+ token_provider: nil,
105
+ skip_auth: false,
106
+ max_retries: self.class::DEFAULT_MAX_RETRIES,
107
+ timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
108
+ initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
109
+ max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY,
110
+ middleware: nil
111
+ )
112
+ if skip_auth && (token_provider || google_credentials)
113
+ raise ArgumentError.new(
114
+ "`skip_auth` is mutually exclusive with `token_provider` and `google_credentials`; " \
115
+ "`skip_auth` disables authentication entirely."
116
+ )
117
+ end
118
+
119
+ @skip_auth = skip_auth
120
+ @token_provider = token_provider
121
+ @authorization = google_credentials
122
+
123
+ # Resolve credentials eagerly so the client is thread-safe (no lazy
124
+ # ADC race) and so the credentials' project can backfill `project`
125
+ # before the base URL is derived.
126
+ if !skip_auth && token_provider.nil? && google_credentials.nil?
127
+ require_googleauth
128
+ @authorization = Google::Auth.get_application_default([SCOPE])
129
+ end
130
+
131
+ if project.nil? && @authorization.respond_to?(:project_id)
132
+ project = @authorization.project_id
133
+ end
134
+ @project = project
135
+ # The kwarg default doesn't fire on an explicitly-passed nil — coerce
136
+ # so nil can't reach the derived URL (matches the other SDKs).
137
+ @location = location || ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global"
138
+
139
+ if workspace_id.nil? && !skip_auth
140
+ raise ArgumentError.new(
141
+ "No workspace ID was given. Set the `workspace_id` argument or the " \
142
+ "`ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID` environment variable."
143
+ )
144
+ end
145
+ @workspace_id = workspace_id
146
+
147
+ base_url = derive_base_url if base_url.nil?
148
+
149
+ # Never inherit first-party Anthropic credentials from the
150
+ # environment: clear the ivars `Anthropic::Client#auth_headers` reads,
151
+ # and bind `BaseClient#initialize` directly so the parent's
152
+ # credential-provider / `ANTHROPIC_BASE_URL` resolution is skipped
153
+ # entirely.
154
+ @api_key = nil
155
+ @auth_token = nil
156
+ @credentials = nil
157
+ @token_cache = nil
158
+
159
+ Anthropic::Internal::Transport::BaseClient.instance_method(:initialize).bind(self).call(
160
+ base_url: base_url,
161
+ timeout: timeout,
162
+ max_retries: max_retries,
163
+ initial_retry_delay: initial_retry_delay,
164
+ max_retry_delay: max_retry_delay,
165
+ headers: {"anthropic-version" => "2023-06-01"},
166
+ middleware: middleware
167
+ )
168
+
169
+ @messages = Anthropic::Resources::Messages.new(client: self)
170
+ @models = Anthropic::Resources::Models.new(client: self)
171
+ @beta = Anthropic::Resources::Beta.new(client: self)
172
+ end
173
+
174
+ # The deprecated text Completions endpoint is not supported on the
175
+ # Google Cloud gateway.
176
+ #
177
+ # @raise [NotImplementedError]
178
+ def completions
179
+ raise NotImplementedError.new(
180
+ "The deprecated text Completions API is not supported on the Google Cloud gateway."
181
+ )
182
+ end
183
+
184
+ # @api private
185
+ #
186
+ # Hard guarantee that no first-party `X-Api-Key` / `Authorization`
187
+ # header from static credentials reaches the wire; auth is the GCP
188
+ # bearer token attached in {#provider_middleware}.
189
+ #
190
+ # @return [Hash{String=>String}]
191
+ private def auth_headers = {}
192
+
193
+ # @api private
194
+ #
195
+ # The Google Cloud provider middleware: applies the Google OAuth
196
+ # `authorization` bearer token. Appended innermost on every dispatch
197
+ # (below user middleware) and runs per attempt. Pure — requests are reused across
198
+ # retry attempts, so the incoming `req` is never mutated.
199
+ #
200
+ # @return [#call]
201
+ private def provider_middleware
202
+ lambda do |req, nxt|
203
+ nxt.call(apply_auth(req))
204
+ end
205
+ end
206
+
207
+ # @api private
208
+ #
209
+ # @param req [Anthropic::APIRequest]
210
+ # @return [Anthropic::APIRequest]
211
+ private def apply_auth(req)
212
+ # `req.headers` is the deep-frozen hash a middleware saw; splat into a
213
+ # fresh mutable copy so googleauth's `#apply` (which `clone`s then
214
+ # `[]=`s) doesn't raise.
215
+ headers = {**req.headers}
216
+
217
+ # `follow_redirect` stripped `authorization` for a cross-origin hop —
218
+ # don't re-add it and leak the bearer token to the new origin.
219
+ if !@skip_auth && !req.metadata[:cross_origin_redirect] && !headers.key?("authorization")
220
+ headers =
221
+ if @token_provider
222
+ headers.merge("authorization" => "Bearer #{@token_provider.call}")
223
+ else
224
+ @authorization.apply(headers)
225
+ end
226
+ end
227
+
228
+ req.with(headers: headers)
229
+ end
230
+
231
+ # @api private
232
+ #
233
+ # @return [String]
234
+ # @raise [ArgumentError]
235
+ private def derive_base_url
236
+ if @project.nil?
237
+ raise ArgumentError.new(
238
+ "No project found. Set the `project` argument, set the " \
239
+ "`ANTHROPIC_GOOGLE_CLOUD_PROJECT` environment variable, or configure " \
240
+ "application default credentials with a project."
241
+ )
242
+ end
243
+ if @workspace_id.nil?
244
+ raise ArgumentError.new(
245
+ "No workspace ID was given. Set the `workspace_id` argument or the " \
246
+ "`ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID` environment variable."
247
+ )
248
+ end
249
+ format(URL_TEMPLATE, project: @project, location: @location, workspace_id: @workspace_id)
250
+ end
251
+
252
+ # @api private
253
+ private def require_googleauth
254
+ require("googleauth")
255
+ rescue LoadError
256
+ message = <<~MSG
257
+
258
+ In order to use the Anthropic Google Cloud client you must require the `googleauth` gem.
259
+ You can install it by adding the following to your Gemfile:
260
+
261
+ gem "googleauth"
262
+
263
+ and then running `bundle install`.
264
+
265
+ Alternatively, if you are not using Bundler, simply run:
266
+
267
+ gem install googleauth
268
+ MSG
269
+
270
+ raise RuntimeError.new(message)
271
+ end
272
+ end
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Sessions
6
+ # Accumulates session stream events into `agent.message` snapshots.
7
+ #
8
+ # `event_delta` previews are lossy and best-effort; the buffered final
9
+ # `agent.message` event is canonical and replaces whatever the preview
10
+ # accumulated. A `span.model_request_end` event closes all open previews
11
+ # (the producing model request ended without emitting the buffered
12
+ # event), discarding their partial snapshots.
13
+ #
14
+ # @example
15
+ # accumulator = Anthropic::Helpers::Sessions::EventAccumulator.new
16
+ # accumulator.wrap(stream).each do |event|
17
+ # print accumulator.agent_message_text(event.event_id) if event.type == :event_delta
18
+ # end
19
+ class EventAccumulator
20
+ # Raised when the delta stream is malformed: an `event_delta` whose
21
+ # `event_start` was never seen, or whose index is out of range for the
22
+ # accumulated content.
23
+ class AccumulationError < Anthropic::Errors::Error
24
+ end
25
+
26
+ # Snapshots keyed by event id, including those still accumulating.
27
+ # While a preview is open the snapshot is partial and its
28
+ # `processed_at` is unset; once the buffered final `agent.message`
29
+ # arrives it is the canonical event.
30
+ #
31
+ # @return [Hash{String => Anthropic::Models::Beta::Sessions::BetaManagedAgentsAgentMessageEvent}]
32
+ attr_reader :agent_messages
33
+
34
+ def initialize
35
+ @agent_messages = {}
36
+ end
37
+
38
+ # Folds one stream event into the accumulated state and returns the
39
+ # event unchanged.
40
+ #
41
+ # @param event [Anthropic::Models::Beta::Sessions::BetaManagedAgentsStreamSessionEvents]
42
+ #
43
+ # @raise [Anthropic::Helpers::Sessions::EventAccumulator::AccumulationError]
44
+ # on an `event_delta` whose `event_start` was never seen, or whose
45
+ # index is out of range for the accumulated content.
46
+ #
47
+ # @return [Anthropic::Models::Beta::Sessions::BetaManagedAgentsStreamSessionEvents]
48
+ def accumulate(event)
49
+ case event.type
50
+ in :event_start
51
+ on_event_start(event)
52
+ in :event_delta
53
+ on_event_delta(event)
54
+ in :"agent.message"
55
+ @agent_messages[event.id] = event
56
+ in :"span.model_request_end"
57
+ # The model request ended without buffering the previewed events
58
+ # (an error or interrupt mid-stream); discard the open previews.
59
+ # Canonical events (`processed_at` set) survive.
60
+ @agent_messages.delete_if { |_, message| message.processed_at.nil? }
61
+ else
62
+ # Other event types carry no `agent.message` content.
63
+ end
64
+ event
65
+ end
66
+
67
+ # Returns an Enumerator that lazily folds each event from `enum`
68
+ # through {#accumulate} and yields it unchanged. The enumerator is
69
+ # fused: it can be consumed once, and re-iterating yields nothing
70
+ # rather than folding deltas a second time.
71
+ #
72
+ # @param enum [Enumerable<Anthropic::Models::Beta::Sessions::BetaManagedAgentsStreamSessionEvents>]
73
+ #
74
+ # @return [Enumerator<Anthropic::Models::Beta::Sessions::BetaManagedAgentsStreamSessionEvents>]
75
+ def wrap(enum)
76
+ Anthropic::Internal::Util.chain_fused(enum) do |yielder|
77
+ enum.each { yielder << accumulate(_1) }
78
+ end
79
+ end
80
+
81
+ # The concatenated text of the accumulated `agent.message` with the
82
+ # given event id, or `nil` when the id is unknown.
83
+ #
84
+ # @param event_id [String]
85
+ #
86
+ # @return [String, nil]
87
+ def agent_message_text(event_id)
88
+ @agent_messages[event_id]&.content&.filter_map { _1.text if _1.type == :text }&.join
89
+ end
90
+
91
+ private
92
+
93
+ def on_event_start(event)
94
+ # `agent.thinking` previews are start-only today; deltas may come later.
95
+ return unless event.event.type == :"agent.message"
96
+
97
+ id = event.event.id
98
+ # `processed_at` is left unset: that is what marks the snapshot as an
99
+ # open preview until the buffered final event reconciles it.
100
+ @agent_messages[id] = Anthropic::Beta::Sessions::BetaManagedAgentsAgentMessageEvent.new(
101
+ id: id,
102
+ type: :"agent.message",
103
+ content: []
104
+ )
105
+ end
106
+
107
+ def on_event_delta(event)
108
+ snapshot = @agent_messages[event.event_id]
109
+ if snapshot.nil?
110
+ raise AccumulationError.new(
111
+ "event_delta for #{event.event_id} received before its event_start"
112
+ )
113
+ end
114
+
115
+ # The final event is canonical; drop deltas that straggle in after it.
116
+ return unless snapshot.processed_at.nil?
117
+
118
+ index = event.delta.index || 0
119
+ fragment = event.delta.content
120
+ content = snapshot.content
121
+
122
+ # Indices arrive in order — the first delta at a new index opens the
123
+ # slot. A gap means deltas arrived out of order or were mis-routed.
124
+ unless (0..content.length).cover?(index)
125
+ raise AccumulationError.new(
126
+ "event_delta index #{index} is beyond the end of content (length #{content.length})"
127
+ )
128
+ end
129
+
130
+ existing = content[index]
131
+ if existing.nil?
132
+ # Copy the fragment so later appends never mutate the wire event.
133
+ block = Anthropic::Beta::Sessions::BetaManagedAgentsTextBlock.new(
134
+ type: fragment.type,
135
+ text: fragment.text
136
+ )
137
+ snapshot.content = content + [block]
138
+ elsif fragment.type == :text && existing.type == :text
139
+ existing.text += fragment.text
140
+ else
141
+ # Unrecognised fragment type: no-op; the canonical event fills the gap.
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -107,11 +107,13 @@ module Anthropic
107
107
  required :partial_json, String
108
108
 
109
109
  # @!attribute snapshot
110
- # The currently accumulated parsed object up to this point in the stream.
110
+ # The raw accumulated partial-JSON string for the tool input up to this
111
+ # point in the stream. Decoded into the finished block's `input` when the
112
+ # content block stops.
111
113
  #
112
114
  # Example:
113
115
  # ```
114
- # {'location': 'San Francisco, CA'}
116
+ # '{"location":"San Francisco'
115
117
  # ```
116
118
  #
117
119
  # @return [Object]
@@ -141,6 +141,20 @@ module Anthropic
141
141
  content.encrypted_content = delta.encrypted_content
142
142
  else
143
143
  end
144
+ in Anthropic::Models::RawContentBlockStopEvent | Anthropic::Models::BetaRawContentBlockStopEvent
145
+ # The input_json_delta arm above buffers into `input` as a raw String so the
146
+ # streaming `input_json` events can expose the partial snapshot verbatim; the
147
+ # finished block must carry the decoded object, as in the non-streaming
148
+ # message and the other SDKs' accumulators.
149
+ case current_snapshot.content[event.index]
150
+ in {type: :tool_use, input: String => raw} => content
151
+ begin
152
+ content.input = decode_tool_use_input(raw)
153
+ rescue JSON::ParserError
154
+ # malformed complete input JSON from the server: leave the raw buffer
155
+ end
156
+ else
157
+ end
144
158
  in Anthropic::Models::RawMessageDeltaEvent | Anthropic::Models::BetaRawMessageDeltaEvent
145
159
  current_snapshot.stop_reason = event.delta.stop_reason
146
160
  current_snapshot.stop_sequence = event.delta.stop_sequence
@@ -248,6 +262,18 @@ module Anthropic
248
262
  events_to_yield
249
263
  end
250
264
 
265
+ # @api private
266
+ #
267
+ # Decodes a tool_use block's buffered partial-JSON input. An empty buffer
268
+ # means a no-argument tool call, which the wire encodes as an empty object.
269
+ #
270
+ # @param raw [String]
271
+ #
272
+ # @return [Hash{Symbol=>Object}]
273
+ private def decode_tool_use_input(raw)
274
+ raw.empty? ? {} : JSON.parse(raw, symbolize_names: true)
275
+ end
276
+
251
277
  # @api private
252
278
  #
253
279
  # Parse tool use blocks and text blocks with structured output in the message.
@@ -267,7 +293,7 @@ module Anthropic
267
293
  parsed =
268
294
  begin
269
295
  parsed_input = if content.input.is_a?(String)
270
- JSON.parse(content.input, symbolize_names: true)
296
+ decode_tool_use_input(content.input)
271
297
  else
272
298
  content.input
273
299
  end
@@ -57,12 +57,16 @@ module Anthropic
57
57
 
58
58
  variant const: -> { Anthropic::Models::AnthropicBeta::CACHE_DIAGNOSIS_2026_04_07 }
59
59
 
60
+ variant const: -> { Anthropic::Models::AnthropicBeta::DREAMING_2026_04_21 }
61
+
60
62
  variant const: -> { Anthropic::Models::AnthropicBeta::THINKING_TOKEN_COUNT_2026_05_13 }
61
63
 
62
64
  variant const: -> { Anthropic::Models::AnthropicBeta::SERVER_SIDE_FALLBACK_2026_06_01 }
63
65
 
64
66
  variant const: -> { Anthropic::Models::AnthropicBeta::FALLBACK_CREDIT_2026_06_01 }
65
67
 
68
+ variant const: -> { Anthropic::Models::AnthropicBeta::AGENT_MEMORY_2026_07_22 }
69
+
66
70
  # @!method self.variants
67
71
  # @return [Array(String, Symbol)]
68
72
 
@@ -97,9 +101,11 @@ module Anthropic
97
101
  ADVISOR_TOOL_2026_03_01 = :"advisor-tool-2026-03-01"
98
102
  MANAGED_AGENTS_2026_04_01 = :"managed-agents-2026-04-01"
99
103
  CACHE_DIAGNOSIS_2026_04_07 = :"cache-diagnosis-2026-04-07"
104
+ DREAMING_2026_04_21 = :"dreaming-2026-04-21"
100
105
  THINKING_TOKEN_COUNT_2026_05_13 = :"thinking-token-count-2026-05-13"
101
106
  SERVER_SIDE_FALLBACK_2026_06_01 = :"server-side-fallback-2026-06-01"
102
107
  FALLBACK_CREDIT_2026_06_01 = :"fallback-credit-2026-06-01"
108
+ AGENT_MEMORY_2026_07_22 = :"agent-memory-2026-07-22"
103
109
 
104
110
  # @!endgroup
105
111
  end