index.ts 5.89 KB
Newer Older
1
import { spawn } from 'child_process'
2
3
4
5
6
7
8
9
10
11
12
import {
  app,
  autoUpdater,
  dialog,
  Tray,
  Menu,
  BrowserWindow,
  MenuItemConstructorOptions,
  nativeTheme,
  systemPreferences,
} from 'electron'
Eva Ho's avatar
Eva Ho committed
13
import Store from 'electron-store'
Eva Ho's avatar
Eva Ho committed
14
15
import winston from 'winston'
import 'winston-daily-rotate-file'
Bruce MacDonald's avatar
Bruce MacDonald committed
16
import * as path from 'path'
Jeffrey Morgan's avatar
Jeffrey Morgan committed
17

Jeffrey Morgan's avatar
Jeffrey Morgan committed
18
import { analytics, id } from './telemetry'
Jeffrey Morgan's avatar
Jeffrey Morgan committed
19
import { installed } from './install'
Jeffrey Morgan's avatar
Jeffrey Morgan committed
20

Jeffrey Morgan's avatar
Jeffrey Morgan committed
21
22
require('@electron/remote/main').initialize()

Jeffrey Morgan's avatar
Jeffrey Morgan committed
23
const store = new Store()
24

25
26
27
let welcomeWindow: BrowserWindow | null = null

declare const MAIN_WINDOW_WEBPACK_ENTRY: string
28

29
30
31
32
33
34
35
36
37
const logger = winston.createLogger({
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({
      filename: path.join(app.getPath('home'), '.ollama', 'logs', 'server.log'),
      maxsize: 1024 * 1024 * 20,
      maxFiles: 5,
    }),
  ],
Jeffrey Morgan's avatar
Jeffrey Morgan committed
38
  format: winston.format.printf(info => info.message),
Eva Ho's avatar
Eva Ho committed
39
40
})

Eva Ho's avatar
Eva Ho committed
41
const SingleInstanceLock = app.requestSingleInstanceLock()
42
43
44
if (!SingleInstanceLock) {
  app.quit()
}
Jeffrey Morgan's avatar
Jeffrey Morgan committed
45

46
47
48
49
50
51
52
53
function firstRunWindow() {
  // Create the browser window.
  welcomeWindow = new BrowserWindow({
    width: 400,
    height: 500,
    frame: false,
    fullscreenable: false,
    resizable: false,
54
55
    movable: true,
    show: false,
56
57
58
59
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
    },
60
    alwaysOnTop: true,
61
62
63
64
65
66
  })

  require('@electron/remote/main').enable(welcomeWindow.webContents)

  // and load the index.html of the app.
  welcomeWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)
67
68
  welcomeWindow.on('ready-to-show', () => welcomeWindow.show())

69
70
  if (process.platform === 'darwin') {
    app.dock.hide()
71
  }
72
73
}

74
let tray: Tray | null = null
Eva Ho's avatar
Eva Ho committed
75

76
77
78
79
function setTray(updateAvailable: boolean) {
  const menuItemAvailable: MenuItemConstructorOptions = {
    label: 'Restart to update',
    click: () => autoUpdater.quitAndInstall(),
Eva Ho's avatar
Eva Ho committed
80
  }
Jeffrey Morgan's avatar
Jeffrey Morgan committed
81

82
83
84
85
  const menuItemUpToDate: MenuItemConstructorOptions = {
    label: 'Ollama is up to date',
    enabled: false,
  }
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  const menu = Menu.buildFromTemplate([
    ...(updateAvailable
      ? [{ label: 'An update is available', enabled: false }, menuItemAvailable]
      : [menuItemUpToDate]),
    { type: 'separator' },
    { role: 'quit', label: 'Quit Ollama', accelerator: 'Command+Q' },
  ])

  const iconPath = app.isPackaged
    ? updateAvailable
      ? path.join(process.resourcesPath, 'iconUpdateTemplate.png')
      : path.join(process.resourcesPath, 'iconTemplate.png')
    : updateAvailable
    ? path.join(__dirname, '..', '..', 'assets', 'iconUpdateTemplate.png')
    : path.join(__dirname, '..', '..', 'assets', 'iconTemplate.png')

  if (!tray) {
    tray = new Tray(iconPath)
  }
Jeffrey Morgan's avatar
Jeffrey Morgan committed
106

107
108
109
  tray.setToolTip(updateAvailable ? 'An update is available' : 'Ollama')
  tray.setContextMenu(menu)
  tray.setImage(iconPath)
110
111
112
113
}

if (require('electron-squirrel-startup')) {
  app.quit()
Jeffrey Morgan's avatar
Jeffrey Morgan committed
114
115
}

