{"version":3,"sources":["../src/json-patch.ts"],"sourcesContent":["/**\n * Copyright 2026 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * A tiny, dependency-free RFC 6902 (JSON Patch) implementation.\n *\n * This module is intentionally self-contained and browser-safe (no Node APIs,\n * no runtime dependencies) so it can be shared by the in-process server agent\n * and the browser-facing agent client.\n *\n * Genkit uses JSON Patch to stream incremental changes to a session's custom\n * state (`AgentStreamChunk.customPatch`). The {@link diff} helper only emits\n * `add` / `remove` / `replace` operations (a valid RFC 6902 subset - `move` /\n * `copy` are optimizations we deliberately skip), while {@link applyPatch}\n * understands the full operation set for interoperability.\n *\n * @module json-patch\n */\n\n/**\n * A single RFC 6902 (JSON Patch) operation.\n */\nexport interface JsonPatchOperation {\n  op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n  /** A JSON Pointer (RFC 6901) to the target location, e.g. `\"/agentStatus\"`. */\n  path: string;\n  /** Source pointer; required for `move` and `copy`. */\n  from?: string;\n  /** New value; required for `add`, `replace`, and `test`. */\n  value?: any;\n}\n\n/**\n * An RFC 6902 JSON Patch: an ordered list of operations.\n */\nexport type JsonPatch = JsonPatchOperation[];\n\n/**\n * Escapes a single JSON Pointer reference token per RFC 6901 (`~` → `~0`,\n * `/` → `~1`).\n */\nfunction escapeToken(token: string): string {\n  return token.replace(/~/g, '~0').replace(/\\//g, '~1');\n}\n\n/**\n * Unescapes a single JSON Pointer reference token per RFC 6901.\n */\nfunction unescapeToken(token: string): string {\n  return token.replace(/~1/g, '/').replace(/~0/g, '~');\n}\n\n/**\n * Reference tokens that could be used to pollute `Object.prototype` (or an\n * object's constructor) when walking into an existing object. We reject these\n * outright since patches may originate from untrusted, server-sent data.\n */\nconst FORBIDDEN_TOKENS = new Set(['__proto__', 'prototype', 'constructor']);\n\n/**\n * Parses a JSON Pointer string into its reference tokens.\n *\n * The root pointer (`\"\"`) parses to an empty array.\n *\n * Reference tokens that could lead to prototype pollution (`__proto__`,\n * `prototype`, `constructor`) are rejected.\n */\nfunction parsePointer(pointer: string): string[] {\n  if (pointer === '') return [];\n  if (pointer[0] !== '/') {\n    throw new Error(`Invalid JSON Pointer: \"${pointer}\" must start with \"/\".`);\n  }\n  const tokens = pointer.slice(1).split('/').map(unescapeToken);\n  for (const token of tokens) {\n    if (FORBIDDEN_TOKENS.has(token)) {\n      throw new Error(\n        `Invalid JSON Pointer: \"${pointer}\" contains forbidden token \"${token}\".`\n      );\n    }\n  }\n  return tokens;\n}\n\n/**\n * Returns `true` for values that are plain JSON objects (not arrays / null).\n */\nfunction isObject(value: unknown): value is Record<string, any> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Lists the keys of a plain object whose values are not `undefined`.\n *\n * JSON has no `undefined`: `JSON.stringify` drops such members entirely, so\n * `{ a: undefined }` and `{}` serialize identically. We mirror that here so\n * that diffing/equality match how state is actually persisted (and so we never\n * emit `undefined`-valued `add`/`replace` ops, which serialize to a valueless,\n * meaningless operation).\n */\nfunction definedKeys(obj: Record<string, any>): string[] {\n  return Object.keys(obj).filter((key) => obj[key] !== undefined);\n}\n\n/**\n * Deep structural equality for JSON-serializable values.\n *\n * Object members whose value is `undefined` are treated as absent, matching\n * JSON semantics (see {@link definedKeys}).\n */\nfunction deepEqual(a: unknown, b: unknown): boolean {\n  if (a === b) return true;\n  if (typeof a !== typeof b) return false;\n  if (Array.isArray(a) && Array.isArray(b)) {\n    if (a.length !== b.length) return false;\n    for (let i = 0; i < a.length; i++) {\n      if (!deepEqual(a[i], b[i])) return false;\n    }\n    return true;\n  }\n  if (isObject(a) && isObject(b)) {\n    const aKeys = definedKeys(a);\n    const bKeys = definedKeys(b);\n    if (aKeys.length !== bKeys.length) return false;\n    for (const key of aKeys) {\n      if (b[key] === undefined) return false;\n      if (!deepEqual(a[key], b[key])) return false;\n    }\n    return true;\n  }\n  return false;\n}\n\n/**\n * Structured clone fallback that works across runtimes (uses the global\n * `structuredClone` when available, otherwise JSON round-trips).\n */\nfunction clone<T>(value: T): T {\n  if (value === undefined) return value;\n  if (typeof structuredClone === 'function') {\n    return structuredClone(value);\n  }\n  return JSON.parse(JSON.stringify(value));\n}\n\n/**\n * Computes an RFC 6902 JSON Patch that transforms `from` into `to`.\n *\n * The diff is rooted at the document, so pointers are bare (e.g. `/agentStatus`,\n * `/items/0`). Only `add` / `remove` / `replace` operations are emitted.\n *\n * When the two documents differ at the root in a way that cannot be expressed\n * as member-level changes (e.g. an object becomes an array, or a primitive\n * changes), a single whole-document `replace` at path `\"\"` is returned.\n */\nexport function diff(from: unknown, to: unknown): JsonPatch {\n  const patch: JsonPatch = [];\n  diffRecursive(from, to, '', patch);\n  return patch;\n}\n\nfunction diffRecursive(\n  from: unknown,\n  to: unknown,\n  pointer: string,\n  patch: JsonPatch\n): void {\n  if (deepEqual(from, to)) return;\n\n  // Both plain objects - recurse member by member. Members whose value is\n  // `undefined` are treated as absent (JSON has no `undefined`); this avoids\n  // emitting valueless `add`/`replace` ops and correctly turns \"set to\n  // undefined\" into a `remove`.\n  if (isObject(from) && isObject(to)) {\n    const keys = new Set([...Object.keys(from), ...Object.keys(to)]);\n    for (const key of keys) {\n      const childPointer = `${pointer}/${escapeToken(key)}`;\n      const inFrom = from[key] !== undefined;\n      const inTo = to[key] !== undefined;\n      if (inFrom && !inTo) {\n        patch.push({ op: 'remove', path: childPointer });\n      } else if (!inFrom && inTo) {\n        patch.push({ op: 'add', path: childPointer, value: clone(to[key]) });\n      } else if (inFrom && inTo) {\n        diffRecursive(from[key], to[key], childPointer, patch);\n      }\n    }\n    return;\n  }\n\n  // Both arrays - recurse by index, then add/remove the tail difference.\n  if (Array.isArray(from) && Array.isArray(to)) {\n    const min = Math.min(from.length, to.length);\n    for (let i = 0; i < min; i++) {\n      diffRecursive(from[i], to[i], `${pointer}/${i}`, patch);\n    }\n    if (to.length > from.length) {\n      for (let i = from.length; i < to.length; i++) {\n        // Appends use the \"-\" end-of-array token per RFC 6902.\n        patch.push({ op: 'add', path: `${pointer}/-`, value: clone(to[i]) });\n      }\n    } else if (from.length > to.length) {\n      // Remove from the tail backwards so indices stay valid as we go.\n      for (let i = from.length - 1; i >= to.length; i--) {\n        patch.push({ op: 'remove', path: `${pointer}/${i}` });\n      }\n    }\n    return;\n  }\n\n  // Type mismatch or differing primitives - replace at this location.\n  patch.push({ op: 'replace', path: pointer, value: clone(to) });\n}\n\n/**\n * Applies an RFC 6902 JSON Patch to `document`, returning the new value.\n *\n * The input is not mutated; a clone is patched and returned. Operating on the\n * root pointer (`\"\"`) replaces / adds the whole document.\n *\n * Apply is intentionally lenient to keep streaming robust: applying an `add` /\n * `replace` whose parent container is missing initializes the parent as an\n * object, and a `remove` / `replace` targeting a missing member is a no-op\n * rather than an error. `test` operations are honored and throw on mismatch.\n */\nexport function applyPatch<T = any>(document: T, patch: JsonPatch): T {\n  let doc: any = clone(document);\n  for (const op of patch) {\n    doc = applyOperation(doc, op);\n  }\n  return doc as T;\n}\n\nfunction applyOperation(doc: any, op: JsonPatchOperation): any {\n  const tokens = parsePointer(op.path);\n\n  // Root operations replace / set the entire document.\n  if (tokens.length === 0) {\n    switch (op.op) {\n      case 'add':\n      case 'replace':\n        return clone(op.value);\n      case 'remove':\n        return undefined;\n      case 'test':\n        if (!deepEqual(doc, op.value)) {\n          throw new Error(`JSON Patch 'test' failed at root.`);\n        }\n        return doc;\n      case 'move':\n      case 'copy': {\n        const value = clone(getValue(doc, parsePointer(op.from!)));\n        return value;\n      }\n      default:\n        throw new Error(`Unsupported JSON Patch op: ${(op as any).op}`);\n    }\n  }\n\n  // Lenient: initialize a missing root container so member-level adds/replaces\n  // still land (e.g. applying `/status` onto an undefined document).\n  if (doc == null && (op.op === 'add' || op.op === 'replace')) {\n    doc = {};\n  }\n\n  switch (op.op) {\n    case 'add':\n      setValue(doc, tokens, clone(op.value), /* isAdd= */ true);\n      return doc;\n    case 'replace':\n      setValue(doc, tokens, clone(op.value), /* isAdd= */ false);\n      return doc;\n    case 'remove':\n      removeValue(doc, tokens);\n      return doc;\n    case 'test': {\n      const actual = getValue(doc, tokens);\n      if (!deepEqual(actual, op.value)) {\n        throw new Error(`JSON Patch 'test' failed at \"${op.path}\".`);\n      }\n      return doc;\n    }\n    case 'move': {\n      const fromTokens = parsePointer(op.from!);\n      const value = clone(getValue(doc, fromTokens));\n      removeValue(doc, fromTokens);\n      setValue(doc, tokens, value, /* isAdd= */ true);\n      return doc;\n    }\n    case 'copy': {\n      const value = clone(getValue(doc, parsePointer(op.from!)));\n      setValue(doc, tokens, value, /* isAdd= */ true);\n      return doc;\n    }\n    default:\n      throw new Error(`Unsupported JSON Patch op: ${(op as any).op}`);\n  }\n}\n\n/**\n * Reads the value at `tokens`, returning `undefined` for any missing segment.\n */\nfunction getValue(doc: any, tokens: string[]): any {\n  let cur = doc;\n  for (const token of tokens) {\n    if (cur == null) return undefined;\n    cur = Array.isArray(cur) ? cur[Number(token)] : cur[token];\n  }\n  return cur;\n}\n\n/**\n * Sets the value at `tokens`, creating intermediate object containers as\n * needed. When `isAdd` is true and the parent is an array, the special `-`\n * token appends and a numeric token inserts at that index.\n */\nfunction setValue(\n  doc: any,\n  tokens: string[],\n  value: any,\n  isAdd: boolean\n): void {\n  const parent = ensureParent(doc, tokens);\n  if (parent == null) return; // Lenient: nothing to set onto.\n  const last = tokens[tokens.length - 1];\n  if (Array.isArray(parent)) {\n    if (last === '-') {\n      parent.push(value);\n      return;\n    }\n    const idx = Number(last);\n    if (Number.isNaN(idx)) return;\n    if (isAdd) {\n      parent.splice(idx, 0, value);\n    } else {\n      parent[idx] = value;\n    }\n    return;\n  }\n  parent[last] = value;\n}\n\n/**\n * Removes the value at `tokens`. Missing members are a no-op.\n */\nfunction removeValue(doc: any, tokens: string[]): void {\n  const parent = getValue(doc, tokens.slice(0, -1));\n  if (parent == null) return;\n  const last = tokens[tokens.length - 1];\n  if (Array.isArray(parent)) {\n    const idx = Number(last);\n    if (!Number.isNaN(idx) && idx >= 0 && idx < parent.length) {\n      parent.splice(idx, 1);\n    }\n    return;\n  }\n  if (isObject(parent)) {\n    delete parent[last];\n  }\n}\n\n/**\n * Walks to the parent container of `tokens`, lazily creating intermediate\n * objects for missing segments so leniently-applied patches still land.\n */\nfunction ensureParent(doc: any, tokens: string[]): any {\n  let cur = doc;\n  for (let i = 0; i < tokens.length - 1; i++) {\n    const token = tokens[i];\n    if (cur == null) return undefined;\n    const next = Array.isArray(cur) ? cur[Number(token)] : cur[token];\n    if (next == null || typeof next !== 'object') {\n      const created: any = {};\n      if (Array.isArray(cur)) {\n        cur[Number(token)] = created;\n      } else {\n        cur[token] = created;\n      }\n      cur = created;\n    } else {\n      cur = next;\n    }\n  }\n  return cur;\n}\n"],"mappings":"AAsDA,SAAS,YAAY,OAAuB;AAC1C,SAAO,MAAM,QAAQ,MAAM,IAAI,EAAE,QAAQ,OAAO,IAAI;AACtD;AAKA,SAAS,cAAc,OAAuB;AAC5C,SAAO,MAAM,QAAQ,OAAO,GAAG,EAAE,QAAQ,OAAO,GAAG;AACrD;AAOA,MAAM,mBAAmB,oBAAI,IAAI,CAAC,aAAa,aAAa,aAAa,CAAC;AAU1E,SAAS,aAAa,SAA2B;AAC/C,MAAI,YAAY,GAAI,QAAO,CAAC;AAC5B,MAAI,QAAQ,CAAC,MAAM,KAAK;AACtB,UAAM,IAAI,MAAM,0BAA0B,OAAO,wBAAwB;AAAA,EAC3E;AACA,QAAM,SAAS,QAAQ,MAAM,CAAC,EAAE,MAAM,GAAG,EAAE,IAAI,aAAa;AAC5D,aAAW,SAAS,QAAQ;AAC1B,QAAI,iBAAiB,IAAI,KAAK,GAAG;AAC/B,YA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