Skip to content

Commit

Permalink
Add special suggestion for Math and Compare nodes in node selector (#…
Browse files Browse the repository at this point in the history
…2908)

* Add special suggestion for Math and Compare nodes in node selector

* docs

* Fix type error
  • Loading branch information
RunDevelopment authored May 24, 2024
1 parent 0876bc8 commit 4854e59
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 45 deletions.
11 changes: 10 additions & 1 deletion backend/src/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
check_naming_conventions,
check_schema_types,
)
from .node_data import IteratorInputInfo, IteratorOutputInfo, KeyInfo, NodeData
from .node_data import (
IteratorInputInfo,
IteratorOutputInfo,
KeyInfo,
NodeData,
SpecialSuggestion,
)
from .output import BaseOutput
from .settings import Setting
from .types import FeatureId, InputId, NodeId, NodeKind, OutputId, RunFn
Expand Down Expand Up @@ -98,6 +104,7 @@ def to_dict(self):
def register(
self,
schema_id: str,
*,
name: str,
description: str | list[str],
inputs: list[BaseInput | NestedGroup],
Expand All @@ -114,6 +121,7 @@ def register(
iterator_outputs: list[IteratorOutputInfo] | IteratorOutputInfo | None = None,
node_context: bool = False,
key_info: KeyInfo | None = None,
suggestions: list[SpecialSuggestion] | None = None,
):
if not isinstance(description, str):
description = "\n\n".join(description)
Expand Down Expand Up @@ -183,6 +191,7 @@ def inner_wrapper(wrapped_func: T) -> T:
iterator_inputs=iterator_inputs,
iterator_outputs=iterator_outputs,
key_info=key_info,
suggestions=suggestions or [],
side_effects=side_effects,
deprecated=deprecated,
node_context=node_context,
Expand Down
55 changes: 54 additions & 1 deletion backend/src/api/node_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any
from enum import Enum
from typing import Any, Mapping

import navi

Expand Down Expand Up @@ -71,6 +72,57 @@ def to_dict(self):
return self._data


class SpecialSuggestion:
"""
A special suggestion in chaiNNer's context node selector.
A suggestion consists of 3 parts:
1. The search query to match. The query may optionally contain a pattern at the end
to supply a value to an input. E.g. `+{2}` will match the search query "+123"
and "123" will be parsed for the input with ID 2.
2. The name of the suggestion. This is the text that will be displayed in the
suggestion list.
3. The input values to supply to the node. This is a mapping of input IDs to the
values to supply to them. Values that aren't defined here will be left as
default values.
"""

def __init__(
self,
query: str,
*,
name: str | None = None,
inputs: Mapping[InputId | int, Any] = {},
) -> None:
self.query, self.parse_input = SpecialSuggestion._parse_query(query)
self.name = name
self.inputs: dict[InputId, Any] = {InputId(k): v for k, v in inputs.items()}

@staticmethod
def _parse_query(query: str) -> tuple[str, InputId | None]:
# e.g. "+{2}"
if "{" in query:
query, input_id = query.split("{")
input_id = int(input_id[:-1])
return query, InputId(input_id)
return query, None

def to_dict(self):
def convert_value(value: Any) -> Any:
if isinstance(value, bool):
return int(value)
if isinstance(value, Enum):
return value.value
return value

return {
"query": self.query,
"name": self.name,
"parseInput": self.parse_input,
"inputs": {k: convert_value(v) for k, v in self.inputs.items()},
}


@dataclass(frozen=True)
class NodeData:
schema_id: str
Expand All @@ -88,6 +140,7 @@ class NodeData:
iterator_outputs: list[IteratorOutputInfo]

key_info: KeyInfo | None
suggestions: list[SpecialSuggestion]

side_effects: bool
deprecated: bool
Expand Down
39 changes: 38 additions & 1 deletion backend/src/packages/chaiNNer_standard/utility/math/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from enum import Enum

from api import KeyInfo
from api import KeyInfo, SpecialSuggestion
from nodes.properties.inputs import EnumInput, NumberInput
from nodes.properties.outputs import BoolOutput

Expand Down Expand Up @@ -79,6 +79,43 @@ class Comparison(Enum):
""",
).suggest(),
],
suggestions=[
SpecialSuggestion(
"={2}",
name="Comparison: Equal",
inputs={0: Comparison.EQUAL},
),
SpecialSuggestion(
"=={2}",
name="Comparison: Equal",
inputs={0: Comparison.EQUAL},
),
SpecialSuggestion(
"!={2}",
name="Comparison: Not Equal",
inputs={0: Comparison.NOT_EQUAL},
),
SpecialSuggestion(
">{2}",
name="Comparison: Greater",
inputs={0: Comparison.GREATER},
),
SpecialSuggestion(
"<{2}",
name="Comparison: Less",
inputs={0: Comparison.LESS},
),
SpecialSuggestion(
">={2}",
name="Comparison: Greater or Equal",
inputs={0: Comparison.GREATER_EQUAL},
),
SpecialSuggestion(
"<={2}",
name="Comparison: Less or Equal",
inputs={0: Comparison.LESS_EQUAL},
),
],
key_info=KeyInfo.enum(0),
)
def compare_node(op: Comparison, left: float, right: float) -> bool:
Expand Down
33 changes: 33 additions & 0 deletions backend/src/packages/chaiNNer_standard/utility/math/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
from enum import Enum

from api import SpecialSuggestion
from nodes.properties.inputs import EnumInput, NumberInput
from nodes.properties.outputs import NumberOutput

Expand Down Expand Up @@ -90,6 +91,38 @@ def nonZero(x: number): number {
)
.as_passthrough_of(0)
],
suggestions=[
SpecialSuggestion(
"+{2}",
name="Math: Add",
inputs={1: MathOperation.ADD},
),
SpecialSuggestion(
"-{2}",
name="Math: Subtract",
inputs={1: MathOperation.SUBTRACT},
),
SpecialSuggestion(
"*{2}",
name="Math: Multiply",
inputs={1: MathOperation.MULTIPLY, 2: 1},
),
SpecialSuggestion(
"/{2}",
name="Math: Divide",
inputs={1: MathOperation.DIVIDE, 2: 1},
),
SpecialSuggestion(
"**{2}",
name="Math: Power",
inputs={1: MathOperation.POWER, 2: 1},
),
SpecialSuggestion(
"^{2}",
name="Math: Power",
inputs={1: MathOperation.POWER, 2: 1},
),
],
)
def math_node(op: MathOperation, a: float, b: float) -> int | float:
if op == MathOperation.ADD:
Expand Down
1 change: 1 addition & 0 deletions backend/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ async def nodes(_request: Request):
"iteratorInputs": [x.to_dict() for x in node.iterator_inputs],
"iteratorOutputs": [x.to_dict() for x in node.iterator_outputs],
"keyInfo": node.key_info.to_dict() if node.key_info else None,
"suggestions": [x.to_dict() for x in node.suggestions],
"description": node.description,
"seeAlso": node.see_also,
"icon": node.icon,
Expand Down
1 change: 1 addition & 0 deletions src/common/SchemaMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const BLANK_SCHEMA: NodeSchema = {
hasSideEffects: false,
deprecated: false,
features: [],
suggestions: [],
};

export class SchemaMap {
Expand Down
8 changes: 8 additions & 0 deletions src/common/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ export interface TypeKeyInfo {
readonly expression: ExpressionJson;
}

export interface SpecialSuggestion {
readonly query: string;
readonly name?: string | null;
readonly parseInput?: InputId | null;
readonly inputs: Partial<InputData>;
}

export interface NodeSchema {
readonly name: string;
readonly category: CategoryId;
Expand All @@ -315,6 +322,7 @@ export interface NodeSchema {
readonly iteratorInputs: readonly IteratorInputInfo[];
readonly iteratorOutputs: readonly IteratorOutputInfo[];
readonly keyInfo?: KeyInfo | null;
readonly suggestions: readonly SpecialSuggestion[];
readonly schemaId: SchemaId;
readonly hasSideEffects: boolean;
readonly deprecated: boolean;
Expand Down
Loading

0 comments on commit 4854e59

Please sign in to comment.