Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): Tweak node creator search logic for AI sub-nodes #10025

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const createVectorStoreNode = (args: VectorStoreNodeConstructorArgs) =>
codex: {
categories: ['AI'],
subcategories: {
AI: ['Vector Stores'],
AI: ['Vector Stores', 'Root Nodes'],
},
resources: {
primaryDocumentation: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,23 @@ export const useViewStacks = defineStore('nodeCreatorViewStacks', () => {
}

if (stack.search && searchBaseItems.value) {
const searchBase =
searchBaseItems.value.length > 0
? searchBaseItems.value
: flattenCreateElements(stack.baselineItems ?? []);

let searchBase: INodeCreateElement[] = searchBaseItems.value;
const canvasHasAINodes = useCanvasStore().aiNodes.length > 0;
const filteredNodes =
isAiRootView(stack) || canvasHasAINodes ? searchBase : filterOutAiNodes(searchBase);

const searchResults = extendItemsWithUUID(searchNodes(stack.search || '', filteredNodes));
if (searchBaseItems.value.length === 0) {
searchBase = flattenCreateElements(stack.baselineItems ?? []);
}

if (
// Filter-out AI sub-nodes if canvas has no AI nodes and the root view is not AI
!(isAiRootView(stack) || canvasHasAINodes) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could extract isAiRootView(stack) || canvasHasAINodes into a new computed (also used below in groupIfAiRootNodes

// or if the source is a plus endpoint or a node connection drop
['plus_endpoint', 'node_connection_drop'].includes(nodeCreatorStore.openSource)
) {
searchBase = filterOutAiNodes(searchBase);
}

const searchResults = extendItemsWithUUID(searchNodes(stack.search || '', searchBase));

const groupedNodes = groupIfAiNodes(searchResults, false) ?? searchResults;
// Set the active index to the second item if there's a section
Expand Down Expand Up @@ -183,12 +190,26 @@ export const useViewStacks = defineStore('nodeCreatorViewStacks', () => {
return stack.rootView === AI_NODE_CREATOR_VIEW;
}

function filterAiRootNodes(items: NodeCreateElement[]) {
return items.filter((node) => {
if (node.type !== 'node') return false;

return node.properties.codex?.subcategories?.[AI_SUBCATEGORY].includes(
AI_CATEGORY_ROOT_NODES,
);
});
}

function groupIfAiNodes(items: INodeCreateElement[], sortAlphabetically = true) {
const aiNodes = items.filter((node): node is NodeCreateElement => isAINode(node));
const canvasHasAINodes = useCanvasStore().aiNodes.length > 0;

if (aiNodes.length > 0) {
if (aiNodes.length > 0 && (canvasHasAINodes || isAiRootView(getLastActiveStack()))) {
const sectionsMap = new Map<string, NodeViewItemSection>();
aiNodes.forEach((node) => {
const aiRootNodes = filterAiRootNodes(aiNodes);
const aiSubNodes = difference(aiNodes, aiRootNodes);

aiSubNodes.forEach((node) => {
const section = node.properties.codex?.subcategories?.[AI_SUBCATEGORY]?.[0];

if (section) {
Expand All @@ -209,34 +230,14 @@ export const useViewStacks = defineStore('nodeCreatorViewStacks', () => {
});

const nonAiNodes = difference(items, aiNodes);
const nonAiTriggerNodes = nonAiNodes.filter(
(item) => item.type === 'node' && useNodeTypesStore().isTriggerNode(item.properties.name),
);

const nonAiRegularNodes = difference(nonAiNodes, nonAiTriggerNodes);

if (nonAiNodes.length > 0) {
let sectionKey = '';
if (nonAiRegularNodes.length && nonAiTriggerNodes.length) {
sectionKey = i18n.baseText('nodeCreator.actionsCategory.regularAndTriggers');
} else {
sectionKey = nonAiRegularNodes.length
? i18n.baseText('nodeCreator.actionsCategory.regularNodes')
: i18n.baseText('nodeCreator.actionsCategory.triggerNodes');
}

const nodesKeys = nonAiNodes.map((node) => node.key);

sectionsMap.set(sectionKey, {
key: sectionKey,
title: sectionKey,
items: [...nodesKeys],
});
}
// Convert sectionsMap to array of sections
const sections = Array.from(sectionsMap.values());

return groupItemsInSections(items, sections, sortAlphabetically);
return [
...nonAiNodes,
...aiRootNodes,
...groupItemsInSections(aiSubNodes, sections, sortAlphabetically),
];
}

return items;
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ export default defineComponent({

// Automatically deselect all nodes and select the current one and also active
// current node. But only if it's added manually by the user (not by undo/redo mechanism)
if (trackHistory) {
if (trackHistory && !isAutoAdd) {
this.deselectAllNodes();
setTimeout(() => {
this.nodeSelectedByName(
Expand Down
Loading