ollama/app/src/index.ts

219 lines
6 KiB
TypeScript
Raw Normal View History

import { spawn, exec } from 'child_process'
import { app, autoUpdater, dialog, Tray, Menu } 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-07-06 16:23:08 +00:00
import * as fs from 'fs'
2023-06-27 16:35:51 +00:00
2023-07-06 21:32:48 +00:00
import { analytics, id } from './telemetry'
2023-06-27 16:35:51 +00:00
require('@electron/remote/main').initialize()
2023-07-08 17:18:34 +00:00
const store = new Store()
2023-07-05 20:10:30 +00:00
let tray: Tray | null = null
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,
}),
],
format: winston.format.printf(info => `${info.message}`),
2023-07-11 17:33:32 +00:00
})
2023-07-06 20:59:36 +00:00
const SingleInstanceLock = app.requestSingleInstanceLock()
2023-07-06 22:05:31 +00:00
if (!SingleInstanceLock) {
app.quit()
}
2023-07-06 18:41:25 +00:00
2023-07-05 20:10:30 +00:00
const createSystemtray = () => {
let iconPath = path.join(__dirname, '..', '..', 'assets', 'ollama_icon_16x16Template.png')
2023-07-06 16:45:58 +00:00
if (app.isPackaged) {
iconPath = path.join(process.resourcesPath, 'ollama_icon_16x16Template.png')
2023-07-06 16:45:58 +00:00
}
2023-06-22 16:45:31 +00:00
tray = new Tray(iconPath)
2023-06-22 16:45:31 +00:00
2023-07-08 17:18:34 +00:00
const contextMenu = Menu.buildFromTemplate([{ role: 'quit', label: 'Quit Ollama', accelerator: 'Command+Q' }])
2023-06-27 16:35:51 +00:00
2023-07-05 20:10:30 +00:00
tray.setContextMenu(contextMenu)
tray.setToolTip('Ollama')
}
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit()
2023-06-25 04:30:02 +00:00
}
2023-07-06 16:17:42 +00:00
const ollama = path.join(process.resourcesPath, 'ollama')
2023-07-06 18:32:48 +00:00
function server() {
const binary = app.isPackaged
2023-07-11 22:51:59 +00:00
? path.join(process.resourcesPath, 'ollama')
: path.resolve(process.cwd(), '..', 'ollama')
2023-07-06 18:32:48 +00:00
2023-07-11 22:51:59 +00:00
const proc = spawn(binary, ['serve'], { cwd: path.dirname(binary) })
2023-07-06 18:32:48 +00:00
proc.stdout.on('data', data => {
2023-07-11 18:38:38 +00:00
logger.info(`server: ${data.toString()}`)
2023-07-06 18:32:48 +00:00
})
proc.stderr.on('data', data => {
2023-07-11 22:51:59 +00:00
logger.info(`server: ${data.toString()}`)
2023-07-06 18:32:48 +00:00
})
proc.on('exit', () => {
2023-07-11 22:51:59 +00:00
logger.info('Restarting the server...')
server()
})
proc.on('disconnect', () => {
2023-07-11 22:51:59 +00:00
logger.info('Server disconnected. Reconnecting...')
server()
})
2023-07-06 18:32:48 +00:00
process.on('exit', () => {
proc.kill()
})
}
function installCLI() {
2023-07-06 16:23:08 +00:00
const symlinkPath = '/usr/local/bin/ollama'
if (fs.existsSync(symlinkPath) && fs.readlinkSync(symlinkPath) === ollama) {
return
}
dialog
.showMessageBox({
type: 'info',
title: 'Ollama CLI installation',
2023-07-07 17:44:36 +00:00
message: 'To make the Ollama command work in your terminal, it needs administrator privileges.',
buttons: ['OK'],
})
.then(result => {
if (result.response === 0) {
const command = `
2023-07-06 16:23:08 +00:00
do shell script "ln -F -s ${ollama} /usr/local/bin/ollama" with administrator privileges
`
exec(`osascript -e '${command}'`, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
2023-07-11 18:38:38 +00:00
logger.error(`CLI: Failed to install CLI - ${error.message}`)
return
}
2023-07-11 17:33:32 +00:00
2023-07-11 18:38:38 +00:00
logger.info(`CLI: ${stdout}}`)
logger.error(`CLI: ${stderr}`)
})
}
})
}
2023-07-06 22:05:31 +00:00
app.on('ready', () => {
if (process.platform === 'darwin') {
app.dock.hide()
2023-07-07 17:43:26 +00:00
if (!store.has('first-time-run')) {
// This is the first run
app.setLoginItemSettings({ openAtLogin: true })
2023-07-08 17:18:34 +00:00
store.set('first-time-run', false)
2023-07-07 17:43:26 +00:00
} else {
// The app has been run before
app.setLoginItemSettings({ openAtLogin: app.getLoginItemSettings().openAtLogin })
}
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}}`)
}
}
}
installCLI()
}
2023-07-06 22:05:31 +00:00
}
2023-07-06 22:02:37 +00:00
2023-07-06 22:05:31 +00:00
createSystemtray()
server()
2023-07-06 22:05:31 +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 18:50:46 +00:00
if (app.isPackaged) {
2023-07-06 21:32:48 +00:00
heartbeat()
2023-06-27 21:50:50 +00:00
autoUpdater.checkForUpdates()
2023-07-06 18:50:46 +00:00
setInterval(() => {
2023-07-06 21:32:48 +00:00
heartbeat()
2023-07-06 18:50:46 +00:00
autoUpdater.checkForUpdates()
2023-07-08 17:18:34 +00:00
}, 60 * 60 * 1000)
2023-07-06 18:50:46 +00:00
}
2023-06-28 00:31:02 +00:00
autoUpdater.on('error', e => {
2023-07-11 18:38:38 +00:00
logger.error(`auto updater: update check failed - ${e.message}`)
})
2023-06-28 00:31:02 +00:00
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
dialog
.showMessageBox({
type: 'info',
buttons: ['Restart Now', 'Later'],
title: 'New update available',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version of Ollama is available. Restart to apply the update.',
})
.then(returnValue => {
if (returnValue.response === 0) autoUpdater.quitAndInstall()
})
})