code-puppy 0.0.351__py3-none-any.whl → 0.0.353__py3-none-any.whl

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.
@@ -5,6 +5,11 @@ ClaudeCacheAsyncClient: httpx client that tries to patch /v1/messages bodies.
5
5
  We now also expose `patch_anthropic_client_messages` which monkey-patches
6
6
  AsyncAnthropic.messages.create() so we can inject cache_control BEFORE
7
7
  serialization, avoiding httpx/Pydantic internals.
8
+
9
+ This module also handles:
10
+ - Tool name prefixing/unprefixing for Claude Code OAuth compatibility
11
+ - Header transformations (anthropic-beta, user-agent)
12
+ - URL modifications (adding ?beta=true query param)
8
13
  """
9
14
 
10
15
  from __future__ import annotations
@@ -12,8 +17,10 @@ from __future__ import annotations
12
17
  import base64
13
18
  import json
14
19
  import logging
20
+ import re
15
21
  import time
16
22
  from typing import Any, Callable, MutableMapping
23
+ from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
17
24
 
18
25
  import httpx
19
26
 
@@ -22,6 +29,13 @@ logger = logging.getLogger(__name__)
22
29
  # Refresh token if it's older than 1 hour (3600 seconds)
23
30
  TOKEN_MAX_AGE_SECONDS = 3600
24
31
 
32
+ # Tool name prefix for Claude Code OAuth compatibility
33
+ # Tools are prefixed on outgoing requests and unprefixed on incoming responses
34
+ TOOL_PREFIX = "cp_"
35
+
36
+ # User-Agent to send with Claude Code OAuth requests
37
+ CLAUDE_CLI_USER_AGENT = "claude-cli/2.1.2 (external, cli)"
38
+
25
39
  try:
26
40
  from anthropic import AsyncAnthropic
27
41
  except ImportError: # pragma: no cover - optional dep
@@ -29,6 +43,22 @@ except ImportError: # pragma: no cover - optional dep
29
43
 
30
44
 
31
45
  class ClaudeCacheAsyncClient(httpx.AsyncClient):
46
+ """Async HTTP client with Claude Code OAuth transformations.
47
+
48
+ Handles:
49
+ - Cache control injection for prompt caching
50
+ - Tool name prefixing on outgoing requests
51
+ - Tool name unprefixing on incoming streaming responses
52
+ - Header transformations (anthropic-beta, user-agent)
53
+ - URL modifications (adding ?beta=true)
54
+ - Proactive token refresh
55
+ """
56
+
57
+ # Regex pattern for unprefixing tool names in streaming responses
58
+ _TOOL_UNPREFIX_PATTERN = re.compile(
59
+ rf'"name"\s*:\s*"{re.escape(TOOL_PREFIX)}([^"]+)"'
60
+ )
61
+
32
62
  def _get_jwt_age_seconds(self, token: str | None) -> float | None:
33
63
  """Decode a JWT and return its age in seconds.
34
64
 
@@ -107,9 +137,100 @@ class ClaudeCacheAsyncClient(httpx.AsyncClient):
107
137
  )
108
138
  return should_refresh
109
139
 
