src / toolBudget.ts

import type { LLMTool } from "@lmstudio/sdk";

// Shorten prose only under context pressure; executable constraints and stored values stay exact.
export function compactToolDescriptions(tools:LLMTool[]):LLMTool[] {
  const shorten=(text:string,max:number)=>text.length>max?text.slice(0,max)+"…":text;
  const schema=(value:any):any=>Array.isArray(value)?value.map(schema):value && typeof value==="object"?
    Object.fromEntries(Object.entries(value).map(([key,child])=>[key,
      ["enum","const","default","examples"].includes(key)?child:key==="description" && typeof child==="string"?shorten(child,160):schema(child)])):value;
  return tools.map(tool=>({type:"function",function:{...tool.function,
    ...(tool.function.description?{description:shorten(tool.function.description,320)}:{}),
    ...(tool.function.parameters?{parameters:schema(tool.function.parameters)}:{}),
  }}));
}