{"version":3,"file":"patch-BbC0U8aR.mjs","names":[],"sources":["../src/lib/middleware/run_onion.ts","../src/lib/mime/is_textual.ts","../src/lib/text/decode_text.ts","../src/lib/patch/index.ts"],"sourcesContent":["import { Middleware } from '@nhtio/middleware'\nimport type { NextFn } from '@nhtio/middleware'\n\n/**\n * Execute an asynchronous middleware onion around a terminal operation.\n *\n * A fresh runner is deliberately created for each invocation: middleware runners are single-use.\n * Errors are captured and re-thrown unchanged — including a thrown `undefined`, which a\n * value-based sentinel would silently swallow — while a chain that stops without invoking its\n * terminal operation is reported through `onNoNext`.\n */\nexport const runOnion = async <Ctx, Res>(\n  ctx: Ctx,\n  use: readonly ((ctx: Ctx, next: NextFn) => void | Promise<void>)[],\n  core: () => Promise<Res>,\n  onNoNext: () => never\n): Promise<Res> => {\n  if (use.length === 0) return core()\n\n  const middleware = new Middleware<(ctx: Ctx, next: NextFn) => void | Promise<void>>()\n  for (const fn of use) middleware.add(fn)\n\n  let result!: Res\n  let terminalInvoked = false\n  let caught: unknown\n  // `didCatch` rather than testing `caught !== undefined`: `throw undefined` is legal JS, and a\n  // value test cannot tell it from \"nothing was thrown\" — the chain would resolve as a success.\n  let didCatch = false\n  await middleware\n    .runner()\n    .errorHandler(async (error: unknown) => {\n      didCatch = true\n      caught = error\n    })\n    .finalHandler(async () => {\n      terminalInvoked = true\n      result = await core()\n    })\n    .run((fn, next) => Promise.resolve(fn(ctx, next)))\n\n  if (didCatch) throw caught\n  if (!terminalInvoked) return onNoNext()\n  return result\n}\n","/** Whether a MIME type represents text that can be handled as markup or plain text. */\nexport const isTextual = (mime: string): boolean => {\n  const normalized = mime.toLowerCase().split(';', 1)[0].trim()\n  if (normalized.startsWith('text/')) return true\n  if (normalized === 'application/json' || normalized === 'application/yaml') return true\n  const subtype = normalized.split('/', 2)[1]\n  return subtype === 'xml' || subtype?.endsWith('+xml') === true\n}\n","/** Decode text bytes as UTF-8 text, recognizing UTF-16 byte-order marks. */\nconst utf8Decoder = new TextDecoder('utf-8', { fatal: false })\n\nexport const decodeText = (bytes: Uint8Array): string => {\n  let text: string\n  if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xfe)\n    text = new TextDecoder('utf-16le').decode(bytes.subarray(2))\n  else if (bytes.length >= 2 && bytes[0] === 0xfe && bytes[1] === 0xff)\n    text = new TextDecoder('utf-16be').decode(bytes.subarray(2))\n  else text = utf8Decoder.decode(bytes)\n  return text.replace(/\\r\\n?/g, '\\n')\n}\n","/**\n * The structured apply_patch envelope: parser and applier for the\n * `*** Begin Patch` multi-file dialect.\n *\n * @remarks\n * Shared structured-patch primitives for workspace-shaped batteries. This dialect derives from the\n * GitHub Copilot apply_patch format — one of the most robust and battle-tested patch formats\n * in production use, which is also why models already know how to write it. The grammar\n * (`*** Begin Patch` … `*** End Patch`, `*** Add File:` / `*** Delete File:` /\n * `*** Update File:` (+ `*** Move to:`), `@@` context hunks with `+`/`-`/space lines) is\n * preserved exactly — inventing a \"better\" variation would forfeit the pretraining. Ported\n * from the source server's `doc.apply_patch` adapter (its green e2e suite informs the spec\n * coverage); the context matcher rejects ambiguity (a hunk whose context matches more than\n * one location fails rather than guessing).\n */\n\n/** One parsed `@@` hunk: the context+removal lines and their replacement. */\nexport interface ParsedHunk {\n  oldLines: string[]\n  newLines: string[]\n  added: number\n  removed: number\n}\n\n/** `*** Add File:` — create a new file from `+` lines. */\nexport interface AddOperation {\n  type: 'add'\n  path: string\n  content: string\n  added: number\n}\n\n/** `*** Delete File:` — remove a file. */\nexport interface DeleteOperation {\n  type: 'delete'\n  path: string\n}\n\n/** `*** Update File:` (+ optional `*** Move to:`) — apply hunks, optionally rename. */\nexport interface UpdateOperation {\n  type: 'update'\n  path: string\n  movePath?: string\n  hunks: ParsedHunk[]\n  added: number\n  removed: number\n}\n\n/** Any one operation of a structured patch. */\nexport type PatchOperation = AddOperation | DeleteOperation | UpdateOperation\n\n/** The parsed envelope: ordered operations plus totals. */\nexport interface ParsedApplyPatch {\n  operations: PatchOperation[]\n  added: number\n  removed: number\n}\n\nconst PATCH_PREFIX = '*** Begin Patch'\nconst PATCH_SUFFIX = '*** End Patch'\nconst ADD_FILE_PREFIX = '*** Add File:'\nconst DELETE_FILE_PREFIX = '*** Delete File:'\nconst UPDATE_FILE_PREFIX = '*** Update File:'\nconst MOVE_TO_PREFIX = '*** Move to:'\n\n/** `true` when `patch` is the structured envelope rather than a unified diff. */\nexport const isStructuredPatch = (patch: string): boolean =>\n  patch.trimStart().startsWith(PATCH_PREFIX)\n\n/** Normalize a workspace path: relative, no `.`/`..`/empty segments, forward slashes. */\nexport const normalizeWorkspacePath = (path: string): string => {\n  const normalized = path.replace(/\\\\/g, '/').trim()\n  if (!normalized) {\n    throw new Error('apply_patch path cannot be empty')\n  }\n  if (normalized.startsWith('/') || /^[A-Za-z]:/.test(normalized)) {\n    throw new Error(`apply_patch path \"${path}\" must be relative to the workspace root`)\n  }\n  const segments = normalized.split('/')\n  const sanitizedSegments: string[] = []\n  for (const segment of segments) {\n    if (!segment || segment === '.' || segment === '..') {\n      throw new Error(\n        `apply_patch path \"${path}\" contains invalid segment \"${segment || '(empty)'}\"`\n      )\n    }\n    sanitizedSegments.push(segment)\n  }\n  return sanitizedSegments.join('/')\n}\n\n/**\n * Parse a structured `*** Begin Patch` envelope.\n *\n * @param patch - The raw patch text.\n * @returns The parsed operations.\n */\nexport const parseStructuredPatch = (patch: string): ParsedApplyPatch => {\n  const lines = patch.replace(/\\r\\n/g, '\\n').split('\\n')\n  const firstLine = lines[0]?.trim()\n  if (firstLine !== PATCH_PREFIX) {\n    throw new Error('a structured patch must start with \"*** Begin Patch\"')\n  }\n\n  const lastNonEmptyIndex = [...lines].reverse().findIndex((line) => line.trim() !== '')\n  const endIndex = lastNonEmptyIndex === -1 ? -1 : lines.length - 1 - lastNonEmptyIndex\n  if (endIndex < 0 || lines[endIndex]?.trim() !== PATCH_SUFFIX) {\n    throw new Error('a structured patch must end with \"*** End Patch\"')\n  }\n\n  const body = lines.slice(1, endIndex)\n\n  const operations: PatchOperation[] = []\n  let totalAdded = 0\n  let totalRemoved = 0\n  let i = 0\n\n  while (i < body.length) {\n    const line = body[i] ?? ''\n\n    if (line.trim() === '' || line.startsWith('*** End of File')) {\n      i += 1\n      continue\n    }\n\n    if (line.startsWith(ADD_FILE_PREFIX)) {\n      const path = normalizeWorkspacePath(line.slice(ADD_FILE_PREFIX.length).trim())\n      i += 1\n      const contentLines: string[] = []\n      while (i < body.length) {\n        const next = body[i] ?? ''\n        if (next.startsWith('*** ')) {\n          break\n        }\n        if (!next.startsWith('+')) {\n          throw new Error(`apply_patch add-file line must start with \"+\": \"${next}\"`)\n        }\n        contentLines.push(next.slice(1))\n        i += 1\n      }\n\n      operations.push({\n        type: 'add',\n        path,\n        content: contentLines.join('\\n'),\n        added: contentLines.length,\n      })\n      totalAdded += contentLines.length\n      continue\n    }\n\n    if (line.startsWith(DELETE_FILE_PREFIX)) {\n      const path = normalizeWorkspacePath(line.slice(DELETE_FILE_PREFIX.length).trim())\n      operations.push({ type: 'delete', path })\n      i += 1\n      continue\n    }\n\n    if (!line.startsWith(UPDATE_FILE_PREFIX)) {\n      throw new Error(`apply_patch invalid structured patch header: \"${line}\"`)\n    }\n\n    const path = normalizeWorkspacePath(line.slice(UPDATE_FILE_PREFIX.length).trim())\n    i += 1\n\n    let movePath: string | undefined\n    if ((body[i] ?? '').startsWith(MOVE_TO_PREFIX)) {\n      movePath = normalizeWorkspacePath((body[i] ?? '').slice(MOVE_TO_PREFIX.length).trim())\n      i += 1\n    }\n\n    const hunks: ParsedHunk[] = []\n    let opAdded = 0\n    let opRemoved = 0\n\n    while (i < body.length && (body[i] ?? '').startsWith('@@')) {\n      i += 1\n      const hunkLines: string[] = []\n      while (i < body.length) {\n        const next = body[i] ?? ''\n        if (next.startsWith('@@') || next.startsWith('*** ')) {\n          break\n        }\n        hunkLines.push(next)\n        i += 1\n      }\n\n      if (hunkLines.length === 0) {\n        throw new Error('apply_patch contains an empty hunk')\n      }\n\n      const oldLines: string[] = []\n      const newLines: string[] = []\n      let added = 0\n      let removed = 0\n\n      for (const hunkLine of hunkLines) {\n        if (hunkLine.startsWith('+')) {\n          newLines.push(hunkLine.slice(1))\n          added += 1\n        } else if (hunkLine.startsWith('-')) {\n          oldLines.push(hunkLine.slice(1))\n          removed += 1\n        } else if (hunkLine.startsWith(' ')) {\n          const contextLine = hunkLine.slice(1)\n          oldLines.push(contextLine)\n          newLines.push(contextLine)\n        } else {\n          throw new Error(\n            `apply_patch invalid hunk line (must start with +, -, or space): \"${hunkLine}\"`\n          )\n        }\n      }\n\n      if (added === 0 && removed === 0) {\n        throw new Error('apply_patch hunk must contain at least one added or removed line')\n      }\n\n      opAdded += added\n      opRemoved += removed\n      hunks.push({ oldLines, newLines, added, removed })\n    }\n\n    if (hunks.length === 0) {\n      throw new Error(`apply_patch update operation for \"${path}\" must contain at least one hunk`)\n    }\n\n    operations.push({ type: 'update', path, movePath, hunks, added: opAdded, removed: opRemoved })\n    totalAdded += opAdded\n    totalRemoved += opRemoved\n  }\n\n  if (operations.length === 0) {\n    throw new Error('apply_patch contains no operations')\n  }\n\n  return {\n    operations,\n    added: totalAdded,\n    removed: totalRemoved,\n  }\n}\n\nconst countSequenceMatches = (haystack: string[], needle: string[], start: number): number => {\n  if (needle.length === 0) {\n    return 0\n  }\n  let matches = 0\n  let i = start\n  while (i <= haystack.length - needle.length) {\n    let matched = true\n    let j = 0\n    while (j < needle.length) {\n      if (haystack[i + j] !== needle[j]) {\n        matched = false\n        break\n      }\n      j += 1\n    }\n    if (matched) {\n      matches += 1\n    }\n    i += 1\n  }\n  return matches\n}\n\nconst findExactSequence = (haystack: string[], needle: string[], start: number): number => {\n  if (needle.length === 0) {\n    return start\n  }\n  let i = start\n  while (i <= haystack.length - needle.length) {\n    let matched = true\n    let j = 0\n    while (j < needle.length) {\n      if (haystack[i + j] !== needle[j]) {\n        matched = false\n        break\n      }\n      j += 1\n    }\n    if (matched) {\n      return i\n    }\n    i += 1\n  }\n  return -1\n}\n\n/**\n * Apply an update operation's hunks to `inputText`. Context matching is exact and rejects\n * ambiguity: a hunk whose old-lines match more than one location past the cursor fails\n * rather than guessing.\n *\n * The input's line-ending convention is preserved: a file containing CRLF is rejoined with CRLF,\n * so a one-line edit stays a one-line diff rather than a whole-file newline rewrite.\n *\n * @param inputText - The file's current text.\n * @param hunks - The parsed hunks, in order.\n * @returns The patched text, using the input's dominant line terminator.\n */\nexport const applyUpdateHunks = (inputText: string, hunks: ParsedHunk[]): string => {\n  // Match the input's dominant terminator so a localized edit does not rewrite every line of a\n  // CRLF file. Hunk text is always LF-split internally; only the join is convention-aware.\n  const newline = inputText.includes('\\r\\n') ? '\\r\\n' : '\\n'\n  const lines = inputText.replace(/\\r\\n/g, '\\n').split('\\n')\n  let cursor = 0\n  for (const hunk of hunks) {\n    const matchCount = countSequenceMatches(lines, hunk.oldLines, cursor)\n    if (matchCount > 1) {\n      throw new Error('patch context is ambiguous and matches multiple locations')\n    }\n    const start = findExactSequence(lines, hunk.oldLines, cursor)\n    if (start === -1) {\n      throw new Error('the patch could not be applied cleanly to the source text')\n    }\n    lines.splice(start, hunk.oldLines.length, ...hunk.newLines)\n    cursor = start + hunk.newLines.length\n  }\n  return lines.join(newline)\n}\n\n/** One file in the virtual workspace a structured patch operates over. */\nexport interface WorkspaceFile {\n  text: string\n  mimeType: string\n}\n\n/**\n * Apply a parsed structured patch to a virtual workspace of files keyed by normalized path.\n *\n * @param files - The workspace (mutated in place).\n * @param patch - The parsed envelope.\n * @returns The workspace and the number of files touched.\n */\nexport const applyOperations = (\n  files: Map<string, WorkspaceFile>,\n  patch: ParsedApplyPatch\n): { files: Map<string, WorkspaceFile>; modifiedFiles: number } => {\n  let modifiedFiles = 0\n\n  for (const operation of patch.operations) {\n    if (operation.type === 'add') {\n      if (files.has(operation.path)) {\n        throw new Error(`cannot add file \"${operation.path}\": the path already exists`)\n      }\n      files.set(operation.path, {\n        text: operation.content,\n        mimeType: inferTextMimeFromPath(operation.path),\n      })\n      modifiedFiles += 1\n      continue\n    }\n\n    if (operation.type === 'delete') {\n      if (!files.has(operation.path)) {\n        throw new Error(`cannot delete file \"${operation.path}\": the path does not exist`)\n      }\n      files.delete(operation.path)\n      modifiedFiles += 1\n      continue\n    }\n\n    const current = files.get(operation.path)\n    if (!current) {\n      throw new Error(`cannot update file \"${operation.path}\": the path does not exist`)\n    }\n\n    const updatedText = applyUpdateHunks(current.text, operation.hunks)\n    const targetPath = operation.movePath ?? operation.path\n\n    if (operation.movePath && files.has(targetPath)) {\n      throw new Error(\n        `cannot move file \"${operation.path}\" to \"${targetPath}\": the target already exists`\n      )\n    }\n\n    files.delete(operation.path)\n    files.set(targetPath, {\n      text: updatedText,\n      mimeType: current.mimeType,\n    })\n    modifiedFiles += 1\n  }\n\n  return { files, modifiedFiles }\n}\n\n/** Infer a text MIME from a workspace path's extension (Add File outputs). */\nexport const inferTextMimeFromPath = (path: string): string => {\n  const dot = path.lastIndexOf('.')\n  const ext = dot > 0 ? path.slice(dot + 1).toLowerCase() : ''\n  if (ext === 'json') return 'application/json'\n  if (ext === 'md' || ext === 'markdown') return 'text/markdown'\n  if (ext === 'csv') return 'text/csv'\n  if (ext === 'yaml' || ext === 'yml') return 'application/yaml'\n  if (ext === 'html' || ext === 'htm') return 'text/html'\n  return 'text/plain'\n}\n"],"mappings":";;;;;;;;;;AAWA,IAAa,WAAW,OACtB,KACA,KACA,MACA,aACiB;CACjB,IAAI,IAAI,WAAW,GAAG,OAAO,KAAK;CAElC,MAAM,aAAa,IAAI,WAA6D;CACpF,KAAK,MAAM,MAAM,KAAK,WAAW,IAAI,EAAE;CAEvC,IAAI;CACJ,IAAI,kBAAkB;CACtB,IAAI;CAGJ,IAAI,WAAW;CACf,MAAM,WACH,OAAO,EACP,aAAa,OAAO,UAAmB;EACtC,WAAW;EACX,SAAS;CACX,CAAC,EACA,aAAa,YAAY;EACxB,kBAAkB;EAClB,SAAS,MAAM,KAAK;CACtB,CAAC,EACA,KAAK,IA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