// src/mastra/types.ts
function mergeVersionOverrides(base, overrides) {
  if (!base) return overrides;
  if (!overrides) return base;
  return {
    ...base,
    ...overrides,
    agents: {
      ...base.agents,
      ...overrides.agents
    }
  };
}

// src/request-context/index.ts
var MASTRA_RESOURCE_ID_KEY = "mastra__resourceId";
var MASTRA_THREAD_ID_KEY = "mastra__threadId";
var MASTRA_VERSIONS_KEY = "mastra__versions";
var RequestContext = class {
  registry = /* @__PURE__ */ new Map();
  constructor(iterable) {
    if (iterable && typeof iterable === "object" && typeof iterable[Symbol.iterator] !== "function") {
      this.registry = new Map(Object.entries(iterable));
    } else {
      this.registry = new Map(iterable);
    }
  }
  /**
   * set a value with strict typing if `Values` is a Record and the key exists in it.
   */
  set(key, value) {
    this.registry.set(key, value);
  }
  /**
   * Get a value with its type
   */
  get(key) {
    return this.registry.get(key);
  }
  /**
   * Check if a key exists in the container
   */
  has(key) {
    return this.registry.has(key);
  }
  /**
   * Delete a value by key
   */
  delete(key) {
    return this.registry.delete(key);
  }
  /**
   * Clear all values from the container
   */
  clear() {
    this.registry.clear();
  }
  /**
   * Get all keys in the container
   */
  keys() {
    return this.registry.keys();
  }
  /**
   * Get all values in the container
   */
  values() {
    return this.registry.values();
  }
  /**
   * Get all entries in the container.
   * Returns a discriminated union of tuples for proper type narrowing when iterating.
   */
  entries() {
    return this.registry.entries();
  }
  /**
   * Get the size of the container
   */
  size() {
    return this.registry.size;
  }
  /**
   * Execute a function for each entry in the container.
   * The callback receives properly typed key-value pairs.
   */
  forEach(callbackfn) {
    this.registry.forEach(callbackfn);
  }
  /**
   * Custom JSON serialization method.
   * Converts the internal Map to a plain object for proper JSON serialization.
   * Non-serializable values (e.g., RPC proxies, functions, circular references)
   * are skipped to prevent serialization errors when storing to database.
   */
  toJSON() {
    const result = {};
    for (const [key, value] of this.registry.entries()) {
      if (this.isSerializable(value)) {
        result[key] = value;
      }
    }
    return result;
  }
  /**
   * Check if a value can be safely serialized to JSON.
   */
  isSerializable(value) {
    if (value === null || value === void 0) return true;
    if (typeof value === "function") return false;
    if (typeof value === "symbol") return false;
    if (typeof value !== "object") return true;
    try {
      JSON.stringify(value);
      return true;
    } catch {
      return false;
    }
  }
  /**
   * Get all values as a typed object for destructuring.
   * Returns Record<string, any> when untyped, or the Values type when typed.
   *
   * @example
   * ```typescript
   * const ctx = new RequestContext<{ userId: string; apiKey: string }>();
   * ctx.set('userId', 'user-123');
   * ctx.set('apiKey', 'key-456');
   * const { userId, apiKey } = ctx.all;
   * ```
   */
  get all() {
    return Object.fromEntries(this.registry);
  }
};

export { MASTRA_RESOURCE_ID_KEY, MASTRA_THREAD_ID_KEY, MASTRA_VERSIONS_KEY, RequestContext, mergeVersionOverrides };
//# sourceMappingURL=chunk-AJVVIFTH.js.map
//# sourceMappingURL=chunk-AJVVIFTH.js.map