feat(agent): human-in-the-loop approval for MCP tool calls (#1173)
Add an opt-in human approval gate so Agent runs pause before executing MCP tools that operators flag as dangerous, surface an approval card in the chat UI, and only resume after the user approves (optionally with edited args) or rejects. Backend - New mcp_tool_approvals table + repo/service to mark per-tool approval required (PG migration 000042 + sqlite init). - approval.Gate coordinates RequestAndWait / Resolve with sync.Once delivery, configurable timeout, and Redis Pub/Sub fan-out so multi- replica deployments work without sticky sessions. - MCPTool.Execute integrates the gate; uses a round-level ApprovalCtx (without the per-tool 60s timeout) for the wait, and re-derives a fresh 60s exec ctx after approval so CallTool keeps a full window. - New SSE response types (tool_approval_required / _resolved) and EventBus events plumb approval state to AgentStreamDisplay. - REST: list/set per-tool approval flag, resolve pending approval. - Configurable via agent.tool_approval_timeout_seconds (yaml) or WEKNORA_AGENT_TOOL_APPROVAL_TIMEOUT env (accepts seconds or Go duration). Frontend - MCP settings: per-tool "require approval" switch on the test panel. - Chat: ToolApprovalCard renders the pause point with editable JSON args, validation feedback, mm:ss countdown that turns warning/danger near deadline, and a resolved state that retains context. - i18n strings added for zh-CN / en-US / ko-KR / ru-RU. Docs - docs/zh/mcp-approval.md covering behavior, config, API, deployment considerations (Redis cross-instance, restart limitations).
This commit is contained in:
@@ -70,6 +70,22 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- MCP tool human approval (issue #1173) -->
|
||||
<div v-else-if="event.type === 'tool_approval_required'" class="tool-event">
|
||||
<ToolApprovalCard
|
||||
:pending-id="event.pending_id"
|
||||
:service-name="event.service_name || ''"
|
||||
:mcp-tool-name="event.mcp_tool_name || ''"
|
||||
:description="event.description"
|
||||
:args-json="event.args_json"
|
||||
:timeout-seconds="event.timeout_seconds"
|
||||
:requested-at="event.requested_at"
|
||||
:resolved="event.resolved"
|
||||
:approved="event.approved"
|
||||
:resolve-reason="event.resolve_reason"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Tool Call Event (non-thinking) -->
|
||||
<div v-else-if="event.type === 'tool_call'" class="tool-event">
|
||||
<div
|
||||
@@ -179,6 +195,22 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- MCP tool human approval -->
|
||||
<div v-else-if="event.type === 'tool_approval_required'" class="tool-event">
|
||||
<ToolApprovalCard
|
||||
:pending-id="event.pending_id"
|
||||
:service-name="event.service_name || ''"
|
||||
:mcp-tool-name="event.mcp_tool_name || ''"
|
||||
:description="event.description"
|
||||
:args-json="event.args_json"
|
||||
:timeout-seconds="event.timeout_seconds"
|
||||
:requested-at="event.requested_at"
|
||||
:resolved="event.resolved"
|
||||
:approved="event.approved"
|
||||
:resolve-reason="event.resolve_reason"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Thinking Tool Call -->
|
||||
<div v-else-if="event.type === 'tool_call' && event.tool_name === 'thinking'" class="tool-event">
|
||||
<div class="action-card" :class="{ 'action-pending': event.pending || isThinkingActive(event.tool_call_id) }">
|
||||
@@ -375,6 +407,7 @@ import markedKatex from 'marked-katex-extension';
|
||||
import 'katex/dist/katex.min.css';
|
||||
import DOMPurify from 'dompurify';
|
||||
import ToolResultRenderer from './ToolResultRenderer.vue';
|
||||
import ToolApprovalCard from './ToolApprovalCard.vue';
|
||||
import picturePreview from '@/components/picture-preview.vue';
|
||||
import { getChunkByIdOnly } from '@/api/knowledge-base';
|
||||
import { getWikiPage, type WikiPage } from '@/api/wiki';
|
||||
@@ -1196,6 +1229,9 @@ const getEventKey = (event: any, index: number): string => {
|
||||
if (!event) return `event-${index}`;
|
||||
if (event.event_id) return `event-${event.event_id}`;
|
||||
if (event.tool_call_id) return `tool-${event.tool_call_id}`;
|
||||
if (event.type === 'tool_approval_required' && event.pending_id) {
|
||||
return `approval-${event.pending_id}`;
|
||||
}
|
||||
return `event-${index}-${event.type || 'unknown'}`;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,430 @@
|
||||
<template>
|
||||
<div class="approval-card" :class="cardClass">
|
||||
<!-- Status strip -->
|
||||
<div class="approval-strip">
|
||||
<span class="approval-strip-icon">
|
||||
<t-icon v-if="!resolved" name="info-circle-filled" />
|
||||
<t-icon v-else-if="approved" name="check-circle-filled" />
|
||||
<t-icon v-else name="close-circle-filled" />
|
||||
</span>
|
||||
<span class="approval-strip-text">
|
||||
<template v-if="!resolved">{{ $t('agentStream.toolApproval.banner') }}</template>
|
||||
<template v-else-if="approved">{{ $t('agentStream.toolApproval.approvedTag') }}</template>
|
||||
<template v-else>{{ $t('agentStream.toolApproval.rejectedTag') }}</template>
|
||||
</span>
|
||||
<span v-if="!resolved && secondsLeft >= 0" class="approval-strip-timer" :class="timerClass">
|
||||
<t-icon name="time" />
|
||||
{{ formatCountdown(secondsLeft) }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- Identity row -->
|
||||
<div class="approval-identity">
|
||||
<span class="ident-service">{{ serviceName }}</span>
|
||||
<t-icon name="chevron-right" class="ident-sep" />
|
||||
<span class="ident-tool">{{ mcpToolName }}</span>
|
||||
</div>
|
||||
|
||||
<div v-if="description" class="approval-desc">{{ description }}</div>
|
||||
|
||||
<!-- Args (editable while pending, read-only after resolve) -->
|
||||
<div class="approval-args">
|
||||
<div class="approval-args-label">
|
||||
<span class="args-label-text">{{ $t('agentStream.toolApproval.argsLabel') }}</span>
|
||||
<span v-if="!resolved && !isJsonValid" class="args-status args-invalid">
|
||||
<t-icon name="error-circle" /> {{ $t('agentStream.toolApproval.invalidJson') }}
|
||||
</span>
|
||||
<span v-else-if="!resolved && isJsonValid && argsDirty" class="args-status args-dirty">
|
||||
{{ $t('agentStream.toolApproval.argsModified') }}
|
||||
</span>
|
||||
</div>
|
||||
<t-textarea
|
||||
v-if="!resolved"
|
||||
v-model="argsText"
|
||||
class="approval-args-input"
|
||||
:autosize="{ minRows: 3, maxRows: 14 }"
|
||||
placeholder="{}"
|
||||
/>
|
||||
<pre v-else class="approval-args-readonly"><code>{{ argsText }}</code></pre>
|
||||
</div>
|
||||
|
||||
<!-- Footer (pending) -->
|
||||
<div v-if="!resolved" class="approval-footer">
|
||||
<span class="approval-spacer" />
|
||||
<t-button
|
||||
theme="default"
|
||||
variant="outline"
|
||||
size="small"
|
||||
:loading="submitting && pendingDecision === 'reject'"
|
||||
:disabled="submitting"
|
||||
@click="submit('reject')"
|
||||
>
|
||||
{{ $t('agentStream.toolApproval.reject') }}
|
||||
</t-button>
|
||||
<t-button
|
||||
theme="primary"
|
||||
size="small"
|
||||
:loading="submitting && pendingDecision === 'approve'"
|
||||
:disabled="submitting || !isJsonValid"
|
||||
@click="submit('approve')"
|
||||
>
|
||||
{{ $t('agentStream.toolApproval.approve') }}
|
||||
</t-button>
|
||||
</div>
|
||||
|
||||
<!-- Footer (resolved) -->
|
||||
<div v-else class="approval-resolved-footer">
|
||||
<span v-if="resolveReason" class="approval-resolved-reason">{{ resolveReason }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { resolveToolApproval } from '@/api/mcp-service'
|
||||
|
||||
const props = defineProps<{
|
||||
pendingId: string
|
||||
serviceName: string
|
||||
mcpToolName: string
|
||||
description?: string
|
||||
argsJson?: string
|
||||
timeoutSeconds?: number
|
||||
requestedAt?: number
|
||||
resolved?: boolean
|
||||
approved?: boolean
|
||||
resolveReason?: string
|
||||
}>()
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
function formatJson(raw: string): string {
|
||||
try {
|
||||
return JSON.stringify(JSON.parse(raw), null, 2)
|
||||
} catch {
|
||||
return raw
|
||||
}
|
||||
}
|
||||
|
||||
const initialArgs = formatJson(props.argsJson || '{}')
|
||||
const argsText = ref(initialArgs)
|
||||
const submitting = ref(false)
|
||||
const pendingDecision = ref<'approve' | 'reject' | null>(null)
|
||||
const now = ref(Date.now())
|
||||
let timer: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
const isJsonValid = computed(() => {
|
||||
if (!argsText.value.trim()) return true
|
||||
try {
|
||||
JSON.parse(argsText.value)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
const argsDirty = computed(() => argsText.value.trim() !== initialArgs.trim())
|
||||
|
||||
const deadline = computed(() => {
|
||||
const base = (props.requestedAt || 0) * 1000
|
||||
const add = (props.timeoutSeconds || 600) * 1000
|
||||
return base + add
|
||||
})
|
||||
|
||||
const secondsLeft = computed(() => {
|
||||
if (props.resolved) return -1
|
||||
return Math.max(0, Math.floor((deadline.value - now.value) / 1000))
|
||||
})
|
||||
|
||||
const timerClass = computed(() => {
|
||||
if (secondsLeft.value <= 30) return 'timer-critical'
|
||||
if (secondsLeft.value <= 120) return 'timer-warning'
|
||||
return ''
|
||||
})
|
||||
|
||||
const cardClass = computed(() => ({
|
||||
'is-resolved': !!props.resolved,
|
||||
'is-approved': !!props.resolved && !!props.approved,
|
||||
'is-rejected': !!props.resolved && !props.approved,
|
||||
'is-pending': !props.resolved,
|
||||
}))
|
||||
|
||||
function formatCountdown(s: number): string {
|
||||
if (s < 60) return t('agentStream.toolApproval.countdown', { seconds: s })
|
||||
const m = Math.floor(s / 60)
|
||||
const r = s % 60
|
||||
return `${m}:${r.toString().padStart(2, '0')}`
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
timer = setInterval(() => {
|
||||
now.value = Date.now()
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (timer) clearInterval(timer)
|
||||
})
|
||||
|
||||
const submit = async (decision: 'approve' | 'reject') => {
|
||||
if (props.resolved) return
|
||||
submitting.value = true
|
||||
pendingDecision.value = decision
|
||||
try {
|
||||
let modified: Record<string, unknown> | undefined
|
||||
if (decision === 'approve') {
|
||||
try {
|
||||
modified = JSON.parse(argsText.value || '{}') as Record<string, unknown>
|
||||
} catch {
|
||||
MessagePlugin.error(t('agentStream.toolApproval.invalidJson'))
|
||||
return
|
||||
}
|
||||
}
|
||||
await resolveToolApproval(props.pendingId, {
|
||||
decision,
|
||||
modified_args: decision === 'approve' ? modified : undefined,
|
||||
reason: decision === 'reject' ? t('agentStream.toolApproval.userRejected') : undefined,
|
||||
})
|
||||
MessagePlugin.success(t('agentStream.toolApproval.submitted'))
|
||||
} catch (e: any) {
|
||||
const msg = e?.response?.data?.error?.message || e?.message || t('agentStream.toolApproval.submitFailed')
|
||||
MessagePlugin.error(msg)
|
||||
} finally {
|
||||
submitting.value = false
|
||||
pendingDecision.value = null
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
@warning-rgb: 237, 122, 11;
|
||||
@success-rgb: 7, 192, 95;
|
||||
@danger-rgb: 232, 80, 91;
|
||||
|
||||
.approval-card {
|
||||
--strip-color: var(--td-warning-color);
|
||||
--strip-rgb: @warning-rgb;
|
||||
background: var(--td-bg-color-container);
|
||||
border: 1px solid var(--td-component-stroke);
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
|
||||
transition: border-color 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
position: relative;
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset: 0 auto 0 0;
|
||||
width: 3px;
|
||||
background: var(--strip-color);
|
||||
transition: background-color 0.2s ease;
|
||||
}
|
||||
|
||||
&.is-pending {
|
||||
box-shadow: 0 1px 6px rgba(@warning-rgb, 0.08);
|
||||
}
|
||||
|
||||
&.is-approved {
|
||||
--strip-color: var(--td-success-color);
|
||||
--strip-rgb: @success-rgb;
|
||||
opacity: 0.94;
|
||||
}
|
||||
|
||||
&.is-rejected {
|
||||
--strip-color: var(--td-error-color);
|
||||
--strip-rgb: @danger-rgb;
|
||||
opacity: 0.94;
|
||||
}
|
||||
}
|
||||
|
||||
.approval-strip {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 7px 12px 7px 14px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: var(--strip-color);
|
||||
background: rgba(var(--strip-rgb), 0.06);
|
||||
border-bottom: 1px solid var(--td-component-stroke);
|
||||
|
||||
.approval-strip-icon {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
.t-icon {
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
.approval-strip-text {
|
||||
flex: 1;
|
||||
color: var(--strip-color);
|
||||
}
|
||||
.approval-strip-timer {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 2px 8px;
|
||||
border-radius: 10px;
|
||||
background: rgba(0, 0, 0, 0.04);
|
||||
color: var(--td-text-color-secondary);
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-weight: 500;
|
||||
|
||||
.t-icon {
|
||||
font-size: 12px;
|
||||
}
|
||||
&.timer-warning {
|
||||
color: var(--td-warning-color);
|
||||
background: rgba(@warning-rgb, 0.1);
|
||||
}
|
||||
&.timer-critical {
|
||||
color: var(--td-error-color);
|
||||
background: rgba(@danger-rgb, 0.12);
|
||||
animation: timerPulse 1.2s ease-in-out infinite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.approval-identity {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 10px 12px 6px 14px;
|
||||
font-size: 13px;
|
||||
flex-wrap: wrap;
|
||||
|
||||
.ident-service {
|
||||
color: var(--td-text-color-secondary);
|
||||
font-weight: 500;
|
||||
}
|
||||
.ident-sep {
|
||||
color: var(--td-text-color-placeholder);
|
||||
font-size: 12px;
|
||||
}
|
||||
.ident-tool {
|
||||
color: var(--td-brand-color);
|
||||
font-weight: 600;
|
||||
font-family: var(--td-font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
|
||||
font-size: 13px;
|
||||
}
|
||||
}
|
||||
|
||||
.approval-desc {
|
||||
padding: 0 12px 4px 14px;
|
||||
font-size: 12px;
|
||||
line-height: 1.6;
|
||||
color: var(--td-text-color-secondary);
|
||||
}
|
||||
|
||||
.approval-args {
|
||||
padding: 8px 12px 0 14px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.approval-args-label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 12px;
|
||||
|
||||
.args-label-text {
|
||||
color: var(--td-text-color-placeholder);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
font-size: 11px;
|
||||
font-weight: 500;
|
||||
}
|
||||
.args-status {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 3px;
|
||||
margin-left: auto;
|
||||
font-size: 11px;
|
||||
.t-icon {
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
.args-invalid {
|
||||
color: var(--td-error-color);
|
||||
}
|
||||
.args-dirty {
|
||||
color: var(--td-warning-color);
|
||||
}
|
||||
}
|
||||
|
||||
.approval-args-input {
|
||||
:deep(.t-textarea__inner) {
|
||||
font-family: var(--td-font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
|
||||
font-size: 12px;
|
||||
line-height: 1.7;
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
border-color: var(--td-component-stroke);
|
||||
color: var(--td-text-color-primary);
|
||||
padding: 8px 10px;
|
||||
transition: border-color 0.15s ease, box-shadow 0.15s ease;
|
||||
|
||||
&:hover {
|
||||
border-color: var(--td-brand-color-hover);
|
||||
}
|
||||
&:focus,
|
||||
&:focus-visible {
|
||||
border-color: var(--td-brand-color);
|
||||
box-shadow: 0 0 0 2px rgba(@success-rgb, 0.12);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.approval-args-readonly {
|
||||
margin: 0;
|
||||
padding: 8px 10px;
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
border: 1px solid var(--td-component-stroke);
|
||||
border-radius: 4px;
|
||||
font-family: var(--td-font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
|
||||
font-size: 12px;
|
||||
line-height: 1.7;
|
||||
color: var(--td-text-color-primary);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
max-height: 180px;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.approval-footer {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 10px 12px 12px 14px;
|
||||
}
|
||||
|
||||
.approval-spacer {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.approval-resolved-footer {
|
||||
padding: 6px 12px 10px 14px;
|
||||
font-size: 12px;
|
||||
color: var(--td-text-color-secondary);
|
||||
min-height: 0;
|
||||
|
||||
.approval-resolved-reason {
|
||||
color: var(--td-text-color-secondary);
|
||||
}
|
||||
|
||||
&:empty {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes timerPulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.55; }
|
||||
}
|
||||
</style>
|
||||
@@ -825,7 +825,7 @@ const handleAgentChunk = (data) => {
|
||||
|
||||
// 确保在继续流式传输时(刷新页面场景),一旦接收到实际内容就关闭 loading
|
||||
// 这是一个保护措施,防止任何边缘情况导致 loading 残留
|
||||
if (loading.value && (data.response_type === 'thinking' || data.response_type === 'answer' || data.response_type === 'tool_call')) {
|
||||
if (loading.value && (data.response_type === 'thinking' || data.response_type === 'answer' || data.response_type === 'tool_call' || data.response_type === 'tool_approval_required')) {
|
||||
console.log('[Agent Chunk] Closing loading for continued stream');
|
||||
loading.value = false;
|
||||
}
|
||||
@@ -891,6 +891,38 @@ const handleAgentChunk = (data) => {
|
||||
}
|
||||
break;
|
||||
|
||||
case 'tool_approval_required': {
|
||||
if (!message.agentEventStream) message.agentEventStream = [];
|
||||
const d = data.data || {};
|
||||
message.agentEventStream.push({
|
||||
type: 'tool_approval_required',
|
||||
pending_id: d.pending_id,
|
||||
service_name: d.service_name,
|
||||
mcp_tool_name: d.mcp_tool_name,
|
||||
description: d.description,
|
||||
args_json: d.args_json,
|
||||
timeout_seconds: d.timeout_seconds,
|
||||
requested_at: d.requested_at,
|
||||
tool_call_id: d.tool_call_id,
|
||||
resolved: false,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'tool_approval_resolved': {
|
||||
const d = data.data || {};
|
||||
const pid = d.pending_id;
|
||||
const ev = message.agentEventStream?.find(
|
||||
(e) => e.type === 'tool_approval_required' && e.pending_id === pid
|
||||
);
|
||||
if (ev) {
|
||||
ev.resolved = true;
|
||||
ev.approved = d.approved;
|
||||
ev.resolve_reason = d.reason;
|
||||
ev.timed_out = d.timed_out;
|
||||
ev.canceled = d.canceled;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'tool_call':
|
||||
// Skip final_answer tool call from event stream - its content appears as answer events
|
||||
if (data.data && data.data.tool_name === 'final_answer') {
|
||||
|
||||
@@ -97,6 +97,7 @@
|
||||
v-model:visible="testDialogVisible"
|
||||
:result="testResult"
|
||||
:service-name="testingServiceName"
|
||||
:service-id="testingServiceId"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
@@ -129,6 +130,7 @@ const currentService = ref<MCPService | null>(null)
|
||||
const testDialogVisible = ref(false)
|
||||
const testResult = ref<MCPTestResult | null>(null)
|
||||
const testingServiceName = ref('')
|
||||
const testingServiceId = ref('')
|
||||
const testing = ref(false)
|
||||
|
||||
// Load MCP services
|
||||
@@ -184,6 +186,7 @@ const handleTest = async (service: MCPService) => {
|
||||
if (!service || !service.id) return
|
||||
|
||||
testingServiceName.value = service.name
|
||||
testingServiceId.value = service.id
|
||||
testing.value = true
|
||||
|
||||
MessagePlugin.info({
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
</div>
|
||||
<div class="tools-grid">
|
||||
<div
|
||||
v-for="(tool, index) in result.tools"
|
||||
v-for="(tool, index) in displayTools"
|
||||
:key="index"
|
||||
class="tool-card"
|
||||
:class="{ 'tool-card-expanded': expandedToolIndex === index }"
|
||||
@@ -44,10 +44,24 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<t-icon
|
||||
:name="expandedToolIndex === index ? 'chevron-up' : 'chevron-down'"
|
||||
class="expand-icon"
|
||||
/>
|
||||
<div class="tool-header-right" @click.stop>
|
||||
<t-tooltip v-if="serviceId" :content="$t('mcp.testResult.requireApprovalTip')" placement="top">
|
||||
<div class="approval-switch">
|
||||
<t-icon name="error-circle-filled" class="danger-icon" />
|
||||
<span class="approval-label">{{ $t('mcp.testResult.requireApproval') }}</span>
|
||||
<t-switch
|
||||
:value="tool.require_approval"
|
||||
:loading="approvalLoading[tool.name]"
|
||||
size="small"
|
||||
@change="(v: boolean) => onRequireApprovalChange(tool.name, v)"
|
||||
/>
|
||||
</div>
|
||||
</t-tooltip>
|
||||
<t-icon
|
||||
:name="expandedToolIndex === index ? 'chevron-up' : 'chevron-down'"
|
||||
class="expand-icon"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="expandedToolIndex === index" class="tool-card-content">
|
||||
<div v-if="tool.description" class="tool-description">
|
||||
@@ -119,14 +133,18 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import type { MCPTestResult } from '@/api/mcp-service'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import type { MCPTestResult, MCPTool } from '@/api/mcp-service'
|
||||
import { getMCPToolApprovals, setMCPToolApproval } from '@/api/mcp-service'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
interface Props {
|
||||
visible: boolean
|
||||
result: MCPTestResult | null
|
||||
serviceName: string
|
||||
/** When set, loads/saves per-tool approval flags */
|
||||
serviceId?: string
|
||||
}
|
||||
|
||||
interface Emits {
|
||||
@@ -138,6 +156,56 @@ const emit = defineEmits<Emits>()
|
||||
|
||||
const expandedToolIndex = ref<number | null>(null)
|
||||
const { t } = useI18n()
|
||||
const displayTools = ref<MCPTool[]>([])
|
||||
const approvalLoading = ref<Record<string, boolean>>({})
|
||||
|
||||
const mergeApprovals = async () => {
|
||||
const tools = props.result?.tools
|
||||
if (!tools?.length) {
|
||||
displayTools.value = []
|
||||
return
|
||||
}
|
||||
if (!props.serviceId) {
|
||||
displayTools.value = tools.map((x) => ({ ...x }))
|
||||
return
|
||||
}
|
||||
try {
|
||||
const rows = await getMCPToolApprovals(props.serviceId)
|
||||
const map = new Map(rows.map((r) => [r.tool_name, r.require_approval]))
|
||||
displayTools.value = tools.map((tool) => ({
|
||||
...tool,
|
||||
require_approval: map.get(tool.name) || false,
|
||||
}))
|
||||
} catch {
|
||||
displayTools.value = tools.map((x) => ({ ...x }))
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [props.visible, props.serviceId, props.result?.tools],
|
||||
() => {
|
||||
if (props.visible) {
|
||||
void mergeApprovals()
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
const onRequireApprovalChange = async (toolName: string, value: boolean) => {
|
||||
if (!props.serviceId) return
|
||||
approvalLoading.value = { ...approvalLoading.value, [toolName]: true }
|
||||
try {
|
||||
await setMCPToolApproval(props.serviceId, toolName, value)
|
||||
displayTools.value = displayTools.value.map((x) =>
|
||||
x.name === toolName ? { ...x, require_approval: value } : x
|
||||
)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
MessagePlugin.error(t('mcp.testResult.approvalSaveFailed'))
|
||||
} finally {
|
||||
approvalLoading.value = { ...approvalLoading.value, [toolName]: false }
|
||||
}
|
||||
}
|
||||
|
||||
const dialogVisible = computed({
|
||||
get: () => props.visible,
|
||||
@@ -310,6 +378,29 @@ const handleClose = () => {
|
||||
}
|
||||
}
|
||||
|
||||
.tool-header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.approval-switch {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 12px;
|
||||
color: var(--td-text-color-secondary);
|
||||
.danger-icon {
|
||||
color: var(--td-warning-color);
|
||||
font-size: 16px;
|
||||
}
|
||||
.approval-label {
|
||||
max-width: 88px;
|
||||
line-height: 1.2;
|
||||
}
|
||||
}
|
||||
|
||||
.expand-icon {
|
||||
color: var(--td-text-color-placeholder);
|
||||
font-size: 16px;
|
||||
|
||||
Reference in New Issue
Block a user