Jeffrey Morgan's avatar
Jeffrey Morgan committed
116
117
function server() {
  const binary = app.isPackaged
Eva Ho's avatar
Eva Ho committed
118
119
    ? path.join(process.resourcesPath, 'ollama')
    : path.resolve(process.cwd(), '..', 'ollama')
Jeffrey Morgan's avatar
Jeffrey Morgan committed
120

Eva Ho's avatar
Eva Ho committed
121
  const proc = spawn(binary, ['serve'])
Jeffrey Morgan's avatar
Jeffrey Morgan committed
122

Jeffrey Morgan's avatar
Jeffrey Morgan committed
123
  proc.stdout.on('data', data => {
Eva Ho's avatar
Eva Ho committed
124
125
    logger.info(data.toString().trim())
  })
Jeffrey Morgan's avatar
Jeffrey Morgan committed
126

Jeffrey Morgan's avatar
Jeffrey Morgan committed
127
  proc.stderr.on('data', data => {
Eva Ho's avatar
Eva Ho committed
128
129
    logger.error(data.toString().trim())
  })
130

Eva Ho's avatar
Eva Ho committed
131
  function restart() {
hoyyeva's avatar
hoyyeva committed
132
    setTimeout(server, 3000)
Eva Ho's avatar
Eva Ho committed
133
134
  }

Eva Ho's avatar
Eva Ho committed
135
  proc.on('exit', restart)
136

137
  app.on('before-quit', () => {
Eva Ho's avatar
Eva Ho committed
138
    proc.off('exit', restart)
Eva Ho's avatar
Eva Ho committed
139
140
    proc.kill()
  })
Jeffrey Morgan's avatar
Jeffrey Morgan committed
141
142
}

143
144
if (process.platform === 'darwin') {
  app.dock.hide()
145
146
}

147
app.on('ready', () => {
148
149
150
151
152
153
154
155
156
157
158
  setTray(false)

  if (app.isPackaged) {
    heartbeat()
    autoUpdater.checkForUpdates()
    setInterval(() => {
      heartbeat()
      autoUpdater.checkForUpdates()
    }, 60 * 60 * 1000)
  }

159
  if (process.platform === 'darwin') {
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    if (app.isPackaged) {
      if (!app.isInApplicationsFolder()) {
        const chosen = dialog.showMessageBoxSync({
          type: 'question',
          buttons: ['Move to Applications', 'Do Not Move'],
          message: 'Ollama works best when run from the Applications directory.',
          defaultId: 0,
          cancelId: 1,
        })

        if (chosen === 0) {
          try {
            app.moveToApplicationsFolder({
              conflictHandler: conflictType => {
                if (conflictType === 'existsAndRunning') {
                  dialog.showMessageBoxSync({
                    type: 'info',
                    message: 'Cannot move to Applications directory',
                    detail:
                      'Another version of Ollama is currently running from your Applications directory. Close it first and try again.',
                  })
                }
                return true
              },
            })
            return
          } catch (e) {
Eva Ho's avatar
Eva Ho committed
187
            logger.error(`[Move to Applications] Failed to move to applications folder - ${e.message}}`)
188
          }
189
190
191
        }
      }
    }
192
  }
Jeffrey Morgan's avatar
Jeffrey Morgan committed
193

194
  server()
195

196
  if (store.get('first-time-run') && installed()) {
197
    app.setLoginItemSettings({ openAtLogin: app.getLoginItemSettings().openAtLogin })
198
    return
199
  }
200
201
202
203

  // This is the first run or the CLI is no longer installed
  app.setLoginItemSettings({ openAtLogin: true })
  firstRunWindow()
204
})
Jeffrey Morgan's avatar
Jeffrey Morgan committed
205

Jeffrey Morgan's avatar
Jeffrey Morgan committed
206
207
208
209
210
211
212
213
214
215
216
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
Jeffrey Morgan's avatar
Jeffrey Morgan committed
217
218
219
autoUpdater.setFeedURL({
  url: `https://ollama.ai/api/update?os=${process.platform}&arch=${process.arch}&version=${app.getVersion()}`,
})
220

Jeffrey Morgan's avatar
Jeffrey Morgan committed
221
222
223
224
async function heartbeat() {
  analytics.track({
    anonymousId: id(),
    event: 'heartbeat',
Jeffrey Morgan's avatar
Jeffrey Morgan committed
225
226
227
    properties: {
      version: app.getVersion(),
    },
Jeffrey Morgan's avatar
Jeffrey Morgan committed
228
229
230
  })
}

231
autoUpdater.on('error', e => {
232
  console.error(`update check failed - ${e.message}`)
233
234
})

235
236
autoUpdater.on('update-downloaded', () => {
  setTray(true)
Jeffrey Morgan's avatar
Jeffrey Morgan committed
237
})