style: auto ruff format

This commit is contained in:
banteg
2025-12-29 18:35:30 +04:00
parent a42692f935
commit b30e460c0e
8 changed files with 52 additions and 32 deletions
+2 -6
View File
@@ -11,8 +11,7 @@ class ConfigError(RuntimeError):
_EXAMPLE_CONFIG = (
'bot_token = "123456789:ABCdefGHIjklMNOpqrsTUVwxyz"\n'
"chat_id = 123456789\n"
'bot_token = "123456789:ABCdefGHIjklMNOpqrsTUVwxyz"\nchat_id = 123456789\n'
)
@@ -29,10 +28,7 @@ def _display_path(path: Path) -> str:
def _missing_config_message(primary: Path, alternate: Path | None = None) -> str:
example = "Example config:\n```\n" + _EXAMPLE_CONFIG + "```\n"
if alternate is None:
return (
f"Missing config file `{_display_path(primary)}`.\n"
f"{example}"
)
return f"Missing config file `{_display_path(primary)}`.\n{example}"
return (
"Missing takopi config.\n"
"Create one of these files:\n"
+3 -9
View File
@@ -41,9 +41,7 @@ def extract_session_id(text: str | None) -> str | None:
return found
def resolve_resume_session(
text: str | None, reply_text: str | None
) -> str | None:
def resolve_resume_session(text: str | None, reply_text: str | None) -> str | None:
return extract_session_id(text) or extract_session_id(reply_text)
@@ -124,9 +122,7 @@ def truncate_for_telegram(text: str, limit: int) -> str:
return (head + sep + tail)[:limit]
def prepare_telegram(
md: str, *, limit: int
) -> tuple[str, list[dict[str, Any]] | None]:
def prepare_telegram(md: str, *, limit: int) -> tuple[str, list[dict[str, Any]] | None]:
rendered, entities = render_markdown(md)
if len(rendered) > limit:
rendered = truncate_for_telegram(rendered, limit)
@@ -655,9 +651,7 @@ async def _run_main_loop(cfg: BridgeConfig) -> None:
r = msg.get("reply_to_message") or {}
resume_session = resolve_resume_session(text, r.get("text"))
await queue.put(
(msg["chat"]["id"], user_msg_id, text, resume_session)
)
await queue.put((msg["chat"]["id"], user_msg_id, text, resume_session))
finally:
await cfg.bot.close()
+31 -7
View File
@@ -130,15 +130,27 @@ def format_event(
exit_part = ""
else:
status = STATUS_FAIL if exit_code is not None else STATUS_DONE
exit_part = f" (exit {exit_code})" if exit_code is not None else ""
exit_part = (
f" (exit {exit_code})" if exit_code is not None else ""
)
line = prefix + f"{status} {command}{exit_part}"
return last_item, [line], line, prefix
case ("mcp_tool_call", "item.started"):
name = ".".join(part for part in (item["server"], item["tool"]) if part) or "tool"
name = (
".".join(
part for part in (item["server"], item["tool"]) if part
)
or "tool"
)
line = prefix + f"{STATUS_RUNNING} tool: {name}"
return last_item, [line], line, prefix
case ("mcp_tool_call", "item.completed"):
name = ".".join(part for part in (item["server"], item["tool"]) if part) or "tool"
name = (
".".join(
part for part in (item["server"], item["tool"]) if part
)
or "tool"
)
line = prefix + f"{STATUS_DONE} tool: {name}"
return last_item, [line], line, prefix
case ("web_search", "item.completed"):
@@ -146,12 +158,20 @@ def format_event(
line = prefix + f"{STATUS_DONE} searched: {query}"
return last_item, [line], line, prefix
case ("file_change", "item.completed"):
paths = [change["path"] for change in item["changes"] if change.get("path")]
paths = [
change["path"]
for change in item["changes"]
if change.get("path")
]
if not paths:
total = len(item["changes"])
desc = "updated files" if total == 0 else f"updated {total} files"
desc = (
"updated files" if total == 0 else f"updated {total} files"
)
elif len(paths) <= 3:
desc = "updated " + ", ".join(f"`{_shorten_path(p, MAX_PATH_LEN)}`" for p in paths)
desc = "updated " + ", ".join(
f"`{_shorten_path(p, MAX_PATH_LEN)}`" for p in paths
)
else:
desc = f"updated {len(paths)} files"
line = prefix + f"{STATUS_DONE} {desc}"
@@ -200,7 +220,11 @@ class ExecProgressRenderer:
return False
# Replace the preceding "running" line for the same item on completion.
if event["type"] == "item.completed" and progress_prefix and self.recent_actions:
if (
event["type"] == "item.completed"
and progress_prefix
and self.recent_actions
):
last = self.recent_actions[-1]
if last.startswith(progress_prefix + f"{STATUS_RUNNING} "):
self.recent_actions.pop()