WorkspaceVoiceProviders.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <script setup lang="ts">
  2. import { onMounted, ref, watch } from 'vue'
  3. import { apiRequest } from '@/api/client'
  4. const kind = ref<'tts' | 'stt'>('tts'),
  5. provider = ref('openai'),
  6. active = ref(''),
  7. rows = ref<
  8. Array<{ provider: string; settings: Record<string, unknown>; secrets: Record<string, string> }>
  9. >([])
  10. const baseUrl = ref(''),
  11. model = ref(''),
  12. voice = ref(''),
  13. apiKey = ref(''),
  14. error = ref(''),
  15. notice = ref(''),
  16. busy = ref(false)
  17. async function load() {
  18. const r = await apiRequest<{ activeProvider: string; settings: typeof rows.value }>(
  19. `/api/app/${kind.value}/settings`,
  20. )
  21. rows.value = r.settings
  22. active.value = r.activeProvider
  23. fill()
  24. }
  25. function fill() {
  26. const r = rows.value.find((r) => r.provider === provider.value)
  27. baseUrl.value = String(r?.settings.baseUrl ?? '')
  28. model.value = String(r?.settings.model ?? '')
  29. voice.value = String(r?.settings.voice ?? '')
  30. apiKey.value = ''
  31. }
  32. async function save() {
  33. busy.value = true
  34. error.value = ''
  35. try {
  36. await apiRequest(`/api/app/${kind.value}/settings/${encodeURIComponent(provider.value)}`, {
  37. method: 'PUT',
  38. body: {
  39. settings: { baseUrl: baseUrl.value, model: model.value, voice: voice.value },
  40. secrets: apiKey.value ? { apiKey: apiKey.value } : {},
  41. activeProvider: provider.value,
  42. },
  43. })
  44. await load()
  45. notice.value = '配置已保存'
  46. } catch (e) {
  47. error.value = String(e)
  48. } finally {
  49. busy.value = false
  50. }
  51. }
  52. async function probe() {
  53. busy.value = true
  54. try {
  55. const r = await apiRequest<{ ok: boolean; models: string[]; errorSummary?: string }>(
  56. '/api/app/voice/probe',
  57. {
  58. method: 'POST',
  59. body: {
  60. kind: kind.value,
  61. provider: provider.value,
  62. baseUrl: baseUrl.value,
  63. ...(apiKey.value ? { apiKey: apiKey.value } : {}),
  64. compatibility: 'openai-compatible',
  65. },
  66. },
  67. )
  68. notice.value = r.ok
  69. ? `连接成功,可用模型:${r.models.slice(0, 8).join('、')}`
  70. : (r.errorSummary ?? '连接失败')
  71. } catch (e) {
  72. error.value = String(e)
  73. } finally {
  74. busy.value = false
  75. }
  76. }
  77. async function remove() {
  78. try {
  79. await apiRequest(`/api/app/${kind.value}/settings/${encodeURIComponent(provider.value)}`, {
  80. method: 'DELETE',
  81. })
  82. await load()
  83. } catch (e) {
  84. error.value = String(e)
  85. }
  86. }
  87. watch(kind, () => void load().catch((e) => (error.value = String(e))))
  88. watch(provider, fill)
  89. onMounted(() => void load().catch((e) => (error.value = String(e))))
  90. </script>
  91. <template>
  92. <section class="voice-providers">
  93. <h3>语音服务商</h3>
  94. <p>当前启用:{{ active || '未配置' }}</p>
  95. <p v-if="error" role="alert">{{ error }}</p>
  96. <p v-if="notice" role="status">{{ notice }}</p>
  97. <form @submit.prevent="save">
  98. <label
  99. >用途<select v-model="kind">
  100. <option value="tts">语音合成 TTS</option>
  101. <option value="stt">语音识别 STT</option>
  102. </select></label
  103. ><label
  104. >服务商<select v-model="provider">
  105. <option
  106. v-for="p in kind === 'tts'
  107. ? ['edge', 'openai', 'custom', 'mimo', 'doubao']
  108. : ['openai', 'custom', 'doubao']"
  109. :key="p"
  110. >
  111. {{ p }}
  112. </option>
  113. </select></label
  114. ><label>服务地址<input v-model="baseUrl" type="url" /></label
  115. ><label>模型<input v-model="model" /></label
  116. ><label v-if="kind === 'tts'">音色<input v-model="voice" /></label
  117. ><label
  118. >API Key<input
  119. v-model="apiKey"
  120. type="password"
  121. placeholder="留空保留已保存密钥"
  122. autocomplete="new-password"
  123. /></label>
  124. <div>
  125. <button :disabled="busy">保存并启用</button
  126. ><button type="button" :disabled="busy || !baseUrl" @click="probe">测试连接</button
  127. ><button type="button" :disabled="busy" @click="remove">删除配置</button>
  128. </div>
  129. </form>
  130. </section>
  131. </template>
  132. <style scoped>
  133. .voice-providers {
  134. padding: 20px 0;
  135. border-top: 1px solid var(--line);
  136. margin-top: 22px;
  137. }
  138. form {
  139. display: grid;
  140. gap: 14px;
  141. }
  142. label {
  143. display: grid;
  144. gap: 7px;
  145. font-size: 13px;
  146. }
  147. input,
  148. select,
  149. button {
  150. padding: 10px;
  151. border: 1px solid var(--line);
  152. border-radius: 9px;
  153. color: var(--text-primary);
  154. background: var(--surface-soft);
  155. }
  156. button {
  157. margin-right: 8px;
  158. cursor: pointer;
  159. }
  160. p {
  161. font-size: 12px;
  162. color: var(--text-muted);
  163. }
  164. [role='alert'] {
  165. color: var(--danger, #b44);
  166. }
  167. </style>