GeezerVison is showing github.com — fix(core): copy models.dev snapshot without structuredClone (#46710) · anomalyco/opencode@e327f93
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/models-dev.ts +21-2Lines changed: 21 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,7 @@ export const ModelsDevPlugin = define({ | 37 | 37 | }) | 38 | 38 | for (const model of provider.models) { | 39 | 39 | if (model.status === "deprecated") continue | 40 | | - catalog.model.update(provider.info.id, model.id, (draft) => Object.assign(draft, structuredClone(model))) | | 40 | + catalog.model.update(provider.info.id, model.id, (draft) => Object.assign(draft, copy(model))) | 41 | 41 | } | 42 | 42 | } | 43 | 43 | }) | @@ -65,8 +65,27 @@ function environmentNames(provider: ModelsDev.Snapshot) { | 65 | 65 | } | 66 | 66 |
| 67 | 67 | function snapshots(data: readonly ModelsDev.Snapshot[]) { | 68 | | - return structuredClone(data).filter( | | 68 | + return copy(data).filter( | 69 | 69 | // These deprecated aliases are replaced by the canonical Azure and Google Vertex providers. | 70 | 70 | (provider) => provider.info.id !== "azure-cognitive-services" && provider.info.id !== "google-vertex-anthropic", | 71 | 71 | ) | 72 | 72 | } | | 73 | + | | 74 | +// The catalog owns and mutates its model records, so every rebuild needs fresh copies of the | | 75 | +// thousands of snapshot models. Snapshot data is plain JSON, and a direct copy is an order of | | 76 | +// magnitude faster than structuredClone's general graph walk on the startup path. | | 77 | +function copy<T>(value: T): T { | | 78 | + if (Array.isArray(value)) return value.map(copy) as T | | 79 | + if (value !== null && typeof value === "object") { | | 80 | + const result: Record<string, unknown> = {} | | 81 | + for (const key of Object.keys(value)) { | | 82 | + const copied = copy((value as Record<string, unknown>)[key]) | | 83 | + // Assigning this key would set the prototype rather than an own property, unlike structuredClone. | | 84 | + if (key === "__proto__") | | 85 | + Object.defineProperty(result, key, { value: copied, enumerable: true, writable: true, configurable: true }) | | 86 | + else result[key] = copied | | 87 | + } | | 88 | + return result as T | | 89 | + } | | 90 | + return value | | 91 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/plugin/models-dev.test.ts +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -411,6 +411,60 @@ describe("ModelsDevPlugin", () => { | 411 | 411 | ), | 412 | 412 | ) | 413 | 413 |
| | 414 | + it.effect("copies model request bodies without reinterpreting literal __proto__ keys", () => | | 415 | + Effect.gen(function* () { | | 416 | + const integrations = yield* Integration.Service | | 417 | + const catalog = yield* Catalog.Service | | 418 | + const providerID = Provider.ID.make("acme") | | 419 | + const modelID = Model.ID.make("gpt-5.4") | | 420 | + // A JSON body may legitimately contain a "__proto__" key; both copy stages must keep it as an own property. | | 421 | + const body = JSON.parse('{"__proto__":{"service_tier":"priority"},"keep":true}') as Record<string, unknown> | | 422 | + const snapshot = { | | 423 | + info: { | | 424 | + id: providerID, | | 425 | + name: "Acme", | | 426 | + activation: "auto", | | 427 | + package: Provider.aisdk("@ai-sdk/openai-compatible"), | | 428 | + }, | | 429 | + environment: [], | | 430 | + models: [ | | 431 | + { | | 432 | + id: modelID, | | 433 | + modelID, | | 434 | + providerID, | | 435 | + name: "GPT-5.4", | | 436 | + capabilities: { tools: true, input: [], output: [] }, | | 437 | + variants: [], | | 438 | + time: { released: Date.parse("2026-01-01") }, | | 439 | + cost: [], | | 440 | + status: "active", | | 441 | + enabled: true, | | 442 | + limit: { context: 1_050_000, output: 128_000 }, | | 443 | + body, | | 444 | + }, | | 445 | + ], | | 446 | + } satisfies ModelsDev.Snapshot | | 447 | + yield* ModelsDevPlugin.effect( | | 448 | + host({ | | 449 | + catalog: catalogHost(catalog), | | 450 | + integration: integrationHost(integrations), | | 451 | + }), | | 452 | + ).pipe( | | 453 | + Effect.provideService( | | 454 | + ModelsDev.Service, | | 455 | + ModelsDev.Service.of({ get: () => Effect.succeed([snapshot]), refresh: () => Effect.void }), | | 456 | + ), | | 457 | + ) | | 458 | + | | 459 | + const copied = (yield* catalog.model.get(providerID, modelID))?.body | | 460 | + expect(copied).not.toBe(body) | | 461 | + expect(Object.hasOwn(copied ?? {}, "__proto__")).toBe(true) | | 462 | + expect(Object.keys(copied ?? {})).toEqual(["__proto__", "keep"]) | | 463 | + expect(JSON.stringify(copied)).toBe(JSON.stringify(body)) | | 464 | + expect(copied).not.toHaveProperty("service_tier") | | 465 | + }), | | 466 | + ) | | 467 | + | 414 | 468 | it.effect("omits legacy provider aliases", () => | 415 | 469 | Effect.gen(function* () { | 416 | 470 | const integrations = yield* Integration.Service | |
|