| 1 | from helpers.api import ApiHandler, Input, Output, Request, Response |
| 2 | |
| 3 | |
| 4 | from helpers import settings, projects, guids |
| 5 | from agent import AgentContext |
| 6 | |
| 7 | |
| 8 | class CreateChat(ApiHandler): |
| 9 | async def process(self, input: Input, request: Request) -> Output: |
| 10 | current_ctxid = input.get("current_context", "") # current context id |
| 11 | new_ctxid = input.get("new_context", guids.generate_id()) # given or new guid |
| 12 | |
| 13 | # context instance - get or create |
| 14 | current_context = AgentContext.get(current_ctxid) |
| 15 | |
| 16 | # get/create new context |
| 17 | new_context = self.use_context(new_ctxid) |
| 18 | |
| 19 | # copy selected data from current to new context |
| 20 | if current_context and settings.get_settings().get("chat_inherit_project", True): |
| 21 | current_data_1 = current_context.get_data(projects.CONTEXT_DATA_KEY_PROJECT) |
| 22 | if current_data_1: |
| 23 | new_context.set_data(projects.CONTEXT_DATA_KEY_PROJECT, current_data_1) |
| 24 | current_data_2 = current_context.get_output_data(projects.CONTEXT_DATA_KEY_PROJECT) |
| 25 | if current_data_2: |
| 26 | new_context.set_output_data(projects.CONTEXT_DATA_KEY_PROJECT, current_data_2) |
| 27 | |
| 28 | projects.reconcile_agent_profile( |
| 29 | new_context, projects.get_context_project_name(new_context) |
| 30 | ) |
| 31 | |
| 32 | # copy model override from current context (only if override is allowed) |
| 33 | if current_context: |
| 34 | model_override = current_context.get_data("chat_model_override") |
| 35 | if model_override: |
| 36 | from plugins._model_config.helpers.model_config import is_chat_override_allowed |
| 37 | if is_chat_override_allowed(new_context.agent0): |
| 38 | new_context.set_data("chat_model_override", model_override) |
| 39 | |
| 40 | # New context should appear in other tabs' chat lists via state_push. |
| 41 | from helpers.state_monitor_integration import mark_dirty_all |
| 42 | mark_dirty_all(reason="api.chat_create.CreateChat") |
| 43 | |
| 44 | return { |
| 45 | "ok": True, |
| 46 | "ctxid": new_context.id, |
| 47 | "message": "Context created.", |
| 48 | } |