Files
deepagentsjs/scripts/vitest-setup-langsmith-gateway.ts
T
Christian Bromann b215e70d88 ci: route integration tests through LangSmith LLM Gateway (#697)
## Summary
- Add a shared Vitest setup that remaps Anthropic/OpenAI/etc. clients
onto the LangSmith LLM Gateway when `LANGSMITH_GATEWAY_KEY` is present.
- Wire that setup into all package int-test Vitest configs.
- Pass `LANGSMITH_GATEWAY_KEY` into the CI integration-test job (falls
back to direct provider keys if unset).
2026-07-20 16:28:01 -07:00

52 lines
1.9 KiB
TypeScript

/**
* Route integration-test LLM calls through the LangSmith LLM Gateway.
*
* When `LANGSMITH_GATEWAY_KEY` (or `LC_GATEWAY_KEY`) is set, provider SDKs are
* pointed at `https://gateway.smith.langchain.com` and authenticate with that
* key. Provider secrets stay in LangSmith; local `ANTHROPIC_API_KEY` /
* `OPENAI_API_KEY` values are overridden for the test process.
*
* @see https://docs.langchain.com/langsmith/llm-gateway-quickstart
*/
import path from "node:path";
import { fileURLToPath } from "node:url";
import dotenv from "dotenv";
const DEFAULT_GATEWAY_BASE_URL = "https://gateway.smith.langchain.com";
dotenv.config({
path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.env"),
});
/**
* Configure process.env so Anthropic/OpenAI/Gemini/etc. clients use the gateway.
* No-op when no gateway key is present (keeps direct-provider local/CI fallbacks).
*/
const gatewayKey =
process.env.LANGSMITH_GATEWAY_KEY?.trim() ||
process.env.LC_GATEWAY_KEY?.trim();
if (gatewayKey) {
const baseUrl = (
process.env.LANGSMITH_GATEWAY_BASE_URL?.trim() || DEFAULT_GATEWAY_BASE_URL
).replace(/\/+$/, "");
process.env.ANTHROPIC_BASE_URL = `${baseUrl}/anthropic`;
process.env.OPENAI_BASE_URL = `${baseUrl}/openai/v1`;
process.env.GOOGLE_GEMINI_BASE_URL = `${baseUrl}/gemini`;
process.env.FIREWORKS_BASE_URL = `${baseUrl}/fireworks`;
process.env.BASETEN_BASE_URL = `${baseUrl}/baseten/v1`;
// Gateway auth: LangSmith API key stands in for every provider SDK key.
process.env.ANTHROPIC_API_KEY = gatewayKey;
process.env.OPENAI_API_KEY = gatewayKey;
process.env.GEMINI_API_KEY = gatewayKey;
process.env.GOOGLE_API_KEY = gatewayKey;
process.env.FIREWORKS_API_KEY = gatewayKey;
process.env.BASETEN_API_KEY = gatewayKey;
// Anthropic SDK also honors this for proxy setups (matches org MDM/Kandji env).
process.env.ANTHROPIC_CUSTOM_HEADERS = `X-Api-Key: ${gatewayKey}`;
}