mirror of
https://github.com/cline/cline.git
synced 2026-09-19 02:05:44 +08:00
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { Anthropic } from "@anthropic-ai/sdk"
|
|
import OpenAI from "openai"
|
|
import { ApiHandler } from "../"
|
|
import { ApiHandlerOptions, ModelInfo, openAiModelInfoSaneDefaults } from "@shared/api"
|
|
import { convertToOpenAiMessages } from "../transform/openai-format"
|
|
import { ApiStream } from "../transform/stream"
|
|
import { withRetry } from "../retry"
|
|
|
|
export class LmStudioHandler implements ApiHandler {
|
|
private options: ApiHandlerOptions
|
|
private client: OpenAI
|
|
|
|
constructor(options: ApiHandlerOptions) {
|
|
this.options = options
|
|
this.client = new OpenAI({
|
|
baseURL: (this.options.lmStudioBaseUrl || "http://localhost:1234") + "/v1",
|
|
apiKey: "noop",
|
|
})
|
|
}
|
|
|
|
@withRetry({ retryAllErrors: true })
|
|
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
|
|
const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
|
|
{ role: "system", content: systemPrompt },
|
|
...convertToOpenAiMessages(messages),
|
|
]
|
|
|
|
try {
|
|
const stream = await this.client.chat.completions.create({
|
|
model: this.getModel().id,
|
|
messages: openAiMessages,
|
|
stream: true,
|
|
})
|
|
for await (const chunk of stream) {
|
|
const delta = chunk.choices[0]?.delta
|
|
if (delta?.content) {
|
|
yield {
|
|
type: "text",
|
|
text: delta.content,
|
|
}
|
|
}
|
|
if (delta && "reasoning_content" in delta && delta.reasoning_content) {
|
|
yield {
|
|
type: "reasoning",
|
|
reasoning: (delta.reasoning_content as string | undefined) || "",
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// LM Studio doesn't return an error code/body for now
|
|
throw new Error(
|
|
"Please check the LM Studio developer logs to debug what went wrong. You may need to load the model with a larger context length to work with Cline's prompts.",
|
|
)
|
|
}
|
|
}
|
|
|
|
getModel(): { id: string; info: ModelInfo } {
|
|
return {
|
|
id: this.options.lmStudioModelId || "",
|
|
info: openAiModelInfoSaneDefaults,
|
|
}
|
|
}
|
|
}
|