{"version":3,"sources":["../../src/metrics/jsonata.ts"],"sourcesContent":["/**\n * Copyright 2024 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\nimport {\n  EvalStatusEnum,\n  type BaseEvalDataPoint,\n  type Score,\n} from 'genkit/evaluator';\nimport jsonata from 'jsonata';\n\n/**\n * JSONata evaluator -- matches JSONata expression from the reference against the output.\n */\nexport async function jsonataMetric(\n  dataPoint: BaseEvalDataPoint\n): Promise<Score> {\n  if (!dataPoint.output) {\n    throw new Error('Output was not provided');\n  }\n  if (!dataPoint.reference) {\n    throw new Error('Reference was not provided');\n  }\n  if (typeof dataPoint.reference !== 'string') {\n    throw new Error('Reference must be a string (jsonata)');\n  }\n\n  const expression = jsonata(dataPoint.reference);\n  const result = await expression.evaluate(dataPoint.output);\n\n  return {\n    score: !!result,\n    status: !!result ? EvalStatusEnum.PASS : EvalStatusEnum.FAIL,\n  };\n}\n"],"mappings":"AAgBA;AAAA,EACE;AAAA,OAGK;AACP,OAAO,aAAa;AAKpB,eAAsB,cACpB,WACgB;AAChB,MAAI,CAAC,UAAU,QAAQ;AACrB,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC3C;AACA,MAAI,CAAC,UAAU,WAAW;AACxB,UAAM,IAAI,MAAM,4BAA4B;AAAA,EAC9C;AACA,MAAI,OAAO,UAAU,cAAc,UAAU;AAC3C,UAAM,IAAI,MAAM,sCAAsC;AAAA,EACxD;AAEA,QAAM,aAAa,QAAQ,UAAU,SAAS;AAC9C,QAAM,SAAS,MAAM,WAAW,SAAS,UAAU,MAAM;AAEzD,SAAO;AAAA,IACL,OAAO,CAAC,CAAC;AAAA,IACT,QAAQ,CAAC,CAAC,SAAS,eAAe,OAAO,eAAe;AAAA,EAC1D;AACF;","names":[]}