@@ -272,64 +272,56 @@ def _map_message(self, messages: list[ModelMessage]) -> tuple[str, list[MessageP
272
272
anthropic_messages : list [MessageParam ] = []
273
273
for m in messages :
274
274
if isinstance (m , ModelRequest ):
275
- for part in m .parts :
276
- if isinstance (part , SystemPromptPart ):
277
- system_prompt += part .content
278
- elif isinstance (part , UserPromptPart ):
279
- anthropic_messages .append (MessageParam (role = 'user' , content = part .content ))
280
- elif isinstance (part , ToolReturnPart ):
281
- anthropic_messages .append (
282
- MessageParam (
283
- role = 'user' ,
284
- content = [
285
- ToolResultBlockParam (
286
- tool_use_id = _guard_tool_call_id (t = part , model_source = 'Anthropic' ),
287
- type = 'tool_result' ,
288
- content = part .model_response_str (),
289
- is_error = False ,
290
- )
291
- ],
292
- )
275
+ user_content_params : list [ToolResultBlockParam | TextBlockParam ] = []
276
+ for request_part in m .parts :
277
+ if isinstance (request_part , SystemPromptPart ):
278
+ system_prompt += request_part .content
279
+ elif isinstance (request_part , UserPromptPart ):
280
+ text_block_param = TextBlockParam (type = 'text' , text = request_part .content )
281
+ user_content_params .append (text_block_param )
282
+ elif isinstance (request_part , ToolReturnPart ):
283
+ tool_result_block_param = ToolResultBlockParam (
284
+ tool_use_id = _guard_tool_call_id (t = request_part , model_source = 'Anthropic' ),
285
+ type = 'tool_result' ,
286
+ content = request_part .model_response_str (),
287
+ is_error = False ,
293
288
)
294
- elif isinstance (part , RetryPromptPart ):
295
- if part .tool_name is None :
296
- anthropic_messages .append (MessageParam (role = 'user' , content = part .model_response ()))
289
+ user_content_params .append (tool_result_block_param )
290
+ elif isinstance (request_part , RetryPromptPart ):
291
+ if request_part .tool_name is None :
292
+ retry_param = TextBlockParam (type = 'text' , text = request_part .model_response ())
297
293
else :
298
- anthropic_messages .append (
299
- MessageParam (
300
- role = 'user' ,
301
- content = [
302
- ToolResultBlockParam (
303
- tool_use_id = _guard_tool_call_id (t = part , model_source = 'Anthropic' ),
304
- type = 'tool_result' ,
305
- content = part .model_response (),
306
- is_error = True ,
307
- ),
308
- ],
309
- )
294
+ retry_param = ToolResultBlockParam (
295
+ tool_use_id = _guard_tool_call_id (t = request_part , model_source = 'Anthropic' ),
296
+ type = 'tool_result' ,
297
+ content = request_part .model_response (),
298
+ is_error = True ,
310
299
)
300
+ user_content_params .append (retry_param )
301
+ anthropic_messages .append (
302
+ MessageParam (
303
+ role = 'user' ,
304
+ content = user_content_params ,
305
+ )
306
+ )
311
307
elif isinstance (m , ModelResponse ):
312
- content : list [TextBlockParam | ToolUseBlockParam ] = []
313
- for item in m .parts :
314
- if isinstance (item , TextPart ):
315
- content .append (TextBlockParam (text = item .content , type = 'text' ))
308
+ assistant_content_params : list [TextBlockParam | ToolUseBlockParam ] = []
309
+ for response_part in m .parts :
310
+ if isinstance (response_part , TextPart ):
311
+ assistant_content_params .append (TextBlockParam (text = response_part .content , type = 'text' ))
316
312
else :
317
- assert isinstance (item , ToolCallPart )
318
- content .append (self ._map_tool_call (item ))
319
- anthropic_messages .append (MessageParam (role = 'assistant' , content = content ))
313
+ tool_use_block_param = ToolUseBlockParam (
314
+ id = _guard_tool_call_id (t = response_part , model_source = 'Anthropic' ),
315
+ type = 'tool_use' ,
316
+ name = response_part .tool_name ,
317
+ input = response_part .args_as_dict (),
318
+ )
319
+ assistant_content_params .append (tool_use_block_param )
320
+ anthropic_messages .append (MessageParam (role = 'assistant' , content = assistant_content_params ))
320
321
else :
321
322
assert_never (m )
322
323
return system_prompt , anthropic_messages
323
324
324
- @staticmethod
325
- def _map_tool_call (t : ToolCallPart ) -> ToolUseBlockParam :
326
- return ToolUseBlockParam (
327
- id = _guard_tool_call_id (t = t , model_source = 'Anthropic' ),
328
- type = 'tool_use' ,
329
- name = t .tool_name ,
330
- input = t .args_as_dict (),
331
- )
332
-
333
325
@staticmethod
334
326
def _map_tool_definition (f : ToolDefinition ) -> ToolParam :
335
327
return {
0 commit comments