diff --git a/src/datacustomcode/cli.py b/src/datacustomcode/cli.py index 3a03e6d9..a22fcb5b 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() @@ -349,14 +351,27 @@ def init(directory: str, code_type: str, use_in_feature: Optional[str]): + " to automatically update config.json when you make changes to your code" ) - # Generate test.json for functions + # Prefer the sample tests/test.json shipped in the template; only + # generate one from the request model if the template doesn't have it if code_type == "function": - test_json_path = _generate_function_test_file(entrypoint_path) - if test_json_path: + template_test_json = os.path.join( + os.path.dirname(entrypoint_path), TESTS_DIR, TEST_FILE + ) + test_json_path: Optional[str] + if os.path.isfile(template_test_json): + test_json_path = template_test_json click.echo( - "Generated test file at " + "Sample test file at " + click.style(test_json_path, fg="blue", bold=True) ) + else: + test_json_path = _generate_function_test_file(entrypoint_path) + if test_json_path: + click.echo( + "Generated test file at " + + click.style(test_json_path, fg="blue", bold=True) + ) + if test_json_path: click.echo( "Test your function locally with " + click.style( diff --git a/src/datacustomcode/constants.py b/src/datacustomcode/constants.py index fe29a5cf..23b139d3 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 51d6cdb8..c0646ce1 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/src/datacustomcode/templates/function/chunking/payload/tests/test.json b/src/datacustomcode/templates/function/chunking/payload/tests/test.json new file mode 100644 index 00000000..63623b9b --- /dev/null +++ b/src/datacustomcode/templates/function/chunking/payload/tests/test.json @@ -0,0 +1,33 @@ +{ + "input": [ + { + "text": "Online Remittance Instructions", + "metadata": { + "type": "text", + "page_number": 1, + "transcript_fields": { + "speaker": "Agent", + "start_timestamp": 1.0, + "end_timestamp": 8.75 + }, + "text_as_html": "

Online Remittance Instructions

", + "source_dmo_fields": { + "FilePath__c": "quarterly_report.pdf", + "Size__c": 1377454.0, + "ContentType__c": "pdf", + "LastModified__c": "2026-03-25T02:01:24.918000" + }, + "prepend": [ + { + "dmo_name": "udmo_1__dlm", + "field_name": "ResolvedFilePath__c", + "value": "udlo_1__dll:quarterly_report.pdf" + } + ], + "image_base64": "sample_image_base64", + "image_mime_type": "image/png", + "image_type": "diagram" + } + } + ] +} \ No newline at end of file diff --git a/tests/test_describe_supported_signatures.py b/tests/test_describe_supported_signatures.py new file mode 100644 index 00000000..767a28a3 --- /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