{% extends "admin/base.html" %} {%- macro purpose_test_summary(purpose_tests) -%} {#- What "Test all purposes" or one extension's test answered: one line; each row says what it answered. -#} {% set failed = purpose_tests.values() | rejectattr('success') | list %} {% if not purpose_tests %}
{{ _('No installed extension names a purpose, so there was nothing to test.') }}
{% elif failed %}
{{ _('{failed} of {count} purposes did not answer. See the rows below.').format(failed=failed | length, count=purpose_tests | length) }}
{% else %}
{{ ungettext('The purpose answered. Nothing was saved.', 'All {count} purposes answered. Nothing was saved.', purpose_tests | length).format(count=purpose_tests | length) }}
{% endif %} {%- endmacro -%} {% set labels = { 'model': _('Default model'), 'api_key_env': _('API key'), 'timeout_seconds': _('Time limit for an ordinary call, seconds'), 'page_read_timeout_seconds': _('Time limit for reading a web page, seconds'), } %} {% set by_name = {} %} {% for row in rows %}{% do by_name.update({row.name: row}) %}{% endfor %} {% macro key_text(k) %}{{ k.name }} – {% if k.source == 'page' %}{{ _('entered here') }}{% elif k.source == 'server' %}{{ _('set on the server') }}{% else %}{{ _('missing') }}{% endif %}{% if k.tail %} (…{{ k.tail }}){% endif %}{% if k.unreadable %}, {{ _('a key entered here cannot be read, enter it again') }}{% endif %}{% endmacro %} {% macro origin(name) %} {% set row = by_name[name] %} {% if row.saved and row.default %} {# Only when it matters: the server has a value of its own, and this one goes before it. #} {{ _('Set on this page. Without the setting on this page, the server\'s setting applies: {value}').format(value=row.default) }} {% endif %} {% for e in errors[name] or [] %}{{ e }}{% endfor %} {% endmacro %} {% block primary_content_inner %} {% asset 'ai_connector/ai-connector-settings' %}

{{ _('AI settings') }}

{{ _('The connection to an AI service that the portal\'s extensions share: model, key and time limits. Changes apply at once, for the whole portal, without a restart. Where nothing is saved here, the server default applies. What each extension asks the AI to do is set on that extension\'s own pages.') }}

{% set problems = checks | selectattr('status', 'equalto', 'error') | list %}

{{ _('Setup') }}

{% if problems %}{{ _('Something needs to be done before everything works:') }} {% else %}{{ _('Everything needed is in place.') }}{% endif %}

{% if resettable and confirm_reset %} {# The second step: what goes and what replaces it, before anything changes. #} {% endif %} {% if test and not test.purpose_id %} {% if test.success %}
{% if test.purpose %}{{ test.purpose }}:{% endif %} {% trans model=test.model, seconds=h.localised_number(test.seconds) %}The AI answered in {{ seconds }} s with the model {{ model }}.{% endtrans %} {% if test.answer %}
{{ _('Test answer:') }} {{ test.answer }}{% endif %}
{% else %}
{% if test.purpose %}{{ test.purpose }}:{% endif %} {{ _('The connection test failed:') }} {{ test.error }} {% if test.hint %}
{{ test.hint }}{% endif %} {% if test.detail %}
{{ test.detail }}{% endif %}
{% endif %} {% endif %} {% set keys_section %} {% set entered = keys | selectattr('source', 'equalto', 'page') | list + keys | selectattr('unreadable') | list %} {% set on_server = keys | selectattr('source', 'equalto', 'server') | list %} {% set key_errors = errors.custom_name or errors.custom_key or errors.custom_confirm or errors.remove_key %} {#- Everything about the API keys in one fold: the keys there are (entered here, and the server's), then adding or replacing one. It sits inside the connection form, so its fields belong to two forms of their own outside it (the form attribute; HTML has no nested forms): ai-key-remove and ai-key-add, at the end of the page. -#} {% if entered or on_server or can_store_keys %}
{{ _('Manage API keys') }}
{% if entered or on_server %} {% for e in errors.remove_key or [] %}{{ e }}{% endfor %} {% elif can_store_keys %}

{{ _('None yet.') }}

{% endif %} {% if can_store_keys %}

{{ _('Add an API key') }}

{{ _('Adds a key without changing the model, for example before you switch service. The key is stored encrypted, goes only to the service you choose as the model, and is never shown again, only its last four characters.') }}

{#- Chosen first: what was sent (after an error), else the first service without a key. -#} {% set without_key = key_services | rejectattr('set') | list %} {% set chosen_service = request.form.get('custom_service', (without_key or key_services or [{'env': 'other'}])[0].env) %}
{{ _('Only for another service: a name of your own that ends in _API_KEY, such as ACME_API_KEY. It is a label, never looked up among the server\'s variables.') }} {% for e in errors.custom_name or [] %}{{ e }}{% endfor %}
{% for e in errors.custom_key or [] %}{{ e }}{% endfor %}
{% for e in errors.custom_confirm or [] %}{{ e }}{% endfor %}
{% endif %}
{% endif %} {% endset %} {% if first_time %}{{ keys_section }}{% endif %} {% if first_time %} {#- First time: no key anywhere (and no AI server of one's own), so there is nothing to choose yet. -#} {% if can_store_keys %}

{{ _('Start with the API key: add the key you got from your AI service under “Manage API keys” above. Then the model is chosen here, among that service\'s models.') }}

{% else %}

{{ _('There is no API key yet, and this portal keeps its keys in the server configuration. Ask whoever runs the server to add one.') }}

{% endif %} {% else %}
{{ h.csrf_input() }} {% set model_block %}
{% set known = suggestions | map(attribute='model') | list %} {% set chosen_model = form.model or recommended %} {% if suggestions %} {#- The list follows the chosen API key: only that service's models (settings.js), with a suggested one. -#} {% endif %} {% if model_retired %}{{ _('The service retired this model on {date}: choose another one, test the connection and save.').format(date=model_retired) }}{% endif %} {% if recommended %}{{ _('A suggested model is chosen: check it, then test the connection and save.') }}{% endif %} {{ _('The list holds the models for the services whose key is set. A newer model that is not in the list can be written in full under “Other model”, as the service, a slash and the model, for example gemini/gemini-3.5-flash-lite. Try it with “Test the connection”.') }} {% if library_version %}{{ _('The portal talks to the AI services through LiteLLM {version}.').format(version=library_version) }}{% endif %} {{ origin('model') }}
{% endset %} {% set key_block %} {#- The key in use, chosen among the keys that are there; they are added, replaced and removed under "API keys" below. Choosing a model chooses its service's key, and choosing a key shows that service's models (settings.js). -#}
{% if can_store_keys %}{{ _('Add, replace or remove API keys under “Manage API keys” below.') }} {% else %}{{ _('The keys are kept in the server configuration. Only their last four characters are shown, to tell them apart. To add or change a key, ask whoever runs the server.') }}{% endif %} {{ origin('api_key_env') }}
{% endset %} {{ model_block }} {{ key_block }} {{ keys_section }} {% set timeout_help = { 'timeout_seconds': _('How long to wait for the AI to answer an ordinary call. Usually a few seconds.'), 'page_read_timeout_seconds': _('How long to wait when the AI service fetches a web page itself and reads it (models that can). That usually takes a few seconds but can take longer for large pages, so this limit is longer.'), } %} {% for name in ['timeout_seconds', 'page_read_timeout_seconds'] %}
{{ timeout_help[name] }} {{ origin(name) }}
{% endfor %} {% if api_base %}

{{ _('Service address') }}: {{ api_base }} ({{ _('only in the server configuration') }})

{% endif %}
{% endif %} {#- Models for different purposes (API 2): an extension names what it uses the AI for, and each purpose may get a model of its own. Empty: the default model above. The key follows the model's service unless another is chosen. The heading is there also without any purpose, so that a sysadmin knows the possibility exists. -#} {% set chosen_any = purpose_rows | selectattr('model') | list or purpose_rows | selectattr('broken') | list %} {% set many_groups = (purpose_rows | map(attribute='consumer') | unique | list | length) > 1 %}
{{ _('Models for different purposes') }}

{{ _('An extension can use the AI for several things. Each can have a model of its own, for example a fast, inexpensive one for translation and a stronger one for reviews. Leave a model empty to use the default model above. The key is the one for the model\'s service, unless you choose another; it is never sent to another service.') }} {% if purpose_rows and purpose_rows[0].default_model %}{{ _('The default model is now {model}.').format(model=purpose_rows[0].default_model) }}{% endif %}

{{ _('An extension that offers this lists its purposes here. An extension that does not offer it uses the default model and is not listed.') }}

{% if purposes_saved %}
{{ _('The models for the purposes are saved and apply now.') }}
{% endif %} {% if not purpose_rows %}

{{ _('No installed extension offers purposes of its own yet.') }}

{% elif first_time %}

{{ _('Set up the default model above first; then a model can be chosen for each purpose.') }}

{% endif %} {% if purpose_tests is not none and not tested_consumer %}{{ purpose_test_summary(purpose_tests) }}{% endif %} {% if purpose_rows and not first_time %} {#- Firefox shows an option's text instead of its value, so the text names the model too. -#} {% for s in suggestions %}{% endfor %}
{{ h.csrf_input() }} {#- The form's default button (Enter in a field) is its first submit button: Save, not the first row's Test. -#} {% for consumer, rows in purpose_rows | groupby('consumer') %} {% set own = rows | selectattr('model') | list + rows | selectattr('broken') | list %}
{{ rows[0].consumer_name }} {{ _('Models of their own: {own} of {count}').format(own=own | length, count=rows | length) }} {% if many_groups %} {% endif %} {% if purpose_tests is not none and tested_consumer == consumer %}{{ purpose_test_summary(purpose_tests) }}{% endif %} {% for row in rows %} {% set model_field = 'purpose_model.' ~ row.id %} {% set key_field = 'purpose_key.' ~ row.id %} {% set shown_model = submitted_purposes.get(model_field, row.model) %} {% set shown_key = submitted_purposes.get(key_field, row.api_key_env if row.model and not row.key_follows_model else '') %}
{% if not row.declared %}{{ _('No installed extension uses this purpose now. Empty the model to remove the choice.') }} {% elif row.model or row.broken %}{{ _('A model of its own') }}{% if row.model %} ({{ row.model }}){% endif %} {% else %}{{ _('Uses the default model') }}{% if row.default_model %} ({{ row.default_model }}){% endif %}{% endif %} {#- The status above is what is saved; when the field says something else, the row says so (the script keeps it up to date). -#} {{ _('Changed, not saved') }} {% set tried = (purpose_tests or {}).get(row.id) %} {% if tried and tried.success %} ✓ {% trans model=tried.model, seconds=h.localised_number(tried.seconds) %}Answered in {{ seconds }} s with {{ model }}.{% endtrans %} {% elif tried %} ✗ {{ tried.error }}{% if tried.hint %} {{ tried.hint }}{% endif %}{% if tried.detail %} ({{ tried.detail }}){% endif %} {% endif %} {% if test and test.purpose_id == row.id %} {#- The row's own Test: its answer here, where the button is. -#} {% if test.success %} ✓ {% trans model=test.model, seconds=h.localised_number(test.seconds) %}Answered in {{ seconds }} s with {{ model }}.{% endtrans %}{% if test.answer %} {{ _('Test answer:') }} {{ test.answer }}{% endif %} {% else %} ✗ {{ _('The connection test failed:') }} {{ test.error }}{% if test.hint %} {{ test.hint }}{% endif %}{% if test.detail %} ({{ test.detail }}){% endif %} {% endif %} {% endif %}
{#- The browser's list shows only the models that match what is in the field: emptying it shows them all. -#}
{% if row.broken %}{{ _('The saved choice cannot be read. Choose the model again.') }}{% endif %}
{% for e in errors['purpose.' ~ row.id] or [] %}
{{ e }}
{% endfor %}
{% endfor %}
{% endfor %} {{ _('Tests the models as they are in the fields, without saving: one short call for each model and key.') }}
{% endif %}
{% if resettable and not confirm_reset %} {# Only when a value saved here has a server value to go back to; values the server has none for stay. #}
{{ h.csrf_input() }} {% set reset_labels = [] %}{% for name in resettable %}{% do reset_labels.append(labels[name]) %}{% endfor %} {{ _('For: {fields}').format(fields=reset_labels | join(', ')) }}
{% endif %} {#- The forms the fields under "Manage API keys" belong to (their form attribute). -#}
{{ h.csrf_input() }}
{{ h.csrf_input() }}
{% if purpose_errors or purpose_tests is not none or focus_row %} {#- Back to where the button was: the row for its own Test (also with an error), the extension for its test, else the top of the section. -#} {% endif %} {% if purposes_saved %} {#- The confirmation is for this one visit: a reload or a bookmark should not say it again. -#} {% endif %} {% if errors.custom_name or errors.custom_key or errors.custom_confirm or errors.remove_key %} {% endif %} {% endblock %}