Skip to content

Commit

Permalink
feat: keep system awake
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Jan 14, 2025
1 parent bcfdb82 commit 4ab9ab7
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 127 deletions.
602 changes: 538 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

Binary file modified desktop/bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"lint": "eslint ."
},
"dependencies": {
"@fontsource/roboto": "^5.1.0",
"@formatjs/intl-durationformat": "^0.6.3",
"@tauri-apps/api": "~2.1.1",
"@tauri-apps/plugin-clipboard-manager": "~2.0.0",
"@tauri-apps/plugin-deep-link": "~2.0.0",
Expand All @@ -23,8 +25,6 @@
"@tauri-apps/plugin-store": "~2.1.0",
"@tauri-apps/plugin-updater": "~2.0.0",
"@tauri-apps/plugin-window-state": "~2.0.0",
"@fontsource/roboto": "^5.1.0",
"@formatjs/intl-durationformat": "^0.6.3",
"docx": "^9.0.3",
"format-duration": "^3.0.2",
"i18next": "^23.16.5",
Expand All @@ -35,6 +35,7 @@
"react-hot-toast": "^2.4.1",
"react-i18next": "^15.1.1",
"react-router-dom": "^6.28.0",
"tauri-plugin-keepawake-api": "^0.1.0",
"usehooks-ts": "^3.1.0",
"vite-plugin-svgr": "^4.3.0"
},
Expand Down
1 change: 1 addition & 0 deletions desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tauri-plugin-deep-link = "2"
tauri-plugin-store = "2"
tauri-plugin-single-instance = "2"
tauri-plugin-clipboard-manager = "2"
tauri-plugin-keepawake = "0.1.0"
# Unsafe headers required for Ollama (to set Origin)
tauri-plugin-http = { version = "2", features = ["unsafe-headers"] }

