All files / src/entities/context handlers.ts

100% Statements 33/33
100% Branches 11/11
100% Functions 7/7
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127            22x                                   22x 22x                         22x 4x 4x           22x 2x 2x           22x 3x 3x           22x 4x 4x           22x 1x 1x           22x 100x           22x                         119x   4x   2x   3x   4x   1x   1x   100x   1x   1x   1x   1x            
/**
 * Handlers for manage_context tool actions
 *
 * Each handler corresponds to an action in ManageContextSchema.
 */
 
import { getContextManager } from './context-manager';
import {
  ListPresetsInput,
  ManageContextInput,
  ResetContextInput,
  SetScopeInput,
  ShowContextInput,
  SwitchPresetInput,
  WhoamiInput,
} from './schema';
import {
  PresetInfo,
  ResetResult,
  SessionContext,
  SetScopeResult,
  SwitchResult,
  WhoamiResult,
} from './types';
import { executeWhoami } from './whoami';
import {
  AccountChangeResult,
  AccountLinkResult,
  handleLinkAccount,
  handleListAccounts,
  handleSwitchAccount,
  handleUnlinkAccount,
} from './accounts';
import type { ChatAccount } from '../../accounts/chat-accounts';
 
/**
 * Handle show action - return current context
 */
export async function handleShowContext(_input: ShowContextInput): Promise<SessionContext> {
  const manager = getContextManager();
  return manager.getContext();
}
 
/**
 * Handle list_presets action - return available presets
 */
export async function handleListPresets(_input: ListPresetsInput): Promise<PresetInfo[]> {
  const manager = getContextManager();
  return manager.listPresets();
}
 
/**
 * Handle switch_preset action - change active preset
 */
export async function handleSwitchPreset(input: SwitchPresetInput): Promise<SwitchResult> {
  const manager = getContextManager();
  return manager.switchPreset(input.preset);
}
 
/**
 * Handle set_scope action - set namespace scope with auto-detection
 */
export async function handleSetScope(input: SetScopeInput): Promise<SetScopeResult> {
  const manager = getContextManager();
  return manager.setScope(input.namespace, input.includeSubgroups);
}
 
/**
 * Handle reset action - restore initial context
 */
export async function handleResetContext(_input: ResetContextInput): Promise<ResetResult> {
  const manager = getContextManager();
  return manager.reset();
}
 
/**
 * Handle whoami action - return authentication status and capabilities
 */
export async function handleWhoami(_input: WhoamiInput): Promise<WhoamiResult> {
  return executeWhoami();
}
 
/**
 * Main handler dispatcher for manage_context tool
 */
export async function handleManageContext(
  input: ManageContextInput,
): Promise<
  | SessionContext
  | PresetInfo[]
  | SwitchResult
  | SetScopeResult
  | ResetResult
  | WhoamiResult
  | ChatAccount[]
  | AccountChangeResult
  | AccountLinkResult
> {
  switch (input.action) {
    case 'show':
      return handleShowContext(input);
    case 'list_presets':
      return handleListPresets(input);
    case 'switch_preset':
      return handleSwitchPreset(input);
    case 'set_scope':
      return handleSetScope(input);
    case 'clear_scope':
      return getContextManager().clearScope();
    case 'reset':
      return handleResetContext(input);
    case 'whoami':
      return handleWhoami(input);
    case 'list_accounts':
      return handleListAccounts();
    case 'switch_account':
      return handleSwitchAccount(input);
    case 'link_account':
      return handleLinkAccount(input);
    case 'unlink_account':
      return handleUnlinkAccount(input);
    /* istanbul ignore next -- unreachable with Zod discriminatedUnion */
    default:
      throw new Error(`Unknown action: ${(input as { action: string }).action}`);
  }
}