UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

4 lines (3 loc) • 6.07 kB
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 = () => {}; } } import{normalizeProviderError}from"./chunk-AC72EWI5.js";import{formatProviderRequestId}from"./chunk-AFDUWYAX.js";import{appendAssistantMessageDiagnostic}from"./chunk-NDNQ23SK.js";var GITHUB_COPILOT_TOOL_LIMIT=128,GITHUB_COPILOT_TOOL_LIMIT_DIAGNOSTIC="github_copilot_tool_limit";function isRecord(value){return typeof value=="object"&&value!==null}function selectedToolName(toolChoice){if(isRecord(toolChoice)){if(toolChoice.type==="function")return isRecord(toolChoice.function)&&typeof toolChoice.function.name=="string"?toolChoice.function.name:typeof toolChoice.name=="string"?toolChoice.name:void 0;if(!(toolChoice.type!=="custom"&&toolChoice.type!=="tool"))return typeof toolChoice.name=="string"?toolChoice.name:void 0}}function toolName(tool){if(isRecord(tool))return tool.type==="function"&&isRecord(tool.function)&&typeof tool.function.name=="string"?tool.function.name:typeof tool.name=="string"?tool.name:void 0}function limitGitHubCopilotTools(provider,tools,toolChoice,priorityNames){if(provider!=="github-copilot"||tools===void 0||tools.length<=GITHUB_COPILOT_TOOL_LIMIT)return{tools,omittedCount:0};let forcedName=selectedToolName(toolChoice);if(priorityNames!==void 0)return limitByPriority(tools,forcedName,priorityNames);let forcedIndex=forcedName===void 0?-1:tools.findIndex(tool=>toolName(tool)===forcedName);if(forcedIndex>=GITHUB_COPILOT_TOOL_LIMIT){let forcedTool=tools[forcedIndex];if(forcedTool!==void 0)return{tools:[...tools.slice(0,GITHUB_COPILOT_TOOL_LIMIT-1),forcedTool],omittedCount:tools.length-GITHUB_COPILOT_TOOL_LIMIT}}return{tools:tools.slice(0,GITHUB_COPILOT_TOOL_LIMIT),omittedCount:tools.length-GITHUB_COPILOT_TOOL_LIMIT}}function limitByPriority(tools,forcedName,priorityNames){let rank=new Map(priorityNames.map((name,index)=>[name,index])),rankOf=(tool,index)=>{let name=toolName(tool);return name!==void 0&&name===forcedName?-1:rank.get(name??"")??priorityNames.length+index},kept=new Set(tools.map((tool,index)=>({index,rank:rankOf(tool,index)})).sort((a,b)=>a.rank-b.rank).slice(0,GITHUB_COPILOT_TOOL_LIMIT).map(entry=>entry.index));return{tools:tools.filter((_,index)=>kept.has(index)),omittedCount:tools.length-GITHUB_COPILOT_TOOL_LIMIT}}function recordGitHubCopilotToolLimit(message,omittedCount){omittedCount===0||message.diagnostics?.some(diagnostic=>diagnostic.type===GITHUB_COPILOT_TOOL_LIMIT_DIAGNOSTIC)||appendAssistantMessageDiagnostic(message,{type:GITHUB_COPILOT_TOOL_LIMIT_DIAGNOSTIC,timestamp:Date.now(),details:{limit:GITHUB_COPILOT_TOOL_LIMIT,omittedCount,message:`GitHub Copilot accepts at most ${GITHUB_COPILOT_TOOL_LIMIT} tools on endpoints without tool search; senpi omitted ${omittedCount} excess tool definition${omittedCount===1?"":"s"}.`}})}function formatGitHubCopilotToolLimitError(message,errorMessage){let toolLimitApplied=message.diagnostics?.some(diagnostic=>diagnostic.type===GITHUB_COPILOT_TOOL_LIMIT_DIAGNOSTIC);return message.provider!=="github-copilot"||!toolLimitApplied||message.providerDiagnostic?.httpStatus!==400||!/400 Bad Request$/i.test(errorMessage)?errorMessage:`GitHub Copilot rejected the request after senpi limited its tool list to ${GITHUB_COPILOT_TOOL_LIMIT}. The endpoint may enforce a lower limit or count additional hosted tools. (${errorMessage})`}var QUOTA_CODES=new Set(["quota_exceeded","free_quota_exceeded","overage_limit_reached","billing_not_configured","additional_spend_limit_reached"]);function headerValue(headers,name){if(headers instanceof Headers)return headers.get(name)??void 0;if(!(typeof headers!="object"||headers===null)){for(let[key,value]of Object.entries(headers))if(key.toLowerCase()===name&&typeof value=="string")return value}}function errorCode(body){if(body!==void 0)try{let parsed=JSON.parse(body);if(typeof parsed!="object"||parsed===null)return;let nested=Reflect.get(parsed,"error"),code=Reflect.get(typeof nested=="object"&&nested!==null?nested:parsed,"code");return typeof code=="string"?code:void 0}catch{return}}function isQuotaFailure(status,body,limitKey){let code=errorCode(body);return code!==void 0&&QUOTA_CODES.has(code)||status===402?!0:status===429&&(limitKey?.includes("quota")===!0||/quota exceeded/i.test(body??""))}function describeGitHubCopilotFailure(error){if(!(error instanceof Error))return;let{status,body}=normalizeProviderError(error);if(status===void 0)return;let headers=Reflect.get(error,"headers"),requestId=headerValue(headers,"x-github-request-id"),reference=requestId===void 0?"":` ${formatProviderRequestId("GitHub",requestId)}.`;if(isQuotaFailure(status,body,headerValue(headers,"x-ratelimit-exceeded")))return`GitHub Copilot quota exceeded (HTTP ${status}): the plan's included usage or its additional-usage limit is used up, so premium models are refused until it resets or the limit is raised. Usage is shown at https://github.com/settings/copilot.${reference}`;if(status===421)return`GitHub Copilot sent this account to a different API host (HTTP 421 Misdirected Request): Business and Enterprise accounts are served from their own host, which senpi reads from the Copilot token. Run /login github-copilot so senpi stores the account's endpoint, and report it with the GitHub request id if it persists.${reference}`;if(status===403)return`GitHub Copilot refused the request (HTTP 403${body===void 0?" with an empty body":""}). Copilot checks access per model and per client; if a fresh /login github-copilot does not help, report it with the GitHub request id.${reference}`}function withGitHubCopilotFailureNote(message,provider,error){if(provider!=="github-copilot")return message;let note=describeGitHubCopilotFailure(error);return note===void 0?message:`${message} ${note}`}export{limitGitHubCopilotTools,recordGitHubCopilotToolLimit,formatGitHubCopilotToolLimitError,withGitHubCopilotFailureNote};