Expand Down
116 changes: 59 additions & 57 deletions desktop/src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,68 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "migrated",
"description": "permissions that were migrated from v1",
"identifier": "default",
"description": "default permissions",
"local": true,
"windows": [
"main"
],
"permissions": [
"keepawake:allow-start",
"keepawake:allow-stop",
"http:default",
"http:allow-fetch",
"store:allow-set",
"store:allow-get",
"store:allow-has",
"store:allow-reset",
"store:allow-clear",
"store:allow-save",
"store:allow-load",
"store:allow-get-store",
"core:path:default",
"core:event:default",
"core:resources:default",
"core:menu:default",
"core:tray:default",
"fs:allow-remove",
"fs:allow-read-dir",
"fs:allow-read-text-file",
"fs:allow-app-write",
"os:allow-arch",
"os:allow-platform",
"os:allow-version",
"os:allow-os-type",
"os:allow-locale",
"core:window:default",
"core:window:allow-unminimize",
"core:window:allow-set-focus",
"core:window:allow-close",
"core:window:allow-destroy",
"core:window:allow-show",
"core:window:allow-is-visible",
"window-state:allow-restore-state",
"window-state:allow-save-window-state",
"core:webview:default",
"core:webview:allow-print",
"dialog:default",
"dialog:allow-open",
"dialog:allow-save",
"dialog:allow-ask",
"dialog:allow-message",
"process:allow-restart",
"core:app:default",
"core:app:allow-version",
"core:app:allow-name",
"updater:default",
"shell:allow-open",
"shell:allow-execute",
"shell:allow-kill",
"shell:allow-spawn",
"fs:default",
"fs:allow-exists",
"core:path:allow-basename",
"deep-link:allow-get-current",
"clipboard-manager:allow-write-text",
{
"identifier": "fs:scope",
"allow": [
Expand Down Expand Up @@ -65,60 +121,6 @@
"url": "http{s}?://localhost:*/*"
}
]
},
"http:default",
"http:allow-fetch",
"store:allow-set",
"store:allow-get",
"store:allow-has",
"store:allow-reset",
"store:allow-clear",
"store:allow-save",
"store:allow-load",
"store:allow-get-store",
"core:path:default",
"core:event:default",
"core:resources:default",
"core:menu:default",
"core:tray:default",
"fs:allow-remove",
"fs:allow-read-dir",
"fs:allow-read-text-file",
"fs:allow-app-write",
"os:allow-arch",
"os:allow-platform",
"os:allow-version",
"os:allow-os-type",
"os:allow-locale",
"core:window:default",
"core:window:allow-unminimize",
"core:window:allow-set-focus",
"core:window:allow-close",
"core:window:allow-destroy",
"core:window:allow-show",
"core:window:allow-is-visible",
"window-state:allow-restore-state",
"window-state:allow-save-window-state",
"core:webview:default",
"core:webview:allow-print",
"dialog:default",
"dialog:allow-open",
"dialog:allow-save",
"dialog:allow-ask",
"dialog:allow-message",
"process:allow-restart",
"core:app:default",
"core:app:allow-version",
"core:app:allow-name",
"updater:default",
"shell:allow-open",
"shell:allow-execute",
"shell:allow-kill",
"shell:allow-spawn",
"fs:default",
"fs:allow-exists",
"core:path:allow-basename",
"deep-link:allow-get-current",
"clipboard-manager:allow-write-text"
}
]
}
1 change: 1 addition & 0 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn main() -> Result<()> {
.plugin(tauri_plugin_updater::Builder::default().build())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_keepawake::init())
.invoke_handler(tauri::generate_handler![
cmd::download_file,
cmd::get_cargo_features,
Expand Down
22 changes: 21 additions & 1 deletion desktop/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from '@tauri-apps/plugin-fs'
import * as config from './config'
import { Dispatch, SetStateAction } from 'react'
import { load } from '@tauri-apps/plugin-store'
import * as keepAwake from 'tauri-plugin-keepawake-api'

export interface NamedPath {
name: string
Expand Down Expand Up @@ -79,8 +80,27 @@ export async function openPath(path: NamedPath) {
await invoke('open_path', { path: path.path })
}

export async function getModelsFolder() {}
export async function getModelsFolder() { }

export function formatSpeaker(speaker?: string, prefix = 'Speaker') {
return `${prefix} ${speaker ?? '?'}: `
}

export async function startKeepAwake() {
try {
console.log('start keepawake')
keepAwake.start({ display: true, idle: true, sleep: true })
} catch (e) {
console.error(`Keep awake failed: ${e}`)
}
}

export async function stopKeepAwake() {
console.log('stop keepawake')
try {
keepAwake.stop()
} catch (e) {
console.error(`Keep awake failed: ${e}`)
}
}

7 changes: 6 additions & 1 deletion desktop/src/pages/batch/viewModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { TextFormat, formatExtensions } from '~/components/FormatSelect'
import { Segment, Transcript, asJson, asSrt, asText, asVtt } from '~/lib/transcript'
import { NamedPath, pathToNamedPath } from '~/lib/utils'
import { NamedPath, pathToNamedPath, startKeepAwake, stopKeepAwake } from '~/lib/utils'
import * as webview from '@tauri-apps/api/webviewWindow'
import * as dialog from '@tauri-apps/plugin-dialog'
import * as config from '~/lib/config'
Expand Down Expand Up @@ -112,6 +112,9 @@ export function viewModel() {
if (inProgress) {
return
}

startKeepAwake()

setInProgress(true)
let localIndex = 0
await invoke('load_model', { modelPath: preference.modelPath, gpuDevice: preference.gpuDevice })
Expand Down Expand Up @@ -176,6 +179,7 @@ export function viewModel() {
await new Promise((resolve) => setTimeout(resolve, 100))
setCurrentIndex(localIndex)
} catch (error) {
stopKeepAwake()
if (isAbortingRef.current) {
navigate('/')
} else {
Expand All @@ -185,6 +189,7 @@ export function viewModel() {
setCurrentIndex(localIndex)
}
}
stopKeepAwake()
setCurrentIndex(files.length + 1)
setInProgress(false)
setIsAborting(false)
Expand Down
8 changes: 7 additions & 1 deletion desktop/src/pages/home/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ReactComponent as MicrphoneIcon } from '~/icons/microphone.svg'
import { ReactComponent as LinkIcon } from '~/icons/link.svg'
import { useEffect } from 'react'
import { webviewWindow } from '@tauri-apps/api'
import * as keepAwake from 'tauri-plugin-keepawake-api'

export default function Home() {
const { t } = useTranslation()
Expand Down Expand Up @@ -76,7 +77,12 @@ export default function Home() {

{vm.isRecording && (
<>
<button onMouseDown={vm.stopRecord} className="btn relative btn-success mt-3">
<button onMouseDown={() => {
// Stop keepawake
console.log('stop keepawake')
keepAwake.stop()
vm.stopRecord()
}} className="btn relative btn-success mt-3">
<span className="loading loading-spinner"></span>
{t('common.stop-and-transcribe')}
</button>
Expand Down
10 changes: 9 additions & 1 deletion desktop/src/pages/home/viewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TextFormat } from '~/components/FormatSelect'
import { AudioDevice } from '~/lib/audio'
import * as config from '~/lib/config'
import * as transcript from '~/lib/transcript'
import { NamedPath, ls, openPath, pathToNamedPath } from '~/lib/utils'
import { NamedPath, ls, openPath, pathToNamedPath, startKeepAwake, stopKeepAwake } from '~/lib/utils'
import { getX86Features } from '~/lib/x86Features'
import { ErrorModalContext } from '~/providers/ErrorModal'
import { useFilesContext } from '~/providers/FilesProvider'
Expand Down Expand Up @@ -278,6 +278,7 @@ export function viewModel() {
}, [])

async function startRecord() {
startKeepAwake()
setSegments(null)
setSummarizeSegments(null)
setTranscriptTab('transcript')
Expand All @@ -298,6 +299,10 @@ export function viewModel() {
}

async function transcribe(path: string) {
// Keep system awake
console.log('keep awake start')
startKeepAwake()

setSegments(null)
setSummarizeSegments(null)
setTranscriptTab('transcript')
Expand Down Expand Up @@ -330,11 +335,14 @@ export function viewModel() {
hotToast.success(t('common.transcribe-took', { total: String(total) }), { position: 'bottom-center' })
} catch (error) {
if (!abortRef.current) {
stopKeepAwake()
console.error('error: ', error)
setErrorModal?.({ log: String(error), open: true })
setLoading(false)
}
} finally {
// Stop keepawake
stopKeepAwake()
setLoading(false)
setIsAborting(false)
setProgress(null)
Expand Down

0 comments on commit 4ab9ab7

Please sign in to comment.