# auto-generated by @connexum/ai-governance — do not edit; regenerate via npx ai-governance init --agent-dir
# agent-id: {{AGENT_ID}}
# source:   {{SOURCE_FILE}}
"""
Governance shim for {{MODULE_NAME}}.
Wraps LangChain (OpenAI backend) with Connexum governance enforcement via
GovernanceCallbackHandler — intercepts agent/chain/tool lifecycle events.
Change pack assignments in governance.json; regenerate this file to apply.
"""
from __future__ import annotations

import importlib
import importlib.util
import sys
import os
from pathlib import Path

# ---------------------------------------------------------------------------
# Load original module
# ---------------------------------------------------------------------------
_src_dir = Path(__file__).parent
_orig_spec = importlib.util.spec_from_file_location(
    "{{MODULE_NAME}}._original",
    _src_dir / "{{SOURCE_FILE}}",
)
_orig_mod = importlib.util.module_from_spec(_orig_spec)  # type: ignore[arg-type]
_orig_spec.loader.exec_module(_orig_mod)  # type: ignore[union-attr]

# ---------------------------------------------------------------------------
# Set up LangChain governance callback
# ---------------------------------------------------------------------------
from connexum_governance import GovernanceClient
from connexum_governance.integrations.langchain import (
    GovernanceCallbackHandler,
    GovernedTool,
)

_gov_client = GovernanceClient(
    api_url=os.environ.get("CONNEXUM_API_URL", "http://localhost:4201"),
    license_key=os.environ.get("CONNEXUM_LICENSE_KEY", ""),
)
governance_callback = GovernanceCallbackHandler(
    client=_gov_client,
    agent_name="{{AGENT_ID}}",
    packs={{PACKS}},
)
"""
USAGE: Pass governance_callback to your LangChain chain or agent:

    chain.invoke(input, config={"callbacks": [governance_callback]})
    agent_executor.invoke(input, callbacks=[governance_callback])

NOTE: For guaranteed blocking on tool calls, wrap tools with GovernedTool:

    from {{MODULE_NAME}}.governed import governance_callback, GovernedTool
    safe_tool = GovernedTool(tool=my_tool, governance_callback=governance_callback)
"""

# ---------------------------------------------------------------------------
# Re-export original module's public API
# ---------------------------------------------------------------------------
for _name in dir(_orig_mod):
    if not _name.startswith("_"):
        globals()[_name] = getattr(_orig_mod, _name)

__all__ = [n for n in dir(_orig_mod) if not n.startswith("_")] + ["governance_callback", "GovernedTool"]
