@4yi-dev/cli 0.1.5
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/README.md +36 -0
- package/bin/4yi.mjs +39 -0
- package/package.json +28 -0
- package/src/auth.mjs +57 -0
- package/src/config.mjs +50 -0
- package/src/http.mjs +29 -0
- package/src/opencode.mjs +127 -0
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# 4YI CLI
|
|
2
|
+
|
|
3
|
+
4YI CLI signs in through the 4YI web app and launches OpenCode with 4YI model routing.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Temporary dev environment:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g @4yi-dev/cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Production environment:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install -g @4yi-dev/cli
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Both packages install the `4yi` command. The dev package points at `https://xclaw-dev.bieases.com`; the production package points at `https://app.4yi.ai`.
|
|
20
|
+
|
|
21
|
+
## Commands
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
4yi login
|
|
25
|
+
4yi whoami
|
|
26
|
+
4yi code
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`4yi code` installs OpenCode under `~/.4yi/vendor/opencode`, writes an OpenCode config under `~/.4yi/opencode/opencode.json`, and launches OpenCode with a 4YI provider.
|
|
30
|
+
|
|
31
|
+
For local development:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
FOURYI_BASE_URL=http://localhost:3000 node packages/cli/bin/4yi.mjs login
|
|
35
|
+
node packages/cli/bin/4yi.mjs code
|
|
36
|
+
```
|
package/bin/4yi.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { login, loadSession, clearSession } from "../src/auth.mjs";
|
|
3
|
+
import { runCode } from "../src/opencode.mjs";
|
|
4
|
+
|
|
5
|
+
const command = process.argv[2] || "help";
|
|
6
|
+
|
|
7
|
+
if (command === "help" || command === "--help" || command === "-h") {
|
|
8
|
+
console.log("Usage: 4yi <login|whoami|logout|code>");
|
|
9
|
+
process.exit(0);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (command === "--version" || command === "-v") {
|
|
13
|
+
const pkg = await import("../package.json", { with: { type: "json" } });
|
|
14
|
+
console.log(pkg.default.version);
|
|
15
|
+
process.exit(0);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
if (command === "login") {
|
|
20
|
+
await login();
|
|
21
|
+
} else if (command === "whoami") {
|
|
22
|
+
const session = loadSession();
|
|
23
|
+
if (!session.token) throw new Error("Not signed in. Run `4yi login`.");
|
|
24
|
+
console.log(`${session.user?.email || "4yi user"} in ${session.org?.name || session.org?.id}`);
|
|
25
|
+
} else if (command === "logout") {
|
|
26
|
+
clearSession();
|
|
27
|
+
console.log("Signed out.");
|
|
28
|
+
} else if (command === "code") {
|
|
29
|
+
const session = loadSession();
|
|
30
|
+
const code = await runCode({ session, argv: process.argv.slice(3) });
|
|
31
|
+
process.exit(Number(code || 0));
|
|
32
|
+
} else {
|
|
33
|
+
console.error(`Unknown command: ${command}`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
} catch (err) {
|
|
37
|
+
console.error(err.message || String(err));
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@4yi-dev/cli",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "4YI command-line launcher for OAuth login and OpenCode runtime",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"4yi": "bin/4yi.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "node --test",
|
|
16
|
+
"pack:dry": "npm pack --dry-run",
|
|
17
|
+
"stage:dev": "node scripts/stage-package.mjs dev",
|
|
18
|
+
"stage:prod": "node scripts/stage-package.mjs prod",
|
|
19
|
+
"pack:dev": "node scripts/stage-package.mjs dev --pack",
|
|
20
|
+
"pack:prod": "node scripts/stage-package.mjs prod --pack",
|
|
21
|
+
"publish:dev": "node scripts/stage-package.mjs dev --publish",
|
|
22
|
+
"publish:prod": "node scripts/stage-package.mjs prod --publish"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20.0.0"
|
|
26
|
+
},
|
|
27
|
+
"license": "UNLICENSED"
|
|
28
|
+
}
|
package/src/auth.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { normalizeBaseUrl, readConfig, writeConfig } from "./config.mjs";
|
|
4
|
+
import { requestJson } from "./http.mjs";
|
|
5
|
+
|
|
6
|
+
export function loadSession(home = os.homedir()) {
|
|
7
|
+
return readConfig(home);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function saveSession(session, home = os.homedir()) {
|
|
11
|
+
writeConfig(session, home);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function clearSession(home = os.homedir()) {
|
|
15
|
+
const current = readConfig(home);
|
|
16
|
+
writeConfig({ baseUrl: current.baseUrl }, home);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function openBrowser(url) {
|
|
20
|
+
const command = process.platform === "darwin"
|
|
21
|
+
? "open"
|
|
22
|
+
: process.platform === "win32"
|
|
23
|
+
? "cmd"
|
|
24
|
+
: "xdg-open";
|
|
25
|
+
const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
|
|
26
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
27
|
+
child.unref();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function login({
|
|
31
|
+
baseUrl = process.env.FOURYI_BASE_URL,
|
|
32
|
+
home = os.homedir(),
|
|
33
|
+
open = openBrowser,
|
|
34
|
+
stdout = console.log,
|
|
35
|
+
} = {}) {
|
|
36
|
+
const resolvedBaseUrl = normalizeBaseUrl(baseUrl);
|
|
37
|
+
const start = await requestJson(resolvedBaseUrl, "/api/cli/auth/start", { method: "POST" });
|
|
38
|
+
stdout("Opening browser for OAuth web verification...");
|
|
39
|
+
stdout(`If it does not open, visit: ${start.verification_uri_complete}`);
|
|
40
|
+
open(start.verification_uri_complete);
|
|
41
|
+
|
|
42
|
+
const started = Date.now();
|
|
43
|
+
while (Date.now() - started < start.expires_in * 1000) {
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, start.interval * 1000));
|
|
45
|
+
const poll = await requestJson(resolvedBaseUrl, "/api/cli/auth/poll", {
|
|
46
|
+
method: "POST",
|
|
47
|
+
body: JSON.stringify({ device_code: start.device_code }),
|
|
48
|
+
});
|
|
49
|
+
if (poll.status === "approved" && poll.token) {
|
|
50
|
+
const session = await requestJson(resolvedBaseUrl, "/api/cli/session", { token: poll.token });
|
|
51
|
+
saveSession({ baseUrl: resolvedBaseUrl, token: poll.token, org: session.org, user: session.user }, home);
|
|
52
|
+
stdout(`Signed in to ${session.org.name}`);
|
|
53
|
+
return session;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw new Error("OAuth verification expired");
|
|
57
|
+
}
|
package/src/config.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
export const PACKAGE_NAME = "@4yi-dev/cli";
|
|
6
|
+
|
|
7
|
+
const PACKAGE_BASE_URLS = {
|
|
8
|
+
"@4yi-dev/cli": "https://xclaw-dev.bieases.com",
|
|
9
|
+
"@4yi/cli": "https://app.4yi.ai",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function defaultBaseUrlForPackage(packageName = PACKAGE_NAME) {
|
|
13
|
+
return PACKAGE_BASE_URLS[packageName] || PACKAGE_BASE_URLS["@4yi/cli"];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_BASE_URL = defaultBaseUrlForPackage(PACKAGE_NAME);
|
|
17
|
+
|
|
18
|
+
export function normalizeBaseUrl(value) {
|
|
19
|
+
return String(value || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function pathsForHome(home = os.homedir()) {
|
|
23
|
+
const homeDir = path.join(home, ".4yi");
|
|
24
|
+
const opencodeDir = path.join(homeDir, "vendor", "opencode");
|
|
25
|
+
const opencodeConfigDir = path.join(homeDir, "opencode");
|
|
26
|
+
return {
|
|
27
|
+
homeDir,
|
|
28
|
+
configFile: path.join(homeDir, "config.json"),
|
|
29
|
+
opencodeDir,
|
|
30
|
+
opencodePackageFile: path.join(opencodeDir, "package.json"),
|
|
31
|
+
opencodeConfigDir,
|
|
32
|
+
opencodeConfigFile: path.join(opencodeConfigDir, "opencode.json"),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function ensureDir(dir) {
|
|
37
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function readConfig(home = os.homedir()) {
|
|
41
|
+
const { configFile } = pathsForHome(home);
|
|
42
|
+
if (!fs.existsSync(configFile)) return {};
|
|
43
|
+
return JSON.parse(fs.readFileSync(configFile, "utf8"));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function writeConfig(config, home = os.homedir()) {
|
|
47
|
+
const paths = pathsForHome(home);
|
|
48
|
+
ensureDir(paths.homeDir);
|
|
49
|
+
fs.writeFileSync(paths.configFile, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
50
|
+
}
|
package/src/http.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export async function requestJson(baseUrl, path, options = {}) {
|
|
2
|
+
const url = `${baseUrl}${path}`;
|
|
3
|
+
const res = await fetch(url, {
|
|
4
|
+
...options,
|
|
5
|
+
headers: {
|
|
6
|
+
"content-type": "application/json",
|
|
7
|
+
...(options.token ? { authorization: `Bearer ${options.token}` } : {}),
|
|
8
|
+
...(options.headers || {}),
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
const text = await res.text();
|
|
12
|
+
let body = null;
|
|
13
|
+
if (text) {
|
|
14
|
+
try {
|
|
15
|
+
body = JSON.parse(text);
|
|
16
|
+
} catch {
|
|
17
|
+
const contentType = res.headers.get("content-type") || "unknown content type";
|
|
18
|
+
throw new Error(`Expected JSON from ${url} but received ${contentType} (HTTP ${res.status})`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!res.ok) {
|
|
22
|
+
const message = body?.error?.message || body?.error || `HTTP ${res.status}`;
|
|
23
|
+
const err = new Error(message);
|
|
24
|
+
err.status = res.status;
|
|
25
|
+
err.body = body;
|
|
26
|
+
throw err;
|
|
27
|
+
}
|
|
28
|
+
return body;
|
|
29
|
+
}
|
package/src/opencode.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
|
+
import { pathsForHome, ensureDir } from "./config.mjs";
|
|
6
|
+
import { requestJson } from "./http.mjs";
|
|
7
|
+
|
|
8
|
+
const DEFAULT_OPENCODE_PACKAGE = "opencode-ai";
|
|
9
|
+
const DEFAULT_CONTEXT_LIMIT = 200000;
|
|
10
|
+
const DEFAULT_OUTPUT_LIMIT = 8192;
|
|
11
|
+
|
|
12
|
+
function isClaudeModel(model) {
|
|
13
|
+
const id = model?.id || "";
|
|
14
|
+
const name = model?.display_name || "";
|
|
15
|
+
return /claude/i.test(id) || /claude/i.test(name);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function modelContextLimit(model) {
|
|
19
|
+
return model.context_length || model.context_limit || model.input_limit || DEFAULT_CONTEXT_LIMIT;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function modelOutputLimit(model) {
|
|
23
|
+
return model.output_limit || model.max_output_tokens || model.max_tokens || DEFAULT_OUTPUT_LIMIT;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function buildOpenCodeConfig({ modelConfig, tokenEnv = "FOURYI_CLI_TOKEN", orgEnv = "FOURYI_ORG_ID" }) {
|
|
27
|
+
const models = {};
|
|
28
|
+
const claudeModels = (modelConfig.models || []).filter(isClaudeModel);
|
|
29
|
+
for (const model of claudeModels) {
|
|
30
|
+
models[model.id] = {
|
|
31
|
+
name: model.display_name || model.id,
|
|
32
|
+
limit: {
|
|
33
|
+
context: modelContextLimit(model),
|
|
34
|
+
output: modelOutputLimit(model),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const defaultModel = claudeModels.some((model) => model.id === modelConfig.default_model)
|
|
39
|
+
? modelConfig.default_model
|
|
40
|
+
: claudeModels[0]?.id;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
"$schema": "https://opencode.ai/config.json",
|
|
44
|
+
model: defaultModel ? `4yi/${defaultModel}` : undefined,
|
|
45
|
+
enabled_providers: ["4yi"],
|
|
46
|
+
provider: {
|
|
47
|
+
"4yi": {
|
|
48
|
+
name: "4YI",
|
|
49
|
+
npm: "@ai-sdk/openai-compatible",
|
|
50
|
+
options: {
|
|
51
|
+
baseURL: modelConfig.base_url,
|
|
52
|
+
apiKey: `{env:${tokenEnv}}`,
|
|
53
|
+
headers: {
|
|
54
|
+
"X-4YI-Org-ID": `{env:${orgEnv}}`,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
models,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function opencodeEnv({ home = os.homedir(), token, orgId = "", configFile, baseEnv = process.env } = {}) {
|
|
64
|
+
const xdgRoot = path.join(pathsForHome(home).opencodeConfigDir, "xdg");
|
|
65
|
+
return {
|
|
66
|
+
...baseEnv,
|
|
67
|
+
FOURYI_CLI_TOKEN: token,
|
|
68
|
+
FOURYI_ORG_ID: orgId,
|
|
69
|
+
OPENCODE_CONFIG: configFile,
|
|
70
|
+
XDG_CONFIG_HOME: path.join(xdgRoot, "config"),
|
|
71
|
+
XDG_DATA_HOME: path.join(xdgRoot, "data"),
|
|
72
|
+
XDG_CACHE_HOME: path.join(xdgRoot, "cache"),
|
|
73
|
+
XDG_STATE_HOME: path.join(xdgRoot, "state"),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function ensureOpenCodeRuntime({ home = os.homedir(), stdout = console.log } = {}) {
|
|
78
|
+
const paths = pathsForHome(home);
|
|
79
|
+
ensureDir(paths.opencodeDir);
|
|
80
|
+
if (!fs.existsSync(paths.opencodePackageFile)) {
|
|
81
|
+
fs.writeFileSync(paths.opencodePackageFile, JSON.stringify({
|
|
82
|
+
private: true,
|
|
83
|
+
type: "module",
|
|
84
|
+
dependencies: {},
|
|
85
|
+
}, null, 2));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const bin = path.join(paths.opencodeDir, "node_modules", ".bin", process.platform === "win32" ? "opencode.cmd" : "opencode");
|
|
89
|
+
if (fs.existsSync(bin)) return bin;
|
|
90
|
+
|
|
91
|
+
const opencodePackage = process.env.FOURYI_OPENCODE_PACKAGE || DEFAULT_OPENCODE_PACKAGE;
|
|
92
|
+
stdout(`Installing OpenCode runtime into ${paths.opencodeDir}...`);
|
|
93
|
+
const result = spawnSync("npm", ["install", "--prefix", paths.opencodeDir, opencodePackage, "@ai-sdk/openai-compatible"], {
|
|
94
|
+
stdio: "inherit",
|
|
95
|
+
});
|
|
96
|
+
if (result.status !== 0) throw new Error("OpenCode runtime install failed");
|
|
97
|
+
if (!fs.existsSync(bin)) throw new Error(`OpenCode binary not found at ${bin}`);
|
|
98
|
+
stdout("OpenCode runtime installed.");
|
|
99
|
+
return bin;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function runCode({ session, home = os.homedir(), argv = [], stdout = console.log } = {}) {
|
|
103
|
+
if (!session?.token) throw new Error("Not signed in. Run `4yi login`.");
|
|
104
|
+
const modelConfig = await requestJson(session.baseUrl, "/api/cli/models", { token: session.token });
|
|
105
|
+
if (!modelConfig.default_model) throw new Error("No chat models available for this organization.");
|
|
106
|
+
if (!(modelConfig.models || []).some(isClaudeModel)) throw new Error("No Claude models available for this organization.");
|
|
107
|
+
|
|
108
|
+
const paths = pathsForHome(home);
|
|
109
|
+
ensureDir(paths.opencodeConfigDir);
|
|
110
|
+
for (const dir of ["config", "data", "cache", "state"]) {
|
|
111
|
+
ensureDir(path.join(paths.opencodeConfigDir, "xdg", dir));
|
|
112
|
+
}
|
|
113
|
+
fs.writeFileSync(paths.opencodeConfigFile, `${JSON.stringify(buildOpenCodeConfig({ modelConfig }), null, 2)}\n`, { mode: 0o600 });
|
|
114
|
+
|
|
115
|
+
const bin = ensureOpenCodeRuntime({ home, stdout });
|
|
116
|
+
stdout("Launching OpenCode with 4YI models...");
|
|
117
|
+
const child = spawn(bin, argv, {
|
|
118
|
+
stdio: "inherit",
|
|
119
|
+
env: opencodeEnv({
|
|
120
|
+
home,
|
|
121
|
+
token: session.token,
|
|
122
|
+
orgId: session.org?.id || "",
|
|
123
|
+
configFile: paths.opencodeConfigFile,
|
|
124
|
+
}),
|
|
125
|
+
});
|
|
126
|
+
return new Promise((resolve) => child.on("exit", (code) => resolve(code ?? 0)));
|
|
127
|
+
}
|