import {
  isValidCapsuleSince
} from "./chunk-IYY4MCPG.js";
import {
  CAPSULE_ID_PATTERN
} from "./chunk-OA3L7BFR.js";

// src/access-schema.ts
import { z } from "zod";
function formatZodError(error) {
  return {
    error: "request validation failed",
    code: "validation_error",
    details: error.issues.map((issue) => ({
      field: issue.path.join(".") || "(root)",
      message: issue.message
    }))
  };
}
var namespaceSchema = z.string().trim().max(256).optional();
var sessionKeySchema = z.string().trim().min(1).max(512).optional();
var idempotencyKeySchema = z.string().trim().min(1).max(256).optional();
var dryRunSchema = z.boolean().optional();
var schemaVersionSchema = z.number().int().optional();
var codingContextSchema = z.object({
  projectId: z.string().trim().min(1, "codingContext.projectId is required").max(128),
  branch: z.string().trim().max(256).nullable(),
  rootPath: z.string().trim().min(1, "codingContext.rootPath is required").max(1024),
  defaultBranch: z.string().trim().max(256).nullable()
}).nullable();
var recallDisclosureSchema = z.enum(["chunk", "section", "raw"]);
var tagMatchSchema = z.enum(["any", "all"]);
var recallRequestSchema = z.object({
  query: z.string().min(1, "query is required"),
  sessionKey: sessionKeySchema,
  namespace: namespaceSchema,
  topK: z.number().int().min(0).max(200).optional(),
  mode: z.enum(["auto", "no_recall", "minimal", "full", "graph_mode"]).optional(),
  includeDebug: z.boolean().optional(),
  disclosure: recallDisclosureSchema.optional(),
  codingContext: codingContextSchema.optional(),
  /** Working directory for auto git-context resolution (issue #569). */
  cwd: z.string().trim().min(1, "cwd must be non-empty when provided").max(2048).optional(),
  /**
   * Arbitrary project tag for non-git-based project scoping (issue #569).
   * Creates a coding context with `projectId: "tag:<projectTag>"`.
   */
  projectTag: z.string().trim().min(1, "projectTag must be non-empty when provided").max(256).optional(),
  /**
   * Historical recall pin (issue #680).  ISO 8601 timestamp.  The
   * schema only enforces the basic shape; the access service runs
   * `Date.parse` and emits a structured 400 on malformed input
   * (CLAUDE.md rule 51).
   */
  asOf: z.string().trim().min(1, "asOf must be a non-empty ISO 8601 timestamp").max(64).optional(),
  /**
   * Free-form recall tag filter (issue #689). When provided, recall results
   * whose frontmatter `tags` do not match the filter are removed before the
   * response is returned. Comparison is case-sensitive exact match.
   */
  tags: z.array(z.string().trim().min(1).max(256)).max(50).optional(),
  /**
   * Match mode for `tags` (issue #689). Defaults to `"any"` when `tags` is
   * provided and `tagMatch` is omitted. Ignored when `tags` is absent.
   */
  tagMatch: tagMatchSchema.optional(),
  /**
   * Include graph edges below `graphTraversalConfidenceFloor` for diagnostic
   * recall traversal (issue #681). Defaults to false.
   */
  includeLowConfidence: z.boolean().optional()
});
var recallExplainRequestSchema = z.object({
  sessionKey: sessionKeySchema,
  namespace: namespaceSchema
});
var setCodingContextRequestSchema = z.object({
  sessionKey: z.string().trim().min(1, "sessionKey is required").max(512),
  codingContext: codingContextSchema
});
var messageSchema = z.object({
  role: z.enum(["user", "assistant"]),
  content: z.string().min(1, "message content must be non-empty")
});
var observeRequestSchema = z.object({
  sessionKey: z.string().trim().min(1, "sessionKey is required").max(512),
  messages: z.array(messageSchema).min(1, "messages must be a non-empty array"),
  namespace: namespaceSchema,
  skipExtraction: z.boolean().optional(),
  /** Working directory for auto git-context resolution (issue #569). */
  cwd: z.string().trim().min(1, "cwd must be non-empty when provided").max(2048).optional(),
  /**
   * Arbitrary project tag for non-git-based project scoping (issue #569).
   * Creates a coding context with `projectId: "tag:<projectTag>"`.
   */
  projectTag: z.string().trim().min(1, "projectTag must be non-empty when provided").max(256).optional()
});
var writeContentSchema = z.string().min(1, "content is required").max(5e4);
var categorySchema = z.enum([
  "fact",
  "preference",
  "correction",
  "entity",
  "decision",
  "relationship",
  "principle",
  "commitment",
  "moment",
  "skill",
  "rule",
  "procedure",
  "reasoning_trace"
]).optional();
var confidenceSchema = z.number().min(0).max(1).optional();
var tagsSchema = z.array(z.string().max(256)).max(50).optional();
var entityRefSchema = z.string().trim().max(512).optional();
var ttlSchema = z.string().trim().max(128).optional();
var sourceReasonSchema = z.string().trim().max(2e3).optional();
var memoryStoreRequestSchema = z.object({
  schemaVersion: schemaVersionSchema,
  idempotencyKey: idempotencyKeySchema,
  dryRun: dryRunSchema,
  sessionKey: sessionKeySchema,
  content: writeContentSchema,
  category: categorySchema,
  confidence: confidenceSchema,
  namespace: namespaceSchema,
  tags: tagsSchema,
  entityRef: entityRefSchema,
  ttl: ttlSchema,
  sourceReason: sourceReasonSchema
});
var suggestionSubmitRequestSchema = memoryStoreRequestSchema;
var reviewDispositionRequestSchema = z.object({
  memoryId: z.string().trim().min(1, "memoryId is required"),
  status: z.enum([
    "active",
    "pending_review",
    "quarantined",
    "rejected",
    "superseded",
    "archived"
  ]),
  reasonCode: z.string().trim().min(1, "reasonCode is required"),
  namespace: namespaceSchema
});
var trustZonePromoteRequestSchema = z.object({
  recordId: z.string().trim().min(1, "recordId is required"),
  targetZone: z.enum(["working", "trusted"], {
    errorMap: () => ({ message: "targetZone must be 'working' or 'trusted'" })
  }),
  promotionReason: z.string().trim().min(1, "promotionReason is required"),
  recordedAt: z.string().trim().optional(),
  summary: z.string().trim().max(5e3).optional(),
  dryRun: dryRunSchema,
  namespace: namespaceSchema
});
var trustZoneDemoSeedRequestSchema = z.object({
  scenario: z.string().trim().max(256).optional(),
  recordedAt: z.string().trim().optional(),
  dryRun: dryRunSchema,
  namespace: namespaceSchema
});
var lcmSearchRequestSchema = z.object({
  query: z.string().min(1, "query is required"),
  sessionKey: sessionKeySchema,
  namespace: namespaceSchema,
  limit: z.number().int().min(1).max(100).optional()
});
var daySummaryRequestSchema = z.object({
  memories: z.string().max(1e5).optional(),
  sessionKey: sessionKeySchema,
  namespace: namespaceSchema
});
var capsuleTopLevelSegmentSchema = z.string().trim().min(1).max(128).refine(
  (value) => !value.includes("/") && !value.includes("\\"),
  "must be a top-level directory name without path separators"
);
var capsulePeerIdSchema = z.string().trim().min(1).max(256).refine(
  (value) => value !== "." && value !== ".." && !value.includes("/") && !value.includes("\\"),
  "must be a plain peer id without path separators"
);
var capsuleIsoSinceSchema = z.string().trim().min(1, "since must be a non-empty ISO 8601 timestamp").max(128).refine(
  isValidCapsuleSince,
  "since must be a valid ISO 8601 timestamp with no calendar overflow"
);
var capsuleExportRequestSchema = z.object({
  name: z.string().trim().min(1, "name is required").max(64, "name must be 64 characters or fewer").regex(
    CAPSULE_ID_PATTERN,
    "name must be alphanumeric with single dashes (no spaces, no leading/trailing dashes)"
  ),
  namespace: namespaceSchema,
  since: capsuleIsoSinceSchema.optional(),
  includeKinds: z.array(capsuleTopLevelSegmentSchema).max(50).optional(),
  peerIds: z.array(capsulePeerIdSchema).max(100).optional(),
  includeTranscripts: z.boolean().optional(),
  encrypt: z.boolean().optional()
});
var capsuleImportRequestSchema = z.object({
  archivePath: z.string().trim().min(1, "archivePath is required").max(4096),
  namespace: namespaceSchema,
  mode: z.enum(["skip", "overwrite", "fork"]).optional()
});
var capsuleListRequestSchema = z.object({
  namespace: namespaceSchema
});
var schemas = {
  recall: recallRequestSchema,
  recallExplain: recallExplainRequestSchema,
  setCodingContext: setCodingContextRequestSchema,
  observe: observeRequestSchema,
  memoryStore: memoryStoreRequestSchema,
  suggestionSubmit: suggestionSubmitRequestSchema,
  reviewDisposition: reviewDispositionRequestSchema,
  trustZonePromote: trustZonePromoteRequestSchema,
  trustZoneDemoSeed: trustZoneDemoSeedRequestSchema,
  lcmSearch: lcmSearchRequestSchema,
  daySummary: daySummaryRequestSchema,
  capsuleExport: capsuleExportRequestSchema,
  capsuleImport: capsuleImportRequestSchema,
  capsuleList: capsuleListRequestSchema
};
function validateRequest(schemaName, body) {
  const schema = schemas[schemaName];
  if (!schema) {
    return {
      success: false,
      error: {
        error: `unknown schema: ${schemaName}`,
        code: "validation_error",
        details: []
      }
    };
  }
  const result = schema.safeParse(body);
  if (result.success) {
    return { success: true, data: result.data };
  }
  return { success: false, error: formatZodError(result.error) };
}

export {
  formatZodError,
  codingContextSchema,
  recallDisclosureSchema,
  tagMatchSchema,
  recallRequestSchema,
  recallExplainRequestSchema,
  setCodingContextRequestSchema,
  observeRequestSchema,
  memoryStoreRequestSchema,
  suggestionSubmitRequestSchema,
  reviewDispositionRequestSchema,
  trustZonePromoteRequestSchema,
  trustZoneDemoSeedRequestSchema,
  lcmSearchRequestSchema,
  daySummaryRequestSchema,
  capsuleExportRequestSchema,
  capsuleImportRequestSchema,
  capsuleListRequestSchema,
  validateRequest
};
//# sourceMappingURL=chunk-RILIVK4O.js.map