(event?: FormEvent<HTMLFormElement>)
| 928 | } |
| 929 | |
| 930 | async function sendMessage(event?: FormEvent<HTMLFormElement>) { |
| 931 | event?.preventDefault() |
| 932 | |
| 933 | if (!session || !input.trim() || isRunning) { |
| 934 | return |
| 935 | } |
| 936 | |
| 937 | const conversationId = activeConversation.id |
| 938 | const activeSession = session |
| 939 | const activeModel = model |
| 940 | const userText = input.trim() |
| 941 | const assistantId = crypto.randomUUID() |
| 942 | lastStreamEventTypeRef.current[conversationId] = null |
| 943 | setConversationRuntime(conversationId, (current) => ({ |
| 944 | ...current, |
| 945 | isRunning: true, |
| 946 | })) |
| 947 | updateConversation(conversationId, (conversation) => ({ |
| 948 | ...conversation, |
| 949 | input: "", |
| 950 | messages: [ |
| 951 | ...conversation.messages, |
| 952 | { id: crypto.randomUUID(), role: "user", content: userText }, |
| 953 | { |
| 954 | id: createAssistantSegmentId(assistantId), |
| 955 | role: "assistant", |
| 956 | content: "", |
| 957 | }, |
| 958 | ], |
| 959 | updatedAt: Date.now(), |
| 960 | })) |
| 961 | |
| 962 | try { |
| 963 | const response = await fetch("/api/chat", { |
| 964 | method: "POST", |
| 965 | headers: { "Content-Type": "application/json" }, |
| 966 | body: JSON.stringify({ |
| 967 | sessionId: activeSession.id, |
| 968 | message: userText, |
| 969 | model: activeModel, |
| 970 | }), |
| 971 | }) |
| 972 | |
| 973 | if (!response.ok || !response.body) { |
| 974 | const data = await response.json().catch(() => ({})) |
| 975 | throw new Error(data.error ?? "The agent request failed.") |
| 976 | } |
| 977 | |
| 978 | await readAgentStream(response.body, conversationId, assistantId) |
| 979 | } catch (error) { |
| 980 | const message = getFriendlyErrorMessage(error) |
| 981 | setConversationMessages(conversationId, (current) => [ |
| 982 | ...current, |
| 983 | { id: crypto.randomUUID(), role: "system", content: message }, |
| 984 | ]) |
| 985 | } finally { |
| 986 | setConversationMessages(conversationId, finalizeActiveThinkingMessages) |
| 987 | setConversationRuntime(conversationId, (current) => ({ |
nothing calls this directly
no test coverage detected