All files / kernel-errors/src/errors SampleGenerationError.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8

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                                                                      9x         9x           134x           1x                               4x 4x     4x             134x  
import {
  assert,
  lazy,
  literal,
  object,
  optional,
  string,
  union,
} from '@metamask/superstruct';
 
import { BaseError } from '../BaseError.ts';
import {
  marshaledErrorSchema,
  ErrorCode,
  MarshaledErrorStruct,
} from '../constants.ts';
import type { ErrorOptionsWithStack, MarshaledOcapError } from '../types.ts';
 
/**
 * An error indicating that the LLM generated invalid response.
 * This error should trigger resampling from the LLM.
 */
export class SampleGenerationError extends BaseError {
  /**
   * Creates a new SampleGenerationError.
   *
   * @param sample - The invalid sample text generated by the LLM.
   * @param cause - The underlying error that caused sample generation to fail.
   * @param options - Additional error options including stack.
   * @param options.data - Additional data about the error.
   * @param options.data.sample - The invalid sample text generated by the LLM.
   * @param options.cause - The underlying error that caused sample generation to fail.
   * @param options.stack - The stack trace of the error.
   */
  constructor(sample: string, cause: Error, options?: ErrorOptionsWithStack) {
    super(ErrorCode.SampleGenerationError, 'LLM generated invalid response.', {
      ...options,
      cause,
      data: { sample },
    });
    harden(this);
  }
 
  /**
   * A superstruct struct for validating marshaled {@link SampleGenerationError} instances.
   */
  public static struct = object({
    ...marshaledErrorSchema,
    code: literal(ErrorCode.SampleGenerationError),
    data: object({
      sample: string(),
    }),
    cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
  });
 
  /**
   * Unmarshals a {@link MarshaledError} into a {@link SampleGenerationError}.
   *
   * @param marshaledError - The marshaled error to unmarshal.
   * @param unmarshalErrorOptions - The function to unmarshal the error options.
   * @returns The unmarshaled error.
   */
  public static unmarshal(
    marshaledError: MarshaledOcapError,
    unmarshalErrorOptions: (
      marshaledError: MarshaledOcapError,
    ) => ErrorOptionsWithStack,
  ): SampleGenerationError {
    assert(marshaledError, this.struct);
    const cause = marshaledError.cause
      ? (unmarshalErrorOptions(marshaledError).cause as Error)
      : new Error('Unknown cause');
    return new SampleGenerationError(
      marshaledError.data.sample,
      cause,
      unmarshalErrorOptions(marshaledError),
    );
  }
}
harden(SampleGenerationError);