imio.omnia.assistant 1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. imio/omnia/assistant/__init__.py +6 -0
  2. imio/omnia/assistant/adapters.py +94 -0
  3. imio/omnia/assistant/browser/__init__.py +0 -0
  4. imio/omnia/assistant/browser/configure.zcml +56 -0
  5. imio/omnia/assistant/browser/controlpanel.py +147 -0
  6. imio/omnia/assistant/browser/overrides/.gitkeep +0 -0
  7. imio/omnia/assistant/browser/proxy.py +89 -0
  8. imio/omnia/assistant/browser/resources/package-lock.json +3990 -0
  9. imio/omnia/assistant/browser/resources/package.json +8 -0
  10. imio/omnia/assistant/browser/static/omnia-assistant-ui.js +91 -0
  11. imio/omnia/assistant/browser/templates/omnia_assistant_config.pt +11 -0
  12. imio/omnia/assistant/browser/viewlets.py +25 -0
  13. imio/omnia/assistant/configure.zcml +43 -0
  14. imio/omnia/assistant/interfaces.py +20 -0
  15. imio/omnia/assistant/locales/README.rst +37 -0
  16. imio/omnia/assistant/locales/__init__.py +0 -0
  17. imio/omnia/assistant/locales/en/LC_MESSAGES/imio.omnia.assistant.po +109 -0
  18. imio/omnia/assistant/locales/fr/LC_MESSAGES/imio.omnia.assistant.po +123 -0
  19. imio/omnia/assistant/locales/imio.omnia.assistant.pot +112 -0
  20. imio/omnia/assistant/locales/update.py +74 -0
  21. imio/omnia/assistant/locales/update.sh +12 -0
  22. imio/omnia/assistant/permissions.zcml +10 -0
  23. imio/omnia/assistant/profiles/default/actions.xml +13 -0
  24. imio/omnia/assistant/profiles/default/browserlayer.xml +7 -0
  25. imio/omnia/assistant/profiles/default/catalog.xml +4 -0
  26. imio/omnia/assistant/profiles/default/metadata.xml +7 -0
  27. imio/omnia/assistant/profiles/default/registry/main.xml +18 -0
  28. imio/omnia/assistant/profiles/default/rolemap.xml +7 -0
  29. imio/omnia/assistant/profiles/uninstall/actions.xml +8 -0
  30. imio/omnia/assistant/profiles/uninstall/browserlayer.xml +7 -0
  31. imio/omnia/assistant/profiles/uninstall/registry.xml +15 -0
  32. imio/omnia/assistant/setuphandlers.py +35 -0
  33. imio/omnia/assistant/testing.py +46 -0
  34. imio/omnia/assistant/tests/__init__.py +0 -0
  35. imio/omnia/assistant/tests/robot/test_example.robot +66 -0
  36. imio/omnia/assistant/tests/test_controlpanel.py +76 -0
  37. imio/omnia/assistant/tests/test_proxy.py +657 -0
  38. imio/omnia/assistant/tests/test_robot.py +28 -0
  39. imio/omnia/assistant/tests/test_setup.py +88 -0
  40. imio/omnia/assistant/tests/test_viewlet.py +206 -0
  41. imio/omnia/assistant/upgrades.py +13 -0
  42. imio/omnia/assistant/upgrades.zcml +15 -0
  43. imio.omnia.assistant-1.0-py3.12-nspkg.pth +2 -0
  44. imio_omnia_assistant-1.0.dist-info/METADATA +366 -0
  45. imio_omnia_assistant-1.0.dist-info/RECORD +51 -0
  46. imio_omnia_assistant-1.0.dist-info/WHEEL +5 -0
  47. imio_omnia_assistant-1.0.dist-info/entry_points.txt +5 -0
  48. imio_omnia_assistant-1.0.dist-info/licenses/LICENSE.GPL +339 -0
  49. imio_omnia_assistant-1.0.dist-info/licenses/LICENSE.rst +15 -0
  50. imio_omnia_assistant-1.0.dist-info/namespace_packages.txt +2 -0
  51. imio_omnia_assistant-1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Init and utils."""
3
+ from zope.i18nmessageid import MessageFactory
4
+
5
+
6
+ _ = MessageFactory('imio.omnia.assistant')
@@ -0,0 +1,94 @@
1
+ # -*- coding: utf-8 -*-
2
+ from plone import api
3
+ from zope.component import adapter
4
+ from zope.interface import Interface
5
+ from zope.interface import implementer
6
+ from zope.publisher.interfaces.browser import IBrowserRequest
7
+
8
+ from imio.omnia.assistant.interfaces import IOmniaAssistantAdapter
9
+ from imio.omnia.core.tokens import generate_token
10
+
11
+
12
+ _SETTINGS_PREFIX = (
13
+ "imio.omnia.assistant.browser.controlpanel.IOmniaAssistantSettings"
14
+ )
15
+
16
+
17
+ @adapter(Interface, IBrowserRequest)
18
+ @implementer(IOmniaAssistantAdapter)
19
+ class OmniaAssistantAdapter:
20
+ """Default assistant configuration hook.
21
+
22
+ Product add-ons can register more specific multi-adapters to override
23
+ assistant availability, server-side prompt and frontend initialization
24
+ settings.
25
+ """
26
+
27
+ def __init__(self, context, request):
28
+ self.context = context
29
+ self.request = request
30
+
31
+ def _get_registry_record(self, key, default=None):
32
+ try:
33
+ return api.portal.get_registry_record(key)
34
+ except (KeyError, api.exc.InvalidParameterError):
35
+ return default
36
+
37
+ def is_available(self):
38
+ return self._get_registry_record(f"{_SETTINGS_PREFIX}.enabled", True)
39
+
40
+ def get_system_prompt(self):
41
+ return self._get_registry_record(f"{_SETTINGS_PREFIX}.base_prompt", "")
42
+
43
+ def _get_registry_config(self):
44
+ config = {
45
+ "model": self._get_registry_record(f"{_SETTINGS_PREFIX}.model", ""),
46
+ "include_page_content": self._get_registry_record(
47
+ f"{_SETTINGS_PREFIX}.include_page_content", True
48
+ ),
49
+ "page_content_selector": self._get_registry_record(
50
+ f"{_SETTINGS_PREFIX}.page_content_selector", "#content"
51
+ ),
52
+ "page_content_clean": self._get_registry_record(
53
+ f"{_SETTINGS_PREFIX}.page_content_clean", False
54
+ ),
55
+ "max_context_chars": self._get_registry_record(
56
+ f"{_SETTINGS_PREFIX}.max_context_chars", 20000
57
+ ),
58
+ "max_messages_per_session": self._get_registry_record(
59
+ f"{_SETTINGS_PREFIX}.max_messages_per_session", 0
60
+ ),
61
+ "mode": self._get_registry_record(f"{_SETTINGS_PREFIX}.mode", "floating"),
62
+ "initial_width": self._get_registry_record(
63
+ f"{_SETTINGS_PREFIX}.initial_width", 380
64
+ ),
65
+ "initial_height": self._get_registry_record(
66
+ f"{_SETTINGS_PREFIX}.initial_height", 520
67
+ ),
68
+ }
69
+ disclaimer = self._get_registry_record(f"{_SETTINGS_PREFIX}.disclaimer")
70
+ if disclaimer:
71
+ config["disclaimer"] = disclaimer
72
+ return config
73
+
74
+ def _merge_config(self, config, overrides):
75
+ if not overrides:
76
+ return config
77
+ merged = dict(config)
78
+ merged.update(overrides)
79
+ return merged
80
+
81
+ def _get_config_overrides(self):
82
+ return {}
83
+
84
+ def get_config(self):
85
+ if not self.is_available():
86
+ return None
87
+
88
+ config = self._get_registry_config()
89
+ config = self._merge_config(config, self._get_config_overrides())
90
+ config["api_service_url"] = (
91
+ f"{self.context.absolute_url()}/@@omnia-assistant-api"
92
+ )
93
+ config["api_key"] = generate_token(api.portal.get().absolute_url())
94
+ return config
File without changes
@@ -0,0 +1,56 @@
1
+ <configure
2
+ xmlns="http://namespaces.zope.org/zope"
3
+ xmlns:browser="http://namespaces.zope.org/browser"
4
+ xmlns:plone="http://namespaces.plone.org/plone"
5
+ i18n_domain="imio.omnia.assistant">
6
+
7
+ <!-- Set overrides folder for Just-a-Bunch-Of-Templates product -->
8
+ <include package="z3c.jbot" file="meta.zcml"/>
9
+ <browser:jbot
10
+ directory="overrides"
11
+ layer="imio.omnia.assistant.interfaces.IImioOmniaAssistantLayer"
12
+ />
13
+
14
+ <!-- Publish static files -->
15
+ <plone:static
16
+ name="imio.omnia.assistant"
17
+ type="plone"
18
+ directory="static"
19
+ />
20
+
21
+ <!-- Vocabulary: available models from the OpenAI-compatible API -->
22
+ <utility
23
+ factory=".controlpanel.OmniaModelsVocabularyFactory"
24
+ provides="zope.schema.interfaces.IVocabularyFactory"
25
+ name="imio.omnia.assistant.models"
26
+ />
27
+
28
+ <browser:page
29
+ name="omnia-assistant-settings"
30
+ for="Products.CMFPlone.interfaces.IPloneSiteRoot"
31
+ class=".controlpanel.OmniaAssistantControlPanelView"
32
+ permission="cmf.ManagePortal"
33
+ layer="imio.omnia.assistant.interfaces.IImioOmniaAssistantLayer"
34
+ />
35
+
36
+ <browser:page
37
+ name="omnia-assistant-api"
38
+ for="*"
39
+ class=".proxy.OmniaAssistantOpenAIProxyView"
40
+ permission="imio.omnia.core.AccessOmniaOpenAIProxy"
41
+ layer="imio.omnia.assistant.interfaces.IImioOmniaAssistantLayer"
42
+ />
43
+
44
+ <!-- Viewlet: inject window.omnia_assistant_settings and mount the chat widget -->
45
+ <browser:viewlet
46
+ name="omnia-assistant-config"
47
+ for="*"
48
+ manager="plone.app.layout.viewlets.interfaces.IHtmlHead"
49
+ class=".viewlets.OmniaAssistantConfigViewlet"
50
+ template="templates/omnia_assistant_config.pt"
51
+ permission="zope2.View"
52
+ layer="imio.omnia.assistant.interfaces.IImioOmniaAssistantLayer"
53
+ />
54
+
55
+
56
+ </configure>
@@ -0,0 +1,147 @@
1
+ # -*- coding: utf-8 -*-
2
+ import logging
3
+
4
+ from imio.omnia.assistant import _
5
+ from imio.omnia.core.browser.controlpanel import OmniaCoreControlPanelFormWrapper
6
+ from imio.omnia.core.interfaces import IOmniaOpenAIService
7
+ from plone import api
8
+ from plone.app.registry.browser.controlpanel import RegistryEditForm
9
+ from plone.z3cform import layout
10
+ from zope import schema
11
+ from zope.component import getMultiAdapter
12
+ from zope.globalrequest import getRequest
13
+ from zope.interface import Interface
14
+ from zope.interface import implementer
15
+ from zope.schema.interfaces import IVocabularyFactory
16
+ from zope.schema.vocabulary import SimpleTerm
17
+ from zope.schema.vocabulary import SimpleVocabulary
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+ _MODEL_REGISTRY_KEY = (
22
+ "imio.omnia.assistant.browser.controlpanel.IOmniaAssistantSettings.model"
23
+ )
24
+
25
+
26
+ @implementer(IVocabularyFactory)
27
+ class OmniaModelsVocabularyFactory:
28
+ """Vocabulary of available models from the OpenAI-compatible API."""
29
+
30
+ def __call__(self, context):
31
+ terms = []
32
+ try:
33
+ portal = api.portal.get()
34
+ request = getRequest()
35
+ service = getMultiAdapter((portal, request), IOmniaOpenAIService)
36
+ result = service.list_models()
37
+ for model_data in result.get("data", []):
38
+ model_id = model_data["id"]
39
+ terms.append(SimpleTerm(value=model_id, token=model_id, title=model_id))
40
+ except Exception:
41
+ logger.error("Could not fetch models from OpenAI API", exc_info=True)
42
+
43
+ if not terms:
44
+ # Fallback: keep the currently saved value so the form still renders.
45
+ try:
46
+ current = api.portal.get_registry_record(
47
+ _MODEL_REGISTRY_KEY, default=None
48
+ )
49
+ if current:
50
+ terms = [SimpleTerm(value=current, token=current, title=current)]
51
+ except Exception:
52
+ pass
53
+
54
+ return SimpleVocabulary(terms)
55
+
56
+
57
+ class IOmniaAssistantSettings(Interface):
58
+ enabled = schema.Bool(
59
+ title=_("Enabled"),
60
+ description=_("Enable or disable the AI assistant globally."),
61
+ required=False,
62
+ default=True,
63
+ )
64
+ model = schema.Choice(
65
+ title=_("Model"),
66
+ description=_("Model for the OpenAI-compatible API."),
67
+ vocabulary="imio.omnia.assistant.models",
68
+ required=True,
69
+ )
70
+ base_prompt = schema.Text(
71
+ title=_("System prompt"),
72
+ description=_("System prompt sent with every conversation."),
73
+ required=False,
74
+ )
75
+ include_page_content = schema.Bool(
76
+ title=_("Include page content"),
77
+ description=_("Include current page content as context for the AI."),
78
+ required=False,
79
+ default=True,
80
+ )
81
+ page_content_selector = schema.TextLine(
82
+ title=_("Page content CSS selector"),
83
+ description=_("CSS selector to extract page content from."),
84
+ required=False,
85
+ default="#content",
86
+ )
87
+ page_content_clean = schema.Bool(
88
+ title=_("Use plain text"),
89
+ description=_(
90
+ "Use innerText instead of innerHTML for page content extraction."
91
+ ),
92
+ required=False,
93
+ default=False,
94
+ )
95
+ max_context_chars = schema.Int(
96
+ title=_("Max context characters"),
97
+ description=_("Maximum characters of page content to include as context."),
98
+ required=False,
99
+ default=20000,
100
+ )
101
+ max_messages_per_session = schema.Int(
102
+ title=_("Max messages per conversation"),
103
+ description=_(
104
+ "Maximum number of user messages allowed in a single conversation. "
105
+ "Set to 0 for no limit."
106
+ ),
107
+ required=False,
108
+ default=0,
109
+ min=0,
110
+ )
111
+ mode = schema.Choice(
112
+ title=_("Display mode"),
113
+ description=_("Panel display mode: floating (draggable) or fixed."),
114
+ values=["floating", "fixed"],
115
+ required=False,
116
+ default="floating",
117
+ )
118
+ initial_width = schema.Int(
119
+ title=_("Panel width"),
120
+ description=_("Initial panel width in pixels."),
121
+ required=False,
122
+ default=380,
123
+ )
124
+ initial_height = schema.Int(
125
+ title=_("Panel height"),
126
+ description=_("Initial panel height in pixels."),
127
+ required=False,
128
+ default=520,
129
+ )
130
+ disclaimer = schema.Text(
131
+ title=_("Disclaimer"),
132
+ description=_(
133
+ "Disclaimer text shown at the bottom of the chat panel. "
134
+ "Leave empty for the default French disclaimer."
135
+ ),
136
+ required=False,
137
+ )
138
+
139
+
140
+ class OmniaAssistantControlPanelForm(RegistryEditForm):
141
+ label = _("AI Assistant settings")
142
+ schema = IOmniaAssistantSettings
143
+
144
+
145
+ OmniaAssistantControlPanelView = layout.wrap_form(
146
+ OmniaAssistantControlPanelForm, OmniaCoreControlPanelFormWrapper
147
+ )
File without changes
@@ -0,0 +1,89 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from plone import api
4
+ from zope.component import getMultiAdapter
5
+
6
+ from imio.omnia.assistant.interfaces import IOmniaAssistantAdapter
7
+ from imio.omnia.core.browser.proxy import OmniaOpenAIProxyView
8
+
9
+
10
+ _SETTINGS_PREFIX = (
11
+ "imio.omnia.assistant.browser.controlpanel.IOmniaAssistantSettings"
12
+ )
13
+
14
+
15
+ class OmniaAssistantOpenAIProxyView(OmniaOpenAIProxyView):
16
+ """Assistant-specific OpenAI proxy with server-side prompt injection."""
17
+
18
+ def _get_registry_record(self, key, default=None):
19
+ try:
20
+ return api.portal.get_registry_record(key)
21
+ except (KeyError, api.exc.InvalidParameterError):
22
+ return default
23
+
24
+ def _get_assistant_adapter(self):
25
+ return getMultiAdapter((self.context, self.request), IOmniaAssistantAdapter)
26
+
27
+ def _is_proxy_enabled(self):
28
+ return self._get_assistant_adapter().is_available()
29
+
30
+ def _get_base_prompt(self):
31
+ return self._get_assistant_adapter().get_system_prompt()
32
+
33
+ def _get_max_messages_per_session(self):
34
+ return (
35
+ self._get_registry_record(
36
+ f"{_SETTINGS_PREFIX}.max_messages_per_session", 0
37
+ )
38
+ or 0
39
+ )
40
+
41
+ def _validate_message_limit(self, body):
42
+ max_messages = self._get_max_messages_per_session()
43
+ if max_messages <= 0:
44
+ return None
45
+
46
+ messages = body.get("messages", [])
47
+ user_messages_count = sum(
48
+ 1
49
+ for message in messages
50
+ if isinstance(message, dict) and message.get("role") == "user"
51
+ )
52
+ if user_messages_count <= max_messages:
53
+ return None
54
+
55
+ return self._json_error(
56
+ 400,
57
+ "Maximum messages per conversation exceeded. "
58
+ "Start a new conversation.",
59
+ )
60
+
61
+ def _inject_base_prompt(self, body):
62
+ base_prompt = self._get_base_prompt()
63
+ if not base_prompt:
64
+ return body
65
+
66
+ messages = body.get("messages", [])
67
+ if not isinstance(messages, list):
68
+ messages = []
69
+
70
+ first_message = messages[0] if messages else None
71
+ if (
72
+ isinstance(first_message, dict)
73
+ and first_message.get("role") == "system"
74
+ and first_message.get("content") == base_prompt
75
+ ):
76
+ return body
77
+
78
+ updated_body = dict(body)
79
+ updated_body["messages"] = [
80
+ {"role": "system", "content": base_prompt},
81
+ *messages,
82
+ ]
83
+ return updated_body
84
+
85
+ def _prepare_request_body(self, body):
86
+ limit_error = self._validate_message_limit(body)
87
+ if limit_error is not None:
88
+ return body, limit_error
89
+ return self._inject_base_prompt(body), None