Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ Argument:
- `DIRECTORY`: Directory to create project in (default: ".")
Options:
- `--code-type TEXT`: This can be either `function` or `script`. The default value is `script` if the argument is missing.
- `--use-in-feature TEXT`, `-u TEXT`: Invoke option for this package. For scripts: `BatchTransform` (default) or `StreamingTransform`. For functions: `SearchIndexChunking` or `KnowledgeHarmonizationPostProcessing`.


#### `datacustomcode scan`
Expand Down Expand Up @@ -414,7 +415,7 @@ Options:
- `--network TEXT`: docker network (default: "default")
- `--cpu-size TEXT`: CPU size for the deployment (default: `CPU_2XL`). Available options: CPU_L(Large), CPU_XL(Extra Large), CPU_2XL(2X Large), CPU_4XL(4X Large)
- `--sf-cli-org TEXT`: Salesforce CLI org alias or username (e.g. `myorg`). Fetches credentials via `sf org display` — no `datacustomcode configure` step needed. Takes precedence over `--profile` if both are supplied.
- `--function-invoke-opt TEXT`: Currently we support only `UnstructuredChunking` for functions.
- Function invoke option: inferred from the function signature on deploy. `SearchIndexChunking` is sent as `UnstructuredChunking`. `KnowledgeHarmonizationPostProcessing` is sent as `KnowledgeHarmonizationPostProcessing`.


## Testing LLM Gateway
Expand Down
3 changes: 2 additions & 1 deletion src/datacustomcode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ def deploy(
default=None,
help=(
"Invoke option for this package. For scripts: 'BatchTransform' "
"(default) or 'StreamingTransform'. For functions: 'SearchIndexChunking'."
"(default) or 'StreamingTransform'. For functions: 'SearchIndexChunking' "
"or 'KnowledgeHarmonizationPostProcessing'."
),
)
def init(directory: str, code_type: str, use_in_feature: Optional[str]):
Expand Down
6 changes: 6 additions & 0 deletions src/datacustomcode/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
# Feature to template folder mapping
FEATURE_TEMPLATE_MAPPING = {
"SearchIndexChunking": "chunking",
"KnowledgeHarmonizationPostProcessing": "knowledge_harmonization_post_processing",
}

# Feature name to Connect API name mapping
USE_IN_FEATURE_MAPPING_FOR_CONNECT_API = {
"SearchIndexChunking": "UnstructuredChunking",
"KnowledgeHarmonizationPostProcessing": "KnowledgeHarmonizationPostProcessing",
}

