From c312c4aef6865eb709ded2cc16a3a0fc8024af73 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 27 Nov 2025 11:35:00 -0700 Subject: [PATCH] Asksage usage fetch models (#7329) * Add flagship models * Add model fetching * Add usage handling, tool result handling * Update AskSageProvider.tsx * Create eight-pants-explode.md --------- Co-authored-by: alex-mcgraw-askSage --- .changeset/eight-pants-explode.md | 5 ++ src/core/api/providers/asksage.ts | 83 ++++++++++++++++++- src/shared/api.ts | 40 +++++++++ .../settings/providers/AskSageProvider.tsx | 44 +++++++++- 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 .changeset/eight-pants-explode.md diff --git a/.changeset/eight-pants-explode.md b/.changeset/eight-pants-explode.md new file mode 100644 index 00000000..37f3d440 --- /dev/null +++ b/.changeset/eight-pants-explode.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +This minor change adds new models and image support for rleated models, adds fetching of model info from API, updates tool handling, and adds retrieval usage stats for individual messages and a user's monthly token usage. diff --git a/src/core/api/providers/asksage.ts b/src/core/api/providers/asksage.ts index d2f4f58c..254346ee 100644 --- a/src/core/api/providers/asksage.ts +++ b/src/core/api/providers/asksage.ts @@ -19,6 +19,16 @@ type AskSageRequest = { }[] model: string dataset: "none" + usage: boolean +} + +type AskSageUsage = { + model_tokens: { + completion_tokens: number + prompt_tokens: number + total_tokens: number + } + asksage_tokens: number } type AskSageResponse = { @@ -28,6 +38,18 @@ type AskSageResponse = { response: string // Generated response message message: string + // whether embedding & vector systems are down + embedding_down: boolean + vectors_down: boolean + // references if dataset is not none + references: string + type: string + added_obj: any + tool_calls: any + // usage metrics + usage: AskSageUsage | null + tool_responses: any[] + tool_calls_unified: any[] } export class AskSageHandler implements ApiHandler { @@ -50,7 +72,6 @@ export class AskSageHandler implements ApiHandler { async *createMessage(systemPrompt: string, messages: ClineStorageMessage[]): ApiStream { try { const model = this.getModel() - // Transform messages into AskSageRequest format const formattedMessages = messages.map((msg) => { const content = Array.isArray(msg.content) @@ -68,6 +89,7 @@ export class AskSageHandler implements ApiHandler { message: formattedMessages, model: model.id, dataset: "none", + usage: true, } // Make request to AskSage API @@ -91,15 +113,72 @@ export class AskSageHandler implements ApiHandler { throw new Error("No content in AskSage response") } - // Return entire response as a single chunk since streaming is not supported + // Yield tool responses if they exist + if (result.tool_responses && result.tool_responses.length > 0) { + for (const toolResponse of result.tool_responses) { + yield { + type: "text", + text: `[Tool Response: ${JSON.stringify(toolResponse)}]\n`, + } + } + } + + // Yield the main response text yield { type: "text", text: result.message, } + + // Yield usage information if available + if (result.usage) { + yield { + type: "usage", + inputTokens: result.usage.model_tokens.prompt_tokens, + outputTokens: result.usage.model_tokens.completion_tokens, + cacheReadTokens: 0, + cacheWriteTokens: 0, + totalCost: result.usage.asksage_tokens, // Cost = Consumed AskSage tokens + } + } } catch (error) { if (error instanceof Error) { throw new Error(`AskSage request failed: ${error.message}`) } + throw error + } + } + + async getApiStreamUsage() { + if (!this.apiKey) { + return undefined + } + + try { + const response = await fetch(`${this.apiUrl}/count-monthly-tokens`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "x-access-tokens": this.apiKey, + }, + body: JSON.stringify({ app_name: "asksage" }), + }) + + if (!response.ok) { + console.error("Failed to fetch AskSage usage", await response.text()) + return undefined + } + + const data = await response.json() + const usedTokens = data.response as number + + return { + type: "usage" as const, + inputTokens: usedTokens, + outputTokens: 0, + } + } catch (error) { + console.error("Error fetching AskSage usage:", error) + return undefined } } diff --git a/src/shared/api.ts b/src/shared/api.ts index 0fae3835..e56a7404 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -2664,6 +2664,46 @@ export const askSageModels = { "google-gemini-2.5-pro": { maxTokens: 65536, contextWindow: 1_048_576, + supportsImages: true, + supportsPromptCache: false, + inputPrice: 0, + outputPrice: 0, + }, + "google-claude-45-sonnet": { + maxTokens: 64000, + contextWindow: 200_000, + supportsImages: true, + supportsPromptCache: false, + inputPrice: 0, + outputPrice: 0, + }, + "google-claude-4-opus": { + maxTokens: 32000, + contextWindow: 200_000, + supportsImages: true, + supportsPromptCache: false, + inputPrice: 0, + outputPrice: 0, + }, + "gpt-5": { + maxTokens: 65536, + contextWindow: 2_097_152, + supportsImages: false, + supportsPromptCache: false, + inputPrice: 0, + outputPrice: 0, + }, + "gpt-5-mini": { + maxTokens: 32768, + contextWindow: 1_048_576, + supportsImages: false, + supportsPromptCache: false, + inputPrice: 0, + outputPrice: 0, + }, + "gpt-5-nano": { + maxTokens: 16384, + contextWindow: 262_144, supportsImages: false, supportsPromptCache: false, inputPrice: 0, diff --git a/webview-ui/src/components/settings/providers/AskSageProvider.tsx b/webview-ui/src/components/settings/providers/AskSageProvider.tsx index b2f67900..db36fa26 100644 --- a/webview-ui/src/components/settings/providers/AskSageProvider.tsx +++ b/webview-ui/src/components/settings/providers/AskSageProvider.tsx @@ -1,5 +1,6 @@ -import { askSageDefaultURL, askSageModels } from "@shared/api" +import { askSageDefaultURL, askSageModels, ModelInfo } from "@shared/api" import { Mode } from "@shared/storage/types" +import { useEffect, useState } from "react" import { useExtensionState } from "@/context/ExtensionStateContext" import { ApiKeyField } from "../common/ApiKeyField" import { DebouncedTextField } from "../common/DebouncedTextField" @@ -23,10 +24,49 @@ interface AskSageProviderProps { export const AskSageProvider = ({ showModelOptions, isPopup, currentMode }: AskSageProviderProps) => { const { apiConfiguration } = useExtensionState() const { handleFieldChange, handleModeFieldChange } = useApiConfigurationHandlers() + const [availableModels, setAvailableModels] = useState>(askSageModels) // Get the normalized configuration const { selectedModelId, selectedModelInfo } = normalizeApiConfiguration(apiConfiguration, currentMode) + useEffect(() => { + const fetchModels = async () => { + try { + const apiUrl = apiConfiguration?.asksageApiUrl || askSageDefaultURL + const response = await fetch(`${apiUrl}/get-models`) + + if (!response.ok) { + console.error("Failed to fetch AskSage models, falling back to default list.") + setAvailableModels(askSageModels) + return + } + + const data = await response.json() + const modelIds = data.response as string[] + + if (Array.isArray(modelIds) && modelIds.length > 0) { + const filteredModels = Object.entries(askSageModels) + .filter(([id]) => modelIds.includes(id)) + .reduce( + (acc, [id, info]) => { + acc[id] = info + return acc + }, + {} as Record, + ) + setAvailableModels(Object.keys(filteredModels).length > 0 ? filteredModels : askSageModels) + } else { + setAvailableModels(askSageModels) + } + } catch (error) { + console.error("Error fetching AskSage models:", error) + setAvailableModels(askSageModels) + } + } + + fetchModels() + }, [apiConfiguration?.asksageApiUrl]) + return (
handleModeFieldChange( { plan: "planModeApiModelId", act: "actModeApiModelId" },