|
|
|
|
@ -5,7 +5,7 @@ import uuid
|
|
|
|
|
from collections.abc import Generator, Mapping
|
|
|
|
|
from typing import Any, Literal, Optional, Union, overload
|
|
|
|
|
|
|
|
|
|
from flask import Flask, current_app
|
|
|
|
|
from flask import Flask, copy_current_request_context, current_app, has_request_context
|
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
|
|
|
|
|
@ -158,7 +158,6 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|
|
|
|
trace_manager=trace_manager,
|
|
|
|
|
workflow_run_id=workflow_run_id,
|
|
|
|
|
)
|
|
|
|
|
contexts.tenant_id.set(application_generate_entity.app_config.tenant_id)
|
|
|
|
|
contexts.plugin_tool_providers.set({})
|
|
|
|
|
contexts.plugin_tool_providers_lock.set(threading.Lock())
|
|
|
|
|
|
|
|
|
|
@ -240,7 +239,6 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|
|
|
|
node_id=node_id, inputs=args["inputs"]
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
contexts.tenant_id.set(application_generate_entity.app_config.tenant_id)
|
|
|
|
|
contexts.plugin_tool_providers.set({})
|
|
|
|
|
contexts.plugin_tool_providers_lock.set(threading.Lock())
|
|
|
|
|
|
|
|
|
|
@ -316,7 +314,6 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|
|
|
|
extras={"auto_generate_conversation_name": False},
|
|
|
|
|
single_loop_run=AdvancedChatAppGenerateEntity.SingleLoopRunEntity(node_id=node_id, inputs=args["inputs"]),
|
|
|
|
|
)
|
|
|
|
|
contexts.tenant_id.set(application_generate_entity.app_config.tenant_id)
|
|
|
|
|
contexts.plugin_tool_providers.set({})
|
|
|
|
|
contexts.plugin_tool_providers_lock.set(threading.Lock())
|
|
|
|
|
|
|
|
|
|
@ -399,18 +396,23 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|
|
|
|
message_id=message.id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# new thread
|
|
|
|
|
worker_thread = threading.Thread(
|
|
|
|
|
target=self._generate_worker,
|
|
|
|
|
kwargs={
|
|
|
|
|
"flask_app": current_app._get_current_object(), # type: ignore
|
|
|
|
|
"application_generate_entity": application_generate_entity,
|
|
|
|
|
"queue_manager": queue_manager,
|
|
|
|
|
"conversation_id": conversation.id,
|
|
|
|
|
"message_id": message.id,
|
|
|
|
|
"context": contextvars.copy_context(),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
# new thread with request context and contextvars
|
|
|
|
|
context = contextvars.copy_context()
|
|
|
|
|
|
|
|
|
|
@copy_current_request_context
|
|
|
|
|
def worker_with_context():
|
|
|
|
|
# Run the worker within the copied context
|
|
|
|
|
return context.run(
|
|
|
|
|
self._generate_worker,
|
|
|
|
|
flask_app=current_app._get_current_object(), # type: ignore
|
|
|
|
|
application_generate_entity=application_generate_entity,
|
|
|
|
|
queue_manager=queue_manager,
|
|
|
|
|
conversation_id=conversation.id,
|
|
|
|
|
message_id=message.id,
|
|
|
|
|
context=context,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
worker_thread = threading.Thread(target=worker_with_context)
|
|
|
|
|
|
|
|
|
|
worker_thread.start()
|
|
|
|
|
|
|
|
|
|
@ -449,8 +451,22 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
|
|
|
|
|
"""
|
|
|
|
|
for var, val in context.items():
|
|
|
|
|
var.set(val)
|
|
|
|
|
|
|
|
|
|
# Save current user before entering new app context
|
|
|
|
|
from flask import g
|
|
|
|
|
|
|
|
|
|
saved_user = None
|
|
|
|
|
if has_request_context() and hasattr(g, "_login_user"):
|
|
|
|
|
saved_user = g._login_user
|
|
|
|
|
|
|
|
|
|
with flask_app.app_context():
|
|
|
|
|
try:
|
|
|
|
|
# Restore user in new app context
|
|
|
|
|
if saved_user is not None:
|
|
|
|
|
from flask import g
|
|
|
|
|
|
|
|
|
|
g._login_user = saved_user
|
|
|
|
|
|
|
|
|
|
# get conversation and message
|
|
|
|
|
conversation = self._get_conversation(conversation_id)
|
|
|
|
|
message = self._get_message(message_id)
|
|
|
|
|
|