@hyperez/regna-code 0.1.5 → 0.1.7

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 CHANGED
@@ -62,6 +62,8 @@ For CI or scripted use, set `REGNA_API_KEY` directly; it takes priority over the
62
62
  |---|---|
63
63
  | `regna login` | Log in to Regna and store your API key at `~/.regna/auth.json`. |
64
64
  | `regna logout` | Remove stored credentials. |
65
+ | `/exit` | Exit Regna Code from inside a session. |
66
+ | `/compact` | Compact the session context (also runs automatically as it fills). |
65
67
  | `/regna-coder` | Switch to a coding model (prefers an id containing `coder`, else default). |
66
68
  | `/regna-general` | Switch to a general model (prefers `instruct`, else default). |
67
69
  | `/regna-brand` | Toggle the Regna Code header/title branding. |
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{spawn as v}from"node:child_process";import{fileURLToPath as P}from"node:url";import{dirname as m,join as o}from"node:path";import{existsSync as p,readFileSync as w,writeFileSync as I,mkdirSync as N,unlinkSync as G,chmodSync as L}from"node:fs";import{homedir as k}from"node:os";import{createInterface as U}from"node:readline/promises";var $=m(P(import.meta.url)),c=m($),g=o(c,"extensions"),_=o(c,"themes","regna.json"),f=o(c,"APPEND_SYSTEM.md"),T="https://regnax.ai/v1",D="regna/regna-pro",h=o(k(),".regna"),d=o(h,"auth.json"),x=o(h,"engine"),a=process.argv.slice(2),u=(...e)=>a.some(r=>e.some(n=>!!(r===n||r.startsWith(`${n}=`)||/^-[a-z]$/i.test(n)&&r.startsWith(n)&&r.length>n.length)));function E(){for(let e of[o(c,"package.json"),o(c,"..","package.json")])try{let r=JSON.parse(w(e,"utf8")).version;if(r)return`v${r}`}catch{}return""}var C=`Regna Code ${E()} - a terminal coding agent on Regna
2
+ import{spawn as v}from"node:child_process";import{fileURLToPath as P}from"node:url";import{dirname as f,join as o}from"node:path";import{existsSync as p,readFileSync as w,writeFileSync as I,mkdirSync as N,unlinkSync as G,chmodSync as L}from"node:fs";import{homedir as k}from"node:os";import{createInterface as U}from"node:readline/promises";var $=f(P(import.meta.url)),c=f($),g=o(c,"extensions"),_=o(c,"themes","regna.json"),m=o(c,"APPEND_SYSTEM.md"),T="https://regnax.ai/v1",D="regna/regna-pro",h=o(k(),".regna"),d=o(h,"auth.json"),x=o(h,"engine"),a=process.argv.slice(2),u=(...e)=>a.some(t=>e.some(n=>!!(t===n||t.startsWith(`${n}=`)||/^-[a-z]$/i.test(n)&&t.startsWith(n)&&t.length>n.length)));function E(){for(let e of[o(c,"package.json"),o(c,"..","package.json")])try{let t=JSON.parse(w(e,"utf8")).version;if(t)return`v${t}`}catch{}return""}var C=`Regna Code ${E()} - a terminal coding agent on Regna
3
3
 
4
4
  Usage:
5
5
  regna [message...] start interactive, optionally with a first message
@@ -15,6 +15,8 @@ In-session commands:
15
15
  /regna-general switch to a general model
16
16
  /regna-brand toggle the Regna Code branding
17
17
  /regna-docs <q> document-grounded query (requires REGNA_DOCS_ENABLED=1)
18
+ /exit exit Regna Code
19
+ /compact compact the session context
18
20
 
19
21
  Environment:
20
22
  REGNA_API_KEY Regna API key (optional once logged in)
@@ -23,17 +25,17 @@ Environment:
23
25
  REGNA_POLICY network egress guard: off | warn | enforce
24
26
 
25
27
  Docs: https://github.com/HyperEZ/regna-code
