All files / kernel-agents-repl/src/strategies/repl messages.ts

98.3% Statements 58/59
95% Branches 38/40
100% Functions 21/21
98.3% Lines 58/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313                                                              4x 14x   4x 9x                                   4x 4x 5x   1x   4x                                     32x                   21x       4x         30x 30x 30x     30x                           12x 12x                             10x 10x                   7x 7x                             32x 32x                     21x 21x 21x   1x   1x   19x                                 6x                 5x       4x   4x 4x 4x   4x 36x 36x 36x 36x         4x               30x                                                             38x 38x 6x 6x   38x 11x   38x 23x       25x     38x 38x                 15x         15x   15x 15x                              
import { stringify } from '@metamask/kernel-utils';
import { Message } from '@ocap/kernel-agents/types/messages';
import type { Transcript } from '@ocap/kernel-agents/types/messages';
import type { SyntaxNode } from 'tree-sitter';
 
import { makeCompartment } from './compartment.ts';
import { parse } from './parse/javascript.ts';
import { ERROR, RETURN } from './symbols.ts';
 
export type StatementType = 'import' | 'evaluation' | 'comment';
 
export type ReplMessageType = StatementType | 'interjection' | 'result';
 
export type ReplObservable = {
  toReplString(): string;
};
 
export type JsonObservable = {
  toJsonString(): string;
};
 
type JsonKey = string | number;
 
type Primitive = string | number | boolean | null | undefined;
 
type JsonObservation =
  | Primitive
  | JsonObservable
  | JsonObservation[]
  | { [key: JsonKey]: JsonObservation };
 
const isJsonObservable = (value: unknown): value is JsonObservable =>
  typeof value === 'object' && value !== null && 'toJsonString' in value;
 
export const observeJson = (value: JsonObservation): string =>
  isJsonObservable(value) ? value.toJsonString() : stringify(value);
 
/**
 * An abstract REPL message with JSON and REPL string serialization capabilities.
 */
export abstract class ReplMessage<
    Type extends ReplMessageType,
    Body extends Record<JsonKey, JsonObservation | SyntaxNode>,
  >
  extends Message<Type, Body>
  implements ReplObservable, JsonObservable
{
  /**
   * Serializes the message to a JSON-formatted string.
   *
   * @returns The JSON string representation of the message.
   */
  toJsonString(): string {
    const messageType = `"messageType": "${this.messageType}"`;
    const bodyEntries = Object.entries(this.messageBody)
      .filter(([, value]) => isJsonObservable(value))
      .map(
        ([key, value]) => `"${key}": ${observeJson(value as JsonObservation)}`,
      );
    return `{ ${messageType}, ${bodyEntries.join(', ')} }`;
  }
 
  abstract toReplString(): string;
}
 
// Statements comprise the action space of the REPL agent.
/**
 * A message representing a statement in the REPL action space.
 */
export class StatementMessage<
  Type extends StatementType = StatementType,
> extends ReplMessage<Type, { code: string; node: SyntaxNode }> {
  /**
   * Serializes the statement to a REPL-formatted string with prompt prefix.
   *
   * @returns The REPL string representation of the statement.
   */
  toReplString(): string {
    return `> ${this.messageBody.code}`;
  }
 
  /**
   * Creates a statement message from code by parsing it and determining its type.
   *
   * @param code - The code string to parse into a statement message.
   * @returns A statement message of the appropriate type.
   */
  static fromCode(code: string): StatementMessage {
    return statementMessageFromCode(code);
  }
}
 
const parseStatement = (
  code: string,
  name?: string,
  bound?: StatementType[],
): SyntaxNode => {
  const { rootNode } = parse(code);
  const [statement] = rootNode.children as [SyntaxNode];
  Iif (bound && !bound.includes(statement.type as StatementType)) {
    throw new Error(`"${code}" is not a valid ${name}.`);
  }
  return statement;
};
 
/**
 * A message representing a comment statement.
 */
export class CommentMessage extends StatementMessage<'comment'> {
  /**
   * Constructs a new {@link CommentMessage}.
   *
   * @param code - The comment code string.
   * @param statement - Optional pre-parsed syntax node; if not provided, the code will be parsed.
   */
  constructor(code: string, statement?: SyntaxNode) {
    const node = statement ?? parseStatement(code, 'comment');
    super('comment', { code, node });
  }
}
 
/**
 * A message representing an import statement.
 */
export class ImportMessage extends StatementMessage<'import'> {
  /**
   * Constructs a new {@link ImportMessage}.
   *
   * @param code - The import statement code string.
   * @param statement - Optional pre-parsed syntax node; if not provided, the code will be parsed.
   */
  constructor(code: string, statement?: SyntaxNode) {
    const node = statement ?? parseStatement(code, 'import_statement');
    super('import', { code, node });
  }
 
