Skip to content

Commit

Permalink
implement mechanism to detect ui-freezes; "soft-restart" application …
Browse files Browse the repository at this point in the history
…if detect them
rainu committed Jan 22, 2025
1 parent 2581cd1 commit ceb07a7
Showing 17 changed files with 244 additions and 31 deletions.
14 changes: 8 additions & 6 deletions config/debug.go
Original file line number Diff line number Diff line change
@@ -6,15 +6,17 @@ import (
)

type DebugConfig struct {
LogLevel int `yaml:"log-level"`
PprofAddress string `yaml:"pprof-address" usage:"Address for the pprof server (only available for debug binary)"`
VueDevTools VueDevToolsConfig `yaml:"vue-dev-tools"`
WebKit WebKitInspectorConfig `yaml:"webkit" usage:"Webkit debug configuration (only available for debug binary): "`
PrintVersion bool `config:"version" yaml:"-" short:"v" usage:"Show the version"`
LogLevel int `yaml:"log-level"`
PprofAddress string `yaml:"pprof-address" usage:"Address for the pprof server (only available for debug binary)"`
VueDevTools VueDevToolsConfig `yaml:"vue-dev-tools"`
WebKit WebKitInspectorConfig `yaml:"webkit" usage:"Webkit debug configuration (only available for debug binary): "`
EnableCrashDetection bool `yaml:"enable" usage:"Enable crash detection. If a crash is detected the application will try to recover the last state"`
RestartShortcut Shortcut `yaml:"shortcut" usage:"The shortcut for triggering a restart"`
PrintVersion bool `config:"version" yaml:"-" short:"v" usage:"Show the version"`
}

type VueDevToolsConfig struct {
Host string `yaml:"host" usage:"The host of the vue dev tools server. If empty the dev tools will be disabled."`
Host string `yaml:"host" usage:"The host of the vue dev tools server. If empty the dev tools will be disabled"`
Port int `yaml:"port" usage:"The port of the vue dev tools server"`
}

