@octen.ai/cli 0.5.5 → 0.6.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.
@@ -1,4 +1,4 @@
1
- import { existsSync, readFileSync, appendFileSync, writeFileSync, mkdirSync } from "node:fs";
1
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
2
2
  import os from "node:os";
3
3
  import { join } from "node:path";
4
4
  import { OctenValidationError } from "../api/errors.js";
@@ -114,6 +114,79 @@ function fishScript(model) {
114
114
  }
115
115
  return lines.join("\n") + "\n";
116
116
  }
117
+ /**
118
+ * A native zsh completion function — a `#compdef octen` file meant to live on
119
+ * `fpath` and be autoloaded by `compinit`. Unlike the `bashcompinit` `eval` shim,
120
+ * a file on `fpath` is re-discovered every time `compinit` runs, so a later
121
+ * `compinit` (e.g. appended by another tool's installer) reloads it instead of
122
+ * silently dropping the registration. This is the order-robust install target.
123
+ */
124
+ function zshCompletionFile(model) {
125
+ const subs = model.subcommands.join(" ");
126
+ const globals = model.globalFlags.join(" ");
127
+ const branches = model.subcommands
128
+ .map((sub) => {
129
+ const flags = [...(model.subFlags[sub] ?? []), ...model.globalFlags].join(" ");
130
+ return ` ${sub})\n opts="${flags}"\n ;;`;
131
+ })
132
+ .join("\n");
133
+ return `#compdef octen
134
+ # octen zsh completion (native, fpath-autoloaded). Do not edit by hand;
135
+ # regenerate with: octen completion zsh --install
136
+ _octen() {
137
+ local cur sub i opts
138
+ cur=\${words[CURRENT]}
139
+ sub=""
140
+ for (( i = 2; i < CURRENT; i++ )); do
141
+ case \${words[i]} in
142
+ -*) ;;
143
+ *) sub=\${words[i]}; break ;;
144
+ esac
145
+ done
146
+ if [[ -z $sub ]]; then
147
+ opts="${subs} ${globals}"
148
+ else
149
+ case $sub in
150
+ ${branches}
151
+ *)
152
+ opts="${globals}"
153
+ ;;
154
+ esac
155
+ fi
156
+ compadd -- \${=opts}
157
+ }
158
+ _octen "$@"
159
+ `;
160
+ }
161
+ /** Native bash completion file body (function + `complete`), for the bash-
162
+ * completion user dir which lazy-loads it on demand regardless of rc ordering. */
163
+ function bashCompletionFile(model) {
164
+ return `${bashFunction(model)}\n`;
165
+ }
166
+ const ZSH_BLOCK_START = "# >>> octen completion >>>";
167
+ const ZSH_BLOCK_END = "# <<< octen completion <<<";
168
+ /** The `~/.zshrc` block that puts our completions dir on `fpath` and (re)runs
169
+ * `compinit` so the `_octen` file is discovered no matter where it lands. */
170
+ function zshRcBlock() {
171
+ return [
172
+ ZSH_BLOCK_START,
173
+ 'fpath=("$HOME/.octen/completions" $fpath)',
174
+ "autoload -Uz compinit && compinit",
175
+ ZSH_BLOCK_END,
176
+ ].join("\n");
177
+ }
178
+ /** Remove the legacy `# octen CLI completion` + `eval` block older versions
179
+ * appended, so re-running `--install` cleanly upgrades to the fpath approach. */
180
+ function stripLegacyEvalBlock(contents, shell) {
181
+ const evalLine = `eval "$(octen completion ${shell})"`;
182
+ const legacyBlock = `\n# octen CLI completion\n${evalLine}\n`;
183
+ const withoutBlock = contents.split(legacyBlock).join("");
184
+ // Also drop any bare eval line left on its own (e.g. hand-placed).
185
+ return withoutBlock
186
+ .split("\n")
187
+ .filter((l) => l.trim() !== evalLine)
188
+ .join("\n");
189
+ }
117
190
  export function buildCompletionScript(program, shell) {
118
191
  const model = buildModel(program);
119
192
  switch (shell) {
@@ -128,9 +201,13 @@ export function buildCompletionScript(program, shell) {
128
201
  }
129
202
  }
130
203
  /**
131
- * Persist completion into the user's shell config.
132
- * - bash/zsh: append an idempotent `eval "$(octen completion <shell>)"` line to
133
- * the rc file, so it stays in sync with the CLI and loads in new shells.
204
+ * Persist completion into the user's shell so it survives re-initialization.
205
+ * - zsh: write a native `#compdef` `_octen` file into `~/.octen/completions`
206
+ * (an fpath dir) and ensure `~/.zshrc` adds that dir to `fpath` + runs
207
+ * `compinit`. Because it lives on `fpath`, a later `compinit` re-discovers it
208
+ * instead of dropping it — the failure mode of the old `eval` line.
209
+ * - bash: write a native completion file into the bash-completion user dir,
210
+ * which is lazy-loaded on demand regardless of rc ordering.
134
211
  * - fish: write the generated script into the auto-loaded completions dir.
135
212
  */
136
213
  function installCompletion(shell, program, home) {
@@ -141,15 +218,30 @@ function installCompletion(shell, program, home) {
141
218
  writeFileSync(file, buildCompletionScript(program, "fish"), "utf8");
142
219
  return { action: "written", path: file };
143
220
  }
144
- const rc = shell === "zsh" ? join(home, ".zshrc") : join(home, ".bashrc");
145
- const line = `eval "$(octen completion ${shell})"`;
146
- const existing = existsSync(rc) ? readFileSync(rc, "utf8") : "";
147
- if (existing.includes(line)) {
148
- return { action: "already", path: rc };
221
+ const model = buildModel(program);
222
+ if (shell === "zsh") {
223
+ // 1) (Re)write the native completion file into an fpath dir.
224
+ const dir = join(home, ".octen/completions");
225
+ mkdirSync(dir, { recursive: true });
226
+ const file = join(dir, "_octen");
227
+ writeFileSync(file, zshCompletionFile(model), "utf8");
228
+ // 2) Ensure ~/.zshrc puts that dir on fpath and runs compinit.
229
+ const rc = join(home, ".zshrc");
230
+ const existing = existsSync(rc) ? readFileSync(rc, "utf8") : "";
231
+ if (existing.includes(ZSH_BLOCK_START)) {
232
+ return { action: "already", path: rc, file };
233
+ }
234
+ const cleaned = stripLegacyEvalBlock(existing, "zsh");
235
+ const prefix = cleaned === "" || cleaned.endsWith("\n") ? "" : "\n";
236
+ writeFileSync(rc, `${cleaned}${prefix}\n${zshRcBlock()}\n`, "utf8");
237
+ return { action: "installed", path: rc, file };
149
238
  }
150
- const prefix = existing === "" || existing.endsWith("\n") ? "" : "\n";
151
- appendFileSync(rc, `${prefix}\n# octen CLI completion\n${line}\n`, "utf8");
152
- return { action: "installed", path: rc };
239
+ // bash: drop a native completion file into the bash-completion user dir.
240
+ const dir = join(home, ".local/share/bash-completion/completions");
241
+ mkdirSync(dir, { recursive: true });
242
+ const file = join(dir, "octen");
243
+ writeFileSync(file, bashCompletionFile(model), "utf8");
244
+ return { action: "written", path: file };
153
245
  }
154
246
  export function registerCompletion(program, internal = {}) {
155
247
  program
@@ -166,18 +258,21 @@ export function registerCompletion(program, internal = {}) {
166
258
  }
167
259
  const home = internal.home ?? os.homedir();
168
260
  const result = installCompletion(shell, program, home);
169
- if (result.action === "already") {
170
- process.stdout.write(`completion already installed in ${quotePath(result.path)}\n`);
171
- }
172
- else if (result.action === "written") {
261
+ if (shell === "fish") {
173
262
  process.stdout.write(`wrote fish completion to ${quotePath(result.path)} (new shells pick it up automatically)\n`);
263
+ return;
174
264
  }
175
- else {
176
- process.stdout.write(`added octen completion to ${quotePath(result.path)}\n`);
265
+ if (shell === "bash") {
266
+ process.stdout.write(`wrote octen bash completion to ${quotePath(result.path)} (new shells pick it up automatically)\n`);
267
+ return;
177
268
  }
178
- if (shell !== "fish") {
179
- const rc = shell === "zsh" ? "~/.zshrc" : "~/.bashrc";
180
- process.stdout.write(`run \`source ${rc}\` (or open a new terminal) to activate it now\n`);
269
+ // zsh: file on fpath + rc block.
270
+ if (result.action === "already") {
271
+ process.stdout.write(`octen completion already configured in ${quotePath(result.path)} (refreshed ${quotePath(result.file)})\n`);
272
+ }
273
+ else {
274
+ process.stdout.write(`installed octen completion: wrote ${quotePath(result.file)} and updated ${quotePath(result.path)}\n`);
181
275
  }
276
+ process.stdout.write("run `source ~/.zshrc` (or open a new terminal) to activate it now\n");
182
277
  });
183
278
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octen.ai/cli",
3
- "version": "0.5.5",
3
+ "version": "0.6.0",
4
4
  "description": "Command-line tool for Octen — web search, extract, chat, embeddings, and one-command MCP/Skills setup.",
5
5
  "type": "module",
6
6
  "license": "MIT",