Refine AI image context handling

This commit is contained in:
Codex
2026-05-14 18:34:40 +02:00
parent 2a731600e0
commit 86f7827802
10 changed files with 623 additions and 59 deletions

View File

@@ -285,7 +285,7 @@ async def echo(ctx: "CommandContext") -> None:
@command_registry.command(
"ai",
"Ask a configurable AI model",
usage=".ai / ..ai [N] [--model] prompt",
usage=".ai / ..ai [N] [--img] [--model] prompt",
category="ai",
).shared()
async def ai(ctx: "CommandContext") -> None:
@@ -299,20 +299,55 @@ async def ai(ctx: "CommandContext") -> None:
ctx.args,
default_context_messages=ctx.manager.config.ai.context_messages,
)
except ValueError as exc:
await ctx.reply(str(exc))
return
try:
selected_alias = ai_service.select_model_alias(invocation.model_flags)
except ValueError as exc:
await ctx.reply(str(exc))
return
status = await ctx.reply(
"\n".join(
[
"⏳ AI request started",
f"model: {selected_alias}",
f"context: {invocation.context_messages} message(s)",
"tools: understand_image"
if invocation.use_image_understanding
else "tools: none",
f"prompt: {invocation.prompt[:200]}{'...' if len(invocation.prompt) > 200 else ''}",
]
)
)
async def _progress(text: str) -> None:
try:
await status.edit_text(text)
except Exception:
pass
try:
response = await ai_service.generate(
ctx,
AIInvocation(
context_messages=invocation.context_messages,
model_flags=[selected_alias],
use_image_understanding=invocation.use_image_understanding,
prompt=invocation.prompt,
),
progress=_progress,
)
except ValueError as exc:
await ctx.reply(str(exc))
return
except RuntimeError as exc:
await ctx.reply(str(exc))
except (RuntimeError, ValueError) as exc:
await _progress(f"❌ {exc}")
return
final_text = f"✅\n{response.text}"
if len(final_text) <= 3900:
await _progress(final_text)
return
await _progress("✅ Response ready")
await _reply_long(ctx, response.text)