5 changes: 5 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,11 @@ func defaultConfig() *Config {
OpenInspectorOnStartup: false,
HttpServerAddress: "",
},
EnableCrashDetection: true,
RestartShortcut: Shortcut{
Alt: true,
Code: "keyr",
},
PrintVersion: false,
},
LLM: llm.LLMConfig{
2 changes: 1 addition & 1 deletion config/parser.go
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ func Parse(arguments []string, env []string) *Config {
} else if target == PrinterTargetErr {
c.Printer.Targets = append(c.Printer.Targets, os.Stderr)
} else {
file, err := os.Create(target)
file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
panic(fmt.Errorf("Error creating printer target file: %w", err))
}
1 change: 1 addition & 0 deletions controller/base.go
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ type Controller struct {

vueAppMounted bool
streamBuffer []byte
lastState string
}

type llmAskResult struct {
3 changes: 2 additions & 1 deletion controller/build.go
Original file line number Diff line number Diff line change
@@ -16,9 +16,10 @@ import (
"net/http"
)

func BuildFromConfig(cfg *config.Config) (ctrl *Controller, err error) {
func BuildFromConfig(cfg *config.Config, lastState string) (ctrl *Controller, err error) {
ctrl = &Controller{
appConfig: cfg,
lastState: lastState,
}

printer := io.MultiResponsePrinter{}
17 changes: 17 additions & 0 deletions controller/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package controller

import "github.com/wailsapp/wails/v2/pkg/runtime"

func (c *Controller) TriggerRestart() {
runtime.EventsEmit(c.ctx, "system:restart")
}

func (c *Controller) Restart(state string) {
c.lastState = state
runtime.Hide(c.ctx)
runtime.Quit(c.ctx)
}

func (c *Controller) GetLastState() string {
return c.lastState
}
4 changes: 4 additions & 0 deletions controller/vue.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ func (c *Controller) AppMounted() {
c.vueAppMounted = true
}

func (c *Controller) IsAppMounted() bool {
return c.vueAppMounted
}

func (c *Controller) applyInitialWindowConfig() {
_, height := runtime.WindowGetSize(c.ctx)

21 changes: 15 additions & 6 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@

<script lang="ts">
import { defineComponent } from 'vue'
import { Shutdown } from '../wailsjs/go/controller/Controller'
import { TriggerRestart, Shutdown } from '../wailsjs/go/controller/Controller'
export default defineComponent({
data() {
@@ -36,15 +36,24 @@ export default defineComponent({
},
methods: {
handleGlobalKeydown(event: KeyboardEvent) {
const code = event.code.toLowerCase() === this.$appConfig.UI.QuitShortcut.Code.toLowerCase()
const ctrl = event.ctrlKey === this.$appConfig.UI.QuitShortcut.Ctrl
const shift = event.shiftKey === this.$appConfig.UI.QuitShortcut.Shift
const alt = event.altKey === this.$appConfig.UI.QuitShortcut.Alt
const meta = event.metaKey === this.$appConfig.UI.QuitShortcut.Meta
let code = event.code.toLowerCase() === this.$appConfig.UI.QuitShortcut.Code.toLowerCase()
let ctrl = event.ctrlKey === this.$appConfig.UI.QuitShortcut.Ctrl
let shift = event.shiftKey === this.$appConfig.UI.QuitShortcut.Shift
let alt = event.altKey === this.$appConfig.UI.QuitShortcut.Alt
let meta = event.metaKey === this.$appConfig.UI.QuitShortcut.Meta
if (code && ctrl && shift && alt && meta) {
Shutdown()
}
code = event.code.toLowerCase() === this.$appConfig.Debug.RestartShortcut.Code.toLowerCase()
ctrl = event.ctrlKey === this.$appConfig.Debug.RestartShortcut.Ctrl
shift = event.shiftKey === this.$appConfig.Debug.RestartShortcut.Shift
alt = event.altKey === this.$appConfig.Debug.RestartShortcut.Alt
meta = event.metaKey === this.$appConfig.Debug.RestartShortcut.Meta
if (code && ctrl && shift && alt && meta) {
TriggerRestart()
}
},
opacityValue(isHovering: boolean | null): number {
switch (this.$appConfig.UI.Window.Translucent) {
52 changes: 45 additions & 7 deletions frontend/src/views/Home.vue
Original file line number Diff line number Diff line change
@@ -52,7 +52,14 @@
</template>

<script lang="ts">
import { AppMounted, LLMAsk, LLMInterrupt, LLMWait } from '../../wailsjs/go/controller/Controller'
import {
AppMounted,
GetLastState,
LLMAsk,
LLMInterrupt,
LLMWait,
Restart,
} from '../../wailsjs/go/controller/Controller'
import { EventsOn, WindowGetSize, WindowSetPosition, WindowSetSize } from '../../wailsjs/runtime'
import ChatMessage, { ContentType, Role } from '../components/ChatMessage.vue'
import ChatInput, { ChatInputType } from '../components/ChatInput.vue'
@@ -68,6 +75,11 @@ type HistoryEntry = {
Message: controller.LLMMessage
}
type State = {
input: ChatInputType
chatHistory: HistoryEntry[]
}
export default {
name: 'Home',
components: { UserScrollDetector, ZoomDetector, ChatInput, ChatMessage },
@@ -222,14 +234,40 @@ export default {
EventsOn('llm:stream:chunk', (chunk: string) => {
this.outputStream[0].Content += chunk
})
EventsOn('system:restart', () => {
// backend requested a restart
// so we have to save the current state and restart the app
// but we have to wait until the progress is done (if any)
const restartAfterProgress = () => {
if(this.progress) {
setTimeout(restartAfterProgress, 50)
} else {
const state = {
input: this.input,
chatHistory: this.chatHistory,
} as State
AppMounted()
.then(() => {
if (this.$appConfig.UI.Prompt.InitValue) {
this.waitForLLM()
Restart(JSON.stringify(state))
}
})
.then(() => this.adjustHeight())
}
restartAfterProgress()
})
GetLastState().then((stateAsString) => {
if (stateAsString) {
const state = JSON.parse(stateAsString) as State
this.input = state.input
this.chatHistory = state.chatHistory
}
AppMounted()
.then(() => {
if (this.$appConfig.UI.Prompt.InitValue && !stateAsString) {
this.waitForLLM()
}
})
.then(() => this.adjustHeight())
})
},
updated() {
this.$nextTick(() => {
8 changes: 8 additions & 0 deletions frontend/wailsjs/go/controller/Controller.d.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ export function GetApplicationConfig():Promise<config.Config>;

export function GetAssetMeta(arg1:string):Promise<controller.AssetMeta>;

export function GetLastState():Promise<string>;

export function IsAppMounted():Promise<boolean>;

export function LLMAsk(arg1:controller.LLMAskArgs):Promise<string>;

export function LLMInterrupt():Promise<void>;
@@ -19,4 +23,8 @@ export function Log(arg1:string,arg2:string):Promise<void>;

export function OpenFileDialog(arg1:controller.OpenFileDialogArgs):Promise<Array<string>>;

export function Restart(arg1:string):Promise<void>;

export function Shutdown():Promise<void>;

export function TriggerRestart():Promise<void>;
16 changes: 16 additions & 0 deletions frontend/wailsjs/go/controller/Controller.js
Original file line number Diff line number Diff line change
@@ -14,6 +14,14 @@ export function GetAssetMeta(arg1) {
return window['go']['controller']['Controller']['GetAssetMeta'](arg1);
}

export function GetLastState() {
return window['go']['controller']['Controller']['GetLastState']();
}

export function IsAppMounted() {
return window['go']['controller']['Controller']['IsAppMounted']();
}

export function LLMAsk(arg1) {
return window['go']['controller']['Controller']['LLMAsk'](arg1);
}
@@ -34,6 +42,14 @@ export function OpenFileDialog(arg1) {
return window['go']['controller']['Controller']['OpenFileDialog'](arg1);
}

export function Restart(arg1) {
return window['go']['controller']['Controller']['Restart'](arg1);
}

export function Shutdown() {
return window['go']['controller']['Controller']['Shutdown']();
}

export function TriggerRestart() {
return window['go']['controller']['Controller']['TriggerRestart']();
}
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ export namespace config {
PprofAddress: string;
VueDevTools: VueDevToolsConfig;
WebKit: WebKitInspectorConfig;
EnableCrashDetection: boolean;
RestartShortcut: Shortcut;
PrintVersion: boolean;

static createFrom(source: any = {}) {
@@ -45,6 +47,8 @@ export namespace config {
this.PprofAddress = source["PprofAddress"];
this.VueDevTools = this.convertValues(source["VueDevTools"], VueDevToolsConfig);
this.WebKit = this.convertValues(source["WebKit"], WebKitInspectorConfig);
this.EnableCrashDetection = source["EnableCrashDetection"];
this.RestartShortcut = this.convertValues(source["RestartShortcut"], Shortcut);
this.PrintVersion = source["PrintVersion"];
}

13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -6,8 +6,9 @@ require (
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd
github.com/gabriel-vasile/mimetype v1.4.7
github.com/rainu/go-command-chain v0.4.0
github.com/shirou/gopsutil/v4 v4.24.12
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/tmc/langchaingo v0.1.12
github.com/wailsapp/wails/v2 v2.9.2
gopkg.in/yaml.v3 v3.0.1
@@ -17,6 +18,7 @@ require (
github.com/bep/debounce v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/ebitengine/purego v0.8.1 // indirect
github.com/gage-technologies/mistral-go v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
@@ -30,23 +32,28 @@ require (
github.com/leaanthony/gosod v1.0.3 // indirect
github.com/leaanthony/slicer v1.6.0 // indirect
github.com/leaanthony/u v1.1.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkoukk/tiktoken-go v0.1.6 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tkrajina/go-reflector v0.5.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/wailsapp/go-webview2 v1.0.16 // indirect
github.com/wailsapp/go-webview2 v1.0.18 // indirect
github.com/wailsapp/mimetype v1.4.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
Loading

0 comments on commit ceb07a7

Please sign in to comment.