Configuration View
PromptConfigView is a Django class-based view responsible for providing configuration data to the ReactJS prompt UI component in the Smarter web application.
- class smarter.apps.prompt.views.detailviews.prompt_config_view.PromptConfigView(**kwargs)[source]
Bases:
SmarterOptionallyAuthenticatedApiViewPrompt configuration view for the Smarter web application.
This view is responsible for providing all configuration information required by the ReactJS prompt UI component. It is designed to be performant and efficient, as it is invoked on every user-facing browser load and refresh.
Key Features:
Django Template-Based: This view uses Django’s template system for rendering and does not rely on Django REST Framework (DRF) serializers for its main response. The configuration data is returned as a JSON response, but the view itself is structured as a Django class-based view.
CSRF Exempt: The view is decorated with @csrf_exempt because it is read-only and does not modify server-side data. This exemption is safe in this context and avoids unnecessary CSRF validation for GET and POST requests that only retrieve configuration data.
ReactJS UI Integration: The endpoint provides all necessary configuration and context information for the ReactJS prompt component. This includes llmclient metadata, plugin information, session keys, and historical data relevant to the prompt session. The React app consumes this configuration to initialize and render the prompt UI in the user’s browser.
Session-Based Architecture: Each prompt session is uniquely identified and managed by a globally unique 64-character session key. The session key is generated by RequestHelper.generate_session_key() and is persisted by the browser in a domain and path-specific cookie, meaning that this design supports an unlimited number of concurrent sessions per user, one per device per llmclient.
Performance Considerations: Since this endpoint is called on every browser load and refresh, performance is a primary concern. The view is optimized to minimize database queries and serialization overhead. Only a limited number of plugins (as defined by MAX_RETURNED_PLUGINS) are returned to avoid excessive payload sizes. Caching and efficient queryset usage are employed where possible to ensure fast response times for end users.
Example Usage:
- Returns:
- JSON response containing:
session_key: Unique identifier for the prompt session.
sandbox_mode: Boolean indicating if the llmclient is running in sandbox mode.
debug_mode: Boolean indicating if debug mode is enabled.
llmclient: Serialized llmclient configuration.
history: The session’s messages, and, with
?verbose=true, its tool calls.meta_data: The llmclient’s state, and, with
?verbose=true, the complete LLMClientHelper.plugins and extensions: Their counts, and, with
?verbose=true, a limited list of each.
See
verbose.- Security:
The view is protected and requires the user to be authenticated, unless the llmclient is configured to allow unauthenticated access.
If authentication is required and the user is not authenticated, a 403 Forbidden response is returned.
- See Also:
SmarterPromptSession: Helper class for managing prompt sessions.
LLMClientConfigSerializer: Serializer for the llmclient’s data.
LLMClientHelper: Helper for llmclient-related operations.
- authentication_classes = None
- chat_config()[source]
Assemble the configuration required by the ReactJS prompt UI component, as the contract’s model.
This method gathers all relevant context and configuration data for the prompt session and llmclient, and returns it as a
PromptChatApiConfig, the Pydantic model of version 2 of the Smarter Chat api contract.config()derives the response from it: the complete or public configuration, in version 2 or 1 of the contract.- Performance:
Since this endpoint is called on every browser load and refresh, the logic is optimized to minimize database queries and serialization overhead. The diagnostic parts, which the workbench’s Console displays, are built only for a client that asks for them (see
verbose), and only a limited number of plugins (seeMAX_RETURNED_PLUGINS) are returned.
- Returns:
The configuration, or None if the session or the LLMClient helper is not set. Its fields:
session_key,sandbox_mode,debug_mode,llmclient(serialized byLLMClientConfigSerializer, withurl_llmclientfromprompt_url()),history(chat_history()),meta_data(meta_data()),pluginsandextensions.- Return type:
- Raises:
pydantic.ValidationError – If the data breaks the contract.
See also
SmarterPromptSessionHelper class for managing prompt sessions.
LLMClientConfigSerializerSerializer for the llmclient’s data.
LLMClientHelperHelper for llmclient-related operations.
- chat_history()[source]
The chat session’s history, from its rows.
chat_history, the session’s messages, which restore the chat thread, always.prompt_tool_call_history, the session’s tool calls, ifverbose.prompt_plugin_usage_history,plugin_selector_historyandllmclient_request_historyfor the cli only (include_diagnostics). Smarter Chat doesn’t display them, and the requests, each of which holds the whole thread, grow with the square of the session’s length.
- Returns:
The history, empty without a session.
- Return type:
- chat_history_row: PromptHistory | None = None
The PromptHistory row that
chat_history()read the thread from, which the journal reads again.
- clean_url(url)[source]
Clean the url of any query strings and trailing ‘/config/’ strings.
- Return type:
- command: SmarterJournalCliCommands | None = None
- config()[source]
The configuration api’s response body:
chat_config(), in the form that the client receives.The complete configuration, or
PromptChatApiConfig.public()unlessfull_config.Version 2 of the contract to a client that asks for it, and to the cli (
legacy_keys=False), orPromptChatApiConfig.to_v1().
- config_response(request)[source]
The configuration, as the response to the client.
- Parameters:
request (
HttpRequest) – The request.- Returns:
The configuration, or an error response if it breaks the contract.
- Return type:
SmarterJournaledJsonResponse|SmarterJournaledJsonErrorResponse
- contract: int = 1
The version of the contract that the client asked for.
See
requested_contract().
- dispatch(request, *args, **kwargs)[source]
Handles incoming HTTP requests for the prompt configuration endpoint.
This method is responsible for orchestrating the retrieval and assembly of all configuration data required by the ReactJS prompt UI component. It is invoked on every user-facing browser load and refresh, making performance a critical concern.
Key Details:
Django Template-Based: This view uses Django’s class-based view and template system, not Django REST Framework (DRF). The response is a JSON object, but the view logic is not DRF-based.
CSRF Exempt: The view is decorated with
@csrf_exemptbecause it is strictly read-only and does not modify server-side data. This avoids unnecessary CSRF validation for GET and POST requests that only retrieve configuration data.ReactJS UI Integration: The endpoint provides all configuration and context information needed by the ReactJS prompt component, including llmclient metadata, plugin information, session keys, and prompt history.
Session-Based: Sessions are managed by the
SmarterPromptSessionhelper, which uniquely defines a prompt session using a combination of the user’s IP address and device-identifying information. This ensures each device/browser instance receives a unique session key, which is used to track prompt history and plugin usage.Performance: Since this endpoint is called on every browser load and refresh, it is optimized to minimize database queries and serialization overhead. Only a limited number of plugins are returned (see
MAX_RETURNED_PLUGINS) to keep payloads small and response times fast.
- Parameters:
request (
HttpRequest) – The incoming HTTP request object.llmclient_id (Optional[int], default=None) – The ID of the llmclient to retrieve configuration for, if specified.
*args – Additional positional arguments.
**kwargs – Additional keyword arguments.
- Returns:
A JSON response containing all configuration data required by the ReactJS prompt UI component, or an error response if the request is invalid or unauthorized.
- Return type:
JsonResponse|HttpResponse|SmarterJournaledJsonErrorResponse
See also
SmarterPromptSessionHelper class for managing prompt sessions.
LLMClientConfigSerializerSerializer for the llmclient’s data.
LLMClientHelperHelper for llmclient-related operations.
- dispatch_config(request, *args, **kwargs)[source]
Handle the request, for
dispatch(), which scopes the chat session key of its log records to it.- Parameters:
request (
HttpRequest) – The incoming HTTP request.- Return type:
JsonResponse|HttpResponse|SmarterJournaledJsonErrorResponse- Returns:
The configuration, or an error response.
- error_response(request, error)[source]
The configuration api’s error response, the same shape for every error.
- Parameters:
request (
HttpRequest) – The request.error (
PromptChatApiError) – The error.
- Returns:
The error response, without a stack trace.
- Return type:
- property formatted_class_name: str
Returns a formatted string of the class name for logging purposes.
- property full_config: bool
Whether the client receives the complete configuration.
The complete configuration includes the LLMClient’s every field, e.g. its owner’s profile, the session’s diagnostic histories, and the lists of plugins and extensions. It is returned when the LLMClient was found by its id or hashed id, i.e. by a workbench or platform api url, which checks that the user may read it, and to the cli. A deployed LLMClient’s own host, which pages that embed Smarter Chat call, receives
public_config().- Returns:
True for the complete configuration.
- Return type:
- get(request, *args, **kwargs)[source]
Get the llmclient configuration.
- include_diagnostics: bool = False
Return the complete configuration, whatever url resolved the LLMClient.
The cli passes as_view(include_diagnostics=True). See
full_config().
- legacy_keys: bool = True
Rename keys for older versions of the React app (‘llmclient’ to ‘chatbot’), in version 1 of the contract.
The cli passes as_view(legacy_keys=False).
- property llmclient: LLMClient | None
The LLMClient of this config request, set by the
llmclient_helpersetter.
- property llmclient_helper: LLMClientHelper | None
The LLMClientHelper of this request, created on first access and cached.
Built from the LLMClient when one is set, else from the LLMClient name of the view or the request.
- meta_data()[source]
The LLMClient’s state.
It is the complete
LLMClientHelper.to_json()ifverbose, and otherwise only the fields ofPUBLIC_META_DATA_FIELDSthat the helper has, which are what Smarter Chat reads.to_json()serializes the LLMClient a second time, with its tools, and the request.- Returns:
The meta data, empty without an LLMClient helper.
- Return type:
- permission_classes = (<class 'smarter.lib.drf.views.helpers.UnauthenticatedPermissionClass'>,)
- post(request, *args, **kwargs)[source]
Get the llmclient configuration.
- Return type:
SmarterJournaledJsonResponse|SmarterJournaledJsonErrorResponse
- prompt_url(url_llmclient)[source]
The url of the LLMClient’s prompt api, on the host that served the configuration.
A deployed LLMClient’s host, or its custom domain, also serves its prompt api, at
/prompt/, so that a page that embeds Smarter Chat needs access to one host only. A workbench or platform api url keeps the platform’s prompt api url.
- resolved_by_id: bool = False
True if the LLMClient was found by its id or hashed id, which also checks that the user may read it.
- session: SmarterPromptSession | None = None
- thing: SmarterJournalThings | None = None
- property verbose: bool
Whether the configuration includes its diagnostic parts, which the workbench’s Console displays.
They are the session’s tool calls, the complete
meta_data(LLMClientHelper.to_json()), and the lists of the LLMClient’s plugins and extensions. They cost database queries and serialization, and the tool calls grow with the session, so a client asks for them with?verbose=true(VERBOSE_PARAMETER) only when it displays them. Only a client that receives the complete configuration (full_config) may ask for them, and the cli always receives them.- Returns:
True for the diagnostic parts.
- Return type:
- smarter.apps.prompt.views.detailviews.prompt_config_view.should_log(level)[source]
Check if logging should be done based on the waffle switch.
- smarter.apps.prompt.views.detailviews.prompt_config_view.should_log_verbose(level)[source]
Check if logging should be done based on the waffle switch.