n8n/packages/frontend/@n8n/stores/src/useAgentRequestStore.ts
Elias Meire ab2c8b56ab
feat(core): Generate HITL tool nodes for sendAndWait operations (#22315)
Co-authored-by: yehorkardash <yehor.kardash@n8n.io>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
2026-01-23 10:04:11 +00:00

93 lines
2.4 KiB
TypeScript

import { useLocalStorage } from '@vueuse/core';
import type { AgentRequestQuery } from 'n8n-workflow';
import { defineStore } from 'pinia';
const LOCAL_STORAGE_AGENT_REQUESTS = 'N8N_AGENT_REQUESTS';
export interface IAgentRequest {
query: AgentRequestQuery;
toolName?: string;
}
export interface IAgentRequestStoreState {
[workflowId: string]: {
[nodeName: string]: IAgentRequest;
};
}
export const useAgentRequestStore = defineStore('agentRequest', () => {
// State
const agentRequests = useLocalStorage<IAgentRequestStoreState>(LOCAL_STORAGE_AGENT_REQUESTS, {});
// Helper function to ensure workflow and node entries exist
const ensureWorkflowAndNodeExist = (workflowId: string, nodeId: string): void => {
if (!agentRequests.value[workflowId]) {
agentRequests.value[workflowId] = {};
}
if (!agentRequests.value[workflowId][nodeId]) {
agentRequests.value[workflowId][nodeId] = { query: {} };
}
};
// Getters
const getAgentRequests = (workflowId: string, nodeId: string): IAgentRequest['query'] => {
return agentRequests.value[workflowId]?.[nodeId]?.query || {};
};
const getQueryValue = (
workflowId: string,
nodeId: string,
nodeName: string,
paramName?: string,
): unknown => {
const query = agentRequests.value[workflowId]?.[nodeId]?.query?.[nodeName];
if (typeof query === 'string' || !paramName) {
return query;
}
return query?.[paramName];
};
const setAgentRequestForNode = (
workflowId: string,
nodeId: string,
request: IAgentRequest,
): void => {
ensureWorkflowAndNodeExist(workflowId, nodeId);
agentRequests.value[workflowId][nodeId] = {
...request,
query: { ...request.query },
};
};
const clearAgentRequests = (workflowId: string, nodeId: string): void => {
if (agentRequests.value[workflowId]) {
agentRequests.value[workflowId][nodeId] = { query: {} };
}
};
const clearAllAgentRequests = (workflowId?: string): void => {
if (workflowId) {
agentRequests.value[workflowId] = {};
} else {
agentRequests.value = {};
}
};
const getAgentRequest = (workflowId: string, nodeId: string): IAgentRequest | undefined => {
if (agentRequests.value[workflowId]) return agentRequests.value[workflowId]?.[nodeId];
return undefined;
};
return {
agentRequests,
getAgentRequests,
getQueryValue,
setAgentRequestForNode,
clearAgentRequests,
clearAllAgentRequests,
getAgentRequest,
};
});