Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

mitigate settings rendering lag of GenAI LLM for country and timezone list

Forhad Hosain94aefd079070
diff --git a/packages/app/src/builder-ui/components/GenAILLM.class.ts b/packages/app/src/builder-ui/components/GenAILLM.class.ts
index e34ffdc9..55b08f82 100644
--- a/packages/app/src/builder-ui/components/GenAILLM.class.ts
+++ b/packages/app/src/builder-ui/components/GenAILLM.class.ts
@@ -1,20 +1,40 @@
// TODO: Refactor this file (specially setting fields for different providers)
import { LLMRegistry } from '../../shared/services/LLMRegistry.service';
-import { REASONING_EFFORTS } from '../constants';
+import { LLM_PROVIDERS, REASONING_EFFORTS } from '../constants';
import { LLMFormController } from '../helpers/LLMFormController.helper';
import llmParams from '../params/LLM.params.json';
import { createBadge } from '../ui/badges';
+import { registerDatalistOptions } from '../ui/form/fields';
import { IconArrowRight, IconConfigure } from '../ui/icons';
import { refreshLLMModels, saveApiKey, setupSidebarTooltips } from '../utils';
import { delay } from '../utils/general.utils';
import { Component } from './Component.class';
-// Since getWebSearchFields is called multiple times, using a static import is more efficient than a lazy import.
+// Import modules statically - this happens at module load but doesn't process arrays yet
import ianaTimezones from '../params/IANA-time-zones';
import isoCountryCodes from '../params/ISO-country-code';
declare var Metro;
+/**
+ * Register datalist options with lazy functions.
+ * The arrays are imported but only processed when the datalist is actually needed (on first focus).
+ * This prevents blocking during field generation.
+ */
+registerDatalistOptions('country-datalist', () =>
+ isoCountryCodes.map((country) => ({
+ value: country.value,
+ text: `${country.value} - ${country.text}`, // Show "US - United States"
+ })),
+);
+registerDatalistOptions('xai-country-datalist', () =>
+ isoCountryCodes.map((country) => ({
+ value: country.value,
+ text: `${country.value} - ${country.text}`, // Show "US - United States"
+ })),
+);
+registerDatalistOptions('timezone-datalist', () => ianaTimezones.map((tz) => ({ text: tz, value: tz })));
+
/*
* Here field name like model, apiKey, temperature is very important
* Because we have ID like #model, #apiKey, #temperature in the HTML
@@ -225,15 +245,13 @@ export class GenAILLM extends Component {
await delay(200);
await setupSidebarTooltips(sidebar, this);
- const useWebSearchElm = form?.querySelector('#useWebSearch') as HTMLInputElement;
- if (useWebSearchElm) {
- this.toggleWebSearchNestedFields(useWebSearchElm, form);
- }
-
- const useSearchElm = form?.querySelector('#useSearch') as HTMLInputElement;
- if (useSearchElm) {
- this.toggleSearchNestedFields(useSearchElm, form);
- }
+ // Auto-set search options based on current model's provider
+ const modelSelect = form?.querySelector('#model') as HTMLSelectElement;
+ const currentModel = modelSelect?.value || this.data.model;
+ const provider = LLMRegistry.getModelProvider(currentModel);
+
+ // Automatically switch search options based on provider
+ this.autoSwitchSearchOptions(provider, form);
const useReasoningElm = form?.querySelector('#useReasoning') as HTMLInputElement;
if (useReasoningElm) {
@@ -368,28 +386,20 @@ export class GenAILLM extends Component {
}
// #endregion
- // #region Toggle web search nested fields on model change
+ // #region Auto-set search options based on provider
const form = currentElement.closest('form');
- const useWebSearchElm = form?.querySelector('#useWebSearch') as HTMLInputElement;
-
- if (useWebSearchElm) {
- this.toggleWebSearchNestedFields(useWebSearchElm, form);
- }
- // #endregion
-
- // #region Toggle search nested fields on model change
- const useSearchElm = form?.querySelector('#useSearch') as HTMLInputElement;
- if (useSearchElm) {
- this.toggleSearchNestedFields(useSearchElm, form);
- }
+ const provider = LLMRegistry.getModelProvider(currentElement.value);
+
+ // Automatically switch search options based on provider
+ this.autoSwitchSearchOptions(provider, form);
// #endregion
// #region Hide provider-specific fields when switching models
- const provider = LLMRegistry.getModelProvider(currentElement.value);
- if (provider.toLowerCase() !== 'xai') {
+ const providerLower = provider.toLowerCase();
+ if (providerLower !== LLM_PROVIDERS.XAI.toLowerCase()) {
this.hideXAISpecificFields(form);
}
- if (provider.toLowerCase() !== 'openai') {
+ if (providerLower !== LLM_PROVIDERS.OPENAI.toLowerCase()) {
this.hideOpenAIWebSearchFields(form);
}
// #endregion
@@ -835,7 +845,7 @@ export class GenAILLM extends Component {
fieldsGroup: 'Reasoning Effort',
},
- ...(await this.getWebSearchFields()),
+ ...this.getWebSearchFields(),
};
}
@@ -855,22 +865,19 @@ export class GenAILLM extends Component {
}
}
- private async getWebSearchFields() {
- const countryOptions = [{ text: 'Select a Country', value: '' }, ...isoCountryCodes];
- const timezoneOptions = [{ text: 'Select a Timezone', value: '' }, ...ianaTimezones];
-
+ private getWebSearchFields() {
// Include all search models, but exclude xAI for the generic useWebSearch field
const webSearchSupportedModels = [...this.searchModels].join(',');
const openAISearchModels = this.searchModels
.filter((model) => {
const provider = LLMRegistry.getModelProvider(model);
- return provider.toLowerCase() !== 'xai';
+ return provider.toLowerCase() !== LLM_PROVIDERS.XAI.toLowerCase();
})
.join(',');
const xAISearchModels = this.searchModels
.filter((model) => {
const provider = LLMRegistry.getModelProvider(model);
- return provider.toLowerCase() === 'xai';
+ return provider.toLowerCase() === LLM_PROVIDERS.XAI.toLowerCase();
})
.join(',');
@@ -899,6 +906,16 @@ export class GenAILLM extends Component {
const target = event.target as HTMLInputElement;
const form = target.closest('form');
+ // Make useWebSearch and useSearch mutually exclusive
+ if (target.checked) {
+ const useSearchElm = form?.querySelector('#useSearch') as HTMLInputElement;
+ if (useSearchElm && useSearchElm.checked) {
+ useSearchElm.checked = false;
+ // Hide useSearch nested fields
+ this.toggleSearchNestedFields(useSearchElm, form);
+ }
+ }
+
this.toggleWebSearchNestedFields(target, form);
},
},
@@ -950,15 +967,22 @@ export class GenAILLM extends Component {
fieldsGroup: 'Location',
},
webSearchCountry: {
- type: 'select',
+ type: 'datalist',
label: 'Country',
value: '',
- attributes: openAIAttributes,
+ datalistId: 'country-datalist',
+ attributes: {
+ ...openAIAttributes,
+ placeholder: 'Type to search countries (e.g., US)',
+ autocomplete: 'off',
+ maxlength: '2',
+ },
class: 'hidden',
- options: countryOptions,
- help: "Country: a two-letter ISO country code, like 'US'.",
+ help: "Country: a two-letter ISO country code, like 'US'. Type to search.",
section: 'Advanced',
fieldsGroup: 'Location',
+ validate: 'maxlength=2',
+ validateMessage: 'Country code must be 2 characters',
},
webSearchRegion: {
type: 'text',
@@ -973,13 +997,17 @@ export class GenAILLM extends Component {
fieldsGroup: 'Location',
},
webSearchTimezone: {
- type: 'select',
+ type: 'datalist',
label: 'Timezone',
value: '',
- attributes: openAIAttributes,
+ datalistId: 'timezone-datalist',
+ attributes: {
+ ...openAIAttributes,
+ placeholder: 'Type to search timezones (e.g., America/Chicago)',
+ autocomplete: 'off',
+ },
class: 'hidden',
- options: timezoneOptions,
- help: 'Timezone: an IANA timezone like America/Chicago.',
+ help: 'Timezone: an IANA timezone like America/Chicago. Type to search.',
section: 'Advanced',
fieldsGroup: 'Location',
},
@@ -997,6 +1025,16 @@ export class GenAILLM extends Component {
const target = event.target as HTMLInputElement;
const form = target.closest('form');
+ // Make useSearch and useWebSearch mutually exclusive
+ if (target.checked) {
+ const useWebSearchElm = form?.querySelector('#useWebSearch') as HTMLInputElement;
+ if (useWebSearchElm && useWebSearchElm.checked) {
+ useWebSearchElm.checked = false;
+ // Hide useWebSearch nested fields
+ this.toggleWebSearchNestedFields(useWebSearchElm, form);
+ }
+ }
+
this.toggleSearchNestedFields(target, form);
},
},
@@ -1120,15 +1158,22 @@ export class GenAILLM extends Component {
fieldsGroup: 'Data Sources',
},
searchCountry: {
- type: 'select',
+ type: 'datalist',
label: 'Country',
value: '',
- attributes: xAIAttributes,
+ datalistId: 'xai-country-datalist',
+ attributes: {
+ ...xAIAttributes,
+ placeholder: 'Type to search countries (e.g., US)',
+ autocomplete: 'off',
+ maxlength: '2',
+ },
class: 'hidden',
- options: countryOptions,
- help: 'Select a country to limit search results to.',
+ help: 'Country code to limit search results to (e.g., US). Type to search.',
section: 'Advanced',
fieldsGroup: 'Data Sources',
+ validate: 'maxlength=2',
+ validateMessage: 'Country code must be 2 characters',
},
excludedWebsites: {
type: 'tag',
@@ -1280,14 +1325,57 @@ export class GenAILLM extends Component {
});
}
+ /**
+ * Enforces mutually exclusive search options based on the model's provider.
+ * Only disables incompatible search options, doesn't automatically enable them.
+ *
+ * @param provider - The LLM provider name
+ * @param form - The form element containing the search checkboxes
+ */
+ private autoSwitchSearchOptions(provider: string, form: HTMLFormElement): void {
+ const useWebSearchElm = form?.querySelector('#useWebSearch') as HTMLInputElement;
+ const useSearchElm = form?.querySelector('#useSearch') as HTMLInputElement;
+
+ const providerLower = provider.toLowerCase();
+
+ if (providerLower === LLM_PROVIDERS.OPENAI.toLowerCase()) {
+ // OpenAI models: disable useSearch (keep useWebSearch as is)
+ if (useSearchElm && useSearchElm.checked) {
+ useSearchElm.checked = false;
+ }
+ } else if (providerLower === LLM_PROVIDERS.XAI.toLowerCase()) {
+ // XAI models: disable useWebSearch (keep useSearch as is)
+ if (useWebSearchElm && useWebSearchElm.checked) {
+ useWebSearchElm.checked = false;
+ }
+ } else {
+ // Other providers: disable both
+ if (useWebSearchElm && useWebSearchElm.checked) {
+ useWebSearchElm.checked = false;
+ }
+ if (useSearchElm && useSearchElm.checked) {
+ useSearchElm.checked = false;
+ }
+ }
+
+ // Toggle visibility of nested fields
+ if (useWebSearchElm) {
+ this.toggleWebSearchNestedFields(useWebSearchElm, form);
+ }
+ if (useSearchElm) {
+ this.toggleSearchNestedFields(useSearchElm, form);
+ }
+ }
+
/**
* Get the provider-specific fields based on the current model
- * @param provider - The LLM provider (e.g., 'openai', 'xai')
+ * @param provider - The LLM provider (e.g., 'OpenAI', 'xAI')
* @returns Array of field names to show for the provider
*/
private getProviderWebSearchFields(provider: string): string[] {
- const providerFieldMap = {
- openai: [
+ const providerLower = provider.toLowerCase();
+ const providerFieldMap: Record<string, string[]> = {
+ [LLM_PROVIDERS.OPENAI.toLowerCase()]: [
'webSearchContextSize',
'locationTitle',
'webSearchCity',
@@ -1295,7 +1383,7 @@ export class GenAILLM extends Component {
'webSearchRegion',
'webSearchTimezone',
],
- xai: [
+ [LLM_PROVIDERS.XAI.toLowerCase()]: [
'searchMode',
'returnCitations',
'maxSearchResults',
@@ -1315,7 +1403,7 @@ export class GenAILLM extends Component {
// Add other providers as needed
};
- return providerFieldMap[provider.toLowerCase()] || [];
+ return providerFieldMap[providerLower] || [];
}
/**
diff --git a/packages/app/src/builder-ui/constants.ts b/packages/app/src/builder-ui/constants.ts
index 3ebdaa9c..28289609 100644
--- a/packages/app/src/builder-ui/constants.ts
+++ b/packages/app/src/builder-ui/constants.ts
@@ -6,6 +6,7 @@ export const JSON_FIELD_CLASS = '_smythos_json_field';
export enum LLM_PROVIDERS {
OPENAI = 'OpenAI',
+ XAI = 'xAI',
RUNWARE = 'Runware',
GOOGLEAI = 'GoogleAI',
}
diff --git a/packages/app/src/builder-ui/ui/form/createFormField.ts b/packages/app/src/builder-ui/ui/form/createFormField.ts
index 8e7c1265..9a7d317b 100644
--- a/packages/app/src/builder-ui/ui/form/createFormField.ts
+++ b/packages/app/src/builder-ui/ui/form/createFormField.ts
@@ -11,6 +11,7 @@ import {
createCheckbox,
createCheckboxGroup,
createColorInput,
+ createDatalistInput,
createHiddenInput,
createHint,
createInfoButton,
@@ -296,6 +297,17 @@ export default function createFormField(entry, displayType = 'block', entryIndex
break;
+ case 'datalist':
+ case 'DATALIST':
+ // Create text input with datalist support for autocomplete
+ // The datalist is lazily created on first focus using options from the global registry
+ // This prevents storing large arrays in field definitions
+ const datalistId = entry.datalistId || `${entry.name}-datalist`;
+ formElement = createDatalistInput(value, datalistId);
+ formElement.classList.add('form-control');
+
+ break;
+
default:
formElement = createInput(value);
diff --git a/packages/app/src/builder-ui/ui/form/fields.ts b/packages/app/src/builder-ui/ui/form/fields.ts
index 414e81da..38760a1b 100644
--- a/packages/app/src/builder-ui/ui/form/fields.ts
+++ b/packages/app/src/builder-ui/ui/form/fields.ts
@@ -6,6 +6,27 @@ import { delay } from '../../utils';
declare var Metro;
+/**
+ * Global registry for datalist options.
+ * Options are stored as functions to avoid loading large data modules during app initialization.
+ * The functions are called synchronously on first focus to ensure immediate availability.
+ */
+const datalistRegistry: Map<string, () => Array<string | { text: string; value: string }>> = new Map();
+
+/**
+ * Register a datalist options provider by datalist ID.
+ * The provider function is called synchronously when the field is first focused.
+ *
+ * @param datalistId - Unique ID for the datalist
+ * @param optionsProvider - Function that returns the options array
+ */
+export const registerDatalistOptions = (
+ datalistId: string,
+ optionsProvider: () => Array<string | { text: string; value: string }>,
+): void => {
+ datalistRegistry.set(datalistId, optionsProvider);
+};
+
export const createInput = (value: string = ''): HTMLInputElement => {
const fieldElm = document.createElement('input');
fieldElm.setAttribute('data-role', 'input');
@@ -22,6 +43,88 @@ export const createHiddenInput = (value: string = ''): HTMLInputElement => {
return fieldElm;
};
+/**
+ * Creates a text input with datalist autocomplete support.
+ * The datalist is created synchronously on first user interaction (mousedown, touchstart, or focus).
+ * Using mousedown ensures the datalist is ready BEFORE focus, making it work on the first try.
+ * This keeps initial render fast while ensuring the field works immediately.
+ *
+ * @param value - Initial value for the input
+ * @param datalistId - ID for the datalist element (used to lookup options in registry)
+ * @returns HTMLInputElement with datalist support
+ */
+export const createDatalistInput = (value: string = '', datalistId?: string): HTMLInputElement => {
+ const fieldElm = document.createElement('input');
+ fieldElm.setAttribute('data-role', 'input');
+ fieldElm.setAttribute('type', 'text');
+ fieldElm.setAttribute('value', value || '');
+
+ // Set up datalist if datalistId is provided
+ if (datalistId) {
+ fieldElm.setAttribute('list', datalistId);
+
+ let datalistCreated = false;
+
+ // Create datalist on first interaction - synchronously so it's ready immediately
+ const createDatalistOnce = (): void => {
+ // Prevent multiple calls
+ if (datalistCreated) {
+ return;
+ }
+
+ // Check if datalist already exists
+ if (document.getElementById(datalistId)) {
+ datalistCreated = true;
+ return;
+ }
+
+ // Get options provider from registry
+ const optionsProvider = datalistRegistry.get(datalistId);
+ if (!optionsProvider) {
+ console.warn(`Datalist options not registered for ID: ${datalistId}`);
+ datalistCreated = true;
+ return;
+ }
+
+ // Create datalist SYNCHRONOUSLY (no setTimeout/requestIdleCallback)
+ // This ensures the datalist exists before the browser tries to show the dropdown
+ const options = optionsProvider();
+
+ const datalist = document.createElement('datalist');
+ datalist.id = datalistId;
+
+ // Use DocumentFragment for batch DOM operations (much faster)
+ const fragment = document.createDocumentFragment();
+
+ options.forEach((option) => {
+ const optionEl = document.createElement('option');
+ if (typeof option === 'string') {
+ optionEl.value = option;
+ } else {
+ optionEl.value = option.value;
+ if (option.text) {
+ optionEl.textContent = option.text;
+ }
+ }
+ fragment.appendChild(optionEl);
+ });
+
+ datalist.appendChild(fragment);
+ document.body.appendChild(datalist);
+ datalistCreated = true;
+ };
+
+ // Listen to multiple events to catch interaction as early as possible
+ // mousedown fires BEFORE focus, ensuring datalist is ready
+ fieldElm.addEventListener('mousedown', createDatalistOnce, { once: true });
+ fieldElm.addEventListener('focus', createDatalistOnce, { once: true });
+ // Also handle touch devices
+ fieldElm.addEventListener('touchstart', createDatalistOnce, { once: true, passive: true });
+ }
+
+ return fieldElm;
+};
+
/*
* Note:
* Currently, the 'options()' function does not support badges. To enable this feature,