Skip to content

Manifest_Function

Manifest_Function

CodeEngineManifest_Function dataclass

CodeEngineManifest_Function(
    ast_fn: FunctionDef,
    name: str,
    display_name: str,
    description: str,
    is_private: bool,
    editor_start_index: int,
    has_return: bool,
    output: CodeEngineManifest_Argument,
    editor_end_index: int = None,
    content: str = None,
    input_args: list[CodeEngineManifest_Argument] = list(),
    _target_from_api: dict = None,
)

from_ast_function_def classmethod

from_ast_function_def(
    ast_fn: FunctionDef, original_module_string
) -> CodeEngineManifest_Function

Create a complete function description as a CodeEngineManifest_Function object.

Combines all extracted function metadata into a structured FunctionMetadata dataclass instance that includes the function name, parameters, return type, docstring, and editor positioning information required for code engine integration.

Parameters:

Name Type Description Default
function_node FunctionDef

The AST node representing the function to describe

required

Returns:

Name Type Description
FunctionMetadata CodeEngineManifest_Function

Complete function metadata object containing: - name (str): Original function name - displayName (str): Human-readable function name - description (str): Function docstring - inputs (list[Dict]): Parameter descriptions - output (Dict): Return value description - variables (Dict): Variable descriptions (copy of inputs) - isPrivate (bool): Whether function is private (based on underscore prefix) - editorStartIndex (int): Character position in source file - hasReturn (bool): Whether function has return type annotation

Note

The isPrivate field is determined by checking if the function name starts with an underscore, following Python naming conventions.

Source code in src/crew_dcs/classes/DomoCodeEngine/Manifest_Function.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@classmethod
def from_ast_function_def(
    cls, ast_fn: ast.FunctionDef, original_module_string
) -> "CodeEngineManifest_Function":
    """
    Create a complete function description as a CodeEngineManifest_Function object.

    Combines all extracted function metadata into a structured FunctionMetadata
    dataclass instance that includes the function name, parameters, return type,
    docstring, and editor positioning information required for code engine integration.

    Args:
        function_node (ast.FunctionDef): The AST node representing the function to describe

    Returns:
        FunctionMetadata: Complete function metadata object containing:
            - name (str): Original function name
            - displayName (str): Human-readable function name
            - description (str): Function docstring
            - inputs (list[Dict]): Parameter descriptions
            - output (Dict): Return value description
            - variables (Dict): Variable descriptions (copy of inputs)
            - isPrivate (bool): Whether function is private (based on underscore prefix)
            - editorStartIndex (int): Character position in source file
            - hasReturn (bool): Whether function has return type annotation

    Note:
        The isPrivate field is determined by checking if the function name starts
        with an underscore, following Python naming conventions.
    """
    function_name = ast_fn.name
    is_private = function_name.startswith("_")

    input_args = [
        CodeEngineManifest_Argument.from_ast_arg(arg, ast_fn)
        for arg in ast_fn.args.args
    ]

    output_arg = CodeEngineManifest_Argument.from_ast_function_return_arg(ast_fn)

    editor_start_index = calculate_character_index_of_line_number(
        ast_fn.lineno - 1, full_text=original_module_string
    )

    editor_end_index = calculate_character_index_of_line_number(
        ast_fn.end_lineno - 1, full_text=original_module_string
    )

    content = "\n".join(
        original_module_string.split("\n")[
            ast_fn.lineno - 1 : ast_fn.end_lineno - 1
        ]
        if ast_fn.end_lineno
        else []
    )
    # content = original_module_string[editor_start_index:editor_end_index] if editor_end_index else None

    return cls(
        ast_fn=ast_fn,
        name=function_name,
        is_private=is_private,
        display_name=dmcv.convert_programming_text_to_title_case(function_name),
        description=extract_function_docstring(ast_fn),
        input_args=input_args,
        output=output_arg,
        editor_start_index=editor_start_index,
        editor_end_index=editor_end_index,
        content=content,
        has_return=ast_fn.returns is not None,
    )

to_dict

to_dict() -> dict

Serialize the function metadata and arguments to a manifest-compatible dictionary.

