@lensmcp/core 1.25.45 → 1.25.47
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/lib/answer-budget.d.ts +42 -17
- package/lib/answer-budget.js +2 -2
- package/package.json +2 -2
package/lib/answer-budget.d.ts
CHANGED
|
@@ -11,8 +11,17 @@ export declare const DEFAULT_ANSWER_BUDGET_BYTES: number;
|
|
|
11
11
|
export declare const ANSWER_BUDGET_ENV = "LENSMCP_ANSWER_BUDGET_BYTES";
|
|
12
12
|
/** A budget below this cannot hold the note that says an answer was cut, so it is raised to it. */
|
|
13
13
|
export declare const MIN_ANSWER_BUDGET_BYTES: number;
|
|
14
|
-
/** The key a cut document carries its
|
|
14
|
+
/** The key a cut document carries its {@link AnswerCut} under. */
|
|
15
15
|
export declare const ANSWER_CUT_KEY = "cutToFit";
|
|
16
|
+
/**
|
|
17
|
+
* Longest a cut's `note` may be, in UTF-8 bytes. The note used to be a paragraph of about 700 characters that listed
|
|
18
|
+
* every cut list, repeated the tool's hint and ended by telling the agent to start the lens with the budget off. One
|
|
19
|
+
* Codex lead read it 45 times in a run, and the last sentence pushed agents toward larger answers
|
|
20
|
+
* (issues/closed/foodguard-155). Now the details are fields of the cut, and the note is one short sentence that says
|
|
21
|
+
* how to read them. How to turn the budget off is for the person: it is in the docs and on the dashboard, never in an
|
|
22
|
+
* answer to an agent.
|
|
23
|
+
*/
|
|
24
|
+
export declare const MAX_CUT_NOTE_BYTES = 120;
|
|
16
25
|
/** Longest string left whole in an item of a list that is over its budget — a message and about fifteen stack frames. */
|
|
17
26
|
export declare const FITTED_STRING_CHARS = 1500;
|
|
18
27
|
/** A string at least this long is stated once per list; a repeat becomes a reference to the first. */
|
|
@@ -83,29 +92,45 @@ export interface ListFit<T> {
|
|
|
83
92
|
export declare function fitList<T>(items: readonly T[], budgetBytes: number, opts?: ListFitOptions): ListFit<T>;
|
|
84
93
|
/** Split a budget between several claims: a small one is granted whole, and the large ones share what is left evenly. */
|
|
85
94
|
export declare function shareBudget(sizes: readonly number[], budgetBytes: number): number[];
|
|
86
|
-
/**
|
|
95
|
+
/** One list that lost items: how many it holds, how many are shown, and which. */
|
|
96
|
+
export interface ListCut {
|
|
97
|
+
list: string;
|
|
98
|
+
total: number;
|
|
99
|
+
returned: number;
|
|
100
|
+
kept: 'newest' | 'first' | 'last';
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* What a cut answer says about itself, under {@link ANSWER_CUT_KEY}. The details are fields an agent reads, and a
|
|
104
|
+
* field is left out when it has nothing to say, so the whole object is a few hundred bytes. It used to carry a
|
|
105
|
+
* paragraph-long note and every count, zeros included (issues/closed/foodguard-155).
|
|
106
|
+
*/
|
|
87
107
|
export interface AnswerCut {
|
|
88
|
-
/** The budget it was fitted to. */
|
|
89
|
-
|
|
90
|
-
/** What the whole answer weighs. */
|
|
91
|
-
|
|
92
|
-
/** Each list that lost items
|
|
93
|
-
lists
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
shortenedStrings: number;
|
|
100
|
-
repeatedStrings: number;
|
|
101
|
-
/** Cutting lists was not enough: large values elsewhere in the document were shrunk too. */
|
|
108
|
+
/** The budget it was fitted to, in UTF-8 bytes. */
|
|
109
|
+
budget: number;
|
|
110
|
+
/** What the whole answer weighs, in UTF-8 bytes (`null` on the wire for one that cannot be measured). */
|
|
111
|
+
original: number;
|
|
112
|
+
/** Each list that lost items. Absent when no list did. */
|
|
113
|
+
lists?: ListCut[];
|
|
114
|
+
/** How many long texts were cut to their head (`…[lensmcp truncated: N more chars]`). */
|
|
115
|
+
shortened?: number;
|
|
116
|
+
/** How many texts repeated between items are stated once and referenced (`…[lensmcp: same text as …]`). */
|
|
117
|
+
repeated?: number;
|
|
118
|
+
/** Cutting lists was not enough: large values elsewhere in the document were shrunk too (`__lensmcpTruncation`). */
|
|
102
119
|
clamped?: true;
|
|
103
120
|
/** The values it holds that were sent whole, whatever the budget ({@link AnswerFitOptions.whole}). */
|
|
104
121
|
whole?: string[];
|
|
122
|
+
/** Where the rest can be read, when the caller knows ({@link AnswerFitOptions.more}). */
|
|
123
|
+
more?: string;
|
|
124
|
+
/** One sentence on how to read the fields above, at most {@link MAX_CUT_NOTE_BYTES}. */
|
|
105
125
|
note: string;
|
|
106
126
|
}
|
|
127
|
+
/** The note for a cut: how to read its fields. It never names a way to turn the budget off (issues/closed/foodguard-155). */
|
|
128
|
+
export declare function cutNote(cut: Pick<AnswerCut, 'lists' | 'more'>): string;
|
|
107
129
|
export interface AnswerFitOptions {
|
|
108
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* Where the rest can be read, such as the tool that pages this list. It goes in the cut's `more` field: keep it to a
|
|
132
|
+
* line, because every cut answer of that tool carries it.
|
|
133
|
+
*/
|
|
109
134
|
more?: string;
|
|
110
135
|
/** Per top-level list: the end that holds its newest items, for a ring whose items carry no time. */
|
|
111
136
|
newest?: Readonly<Record<string, ListEnd>>;
|
package/lib/answer-budget.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`,
|
|
1
|
+
"use strict";var L=Object.defineProperty;var m=(e,n)=>L(e,"name",{value:n,configurable:!0});var Y=Object.defineProperty,f=m((e,n)=>Y(e,"name",{value:n,configurable:!0}),"h");import{boundedClone as z}from"@lensmcp/protocol-types";export const DEFAULT_ANSWER_BUDGET_BYTES=24*1024,ANSWER_BUDGET_ENV="LENSMCP_ANSWER_BUDGET_BYTES",MIN_ANSWER_BUDGET_BYTES=2*1024,ANSWER_CUT_KEY="cutToFit",MAX_CUT_NOTE_BYTES=120,FITTED_STRING_CHARS=1500,REPEATED_STRING_MIN_CHARS=200;const C=600,V=32,H=new Set(["timestamp","startedAt","startTime","at","emittedAt","lastSeenAt","endedAt","endTime","t","ts","time"]),K=f(e=>typeof e=="number"&&e>=1e11&&e<=1e14,"isEpochMs"),J=.95;export function answerBudgetBytes(e=process.env){const n=e[ANSWER_BUDGET_ENV];if(n===void 0||n.trim()==="")return DEFAULT_ANSWER_BUDGET_BYTES;const r=Number(n);return!Number.isFinite(r)||r<0?DEFAULT_ANSWER_BUDGET_BYTES:r===0?Number.POSITIVE_INFINITY:Math.max(MIN_ANSWER_BUDGET_BYTES,Math.floor(r))}m(answerBudgetBytes,"answerBudgetBytes"),f(answerBudgetBytes,"answerBudgetBytes");export function utf8Bytes(e){let n=0;for(let r=0;r<e.length;r++){const o=e.charCodeAt(r);o<128?n+=1:o<2048?n+=2:o>=55296&&o<=56319&&r+1<e.length&&(e.charCodeAt(r+1)&64512)===56320?(n+=4,r+=1):n+=3}return n}m(utf8Bytes,"utf8Bytes"),f(utf8Bytes,"utf8Bytes");export function jsonBytes(e){let n;try{n=JSON.stringify(e)}catch{return Number.POSITIVE_INFINITY}return n===void 0?0:utf8Bytes(n)}m(jsonBytes,"jsonBytes"),f(jsonBytes,"jsonBytes");const R=f(e=>typeof e=="object"&&e!==null&&!Array.isArray(e),"isRecord");function O(e){if(!R(e))return!1;const n=Object.getPrototypeOf(e);return n===Object.prototype||n===null}m(O,"C"),f(O,"isPlain");export function newestEndOf(e){const n=e[0];if(e.length<2||!R(n))return;let r;for(const o of Object.keys(n)){if(typeof n[o]!="number"||!(H.has(o)||K(n[o])))continue;let s=0,i=0,u=!0,a=n[o];for(let y=1;y<e.length;y++){const x=e[y],d=R(x)?x[o]:void 0;if(typeof d!="number"){u=!1;break}d>a?s+=1:d<a&&(i+=1),a=d}const h=s+i;if(!u||h===0)continue;const l=Math.max(s,i)/h;l>=J&&(!r||l>r.agreement)&&(r={end:s>=i?"end":"start",agreement:l})}return r?.end}m(newestEndOf,"newestEndOf"),f(newestEndOf,"newestEndOf");export function newestFirst(e){return newestEndOf(e)==="end"?[...e].reverse():e.slice()}m(newestFirst,"newestFirst"),f(newestFirst,"newestFirst");export function shortenText(e,n){if(e.length<=n)return e;let r=Math.max(0,n);const o=e.lastIndexOf(`
|
|
2
|
+
`,r);return o>=r/2&&(r=o),r>0&&(e.charCodeAt(r-1)&64512)===55296&&(r-=1),`${e.slice(0,r)}\u2026[lensmcp truncated: ${e.length-r} more chars]`}m(shortenText,"shortenText"),f(shortenText,"shortenText");export function fitList(e,n,r={}){const o=e.length,s=e.reduce((c,w)=>c+jsonBytes(w)+1,2);if(s<=n)return{items:e.slice(),total:o,returned:o,kept:"all",shortenedStrings:0,repeatedStrings:0,bytes:s};const i=r.newest??newestEndOf(e),u=i??r.keep??"start",a=r.name??"items",h=r.maxStringChars??FITTED_STRING_CHARS,l=u==="start"?e:[...e].reverse(),y=String(Math.max(0,o-1)).length,x=new Map,d=[],b=[];let E=2,_=0,$=0;for(let c=0;c<l.length;c++){const w=new Map,T=[];let M=0;const A=f((t,p,g,G,W)=>{if(typeof t=="string"){if(t.length>=REPEATED_STRING_MIN_CHARS){const S=x.get(t)??w.get(t);if(S)return T.push({holder:G,key:W,owner:S.owner,path:S.path}),`\u2026[lensmcp: same text as ${a}[${"9".repeat(y)}]${S.path}]`;w.set(t,{owner:c,path:p})}return t.length>h?(M+=1,shortenText(t,h)):t}if(g>=V)return t;if(Array.isArray(t)){const S=[];return t.forEach((I,D)=>S.push(A(I,`${p}[${D}]`,g+1,S,D))),S}if(O(t)){const S={};for(const I of Object.keys(t))S[I]=A(t[I],`${p}.${I}`,g+1,S,I);return S}return t},"slim"),B=[];let N=A(l[c],"",0,B,0),v=jsonBytes(N)+1;if(b.length===0&&E+v>n)N=F(l[c],Math.max(0,n-E-1)),v=jsonBytes(N)+1,T.length=0,w.clear(),M=1;else if(E+v>n)break;b.push(N),E+=v,_+=M,$+=T.length;for(const[t,p]of w)x.set(t,p);for(const t of T)d.push(t.holder===B?{...t,holder:b,key:b.length-1}:t)}const k=b.length,j=f(c=>u==="start"?c:k-1-c,"indexOf");if(u==="end"){for(const c of d)c.holder===b&&(c.key=j(c.key));b.reverse()}for(const c of d)c.holder[c.key]=`\u2026[lensmcp: same text as ${a}[${j(c.owner)}]${c.path}]`;return{items:b,total:o,returned:k,kept:k===o?"all":i?"newest":u==="start"?"first":"last",shortenedStrings:_,repeatedStrings:$,bytes:jsonBytes(b)}}m(fitList,"fitList"),f(fitList,"fitList");function F(e,n){let r=Math.max(256,n),o=e;for(let s=0;s<6;s++){if(o=z(e,{rawByteLimit:r,maxStringBytes:Math.min(2e3,Math.max(128,Math.floor(r/4))),maxArrayItems:64}).value,jsonBytes(o)<=n)return o;r=Math.floor(r*.6)}return o}m(F,"D"),f(F,"clampTo");export function shareBudget(e,n){const r=e.map((i,u)=>({size:i,index:u})).sort((i,u)=>i.size-u.size),o=new Array(e.length).fill(0);let s=Math.max(0,n);return r.forEach(({size:i,index:u},a)=>{const h=Math.floor(s/(r.length-a));o[u]=Math.min(i,h),s-=o[u]}),o}m(shareBudget,"shareBudget"),f(shareBudget,"shareBudget");export function cutNote(e){return(e.lists?.length?"Cut to fit: each list in `lists` shows `returned` of `total`; every other count is of the whole.":"Cut to fit: large values were shortened; every count is of the whole.")+(e.more?" Rest: see `more`.":"")}m(cutNote,"cutNote"),f(cutNote,"cutNote");function P(e,n){const r={...e},o=new WeakSet([r]),s=[];let i=0;for(const u of n){const a=u.split("."),h=a[a.length-1];let l=r;for(const y of a.slice(0,-1)){const x=l[y];if(!O(x)){l=void 0;break}const d=o.has(x)?x:{...x};o.add(d),l[y]=d,l=d}!l||l[h]===void 0||l[h]===null||(s.push({keys:a,value:l[h]}),i+=jsonBytes(l[h]),l[h]=null)}return s.length?{body:r,parts:s,bytes:i}:void 0}m(P,"K"),f(P,"holdWhole");function U(e,n){for(const{keys:r,value:o}of n){let s=e;for(const i of r.slice(0,-1)){const u=s[i],a=O(u)?u:{};s[i]=a,s=a}s[r[r.length-1]]=o}return e}m(U,"z"),f(U,"putBack");export function fitAnswer(e,n,r={}){if(!Number.isFinite(n)||e===null||typeof e!="object")return{value:e};const o=jsonBytes(e);if(o<=n)return{value:e};const s=r.whole?.length&&O(e)?P(e,r.whole):void 0,i=s?s.body:e,u=s?jsonBytes(i):o,a=s?Math.max(MIN_ANSWER_BUDGET_BYTES,n-s.bytes):n;if(u<=a)return{value:e};const h=[];let l=0,y=0,x=!1,d;const b=f(()=>(d={budget:n,original:o,...h.length?{lists:h}:{},...l?{shortened:l}:{},...y?{repeated:y}:{},...x?{clamped:!0}:{},...s?{whole:s.parts.map(t=>t.keys.join("."))}:{},...r.more?{more:r.more}:{},note:cutNote({lists:h,more:r.more})},d),"describe"),E=f(t=>({value:s?U(t,s.parts):t,cut:d}),"done");if(Array.isArray(i)){const t=fitList(i,a);return t.kept!=="all"&&h.push({list:"items",total:t.total,returned:t.returned,kept:t.kept}),l=t.shortenedStrings,y=t.repeatedStrings,b(),E(t.items)}if(!O(i))return{value:e};const _=Object.keys(i),$=_.filter(t=>Array.isArray(i[t])&&i[t].length>0),k=$.map(t=>jsonBytes(i[t])),j=u-k.reduce((t,p)=>t+p,0),c=shareBudget(k,a-j-C),w={};$.forEach((t,p)=>{if(k[p]<=c[p])return;const g=fitList(i[t],c[p],{name:t,...r.newest?.[t]?{newest:r.newest[t]}:{}});w[t]=g.items,g.kept!=="all"&&h.push({list:t,total:g.total,returned:g.returned,kept:g.kept}),l+=g.shortenedStrings,y+=g.repeatedStrings});const T=f(t=>({[ANSWER_CUT_KEY]:b(),...t}),"assemble"),M={};for(const t of _)M[t]=t in w?w[t]:i[t];let A=T(M);if(jsonBytes(A)<=a)return E(A);x=!0;let B=Math.max(512,a-C);for(let t=0;t<6;t++){if(A=T(F(M,B)),jsonBytes(A)<=a)return E(A);B=Math.floor(B*.6)}const N={};let v=C;for(const t of _){const p=i[t];if(p!==null&&typeof p=="object")continue;const g=jsonBytes(p)+utf8Bytes(t)+4;if(v+g>a)break;N[t]=p,v+=g}return E(T(N))}m(fitAnswer,"fitAnswer"),f(fitAnswer,"fitAnswer");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lensmcp/core",
|
|
3
|
-
"version": "1.25.
|
|
3
|
+
"version": "1.25.47",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@lensmcp/protocol-types": "1.25.
|
|
87
|
+
"@lensmcp/protocol-types": "1.25.47",
|
|
88
88
|
"tslib": "^2.3.0"
|
|
89
89
|
},
|
|
90
90
|
"license": "Apache-2.0",
|