// Codex follow-up #4: validate `sessionId` is a non-empty string instead of
// casting it blindly. Malformed waiting_human output must not yield a typed
// waiting-human result carrying `browserSessionId: undefined`.
export const parseInteractiveBrowseWaitingHuman = (
  toolName: string,
  resultStr: string,
): { waitReason: string; sessionId: string } | null => {
  if (toolName !== 'InteractiveBrowse') return null;
  try {
    const parsed = JSON.parse(resultStr) as Record<string, unknown>;
    const sessionId = parsed['sessionId'];
    if (
      parsed['action'] === 'waiting_human' &&
      typeof parsed['waitReason'] === 'string' &&
      typeof sessionId === 'string' &&
      sessionId.length > 0
    ) {
      return {
        waitReason: parsed['waitReason'] as string,
        sessionId,
      };
    }
  } catch {
    // not JSON
  }
  return null;
};
