(config: ChatConfig)
| 14 | import { useChatStore, useThinkingStore, useToolsStore, useTodosStore, useApprovalStore } from "@/stores"; |
| 15 | |
| 16 | export function useChat(config: ChatConfig) { |
| 17 | // ================== |
| 18 | // Stores |
| 19 | // ================== |
| 20 | |
| 21 | const chatStore = useChatStore(); |
| 22 | const thinkingStore = useThinkingStore(); |
| 23 | const toolsStore = useToolsStore(); |
| 24 | const todosStore = useTodosStore(); |
| 25 | const approvalStore = useApprovalStore(); |
| 26 | |
| 27 | // ================== |
| 28 | // 配置 |
| 29 | // ================== |
| 30 | |
| 31 | const isDemoMode = config.demoMode ?? true; |
| 32 | const apiUrl = config.apiUrl || import.meta.env.VITE_API_URL || "http://localhost:8080"; |
| 33 | const wsUrlOverride = config.wsUrl || import.meta.env.VITE_WS_URL; |
| 34 | |
| 35 | // ================== |
| 36 | // 客户端初始化 |
| 37 | // ================== |
| 38 | |
| 39 | const { client } = useAsterClient({ |
| 40 | baseUrl: apiUrl, |
| 41 | apiKey: config.apiKey, |
| 42 | wsUrl: wsUrlOverride, |
| 43 | }); |
| 44 | |
| 45 | const { connect, getInstance, isConnected: wsConnected } = useWebSocket(); |
| 46 | |
| 47 | // 连接状态 |
| 48 | const connectionState = computed(() => (isDemoMode ? true : wsConnected.value)); |
| 49 | |
| 50 | // ================== |
| 51 | // Demo 模式相关 |
| 52 | // ================== |
| 53 | |
| 54 | let demoCursor = 0; |
| 55 | |
| 56 | const fallbackResponses = [ |
| 57 | "我已经为你生成了一个新的多 Agent 工作流,包含大纲、评价器和部署策略。", |
| 58 | "Aster 的沙箱已准备好,所有写入都被限制在 /workspace 目录,你可以放心执行指令。", |
| 59 | "我为这个会话自动挂载了上下文记忆,后续可以直接引用历史工单。", |
| 60 | "Streaming 模式已打开,等待后端返回 token,平均延迟 220ms。", |
| 61 | ]; |
| 62 | |
| 63 | const pickDemoResponse = (content: string) => { |
| 64 | const list = config.demoResponses?.length ? config.demoResponses : fallbackResponses; |
| 65 | const index = demoCursor % list.length; |
| 66 | demoCursor += 1; |
| 67 | const template = list[index] ?? ""; |
| 68 | return template.includes("{question}") ? template.split("{question}").join(content) : template; |
| 69 | }; |
| 70 | |
| 71 | // ================== |
| 72 | // 事件处理器 |
| 73 | // ================== |
nothing calls this directly
no test coverage detected