mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
refactor(core): remove the import cycle between provider and aisdk-native (#62883)
Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,65 @@
|
||||
export * as AISDKNative from "./aisdk-native.js"
|
||||
|
||||
import { Effect, Option, Schema, Struct } from "effect"
|
||||
import { Provider } from "./provider.js"
|
||||
import { isRecord } from "@opencode/util/record"
|
||||
import type { Provider } from "./provider.js"
|
||||
|
||||
export const AISDK_PREFIX = "aisdk:"
|
||||
export const isAISDK = (value: string | undefined): value is string => value?.startsWith(AISDK_PREFIX) ?? false
|
||||
export const aisdk = (value: string) => (isAISDK(value) ? value : `${AISDK_PREFIX}${value}`)
|
||||
export function packageName(value: string): string
|
||||
export function packageName(value: undefined): undefined
|
||||
export function packageName(value: string | undefined): string | undefined
|
||||
export function packageName(value: string | undefined) {
|
||||
// Native provider entrypoints can persist in user configuration across the npm scope migration.
|
||||
if (value?.startsWith("@opencode-ai/ai/")) return value.replace("@opencode-ai/", "@opencode/")
|
||||
if (value === undefined || !isAISDK(value)) return value
|
||||
return value.slice(AISDK_PREFIX.length)
|
||||
}
|
||||
|
||||
type Json = Schema.Schema.Type<typeof Schema.Json>
|
||||
const JsonRecord = Schema.Record(Schema.String, Schema.Json)
|
||||
const decodeJsonRecord = Schema.decodeUnknownSync(JsonRecord)
|
||||
|
||||
export function mergeOverlay(
|
||||
base: Readonly<Record<string, unknown>> | undefined,
|
||||
overlay: Readonly<Record<string, unknown>> | undefined,
|
||||
): Record<string, Json> | undefined {
|
||||
if (base === undefined) return overlay && decodeJsonRecord({ ...overlay })
|
||||
if (overlay === undefined) return decodeJsonRecord({ ...base })
|
||||
return decodeJsonRecord(mergeRecord(base, overlay))
|
||||
}
|
||||
|
||||
function mergeRecord(
|
||||
base: Readonly<Record<string, unknown>>,
|
||||
overlay: Readonly<Record<string, unknown>>,
|
||||
): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
new Set([...Object.keys(base), ...Object.keys(overlay)]).values().map((key): [string, unknown] => {
|
||||
const left = base[key]
|
||||
const right = overlay[key]
|
||||
if (right === undefined) return [key, left]
|
||||
if (isRecord(left) && isRecord(right)) return [key, mergeRecord(left, right)]
|
||||
return [key, right]
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
export function mergeHeaders(
|
||||
base: Readonly<Record<string, string>> | undefined,
|
||||
overlay: Readonly<Record<string, string>> | undefined,
|
||||
) {
|
||||
if (base === undefined) return overlay && { ...overlay }
|
||||
if (overlay === undefined) return { ...base }
|
||||
return Object.fromEntries(
|
||||
[...Object.entries(base), ...Object.entries(overlay)]
|
||||
.reduce((result, entry) => {
|
||||
result.set(entry[0].toLowerCase(), entry)
|
||||
return result
|
||||
}, new Map<string, [string, string]>())
|
||||
.values(),
|
||||
)
|
||||
}
|
||||
|
||||
type Overlays = {
|
||||
settings?: Provider.Settings
|
||||
@@ -23,7 +81,7 @@ type Context = {
|
||||
|
||||
export function rewrite<ID extends string>(
|
||||
target: Target<ID>,
|
||||
input: Context & { readonly specifier: string | undefined },
|
||||
input: Context & { readonly specifier: string | undefined; readonly builtins: ReadonlySet<string> },
|
||||
) {
|
||||
if (input.specifier === undefined) return
|
||||
const settings = decode(target.settings ?? {})
|
||||
@@ -33,16 +91,16 @@ export function rewrite<ID extends string>(
|
||||
if (target.package !== undefined || replacement !== input.specifier) target.package = replacement
|
||||
// Without `provider`, the native compatible package reports every AI SDK-mapped host as `openai-compatible`.
|
||||
const identity =
|
||||
replacement === "@opencode/ai/providers/openai-compatible" && !Provider.builtinPackages.has(input.specifier)
|
||||
replacement === "@opencode/ai/providers/openai-compatible" && !input.builtins.has(input.specifier)
|
||||
? { provider: input.canonical ?? input.providerID }
|
||||
: {}
|
||||
target.settings = target.settings === undefined ? undefined : { ...translated.settings, ...identity }
|
||||
target.headers = Provider.mergeHeaders(translated.headers, target.headers)
|
||||
target.body = Provider.mergeOverlay(translated.body, target.body)
|
||||
target.headers = mergeHeaders(translated.headers, target.headers)
|
||||
target.body = mergeOverlay(translated.body, target.body)
|
||||
target.variants = target.variants?.map((variant) => {
|
||||
const overlay = options(replacement, input.modelID, decode(variant.settings ?? {}))
|
||||
const headers = Provider.mergeHeaders(overlay.headers, variant.headers)
|
||||
const body = Provider.mergeOverlay(overlay.body, variant.body)
|
||||
const headers = mergeHeaders(overlay.headers, variant.headers)
|
||||
const body = mergeOverlay(overlay.body, variant.body)
|
||||
return {
|
||||
id: variant.id,
|
||||
...(variant.settings === undefined ? {} : { settings: overlay.settings }),
|
||||
@@ -144,10 +202,12 @@ const LEGACY_VERTEX: Readonly<Record<string, string>> = {
|
||||
"@opencode/ai/providers/google-vertex-mistral": "@opencode/ai/providers/google-vertex/mistral",
|
||||
}
|
||||
|
||||
function resolve(specifier: string, context: Context & { readonly settings?: Provider.Settings }): string | undefined {
|
||||
const npm = Provider.packageName(specifier)
|
||||
if (Provider.isAISDK(specifier) || npm in PACKAGES || npm in (HOSTS[context.providerID] ?? {}))
|
||||
return native(npm, context)
|
||||
function resolve(
|
||||
specifier: string,
|
||||
context: Context & { readonly settings?: Provider.Settings; readonly builtins: ReadonlySet<string> },
|
||||
): string | undefined {
|
||||
const npm = packageName(specifier)
|
||||
if (isAISDK(specifier) || npm in PACKAGES || npm in (HOSTS[context.providerID] ?? {})) return native(npm, context)
|
||||
if (npm === "@opencode/ai/providers/amazon-bedrock/mantle" || npm === "@opencode/ai/providers/amazon-bedrock-mantle")
|
||||
return mantle(context.modelID)
|
||||
if (npm in LEGACY_VERTEX) return LEGACY_VERTEX[npm]
|
||||
@@ -156,7 +216,7 @@ function resolve(specifier: string, context: Context & { readonly settings?: Pro
|
||||
azureChat(context)
|
||||
)
|
||||
return "@opencode/ai/providers/azure/chat"
|
||||
return Provider.builtinPackages.has(npm) ? npm : undefined
|
||||
return context.builtins.has(npm) ? npm : undefined
|
||||
}
|
||||
|
||||
type Overlay = {
|
||||
@@ -236,7 +296,7 @@ function bedrockRequest(modelID: string | undefined, settings: Legacy): Pick<Ove
|
||||
const budget = reasoning?.budgetTokens
|
||||
const display = reasoning?.display
|
||||
const betas = settings.anthropicBeta ?? []
|
||||
const fields = Provider.mergeOverlay(additional, {
|
||||
const fields = mergeOverlay(additional, {
|
||||
...(betas.length > 0 ? { anthropic_beta: [...(additional.anthropic_beta ?? []), ...betas] } : {}),
|
||||
...(anthropic && type === "adaptive"
|
||||
? { thinking: { type: "adaptive", ...(display === undefined ? {} : { display }) } }
|
||||
@@ -269,7 +329,7 @@ const OPENROUTER_KEYS = ["appName", "appUrl", "api_keys", "compatibility"] as co
|
||||
|
||||
function openRouterRequest(settings: Legacy): Pick<Overlay, "headers"> {
|
||||
const headers =
|
||||
Provider.mergeHeaders(
|
||||
mergeHeaders(
|
||||
{
|
||||
...(settings.appName === undefined ? {} : { "X-OpenRouter-Title": settings.appName }),
|
||||
...(settings.appUrl === undefined ? {} : { "HTTP-Referer": settings.appUrl }),
|
||||
|
||||
@@ -145,6 +145,7 @@ const layer = Layer.effect(
|
||||
model.providerID = providerID
|
||||
const provider = input?.records.get(providerID)?.provider
|
||||
AISDKNative.rewrite(model, {
|
||||
builtins: Provider.builtinPackages,
|
||||
specifier: model.package ?? provider?.package,
|
||||
providerID,
|
||||
canonical: model.canonical ?? provider?.canonical,
|
||||
|
||||
@@ -5,7 +5,6 @@ import { Provider } from "@opencode/schema/provider"
|
||||
import { Model } from "@opencode/schema/model"
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import { HttpTimeout, type ProviderPackage } from "@opencode/ai/effect"
|
||||
import { isRecord } from "@opencode/util/record"
|
||||
import { Npm } from "@opencode/util/npm"
|
||||
import type { DeepMutable } from "./schema.js"
|
||||
import { importModule, resolveModule } from "@opencode/util/runtime-import"
|
||||
@@ -18,26 +17,11 @@ import { Location } from "./location.js"
|
||||
import { freeze } from "immer"
|
||||
import { AISDKNative } from "./aisdk-native.js"
|
||||
|
||||
export { AISDK_PREFIX, aisdk, isAISDK, mergeHeaders, mergeOverlay, packageName } from "./aisdk-native.js"
|
||||
|
||||
export const ID = Provider.ID
|
||||
export type ID = typeof ID.Type
|
||||
|
||||
export const AISDK_PREFIX = "aisdk:"
|
||||
export const isAISDK = (value: string | undefined): value is string => value?.startsWith(AISDK_PREFIX) ?? false
|
||||
export const aisdk = (value: string) => (isAISDK(value) ? value : `${AISDK_PREFIX}${value}`)
|
||||
export function packageName(value: string): string
|
||||
export function packageName(value: undefined): undefined
|
||||
export function packageName(value: string | undefined): string | undefined
|
||||
export function packageName(value: string | undefined) {
|
||||
// Native provider entrypoints can persist in user configuration across the npm scope migration.
|
||||
if (value?.startsWith("@opencode-ai/ai/")) return value.replace("@opencode-ai/", "@opencode/")
|
||||
if (value === undefined || !isAISDK(value)) return value
|
||||
return value.slice(AISDK_PREFIX.length)
|
||||
}
|
||||
|
||||
type Json = Schema.Schema.Type<typeof Schema.Json>
|
||||
const JsonRecord = Schema.Record(Schema.String, Schema.Json)
|
||||
const decodeJsonRecord = Schema.decodeUnknownSync(JsonRecord)
|
||||
|
||||
export class LoadError extends Schema.TaggedError<LoadError>()("Provider.LoadError", {
|
||||
package: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
@@ -202,7 +186,7 @@ const builtins = new Map<string, () => Promise<unknown>>([
|
||||
export const builtinPackages: ReadonlySet<string> = new Set(builtins.keys())
|
||||
|
||||
export const loadPackage = Effect.fn("Provider.loadPackage")(function* (input: string, npm?: Npm.Interface) {
|
||||
const specifier = packageName(input)
|
||||
const specifier = AISDKNative.packageName(input)
|
||||
const builtin = builtins.get(specifier)
|
||||
if (builtin) return yield* importPackage(specifier, specifier, builtin)
|
||||
const resolved = yield* Effect.sync(() => {
|
||||
@@ -276,46 +260,6 @@ export function encoding(settings: Readonly<Record<string, unknown>>) {
|
||||
return Option.getOrUndefined(decodeEncoding(settings.encoding))
|
||||
}
|
||||
|
||||
export function mergeOverlay(
|
||||
base: Readonly<Record<string, unknown>> | undefined,
|
||||
overlay: Readonly<Record<string, unknown>> | undefined,
|
||||
): Record<string, Json> | undefined {
|
||||
if (base === undefined) return overlay && decodeJsonRecord({ ...overlay })
|
||||
if (overlay === undefined) return decodeJsonRecord({ ...base })
|
||||
return decodeJsonRecord(mergeRecord(base, overlay))
|
||||
}
|
||||
|
||||
function mergeRecord(
|
||||
base: Readonly<Record<string, unknown>>,
|
||||
overlay: Readonly<Record<string, unknown>>,
|
||||
): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
new Set([...Object.keys(base), ...Object.keys(overlay)]).values().map((key): [string, unknown] => {
|
||||
const left = base[key]
|
||||
const right = overlay[key]
|
||||
if (right === undefined) return [key, left]
|
||||
if (isRecord(left) && isRecord(right)) return [key, mergeRecord(left, right)]
|
||||
return [key, right]
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
export function mergeHeaders(
|
||||
base: Readonly<Record<string, string>> | undefined,
|
||||
overlay: Readonly<Record<string, string>> | undefined,
|
||||
) {
|
||||
if (base === undefined) return overlay && { ...overlay }
|
||||
if (overlay === undefined) return { ...base }
|
||||
return Object.fromEntries(
|
||||
[...Object.entries(base), ...Object.entries(overlay)]
|
||||
.reduce((result, entry) => {
|
||||
result.set(entry[0].toLowerCase(), entry)
|
||||
return result
|
||||
}, new Map<string, [string, string]>())
|
||||
.values(),
|
||||
)
|
||||
}
|
||||
|
||||
export const Request = Provider.Request
|
||||
export type Request = Provider.Request
|
||||
|
||||
@@ -447,6 +391,7 @@ const layer = Layer.effect(
|
||||
update(record.provider)
|
||||
record.provider.id = id
|
||||
AISDKNative.rewrite(record.provider, {
|
||||
builtins: builtinPackages,
|
||||
specifier: record.provider.package,
|
||||
providerID: id,
|
||||
canonical: record.provider.canonical,
|
||||
@@ -475,6 +420,7 @@ const layer = Layer.effect(
|
||||
model.id = modelID
|
||||
model.providerID = providerID
|
||||
AISDKNative.rewrite(model, {
|
||||
builtins: builtinPackages,
|
||||
specifier: model.package ?? record.provider.package,
|
||||
providerID,
|
||||
canonical: model.canonical ?? record.provider.canonical,
|
||||
|
||||
@@ -17,7 +17,7 @@ function map(
|
||||
package: `aisdk:${packageName}`,
|
||||
settings: { ...settings },
|
||||
}
|
||||
AISDKNative.rewrite(target, { specifier: target.package, providerID, modelID })
|
||||
AISDKNative.rewrite(target, { builtins: Provider.builtinPackages, specifier: target.package, providerID, modelID })
|
||||
return target.package?.startsWith("aisdk:") ? undefined : target
|
||||
}
|
||||
|
||||
@@ -240,7 +240,12 @@ describe("AISDKNative", () => {
|
||||
package: specifier,
|
||||
settings,
|
||||
}
|
||||
AISDKNative.rewrite(target, { specifier, providerID: "test-provider", modelID })
|
||||
AISDKNative.rewrite(target, {
|
||||
builtins: Provider.builtinPackages,
|
||||
specifier,
|
||||
providerID: "test-provider",
|
||||
modelID,
|
||||
})
|
||||
return target
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ function model(packageName: string | undefined, options: RequestOptions = {}) {
|
||||
limit: options.limit ?? { context: 100, output: 20 },
|
||||
}
|
||||
AISDKNative.rewrite(info, {
|
||||
builtins: Provider.builtinPackages,
|
||||
specifier: packageName,
|
||||
providerID: info.providerID,
|
||||
canonical: info.canonical,
|
||||
|
||||
Reference in New Issue
Block a user