MCPcopy Create free account
hub / github.com/e2b-dev/surf / sendMessage

Function sendMessage

lib/chat-context.tsx:84–316  ·  view source on GitHub ↗
({
    content,
    sandboxId,
    environment,
    resolution,
  }: SendMessageOptions)

Source from the content-addressed store, hash-verified

82 };
83
84 const sendMessage = async ({
85 content,
86 sandboxId,
87 environment,
88 resolution,
89 }: SendMessageOptions) => {
90 if (isLoading) return;
91
92 setIsLoading(true);
93 setError(null);
94
95 const userMessage: ChatMessage = {
96 role: "user",
97 content,
98 id: Date.now().toString(),
99 };
100
101 setMessages((prev) => [...prev, userMessage]);
102
103 abortControllerRef.current = new AbortController();
104
105 try {
106 const apiMessages = messages
107 .concat(userMessage)
108 .filter((msg) => msg.role === "user" || msg.role === "assistant")
109 .map((msg) => {
110 const typedMsg = msg as UserChatMessage | AssistantChatMessage;
111 return {
112 role: typedMsg.role,
113 content: typedMsg.content,
114 };
115 });
116
117 const response = await fetch("/api/chat", {
118 method: "POST",
119 headers: { "Content-Type": "application/json" },
120 body: JSON.stringify({
121 messages: apiMessages,
122 sandboxId,
123 environment,
124 resolution,
125 }),
126 signal: abortControllerRef.current.signal,
127 });
128
129 if (!response.ok) {
130 throw new Error(`HTTP error! status: ${response.status}`);
131 }
132
133 const reader = response.body?.getReader();
134 if (!reader) throw new Error("Response body is null");
135
136 setMessages((prev) => [
137 ...prev,
138 {
139 role: "system",
140 id: `system-message-${Date.now()}`,
141 content: "Task started",

Callers 2

onSubmitFunction · 0.85
handleExampleClickFunction · 0.85

Calls 3

logDebugFunction · 0.90
logErrorFunction · 0.90
parseSSEEventFunction · 0.85

Tested by

no test coverage detected