Add Brave web search support
This commit is contained in:
@@ -2,12 +2,14 @@ from __future__ import annotations
|
||||
|
||||
import html
|
||||
import re
|
||||
import shlex
|
||||
from textwrap import wrap
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from pyrogram import enums
|
||||
|
||||
from app.ai import AIInvocation, parse_ai_invocation
|
||||
from app.ai.web_search import format_results
|
||||
from app.commands.core import CommandRegistry, normalize_command_name
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -322,6 +324,67 @@ async def echo(ctx: "CommandContext") -> None:
|
||||
await ctx.reply(text)
|
||||
|
||||
|
||||
def _parse_search_args(text: str) -> dict[str, Any]:
|
||||
try:
|
||||
tokens = shlex.split(text)
|
||||
except ValueError:
|
||||
tokens = text.split()
|
||||
|
||||
arguments: dict[str, Any] = {}
|
||||
query_parts: list[str] = []
|
||||
index = 0
|
||||
while index < len(tokens):
|
||||
token = tokens[index]
|
||||
if token == "--count":
|
||||
index += 1
|
||||
if index >= len(tokens):
|
||||
raise ValueError("Usage: .search / ..search [--count N] [--freshness pd|pw|pm|py|YYYY-MM-DDtoYYYY-MM-DD] <query>")
|
||||
arguments["count"] = tokens[index]
|
||||
elif token.startswith("--count="):
|
||||
arguments["count"] = token.split("=", 1)[1]
|
||||
elif token == "--freshness":
|
||||
index += 1
|
||||
if index >= len(tokens):
|
||||
raise ValueError("Usage: .search / ..search [--count N] [--freshness pd|pw|pm|py|YYYY-MM-DDtoYYYY-MM-DD] <query>")
|
||||
arguments["freshness"] = tokens[index]
|
||||
elif token.startswith("--freshness="):
|
||||
arguments["freshness"] = token.split("=", 1)[1]
|
||||
else:
|
||||
query_parts.append(token)
|
||||
index += 1
|
||||
|
||||
query = " ".join(query_parts).strip()
|
||||
if not query:
|
||||
raise ValueError("Usage: .search / ..search [--count N] [--freshness pd|pw|pm|py|YYYY-MM-DDtoYYYY-MM-DD] <query>")
|
||||
arguments["query"] = query
|
||||
return arguments
|
||||
|
||||
|
||||
@command_registry.command(
|
||||
"search",
|
||||
"Search the web with Brave Search",
|
||||
usage=".search / ..search [--count N] [--freshness pd|pw|pm|py|YYYY-MM-DDtoYYYY-MM-DD] <query>",
|
||||
category="ai",
|
||||
).alias("web").shared()
|
||||
async def search(ctx: "CommandContext") -> None:
|
||||
ai_service = ctx.manager.ai_service
|
||||
if ai_service is None:
|
||||
await ctx.reply("Brave web search is not configured. Configure ai.web_search and BRAVE_API_KEY.")
|
||||
return
|
||||
if not ai_service.web_search.available:
|
||||
await ctx.reply("Brave web search is not configured. Set BRAVE_API_KEY.")
|
||||
return
|
||||
|
||||
try:
|
||||
arguments = _parse_search_args(ctx.args)
|
||||
response = await ai_service.web_search.search(arguments)
|
||||
except ValueError as exc:
|
||||
await ctx.reply(str(exc))
|
||||
return
|
||||
|
||||
await _reply_long(ctx, format_results(response, include_heading=True))
|
||||
|
||||
|
||||
@command_registry.command(
|
||||
"ai",
|
||||
"Ask a configurable AI model",
|
||||
|
||||
Reference in New Issue
Block a user