@earendil-works/pi-coding-agent
Version:
Coding agent CLI with read, bash, edit, write tools and session management
6 lines (4 loc) • 4.65 kB
JavaScript
import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url);
import{isRecord,parseClassifierUsage,postClassifierRequest,requiredNumber}from"./chunk-ZBIZYD6W.js";import"./chunk-PMRZO46Y.js";import{formatProviderError,normalizeProviderError}from"./chunk-RKNK4Y6L.js";import"./chunk-UTB4DTYQ.js";import"./chunk-AUUCDC7R.js";import"./chunk-LPXNMA2W.js";import"./chunk-BAC3QPNW.js";import"./chunk-JXWJOIH4.js";var LABEL="OpenAI Decisions",MAX_IMAGES=128;function predicateInstructions(question){let meanings=[question.criteria.true?`True means: ${question.criteria.true}`:"",question.criteria.false?`False means: ${question.criteria.false}`:""].filter(Boolean);return meanings.length>0?`${question.instructions}
${meanings.join(`
`)}`:question.instructions}function wireQuestion(name,question){return question.type==="choice"?{type:"choice",name,instructions:question.instructions,choices:Object.entries(question.criteria).map(([value,description])=>description?{value,description}:{value})}:question.type==="score"?{type:"score",name,instructions:question.instructions,levels:question.criteria.map(label=>({label}))}:{type:"predicate",name,instructions:predicateInstructions(question)}}function wireInput(context){let state=JSON.stringify(context.state),images=context.images??[];if(images.length===0)return state;if(images.length>MAX_IMAGES)throw new Error(`${LABEL} accepts at most ${MAX_IMAGES} images, got ${images.length}`);return[{role:"user",content:[{type:"input_text",text:state},...images.map(image=>({type:"input_image",image_url:`data:${image.mimeType};base64,${image.data}`}))]}]}function choiceProbabilities(value,id){if(!Array.isArray(value))throw new Error(`${LABEL} returned invalid probabilities for ${id}`);return Object.fromEntries(value.map(entry=>{if(!isRecord(entry)||typeof entry.value!="string")throw new Error(`${LABEL} returned invalid probabilities for ${id}`);return[entry.value,requiredNumber(LABEL,entry.probability,`probability for ${id}.${entry.value}`)]}))}function parseAnswer(id,question,answer){if(answer.type==="refusal")throw new Error(`${LABEL} refused to answer ${id}`);if(question.type==="choice"){if(answer.type!=="choice"||typeof answer.choice!="string")throw new Error(`${LABEL} did not return a choice answer for ${id}`);return{type:"choice",choice:answer.choice,probabilities:choiceProbabilities(answer.probabilities,id),confidence:requiredNumber(LABEL,answer.confidence,`confidence for ${id}`)}}if(question.type==="score"){if(answer.type!=="score")throw new Error(`${LABEL} did not return a score answer for ${id}`);return{type:"score",score:requiredNumber(LABEL,answer.score,`score for ${id}`),confidence:requiredNumber(LABEL,answer.confidence,`confidence for ${id}`)}}if(answer.type!=="predicate")throw new Error(`${LABEL} did not return a predicate answer for ${id}`);return{type:"bool",probability:requiredNumber(LABEL,answer.probability,`probability for ${id}`)}}function parseAnswers(value,context){if(!Array.isArray(value))throw new Error(`${LABEL} returned an unexpected response`);let byName=new Map;for(let answer of value)isRecord(answer)&&typeof answer.name=="string"&&byName.set(answer.name,answer);return Object.fromEntries(Object.entries(context.questions).map(([id,question])=>{let answer=byName.get(id);if(!answer)throw new Error(`${LABEL} did not return an answer for ${id}`);return[id,parseAnswer(id,question,answer)]}))}var NO_RETRY_STATUSES=[504];function errorMessage(error){return error.status===504?`${LABEL} error (504): the request timed out at the gateway. Very large inputs (above roughly 600K tokens) currently exceed its time limit.`:formatProviderError(normalizeProviderError(error),`${LABEL} error`)}var classify=async(model,context,options)=>{let output={api:model.api,provider:model.provider,model:model.id,answers:{},stopReason:"stop",timestamp:Date.now()};try{if(model.api!=="openai-decisions")throw new Error(`Unsupported classifier API: ${model.api}`);let body=await postClassifierRequest(LABEL,new URL("decisions",`${model.baseUrl.replace(/\/+$/u,"")}/`),model,{model:model.id,input:wireInput(context),questions:Object.entries(context.questions).map(([id,question])=>wireQuestion(id,question))},options,NO_RETRY_STATUSES);if(!isRecord(body))throw new Error(`${LABEL} returned an unexpected response`);let usage=parseClassifierUsage(body.usage,model);return usage&&(output.usage=usage),output.answers=parseAnswers(body.answers,context),output}catch(error){return output.answers={},output.stopReason=options?.signal?.aborted?"aborted":"error",output.errorMessage=errorMessage(error),output}};export{classify};