import { NativeEventEmitter, DeviceEventEmitter, Platform } from 'react-native';
import RNLlama from './NativeRNLlama';
import { SchemaGrammarConverter, convertJsonSchemaToGrammar } from './grammar';
export { SchemaGrammarConverter, convertJsonSchemaToGrammar };
const EVENT_ON_TOKEN = '@RNLlama_onToken';
let EventEmitter;
if (Platform.OS === 'ios') {
  // @ts-ignore
  EventEmitter = new NativeEventEmitter(RNLlama);
}
if (Platform.OS === 'android') {
  EventEmitter = DeviceEventEmitter;
}
export class LlamaContext {
  gpu = false;
  reasonNoGPU = '';
  constructor(_ref) {
    let {
      contextId,
      gpu,
      reasonNoGPU
    } = _ref;
    this.id = contextId;
    this.gpu = gpu;
    this.reasonNoGPU = reasonNoGPU;
  }

  /**
   * Load cached prompt & completion state from a file.
   */
  async loadSession(filepath) {
    return RNLlama.loadSession(this.id, filepath);
  }

  /**
   * Save current cached prompt & completion state to a file.
   */
  async saveSession(filepath, options) {
    return RNLlama.saveSession(this.id, filepath, (options === null || options === void 0 ? void 0 : options.tokenSize) || -1);
  }
  async completion(params, callback) {
    let tokenListener = callback && EventEmitter.addListener(EVENT_ON_TOKEN, evt => {
      const {
        contextId,
        tokenResult
      } = evt;
      if (contextId !== this.id) return;
      callback(tokenResult);
    });
    const promise = RNLlama.completion(this.id, {
      ...params,
      emit_partial_completion: !!callback
    });
    return promise.then(completionResult => {
      var _tokenListener;
      (_tokenListener = tokenListener) === null || _tokenListener === void 0 ? void 0 : _tokenListener.remove();
      tokenListener = null;
      return completionResult;
    }).catch(err => {
      var _tokenListener2;
      (_tokenListener2 = tokenListener) === null || _tokenListener2 === void 0 ? void 0 : _tokenListener2.remove();
      tokenListener = null;
      throw err;
    });
  }
  stopCompletion() {
    return RNLlama.stopCompletion(this.id);
  }
  tokenize(text) {
    return RNLlama.tokenize(this.id, text);
  }
  detokenize(tokens) {
    return RNLlama.detokenize(this.id, tokens);
  }
  embedding(text) {
    return RNLlama.embedding(this.id, text);
  }
  async bench(pp, tg, pl, nr) {
    const result = await RNLlama.bench(this.id, pp, tg, pl, nr);
    const [modelDesc, modelSize, modelNParams, ppAvg, ppStd, tgAvg, tgStd] = JSON.parse(result);
    return {
      modelDesc,
      modelSize,
      modelNParams,
      ppAvg,
      ppStd,
      tgAvg,
      tgStd
    };
  }
  async release() {
    return RNLlama.releaseContext(this.id);
  }
}
export async function setContextLimit(limit) {
  return RNLlama.setContextLimit(limit);
}
export async function initLlama(_ref2) {
  let {
    model,
    is_model_asset: isModelAsset,
    ...rest
  } = _ref2;
  let path = model;
  if (path.startsWith('file://')) path = path.slice(7);
  const {
    contextId,
    gpu,
    reasonNoGPU
  } = await RNLlama.initContext({
    model: path,
    is_model_asset: !!isModelAsset,
    ...rest
  });
  return new LlamaContext({
    contextId,
    gpu,
    reasonNoGPU
  });
}
export async function releaseAllLlama() {
  return RNLlama.releaseAllContexts();
}
//# sourceMappingURL=index.js.map