unique_toolkit 1.8.1__py3-none-any.whl → 1.23.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.

Potentially problematic release.


This version of unique_toolkit might be problematic. Click here for more details.

Files changed (105) hide show
  1. unique_toolkit/__init__.py +20 -0
  2. unique_toolkit/_common/api_calling/human_verification_manager.py +121 -28
  3. unique_toolkit/_common/chunk_relevancy_sorter/config.py +3 -3
  4. unique_toolkit/_common/chunk_relevancy_sorter/tests/test_service.py +2 -5
  5. unique_toolkit/_common/default_language_model.py +9 -3
  6. unique_toolkit/_common/docx_generator/__init__.py +7 -0
  7. unique_toolkit/_common/docx_generator/config.py +12 -0
  8. unique_toolkit/_common/docx_generator/schemas.py +80 -0
  9. unique_toolkit/_common/docx_generator/service.py +252 -0
  10. unique_toolkit/_common/docx_generator/template/Doc Template.docx +0 -0
  11. unique_toolkit/_common/endpoint_builder.py +138 -117
  12. unique_toolkit/_common/endpoint_requestor.py +240 -14
  13. unique_toolkit/_common/exception.py +20 -0
  14. unique_toolkit/_common/feature_flags/schema.py +1 -5
  15. unique_toolkit/_common/referencing.py +53 -0
  16. unique_toolkit/_common/string_utilities.py +52 -1
  17. unique_toolkit/_common/tests/test_referencing.py +521 -0
  18. unique_toolkit/_common/tests/test_string_utilities.py +506 -0
  19. unique_toolkit/_common/utils/files.py +43 -0
  20. unique_toolkit/agentic/debug_info_manager/debug_info_manager.py +16 -6
  21. unique_toolkit/agentic/debug_info_manager/test/test_debug_info_manager.py +278 -0
  22. unique_toolkit/agentic/evaluation/config.py +3 -2
  23. unique_toolkit/agentic/evaluation/context_relevancy/service.py +2 -2
  24. unique_toolkit/agentic/evaluation/evaluation_manager.py +9 -5
  25. unique_toolkit/agentic/evaluation/hallucination/constants.py +1 -1
  26. unique_toolkit/agentic/evaluation/hallucination/hallucination_evaluation.py +26 -3
  27. unique_toolkit/agentic/history_manager/history_manager.py +14 -11
  28. unique_toolkit/agentic/history_manager/loop_token_reducer.py +3 -4
  29. unique_toolkit/agentic/history_manager/utils.py +10 -87
  30. unique_toolkit/agentic/postprocessor/postprocessor_manager.py +107 -16
  31. unique_toolkit/agentic/reference_manager/reference_manager.py +1 -1
  32. unique_toolkit/agentic/responses_api/__init__.py +19 -0
  33. unique_toolkit/agentic/responses_api/postprocessors/code_display.py +63 -0
  34. unique_toolkit/agentic/responses_api/postprocessors/generated_files.py +145 -0
  35. unique_toolkit/agentic/responses_api/stream_handler.py +15 -0
  36. unique_toolkit/agentic/tools/a2a/__init__.py +18 -2
  37. unique_toolkit/agentic/tools/a2a/evaluation/__init__.py +2 -0
  38. unique_toolkit/agentic/tools/a2a/evaluation/_utils.py +3 -3
  39. unique_toolkit/agentic/tools/a2a/evaluation/config.py +1 -1
  40. unique_toolkit/agentic/tools/a2a/evaluation/evaluator.py +143 -91
  41. unique_toolkit/agentic/tools/a2a/manager.py +7 -1
  42. unique_toolkit/agentic/tools/a2a/postprocessing/__init__.py +11 -3
  43. unique_toolkit/agentic/tools/a2a/postprocessing/_display_utils.py +185 -0
  44. unique_toolkit/agentic/tools/a2a/postprocessing/_ref_utils.py +73 -0
  45. unique_toolkit/agentic/tools/a2a/postprocessing/config.py +21 -0
  46. unique_toolkit/agentic/tools/a2a/postprocessing/display.py +180 -0
  47. unique_toolkit/agentic/tools/a2a/postprocessing/references.py +101 -0
  48. unique_toolkit/agentic/tools/a2a/postprocessing/test/test_display_utils.py +1335 -0
  49. unique_toolkit/agentic/tools/a2a/postprocessing/test/test_ref_utils.py +603 -0
  50. unique_toolkit/agentic/tools/a2a/prompts.py +46 -0
  51. unique_toolkit/agentic/tools/a2a/response_watcher/__init__.py +6 -0
  52. unique_toolkit/agentic/tools/a2a/response_watcher/service.py +91 -0
  53. unique_toolkit/agentic/tools/a2a/tool/config.py +15 -5
  54. unique_toolkit/agentic/tools/a2a/tool/service.py +69 -36
  55. unique_toolkit/agentic/tools/config.py +16 -2
  56. unique_toolkit/agentic/tools/factory.py +4 -0
  57. unique_toolkit/agentic/tools/mcp/tool_wrapper.py +7 -35
  58. unique_toolkit/agentic/tools/openai_builtin/__init__.py +11 -0
  59. unique_toolkit/agentic/tools/openai_builtin/base.py +30 -0
  60. unique_toolkit/agentic/tools/openai_builtin/code_interpreter/__init__.py +8 -0
  61. unique_toolkit/agentic/tools/openai_builtin/code_interpreter/config.py +57 -0
  62. unique_toolkit/agentic/tools/openai_builtin/code_interpreter/service.py +230 -0
  63. unique_toolkit/agentic/tools/openai_builtin/manager.py +62 -0
  64. unique_toolkit/agentic/tools/test/test_mcp_manager.py +95 -7
  65. unique_toolkit/agentic/tools/test/test_tool_progress_reporter.py +240 -0
  66. unique_toolkit/agentic/tools/tool.py +0 -11
  67. unique_toolkit/agentic/tools/tool_manager.py +337 -122
  68. unique_toolkit/agentic/tools/tool_progress_reporter.py +81 -15
  69. unique_toolkit/agentic/tools/utils/__init__.py +18 -0
  70. unique_toolkit/agentic/tools/utils/execution/execution.py +8 -4
  71. unique_toolkit/agentic/tools/utils/source_handling/schema.py +1 -1
  72. unique_toolkit/chat/__init__.py +8 -1
  73. unique_toolkit/chat/deprecated/service.py +232 -0
  74. unique_toolkit/chat/functions.py +54 -40
  75. unique_toolkit/chat/rendering.py +34 -0
  76. unique_toolkit/chat/responses_api.py +461 -0
  77. unique_toolkit/chat/schemas.py +1 -1
  78. unique_toolkit/chat/service.py +96 -1569
  79. unique_toolkit/content/functions.py +116 -1
  80. unique_toolkit/content/schemas.py +59 -0
  81. unique_toolkit/content/service.py +5 -37
  82. unique_toolkit/content/smart_rules.py +301 -0
  83. unique_toolkit/framework_utilities/langchain/client.py +27 -3
  84. unique_toolkit/framework_utilities/openai/client.py +12 -1
  85. unique_toolkit/framework_utilities/openai/message_builder.py +85 -1
  86. unique_toolkit/language_model/default_language_model.py +3 -0
  87. unique_toolkit/language_model/functions.py +25 -9
  88. unique_toolkit/language_model/infos.py +72 -4
  89. unique_toolkit/language_model/schemas.py +246 -40
  90. unique_toolkit/protocols/support.py +91 -9
  91. unique_toolkit/services/__init__.py +7 -0
  92. unique_toolkit/services/chat_service.py +1630 -0
  93. unique_toolkit/services/knowledge_base.py +861 -0
  94. unique_toolkit/smart_rules/compile.py +56 -301
  95. unique_toolkit/test_utilities/events.py +197 -0
  96. {unique_toolkit-1.8.1.dist-info → unique_toolkit-1.23.0.dist-info}/METADATA +173 -3
  97. {unique_toolkit-1.8.1.dist-info → unique_toolkit-1.23.0.dist-info}/RECORD +99 -67
  98. unique_toolkit/agentic/tools/a2a/postprocessing/_display.py +0 -122
  99. unique_toolkit/agentic/tools/a2a/postprocessing/_utils.py +0 -19
  100. unique_toolkit/agentic/tools/a2a/postprocessing/postprocessor.py +0 -230
  101. unique_toolkit/agentic/tools/a2a/postprocessing/test/test_consolidate_references.py +0 -665
  102. unique_toolkit/agentic/tools/a2a/postprocessing/test/test_display.py +0 -391
  103. unique_toolkit/agentic/tools/a2a/postprocessing/test/test_postprocessor_reference_functions.py +0 -256
  104. {unique_toolkit-1.8.1.dist-info → unique_toolkit-1.23.0.dist-info}/LICENSE +0 -0
  105. {unique_toolkit-1.8.1.dist-info → unique_toolkit-1.23.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,1630 @@
1
+ import logging
2
+ from typing import Any, Sequence
3
+
4
+ import unique_sdk
5
+ from openai.types.chat import ChatCompletionToolChoiceOptionParam
6
+ from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
7
+ from openai.types.responses import (
8
+ ResponseIncludable,
9
+ ResponseInputItemParam,
10
+ ResponseOutputItem,
11
+ ResponseTextConfigParam,
12
+ ToolParam,
13
+ response_create_params,
14
+ )
15
+ from openai.types.shared_params import Metadata, Reasoning
16
+ from typing_extensions import deprecated
17
+
18
+ from unique_toolkit._common.utils.files import is_file_content, is_image_content
19
+ from unique_toolkit.chat.constants import (
20
+ DEFAULT_MAX_MESSAGES,
21
+ DEFAULT_PERCENT_OF_MAX_TOKENS,
22
+ DOMAIN_NAME,
23
+ )
24
+ from unique_toolkit.chat.deprecated.service import ChatServiceDeprecated
25
+ from unique_toolkit.chat.functions import (
26
+ create_message,
27
+ create_message_assessment,
28
+ create_message_assessment_async,
29
+ create_message_async,
30
+ create_message_execution,
31
+ create_message_execution_async,
32
+ create_message_log,
33
+ create_message_log_async,
34
+ get_full_history,
35
+ get_full_history_async,
36
+ get_message_execution,
37
+ get_message_execution_async,
38
+ get_selection_from_history,
39
+ modify_message,
40
+ modify_message_assessment,
41
+ modify_message_assessment_async,
42
+ modify_message_async,
43
+ stream_complete_with_references,
44
+ stream_complete_with_references_async,
45
+ update_message_execution,
46
+ update_message_execution_async,
47
+ update_message_log,
48
+ update_message_log_async,
49
+ )
50
+ from unique_toolkit.chat.responses_api import (
51
+ stream_responses_with_references,
52
+ stream_responses_with_references_async,
53
+ )
54
+ from unique_toolkit.chat.schemas import (
55
+ ChatMessage,
56
+ ChatMessageAssessment,
57
+ ChatMessageAssessmentLabel,
58
+ ChatMessageAssessmentStatus,
59
+ ChatMessageAssessmentType,
60
+ ChatMessageRole,
61
+ MessageExecution,
62
+ MessageExecutionType,
63
+ MessageExecutionUpdateStatus,
64
+ MessageLog,
65
+ MessageLogDetails,
66
+ MessageLogStatus,
67
+ MessageLogUncitedReferences,
68
+ )
69
+ from unique_toolkit.content.functions import (
70
+ download_content_to_bytes,
71
+ search_contents,
72
+ upload_content_from_bytes,
73
+ )
74
+ from unique_toolkit.content.schemas import (
75
+ Content,
76
+ ContentChunk,
77
+ ContentReference,
78
+ )
79
+ from unique_toolkit.language_model.constants import (
80
+ DEFAULT_COMPLETE_TEMPERATURE,
81
+ DEFAULT_COMPLETE_TIMEOUT,
82
+ )
83
+ from unique_toolkit.language_model.infos import (
84
+ LanguageModelName,
85
+ )
86
+ from unique_toolkit.language_model.schemas import (
87
+ LanguageModelMessageOptions,
88
+ LanguageModelMessages,
89
+ LanguageModelResponse,
90
+ LanguageModelStreamResponse,
91
+ LanguageModelTool,
92
+ LanguageModelToolDescription,
93
+ ResponsesLanguageModelStreamResponse,
94
+ )
95
+
96
+ logger = logging.getLogger(f"toolkit.{DOMAIN_NAME}.{__name__}")
97
+
98
+
99
+ class ChatService(ChatServiceDeprecated):
100
+ """Provides all functionalities to manage the chat session."""
101
+
102
+ async def update_debug_info_async(self, debug_info: dict):
103
+ """Updates the debug information for the chat session.
104
+
105
+ Args:
106
+ debug_info (dict): The new debug information.
107
+
108
+ """
109
+ return await modify_message_async(
110
+ user_id=self._user_id,
111
+ company_id=self._company_id,
112
+ assistant_message_id=self._assistant_message_id,
113
+ chat_id=self._chat_id,
114
+ user_message_id=self._user_message_id,
115
+ user_message_text=self._user_message_text,
116
+ assistant=False,
117
+ debug_info=debug_info,
118
+ )
119
+
120
+ def replace_debug_info(self, debug_info: dict):
121
+ """Replace the debug information in the last user message
122
+
123
+ Args:
124
+ debug_info (dict): The new debug information.
125
+
126
+ """
127
+ return modify_message(
128
+ user_id=self._user_id,
129
+ company_id=self._company_id,
130
+ assistant_message_id=self._assistant_message_id,
131
+ chat_id=self._chat_id,
132
+ user_message_id=self._user_message_id,
133
+ user_message_text=self._user_message_text,
134
+ assistant=False,
135
+ debug_info=debug_info,
136
+ )
137
+
138
+ # Message Methods
139
+ ############################################################################
140
+
141
+ def modify_user_message(
142
+ self,
143
+ content: str,
144
+ references: list[ContentReference] | None = None,
145
+ debug_info: dict | None = None,
146
+ message_id: str | None = None,
147
+ set_completed_at: bool | None = False,
148
+ ) -> ChatMessage:
149
+ """Modifies a user message in the chat session synchronously.
150
+
151
+ Args:
152
+ content (str): The new content for the message.
153
+ references (list[ContentReference]): list of ContentReference objects.
154
+ debug_info (dict[str, Any]]]): Debug information.
155
+ message_id (str, optional): The message ID, if not specified the last user message is edited.
156
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
157
+
158
+ Returns:
159
+ ChatMessage: The modified message.
160
+
161
+ Raises:
162
+ Exception: If the modification fails.
163
+
164
+ """
165
+ return modify_message(
166
+ user_id=self._user_id,
167
+ company_id=self._company_id,
168
+ assistant_message_id=self._assistant_message_id,
169
+ chat_id=self._chat_id,
170
+ user_message_id=self._user_message_id,
171
+ user_message_text=self._user_message_text,
172
+ assistant=False,
173
+ content=content,
174
+ references=references,
175
+ debug_info=debug_info,
176
+ message_id=message_id,
177
+ set_completed_at=set_completed_at or False,
178
+ )
179
+
180
+ async def modify_user_message_async(
181
+ self,
182
+ content: str,
183
+ references: list[ContentReference] = [],
184
+ debug_info: dict = {},
185
+ message_id: str | None = None,
186
+ set_completed_at: bool | None = False,
187
+ ) -> ChatMessage:
188
+ """Modifies a message in the chat session asynchronously.
189
+
190
+ Args:
191
+ content (str): The new content for the message.
192
+ message_id (str, optional): The message ID. Defaults to None, then the ChatState user message id is used.
193
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
194
+ debug_info (dict[str, Any]]]): Debug information. Defaults to {}.
195
+ set_completed_at (bool, optional): Whether to set the completedAt field with the current date time. Defaults to False.
196
+
197
+ Returns:
198
+ ChatMessage: The modified message.
199
+
200
+ Raises:
201
+ Exception: If the modification fails.
202
+
203
+ """
204
+ return await modify_message_async(
205
+ user_id=self._user_id,
206
+ company_id=self._company_id,
207
+ assistant_message_id=self._assistant_message_id,
208
+ chat_id=self._chat_id,
209
+ user_message_id=self._user_message_id,
210
+ user_message_text=self._user_message_text,
211
+ assistant=False,
212
+ content=content,
213
+ references=references,
214
+ debug_info=debug_info,
215
+ message_id=message_id,
216
+ set_completed_at=set_completed_at or False,
217
+ )
218
+
219
+ def modify_assistant_message(
220
+ self,
221
+ content: str | None = None,
222
+ original_content: str | None = None,
223
+ references: list[ContentReference] | None = None,
224
+ debug_info: dict | None = None,
225
+ message_id: str | None = None,
226
+ set_completed_at: bool | None = None,
227
+ ) -> ChatMessage:
228
+ """Modifies a message in the chat session synchronously if parameter is not specified the corresponding field will remain as is.
229
+
230
+ Args:
231
+ content (str, optional): The new content for the message.
232
+ original_content (str, optional): The original content for the message.
233
+ references (list[ContentReference]): list of ContentReference objects. Defaults to [].
234
+ debug_info (dict[str, Any]]]): Debug information. Defaults to {}.
235
+ message_id (Optional[str]): The message ID. Defaults to None.
236
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
237
+
238
+ Returns:
239
+ ChatMessage: The modified message.
240
+
241
+ Raises:
242
+ Exception: If the modification fails.
243
+
244
+ """
245
+ return modify_message(
246
+ user_id=self._user_id,
247
+ company_id=self._company_id,
248
+ assistant_message_id=self._assistant_message_id,
249
+ chat_id=self._chat_id,
250
+ user_message_id=self._user_message_id,
251
+ user_message_text=self._user_message_text,
252
+ assistant=True,
253
+ content=content,
254
+ original_content=original_content,
255
+ references=references,
256
+ debug_info=debug_info,
257
+ message_id=message_id,
258
+ set_completed_at=set_completed_at or False,
259
+ )
260
+
261
+ async def modify_assistant_message_async(
262
+ self,
263
+ content: str | None = None,
264
+ original_content: str | None = None,
265
+ references: list[ContentReference] | None = None,
266
+ debug_info: dict | None = None,
267
+ message_id: str | None = None,
268
+ set_completed_at: bool | None = False,
269
+ ) -> ChatMessage:
270
+ """Modifies a message in the chat session asynchronously.
271
+
272
+ Args:
273
+ content (str, optional): The new content for the message.
274
+ original_content (str, optional): The original content for the message.
275
+ message_id (str, optional): The message ID. Defaults to None, then the ChatState assistant message id is used.
276
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
277
+ debug_info (dict[str, Any]], optional): Debug information. Defaults to None.
278
+ set_completed_at (bool, optional): Whether to set the completedAt field with the current date time. Defaults to False.
279
+
280
+ Returns:
281
+ ChatMessage: The modified message.
282
+
283
+ Raises:
284
+ Exception: If the modification fails.
285
+
286
+ """
287
+ return await modify_message_async(
288
+ user_id=self._user_id,
289
+ company_id=self._company_id,
290
+ assistant_message_id=self._assistant_message_id,
291
+ chat_id=self._chat_id,
292
+ user_message_id=self._user_message_id,
293
+ user_message_text=self._user_message_text,
294
+ assistant=True,
295
+ content=content,
296
+ original_content=original_content,
297
+ references=references,
298
+ debug_info=debug_info,
299
+ message_id=message_id,
300
+ set_completed_at=set_completed_at or False,
301
+ )
302
+
303
+ def create_assistant_message(
304
+ self,
305
+ content: str,
306
+ original_content: str | None = None,
307
+ references: list[ContentReference] | None = None,
308
+ debug_info: dict | None = None,
309
+ set_completed_at: bool | None = False,
310
+ ) -> ChatMessage:
311
+ """Creates a message in the chat session synchronously.
312
+
313
+ Args:
314
+ content (str): The content for the message.
315
+ original_content (str, optional): The original content for the message.
316
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
317
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
318
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
319
+
320
+ Returns:
321
+ ChatMessage: The created message.
322
+
323
+ Raises:
324
+ Exception: If the creation fails.
325
+
326
+ """
327
+ chat_message = create_message(
328
+ user_id=self._user_id,
329
+ company_id=self._company_id,
330
+ chat_id=self._chat_id,
331
+ assistant_id=self._assistant_id,
332
+ role=ChatMessageRole.ASSISTANT,
333
+ content=content,
334
+ original_content=original_content,
335
+ references=references,
336
+ debug_info=debug_info,
337
+ set_completed_at=set_completed_at,
338
+ )
339
+ # Update the assistant message id
340
+ self._assistant_message_id = chat_message.id or "unknown"
341
+ return chat_message
342
+
343
+ async def create_assistant_message_async(
344
+ self,
345
+ content: str,
346
+ original_content: str | None = None,
347
+ references: list[ContentReference] | None = None,
348
+ debug_info: dict | None = None,
349
+ set_completed_at: bool | None = False,
350
+ ) -> ChatMessage:
351
+ """Creates a message in the chat session asynchronously.
352
+
353
+ Args:
354
+ content (str): The content for the message.
355
+ original_content (str, optional): The original content for the message.
356
+ references (list[ContentReference]): list of references. Defaults to None.
357
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
358
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
359
+
360
+ Returns:
361
+ ChatMessage: The created message.
362
+
363
+ Raises:
364
+ Exception: If the creation fails.
365
+
366
+ """
367
+ chat_message = await create_message_async(
368
+ user_id=self._user_id,
369
+ company_id=self._company_id,
370
+ chat_id=self._chat_id,
371
+ assistant_id=self._assistant_id,
372
+ role=ChatMessageRole.ASSISTANT,
373
+ content=content,
374
+ original_content=original_content,
375
+ references=references,
376
+ debug_info=debug_info,
377
+ set_completed_at=set_completed_at,
378
+ )
379
+ # Update the assistant message id
380
+ self._assistant_message_id = chat_message.id or "unknown"
381
+ return chat_message
382
+
383
+ def create_user_message(
384
+ self,
385
+ content: str,
386
+ original_content: str | None = None,
387
+ references: list[ContentReference] | None = None,
388
+ debug_info: dict | None = None,
389
+ set_completed_at: bool | None = False,
390
+ ) -> ChatMessage:
391
+ """Creates a user message in the chat session synchronously.
392
+
393
+ Args:
394
+ content (str): The content for the message.
395
+ original_content (str, optional): The original content for the message.
396
+ references (list[ContentReference]): list of ContentReference objects. Defaults to None.
397
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
398
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
399
+
400
+ Returns:
401
+ ChatMessage: The created message.
402
+
403
+ Raises:
404
+ Exception: If the creation fails.
405
+
406
+ """
407
+ chat_message = create_message(
408
+ user_id=self._user_id,
409
+ company_id=self._company_id,
410
+ chat_id=self._chat_id,
411
+ assistant_id=self._assistant_id,
412
+ role=ChatMessageRole.USER,
413
+ content=content,
414
+ original_content=original_content,
415
+ references=references,
416
+ debug_info=debug_info,
417
+ set_completed_at=set_completed_at,
418
+ )
419
+ # Update the user message id
420
+ self._user_message_id = chat_message.id or "unknown"
421
+ return chat_message
422
+
423
+ async def create_user_message_async(
424
+ self,
425
+ content: str,
426
+ original_content: str | None = None,
427
+ references: list[ContentReference] | None = None,
428
+ debug_info: dict | None = None,
429
+ set_completed_at: bool | None = False,
430
+ ) -> ChatMessage:
431
+ """Creates a user message in the chat session asynchronously.
432
+
433
+ Args:
434
+ content (str): The content for the message.
435
+ original_content (str, optional): The original content for the message.
436
+ references (list[ContentReference]): list of references. Defaults to None.
437
+ debug_info (dict[str, Any]]): Debug information. Defaults to None.
438
+ set_completed_at (Optional[bool]): Whether to set the completedAt field with the current date time. Defaults to False.
439
+
440
+ Returns:
441
+ ChatMessage: The created message.
442
+
443
+ Raises:
444
+ Exception: If the creation fails.
445
+
446
+ """
447
+ chat_message = await create_message_async(
448
+ user_id=self._user_id,
449
+ company_id=self._company_id,
450
+ chat_id=self._chat_id,
451
+ assistant_id=self._assistant_id,
452
+ role=ChatMessageRole.USER,
453
+ content=content,
454
+ original_content=original_content,
455
+ references=references,
456
+ debug_info=debug_info,
457
+ set_completed_at=set_completed_at,
458
+ )
459
+ # Update the user message id
460
+ self._user_message_id = chat_message.id or "unknown"
461
+ return chat_message
462
+
463
+ def free_user_input(self) -> None:
464
+ """Unblocks the next user input"""
465
+ self.modify_assistant_message(set_completed_at=True)
466
+
467
+ # History Methods
468
+ ############################################################################
469
+
470
+ def get_full_history(self) -> list[ChatMessage]:
471
+ """Loads the full chat history for the chat session synchronously.
472
+
473
+ Returns:
474
+ list[ChatMessage]: The full chat history.
475
+
476
+ Raises:
477
+ Exception: If the loading fails.
478
+
479
+ """
480
+ return get_full_history(
481
+ event_user_id=self._user_id,
482
+ event_company_id=self._company_id,
483
+ event_payload_chat_id=self._chat_id,
484
+ )
485
+
486
+ async def get_full_history_async(self) -> list[ChatMessage]:
487
+ """Loads the full chat history for the chat session asynchronously.
488
+
489
+ Returns:
490
+ list[ChatMessage]: The full chat history.
491
+
492
+ Raises:
493
+ Exception: If the loading fails.
494
+
495
+ """
496
+ return await get_full_history_async(
497
+ event_user_id=self._user_id,
498
+ event_company_id=self._company_id,
499
+ event_payload_chat_id=self._chat_id,
500
+ )
501
+
502
+ def get_full_and_selected_history(
503
+ self,
504
+ token_limit: int,
505
+ percent_of_max_tokens: float = DEFAULT_PERCENT_OF_MAX_TOKENS,
506
+ max_messages: int = DEFAULT_MAX_MESSAGES,
507
+ ) -> tuple[list[ChatMessage], list[ChatMessage]]:
508
+ """Loads the chat history for the chat session synchronously.
509
+
510
+ Args:
511
+ token_limit (int): The maximum number of tokens to load.
512
+ percent_of_max_tokens (float): The percentage of the maximum tokens to load. Defaults to 0.15.
513
+ max_messages (int): The maximum number of messages to load. Defaults to 4.
514
+
515
+ Returns:
516
+ tuple[list[ChatMessage], list[ChatMessage]]: The selected and full chat history.
517
+
518
+ Raises:
519
+ Exception: If the loading fails.
520
+
521
+ """
522
+ full_history = get_full_history(
523
+ event_user_id=self._user_id,
524
+ event_company_id=self._company_id,
525
+ event_payload_chat_id=self._chat_id,
526
+ )
527
+ selected_history = get_selection_from_history(
528
+ full_history=full_history,
529
+ max_tokens=int(round(token_limit * percent_of_max_tokens)),
530
+ max_messages=max_messages,
531
+ )
532
+
533
+ return full_history, selected_history
534
+
535
+ async def get_full_and_selected_history_async(
536
+ self,
537
+ token_limit: int,
538
+ percent_of_max_tokens: float = DEFAULT_PERCENT_OF_MAX_TOKENS,
539
+ max_messages: int = DEFAULT_MAX_MESSAGES,
540
+ ) -> tuple[list[ChatMessage], list[ChatMessage]]:
541
+ """Loads the chat history for the chat session asynchronously.
542
+
543
+ Args:
544
+ token_limit (int): The maximum number of tokens to load.
545
+ percent_of_max_tokens (float): The percentage of the maximum tokens to load. Defaults to 0.15.
546
+ max_messages (int): The maximum number of messages to load. Defaults to 4.
547
+
548
+ Returns:
549
+ tuple[list[ChatMessage], list[ChatMessage]]: The selected and full chat history.
550
+
551
+ Raises:
552
+ Exception: If the loading fails.
553
+
554
+ """
555
+ full_history = await get_full_history_async(
556
+ event_user_id=self._user_id,
557
+ event_company_id=self._company_id,
558
+ event_payload_chat_id=self._chat_id,
559
+ )
560
+ selected_history = get_selection_from_history(
561
+ full_history=full_history,
562
+ max_tokens=int(round(token_limit * percent_of_max_tokens)),
563
+ max_messages=max_messages,
564
+ )
565
+
566
+ return full_history, selected_history
567
+
568
+ # Message Assessment Methods
569
+ ############################################################################
570
+
571
+ def create_message_assessment(
572
+ self,
573
+ assistant_message_id: str,
574
+ status: ChatMessageAssessmentStatus,
575
+ type: ChatMessageAssessmentType,
576
+ title: str | None = None,
577
+ explanation: str | None = None,
578
+ label: ChatMessageAssessmentLabel | None = None,
579
+ is_visible: bool = True,
580
+ ) -> ChatMessageAssessment:
581
+ """Creates a message assessment for an assistant message synchronously.
582
+
583
+ Args:
584
+ assistant_message_id (str): The ID of the assistant message to assess
585
+ status (MessageAssessmentStatus): The status of the assessment (e.g. "DONE")
586
+ type (MessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
587
+ title (str | None): The title of the assessment
588
+ explanation (str | None): Explanation of the assessment
589
+ label (MessageAssessmentLabel | None): The assessment label (e.g. "RED")
590
+ is_visible (bool): Whether the assessment is visible to users. Defaults to True.
591
+
592
+ Returns:
593
+ ChatMessageAssessment: The created message assessment
594
+
595
+ Raises:
596
+ Exception: If the creation fails
597
+
598
+ """
599
+ return create_message_assessment(
600
+ user_id=self._user_id,
601
+ company_id=self._company_id,
602
+ assistant_message_id=assistant_message_id,
603
+ status=status,
604
+ type=type,
605
+ title=title,
606
+ explanation=explanation,
607
+ label=label,
608
+ is_visible=is_visible,
609
+ )
610
+
611
+ async def create_message_assessment_async(
612
+ self,
613
+ assistant_message_id: str,
614
+ status: ChatMessageAssessmentStatus,
615
+ type: ChatMessageAssessmentType,
616
+ title: str | None = None,
617
+ explanation: str | None = None,
618
+ label: ChatMessageAssessmentLabel | None = None,
619
+ is_visible: bool = True,
620
+ ) -> ChatMessageAssessment:
621
+ """Creates a message assessment for an assistant message asynchronously.
622
+
623
+ Args:
624
+ assistant_message_id (str): The ID of the assistant message to assess
625
+ status (ChatMessageAssessmentStatus): The status of the assessment (e.g. "DONE")
626
+ type (ChatMessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
627
+ title (str | None): The title of the assessment
628
+ explanation (str | None): Explanation of the assessment
629
+ label (ChatMessageAssessmentLabel | None): The assessment label (e.g. "RED")
630
+ is_visible (bool): Whether the assessment is visible to users. Defaults to True.
631
+
632
+ Returns:
633
+ ChatMessageAssessment: The created message assessment
634
+
635
+ Raises:
636
+ Exception: If the creation fails
637
+
638
+ """
639
+ return await create_message_assessment_async(
640
+ user_id=self._user_id,
641
+ company_id=self._company_id,
642
+ assistant_message_id=assistant_message_id,
643
+ status=status,
644
+ type=type,
645
+ title=title,
646
+ explanation=explanation,
647
+ label=label,
648
+ is_visible=is_visible,
649
+ )
650
+
651
+ def modify_message_assessment(
652
+ self,
653
+ assistant_message_id: str,
654
+ status: ChatMessageAssessmentStatus,
655
+ type: ChatMessageAssessmentType,
656
+ title: str | None = None,
657
+ explanation: str | None = None,
658
+ label: ChatMessageAssessmentLabel | None = None,
659
+ ) -> ChatMessageAssessment:
660
+ """Modifies a message assessment for an assistant message synchronously.
661
+
662
+ Args:
663
+ assistant_message_id (str): The ID of the assistant message to assess
664
+ status (MessageAssessmentStatus): The status of the assessment (e.g. "DONE")
665
+ title (str | None): The title of the assessment
666
+ explanation (str | None): Explanation of the assessment
667
+ label (ChatMessageAssessmentLabel | None): The assessment label (e.g. "RED")
668
+ type (ChatMessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
669
+
670
+ Returns:
671
+ dict: The modified message assessment
672
+
673
+ Raises:
674
+ Exception: If the modification fails
675
+
676
+ """
677
+ return modify_message_assessment(
678
+ user_id=self._user_id,
679
+ company_id=self._company_id,
680
+ assistant_message_id=assistant_message_id,
681
+ status=status,
682
+ type=type,
683
+ title=title,
684
+ explanation=explanation,
685
+ label=label,
686
+ )
687
+
688
+ async def modify_message_assessment_async(
689
+ self,
690
+ assistant_message_id: str,
691
+ type: ChatMessageAssessmentType,
692
+ title: str | None = None,
693
+ status: ChatMessageAssessmentStatus | None = None,
694
+ explanation: str | None = None,
695
+ label: ChatMessageAssessmentLabel | None = None,
696
+ ) -> ChatMessageAssessment:
697
+ """Modifies a message assessment for an assistant message asynchronously.
698
+
699
+ Args:
700
+ assistant_message_id (str): The ID of the assistant message to assess
701
+ status (ChatMessageAssessmentStatus): The status of the assessment (e.g. "DONE")
702
+ title (str | None): The title of the assessment
703
+ explanation (str | None): Explanation of the assessment
704
+ label (ChatMessageAssessmentLabel | None): The assessment label (e.g. "RED")
705
+ type (ChatMessageAssessmentType): The type of assessment (e.g. "HALLUCINATION")
706
+
707
+ Returns:
708
+ ChatMessageAssessment: The modified message assessment
709
+
710
+ Raises:
711
+ Exception: If the modification fails
712
+
713
+ """
714
+ return await modify_message_assessment_async(
715
+ user_id=self._user_id,
716
+ company_id=self._company_id,
717
+ assistant_message_id=assistant_message_id,
718
+ status=status,
719
+ type=type,
720
+ title=title,
721
+ explanation=explanation,
722
+ label=label,
723
+ )
724
+
725
+ # Message Log Methods
726
+ ############################################################################
727
+
728
+ def create_message_log(
729
+ self,
730
+ *,
731
+ message_id: str,
732
+ text: str,
733
+ status: MessageLogStatus,
734
+ order: int,
735
+ details: MessageLogDetails | None = None,
736
+ uncited_references: MessageLogUncitedReferences | None = None,
737
+ references: list[ContentReference] | None = None,
738
+ ) -> MessageLog:
739
+ """Creates a message log for tracking execution steps synchronously.
740
+
741
+ Args:
742
+ message_id (str): The ID of the message this log belongs to
743
+ text (str): The log text content
744
+ status (MessageLogStatus): The status of this log entry
745
+ order (int): The order/sequence number of this log entry
746
+ details (MessageLogDetails | None): Additional details about this log entry
747
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
748
+ references (list[ContentReference] | None): List of references for this log
749
+
750
+ Returns:
751
+ MessageLog: The created message log
752
+
753
+ Raises:
754
+ Exception: If the creation fails
755
+
756
+ """
757
+ return create_message_log(
758
+ user_id=self._user_id,
759
+ company_id=self._company_id,
760
+ message_id=message_id,
761
+ text=text,
762
+ status=status,
763
+ order=order,
764
+ details=details,
765
+ uncited_references=uncited_references,
766
+ references=references,
767
+ )
768
+
769
+ async def create_message_log_async(
770
+ self,
771
+ *,
772
+ message_id: str,
773
+ text: str,
774
+ status: MessageLogStatus,
775
+ order: int,
776
+ details: MessageLogDetails | None = None,
777
+ uncited_references: MessageLogUncitedReferences | None = None,
778
+ references: list[ContentReference] | None = None,
779
+ ) -> MessageLog:
780
+ """Creates a message log for tracking execution steps asynchronously.
781
+
782
+ Args:
783
+ message_id (str): The ID of the message this log belongs to
784
+ text (str): The log text content
785
+ status (MessageLogStatus): The status of this log entry
786
+ order (int): The order/sequence number of this log entry
787
+ details (MessageLogDetails | None): Additional details about this log entry
788
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
789
+ references (list[ContentReference] | None): List of references for this log
790
+
791
+ Returns:
792
+ MessageLog: The created message log
793
+
794
+ Raises:
795
+ Exception: If the creation fails
796
+
797
+ """
798
+ return await create_message_log_async(
799
+ user_id=self._user_id,
800
+ company_id=self._company_id,
801
+ message_id=message_id,
802
+ text=text,
803
+ status=status,
804
+ order=order,
805
+ details=details,
806
+ uncited_references=uncited_references,
807
+ references=references,
808
+ )
809
+
810
+ def update_message_log(
811
+ self,
812
+ *,
813
+ message_log_id: str,
814
+ order: int,
815
+ text: str | None = None,
816
+ status: MessageLogStatus | None = None,
817
+ details: MessageLogDetails | None = None,
818
+ uncited_references: MessageLogUncitedReferences | None = None,
819
+ references: list[ContentReference] | None = None,
820
+ ) -> MessageLog:
821
+ """Updates a message log synchronously.
822
+
823
+ Args:
824
+ message_log_id (str): The ID of the message log to update
825
+ order (int): The order/sequence number (required)
826
+ text (str | None): The updated log text content
827
+ status (MessageLogStatus | None): The updated status
828
+ details (MessageLogDetails | None): Updated additional details
829
+ uncited_references (MessageLogUncitedReferences | None): Updated uncited references
830
+ references (list[ContentReference] | None): Updated list of references
831
+
832
+ Returns:
833
+ MessageLog: The updated message log
834
+
835
+ Raises:
836
+ Exception: If the update fails
837
+
838
+ """
839
+ return update_message_log(
840
+ user_id=self._user_id,
841
+ company_id=self._company_id,
842
+ message_log_id=message_log_id,
843
+ order=order,
844
+ text=text,
845
+ status=status,
846
+ details=details,
847
+ uncited_references=uncited_references,
848
+ references=references,
849
+ )
850
+
851
+ async def update_message_log_async(
852
+ self,
853
+ *,
854
+ message_log_id: str,
855
+ order: int,
856
+ text: str | None = None,
857
+ status: MessageLogStatus | None = None,
858
+ details: MessageLogDetails | None = None,
859
+ uncited_references: MessageLogUncitedReferences | None = None,
860
+ references: list[ContentReference] | None = None,
861
+ ) -> MessageLog:
862
+ """Updates a message log asynchronously.
863
+
864
+ Args:
865
+ message_log_id (str): The ID of the message log to update
866
+ order (int): The order/sequence number (required)
867
+ text (str | None): The updated log text content
868
+ status (MessageLogStatus | None): The updated status
869
+ details (MessageLogDetails | None): Updated additional details
870
+ uncited_references (MessageLogUncitedReferences | None): Updated uncited references
871
+ references (list[ContentReference] | None): Updated list of references
872
+
873
+ Returns:
874
+ MessageLog: The updated message log
875
+
876
+ Raises:
877
+ Exception: If the update fails
878
+
879
+ """
880
+ return await update_message_log_async(
881
+ user_id=self._user_id,
882
+ company_id=self._company_id,
883
+ message_log_id=message_log_id,
884
+ order=order,
885
+ text=text,
886
+ status=status,
887
+ details=details,
888
+ uncited_references=uncited_references,
889
+ references=references,
890
+ )
891
+
892
+ def create_assistant_message_log(
893
+ self,
894
+ *,
895
+ text: str,
896
+ status: MessageLogStatus,
897
+ order: int,
898
+ details: MessageLogDetails | None = None,
899
+ uncited_references: MessageLogUncitedReferences | None = None,
900
+ references: list[ContentReference] | None = None,
901
+ ) -> MessageLog:
902
+ """Creates a message log for the current assistant message synchronously.
903
+
904
+ This is a convenience method that uses the current assistant message ID.
905
+
906
+ Args:
907
+ text (str): The log text content
908
+ status (MessageLogStatus): The status of this log entry
909
+ order (int): The order/sequence number of this log entry
910
+ details (MessageLogDetails | None): Additional details about this log entry
911
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
912
+ references (list[ContentReference] | None): List of references for this log
913
+
914
+ Returns:
915
+ MessageLog: The created message log
916
+
917
+ Raises:
918
+ Exception: If the creation fails
919
+
920
+ """
921
+ return self.create_message_log(
922
+ message_id=self._assistant_message_id,
923
+ text=text,
924
+ status=status,
925
+ order=order,
926
+ details=details,
927
+ uncited_references=uncited_references,
928
+ references=references,
929
+ )
930
+
931
+ async def create_assistant_message_log_async(
932
+ self,
933
+ *,
934
+ text: str,
935
+ status: MessageLogStatus,
936
+ order: int,
937
+ details: MessageLogDetails | None = None,
938
+ uncited_references: MessageLogUncitedReferences | None = None,
939
+ references: list[ContentReference] | None = None,
940
+ ) -> MessageLog:
941
+ """Creates a message log for the current assistant message asynchronously.
942
+
943
+ This is a convenience method that uses the current assistant message ID.
944
+
945
+ Args:
946
+ text (str): The log text content
947
+ status (MessageLogStatus): The status of this log entry
948
+ order (int): The order/sequence number of this log entry
949
+ details (MessageLogDetails | None): Additional details about this log entry
950
+ uncited_references (MessageLogUncitedReferences | None): References that are not cited
951
+ references (list[ContentReference] | None): List of references for this log
952
+
953
+ Returns:
954
+ MessageLog: The created message log
955
+
956
+ Raises:
957
+ Exception: If the creation fails
958
+
959
+ """
960
+ return await self.create_message_log_async(
961
+ message_id=self._assistant_message_id,
962
+ text=text,
963
+ status=status,
964
+ order=order,
965
+ details=details,
966
+ uncited_references=uncited_references,
967
+ references=references,
968
+ )
969
+
970
+ # Message Execution Methods
971
+ ############################################################################
972
+
973
+ def create_message_execution(
974
+ self,
975
+ *,
976
+ message_id: str,
977
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
978
+ seconds_remaining: int | None = None,
979
+ percentage_completed: int | None = None,
980
+ ) -> MessageExecution:
981
+ """Creates a message execution for tracking long-running operations synchronously.
982
+
983
+ Args:
984
+ message_id (str): The ID of the message this execution belongs to
985
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
986
+ seconds_remaining (int | None): Estimated seconds remaining for completion
987
+ percentage_completed (int | None): Percentage of completion (0-100)
988
+
989
+ Returns:
990
+ MessageExecution: The created message execution
991
+
992
+ Raises:
993
+ Exception: If the creation fails
994
+
995
+ """
996
+ return create_message_execution(
997
+ user_id=self._user_id,
998
+ company_id=self._company_id,
999
+ message_id=message_id,
1000
+ chat_id=self._chat_id,
1001
+ type=type,
1002
+ seconds_remaining=seconds_remaining,
1003
+ percentage_completed=percentage_completed,
1004
+ )
1005
+
1006
+ async def create_message_execution_async(
1007
+ self,
1008
+ *,
1009
+ message_id: str,
1010
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
1011
+ seconds_remaining: int | None = None,
1012
+ percentage_completed: int | None = None,
1013
+ ) -> MessageExecution:
1014
+ """Creates a message execution for tracking long-running operations asynchronously.
1015
+
1016
+ Args:
1017
+ message_id (str): The ID of the message this execution belongs to
1018
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
1019
+ seconds_remaining (int | None): Estimated seconds remaining for completion
1020
+ percentage_completed (int | None): Percentage of completion (0-100)
1021
+
1022
+ Returns:
1023
+ MessageExecution: The created message execution
1024
+
1025
+ Raises:
1026
+ Exception: If the creation fails
1027
+
1028
+ """
1029
+ return await create_message_execution_async(
1030
+ user_id=self._user_id,
1031
+ company_id=self._company_id,
1032
+ message_id=message_id,
1033
+ chat_id=self._chat_id,
1034
+ type=type,
1035
+ seconds_remaining=seconds_remaining,
1036
+ percentage_completed=percentage_completed,
1037
+ )
1038
+
1039
+ def get_message_execution(
1040
+ self,
1041
+ *,
1042
+ message_id: str,
1043
+ ) -> MessageExecution:
1044
+ """Gets a message execution by message ID synchronously.
1045
+
1046
+ Args:
1047
+ message_id (str): The ID of the message to get execution for
1048
+
1049
+ Returns:
1050
+ MessageExecution: The message execution
1051
+
1052
+ Raises:
1053
+ Exception: If the retrieval fails
1054
+
1055
+ """
1056
+ return get_message_execution(
1057
+ user_id=self._user_id,
1058
+ company_id=self._company_id,
1059
+ message_id=message_id,
1060
+ )
1061
+
1062
+ async def get_message_execution_async(
1063
+ self,
1064
+ *,
1065
+ message_id: str,
1066
+ ) -> MessageExecution:
1067
+ """Gets a message execution by message ID asynchronously.
1068
+
1069
+ Args:
1070
+ message_id (str): The ID of the message to get execution for
1071
+
1072
+ Returns:
1073
+ MessageExecution: The message execution
1074
+
1075
+ Raises:
1076
+ Exception: If the retrieval fails
1077
+
1078
+ """
1079
+ return await get_message_execution_async(
1080
+ user_id=self._user_id,
1081
+ company_id=self._company_id,
1082
+ message_id=message_id,
1083
+ )
1084
+
1085
+ def update_message_execution(
1086
+ self,
1087
+ *,
1088
+ message_id: str,
1089
+ status: MessageExecutionUpdateStatus | None = None,
1090
+ seconds_remaining: int | None = None,
1091
+ percentage_completed: int | None = None,
1092
+ ) -> MessageExecution:
1093
+ """Updates a message execution synchronously.
1094
+
1095
+ Args:
1096
+ message_id (str): The ID of the message to update execution for
1097
+ status (MessageExecutionUpdateStatus | None): The updated status (COMPLETED or FAILED). Defaults to None
1098
+ seconds_remaining (int | None): Updated estimated seconds remaining
1099
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1100
+
1101
+ Returns:
1102
+ MessageExecution: The updated message execution
1103
+
1104
+ Raises:
1105
+ Exception: If the update fails
1106
+
1107
+ """
1108
+ return update_message_execution(
1109
+ user_id=self._user_id,
1110
+ company_id=self._company_id,
1111
+ message_id=message_id,
1112
+ status=status,
1113
+ seconds_remaining=seconds_remaining,
1114
+ percentage_completed=percentage_completed,
1115
+ )
1116
+
1117
+ async def update_message_execution_async(
1118
+ self,
1119
+ *,
1120
+ message_id: str,
1121
+ status: MessageExecutionUpdateStatus | None = None,
1122
+ seconds_remaining: int | None = None,
1123
+ percentage_completed: int | None = None,
1124
+ ) -> MessageExecution:
1125
+ """Updates a message execution asynchronously.
1126
+
1127
+ Args:
1128
+ message_id (str): The ID of the message to update execution for
1129
+ status (MessageExecutionUpdateStatus | None): The updated status (COMPLETED or FAILED). Defaults to None
1130
+ seconds_remaining (int | None): Updated estimated seconds remaining
1131
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1132
+
1133
+ Returns:
1134
+ MessageExecution: The updated message execution
1135
+
1136
+ Raises:
1137
+ Exception: If the update fails
1138
+
1139
+ """
1140
+ return await update_message_execution_async(
1141
+ user_id=self._user_id,
1142
+ company_id=self._company_id,
1143
+ message_id=message_id,
1144
+ status=status,
1145
+ seconds_remaining=seconds_remaining,
1146
+ percentage_completed=percentage_completed,
1147
+ )
1148
+
1149
+ def create_assistant_message_execution(
1150
+ self,
1151
+ *,
1152
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
1153
+ seconds_remaining: int | None = None,
1154
+ percentage_completed: int | None = None,
1155
+ ) -> MessageExecution:
1156
+ """Creates a message execution for the current assistant message synchronously.
1157
+
1158
+ This is a convenience method that uses the current assistant message ID.
1159
+
1160
+ Args:
1161
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
1162
+ seconds_remaining (int | None): Estimated seconds remaining for completion
1163
+ percentage_completed (int | None): Percentage of completion (0-100)
1164
+
1165
+ Returns:
1166
+ MessageExecution: The created message execution
1167
+
1168
+ Raises:
1169
+ Exception: If the creation fails
1170
+
1171
+ """
1172
+ return self.create_message_execution(
1173
+ message_id=self._assistant_message_id,
1174
+ type=type,
1175
+ seconds_remaining=seconds_remaining,
1176
+ percentage_completed=percentage_completed,
1177
+ )
1178
+
1179
+ async def create_assistant_message_execution_async(
1180
+ self,
1181
+ *,
1182
+ type: MessageExecutionType = MessageExecutionType.DEEP_RESEARCH,
1183
+ seconds_remaining: int | None = None,
1184
+ percentage_completed: int | None = None,
1185
+ ) -> MessageExecution:
1186
+ """Creates a message execution for the current assistant message asynchronously.
1187
+
1188
+ This is a convenience method that uses the current assistant message ID.
1189
+
1190
+ Args:
1191
+ type (MessageExecutionType): The type of execution. Defaults to DEEP_RESEARCH.
1192
+ seconds_remaining (int | None): Estimated seconds remaining for completion
1193
+ percentage_completed (int | None): Percentage of completion (0-100)
1194
+
1195
+ Returns:
1196
+ MessageExecution: The created message execution
1197
+
1198
+ Raises:
1199
+ Exception: If the creation fails
1200
+
1201
+ """
1202
+ return await self.create_message_execution_async(
1203
+ message_id=self._assistant_message_id,
1204
+ type=type,
1205
+ seconds_remaining=seconds_remaining,
1206
+ percentage_completed=percentage_completed,
1207
+ )
1208
+
1209
+ def get_assistant_message_execution(self) -> MessageExecution:
1210
+ """Gets the message execution for the current assistant message synchronously.
1211
+
1212
+ This is a convenience method that uses the current assistant message ID.
1213
+
1214
+ Returns:
1215
+ MessageExecution: The message execution
1216
+
1217
+ Raises:
1218
+ Exception: If the retrieval fails
1219
+
1220
+ """
1221
+ return self.get_message_execution(message_id=self._assistant_message_id)
1222
+
1223
+ async def get_assistant_message_execution_async(self) -> MessageExecution:
1224
+ """Gets the message execution for the current assistant message asynchronously.
1225
+
1226
+ This is a convenience method that uses the current assistant message ID.
1227
+
1228
+ Returns:
1229
+ MessageExecution: The message execution
1230
+
1231
+ Raises:
1232
+ Exception: If the retrieval fails
1233
+
1234
+ """
1235
+ return await self.get_message_execution_async(
1236
+ message_id=self._assistant_message_id
1237
+ )
1238
+
1239
+ def update_assistant_message_execution(
1240
+ self,
1241
+ *,
1242
+ status: MessageExecutionUpdateStatus | None = None,
1243
+ seconds_remaining: int | None = None,
1244
+ percentage_completed: int | None = None,
1245
+ ) -> MessageExecution:
1246
+ """Updates the message execution for the current assistant message synchronously.
1247
+
1248
+ This is a convenience method that uses the current assistant message ID.
1249
+
1250
+ Args:
1251
+ status (MessageExecutionUpdateStatus | None): The updated status (COMPLETED or FAILED). Defaults to None
1252
+ seconds_remaining (int | None): Updated estimated seconds remaining
1253
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1254
+
1255
+ Returns:
1256
+ MessageExecution: The updated message execution
1257
+
1258
+ Raises:
1259
+ Exception: If the update fails
1260
+
1261
+ """
1262
+ return self.update_message_execution(
1263
+ message_id=self._assistant_message_id,
1264
+ status=status,
1265
+ seconds_remaining=seconds_remaining,
1266
+ percentage_completed=percentage_completed,
1267
+ )
1268
+
1269
+ async def update_assistant_message_execution_async(
1270
+ self,
1271
+ *,
1272
+ status: MessageExecutionUpdateStatus | None = None,
1273
+ seconds_remaining: int | None = None,
1274
+ percentage_completed: int | None = None,
1275
+ ) -> MessageExecution:
1276
+ """Updates the message execution for the current assistant message asynchronously.
1277
+
1278
+ This is a convenience method that uses the current assistant message ID.
1279
+
1280
+ Args:
1281
+ status (MessageExecutionUpdateStatus | None): The updated status (COMPLETED or FAILED). Defaults to None
1282
+ seconds_remaining (int | None): Updated estimated seconds remaining
1283
+ percentage_completed (int | None): Updated percentage of completion (0-100)
1284
+
1285
+ Returns:
1286
+ MessageExecution: The updated message execution
1287
+
1288
+ Raises:
1289
+ Exception: If the update fails
1290
+
1291
+ """
1292
+ return await self.update_message_execution_async(
1293
+ message_id=self._assistant_message_id,
1294
+ status=status,
1295
+ seconds_remaining=seconds_remaining,
1296
+ percentage_completed=percentage_completed,
1297
+ )
1298
+
1299
+ # Language Model Methods
1300
+ ############################################################################
1301
+
1302
+ @deprecated("Use complete_with_references instead")
1303
+ def stream_complete(
1304
+ self,
1305
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1306
+ model_name: LanguageModelName | str,
1307
+ content_chunks: list[ContentChunk] | None = None,
1308
+ debug_info: dict = {},
1309
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1310
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1311
+ tools: Sequence[LanguageModelTool | LanguageModelToolDescription] | None = None,
1312
+ start_text: str | None = None,
1313
+ tool_choice: ChatCompletionToolChoiceOptionParam | None = None,
1314
+ other_options: dict | None = None,
1315
+ ) -> LanguageModelStreamResponse:
1316
+ return self.complete_with_references(
1317
+ messages=messages,
1318
+ model_name=model_name,
1319
+ content_chunks=content_chunks,
1320
+ debug_info=debug_info,
1321
+ temperature=temperature,
1322
+ timeout=timeout,
1323
+ tools=tools,
1324
+ start_text=start_text,
1325
+ tool_choice=tool_choice,
1326
+ other_options=other_options,
1327
+ )
1328
+
1329
+ def complete_with_references(
1330
+ self,
1331
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1332
+ model_name: LanguageModelName | str,
1333
+ content_chunks: list[ContentChunk] | None = None,
1334
+ debug_info: dict | None = None,
1335
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1336
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1337
+ tools: Sequence[LanguageModelTool | LanguageModelToolDescription] | None = None,
1338
+ start_text: str | None = None,
1339
+ tool_choice: ChatCompletionToolChoiceOptionParam | None = None,
1340
+ other_options: dict | None = None,
1341
+ ) -> LanguageModelStreamResponse:
1342
+ """Streams a completion in the chat session synchronously."""
1343
+ return stream_complete_with_references(
1344
+ company_id=self._company_id,
1345
+ user_id=self._user_id,
1346
+ assistant_message_id=self._assistant_message_id,
1347
+ user_message_id=self._user_message_id,
1348
+ chat_id=self._chat_id,
1349
+ assistant_id=self._assistant_id,
1350
+ messages=messages,
1351
+ model_name=model_name,
1352
+ content_chunks=content_chunks,
1353
+ debug_info=debug_info,
1354
+ temperature=temperature,
1355
+ timeout=timeout,
1356
+ tools=tools,
1357
+ start_text=start_text,
1358
+ tool_choice=tool_choice,
1359
+ other_options=other_options,
1360
+ )
1361
+
1362
+ def complete(
1363
+ self,
1364
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1365
+ model_name: LanguageModelName | str,
1366
+ content_chunks: list[ContentChunk] | None = None,
1367
+ debug_info: dict | None = None,
1368
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1369
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1370
+ tools: Sequence[LanguageModelTool | LanguageModelToolDescription] | None = None,
1371
+ start_text: str | None = None,
1372
+ tool_choice: ChatCompletionToolChoiceOptionParam | None = None,
1373
+ other_options: dict | None = None,
1374
+ ) -> LanguageModelResponse:
1375
+ response = self.complete_with_references(
1376
+ messages=messages,
1377
+ model_name=model_name,
1378
+ content_chunks=content_chunks,
1379
+ debug_info=debug_info,
1380
+ temperature=temperature,
1381
+ timeout=timeout,
1382
+ tools=tools,
1383
+ start_text=start_text,
1384
+ tool_choice=tool_choice,
1385
+ other_options=other_options,
1386
+ )
1387
+
1388
+ return LanguageModelResponse.from_stream_response(response)
1389
+
1390
+ @deprecated("use complete_with_references_async instead.")
1391
+ async def stream_complete_async(
1392
+ self,
1393
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1394
+ model_name: LanguageModelName | str,
1395
+ content_chunks: list[ContentChunk] | None = None,
1396
+ debug_info: dict | None = None,
1397
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1398
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1399
+ tools: Sequence[LanguageModelTool | LanguageModelToolDescription] | None = None,
1400
+ start_text: str | None = None,
1401
+ tool_choice: ChatCompletionToolChoiceOptionParam | None = None,
1402
+ other_options: dict | None = None,
1403
+ ) -> LanguageModelStreamResponse:
1404
+ """Stream a completion in the chat session asynchronously."""
1405
+ return await self.complete_with_references_async(
1406
+ messages=messages,
1407
+ model_name=model_name,
1408
+ content_chunks=content_chunks,
1409
+ debug_info=debug_info,
1410
+ temperature=temperature,
1411
+ timeout=timeout,
1412
+ tools=tools,
1413
+ start_text=start_text,
1414
+ tool_choice=tool_choice,
1415
+ other_options=other_options,
1416
+ )
1417
+
1418
+ async def complete_with_references_async(
1419
+ self,
1420
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1421
+ model_name: LanguageModelName | str,
1422
+ content_chunks: list[ContentChunk] | None = None,
1423
+ debug_info: dict | None = None,
1424
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1425
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1426
+ tools: Sequence[LanguageModelTool | LanguageModelToolDescription] | None = None,
1427
+ tool_choice: ChatCompletionToolChoiceOptionParam | None = None,
1428
+ start_text: str | None = None,
1429
+ other_options: dict | None = None,
1430
+ ) -> LanguageModelStreamResponse:
1431
+ return await stream_complete_with_references_async(
1432
+ company_id=self._company_id,
1433
+ user_id=self._user_id,
1434
+ assistant_message_id=self._assistant_message_id,
1435
+ user_message_id=self._user_message_id,
1436
+ chat_id=self._chat_id,
1437
+ assistant_id=self._assistant_id,
1438
+ messages=messages,
1439
+ model_name=model_name,
1440
+ content_chunks=content_chunks,
1441
+ debug_info=debug_info,
1442
+ temperature=temperature,
1443
+ timeout=timeout,
1444
+ tools=tools,
1445
+ start_text=start_text,
1446
+ tool_choice=tool_choice,
1447
+ other_options=other_options,
1448
+ )
1449
+
1450
+ async def complete_async(
1451
+ self,
1452
+ messages: LanguageModelMessages | list[ChatCompletionMessageParam],
1453
+ model_name: LanguageModelName | str,
1454
+ content_chunks: list[ContentChunk] | None,
1455
+ debug_info: dict | None = None,
1456
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1457
+ timeout: int = DEFAULT_COMPLETE_TIMEOUT,
1458
+ tools: Sequence[LanguageModelTool | LanguageModelToolDescription] | None = None,
1459
+ start_text: str | None = None,
1460
+ tool_choice: ChatCompletionToolChoiceOptionParam | None = None,
1461
+ other_options: dict | None = None,
1462
+ ) -> LanguageModelResponse:
1463
+ response = self.complete_with_references_async(
1464
+ messages=messages,
1465
+ model_name=model_name,
1466
+ content_chunks=content_chunks,
1467
+ debug_info=debug_info,
1468
+ temperature=temperature,
1469
+ timeout=timeout,
1470
+ tools=tools,
1471
+ start_text=start_text,
1472
+ tool_choice=tool_choice,
1473
+ other_options=other_options,
1474
+ )
1475
+
1476
+ return LanguageModelResponse.from_stream_response(await response)
1477
+
1478
+ def complete_responses_with_references(
1479
+ self,
1480
+ *,
1481
+ model_name: LanguageModelName | str,
1482
+ messages: str
1483
+ | LanguageModelMessages
1484
+ | Sequence[
1485
+ ResponseInputItemParam
1486
+ | LanguageModelMessageOptions
1487
+ | ResponseOutputItem # History is automatically convertible
1488
+ ],
1489
+ content_chunks: list[ContentChunk] | None = None,
1490
+ tools: Sequence[LanguageModelToolDescription | ToolParam] | None = None,
1491
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1492
+ debug_info: dict | None = None,
1493
+ start_text: str | None = None,
1494
+ include: list[ResponseIncludable] | None = None,
1495
+ instructions: str | None = None,
1496
+ max_output_tokens: int | None = None,
1497
+ metadata: Metadata | None = None,
1498
+ parallel_tool_calls: bool | None = None,
1499
+ text: ResponseTextConfigParam | None = None,
1500
+ tool_choice: response_create_params.ToolChoice | None = None,
1501
+ top_p: float | None = None,
1502
+ reasoning: Reasoning | None = None,
1503
+ other_options: dict | None = None,
1504
+ ) -> ResponsesLanguageModelStreamResponse:
1505
+ return stream_responses_with_references(
1506
+ company_id=self._company_id,
1507
+ user_id=self._user_id,
1508
+ assistant_message_id=self._assistant_message_id,
1509
+ user_message_id=self._user_message_id,
1510
+ chat_id=self._chat_id,
1511
+ assistant_id=self._assistant_id,
1512
+ model_name=model_name,
1513
+ messages=messages,
1514
+ content_chunks=content_chunks,
1515
+ tools=tools,
1516
+ temperature=temperature,
1517
+ debug_info=debug_info,
1518
+ start_text=start_text,
1519
+ include=include,
1520
+ instructions=instructions,
1521
+ max_output_tokens=max_output_tokens,
1522
+ metadata=metadata,
1523
+ parallel_tool_calls=parallel_tool_calls,
1524
+ text=text,
1525
+ tool_choice=tool_choice,
1526
+ top_p=top_p,
1527
+ reasoning=reasoning,
1528
+ other_options=other_options,
1529
+ )
1530
+
1531
+ async def complete_responses_with_references_async(
1532
+ self,
1533
+ *,
1534
+ model_name: LanguageModelName | str,
1535
+ messages: str
1536
+ | LanguageModelMessages
1537
+ | Sequence[
1538
+ ResponseInputItemParam | LanguageModelMessageOptions | ResponseOutputItem
1539
+ ],
1540
+ content_chunks: list[ContentChunk] | None = None,
1541
+ tools: Sequence[LanguageModelToolDescription | ToolParam] | None = None,
1542
+ temperature: float = DEFAULT_COMPLETE_TEMPERATURE,
1543
+ debug_info: dict | None = None,
1544
+ start_text: str | None = None,
1545
+ include: list[ResponseIncludable] | None = None,
1546
+ instructions: str | None = None,
1547
+ max_output_tokens: int | None = None,
1548
+ metadata: Metadata | None = None,
1549
+ parallel_tool_calls: bool | None = None,
1550
+ text: ResponseTextConfigParam | None = None,
1551
+ tool_choice: response_create_params.ToolChoice | None = None,
1552
+ top_p: float | None = None,
1553
+ reasoning: Reasoning | None = None,
1554
+ other_options: dict | None = None,
1555
+ ) -> ResponsesLanguageModelStreamResponse:
1556
+ return await stream_responses_with_references_async(
1557
+ company_id=self._company_id,
1558
+ user_id=self._user_id,
1559
+ assistant_message_id=self._assistant_message_id,
1560
+ user_message_id=self._user_message_id,
1561
+ chat_id=self._chat_id,
1562
+ assistant_id=self._assistant_id,
1563
+ model_name=model_name,
1564
+ messages=messages,
1565
+ content_chunks=content_chunks,
1566
+ tools=tools,
1567
+ temperature=temperature,
1568
+ debug_info=debug_info,
1569
+ start_text=start_text,
1570
+ include=include,
1571
+ instructions=instructions,
1572
+ max_output_tokens=max_output_tokens,
1573
+ metadata=metadata,
1574
+ parallel_tool_calls=parallel_tool_calls,
1575
+ text=text,
1576
+ tool_choice=tool_choice,
1577
+ top_p=top_p,
1578
+ reasoning=reasoning,
1579
+ other_options=other_options,
1580
+ )
1581
+
1582
+ # Chat Content Methods
1583
+ ############################################################################
1584
+
1585
+ def upload_to_chat_from_bytes(
1586
+ self,
1587
+ *,
1588
+ content: bytes,
1589
+ content_name: str,
1590
+ mime_type: str,
1591
+ scope_id: str | None = None,
1592
+ skip_ingestion: bool = False,
1593
+ ingestion_config: unique_sdk.Content.IngestionConfig | None = None,
1594
+ metadata: dict[str, Any] | None = None,
1595
+ ) -> Content:
1596
+ return upload_content_from_bytes(
1597
+ user_id=self._user_id,
1598
+ company_id=self._company_id,
1599
+ content=content,
1600
+ content_name=content_name,
1601
+ mime_type=mime_type,
1602
+ scope_id=scope_id,
1603
+ chat_id=self._chat_id,
1604
+ skip_ingestion=skip_ingestion,
1605
+ ingestion_config=ingestion_config,
1606
+ metadata=metadata,
1607
+ )
1608
+
1609
+ def download_chat_content_to_bytes(self, *, content_id: str) -> bytes:
1610
+ return download_content_to_bytes(
1611
+ user_id=self._user_id,
1612
+ company_id=self._company_id,
1613
+ content_id=content_id,
1614
+ chat_id=self._chat_id,
1615
+ )
1616
+
1617
+ def download_chat_images_and_documents(self) -> tuple[list[Content], list[Content]]:
1618
+ images: list[Content] = []
1619
+ files: list[Content] = []
1620
+ for c in search_contents(
1621
+ user_id=self._user_id,
1622
+ company_id=self._company_id,
1623
+ chat_id=self._chat_id,
1624
+ where={"ownerId": {"equals": self._chat_id}},
1625
+ ):
1626
+ if is_file_content(filename=c.key):
1627
+ files.append(c)
1628
+ if is_image_content(filename=c.key):
1629
+ images.append(c)
1630
+ return images, files