fix: using baseURL to fetch models and get API key (#5732)
* fix: using baseURL to fetch models and get API key * change set * fix: keeping /v1 in URL This is because `URL` constructor, strips the path when giving two arguments.
This commit is contained in:
1 parent
c3591290d2
commit
6b9dbccfd2
6 files changed
+128
-77
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"claude-dev": patch
|
||||
---
|
||||
|
||||
use Requesty's base URL to get models and API keys
|
||||
@@ -2,6 +2,7 @@ import { Anthropic } from "@anthropic-ai/sdk"
|
||||
import { ModelInfo, requestyDefaultModelId, requestyDefaultModelInfo } from "@shared/api"
|
||||
import { calculateApiCostOpenAI } from "@utils/cost"
|
||||
import OpenAI from "openai"
|
||||
import { toRequestyServiceStringUrl } from "@/shared/providers/requesty"
|
||||
import { ApiHandler, CommonApiHandlerOptions } from "../index"
|
||||
import { withRetry } from "../retry"
|
||||
import { convertToOpenAiMessages } from "../transform/openai-format"
|
||||
@@ -41,7 +42,7 @@ export class RequestyHandler implements ApiHandler {
|
||||
}
|
||||
try {
|
||||
this.client = new OpenAI({
|
||||
baseURL: this.options.requestyBaseUrl || "https://router.requesty.ai/v1",
|
||||
baseURL: toRequestyServiceStringUrl(this.options.requestyBaseUrl),
|
||||
apiKey: this.options.requestyApiKey,
|
||||
defaultHeaders: {
|
||||
"HTTP-Referer": "https://cline.bot",
|
||||
@@ -82,7 +83,6 @@ export class RequestyHandler implements ApiHandler {
|
||||
? thinking
|
||||
: {}
|
||||
|
||||
// @ts-ignore-next-line
|
||||
const stream = await client.chat.completions.create({
|
||||
model: model.id,
|
||||
max_tokens: model.info.maxTokens || undefined,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { EmptyRequest } from "@shared/proto/cline/common"
|
||||
import { OpenRouterCompatibleModelInfo, OpenRouterModelInfo } from "@shared/proto/cline/models"
|
||||
import axios from "axios"
|
||||
import { toRequestyServiceUrl } from "@/shared/providers/requesty"
|
||||
import { Controller } from ".."
|
||||
|
||||
/**
|
||||
@@ -20,10 +21,15 @@ export async function refreshRequestyModels(controller: Controller, _: EmptyRequ
|
||||
const models: Record<string, OpenRouterModelInfo> = {}
|
||||
try {
|
||||
const apiKey = controller.stateManager.getSecretKey("requestyApiKey")
|
||||
const baseUrl = controller.stateManager.getGlobalStateKey("requestyBaseUrl")
|
||||
|
||||
const resolvedUrl = toRequestyServiceUrl(baseUrl)
|
||||
const url = new URL(`${resolvedUrl.pathname}/models`, resolvedUrl).toString()
|
||||
|
||||
const headers = {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
}
|
||||
const response = await axios.get("https://router.requesty.ai/v1/models", { headers })
|
||||
const response = await axios.get(url, { headers })
|
||||
if (response.data?.data) {
|
||||
for (const model of response.data.data) {
|
||||
const modelInfo: OpenRouterModelInfo = OpenRouterModelInfo.create({
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const REQUESTY_BASE_URL = "https://router.requesty.ai/v1"
|
||||
|
||||
type URLType = "router" | "app" | "api"
|
||||
|
||||
const replaceCname = (baseUrl: string, type: URLType): string => {
|
||||
if (type === "router") {
|
||||
return baseUrl
|
||||
} else {
|
||||
return baseUrl.replace("router", type).replace("v1", "")
|
||||
}
|
||||
}
|
||||
|
||||
export const toRequestyServiceUrl = (baseUrl?: string, service: URLType = "router"): URL => {
|
||||
const url = replaceCname(baseUrl ?? REQUESTY_BASE_URL, service)
|
||||
|
||||
return new URL(url)
|
||||
}
|
||||
|
||||
export const toRequestyServiceStringUrl = (baseUrl?: string, service: URLType = "router"): string => {
|
||||
return toRequestyServiceUrl(baseUrl, service).toString()
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { requestyDefaultModelId, requestyDefaultModelInfo } from "@shared/api"
|
||||
import { EmptyRequest } from "@shared/proto/cline/common"
|
||||
import { toRequestyServiceUrl } from "@shared/providers/requesty"
|
||||
import { Mode } from "@shared/storage/types"
|
||||
import { VSCodeLink, VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
|
||||
import Fuse from "fuse.js"
|
||||
@@ -18,10 +19,11 @@ import { useApiConfigurationHandlers } from "./utils/useApiConfigurationHandlers
|
||||
|
||||
export interface RequestyModelPickerProps {
|
||||
isPopup?: boolean
|
||||
baseUrl?: string
|
||||
currentMode: Mode
|
||||
}
|
||||
|
||||
const RequestyModelPicker: React.FC<RequestyModelPickerProps> = ({ isPopup, currentMode }) => {
|
||||
const RequestyModelPicker: React.FC<RequestyModelPickerProps> = ({ isPopup, baseUrl, currentMode }) => {
|
||||
const { apiConfiguration, requestyModels, setRequestyModels } = useExtensionState()
|
||||
const { handleModeFieldsChange } = useApiConfigurationHandlers()
|
||||
const modeFields = getModeSpecificFields(apiConfiguration, currentMode)
|
||||
@@ -32,13 +34,22 @@ const RequestyModelPicker: React.FC<RequestyModelPickerProps> = ({ isPopup, curr
|
||||
const itemRefs = useRef<(HTMLDivElement | null)[]>([])
|
||||
const dropdownListRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const resolvedUrl = toRequestyServiceUrl(baseUrl)
|
||||
const requestyModelListUrl = new URL("models", resolvedUrl)
|
||||
|
||||
const handleModelChange = (newModelId: string) => {
|
||||
// could be setting invalid model id/undefined info but validation will catch it
|
||||
|
||||
handleModeFieldsChange(
|
||||
{
|
||||
requestyModelId: { plan: "planModeRequestyModelId", act: "actModeRequestyModelId" },
|
||||
requestyModelInfo: { plan: "planModeRequestyModelInfo", act: "actModeRequestyModelInfo" },
|
||||
requestyModelId: {
|
||||
plan: "planModeRequestyModelId",
|
||||
act: "actModeRequestyModelId",
|
||||
},
|
||||
requestyModelInfo: {
|
||||
plan: "planModeRequestyModelInfo",
|
||||
act: "actModeRequestyModelInfo",
|
||||
},
|
||||
},
|
||||
{
|
||||
requestyModelId: newModelId,
|
||||
@@ -248,16 +259,18 @@ const RequestyModelPicker: React.FC<RequestyModelPickerProps> = ({ isPopup, curr
|
||||
marginTop: 0,
|
||||
color: "var(--vscode-descriptionForeground)",
|
||||
}}>
|
||||
The extension automatically fetches the latest list of models available on{" "}
|
||||
<VSCodeLink href="https://app.requesty.ai/router/list" style={{ display: "inline", fontSize: "inherit" }}>
|
||||
Requesty.
|
||||
</VSCodeLink>
|
||||
If you're unsure which model to choose, Cline works best with{" "}
|
||||
<VSCodeLink
|
||||
onClick={() => handleModelChange("anthropic/claude-3-7-sonnet-latest")}
|
||||
style={{ display: "inline", fontSize: "inherit" }}>
|
||||
anthropic/claude-3-7-sonnet-latest.
|
||||
</VSCodeLink>
|
||||
<>
|
||||
The extension automatically fetches the latest list of models available on{" "}
|
||||
<VSCodeLink href={requestyModelListUrl.toString()} style={{ display: "inline", fontSize: "inherit" }}>
|
||||
Requesty.
|
||||
</VSCodeLink>
|
||||
If you're unsure which model to choose, Cline works best with{" "}
|
||||
<VSCodeLink
|
||||
onClick={() => handleModelChange("anthropic/claude-3-7-sonnet-latest")}
|
||||
style={{ display: "inline", fontSize: "inherit" }}>
|
||||
anthropic/claude-3-7-sonnet-latest.
|
||||
</VSCodeLink>
|
||||
</>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
@@ -269,84 +282,84 @@ export default RequestyModelPicker
|
||||
// Dropdown
|
||||
|
||||
const DropdownWrapper = styled.div`
|
||||
position: relative;
|
||||
width: 100%;
|
||||
position: relative;
|
||||
width: 100%;
|
||||
`
|
||||
|
||||
export const REQUESTY_MODEL_PICKER_Z_INDEX = 1_000
|
||||
|
||||
const DropdownList = styled.div`
|
||||
position: absolute;
|
||||
top: calc(100% - 3px);
|
||||
left: 0;
|
||||
width: calc(100% - 2px);
|
||||
max-height: 200px;
|
||||
overflow-y: auto;
|
||||
background-color: var(--vscode-dropdown-background);
|
||||
border: 1px solid var(--vscode-list-activeSelectionBackground);
|
||||
z-index: ${REQUESTY_MODEL_PICKER_Z_INDEX - 1};
|
||||
border-bottom-left-radius: 3px;
|
||||
border-bottom-right-radius: 3px;
|
||||
position: absolute;
|
||||
top: calc(100% - 3px);
|
||||
left: 0;
|
||||
width: calc(100% - 2px);
|
||||
max-height: 200px;
|
||||
overflow-y: auto;
|
||||
background-color: var(--vscode-dropdown-background);
|
||||
border: 1px solid var(--vscode-list-activeSelectionBackground);
|
||||
z-index: ${REQUESTY_MODEL_PICKER_Z_INDEX - 1};
|
||||
border-bottom-left-radius: 3px;
|
||||
border-bottom-right-radius: 3px;
|
||||
`
|
||||
|
||||
const DropdownItem = styled.div<{ isSelected: boolean }>`
|
||||
padding: 5px 10px;
|
||||
cursor: pointer;
|
||||
word-break: break-all;
|
||||
white-space: normal;
|
||||
padding: 5px 10px;
|
||||
cursor: pointer;
|
||||
word-break: break-all;
|
||||
white-space: normal;
|
||||
|
||||
background-color: ${({ isSelected }) => (isSelected ? "var(--vscode-list-activeSelectionBackground)" : "inherit")};
|
||||
background-color: ${({ isSelected }) => (isSelected ? "var(--vscode-list-activeSelectionBackground)" : "inherit")};
|
||||
|
||||
&:hover {
|
||||
background-color: var(--vscode-list-activeSelectionBackground);
|
||||
}
|
||||
&:hover {
|
||||
background-color: var(--vscode-list-activeSelectionBackground);
|
||||
}
|
||||
`
|
||||
|
||||
// Markdown
|
||||
|
||||
const StyledMarkdown = styled.div`
|
||||
font-family:
|
||||
var(--vscode-font-family),
|
||||
system-ui,
|
||||
-apple-system,
|
||||
BlinkMacSystemFont,
|
||||
"Segoe UI",
|
||||
Roboto,
|
||||
Oxygen,
|
||||
Ubuntu,
|
||||
Cantarell,
|
||||
"Open Sans",
|
||||
"Helvetica Neue",
|
||||
sans-serif;
|
||||
font-size: 12px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
font-family:
|
||||
var(--vscode-font-family),
|
||||
system-ui,
|
||||
-apple-system,
|
||||
BlinkMacSystemFont,
|
||||
"Segoe UI",
|
||||
Roboto,
|
||||
Oxygen,
|
||||
Ubuntu,
|
||||
Cantarell,
|
||||
"Open Sans",
|
||||
"Helvetica Neue",
|
||||
sans-serif;
|
||||
font-size: 12px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
|
||||
p,
|
||||
li,
|
||||
ol,
|
||||
ul {
|
||||
line-height: 1.25;
|
||||
margin: 0;
|
||||
}
|
||||
p,
|
||||
li,
|
||||
ol,
|
||||
ul {
|
||||
line-height: 1.25;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
ol,
|
||||
ul {
|
||||
padding-left: 1.5em;
|
||||
margin-left: 0;
|
||||
}
|
||||
ol,
|
||||
ul {
|
||||
padding-left: 1.5em;
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
p {
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
p {
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
}
|
||||
a {
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
a {
|
||||
text-decoration: none;
|
||||
}
|
||||
a {
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
export const ModelDescriptionMarkdown = memo(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { toRequestyServiceUrl } from "@shared/providers/requesty"
|
||||
import { Mode } from "@shared/storage/types"
|
||||
import { VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react"
|
||||
import { useState } from "react"
|
||||
@@ -25,13 +26,16 @@ export const RequestyProvider = ({ showModelOptions, isPopup, currentMode }: Req
|
||||
|
||||
const [requestyEndpointSelected, setRequestyEndpointSelected] = useState(!!apiConfiguration?.requestyBaseUrl)
|
||||
|
||||
const resolvedUrl = toRequestyServiceUrl(apiConfiguration?.requestyBaseUrl, "app")
|
||||
const apiKeyUrl = new URL("api-keys", resolvedUrl).toString()
|
||||
|
||||
return (
|
||||
<div>
|
||||
<ApiKeyField
|
||||
initialValue={apiConfiguration?.requestyApiKey || ""}
|
||||
onChange={(value) => handleFieldChange("requestyApiKey", value)}
|
||||
providerName="Requesty"
|
||||
signupUrl="https://app.requesty.ai/api-keys"
|
||||
signupUrl={apiKeyUrl}
|
||||
/>
|
||||
<VSCodeCheckbox
|
||||
checked={requestyEndpointSelected}
|
||||
@@ -56,7 +60,9 @@ export const RequestyProvider = ({ showModelOptions, isPopup, currentMode }: Req
|
||||
type="url"
|
||||
/>
|
||||
)}
|
||||
{showModelOptions && <RequestyModelPicker currentMode={currentMode} isPopup={isPopup} />}
|
||||
{showModelOptions && (
|
||||
<RequestyModelPicker baseUrl={apiConfiguration?.requestyBaseUrl} currentMode={currentMode} isPopup={isPopup} />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user