@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 6.45 kB
JavaScript
import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url); { const __piWorkerThreads = require("node:worker_threads"); if (typeof __piWorkerThreads.markAsUncloneable !== "function") { __piWorkerThreads.markAsUncloneable = () => {}; } }
var COMBINER_KEYS=["anyOf","oneOf","allOf"],SCHEMA_MAP_KEYS=["properties","patternProperties","$defs","definitions"],SCHEMA_SINGLE_KEYS=["items","additionalProperties","contains","propertyNames","if","then","else","not"];function isJsonObject(value){return value!==null&&typeof value=="object"&&!Array.isArray(value)}function isScalarType(type){return type==="string"||type==="number"||type==="integer"||type==="boolean"}function moveTypeIntoCombinerBranches(node){if(!("type"in node))return;let parentType=node.type;for(let combiner of COMBINER_KEYS){let branches=node[combiner];if(Array.isArray(branches))for(let branch of branches)isJsonObject(branch)&&!("type"in branch)&&(branch.type=parentType)}delete node.type}function collapseConstUnion(node){let branches=node.anyOf;if(!Array.isArray(branches)||branches.length<2||"type"in node)return;let values=[],sharedType;for(let branch of branches){if(!isJsonObject(branch)||!Object.hasOwn(branch,"const"))return;let keys=Object.keys(branch);if(keys.length!==2||!keys.includes("type"))return;let branchType=branch.type;if(!isScalarType(branchType))return;if(sharedType===void 0)sharedType=branchType;else if(sharedType!==branchType)return;values.push(branch.const)}sharedType===void 0||values.length===0||(delete node.anyOf,node.type=sharedType,node.enum=values)}function mergeRootObjectUnion(schema,combiner){let selected=combiner??(Array.isArray(schema.anyOf)?"anyOf":"oneOf"),branches=schema[selected];if(!Array.isArray(branches)||branches.length===0||schema.properties!==void 0&&!isJsonObject(schema.properties)||schema.required!==void 0&&!Array.isArray(schema.required))return;let objectBranches=[];for(let branch of branches){if(!isJsonObject(branch)||branch.type!=="object"&&branch.type!==void 0||branch.properties!==void 0&&!isJsonObject(branch.properties)||branch.required!==void 0&&!Array.isArray(branch.required))return;objectBranches.push(branch)}let properties=isJsonObject(schema.properties)?{...schema.properties}:{};for(let branch of objectBranches)if(isJsonObject(branch.properties))for(let[name,propertySchema]of Object.entries(branch.properties)){let existing=properties[name];properties[name]=existing===void 0||JSON.stringify(existing)===JSON.stringify(propertySchema)?propertySchema:{[selected==="allOf"?"allOf":"anyOf"]:[existing,propertySchema]}}let rootRequired=Array.isArray(schema.required)?schema.required.filter(name=>typeof name=="string"):[],branchRequiredSets=objectBranches.map(branch=>new Set(Array.isArray(branch.required)?branch.required.filter(name=>typeof name=="string"):[])),firstBranchRequired=branchRequiredSets[0],commonBranchRequired=firstBranchRequired?[...firstBranchRequired].filter(name=>branchRequiredSets.every(names=>names.has(name))):[],required=[...new Set([...rootRequired,...selected==="allOf"?branchRequiredSets.flatMap(names=>[...names]):commonBranchRequired])],hints=selected==="allOf"?[]:objectBranches.flatMap((branch,index)=>{let own=[...branchRequiredSets[index]??[]].filter(name=>!required.includes(name));return own.length===0?[]:[`REQUIRED for ${describeBranch(branch,index)}: ${own.join(", ")}.`]}),description=hints.length===0?void 0:[typeof schema.description=="string"?schema.description:"",...hints].filter(Boolean).join(" "),rest={...schema};for(let key of combiner?[combiner]:["anyOf","oneOf"])delete rest[key];return{...rest,...description===void 0?{}:{description},type:"object",properties,...required.length>0?{required}:{}}}function describeBranch(branch,index){if(isJsonObject(branch.properties))for(let[name,propertySchema]of Object.entries(branch.properties)){let values=literalValues(propertySchema);if(values.length>0)return`${name}=${values.join("|")}`}return`option ${index+1}`}function literalValues(schema){if(!isJsonObject(schema))return[];if(Object.hasOwn(schema,"const"))return[String(schema.const)];if(Array.isArray(schema.enum))return schema.enum.map(String);let branches=schema.anyOf;if(!Array.isArray(branches))return[];let values=branches.map(branch=>literalValues(branch));return values.every(value=>value.length===1)?values.flat():[]}function normalizeNode(node,isRoot=!1){if(Array.isArray(node))return node.map(child=>normalizeNode(child));if(!isJsonObject(node))return node;delete node.optional,COMBINER_KEYS.some(key=>Array.isArray(node[key]))&&!isRoot&&moveTypeIntoCombinerBranches(node);for(let combiner of COMBINER_KEYS){let branches=node[combiner];Array.isArray(branches)&&(node[combiner]=branches.map(branch=>normalizeNode(branch)))}Array.isArray(node.anyOf)&&collapseConstUnion(node);for(let key of SCHEMA_SINGLE_KEYS)Object.hasOwn(node,key)&&(node[key]=normalizeNode(node[key]));for(let key of SCHEMA_MAP_KEYS){let map=node[key];if(isJsonObject(map))for(let name of Object.keys(map))map[name]=normalizeNode(map[name])}return node}function normalizeToolParametersForOpenAICompat(schema){let normalized=normalizeNode(structuredClone(schema),!0);return ensureRootObjectSchema(normalized)}function ensureRootObjectSchema(schema){let merged=mergeRootObjectUnion(schema);return merged||(schema.type!==void 0?schema:{...schema,type:"object"})}function normalizeToolParametersForMoonshot(schema){return stripMoonshotAnnotations(normalizeToolParametersForOpenAICompat(schema))}function resolveRootObjectSchema(schema){return mergeRootObjectUnion(structuredClone(schema))??schema}function stripMoonshotAnnotations(node){if(Array.isArray(node))return node.map(child=>stripMoonshotAnnotations(child));if(!isJsonObject(node))return node;delete node.format,delete node.examples,delete node.readOnly,delete node.writeOnly,delete node.deprecated,delete node.$schema,delete node.$id;for(let combiner of COMBINER_KEYS){let branches=node[combiner];Array.isArray(branches)&&(node[combiner]=branches.map(branch=>stripMoonshotAnnotations(branch)))}for(let key of SCHEMA_SINGLE_KEYS)Object.hasOwn(node,key)&&(node[key]=stripMoonshotAnnotations(node[key]));for(let key of SCHEMA_MAP_KEYS){let map=node[key];if(isJsonObject(map))for(let name of Object.keys(map))map[name]=stripMoonshotAnnotations(map[name])}return node}export{normalizeToolParametersForOpenAICompat,normalizeToolParametersForMoonshot,resolveRootObjectSchema};