@4yi/cli 0.1.2 → 0.1.4

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/opencode.mjs +52 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4yi/cli",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "4YI command-line launcher for OAuth login and OpenCode runtime",
5
5
  "type": "module",
6
6
  "bin": {
package/src/opencode.mjs CHANGED
@@ -6,18 +6,43 @@ import { pathsForHome, ensureDir } from "./config.mjs";
6
6
  import { requestJson } from "./http.mjs";
7
7
 
8
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
+ }
9
25
 
10
26
  export function buildOpenCodeConfig({ modelConfig, tokenEnv = "FOURYI_CLI_TOKEN", orgEnv = "FOURYI_ORG_ID" }) {
11
27
  const models = {};
12
- for (const model of modelConfig.models || []) {
28
+ const claudeModels = (modelConfig.models || []).filter(isClaudeModel);
29
+ for (const model of claudeModels) {
13
30
  models[model.id] = {
14
31
  name: model.display_name || model.id,
15
- ...(model.context_length ? { limit: { context: model.context_length } } : {}),
32
+ limit: {
33
+ context: modelContextLimit(model),
34
+ output: modelOutputLimit(model),
35
+ },
16
36
  };
17
37
  }
38
+ const defaultModel = claudeModels.some((model) => model.id === modelConfig.default_model)
39
+ ? modelConfig.default_model
40
+ : claudeModels[0]?.id;
41
+
18
42
  return {
19
43
  "$schema": "https://opencode.ai/config.json",
20
- model: modelConfig.default_model ? `4yi/${modelConfig.default_model}` : undefined,
44
+ model: defaultModel ? `4yi/${defaultModel}` : undefined,
45
+ enabled_providers: ["4yi"],
21
46
  provider: {
22
47
  "4yi": {
23
48
  name: "4YI",
@@ -35,6 +60,20 @@ export function buildOpenCodeConfig({ modelConfig, tokenEnv = "FOURYI_CLI_TOKEN"
35
60
  };
36
61
  }
37
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
+
38
77
  export function ensureOpenCodeRuntime({ home = os.homedir(), stdout = console.log } = {}) {
39
78
  const paths = pathsForHome(home);
40
79
  ensureDir(paths.opencodeDir);
@@ -64,21 +103,25 @@ export async function runCode({ session, home = os.homedir(), argv = [], stdout
64
103
  if (!session?.token) throw new Error("Not signed in. Run `4yi login`.");
65
104
  const modelConfig = await requestJson(session.baseUrl, "/api/cli/models", { token: session.token });
66
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.");
67
107
 
68
108
  const paths = pathsForHome(home);
69
109
  ensureDir(paths.opencodeConfigDir);
110
+ for (const dir of ["config", "data", "cache", "state"]) {
111
+ ensureDir(path.join(paths.opencodeConfigDir, "xdg", dir));
112
+ }
70
113
  fs.writeFileSync(paths.opencodeConfigFile, `${JSON.stringify(buildOpenCodeConfig({ modelConfig }), null, 2)}\n`, { mode: 0o600 });
71
114
 
72
115
  const bin = ensureOpenCodeRuntime({ home, stdout });
73
116
  stdout("Launching OpenCode with 4YI models...");
74
117
  const child = spawn(bin, argv, {
75
118
  stdio: "inherit",
76
- env: {
77
- ...process.env,
78
- FOURYI_CLI_TOKEN: session.token,
79
- FOURYI_ORG_ID: session.org?.id || "",
80
- OPENCODE_CONFIG: paths.opencodeConfigFile,
81
- },
119
+ env: opencodeEnv({
120
+ home,
121
+ token: session.token,
122
+ orgId: session.org?.id || "",
123
+ configFile: paths.opencodeConfigFile,
124
+ }),
82
125
  });
83
126
  return new Promise((resolve) => child.on("exit", (code) => resolve(code ?? 0)));
84
127
  }