@heventure/model-provider-x 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 +21 -0
- package/README.md +84 -0
- package/dist/cli/args.js +107 -0
- package/dist/cli/commands.js +139 -0
- package/dist/cli/index.js +233 -0
- package/dist/cli/tui.js +178 -0
- package/dist/core/config.js +102 -0
- package/dist/core/provider.js +61 -0
- package/dist/core/tool-config.js +82 -0
- package/dist/proxy/anthropic-chat.js +196 -0
- package/dist/proxy/server.js +103 -0
- package/dist/proxy/sse.js +37 -0
- package/dist/shared/types.js +1 -0
- package/dist/targets/claude-code.js +54 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HHWY Inc.
|
|
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,84 @@
|
|
|
1
|
+
# model-provider-x
|
|
2
|
+
|
|
3
|
+
`model-provider-x` is a TypeScript CLI/TUI tool for wiring custom model providers into local AI coding tools.
|
|
4
|
+
|
|
5
|
+
It currently supports:
|
|
6
|
+
|
|
7
|
+
- OpenCode provider config generation and JSONC merge.
|
|
8
|
+
- Claude Code setup through a local Anthropic Messages-compatible proxy.
|
|
9
|
+
- OpenAI-compatible `/v1/models` discovery.
|
|
10
|
+
- Anthropic Messages API to OpenAI Chat Completions API conversion.
|
|
11
|
+
- Non-streaming and streaming SSE proxy responses.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install
|
|
17
|
+
npm run build
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Or run the published package directly:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npx @heventure/model-provider-x --help
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## OpenCode Setup
|
|
27
|
+
|
|
28
|
+
Run the TUI wizard:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
node dist/cli/index.js
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or print a config fragment without writing files:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
node dist/cli/index.js \
|
|
38
|
+
--name "Unsloth Local" \
|
|
39
|
+
--id unsloth \
|
|
40
|
+
--base-url http://localhost:8888/v1 \
|
|
41
|
+
--models qwen,gemma \
|
|
42
|
+
--print
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Claude Code Setup
|
|
46
|
+
|
|
47
|
+
Create or update a provider profile and write Claude Code user settings:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
node dist/cli/index.js setup --target claude-code \
|
|
51
|
+
--name "Unsloth Local" \
|
|
52
|
+
--id unsloth \
|
|
53
|
+
--base-url http://localhost:8888/v1
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Then start the local proxy:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
node dist/cli/index.js proxy --profile unsloth
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The Claude Code setup writes gateway environment values to `~/.claude/settings.json`.
|
|
63
|
+
Upstream provider keys are stored in `~/.config/model-provider-x/config.jsonc`, not in Claude Code settings.
|
|
64
|
+
|
|
65
|
+
## Commands
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npx @heventure/model-provider-x --help
|
|
69
|
+
npx @heventure/model-provider-x setup --target claude-code
|
|
70
|
+
npx @heventure/model-provider-x proxy --profile <id>
|
|
71
|
+
npx @heventure/model-provider-x config print --profile <id>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Development
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm test
|
|
78
|
+
npm run lint
|
|
79
|
+
npm run build
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|
package/dist/cli/args.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export function parseCliArgs(argv) {
|
|
2
|
+
const options = { print: false, yes: false };
|
|
3
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
4
|
+
const arg = argv[index];
|
|
5
|
+
const next = () => {
|
|
6
|
+
const value = argv[index + 1];
|
|
7
|
+
if (!value || value.startsWith("--")) {
|
|
8
|
+
throw new Error(`${arg} requires a value`);
|
|
9
|
+
}
|
|
10
|
+
index += 1;
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
13
|
+
switch (arg) {
|
|
14
|
+
case "--api-key":
|
|
15
|
+
options.apiKey = next();
|
|
16
|
+
break;
|
|
17
|
+
case "--base-url":
|
|
18
|
+
options.baseURL = next();
|
|
19
|
+
break;
|
|
20
|
+
case "--config":
|
|
21
|
+
options.configPath = next();
|
|
22
|
+
break;
|
|
23
|
+
case "--id":
|
|
24
|
+
options.providerId = next();
|
|
25
|
+
break;
|
|
26
|
+
case "--models":
|
|
27
|
+
options.models = next()
|
|
28
|
+
.split(",")
|
|
29
|
+
.map((model) => model.trim())
|
|
30
|
+
.filter(Boolean);
|
|
31
|
+
break;
|
|
32
|
+
case "--name":
|
|
33
|
+
options.providerName = next();
|
|
34
|
+
break;
|
|
35
|
+
case "--print":
|
|
36
|
+
options.print = true;
|
|
37
|
+
break;
|
|
38
|
+
case "--yes":
|
|
39
|
+
case "-y":
|
|
40
|
+
options.yes = true;
|
|
41
|
+
break;
|
|
42
|
+
case "--help":
|
|
43
|
+
case "-h":
|
|
44
|
+
throw new HelpRequested();
|
|
45
|
+
default:
|
|
46
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return options;
|
|
50
|
+
}
|
|
51
|
+
export function parseModelSelection(selection, models) {
|
|
52
|
+
const trimmed = selection.trim();
|
|
53
|
+
if (!trimmed || trimmed.toLowerCase() === "all") {
|
|
54
|
+
return models;
|
|
55
|
+
}
|
|
56
|
+
const selected = new Set();
|
|
57
|
+
for (const part of trimmed.split(",")) {
|
|
58
|
+
const token = part.trim();
|
|
59
|
+
const range = token.match(/^(\d+)-(\d+)$/);
|
|
60
|
+
if (range) {
|
|
61
|
+
const start = Number(range[1]);
|
|
62
|
+
const end = Number(range[2]);
|
|
63
|
+
for (let index = start; index <= end; index += 1) {
|
|
64
|
+
addModelByOneBasedIndex(selected, models, index);
|
|
65
|
+
}
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const index = Number(token);
|
|
69
|
+
if (Number.isInteger(index)) {
|
|
70
|
+
addModelByOneBasedIndex(selected, models, index);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (models.includes(token)) {
|
|
74
|
+
selected.add(token);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`Unknown model selection: ${token}`);
|
|
78
|
+
}
|
|
79
|
+
return [...selected];
|
|
80
|
+
}
|
|
81
|
+
export function usage() {
|
|
82
|
+
return `model-provider-x
|
|
83
|
+
|
|
84
|
+
Usage:
|
|
85
|
+
model-provider-x [options]
|
|
86
|
+
|
|
87
|
+
Options:
|
|
88
|
+
--base-url <url> OpenAI-compatible API base URL, for example http://localhost:8888/v1
|
|
89
|
+
--api-key <key> Optional API key. Written into config when provided.
|
|
90
|
+
--name <name> Provider display name.
|
|
91
|
+
--id <id> Provider id used under provider.<id>.
|
|
92
|
+
--models <list> Comma-separated model ids. Skips interactive model selection.
|
|
93
|
+
--config <path> OpenCode config path to write.
|
|
94
|
+
--print Print generated JSON and do not write config.
|
|
95
|
+
--yes, -y Accept defaults in non-interactive prompts.
|
|
96
|
+
--help, -h Show this help.
|
|
97
|
+
`;
|
|
98
|
+
}
|
|
99
|
+
export class HelpRequested extends Error {
|
|
100
|
+
}
|
|
101
|
+
function addModelByOneBasedIndex(selected, models, index) {
|
|
102
|
+
const model = models[index - 1];
|
|
103
|
+
if (!model) {
|
|
104
|
+
throw new Error(`Model index out of range: ${index}`);
|
|
105
|
+
}
|
|
106
|
+
selected.add(model);
|
|
107
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { HelpRequested, parseCliArgs, usage } from "./args.js";
|
|
2
|
+
export function parseCommand(argv) {
|
|
3
|
+
const [command, ...rest] = argv;
|
|
4
|
+
if (!command || command.startsWith("--")) {
|
|
5
|
+
return { command: "opencode", options: parseCliArgs(argv) };
|
|
6
|
+
}
|
|
7
|
+
if (command === "setup") {
|
|
8
|
+
return parseSetupCommand(rest);
|
|
9
|
+
}
|
|
10
|
+
if (command === "proxy") {
|
|
11
|
+
return parseProxyCommand(rest);
|
|
12
|
+
}
|
|
13
|
+
if (command === "config") {
|
|
14
|
+
return parseConfigCommand(rest);
|
|
15
|
+
}
|
|
16
|
+
if (command === "--help" || command === "-h") {
|
|
17
|
+
throw new HelpRequested();
|
|
18
|
+
}
|
|
19
|
+
throw new Error(`Unknown command: ${command}`);
|
|
20
|
+
}
|
|
21
|
+
export function commandUsage() {
|
|
22
|
+
return `${usage()}
|
|
23
|
+
Commands:
|
|
24
|
+
model-provider-x setup --target <opencode|claude-code> [options]
|
|
25
|
+
model-provider-x proxy --profile <id> [--host 127.0.0.1] [--port 4141]
|
|
26
|
+
model-provider-x config print --profile <id>
|
|
27
|
+
`;
|
|
28
|
+
}
|
|
29
|
+
function parseSetupCommand(argv) {
|
|
30
|
+
let target = "opencode";
|
|
31
|
+
let profileId;
|
|
32
|
+
let port;
|
|
33
|
+
let host;
|
|
34
|
+
let defaultModel;
|
|
35
|
+
const providerArgs = [];
|
|
36
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
37
|
+
const arg = argv[index];
|
|
38
|
+
const next = () => {
|
|
39
|
+
const value = argv[index + 1];
|
|
40
|
+
if (!value || value.startsWith("--")) {
|
|
41
|
+
throw new Error(`${arg} requires a value`);
|
|
42
|
+
}
|
|
43
|
+
index += 1;
|
|
44
|
+
return value;
|
|
45
|
+
};
|
|
46
|
+
switch (arg) {
|
|
47
|
+
case "--target":
|
|
48
|
+
target = parseTarget(next());
|
|
49
|
+
break;
|
|
50
|
+
case "--profile":
|
|
51
|
+
profileId = next();
|
|
52
|
+
break;
|
|
53
|
+
case "--port":
|
|
54
|
+
port = parsePort(next());
|
|
55
|
+
break;
|
|
56
|
+
case "--host":
|
|
57
|
+
host = next();
|
|
58
|
+
break;
|
|
59
|
+
case "--default-model":
|
|
60
|
+
defaultModel = next();
|
|
61
|
+
break;
|
|
62
|
+
default:
|
|
63
|
+
providerArgs.push(arg);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { command: "setup", target, profileId, port, host, defaultModel, options: parseCliArgs(providerArgs) };
|
|
67
|
+
}
|
|
68
|
+
function parseProxyCommand(argv) {
|
|
69
|
+
let profileId;
|
|
70
|
+
let host;
|
|
71
|
+
let port;
|
|
72
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
73
|
+
const arg = argv[index];
|
|
74
|
+
const next = () => {
|
|
75
|
+
const value = argv[index + 1];
|
|
76
|
+
if (!value || value.startsWith("--")) {
|
|
77
|
+
throw new Error(`${arg} requires a value`);
|
|
78
|
+
}
|
|
79
|
+
index += 1;
|
|
80
|
+
return value;
|
|
81
|
+
};
|
|
82
|
+
switch (arg) {
|
|
83
|
+
case "--profile":
|
|
84
|
+
profileId = next();
|
|
85
|
+
break;
|
|
86
|
+
case "--host":
|
|
87
|
+
host = next();
|
|
88
|
+
break;
|
|
89
|
+
case "--port":
|
|
90
|
+
port = parsePort(next());
|
|
91
|
+
break;
|
|
92
|
+
case "--help":
|
|
93
|
+
case "-h":
|
|
94
|
+
throw new HelpRequested();
|
|
95
|
+
default:
|
|
96
|
+
throw new Error(`Unknown proxy argument: ${arg}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!profileId) {
|
|
100
|
+
throw new Error("--profile is required");
|
|
101
|
+
}
|
|
102
|
+
return { command: "proxy", profileId, host, port };
|
|
103
|
+
}
|
|
104
|
+
function parseConfigCommand(argv) {
|
|
105
|
+
const [subcommand, ...rest] = argv;
|
|
106
|
+
if (subcommand !== "print") {
|
|
107
|
+
throw new Error(`Unknown config command: ${subcommand ?? ""}`.trim());
|
|
108
|
+
}
|
|
109
|
+
let profileId;
|
|
110
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
111
|
+
const arg = rest[index];
|
|
112
|
+
if (arg !== "--profile") {
|
|
113
|
+
throw new Error(`Unknown config print argument: ${arg}`);
|
|
114
|
+
}
|
|
115
|
+
const value = rest[index + 1];
|
|
116
|
+
if (!value || value.startsWith("--")) {
|
|
117
|
+
throw new Error("--profile requires a value");
|
|
118
|
+
}
|
|
119
|
+
profileId = value;
|
|
120
|
+
index += 1;
|
|
121
|
+
}
|
|
122
|
+
if (!profileId) {
|
|
123
|
+
throw new Error("--profile is required");
|
|
124
|
+
}
|
|
125
|
+
return { command: "config-print", profileId };
|
|
126
|
+
}
|
|
127
|
+
function parseTarget(value) {
|
|
128
|
+
if (value === "opencode" || value === "claude-code") {
|
|
129
|
+
return value;
|
|
130
|
+
}
|
|
131
|
+
throw new Error(`Unknown setup target: ${value}`);
|
|
132
|
+
}
|
|
133
|
+
function parsePort(value) {
|
|
134
|
+
const port = Number(value);
|
|
135
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
136
|
+
throw new Error(`Invalid port: ${value}`);
|
|
137
|
+
}
|
|
138
|
+
return port;
|
|
139
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createInterface } from "node:readline/promises";
|
|
3
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
4
|
+
import { getDefaultToolConfigPath, readToolConfig, upsertProviderProfile } from "../core/tool-config.js";
|
|
5
|
+
import { discoverOpenCodeConfigs, getDefaultConfigPath, writeProviderToConfig } from "../core/config.js";
|
|
6
|
+
import { buildProviderConfig, validateAndFetchModels } from "../core/provider.js";
|
|
7
|
+
import { startProxyServer } from "../proxy/server.js";
|
|
8
|
+
import { getDefaultClaudeSettingsPath, writeClaudeCodeSettings } from "../targets/claude-code.js";
|
|
9
|
+
import { HelpRequested, parseModelSelection } from "./args.js";
|
|
10
|
+
import { commandUsage, parseCommand } from "./commands.js";
|
|
11
|
+
import { canUseTui, multiSelectChoices, renderIntro, selectChoice } from "./tui.js";
|
|
12
|
+
async function main() {
|
|
13
|
+
try {
|
|
14
|
+
const command = parseCommand(process.argv.slice(2));
|
|
15
|
+
await runCommand(command);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (error instanceof HelpRequested) {
|
|
19
|
+
console.log(commandUsage());
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
23
|
+
process.exitCode = 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export async function runCommand(command) {
|
|
27
|
+
if (command.command === "opencode") {
|
|
28
|
+
await runCli(command.options);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (command.command === "setup") {
|
|
32
|
+
await runSetup(command);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (command.command === "config-print") {
|
|
36
|
+
const config = await readToolConfig();
|
|
37
|
+
const profile = config.profiles[command.profileId];
|
|
38
|
+
if (!profile) {
|
|
39
|
+
throw new Error(`Unknown provider profile: ${command.profileId}`);
|
|
40
|
+
}
|
|
41
|
+
output.write(`${JSON.stringify(profile, null, 2)}\n`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const config = await readToolConfig();
|
|
45
|
+
const server = await startProxyServer({
|
|
46
|
+
profileId: command.profileId,
|
|
47
|
+
config,
|
|
48
|
+
host: command.host,
|
|
49
|
+
port: command.port
|
|
50
|
+
});
|
|
51
|
+
output.write(`model-provider-x proxy listening at ${server.baseURL}\n`);
|
|
52
|
+
await new Promise((resolve) => {
|
|
53
|
+
const stop = async () => {
|
|
54
|
+
await server.close();
|
|
55
|
+
resolve();
|
|
56
|
+
};
|
|
57
|
+
process.once("SIGINT", stop);
|
|
58
|
+
process.once("SIGTERM", stop);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export async function runCli(options) {
|
|
62
|
+
const rl = createInterface({ input, output });
|
|
63
|
+
try {
|
|
64
|
+
output.write(canUseTui() ? renderIntro() : "model-provider-x\n\n");
|
|
65
|
+
const providerName = await requiredOption(rl, options.providerName, "Provider name");
|
|
66
|
+
const providerId = await requiredOption(rl, options.providerId ?? slugify(providerName), "Provider id");
|
|
67
|
+
const baseURL = await requiredOption(rl, options.baseURL, "API base URL");
|
|
68
|
+
const apiKey = options.apiKey ?? (await rl.question("API key (optional): "));
|
|
69
|
+
output.write("Fetching models...\n");
|
|
70
|
+
const fetched = await validateAndFetchModels({ baseURL, apiKey });
|
|
71
|
+
const selectedModels = options.models ??
|
|
72
|
+
(canUseTui()
|
|
73
|
+
? await multiSelectChoices("Select models", fetched.models.map((model) => ({ label: model, value: model })))
|
|
74
|
+
: parseModelSelection(await rl.question(formatModelPrompt(fetched.models)), fetched.models));
|
|
75
|
+
const fragment = buildProviderConfig({
|
|
76
|
+
providerId,
|
|
77
|
+
providerName,
|
|
78
|
+
baseURL: fetched.baseURL,
|
|
79
|
+
apiKey,
|
|
80
|
+
models: selectedModels
|
|
81
|
+
});
|
|
82
|
+
const provider = fragment.provider[providerId];
|
|
83
|
+
const json = JSON.stringify(fragment, null, 2);
|
|
84
|
+
if (options.print) {
|
|
85
|
+
output.write(`${json}\n`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const targetPath = options.configPath ?? (await chooseConfigPath(rl, providerId, options.yes));
|
|
89
|
+
if (!targetPath) {
|
|
90
|
+
output.write(`${json}\n`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (apiKey.trim()) {
|
|
94
|
+
output.write("Warning: the API key will be written to the selected OpenCode config.\n");
|
|
95
|
+
}
|
|
96
|
+
const result = await writeProviderToConfig({ targetPath, providerId, provider });
|
|
97
|
+
output.write(`Updated ${result.targetPath}\n`);
|
|
98
|
+
if (result.backupPath) {
|
|
99
|
+
output.write(`Backup: ${result.backupPath}\n`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
rl.close();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function runSetup(command) {
|
|
107
|
+
if (command.target === "opencode") {
|
|
108
|
+
await runCli(command.options);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
await runClaudeCodeSetup(command);
|
|
112
|
+
}
|
|
113
|
+
async function runClaudeCodeSetup(command) {
|
|
114
|
+
const rl = createInterface({ input, output });
|
|
115
|
+
try {
|
|
116
|
+
output.write(canUseTui() ? renderIntro() : "model-provider-x\n\n");
|
|
117
|
+
const providerName = await requiredOption(rl, command.options.providerName, "Provider name");
|
|
118
|
+
const providerId = await requiredOption(rl, command.profileId ?? command.options.providerId ?? slugify(providerName), "Provider id");
|
|
119
|
+
const baseURL = await requiredOption(rl, command.options.baseURL, "API base URL");
|
|
120
|
+
const apiKey = command.options.apiKey ?? (await rl.question("Upstream API key (optional): "));
|
|
121
|
+
output.write("Fetching models...\n");
|
|
122
|
+
const fetched = await validateAndFetchModels({ baseURL, apiKey });
|
|
123
|
+
const selectedModels = command.options.models ??
|
|
124
|
+
(canUseTui()
|
|
125
|
+
? await multiSelectChoices("Select Claude Code gateway models", fetched.models.map((model) => ({ label: model, value: model })))
|
|
126
|
+
: parseModelSelection(await rl.question(formatModelPrompt(fetched.models)), fetched.models));
|
|
127
|
+
const toolConfigPath = getDefaultToolConfigPath();
|
|
128
|
+
const config = await upsertProviderProfile(toolConfigPath, {
|
|
129
|
+
id: providerId,
|
|
130
|
+
name: providerName,
|
|
131
|
+
baseURL: fetched.baseURL,
|
|
132
|
+
apiKey: apiKey.trim() || undefined,
|
|
133
|
+
models: selectedModels
|
|
134
|
+
}, {
|
|
135
|
+
host: command.host,
|
|
136
|
+
port: command.port
|
|
137
|
+
});
|
|
138
|
+
const proxyBaseURL = `http://${config.proxy.host}:${config.proxy.port}`;
|
|
139
|
+
const result = await writeClaudeCodeSettings({
|
|
140
|
+
targetPath: getDefaultClaudeSettingsPath(),
|
|
141
|
+
proxy: {
|
|
142
|
+
baseURL: proxyBaseURL,
|
|
143
|
+
authToken: config.proxy.authToken,
|
|
144
|
+
enableModelDiscovery: true,
|
|
145
|
+
defaultModel: command.defaultModel
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
output.write(`Saved profile ${providerId} to ${toolConfigPath}\n`);
|
|
149
|
+
output.write(`Updated Claude Code settings: ${result.targetPath}\n`);
|
|
150
|
+
if (result.backupPath) {
|
|
151
|
+
output.write(`Backup: ${result.backupPath}\n`);
|
|
152
|
+
}
|
|
153
|
+
output.write(`Run proxy: model-provider-x proxy --profile ${providerId}\n`);
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
rl.close();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async function requiredOption(rl, value, label) {
|
|
160
|
+
const answer = value ?? (await rl.question(`${label}: `));
|
|
161
|
+
if (!answer.trim()) {
|
|
162
|
+
throw new Error(`${label} is required`);
|
|
163
|
+
}
|
|
164
|
+
return answer.trim();
|
|
165
|
+
}
|
|
166
|
+
async function chooseConfigPath(rl, providerId, yes) {
|
|
167
|
+
const configs = await discoverOpenCodeConfigs({ providerId });
|
|
168
|
+
if (configs.length === 0) {
|
|
169
|
+
const defaultPath = getDefaultConfigPath();
|
|
170
|
+
if (yes) {
|
|
171
|
+
return defaultPath;
|
|
172
|
+
}
|
|
173
|
+
if (canUseTui()) {
|
|
174
|
+
return selectChoice("No OpenCode config found", [
|
|
175
|
+
{ label: `Create ${defaultPath}`, value: defaultPath },
|
|
176
|
+
{ label: "Print JSON only", value: undefined, hint: "copy it manually" }
|
|
177
|
+
]);
|
|
178
|
+
}
|
|
179
|
+
const answer = await rl.question(`No OpenCode config found. Create ${defaultPath}? [Y/n/print] `);
|
|
180
|
+
if (!answer.trim() || answer.trim().toLowerCase() === "y") {
|
|
181
|
+
return defaultPath;
|
|
182
|
+
}
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
output.write("OpenCode configs:\n");
|
|
186
|
+
configs.forEach((config, index) => {
|
|
187
|
+
output.write(`${index + 1}. ${describeConfig(config)}\n`);
|
|
188
|
+
});
|
|
189
|
+
if (yes) {
|
|
190
|
+
return configs.find((config) => config.writable)?.path;
|
|
191
|
+
}
|
|
192
|
+
if (canUseTui()) {
|
|
193
|
+
return selectChoice("Choose install target", [
|
|
194
|
+
...configs.map((config) => ({
|
|
195
|
+
label: config.label,
|
|
196
|
+
value: config.path,
|
|
197
|
+
hint: `${config.path}${config.hasProvider ? " (provider exists)" : ""}`,
|
|
198
|
+
disabled: !config.writable
|
|
199
|
+
})),
|
|
200
|
+
{ label: "Print JSON only", value: undefined, hint: "copy it manually" }
|
|
201
|
+
]);
|
|
202
|
+
}
|
|
203
|
+
const answer = await rl.question("Choose config number, enter a path, or type print: ");
|
|
204
|
+
if (answer.trim().toLowerCase() === "print") {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
const index = Number(answer.trim());
|
|
208
|
+
if (Number.isInteger(index) && configs[index - 1]) {
|
|
209
|
+
return configs[index - 1].path;
|
|
210
|
+
}
|
|
211
|
+
if (answer.trim()) {
|
|
212
|
+
return answer.trim();
|
|
213
|
+
}
|
|
214
|
+
return configs.find((config) => config.writable)?.path;
|
|
215
|
+
}
|
|
216
|
+
function describeConfig(config) {
|
|
217
|
+
const flags = [config.writable ? "writable" : "read-only", config.hasProvider ? "provider exists" : undefined]
|
|
218
|
+
.filter(Boolean)
|
|
219
|
+
.join(", ");
|
|
220
|
+
return `${config.label} - ${config.path} (${flags})`;
|
|
221
|
+
}
|
|
222
|
+
function formatModelPrompt(models) {
|
|
223
|
+
const lines = models.map((model, index) => `${index + 1}. ${model}`).join("\n");
|
|
224
|
+
return `Models:\n${lines}\nSelect models by number, range, name, comma list, or press Enter for all: `;
|
|
225
|
+
}
|
|
226
|
+
function slugify(value) {
|
|
227
|
+
return value
|
|
228
|
+
.trim()
|
|
229
|
+
.toLowerCase()
|
|
230
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
231
|
+
.replace(/^-+|-+$/g, "");
|
|
232
|
+
}
|
|
233
|
+
void main();
|