diff --git a/frontend/src/backend.js b/frontend/src/backend.js index 753ef7e..bc1283c 100644 --- a/frontend/src/backend.js +++ b/frontend/src/backend.js @@ -157,6 +157,14 @@ export function sendAction(action, body = {}, callback = null) { return deferred.promise; } +export async function act (action, body, { alert = null, cache = null } = {}) { + const res = await sendAction(action, body); + if (res.error) { ui.alert(`Error: ${res.error}`); return null; } + if (cache) invalidateCache(cache); + if (alert) ui.alert(alert); + return res; +} + export function ping() { if (socket && socket.readyState === WebSocket.OPEN) { const data = JSON.stringify({ path: 'ping' }); diff --git a/frontend/src/ui.js b/frontend/src/ui.js index 7f90aa5..147e703 100644 --- a/frontend/src/ui.js +++ b/frontend/src/ui.js @@ -1,5 +1,6 @@ // Global utility functions related with UI functionality +import { ref } from 'vue'; import router from './router' export function goto (path) { @@ -33,6 +34,15 @@ export function contextMenu (entries) { }); } +// Helper object to manage track toggable ui elements +export function useToggleSet () { + const state = ref({}); + return { + has(key) { return Boolean(state.value[key]); }, + toggle(key) { state.value[key] = !state.value[key]; }, + }; +} + // === Events === // diff --git a/frontend/src/views/Agent.vue b/frontend/src/views/Agent.vue index 28087ea..64e2b6f 100644 --- a/frontend/src/views/Agent.vue +++ b/frontend/src/views/Agent.vue @@ -148,45 +148,35 @@ // === Configuration === // - function updateIdentifier() { + async function updateIdentifier() { const value = prompt(`Enter a new identifier for ${agent.value.name}`); if (value) { - backend.sendAction("update-agent", { + let res = await backend.act("update-agent", { // agentId is necessary here agentId: agent.value.id, // used as value instead of identifier agentname: value, - }).then(res => { - ui.alert(res.error ? - 'Error updating agent identifier' : - 'Agent identifier updated' - ); - if (!res.error) { - agent.value.agentname = value; - backend.invalidateCache({agents: true}); - } + }, { + cache: {agents: true}, + alert: 'Agent identifier updated' }); + if (!res.error) agent.value.agentname = value; } else { ui.alert("Cancelled identifier update"); } } - function updateName() { + async function updateName() { const value = prompt(`Enter a new name for ${agent.value.name}`); if (value) { - backend.sendAction("update-agent", { + let res = await backend.sendAction("update-agent", { agentname: agent.value.agentname, name: value, - }).then(res => { - ui.alert(res.error ? - 'Error updating agent name' : - 'Agent name updated' - ); - if (!res.error) { - agent.value.name = value; - backend.invalidateCache({agents: true}); - } + }, { + alert: 'Agent name updated', + cache: {agents: true}, }); + if (!res.error) agent.value.name = value; } else { ui.alert("Cancelled name update"); } @@ -215,19 +205,13 @@ } } - let res = await backend.sendAction("update-agent", { + await backend.act("update-agent", { agentname: agent.value.agentname, data: dataObject, + }, { + alert: 'Agent updated', + cache: {agents: true}, }); - - ui.alert(res.error ? - 'Error updating agent' : - 'Agent updated' - ); - - if (!res.error) { - backend.invalidateCache({agents: true}); - } } async function saveAddingTool() { @@ -292,8 +276,8 @@ Enable Instruction/Input -