From 3ed47fba1723babff9bcb9d3aed1e36f5e89ec55 Mon Sep 17 00:00:00 2001 From: Bee <68532117+abeatrix@users.noreply.github.com> Date: Thu, 10 Jul 2025 15:58:13 -0700 Subject: [PATCH] Request validation and remove credit balance for cline team (#4817) * Fix: Ensure Cline client is initialized with the latest auth token The Cline client was not being re-initialized with the latest authentication token after the user signs in. This resulted in the client using an outdated or non-existent token, leading to authentication errors when making API requests. This commit ensures that the Cline client is initialized with the most recent authentication token by setting the `apiKey` property of the `OpenAI` client instance to the current auth token retrieved from `AuthService` before every request. This guarantees that the client always uses the valid and up-to-date token for authentication. * changeset * move this._authService.getAuthToken() to ensureClient * Request validation and remove credit balance for cline team Add request validation for Cline API requests and fix the user interface for displaying credit-related information in UI. The changes include: - **Credit Balance Validation:** Implemented `validateRequest` method in `ClineAccountService` to check user's credit balance before making API requests. Requests from active organizations are skipped. An error is thrown if the balance is insufficient. - **Error Handling:** Improved error handling in `ClineHandler` to provide more informative error messages to the user. - **UI Enhancements:** - Updated `CreditLimitError` component to display the current balance that matches the account view balance format (4 decimal places). - Modified `ChatRow` to parse error messages and display the `CreditLimitError` component when applicable. - Updated `AccountView` to only display credit balance for user accounts, not organization accounts. - Removed unused props from `CreditLimitError` component. Context: https://cline-space.slack.com/archives/C08KYBFL9DJ/p1752182278852399?thread_ts=1752164726.247429&cid=C08KYBFL9DJ - **Dependencies:** Updated dependencies in `webview-ui` to include `tailwindcss` and configured `tailwind.config.js` to support VSCode theme variables. * changeset * Update src/api/providers/cline.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: abeatrix Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .changeset/fast-singers-rule.md | 5 + src/api/providers/cline.ts | 25 ++-- src/services/account/ClineAccountService.ts | 36 +++++ .../src/components/account/AccountView.tsx | 124 +++++++----------- webview-ui/src/components/chat/ChatRow.tsx | 16 +-- .../src/components/chat/CreditLimitError.tsx | 32 +++-- 6 files changed, 121 insertions(+), 117 deletions(-) create mode 100644 .changeset/fast-singers-rule.md diff --git a/.changeset/fast-singers-rule.md b/.changeset/fast-singers-rule.md new file mode 100644 index 00000000..d1d21ad4 --- /dev/null +++ b/.changeset/fast-singers-rule.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Better error handling for credits-related issues. diff --git a/src/api/providers/cline.ts b/src/api/providers/cline.ts index 318b302b..f01a6afb 100644 --- a/src/api/providers/cline.ts +++ b/src/api/providers/cline.ts @@ -64,19 +64,19 @@ export class ClineHandler implements ApiHandler { @withRetry() async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { - const client = await this.ensureClient() - - this.lastGenerationId = undefined - - const me = await this.clineAccountService.fetchMe() - console.log( - "SwitchAuthToken: Active Organization", - me?.organizations.filter((org) => org.active)[0]?.name || "No active organization", - ) - - let didOutputUsage: boolean = false - try { + // Only continue the request if the user: + // 1. Has signed in to Cline with a token + // 2. Has more than 0 credits + // Or an error is thrown. + await this.clineAccountService.validateRequest() + + const client = await this.ensureClient() + + this.lastGenerationId = undefined + + let didOutputUsage: boolean = false + const stream = await createOpenRouterStream( client, systemPrompt, @@ -183,6 +183,7 @@ export class ClineHandler implements ApiHandler { throw new Error("Unauthorized: Please sign in to Cline before trying again.") } console.error("Cline API Error:", error) + throw error instanceof Error ? error : new Error(String(error)) } } diff --git a/src/services/account/ClineAccountService.ts b/src/services/account/ClineAccountService.ts index 4007dd05..1eeddea4 100644 --- a/src/services/account/ClineAccountService.ts +++ b/src/services/account/ClineAccountService.ts @@ -82,6 +82,42 @@ export class ClineAccountService { } } + /** + * Validates if the user has sufficient credits to make API requests. + * This checks the user's balance and throws an error if the balance is insufficient or if the request fails. + * @throws Error if the user has insufficient credits or if the request fails + * @returns {Promise} A promise that resolves if the user has sufficient credits. + */ + async validateRequest(): Promise { + try { + const { organizations, id } = await this.authenticatedRequest(`/api/v1/users/me`) + const activeOrganization = organizations.find((org) => org.active) + console.log("SwitchAuthToken: Active Organization", activeOrganization?.name || "No active organization") + + // Skip balance check for active organizations + if (activeOrganization) { + return + } + + const balance = await this.authenticatedRequest(`/api/v1/users/${id}/balance`) + const currentBalance = Number(balance?.balance) || 0 + + // Throw error if insufficient credits (balance <= 0) + if (currentBalance <= 0) { + throw new Error( + JSON.stringify({ + code: "insufficient_credits", + current_balance: currentBalance, + message: "Not enough credits available", + }), + ) + } + } catch (error) { + console.error("Invalid Cline API request:", error) + throw error instanceof Error ? error : new Error(`Invalid Request: ${error}`) + } + } + /** * RPC variant that fetches the user's current credit balance without posting to webview * @returns Balance data or undefined if failed diff --git a/webview-ui/src/components/account/AccountView.tsx b/webview-ui/src/components/account/AccountView.tsx index 179c0c45..8ed4da17 100644 --- a/webview-ui/src/components/account/AccountView.tsx +++ b/webview-ui/src/components/account/AccountView.tsx @@ -1,5 +1,5 @@ import { VSCodeButton, VSCodeDivider, VSCodeLink, VSCodeDropdown, VSCodeOption } from "@vscode/webview-ui-toolkit/react" -import { memo, useEffect, useState } from "react" +import { memo, useCallback, useEffect, useState } from "react" import { BadgeCent } from "lucide-react" import { useClineAuth } from "@/context/ClineAuthContext" import VSCodeButtonLink from "../common/VSCodeButtonLink" @@ -10,7 +10,7 @@ import { UsageTransaction, PaymentTransaction } from "@shared/ClineAccount" import { useExtensionState } from "@/context/ExtensionStateContext" import { AccountServiceClient } from "@/services/grpc-client" import { EmptyRequest } from "@shared/proto/common" -import { GetOrganizationCreditsRequest, UserOrganization, UserOrganizationUpdateRequest } from "@shared/proto/account" +import { UserOrganization, UserOrganizationUpdateRequest } from "@shared/proto/account" type VSCodeDropdownChangeEvent = Event & { target: { @@ -72,31 +72,6 @@ export const ClineAccountView = () => { } } - async function getOrganizationCredits() { - setIsLoading(true) - if (!activeOrganization) { - await getUserCredits() - return - } - try { - const response = await AccountServiceClient.getOrganizationCredits( - GetOrganizationCreditsRequest.create({ - organizationId: activeOrganization.organizationId, - }), - ) - setBalance(response.balance?.currentBalance || 0) - setUsageData(response.usageTransactions) - setPaymentsData(response.paymentTransactions) - } catch (error) { - console.error("Failed to fetch organization credits data:", error) - setBalance(0) - setUsageData([]) - setPaymentsData([]) - } finally { - setIsLoading(false) - } - } - async function getUserOrganizations() { setIsLoading(true) try { @@ -118,8 +93,7 @@ export const ClineAccountView = () => { const fetchUserData = async () => { try { - await getUserCredits() - await getUserOrganizations() + Promise.all([getUserCredits(), getUserOrganizations()]) } catch (error) { console.error("Failed to fetch user data:", error) } @@ -128,20 +102,6 @@ export const ClineAccountView = () => { fetchUserData() }, [user]) - useEffect(() => { - if (!activeOrganization) return - - const fetchOrgCredits = async () => { - try { - await getOrganizationCredits() - } catch (error) { - console.error("Failed to fetch organization credits:", error) - } - } - - fetchOrgCredits() - }, [activeOrganization]) - const handleLogin = () => { handleSignIn() } @@ -150,18 +110,23 @@ export const ClineAccountView = () => { handleSignOut() } - const handleOrganizationChange = async (event: any) => { - const newOrgId = (event.target as VSCodeDropdownChangeEvent["target"]).value + const handleOrganizationChange = useCallback( + async (event: any) => { + const newOrgId = (event.target as VSCodeDropdownChangeEvent["target"]).value - if (!activeOrganization || activeOrganization.organizationId !== newOrgId) { - try { - await AccountServiceClient.setUserOrganization(UserOrganizationUpdateRequest.create({ organizationId: newOrgId })) - await getUserOrganizations() - } catch (error) { - console.error("Failed to update organization:", error) + if (activeOrganization?.organizationId !== newOrgId) { + try { + await AccountServiceClient.setUserOrganization( + UserOrganizationUpdateRequest.create({ organizationId: newOrgId }), + ) + await getUserOrganizations() + } catch (error) { + console.error("Failed to update organization:", error) + } } - } - } + }, + [activeOrganization], + ) return (
@@ -217,34 +182,37 @@ export const ClineAccountView = () => {
- + {/* Credit balance is not available for organization account */} + {activeOrganization === null && } -
-
CURRENT BALANCE
+ {activeOrganization === null && ( +
+
CURRENT BALANCE
-
- {isLoading ? ( -
Loading...
- ) : ( - <> - - {/* TODO: Do this in a more correct way. We have to divide by 10000 - * because the balance is stored in microcredits in the backend. - */} - - - - - - )} +
+ {isLoading ? ( +
Loading...
+ ) : ( + <> + + {/* TODO: Do this in a more correct way. We have to divide by 10000 + * because the balance is stored in microcredits in the backend. + */} + + + + + + )} +
+ +
+ + Add Credits + +
- -
- - Add Credits - -
-
+ )} diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx index 97502031..252881e8 100644 --- a/webview-ui/src/components/chat/ChatRow.tsx +++ b/webview-ui/src/components/chat/ChatRow.tsx @@ -1,8 +1,8 @@ -import { VSCodeBadge, VSCodeButton, VSCodeProgressRing } from "@vscode/webview-ui-toolkit/react" +import { VSCodeBadge, VSCodeProgressRing } from "@vscode/webview-ui-toolkit/react" import deepEqual from "fast-deep-equal" import React, { memo, MouseEvent, useCallback, useEffect, useMemo, useRef, useState } from "react" import styled from "styled-components" -import { useEvent, useSize } from "react-use" +import { useSize } from "react-use" import CreditLimitError from "@/components/chat/CreditLimitError" import { OptionsButtons } from "@/components/chat/OptionsButtons" @@ -12,14 +12,12 @@ import CodeBlock, { CODE_BLOCK_BG_COLOR } from "@/components/common/CodeBlock" import MarkdownBlock from "@/components/common/MarkdownBlock" import SuccessButton from "@/components/common/SuccessButton" import { WithCopyButton } from "@/components/common/CopyButton" -import Thumbnails from "@/components/common/Thumbnails" import McpResponseDisplay from "@/components/mcp/chat-display/McpResponseDisplay" import McpResourceRow from "@/components/mcp/configuration/tabs/installed/server-row/McpResourceRow" import McpToolRow from "@/components/mcp/configuration/tabs/installed/server-row/McpToolRow" import { useExtensionState } from "@/context/ExtensionStateContext" import { FileServiceClient, TaskServiceClient, UiServiceClient } from "@/services/grpc-client" import { findMatchingResourceOrTemplate, getMcpServerDisplayName } from "@/utils/mcp" -import { vscode } from "@/utils/vscode" import { ClineApiReqInfo, ClineAskQuestion, @@ -28,7 +26,6 @@ import { ClinePlanModeResponse, ClineSayTool, COMPLETION_RESULT_CHANGES_FLAG, - ExtensionMessage, } from "@shared/ExtensionMessage" import { COMMAND_OUTPUT_STRING, COMMAND_REQ_APP_STRING } from "@shared/combineCommandSequences" import { Int64Request, StringRequest } from "@shared/proto/common" @@ -945,14 +942,13 @@ export const ChatRowContent = memo( <> {(() => { // Try to parse the error message as JSON for credit limit error - const errorData = parseErrorText(apiRequestFailedMessage) + const errorData = parseErrorText( + apiRequestFailedMessage || apiReqStreamingFailedMessage, + ) if (errorData) { if ( errorData.code === "insufficient_credits" && - typeof errorData.current_balance === "number" && - typeof errorData.total_spent === "number" && - typeof errorData.total_promotions === "number" && - typeof errorData.message === "string" + typeof errorData.current_balance === "number" ) { return ( = ({ currentBalance, totalSpent, totalPromotions, message }) => { +const CreditLimitError: React.FC = ({ + currentBalance = 0, + totalSpent = 0, + totalPromotions = 0, + message = "You have run out of credit.", +}) => { + // We have to divide because the balance is stored in microcredits return ( -
-
{message}
-
-
- Current Balance: ${currentBalance.toFixed(2)} +
+
{message}
+
+
+ Current Balance: ${(currentBalance / 1000000).toFixed(4)}
-
Total Spent: ${totalSpent.toFixed(2)}
-
Total Promotions: ${totalPromotions.toFixed(2)}
= ({ currentBalance, tot width: "100%", marginBottom: "8px", }}> - + Buy Credits