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: SmarterOptionallyAuthenticatedApiView

Prompt 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: Prompt and plugin selector history for the session.

  • meta_data: Additional metadata for the llmclient.

  • plugins: Plugin metadata and a limited list of plugins.

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. Only a limited number of plugins (see MAX_RETURNED_PLUGINS) are returned to keep payloads small and response times fast.

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 by LLMClientConfigSerializer, with url_llmclient from prompt_url()), history (the session’s messages in chat_history, and its diagnostic histories), meta_data (LLMClientHelper.to_json()), plugins and functions.

Return type:

PromptChatApiConfig | None

Raises:

pydantic.ValidationError – If the data breaks the contract.

See also

SmarterPromptSession

Helper class for managing prompt sessions.

LLMClientConfigSerializer

Serializer for the llmclient’s data.

LLMClientHelper

Helper for llmclient-related operations.

clean_url(url)[source]

Clean the url of any query strings and trailing ‘/config/’ strings.

Return type:

str

command: SmarterJournalCliCommands | None = None
config()[source]

The configuration api’s response body: chat_config(), in the form that the client receives.

Returns:

{"data": <the configuration>}, or {} without a session or LLMClient helper.

Return type:

dict[str, Any]

Raises:

pydantic.ValidationError – If the configuration breaks the contract.

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_exempt because 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 SmarterPromptSession helper, 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

SmarterPromptSession

Helper class for managing prompt sessions.

LLMClientConfigSerializer

Serializer for the llmclient’s data.

LLMClientHelper

Helper for llmclient-related operations.

error_response(request, error)[source]

The configuration api’s error response, the same shape for every error.

See chat_error_response().

Parameters:
Returns:

The error response, without a stack trace.

Return type:

SmarterJournaledJsonErrorResponse

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 functions. 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:

bool

get(request, *args, **kwargs)[source]

Get the llmclient configuration.

Return type:

SmarterJournaledJsonResponse | SmarterJournaledJsonErrorResponse | HttpResponseNotAllowed

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
property llmclient_helper: LLMClientHelper | None
llmclient_name: str | None = None
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.

Parameters:

url_llmclient (str | None) – The LLMClient’s prompt api url on the platform, LLMClient.url_llmclient.

Returns:

The prompt api url to return to the client.

Return type:

str | None

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
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.