Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/components/Chat/ChatWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface Message {

const API_KEY_STORAGE = 'groq-api-key';

const PENDO_AGENT_ID = '175tdkEo-JuHMRNosvR28MCawKc';

export function ChatWidget() {
const [isOpen, setIsOpen] = useState(false);
const [messages, setMessages] = useState<Message[]>([]);
Expand All @@ -21,6 +23,7 @@ export function ChatWidget() {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const messagesEndRef = useRef<HTMLDivElement>(null);
const conversationIdRef = useRef(crypto.randomUUID());

const { objectives, teams, individuals, selectedQuarter } = useOKR();
const { currentUser, currentOrganization } = useAuth();
Expand Down Expand Up @@ -53,18 +56,30 @@ export function ChatWidget() {
localStorage.removeItem(API_KEY_STORAGE);
setApiKey('');
setMessages([]);
conversationIdRef.current = crypto.randomUUID();
};

const handleSend = async () => {
const trimmed = input.trim();
if (!trimmed || isLoading) return;

const userMessage: Message = { role: 'user', content: trimmed };
const promptMessageId = crypto.randomUUID();
setMessages((prev) => [...prev, userMessage]);
setInput('');
setError('');
setIsLoading(true);

if (typeof pendo !== 'undefined') {
pendo.trackAgent('prompt', {
agentId: PENDO_AGENT_ID,
conversationId: conversationIdRef.current,
messageId: promptMessageId,
content: trimmed,
suggestedPrompt: false,
});
}

try {
const systemPrompt = buildSystemPrompt({
objectives,
Expand All @@ -83,6 +98,16 @@ export function ChatWidget() {

const response = await sendChatMessage(history, apiKey);
setMessages((prev) => [...prev, { role: 'assistant', content: response }]);

if (typeof pendo !== 'undefined') {
pendo.trackAgent('agent_response', {
agentId: PENDO_AGENT_ID,
conversationId: conversationIdRef.current,
messageId: crypto.randomUUID(),
content: response,
modelUsed: 'llama-3.3-70b-versatile',
});
}
} catch (err) {
const msg = err instanceof Error ? err.message : 'Something went wrong.';
setError(msg);
Expand Down
5 changes: 4 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
declare var pendo: any;
declare var pendo: {
trackAgent: (eventType: string, metadata: object) => void;
[key: string]: any;
};