26
- `;function A(){try{let e=JSON.parse(w(d,"utf8"));return e&&typeof e.apiKey=="string"?e:null}catch{return null}}function B(e,r){N(h,{recursive:!0}),I(d,`${JSON.stringify({apiKey:e,baseUrl:r},null,2)}
27
- `);try{L(d,384)}catch{}}function O(){try{return G(d),!0}catch{return!1}}function j(e){try{return`${new URL(e).origin}/console`}catch{return"https://regnax.ai/console"}}function F(e){try{let[r,n]=process.platform==="darwin"?["open",[e]]:process.platform==="win32"?["cmd",["/c","start","",e]]:["xdg-open",[e]];v(r,n,{stdio:"ignore",detached:!0}).unref()}catch{}}async function K(e,r){try{let n=new AbortController,s=setTimeout(()=>n.abort(),8e3),t=await fetch(`${e.replace(/\/+$/,"")}/models`,{headers:{Authorization:`Bearer ${r}`},signal:n.signal});return clearTimeout(s),t.status===401||t.status===403?"invalid":t.ok?"ok":"unknown"}catch{return"unknown"}}async function S(){let e=(process.env.REGNA_BASE_URL||"").trim()||A()?.baseUrl||T,r=j(e);process.stdout.write(`
28
+ `;function A(){try{let e=JSON.parse(w(d,"utf8"));return e&&typeof e.apiKey=="string"?e:null}catch{return null}}function B(e,t){N(h,{recursive:!0}),I(d,`${JSON.stringify({apiKey:e,baseUrl:t},null,2)}
29
+ `);try{L(d,384)}catch{}}function O(){try{return G(d),!0}catch{return!1}}function j(e){try{return`${new URL(e).origin}/console`}catch{return"https://regnax.ai/console"}}function F(e){try{let[t,n]=process.platform==="darwin"?["open",[e]]:process.platform==="win32"?["cmd",["/c","start","",e]]:["xdg-open",[e]];v(t,n,{stdio:"ignore",detached:!0}).unref()}catch{}}async function K(e,t){try{let n=new AbortController,s=setTimeout(()=>n.abort(),8e3),r=await fetch(`${e.replace(/\/+$/,"")}/models`,{headers:{Authorization:`Bearer ${t}`},signal:n.signal});return clearTimeout(s),r.status===401||r.status===403?"invalid":r.ok?"ok":"unknown"}catch{return"unknown"}}async function S(){let e=(process.env.REGNA_BASE_URL||"").trim()||A()?.baseUrl||T,t=j(e);process.stdout.write(`
28
30
  Regna Code ${E()}
29
31
  Log in to continue.
30
32
 
31
- 1) Get an API key at ${r}
33
+ 1) Get an API key at ${t}
32
34
  2) Paste it below.
33
35
 
34
- `),F(r);let n=U({input:process.stdin,output:process.stdout}),s="";try{s=(await n.question(" API key: ")).trim()}finally{n.close()}s||(process.stderr.write(` No key entered. Aborting.
35
- `),process.exit(1));let t=await K(e,s);return t==="invalid"&&(process.stderr.write(` That key was rejected. Check it and run: regna login
36
- `),process.exit(1)),B(s,e),process.stdout.write(t==="ok"?` Logged in.
36
+ `),F(t);let n=U({input:process.stdin,output:process.stdout}),s="";try{s=(await n.question(" API key: ")).trim()}finally{n.close()}s||(process.stderr.write(` No key entered. Aborting.
37
+ `),process.exit(1));let r=await K(e,s);return r==="invalid"&&(process.stderr.write(` That key was rejected. Check it and run: regna login
38
+ `),process.exit(1)),B(s,e),process.stdout.write(r==="ok"?` Logged in.
37
39
 
38
40
  `:` Saved (could not verify right now).
39
41
 
@@ -41,9 +43,9 @@ Docs: https://github.com/HyperEZ/regna-code
41
43
  `),process.exit(0));a[0]==="login"&&(await S(),process.exit(0));a[0]==="logout"&&(process.stdout.write(O()?`Logged out.
