ConversationsView.vue 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. <script setup lang="ts">
  2. import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
  3. import { useRoute, useRouter } from 'vue-router'
  4. import WorkspaceShell from '@/components/app/WorkspaceShell.vue'
  5. import ConversationList from '@/components/workspace/ConversationList.vue'
  6. import WorkspaceNodesPanel from '@/components/workspace/WorkspaceNodesPanel.vue'
  7. import AgentAvatar from '@/components/common/AgentAvatar.vue'
  8. import MarkdownContent from '@/components/messages/MarkdownContent.vue'
  9. import { useAuthStore } from '@/stores/auth'
  10. import { useThemeStore } from '@/stores/theme'
  11. import { apiRequest } from '@/api/client'
  12. import type { JsonValue } from '@shared/types'
  13. import type {
  14. WorkspaceAgent as Agent,
  15. WorkspaceConversation as Conversation,
  16. WorkspaceMessage as Message,
  17. WorkspaceRun as Run,
  18. WorkspaceInteraction as Interaction,
  19. WorkspaceSource as Source,
  20. WorkspaceFile,
  21. WorkspaceEvent,
  22. } from '@shared/workspace'
  23. const auth = useAuthStore(),
  24. theme = useThemeStore(),
  25. route = useRoute(),
  26. router = useRouter()
  27. const agents = ref<Agent[]>([]),
  28. conversations = ref<Conversation[]>([]),
  29. messages = ref<Message[]>([]),
  30. sources = ref<Source[]>([])
  31. const active = ref<Conversation>(),
  32. run = ref<Run | null>(null),
  33. interactions = ref<Interaction[]>([]),
  34. context = ref<Record<string, unknown> | null>(null)
  35. const text = ref(''),
  36. error = ref(''),
  37. busy = ref(false),
  38. loading = ref(false),
  39. files = ref<WorkspaceFile[]>([]),
  40. mentions = ref<string[]>([]),
  41. older = ref(true)
  42. const showNodes = ref(false)
  43. const dialog = ref<'agent' | 'group' | 'editAgent' | 'editGroup' | null>(null),
  44. editingId = ref(''),
  45. scroller = ref<HTMLElement>(),
  46. fileInput = ref<HTMLInputElement>(),
  47. dialogElement = ref<HTMLDialogElement>()
  48. const form = reactive({
  49. name: '',
  50. avatar: '',
  51. instructions: '',
  52. source: '',
  53. memberIds: [] as string[],
  54. administratorId: '',
  55. mode: 'host' as 'host' | 'free',
  56. autoReplyIds: [] as string[],
  57. maxReplyRounds: 3,
  58. })
  59. const answers = reactive<Record<string, string>>({})
  60. let cursor = 0,
  61. disposed = false,
  62. timer: ReturnType<typeof setTimeout> | undefined,
  63. generation = 0,
  64. pendingRequestId: string | undefined
  65. const selected = computed(() => (typeof route.params.id === 'string' ? route.params.id : undefined))
  66. const members = computed(() => agents.value.filter((a) => active.value?.memberIds.includes(a.id)))
  67. const isAgentDialog = computed(() => dialog.value === 'agent' || dialog.value === 'editAgent')
  68. const body = (v: unknown) => v as JsonValue
  69. async function refresh() {
  70. const [a, c] = await Promise.all([
  71. apiRequest<{ agents: Agent[] }>('/api/app/agents'),
  72. apiRequest<{ conversations: Conversation[]; cursor: number }>('/api/app/conversations'),
  73. ])
  74. if (disposed) return
  75. agents.value = a.agents
  76. conversations.value = c.conversations
  77. if (!cursor) cursor = c.cursor
  78. }
  79. async function load(id = selected.value, append = false) {
  80. if (!id) {
  81. active.value = undefined
  82. messages.value = []
  83. return
  84. }
  85. const own = ++generation
  86. if (!append) loading.value = true
  87. try {
  88. const r = await apiRequest<{
  89. conversation: Conversation
  90. messages: Message[]
  91. run: Run | null
  92. interactions: Interaction[]
  93. context: Record<string, unknown> | null
  94. }>(`/api/app/conversations/${id}`)
  95. if (disposed || own !== generation) return
  96. const atBottom =
  97. !scroller.value ||
  98. scroller.value.scrollHeight - scroller.value.scrollTop - scroller.value.clientHeight < 100
  99. active.value = r.conversation
  100. messages.value = append
  101. ? [...new Map([...messages.value, ...r.messages].map((m) => [m.id, m])).values()].sort(
  102. (a, b) => a.seq - b.seq,
  103. )
  104. : r.messages
  105. run.value = r.run
  106. interactions.value = r.interactions
  107. context.value = r.context
  108. if (!append) older.value = r.messages.length === 100
  109. if (!append || atBottom) {
  110. await nextTick()
  111. scroller.value?.scrollTo({ top: scroller.value.scrollHeight })
  112. await markRead()
  113. }
  114. } catch (e) {
  115. error.value = e instanceof Error ? e.message : '加载失败'
  116. } finally {
  117. if (own === generation) loading.value = false
  118. }
  119. }
  120. async function markRead() {
  121. const c = active.value
  122. if (!c || c.readSeq >= c.lastSeq) return
  123. await apiRequest(`/api/app/conversations/${c.id}/read`, {
  124. method: 'PUT',
  125. body: { seq: c.lastSeq },
  126. })
  127. c.readSeq = c.lastSeq
  128. }
  129. async function poll() {
  130. try {
  131. const r = await apiRequest<{ events: WorkspaceEvent[]; cursor: number }>(
  132. `/api/app/events?after=${cursor}`,
  133. )
  134. if (disposed) return
  135. cursor = r.cursor
  136. if (r.events.length) {
  137. await refresh()
  138. if (r.events.some((e) => e.conversationId === selected.value || e.type === 'agent.changed'))
  139. await load(selected.value, true)
  140. }
  141. } catch (e) {
  142. if (!disposed) error.value = e instanceof Error ? e.message : '连接中断,正在重连'
  143. } finally {
  144. if (!disposed) timer = setTimeout(() => void poll(), 1200)
  145. }
  146. }
  147. async function select(id: string) {
  148. error.value = ''
  149. files.value = []
  150. mentions.value = []
  151. text.value = ''
  152. pendingRequestId = undefined
  153. await router.push(`/conversations/${id}`)
  154. }
  155. async function openDialog(kind: NonNullable<typeof dialog.value>) {
  156. error.value = ''
  157. dialog.value = kind
  158. Object.assign(form, {
  159. name: '',
  160. avatar: '',
  161. instructions: '',
  162. source: '',
  163. memberIds: [],
  164. administratorId: '',
  165. mode: 'host',
  166. autoReplyIds: [],
  167. maxReplyRounds: 3,
  168. })
  169. try {
  170. if (kind === 'agent') {
  171. const s = await apiRequest<{ sources: Source[] }>('/api/app/agents/sources')
  172. sources.value = s.sources
  173. form.source = s.sources[0] ? JSON.stringify([s.sources[0].nodeId, s.sources[0].profile]) : ''
  174. }
  175. if (kind === 'editAgent') {
  176. const a = members.value[0]
  177. if (!a) return
  178. editingId.value = a.id
  179. Object.assign(form, a)
  180. }
  181. if (kind === 'editGroup' && active.value) {
  182. editingId.value = active.value.id
  183. Object.assign(form, active.value, {
  184. memberIds: [...active.value.memberIds],
  185. autoReplyIds: [...active.value.autoReplyIds],
  186. })
  187. }
  188. await nextTick()
  189. dialogElement.value?.showModal()
  190. } catch (e) {
  191. error.value = String(e)
  192. dialog.value = null
  193. }
  194. }
  195. function closeDialog() {
  196. dialogElement.value?.close()
  197. dialog.value = null
  198. }
  199. watch(
  200. () => form.memberIds.slice(),
  201. (ids) => {
  202. if (!ids.includes(form.administratorId)) form.administratorId = ids[0] ?? ''
  203. form.autoReplyIds = form.autoReplyIds.filter((id) => ids.includes(id))
  204. },
  205. )
  206. async function avatarChanged(event: Event) {
  207. const file = (event.target as HTMLInputElement).files?.[0]
  208. if (!file) return
  209. if (file.size > 2 * 1024 * 1024) {
  210. error.value = '头像请小于 2 MB'
  211. return
  212. }
  213. form.avatar = await new Promise<string>((resolve, reject) => {
  214. const r = new FileReader()
  215. r.onload = () => resolve(String(r.result))
  216. r.onerror = reject
  217. r.readAsDataURL(file)
  218. })
  219. }
  220. async function save() {
  221. busy.value = true
  222. error.value = ''
  223. try {
  224. const fields = { name: form.name, avatar: form.avatar, instructions: form.instructions }
  225. if (dialog.value === 'agent') {
  226. const [nodeId, profile] = JSON.parse(form.source)
  227. const result = await apiRequest<{ agent: Agent }>('/api/app/agents', {
  228. method: 'POST',
  229. body: { ...fields, nodeId, profile },
  230. })
  231. await refresh()
  232. const c = conversations.value.find(
  233. (c) => c.kind === 'direct' && c.memberIds[0] === result.agent.id,
  234. )
  235. if (c) await select(c.id)
  236. } else if (dialog.value === 'editAgent')
  237. await apiRequest(`/api/app/agents/${editingId.value}`, { method: 'PATCH', body: fields })
  238. else {
  239. const payload = {
  240. ...fields,
  241. administratorId: form.administratorId,
  242. mode: form.mode,
  243. autoReplyIds: form.autoReplyIds,
  244. maxReplyRounds: form.maxReplyRounds,
  245. ...(dialog.value === 'group' ? { memberIds: form.memberIds } : {}),
  246. }
  247. const result = await apiRequest<{ conversation: Conversation }>(
  248. dialog.value === 'group'
  249. ? '/api/app/conversations'
  250. : `/api/app/conversations/${editingId.value}`,
  251. { method: dialog.value === 'group' ? 'POST' : 'PATCH', body: body(payload) },
  252. )
  253. await select(result.conversation.id)
  254. }
  255. closeDialog()
  256. await refresh()
  257. await load()
  258. } catch (e) {
  259. error.value = e instanceof Error ? e.message : '保存失败'
  260. } finally {
  261. busy.value = false
  262. }
  263. }
  264. async function upload(event: Event) {
  265. const picked = (event.target as HTMLInputElement).files
  266. if (!picked?.length) return
  267. busy.value = true
  268. error.value = ''
  269. try {
  270. const data = new FormData()
  271. for (const file of picked) data.append('files', file)
  272. const r = await apiRequest<{ files: WorkspaceFile[] }>('/api/app/uploads', {
  273. method: 'POST',
  274. body: data,
  275. })
  276. files.value = [...files.value, ...r.files].slice(0, 8)
  277. } catch (e) {
  278. error.value = String(e)
  279. } finally {
  280. busy.value = false
  281. ;(event.target as HTMLInputElement).value = ''
  282. }
  283. }
  284. async function send() {
  285. const c = active.value
  286. if (!c || busy.value || c.activeRunId || (!text.value.trim() && !files.value.length)) return
  287. busy.value = true
  288. error.value = ''
  289. pendingRequestId ??= crypto.randomUUID()
  290. try {
  291. await apiRequest(`/api/app/conversations/${c.id}/messages`, {
  292. method: 'POST',
  293. body: {
  294. requestId: pendingRequestId,
  295. content: text.value,
  296. mentionIds: mentions.value,
  297. fileIds: files.value.map((f) => f.id),
  298. },
  299. })
  300. text.value = ''
  301. files.value = []
  302. mentions.value = []
  303. pendingRequestId = undefined
  304. await load(c.id, true)
  305. await refresh()
  306. } catch (e) {
  307. error.value = e instanceof Error ? e.message : '发送失败'
  308. } finally {
  309. busy.value = false
  310. }
  311. }
  312. watch([text, () => files.value.map((f) => f.id).join(), () => mentions.value.join()], () => {
  313. if (!busy.value) pendingRequestId = undefined
  314. })
  315. async function action(operation: 'pin' | 'archive') {
  316. const c = active.value
  317. if (!c) return
  318. try {
  319. if (operation === 'pin')
  320. await apiRequest(`/api/app/conversations/${c.id}`, {
  321. method: 'PATCH',
  322. body: { pinned: !c.pinned },
  323. })
  324. else
  325. await apiRequest(
  326. c.kind === 'direct'
  327. ? `/api/app/agents/${c.memberIds[0]}`
  328. : `/api/app/conversations/${c.id}`,
  329. { method: 'PATCH', body: { archived: !c.archived } },
  330. )
  331. await refresh()
  332. await load()
  333. } catch (e) {
  334. error.value = String(e)
  335. }
  336. }
  337. async function control(action: 'stop' | 'reconcile') {
  338. if (!run.value) return
  339. try {
  340. await apiRequest(`/api/app/runs/${run.value.id}/${action}`, { method: 'POST', body: {} })
  341. await load(undefined, true)
  342. } catch (e) {
  343. error.value = String(e)
  344. }
  345. }
  346. async function respond(i: Interaction, answer: string) {
  347. try {
  348. await apiRequest(`/api/app/interactions/${i.id}/respond`, { method: 'POST', body: { answer } })
  349. await load(undefined, true)
  350. } catch (e) {
  351. error.value = String(e)
  352. }
  353. }
  354. async function loadOlder() {
  355. const c = active.value
  356. if (!c) return
  357. try {
  358. const r = await apiRequest<{ messages: Message[] }>(
  359. `/api/app/conversations/${c.id}/messages?before=${messages.value[0]?.seq ?? 0}`,
  360. )
  361. messages.value = [...r.messages, ...messages.value]
  362. older.value = r.messages.length === 100
  363. } catch (e) {
  364. error.value = String(e)
  365. }
  366. }
  367. function rendered(m: Message) {
  368. return m.content.replace(
  369. /(!?\[[^\]]*\])\(<?([^)>]+)>?\)/g,
  370. (whole, label: string, path: string) => {
  371. const file =
  372. m.attachments.find((f) => f.sourcePath === path) ||
  373. m.attachments.find((f) => path.split('/').at(-1) === f.name)
  374. return file
  375. ? `${label}(/api/app/files/${file.id}/${label.startsWith('!') ? 'preview' : 'download'})`
  376. : whole
  377. },
  378. )
  379. }
  380. watch(selected, () => void load())
  381. onMounted(async () => {
  382. try {
  383. await apiRequest('/api/app/capabilities')
  384. await refresh()
  385. await load()
  386. if (!disposed) void poll()
  387. } catch (e) {
  388. error.value = String(e)
  389. }
  390. })
  391. onBeforeUnmount(() => {
  392. disposed = true
  393. generation++
  394. if (timer) clearTimeout(timer)
  395. })
  396. </script>
  397. <template>
  398. <WorkspaceShell
  399. :user-name="auth.user?.username"
  400. :profiles="auth.profiles"
  401. :active-profile="auth.activeProfile"
  402. :is-admin="auth.user?.role === 'admin'"
  403. :upstream-ready="auth.upstreamReady"
  404. :upstream-error="auth.upstreamError"
  405. :theme="theme.resolvedTheme"
  406. :theme-preference="theme.theme"
  407. sidebar-title="聊天"
  408. sidebar-context-title="聊天列表"
  409. @logout="auth.logout"
  410. @select-profile="auth.selectProfile"
  411. @toggle-theme="theme.toggle"
  412. @set-theme="theme.setTheme"
  413. >
  414. <template #sidebar-action
  415. ><div class="new-actions">
  416. <button @click="openDialog('agent')">+ 创建 Agent</button
  417. ><button @click="openDialog('group')">+ 创建群聊</button
  418. ><button @click="showNodes = true">节点</button>
  419. </div></template
  420. >
  421. <template #sidebar
  422. ><ConversationList :conversations="conversations" :selected="selected" @select="select"
  423. /></template>
  424. <template #mobile-sidebar
  425. ><ConversationList :conversations="conversations" :selected="selected" @select="select"
  426. /></template>
  427. <section class="workspace-chat" aria-label="聊天">
  428. <header v-if="active" class="chat-header">
  429. <AgentAvatar :name="active.name" :avatar="active.avatar" :size="38" />
  430. <div>
  431. <h1>{{ active.name }}</h1>
  432. <small>{{
  433. active.kind === 'group' ? `${members.length} 位成员 · 成员已固定` : 'Agent 单聊'
  434. }}</small>
  435. </div>
  436. <span class="spacer" /><button @click="action('pin')">
  437. {{ active.pinned ? '取消置顶' : '置顶' }}</button
  438. ><button @click="openDialog(active.kind === 'direct' ? 'editAgent' : 'editGroup')">
  439. 设置</button
  440. ><button @click="action('archive')">{{ active.archived ? '恢复' : '归档' }}</button>
  441. </header>
  442. <p v-if="error" class="error" role="alert">
  443. {{ error }} <button @click="error = ''" aria-label="关闭错误">×</button>
  444. </p>
  445. <div
  446. v-if="active"
  447. ref="scroller"
  448. class="messages"
  449. @scroll="
  450. scroller &&
  451. scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < 40 &&
  452. markRead()
  453. "
  454. >
  455. <button v-if="older" class="older" @click="loadOlder">加载更早消息</button>
  456. <p v-if="loading && !messages.length">正在加载…</p>
  457. <article v-for="m in messages" :key="m.id" class="message" :class="m.role">
  458. <small
  459. >{{ m.role === 'user' ? '我' : m.agentName || '系统' }}
  460. <time>{{
  461. new Date(m.createdAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
  462. }}</time></small
  463. >
  464. <details v-if="m.reasoning">
  465. <summary>思考过程</summary>
  466. <p>{{ m.reasoning }}</p>
  467. </details>
  468. <MarkdownContent :content="rendered(m)" :streaming="m.status === 'streaming'" />
  469. <details v-for="(tool, i) in m.tools" :key="i">
  470. <summary>{{ tool.name || tool.tool_name || '工具' }} · {{ tool.status }}</summary>
  471. <pre>{{ JSON.stringify(tool, null, 2) }}</pre>
  472. </details>
  473. <a
  474. v-for="file in m.attachments"
  475. :key="file.id"
  476. class="file"
  477. :href="`/api/app/files/${file.id}/download`"
  478. target="_blank"
  479. rel="noreferrer"
  480. ><img
  481. v-if="file.mimeType.startsWith('image/')"
  482. :src="`/api/app/files/${file.id}/preview`"
  483. :alt="file.name"
  484. />{{ file.name }}</a
  485. ><small v-if="m.status !== 'complete'" class="status">{{
  486. {
  487. queued: '等待中',
  488. streaming: '正在回复…',
  489. failed: '执行失败',
  490. interrupted: '已停止',
  491. uncertain: '执行状态待确认',
  492. }[m.status]
  493. }}</small>
  494. </article>
  495. <p v-if="!loading && !messages.length" class="chat-empty">
  496. {{ active.kind === 'group' ? '向团队描述你希望完成的事。' : '发一条消息,开始协作。' }}
  497. </p>
  498. </div>
  499. <div v-else class="welcome">
  500. <AgentAvatar name="夭夭" :size="72" />
  501. <h1>从一个角色开始</h1>
  502. <p>创建专属 Agent,为它设置职责和规则。<br />让不同角色在同一个群聊中协作。</p>
  503. <button @click="openDialog('agent')">创建 Agent</button>
  504. </div>
  505. <div v-if="active" class="composer-area">
  506. <div v-for="i in interactions" :key="i.id" class="interaction">
  507. <strong>{{ i.kind === 'approval' ? '需要审批' : '需要补充信息' }}</strong>
  508. <p>{{ i.message }}</p>
  509. <template v-if="i.kind === 'approval'"
  510. ><button @click="respond(i, 'once')">允许本次</button
  511. ><button @click="respond(i, 'deny')">拒绝</button></template
  512. ><template v-else
  513. ><input v-model="answers[i.id]" aria-label="补充信息" /><button
  514. @click="respond(i, answers[i.id] || '')"
  515. >
  516. 提交
  517. </button></template
  518. >
  519. </div>
  520. <div v-if="run" class="run-status">
  521. {{
  522. run.status === 'uncertain'
  523. ? '执行状态待确认'
  524. : run.status === 'waiting'
  525. ? '等待你的确认'
  526. : '正在协作'
  527. }}<span>{{ run.error }}</span
  528. ><button v-if="run.status === 'uncertain'" @click="control('reconcile')">核对状态</button
  529. ><button @click="control('stop')">停止</button>
  530. </div>
  531. <div v-if="active.kind === 'group'" class="mentions">
  532. <label v-for="a in members" :key="a.id"
  533. ><input v-model="mentions" type="checkbox" :value="a.id" />@{{ a.name }}</label
  534. >
  535. </div>
  536. <div v-if="files.length" class="uploads">
  537. <button
  538. v-for="f in files"
  539. :key="f.id"
  540. @click="files = files.filter((x) => x.id !== f.id)"
  541. >
  542. {{ f.name }} ×
  543. </button>
  544. </div>
  545. <form class="composer" @submit.prevent="send">
  546. <textarea
  547. v-model="text"
  548. :disabled="active.archived"
  549. aria-label="消息"
  550. :placeholder="active.archived ? '聊天已归档' : '输入消息…'"
  551. @keydown.enter.exact.prevent="send"
  552. />
  553. <div>
  554. <input ref="fileInput" hidden type="file" multiple @change="upload" /><button
  555. type="button"
  556. :disabled="busy || active.archived"
  557. @click="fileInput?.click()"
  558. >
  559. 附件</button
  560. ><span class="spacer" /><small v-if="context"
  561. >上下文 {{ context.context_percent ?? context.percent ?? '—' }}%</small
  562. ><button
  563. type="submit"
  564. :disabled="
  565. busy || !!active.activeRunId || active.archived || (!text.trim() && !files.length)
  566. "
  567. >
  568. {{ busy ? '处理中…' : '发送' }}
  569. </button>
  570. </div>
  571. </form>
  572. </div>
  573. </section>
  574. <div
  575. v-if="showNodes"
  576. class="nodes-overlay"
  577. role="dialog"
  578. aria-modal="true"
  579. aria-label="节点管理"
  580. >
  581. <div><button @click="showNodes = false">关闭</button><WorkspaceNodesPanel /></div>
  582. </div>
  583. <dialog v-if="dialog" ref="dialogElement" class="editor" @cancel.prevent="closeDialog">
  584. <form @submit.prevent="save">
  585. <header>
  586. <h2>
  587. {{ dialog === 'agent' ? '创建 Agent' : dialog === 'group' ? '创建群聊' : '编辑资料' }}
  588. </h2>
  589. <button type="button" aria-label="关闭" @click="closeDialog">×</button>
  590. </header>
  591. <p v-if="error" class="error" role="alert">{{ error }}</p>
  592. <label>名称<input v-model="form.name" required maxlength="100" /></label
  593. ><label
  594. >头像<input
  595. type="file"
  596. accept="image/png,image/jpeg,image/webp"
  597. @change="avatarChanged" /></label
  598. ><AgentAvatar v-if="form.avatar" :name="form.name" :avatar="form.avatar" :size="48" /><label
  599. v-if="dialog === 'agent'"
  600. >基础 Agent<select v-model="form.source" required>
  601. <option
  602. v-for="s in sources"
  603. :key="`${s.nodeId}:${s.profile}`"
  604. :value="JSON.stringify([s.nodeId, s.profile])"
  605. >
  606. {{ s.name }} · {{ s.nodeId === 'local' ? '当前服务' : s.nodeId }}
  607. </option></select
  608. ><small v-if="!sources.length"
  609. >没有可用的基础 Agent,请检查 Web 的 Hermes 连接。</small
  610. ></label
  611. ><label
  612. >{{ isAgentDialog ? '角色提示词与规则' : '群规则'
  613. }}<textarea
  614. v-model="form.instructions"
  615. rows="6"
  616. maxlength="24000"
  617. :placeholder="
  618. isAgentDialog
  619. ? '例如:你是代码审查员。重点检查正确性、边界情况和测试证据。'
  620. : '所有成员共同遵守的协作规则'
  621. "
  622. />
  623. </label>
  624. <fieldset v-if="!isAgentDialog">
  625. <legend>{{ dialog === 'group' ? '选择成员(创建后不可变更)' : '固定成员' }}</legend>
  626. <label
  627. v-for="a in agents.filter((a) =>
  628. dialog === 'group' ? !a.archived : form.memberIds.includes(a.id),
  629. )"
  630. :key="a.id"
  631. ><input
  632. v-model="form.memberIds"
  633. type="checkbox"
  634. :value="a.id"
  635. :disabled="dialog === 'editGroup'"
  636. />{{ a.name }}</label
  637. ><small v-if="agents.filter((a) => !a.archived).length < 2"
  638. >至少需要两个 Agent 才能创建群聊。</small
  639. >
  640. </fieldset>
  641. <template v-if="!isAgentDialog"
  642. ><label
  643. >管理员<select v-model="form.administratorId" required>
  644. <option
  645. v-for="a in agents.filter((a) => form.memberIds.includes(a.id))"
  646. :key="a.id"
  647. :value="a.id"
  648. >
  649. {{ a.name }}
  650. </option>
  651. </select></label
  652. ><label
  653. >协作方式<select v-model="form.mode">
  654. <option value="host">管理员协调</option>
  655. <option value="free">自由协作</option>
  656. </select></label
  657. >
  658. <fieldset v-if="form.mode === 'free'">
  659. <legend>未 @ 时自动回复</legend>
  660. <label v-for="a in agents.filter((a) => form.memberIds.includes(a.id))" :key="a.id"
  661. ><input v-model="form.autoReplyIds" type="checkbox" :value="a.id" />{{
  662. a.name
  663. }}</label
  664. >
  665. </fieldset>
  666. <label
  667. >自动协作轮数<input
  668. v-model.number="form.maxReplyRounds"
  669. type="number"
  670. min="1"
  671. max="100"
  672. required /></label
  673. ></template>
  674. <footer>
  675. <button type="button" @click="closeDialog">取消</button
  676. ><button :disabled="busy || (!isAgentDialog && form.memberIds.length < 2)">
  677. {{ busy ? '保存中…' : '保存' }}
  678. </button>
  679. </footer>
  680. </form>
  681. </dialog>
  682. </WorkspaceShell>
  683. </template>
  684. <style scoped>
  685. .nodes-overlay {
  686. position: fixed;
  687. inset: 0;
  688. z-index: 1000;
  689. background: #0006;
  690. display: grid;
  691. place-items: center;
  692. }
  693. .nodes-overlay > div {
  694. padding: 20px;
  695. border-radius: 18px;
  696. max-height: 85vh;
  697. overflow: auto;
  698. width: min(520px, 90vw);
  699. background: var(--surface);
  700. }
  701. .new-actions {
  702. display: flex;
  703. gap: 4px;
  704. flex-wrap: wrap;
  705. }
  706. .workspace-chat {
  707. height: 100%;
  708. min-height: 0;
  709. display: flex;
  710. flex-direction: column;
  711. color: var(--text-primary);
  712. background: var(--surface);
  713. }
  714. button {
  715. cursor: pointer;
  716. color: inherit;
  717. }
  718. .chat-header {
  719. display: flex;
  720. align-items: center;
  721. gap: 12px;
  722. padding: 17px 24px;
  723. border-bottom: 1px solid var(--line);
  724. }
  725. h1 {
  726. font-size: 18px;
  727. margin: 0 0 4px;
  728. }
  729. small {
  730. color: var(--text-muted);
  731. }
  732. .spacer {
  733. flex: 1;
  734. }
  735. .chat-header button,
  736. .composer button,
  737. .run-status button,
  738. .interaction button,
  739. .editor button,
  740. .welcome button {
  741. padding: 8px 13px;
  742. border: 1px solid var(--line);
  743. border-radius: 9px;
  744. background: var(--surface-soft);
  745. }
  746. button:disabled {
  747. opacity: 0.45;
  748. cursor: default;
  749. }
  750. .messages {
  751. flex: 1;
  752. min-height: 0;
  753. overflow: auto;
  754. padding: 24px max(24px, calc((100% - 780px) / 2));
  755. scrollbar-gutter: stable;
  756. }
  757. .message {
  758. margin: 0 0 28px;
  759. overflow-wrap: anywhere;
  760. }
  761. .message > small {
  762. display: block;
  763. margin-bottom: 9px;
  764. font-size: 11px;
  765. }
  766. .message.user {
  767. margin-left: 15%;
  768. padding: 14px 18px;
  769. background: var(--surface-soft);
  770. border-radius: 17px;
  771. }
  772. .message time {
  773. margin-left: 8px;
  774. opacity: 0.7;
  775. }
  776. .message pre {
  777. white-space: pre-wrap;
  778. font-size: 11px;
  779. max-height: 300px;
  780. overflow: auto;
  781. }
  782. .message details {
  783. margin: 8px 0;
  784. font-size: 12px;
  785. color: var(--text-secondary);
  786. }
  787. .message .file {
  788. display: inline-flex;
  789. vertical-align: top;
  790. flex-direction: column;
  791. max-width: 280px;
  792. margin: 8px;
  793. padding: 10px;
  794. border: 1px solid var(--line);
  795. border-radius: 12px;
  796. color: inherit;
  797. font-size: 12px;
  798. }
  799. .file img {
  800. max-height: 240px;
  801. max-width: 100%;
  802. object-fit: contain;
  803. }
  804. .composer-area {
  805. width: min(820px, 100%);
  806. box-sizing: border-box;
  807. align-self: center;
  808. padding: 12px 24px 24px;
  809. }
  810. .composer {
  811. border: 1px solid var(--line);
  812. border-radius: 16px;
  813. padding: 12px;
  814. background: var(--surface);
  815. }
  816. .composer textarea {
  817. resize: vertical;
  818. width: 100%;
  819. box-sizing: border-box;
  820. min-height: 75px;
  821. max-height: 220px;
  822. border: 0;
  823. background: transparent;
  824. color: inherit;
  825. font: inherit;
  826. line-height: 1.6;
  827. outline: none;
  828. }
  829. .composer > div {
  830. display: flex;
  831. align-items: center;
  832. gap: 10px;
  833. }
  834. .mentions,
  835. .uploads {
  836. display: flex;
  837. flex-wrap: wrap;
  838. gap: 10px;
  839. margin-bottom: 10px;
  840. font-size: 12px;
  841. }
  842. .mentions label {
  843. display: flex;
  844. align-items: center;
  845. gap: 3px;
  846. }
  847. .uploads button {
  848. border: 1px solid var(--line);
  849. border-radius: 8px;
  850. padding: 7px;
  851. background: var(--surface-soft);
  852. }
  853. .run-status {
  854. display: flex;
  855. gap: 10px;
  856. align-items: center;
  857. flex-wrap: wrap;
  858. font-size: 12px;
  859. margin-bottom: 10px;
  860. }
  861. .run-status span {
  862. color: var(--text-muted);
  863. }
  864. .error {
  865. padding: 12px 20px;
  866. margin: 0;
  867. background: color-mix(in srgb, var(--danger, #c64c4c) 10%, transparent);
  868. color: var(--danger, #c64c4c);
  869. font-size: 13px;
  870. }
  871. .error button {
  872. float: right;
  873. border: 0;
  874. background: none;
  875. }
  876. .welcome {
  877. flex: 1;
  878. display: flex;
  879. flex-direction: column;
  880. justify-content: center;
  881. align-items: center;
  882. text-align: center;
  883. padding: 30px;
  884. gap: 18px;
  885. }
  886. .welcome h1 {
  887. font-size: 26px;
  888. }
  889. .welcome p {
  890. line-height: 1.9;
  891. color: var(--text-muted);
  892. }
  893. .chat-empty {
  894. padding: 60px 20px;
  895. text-align: center;
  896. color: var(--text-muted);
  897. }
  898. .interaction {
  899. border: 1px solid var(--line);
  900. padding: 16px;
  901. border-radius: 12px;
  902. margin-bottom: 12px;
  903. font-size: 13px;
  904. }
  905. .interaction button {
  906. margin-right: 8px;
  907. }
  908. .older {
  909. display: block;
  910. margin: 0 auto 25px;
  911. border: 0;
  912. background: transparent;
  913. color: var(--text-muted);
  914. }
  915. .editor {
  916. border: 1px solid var(--line);
  917. border-radius: 18px;
  918. width: min(520px, calc(100vw - 48px));
  919. max-height: 85vh;
  920. overflow: auto;
  921. background: var(--surface);
  922. color: var(--text-primary);
  923. padding: 25px;
  924. }
  925. .editor::backdrop {
  926. background: #0006;
  927. }
  928. .editor header,
  929. .editor footer {
  930. display: flex;
  931. align-items: center;
  932. justify-content: space-between;
  933. gap: 12px;
  934. }
  935. .editor h2 {
  936. font-size: 20px;
  937. }
  938. .editor label {
  939. display: flex;
  940. flex-direction: column;
  941. gap: 7px;
  942. margin: 15px 0;
  943. font-size: 13px;
  944. }
  945. .editor input:not([type='checkbox']),
  946. .editor textarea,
  947. .editor select,
  948. .interaction input {
  949. padding: 10px;
  950. border: 1px solid var(--line);
  951. border-radius: 8px;
  952. background: var(--surface-soft);
  953. color: inherit;
  954. font: inherit;
  955. }
  956. .editor fieldset {
  957. border: 1px solid var(--line);
  958. border-radius: 10px;
  959. }
  960. .editor fieldset label {
  961. flex-direction: row;
  962. align-items: center;
  963. }
  964. .editor footer {
  965. justify-content: flex-end;
  966. margin-top: 25px;
  967. }
  968. .status {
  969. margin-top: 8px;
  970. }
  971. @media (max-width: 700px) {
  972. .chat-header {
  973. padding: 12px;
  974. gap: 8px;
  975. }
  976. .chat-header button {
  977. padding: 6px;
  978. font-size: 11px;
  979. }
  980. .messages {
  981. padding: 18px 15px;
  982. }
  983. .composer-area {
  984. padding: 10px 12px 16px;
  985. }
  986. .message.user {
  987. margin-left: 8%;
  988. }
  989. }
  990. </style>