11// if we are on localhost or 127.0.0.1, use the local explorer
2- BASE_URL = window . location . hostname === "localhost" || window . location . hostname === "127.0.0.1" ? "http://localhost/" : "https://explorer.invariantlabs.ai/" ;
2+ BASE_URL =
3+ window . location . hostname === "localhost" ||
4+ window . location . hostname === "127.0.0.1"
5+ ? "http://localhost/"
6+ : "https://explorer.invariantlabs.ai/" ;
37
48function encodeGuardrailURIComponent ( content ) {
59 /**
@@ -41,10 +45,10 @@ function findSnippetTitle(codeElement) {
4145 return "New Guardrail" ;
4246}
4347
44- function changeElements ( codeElements , endpoint ) {
48+ function changeElements ( codeElements , endpoint , embed = false ) {
4549 // Add a button to each pre element
4650 codeElements . forEach ( function ( codeElement ) {
47- // replace the code element with an iframe
51+ // augment the code element
4852 let textContent = codeElement . textContent || codeElement . innerText ;
4953
5054 // parse and split contents
@@ -60,28 +64,42 @@ function changeElements(codeElements, endpoint) {
6064 ) ;
6165 }
6266
63- // add links for the ${BASE_URL}/playground?policy=...&input=... (Call it Open In Playground)
64- const container = document . createElement ( "div" ) ;
65- container . className = "action-links" ;
67+ if ( ! embed ) {
68+ // add links for the ${BASE_URL}/playground?policy=...&input=... (Call it Open In Playground)
69+ const container = document . createElement ( "div" ) ;
70+ container . className = "action-links" ;
6671
67- const playgroundLink = document . createElement ( "a" ) ;
68- playgroundLink . className = "link open-in-playground" ;
69- playgroundLink . href = `${ BASE_URL } ${ endpoint } =${ encodedContent } ${ exampleTraceURIComponent } ` ;
70- playgroundLink . target = "_blank" ;
71- playgroundLink . innerText = "⏵ Open In Playground" ;
72+ const playgroundLink = document . createElement ( "a" ) ;
73+ playgroundLink . className = "link open-in-playground" ;
74+ playgroundLink . href = `${ BASE_URL } ${ endpoint } =${ encodedContent } ${ exampleTraceURIComponent } ` ;
75+ playgroundLink . target = "_blank" ;
76+ playgroundLink . innerText = "⏵ Open In Playground" ;
7277
73- const agentLink = document . createElement ( "a" ) ;
74- agentLink . className = "link add-to-agent" ;
75- agentLink . href = `${ BASE_URL } deploy-guardrail#policy-code=${ encodeURIComponent (
76- textContent
77- ) } &name=${ encodeURIComponent ( findSnippetTitle ( codeElement ) ) } `;
78- agentLink . target = "_blank" ;
79- agentLink . innerText = "+ Add to Agent" ;
78+ const agentLink = document . createElement ( "a" ) ;
79+ agentLink . className = "link add-to-agent" ;
80+ agentLink . href = `${ BASE_URL } deploy-guardrail#policy-code=${ encodeURIComponent (
81+ textContent
82+ ) } &name=${ encodeURIComponent ( findSnippetTitle ( codeElement ) ) } `;
83+ agentLink . target = "_blank" ;
84+ agentLink . innerText = "+ Add to Agent" ;
8085
81- container . appendChild ( agentLink ) ;
82- container . appendChild ( playgroundLink ) ;
86+ container . appendChild ( agentLink ) ;
87+ container . appendChild ( playgroundLink ) ;
8388
84- codeElement . appendChild ( container ) ;
89+ codeElement . appendChild ( container ) ;
90+ } else {
91+ const id = crypto . randomUUID ( ) . toString ( ) ;
92+ const iframe = document . createElement ( "iframe" , { id : id } ) ;
93+ iframe . src = `${ BASE_URL } embed/${ endpoint } =${ encodedContent } &id=${ id } ` ;
94+ codeElement . replaceWith ( iframe ) ;
95+
96+ window . addEventListener ( "message" , function ( event ) {
97+ //check which element the message is coming from
98+ if ( event . data . type === "resize" && event . data . id === id ) {
99+ iframe . style . height = event . data . height + "px" ;
100+ }
101+ } ) ;
102+ }
85103 } ) ;
86104}
87105
@@ -100,7 +118,8 @@ document.addEventListener("DOMContentLoaded", function () {
100118 // currently disabled as the traceview endpoint is not yet enabled on explorer
101119 changeElements (
102120 document . querySelectorAll ( "div.language-trace" ) ,
103- "traceview?trace"
121+ "traceview?trace" ,
122+ true
104123 ) ;
105124 changeElements (
106125 document . querySelectorAll ( "div.language-guardrail" ) ,
0 commit comments