aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-17 11:53:41 +0100
committerDanilo M. <danix@danix.xyz>2026-11-17 11:53:41 +0100
commit784b5ce41b84e13245555350352973155b97cc3e (patch)
treebec1477af62bfdddd7b4c446ee98667b78907d53
parent50c295794af5eed5d84c12f27cd894134951e486 (diff)
downloaddesktop-assistant-784b5ce41b84e13245555350352973155b97cc3e.tar.gz
desktop-assistant-784b5ce41b84e13245555350352973155b97cc3e.zip
feat: out_dir setting for tool and canvas output
One output folder for the canvas and tools, in config.toml. Empty resolves to Assistant/ in the XDG documents folder, the rule the canvas used to apply itself. Tools get it as $ASSISTANT_DIR, the canvas from hello, and --defaults prints the resolved folder for the settings page's placeholder. A relative path stops the daemon. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--assistant.py57
-rw-r--r--examples/config.toml2
2 files changed, 53 insertions, 6 deletions
diff --git a/assistant.py b/assistant.py
index a7f1e71..69ed34a 100644
--- a/assistant.py
+++ b/assistant.py
@@ -61,6 +61,7 @@ DEFAULTS = {
"tool_rounds": 3,
"temperature": None, # None: the llama-server preset's
"thinking": True, # False skips the model's reasoning
+ "out_dir": "", # "": the documents folder's Assistant/
"cloud": [], # [[cloud]] providers, see check_cloud
"env": {},
}
@@ -105,12 +106,35 @@ def merge(base, over, where=""):
return out
+def out_dir(v):
+ # Where tools and the canvas write files, resolved once at startup.
+ # Empty: Assistant/ in the documents folder; xdg-user-dir answers $HOME
+ # itself when none is set, which would litter it.
+ if not isinstance(v, str):
+ raise SystemExit("config: out_dir must be a string")
+ if v:
+ v = os.path.expanduser(v)
+ if not os.path.isabs(v):
+ raise SystemExit("config: out_dir must be an absolute path")
+ return v
+ home = os.path.expanduser("~")
+ try:
+ d = subprocess.run(["xdg-user-dir", "DOCUMENTS"], capture_output=True, text=True,
+ timeout=5).stdout.strip()
+ except (OSError, subprocess.TimeoutExpired):
+ d = ""
+ if not os.path.isabs(d) or os.path.normpath(d) == os.path.normpath(home):
+ d = os.path.join(home, "Documents")
+ return os.path.join(d, "Assistant")
+
+
def load_config(path):
try:
with open(path, "rb") as f:
- return merge(DEFAULTS, tomllib.load(f))
+ c = merge(DEFAULTS, tomllib.load(f))
except FileNotFoundError:
- return DEFAULTS
+ c = DEFAULTS
+ return {**c, "out_dir": out_dir(c["out_dir"])}
def load_tools(d):
@@ -493,7 +517,8 @@ def emit(event):
def hello():
return {"type": "hello", **now_state, "model": ctx["model"], "ctx_used": ctx["used"],
"ctx_max": ctx["max"], "id": cur["id"], "turns": cur["turns"],
- "pending": cur["pending"], "chat_model": cur["model"]}
+ "pending": cur["pending"], "chat_model": cur["model"],
+ "out_dir": CFG["out_dir"]}
def drop(c):
@@ -733,7 +758,8 @@ def run_tool(tools, name, arguments, d=TOOLS_DIR):
argv = [re.sub(r"\{(\w+)\}", lambda m: vals.get(m.group(1), ""), a) for a in t["command"]]
if "/" not in argv[0] and os.path.exists(os.path.join(d, argv[0])):
argv[0] = os.path.join(d, argv[0])
- env = {**os.environ, **{k: str(v) for k, v in CFG["env"].items()}}
+ env = {**os.environ, "ASSISTANT_DIR": CFG["out_dir"],
+ **{k: str(v) for k, v in CFG["env"].items()}}
try:
# Own process group: a timeout kills the script's children too (curl),
# which would otherwise keep the pipes open and block us.
@@ -1331,6 +1357,22 @@ def test():
import io
import tempfile
+ home = os.path.expanduser("~")
+ assert out_dir("~/x") == os.path.join(home, "x") and out_dir("/a/b") == "/a/b"
+ default = out_dir("")
+ assert os.path.isabs(default) and default.endswith("/Assistant") and default != home
+ for bad in ("rel/dir", 1):
+ try:
+ out_dir(bad)
+ assert False, bad
+ except SystemExit as e:
+ assert "config: out_dir" in str(e)
+ with tempfile.TemporaryDirectory() as d:
+ with open(os.path.join(d, "c.toml"), "w") as f:
+ f.write('out_dir = "/srv/x"\n')
+ assert load_config(os.path.join(d, "c.toml"))["out_dir"] == "/srv/x"
+ assert load_config(os.path.join(d, "none.toml"))["out_dir"] == default
+ assert DEFAULTS["out_dir"] == ""
c = merge(DEFAULTS, {"tool_rounds": 1, "env": {"K": "v"}})
assert c["tool_rounds"] == 1 and c["env"] == {"K": "v"}
assert DEFAULTS["tool_rounds"] == 3 # defaults untouched
@@ -1398,6 +1440,9 @@ def test():
t0 = time.time()
assert run_tool(tools, "slow", "{}", d) == "error: timed out"
assert time.time() - t0 < 2 # the grandchild sleep was killed too
+ tools["dir"] = {"command": ["sh", "-c", "echo $ASSISTANT_DIR"]}
+ assert run_tool(tools, "dir", "{}", d) == CFG["out_dir"]
+
assert run_tool(tools, "fail", "{}", d) == "error: oops"
assert run_tool(tools, "gone", "{}", d).startswith("error: cannot run")
assert run_tool(tools, "nope", "{}", d) == "error: no tool named nope"
@@ -1709,7 +1754,7 @@ def test():
h = json.loads(f.readline())
assert h["type"] == "hello" and h["state"] == "idle" and h["turns"] == []
assert h["pending"] is None and "ctx_max" in h
- assert h["chat_model"] is None
+ assert h["chat_model"] is None and h["out_dir"] == CFG["out_dir"]
# Thinking, a tool round, then the answer.
script["replies"] = [
[{"reasoning_content": "hmm"},
@@ -2041,7 +2086,7 @@ if __name__ == "__main__":
if sys.argv[1:] == ["--test"]:
test()
elif sys.argv[1:] == ["--defaults"]:
- print(json.dumps(DEFAULTS))
+ print(json.dumps({**DEFAULTS, "out_dir": out_dir("")})) # the placeholder
elif len(sys.argv) == 3 and sys.argv[1] == "--cloud-models":
print(json.dumps(cloud_models(sys.argv[2])))
else:
diff --git a/examples/config.toml b/examples/config.toml
index 20a2901..3ba5a21 100644
--- a/examples/config.toml
+++ b/examples/config.toml
@@ -8,6 +8,8 @@
# tool_rounds = 3 # tool round trips per question
# temperature = 0.6 # example; the default is unset: the model preset's
# thinking = true # false skips the model's reasoning (faster first word)
+# out_dir = "" # where tools and the canvas write files; "": Assistant/
+# # in the documents folder. Tools see it as $ASSISTANT_DIR
# Added to every tool's environment. Personal values live here, not in
# the tool files.