140
+ @staticmethod
141
+ def _prefix_tool_names(body: bytes) -> bytes | None:
142
+ """Prefix all tool names in the request body with TOOL_PREFIX.
143
+
144
+ This is required for Claude Code OAuth compatibility - tools must be
145
+ prefixed on outgoing requests and unprefixed on incoming responses.
146
+ """
147
+ try:
148
+ data = json.loads(body.decode("utf-8"))
149
+ except Exception:
150
+ return None
151
+
152
+ if not isinstance(data, dict):
153
+ return None
154
+
155
+ tools = data.get("tools")
156
+ if not isinstance(tools, list) or not tools:
157
+ return None
158
+
159
+ modified = False
160
+ for tool in tools:
161
+ if isinstance(tool, dict) and "name" in tool:
162
+ name = tool["name"]
163
+ if name and not name.startswith(TOOL_PREFIX):
164
+ tool["name"] = f"{TOOL_PREFIX}{name}"
165
+ modified = True
166
+
167
+ if not modified:
168
+ return None
169
+
170
+ return json.dumps(data).encode("utf-8")
171
+
172
+ def _unprefix_tool_names_in_text(self, text: str) -> str:
173
+ """Remove TOOL_PREFIX from tool names in streaming response text."""
174
+ return self._TOOL_UNPREFIX_PATTERN.sub(r'"name": "\1"', text)
175
+
176
+ @staticmethod
177
+ def _transform_headers_for_claude_code(
178
+ headers: MutableMapping[str, str],
179
+ ) -> None:
180
+ """Transform headers for Claude Code OAuth compatibility.
181
+
182
+ - Sets user-agent to claude-cli
183
+ - Merges anthropic-beta headers appropriately
184
+ - Removes x-api-key (using Bearer auth instead)
185
+ """
186
+ # Set user-agent
187
+ headers["user-agent"] = CLAUDE_CLI_USER_AGENT
188
+
189
+ # Handle anthropic-beta header
190
+ incoming_beta = headers.get("anthropic-beta", "")
191
+ incoming_betas = [b.strip() for b in incoming_beta.split(",") if b.strip()]
192
+
193
+ # Check if claude-code beta was explicitly requested
194
+ include_claude_code = "claude-code-20250219" in incoming_betas
195
+
196
+ # Build merged betas list
197
+ merged_betas = [
198
+ "oauth-2025-04-20",
199
+ "interleaved-thinking-2025-05-14",
200
+ ]
201
+ if include_claude_code:
202
+ merged_betas.append("claude-code-20250219")
203
+
204
+ headers["anthropic-beta"] = ",".join(merged_betas)
205
+
206
+ # Remove x-api-key if present (we use Bearer auth)
207
+ for key in ["x-api-key", "X-API-Key", "X-Api-Key"]:
208
+ if key in headers:
209
+ del headers[key]
210
+
211
+ @staticmethod
212
+ def _add_beta_query_param(url: httpx.URL) -> httpx.URL:
213
+ """Add ?beta=true query parameter to the URL if not already present."""
214
+ # Parse the URL
215
+ parsed = urlparse(str(url))
216
+ query_params = parse_qs(parsed.query)
217
+
218
+ # Only add if not already present
219
+ if "beta" not in query_params:
220
+ query_params["beta"] = ["true"]
221
+ # Rebuild query string
222
+ new_query = urlencode(query_params, doseq=True)
223
+ # Rebuild URL
224
+ new_parsed = parsed._replace(query=new_query)
225
+ return httpx.URL(urlunparse(new_parsed))
226
+
227
+ return url
228
+
110
229
  async def send(
111
230
  self, request: httpx.Request, *args: Any, **kwargs: Any
112
231
  ) -> httpx.Response: # type: ignore[override]
232
+ is_messages_endpoint = request.url.path.endswith("/v1/messages")
233
+
113
234
  # Proactive token refresh: check JWT age before every request
114
235
  if not request.extensions.get("claude_oauth_refresh_attempted"):
115
236
  try:
@@ -131,50 +252,88 @@ class ClaudeCacheAsyncClient(httpx.AsyncClient):
131
252
  except Exception as exc:
132
253
  logger.debug("Error during proactive token refresh check: %s", exc)
133
254
 
134
- try:
135
- if request.url.path.endswith("/v1/messages"):
255
+ # Apply Claude Code OAuth transformations for /v1/messages
256
+ if is_messages_endpoint:
257
+ try:
136
258
  body_bytes = self._extract_body_bytes(request)
259
+ headers = dict(request.headers)
260
+ url = request.url
261
+ body_modified = False
262
+ headers_modified = False
263
+
264
+ # 1. Transform headers for Claude Code OAuth
265
+ self._transform_headers_for_claude_code(headers)
266
+ headers_modified = True
267
+
268
+ # 2. Add ?beta=true query param
269
+ url = self._add_beta_query_param(url)
270
+
271
+ # 3. Prefix tool names in request body
137
272
  if body_bytes:
