mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(client): skip catalog refetches for unloaded locations (#59099)
Co-authored-by: Kit Langton <kit.langton@gmail.com>
This commit is contained in:
parent
91b5a7c25d
commit
cfb44b5b33
3 changed files with 171 additions and 52 deletions
|
|
@ -1241,26 +1241,29 @@ export function createData(config: CreateDataInput) {
|
|||
const location = { directory: ref[0], workspaceID: ref[1] ?? undefined }
|
||||
refreshUsageLimits(location)
|
||||
if (event.type === "credential.updated") {
|
||||
result.location.integration.invalidate(location)
|
||||
refresh(() => result.location.integration.sync(location))
|
||||
refreshLocation("integration", result.location.integration, location)
|
||||
return
|
||||
}
|
||||
setStore("location", key, (data) => ({
|
||||
integration: data?.integration?.map((integration) => {
|
||||
if (integration.id !== event.data.integrationID) return integration
|
||||
const active = integration.connections.find(
|
||||
(connection) => connection.type === "credential" && connection.id === event.data.credentialID,
|
||||
)
|
||||
if (!active) return integration
|
||||
return {
|
||||
...integration,
|
||||
connections: [active, ...integration.connections.filter((connection) => connection !== active)],
|
||||
}
|
||||
}),
|
||||
}))
|
||||
if (store.location[key]?.integration)
|
||||
setStore("location", key, (data) => ({
|
||||
integration: data?.integration?.map((integration) => {
|
||||
if (integration.id !== event.data.integrationID) return integration
|
||||
const active = integration.connections.find(
|
||||
(connection) => connection.type === "credential" && connection.id === event.data.credentialID,
|
||||
)
|
||||
if (!active) return integration
|
||||
return {
|
||||
...integration,
|
||||
connections: [active, ...integration.connections.filter((connection) => connection !== active)],
|
||||
}
|
||||
}),
|
||||
}))
|
||||
const active =
|
||||
hasLocation("integration", location) || hasLocation("model", location) || hasLocation("provider", location)
|
||||
result.location.model.invalidate(location)
|
||||
result.location.provider.invalidate(location)
|
||||
refresh(() => Promise.all([result.location.model.sync(location), result.location.provider.sync(location)]))
|
||||
if (active)
|
||||
refresh(() => Promise.all([result.location.model.sync(location), result.location.provider.sync(location)]))
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
@ -1269,30 +1272,24 @@ export function createData(config: CreateDataInput) {
|
|||
const location = event.location
|
||||
switch (event.type) {
|
||||
case "provider.updated":
|
||||
result.location.provider.invalidate(location)
|
||||
refresh(() => result.location.provider.sync(location))
|
||||
refreshLocation("provider", result.location.provider, location)
|
||||
break
|
||||
case "model.updated":
|
||||
result.location.model.invalidate(location)
|
||||
refresh(() => result.location.model.sync(location))
|
||||
refreshLocation("model", result.location.model, location)
|
||||
break
|
||||
case "agent.updated":
|
||||
result.location.agent.invalidate(location)
|
||||
refresh(() => result.location.agent.sync(location))
|
||||
refreshLocation("agent", result.location.agent, location)
|
||||
break
|
||||
case "command.updated":
|
||||
result.location.command.invalidate(location)
|
||||
refresh(() => result.location.command.sync(location))
|
||||
refreshLocation("command", result.location.command, location)
|
||||
break
|
||||
case "skill.updated":
|
||||
result.location.skill.invalidate(location)
|
||||
refresh(() => result.location.skill.sync(location))
|
||||
refreshLocation("skill", result.location.skill, location)
|
||||
break
|
||||
case "vcs.branch.updated":
|
||||
// The event names only the current branch. Reload the rest: the default branch and provider can change with
|
||||
// it, and a read served before the location's VCS provider loaded has neither.
|
||||
result.location.vcs.invalidate(location)
|
||||
refresh(() => result.location.vcs.sync(location))
|
||||
refreshLocation("vcs", result.location.vcs, location)
|
||||
break
|
||||
case "form.created":
|
||||
if (store.session.form[event.data.form.sessionID]?.some((form) => form.id === event.data.form.id)) break
|
||||
|
|
@ -1311,50 +1308,68 @@ export function createData(config: CreateDataInput) {
|
|||
break
|
||||
case "shell.exited":
|
||||
case "shell.deleted":
|
||||
if (!store.location[locationKey(location)]?.shell?.[event.data.id]) break
|
||||
setStore("location", locationKey(location), (data) => ({
|
||||
shell: Object.fromEntries(Object.entries(data?.shell ?? {}).filter(([id]) => id !== event.data.id)),
|
||||
}))
|
||||
break
|
||||
case "reference.updated":
|
||||
result.location.reference.invalidate(location)
|
||||
refresh(() => result.location.reference.sync(location))
|
||||
refreshLocation("reference", result.location.reference, location)
|
||||
break
|
||||
case "integration.updated":
|
||||
case "integration.updated": {
|
||||
refreshUsageLimits(location)
|
||||
const active =
|
||||
hasLocation("integration", location) || hasLocation("model", location) || hasLocation("provider", location)
|
||||
result.location.integration.invalidate(location)
|
||||
result.location.model.invalidate(location)
|
||||
result.location.provider.invalidate(location)
|
||||
refresh(() =>
|
||||
Promise.all([
|
||||
result.location.integration.sync(location),
|
||||
result.location.model.sync(location),
|
||||
result.location.provider.sync(location),
|
||||
]),
|
||||
)
|
||||
if (active)
|
||||
refresh(() =>
|
||||
Promise.all([
|
||||
result.location.integration.sync(location),
|
||||
result.location.model.sync(location),
|
||||
result.location.provider.sync(location),
|
||||
]),
|
||||
)
|
||||
break
|
||||
}
|
||||
case "config.updated":
|
||||
result.location.config.invalidate(location)
|
||||
if (result.location.config.list(location) !== undefined || sync.has(`location.config:${locationKey(location)}`))
|
||||
refresh(() => result.location.config.sync(location))
|
||||
refresh(() => result.location.websearch.refresh(location))
|
||||
refreshLocation("config", result.location.config, location)
|
||||
if (hasLocation("websearch", location)) refresh(() => result.location.websearch.refresh(location))
|
||||
break
|
||||
case "websearch.updated":
|
||||
refresh(() => result.location.websearch.refresh(location))
|
||||
if (hasLocation("websearch", location)) refresh(() => result.location.websearch.refresh(location))
|
||||
break
|
||||
// Authenticating an MCP integration reconnects its server, which emits mcp.status.changed,
|
||||
// so the mcp list syncs here rather than off integration.updated. The server emits one event
|
||||
// per MCP server as each settles, so a location booting nine servers emitted nine refetches.
|
||||
case "mcp.status.changed":
|
||||
case "mcp.status.changed": {
|
||||
const active = hasLocation("mcpServer", location)
|
||||
result.location.mcp.server.invalidate(location)
|
||||
settle(`mcp.status:${locationKey(location)}`, () => result.location.mcp.server.sync(location))
|
||||
if (active) settle(`mcp.status:${locationKey(location)}`, () => result.location.mcp.server.sync(location))
|
||||
break
|
||||
}
|
||||
case "mcp.resources.changed":
|
||||
result.location.mcp.resource.invalidate(location)
|
||||
refresh(() => result.location.mcp.resource.sync(location))
|
||||
refreshLocation("mcpResource", result.location.mcp.resource, location)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
function hasLocation<Field extends keyof LocationData>(field: Field, ref?: LocationRef) {
|
||||
const id = locationKey(ref ?? defaultLocation())
|
||||
return store.location[id]?.[field] !== undefined || sync.has(`location.${field}:${id}`)
|
||||
}
|
||||
|
||||
function refreshLocation<Field extends keyof LocationData>(
|
||||
field: Field,
|
||||
resource: { invalidate: (ref?: LocationRef) => void; sync: (ref?: LocationRef) => Promise<void> },
|
||||
location: LocationRef,
|
||||
) {
|
||||
const active = hasLocation(field, location)
|
||||
resource.invalidate(location)
|
||||
if (active) refresh(() => resource.sync(location))
|
||||
}
|
||||
|
||||
// A cached per-location catalog. `sync` loads once per invalidation, keyed by the
|
||||
// effective location, and publishes under the server's canonical location; `alias`
|
||||
// also publishes under the requested key when the two differ.
|
||||
|
|
|
|||
|
|
@ -695,6 +695,108 @@ for (const resource of ["provider", "model"] as const) {
|
|||
})
|
||||
}
|
||||
|
||||
test("ignores catalog updates for unloaded locations while refreshing loaded locations", async () => {
|
||||
const listeners = new Set<Parameters<CreateDataInput["event"]["listen"]>[0]>()
|
||||
const requests: Array<[string, string | null]> = []
|
||||
const current = { version: "v1" }
|
||||
const api = OpenCode.make({
|
||||
baseUrl: "http://opencode.local",
|
||||
fetch: async (input, init) => {
|
||||
const request = input instanceof Request ? input : new Request(input, init)
|
||||
const url = new URL(request.url)
|
||||
const directory = url.searchParams.get("location[directory]") ?? "/project"
|
||||
requests.push([url.pathname, directory])
|
||||
const location = { directory, project: { id: directory, directory, canonical: directory } }
|
||||
if (url.pathname === "/api/vcs")
|
||||
return Response.json({ location, data: { branch: { current: "main", default: "main" } } })
|
||||
if (url.pathname === "/api/provider")
|
||||
return Response.json({
|
||||
location,
|
||||
data: [{ id: "company", name: current.version, activation: "enabled", package: "aisdk:@ai-sdk/openai" }],
|
||||
})
|
||||
if (url.pathname === "/api/model") return Response.json({ location, data: [modelInfo(current.version)] })
|
||||
if (url.pathname === "/api/command")
|
||||
return Response.json({
|
||||
location,
|
||||
data: [{ name: current.version, source: "command", template: "run", hints: [] }],
|
||||
})
|
||||
if (url.pathname === "/api/agent")
|
||||
return Response.json({
|
||||
location,
|
||||
data: [
|
||||
{
|
||||
id: current.version,
|
||||
name: current.version,
|
||||
mode: "primary",
|
||||
hidden: false,
|
||||
request: { headers: {}, body: {} },
|
||||
permissions: [],
|
||||
},
|
||||
],
|
||||
})
|
||||
throw new Error(`Unexpected request: ${request.url}`)
|
||||
},
|
||||
})
|
||||
const setup = createRoot((dispose) => ({
|
||||
data: createData({
|
||||
api: () => api,
|
||||
directory: "/project",
|
||||
event: {
|
||||
on: () => () => {},
|
||||
listen(handler) {
|
||||
listeners.add(handler)
|
||||
return () => listeners.delete(handler)
|
||||
},
|
||||
},
|
||||
connection: { status: () => "connected" },
|
||||
}),
|
||||
dispose,
|
||||
}))
|
||||
const emit = (details: OpenCodeEvent) => listeners.forEach((listener) => listener({ name: details.type, details }))
|
||||
const loaded = { directory: "/project" }
|
||||
const vcsOnly = { directory: "/vcs-only" }
|
||||
const unloaded = { directory: "/unloaded" }
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
setup.data.location.provider.sync(loaded),
|
||||
setup.data.location.model.sync(loaded),
|
||||
setup.data.location.command.sync(loaded),
|
||||
setup.data.location.agent.sync(loaded),
|
||||
setup.data.location.vcs.sync(vcsOnly),
|
||||
])
|
||||
requests.length = 0
|
||||
current.version = "v2"
|
||||
|
||||
for (const location of [vcsOnly, unloaded]) {
|
||||
for (const type of ["provider.updated", "model.updated", "command.updated", "agent.updated"] as const) {
|
||||
emit({ id: `evt_${type}_${location.directory}`, created: 1, type, location, data: {} })
|
||||
}
|
||||
}
|
||||
await Bun.sleep(20)
|
||||
expect(requests).toEqual([])
|
||||
|
||||
for (const type of ["provider.updated", "model.updated", "command.updated", "agent.updated"] as const) {
|
||||
emit({ id: `evt_${type}_loaded`, created: 2, type, location: loaded, data: {} })
|
||||
}
|
||||
await wait(
|
||||
() =>
|
||||
setup.data.location.provider.list(loaded)?.[0]?.name === "v2" &&
|
||||
setup.data.location.model.list(loaded)?.[0]?.name === "v2" &&
|
||||
setup.data.location.command.list(loaded)?.[0]?.name === "v2" &&
|
||||
setup.data.location.agent.list(loaded)?.[0]?.id === "v2",
|
||||
)
|
||||
expect(requests).toEqual([
|
||||
["/api/provider", "/project"],
|
||||
["/api/model", "/project"],
|
||||
["/api/command", "/project"],
|
||||
["/api/agent", "/project"],
|
||||
])
|
||||
} finally {
|
||||
setup.dispose()
|
||||
}
|
||||
})
|
||||
|
||||
test("revalidates model discovery when an update overtakes an in-flight model list", async () => {
|
||||
const started = Promise.withResolvers<void>()
|
||||
const release = Promise.withResolvers<void>()
|
||||
|
|
|
|||
|
|
@ -122,11 +122,11 @@ test.each(["reconnecting", "disposed"] as const)("background reads respect %s ow
|
|||
const listeners = new Set<Parameters<CreateDataInput["event"]["listen"]>[0]>()
|
||||
const pending = Promise.withResolvers<Response>()
|
||||
const errors: unknown[] = []
|
||||
const state = { requests: 0 }
|
||||
const state = { requests: 0, status: "connected" as "connected" | "reconnecting" }
|
||||
const api = OpenCode.make({
|
||||
baseUrl: "http://opencode.local",
|
||||
fetch: () => {
|
||||
state.requests++
|
||||
if (state.requests++ === 0) return Promise.resolve(Response.json({ location: { directory: "/project" }, data: [] }))
|
||||
return pending.promise
|
||||
},
|
||||
})
|
||||
|
|
@ -141,11 +141,13 @@ test.each(["reconnecting", "disposed"] as const)("background reads respect %s ow
|
|||
return () => listeners.delete(handler)
|
||||
},
|
||||
},
|
||||
connection: { status: () => (mode === "reconnecting" ? "reconnecting" : "connected") },
|
||||
connection: { status: () => state.status },
|
||||
onError: (error) => errors.push(error),
|
||||
}),
|
||||
dispose,
|
||||
}))
|
||||
await setup.data.location.command.sync()
|
||||
state.status = mode === "reconnecting" ? "reconnecting" : "connected"
|
||||
const event: OpenCodeEvent = {
|
||||
id: "evt_refresh_owner",
|
||||
type: "command.updated",
|
||||
|
|
@ -154,7 +156,7 @@ test.each(["reconnecting", "disposed"] as const)("background reads respect %s ow
|
|||
}
|
||||
listeners.forEach((listener) => listener({ name: event.type, details: event }))
|
||||
if (mode === "reconnecting") {
|
||||
expect(state.requests).toBe(0)
|
||||
expect(state.requests).toBe(1)
|
||||
setup.dispose()
|
||||
return
|
||||
}
|
||||
|
|
@ -162,7 +164,7 @@ test.each(["reconnecting", "disposed"] as const)("background reads respect %s ow
|
|||
setup.dispose()
|
||||
pending.reject(new TypeError("Failed to fetch"))
|
||||
await expect(joined).rejects.toThrow("Transport")
|
||||
expect(state.requests).toBe(1)
|
||||
expect(state.requests).toBe(2)
|
||||
expect(errors).toEqual([])
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue