mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 01:34:22 +01:00
wip: zen
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
export type Format = "anthropic" | "openai" | "oa-compat"
|
|
||||||
@@ -18,7 +18,6 @@ import {
|
|||||||
createStreamPartConverter,
|
createStreamPartConverter,
|
||||||
createResponseConverter,
|
createResponseConverter,
|
||||||
} from "./provider/provider"
|
} from "./provider/provider"
|
||||||
import { Format } from "./format"
|
|
||||||
import { anthropicHelper } from "./provider/anthropic"
|
import { anthropicHelper } from "./provider/anthropic"
|
||||||
import { openaiHelper } from "./provider/openai"
|
import { openaiHelper } from "./provider/openai"
|
||||||
import { oaCompatHelper } from "./provider/openai-compatible"
|
import { oaCompatHelper } from "./provider/openai-compatible"
|
||||||
@@ -29,7 +28,7 @@ type Model = ZenData["models"][string]
|
|||||||
export async function handler(
|
export async function handler(
|
||||||
input: APIEvent,
|
input: APIEvent,
|
||||||
opts: {
|
opts: {
|
||||||
format: Format
|
format: ZenData.Format
|
||||||
parseApiKey: (headers: Headers) => string | undefined
|
parseApiKey: (headers: Headers) => string | undefined
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
@@ -248,12 +247,14 @@ export async function handler(
|
|||||||
throw new ModelError(`Provider ${provider.id} not supported`)
|
throw new ModelError(`Provider ${provider.id} not supported`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const format = zenData.providers[provider.id].format
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...provider,
|
...provider,
|
||||||
...zenData.providers[provider.id],
|
...zenData.providers[provider.id],
|
||||||
...(provider.id === "anthropic"
|
...(format === "anthropic"
|
||||||
? anthropicHelper
|
? anthropicHelper
|
||||||
: provider.id === "openai"
|
: format === "openai"
|
||||||
? openaiHelper
|
? openaiHelper
|
||||||
: oaCompatHelper),
|
: oaCompatHelper),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Format } from "../format"
|
import { ZenData } from "@opencode-ai/console-core/model.js"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
fromAnthropicChunk,
|
fromAnthropicChunk,
|
||||||
fromAnthropicRequest,
|
fromAnthropicRequest,
|
||||||
@@ -26,7 +25,7 @@ import {
|
|||||||
} from "./openai-compatible"
|
} from "./openai-compatible"
|
||||||
|
|
||||||
export type ProviderHelper = {
|
export type ProviderHelper = {
|
||||||
format: Format
|
format: ZenData.Format
|
||||||
modifyUrl: (providerApi: string) => string
|
modifyUrl: (providerApi: string) => string
|
||||||
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => void
|
modifyHeaders: (headers: Headers, body: Record<string, any>, apiKey: string) => void
|
||||||
modifyBody: (body: Record<string, any>) => Record<string, any>
|
modifyBody: (body: Record<string, any>) => Record<string, any>
|
||||||
@@ -158,7 +157,7 @@ export interface CommonChunk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createBodyConverter(from: Format, to: Format) {
|
export function createBodyConverter(from: ZenData.Format, to: ZenData.Format) {
|
||||||
return (body: any): any => {
|
return (body: any): any => {
|
||||||
if (from === to) return body
|
if (from === to) return body
|
||||||
|
|
||||||
@@ -173,7 +172,7 @@ export function createBodyConverter(from: Format, to: Format) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createStreamPartConverter(from: Format, to: Format) {
|
export function createStreamPartConverter(from: ZenData.Format, to: ZenData.Format) {
|
||||||
return (part: any): any => {
|
return (part: any): any => {
|
||||||
if (from === to) return part
|
if (from === to) return part
|
||||||
|
|
||||||
@@ -191,7 +190,7 @@ export function createStreamPartConverter(from: Format, to: Format) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createResponseConverter(from: Format, to: Format) {
|
export function createResponseConverter(from: ZenData.Format, to: ZenData.Format) {
|
||||||
return (response: any): any => {
|
return (response: any): any => {
|
||||||
if (from === to) return response
|
if (from === to) return response
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ import { Actor } from "./actor"
|
|||||||
import { Resource } from "@opencode-ai/console-resource"
|
import { Resource } from "@opencode-ai/console-resource"
|
||||||
|
|
||||||
export namespace ZenData {
|
export namespace ZenData {
|
||||||
|
const FormatSchema = z.enum(["anthropic", "openai", "oa-compat"])
|
||||||
|
export type Format = z.infer<typeof FormatSchema>
|
||||||
|
|
||||||
const ModelCostSchema = z.object({
|
const ModelCostSchema = z.object({
|
||||||
input: z.number(),
|
input: z.number(),
|
||||||
output: z.number(),
|
output: z.number(),
|
||||||
@@ -34,6 +37,7 @@ export namespace ZenData {
|
|||||||
const ProviderSchema = z.object({
|
const ProviderSchema = z.object({
|
||||||
api: z.string(),
|
api: z.string(),
|
||||||
apiKey: z.string(),
|
apiKey: z.string(),
|
||||||
|
format: FormatSchema,
|
||||||
headerMappings: z.record(z.string(), z.string()).optional(),
|
headerMappings: z.record(z.string(), z.string()).optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user