-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathconverters_anthropic.py
More file actions
540 lines (424 loc) · 17.2 KB
/
Copy pathconverters_anthropic.py
File metadata and controls
540 lines (424 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# -*- coding: utf-8 -*-
# Kiro Gateway
# https://github.com/jwadow/kiro-gateway
# Copyright (C) 2025 Jwadow
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Converters for transforming Anthropic Messages API format to Kiro format.
This module is an adapter layer that converts Anthropic-specific formats
to the unified format used by converters_core.py.
"""
from typing import Any, Dict, List, Optional
from loguru import logger
from kiro.config import HIDDEN_MODELS
from kiro.model_resolver import get_model_id_for_kiro
from kiro.models_anthropic import (
AnthropicMessagesRequest,
AnthropicMessage,
AnthropicTool,
)
from kiro.converters_core import (
UnifiedMessage,
UnifiedTool,
ThinkingConfig,
build_kiro_payload,
extract_text_content,
extract_images_from_content,
)
def convert_anthropic_content_to_text(content: Any) -> str:
"""
Extracts text content from Anthropic message content.
Anthropic content can be:
- String: "Hello, world!"
- List of content blocks: [{"type": "text", "text": "Hello"}]
Args:
content: Anthropic message content
Returns:
Extracted text content
"""
if isinstance(content, str):
return content
if isinstance(content, list):
text_parts = []
for block in content:
if isinstance(block, dict):
if block.get("type") == "text":
text_parts.append(block.get("text", ""))
elif hasattr(block, "type") and block.type == "text":
text_parts.append(block.text)
return "".join(text_parts)
return str(content) if content else ""
def extract_system_prompt(system: Any) -> str:
"""
Extracts system prompt text from Anthropic system field.
Anthropic API supports system in two formats:
1. String: "You are helpful"
2. List of content blocks: [{"type": "text", "text": "...", "cache_control": {...}}]
The second format is used for prompt caching with cache_control.
We extract only the text, ignoring cache_control (not supported by Kiro).
Args:
system: System prompt in string or list format
Returns:
Extracted system prompt as string
"""
if system is None:
return ""
if isinstance(system, str):
return system
if isinstance(system, list):
text_parts = []
for block in system:
if isinstance(block, dict):
# Handle {"type": "text", "text": "...", "cache_control": {...}}
if block.get("type") == "text":
text_parts.append(block.get("text", ""))
elif hasattr(block, "type") and block.type == "text":
# Handle Pydantic model
text_parts.append(getattr(block, "text", ""))
return "\n".join(text_parts)
return str(system)
def extract_tool_results_from_anthropic_content(content: Any) -> List[Dict[str, Any]]:
"""
Extracts tool results from Anthropic message content.
Looks for content blocks with type="tool_result".
Args:
content: Anthropic message content (list of content blocks)
Returns:
List of tool results in unified format
"""
tool_results = []
if not isinstance(content, list):
return tool_results
for block in content:
block_type = None
tool_use_id = None
result_content = ""
if isinstance(block, dict):
block_type = block.get("type")
tool_use_id = block.get("tool_use_id")
result_content = block.get("content", "")
elif hasattr(block, "type"):
block_type = block.type
tool_use_id = getattr(block, "tool_use_id", None)
result_content = getattr(block, "content", "")
if block_type == "tool_result" and tool_use_id:
# Convert content to text if it's a list
if isinstance(result_content, list):
result_content = extract_text_content(result_content)
elif not isinstance(result_content, str):
result_content = str(result_content) if result_content else ""
tool_results.append(
{
"type": "tool_result",
"tool_use_id": tool_use_id,
"content": result_content or "(empty result)",
}
)
return tool_results
def extract_images_from_tool_results(content: Any) -> List[Dict[str, Any]]:
"""
Extracts images from tool_result content blocks.
Tool results in Anthropic format can contain images (e.g., screenshots from browser tools).
This function extracts those images so they can be passed to the model.
Args:
content: Anthropic message content (list of content blocks)
Returns:
List of images in unified format: [{"media_type": "image/jpeg", "data": "base64..."}]
"""
images: List[Dict[str, Any]] = []
if not isinstance(content, list):
return images
for block in content:
block_type = None
result_content = None
if isinstance(block, dict):
block_type = block.get("type")
result_content = block.get("content")
elif hasattr(block, "type"):
block_type = block.type
result_content = getattr(block, "content", None)
if block_type == "tool_result" and isinstance(result_content, list):
# Extract images from the tool_result's content
tool_result_images = extract_images_from_content(result_content)
images.extend(tool_result_images)
if images:
logger.debug(f"Extracted {len(images)} image(s) from tool_result content")
return images
return tool_results
def extract_tool_uses_from_anthropic_content(content: Any) -> List[Dict[str, Any]]:
"""
Extracts tool uses from Anthropic assistant message content.
Looks for content blocks with type="tool_use".
Args:
content: Anthropic message content (list of content blocks)
Returns:
List of tool calls in unified format
"""
tool_calls = []
if not isinstance(content, list):
return tool_calls
for block in content:
block_type = None
tool_id = None
tool_name = None
tool_input = {}
if isinstance(block, dict):
block_type = block.get("type")
tool_id = block.get("id")
tool_name = block.get("name")
tool_input = block.get("input", {})
elif hasattr(block, "type"):
block_type = block.type
tool_id = getattr(block, "id", None)
tool_name = getattr(block, "name", None)
tool_input = getattr(block, "input", {})
if block_type == "tool_use" and tool_id and tool_name:
tool_calls.append(
{
"id": tool_id,
"type": "function",
"function": {
"name": tool_name,
"arguments": tool_input
if isinstance(tool_input, str)
else tool_input,
},
}
)
return tool_calls
def split_inline_system_messages(
messages: List[AnthropicMessage],
) -> tuple[str, List[AnthropicMessage]]:
"""
Separates inline system messages from the conversation.
The Anthropic API carries the system prompt in a dedicated top-level
field, but several clients (Claude Code, Claude Desktop) also place
runtime context in a message with role "system" or "developer" inside
the messages array. Kiro only accepts user/assistant turns, so those
messages are peeled off here and merged into the system prompt, the same
way convert_openai_messages() handles OpenAI system messages.
Args:
messages: List of Anthropic messages, possibly containing inline
system/developer messages
Returns:
Tuple of (inline_system_prompt, conversation_messages)
"""
system_parts = []
conversation = []
for msg in messages:
if msg.role in ("system", "developer"):
text = convert_anthropic_content_to_text(msg.content)
if text:
system_parts.append(text)
else:
conversation.append(msg)
if system_parts:
logger.debug(
f"Merged {len(system_parts)} inline system message(s) into system prompt"
)
return "\n".join(system_parts), conversation
def convert_anthropic_messages(
messages: List[AnthropicMessage],
) -> List[UnifiedMessage]:
"""
Converts Anthropic messages to unified format.
Handles:
- Text content (string or list of text blocks)
- Tool use blocks (assistant messages)
- Tool result blocks (user messages)
Args:
messages: List of Anthropic messages
Returns:
List of messages in unified format
"""
unified_messages = []
total_tool_calls = 0
total_tool_results = 0
total_images = 0
for msg in messages:
role = msg.role
content = msg.content
# Extract text content
text_content = convert_anthropic_content_to_text(content)
# Extract tool-related data and images based on role
tool_calls = None
tool_results = None
images = None
if role == "assistant":
# Assistant messages may contain tool_use blocks
tool_calls = extract_tool_uses_from_anthropic_content(content)
if tool_calls:
total_tool_calls += len(tool_calls)
elif role == "user":
# User messages may contain tool_result blocks and images
tool_results = extract_tool_results_from_anthropic_content(content)
if tool_results:
total_tool_results += len(tool_results)
# Extract images from user messages (both top-level and inside tool_results)
images = extract_images_from_content(content)
# Also extract images from inside tool_result content blocks
# (e.g., screenshots returned by browser MCP tools)
tool_result_images = extract_images_from_tool_results(content)
if tool_result_images:
if images:
images.extend(tool_result_images)
else:
images = tool_result_images
if images:
total_images += len(images)
unified_msg = UnifiedMessage(
role=role,
content=text_content,
tool_calls=tool_calls if tool_calls else None,
tool_results=tool_results if tool_results else None,
images=images if images else None,
)
unified_messages.append(unified_msg)
# Log summary if any tool content or images were found
if total_tool_calls > 0 or total_tool_results > 0 or total_images > 0:
logger.debug(
f"Converted {len(messages)} Anthropic messages: "
f"{total_tool_calls} tool_calls, {total_tool_results} tool_results, {total_images} images"
)
return unified_messages
def convert_anthropic_tools(
tools: Optional[List[AnthropicTool]],
) -> Optional[List[UnifiedTool]]:
"""
Converts Anthropic tools to unified format.
Args:
tools: List of Anthropic tools
Returns:
List of tools in unified format, or None if no tools
"""
if not tools:
return None
unified_tools = []
for tool in tools:
# Handle both dict and Pydantic model
if isinstance(tool, dict):
name = tool.get("name", "")
description = tool.get("description")
input_schema = tool.get("input_schema", {})
else:
name = tool.name
description = tool.description
input_schema = tool.input_schema
unified_tools.append(
UnifiedTool(name=name, description=description, input_schema=input_schema)
)
return unified_tools if unified_tools else None
def extract_thinking_config_from_anthropic(request: AnthropicMessagesRequest) -> ThinkingConfig:
"""
Extract thinking configuration from Anthropic request.
Handles thinking parameter:
- {"type": "enabled", "budget_tokens": N} → enabled with budget
- {"type": "disabled"} → disabled
- None → enabled with default budget
Args:
request: Anthropic MessagesRequest
Returns:
ThinkingConfig for core layer
Examples:
>>> # No thinking specified → use defaults
>>> request = AnthropicMessagesRequest(model="claude-sonnet-4.5", messages=[...], max_tokens=4096)
>>> extract_thinking_config_from_anthropic(request)
ThinkingConfig(enabled=True, budget_tokens=None)
>>> # Explicitly disabled
>>> request.thinking = {"type": "disabled"}
>>> extract_thinking_config_from_anthropic(request)
ThinkingConfig(enabled=False, budget_tokens=None)
>>> # Enabled with custom budget
>>> request.thinking = {"type": "enabled", "budget_tokens": 8000}
>>> extract_thinking_config_from_anthropic(request)
ThinkingConfig(enabled=True, budget_tokens=8000)
"""
if not request.thinking:
# No thinking specified → use defaults
return ThinkingConfig(enabled=True, budget_tokens=None)
if not isinstance(request.thinking, dict):
# Invalid format → use defaults
return ThinkingConfig(enabled=True, budget_tokens=None)
thinking_type = request.thinking.get("type")
if thinking_type == "disabled":
# Explicitly disabled
return ThinkingConfig(enabled=False, budget_tokens=None)
if thinking_type == "enabled":
# Extract budget_tokens
budget = request.thinking.get("budget_tokens")
if budget:
logger.debug(f"Extracted thinking config from Anthropic: type='enabled', budget={budget}")
return ThinkingConfig(enabled=True, budget_tokens=budget)
# Unknown type → use defaults
return ThinkingConfig(enabled=True, budget_tokens=None)
def anthropic_to_kiro(
request: AnthropicMessagesRequest, conversation_id: str, profile_arn: str
) -> dict:
"""
Converts Anthropic Messages API request to Kiro API payload.
This is the main entry point for Anthropic → Kiro conversion.
Key differences from OpenAI:
- System prompt is a separate field (not in messages)
- Content can be string or list of content blocks
- Tool format uses input_schema instead of parameters
Args:
request: Anthropic MessagesRequest
conversation_id: Unique conversation ID
profile_arn: AWS CodeWhisperer profile ARN
Returns:
Payload dictionary for POST request to Kiro API
Raises:
ValueError: If there are no messages to send
"""
# Peel off inline system messages some clients put in the messages array
inline_system_prompt, conversation_messages = split_inline_system_messages(
request.messages
)
# Convert messages to unified format
unified_messages = convert_anthropic_messages(conversation_messages)
# Convert tools to unified format
unified_tools = convert_anthropic_tools(request.tools)
# System prompt is already separate in Anthropic format!
# It can be a string or list of content blocks (for prompt caching)
system_prompt = extract_system_prompt(request.system)
# Top-level system comes first, inline messages are appended in order
if inline_system_prompt:
system_prompt = (
f"{system_prompt}\n{inline_system_prompt}"
if system_prompt
else inline_system_prompt
)
# Get model ID for Kiro API (normalizes + resolves hidden models)
# Pass-through principle: we normalize and send to Kiro, Kiro decides if valid
model_id = get_model_id_for_kiro(request.model, HIDDEN_MODELS)
# Extract thinking configuration from thinking parameter
thinking_config = extract_thinking_config_from_anthropic(request)
logger.debug(
f"Converting Anthropic request: model={request.model} -> {model_id}, "
f"messages={len(unified_messages)}, tools={len(unified_tools) if unified_tools else 0}, "
f"system_prompt_length={len(system_prompt)}, "
f"thinking_enabled={thinking_config.enabled}, thinking_budget={thinking_config.budget_tokens}"
)
# Use core function to build payload
result = build_kiro_payload(
messages=unified_messages,
system_prompt=system_prompt,
model_id=model_id,
tools=unified_tools,
conversation_id=conversation_id,
profile_arn=profile_arn,
thinking_config=thinking_config,
)
return result.payload