<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';

import { IKernel, IKernelInfo, IKernelMessage } from './ikernel';

import * as utils from './utils';


/**
 * Contents of an 'inspect_request' message.
 */
export
interface IInspectOptions {
  code: string;
  cursor_pos: number;
  detail_level: number;
}


/**
 * Contents of an 'inspect_reply' message.
 */
export
interface IInspectResult {
  status: string;
  data: any;
  metadata: any;
}


/**
 * Contents of an 'execute_request' message.
 */
export
interface IExecuteOptions {
  code: string;
  silent: boolean;
  store_history: boolean;
  user_expressions: any;
  allow_stdin: boolean;
  stop_on_error: boolean;
}


/**
 * Contents of an 'execute_reply' message.
 */
export
interface IExecuteResult {
  execution_count: number;
  data: any;
  metadata: any;
}


/**
 * Send a "kernel_info_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info
 */
export
function infoRequest(kernel: IKernel): Promise<IKernelInfo> {
  var msg = createMessage(kernel, 'kernel_info_request', 'shell');
  var future = kernel.sendMessage(msg);
  return new Promise<IKernelInfo>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<IKernelInfo>msg.content);
    }
  });
}


/**
 * Send an "inspect_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#introspection
 */
export
function inspectRequest(kernel: IKernel, options: IInspectOptions): Promise<IInspectResult> {
  var msg = createMessage(kernel, 'inspect_request', 'shell', options);
  var future = kernel.sendMessage(msg);
  return new Promise<IInspectResult>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<IInspectResult>msg.content);
    }
  });
}

/**
 * Send an "execute_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#execute
 */
export
function executeRequest(kernel: IKernel, options: IExecuteOptions): Promise<IExecuteResult> {
  var msg = createMessage(kernel, 'execute_request', 'shell', options);
  var future = kernel.sendMessage(msg);
  return new Promise<IExecuteResult>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<IExecuteResult>msg.content);
    }
  });
}


/**
 * Create a well-formed Kernel Message.
 */
function createMessage(kernel: IKernel, msgType: string, channel: string, content: any = {}, metadata: any = {}, buffers: ArrayBuffer[] = []) : IKernelMessage {
  return {
    header: {
      username: kernel.username,
      version: '5.0',
      session: kernel.clientId,
      msg_id: utils.uuid(),
      msg_type: msgType
    },
    parent_header: { },
    channel: channel,
    content: content,
    metadata: metadata,
    buffers: buffers
  }
}
<commit_msg>Round out the kernel helpers<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';

import { 
  IKernel, IKernelFuture, IKernelInfo, IKernelMessage 
} from './ikernel';

import * as utils from './utils';


/**
 * Contents of a 'complete_request' message.
 */
export
interface ICompleteRequest {
  code: string;
  cursor_pos: string;
}


/**
 * Contents of a 'complete_reply' message.
 */
export 
interface ICompleteReply {
  matches: string[];
  cursor_start: number;
  cursor_end: number;
  metadata: any;
  status: string;
}


/**
 * Contents of an 'inspect_request' message.
 */
export
interface IInspectRequest {
  code: string;
  cursor_pos: number;
  detail_level: number;
}


/**
 * Contents of an 'inspect_reply' message.
 */
export
interface IInspectReply {
  status: string;
  data: any;
  metadata: any;
}


/**
 * Content of an 'is_complete_reply' message.
 */
export 
interface IIsCompleteReply {
  status: string;
  indent: string;
}


/**
 * Contents of an 'execute_request' message.
 */
export
interface IExecuteRequest {
  code: string;
  silent: boolean;
  store_history: boolean;
  user_expressions: any;
  allow_stdin: boolean;
  stop_on_error: boolean;
}


/**
 * Contents of an 'execute_reply' message.
 */
export
interface IExecuteReply {
  execution_count: number;
  data: any;
  metadata: any;
}


/**
 * Send a "kernel_info_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info
 */
export
function infoRequest(kernel: IKernel): Promise<IKernelInfo> {
  var msg = createMessage(kernel, 'kernel_info_request', 'shell');
  var future = kernel.sendMessage(msg);
  return new Promise<IKernelInfo>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<IKernelInfo>msg.content);
    }
  });
}


/**
 * Send an "complete_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#completion
 */
export
function completeRequest(kernel: IKernel, options: ICompleteRequest): Promise<ICompleteReply> {
  var msg = createMessage(kernel, 'complete_request', 'shell', options);
  var future = kernel.sendMessage(msg);
  return new Promise<ICompleteReply>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<ICompleteReply>msg.content);
    }
  });
}


/**
 * Send an "inspect_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#introspection
 */
export
function inspectRequest(kernel: IKernel, options: IInspectRequest): Promise<IInspectReply> {
  var msg = createMessage(kernel, 'inspect_request', 'shell', options);
  var future = kernel.sendMessage(msg);
  return new Promise<IInspectReply>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<IInspectReply>msg.content);
    }
  });
}

/**
 * Send an "execute_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#execute
 */
export
function executeRequest(kernel: IKernel, options: IExecuteRequest): IKernelFuture {
  var msg = createMessage(kernel, 'execute_request', 'shell', options);
  return kernel.sendMessage(msg);
}


/**
 * Send an "is_complete_request" message.
 *
 * See https://ipython.org/ipython-doc/dev/development/messaging.html#code-completeness
 */
export
function isCompleteRequest(kernel: IKernel, code: string): Promise<IIsCompleteReply> {
  var content = { code: code };
  var msg = createMessage(kernel, 'is_complete_request', 'shell', content);
  var future = kernel.sendMessage(msg);
  return new Promise<IIsCompleteReply>((resolve, reject) => {
    future.onReply = (msg: IKernelMessage) => {
      resolve(<IIsCompleteReply>msg.content);
    }
  });
}


/**
 * Create a well-formed Kernel Message.
 */
function createMessage(kernel: IKernel, msgType: string, channel: string, content: any = {}, metadata: any = {}, buffers: ArrayBuffer[] = []) : IKernelMessage {
  return {
    header: {
      username: kernel.username,
      version: '5.0',
      session: kernel.clientId,
      msg_id: utils.uuid(),
      msg_type: msgType
    },
    parent_header: { },
    channel: channel,
    content: content,
    metadata: metadata,
    buffers: buffers
  }
}

{'fraction_non_alphanumeric': 0.13504106919114575, 'fraction_numerical': 0.0005568703884170959, 'mean_word_length': 4.062720225510923, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 38, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '194733', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2263, 'n_words': 536}