Files
caikn 0daf0c7421 sap-aicore 0.3.0: a new provider for SAP AI Core's GPT Responses and Claude Messages deployments (caikn, #651)
A new provider package, @magpie-community/opencode-sap-aicore-auth 0.3.0
(packages/sap-aicore). It signs in from a local SAP AI Core client-secret
credential file, discovers the account's running GPT (Responses) and Claude
(Messages, Bedrock) deployments per resource group, and sends requests only to
https://*.authentication.*.hana.ondemand.com (token) and
https://api.ai.*.hana.ondemand.com (inference). Image input is gated per model
on the catalog's inputTypes. Also: the root README row, a registry.json entry,
.DS_Store in .gitignore, and scripts/check.mjs accepts an empty static model
list for a package whose provider.models hook is a function with the same id
(npm and the loader are still required).

Reviewed at head b44b3e1b68889c2a12bcaa37ad5d546a398bdde0, with main 9bba5fed
merged in locally, on a Mac mini (macOS, Bun 1.4.2), no real SAP account:
- bun test packages/sap-aicore scripts: 351 pass, 0 fail.
- bun scripts/check.mjs: every package ok, sap-aicore included.
- The two tests that reached the network at e955194 now use inputs root()
  really rejects (/oauth, /v1), and a new test stubs fetch and checks that
  /oauth/token and /v2 are accepted and the token is posted to
  <authUrl>/oauth/token once. plugin.test.js fails on any unstubbed fetch.
- Whole package under a preload that records every non-loopback fetch, connect
  and DNS lookup: 0 attempts (the preload caught a deliberate fetch to
  example.invalid, so it does record).
- Broken by hand: putting the e955194 inputs back turns the test red through
  the new guard; making root() keep the given path turns the new
  /oauth/token + /v2 test red; letting root() accept any path turns 3 URL
  tests red.
- Not tried: a real SAP AI Core account (the author's run record covers one
  GPT and one Claude turn, plain and with a tool call; no image turn).

