<commit_before>import fetch from 'isomorphic-fetch';

const frameworkName = 'SINGULARITY_TAILER';

/* GENERIC CHUNK ACTIONS */

export const ADD_FILE_CHUNK = `${frameworkName}_ADD_FILE_CHUNK`;
export const addFileChunk = (id, chunk) => ({
  type: ADD_FILE_CHUNK,
  id,
  chunk
});

export const SET_FILE_SIZE = `${frameworkName}_SET_FILE_SIZE`;
export const setFileSize = (id, fileSize) => ({
  type: SET_FILE_SIZE,
  id,
  fileSize
});

/* GENERAL API HELPERS */
const checkStatus = (response) => {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  const error = new Error(response.statusText);
  error.response = response;
  throw error;
};

const parseJSON = (response) => {
  return response.json();
};

/* SINGULARITY SANDBOX API */

// must be used before calling a fetch
// this sets the Singularity API root
export const SANDBOX_SET_API_ROOT = `${frameworkName}_SANDBOX_SET_API_ROOT`;
export const sandboxSetApiRoot = (apiRoot) => ({
  type: SANDBOX_SET_API_ROOT,
  apiRoot
});


const SANDBOX_FETCH_CHUNK = `${frameworkName}_SANDBOX_FETCH_CHUNK`;

export const SANDBOX_FETCH_CHUNK_STARTED = `${SANDBOX_FETCH_CHUNK}_STARTED`;
export const SANDBOX_FETCH_CHUNK_ERROR = `${SANDBOX_FETCH_CHUNK}_ERROR`;

export const sandboxFetchChunk = (id, start, end) => {
  // meh, I kinda want to keep it generalized to one string at the top level,
  // but I'm not sure this is the right way to do that.
  // `id` is the taskId and path
  const taskId = id.split('/')[0];
  const path = id.split('/').slice(1).join('/');
  return (dispatch, getState) => {
    dispatch({
      type: SANDBOX_FETCH_CHUNK_STARTED,
      startedAt: Date.now(),
      id,
      start,
      end
    });

    const apiRoot = getState().tailer.config.singularityApiRoot;
    const query = `?path=${path}&offset=${start}&length=${end - start}`;
    const apiPath = `${apiRoot}/sandbox/${taskId}/read${query}`;

    return fetch(apiPath, {credentials: 'include'})
      .then(checkStatus)
      .then(parseJSON)
      .then(({data, nextOffset, offset}) => {
        return dispatch(addFileChunk(id, {
          text: data,
          start: offset,
          end: nextOffset || end,
          byteLength: nextOffset ? (nextOffset - offset) : (end - offset)
        }));
      }).catch((error) => {
        return dispatch({
          type: SANDBOX_FETCH_CHUNK_ERROR,
          name: error.name,
          message: error.message
        });
      });
  };
};

const SANDBOX_FETCH_LENGTH = `${frameworkName}_SANDBOX_FETCH_LENGTH`;

export const SANDBOX_FETCH_LENGTH_STARTED = `${SANDBOX_FETCH_LENGTH}_STARTED`;
export const SANDBOX_FETCH_LENGTH_ERROR = `${SANDBOX_FETCH_LENGTH}_ERROR`;

export const sandboxGetLength = (id) => {
  // `id` is the taskId and path
  const taskId = id.split('/')[0];
  const path = id.split('/').slice(1).join('/');
  return (dispatch, getState) => {
    dispatch({
      type: SANDBOX_FETCH_LENGTH_STARTED,
      startedAt: Date.now(),
      id
    });

    const apiRoot = getState().tailer.config.singularityApiRoot;
    console.warn('this is broken');
    const query = `?path=${path}&length=${0}`;
    const apiPath = `${apiRoot}/sandbox/${taskId}/read${query}`;

    return fetch(apiPath, {credentials: 'include'})
      .then(checkStatus)
      .then(parseJSON)
      .then(({offset}) => {
        return dispatch(setFileSize(id, offset));
      }).catch((error) => {
        return dispatch({
          type: SANDBOX_FETCH_LENGTH_ERROR,
          name: error.name,
          message: error.message
        });
      });
  };
};
<commit_msg>Add warning about the action creator getState()<commit_after>import fetch from 'isomorphic-fetch';

const frameworkName = 'SINGULARITY_TAILER';

