@vheins/opencode-9router 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 vheins
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
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # @vheins/opencode-9router
2
+
3
+ OpenCode plugin provider for [9Router](https://github.com/decolua/9router) — FREE AI Router & Token Saver. 40+ providers, 100+ models.
4
+
5
+ Mendaftarkan 9Router sebagai custom provider di OpenCode dengan auto-discovery models dan konfigurasi baseURL via `/connect`.
6
+
7
+ ## Quick Start
8
+
9
+ ```json
10
+ {
11
+ "plugin": ["@vheins/opencode-9router"]
12
+ }
13
+ ```
14
+
15
+ 1. Tambahkan plugin ke `opencode.json`
16
+ 2. Restart OpenCode
17
+ 3. `/connect` → pilih **Connect to 9Router**
18
+ 4. Masukkan Base URL (default: `http://localhost:20128`) dan API Key
19
+ 5. `/models` → pilih model 9Router
20
+
21
+ ## Features
22
+
23
+ - **Auto-discover models** — Models dari 9Router otomatis terdeteksi saat startup
24
+ - **Configurable baseURL** — Atur Base URL langsung dari `/connect`, bukan dari opencode.json
25
+ - **Dynamic model list** — Semua model dari 9Router tersedia, termasuk combo kustom
26
+ - **27+ fallback models** — Well-known models tersedia jika 9Router belum running
27
+ - **OpenAI-compatible** — Menggunakan `@ai-sdk/openai-compatible`
28
+
29
+ ## Installation
30
+
31
+ ### From npm (recommended)
32
+
33
+ ```json
34
+ {
35
+ "plugin": ["@vheins/opencode-9router"]
36
+ }
37
+ ```
38
+
39
+ ### Local file
40
+
41
+ ```bash
42
+ cp src/plugin.ts .opencode/plugins/9router-provider.ts
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ ### 1. Connect via `/connect`
48
+
49
+ ```
50
+ /connect
51
+ → Select: Connect to 9Router
52
+ → Base URL: http://localhost:20128 (customize if needed)
53
+ → API Key: [paste from 9Router Dashboard → Endpoints]
54
+ ```
55
+
56
+ ### 2. Select model
57
+
58
+ ```
59
+ /models
60
+ → Find 9Router provider
61
+ → Pick any model (e.g., kr/claude-sonnet-4.5, cc/claude-opus-4-7)
62
+ ```
63
+
64
+ ### Custom Base URL
65
+
66
+ Jika 9Router berjalan di host/port berbeda, masukkan URL saat `/connect`:
67
+
68
+ ```
69
+ Base URL: http://192.168.1.100:20128
70
+ ```
71
+
72
+ Atau via plugin options:
73
+
74
+ ```json
75
+ {
76
+ "plugin": [
77
+ ["@vheins/opencode-9router", { "baseURL": "http://192.168.1.100:20128" }]
78
+ ]
79
+ }
80
+ ```
81
+
82
+ Atau via environment variable:
83
+
84
+ ```bash
85
+ export ROUTER_BASE_URL=http://192.168.1.100:20128
86
+ ```
87
+
88
+ ## Model Prefixes
89
+
90
+ | Prefix | Provider | Tier |
91
+ |--------|----------|------|
92
+ | `cc/` | Claude Code | Subscription |
93
+ | `cx/` | Codex | Subscription |
94
+ | `gh/` | GitHub Copilot | Subscription |
95
+ | `cu/` | Cursor IDE | Subscription |
96
+ | `kr/` | Kiro AI | **FREE** |
97
+ | `oc/` | OpenCode Free | **FREE** |
98
+ | `vertex/` | Vertex AI | $300 credits |
99
+ | `glm/` | GLM | $0.6/1M |
100
+ | `minimax/` | MiniMax | $0.2/1M |
101
+ | `kimi/` | Kimi | $9/mo flat |
102
+ | `openrouter/` | OpenRouter | API key |
103
+ | `deepseek/` | DeepSeek | API key |
104
+ | `groq/` | Groq | API key |
105
+
106
+ ## How It Works
107
+
108
+ ```
109
+ opencode.json "plugin": ["@vheins/opencode-9router"]
110
+
111
+ Bun installs package from npm
112
+
113
+ Plugin loads at startup:
114
+ 1. Try GET /v1/models from 9Router (3s timeout)
115
+ 2. If OK → register live models
116
+ 3. If fail → register 27 fallback models
117
+
118
+ Provider "9router" appears in /models
119
+
120
+ User runs /connect → enters baseURL + API key
121
+
122
+ Auth loader overrides provider config with user's baseURL
123
+ ```
124
+
125
+ ## Development
126
+
127
+ ```bash
128
+ git clone https://github.com/vheins/opencode-9router
129
+ cd opencode-9router
130
+ bun install
131
+ ```
132
+
133
+ ### Publish
134
+
135
+ ```bash
136
+ npm login
137
+ npm publish --access public
138
+ ```
139
+
140
+ ## Files
141
+
142
+ ```
143
+ opencode-9router/
144
+ index.ts # Re-exports
145
+ src/
146
+ plugin.ts # Main plugin logic
147
+ constants.ts # Models, defaults, prefixes
148
+ package.json # npm package config
149
+ tsconfig.json # TypeScript config
150
+ README.md # This file
151
+ LICENSE # MIT
152
+ ```
153
+
154
+ ## Links
155
+
156
+ - [OpenCode Plugin Docs](https://opencode.ai/docs/plugins/)
157
+ - [OpenCode Custom Provider](https://opencode.ai/docs/providers/#custom-provider)
158
+ - [9Router GitHub](https://github.com/decolua/9router)
159
+ - [9Router Website](https://9router.com)
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./src/plugin";
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@vheins/opencode-9router",
3
+ "module": "index.ts",
4
+ "version": "0.1.0",
5
+ "description": "OpenCode plugin provider for 9Router — FREE AI Router & Token Saver. 40+ providers, 100+ models.",
6
+ "author": "vheins",
7
+ "keywords": [
8
+ "opencode",
9
+ "9router",
10
+ "ai-router",
11
+ "llm",
12
+ "provider",
13
+ "openai-compatible",
14
+ "token-saver",
15
+ "plugin"
16
+ ],
17
+ "repository": "https://github.com/vheins/opencode-9router",
18
+ "files": [
19
+ "index.ts",
20
+ "src"
21
+ ],
22
+ "license": "MIT",
23
+ "type": "module",
24
+ "devDependencies": {
25
+ "@opencode-ai/sdk": "^1.0.182",
26
+ "@types/bun": "latest"
27
+ },
28
+ "peerDependencies": {
29
+ "typescript": "^5.9.3"
30
+ },
31
+ "dependencies": {
32
+ "@opencode-ai/plugin": "^1.0.182"
33
+ }
34
+ }
@@ -0,0 +1,80 @@
1
+ export const PLUGIN_NAME = "9router";
2
+ export const PROVIDER_DISPLAY_NAME = "9Router";
3
+ export const DEFAULT_BASE_URL = "http://localhost:20128";
4
+ export const DEFAULT_API_PATH = "/v1";
5
+
6
+ /**
7
+ * Well-known 9Router models used as fallback when the API is unreachable.
8
+ * Covers the most common providers and tiers routed through 9Router.
9
+ */
10
+ export const FALLBACK_MODELS: Record<string, { name: string }> = {
11
+ // --- Claude Code (Subscription) ---
12
+ "cc/claude-opus-4-7": { name: "Claude Opus 4.7 (Claude Code)" },
13
+ "cc/claude-opus-4-6": { name: "Claude Opus 4.6 (Claude Code)" },
14
+ "cc/claude-sonnet-4-6": { name: "Claude Sonnet 4.6 (Claude Code)" },
15
+ "cc/claude-haiku-4-5-20251001": { name: "Claude Haiku 4.5 (Claude Code)" },
16
+
17
+ // --- OpenCode / Codex (Subscription) ---
18
+ "cx/gpt-5.5": { name: "GPT-5.5 (Codex)" },
19
+ "cx/gpt-5.4": { name: "GPT-5.4 (Codex)" },
20
+
21
+ // --- GitHub Copilot (Subscription) ---
22
+ "gh/gpt-5.4": { name: "GPT-5.4 (Copilot)" },
23
+ "gh/claude-sonnet-4.6": { name: "Claude Sonnet 4.6 (Copilot)" },
24
+
25
+ // --- Kiro AI (FREE) ---
26
+ "kr/claude-sonnet-4.5": { name: "Claude Sonnet 4.5 (Kiro Free)" },
27
+ "kr/claude-haiku-4.5": { name: "Claude Haiku 4.5 (Kiro Free)" },
28
+ "kr/glm-5": { name: "GLM-5 (Kiro Free)" },
29
+ "kr/MiniMax-M2.5": { name: "MiniMax M2.5 (Kiro Free)" },
30
+ "kr/qwen3-coder-next": { name: "Qwen3 Coder Next (Kiro Free)" },
31
+ "kr/deepseek-3.2": { name: "DeepSeek 3.2 (Kiro Free)" },
32
+
33
+ // --- OpenCode Free (FREE) ---
34
+ "oc/auto": { name: "OpenCode Free (Auto)" },
35
+
36
+ // --- Vertex AI ($300 credits) ---
37
+ "vertex/gemini-3.1-pro-preview": { name: "Gemini 3.1 Pro (Vertex)" },
38
+ "vertex/gemini-3-flash-preview": { name: "Gemini 3 Flash (Vertex)" },
39
+
40
+ // --- GLM (Cheap, $0.6/1M) ---
41
+ "glm/glm-5.1": { name: "GLM-5.1" },
42
+ "glm/glm-5": { name: "GLM-5" },
43
+ "glm/glm-4.7": { name: "GLM-4.7" },
44
+
45
+ // --- MiniMax (Cheapest, $0.2/1M) ---
46
+ "minimax/MiniMax-M2.7": { name: "MiniMax M2.7" },
47
+ "minimax/MiniMax-M2.5": { name: "MiniMax M2.5" },
48
+
49
+ // --- Kimi ($9/mo flat) ---
50
+ "kimi/kimi-k2.5": { name: "Kimi K2.5" },
51
+ "kimi/kimi-k2.5-thinking": { name: "Kimi K2.5 (Thinking)" },
52
+
53
+ // --- Cursor (Subscription) ---
54
+ "cu/claude-4.6-opus-max": { name: "Claude 4.6 Opus Max (Cursor)" },
55
+
56
+ // --- Standard Key Providers ---
57
+ "deepseek/deepseek-chat": { name: "DeepSeek Chat" },
58
+ "groq/llama-4.5-70b": { name: "Llama 4.5 70B (Groq)" },
59
+ };
60
+
61
+ /** Known provider prefixes in 9Router model IDs for human-readable naming. */
62
+ export const KNOWN_PROVIDER_PREFIXES: Record<string, string> = {
63
+ "cc/": "Claude Code",
64
+ "cx/": "Codex",
65
+ "gh/": "GitHub Copilot",
66
+ "kr/": "Kiro AI",
67
+ "oc/": "OpenCode Free",
68
+ "vertex/": "Vertex AI",
69
+ "vertex-partner/": "Vertex Partner",
70
+ "glm/": "GLM",
71
+ "minimax/": "MiniMax",
72
+ "kimi/": "Kimi",
73
+ "cu/": "Cursor",
74
+ "gc/": "Gemini CLI",
75
+ "if/": "iFlow",
76
+ "qw/": "Qwen",
77
+ "openrouter/": "OpenRouter",
78
+ "deepseek/": "DeepSeek",
79
+ "groq/": "Groq",
80
+ };
package/src/plugin.ts ADDED
@@ -0,0 +1,111 @@
1
+ import type { Plugin, PluginInput } from "@opencode-ai/plugin";
2
+ import type { Auth } from "@opencode-ai/sdk";
3
+ import {
4
+ PLUGIN_NAME,
5
+ PROVIDER_DISPLAY_NAME,
6
+ DEFAULT_BASE_URL,
7
+ DEFAULT_API_PATH,
8
+ FALLBACK_MODELS,
9
+ KNOWN_PROVIDER_PREFIXES,
10
+ } from "./constants";
11
+
12
+ function formatModelName(modelId: string): string {
13
+ for (const [prefix, provider] of Object.entries(KNOWN_PROVIDER_PREFIXES)) {
14
+ if (modelId.startsWith(prefix)) {
15
+ return `${modelId.slice(prefix.length)} (${provider})`;
16
+ }
17
+ }
18
+ return modelId;
19
+ }
20
+
21
+ async function discoverModels(
22
+ baseURL: string,
23
+ ): Promise<Record<string, { name: string }> | null> {
24
+ const apiURL = `${baseURL}${DEFAULT_API_PATH}`;
25
+ try {
26
+ const response = await fetch(`${apiURL}/models`, {
27
+ signal: AbortSignal.timeout(3000),
28
+ });
29
+ if (!response.ok) return null;
30
+
31
+ const data = (await response.json()) as {
32
+ data?: Array<{ id: string }>;
33
+ };
34
+ if (!data.data || !Array.isArray(data.data) || data.data.length === 0) {
35
+ return null;
36
+ }
37
+
38
+ const models: Record<string, { name: string }> = {};
39
+ for (const model of data.data) {
40
+ models[model.id] = { name: formatModelName(model.id) };
41
+ }
42
+ return models;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ const plugin: Plugin = async ({ client }: PluginInput, options?: { baseURL?: string }) => {
49
+ const configuredBaseURL = options?.baseURL ?? DEFAULT_BASE_URL;
50
+
51
+ const discovered = await discoverModels(configuredBaseURL);
52
+ const models = discovered ?? FALLBACK_MODELS;
53
+
54
+ if (!discovered && client?.app?.log) {
55
+ await client.app.log({
56
+ body: {
57
+ service: "9router-provider",
58
+ level: "warn",
59
+ message: `9Router not reachable at ${configuredBaseURL}. Using well-known model list. Start 9Router and restart opencode to auto-discover models.`,
60
+ },
61
+ });
62
+ }
63
+
64
+ return {
65
+ // provider hook: auto-registers 9Router with models — NO manual opencode.json config needed
66
+ provider: {
67
+ [PLUGIN_NAME]: {
68
+ npm: "@ai-sdk/openai-compatible",
69
+ name: PROVIDER_DISPLAY_NAME,
70
+ options: {
71
+ baseURL: `${configuredBaseURL}${DEFAULT_API_PATH}`,
72
+ },
73
+ models,
74
+ },
75
+ },
76
+
77
+ // auth hook: enables /connect flow with configurable baseURL + API key
78
+ auth: {
79
+ provider: PLUGIN_NAME,
80
+ async loader(getAuth: () => Promise<Auth>, _provider: unknown) {
81
+ const auth = await getAuth();
82
+ // If user set custom baseURL via /connect, override the provider default
83
+ if (auth && typeof auth === "object" && "baseURL" in auth) {
84
+ return { baseURL: `${String(auth.baseURL)}${DEFAULT_API_PATH}` };
85
+ }
86
+ return {};
87
+ },
88
+ methods: [
89
+ {
90
+ label: `Connect to ${PROVIDER_DISPLAY_NAME}`,
91
+ type: "api",
92
+ prompts: [
93
+ {
94
+ type: "text",
95
+ message: `${PROVIDER_DISPLAY_NAME} Base URL (default: ${DEFAULT_BASE_URL})`,
96
+ key: "baseURL",
97
+ default: DEFAULT_BASE_URL,
98
+ },
99
+ {
100
+ type: "text",
101
+ message: `${PROVIDER_DISPLAY_NAME} API Key (from Dashboard → Endpoints)`,
102
+ key: "apiKey",
103
+ },
104
+ ],
105
+ },
106
+ ],
107
+ },
108
+ };
109
+ };
110
+
111
+ export default plugin;