-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathplugin.ts
More file actions
91 lines (82 loc) · 2.96 KB
/
Copy pathplugin.ts
File metadata and controls
91 lines (82 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { readFileSync } from "fs"
import { join, dirname } from "path"
import { fileURLToPath } from "url"
const __dirname = dirname(fileURLToPath(import.meta.url))
interface ModelEntry {
id: string
name: string
tier: "premium" | "open-source"
reasoning: boolean
tool_call: boolean
cost: { input: number; output: number; cache_read?: number; cache_write?: number }
limit: { context: number; output: number }
}
function loadModels(): ModelEntry[] {
const modelsPath = join(__dirname, "models.json")
return JSON.parse(readFileSync(modelsPath, "utf-8"))
}
function toConfigKey(id: string): string {
const slashIdx = id.indexOf("/")
const short = slashIdx >= 0 ? id.slice(slashIdx + 1) : id
return short.toLowerCase()
}
export default async function commandcodePlugin() {
return {
config: async (config: Record<string, unknown>) => {
const providers = config.provider as Record<string, Record<string, unknown>> | undefined
if (!providers) {
(config as Record<string, unknown>).provider = { commandcode: {} }
}
const cc = ((config as Record<string, unknown>).provider as Record<string, Record<string, unknown>>)?.commandcode as Record<string, unknown> | undefined
if (!cc) return
if (!cc.npm) cc.npm = "commandcode-go-opencode-provider"
if (!cc.name) cc.name = "Command Code"
if (!cc.env) cc.env = ["COMMANDCODE_API_KEY"]
if (!cc.models) {
const models = loadModels()
const modelsObj: Record<string, unknown> = {}
for (const entry of models) {
const key = toConfigKey(entry.id)
const costObj: Record<string, number> = { input: entry.cost.input, output: entry.cost.output }
if (entry.cost.cache_read !== undefined) costObj.cache_read = entry.cost.cache_read
if (entry.cost.cache_write !== undefined) costObj.cache_write = entry.cost.cache_write
modelsObj[key] = {
id: entry.id,
name: entry.name,
reasoning: entry.reasoning,
tool_call: entry.tool_call,
cost: costObj,
limit: entry.limit,
}
}
cc.models = modelsObj
}
},
auth: {
provider: "commandcode",
methods: [
{
type: "api",
label: "API Key",
authorize: async (inputs: Record<string, unknown> | undefined) => {
const rawKey = inputs?.key
if (typeof rawKey !== "string") return { type: "failed" as const }
const key = rawKey.trim()
if (!key) return { type: "failed" as const }
return { type: "success" as const, key }
},
},
],
loader: async (getAuth: () => Promise<{ type: string; key?: string } | null>) => {
try {
const auth = await getAuth()
if (!auth) return {}
if (auth.type === "api" && auth.key) return { apiKey: auth.key }
return {}
} catch {
return {}
}
},
},
}
}