feat: openai-compatible provider
This commit is contained in:
parent
35b81f7ba0
commit
799e41e4eb
1 changed files with 31 additions and 24 deletions
55
index.ts
55
index.ts
|
|
@ -128,7 +128,6 @@ let exitCode = 0
|
|||
let forgejoHost: string
|
||||
let forgejoRepoOwner: string
|
||||
let forgejoRepoName: string
|
||||
let authConfigDir: string | undefined
|
||||
type PromptFile = {
|
||||
filename: string
|
||||
mime: string
|
||||
|
|
@ -142,40 +141,54 @@ type PromptFiles = PromptFile[]
|
|||
// ─── Auth config ─────────────────────────────────────────────────────────────
|
||||
|
||||
async function createAuthConfig(): Promise<string> {
|
||||
const fs = await import("node:fs")
|
||||
const os = await import("node:os")
|
||||
const path = await import("node:path")
|
||||
const crypto = await import("node:crypto")
|
||||
|
||||
const nomyoApiKey = process.env["NOMYO_API_KEY"]
|
||||
const nomyoApiUrl = process.env["NOMYO_API_URL"] || "https://chat.nomyo.ai/api"
|
||||
const modelEnv = process.env["MODEL"]
|
||||
|
||||
if (!nomyoApiKey) {
|
||||
throw new Error('Environment variable "NOMYO_API_KEY" is not set')
|
||||
}
|
||||
if (!modelEnv) {
|
||||
throw new Error('Environment variable "MODEL" is not set')
|
||||
}
|
||||
|
||||
const configDir = path.join(os.tmpdir(), `opencode-auth-${crypto.randomUUID()}`)
|
||||
fs.mkdirSync(configDir, { recursive: true })
|
||||
const [providerID, ...rest] = modelEnv.split("/")
|
||||
const modelID = rest.join("/")
|
||||
if (!providerID || !modelID) {
|
||||
throw new Error(`Invalid MODEL "${modelEnv}". Expected "provider/model".`)
|
||||
}
|
||||
|
||||
const authConfig = {
|
||||
openai: {
|
||||
type: "api" as const,
|
||||
key: nomyoApiKey,
|
||||
url: nomyoApiUrl,
|
||||
const configContent = {
|
||||
provider: {
|
||||
[providerID]: {
|
||||
npm: "@ai-sdk/openai-compatible",
|
||||
name: providerID,
|
||||
options: { baseURL: nomyoApiUrl },
|
||||
models: {
|
||||
[modelID]: { tools: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fs.writeFileSync(path.join(configDir, "auth.json"), JSON.stringify(authConfig, null, 2))
|
||||
console.log("Auth config created:", configDir)
|
||||
const authContent = {
|
||||
[providerID]: {
|
||||
type: "api",
|
||||
key: nomyoApiKey,
|
||||
},
|
||||
}
|
||||
|
||||
return configDir
|
||||
process.env["OPENCODE_CONFIG_CONTENT"] = JSON.stringify(configContent)
|
||||
process.env["OPENCODE_AUTH_CONTENT"] = JSON.stringify(authContent)
|
||||
|
||||
console.log(`Registered provider "${providerID}" (openai-compatible) at ${nomyoApiUrl} with model "${modelID}"`)
|
||||
return ""
|
||||
}
|
||||
|
||||
// ─── Entry ───────────────────────────────────────────────────────────────────
|
||||
|
||||
try {
|
||||
authConfigDir = await createAuthConfig()
|
||||
process.env["OPENCODE_AUTH_CONFIG_DIR"] = authConfigDir
|
||||
await createAuthConfig()
|
||||
|
||||
proc = spawn(`opencode`, [`serve`, `--hostname=${HOST}`, `--port=${PORT}`], {
|
||||
stdio: ["ignore", "inherit", "inherit"],
|
||||
|
|
@ -282,12 +295,6 @@ try {
|
|||
if (proc) {
|
||||
proc.kill()
|
||||
}
|
||||
// Clean up auth config directory
|
||||
const fs = await import("node:fs")
|
||||
const path = await import("node:path")
|
||||
if (authConfigDir && fs.existsSync(authConfigDir)) {
|
||||
fs.rmSync(authConfigDir, { recursive: true, force: true })
|
||||
}
|
||||
await restoreGitConfig()
|
||||
}
|
||||
process.exit(exitCode)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue