Skip to content

Commit 8887707

Browse files
christineyan4Copilot
authored andcommitted
Fix LM Studio wizard prompter binding
Bind LM Studio setup prompter methods before storing them as callbacks so the gateway wizard class prompter keeps its receiver. Co-authored-by: Copilot <[email protected]>
1 parent 0543126 commit 8887707

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎extensions/lmstudio/src/setup.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,54 @@ function createQueuedWizardPrompterHarness(textValues: string[]): {
182182
return { prompter, note, text };
183183
}
184184

185+
function createMethodBoundWizardPrompterHarness(textValues: string[]): {
186+
prompter: WizardPrompter;
187+
note: ReturnType<typeof vi.fn>;
188+
text: ReturnType<typeof vi.fn>;
189+
} {
190+
const queue = [...textValues];
191+
const note = vi.fn(async (_message: string, _title?: string) => {});
192+
const text = vi.fn(async () => queue.shift() ?? "");
193+
194+
class MethodBoundWizardPrompter implements WizardPrompter {
195+
async intro() {}
196+
async outro() {}
197+
async note(message: string, title?: string) {
198+
await this.recordNote(message, title);
199+
}
200+
async select<T>(params: { options: Array<{ value: T }> }) {
201+
const firstOption = params.options[0];
202+
if (!firstOption) {
203+
throw new Error("select called without options");
204+
}
205+
return firstOption.value;
206+
}
207+
async multiselect() {
208+
return [];
209+
}
210+
async text() {
211+
return await this.readText();
212+
}
213+
async confirm() {
214+
return false;
215+
}
216+
progress() {
217+
return {
218+
update: () => {},
219+
stop: () => {},
220+
};
221+
}
222+
private async readText() {
223+
return await text();
224+
}
225+
private async recordNote(message: string, title?: string) {
226+
await note(message, title);
227+
}
228+
}
229+
230+
return { prompter: new MethodBoundWizardPrompter(), note, text };
231+
}
232+
185233
function isRecord(value: unknown): value is Record<string, unknown> {
186234
return typeof value === "object" && value !== null && !Array.isArray(value);
187235
}
@@ -798,6 +846,43 @@ describe("lmstudio setup", () => {
798846
});
799847
});
800848

849+
it("interactive setup preserves gateway wizard prompter method binding", async () => {
850+
const { prompter, text } = createMethodBoundWizardPrompterHarness([
851+
"http://localhost:1234/api/v1/",
852+
"lmstudio-test-key",
853+
"4096",
854+
]);
855+
856+
const result = await promptAndConfigureLmstudioInteractive({
857+
config: buildConfig(),
858+
prompter,
859+
});
860+
861+
expect(text).toHaveBeenCalledTimes(3);
862+
expect(result.defaultModel).toBe("lmstudio/qwen3-8b-instruct");
863+
});
864+
865+
it("interactive setup preserves gateway wizard note binding on discovery failure", async () => {
866+
fetchLmstudioModelsMock.mockResolvedValueOnce({ reachable: false, models: [] });
867+
const { prompter, note, text } = createMethodBoundWizardPrompterHarness([
868+
"http://localhost:1234/api/v1/",
869+
"lmstudio-test-key",
870+
]);
871+
872+
await expect(
873+
promptAndConfigureLmstudioInteractive({
874+
config: buildConfig(),
875+
prompter,
876+
}),
877+
).rejects.toThrow("LM Studio not reachable");
878+
879+
expect(text).toHaveBeenCalledTimes(2);
880+
expect(note).toHaveBeenCalledWith(
881+
"LM Studio could not be reached at http://localhost:1234/v1.\nStart LM Studio (or run lms server start) and re-run setup.",
882+
"LM Studio",
883+
);
884+
});
885+
801886
it("interactive setup accepts a blank API key for unauthenticated local LM Studio", async () => {
802887
const { prompter, text } = createQueuedWizardPrompterHarness([
803888
"http://localhost:1234/api/v1/",

‎extensions/lmstudio/src/setup.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,13 @@ export async function promptAndConfigureLmstudioInteractive(params: {
390390
promptText?: ProviderPromptText;
391391
note?: ProviderPromptNote;
392392
}): Promise<ProviderAuthResult> {
393-
const promptText = params.prompter?.text ?? params.promptText;
393+
const promptText = params.prompter
394+
? params.prompter.text.bind(params.prompter)
395+
: params.promptText;
394396
if (!promptText) {
395397
throw new Error("LM Studio interactive setup requires a text prompter.");
396398
}
397-
const note = params.prompter?.note ?? params.note;
399+
const note = params.prompter ? params.prompter.note.bind(params.prompter) : params.note;
398400
const defaultBaseUrl = resolveLmstudioSetupDefaultBaseUrl();
399401
const baseUrlRaw = await promptText({
400402
message: `${LMSTUDIO_PROVIDER_LABEL} base URL`,

0 commit comments

Comments
 (0)