/* GENERIC CHUNK ACTIONS */

export const ADD_FILE_CHUNK = `${frameworkName}_ADD_FILE_CHUNK`;
export const addFileChunk = (id, chunk) => ({
  type: ADD_FILE_CHUNK,
  id,
  chunk
});

export const SET_FILE_SIZE = `${frameworkName}_SET_FILE_SIZE`;
export const setFileSize = (id, fileSize) => ({
  type: SET_FILE_SIZE,
  id,
  fileSize
});

/* GENERAL API HELPERS */
const checkStatus = (response) => {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  const error = new Error(response.statusText);
  error.response = response;
  throw error;
};

const parseJSON = (response) => {
  return response.json();
};

/* SINGULARITY SANDBOX API */

// must be used before calling a fetch
// this sets the Singularity API root
export const SANDBOX_SET_API_ROOT = `${frameworkName}_SANDBOX_SET_API_ROOT`;
export const sandboxSetApiRoot = (apiRoot) => ({
  type: SANDBOX_SET_API_ROOT,
  apiRoot
});


const SANDBOX_FETCH_CHUNK = `${frameworkName}_SANDBOX_FETCH_CHUNK`;

export const SANDBOX_FETCH_CHUNK_STARTED = `${SANDBOX_FETCH_CHUNK}_STARTED`;
export const SANDBOX_FETCH_CHUNK_ERROR = `${SANDBOX_FETCH_CHUNK}_ERROR`;

export const sandboxFetchChunk = (id, start, end) => {
  // meh, I kinda want to keep it generalized to one string at the top level,
  // but I'm not sure this is the right way to do that.
  // `id` is the taskId and path
  const taskId = id.split('/')[0];
  const path = id.split('/').slice(1).join('/');
  return (dispatch, getState) => {
    dispatch({
      type: SANDBOX_FETCH_CHUNK_STARTED,
      startedAt: Date.now(),
      id,
      start,
      end
    });
    console.warn('this getState() won\'t work outside the example, plsfix');
    const apiRoot = getState().tailer.config.singularityApiRoot;
    const query = `?path=${path}&offset=${start}&length=${end - start}`;
    const apiPath = `${apiRoot}/sandbox/${taskId}/read${query}`;

    return fetch(apiPath, {credentials: 'include'})
      .then(checkStatus)
      .then(parseJSON)
      .then(({data, nextOffset, offset}) => {
        return dispatch(addFileChunk(id, {
          text: data,
          start: offset,
          end: nextOffset || end,
          byteLength: nextOffset ? (nextOffset - offset) : (end - offset)
        }));
      }).catch((error) => {
        return dispatch({
          type: SANDBOX_FETCH_CHUNK_ERROR,
          name: error.name,
          message: error.message
        });
      });
  };
};

const SANDBOX_FETCH_LENGTH = `${frameworkName}_SANDBOX_FETCH_LENGTH`;

export const SANDBOX_FETCH_LENGTH_STARTED = `${SANDBOX_FETCH_LENGTH}_STARTED`;
export const SANDBOX_FETCH_LENGTH_ERROR = `${SANDBOX_FETCH_LENGTH}_ERROR`;

export const sandboxGetLength = (id) => {
  // `id` is the taskId and path
  const taskId = id.split('/')[0];
  const path = id.split('/').slice(1).join('/');
  return (dispatch, getState) => {
    dispatch({
      type: SANDBOX_FETCH_LENGTH_STARTED,
      startedAt: Date.now(),
      id
    });

    const apiRoot = getState().tailer.config.singularityApiRoot;
    console.warn('this is broken');
    const query = `?path=${path}&length=${0}`;
    const apiPath = `${apiRoot}/sandbox/${taskId}/read${query}`;

    return fetch(apiPath, {credentials: 'include'})
      .then(checkStatus)
      .then(parseJSON)
      .then(({offset}) => {
        return dispatch(setFileSize(id, offset));
      }).catch((error) => {
        return dispatch({
          type: SANDBOX_FETCH_LENGTH_ERROR,
          name: error.name,
          message: error.message
        });
      });
  };
};

{'fraction_non_alphanumeric': 0.14330947136563876, 'fraction_numerical': 0.0030286343612334803, 'mean_word_length': 3.3712394705174487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3221379', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2430, 'n_words': 546}