ForcePilot/web/src/views/AgentView.vue

731 lines
18 KiB
Vue
Raw Normal View History

2025-03-25 05:40:07 +08:00
<template>
2025-03-31 23:02:05 +08:00
<div class="agent-view">
<div class="agent-view-header">
<div class="header-left">
<div class="header-item">
<a-button class="header-button" @click="toggleSidebar">
<template #icon><MenuOutlined /></template>
</a-button>
2025-04-02 13:00:25 +08:00
</div>
<div class="header-item">
<a-select
v-model:value="selectedAgentId"
class="agent-list"
style="width: 200px"
@change="selectAgent"
2025-04-05 02:23:32 +08:00
>
<a-select-option
v-for="(agent, name) in agents"
:key="name"
:value="name"
>
<div class="agent-option">
智能体{{ agent.name }}
<StarFilled v-if="name === defaultAgentId" class="default-icon" />
</div>
</a-select-option>
</a-select>
</div>
</div>
<div class="header-center">
</div>
<div class="header-right">
<div class="header-item">
2025-05-04 21:04:01 +08:00
<a-button
class="header-button"
2025-05-04 21:04:01 +08:00
@click="goToAgentPage"
v-if="selectedAgentId"
>
<template #icon><LinkOutlined /></template>
打开独立页面
</a-button>
<a-tooltip :title="isDefaultAgent ? '当前为默认智能体' : '设为默认智能体'" placement="left">
<a-button
class="header-button primary-action"
@click="setAsDefaultAgent"
v-if="selectedAgentId && userStore.isAdmin"
:disabled="isDefaultAgent"
2025-05-02 23:56:59 +08:00
>
<template #icon><StarOutlined /></template>
</a-button>
</a-tooltip>
2025-04-05 02:23:32 +08:00
2025-04-02 13:00:25 +08:00
</div>
2025-03-31 23:02:05 +08:00
</div>
</div>
<div class="agent-view-body">
<!-- 左侧智能体列表侧边栏 -->
<div class="sidebar" :class="{ 'is-open': state.agentSiderbarConfigOpen }">
<div class="agent-info">
<h3 @click="toggleDebugMode">描述</h3>
<p>{{ selectedAgent.description }}</p>
<pre v-if="state.debug_mode">{{ selectedAgent }}</pre>
<div v-if="selectedAgentId && configSchema" class="config-modal-content">
<!-- 配置表单 -->
<a-form :model="agentConfig" layout="vertical">
<a-alert v-if="state.isEmptyConfig" type="warning" message="该智能体没有配置项" show-icon/>
<a-alert v-if="!selectedAgent.has_checkpointer" type="error" message="该智能体没有配置 Checkpointer功能无法正常使用参考https://langchain-ai.github.io/langgraph/concepts/persistence/" show-icon/>
<!-- 统一显示所有配置项 -->
<template v-for="(value, key) in configurableItems" :key="key">
<a-form-item
:label="getConfigLabel(key, value)"
:name="key"
class="config-item"
>
<p v-if="value.description" class="description">{{ value.description }}</p>
2025-06-16 23:08:40 +08:00
<!-- key匹配 -->
<div v-if="key === 'model'" class="agent-model">
<p><small>注意部分模型对于 Tool Calling 的支持不稳定建议采用{{ value.options }} </small></p>
<ModelSelectorComponent
@select-model="handleModelChange"
:model_name="agentConfig[key] ? agentConfig[key].split('/').slice(1).join('/') : ''"
:model_provider="agentConfig[key] ? agentConfig[key].split('/')[0] : ''"
/>
</div>
<a-textarea
v-else-if="key === 'system_prompt'"
v-model:value="agentConfig[key]"
:rows="4"
:placeholder="getPlaceholder(key, value)"
/>
2025-06-16 23:08:40 +08:00
<!-- 数据类型匹配 -->
<a-switch
v-else-if="typeof agentConfig[key] === 'boolean'"
v-model:checked="agentConfig[key]"
/>
<a-select
v-else-if="value?.options && value?.type === 'str'"
v-model:value="agentConfig[key]"
>
<a-select-option v-for="option in value.options" :key="option" :value="option"></a-select-option>
</a-select>
<a-select
v-else-if="value?.options && value?.type === 'list'"
v-model:value="agentConfig[key]"
mode="multiple"
>
<a-select-option v-for="option in value.options" :key="option" :value="option"></a-select-option>
</a-select>
<a-input
v-else
v-model:value="agentConfig[key]"
:placeholder="getPlaceholder(key, value)"
/>
</a-form-item>
</template>
<!-- 弹窗底部按钮 -->
<div class="form-actions" v-if="!state.isEmptyConfig">
<div class="form-actions-left">
<a-button type="primary" @click="saveConfig">保存并发布配置</a-button>
<a-button @click="resetConfig">重置</a-button>
</div>
</div>
</a-form>
2025-04-01 22:16:07 +08:00
</div>
2025-04-05 02:23:32 +08:00
<!-- 添加requirements显示部分 -->
<div v-if="agents[selectedAgentId]?.requirements && agents[selectedAgentId]?.requirements.length > 0" class="info-section">
<h3>所需环境变量:</h3>
<div class="requirements-list">
<a-tag v-for="req in agents[selectedAgentId].requirements" :key="req">
{{ req }}
</a-tag>
</div>
2025-04-02 00:00:04 +08:00
</div>
</div>
2025-04-02 00:00:04 +08:00
</div>
2025-04-03 20:45:58 +08:00
<!-- 中间内容区域 -->
<div class="content">
<AgentChatComponent
:agent-id="selectedAgentId"
:config="agentConfig"
:state="state"
@open-config="toggleConfigSidebar(true)"
>
</AgentChatComponent>
</div>
</div>
2025-03-25 05:40:07 +08:00
</div>
</template>
2025-03-31 23:02:05 +08:00
<script setup>
2025-04-02 00:00:04 +08:00
import { ref, onMounted, reactive, watch, computed, h } from 'vue';
2025-04-05 02:23:32 +08:00
import { useRouter } from 'vue-router';
2025-04-02 00:00:04 +08:00
import {
CloseOutlined,
2025-04-05 02:23:32 +08:00
SettingOutlined,
2025-05-02 23:56:59 +08:00
LinkOutlined,
StarOutlined,
MenuOutlined,
2025-05-02 23:56:59 +08:00
StarFilled
2025-04-02 00:00:04 +08:00
} from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
2025-03-31 23:02:05 +08:00
import AgentChatComponent from '@/components/AgentChatComponent.vue';
import ModelSelectorComponent from '@/components/ModelSelectorComponent.vue';
2025-05-02 23:56:59 +08:00
import { useUserStore } from '@/stores/user';
import { chatApi } from '@/apis/auth_api';
import { systemConfigApi } from '@/apis/admin_api';
2025-03-31 23:02:05 +08:00
2025-04-05 02:23:32 +08:00
// 路由
const router = useRouter();
2025-05-02 23:56:59 +08:00
const userStore = useUserStore();
2025-04-05 02:23:32 +08:00
2025-03-31 23:02:05 +08:00
// 状态
const agents = ref({});
const selectedAgentId = ref(null);
2025-05-02 23:56:59 +08:00
const defaultAgentId = ref(null); // 存储默认智能体ID
2025-04-01 22:16:07 +08:00
const state = reactive({
agentSiderbarConfigOpen: true,
2025-04-02 13:00:25 +08:00
debug_mode: false,
isEmptyConfig: computed(() =>
!selectedAgentId.value ||
Object.keys(configurableItems.value).length === 0
)
2025-04-01 22:16:07 +08:00
});
const selectedAgent = computed(() => agents.value[selectedAgentId.value] || {});
const configSchema = computed(() => selectedAgent.value.config_schema || {});
2025-04-02 13:00:25 +08:00
const configurableItems = computed(() => configSchema.value.configurable_items || {});
2025-04-02 00:00:04 +08:00
// 配置状态
2025-04-02 13:00:25 +08:00
const agentConfig = ref({});
2025-04-02 00:00:04 +08:00
2025-05-02 23:56:59 +08:00
// 检查是否为默认智能体
const isDefaultAgent = computed(() => {
return selectedAgentId.value === defaultAgentId.value;
});
2025-04-03 16:02:14 +08:00
2025-05-02 23:56:59 +08:00
// 设置为默认智能体
const setAsDefaultAgent = async () => {
if (!selectedAgentId.value || !userStore.isAdmin) return;
2025-04-03 16:02:14 +08:00
2025-05-02 23:56:59 +08:00
try {
await systemConfigApi.setDefaultAgent(selectedAgentId.value);
defaultAgentId.value = selectedAgentId.value;
message.success('已将当前智能体设为默认');
} catch (error) {
console.error('设置默认智能体错误:', error);
message.error(error.message || '设置默认智能体时发生错误');
}
2025-04-03 20:45:58 +08:00
};
2025-05-02 23:56:59 +08:00
// 获取默认智能体ID
const fetchDefaultAgent = async () => {
try {
const data = await chatApi.getDefaultAgent();
defaultAgentId.value = data.default_agent_id;
console.log("Default agent ID:", defaultAgentId.value);
} catch (error) {
console.error('获取默认智能体错误:', error);
}
2025-04-03 20:45:58 +08:00
};
// 获取智能体列表
const fetchAgents = async () => {
try {
2025-05-02 23:56:59 +08:00
const data = await chatApi.getAgents();
// 将数组转换为对象
agents.value = data.agents.reduce((acc, agent) => {
acc[agent.name] = agent;
return acc;
}, {});
// console.log("agents", agents.value);
// 加载当前选中智能体的配置
if (selectedAgentId.value) {
loadAgentConfig();
}
} catch (error) {
console.error('获取智能体错误:', error);
}
};
2025-04-02 00:00:04 +08:00
// 根据选中的智能体加载配置
const loadAgentConfig = async () => {
2025-04-02 13:00:25 +08:00
// BUG: 目前消息重置有问题,需要重置消息
2025-04-02 00:00:04 +08:00
if (!selectedAgentId.value || !agents.value[selectedAgentId.value]) return;
const agent = agents.value[selectedAgentId.value];
2025-04-02 13:00:25 +08:00
const schema = agent.config_schema || {};
const items = schema.configurable_items || {};
2025-04-02 00:00:04 +08:00
// 重置配置
2025-04-02 13:00:25 +08:00
agentConfig.value = {};
// 初始化基础配置项
if (schema.system_prompt) {
agentConfig.value.system_prompt = schema.system_prompt;
}
if (schema.model) {
agentConfig.value.model = schema.model;
}
if (schema.tools && schema.tools.length > 0 && schema.tools[0] != 'undefined') {
agentConfig.value.tools = schema.tools;
}
2025-04-02 13:00:25 +08:00
// 初始化可配置项
Object.keys(items).forEach(key => {
const item = items[key];
2025-04-02 00:00:04 +08:00
2025-04-02 13:00:25 +08:00
// 根据类型设置默认值
if (typeof item.default === 'boolean') {
agentConfig.value[key] = item.default;
} else {
agentConfig.value[key] = item.default || '';
}
});
2025-04-02 00:00:04 +08:00
try {
// 从服务器加载配置
const response = await systemConfigApi.getAgentConfig(selectedAgentId.value);
if (response.success && response.config) {
// 合并服务器配置
Object.keys(response.config).forEach(key => {
if (key in agentConfig.value) {
agentConfig.value[key] = response.config[key];
}
});
console.log(`从服务器加载 ${selectedAgentId.value} 配置成功, ${JSON.stringify(agentConfig.value)}`);
}
} catch (error) {
console.error('从服务器加载配置出错:', error);
2025-04-02 00:00:04 +08:00
}
};
const handleModelChange = (data) => {
agentConfig.value.model = `${data.provider}/${data.name}`;
}
2025-04-02 00:00:04 +08:00
// 保存配置
const saveConfig = async () => {
if (!selectedAgentId.value) {
message.error('没有选择智能体');
return;
}
try {
// 保存配置到服务器
await systemConfigApi.saveAgentConfig(selectedAgentId.value, agentConfig.value);
// 提示保存成功
message.success('配置已保存到服务器');
console.log("保存配置:", agentConfig.value);
} catch (error) {
console.error('保存配置到服务器出错:', error);
message.error('保存配置到服务器失败');
}
2025-04-02 00:00:04 +08:00
};
// 重置配置
const resetConfig = async () => {
if (!selectedAgentId.value) {
message.error('没有选择智能体');
return;
}
try {
// 保存空配置到服务器,相当于重置
await systemConfigApi.saveAgentConfig(selectedAgentId.value, {});
// 重新加载默认配置
await loadAgentConfig();
message.info('配置已重置');
} catch (error) {
console.error('重置配置出错:', error);
message.error('重置配置失败');
}
2025-04-02 00:00:04 +08:00
};
2025-04-01 22:16:07 +08:00
2025-04-02 00:00:04 +08:00
// 监听智能体选择变化
watch(
() => selectedAgentId.value,
() => {
loadAgentConfig();
}
);
2025-03-31 23:02:05 +08:00
// 选择智能体
const selectAgent = (agentId) => {
selectedAgentId.value = agentId;
// 保存选择到本地存储
localStorage.setItem('last-selected-agent', agentId);
2025-04-02 00:00:04 +08:00
// 加载该智能体的配置
loadAgentConfig();
2025-03-31 23:02:05 +08:00
};
// 初始化
onMounted(async () => {
2025-05-02 23:56:59 +08:00
// 获取默认智能体
await fetchDefaultAgent();
2025-03-31 23:02:05 +08:00
// 获取智能体列表
await fetchAgents();
// 恢复上次选择的智能体
const lastSelectedAgent = localStorage.getItem('last-selected-agent');
if (lastSelectedAgent && agents.value[lastSelectedAgent]) {
selectedAgentId.value = lastSelectedAgent;
2025-05-02 23:56:59 +08:00
} else if (defaultAgentId.value && agents.value[defaultAgentId.value]) {
// 如果有默认智能体,优先选择默认智能体
selectedAgentId.value = defaultAgentId.value;
2025-03-31 23:02:05 +08:00
} else if (Object.keys(agents.value).length > 0) {
// 默认选择第一个智能体
selectedAgentId.value = Object.keys(agents.value)[0];
}
2025-04-02 00:00:04 +08:00
// 加载配置
loadAgentConfig();
2025-03-31 23:02:05 +08:00
});
2025-04-02 13:00:25 +08:00
// 获取配置标签
const getConfigLabel = (key, value) => {
// 根据配置项属性选择合适的显示文本
if (value.description) {
return `${value.name}${key}`;
}
return key;
};
// 获取占位符
const getPlaceholder = (key, value) => {
// 返回描述作为占位符
return `(默认: ${value.default}` ;
};
2025-04-05 02:23:32 +08:00
// 跳转到独立智能体页面
const goToAgentPage = () => {
if (selectedAgentId.value) {
window.open(`/agent/${selectedAgentId.value}`, '_blank');
}
};
2025-05-02 23:56:59 +08:00
const toggleDebugMode = () => {
state.debug_mode = !state.debug_mode;
console.log("debug_mode", state.debug_mode);
2025-05-02 23:56:59 +08:00
};
const toggleSidebar = () => {
state.agentSiderbarConfigOpen = !state.agentSiderbarConfigOpen
}
2025-03-31 23:02:05 +08:00
</script>
<style lang="less" scoped>
.agent-view {
display: flex;
flex-direction: column;
2025-03-31 23:02:05 +08:00
width: 100%;
height: 100vh;
overflow: hidden;
--agent-view-header-height: 60px;
--agent-sidebar-width: 450px;
2025-03-31 23:02:05 +08:00
}
.agent-view-header {
height: var(--agent-view-header-height);
2025-03-31 23:02:05 +08:00
background-color: var(--bg-sider);
border-bottom: 1px solid var(--main-light-3);
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 16px;
.header-left,
.header-right,
.header-center {
display: flex;
flex-direction: row;
gap: 10px;
}
.header-item {
display: flex;
align-items: center;
gap: 10px;
}
}
.agent-view-body {
--gap-radius: 6px;
display: flex;
width: 100%;
flex: 1;
min-height: calc(100% - var(--agent-view-header-height));
2025-04-01 22:16:07 +08:00
overflow: hidden;
padding: var(--gap-radius);
gap: var(--gap-radius);
2025-04-01 22:16:07 +08:00
.sidebar.is-open,
.content {
border-radius: var(--gap-radius);
border: 1px solid var(--gray-300);
2025-04-01 22:16:07 +08:00
}
2025-03-31 23:02:05 +08:00
}
.sidebar {
2025-04-02 00:00:04 +08:00
width: 0;
max-width: var(--agent-sidebar-width);
2025-04-02 00:00:04 +08:00
background-color: var(--bg-sider);
overflow-y: auto;
transition: width 0.3s ease;
overflow: hidden;
&.is-open {
width: var(--agent-sidebar-width);
box-sizing: content-box;
2025-04-02 00:00:04 +08:00
}
}
2025-03-31 23:02:05 +08:00
.sidebar-title {
2025-04-06 02:53:27 +08:00
height: var(--header-height);
2025-03-31 23:02:05 +08:00
font-weight: bold;
user-select: none;
white-space: nowrap;
overflow: hidden;
padding-bottom: 1rem;
font-size: 1rem;
border-bottom: 1px solid var(--main-light-3);
2025-04-01 22:16:07 +08:00
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px;
margin: 0;
}
2025-03-31 23:02:05 +08:00
.agent-list {
display: flex;
flex-direction: column;
gap: 8px;
}
.agent-item {
display: flex;
align-items: center;
padding: 12px;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s;
&:hover {
background-color: var(--main-light-4);
}
&.active {
background-color: var(--main-light-3);
}
}
.agent-info {
2025-04-01 22:16:07 +08:00
padding: 16px;
min-width: calc(var(--agent-sidebar-width) - 16px);
2025-04-05 02:23:32 +08:00
overflow-y: auto;
max-height: calc(100vh - 60px); /* 减去标题栏的高度 */
scrollbar-width: thin;
&::-webkit-scrollbar {
width: 4px;
}
&::-webkit-scrollbar-track {
background: transparent;
}
&::-webkit-scrollbar-thumb {
background-color: var(--main-light-4);
border-radius: 4px;
}
2025-03-31 23:02:05 +08:00
}
.agent-name {
font-weight: 500;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.agent-desc {
font-size: 12px;
color: #666;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.agent-model {
width: 100%;
}
2025-03-31 23:02:05 +08:00
.content {
flex: 1;
overflow: hidden;
}
2025-04-01 22:16:07 +08:00
// 添加requirements相关样式
2025-04-02 13:00:25 +08:00
.info-section {
2025-04-01 22:16:07 +08:00
margin-top: 16px;
border-top: 1px solid var(--main-light-3);
padding-top: 12px;
h3 {
font-size: 14px;
margin-bottom: 8px;
font-weight: 500;
}
}
.requirements-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
.ant-tag {
user-select: all;
}
}
@media (max-width: 520px) {
.sidebar {
position: absolute;
z-index: 101;
width: 0;
height: 100%;
border-radius: 0 16px 16px 0;
box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.05);
&.is-open {
width: var(--agent-sidebar-width);
padding: 16px;
}
}
2025-04-02 00:00:04 +08:00
.config-sidebar {
position: absolute;
z-index: 101;
right: 0;
width: 0;
height: 100%;
border-radius: 16px 0 0 16px;
box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.05);
&.is-open {
width: 90%;
max-width: var(--config-sidebar-width);
}
}
2025-04-01 22:16:07 +08:00
}
2025-04-03 16:02:14 +08:00
.config-modal-content {
max-height: 70vh;
overflow-y: auto;
user-select: text;
div[role="alert"] {
margin-bottom: 10px;
}
2025-04-03 16:02:14 +08:00
.description {
font-size: 12px;
color: var(--gray-700);
}
.form-actions {
display: flex;
justify-content: space-between;
margin-top: 20px;
gap: 10px;
.form-actions-left,
.form-actions-right {
display: flex;
gap: 10px;
}
2025-04-03 16:02:14 +08:00
}
}
2025-04-05 02:23:32 +08:00
// 添加新按钮的样式
.agent-action-buttons {
margin-top: 16px;
display: flex;
flex-direction: column;
gap: 8px;
}
.action-button {
background-color: white;
border: 1px solid var(--main-light-3);
text-align: left;
height: auto;
padding: 8px 12px;
&:hover {
background-color: var(--main-light-4);
}
2025-05-02 23:56:59 +08:00
&.primary-action {
color: var(--main-color);
border-color: var(--main-color);
&:disabled {
color: var(--main-600);
background-color: var(--main-light-4);
cursor: not-allowed;
opacity: 0.7;
}
}
2025-04-05 02:23:32 +08:00
.anticon {
margin-right: 8px;
}
}
2025-05-02 23:56:59 +08:00
.agent-option {
display: flex;
justify-content: space-between;
align-items: center;
.default-icon {
color: #faad14;
font-size: 14px;
}
}
2025-03-31 23:02:05 +08:00
</style>
2025-03-29 17:34:02 +08:00
<style lang="less">
.toggle-sidebar {
cursor: pointer;
&.nav-btn {
height: 2.5rem;
display: flex;
justify-content: center;
align-items: center;
border-radius: 8px;
color: var(--gray-900);
cursor: pointer;
font-size: 15px;
width: auto;
padding: 0.5rem 1rem;
transition: background-color 0.3s;
overflow: hidden;
.text {
margin-left: 10px;
}
&:hover {
background-color: var(--main-light-3);
}
.nav-btn-icon {
width: 1.5rem;
height: 1.5rem;
}
}
}
</style>