(config: ChatConfig)
| 16 | import { useWorkflowStore } from "@/stores/workflow"; |
| 17 | |
| 18 | export function useChat(config: ChatConfig) { |
| 19 | // 初始化 Pinia Stores |
| 20 | const chatStore = useChatStore(); |
| 21 | const thinkingStore = useThinkingStore(); |
| 22 | const toolsStore = useToolsStore(); |
| 23 | const todosStore = useTodosStore(); |
| 24 | const approvalStore = useApprovalStore(); |
| 25 | const workflowStore = useWorkflowStore(); |
| 26 | |
| 27 | // 保留部分本地状态 (不适合放在 store 中的) |
| 28 | const currentInput = ref(""); |
| 29 | const demoConnection = ref(true); |
| 30 | const isDemoMode = config.demoMode ?? true; |
| 31 | const pendingAskUser = ref<{ requestId: string; questions: Question[] } | null>(null); |
| 32 | const agent = ref<Agent>({ |
| 33 | id: config.agentId || "demo-agent", |
| 34 | name: config.agentProfile?.name || "Aster Copilot", |
| 35 | description: config.agentProfile?.description || "多模态执行、自动规划、符合企业安全的 Agent", |
| 36 | avatar: config.agentProfile?.avatar, |
| 37 | status: "idle", |
| 38 | metadata: { |
| 39 | model: "aster:builder", |
| 40 | }, |
| 41 | }); |
| 42 | const demoCursor = ref(0); |
| 43 | |
| 44 | const apiUrl = config.apiUrl || import.meta.env.VITE_API_URL || "http://localhost:8080"; |
| 45 | const wsUrlOverride = config.wsUrl || import.meta.env.VITE_WS_URL; |
| 46 | |
| 47 | const { client } = useAsterClient({ |
| 48 | baseUrl: apiUrl, |
| 49 | apiKey: config.apiKey, |
| 50 | wsUrl: wsUrlOverride, |
| 51 | }); |
| 52 | |
| 53 | const { connect, getInstance, isConnected: wsConnected } = useWebSocket(); |
| 54 | // connectionState 用于组件中判断连接状态 |
| 55 | const connectionState = computed(() => (isDemoMode ? demoConnection.value : wsConnected.value)); |
| 56 | |
| 57 | // 初始化 WebSocket 连接 |
| 58 | onMounted(async () => { |
| 59 | if (!isDemoMode) { |
| 60 | const wsUrl = wsUrlOverride || apiUrl.replace(/^http/, "ws") + "/v1/ws"; |
| 61 | console.log("🚀 Initializing WebSocket connection to:", wsUrl); |
| 62 | try { |
| 63 | await connect(wsUrl); |
| 64 | console.log("✅ WebSocket initialized in useChat"); |
| 65 | } catch (error) { |
| 66 | console.error("❌ Failed to initialize WebSocket:", error); |
| 67 | } |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | const fallbackResponses = [ |
| 72 | "我已经为你生成了一个新的多 Agent 工作流,包含大纲、评价器和部署策略。", |
| 73 | "Aster 的沙箱已准备好,所有写入都被限制在 /workspace 目录,你可以放心执行指令。", |
| 74 | "我为这个会话自动挂载了上下文记忆,后续可以直接引用历史工单。", |
| 75 | "Streaming 模式已打开,等待后端返回 token,平均延迟 220ms。", |
nothing calls this directly
no test coverage detected