MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / LmStudioHandler

Class LmStudioHandler

src/api/providers/lm-studio.ts:20–218  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18import { handleOpenAIError } from "./utils/openai-error-handler"
19
20export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
21 protected options: ApiHandlerOptions
22 private client: OpenAI
23 private readonly providerName = "LM Studio"
24
25 constructor(options: ApiHandlerOptions) {
26 super()
27 this.options = options
28
29 // LM Studio uses "noop" as a placeholder API key
30 const apiKey = "noop"
31
32 this.client = new OpenAI({
33 baseURL: (this.options.lmStudioBaseUrl || "http://localhost:1234") + "/v1",
34 apiKey: apiKey,
35 timeout: this.timeoutMs,
36 })
37 }
38
39 override async *createMessage(
40 systemPrompt: string,
41 messages: Anthropic.Messages.MessageParam[],
42 metadata?: ApiHandlerCreateMessageMetadata,
43 ): ApiStream {
44 const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
45 { role: "system", content: systemPrompt },
46 ...convertToOpenAiMessages(messages),
47 ]
48
49 // -------------------------
50 // Track token usage
51 // -------------------------
52 const toContentBlocks = (
53 blocks: Anthropic.Messages.MessageParam[] | string,
54 ): Anthropic.Messages.ContentBlockParam[] => {
55 if (typeof blocks === "string") {
56 return [{ type: "text", text: blocks }]
57 }
58
59 const result: Anthropic.Messages.ContentBlockParam[] = []
60 for (const msg of blocks) {
61 if (typeof msg.content === "string") {
62 result.push({ type: "text", text: msg.content })
63 } else if (Array.isArray(msg.content)) {
64 for (const part of msg.content) {
65 if (part.type === "text") {
66 result.push({ type: "text", text: part.text })
67 }
68 }
69 }
70 }
71 return result
72 }
73
74 let inputTokens = 0
75 try {
76 inputTokens = await this.countTokens([{ type: "text", text: systemPrompt }, ...toContentBlocks(messages)])
77 } catch (err) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected