@lensmcp/terminals 1.26.8 → 1.26.10
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/THIRD_PARTY_NOTICES.md +32 -0
- package/index.d.ts +2 -0
- package/index.js +1 -1
- package/lib/program-status-scanner.d.ts +13 -0
- package/lib/program-status-scanner.js +1 -0
- package/lib/program-status.d.ts +29 -0
- package/lib/program-status.js +1 -0
- package/lib/terminal-host.d.ts +13 -1
- package/lib/terminal-host.js +1 -1
- package/package.json +1 -1
- package/run.js +1 -1
- package/statusline.js +2 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
## Orca
|
|
4
|
+
|
|
5
|
+
`src/lib/program-status.ts` and `src/lib/program-status-scanner.ts` (and the cases of `src/lib/program-status.spec.ts`)
|
|
6
|
+
are taken from Orca, github.com/stablyai/orca at commit `2e77caf0`: `src/shared/program-status/protocol.ts`,
|
|
7
|
+
`src/shared/program-status/osc-scanner.ts` and their tests — the OSC 7501 «Program Status» parser. Adapted to this
|
|
8
|
+
package's style and to Node; the protocol is unchanged.
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
MIT License
|
|
12
|
+
|
|
13
|
+
Copyright (c) 2026 Lovecast Inc.
|
|
14
|
+
|
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
in the Software without restriction, including without limitation the rights
|
|
18
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all
|
|
23
|
+
copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
|
+
SOFTWARE.
|
|
32
|
+
```
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";export*from"./lib/terminal-host.js";export*from"./lib/terminal-daemon.js";export*from"./lib/terminal-client.js";
|
|
1
|
+
"use strict";export*from"./lib/terminal-host.js";export*from"./lib/terminal-daemon.js";export*from"./lib/terminal-client.js";export*from"./lib/program-status.js";export*from"./lib/program-status-scanner.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ProgramStatusMessage } from './program-status.js';
|
|
2
|
+
/** What a pty's output says of OSC 7501 records, in byte order; `offset` is the index in the chunk just past the sequence. */
|
|
3
|
+
export type ProgramStatusStreamEvent = (ProgramStatusMessage & {
|
|
4
|
+
offset: number;
|
|
5
|
+
}) | {
|
|
6
|
+
type: 'prompt-start';
|
|
7
|
+
offset: number;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'full-reset';
|
|
10
|
+
offset: number;
|
|
11
|
+
};
|
|
12
|
+
/** A stateful OSC 7501 scanner for one pty's output: it strips nothing, and carries a sequence split across chunks. */
|
|
13
|
+
export declare function createProgramStatusOscScanner(): (data: string) => ProgramStatusStreamEvent[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var _=Object.defineProperty;var h=(e,n)=>_(e,"name",{value:n,configurable:!0});var T=Object.defineProperty,x=h((e,n)=>T(e,"name",{value:n,configurable:!0}),"g");import{PROGRAM_STATUS_MAX_BODY_BYTES as M,PROGRAM_STATUS_OSC_PREFIX as a,parseProgramStatusBody as R}from"./program-status.js";const m="\x1B]133;A",p="\x1Bc",k=[a,m,p],B=M,P=512;function O(e){const n=e.lastIndexOf("\x1B");if(n===-1||e.length-n>=a.length)return"";const o=e.slice(n);return k.some(t=>t.length>o.length&&t.startsWith(o))?o:""}h(O,"d"),x(O,"markerPrefixCarry");function S(e,n,o){const t=Math.min(o,e.length);for(let s=n;s<t;s++){const l=e.charCodeAt(s);if(l===7)return{index:s,length:1};if(l===24||l===26)return{index:s,length:1,aborted:!0};if(l===27&&s+1<e.length)return{index:s,length:e.charCodeAt(s+1)===92?2:0}}return null}h(S,"m"),x(S,"findOscTerminator");export function createProgramStatusOscScanner(){let e="";return n=>{if(e.length===0&&!n.includes("\x1B"))return[];if(e.length===0&&!n.includes(a)&&!n.includes(m)&&!n.includes(p))return e=O(n),[];const o=e.length,t=e+n;e="";const s=[],l=x(r=>Math.max(0,r-o),"offsetOf");let i=0;for(;i<t.length;){const r=t.indexOf("\x1B",i);if(r===-1)break;if(t.startsWith(p,r)){i=r+p.length,s.push({type:"full-reset",offset:l(i)});continue}if(t.startsWith(m,r)){const c=r+m.length,g=t.charCodeAt(c),A=g===7||g===27||g===59,d=A?S(t,c,c+P):null;if(d!==null){i=d.index+d.length,d.aborted||s.push({type:"prompt-start",offset:l(i)});continue}if((Number.isNaN(g)||A)&&t.length-c<P){e=t.slice(r);break}i=c;continue}if(!t.startsWith(a,r)){if(t.length-r<a.length&&(e=O(t.slice(r)),e.length>0))break;i=r+1;continue}const u=r+a.length,f=S(t,u,u+B+1);if(f===null){const c=B+(t.endsWith("\x1B")?1:0);if(t.length-u<=c){e=t.slice(r);break}i=u;continue}i=f.index+f.length;const b=f.aborted?null:R(t.slice(u,f.index));b&&s.push({...b,offset:l(i)})}return s}}h(createProgramStatusOscScanner,"createProgramStatusOscScanner"),x(createProgramStatusOscScanner,"createProgramStatusOscScanner");
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare const PROGRAM_STATUS_OSC_PREFIX = "\u001B]7501;";
|
|
2
|
+
declare const PROGRAM_STATUS_STATES: readonly ["idle", "working", "done", "blocked", "error"];
|
|
3
|
+
export type ProgramStatusState = (typeof PROGRAM_STATUS_STATES)[number];
|
|
4
|
+
declare const PROGRAM_STATUS_KINDS: readonly ["permission", "question", "auth"];
|
|
5
|
+
export type ProgramStatusKind = (typeof PROGRAM_STATUS_KINDS)[number];
|
|
6
|
+
export declare const PROGRAM_STATUS_QUERY_REPLY: string;
|
|
7
|
+
export declare const PROGRAM_STATUS_MAX_BODY_BYTES = 4087;
|
|
8
|
+
export interface ProgramStatusReport {
|
|
9
|
+
/** Null addresses the root record. */
|
|
10
|
+
id: string | null;
|
|
11
|
+
state: ProgramStatusState | 'clear';
|
|
12
|
+
kind?: ProgramStatusKind;
|
|
13
|
+
progress?: number;
|
|
14
|
+
app?: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
msg?: string;
|
|
17
|
+
}
|
|
18
|
+
export type ProgramStatusMessage = {
|
|
19
|
+
type: 'query';
|
|
20
|
+
} | {
|
|
21
|
+
type: 'report';
|
|
22
|
+
report: ProgramStatusReport;
|
|
23
|
+
};
|
|
24
|
+
export declare function isProgramStatusState(value: unknown): value is ProgramStatusState;
|
|
25
|
+
export declare function isProgramStatusKind(value: unknown): value is ProgramStatusKind;
|
|
26
|
+
export declare function isProgramStatusQueryBody(body: string): boolean;
|
|
27
|
+
/** Parses the body between `OSC 7501;` and its terminator; null: ignore it (no state, a bad id, over a limit). */
|
|
28
|
+
export declare function parseProgramStatusBody(body: string): ProgramStatusMessage | null;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var _=Object.defineProperty;var s=(t,e)=>_(t,"name",{value:e,configurable:!0});var x=Object.defineProperty,a=s((t,e)=>x(t,"name",{value:e,configurable:!0}),"i");export const PROGRAM_STATUS_OSC_PREFIX="\x1B]7501;";const S=["idle","working","done","blocked","error"],y=["permission","question","auth"];export const PROGRAM_STATUS_QUERY_REPLY=`${PROGRAM_STATUS_OSC_PREFIX}?:states=${S.join(",")}:kinds=${y.join(",")}\x1B\\`,PROGRAM_STATUS_MAX_BODY_BYTES=4087;const h=16,B=2732,$=2048,R=256,T=192,k=128,b=8,v=32,O=/^[a-z]+$/,M=/^[A-Za-z0-9_.,+/=-]*$/,U=/^[A-Za-z0-9_.+-]{1,32}$/,w=/^[A-Za-z0-9_.+-]+$/,z=/^0*(\d{1,3})$/,E=/^[A-Za-z0-9+/]*={0,2}$/,G=/[\u0000-\u001f\u007f-\u009f]/,I=new TextDecoder("utf-8",{fatal:!0}),f=a((t,e)=>Buffer.byteLength(t,"utf8")<=e,"withinBytes");export function isProgramStatusState(t){return S.some(e=>e===t)}s(isProgramStatusState,"isProgramStatusState"),a(isProgramStatusState,"isProgramStatusState");export function isProgramStatusKind(t){return y.some(e=>e===t)}s(isProgramStatusKind,"isProgramStatusKind"),a(isProgramStatusKind,"isProgramStatusKind");export function isProgramStatusQueryBody(t){return t.trim()==="?"}s(isProgramStatusQueryBody,"isProgramStatusQueryBody"),a(isProgramStatusQueryBody,"isProgramStatusQueryBody");function P(t){if(t.length>k)return!1;const e=t.split("/");return e.length<=b&&e.every(r=>U.test(r))}s(P,"I"),a(P,"isValidId");function A(t,e){if(!E.test(t))return null;const r=t.replace(/=+$/,"");if(r.length%4===1)return null;let n;try{n=atob(r+"=".repeat((4-r.length%4)%4))}catch{return null}if(n.length>e)return null;let i;try{i=I.decode(Uint8Array.from(n,c=>c.charCodeAt(0)))}catch{return null}return G.test(i)?null:i}s(A,"U"),a(A,"decodeText");export function parseProgramStatusBody(t){if(!f(t,PROGRAM_STATUS_MAX_BODY_BYTES))return null;if(isProgramStatusQueryBody(t))return{type:"query"};const e=new Map,r={};for(const l of t.split(":")){const g=l.indexOf("=");if(g===-1)continue;const o=l.slice(0,g).trim(),u=l.slice(g+1).trim();if(!f(o,h)||o==="app"&&!f(u,v)||o==="msg"&&!f(u,B)||o==="title"&&!f(u,R)||o==="id"&&!P(u))return null;if(!(!O.test(o)||!M.test(u))){if(o==="msg"||o==="title"){const m=A(u,o==="msg"?$:T);if(m===null)return null;r[o]=m;continue}e.set(o,u)}}const n=e.get("state");if(n===void 0||n!=="clear"&&!isProgramStatusState(n))return null;const i={id:e.get("id")??null,state:n},c=e.get("kind");n==="blocked"&&c!==void 0&&isProgramStatusKind(c)&&(i.kind=c);const p=z.exec(e.get("progress")??"")?.[1];if((n==="working"||n==="blocked")&&p!==void 0){const l=Number.parseInt(p,10);l<=100&&(i.progress=l)}const d=e.get("app");return d!==void 0&&w.test(d)&&(i.app=d),r.msg&&(i.msg=r.msg),r.title&&(i.title=r.title),{type:"report",report:i}}s(parseProgramStatusBody,"parseProgramStatusBody"),a(parseProgramStatusBody,"parseProgramStatusBody");
|
package/lib/terminal-host.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ProgramStatusKind, type ProgramStatusState } from './program-status.js';
|
|
1
2
|
/** A terminal the host runs: an agent CLI (or any command) on a real pty, kept while nobody watches. */
|
|
2
3
|
export interface TerminalSpec {
|
|
3
4
|
id: string;
|
|
@@ -24,9 +25,18 @@ export interface TerminalInfo {
|
|
|
24
25
|
endedAt?: number;
|
|
25
26
|
exitCode?: number;
|
|
26
27
|
signal?: number;
|
|
28
|
+
/** When it last printed anything. */
|
|
29
|
+
lastOutputAt?: number;
|
|
30
|
+
/** The newest OSC 7501 report its program made of itself, and when. */
|
|
31
|
+
status?: TerminalProgramStatus;
|
|
32
|
+
statusAt?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface TerminalProgramStatus {
|
|
35
|
+
state: ProgramStatusState;
|
|
36
|
+
kind?: ProgramStatusKind;
|
|
27
37
|
}
|
|
28
38
|
export interface TerminalAttachment {
|
|
29
|
-
/** The kept output up to the attach: written to a fresh screen, it shows what the terminal shows. */
|
|
39
|
+
/** The kept output up to the attach, without the program's queries to its terminal: written to a fresh screen, it shows what the terminal shows. */
|
|
30
40
|
replay: string;
|
|
31
41
|
/** How many bytes of its output were dropped before the replay starts. */
|
|
32
42
|
dropped: number;
|
|
@@ -63,6 +73,8 @@ export declare class TerminalHost {
|
|
|
63
73
|
/** Forgets an ended terminal and its scrollback; a running one is refused. */
|
|
64
74
|
remove(id: string): void;
|
|
65
75
|
dispose(): void;
|
|
76
|
+
/** OSC 7501: the program's query is answered here, once, and its newest report of itself kept; a reset or a new prompt clears it. */
|
|
77
|
+
private heard;
|
|
66
78
|
private need;
|
|
67
79
|
private running;
|
|
68
80
|
}
|
package/lib/terminal-host.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var w=Object.defineProperty;var c=(r,t)=>w(r,"name",{value:t,configurable:!0});var u=Object.defineProperty,d=c((r,t)=>u(r,"name",{value:t,configurable:!0}),"d");import{chmodSync as y,statSync as g}from"node:fs";import{createRequire as x}from"node:module";import{dirname as k,join as l}from"node:path";import{PROGRAM_STATUS_QUERY_REPLY as v}from"./program-status.js";import{createProgramStatusOscScanner as A}from"./program-status-scanner.js";export class TerminalRefused extends Error{static{c(this,"TerminalRefused")}static{d(this,"TerminalRefused")}}export const TERMINAL_ID=/^[a-z0-9][a-z0-9-]{0,79}$/,SCROLLBACK_BYTES=2*1024*1024;const R=/\x1b\[(?:[>=]?0?c|\??\d*n|\??[\d;]*\$p|>0?q|\?u|(?:1[13-689]|2[01])(?:;\d+)*t)|\x1b\](?:4;\d+|5;\d+|1\d|52;[a-z0-9]*);\?(?:\x07|\x1b\\)|\x1bP[$+]q[^\x1b]*\x1b\\/g,S={cols:20,rows:5},b={cols:500,rows:200},h=d((r,t,e)=>Number.isInteger(r)?Math.min(b[e],Math.max(S[e],r)):t,"size");export async function loadPty(){try{p();const r=await import("node-pty");return r.spawn??r.default?.spawn}catch{return}}c(loadPty,"loadPty"),d(loadPty,"loadPty");function p(r=process.cwd()){try{const t=x(l(r,"package.json")).resolve("node-pty/package.json",{paths:[r,l(r,"node_modules","@lensmcp","terminals")]});for(const e of[l("prebuilds",`${process.platform}-${process.arch}`),l("build","Release")]){const o=l(k(t),e,"spawn-helper");try{g(o).mode&73||y(o,493)}catch{}}}catch{}}c(p,"S"),d(p,"helperRuns");export class TerminalHost{static{c(this,"TerminalHost")}static{d(this,"TerminalHost")}constructor(t,e=Date.now){this.spawn=t,this.now=e,this.terminals=new Map}static async open(t){const e=await loadPty();return e?new TerminalHost(e,t):void 0}start(t){if(!TERMINAL_ID.test(t.id))throw new TerminalRefused("A terminal id is 1\u201380 lowercase letters, digits and dashes");if(this.terminals.has(t.id))throw new TerminalRefused(`Terminal ${t.id} exists already`);const e=h(t.cols,120,"cols"),o=h(t.rows,32,"rows");let s;try{s=this.spawn(t.command,[...t.args??[]],{name:"xterm-256color",cols:e,rows:o,cwd:t.cwd,env:{TERM:"xterm-256color",COLORTERM:"truecolor",...t.env}})}catch(i){throw new TerminalRefused(`Could not start ${t.command}: ${i instanceof Error?i.message:String(i)}`)}const n={info:{id:t.id,pid:s.pid,command:t.command,cwd:t.cwd,cols:e,rows:o,startedAt:this.now(),written:0,attached:0},pty:s,chunks:[],kept:0,dropped:0,limit:Math.max(4096,t.scrollbackBytes??SCROLLBACK_BYTES),watchers:new Set,ends:new Set},m=A();return s.onData(i=>{n.info.written+=i.length,n.info.lastOutputAt=this.now();for(const a of m(i))this.heard(n,a);for(n.chunks.push(i),n.kept+=i.length;n.kept>n.limit&&n.chunks.length>1;){const a=n.chunks.shift();n.kept-=a.length,n.dropped+=a.length}for(const a of n.watchers)try{a.data(i)}catch{}}),s.onExit(({exitCode:i,signal:a})=>{n.info.endedAt=this.now(),n.info.exitCode=i,a&&(n.info.signal=a);for(const f of n.watchers)try{f.exit?.({...n.info})}catch{}for(const f of n.ends)try{f({...n.info})}catch{}}),this.terminals.set(t.id,n),{...n.info}}list(){return[...this.terminals.values()].map(t=>({...t.info}))}get(t){const e=this.terminals.get(t);return e?{...e.info}:void 0}attach(t,e,o){const s=this.need(t),n={data:e,...o?{exit:o}:{}};return s.watchers.add(n),s.info.attached=s.watchers.size,{replay:s.chunks.join("").replace(R,""),dropped:s.dropped,detach:d(()=>{s.watchers.delete(n),s.info.attached=s.watchers.size},"detach")}}onEnd(t,e){const o=this.need(t);o.info.endedAt!==void 0?e({...o.info}):o.ends.add(e)}write(t,e){this.running(t).pty.write(e)}resize(t,e,o){const s=this.running(t);return s.info.cols=h(e,s.info.cols,"cols"),s.info.rows=h(o,s.info.rows,"rows"),s.pty.resize(s.info.cols,s.info.rows),{...s.info}}stop(t,e=!1){const o=this.need(t);o.info.endedAt===void 0&&o.pty.kill(e?"SIGKILL":"SIGHUP")}remove(t){if(this.need(t).info.endedAt===void 0)throw new TerminalRefused(`Terminal ${t} is still running \u2014 stop it first`);this.terminals.delete(t)}dispose(){for(const t of this.terminals.values())if(t.info.endedAt===void 0)try{t.pty.kill("SIGHUP")}catch{}this.terminals.clear()}heard(t,e){if(e.type==="query"){t.pty.write(v);return}if(!(e.type==="report"&&e.report.id!==null)){if(e.type==="report"&&e.report.state!=="clear"){t.info.status={state:e.report.state,...e.report.kind?{kind:e.report.kind}:{}},t.info.statusAt=this.now();return}delete t.info.status,delete t.info.statusAt}}need(t){const e=this.terminals.get(t);if(!e)throw new TerminalRefused(`No terminal ${t.slice(0,80)}`);return e}running(t){const e=this.need(t);if(e.info.endedAt!==void 0)throw new TerminalRefused(`Terminal ${t} has ended (exit ${e.info.exitCode??"?"})`);return e}}
|
package/package.json
CHANGED
package/run.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var
|
|
2
|
+
"use strict";var x=Object.defineProperty;var i=(e,s)=>x(e,"name",{value:s,configurable:!0});var T=Object.defineProperty,n=i((e,s)=>T(e,"name",{value:s,configurable:!0}),"o");import{chmodSync as b,rmSync as k,writeFileSync as E}from"node:fs";import{basename as I}from"node:path";import{serveTerminals as G}from"./lib/terminal-daemon.js";import{TerminalHost as $}from"./lib/terminal-host.js";const[t,O,g,...y]=process.argv.slice(2);(!t||O!=="--"||!g)&&(console.error("Usage: run <socket> -- <command> [args\u2026]"),process.exit(2));const c=I(t).replace(/\.sock$/,""),r=n((e,s={})=>console.log(JSON.stringify({type:"lensmcp.terminal",event:e,...s})),"say");async function h(){if(process.stdin.isTTY)return"";let e="";process.stdin.setEncoding("utf8");for await(const s of process.stdin)e+=s;return e}i(h,"G"),n(h,"stdinText");const u=(await h()).trim(),o=await $.open();o||(r("failed",{why:"node-pty did not load on this machine"}),process.exit(3));const j=await G(o,t).catch(e=>{r("failed",{why:`its socket could not be opened (${e instanceof Error?e.message:String(e)})`}),process.exit(5)}),w={};for(const[e,s]of Object.entries(process.env))s!==void 0&&(w[e]=s);const v=t.replace(/\.sock$/,"")+".screen";let a=!1,d=!1;const p=o.stop.bind(o);o.stop=(e,s)=>{a||(d=!0),p(e,s)};const S=n(async e=>{await j.close().catch(()=>{}),k(t,{force:!0}),process.exit(e)},"finish"),l=n(()=>{try{const e=o.attach(c,()=>{});e.detach(),E(v,e.replay,{mode:384}),b(v,384)}catch{}},"keepScreen");process.on("uncaughtException",e=>{l(),console.error(e),process.exit(70)});try{const e=o.start({id:c,command:g,args:u?[...y,u]:y,cwd:process.cwd(),env:w});r("started",{pid:e.pid,socket:t}),o.onEnd(c,s=>{l();const f=d&&!a?0:s.signal?128+s.signal:s.exitCode??1;r("ended",{exitCode:f,...d&&!a?{asked:!0}:s.signal?{signal:s.signal}:{}}),S(f)})}catch(e){r("failed",{why:e instanceof Error?e.message:String(e)}),await S(4)}const m=n(()=>{if(!a){a=!0,l();try{p(c)}catch{}setTimeout(()=>{try{p(c,!0)}catch{}},4e3).unref()}},"stop");process.on("SIGTERM",m),process.on("SIGINT",m),process.on("SIGHUP",m);
|
package/statusline.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";import{spawn as
|
|
3
|
-
|
|
2
|
+
"use strict";var S=Object.defineProperty;var n=(e,t)=>S(e,"name",{value:t,configurable:!0});var y=Object.defineProperty,a=n((e,t)=>y(e,"name",{value:t,configurable:!0}),"i");import{spawn as w}from"node:child_process";import{existsSync as v,mkdirSync as g,readdirSync as b,renameSync as h,writeFileSync as N}from"node:fs";import{homedir as O}from"node:os";import{dirname as E,join as o}from"node:path";const[c,_]=process.argv.slice(2),$=/^se-[a-z0-9]{1,12}-[a-f0-9]{6}$/,j=["total_cost_usd","total_duration_ms","total_api_duration_ms","total_lines_added","total_lines_removed"],x=["total_input_tokens","context_window_size","used_percentage"];function d(e,t){const s=`${e}.${process.pid}.tmp`;N(s,t,{mode:384}),h(s,e)}n(d,"p"),a(d,"writeAtomic");const l=a((e,t)=>Object.fromEntries(t.flatMap(s=>{const i=e&&typeof e=="object"?e[s]:void 0;return typeof i=="number"&&Number.isFinite(i)?[[s,i]]:[]})),"figures");function f(e){const t=process.env.LENSMCP_PERSON_SESSION;if(!t||!$.test(t))return;const s=o(process.env.LENSMCP_HOME??o(O(),".lensmcp"),"sessions"),i=b(s,{withFileTypes:!0}).find(p=>p.isDirectory()&&v(o(s,p.name,`${t}.log`)));if(!i)return;const m=typeof e.session_id=="string"?e.session_id.slice(0,200):void 0,u={at:Date.now(),...m?{thread:m}:{},cost:l(e.cost,j),context_window:l(e.context_window,x)};d(o(s,i.name,`${t}.status.json`),JSON.stringify(u))}n(f,"$"),a(f,"keepSessionStatus");let r="";process.stdin.setEncoding("utf8"),process.stdin.on("data",e=>{r.length<2e6&&(r+=e)}),process.stdin.on("end",()=>{try{const t=JSON.parse(r);c&&t.rate_limits&&typeof t.rate_limits=="object"&&(g(E(c),{recursive:!0,mode:448}),d(c,`${JSON.stringify({at:Date.now(),rate_limits:t.rate_limits})}
|
|
3
|
+
`)),f(t)}catch{}if(!_)return;const e=w(_,{shell:!0,stdio:["pipe","inherit","inherit"]});e.on("error",()=>{}),e.stdin.on("error",()=>{}),e.stdin.end(r)});
|