aboutsummaryrefslogtreecommitdiffstats
path: root/assistant.py
diff options
context:
space:
mode:
Diffstat (limited to 'assistant.py')
-rw-r--r--assistant.py40
1 files changed, 32 insertions, 8 deletions
diff --git a/assistant.py b/assistant.py
index 5c31167..fa8aee0 100644
--- a/assistant.py
+++ b/assistant.py
@@ -120,12 +120,23 @@ def load_tools(d):
t = tomllib.load(f)
if not isinstance(t.get("description"), str) or not t.get("command"):
raise SystemExit(f"{path}: needs description and command")
- if not isinstance(t.get("cloud", False), bool):
- raise SystemExit(f"{path}: cloud must be true or false")
+ c = t.get("cloud", False)
+ if not (isinstance(c, bool) or isinstance(c, list) and all(isinstance(x, str) for x in c)):
+ raise SystemExit(f"{path}: cloud must be true, false or a list of provider names")
tools[os.path.basename(path)[:-5]] = t
return tools
+def offered_tools(tools, provider):
+ # A cloud chat is offered only tools whose cloud is true or names its
+ # provider: the others would send mail, money or calendar to it. A list
+ # names trusted no-retention providers; one added later gets nothing.
+ if not provider:
+ return tools
+ return {n: t for n, t in tools.items()
+ if t.get("cloud") is True or isinstance(t.get("cloud"), list) and provider in t["cloud"]}
+
+
def schema(tools):
return [{"type": "function", "function": {
"name": name, "description": t["description"],
@@ -1163,11 +1174,9 @@ def answer(mine, text, files=(), chosen=None):
ctx_for(model)
free = (ctx["max"] - ctx["used"]) // 2 * 3 if ctx["max"] else 24000
blocks, refs, chips = attach(files, max(free, 2000))
- # A cloud chat is offered only the tools marked cloud = true: the others
- # would send mail, money or calendar to the provider. One of them called
- # anyway (copied from history) answers "no tool named".
- offered = ({n: t for n, t in TOOLS.items() if t.get("cloud") is True}
- if route(model)[3] else TOOLS)
+ # One not offered but called anyway (copied from history) answers
+ # "no tool named".
+ offered = offered_tools(TOOLS, route(model)[3])
offer = schema(offered) or None
if refs and not images:
raise Refused(f"{model} cannot see images")
@@ -1348,7 +1357,22 @@ def test():
load_tools(d)
assert False
except SystemExit as e:
- assert "cloud must be true or false" in str(e)
+ assert "cloud must be true, false or a list of provider names" in str(e)
+ with tempfile.TemporaryDirectory() as d:
+ with open(os.path.join(d, "x.toml"), "w") as f:
+ f.write('description = "d"\ncommand = ["date"]\ncloud = [1]\n')
+ try:
+ load_tools(d)
+ assert False
+ except SystemExit as e:
+ assert "list of provider names" in str(e)
+ with open(os.path.join(d, "x.toml"), "w") as f:
+ f.write('description = "d"\ncommand = ["date"]\ncloud = ["hetzner"]\n')
+ assert load_tools(d)["x"]["cloud"] == ["hetzner"]
+ tl = {"any": {"cloud": True}, "het": {"cloud": ["hetzner"]}, "local": {}, "no": {"cloud": False}}
+ assert offered_tools(tl, None) == tl
+ assert set(offered_tools(tl, "hetzner")) == {"any", "het"}
+ assert set(offered_tools(tl, "other")) == {"any"}
with tempfile.TemporaryDirectory() as d:
with open(os.path.join(d, "echo.sh"), "w") as f:
f.write('#!/bin/sh\nprintf "%s|" "$@"\n')