forked from brent-weatherall/opencode-commandcode-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv2.ts
More file actions
82 lines (76 loc) · 2.11 KB
/
Copy pathv2.ts
File metadata and controls
82 lines (76 loc) · 2.11 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
import {
loadModels,
fetchModelsFromApi,
mergeModels,
toConfigKey,
toV2ModelDraft,
} from "./src/models.js"
export const id = "commandcode-go-opencode-provider"
export interface CatalogDraft {
provider: {
update(providerID: string, update: (draft: Record<string, unknown>) => void): void
}
model: {
update(
providerID: string,
modelID: string,
update: (draft: Record<string, unknown>) => void,
): void
}
}
export interface V2PluginContext {
catalog: {
transform(
transform: (catalog: CatalogDraft) => void | Promise<void>,
): Promise<void>
}
}
/**
* OpenCode V2 plugin entrypoint.
*
* V2 loads a plugin from the module's default export, which must be an object
* with a unique `id` and a `setup` function. `setup` registers the Command
* Code provider and its model catalog through `ctx.catalog.transform`:
*
* ```json
* {
* "plugins": ["commandcode-go-opencode-provider/v2"]
* }
* ```
*/
export default {
id,
setup: async (ctx: V2PluginContext): Promise<void> => {
let models = loadModels()
try {
const apiModels = await fetchModelsFromApi()
if (apiModels && apiModels.length > 0) {
models = mergeModels(models, apiModels)
}
} catch {
// Graceful fallback to bundled models
}
await ctx.catalog.transform((catalog) => {
catalog.provider.update("commandcode", (draft) => {
draft.name = "Command Code"
draft.package = "aisdk:commandcode-go-opencode-provider"
draft.settings = {
baseURL: "https://api.commandcode.ai",
...(typeof draft.settings === "object" && draft.settings !== null
? (draft.settings as Record<string, unknown>)
: {}),
}
})
for (const entry of models) {
catalog.model.update("commandcode", toConfigKey(entry.id), (draft) => {
const model = toV2ModelDraft(entry)
draft.modelID = model.modelID
draft.name = model.name
draft.capabilities = model.capabilities
draft.limit = model.limit
draft.cost = model.cost
})
}
})
},
}