mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-18 06:34:50 +00:00
wip: zen
This commit is contained in:
@@ -97,8 +97,8 @@ export async function handler(
|
|||||||
const zenData = ZenData.list(opts.modelList)
|
const zenData = ZenData.list(opts.modelList)
|
||||||
const modelInfo = validateModel(zenData, model)
|
const modelInfo = validateModel(zenData, model)
|
||||||
const dataDumper = createDataDumper(sessionId, requestId, projectId)
|
const dataDumper = createDataDumper(sessionId, requestId, projectId)
|
||||||
const trialLimiter = createTrialLimiter(modelInfo.trialProvider, ip)
|
const trialLimiter = createTrialLimiter(modelInfo.trialProviders, ip)
|
||||||
const trialProvider = await trialLimiter?.check()
|
const trialProviders = await trialLimiter?.check()
|
||||||
const rateLimiter = createRateLimiter(
|
const rateLimiter = createRateLimiter(
|
||||||
modelInfo.id,
|
modelInfo.id,
|
||||||
modelInfo.allowAnonymous,
|
modelInfo.allowAnonymous,
|
||||||
@@ -120,7 +120,7 @@ export async function handler(
|
|||||||
authInfo,
|
authInfo,
|
||||||
modelInfo,
|
modelInfo,
|
||||||
sessionId,
|
sessionId,
|
||||||
trialProvider,
|
trialProviders,
|
||||||
retry,
|
retry,
|
||||||
stickyProvider,
|
stickyProvider,
|
||||||
)
|
)
|
||||||
@@ -402,7 +402,7 @@ export async function handler(
|
|||||||
authInfo: AuthInfo,
|
authInfo: AuthInfo,
|
||||||
modelInfo: ModelInfo,
|
modelInfo: ModelInfo,
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
trialProvider: string | undefined,
|
trialProviders: string[] | undefined,
|
||||||
retry: RetryOptions,
|
retry: RetryOptions,
|
||||||
stickyProvider: string | undefined,
|
stickyProvider: string | undefined,
|
||||||
) {
|
) {
|
||||||
@@ -411,15 +411,17 @@ export async function handler(
|
|||||||
return modelInfo.providers.find((provider) => provider.id === modelInfo.byokProvider)
|
return modelInfo.providers.find((provider) => provider.id === modelInfo.byokProvider)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trialProvider) {
|
|
||||||
return modelInfo.providers.find((provider) => provider.id === trialProvider)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stickyProvider) {
|
if (stickyProvider) {
|
||||||
const provider = modelInfo.providers.find((provider) => provider.id === stickyProvider)
|
const provider = modelInfo.providers.find((provider) => provider.id === stickyProvider)
|
||||||
if (provider) return provider
|
if (provider) return provider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (trialProviders) {
|
||||||
|
const trialProvider = trialProviders[Math.floor(Math.random() * trialProviders.length)]
|
||||||
|
const provider = modelInfo.providers.find((provider) => provider.id === trialProvider)
|
||||||
|
if (provider) return provider
|
||||||
|
}
|
||||||
|
|
||||||
if (retry.retryCount !== MAX_FAILOVER_RETRIES) {
|
if (retry.retryCount !== MAX_FAILOVER_RETRIES) {
|
||||||
const providers = modelInfo.providers
|
const providers = modelInfo.providers
|
||||||
.filter((provider) => !provider.disabled)
|
.filter((provider) => !provider.disabled)
|
||||||
|
|||||||
@@ -175,7 +175,8 @@ export const anthropicHelper: ProviderHelper = ({ reqModel, providerModel }) =>
|
|||||||
outputTokens: usage.output_tokens ?? 0,
|
outputTokens: usage.output_tokens ?? 0,
|
||||||
reasoningTokens: undefined,
|
reasoningTokens: undefined,
|
||||||
cacheReadTokens: usage.cache_read_input_tokens ?? undefined,
|
cacheReadTokens: usage.cache_read_input_tokens ?? undefined,
|
||||||
cacheWrite5mTokens: usage.cache_creation?.ephemeral_5m_input_tokens ?? undefined,
|
cacheWrite5mTokens:
|
||||||
|
usage.cache_creation?.ephemeral_5m_input_tokens ?? usage.cache_creation_input_tokens ?? undefined,
|
||||||
cacheWrite1hTokens: usage.cache_creation?.ephemeral_1h_input_tokens ?? undefined,
|
cacheWrite1hTokens: usage.cache_creation?.ephemeral_1h_input_tokens ?? undefined,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { IpTable } from "@opencode-ai/console-core/schema/ip.sql.js"
|
|||||||
import { UsageInfo } from "./provider/provider"
|
import { UsageInfo } from "./provider/provider"
|
||||||
import { Subscription } from "@opencode-ai/console-core/subscription.js"
|
import { Subscription } from "@opencode-ai/console-core/subscription.js"
|
||||||
|
|
||||||
export function createTrialLimiter(trialProvider: string | undefined, ip: string) {
|
export function createTrialLimiter(trialProviders: string[] | undefined, ip: string) {
|
||||||
if (!trialProvider) return
|
if (!trialProviders) return
|
||||||
if (!ip) return
|
if (!ip) return
|
||||||
|
|
||||||
const limit = Subscription.getFreeLimits().promoTokens
|
const limit = Subscription.getFreeLimits().promoTokens
|
||||||
@@ -24,7 +24,7 @@ export function createTrialLimiter(trialProvider: string | undefined, ip: string
|
|||||||
)
|
)
|
||||||
|
|
||||||
_isTrial = (data?.usage ?? 0) < limit
|
_isTrial = (data?.usage ?? 0) < limit
|
||||||
return _isTrial ? trialProvider : undefined
|
return _isTrial ? trialProviders : undefined
|
||||||
},
|
},
|
||||||
track: async (usageInfo: UsageInfo) => {
|
track: async (usageInfo: UsageInfo) => {
|
||||||
if (!_isTrial) return
|
if (!_isTrial) return
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export namespace ZenData {
|
|||||||
allowAnonymous: z.boolean().optional(),
|
allowAnonymous: z.boolean().optional(),
|
||||||
byokProvider: z.enum(["openai", "anthropic", "google"]).optional(),
|
byokProvider: z.enum(["openai", "anthropic", "google"]).optional(),
|
||||||
stickyProvider: z.enum(["strict", "prefer"]).optional(),
|
stickyProvider: z.enum(["strict", "prefer"]).optional(),
|
||||||
trialProvider: z.string().optional(),
|
trialProviders: z.array(z.string()).optional(),
|
||||||
fallbackProvider: z.string().optional(),
|
fallbackProvider: z.string().optional(),
|
||||||
rateLimit: z.number().optional(),
|
rateLimit: z.number().optional(),
|
||||||
providers: z.array(
|
providers: z.array(
|
||||||
|
|||||||
Reference in New Issue
Block a user