// @bun
import {
  cleanStackTrace
} from "./chunk-m4c14gcg.js";

// ../../node_modules/.bun/llmz@0.0.79+ed0f774122d294fe/node_modules/llmz/dist/chunk-WSVDMGMR.js
var errorClasses = {};
function registerErrorClass(name, errorClass) {
  errorClasses[name] = errorClass;
}
var tryParseMessage = (str) => {
  try {
    return JSON.parse(str);
  } catch {
    return str;
  }
};
var Signals;
((Signals2) => {
  function isWrappedError(error) {
    var _a, _b;
    const isAlreadyWrapped = error.name === "Error" && ((_a = error == null ? undefined : error.message) == null ? undefined : _a.startsWith("{")) && ((_b = error == null ? undefined : error.message) == null ? undefined : _b.endsWith("}"));
    return isAlreadyWrapped;
  }
  Signals2.isWrappedError = isWrappedError;
  function serializeError(error) {
    if (isWrappedError(error)) {
      const msg = tryParseMessage(error.message);
      return JSON.stringify({
        ...typeof msg === "object" ? msg : { message: msg },
        properties: { ...error }
      });
    }
    return JSON.stringify({
      name: error.constructor.name,
      message: error.message,
      stack: cleanStackTrace(error.stack ?? ""),
      properties: { ...error }
    });
  }
  Signals2.serializeError = serializeError;
  function maybeDeserializeError(error) {
    const errorIsAlreadyDeserialized = error instanceof Error && error.name in errorClasses;
    if (errorIsAlreadyDeserialized) {
      return error;
    }
    const serializedError = error instanceof Error ? error.message : typeof error === "string" ? error : (error == null ? undefined : error.toString()) ?? "";
    try {
      const parsed = JSON.parse(serializedError);
      if (parsed && parsed.name && parsed.message) {
        const { name, message, properties = {} } = parsed;
        const ErrorClass = errorClasses[name] || Error;
        const errorInstance = new ErrorClass(message);
        errorInstance.message = message;
        errorInstance.name = name;
        errorInstance.stack = cleanStackTrace((error == null ? undefined : error.stack) ?? "");
        Object.assign(errorInstance, properties);
        if (isWrappedError(errorInstance)) {
          return maybeDeserializeError(errorInstance);
        }
        return errorInstance;
      }
    } catch {}
    return error;
  }
  Signals2.maybeDeserializeError = maybeDeserializeError;
})(Signals || (Signals = {}));
var VMSignal = class extends Error {
  constructor(message) {
    super(message);
    this.message = message;
    this.message = Signals.serializeError(this);
  }
  truncatedCode = "";
  toolCall;
  variables = {};
};
var SnapshotSignal = class extends VMSignal {
  constructor(message) {
    super(message);
    this.message = Signals.serializeError(this);
  }
};
var VMLoopSignal = class extends VMSignal {
  constructor(message) {
    super(message);
    this.message = Signals.serializeError(this);
  }
};
var ThinkSignal = class extends VMLoopSignal {
  constructor(reason, context, metadata) {
    super("Think signal received: " + reason);
    this.reason = reason;
    this.context = context;
    this.metadata = metadata;
    this.message = Signals.serializeError(this);
    this.stack = "";
  }
  toString() {
    return Signals.serializeError(this);
  }
};
var CodeExecutionError = class extends Error {
  constructor(message, code, stacktrace) {
    super(message);
    this.code = code;
    this.stacktrace = stacktrace;
    this.message = Signals.serializeError(this);
  }
};
var InvalidCodeError = class extends Error {
  constructor(message, code) {
    super(message);
    this.code = code;
    this.message = Signals.serializeError(this);
    this.stack = "";
  }
};
var LoopExceededError = class extends Error {
  constructor() {
    super("Loop exceeded error");
    this.message = Signals.serializeError(this);
    this.stack = "";
  }
};
var CodeFormattingError = class extends Error {
  constructor(message, code) {
    super(message, {
      cause: "Code formatting error"
    });
    this.code = code;
    this.stack = "";
  }
};
var AssignmentError = class extends Error {
  constructor(message) {
    super(message);
    this.message = Signals.serializeError(this);
  }
};
var CognitiveError = class extends Error {
  constructor(message) {
    super(message);
    this.message = Signals.serializeError(this);
    this.stack = "";
  }
};
registerErrorClass("VMSignal", VMSignal);
registerErrorClass("SnapshotSignal", SnapshotSignal);
registerErrorClass("VMLoopSignal", VMLoopSignal);
registerErrorClass("ThinkSignal", ThinkSignal);
registerErrorClass("CodeExecutionError", CodeExecutionError);
registerErrorClass("AssignmentError", AssignmentError);
registerErrorClass("CognitiveError", CognitiveError);

export { Signals, VMSignal, SnapshotSignal, ThinkSignal, CodeExecutionError, InvalidCodeError, LoopExceededError, CodeFormattingError, AssignmentError, CognitiveError };
