feat: tf code

This commit is contained in:
Gab
2026-03-24 18:27:16 +11:00
parent 2ad192a312
commit 1460f80d1a
6 changed files with 29 additions and 25 deletions

View File

@@ -97,6 +97,7 @@ export namespace ModelsDev {
toolCalling: boolean
maxTokens: number
deprecated: boolean
deploymentType?: string
pricing?: {
inputPer1mTokens: number
outputPer1mTokens: number
@@ -123,26 +124,26 @@ export namespace ModelsDev {
export async function get() {
const result = await Data()
const providers = result as Record<string, Provider>
// Try to fetch ToothFairyAI models dynamically
// First check env vars, then stored credentials
let tfApiKey = process.env.TF_API_KEY
let tfRegion = process.env.TF_REGION || "au"
// Try to load from stored credentials
if (!tfApiKey) {
try {
const credPath = path.join(Global.Path.data, ".tfcode", "credentials.json")
const credData = await Bun.file(credPath).json() as { api_key?: string; region?: string }
const credData = (await Bun.file(credPath).json()) as { api_key?: string; region?: string }
if (credData.api_key) {
tfApiKey = credData.api_key
tfRegion = credData.region || "au"
}
} catch {}
}
const tfBaseUrl = REGION_URLS[tfRegion] || REGION_URLS.au
if (tfApiKey) {
try {
const tfResponse = await fetch(`${tfBaseUrl}/models_list`, {
@@ -151,16 +152,19 @@ export namespace ModelsDev {
},
signal: AbortSignal.timeout(10000),
})
if (tfResponse.ok) {
const tfData = await tfResponse.json() as { templates: Record<string, TFModel> }
const tfData = (await tfResponse.json()) as { templates: Record<string, TFModel> }
const tfModels: Record<string, Model> = {}
for (const [key, model] of Object.entries(tfData.templates || {})) {
if (model.deprecated) continue
// Only include serverless models
if (model.deploymentType && model.deploymentType !== "serverless") continue
const modelId = key.startsWith("z/") ? key.slice(2) : key
tfModels[modelId] = {
id: modelId,
name: model.name,
@@ -185,7 +189,7 @@ export namespace ModelsDev {
},
}
}
providers["toothfairyai"] = {
id: "toothfairyai",
name: "ToothFairyAI",
@@ -197,7 +201,7 @@ export namespace ModelsDev {
log.error("Failed to fetch ToothFairyAI models", { error: e })
}
}
// Fallback to static models if dynamic fetch failed
if (!providers["toothfairyai"] || Object.keys(providers["toothfairyai"].models).length === 0) {
providers["toothfairyai"] = {
@@ -205,7 +209,7 @@ export namespace ModelsDev {
name: "ToothFairyAI",
env: ["TF_API_KEY", "TF_WORKSPACE_ID"],
models: {
"sorcerer": {
sorcerer: {
id: "sorcerer",
name: "TF Sorcerer",
family: "groq",
@@ -219,7 +223,7 @@ export namespace ModelsDev {
limit: { context: 128000, output: 16000 },
modalities: { input: ["text", "image"], output: ["text"] },
},
"mystica": {
mystica: {
id: "mystica",
name: "TF Mystica",
family: "fireworks",
@@ -236,7 +240,7 @@ export namespace ModelsDev {
},
}
}
return providers
}