


























@@ -0,0 +1,83 @@
1+import type { AuthProfileStore } from "../../agents/auth-profiles/types.js";
2+import { resolveProviderEnvApiKeyCandidates } from "../../agents/model-auth-env-vars.js";
3+import { resolveEnvApiKey } from "../../agents/model-auth-env.js";
4+import { resolveAwsSdkEnvVarName } from "../../agents/model-auth-runtime-shared.js";
5+import { hasUsableCustomProviderApiKey } from "../../agents/model-auth.js";
6+import { resolveProviderAuthAliasMap } from "../../agents/provider-auth-aliases.js";
7+import { normalizeProviderIdForAuth } from "../../agents/provider-id.js";
8+import type { OpenClawConfig } from "../../config/types.openclaw.js";
9+import { readPersistedInstalledPluginIndexSync } from "../../plugins/installed-plugin-index-store.js";
10+11+export type ModelListAuthIndex = {
12+hasProviderAuth(provider: string): boolean;
13+};
14+15+export type CreateModelListAuthIndexParams = {
16+cfg: OpenClawConfig;
17+authStore: AuthProfileStore;
18+env?: NodeJS.ProcessEnv;
19+syntheticAuthProviderRefs?: readonly string[];
20+};
21+22+export const EMPTY_MODEL_LIST_AUTH_INDEX: ModelListAuthIndex = {
23+hasProviderAuth: () => false,
24+};
25+26+function normalizeAuthProvider(
27+provider: string,
28+aliasMap: Readonly<Record<string, string>>,
29+): string {
30+const normalized = normalizeProviderIdForAuth(provider);
31+return aliasMap[normalized] ?? normalized;
32+}
33+34+function listPersistedSyntheticAuthProviderRefs(): readonly string[] {
35+const index = readPersistedInstalledPluginIndexSync();
36+return index?.plugins.flatMap((plugin) => plugin.syntheticAuthRefs ?? []) ?? [];
37+}
38+39+export function createModelListAuthIndex(
40+params: CreateModelListAuthIndexParams,
41+): ModelListAuthIndex {
42+const env = params.env ?? process.env;
43+const aliasMap = resolveProviderAuthAliasMap({ config: params.cfg, env });
44+const envCandidateMap = resolveProviderEnvApiKeyCandidates({ config: params.cfg, env });
45+const authenticatedProviders = new Set<string>();
46+const addProvider = (provider: string | undefined) => {
47+if (!provider?.trim()) {
48+return;
49+}
50+authenticatedProviders.add(normalizeAuthProvider(provider, aliasMap));
51+};
52+53+for (const credential of Object.values(params.authStore.profiles ?? {})) {
54+addProvider(credential.provider);
55+}
56+57+for (const provider of Object.keys(envCandidateMap)) {
58+if (resolveEnvApiKey(provider, env, { aliasMap, candidateMap: envCandidateMap })) {
59+addProvider(provider);
60+}
61+}
62+63+if (resolveAwsSdkEnvVarName(env)) {
64+addProvider("amazon-bedrock");
65+}
66+67+for (const provider of Object.keys(params.cfg.models?.providers ?? {})) {
68+if (hasUsableCustomProviderApiKey(params.cfg, provider, env)) {
69+addProvider(provider);
70+}
71+}
72+73+for (const provider of params.syntheticAuthProviderRefs ??
74+listPersistedSyntheticAuthProviderRefs()) {
75+addProvider(provider);
76+}
77+78+return {
79+hasProviderAuth(provider: string): boolean {
80+return authenticatedProviders.has(normalizeAuthProvider(provider, aliasMap));
81+},
82+};
83+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。