Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/opencode/src/provider/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const OUTPUT_TOKEN_MAX = 32_000
// branch that requests it stays in lockstep.
const INCLUDE_ENCRYPTED_REASONING = ["reasoning.encrypted_content"] as const

const SAP_AI_CORE_PACKAGE = "@jerome-benoit/sap-ai-provider"
const LEGACY_SAP_AI_CORE_PACKAGE = "@jerome-benoit/sap-ai-provider-v2"

export function sanitizeSurrogates(content: string) {
return content.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "\uFFFD")
}
Expand Down Expand Up @@ -1180,7 +1183,8 @@ export function variants(model: Provider.Model): Record<string, Record<string, a
// https://v5.ai-sdk.dev/providers/ai-sdk-providers/perplexity
return {}

case "@jerome-benoit/sap-ai-provider-v2": {
case SAP_AI_CORE_PACKAGE:
case LEGACY_SAP_AI_CORE_PACKAGE: {
if (id.includes("anthropic")) {
if (adaptiveEfforts) {
// Bedrock adaptive splits `effort` out into `output_config` (vs Anthropic
Expand Down Expand Up @@ -1825,7 +1829,8 @@ function reasoningEffort(model: Provider.Model, effort: string) {
return { reasoningEffort: effort, reasoningSummary: "auto", include: INCLUDE_ENCRYPTED_REASONING }
case "@ai-sdk/azure":
return { reasoningEffort: effort, reasoningSummary: "auto", include: INCLUDE_ENCRYPTED_REASONING }
case "@jerome-benoit/sap-ai-provider-v2":
case SAP_AI_CORE_PACKAGE:
case LEGACY_SAP_AI_CORE_PACKAGE:
if (model.id.includes("anthropic"))
return { modelParams: { thinking: { type: "adaptive", display: "summarized" }, output_config: { effort } } }
return { modelParams: { reasoning_effort: effort } }
Expand Down Expand Up @@ -1896,7 +1901,8 @@ function reasoningBudget(model: Provider.Model, budget: number) {
return { thinking: { type: "enabled", tokenBudget: budget } }
case "@ai-sdk/alibaba":
return { enableThinking: true, thinkingBudget: budget }
case "@jerome-benoit/sap-ai-provider-v2":
case SAP_AI_CORE_PACKAGE:
case LEGACY_SAP_AI_CORE_PACKAGE:
if (model.id.includes("anthropic"))
return { modelParams: { thinking: { type: "enabled", budget_tokens: budget } } }
if (model.id.includes("gemini"))
Expand Down
44 changes: 44 additions & 0 deletions packages/opencode/test/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModelsDev } from "@opencode-ai/core/models-dev"
import { FSUtil } from "@opencode-ai/core/fs-util"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { Global } from "@opencode-ai/core/global"
import { Npm } from "@opencode-ai/core/npm"
import { disposeAllInstances, provideInstanceEffect, tmpdirScoped, TestInstance } from "../fixture/fixture"
import { markPluginDependenciesReady } from "../fixture/plugin"
import { Auth } from "@/auth"
Expand Down Expand Up @@ -314,6 +315,49 @@ it.instance("getModel returns model for valid provider/model", () =>
}),
)

it.instance(
"keeps the existing SAP V3 root instead of switching to the V2 facade",
Effect.gen(function* () {
const name = "@jerome-benoit/sap-ai-provider"
// The test preload isolates the npm cache and removes it after the run.
for (const [specifier, hasV2] of [
[name, false],
[`${name}@^5.0.0`, true],
] as const) {
const directory = path.join(Global.Path.cache, "packages", Npm.sanitize(specifier), "node_modules", name)
yield* Effect.promise(async () => {
await Bun.write(
path.join(directory, "package.json"),
JSON.stringify({ name, exports: hasV2 ? { ".": "./index.cjs", "./v2": "./v2.cjs" } : "./index.cjs" }),
)
for (const [file, version] of hasV2 ? [["index.cjs", "v3"], ["v2.cjs", "v2"]] : [["index.cjs", "v3"]]) {
await Bun.write(
path.join(directory, file),
`const model = () => ({ specificationVersion: "${version}" })
exports.createSAPAIProvider = () => Object.assign(model, { languageModel: model })`,
)
}
})
}

const provider = yield* Provider.Service
const model = yield* provider.getModel(ProviderV2.ID.make("sap-ai-core"), ModelV2.ID.make("gpt-5"))
const language = yield* provider.getLanguage(model)
expect(language).toMatchObject({ specificationVersion: "v3" })
}),
{
config: {
provider: {
"sap-ai-core": {
npm: "@jerome-benoit/sap-ai-provider",
models: { "gpt-5": { name: "GPT-5" } },
options: { apiKey: "test-key" },
},
},
},
},
)

it.instance("getModel throws ModelNotFoundError for invalid model", () =>
Effect.gen(function* () {
yield* set("ANTHROPIC_API_KEY", "test-api-key")
Expand Down
22 changes: 20 additions & 2 deletions packages/opencode/test/provider/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3944,6 +3944,24 @@ describe("ProviderTransform.reasoningVariants", () => {
)
})

test.each(["@jerome-benoit/sap-ai-provider-v2", "@jerome-benoit/sap-ai-provider"])(
"converts SAP AI Core catalog effort and budget for %s",
(npm) => {
expect(
ProviderTransform.reasoningVariants(model([{ type: "effort", values: ["high"] }]), target(npm, "gpt-5")),
).toEqual({ high: { modelParams: { reasoning_effort: "high" } } })
expect(
ProviderTransform.reasoningVariants(
model([{ type: "budget_tokens", min: 0, max: 16_000 }]),
target(npm, "gemini-2.5-flash"),
),
).toEqual({
high: { modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: 8_000 } } },
max: { modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } } },
})
},
)

test.each(["luna", "sol", "terra"])("serializes Bedrock GPT-5.6 %s none effort", async (name) => {
const item = target("@ai-sdk/amazon-bedrock", `global.openai.gpt-5.6-${name}`)
const variants = ProviderTransform.reasoningVariants(
Expand Down Expand Up @@ -5874,15 +5892,15 @@ describe("ProviderTransform.variants", () => {
})
})

describe("@jerome-benoit/sap-ai-provider-v2", () => {
describe.each(["@jerome-benoit/sap-ai-provider-v2", "@jerome-benoit/sap-ai-provider"])("%s", (npm) => {
const sapModel = (apiId: string, releaseDate = "2024-01-01") =>
createMockModel({
id: `sap-ai-core/${apiId}`,
providerID: "sap-ai-core",
api: {
id: apiId,
url: "https://api.ai.sap",
npm: "@jerome-benoit/sap-ai-provider-v2",
npm,
},
release_date: releaseDate,
})
Expand Down
Loading