import{Bt as e,Cp as t,Ct as n,Ht as r,Hy as i,Ic as a,Ly as o,Nh as s,Pt as c,Ql as l,Rt as u,Ut as d,Vt as f,Zl as p,Zv as m,_g as h,ac as g,ap as _,bg as v,c_ as y,dg as b,en as x,ep as S,hg as C,i_ as w,lh as T,m_ as E,mg as D,mh as O,mv as k,n_ as A,od as j,pg as M,pl as N,r_ as P,tl as F,ty as I,vg as L,zt as R}from"./index-DDwwBb9e.js";var z=i();function B(e){let{filterParams:t,queryOpts:n}=a(),r=T.traces.getSampleTracesDataset.useQuery(t,n);return e.traceMapping&&!e.traceMapping?.mapping?null:(0,z.jsx)(F,{...e,traces:r.data??[]})}var V=t();function H({slug:e,evaluatorDefinition:t,form:r,checkType:i,name:a,executionMode:c,settings:u,storeSettingsOnCode:d}){let f=c===V.EvaluationExecutionMode.AS_GUARDRAIL,p=d?i:e,{project:y}=_(),x=t.requiredFields.includes(`output`),S=T.project.getProjectAPIKey.useQuery({projectId:y?.id??``},{enabled:!!y}),w=({async:e})=>{let n=`\n        name="${a}",`,r=[];t.requiredFields.includes(`input`)?r.push(`"input": user_input`):t.optionalFields.includes(`input`)&&r.push(`"input": user_input  # optional`),t.requiredFields.includes(`output`)?r.push(`"output": generated_response`):t.optionalFields.includes(`output`)&&r.push(`"output": generated_response  # optional`),t.requiredFields.includes(`contexts`)?r.push(`"contexts": ["retrieved snippet 1", "retrieved snippet 2"]`):t.optionalFields.includes(`contexts`)&&r.push(`"contexts": ["retrieved snippet 1", "retrieved snippet 2"]  # optional`),t.requiredFields.includes(`expected_output`)?r.push(`"expected_output": gold_answer`):t.optionalFields.includes(`expected_output`)&&r.push(`"expected_output": gold_answer  # optional`),t.requiredFields.includes(`conversation`)?r.push(`"conversation": conversation_history`):t.optionalFields.includes(`conversation`)&&r.push(`"conversation": conversation_history  # optional`);let i=r.length>0?`\n        data={\n            ${r.join(`,
            `)}\n        },`:`
        data={},`,s=d?`\n        settings=${JSON.stringify(u??{},null,2).replace(/true/g,`True`).replace(/false/g,`False`).split(`
`).map((e,t)=>t===0?e:`        `+e).join(`
`)},`:``;return(0,z.jsxs)(b,{align:`start`,width:`full`,gap:3,children:[(0,z.jsx)(m,{fontSize:`14px`,children:`Add this import at the top of the file where the LLM call happens:`}),(0,z.jsx)(o,{className:`markdown`,width:`full`,children:(0,z.jsx)(g,{code:`import langwatch`,language:`python`})}),(!x||!f)&&(0,z.jsxs)(z.Fragment,{children:[(0,z.jsx)(m,{fontSize:`14px`,children:f?x?`Then, after calling your LLM, check for the guardrail:`:`Then, either before or after calling your LLM, check for the guardrail:`:`Then, pass in the message data to get the result of the evaluator:`}),(0,z.jsx)(o,{className:`markdown`,width:`full`,children:(0,z.jsx)(g,{code:`def llm_step():
    ... # your existing code

    ${f?`guardrail`:`result`} = ${e?`await langwatch.evaluation.async_evaluate`:`langwatch.evaluation.evaluate`}(
        "${p}",${i}${n}${s}${f?`
        as_guardrail=True,`:``}
    )
${f?`
    if not guardrail.passed:
        # handle the guardrail here
        return "I'm sorry, I can't do that."`:`
    print(result)`}`,language:`python`})})]})]})},O=()=>{let e=`\n        name: "${a}",`,n=[];t.requiredFields.includes(`input`)?n.push(`input: message`):t.optionalFields.includes(`input`)&&n.push(`input: message /* optional */`),t.requiredFields.includes(`output`)?n.push(`output: generatedResponse`):t.optionalFields.includes(`output`)&&n.push(`output: generatedResponse /* optional */`),t.requiredFields.includes(`contexts`)?n.push(`contexts: ["retrieved snippet 1", "retrieved snippet 2"]`):t.optionalFields.includes(`contexts`)&&n.push(`contexts: ["retrieved snippet 1", "retrieved snippet 2"] /* optional */`),t.requiredFields.includes(`expected_output`)?n.push(`expectedOutput: goldAnswer`):t.optionalFields.includes(`expected_output`)&&n.push(`expectedOutput: goldAnswer /* optional */`),t.requiredFields.includes(`conversation`)?n.push(`conversation: conversationHistory`):t.optionalFields.includes(`conversation`)&&n.push(`conversation: conversationHistory /* optional */`);let r=n.length>0?`\n        data: {\n          ${n.join(`,
          `)}\n        },`:`
        data: {},`,i=d?`\n        settings: ${JSON.stringify(u??{},null,2).replace(/"(\w+)"\s*:/g,`$1:`).split(`
`).map((e,t)=>t===0?e:`      `+e).join(`
`)},`:``;return(0,z.jsxs)(b,{align:`start`,width:`full`,gap:3,children:[(0,z.jsxs)(m,{fontSize:`14px`,children:[`First, set up your traces and spans capturing as explained in the`,` `,(0,z.jsx)(N,{href:`https://docs.langwatch.ai/integration/typescript/guide`,isExternal:!0,children:`documentation`}),`.`]}),(!x||!f)&&(0,z.jsxs)(z.Fragment,{children:[(0,z.jsx)(m,{fontSize:`14px`,children:f?x?`Then, after calling your LLM, check for the guardrail:`:`Then, either before or after calling your LLM, check for the guardrail:`:`Then, pass in the message data to get the result of the evaluator:`}),(0,z.jsx)(o,{className:`markdown`,width:`full`,children:(0,z.jsx)(g,{code:`import { LangWatch } from "langwatch";

const langwatch = new LangWatch();

async function llmStep({ message }: { message: string }): Promise<string> {
    ${f?``:`// ... your existing code

    `}// call the ${f?`guardrail`:`evaluator`}
    const ${f?`guardrail`:`result`} = await langwatch.evaluations.evaluate(
      "${p}",
      {${r}${e}${i}${f?`
        asGuardrail: true,`:``}
      }
    );
${f?`
    if (!guardrail.passed) {
        // handle the guardrail here
        return "I'm sorry, I can't do that.";
    }

    // ... your existing code`:`
    console.log(result);`}
}`,language:`typescript`})})]})]})},k=d?`,\n  "settings": ${JSON.stringify(u??{},null,2).split(`
`).map((e,t)=>t===0?e:`  `+e).join(`
`)}`:``;return(0,z.jsxs)(b,{gap:4,align:`start`,width:`full`,children:[(0,z.jsx)(I,{as:`h4`,fontSize:`16px`,fontWeight:500,paddingTop:4,children:c===V.EvaluationExecutionMode.MANUALLY?`Manual Integration`:`Guardrail Integration`}),(0,z.jsxs)(E,{children:[(0,z.jsxs)(m,{fontSize:`14px`,children:[`This`,` `,c===V.EvaluationExecutionMode.MANUALLY?`evaluator`:`guardrail`,` `,`uses:`]}),t.requiredFields.map(e=>(0,z.jsx)(D,{colorPalette:`blue`,children:(0,z.jsxs)(M,{children:[e,` (required)`]})},e)).concat(t.optionalFields.map(e=>(0,z.jsx)(D,{children:(0,z.jsxs)(M,{children:[e,` (optional)`]})},e)))]}),(0,z.jsxs)(m,{fontSize:`14px`,children:[`Follow the code example below to integrate this`,` `,f?`guardrail`:`evaluator`,` in your LLM pipeline, save changes first for the `,f?`guardrail`:`evaluator`,` to work.`]}),r&&(0,z.jsxs)(E,{children:[(0,z.jsx)(j,{...r.register(`storeSettingsOnCode`),children:`Store settings on code`}),(0,z.jsx)(l,{content:`Store the settings on the code to keep it versioned on your side instead of on LangWatch dashboard.`,positioning:{placement:`top`},children:(0,z.jsx)(o,{children:(0,z.jsx)(s,{size:16})})})]}),(0,z.jsxs)(L,{defaultValue:`python`,width:`full`,colorPalette:`orange`,children:[(0,z.jsxs)(h,{marginBottom:4,children:[(0,z.jsx)(v,{value:`python`,children:`Python`}),(0,z.jsx)(v,{value:`python-async`,children:`Python (Async)`}),(0,z.jsx)(v,{value:`typescript`,children:`TypeScript`}),(0,z.jsx)(v,{value:`curl`,children:`Curl`})]}),(0,z.jsx)(C,{value:`python`,padding:0,children:(0,z.jsx)(w,{async:!1})}),(0,z.jsx)(C,{value:`python-async`,padding:0,children:(0,z.jsx)(w,{async:!0})}),(0,z.jsx)(C,{value:`typescript`,padding:0,children:(0,z.jsx)(O,{})}),(0,z.jsx)(C,{value:`curl`,padding:0,children:(0,z.jsxs)(b,{align:`start`,width:`full`,gap:3,children:[(0,z.jsx)(o,{className:`markdown`,width:`full`,children:(0,z.jsx)(g,{code:`# Set your API key and endpoint URL
API_KEY="${S.data?.apiKey??`your_langwatch_api_key`}"

# Use curl to send the POST request, e.g.:
curl -X POST "${n()}/api/evaluations/${p}/evaluate" \\
     -H "X-Auth-Token: $API_KEY" \\
     -H "Content-Type: application/json" \\
     -d @- <<EOF
{
  "trace_id": "trace-123",
  "name": "${a}",
  "data": {
    ${t.requiredFields.map(e=>`"${e}": "${e} content"`).concat(t.optionalFields.map(e=>`"${e}": "${e} content (optional)"`)).join(`,
    `)}
  }${f?`,
  "as_guardrail": true`:``}${k}
}
EOF`,language:`bash`})}),(0,z.jsx)(m,{children:`Response:`}),(0,z.jsx)(o,{className:`markdown`,width:`full`,children:(0,z.jsx)(g,{code:JSON.stringify({status:`processed`,passed:!0,score:1,details:`possible explanation`},null,2),language:`json`})})]})})]})]})}var U=({runOn:t,append:n,remove:i,fields:a,label:s=`Preconditions`,helper:l=`Conditions that must be met for this check to run`})=>{let{control:h,watch:g,setValue:_,formState:v}=S(),C=g(`preconditions`),T=c(g(`checkType`)),D=f(),j=(t,n)=>{let i=n,a=C[t]?.rule;if(a&&!d(i,a)){let n=e(i);n.length>0&&_(`preconditions.${t}.rule`,n[0])}r(i)===`boolean`&&_(`preconditions.${t}.value`,`true`),_(`preconditions.${t}.key`,void 0),_(`preconditions.${t}.subkey`,void 0)};return(0,z.jsx)(p,{label:s,helper:l,children:(0,z.jsxs)(b,{align:`start`,gap:4,children:[T?.requiredFields.includes(`contexts`)&&(0,z.jsx)(o,{borderLeft:`4px solid`,borderLeftColor:`blue.400`,width:`full`,children:(0,z.jsxs)(b,{borderLeftColor:`reset`,padding:3,width:`full`,align:`start`,position:`relative`,children:[(0,z.jsx)(m,{children:`Requires RAG Contexts`}),(0,z.jsx)(m,{color:`fg.muted`,fontStyle:`italic`,children:`This evaluator will only run if the RAG contexts are provided`})]})}),T?.requiredFields.includes(`expected_output`)&&(0,z.jsx)(o,{borderLeft:`4px solid`,borderLeftColor:`blue.400`,width:`full`,children:(0,z.jsxs)(b,{borderLeftColor:`reset`,padding:3,width:`full`,align:`start`,position:`relative`,children:[(0,z.jsx)(m,{children:`Requires an Expected Output`}),(0,z.jsx)(m,{color:`fg.muted`,fontStyle:`italic`,children:`This evaluator will only run if the expected output is provided`})]})}),T?.requiredFields.includes(`expected_contexts`)&&(0,z.jsx)(o,{borderLeft:`4px solid`,borderLeftColor:`blue.400`,width:`full`,children:(0,z.jsxs)(b,{borderLeftColor:`reset`,padding:3,width:`full`,align:`start`,position:`relative`,children:[(0,z.jsx)(m,{children:`Requires Expected Contexts`}),(0,z.jsx)(m,{color:`fg.muted`,fontStyle:`italic`,children:`This evaluator will only run if the expected contexts are provided`})]})}),a.map((t,n)=>{let a=C[n]?.field,s=e(a),c=r(a),l=R(a);return(0,z.jsxs)(o,{borderLeft:`4px solid`,borderLeftColor:`blue.400`,width:`full`,children:[(0,z.jsxs)(b,{borderLeftColor:`reset`,padding:3,width:`full`,align:`start`,position:`relative`,children:[(0,z.jsx)(k,{position:`absolute`,right:0,top:0,padding:0,size:`sm`,variant:`ghost`,onClick:()=>i(n),color:`fg.subtle`,children:(0,z.jsx)(O,{})}),(0,z.jsx)(x,{children:n==0?`When`:`and`}),(0,z.jsxs)(E,{gap:2,flexWrap:`wrap`,children:[(0,z.jsxs)(w,{minWidth:`fit-content`,children:[(0,z.jsx)(A,{...h.register(`preconditions.${n}.field`,{onChange:e=>{j(n,e.target.value)}}),children:D.map(e=>(0,z.jsx)(`optgroup`,{label:e.category,children:e.fields.map(e=>(0,z.jsx)(`option`,{value:e.value,children:e.label},e.value))},e.category))}),(0,z.jsx)(P,{})]}),l&&(0,z.jsx)(y,{...h.register(`preconditions.${n}.key`),placeholder:l.label,minWidth:`120px`,maxWidth:`200px`}),(0,z.jsxs)(w,{minWidth:`fit-content`,children:[(0,z.jsx)(A,{...h.register(`preconditions.${n}.rule`),children:s.map(e=>(0,z.jsx)(`option`,{value:e,children:u[e]},e))}),(0,z.jsx)(P,{})]})]}),(0,z.jsx)(E,{width:`full`,children:c===`boolean`?(0,z.jsxs)(w,{minWidth:`fit-content`,children:[(0,z.jsxs)(A,{...h.register(`preconditions.${n}.value`),children:[(0,z.jsx)(`option`,{value:`true`,children:`true`}),(0,z.jsx)(`option`,{value:`false`,children:`false`})]}),(0,z.jsx)(P,{})]}):(0,z.jsxs)(z.Fragment,{children:[C[n]?.rule.includes(`regex`)&&(0,z.jsx)(m,{fontSize:`16px`,children:`/`}),(0,z.jsx)(y,{...h.register(`preconditions.${n}.value`),placeholder:C[n]?.rule.includes(`regex`)?`regex`:`text`}),C[n]?.rule.includes(`regex`)&&(0,z.jsx)(m,{fontSize:`16px`,children:`/gi`})]})})]}),v.errors.preconditions?.[n]?.value&&(0,z.jsx)(m,{color:`red.500`,fontSize:`12px`,paddingLeft:4,children:v.errors.preconditions?.[n]?.value.message})]},t.id)}),t,(0,z.jsx)(k,{onClick:()=>n({field:`output`,rule:`contains`,value:``}),children:`Add Precondition`})]})})};export{H as n,B as r,U as t};
//# sourceMappingURL=PreconditionsField-DC3684VR.js.map