2023-07-27 04:02:13 +00:00
|
|
|
import { spawn, ChildProcess } from 'child_process'
|
2023-07-27 20:25:36 +00:00
|
|
|
import { app, autoUpdater, dialog, Tray, Menu, BrowserWindow, MenuItemConstructorOptions, nativeTheme } from 'electron'
|
2023-07-07 17:43:26 +00:00
|
|
|
import Store from 'electron-store'
|
2023-07-11 17:33:32 +00:00
|
|
|
import winston from 'winston'
|
|
|
|
import 'winston-daily-rotate-file'
|
2023-06-23 22:38:22 +00:00
|
|
|
import * as path from 'path'
|
2023-06-27 16:35:51 +00:00
|
|
|
|
2023-07-06 21:32:48 +00:00
|
|
|
import { analytics, id } from './telemetry'
|
2023-07-17 03:25:50 +00:00
|
|
|
import { installed } from './install'
|
2023-07-06 21:32:48 +00:00
|
|
|
|
2023-06-27 16:35:51 +00:00
|
|
|
require('@electron/remote/main').initialize()
|
|
|
|
|
2023-07-27 04:02:13 +00:00
|
|
|
if (require('electron-squirrel-startup')) {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
|
2023-07-08 17:18:34 +00:00
|
|
|
const store = new Store()
|
2023-07-26 18:04:36 +00:00
|
|
|
|
2023-07-14 23:34:24 +00:00
|
|
|
let welcomeWindow: BrowserWindow | null = null
|
|
|
|
|
|
|
|
declare const MAIN_WINDOW_WEBPACK_ENTRY: string
|
2023-07-06 22:05:31 +00:00
|
|
|
|
2023-07-11 22:51:59 +00:00
|
|
|
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,
|
|
|
|
}),
|
|
|
|
],
|
2023-07-17 03:26:12 +00:00
|
|
|
format: winston.format.printf(info => info.message),
|
2023-07-11 17:33:32 +00:00
|
|
|
})
|
|
|
|
|
2023-07-27 04:02:13 +00:00
|
|
|
app.on('ready', () => {
|
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
|
|
|
if (!gotTheLock) {
|
|
|
|
app.exit(0)
|
2023-07-27 14:53:38 +00:00
|
|
|
return
|
2023-07-27 04:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.on('second-instance', () => {
|
|
|
|
if (app.hasSingleInstanceLock()) {
|
|
|
|
app.releaseSingleInstanceLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proc) {
|
|
|
|
proc.off('exit', restart)
|
|
|
|
proc.kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
app.exit(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.focus({ steal: true })
|
|
|
|
|
|
|
|
init()
|
|
|
|
})
|
|
|
|
|
2023-07-14 23:34:24 +00:00
|
|
|
function firstRunWindow() {
|
|
|
|
// Create the browser window.
|
|
|
|
welcomeWindow = new BrowserWindow({
|
|
|
|
width: 400,
|
|
|
|
height: 500,
|
|
|
|
frame: false,
|
|
|
|
fullscreenable: false,
|
|
|
|
resizable: false,
|
2023-07-17 03:25:11 +00:00
|
|
|
movable: true,
|
|
|
|
show: false,
|
2023-07-14 23:34:24 +00:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false,
|
|
|
|
},
|
2023-07-17 03:25:11 +00:00
|
|
|
alwaysOnTop: true,
|
2023-07-14 23:34:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
require('@electron/remote/main').enable(welcomeWindow.webContents)
|
|
|
|
|
|
|
|
welcomeWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)
|
2023-07-17 03:25:11 +00:00
|
|
|
welcomeWindow.on('ready-to-show', () => welcomeWindow.show())
|
2023-08-02 15:05:29 +00:00
|
|
|
welcomeWindow.on('closed', () => {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.hide()
|
|
|
|
}
|
|
|
|
})
|
2023-07-14 23:34:24 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 18:04:36 +00:00
|
|
|
let tray: Tray | null = null
|
2023-07-27 20:25:36 +00:00
|
|
|
let updateAvailable = false
|
|
|
|
const assetPath = app.isPackaged ? process.resourcesPath : path.join(__dirname, '..', '..', 'assets')
|
2023-07-06 16:45:58 +00:00
|
|
|
|
2023-07-27 20:25:36 +00:00
|
|
|
function trayIconPath() {
|
|
|
|
return nativeTheme.shouldUseDarkColors
|
|
|
|
? updateAvailable
|
|
|
|
? path.join(assetPath, 'iconDarkUpdateTemplate.png')
|
|
|
|
: path.join(assetPath, 'iconDarkTemplate.png')
|
|
|
|
: updateAvailable
|
|
|
|
? path.join(assetPath, 'iconUpdateTemplate.png')
|
|
|
|
: path.join(assetPath, 'iconTemplate.png')
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTrayIcon() {
|
|
|
|
if (tray) {
|
|
|
|
tray.setImage(trayIconPath())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTray() {
|
2023-07-26 18:24:56 +00:00
|
|
|
const updateItems: MenuItemConstructorOptions[] = [
|
|
|
|
{ label: 'An update is available', enabled: false },
|
|
|
|
{
|
|
|
|
label: 'Restart to update',
|
|
|
|
click: () => autoUpdater.quitAndInstall(),
|
|
|
|
},
|
|
|
|
{ type: 'separator' },
|
|
|
|
]
|
2023-07-19 15:13:49 +00:00
|
|
|
|
2023-07-26 18:04:36 +00:00
|
|
|
const menu = Menu.buildFromTemplate([
|
2023-07-26 18:24:56 +00:00
|
|
|
...(updateAvailable ? updateItems : []),
|
2023-07-26 18:04:36 +00:00
|
|
|
{ role: 'quit', label: 'Quit Ollama', accelerator: 'Command+Q' },
|
|
|
|
])
|
|
|
|
|
|
|
|
if (!tray) {
|
2023-07-27 20:25:36 +00:00
|
|
|
tray = new Tray(trayIconPath())
|
2023-07-26 18:04:36 +00:00
|
|
|
}
|
2023-06-27 16:35:51 +00:00
|
|
|
|
2023-07-26 18:04:36 +00:00
|
|
|
tray.setToolTip(updateAvailable ? 'An update is available' : 'Ollama')
|
|
|
|
tray.setContextMenu(menu)
|
2023-07-27 20:25:36 +00:00
|
|
|
tray.setImage(trayIconPath())
|
|
|
|
|
|
|
|
nativeTheme.off('updated', updateTrayIcon)
|
|
|
|
nativeTheme.on('updated', updateTrayIcon)
|
2023-07-05 20:10:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 04:02:13 +00:00
|
|
|
let proc: ChildProcess = null
|
2023-06-25 04:30:02 +00:00
|
|
|
|
2023-07-06 18:32:48 +00:00
|
|
|
function server() {
|
|
|
|
const binary = app.isPackaged
|
2023-07-21 21:10:05 +00:00
|
|
|
? path.join(process.resourcesPath, 'ollama')
|
|
|
|
: path.resolve(process.cwd(), '..', 'ollama')
|
2023-07-06 18:32:48 +00:00
|
|
|
|
2023-07-27 04:02:13 +00:00
|
|
|
proc = spawn(binary, ['serve'])
|
2023-07-11 23:16:38 +00:00
|
|
|
|
2023-07-06 18:32:48 +00:00
|
|
|
proc.stdout.on('data', data => {
|
2023-07-21 16:49:40 +00:00
|
|
|
logger.info(data.toString().trim())
|
|
|
|
})
|
2023-07-11 23:16:38 +00:00
|
|
|
|
2023-07-06 18:32:48 +00:00
|
|
|
proc.stderr.on('data', data => {
|
2023-07-21 16:49:40 +00:00
|
|
|
logger.error(data.toString().trim())
|
|
|
|
})
|
2023-07-21 16:47:54 +00:00
|
|
|
|
2023-07-21 21:29:07 +00:00
|
|
|
proc.on('exit', restart)
|
2023-07-06 18:32:48 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 04:02:13 +00:00
|
|
|
function restart() {
|
|
|
|
setTimeout(server, 1000)
|
2023-07-06 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 04:02:13 +00:00
|
|
|
app.on('before-quit', () => {
|
|
|
|
if (proc) {
|
|
|
|
proc.off('exit', restart)
|
|
|
|
proc.kill()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function init() {
|
2023-07-26 18:04:36 +00:00
|
|
|
if (app.isPackaged) {
|
|
|
|
heartbeat()
|
|
|
|
autoUpdater.checkForUpdates()
|
|
|
|
setInterval(() => {
|
|
|
|
heartbeat()
|
|
|
|
autoUpdater.checkForUpdates()
|
|
|
|
}, 60 * 60 * 1000)
|
|
|
|
}
|
|
|
|
|
2023-07-27 20:25:36 +00:00
|
|
|
updateTray()
|
2023-07-26 18:24:56 +00:00
|
|
|
|
2023-07-06 22:05:31 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2023-07-10 20:46:31 +00:00
|
|
|
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) {
|
2023-07-11 17:33:32 +00:00
|
|
|
logger.error(`[Move to Applications] Failed to move to applications folder - ${e.message}}`)
|
2023-07-10 20:46:31 +00:00
|
|
|
}
|
2023-07-06 20:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-06 22:05:31 +00:00
|
|
|
}
|
2023-07-06 22:02:37 +00:00
|
|
|
|
2023-07-10 20:46:31 +00:00
|
|
|
server()
|
2023-07-14 23:50:12 +00:00
|
|
|
|
2023-07-17 03:25:11 +00:00
|
|
|
if (store.get('first-time-run') && installed()) {
|
2023-07-27 14:53:38 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.hide()
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:34:24 +00:00
|
|
|
app.setLoginItemSettings({ openAtLogin: app.getLoginItemSettings().openAtLogin })
|
2023-07-17 03:25:11 +00:00
|
|
|
return
|
2023-07-14 23:34:24 +00:00
|
|
|
}
|
2023-07-17 03:25:11 +00:00
|
|
|
|
|
|
|
// This is the first run or the CLI is no longer installed
|
|
|
|
app.setLoginItemSettings({ openAtLogin: true })
|
|
|
|
firstRunWindow()
|
2023-07-27 04:02:13 +00:00
|
|
|
}
|
2023-07-06 18:32:48 +00:00
|
|
|
|
2023-06-22 16:45:31 +00:00
|
|
|
// 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.
|
2023-07-06 04:04:06 +00:00
|
|
|
autoUpdater.setFeedURL({
|
|
|
|
url: `https://ollama.ai/api/update?os=${process.platform}&arch=${process.arch}&version=${app.getVersion()}`,
|
|
|
|
})
|
2023-06-27 21:50:50 +00:00
|
|
|
|
2023-07-06 21:32:48 +00:00
|
|
|
async function heartbeat() {
|
|
|
|
analytics.track({
|
|
|
|
anonymousId: id(),
|
|
|
|
event: 'heartbeat',
|
2023-07-07 17:44:36 +00:00
|
|
|
properties: {
|
|
|
|
version: app.getVersion(),
|
|
|
|
},
|
2023-07-06 21:32:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:02:13 +00:00
|
|
|
autoUpdater.on('error', e => {
|
2023-07-25 19:41:56 +00:00
|
|
|
console.error(`update check failed - ${e.message}`)
|
2023-07-06 16:02:13 +00:00
|
|
|
})
|
|
|
|
|
2023-07-26 18:04:36 +00:00
|
|
|
autoUpdater.on('update-downloaded', () => {
|
2023-07-27 20:25:36 +00:00
|
|
|
updateAvailable = true
|
|
|
|
updateTray()
|
2023-06-28 00:31:02 +00:00
|
|
|
})
|