File size: 11,762 Bytes
91a883e
8628c98
8ca1978
 
91a883e
 
8628c98
91a883e
8ca1978
8628c98
8ca1978
 
 
 
8628c98
8ca1978
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ca1978
 
 
 
 
91a883e
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91a883e
 
8ca1978
 
91a883e
 
 
8ca1978
 
 
9c77d16
91a883e
8628c98
5753b43
8628c98
b7c0740
8628c98
b7c0740
8ca1978
985f8d7
8ca1978
d893281
8ca1978
 
 
 
985f8d7
 
 
8ca1978
 
8628c98
8ca1978
8628c98
 
8ca1978
539ee2b
8ca1978
8628c98
8ca1978
8628c98
 
 
8ca1978
8628c98
 
 
8ca1978
8628c98
8ca1978
 
8628c98
8ca1978
 
 
 
8628c98
8ca1978
 
 
8628c98
 
 
 
 
 
 
 
 
 
539ee2b
8628c98
 
 
539ee2b
8628c98
8ca1978
 
 
8628c98
 
 
 
 
 
 
 
539ee2b
 
8628c98
 
539ee2b
8628c98
f1a34f6
8ca1978
8628c98
627bc6c
8628c98
627bc6c
8628c98
8ca1978
8628c98
848f240
 
8628c98
 
848f240
 
 
8628c98
848f240
8ca1978
df40869
8ca1978
 
8628c98
8ca1978
8628c98
df40869
8ca1978
 
8628c98
f1a34f6
8628c98
 
 
 
 
 
 
 
 
 
 
539ee2b
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91a883e
 
 
8ca1978
91a883e
 
 
 
