@juspay/neurolink 9.92.1 → 9.92.2
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/CHANGELOG.md +6 -0
- package/dist/browser/neurolink.min.js +1 -1
- package/dist/lib/providers/sagemaker/language-model.d.ts +15 -2
- package/dist/lib/providers/sagemaker/language-model.js +52 -25
- package/dist/providers/sagemaker/language-model.d.ts +15 -2
- package/dist/providers/sagemaker/language-model.js +52 -25
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
## [9.92.2](https://github.com/juspay/neurolink/compare/v9.92.1...v9.92.2) (2026-07-17)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
- **(sagemaker):** fix tool/tool-choice format crash on AI SDK's flat tool shape ([344d628](https://github.com/juspay/neurolink/commit/344d628f44fb5a00c0862cc346f51afca8e7473c))
|
|
6
|
+
|
|
1
7
|
## [9.92.1](https://github.com/juspay/neurolink/compare/v9.92.0...v9.92.1) (2026-07-17)
|
|
2
8
|
|
|
3
9
|
### Bug Fixes
|
|
@@ -1442,7 +1442,7 @@ ${o.join(`
|
|
|
1442
1442
|
`||e==="\r"}isDigit(e){return e>="0"&&e<="9"}isValidHexString(e){if(e.length!==4)return!1;for(let t=0;t<4;t++){let n=e[t];if(!(n>="0"&&n<="9"||n>="a"&&n<="f"||n>="A"&&n<="F"))return!1}return!0}reset(){this.buffer="",this.currentObject={},this.currentPath=[],this.inString=!1,this.escapeNext=!1,this.bracketCount=0,this.arrayBracketCount=0,this.lastProcessedLength=0,this.lastKeyValueParsePosition=0,this.bracketTypeStack=[]}getState(){return{bufferLength:this.buffer.length,currentPath:[...this.currentPath],inString:this.inString,objectKeys:Object.keys(this.currentObject),bracketCount:this.bracketCount,arrayBracketCount:this.arrayBracketCount,lastProcessedLength:this.lastProcessedLength,lastKeyValueParsePosition:this.lastKeyValueParsePosition,bracketTypeStack:[...this.bracketTypeStack]}}}});function Kce(r,e){if(e.escapeNext)return e.escapeNext=!1,{isValid:!0};if(r==="\\")return e.escapeNext=!0,{isValid:!0};if(r==='"'&&!e.escapeNext)return e.inString=!e.inString,{isValid:!0};if(!e.inString)switch(r){case"{":e.braceCount++;break;case"}":if(e.braceCount--,e.braceCount<0)return{isValid:!1,reason:"Unmatched closing brace"};break;case"[":e.bracketCount++;break;case"]":if(e.bracketCount--,e.bracketCount<0)return{isValid:!1,reason:"Unmatched closing bracket"};break}return{isValid:!0}}function obr(r){if(r.length<nbr)return{isComplete:!1,reason:"Too short"};if(!r.startsWith("{")||!r.endsWith("}"))return{isComplete:!1,reason:"Missing object braces"};let e={braceCount:0,bracketCount:0,inString:!1,escapeNext:!1};for(let t=0;t<r.length;t++){let n=r[t],o=Kce(n,e);if(!o.isValid)return{isComplete:!1,reason:o.reason}}return e.inString?{isComplete:!1,reason:"Unterminated string"}:e.braceCount!==0?{isComplete:!1,reason:`Unbalanced braces: ${e.braceCount}`}:e.bracketCount!==0?{isComplete:!1,reason:`Unbalanced brackets: ${e.bracketCount}`}:{isComplete:!0}}function sbr(r){let e=r.trim();if(e.length===0)return{arguments:"{}",complete:!0};if(obr(e).isComplete)try{let n=JSON.parse(e);return typeof n=="object"&&n!==null&&!Array.isArray(n)?{arguments:e,complete:!0}:{argumentsDelta:e}}catch(n){return g.debug("JSON parsing failed for tool arguments",{args:e.substring(0,100),error:YGe(n)}),{argumentsDelta:e}}else return{argumentsDelta:e}}function YGe(r){return r instanceof Error?r.message:String(r)}function XGe(r){return _r(r)&&r.message||String(r)}var nbr,LN,Yce,Xce,yH,xH,Jce=k(()=>{"use strict";Kb();JGe();yk();H();vr();nbr=2;LN=class{buffer="";isCompleted=!1;totalUsage;structuredParser;responseSchema;isComplete(e){return e.done===!0||e.finishReason!==void 0}extractUsage(e){return e.usage||this.totalUsage}reset(){this.buffer="",this.isCompleted=!1,this.totalUsage=void 0,this.structuredParser?.reset()}enableStructuredOutput(e){this.responseSchema=e,this.structuredParser=WGe(e)}parseStructuredContent(e){if(!(!this.structuredParser||!KGe(e)))return this.structuredParser.parseChunk(e)}decodeChunk(e){return new TextDecoder().decode(e)}parseJSON(e){try{return JSON.parse(e)}catch(t){return g.warn("Failed to parse JSON in streaming response",{text:e,error:t}),null}}},Yce=class extends LN{getName(){return"HuggingFace SSE Parser"}parse(e){let t=this.decodeChunk(e);this.buffer+=t;let n=[],o=this.buffer.split(`
|
|
1443
1443
|
`);this.buffer=o.pop()||"";for(let s of o){let i=s.trim();if(!(!i||i.startsWith(":"))&&i.startsWith("data: ")){let a=i.substring(6);if(a==="[DONE]"){n.push({content:"",done:!0,finishReason:"stop"}),this.isCompleted=!0;continue}let c=this.parseJSON(a);if(c&&_r(c)){let l=this.parseHuggingFaceChunk(c);l&&n.push(l)}}}return n}parseHuggingFaceChunk(e){if(e.token){let t=e.token;return{content:typeof t.text=="string"?t.text:String(t),done:!1}}if(e.generated_text!==void 0){let t=e.details;return{content:String(e.generated_text),done:t?.finish_reason!==void 0,finishReason:this.mapFinishReason(t?.finish_reason),usage:t?this.parseHuggingFaceUsage(t):void 0}}if(e.error){let t=XGe(e.error);throw new On(`HuggingFace streaming error: ${t}`,{code:"MODEL_ERROR",statusCode:500,retryable:!1})}return null}parseHuggingFaceUsage(e){if(!e.tokens)return;let t=e.tokens;return{promptTokens:Number(t.input)||0,completionTokens:Number(t.generated)||0,total:Number(t.total)||0}}mapFinishReason(e){switch(e){case"stop":return"stop";case"length":return"length";case"eos_token":return"stop";default:return"unknown"}}},Xce=class extends LN{getName(){return"LLaMA JSONL Parser"}parse(e){let t=this.decodeChunk(e);this.buffer+=t;let n=[],o=this.buffer.split(`
|
|
1444
1444
|
`);this.buffer=o.pop()||"";for(let s of o){let i=s.trim();if(!i)continue;let a=this.parseJSON(i);if(a&&_r(a)){let c=this.parseLlamaChunk(a);c&&n.push(c)}}return n}parseLlamaChunk(e){if(Array.isArray(e.choices)&&e.choices[0]){let t=e.choices[0];if(t.delta){let n=t.delta,o=String(n.content||""),s=t.finish_reason,i={content:o,done:s!=null,finishReason:this.mapFinishReason(s||null),usage:e.usage?this.parseLlamaUsage(e.usage):void 0};if(o&&this.structuredParser&&(i.structuredOutput=this.parseStructuredContent(o)),Array.isArray(n.tool_calls)&&n.tool_calls[0]){let a=n.tool_calls[0];i.toolCall=this.parseStreamingToolCall(a),s==="function_call"&&i.toolCall.arguments&&(i.toolCall.complete=!0)}return i}if(t.text!==void 0)return{content:String(t.text),done:t.finish_reason!==null,finishReason:this.mapFinishReason(t.finish_reason),usage:e.usage?this.parseLlamaUsage(e.usage):void 0}}if(e.content!==void 0)return{content:String(e.content),done:!!e.done,finishReason:this.mapFinishReason(e.finish_reason),usage:e.usage?this.parseLlamaUsage(e.usage):void 0};if(e.error){let t=e.error,n=XGe(t);throw new On(`LLaMA streaming error: ${n}`,{code:"MODEL_ERROR",statusCode:500,retryable:!1})}return null}parseToolCallArguments(e,t){if(typeof e.arguments=="string"){let n=sbr(e.arguments);n.complete?(t.arguments=n.arguments,t.complete=!0):n.argumentsDelta!==void 0&&(t.argumentsDelta=n.argumentsDelta)}else if(e.arguments!==void 0)try{t.arguments=JSON.stringify(e.arguments),t.complete=!0}catch(n){g.warn("Failed to stringify tool arguments",{args:e.arguments,error:YGe(n)}),t.arguments="{}",t.complete=!0}else t.arguments="{}",t.complete=!0}parseStreamingToolCall(e){let t={id:String(e.id||`call_${We()}`),type:"function"},n=e.function;return n?.name&&(t.name=String(n.name)),n?.arguments&&this.parseToolCallArguments(n,t),t}parseLlamaUsage(e){return{promptTokens:Number(e.prompt_tokens)||0,completionTokens:Number(e.completion_tokens)||0,total:Number(e.total_tokens)||0}}mapFinishReason(e){switch(e){case"stop":return"stop";case"length":return"length";case"function_call":return"tool-calls";case"content_filter":return"content-filter";default:return e?"unknown":void 0}}},yH=class extends LN{expectedFormat="json";constructor(e="json"){super(),this.expectedFormat=e}getName(){return`Custom ${this.expectedFormat.toUpperCase()} Parser`}parse(e){let t=this.decodeChunk(e);return this.buffer+=t,this.expectedFormat==="json"?this.parseJSONFormat():this.parseTextFormat()}parseJSONFormat(){let e=[],t=0;for(;t<this.buffer.length;)try{let n=this.buffer.substring(t),o=JSON.parse(n),s={content:o.text||o.generated_text||o.output||String(o),done:!0,finishReason:"stop"};e.push(s),this.buffer="",this.isCompleted=!0;break}catch{let n=this.buffer.indexOf(`
|
|
1445
|
-
`,t);if(n===-1)break;let o=this.buffer.substring(t,n);if(o.trim()){let s=this.parseJSON(o.trim());if(s&&_r(s)){let i=this.parseCustomChunk(s);i&&e.push(i)}}t=n+1}return t>0&&(this.buffer=this.buffer.substring(t)),e}parseTextFormat(){if(this.buffer){let e=this.buffer;return this.buffer="",[{content:e,done:!1}]}return[]}parseCustomChunk(e){let t=e.text||e.generated_text||e.output||e.response||e.content||(typeof e=="string"?e:JSON.stringify(e));return{content:String(t),done:!!(e.done||e.finished||e.complete),finishReason:e.finish_reason||e.status==="complete"?"stop":void 0,usage:e.usage||e.tokens?this.parseCustomUsage(e):void 0}}parseCustomUsage(e){let t=e.usage||e.tokens||{};return{promptTokens:Number(t.prompt_tokens||t.input_tokens)||0,completionTokens:Number(t.completion_tokens||t.output_tokens)||0,total:Number(t.total_tokens)||0}}},xH=class{static createParser(e,t){switch(e){case"sse":return new Yce;case"jsonl":return new Xce;case"chunked":{let n=t?.format;return new yH(n||"json")}default:return new yH("text")}}static getSupportedProtocols(){return["sse","jsonl","chunked","text"]}}});async function QGe(r,e,t,n={}){let o=qGe(t);try{g.debug("Detecting streaming capabilities",{endpointName:e});let s=await o.detectStreamingCapability(e);if(!s.supported)return g.info("Streaming not supported, falling back to synthetic stream",{endpointName:e,modelType:s.modelType}),ZGe(r,n);g.info("Creating streaming response",{endpointName:e,protocol:s.protocol,modelType:s.modelType,confidence:s.confidence});let i=xH.createParser(s.protocol);return abr(r,i,s,n)}catch(s){return g.error("Failed to create streaming response",{endpointName:e,error:s instanceof Error?s.message:String(s)}),ZGe(r,n)}}async function abr(r,e,t,n){return new Vce({async start(o){let s=r[Symbol.asyncIterator](),i="",a;try{for(e.reset();;){if(n.abortSignal?.aborted){try{await s.return?.()}catch{}throw new On("Stream aborted by user",{code:"NETWORK_ERROR",statusCode:499,retryable:!1})}let{done:c,value:l}=await s.next();if(c){!a&&i&&(a=UN("",i));let d={type:"finish",finishReason:"stop",usage:a};o.enqueue(d),n.onComplete?.(a||{promptTokens:0,completionTokens:0,total:0}),o.close();break}let u=e.parse(l);for(let d of u){if(d.content&&(i+=d.content),d.toolCall){let p={type:"tool-call-delta",toolCallType:"function",toolCallId:d.toolCall.id,toolName:d.toolCall.name||"",argsTextDelta:d.toolCall.argumentsDelta||""};if(o.enqueue(p),n.onChunk?.(d),d.toolCall.complete&&d.toolCall.arguments){let f={type:"tool-call",toolCallType:"function",toolCallId:d.toolCall.id,toolName:d.toolCall.name||"",args:JSON.parse(d.toolCall.arguments)};o.enqueue(f)}continue}if(d.toolResult){let p={type:"tool-result",toolCallId:d.toolResult.toolCallId,toolName:d.toolResult.toolName,result:d.toolResult.result,args:{}};o.enqueue(p),n.onChunk?.(d);continue}if(d.structuredOutput){let p={type:"object-delta",objectDelta:d.structuredOutput.partialObject||{},objectPath:d.structuredOutput.currentPath||"",isComplete:d.structuredOutput.complete||!1,validationErrors:d.structuredOutput.validationErrors||[]};if(o.enqueue(p),n.onChunk?.(d),d.structuredOutput.complete&&d.structuredOutput.partialObject){let f={type:"object",object:d.structuredOutput.partialObject};o.enqueue(f)}continue}if(d.content){let p={type:"text-delta",textDelta:d.content};o.enqueue(p),n.onChunk?.(d)}if(e.isComplete(d)){a=e.extractUsage(d)||UN("",i);let p={type:"finish",finishReason:d.finishReason||"stop",usage:a};o.enqueue(p),n.onComplete?.(a),o.close();return}}}}catch(c){let l=Fu(c);g.error("Streaming error",{error:l.message,modelType:t.modelType,protocol:t.protocol}),n.onError?.(l),o.error(l)}}})}async function ZGe(r,e){return new Vce({async start(t){try{let n=[],o=r[Symbol.asyncIterator]();for(;;){let{done:y,value:x}=await o.next();if(y)break;n.push(x)}let s=0;for(let y of n)s+=y.length;let i=new Uint8Array(s),a=0;for(let y of n)i.set(y,a),a+=y.length;let c=new TextDecoder().decode(i),l=JSON.parse(c),u=l.generated_text||l.text||l.output||l[0]?.generated_text||String(l),d=u.split(/\s+/),p=Math.max(1,Math.floor(d.length/10));for(let y=0;y<d.length;y+=p){let x=d.slice(y,y+p).join(" "),b=y===0?x:" "+x,T={type:"text-delta",textDelta:b};t.enqueue(T),e.onChunk?.({content:b,done:!1}),await new Promise(S=>setTimeout(S,ibr))}let f=UN(e.prompt||"",u),h={type:"finish",finishReason:"stop",usage:f};t.enqueue(h),e.onComplete?.(f),t.close()}catch(n){let o=Fu(n);e.onError?.(o),t.error(o)}}})}function UN(r,e){let t=bn(r,"sagemaker"),n=bn(e,"sagemaker");return{promptTokens:t,completionTokens:n,total:t+n}}var ibr,eVe=k(()=>{"use strict";VGe();yk();H();Ed();HGe();Jce();ibr=50});function tVe(r,e=10,t=1){return new Zce({initialConcurrency:r,maxConcurrency:e,minConcurrency:t})}var Zce,rVe=k(()=>{"use strict";H();Zce=class{count;waiters=[];currentConcurrency;activeRequests=0;completedCount=0;errorCount=0;responseTimes=[];maxConcurrency;minConcurrency;constructor(e){this.currentConcurrency=e.initialConcurrency,this.count=e.initialConcurrency,this.maxConcurrency=e.maxConcurrency,this.minConcurrency=e.minConcurrency,g.debug("AdaptiveSemaphore initialized",{initialConcurrency:e.initialConcurrency,maxConcurrency:e.maxConcurrency,minConcurrency:e.minConcurrency})}async acquire(){return new Promise(e=>{this.count>0?(this.count--,this.activeRequests++,e()):this.waiters.push(()=>{this.count--,this.activeRequests++,e()})})}release(){if(this.activeRequests--,this.waiters.length>0){let e=this.waiters.shift();e&&(this.count++,e())}else this.count++}recordSuccess(e){this.completedCount++,this.responseTimes.push(e),this.responseTimes.length>10&&this.responseTimes.shift(),this.adjustConcurrencyBasedOnPerformance(e,!1)}recordError(e){this.errorCount++,e&&(this.responseTimes.push(e),this.responseTimes.length>10&&this.responseTimes.shift()),this.adjustConcurrencyBasedOnPerformance(e||0,!0)}adjustConcurrency(e){let t=Math.max(this.minConcurrency,Math.min(this.maxConcurrency,e)),n=t-(this.currentConcurrency-this.count);for(this.count+=n,this.currentConcurrency=t,g.debug("Concurrency adjusted",{newConcurrency:t,previousConcurrency:this.currentConcurrency-n,availableCount:this.count,activeRequests:this.activeRequests});this.count>0&&this.waiters.length>0;){let o=this.waiters.shift();o&&o()}}getMetrics(){let e=this.responseTimes.length>0?this.responseTimes.reduce((t,n)=>t+n,0)/this.responseTimes.length:0;return{activeRequests:this.activeRequests,currentConcurrency:this.currentConcurrency,completedCount:this.completedCount,errorCount:this.errorCount,averageResponseTime:e,waitingCount:this.waiters.length}}resetMetrics(){this.completedCount=0,this.errorCount=0,this.responseTimes=[]}adjustConcurrencyBasedOnPerformance(e,t){let n=this.getMetrics();if(t){this.currentConcurrency>this.minConcurrency&&(this.adjustConcurrency(Math.max(this.minConcurrency,this.currentConcurrency-1)),g.warn("Reduced concurrency due to error",{newConcurrency:this.currentConcurrency,errorCount:this.errorCount}));return}if(this.completedCount<3)return;let o=2e3;e<o&&n.averageResponseTime<o&&this.currentConcurrency<this.maxConcurrency?(this.adjustConcurrency(Math.min(this.maxConcurrency,this.currentConcurrency+1)),g.debug("Increased concurrency due to fast responses",{newConcurrency:this.currentConcurrency,averageResponseTime:n.averageResponseTime})):e>5e3&&this.currentConcurrency>this.minConcurrency&&(this.adjustConcurrency(Math.max(this.minConcurrency,this.currentConcurrency-1)),g.debug("Decreased concurrency due to slow responses",{newConcurrency:this.currentConcurrency,responseTime:e}))}isIdle(){return this.activeRequests===0&&this.waiters.length===0}getCurrentConcurrency(){return this.currentConcurrency}getActiveRequestCount(){return this.activeRequests}getWaitingRequestCount(){return this.waiters.length}}});function ubr(r,e){let t=Math.max(10,Math.min(lbr,cbr*(1e3/Math.max(r,100))));return e>20?Math.max(10,t*.5):e>10?Math.max(15,t*.7):t}async function*dbr(r){if(!r)return;let e=r.split(/\s+/),t=Math.max(1,Math.floor(e.length/10)),n=Math.ceil(e.length/t),o=ubr(r.length,n);for(let s=0;s<e.length;s+=t){let i=e.slice(s,s+t).join(" "),a=s===0?i:" "+i;s>0&&await new Promise(c=>setTimeout(c,o)),yield a}}var cbr,lbr,pbr,mbr,fbr,vH,nVe=k(()=>{"use strict";vr();Gce();yk();eVe();rVe();H();cbr=process.env.SAGEMAKER_BASE_STREAMING_DELAY_MS?parseInt(process.env.SAGEMAKER_BASE_STREAMING_DELAY_MS,10):50,lbr=process.env.SAGEMAKER_MAX_STREAMING_DELAY_MS?parseInt(process.env.SAGEMAKER_MAX_STREAMING_DELAY_MS,10):200;pbr=5,mbr=10,fbr=1,vH=class{specificationVersion="v2";provider="sagemaker";modelId;supportsStreaming=!0;defaultObjectGenerationMode="json";supportedUrls={};client;config;modelConfig;constructor(e,t,n){this.modelId=e,this.config=t,this.modelConfig=n,this.client=new vk(t),g.debug("SageMaker Language Model initialized",{modelId:this.modelId,endpointName:this.modelConfig.endpointName,provider:this.provider,specificationVersion:this.specificationVersion})}async doGenerate(e){let t=Date.now();try{let n=this.extractPromptText(e);g.debug("SageMaker doGenerate called",{endpointName:this.modelConfig.endpointName,promptLength:n.length,maxTokens:e.maxTokens,temperature:e.temperature});let o=this.convertToSageMakerRequest(e),s=await this.client.invokeEndpoint({EndpointName:this.modelConfig.endpointName,Body:JSON.stringify(o),ContentType:"application/json",Accept:"application/json"}),i=JSON.parse(new TextDecoder().decode(s.Body)),a=this.extractTextFromResponse(i),c=this.extractToolCallsFromResponse(i),l=UN(n,a),u="stop";c&&c.length>0?u="tool-calls":i.finish_reason&&(u=this.mapSageMakerFinishReason(i.finish_reason));let d=Date.now()-t;g.debug("SageMaker doGenerate completed",{duration:d,outputLength:a.length,usage:l,toolCallsCount:c?.length||0,finishReason:u});let p={text:a,usage:{inputTokens:l.promptTokens,outputTokens:l.completionTokens,totalTokens:l.total},finishReason:u,rawCall:{rawPrompt:e.prompt,rawSettings:{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,endpointName:this.modelConfig.endpointName}},rawResponse:{headers:{"content-type":s.ContentType||"application/json","invoked-variant":s.InvokedProductionVariant||""}},request:{body:JSON.stringify(o)}};c&&c.length>0&&(p.toolCalls=c);let f=o.response_format;if(f&&(f.type==="json_object"||f.type==="json_schema"))try{let h=JSON.parse(a);p.object=h,g.debug("Extracted structured data from response",{responseFormat:f.type,hasObject:!!p.object})}catch(h){g.warn("Failed to parse structured response as JSON",{error:h instanceof Error?h.message:String(h),responseText:a.substring(0,200)})}return p}catch(n){let o=Date.now()-t;throw g.error("SageMaker doGenerate failed",{duration:o,error:n instanceof Error?n.message:String(n)}),Fu(n,this.modelConfig.endpointName)}}async doStream(e){try{let t=this.extractPromptText(e);g.debug("SageMaker doStream called",{endpointName:this.modelConfig.endpointName,promptLength:t.length});let n=this.convertToSageMakerRequest(e),o={...n,parameters:{...typeof n.parameters=="object"&&n.parameters!==null?n.parameters:{},stream:!0}};g.debug("Attempting streaming generation",{endpointName:this.modelConfig.endpointName,hasStreamingFlag:!0});try{let s=await this.client.invokeEndpointWithStreaming({EndpointName:this.modelConfig.endpointName,Body:JSON.stringify(o),ContentType:this.modelConfig.contentType||"application/json",Accept:this.modelConfig.accept||"application/json"});return{stream:await QGe(s.Body,this.modelConfig.endpointName,this.config,{prompt:t,onChunk:a=>{g.debug("Streaming chunk received",{contentLength:a.content?.length||0,done:a.done})},onComplete:a=>{g.debug("Streaming completed",{usage:a,endpointName:this.modelConfig.endpointName})},onError:a=>{g.error("Streaming error",{error:a.message,endpointName:this.modelConfig.endpointName})}}),rawCall:{rawPrompt:n,rawSettings:this.modelConfig},rawResponse:{headers:{"Content-Type":s.ContentType||"application/json","X-Invoked-Production-Variant":s.InvokedProductionVariant||"unknown"}}}}catch(s){g.warn("Streaming failed, falling back to non-streaming",{endpointName:this.modelConfig.endpointName,error:s instanceof Error?s.message:String(s)});let i=await this.doGenerate(e);return{stream:new ReadableStream({async start(c){try{let l=dbr(i.text);for await(let u of l)c.enqueue({type:"text-delta",textDelta:u});c.enqueue({type:"finish",finishReason:i.finishReason,usage:i.usage}),c.close()}catch(l){c.error(l)}}}),rawCall:i.rawCall,rawResponse:i.rawResponse,request:i.request,warnings:[...i.warnings||[],{type:"other",message:"Streaming not supported, using synthetic stream"}]}}}catch(t){throw g.error("SageMaker doStream failed",{error:t instanceof Error?t.message:String(t)}),Fu(t,this.modelConfig.endpointName)}}convertToSageMakerRequest(e){let t=this.extractPromptText(e),n={inputs:t,parameters:{...e.maxTokens!==void 0?{max_new_tokens:e.maxTokens}:{},temperature:e.temperature||.7,top_p:e.topP||.9,stop:e.stopSequences||[]}},o=e.tools;if(o&&Array.isArray(o)&&o.length>0){n.tools=this.convertToolsToSageMakerFormat(o);let i=e.toolChoice;i&&(n.tool_choice=this.convertToolChoiceToSageMakerFormat(i)),g.debug("Added tool support to SageMaker request",{toolCount:o.length,toolChoice:i})}let s=e.responseFormat;return s&&(n.response_format=this.convertResponseFormatToSageMakerFormat(s),g.debug("Added structured output support to SageMaker request",{responseFormat:s.type})),g.debug("Converted to SageMaker request format",{inputLength:t.length,parameters:n.parameters,hasTools:!!n.tools}),n}convertToolsToSageMakerFormat(e){return e.map(t=>t.type==="function"?{type:"function",function:{name:t.function.name,description:t.function.description||"",parameters:t.function.parameters||{}}}:t)}convertToolChoiceToSageMakerFormat(e){return typeof e=="string"?e:e?.type==="function"?{type:"function",function:{name:e.function.name}}:e}convertResponseFormatToSageMakerFormat(e){return e.type==="json_object"?{type:"json_object",schema:e.schema||void 0}:e.type==="json_schema"?{type:"json_schema",json_schema:{name:e.json_schema?.name||"response",description:e.json_schema?.description||"Generated response",schema:e.json_schema?.schema||{}}}:{type:"text"}}extractPromptText(e){let t=e.messages;if(t&&Array.isArray(t))return t.filter(o=>o.role&&o.content).map(o=>typeof o.content=="string"?`${o.role}: ${o.content}`:`${o.role}: ${JSON.stringify(o.content)}`).join(`
|
|
1445
|
+
`,t);if(n===-1)break;let o=this.buffer.substring(t,n);if(o.trim()){let s=this.parseJSON(o.trim());if(s&&_r(s)){let i=this.parseCustomChunk(s);i&&e.push(i)}}t=n+1}return t>0&&(this.buffer=this.buffer.substring(t)),e}parseTextFormat(){if(this.buffer){let e=this.buffer;return this.buffer="",[{content:e,done:!1}]}return[]}parseCustomChunk(e){let t=e.text||e.generated_text||e.output||e.response||e.content||(typeof e=="string"?e:JSON.stringify(e));return{content:String(t),done:!!(e.done||e.finished||e.complete),finishReason:e.finish_reason||e.status==="complete"?"stop":void 0,usage:e.usage||e.tokens?this.parseCustomUsage(e):void 0}}parseCustomUsage(e){let t=e.usage||e.tokens||{};return{promptTokens:Number(t.prompt_tokens||t.input_tokens)||0,completionTokens:Number(t.completion_tokens||t.output_tokens)||0,total:Number(t.total_tokens)||0}}},xH=class{static createParser(e,t){switch(e){case"sse":return new Yce;case"jsonl":return new Xce;case"chunked":{let n=t?.format;return new yH(n||"json")}default:return new yH("text")}}static getSupportedProtocols(){return["sse","jsonl","chunked","text"]}}});async function QGe(r,e,t,n={}){let o=qGe(t);try{g.debug("Detecting streaming capabilities",{endpointName:e});let s=await o.detectStreamingCapability(e);if(!s.supported)return g.info("Streaming not supported, falling back to synthetic stream",{endpointName:e,modelType:s.modelType}),ZGe(r,n);g.info("Creating streaming response",{endpointName:e,protocol:s.protocol,modelType:s.modelType,confidence:s.confidence});let i=xH.createParser(s.protocol);return abr(r,i,s,n)}catch(s){return g.error("Failed to create streaming response",{endpointName:e,error:s instanceof Error?s.message:String(s)}),ZGe(r,n)}}async function abr(r,e,t,n){return new Vce({async start(o){let s=r[Symbol.asyncIterator](),i="",a;try{for(e.reset();;){if(n.abortSignal?.aborted){try{await s.return?.()}catch{}throw new On("Stream aborted by user",{code:"NETWORK_ERROR",statusCode:499,retryable:!1})}let{done:c,value:l}=await s.next();if(c){!a&&i&&(a=UN("",i));let d={type:"finish",finishReason:"stop",usage:a};o.enqueue(d),n.onComplete?.(a||{promptTokens:0,completionTokens:0,total:0}),o.close();break}let u=e.parse(l);for(let d of u){if(d.content&&(i+=d.content),d.toolCall){let p={type:"tool-call-delta",toolCallType:"function",toolCallId:d.toolCall.id,toolName:d.toolCall.name||"",argsTextDelta:d.toolCall.argumentsDelta||""};if(o.enqueue(p),n.onChunk?.(d),d.toolCall.complete&&d.toolCall.arguments){let f={type:"tool-call",toolCallType:"function",toolCallId:d.toolCall.id,toolName:d.toolCall.name||"",args:JSON.parse(d.toolCall.arguments)};o.enqueue(f)}continue}if(d.toolResult){let p={type:"tool-result",toolCallId:d.toolResult.toolCallId,toolName:d.toolResult.toolName,result:d.toolResult.result,args:{}};o.enqueue(p),n.onChunk?.(d);continue}if(d.structuredOutput){let p={type:"object-delta",objectDelta:d.structuredOutput.partialObject||{},objectPath:d.structuredOutput.currentPath||"",isComplete:d.structuredOutput.complete||!1,validationErrors:d.structuredOutput.validationErrors||[]};if(o.enqueue(p),n.onChunk?.(d),d.structuredOutput.complete&&d.structuredOutput.partialObject){let f={type:"object",object:d.structuredOutput.partialObject};o.enqueue(f)}continue}if(d.content){let p={type:"text-delta",textDelta:d.content};o.enqueue(p),n.onChunk?.(d)}if(e.isComplete(d)){a=e.extractUsage(d)||UN("",i);let p={type:"finish",finishReason:d.finishReason||"stop",usage:a};o.enqueue(p),n.onComplete?.(a),o.close();return}}}}catch(c){let l=Fu(c);g.error("Streaming error",{error:l.message,modelType:t.modelType,protocol:t.protocol}),n.onError?.(l),o.error(l)}}})}async function ZGe(r,e){return new Vce({async start(t){try{let n=[],o=r[Symbol.asyncIterator]();for(;;){let{done:y,value:x}=await o.next();if(y)break;n.push(x)}let s=0;for(let y of n)s+=y.length;let i=new Uint8Array(s),a=0;for(let y of n)i.set(y,a),a+=y.length;let c=new TextDecoder().decode(i),l=JSON.parse(c),u=l.generated_text||l.text||l.output||l[0]?.generated_text||String(l),d=u.split(/\s+/),p=Math.max(1,Math.floor(d.length/10));for(let y=0;y<d.length;y+=p){let x=d.slice(y,y+p).join(" "),b=y===0?x:" "+x,T={type:"text-delta",textDelta:b};t.enqueue(T),e.onChunk?.({content:b,done:!1}),await new Promise(S=>setTimeout(S,ibr))}let f=UN(e.prompt||"",u),h={type:"finish",finishReason:"stop",usage:f};t.enqueue(h),e.onComplete?.(f),t.close()}catch(n){let o=Fu(n);e.onError?.(o),t.error(o)}}})}function UN(r,e){let t=bn(r,"sagemaker"),n=bn(e,"sagemaker");return{promptTokens:t,completionTokens:n,total:t+n}}var ibr,eVe=k(()=>{"use strict";VGe();yk();H();Ed();HGe();Jce();ibr=50});function tVe(r,e=10,t=1){return new Zce({initialConcurrency:r,maxConcurrency:e,minConcurrency:t})}var Zce,rVe=k(()=>{"use strict";H();Zce=class{count;waiters=[];currentConcurrency;activeRequests=0;completedCount=0;errorCount=0;responseTimes=[];maxConcurrency;minConcurrency;constructor(e){this.currentConcurrency=e.initialConcurrency,this.count=e.initialConcurrency,this.maxConcurrency=e.maxConcurrency,this.minConcurrency=e.minConcurrency,g.debug("AdaptiveSemaphore initialized",{initialConcurrency:e.initialConcurrency,maxConcurrency:e.maxConcurrency,minConcurrency:e.minConcurrency})}async acquire(){return new Promise(e=>{this.count>0?(this.count--,this.activeRequests++,e()):this.waiters.push(()=>{this.count--,this.activeRequests++,e()})})}release(){if(this.activeRequests--,this.waiters.length>0){let e=this.waiters.shift();e&&(this.count++,e())}else this.count++}recordSuccess(e){this.completedCount++,this.responseTimes.push(e),this.responseTimes.length>10&&this.responseTimes.shift(),this.adjustConcurrencyBasedOnPerformance(e,!1)}recordError(e){this.errorCount++,e&&(this.responseTimes.push(e),this.responseTimes.length>10&&this.responseTimes.shift()),this.adjustConcurrencyBasedOnPerformance(e||0,!0)}adjustConcurrency(e){let t=Math.max(this.minConcurrency,Math.min(this.maxConcurrency,e)),n=t-(this.currentConcurrency-this.count);for(this.count+=n,this.currentConcurrency=t,g.debug("Concurrency adjusted",{newConcurrency:t,previousConcurrency:this.currentConcurrency-n,availableCount:this.count,activeRequests:this.activeRequests});this.count>0&&this.waiters.length>0;){let o=this.waiters.shift();o&&o()}}getMetrics(){let e=this.responseTimes.length>0?this.responseTimes.reduce((t,n)=>t+n,0)/this.responseTimes.length:0;return{activeRequests:this.activeRequests,currentConcurrency:this.currentConcurrency,completedCount:this.completedCount,errorCount:this.errorCount,averageResponseTime:e,waitingCount:this.waiters.length}}resetMetrics(){this.completedCount=0,this.errorCount=0,this.responseTimes=[]}adjustConcurrencyBasedOnPerformance(e,t){let n=this.getMetrics();if(t){this.currentConcurrency>this.minConcurrency&&(this.adjustConcurrency(Math.max(this.minConcurrency,this.currentConcurrency-1)),g.warn("Reduced concurrency due to error",{newConcurrency:this.currentConcurrency,errorCount:this.errorCount}));return}if(this.completedCount<3)return;let o=2e3;e<o&&n.averageResponseTime<o&&this.currentConcurrency<this.maxConcurrency?(this.adjustConcurrency(Math.min(this.maxConcurrency,this.currentConcurrency+1)),g.debug("Increased concurrency due to fast responses",{newConcurrency:this.currentConcurrency,averageResponseTime:n.averageResponseTime})):e>5e3&&this.currentConcurrency>this.minConcurrency&&(this.adjustConcurrency(Math.max(this.minConcurrency,this.currentConcurrency-1)),g.debug("Decreased concurrency due to slow responses",{newConcurrency:this.currentConcurrency,responseTime:e}))}isIdle(){return this.activeRequests===0&&this.waiters.length===0}getCurrentConcurrency(){return this.currentConcurrency}getActiveRequestCount(){return this.activeRequests}getWaitingRequestCount(){return this.waiters.length}}});function ubr(r,e){let t=Math.max(10,Math.min(lbr,cbr*(1e3/Math.max(r,100))));return e>20?Math.max(10,t*.5):e>10?Math.max(15,t*.7):t}async function*dbr(r){if(!r)return;let e=r.split(/\s+/),t=Math.max(1,Math.floor(e.length/10)),n=Math.ceil(e.length/t),o=ubr(r.length,n);for(let s=0;s<e.length;s+=t){let i=e.slice(s,s+t).join(" "),a=s===0?i:" "+i;s>0&&await new Promise(c=>setTimeout(c,o)),yield a}}var cbr,lbr,pbr,mbr,fbr,vH,nVe=k(()=>{"use strict";vr();Gce();yk();eVe();rVe();H();cbr=process.env.SAGEMAKER_BASE_STREAMING_DELAY_MS?parseInt(process.env.SAGEMAKER_BASE_STREAMING_DELAY_MS,10):50,lbr=process.env.SAGEMAKER_MAX_STREAMING_DELAY_MS?parseInt(process.env.SAGEMAKER_MAX_STREAMING_DELAY_MS,10):200;pbr=5,mbr=10,fbr=1,vH=class{specificationVersion="v2";provider="sagemaker";modelId;supportsStreaming=!0;defaultObjectGenerationMode="json";supportedUrls={};client;config;modelConfig;constructor(e,t,n){this.modelId=e,this.config=t,this.modelConfig=n,this.client=new vk(t),g.debug("SageMaker Language Model initialized",{modelId:this.modelId,endpointName:this.modelConfig.endpointName,provider:this.provider,specificationVersion:this.specificationVersion})}async doGenerate(e){let t=Date.now();try{let n=this.extractPromptText(e);g.debug("SageMaker doGenerate called",{endpointName:this.modelConfig.endpointName,promptLength:n.length,maxTokens:e.maxTokens,temperature:e.temperature});let o=this.convertToSageMakerRequest(e),s=await this.client.invokeEndpoint({EndpointName:this.modelConfig.endpointName,Body:JSON.stringify(o),ContentType:"application/json",Accept:"application/json"}),i=JSON.parse(new TextDecoder().decode(s.Body)),a=this.extractTextFromResponse(i),c=this.extractToolCallsFromResponse(i),l=UN(n,a),u="stop";c&&c.length>0?u="tool-calls":i.finish_reason&&(u=this.mapSageMakerFinishReason(i.finish_reason));let d=Date.now()-t;g.debug("SageMaker doGenerate completed",{duration:d,outputLength:a.length,usage:l,toolCallsCount:c?.length||0,finishReason:u});let p={text:a,usage:{inputTokens:l.promptTokens,outputTokens:l.completionTokens,totalTokens:l.total},finishReason:u,rawCall:{rawPrompt:e.prompt,rawSettings:{maxTokens:e.maxTokens,temperature:e.temperature,topP:e.topP,endpointName:this.modelConfig.endpointName}},rawResponse:{headers:{"content-type":s.ContentType||"application/json","invoked-variant":s.InvokedProductionVariant||""}},request:{body:JSON.stringify(o)}};c&&c.length>0&&(p.toolCalls=c);let f=o.response_format;if(f&&(f.type==="json_object"||f.type==="json_schema"))try{let h=JSON.parse(a);p.object=h,g.debug("Extracted structured data from response",{responseFormat:f.type,hasObject:!!p.object})}catch(h){g.warn("Failed to parse structured response as JSON",{error:h instanceof Error?h.message:String(h),responseText:a.substring(0,200)})}return p}catch(n){let o=Date.now()-t;throw g.error("SageMaker doGenerate failed",{duration:o,error:n instanceof Error?n.message:String(n)}),Fu(n,this.modelConfig.endpointName)}}async doStream(e){try{let t=this.extractPromptText(e);g.debug("SageMaker doStream called",{endpointName:this.modelConfig.endpointName,promptLength:t.length});let n=this.convertToSageMakerRequest(e),o={...n,parameters:{...typeof n.parameters=="object"&&n.parameters!==null?n.parameters:{},stream:!0}};g.debug("Attempting streaming generation",{endpointName:this.modelConfig.endpointName,hasStreamingFlag:!0});try{let s=await this.client.invokeEndpointWithStreaming({EndpointName:this.modelConfig.endpointName,Body:JSON.stringify(o),ContentType:this.modelConfig.contentType||"application/json",Accept:this.modelConfig.accept||"application/json"});return{stream:await QGe(s.Body,this.modelConfig.endpointName,this.config,{prompt:t,onChunk:a=>{g.debug("Streaming chunk received",{contentLength:a.content?.length||0,done:a.done})},onComplete:a=>{g.debug("Streaming completed",{usage:a,endpointName:this.modelConfig.endpointName})},onError:a=>{g.error("Streaming error",{error:a.message,endpointName:this.modelConfig.endpointName})}}),rawCall:{rawPrompt:n,rawSettings:this.modelConfig},rawResponse:{headers:{"Content-Type":s.ContentType||"application/json","X-Invoked-Production-Variant":s.InvokedProductionVariant||"unknown"}}}}catch(s){g.warn("Streaming failed, falling back to non-streaming",{endpointName:this.modelConfig.endpointName,error:s instanceof Error?s.message:String(s)});let i=await this.doGenerate(e);return{stream:new ReadableStream({async start(c){try{let l=dbr(i.text);for await(let u of l)c.enqueue({type:"text-delta",textDelta:u});c.enqueue({type:"finish",finishReason:i.finishReason,usage:i.usage}),c.close()}catch(l){c.error(l)}}}),rawCall:i.rawCall,rawResponse:i.rawResponse,request:i.request,warnings:[...i.warnings||[],{type:"other",message:"Streaming not supported, using synthetic stream"}]}}}catch(t){throw g.error("SageMaker doStream failed",{error:t instanceof Error?t.message:String(t)}),Fu(t,this.modelConfig.endpointName)}}convertToSageMakerRequest(e){let t=this.extractPromptText(e),n={inputs:t,parameters:{...e.maxTokens!==void 0?{max_new_tokens:e.maxTokens}:{},temperature:e.temperature||.7,top_p:e.topP||.9,stop:e.stopSequences||[]}},o=e.tools;if(o&&Array.isArray(o)&&o.length>0){n.tools=this.convertToolsToSageMakerFormat(o);let i=e.toolChoice;i&&(n.tool_choice=this.convertToolChoiceToSageMakerFormat(i)),g.debug("Added tool support to SageMaker request",{toolCount:o.length,toolChoice:i})}let s=e.responseFormat;return s&&(n.response_format=this.convertResponseFormatToSageMakerFormat(s),g.debug("Added structured output support to SageMaker request",{responseFormat:s.type})),g.debug("Converted to SageMaker request format",{inputLength:t.length,parameters:n.parameters,hasTools:!!n.tools}),n}convertToolsToSageMakerFormat(e){let t=[];for(let n of e){if(n.type!=="function"){g.debug("SageMaker: dropping tool with no SageMaker equivalent",{toolType:n.type,toolName:n.name});continue}t.push({type:"function",function:{name:n.name,description:n.description||"",parameters:n.inputSchema||{}}})}return t}convertToolChoiceToSageMakerFormat(e){if(typeof e=="string")return e;switch(e.type){case"auto":case"none":case"required":return e.type;case"tool":return{type:"function",function:{name:e.toolName}};default:return"auto"}}convertResponseFormatToSageMakerFormat(e){return e.type==="json_object"?{type:"json_object",schema:e.schema||void 0}:e.type==="json_schema"?{type:"json_schema",json_schema:{name:e.json_schema?.name||"response",description:e.json_schema?.description||"Generated response",schema:e.json_schema?.schema||{}}}:{type:"text"}}extractPromptText(e){let t=e.messages;if(t&&Array.isArray(t))return t.filter(o=>o.role&&o.content).map(o=>typeof o.content=="string"?`${o.role}: ${o.content}`:`${o.role}: ${JSON.stringify(o.content)}`).join(`
|
|
1446
1446
|
`);let n=e.prompt;return typeof n=="string"?n:Array.isArray(n)?n.filter(o=>o.role&&o.content).map(o=>typeof o.content=="string"?`${o.role}: ${o.content}`:`${o.role}: ${JSON.stringify(o.content)}`).join(`
|
|
1447
1447
|
`):String(n)}extractTextFromResponse(e){if(typeof e=="string")return e;if(e.generated_text)return e.generated_text;if(e.outputs)return e.outputs;if(e.text)return e.text;if(Array.isArray(e)&&e[0]?.generated_text)return e[0].generated_text;if(e.choices&&Array.isArray(e.choices)){let t=e.choices[0];if(t?.message?.content)return t.message.content}return JSON.stringify(e)}extractToolCallsFromResponse(e){if(e.choices&&Array.isArray(e.choices)){let t=e.choices[0];if(t?.message?.tool_calls)return t.message.tool_calls.map(n=>({type:"function",id:String(n.id||`call_${We()}`),function:{name:String(n.function.name),arguments:String(n.function.arguments)}}))}if(e.tool_calls&&Array.isArray(e.tool_calls))return e.tool_calls;if(e.content&&Array.isArray(e.content)){let t=e.content.filter(n=>n.type==="tool_use");if(t.length>0)return t.map(n=>({type:"function",id:String(n.id||`call_${We()}`),function:{name:String(n.name),arguments:JSON.stringify(n.input||{})}}))}}mapSageMakerFinishReason(e){switch(e?.toLowerCase()){case"stop":case"end_turn":case"stop_sequence":return"stop";case"length":case"max_tokens":case"max_length":return"length";case"content_filter":case"content_filtered":return"content-filter";case"tool_calls":case"function_call":return"tool-calls";case"error":return"error";default:return"unknown"}}getModelInfo(){return{modelId:this.modelId,provider:this.provider,specificationVersion:this.specificationVersion,endpointName:this.modelConfig.endpointName,modelType:this.modelConfig.modelType,region:this.config.region}}async testConnectivity(){try{return{success:!!(await this.doGenerate({inputFormat:"messages",mode:{type:"regular"},prompt:[{role:"user",content:[{type:"text",text:"Hello"}]}],maxTokens:10})).text}}catch(e){return{success:!1,error:e instanceof Error?e.message:String(e)}}}async doBatchGenerate(e,t){try{g.debug("SageMaker batch generate called",{batchSize:e.length,endpointName:this.modelConfig.endpointName});let n=await this.processPromptsInParallel(e,t);return g.debug("SageMaker batch generate completed",{batchSize:e.length,successCount:n.length}),n}catch(n){throw g.error("SageMaker batch generate failed",{error:n instanceof Error?n.message:String(n),batchSize:e.length}),Fu(n,this.modelConfig.endpointName)}}async processPromptsInParallel(e,t){let n=Math.min(this.modelConfig.initialConcurrency??pbr,e.length),o=this.modelConfig.maxConcurrency??mbr,s=this.modelConfig.minConcurrency??fbr,i=new Array(e.length),a=[],c=tVe(n,o,s),l=async(p,f)=>{await c.acquire();let h=Date.now();try{let y=await this.doGenerate({inputFormat:"messages",mode:{type:"regular"},prompt:[{role:"user",content:[{type:"text",text:p}]}],maxTokens:t?.maxTokens,temperature:t?.temperature,topP:t?.topP}),x=Date.now()-h;i[f]={text:y.text||"",usage:{promptTokens:y.usage.inputTokens??0,completionTokens:y.usage.outputTokens??0,total:y.usage.totalTokens??(y.usage.inputTokens??0)+(y.usage.outputTokens??0)},finishReason:y.finishReason,index:f},c.recordSuccess(x)}catch(y){a.push({index:f,error:y instanceof Error?y:new Error(String(y))});let x=Date.now()-h;c.recordError(x),i[f]={text:"",usage:{promptTokens:0,completionTokens:0,total:0},finishReason:"error",index:f}}finally{c.release()}},u=e.map((p,f)=>l(p,f));await Promise.all(u);let d=c.getMetrics();return g.debug("Parallel batch processing completed",{totalPrompts:e.length,successCount:d.completedCount,errorCount:d.errorCount,finalConcurrency:d.currentConcurrency,errorRate:d.errorCount/e.length,averageResponseTime:d.averageResponseTime}),a.length>0&&g.warn("Batch processing encountered errors",{errorCount:a.length,sampleErrors:a.slice(0,3).map(p=>({index:p.index,message:p.error.message}))}),i.map(({text:p,usage:f,finishReason:h})=>({text:p,usage:f,finishReason:h}))}getModelCapabilities(){return{...this.getModelInfo(),capabilities:{streaming:!0,toolCalling:!0,structuredOutput:!0,batchInference:!0,supportedResponseFormats:["text","json_object","json_schema"],supportedToolTypes:["function"],maxBatchSize:100,adaptiveConcurrency:!0,errorRecovery:!0}}}}});var oVe={};Q(oVe,{AmazonSageMakerProvider:()=>bH,default:()=>gbr});var bH,gbr,sVe=k(()=>{"use strict";Iu();H();Cn();Vr();UGe();yk();nVe();bH=class extends po{sagemakerModel;sagemakerConfig;modelConfig;constructor(e,t,n,o,s){super(e,"sagemaker",o);try{let i=NGe(s?.region??n);this.sagemakerConfig={...i,...s?.region!==void 0&&{region:s.region},...s?.accessKeyId!==void 0&&{accessKeyId:s.accessKeyId},...s?.secretAccessKey!==void 0&&{secretAccessKey:s.secretAccessKey},...s?.sessionToken!==void 0&&{sessionToken:s.sessionToken},...s?.endpoint!==void 0&&{endpoint:s.endpoint}},this.modelConfig=DGe(t||$ce());let a=new vH(this.modelName,this.sagemakerConfig,this.modelConfig);this.sagemakerModel=a,g.debug("Amazon SageMaker Provider initialized",{modelName:this.modelName,endpointName:this.modelConfig.endpointName,region:this.sagemakerConfig.region,provider:this.providerName})}catch(i){throw g.error("Failed to initialize SageMaker provider",{error:i instanceof Error?i.message:String(i),modelName:e,endpointName:t}),Fu(i)}}getProviderName(){return"sagemaker"}getDefaultModel(){return LGe()}getAISDKModel(){return this.sagemakerModel}async executeStream(e,t){return Ke({name:"neurolink.provider.sagemaker.stream",tracer:ke.stream,attributes:{"provider.name":"sagemaker","model.name":this.modelName,"sagemaker.endpoint":this.modelConfig.endpointName,"sagemaker.region":this.sagemakerConfig.region,"sagemaker.not_implemented":!0}},async()=>{try{throw new On("SageMaker streaming not yet fully implemented. Coming in next phase.",{code:"MODEL_ERROR",statusCode:501,endpoint:this.modelConfig.endpointName})}catch(n){throw this.handleProviderError(n)}})}formatProviderError(e){return e instanceof On?e:e instanceof Error&&e.name==="TimeoutError"?new On("SageMaker request timed out. Consider increasing timeout.",{code:"NETWORK_ERROR",statusCode:408,cause:e,endpoint:this.modelConfig.endpointName}):Fu(e,this.modelConfig.endpointName)}getSageMakerInfo(){return{endpointName:this.modelConfig.endpointName,modelType:this.modelConfig.modelType||"custom",region:this.sagemakerConfig.region,configured:!!(this.sagemakerConfig.accessKeyId&&this.sagemakerConfig.secretAccessKey)}}async testConnection(){try{return!this.sagemakerConfig.accessKeyId||!this.sagemakerConfig.secretAccessKey?{connected:!1,error:"AWS credentials not configured"}:!this.modelConfig.endpointName||this.modelConfig.endpointName==="default-endpoint"?{connected:!1,error:"SageMaker endpoint not configured"}:{connected:!0}}catch(e){return{connected:!1,error:e instanceof Error?e.message:String(e)}}}async getModel(){return this.getAISDKModel()}async testConnectivity(){let e=this.sagemakerModel;return e.testConnectivity?await e.testConnectivity():{success:!1,error:"Test method not available"}}getModelCapabilities(){let e=this.sagemakerModel;return e.getModelCapabilities?e.getModelCapabilities():{capabilities:{streaming:!0,toolCalling:!0,structuredOutput:!0,batchInference:!0,supportedResponseFormats:["text","json_object"],supportedToolTypes:["function"],maxBatchSize:10}}}},gbr=bH});var iVe={};Q(iVe,{DeepSeekProvider:()=>Qce});var hbr,ybr,xbr,Qce,aVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();hbr="https://api.deepseek.com",ybr=()=>Mr(y2e()),xbr=()=>Or("DEEPSEEK_MODEL","deepseek-chat"),Qce=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:ybr(),a=o?.baseURL?.trim()||process.env.DEEPSEEK_BASE_URL?.trim()||hbr;super("deepseek",e,t,{baseURL:a,apiKey:i}),g.debug("DeepSeek Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"deepseek"}getDefaultModel(){return xbr()}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"deepseek");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")?new Mt("Invalid DeepSeek API key. Please check your DEEPSEEK_API_KEY environment variable.","deepseek"):n.includes("rate limit")||n.includes("429")?new er("DeepSeek rate limit exceeded","deepseek"):n.includes("Insufficient Balance")||n.includes("insufficient_balance")||n.includes("402")?new qe("DeepSeek account has insufficient balance. Top up at https://platform.deepseek.com/usage","deepseek"):n.includes("model_not_found")||n.includes("404")?new Zt(`DeepSeek model '${this.modelName}' not found. Use 'deepseek-chat' or 'deepseek-reasoner'.`,"deepseek"):new qe(`DeepSeek error: ${n}`,"deepseek")}getFallbackModelName(){return"deepseek-chat"}getFallbackModels(){return["deepseek-chat","deepseek-reasoner"]}adjustResponseFormat(e,t){return e?.type==="json_schema"?{type:"json_object"}:e}}});var mVe={};Q(mVe,{NvidiaNimProvider:()=>ele,isNimFieldRejection:()=>dVe,stripFieldFromJsonBody:()=>pVe});var vbr,dVe,pVe,bbr,cVe,lVe,Tbr,Sbr,uVe,ele,fVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();vbr=["unsupported","not supported","does not support","unrecognized","unknown field","unknown parameter","unknown argument","invalid field","invalid parameter","invalid argument"],dVe=(r,e)=>{if(!r)return!1;let t=r.toLowerCase(),n=e.toLowerCase(),o=t.indexOf(n);for(;o!==-1;){let s=Math.max(0,o-80),i=Math.min(t.length,o+n.length+80),a=t.slice(s,i);if(vbr.some(c=>a.includes(c)))return!0;o=t.indexOf(n,o+n.length)}return!1},pVe=(r,e)=>{try{let t=JSON.parse(r),n=!1;if(e==="chat_template"&&"chat_template"in t&&(delete t.chat_template,n=!0),e==="reasoning_budget"){let o=t.chat_template_kwargs;o&&"reasoning_budget"in o&&(delete o.reasoning_budget,n=!0,Object.keys(o).length===0&&delete t.chat_template_kwargs)}return n?JSON.stringify(t):null}catch{return null}},bbr="https://integrate.api.nvidia.com/v1",cVe=r=>{let e=process.env[r];if(!e)return;let t=Number.parseInt(e,10);return Number.isFinite(t)?t:void 0},lVe=r=>{let e=process.env[r];if(!e)return;let t=Number.parseFloat(e);return Number.isFinite(t)?t:void 0},Tbr=(r,e)=>{let t={},n=cVe("NVIDIA_NIM_TOP_K");n!==void 0&&n!==-1&&(t.top_k=n);let o=lVe("NVIDIA_NIM_MIN_P");o!==void 0&&o!==0&&(t.min_p=o);let s=lVe("NVIDIA_NIM_REPETITION_PENALTY");s!==void 0&&s!==1&&(t.repetition_penalty=s);let i=cVe("NVIDIA_NIM_MIN_TOKENS");i!==void 0&&i!==0&&(t.min_tokens=i);let a=process.env.NVIDIA_NIM_CHAT_TEMPLATE;return a&&(t.chat_template=a),r&&(t.chat_template_kwargs={thinking:!0,enable_thinking:!0,...e?{reasoning_budget:e}:{}}),t},Sbr=()=>Mr(x2e()),uVe=()=>Or("NVIDIA_NIM_MODEL","meta/llama-3.3-70b-instruct"),ele=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Sbr(),a=o?.baseURL??process.env.NVIDIA_NIM_BASE_URL??bbr;super("nvidia-nim",e,t,{baseURL:a,apiKey:i}),g.debug("NVIDIA NIM Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"nvidia-nim"}getDefaultModel(){return uVe()}adjustBuildBodyOptions(e,t){let n=t,o=n.thinkingConfig,s=n.thinkingLevel??o?.thinkingLevel,i=s!==void 0&&s!=="minimal",a=typeof n.maxTokens=="number"?n.maxTokens:void 0,c=Tbr(i,a);return Object.keys(c).length>0?{...t,extraBody:c}:t}adjustBodyAfter400(e,t){let n=t.responseBody??"",o=JSON.stringify(e),s=[];for(let i of["chat_template","reasoning_budget"])if(dVe(n,i)){let a=pVe(o,i);a!==null&&(o=a,s.push(i))}if(s.length!==0)return g.debug("NVIDIA NIM: 400 rejected request field(s) \u2014 retrying once without them",{strippedFields:s,model:e.model,rejection:n.slice(0,200)}),JSON.parse(o)}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"nvidia-nim");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("401")||n.includes("Unauthorized")?new Mt("Invalid NVIDIA NIM API key. Get one at https://build.nvidia.com/settings/api-keys","nvidia-nim"):n.includes("rate limit")||n.includes("429")?new er("NVIDIA NIM rate limit exceeded","nvidia-nim"):n.includes("404")||n.includes("model_not_found")?new Zt(`NVIDIA NIM model '${this.modelName}' not available. Browse the catalog at https://build.nvidia.com/models`,"nvidia-nim"):n.includes("quota")||n.includes("403")?new qe("NVIDIA NIM quota exceeded for your account","nvidia-nim"):new qe(`NVIDIA NIM error: ${n}`,"nvidia-nim")}async validateConfiguration(){return typeof this.config.apiKey=="string"&&this.config.apiKey.trim().length>0}getConfiguration(){return{provider:this.providerName,model:this.modelName,defaultModel:uVe(),baseURL:this.config.baseURL}}}});var yVe={};Q(yVe,{LMStudioProvider:()=>tle});var wbr,gVe,hVe,Ebr,tle,xVe=k(()=>{"use strict";Eo();Dt();H();Pn();no();yi();wbr="http://localhost:1234/v1",gVe="lm-studio",hVe="local-model",Ebr=()=>process.env.LM_STUDIO_BASE_URL||wbr,tle=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey??process.env.LM_STUDIO_API_KEY??gVe,i=o?.baseURL??Ebr();super("lm-studio",e,t,{baseURL:i,apiKey:s}),g.debug("LM Studio Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"lm-studio"}getDefaultModel(){return process.env.LM_STUDIO_MODEL||""}getFallbackModelName(){return hVe}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"lm-studio");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error",o=t?.cause??{};return(t?.code??o?.code)==="ECONNREFUSED"||n.includes("ECONNREFUSED")||n.includes("Failed to fetch")||n.includes("fetch failed")?new zt(`LM Studio server not reachable at ${mr(this.config.baseURL)}. Open the LM Studio app, load a model, and click "Start Server".`,"lm-studio"):n.includes("model_not_found")||n.includes("404")?new Zt(`LM Studio model '${this.modelName}' is not loaded. Load it in the LM Studio app first.`,"lm-studio"):new qe(`LM Studio error: ${n}`,"lm-studio")}async validateConfiguration(){try{let e=`${this.config.baseURL.replace(/\/$/,"")}/models`,n=await vt()(e,{headers:this.config.apiKey&&this.config.apiKey!==gVe?{Authorization:`Bearer ${this.config.apiKey}`}:void 0,signal:AbortSignal.timeout(5e3)});return n.ok?!!(await n.json().catch(()=>null))?.data?.some(s=>typeof s?.id=="string"&&s.id.trim().length>0):!1}catch{return!1}}getConfiguration(){return{provider:this.providerName,model:this.modelName||this.resolvedModel||hVe,defaultModel:this.getDefaultModel(),baseURL:this.config.baseURL}}}});var vVe={};Q(vVe,{LlamaCppProvider:()=>rle});var _br,kbr,Cbr,rle,bVe=k(()=>{"use strict";Dt();H();Pn();no();yi();_br="http://localhost:8080/v1",kbr="llamacpp",Cbr=()=>process.env.LLAMACPP_BASE_URL||_br,rle=class extends Fr{constructor(e,t,n,o){let s=o?.baseURL?.trim()||Cbr(),i=o?.apiKey?.trim()||process.env.LLAMACPP_API_KEY||kbr;super("llamacpp",e,t,{baseURL:s,apiKey:i}),g.debug("llama.cpp Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"llamacpp"}getDefaultModel(){return process.env.LLAMACPP_MODEL||""}getFallbackModelName(){return"loaded-model"}getFallbackModels(){return["loaded-model"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"llamacpp");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error",o=t?.cause??{};return(t?.code??o?.code)==="ECONNREFUSED"||n.includes("ECONNREFUSED")||n.includes("Failed to fetch")||n.includes("fetch failed")?new zt(`llama.cpp server not reachable at ${mr(this.config.baseURL)}. Start it with: ./llama-server -m model.gguf --port 8080`,"llamacpp"):n.includes("400")?new qe("llama.cpp rejected the request. Common cause: model doesn't support tools (start llama-server with --jinja for tool support).","llamacpp"):new qe(`llama.cpp error: ${n}`,"llamacpp")}}});var TVe={};Q(TVe,{XaiProvider:()=>nle});var Rbr,Abr,Ibr,nle,SVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();Rbr="https://api.x.ai/v1",Abr=()=>Mr(v2e()),Ibr=()=>Or("XAI_MODEL","grok-3"),nle=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Abr(),a=o?.baseURL?.trim()||process.env.XAI_BASE_URL?.trim()||Rbr;super("xai",e,t,{baseURL:a,apiKey:i}),g.debug("xAI Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"xai"}getDefaultModel(){return Ibr()}getFallbackModelName(){return"grok-3-mini"}getFallbackModels(){return["grok-3","grok-3-mini","grok-2-latest","grok-2-vision-latest","grok-beta"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"xai");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")||n.includes("invalid_api_key")?new Mt("Invalid xAI API key. Please check your XAI_API_KEY environment variable. Get one at https://console.x.ai/","xai"):n.includes("rate limit")||n.includes("429")?new er("xAI rate limit exceeded. Back off and retry.","xai"):n.includes("model_not_found")||n.includes("404")?new Zt(`xAI model '${this.modelName}' not found. Use grok-2-latest, grok-3, grok-3-mini, grok-2-vision-latest, or grok-beta.`,"xai"):n.includes("insufficient_quota")||n.includes("quota exceeded")?new qe("xAI account has insufficient quota. Top up at https://console.x.ai/","xai"):new qe(`xAI error: ${n}`,"xai")}}});var wVe={};Q(wVe,{GroqProvider:()=>ole});var Pbr,Mbr,Obr,ole,EVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();Pbr="https://api.groq.com/openai/v1",Mbr=()=>Mr(b2e()),Obr=()=>Or("GROQ_MODEL","llama-3.3-70b-versatile"),ole=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Mbr(),a=o?.baseURL?.trim()||process.env.GROQ_BASE_URL?.trim()||Pbr;super("groq",e,t,{baseURL:a,apiKey:i}),g.debug("Groq Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"groq"}getDefaultModel(){return Obr()}getFallbackModelName(){return"llama-3.1-8b-instant"}getFallbackModels(){return["llama-3.3-70b-versatile","llama-3.1-8b-instant","gemma2-9b-it","mixtral-8x7b-32768","llama-3.2-90b-vision-preview","llama-3.2-11b-vision-preview"]}formatProviderError(e){if(e instanceof tr)return new qe(`Groq request timed out: ${e.message}`,"groq");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")||n.includes("invalid_api_key")?new Mt("Invalid Groq API key. Check GROQ_API_KEY. Get one at https://console.groq.com/keys","groq"):n.includes("rate limit")||n.includes("429")?new er("Groq rate limit exceeded. Free tier limits are tight; consider upgrading or backing off.","groq"):n.includes("model_not_found")||n.includes("404")||n.includes("model_decommissioned")?new Zt(n.includes("model_decommissioned")?`Groq model '${this.modelName}' was decommissioned. Pick a current model from https://console.groq.com/docs/models.`:`Groq model '${this.modelName}' not found. See https://console.groq.com/docs/models for the current catalog.`,"groq"):new qe(`Groq error: ${n}`,"groq")}}});var kVe={};Q(kVe,{CohereProvider:()=>sle});var Nbr,Dbr,_Ve,sle,CVe=k(()=>{"use strict";Dt();H();Pn();Eo();mo();no();wg();yi();Nbr="https://api.cohere.com/compatibility/v1",Dbr=()=>Mr(T2e()),_Ve=()=>Or("COHERE_MODEL","command-r-plus-08-2024"),sle=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Dbr(),a=o?.baseURL?.trim()||process.env.COHERE_BASE_URL?.trim()||Nbr;super("cohere",e,t,{baseURL:a,apiKey:i}),g.debug("Cohere Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"cohere"}getDefaultModel(){return _Ve()}getFallbackModelName(){return"command-r-08-2024"}getFallbackModels(){return["command-a-03-2025","command-a-reasoning-08-2025","command-r-plus-08-2024","command-r-08-2024","command-r7b-12-2024"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"cohere");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("invalid api token")||n.includes("Authentication")||n.includes("401")||n.includes("invalid_api_token")?new Mt("Invalid Cohere API key. Check COHERE_API_KEY. Get one at https://dashboard.cohere.com/api-keys","cohere"):n.includes("rate limit")||n.includes("429")?new er("Cohere rate limit exceeded. Back off and retry.","cohere"):n.includes("model_not_found")||n.includes("404")?new Zt(`Cohere model '${this.modelName}' not found. Use command-r, command-r-plus, or command-r7b-12-2024.`,"cohere"):n.includes("trial limit")||n.includes("trial_limit")?new qe("Cohere trial usage limit exceeded. Upgrade at https://dashboard.cohere.com/billing.","cohere"):new qe(`Cohere error: ${n}`,"cohere")}async validateConfiguration(){return typeof this.config.apiKey=="string"&&this.config.apiKey.trim().length>0}getConfiguration(){return{provider:this.providerName,model:this.modelName,defaultModel:_Ve(),baseURL:this.config.baseURL}}getDefaultEmbeddingModel(){return"embed-english-v3.0"}async embed(e,t){let n=await this.embedMany([e],t);if(!n[0])throw new qe("Cohere /v2/embed returned no embeddings.","cohere");return n[0]}async embedMany(e,t){if(e.length===0)return[];let n=t??this.getDefaultEmbeddingModel(),s=`${Ra(this.config.baseURL.replace(/\/compatibility\/v\d+\/?$/,""))}/v2/embed`,i=96,a=[],c=vt();for(let l=0;l<e.length;l+=i){let u=e.slice(l,l+i),d=di(3e4,this.providerName,"generate");try{let p=await c(s,{method:"POST",headers:{Authorization:`Bearer ${this.config.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify({model:n,texts:u,input_type:"search_document",embedding_types:["float"]}),...d?.controller.signal?{signal:d.controller.signal}:{}});if(!p.ok){let y=await p.text().catch(()=>"");throw this.formatProviderError(new Error(`Cohere /v2/embed failed: ${p.status} \u2014 ${y.slice(0,500)}`))}let f=await p.json(),h=f.embeddings?.float??(Array.isArray(f.embeddings)?f.embeddings:void 0);if(!h||h.length!==u.length)throw new qe(`Cohere /v2/embed returned ${h?.length??0} embeddings for ${u.length} inputs.`,"cohere");a.push(...h)}finally{d?.cleanup()}}return a}}});var RVe={};Q(RVe,{TogetherAIProvider:()=>ile});var Lbr,Ubr,Fbr,ile,AVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();Lbr="https://api.together.xyz/v1",Ubr=()=>Mr(w2e()),Fbr=()=>Or("TOGETHER_MODEL","meta-llama/Llama-3.3-70B-Instruct-Turbo"),ile=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Ubr(),a=o?.baseURL?.trim()||process.env.TOGETHER_BASE_URL?.trim()||Lbr;super("together-ai",e,t,{baseURL:a,apiKey:i}),g.debug("Together AI Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"together-ai"}getDefaultModel(){return Fbr()}getFallbackModelName(){return"meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"}getFallbackModels(){return["meta-llama/Llama-3.3-70B-Instruct-Turbo","meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo","meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo","meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo","mistralai/Mixtral-8x22B-Instruct-v0.1","Qwen/Qwen2.5-72B-Instruct-Turbo","deepseek-ai/DeepSeek-R1","deepseek-ai/DeepSeek-V3"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"together-ai");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")?new Mt("Invalid Together AI API key. Get one at https://api.together.xyz/settings/api-keys","together-ai"):n.includes("rate limit")||n.includes("429")?new er("Together AI rate limit exceeded. Back off and retry.","together-ai"):n.includes("model_not_found")||n.includes("404")?new Zt(`Together AI model '${this.modelName}' not found. Browse the catalog at https://api.together.xyz/models`,"together-ai"):new qe(`Together AI error: ${n}`,"together-ai")}}});var IVe={};Q(IVe,{FireworksProvider:()=>ale});var $br,Bbr,zbr,ale,PVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();$br="https://api.fireworks.ai/inference/v1",Bbr=()=>Mr(E2e()),zbr=()=>Or("FIREWORKS_MODEL","accounts/fireworks/models/deepseek-v4-pro"),ale=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Bbr(),a=o?.baseURL?.trim()||process.env.FIREWORKS_BASE_URL?.trim()||$br;super("fireworks",e,t,{baseURL:a,apiKey:i}),g.debug("Fireworks Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"fireworks"}getDefaultModel(){return zbr()}getFallbackModelName(){return"accounts/fireworks/models/deepseek-v4-pro"}getFallbackModels(){return["accounts/fireworks/models/deepseek-v4-pro","accounts/fireworks/models/glm-5p1","accounts/fireworks/models/glm-5","accounts/fireworks/models/kimi-k2p6","accounts/fireworks/models/kimi-k2p5","accounts/fireworks/models/gpt-oss-120b"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"fireworks");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")?new Mt("Invalid Fireworks API key. Get one at https://fireworks.ai/account/api-keys","fireworks"):n.includes("rate limit")||n.includes("429")?new er("Fireworks rate limit exceeded. Back off and retry.","fireworks"):n.includes("model_not_found")||n.includes("404")?new Zt(`Fireworks model '${this.modelName}' not found. Browse https://fireworks.ai/models`,"fireworks"):new qe(`Fireworks error: ${n}`,"fireworks")}}});var MVe={};Q(MVe,{PerplexityProvider:()=>cle});var jbr,Gbr,Vbr,cle,OVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();jbr="https://api.perplexity.ai",Gbr=()=>Mr(_2e()),Vbr=()=>Or("PERPLEXITY_MODEL","sonar"),cle=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Gbr(),a=o?.baseURL?.trim()||process.env.PERPLEXITY_BASE_URL?.trim()||jbr;super("perplexity",e,t,{baseURL:a,apiKey:i}),g.debug("Perplexity Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"perplexity"}getDefaultModel(){return Vbr()}getFallbackModels(){return["sonar","sonar-pro","sonar-reasoning","sonar-reasoning-pro","sonar-deep-research"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"perplexity");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")?new Mt("Invalid Perplexity API key. Get one at https://www.perplexity.ai/settings/api","perplexity"):n.includes("rate limit")||n.includes("429")?new er("Perplexity rate limit exceeded. Back off and retry.","perplexity"):n.includes("model_not_found")||n.includes("404")?new Zt(`Perplexity model '${this.modelName}' not found. Use sonar, sonar-pro, sonar-reasoning, sonar-reasoning-pro, or sonar-deep-research.`,"perplexity"):new qe(`Perplexity error: ${n}`,"perplexity")}}});var NVe={};Q(NVe,{CloudflareProvider:()=>lle});var qbr,Hbr,Wbr,lle,DVe=k(()=>{"use strict";Dt();H();Pn();mo();no();yi();qbr=r=>`https://api.cloudflare.com/client/v4/accounts/${r}/ai/v1`,Hbr=()=>Mr(I2e()),Wbr=()=>Or("CLOUDFLARE_MODEL","@cf/meta/llama-3.3-70b-instruct-fp8-fast"),lle=class extends Fr{constructor(e,t,n,o){let s=o?.apiKey?.trim(),i=s&&s.length>0?s:Hbr(),a=(o?.accountId??process.env.CLOUDFLARE_ACCOUNT_ID??"").trim();if(!a)throw new Error("CLOUDFLARE_ACCOUNT_ID is required (or pass credentials.cloudflare.accountId). Get the account id from https://dash.cloudflare.com/");let c=o?.baseURL??qbr(a);super("cloudflare",e,t,{baseURL:c,apiKey:i}),g.debug("Cloudflare Workers AI Provider initialized",{modelName:this.modelName,providerName:this.providerName,baseURL:mr(this.config.baseURL)})}getProviderName(){return"cloudflare"}getDefaultModel(){return Wbr()}getFallbackModelName(){return"@cf/meta/llama-3.1-8b-instruct-fast"}getFallbackModels(){return["@cf/meta/llama-3.3-70b-instruct-fp8-fast","@cf/meta/llama-3.1-70b-instruct","@cf/meta/llama-3.1-8b-instruct-fast","@cf/meta/llama-3.2-11b-vision-instruct","@cf/mistral/mistral-7b-instruct-v0.2","@cf/qwen/qwen1.5-14b-chat-awq"]}formatProviderError(e){if(e instanceof tr)return new zt(`Request timed out: ${e.message}`,"cloudflare");let t=e,n=typeof t?.message=="string"?t.message:"Unknown error";return n.includes("Invalid API key")||n.includes("Authentication")||n.includes("401")?new Mt("Invalid Cloudflare API key. Use a token with Workers AI Read+Write scope. Get one at https://dash.cloudflare.com/profile/api-tokens","cloudflare"):n.includes("rate limit")||n.includes("429")?new er("Cloudflare Workers AI rate limit exceeded. Free-tier neurons reset daily.","cloudflare"):n.includes("model_not_found")||n.includes("404")?new Zt(`Cloudflare model '${this.modelName}' not found. Browse https://developers.cloudflare.com/workers-ai/models/`,"cloudflare"):new qe(`Cloudflare Workers AI error: ${n}`,"cloudflare")}}});function Qc(r){let{traceId:e,parentSpanId:t}=id(),n=ve.createSpan("context.compaction","context.budgetCheck",{"context.operation":"budgetCheck"},t,e),o=Date.now();try{let{provider:s,model:i,maxTokens:a,systemPrompt:c,conversationMessages:l,currentPrompt:u,toolDefinitions:d,fileAttachments:p,compactionThreshold:f=Kbr}=r,h=by(s,i,a),y=c?bn(c,s)+4:0,x=l?.length?wo(l,s):0,b=u?bn(u,s)+4:0,T=d?.length?d.reduce((M,D)=>{try{let B=JSON.stringify(D);return M+bn(B,s)}catch{return M+Jbr}},0):0,S=p?.length?p.reduce((M,D)=>M+bn(D.content,s),0):0,_=y+x+b+T+S,C=h>0?_/h:1,R=_<=h,P=C>=f,A={withinBudget:R,estimatedInputTokens:_,availableInputTokens:h,usageRatio:C,shouldCompact:P,breakdown:{systemPrompt:y,conversationHistory:x,currentPrompt:b,toolDefinitions:T,fileAttachments:S}};n.durationMs=Date.now()-o;let I=ve.endSpan(ve.updateAttributes(n,{"context.budgetUsage":C,"context.triggered":P,"context.estimatedTokens":_,"context.availableTokens":h}),1);return Ze().recordSpan(I),A}catch(s){n.durationMs=Date.now()-o;let i=ve.endSpan(n,2);throw i.statusMessage=s instanceof Error?s.message:String(s),Ze().recordSpan(i),s}}var Kbr,Jbr,UVe=k(()=>{"use strict";eM();Ed();xo();qv();Kbr=.8,Jbr=200});function FVe(r,e){let t=e?.protectTokens??Ybr,n=e?.minimumSavings??Xbr,o=new Set(e?.protectedTools??["skill"]),s=e?.provider,i=[...r],a=0,c=0;for(let u=i.length-1;u>=0;u--){let d=i[u];if(d.role!=="tool_result"||d.tool&&o.has(d.tool))continue;let p=bn(d.content,s);if(a<t){a+=p;continue}d.content!==ule&&(c+=p-bn(ule,s),i[u]={...d,content:ule,metadata:{...d.metadata,truncated:!0}})}let l=c>=n;return{pruned:l,messages:l?i:r,tokensSaved:l?c:0}}var ule,Ybr,Xbr,$Ve=k(()=>{"use strict";Ed();ule="[Tool result cleared]",Ybr=4e4,Xbr=2e4});function BVe(r){let e=new Map;for(let c=0;c<r.length;c++){let l=r[c];if(l.role!=="tool_result"&&l.role!=="assistant")continue;let u=l.content.match(Zbr);if(u){let d=u[1],p=e.get(d)||[];p.push(c),e.set(d,p)}}let t=new Map;for(let[c,l]of e)l.length>1&&t.set(c,l);if(t.size===0)return{deduplicated:!1,messages:r,filesDeduped:0};let n=[...r],o=0,s=0;for(let[c,l]of t){let u=l[l.length-1];for(let d of l){if(d===u)continue;let p=n[d];o+=p.content.length;let f=Qbr(c);s+=f.length,n[d]={...p,content:f,metadata:{...p.metadata,truncated:!0}}}}let a=(o>0?(o-s)/o:0)>=.3;return{deduplicated:a,messages:a?n:r,filesDeduped:a?t.size:0}}var Zbr,Qbr,zVe=k(()=>{"use strict";Zbr=/(?:read|reading|read_file|readFile|Read file|cat)\s+['"`]?([^\s'"`\n]+)/i,Qbr=r=>`[File ${r} - refer to latest read below]`});function jVe(r){for(let e=1;e<r.length;e++)r[e].role===r[e-1].role&&r[e].role!=="system"&&g.warn(`[SlidingWindowTruncator] Role alternation broken at index ${e}: consecutive "${r[e].role}" messages`)}function tTr(r,e){if(!e?.targetTokens)return{truncated:!1,messages:r,messagesRemoved:0};let t=e.provider,n=e.targetTokens;if(wo(r,t)<=n)return{truncated:!1,messages:r,messagesRemoved:0};let s=200,i=24+r.length*4,a=n-i;if(a<=0)return{truncated:!1,messages:r,messagesRemoved:0};let c=r.map(p=>bn(p.content,t)),l=c.reduce((p,f)=>p+f,0),u=[...r],d=0;for(let p=0;p<u.length;p++){let f=u[p];if(f.role==="system"||f.metadata?.isSummary||f.metadata?.isSkill)continue;let h=Math.floor(c[p]/l*a),y=Math.max(s,h);if(c[p]>y){let x=s4(f.content,y,t);x.truncated&&(d+=c[p]-bn(x.text,t),u[p]={...f,content:x.text,metadata:{...f.metadata,truncated:!0}})}}if(d>0){let p=wo(u,t);return g.info("[Truncation] Small conversation content truncated",{messageCount:r.length,tokensSaved:d,targetTokens:n,finalTokens:p}),{truncated:p<=n,messages:u,messagesRemoved:0}}return{truncated:!1,messages:r,messagesRemoved:0}}function GVe(r,e){let t=new Map,n=[],o=[];for(let h=0;h<r.length;h++){let y=r[h];if(!y.metadata?.isSkill){o.push(y);continue}let x=r.slice(h+1).find(b=>!b.metadata?.isSkill);if(x){let b=t.get(x.id);b?b.push(y):t.set(x.id,[y])}else n.push(y)}if(o.length<=4)return tTr(r,e);let s;if(e?.currentTokens&&e?.targetTokens&&e.currentTokens>e.targetTokens){let h=(e.currentTokens-e.targetTokens)/e.currentTokens,y=e?.adaptiveBuffer??.15;s=Math.min(.9,Math.max(.1,h+y)),g.info("[Truncation] Adaptive fraction calculated",{currentTokens:e.currentTokens,targetTokens:e.targetTokens,overageRatio:Math.round(h*100),fraction:Math.round(s*100)})}else s=e?.fraction??.5;let i=o.slice(0,2),a=o.slice(2),c=(h,y)=>{let x=new Set([...i,...h].map(S=>S.id)),b=[];for(let[S,_]of t)x.has(S)||b.push(..._);let T=S=>{let _=t.get(S.id);return _?[..._,S]:[S]};return[...i.flatMap(T),y,...b,...h.flatMap(T),...n]},l=()=>{let h=We();return{id:`truncation-marker-${h}`,role:"user",content:eTr,isTruncationMarker:!0,truncationId:h}},u=e?.maxIterations??3,d=s;for(let h=0;h<u;h++){let y=Math.floor(a.length*d),x=y-y%2;if(x<=0)break;let b=c(a.slice(x),l());if(jVe(b),e?.targetTokens){if(wo(b,e.provider)<=e.targetTokens)return{truncated:!0,messages:b,messagesRemoved:x};d=Math.min(.95,d+.1);continue}return{truncated:!0,messages:b,messagesRemoved:x}}let p=Math.floor(a.length*.95),f=p-p%2;if(f>0){let h=c(a.slice(f),l());return jVe(h),{truncated:!0,messages:h,messagesRemoved:f}}return{truncated:!1,messages:r,messagesRemoved:0}}var eTr,VVe=k(()=>{"use strict";Ed();H();vr();eTr="[Earlier conversation history was truncated to fit within context limits]"});function qVe(r,e){if(!(r&&r.length>0||e&&e.length>0))return"";let n=`
|
|
1448
1448
|
|
|
@@ -105,11 +105,24 @@ export declare class SageMakerLanguageModel implements SageMakerAsLanguageModel
|
|
|
105
105
|
*/
|
|
106
106
|
private convertToSageMakerRequest;
|
|
107
107
|
/**
|
|
108
|
-
* Convert
|
|
108
|
+
* Convert AI SDK tools (`LanguageModelV3FunctionTool | LanguageModelV3ProviderTool`,
|
|
109
|
+
* the same union every other provider's tool converter accepts — see
|
|
110
|
+
* `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts) into the SageMaker
|
|
111
|
+
* wire format, which mirrors the OpenAI Chat Completions nested
|
|
112
|
+
* `{ function: { name, description, parameters } }` convention.
|
|
113
|
+
*
|
|
114
|
+
* Function tools carry their fields flat (`tool.name`, `tool.inputSchema`,
|
|
115
|
+
* etc.) — there is no `tool.function` sub-object on the AI SDK side.
|
|
116
|
+
* `type: "provider"` tools (provider-defined tools like web search) have
|
|
117
|
+
* no SageMaker equivalent; they're dropped explicitly with a debug log
|
|
118
|
+
* rather than crashing or being sent malformed.
|
|
109
119
|
*/
|
|
110
120
|
private convertToolsToSageMakerFormat;
|
|
111
121
|
/**
|
|
112
|
-
* Convert
|
|
122
|
+
* Convert an AI SDK tool choice (`LanguageModelV3ToolChoice`) into the
|
|
123
|
+
* SageMaker/OpenAI-style wire format. The AI SDK shape is
|
|
124
|
+
* `{ type: "auto" | "none" | "required" }` or `{ type: "tool", toolName }`
|
|
125
|
+
* — never the nested `{ type: "function", function: { name } }` shape.
|
|
113
126
|
*/
|
|
114
127
|
private convertToolChoiceToSageMakerFormat;
|
|
115
128
|
/**
|
|
@@ -370,12 +370,19 @@ export class SageMakerLanguageModel {
|
|
|
370
370
|
stop: options.stopSequences || [],
|
|
371
371
|
},
|
|
372
372
|
};
|
|
373
|
-
// Add tool support if tools are present
|
|
373
|
+
// Add tool support if tools are present. `options.tools` arrives here in
|
|
374
|
+
// the AI SDK's LanguageModelV2/V3 call-options shape — a flat
|
|
375
|
+
// `{ type: "function", name, description?, inputSchema }` per tool (see
|
|
376
|
+
// `LanguageModelV3FunctionTool` in @ai-sdk/provider), the same shape
|
|
377
|
+
// every other provider's tool converter consumes (compare
|
|
378
|
+
// `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts). It is NOT the
|
|
379
|
+
// nested OpenAI Chat Completions wire format.
|
|
374
380
|
const tools = options.tools;
|
|
375
381
|
if (tools && Array.isArray(tools) && tools.length > 0) {
|
|
376
382
|
request.tools = this.convertToolsToSageMakerFormat(tools);
|
|
377
383
|
// Add tool choice if specified
|
|
378
|
-
const toolChoice = options
|
|
384
|
+
const toolChoice = options
|
|
385
|
+
.toolChoice;
|
|
379
386
|
if (toolChoice) {
|
|
380
387
|
request.tool_choice =
|
|
381
388
|
this.convertToolChoiceToSageMakerFormat(toolChoice);
|
|
@@ -403,39 +410,59 @@ export class SageMakerLanguageModel {
|
|
|
403
410
|
return request;
|
|
404
411
|
}
|
|
405
412
|
/**
|
|
406
|
-
* Convert
|
|
413
|
+
* Convert AI SDK tools (`LanguageModelV3FunctionTool | LanguageModelV3ProviderTool`,
|
|
414
|
+
* the same union every other provider's tool converter accepts — see
|
|
415
|
+
* `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts) into the SageMaker
|
|
416
|
+
* wire format, which mirrors the OpenAI Chat Completions nested
|
|
417
|
+
* `{ function: { name, description, parameters } }` convention.
|
|
418
|
+
*
|
|
419
|
+
* Function tools carry their fields flat (`tool.name`, `tool.inputSchema`,
|
|
420
|
+
* etc.) — there is no `tool.function` sub-object on the AI SDK side.
|
|
421
|
+
* `type: "provider"` tools (provider-defined tools like web search) have
|
|
422
|
+
* no SageMaker equivalent; they're dropped explicitly with a debug log
|
|
423
|
+
* rather than crashing or being sent malformed.
|
|
407
424
|
*/
|
|
408
425
|
convertToolsToSageMakerFormat(tools) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
},
|
|
418
|
-
};
|
|
426
|
+
const converted = [];
|
|
427
|
+
for (const tool of tools) {
|
|
428
|
+
if (tool.type !== "function") {
|
|
429
|
+
logger.debug("SageMaker: dropping tool with no SageMaker equivalent", {
|
|
430
|
+
toolType: tool.type,
|
|
431
|
+
toolName: tool.name,
|
|
432
|
+
});
|
|
433
|
+
continue;
|
|
419
434
|
}
|
|
420
|
-
|
|
421
|
-
|
|
435
|
+
converted.push({
|
|
436
|
+
type: "function",
|
|
437
|
+
function: {
|
|
438
|
+
name: tool.name,
|
|
439
|
+
description: tool.description || "",
|
|
440
|
+
parameters: tool.inputSchema || {},
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
return converted;
|
|
422
445
|
}
|
|
423
446
|
/**
|
|
424
|
-
* Convert
|
|
447
|
+
* Convert an AI SDK tool choice (`LanguageModelV3ToolChoice`) into the
|
|
448
|
+
* SageMaker/OpenAI-style wire format. The AI SDK shape is
|
|
449
|
+
* `{ type: "auto" | "none" | "required" }` or `{ type: "tool", toolName }`
|
|
450
|
+
* — never the nested `{ type: "function", function: { name } }` shape.
|
|
425
451
|
*/
|
|
426
452
|
convertToolChoiceToSageMakerFormat(toolChoice) {
|
|
427
453
|
if (typeof toolChoice === "string") {
|
|
428
|
-
return toolChoice; //
|
|
454
|
+
return toolChoice; // Defensive: tolerate a raw string if ever passed.
|
|
429
455
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
456
|
+
switch (toolChoice.type) {
|
|
457
|
+
case "auto":
|
|
458
|
+
case "none":
|
|
459
|
+
case "required":
|
|
460
|
+
return toolChoice.type;
|
|
461
|
+
case "tool":
|
|
462
|
+
return { type: "function", function: { name: toolChoice.toolName } };
|
|
463
|
+
default:
|
|
464
|
+
return "auto";
|
|
437
465
|
}
|
|
438
|
-
return toolChoice;
|
|
439
466
|
}
|
|
440
467
|
/**
|
|
441
468
|
* Convert Vercel AI SDK response format to SageMaker format (Phase 4)
|
|
@@ -105,11 +105,24 @@ export declare class SageMakerLanguageModel implements SageMakerAsLanguageModel
|
|
|
105
105
|
*/
|
|
106
106
|
private convertToSageMakerRequest;
|
|
107
107
|
/**
|
|
108
|
-
* Convert
|
|
108
|
+
* Convert AI SDK tools (`LanguageModelV3FunctionTool | LanguageModelV3ProviderTool`,
|
|
109
|
+
* the same union every other provider's tool converter accepts — see
|
|
110
|
+
* `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts) into the SageMaker
|
|
111
|
+
* wire format, which mirrors the OpenAI Chat Completions nested
|
|
112
|
+
* `{ function: { name, description, parameters } }` convention.
|
|
113
|
+
*
|
|
114
|
+
* Function tools carry their fields flat (`tool.name`, `tool.inputSchema`,
|
|
115
|
+
* etc.) — there is no `tool.function` sub-object on the AI SDK side.
|
|
116
|
+
* `type: "provider"` tools (provider-defined tools like web search) have
|
|
117
|
+
* no SageMaker equivalent; they're dropped explicitly with a debug log
|
|
118
|
+
* rather than crashing or being sent malformed.
|
|
109
119
|
*/
|
|
110
120
|
private convertToolsToSageMakerFormat;
|
|
111
121
|
/**
|
|
112
|
-
* Convert
|
|
122
|
+
* Convert an AI SDK tool choice (`LanguageModelV3ToolChoice`) into the
|
|
123
|
+
* SageMaker/OpenAI-style wire format. The AI SDK shape is
|
|
124
|
+
* `{ type: "auto" | "none" | "required" }` or `{ type: "tool", toolName }`
|
|
125
|
+
* — never the nested `{ type: "function", function: { name } }` shape.
|
|
113
126
|
*/
|
|
114
127
|
private convertToolChoiceToSageMakerFormat;
|
|
115
128
|
/**
|
|
@@ -370,12 +370,19 @@ export class SageMakerLanguageModel {
|
|
|
370
370
|
stop: options.stopSequences || [],
|
|
371
371
|
},
|
|
372
372
|
};
|
|
373
|
-
// Add tool support if tools are present
|
|
373
|
+
// Add tool support if tools are present. `options.tools` arrives here in
|
|
374
|
+
// the AI SDK's LanguageModelV2/V3 call-options shape — a flat
|
|
375
|
+
// `{ type: "function", name, description?, inputSchema }` per tool (see
|
|
376
|
+
// `LanguageModelV3FunctionTool` in @ai-sdk/provider), the same shape
|
|
377
|
+
// every other provider's tool converter consumes (compare
|
|
378
|
+
// `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts). It is NOT the
|
|
379
|
+
// nested OpenAI Chat Completions wire format.
|
|
374
380
|
const tools = options.tools;
|
|
375
381
|
if (tools && Array.isArray(tools) && tools.length > 0) {
|
|
376
382
|
request.tools = this.convertToolsToSageMakerFormat(tools);
|
|
377
383
|
// Add tool choice if specified
|
|
378
|
-
const toolChoice = options
|
|
384
|
+
const toolChoice = options
|
|
385
|
+
.toolChoice;
|
|
379
386
|
if (toolChoice) {
|
|
380
387
|
request.tool_choice =
|
|
381
388
|
this.convertToolChoiceToSageMakerFormat(toolChoice);
|
|
@@ -403,39 +410,59 @@ export class SageMakerLanguageModel {
|
|
|
403
410
|
return request;
|
|
404
411
|
}
|
|
405
412
|
/**
|
|
406
|
-
* Convert
|
|
413
|
+
* Convert AI SDK tools (`LanguageModelV3FunctionTool | LanguageModelV3ProviderTool`,
|
|
414
|
+
* the same union every other provider's tool converter accepts — see
|
|
415
|
+
* `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts) into the SageMaker
|
|
416
|
+
* wire format, which mirrors the OpenAI Chat Completions nested
|
|
417
|
+
* `{ function: { name, description, parameters } }` convention.
|
|
418
|
+
*
|
|
419
|
+
* Function tools carry their fields flat (`tool.name`, `tool.inputSchema`,
|
|
420
|
+
* etc.) — there is no `tool.function` sub-object on the AI SDK side.
|
|
421
|
+
* `type: "provider"` tools (provider-defined tools like web search) have
|
|
422
|
+
* no SageMaker equivalent; they're dropped explicitly with a debug log
|
|
423
|
+
* rather than crashing or being sent malformed.
|
|
407
424
|
*/
|
|
408
425
|
convertToolsToSageMakerFormat(tools) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
},
|
|
418
|
-
};
|
|
426
|
+
const converted = [];
|
|
427
|
+
for (const tool of tools) {
|
|
428
|
+
if (tool.type !== "function") {
|
|
429
|
+
logger.debug("SageMaker: dropping tool with no SageMaker equivalent", {
|
|
430
|
+
toolType: tool.type,
|
|
431
|
+
toolName: tool.name,
|
|
432
|
+
});
|
|
433
|
+
continue;
|
|
419
434
|
}
|
|
420
|
-
|
|
421
|
-
|
|
435
|
+
converted.push({
|
|
436
|
+
type: "function",
|
|
437
|
+
function: {
|
|
438
|
+
name: tool.name,
|
|
439
|
+
description: tool.description || "",
|
|
440
|
+
parameters: tool.inputSchema || {},
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
return converted;
|
|
422
445
|
}
|
|
423
446
|
/**
|
|
424
|
-
* Convert
|
|
447
|
+
* Convert an AI SDK tool choice (`LanguageModelV3ToolChoice`) into the
|
|
448
|
+
* SageMaker/OpenAI-style wire format. The AI SDK shape is
|
|
449
|
+
* `{ type: "auto" | "none" | "required" }` or `{ type: "tool", toolName }`
|
|
450
|
+
* — never the nested `{ type: "function", function: { name } }` shape.
|
|
425
451
|
*/
|
|
426
452
|
convertToolChoiceToSageMakerFormat(toolChoice) {
|
|
427
453
|
if (typeof toolChoice === "string") {
|
|
428
|
-
return toolChoice; //
|
|
454
|
+
return toolChoice; // Defensive: tolerate a raw string if ever passed.
|
|
429
455
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
456
|
+
switch (toolChoice.type) {
|
|
457
|
+
case "auto":
|
|
458
|
+
case "none":
|
|
459
|
+
case "required":
|
|
460
|
+
return toolChoice.type;
|
|
461
|
+
case "tool":
|
|
462
|
+
return { type: "function", function: { name: toolChoice.toolName } };
|
|
463
|
+
default:
|
|
464
|
+
return "auto";
|
|
437
465
|
}
|
|
438
|
-
return toolChoice;
|
|
439
466
|
}
|
|
440
467
|
/**
|
|
441
468
|
* Convert Vercel AI SDK response format to SageMaker format (Phase 4)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juspay/neurolink",
|
|
3
|
-
"version": "9.92.
|
|
3
|
+
"version": "9.92.2",
|
|
4
4
|
"packageManager": "pnpm@10.15.1",
|
|
5
5
|
"description": "TypeScript AI SDK with 24+ LLM providers behind one consistent API. MCP-native (connect any MCP server), voice TTS/STT/realtime, RAG, agents, memory, context compaction. OpenAI · Anthropic · Gemini · Bedrock · Azure · Ollama · DeepSeek · NVIDIA NIM and more.",
|
|
6
6
|
"author": {
|
|
@@ -128,6 +128,7 @@
|
|
|
128
128
|
"test:autoresearch": "npx tsx test/continuous-test-suite-autoresearch.ts",
|
|
129
129
|
"test:autoresearch:redis": "npx tsx test/continuous-test-suite-autoresearch-redis.ts",
|
|
130
130
|
"test:anthropic-tools-policy": "npx tsx test/continuous-test-suite-anthropic-tools-policy.ts",
|
|
131
|
+
"test:sagemaker-tools": "npx tsx test/continuous-test-suite-sagemaker-tools.ts",
|
|
131
132
|
"test:anthropic-multimodal": "npx tsx test/continuous-test-suite-anthropic-multimodal.ts",
|
|
132
133
|
"test:excel-interop": "npx tsx test/continuous-test-suite-excel-interop.ts",
|
|
133
134
|
"test:envguard": "npx tsx test/helpers/envGuard.test.ts",
|
|
@@ -145,7 +146,7 @@
|
|
|
145
146
|
"test:system-messages:vitest": "pnpm exec vitest run test/systemMessages.test.ts",
|
|
146
147
|
"test:tool-routing-semantic:vitest": "pnpm exec vitest run test/toolRoutingSemantic.test.ts",
|
|
147
148
|
"test:tool-routing-semantic": "pnpm run test:tool-routing-semantic:vitest && npx tsx test/continuous-test-suite-tool-routing-semantic.ts",
|
|
148
|
-
"test:unit": "pnpm run test:envguard && pnpm run test:bugfixes && pnpm run test:file-detector-extension && pnpm run test:mcp:infra && pnpm run test:mcp:bash && pnpm run test:mcp:limits && pnpm run test:mcp:spans && pnpm run test:autoresearch:redis && pnpm run test:unit:vitest && pnpm run test:tool-routing-cli:vitest && pnpm run test:tool-dedup:vitest && pnpm run test:model-pool:vitest && pnpm run test:system-messages:vitest && pnpm run test:tool-routing-semantic:vitest && pnpm run test:anthropic-tools-policy && pnpm run test:anthropic-multimodal && pnpm run test:excel-interop",
|
|
149
|
+
"test:unit": "pnpm run test:envguard && pnpm run test:bugfixes && pnpm run test:file-detector-extension && pnpm run test:mcp:infra && pnpm run test:mcp:bash && pnpm run test:mcp:limits && pnpm run test:mcp:spans && pnpm run test:autoresearch:redis && pnpm run test:unit:vitest && pnpm run test:tool-routing-cli:vitest && pnpm run test:tool-dedup:vitest && pnpm run test:model-pool:vitest && pnpm run test:system-messages:vitest && pnpm run test:tool-routing-semantic:vitest && pnpm run test:anthropic-tools-policy && pnpm run test:sagemaker-tools && pnpm run test:anthropic-multimodal && pnpm run test:excel-interop",
|
|
149
150
|
"// CI tier — live providers, runs only when API keys are present (test:credentials and test:dynamic make real provider calls when keys are set, so they live here, not in test:unit)": "",
|
|
150
151
|
"test:live": "pnpm run test:providers && pnpm run test:mcp:http && pnpm run test:mcp:sdk && pnpm run test:mcp:cli && pnpm run test:observability && pnpm run test:context && pnpm run test:memory && pnpm run test:tool-reliability && pnpm run test:evaluation && pnpm run test:autoresearch && pnpm run test:credentials && pnpm run test:dynamic",
|
|
151
152
|
"// CI tier — product output (image/video/TTS/PPT) — costs $$ per run": "",
|