The first version goes out through the dist branch (npm doesn't have the name).
2026-11-04 22:19:43 +08:00

158 lines
8.4 KiB
JavaScript

import { test, afterEach, jest } from "bun:test"
import assert from "node:assert/strict"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import definition from "../opencode2.mjs"
const cleanups = []
afterEach(async () => {
for (const fn of cleanups.splice(0).reverse()) await fn()
jest.useRealTimers()
})
const ROOT = "https://api.ai.eu10.hana.ondemand.com"
const GPT = "gpt-6-astra"
const OPUS = "claude-opus-4-8"
const OPUS_SAP = "anthropic--claude-4.8-opus"
const TOKEN = "synthetic-opencode2-token"
function context() {
const methods = []
const transforms = []
const sdkHooks = []
const stored = new Map()
const events = []
let connection
let credential
let wake
let refreshed
const modelsRefreshed = new Promise(resolve => { refreshed = resolve })
const ctx = {
location: { directory: os.tmpdir() },
integration: {
transform: async transform => transform({ method: { update: method => methods.push(method) } }),
connection: {
active: async () => connection,
resolve: async () => credential,
},
},
provider: {
transform: async transform => transforms.push(transform),
reload: async () => refreshed(),
},
model: { reload: async () => {} },
aisdk: { hook: async (name, callback, filter) => sdkHooks.push({ name, callback, filter }) },
storage: { get: async key => stored.get(key), set: async (key, value) => stored.set(key, value) },
event: {
async *subscribe() {
for (;;) {
while (events.length) yield events.shift()
await new Promise(resolve => { wake = resolve })
}
},
},
}
const providers = () => {
const result = []
for (const transform of transforms) transform({ add: provider => result.push(provider) })
return result
}
const emit = event => { events.push(event); wake?.() }
return {
ctx, methods, providers, sdkHooks, modelsRefreshed, emit,
use(value) {
connection = { type: "credential", id: "synthetic-account" }
credential = value
emit({ type: "credential.switched", data: { integrationID: "sap-aicore" } })
},
}
}
function backend() {
const original = globalThis.fetch
cleanups.push(() => { globalThis.fetch = original })
const calls = []
const json = value => new Response(JSON.stringify(value), { headers: { "content-type": "application/json" } })
const models = [
{ id: "gpt", name: GPT, version: "2026-09-03", executable: "azure-openai" },
{ id: "opus", name: OPUS_SAP, version: "1", executable: "aws-bedrock" },
{ id: "old-gpt", name: "gpt-5.9", version: "1", executable: "azure-openai" },
]
globalThis.fetch = async (input, init) => {
const url = new URL(String(input))
if (url.pathname === "/oauth/token") return json({ access_token: TOKEN, token_type: "bearer", expires_in: 3600 })
if (url.pathname === "/v2/lm/deployments") return json({ resources: url.searchParams.get("$skip") === "0" ? models.map(model => ({
id: model.id, status: "RUNNING", deploymentUrl: `${ROOT}/v2/inference/deployments/${model.id}`, configurationId: `cfg-${model.id}`,
details: { resources: { backendDetails: { model: { name: model.name, version: model.version } } } },
})) : [] })
if (url.pathname.startsWith("/v2/lm/configurations/")) {
const model = models.find(model => url.pathname === `/v2/lm/configurations/cfg-${model.id}`)
assert.ok(model, `unexpected configuration request ${url}`)
return json({ id: `cfg-${model.id}`, scenarioId: "foundation-models", executableId: model.executable, parameterBindings: [{ key: "modelName", value: model.name }, { key: "modelVersion", value: model.version }] })
}
if (url.pathname === "/v2/lm/scenarios/foundation-models/models") return json({ count: models.length, resources: models.map(model => ({
model: model.name, executableId: model.executable, ...(model.executable === "aws-bedrock" ? { provider: "Anthropic", displayName: "Claude 4.8 Opus" } : {}),
allowedScenarios: [{ scenarioId: "foundation-models", executableId: model.executable }],
versions: [{ name: model.version, contextLength: 1000000, inputTypes: ["text", "image"], streamingSupported: true }],
})) })
calls.push({ url: url.href, headers: Object.fromEntries(new Headers(init.headers)), body: JSON.parse(init.body) })
if (url.pathname === "/v2/inference/deployments/gpt/responses") return json({ id: "response-gpt", object: "response", model: GPT, status: "completed", output: [{ id: "message-gpt", type: "message", role: "assistant", status: "completed", content: [{ type: "output_text", text: "GPT_BRIDGE_OK", annotations: [] }] }], error: null, incomplete_details: null, usage: { input_tokens: 4, output_tokens: 3, total_tokens: 7 } })
if (url.pathname === "/v2/inference/deployments/opus/invoke") return json({ id: "message-opus", type: "message", role: "assistant", model: OPUS, content: [{ type: "text", text: "CLAUDE_BRIDGE_OK" }], stop_reason: "end_turn", stop_sequence: null, usage: { input_tokens: 4, output_tokens: 3 } })
throw new Error(`unexpected inference request ${url}`)
}
return calls
}
test("OpenCode 2 setup keeps the unsigned SAP catalog empty and does not fetch credentials", async () => {
const original = globalThis.fetch
cleanups.push(() => { globalThis.fetch = original })
let requests = 0
globalThis.fetch = async () => { requests++; throw new Error("unsigned setup must not request credentials") }
const host = context()
const stop = await definition.setup(host.ctx)
cleanups.push(() => { stop(); host.emit({}) })
assert.deepEqual(host.providers().map(provider => [provider.info.id, provider.models]), [["sap-aicore", []]])
const signIn = host.methods.find(method => method.integrationID === "sap-aicore")
assert.ok(signIn)
assert.equal(signIn.method.form.find(field => field.key === "credentialFile").required, true)
assert.equal(requests, 0)
})
test("OpenCode 2 credential-file sign-in discovers both families and routes their SDK requests safely", async () => {
const calls = backend()
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "magpie-opencode2-"))
cleanups.push(() => fs.rmSync(dir, { recursive: true, force: true }))
const credentialFile = path.join(dir, "credentials.json")
fs.writeFileSync(credentialFile, JSON.stringify({ authUrl: "https://synthetic.authentication.eu10.hana.ondemand.com", baseUrl: ROOT + "/v2", clientId: "synthetic-client", clientSecret: "synthetic-client-secret", resourceGroup: "bridge-group" }), { mode: 0o600 })
const host = context()
const stop = await definition.setup(host.ctx)
cleanups.push(() => { stop(); host.emit({}) })
const method = host.methods.find(method => method.integrationID === "sap-aicore")
const signIn = await method.authorize({ credentialFile })
host.use(await signIn.callback)
await host.modelsRefreshed
const provider = host.providers()[0]
assert.deepEqual(provider.models.map(model => model.id).sort(), [OPUS, GPT])
assert.deepEqual(provider.models.find(model => model.id === OPUS).capabilities.input, ["text", "image"])
assert.equal(provider.models.find(model => model.id === OPUS).package, "aisdk:@ai-sdk/anthropic@ai-v6")
const options = { baseURL: provider.info.settings.baseURL }
await host.sdkHooks.find(hook => hook.name === "sdk" && hook.filter.providerID === "sap-aicore").callback({ options })
const request = (route, body) => options.fetch(options.baseURL + route, { method: "POST", headers: { authorization: `Bearer ${options.apiKey}`, "x-api-key": "synthetic-caller-key" }, body: JSON.stringify(body) })
const gpt = await request("/responses", { model: GPT, input: "hello", store: false })
assert.equal(gpt.status, 200)
assert.equal((await gpt.json()).output[0].content[0].text, "GPT_BRIDGE_OK")
const claude = await request("/messages", { model: OPUS, messages: [{ role: "user", content: "hello" }], max_tokens: 64 })
assert.equal(claude.status, 200)
assert.equal((await claude.json()).content[0].text, "CLAUDE_BRIDGE_OK")
assert.deepEqual(calls.map(call => call.url), [`${ROOT}/v2/inference/deployments/gpt/responses`, `${ROOT}/v2/inference/deployments/opus/invoke`])
for (const call of calls) {
assert.equal(call.headers.authorization, `Bearer ${TOKEN}`)
assert.equal(call.headers["ai-resource-group"], "bridge-group")
assert.equal(call.headers["x-api-key"], undefined)
}
assert.equal(calls[1].body.anthropic_version, "bedrock-2023-05-31")
assert.equal(calls[1].body.model, undefined)
assert.equal(calls[1].body.stream, undefined)
}, 15000)