Skip to content

Commit 2ba8cd6

Browse files
committed
improvement(config): consolidate Ollama URL and CSP socket/Ollama hardcodes
Add getOllamaUrl() to urls.ts and replace inline env.OLLAMA_URL fallbacks in the provider and API route. Update CSP to use getSocketUrl(), getOllamaUrl(), and a local toWebSocketUrl() helper instead of hardcoded localhost strings.
1 parent 31362cd commit 2ba8cd6

4 files changed

Lines changed: 24 additions & 17 deletions

File tree

apps/sim/app/api/providers/ollama/models/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { createLogger } from '@sim/logger'
22
import { type NextRequest, NextResponse } from 'next/server'
3-
import { env } from '@/lib/core/config/env'
3+
import { getOllamaUrl } from '@/lib/core/utils/urls'
44
import type { ModelsObject } from '@/providers/ollama/types'
55
import { filterBlacklistedModels, isProviderBlacklisted } from '@/providers/utils'
66

77
const logger = createLogger('OllamaModelsAPI')
8-
const OLLAMA_HOST = env.OLLAMA_URL || 'http://localhost:11434'
8+
const OLLAMA_HOST = getOllamaUrl()
99

1010
/**
1111
* Get available Ollama models

apps/sim/lib/core/security/csp.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { env, getEnv } from '../config/env'
22
import { isDev, isHosted, isReactGrabEnabled } from '../config/feature-flags'
3+
import { getOllamaUrl, getSocketUrl } from '../utils/urls'
34

45
/**
56
* Content Security Policy (CSP) configuration builder
67
*/
78

9+
function toWebSocketUrl(httpUrl: string): string {
10+
return httpUrl.replace('http://', 'ws://').replace('https://', 'wss://')
11+
}
12+
813
function getHostnameFromUrl(url: string | undefined): string[] {
914
if (!url) return []
1015
try {
@@ -156,14 +161,11 @@ export const buildTimeCSPDirectives: CSPDirectives = {
156161
'connect-src': [
157162
...STATIC_CONNECT_SRC,
158163
env.NEXT_PUBLIC_APP_URL || '',
159-
...(env.OLLAMA_URL ? [env.OLLAMA_URL] : isDev ? ['http://localhost:11434'] : []),
164+
...(env.OLLAMA_URL ? [env.OLLAMA_URL] : isDev ? [getOllamaUrl()] : []),
160165
...(env.NEXT_PUBLIC_SOCKET_URL
161-
? [
162-
env.NEXT_PUBLIC_SOCKET_URL,
163-
env.NEXT_PUBLIC_SOCKET_URL.replace('http://', 'ws://').replace('https://', 'wss://'),
164-
]
166+
? [env.NEXT_PUBLIC_SOCKET_URL, toWebSocketUrl(env.NEXT_PUBLIC_SOCKET_URL)]
165167
: isDev
166-
? ['http://localhost:3002', 'ws://localhost:3002']
168+
? [getSocketUrl(), toWebSocketUrl(getSocketUrl())]
167169
: []),
168170
...getHostnameFromUrl(env.NEXT_PUBLIC_BRAND_LOGO_URL),
169171
...getHostnameFromUrl(env.NEXT_PUBLIC_PRIVACY_URL),
@@ -201,13 +203,9 @@ export function buildCSPString(directives: CSPDirectives): string {
201203
export function generateRuntimeCSP(): string {
202204
const appUrl = getEnv('NEXT_PUBLIC_APP_URL') || ''
203205

204-
const socketUrl = getEnv('NEXT_PUBLIC_SOCKET_URL') || (isDev ? 'http://localhost:3002' : '')
205-
const socketWsUrl = socketUrl
206-
? socketUrl.replace('http://', 'ws://').replace('https://', 'wss://')
207-
: isDev
208-
? 'ws://localhost:3002'
209-
: ''
210-
const ollamaUrl = getEnv('OLLAMA_URL') || (isDev ? 'http://localhost:11434' : '')
206+
const socketUrl = getEnv('NEXT_PUBLIC_SOCKET_URL') || (isDev ? getSocketUrl() : '')
207+
const socketWsUrl = socketUrl ? toWebSocketUrl(socketUrl) : ''
208+
const ollamaUrl = getEnv('OLLAMA_URL') || (isDev ? getOllamaUrl() : '')
211209

212210
const brandLogoDomains = getHostnameFromUrl(getEnv('NEXT_PUBLIC_BRAND_LOGO_URL'))
213211
const brandFaviconDomains = getHostnameFromUrl(getEnv('NEXT_PUBLIC_BRAND_FAVICON_URL'))

apps/sim/lib/core/utils/urls.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export function getEmailDomain(): string {
102102
}
103103

104104
const DEFAULT_SOCKET_URL = 'http://localhost:3002'
105+
const DEFAULT_OLLAMA_URL = 'http://localhost:11434'
105106

106107
/**
107108
* Returns the socket server URL for server-side internal API calls.
@@ -118,3 +119,11 @@ export function getSocketServerUrl(): string {
118119
export function getSocketUrl(): string {
119120
return getEnv('NEXT_PUBLIC_SOCKET_URL') || DEFAULT_SOCKET_URL
120121
}
122+
123+
/**
124+
* Returns the Ollama server URL.
125+
* Reads from OLLAMA_URL with a localhost fallback for development.
126+
*/
127+
export function getOllamaUrl(): string {
128+
return env.OLLAMA_URL || DEFAULT_OLLAMA_URL
129+
}

apps/sim/providers/ollama/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import OpenAI from 'openai'
33
import type { ChatCompletionCreateParamsStreaming } from 'openai/resources/chat/completions'
4-
import { env } from '@/lib/core/config/env'
4+
import { getOllamaUrl } from '@/lib/core/utils/urls'
55
import type { StreamingExecution } from '@/executor/types'
66
import { MAX_TOOL_ITERATIONS } from '@/providers'
77
import type { ModelsObject } from '@/providers/ollama/types'
@@ -18,7 +18,7 @@ import { useProvidersStore } from '@/stores/providers'
1818
import { executeTool } from '@/tools'
1919

2020
const logger = createLogger('OllamaProvider')
21-
const OLLAMA_HOST = env.OLLAMA_URL || 'http://localhost:11434'
21+
const OLLAMA_HOST = getOllamaUrl()
2222

2323
export const ollamaProvider: ProviderConfig = {
2424
id: 'ollama',

0 commit comments

Comments
 (0)