opencode-models-manager 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +27 -8
  3. package/package.json +6 -4
  4. package/src/models.ts +95 -20
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Felipe Gutiérrez
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Felipe Gutiérrez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -19,13 +19,13 @@ This repo was created to provide a simple solution for managing AI models for ev
19
19
 
20
20
  ## Installation
21
21
 
22
- This branch is the OpenCode v1 line and requires OpenCode 1.18.0 or later. Pin 0.1.0 explicitly so a future v2 release on the latest dist-tag is never installed into a v1 host:
22
+ The `main` branch is the OpenCode v1 line and requires OpenCode 1.18.0 or later. It is not compatible with OpenCode v2.
23
23
 
24
24
  ```
25
- opencode plugin opencode-model-manager@0.1.0
25
+ opencode plugin opencode-models-manager@0.1.1
26
26
  ```
27
27
 
28
- Once the opencode-v1 dist-tag is published, `opencode-model-manager@opencode-v1` resolves to this line and can replace the pinned version above.
28
+ For OpenCode v2, use the `v2` branch until the 2.x package release is published.
29
29
 
30
30
  Restart OpenCode after installation.
31
31
 
@@ -35,7 +35,7 @@ Add to `~/.config/opencode/opencode.json` (global) or `opencode.json` (project):
35
35
 
36
36
  ```json
37
37
  {
38
- "plugin": ["opencode-model-manager@0.1.0"]
38
+ "plugin": ["opencode-models-manager@0.1.1"]
39
39
  }
40
40
  ```
41
41
 
@@ -55,13 +55,32 @@ Create `.opencode/models.json` in the project root:
55
55
 
56
56
  ```json
57
57
  {
58
- "chat": "opencode-go/deepseek-v4.1-flash",
59
- "chat2": "opencode-go/mimo-v2.6-flash"
58
+ "agent": {
59
+ "chat": "opencode-go/deepseek-v4.1-flash",
60
+ "chat2": "opencode-go/mimo-v2.6-flash"
61
+ },
62
+ "commands": {
63
+ "document": "opencode-go/deepseek-v4.1-flash",
64
+ "document2": "opencode-go/mimo-v2.6-flash"
65
+ }
60
66
  }
61
67
  ```
62
68
 
63
- Each key is an agent name and each value is a `provider/model` reference. The plugin merges the model into that agent's existing configuration, so prompts, permissions, and other settings are preserved.
69
+ The `agent` section targets agents in `.opencode/agent` and the `commands` section targets commands in `.opencode/commands`. The plugin merges the model into each existing configuration, so prompts, permissions, frontmatter, and other settings are preserved. A flat map such as `{ "chat": "provider/model" }` is still supported and applies to agents.
64
70
 
65
71
  Invalid entries are logged and skipped. Restart OpenCode after changing `models.json` so the config hook runs again.
66
72
 
67
- Actually Opencode v1 is supported.
73
+ ## OpenCode v2
74
+
75
+ OpenCode v2 requires a default plugin definition object. The v2 implementation lives on the [`v2`](https://github.com/RainingDaemons/opencode-models-manager/tree/v2) branch and is versioned as `0.2.0`.
76
+
77
+ Use the v2 configuration key (`plugins`, plural):
78
+
79
+ ```json
80
+ {
81
+ "$schema": "https://opencode.ai/config.json",
82
+ "plugins": ["opencode-models-manager@0.2.0"]
83
+ }
84
+ ```
85
+
86
+ Agent models in `.opencode/models.json` are supported. OpenCode v2 currently exposes an add-only command transform API, so it cannot update models on commands that were loaded from `.opencode/commands`; command entries are logged and skipped.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-models-manager",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin — manage agent models from a single JSON file",
6
6
  "author": "RainingDaemons",
@@ -22,7 +22,7 @@
22
22
  "model-manager"
23
23
  ],
24
24
  "engines": {
25
- "opencode": ">=1.18.0"
25
+ "opencode": ">=2.0.0"
26
26
  },
27
27
  "main": "./src/models.ts",
28
28
  "exports": {
@@ -34,11 +34,13 @@
34
34
  "!test",
35
35
  "!test/**"
36
36
  ],
37
- "dependencies": {
38
- "@opencode-ai/plugin": "^1.18.1"
37
+ "devDependencies": {
38
+ "@types/node": "^24.0.0",
39
+ "typescript": "^6.0.0"
39
40
  },
40
41
  "scripts": {
41
42
  "test": "node --test test/models.test.ts",
43
+ "typecheck": "tsc --noEmit",
42
44
  "logo": "node script/logo.mjs"
43
45
  }
44
46
  }
package/src/models.ts CHANGED
@@ -1,9 +1,18 @@
1
1
  import { readFileSync } from "node:fs"
2
2
  import path from "node:path"
3
- import type { Plugin } from "@opencode-ai/plugin"
4
3
 
5
4
  const CONFIG_FILE = path.join(".opencode", "models.json")
6
5
 
6
+ type Section = "agent" | "command"
7
+ type Models = Record<Section, Record<string, string>>
8
+
9
+ const SECTIONS: Record<string, Section> = {
10
+ agent: "agent",
11
+ agents: "agent",
12
+ command: "command",
13
+ commands: "command",
14
+ }
15
+
7
16
  function isRecord(value: unknown): value is Record<string, unknown> {
8
17
  return typeof value === "object" && value !== null && !Array.isArray(value)
9
18
  }
