Organise the prompt service code a bit better

Note that instance fields are evaluated during instance creation, at the start of the constructor.
This commit is contained in:
Mattias Granlund 2024-03-20 11:58:07 +01:00
parent 14def6e616
commit 0d5dbc25b7

View File

@ -6,6 +6,7 @@ export type SystemPrompt = {
id: string; id: string;
prompt: string; prompt: string;
context?: { context?: {
// TODO: camelCase this field
branch_id?: string; branch_id?: string;
action?: string; action?: string;
}; };
@ -20,19 +21,19 @@ type PromptResponse = {
export class PromptService { export class PromptService {
prompt$ = new Subject<SystemPrompt>(); prompt$ = new Subject<SystemPrompt>();
constructor() { private unlisten = listen<SystemPrompt>('git_prompt', async (e) => {
this.subscribe(async (payload) => { // You can send an action token to e.g. `fetch_target_data` and it will be echoed in
if (payload.context?.action == 'auto') { // these events. The action `auto` is used by the `BaseBranchService` so we can not
this.cancel(payload.id).then(() => console.log('cancelled auto askpass', payload)); // respond to them.
} else { if (e.payload.context?.action != 'auto') {
this.prompt$.next(payload); this.prompt$.next(e.payload);
} } else {
}); // Always cancel actions that are marked "auto", e.g. periodic sync
} await this.cancel(e.payload.id);
}
});
private subscribe(callback: (params: SystemPrompt) => Promise<void> | void) { constructor() {}
return listen<SystemPrompt>('git_prompt', (e) => callback(e.payload));
}
async respond(payload: PromptResponse) { async respond(payload: PromptResponse) {
return await invoke('submit_prompt_response', payload); return await invoke('submit_prompt_response', payload);
@ -41,4 +42,8 @@ export class PromptService {
async cancel(id: string) { async cancel(id: string) {
return await invoke('submit_prompt_response', { id: id, response: null }); return await invoke('submit_prompt_response', { id: id, response: null });
} }
destroy() {
this.unlisten();
}
} }