|
| 1 | +import { Context, InlineKeyboard } from "grammy"; |
| 2 | +import { |
| 3 | + clearActiveInlineMenu, |
| 4 | + ensureActiveInlineMenu, |
| 5 | + replyWithInlineMenu, |
| 6 | +} from "../handlers/inline-menu.js"; |
| 7 | +import { showAgentSelectionMenu } from "../handlers/agent.js"; |
| 8 | +import { showModelSelectionMenu } from "../handlers/model.js"; |
| 9 | +import { showVariantSelectionMenu } from "../handlers/variant.js"; |
| 10 | +import { handleContextButtonPress } from "../handlers/context.js"; |
| 11 | +import { logger } from "../../utils/logger.js"; |
| 12 | +import { t } from "../../i18n/index.js"; |
| 13 | + |
| 14 | +export async function settingsCommand(ctx: Context): Promise<void> { |
| 15 | + const keyboard = new InlineKeyboard() |
| 16 | + .text(t("settings.button.agent"), "settings:agent") |
| 17 | + .row() |
| 18 | + .text(t("settings.button.model"), "settings:model") |
| 19 | + .row() |
| 20 | + .text(t("settings.button.variant"), "settings:variant") |
| 21 | + .row() |
| 22 | + .text(t("settings.button.context"), "settings:context"); |
| 23 | + |
| 24 | + await replyWithInlineMenu(ctx, { |
| 25 | + menuKind: "settings", |
| 26 | + text: t("settings.select"), |
| 27 | + keyboard, |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +export async function handleSettingsSelect(ctx: Context): Promise<boolean> { |
| 32 | + const callbackQuery = ctx.callbackQuery; |
| 33 | + if (!callbackQuery?.data || !callbackQuery.data.startsWith("settings:")) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + const isActiveMenu = await ensureActiveInlineMenu(ctx, "settings"); |
| 38 | + if (!isActiveMenu) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + const action = callbackQuery.data.slice("settings:".length); |
| 43 | + |
| 44 | + try { |
| 45 | + switch (action) { |
| 46 | + case "agent": |
| 47 | + clearActiveInlineMenu("settings_selected"); |
| 48 | + await ctx.deleteMessage().catch(() => {}); |
| 49 | + await ctx.answerCallbackQuery().catch(() => {}); |
| 50 | + await showAgentSelectionMenu(ctx); |
| 51 | + break; |
| 52 | + case "model": |
| 53 | + clearActiveInlineMenu("settings_selected"); |
| 54 | + await ctx.deleteMessage().catch(() => {}); |
| 55 | + await ctx.answerCallbackQuery().catch(() => {}); |
| 56 | + await showModelSelectionMenu(ctx); |
| 57 | + break; |
| 58 | + case "variant": |
| 59 | + clearActiveInlineMenu("settings_selected"); |
| 60 | + await ctx.deleteMessage().catch(() => {}); |
| 61 | + await ctx.answerCallbackQuery().catch(() => {}); |
| 62 | + await showVariantSelectionMenu(ctx); |
| 63 | + break; |
| 64 | + case "context": |
| 65 | + clearActiveInlineMenu("settings_selected"); |
| 66 | + await ctx.deleteMessage().catch(() => {}); |
| 67 | + await ctx.answerCallbackQuery().catch(() => {}); |
| 68 | + await handleContextButtonPress(ctx); |
| 69 | + break; |
| 70 | + default: |
| 71 | + await ctx.answerCallbackQuery({ text: t("callback.unknown_command") }); |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + return true; |
| 76 | + } catch (err) { |
| 77 | + clearActiveInlineMenu("settings_error"); |
| 78 | + logger.error("[Settings] Error handling settings select:", err); |
| 79 | + await ctx.answerCallbackQuery({ text: t("callback.processing_error") }).catch(() => {}); |
| 80 | + return false; |
| 81 | + } |
| 82 | +} |
0 commit comments