# Script (data transform) invoke options
Expand All @@ -51,4 +53,8 @@
("SearchIndexChunkingV1Request", "SearchIndexChunkingV1Response"): (
"SearchIndexChunking"
),
(
"KnowledgeHarmonizationPostProcessingV1Request",
"KnowledgeHarmonizationPostProcessingV1Response",
): "KnowledgeHarmonizationPostProcessing",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Copyright (c) 2026, Salesforce, Inc.
# SPDX-License-Identifier: Apache-2
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Pydantic models for Knowledge Harmonization Post-Processing V1
"""

from typing import (
Dict,
Optional,
Union,
)

from pydantic import (
BaseModel,
ConfigDict,
Field,
)


class KnowledgeHarmonizationPostProcessingV1Metadata(BaseModel):
"""Metadata for the harmonized content element"""

source_dmo_fields: Optional[Dict[str, Union[str, int, float]]] = Field(
default=None,
description=(
"Source Data Model Object fields as key-value pairs, "
"e.g. FilePath__c, ContentType__c, LastModified__c."
),
examples=[
{
"FilePath__c": "quarterly_report.pdf",
"ContentType__c": "application/pdf",
"LastModified__c": "2026-08-12T00:00:00.000000",
}
],
)
model_config = ConfigDict(extra="ignore")


class KnowledgeHarmonizationPostProcessingV1DocElement(BaseModel):
"""The harmonized content element to be transformed"""

text: str = Field(
default="",
description=(
"Harmonized content produced by the built-in pipeline steps, "
"before the extension runs."
),
examples=["<h1>Quarterly Report</h1><p>Revenue grew 12%.</p>"],
)
metadata: Optional[KnowledgeHarmonizationPostProcessingV1Metadata] = Field(
default=None,
description="Source document metadata. Optional, may be absent.",
)
model_config = ConfigDict(extra="ignore")


class KnowledgeHarmonizationPostProcessingV1Output(BaseModel):
"""The transformed content element returned by the function"""

text: str = Field(
default="",
description=(
"Transformed content. Replaces the harmonized content at the same "
"location. On any failure the caller falls back to the original."
),
examples=["<h1>Quarterly Report</h1><p>Revenue grew 12% (redacted).</p>"],
)
model_config = ConfigDict(extra="ignore")


class KnowledgeHarmonizationPostProcessingV1Request(BaseModel):
"""Request for Knowledge Harmonization Post-Processing"""

input: KnowledgeHarmonizationPostProcessingV1DocElement = Field(
...,
description=(
"The single harmonized content element to transform. "
"One file per call, not a list."
),
)
properties: Optional[Dict[str, Union[str, int, float, bool]]] = Field(
default=None,
description=(
"Optional per-call key-value settings. Advisory: functions ignore "
"keys they do not know. Do not put secrets or PII here."
),
examples=[{"mode": "redact", "locale": "en-US"}],
)
model_config = ConfigDict(extra="ignore")


class KnowledgeHarmonizationPostProcessingV1Response(BaseModel):
"""Response for Knowledge Harmonization Post-Processing"""

output: KnowledgeHarmonizationPostProcessingV1Output = Field(
...,
description="The single transformed content element (1:1 with Request.input).",
)
model_config = ConfigDict(extra="ignore")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging

from datacustomcode.function import Runtime
from datacustomcode.function.feature_types.knowledge_harmonization_post_processing import (
KnowledgeHarmonizationPostProcessingV1Output,
KnowledgeHarmonizationPostProcessingV1Request,
KnowledgeHarmonizationPostProcessingV1Response,
)

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# Injected banner. Inline styles because the harmonized HTML is consumed standalone.
BANNER = (
'<section style="font-size:48px;font-weight:bold;text-align:center;'
'padding:24px;border:4px solid #000;">BYOC APPLIED</section>'
)


def apply_banner(html: str) -> str:
"""Return the HTML with the BYOC APPLIED banner inserted at the top of the body."""
lower = html.lower()
body_open = lower.find("<body")
if body_open != -1:
insert_at = html.find(">", body_open) + 1
return html[:insert_at] + BANNER + html[insert_at:]
return BANNER + html


def function(
request: KnowledgeHarmonizationPostProcessingV1Request, runtime: Runtime
) -> KnowledgeHarmonizationPostProcessingV1Response:
"""Insert a banner after <body> in the single harmonized document.

Args:
request: One harmonized content element in request.input.text.
runtime: Runtime instance supplied by the customer-function host.

Returns:
The same document with the banner, in response.output.text.
"""
logger.info("transform_html: received 1 document(s)")
logger.info("Runtime available: %s", runtime is not None)
return KnowledgeHarmonizationPostProcessingV1Response(
output=KnowledgeHarmonizationPostProcessingV1Output(
text=apply_banner(request.input.text)
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"input": {
"text": "<html><head><title>Doc</title></head><body><h1>Quarterly Report</h1><p>Online Remittance Instructions</p></body></html>",
"metadata": {
"source_dmo_fields": {
"FilePath__c": "quarterly_report.html",
"ContentType__c": "text/html",
"LastModified__c": "2026-03-25T02:01:24.918000"
}
}
}
}
7 changes: 6 additions & 1 deletion tests/test_describe_supported_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ def describe(request_type, response_type):
assert describe("SearchIndexChunkingV1Request", "dict") == chunk_pair
assert describe("dict", "SearchIndexChunkingV1Response") == chunk_pair
# Unrecognised types fall back to listing every supported pair
kh_pair = (
"KnowledgeHarmonizationPostProcessingV1Request/"
"KnowledgeHarmonizationPostProcessingV1Response "
"for use_in_feature: KnowledgeHarmonizationPostProcessing"
)
fallback = describe("dict", "dict")
assert fallback == chunk_pair
assert fallback == f"{chunk_pair} or {kh_pair}"