MCPcopy
hub / github.com/21st-dev/1code / checkOfflineFallback

Function checkOfflineFallback

src/main/lib/claude/offline-handler.ts:31–110  ·  view source on GitHub ↗
(
  customConfig: CustomClaudeConfig | undefined,
  claudeCodeToken: string | null,
  selectedOllamaModel?: string | null,
  offlineModeEnabled: boolean = false,
)

Source from the content-addressed store, hash-verified

29 * @param offlineModeEnabled - Whether offline mode is enabled in settings
30 */
31export async function checkOfflineFallback(
32 customConfig: CustomClaudeConfig | undefined,
33 claudeCodeToken: string | null,
34 selectedOllamaModel?: string | null,
35 offlineModeEnabled: boolean = false,
36): Promise<OfflineCheckResult> {
37 // If custom config is provided, use it (highest priority)
38 if (customConfig) {
39 const isUsingOllama = customConfig.baseUrl.includes('localhost:11434')
40 return {
41 config: customConfig,
42 isUsingOllama,
43 }
44 }
45
46 // If offline mode is disabled in settings, skip all Ollama checks
47 // and just use Claude API (will fail with auth error if no token)
48 if (!offlineModeEnabled) {
49 return {
50 config: undefined,
51 isUsingOllama: false,
52 }
53 }
54
55 // Check internet FIRST - if offline, use Ollama regardless of auth
56 console.log('[Offline] Checking internet connectivity...')
57 const hasInternet = await checkInternetConnection()
58 console.log(`[Offline] Internet check result: ${hasInternet ? 'ONLINE' : 'OFFLINE'}`)
59
60 if (!hasInternet) {
61 // No internet - try Ollama
62 console.log('[Offline] No internet connection, checking Ollama...')
63
64 const ollamaStatus = await checkOllamaStatus()
65
66 if (!ollamaStatus.available) {
67 return {
68 config: undefined,
69 isUsingOllama: false,
70 error: 'No internet connection and Ollama is not available. Please install Ollama or connect to internet.',
71 }
72 }
73
74 if (!ollamaStatus.recommendedModel) {
75 return {
76 config: undefined,
77 isUsingOllama: false,
78 error: 'Ollama is running but no suitable model found. Please install a coding model like qwen2.5-coder:7b',
79 }
80 }
81
82 // Use Ollama with selected model or recommended model
83 console.log(`[Offline] selectedOllamaModel param: ${selectedOllamaModel || "(null/undefined)"}, recommendedModel: ${ollamaStatus.recommendedModel}`)
84 const modelToUse = selectedOllamaModel || ollamaStatus.recommendedModel
85 const config = getOllamaConfig(modelToUse)
86
87 console.log(`[Offline] Switching to Ollama (model: ${modelToUse})`)
88

Callers 1

claude.tsFile · 0.90

Calls 3

checkInternetConnectionFunction · 0.90
checkOllamaStatusFunction · 0.90
getOllamaConfigFunction · 0.90

Tested by

no test coverage detected