mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): send saved keys during local model discovery (#63266)
This commit is contained in:
committed by
GitHub
Unverified
parent
eb670f1e98
commit
f753100ad5
@@ -10,6 +10,8 @@ import { Location } from "../../location.js"
|
||||
import { Model } from "../../model.js"
|
||||
import type { PluginInternal } from "../internal.js"
|
||||
import { foldSettings } from "./configured.js"
|
||||
import { activeCredential } from "./credential-settings.js"
|
||||
import { ProviderRefresh } from "./refresh.js"
|
||||
|
||||
export type LocalModel = {
|
||||
readonly id: string
|
||||
@@ -53,10 +55,11 @@ export function createLocalProviderPlugin<Discovered>(input: {
|
||||
? undefined
|
||||
: input.origin)
|
||||
const source = { current: configured(yield* config.entries(), input.providerID, defaultOrigin, suffix) }
|
||||
const loaded = { models: [] as readonly Discovered[], hash: "[]" }
|
||||
const loaded = { models: [] as readonly Discovered[], connected: false, hash: "[]" }
|
||||
|
||||
yield* ctx.integration.transform((integrations) => {
|
||||
if (loaded.models.length === 0) return
|
||||
// A key connection lives on the integration (env keys only exist while it does), so keep it then.
|
||||
if (loaded.models.length === 0 || loaded.connected) return
|
||||
integrations.remove(input.providerID)
|
||||
})
|
||||
|
||||
@@ -74,7 +77,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
|
||||
provider: input.providerID,
|
||||
apiKey: source.current.apiKey ?? "",
|
||||
}
|
||||
provider.integrationID = undefined
|
||||
if (!loaded.connected) provider.integrationID = undefined
|
||||
})
|
||||
for (const item of loaded.models) {
|
||||
const spec = input.model(item)
|
||||
@@ -101,21 +104,21 @@ export function createLocalProviderPlugin<Discovered>(input: {
|
||||
if (!current.endpoint) return undefined
|
||||
return yield* discoveryLock.withPermit(
|
||||
Effect.gen(function* () {
|
||||
// A key saved with /connect or set in the integration's env var wins over config, as it does for requests.
|
||||
const active = yield* activeCredential(ctx, input.providerID)
|
||||
const connected = active.connection !== undefined
|
||||
const apiKey = active.credential?.type === "key" ? active.credential.key : current.apiKey
|
||||
const cached = discovery.get(current.root)
|
||||
if (
|
||||
cached &&
|
||||
cached.apiKey === current.apiKey &&
|
||||
Date.now() - cached.checked < Duration.toMillis(interval)
|
||||
)
|
||||
return { source: current, models: cached.models }
|
||||
if (cached && cached.apiKey === apiKey && Date.now() - cached.checked < Duration.toMillis(interval))
|
||||
return { source: current, connected, models: cached.models }
|
||||
discovery.set(current.root, {
|
||||
checked: Date.now(),
|
||||
apiKey: current.apiKey,
|
||||
models: cached && cached.apiKey === current.apiKey ? cached.models : undefined,
|
||||
apiKey,
|
||||
models: cached && cached.apiKey === apiKey ? cached.models : undefined,
|
||||
})
|
||||
const models = yield* input.discover(createClient(http, current.root, current.apiKey, current.endpoint))
|
||||
discovery.set(current.root, { checked: Date.now(), apiKey: current.apiKey, models })
|
||||
return { source: current, models }
|
||||
const models = yield* input.discover(createClient(http, current.root, apiKey, current.endpoint))
|
||||
discovery.set(current.root, { checked: Date.now(), apiKey, models })
|
||||
return { source: current, connected, models }
|
||||
}),
|
||||
)
|
||||
})
|
||||
@@ -123,9 +126,10 @@ export function createLocalProviderPlugin<Discovered>(input: {
|
||||
const refresh = Effect.fn("LocalProviderPlugin.refresh")(function* () {
|
||||
const result = yield* discover()
|
||||
if (!result?.models || result.source !== source.current) return
|
||||
const hash = JSON.stringify(result.models)
|
||||
const hash = JSON.stringify([result.connected, result.models])
|
||||
if (hash === loaded.hash) return
|
||||
loaded.models = result.models
|
||||
loaded.connected = result.connected
|
||||
loaded.hash = hash
|
||||
yield* ctx.integration.reload()
|
||||
yield* ctx.provider.reload()
|
||||
@@ -133,6 +137,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
|
||||
|
||||
// Keep the last successful inventory through transient outages instead of flickering model availability.
|
||||
yield* refresh().pipe(Effect.ignore, Effect.repeat(Schedule.spaced(interval)), Effect.forkScoped)
|
||||
yield* ProviderRefresh.onSwitch(input.providerID, refresh().pipe(Effect.ignore))
|
||||
const reload = Effect.fn("LocalProviderPlugin.reload")(function* () {
|
||||
const next = configured(yield* config.entries(), input.providerID, defaultOrigin, suffix)
|
||||
if (
|
||||
@@ -143,6 +148,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
|
||||
return
|
||||
source.current = next
|
||||
loaded.models = []
|
||||
loaded.connected = false
|
||||
loaded.hash = "[]"
|
||||
yield* ctx.integration.reload()
|
||||
yield* ctx.provider.reload()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Bus } from "@opencode/core/bus"
|
||||
import { Config } from "@opencode/core/config"
|
||||
import { Credential } from "@opencode/core/credential"
|
||||
import { Integration } from "@opencode/core/integration"
|
||||
import { Model } from "@opencode/core/model"
|
||||
import { Plugin } from "@opencode/core/plugin"
|
||||
@@ -245,6 +246,126 @@ describe("LMStudioPlugin", () => {
|
||||
10_000,
|
||||
)
|
||||
|
||||
it.live("discovers with a key saved through /connect as soon as it is saved", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const requests: Array<string | null> = []
|
||||
return {
|
||||
requests,
|
||||
server: Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
requests.push(request.headers.get("authorization"))
|
||||
if (request.headers.get("authorization") !== "Bearer saved-key")
|
||||
return Response.json({ error: { code: "invalid_api_key" } }, { status: 401 })
|
||||
return Response.json({
|
||||
models: [
|
||||
{
|
||||
type: "llm",
|
||||
key: "secured-model",
|
||||
display_name: "Secured",
|
||||
loaded_instances: [],
|
||||
max_context_length: 8192,
|
||||
},
|
||||
],
|
||||
})
|
||||
},
|
||||
}),
|
||||
}
|
||||
}),
|
||||
({ requests, server }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const integrations = yield* Integration.Service
|
||||
const modelState = yield* Model.Service
|
||||
const providerID = Provider.ID.make("lmstudio")
|
||||
yield* integrations.transform((editor) => {
|
||||
editor.update(Integration.ID.make("lmstudio"), (integration) => {
|
||||
integration.name = "LMStudio"
|
||||
})
|
||||
editor.method.update({ integrationID: Integration.ID.make("lmstudio"), method: { type: "key" } })
|
||||
})
|
||||
yield* addPlugin(server.url.origin)
|
||||
yield* eventually(
|
||||
Effect.sync(() => requests.length),
|
||||
(count) => count > 0,
|
||||
)
|
||||
expect(requests).toEqual([null])
|
||||
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("lmstudio"),
|
||||
value: Credential.Key.make({ type: "key", key: "saved-key" }),
|
||||
})
|
||||
yield* eventually(modelState.get(providerID, Model.ID.make("secured-model")), (model) => model !== undefined)
|
||||
expect(requests.at(-1)).toBe("Bearer saved-key")
|
||||
// The saved key stays listed under the integration, so /connect can still manage it.
|
||||
expect(yield* integrations.get(Integration.ID.make("lmstudio"))).toBeDefined()
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("keeps sending the integration's env key after models load", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
process.env.OPENCODE_TEST_LMSTUDIO_KEY = "env-key"
|
||||
const requests: Array<string | null> = []
|
||||
return {
|
||||
requests,
|
||||
server: Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
requests.push(request.headers.get("authorization"))
|
||||
if (request.headers.get("authorization") !== "Bearer env-key")
|
||||
return Response.json({ error: { code: "invalid_api_key" } }, { status: 401 })
|
||||
return Response.json({
|
||||
models: [
|
||||
{
|
||||
type: "llm",
|
||||
key: "secured-model",
|
||||
display_name: "Secured",
|
||||
loaded_instances: [],
|
||||
max_context_length: 8192,
|
||||
},
|
||||
],
|
||||
})
|
||||
},
|
||||
}),
|
||||
}
|
||||
}),
|
||||
({ requests, server }) =>
|
||||
Effect.gen(function* () {
|
||||
const integrations = yield* Integration.Service
|
||||
const modelState = yield* Model.Service
|
||||
yield* integrations.transform((editor) => {
|
||||
editor.update(Integration.ID.make("lmstudio"), (integration) => {
|
||||
integration.name = "LMStudio"
|
||||
})
|
||||
editor.method.update({
|
||||
integrationID: Integration.ID.make("lmstudio"),
|
||||
method: { type: "env", names: ["OPENCODE_TEST_LMSTUDIO_KEY"] },
|
||||
})
|
||||
})
|
||||
yield* addPlugin(server.url.origin, "5 millis")
|
||||
yield* eventually(
|
||||
modelState.get(Provider.ID.make("lmstudio"), Model.ID.make("secured-model")),
|
||||
(model) => model !== undefined,
|
||||
)
|
||||
yield* eventually(
|
||||
Effect.sync(() => requests.length),
|
||||
(count) => count >= 3,
|
||||
)
|
||||
|
||||
expect(requests.every((authorization) => authorization === "Bearer env-key")).toBe(true)
|
||||
expect(yield* integrations.get(Integration.ID.make("lmstudio"))).toBeDefined()
|
||||
}),
|
||||
({ server }) =>
|
||||
Effect.promise(() => server.stop(true)).pipe(
|
||||
Effect.ensuring(Effect.sync(() => delete process.env.OPENCODE_TEST_LMSTUDIO_KEY)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("shares discovery requests across plugin instances", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
|
||||
Reference in New Issue
Block a user