1
1
// 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/" ;
3
7
4
8
function encodeGuardrailURIComponent ( content ) {
5
9
/**
@@ -41,10 +45,10 @@ function findSnippetTitle(codeElement) {
41
45
return "New Guardrail" ;
42
46
}
43
47
44
- function changeElements ( codeElements , endpoint ) {
48
+ function changeElements ( codeElements , endpoint , embed = false ) {
45
49
// Add a button to each pre element
46
50
codeElements . forEach ( function ( codeElement ) {
47
- // replace the code element with an iframe
51
+ // augment the code element
48
52
let textContent = codeElement . textContent || codeElement . innerText ;
49
53
50
54
// parse and split contents
@@ -60,28 +64,42 @@ function changeElements(codeElements, endpoint) {
60
64
) ;
61
65
}
62
66
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" ;
66
71
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" ;
72
77
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" ;
80
85
81
- container . appendChild ( agentLink ) ;
82
- container . appendChild ( playgroundLink ) ;
86
+ container . appendChild ( agentLink ) ;
87
+ container . appendChild ( playgroundLink ) ;
83
88
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
+ }
85
103
} ) ;
86
104
}
87
105
@@ -100,7 +118,8 @@ document.addEventListener("DOMContentLoaded", function () {
100
118
// currently disabled as the traceview endpoint is not yet enabled on explorer
101
119
changeElements (
102
120
document . querySelectorAll ( "div.language-trace" ) ,
103
- "traceview?trace"
121
+ "traceview?trace" ,
122
+ true
104
123
) ;
105
124
changeElements (
106
125
document . querySelectorAll ( "div.language-guardrail" ) ,
0 commit comments