






















@@ -1,9 +1,122 @@
11import { buildAuthProfileId } from "../agents/auth-profiles/identity.js";
22import type { AuthProfileCredential } from "../agents/auth-profiles/types.js";
3-import { normalizeAgentModelRefForConfig } from "../config/model-input.js";
3+import { normalizeConfiguredProviderCatalogModelId } from "../agents/model-ref-shared.js";
4+import {
5+normalizeAgentModelMapForConfig,
6+normalizeAgentModelRefForConfig,
7+} from "../config/model-input.js";
8+import type { ModelProviderConfig } from "../config/types.models.js";
49import type { OpenClawConfig } from "../config/types.openclaw.js";
510import type { ProviderAuthResult } from "../plugins/types.js";
61112+function normalizeAgentModelConfigForAuthResult(value: unknown): unknown {
13+if (typeof value === "string") {
14+return normalizeAgentModelRefForConfig(value);
15+}
16+if (!value || typeof value !== "object" || Array.isArray(value)) {
17+return value;
18+}
19+20+let mutated = false;
21+const next: Record<string, unknown> = { ...(value as Record<string, unknown>) };
22+if (typeof next.primary === "string") {
23+const primary = normalizeAgentModelRefForConfig(next.primary);
24+if (primary !== next.primary) {
25+next.primary = primary;
26+mutated = true;
27+}
28+}
29+if (Array.isArray(next.fallbacks)) {
30+const originalFallbacks = next.fallbacks;
31+const fallbacks = originalFallbacks.map((fallback) =>
32+typeof fallback === "string" ? normalizeAgentModelRefForConfig(fallback) : fallback,
33+);
34+if (fallbacks.some((fallback, index) => fallback !== originalFallbacks[index])) {
35+next.fallbacks = fallbacks;
36+mutated = true;
37+}
38+}
39+return mutated ? next : value;
40+}
41+42+function normalizeProviderConfigModelIdsForAuthResult(
43+provider: string,
44+providerConfig: ModelProviderConfig,
45+): ModelProviderConfig {
46+const models = providerConfig.models;
47+if (!Array.isArray(models) || models.length === 0) {
48+return providerConfig;
49+}
50+51+let mutated = false;
52+const nextModels = models.map((model) => {
53+const id = normalizeConfiguredProviderCatalogModelId(provider, model.id);
54+if (id === model.id) {
55+return model;
56+}
57+mutated = true;
58+return Object.assign({}, model, { id });
59+});
60+return mutated ? { ...providerConfig, models: nextModels } : providerConfig;
61+}
62+63+function normalizeProviderAuthConfigPatchModelRefs(
64+patch: Partial<OpenClawConfig>,
65+): Partial<OpenClawConfig> {
66+let next = patch;
67+const defaults = patch.agents?.defaults;
68+if (defaults) {
69+let nextDefaults = defaults;
70+if (defaults.model !== undefined) {
71+const model = normalizeAgentModelConfigForAuthResult(defaults.model);
72+if (model !== defaults.model) {
73+nextDefaults = { ...nextDefaults, model: model as typeof defaults.model };
74+}
75+}
76+if (defaults.models) {
77+const models = normalizeAgentModelMapForConfig(defaults.models);
78+if (models !== defaults.models) {
79+nextDefaults = { ...nextDefaults, models };
80+}
81+}
82+if (nextDefaults !== defaults) {
83+next = {
84+ ...next,
85+agents: {
86+ ...next.agents,
87+defaults: nextDefaults,
88+},
89+};
90+}
91+}
92+93+const providers = patch.models?.providers;
94+if (!providers) {
95+return next;
96+}
97+98+let mutated = false;
99+const nextProviders = { ...providers };
100+for (const [provider, providerConfig] of Object.entries(providers)) {
101+const normalized = normalizeProviderConfigModelIdsForAuthResult(provider, providerConfig);
102+if (normalized === providerConfig) {
103+continue;
104+}
105+nextProviders[provider] = normalized;
106+mutated = true;
107+}
108+109+return mutated
110+ ? {
111+ ...next,
112+models: {
113+ ...next.models,
114+providers: nextProviders,
115+},
116+}
117+ : next;
118+}
119+7120/** Build the standard auth result payload for OAuth-style provider login flows. */
8121export function buildOauthProviderAuthResult(params: {
9122providerId: string;
@@ -41,17 +154,18 @@ export function buildOauthProviderAuthResult(params: {
4115442155return {
43156profiles: [{ profileId, credential }],
44-configPatch:
157+configPatch: normalizeProviderAuthConfigPatchModelRefs(
45158params.configPatch ??
46-({
47-agents: {
48-defaults: {
49-models: {
50-[defaultModel]: {},
159+({
160+agents: {
161+defaults: {
162+models: {
163+[defaultModel]: {},
164+},
51165},
52166},
53-},
54- } as Partial<OpenClawConfig>),
167+} as Partial<OpenClawConfig>),
168+),
55169 defaultModel,
56170notes: params.notes,
57171};
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。