Source code in src/crew_dcs/classes/DomoCodeEngine/Manifest_Function.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def to_dict(self) -> dict:
    """
    Serialize the function metadata and arguments to a manifest-compatible dictionary.
    """
    return {
        "name": self.name,
        "displayName": self.display_name,
        "description": self.description,
        "isPrivate": self.is_private,
        "inputs": [
            arg.to_dict(include_orig=False) if hasattr(arg, "to_dict") else arg
            for arg in self.input_args
        ],
        "code": self.content,
        "output": (
            self.output.to_dict(include_orig=False)
            if hasattr(self.output, "to_dict")
            else self.output
        ),
    }

calculate_character_index_of_line_number

calculate_character_index_of_line_number(
    line_num: int, full_text: str
) -> int

Calculate the character index at the start of a given line (0-based) in the source file. Args: line_num (int): The line number (0-based) where the function starts. full_text (str): The full text of the file. Returns: int: Character position from the beginning of the file (0-based).

Source code in src/crew_dcs/classes/DomoCodeEngine/Manifest_Function.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def calculate_character_index_of_line_number(line_num: int, full_text: str) -> int:
    """
    Calculate the character index at the start of a given line (0-based) in the source file.
    Args:
        line_num (int): The line number (0-based) where the function starts.
        full_text (str): The full text of the file.
    Returns:
        int: Character position from the beginning of the file (0-based).
    """
    character_position = 0

    if not full_text:
        return character_position

    file_lines = full_text.split("\n")

    for line_index in range(line_num):
        if line_index < len(file_lines):
            character_position += len(file_lines[line_index]) + 1  # +1 for newline

    return character_position

extract_function_docstring

extract_function_docstring(
    function_node: FunctionDef,
) -> str

Extract the docstring from a function AST node if present.

Identifies and extracts the first string literal in a function body, which by Python convention serves as the function's documentation. Only extracts docstrings that are the first statement in the function.

Parameters:

Name Type Description Default
function_node FunctionDef

The function AST node to examine

required

Returns:

Name Type Description
str str

The function's docstring with whitespace stripped, or empty string if none found

Note

Follows Python docstring conventions where the first statement in a function body, if it's a string literal, is considered the docstring. Multi-line docstrings are preserved but leading/trailing whitespace is removed.

Source code in src/crew_dcs/classes/DomoCodeEngine/Manifest_Function.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def extract_function_docstring(function_node: ast.FunctionDef) -> str:
    """
    Extract the docstring from a function AST node if present.

    Identifies and extracts the first string literal in a function body,
    which by Python convention serves as the function's documentation.
    Only extracts docstrings that are the first statement in the function.

    Args:
        function_node (ast.FunctionDef): The function AST node to examine

    Returns:
        str: The function's docstring with whitespace stripped, or empty string if none found

    Note:
        Follows Python docstring conventions where the first statement in a function
        body, if it's a string literal, is considered the docstring. Multi-line
        docstrings are preserved but leading/trailing whitespace is removed.
    """
    if (
        function_node.body
        and isinstance(function_node.body[0], ast.Expr)
        and isinstance(function_node.body[0].value, ast.Constant)
        and isinstance(function_node.body[0].value.value, str)
    ):
        return function_node.body[0].value.value.strip()
    return ""

extract_return_type_annotation

extract_return_type_annotation(
    function_node: FunctionDef,
) -> str

Extract return type annotation from a function AST node.

Converts the function's return type annotation from AST format back to a string representation for type analysis and schema mapping.

Note

Only works with Python 3.9+ that has ast.unparse(). For older versions, returns empty string if return annotation exists but cannot be parsed.

Source code in src/crew_dcs/classes/DomoCodeEngine/Manifest_Function.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def extract_return_type_annotation(function_node: ast.FunctionDef) -> str:
    """
    Extract return type annotation from a function AST node.

    Converts the function's return type annotation from AST format back to
    a string representation for type analysis and schema mapping.

    Note:
        Only works with Python 3.9+ that has ast.unparse(). For older versions,
        returns empty string if return annotation exists but cannot be parsed.
    """
    if function_node.returns and hasattr(ast, "unparse"):
        return ast.unparse(function_node.returns)
    return None