2023-07-06 16:02:13 +00:00
|
|
|
import { spawn, exec } from 'child_process'
|
2023-07-05 20:10:30 +00:00
|
|
|
import { app, autoUpdater, dialog, Tray, Menu, nativeTheme } from 'electron'
|
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
|
|
|
|
|
|
|
require('@electron/remote/main').initialize()
|
|
|
|
|
2023-07-05 20:10:30 +00:00
|
|
|
let tray: Tray | null = null
|
2023-07-06 18:41:25 +00:00
|
|
|
|
2023-07-05 20:10:30 +00:00
|
|
|
const createSystemtray = () => {
|
2023-07-06 16:45:58 +00: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 16:45:31 +00:00
|
|
|
|
2023-07-05 20:10:30 +00:00
|
|
|
tray = new Tray(brightModeIconPath)
|
2023-06-22 16:45:31 +00:00
|
|
|
|
2023-07-05 20:10:30 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
tray.setImage(nativeTheme.shouldUseDarkColors ? darkModeIconPath : brightModeIconPath)
|
|
|
|
nativeTheme.on('updated', () => {
|
|
|
|
tray.setImage(nativeTheme.shouldUseDarkColors ? darkModeIconPath : brightModeIconPath)
|
|
|
|
})
|
|
|
|
}
|
2023-06-22 16:45:31 +00:00
|
|
|
|
2023-07-05 20:10:30 +00:00
|
|
|
const contextMenu = Menu.buildFromTemplate([{ label: 'Quit', type: 'normal', click: () => app.quit() }])
|
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-06-25 04:30:02 +00:00
|
|
|
// if the app is packaged then run the server
|
|
|
|
if (app.isPackaged) {
|
2023-06-23 22:38:22 +00:00
|
|
|
// Start the executable
|
2023-07-06 16:02:13 +00:00
|
|
|
console.log(`Starting server`)
|
|
|
|
const proc = spawn(ollama, ['serve'])
|
2023-06-25 04:30:02 +00:00
|
|
|
proc.stdout.on('data', data => {
|
2023-06-23 22:38:22 +00:00
|
|
|
console.log(`server: ${data}`)
|
|
|
|
})
|
2023-06-25 04:30:02 +00:00
|
|
|
proc.stderr.on('data', data => {
|
2023-06-23 22:38:22 +00:00
|
|
|
console.error(`server: ${data}`)
|
|
|
|
})
|
|
|
|
|
2023-06-25 04:30:02 +00:00
|
|
|
process.on('exit', () => {
|
|
|
|
proc.kill()
|
|
|
|
})
|
2023-06-22 16:45:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 18:32:48 +00: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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:02:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:02:13 +00:00
|
|
|
dialog
|
|
|
|
.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: 'Ollama CLI installation',
|
2023-07-06 16:23:08 +00:00
|
|
|
message: 'To install the Ollama CLI, we need to ask you for administrator privileges.',
|
2023-07-06 16:02:13 +00:00
|
|
|
buttons: ['OK'],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
if (result.response === 0) {
|
|
|
|
let command = `
|
2023-07-06 16:23:08 +00:00
|
|
|
do shell script "ln -F -s ${ollama} /usr/local/bin/ollama" with administrator privileges
|
|
|
|
`
|
2023-07-06 16:02:13 +00:00
|
|
|
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-06-22 16:45:31 +00:00
|
|
|
// This method will be called when Electron has finished
|
|
|
|
// initialization and is ready to create browser windows.
|
|
|
|
// Some APIs can only be used after this event occurs.
|
2023-07-06 16:02:13 +00:00
|
|
|
app.on('ready', () => {
|
2023-07-05 20:10:30 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.hide()
|
|
|
|
}
|
|
|
|
|
|
|
|
createSystemtray()
|
2023-07-06 18:32:48 +00:00
|
|
|
|
|
|
|
if (app.isPackaged) {
|
|
|
|
installCLI()
|
|
|
|
}
|
2023-07-06 16:02:13 +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-06-28 13:14:50 +00:00
|
|
|
autoUpdater.checkForUpdates()
|
2023-06-27 21:50:50 +00:00
|
|
|
setInterval(() => {
|
|
|
|
autoUpdater.checkForUpdates()
|
|
|
|
}, 60000)
|
2023-06-28 00:31:02 +00:00
|
|
|
|
2023-07-06 16:02:13 +00:00
|
|
|
autoUpdater.on('error', e => {
|
|
|
|
console.error('update check failed', e)
|
|
|
|
})
|
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|