42
44
  `:`Not logged in.
43
45
  `),process.exit(0));var l=(process.env.REGNA_API_KEY||"").trim();if(!l){let e=A();e?.apiKey&&(l=e.apiKey.trim(),!process.env.REGNA_BASE_URL&&e.baseUrl&&(process.env.REGNA_BASE_URL=e.baseUrl))}if(!l)if(process.stdin.isTTY&&process.stdout.isTTY){if(l=await S(),!process.env.REGNA_BASE_URL){let e=A();e?.baseUrl&&(process.env.REGNA_BASE_URL=e.baseUrl)}}else process.stderr.write(`[regna] Not logged in. Run: regna login
44
- `),process.exit(1);process.env.REGNA_API_KEY=l;var i=[],Y=process.env.REGNA_DISCOVER==="1";if(!Y){i.push("--no-extensions");let e=["provider","policy","branding","model","docs-search","docs-analysis"],r=t=>{let R=o(g,`${t}.js`);if(p(R))return R;let y=o(g,`${t}.ts`);return p(y)?y:null},n=e.map(t=>[t,r(t)]),s=n.filter(([,t])=>!t).map(([t])=>t);s.length>0&&(process.stderr.write(`[regna] Missing required extensions: ${s.join(", ")} (in ${g}). Install is corrupted. Aborting.
45
- `),process.exit(1));for(let[,t]of n)i.push("-e",t)}!u("--theme")&&p(_)&&i.push("--theme",_);u("--system-prompt")||(p(f)||(process.stderr.write(`[regna] Missing required prompt: APPEND_SYSTEM.md (${f}). Install is corrupted. Aborting.
46
+ `),process.exit(1);process.env.REGNA_API_KEY=l;var i=[],Y=process.env.REGNA_DISCOVER==="1";if(!Y){i.push("--no-extensions");let e=["provider","policy","branding","model","context","exit","docs-search","docs-analysis"],t=r=>{let R=o(g,`${r}.js`);if(p(R))return R;let y=o(g,`${r}.ts`);return p(y)?y:null},n=e.map(r=>[r,t(r)]),s=n.filter(([,r])=>!r).map(([r])=>r);s.length>0&&(process.stderr.write(`[regna] Missing required extensions: ${s.join(", ")} (in ${g}). Install is corrupted. Aborting.
47
+ `),process.exit(1));for(let[,r]of n)i.push("-e",r)}!u("--theme")&&p(_)&&i.push("--theme",_);u("--system-prompt")||(p(m)||(process.stderr.write(`[regna] Missing required prompt: APPEND_SYSTEM.md (${m}). Install is corrupted. Aborting.
46
48
  (To set the prompt yourself on purpose, run with --system-prompt.)
