// src/transfer/types.ts
import { z } from "zod";
var ExportManifestV1Schema = z.object({
  format: z.literal("openclaw-engram-export"),
  schemaVersion: z.literal(1),
  createdAt: z.string(),
  pluginVersion: z.string(),
  includesTranscripts: z.boolean(),
  files: z.array(
    z.object({
      path: z.string(),
      sha256: z.string(),
      bytes: z.number().int().nonnegative()
    })
  )
});
var ExportMemoryRecordV1Schema = z.object({
  path: z.string(),
  content: z.string()
});
var ExportBundleV1Schema = z.object({
  manifest: ExportManifestV1Schema,
  records: z.array(ExportMemoryRecordV1Schema)
});
var CAPSULE_ID_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9]|-(?!-))*[A-Za-z0-9]$|^[A-Za-z0-9]$/;
var CapsuleIdSchema = z.string().min(1, "capsule.id must not be empty").max(64, "capsule.id must be 64 characters or fewer").regex(
  CAPSULE_ID_PATTERN,
  "capsule.id must be alphanumeric with single dashes (no spaces, no leading/trailing dashes)"
);
var SemverLikeSchema = z.string().min(1, "capsule.version must not be empty").regex(
  /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/,
  "capsule.version must be a semver-like string (e.g. 1.0.0)"
);
var CapsuleRetrievalPolicySchema = z.object({
  /**
   * Per-tier weight overrides applied during recall when the capsule is the
   * active scope. Keys are tier names (e.g. `bm25`, `vector`, `graph`); values
   * are non-negative finite multipliers. The set of valid keys is left open
   * for now so future tiers do not break older manifests.
   */
  tierWeights: z.record(
    z.string().min(1),
    z.number().finite().nonnegative()
  ),
  /**
   * Whether the direct-answer fast path is allowed when this capsule is
   * active. Operators may disable it for capsules whose contents should
   * always flow through the full retrieval pipeline.
   */
  directAnswerEnabled: z.boolean()
});
var CapsuleIncludesSchema = z.object({
  taxonomy: z.boolean(),
  identityAnchors: z.boolean(),
  peerProfiles: z.boolean(),
  procedural: z.boolean()
});
var CapsuleParentSchema = z.object({
  capsuleId: CapsuleIdSchema,
  version: SemverLikeSchema,
  /**
   * Posix-relative path prefix under the fork's memory root where the parent
   * capsule's records were placed. Typically `forks/<parent-capsule-id>`.
   * Must be a non-empty string with no leading slash.
   */
  forkRoot: z.string().min(1, "capsule.parent.forkRoot must not be empty").refine((v) => !v.startsWith("/"), {
    message: "capsule.parent.forkRoot must be a relative path (no leading slash)"
  })
});
var CapsuleBlockSchema = z.object({
  id: CapsuleIdSchema,
  version: SemverLikeSchema,
  /**
   * Taxonomy schema version the capsule was authored against. Free-form for
   * now; later PRs may tighten this to a known taxonomy registry.
   */
  schemaVersion: z.string().min(1, "capsule.schemaVersion must not be empty"),
  /**
   * Optional reference to the parent capsule this one was forked or derived
   * from. `null` (not `undefined`) is the explicit "no parent" sentinel so
   * that round-trips through JSON do not silently drop the field.
   *
   * @deprecated Prefer {@link parent} (structured). This field is preserved
   *   for backward compatibility with V2 manifests written before PR 4/6.
   */
  parentCapsule: z.string().min(1).nullable(),
  /**
   * Structured fork-lineage pointer added in PR 4/6. When present, this
   * capsule is a fork of the described parent. `null` is the explicit
   * "not a fork" sentinel — round-trips through JSON cleanly without
   * silently dropping the field.
   *
   * Defaults to `null` so that V2 manifests written before PR 4/6 (which
   * omit this field) still parse without error. This preserves backward
   * compatibility with the existing `makeV2Manifest()` test fixtures and
   * any in-the-wild V2 archives produced by PR 2/6 and PR 3/6.
   *
   * Invariant: when `parent` is non-null, `parentCapsule` SHOULD equal
   * `parent.capsuleId` for backward compatibility with readers that only
   * understand the legacy field. `forkCapsule()` sets both.
   */
  parent: CapsuleParentSchema.nullable().default(null),
  description: z.string(),
  retrievalPolicy: CapsuleRetrievalPolicySchema,
  includes: CapsuleIncludesSchema
});
var ExportManifestV2Schema = z.object({
  format: z.literal("openclaw-engram-export"),
  schemaVersion: z.literal(2),
  createdAt: z.string(),
  pluginVersion: z.string(),
  includesTranscripts: z.boolean(),
  files: z.array(
    z.object({
      path: z.string(),
      sha256: z.string(),
      bytes: z.number().int().nonnegative()
    })
  ),
  capsule: CapsuleBlockSchema
});
var ExportBundleV2Schema = z.object({
  manifest: ExportManifestV2Schema,
  records: z.array(ExportMemoryRecordV1Schema)
});
function parseExportManifest(input) {
  const version = typeof input === "object" && input !== null ? input.schemaVersion : void 0;
  if (version === 2) {
    const manifest = ExportManifestV2Schema.parse(input);
    return {
      capsuleVersion: 2,
      manifest,
      capsule: manifest.capsule
    };
  }
  if (version === 1) {
    const manifest = ExportManifestV1Schema.parse(input);
    return { capsuleVersion: 1, manifest, capsule: null };
  }
  const v1 = ExportManifestV1Schema.safeParse(input);
  if (v1.success) {
    return { capsuleVersion: 1, manifest: v1.data, capsule: null };
  }
  const v2 = ExportManifestV2Schema.safeParse(input);
  if (v2.success) {
    return {
      capsuleVersion: 2,
      manifest: v2.data,
      capsule: v2.data.capsule
    };
  }
  throw v2.error;
}
function parseExportBundle(input) {
  if (typeof input !== "object" || input === null) {
    ExportBundleV1Schema.parse(input);
    throw new Error("unreachable");
  }
  const manifestRaw = input.manifest;
  const normalized = parseExportManifest(manifestRaw);
  if (normalized.capsuleVersion === 2) {
    const bundle2 = ExportBundleV2Schema.parse(input);
    return { capsuleVersion: 2, bundle: bundle2, capsule: bundle2.manifest.capsule };
  }
  const bundle = ExportBundleV1Schema.parse(input);
  return { capsuleVersion: 1, bundle, capsule: null };
}

export {
  ExportManifestV1Schema,
  ExportMemoryRecordV1Schema,
  ExportBundleV1Schema,
  CAPSULE_ID_PATTERN,
  CapsuleRetrievalPolicySchema,
  CapsuleIncludesSchema,
  CapsuleParentSchema,
  CapsuleBlockSchema,
  ExportManifestV2Schema,
  ExportBundleV2Schema,
  parseExportManifest,
  parseExportBundle
};
//# sourceMappingURL=chunk-OA3L7BFR.js.map