惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
A
About on SuperTechFans
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
D
DataBreaches.Net
B
Blog
博客园 - 【当耐特】
爱范儿
爱范儿
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
WordPress大学
WordPress大学
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
量子位
G
Google Developers Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(ollama): scope auth to local hosts · openclaw/opencla...
steipete · 2026-04-27 · via Recent Commits to openclaw:main

@@ -43,18 +43,85 @@ function readStringValue(value: unknown): string | undefined {

43434444

export function resolveOllamaDiscoveryApiKey(params: {

4545

env: NodeJS.ProcessEnv;

46+

baseUrl?: string;

4647

explicitApiKey?: string;

48+

hasDeclaredApiKey?: boolean;

4749

resolvedApiKey?: unknown;

48-

}): string {

49-

const envApiKey = params.env.OLLAMA_API_KEY?.trim() ? "OLLAMA_API_KEY" : undefined;

50+

}): string | undefined {

51+

const envValue = normalizeOptionalString(params.env.OLLAMA_API_KEY);

52+

const envApiKey = envValue ? "OLLAMA_API_KEY" : undefined;

5053

const resolvedApiKey = normalizeOptionalString(params.resolvedApiKey);

51-

return envApiKey ?? params.explicitApiKey ?? resolvedApiKey ?? OLLAMA_DEFAULT_API_KEY;

54+

const explicitApiKey = normalizeOptionalString(params.explicitApiKey);

55+

if (explicitApiKey) {

56+

return explicitApiKey;

57+

}

58+

if (params.hasDeclaredApiKey && resolvedApiKey) {

59+

return resolvedApiKey;

60+

}

61+

if (!isLocalOllamaBaseUrl(params.baseUrl)) {

62+

return envApiKey ?? (resolvedApiKey !== OLLAMA_DEFAULT_API_KEY ? resolvedApiKey : undefined);

63+

}

64+

if (resolvedApiKey && resolvedApiKey !== envValue && resolvedApiKey !== OLLAMA_DEFAULT_API_KEY) {

65+

return resolvedApiKey;

66+

}

67+

return OLLAMA_DEFAULT_API_KEY;

5268

}

53695470

function shouldSkipAmbientOllamaDiscovery(env: NodeJS.ProcessEnv): boolean {

5571

return Boolean(env.VITEST) || env.NODE_ENV === "test";

5672

}

577374+

const LOCAL_OLLAMA_HOSTNAMES = new Set(["localhost", "127.0.0.1", "0.0.0.0", "::1", "::"]);

75+76+

function isIpv4PrivateRange(host: string): boolean {

77+

if (!/^\d+\.\d+\.\d+\.\d+$/.test(host)) {

78+

return false;

79+

}

80+

const octets = host.split(".").map((part) => Number.parseInt(part, 10));

81+

if (octets.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {

82+

return false;

83+

}

84+

const [a, b] = octets;

85+

return a === 10 || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168);

86+

}

87+88+

function isIpv6LocalRange(host: string): boolean {

89+

const lower = host.toLowerCase();

90+

return /^fe[89ab][0-9a-f]:/.test(lower) || /^f[cd][0-9a-f]{2}:/.test(lower);

91+

}

92+93+

export function isLocalOllamaBaseUrl(baseUrl: string | undefined | null): boolean {

94+

if (!baseUrl) {

95+

return true;

96+

}

97+

let parsed: URL;

98+

try {

99+

parsed = new URL(baseUrl);

100+

} catch {

101+

return false;

102+

}

103+

let host = parsed.hostname.toLowerCase();

104+

if (host.startsWith("[") && host.endsWith("]")) {

105+

host = host.slice(1, -1);

106+

}

107+

return (

108+

LOCAL_OLLAMA_HOSTNAMES.has(host) ||

109+

host.endsWith(".local") ||

110+

isIpv4PrivateRange(host) ||

111+

isIpv6LocalRange(host) ||

112+

(!host.includes(".") && !host.includes(":"))

113+

);

114+

}

115+116+

export function shouldUseSyntheticOllamaAuth(

117+

providerConfig: ModelProviderConfig | undefined,

118+

): boolean {

119+

if (!hasMeaningfulExplicitOllamaConfig(providerConfig)) {

120+

return false;

121+

}

122+

return isLocalOllamaBaseUrl(readProviderBaseUrl(providerConfig));

123+

}

124+58125

export function hasMeaningfulExplicitOllamaConfig(

59126

providerConfig: ModelProviderConfig | undefined,

60127

): boolean {

@@ -116,17 +183,22 @@ export async function resolveOllamaDiscoveryResult(params: {

116183

ollamaKey.trim().length > 0 &&

117184

ollamaKey.trim() !== OLLAMA_DEFAULT_API_KEY;

118185

const explicitApiKey = readStringValue(explicit?.apiKey);

186+

const hasDeclaredApiKey = explicit?.apiKey !== undefined;

119187

if (hasExplicitModels && explicit) {

188+

const baseUrl = resolveOllamaApiBase(readProviderBaseUrl(explicit) ?? OLLAMA_DEFAULT_BASE_URL);

189+

const apiKey = resolveOllamaDiscoveryApiKey({

190+

env: params.ctx.env,

191+

baseUrl,

192+

explicitApiKey,

193+

hasDeclaredApiKey,

194+

resolvedApiKey: ollamaKey,

195+

});

120196

return {

121197

provider: {

122198

...explicit,

123-

baseUrl: resolveOllamaApiBase(readProviderBaseUrl(explicit) ?? OLLAMA_DEFAULT_BASE_URL),

199+

baseUrl,

124200

api: explicit.api ?? "ollama",

125-

apiKey: resolveOllamaDiscoveryApiKey({

126-

env: params.ctx.env,

127-

explicitApiKey,

128-

resolvedApiKey: ollamaKey,

129-

}),

201+

...(apiKey ? { apiKey } : {}),

130202

},

131203

};

132204

}

@@ -141,20 +213,24 @@ export async function resolveOllamaDiscoveryResult(params: {

141213

return null;

142214

}

143215144-

const provider = await params.buildProvider(readProviderBaseUrl(explicit), {

216+

const configuredBaseUrl = readProviderBaseUrl(explicit);

217+

const provider = await params.buildProvider(configuredBaseUrl, {

145218

quiet: !hasRealOllamaKey && !hasMeaningfulExplicitConfig,

146219

});

147220

if (provider.models?.length === 0 && !ollamaKey && !explicit?.apiKey) {

148221

return null;

149222

}

223+

const apiKey = resolveOllamaDiscoveryApiKey({

224+

env: params.ctx.env,

225+

baseUrl: provider.baseUrl ?? configuredBaseUrl,

226+

explicitApiKey,

227+

hasDeclaredApiKey,

228+

resolvedApiKey: ollamaKey,

229+

});

150230

return {

151231

provider: {

152232

...provider,

153-

apiKey: resolveOllamaDiscoveryApiKey({

154-

env: params.ctx.env,

155-

explicitApiKey,

156-

resolvedApiKey: ollamaKey,

157-

}),

233+

...(apiKey ? { apiKey } : {}),

158234

},

159235

};

160236

}