CreateGroupDialog.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <script setup lang="ts">
  2. import { computed, ref, watch } from 'vue'
  3. import AppIcon from '@/components/common/AppIcon.vue'
  4. import AgentAvatar from '@/components/common/AgentAvatar.vue'
  5. import TeamAvatar from '@/components/common/TeamAvatar.vue'
  6. import { processTeamAvatarFile } from '@/utils/teamAvatar'
  7. import type { GroupProfileOption } from './types'
  8. interface CreateGroupPayload {
  9. name: string
  10. avatar?: string
  11. members: GroupProfileOption[]
  12. autoReply: boolean
  13. replyRounds: number
  14. instructions?: string
  15. hostProfile?: string
  16. orchestrationMode?: 'free' | 'host'
  17. }
  18. const props = withDefaults(defineProps<{ open: boolean; profiles: GroupProfileOption[]; avatarEnabled?: boolean; hostEnabled?: boolean; hostFlowEnabled?: boolean; roomInstructionsEnabled?: boolean; busy?: boolean }>(), {
  19. avatarEnabled: false,
  20. hostEnabled: false,
  21. hostFlowEnabled: false,
  22. roomInstructionsEnabled: false,
  23. busy: false,
  24. })
  25. const emit = defineEmits<{ close: []; create: [payload: CreateGroupPayload] }>()
  26. const name = ref('')
  27. const instructions = ref('')
  28. const selected = ref<string[]>([])
  29. const hostProfile = ref('')
  30. const autoReply = ref(true)
  31. const replyRounds = ref(3)
  32. const orchestrationMode = ref<'free' | 'host'>('free')
  33. const avatarMode = ref<'auto' | 'upload'>('auto')
  34. const avatarDataURL = ref('')
  35. const avatarError = ref('')
  36. const avatarInput = ref<HTMLInputElement>()
  37. const selectedMembers = computed(() => selected.value.flatMap(id => props.profiles.find(profile => profile.id === id) ?? []))
  38. const valid = computed(() => name.value.trim().length > 0
  39. && selected.value.length >= 1
  40. && selected.value.length <= 8
  41. && (!props.hostEnabled || selected.value.includes(hostProfile.value)))
  42. watch(() => props.open, open => {
  43. if (!open) return
  44. name.value = ''
  45. instructions.value = ''
  46. selected.value = props.profiles.slice(0, 1).map(profile => profile.id)
  47. hostProfile.value = selected.value[0] ?? ''
  48. autoReply.value = true
  49. replyRounds.value = 3
  50. orchestrationMode.value = 'free'
  51. avatarMode.value = 'auto'
  52. avatarDataURL.value = ''
  53. avatarError.value = ''
  54. }, { immediate: true })
  55. function toggle(profile: GroupProfileOption) {
  56. if (selected.value.includes(profile.id)) selected.value = selected.value.filter(item => item !== profile.id)
  57. else if (selected.value.length < 8) selected.value.push(profile.id)
  58. if (!selected.value.includes(hostProfile.value)) hostProfile.value = selected.value[0] ?? ''
  59. }
  60. async function chooseAvatar(event: Event) {
  61. const file = (event.target as HTMLInputElement).files?.[0]
  62. if (!file) return
  63. try {
  64. avatarDataURL.value = await processTeamAvatarFile(file)
  65. avatarMode.value = 'upload'
  66. avatarError.value = ''
  67. } catch (cause) {
  68. avatarError.value = cause instanceof Error ? cause.message : '处理头像失败'
  69. } finally {
  70. if (avatarInput.value) avatarInput.value.value = ''
  71. }
  72. }
  73. function create() {
  74. if (!valid.value) return
  75. const payload: CreateGroupPayload = {
  76. name: name.value.trim(),
  77. members: selectedMembers.value,
  78. autoReply: autoReply.value,
  79. replyRounds: replyRounds.value,
  80. }
  81. if (props.avatarEnabled) payload.avatar = avatarMode.value === 'upload' ? avatarDataURL.value : ''
  82. if (props.hostEnabled) payload.hostProfile = hostProfile.value
  83. if (props.hostFlowEnabled) payload.orchestrationMode = orchestrationMode.value
  84. if (props.roomInstructionsEnabled) payload.instructions = instructions.value.trim()
  85. emit('create', payload)
  86. }
  87. </script>
  88. <template>
  89. <Teleport to="body">
  90. <Transition name="dialog-fade">
  91. <div v-if="open" class="dialog-layer" role="presentation" @mousedown.self="emit('close')">
  92. <section class="create-dialog" role="dialog" aria-modal="true" aria-labelledby="create-group-title">
  93. <header><div><small>9119 团队</small><h2 id="create-group-title">新建团队</h2></div><button class="icon-button" type="button" aria-label="关闭" @click="emit('close')"><AppIcon name="close" /></button></header>
  94. <label class="field"><span>团队名称</span><input v-model="name" maxlength="80" autofocus placeholder="例如:产品评审" /></label>
  95. <section v-if="avatarEnabled" class="avatar-picker" aria-labelledby="team-avatar-label">
  96. <TeamAvatar :name="name || '团队'" :avatar="avatarMode === 'upload' ? avatarDataURL : ''" :members="selectedMembers.map(member => ({ name: member.displayName, avatar: member.avatar }))" :size="58" />
  97. <div><strong id="team-avatar-label">团队头像</strong><small>{{ avatarMode === 'auto' ? '自动组合已选 Agent 的头像' : '使用上传的图片' }}</small><p><button class="quiet-button" :class="{ active: avatarMode === 'auto' }" type="button" @click="avatarMode = 'auto'">自动生成</button><button class="quiet-button" type="button" @click="avatarInput?.click()"><AppIcon name="image" :size="14" />上传图片</button></p></div>
  98. <input ref="avatarInput" class="sr-only" type="file" accept="image/png,image/jpeg,image/webp" @change="chooseAvatar" />
  99. </section>
  100. <p v-if="avatarError" class="avatar-error" role="alert">{{ avatarError }}</p>
  101. <label v-if="roomInstructionsEnabled" class="field room-instructions"><span>说明 <small>供所有 Agent 查阅的规则和形式准则</small></span><textarea v-model="instructions" maxlength="4000" rows="4" placeholder="例如:先确认事实;结论使用中文;涉及发布必须等待确认。" /></label>
  102. <div class="agent-picker">
  103. <div class="agent-picker__heading"><span>选择 Agent</span><small>{{ selected.length }}/8</small></div>
  104. <button v-for="profile in profiles" :key="profile.id" type="button" :class="{ selected: selected.includes(profile.id) }" :disabled="!selected.includes(profile.id) && selected.length >= 8" @click="toggle(profile)">
  105. <AgentAvatar :name="profile.displayName" :avatar="profile.avatar || ''" :size="28" /><strong>{{ profile.displayName }}<small>{{ profile.profile }} · {{ profile.nodeLabel }}</small></strong><AppIcon v-if="selected.includes(profile.id)" name="check" :size="15" />
  106. </button>
  107. <p v-if="!profiles.length">当前没有可用 Agent,请先在 Hermes 配置 Profile。</p>
  108. </div>
  109. <label v-if="hostEnabled" class="host-picker">
  110. <span>主持人<small>用户没有明确 @ 时,主持人始终负责回应。</small></span>
  111. <select v-model="hostProfile" aria-label="主持人">
  112. <option v-for="profileID in selected" :key="profileID" :value="profileID">{{ profiles.find(profile => profile.id === profileID)?.displayName || profileID }}</option>
  113. </select>
  114. </label>
  115. <label v-if="hostFlowEnabled" class="host-picker">
  116. <span>协作模式<small>主持人可按依赖串行调度,也可一次 @ 多人并列执行;整批结束后统一复核。</small></span>
  117. <select v-model="orchestrationMode" aria-label="协作模式">
  118. <option value="free">自由讨论</option>
  119. <option value="host">主持流程</option>
  120. </select>
  121. </label>
  122. <div class="dialog-settings">
  123. <label><input v-model="autoReply" type="checkbox" :aria-label="hostEnabled ? '所有成员无需 @ 也回复' : '启用自动回复'" />{{ hostEnabled ? (orchestrationMode === 'host' ? '自由讨论时无需 @ 也回复' : '所有成员无需 @ 也回复') : '启用自动回复' }}</label>
  124. <label>最多轮数 <input v-model.number="replyRounds" type="number" min="1" max="12" /></label>
  125. </div>
  126. <footer><button class="quiet-button" type="button" @click="emit('close')">取消</button><button class="solid-button" type="button" :disabled="!valid || busy" @click="create">{{ busy ? '创建中…' : '创建团队' }}</button></footer>
  127. </section>
  128. </div>
  129. </Transition>
  130. </Teleport>
  131. </template>
  132. <style scoped>
  133. .dialog-layer { position: fixed; z-index: 200; inset: 0; display: grid; place-items: center; padding: 18px; background: var(--scrim); backdrop-filter: blur(5px); }
  134. .create-dialog { width: min(470px, 100%); max-height: min(680px, calc(100vh - 36px)); padding: 18px; overflow-y: auto; border: 1px solid var(--line); border-radius: 17px; background: var(--surface-raised); box-shadow: var(--shadow-float); }
  135. header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 18px; } header small { color: var(--text-muted); font-size: 9px; letter-spacing: .06em; } h2 { margin: 3px 0 0; font-size: 19px; letter-spacing: -.03em; }
  136. .field { display: flex; flex-direction: column; gap: 6px; color: var(--text-secondary); font-size: 10px; }.field span { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }.field small { color: var(--text-muted); font-size: 9px; font-weight: 400; }.field input, .field textarea { padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; outline: none; resize: vertical; background: var(--surface-soft); color: var(--text-primary); }.field input { height: 38px; }.field textarea { min-height: 86px; padding-block: 9px; line-height: 1.5; }.field input:focus, .field textarea:focus { border-color: var(--line-strong); box-shadow: 0 0 0 3px var(--focus-ring); }.room-instructions { margin-top: 13px; }
  137. .avatar-picker { display: flex; align-items: center; gap: 12px; margin-top: 13px; padding: 11px; border-radius: 11px; background: var(--surface-soft); }.avatar-picker > div { display: grid; min-width: 0; gap: 3px; }.avatar-picker strong { font-size: 10px; }.avatar-picker small { color: var(--text-muted); font-size: 9px; }.avatar-picker p { display: flex; gap: 5px; margin: 3px 0 0; }.avatar-picker .quiet-button { display: inline-flex; min-height: 27px; align-items: center; gap: 5px; padding: 0 8px; border: 1px solid transparent; border-radius: 7px; background: var(--surface-hover); color: var(--text-secondary); cursor: pointer; font-size: 9px; }.avatar-picker .quiet-button.active { border-color: color-mix(in srgb, var(--accent) 42%, var(--line)); color: var(--accent); }.avatar-error { margin: 6px 0 0; color: var(--danger); font-size: 9px; }.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  138. .agent-picker { margin-top: 17px; }.agent-picker__heading { display: flex; justify-content: space-between; margin-bottom: 7px; color: var(--text-secondary); font-size: 10px; }.agent-picker__heading small { color: var(--text-muted); }.agent-picker > button { display: flex; width: 100%; min-height: 42px; align-items: center; gap: 9px; padding: 5px 8px; border: 0; border-radius: 9px; background: transparent; color: var(--text-primary); cursor: pointer; text-align: left; }.agent-picker > button:hover, .agent-picker > button.selected { background: var(--surface-soft); }.agent-picker > button:disabled { opacity: .35; }.agent-picker > button strong { display: flex; min-width: 0; flex: 1; flex-direction: column; gap: 2px; font-size: 11px; }.agent-picker > button strong small { overflow: hidden; color: var(--text-muted); font-size: 9px; font-weight: 400; text-overflow: ellipsis; white-space: nowrap; }.agent-picker p { color: var(--text-muted); font-size: 10px; }
  139. .host-picker { display: grid; grid-template-columns: minmax(0, 1fr) 148px; align-items: center; gap: 12px; margin-top: 13px; padding: 11px; border: 1px solid var(--line); border-radius: 10px; color: var(--text-secondary); font-size: 10px; }.host-picker > span { display: flex; min-width: 0; flex-direction: column; gap: 3px; font-weight: 650; }.host-picker small { color: var(--text-muted); font-size: 9px; font-weight: 400; line-height: 1.45; }.host-picker select { width: 100%; min-width: 0; padding: 7px 8px; border: 1px solid var(--line); border-radius: 8px; outline: 0; background: var(--surface-soft); color: var(--text-primary); font-size: 10px; }.host-picker select:focus { border-color: var(--line-strong); box-shadow: 0 0 0 3px var(--focus-ring); }
  140. .dialog-settings { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 15px; padding: 11px; border-radius: 10px; background: var(--surface-soft); }.dialog-settings label { display: flex; align-items: center; gap: 6px; color: var(--text-secondary); font-size: 10px; }.dialog-settings input[type="checkbox"] { accent-color: var(--accent); }.dialog-settings input[type="number"] { width: 51px; padding: 4px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface-raised); color: var(--text-primary); text-align: center; }
  141. footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
  142. .dialog-fade-enter-active, .dialog-fade-leave-active { transition: opacity 140ms ease; }.dialog-fade-enter-active .create-dialog, .dialog-fade-leave-active .create-dialog { transition: transform 170ms var(--ease-out); }.dialog-fade-enter-from, .dialog-fade-leave-to { opacity: 0; }.dialog-fade-enter-from .create-dialog, .dialog-fade-leave-to .create-dialog { transform: translateY(8px) scale(.985); }
  143. @media (max-width: 480px) { .host-picker { grid-template-columns: 1fr; }.host-picker select { width: 100%; } }
  144. </style>