Skip to content

Commit

Permalink
prepare prompt anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Oct 28, 2024
1 parent e65d5fe commit 685a776
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 42 deletions.
2 changes: 1 addition & 1 deletion build/build_number.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
321
323
5 changes: 5 additions & 0 deletions src/composables/audio_player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class AudioPlayer {
const tts = new Tts(this.config)
const response = await tts.synthetize(content)

// have we been cancelled
if (this.uuid != uuid) {
return
}

// stream it
this.player = new SpeechPlayer({
audio: audioEl,
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ app.whenReady().then(() => {
// create the document repository
docRepo = new DocumentRepository(app);

// we want prompt anywhere to be as fast as possible
window.preparePromptAnywhere({});

});

// called when the app is already running
Expand Down
42 changes: 26 additions & 16 deletions src/main/windows/anywhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
import { strDict } from 'types/index.d';
import { BrowserWindow } from 'electron';
import { createWindow, getCurrentScreen, getCenteredCoordinates } from './index';
import { wait } from '../utils';

export let promptAnywhereWindow: BrowserWindow = null;

export const openPromptAnywhere = (params: strDict) => {

// try to close existig one
closePromptAnywhere();
export const preparePromptAnywhere = (params: strDict, keepHidden: boolean = true) => {

// get bounds
const width = Math.floor(getCurrentScreen().workAreaSize.width / 2.5);
Expand All @@ -29,21 +25,35 @@ export const openPromptAnywhere = (params: strDict) => {
transparent: true,
hiddenInMissionControl: true,
queryParams: params,
keepHidden: keepHidden,
});

// notify show
if (!keepHidden) {
promptAnywhereWindow.webContents.send('show', params);
}

}


export const openPromptAnywhere = (params: strDict) => {

// do we have one
if (promptAnywhereWindow && !promptAnywhereWindow.isDestroyed()) {
promptAnywhereWindow.webContents.send('query-params', params);
promptAnywhereWindow.webContents.send('show', params);
promptAnywhereWindow.show();
return;
}

// create a new one
preparePromptAnywhere(params, false);

};

export const closePromptAnywhere = async () => {

// now close window itself
try {
if (promptAnywhereWindow && !promptAnywhereWindow.isDestroyed()) {
// console.log('Closing prompt anywhere window')
promptAnywhereWindow?.close()
await wait();
}
} catch (error) {
console.error('Error while closing prompt anywhere window', error);
}
promptAnywhereWindow = null;
// just hide so we reuse it
promptAnywhereWindow.hide();

}
4 changes: 3 additions & 1 deletion src/main/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const createWindow = (opts: CreateWindowOpts = {}) => {

// show when ready
window.once('ready-to-show', () => {
window.show();
if (!opts.keepHidden) {
window.show();
}
});

// web console to here
Expand Down
71 changes: 49 additions & 22 deletions src/screens/PromptAnywhere.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,41 @@ onMounted(() => {
audioPlayer.addListener(onAudioPlayerStatus)
onEvent('audio-noise-detected', () => audioPlayer.stop)
// auto-fill
if (props.extra?.foremostApp) {
const expert = store.experts.find((p) => p.triggerApps?.find((app) => app.identifier == props.extra.foremostApp))
if (expert) {
console.log(`Tiggered on ${props.extra.foremostApp}: filling prompt with expert ${expert.name}`)
onSetExpertPrompt(expert.id)
}
}
// other stuff
isMas.value = window.api.isMasBuild
// init llm with tools
llm = igniteEngine(store.config.llm.engine, store.config.engines[store.config.llm.engine])
for (const pluginName in availablePlugins) {
const pluginClass = availablePlugins[pluginName]
const instance = new pluginClass(store.config.plugins[pluginName])
llm.addPlugin(instance)
// query params
window.api.on('query-params', (params) => {
processQueryParams(params)
})
if (props.extra) {
processQueryParams(props.extra)
}
// init thread
chat.value = new Chat()
chat.value.title = null
chat.value.setEngineModel(store.config.llm.engine, store.config.engines[store.config.llm.engine])
chat.value.addMessage(new Message('system', store.config.instructions.default))
// init on show
window.api.on('show', () => {
// log
console.log('initialize prompt window llm')
// init llm with tools
llm = igniteEngine(store.config.llm.engine, store.config.engines[store.config.llm.engine])
for (const pluginName in availablePlugins) {
const pluginClass = availablePlugins[pluginName]
const instance = new pluginClass(store.config.plugins[pluginName])
llm.addPlugin(instance)
}
// other stuff
isMas.value = window.api.isMasBuild
// init thread
chat.value = new Chat()
chat.value.title = null
chat.value.setEngineModel(store.config.llm.engine, store.config.engines[store.config.llm.engine])
chat.value.addMessage(new Message('system', store.config.instructions.default))
// reset response
response.value = null
})
})
Expand All @@ -109,6 +119,23 @@ onUnmounted(() => {
audioPlayer.removeListener(onAudioPlayerStatus)
})
const processQueryParams = (params) => {
// log
console.log('Processing query params', JSON.stringify(params))
// auto-fill
if (params?.foremostApp) {
const expert = store.experts.find((p) => p.triggerApps?.find((app) => app.identifier == params.foremostApp))
if (expert) {
console.log(`Tiggered on ${params.foremostApp}: filling prompt with expert ${expert.name}`)
onSetExpertPrompt(expert.id)
}
}
}
const onSetExpertPrompt = (id) => {
const prompt = store.experts.find((p) => p.id == id)
emitEvent('set-expert-prompt', prompt.prompt)
Expand All @@ -128,7 +155,6 @@ const onExperts = () => {
const onKeyUp = (event) => {
if (event.key === 'Escape') {
onClose()
window.api.anywhere.cancel()
}
}
Expand All @@ -139,6 +165,7 @@ const onClick = (ev) => {
}
const onClose = () => {
audioPlayer.stop()
window.api.anywhere.cancel()
}
Expand Down
5 changes: 3 additions & 2 deletions src/types/window.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { BrowserWindowConstructorOptions } from 'electron'
interface CreateWindowOpts extends BrowserWindowConstructorOptions {
queryParams?: strDict,
hash? : string,
keepHidden?: boolean;
queryParams?: strDict;
hash?: string;
}

0 comments on commit 685a776

Please sign in to comment.