InteractionCard.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <script setup lang="ts">
  2. import { computed, nextTick, onMounted, reactive, ref } from 'vue'
  3. import type { InteractionFormField, InteractionFormValue, InteractionResponse } from '@agent-server/shared'
  4. import type { TimelineItem } from '../store'
  5. import { stringifyInput, toolLabel } from '../util'
  6. const props = defineProps<{
  7. item: Extract<TimelineItem, { kind: 'interaction' }>
  8. readonly?: boolean
  9. reply?: (interactionId: string, body: InteractionResponse) => Promise<unknown>
  10. }>()
  11. const root = ref<HTMLElement>()
  12. const submitting = ref(false)
  13. const error = ref<string | null>(null)
  14. const denying = ref(false)
  15. const denyReason = ref('')
  16. const active = computed(() => !props.item.resolution && !props.readonly && !!props.reply)
  17. const req = computed(() => props.item.request)
  18. const approval = computed(() => (req.value.kind === 'approval' ? req.value : null))
  19. const question = computed(() => (req.value.kind === 'question' ? req.value : null))
  20. const form = computed(() => (req.value.kind === 'form' ? req.value : null))
  21. const detail = computed(() => {
  22. const a = approval.value
  23. if (!a) return ''
  24. return a.detail?.command ?? a.detail?.url ?? a.detail?.path ?? stringifyInput(a.input)
  25. })
  26. const RESOLUTION_TEXT: Record<string, string> = {
  27. allowed: '已允许', denied: '已拒绝', answered: '已回答', timeout: '已超时(按拒绝处理)', cancelled: '已取消',
  28. }
  29. const resolutionText = computed(() => {
  30. const r = props.item.resolution
  31. if (!r) return ''
  32. if (form.value) return ({ answered: form.value.mode === 'url' ? '已完成' : '已提交', denied: '已拒绝', timeout: '已超时(未处理)', cancelled: '已关闭' } as Record<string, string>)[r] ?? r
  33. return RESOLUTION_TEXT[r]
  34. })
  35. // 表单询问(外部服务通过 MCP 询问):按字段填写,默认值预先填好
  36. const values = reactive<Record<string, InteractionFormValue | undefined>>({})
  37. for (const f of form.value?.fields ?? []) {
  38. if (f.default !== undefined) values[f.name] = Array.isArray(f.default) ? [...f.default] : f.default
  39. else if (f.type === 'choice' && f.multiple) values[f.name] = []
  40. }
  41. const urlHost = computed(() => {
  42. try {
  43. return form.value?.url ? new URL(form.value.url).host : ''
  44. } catch {
  45. return ''
  46. }
  47. })
  48. const canSubmitForm = computed(() => form.value?.fields.every(f => !f.required || isFilled(values[f.name])) ?? false)
  49. function isFilled(v: InteractionFormValue | undefined) {
  50. return v !== undefined && v !== '' && !(Array.isArray(v) && !v.length)
  51. }
  52. function toggleMulti(name: string, value: string) {
  53. const cur = (values[name] as string[] | undefined) ?? []
  54. values[name] = cur.includes(value) ? cur.filter(v => v !== value) : [...cur, value]
  55. }
  56. function submitForm() {
  57. const f = form.value
  58. if (!f) return
  59. if (f.mode === 'url') return void send({ action: 'accept' })
  60. const content: Record<string, InteractionFormValue> = {}
  61. for (const field of f.fields) {
  62. const v = values[field.name]
  63. if (!isFilled(v)) continue
  64. content[field.name] = (field.type === 'number' || field.type === 'integer') && typeof v === 'string' ? Number(v) : v!
  65. }
  66. void send({ action: 'accept', content })
  67. }
  68. function shownValue(field: InteractionFormField, v: InteractionFormValue | undefined): string {
  69. if (v === undefined) return '—'
  70. if (field.type === 'boolean') return v ? '是' : '否'
  71. if (field.type === 'choice') {
  72. const label = (x: string) => field.options.find(o => o.value === x)?.label ?? x
  73. return Array.isArray(v) ? v.map(label).join('、') || '—' : label(String(v))
  74. }
  75. return String(v)
  76. }
  77. const FORMAT_INPUT = { email: 'email', uri: 'url', date: 'date', 'date-time': 'datetime-local' } as const
  78. function inputType(f: InteractionFormField) {
  79. if (f.type === 'number' || f.type === 'integer') return 'number'
  80. return f.type === 'string' && f.format ? FORMAT_INPUT[f.format] : 'text'
  81. }
  82. // 提问:每个问题的选择与"其他"输入
  83. const answers = reactive<Record<string, string[]>>({})
  84. const others = reactive<Record<string, string>>({})
  85. function toggle(qid: string, label: string, multi: boolean) {
  86. const cur = answers[qid] ?? []
  87. answers[qid] = multi ? (cur.includes(label) ? cur.filter(l => l !== label) : [...cur, label]) : [label]
  88. if (!multi) others[qid] = ''
  89. }
  90. function picked(qid: string): string[] {
  91. if (active.value) return answers[qid] ?? []
  92. const a = props.item.answers?.[qid]
  93. return a === undefined ? [] : Array.isArray(a) ? a : [a]
  94. }
  95. const canSubmitQuestion = computed(() =>
  96. question.value?.questions.every(q => (answers[q.id]?.length ?? 0) > 0 || (others[q.id] ?? '').trim()) ?? false)
  97. async function send(body: InteractionResponse) {
  98. if (!active.value || submitting.value) return
  99. submitting.value = true
  100. error.value = null
  101. try {
  102. await props.reply!(props.item.id, body)
  103. } catch (err) {
  104. error.value = (err as Error).message
  105. } finally {
  106. submitting.value = false
  107. }
  108. }
  109. function submitQuestion() {
  110. const q = question.value
  111. if (!q || !canSubmitQuestion.value) return
  112. const out: Record<string, string | string[]> = {}
  113. for (const item of q.questions) {
  114. const picked = [...(answers[item.id] ?? [])]
  115. const other = (others[item.id] ?? '').trim()
  116. if (other) picked.push(other)
  117. out[item.id] = item.multiSelect ? picked : picked[0] ?? ''
  118. }
  119. void send({ answers: out })
  120. }
  121. async function startDeny() {
  122. denying.value = true
  123. await nextTick()
  124. root.value?.querySelector<HTMLInputElement>('.deny-input')?.focus()
  125. }
  126. function onKey(e: KeyboardEvent) {
  127. if (!approval.value || !active.value || denying.value) return
  128. if (e.key === '1') void send({ decision: 'allow_once' })
  129. else if (e.key === '2') void send({ decision: 'allow_session' })
  130. else if (e.key === '3') void startDeny()
  131. }
  132. onMounted(() => {
  133. if (active.value) root.value?.focus({ preventScroll: true })
  134. })
  135. </script>
  136. <template>
  137. <div v-if="approval && !active" class="compact" :class="item.resolution ?? 'pending'">
  138. <span class="codicon" :class="item.resolution === 'allowed' ? 'codicon-pass' : item.resolution ? 'codicon-circle-slash' : 'codicon-shield'" />
  139. <span>{{ item.resolution ? RESOLUTION_TEXT[item.resolution] : '等待审批' }}</span>
  140. <span class="compact-title">{{ toolLabel(approval.tool) }}:{{ approval.title }}</span>
  141. <span v-if="item.reason" class="compact-reason">({{ item.reason }})</span>
  142. </div>
  143. <div v-else ref="root" class="card" :class="{ done: !active }" tabindex="0" @keydown="onKey">
  144. <template v-if="approval">
  145. <div class="header">
  146. <span class="codicon codicon-shield" />
  147. {{ active ? `允许 ${toolLabel(approval.tool)} 执行这个操作吗?` : `${toolLabel(approval.tool)}:${approval.title}` }}
  148. </div>
  149. <template v-if="active">
  150. <div class="desc">{{ approval.title }}</div>
  151. <pre v-if="detail" class="detail">{{ detail }}</pre>
  152. <div v-if="approval.detail?.reason" class="reason">{{ approval.detail.reason }}</div>
  153. <div class="buttons">
  154. <button type="button" class="opt primary" :disabled="submitting" @click="send({ decision: 'allow_once' })">
  155. <span class="num">1</span>允许
  156. </button>
  157. <button v-if="approval.options.includes('allow_session')" type="button" class="opt" :disabled="submitting" @click="send({ decision: 'allow_session' })">
  158. <span class="num">2</span>允许,本会话不再询问 {{ toolLabel(approval.tool) }}
  159. </button>
  160. <div v-if="!denying" class="opt-row">
  161. <button type="button" class="opt" :disabled="submitting" @click="startDeny"><span class="num">3</span>拒绝,并告诉 Agent 原因…</button>
  162. </div>
  163. <form v-else class="deny" @submit.prevent="send({ decision: 'deny', reason: denyReason.trim() || undefined })">
  164. <input v-model="denyReason" class="deny-input" aria-label="拒绝原因" placeholder="拒绝原因(可选),回车确认" @keydown.esc="denying = false">
  165. <button type="submit" class="deny-btn" :disabled="submitting">拒绝</button>
  166. </form>
  167. </div>
  168. <div class="hint">按 1 / 2 / 3 快速选择</div>
  169. </template>
  170. </template>
  171. <template v-else-if="question">
  172. <div class="header"><span class="codicon codicon-question" />{{ active ? 'Agent 需要你的回答' : 'Agent 的提问' }}</div>
  173. <div v-for="q in question.questions" :key="q.id" class="question">
  174. <div class="q-title"><span v-if="q.header" class="chip">{{ q.header }}</span>{{ q.question }}</div>
  175. <div class="q-options">
  176. <button
  177. v-for="o in q.options" :key="o.label" type="button" class="q-opt"
  178. :class="{ picked: picked(q.id).includes(o.label) }" :disabled="!active"
  179. @click="toggle(q.id, o.label, q.multiSelect)"
  180. >
  181. <span class="codicon" :class="q.multiSelect ? (picked(q.id).includes(o.label) ? 'codicon-pass-filled' : 'codicon-circle-large') : (picked(q.id).includes(o.label) ? 'codicon-circle-filled' : 'codicon-circle-large')" />
  182. <span class="q-opt-text"><span class="q-label">{{ o.label }}</span><span v-if="o.description" class="q-desc">{{ o.description }}</span></span>
  183. </button>
  184. <div v-if="!active && picked(q.id).some(a => !q.options.some(o => o.label === a))" class="q-other">
  185. 其他:{{ picked(q.id).filter(a => !q.options.some(o => o.label === a)).join('、') }}
  186. </div>
  187. <input
  188. v-if="active" v-model="others[q.id]" class="deny-input" aria-label="其他答案" placeholder="其他(自己填写)"
  189. @input="!q.multiSelect && others[q.id] ? (answers[q.id] = []) : null"
  190. >
  191. </div>
  192. </div>
  193. <div v-if="active" class="buttons">
  194. <button type="button" class="submit" :disabled="!canSubmitQuestion || submitting" @click="submitQuestion">提交回答</button>
  195. </div>
  196. </template>
  197. <template v-else-if="form">
  198. <div class="header">
  199. <span class="codicon" :class="form.mode === 'url' ? 'codicon-link-external' : 'codicon-edit'" />
  200. {{ form.mode === 'url' ? `「${form.source}」请你打开网址完成操作` : `「${form.source}」需要你提供信息` }}
  201. </div>
  202. <div v-if="form.message" class="desc form-message">{{ form.message }}</div>
  203. <template v-if="form.mode === 'url'">
  204. <div class="url-box">
  205. <div class="url-host">{{ urlHost }}</div>
  206. <div class="url-full">{{ form.url }}</div>
  207. </div>
  208. <div v-if="active" class="buttons row">
  209. <a class="submit link-btn" :href="form.url" target="_blank" rel="noopener noreferrer"><span class="codicon codicon-link-external" />打开网址</a>
  210. <button type="button" class="opt-inline" :disabled="submitting" @click="submitForm">我已完成</button>
  211. <button type="button" class="opt-inline" :disabled="submitting" @click="send({ action: 'decline' })">拒绝</button>
  212. </div>
  213. <div v-if="active" class="hint">请确认网址的域名可信再打开;完成后点"我已完成"。</div>
  214. </template>
  215. <template v-else>
  216. <form v-if="active" class="itx-fields" @submit.prevent="submitForm">
  217. <div v-for="f in form.fields" :key="f.name" class="itx-field">
  218. <label class="itx-label" :for="`${item.id}-${f.name}`">{{ f.title }}<span v-if="f.required" class="req">*</span></label>
  219. <div v-if="f.description" class="q-desc">{{ f.description }}</div>
  220. <label v-if="f.type === 'boolean'" class="check">
  221. <input :id="`${item.id}-${f.name}`" type="checkbox" :checked="values[f.name] === true" @change="values[f.name] = ($event.target as HTMLInputElement).checked">是
  222. </label>
  223. <select v-else-if="f.type === 'choice' && !f.multiple" :id="`${item.id}-${f.name}`" v-model="values[f.name]" class="f-input">
  224. <option :value="undefined">请选择</option>
  225. <option v-for="o in f.options" :key="o.value" :value="o.value">{{ o.label }}</option>
  226. </select>
  227. <div v-else-if="f.type === 'choice'" :id="`${item.id}-${f.name}`" class="q-options">
  228. <button v-for="o in f.options" :key="o.value" type="button" class="q-opt" :class="{ picked: (values[f.name] as string[] | undefined)?.includes(o.value) }" @click="toggleMulti(f.name, o.value)">
  229. <span class="codicon" :class="(values[f.name] as string[] | undefined)?.includes(o.value) ? 'codicon-pass-filled' : 'codicon-circle-large'" />
  230. <span class="q-label">{{ o.label }}</span>
  231. </button>
  232. </div>
  233. <input
  234. v-else :id="`${item.id}-${f.name}`" v-model="values[f.name]" class="f-input" :type="inputType(f)"
  235. :min="f.type !== 'string' ? f.minimum : undefined" :max="f.type !== 'string' ? f.maximum : undefined" :step="f.type === 'integer' ? 1 : f.type === 'number' ? 'any' : undefined"
  236. :minlength="f.type === 'string' ? f.minLength : undefined" :maxlength="f.type === 'string' ? f.maxLength : undefined"
  237. >
  238. </div>
  239. <div class="buttons row">
  240. <button type="submit" class="submit" :disabled="!canSubmitForm || submitting">提交</button>
  241. <button type="button" class="opt-inline" :disabled="submitting" @click="send({ action: 'decline' })">拒绝</button>
  242. <button type="button" class="opt-inline" :disabled="submitting" @click="send({ action: 'cancel' })">暂不处理</button>
  243. </div>
  244. </form>
  245. <div v-else-if="item.content" class="itx-fields done-values">
  246. <div v-for="f in form.fields" :key="f.name" class="kv"><span class="k">{{ f.title }}</span><span>{{ shownValue(f, item.content[f.name]) }}</span></div>
  247. </div>
  248. </template>
  249. </template>
  250. <div v-if="item.resolution" class="resolved" :class="item.resolution">{{ resolutionText }}</div>
  251. <div v-if="error" class="err">{{ error }}</div>
  252. </div>
  253. </template>
  254. <style scoped>
  255. .card {
  256. position: relative;
  257. display: flex;
  258. flex-direction: column;
  259. gap: 6px;
  260. padding: 8px;
  261. margin: 2px 0 6px;
  262. background: var(--app-input-secondary-background);
  263. border: 1px solid var(--app-input-border);
  264. border-radius: var(--corner-radius-large);
  265. outline: none;
  266. }
  267. .card:focus-within { border-color: color-mix(in srgb, var(--app-input-active-border) 65%, transparent); }
  268. .card.done { opacity: 0.85; }
  269. .header { display: flex; align-items: center; gap: 6px; font-weight: 700; }
  270. .desc { color: var(--app-secondary-foreground); font-size: 12px; }
  271. .detail {
  272. margin: 0;
  273. padding: 6px 8px;
  274. max-height: 200px;
  275. overflow: auto;
  276. background: var(--app-code-background);
  277. border-radius: var(--corner-radius-medium);
  278. font-family: var(--app-monospace-font-family);
  279. font-size: var(--app-monospace-font-size);
  280. white-space: pre-wrap;
  281. word-break: break-word;
  282. }
  283. .reason { font-size: 12px; color: var(--app-warning); }
  284. .buttons { display: flex; flex-direction: column; gap: 2px; }
  285. .opt, .q-opt {
  286. display: flex;
  287. align-items: center;
  288. gap: 8px;
  289. width: 100%;
  290. padding: 5px 8px;
  291. border: none;
  292. border-radius: var(--corner-radius-small);
  293. background: transparent;
  294. text-align: left;
  295. font-weight: 500;
  296. cursor: pointer;
  297. }
  298. .opt:hover:not(:disabled), .q-opt:hover:not(:disabled) { background: var(--app-list-hover-background); }
  299. .opt.primary { background: var(--app-list-active-background); }
  300. .num {
  301. display: inline-flex;
  302. align-items: center;
  303. justify-content: center;
  304. width: 16px;
  305. height: 16px;
  306. border-radius: 3px;
  307. font-size: 11px;
  308. color: var(--app-secondary-foreground);
  309. border: 1px solid var(--app-input-border);
  310. }
  311. .deny { display: flex; gap: 6px; padding: 2px 0; }
  312. .deny-input {
  313. flex: 1;
  314. height: 26px;
  315. padding: 0 8px;
  316. border: 1px solid var(--app-input-border);
  317. border-radius: var(--corner-radius-small);
  318. background: var(--app-input-background);
  319. outline: none;
  320. }
  321. .deny-input:focus { border-color: var(--app-input-active-border); }
  322. .deny-btn, .submit {
  323. height: 26px;
  324. padding: 0 12px;
  325. border: none;
  326. border-radius: var(--corner-radius-small);
  327. cursor: pointer;
  328. background: var(--app-button-background);
  329. color: var(--app-button-foreground);
  330. }
  331. .deny-btn { background: var(--app-danger); }
  332. .submit { align-self: flex-start; }
  333. .submit:disabled, .deny-btn:disabled { opacity: 0.5; cursor: not-allowed; }
  334. .hint { font-size: 11px; color: var(--app-secondary-foreground); }
  335. .question { display: flex; flex-direction: column; gap: 4px; }
  336. .q-title { display: flex; align-items: center; gap: 6px; }
  337. .chip {
  338. padding: 0 6px;
  339. border-radius: var(--corner-radius-small);
  340. font-size: 11px;
  341. background: color-mix(in srgb, var(--app-primary-foreground) 10%, transparent);
  342. }
  343. .q-options { display: flex; flex-direction: column; gap: 2px; }
  344. .q-opt { align-items: flex-start; font-weight: 400; }
  345. .q-opt .codicon { margin-top: 2px; font-size: 14px; color: var(--app-secondary-foreground); }
  346. .q-opt.picked .codicon { color: var(--app-claude-orange); }
  347. .q-opt-text { display: flex; flex-direction: column; }
  348. .q-label { font-weight: 500; }
  349. .q-desc { font-size: 12px; color: var(--app-secondary-foreground); }
  350. .resolved { font-size: 12px; color: var(--app-secondary-foreground); }
  351. .compact { display: flex; align-items: baseline; gap: 6px; font-size: 12px; color: var(--app-secondary-foreground); min-width: 0; }
  352. .compact .codicon { font-size: 12px; align-self: center; }
  353. .compact.allowed .codicon { color: var(--app-success); }
  354. .compact.denied .codicon, .compact.timeout .codicon, .compact.cancelled .codicon { color: var(--app-danger); }
  355. .compact-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; font-family: var(--app-monospace-font-family); }
  356. .compact-reason { flex: none; }
  357. .q-other { font-size: 12px; padding: 2px 8px; }
  358. .resolved.allowed, .resolved.answered { color: var(--app-success); }
  359. .resolved.denied, .resolved.timeout { color: var(--app-danger); }
  360. .err { font-size: 12px; color: var(--app-error-foreground); }
  361. .form-message { white-space: pre-wrap; }
  362. /* 类名带前缀:后台的全局样式中有 field(固定高度)等通用类,同名会把字段压扁 */
  363. .itx-fields { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
  364. .itx-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  365. /* 表单输入框:纵向排列时不能用 flex: 1(会把高度压扁、控件重叠) */
  366. .f-input {
  367. box-sizing: border-box;
  368. width: 100%;
  369. max-width: 100%;
  370. height: 28px;
  371. padding: 0 8px;
  372. border: 1px solid var(--app-input-border);
  373. border-radius: var(--corner-radius-small);
  374. background: var(--app-input-background);
  375. color: inherit;
  376. font: inherit;
  377. outline: none;
  378. }
  379. .f-input:focus { border-color: var(--app-input-active-border); }
  380. .q-options > .deny-input { flex: none; box-sizing: border-box; width: 100%; }
  381. .itx-field .q-desc { overflow-wrap: anywhere; }
  382. .itx-label { font-weight: 500; }
  383. .req { color: var(--app-danger); margin-left: 2px; }
  384. .check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
  385. .buttons.row { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 6px; }
  386. .opt-inline {
  387. height: 26px;
  388. padding: 0 10px;
  389. border: 1px solid var(--app-input-border);
  390. border-radius: var(--corner-radius-small);
  391. background: transparent;
  392. cursor: pointer;
  393. }
  394. .opt-inline:hover:not(:disabled) { background: var(--app-list-hover-background); }
  395. .link-btn { display: inline-flex; align-items: center; gap: 4px; text-decoration: none; line-height: 26px; }
  396. .url-box {
  397. padding: 6px 8px;
  398. background: var(--app-code-background);
  399. border-radius: var(--corner-radius-medium);
  400. word-break: break-all;
  401. }
  402. .url-host { font-weight: 700; }
  403. .url-full { font-size: 12px; color: var(--app-secondary-foreground); font-family: var(--app-monospace-font-family); }
  404. .done-values { gap: 2px; font-size: 12px; }
  405. .kv { display: flex; gap: 8px; }
  406. .kv .k { color: var(--app-secondary-foreground); min-width: 72px; }
  407. </style>