anthropic 1.1.1 → 1.9.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +170 -0
- data/README.md +32 -16
- data/lib/anthropic/client.rb +5 -2
- data/lib/anthropic/errors.rb +22 -0
- data/lib/anthropic/helpers/bedrock/client.rb +34 -4
- data/lib/anthropic/helpers/input_schema/array_of.rb +41 -0
- data/lib/anthropic/helpers/input_schema/base_model.rb +86 -0
- data/lib/anthropic/helpers/input_schema/boolean.rb +29 -0
- data/lib/anthropic/helpers/input_schema/enum_of.rb +63 -0
- data/lib/anthropic/helpers/input_schema/json_schema_converter.rb +199 -0
- data/lib/anthropic/helpers/input_schema/parsed_json.rb +39 -0
- data/lib/anthropic/helpers/input_schema/property_mapping.rb +47 -0
- data/lib/anthropic/helpers/input_schema/union_of.rb +56 -0
- data/lib/anthropic/helpers/input_schema.rb +16 -0
- data/lib/anthropic/helpers/streaming/events.rb +141 -0
- data/lib/anthropic/helpers/streaming/message_stream.rb +278 -0
- data/lib/anthropic/helpers/streaming.rb +37 -0
- data/lib/anthropic/helpers/vertex/client.rb +4 -1
- data/lib/anthropic/input_schema.rb +10 -0
- data/lib/anthropic/internal/stream.rb +6 -3
- data/lib/anthropic/internal/transport/base_client.rb +30 -5
- data/lib/anthropic/internal/transport/pooled_net_requester.rb +1 -9
- data/lib/anthropic/internal/type/array_of.rb +7 -1
- data/lib/anthropic/internal/type/base_model.rb +81 -32
- data/lib/anthropic/internal/type/base_stream.rb +3 -1
- data/lib/anthropic/internal/type/boolean.rb +7 -1
- data/lib/anthropic/internal/type/converter.rb +69 -34
- data/lib/anthropic/internal/type/enum.rb +16 -5
- data/lib/anthropic/internal/type/file_input.rb +6 -1
- data/lib/anthropic/internal/type/hash_of.rb +7 -1
- data/lib/anthropic/internal/type/union.rb +26 -16
- data/lib/anthropic/internal/type/unknown.rb +7 -1
- data/lib/anthropic/internal/util.rb +10 -11
- data/lib/anthropic/models/anthropic_beta.rb +3 -0
- data/lib/anthropic/models/beta/beta_base64_pdf_block.rb +1 -65
- data/lib/anthropic/models/beta/beta_bash_code_execution_output_block.rb +25 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_output_block_param.rb +25 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_result_block.rb +44 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_result_block_param.rb +44 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_block.rb +43 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_block_param.rb +53 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_error.rb +39 -0
- data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_error_param.rb +39 -0
- data/lib/anthropic/models/beta/beta_citation_char_location.rb +7 -1
- data/lib/anthropic/models/beta/beta_citation_config.rb +19 -0
- data/lib/anthropic/models/beta/beta_citation_content_block_location.rb +7 -1
- data/lib/anthropic/models/beta/beta_citation_page_location.rb +7 -1
- data/lib/anthropic/models/beta/beta_citation_search_result_location.rb +55 -0
- data/lib/anthropic/models/beta/beta_citation_search_result_location_param.rb +55 -0
- data/lib/anthropic/models/beta/beta_citations_delta.rb +5 -3
- data/lib/anthropic/models/beta/beta_code_execution_tool_20250825.rb +40 -0
- data/lib/anthropic/models/beta/beta_content_block.rb +12 -5
- data/lib/anthropic/models/beta/beta_content_block_param.rb +28 -17
- data/lib/anthropic/models/beta/beta_document_block.rb +56 -0
- data/lib/anthropic/models/beta/beta_message.rb +6 -2
- data/lib/anthropic/models/beta/beta_message_param.rb +3 -3
- data/lib/anthropic/models/beta/beta_raw_content_block_start_event.rb +14 -7
- data/lib/anthropic/models/beta/beta_request_document_block.rb +75 -0
- data/lib/anthropic/models/beta/beta_search_result_block_param.rb +55 -0
- data/lib/anthropic/models/beta/beta_server_tool_usage.rb +9 -1
- data/lib/anthropic/models/beta/beta_server_tool_use_block.rb +3 -0
- data/lib/anthropic/models/beta/beta_server_tool_use_block_param.rb +3 -0
- data/lib/anthropic/models/beta/beta_text_block.rb +2 -2
- data/lib/anthropic/models/beta/beta_text_block_param.rb +2 -2
- data/lib/anthropic/models/beta/beta_text_citation.rb +3 -1
- data/lib/anthropic/models/beta/beta_text_citation_param.rb +3 -1
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_create_result_block.rb +25 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_create_result_block_param.rb +26 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block.rb +49 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block_param.rb +50 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block.rb +47 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block_param.rb +57 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error.rb +45 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error_param.rb +46 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_view_result_block.rb +61 -0
- data/lib/anthropic/models/beta/beta_text_editor_code_execution_view_result_block_param.rb +62 -0
- data/lib/anthropic/models/beta/beta_tool.rb +7 -1
- data/lib/anthropic/models/beta/beta_tool_result_block_param.rb +8 -4
- data/lib/anthropic/models/beta/beta_tool_text_editor_20250728.rb +49 -0
- data/lib/anthropic/models/beta/beta_tool_union.rb +12 -6
- data/lib/anthropic/models/beta/beta_web_fetch_block.rb +42 -0
- data/lib/anthropic/models/beta/beta_web_fetch_block_param.rb +42 -0
- data/lib/anthropic/models/beta/beta_web_fetch_tool_20250910.rb +82 -0
- data/lib/anthropic/models/beta/beta_web_fetch_tool_result_block.rb +43 -0
- data/lib/anthropic/models/beta/beta_web_fetch_tool_result_block_param.rb +53 -0
- data/lib/anthropic/models/beta/beta_web_fetch_tool_result_error_block.rb +25 -0
- data/lib/anthropic/models/beta/beta_web_fetch_tool_result_error_block_param.rb +25 -0
- data/lib/anthropic/models/beta/beta_web_fetch_tool_result_error_code.rb +25 -0
- data/lib/anthropic/models/beta/message_count_tokens_params.rb +23 -36
- data/lib/anthropic/models/beta/message_create_params.rb +10 -27
- data/lib/anthropic/models/beta/messages/batch_create_params.rb +13 -37
- data/lib/anthropic/models/beta/messages/beta_message_batch.rb +1 -4
- data/lib/anthropic/models/beta_error_response.rb +7 -1
- data/lib/anthropic/models/cache_control_ephemeral.rb +39 -1
- data/lib/anthropic/models/cache_creation.rb +24 -0
- data/lib/anthropic/models/citation_char_location.rb +7 -1
- data/lib/anthropic/models/citation_content_block_location.rb +7 -1
- data/lib/anthropic/models/citation_page_location.rb +7 -1
- data/lib/anthropic/models/citation_search_result_location_param.rb +51 -0
- data/lib/anthropic/models/citations_delta.rb +5 -3
- data/lib/anthropic/models/citations_search_result_location.rb +51 -0
- data/lib/anthropic/models/content_block.rb +5 -5
- data/lib/anthropic/models/content_block_param.rb +14 -11
- data/lib/anthropic/models/document_block_param.rb +2 -2
- data/lib/anthropic/models/error_response.rb +7 -1
- data/lib/anthropic/models/message.rb +6 -2
- data/lib/anthropic/models/message_count_tokens_params.rb +10 -27
- data/lib/anthropic/models/message_count_tokens_tool.rb +5 -1
- data/lib/anthropic/models/message_create_params.rb +10 -27
- data/lib/anthropic/models/message_param.rb +3 -3
- data/lib/anthropic/models/messages/batch_create_params.rb +11 -31
- data/lib/anthropic/models/model.rb +16 -21
- data/lib/anthropic/models/raw_content_block_start_event.rb +7 -7
- data/lib/anthropic/models/search_result_block_param.rb +51 -0
- data/lib/anthropic/models/text_block.rb +2 -2
- data/lib/anthropic/models/text_block_param.rb +2 -2
- data/lib/anthropic/models/text_citation.rb +3 -1
- data/lib/anthropic/models/text_citation_param.rb +3 -1
- data/lib/anthropic/models/tool.rb +7 -1
- data/lib/anthropic/models/tool_result_block_param.rb +8 -4
- data/lib/anthropic/models/tool_text_editor_20250429.rb +36 -0
- data/lib/anthropic/models/tool_text_editor_20250728.rb +45 -0
- data/lib/anthropic/models/tool_union.rb +5 -1
- data/lib/anthropic/models/tool_use_block.rb +14 -0
- data/lib/anthropic/models/usage.rb +9 -1
- data/lib/anthropic/models.rb +16 -4
- data/lib/anthropic/resources/beta/messages.rb +73 -6
- data/lib/anthropic/resources/messages.rb +152 -7
- data/lib/anthropic/streaming.rb +5 -0
- data/lib/anthropic/version.rb +1 -1
- data/lib/anthropic.rb +54 -0
- data/rbi/anthropic/errors.rbi +18 -2
- data/rbi/anthropic/helpers/bedrock/client.rbi +17 -6
- data/rbi/anthropic/helpers/input_schema/array_of.rbi +16 -0
- data/rbi/anthropic/helpers/input_schema/base_model.rbi +21 -0
- data/rbi/anthropic/helpers/input_schema/boolean.rbi +11 -0
- data/rbi/anthropic/helpers/input_schema/enum_of.rbi +30 -0
- data/rbi/anthropic/helpers/input_schema/json_schema_converter.rbi +89 -0
- data/rbi/anthropic/helpers/input_schema/union_of.rbi +23 -0
- data/rbi/anthropic/helpers/streaming/events.rbi +95 -0
- data/rbi/anthropic/helpers/streaming/message_stream.rbi +73 -0
- data/rbi/anthropic/helpers/structured_output.rbi +16 -0
- data/rbi/anthropic/helpers/vertex/client.rbi +17 -6
- data/rbi/anthropic/input_schema.rbi +12 -0
- data/rbi/anthropic/internal/transport/base_client.rbi +1 -1
- data/rbi/anthropic/internal/type/base_stream.rbi +8 -1
- data/rbi/anthropic/internal/type/boolean.rbi +2 -0
- data/rbi/anthropic/internal/type/converter.rbi +69 -15
- data/rbi/anthropic/internal/type/union.rbi +14 -2
- data/rbi/anthropic/internal/type/unknown.rbi +2 -0
- data/rbi/anthropic/internal/util.rbi +2 -0
- data/rbi/anthropic/models/anthropic_beta.rbi +2 -0
- data/rbi/anthropic/models/beta/beta_base64_pdf_block.rbi +1 -128
- data/rbi/anthropic/models/beta/beta_bash_code_execution_output_block.rbi +33 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_output_block_param.rbi +34 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_result_block.rbi +72 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_result_block_param.rbi +77 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_block.rbi +86 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_block_param.rbi +110 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_error.rbi +101 -0
- data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_error_param.rbi +101 -0
- data/rbi/anthropic/models/beta/beta_citation_char_location.rbi +6 -0
- data/rbi/anthropic/models/beta/beta_citation_config.rbi +30 -0
- data/rbi/anthropic/models/beta/beta_citation_content_block_location.rbi +6 -0
- data/rbi/anthropic/models/beta/beta_citation_page_location.rbi +6 -0
- data/rbi/anthropic/models/beta/beta_citation_search_result_location.rbi +78 -0
- data/rbi/anthropic/models/beta/beta_citation_search_result_location_param.rbi +79 -0
- data/rbi/anthropic/models/beta/beta_citations_delta.rbi +4 -2
- data/rbi/anthropic/models/beta/beta_code_execution_tool_20250825.rbi +72 -0
- data/rbi/anthropic/models/beta/beta_content_block.rbi +6 -3
- data/rbi/anthropic/models/beta/beta_content_block_param.rbi +11 -7
- data/rbi/anthropic/models/beta/beta_document_block.rbi +95 -0
- data/rbi/anthropic/models/beta/beta_message.rbi +14 -3
- data/rbi/anthropic/models/beta/beta_raw_content_block_start_event.rbi +12 -6
- data/rbi/anthropic/models/beta/beta_request_document_block.rbi +142 -0
- data/rbi/anthropic/models/beta/beta_search_result_block_param.rbi +91 -0
- data/rbi/anthropic/models/beta/beta_server_tool_usage.rbi +17 -2
- data/rbi/anthropic/models/beta/beta_server_tool_use_block.rbi +15 -0
- data/rbi/anthropic/models/beta/beta_server_tool_use_block_param.rbi +15 -0
- data/rbi/anthropic/models/beta/beta_text_block.rbi +2 -1
- data/rbi/anthropic/models/beta/beta_text_block_param.rbi +6 -3
- data/rbi/anthropic/models/beta/beta_text_citation.rbi +2 -1
- data/rbi/anthropic/models/beta/beta_text_citation_param.rbi +2 -1
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_create_result_block.rbi +41 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_create_result_block_param.rbi +41 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block.rbi +73 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block_param.rbi +73 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block.rbi +90 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block_param.rbi +118 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error.rbi +110 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error_param.rbi +110 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_view_result_block.rbi +118 -0
- data/rbi/anthropic/models/beta/beta_text_editor_code_execution_view_result_block_param.rbi +118 -0
- data/rbi/anthropic/models/beta/beta_tool.rbi +14 -5
- data/rbi/anthropic/models/beta/beta_tool_result_block_param.rbi +3 -1
- data/rbi/anthropic/models/beta/beta_tool_text_editor_20250728.rbi +82 -0
- data/rbi/anthropic/models/beta/beta_tool_union.rbi +7 -4
- data/rbi/anthropic/models/beta/beta_web_fetch_block.rbi +67 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_block_param.rbi +71 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_tool_20250910.rbi +125 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_block.rbi +81 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_block_param.rbi +109 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_error_block.rbi +51 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_error_block_param.rbi +50 -0
- data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_error_code.rbi +70 -0
- data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +51 -70
- data/rbi/anthropic/models/beta/message_create_params.rbi +44 -66
- data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +44 -66
- data/rbi/anthropic/models/beta_error_response.rbi +10 -2
- data/rbi/anthropic/models/cache_control_ephemeral.rbi +68 -3
- data/rbi/anthropic/models/cache_creation.rbi +45 -0
- data/rbi/anthropic/models/citation_char_location.rbi +6 -0
- data/rbi/anthropic/models/citation_content_block_location.rbi +6 -0
- data/rbi/anthropic/models/citation_page_location.rbi +6 -0
- data/rbi/anthropic/models/citation_search_result_location_param.rbi +74 -0
- data/rbi/anthropic/models/citations_delta.rbi +4 -2
- data/rbi/anthropic/models/citations_search_result_location.rbi +74 -0
- data/rbi/anthropic/models/content_block.rbi +3 -3
- data/rbi/anthropic/models/content_block_param.rbi +6 -5
- data/rbi/anthropic/models/document_block_param.rbi +7 -3
- data/rbi/anthropic/models/error_response.rbi +10 -2
- data/rbi/anthropic/models/message.rbi +11 -3
- data/rbi/anthropic/models/message_count_tokens_params.rbi +24 -50
- data/rbi/anthropic/models/message_count_tokens_tool.rbi +2 -0
- data/rbi/anthropic/models/message_create_params.rbi +24 -50
- data/rbi/anthropic/models/messages/batch_create_params.rbi +24 -50
- data/rbi/anthropic/models/model.rbi +4 -8
- data/rbi/anthropic/models/raw_content_block_start_event.rbi +6 -6
- data/rbi/anthropic/models/search_result_block_param.rbi +77 -0
- data/rbi/anthropic/models/text_block.rbi +2 -1
- data/rbi/anthropic/models/text_block_param.rbi +6 -3
- data/rbi/anthropic/models/text_citation.rbi +2 -1
- data/rbi/anthropic/models/text_citation_param.rbi +2 -1
- data/rbi/anthropic/models/tool.rbi +16 -5
- data/rbi/anthropic/models/tool_result_block_param.rbi +6 -1
- data/rbi/anthropic/models/tool_text_editor_20250429.rbi +62 -0
- data/rbi/anthropic/models/tool_text_editor_20250728.rbi +72 -0
- data/rbi/anthropic/models/tool_union.rbi +2 -0
- data/rbi/anthropic/models/usage.rbi +13 -0
- data/rbi/anthropic/models.rbi +14 -0
- data/rbi/anthropic/resources/beta/messages.rbi +45 -87
- data/rbi/anthropic/resources/messages.rbi +315 -69
- data/rbi/anthropic/streaming.rbi +5 -0
- data/sig/anthropic/errors.rbs +9 -0
- data/sig/anthropic/helpers/streaming/events.rbs +117 -0
- data/sig/anthropic/helpers/streaming/message_stream.rbs +57 -0
- data/sig/anthropic/internal/transport/base_client.rbs +1 -1
- data/sig/anthropic/internal/type/base_stream.rbs +4 -0
- data/sig/anthropic/internal/type/converter.rbs +24 -1
- data/sig/anthropic/internal/type/union.rbs +2 -2
- data/sig/anthropic/models/anthropic_beta.rbs +2 -0
- data/sig/anthropic/models/beta/beta_base64_pdf_block.rbs +1 -58
- data/sig/anthropic/models/beta/beta_bash_code_execution_output_block.rbs +23 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_output_block_param.rbs +23 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_result_block.rbs +44 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_result_block_param.rbs +44 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_block.rbs +44 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_block_param.rbs +49 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_error.rbs +48 -0
- data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_error_param.rbs +48 -0
- data/sig/anthropic/models/beta/beta_citation_char_location.rbs +5 -0
- data/sig/anthropic/models/beta/beta_citation_config.rbs +17 -0
- data/sig/anthropic/models/beta/beta_citation_content_block_location.rbs +5 -0
- data/sig/anthropic/models/beta/beta_citation_page_location.rbs +5 -0
- data/sig/anthropic/models/beta/beta_citation_search_result_location.rbs +54 -0
- data/sig/anthropic/models/beta/beta_citation_search_result_location_param.rbs +54 -0
- data/sig/anthropic/models/beta/beta_citations_delta.rbs +1 -0
- data/sig/anthropic/models/beta/beta_code_execution_tool_20250825.rbs +34 -0
- data/sig/anthropic/models/beta/beta_content_block.rbs +5 -2
- data/sig/anthropic/models/beta/beta_content_block_param.rbs +12 -8
- data/sig/anthropic/models/beta/beta_document_block.rbs +49 -0
- data/sig/anthropic/models/beta/beta_raw_content_block_start_event.rbs +5 -2
- data/sig/anthropic/models/beta/beta_request_document_block.rbs +62 -0
- data/sig/anthropic/models/beta/beta_search_result_block_param.rbs +53 -0
- data/sig/anthropic/models/beta/beta_server_tool_usage.rbs +12 -3
- data/sig/anthropic/models/beta/beta_server_tool_use_block.rbs +9 -1
- data/sig/anthropic/models/beta/beta_server_tool_use_block_param.rbs +9 -1
- data/sig/anthropic/models/beta/beta_text_citation.rbs +1 -0
- data/sig/anthropic/models/beta/beta_text_citation_param.rbs +1 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_create_result_block.rbs +29 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_create_result_block_param.rbs +29 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block.rbs +49 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block_param.rbs +49 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block.rbs +46 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block_param.rbs +51 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error.rbs +53 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error_param.rbs +53 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_view_result_block.rbs +61 -0
- data/sig/anthropic/models/beta/beta_text_editor_code_execution_view_result_block_param.rbs +61 -0
- data/sig/anthropic/models/beta/beta_tool.rbs +14 -3
- data/sig/anthropic/models/beta/beta_tool_result_block_param.rbs +2 -0
- data/sig/anthropic/models/beta/beta_tool_text_editor_20250728.rbs +39 -0
- data/sig/anthropic/models/beta/beta_tool_union.rbs +7 -4
- data/sig/anthropic/models/beta/beta_web_fetch_block.rbs +39 -0
- data/sig/anthropic/models/beta/beta_web_fetch_block_param.rbs +39 -0
- data/sig/anthropic/models/beta/beta_web_fetch_tool_20250910.rbs +59 -0
- data/sig/anthropic/models/beta/beta_web_fetch_tool_result_block.rbs +44 -0
- data/sig/anthropic/models/beta/beta_web_fetch_tool_result_block_param.rbs +49 -0
- data/sig/anthropic/models/beta/beta_web_fetch_tool_result_error_block.rbs +29 -0
- data/sig/anthropic/models/beta/beta_web_fetch_tool_result_error_block_param.rbs +29 -0
- data/sig/anthropic/models/beta/beta_web_fetch_tool_result_error_code.rbs +32 -0
- data/sig/anthropic/models/beta/message_count_tokens_params.rbs +7 -4
- data/sig/anthropic/models/beta_error_response.rbs +13 -2
- data/sig/anthropic/models/cache_control_ephemeral.rbs +27 -3
- data/sig/anthropic/models/cache_creation.rbs +25 -0
- data/sig/anthropic/models/citation_char_location.rbs +5 -0
- data/sig/anthropic/models/citation_content_block_location.rbs +5 -0
- data/sig/anthropic/models/citation_page_location.rbs +5 -0
- data/sig/anthropic/models/citation_search_result_location_param.rbs +50 -0
- data/sig/anthropic/models/citations_delta.rbs +1 -0
- data/sig/anthropic/models/citations_search_result_location.rbs +50 -0
- data/sig/anthropic/models/content_block.rbs +2 -2
- data/sig/anthropic/models/content_block_param.rbs +6 -5
- data/sig/anthropic/models/document_block_param.rbs +4 -8
- data/sig/anthropic/models/error_response.rbs +13 -2
- data/sig/anthropic/models/message_count_tokens_tool.rbs +2 -0
- data/sig/anthropic/models/model.rbs +4 -10
- data/sig/anthropic/models/raw_content_block_start_event.rbs +2 -2
- data/sig/anthropic/models/search_result_block_param.rbs +49 -0
- data/sig/anthropic/models/text_citation.rbs +1 -0
- data/sig/anthropic/models/text_citation_param.rbs +1 -0
- data/sig/anthropic/models/tool.rbs +14 -3
- data/sig/anthropic/models/tool_result_block_param.rbs +5 -1
- data/sig/anthropic/models/tool_text_editor_20250429.rbs +30 -0
- data/sig/anthropic/models/tool_text_editor_20250728.rbs +35 -0
- data/sig/anthropic/models/tool_union.rbs +2 -0
- data/sig/anthropic/models/usage.rbs +5 -0
- data/sig/anthropic/models.rbs +12 -0
- data/sig/anthropic/streaming.rbs +3 -0
- metadata +152 -3
@@ -0,0 +1,278 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Anthropic
|
4
|
+
module Helpers
|
5
|
+
module Streaming
|
6
|
+
# @api private
|
7
|
+
#
|
8
|
+
# MessageStream provides a Ruby Enumerable interface over Server-Sent Events from
|
9
|
+
# the Anthropic API, yielding a mix of raw streaming events and higher-level typed
|
10
|
+
# events while maintaining accumulated message state throughout the stream lifecycle.
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# @generic Elem
|
14
|
+
class MessageStream
|
15
|
+
include Anthropic::Internal::Type::BaseStream
|
16
|
+
|
17
|
+
# @api private
|
18
|
+
#
|
19
|
+
# Consumes raw stream events and yields a mix of raw and higher-level typed events while
|
20
|
+
# maintaining accumulated message state. This is what's called when you run `each` on the
|
21
|
+
# stream.
|
22
|
+
#
|
23
|
+
# @return [Enumerable<generic<Elem>>]
|
24
|
+
private def iterator
|
25
|
+
@iterator ||= Anthropic::Internal::Util.chain_fused(@stream) do |y|
|
26
|
+
@raw_stream.each do |raw_event|
|
27
|
+
@accumated_message_snapshot = accumulate_event(
|
28
|
+
event: raw_event,
|
29
|
+
current_snapshot: @accumated_message_snapshot
|
30
|
+
)
|
31
|
+
events_to_yield = build_events(event: raw_event, message_snapshot: @accumated_message_snapshot)
|
32
|
+
events_to_yield.each(&y)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# @api public
|
38
|
+
#
|
39
|
+
# Blocks until the stream has been consumed
|
40
|
+
#
|
41
|
+
# @return [void]
|
42
|
+
def until_done = each {} # rubocop:disable Lint/EmptyBlock
|
43
|
+
|
44
|
+
# @api public
|
45
|
+
#
|
46
|
+
# Returns an enumerable of text deltas from the streaming response.
|
47
|
+
#
|
48
|
+
# @return [Enumerable<String>]
|
49
|
+
def text
|
50
|
+
Anthropic::Internal::Util.chain_fused(@iterator) do |y|
|
51
|
+
@iterator.each do |event|
|
52
|
+
if event.type == :content_block_delta && event.delta.type == :text_delta
|
53
|
+
y << event.delta.text
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# @api public
|
60
|
+
#
|
61
|
+
# Returns the complete accumulated Message object after stream completion.
|
62
|
+
#
|
63
|
+
# @return [Anthropic::Models::Message]
|
64
|
+
def accumulated_message
|
65
|
+
until_done
|
66
|
+
parse_tool_uses!(@accumated_message_snapshot) if @tool_models.any?
|
67
|
+
@accumated_message_snapshot
|
68
|
+
end
|
69
|
+
|
70
|
+
# @api public
|
71
|
+
#
|
72
|
+
# Returns all text content blocks concatenated into a single string.
|
73
|
+
# NOTE: Currently the API will only respond with a single content block.
|
74
|
+
#
|
75
|
+
# Will raise an error if no `text` content blocks were returned.
|
76
|
+
# @return [String]
|
77
|
+
def accumulated_text
|
78
|
+
message = accumulated_message
|
79
|
+
text_blocks = []
|
80
|
+
message.content.each do |block|
|
81
|
+
if block.type == :text
|
82
|
+
text_blocks << block.text
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
if text_blocks.empty?
|
87
|
+
raise RuntimeError.new("Expected to have received at least 1 text block")
|
88
|
+
end
|
89
|
+
|
90
|
+
text_blocks.join
|
91
|
+
end
|
92
|
+
|
93
|
+
# @api private
|
94
|
+
#
|
95
|
+
# Builds up a complete Message object as streaming events arrive.
|
96
|
+
#
|
97
|
+
# @param event [Anthropic::Models::RawMessageStreamEvent] the raw streaming event to process
|
98
|
+
# @param current_snapshot [Anthropic::Models::Message, nil] current accumulated message state
|
99
|
+
#
|
100
|
+
# @return [Anthropic::Models::Message] updated message snapshot with event applied
|
101
|
+
private def accumulate_event(event:, current_snapshot:)
|
102
|
+
unless event in Anthropic::Models::RawMessageStreamEvent
|
103
|
+
message = "Expected event to be a variant of RawMessageStreamEvent, got #{event.class}"
|
104
|
+
raise ArgumentError.new(message)
|
105
|
+
end
|
106
|
+
|
107
|
+
if current_snapshot.nil?
|
108
|
+
return event.message if event.type == :message_start
|
109
|
+
|
110
|
+
message = "Unexpected event order, got \"#{event.type}\" before \":message_start\""
|
111
|
+
raise RuntimeError.new(message)
|
112
|
+
end
|
113
|
+
|
114
|
+
case event
|
115
|
+
in Anthropic::Models::RawMessageStartEvent
|
116
|
+
# Use the converter to create a new, isolated copy of the message object.
|
117
|
+
# This ensures proper type validation and prevents shared object references
|
118
|
+
# that could lead to unintended mutations during streaming accumulation.
|
119
|
+
# Matches the Python SDK's approach of explicitly constructing Message objects.
|
120
|
+
return Anthropic::Internal::Type::Converter.coerce(Anthropic::Models::Message, event.message)
|
121
|
+
in Anthropic::Models::RawContentBlockStartEvent
|
122
|
+
current_snapshot.content = (current_snapshot.content || []) + [event.content_block]
|
123
|
+
in Anthropic::Models::RawContentBlockDeltaEvent
|
124
|
+
content = current_snapshot.content[event.index]
|
125
|
+
|
126
|
+
case (delta = event.delta)
|
127
|
+
in Anthropic::Models::TextDelta if content.type == :text
|
128
|
+
content.text += delta.text
|
129
|
+
in Anthropic::Models::InputJSONDelta if content.type == :tool_use
|
130
|
+
json_buf = content.json_buf.to_s
|
131
|
+
json_buf += delta.partial_json
|
132
|
+
|
133
|
+
content.input = json_buf
|
134
|
+
content.json_buf = json_buf
|
135
|
+
in Anthropic::Models::CitationsDelta if content.type == :text
|
136
|
+
content.citations ||= []
|
137
|
+
content.citations << delta.citation
|
138
|
+
in Anthropic::Models::ThinkingDelta if content.type == :thinking
|
139
|
+
content.thinking += delta.thinking
|
140
|
+
in Anthropic::Models::SignatureDelta if content.type == :thinking
|
141
|
+
content.signature = delta.signature
|
142
|
+
else
|
143
|
+
end
|
144
|
+
in Anthropic::Models::RawMessageDeltaEvent
|
145
|
+
current_snapshot.stop_reason = event.delta.stop_reason
|
146
|
+
current_snapshot.stop_sequence = event.delta.stop_sequence
|
147
|
+
current_snapshot.usage.output_tokens = event.usage.output_tokens
|
148
|
+
else
|
149
|
+
end
|
150
|
+
|
151
|
+
current_snapshot
|
152
|
+
end
|
153
|
+
|
154
|
+
# @api private
|
155
|
+
#
|
156
|
+
# Determines which events to yield for a given raw streaming event.
|
157
|
+
#
|
158
|
+
# May transform events into higher-level types (TextEvent, InputJsonEvent),
|
159
|
+
# pass through raw events unchanged, or produce multiple events.
|
160
|
+
#
|
161
|
+
# @param event [Anthropic::Models::RawMessageStreamEvent] the raw event to process
|
162
|
+
# @param message_snapshot [Anthropic::Models::Message] current accumulated message state
|
163
|
+
#
|
164
|
+
# @return [Array<Object>] events to yield (mix of raw and typed events)
|
165
|
+
private def build_events(event:, message_snapshot:)
|
166
|
+
events_to_yield = []
|
167
|
+
|
168
|
+
case event
|
169
|
+
in Anthropic::Models::RawMessageStopEvent
|
170
|
+
events_to_yield << MessageStopEvent.new(
|
171
|
+
type: :message_stop,
|
172
|
+
message: message_snapshot
|
173
|
+
)
|
174
|
+
in Anthropic::Models::RawContentBlockDeltaEvent
|
175
|
+
events_to_yield << event
|
176
|
+
content_block = message_snapshot.content[event.index]
|
177
|
+
|
178
|
+
case (delta = event.delta)
|
179
|
+
in Anthropic::Models::TextDelta if content_block.type == :text
|
180
|
+
events_to_yield << Anthropic::Streaming::TextEvent.new(
|
181
|
+
type: :text,
|
182
|
+
text: delta.text,
|
183
|
+
snapshot: content_block.text
|
184
|
+
)
|
185
|
+
in Anthropic::Models::InputJSONDelta if content_block.type == :tool_use
|
186
|
+
events_to_yield << Anthropic::Streaming::InputJsonEvent.new(
|
187
|
+
type: :input_json,
|
188
|
+
partial_json: delta.partial_json,
|
189
|
+
snapshot: content_block.input
|
190
|
+
)
|
191
|
+
in Anthropic::Models::CitationsDelta if content_block.type == :text
|
192
|
+
events_to_yield << Anthropic::Streaming::CitationEvent.new(
|
193
|
+
type: :citation,
|
194
|
+
citation: delta.citation,
|
195
|
+
snapshot: content_block.citations || []
|
196
|
+
)
|
197
|
+
in Anthropic::Models::ThinkingDelta if content_block.type == :thinking
|
198
|
+
events_to_yield << Anthropic::Streaming::ThinkingEvent.new(
|
199
|
+
type: :thinking,
|
200
|
+
thinking: delta.thinking,
|
201
|
+
snapshot: content_block.thinking
|
202
|
+
)
|
203
|
+
in Anthropic::Models::SignatureDelta if content_block.type == :thinking
|
204
|
+
events_to_yield << Anthropic::Streaming::SignatureEvent.new(
|
205
|
+
type: :signature,
|
206
|
+
signature: content_block.signature
|
207
|
+
)
|
208
|
+
else
|
209
|
+
end
|
210
|
+
in Anthropic::Models::RawContentBlockStopEvent
|
211
|
+
content_block = message_snapshot.content[event.index]
|
212
|
+
|
213
|
+
events_to_yield << ContentBlockStopEvent.new(
|
214
|
+
type: :content_block_stop,
|
215
|
+
index: event.index,
|
216
|
+
content_block: content_block
|
217
|
+
)
|
218
|
+
else
|
219
|
+
events_to_yield << event
|
220
|
+
end
|
221
|
+
|
222
|
+
events_to_yield
|
223
|
+
end
|
224
|
+
|
225
|
+
# @api private
|
226
|
+
#
|
227
|
+
# Parse tool use blocks in the message using the provided tool models.
|
228
|
+
#
|
229
|
+
# @param message [Anthropic::Models::Message] The message to parse
|
230
|
+
#
|
231
|
+
# @return [Anthropic::Models::Message] The message with parsed tool uses
|
232
|
+
private def parse_tool_uses!(message)
|
233
|
+
return message unless message&.content
|
234
|
+
|
235
|
+
message.content.each_with_index do |content, index|
|
236
|
+
next unless content.type == :tool_use
|
237
|
+
|
238
|
+
next unless (model = @tool_models[content.name])
|
239
|
+
|
240
|
+
parsed =
|
241
|
+
begin
|
242
|
+
parsed_input = content.input.is_a?(String) ? JSON.parse(content.input) : content.input
|
243
|
+
|
244
|
+
Anthropic::Internal::Type::Converter.coerce(model, parsed_input)
|
245
|
+
rescue StandardError => e
|
246
|
+
e
|
247
|
+
end
|
248
|
+
|
249
|
+
message.content[index] = Anthropic::Models::ToolUseBlock.new(
|
250
|
+
id: content.id,
|
251
|
+
input: content.input,
|
252
|
+
name: content.name,
|
253
|
+
type: content.type,
|
254
|
+
parsed: parsed
|
255
|
+
)
|
256
|
+
end
|
257
|
+
|
258
|
+
message
|
259
|
+
end
|
260
|
+
|
261
|
+
# @api private
|
262
|
+
#
|
263
|
+
# @param raw_stream [Anthropic::Internal::Type::BaseStream]
|
264
|
+
# @param tool_models [Hash{String=>Class}] Mapping of tool names to model classes
|
265
|
+
def initialize(raw_stream:, tool_models: {})
|
266
|
+
# The underlying Server-Sent Event stream from the Anthropic API.
|
267
|
+
@raw_stream = raw_stream
|
268
|
+
# Accumulated message state that builds up as events are processed.
|
269
|
+
@accumated_message_snapshot = nil
|
270
|
+
# Mapping of tool names to model classes for parsing.
|
271
|
+
@tool_models = tool_models
|
272
|
+
# Lazy enumerable that transforms raw events into consumable events.
|
273
|
+
@iterator = iterator
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Anthropic
|
4
|
+
module Helpers
|
5
|
+
module Streaming
|
6
|
+
extend Anthropic::Internal::Util::SorbetRuntimeSupport
|
7
|
+
|
8
|
+
define_sorbet_constant!(:RawMessageEvent) do
|
9
|
+
T.type_alias do
|
10
|
+
T.any(
|
11
|
+
Anthropic::Models::RawMessageStartEvent,
|
12
|
+
Anthropic::Models::RawMessageDeltaEvent,
|
13
|
+
Anthropic::Models::RawMessageStopEvent,
|
14
|
+
Anthropic::Models::RawContentBlockStartEvent,
|
15
|
+
Anthropic::Models::RawContentBlockDeltaEvent,
|
16
|
+
Anthropic::Models::RawContentBlockStopEvent
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
define_sorbet_constant!(:StreamEvent) do
|
22
|
+
T.type_alias do
|
23
|
+
T.any(
|
24
|
+
Anthropic::Streaming::RawMessageEvent,
|
25
|
+
Anthropic::Streaming::TextEvent,
|
26
|
+
Anthropic::Streaming::CitationEvent,
|
27
|
+
Anthropic::Streaming::ThinkingEvent,
|
28
|
+
Anthropic::Streaming::SignatureEvent,
|
29
|
+
Anthropic::Streaming::InputJsonEvent,
|
30
|
+
Anthropic::Streaming::MessageStopEvent,
|
31
|
+
Anthropic::Streaming::ContentBlockStopEvent
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -82,7 +82,10 @@ module Anthropic
|
|
82
82
|
end
|
83
83
|
@project_id = project_id
|
84
84
|
|
85
|
-
base_url ||= ENV.fetch(
|
85
|
+
base_url ||= ENV.fetch(
|
86
|
+
"ANTHROPIC_VERTEX_BASE_URL",
|
87
|
+
@region.to_s == "global" ? "https://aiplatform.googleapis.com/v1" : "https://#{@region}-aiplatform.googleapis.com/v1"
|
88
|
+
)
|
86
89
|
|
87
90
|
super(
|
88
91
|
base_url: base_url,
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Anthropic
|
4
|
+
InputSchema = Anthropic::Helpers::InputSchema
|
5
|
+
ArrayOf = Anthropic::Helpers::InputSchema::ArrayOf
|
6
|
+
BaseModel = Anthropic::Helpers::InputSchema::BaseModel
|
7
|
+
Boolean = Anthropic::Helpers::InputSchema::Boolean
|
8
|
+
EnumOf = Anthropic::Helpers::InputSchema::EnumOf
|
9
|
+
UnionOf = Anthropic::Helpers::InputSchema::UnionOf
|
10
|
+
end
|
@@ -23,13 +23,15 @@ module Anthropic
|
|
23
23
|
case msg
|
24
24
|
in {event: "completion", data: String => data}
|
25
25
|
decoded = JSON.parse(data, symbolize_names: true)
|
26
|
-
|
26
|
+
unwrapped = Anthropic::Internal::Util.dig(decoded, @unwrap)
|
27
|
+
y << Anthropic::Internal::Type::Converter.coerce(@model, unwrapped)
|
27
28
|
in {
|
28
29
|
event: "message_start" | "message_delta" | "message_stop" | "content_block_start" | "content_block_delta" | "content_block_stop",
|
29
30
|
data: String => data
|
30
31
|
}
|
31
32
|
decoded = JSON.parse(data, symbolize_names: true)
|
32
|
-
|
33
|
+
unwrapped = Anthropic::Internal::Util.dig(decoded, @unwrap)
|
34
|
+
y << Anthropic::Internal::Type::Converter.coerce(@model, unwrapped)
|
33
35
|
in {event: "ping"}
|
34
36
|
next
|
35
37
|
in {event: "error", data: String => data}
|
@@ -38,13 +40,14 @@ module Anthropic
|
|
38
40
|
rescue JSON::ParserError
|
39
41
|
data
|
40
42
|
end
|
41
|
-
Anthropic::Errors::APIStatusError.for(
|
43
|
+
err = Anthropic::Errors::APIStatusError.for(
|
42
44
|
url: @url,
|
43
45
|
status: @status,
|
44
46
|
body: decoded,
|
45
47
|
request: nil,
|
46
48
|
response: @response
|
47
49
|
)
|
50
|
+
raise err
|
48
51
|
else
|
49
52
|
end
|
50
53
|
end
|
@@ -341,6 +341,23 @@ module Anthropic
|
|
341
341
|
(@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
|
342
342
|
end
|
343
343
|
|
344
|
+
# @api private
|
345
|
+
#
|
346
|
+
# Very private API, do not use
|
347
|
+
#
|
348
|
+
# @param request [Hash{Symbol=>Object}] .
|
349
|
+
#
|
350
|
+
# @option request [Symbol] :method
|
351
|
+
#
|
352
|
+
# @option request [URI::Generic] :url
|
353
|
+
#
|
354
|
+
# @option request [Hash{String=>String}] :headers
|
355
|
+
#
|
356
|
+
# @option request [Object] :body
|
357
|
+
#
|
358
|
+
# @return [Hash{Symbol, Object}]
|
359
|
+
private def transform_request(request) = request
|
360
|
+
|
344
361
|
# @api private
|
345
362
|
#
|
346
363
|
# @param request [Hash{Symbol=>Object}] .
|
@@ -365,7 +382,8 @@ module Anthropic
|
|
365
382
|
#
|
366
383
|
# @raise [Anthropic::Errors::APIError]
|
367
384
|
# @return [Array(Integer, Net::HTTPResponse, Enumerable<String>)]
|
368
|
-
|
385
|
+
def send_request(request, redirect_count:, retry_count:, send_retry_header:)
|
386
|
+
request = transform_request(request)
|
369
387
|
url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
|
370
388
|
input = {**request.except(:timeout), deadline: Anthropic::Internal::Util.monotonic_secs + timeout}
|
371
389
|
|
@@ -471,6 +489,7 @@ module Anthropic
|
|
471
489
|
self.class.validate!(req)
|
472
490
|
model = req.fetch(:model) { Anthropic::Internal::Type::Unknown }
|
473
491
|
opts = req[:options].to_h
|
492
|
+
unwrap = req[:unwrap]
|
474
493
|
Anthropic::RequestOptions.validate!(opts)
|
475
494
|
request = build_request(req.except(:options), opts)
|
476
495
|
url = request.fetch(:url)
|
@@ -487,11 +506,18 @@ module Anthropic
|
|
487
506
|
decoded = Anthropic::Internal::Util.decode_content(response, stream: stream)
|
488
507
|
case req
|
489
508
|
in {stream: Class => st}
|
490
|
-
st.new(
|
509
|
+
st.new(
|
510
|
+
model: model,
|
511
|
+
url: url,
|
512
|
+
status: status,
|
513
|
+
response: response,
|
514
|
+
unwrap: unwrap,
|
515
|
+
stream: decoded
|
516
|
+
)
|
491
517
|
in {page: Class => page}
|
492
518
|
page.new(client: self, req: req, headers: response, page_data: decoded)
|
493
519
|
else
|
494
|
-
unwrapped = Anthropic::Internal::Util.dig(decoded,
|
520
|
+
unwrapped = Anthropic::Internal::Util.dig(decoded, unwrap)
|
495
521
|
Anthropic::Internal::Type::Converter.coerce(model, unwrapped)
|
496
522
|
end
|
497
523
|
end
|
@@ -532,8 +558,7 @@ module Anthropic
|
|
532
558
|
),
|
533
559
|
page: T.nilable(T::Class[Anthropic::Internal::Type::BasePage[Anthropic::Internal::Type::BaseModel]]),
|
534
560
|
stream: T.nilable(
|
535
|
-
T::Class[Anthropic::Internal::Type::BaseStream[T.anything,
|
536
|
-
Anthropic::Internal::Type::BaseModel]]
|
561
|
+
T::Class[Anthropic::Internal::Type::BaseStream[T.anything, Anthropic::Internal::Type::BaseModel]]
|
537
562
|
),
|
538
563
|
model: T.nilable(Anthropic::Internal::Type::Converter::Input),
|
539
564
|
options: T.nilable(Anthropic::RequestOptions::OrHash)
|
@@ -193,15 +193,7 @@ module Anthropic
|
|
193
193
|
end
|
194
194
|
|
195
195
|
define_sorbet_constant!(:Request) do
|
196
|
-
T.type_alias
|
197
|
-
{
|
198
|
-
method: Symbol,
|
199
|
-
url: URI::Generic,
|
200
|
-
headers: T::Hash[String, String],
|
201
|
-
body: T.anything,
|
202
|
-
deadline: Float
|
203
|
-
}
|
204
|
-
end
|
196
|
+
T.type_alias { {method: Symbol, url: URI::Generic, headers: T::Hash[String, String], body: T.anything, deadline: Float} }
|
205
197
|
end
|
206
198
|
end
|
207
199
|
end
|
@@ -62,10 +62,14 @@ module Anthropic
|
|
62
62
|
#
|
63
63
|
# @param state [Hash{Symbol=>Object}] .
|
64
64
|
#
|
65
|
-
# @option state [Boolean
|
65
|
+
# @option state [Boolean] :translate_names
|
66
|
+
#
|
67
|
+
# @option state [Boolean] :strictness
|
66
68
|
#
|
67
69
|
# @option state [Hash{Symbol=>Object}] :exactness
|
68
70
|
#
|
71
|
+
# @option state [Class<StandardError>] :error
|
72
|
+
#
|
69
73
|
# @option state [Integer] :branched
|
70
74
|
#
|
71
75
|
# @return [Array<Object>, Object]
|
@@ -74,6 +78,7 @@ module Anthropic
|
|
74
78
|
|
75
79
|
unless value.is_a?(Array)
|
76
80
|
exactness[:no] += 1
|
81
|
+
state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}")
|
77
82
|
return value
|
78
83
|
end
|
79
84
|
|
@@ -143,6 +148,7 @@ module Anthropic
|
|
143
148
|
# @option spec [Boolean] :"nil?"
|
144
149
|
def initialize(type_info, spec = {})
|
145
150
|
@item_type_fn = Anthropic::Internal::Type::Converter.type_info(type_info || spec)
|
151
|
+
@meta = Anthropic::Internal::Type::Converter.meta_info(type_info, spec)
|
146
152
|
@nilable = spec.fetch(:nil?, false)
|
147
153
|
end
|
148
154
|
|
@@ -59,18 +59,12 @@ module Anthropic
|
|
59
59
|
in Hash
|
60
60
|
[Anthropic::Internal::Type::Converter.type_info(type_info), type_info]
|
61
61
|
end
|
62
|
+
meta = Anthropic::Internal::Type::Converter.meta_info(type_info, spec)
|
62
63
|
|
63
|
-
setter = "#{name_sym}="
|
64
|
+
setter = :"#{name_sym}="
|
64
65
|
api_name = info.fetch(:api_name, name_sym)
|
65
66
|
nilable = info.fetch(:nil?, false)
|
66
|
-
const =
|
67
|
-
info.fetch(
|
68
|
-
:const,
|
69
|
-
Anthropic::Internal::OMIT
|
70
|
-
)
|
71
|
-
else
|
72
|
-
Anthropic::Internal::OMIT
|
73
|
-
end
|
67
|
+
const = required && !nilable ? info.fetch(:const, Anthropic::Internal::OMIT) : Anthropic::Internal::OMIT
|
74
68
|
|
75
69
|
[name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
|
76
70
|
|
@@ -81,31 +75,65 @@ module Anthropic
|
|
81
75
|
required: required,
|
82
76
|
nilable: nilable,
|
83
77
|
const: const,
|
84
|
-
type_fn: type_fn
|
78
|
+
type_fn: type_fn,
|
79
|
+
meta: meta
|
85
80
|
}
|
86
81
|
|
87
|
-
define_method(setter)
|
82
|
+
define_method(setter) do |value|
|
83
|
+
target = type_fn.call
|
84
|
+
state = Anthropic::Internal::Type::Converter.new_coerce_state(translate_names: false)
|
85
|
+
coerced = Anthropic::Internal::Type::Converter.coerce(target, value, state: state)
|
86
|
+
status = @coerced.store(name_sym, state.fetch(:error) || true)
|
87
|
+
stored =
|
88
|
+
case [target, status]
|
89
|
+
in [Anthropic::Internal::Type::Converter | Symbol, true]
|
90
|
+
coerced
|
91
|
+
else
|
92
|
+
value
|
93
|
+
end
|
94
|
+
@data.store(name_sym, stored)
|
95
|
+
end
|
88
96
|
|
97
|
+
# rubocop:disable Style/CaseEquality
|
98
|
+
# rubocop:disable Metrics/BlockLength
|
89
99
|
define_method(name_sym) do
|
90
100
|
target = type_fn.call
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
Anthropic::
|
97
|
-
|
101
|
+
|
102
|
+
case @coerced[name_sym]
|
103
|
+
in true | false if Anthropic::Internal::Type::Converter === target
|
104
|
+
@data.fetch(name_sym)
|
105
|
+
in ::StandardError => e
|
106
|
+
raise Anthropic::Errors::ConversionError.new(
|
107
|
+
on: self.class,
|
108
|
+
method: __method__,
|
109
|
+
target: target,
|
110
|
+
value: @data.fetch(name_sym),
|
111
|
+
cause: e
|
98
112
|
)
|
113
|
+
else
|
114
|
+
Kernel.then do
|
115
|
+
value = @data.fetch(name_sym) { const == Anthropic::Internal::OMIT ? nil : const }
|
116
|
+
state = Anthropic::Internal::Type::Converter.new_coerce_state(translate_names: false)
|
117
|
+
if (nilable || !required) && value.nil?
|
118
|
+
nil
|
119
|
+
else
|
120
|
+
Anthropic::Internal::Type::Converter.coerce(
|
121
|
+
target, value, state: state
|
122
|
+
)
|
123
|
+
end
|
124
|
+
rescue StandardError => e
|
125
|
+
raise Anthropic::Errors::ConversionError.new(
|
126
|
+
on: self.class,
|
127
|
+
method: __method__,
|
128
|
+
target: target,
|
129
|
+
value: value,
|
130
|
+
cause: e
|
131
|
+
)
|
132
|
+
end
|
99
133
|
end
|
100
|
-
rescue StandardError => e
|
101
|
-
cls = self.class.name.split("::").last
|
102
|
-
message = [
|
103
|
-
"Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
|
104
|
-
"To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
|
105
|
-
"Cause: #{e.message}"
|
106
|
-
].join(" ")
|
107
|
-
raise Anthropic::Errors::ConversionError.new(message)
|
108
134
|
end
|
135
|
+
# rubocop:enable Metrics/BlockLength
|
136
|
+
# rubocop:enable Style/CaseEquality
|
109
137
|
end
|
110
138
|
|
111
139
|
# @api private
|
@@ -205,23 +233,28 @@ module Anthropic
|
|
205
233
|
#
|
206
234
|
# @param state [Hash{Symbol=>Object}] .
|
207
235
|
#
|
208
|
-
# @option state [Boolean
|
236
|
+
# @option state [Boolean] :translate_names
|
237
|
+
#
|
238
|
+
# @option state [Boolean] :strictness
|
209
239
|
#
|
210
240
|
# @option state [Hash{Symbol=>Object}] :exactness
|
211
241
|
#
|
242
|
+
# @option state [Class<StandardError>] :error
|
243
|
+
#
|
212
244
|
# @option state [Integer] :branched
|
213
245
|
#
|
214
246
|
# @return [self, Object]
|
215
247
|
def coerce(value, state:)
|
216
248
|
exactness = state.fetch(:exactness)
|
217
249
|
|
218
|
-
if value.is_a?(self
|
250
|
+
if value.is_a?(self)
|
219
251
|
exactness[:yes] += 1
|
220
252
|
return value
|
221
253
|
end
|
222
254
|
|
223
255
|
unless (val = Anthropic::Internal::Util.coerce_hash(value)).is_a?(Hash)
|
224
256
|
exactness[:no] += 1
|
257
|
+
state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
|
225
258
|
return value
|
226
259
|
end
|
227
260
|
exactness[:yes] += 1
|
@@ -229,13 +262,15 @@ module Anthropic
|
|
229
262
|
keys = val.keys.to_set
|
230
263
|
instance = new
|
231
264
|
data = instance.to_h
|
265
|
+
status = instance.instance_variable_get(:@coerced)
|
232
266
|
|
233
267
|
# rubocop:disable Metrics/BlockLength
|
234
268
|
fields.each do |name, field|
|
235
269
|
mode, required, target = field.fetch_values(:mode, :required, :type)
|
236
270
|
api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
|
271
|
+
src_name = state.fetch(:translate_names) ? api_name : name
|
237
272
|
|
238
|
-
unless val.key?(
|
273
|
+
unless val.key?(src_name)
|
239
274
|
if required && mode != :dump && const == Anthropic::Internal::OMIT
|
240
275
|
exactness[nilable ? :maybe : :no] += 1
|
241
276
|
else
|
@@ -244,9 +279,10 @@ module Anthropic
|
|
244
279
|
next
|
245
280
|
end
|
246
281
|
|
247
|
-
item = val.fetch(
|
248
|
-
keys.delete(
|
282
|
+
item = val.fetch(src_name)
|
283
|
+
keys.delete(src_name)
|
249
284
|
|
285
|
+
state[:error] = nil
|
250
286
|
converted =
|
251
287
|
if item.nil? && (nilable || !required)
|
252
288
|
exactness[nilable ? :yes : :maybe] += 1
|
@@ -260,6 +296,8 @@ module Anthropic
|
|
260
296
|
item
|
261
297
|
end
|
262
298
|
end
|
299
|
+
|
300
|
+
status.store(name, state.fetch(:error) || true)
|
263
301
|
data.store(name, converted)
|
264
302
|
end
|
265
303
|
# rubocop:enable Metrics/BlockLength
|
@@ -434,7 +472,18 @@ module Anthropic
|
|
434
472
|
# Create a new instance of a model.
|
435
473
|
#
|
436
474
|
# @param data [Hash{Symbol=>Object}, self]
|
437
|
-
def initialize(data = {})
|
475
|
+
def initialize(data = {})
|
476
|
+
@data = {}
|
477
|
+
@coerced = {}
|
478
|
+
Anthropic::Internal::Util.coerce_hash!(data).each do
|
479
|
+
if self.class.known_fields.key?(_1)
|
480
|
+
public_send(:"#{_1}=", _2)
|
481
|
+
else
|
482
|
+
@data.store(_1, _2)
|
483
|
+
@coerced.store(_1, false)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
438
487
|
|
439
488
|
class << self
|
440
489
|
# @api private
|