src/pqc_agent_wallet/integrations/langchain.py
| 1 | """LangChain-style secret provider. |
| 2 | |
| 3 | LangChain (and many frameworks) accept a callable that returns secrets. |
| 4 | `make_langchain_secret_provider(wallet)` returns a callable compatible |
| 5 | with that pattern. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | from typing import Callable |
| 11 | |
| 12 | from pqc_agent_wallet.errors import CredentialNotFoundError |
| 13 | from pqc_agent_wallet.vault import Wallet |
| 14 | |
| 15 | |
| 16 | def make_langchain_secret_provider(wallet: Wallet) -> Callable[[str], str]: |
| 17 | """Return a callable `provider(name) -> str` that reads from the wallet.""" |
| 18 | |
| 19 | def provider(name: str) -> str: |
| 20 | try: |
| 21 | return wallet.get(name) |
| 22 | except CredentialNotFoundError as exc: |
| 23 | raise KeyError(f"credential '{name}' not in wallet") from exc |
| 24 | |
| 25 | return provider |
| 26 | |
| 27 | |
| 28 | def walletize_env(wallet: Wallet, env_mapping: dict[str, str]) -> dict[str, str]: |
| 29 | """Resolve a mapping of {env_name: credential_name} to real values from the wallet. |
| 30 | |
| 31 | Useful for bulk-populating framework configs that expect env-style strings. |
| 32 | """ |
| 33 | out: dict[str, str] = {} |
| 34 | for env_name, cred_name in env_mapping.items(): |
| 35 | out[env_name] = wallet.get(cred_name) |
| 36 | return out |
| 37 | |