|
| 1 | +import { formatErrorMessage, type AdvisorStatusSnapshot } from '@pymodel/pythinker-code-sdk'; |
| 2 | +import type { SlashCommandHost } from './dispatch'; |
| 3 | + |
| 4 | +const ADVISOR_STATUS_GLYPHS: Record<string, string> = { |
| 5 | + running: '●', |
| 6 | + paused: '○', |
| 7 | + no_model: '○', |
| 8 | + quota_exhausted: '✕', |
| 9 | + error: '✕', |
| 10 | +}; |
| 11 | + |
| 12 | +const ADVISOR_STATUS_LABELS: Record<string, string> = { |
| 13 | + running: 'running', |
| 14 | + paused: 'off', |
| 15 | + no_model: 'no model', |
| 16 | + quota_exhausted: 'quota exhausted', |
| 17 | + error: 'error', |
| 18 | +}; |
| 19 | + |
| 20 | +export async function handleAdvisorCommand(host: SlashCommandHost, args: string): Promise<void> { |
| 21 | + const parts = args.trim().split(/\s+/u).filter(Boolean); |
| 22 | + const verb = parts[0] ?? 'status'; |
| 23 | + const advisorId = parts[1]; |
| 24 | + if (parts.length > 2 || !['on', 'off', 'reload', 'status', 'toggle'].includes(verb)) { |
| 25 | + host.showError('Usage: /advisor [on|off|status|reload|toggle] [advisor-id]'); |
| 26 | + return; |
| 27 | + } |
| 28 | + if (host.session === undefined) { |
| 29 | + host.showError('No active session.'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (verb === 'status') { |
| 34 | + host.showNotice('Advisor status', formatAdvisorStatuses(await host.session.advisor.status())); |
| 35 | + return; |
| 36 | + } |
| 37 | + if (verb === 'reload') { |
| 38 | + await host.session.advisor.reload(); |
| 39 | + host.showStatus('Advisor configuration reloaded.'); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const statuses = await host.session.advisor.status(); |
| 44 | + if (advisorId !== undefined && !statuses.some((status) => status.id === advisorId)) { |
| 45 | + host.showError( |
| 46 | + `Unknown advisor: ${advisorId}. Run /advisor status to list configured advisors.`, |
| 47 | + ); |
| 48 | + return; |
| 49 | + } |
| 50 | + const enabled = |
| 51 | + verb === 'toggle' |
| 52 | + ? !( |
| 53 | + statuses.find((status) => |
| 54 | + advisorId === undefined ? true : status.id === advisorId, |
| 55 | + )?.enabled ?? false |
| 56 | + ) |
| 57 | + : verb === 'on'; |
| 58 | + let updatedStatuses: readonly AdvisorStatusSnapshot[]; |
| 59 | + try { |
| 60 | + updatedStatuses = await host.session.advisor.setEnabled(enabled, advisorId); |
| 61 | + } catch (error) { |
| 62 | + host.showError(formatErrorMessage(error)); |
| 63 | + return; |
| 64 | + } |
| 65 | + const target = advisorId === undefined ? 'Advisor' : `Advisor ${advisorId}`; |
| 66 | + const applied = |
| 67 | + advisorId === undefined |
| 68 | + ? updatedStatuses.length > 0 && |
| 69 | + updatedStatuses.every((status) => status.enabled === enabled) |
| 70 | + : updatedStatuses.find((status) => status.id === advisorId)?.enabled === enabled; |
| 71 | + if (!applied) { |
| 72 | + host.showError(`${target} remains ${enabled ? 'disabled' : 'enabled'}.`); |
| 73 | + return; |
| 74 | + } |
| 75 | + host.showStatus(`${target} ${enabled ? 'enabled' : 'disabled'}.`); |
| 76 | +} |
| 77 | + |
| 78 | +function formatAdvisorStatuses(statuses: readonly AdvisorStatusSnapshot[]): string { |
| 79 | + if (statuses.length === 0) return 'Advisor is disabled.'; |
| 80 | + return statuses |
| 81 | + .map((advisor) => { |
| 82 | + const glyph = ADVISOR_STATUS_GLYPHS[advisor.status] ?? '?'; |
| 83 | + const label = ADVISOR_STATUS_LABELS[advisor.status] ?? advisor.status; |
| 84 | + const model = advisor.model === undefined ? '' : `\n Model: ${advisor.model}`; |
| 85 | + const details = `\n ${advisor.notes} notes · $${advisor.costUsd.toFixed(4)} · ${advisor.failures} failures`; |
| 86 | + const message = advisor.message === undefined ? '' : `\n ${advisor.message}`; |
| 87 | + return `${glyph} ${advisor.name} [${label}]${advisor.enabled ? '' : ' (disabled)'}${model}${details}${message}`; |
| 88 | + }) |
| 89 | + .join('\n\n'); |
| 90 | +} |
0 commit comments