  /**
   * Creates an import message from a list of named imports from the abilities module.
   *
   * @param names - The names to import from the abilities module.
   * @returns An import message for the specified names.
   */
  static fromNames(names: string[]): ImportMessage {
    const code = `import { ${names.join(', ')} } from "@ocap/abilities";`;
    return new ImportMessage(code);
  }
}
 
/**
 * A message representing an evaluation statement to be executed.
 */
export class EvaluationMessage extends StatementMessage<'evaluation'> {
  /**
   * Constructs a new {@link EvaluationMessage}.
   *
   * @param code - The code to evaluate.
   * @param statement - Optional pre-parsed syntax node; if not provided, the code will be parsed.
   */
  constructor(code: string, statement?: SyntaxNode) {
    const node = statement ?? parseStatement(code, 'expression_statement');
    super('evaluation', { code, node });
  }
}
 
/**
 * Make a statement message from code.
 *
 * @param code - The code to parse.
 * @returns A statement message.
 */
function statementMessageFromCode(code: string): StatementMessage {
  const { rootNode } = parse(code);
  const [statement] = rootNode.children as [SyntaxNode];
  switch (statement.type) {
    case 'comment':
      return new CommentMessage(code, statement);
    case 'import_statement':
      return new ImportMessage(code, statement);
    default: // XXX Maybe too permissive as the default case.
      return new EvaluationMessage(code, statement);
  }
}
 
/**
 * A message representing an interjection in the REPL session.
 */
export class InterjectionMessage extends ReplMessage<
  'interjection',
  { interjection: string }
> {
  /**
   * Constructs a new {@link InterjectionMessage}.
   *
   * @param interjection - The interjection text to display.
   */
  constructor(interjection: string) {
    super('interjection', { interjection });
  }
 
  /**
   * Serializes the interjection to a REPL-formatted string with exclamation prefix.
   *
   * @returns The REPL string representation of the interjection.
   */
  toReplString(): string {
    return `! ${this.messageBody.interjection}`;
  }
}
 
const $stringify = harden(stringify);
 
export const MAX_LINES = 20;
const HEAD_LENGTH = 14;
const ELLIPSIS = '// ...';
 
const hardenEntry = ([key, value]: [string, unknown]): [string, string] => {
  const hardValue = harden(value);
  const compartment = makeCompartment({ hardValue, $stringify });
  const stringified = compartment.evaluate('$stringify(hardValue);') as string;
  return [key, stringified];
};
 
type ResultMessageBody = { value?: string; error?: string; return?: string };
 
const compressLines = (
  lines: string[],
  {
    maxLines = MAX_LINES,
    headLength = HEAD_LENGTH,
    ellipsis = ELLIPSIS,
  }: { maxLines?: number; headLength?: number; ellipsis?: string } = {},
): string[] =>
  lines.length > maxLines
    ? [
        ...lines.slice(0, headLength),
        ellipsis,
        ...lines.slice(-(maxLines - headLength - 1)),
      ]
    : lines;
 
type ResultArg = {
  value?: Record<string, unknown>;
  [ERROR]?: unknown;
  [RETURN]?: unknown;
};
 
/**
 * A message representing the result of evaluating a statement.
 */
export class ResultMessage extends ReplMessage<'result', ResultMessageBody> {
  readonly #compress: boolean;
 
  /**
   * Constructs a new {@link ResultMessage}.
   *
   * @param result - The result object containing optional value, error, or return properties.
   * @param options - Configuration options for the result message.
   * @param options.compress - Whether to compress long output by truncating lines; defaults to true.
   */
  constructor(
    result: ResultArg,
    { compress = true }: { compress?: boolean } = {},
  ) {
    const messageBody: ResultMessageBody = {};
    if (Object.hasOwn(result, ERROR)) {
      const error = result[ERROR] as Error;
      messageBody.error = `${error.name}: ${error.message}`;
    }
    if (Object.hasOwn(result, RETURN)) {
      messageBody.return = hardenEntry(['', result[RETURN]])[1];
    }
    if (Object.hasOwn(result, 'value')) {
      messageBody.value = Object.entries(
        result.value as Record<string, unknown>,
      )
        .map(hardenEntry)
        .map(([key, val]) => `${key}: ${val}`)
        .join('\n');
    }
    super('result', messageBody);
    this.#compress = compress;
  }
 
  /**
   * Serializes the result to a REPL-formatted string, optionally compressing long output.
   *
   * @returns The REPL string representation of the result.
   */
  toReplString(): string {
    const lines = {
      error: this.messageBody.error?.split('\n') ?? [],
      return: this.messageBody.return?.split('\n') ?? [],
      value: this.messageBody.value?.split('\n') ?? [],
    };
    const transform = this.#compress
      ? compressLines
      : (value: string[]) => value;
    return [
      ...transform(lines.error),
      ...transform(lines.return),
      ...transform(lines.value),
    ].join('\n');
  }
}
 
export type ReplTranscript = Transcript<ReplMessageType, ReplObservable>;
 
export type Observation = InterjectionMessage | ResultMessage;
 
export type Action = StatementMessage;
 
export type State = (Observation | Action)[];