|
1 | 1 | import { bootstrapRuntimeCredentialsSync } from "@openagentpack/sdk"; |
| 2 | +import { readConfigFile } from "bailian-cli-core"; |
2 | 3 |
|
3 | 4 | let bootstrapped = false; |
4 | 5 |
|
| 6 | +/** |
| 7 | + * Shared `--help` note documenting where agent commands get provider |
| 8 | + * credentials. Mirrors the credential-source hint bl's native commands surface |
| 9 | + * (knowledge / usage / token-plan), adapted for the SDK's env-based resolution |
| 10 | + * and the {@link bridgeBailianCredentials} fallback. Attach to every command |
| 11 | + * that loads agents.yaml. `bl` prefix is safe: agent commands ship on `bl` only. |
| 12 | + */ |
| 13 | +export const CREDENTIALS_NOTE = [ |
| 14 | + "Credentials come from the env vars referenced in agents.yaml (e.g. ${DASHSCOPE_API_KEY}, ${BAILIAN_WORKSPACE_ID}).", |
| 15 | + "For the bailian provider, bl fills these from your login as a fallback: `bl auth login` (API key) and `bl config set workspace_id <id>`.", |
| 16 | +]; |
| 17 | + |
| 18 | +/** |
| 19 | + * Bridge bl's own login state into the env vars the OpenAgentPack SDK reads for |
| 20 | + * the bailian provider. bl persists `api_key` / `workspace_id` in |
| 21 | + * `~/.bailian/config.json` (via `bl auth login` / `bl config set`); mirror them |
| 22 | + * onto `DASHSCOPE_API_KEY` / `BAILIAN_WORKSPACE_ID` so users don't have to |
| 23 | + * re-declare the same credentials for `bl agent *`. |
| 24 | + * |
| 25 | + * Lowest priority: only fills a var that is still unset, so anything already in |
| 26 | + * the environment (shell export, `.env`, or `~/.agents/config.json` — which the |
| 27 | + * SDK bootstrap has already applied) wins. Missing values are left alone; the |
| 28 | + * SDK surfaces its own error when interpolation can't resolve, and non-bailian |
| 29 | + * providers (claude/qoder/ark) don't need DashScope credentials at all. |
| 30 | + */ |
| 31 | +export function bridgeBailianCredentials(): void { |
| 32 | + const file = readConfigFile(); |
| 33 | + if (!process.env.DASHSCOPE_API_KEY?.trim() && file.api_key) { |
| 34 | + process.env.DASHSCOPE_API_KEY = file.api_key; |
| 35 | + } |
| 36 | + if (!process.env.BAILIAN_WORKSPACE_ID?.trim() && file.workspace_id) { |
| 37 | + process.env.BAILIAN_WORKSPACE_ID = file.workspace_id; |
| 38 | + } |
| 39 | +} |
| 40 | + |
5 | 41 | /** |
6 | 42 | * Lazily load `.env` and `~/.agents/config.json` into `process.env` so the |
7 | 43 | * OpenAgentPack SDK can resolve provider credentials (e.g. DASHSCOPE_API_KEY, |
8 | | - * BAILIAN_WORKSPACE_ID). Safe to call repeatedly — only the first call does I/O. |
| 44 | + * BAILIAN_WORKSPACE_ID), then bridge bl's own config as a fallback. Safe to call |
| 45 | + * repeatedly — only the first call does I/O. |
| 46 | + * |
| 47 | + * Effective precedence for bailian provider fields: |
| 48 | + * ~/.agents/config.json > shell env > .env > ~/.bailian/config.json |
9 | 49 | * |
10 | 50 | * NOTE: agent commands declare `auth: "none"` and let the SDK own credential |
11 | | - * resolution. This is the documented tradeoff of the wholesale SDK integration; |
12 | | - * bl's own `--api-key` / `bl auth login` flow is bridged separately as a follow-up. |
| 51 | + * resolution. The bl-config bridge is a best-effort fallback; it never overrides |
| 52 | + * a value the SDK bootstrap already resolved. |
13 | 53 | */ |
14 | 54 | export function ensureCredentials(): void { |
15 | 55 | if (bootstrapped) return; |
16 | 56 | bootstrapped = true; |
17 | 57 | bootstrapRuntimeCredentialsSync(); |
| 58 | + bridgeBailianCredentials(); |
18 | 59 | } |
0 commit comments