8628c98
 
 
 
 
 
 
91a883e
8628c98
91a883e
8628c98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
ALLI Code Generator Service (alli-codegen) -- Phase 7 Facade
Synthesizes deterministic, standalone, PEP 8-compliant Python scripts,
requirements.txt, and Dockerfiles directly from validated visual graphs.
"""

from collections import defaultdict
import json
import re
from typing import Any, Dict, List, Optional

from language_core.codegen_contracts import (
    BANNED_IMPORTS,
    PINNED_PACKAGE_VERSIONS,
    STANDARD_LIBS,
    format_literal,
    sanitize_identifier,
)
from language_core.execution_session import compute_graph_hash
from language_core.node_schema import default_registry, get_effective_node_schema
from lambdas.codegen.assembler import assemble_python_artifacts
from lambdas.codegen.context import (
    CodegenContext,
    NodeCodegenCtx,
    build_empty_workflow_response,
    compute_topological_order,
    run_preflight_codegen_checks,
)
from lambdas.codegen.emitters_control_oop import (
    emit_control_and_functions_node,
    emit_module_and_fallback_node,
    emit_string_and_oop_node,
)
from lambdas.codegen.emitters_core import (
    emit_category_node,
    emit_essentials_node,
    emit_values_math_io_node,
)
from lambdas.codegen.emitters_files_data import (
    emit_collection_node,
    emit_file_and_structured_node,
)

# Backwards compatible internal aliases
_sanitize_identifier = sanitize_identifier
_format_literal = format_literal

EMITTER_PIPELINE = (
    emit_category_node,
    emit_essentials_node,
    emit_values_math_io_node,
    emit_file_and_structured_node,
    emit_control_and_functions_node,
    emit_collection_node,
    emit_string_and_oop_node,
    emit_module_and_fallback_node,
)


def _register_node_outputs(nctx: NodeCodegenCtx, node_var: Optional[str]) -> None:
    if not node_var:
        return
    ovm = nctx.cg.output_var_map
    nid = nctx.node_id
    ovm[nid] = node_var
    for port in ("result", "out", "val", "value", "out_val"):
        ovm[f"{nid}:{port}"] = node_var
    if nctx.schema and hasattr(nctx.schema, "outputs"):
        for out in nctx.schema.outputs:
            out_id = getattr(out, "id", None) or (out.get("id") if isinstance(out, dict) else None)
            if out_id and f"{nid}:{out_id}" not in ovm:
                ovm[f"{nid}:{out_id}"] = node_var
    nctx.cg.last_return_var = node_var


def generate_code_logic(
    nodes: Optional[List[Dict[str, Any]]] = None,
    edges: Optional[List[Dict[str, Any]]] = None,
    execution_order: Optional[List[str]] = None,
    graph_name: str = "alli_workflow",
    dependencies: Optional[List[str]] = None,
    graph: Optional[Dict[str, Any]] = None,
    validate: bool = True,
    strict_validation: bool = False,
    pin_exact: bool = True,
) -> Dict[str, Any]:
    """Authoritative Python code generation engine (Phase 7)."""
    from language_core.workflow_migration import migrate_workflow

    if isinstance(nodes, dict) and "nodes" in nodes and graph is None:
        graph, nodes = nodes, None

    if graph and isinstance(graph, dict):
        graph = migrate_workflow(graph)
        raw_nodes = graph.get("nodes") or []
        raw_edges = graph.get("edges") if graph.get("edges") is not None else (graph.get("connections") or [])
        name = graph.get("name") or graph_name or "alli_workflow"
        deps = graph.get("dependencies") or dependencies or []
        graph_obj = graph
    else:
        migrated = migrate_workflow({"nodes": nodes or [], "edges": edges or []})
        raw_nodes = migrated.get("nodes") or []
        raw_edges = migrated.get("edges") or []
        name = graph_name or "alli_workflow"
        deps = dependencies or []
        graph_obj = {"name": name, "nodes": raw_nodes, "edges": raw_edges, "dependencies": deps}

    clean_name = sanitize_identifier(name.lower().replace(" ", "_"), "alli_workflow")
    clean_filename = f"{clean_name}.py"
    graph_fingerprint = compute_graph_hash(graph_obj)

    if not raw_nodes:
        return build_empty_workflow_response(name, clean_filename, graph_fingerprint)

    diagnostics = run_preflight_codegen_checks(raw_nodes, graph_obj, validate, strict_validation)
    blocking = [d for d in diagnostics if d.get("severity") == "error"]
    if blocking:
        return {
            "success": False, "python_code": "", "code": "", "requirements_txt": "", "dockerfile": "",
            "filename": clean_filename, "dependencies": [], "execution_order": [],
            "diagnostics": diagnostics, "source_map": [], "generator_version": "1.0.0",
            "graph_fingerprint": graph_fingerprint,
            "metadata": {"valid": False, "error_count": len(blocking), "ast_valid": False, "node_count": len(raw_nodes)},
        }

    filtered_nodes = [
        n for n in raw_nodes
        if str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "")
        not in ("system.workflow_group", "workflow_group", "workflowGroup")
    ]
    filtered_edges = [
        e for e in raw_edges
        if not str(e.get("source", "")).startswith("group_") and not str(e.get("target", "")).startswith("group_")
    ]
    node_map = {str(n["id"]): n for n in filtered_nodes if n.get("id")}
    ordered_ids, has_cycle = compute_topological_order(node_map, filtered_edges, execution_order)
    if has_cycle:
        return {
            "success": False, "python_code": "", "requirements_txt": "", "dockerfile": "",
            "filename": clean_filename, "dependencies": [], "execution_order": [],
            "diagnostics": [{"code": "DAG-001", "severity": "error", "message": "Cycle detected during code generation topological sequencing.", "fix_suggestion": "Remove circular feedback wires between procedural nodes."}],
            "source_map": [], "generator_version": "1.0.0", "graph_fingerprint": graph_fingerprint,
            "metadata": {"valid": False, "has_cycles": True},
        }

    incoming_edges_map: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
    for edge in filtered_edges:
        incoming_edges_map[str(edge.get("target") or edge.get("to_node") or "")].append(edge)

    cg = CodegenContext(diagnostics=diagnostics)
    for dep in deps:
        clean_dep = str(dep).strip()
        if clean_dep and clean_dep not in STANDARD_LIBS and clean_dep not in BANNED_IMPORTS:
            cg.discovered_requirements.add(clean_dep)

    def _emit_single_node(
        node_id: str,
        extra_indent: int = 0,
        incoming_override: Optional[List[Dict[str, Any]]] = None,
        build_only: bool = False,
    ) -> Tuple[Optional[NodeCodegenCtx], Optional[str]]:
        node = node_map.get(node_id)
        if not node:
            return None, None
        raw_type = str(node.get("node_type") or node.get("type") or node.get("data", {}).get("node_type") or node.get("data", {}).get("nodeType") or "").strip().lower()
        data = node.get("data", {}) or {}
        params = dict(node.get("parameters") if isinstance(node.get("parameters"), dict) else {})
        node_inputs = node.get("inputs") if isinstance(node.get("inputs"), dict) else (data.get("inputs") if isinstance(data.get("inputs"), dict) else {})
        label = str(data.get("label") or node.get("label") or raw_type or node_id)
        clean_lbl = re.sub(r"_+", "_", re.sub(r"[^a-zA-Z0-9_]", "_", label.lower()).strip("_"))
        if str(node_id).startswith("alli_primitive_") or len(str(node_id)) > 20:
            safe_id = f"{clean_lbl}_{cg.step_counter}" if (clean_lbl and not clean_lbl[0].isdigit() and len(clean_lbl) <= 24) else f"step_{cg.step_counter}"
        else:
            safe_id = sanitize_identifier(node_id.replace("-", "_"), f"node_{cg.step_counter}")

        schema = get_effective_node_schema(raw_type)
        cap_states = (
            (schema.capability_states if schema and hasattr(schema, "capability_states") else None)
            or (schema.verification.capability_states if schema and getattr(schema, "verification", None) else None)
            or data.get("capability_states") or params.get("capability_states") or node.get("capability_states")
            or (data.get("verification") or {}).get("capability_states")
        )
        if cap_states and cap_states.get("codegen_supported") is False:
            cg.diagnostics.append({"code": "CODEGEN_UNSUPPORTED", "severity": "error", "message": f"Code generation is unsupported for node '{label}' ({node_id}) of type '{raw_type}'.", "node_id": node_id})

        if schema and getattr(schema, "codegen", None):
            for imp in schema.codegen.imports:
                clean_imp = str(imp).strip()
                if clean_imp not in BANNED_IMPORTS:
                    cg.discovered_modules.add(clean_imp)
                    if clean_imp not in STANDARD_LIBS:
                        cg.discovered_requirements.add(clean_imp)
            for req in schema.codegen.requirements:
                clean_req = str(req).strip()
                if clean_req not in STANDARD_LIBS and clean_req not in BANNED_IMPORTS:
                    cg.discovered_requirements.add(clean_req)

        nctx = NodeCodegenCtx(
            node_id=node_id, raw_type=raw_type, label=label, clean_lbl=clean_lbl,
            safe_id=safe_id, params=params, data=data, node_inputs=node_inputs,
            incoming=incoming_override if incoming_override is not None else incoming_edges_map.get(node_id, []),
            schema=schema, cg=cg,
        )
        if build_only:
            return nctx, None
        start_line = len(cg.code_body_lines) + 1
        prev_len = len(cg.code_body_lines)
        cg.code_body_lines.append(f"    # Step {cg.step_counter}: [{label}]")
        node_var: Optional[str] = None
        for emit_fn in EMITTER_PIPELINE:
            handled, node_var = emit_fn(nctx)
            if handled:
                break
        _register_node_outputs(nctx, node_var)
        if extra_indent > 0:
            pad = " " * extra_indent
            for li in range(prev_len, len(cg.code_body_lines)):
                if cg.code_body_lines[li].strip():
                    cg.code_body_lines[li] = pad + cg.code_body_lines[li]
        cg.source_map.append({"node_id": node_id, "line_start": start_line, "line_end": len(cg.code_body_lines), "target_var": node_var, "statement_label": label})
        cg.step_counter += 1
        return nctx, node_var

    from lambdas.codegen.scoped_blocks import emit_workflow_with_scopes
    if not emit_workflow_with_scopes(cg, ordered_ids, node_map, filtered_edges, incoming_edges_map, _emit_single_node, _register_node_outputs):
        for node_id in ordered_ids:
            _emit_single_node(node_id)

    return assemble_python_artifacts(cg, name, clean_filename, ordered_ids, graph_fingerprint, pin_exact)


def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
    """AWS Lambda entrypoint for alli-codegen function."""
    try:
        body_raw = event.get("body", "{}")
        body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw
        result = generate_code_logic(
            nodes=body.get("nodes", []),
            edges=body.get("edges", []),
            execution_order=body.get("execution_order"),
            graph_name=body.get("name", "alli_workflow"),
            dependencies=body.get("dependencies", []),
            strict_validation=bool(body.get("strict_validation", False)),
            pin_exact=bool(body.get("pin_exact", True)),
        )
        return {"statusCode": 200, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS"}, "body": json.dumps(result)}
    except Exception as err:
        return {"statusCode": 500, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, "body": json.dumps({"error": str(err)})}