@@ -13,7 +22,41 @@ function isModelRef(value: string): boolean {
13
22
  return slash > 0 && slash < value.length - 1
14
23
  }
15
24
 
16
- function loadModels(file: string): Record<string, string> | null {
25
+ function isEntryName(name: string): boolean {
26
+ return name.trim() !== ""
27
+ }
28
+
29
+ function parseModelRef(value: string): { providerID: string; id: string } {
30
+ const separator = value.indexOf("/")
31
+ return {
32
+ providerID: value.slice(0, separator),
33
+ id: value.slice(separator + 1),
34
+ }
35
+ }
36
+
37
+ function loadSection(
38
+ file: string,
39
+ section: Section,
40
+ value: Record<string, unknown>
41
+ ): Record<string, string> {
42
+ const models: Record<string, string> = {}
43
+ for (const [name, model] of Object.entries(value)) {
44
+ if (!isEntryName(name)) {
45
+ console.error(`[model-manager] ${file} ${section}s contains an empty name`)
46
+ continue
47
+ }
48
+ if (typeof model !== "string" || !isModelRef(model)) {
49
+ console.error(
50
+ `[model-manager] ${file} "${section}.${name}" must be "provider/model", got ${JSON.stringify(model)}`
51
+ )
52
+ continue
53
+ }
54
+ models[name] = model
55
+ }
56
+ return models
57
+ }
58
+
59
+ export function loadModels(file: string): Models | null {
17
60
  let raw: string
18
61
  try {
19
62
  raw = readFileSync(file, "utf8")
@@ -34,34 +77,66 @@ function loadModels(file: string): Record<string, string> | null {
34
77
  return null
35
78
  }
36
79
 
37
- const models: Record<string, string> = {}
38
- for (const [name, value] of Object.entries(parsed)) {
39
- if (name.trim() === "") {
40
- console.error(`[model-manager] ${file} contains an empty agent name`)
80
+ const models: Models = { agent: {}, command: {} }
81
+ for (const [key, value] of Object.entries(parsed)) {
82
+ if (typeof value === "string") {
83
+ if (!isEntryName(key)) {
84
+ console.error(`[model-manager] ${file} contains an empty agent name`)
85
+ continue
86
+ }
87
+ if (!isModelRef(value)) {
88
+ console.error(
89
+ `[model-manager] ${file} "${key}" must be "provider/model", got ${JSON.stringify(value)}`
90
+ )
91
+ continue
92
+ }
93
+ models.agent[key] = value
41
94
  continue
42
95
  }
43
- if (typeof value !== "string" || !isModelRef(value)) {
44
- console.error(
45
- `[model-manager] "${name}" must be "provider/model", got ${JSON.stringify(value)}`
46
- )
96
+
97
+ const section = Object.hasOwn(SECTIONS, key) ? SECTIONS[key] : undefined
98
+ if (!section) {
99
+ console.error(`[model-manager] ${file} has unknown key "${key}"`)
47
100
  continue
48
101
  }
49
- models[name] = value
102
+ if (!isRecord(value)) {
103
+ console.error(`[model-manager] ${file} "${key}" must be a JSON object`)
104
+ continue
105
+ }
106
+ Object.assign(models[section], loadSection(file, section, value))
50
107
  }
51
108
 
52
109
  return models
53
110
  }
54
111
 
55
- export default (async ({ directory, worktree }) => ({
56
- config: async (cfg) => {
57
- const root = worktree || directory
58
- const models = loadModels(path.join(root, CONFIG_FILE)) ??
59
- (root === directory ? null : loadModels(path.join(directory, CONFIG_FILE)))
112
+ /**
113
+ * OpenCode v2 setup plugin.
114
+ *
115
+ * This intentionally has no package imports beyond Node built-ins: OpenCode
116
+ * 2.0 resolves local and package plugins independently of its own SDK modules.
117
+ */
118
+ export default {
119
+ id: "opencode-models-manager",
120
+ async setup(ctx: any) {
121
+ const file = path.join(process.cwd(), CONFIG_FILE)
122
+ const models = loadModels(file)
60
123
  if (!models) return
61
124
 
62
- cfg.agent ??= {}
63
- for (const [name, model] of Object.entries(models)) {
64
- cfg.agent[name] = { ...cfg.agent[name], model }
125
+ if (Object.keys(models.agent).length > 0) {
126
+ await ctx.agent.transform((editor: any) => {
127
+ for (const [name, model] of Object.entries(models.agent)) {
128
+ editor.update(name, (agent: any) => {
129
+ agent.model = parseModelRef(model)
130
+ })
131
+ }
132
+ })
133
+ await ctx.agent.reload()
134
+ }
135
+
136
+ if (Object.keys(models.command).length > 0) {
137
+ console.error(
138
+ `[model-manager] ${file} contains command models, but OpenCode v2 does not support updating existing command definitions; those entries were skipped`
139
+ )
65
140
  }
66
141
  },
67
- })) satisfies Plugin
142
+ }