registerSession method

void registerSession({
  1. required String sessionId,
  2. required Map<Tool, ToolCallHandler> toolHandlers,
  3. Duration timeout = const Duration(seconds: 30),
})

Implementation

void registerSession({
  required String sessionId,
  required Map<Tool, ToolCallHandler> toolHandlers,
  Duration timeout = const Duration(seconds: 30),
}) {
  if (_sessions.containsKey(sessionId)) {
    throw AppleFoundationException(
      'Session already registered: $sessionId',
      code: 'SESSION_ALREADY_EXISTS',
    );
  }


  final toolNames = toolHandlers.keys.map((t) => t.name).toList();
  final uniqueNames = toolNames.toSet();
  if (toolNames.length != uniqueNames.length) {
    throw AppleFoundationException(
      'Duplicate tool names detected',
      code: 'DUPLICATE_TOOL_NAMES',
    );
  }

  final handlersByName = <String, ToolCallHandler>{};
  for (final entry in toolHandlers.entries) {
    handlersByName[entry.key.name] = entry.value;
  }

  _sessions[sessionId] = _SessionHandlers(
    handlers: handlersByName,
    createdAt: DateTime.now(),
    isActive: true,
    timeout: timeout,
  );

  developer.log(
    'Registered session $sessionId with ${toolHandlers.length} tools',
    name: 'ToolHandlerManager',
  );
}