Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 261 lines 10.7 KB
Raw
1 'use client';
2
3 import { useEffect, useRef, useState } from 'react';
4 import { usePathname } from 'next/navigation';
5 import { useEveAgent } from 'eve/react';
6 import { Send, X, Sparkles, Square, SquarePen } from 'lucide-react';
7 import { AssistantMessage, EagerSourcePreview, ToolActivity, type EagerSource } from './eve-message';
8 import { closeEveChat, useEveChatOpen } from './eve-chat-store';
9
10 const SUGGESTIONS = [
11 'How do I create a session?',
12 'How does authentication work?',
13 'How do I use the sandbox files?',
14 ];
15
16 /**
17 * EveChat — the right-sidebar docs assistant, backed by the eve agent in
18 * `agent/`. Always mounted (so the session persists) and slid off-screen when
19 * closed. Each turn carries the current route as `clientContext` so Eve can
20 * answer about the page you're on.
21 */
22 export function EveChat() {
23 const isOpen = useEveChatOpen();
24 const pathname = usePathname();
25 const agent = useEveAgent({
26 prepareSend: (input) => ({ ...input, clientContext: { route: pathname } }),
27 });
28
29 const isBusy = agent.status === 'submitted' || agent.status === 'streaming';
30 const messages = agent.data.messages;
31 const lastMessage = messages[messages.length - 1];
32 const lastHasAssistantText =
33 lastMessage?.role === 'assistant' &&
34 lastMessage.parts.some((part) => part.type === 'text' && part.text.trim().length > 0);
35 // Show the loading indicator from submit through retrieval/model synthesis,
36 // until the assistant's text actually starts streaming, so it doesn't flicker off.
37 const thinking = isBusy && !lastHasAssistantText;
38 const inputRef = useRef<HTMLTextAreaElement>(null);
39 const scrollRef = useRef<HTMLDivElement>(null);
40 const previewAbortRef = useRef<AbortController | null>(null);
41 const [eagerSources, setEagerSources] = useState<EagerSource[]>([]);
42
43 useEffect(() => {
44 if (isOpen) inputRef.current?.focus();
45 }, [isOpen]);
46
47 useEffect(() => {
48 scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight });
49 }, [agent.data.messages]);
50
51 function clearEagerPreview() {
52 previewAbortRef.current?.abort();
53 previewAbortRef.current = null;
54 setEagerSources([]);
55 }
56
57 function fetchEagerPreview(message: string) {
58 clearEagerPreview();
59 const controller = new AbortController();
60 previewAbortRef.current = controller;
61
62 void fetch('/api/docs-agent/eager-search', {
63 method: 'POST',
64 headers: { 'Content-Type': 'application/json' },
65 body: JSON.stringify({ message }),
66 signal: controller.signal,
67 })
68 .then((response) => (response.ok ? response.json() : null))
69 .then((data: { sources?: EagerSource[] } | null) => {
70 if (controller.signal.aborted) return;
71 setEagerSources(Array.isArray(data?.sources) ? data.sources : []);
72 })
73 .catch(() => {});
74 }
75
76 function submit(message: string) {
77 const trimmed = message.trim();
78 if (trimmed.length > 0 && !isBusy) {
79 fetchEagerPreview(trimmed);
80 void agent.send({ message: trimmed });
81 }
82 }
83
84 return (
85 <>
86 {isOpen && (
87 <div
88 aria-hidden="true"
89 className="fixed inset-0 z-40 bg-black/20 backdrop-blur-[1px] md:hidden"
90 onClick={closeEveChat}
91 />
92 )}
93 <aside
94 aria-label="Ask AI"
95 className={
96 'fixed right-0 top-0 z-50 flex h-dvh w-full flex-col border-l border-fd-border bg-fd-background shadow-xl transition-transform duration-200 ease-out md:w-[400px] ' +
97 (isOpen ? 'translate-x-0' : 'translate-x-full')
98 }
99 >
100 {/* header */}
101 <div className="flex items-center justify-between border-b border-fd-border px-4 py-3">
102 <div className="flex items-center gap-2">
103 <Sparkles className="size-4 text-[var(--composio-brand)]" aria-hidden="true" />
104 <span className="text-sm font-medium text-fd-foreground">Ask AI</span>
105 <span className="font-mono text-[10px] uppercase tracking-[0.06em] text-fd-foreground/40">docs assistant</span>
106 </div>
107 <div className="flex items-center gap-0.5">
108 <button
109 type="button"
110 aria-label="New chat"
111 title="New chat"
112 onClick={() => {
113 if (isBusy) agent.stop();
114 clearEagerPreview();
115 agent.reset();
116 inputRef.current?.focus();
117 }}
118 disabled={agent.data.messages.length === 0}
119 className="inline-flex size-7 items-center justify-center rounded-md text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-foreground disabled:opacity-40 disabled:hover:bg-transparent"
120 >
121 <SquarePen className="size-4" />
122 </button>
123 <button
124 type="button"
125 aria-label="Close"
126 onClick={closeEveChat}
127 className="inline-flex size-7 items-center justify-center rounded-md text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-foreground"
128 >
129 <X className="size-4" />
130 </button>
131 </div>
132 </div>
133
134 {/* messages */}
135 <div ref={scrollRef} className="flex-1 overflow-y-auto px-4 py-4">
136 {agent.data.messages.length === 0 ? (
137 <div className="flex h-full flex-col justify-center gap-3 text-center">
138 <p className="text-sm text-fd-muted-foreground">
139 Ask about the Composio docs. It answers from the docs and links the pages it used. It&apos;s not customer support and can&apos;t act on your Composio account.
140 </p>
141 <div className="flex flex-col gap-1.5">
142 {SUGGESTIONS.map((s) => (
143 <button
144 key={s}
145 type="button"
146 onClick={() => submit(s)}
147 className="rounded-md border border-fd-border bg-fd-card px-3 py-2 text-left text-[13px] text-fd-foreground/80 transition-colors hover:border-[var(--composio-brand)]/40 hover:text-fd-foreground"
148 >
149 {s}
150 </button>
151 ))}
152 </div>
153 </div>
154 ) : (
155 <ul className="flex flex-col gap-4">
156 {agent.data.messages.map((message) => (
157 <li key={message.id} className={message.role === 'user' ? 'flex justify-end' : ''}>
158 <div
159 className={
160 message.role === 'user'
161 ? 'max-w-[85%] rounded-lg bg-[var(--composio-brand)]/10 px-3 py-2 text-[13px] text-fd-foreground'
162 : 'max-w-full text-[13px] leading-relaxed text-fd-foreground/90'
163 }
164 >
165 {message.parts.map((part, i) => {
166 if (part.type === 'text') {
167 return message.role === 'assistant' ? (
168 <AssistantMessage key={i} text={part.text} />
169 ) : (
170 <p key={i} className="whitespace-pre-wrap break-words">
171 {part.text}
172 </p>
173 );
174 }
175 return message.role === 'assistant' ? (
176 <ToolActivity key={i} part={part} />
177 ) : null;
178 })}
179 </div>
180 </li>
181 ))}
182 {thinking && eagerSources.length > 0 && (
183 <li>
184 <EagerSourcePreview active sources={eagerSources} />
185 </li>
186 )}
187 {thinking && (
188 <li className="flex items-center gap-2 text-[13px] text-fd-muted-foreground">
189 <span className="inline-flex gap-1">
190 <span className="size-1.5 animate-bounce rounded-full bg-fd-muted-foreground/60 [animation-delay:-0.3s]" />
191 <span className="size-1.5 animate-bounce rounded-full bg-fd-muted-foreground/60 [animation-delay:-0.15s]" />
192 <span className="size-1.5 animate-bounce rounded-full bg-fd-muted-foreground/60" />
193 </span>
194 Thinking with the docs…
195 </li>
196 )}
197 </ul>
198 )}
199 </div>
200
201 {/* composer */}
202 <form
203 className="border-t border-fd-border p-3"
204 onSubmit={(event) => {
205 event.preventDefault();
206 const value = inputRef.current?.value ?? '';
207 submit(value);
208 if (inputRef.current) {
209 inputRef.current.value = '';
210 inputRef.current.style.height = 'auto';
211 }
212 }}
213 >
214 <div className="flex items-center gap-2 rounded-lg border border-fd-border bg-fd-card px-3 py-1.5 focus-within:border-[var(--composio-brand)]/50">
215 <textarea
216 ref={inputRef}
217 rows={1}
218 placeholder="Ask about the docs…"
219 className="block max-h-32 min-h-[1.5rem] flex-1 resize-none self-center overflow-y-auto bg-transparent py-0 text-[13px] leading-6 text-fd-foreground outline-none placeholder:text-fd-muted-foreground"
220 onChange={(event) => {
221 // Auto-grow with content, up to max-h-32 (128px), then scroll.
222 const el = event.currentTarget;
223 el.style.height = 'auto';
224 el.style.height = `${Math.min(el.scrollHeight, 128)}px`;
225 }}
226 onKeyDown={(event) => {
227 if (event.key === 'Enter' && !event.shiftKey) {
228 event.preventDefault();
229 submit(event.currentTarget.value);
230 event.currentTarget.value = '';
231 event.currentTarget.style.height = 'auto';
232 }
233 }}
234 />
235 {isBusy ? (
236 <button
237 type="button"
238 aria-label="Stop"
239 onClick={() => agent.stop()}
240 className="inline-flex size-7 items-center justify-center rounded-md text-fd-muted-foreground hover:bg-fd-accent hover:text-fd-foreground"
241 >
242 <Square className="size-3.5" />
243 </button>
244 ) : (
245 <button
246 type="submit"
247 aria-label="Send"
248 className="inline-flex size-7 items-center justify-center rounded-md bg-[var(--composio-brand)] text-white transition-opacity hover:opacity-90"
249 >
250 <Send className="size-3.5" />
251 </button>
252 )}
253 </div>
254 {agent.status === 'error' && (
255 <p className="mt-2 text-[12px] text-red-500">Something went wrong. Try again.</p>
256 )}
257 </form>
258 </aside>
259 </>
260 );
261 }