MCPcopy Create free account
hub / github.com/CopilotKit/OpenGenerativeUI / apply_template

Function apply_template

apps/agent/src/templates.py:162–214  ·  view source on GitHub ↗

Retrieve a saved template's HTML so you can adapt it with new data. After calling this, generate a NEW widget in the same style and render via generateSandboxedUi. This tool automatically checks for a pending_template in state (set by the frontend when the user picks a template fro

(runtime: ToolRuntime, name: str = "", template_id: str = "")

Source from the content-addressed store, hash-verified

160
161@tool
162def apply_template(runtime: ToolRuntime, name: str = "", template_id: str = ""):
163 """
164 Retrieve a saved template's HTML so you can adapt it with new data.
165 After calling this, generate a NEW widget in the same style and render via generateSandboxedUi.
166
167 This tool automatically checks for a pending_template in state (set by the
168 frontend when the user picks a template from the library). If pending_template
169 is present, it takes priority over name/template_id arguments.
170
171 Also searches built-in seed templates, so users can apply them by name in chat
172 even if the frontend hasn't pushed them into agent state yet.
173
174 Args:
175 name: The name of the template to apply (fallback if no pending_template)
176 template_id: The ID of the template to apply (fallback if no pending_template)
177 """
178 state_templates = runtime.state.get("templates", [])
179 state_ids = {t["id"] for t in state_templates}
180 templates = [*state_templates, *(s for s in SEED_TEMPLATES if s["id"] not in state_ids)]
181
182 # Check pending_template from frontend first — this is the most reliable source
183 pending = runtime.state.get("pending_template")
184 if pending and pending.get("id"):
185 template_id = pending["id"]
186
187 # Look up by ID first
188 if template_id:
189 for t in templates:
190 if t["id"] == template_id:
191 return {
192 "name": t["name"],
193 "description": t["description"],
194 "html": t["html"],
195 "data_description": t.get("data_description", ""),
196 "usage_note": TEMPLATE_USAGE_NOTE,
197 }
198 return {"error": f"Template with id '{template_id}' not found"}
199
200 # Look up by name (most recent match)
201 if name:
202 matches = [t for t in templates if t["name"].lower() == name.lower()]
203 if matches:
204 t = max(matches, key=lambda x: x.get("created_at", ""))
205 return {
206 "name": t["name"],
207 "description": t["description"],
208 "html": t["html"],
209 "data_description": t.get("data_description", ""),
210 "usage_note": TEMPLATE_USAGE_NOTE,
211 }
212 return {"error": f"No template named '{name}' found"}
213
214 return {"error": "Provide either a name or template_id"}
215
216
217@tool

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected