wren node: targets from the wiring, on wren 2

Every output is a target and every input besides prompt a reading. A
targets setting gives descriptions, values, ranges and per-target
guards, model takes a trained pack's artifact digest, min_confidence
defaults to the model's threshold. A command is applied whole or not at
all, and targets the model was not trained on read degraded. The home
path (area/kind ports, groups, brightness, temperature answers) is gone.
This commit is contained in:
2026-10-24 21:28:42 +02:00
parent 6892651e76
commit 07fa5a2dc2
5 changed files with 745 additions and 863 deletions
+292 -506
View File
@@ -1,18 +1,17 @@
"""Wren: a typed command in, values for the devices wired to it out.
"""Wren: a typed command in, values for the targets wired to it out.
`Wren <https://github.com/stroblme/wren>`_ is a small language model trained on
one job: read a house — its areas, the devices in each and their states — and
answer a command with calls on those devices. This node is the bridge. Its
output ports *are* the devices, named ``<area>_<kind>``, so the wiring is the
`Wren <https://github.com/stroblme/wren>`_ is a small language model that
turns a short command into calls on a list of targets. This node is the
bridge. Its ports *are* the targets: each output is one a command may set,
each input besides ``prompt`` a reading it may ask about. The wiring is the
allowlist: the model can reach nothing that is not wired to the node.
"""
from __future__ import annotations
import re
import time
from collections.abc import Iterable
from typing import Any, NamedTuple
from typing import TYPE_CHECKING, Any
from pydantic import BaseModel, ConfigDict, Field
from threadpoolctl import threadpool_limits
@@ -20,6 +19,9 @@ from threadpoolctl import threadpool_limits
from fluksio.flow.messages import DType, MessageSpec
from fluksio.flow.nodes.base import Node
if TYPE_CHECKING:
from fastapi import FastAPI
def _wren() -> Any:
"""The model package, which is optional.
@@ -33,120 +35,29 @@ def _wren() -> Any:
except ImportError:
# Not `pip install wren`: that name on PyPI is another package.
raise RuntimeError(
"the wren node needs the model: pip install git+https://github.com/"
"stroblme/wren@eae4d6ff9ded68e36624972fd3f4a8f8d210b0c2"
"the wren node needs the model: pip install "
"git+https://github.com/stroblme/wren"
) from None
if not hasattr(wren, "Target"):
raise RuntimeError(
"the wren node needs wren 2, the installed one is older: pip install "
"--upgrade git+https://github.com/stroblme/wren"
)
return wren
#: The last word of a target port, and the kind of device it stands for.
KIND_WORDS = {
"light": "light",
"lamp": "light",
"brightness": "light",
"setpoint": "heat",
"heating": "heat",
"heat": "heat",
"boost": "heat",
"media": "media",
"music": "media",
"tv": "media",
"shutter": "shutter",
"blind": "shutter",
"cover": "shutter",
"water": "water",
"watering": "water",
"sprinkler": "water",
"switch": "switch",
"plug": "switch",
"socket": "switch",
"appliances": "switch",
}
#: What the node reads, and the types each may be. Only the prompt wakes it.
#: Besides these, ``<area>_temperature`` inputs answer "how warm is it".
INPUTS = {
"prompt": {DType.STR},
"presence": {DType.STR},
"outdoor": {DType.FLOAT, DType.INT},
"dark": {DType.BOOL},
}
#: The input the command arrives on, and the only one that wakes the node.
PROMPT = "prompt"
#: The output the calls are reported on, and the two that say what came of
#: the command. Every other output is a device.
#: the command. Every other output is a target.
ACTIONS = "actions"
REPLY = "reply"
UNHANDLED = "unhandled"
#: The reply when nothing was done and nothing reads ``unhandled``.
CANT = "Sorry, I can't do that yet."
#: The output for the house's brightness, 0 to 100, which the model's
#: brightness calls write whatever area they name.
BRIGHTNESS = "brightness"
_TARGET_TYPES = {DType.BOOL, DType.INT, DType.FLOAT, DType.STR}
#: The tools that act on each kind.
_TOOLS = {
"light": "lights",
"media": "media",
"heat": "heating",
"shutter": "shutters",
"water": "watering",
"switch": "switches",
}
#: What a call sets a target to, by the target's type: (bool, number, text).
#: None is a call that type has no value for, which is then not applied.
#: `heating.set` carries its own number.
_VALUES: dict[str, tuple[bool | None, float | None, str | None]] = {
"lights.on": (True, 100, "on"),
"lights.off": (False, 0, "off"),
"media.play": (True, None, "play"),
"media.stop": (False, None, "stop"),
"heating.boost": (True, None, "boost"),
"heating.off": (False, None, "off"),
"shutters.up": (True, 100, "up"),
"shutters.down": (False, 0, "down"),
"shutters.stop": (None, None, "stop"),
"watering.start": (True, None, "on"),
"watering.stop": (False, None, "off"),
"switches.on": (True, 1, "on"),
"switches.off": (False, 0, "off"),
}
#: The brightness calls, and what "dim" and "brighter" do: a step from the
#: brightness now, or from a usual one when none is known. Dimmed lights
#: stay on.
_LEVELS = ("lights.set", "lights.dim", "lights.brighten")
_STEP, _DIMMEST, _BRIGHTEST, _USUAL = 30, 10, 100, 70
#: An applied call, said: "kitchen light on". `heating.set` says its number.
_SAID = {
"lights.on": "light on",
"lights.off": "light off",
"media.play": "media playing",
"media.stop": "media stopped",
"heating.boost": "heating boosted",
"heating.off": "heating off",
"shutters.up": "shutters up",
"shutters.down": "shutters down",
"shutters.stop": "shutters stopped",
"watering.start": "watering on",
"watering.stop": "watering off",
"switches.on": "switch on",
"switches.off": "switch off",
}
#: What every area held in training: a light, and in the living room, which
#: always existed, media and heating besides. A house missing them reads as
#: unfamiliar and the confidence drops with it, so they are filled in; a call
#: on one that nothing is wired to is reported and not applied. Every area is
#: shown, wired or not, or "the bathroom light" lands on a light that is.
_TRAINED = {"living room": ("light", "media", "heat")}
#: wren's areas outside, whose temperature is the ``outdoor`` reading.
_OUTSIDE = {"garden", "patio", "garage", "driveway", "balcony"}
#: Number words, which wren was trained on as digits.
_ONES = (
"zero one two three four five six seven eight nine ten eleven twelve "
@@ -157,154 +68,6 @@ _NUMBERS = {w: n for n, w in enumerate(_ONES)} | {
w: 20 + 10 * n for n, w in enumerate(_TENS)
}
#: A command for a whole group of devices: "turn off all lights".
_GROUP = re.compile(r"\b(all|every|everything|everywhere)\b")
#: What leaves such a command to the model and the assistant: a question, or
#: an exception to the group.
_NOT_GROUP = re.compile(
r"^(is|are|was|were|do|does|did|can|could|what|which|how|why|when|where|who)\b"
r"|\b(except|but|apart from|other than)\b"
)
#: The kind a group command names, by its word. None named is every kind.
_GROUP_KINDS = {
"light": "light",
"lights": "light",
"lamps": "light",
"music": "media",
"media": "media",
"tv": "media",
"shutters": "shutter",
"blinds": "shutter",
"heating": "heat",
"watering": "water",
"sprinklers": "water",
"switches": "switch",
"plugs": "switch",
"appliances": "switch",
}
#: The call a group command makes on each kind, by its action word.
_GROUP_TOOLS = {
"on": {
"light": "lights.on",
"media": "media.play",
"water": "watering.start",
"switch": "switches.on",
},
"off": {
"light": "lights.off",
"media": "media.stop",
"heat": "heating.off",
"water": "watering.stop",
"switch": "switches.off",
},
"open": {"shutter": "shutters.up"},
"up": {"shutter": "shutters.up"},
"raise": {"shutter": "shutters.up"},
"close": {"shutter": "shutters.down"},
"down": {"shutter": "shutters.down"},
"lower": {"shutter": "shutters.down"},
"stop": {
"media": "media.stop",
"shutter": "shutters.stop",
"water": "watering.stop",
},
}
#: A group call, said: "all lights off".
_GROUP_SAID = {
"lights.on": "lights on",
"lights.off": "lights off",
"media.play": "media playing",
"media.stop": "media stopped",
"heating.off": "heating off",
"shutters.up": "shutters up",
"shutters.down": "shutters down",
"shutters.stop": "shutters stopped",
"watering.start": "watering on",
"watering.stop": "watering off",
"switches.on": "switches on",
"switches.off": "switches off",
}
class _Call(NamedTuple):
"""A call the node makes itself, shaped as the model's."""
tool: str
args: dict[str, Any]
delay_s: float | None = None
def _device(port: str, areas: dict[str, str]) -> tuple[str, str] | None:
"""The (area, kind) a port name stands for, or None."""
for slug, area in areas.items():
word = port.removeprefix(f"{slug}_")
if word != port and word in KIND_WORDS:
return area, KIND_WORDS[word]
return None
def _on(value: Any) -> bool:
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
return value > 0
return str(value).strip().lower() in {"on", "up", "open", "play", "true"}
def _state(kind: str, values: list[Any]) -> str:
"""A device's state as the model reads it, from what its targets hold."""
if kind == "heat":
temps = [
v for v in values if isinstance(v, (int, float)) and not isinstance(v, bool)
]
return f"{round(temps[0])}C" if temps else "20C"
on = bool(values) and _on(values[0])
if kind == "shutter":
return "up" if on else "down"
return "on" if on else "off"
def _value(call: Any, dtype: DType) -> Any:
"""What ``call`` sets a target of this type to, or None."""
if call.tool == "heating.set":
temp = call.args.get("temp")
flag, number, text = (
None,
temp if isinstance(temp, (int, float)) else None,
None,
)
elif call.tool == "lights.set":
level = call.args.get("level")
flag, number, text = (
None,
level if isinstance(level, (int, float)) else None,
None,
)
else:
flag, number, text = _VALUES.get(call.tool, (None, None, None))
if dtype is DType.BOOL:
return flag
if dtype is DType.STR:
return text
if number is None:
return None
return int(number) if dtype is DType.INT else float(number)
def _said(call: Any, area: str) -> str:
"""An applied call as the reply says it: "garage light on in 5 minutes"."""
if call.tool == "heating.set":
said = f"heating to {_value(call, DType.FLOAT):g} degrees"
elif call.tool == "lights.set":
said = f"brightness at {_value(call, DType.FLOAT):g} percent"
else:
said = _SAID.get(call.tool, call.tool)
if call.delay_s:
minutes = round(call.delay_s / 60)
said += f" in {minutes} minute{'s' * (minutes != 1)}"
# The brightness is the house's, not the area's the call names.
return said if call.tool == "lights.set" else f"{area} {said}"
def _plain(command: str) -> str:
"""The command as the model was trained on it: lower case, no sentence marks.
@@ -343,52 +106,125 @@ def _digits(command: str) -> str:
return re.sub(rf"\b({words})\b", lambda m: str(_NUMBERS[m[1]]), command)
def _named(areas: Iterable[str], command: str) -> list[str]:
"""The areas ``command`` names, in the house's order."""
return [a for a in areas if re.search(rf"\b{re.escape(a)}\b", command)]
def _learnt(target: dict[str, Any]) -> tuple[Any, ...]:
"""What a model learns of a target: all but its unit, which it never sees,
and a range as floats, so [5, 30] is (5.0, 30.0)."""
low_high = target.get("range")
return (
target.get("name"),
target.get("type"),
target.get("values"),
None if low_high is None else tuple(float(n) for n in low_high),
target.get("description", ""),
bool(target.get("readonly", False)),
)
def _asked(house: Any, command: str) -> set[str]:
"""The areas a call may land on: those the command names, or else the
user's and the living room.
def _unset(target: dict[str, Any]) -> Any:
"""The value of a target nothing has set yet: off, 0 within its range, or
its first value."""
if target["type"] == "bool":
return False
if target["type"] == "str":
return (target["values"] or [""])[0]
low, high = target["range"] or (0, 0)
number = min(max(0, low), high)
return int(number) if target["type"] == "int" else float(number)
That is how wren picks the areas it shows as the command's own. The few
it adds besides are there to be told apart, and a call on one of them is a
word the model never learned landing on a neighbour: "switch on the
appliances" turned on the bathroom light at 0.97.
"""
return set(_named(house.areas, command)) or {house.user_area, "living room"}
def _typed(value: Any, dtype: DType) -> Any:
"""A call's value as its port's type: wren may write 21 for a float."""
if dtype is DType.INT:
return int(value)
if dtype is DType.FLOAT:
return float(value)
return value
def _spoken(value: Any, unit: str | None) -> str:
"""A value as the reply says it: "on", "down", "21 °C"."""
if isinstance(value, bool):
return "on" if value else "off"
if isinstance(value, str):
return value.lower()
return f"{value:g}" + (f" {unit}" if unit else "")
def _said(port: str, value: Any, unit: str | None, delay_s: float | None) -> str:
"""An applied call as the reply says it: "living room heating to 21 °C in
5 minutes"."""
to = "" if isinstance(value, (bool, str)) else "to "
said = f"{port.replace('_', ' ')} {to}{_spoken(value, unit)}"
if delay_s:
minutes = delay_s >= 60
n = round(delay_s / 60 if minutes else delay_s)
said += f" in {n} {'minute' if minutes else 'second'}{'s' * (n != 1)}"
return said
def _text(call: Any) -> str:
"""The call as the chat shows it, less the device it names."""
args = ", ".join(f"{k}={v}" for k, v in call.args.items() if k != "id")
return f"{call.tool}({args})"
"""The call as wren writes it, less its delay: ``set(kitchen_light, true)``."""
if call.value is None:
return f"read({call.target})"
value = call.value
if isinstance(value, bool):
value = "true" if value else "false"
elif isinstance(value, float):
value = f"{value:g}"
return f"set({call.target}, {value})"
class TargetInfo(BaseModel):
"""What the model is told of a target besides its port, and how far it is
trusted with it."""
model_config = ConfigDict(extra="forbid")
description: str = Field(
default="",
description='More words for the target, which help pick it: "awning over the patio".',
)
values: list[str] | None = Field(
default=None,
description='The only values a str target takes: ["UP", "DOWN", "STOP"].',
)
range: tuple[float, float] | None = Field(
default=None,
description="The lowest and highest value of a number: [5, 30].",
)
unit: str | None = Field(
default=None,
description='Said after a number in the reply: "°C". The model never sees it.',
)
min_confidence: float | None = Field(
default=None,
ge=0,
le=1,
description="The node's min_confidence for this target alone, such as a "
"higher one for a lock.",
)
pass_on: bool = Field(
default=False,
description="Never set by wren: a command on it goes on to unhandled.",
)
class WrenNode(Node):
"""Turn the command on ``prompt`` into values for the devices wired to it.
"""Turn the command on ``prompt`` into values for the targets wired to it.
Inputs: ``prompt`` (the command, and the only one that wakes the node),
``presence`` (the area the user is in), ``outdoor`` (°C), ``dark``
(otherwise read off the clock: before 07:00 or from 18:00) and any
``<area>_temperature``. A question for a temperature is answered from
that reading, or from ``outdoor`` for an area outside.
Inputs: ``prompt``, the command and the only one that wakes the node, and
any number of readings, which a command may ask about but not set.
The command reaches the model with the ``aliases`` applied and its number
words as digits. A command for all of a kind ("turn off all lights",
"everything off in the office") is not the model's: the node applies it
to every wired device of that kind itself.
Outputs: ``actions``, a list of records, one per call, ``reply``, a
sentence saying what was done or what a reading holds, or that nothing
was done when no node reads ``unhandled``, ``unhandled``, the prompt when
nothing was, and any number of targets a command may set.
Outputs: ``actions``, a list of records, one per call and target,
``reply``, a sentence saying what was applied ("Kitchen light on."), or
that nothing was when no node reads ``unhandled``, ``unhandled``, the
prompt when nothing was, ``brightness``, the house's, for "dim the
lights" or "set the brightness to 30%", and any number of targets
named ``<area>_<kind>``. Targets of one area and kind are one device to
the model; each takes what the call means for its type.
A delayed call is held by the engine's work queue, like the Trigger
node's second value.
A target is named by its port. Its type, its current value and its entry
in ``targets`` are what the model is shown. The command reaches the model
with the ``aliases`` applied and its number words as digits. A delayed
call is held by the engine's work queue, like the Debounce node's second
value.
"""
class Params(BaseModel):
@@ -398,19 +234,34 @@ class WrenNode(Node):
default=False,
description="Write to the targets. Off, the node only reports what it would do.",
)
min_confidence: float = Field(
default=0.93,
model: str = Field(
default="",
pattern=r"^(sha256:[0-9a-f]{64})?$",
description=(
"The trained model, an artifact digest. Empty runs wren's demo "
"model, which knows only its demo home."
),
)
min_confidence: float | None = Field(
default=None,
ge=0,
le=1,
description="Below this, nothing is written; the calls are still reported.",
description="Below this, nothing is written; the calls are still "
"reported. Empty takes the model's own threshold.",
)
targets: dict[str, TargetInfo] = Field(
default_factory=dict,
description="By target port: description, values, range, unit, "
'min_confidence and pass_on, as {"canopy": {"values": ["UP", '
'"DOWN", "STOP"], "description": "awning over the patio"}}.',
)
aliases: dict[str, str] = Field(
default_factory=dict,
description="The house's own words, and words heard wrong, in wren's: "
description="Your own words, and words heard wrong, in the model's: "
'{"traverse": "living room light", "page too": "patio"}.',
)
__slots__ = ("cfg", "devices", "readings")
__slots__ = ("cfg", "targets", "pack", "untrained")
# A call is a command to a device; doing it twice is not doing it once.
idempotent = False
@@ -423,69 +274,64 @@ class WrenNode(Node):
name: str | None = None,
):
self.cfg = self.Params.model_validate(params or {})
wren = _wren()
_wren()
inputs = self._normalize_ports(requires)
outputs = self._normalize_ports(provides)
areas = {area.replace(" ", "_"): area for area in wren.AREAS}
#: The area each ``<area>_temperature`` input reads, to its port.
self.readings: dict[str, str] = {}
for spec in inputs:
reading = areas.get(spec.port.removesuffix("_temperature"))
if reading and spec.port.endswith("_temperature"):
self.readings[reading] = spec.port
allowed = INPUTS["outdoor"] if reading else INPUTS.get(spec.port)
if allowed is None:
raise ValueError(
f"'{spec.port}' is not an input of wren. It reads "
f"{', '.join(INPUTS)} and <area>_temperature."
)
if spec.dtype not in allowed:
raise ValueError(
f"'{spec.port}' is {spec.dtype.value}; wren reads it as "
f"{' or '.join(sorted(d.value for d in allowed))}."
)
if not any(spec.port == "prompt" for spec in inputs):
if not any(spec.port == PROMPT for spec in inputs):
raise ValueError("wren needs a 'prompt' input: the command to act on.")
#: (area, kind) to the ports standing for that device.
self.devices: dict[tuple[str, str], list[str]] = {}
ports: list[tuple[MessageSpec, bool]] = []
for spec in outputs:
if spec.port == ACTIONS:
if spec.dtype is not DType.LIST or spec.item_dtype is not DType.RECORD:
raise ValueError(f"'{ACTIONS}' is a list of records.")
continue
if spec.port in (REPLY, UNHANDLED):
elif spec.port in (REPLY, UNHANDLED):
if spec.dtype is not DType.STR:
raise ValueError(f"'{spec.port}' is a str.")
continue
if spec.port == BRIGHTNESS:
if spec.dtype not in (DType.FLOAT, DType.INT):
raise ValueError(f"'{BRIGHTNESS}' is a float or an int.")
continue
device = _device(spec.port, areas)
if device is None:
raise ValueError(
f"'{spec.port}' names no device wren knows. A target is "
f"<area>_<kind>, the area one of {', '.join(areas)}, the kind "
f"one of {', '.join(KIND_WORDS)}."
)
if device[1] not in wren.KINDS: # switches came with wren's s10
raise ValueError(
f"'{spec.port}' is a {device[1]}, which this wren does not know."
)
else:
ports.append((spec, False))
for spec in inputs:
if spec.port != PROMPT:
ports.append((spec, True))
elif spec.dtype is not DType.STR:
raise ValueError(f"'{PROMPT}' is a str.")
#: Each target by its port, as a model's ``meta`` lists the targets it
#: was trained on.
self.targets: dict[str, dict[str, Any]] = {}
for spec, readonly in ports:
if spec.dtype not in _TARGET_TYPES:
raise ValueError(
f"'{spec.port}' is {spec.dtype.value}; a target is bool, int, "
"float or str."
)
self.devices.setdefault(device, []).append(spec.port)
info = self.cfg.targets.get(spec.port, TargetInfo())
self.targets[spec.port] = {
"name": spec.port,
"type": spec.dtype.value,
"values": info.values,
"range": None if info.range is None else list(info.range),
"description": info.description,
"unit": info.unit,
"readonly": readonly,
}
for port, info in self.cfg.targets.items():
kind = self.targets[port]["type"] if port in self.targets else None
if kind is None:
raise ValueError(f"'targets' names '{port}', which no target port is.")
if info.values is not None and kind != "str":
raise ValueError(f"'{port}' is {kind}; only a str target has values.")
if info.range is not None and kind not in ("int", "float"):
raise ValueError(f"'{port}' is {kind}; only a number has a range.")
# Set on start, from the model and the targets it was trained on.
self.pack: dict[str, Any] | None = None
self.untrained: set[str] = set()
super().__init__(
self._act,
# The prompt is the event; the rest is read when it arrives.
requires=[
spec.model_copy(update={"trigger": spec.port == "prompt"})
spec.model_copy(update={"trigger": spec.port == PROMPT})
for spec in inputs
],
provides=outputs,
@@ -493,130 +339,141 @@ class WrenNode(Node):
name=name or "wren",
)
def _house(
self,
wren: Any,
specs: dict[str, MessageSpec],
presence: str | None,
outdoor: float | None,
dark: bool | None,
) -> Any:
"""The house as the model reads it, each device in its current state."""
names = [specs[port].name for ports in self.devices.values() for port in ports]
current = (
self._pipeline.state.get_present(names)
async def start(self, app: FastAPI | None = None) -> None:
store = getattr(app.state, "artifact_store", None) if app else None
self.pack = self._load(store)
def _load(self, store: Any) -> dict[str, Any] | None:
"""The model, read once, or None when there is none to run.
A target it was not trained on, or one described otherwise since, is
a word it never learnt, so a call on it is never applied.
"""
wren = _wren()
path = wren.runtime.MODEL
if self.cfg.model:
path = store.path(self.cfg.model) if store is not None else None
if path is None:
self.report_health(
"degraded",
f"model {self.cfg.model[:19]}… not found, every command goes on",
)
return None
try:
pack: dict[str, Any] = wren.load_pack(path)
except Exception as exc:
self.report_health(
"degraded", f"model unreadable ({exc}), every command goes on"
)
return None
known = {t.get("name"): _learnt(t) for t in pack["meta"].get("targets", [])}
self.untrained = {
port for port, t in self.targets.items() if known.get(port) != _learnt(t)
}
if self.untrained:
self.report_health(
"degraded",
f"the model needs retraining for {', '.join(sorted(self.untrained))}; "
"commands on them go on",
)
else:
self.report_health("ok")
return pack
def _current(self, readings: dict[str, Any]) -> dict[str, Any]:
"""Each target's value now, by port: an output's from the state, a
reading's from its input."""
ports = {
spec.name: spec.port
for spec in self.output_ports
if spec.name and spec.port in self.targets
}
present = (
self._pipeline.state.get_present(list(ports))
if self._pipeline is not None
else {}
)
areas: dict[str, list[Any]] = {}
for area in wren.AREAS:
wired = {kind for (a, kind) in self.devices if a == area}
kinds = wired | set(_TRAINED.get(area, ("light",)))
areas[area] = []
for kind in (k for k in wren.KINDS if k in kinds):
ports = self.devices.get((area, kind), [])
held = [
current[specs[p].name] for p in ports if specs[p].name in current
]
# The id is ours to choose: the model is shown aliases anyway.
areas[area].append(
wren.Device(kind, f"{area}:{kind}", _state(kind, held))
)
clock = time.localtime()
where = str(presence or "").strip().lower().replace("_", " ")
return wren.House(
time=time.strftime("%H:%M", clock),
# The range the model was trained on; past it a number is unfamiliar.
outdoor_c=min(max(float(20 if outdoor is None else outdoor), -5), 30),
dark=clock.tm_hour < 7 or clock.tm_hour >= 18 if dark is None else dark,
user_area=where if where in wren.AREAS else None,
areas=areas,
)
return {ports[name]: value for name, value in present.items()} | readings
def _bar(self, port: str, threshold: float) -> float:
"""The confidence a call on ``port`` needs: the target's own, the
node's, or else the model's."""
info = self.cfg.targets.get(port, TargetInfo())
bars = (info.min_confidence, self.cfg.min_confidence, threshold)
return next(bar for bar in bars if bar is not None)
def _act(
self,
params: dict[str, Any],
prompt: str = "",
presence: str | None = None,
outdoor: float | None = None,
dark: bool | None = None,
**readings: float | None,
self, params: dict[str, Any], prompt: str = "", **readings: Any
) -> dict[str, Any] | None:
if not prompt.strip():
# An empty command is nothing to do, which is what a flow input
# starts as.
return None
wren = _wren()
specs = {spec.port: spec for spec in self.output_ports}
house = self._house(wren, specs, presence, outdoor, dark)
prompt = _aliased(prompt, self.cfg.aliases)
command = _digits(_plain(prompt))
asked = _asked(house, command)
group = self._group(command, house.areas)
if group is not None:
calls, confidence, asked = group, 1.0, set(house.areas)
else:
current = self._current(readings)
calls: list[Any] = []
threshold = 1.0
if self.pack is not None:
wren = _wren()
targets = [
wren.Target(
**t, value=_unset(t) if current.get(p) is None else current[p]
)
for p, t in self.targets.items()
]
# One BLAS thread answers in about half the time for a model
# this small. Scoped to the call, so numpy elsewhere in the
# process keeps its threads.
with threadpool_limits(1):
calls, confidence = wren.act(command, house)
confidence = round(float(confidence), 3)
write = self.cfg.apply and confidence >= self.cfg.min_confidence
calls = wren.act(command, targets, model=self.pack)
threshold = self.pack["meta"].get("threshold", threshold)
# All of a command or none of it: applying a part leaves half of a
# duration ("off for 7 minutes": off now, back on later), or lets the
# assistant do again what was applied. `apply` only holds back writes:
# a reading is answered either way.
sure = all(
call.confidence >= self._bar(call.target, threshold)
and call.target not in self.untrained
and not self.cfg.targets.get(call.target, TargetInfo()).pass_on
# A reading nothing has written yet has no answer.
and (call.value is not None or current.get(call.target) is not None)
for call in calls
)
specs = {spec.port: spec for spec in [*self.input_ports, *self.output_ports]}
writes: dict[str, Any] = {}
actions: list[dict[str, Any]] = []
said: list[str] = []
answers: list[str] = []
for call in calls:
area = str(call.args.get("area", ""))
if (
call.tool == "sensors.temperature"
and area in asked
and confidence >= self.cfg.min_confidence
and (answer := self._temperature(area, outdoor, readings))
):
answers.append(answer)
area, _, kind = str(call.args.get("id", "")).partition(":")
ports = self.devices.get((area, kind), [])
if call.tool.split(".")[0] != _TOOLS.get(kind) or area not in asked:
ports = []
if call.tool in _LEVELS:
ports = []
if BRIGHTNESS in specs:
ports = [BRIGHTNESS]
call = self._leveled(call, specs[BRIGHTNESS].name)
hits = [
(specs[port], value)
for port in ports
if (value := _value(call, specs[port].dtype)) is not None
]
record = {
"message": "",
"value": None,
"applied": False,
"delay_s": call.delay_s,
"call": _text(call),
"confidence": confidence,
}
if not hits:
actions.append(record)
done = False
for spec, value in hits:
applied = write
spec, unit = specs[call.target], self.targets[call.target]["unit"]
applied = sure if call.value is None else sure and self.cfg.apply
if call.value is None:
value = current.get(call.target)
if applied:
words = call.target.replace("_", " ").capitalize()
answers.append(f"{words} is {_spoken(value, unit)}.")
else:
value = _typed(call.value, spec.dtype)
if applied and call.delay_s:
applied = self._pipeline is not None and self._pipeline.defer(
self, self._to_messages({spec.port: value}) or {}, call.delay_s
)
elif applied:
writes[spec.port] = value
actions.append(
record | {"message": spec.name, "value": value, "applied": applied}
)
done = done or applied
if done:
said.append(_said(call, area))
if group is not None and said:
said = self._group_said(group, command, house.areas)
if applied:
said.append(_said(call.target, value, unit, call.delay_s))
actions.append(
{
"message": spec.name,
"value": value,
"applied": applied,
"delay_s": call.delay_s,
"call": _text(call),
"confidence": round(float(call.confidence), 3),
}
)
wired = {spec.port for spec in self.output_ports}
if ACTIONS in wired:
@@ -632,77 +489,6 @@ class WrenNode(Node):
writes[REPLY] = CANT
return writes or None
def _leveled(self, call: Any, name: str) -> _Call:
"""A brightness call as the level it sets: "dim" is 30 below now."""
level = call.args.get("level")
if call.tool != "lights.set":
now = (
self._pipeline.state.get_present([name]).get(name)
if self._pipeline is not None
else None
)
if not isinstance(now, (int, float)) or isinstance(now, bool):
now = _USUAL
if call.tool == "lights.dim":
level = max(now - _STEP, min(now, _DIMMEST))
else:
level = min(now + _STEP, _BRIGHTEST)
elif isinstance(level, (int, float)):
level = min(max(level, 0), _BRIGHTEST)
return _Call("lights.set", {"level": level}, call.delay_s)
def _group(self, command: str, areas: Iterable[str]) -> list[_Call] | None:
"""The calls a command for a whole group means, one per wired device,
or None when it is not one: "turn off all lights" is every light.
Not the model's to answer: it makes one call of it, and "turn off all
lights" switched a living room light on at 0.98.
"""
action = re.search(rf"\b({'|'.join(_GROUP_TOOLS)})\b", command)
if not _GROUP.search(command) or _NOT_GROUP.search(command) or not action:
return None
tools = _GROUP_TOOLS[action[1]]
kinds = {_GROUP_KINDS[w] for w in command.split() if w in _GROUP_KINDS}
named = _named(areas, command)
delay = re.search(r"\bin (\d+) minutes?\b", command)
seconds = int(delay[1]) * 60.0 if delay else None
calls = [
_Call(tools[kind], {"id": f"{area}:{kind}"}, seconds)
for area, kind in self.devices
if kind in tools
and (not kinds or kind in kinds)
and (not named or area in named)
]
return calls or None
@staticmethod
def _group_said(
calls: list[_Call], command: str, areas: Iterable[str]
) -> list[str]:
"""A group command's reply: "all lights off", "lights off in the office"."""
said = list(dict.fromkeys(_GROUP_SAID[call.tool] for call in calls))
named = _named(areas, command)
if named:
said[-1] += " in the " + " and the ".join(named)
else:
said[0] = "all " + said[0]
if calls[0].delay_s:
minutes = round(calls[0].delay_s / 60)
said[-1] += f" in {minutes} minute{'s' * (minutes != 1)}"
return said
def _temperature(
self, area: str, outdoor: float | None, readings: dict[str, float | None]
) -> str | None:
""" "It's 8 degrees outside.", from the reading wired for the area."""
value = readings.get(self.readings.get(area, ""))
where = f"in the {area}"
if value is None and area in _OUTSIDE:
value, where = outdoor, "outside"
if not isinstance(value, (int, float)) or isinstance(value, bool):
return None
return f"It's {round(value)} degrees {where}."
def _passed_on(self) -> bool:
"""Whether a node reads ``unhandled``, such as an assistant."""
name = next((s.name for s in self.output_ports if s.port == UNHANDLED), "")