appConfig.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { ENGINE_TYPE, AppConfig, ResourceModel, RESOURCE_TYPE, APP_TYPE } from "@/lib/protocol";
  2. import {
  3. useSentioBasicStore,
  4. useSentioAsrStore,
  5. useSentioTtsStore,
  6. useSentioAgentStore,
  7. useSentioBackgroundStore,
  8. useSentioCharacterStore,
  9. useSentioChatModeStore,
  10. useSentioThemeStore,
  11. useChatRecordStore
  12. } from "@/lib/store/sentio";
  13. import { Live2dManager } from "@/lib/live2d/live2dManager";
  14. import { getSrcPath } from "@/lib/path";
  15. import { useLive2D } from "./live2d";
  16. import * as CONSTANTS from '@/lib/constants';
  17. export function useAppConfig() {
  18. const { enable: asrEnable, setEnable: setAsrEnable, engine: asrEngine, setEngine: setAsrEngine, settings: asrSettings, setSettings: setAsrSettings } = useSentioAsrStore();
  19. const { enable: ttsEnable, setEnable: setTtsEnable, engine: ttsEngine, setEngine: setTtsEngine, setSettings: setTtsSettings, settings: ttsSettings } = useSentioTtsStore();
  20. const { engine: agentEngine, setEngine: setAgentEngine, setSettings: setAgentSettings, settings: agentSettings } = useSentioAgentStore();
  21. const { background, setBackground } = useSentioBackgroundStore();
  22. const { character, setCharacter } = useSentioCharacterStore();
  23. const { sound, setSound, showThink, setShowThink, lipFactor, setLipFactor } = useSentioBasicStore();
  24. const { chatMode, setChatMode } = useSentioChatModeStore();
  25. const { theme, setTheme } = useSentioThemeStore();
  26. const { clearChatRecord } = useChatRecordStore();
  27. const { setLive2dCharacter } = useLive2D();
  28. const setCurrentCharacter = (character: ResourceModel | null) => {
  29. if (character == null) {
  30. // 默认切换到 shashayu
  31. const defaultCharacter: ResourceModel = {
  32. resource_id: "FREE_shashayu",
  33. name: "莎莎鱼",
  34. // 指向同目录下任意文件即可,内部通过 dirname 解析目录,再用 name 组装 model3.json
  35. link: getSrcPath("sentio/characters/free/shashayu/ba50d30c08e53969de577c5e4a24307.png"),
  36. type: RESOURCE_TYPE.CHARACTER,
  37. };
  38. setCharacter(defaultCharacter);
  39. setLive2dCharacter(defaultCharacter);
  40. } else {
  41. setCharacter(character);
  42. setLive2dCharacter(character);
  43. }
  44. }
  45. const setCurrentTheme = (theme: APP_TYPE) => {
  46. setTheme(theme);
  47. }
  48. const setCurrentLipFactor = (lipFactor: number) => {
  49. setLipFactor(lipFactor);
  50. Live2dManager.getInstance().setLipFactor(lipFactor);
  51. }
  52. const getAppConfig = (): AppConfig => {
  53. return {
  54. asr_enable: asrEnable,
  55. tts_enable: ttsEnable,
  56. asr: {
  57. name: asrEngine,
  58. type: ENGINE_TYPE.ASR,
  59. config: asrSettings,
  60. },
  61. tts: {
  62. name: ttsEngine,
  63. type: ENGINE_TYPE.TTS,
  64. config: ttsSettings
  65. },
  66. llm: {
  67. name: agentEngine,
  68. type: ENGINE_TYPE.LLM,
  69. config: agentSettings
  70. },
  71. agent: {
  72. name: agentEngine,
  73. type: ENGINE_TYPE.AGENT,
  74. config: agentSettings
  75. },
  76. background: background,
  77. character: character,
  78. type: theme,
  79. ext: {
  80. sound: sound,
  81. showThink: showThink,
  82. lip_factor: lipFactor,
  83. chat_mode: chatMode
  84. }
  85. }
  86. }
  87. const resetAppEngine = (engine?: string) => {
  88. setAsrEngine(engine || "default");
  89. setTtsEngine(engine || "default");
  90. setAgentEngine(engine || "default");
  91. setAsrSettings({});
  92. setTtsSettings({});
  93. setAgentSettings({});
  94. }
  95. const resetAppConfig = () => {
  96. setAsrEnable(true);
  97. setTtsEnable(true);
  98. resetAppEngine();
  99. clearChatRecord();
  100. // 设置默认背景为 usky.mp4
  101. const defaultBg: ResourceModel = {
  102. resource_id: 'DYNAMIC_usky.mp4',
  103. type: RESOURCE_TYPE.BACKGROUND,
  104. name: 'usky',
  105. link: getSrcPath(`${CONSTANTS.SENTIO_BACKGROUND_DYNAMIC_PATH}/usky.mp4`)
  106. };
  107. setBackground(defaultBg);
  108. setCharacter(null);
  109. setSound(true);
  110. setShowThink(true);
  111. setCurrentTheme(CONSTANTS.SENTIO_THENE_DEFAULT);
  112. setCurrentLipFactor(CONSTANTS.SENTIO_LIPFACTOR_DEFAULT);
  113. setChatMode(CONSTANTS.SENTIO_CHATMODE_DEFULT);
  114. }
  115. const setAppConfig = (config: AppConfig) => {
  116. if (!!config) {
  117. setAsrEnable(config.asr_enable);
  118. config.asr && setAsrEngine(config.asr.name);
  119. config.asr && setAsrSettings(config.asr.config);
  120. setTtsEnable(config.tts_enable);
  121. config.tts && setTtsEngine(config.tts.name);
  122. config.tts && setTtsSettings(config.tts.config);
  123. config.agent && setAgentEngine(config.agent.name);
  124. config.agent && setAgentSettings(config.agent.config);
  125. setBackground(config.background);
  126. setCurrentCharacter(config.character);
  127. setTheme(config.type);
  128. setSound(config.ext.sound);
  129. setShowThink(config.ext.showThink);
  130. setCurrentLipFactor(config.ext.lip_factor);
  131. setChatMode(config.ext.chat_mode);
  132. Live2dManager.getInstance().setLipFactor(config.ext.lip_factor);
  133. } else {
  134. // 新应用使用默认值
  135. resetAppConfig();
  136. setCurrentCharacter(null);
  137. }
  138. }
  139. return { getAppConfig, setAppConfig, resetAppConfig, resetAppEngine, setCurrentTheme }
  140. }