mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(ai): add Together AI, DeepInfra and Venice image models
This commit is contained in:
@@ -53,6 +53,7 @@
|
||||
"./providers/cohere/chat": "./src/providers/cohere/chat.ts",
|
||||
"./providers/deepgram": "./src/providers/deepgram.ts",
|
||||
"./providers/deepinfra": "./src/providers/deepinfra.ts",
|
||||
"./providers/deepinfra/images": "./src/providers/deepinfra/images.ts",
|
||||
"./providers/deepseek": "./src/providers/deepseek.ts",
|
||||
"./providers/digitalocean": "./src/providers/digitalocean.ts",
|
||||
"./providers/digitalocean/decisions": "./src/providers/digitalocean/decisions.ts",
|
||||
@@ -104,9 +105,11 @@
|
||||
"./providers/openrouter/responses": "./src/providers/openrouter/responses.ts",
|
||||
"./providers/runway": "./src/providers/runway.ts",
|
||||
"./providers/togetherai": "./src/providers/togetherai.ts",
|
||||
"./providers/togetherai/images": "./src/providers/togetherai/images.ts",
|
||||
"./providers/typesafe-ai": "./src/providers/typesafe-ai.ts",
|
||||
"./providers/typesafe-ai/decisions": "./src/providers/typesafe-ai/decisions.ts",
|
||||
"./providers/venice": "./src/providers/venice.ts",
|
||||
"./providers/venice/images": "./src/providers/venice/images.ts",
|
||||
"./providers/venice/responses": "./src/providers/venice/responses.ts",
|
||||
"./providers/vercel-ai-gateway": "./src/providers/vercel-ai-gateway.ts",
|
||||
"./providers/vercel-ai-gateway/chat": "./src/providers/vercel-ai-gateway/chat.ts",
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import type { ProviderPackage } from "../provider-package.js"
|
||||
import { OpenAIChat } from "../protocols/openai-chat.js"
|
||||
import { OpenAIImages } from "../protocols/openai-images.js"
|
||||
import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js"
|
||||
import { Route, type RouteDefaultsInput } from "../route/client.js"
|
||||
import { Endpoint } from "../route/endpoint.js"
|
||||
import { ProviderID, type ModelID } from "../schema/index.js"
|
||||
import { MediaRoute } from "../route/media.js"
|
||||
import { ProviderID, type ModelID, type ProviderOptions } from "../schema/index.js"
|
||||
import type { OpenAIProviderOptionsInput } from "./openai-options.js"
|
||||
|
||||
export const id = ProviderID.make("deepinfra")
|
||||
@@ -35,13 +37,10 @@ export const routes = [route]
|
||||
export const configure = (input: Config = {}) => {
|
||||
const { apiKey: _apiKey, auth: _auth, baseURL: endpoint, ...defaults } = input
|
||||
const root = endpoint?.replace(/\/+$/, "")
|
||||
const configured = route.with({
|
||||
...defaults,
|
||||
endpoint: {
|
||||
baseURL: root === undefined ? baseURL : root.endsWith("/openai") ? root : `${root}/openai`,
|
||||
},
|
||||
auth: AuthOptions.bearer(input, "DEEPINFRA_API_KEY"),
|
||||
})
|
||||
const openaiURL = root === undefined ? baseURL : root.endsWith("/openai") ? root : `${root}/openai`
|
||||
const auth = AuthOptions.bearer(input, "DEEPINFRA_API_KEY")
|
||||
const configured = route.with({ ...defaults, endpoint: { baseURL: openaiURL }, auth })
|
||||
const media = MediaRoute.deployment({ ...input, baseURL: openaiURL }, auth)
|
||||
return {
|
||||
id,
|
||||
model: (modelID: string | ModelID) =>
|
||||
@@ -53,20 +52,28 @@ export const configure = (input: Config = {}) => {
|
||||
supportsPromptCacheKey: true,
|
||||
},
|
||||
}),
|
||||
image: (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
OpenAIImages.model({ ...media, id: modelID, provider: id, providerOptions }),
|
||||
configure,
|
||||
}
|
||||
}
|
||||
|
||||
export const provider = configure()
|
||||
export const image = provider.image
|
||||
|
||||
export const model: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (
|
||||
modelID,
|
||||
{ apiKey, baseURL, body, headers, ...providerOptions }: Settings = {},
|
||||
) =>
|
||||
configure({
|
||||
export const model: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).model(modelID)
|
||||
export const imageModel = (modelID: string, settings?: Settings) => {
|
||||
const input = config(settings)
|
||||
return configure(input).image(modelID, input.providerOptions)
|
||||
}
|
||||
|
||||
function config({ apiKey, baseURL, body, headers, ...providerOptions }: Settings = {}) {
|
||||
return {
|
||||
apiKey,
|
||||
baseURL,
|
||||
headers,
|
||||
http: body === undefined ? undefined : { body },
|
||||
providerOptions,
|
||||
}).model(modelID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export { imageModel as model } from "../deepinfra.js"
|
||||
export type { Settings } from "../deepinfra.js"
|
||||
@@ -1,9 +1,12 @@
|
||||
import type { ProviderPackage } from "../provider-package.js"
|
||||
import { OpenAIChat } from "../protocols/openai-chat.js"
|
||||
import { OpenAIImages, type OpenAIImageSizing } from "../protocols/openai-images.js"
|
||||
import { ImageSizing } from "../protocols/utils/image-sizing.js"
|
||||
import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js"
|
||||
import { Route, type RouteDefaultsInput } from "../route/client.js"
|
||||
import { Endpoint } from "../route/endpoint.js"
|
||||
import { ProviderID, type ModelID } from "../schema/index.js"
|
||||
import { MediaRoute } from "../route/media.js"
|
||||
import { ProviderID, mergeProviderOptions, type ModelID, type ProviderOptions } from "../schema/index.js"
|
||||
import type { OpenAIProviderOptionsInput } from "./openai-options.js"
|
||||
|
||||
export const id = ProviderID.make("togetherai")
|
||||
@@ -32,13 +35,18 @@ export const route = Route.make({
|
||||
|
||||
export const routes = [route]
|
||||
|
||||
// Together takes `width` and `height` instead of `size`.
|
||||
const imageSizing: OpenAIImageSizing = (request) => {
|
||||
if (!ImageSizing.isRequested(request)) return undefined
|
||||
const size = ImageSizing.fit(request, { multiple: 16 })
|
||||
return { width: size.width, height: size.height }
|
||||
}
|
||||
|
||||
export const configure = (input: Config = {}) => {
|
||||
const { apiKey: _apiKey, auth: _auth, baseURL: endpoint, ...defaults } = input
|
||||
const configured = route.with({
|
||||
...defaults,
|
||||
endpoint: { baseURL: endpoint ?? baseURL },
|
||||
auth: AuthOptions.bearer(input, ["TOGETHER_API_KEY", "TOGETHER_AI_API_KEY"]),
|
||||
})
|
||||
const auth = AuthOptions.bearer(input, ["TOGETHER_API_KEY", "TOGETHER_AI_API_KEY"])
|
||||
const configured = route.with({ ...defaults, endpoint: { baseURL: endpoint ?? baseURL }, auth })
|
||||
const media = MediaRoute.deployment({ ...input, baseURL: endpoint ?? baseURL }, auth)
|
||||
return {
|
||||
id,
|
||||
model: (modelID: string | ModelID) =>
|
||||
@@ -46,20 +54,35 @@ export const configure = (input: Config = {}) => {
|
||||
id: modelID,
|
||||
compatibility: { maxTokensField: "max_tokens", supportsStrictMode: false },
|
||||
}),
|
||||
// Together returns hosted URLs unless asked for `base64`.
|
||||
image: (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
OpenAIImages.model({
|
||||
...media,
|
||||
id: modelID,
|
||||
provider: id,
|
||||
providerOptions: mergeProviderOptions({ response_format: "base64" }, providerOptions),
|
||||
sizing: imageSizing,
|
||||
}),
|
||||
configure,
|
||||
}
|
||||
}
|
||||
|
||||
export const provider = configure()
|
||||
export const image = provider.image
|
||||
|
||||
export const model: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (
|
||||
modelID,
|
||||
{ apiKey, baseURL, body, headers, ...providerOptions }: Settings = {},
|
||||
) =>
|
||||
configure({
|
||||
export const model: ProviderPackage.Model<Settings, OpenAIProviderOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).model(modelID)
|
||||
export const imageModel = (modelID: string, settings?: Settings) => {
|
||||
const input = config(settings)
|
||||
return configure(input).image(modelID, input.providerOptions)
|
||||
}
|
||||
|
||||
function config({ apiKey, baseURL, body, headers, ...providerOptions }: Settings = {}) {
|
||||
return {
|
||||
apiKey,
|
||||
baseURL,
|
||||
headers,
|
||||
http: body === undefined ? undefined : { body },
|
||||
providerOptions,
|
||||
}).model(modelID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export { imageModel as model } from "../togetherai.js"
|
||||
export type { Settings } from "../togetherai.js"
|
||||
@@ -1,11 +1,14 @@
|
||||
import type { ProviderPackage } from "../provider-package.js"
|
||||
import { VeniceChat } from "../protocols/venice-chat.js"
|
||||
import { OpenAIChat } from "../protocols/openai-chat.js"
|
||||
import { OpenAIImages, type OpenAIImageSizing } from "../protocols/openai-images.js"
|
||||
import { ImageSizing } from "../protocols/utils/image-sizing.js"
|
||||
import { OpenResponses } from "../protocols/open-responses.js"
|
||||
import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js"
|
||||
import { Route, type RouteDefaultsInput } from "../route/client.js"
|
||||
import { Endpoint } from "../route/endpoint.js"
|
||||
import { ProviderID, type ModelID } from "../schema/index.js"
|
||||
import { MediaRoute } from "../route/media.js"
|
||||
import { HttpOptions, mergeHttpOptions, ProviderID, type ModelID, type ProviderOptions } from "../schema/index.js"
|
||||
import type { OpenResponsesProviderOptionsInput } from "./open-responses-options.js"
|
||||
|
||||
export const id = ProviderID.make("venice")
|
||||
@@ -44,29 +47,58 @@ const responsesRoute = Route.make({
|
||||
})
|
||||
export const routes = [route, responsesRoute]
|
||||
|
||||
// Venice's OpenAI-compatible endpoint only takes these sizes.
|
||||
const IMAGE_SIZES = [
|
||||
{ width: 256, height: 256 },
|
||||
{ width: 512, height: 512 },
|
||||
{ width: 1024, height: 1024 },
|
||||
{ width: 1536, height: 1024 },
|
||||
{ width: 1024, height: 1536 },
|
||||
{ width: 1792, height: 1024 },
|
||||
{ width: 1024, height: 1792 },
|
||||
]
|
||||
|
||||
const imageSizing: OpenAIImageSizing = (request) => {
|
||||
if (!ImageSizing.isRequested(request)) return undefined
|
||||
const size = ImageSizing.nearest(request, IMAGE_SIZES)
|
||||
return { size: `${size.width}x${size.height}` }
|
||||
}
|
||||
|
||||
export const configure = (input: Config = {}) => {
|
||||
const { apiKey: _apiKey, auth: _auth, baseURL: endpoint, queryParams, ...rest } = input
|
||||
const options = {
|
||||
...rest,
|
||||
endpoint: { baseURL: endpoint ?? baseURL, query: queryParams },
|
||||
auth: AuthOptions.bearer(input, "VENICE_API_KEY"),
|
||||
}
|
||||
const auth = AuthOptions.bearer(input, "VENICE_API_KEY")
|
||||
const options = { ...rest, endpoint: { baseURL: endpoint ?? baseURL, query: queryParams }, auth }
|
||||
const chat = (modelID: string | ModelID) =>
|
||||
route.with(options).model<ChatOptionsInput>({ id: modelID, compatibility: VeniceChat.compatibility })
|
||||
const responses = (modelID: string | ModelID) =>
|
||||
responsesRoute.with(options).model<OpenResponsesProviderOptionsInput>({ id: modelID })
|
||||
return { id, model: chat, chat, responses, configure }
|
||||
const deployment = MediaRoute.deployment({ ...input, baseURL: endpoint ?? baseURL }, auth)
|
||||
const media = {
|
||||
...deployment,
|
||||
http: mergeHttpOptions(
|
||||
deployment.http,
|
||||
queryParams === undefined ? undefined : new HttpOptions({ query: queryParams }),
|
||||
),
|
||||
}
|
||||
const image = (modelID: string | ModelID, providerOptions?: ProviderOptions) =>
|
||||
OpenAIImages.model({ ...media, id: modelID, provider: id, providerOptions, sizing: imageSizing })
|
||||
return { id, model: chat, chat, responses, image, configure }
|
||||
}
|
||||
|
||||
export const provider = configure()
|
||||
export const chat = provider.chat
|
||||
export const responses = provider.responses
|
||||
export const image = provider.image
|
||||
export const model: ProviderPackage.Model<Settings, ChatOptionsInput> = (modelID, settings) =>
|
||||
configure(config(settings)).model(modelID)
|
||||
export const responsesModel: ProviderPackage.Model<
|
||||
Settings<OpenResponsesProviderOptionsInput>,
|
||||
OpenResponsesProviderOptionsInput
|
||||
> = (modelID, settings) => configure(config(settings)).responses(modelID)
|
||||
export const imageModel = (modelID: string, settings?: Settings) => {
|
||||
const input = config(settings)
|
||||
return configure(input).image(modelID, input.providerOptions)
|
||||
}
|
||||
|
||||
function config({
|
||||
apiKey,
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export { imageModel as model } from "../venice.js"
|
||||
export type { Settings } from "../venice.js"
|
||||
+32
File diff suppressed because one or more lines are too long
@@ -0,0 +1,44 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { ai, ImageClient } from "../../src/effect.js"
|
||||
import { DeepInfra } from "../../src/providers.js"
|
||||
import { it } from "../lib/effect.js"
|
||||
import { dynamicResponse, json, observe, type Call } from "../lib/http.js"
|
||||
|
||||
// No DeepInfra key to record with, so this pins the request shape without a cassette.
|
||||
describe("DeepInfra Images", () => {
|
||||
it.effect("sends a pixel size to the OpenAI-compatible route under a configured root", () =>
|
||||
Effect.gen(function* () {
|
||||
const calls: Array<Call> = []
|
||||
const response = yield* ai.image
|
||||
.generate({
|
||||
model: DeepInfra.configure({ apiKey: "deepinfra-key", baseURL: "https://api.deepinfra.test/v1" }).image(
|
||||
"black-forest-labs/FLUX-1-schnell",
|
||||
),
|
||||
prompt: "A red circle",
|
||||
aspect: "16:9",
|
||||
resolution: "1K",
|
||||
})
|
||||
.pipe(
|
||||
Effect.provide(
|
||||
ImageClient.layer.pipe(
|
||||
Layer.provide(
|
||||
dynamicResponse((input) =>
|
||||
observe(calls, input).pipe(Effect.as(json(input, { data: [{ b64_json: "iVBORw0KGgo=" }] }))),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
expect(calls[0].url).toBe("https://api.deepinfra.test/v1/openai/images/generations")
|
||||
expect(calls[0].headers.get("authorization")).toBe("Bearer deepinfra-key")
|
||||
expect(JSON.parse(calls[0].body)).toEqual({
|
||||
model: "black-forest-labs/FLUX-1-schnell",
|
||||
prompt: "A red circle",
|
||||
size: "1360x768",
|
||||
})
|
||||
expect(response.image.mediaType).toBe("image/png")
|
||||
}),
|
||||
)
|
||||
})
|
||||
@@ -104,6 +104,36 @@ const images: ReadonlyArray<Case<ImageModel>> = [
|
||||
auth: ["authorization", "Bearer vercel-key"],
|
||||
provider: "vercel-ai-gateway",
|
||||
},
|
||||
{
|
||||
name: "togetherai",
|
||||
load: async () => {
|
||||
const { model } = await import("@opencode/ai/providers/togetherai/images")
|
||||
return model("black-forest-labs/FLUX.2-dev", { ...shared, apiKey: "together-key" })
|
||||
},
|
||||
url: "https://api.together.xyz/v1/images/generations",
|
||||
auth: ["authorization", "Bearer together-key"],
|
||||
provider: "togetherai",
|
||||
},
|
||||
{
|
||||
name: "deepinfra",
|
||||
load: async () => {
|
||||
const { model } = await import("@opencode/ai/providers/deepinfra/images")
|
||||
return model("black-forest-labs/FLUX-1-schnell", { ...shared, apiKey: "deepinfra-key" })
|
||||
},
|
||||
url: "https://api.deepinfra.com/v1/openai/images/generations",
|
||||
auth: ["authorization", "Bearer deepinfra-key"],
|
||||
provider: "deepinfra",
|
||||
},
|
||||
{
|
||||
name: "venice",
|
||||
load: async () => {
|
||||
const { model } = await import("@opencode/ai/providers/venice/images")
|
||||
return model("z-image-turbo", { ...shared, apiKey: "venice-key", queryParams: { trace: "1" } })
|
||||
},
|
||||
url: "https://api.venice.ai/api/v1/images/generations?trace=1",
|
||||
auth: ["authorization", "Bearer venice-key"],
|
||||
provider: "venice",
|
||||
},
|
||||
{
|
||||
name: "digitalocean",
|
||||
load: async () => {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { ai, ImageClient } from "../../src/effect.js"
|
||||
import { TogetherAI } from "../../src/providers.js"
|
||||
import { it } from "../lib/effect.js"
|
||||
import { dynamicResponse, json, observe, type Call } from "../lib/http.js"
|
||||
|
||||
// Together's account settings can block every hosted image model, so this pins the request shape without a cassette.
|
||||
describe("Together AI Images", () => {
|
||||
it.effect("sends width, height and a base64 response format", () =>
|
||||
Effect.gen(function* () {
|
||||
const calls: Array<Call> = []
|
||||
const response = yield* ai.image
|
||||
.generate({
|
||||
model: TogetherAI.configure({ apiKey: "together-key" }).image("black-forest-labs/FLUX.2-dev"),
|
||||
prompt: "A red circle",
|
||||
aspect: "16:9",
|
||||
resolution: "1K",
|
||||
providerOptions: { steps: 4 },
|
||||
})
|
||||
.pipe(
|
||||
Effect.provide(
|
||||
ImageClient.layer.pipe(
|
||||
Layer.provide(
|
||||
dynamicResponse((input) =>
|
||||
observe(calls, input).pipe(
|
||||
Effect.as(json(input, { data: [{ index: 0, type: "b64_json", b64_json: "iVBORw0KGgo=" }] })),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
expect(calls[0].url).toBe("https://api.together.xyz/v1/images/generations")
|
||||
expect(calls[0].headers.get("authorization")).toBe("Bearer together-key")
|
||||
expect(JSON.parse(calls[0].body)).toEqual({
|
||||
model: "black-forest-labs/FLUX.2-dev",
|
||||
prompt: "A red circle",
|
||||
width: 1360,
|
||||
height: 768,
|
||||
steps: 4,
|
||||
response_format: "base64",
|
||||
})
|
||||
expect(response.image.mediaType).toBe("image/png")
|
||||
}),
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,35 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { ai } from "../../src/effect.js"
|
||||
import { Venice } from "../../src/providers.js"
|
||||
import { dimensions } from "../lib/image.js"
|
||||
import { recordedTests } from "../recorded-test.js"
|
||||
|
||||
const recorded = recordedTests({
|
||||
prefix: "venice-images",
|
||||
provider: "venice",
|
||||
protocol: "openai-images",
|
||||
requires: ["VENICE_API_KEY"],
|
||||
})
|
||||
|
||||
// Venice only takes a fixed list of sizes; replay matches the recorded body, so this also pins `size: 1792x1024`.
|
||||
describe("Venice Images recorded", () => {
|
||||
recorded.effect(
|
||||
"renders the listed size closest to the requested aspect",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const model = Venice.configure({ apiKey: process.env.VENICE_API_KEY ?? "fixture" }).image("z-image-turbo")
|
||||
const response = yield* ai.image.generate({
|
||||
model,
|
||||
prompt: "A simple flat black circle centered on a plain white background.",
|
||||
aspect: "16:9",
|
||||
providerOptions: { outputFormat: "jpeg" },
|
||||
})
|
||||
|
||||
expect(model.provider).toBe("venice")
|
||||
expect(response.images).toHaveLength(1)
|
||||
expect(dimensions(yield* response.image.bytes())).toEqual({ width: 1792, height: 1024 })
|
||||
}),
|
||||
{ timeout: 120_000 },
|
||||
)
|
||||
})
|
||||
@@ -91,6 +91,7 @@ const builtins = new Map<string, () => Promise<unknown>>([
|
||||
["@opencode/ai/providers/cohere", () => import("@opencode/ai/providers/cohere")],
|
||||
["@opencode/ai/providers/cohere/chat", () => import("@opencode/ai/providers/cohere/chat")],
|
||||
["@opencode/ai/providers/deepinfra", () => import("@opencode/ai/providers/deepinfra")],
|
||||
["@opencode/ai/providers/deepinfra/images", () => import("@opencode/ai/providers/deepinfra/images")],
|
||||
["@opencode/ai/providers/deepseek", () => import("@opencode/ai/providers/deepseek")],
|
||||
["@opencode/ai/providers/digitalocean", () => import("@opencode/ai/providers/digitalocean")],
|
||||
["@opencode/ai/providers/digitalocean/decisions", () => import("@opencode/ai/providers/digitalocean/decisions")],
|
||||
@@ -148,8 +149,10 @@ const builtins = new Map<string, () => Promise<unknown>>([
|
||||
["@opencode/ai/providers/openrouter/images", () => import("@opencode/ai/providers/openrouter/images")],
|
||||
["@opencode/ai/providers/openrouter/responses", () => import("@opencode/ai/providers/openrouter/responses")],
|
||||
["@opencode/ai/providers/togetherai", () => import("@opencode/ai/providers/togetherai")],
|
||||
["@opencode/ai/providers/togetherai/images", () => import("@opencode/ai/providers/togetherai/images")],
|
||||
["@opencode/ai/providers/typesafe-ai/decisions", () => import("@opencode/ai/providers/typesafe-ai/decisions")],
|
||||
["@opencode/ai/providers/venice", () => import("@opencode/ai/providers/venice")],
|
||||
["@opencode/ai/providers/venice/images", () => import("@opencode/ai/providers/venice/images")],
|
||||
["@opencode/ai/providers/venice/responses", () => import("@opencode/ai/providers/venice/responses")],
|
||||
["@opencode/ai/providers/vercel-ai-gateway", () => import("@opencode/ai/providers/vercel-ai-gateway")],
|
||||
["@opencode/ai/providers/vercel-ai-gateway/chat", () => import("@opencode/ai/providers/vercel-ai-gateway/chat")],
|
||||
|
||||
Reference in New Issue
Block a user