- Finish editing of Specialists with overview, agent - task - tool editor
- Split differrent caching mechanisms (types, version tree, config) into different cachers - Improve resource usage on starting components, and correct gevent usage - Refine repopack usage for eveai_app (too large) - Change nginx dockerfile to allow for specialist overviews being served statically
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import ast
|
||||
from datetime import datetime as dt, timezone as tz
|
||||
|
||||
from flask import request, redirect, flash, render_template, Blueprint, session, current_app
|
||||
from flask import request, redirect, flash, render_template, Blueprint, session, current_app, jsonify, url_for
|
||||
from flask_security import roles_accepted
|
||||
from langchain.agents import Agent
|
||||
from sqlalchemy import desc
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from common.models.document import Embedding, DocumentVersion, Retriever
|
||||
from common.models.interaction import (ChatSession, Interaction, InteractionEmbedding, Specialist, SpecialistRetriever)
|
||||
from common.models.interaction import (ChatSession, Interaction, InteractionEmbedding, Specialist, SpecialistRetriever,
|
||||
EveAIAgent, EveAITask, EveAITool)
|
||||
|
||||
from common.extensions import db, cache_manager
|
||||
from common.utils.model_logging_utils import set_logging_information, update_logging_information
|
||||
@@ -19,7 +21,8 @@ from common.utils.specialist_utils import initialize_specialist
|
||||
|
||||
from config.type_defs.specialist_types import SPECIALIST_TYPES
|
||||
|
||||
from .interaction_forms import (SpecialistForm, EditSpecialistForm)
|
||||
from .interaction_forms import (SpecialistForm, EditSpecialistForm, EditEveAIAgentForm, EditEveAITaskForm,
|
||||
EditEveAIToolForm)
|
||||
|
||||
interaction_bp = Blueprint('interaction_bp', __name__, url_prefix='/interaction')
|
||||
|
||||
@@ -140,7 +143,7 @@ def specialist():
|
||||
new_specialist.name = form.name.data
|
||||
new_specialist.description = form.description.data
|
||||
new_specialist.type = form.type.data
|
||||
new_specialist.type_version = cache_manager.specialist_config_cache.get_latest_version(new_specialist.type)
|
||||
new_specialist.type_version = cache_manager.specialists_version_tree_cache.get_latest_version(new_specialist.type)
|
||||
new_specialist.tuning = form.tuning.data
|
||||
|
||||
set_logging_information(new_specialist, dt.now(tz.utc))
|
||||
@@ -182,16 +185,29 @@ def edit_specialist(specialist_id):
|
||||
specialist = Specialist.query.get_or_404(specialist_id)
|
||||
form = EditSpecialistForm(request.form, obj=specialist)
|
||||
|
||||
specialist_config = cache_manager.specialist_config_cache.get_config(specialist.type, specialist.type_version)
|
||||
specialist_config = cache_manager.specialists_config_cache.get_config(specialist.type, specialist.type_version)
|
||||
configuration_config = specialist_config.get('configuration')
|
||||
form.add_dynamic_fields("configuration", configuration_config, specialist.configuration)
|
||||
|
||||
agent_rows = prepare_table_for_macro(specialist.agents,
|
||||
[('id', ''), ('name', ''), ('type', ''), ('type_version', '')])
|
||||
task_rows = prepare_table_for_macro(specialist.tasks,
|
||||
[('id', ''), ('name', ''), ('type', ''), ('type_version', '')])
|
||||
tool_rows = prepare_table_for_macro(specialist.tools,
|
||||
[('id', ''), ('name', ''), ('type', ''), ('type_version', '')])
|
||||
|
||||
# Construct the SVG overview path
|
||||
svg_filename = f"{specialist.type}_{specialist.type_version}_overview.svg"
|
||||
svg_path = url_for('static', filename=f'assets/specialists/{svg_filename}')
|
||||
|
||||
if request.method == 'GET':
|
||||
# Get the actual Retriever objects for the associated retriever_ids
|
||||
retriever_objects = Retriever.query.filter(
|
||||
Retriever.id.in_([sr.retriever_id for sr in specialist.retrievers])
|
||||
).all()
|
||||
form.retrievers.data = retriever_objects
|
||||
if specialist.description is None:
|
||||
specialist.description = specialist_config.get('metadata').get('description', '')
|
||||
|
||||
if form.validate_on_submit():
|
||||
# Update the basic fields
|
||||
@@ -230,11 +246,25 @@ def edit_specialist(specialist_id):
|
||||
db.session.rollback()
|
||||
flash(f'Failed to update specialist. Error: {str(e)}', 'danger')
|
||||
current_app.logger.error(f'Failed to update specialist {specialist_id}. Error: {str(e)}')
|
||||
return render_template('interaction/edit_specialist.html', form=form, specialist_id=specialist_id)
|
||||
return render_template('interaction/edit_specialist.html',
|
||||
form=form,
|
||||
specialist_id=specialist_id,
|
||||
agent_rows=agent_rows,
|
||||
task_rows=task_rows,
|
||||
tool_rows=tool_rows,
|
||||
prefixed_url_for=prefixed_url_for,
|
||||
svg_path=svg_path,)
|
||||
else:
|
||||
form_validation_failed(request, form)
|
||||
|
||||
return render_template('interaction/edit_specialist.html', form=form, specialist_id=specialist_id)
|
||||
return render_template('interaction/edit_specialist.html',
|
||||
form=form,
|
||||
specialist_id=specialist_id,
|
||||
agent_rows=agent_rows,
|
||||
task_rows=task_rows,
|
||||
tool_rows=tool_rows,
|
||||
prefixed_url_for=prefixed_url_for,
|
||||
svg_path=svg_path,)
|
||||
|
||||
|
||||
@interaction_bp.route('/specialists', methods=['GET', 'POST'])
|
||||
@@ -268,3 +298,154 @@ def handle_specialist_selection():
|
||||
|
||||
return redirect(prefixed_url_for('interaction_bp.specialists'))
|
||||
|
||||
|
||||
# Routes for Agent management
|
||||
@interaction_bp.route('/agent/<int:agent_id>/edit', methods=['GET'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def edit_agent(agent_id):
|
||||
agent = EveAIAgent.query.get_or_404(agent_id)
|
||||
form = EditEveAIAgentForm(obj=agent)
|
||||
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
# Return just the form portion for AJAX requests
|
||||
return render_template('interaction/components/edit_agent.html',
|
||||
form=form,
|
||||
agent=agent,
|
||||
title="Edit Agent",
|
||||
description="Configure the agent with company-specific details if required",
|
||||
submit_text="Save Agent")
|
||||
|
||||
|
||||
@interaction_bp.route('/agent/<int:agent_id>/save', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def save_agent(agent_id):
|
||||
agent = EveAIAgent.query.get_or_404(agent_id) if agent_id else EveAIAgent()
|
||||
tenant_id = session.get('tenant').get('id')
|
||||
form = EditEveAIAgentForm(obj=agent)
|
||||
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
form.populate_obj(agent)
|
||||
update_logging_information(agent, dt.now(tz.utc))
|
||||
if not agent_id: # New agent
|
||||
db.session.add(agent)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'message': 'Agent saved successfully'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
current_app.logger.error(f'Failed to save agent {agent_id} for tenant {tenant_id}. Error: {str(e)}')
|
||||
return jsonify({'success': False, 'message': f"Failed to save agent {agent_id}: {str(e)}"})
|
||||
|
||||
return jsonify({'success': False, 'message': 'Validation failed'})
|
||||
|
||||
|
||||
# Routes for Task management
|
||||
@interaction_bp.route('/task/<int:task_id>/edit', methods=['GET'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def edit_task(task_id):
|
||||
task = EveAITask.query.get_or_404(task_id)
|
||||
form = EditEveAITaskForm(obj=task)
|
||||
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
return render_template('interaction/components/edit_task.html',
|
||||
form=form,
|
||||
task=task)
|
||||
|
||||
|
||||
@interaction_bp.route('/task/<int:task_id>/save', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def save_task(task_id):
|
||||
task = EveAITask.query.get_or_404(task_id) if task_id else EveAITask()
|
||||
tenant_id = session.get('tenant').get('id')
|
||||
form = EditEveAITaskForm(obj=task) # Replace with actual task form
|
||||
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
form.populate_obj(task)
|
||||
update_logging_information(task, dt.now(tz.utc))
|
||||
if not task_id: # New task
|
||||
db.session.add(task)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'message': 'Task saved successfully'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
current_app.logger.error(f'Failed to save task {task_id} for tenant {tenant_id}. Error: {str(e)}')
|
||||
return jsonify({'success': False, 'message': f"Failed to save task {task_id}: {str(e)}"})
|
||||
|
||||
return jsonify({'success': False, 'message': 'Validation failed'})
|
||||
|
||||
|
||||
# Routes for Tool management
|
||||
@interaction_bp.route('/tool/<int:tool_id>/edit', methods=['GET'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def edit_tool(tool_id):
|
||||
tool = EveAITool.query.get_or_404(tool_id)
|
||||
form = EditEveAIToolForm(obj=tool)
|
||||
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
return render_template('interaction/components/edit_tool.html',
|
||||
form=form,
|
||||
tool=tool)
|
||||
|
||||
|
||||
@interaction_bp.route('/tool/<int:tool_id>/save', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def save_tool(tool_id):
|
||||
tool = EveAITool.query.get_or_404(tool_id) if tool_id else EveAITool()
|
||||
tenant_id = session.get('tenant').get('id')
|
||||
form = EditEveAIToolForm(obj=tool) # Replace with actual tool form
|
||||
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
form.populate_obj(tool)
|
||||
update_logging_information(tool, dt.now(tz.utc))
|
||||
if not tool_id: # New tool
|
||||
db.session.add(tool)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True, 'message': 'Tool saved successfully'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
current_app.logger.error(f'Failed to save tool {tool_id} for tenant {tenant_id}. Error: {str(e)}')
|
||||
return jsonify({'success': False, 'message': f"Failed to save tool {tool_id}: {str(e)}"})
|
||||
|
||||
return jsonify({'success': False, 'message': 'Validation failed'})
|
||||
|
||||
|
||||
# Component selection handlers
|
||||
@interaction_bp.route('/handle_agent_selection', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_agent_selection():
|
||||
agent_identification = request.form['selected_row']
|
||||
agent_id = ast.literal_eval(agent_identification).get('value')
|
||||
action = request.form.get('action')
|
||||
|
||||
if action == "edit_agent":
|
||||
return redirect(prefixed_url_for('interaction_bp.edit_agent', agent_id=agent_id))
|
||||
|
||||
return redirect(prefixed_url_for('interaction_bp.edit_specialist'))
|
||||
|
||||
|
||||
@interaction_bp.route('/handle_task_selection', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_task_selection():
|
||||
task_identification = request.form['selected_row']
|
||||
task_id = ast.literal_eval(task_identification).get('value')
|
||||
action = request.form.get('action')
|
||||
|
||||
if action == "edit_task":
|
||||
return redirect(prefixed_url_for('interaction_bp.edit_task', task_id=task_id))
|
||||
|
||||
return redirect(prefixed_url_for('interaction_bp.edit_specialist'))
|
||||
|
||||
|
||||
@interaction_bp.route('/handle_tool_selection', methods=['POST'])
|
||||
@roles_accepted('Super User', 'Tenant Admin')
|
||||
def handle_tool_selection():
|
||||
tool_identification = request.form['selected_row']
|
||||
tool_id = ast.literal_eval(tool_identification).get('value')
|
||||
action = request.form.get('action')
|
||||
|
||||
if action == "edit_tool":
|
||||
return redirect(prefixed_url_for('interaction_bp.edit_tool', tool_id=tool_id))
|
||||
|
||||
return redirect(prefixed_url_for('interaction_bp.edit_specialist'))
|
||||
Reference in New Issue
Block a user