Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,15 @@ async def get_group_info(ctx: RunContext[BotDependencies], group_jid: str = "")
return "Could not get group info"


bot_agent = _create_agent()
_bot_agent: Agent | None = None


def get_agent() -> Agent:
"""Get or create the global agent instance."""
global _bot_agent
if _bot_agent is None:
_bot_agent = _create_agent()
return _bot_agent


class AgenticAI:
Expand Down Expand Up @@ -422,7 +430,7 @@ async def process(self, msg: MessageHelper, bot: BotClient) -> str | None:
if image_data:
user_prompt_parts.append(BinaryContent(data=image_data, media_type="image/jpeg"))

result = await bot_agent.run(
result = await get_agent().run(
user_prompt_parts,
deps=deps,
model=model_str,
Expand Down
45 changes: 45 additions & 0 deletions commands/downloader/cancel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from core import symbols as sym
from core.command import Command, CommandContext
from core.downloader import downloader
from core.i18n import t
from core.permissions import check_admin_permission
from core.runtime_config import runtime_config


class CancelCommand(Command):
name = "cancel"
description = "Cancel your active download in this chat"
usage = "cancel [all]"
category = "downloader"

async def execute(self, ctx: CommandContext) -> None:
"""Cancel an active download."""
chat_jid = ctx.message.chat_jid
sender_jid = ctx.message.sender_jid

if ctx.args and ctx.args[0].lower() == "all":
is_owner = await runtime_config.is_owner_async(sender_jid, ctx.client)
is_admin = False
if ctx.message.is_group:
is_admin = await check_admin_permission(ctx.client, chat_jid, sender_jid)

if not (is_owner or is_admin):
await ctx.client.reply(ctx.message, t("errors.admin_required"))
return

count = downloader.cancel_all_in_chat(chat_jid)
if count > 0:
await ctx.client.reply(
ctx.message, f"{sym.SUCCESS} Cancelled {count} active download(s)."
)
else:
await ctx.client.reply(ctx.message, f"{sym.INFO} No active downloads in this chat.")
return

cancelled = downloader.cancel_download(chat_jid, sender_jid)
if cancelled:
await ctx.client.reply(ctx.message, f"{sym.SUCCESS} {t('downloader.cancelled')}")
else:
await ctx.client.reply(
ctx.message, f"{sym.INFO} You don't have any active downloads in this chat."
)
22 changes: 20 additions & 2 deletions commands/downloader/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

from core import symbols as sym
from core.command import Command, CommandContext
from core.downloader import DownloadError, FileTooLargeError, _format_size, downloader
from core.downloader import (
DownloadAbortedError,
DownloadError,
FileTooLargeError,
_format_size,
downloader,
)
from core.errors import report_error
from core.i18n import t, t_error

Expand Down Expand Up @@ -86,7 +92,12 @@ def _progress_hook(downloaded_bytes, total_bytes, speed, eta):
loop,
)

filepath = await self.download_func(url, progress_hook=_progress_hook)
filepath = await self.download_func(
url,
progress_hook=_progress_hook,
chat_jid=ctx.message.chat_jid,
sender_jid=ctx.message.sender_jid,
)

await ctx.client.edit_message(
ctx.message.chat_jid,
Expand Down Expand Up @@ -119,6 +130,13 @@ def _progress_hook(downloaded_bytes, total_bytes, speed, eta):
ctx.message,
t_error("downloader.too_large", size=f"{e.size_mb:.1f}", max=f"{e.max_mb:.0f}"),
)
except DownloadAbortedError:
await ctx.client.edit_message(
ctx.message.chat_jid,
progress_msg_id,
f"{header}{sym.INFO} {t('downloader.cancelled')}",
)
await ctx.client.send_reaction(ctx.message, "🚫")
except DownloadError as e:
await ctx.client.reply(ctx.message, t_error("downloader.failed", error=str(e)))
except Exception as e:
Expand Down
7 changes: 6 additions & 1 deletion commands/downloader/dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from core import symbols as sym
from core.command import Command, CommandContext
from core.downloader import DownloadError, FileTooLargeError, downloader
from core.downloader import DownloadAbortedError, DownloadError, FileTooLargeError, downloader
from core.i18n import t, t_error
from core.logger import log_info, log_warning
from core.pending_store import (
Expand Down Expand Up @@ -165,6 +165,8 @@ async def _auto_download(self, ctx: CommandContext, info, fmt) -> None:
fmt.format_id,
merge_audio=not fmt.has_audio,
is_audio=fmt.type == "audio",
chat_jid=ctx.message.chat_jid,
sender_jid=ctx.message.sender_jid,
)

media_type = "audio" if fmt.type == "audio" else "video"
Expand All @@ -188,6 +190,9 @@ async def _auto_download(self, ctx: CommandContext, info, fmt) -> None:
ctx.message,
t_error("downloader.too_large", size=f"{e.size_mb:.1f}", max=f"{e.max_mb:.0f}"),
)
except DownloadAbortedError:
await ctx.client.send_reaction(ctx.message, "🚫")
await ctx.client.reply(ctx.message, f"{sym.INFO} {t('downloader.cancelled')}")
except (DownloadError, Exception) as e:
await ctx.client.send_reaction(ctx.message, "❌")
await ctx.client.reply(ctx.message, t_error("downloader.failed", error=str(e)))
Expand Down
7 changes: 5 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
"action": "kick"
},
"downloader": {
"max_file_size_mb": 50
"max_file_size_mb": 180
},
"disabled_commands": []
"disabled_commands": [],
"dashboard": {
"enabled": false
}
}
11 changes: 11 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@
},
"description": "List of command names that are disabled",
"default": []
},
"dashboard": {
"type": "object",
"description": "Dashboard API and UI settings",
"properties": {
"enabled": {
"type": "boolean",
"description": "Enable the Dashboard API server",
"default": false
}
}
}
},
"required": [
Expand Down
Loading