Skip to content

Commit f753100

Browse files
authored
fix(core): send saved keys during local model discovery (#63266)
1 parent eb670f1 commit f753100

2 files changed

Lines changed: 142 additions & 15 deletions

File tree

‎packages/core/src/plugin/provider/local.ts‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { Location } from "../../location.js"
1010
import { Model } from "../../model.js"
1111
import type { PluginInternal } from "../internal.js"
1212
import { foldSettings } from "./configured.js"
13+
import { activeCredential } from "./credential-settings.js"
14+
import { ProviderRefresh } from "./refresh.js"
1315

1416
export type LocalModel = {
1517
readonly id: string
@@ -53,10 +55,11 @@ export function createLocalProviderPlugin<Discovered>(input: {
5355
? undefined
5456
: input.origin)
5557
const source = { current: configured(yield* config.entries(), input.providerID, defaultOrigin, suffix) }
56-
const loaded = { models: [] as readonly Discovered[], hash: "[]" }
58+
const loaded = { models: [] as readonly Discovered[], connected: false, hash: "[]" }
5759

5860
yield* ctx.integration.transform((integrations) => {
59-
if (loaded.models.length === 0) return
61+
// A key connection lives on the integration (env keys only exist while it does), so keep it then.
62+
if (loaded.models.length === 0 || loaded.connected) return
6063
integrations.remove(input.providerID)
6164
})
6265

@@ -74,7 +77,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
7477
provider: input.providerID,
7578
apiKey: source.current.apiKey ?? "",
7679
}
77-
provider.integrationID = undefined
80+
if (!loaded.connected) provider.integrationID = undefined
7881
})
7982
for (const item of loaded.models) {
8083
const spec = input.model(item)
@@ -101,38 +104,40 @@ export function createLocalProviderPlugin<Discovered>(input: {
101104
if (!current.endpoint) return undefined
102105
return yield* discoveryLock.withPermit(
103106
Effect.gen(function* () {
107+
// A key saved with /connect or set in the integration's env var wins over config, as it does for requests.
108+
const active = yield* activeCredential(ctx, input.providerID)
109+
const connected = active.connection !== undefined
110+
const apiKey = active.credential?.type === "key" ? active.credential.key : current.apiKey
104111
const cached = discovery.get(current.root)
105-
if (
106-
cached &&
107-
cached.apiKey === current.apiKey &&
108-
Date.now() - cached.checked < Duration.toMillis(interval)
109-
)
110-
return { source: current, models: cached.models }
112+
if (cached && cached.apiKey === apiKey && Date.now() - cached.checked < Duration.toMillis(interval))
113+
return { source: current, connected, models: cached.models }
111114
discovery.set(current.root, {
112115
checked: Date.now(),
113-
apiKey: current.apiKey,
114-
models: cached && cached.apiKey === current.apiKey ? cached.models : undefined,
116+
apiKey,
117+
models: cached && cached.apiKey === apiKey ? cached.models : undefined,
115118
})
116-
const models = yield* input.discover(createClient(http, current.root, current.apiKey, current.endpoint))
117-
discovery.set(current.root, { checked: Date.now(), apiKey: current.apiKey, models })
118-
return { source: current, models }
119+
const models = yield* input.discover(createClient(http, current.root, apiKey, current.endpoint))
120+
discovery.set(current.root, { checked: Date.now(), apiKey, models })
121+
return { source: current, connected, models }
119122
}),
120123
)
121124
})
122125

123126
const refresh = Effect.fn("LocalProviderPlugin.refresh")(function* () {
124127
const result = yield* discover()
125128
if (!result?.models || result.source !== source.current) return
126-
const hash = JSON.stringify(result.models)
129+
const hash = JSON.stringify([result.connected, result.models])
127130
if (hash === loaded.hash) return
128131
loaded.models = result.models
132+
loaded.connected = result.connected
129133
loaded.hash = hash
130134
yield* ctx.integration.reload()
131135
yield* ctx.provider.reload()
132136
})
133137

134138
// Keep the last successful inventory through transient outages instead of flickering model availability.
135139
yield* refresh().pipe(Effect.ignore, Effect.repeat(Schedule.spaced(interval)), Effect.forkScoped)
140+
yield* ProviderRefresh.onSwitch(input.providerID, refresh().pipe(Effect.ignore))
136141
const reload = Effect.fn("LocalProviderPlugin.reload")(function* () {
137142
const next = configured(yield* config.entries(), input.providerID, defaultOrigin, suffix)
138143
if (
@@ -143,6 +148,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
143148
return
144149
source.current = next
145150
loaded.models = []
151+
loaded.connected = false
146152
loaded.hash = "[]"
147153
yield* ctx.integration.reload()
148154
yield* ctx.provider.reload()

‎packages/core/test/plugin/provider-lmstudio.test.ts‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Bus } from "@opencode/core/bus"
22
import { Config } from "@opencode/core/config"
3+
import { Credential } from "@opencode/core/credential"
34
import { Integration } from "@opencode/core/integration"
45
import { Model } from "@opencode/core/model"
56
import { Plugin } from "@opencode/core/plugin"
@@ -245,6 +246,126 @@ describe("LMStudioPlugin", () => {
245246
10_000,
246247
)
247248

249+
it.live("discovers with a key saved through /connect as soon as it is saved", () =>
250+
Effect.acquireUseRelease(
251+
Effect.sync(() => {
252+
const requests: Array<string | null> = []
253+
return {
254+
requests,
255+
server: Bun.serve({
256+
port: 0,
257+
fetch: (request) => {
258+
requests.push(request.headers.get("authorization"))
259+
if (request.headers.get("authorization") !== "Bearer saved-key")
260+
return Response.json({ error: { code: "invalid_api_key" } }, { status: 401 })
261+
return Response.json({
262+
models: [
263+
{
264+
type: "llm",
265+
key: "secured-model",
266+
display_name: "Secured",
267+
loaded_instances: [],
268+
max_context_length: 8192,
269+
},
270+
],
271+
})
272+
},
273+
}),
274+
}
275+
}),
276+
({ requests, server }) =>
277+
Effect.gen(function* () {
278+
const credentials = yield* Credential.Service
279+
const integrations = yield* Integration.Service
280+
const modelState = yield* Model.Service
281+
const providerID = Provider.ID.make("lmstudio")
282+
yield* integrations.transform((editor) => {
283+
editor.update(Integration.ID.make("lmstudio"), (integration) => {
284+
integration.name = "LMStudio"
285+
})
286+
editor.method.update({ integrationID: Integration.ID.make("lmstudio"), method: { type: "key" } })
287+
})
288+
yield* addPlugin(server.url.origin)
289+
yield* eventually(
290+
Effect.sync(() => requests.length),
291+
(count) => count > 0,
292+
)
293+
expect(requests).toEqual([null])
294+
295+
yield* credentials.create({
296+
integrationID: Integration.ID.make("lmstudio"),
297+
value: Credential.Key.make({ type: "key", key: "saved-key" }),
298+
})
299+
yield* eventually(modelState.get(providerID, Model.ID.make("secured-model")), (model) => model !== undefined)
300+
expect(requests.at(-1)).toBe("Bearer saved-key")
301+
// The saved key stays listed under the integration, so /connect can still manage it.
302+
expect(yield* integrations.get(Integration.ID.make("lmstudio"))).toBeDefined()
303+
}),
304+
({ server }) => Effect.promise(() => server.stop(true)),
305+
),
306+
)
307+
308+
it.live("keeps sending the integration's env key after models load", () =>
309+
Effect.acquireUseRelease(
310+
Effect.sync(() => {
311+
process.env.OPENCODE_TEST_LMSTUDIO_KEY = "env-key"
312+
const requests: Array<string | null> = []
313+
return {
314+
requests,
315+
server: Bun.serve({
316+
port: 0,
317+
fetch: (request) => {
318+
requests.push(request.headers.get("authorization"))
319+
if (request.headers.get("authorization") !== "Bearer env-key")
320+
return Response.json({ error: { code: "invalid_api_key" } }, { status: 401 })
321+
return Response.json({
322+
models: [
323+
{
324+
type: "llm",
325+
key: "secured-model",
326+
display_name: "Secured",
327+
loaded_instances: [],
328+
max_context_length: 8192,
329+
},
330+
],
331+
})
332+
},
333+
}),
334+
}
335+
}),
336+
({ requests, server }) =>
337+
Effect.gen(function* () {
338+
const integrations = yield* Integration.Service
339+
const modelState = yield* Model.Service
340+
yield* integrations.transform((editor) => {
341+
editor.update(Integration.ID.make("lmstudio"), (integration) => {
342+
integration.name = "LMStudio"
343+
})
344+
editor.method.update({
345+
integrationID: Integration.ID.make("lmstudio"),
346+
method: { type: "env", names: ["OPENCODE_TEST_LMSTUDIO_KEY"] },
347+
})
348+
})
349+
yield* addPlugin(server.url.origin, "5 millis")
350+
yield* eventually(
351+
modelState.get(Provider.ID.make("lmstudio"), Model.ID.make("secured-model")),
352+
(model) => model !== undefined,
353+
)
354+
yield* eventually(
355+
Effect.sync(() => requests.length),
356+
(count) => count >= 3,
357+
)
358+
359+
expect(requests.every((authorization) => authorization === "Bearer env-key")).toBe(true)
360+
expect(yield* integrations.get(Integration.ID.make("lmstudio"))).toBeDefined()
361+
}),
362+
({ server }) =>
363+
Effect.promise(() => server.stop(true)).pipe(
364+
Effect.ensuring(Effect.sync(() => delete process.env.OPENCODE_TEST_LMSTUDIO_KEY)),
365+
),
366+
),
367+
)
368+
248369
it.live("shares discovery requests across plugin instances", () =>
249370
Effect.acquireUseRelease(
250371
Effect.sync(() => {

0 commit comments

Comments
 (0)