138
- updated = self._inject_cache_control(body_bytes)
139
- if updated is not None:
140
- # Rebuild a request with the updated body and transplant internals
141
- try:
142
- rebuilt = self.build_request(
143
- method=request.method,
144
- url=request.url,
145
- headers=request.headers,
146
- content=updated,
147
- )
148
-
149
- # Copy core internals so httpx uses the modified body/stream
150
- if hasattr(rebuilt, "_content"):
151
- setattr(request, "_content", rebuilt._content) # type: ignore[attr-defined]
152
- if hasattr(rebuilt, "stream"):
153
- request.stream = rebuilt.stream
154
- if hasattr(rebuilt, "extensions"):
155
- request.extensions = rebuilt.extensions
156
-
157
- # Ensure Content-Length matches the new body
158
- request.headers["Content-Length"] = str(len(updated))
159
-
160
- except Exception:
161
- # Swallow instrumentation errors; do not break real calls.
162
- pass
163
- except Exception:
164
- # Swallow wrapper errors; do not break real calls.
165
- pass
273
+ prefixed_body = self._prefix_tool_names(body_bytes)
274
+ if prefixed_body is not None:
275
+ body_bytes = prefixed_body
276
+ body_modified = True
277
+
278
+ # 4. Inject cache_control
279
+ cached_body = self._inject_cache_control(body_bytes)
280
+ if cached_body is not None:
281
+ body_bytes = cached_body
282
+ body_modified = True
283
+
284
+ # Rebuild request if anything changed
285
+ if body_modified or headers_modified or url != request.url:
286
+ try:
287
+ rebuilt = self.build_request(
288
+ method=request.method,
289
+ url=url,
290
+ headers=headers,
291
+ content=body_bytes,
292
+ )
293
+
294
+ # Copy core internals so httpx uses the modified body/stream
295
+ if hasattr(rebuilt, "_content"):
296
+ setattr(request, "_content", rebuilt._content) # type: ignore[attr-defined]
297
+ if hasattr(rebuilt, "stream"):
298
+ request.stream = rebuilt.stream
299
+ if hasattr(rebuilt, "extensions"):
300
+ request.extensions = rebuilt.extensions
301
+
302
+ # Update URL
303
+ request.url = url
304
+
305
+ # Update headers
306
+ for key, value in headers.items():
307
+ request.headers[key] = value
308
+
309
+ # Ensure Content-Length matches the new body
310
+ if body_bytes:
311
+ request.headers["Content-Length"] = str(len(body_bytes))
312
+
313
+ except Exception as exc:
314
+ logger.debug("Error rebuilding request: %s", exc)
315
+
316
+ except Exception as exc:
317
+ logger.debug("Error in Claude Code transformations: %s", exc)
318
+
319
+ # Send the request
166
320
  response = await super().send(request, *args, **kwargs)
321
+
322
+ # Transform streaming response to unprefix tool names
323
+ if is_messages_endpoint and response.status_code == 200:
324
+ try:
325
+ response = self._wrap_response_with_tool_unprefixing(response, request)
326
+ except Exception as exc:
327
+ logger.debug("Error wrapping response for tool unprefixing: %s", exc)
328
+
329
+ # Handle auth errors with token refresh
167
330
  try:
168
- # Check for both 401 and 400 - Anthropic/Cloudflare may return 400 for auth errors
169
- # Also check if it's a Cloudflare HTML error response
170
331
  if response.status_code in (400, 401) and not request.extensions.get(
171
332
  "claude_oauth_refresh_attempted"
172
333
  ):
173
- # Determine if this is an auth error (including Cloudflare HTML errors)
174
334
  is_auth_error = response.status_code == 401
175
335
 
176
336
  if response.status_code == 400:
177
- # Check if this is a Cloudflare HTML error
178
337
  is_auth_error = self._is_cloudflare_html_error(response)
179
338
  if is_auth_error:
