mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-14 12:10:45 +03:00
feat(ai): a named method for every provider API (#60926)
This commit is contained in:
committed by
GitHub
Unverified
parent
56ec471ee6
commit
a5518690fc
@@ -127,6 +127,9 @@ export const responsesModel: ProviderPackage.Model<Settings<ResponsesOptionsInpu
|
||||
id,
|
||||
input,
|
||||
) => configure(config(input)).responses(id)
|
||||
export const chat = model
|
||||
export const messages = messagesModel
|
||||
export const responses = responsesModel
|
||||
export const imageModel = (id: string, settings: Settings) => {
|
||||
const input = config(settings)
|
||||
return configure(input).image(id, input.providerOptions)
|
||||
|
||||
@@ -152,6 +152,9 @@ export const configure = (input: Config = {}) => {
|
||||
}
|
||||
|
||||
export const provider = configure()
|
||||
export const chat = provider.chat
|
||||
export const messages = provider.messages
|
||||
export const responses = provider.responses
|
||||
|
||||
const fromSettings = ({
|
||||
apiKey,
|
||||
|
||||
@@ -228,3 +228,5 @@ export const imageModel = (modelID: string, settings: Settings) => {
|
||||
return configure(input).image(modelID, input.providerOptions)
|
||||
}
|
||||
export const model = responsesModel
|
||||
export const responses = responsesModel
|
||||
export const chat = chatModel
|
||||
|
||||
@@ -130,9 +130,19 @@ export const configure = (input: Config) => {
|
||||
headers: headers(input),
|
||||
}
|
||||
: rest
|
||||
const responses = responsesRoute.with(passthrough("openai"))
|
||||
const messages = messagesRoute.with(passthrough("anthropic/v1"))
|
||||
const chat = route.with(rest)
|
||||
const responsesConfigured = responsesRoute.with(passthrough("openai"))
|
||||
const messagesConfigured = messagesRoute.with(passthrough("anthropic/v1"))
|
||||
const chatConfigured = route.with(rest)
|
||||
const responses = (value: string | ModelID) =>
|
||||
responsesConfigured.model<OpenAIProviderOptionsInput>({
|
||||
id: native ? value.replace(/^openai\//, "") : value,
|
||||
})
|
||||
const messages = (value: string | ModelID) =>
|
||||
messagesConfigured.model<OpenAIProviderOptionsInput>({
|
||||
id: native ? value.replace(/^anthropic\//, "").replaceAll(".", "-") : value,
|
||||
})
|
||||
const chat = (value: string | ModelID) =>
|
||||
chatConfigured.model<OpenAIProviderOptionsInput>({ id: value.replace(/^workers-ai\//, "") })
|
||||
// The Run API base is derived per call so a chat-only configuration without an account still constructs.
|
||||
const decision = (value: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
CloudflareRunDecisions.route
|
||||
@@ -147,16 +157,13 @@ export const configure = (input: Config) => {
|
||||
return {
|
||||
id,
|
||||
model: (value: string | ModelID) => {
|
||||
if (value.startsWith("openai/"))
|
||||
return responses.model<OpenAIProviderOptionsInput>({ id: native ? value.slice("openai/".length) : value })
|
||||
if (value.startsWith("anthropic/"))
|
||||
return messages.model<OpenAIProviderOptionsInput>({
|
||||
id: native ? value.slice("anthropic/".length).replaceAll(".", "-") : value,
|
||||
})
|
||||
if (value.startsWith("workers-ai/"))
|
||||
return chat.model<OpenAIProviderOptionsInput>({ id: value.slice("workers-ai/".length) })
|
||||
return chat.model<OpenAIProviderOptionsInput>({ id: value })
|
||||
if (value.startsWith("openai/")) return responses(value)
|
||||
if (value.startsWith("anthropic/")) return messages(value)
|
||||
return chat(value)
|
||||
},
|
||||
chat,
|
||||
responses,
|
||||
messages,
|
||||
decision,
|
||||
configure,
|
||||
}
|
||||
@@ -166,6 +173,12 @@ export const provider = { id, configure }
|
||||
|
||||
export const model: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).model(modelID)
|
||||
export const chat: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).chat(modelID)
|
||||
export const responses: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).responses(modelID)
|
||||
export const messages: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).messages(modelID)
|
||||
export const decisionModel = (modelID: string, settings: Settings) => {
|
||||
const input = config(settings)
|
||||
return configure(input).decision(modelID, input.providerOptions)
|
||||
|
||||
@@ -50,6 +50,7 @@ export const configure = (input: Config = {}) => {
|
||||
}
|
||||
}
|
||||
export const provider = configure()
|
||||
export const chat = provider.chat
|
||||
export const model: ProviderPackage.Model<Settings, CohereChat.ProviderOptionsInput> = (
|
||||
modelID,
|
||||
{ apiKey, baseURL, body, headers, ...providerOptions }: Settings = {},
|
||||
|
||||
@@ -75,15 +75,17 @@ export const configure = (input: Config = {}) => {
|
||||
const media = MediaRoute.deployment({ ...input, baseURL: endpoint ?? baseURL }, auth)
|
||||
const image = (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
OpenAIImages.model({ ...media, id: modelID, provider: id, providerOptions })
|
||||
const chat = (modelID: string | ModelID) =>
|
||||
configured.model<OpenAIProviderOptionsInput>({
|
||||
id: modelID,
|
||||
compatibility: {
|
||||
supportsPromptCacheKey: true,
|
||||
},
|
||||
})
|
||||
return {
|
||||
id,
|
||||
model: (modelID: string | ModelID) =>
|
||||
configured.model<OpenAIProviderOptionsInput>({
|
||||
id: modelID,
|
||||
compatibility: {
|
||||
supportsPromptCacheKey: true,
|
||||
},
|
||||
}),
|
||||
model: chat,
|
||||
chat,
|
||||
responses: (modelID: string | ModelID) =>
|
||||
configuredResponses.model<OpenResponsesProviderOptionsInput>({ id: modelID }),
|
||||
image,
|
||||
@@ -95,6 +97,7 @@ export const configure = (input: Config = {}) => {
|
||||
export const provider = configure()
|
||||
export const decision = provider.decision
|
||||
export const image = provider.image
|
||||
export const chat = provider.chat
|
||||
export const responses = provider.responses
|
||||
|
||||
export const model: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
|
||||
@@ -51,9 +51,11 @@ const interactionsRoute = (input: Config) => {
|
||||
export const configure = (input: Config = {}) => {
|
||||
const route = configuredRoute(input)
|
||||
const media = MediaRoute.deployment(input, auth(input))
|
||||
const gemini = (modelID: string | ModelID) => route.model<Gemini.ProviderOptionsInput>({ id: modelID })
|
||||
return {
|
||||
id,
|
||||
model: (modelID: string | ModelID) => route.model<Gemini.ProviderOptionsInput>({ id: modelID }),
|
||||
model: gemini,
|
||||
gemini,
|
||||
interactions: (modelID: string | ModelID) =>
|
||||
interactionsRoute(input).model<GoogleInteractions.ProviderOptionsInput>({ id: modelID }),
|
||||
image: (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
@@ -84,6 +86,7 @@ function config({ apiKey, baseURL, body, headers, ...providerOptions }: Settings
|
||||
}
|
||||
|
||||
export const image = provider.image
|
||||
export const gemini = provider.gemini
|
||||
export const interactions = provider.interactions
|
||||
export const video = provider.video
|
||||
export const speech = provider.speech
|
||||
|
||||
@@ -272,10 +272,12 @@ export const configure = (input: Config = {}) => {
|
||||
compatibility: { mediaInput: "state" },
|
||||
providerOptions,
|
||||
})
|
||||
const chat = (modelID: string | ModelID) =>
|
||||
chatRoute.model<OpenRouterProviderOptionsInput>({ id: modelID, compatibility: { supportsPromptCacheKey: true } })
|
||||
return {
|
||||
id,
|
||||
model: (modelID: string | ModelID) =>
|
||||
chatRoute.model<OpenRouterProviderOptionsInput>({ id: modelID, compatibility: { supportsPromptCacheKey: true } }),
|
||||
model: chat,
|
||||
chat,
|
||||
responses: (modelID: string | ModelID) =>
|
||||
configuredResponses.model<OpenRouterResponsesProviderOptionsInput>({ id: modelID }),
|
||||
image,
|
||||
@@ -287,6 +289,7 @@ export const configure = (input: Config = {}) => {
|
||||
export const provider = configure()
|
||||
export const decision = provider.decision
|
||||
export const image = provider.image
|
||||
export const chat = provider.chat
|
||||
export const responses = provider.responses
|
||||
export const model: ProviderPackage.Model<Settings, OpenRouterProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).model(modelID)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { ai } from "../src/index.js"
|
||||
import { DigitalOcean, OpenAI, OpenRouter, Venice, XAI } from "../src/providers.js"
|
||||
|
||||
for (const p of [OpenAI, XAI, Venice, OpenRouter, DigitalOcean]) {
|
||||
p.chat("x")
|
||||
p.configure().chat("x")
|
||||
void ai.generate({ model: p.chat("x"), prompt: "Say hi" })
|
||||
}
|
||||
Reference in New Issue
Block a user