set_theme_tool.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Set the Hermes desktop app's accent colour, light/dark mode and layout, or show a picker card.
  2. The renderer applies the values and answers with what it applied, so the model reports what the user
  3. sees: an unknown layout preset, a session that is not on screen and an absent desktop window all come
  4. back as errors. Swatch ids and preset ids are validated again in the renderer, which also knows the
  5. user's own saved layouts.
  6. """
  7. import json
  8. import re
  9. from typing import Callable, Optional
  10. from tools.registry import registry, tool_error
  11. ACCENT_SWATCHES = ("mono", "green", "cyan", "nous", "violet", "pink", "red", "orange")
  12. # The theme's own accent; the renderer stores it as "no accent pick".
  13. THEME_ACCENT = "theme"
  14. MODES = ("light", "dark", "system")
  15. PICKERS = ("accent", "layout")
  16. # The bundled presets (apps/desktop/src/app/contrib/layout-presets.ts). Simple mode shows only the
  17. # first two; applying an Advanced preset switches the app to Advanced mode.
  18. SIMPLE_LAYOUTS = ("sidebar-left", "sidebar-right")
  19. ADVANCED_LAYOUTS = ("default", "basic", "focus", "terminal-deck", "quad")
  20. _HEX = re.compile(r"^#[0-9a-fA-F]{6}$")
  21. def _accent_error(accent: str) -> Optional[str]:
  22. if accent in ACCENT_SWATCHES or accent == THEME_ACCENT or _HEX.match(accent):
  23. return None
  24. return json.dumps({"success": False, "error": f"Unknown accent {accent!r}: use a swatch id, 'theme' or #rrggbb.",
  25. "available": [*ACCENT_SWATCHES, THEME_ACCENT]}, ensure_ascii=False)
  26. def set_theme_tool(accent: Optional[str] = None, mode: Optional[str] = None, layout: Optional[str] = None,
  27. picker: Optional[str] = None, callback: Optional[Callable[[dict], dict]] = None) -> str:
  28. """Apply the given appearance values, or show one picker card and wait for the pick."""
  29. if callback is None:
  30. return tool_error("set_theme is only available in the Hermes desktop app.")
  31. values = {key: value.strip() for key, value in (("accent", accent), ("mode", mode), ("layout", layout))
  32. if isinstance(value, str) and value.strip()}
  33. if picker is not None:
  34. if picker not in PICKERS:
  35. return tool_error(f"picker must be one of: {', '.join(PICKERS)}.")
  36. if values:
  37. return tool_error("picker shows a card for the user to choose; pass picker alone, without values.")
  38. return json.dumps(callback({"picker": picker}), ensure_ascii=False)
  39. if not values:
  40. return tool_error("Pass at least one of accent, mode or layout, or picker to let the user choose.")
  41. if "mode" in values and values["mode"] not in MODES:
  42. return json.dumps({"success": False, "error": f"Unknown mode {values['mode']!r}.", "available": list(MODES)},
  43. ensure_ascii=False)
  44. if "accent" in values and (error := _accent_error(values["accent"])):
  45. return error
  46. return json.dumps(callback(values), ensure_ascii=False)
  47. SET_THEME_SCHEMA = {
  48. "name": "set_theme",
  49. "description": (
  50. "Change how the Hermes desktop app looks: accent colour, light/dark mode, layout. Pass the values "
  51. "to apply them now; the result says what was applied. Pass picker instead to show the user an "
  52. "accent or layout picker card in the chat and wait for their pick. To reveal ONE pane, use "
  53. "focus_pane instead."
  54. ),
  55. "parameters": {
  56. "type": "object",
  57. "properties": {
  58. "accent": {
  59. "type": "string",
  60. "description": (f"A swatch id ({', '.join(ACCENT_SWATCHES)}), 'theme' for the theme's own "
  61. "accent, or a hex colour '#rrggbb'."),
  62. },
  63. "mode": {"type": "string", "enum": list(MODES)},
  64. "layout": {
  65. "type": "string",
  66. "description": (f"Layout preset id. Simple mode: {', '.join(SIMPLE_LAYOUTS)}. Advanced mode: "
  67. f"{', '.join(ADVANCED_LAYOUTS)}. The user's saved layouts by id."),
  68. },
  69. "picker": {
  70. "type": "string",
  71. "enum": list(PICKERS),
  72. "description": "Show that picker card and wait for the user (up to 5 minutes). Pass it alone.",
  73. },
  74. },
  75. },
  76. }
  77. registry.register(
  78. name="set_theme",
  79. toolset="desktop_ui",
  80. schema=SET_THEME_SCHEMA,
  81. handler=lambda args, **kw: set_theme_tool(
  82. callback=kw.get("callback"), **{k: args.get(k) for k in ("accent", "mode", "layout", "picker")}),
  83. emoji="🎨",
  84. )