180
339
  logger.info(
@@ -203,8 +362,64 @@ class ClaudeCacheAsyncClient(httpx.AsyncClient):
203
362
  logger.warning("Token refresh failed, returning original error")
204
363
  except Exception as exc:
205
364
  logger.debug("Error during token refresh attempt: %s", exc)
365
+
206
366
  return response
207
367
 
368
+ def _wrap_response_with_tool_unprefixing(
369
+ self, response: httpx.Response, request: httpx.Request
370
+ ) -> httpx.Response:
371
+ """Wrap a streaming response to unprefix tool names.
372
+
373
+ Creates a new response with a transformed stream that removes the
374
+ TOOL_PREFIX from tool names in the response body.
375
+ """
376
+ original_stream = response.stream
377
+ unprefix_fn = self._unprefix_tool_names_in_text
378
+
379
+ class UnprefixingStream(httpx.AsyncByteStream):
380
+ """Async byte stream that unprefixes tool names.
381
+
382
+ Inherits from httpx.AsyncByteStream to ensure proper stream interface.
383
+ """
384
+
385
+ def __init__(self, inner_stream: Any) -> None:
386
+ self._inner = inner_stream
387
+
388
+ async def __aiter__(self):
389
+ async for chunk in self._inner:
390
+ if isinstance(chunk, bytes):
391
+ text = chunk.decode("utf-8", errors="replace")
392
+ text = unprefix_fn(text)
393
+ yield text.encode("utf-8")
394
+ else:
395
+ yield chunk
396
+
397
+ async def aclose(self) -> None:
398
+ if hasattr(self._inner, "aclose"):
399
+ try:
400
+ result = self._inner.aclose()
401
+ # Handle both sync and async aclose
402
+ if hasattr(result, "__await__"):
403
+ await result
404
+ except Exception:
405
+ pass # Ignore close errors
406
+ elif hasattr(self._inner, "close"):
407
+ try:
408
+ self._inner.close()
409
+ except Exception:
410
+ pass
411
+
412
+ # Create a new response with the transformed stream
413
+ # Must include request for raise_for_status() to work
414
+ new_response = httpx.Response(
415
+ status_code=response.status_code,
416
+ headers=response.headers,
417
+ stream=UnprefixingStream(original_stream),
418
+ extensions=response.extensions,
419
+ request=request,
420
+ )
421
+ return new_response
422
+
208
423
  @staticmethod
209
424
  def _extract_body_bytes(request: httpx.Request) -> bytes | None:
210
425
  # Try public content first
@@ -33,6 +33,7 @@ from code_puppy.messaging import (
33
33
  set_session_context,
34
34
  )
35
35
  from code_puppy.model_factory import ModelFactory, make_model_settings
36
+ from code_puppy.model_utils import is_claude_code_model
36
37
  from code_puppy.tools.common import generate_group_id
37
38
 
38
39
  # Set to track active subagent invocation tasks
@@ -539,6 +540,15 @@ def register_invoke_agent(agent):
539
540
  # Run the temporary agent with the provided prompt as an asyncio task
540
541
  # Pass the message_history from the session to continue the conversation
541
542
  workflow_id = None # Track for potential cancellation
543
+
544
+ # For claude-code models, we MUST use streaming to properly handle
545
+ # tool name unprefixing in the HTTP transport layer
546
+ stream_handler = None
547
+ if is_claude_code_model(model_name):
548
+ from code_puppy.agents.event_stream_handler import event_stream_handler
549
+
550
+ stream_handler = event_stream_handler
551
+
542
552
  if get_use_dbos():
543
553
  # Generate a unique workflow ID for DBOS - ensures no collisions in back-to-back calls
544
554
  workflow_id = _generate_dbos_workflow_id(group_id)
@@ -554,6 +564,7 @@ def register_invoke_agent(agent):
554
564
  prompt,
555
565
  message_history=message_history,
556
566
  usage_limits=UsageLimits(request_limit=get_message_limit()),
567
+ event_stream_handler=stream_handler,
557
568
  )
558
569
  )
559
570
  _active_subagent_tasks.add(task)
@@ -563,6 +574,7 @@ def register_invoke_agent(agent):
563
574
  prompt,
564
575
  message_history=message_history,
565
576
  usage_limits=UsageLimits(request_limit=get_message_limit()),
577
+ event_stream_handler=stream_handler,
566
578
  )
567
579
  )
568
580
  _active_subagent_tasks.add(task)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: code-puppy
3
- Version: 0.0.351
3
+ Version: 0.0.353
4
4
  Summary: Code generation agent
5
5
  Project-URL: repository, https://github.com/mpfaffenberger/code_puppy
6
6
  Project-URL: HomePage, https://github.com/mpfaffenberger/code_puppy
@@ -2,7 +2,7 @@ code_puppy/__init__.py,sha256=xMPewo9RNHb3yfFNIk5WCbv2cvSPtJOCgK2-GqLbNnU,373
2
2
  code_puppy/__main__.py,sha256=pDVssJOWP8A83iFkxMLY9YteHYat0EyWDQqMkKHpWp4,203
3
3
  code_puppy/callbacks.py,sha256=hqTV--dNxG5vwWWm3MrEjmb8MZuHFFdmHePl23NXPHk,8621
4
4
  code_puppy/chatgpt_codex_client.py,sha256=Om0ANB_kpHubhCwNzF9ENf8RvKBqs0IYzBLl_SNw0Vk,9833
5
- code_puppy/claude_cache_client.py,sha256=MLIRSJP428r9IK_aV6XyCXrCfQnNti32U60psPymLM4,14860
5
+ code_puppy/claude_cache_client.py,sha256=Gl6um5ZaKpcnxOvoFSM8Lwm_Vu4-VyWz8Nli8DnRLa4,22508
6
6
  code_puppy/cli_runner.py,sha256=w5CLKgQYYaT7My3Cga2StXYol-u6DBxNzzUuhhsfhsA,34952
7
7
  code_puppy/config.py,sha256=RlnrLkyFXm7h2Htf8rQA7vqoAyzLPMrESle417uLmFw,52373
8
8
  code_puppy/error_logging.py,sha256=a80OILCUtJhexI6a9GM-r5LqIdjvSRzggfgPp2jv1X0,3297
