ollama/app/src/index.ts

200 lines
5.6 KiB
TypeScript
Raw Normal View History

import { spawn, exec } from 'child_process'
2023-07-06 16:59:36 -04:00
import { app, autoUpdater, dialog, Tray, Menu, nativeTheme, Notification } from 'electron'
2023-06-23 18:38:22 -04:00
import * as path from 'path'
2023-07-06 12:23:08 -04:00
import * as fs from 'fs'
2023-06-27 12:35:51 -04:00
require('@electron/remote/main').initialize()
2023-07-05 16:10:30 -04:00
let tray: Tray | null = null
2023-07-06 16:59:36 -04:00
let secondInstance: Boolean = false
const SingleInstanceLock = app.requestSingleInstanceLock()
2023-07-06 14:41:25 -04:00
2023-07-05 16:10:30 -04:00
const createSystemtray = () => {
2023-07-06 12:45:58 -04:00
let brightModeIconPath = path.join(__dirname, '..', '..', 'assets', 'ollama_icon_dark_16x16.png')
let darkModeIconPath = path.join(__dirname, '..', '..', 'assets', 'ollama_icon_bright_16x16.png')
if (app.isPackaged) {
brightModeIconPath = path.join(process.resourcesPath, 'ollama_icon_dark_16x16@2x.png')
darkModeIconPath = path.join(process.resourcesPath, 'ollama_icon_bright_16x16@2x.png')
}
2023-06-22 12:45:31 -04:00
2023-07-05 16:10:30 -04:00
tray = new Tray(brightModeIconPath)
2023-06-22 12:45:31 -04:00
2023-07-05 16:10:30 -04:00
if (process.platform === 'darwin') {
tray.setImage(nativeTheme.shouldUseDarkColors ? darkModeIconPath : brightModeIconPath)
nativeTheme.on('updated', () => {
tray.setImage(nativeTheme.shouldUseDarkColors ? darkModeIconPath : brightModeIconPath)
})
}
2023-06-22 12:45:31 -04:00
const contextMenu = Menu.buildFromTemplate([
{ role: 'quit', label: 'Quit Ollama', accelerator: 'Command+Q' },
])
2023-06-27 12:35:51 -04:00
2023-07-05 16:10:30 -04: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 00:30:02 -04:00
}
2023-07-06 12:17:42 -04:00
const ollama = path.join(process.resourcesPath, 'ollama')
2023-06-25 00:30:02 -04:00
// if the app is packaged then run the server
if (app.isPackaged) {
2023-06-23 18:38:22 -04:00
// Start the executable
console.log(`Starting server`)
const proc = spawn(ollama, ['serve'])
2023-06-25 00:30:02 -04:00
proc.stdout.on('data', data => {
2023-06-23 18:38:22 -04:00
console.log(`server: ${data}`)
})
2023-06-25 00:30:02 -04:00
proc.stderr.on('data', data => {
2023-06-23 18:38:22 -04:00
console.error(`server: ${data}`)
})
2023-06-25 00:30:02 -04:00
process.on('exit', () => {
proc.kill()
})
2023-06-22 12:45:31 -04:00
}
2023-07-06 14:32:48 -04:00
function server() {
const binary = app.isPackaged
? path.join(process.resourcesPath, 'ollama')
: path.resolve(__dirname, '..', '..', 'ollama')
console.log(`Starting server`)
const proc = spawn(binary, ['serve'])
proc.stdout.on('data', data => {
console.log(`server: ${data}`)
})
proc.stderr.on('data', data => {
console.error(`server: ${data}`)
})
process.on('exit', () => {
proc.kill()
})
}
function installCLI() {
2023-07-06 12:23:08 -04: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-06 12:23:08 -04:00
message: 'To install the Ollama CLI, we need to ask you for administrator privileges.',
buttons: ['OK'],
})
.then(result => {
if (result.response === 0) {
const command = `
2023-07-06 12:23:08 -04: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) {
console.error(`exec error: ${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.error(`stderr: ${stderr}`)
})
}
})
}
2023-07-06 16:59:36 -04:00
if (!SingleInstanceLock) {
app.quit()
} else {
app.on('ready', () => {
if (process.platform === 'darwin') {
app.dock.hide()
if (!app.isInApplicationsFolder()) {
const chosen = dialog.showMessageBoxSync({
type: 'question',
buttons: ['Move to Applications', 'Do Not Move'],
message: 'Move Ollama to 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
}
2023-07-06 16:59:36 -04:00
})
return
} catch (e) {
console.error('Failed to move to applications folder')
console.error(e)
}
}
}
}
2023-07-06 16:59:36 -04:00
createSystemtray()
if (app.isPackaged) {
installCLI()
}
})
}
2023-07-06 14:32:48 -04:00
2023-06-22 12:45:31 -04: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 00:04:06 -04:00
autoUpdater.setFeedURL({
url: `https://ollama.ai/api/update?os=${process.platform}&arch=${process.arch}&version=${app.getVersion()}`,
})
2023-06-27 17:50:50 -04:00
2023-07-06 14:50:46 -04:00
if (app.isPackaged) {
2023-06-27 17:50:50 -04:00
autoUpdater.checkForUpdates()
2023-07-06 14:50:46 -04:00
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)
}
2023-06-27 20:31:02 -04:00
autoUpdater.on('error', e => {
console.error('update check failed', e)
})
2023-06-27 20:31:02 -04: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()
})
})