mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
perf(core): rebuild providers on account switch only when a plugin's state changed (#60787)
This commit is contained in:
committed by
GitHub
Unverified
parent
54988df055
commit
e469e85a29
@@ -2,7 +2,7 @@ import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/effect
|
||||
import type { Context } from "@opencode/plugin/effect/plugin"
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
// import { Deferred, Duration, Effect, Option, Schema, Semaphore, Stream } from "effect"
|
||||
import { Deferred, Effect, Option, Schema, Semaphore, Stream } from "effect"
|
||||
import { Deferred, Effect, Equal, Option, Schema, Semaphore, Stream } from "effect"
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import type { Server, ServerResponse } from "node:http"
|
||||
import { App } from "../../app.js"
|
||||
@@ -224,12 +224,14 @@ export const ChatGPTPlugin = define({
|
||||
? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined))
|
||||
: undefined
|
||||
chatgpt = credential?.type === "oauth" && credential.methodID === methodID ? credential : undefined
|
||||
const previous = source
|
||||
source = chatgpt ? connection : undefined
|
||||
// if (previous !== IntegrationConnection.key(source)) {
|
||||
// const metadata = Option.getOrUndefined(decodeMetadata(chatgpt?.metadata))
|
||||
// const stored = metadata ? yield* ctx.storage.get(modelCacheKey(metadata.clientID)) : undefined
|
||||
// available = Option.getOrUndefined(decodeCachedModels(stored))
|
||||
// }
|
||||
return !Equal.equals(previous, source)
|
||||
})
|
||||
|
||||
yield* ctx.integration.transform((editor) => {
|
||||
@@ -357,7 +359,7 @@ export const ChatGPTPlugin = define({
|
||||
// }),
|
||||
// )
|
||||
// })
|
||||
const reload = () => loading.withPermit(load().pipe(Effect.andThen(ctx.provider.reload())))
|
||||
const reload = () => loading.withPermit(load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void))))
|
||||
// .pipe(Effect.andThen(refreshModels().pipe(Effect.forkScoped, Effect.asVoid)))
|
||||
yield* bus.subscribe(Credential.Event.Switched).pipe(
|
||||
Stream.filter((event) => event.data.integrationID === integrationID),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import os from "os"
|
||||
import { App } from "../../app.js"
|
||||
import { Clock, Deferred, Effect, Exit, Option, Schema, Semaphore, Stream } from "effect"
|
||||
import { Clock, Deferred, Effect, Equal, Exit, Option, Schema, Semaphore, Stream } from "effect"
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import type { Server, ServerResponse } from "node:http"
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
@@ -71,9 +71,10 @@ export const GitLabPlugin = define({
|
||||
? yield* ctx.integration.connection.resolve(stored).pipe(Effect.orElseSucceed(() => undefined))
|
||||
: undefined
|
||||
if (!stored || !credential || credential.type === "external") {
|
||||
const changed = loaded.models !== undefined || loaded.connection !== undefined
|
||||
loaded.models = undefined
|
||||
loaded.connection = undefined
|
||||
return
|
||||
return changed
|
||||
}
|
||||
|
||||
const provider = yield* providers.get(providerID)
|
||||
@@ -118,9 +119,11 @@ export const GitLabPlugin = define({
|
||||
IntegrationConnection.key(connection) !==
|
||||
IntegrationConnection.key(yield* ctx.integration.connection.active(integrationID))
|
||||
)
|
||||
return
|
||||
return false
|
||||
const changed = !Equal.equals(remote?.models, loaded.models) || !Equal.equals(connection, loaded.connection)
|
||||
loaded.models = remote?.models
|
||||
loaded.connection = connection
|
||||
return changed
|
||||
})
|
||||
|
||||
yield* ctx.integration.transform((editor) => {
|
||||
@@ -291,7 +294,7 @@ export const GitLabPlugin = define({
|
||||
],
|
||||
})
|
||||
})
|
||||
const refresh = () => loading.withPermit(load().pipe(Effect.andThen(ctx.provider.reload())))
|
||||
const refresh = () => loading.withPermit(load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void))))
|
||||
yield* bus.subscribe(Credential.Event.Switched).pipe(
|
||||
Stream.filter((event) => event.data.integrationID === integrationID),
|
||||
Stream.runForEach(refresh),
|
||||
|
||||
@@ -25,7 +25,9 @@ export const OpenAIContextCapsPlugin = define({
|
||||
const credential = connection
|
||||
? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined))
|
||||
: undefined
|
||||
const previous = signedIn
|
||||
signedIn = credential?.type === "oauth" && methodIDs.has(credential.methodID)
|
||||
return signedIn !== previous
|
||||
})
|
||||
|
||||
yield* load()
|
||||
@@ -41,7 +43,7 @@ export const OpenAIContextCapsPlugin = define({
|
||||
})
|
||||
yield* bus.subscribe(Credential.Event.Switched).pipe(
|
||||
Stream.filter((event) => event.data.integrationID === integrationID),
|
||||
Stream.runForEach(() => load().pipe(Effect.andThen(ctx.provider.reload()))),
|
||||
Stream.runForEach(() => load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
}),
|
||||
|
||||
@@ -243,11 +243,16 @@ export const OpenAIPlugin = define({
|
||||
const credential = connection
|
||||
? yield* ctx.integration.connection.resolve(connection).pipe(Effect.orElseSucceed(() => undefined))
|
||||
: undefined
|
||||
const previous = chatgpt
|
||||
chatgpt =
|
||||
credential?.type === "oauth" &&
|
||||
(credential.methodID === browserMethodID || credential.methodID === headlessMethodID)
|
||||
? credential
|
||||
: undefined
|
||||
return (
|
||||
(previous === undefined) !== (chatgpt === undefined) ||
|
||||
previous?.metadata?.accountID !== chatgpt?.metadata?.accountID
|
||||
)
|
||||
})
|
||||
|
||||
yield* ctx.integration.transform((editor) => {
|
||||
@@ -326,7 +331,7 @@ export const OpenAIPlugin = define({
|
||||
})
|
||||
for (const name of ["context", "compaction"] as const)
|
||||
yield* ctx.session.hook(name, omitOutputLimit, { providerID: Provider.ID.openai })
|
||||
const refresh = () => loading.withPermit(load().pipe(Effect.andThen(ctx.provider.reload())))
|
||||
const refresh = () => loading.withPermit(load().pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void))))
|
||||
yield* bus.subscribe(Credential.Event.Switched).pipe(
|
||||
Stream.filter((event) => event.data.integrationID === Integration.ID.make("openai")),
|
||||
Stream.runForEach(refresh),
|
||||
|
||||
@@ -172,7 +172,9 @@ export const SnowflakeCortexPlugin = define({
|
||||
|
||||
const load = Effect.gen(function* () {
|
||||
const value = (yield* saved)?.value
|
||||
const previous = account.value
|
||||
account.value = normalizeAccount(process.env.SNOWFLAKE_ACCOUNT ?? value?.metadata?.account ?? configured?.account)
|
||||
return account.value !== previous
|
||||
})
|
||||
yield* load
|
||||
yield* ctx.provider.transform((providers) => {
|
||||
@@ -201,7 +203,7 @@ export const SnowflakeCortexPlugin = define({
|
||||
})
|
||||
yield* bus.subscribe(Credential.Event.Switched).pipe(
|
||||
Stream.filter((event) => event.data.integrationID === integrationID),
|
||||
Stream.runForEach(() => load.pipe(Effect.andThen(ctx.provider.reload()))),
|
||||
Stream.runForEach(() => load.pipe(Effect.flatMap((changed) => (changed ? ctx.provider.reload() : Effect.void)))),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
yield* ctx.session.hook(
|
||||
|
||||
@@ -19,6 +19,7 @@ import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { SessionModelRequest } from "@opencode/core/session/model-request"
|
||||
import { SessionModelTransport } from "@opencode/core/session/model-transport"
|
||||
import { SessionRunnerModel } from "@opencode/core/session/runner/model"
|
||||
import { drain } from "../lib/clock"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { PluginTestLayer } from "./fixture"
|
||||
|
||||
@@ -243,6 +244,51 @@ describe("OpenAIPlugin", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("reloads providers on a switch only when the ChatGPT login changes", () =>
|
||||
Effect.gen(function* () {
|
||||
const catalog = yield* Provider.Service
|
||||
const credentials = yield* Credential.Service
|
||||
yield* catalog.transform((catalog) => {
|
||||
catalog.update(Provider.ID.openai, () => {})
|
||||
})
|
||||
const first = yield* credentials.create({
|
||||
integrationID: Integration.ID.make("openai"),
|
||||
value: Credential.Key.make({ type: "key", key: "sk-first" }),
|
||||
})
|
||||
yield* addPlugin()
|
||||
const folds = { count: 0 }
|
||||
yield* catalog.transform(() => {
|
||||
folds.count++
|
||||
})
|
||||
const before = folds.count
|
||||
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("openai"),
|
||||
value: Credential.Key.make({ type: "key", key: "sk-second" }),
|
||||
})
|
||||
yield* credentials.activate(first.id)
|
||||
yield* drain
|
||||
expect(folds.count).toBe(before)
|
||||
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("openai"),
|
||||
value: Credential.OAuth.make({
|
||||
type: "oauth",
|
||||
methodID: Integration.MethodID.make("chatgpt-browser"),
|
||||
access: "chatgpt-token",
|
||||
refresh: "refresh",
|
||||
expires: Date.now() + 60_000,
|
||||
metadata: { accountID: "acct_123" },
|
||||
}),
|
||||
})
|
||||
yield* drain
|
||||
expect(folds.count).toBeGreaterThan(before)
|
||||
expect(required(yield* catalog.get(Provider.ID.openai)).headers).toMatchObject({
|
||||
"chatgpt-account-id": "acct_123",
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("omits the default output limit from OpenAI steps and compaction", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* addPlugin()
|
||||
|
||||
Reference in New Issue
Block a user