@@ -160,7 +160,7 @@ code_puppy/plugins/shell_safety/register_callbacks.py,sha256=W3v664RR48Fdbbbltf_
160
160
  code_puppy/prompts/antigravity_system_prompt.md,sha256=ZaTfRyY57ttROyZMmOBtqZQu1to7sdTNTv8_0fTgPNw,6807
161
161
  code_puppy/prompts/codex_system_prompt.md,sha256=hEFTCziroLqZmqNle5kG34A8kvTteOWezCiVrAEKhE0,24400
162
162
  code_puppy/tools/__init__.py,sha256=eQY-GL2ToV9IdRKlrnWlcPLyncJyU1VGZxq9yy0twNI,6137
163
- code_puppy/tools/agent_tools.py,sha256=faN0QPwfUvQFwN3Pv8kVL4wN9rV3IX0PRM_lSKhcGQQ,23570
163
+ code_puppy/tools/agent_tools.py,sha256=3MI7ZvlBBbu2CbZCPIZfW0q4lnD_mS-wdDygQt9mQIM,24123
164
164
  code_puppy/tools/command_runner.py,sha256=3qXVnVTaBPia6y2D29As47_TRKgpyCj82yMFK-8UUYc,44954
165
165
  code_puppy/tools/common.py,sha256=IYf-KOcP5eN2MwTlpULSXNATn7GzloAKl7_M1Uyfe4Y,40360
166
166
  code_puppy/tools/display.py,sha256=T2bIyb233eds0q8C1jZRl6NjwERrLgT_APhEz9drN1w,2472
@@ -177,10 +177,10 @@ code_puppy/tools/browser/browser_scripts.py,sha256=sNb8eLEyzhasy5hV4B9OjM8yIVMLV
177
177
  code_puppy/tools/browser/browser_workflows.py,sha256=nitW42vCf0ieTX1gLabozTugNQ8phtoFzZbiAhw1V90,6491
178
178
  code_puppy/tools/browser/camoufox_manager.py,sha256=RZjGOEftE5sI_tsercUyXFSZI2wpStXf-q0PdYh2G3I,8680
179
179
  code_puppy/tools/browser/vqa_agent.py,sha256=DBn9HKloILqJSTSdNZzH_PYWT0B2h9VwmY6akFQI_uU,2913
180
- code_puppy-0.0.351.data/data/code_puppy/models.json,sha256=FMQdE_yvP_8y0xxt3K918UkFL9cZMYAqW1SfXcQkU_k,3105
181
- code_puppy-0.0.351.data/data/code_puppy/models_dev_api.json,sha256=wHjkj-IM_fx1oHki6-GqtOoCrRMR0ScK0f-Iz0UEcy8,548187
182
- code_puppy-0.0.351.dist-info/METADATA,sha256=CeEJbr8iSJB0kJ-s-PqQ-Qcef7Bkr0leg-cWB8OI-FA,27572
183
- code_puppy-0.0.351.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
184
- code_puppy-0.0.351.dist-info/entry_points.txt,sha256=Tp4eQC99WY3HOKd3sdvb22vZODRq0XkZVNpXOag_KdI,91
185
- code_puppy-0.0.351.dist-info/licenses/LICENSE,sha256=31u8x0SPgdOq3izJX41kgFazWsM43zPEF9eskzqbJMY,1075
186
- code_puppy-0.0.351.dist-info/RECORD,,
180
+ code_puppy-0.0.353.data/data/code_puppy/models.json,sha256=FMQdE_yvP_8y0xxt3K918UkFL9cZMYAqW1SfXcQkU_k,3105
181
+ code_puppy-0.0.353.data/data/code_puppy/models_dev_api.json,sha256=wHjkj-IM_fx1oHki6-GqtOoCrRMR0ScK0f-Iz0UEcy8,548187
182
+ code_puppy-0.0.353.dist-info/METADATA,sha256=SZGW2VJQ_sk4iAb3UBANyh8Tzit9QwP47l1XHaRIrjY,27572
183
+ code_puppy-0.0.353.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
184
+ code_puppy-0.0.353.dist-info/entry_points.txt,sha256=Tp4eQC99WY3HOKd3sdvb22vZODRq0XkZVNpXOag_KdI,91
185
+ code_puppy-0.0.353.dist-info/licenses/LICENSE,sha256=31u8x0SPgdOq3izJX41kgFazWsM43zPEF9eskzqbJMY,1075
186
+ code_puppy-0.0.353.dist-info/RECORD,,