+110









Yeuoly
GitHub
takatost
kurokobo
Novice Lee
zxhlyh
AkaraChen
Yi
Joel
JzoNg
twwu
Hiroshi Fujita
AkaraChen
NFish
Wu Tianwei
非法操作
Novice
Hiroki Nagai
Gen Sato
eux
huangzhuo1949
huangzhuo
lotsik
crazywoola
nite-knite
Jyong
github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
gakkiyomi
CN-P5
CN-P5
Chuehnone
yihong
Kevin9703
-LAN-
Boris Feld
mbo
mabo
Warren Chen
JzoNgKVO
jiandanfeng
zhu-an
zhaoqingyu.1075
海狸大師
Xu Song
rayshaw001
Ding Jiatong
Bowen Liang
JasonVV
le0zh
zhuxinliang
k-zaku
luckylhb90
hobo.l
jiangbo721
刘江波
Shun Miyazawa
EricPan
crazywoola
sino
Jhvcc
lowell
Boris Polonsky
Ademílson Tonato
Ademílson Tonato
IWAI, Masaharu <[email protected]>
Yueh-Po Peng
Jason
Xin Zhang
yjc980121
heyszt
Abdullah AlOsaimi
Abdullah AlOsaimi
Yingchun Lai
Hash Brown
zuodongxu
Masashi Tomooka
aplio
Obada Khalili
Nam Vu
Kei YAMAZAKI
TechnoHouse
Riddhimaan-Senapati
MaFee921
te-chan
HQidea
Joshbly
xhe
weiwenyan-dev
ex_wenyan.wei
engchina
engchina
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
呆萌闷油瓶
Kemal
Lazy_Frog
Yi Xiao
Steven sun
steven
Kalo Chin
Katy Tao
depy
胡春东
Junjie.M
MuYu
Naoki Takashima
Summer-Gu
Fei He
ybalbert001
Yuanbo Li
douxc
liuzhenghua
Wu Jiayang
Your Name
kimjion
AugNSo
llinvokerl
liusurong.lsr
Vasu Negi
Hundredwz
Xiyuan Chen
403e2d58b9
Signed-off-by: yihong0618 <[email protected]> Signed-off-by: -LAN- <[email protected]> Signed-off-by: xhe <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: takatost <[email protected]> Co-authored-by: kurokobo <[email protected]> Co-authored-by: Novice Lee <[email protected]> Co-authored-by: zxhlyh <[email protected]> Co-authored-by: AkaraChen <[email protected]> Co-authored-by: Yi <[email protected]> Co-authored-by: Joel <[email protected]> Co-authored-by: JzoNg <[email protected]> Co-authored-by: twwu <[email protected]> Co-authored-by: Hiroshi Fujita <[email protected]> Co-authored-by: AkaraChen <[email protected]> Co-authored-by: NFish <[email protected]> Co-authored-by: Wu Tianwei <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Novice <[email protected]> Co-authored-by: Hiroki Nagai <[email protected]> Co-authored-by: Gen Sato <[email protected]> Co-authored-by: eux <[email protected]> Co-authored-by: huangzhuo1949 <[email protected]> Co-authored-by: huangzhuo <[email protected]> Co-authored-by: lotsik <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: nite-knite <[email protected]> Co-authored-by: Jyong <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <[email protected]> Co-authored-by: CN-P5 <[email protected]> Co-authored-by: CN-P5 <[email protected]> Co-authored-by: Chuehnone <[email protected]> Co-authored-by: yihong <[email protected]> Co-authored-by: Kevin9703 <[email protected]> Co-authored-by: -LAN- <[email protected]> Co-authored-by: Boris Feld <[email protected]> Co-authored-by: mbo <[email protected]> Co-authored-by: mabo <[email protected]> Co-authored-by: Warren Chen <[email protected]> Co-authored-by: JzoNgKVO <[email protected]> Co-authored-by: jiandanfeng <[email protected]> Co-authored-by: zhu-an <[email protected]> Co-authored-by: zhaoqingyu.1075 <[email protected]> Co-authored-by: 海狸大師 <[email protected]> Co-authored-by: Xu Song <[email protected]> Co-authored-by: rayshaw001 <[email protected]> Co-authored-by: Ding Jiatong <[email protected]> Co-authored-by: Bowen Liang <[email protected]> Co-authored-by: JasonVV <[email protected]> Co-authored-by: le0zh <[email protected]> Co-authored-by: zhuxinliang <[email protected]> Co-authored-by: k-zaku <[email protected]> Co-authored-by: luckylhb90 <[email protected]> Co-authored-by: hobo.l <[email protected]> Co-authored-by: jiangbo721 <[email protected]> Co-authored-by: 刘江波 <[email protected]> Co-authored-by: Shun Miyazawa <[email protected]> Co-authored-by: EricPan <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: sino <[email protected]> Co-authored-by: Jhvcc <[email protected]> Co-authored-by: lowell <[email protected]> Co-authored-by: Boris Polonsky <[email protected]> Co-authored-by: Ademílson Tonato <[email protected]> Co-authored-by: Ademílson Tonato <[email protected]> Co-authored-by: IWAI, Masaharu <[email protected]> Co-authored-by: Yueh-Po Peng (Yabi) <[email protected]> Co-authored-by: Jason <[email protected]> Co-authored-by: Xin Zhang <[email protected]> Co-authored-by: yjc980121 <[email protected]> Co-authored-by: heyszt <[email protected]> Co-authored-by: Abdullah AlOsaimi <[email protected]> Co-authored-by: Abdullah AlOsaimi <[email protected]> Co-authored-by: Yingchun Lai <[email protected]> Co-authored-by: Hash Brown <[email protected]> Co-authored-by: zuodongxu <[email protected]> Co-authored-by: Masashi Tomooka <[email protected]> Co-authored-by: aplio <[email protected]> Co-authored-by: Obada Khalili <[email protected]> Co-authored-by: Nam Vu <[email protected]> Co-authored-by: Kei YAMAZAKI <[email protected]> Co-authored-by: TechnoHouse <[email protected]> Co-authored-by: Riddhimaan-Senapati <[email protected]> Co-authored-by: MaFee921 <[email protected]> Co-authored-by: te-chan <[email protected]> Co-authored-by: HQidea <[email protected]> Co-authored-by: Joshbly <[email protected]> Co-authored-by: xhe <[email protected]> Co-authored-by: weiwenyan-dev <[email protected]> Co-authored-by: ex_wenyan.wei <[email protected]> Co-authored-by: engchina <[email protected]> Co-authored-by: engchina <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 呆萌闷油瓶 <[email protected]> Co-authored-by: Kemal <[email protected]> Co-authored-by: Lazy_Frog <[email protected]> Co-authored-by: Yi Xiao <[email protected]> Co-authored-by: Steven sun <[email protected]> Co-authored-by: steven <[email protected]> Co-authored-by: Kalo Chin <[email protected]> Co-authored-by: Katy Tao <[email protected]> Co-authored-by: depy <[email protected]> Co-authored-by: 胡春东 <[email protected]> Co-authored-by: Junjie.M <[email protected]> Co-authored-by: MuYu <[email protected]> Co-authored-by: Naoki Takashima <[email protected]> Co-authored-by: Summer-Gu <[email protected]> Co-authored-by: Fei He <[email protected]> Co-authored-by: ybalbert001 <[email protected]> Co-authored-by: Yuanbo Li <[email protected]> Co-authored-by: douxc <[email protected]> Co-authored-by: liuzhenghua <[email protected]> Co-authored-by: Wu Jiayang <[email protected]> Co-authored-by: Your Name <[email protected]> Co-authored-by: kimjion <[email protected]> Co-authored-by: AugNSo <[email protected]> Co-authored-by: llinvokerl <[email protected]> Co-authored-by: liusurong.lsr <[email protected]> Co-authored-by: Vasu Negi <[email protected]> Co-authored-by: Hundredwz <[email protected]> Co-authored-by: Xiyuan Chen <[email protected]>
177 lines
6.6 KiB
Python
177 lines
6.6 KiB
Python
import threading
|
|
from typing import Optional
|
|
|
|
import pytz
|
|
from flask_login import current_user # type: ignore
|
|
|
|
import contexts
|
|
from core.app.app_config.easy_ui_based_app.agent.manager import AgentConfigManager
|
|
from core.plugin.manager.agent import PluginAgentManager
|
|
from core.plugin.manager.exc import PluginDaemonClientSideError
|
|
from core.tools.tool_manager import ToolManager
|
|
from extensions.ext_database import db
|
|
from models.account import Account
|
|
from models.model import App, Conversation, EndUser, Message, MessageAgentThought
|
|
|
|
|
|
class AgentService:
|
|
@classmethod
|
|
def get_agent_logs(cls, app_model: App, conversation_id: str, message_id: str) -> dict:
|
|
"""
|
|
Service to get agent logs
|
|
"""
|
|
contexts.plugin_tool_providers.set({})
|
|
contexts.plugin_tool_providers_lock.set(threading.Lock())
|
|
|
|
conversation: Conversation | None = (
|
|
db.session.query(Conversation)
|
|
.filter(
|
|
Conversation.id == conversation_id,
|
|
Conversation.app_id == app_model.id,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
if not conversation:
|
|
raise ValueError(f"Conversation not found: {conversation_id}")
|
|
|
|
message: Optional[Message] = (
|
|
db.session.query(Message)
|
|
.filter(
|
|
Message.id == message_id,
|
|
Message.conversation_id == conversation_id,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
if not message:
|
|
raise ValueError(f"Message not found: {message_id}")
|
|
|
|
agent_thoughts: list[MessageAgentThought] = message.agent_thoughts
|
|
|
|
if conversation.from_end_user_id:
|
|
# only select name field
|
|
executor = (
|
|
db.session.query(EndUser, EndUser.name).filter(EndUser.id == conversation.from_end_user_id).first()
|
|
)
|
|
else:
|
|
executor = (
|
|
db.session.query(Account, Account.name).filter(Account.id == conversation.from_account_id).first()
|
|
)
|
|
|
|
if executor:
|
|
executor = executor.name
|
|
else:
|
|
executor = "Unknown"
|
|
|
|
timezone = pytz.timezone(current_user.timezone)
|
|
|
|
app_model_config = app_model.app_model_config
|
|
if not app_model_config:
|
|
raise ValueError("App model config not found")
|
|
|
|
result = {
|
|
"meta": {
|
|
"status": "success",
|
|
"executor": executor,
|
|
"start_time": message.created_at.astimezone(timezone).isoformat(),
|
|
"elapsed_time": message.provider_response_latency,
|
|
"total_tokens": message.answer_tokens + message.message_tokens,
|
|
"agent_mode": app_model_config.agent_mode_dict.get("strategy", "react"),
|
|
"iterations": len(agent_thoughts),
|
|
},
|
|
"iterations": [],
|
|
"files": message.message_files,
|
|
}
|
|
|
|
agent_config = AgentConfigManager.convert(app_model_config.to_dict())
|
|
if not agent_config:
|
|
raise ValueError("Agent config not found")
|
|
|
|
agent_tools = agent_config.tools or []
|
|
|
|
def find_agent_tool(tool_name: str):
|
|
for agent_tool in agent_tools:
|
|
if agent_tool.tool_name == tool_name:
|
|
return agent_tool
|
|
|
|
for agent_thought in agent_thoughts:
|
|
tools = agent_thought.tools
|
|
tool_labels = agent_thought.tool_labels
|
|
tool_meta = agent_thought.tool_meta
|
|
tool_inputs = agent_thought.tool_inputs_dict
|
|
tool_outputs = agent_thought.tool_outputs_dict or {}
|
|
tool_calls = []
|
|
for tool in tools:
|
|
tool_name = tool
|
|
tool_label = tool_labels.get(tool_name, tool_name)
|
|
tool_input = tool_inputs.get(tool_name, {})
|
|
tool_output = tool_outputs.get(tool_name, {})
|
|
tool_meta_data = tool_meta.get(tool_name, {})
|
|
tool_config = tool_meta_data.get("tool_config", {})
|
|
if tool_config.get("tool_provider_type", "") != "dataset-retrieval":
|
|
tool_icon = ToolManager.get_tool_icon(
|
|
tenant_id=app_model.tenant_id,
|
|
provider_type=tool_config.get("tool_provider_type", ""),
|
|
provider_id=tool_config.get("tool_provider", ""),
|
|
)
|
|
if not tool_icon:
|
|
tool_entity = find_agent_tool(tool_name)
|
|
if tool_entity:
|
|
tool_icon = ToolManager.get_tool_icon(
|
|
tenant_id=app_model.tenant_id,
|
|
provider_type=tool_entity.provider_type,
|
|
provider_id=tool_entity.provider_id,
|
|
)
|
|
else:
|
|
tool_icon = ""
|
|
|
|
tool_calls.append(
|
|
{
|
|
"status": "success" if not tool_meta_data.get("error") else "error",
|
|
"error": tool_meta_data.get("error"),
|
|
"time_cost": tool_meta_data.get("time_cost", 0),
|
|
"tool_name": tool_name,
|
|
"tool_label": tool_label,
|
|
"tool_input": tool_input,
|
|
"tool_output": tool_output,
|
|
"tool_parameters": tool_meta_data.get("tool_parameters", {}),
|
|
"tool_icon": tool_icon,
|
|
}
|
|
)
|
|
|
|
result["iterations"].append(
|
|
{
|
|
"tokens": agent_thought.tokens,
|
|
"tool_calls": tool_calls,
|
|
"tool_raw": {
|
|
"inputs": agent_thought.tool_input,
|
|
"outputs": agent_thought.observation,
|
|
},
|
|
"thought": agent_thought.thought,
|
|
"created_at": agent_thought.created_at.isoformat(),
|
|
"files": agent_thought.files,
|
|
}
|
|
)
|
|
|
|
return result
|
|
|
|
@classmethod
|
|
def list_agent_providers(cls, user_id: str, tenant_id: str):
|
|
"""
|
|
List agent providers
|
|
"""
|
|
manager = PluginAgentManager()
|
|
return manager.fetch_agent_strategy_providers(tenant_id)
|
|
|
|
@classmethod
|
|
def get_agent_provider(cls, user_id: str, tenant_id: str, provider_name: str):
|
|
"""
|
|
Get agent provider
|
|
"""
|
|
manager = PluginAgentManager()
|
|
try:
|
|
return manager.fetch_agent_strategy_provider(tenant_id, provider_name)
|
|
except PluginDaemonClientSideError as e:
|
|
raise ValueError(str(e)) from e
|