MCPcopy
hub / github.com/Skyvern-AI/skyvern / load_prompt_with_elements_tracked

Function load_prompt_with_elements_tracked

skyvern/utils/prompt_engine.py:71–187  ·  view source on GitHub ↗

Same as load_prompt_with_elements but also returns post-ceiling kwargs. The returned kwargs dict reflects every fallback key that was set to None to bring the prompt under the hard ceiling. Callers that hash prompt inputs for caching should use these values instead of the pre-drop kwarg

(
    element_tree_builder: ElementTreeBuilder,
    prompt_engine: PromptEngine,
    template_name: str,
    html_need_skyvern_attrs: bool = True,
    *,
    # SKY-9718 Layer 1 — deterministic lean-tree transforms. Each flag toggles
    # one transform independently. Callers decide which to enable (typically by
    # AND-ing with `skyvern_context.current().enable_lean_element_tree` if they
    # want the experiment gate). To drop Skyvern internal IDs from the rendered
    # HTML, callers pass `html_need_skyvern_attrs=False` — that's the existing
    # `json_to_html` mechanism and stacks on top of any lean flags chosen here.
    lean_compress_long_href: bool = False,
    lean_compress_image_src: bool = False,
    lean_strip_url_query_strings: bool = False,
    lean_compress_nonnavigable_href: bool = False,
    **kwargs: Any,
)

Source from the content-addressed store, hash-verified

69
70
71def load_prompt_with_elements_tracked(
72 element_tree_builder: ElementTreeBuilder,
73 prompt_engine: PromptEngine,
74 template_name: str,
75 html_need_skyvern_attrs: bool = True,
76 *,
77 # SKY-9718 Layer 1 — deterministic lean-tree transforms. Each flag toggles
78 # one transform independently. Callers decide which to enable (typically by
79 # AND-ing with `skyvern_context.current().enable_lean_element_tree` if they
80 # want the experiment gate). To drop Skyvern internal IDs from the rendered
81 # HTML, callers pass `html_need_skyvern_attrs=False` — that's the existing
82 # `json_to_html` mechanism and stacks on top of any lean flags chosen here.
83 lean_compress_long_href: bool = False,
84 lean_compress_image_src: bool = False,
85 lean_strip_url_query_strings: bool = False,
86 lean_compress_nonnavigable_href: bool = False,
87 **kwargs: Any,
88) -> tuple[str, dict[str, Any]]:
89 """Same as load_prompt_with_elements but also returns post-ceiling kwargs.
90
91 The returned kwargs dict reflects every fallback key that was set to None
92 to bring the prompt under the hard ceiling. Callers that hash prompt
93 inputs for caching should use these values instead of the pre-drop kwargs
94 so two requests that render to the same final prompt share a cache key.
95 """
96 lean_any = (
97 lean_compress_long_href
98 or lean_compress_image_src
99 or lean_strip_url_query_strings
100 or lean_compress_nonnavigable_href
101 )
102 if lean_any and element_tree_builder.support_lean_elements_tree():
103 elements = _sanitize_elements_for_prompt(
104 element_tree_builder,
105 element_tree_builder.build_lean_elements_tree(
106 html_need_skyvern_attrs=html_need_skyvern_attrs,
107 compress_long_href=lean_compress_long_href,
108 compress_image_src=lean_compress_image_src,
109 strip_url_query_strings=lean_strip_url_query_strings,
110 compress_nonnavigable_href=lean_compress_nonnavigable_href,
111 ),
112 )
113 else:
114 # Builder doesn't implement lean (e.g. IncrementalScrapePage) or caller
115 # asked for no transforms — fall back to the plain element tree.
116 elements = _sanitize_elements_for_prompt(
117 element_tree_builder,
118 element_tree_builder.build_element_tree(html_need_skyvern_attrs=html_need_skyvern_attrs),
119 )
120 prompt = prompt_engine.load_prompt(
121 template_name,
122 elements=elements,
123 **kwargs,
124 )
125 token_count = count_tokens(prompt)
126 if token_count > DEFAULT_MAX_TOKENS and element_tree_builder.support_economy_elements_tree():
127 # get rid of all the secondary elements like SVG, etc
128 # NOTE: economy fallback drops the lean recipe — context-overflow firefighting

Calls 10

count_tokensFunction · 0.90
load_promptMethod · 0.80
build_element_treeMethod · 0.45
warningMethod · 0.45