Skip to content

Commit 6a00fa8

Browse files
committed
fix: move variable validations from llm tool to page controllers
1 parent 529cf57 commit 6a00fa8

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

studio/ai/agent/tools/variables.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"""
1515

1616
import json
17-
import re
1817

1918
import frappe
2019

@@ -23,24 +22,17 @@
2322
from studio.ai.block_codec import BlockCodec
2423

2524
VARIABLE_TYPES = ("String", "Number", "Boolean", "Object")
26-
# A binding name has to be a bare JS identifier — it's spread into the eval context
27-
# and referenced as {{ <name> }}.
28-
VARIABLE_NAME_REGEX = re.compile(r"^[A-Za-z_$][A-Za-z0-9_$]*$")
2925

3026

3127
def run_add_variable(ctx, args: dict) -> str:
3228
name = text_arg(args.get("variable_name"))
3329
var_type = text_arg(args.get("variable_type")) or "String"
34-
if error := _validate_name(name):
35-
return f"FAILED: {error}"
3630
if var_type not in VARIABLE_TYPES:
3731
return f"FAILED: variable_type must be one of {list(VARIABLE_TYPES)}."
3832

3933
page = load_page(ctx)
4034
if page is None:
4135
return "FAILED: no page in context."
42-
if _find_variable(page, name):
43-
return f"FAILED: a variable named '{name}' already exists. Use update_variable to change it."
4436

4537
page.append(
4638
"variables",
@@ -117,14 +109,6 @@ def run_delete_variable(ctx, args: dict) -> str:
117109
# --- helpers --------------------------------------------------------------
118110

119111

120-
def _validate_name(name: str) -> str | None:
121-
if not name:
122-
return "variable_name is required."
123-
if not VARIABLE_NAME_REGEX.match(name):
124-
return f"'{name}' is not a valid variable name — use letters, digits and underscores, starting with a letter."
125-
return None
126-
127-
128112
def _find_variable(page, name: str):
129113
if not name:
130114
return None

studio/studio/doctype/studio_page/studio_page.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) 2024, Frappe Technologies Pvt Ltd and contributors
22
# For license information, please see license.txt
33
import os
4+
import re
45

56
import frappe
67
from frappe import _
@@ -17,6 +18,10 @@
1718
)
1819
from studio.utils import camel_case_to_kebab_case
1920

21+
# A variable is referenced as {{ name }} and spread into the page's JS eval context, so its
22+
# name must be a bare JS identifier.
23+
VARIABLE_NAME_REGEX = re.compile(r"^[A-Za-z_$][A-Za-z0-9_$]*$")
24+
2025

2126
class StudioPage(Document):
2227
# begin: auto-generated types
@@ -200,6 +205,14 @@ def validate_variables(self):
200205
if duplicate_variable_names:
201206
frappe.throw(_("Duplicate variable name: {0}").format(", ".join(duplicate_variable_names)))
202207

208+
for variable in self.variables:
209+
if not VARIABLE_NAME_REGEX.match(variable.variable_name or ""):
210+
frappe.throw(
211+
_(
212+
"Invalid variable name '{0}' — use letters, digits and underscores, starting with a letter."
213+
).format(variable.variable_name)
214+
)
215+
203216
def process_resources(self):
204217
for resource in self.resources:
205218
self.validate_resources(resource)

0 commit comments

Comments
 (0)