Skip to content

Commit 2466068

Browse files
Merge pull request #3 from Zipstack/fix-token-usage-endpoint-OSS-mode
Fix skip token-usage API calls in OSS mode — fetch only in cloud
2 parents 6cee2ee + 217595c commit 2466068

3 files changed

Lines changed: 26 additions & 27 deletions

File tree

backend/backend/core/routers/chat_message/views/message_views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def persist_prompt(self, request: Request, *args, **kwargs) -> Response:
5252
def get_token_usage(self, request, project_id=None, chat_id=None, chat_message_id=None, *args, **kwargs) -> Response:
5353
"""
5454
Get token usage data for a specific chat message.
55-
55+
5656
Returns:
5757
- remaining_balance: Current token balance
5858
- total_consumed: Total tokens consumed
@@ -67,18 +67,18 @@ def get_token_usage(self, request, project_id=None, chat_id=None, chat_message_i
6767
{"error": "Organization header is required"},
6868
status=status.HTTP_400_BAD_REQUEST
6969
)
70-
70+
7171
# Get token usage data using the existing function
7272
token_data = get_token_usage_data(org_id, str(chat_message_id), str(chat_id))
73-
73+
7474
if token_data is None:
7575
return Response(
7676
{"error": "Failed to retrieve token usage data"},
7777
status=status.HTTP_500_INTERNAL_SERVER_ERROR
7878
)
79-
79+
8080
return Response(token_data, status=status.HTTP_200_OK)
81-
81+
8282
except Exception as e:
8383
return Response(
8484
{"error": f"Error retrieving token usage: {str(e)}"},

frontend/src/ide/chat-ai/ExistingChat.jsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ import { TodoGuide } from "./TodoGuide";
1616
import { OnboardingGuide } from "./OnboardingGuide";
1717
import { OnboardingCompletionPopup } from "./OnboardingCompletionPopup";
1818
import { useNotificationService } from "../../service/notification-service";
19+
import { useAxiosPrivate } from "../../service/axios-service";
1920
import { SpinnerLoader } from "../../widgets/spinner_loader";
21+
import { useSessionStore } from "../../store/session-store";
22+
23+
// Cloud-only: fetch per-message token usage (unavailable in OSS — import fails gracefully)
24+
let getTokenUsage = null;
25+
try {
26+
({ getTokenUsage } = require("../../plugins/token-management/token-usage"));
27+
} catch {
28+
// OSS: token usage API not available
29+
}
2030

2131
const ExistingChat = memo(function ExistingChat({
2232
selectedChatId,
@@ -61,8 +71,9 @@ const ExistingChat = memo(function ExistingChat({
6171
onSkipCurrentTask,
6272
onSendButtonClick,
6373
}) {
64-
const { getChatMessagesByChatId, getTokenUsage, updateChatName } =
65-
useChatAIService();
74+
const { getChatMessagesByChatId, updateChatName } = useChatAIService();
75+
const axiosPrivate = useAxiosPrivate();
76+
const isCloud = useSessionStore((state) => state.sessionDetails?.is_cloud);
6677
const chatContainerRef = useRef(null);
6778

6879
const [isLoadingChats, setIsLoadingChats] = useState(false);
@@ -262,10 +273,15 @@ const ExistingChat = memo(function ExistingChat({
262273
: [msg.response].filter(Boolean),
263274
}));
264275

265-
// Fetch token usage for all messages to display in historical conversations
266-
if (updatedData.length > 0) {
276+
// Fetch token usage for all messages to display in historical conversations.
277+
// Only available in cloud mode via the token-usage plugin.
278+
if (getTokenUsage && isCloud && updatedData.length > 0) {
267279
const tokenUsagePromises = updatedData.map((msg) =>
268-
getTokenUsage(selectedChatId, msg.chat_message_id).catch(() => null)
280+
getTokenUsage(
281+
axiosPrivate,
282+
selectedChatId,
283+
msg.chat_message_id
284+
).catch(() => null)
269285
);
270286
const tokenUsageResults = await Promise.all(tokenUsagePromises);
271287

frontend/src/ide/chat-ai/services.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,6 @@ export function useChatAIService() {
134134
return response.data;
135135
};
136136

137-
const getTokenUsage = async (chatId, chatMessageId) => {
138-
const url = `/api/v1/visitran/${orgId}/project/${projectId}/chat/${chatId}/chat-message/${chatMessageId}/token-usage/`;
139-
try {
140-
const response = await axiosPrivate.get(url, {
141-
headers: {
142-
"Content-Type": "application/json",
143-
"X-Organization": orgId,
144-
},
145-
});
146-
return response.data || null;
147-
} catch (error) {
148-
console.warn("Failed to fetch token usage:", error);
149-
return null;
150-
}
151-
};
152-
153137
return {
154138
getAllChats,
155139
deleteChatById,
@@ -161,6 +145,5 @@ export function useChatAIService() {
161145
getChatLlmModels,
162146
completeOnboardingTask,
163147
getOnboardingStatus,
164-
getTokenUsage,
165148
};
166149
}

0 commit comments

Comments
 (0)