47
- `),process.exit(1)),i.push("--append-system-prompt",`@${f}`));u("--model","-m")||i.push("--model",process.env.REGNA_MODEL||D);i.push(...a);try{N(x,{recursive:!0})}catch{}var q={...process.env,PI_OFFLINE:"1",PI_CODING_AGENT_DIR:x};function M(){let e=o("node_modules","@earendil-works","pi-coding-agent","dist","cli.js"),r=c;for(let n=0;n<10;n++){let s=o(r,e);if(p(s))return s;let t=m(r);if(t===r)break;r=t}return null}function H(){let e=process.env.REGNA_ENGINE;if(e)return{cmd:e,prefix:[],label:e};let r=M();return r?{cmd:process.execPath,prefix:[r],label:r}:{cmd:"pi",prefix:[],label:"runtime (PATH)"}}var{cmd:z,prefix:J,label:V}=H(),b=v(z,[...J,...i],{stdio:"inherit",env:q});b.on("error",e=>{e&&e.code==="ENOENT"&&(process.stderr.write(`[regna] Runtime executable not found (${V}). Reinstall (npm i -g @hyperez/regna-code) or set REGNA_ENGINE.
49
+ `),process.exit(1)),i.push("--append-system-prompt",`@${m}`));u("--model","-m")||i.push("--model",process.env.REGNA_MODEL||D);i.push(...a);try{N(x,{recursive:!0})}catch{}var q={...process.env,PI_OFFLINE:"1",PI_CODING_AGENT_DIR:x};function M(){let e=o("node_modules","@earendil-works","pi-coding-agent","dist","cli.js"),t=c;for(let n=0;n<10;n++){let s=o(t,e);if(p(s))return s;let r=f(t);if(r===t)break;t=r}return null}function H(){let e=process.env.REGNA_ENGINE;if(e)return{cmd:e,prefix:[],label:e};let t=M();return t?{cmd:process.execPath,prefix:[t],label:t}:{cmd:"pi",prefix:[],label:"runtime (PATH)"}}var{cmd:z,prefix:J,label:V}=H(),b=v(z,[...J,...i],{stdio:"inherit",env:q});b.on("error",e=>{e&&e.code==="ENOENT"&&(process.stderr.write(`[regna] Runtime executable not found (${V}). Reinstall (npm i -g @hyperez/regna-code) or set REGNA_ENGINE.
48
50
  `),process.exit(127)),process.stderr.write(`[regna] Failed to start the runtime: ${e?.message??String(e)}
49
- `),process.exit(1)});b.on("exit",(e,r)=>{if(r){process.kill(process.pid,r);return}process.exit(e??0)});
51
+ `),process.exit(1)});b.on("exit",(e,t)=>{if(t){process.kill(process.pid,t);return}process.exit(e??0)});
@@ -0,0 +1 @@
1
+ var o="regna-context";var a="\u25B0",E="\u25B1",d="\x1B[32m",m="\x1B[33m",L="\x1B[31m",p="\x1B[90m",s="\x1B[39m";function f(n){let t=Math.max(0,Math.min(100,n)),e=Math.max(0,Math.min(10,Math.round(t/100*10))),r=t>90?L:t>70?m:d,u=e>0?`${r}${a.repeat(e)}${s}`:"",c=e<10?`${p}${E.repeat(10-e)}${s}`:"";return`${u}${c} ${t.toFixed(0)}%`}function i(n){if(n.mode!=="tui")return;let t=n.getContextUsage();if(!t||t.percent===null||t.percent===void 0){n.ui.setStatus(o,void 0);return}n.ui.setStatus(o,f(t.percent))}function l(n){n.on("session_start",async(t,e)=>i(e)),n.on("turn_end",async(t,e)=>i(e))}export{f as buildBar,l as default};
@@ -0,0 +1 @@
1
+ function o(e){e.registerCommand("exit",{description:"Exit Regna Code",handler:async(i,n)=>{n.shutdown()}})}export{o as default};
@@ -1,20 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * Postinstall: brand the terminal/tab title as "Regna".
3
+ * Postinstall: small, surgical brand patches to the installed engine.
4
4
  *
5
- * The runtime composes the terminal title as `${APP_TITLE} - <cwd>` and re-asserts it on
6
- * every turn/cwd change, so a one-time setTitle from an extension cannot win the race.
7
- * APP_TITLE is a compiled constant whose fallback is the engine's own mark. We surgically
8
- * rewrite ONLY that fallback string to "Regna" in the installed dependency.
5
+ * The engine ships compiled JS whose constants/built-ins we cannot override at runtime via
6
+ * the extension API. We rewrite a few exact spots in the installed dependency:
9
7
  *
10
- * Scope is deliberately narrow:
11
- * - Only the `APP_TITLE = piConfigName ? APP_NAME : "<x>"` literal is touched.
12
- * - APP_NAME, the config dir name, and the agent-dir env var are left untouched, so the
13
- * launcher's state isolation (PI_CODING_AGENT_DIR) keeps working regardless.
8
+ * 1) Terminal title. The engine composes the title as `${APP_TITLE} - <cwd>` and re-asserts
9
+ * it on every turn/cwd change, so an extension setTitle cannot win. We rewrite ONLY the
10
+ * `APP_TITLE = piConfigName ? APP_NAME : "<x>"` fallback string to "Regna". APP_NAME, the
11
+ * config dir name, and the agent-dir env var are untouched, so the launcher's state
12
+ * isolation (PI_CODING_AGENT_DIR) keeps working regardless.
14
13
  *
