MCPcopy
hub / github.com/Max-Eee/NeoPass / handleChatMessage

Function handleChatMessage

worker.js:1642–1799  ·  view source on GitHub ↗
(message, sender)

Source from the content-addressed store, hash-verified

1640});
1641
1642async function handleChatMessage(message, sender) {
1643 try {
1644 // Check if user has custom API configured
1645 const customAPIConfig = await getCustomAPIConfig();
1646
1647 if (customAPIConfig.useCustomAPI && customAPIConfig.apiKey) {
1648 // Use custom API for chat
1649 const chatPrompt = message.context
1650 ? `Context: ${message.context}\n\nUser: ${message.message}\n\nPlease provide a helpful response.`
1651 : message.message;
1652
1653 const result = await queryCustomAPI(chatPrompt, false, false, customAPIConfig);
1654
1655 if (typeof result === 'string') {
1656 sendChatResponse(sender.tab.id, result);
1657 } else {
1658 sendChatErrorResponse(sender.tab.id, result.error || 'Failed to get response from custom API');
1659 }
1660 return;
1661 }
1662
1663 // Check if user is logged in
1664 const {
1665 accessToken,
1666 refreshToken,
1667 isPro
1668 } = await getTokens();
1669
1670 // If not logged in and no custom API configured, require custom API
1671 if (!accessToken || !refreshToken) {
1672 sendChatErrorResponse(sender.tab.id, "Please configure your custom API key in Settings or login with Pro to use our proxy-server.");
1673 return;
1674 }
1675
1676 // Always use Pro endpoint
1677 const chatEndpoint = `${API_BASE_URL}/api/pro-chat`;
1678
1679 const requestBody = {
1680 message: message.message,
1681 context: message.context,
1682 refreshToken: refreshToken // Send refresh token for server-side auto-refresh
1683 };
1684
1685 // Include image if present
1686 if (message.image) {
1687 requestBody.image = message.image;
1688 }
1689
1690 let response = await makeAuthenticatedRequest(
1691 chatEndpoint,
1692 "POST",
1693 accessToken,
1694 requestBody
1695 );
1696
1697 // Server automatically handles token refresh if access token expired
1698 // If auth fails, it means refresh token is also invalid/expired
1699 if (!response.ok && (response.status === 401 || response.status === 403)) {

Callers 1

worker.jsFile · 0.85

Calls 6

getCustomAPIConfigFunction · 0.85
queryCustomAPIFunction · 0.85
sendChatResponseFunction · 0.85
sendChatErrorResponseFunction · 0.85
getTokensFunction · 0.85
makeAuthenticatedRequestFunction · 0.85

Tested by

no test coverage detected