opengap icon indicating copy to clipboard operation
opengap copied to clipboard

feat(adapters): add LangChain export adapter & runner with multi-provider support

Open AJAmit17 opened this issue 6 months ago • 1 comments

Summary

This is a clean rebase of #16 onto current main. The original PR predated several merges that touched the same integration files (src/adapters/index.ts, src/commands/export.ts, src/commands/run.ts), making the diff unreadable. This branch is three focused commits on top of b3a057a with all conflicts resolved — no existing adapters or options were removed.

Original PR: #16 (superseded by this one — closed) Closes: #2


Hey @shreyas-lyzr — this is the clean rebase you asked for. Quick summary of what changed since #16:

  • Rebased onto current main (b3a057a) as a single feature commit — no merge noise
  • All integration files (src/adapters/index.ts, src/commands/export.ts, src/commands/run.ts) updated additively — kiro, gitclaw, --workspace, and exportToClaudeCodeString are all intact
  • Both reviewer questions from #16 answered inline below (runner scope + provider precedence)
  • Also picked up and fixed the pre-existing TypeScript build failure from #79 so CI goes green (details in the section below)

What this adds

LangChain as a first-class export + run target alongside the existing adapters (kiro, gitclaw, codex, etc.):

File Role
src/adapters/langchain.ts Converts agent.yaml → standalone Python script using LangChain agent framework
src/runners/langchain.ts Executes the generated script in an auto-managed Python venv at ~/.gitagent/langchain-venv
src/adapters/langchain.test.ts Unit tests for provider detection and export shape
docs/adapters/langchain.md Full field-mapping table, provider table, usage examples

Integration changes (additive only):

  • src/adapters/index.ts — exportToLangChain added as a new export line
  • src/commands/export.ts — langchain case added; format help string updated
  • src/commands/run.ts — langchain case added; adapter help string updated

Also fixes: pre-existing TypeScript build failure from #79

PR #79 added financial_governance references in src/commands/validate.ts and src/adapters/shared.ts but the property was never declared on ComplianceConfig in src/utils/loader.ts, causing a TypeScript build failure on all Node versions in CI. This PR adds the missing type declaration and guards the unsafe <= 0 comparison that the strict types exposed.


Addressing reviewer questions from #16

Q: Is runWithLangChain invoking the user's installed Python LangChain (subprocess) or shipping a JS LangChain runtime?

Subprocess. The runner (src/runners/langchain.ts) bootstraps a Python venv at ~/.gitagent/langchain-venv on first run, installs the required langchain-openai or langchain-anthropic pip package depending on the detected provider, writes the generated Python script to a temp file, then invokes it via spawnSync. No JS LangChain runtime is shipped.

Q: Multi-provider switching — provider precedence and conflict behaviour?

Provider is determined solely from manifest.model.preferred (the model name string). The detection rules:

  • claude-* → Anthropic (langchain-anthropic, ANTHROPIC_API_KEY)
  • gpt-*, o1-*/o2-*/o3-*/o4-* → OpenAI (langchain-openai, OPENAI_API_KEY)
  • Anything else → hard error at export time (unsupported model)

The corresponding env var must be set at runtime; the runner validates and errors clearly if it is missing. There is no silent fallback between providers.

Q: Fidelity matrix row?

Happy to add a row to paper/tables/fidelity-matrix.csv in a follow-up, or I can include it here if preferred — just say the word.


Note on scope vs #80 (LangGraph + DeepAgents)

PR #80 added LangGraph (the state-machine layer on top of LangChain) and DeepAgents (a higher-level harness). This PR adds the base LangChain runtime. They compose — LangGraph imports from LangChain — so this is the foundational piece, not a duplicate.


Test plan

  • [x] npm run build passes (Node 18 / 20 / 22)
  • [x] gitagent export --format langchain -d <agent-dir> produces a valid Python script
  • [x] gitagent run -a langchain -d <agent-dir> -p "hello" bootstraps venv and executes
  • [x] Provider detection unit tests pass (langchain.test.ts)
  • [x] Manually validated on Windows 11, Node 22, Python 3.12 with OpenAI and Anthropic models
  • [x] All existing adapter cases untouched (kiro, gitclaw, codex, cursor, gemini, etc.)
  • [x] ComplianceConfig type fix unblocks CI for all open PRs targeting main

AJAmit17 avatar May 13 '26 18:05 AJAmit17

Thanks for the detailed review! Here's what's been addressed:

Type fix removed Commits 8174af8 and 434cdb9 have been dropped from this branch — that fix already landed in main via #83 and doesn't belong here.

detectProvider moved out of the public API Extracted to src/adapters/langchain-utils.ts. Both the runner and test file now import from there instead of from the adapter directly.

Renamed exportToLangChain → exportToLangChainString Updated in langchain.ts, index.ts, export.ts, runners/langchain.ts, and langchain.test.ts — consistent with the naming convention used by all other adapters.

Fidelity matrix row added langchain row added to paper/tables/fidelity-matrix.csv: F,F,F,F,P,-,P,F,P (tools and memory are partial since tools generate stubs and memory is inlined into the system prompt rather than managed natively).

Unhandled exception concern (export.ts) The case 'langchain': block sits inside the existing outer try/catch in export.ts (wraps the entire switch). Any error thrown by exportToLangChainString is caught, printed via error(e.message), and exits with code 1 — no unhandled exception path.

venv bootstrap Already addressed in the current code — runners/langchain.ts prints "Creating Python virtual environment..." and "Installing packages: ..." via info() before each spawnSync, and both spawns use stdio: 'inherit' so pip output is forwarded to the terminal.

Test import path langchain.test.ts already imports from './langchain.js' and now './langchain-utils.js' — both compiled output paths, consistent with other test files.

/^o\d/ regex Added a comment in langchain-utils.ts clarifying the intended coverage (o1, o3, o4-mini, etc.).

AJAmit17 avatar Jun 01 '26 10:06 AJAmit17