15
- * Best effort: any failure is swallowed and the install still succeeds. If the patch does
16
- * not apply (e.g. the engine changed its layout), the only effect is the title stays as the
17
- * engine default. This is idempotent and safe to run on every install.
14
+ * 2) Remove the built-in /quit command. Regna Code exposes /exit instead. We drop the "quit"
15
+ * entry from the built-in slash-command menu and neutralize its handler so /quit no longer
16
+ * triggers shutdown (Ctrl+C/Ctrl+D still exit).
17
+ *
18
+ * Best effort: every patch is wrapped so a failure (e.g. the engine changed its layout) just
19
+ * leaves that aspect at the engine default and never fails the install. All patches are
20
+ * idempotent and safe to run on every install.
18
21
  */
19
22
 
20
23
  import { readFileSync, writeFileSync, existsSync } from "node:fs";
@@ -39,16 +42,37 @@ function findEngineFile(relInsidePackage) {
39
42
  return null;
40
43
  }
41
44
 
42
- try {
43
- const configPath = findEngineFile(join("dist", "config.js"));
44
- if (!configPath) throw new Error("engine config.js not found");
45
- const src = readFileSync(configPath, "utf8");
46
- // Match: APP_TITLE = piConfigName ? APP_NAME : "<anything>"
47
- const re = /(APP_TITLE\s*=\s*piConfigName\s*\?\s*APP_NAME\s*:\s*)"[^"]*"/;
48
- if (re.test(src)) {
49
- const out = src.replace(re, `$1"${TITLE}"`);
50
- if (out !== src) writeFileSync(configPath, out);
45
+ /** Apply one regex replacement to an engine file. No-op if the file or pattern is missing. */
46
+ function patchEngineFile(relInsidePackage, re, replacement) {
47
+ try {
48
+ const path = findEngineFile(relInsidePackage);
49
+ if (!path) return;
50
+ const src = readFileSync(path, "utf8");
51
+ if (!re.test(src)) return; // already patched or layout changed
52
+ const out = src.replace(re, replacement);
53
+ if (out !== src) writeFileSync(path, out);
54
+ } catch {
55
+ /* best effort; never fail the install */
51
56
  }
52
- } catch {
53
- /* cosmetic only; never fail the install */
54
57
  }
58
+
59
+ // 1) Terminal title fallback -> "Regna".
60
+ patchEngineFile(
61
+ join("dist", "config.js"),
62
+ /(APP_TITLE\s*=\s*piConfigName\s*\?\s*APP_NAME\s*:\s*)"[^"]*"/,
63
+ `$1"${TITLE}"`,
64
+ );
65
+
66
+ // 2a) Drop the built-in "quit" entry from the slash-command menu.
67
+ patchEngineFile(
68
+ join("dist", "core", "slash-commands.js"),
69
+ /\{\s*name:\s*"quit",\s*description:\s*`[^`]*`\s*\},?\n?/,
70
+ "",
71
+ );
72
+
73
+ // 2b) Neutralize the /quit handler so it no longer matches (Ctrl+C/Ctrl+D still exit).
74
+ patchEngineFile(
75
+ join("dist", "modes", "interactive", "interactive-mode.js"),
76
+ /if\s*\(\s*text\s*===\s*"\/quit"\s*\)/,
77
+ 'if (text === "/__regna_quit_removed__")',
78
+ );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperez/regna-code",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Regna Code. A terminal coding agent on Regna. Install with `npm i -g @hyperez/regna-code` and run `regna`. Works with the Regna cloud API and on-premise deployments.",
5
5
  "license": "PolyForm-Noncommercial-1.0.0",
6
6
  "private": false,