|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Cause, Effect, Exit, Layer, Schema } from "effect" |
| 3 | +import { Catalog } from "@opencode-ai/core/catalog" |
| 4 | +import { Config } from "@opencode-ai/core/config" |
| 5 | +import { ConfigProviderPlugin } from "@opencode-ai/core/config/plugin/provider" |
| 6 | +import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder" |
| 7 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 8 | +import { EventV2 } from "@opencode-ai/core/event" |
| 9 | +import { Integration } from "@opencode-ai/core/integration" |
| 10 | +import { Location } from "@opencode-ai/core/location" |
| 11 | +import { ModelV2 } from "@opencode-ai/core/model" |
| 12 | +import { ModelsDev } from "@opencode-ai/core/models-dev" |
| 13 | +import { ModelsDevPlugin } from "@opencode-ai/core/plugin/models-dev" |
| 14 | +import { ProviderV2 } from "@opencode-ai/core/provider" |
| 15 | +import { AbsolutePath } from "@opencode-ai/core/schema" |
| 16 | +import { location } from "../fixture/location" |
| 17 | +import { testEffect } from "../lib/effect" |
| 18 | +import { catalogHost, host, integrationHost } from "../plugin/host" |
| 19 | + |
| 20 | +const it = testEffect( |
| 21 | + AppNodeBuilder.build(LayerNode.group([Catalog.node, Integration.node, EventV2.node]), [ |
| 22 | + [ |
| 23 | + Location.node, |
| 24 | + Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) }))), |
| 25 | + ], |
| 26 | + ]), |
| 27 | +) |
| 28 | + |
| 29 | +const providerID = ProviderV2.ID.make("minimax") |
| 30 | +const modelID = ModelV2.ID.make("MiniMax-M2") |
| 31 | + |
| 32 | +// The models.dev entry for minimax: an Anthropic-protocol SDK bound to an Anthropic-protocol host. |
| 33 | +const catalogData = { |
| 34 | + minimax: { |
| 35 | + id: "minimax", |
| 36 | + name: "MiniMax", |
| 37 | + env: ["MINIMAX_API_KEY"], |
| 38 | + npm: "@ai-sdk/anthropic", |
| 39 | + api: "https://api.minimax.io/anthropic/v1", |
| 40 | + models: { |
| 41 | + "MiniMax-M2": { |
| 42 | + id: "MiniMax-M2", |
| 43 | + name: "MiniMax M2", |
| 44 | + release_date: "2025-10-01", |
| 45 | + attachment: false, |
| 46 | + reasoning: true, |
| 47 | + temperature: true, |
| 48 | + tool_call: true, |
| 49 | + limit: { context: 200_000, output: 128_000 }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | +} satisfies Record<string, ModelsDev.Provider> |
| 54 | + |
| 55 | +const decode = Schema.decodeUnknownSync(Config.Info) |
| 56 | + |
| 57 | +const resolve = Effect.fn(function* (info: unknown) { |
| 58 | + const catalog = yield* Catalog.Service |
| 59 | + const integrations = yield* Integration.Service |
| 60 | + const context = host({ catalog: catalogHost(catalog), integration: integrationHost(integrations) }) |
| 61 | + yield* ModelsDevPlugin.effect(context).pipe( |
| 62 | + Effect.provideService( |
| 63 | + ModelsDev.Service, |
| 64 | + ModelsDev.Service.of({ get: () => Effect.succeed(catalogData), refresh: () => Effect.void }), |
| 65 | + ), |
| 66 | + ) |
| 67 | + yield* ConfigProviderPlugin.Plugin.effect(context).pipe( |
| 68 | + Effect.provideService( |
| 69 | + Config.Service, |
| 70 | + Config.Service.of({ |
| 71 | + entries: () => Effect.succeed([new Config.Document({ type: "document", info: decode(info) })]), |
| 72 | + }), |
| 73 | + ), |
| 74 | + ) |
| 75 | + return yield* catalog.model.get(providerID, modelID) |
| 76 | +}) |
| 77 | + |
| 78 | +describe("ConfigProviderPlugin npm coherence", () => { |
| 79 | + it.effect("rejects a provider block whose npm disagrees with the catalog package", () => |
| 80 | + Effect.gen(function* () { |
| 81 | + const exit = yield* resolve({ |
| 82 | + providers: { |
| 83 | + minimax: { |
| 84 | + npm: "@ai-sdk/openai-compatible", |
| 85 | + request: { body: { baseURL: "https://api.minimax.chat/v1" } }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }).pipe(Effect.exit) |
| 89 | + |
| 90 | + // Without the check the package stays "@ai-sdk/anthropic" while the url becomes the configured |
| 91 | + // host, so the request goes to https://api.minimax.chat/v1/messages, which exists nowhere. |
| 92 | + expect(Exit.isFailure(exit)).toBe(true) |
| 93 | + const message = Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : "" |
| 94 | + expect(message).toContain('Provider "minimax"') |
| 95 | + expect(message).toContain('npm "@ai-sdk/openai-compatible"') |
| 96 | + expect(message).toContain('resolved package is "@ai-sdk/anthropic"') |
| 97 | + expect(message).toContain("https://api.minimax.chat/v1") |
| 98 | + expect(message).toContain("providers.minimax.api") |
| 99 | + }), |
| 100 | + ) |
| 101 | + |
| 102 | + it.effect("rejects the legacy npm and options pair instead of dropping both", () => |
| 103 | + Effect.gen(function* () { |
| 104 | + const exit = yield* resolve({ |
| 105 | + providers: { |
| 106 | + minimax: { npm: "@ai-sdk/openai-compatible", options: { baseURL: "https://api.minimax.chat/v1" } }, |
| 107 | + }, |
| 108 | + }).pipe(Effect.exit) |
| 109 | + |
| 110 | + expect(Exit.isFailure(exit)).toBe(true) |
| 111 | + const message = Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : "" |
| 112 | + expect(message).toContain('npm "@ai-sdk/openai-compatible"') |
| 113 | + expect(message).toContain('resolved package is "@ai-sdk/anthropic"') |
| 114 | + }), |
| 115 | + ) |
| 116 | + |
| 117 | + it.effect("accepts npm that agrees with the resolved package", () => |
| 118 | + Effect.gen(function* () { |
| 119 | + const model = yield* resolve({ |
| 120 | + providers: { minimax: { npm: "@ai-sdk/anthropic" } }, |
| 121 | + }) |
| 122 | + |
| 123 | + expect(model?.api).toMatchObject({ |
| 124 | + type: "aisdk", |
| 125 | + package: "@ai-sdk/anthropic", |
| 126 | + url: "https://api.minimax.io/anthropic/v1", |
| 127 | + }) |
| 128 | + }), |
| 129 | + ) |
| 130 | + |
| 131 | + it.effect("keeps retargeting the catalog package at another host when npm is absent", () => |
| 132 | + Effect.gen(function* () { |
| 133 | + const model = yield* resolve({ |
| 134 | + providers: { minimax: { request: { body: { baseURL: "https://minimax.internal/anthropic/v1" } } } }, |
| 135 | + }) |
| 136 | + |
| 137 | + expect(model?.api).toMatchObject({ |
| 138 | + type: "aisdk", |
| 139 | + package: "@ai-sdk/anthropic", |
| 140 | + url: "https://minimax.internal/anthropic/v1", |
| 141 | + }) |
| 142 | + }), |
| 143 | + ) |
| 144 | +}) |
0 commit comments