Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

5.8 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""Patch merge operation for strings, direct replacement for other field types."""

import asyncio
from typing import Any, Type

from openviking.session.memory.merge_op.base import (
    FieldType,
    MergeOp,
    MergeOpBase,
    SearchReplaceBlock,
    StrPatch,
    get_python_type_for_field,
)


class PatchOp(MergeOpBase):
    """Apply SEARCH/REPLACE or DELETE blocks to strings."""

    op_type = MergeOp.PATCH

    def __init__(self, field_type: FieldType):
        self._field_type = field_type

    def get_output_schema_type(self, field_type: FieldType) -> Type[Any]:
        if field_type == FieldType.STRING:
            return StrPatch
        return get_python_type_for_field(field_type)

    def get_output_schema_description(self, field_description: str) -> str:
        if self._field_type == FieldType.STRING:
            return (
                f"PATCH operation for '{field_description}'. Follow the shared "
                "SEARCH/REPLACE rules above. Use a DELETE block to remove complete lines."
            )
        return f"Replace value for '{field_description}'"

    async def apply(self, current_value: Any, patch_value: Any) -> Any:
        """
        Apply patch operation.

        For string fields (content):
        - StrPatch: use apply_str_patch()
        - other: full replacement

        For non-string fields:
        - Just replace with patch_value

        Special case: when current_value is None (no original content),
        use the replace value directly instead of trying to match.
        """
        # For non-string fields, just replace
        if self._field_type != FieldType.STRING:
            return patch_value

        # For string fields - check if current_value is None (no original)
        if current_value is None:
            # No original content - extract replace value from patch
            return self._extract_replace_when_no_original(patch_value)

        # For string fields with existing content
        from openviking.session.memory.merge_op.patch_handler import apply_str_patch

        current_str = current_value or ""

        # Case 1: StrPatch object - apply patch
        if isinstance(patch_value, StrPatch):
            # Filter out empty-search blocks when there's existing content.
            # Empty search with existing content is invalid (can't match empty string
            # against non-empty content), so skip those blocks.
            valid_blocks = [b for b in patch_value.blocks if b.search]
            if valid_blocks:
                return await asyncio.to_thread(
                    apply_str_patch,
                    current_str,
                    StrPatch(blocks=valid_blocks),
                )
            # All blocks have empty search → no valid patches, keep original
            return current_value

        # Case 2: dict form of StrPatch (from JSON parsing)
        if isinstance(patch_value, dict):
            if "blocks" in patch_value:
                try:
                    blocks = StrPatch.model_validate(patch_value).blocks
                    # Filter out empty-search blocks when there's existing content
                    valid_blocks = [b for b in blocks if b.search]
                    converted_patch = StrPatch(blocks=valid_blocks) if valid_blocks else None
                except Exception:
                    # If conversion fails, treat as simple replacement
                    return str(patch_value) if patch_value is not None else ""

                if converted_patch is not None:
                    return await asyncio.to_thread(
                        apply_str_patch,
                        current_str,
                        converted_patch,
                    )
                # All blocks have empty search → keep original
                return current_value

        # Case 3: Simple full replacement
        # 空字符串和 None 都保持原值
        if patch_value is None or patch_value == "":
            return current_value
        return patch_value

    def _extract_replace_when_no_original(self, patch_value: Any) -> Any:
        """
        Extract replace value from patch when there's no original content.

        Called when current_value is None - we use the replace content
        directly instead of trying to match against an empty string.

        Args:
            patch_value: The patch value (StrPatch, dict, or string)

        Returns:
            The replace content, or empty string if not available
        """
        # Case 1: StrPatch object — concatenate ALL blocks' replace content.
        # The schema instructs the model to split non-adjacent edits into
        # separate blocks, so a new memory routinely arrives as a multi-block
        # patch. Taking only blocks[0] would silently drop every subsequent
        # fact/preference the model extracted.
        if isinstance(patch_value, StrPatch):
            replaces = [b.replace for b in patch_value.blocks if b.replace is not None]
            return "\n".join(replaces) if replaces else ""

        # Case 2: dict form (from JSON parsing) — same, collect every block.
        if isinstance(patch_value, dict) and "blocks" in patch_value:
            replaces = []
            for block in patch_value.get("blocks", []):
                if isinstance(block, SearchReplaceBlock):
                    replace = block.replace
                elif isinstance(block, dict):
                    replace = block.get("replace")
                else:
                    replace = None
                if replace is not None:
                    replaces.append(replace)
            return "\n".join(replaces) if replaces else ""

        # Case 3: Simple string - use as is
        if isinstance(patch_value, str):
            return patch_value

        return ""