|
| 1 | +import { |
| 2 | + createProjectRuntime, |
| 3 | + type LoadedProjectConfig, |
| 4 | + type ProjectRuntimeContext, |
| 5 | + resolveProjectConfig, |
| 6 | + UserError, |
| 7 | +} from "@openagentpack/sdk"; |
| 8 | +import { |
| 9 | + assertProviderCredentials, |
| 10 | + type CredentialHost, |
| 11 | + injectProviderCredentials, |
| 12 | + normalizeInterpolatedProviderBlocks, |
| 13 | + prepareProviderEnv, |
| 14 | + scrubCredentialEnv, |
| 15 | +} from "./credentials.ts"; |
| 16 | +import { loadFileState } from "./file-state-manager.ts"; |
| 17 | +import { type HostContext, installSdkTransport } from "./transport.ts"; |
| 18 | + |
| 19 | +export { CREDENTIALS_NOTE, OFFLINE_NOTE } from "./credentials.ts"; |
| 20 | + |
| 21 | +/** |
| 22 | + * Whether this run requires provider keys: |
| 23 | + * - "all" (default) — online command: every provider declared in agents.yaml |
| 24 | + * must have a non-empty key after injection |
| 25 | + * - "none" — offline command (local config/state only), skip the check |
| 26 | + */ |
| 27 | +export type CredentialScope = "all" | "none"; |
| 28 | + |
| 29 | +interface AgentConfigOptions { |
| 30 | + resolveEnv?: boolean; |
| 31 | + projectName?: string; |
| 32 | + statePath?: string; |
| 33 | + credentials?: CredentialScope; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Resolve agents.yaml with credentials injected the bl way and scrubbed from the |
| 38 | + * environment — the shared credential spine for every SDK-engine command: |
| 39 | + * 1. prepare env (SDK bootstrap for non-bailian + placeholders so interpolation |
| 40 | + * never throws on a value we're about to supply/reject) |
| 41 | + * 2. resolve + interpolate the config |
| 42 | + * 3. override the bailian block with the CLI auth chain's credential (in-memory) |
| 43 | + * 4. scrub all credential vars from process.env (real values now live only in |
| 44 | + * the config object → provider adapters, never the environment) |
| 45 | + * 5. fail with a CLI-authoritative AUTH error if any provider's key is empty |
| 46 | + * (offline commands pass `credentials: "none"` to skip the check) |
| 47 | + */ |
| 48 | +export async function resolveAgentProjectConfig( |
| 49 | + host: CredentialHost, |
| 50 | + filePath: string, |
| 51 | + options: AgentConfigOptions = {}, |
| 52 | +): Promise<LoadedProjectConfig> { |
| 53 | + prepareProviderEnv(); |
| 54 | + const resolved = await resolveProjectConfig(filePath, options); |
| 55 | + normalizeInterpolatedProviderBlocks(resolved.config.providers); |
| 56 | + injectProviderCredentials(resolved.config.providers, host); |
| 57 | + scrubCredentialEnv(); |
| 58 | + if ((options.credentials ?? "all") !== "none") { |
| 59 | + assertProviderCredentials(resolved.config.providers); |
| 60 | + } |
| 61 | + return resolved; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Build a full ProjectRuntimeContext from a config file path — the standard |
| 66 | + * entry point for agent commands that need the SDK engine. Mirrors OpenAgentPack |
| 67 | + * CLI's buildCliRuntime: resolve config → load local state → assemble runtime. |
| 68 | + * Takes the host context first so every SDK-engine command wires the |
| 69 | + * instrumented transport (UA / tracking headers / verbose) and the bl-resolved, |
| 70 | + * in-memory-injected credential ({@link resolveAgentProjectConfig}) by construction. |
| 71 | + */ |
| 72 | +export async function buildAgentRuntime( |
| 73 | + host: HostContext & CredentialHost, |
| 74 | + filePath: string, |
| 75 | + options: AgentConfigOptions = {}, |
| 76 | +): Promise<ProjectRuntimeContext & { configPath: string }> { |
| 77 | + installSdkTransport(host); |
| 78 | + const { config, configPath, projectName } = await resolveAgentProjectConfig( |
| 79 | + host, |
| 80 | + filePath, |
| 81 | + options, |
| 82 | + ); |
| 83 | + const state = await loadFileState(configPath, options.statePath, projectName); |
| 84 | + const ctx = createProjectRuntime({ |
| 85 | + projectName, |
| 86 | + config, |
| 87 | + state, |
| 88 | + configPath, |
| 89 | + providers: config.providers, |
| 90 | + }); |
| 91 | + return { ...ctx, configPath }; |
| 92 | +} |
| 93 | + |
| 94 | +/** Ensure a user-supplied --provider value is actually configured in agents.yaml. */ |
| 95 | +export function assertProviderConfigured( |
| 96 | + ctx: ProjectRuntimeContext, |
| 97 | + provider: string | undefined, |
| 98 | +): void { |
| 99 | + if (!provider || provider === "all") return; |
| 100 | + if (ctx.providers.has(provider)) return; |
| 101 | + const available = Array.from(ctx.providers.keys()).join(", ") || "none"; |
| 102 | + throw new UserError( |
| 103 | + `Provider '${provider}' is not configured. Available providers: ${available}.`, |
| 104 | + ); |
| 105 | +} |
0 commit comments