diff --git a/src/datacustomcode/cli.py b/src/datacustomcode/cli.py index 3a03e6d..e2690a6 100644 --- a/src/datacustomcode/cli.py +++ b/src/datacustomcode/cli.py @@ -215,6 +215,7 @@ def deploy( COMPUTE_TYPES, CodeExtensionMetadata, deploy_full, + describe_supported_signatures, infer_use_in_feature, ) from datacustomcode.token_provider import ( @@ -252,8 +253,9 @@ def deploy( else: click.secho( "Error: Function signature does not match a supported type. " - "Use SearchIndexChunkingV1Request and " - "SearchIndexChunkingV1Response in function signature.", + "Use a supported Request/Response pair in the function " + "signature. For example, " + f"{describe_supported_signatures(entrypoint_path)}.", fg="red", ) raise click.Abort() diff --git a/src/datacustomcode/constants.py b/src/datacustomcode/constants.py index fe29a5c..23b139d 100644 --- a/src/datacustomcode/constants.py +++ b/src/datacustomcode/constants.py @@ -46,8 +46,9 @@ SCRIPT_USE_IN_FEATURE_STREAMING, ] -# Pydantic request/response type names to feature names +# (request type name, response type name) pair to feature name REQUEST_TYPE_TO_FEATURE = { - "SearchIndexChunkingV1Request": "SearchIndexChunking", - "SearchIndexChunkingV1Response": "SearchIndexChunking", + ("SearchIndexChunkingV1Request", "SearchIndexChunkingV1Response"): ( + "SearchIndexChunking" + ), } diff --git a/src/datacustomcode/deploy.py b/src/datacustomcode/deploy.py index 51d6cdb..c0646ce 100644 --- a/src/datacustomcode/deploy.py +++ b/src/datacustomcode/deploy.py @@ -95,15 +95,35 @@ def infer_use_in_feature(entrypoint_path: str) -> Union[str, None]: if not request_type_name or not response_type_name: return None - # Look up features for both types - request_feature = REQUEST_TYPE_TO_FEATURE.get(request_type_name) - response_feature = REQUEST_TYPE_TO_FEATURE.get(response_type_name) + return REQUEST_TYPE_TO_FEATURE.get((request_type_name, response_type_name)) - # Both must be present and must match - if request_feature and response_feature and request_feature == response_feature: - return request_feature - return None +def describe_supported_signatures(entrypoint_path: str) -> str: + """Build a contract-mismatch hint based on the function's feature type. + + If the request or response type belongs to a known feature, only that + feature's expected pair is listed; otherwise all supported pairs are. + Each pair is followed by the use_in_feature it maps to, e.g. + ``Req/Resp for use_in_feature: SearchIndexChunking``. + """ + from datacustomcode.function_utils import inspect_function_types_static + + request_type_name, response_type_name = inspect_function_types_static( + entrypoint_path + ) + supported = list(REQUEST_TYPE_TO_FEATURE.items()) + sharing_a_type = [ + ((expected_request, expected_response), feature) + for (expected_request, expected_response), feature in supported + if request_type_name == expected_request + or response_type_name == expected_response + ] + # Narrow the hint only when the detected types point at a single feature + to_show = sharing_a_type if len(sharing_a_type) == 1 else supported + return " or ".join( + f"{expected_request}/{expected_response} for use_in_feature: {feature}" + for (expected_request, expected_response), feature in to_show + ) class CodeExtensionMetadata(BaseModel): diff --git a/tests/test_describe_supported_signatures.py b/tests/test_describe_supported_signatures.py new file mode 100644 index 0000000..767a28a --- /dev/null +++ b/tests/test_describe_supported_signatures.py @@ -0,0 +1,23 @@ +from datacustomcode.deploy import describe_supported_signatures + + +def test_describe_supported_signatures(tmp_path): + entrypoint = tmp_path / "entrypoint.py" + chunk_pair = ( + "SearchIndexChunkingV1Request/SearchIndexChunkingV1Response " + "for use_in_feature: SearchIndexChunking" + ) + + def describe(request_type, response_type): + entrypoint.write_text( + f"def function(request: {request_type}, runtime)" + f" -> {response_type}:\n pass\n" + ) + return describe_supported_signatures(str(entrypoint)) + + # A recognised request or response narrows the hint to its own feature + assert describe("SearchIndexChunkingV1Request", "dict") == chunk_pair + assert describe("dict", "SearchIndexChunkingV1Response") == chunk_pair + # Unrecognised types fall back to listing every supported pair + fallback = describe("dict", "dict") + assert fallback == chunk_pair